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 188617283 : 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 188617283 : 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 188617283 : if (expr == nullptr_node)
158 : return true;
159 :
160 188616600 : return (TREE_CODE (expr) == INTEGER_CST
161 19100924 : && !TREE_OVERFLOW (expr)
162 19100827 : && integer_zerop (expr)
163 192548616 : && (INTEGRAL_TYPE_P (type)
164 333842 : || (TREE_CODE (type) == POINTER_TYPE
165 333840 : && VOID_TYPE_P (TREE_TYPE (type))
166 258556 : && 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 33758933 : remove_c_maybe_const_expr (tree expr)
198 : {
199 33758933 : 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 773419010 : require_complete_type (location_t loc, tree value)
219 : {
220 773419010 : tree type = TREE_TYPE (value);
221 :
222 773419010 : if (error_operand_p (value))
223 8916 : return error_mark_node;
224 :
225 : /* First, detect a valid value with a complete type. */
226 773410094 : 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 232 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
240 : {
241 : /* Avoid duplicate error message. */
242 232 : if (TREE_CODE (type) == ERROR_MARK)
243 : return;
244 :
245 232 : 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 134 : retry:
250 : /* We must print an error message. Be clever about what it says. */
251 :
252 134 : switch (TREE_CODE (type))
253 : {
254 90 : case RECORD_TYPE:
255 90 : case UNION_TYPE:
256 90 : case ENUMERAL_TYPE:
257 90 : 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 90 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
282 88 : 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 124525351 : c_type_promotes_to (tree type)
294 : {
295 124525351 : tree ret = NULL_TREE;
296 :
297 124525351 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
298 1504107 : ret = double_type_node;
299 123021244 : else if (c_promoting_integer_type_p (type))
300 : {
301 : /* Preserve unsignedness if not really getting any wider. */
302 18658365 : if (TYPE_UNSIGNED (type)
303 18658365 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
304 0 : ret = unsigned_type_node;
305 : else
306 18658365 : ret = integer_type_node;
307 : }
308 :
309 20162472 : if (ret != NULL_TREE)
310 20162472 : return (TYPE_ATOMIC (type)
311 20162472 : ? 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 7356231 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
323 : {
324 7356231 : if (as1 == as2)
325 : {
326 7356231 : *common = as1;
327 7356231 : 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 2597850 : qualify_type (tree type, tree like)
348 : {
349 2597850 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
350 2597850 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
351 2597850 : 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 2597850 : 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 5195700 : return c_build_qualified_type (type,
363 2597850 : TYPE_QUALS_NO_ADDR_SPACE (type)
364 2597850 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
365 2597850 : | 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 140426429 : c_verify_type (tree type)
377 : {
378 140426429 : switch (TREE_CODE (type))
379 : {
380 67143059 : case POINTER_TYPE:
381 67143059 : case FUNCTION_TYPE:
382 67143059 : case ARRAY_TYPE:
383 : /* Pointer, array, functions are variably modified if and only if the
384 : target, element, return type is variably modified. */
385 67143059 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
386 67143059 : && (C_TYPE_VARIABLY_MODIFIED (type)
387 66960582 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))))
388 : return false;
389 : /* If the target type is structural equality, the type should also. */
390 67143059 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
391 67143059 : && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (type)))
392 : return false;
393 :
394 140426429 : default:
395 140426429 : break;
396 : }
397 :
398 140426429 : switch (TREE_CODE (type))
399 : {
400 66231273 : case POINTER_TYPE:
401 66231273 : case FUNCTION_TYPE:
402 : /* Pointer and functions can not have variable size. */
403 66231273 : if (C_TYPE_VARIABLE_SIZE (type))
404 0 : return false;
405 : break;
406 911786 : 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 911786 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
410 911786 : || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
411 886032 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
412 : != INTEGER_CST))
413 911786 : != 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 911786 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
418 911786 : || C_TYPE_VARIABLE_SIZE (type))
419 : {
420 28699 : if (!C_TYPE_VARIABLE_SIZE (type))
421 : return false;
422 28699 : 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 133442179 : c_set_type_bits (tree new_type, tree old_type)
436 : {
437 133442179 : gcc_checking_assert (c_verify_type (old_type));
438 :
439 133442179 : if (c_type_variably_modified_p (old_type))
440 28333 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
441 :
442 133442179 : 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 133442179 : return new_type;
448 : }
449 :
450 : /* Build a pointer type using the default pointer mode. */
451 :
452 : tree
453 71520378 : c_build_pointer_type (tree to_type)
454 : {
455 71520378 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
456 71520378 : : TYPE_ADDR_SPACE (to_type);
457 71520378 : machine_mode pointer_mode;
458 :
459 71520378 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
460 71520378 : pointer_mode = targetm.addr_space.pointer_mode (as);
461 : else
462 : pointer_mode = c_default_pointer_mode;
463 :
464 71520378 : 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 71768635 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
471 : {
472 71768635 : tree ret = build_pointer_type_for_mode (type, mode, m);
473 71768635 : return c_set_type_bits (ret, type);
474 : }
475 :
476 : /* Build a function type. */
477 :
478 : tree
479 53289894 : c_build_function_type (tree type, tree args, bool no)
480 : {
481 53289894 : tree ret = build_function_type (type, args, no);
482 53289894 : 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 1171896 : c_build_array_type (tree type, tree domain)
491 : {
492 1171896 : int type_quals = TYPE_QUALS (type);
493 :
494 : /* Identify typeless storage as introduced in C2Y
495 : and supported also in earlier language modes. */
496 1749227 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
497 1171896 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
498 :
499 1171896 : tree ret = build_array_type (type, domain, typeless);
500 :
501 1086185 : if (domain && TYPE_MAX_VALUE (domain)
502 2168788 : && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
503 : {
504 24266 : C_TYPE_VARIABLE_SIZE (ret) = 1;
505 24266 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
506 : }
507 :
508 1171896 : 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 7211754 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
524 : {
525 7211754 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
526 7211754 : return c_set_type_bits (ret, type);
527 : }
528 :
529 : tree
530 7022653 : c_build_type_attribute_variant (tree type, tree attrs)
531 : {
532 7022653 : 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 839886 : c_reconstruct_complex_type (tree type, tree bottom)
540 : {
541 839886 : tree inner, outer;
542 :
543 839886 : if (TREE_CODE (type) == POINTER_TYPE)
544 : {
545 174873 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
546 174873 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
547 174873 : TYPE_REF_CAN_ALIAS_ALL (type));
548 : }
549 : else if (TREE_CODE (type) == ARRAY_TYPE)
550 : {
551 7269 : bool rso = TYPE_REVERSE_STORAGE_ORDER (type);
552 7269 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
553 7269 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
554 7269 : if (rso)
555 : {
556 1 : outer = build_distinct_type_copy (outer);
557 1 : TYPE_REVERSE_STORAGE_ORDER (outer) = 1;
558 : }
559 :
560 7269 : 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 182727 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
576 365454 : 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 3442225 : ptr_to_tagged_member (tree field)
585 : {
586 3442225 : gcc_assert (FIELD_DECL == TREE_CODE (field));
587 3442225 : tree type = TREE_TYPE (field);
588 3442225 : bool ptr_seen = false;
589 3442225 : while (TREE_CODE (type) == ARRAY_TYPE
590 : || TREE_CODE (type) == FUNCTION_TYPE
591 4729206 : || TREE_CODE (type) == POINTER_TYPE)
592 : {
593 1286981 : if (TREE_CODE (type) == POINTER_TYPE)
594 698777 : ptr_seen = true;
595 1286981 : type = TREE_TYPE (type);
596 : }
597 :
598 3442225 : if (ptr_seen
599 677731 : && RECORD_OR_UNION_TYPE_P (type)
600 3691039 : && 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 884451 : c_type_canonical (tree type)
618 : {
619 884451 : gcc_assert (RECORD_OR_UNION_TYPE_P (type));
620 :
621 884451 : bool needs_mod = false;
622 3634239 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
623 : {
624 2831457 : if (!ptr_to_tagged_member (x))
625 2749788 : continue;
626 : needs_mod = true;
627 : break;
628 : }
629 884451 : if (!needs_mod)
630 : return type;
631 :
632 : /* Construct new version with such members replaced. */
633 81669 : tree n = build_distinct_type_copy (type);
634 81669 : tree *fields = &TYPE_FIELDS (n);
635 692437 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
636 : {
637 610768 : tree f = copy_node (x);
638 610768 : if (ptr_to_tagged_member (x))
639 161946 : TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
640 : ptr_type_node);
641 610768 : *fields = f;
642 610768 : fields = &DECL_CHAIN (f);
643 : }
644 81669 : TYPE_CANONICAL (n) = n;
645 81669 : 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 62307640 : 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 62307640 : while (TYPE_NAME (t)
680 32098755 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
681 62355920 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
682 24138 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
683 62307640 : return t;
684 : }
685 :
686 : /* Return the tag for a tagged type. */
687 : tree
688 62307640 : c_type_tag (const_tree t)
689 : {
690 62307640 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
691 62307640 : const_tree orig = c_type_original (t);
692 62307640 : tree name = TYPE_NAME (orig);
693 62307640 : if (!name)
694 : return NULL_TREE;
695 32074617 : 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 32074615 : 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 62317077 : remove_qualifiers (tree t)
709 : {
710 62317077 : if (!t || t == error_mark_node)
711 : return t;
712 62317052 : return TYPE_ATOMIC (strip_array_types (t))
713 62317052 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
714 62301477 : : 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 8206750 : transparent_union_replacement (tree u, tree t)
723 : {
724 8206745 : if (u == error_mark_node || t == error_mark_node
725 8206740 : || TREE_CODE (u) != UNION_TYPE
726 64 : || !(TYPE_TRANSPARENT_AGGR (u) || TYPE_NAME (u) == NULL_TREE)
727 8206814 : || comptypes (u, t))
728 8206686 : 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 13013054 : composite_type_internal (tree t1, tree t2, tree cond,
761 : struct composite_cache* cache)
762 : {
763 13013054 : enum tree_code code1;
764 13013054 : enum tree_code code2;
765 13013054 : tree attributes;
766 :
767 : /* Save time if the two types are the same. */
768 :
769 13013054 : if (t1 == t2) return t1;
770 :
771 : /* If one type is nonsense, use the other. */
772 2631572 : if (t1 == error_mark_node)
773 : return t2;
774 2631568 : if (t2 == error_mark_node)
775 : return t1;
776 :
777 2631566 : code1 = TREE_CODE (t1);
778 2631566 : code2 = TREE_CODE (t2);
779 :
780 : /* Merge the attributes. */
781 2631566 : 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 2631566 : if (code1 == ENUMERAL_TYPE
789 200 : && (code2 == INTEGER_TYPE
790 200 : || code2 == BOOLEAN_TYPE))
791 : return t1;
792 2631366 : if (code2 == ENUMERAL_TYPE
793 69 : && (code1 == INTEGER_TYPE
794 69 : || code1 == BOOLEAN_TYPE))
795 : return t2;
796 :
797 2631297 : gcc_assert (code1 == code2);
798 :
799 2631297 : switch (code1)
800 : {
801 6374 : case POINTER_TYPE:
802 : /* For two pointers, do this recursively on the target type. */
803 6374 : {
804 6374 : gcc_checking_assert (TYPE_QUALS (t1) == TYPE_QUALS (t2));
805 6374 : tree target = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
806 : cond, cache);
807 6374 : tree n = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
808 12748 : return c_build_type_attribute_qual_variant (n, attributes,
809 6374 : 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 2572798 : 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 2572798 : {
1016 2572798 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1017 : cond, cache);
1018 2572798 : tree p1 = TYPE_ARG_TYPES (t1);
1019 2572798 : tree p2 = TYPE_ARG_TYPES (t2);
1020 :
1021 : /* Save space: see if the result is identical to one of the args. */
1022 2572798 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
1023 9843 : return c_build_functype_attribute_variant (t1, t2, attributes);
1024 2563482 : 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 2562965 : 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 2562956 : 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 2562955 : tree newargs = NULL_TREE;
1046 2562955 : tree *endp = &newargs;
1047 :
1048 6666330 : for (; p1; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1049 : {
1050 6499012 : if (p1 == void_list_node)
1051 : {
1052 2395637 : *endp = void_list_node;
1053 2395637 : break;
1054 : }
1055 4103375 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
1056 4103375 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
1057 :
1058 4103375 : gcc_assert (mv1);
1059 4103375 : gcc_assert (mv2);
1060 :
1061 4103375 : mv1 = transparent_union_replacement (mv1, mv2);
1062 4103375 : mv2 = transparent_union_replacement (mv2, mv1);
1063 :
1064 4103375 : *endp = tree_cons (NULL_TREE,
1065 : composite_type_internal (mv1, mv2, cond, cache),
1066 : NULL_TREE);
1067 :
1068 4103375 : endp = &TREE_CHAIN (*endp);
1069 : }
1070 :
1071 2562955 : t1 = c_build_function_type (valtype, newargs);
1072 2562955 : t1 = qualify_type (t1, t2);
1073 : }
1074 : /* FALLTHRU */
1075 :
1076 2603833 : default:
1077 2603833 : return c_build_type_attribute_variant (t1, attributes);
1078 : }
1079 : }
1080 :
1081 : tree
1082 6319322 : composite_type_cond (tree t1, tree t2, tree cond)
1083 : {
1084 6319322 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1085 :
1086 6319322 : struct composite_cache cache = { };
1087 6319322 : tree n = composite_type_internal (t1, t2, cond, &cache);
1088 :
1089 6319322 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1090 6319322 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1091 6319322 : return n;
1092 : }
1093 :
1094 :
1095 : tree
1096 6314852 : composite_type (tree t1, tree t2)
1097 : {
1098 6314852 : 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 114825 : common_pointer_type (tree t1, tree t2, tree cond)
1109 : {
1110 114825 : tree attributes;
1111 114825 : unsigned target_quals;
1112 114825 : addr_space_t as1, as2, as_common;
1113 114825 : int quals1, quals2;
1114 :
1115 : /* Save time if the two types are the same. */
1116 :
1117 114825 : 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 13955475 : c_common_type (tree t1, tree t2)
1174 : {
1175 13955475 : enum tree_code code1;
1176 13955475 : enum tree_code code2;
1177 :
1178 : /* If one type is nonsense, use the other. */
1179 13955475 : if (t1 == error_mark_node)
1180 : return t2;
1181 13955475 : if (t2 == error_mark_node)
1182 : return t1;
1183 :
1184 13955475 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1185 37966 : t1 = TYPE_MAIN_VARIANT (t1);
1186 :
1187 13955475 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1188 49382 : t2 = TYPE_MAIN_VARIANT (t2);
1189 :
1190 13955475 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1191 : {
1192 188346 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1193 188346 : t1 = c_build_type_attribute_variant (t1, attrs);
1194 : }
1195 :
1196 13955475 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1197 : {
1198 187824 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1199 187824 : t2 = c_build_type_attribute_variant (t2, attrs);
1200 : }
1201 :
1202 : /* Save time if the two types are the same. */
1203 :
1204 13955475 : if (t1 == t2) return t1;
1205 :
1206 2325296 : code1 = TREE_CODE (t1);
1207 2325296 : code2 = TREE_CODE (t2);
1208 :
1209 2325296 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1210 : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1211 : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1212 2325296 : 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 2325296 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1220 2327719 : && !(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 2325272 : if (code1 == VECTOR_TYPE)
1244 : return t1;
1245 :
1246 2324983 : 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 2324980 : 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 2235658 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1275 : return t1;
1276 :
1277 2107240 : 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 2075911 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1284 : {
1285 107197 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1286 107197 : || 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 106648 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1291 106648 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1292 : return dfloat64x_type_node;
1293 106644 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1294 106644 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1295 : return dfloat64_type_node;
1296 106199 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1297 106199 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1298 : return dfloat32_type_node;
1299 : }
1300 :
1301 : /* Deal with fixed-point types. */
1302 2074568 : 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 2074568 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1391 : return t1;
1392 1102980 : 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 738535 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1400 738535 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1401 : return long_long_unsigned_type_node;
1402 :
1403 649296 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1404 649296 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1405 : {
1406 15314 : 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 633982 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1413 633982 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1414 : return long_unsigned_type_node;
1415 :
1416 534843 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1417 534843 : || 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 26410 : 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 508433 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1439 508433 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1440 :
1441 2541599 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1442 2033329 : 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 508270 : 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 508028 : if (mv1 == double_type_node || mv2 == double_type_node)
1453 : return double_type_node;
1454 :
1455 507456 : if (mv1 == float_type_node || mv2 == float_type_node)
1456 : return float_type_node;
1457 :
1458 2021148 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1459 1515861 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1460 : return FLOATNX_TYPE_NODE (i);
1461 :
1462 505287 : 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 505279 : 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 102956 : common_type (tree t1, tree t2)
1491 : {
1492 102956 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1493 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1494 102956 : 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 102956 : 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 102372 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1504 : return t2;
1505 102372 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1506 : return t1;
1507 :
1508 102372 : 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 44402423 : comptypes_verify (tree type1, tree type2)
1519 : {
1520 44402423 : if (type1 == type2 || !type1 || !type2
1521 3492126 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1522 : return true;
1523 :
1524 3492125 : gcc_checking_assert (c_verify_type (type1));
1525 3492125 : gcc_checking_assert (c_verify_type (type2));
1526 :
1527 3492125 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1528 385893 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1529 3876226 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1530 : {
1531 : /* FIXME: check other types. */
1532 383737 : if (RECORD_OR_UNION_TYPE_P (type1)
1533 383737 : || TREE_CODE (type1) == ENUMERAL_TYPE
1534 383737 : || 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 18957966 : comptypes_check_for_composite (tree t1, tree t2)
1567 : {
1568 18957966 : struct comptypes_data data = { };
1569 18957966 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1570 18957966 : 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 56347065 : comptypes (tree type1, tree type2)
1588 : {
1589 56347065 : struct comptypes_data data = { };
1590 56347065 : bool ret = comptypes_internal (type1, type2, &data);
1591 :
1592 56347065 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1593 :
1594 37857566 : 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 375 : comptypes_same_p (tree type1, tree type2)
1603 : {
1604 375 : struct comptypes_data data = { };
1605 375 : bool ret = comptypes_internal (type1, type2, &data);
1606 :
1607 375 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1608 :
1609 375 : 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 6744220 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1621 : {
1622 6744220 : struct comptypes_data data = { };
1623 6744220 : bool ret = comptypes_internal (type1, type2, &data);
1624 6744220 : *enum_and_int_p = data.enum_and_int_p;
1625 :
1626 6744220 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1627 :
1628 6498653 : 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 45869 : comptypes_check_different_types (tree type1, tree type2,
1636 : bool *different_types_p)
1637 : {
1638 45869 : struct comptypes_data data = { };
1639 45869 : bool ret = comptypes_internal (type1, type2, &data);
1640 45869 : *different_types_p = data.different_types_p;
1641 :
1642 45869 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1643 :
1644 45869 : 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 14623505 : comptypes_equiv_p (tree type1, tree type2)
1681 : {
1682 14623505 : struct comptypes_data data = { };
1683 14623505 : data.equiv = true;
1684 14623505 : 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 14623505 : gcc_checking_assert (ret || !comptypes (type1, type2));
1689 :
1690 14623505 : 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 132613017 : comptypes_internal (const_tree type1, const_tree type2,
1712 : struct comptypes_data *data)
1713 : {
1714 132895892 : const_tree t1 = type1;
1715 132895892 : const_tree t2 = type2;
1716 :
1717 : /* Suppress errors caused by previously reported errors. */
1718 :
1719 132895892 : if (t1 == t2 || !t1 || !t2
1720 45031562 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1721 : return true;
1722 :
1723 : /* Qualifiers must match. C99 6.7.3p9 */
1724 :
1725 45031559 : 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 44991495 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1733 1030 : && COMPLETE_TYPE_P (t1)
1734 44992481 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1735 : {
1736 870 : t1 = ENUM_UNDERLYING_TYPE (t1);
1737 870 : if (TREE_CODE (t2) != VOID_TYPE)
1738 : {
1739 866 : data->enum_and_int_p = true;
1740 866 : data->different_types_p = true;
1741 : }
1742 : }
1743 44990625 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1744 4733 : && COMPLETE_TYPE_P (t2)
1745 44995352 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1746 : {
1747 4611 : t2 = ENUM_UNDERLYING_TYPE (t2);
1748 4611 : if (TREE_CODE (t1) != VOID_TYPE)
1749 : {
1750 3712 : data->enum_and_int_p = true;
1751 3712 : data->different_types_p = true;
1752 : }
1753 : }
1754 :
1755 44991495 : if (t1 == t2)
1756 : return true;
1757 :
1758 : /* Different classes of types can't be compatible. */
1759 :
1760 44990099 : 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 37020433 : if (TREE_CODE (t1) != ARRAY_TYPE
1768 37020433 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1769 : return true;
1770 :
1771 36685389 : int attrval;
1772 :
1773 : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1774 36685389 : if (!(attrval = comp_type_attributes (t1, t2)))
1775 : return false;
1776 :
1777 36685012 : if (2 == attrval)
1778 336 : data->warning_needed = true;
1779 :
1780 36685012 : switch (TREE_CODE (t1))
1781 : {
1782 3358357 : case INTEGER_TYPE:
1783 3358357 : case FIXED_POINT_TYPE:
1784 3358357 : case REAL_TYPE:
1785 3358357 : 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 3358357 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1801 :
1802 282875 : case POINTER_TYPE:
1803 : /* Do not remove mode information. */
1804 282875 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1805 : return false;
1806 282875 : data->pointedto = true;
1807 282875 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1808 :
1809 8891913 : case FUNCTION_TYPE:
1810 8891913 : return function_types_compatible_p (t1, t2, data);
1811 :
1812 154479 : case ARRAY_TYPE:
1813 154479 : {
1814 154479 : tree d1 = TYPE_DOMAIN (t1);
1815 154479 : tree d2 = TYPE_DOMAIN (t2);
1816 154479 : bool d1_variable, d2_variable;
1817 154479 : bool d1_zero, d2_zero;
1818 :
1819 : /* Target types must match incl. qualifiers. */
1820 154479 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1821 : return false;
1822 :
1823 119492 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1824 6351 : data->different_types_p = true;
1825 : /* Ignore size mismatches when forming equivalence classes. */
1826 119492 : if (data->equiv)
1827 : return true;
1828 : /* Sizes must match unless one is missing or variable. */
1829 50417 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1830 : return true;
1831 :
1832 34142 : d1_zero = !TYPE_MAX_VALUE (d1);
1833 34142 : d2_zero = !TYPE_MAX_VALUE (d2);
1834 :
1835 68284 : d1_variable = (!d1_zero
1836 34142 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1837 34110 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1838 68284 : d2_variable = (!d2_zero
1839 34142 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1840 34014 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1841 :
1842 34142 : if (d1_variable != d2_variable)
1843 943 : data->different_types_p = true;
1844 34142 : if (d1_variable || d2_variable)
1845 : return true;
1846 4048 : if (d1_zero && d2_zero)
1847 : return true;
1848 4048 : if (d1_zero || d2_zero
1849 3952 : || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1850 8000 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1851 4048 : return false;
1852 :
1853 : return true;
1854 : }
1855 :
1856 23459637 : case ENUMERAL_TYPE:
1857 23459637 : case RECORD_TYPE:
1858 23459637 : case UNION_TYPE:
1859 :
1860 23459637 : if (!flag_isoc23)
1861 : return false;
1862 :
1863 23459590 : return tagged_types_tu_compatible_p (t1, t2, data);
1864 :
1865 536946 : case VECTOR_TYPE:
1866 1073892 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1867 536946 : && 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 1764264 : comp_target_types (location_t location, tree ttl, tree ttr)
1882 : {
1883 1764264 : int val;
1884 1764264 : int val_ped;
1885 1764264 : tree mvl = TREE_TYPE (ttl);
1886 1764264 : tree mvr = TREE_TYPE (ttr);
1887 1764264 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1888 1764264 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1889 1764264 : addr_space_t as_common;
1890 1764264 : bool enum_and_int_p;
1891 :
1892 : /* Fail if pointers point to incompatible address spaces. */
1893 1764264 : 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 1764264 : val_ped = 1;
1899 :
1900 1764264 : 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 1764264 : mvl = remove_qualifiers (mvl);
1907 1764264 : mvr = remove_qualifiers (mvr);
1908 :
1909 1764264 : enum_and_int_p = false;
1910 1764264 : val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1911 :
1912 1764264 : 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 1764264 : if (val == 2)
1917 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1918 :
1919 1764264 : 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 1764264 : 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 23459590 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1934 : struct comptypes_data *data)
1935 : {
1936 23459590 : tree s1, s2;
1937 :
1938 23459590 : 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 19389299 : 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 19389237 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1950 : return false;
1951 :
1952 14256428 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1953 14255383 : data->different_types_p = true;
1954 :
1955 : /* Incomplete types are incompatible inside a TU. */
1956 14256428 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1957 : return false;
1958 :
1959 14256418 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1960 14256418 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1961 14256363 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1962 : return false;
1963 :
1964 14256408 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1965 144698 : 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 14256408 : for (const struct tagged_tu_seen_cache *t = data->cache;
1972 14266085 : t != NULL; t = t->next)
1973 9972 : if (t->t1 == t1 && t->t2 == t2)
1974 : return true;
1975 :
1976 14256113 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1977 :
1978 14256113 : 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 14256058 : case UNION_TYPE:
2025 14256058 : case RECORD_TYPE:
2026 :
2027 14256058 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2028 : return false;
2029 :
2030 12322940 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2031 13846613 : s1 && s2;
2032 1523673 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2033 : {
2034 13781148 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2035 13781148 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2036 :
2037 13781148 : tree ft1 = TREE_TYPE (s1);
2038 13781148 : tree ft2 = TREE_TYPE (s2);
2039 :
2040 13781148 : if (DECL_NAME (s1) != DECL_NAME (s2))
2041 : return false;
2042 :
2043 12135025 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2044 : return false;
2045 :
2046 3233694 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2047 : return false;
2048 :
2049 3233674 : 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 3233520 : if (!ft1 || !ft2)
2059 : return false;
2060 :
2061 3233520 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2062 : return false;
2063 :
2064 3233519 : data->anon_field = !DECL_NAME (s1);
2065 3233519 : data->pointedto = false;
2066 :
2067 3233519 : const struct tagged_tu_seen_cache *cache = data->cache;
2068 3233519 : data->cache = &entry;
2069 3233519 : bool ret = comptypes_internal (ft1, ft2, data);
2070 3233519 : data->cache = cache;
2071 3233519 : if (!ret)
2072 : return false;
2073 :
2074 1592636 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2075 1592636 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2076 :
2077 1592636 : if (data->equiv
2078 998352 : && st1 && TREE_CODE (st1) == INTEGER_CST
2079 997870 : && st2 && TREE_CODE (st2) == INTEGER_CST
2080 2590500 : && !tree_int_cst_equal (st1, st2))
2081 : return false;
2082 :
2083 1523707 : tree counted_by1 = lookup_attribute ("counted_by",
2084 1523707 : DECL_ATTRIBUTES (s1));
2085 1523707 : tree counted_by2 = lookup_attribute ("counted_by",
2086 1523707 : DECL_ATTRIBUTES (s2));
2087 : /* If there is no counted_by attribute for both fields. */
2088 1523707 : if (!counted_by1 && !counted_by2)
2089 1523643 : 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 8891913 : function_types_compatible_p (const_tree f1, const_tree f2,
2131 : struct comptypes_data *data)
2132 : {
2133 8891913 : tree ret1 = TREE_TYPE (f1);
2134 8891913 : tree ret2 = TREE_TYPE (f2);
2135 :
2136 : /* 'volatile' qualifiers on a function's return type used to mean
2137 : the function is noreturn. */
2138 8891913 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2139 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2140 8891913 : if (TYPE_VOLATILE (ret1))
2141 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2142 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2143 8891913 : if (TYPE_VOLATILE (ret2))
2144 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2145 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2146 :
2147 8891913 : bool ignore_pargs = data->ignore_promoting_args;
2148 8891913 : data->ignore_promoting_args = false;
2149 :
2150 8891913 : if (!comptypes_internal (ret1, ret2, data))
2151 : return false;
2152 :
2153 8889318 : data->ignore_promoting_args = ignore_pargs;
2154 :
2155 8889318 : tree args1 = TYPE_ARG_TYPES (f1);
2156 8889318 : tree args2 = TYPE_ARG_TYPES (f2);
2157 :
2158 8889318 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2159 41521 : 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 8889318 : 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 8867413 : if (args2 == NULL_TREE)
2180 : {
2181 19698 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2182 : return false;
2183 19696 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2184 : return false;
2185 19635 : data->ignore_promoting_args = false;
2186 19635 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2187 19635 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2188 1 : data->warning_needed = true;
2189 19635 : return true;
2190 : }
2191 :
2192 : /* Both types have argument lists: compare them and propagate results. */
2193 8847715 : 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 8847791 : type_lists_compatible_p (const_tree args1, const_tree args2,
2201 : struct comptypes_data *data)
2202 : {
2203 55104139 : while (1)
2204 : {
2205 31975965 : 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 23272613 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2210 : return false;
2211 23272606 : tree a1 = TREE_VALUE (args1);
2212 23272606 : tree a2 = TREE_VALUE (args2);
2213 23272606 : tree mv1 = remove_qualifiers (a1);
2214 23272606 : tree mv2 = remove_qualifiers (a2);
2215 :
2216 23272606 : gcc_assert (mv2);
2217 23272606 : gcc_assert (mv2);
2218 :
2219 : /* If one of the lists has an error marker, ignore this arg. */
2220 23272606 : if (TREE_CODE (a1) == ERROR_MARK
2221 23272602 : || TREE_CODE (a2) == ERROR_MARK)
2222 : ;
2223 23272586 : else if (!comptypes_internal (mv1, mv2, data))
2224 : {
2225 144663 : data->different_types_p = true;
2226 : /* Allow wait (union {union wait *u; int *i} *)
2227 : and wait (union wait *) to be compatible. */
2228 144663 : 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 144793 : && 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 144533 : 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 144638 : && 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 144428 : return false;
2266 : }
2267 :
2268 23128174 : args1 = TREE_CHAIN (args1);
2269 23128174 : args2 = TREE_CHAIN (args2);
2270 23128174 : }
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 233744 : c_size_in_bytes (const_tree type)
2280 : {
2281 233744 : enum tree_code code = TREE_CODE (type);
2282 :
2283 233744 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2284 233744 : || !COMPLETE_TYPE_P (type))
2285 191 : return size_one_node;
2286 :
2287 : /* Convert in case a char is more than one unit. */
2288 233553 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2289 233553 : 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 14337257 : decl_constant_value_1 (tree decl, bool in_init)
2297 : {
2298 14337257 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2299 14337257 : TREE_CODE (decl) != PARM_DECL
2300 14337257 : && !TREE_THIS_VOLATILE (decl)
2301 13992946 : && TREE_READONLY (decl)
2302 36753 : && DECL_INITIAL (decl) != NULL_TREE
2303 35757 : && !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 35748 : && TREE_CONSTANT (DECL_INITIAL (decl))
2308 : /* Check for cases where this is sub-optimal, even though valid. */
2309 14359945 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2310 17572 : 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 14337009 : 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 14337009 : 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 820853 : array_to_pointer_conversion (location_t loc, tree exp)
2328 : {
2329 820853 : tree orig_exp = exp;
2330 820853 : tree type = TREE_TYPE (exp);
2331 820853 : tree adr;
2332 820853 : tree restype = TREE_TYPE (type);
2333 820853 : tree ptrtype;
2334 :
2335 820853 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2336 :
2337 820853 : STRIP_TYPE_NOPS (exp);
2338 :
2339 820853 : copy_warning (exp, orig_exp);
2340 :
2341 820853 : ptrtype = c_build_pointer_type (restype);
2342 :
2343 820853 : 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 817947 : 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 817947 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2359 817947 : return convert (ptrtype, adr);
2360 : }
2361 :
2362 : /* Convert the function expression EXP to a pointer. */
2363 : static tree
2364 50592976 : function_to_pointer_conversion (location_t loc, tree exp)
2365 : {
2366 50592976 : tree orig_exp = exp;
2367 :
2368 50592976 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2369 :
2370 50592976 : STRIP_TYPE_NOPS (exp);
2371 :
2372 50592976 : copy_warning (exp, orig_exp);
2373 :
2374 50592976 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2375 :
2376 : /* If the function is defined and known to not require a non-local
2377 : context, make sure no trampoline is generated. */
2378 50592976 : if (TREE_CODE (exp) == FUNCTION_DECL
2379 50592976 : && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))
2380 16066869 : TREE_NO_TRAMPOLINE (exp2) = 1;
2381 :
2382 50592976 : 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 489928713 : mark_exp_read (tree exp)
2390 : {
2391 616300512 : switch (TREE_CODE (exp))
2392 : {
2393 203575494 : case VAR_DECL:
2394 203575494 : case PARM_DECL:
2395 203575494 : DECL_READ_P (exp) = 1;
2396 203575494 : break;
2397 19714608 : CASE_CONVERT:
2398 19714608 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2399 3805 : 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 123626403 : case ARRAY_REF:
2411 123626403 : case COMPONENT_REF:
2412 123626403 : case MODIFY_EXPR:
2413 123626403 : case REALPART_EXPR:
2414 123626403 : case IMAGPART_EXPR:
2415 123626403 : case ADDR_EXPR:
2416 123626403 : case VIEW_CONVERT_EXPR:
2417 123626403 : case PREINCREMENT_EXPR:
2418 123626403 : case PREDECREMENT_EXPR:
2419 123626403 : case POSTINCREMENT_EXPR:
2420 123626403 : case POSTDECREMENT_EXPR:
2421 123626403 : mark_exp_read (TREE_OPERAND (exp, 0));
2422 123626403 : break;
2423 236453 : case COMPOUND_EXPR:
2424 : /* Pattern match what build_atomic_assign produces with modifycode
2425 : NOP_EXPR. */
2426 236453 : if (VAR_P (TREE_OPERAND (exp, 1))
2427 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2428 263596 : && 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 2744853 : case C_MAYBE_CONST_EXPR:
2469 2744853 : mark_exp_read (TREE_OPERAND (exp, 1));
2470 2744853 : 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 362158305 : default_function_array_conversion (location_t loc, struct c_expr exp)
2491 : {
2492 362158305 : tree orig_exp = exp.value;
2493 362158305 : tree type = TREE_TYPE (exp.value);
2494 362158305 : enum tree_code code = TREE_CODE (type);
2495 :
2496 362158305 : switch (code)
2497 : {
2498 : case ARRAY_TYPE:
2499 : {
2500 : bool not_lvalue = false;
2501 : bool lvalue_array_p;
2502 :
2503 773954 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2504 773954 : || CONVERT_EXPR_P (exp.value))
2505 773954 : && 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 773954 : copy_warning (exp.value, orig_exp);
2513 :
2514 773954 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2515 773954 : 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 773845 : exp.value = array_to_pointer_conversion (loc, exp.value);
2525 : }
2526 773845 : break;
2527 762070 : case FUNCTION_TYPE:
2528 762070 : exp.value = function_to_pointer_conversion (loc, exp.value);
2529 762070 : break;
2530 : default:
2531 : break;
2532 : }
2533 :
2534 362158196 : return exp;
2535 : }
2536 :
2537 : struct c_expr
2538 947736 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2539 : {
2540 947736 : mark_exp_read (exp.value);
2541 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2542 947736 : if (TREE_CODE (exp.value) == COMPONENT_REF
2543 947736 : && handle_counted_by_p (exp.value))
2544 66628 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2545 947736 : 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 363253179 : really_atomic_lvalue (tree expr)
2553 : {
2554 363253179 : if (error_operand_p (expr))
2555 : return false;
2556 363245653 : 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 360063701 : maybe_get_constexpr_init (tree expr)
2593 : {
2594 360063701 : tree decl = NULL_TREE;
2595 360063701 : if (TREE_CODE (expr) == VAR_DECL)
2596 : decl = expr;
2597 348351001 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2598 822113 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2599 822113 : if (decl
2600 12534813 : && C_DECL_DECLARED_CONSTEXPR (decl)
2601 370 : && DECL_INITIAL (decl) != NULL_TREE
2602 822483 : && !error_operand_p (DECL_INITIAL (decl)))
2603 370 : return DECL_INITIAL (decl);
2604 360063331 : if (TREE_CODE (expr) != COMPONENT_REF)
2605 : return NULL_TREE;
2606 871955 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2607 871955 : 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 359379437 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2658 : bool convert_p, bool read_p, bool for_init)
2659 : {
2660 359379437 : bool force_non_npc = false;
2661 359379437 : if (read_p)
2662 315345898 : mark_exp_read (exp.value);
2663 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2664 315345898 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2665 780876 : && handle_counted_by_p (exp.value))
2666 780847 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2667 :
2668 359379437 : if (convert_p)
2669 359366207 : exp = default_function_array_conversion (loc, exp);
2670 359379437 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2671 359379437 : || (flag_isoc2y
2672 1569 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2673 356898781 : exp.value = require_complete_type (loc, exp.value);
2674 359379437 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2675 : {
2676 359191746 : tree init = maybe_get_constexpr_init (exp.value);
2677 359191746 : 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 359379437 : 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 359366207 : if (convert_p && !error_operand_p (exp.value)
2746 718738162 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2747 359358616 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2748 359379437 : if (force_non_npc)
2749 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2750 :
2751 359379437 : {
2752 359379437 : tree false_value, true_value;
2753 359366207 : if (convert_p && !error_operand_p (exp.value)
2754 718738162 : && 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 359379437 : 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 575423 : convert_lvalue_to_rvalue (location_t loc, tree val,
2787 : bool convert_p, bool read_p, bool for_init = false)
2788 : {
2789 575423 : struct c_expr expr;
2790 575423 : memset (&expr, 0, sizeof (expr));
2791 575423 : expr.value = val;
2792 575423 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2793 575423 : 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 76805009 : perform_integral_promotions (tree exp)
2801 : {
2802 76805009 : tree type = TREE_TYPE (exp);
2803 76805009 : enum tree_code code = TREE_CODE (type);
2804 :
2805 76805009 : gcc_assert (INTEGRAL_TYPE_P (type));
2806 :
2807 : /* Convert enums to the result of applying the integer promotions to
2808 : their underlying type. */
2809 76805009 : if (code == ENUMERAL_TYPE)
2810 : {
2811 650347 : type = ENUM_UNDERLYING_TYPE (type);
2812 650347 : 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 650347 : return convert (type, exp);
2822 : }
2823 :
2824 : /* ??? This should no longer be needed now bit-fields have their
2825 : proper types. */
2826 76154662 : if (TREE_CODE (exp) == COMPONENT_REF
2827 76154662 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2828 : {
2829 62183 : 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 61729 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2835 61729 : TYPE_PRECISION (integer_type_node)) < 0)
2836 53467 : return convert (integer_type_node, exp);
2837 : }
2838 :
2839 76100741 : if (c_promoting_integer_type_p (type))
2840 : {
2841 : /* Preserve unsignedness if not really getting any wider. */
2842 644261 : if (TYPE_UNSIGNED (type)
2843 644261 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2844 0 : return convert (unsigned_type_node, exp);
2845 :
2846 644261 : 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 86331972 : default_conversion (tree exp)
2859 : {
2860 86331972 : tree orig_exp;
2861 86331972 : tree type = TREE_TYPE (exp);
2862 86331972 : enum tree_code code = TREE_CODE (type);
2863 86331972 : tree promoted_type;
2864 :
2865 86331972 : mark_exp_read (exp);
2866 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2867 86331972 : if (TREE_CODE (exp) == COMPONENT_REF
2868 86331972 : && handle_counted_by_p (exp))
2869 598144 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2870 :
2871 : /* Functions and arrays have been converted during parsing. */
2872 86331972 : gcc_assert (code != FUNCTION_TYPE);
2873 86331972 : if (code == ARRAY_TYPE)
2874 : return exp;
2875 :
2876 : /* Constants can be used directly unless they're not loadable. */
2877 86331813 : if (TREE_CODE (exp) == CONST_DECL)
2878 0 : exp = DECL_INITIAL (exp);
2879 :
2880 : /* Strip no-op conversions. */
2881 86331813 : orig_exp = exp;
2882 86703347 : STRIP_TYPE_NOPS (exp);
2883 :
2884 86331813 : copy_warning (exp, orig_exp);
2885 :
2886 86331813 : 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 86331811 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2894 86331811 : if (exp == error_mark_node)
2895 : return error_mark_node;
2896 :
2897 86330907 : promoted_type = targetm.promoted_type (type);
2898 86330907 : if (promoted_type)
2899 0 : return convert (promoted_type, exp);
2900 :
2901 86330907 : if (INTEGRAL_TYPE_P (type))
2902 75807831 : 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 2314497 : lookup_field (const_tree type, tree component)
2918 : {
2919 2314497 : 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 2614497 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2931 2614497 : && !seen_error ())
2932 : {
2933 299979 : int bot, top, half;
2934 299979 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2935 :
2936 299979 : field = TYPE_FIELDS (type);
2937 299979 : bot = 0;
2938 299979 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2939 1378743 : while (top - bot > 1)
2940 : {
2941 1350106 : half = (top - bot + 1) >> 1;
2942 1350106 : field = field_array[bot+half];
2943 :
2944 1350106 : 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 1350106 : if (DECL_NAME (field) == component)
2979 : break;
2980 1078764 : if (DECL_NAME (field) < component)
2981 : bot += half;
2982 : else
2983 892973 : top = bot + half;
2984 : }
2985 :
2986 299979 : if (DECL_NAME (field_array[bot]) == component)
2987 : field = field_array[bot];
2988 271342 : else if (DECL_NAME (field) != component)
2989 : return NULL_TREE;
2990 : }
2991 : else
2992 : {
2993 5245820 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2994 : {
2995 5236527 : if (DECL_NAME (field) == NULL_TREE
2996 5236527 : && 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 5233525 : if (DECL_NAME (field) == component)
3015 : break;
3016 : }
3017 :
3018 2011520 : if (field == NULL_TREE)
3019 : return NULL_TREE;
3020 : }
3021 :
3022 2302206 : 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 2270573 : handle_counted_by_p (tree ref)
3094 : {
3095 2270573 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3096 2270573 : 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 2270573 : if (TREE_CODE (datum) == INDIRECT_REF
3101 2270573 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3102 : return false;
3103 2270243 : 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 2270501 : check_counted_by_attribute (location_t loc, tree ref)
3114 : {
3115 2270501 : tree subdatum = TREE_OPERAND (ref, 1);
3116 2270501 : tree sub_type = TREE_TYPE (subdatum);
3117 :
3118 2270501 : if (!c_flexible_array_member_type_p (sub_type)
3119 2270501 : && TREE_CODE (sub_type) != POINTER_TYPE)
3120 : return;
3121 :
3122 386775 : tree element_type = TREE_TYPE (sub_type);
3123 :
3124 386775 : tree attr_counted_by = lookup_attribute ("counted_by",
3125 386775 : DECL_ATTRIBUTES (subdatum));
3126 386775 : if (attr_counted_by)
3127 : {
3128 : /* Issue error when the element_type is a structure or
3129 : union including a flexible array member. */
3130 873 : if (RECORD_OR_UNION_TYPE_P (element_type)
3131 873 : && 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 499791 : build_counted_by_ref (tree datum, tree subdatum,
3163 : tree *counted_by_type)
3164 : {
3165 499791 : tree sub_type = TREE_TYPE (subdatum);
3166 499791 : if (!c_flexible_array_member_type_p (sub_type)
3167 499791 : && TREE_CODE (sub_type) != POINTER_TYPE)
3168 : return NULL_TREE;
3169 :
3170 499791 : tree attr_counted_by = lookup_attribute ("counted_by",
3171 499791 : DECL_ATTRIBUTES (subdatum));
3172 499791 : if (!attr_counted_by)
3173 : return NULL_TREE;
3174 :
3175 653 : tree counted_by_ref = NULL_TREE;
3176 653 : *counted_by_type = NULL_TREE;
3177 :
3178 653 : 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 653 : while (TREE_CODE (datum) == COMPONENT_REF
3184 35 : && c_type_tag (type) == NULL_TREE
3185 723 : && 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 653 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3192 653 : tree counted_by_field = lookup_field (type, field_id);
3193 653 : gcc_assert (counted_by_field);
3194 :
3195 1065 : tree counted_by_subdatum;
3196 1065 : do
3197 : {
3198 1065 : counted_by_subdatum = TREE_VALUE (counted_by_field);
3199 : /* Get the TYPE of the counted_by field. */
3200 1065 : *counted_by_type = TREE_TYPE (counted_by_subdatum);
3201 :
3202 1065 : counted_by_ref
3203 1065 : = build3 (COMPONENT_REF, TREE_TYPE (counted_by_subdatum),
3204 : datum, counted_by_subdatum, NULL_TREE);
3205 :
3206 1065 : datum = counted_by_ref;
3207 1065 : counted_by_field = TREE_CHAIN (counted_by_field);
3208 : }
3209 1065 : while (counted_by_field);
3210 :
3211 653 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3212 653 : 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 653 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3258 : tree counted_by_ref,
3259 : tree counted_by_type)
3260 : {
3261 653 : gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))
3262 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3263 :
3264 653 : 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 653 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3269 452 : : TREE_TYPE (ref);
3270 :
3271 653 : tree element_type = TREE_TYPE (TREE_TYPE (ref));
3272 653 : tree element_size = VOID_TYPE_P (element_type)
3273 653 : ? size_one_node
3274 653 : : TYPE_SIZE_UNIT (element_type);
3275 653 : if (element_size == NULL_TREE)
3276 : return ref;
3277 :
3278 639 : tree first_param = is_fam
3279 639 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3280 : false, NULL)
3281 438 : : c_fully_fold (ref, false, NULL);
3282 639 : tree second_param
3283 639 : = c_fully_fold (counted_by_ref, false, NULL);
3284 639 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3285 :
3286 639 : tree call
3287 639 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3288 : result_type, 4,
3289 : first_param,
3290 : second_param,
3291 : third_param,
3292 : element_size);
3293 :
3294 : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3295 639 : if (is_fam)
3296 201 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3297 639 : SET_EXPR_LOCATION (call, loc);
3298 639 : return call;
3299 : }
3300 :
3301 : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3302 : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3303 : function .ACCESS_WITH_SIZE.
3304 : Otherwise, return the ref itself. */
3305 :
3306 : tree
3307 2261537 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3308 : {
3309 2261537 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3310 2261537 : tree datum = TREE_OPERAND (ref, 0);
3311 2261537 : tree subdatum = TREE_OPERAND (ref, 1);
3312 2261537 : tree counted_by_type = NULL_TREE;
3313 :
3314 2261537 : if (!(c_flexible_array_member_type_p (TREE_TYPE (ref))
3315 2191079 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3316 : return ref;
3317 :
3318 499791 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3319 : &counted_by_type);
3320 499791 : if (counted_by_ref)
3321 653 : ref = build_access_with_size_for_counted_by (loc, ref,
3322 : counted_by_ref,
3323 : counted_by_type);
3324 : return ref;
3325 : }
3326 :
3327 : /* Make an expression to refer to the COMPONENT field of structure or
3328 : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3329 : location of the COMPONENT_REF. COMPONENT_LOC is the location
3330 : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3331 : it is from -> operator.
3332 : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3333 : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3334 :
3335 : tree
3336 2268391 : build_component_ref (location_t loc, tree datum, tree component,
3337 : location_t component_loc, location_t arrow_loc)
3338 : {
3339 2268391 : tree type = TREE_TYPE (datum);
3340 2268391 : enum tree_code code = TREE_CODE (type);
3341 2268391 : tree field = NULL;
3342 2268391 : tree ref;
3343 2268391 : bool datum_lvalue = lvalue_p (datum);
3344 :
3345 2268391 : if (!objc_is_public (datum, component))
3346 0 : return error_mark_node;
3347 :
3348 : /* Detect Objective-C property syntax object.property. */
3349 2268391 : if (c_dialect_objc ()
3350 2268391 : && (ref = objc_maybe_build_component_ref (datum, component)))
3351 : return ref;
3352 :
3353 : /* See if there is a field or component with name COMPONENT. */
3354 :
3355 2268391 : if (code == RECORD_TYPE || code == UNION_TYPE)
3356 : {
3357 2268319 : if (!COMPLETE_TYPE_P (type))
3358 : {
3359 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3360 19 : return error_mark_node;
3361 : }
3362 :
3363 2268300 : field = lookup_field (type, component);
3364 :
3365 2268300 : if (!field)
3366 : {
3367 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3368 63 : if (guessed_id)
3369 : {
3370 : /* Attempt to provide a fixit replacement hint, if
3371 : we have a valid range for the component. */
3372 0 : location_t reported_loc
3373 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3374 26 : gcc_rich_location rich_loc (reported_loc);
3375 26 : if (component_loc != UNKNOWN_LOCATION)
3376 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3377 26 : error_at (&rich_loc,
3378 : "%qT has no member named %qE; did you mean %qE?",
3379 : type, component, guessed_id);
3380 26 : }
3381 : else
3382 37 : error_at (loc, "%qT has no member named %qE", type, component);
3383 63 : return error_mark_node;
3384 : }
3385 :
3386 : /* Accessing elements of atomic structures or unions is undefined
3387 : behavior (C11 6.5.2.3#5). */
3388 2268237 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3389 : {
3390 18 : if (code == RECORD_TYPE)
3391 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3392 : "structure %qE", component, datum);
3393 : else
3394 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3395 : "union %qE", component, datum);
3396 : }
3397 :
3398 : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3399 : This might be better solved in future the way the C++ front
3400 : end does it - by giving the anonymous entities each a
3401 : separate name and type, and then have build_component_ref
3402 : recursively call itself. We can't do that here. */
3403 2270501 : do
3404 : {
3405 2270501 : tree subdatum = TREE_VALUE (field);
3406 2270501 : int quals;
3407 2270501 : tree subtype;
3408 2270501 : bool use_datum_quals;
3409 :
3410 2270501 : if (TREE_TYPE (subdatum) == error_mark_node)
3411 : return error_mark_node;
3412 :
3413 : /* If this is an rvalue, it does not have qualifiers in C
3414 : standard terms and we must avoid propagating such
3415 : qualifiers down to a non-lvalue array that is then
3416 : converted to a pointer. */
3417 4541002 : use_datum_quals = (datum_lvalue
3418 2270501 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3419 :
3420 2270501 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3421 2270501 : if (use_datum_quals)
3422 2270239 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3423 2270501 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3424 :
3425 2270501 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3426 : NULL_TREE);
3427 2270501 : SET_EXPR_LOCATION (ref, loc);
3428 :
3429 2270501 : check_counted_by_attribute (loc, ref);
3430 :
3431 2270501 : if (TREE_READONLY (subdatum)
3432 2270501 : || (use_datum_quals && TREE_READONLY (datum)))
3433 30511 : TREE_READONLY (ref) = 1;
3434 2270501 : if (TREE_THIS_VOLATILE (subdatum)
3435 2269507 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3436 2428 : TREE_THIS_VOLATILE (ref) = 1;
3437 :
3438 2270501 : if (TREE_UNAVAILABLE (subdatum))
3439 10 : error_unavailable_use (subdatum, NULL_TREE);
3440 2270491 : else if (TREE_DEPRECATED (subdatum))
3441 16 : warn_deprecated_use (subdatum, NULL_TREE);
3442 :
3443 2270501 : datum = ref;
3444 :
3445 2270501 : field = TREE_CHAIN (field);
3446 : }
3447 2270501 : while (field);
3448 :
3449 : return ref;
3450 : }
3451 72 : else if (should_suggest_deref_p (type))
3452 : {
3453 : /* Special-case the error message for "ptr.field" for the case
3454 : where the user has confused "." vs "->". */
3455 7 : rich_location richloc (line_table, loc);
3456 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3457 : {
3458 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3459 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3460 2 : error_at (&richloc,
3461 : "%qE is a pointer to pointer; did you mean to dereference "
3462 : "it before applying %<->%> to it?",
3463 2 : TREE_OPERAND (datum, 0));
3464 : }
3465 : else
3466 : {
3467 : /* "loc" should be the "." token. */
3468 5 : richloc.add_fixit_replace ("->");
3469 5 : error_at (&richloc,
3470 : "%qE is a pointer; did you mean to use %<->%>?",
3471 : datum);
3472 : }
3473 7 : return error_mark_node;
3474 7 : }
3475 65 : else if (code != ERROR_MARK)
3476 1 : error_at (loc,
3477 : "request for member %qE in something not a structure or union",
3478 : component);
3479 :
3480 65 : return error_mark_node;
3481 : }
3482 :
3483 : /* Given an expression PTR for a pointer, return an expression
3484 : for the value pointed to.
3485 : ERRORSTRING is the name of the operator to appear in error messages.
3486 :
3487 : LOC is the location to use for the generated tree. */
3488 :
3489 : tree
3490 2748157 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3491 : {
3492 2748157 : tree pointer = default_conversion (ptr);
3493 2748157 : tree type = TREE_TYPE (pointer);
3494 2748157 : tree ref;
3495 :
3496 2748157 : if (TREE_CODE (type) == POINTER_TYPE)
3497 : {
3498 2747803 : if (CONVERT_EXPR_P (pointer)
3499 1967369 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3500 : {
3501 : /* If a warning is issued, mark it to avoid duplicates from
3502 : the backend. This only needs to be done at
3503 : warn_strict_aliasing > 2. */
3504 780434 : if (warn_strict_aliasing > 2)
3505 334865 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3506 334865 : type, TREE_OPERAND (pointer, 0)))
3507 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3508 : }
3509 :
3510 2747803 : if (TREE_CODE (pointer) == ADDR_EXPR
3511 2747803 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3512 67439 : == TREE_TYPE (type)))
3513 : {
3514 67430 : ref = TREE_OPERAND (pointer, 0);
3515 67430 : protected_set_expr_location (ref, loc);
3516 67430 : return ref;
3517 : }
3518 : else
3519 : {
3520 2680373 : tree t = TREE_TYPE (type);
3521 :
3522 2680373 : ref = build1 (INDIRECT_REF, t, pointer);
3523 :
3524 2680373 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3525 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3526 :
3527 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3528 : so that we get the proper error message if the result is used
3529 : to assign to. Also, &* is supposed to be a no-op.
3530 : And ANSI C seems to specify that the type of the result
3531 : should be the const type. */
3532 : /* A de-reference of a pointer to const is not a const. It is valid
3533 : to change it via some other pointer. */
3534 2680373 : TREE_READONLY (ref) = TYPE_READONLY (t);
3535 2680373 : TREE_SIDE_EFFECTS (ref)
3536 2680373 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3537 2680373 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3538 2680373 : protected_set_expr_location (ref, loc);
3539 2680373 : return ref;
3540 : }
3541 : }
3542 354 : else if (TREE_CODE (pointer) != ERROR_MARK)
3543 278 : invalid_indirection_error (loc, type, errstring);
3544 :
3545 354 : return error_mark_node;
3546 : }
3547 :
3548 : /* This handles expressions of the form "a[i]", which denotes
3549 : an array reference.
3550 :
3551 : This is logically equivalent in C to *(a+i), but we may do it differently.
3552 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3553 : This avoids forcing the array out of registers, and can work on
3554 : arrays that are not lvalues (for example, members of structures returned
3555 : by functions).
3556 :
3557 : For vector types, allow vector[i] but not i[vector], and create
3558 : *(((type*)&vectortype) + i) for the expression.
3559 :
3560 : LOC is the location to use for the returned expression. */
3561 :
3562 : tree
3563 3469525 : build_array_ref (location_t loc, tree array, tree index)
3564 : {
3565 3469525 : tree ret;
3566 3469525 : bool swapped = false;
3567 3469525 : if (TREE_TYPE (array) == error_mark_node
3568 3469525 : || TREE_TYPE (index) == error_mark_node)
3569 : return error_mark_node;
3570 :
3571 3469417 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3572 1995380 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3573 : /* Allow vector[index] but not index[vector]. */
3574 4492121 : && !gnu_vector_type_p (TREE_TYPE (array)))
3575 : {
3576 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3577 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3578 : {
3579 2 : error_at (loc,
3580 : "subscripted value is neither array nor pointer nor vector");
3581 :
3582 2 : return error_mark_node;
3583 : }
3584 192 : std::swap (array, index);
3585 192 : swapped = true;
3586 : }
3587 :
3588 3469415 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3589 : {
3590 2 : error_at (loc, "array subscript is not an integer");
3591 2 : return error_mark_node;
3592 : }
3593 :
3594 3469413 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3595 : {
3596 10 : error_at (loc, "subscripted value is pointer to function");
3597 10 : return error_mark_node;
3598 : }
3599 :
3600 : /* ??? Existing practice has been to warn only when the char
3601 : index is syntactically the index, not for char[array]. */
3602 3469403 : if (!swapped)
3603 3469217 : warn_array_subscript_with_type_char (loc, index);
3604 :
3605 : /* Apply default promotions *after* noticing character types. */
3606 3469403 : index = default_conversion (index);
3607 3469403 : if (index == error_mark_node)
3608 : return error_mark_node;
3609 :
3610 3469402 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3611 : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3612 :
3613 3469402 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3614 3469402 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3615 :
3616 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3617 3469402 : if (TREE_CODE (array) == COMPONENT_REF
3618 3469402 : && handle_counted_by_p (array))
3619 815838 : array = handle_counted_by_for_component_ref (loc, array);
3620 :
3621 3469402 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3622 : {
3623 2496554 : tree rval, type;
3624 :
3625 : /* An array that is indexed by a non-constant
3626 : cannot be stored in a register; we must be able to do
3627 : address arithmetic on its address.
3628 : Likewise an array of elements of variable size. */
3629 2496554 : if (TREE_CODE (index) != INTEGER_CST
3630 2496554 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3631 2023056 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3632 : {
3633 474238 : if (!c_mark_addressable (array, true, true))
3634 1 : return error_mark_node;
3635 : }
3636 : /* An array that is indexed by a constant value which is not within
3637 : the array bounds cannot be stored in a register either; because we
3638 : would get a crash in store_bit_field/extract_bit_field when trying
3639 : to access a non-existent part of the register. */
3640 2496553 : if (TREE_CODE (index) == INTEGER_CST
3641 2023056 : && TYPE_DOMAIN (TREE_TYPE (array))
3642 4516250 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3643 : {
3644 60650 : if (!c_mark_addressable (array, false, true))
3645 0 : return error_mark_node;
3646 : /* ISO C2Y disallows a negative integer constant expression index
3647 : when array subscripting has an operand of array type. */
3648 60650 : if (flag_isoc2y
3649 26 : && !TREE_OVERFLOW (index)
3650 60673 : && tree_int_cst_sgn (index) < 0)
3651 10 : error_at (loc, "array subscript is negative");
3652 : }
3653 :
3654 2496553 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3655 2473204 : && ! was_vector)
3656 : {
3657 1473323 : tree foo = array;
3658 2308352 : while (TREE_CODE (foo) == COMPONENT_REF)
3659 835029 : foo = TREE_OPERAND (foo, 0);
3660 974155 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3661 2447412 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3662 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3663 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3664 : "ISO C forbids subscripting %<register%> array "
3665 : "before C2Y");
3666 1473245 : else if (!lvalue_p (foo))
3667 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3668 : "ISO C90 forbids subscripting non-lvalue "
3669 : "array");
3670 : }
3671 :
3672 2496553 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3673 2496553 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3674 3 : index = fold_convert (sizetype, index);
3675 :
3676 2496553 : type = TREE_TYPE (TREE_TYPE (array));
3677 2496553 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3678 : /* Array ref is const/volatile if the array elements are
3679 : or if the array is. */
3680 7489659 : TREE_READONLY (rval)
3681 2496553 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3682 2496553 : | TREE_READONLY (array));
3683 7489659 : TREE_SIDE_EFFECTS (rval)
3684 2496553 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3685 2496553 : | TREE_SIDE_EFFECTS (array));
3686 4993106 : TREE_THIS_VOLATILE (rval)
3687 2496553 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3688 : /* This was added by rms on 16 Nov 91.
3689 : It fixes vol struct foo *a; a->elts[1]
3690 : in an inline function.
3691 : Hope it doesn't break something else. */
3692 2496553 : | TREE_THIS_VOLATILE (array));
3693 2496553 : ret = require_complete_type (loc, rval);
3694 2496553 : protected_set_expr_location (ret, loc);
3695 2496553 : if (non_lvalue)
3696 105143 : ret = non_lvalue_loc (loc, ret);
3697 2496553 : return ret;
3698 : }
3699 : else
3700 : {
3701 972848 : tree ar = default_conversion (array);
3702 :
3703 972848 : if (ar == error_mark_node)
3704 : return ar;
3705 :
3706 972848 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3707 972848 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3708 :
3709 972848 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3710 : index, false),
3711 : RO_ARRAY_INDEXING);
3712 972848 : if (non_lvalue)
3713 0 : ret = non_lvalue_loc (loc, ret);
3714 972848 : return ret;
3715 : }
3716 : }
3717 :
3718 : /* Build an OpenMP array section reference, creating an exact type for the
3719 : resulting expression based on the element type and bounds if possible. If
3720 : we have variable bounds, create an incomplete array type for the result
3721 : instead. */
3722 :
3723 : tree
3724 3950 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3725 : {
3726 3950 : tree type = TREE_TYPE (array);
3727 3950 : gcc_assert (type);
3728 :
3729 3950 : tree sectype, eltype = TREE_TYPE (type);
3730 :
3731 : /* It's not an array or pointer type. Just reuse the type of the original
3732 : expression as the type of the array section (an error will be raised
3733 : anyway, later). */
3734 3950 : if (eltype == NULL_TREE || error_operand_p (eltype))
3735 12 : sectype = TREE_TYPE (array);
3736 : else
3737 : {
3738 3938 : tree idxtype = NULL_TREE;
3739 :
3740 3938 : if (index != NULL_TREE
3741 3938 : && length != NULL_TREE
3742 3011 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3743 6944 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3744 : {
3745 2999 : tree low = fold_convert (sizetype, index);
3746 2999 : tree high = fold_convert (sizetype, length);
3747 2999 : high = size_binop (PLUS_EXPR, low, high);
3748 2999 : high = size_binop (MINUS_EXPR, high, size_one_node);
3749 2999 : idxtype = build_range_type (sizetype, low, high);
3750 : }
3751 135 : else if ((index == NULL_TREE || integer_zerop (index))
3752 829 : && length != NULL_TREE
3753 1616 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3754 660 : idxtype = build_index_type (length);
3755 :
3756 3938 : gcc_assert (!error_operand_p (idxtype));
3757 :
3758 3938 : sectype = c_build_array_type (eltype, idxtype);
3759 : }
3760 :
3761 3950 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3762 : }
3763 :
3764 :
3765 : /* Record that REF is used. This is relevant for undeclared static
3766 : function and declarations referenced from a non-local context.
3767 : ADDRESS indicates whether the address is taken. */
3768 :
3769 : void
3770 475416686 : mark_decl_used (tree ref, bool address)
3771 : {
3772 475416686 : if (!ref || !DECL_P (ref) || in_alignof)
3773 : return;
3774 :
3775 : /* Non-file-scope and non-local reference in nested function. */
3776 348317068 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3777 122168230 : && DECL_CONTEXT (current_function_decl)
3778 475382630 : && DECL_CONTEXT (ref) != current_function_decl;
3779 :
3780 : /* An undeclared static function. */
3781 475372546 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3782 101905573 : && DECL_INITIAL (ref) == NULL_TREE
3783 69708305 : && DECL_EXTERNAL (ref)
3784 545078253 : && !TREE_PUBLIC (ref);
3785 :
3786 475239028 : if (!static_p && !nonloc_p)
3787 : return;
3788 :
3789 : /* If we may be in an unevaluated context, delay the decision. */
3790 136147 : if (in_sizeof || in_typeof || in_countof || in_generic)
3791 273 : return record_maybe_used_decl (ref, address);
3792 :
3793 135874 : if (static_p)
3794 133262 : C_DECL_USED (ref) = 1;
3795 :
3796 135874 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3797 : || TREE_CODE (ref) == PARM_DECL))
3798 2049 : DECL_NONLOCAL (ref) = 1;
3799 :
3800 : /* Nothing to do anymore. */
3801 2612 : if (!nonloc_p || C_FUNC_NONLOCAL_CONTEXT (current_function_decl))
3802 134667 : return;
3803 :
3804 : /* Filter out the cases where referencing a non-local variable does not
3805 : require a non-local context passed via the static chain. */
3806 1207 : if (!c_type_variably_modified_p (TREE_TYPE (ref)))
3807 1135 : switch (TREE_CODE (ref))
3808 : {
3809 215 : case FUNCTION_DECL:
3810 : /* Use of another local function that requires no context is ok. */
3811 215 : if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref))
3812 : return;
3813 : break;
3814 438 : case VAR_DECL:
3815 : /* Static variables and constexpr are ok, but for the later only
3816 : when the address is not taken. */
3817 438 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3818 : return;
3819 : break;
3820 : case TYPE_DECL:
3821 : /* A typedef is ok when not for a variably-modified type. */
3822 : return;
3823 : case CONST_DECL:
3824 : /* An enumeration constant is ok. */
3825 : return;
3826 : case PARM_DECL:
3827 : break;
3828 : case LABEL_DECL:
3829 : break;
3830 0 : default:
3831 0 : gcc_unreachable ();
3832 : }
3833 :
3834 : /* Mark all parent functions up to the nesting level of the variable as
3835 : as needing the non-local context. */
3836 2019 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3837 : {
3838 2019 : if (cont == DECL_CONTEXT (ref))
3839 : break;
3840 :
3841 : /* There should not be any other type of context used for function
3842 : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3843 1015 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3844 :
3845 1015 : if (TREE_CODE (cont) == FUNCTION_DECL)
3846 1015 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3847 : }
3848 : }
3849 :
3850 :
3851 : /* Build an external reference to identifier ID. FUN indicates
3852 : whether this will be used for a function call. LOC is the source
3853 : location of the identifier. This sets *TYPE to the type of the
3854 : identifier, which is not the same as the type of the returned value
3855 : for CONST_DECLs defined as enum constants. If the type of the
3856 : identifier is not available, *TYPE is set to NULL. */
3857 : tree
3858 174929191 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3859 : {
3860 174929191 : tree ref;
3861 174929191 : tree decl = lookup_name (id);
3862 :
3863 : /* In Objective-C, an instance variable (ivar) may be preferred to
3864 : whatever lookup_name() found. */
3865 174929191 : decl = objc_lookup_ivar (decl, id);
3866 :
3867 174929191 : *type = NULL;
3868 174929191 : if (decl && decl != error_mark_node)
3869 : {
3870 174922994 : ref = decl;
3871 174922994 : *type = TREE_TYPE (ref);
3872 174922994 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3873 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3874 : decl);
3875 : }
3876 6197 : else if (fun)
3877 : /* Implicit function declaration. */
3878 4729 : ref = implicitly_declare (loc, id);
3879 1468 : else if (decl == error_mark_node)
3880 : /* Don't complain about something that's already been
3881 : complained about. */
3882 : return error_mark_node;
3883 : else
3884 : {
3885 1234 : undeclared_variable (loc, id);
3886 1234 : return error_mark_node;
3887 : }
3888 :
3889 : /* For an OpenMP map clause, we can get better diagnostics for decls with
3890 : unmappable types if we return the decl with an error_mark_node type,
3891 : rather than returning error_mark_node for the decl itself. */
3892 174927723 : if (TREE_TYPE (ref) == error_mark_node
3893 174927723 : && !c_omp_array_section_p)
3894 : return error_mark_node;
3895 :
3896 174927705 : if (TREE_UNAVAILABLE (ref))
3897 14 : error_unavailable_use (ref, NULL_TREE);
3898 174927691 : else if (TREE_DEPRECATED (ref))
3899 100 : warn_deprecated_use (ref, NULL_TREE);
3900 :
3901 : /* Recursive call does not count as usage. */
3902 174927705 : if (ref != current_function_decl)
3903 : {
3904 174925059 : TREE_USED (ref) = 1;
3905 : }
3906 :
3907 174927705 : mark_decl_used (ref, false);
3908 :
3909 174927705 : if (TREE_CODE (ref) == CONST_DECL)
3910 : {
3911 417799 : used_types_insert (TREE_TYPE (ref));
3912 :
3913 417799 : if (warn_cxx_compat
3914 377 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3915 418174 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3916 : {
3917 1 : warning_at (loc, OPT_Wc___compat,
3918 : "enum constant defined in struct or union "
3919 : "is not visible in C++");
3920 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3921 : }
3922 :
3923 417799 : ref = DECL_INITIAL (ref);
3924 417799 : TREE_CONSTANT (ref) = 1;
3925 : }
3926 : /* C99 6.7.4p3: An inline definition of a function with external
3927 : linkage ... shall not contain a reference to an identifier with
3928 : internal linkage. */
3929 174509906 : else if (current_function_decl != NULL_TREE
3930 172998698 : && DECL_DECLARED_INLINE_P (current_function_decl)
3931 159361479 : && DECL_EXTERNAL (current_function_decl)
3932 158587845 : && VAR_OR_FUNCTION_DECL_P (ref)
3933 56514637 : && (!VAR_P (ref) || TREE_STATIC (ref))
3934 47366730 : && ! TREE_PUBLIC (ref)
3935 174509940 : && DECL_CONTEXT (ref) != current_function_decl)
3936 25 : record_inline_static (loc, current_function_decl, ref,
3937 : csi_internal);
3938 :
3939 : return ref;
3940 : }
3941 :
3942 : static struct maybe_used_decl *maybe_used_decls;
3943 :
3944 : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
3945 : _Generic, might be used if the operand of sizeof is a VLA type or the
3946 : operand of typeof is a variably modified type or the operand of _Countof has
3947 : a variable number of elements or the operand of _Generic is the one selected
3948 : as the result. */
3949 :
3950 : static void
3951 273 : record_maybe_used_decl (tree decl, bool address)
3952 : {
3953 273 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3954 273 : t->decl = decl;
3955 273 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
3956 273 : t->address = address;
3957 273 : t->next = maybe_used_decls;
3958 273 : maybe_used_decls = t;
3959 273 : }
3960 :
3961 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3962 : _Generic. If USED is false, just discard them. If it is true, mark them
3963 : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
3964 : them to the next level up (if still inside sizeof or typeof or _Countof or
3965 : _Generic). */
3966 :
3967 : void
3968 1466962 : pop_maybe_used (bool used)
3969 : {
3970 1466962 : struct maybe_used_decl *p = maybe_used_decls;
3971 1466962 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3972 1467259 : while (p && p->level > cur_level)
3973 : {
3974 297 : if (used)
3975 : {
3976 77 : if (cur_level == 0)
3977 53 : mark_decl_used (p->decl, p->address);
3978 : else
3979 24 : p->level = cur_level;
3980 : }
3981 297 : p = p->next;
3982 : }
3983 1466962 : if (!used || cur_level == 0)
3984 1466881 : maybe_used_decls = p;
3985 1466962 : }
3986 :
3987 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3988 : _Generic, without acting on them, and return the pointer to the previous top
3989 : of the stack. This for use at the end of a default generic association when
3990 : it is not yet known whether the expression is used. If it later turns out
3991 : the expression is used (or treated as used before C23), restore_maybe_used
3992 : should be called on the return value followed by pop_maybe_used (true);
3993 : otherwise, the return value can be discarded. */
3994 :
3995 : struct maybe_used_decl *
3996 602 : save_maybe_used ()
3997 : {
3998 602 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
3999 602 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
4000 634 : while (p && p->level > cur_level)
4001 32 : p = p->next;
4002 602 : maybe_used_decls = p;
4003 602 : return orig;
4004 : }
4005 :
4006 : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
4007 : or _Generic returned by save_maybe_used. It is required that the stack is
4008 : at exactly the point where it was left by save_maybe_used. */
4009 :
4010 : void
4011 72 : restore_maybe_used (struct maybe_used_decl *stack)
4012 : {
4013 72 : maybe_used_decls = stack;
4014 72 : }
4015 :
4016 : /* Return the result of sizeof applied to EXPR. */
4017 :
4018 : struct c_expr
4019 313048 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
4020 : {
4021 313048 : struct c_expr ret;
4022 313048 : if (expr.value == error_mark_node)
4023 : {
4024 113 : ret.value = error_mark_node;
4025 113 : ret.original_code = ERROR_MARK;
4026 113 : ret.original_type = NULL;
4027 113 : ret.m_decimal = 0;
4028 113 : pop_maybe_used (false);
4029 : }
4030 : else
4031 : {
4032 312935 : bool expr_const_operands = true;
4033 :
4034 312935 : if (TREE_CODE (expr.value) == PARM_DECL
4035 312935 : && C_ARRAY_PARAMETER (expr.value))
4036 : {
4037 55 : auto_diagnostic_group d;
4038 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
4039 : "%<sizeof%> on array function parameter %qE will "
4040 : "return size of %qT", expr.value,
4041 55 : TREE_TYPE (expr.value)))
4042 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
4043 55 : }
4044 312935 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4045 : &expr_const_operands);
4046 312935 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
4047 312935 : c_last_sizeof_arg = expr.value;
4048 312935 : c_last_sizeof_loc = loc;
4049 312935 : ret.original_code = SIZEOF_EXPR;
4050 312935 : ret.original_type = NULL;
4051 312935 : ret.m_decimal = 0;
4052 312935 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
4053 : {
4054 : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
4055 288 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4056 : folded_expr, ret.value);
4057 288 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4058 288 : SET_EXPR_LOCATION (ret.value, loc);
4059 : }
4060 312935 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
4061 : }
4062 313048 : return ret;
4063 : }
4064 :
4065 : /* Return the result of sizeof applied to T, a structure for the type
4066 : name passed to sizeof (rather than the type itself). LOC is the
4067 : location of the original expression. */
4068 :
4069 : struct c_expr
4070 331387 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
4071 : {
4072 331387 : tree type;
4073 331387 : struct c_expr ret;
4074 331387 : tree type_expr = NULL_TREE;
4075 331387 : bool type_expr_const = true;
4076 331387 : type = groktypename (t, &type_expr, &type_expr_const);
4077 331387 : ret.value = c_sizeof (loc, type);
4078 331387 : c_last_sizeof_arg = type;
4079 331387 : c_last_sizeof_loc = loc;
4080 331387 : ret.original_code = SIZEOF_EXPR;
4081 331387 : ret.original_type = NULL;
4082 331387 : ret.m_decimal = 0;
4083 331387 : if (type == error_mark_node)
4084 : {
4085 5 : ret.value = error_mark_node;
4086 5 : ret.original_code = ERROR_MARK;
4087 : }
4088 : else
4089 331340 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4090 662637 : && C_TYPE_VARIABLE_SIZE (type))
4091 : {
4092 : /* If the type is a [*] array, it is a VLA but is represented as
4093 : having a size of zero. In such a case we must ensure that
4094 : the result of sizeof does not get folded to a constant by
4095 : c_fully_fold, because if the size is evaluated the result is
4096 : not constant and so constraints on zero or negative size
4097 : arrays must not be applied when this sizeof call is inside
4098 : another array declarator. */
4099 41 : if (!type_expr)
4100 16 : type_expr = integer_zero_node;
4101 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4102 : type_expr, ret.value);
4103 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4104 : }
4105 331387 : pop_maybe_used (type != error_mark_node
4106 331387 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4107 331387 : return ret;
4108 : }
4109 :
4110 : static bool
4111 138 : is_top_array_vla (tree type)
4112 : {
4113 138 : bool zero, var;
4114 138 : tree d;
4115 :
4116 138 : if (TREE_CODE (type) != ARRAY_TYPE)
4117 : return false;
4118 120 : if (!COMPLETE_TYPE_P (type))
4119 : return false;
4120 :
4121 116 : d = TYPE_DOMAIN (type);
4122 116 : zero = !TYPE_MAX_VALUE (d);
4123 116 : if (zero)
4124 : return false;
4125 :
4126 100 : var = (TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST
4127 100 : || TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST);
4128 : return var;
4129 : }
4130 :
4131 : /* Return the result of countof applied to EXPR. */
4132 :
4133 : struct c_expr
4134 54 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4135 : {
4136 54 : struct c_expr ret;
4137 54 : if (expr.value == error_mark_node)
4138 : {
4139 1 : ret.value = error_mark_node;
4140 1 : ret.original_code = ERROR_MARK;
4141 1 : ret.original_type = NULL;
4142 1 : ret.m_decimal = 0;
4143 1 : pop_maybe_used (false);
4144 : }
4145 : else
4146 : {
4147 53 : bool expr_const_operands = true;
4148 :
4149 53 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4150 : &expr_const_operands);
4151 53 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4152 53 : c_last_sizeof_arg = expr.value;
4153 53 : c_last_sizeof_loc = loc;
4154 53 : ret.original_code = COUNTOF_EXPR;
4155 53 : ret.original_type = NULL;
4156 53 : ret.m_decimal = 0;
4157 53 : if (is_top_array_vla (TREE_TYPE (folded_expr)))
4158 : {
4159 : /* countof is evaluated when given a vla. */
4160 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4161 : folded_expr, ret.value);
4162 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4163 11 : SET_EXPR_LOCATION (ret.value, loc);
4164 : }
4165 53 : pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
4166 : }
4167 54 : return ret;
4168 : }
4169 :
4170 : /* Return the result of countof applied to T, a structure for the type
4171 : name passed to countof (rather than the type itself). LOC is the
4172 : location of the original expression. */
4173 :
4174 : struct c_expr
4175 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4176 : {
4177 18 : tree type;
4178 18 : struct c_expr ret;
4179 18 : tree type_expr = NULL_TREE;
4180 18 : bool type_expr_const = true;
4181 18 : type = groktypename (t, &type_expr, &type_expr_const);
4182 18 : ret.value = c_countof_type (loc, type);
4183 18 : c_last_sizeof_arg = type;
4184 18 : c_last_sizeof_loc = loc;
4185 18 : ret.original_code = COUNTOF_EXPR;
4186 18 : ret.original_type = NULL;
4187 18 : ret.m_decimal = 0;
4188 18 : if (type == error_mark_node)
4189 : {
4190 0 : ret.value = error_mark_node;
4191 0 : ret.original_code = ERROR_MARK;
4192 : }
4193 : else
4194 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4195 22 : && is_top_array_vla (type))
4196 : {
4197 : /* If the type is a [*] array, it is a VLA but is represented as
4198 : having a size of zero. In such a case we must ensure that
4199 : the result of countof does not get folded to a constant by
4200 : c_fully_fold, because if the number of elements is evaluated
4201 : the result is not constant and so
4202 : constraints on zero or negative size arrays must not be applied
4203 : when this countof call is inside another array declarator. */
4204 6 : if (!type_expr)
4205 0 : type_expr = integer_zero_node;
4206 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4207 : type_expr, ret.value);
4208 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4209 : }
4210 18 : pop_maybe_used (type != error_mark_node ? is_top_array_vla (type) : false);
4211 18 : return ret;
4212 : }
4213 :
4214 : /* Return the result of maxof applied to T, a structure for the type
4215 : name passed to maxof (rather than the type itself). LOC is the
4216 : location of the original expression. */
4217 :
4218 : struct c_expr
4219 46 : c_expr_maxof_type (location_t loc, struct c_type_name *t)
4220 : {
4221 46 : tree type;
4222 46 : struct c_expr ret;
4223 46 : tree type_expr = NULL_TREE;
4224 46 : bool type_expr_const = true;
4225 46 : type = groktypename (t, &type_expr, &type_expr_const);
4226 46 : ret.value = c_maxof_type (loc, type);
4227 46 : c_last_sizeof_arg = type;
4228 46 : c_last_sizeof_loc = loc;
4229 46 : ret.original_code = MAXOF_EXPR;
4230 46 : ret.original_type = NULL;
4231 46 : ret.m_decimal = 0;
4232 46 : if (type == error_mark_node)
4233 : {
4234 0 : ret.value = error_mark_node;
4235 0 : ret.original_code = ERROR_MARK;
4236 : }
4237 46 : pop_maybe_used (type != error_mark_node);
4238 46 : return ret;
4239 : }
4240 :
4241 : /* Return the result of minof applied to T, a structure for the type
4242 : name passed to minof (rather than the type itself). LOC is the
4243 : location of the original expression. */
4244 :
4245 : struct c_expr
4246 46 : c_expr_minof_type (location_t loc, struct c_type_name *t)
4247 : {
4248 46 : tree type;
4249 46 : struct c_expr ret;
4250 46 : tree type_expr = NULL_TREE;
4251 46 : bool type_expr_const = true;
4252 46 : type = groktypename (t, &type_expr, &type_expr_const);
4253 46 : ret.value = c_minof_type (loc, type);
4254 46 : c_last_sizeof_arg = type;
4255 46 : c_last_sizeof_loc = loc;
4256 46 : ret.original_code = MINOF_EXPR;
4257 46 : ret.original_type = NULL;
4258 46 : ret.m_decimal = 0;
4259 46 : if (type == error_mark_node)
4260 : {
4261 0 : ret.value = error_mark_node;
4262 0 : ret.original_code = ERROR_MARK;
4263 : }
4264 46 : pop_maybe_used (type != error_mark_node);
4265 46 : return ret;
4266 : }
4267 :
4268 : /* Build a function call to function FUNCTION with parameters PARAMS.
4269 : The function call is at LOC.
4270 : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4271 : TREE_VALUE of each node is a parameter-expression.
4272 : FUNCTION's data type may be a function type or a pointer-to-function. */
4273 :
4274 : tree
4275 0 : build_function_call (location_t loc, tree function, tree params)
4276 : {
4277 0 : vec<tree, va_gc> *v;
4278 0 : tree ret;
4279 :
4280 0 : vec_alloc (v, list_length (params));
4281 0 : for (; params; params = TREE_CHAIN (params))
4282 0 : v->quick_push (TREE_VALUE (params));
4283 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4284 0 : vec_free (v);
4285 0 : return ret;
4286 : }
4287 :
4288 : /* Give a note about the location of the declaration of DECL,
4289 : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4290 :
4291 : static void
4292 299 : inform_declaration (tree decl, tree function_expr)
4293 : {
4294 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4295 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4296 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4297 92 : else if (function_expr)
4298 92 : switch (TREE_CODE (function_expr))
4299 : {
4300 : default:
4301 : break;
4302 13 : case COMPONENT_REF:
4303 : /* Show the decl of the pertinent field (e.g. for callback
4304 : fields in a struct. */
4305 13 : {
4306 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4307 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4308 13 : inform (loc, "declared here");
4309 : }
4310 : break;
4311 : }
4312 299 : }
4313 :
4314 : /* C implementation of callback for use when checking param types. */
4315 :
4316 : static bool
4317 2 : comp_parm_types (tree wanted_type, tree actual_type)
4318 : {
4319 2 : return comptypes (wanted_type, actual_type);
4320 : }
4321 :
4322 : /* Build a function call to function FUNCTION with parameters PARAMS.
4323 : If FUNCTION is the result of resolving an overloaded target built-in,
4324 : ORIG_FUNDECL is the original function decl, otherwise it is null.
4325 : ORIGTYPES, if not NULL, is a vector of types; each element is
4326 : either NULL or the original type of the corresponding element in
4327 : PARAMS. The original type may differ from TREE_TYPE of the
4328 : parameter for enums. FUNCTION's data type may be a function type
4329 : or pointer-to-function. This function changes the elements of
4330 : PARAMS. */
4331 :
4332 : tree
4333 49893854 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4334 : tree function, vec<tree, va_gc> *params,
4335 : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4336 : {
4337 49893854 : tree fntype, fundecl = NULL_TREE;
4338 49893854 : tree name = NULL_TREE, result;
4339 49893854 : tree tem;
4340 49893854 : int nargs;
4341 49893854 : tree *argarray;
4342 :
4343 :
4344 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4345 49893854 : STRIP_TYPE_NOPS (function);
4346 :
4347 : /* Convert anything with function type to a pointer-to-function. */
4348 49893854 : if (TREE_CODE (function) == FUNCTION_DECL)
4349 : {
4350 49816428 : name = DECL_NAME (function);
4351 :
4352 49816428 : if (flag_tm)
4353 421 : tm_malloc_replacement (function);
4354 49816428 : fundecl = function;
4355 49816428 : if (!orig_fundecl)
4356 49816428 : orig_fundecl = fundecl;
4357 : /* Atomic functions have type checking/casting already done. They are
4358 : often rewritten and don't match the original parameter list. */
4359 99632856 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4360 : origtypes = NULL;
4361 : }
4362 49893854 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4363 49830906 : function = function_to_pointer_conversion (loc, function);
4364 :
4365 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4366 : expressions, like those used for ObjC messenger dispatches. */
4367 49893854 : if (params && !params->is_empty ())
4368 38179340 : function = objc_rewrite_function_call (function, (*params)[0]);
4369 :
4370 49893854 : function = c_fully_fold (function, false, NULL);
4371 :
4372 49893854 : fntype = TREE_TYPE (function);
4373 :
4374 49893854 : if (TREE_CODE (fntype) == ERROR_MARK)
4375 5 : return error_mark_node;
4376 :
4377 49893849 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4378 49893815 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4379 : {
4380 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4381 33 : error_at (loc,
4382 : "called object %qE is not a function or function pointer",
4383 : function);
4384 4 : else if (DECL_P (function))
4385 : {
4386 1 : auto_diagnostic_group d;
4387 1 : error_at (loc,
4388 : "called object %qD is not a function or function pointer",
4389 : function);
4390 1 : inform_declaration (function, NULL_TREE);
4391 1 : }
4392 : else
4393 3 : error_at (loc,
4394 : "called object is not a function or function pointer");
4395 37 : return error_mark_node;
4396 : }
4397 :
4398 49893812 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4399 374365 : current_function_returns_abnormally = 1;
4400 :
4401 : /* fntype now gets the type of function pointed to. */
4402 49893812 : fntype = TREE_TYPE (fntype);
4403 49893812 : tree return_type = TREE_TYPE (fntype);
4404 :
4405 : /* Convert the parameters to the types declared in the
4406 : function prototype, or apply default promotions. */
4407 :
4408 49893812 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4409 : origtypes, function, fundecl);
4410 49893812 : if (nargs < 0)
4411 178 : return error_mark_node;
4412 :
4413 : /* Check that the function is called through a compatible prototype.
4414 : If it is not, warn. */
4415 49892727 : if (CONVERT_EXPR_P (function)
4416 923 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4417 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4418 49893819 : && !comptypes (fntype, TREE_TYPE (tem)))
4419 : {
4420 : /* This situation leads to run-time undefined behavior. We can't,
4421 : therefore, simply error unless we can prove that all possible
4422 : executions of the program must execute the code. */
4423 20 : warning_at (loc, 0, "function called through a non-compatible type");
4424 :
4425 20 : if (VOID_TYPE_P (return_type)
4426 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4427 1 : pedwarn (loc, 0,
4428 : "function with qualified void return type called");
4429 : }
4430 :
4431 49893634 : argarray = vec_safe_address (params);
4432 :
4433 : /* Check that arguments to builtin functions match the expectations. */
4434 49893634 : if (fundecl
4435 49816265 : && fndecl_built_in_p (fundecl)
4436 83140319 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4437 : orig_fundecl, nargs, argarray))
4438 298 : return error_mark_node;
4439 :
4440 : /* Check that the arguments to the function are valid. */
4441 49893336 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4442 : nargs, argarray, &arg_loc,
4443 : comp_parm_types);
4444 :
4445 49893336 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4446 49893336 : && !VOID_TYPE_P (return_type))
4447 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4448 49893336 : if (name != NULL_TREE
4449 99709303 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4450 : {
4451 32526702 : if (require_constant_value)
4452 3460 : result
4453 3460 : = fold_build_call_array_initializer_loc (loc, return_type,
4454 : function, nargs, argarray);
4455 : else
4456 32523242 : result = fold_build_call_array_loc (loc, return_type,
4457 : function, nargs, argarray);
4458 32526702 : if (TREE_CODE (result) == NOP_EXPR
4459 32526702 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4460 26442 : STRIP_TYPE_NOPS (result);
4461 : }
4462 : else
4463 17366634 : result = build_call_array_loc (loc, return_type,
4464 : function, nargs, argarray);
4465 : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4466 : later. */
4467 49893336 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4468 215 : suppress_warning (result, OPT_Wnonnull);
4469 :
4470 : /* In this improbable scenario, a nested function returns a VM type.
4471 : Create a TARGET_EXPR so that the call always has a LHS, much as
4472 : what the C++ FE does for functions returning non-PODs. */
4473 49893336 : if (c_type_variably_modified_p (TREE_TYPE (fntype)))
4474 : {
4475 83 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4476 83 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4477 : NULL_TREE, NULL_TREE);
4478 : }
4479 :
4480 49893336 : if (VOID_TYPE_P (TREE_TYPE (result)))
4481 : {
4482 2390553 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4483 2 : pedwarn (loc, 0,
4484 : "function with qualified void return type called");
4485 2390553 : return result;
4486 : }
4487 47502783 : return require_complete_type (loc, result);
4488 : }
4489 :
4490 : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4491 :
4492 : tree
4493 49893411 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4494 : tree function, vec<tree, va_gc> *params,
4495 : vec<tree, va_gc> *origtypes)
4496 : {
4497 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4498 49893422 : STRIP_TYPE_NOPS (function);
4499 :
4500 : /* Convert anything with function type to a pointer-to-function. */
4501 49893411 : if (TREE_CODE (function) == FUNCTION_DECL)
4502 : {
4503 : /* Implement type-directed function overloading for builtins.
4504 : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4505 : handle all the type checking. The result is a complete expression
4506 : that implements this function call. */
4507 49815985 : tree tem = resolve_overloaded_builtin (loc, function, params);
4508 49815985 : if (tem)
4509 : return tem;
4510 : }
4511 49807460 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4512 : }
4513 :
4514 : /* Helper for convert_arguments called to convert the VALue of argument
4515 : number ARGNUM from ORIGTYPE to the corresponding parameter number
4516 : PARMNUM and TYPE.
4517 : PLOC is the location where the conversion is being performed.
4518 : FUNCTION and FUNDECL are the same as in convert_arguments.
4519 : VALTYPE is the original type of VAL before the conversion and,
4520 : for EXCESS_PRECISION_EXPR, the operand of the expression.
4521 : NPC is true if VAL represents the null pointer constant (VAL itself
4522 : will have been folded to an integer constant).
4523 : RNAME is the same as FUNCTION except in Objective C when it's
4524 : the function selector.
4525 : EXCESS_PRECISION is true when VAL was originally represented
4526 : as EXCESS_PRECISION_EXPR.
4527 : WARNOPT is the same as in convert_for_assignment. */
4528 :
4529 : static tree
4530 125605154 : convert_argument (location_t ploc, tree function, tree fundecl,
4531 : tree type, tree origtype, tree val, tree valtype,
4532 : bool npc, tree rname, int parmnum, int argnum,
4533 : bool excess_precision, int warnopt)
4534 : {
4535 : /* Formal parm type is specified by a function prototype. */
4536 :
4537 125605154 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4538 : {
4539 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4540 : parmnum + 1);
4541 3 : return error_mark_node;
4542 : }
4543 :
4544 : /* Optionally warn about conversions that differ from the default
4545 : conversions. */
4546 125605151 : if (warn_traditional_conversion || warn_traditional)
4547 : {
4548 193 : if (INTEGRAL_TYPE_P (type)
4549 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4550 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4551 : "passing argument %d of %qE as integer rather "
4552 : "than floating due to prototype",
4553 : argnum, rname);
4554 193 : if (INTEGRAL_TYPE_P (type)
4555 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4556 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4557 : "passing argument %d of %qE as integer rather "
4558 : "than complex due to prototype",
4559 : argnum, rname);
4560 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4561 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4562 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4563 : "passing argument %d of %qE as complex rather "
4564 : "than floating due to prototype",
4565 : argnum, rname);
4566 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4567 71 : && INTEGRAL_TYPE_P (valtype))
4568 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4569 : "passing argument %d of %qE as floating rather "
4570 : "than integer due to prototype",
4571 : argnum, rname);
4572 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4573 7 : && INTEGRAL_TYPE_P (valtype))
4574 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4575 : "passing argument %d of %qE as complex rather "
4576 : "than integer due to prototype",
4577 : argnum, rname);
4578 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4579 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4580 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4581 : "passing argument %d of %qE as floating rather "
4582 : "than complex due to prototype",
4583 : argnum, rname);
4584 : /* ??? At some point, messages should be written about
4585 : conversions between complex types, but that's too messy
4586 : to do now. */
4587 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4588 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4589 : {
4590 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4591 :
4592 : /* Warn if any argument is passed as `float',
4593 : since without a prototype it would be `double'. */
4594 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4595 45 : && type != dfloat32_type_node)
4596 7 : warning_at (ploc, 0,
4597 : "passing argument %d of %qE as %<float%> "
4598 : "rather than %<double%> due to prototype",
4599 : argnum, rname);
4600 :
4601 : /* Warn if mismatch between argument and prototype
4602 : for decimal float types. Warn of conversions with
4603 : binary float types and of precision narrowing due to
4604 : prototype. */
4605 38 : else if (type != valtype
4606 32 : && (type == dfloat32_type_node
4607 22 : || type == dfloat64_type_node
4608 12 : || type == dfloat128_type_node
4609 2 : || valtype == dfloat32_type_node
4610 2 : || valtype == dfloat64_type_node
4611 2 : || valtype == dfloat128_type_node)
4612 38 : && (formal_prec
4613 30 : <= TYPE_PRECISION (valtype)
4614 14 : || (type == dfloat128_type_node
4615 10 : && (valtype
4616 10 : != dfloat64_type_node
4617 8 : && (valtype
4618 : != dfloat32_type_node)))
4619 8 : || (type == dfloat64_type_node
4620 4 : && (valtype
4621 : != dfloat32_type_node))))
4622 24 : warning_at (ploc, 0,
4623 : "passing argument %d of %qE as %qT "
4624 : "rather than %qT due to prototype",
4625 : argnum, rname, type, valtype);
4626 :
4627 : }
4628 : /* Detect integer changing in width or signedness.
4629 : These warnings are only activated with
4630 : -Wtraditional-conversion, not with -Wtraditional. */
4631 105 : else if (warn_traditional_conversion
4632 105 : && INTEGRAL_TYPE_P (type)
4633 102 : && INTEGRAL_TYPE_P (valtype))
4634 : {
4635 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4636 83 : tree would_have_been = default_conversion (val);
4637 83 : tree type1 = TREE_TYPE (would_have_been);
4638 :
4639 83 : if (val == error_mark_node)
4640 : /* VAL could have been of incomplete type. */;
4641 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4642 83 : && (TYPE_MAIN_VARIANT (type)
4643 2 : == TYPE_MAIN_VARIANT (valtype)))
4644 : /* No warning if function asks for enum
4645 : and the actual arg is that enum type. */
4646 : ;
4647 81 : else if (formal_prec != TYPE_PRECISION (type1))
4648 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4649 : "passing argument %d of %qE "
4650 : "with different width due to prototype",
4651 : argnum, rname);
4652 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4653 : ;
4654 : /* Don't complain if the formal parameter type
4655 : is an enum, because we can't tell now whether
4656 : the value was an enum--even the same enum. */
4657 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4658 : ;
4659 32 : else if (TREE_CODE (val) == INTEGER_CST
4660 5 : && int_fits_type_p (val, type))
4661 : /* Change in signedness doesn't matter
4662 : if a constant value is unaffected. */
4663 : ;
4664 : /* If the value is extended from a narrower
4665 : unsigned type, it doesn't matter whether we
4666 : pass it as signed or unsigned; the value
4667 : certainly is the same either way. */
4668 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4669 32 : && TYPE_UNSIGNED (valtype))
4670 : ;
4671 32 : else if (TYPE_UNSIGNED (type))
4672 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4673 : "passing argument %d of %qE "
4674 : "as unsigned due to prototype",
4675 : argnum, rname);
4676 : else
4677 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4678 : "passing argument %d of %qE "
4679 : "as signed due to prototype",
4680 : argnum, rname);
4681 : }
4682 : }
4683 :
4684 : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4685 : sake of better warnings from convert_and_check. */
4686 125605151 : if (excess_precision)
4687 380 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4688 :
4689 125605151 : tree parmval = convert_for_assignment (ploc, ploc, type,
4690 : val, origtype, ic_argpass,
4691 : npc, fundecl, function,
4692 : parmnum + 1, warnopt);
4693 125605151 : return parmval;
4694 : }
4695 :
4696 : /* Convert the argument expressions in the vector VALUES
4697 : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4698 :
4699 : If the list is exhausted, or when an element has NULL as its type,
4700 : perform the default conversions.
4701 :
4702 : ORIGTYPES is the original types of the expressions in VALUES. This
4703 : holds the type of enum values which have been converted to integral
4704 : types. It may be NULL.
4705 :
4706 : FUNCTION is a tree for the called function. It is used only for
4707 : error messages, where it is formatted with %qE.
4708 :
4709 : This is also where warnings about wrong number of args are generated.
4710 :
4711 : ARG_LOC are locations of function arguments (if any).
4712 :
4713 : Returns the actual number of arguments processed (which may be less
4714 : than the length of VALUES in some error situations), or -1 on
4715 : failure. */
4716 :
4717 : static int
4718 49893812 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4719 : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4720 : tree function, tree fundecl)
4721 : {
4722 49893812 : tree typelist = TYPE_ARG_TYPES (fntype);
4723 49893812 : unsigned int parmnum;
4724 49893812 : bool error_args = false;
4725 49893812 : const bool type_generic = fundecl
4726 49893812 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4727 49893812 : bool type_generic_remove_excess_precision = false;
4728 49893812 : bool type_generic_overflow_p = false;
4729 49893812 : bool type_generic_bit_query = false;
4730 49893812 : tree selector;
4731 :
4732 : /* Change pointer to function to the function itself for
4733 : diagnostics. */
4734 49893812 : if (TREE_CODE (function) == ADDR_EXPR
4735 49893812 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4736 49818526 : function = TREE_OPERAND (function, 0);
4737 :
4738 : /* Handle an ObjC selector specially for diagnostics. */
4739 49893812 : selector = objc_message_selector ();
4740 :
4741 : /* For a call to a built-in function declared without a prototype,
4742 : set to the built-in function's argument list. */
4743 49893812 : tree builtin_typelist = NULL_TREE;
4744 :
4745 : /* For type-generic built-in functions, determine whether excess
4746 : precision should be removed (classification) or not
4747 : (comparison). */
4748 49893812 : if (fundecl
4749 49893812 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4750 : {
4751 1397809 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4752 1397809 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4753 : {
4754 : /* For a call to a built-in function declared without a prototype
4755 : use the types of the parameters of the internal built-in to
4756 : match those of the arguments to. */
4757 691094 : if (tree bdecl = builtin_decl_explicit (code))
4758 691094 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4759 : }
4760 :
4761 : /* For type-generic built-in functions, determine whether excess
4762 : precision should be removed (classification) or not
4763 : (comparison). */
4764 1397809 : if (type_generic)
4765 60876 : switch (code)
4766 : {
4767 5367 : case BUILT_IN_ISFINITE:
4768 5367 : case BUILT_IN_ISINF:
4769 5367 : case BUILT_IN_ISINF_SIGN:
4770 5367 : case BUILT_IN_ISNAN:
4771 5367 : case BUILT_IN_ISNORMAL:
4772 5367 : case BUILT_IN_ISSIGNALING:
4773 5367 : case BUILT_IN_FPCLASSIFY:
4774 5367 : type_generic_remove_excess_precision = true;
4775 5367 : break;
4776 :
4777 21026 : case BUILT_IN_ADD_OVERFLOW_P:
4778 21026 : case BUILT_IN_SUB_OVERFLOW_P:
4779 21026 : case BUILT_IN_MUL_OVERFLOW_P:
4780 : /* The last argument of these type-generic builtins
4781 : should not be promoted. */
4782 21026 : type_generic_overflow_p = true;
4783 21026 : break;
4784 :
4785 1302 : case BUILT_IN_CLZG:
4786 1302 : case BUILT_IN_CTZG:
4787 1302 : case BUILT_IN_CLRSBG:
4788 1302 : case BUILT_IN_FFSG:
4789 1302 : case BUILT_IN_PARITYG:
4790 1302 : case BUILT_IN_POPCOUNTG:
4791 : /* The first argument of these type-generic builtins
4792 : should not be promoted. */
4793 1302 : type_generic_bit_query = true;
4794 1302 : break;
4795 :
4796 : default:
4797 : break;
4798 : }
4799 : }
4800 :
4801 : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4802 : individual converted arguments. */
4803 :
4804 49893812 : tree typetail, builtin_typetail, val;
4805 49893812 : for (typetail = typelist,
4806 49893812 : builtin_typetail = builtin_typelist,
4807 49893812 : parmnum = 0;
4808 176433529 : values && values->iterate (parmnum, &val);
4809 : ++parmnum)
4810 : {
4811 : /* The type of the function parameter (if it was declared with one). */
4812 252143021 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4813 : /* The type of the built-in function parameter (if the function
4814 : is a built-in). Used to detect type incompatibilities in
4815 : calls to built-ins declared without a prototype. */
4816 126539753 : tree builtin_type = (builtin_typetail
4817 127577616 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4818 : /* The original type of the argument being passed to the function. */
4819 126539753 : tree valtype = TREE_TYPE (val);
4820 : /* The called function (or function selector in Objective C). */
4821 126539753 : tree rname = function;
4822 126539753 : int argnum = parmnum + 1;
4823 126539753 : const char *invalid_func_diag;
4824 : /* Set for EXCESS_PRECISION_EXPR arguments. */
4825 126539753 : bool excess_precision = false;
4826 : /* The value of the argument after conversion to the type
4827 : of the function parameter it is passed to. */
4828 126539753 : tree parmval;
4829 : /* Some __atomic_* builtins have additional hidden argument at
4830 : position 0. */
4831 126539753 : location_t ploc
4832 126270905 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4833 126270305 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4834 126539753 : : input_location;
4835 :
4836 126539753 : if (type == void_type_node)
4837 : {
4838 33 : auto_diagnostic_group d;
4839 33 : int num_expected = parmnum;
4840 33 : int num_actual = values->length ();
4841 33 : gcc_rich_location rich_loc (loc);
4842 33 : if (ploc != input_location)
4843 32 : rich_loc.add_range (ploc);
4844 33 : if (selector)
4845 0 : error_at (&rich_loc,
4846 : "too many arguments to method %qE; expected %i, have %i",
4847 : selector, num_expected, num_actual);
4848 : else
4849 33 : error_at (&rich_loc,
4850 : "too many arguments to function %qE; expected %i, have %i",
4851 : function, num_expected, num_actual);
4852 33 : inform_declaration (fundecl, function);
4853 65 : return error_args ? -1 : (int) parmnum;
4854 33 : }
4855 :
4856 126539720 : if (builtin_type == void_type_node)
4857 : {
4858 12 : auto_diagnostic_group d;
4859 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4860 : "too many arguments to built-in function %qE "
4861 : "expecting %d", function, parmnum))
4862 12 : inform_declaration (fundecl, function);
4863 12 : builtin_typetail = NULL_TREE;
4864 12 : }
4865 :
4866 90393 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4867 126629994 : && !(fundecl && fndecl_built_in_p (fundecl)))
4868 : {
4869 6962 : auto_diagnostic_group d;
4870 6962 : bool warned;
4871 6962 : if (selector)
4872 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4873 : "ISO C23 does not allow arguments"
4874 : " for method %qE declared without parameters",
4875 : function);
4876 : else
4877 6962 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4878 : "ISO C23 does not allow arguments"
4879 : " for function %qE declared without parameters",
4880 : function);
4881 6962 : if (warned)
4882 4 : inform_declaration (fundecl, function);
4883 6962 : }
4884 :
4885 126539720 : if (selector && argnum > 2)
4886 : {
4887 0 : rname = selector;
4888 0 : argnum -= 2;
4889 : }
4890 :
4891 : /* Determine if VAL is a null pointer constant before folding it. */
4892 126539720 : bool npc = null_pointer_constant_p (val);
4893 :
4894 : /* If there is excess precision and a prototype, convert once to
4895 : the required type rather than converting via the semantic
4896 : type. Likewise without a prototype a float value represented
4897 : as long double should be converted once to double. But for
4898 : type-generic classification functions excess precision must
4899 : be removed here. */
4900 126539720 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4901 433 : && (type || !type_generic || !type_generic_remove_excess_precision))
4902 : {
4903 413 : val = TREE_OPERAND (val, 0);
4904 413 : excess_precision = true;
4905 : }
4906 126539720 : val = c_fully_fold (val, false, NULL);
4907 253079441 : STRIP_TYPE_NOPS (val);
4908 :
4909 126539720 : val = require_complete_type (ploc, val);
4910 :
4911 : /* Some floating-point arguments must be promoted to double when
4912 : no type is specified by a prototype. This applies to
4913 : arguments of type float, and to architecture-specific types
4914 : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4915 126539720 : bool promote_float_arg = false;
4916 126539720 : if (type == NULL_TREE
4917 936485 : && TREE_CODE (valtype) == REAL_TYPE
4918 269411 : && (TYPE_PRECISION (valtype)
4919 269411 : <= TYPE_PRECISION (double_type_node))
4920 262165 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4921 131285 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4922 126671005 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4923 : {
4924 : /* Promote this argument, unless it has a _FloatN or
4925 : _FloatNx type. */
4926 1026782 : promote_float_arg = true;
4927 1026782 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4928 899507 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4929 : {
4930 : promote_float_arg = false;
4931 : break;
4932 : }
4933 : /* Don't promote __bf16 either. */
4934 130552 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4935 126409238 : promote_float_arg = false;
4936 : }
4937 :
4938 126539720 : if (type != NULL_TREE)
4939 : {
4940 125603235 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4941 125603235 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4942 : val, valtype, npc, rname, parmnum, argnum,
4943 : excess_precision, 0);
4944 : }
4945 : /* A NULLPTR type is just a nullptr always. */
4946 936485 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4947 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4948 936476 : else if (promote_float_arg)
4949 : {
4950 127205 : if (type_generic)
4951 : parmval = val;
4952 : else
4953 : {
4954 : /* Convert `float' to `double'. */
4955 124446 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4956 6 : warning_at (ploc, OPT_Wdouble_promotion,
4957 : "implicit conversion from %qT to %qT when passing "
4958 : "argument to function",
4959 : valtype, double_type_node);
4960 124446 : parmval = convert (double_type_node, val);
4961 : }
4962 : }
4963 809271 : else if ((excess_precision && !type_generic)
4964 809269 : || (type_generic_overflow_p && parmnum == 2)
4965 788247 : || (type_generic_bit_query && parmnum == 0))
4966 : /* A "double" argument with excess precision being passed
4967 : without a prototype or in variable arguments.
4968 : The last argument of __builtin_*_overflow_p should not be
4969 : promoted, similarly the first argument of
4970 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4971 22320 : parmval = convert (valtype, val);
4972 1573902 : else if ((invalid_func_diag =
4973 786951 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4974 : {
4975 0 : error (invalid_func_diag);
4976 0 : return -1;
4977 : }
4978 786951 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4979 : {
4980 : return -1;
4981 : }
4982 : else
4983 : /* Convert `short' and `char' to full-size `int'. */
4984 786948 : parmval = default_conversion (val);
4985 :
4986 126539717 : (*values)[parmnum] = parmval;
4987 126539717 : if (parmval == error_mark_node)
4988 105 : error_args = true;
4989 :
4990 126539717 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4991 : {
4992 : /* For a call to a built-in function declared without a prototype,
4993 : perform the conversions from the argument to the expected type
4994 : but issue warnings rather than errors for any mismatches.
4995 : Ignore the converted argument and use the PARMVAL obtained
4996 : above by applying default conversions instead. */
4997 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4998 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4999 : val, valtype, npc, rname, parmnum, argnum,
5000 : excess_precision,
5001 : OPT_Wbuiltin_declaration_mismatch);
5002 : }
5003 :
5004 126539717 : if (typetail)
5005 125603235 : typetail = TREE_CHAIN (typetail);
5006 :
5007 126539717 : if (builtin_typetail)
5008 1037846 : builtin_typetail = TREE_CHAIN (builtin_typetail);
5009 : }
5010 :
5011 88073069 : gcc_assert (parmnum == vec_safe_length (values));
5012 :
5013 99457701 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
5014 : {
5015 : /* Not enough args.
5016 : Determine minimum number of arguments required. */
5017 : int min_expected_num = 0;
5018 231 : bool at_least_p = false;
5019 : tree iter = typelist;
5020 386 : while (true)
5021 : {
5022 231 : if (!iter)
5023 : {
5024 : /* Variadic arguments; stop iterating. */
5025 : at_least_p = true;
5026 : break;
5027 : }
5028 222 : if (iter == void_list_node)
5029 : /* End of arguments; stop iterating. */
5030 : break;
5031 155 : ++min_expected_num;
5032 155 : iter = TREE_CHAIN (iter);
5033 : }
5034 76 : auto_diagnostic_group d;
5035 76 : int actual_num = vec_safe_length (values);
5036 143 : error_at (loc,
5037 : at_least_p
5038 : ? G_("too few arguments to function %qE; expected at least %i, have %i")
5039 : : G_("too few arguments to function %qE; expected %i, have %i"),
5040 : function, min_expected_num, actual_num);
5041 76 : inform_declaration (fundecl, function);
5042 76 : return -1;
5043 76 : }
5044 :
5045 50548587 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
5046 : {
5047 : unsigned nargs = parmnum;
5048 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
5049 428 : ++nargs;
5050 :
5051 188 : auto_diagnostic_group d;
5052 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
5053 : "too few arguments to built-in function %qE "
5054 : "expecting %u", function, nargs - 1))
5055 173 : inform_declaration (fundecl, function);
5056 188 : }
5057 :
5058 49893700 : return error_args ? -1 : (int) parmnum;
5059 : }
5060 :
5061 : /* This is the entry point used by the parser to build unary operators
5062 : in the input. CODE, a tree_code, specifies the unary operator, and
5063 : ARG is the operand. For unary plus, the C parser currently uses
5064 : CONVERT_EXPR for code.
5065 :
5066 : LOC is the location to use for the tree generated.
5067 : */
5068 :
5069 : struct c_expr
5070 8163304 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5071 : {
5072 8163304 : struct c_expr result;
5073 :
5074 8163304 : result.original_code = code;
5075 8163304 : result.original_type = NULL;
5076 8163304 : result.m_decimal = 0;
5077 :
5078 8163304 : if (reject_gcc_builtin (arg.value))
5079 : {
5080 2 : result.value = error_mark_node;
5081 : }
5082 : else
5083 : {
5084 8163302 : result.value = build_unary_op (loc, code, arg.value, false);
5085 :
5086 8163302 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
5087 5 : overflow_warning (loc, result.value, arg.value);
5088 : }
5089 :
5090 : /* We are typically called when parsing a prefix token at LOC acting on
5091 : ARG. Reflect this by updating the source range of the result to
5092 : start at LOC and end at the end of ARG. */
5093 8163304 : set_c_expr_source_range (&result,
5094 : loc, arg.get_finish ());
5095 :
5096 8163304 : return result;
5097 : }
5098 :
5099 : /* Returns true if TYPE is a character type, *not* including wchar_t. */
5100 :
5101 : bool
5102 1282435 : char_type_p (tree type)
5103 : {
5104 1282435 : return (type == char_type_node
5105 991769 : || type == unsigned_char_type_node
5106 956612 : || type == signed_char_type_node
5107 955764 : || type == char16_type_node
5108 2018770 : || type == char32_type_node);
5109 : }
5110 :
5111 : /* This is the entry point used by the parser to build binary operators
5112 : in the input. CODE, a tree_code, specifies the binary operator, and
5113 : ARG1 and ARG2 are the operands. In addition to constructing the
5114 : expression, we check for operands that were written with other binary
5115 : operators in a way that is likely to confuse the user.
5116 :
5117 : LOCATION is the location of the binary operator. */
5118 :
5119 : struct c_expr
5120 9328095 : parser_build_binary_op (location_t location, enum tree_code code,
5121 : struct c_expr arg1, struct c_expr arg2)
5122 : {
5123 9328095 : struct c_expr result;
5124 9328095 : result.m_decimal = 0;
5125 :
5126 9328095 : enum tree_code code1 = arg1.original_code;
5127 9328095 : enum tree_code code2 = arg2.original_code;
5128 9328095 : tree type1 = (arg1.original_type
5129 9328095 : ? arg1.original_type
5130 5987245 : : TREE_TYPE (arg1.value));
5131 9328095 : tree type2 = (arg2.original_type
5132 9328095 : ? arg2.original_type
5133 6882891 : : TREE_TYPE (arg2.value));
5134 :
5135 9328095 : result.value = build_binary_op (location, code,
5136 : arg1.value, arg2.value, true);
5137 9328094 : result.original_code = code;
5138 9328094 : result.original_type = NULL;
5139 9328094 : result.m_decimal = 0;
5140 :
5141 9328094 : if (TREE_CODE (result.value) == ERROR_MARK)
5142 : {
5143 1297 : set_c_expr_source_range (&result,
5144 : arg1.get_start (),
5145 : arg2.get_finish ());
5146 1297 : return result;
5147 : }
5148 :
5149 9326797 : if (location != UNKNOWN_LOCATION)
5150 9326797 : protected_set_expr_location (result.value, location);
5151 :
5152 9326797 : set_c_expr_source_range (&result,
5153 : arg1.get_start (),
5154 : arg2.get_finish ());
5155 :
5156 : /* Check for cases such as x+y<<z which users are likely
5157 : to misinterpret. */
5158 9326797 : if (warn_parentheses)
5159 2004224 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5160 : arg2.value);
5161 :
5162 9326797 : if (warn_logical_op)
5163 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5164 : code1, arg1.value, code2, arg2.value);
5165 :
5166 9326797 : if (warn_tautological_compare)
5167 : {
5168 2002737 : tree lhs = arg1.value;
5169 2002737 : tree rhs = arg2.value;
5170 2002737 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5171 : {
5172 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5173 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5174 : lhs = NULL_TREE;
5175 : else
5176 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5177 : }
5178 2002737 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5179 : {
5180 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5181 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5182 : rhs = NULL_TREE;
5183 : else
5184 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5185 : }
5186 2002737 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5187 2002737 : warn_tautological_cmp (location, code, lhs, rhs);
5188 : }
5189 :
5190 9326797 : if (warn_logical_not_paren
5191 2002820 : && TREE_CODE_CLASS (code) == tcc_comparison
5192 405979 : && code1 == TRUTH_NOT_EXPR
5193 405979 : && code2 != TRUTH_NOT_EXPR
5194 : /* Avoid warning for !!x == y. */
5195 9326923 : && (TREE_CODE (arg1.value) != NE_EXPR
5196 19 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5197 : {
5198 : /* Avoid warning for !b == y where b has _Bool type. */
5199 107 : tree t = integer_zero_node;
5200 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5201 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5202 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5203 : {
5204 90 : t = TREE_OPERAND (arg1.value, 0);
5205 199 : do
5206 : {
5207 199 : if (TREE_TYPE (t) != integer_type_node)
5208 : break;
5209 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5210 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5211 90 : else if (CONVERT_EXPR_P (t))
5212 19 : t = TREE_OPERAND (t, 0);
5213 : else
5214 : break;
5215 : }
5216 : while (1);
5217 : }
5218 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5219 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5220 : }
5221 :
5222 : /* Warn about comparisons against string literals, with the exception
5223 : of testing for equality or inequality of a string literal with NULL. */
5224 9326797 : if (code == EQ_EXPR || code == NE_EXPR)
5225 : {
5226 1167678 : if ((code1 == STRING_CST
5227 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5228 1167689 : || (code2 == STRING_CST
5229 39 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5230 36 : warning_at (location, OPT_Waddress,
5231 : "comparison with string literal results in unspecified behavior");
5232 : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5233 1167678 : if (POINTER_TYPE_P (type1)
5234 90582 : && null_pointer_constant_p (arg2.value)
5235 1188742 : && char_type_p (type2))
5236 : {
5237 18 : auto_diagnostic_group d;
5238 18 : if (warning_at (location, OPT_Wpointer_compare,
5239 : "comparison between pointer and zero character "
5240 : "constant"))
5241 10 : inform (arg1.get_start (),
5242 : "did you mean to dereference the pointer?");
5243 18 : }
5244 1167660 : else if (POINTER_TYPE_P (type2)
5245 87890 : && null_pointer_constant_p (arg1.value)
5246 1167926 : && char_type_p (type1))
5247 : {
5248 10 : auto_diagnostic_group d;
5249 10 : if (warning_at (location, OPT_Wpointer_compare,
5250 : "comparison between pointer and zero character "
5251 : "constant"))
5252 10 : inform (arg2.get_start (),
5253 : "did you mean to dereference the pointer?");
5254 10 : }
5255 : }
5256 8159119 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5257 996605 : && (code1 == STRING_CST || code2 == STRING_CST))
5258 0 : warning_at (location, OPT_Waddress,
5259 : "comparison with string literal results in unspecified "
5260 : "behavior");
5261 :
5262 9326797 : if (warn_zero_as_null_pointer_constant
5263 24 : && c_inhibit_evaluation_warnings == 0
5264 24 : && TREE_CODE_CLASS (code) == tcc_comparison)
5265 : {
5266 24 : if ((TREE_CODE (type1) == POINTER_TYPE
5267 17 : || TREE_CODE (type1) == NULLPTR_TYPE)
5268 9 : && INTEGRAL_TYPE_P (type2)
5269 33 : && null_pointer_constant_p (arg2.value))
5270 9 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5271 : "zero as null pointer constant");
5272 :
5273 24 : if ((TREE_CODE (type2) == POINTER_TYPE
5274 15 : || TREE_CODE (type2) == NULLPTR_TYPE)
5275 11 : && INTEGRAL_TYPE_P (type1)
5276 35 : && null_pointer_constant_p (arg1.value))
5277 11 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5278 : "zero as null pointer constant");
5279 : }
5280 :
5281 9326797 : if (warn_array_compare
5282 2002575 : && TREE_CODE_CLASS (code) == tcc_comparison
5283 405764 : && TREE_CODE (type1) == ARRAY_TYPE
5284 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5285 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5286 :
5287 2180932 : if (TREE_OVERFLOW_P (result.value)
5288 299 : && !TREE_OVERFLOW_P (arg1.value)
5289 9327089 : && !TREE_OVERFLOW_P (arg2.value))
5290 249 : overflow_warning (location, result.value);
5291 :
5292 : /* Warn about comparisons of different enum types. */
5293 9326796 : if (warn_enum_compare
5294 2022513 : && TREE_CODE_CLASS (code) == tcc_comparison
5295 410189 : && TREE_CODE (type1) == ENUMERAL_TYPE
5296 7038 : && TREE_CODE (type2) == ENUMERAL_TYPE
5297 9333772 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5298 2 : warning_at (location, OPT_Wenum_compare,
5299 : "comparison between %qT and %qT",
5300 : type1, type2);
5301 :
5302 9326796 : if (warn_xor_used_as_pow
5303 9326796 : && code == BIT_XOR_EXPR
5304 77100 : && arg1.m_decimal
5305 730 : && arg2.m_decimal)
5306 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5307 : location,
5308 : arg2.get_location (), arg2.value);
5309 :
5310 : return result;
5311 : }
5312 :
5313 : /* Return a tree for the difference of pointers OP0 and OP1.
5314 : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5315 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5316 :
5317 : static tree
5318 3683 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5319 : {
5320 3683 : tree restype = ptrdiff_type_node;
5321 3683 : tree result, inttype;
5322 :
5323 3683 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5324 3683 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5325 3683 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5326 3683 : tree orig_op0 = op0;
5327 3683 : tree orig_op1 = op1;
5328 :
5329 : /* If the operands point into different address spaces, we need to
5330 : explicitly convert them to pointers into the common address space
5331 : before we can subtract the numerical address values. */
5332 3683 : if (as0 != as1)
5333 : {
5334 0 : addr_space_t as_common;
5335 0 : tree common_type;
5336 :
5337 : /* Determine the common superset address space. This is guaranteed
5338 : to exist because the caller verified that comp_target_types
5339 : returned non-zero. */
5340 0 : if (!addr_space_superset (as0, as1, &as_common))
5341 0 : gcc_unreachable ();
5342 :
5343 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5344 0 : op0 = convert (common_type, op0);
5345 0 : op1 = convert (common_type, op1);
5346 : }
5347 :
5348 : /* Determine integer type result of the subtraction. This will usually
5349 : be the same as the result type (ptrdiff_t), but may need to be a wider
5350 : type if pointers for the address space are wider than ptrdiff_t. */
5351 3683 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5352 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5353 : else
5354 : inttype = restype;
5355 :
5356 3683 : if (VOID_TYPE_P (target_type))
5357 146 : pedwarn (loc, OPT_Wpointer_arith,
5358 : "pointer of type %<void *%> used in subtraction");
5359 3683 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5360 4 : pedwarn (loc, OPT_Wpointer_arith,
5361 : "pointer to a function used in subtraction");
5362 :
5363 3683 : if (current_function_decl != NULL_TREE
5364 3683 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5365 : {
5366 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5367 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5368 :
5369 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5370 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5371 : }
5372 :
5373 : /* First do the subtraction, then build the divide operator
5374 : and only convert at the very end.
5375 : Do not do default conversions in case restype is a short type. */
5376 :
5377 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5378 : pointers. If some platform cannot provide that, or has a larger
5379 : ptrdiff_type to support differences larger than half the address
5380 : space, cast the pointers to some larger integer type and do the
5381 : computations in that type. */
5382 3683 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5383 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5384 : convert (inttype, op1), false);
5385 : else
5386 : {
5387 : /* Cast away qualifiers. */
5388 3683 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5389 3683 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5390 3683 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5391 : }
5392 :
5393 : /* This generates an error if op1 is pointer to incomplete type. */
5394 3683 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5395 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5396 3679 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5397 3679 : TREE_TYPE (TREE_TYPE (orig_op0))))
5398 7358 : verify_type_context (loc, TCTX_POINTER_ARITH,
5399 3679 : TREE_TYPE (TREE_TYPE (orig_op1)));
5400 :
5401 3683 : op1 = c_size_in_bytes (target_type);
5402 :
5403 3683 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5404 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5405 :
5406 : /* Divide by the size, in easiest possible way. */
5407 3683 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5408 : op0, convert (inttype, op1));
5409 :
5410 : /* Convert to final result type if necessary. */
5411 3683 : return convert (restype, result);
5412 : }
5413 :
5414 : /* Expand atomic compound assignments into an appropriate sequence as
5415 : specified by the C11 standard section 6.5.16.2.
5416 :
5417 : _Atomic T1 E1
5418 : T2 E2
5419 : E1 op= E2
5420 :
5421 : This sequence is used for all types for which these operations are
5422 : supported.
5423 :
5424 : In addition, built-in versions of the 'fe' prefixed routines may
5425 : need to be invoked for floating point (real, complex or vector) when
5426 : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5427 :
5428 : T1 newval;
5429 : T1 old;
5430 : T1 *addr
5431 : T2 val
5432 : fenv_t fenv
5433 :
5434 : addr = &E1;
5435 : val = (E2);
5436 : __atomic_load (addr, &old, SEQ_CST);
5437 : feholdexcept (&fenv);
5438 : loop:
5439 : newval = old op val;
5440 : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5441 : SEQ_CST))
5442 : goto done;
5443 : feclearexcept (FE_ALL_EXCEPT);
5444 : goto loop:
5445 : done:
5446 : feupdateenv (&fenv);
5447 :
5448 : The compiler will issue the __atomic_fetch_* built-in when possible,
5449 : otherwise it will generate the generic form of the atomic operations.
5450 : This requires temp(s) and has their address taken. The atomic processing
5451 : is smart enough to figure out when the size of an object can utilize
5452 : a lock-free version, and convert the built-in call to the appropriate
5453 : lock-free routine. The optimizers will then dispose of any temps that
5454 : are no longer required, and lock-free implementations are utilized as
5455 : long as there is target support for the required size.
5456 :
5457 : If the operator is NOP_EXPR, then this is a simple assignment, and
5458 : an __atomic_store is issued to perform the assignment rather than
5459 : the above loop. */
5460 :
5461 : /* Build an atomic assignment at LOC, expanding into the proper
5462 : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5463 : the result of the operation, unless RETURN_OLD_P, in which case
5464 : return the old value of LHS (this is only for postincrement and
5465 : postdecrement). */
5466 :
5467 : static tree
5468 30433 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5469 : tree rhs, bool return_old_p)
5470 : {
5471 30433 : tree fndecl, func_call;
5472 30433 : vec<tree, va_gc> *params;
5473 30433 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5474 30433 : tree old, old_addr;
5475 30433 : tree compound_stmt = NULL_TREE;
5476 30433 : tree stmt, goto_stmt;
5477 30433 : tree loop_label, loop_decl, done_label, done_decl;
5478 :
5479 30433 : tree lhs_type = TREE_TYPE (lhs);
5480 30433 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5481 30433 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5482 30433 : tree rhs_semantic_type = TREE_TYPE (rhs);
5483 30433 : tree nonatomic_rhs_semantic_type;
5484 30433 : tree rhs_type;
5485 :
5486 30433 : gcc_assert (TYPE_ATOMIC (lhs_type));
5487 :
5488 30433 : if (return_old_p)
5489 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5490 :
5491 : /* Allocate enough vector items for a compare_exchange. */
5492 30433 : vec_alloc (params, 6);
5493 :
5494 : /* Create a compound statement to hold the sequence of statements
5495 : with a loop. */
5496 30433 : if (modifycode != NOP_EXPR)
5497 : {
5498 19954 : compound_stmt = c_begin_compound_stmt (false);
5499 :
5500 : /* For consistency with build_modify_expr on non-_Atomic,
5501 : mark the lhs as read. Also, it would be very hard to match
5502 : such expressions in mark_exp_read. */
5503 19954 : mark_exp_read (lhs);
5504 : }
5505 :
5506 : /* Remove any excess precision (which is only present here in the
5507 : case of compound assignments). */
5508 30433 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5509 : {
5510 0 : gcc_assert (modifycode != NOP_EXPR);
5511 0 : rhs = TREE_OPERAND (rhs, 0);
5512 : }
5513 30433 : rhs_type = TREE_TYPE (rhs);
5514 :
5515 : /* Fold the RHS if it hasn't already been folded. */
5516 30433 : if (modifycode != NOP_EXPR)
5517 19954 : rhs = c_fully_fold (rhs, false, NULL);
5518 :
5519 : /* Remove the qualifiers for the rest of the expressions and create
5520 : the VAL temp variable to hold the RHS. */
5521 30433 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5522 30433 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5523 30433 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5524 : TYPE_UNQUALIFIED);
5525 30433 : val = create_tmp_var_raw (nonatomic_rhs_type);
5526 30433 : TREE_ADDRESSABLE (val) = 1;
5527 30433 : suppress_warning (val);
5528 30433 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5529 : NULL_TREE);
5530 30433 : TREE_SIDE_EFFECTS (rhs) = 1;
5531 30433 : SET_EXPR_LOCATION (rhs, loc);
5532 30433 : if (modifycode != NOP_EXPR)
5533 19954 : add_stmt (rhs);
5534 :
5535 : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5536 : an atomic_store. */
5537 30433 : if (modifycode == NOP_EXPR)
5538 : {
5539 10479 : compound_stmt = rhs;
5540 : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5541 10479 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5542 10479 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5543 10479 : params->quick_push (lhs_addr);
5544 10479 : params->quick_push (rhs);
5545 10479 : params->quick_push (seq_cst);
5546 10479 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5547 :
5548 10479 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5549 : compound_stmt, func_call);
5550 :
5551 : /* VAL is the value which was stored, return a COMPOUND_STMT of
5552 : the statement and that value. */
5553 10479 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5554 : }
5555 :
5556 : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5557 : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5558 : isn't applicable for such builtins. ??? Do we want to handle enums? */
5559 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5560 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5561 : {
5562 11713 : built_in_function fncode;
5563 11713 : switch (modifycode)
5564 : {
5565 3075 : case PLUS_EXPR:
5566 3075 : case POINTER_PLUS_EXPR:
5567 2124 : fncode = (return_old_p
5568 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5569 : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5570 : break;
5571 2889 : case MINUS_EXPR:
5572 2094 : fncode = (return_old_p
5573 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5574 : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5575 : break;
5576 609 : case BIT_AND_EXPR:
5577 609 : fncode = (return_old_p
5578 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5579 : : BUILT_IN_ATOMIC_AND_FETCH_N);
5580 : break;
5581 609 : case BIT_IOR_EXPR:
5582 609 : fncode = (return_old_p
5583 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5584 : : BUILT_IN_ATOMIC_OR_FETCH_N);
5585 : break;
5586 609 : case BIT_XOR_EXPR:
5587 609 : fncode = (return_old_p
5588 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5589 : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5590 : break;
5591 3922 : default:
5592 3922 : goto cas_loop;
5593 : }
5594 :
5595 : /* We can only use "_1" through "_16" variants of the atomic fetch
5596 : built-ins. */
5597 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5598 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5599 0 : goto cas_loop;
5600 :
5601 : /* If this is a pointer type, we need to multiply by the size of
5602 : the pointer target type. */
5603 7791 : if (POINTER_TYPE_P (lhs_type))
5604 : {
5605 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5606 : /* ??? This would introduce -Wdiscarded-qualifiers
5607 : warning: __atomic_fetch_* expect volatile void *
5608 : type as the first argument. (Assignments between
5609 : atomic and non-atomic objects are OK.) */
5610 1018 : || TYPE_RESTRICT (lhs_type))
5611 17 : goto cas_loop;
5612 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5613 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5614 : convert (ptrdiff_type_node, rhs),
5615 : convert (ptrdiff_type_node, sz));
5616 : }
5617 :
5618 : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5619 : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5620 7774 : fndecl = builtin_decl_explicit (fncode);
5621 7774 : params->quick_push (lhs_addr);
5622 7774 : params->quick_push (rhs);
5623 7774 : params->quick_push (seq_cst);
5624 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5625 :
5626 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5627 7774 : TREE_ADDRESSABLE (newval) = 1;
5628 7774 : suppress_warning (newval);
5629 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5630 : NULL_TREE, NULL_TREE);
5631 7774 : SET_EXPR_LOCATION (rhs, loc);
5632 7774 : add_stmt (rhs);
5633 :
5634 : /* Finish the compound statement. */
5635 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5636 :
5637 : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5638 : the statement and that value. */
5639 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5640 : }
5641 :
5642 8241 : cas_loop:
5643 : /* Create the variables and labels required for the op= form. */
5644 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5645 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5646 12180 : TREE_ADDRESSABLE (old) = 1;
5647 12180 : suppress_warning (old);
5648 :
5649 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5650 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5651 12180 : TREE_ADDRESSABLE (newval) = 1;
5652 12180 : suppress_warning (newval);
5653 :
5654 12180 : loop_decl = create_artificial_label (loc);
5655 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5656 :
5657 12180 : done_decl = create_artificial_label (loc);
5658 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5659 :
5660 : /* __atomic_load (addr, &old, SEQ_CST). */
5661 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5662 12180 : params->quick_push (lhs_addr);
5663 12180 : params->quick_push (old_addr);
5664 12180 : params->quick_push (seq_cst);
5665 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5666 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5667 : NULL_TREE);
5668 12180 : add_stmt (old);
5669 12180 : params->truncate (0);
5670 :
5671 : /* Create the expressions for floating-point environment
5672 : manipulation, if required. */
5673 12180 : bool need_fenv = (flag_trapping_math
5674 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5675 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5676 12180 : if (need_fenv)
5677 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5678 :
5679 12180 : if (hold_call)
5680 7054 : add_stmt (hold_call);
5681 :
5682 : /* loop: */
5683 12180 : add_stmt (loop_label);
5684 :
5685 : /* newval = old + val; */
5686 12180 : if (rhs_type != rhs_semantic_type)
5687 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5688 12180 : rhs = build_binary_op (loc, modifycode,
5689 : convert_lvalue_to_rvalue (loc, old, true, true),
5690 : val, true);
5691 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5692 : {
5693 0 : tree eptype = TREE_TYPE (rhs);
5694 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5695 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5696 : }
5697 : else
5698 12180 : rhs = c_fully_fold (rhs, false, NULL);
5699 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5700 : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5701 : NULL_TREE, 0);
5702 : /* The temporary variable for NEWVAL is only gimplified correctly (in
5703 : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5704 : subsequent ICEs in nested function processing after an error, ensure such
5705 : a TARGET_EXPR is built even after an error. */
5706 12180 : if (rhs == error_mark_node)
5707 10 : rhs = old;
5708 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5709 : NULL_TREE);
5710 12180 : SET_EXPR_LOCATION (rhs, loc);
5711 12180 : add_stmt (rhs);
5712 :
5713 : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5714 : goto done; */
5715 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5716 12180 : params->quick_push (lhs_addr);
5717 12180 : params->quick_push (old_addr);
5718 12180 : params->quick_push (newval_addr);
5719 12180 : params->quick_push (integer_zero_node);
5720 12180 : params->quick_push (seq_cst);
5721 12180 : params->quick_push (seq_cst);
5722 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5723 :
5724 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5725 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5726 :
5727 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5728 12180 : SET_EXPR_LOCATION (stmt, loc);
5729 12180 : add_stmt (stmt);
5730 :
5731 12180 : if (clear_call)
5732 7054 : add_stmt (clear_call);
5733 :
5734 : /* goto loop; */
5735 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5736 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5737 12180 : add_stmt (goto_stmt);
5738 :
5739 : /* done: */
5740 12180 : add_stmt (done_label);
5741 :
5742 12180 : if (update_call)
5743 7054 : add_stmt (update_call);
5744 :
5745 : /* Finish the compound statement. */
5746 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5747 :
5748 : /* NEWVAL is the value that was successfully stored, return a
5749 : COMPOUND_EXPR of the statement and the appropriate value. */
5750 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5751 12180 : return_old_p ? old : newval);
5752 : }
5753 :
5754 : /* Construct and perhaps optimize a tree representation
5755 : for a unary operation. CODE, a tree_code, specifies the operation
5756 : and XARG is the operand.
5757 : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5758 : promotions (such as from short to int).
5759 : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5760 : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5761 : to pointers in C99.
5762 :
5763 : LOCATION is the location of the operator. */
5764 :
5765 : tree
5766 60515728 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5767 : bool noconvert)
5768 : {
5769 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5770 60515728 : tree arg = xarg;
5771 60515728 : tree argtype = NULL_TREE;
5772 60515728 : enum tree_code typecode;
5773 60515728 : tree val;
5774 60515728 : tree ret = error_mark_node;
5775 60515728 : tree eptype = NULL_TREE;
5776 60515728 : const char *invalid_op_diag;
5777 60515728 : bool int_operands;
5778 :
5779 60515728 : int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5780 6461805 : if (int_operands)
5781 6461805 : arg = remove_c_maybe_const_expr (arg);
5782 :
5783 60515728 : if (code != ADDR_EXPR)
5784 8536219 : arg = require_complete_type (location, arg);
5785 :
5786 60515728 : typecode = TREE_CODE (TREE_TYPE (arg));
5787 60515728 : if (typecode == ERROR_MARK)
5788 74 : return error_mark_node;
5789 60515654 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5790 32239 : typecode = INTEGER_TYPE;
5791 :
5792 121031308 : if ((invalid_op_diag
5793 60515654 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5794 : {
5795 0 : error_at (location, invalid_op_diag);
5796 0 : return error_mark_node;
5797 : }
5798 :
5799 60515654 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5800 : {
5801 245 : eptype = TREE_TYPE (arg);
5802 245 : arg = TREE_OPERAND (arg, 0);
5803 : }
5804 :
5805 60515654 : switch (code)
5806 : {
5807 28749 : case CONVERT_EXPR:
5808 : /* This is used for unary plus, because a CONVERT_EXPR
5809 : is enough to prevent anybody from looking inside for
5810 : associativity, but won't generate any code. */
5811 28754 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5812 8 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5813 5 : || typecode == BITINT_TYPE
5814 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5815 : {
5816 1 : error_at (location, "wrong type argument to unary plus");
5817 1 : return error_mark_node;
5818 : }
5819 28748 : else if (!noconvert)
5820 28748 : arg = default_conversion (arg);
5821 28748 : arg = non_lvalue_loc (location, arg);
5822 28748 : break;
5823 :
5824 6697999 : case NEGATE_EXPR:
5825 7145403 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5826 484073 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5827 452806 : || typecode == BITINT_TYPE
5828 447404 : || gnu_vector_type_p (TREE_TYPE (arg))))
5829 : {
5830 1 : error_at (location, "wrong type argument to unary minus");
5831 1 : return error_mark_node;
5832 : }
5833 6697998 : else if (!noconvert)
5834 6697992 : arg = default_conversion (arg);
5835 : break;
5836 :
5837 295301 : case BIT_NOT_EXPR:
5838 : /* ~ works on integer types and non float vectors. */
5839 295301 : if (typecode == INTEGER_TYPE
5840 295301 : || typecode == BITINT_TYPE
5841 295301 : || (gnu_vector_type_p (TREE_TYPE (arg))
5842 2057 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5843 : {
5844 : tree e = arg;
5845 :
5846 : /* Warn if the expression has boolean value. */
5847 294702 : while (TREE_CODE (e) == COMPOUND_EXPR)
5848 11 : e = TREE_OPERAND (e, 1);
5849 :
5850 589368 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5851 589368 : || truth_value_p (TREE_CODE (e))))
5852 : {
5853 146 : auto_diagnostic_group d;
5854 146 : if (warning_at (location, OPT_Wbool_operation,
5855 : "%<~%> on a boolean expression"))
5856 : {
5857 12 : gcc_rich_location richloc (location);
5858 12 : richloc.add_fixit_insert_before (location, "!");
5859 12 : inform (&richloc, "did you mean to use logical not?");
5860 12 : }
5861 146 : }
5862 294691 : if (!noconvert)
5863 294688 : arg = default_conversion (arg);
5864 : }
5865 610 : else if (typecode == COMPLEX_TYPE)
5866 : {
5867 605 : code = CONJ_EXPR;
5868 605 : pedwarn (location, OPT_Wpedantic,
5869 : "ISO C does not support %<~%> for complex conjugation");
5870 605 : if (!noconvert)
5871 605 : arg = default_conversion (arg);
5872 : }
5873 : else
5874 : {
5875 5 : error_at (location, "wrong type argument to bit-complement");
5876 5 : return error_mark_node;
5877 : }
5878 : break;
5879 :
5880 3 : case ABS_EXPR:
5881 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5882 : {
5883 0 : error_at (location, "wrong type argument to abs");
5884 0 : return error_mark_node;
5885 : }
5886 3 : else if (!noconvert)
5887 0 : arg = default_conversion (arg);
5888 : break;
5889 :
5890 7 : case ABSU_EXPR:
5891 7 : if (!(typecode == INTEGER_TYPE))
5892 : {
5893 0 : error_at (location, "wrong type argument to absu");
5894 0 : return error_mark_node;
5895 : }
5896 7 : else if (!noconvert)
5897 0 : arg = default_conversion (arg);
5898 : break;
5899 :
5900 0 : case CONJ_EXPR:
5901 : /* Conjugating a real value is a no-op, but allow it anyway. */
5902 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5903 : || typecode == COMPLEX_TYPE))
5904 : {
5905 0 : error_at (location, "wrong type argument to conjugation");
5906 0 : return error_mark_node;
5907 : }
5908 0 : else if (!noconvert)
5909 0 : arg = default_conversion (arg);
5910 : break;
5911 :
5912 375908 : case TRUTH_NOT_EXPR:
5913 375908 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5914 7057 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5915 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5916 18 : && typecode != BITINT_TYPE)
5917 : {
5918 10 : error_at (location,
5919 : "wrong type argument to unary exclamation mark");
5920 10 : return error_mark_node;
5921 : }
5922 375898 : if (int_operands)
5923 : {
5924 139857 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5925 139857 : arg = remove_c_maybe_const_expr (arg);
5926 : }
5927 : else
5928 236041 : arg = c_objc_common_truthvalue_conversion (location, arg);
5929 375898 : ret = invert_truthvalue_loc (location, arg);
5930 : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5931 375898 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5932 235648 : location = EXPR_LOCATION (ret);
5933 375898 : goto return_build_unary_op;
5934 :
5935 203100 : case REALPART_EXPR:
5936 203100 : case IMAGPART_EXPR:
5937 203100 : ret = build_real_imag_expr (location, code, arg);
5938 203100 : if (ret == error_mark_node)
5939 : return error_mark_node;
5940 203092 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5941 2 : eptype = TREE_TYPE (eptype);
5942 203092 : goto return_build_unary_op;
5943 :
5944 935109 : case PREINCREMENT_EXPR:
5945 935109 : case POSTINCREMENT_EXPR:
5946 935109 : case PREDECREMENT_EXPR:
5947 935109 : case POSTDECREMENT_EXPR:
5948 :
5949 935109 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5950 : {
5951 2 : tree inner = build_unary_op (location, code,
5952 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
5953 : noconvert);
5954 2 : if (inner == error_mark_node)
5955 : return error_mark_node;
5956 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5957 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
5958 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5959 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
5960 2 : goto return_build_unary_op;
5961 : }
5962 :
5963 : /* Complain about anything that is not a true lvalue. In
5964 : Objective-C, skip this check for property_refs. */
5965 935107 : if (!objc_is_property_ref (arg)
5966 1870214 : && !lvalue_or_else (location,
5967 935107 : arg, ((code == PREINCREMENT_EXPR
5968 935107 : || code == POSTINCREMENT_EXPR)
5969 : ? lv_increment
5970 : : lv_decrement)))
5971 22 : return error_mark_node;
5972 :
5973 935085 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
5974 : {
5975 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5976 2 : warning_at (location, OPT_Wc___compat,
5977 : "increment of enumeration value is invalid in C++");
5978 : else
5979 2 : warning_at (location, OPT_Wc___compat,
5980 : "decrement of enumeration value is invalid in C++");
5981 : }
5982 :
5983 935085 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5984 : {
5985 664 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5986 328 : warning_at (location, OPT_Wbool_operation,
5987 : "increment of a boolean expression");
5988 : else
5989 336 : warning_at (location, OPT_Wbool_operation,
5990 : "decrement of a boolean expression");
5991 : }
5992 :
5993 : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
5994 935085 : arg = c_fully_fold (arg, false, NULL, true);
5995 :
5996 935085 : bool atomic_op;
5997 935085 : atomic_op = really_atomic_lvalue (arg);
5998 :
5999 : /* Increment or decrement the real part of the value,
6000 : and don't change the imaginary part. */
6001 935085 : if (typecode == COMPLEX_TYPE)
6002 : {
6003 57 : tree real, imag;
6004 :
6005 57 : pedwarn_c23 (location, OPT_Wpedantic,
6006 : "ISO C does not support %<++%> and %<--%> on complex "
6007 : "types before C2Y");
6008 :
6009 57 : if (!atomic_op)
6010 : {
6011 53 : arg = stabilize_reference (arg);
6012 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
6013 : true);
6014 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
6015 : true);
6016 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
6017 53 : if (real == error_mark_node || imag == error_mark_node)
6018 : return error_mark_node;
6019 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6020 : real, imag);
6021 53 : goto return_build_unary_op;
6022 : }
6023 : }
6024 :
6025 : /* Report invalid types. */
6026 :
6027 935032 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
6028 704971 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
6029 208 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
6030 935088 : && !gnu_vector_type_p (TREE_TYPE (arg)))
6031 : {
6032 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6033 3 : error_at (location, "wrong type argument to increment");
6034 : else
6035 2 : error_at (location, "wrong type argument to decrement");
6036 :
6037 5 : return error_mark_node;
6038 : }
6039 :
6040 935027 : {
6041 935027 : tree inc;
6042 :
6043 935027 : argtype = TREE_TYPE (arg);
6044 :
6045 : /* Compute the increment. */
6046 :
6047 935027 : if (typecode == POINTER_TYPE)
6048 : {
6049 : /* If pointer target is an incomplete type,
6050 : we just cannot know how to do the arithmetic. */
6051 230061 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
6052 : {
6053 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6054 17 : error_at (location,
6055 : "increment of pointer to an incomplete type %qT",
6056 17 : TREE_TYPE (argtype));
6057 : else
6058 10 : error_at (location,
6059 : "decrement of pointer to an incomplete type %qT",
6060 10 : TREE_TYPE (argtype));
6061 : }
6062 230034 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
6063 230034 : || VOID_TYPE_P (TREE_TYPE (argtype)))
6064 : {
6065 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6066 6 : pedwarn (location, OPT_Wpointer_arith,
6067 : "wrong type argument to increment");
6068 : else
6069 4 : pedwarn (location, OPT_Wpointer_arith,
6070 : "wrong type argument to decrement");
6071 : }
6072 : else
6073 460048 : verify_type_context (location, TCTX_POINTER_ARITH,
6074 230024 : TREE_TYPE (argtype));
6075 :
6076 230061 : inc = c_size_in_bytes (TREE_TYPE (argtype));
6077 230061 : inc = convert_to_ptrofftype_loc (location, inc);
6078 : }
6079 704966 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
6080 : {
6081 : /* For signed fract types, we invert ++ to -- or
6082 : -- to ++, and change inc from 1 to -1, because
6083 : it is not possible to represent 1 in signed fract constants.
6084 : For unsigned fract types, the result always overflows and
6085 : we get an undefined (original) or the maximum value. */
6086 0 : if (code == PREINCREMENT_EXPR)
6087 : code = PREDECREMENT_EXPR;
6088 : else if (code == PREDECREMENT_EXPR)
6089 : code = PREINCREMENT_EXPR;
6090 : else if (code == POSTINCREMENT_EXPR)
6091 : code = POSTDECREMENT_EXPR;
6092 : else /* code == POSTDECREMENT_EXPR */
6093 0 : code = POSTINCREMENT_EXPR;
6094 :
6095 0 : inc = integer_minus_one_node;
6096 0 : inc = convert (argtype, inc);
6097 : }
6098 : else
6099 : {
6100 704915 : inc = VECTOR_TYPE_P (argtype)
6101 704966 : ? build_one_cst (argtype)
6102 : : integer_one_node;
6103 704966 : inc = convert (argtype, inc);
6104 : }
6105 :
6106 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6107 : need to ask Objective-C to build the increment or decrement
6108 : expression for it. */
6109 935027 : if (objc_is_property_ref (arg))
6110 0 : return objc_build_incr_expr_for_property_ref (location, code,
6111 20 : arg, inc);
6112 :
6113 : /* Report a read-only lvalue. */
6114 935027 : if (TYPE_READONLY (argtype))
6115 : {
6116 20 : readonly_error (location, arg,
6117 20 : ((code == PREINCREMENT_EXPR
6118 20 : || code == POSTINCREMENT_EXPR)
6119 : ? lv_increment : lv_decrement));
6120 20 : return error_mark_node;
6121 : }
6122 935007 : else if (TREE_READONLY (arg))
6123 4 : readonly_warning (arg,
6124 4 : ((code == PREINCREMENT_EXPR
6125 4 : || code == POSTINCREMENT_EXPR)
6126 : ? lv_increment : lv_decrement));
6127 :
6128 : /* If the argument is atomic, use the special code sequences for
6129 : atomic compound assignment. */
6130 935007 : if (atomic_op)
6131 : {
6132 4469 : arg = stabilize_reference (arg);
6133 8938 : ret = build_atomic_assign (location, arg,
6134 4469 : ((code == PREINCREMENT_EXPR
6135 4469 : || code == POSTINCREMENT_EXPR)
6136 : ? PLUS_EXPR
6137 : : MINUS_EXPR),
6138 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
6139 : ? inc
6140 : : integer_one_node),
6141 : (code == POSTINCREMENT_EXPR
6142 4469 : || code == POSTDECREMENT_EXPR));
6143 935007 : goto return_build_unary_op;
6144 : }
6145 :
6146 930538 : tree true_res;
6147 930538 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6148 : {
6149 364 : tree larg = stabilize_reference (arg);
6150 364 : tree sarg = save_expr (larg);
6151 364 : switch (code)
6152 : {
6153 91 : case PREINCREMENT_EXPR:
6154 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6155 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6156 91 : break;
6157 91 : case POSTINCREMENT_EXPR:
6158 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6159 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6160 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6161 91 : break;
6162 91 : case PREDECREMENT_EXPR:
6163 91 : {
6164 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6165 : true, true);
6166 91 : rarg = invert_truthvalue_loc (location, rarg);
6167 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6168 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6169 : }
6170 91 : break;
6171 91 : case POSTDECREMENT_EXPR:
6172 91 : {
6173 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6174 : true, true);
6175 91 : rarg = invert_truthvalue_loc (location, rarg);
6176 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6177 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6178 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6179 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6180 : }
6181 91 : break;
6182 : default:
6183 : gcc_unreachable ();
6184 : }
6185 364 : TREE_SIDE_EFFECTS (val) = 1;
6186 : }
6187 930174 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6188 64 : val = boolean_increment (code, arg);
6189 : else
6190 930110 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6191 930538 : TREE_SIDE_EFFECTS (val) = 1;
6192 930538 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6193 3939 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6194 : TYPE_UNQUALIFIED);
6195 930538 : ret = val;
6196 930538 : goto return_build_unary_op;
6197 : }
6198 :
6199 51979468 : case ADDR_EXPR:
6200 : /* Note that this operation never does default_conversion. */
6201 :
6202 : /* The operand of unary '&' must be an lvalue (which excludes
6203 : expressions of type void), or, in C99, the result of a [] or
6204 : unary '*' operator. */
6205 51979468 : if (VOID_TYPE_P (TREE_TYPE (arg))
6206 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6207 51979487 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6208 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6209 :
6210 : /* Let &* cancel out to simplify resulting code. */
6211 51979468 : if (INDIRECT_REF_P (arg))
6212 : {
6213 : /* Don't let this be an lvalue. */
6214 22249 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6215 16778 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6216 5471 : ret = TREE_OPERAND (arg, 0);
6217 5471 : goto return_build_unary_op;
6218 : }
6219 :
6220 : /* Anything not already handled and not a true memory reference
6221 : or a non-lvalue array is an error. */
6222 51957219 : if (typecode != FUNCTION_TYPE && !noconvert
6223 51957219 : && !lvalue_or_else (location, arg, lv_addressof))
6224 17 : return error_mark_node;
6225 :
6226 : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6227 : folding later. */
6228 51957202 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6229 : {
6230 14 : tree inner = build_unary_op (location, code,
6231 14 : C_MAYBE_CONST_EXPR_EXPR (arg),
6232 : noconvert);
6233 28 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6234 14 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6235 14 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6236 14 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6237 14 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6238 14 : goto return_build_unary_op;
6239 : }
6240 :
6241 : /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to
6242 : .ACCESS_WITH_SIZE. */
6243 51957188 : if (is_access_with_size_p (arg))
6244 0 : arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0);
6245 :
6246 51957188 : argtype = TREE_TYPE (arg);
6247 :
6248 : /* If the lvalue is const or volatile, merge that into the type
6249 : to which the address will point. This is only needed
6250 : for function types. */
6251 51957188 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6252 51470312 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6253 82048428 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6254 : {
6255 30011255 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
6256 30011255 : int quals = orig_quals;
6257 :
6258 30011255 : if (TREE_READONLY (arg))
6259 29637590 : quals |= TYPE_QUAL_CONST;
6260 30011255 : if (TREE_THIS_VOLATILE (arg))
6261 374552 : quals |= TYPE_QUAL_VOLATILE;
6262 :
6263 30011255 : argtype = c_build_qualified_type (argtype, quals);
6264 : }
6265 :
6266 51957188 : switch (TREE_CODE (arg))
6267 : {
6268 65575 : case COMPONENT_REF:
6269 65575 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6270 : {
6271 8 : error_at (location, "cannot take address of bit-field %qD",
6272 8 : TREE_OPERAND (arg, 1));
6273 8 : return error_mark_node;
6274 : }
6275 :
6276 : /* fall through */
6277 :
6278 139711 : case ARRAY_REF:
6279 139711 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6280 : {
6281 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6282 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6283 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6284 : {
6285 6 : error_at (location, "cannot take address of scalar with "
6286 : "reverse storage order");
6287 6 : return error_mark_node;
6288 : }
6289 :
6290 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6291 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6292 58 : warning_at (location, OPT_Wscalar_storage_order,
6293 : "address of array with reverse scalar storage "
6294 : "order requested");
6295 : }
6296 :
6297 51957174 : default:
6298 51957174 : break;
6299 : }
6300 :
6301 51957174 : if (!c_mark_addressable (arg))
6302 20 : return error_mark_node;
6303 :
6304 51957154 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6305 : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6306 :
6307 51957154 : argtype = c_build_pointer_type (argtype);
6308 :
6309 : /* ??? Cope with user tricks that amount to offsetof. Delete this
6310 : when we have proper support for integer constant expressions. */
6311 51957154 : val = get_base_address (arg);
6312 51957154 : if (val && INDIRECT_REF_P (val)
6313 51981167 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6314 : {
6315 540 : ret = fold_offsetof (arg, argtype);
6316 540 : goto return_build_unary_op;
6317 : }
6318 :
6319 51956614 : val = build1 (ADDR_EXPR, argtype, arg);
6320 :
6321 51956614 : ret = val;
6322 51956614 : goto return_build_unary_op;
6323 :
6324 10 : case PAREN_EXPR:
6325 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6326 10 : goto return_build_unary_op;
6327 :
6328 0 : default:
6329 0 : gcc_unreachable ();
6330 : }
6331 :
6332 7022052 : if (argtype == NULL_TREE)
6333 7022052 : argtype = TREE_TYPE (arg);
6334 7022052 : if (TREE_CODE (arg) == INTEGER_CST)
6335 6317605 : ret = (require_constant_value
6336 6317605 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6337 6299016 : : fold_build1_loc (location, code, argtype, arg));
6338 : else
6339 704447 : ret = build1 (code, argtype, arg);
6340 60498753 : return_build_unary_op:
6341 60498753 : gcc_assert (ret != error_mark_node);
6342 6462281 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6343 66961025 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6344 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6345 60498191 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6346 74 : ret = note_integer_operands (ret);
6347 60498753 : if (eptype)
6348 245 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6349 60498753 : protected_set_expr_location (ret, location);
6350 60498753 : return ret;
6351 : }
6352 :
6353 : /* Return nonzero if REF is an lvalue valid for this language.
6354 : Lvalues can be assigned, unless their type has TYPE_READONLY.
6355 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6356 :
6357 : bool
6358 178211820 : lvalue_p (const_tree ref)
6359 : {
6360 179705520 : const enum tree_code code = TREE_CODE (ref);
6361 :
6362 179705520 : switch (code)
6363 : {
6364 1468891 : case REALPART_EXPR:
6365 1468891 : case IMAGPART_EXPR:
6366 1468891 : case COMPONENT_REF:
6367 1468891 : return lvalue_p (TREE_OPERAND (ref, 0));
6368 :
6369 24809 : case C_MAYBE_CONST_EXPR:
6370 24809 : return lvalue_p (TREE_OPERAND (ref, 1));
6371 :
6372 : case COMPOUND_LITERAL_EXPR:
6373 : case STRING_CST:
6374 : return true;
6375 :
6376 25466526 : case MEM_REF:
6377 25466526 : case TARGET_MEM_REF:
6378 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6379 : here as well. */
6380 25466526 : case INDIRECT_REF:
6381 25466526 : case ARRAY_REF:
6382 25466526 : case VAR_DECL:
6383 25466526 : case PARM_DECL:
6384 25466526 : case RESULT_DECL:
6385 25466526 : case ERROR_MARK:
6386 25466526 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6387 25466526 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6388 :
6389 6 : case BIND_EXPR:
6390 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6391 :
6392 7075188 : case CALL_EXPR:
6393 7075188 : return is_access_with_size_p (ref);
6394 :
6395 : default:
6396 : return false;
6397 : }
6398 : }
6399 :
6400 : /* Give a warning for storing in something that is read-only in GCC
6401 : terms but not const in ISO C terms. */
6402 :
6403 : static void
6404 5 : readonly_warning (tree arg, enum lvalue_use use)
6405 : {
6406 5 : switch (use)
6407 : {
6408 1 : case lv_assign:
6409 1 : warning (0, "assignment of read-only location %qE", arg);
6410 1 : break;
6411 2 : case lv_increment:
6412 2 : warning (0, "increment of read-only location %qE", arg);
6413 2 : break;
6414 2 : case lv_decrement:
6415 2 : warning (0, "decrement of read-only location %qE", arg);
6416 2 : break;
6417 0 : default:
6418 0 : gcc_unreachable ();
6419 : }
6420 5 : return;
6421 : }
6422 :
6423 :
6424 : /* Return nonzero if REF is an lvalue valid for this language;
6425 : otherwise, print an error message and return zero. USE says
6426 : how the lvalue is being used and so selects the error message.
6427 : LOCATION is the location at which any error should be reported. */
6428 :
6429 : static int
6430 4717195 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6431 : {
6432 4717195 : int win = lvalue_p (ref);
6433 :
6434 4717195 : if (!win)
6435 75 : lvalue_error (loc, use);
6436 :
6437 4717195 : return win;
6438 : }
6439 :
6440 : /* Mark EXP saying that we need to be able to take the
6441 : address of it; it should not be allocated in a register.
6442 : Returns true if successful. ARRAY_REF_P is true if this
6443 : is for ARRAY_REF construction - in that case we don't want
6444 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6445 : it is fine to use ARRAY_REFs for vector subscripts on vector
6446 : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6447 : than producing an error for taking the address of a register. */
6448 :
6449 : bool
6450 52501830 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6451 : {
6452 52501830 : tree x = exp;
6453 :
6454 53082396 : while (1)
6455 53082396 : switch (TREE_CODE (x))
6456 : {
6457 10029 : case VIEW_CONVERT_EXPR:
6458 10029 : if (array_ref_p
6459 9919 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6460 19948 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6461 : return true;
6462 110 : x = TREE_OPERAND (x, 0);
6463 110 : break;
6464 :
6465 396358 : case COMPONENT_REF:
6466 396358 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6467 : {
6468 6 : error ("cannot take address of bit-field %qD",
6469 3 : TREE_OPERAND (x, 1));
6470 3 : return false;
6471 : }
6472 : /* FALLTHRU */
6473 580456 : case ADDR_EXPR:
6474 580456 : case ARRAY_REF:
6475 580456 : case REALPART_EXPR:
6476 580456 : case IMAGPART_EXPR:
6477 580456 : x = TREE_OPERAND (x, 0);
6478 580456 : break;
6479 :
6480 587 : case COMPOUND_LITERAL_EXPR:
6481 587 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6482 : {
6483 16 : if (override_register)
6484 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6485 : else
6486 : {
6487 4 : error ("address of register compound literal requested");
6488 4 : return false;
6489 : }
6490 : }
6491 583 : TREE_ADDRESSABLE (x) = 1;
6492 583 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6493 583 : return true;
6494 :
6495 0 : case CONSTRUCTOR:
6496 0 : TREE_ADDRESSABLE (x) = 1;
6497 0 : return true;
6498 :
6499 1321631 : case VAR_DECL:
6500 1321631 : case CONST_DECL:
6501 1321631 : case PARM_DECL:
6502 1321631 : case RESULT_DECL:
6503 1321631 : if (C_DECL_REGISTER (x)
6504 1321631 : && DECL_NONLOCAL (x))
6505 : {
6506 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6507 : {
6508 0 : error
6509 0 : ("global register variable %qD used in nested function", x);
6510 0 : return false;
6511 : }
6512 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6513 : }
6514 1321631 : else if (C_DECL_REGISTER (x))
6515 : {
6516 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6517 4 : error ("address of global register variable %qD requested", x);
6518 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6519 : {
6520 54 : DECL_REGISTER (x) = 0;
6521 54 : TREE_ADDRESSABLE (x) = 1;
6522 54 : mark_decl_used (x, true);
6523 54 : return true;
6524 : }
6525 : else
6526 15 : error ("address of register variable %qD requested", x);
6527 19 : return false;
6528 : }
6529 :
6530 : /* FALLTHRU */
6531 51929213 : case FUNCTION_DECL:
6532 51929213 : TREE_ADDRESSABLE (x) = 1;
6533 51929213 : mark_decl_used (x, true);
6534 : /* FALLTHRU */
6535 : default:
6536 : return true;
6537 : }
6538 : }
6539 :
6540 : /* Convert EXPR to TYPE, warning about conversion problems with
6541 : constants. SEMANTIC_TYPE is the type this conversion would use
6542 : without excess precision. If SEMANTIC_TYPE is NULL, this function
6543 : is equivalent to convert_and_check. This function is a wrapper that
6544 : handles conversions that may be different than
6545 : the usual ones because of excess precision. */
6546 :
6547 : static tree
6548 23613062 : ep_convert_and_check (location_t loc, tree type, tree expr,
6549 : tree semantic_type)
6550 : {
6551 23613062 : if (TREE_TYPE (expr) == type)
6552 : return expr;
6553 :
6554 : /* For C11, integer conversions may have results with excess
6555 : precision. */
6556 2059651 : if (flag_isoc11 || !semantic_type)
6557 2059647 : return convert_and_check (loc, type, expr);
6558 :
6559 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6560 4 : && TREE_TYPE (expr) != semantic_type)
6561 : {
6562 : /* For integers, we need to check the real conversion, not
6563 : the conversion to the excess precision type. */
6564 4 : expr = convert_and_check (loc, semantic_type, expr);
6565 : }
6566 : /* Result type is the excess precision type, which should be
6567 : large enough, so do not check. */
6568 4 : return convert (type, expr);
6569 : }
6570 :
6571 : /* If EXPR refers to a built-in declared without a prototype returns
6572 : the actual type of the built-in and, if non-null, set *BLTIN to
6573 : a pointer to the built-in. Otherwise return the type of EXPR
6574 : and clear *BLTIN if non-null. */
6575 :
6576 : static tree
6577 2835457 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6578 : {
6579 2835457 : tree dummy;
6580 2835457 : if (!bltin)
6581 0 : bltin = &dummy;
6582 :
6583 2835457 : *bltin = NULL_TREE;
6584 :
6585 2835457 : tree type = TREE_TYPE (expr);
6586 2835457 : if (TREE_CODE (expr) != ADDR_EXPR)
6587 : return type;
6588 :
6589 208880 : tree oper = TREE_OPERAND (expr, 0);
6590 208880 : if (!DECL_P (oper)
6591 170384 : || TREE_CODE (oper) != FUNCTION_DECL
6592 245301 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6593 : return type;
6594 :
6595 2085 : built_in_function code = DECL_FUNCTION_CODE (oper);
6596 2085 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6597 : return type;
6598 :
6599 1977 : if ((*bltin = builtin_decl_implicit (code)))
6600 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6601 :
6602 : return type;
6603 : }
6604 :
6605 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6606 : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6607 : if folded to an integer constant then the unselected half may
6608 : contain arbitrary operations not normally permitted in constant
6609 : expressions. Set the location of the expression to LOC. */
6610 :
6611 : tree
6612 408789 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6613 : tree op1, tree op1_original_type, location_t op1_loc,
6614 : tree op2, tree op2_original_type, location_t op2_loc)
6615 : {
6616 408789 : tree type1;
6617 408789 : tree type2;
6618 408789 : tree result_type = NULL;
6619 408789 : tree semantic_result_type = NULL;
6620 408789 : tree orig_op1 = op1, orig_op2 = op2;
6621 408789 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6622 408789 : bool ifexp_int_operands;
6623 408789 : tree ret;
6624 :
6625 408789 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6626 116106 : if (op1_int_operands)
6627 116106 : op1 = remove_c_maybe_const_expr (op1);
6628 408789 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6629 151292 : if (op2_int_operands)
6630 151292 : op2 = remove_c_maybe_const_expr (op2);
6631 408789 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6632 242253 : if (ifexp_int_operands)
6633 242253 : ifexp = remove_c_maybe_const_expr (ifexp);
6634 :
6635 : /* Promote both alternatives. */
6636 :
6637 408789 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6638 404867 : op1 = default_conversion (op1);
6639 408789 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6640 378944 : op2 = default_conversion (op2);
6641 :
6642 408789 : if (TREE_CODE (ifexp) == ERROR_MARK
6643 408720 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6644 817495 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6645 83 : return error_mark_node;
6646 :
6647 408706 : tree bltin1 = NULL_TREE;
6648 408706 : tree bltin2 = NULL_TREE;
6649 408706 : type1 = type_or_builtin_type (op1, &bltin1);
6650 408706 : const enum tree_code code1 = TREE_CODE (type1);
6651 408706 : type2 = type_or_builtin_type (op2, &bltin2);
6652 408706 : const enum tree_code code2 = TREE_CODE (type2);
6653 :
6654 408706 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6655 1 : return error_mark_node;
6656 :
6657 408705 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6658 1 : return error_mark_node;
6659 :
6660 : /* C90 does not permit non-lvalue arrays in conditional expressions.
6661 : In C99 they will be pointers by now. */
6662 408704 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6663 : {
6664 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6665 1 : return error_mark_node;
6666 : }
6667 :
6668 408703 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6669 408697 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6670 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6671 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6672 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6673 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6674 : {
6675 7 : semantic_result_type = c_common_type (type1, type2);
6676 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6677 : {
6678 6 : op1 = TREE_OPERAND (op1, 0);
6679 6 : type1 = TREE_TYPE (op1);
6680 6 : gcc_assert (TREE_CODE (type1) == code1);
6681 : }
6682 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6683 : {
6684 1 : op2 = TREE_OPERAND (op2, 0);
6685 1 : type2 = TREE_TYPE (op2);
6686 1 : gcc_assert (TREE_CODE (type2) == code2);
6687 : }
6688 : }
6689 :
6690 408703 : if (warn_cxx_compat)
6691 : {
6692 418 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6693 418 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6694 :
6695 418 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6696 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6697 424 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6698 2 : warning_at (colon_loc, OPT_Wc___compat,
6699 : "different enum types in conditional is "
6700 : "invalid in C++: %qT vs %qT", t1, t2);
6701 : }
6702 :
6703 408703 : if (warn_zero_as_null_pointer_constant
6704 20 : && c_inhibit_evaluation_warnings == 0)
6705 : {
6706 20 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6707 20 : && INTEGRAL_TYPE_P (type2) && null_pointer_constant_p (orig_op2))
6708 5 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6709 : "zero as null pointer constant");
6710 :
6711 20 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6712 20 : && INTEGRAL_TYPE_P (type1) && null_pointer_constant_p (orig_op1))
6713 5 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6714 : "zero as null pointer constant");
6715 : }
6716 :
6717 : /* Quickly detect the usual case where op1 and op2 have the same type
6718 : after promotion. */
6719 408703 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6720 : {
6721 274164 : if (type1 == type2)
6722 : result_type = type1;
6723 : else
6724 6387 : result_type = TYPE_MAIN_VARIANT (type1);
6725 : }
6726 134539 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6727 79884 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6728 54703 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6729 26152 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6730 : {
6731 : /* In C11, a conditional expression between a floating-point
6732 : type and an integer type should convert the integer type to
6733 : the evaluation format of the floating-point type, with
6734 : possible excess precision. */
6735 28651 : tree eptype1 = type1;
6736 28651 : tree eptype2 = type2;
6737 28651 : if (flag_isoc11)
6738 : {
6739 28338 : tree eptype;
6740 11640 : if (ANY_INTEGRAL_TYPE_P (type1)
6741 28338 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6742 : {
6743 2 : eptype2 = eptype;
6744 2 : if (!semantic_result_type)
6745 2 : semantic_result_type = c_common_type (type1, type2);
6746 : }
6747 501 : else if (ANY_INTEGRAL_TYPE_P (type2)
6748 28336 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6749 : {
6750 4626 : eptype1 = eptype;
6751 4626 : if (!semantic_result_type)
6752 4626 : semantic_result_type = c_common_type (type1, type2);
6753 : }
6754 : }
6755 28651 : result_type = c_common_type (eptype1, eptype2);
6756 28651 : if (result_type == error_mark_node)
6757 : return error_mark_node;
6758 28649 : do_warn_double_promotion (result_type, type1, type2,
6759 : "implicit conversion from %qT to %qT to "
6760 : "match other result of conditional",
6761 : colon_loc);
6762 :
6763 : /* If -Wsign-compare, warn here if type1 and type2 have
6764 : different signedness. We'll promote the signed to unsigned
6765 : and later code won't know it used to be different.
6766 : Do this check on the original types, so that explicit casts
6767 : will be considered, but default promotions won't. */
6768 28649 : if (c_inhibit_evaluation_warnings == 0)
6769 : {
6770 28458 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6771 28458 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6772 :
6773 28458 : if (unsigned_op1 ^ unsigned_op2)
6774 : {
6775 13020 : bool ovf;
6776 :
6777 : /* Do not warn if the result type is signed, since the
6778 : signed type will only be chosen if it can represent
6779 : all the values of the unsigned type. */
6780 13020 : if (!TYPE_UNSIGNED (result_type))
6781 : /* OK */;
6782 : else
6783 : {
6784 12916 : bool op1_maybe_const = true;
6785 12916 : bool op2_maybe_const = true;
6786 :
6787 : /* Do not warn if the signed quantity is an
6788 : unsuffixed integer literal (or some static
6789 : constant expression involving such literals) and
6790 : it is non-negative. This warning requires the
6791 : operands to be folded for best results, so do
6792 : that folding in this case even without
6793 : warn_sign_compare to avoid warning options
6794 : possibly affecting code generation. */
6795 12916 : c_inhibit_evaluation_warnings
6796 12916 : += (ifexp == truthvalue_false_node);
6797 12916 : op1 = c_fully_fold (op1, require_constant_value,
6798 : &op1_maybe_const);
6799 12916 : c_inhibit_evaluation_warnings
6800 12916 : -= (ifexp == truthvalue_false_node);
6801 :
6802 12916 : c_inhibit_evaluation_warnings
6803 12916 : += (ifexp == truthvalue_true_node);
6804 12916 : op2 = c_fully_fold (op2, require_constant_value,
6805 : &op2_maybe_const);
6806 12916 : c_inhibit_evaluation_warnings
6807 12916 : -= (ifexp == truthvalue_true_node);
6808 :
6809 12916 : if (warn_sign_compare)
6810 : {
6811 1245 : if ((unsigned_op2
6812 685 : && tree_expr_nonnegative_warnv_p (op1, &ovf))
6813 1248 : || (unsigned_op1
6814 560 : && tree_expr_nonnegative_warnv_p (op2, &ovf)))
6815 : /* OK */;
6816 10 : else if (unsigned_op2)
6817 3 : warning_at (op1_loc, OPT_Wsign_compare,
6818 : "operand of %<?:%> changes signedness from "
6819 : "%qT to %qT due to unsignedness of other "
6820 3 : "operand", TREE_TYPE (orig_op1),
6821 3 : TREE_TYPE (orig_op2));
6822 : else
6823 7 : warning_at (op2_loc, OPT_Wsign_compare,
6824 : "operand of %<?:%> changes signedness from "
6825 : "%qT to %qT due to unsignedness of other "
6826 7 : "operand", TREE_TYPE (orig_op2),
6827 7 : TREE_TYPE (orig_op1));
6828 : }
6829 12916 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6830 7566 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6831 12916 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6832 4396 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6833 : }
6834 : }
6835 : }
6836 : }
6837 105888 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6838 : {
6839 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6840 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6841 : "ISO C forbids conditional expr with only one void side");
6842 25995 : result_type = void_type_node;
6843 : }
6844 79893 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6845 : {
6846 79496 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6847 79496 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6848 79496 : addr_space_t as_common;
6849 :
6850 79496 : if (comp_target_types (colon_loc, type1, type2))
6851 : {
6852 2619 : ifexp = save_expr (ifexp);
6853 2619 : result_type = common_pointer_type (type1, type2, ifexp);
6854 : }
6855 76877 : else if (null_pointer_constant_p (orig_op1))
6856 : result_type = type2;
6857 72715 : else if (null_pointer_constant_p (orig_op2))
6858 : result_type = type1;
6859 34803 : else if (!addr_space_superset (as1, as2, &as_common))
6860 : {
6861 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6862 : "used in conditional expression");
6863 0 : return error_mark_node;
6864 : }
6865 34803 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6866 330 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6867 34805 : || (VOID_TYPE_P (TREE_TYPE (type2))
6868 34428 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6869 : {
6870 34755 : tree t1 = TREE_TYPE (type1);
6871 34755 : tree t2 = TREE_TYPE (type2);
6872 34755 : if (!(VOID_TYPE_P (t1)
6873 329 : && !TYPE_ATOMIC (t1)))
6874 : {
6875 : /* roles are swapped */
6876 34427 : t1 = t2;
6877 34427 : t2 = TREE_TYPE (type1);
6878 : }
6879 34755 : tree t2_stripped = strip_array_types (t2);
6880 34755 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6881 34755 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6882 : {
6883 18 : if (!flag_isoc23)
6884 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6885 : "pointer to array loses qualifier "
6886 : "in conditional expression");
6887 12 : else if (warn_c11_c23_compat > 0)
6888 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6889 : "pointer to array loses qualifier "
6890 : "in conditional expression in ISO C before C23");
6891 : }
6892 34755 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6893 4 : pedwarn (colon_loc, OPT_Wpedantic,
6894 : "ISO C forbids conditional expr between "
6895 : "%<void *%> and function pointer");
6896 : /* for array, use qualifiers of element type */
6897 34755 : if (flag_isoc23)
6898 11006 : t2 = t2_stripped;
6899 34755 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6900 : }
6901 : /* Objective-C pointer comparisons are a bit more lenient. */
6902 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6903 0 : result_type = objc_common_type (type1, type2);
6904 : else
6905 : {
6906 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6907 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6908 48 : if (!flag_isoc99)
6909 : /* This downgrade to a warning ensures that -std=gnu89
6910 : -pedantic-errors does not flag these mismatches between
6911 : builtins as errors (as diagnostics::kind::permerror would)
6912 : ISO C99 and later do not have implicit function declarations,
6913 : so the mismatch cannot occur naturally there. */
6914 5 : kind = (bltin1 && bltin2
6915 5 : ? diagnostics::kind::warning
6916 : : diagnostics::kind::pedwarn);
6917 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6918 : "pointer type mismatch "
6919 : "in conditional expression"))
6920 : {
6921 46 : inform (op1_loc, "first expression has type %qT", type1);
6922 46 : inform (op2_loc, "second expression has type %qT", type2);
6923 : }
6924 48 : result_type = c_build_pointer_type
6925 48 : (c_build_qualified_type (void_type_node, qual));
6926 : }
6927 : }
6928 397 : else if (code1 == POINTER_TYPE
6929 277 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6930 : {
6931 273 : if (!null_pointer_constant_p (orig_op2))
6932 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6933 : "pointer/integer type mismatch "
6934 : "in conditional expression");
6935 : else
6936 : {
6937 259 : op2 = null_pointer_node;
6938 : }
6939 : result_type = type1;
6940 : }
6941 124 : else if (code2 == POINTER_TYPE
6942 90 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6943 : {
6944 86 : if (!null_pointer_constant_p (orig_op1))
6945 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6946 : "pointer/integer type mismatch "
6947 : "in conditional expression");
6948 : else
6949 : {
6950 72 : op1 = null_pointer_node;
6951 : }
6952 : result_type = type2;
6953 : }
6954 : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6955 : type nullptr_t and the other is a pointer, the result type is the pointer
6956 : type." */
6957 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
6958 : result_type = type2;
6959 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
6960 : result_type = type1;
6961 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
6962 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
6963 8 : TYPE_MAIN_VARIANT (type2)))
6964 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
6965 8 : TYPE_MAIN_VARIANT (type2));
6966 :
6967 408679 : if (!result_type)
6968 : {
6969 22 : if (flag_cond_mismatch)
6970 0 : result_type = void_type_node;
6971 : else
6972 : {
6973 22 : error_at (colon_loc, "type mismatch in conditional expression");
6974 22 : return error_mark_node;
6975 : }
6976 : }
6977 :
6978 : /* Merge const and volatile flags of the incoming types. */
6979 408679 : result_type
6980 408679 : = build_type_variant (result_type,
6981 : TYPE_READONLY (type1) || TYPE_READONLY (type2),
6982 : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6983 :
6984 408679 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
6985 : semantic_result_type);
6986 408679 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
6987 : semantic_result_type);
6988 :
6989 408679 : if (ifexp_bcp && ifexp == truthvalue_true_node)
6990 : {
6991 10 : op2_int_operands = true;
6992 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
6993 : }
6994 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
6995 : {
6996 49 : op1_int_operands = true;
6997 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
6998 : }
6999 914474 : int_const = int_operands = (ifexp_int_operands
7000 408679 : && op1_int_operands
7001 408679 : && op2_int_operands);
7002 97116 : if (int_operands)
7003 : {
7004 97116 : int_const = ((ifexp == truthvalue_true_node
7005 58769 : && TREE_CODE (orig_op1) == INTEGER_CST
7006 58755 : && !TREE_OVERFLOW (orig_op1))
7007 97130 : || (ifexp == truthvalue_false_node
7008 38301 : && TREE_CODE (orig_op2) == INTEGER_CST
7009 38285 : && !TREE_OVERFLOW (orig_op2)));
7010 : }
7011 :
7012 : /* Need to convert condition operand into a vector mask. */
7013 408679 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
7014 : {
7015 0 : tree vectype = TREE_TYPE (ifexp);
7016 0 : tree elem_type = TREE_TYPE (vectype);
7017 0 : tree zero = build_int_cst (elem_type, 0);
7018 0 : tree zero_vec = build_vector_from_val (vectype, zero);
7019 0 : tree cmp_type = truth_type_for (vectype);
7020 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
7021 : }
7022 :
7023 408679 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
7024 97072 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
7025 : else
7026 : {
7027 311607 : if (int_operands)
7028 : {
7029 : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
7030 : nested inside of the expression. */
7031 76 : op1 = c_fully_fold (op1, false, NULL);
7032 76 : op2 = c_fully_fold (op2, false, NULL);
7033 : }
7034 311607 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
7035 311607 : if (int_operands)
7036 76 : ret = note_integer_operands (ret);
7037 : }
7038 408679 : if (semantic_result_type)
7039 4635 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
7040 :
7041 408679 : protected_set_expr_location (ret, colon_loc);
7042 :
7043 : /* If the OP1 and OP2 are the same and don't have side-effects,
7044 : warn here, because the COND_EXPR will be turned into OP1. */
7045 408679 : if (warn_duplicated_branches
7046 34 : && TREE_CODE (ret) == COND_EXPR
7047 408713 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
7048 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
7049 : "this condition has identical branches");
7050 :
7051 : return ret;
7052 : }
7053 :
7054 : /* EXPR is an expression, location LOC, whose result is discarded.
7055 : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
7056 : whose right-hand operand is such a call, possibly recursively). */
7057 :
7058 : static void
7059 6474572 : maybe_warn_nodiscard (location_t loc, tree expr)
7060 : {
7061 6474572 : if (VOID_TYPE_P (TREE_TYPE (expr)))
7062 : return;
7063 3959039 : while (TREE_CODE (expr) == COMPOUND_EXPR)
7064 : {
7065 49434 : expr = TREE_OPERAND (expr, 1);
7066 49434 : if (EXPR_HAS_LOCATION (expr))
7067 32333 : loc = EXPR_LOCATION (expr);
7068 : }
7069 3909605 : if (TREE_CODE (expr) != CALL_EXPR)
7070 : return;
7071 344066 : tree fn = CALL_EXPR_FN (expr);
7072 344066 : if (!fn)
7073 : return;
7074 344050 : tree attr;
7075 344050 : if (TREE_CODE (fn) == ADDR_EXPR
7076 343383 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
7077 687433 : && (attr = lookup_attribute ("nodiscard",
7078 343383 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
7079 : {
7080 3 : fn = TREE_OPERAND (fn, 0);
7081 3 : tree args = TREE_VALUE (attr);
7082 3 : if (args)
7083 1 : args = TREE_VALUE (args);
7084 3 : auto_diagnostic_group d;
7085 3 : auto_urlify_attributes sentinel;
7086 3 : int warned;
7087 3 : if (args)
7088 1 : warned = warning_at (loc, OPT_Wunused_result,
7089 : "ignoring return value of %qD, declared with "
7090 : "attribute %<nodiscard%>: %E", fn, args);
7091 : else
7092 2 : warned = warning_at (loc, OPT_Wunused_result,
7093 : "ignoring return value of %qD, declared with "
7094 : "attribute %<nodiscard%>", fn);
7095 3 : if (warned)
7096 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
7097 3 : }
7098 : else
7099 : {
7100 344047 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
7101 344047 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
7102 344047 : if (!attr)
7103 344035 : return;
7104 12 : tree args = TREE_VALUE (attr);
7105 12 : if (args)
7106 5 : args = TREE_VALUE (args);
7107 12 : auto_diagnostic_group d;
7108 12 : auto_urlify_attributes sentinel;
7109 12 : int warned;
7110 12 : if (args)
7111 5 : warned = warning_at (loc, OPT_Wunused_result,
7112 : "ignoring return value of type %qT, declared "
7113 : "with attribute %<nodiscard%>: %E",
7114 : rettype, args);
7115 : else
7116 7 : warned = warning_at (loc, OPT_Wunused_result,
7117 : "ignoring return value of type %qT, declared "
7118 : "with attribute %<nodiscard%>", rettype);
7119 12 : if (warned)
7120 : {
7121 12 : if (TREE_CODE (fn) == ADDR_EXPR)
7122 : {
7123 11 : fn = TREE_OPERAND (fn, 0);
7124 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
7125 11 : inform (DECL_SOURCE_LOCATION (fn),
7126 : "in call to %qD, declared here", fn);
7127 : }
7128 : }
7129 12 : }
7130 : }
7131 :
7132 : /* Return a compound expression that performs two expressions and
7133 : returns the value of the second of them.
7134 :
7135 : LOC is the location of the COMPOUND_EXPR. */
7136 :
7137 : tree
7138 111776 : build_compound_expr (location_t loc, tree expr1, tree expr2)
7139 : {
7140 111776 : bool expr1_int_operands, expr2_int_operands;
7141 111776 : tree eptype = NULL_TREE;
7142 111776 : tree ret;
7143 :
7144 111776 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
7145 326 : if (expr1_int_operands)
7146 326 : expr1 = remove_c_maybe_const_expr (expr1);
7147 111776 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7148 66105 : if (expr2_int_operands)
7149 66105 : expr2 = remove_c_maybe_const_expr (expr2);
7150 :
7151 111776 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7152 0 : expr1 = TREE_OPERAND (expr1, 0);
7153 111776 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7154 : {
7155 1 : eptype = TREE_TYPE (expr2);
7156 1 : expr2 = TREE_OPERAND (expr2, 0);
7157 : }
7158 :
7159 111776 : if (!TREE_SIDE_EFFECTS (expr1))
7160 : {
7161 : /* The left-hand operand of a comma expression is like an expression
7162 : statement: with -Wunused, we should warn if it doesn't have
7163 : any side-effects, unless it was explicitly cast to (void). */
7164 8406 : if (warn_unused_value)
7165 : {
7166 1257 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7167 1257 : && CONVERT_EXPR_P (expr1))
7168 : ; /* (void) a, b */
7169 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7170 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7171 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7172 : ; /* (void) a, (void) b, c */
7173 : else
7174 11 : warning_at (loc, OPT_Wunused_value,
7175 : "left-hand operand of comma expression has no effect");
7176 : }
7177 : }
7178 103370 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7179 13111 : && warn_unused_value)
7180 : {
7181 : tree r = expr1;
7182 : location_t cloc = loc;
7183 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7184 : {
7185 2417 : if (EXPR_HAS_LOCATION (r))
7186 2417 : cloc = EXPR_LOCATION (r);
7187 2417 : r = TREE_OPERAND (r, 1);
7188 : }
7189 2416 : if (!TREE_SIDE_EFFECTS (r)
7190 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7191 2420 : && !CONVERT_EXPR_P (r))
7192 4 : warning_at (cloc, OPT_Wunused_value,
7193 : "right-hand operand of comma expression has no effect");
7194 : }
7195 :
7196 : /* With -Wunused, we should also warn if the left-hand operand does have
7197 : side-effects, but computes a value which is not used. For example, in
7198 : `foo() + bar(), baz()' the result of the `+' operator is not used,
7199 : so we should issue a warning. */
7200 100954 : else if (warn_unused_value)
7201 35400 : warn_if_unused_value (expr1, loc);
7202 :
7203 111776 : maybe_warn_nodiscard (loc, expr1);
7204 :
7205 111776 : if (expr2 == error_mark_node)
7206 : return error_mark_node;
7207 :
7208 111763 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7209 :
7210 111763 : if (flag_isoc99
7211 : && expr1_int_operands
7212 111307 : && expr2_int_operands)
7213 153 : ret = note_integer_operands (ret);
7214 :
7215 111763 : if (eptype)
7216 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7217 :
7218 111763 : protected_set_expr_location (ret, loc);
7219 111763 : return ret;
7220 : }
7221 :
7222 : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7223 : which we are casting. OTYPE is the type of the expression being
7224 : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7225 : of the cast. -Wcast-qual appeared on the command line. Named
7226 : address space qualifiers are not handled here, because they result
7227 : in different warnings. */
7228 :
7229 : static void
7230 13405 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7231 : {
7232 13405 : tree in_type = type;
7233 13405 : tree in_otype = otype;
7234 13405 : int added = 0;
7235 13405 : int discarded = 0;
7236 13575 : bool is_const;
7237 :
7238 : /* Check that the qualifiers on IN_TYPE are a superset of the
7239 : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7240 : nodes is uninteresting and we stop as soon as we hit a
7241 : non-POINTER_TYPE node on either type. */
7242 13575 : do
7243 : {
7244 13575 : in_otype = TREE_TYPE (in_otype);
7245 13575 : in_type = TREE_TYPE (in_type);
7246 :
7247 : /* GNU C allows cv-qualified function types. 'const' means the
7248 : function is very pure, 'volatile' means it can't return. We
7249 : need to warn when such qualifiers are added, not when they're
7250 : taken away. */
7251 13575 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7252 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7253 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7254 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7255 : else
7256 13557 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7257 13557 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7258 : }
7259 13575 : while (TREE_CODE (in_type) == POINTER_TYPE
7260 13575 : && TREE_CODE (in_otype) == POINTER_TYPE);
7261 :
7262 13405 : if (added)
7263 2 : warning_at (loc, OPT_Wcast_qual,
7264 : "cast adds %q#v qualifier to function type", added);
7265 :
7266 13405 : if (discarded)
7267 : /* There are qualifiers present in IN_OTYPE that are not present
7268 : in IN_TYPE. */
7269 1117 : warning_at (loc, OPT_Wcast_qual,
7270 : "cast discards %qv qualifier from pointer target type",
7271 : discarded);
7272 :
7273 13405 : if (added || discarded)
7274 : return;
7275 :
7276 : /* A cast from **T to const **T is unsafe, because it can cause a
7277 : const value to be changed with no additional warning. We only
7278 : issue this warning if T is the same on both sides, and we only
7279 : issue the warning if there are the same number of pointers on
7280 : both sides, as otherwise the cast is clearly unsafe anyhow. A
7281 : cast is unsafe when a qualifier is added at one level and const
7282 : is not present at all outer levels.
7283 :
7284 : To issue this warning, we check at each level whether the cast
7285 : adds new qualifiers not already seen. We don't need to special
7286 : case function types, as they won't have the same
7287 : TYPE_MAIN_VARIANT. */
7288 :
7289 12286 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7290 : return;
7291 51 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7292 : return;
7293 :
7294 48 : in_type = type;
7295 48 : in_otype = otype;
7296 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7297 130 : do
7298 : {
7299 130 : in_type = TREE_TYPE (in_type);
7300 130 : in_otype = TREE_TYPE (in_otype);
7301 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7302 130 : && !is_const)
7303 : {
7304 27 : warning_at (loc, OPT_Wcast_qual,
7305 : "to be safe all intermediate pointers in cast from "
7306 : "%qT to %qT must be %<const%> qualified",
7307 : otype, type);
7308 27 : break;
7309 : }
7310 103 : if (is_const)
7311 72 : is_const = TYPE_READONLY (in_type);
7312 : }
7313 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7314 : }
7315 :
7316 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7317 :
7318 : static bool
7319 1280 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7320 : {
7321 1280 : if (error_operand_p (t1) || error_operand_p (t2))
7322 : return true;
7323 :
7324 1279 : t1 = TYPE_MAIN_VARIANT (t1);
7325 1279 : t2 = TYPE_MAIN_VARIANT (t2);
7326 :
7327 1279 : if (TREE_CODE (t1) == POINTER_TYPE
7328 213 : && TREE_CODE (t2) == POINTER_TYPE)
7329 : return true;
7330 :
7331 : /* Only the precision of the parameter matters. This check should
7332 : make sure that the callee does not see undefined values in argument
7333 : registers. */
7334 1087 : if (INTEGRAL_TYPE_P (t1)
7335 905 : && INTEGRAL_TYPE_P (t2)
7336 1424 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7337 : return true;
7338 :
7339 930 : return comptypes (t1, t2);
7340 : }
7341 :
7342 : /* Check if a type cast between two function types can be considered safe. */
7343 :
7344 : static bool
7345 7469 : c_safe_function_type_cast_p (tree t1, tree t2)
7346 : {
7347 7469 : if (TREE_TYPE (t1) == void_type_node &&
7348 5479 : TYPE_ARG_TYPES (t1) == void_list_node)
7349 : return true;
7350 :
7351 3714 : if (TREE_TYPE (t2) == void_type_node &&
7352 2889 : TYPE_ARG_TYPES (t2) == void_list_node)
7353 : return true;
7354 :
7355 884 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7356 : return false;
7357 :
7358 189 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7359 481 : t1 && t2;
7360 292 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7361 396 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7362 : return false;
7363 :
7364 : return true;
7365 : }
7366 :
7367 : /* Build an expression representing a cast to type TYPE of expression EXPR.
7368 : LOC is the location of the cast-- typically the open paren of the cast. */
7369 :
7370 : tree
7371 120249786 : build_c_cast (location_t loc, tree type, tree expr)
7372 : {
7373 120249786 : tree value;
7374 :
7375 120249786 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7376 :
7377 120249786 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7378 66 : expr = TREE_OPERAND (expr, 0);
7379 :
7380 120249786 : value = expr;
7381 120249786 : if (int_operands)
7382 6871380 : value = remove_c_maybe_const_expr (value);
7383 :
7384 120249786 : if (type == error_mark_node || expr == error_mark_node)
7385 : return error_mark_node;
7386 :
7387 : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7388 : only in <protocol> qualifications. But when constructing cast expressions,
7389 : the protocols do matter and must be kept around. */
7390 120248764 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7391 0 : return build1 (NOP_EXPR, type, expr);
7392 :
7393 120248764 : type = TYPE_MAIN_VARIANT (type);
7394 :
7395 120248764 : if (TREE_CODE (type) == ARRAY_TYPE)
7396 : {
7397 13 : error_at (loc, "cast specifies array type");
7398 13 : return error_mark_node;
7399 : }
7400 :
7401 120248751 : if (TREE_CODE (type) == FUNCTION_TYPE)
7402 : {
7403 6 : error_at (loc, "cast specifies function type");
7404 6 : return error_mark_node;
7405 : }
7406 :
7407 120248745 : if (!VOID_TYPE_P (type))
7408 : {
7409 120204256 : value = require_complete_type (loc, value);
7410 120204256 : if (value == error_mark_node)
7411 : return error_mark_node;
7412 : }
7413 :
7414 120248744 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7415 : {
7416 18121055 : if (RECORD_OR_UNION_TYPE_P (type)
7417 18121055 : && pedwarn (loc, OPT_Wpedantic,
7418 : "ISO C forbids casting nonscalar to the same type"))
7419 : ;
7420 18121051 : else if (warn_useless_cast)
7421 7 : warning_at (loc, OPT_Wuseless_cast,
7422 : "useless cast to type %qT", type);
7423 :
7424 : /* Convert to remove any qualifiers from VALUE's type. */
7425 18121055 : value = convert (type, value);
7426 : }
7427 102127689 : else if (TREE_CODE (type) == UNION_TYPE)
7428 : {
7429 137 : tree field;
7430 :
7431 192 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7432 185 : if (TREE_TYPE (field) != error_mark_node
7433 369 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7434 184 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7435 : break;
7436 :
7437 137 : if (field)
7438 : {
7439 130 : tree t;
7440 130 : bool maybe_const = true;
7441 :
7442 130 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7443 130 : t = c_fully_fold (value, false, &maybe_const);
7444 130 : t = build_constructor_single (type, field, t);
7445 130 : if (!maybe_const)
7446 15 : t = c_wrap_maybe_const (t, true);
7447 130 : t = digest_init (loc, field, type, t,
7448 : NULL_TREE, false, false, false, true, false, false);
7449 130 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7450 130 : return t;
7451 : }
7452 7 : error_at (loc, "cast to union type from type not present in union");
7453 7 : return error_mark_node;
7454 : }
7455 : else
7456 : {
7457 102127552 : tree otype, ovalue;
7458 :
7459 102127552 : if (type == void_type_node)
7460 : {
7461 18519 : tree t = build1 (CONVERT_EXPR, type, value);
7462 18519 : SET_EXPR_LOCATION (t, loc);
7463 18519 : return t;
7464 : }
7465 :
7466 102109033 : otype = TREE_TYPE (value);
7467 :
7468 : /* Optionally warn about potentially worrisome casts. */
7469 102109033 : if (warn_cast_qual
7470 179503 : && TREE_CODE (type) == POINTER_TYPE
7471 26469 : && TREE_CODE (otype) == POINTER_TYPE)
7472 13405 : handle_warn_cast_qual (loc, type, otype);
7473 :
7474 : /* Warn about conversions between pointers to disjoint
7475 : address spaces. */
7476 102109033 : if (TREE_CODE (type) == POINTER_TYPE
7477 3331715 : && TREE_CODE (otype) == POINTER_TYPE
7478 105096170 : && !null_pointer_constant_p (value))
7479 : {
7480 2933436 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7481 2933436 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7482 2933436 : addr_space_t as_common;
7483 :
7484 2933436 : if (!addr_space_superset (as_to, as_from, &as_common))
7485 : {
7486 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7487 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7488 : "from disjoint generic address space pointer",
7489 : c_addr_space_name (as_to));
7490 :
7491 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7492 0 : warning_at (loc, 0, "cast to generic address space pointer "
7493 : "from disjoint %qs address space pointer",
7494 : c_addr_space_name (as_from));
7495 :
7496 : else
7497 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7498 : "from disjoint %qs address space pointer",
7499 : c_addr_space_name (as_to),
7500 : c_addr_space_name (as_from));
7501 : }
7502 :
7503 : /* Warn of new allocations that are not big enough for the target
7504 : type. */
7505 2933436 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7506 805 : if (tree fndecl = get_callee_fndecl (value))
7507 801 : if (DECL_IS_MALLOC (fndecl))
7508 : {
7509 517 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7510 517 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7511 517 : if (alloc_size)
7512 159 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7513 : alloc_size);
7514 : }
7515 : }
7516 :
7517 : /* Warn about possible alignment problems. */
7518 102109033 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7519 7 : && TREE_CODE (type) == POINTER_TYPE
7520 7 : && TREE_CODE (otype) == POINTER_TYPE
7521 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7522 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7523 : /* Don't warn about opaque types, where the actual alignment
7524 : restriction is unknown. */
7525 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7526 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7527 102109047 : && min_align_of_type (TREE_TYPE (type))
7528 7 : > min_align_of_type (TREE_TYPE (otype)))
7529 5 : warning_at (loc, OPT_Wcast_align,
7530 : "cast increases required alignment of target type");
7531 :
7532 102109033 : if ((TREE_CODE (type) == INTEGER_TYPE
7533 102109033 : || TREE_CODE (type) == BITINT_TYPE)
7534 7087607 : && TREE_CODE (otype) == POINTER_TYPE
7535 102134271 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7536 : /* Unlike conversion of integers to pointers, where the
7537 : warning is disabled for converting constants because
7538 : of cases such as SIG_*, warn about converting constant
7539 : pointers to integers. In some cases it may cause unwanted
7540 : sign extension, and a warning is appropriate. */
7541 1450 : warning_at (loc, OPT_Wpointer_to_int_cast,
7542 : "cast from pointer to integer of different size");
7543 :
7544 102109033 : if ((TREE_CODE (value) == CALL_EXPR
7545 34667407 : && !is_access_with_size_p (value))
7546 136776438 : && TREE_CODE (type) != TREE_CODE (otype))
7547 2286 : warning_at (loc, OPT_Wbad_function_cast,
7548 : "cast from function call of type %qT "
7549 : "to non-matching type %qT", otype, type);
7550 :
7551 102109033 : if (TREE_CODE (type) == POINTER_TYPE
7552 3331715 : && (TREE_CODE (otype) == INTEGER_TYPE
7553 3331715 : || TREE_CODE (otype) == BITINT_TYPE)
7554 344492 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7555 : /* Don't warn about converting any constant. */
7556 102415957 : && !TREE_CONSTANT (value))
7557 407 : warning_at (loc,
7558 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7559 : "of different size");
7560 :
7561 102109033 : if (warn_strict_aliasing <= 2)
7562 93576990 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7563 :
7564 : /* If pedantic, warn for conversions between function and object
7565 : pointer types, except for converting a null pointer constant
7566 : to function pointer type. */
7567 102109033 : if (pedantic
7568 238933 : && TREE_CODE (type) == POINTER_TYPE
7569 144183 : && TREE_CODE (otype) == POINTER_TYPE
7570 2784 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7571 102109054 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7572 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7573 : "conversion of function pointer to object pointer type");
7574 :
7575 102109033 : if (pedantic
7576 238933 : && TREE_CODE (type) == POINTER_TYPE
7577 144183 : && TREE_CODE (otype) == POINTER_TYPE
7578 2784 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7579 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7580 102109043 : && !null_pointer_constant_p (value))
7581 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7582 : "conversion of object pointer to function pointer type");
7583 :
7584 102109033 : if (TREE_CODE (type) == POINTER_TYPE
7585 3331715 : && TREE_CODE (otype) == POINTER_TYPE
7586 2987137 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7587 8581 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7588 102116502 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7589 7469 : TREE_TYPE (otype)))
7590 799 : warning_at (loc, OPT_Wcast_function_type,
7591 : "cast between incompatible function types"
7592 : " from %qT to %qT", otype, type);
7593 :
7594 102109033 : ovalue = value;
7595 : /* If converting to boolean a value with integer operands that
7596 : is not itself represented as an INTEGER_CST, the call below
7597 : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7598 : build_binary_op as called by c_common_truthvalue_conversion
7599 : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7600 : subexpression has been fully folded. To avoid nested
7601 : C_MAYBE_CONST_EXPR, ensure that
7602 : c_objc_common_truthvalue_conversion receives an argument
7603 : properly marked as having integer operands in that case. */
7604 102109033 : if (int_operands
7605 6681095 : && TREE_CODE (value) != INTEGER_CST
7606 102109075 : && (TREE_CODE (type) == BOOLEAN_TYPE
7607 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7608 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7609 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7610 28 : value = note_integer_operands (value);
7611 102109033 : value = convert (type, value);
7612 :
7613 : /* Ignore any integer overflow caused by the cast. */
7614 102109033 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7615 : {
7616 6627600 : if (TREE_OVERFLOW_P (ovalue))
7617 : {
7618 9 : if (!TREE_OVERFLOW (value))
7619 : {
7620 : /* Avoid clobbering a shared constant. */
7621 0 : value = copy_node (value);
7622 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7623 : }
7624 : }
7625 6627591 : else if (TREE_OVERFLOW (value))
7626 : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7627 3085 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7628 : }
7629 : }
7630 :
7631 : /* Don't let a cast be an lvalue. */
7632 120230088 : if (lvalue_p (value))
7633 90613 : value = non_lvalue_loc (loc, value);
7634 :
7635 : /* Don't allow the results of casting to floating-point or complex
7636 : types be confused with actual constants, or casts involving
7637 : integer and pointer types other than direct integer-to-integer
7638 : and integer-to-pointer be confused with integer constant
7639 : expressions and null pointer constants. */
7640 120230088 : if (TREE_CODE (value) == REAL_CST
7641 120180211 : || TREE_CODE (value) == COMPLEX_CST
7642 240387676 : || (TREE_CODE (value) == INTEGER_CST
7643 6876463 : && !((TREE_CODE (expr) == INTEGER_CST
7644 6803022 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7645 66038 : || TREE_CODE (expr) == REAL_CST
7646 : || TREE_CODE (expr) == COMPLEX_CST)))
7647 134171 : value = build1 (NOP_EXPR, type, value);
7648 :
7649 : /* If the expression has integer operands and so can occur in an
7650 : unevaluated part of an integer constant expression, ensure the
7651 : return value reflects this. */
7652 120230088 : if (int_operands
7653 6857456 : && INTEGRAL_TYPE_P (type)
7654 6413293 : && value != error_mark_node
7655 126643380 : && !EXPR_INT_CONST_OPERANDS (value))
7656 11 : value = note_integer_operands (value);
7657 :
7658 120230088 : protected_set_expr_location (value, loc);
7659 120230088 : return value;
7660 : }
7661 :
7662 : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7663 : location of the open paren of the cast, or the position of the cast
7664 : expr. */
7665 : tree
7666 120249290 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7667 : {
7668 120249290 : tree type;
7669 120249290 : tree type_expr = NULL_TREE;
7670 120249290 : bool type_expr_const = true;
7671 120249290 : tree ret;
7672 120249290 : int saved_wsp = warn_strict_prototypes;
7673 :
7674 : /* This avoids warnings about unprototyped casts on
7675 : integers. E.g. "#define SIG_DFL (void(*)())0". */
7676 120249290 : if (TREE_CODE (expr) == INTEGER_CST)
7677 6925192 : warn_strict_prototypes = 0;
7678 120249290 : type = groktypename (type_name, &type_expr, &type_expr_const);
7679 120249290 : warn_strict_prototypes = saved_wsp;
7680 :
7681 764923 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7682 121013959 : && reject_gcc_builtin (expr))
7683 2 : return error_mark_node;
7684 :
7685 120249288 : ret = build_c_cast (loc, type, expr);
7686 120249288 : if (ret == error_mark_node)
7687 : return error_mark_node;
7688 :
7689 120248126 : if (type_expr)
7690 : {
7691 250 : bool inner_expr_const = true;
7692 250 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7693 250 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7694 500 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7695 145 : && inner_expr_const);
7696 250 : SET_EXPR_LOCATION (ret, loc);
7697 : }
7698 :
7699 120248126 : if (!EXPR_HAS_LOCATION (ret))
7700 6803706 : protected_set_expr_location (ret, loc);
7701 :
7702 : /* C++ does not permits types to be defined in a cast, but it
7703 : allows references to incomplete types. */
7704 120248126 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7705 1 : warning_at (loc, OPT_Wc___compat,
7706 : "defining a type in a cast is invalid in C++");
7707 :
7708 : return ret;
7709 : }
7710 :
7711 : /* Build an assignment expression of lvalue LHS from value RHS.
7712 : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7713 : may differ from TREE_TYPE (LHS) for an enum bitfield.
7714 : MODIFYCODE is the code for a binary operator that we use
7715 : to combine the old value of LHS with RHS to get the new value.
7716 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7717 : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7718 : which may differ from TREE_TYPE (RHS) for an enum value.
7719 :
7720 : LOCATION is the location of the MODIFYCODE operator.
7721 : RHS_LOC is the location of the RHS. */
7722 :
7723 : tree
7724 2901697 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7725 : enum tree_code modifycode,
7726 : location_t rhs_loc, tree rhs, tree rhs_origtype)
7727 : {
7728 2901697 : tree result;
7729 2901697 : tree newrhs;
7730 2901697 : tree rhseval = NULL_TREE;
7731 2901697 : tree lhstype = TREE_TYPE (lhs);
7732 2901697 : tree olhstype = lhstype;
7733 2901697 : bool npc;
7734 2901697 : bool is_atomic_op;
7735 :
7736 : /* Types that aren't fully specified cannot be used in assignments. */
7737 2901697 : lhs = require_complete_type (location, lhs);
7738 2901697 : rhs = require_complete_type (location, rhs);
7739 :
7740 : /* Avoid duplicate error messages from operands that had errors. */
7741 2901697 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7742 473 : return error_mark_node;
7743 :
7744 : /* Ensure an error for assigning a non-lvalue array to an array in
7745 : C90. */
7746 2901224 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7747 : {
7748 1 : error_at (location, "assignment to expression with array type");
7749 1 : return error_mark_node;
7750 : }
7751 :
7752 : /* For ObjC properties, defer this check. */
7753 2901223 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7754 32 : return error_mark_node;
7755 :
7756 2901191 : is_atomic_op = really_atomic_lvalue (lhs);
7757 :
7758 2901191 : newrhs = rhs;
7759 :
7760 2901191 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7761 : {
7762 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7763 : lhs_origtype, modifycode, rhs_loc, rhs,
7764 : rhs_origtype);
7765 2 : if (inner == error_mark_node)
7766 : return error_mark_node;
7767 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7768 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7769 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7770 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7771 2 : protected_set_expr_location (result, location);
7772 2 : return result;
7773 : }
7774 :
7775 : /* If a binary op has been requested, combine the old LHS value with the RHS
7776 : producing the value we should actually store into the LHS. */
7777 :
7778 2901189 : if (modifycode != NOP_EXPR)
7779 : {
7780 215244 : lhs = c_fully_fold (lhs, false, NULL, true);
7781 215244 : lhs = stabilize_reference (lhs);
7782 :
7783 : /* Construct the RHS for any non-atomic compound assignemnt. */
7784 215244 : if (!is_atomic_op)
7785 : {
7786 : /* If in LHS op= RHS the RHS has side-effects, ensure they
7787 : are preevaluated before the rest of the assignment expression's
7788 : side-effects, because RHS could contain e.g. function calls
7789 : that modify LHS. */
7790 199759 : if (TREE_SIDE_EFFECTS (rhs))
7791 : {
7792 8814 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7793 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7794 : else
7795 8786 : newrhs = save_expr (rhs);
7796 8814 : rhseval = newrhs;
7797 8814 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7798 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7799 : newrhs);
7800 : }
7801 199759 : bool clear_decl_read = false;
7802 88305 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7803 153555 : && !DECL_READ_P (lhs)
7804 252059 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7805 : : warn_unused_but_set_parameter) > 2)
7806 : {
7807 21550 : mark_exp_read (newrhs);
7808 21550 : if (!DECL_READ_P (lhs))
7809 199759 : clear_decl_read = true;
7810 : }
7811 :
7812 199759 : newrhs = build_binary_op (location, modifycode,
7813 : convert_lvalue_to_rvalue (location, lhs,
7814 : true, true),
7815 : newrhs, true);
7816 199759 : if (clear_decl_read)
7817 21550 : DECL_READ_P (lhs) = 0;
7818 :
7819 : /* The original type of the right hand side is no longer
7820 : meaningful. */
7821 : rhs_origtype = NULL_TREE;
7822 : }
7823 : }
7824 :
7825 2901189 : if (c_dialect_objc ())
7826 : {
7827 : /* Check if we are modifying an Objective-C property reference;
7828 : if so, we need to generate setter calls. */
7829 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7830 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7831 : else
7832 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7833 0 : if (result)
7834 0 : goto return_result;
7835 :
7836 : /* Else, do the check that we postponed for Objective-C. */
7837 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7838 0 : return error_mark_node;
7839 : }
7840 :
7841 : /* Give an error for storing in something that is 'const'. */
7842 :
7843 2901189 : if (TYPE_READONLY (lhstype)
7844 2901189 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7845 25774 : && C_TYPE_FIELDS_READONLY (lhstype)))
7846 : {
7847 88 : readonly_error (location, lhs, lv_assign);
7848 88 : return error_mark_node;
7849 : }
7850 2901101 : else if (TREE_READONLY (lhs))
7851 1 : readonly_warning (lhs, lv_assign);
7852 :
7853 : /* If storing into a structure or union member,
7854 : it has probably been given type `int'.
7855 : Compute the type that would go with
7856 : the actual amount of storage the member occupies. */
7857 :
7858 2901101 : if (TREE_CODE (lhs) == COMPONENT_REF
7859 248855 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7860 248855 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7861 109791 : || SCALAR_FLOAT_TYPE_P (lhstype)
7862 86460 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7863 169391 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7864 :
7865 : /* If storing in a field that is in actuality a short or narrower than one,
7866 : we must store in the field in its actual type. */
7867 :
7868 2901101 : if (lhstype != TREE_TYPE (lhs))
7869 : {
7870 0 : lhs = copy_node (lhs);
7871 0 : TREE_TYPE (lhs) = lhstype;
7872 : }
7873 :
7874 : /* Issue -Wc++-compat warnings about an assignment to an enum type
7875 : when LHS does not have its original type. This happens for,
7876 : e.g., an enum bitfield in a struct. */
7877 2901101 : if (warn_cxx_compat
7878 3196 : && lhs_origtype != NULL_TREE
7879 3196 : && lhs_origtype != lhstype
7880 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7881 : {
7882 4 : tree checktype = (rhs_origtype != NULL_TREE
7883 4 : ? rhs_origtype
7884 0 : : TREE_TYPE (rhs));
7885 4 : if (checktype != error_mark_node
7886 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7887 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7888 2 : warning_at (location, OPT_Wc___compat,
7889 : "enum conversion in assignment is invalid in C++");
7890 : }
7891 :
7892 : /* Remove qualifiers. */
7893 2901101 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7894 2901101 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7895 :
7896 : /* Convert new value to destination type. Fold it first, then
7897 : restore any excess precision information, for the sake of
7898 : conversion warnings. */
7899 :
7900 2901101 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7901 : {
7902 2885616 : tree rhs_semantic_type = NULL_TREE;
7903 2885616 : if (!c_in_omp_for)
7904 : {
7905 2869295 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7906 : {
7907 5077 : rhs_semantic_type = TREE_TYPE (newrhs);
7908 5077 : newrhs = TREE_OPERAND (newrhs, 0);
7909 : }
7910 2869295 : npc = null_pointer_constant_p (newrhs);
7911 2869295 : newrhs = c_fully_fold (newrhs, false, NULL);
7912 2869295 : if (rhs_semantic_type)
7913 5077 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7914 : }
7915 : else
7916 16321 : npc = null_pointer_constant_p (newrhs);
7917 2885616 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7918 : rhs_origtype, ic_assign, npc,
7919 : NULL_TREE, NULL_TREE, 0);
7920 2885616 : if (TREE_CODE (newrhs) == ERROR_MARK)
7921 127 : return error_mark_node;
7922 : }
7923 :
7924 : /* Emit ObjC write barrier, if necessary. */
7925 2900974 : if (c_dialect_objc () && flag_objc_gc)
7926 : {
7927 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7928 0 : if (result)
7929 : {
7930 0 : protected_set_expr_location (result, location);
7931 0 : goto return_result;
7932 : }
7933 : }
7934 :
7935 : /* Scan operands. */
7936 :
7937 2900974 : if (is_atomic_op)
7938 25964 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7939 : else
7940 : {
7941 2875010 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7942 2875010 : TREE_SIDE_EFFECTS (result) = 1;
7943 2875010 : protected_set_expr_location (result, location);
7944 : }
7945 :
7946 : /* If we got the LHS in a different type for storing in,
7947 : convert the result back to the nominal type of LHS
7948 : so that the value we return always has the same type
7949 : as the LHS argument. */
7950 :
7951 2900974 : if (olhstype == TREE_TYPE (result))
7952 2900974 : goto return_result;
7953 :
7954 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
7955 : rhs_origtype, ic_assign, false, NULL_TREE,
7956 : NULL_TREE, 0);
7957 0 : protected_set_expr_location (result, location);
7958 :
7959 2900974 : return_result:
7960 2900974 : if (rhseval)
7961 8814 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
7962 : return result;
7963 : }
7964 :
7965 : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
7966 : This is used to implement -fplan9-extensions. */
7967 :
7968 : static bool
7969 26 : find_anonymous_field_with_type (tree struct_type, tree type)
7970 : {
7971 26 : tree field;
7972 26 : bool found;
7973 :
7974 26 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
7975 26 : found = false;
7976 26 : for (field = TYPE_FIELDS (struct_type);
7977 68 : field != NULL_TREE;
7978 42 : field = TREE_CHAIN (field))
7979 : {
7980 42 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7981 42 : if (DECL_NAME (field) == NULL
7982 42 : && comptypes (type, fieldtype))
7983 : {
7984 4 : if (found)
7985 : return false;
7986 : found = true;
7987 : }
7988 38 : else if (DECL_NAME (field) == NULL
7989 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
7990 40 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
7991 : {
7992 0 : if (found)
7993 : return false;
7994 : found = true;
7995 : }
7996 : }
7997 : return found;
7998 : }
7999 :
8000 : /* RHS is an expression whose type is pointer to struct. If there is
8001 : an anonymous field in RHS with type TYPE, then return a pointer to
8002 : that field in RHS. This is used with -fplan9-extensions. This
8003 : returns NULL if no conversion could be found. */
8004 :
8005 : static tree
8006 28 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
8007 : {
8008 28 : tree rhs_struct_type, lhs_main_type;
8009 28 : tree field, found_field;
8010 28 : bool found_sub_field;
8011 28 : tree ret;
8012 :
8013 28 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
8014 28 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
8015 28 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
8016 :
8017 28 : gcc_assert (POINTER_TYPE_P (type));
8018 28 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
8019 :
8020 28 : found_field = NULL_TREE;
8021 28 : found_sub_field = false;
8022 28 : for (field = TYPE_FIELDS (rhs_struct_type);
8023 172 : field != NULL_TREE;
8024 144 : field = TREE_CHAIN (field))
8025 : {
8026 148 : if (DECL_NAME (field) != NULL_TREE
8027 148 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
8028 96 : continue;
8029 52 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8030 52 : if (comptypes (lhs_main_type, fieldtype))
8031 : {
8032 28 : if (found_field != NULL_TREE)
8033 : return NULL_TREE;
8034 : found_field = field;
8035 : }
8036 24 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
8037 : lhs_main_type))
8038 : {
8039 4 : if (found_field != NULL_TREE)
8040 : return NULL_TREE;
8041 : found_field = field;
8042 : found_sub_field = true;
8043 : }
8044 : }
8045 :
8046 24 : if (found_field == NULL_TREE)
8047 : return NULL_TREE;
8048 :
8049 24 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
8050 : build_fold_indirect_ref (rhs), found_field,
8051 : NULL_TREE);
8052 24 : ret = build_fold_addr_expr_loc (location, ret);
8053 :
8054 24 : if (found_sub_field)
8055 : {
8056 2 : ret = convert_to_anonymous_field (location, type, ret);
8057 2 : gcc_assert (ret != NULL_TREE);
8058 : }
8059 :
8060 : return ret;
8061 : }
8062 :
8063 : /* Issue an error message for a bad initializer component.
8064 : GMSGID identifies the message.
8065 : The component name is taken from the spelling stack. */
8066 :
8067 : static void ATTRIBUTE_GCC_DIAG (2,0)
8068 944 : error_init (location_t loc, const char *gmsgid, ...)
8069 : {
8070 944 : char *ofwhat;
8071 :
8072 944 : auto_diagnostic_group d;
8073 :
8074 : /* The gmsgid may be a format string with %< and %>. */
8075 944 : va_list ap;
8076 944 : va_start (ap, gmsgid);
8077 944 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
8078 944 : loc, -1, gmsgid, &ap);
8079 944 : va_end (ap);
8080 :
8081 944 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8082 944 : if (*ofwhat && warned)
8083 220 : inform (loc, "(near initialization for %qs)", ofwhat);
8084 944 : }
8085 :
8086 : /* Used to implement pedwarn_init and permerror_init. */
8087 :
8088 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8089 2538 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
8090 : va_list *ap, enum diagnostics::kind kind)
8091 : {
8092 : /* Use the location where a macro was expanded rather than where
8093 : it was defined to make sure macros defined in system headers
8094 : but used incorrectly elsewhere are diagnosed. */
8095 2538 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8096 2538 : auto_diagnostic_group d;
8097 2538 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
8098 2538 : if (warned)
8099 : {
8100 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8101 1815 : if (*ofwhat)
8102 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
8103 : }
8104 5076 : return warned;
8105 2538 : }
8106 :
8107 : /* Issue a pedantic warning for a bad initializer component. OPT is
8108 : the option OPT_* (from options.h) controlling this warning or 0 if
8109 : it is unconditionally given. GMSGID identifies the message. The
8110 : component name is taken from the spelling stack. */
8111 :
8112 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8113 2187 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
8114 : {
8115 2187 : va_list ap;
8116 2187 : va_start (ap, gmsgid);
8117 2187 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8118 : diagnostics::kind::pedwarn);
8119 2187 : va_end (ap);
8120 2187 : return warned;
8121 : }
8122 :
8123 : /* Like pedwarn_init, but issue a permerror. */
8124 :
8125 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8126 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
8127 : {
8128 351 : va_list ap;
8129 351 : va_start (ap, gmsgid);
8130 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8131 : diagnostics::kind::permerror);
8132 351 : va_end (ap);
8133 351 : return warned;
8134 : }
8135 :
8136 : /* Issue a warning for a bad initializer component.
8137 :
8138 : OPT is the OPT_W* value corresponding to the warning option that
8139 : controls this warning. GMSGID identifies the message. The
8140 : component name is taken from the spelling stack. */
8141 :
8142 : static void
8143 146 : warning_init (location_t loc, int opt, const char *gmsgid)
8144 : {
8145 146 : char *ofwhat;
8146 146 : bool warned;
8147 :
8148 146 : auto_diagnostic_group d;
8149 :
8150 : /* Use the location where a macro was expanded rather than where
8151 : it was defined to make sure macros defined in system headers
8152 : but used incorrectly elsewhere are diagnosed. */
8153 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8154 :
8155 : /* The gmsgid may be a format string with %< and %>. */
8156 146 : warned = warning_at (exploc, opt, gmsgid);
8157 146 : if (warned)
8158 : {
8159 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8160 133 : if (*ofwhat)
8161 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8162 : }
8163 146 : }
8164 :
8165 : /* If TYPE is an array type and EXPR is a parenthesized string
8166 : constant, warn if pedantic that EXPR is being used to initialize an
8167 : object of type TYPE. */
8168 :
8169 : void
8170 7280750 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8171 : {
8172 7280750 : if (pedantic
8173 80608 : && TREE_CODE (type) == ARRAY_TYPE
8174 1193 : && TREE_CODE (expr.value) == STRING_CST
8175 472 : && expr.original_code != STRING_CST)
8176 13 : pedwarn_init (loc, OPT_Wpedantic,
8177 : "array initialized from parenthesized string constant");
8178 7280750 : }
8179 :
8180 : /* Attempt to locate the parameter with the given index within FNDECL,
8181 : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8182 :
8183 : static location_t
8184 937 : get_fndecl_argument_location (tree fndecl, int argnum)
8185 : {
8186 937 : int i;
8187 937 : tree param;
8188 :
8189 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8190 937 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8191 1016 : i < argnum && param;
8192 79 : i++, param = TREE_CHAIN (param))
8193 : ;
8194 :
8195 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8196 : return DECL_SOURCE_LOCATION (FNDECL). */
8197 937 : if (param == NULL)
8198 653 : return DECL_SOURCE_LOCATION (fndecl);
8199 :
8200 284 : return DECL_SOURCE_LOCATION (param);
8201 : }
8202 :
8203 : /* Issue a note about a mismatching argument for parameter PARMNUM
8204 : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8205 : Attempt to issue the note at the pertinent parameter of the decl;
8206 : failing that issue it at the location of FUNDECL; failing that
8207 : issue it at PLOC.
8208 : Use highlight_colors::actual for the ACTUAL_TYPE
8209 : and highlight_colors::expected for EXPECTED_TYPE and the
8210 : parameter of FUNDECL*/
8211 :
8212 : static void
8213 1061 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8214 : tree expected_type, tree actual_type)
8215 : {
8216 1061 : location_t loc;
8217 1061 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8218 937 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8219 : else
8220 : loc = ploc;
8221 :
8222 1061 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8223 :
8224 1061 : pp_markup::element_expected_type elem_expected_type (expected_type);
8225 1061 : pp_markup::element_actual_type elem_actual_type (actual_type);
8226 1061 : inform (&richloc,
8227 : "expected %e but argument is of type %e",
8228 : &elem_expected_type, &elem_actual_type);
8229 1061 : }
8230 :
8231 : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8232 : function FUNDECL declared without prototype to parameter PARMNUM of
8233 : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8234 :
8235 : static void
8236 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8237 : tree parmtype, tree argtype)
8238 : {
8239 418 : tree_code parmcode = TREE_CODE (parmtype);
8240 418 : tree_code argcode = TREE_CODE (argtype);
8241 418 : tree promoted = c_type_promotes_to (argtype);
8242 :
8243 : /* Avoid warning for enum arguments that promote to an integer type
8244 : of the same size/mode. */
8245 418 : if (parmcode == INTEGER_TYPE
8246 418 : && argcode == ENUMERAL_TYPE
8247 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8248 : return;
8249 :
8250 417 : if ((parmcode == argcode
8251 222 : || (parmcode == INTEGER_TYPE
8252 : && argcode == ENUMERAL_TYPE))
8253 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8254 : return;
8255 :
8256 : /* This diagnoses even signed/unsigned mismatches. Those might be
8257 : safe in many cases but GCC may emit suboptimal code for them so
8258 : warning on those cases drives efficiency improvements. */
8259 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8260 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8261 : ? G_("%qD argument %d type is %qT where %qT is expected "
8262 : "in a call to built-in function declared without "
8263 : "prototype")
8264 : : G_("%qD argument %d promotes to %qT where %qT is expected "
8265 : "in a call to built-in function declared without "
8266 : "prototype"),
8267 : fundecl, parmnum, promoted, parmtype))
8268 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8269 : "built-in %qD declared here",
8270 : fundecl);
8271 : }
8272 :
8273 : /* Print a declaration in quotes, with the given highlight_color.
8274 : Analogous to handler for %qD, but with a specific highlight color. */
8275 :
8276 199 : class pp_element_quoted_decl : public pp_element
8277 : {
8278 : public:
8279 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8280 199 : : m_decl (decl),
8281 199 : m_highlight_color (highlight_color)
8282 : {
8283 : }
8284 :
8285 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8286 : {
8287 199 : ctxt.begin_quote ();
8288 199 : ctxt.begin_highlight_color (m_highlight_color);
8289 :
8290 199 : print_decl (ctxt);
8291 :
8292 199 : ctxt.end_highlight_color ();
8293 199 : ctxt.end_quote ();
8294 199 : }
8295 :
8296 199 : void print_decl (pp_markup::context &ctxt)
8297 : {
8298 199 : pretty_printer *const pp = &ctxt.m_pp;
8299 199 : pp->set_padding (pp_none);
8300 199 : if (DECL_NAME (m_decl))
8301 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8302 : else
8303 0 : pp_string (pp, _("({anonymous})"));
8304 199 : }
8305 :
8306 : private:
8307 : tree m_decl;
8308 : const char *m_highlight_color;
8309 : };
8310 :
8311 : /* If TYPE is from a typedef, issue a note showing the location
8312 : to the user.
8313 : Use HIGHLIGHT_COLOR as the highlight color. */
8314 :
8315 : static void
8316 1178 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8317 : {
8318 1178 : if (!typedef_variant_p (type))
8319 1144 : return;
8320 :
8321 34 : tree typedef_decl = TYPE_NAME (type);
8322 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8323 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8324 34 : nullptr, highlight_color);
8325 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8326 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8327 34 : }
8328 :
8329 : /* Convert value RHS to type TYPE as preparation for an assignment to
8330 : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8331 : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8332 : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8333 : constant before any folding.
8334 : The real work of conversion is done by `convert'.
8335 : The purpose of this function is to generate error messages
8336 : for assignments that are not allowed in C.
8337 : ERRTYPE says whether it is argument passing, assignment,
8338 : initialization or return.
8339 :
8340 : In the following example, '~' denotes where EXPR_LOC and '^' where
8341 : LOCATION point to:
8342 :
8343 : f (var); [ic_argpass]
8344 : ^ ~~~
8345 : x = var; [ic_assign]
8346 : ^ ~~~;
8347 : int x = var; [ic_init]
8348 : ^^^
8349 : return x; [ic_return]
8350 : ^
8351 :
8352 : FUNCTION is a tree for the function being called.
8353 : PARMNUM is the number of the argument, for printing in error messages.
8354 : WARNOPT may be set to a warning option to issue the corresponding warning
8355 : rather than an error for invalid conversions. Used for calls to built-in
8356 : functions declared without a prototype. */
8357 :
8358 : static tree
8359 167045593 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8360 : tree rhs, tree origtype, enum impl_conv errtype,
8361 : bool null_pointer_constant, tree fundecl,
8362 : tree function, int parmnum, int warnopt /* = 0 */)
8363 : {
8364 167045593 : enum tree_code codel = TREE_CODE (type);
8365 167045593 : tree orig_rhs = rhs;
8366 167045593 : tree rhstype;
8367 167045593 : enum tree_code coder;
8368 167045593 : tree rname = NULL_TREE;
8369 167045593 : bool objc_ok = false;
8370 :
8371 : /* Use the expansion point location to handle cases such as user's
8372 : function returning a wrong-type macro defined in a system header. */
8373 167045593 : location = expansion_point_location_if_in_system_header (location);
8374 :
8375 167045593 : if (errtype == ic_argpass)
8376 : {
8377 125605437 : tree selector;
8378 : /* Change pointer to function to the function itself for
8379 : diagnostics. */
8380 125605437 : if (TREE_CODE (function) == ADDR_EXPR
8381 125605437 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8382 0 : function = TREE_OPERAND (function, 0);
8383 :
8384 : /* Handle an ObjC selector specially for diagnostics. */
8385 125605437 : selector = objc_message_selector ();
8386 125605437 : rname = function;
8387 125605437 : if (selector && parmnum > 2)
8388 : {
8389 0 : rname = selector;
8390 0 : parmnum -= 2;
8391 : }
8392 : }
8393 :
8394 : /* This macro is used to emit diagnostics to ensure that all format
8395 : strings are complete sentences, visible to gettext and checked at
8396 : compile time. */
8397 : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8398 : do { \
8399 : switch (errtype) \
8400 : { \
8401 : case ic_argpass: \
8402 : { \
8403 : auto_diagnostic_group d; \
8404 : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8405 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8406 : } \
8407 : break; \
8408 : case ic_assign: \
8409 : pedwarn (LOCATION, OPT, AS); \
8410 : break; \
8411 : case ic_init: \
8412 : case ic_init_const: \
8413 : pedwarn_init (LOCATION, OPT, IN); \
8414 : break; \
8415 : case ic_return: \
8416 : pedwarn (LOCATION, OPT, RE); \
8417 : break; \
8418 : default: \
8419 : gcc_unreachable (); \
8420 : } \
8421 : } while (0)
8422 :
8423 : /* This macro is used to emit diagnostics to ensure that all format
8424 : strings are complete sentences, visible to gettext and checked at
8425 : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8426 : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8427 : do { \
8428 : switch (errtype) \
8429 : { \
8430 : case ic_argpass: \
8431 : { \
8432 : auto_diagnostic_group d; \
8433 : if (PEDWARN) { \
8434 : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8435 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8436 : } else { \
8437 : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8438 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8439 : } \
8440 : } \
8441 : break; \
8442 : case ic_assign: \
8443 : if (PEDWARN) \
8444 : pedwarn (LOCATION, OPT, AS, QUALS); \
8445 : else \
8446 : warning_at (LOCATION, OPT, AS, QUALS); \
8447 : break; \
8448 : case ic_init: \
8449 : case ic_init_const: \
8450 : if (PEDWARN) \
8451 : pedwarn (LOCATION, OPT, IN, QUALS); \
8452 : else \
8453 : warning_at (LOCATION, OPT, IN, QUALS); \
8454 : break; \
8455 : case ic_return: \
8456 : if (PEDWARN) \
8457 : pedwarn (LOCATION, OPT, RE, QUALS); \
8458 : else \
8459 : warning_at (LOCATION, OPT, RE, QUALS); \
8460 : break; \
8461 : default: \
8462 : gcc_unreachable (); \
8463 : } \
8464 : } while (0)
8465 :
8466 : /* This macro is used to emit diagnostics to ensure that all format
8467 : strings are complete sentences, visible to gettext and checked at
8468 : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8469 : extra parameter to enumerate qualifiers. */
8470 : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8471 : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8472 :
8473 :
8474 167045593 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8475 5985 : rhs = TREE_OPERAND (rhs, 0);
8476 :
8477 167045593 : rhstype = TREE_TYPE (rhs);
8478 167045593 : coder = TREE_CODE (rhstype);
8479 :
8480 167045593 : if (coder == ERROR_MARK)
8481 347 : return error_mark_node;
8482 :
8483 167045246 : if (c_dialect_objc ())
8484 : {
8485 0 : int parmno;
8486 :
8487 0 : switch (errtype)
8488 : {
8489 : case ic_return:
8490 : parmno = 0;
8491 : break;
8492 :
8493 : case ic_assign:
8494 : parmno = -1;
8495 : break;
8496 :
8497 : case ic_init:
8498 : case ic_init_const:
8499 : parmno = -2;
8500 : break;
8501 :
8502 : default:
8503 0 : parmno = parmnum;
8504 : break;
8505 : }
8506 :
8507 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8508 : }
8509 :
8510 167045246 : if (warn_cxx_compat)
8511 : {
8512 28859 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8513 28859 : if (checktype != error_mark_node
8514 28859 : && TREE_CODE (type) == ENUMERAL_TYPE
8515 28965 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8516 45 : switch (errtype)
8517 : {
8518 8 : case ic_argpass:
8519 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8520 : "passing argument %d of %qE is invalid in C++",
8521 : parmnum, rname))
8522 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8523 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8524 : "expected %qT but argument is of type %qT",
8525 : type, rhstype);
8526 : break;
8527 10 : case ic_assign:
8528 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8529 : "%qT in assignment is invalid in C++", rhstype, type);
8530 10 : break;
8531 18 : case ic_init:
8532 18 : case ic_init_const:
8533 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8534 : "%qT to %qT in initialization is invalid in C++",
8535 : rhstype, type);
8536 18 : break;
8537 9 : case ic_return:
8538 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8539 : "%qT in return is invalid in C++", rhstype, type);
8540 9 : break;
8541 0 : default:
8542 0 : gcc_unreachable ();
8543 : }
8544 : }
8545 :
8546 167045246 : if (warn_enum_conversion)
8547 : {
8548 14587102 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8549 14587102 : if (checktype != error_mark_node
8550 14587102 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8551 12729 : && TREE_CODE (type) == ENUMERAL_TYPE
8552 14595298 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8553 : {
8554 7 : gcc_rich_location loc (location);
8555 7 : warning_at (&loc, OPT_Wenum_conversion,
8556 : "implicit conversion from %qT to %qT",
8557 : checktype, type);
8558 7 : }
8559 : }
8560 :
8561 167045246 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8562 : {
8563 147947864 : warn_for_address_of_packed_member (type, orig_rhs);
8564 147947864 : if (type != rhstype)
8565 : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8566 36861710 : rhs = convert (type, rhs);
8567 147947864 : return rhs;
8568 : }
8569 :
8570 19097382 : if (coder == VOID_TYPE)
8571 : {
8572 : /* Except for passing an argument to an unprototyped function,
8573 : this is a constraint violation. When passing an argument to
8574 : an unprototyped function, it is compile-time undefined;
8575 : making it a constraint in that case was rejected in
8576 : DR#252. */
8577 8 : const char msg[] = "void value not ignored as it ought to be";
8578 8 : if (warnopt)
8579 0 : warning_at (location, warnopt, msg);
8580 : else
8581 8 : error_at (location, msg);
8582 8 : return error_mark_node;
8583 : }
8584 19097374 : rhs = require_complete_type (location, rhs);
8585 19097374 : if (rhs == error_mark_node)
8586 : return error_mark_node;
8587 :
8588 19097374 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8589 6 : return error_mark_node;
8590 :
8591 : /* A non-reference type can convert to a reference. This handles
8592 : va_start, va_copy and possibly port built-ins. */
8593 19097368 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8594 : {
8595 286 : if (!lvalue_p (rhs))
8596 : {
8597 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8598 0 : if (warnopt)
8599 0 : warning_at (location, warnopt, msg);
8600 : else
8601 0 : error_at (location, msg);
8602 0 : return error_mark_node;
8603 : }
8604 286 : if (!c_mark_addressable (rhs))
8605 0 : return error_mark_node;
8606 286 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8607 286 : SET_EXPR_LOCATION (rhs, location);
8608 :
8609 286 : rhs = convert_for_assignment (location, expr_loc,
8610 286 : c_build_pointer_type (TREE_TYPE (type)),
8611 : rhs, origtype, errtype,
8612 : null_pointer_constant, fundecl, function,
8613 : parmnum, warnopt);
8614 286 : if (rhs == error_mark_node)
8615 : return error_mark_node;
8616 :
8617 286 : rhs = build1 (NOP_EXPR, type, rhs);
8618 286 : SET_EXPR_LOCATION (rhs, location);
8619 286 : return rhs;
8620 : }
8621 : /* Some types can interconvert without explicit casts. */
8622 19097082 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8623 19097082 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8624 9287053 : return convert (type, rhs);
8625 : /* Arithmetic types all interconvert, and enum is treated like int. */
8626 9810029 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8627 2276837 : || codel == FIXED_POINT_TYPE
8628 2276837 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8629 2190605 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8630 7713471 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8631 83229 : || coder == FIXED_POINT_TYPE
8632 83229 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8633 40173 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8634 : {
8635 7712352 : if (warnopt && errtype == ic_argpass)
8636 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8637 : rhstype);
8638 :
8639 7712352 : bool save = in_late_binary_op;
8640 7681150 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8641 15336354 : || (coder == REAL_TYPE
8642 275981 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8643 24196 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8644 95094 : in_late_binary_op = true;
8645 11089738 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8646 : ? expr_loc : location, type, orig_rhs,
8647 : errtype == ic_init_const);
8648 7712352 : in_late_binary_op = save;
8649 7712352 : return ret;
8650 : }
8651 :
8652 : /* Aggregates in different TUs might need conversion. */
8653 2097677 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8654 105 : && codel == coder
8655 2097699 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8656 9 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8657 9 : ? expr_loc : location, type, rhs);
8658 :
8659 : /* Conversion to a transparent union or record from its member types.
8660 : This applies only to function arguments. */
8661 2097668 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8662 96 : && TYPE_TRANSPARENT_AGGR (type))
8663 2097707 : && errtype == ic_argpass)
8664 : {
8665 39 : tree memb, marginal_memb = NULL_TREE;
8666 :
8667 50 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8668 : {
8669 50 : tree memb_type = TREE_TYPE (memb);
8670 :
8671 50 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8672 50 : TYPE_MAIN_VARIANT (rhstype)))
8673 : break;
8674 :
8675 27 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8676 1 : continue;
8677 :
8678 26 : if (coder == POINTER_TYPE)
8679 : {
8680 26 : tree ttl = TREE_TYPE (memb_type);
8681 26 : tree ttr = TREE_TYPE (rhstype);
8682 :
8683 : /* Any non-function converts to a [const][volatile] void *
8684 : and vice versa; otherwise, targets must be the same.
8685 : Meanwhile, the lhs target must have all the qualifiers of
8686 : the rhs. */
8687 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8688 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8689 45 : || comp_target_types (location, memb_type, rhstype))
8690 : {
8691 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8692 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8693 : /* If this type won't generate any warnings, use it. */
8694 16 : if (lquals == rquals
8695 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8696 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8697 0 : ? ((lquals | rquals) == rquals)
8698 16 : : ((lquals | rquals) == lquals)))
8699 : break;
8700 :
8701 : /* Keep looking for a better type, but remember this one. */
8702 0 : if (!marginal_memb)
8703 10 : marginal_memb = memb;
8704 : }
8705 : }
8706 :
8707 : /* Can convert integer zero to any pointer type. */
8708 10 : if (null_pointer_constant)
8709 : {
8710 0 : rhs = null_pointer_node;
8711 0 : break;
8712 : }
8713 : }
8714 :
8715 39 : if (memb || marginal_memb)
8716 : {
8717 39 : if (!memb)
8718 : {
8719 : /* We have only a marginally acceptable member type;
8720 : it needs a warning. */
8721 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8722 0 : tree ttr = TREE_TYPE (rhstype);
8723 :
8724 : /* Const and volatile mean something different for function
8725 : types, so the usual warnings are not appropriate. */
8726 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8727 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8728 : {
8729 : /* Because const and volatile on functions are
8730 : restrictions that say the function will not do
8731 : certain things, it is okay to use a const or volatile
8732 : function where an ordinary one is wanted, but not
8733 : vice-versa. */
8734 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8735 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8736 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8737 : OPT_Wdiscarded_qualifiers,
8738 : G_("passing argument %d of %qE "
8739 : "makes %q#v qualified function "
8740 : "pointer from unqualified"),
8741 : G_("assignment makes %q#v qualified "
8742 : "function pointer from "
8743 : "unqualified"),
8744 : G_("initialization makes %q#v qualified "
8745 : "function pointer from "
8746 : "unqualified"),
8747 : G_("return makes %q#v qualified function "
8748 : "pointer from unqualified"),
8749 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8750 : }
8751 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8752 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8753 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8754 : OPT_Wdiscarded_qualifiers,
8755 : G_("passing argument %d of %qE discards "
8756 : "%qv qualifier from pointer target type"),
8757 : G_("assignment discards %qv qualifier "
8758 : "from pointer target type"),
8759 : G_("initialization discards %qv qualifier "
8760 : "from pointer target type"),
8761 : G_("return discards %qv qualifier from "
8762 : "pointer target type"),
8763 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8764 :
8765 : memb = marginal_memb;
8766 : }
8767 :
8768 39 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8769 31 : pedwarn (location, OPT_Wpedantic,
8770 : "ISO C prohibits argument conversion to union type");
8771 :
8772 39 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8773 39 : return build_constructor_single (type, memb, rhs);
8774 : }
8775 : }
8776 :
8777 : /* Conversions among pointers */
8778 2097629 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8779 2096354 : && (coder == codel))
8780 : {
8781 : /* If RHS refers to a built-in declared without a prototype
8782 : BLTIN is the declaration of the built-in with a prototype
8783 : and RHSTYPE is set to the actual type of the built-in. */
8784 2018045 : tree bltin;
8785 2018045 : rhstype = type_or_builtin_type (rhs, &bltin);
8786 :
8787 2018045 : tree ttl = TREE_TYPE (type);
8788 2018045 : tree ttr = TREE_TYPE (rhstype);
8789 2018045 : bool is_opaque_pointer;
8790 2018045 : bool target_cmp = false; /* Cache comp_target_types () result. */
8791 2018045 : addr_space_t asl;
8792 2018045 : addr_space_t asr;
8793 :
8794 2018045 : tree mvl = remove_qualifiers (ttl);
8795 2018045 : tree mvr = remove_qualifiers (ttr);
8796 :
8797 : /* Opaque pointers are treated like void pointers. */
8798 2018045 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8799 :
8800 : /* The Plan 9 compiler permits a pointer to a struct to be
8801 : automatically converted into a pointer to an anonymous field
8802 : within the struct. */
8803 2018045 : if (flag_plan9_extensions
8804 50 : && RECORD_OR_UNION_TYPE_P (mvl)
8805 26 : && RECORD_OR_UNION_TYPE_P (mvr)
8806 26 : && mvl != mvr)
8807 : {
8808 26 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8809 26 : if (new_rhs != NULL_TREE)
8810 : {
8811 22 : rhs = new_rhs;
8812 22 : rhstype = TREE_TYPE (rhs);
8813 22 : coder = TREE_CODE (rhstype);
8814 22 : ttr = TREE_TYPE (rhstype);
8815 22 : mvr = TYPE_MAIN_VARIANT (ttr);
8816 : }
8817 : }
8818 :
8819 : /* C++ does not allow the implicit conversion void* -> T*. However,
8820 : for the purpose of reducing the number of false positives, we
8821 : tolerate the special case of
8822 :
8823 : int *p = NULL;
8824 :
8825 : where NULL is typically defined in C to be '(void *) 0'. */
8826 2018045 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8827 26840 : warning_at (errtype == ic_argpass ? expr_loc : location,
8828 14230 : OPT_Wc___compat,
8829 : "request for implicit conversion "
8830 : "from %qT to %qT not permitted in C++", rhstype, type);
8831 :
8832 : /* Warn of new allocations that are not big enough for the target
8833 : type. */
8834 2018045 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8835 5455 : if (tree fndecl = get_callee_fndecl (rhs))
8836 5455 : if (DECL_IS_MALLOC (fndecl))
8837 : {
8838 4599 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8839 4599 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8840 4599 : if (alloc_size)
8841 445 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8842 : }
8843 :
8844 : /* See if the pointers point to incompatible address spaces. */
8845 2018045 : asl = TYPE_ADDR_SPACE (ttl);
8846 2018045 : asr = TYPE_ADDR_SPACE (ttr);
8847 2018045 : if (!null_pointer_constant_p (rhs)
8848 2018045 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8849 : {
8850 3 : auto_diagnostic_group d;
8851 3 : bool diagnosed = true;
8852 3 : switch (errtype)
8853 : {
8854 1 : case ic_argpass:
8855 1 : {
8856 1 : const char msg[] = G_("passing argument %d of %qE from "
8857 : "pointer to non-enclosed address space");
8858 1 : if (warnopt)
8859 0 : diagnosed
8860 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8861 : else
8862 1 : error_at (expr_loc, msg, parmnum, rname);
8863 0 : break;
8864 : }
8865 0 : case ic_assign:
8866 0 : {
8867 0 : const char msg[] = G_("assignment from pointer to "
8868 : "non-enclosed address space");
8869 0 : if (warnopt)
8870 0 : diagnosed = warning_at (location, warnopt, msg);
8871 : else
8872 0 : error_at (location, msg);
8873 0 : break;
8874 : }
8875 0 : case ic_init:
8876 0 : case ic_init_const:
8877 0 : {
8878 0 : const char msg[] = G_("initialization from pointer to "
8879 : "non-enclosed address space");
8880 0 : if (warnopt)
8881 0 : diagnosed = warning_at (location, warnopt, msg);
8882 : else
8883 0 : error_at (location, msg);
8884 0 : break;
8885 : }
8886 2 : case ic_return:
8887 2 : {
8888 2 : const char msg[] = G_("return from pointer to "
8889 : "non-enclosed address space");
8890 2 : if (warnopt)
8891 0 : diagnosed = warning_at (location, warnopt, msg);
8892 : else
8893 2 : error_at (location, msg);
8894 0 : break;
8895 : }
8896 0 : default:
8897 0 : gcc_unreachable ();
8898 : }
8899 3 : if (diagnosed)
8900 : {
8901 3 : if (errtype == ic_argpass)
8902 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8903 : else
8904 2 : inform (location, "expected %qT but pointer is of type %qT",
8905 : type, rhstype);
8906 : }
8907 3 : return error_mark_node;
8908 3 : }
8909 :
8910 : /* Check if the right-hand side has a format attribute but the
8911 : left-hand side doesn't. */
8912 2018042 : if (warn_suggest_attribute_format
8913 2018042 : && check_missing_format_attribute (type, rhstype))
8914 : {
8915 16 : switch (errtype)
8916 : {
8917 4 : case ic_argpass:
8918 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8919 : "argument %d of %qE might be "
8920 : "a candidate for a format attribute",
8921 : parmnum, rname);
8922 4 : break;
8923 4 : case ic_assign:
8924 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8925 : "assignment left-hand side might be "
8926 : "a candidate for a format attribute");
8927 4 : break;
8928 4 : case ic_init:
8929 4 : case ic_init_const:
8930 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8931 : "initialization left-hand side might be "
8932 : "a candidate for a format attribute");
8933 4 : break;
8934 4 : case ic_return:
8935 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8936 : "return type might be "
8937 : "a candidate for a format attribute");
8938 4 : break;
8939 0 : default:
8940 0 : gcc_unreachable ();
8941 : }
8942 : }
8943 :
8944 : /* See if the pointers point to incompatible scalar storage orders. */
8945 2018042 : if (warn_scalar_storage_order
8946 2015126 : && !null_pointer_constant_p (rhs)
8947 5981920 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8948 1981939 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8949 : {
8950 18 : tree t;
8951 :
8952 18 : switch (errtype)
8953 : {
8954 11 : case ic_argpass:
8955 : /* Do not warn for built-in functions, for example memcpy, since we
8956 : control how they behave and they can be useful in this area. */
8957 11 : if (TREE_CODE (rname) != FUNCTION_DECL
8958 11 : || !fndecl_built_in_p (rname))
8959 1 : warning_at (location, OPT_Wscalar_storage_order,
8960 : "passing argument %d of %qE from incompatible "
8961 : "scalar storage order", parmnum, rname);
8962 : break;
8963 3 : case ic_assign:
8964 : /* Do not warn if the RHS is a call to a function that returns a
8965 : pointer that is not an alias. */
8966 3 : if (TREE_CODE (rhs) != CALL_EXPR
8967 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8968 5 : || !DECL_IS_MALLOC (t))
8969 1 : warning_at (location, OPT_Wscalar_storage_order,
8970 : "assignment to %qT from pointer type %qT with "
8971 : "incompatible scalar storage order", type, rhstype);
8972 : break;
8973 3 : case ic_init:
8974 3 : case ic_init_const:
8975 : /* Likewise. */
8976 3 : if (TREE_CODE (rhs) != CALL_EXPR
8977 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8978 5 : || !DECL_IS_MALLOC (t))
8979 1 : warning_at (location, OPT_Wscalar_storage_order,
8980 : "initialization of %qT from pointer type %qT with "
8981 : "incompatible scalar storage order", type, rhstype);
8982 : break;
8983 1 : case ic_return:
8984 1 : warning_at (location, OPT_Wscalar_storage_order,
8985 : "returning %qT from pointer type with incompatible "
8986 : "scalar storage order %qT", rhstype, type);
8987 1 : break;
8988 0 : default:
8989 0 : gcc_unreachable ();
8990 : }
8991 : }
8992 :
8993 : /* Any non-function converts to a [const][volatile] void *
8994 : and vice versa; otherwise, targets must be the same.
8995 : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
8996 423323 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8997 1594720 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8998 1547440 : || (target_cmp = comp_target_types (location, type, rhstype))
8999 2446 : || is_opaque_pointer
9000 2022934 : || ((c_common_unsigned_type (mvl)
9001 2446 : == c_common_unsigned_type (mvr))
9002 3340 : && (c_common_signed_type (mvl)
9003 1670 : == c_common_signed_type (mvr))
9004 1655 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
9005 : {
9006 : /* Warn about loss of qualifers from pointers to arrays with
9007 : qualifiers on the element type. */
9008 2017243 : if (TREE_CODE (ttr) == ARRAY_TYPE)
9009 : {
9010 2131 : ttr = strip_array_types (ttr);
9011 2131 : ttl = strip_array_types (ttl);
9012 :
9013 2131 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9014 2131 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
9015 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
9016 : location, expr_loc,
9017 : OPT_Wdiscarded_array_qualifiers,
9018 : G_("passing argument %d of %qE discards "
9019 : "%qv qualifier from pointer target type"),
9020 : G_("assignment discards %qv qualifier "
9021 : "from pointer target type"),
9022 : G_("initialization discards %qv qualifier "
9023 : "from pointer target type"),
9024 : G_("return discards %qv qualifier from "
9025 : "pointer target type"),
9026 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9027 : }
9028 2015112 : else if (pedantic
9029 146373 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
9030 146353 : ||
9031 : (VOID_TYPE_P (ttr)
9032 2953 : && !null_pointer_constant
9033 178 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
9034 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
9035 : G_("ISO C forbids passing argument %d of "
9036 : "%qE between function pointer "
9037 : "and %<void *%>"),
9038 : G_("ISO C forbids assignment between "
9039 : "function pointer and %<void *%>"),
9040 : G_("ISO C forbids initialization between "
9041 : "function pointer and %<void *%>"),
9042 : G_("ISO C forbids return between function "
9043 : "pointer and %<void *%>"));
9044 : /* Const and volatile mean something different for function types,
9045 : so the usual warnings are not appropriate. */
9046 2015078 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
9047 1982662 : && TREE_CODE (ttl) != FUNCTION_TYPE)
9048 : {
9049 : /* Assignments between atomic and non-atomic objects are OK. */
9050 1981965 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9051 1981965 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
9052 1981965 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9053 1981965 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
9054 :
9055 : /* Don't warn about loss of qualifier for conversions from
9056 : qualified void* to pointers to arrays with corresponding
9057 : qualifier on the element type (except for pedantic before C23). */
9058 1981965 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
9059 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9060 : OPT_Wdiscarded_qualifiers,
9061 : G_("passing argument %d of %qE discards "
9062 : "%qv qualifier from pointer target type"),
9063 : G_("assignment discards %qv qualifier "
9064 : "from pointer target type"),
9065 : G_("initialization discards %qv qualifier "
9066 : "from pointer target type"),
9067 : G_("return discards %qv qualifier from "
9068 : "pointer target type"),
9069 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9070 11 : else if (warn_quals_ped)
9071 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
9072 : "array with qualifier on the element is not qualified before C23");
9073 :
9074 : /* If this is not a case of ignoring a mismatch in signedness,
9075 : no warning. */
9076 1981257 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
9077 1542520 : || target_cmp)
9078 : ;
9079 : /* If there is a mismatch, do warn. */
9080 1647 : else if (warn_pointer_sign)
9081 82 : switch (errtype)
9082 : {
9083 33 : case ic_argpass:
9084 33 : {
9085 33 : auto_diagnostic_group d;
9086 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9087 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9088 33 : highlight_colors::actual);
9089 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
9090 : "pointer targets in passing argument %d of "
9091 : "%qE differ in signedness", parmnum, rname))
9092 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
9093 : rhstype);
9094 33 : }
9095 33 : break;
9096 21 : case ic_assign:
9097 21 : pedwarn (location, OPT_Wpointer_sign,
9098 : "pointer targets in assignment from %qT to %qT "
9099 : "differ in signedness", rhstype, type);
9100 21 : break;
9101 14 : case ic_init:
9102 14 : case ic_init_const:
9103 14 : pedwarn_init (location, OPT_Wpointer_sign,
9104 : "pointer targets in initialization of %qT "
9105 : "from %qT differ in signedness", type,
9106 : rhstype);
9107 14 : break;
9108 14 : case ic_return:
9109 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
9110 : "returning %qT from a function with return type "
9111 : "%qT differ in signedness", rhstype, type);
9112 14 : break;
9113 0 : default:
9114 0 : gcc_unreachable ();
9115 : }
9116 : }
9117 33113 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
9118 4147 : && TREE_CODE (ttr) == FUNCTION_TYPE)
9119 : {
9120 : /* Because const and volatile on functions are restrictions
9121 : that say the function will not do certain things,
9122 : it is okay to use a const or volatile function
9123 : where an ordinary one is wanted, but not vice-versa. */
9124 3450 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
9125 3450 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
9126 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9127 : OPT_Wdiscarded_qualifiers,
9128 : G_("passing argument %d of %qE makes "
9129 : "%q#v qualified function pointer "
9130 : "from unqualified"),
9131 : G_("assignment makes %q#v qualified function "
9132 : "pointer from unqualified"),
9133 : G_("initialization makes %q#v qualified "
9134 : "function pointer from unqualified"),
9135 : G_("return makes %q#v qualified function "
9136 : "pointer from unqualified"),
9137 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
9138 : }
9139 : }
9140 : /* Avoid warning about the volatile ObjC EH puts on decls. */
9141 799 : else if (!objc_ok)
9142 : {
9143 799 : auto_diagnostic_group d;
9144 799 : bool warned = false;
9145 799 : pp_markup::element_expected_type e_type (type);
9146 799 : pp_markup::element_actual_type e_rhstype (rhstype);
9147 799 : switch (errtype)
9148 : {
9149 306 : case ic_argpass:
9150 306 : {
9151 306 : range_label_for_type_mismatch rhs_label (rhstype, type);
9152 306 : gcc_rich_location richloc (expr_loc, &rhs_label,
9153 306 : highlight_colors::actual);
9154 306 : warned
9155 306 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9156 : "passing argument %d of %qE from "
9157 : "incompatible pointer type",
9158 : parmnum, rname);
9159 306 : if (warned)
9160 174 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9161 306 : }
9162 306 : break;
9163 267 : case ic_assign:
9164 267 : if (bltin)
9165 11 : warned
9166 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9167 : "assignment to %e from pointer to "
9168 : "%qD with incompatible type %e",
9169 : &e_type, bltin, &e_rhstype);
9170 : else
9171 256 : warned
9172 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9173 : "assignment to %e from incompatible "
9174 : "pointer type %e",
9175 : &e_type, &e_rhstype);
9176 : break;
9177 167 : case ic_init:
9178 167 : case ic_init_const:
9179 167 : if (bltin)
9180 13 : warned
9181 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9182 : "initialization of %e from pointer to "
9183 : "%qD with incompatible type %e",
9184 : &e_type, bltin, &e_rhstype);
9185 : else
9186 154 : warned
9187 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9188 : "initialization of %e from incompatible "
9189 : "pointer type %e",
9190 : &e_type, &e_rhstype);
9191 : break;
9192 59 : case ic_return:
9193 59 : if (bltin)
9194 11 : warned
9195 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9196 : "returning pointer to %qD of type %e from "
9197 : "a function with incompatible type %e",
9198 : bltin, &e_rhstype, &e_type);
9199 : else
9200 48 : warned
9201 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9202 : "returning %e from a function with "
9203 : "incompatible return type %e",
9204 : &e_rhstype, &e_type);
9205 : break;
9206 0 : default:
9207 0 : gcc_unreachable ();
9208 : }
9209 799 : if (warned)
9210 : {
9211 : /* If the mismatching function type is a pointer to a function,
9212 : try to show the decl of the function. */
9213 589 : if (TREE_CODE (rhs) == ADDR_EXPR
9214 589 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9215 : {
9216 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9217 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9218 : {
9219 165 : gcc_rich_location richloc
9220 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9221 165 : highlight_colors::actual);
9222 165 : pp_element_quoted_decl e_rhs_fndecl
9223 165 : (rhs_fndecl, highlight_colors::actual);
9224 165 : inform (&richloc,
9225 : "%e declared here", &e_rhs_fndecl);
9226 165 : }
9227 : }
9228 : /* If either/both of the types are typedefs, show the decl. */
9229 589 : maybe_inform_typedef_location (type,
9230 : highlight_colors::expected);
9231 589 : maybe_inform_typedef_location (rhstype,
9232 : highlight_colors::actual);
9233 : }
9234 799 : }
9235 :
9236 : /* If RHS isn't an address, check pointer or array of packed
9237 : struct or union. */
9238 2018042 : warn_for_address_of_packed_member (type, orig_rhs);
9239 :
9240 2018042 : return convert (type, rhs);
9241 : }
9242 79584 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9243 : {
9244 : /* ??? This should not be an error when inlining calls to
9245 : unprototyped functions. */
9246 4 : const char msg[] = "invalid use of non-lvalue array";
9247 4 : if (warnopt)
9248 0 : warning_at (location, warnopt, msg);
9249 : else
9250 4 : error_at (location, msg);
9251 4 : return error_mark_node;
9252 : }
9253 79580 : else if (codel == POINTER_TYPE
9254 78305 : && (coder == INTEGER_TYPE
9255 78305 : || coder == ENUMERAL_TYPE
9256 615 : || coder == BOOLEAN_TYPE
9257 615 : || coder == NULLPTR_TYPE
9258 27 : || coder == BITINT_TYPE))
9259 : {
9260 78292 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9261 77294 : && coder != NULLPTR_TYPE)
9262 76735 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9263 : "zero as null pointer constant");
9264 : /* A NULLPTR type is just a nullptr always. */
9265 77733 : if (coder == NULLPTR_TYPE)
9266 574 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9267 : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9268 : or one that results from arithmetic, even including a cast to
9269 : integer type. */
9270 77718 : else if (!null_pointer_constant)
9271 972 : switch (errtype)
9272 : {
9273 800 : case ic_argpass:
9274 800 : {
9275 800 : auto_diagnostic_group d;
9276 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9277 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9278 800 : highlight_colors::actual);
9279 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9280 : "passing argument %d of %qE makes pointer "
9281 : "from integer without a cast", parmnum, rname))
9282 : {
9283 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9284 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9285 : }
9286 800 : }
9287 800 : break;
9288 93 : case ic_assign:
9289 93 : permerror_opt (location, OPT_Wint_conversion,
9290 : "assignment to %qT from %qT makes pointer from "
9291 : "integer without a cast", type, rhstype);
9292 93 : break;
9293 56 : case ic_init:
9294 56 : case ic_init_const:
9295 56 : permerror_init (location, OPT_Wint_conversion,
9296 : "initialization of %qT from %qT makes pointer "
9297 : "from integer without a cast", type, rhstype);
9298 56 : break;
9299 23 : case ic_return:
9300 23 : permerror_init (location, OPT_Wint_conversion,
9301 : "returning %qT from a function with return type "
9302 : "%qT makes pointer from integer without a cast",
9303 : rhstype, type);
9304 23 : break;
9305 0 : default:
9306 0 : gcc_unreachable ();
9307 : }
9308 :
9309 77718 : return convert (type, rhs);
9310 : }
9311 1288 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9312 540 : && coder == POINTER_TYPE)
9313 : {
9314 514 : switch (errtype)
9315 : {
9316 344 : case ic_argpass:
9317 344 : {
9318 344 : auto_diagnostic_group d;
9319 344 : range_label_for_type_mismatch rhs_label (rhstype, type);
9320 344 : gcc_rich_location richloc (expr_loc, &rhs_label,
9321 344 : highlight_colors::actual);
9322 344 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9323 : "passing argument %d of %qE makes integer from "
9324 : "pointer without a cast", parmnum, rname))
9325 : {
9326 323 : maybe_emit_indirection_note (expr_loc, rhs, type);
9327 323 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9328 : }
9329 344 : }
9330 344 : break;
9331 52 : case ic_assign:
9332 52 : permerror_opt (location, OPT_Wint_conversion,
9333 : "assignment to %qT from %qT makes integer from "
9334 : "pointer without a cast", type, rhstype);
9335 52 : break;
9336 83 : case ic_init:
9337 83 : case ic_init_const:
9338 83 : permerror_init (location, OPT_Wint_conversion,
9339 : "initialization of %qT from %qT makes integer "
9340 : "from pointer without a cast", type, rhstype);
9341 83 : break;
9342 35 : case ic_return:
9343 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9344 : "function with return type %qT makes integer from "
9345 : "pointer without a cast", rhstype, type);
9346 35 : break;
9347 0 : default:
9348 0 : gcc_unreachable ();
9349 : }
9350 :
9351 514 : return convert (type, rhs);
9352 : }
9353 626 : else if (C_BOOLEAN_TYPE_P (type)
9354 : /* The type nullptr_t may be converted to bool. The
9355 : result is false. */
9356 776 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9357 : {
9358 150 : tree ret;
9359 150 : bool save = in_late_binary_op;
9360 150 : in_late_binary_op = true;
9361 150 : ret = convert (type, rhs);
9362 150 : in_late_binary_op = save;
9363 150 : return ret;
9364 : }
9365 624 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9366 6 : return convert (type, rhs);
9367 :
9368 618 : switch (errtype)
9369 : {
9370 35 : case ic_argpass:
9371 35 : {
9372 35 : auto_diagnostic_group d;
9373 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9374 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9375 35 : highlight_colors::actual);
9376 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9377 35 : if (warnopt)
9378 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9379 : else
9380 27 : error_at (&richloc, msg, parmnum, rname);
9381 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9382 35 : }
9383 35 : break;
9384 108 : case ic_assign:
9385 108 : {
9386 108 : const char msg[]
9387 : = G_("incompatible types when assigning to type %qT from type %qT");
9388 108 : if (warnopt)
9389 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9390 : else
9391 108 : error_at (expr_loc, msg, type, rhstype);
9392 108 : break;
9393 : }
9394 460 : case ic_init:
9395 460 : case ic_init_const:
9396 460 : {
9397 460 : const char msg[]
9398 : = G_("incompatible types when initializing type %qT using type %qT");
9399 460 : if (warnopt)
9400 0 : warning_at (location, 0, msg, type, rhstype);
9401 : else
9402 460 : error_at (location, msg, type, rhstype);
9403 460 : break;
9404 : }
9405 15 : case ic_return:
9406 15 : {
9407 15 : const char msg[]
9408 : = G_("incompatible types when returning type %qT but %qT was expected");
9409 15 : if (warnopt)
9410 0 : warning_at (location, 0, msg, rhstype, type);
9411 : else
9412 15 : error_at (location, msg, rhstype, type);
9413 15 : break;
9414 : }
9415 0 : default:
9416 0 : gcc_unreachable ();
9417 : }
9418 :
9419 618 : return error_mark_node;
9420 : }
9421 :
9422 : /* If VALUE is a compound expr all of whose expressions are constant, then
9423 : return its value. Otherwise, return error_mark_node.
9424 :
9425 : This is for handling COMPOUND_EXPRs as initializer elements
9426 : which is allowed with a warning when -pedantic is specified. */
9427 :
9428 : static tree
9429 2 : valid_compound_expr_initializer (tree value, tree endtype)
9430 : {
9431 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9432 : {
9433 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9434 1 : == error_mark_node)
9435 : return error_mark_node;
9436 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9437 0 : endtype);
9438 : }
9439 1 : else if (!initializer_constant_valid_p (value, endtype))
9440 1 : return error_mark_node;
9441 : else
9442 : return value;
9443 : }
9444 :
9445 : /* Perform appropriate conversions on the initial value of a variable,
9446 : store it in the declaration DECL,
9447 : and print any error messages that are appropriate.
9448 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9449 : If the init is invalid, store an ERROR_MARK.
9450 :
9451 : INIT_LOC is the location of the initial value. */
9452 :
9453 : void
9454 7271851 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9455 : {
9456 7271851 : tree value, type;
9457 7271851 : bool npc = false;
9458 7271851 : bool int_const_expr = false;
9459 7271851 : bool arith_const_expr = false;
9460 :
9461 : /* If variable's type was invalidly declared, just ignore it. */
9462 :
9463 7271851 : type = TREE_TYPE (decl);
9464 7271851 : if (TREE_CODE (type) == ERROR_MARK)
9465 : return;
9466 :
9467 : /* Digest the specified initializer into an expression. */
9468 :
9469 7271843 : if (init)
9470 : {
9471 7271840 : npc = null_pointer_constant_p (init);
9472 7271840 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9473 419714 : && !TREE_OVERFLOW (init)
9474 7691518 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9475 : /* Not fully determined before folding. */
9476 : arith_const_expr = true;
9477 : }
9478 7271843 : bool constexpr_p = (VAR_P (decl)
9479 7271843 : && C_DECL_DECLARED_CONSTEXPR (decl));
9480 7271843 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9481 : int_const_expr, arith_const_expr, true,
9482 7271843 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9483 :
9484 : /* Store the expression if valid; else report error. */
9485 :
9486 7271843 : if (!in_system_header_at (input_location)
9487 7271843 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9488 28274 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9489 : "aggregate initialization");
9490 :
9491 7271843 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9492 7271842 : DECL_INITIAL (decl) = value;
9493 :
9494 : /* ANSI wants warnings about out-of-range constant initializers. */
9495 7271854 : STRIP_TYPE_NOPS (value);
9496 7271843 : if (TREE_STATIC (decl))
9497 179814 : constant_expression_warning (value);
9498 :
9499 : /* Check if we need to set array size from compound literal size. */
9500 7271843 : if (TREE_CODE (type) == ARRAY_TYPE
9501 26441 : && TYPE_DOMAIN (type) == NULL_TREE
9502 7284377 : && value != error_mark_node)
9503 : {
9504 : tree inside_init = init;
9505 :
9506 11662 : STRIP_TYPE_NOPS (inside_init);
9507 11662 : inside_init = fold (inside_init);
9508 :
9509 11662 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9510 : {
9511 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9512 :
9513 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9514 : {
9515 : /* For int foo[] = (int [3]){1}; we need to set array size
9516 : now since later on array initializer will be just the
9517 : brace enclosed list of the compound literal. */
9518 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9519 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9520 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9521 13 : layout_type (type);
9522 13 : layout_decl (cldecl, 0);
9523 13 : TREE_TYPE (decl)
9524 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9525 : }
9526 : }
9527 : }
9528 : }
9529 :
9530 : /* Methods for storing and printing names for error messages. */
9531 :
9532 : /* Implement a spelling stack that allows components of a name to be pushed
9533 : and popped. Each element on the stack is this structure. */
9534 :
9535 : struct spelling
9536 : {
9537 : int kind;
9538 : union
9539 : {
9540 : unsigned HOST_WIDE_INT i;
9541 : const char *s;
9542 : } u;
9543 : };
9544 :
9545 : #define SPELLING_STRING 1
9546 : #define SPELLING_MEMBER 2
9547 : #define SPELLING_BOUNDS 3
9548 :
9549 : static struct spelling *spelling; /* Next stack element (unused). */
9550 : static struct spelling *spelling_base; /* Spelling stack base. */
9551 : static int spelling_size; /* Size of the spelling stack. */
9552 :
9553 : /* Macros to save and restore the spelling stack around push_... functions.
9554 : Alternative to SAVE_SPELLING_STACK. */
9555 :
9556 : #define SPELLING_DEPTH() (spelling - spelling_base)
9557 : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9558 :
9559 : /* Push an element on the spelling stack with type KIND and assign VALUE
9560 : to MEMBER. */
9561 :
9562 : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9563 : { \
9564 : int depth = SPELLING_DEPTH (); \
9565 : \
9566 : if (depth >= spelling_size) \
9567 : { \
9568 : spelling_size += 10; \
9569 : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9570 : spelling_size); \
9571 : RESTORE_SPELLING_DEPTH (depth); \
9572 : } \
9573 : \
9574 : spelling->kind = (KIND); \
9575 : spelling->MEMBER = (VALUE); \
9576 : spelling++; \
9577 : }
9578 :
9579 : /* Push STRING on the stack. Printed literally. */
9580 :
9581 : static void
9582 7269078 : push_string (const char *string)
9583 : {
9584 7269078 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9585 7269078 : }
9586 :
9587 : /* Push a member name on the stack. Printed as '.' STRING. */
9588 :
9589 : static void
9590 1226700 : push_member_name (tree decl)
9591 : {
9592 1226700 : const char *const string
9593 1226700 : = (DECL_NAME (decl)
9594 1226700 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9595 208 : : _("<anonymous>"));
9596 1226700 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9597 1226700 : }
9598 :
9599 : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9600 :
9601 : static void
9602 2601174 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9603 : {
9604 2601174 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9605 2601174 : }
9606 :
9607 : /* Compute the maximum size in bytes of the printed spelling. */
9608 :
9609 : static int
9610 2892 : spelling_length (void)
9611 : {
9612 2892 : int size = 0;
9613 2892 : struct spelling *p;
9614 :
9615 6416 : for (p = spelling_base; p < spelling; p++)
9616 : {
9617 3524 : if (p->kind == SPELLING_BOUNDS)
9618 1479 : size += 25;
9619 : else
9620 2045 : size += strlen (p->u.s) + 1;
9621 : }
9622 :
9623 2892 : return size;
9624 : }
9625 :
9626 : /* Print the spelling to BUFFER and return it. */
9627 :
9628 : static char *
9629 2892 : print_spelling (char *buffer)
9630 : {
9631 2892 : char *d = buffer;
9632 2892 : struct spelling *p;
9633 :
9634 6416 : for (p = spelling_base; p < spelling; p++)
9635 3524 : if (p->kind == SPELLING_BOUNDS)
9636 : {
9637 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9638 1479 : d += strlen (d);
9639 : }
9640 : else
9641 : {
9642 2045 : const char *s;
9643 2045 : if (p->kind == SPELLING_MEMBER)
9644 198 : *d++ = '.';
9645 16715 : for (s = p->u.s; (*d = *s++); d++)
9646 : ;
9647 : }
9648 2892 : *d++ = '\0';
9649 2892 : return buffer;
9650 : }
9651 :
9652 : /* Check whether INIT, a floating or integer constant, is
9653 : representable in TYPE, a real floating type with the same radix or
9654 : a decimal floating type initialized with a binary floating
9655 : constant. Return true if OK, false if not. */
9656 : static bool
9657 362 : constexpr_init_fits_real_type (tree type, tree init)
9658 : {
9659 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9660 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9661 362 : if (TREE_CODE (init) == REAL_CST
9662 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9663 : {
9664 : /* Same mode, no conversion required except for the case of
9665 : signaling NaNs if the types are incompatible (e.g. double and
9666 : long double with the same mode). */
9667 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9668 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9669 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9670 : return false;
9671 177 : return true;
9672 : }
9673 185 : if (TREE_CODE (init) == INTEGER_CST)
9674 : {
9675 54 : tree converted = build_real_from_int_cst (type, init);
9676 54 : bool fail = false;
9677 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9678 54 : TYPE_PRECISION (TREE_TYPE (init)));
9679 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9680 54 : }
9681 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9682 : return false;
9683 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9684 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9685 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9686 34 : && MODE_HAS_NANS (TYPE_MODE (type))))
9687 20 : return true;
9688 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9689 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9690 : {
9691 : /* This is valid if the real number represented by the
9692 : initializer can be exactly represented in the decimal
9693 : type. Compare the values using MPFR. */
9694 8 : REAL_VALUE_TYPE t;
9695 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9696 8 : mpfr_t bin_val, dec_val;
9697 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9698 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9699 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9700 8 : char string[256];
9701 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9702 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9703 8 : && mpfr_equal_p (bin_val, dec_val));
9704 8 : mpfr_clear (bin_val);
9705 8 : mpfr_clear (dec_val);
9706 8 : return res;
9707 : }
9708 : /* exact_real_truncate is not quite right here, since it doesn't
9709 : allow even an exact conversion to subnormal values. */
9710 86 : REAL_VALUE_TYPE t;
9711 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9712 86 : return real_identical (&t, &TREE_REAL_CST (init));
9713 : }
9714 :
9715 : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9716 : initializer for type TYPE, and give an error if not. INIT has
9717 : already been folded and verified to be constant. INT_CONST_EXPR
9718 : and ARITH_CONST_EXPR say whether it is an integer constant
9719 : expression or arithmetic constant expression, respectively. If
9720 : TYPE is not a scalar type, this function does nothing. */
9721 :
9722 : static void
9723 912 : check_constexpr_init (location_t loc, tree type, tree init,
9724 : bool int_const_expr, bool arith_const_expr)
9725 : {
9726 912 : if (POINTER_TYPE_P (type))
9727 : {
9728 : /* The initializer must be null. */
9729 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9730 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9731 86 : return;
9732 : }
9733 826 : if (INTEGRAL_TYPE_P (type))
9734 : {
9735 : /* The initializer must be an integer constant expression,
9736 : representable in the target type. */
9737 316 : if (!int_const_expr)
9738 : {
9739 13 : if (TREE_CODE (init) == RAW_DATA_CST
9740 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9741 : {
9742 4 : if (!TYPE_UNSIGNED (type))
9743 137 : for (unsigned int i = 0;
9744 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9745 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9746 : {
9747 1 : error_at (loc, "%<constexpr%> initializer not "
9748 : "representable in type of object");
9749 1 : break;
9750 : }
9751 : }
9752 : else
9753 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9754 : "integer constant expression");
9755 : }
9756 303 : else if (!int_fits_type_p (init, type))
9757 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9758 : "type of object");
9759 316 : return;
9760 : }
9761 : /* We don't apply any extra checks to extension types such as vector
9762 : or fixed-point types. */
9763 510 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9764 : return;
9765 353 : if (!arith_const_expr)
9766 : {
9767 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9768 : "constant expression");
9769 5 : return;
9770 : }
9771 : /* We don't apply any extra checks to complex integers. */
9772 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9773 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9774 : return;
9775 : /* Following N3082, a real type cannot be initialized from a complex
9776 : type and a binary type cannot be initialized from a decimal type
9777 : (but initializing a decimal type from a binary type is OK).
9778 : Signaling NaN initializers are OK only if the types are
9779 : compatible (not just the same mode); all quiet NaN and infinity
9780 : initializations are considered to preserve the value. */
9781 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9782 348 : && SCALAR_FLOAT_TYPE_P (type))
9783 : {
9784 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9785 : "complex type");
9786 6 : return;
9787 : }
9788 342 : if (SCALAR_FLOAT_TYPE_P (type)
9789 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9790 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9791 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9792 : {
9793 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9794 : "floating-point type is of decimal type");
9795 6 : return;
9796 : }
9797 336 : bool fits;
9798 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9799 : {
9800 42 : switch (TREE_CODE (init))
9801 : {
9802 10 : case INTEGER_CST:
9803 10 : case REAL_CST:
9804 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9805 10 : break;
9806 32 : case COMPLEX_CST:
9807 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9808 32 : TREE_REALPART (init))
9809 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9810 26 : TREE_IMAGPART (init)));
9811 : break;
9812 0 : default:
9813 0 : gcc_unreachable ();
9814 : }
9815 : }
9816 : else
9817 294 : fits = constexpr_init_fits_real_type (type, init);
9818 304 : if (!fits)
9819 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9820 : "type of object");
9821 : }
9822 :
9823 : /* Digest the parser output INIT as an initializer for type TYPE
9824 : initializing DECL.
9825 : Return a C expression of type TYPE to represent the initial value.
9826 :
9827 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9828 :
9829 : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9830 : INT_CONST_EXPR is true if INIT is an integer constant expression,
9831 : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9832 : constant expression, false if it has already been determined in the
9833 : caller that it is not (but folding may have made the value passed here
9834 : indistinguishable from an arithmetic constant expression).
9835 :
9836 : If INIT is a string constant, STRICT_STRING is true if it is
9837 : unparenthesized or we should not warn here for it being parenthesized.
9838 : For other types of INIT, STRICT_STRING is not used.
9839 :
9840 : INIT_LOC is the location of the INIT.
9841 :
9842 : REQUIRE_CONSTANT requests an error if non-constant initializers or
9843 : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9844 : on initializers for 'constexpr' objects apply. */
9845 :
9846 : static tree
9847 16894790 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9848 : tree origtype, bool null_pointer_constant, bool int_const_expr,
9849 : bool arith_const_expr, bool strict_string,
9850 : bool require_constant, bool require_constexpr)
9851 : {
9852 16894790 : enum tree_code code = TREE_CODE (type);
9853 16894790 : tree inside_init = init;
9854 16894790 : tree semantic_type = NULL_TREE;
9855 16894790 : bool maybe_const = true;
9856 :
9857 16894790 : if (type == error_mark_node
9858 16894790 : || !init
9859 33789577 : || error_operand_p (init))
9860 : return error_mark_node;
9861 :
9862 16902203 : STRIP_TYPE_NOPS (inside_init);
9863 :
9864 : /* If require_constant is TRUE, when the initializer is a call to
9865 : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9866 : For example:
9867 : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9868 : will be converted to
9869 : y = &static_annotated.c. */
9870 :
9871 16893651 : if (require_constant
9872 2890849 : && TREE_CODE (inside_init) == NOP_EXPR
9873 736235 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9874 16893653 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9875 2 : inside_init
9876 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9877 :
9878 16893651 : if (!c_in_omp_for)
9879 : {
9880 16888899 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9881 : {
9882 441 : semantic_type = TREE_TYPE (inside_init);
9883 441 : inside_init = TREE_OPERAND (inside_init, 0);
9884 : }
9885 16888899 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9886 : }
9887 : /* TODO: this may not detect all cases of expressions folding to
9888 : constants that are not arithmetic constant expressions. */
9889 16893651 : if (!maybe_const)
9890 : arith_const_expr = false;
9891 25207674 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9892 5653913 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9893 16823700 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9894 : arith_const_expr = false;
9895 8412532 : else if (TREE_CODE (inside_init) != INTEGER_CST
9896 : && TREE_CODE (inside_init) != REAL_CST
9897 : && TREE_CODE (inside_init) != COMPLEX_CST
9898 : && TREE_CODE (inside_init) != RAW_DATA_CST)
9899 : arith_const_expr = false;
9900 5073973 : else if (TREE_OVERFLOW (inside_init))
9901 11819722 : arith_const_expr = false;
9902 :
9903 : /* Initialization of an array of chars from a string constant
9904 : optionally enclosed in braces. */
9905 :
9906 16893651 : if (code == ARRAY_TYPE && inside_init
9907 188407 : && TREE_CODE (inside_init) == STRING_CST)
9908 : {
9909 11774 : tree typ1
9910 11774 : = (TYPE_ATOMIC (TREE_TYPE (type))
9911 11774 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9912 : TYPE_QUAL_ATOMIC)
9913 11760 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9914 : /* Note that an array could be both an array of character type
9915 : and an array of wchar_t if wchar_t is signed char or unsigned
9916 : char. */
9917 23548 : bool char_array = (typ1 == char_type_node
9918 516 : || typ1 == signed_char_type_node
9919 12255 : || typ1 == unsigned_char_type_node);
9920 11774 : bool wchar_array = !!comptypes (typ1, wchar_type_node);
9921 11774 : bool char16_array = !!comptypes (typ1, char16_type_node);
9922 11774 : bool char32_array = !!comptypes (typ1, char32_type_node);
9923 :
9924 11774 : if (char_array || wchar_array || char16_array || char32_array)
9925 : {
9926 11745 : struct c_expr expr;
9927 11745 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9928 11745 : bool incompat_string_cst = false;
9929 11745 : expr.value = inside_init;
9930 11745 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9931 11745 : expr.original_type = NULL;
9932 11745 : expr.m_decimal = 0;
9933 11745 : maybe_warn_string_init (init_loc, type, expr);
9934 :
9935 11745 : if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
9936 91 : pedwarn_init (init_loc, OPT_Wpedantic,
9937 : "initialization of a flexible array member");
9938 :
9939 11745 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9940 11745 : TYPE_MAIN_VARIANT (type)))
9941 : return inside_init;
9942 :
9943 4186 : if (char_array)
9944 : {
9945 4075 : if (typ2 != char_type_node && typ2 != char8_type_node)
9946 : incompat_string_cst = true;
9947 : }
9948 111 : else if (!comptypes (typ1, typ2))
9949 : incompat_string_cst = true;
9950 :
9951 : if (incompat_string_cst)
9952 : {
9953 72 : error_init (init_loc, "cannot initialize array of %qT from "
9954 : "a string literal with type array of %qT",
9955 : typ1, typ2);
9956 72 : return error_mark_node;
9957 : }
9958 :
9959 4114 : if (require_constexpr
9960 4114 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
9961 : {
9962 : /* Check if all characters of the string can be
9963 : represented in the type of the constexpr object being
9964 : initialized. */
9965 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9966 24 : const unsigned char *p =
9967 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
9968 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
9969 70 : for (unsigned i = 0; i < len; i++)
9970 58 : if (p[i] > 127)
9971 : {
9972 12 : error_init (init_loc, "%<constexpr%> initializer not "
9973 : "representable in type of object");
9974 12 : break;
9975 : }
9976 : }
9977 :
9978 4114 : if (TYPE_DOMAIN (type) != NULL_TREE
9979 4020 : && TYPE_SIZE (type) != NULL_TREE
9980 8075 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9981 : {
9982 3961 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9983 :
9984 3961 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
9985 : {
9986 397 : unsigned HOST_WIDE_INT avail
9987 397 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9988 397 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
9989 397 : const char *p = TREE_STRING_POINTER (inside_init);
9990 :
9991 : /* Construct truncated string. */
9992 397 : inside_init = build_string (avail, p);
9993 :
9994 : /* Subtract the size of a single (possibly wide) character
9995 : because it may be ok to ignore the terminating NUL char
9996 : that is counted in the length of the constant. */
9997 397 : if (len - unit > avail)
9998 63 : pedwarn_init (init_loc, 0,
9999 : "initializer-string for array of %qT "
10000 : "is too long (%wu chars into %wu "
10001 : "available)", typ1, len, avail);
10002 334 : else if (warn_cxx_compat)
10003 9 : warning_at (init_loc, OPT_Wc___compat,
10004 : "initializer-string for array of %qT "
10005 : "is too long for C++ (%wu chars into %wu "
10006 : "available)", typ1, len, avail);
10007 325 : else if (warn_unterminated_string_initialization
10008 325 : && get_attr_nonstring_decl (decl) == NULL_TREE)
10009 22 : warning_at (init_loc,
10010 22 : OPT_Wunterminated_string_initialization,
10011 : "initializer-string for array of %qT "
10012 : "truncates NUL terminator but destination "
10013 : "lacks %qs attribute (%wu chars into %wu "
10014 : "available)", typ1, "nonstring", len, avail);
10015 : }
10016 : }
10017 :
10018 4114 : TREE_TYPE (inside_init) = type;
10019 4114 : return inside_init;
10020 : }
10021 29 : else if (INTEGRAL_TYPE_P (typ1))
10022 : {
10023 29 : error_init (init_loc, "array of inappropriate type initialized "
10024 : "from string constant");
10025 29 : return error_mark_node;
10026 : }
10027 : }
10028 :
10029 : /* Build a VECTOR_CST from a *constant* vector constructor. If the
10030 : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
10031 : below and handle as a constructor. */
10032 16881877 : if (code == VECTOR_TYPE
10033 6311085 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
10034 6311083 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
10035 23192922 : && TREE_CONSTANT (inside_init))
10036 : {
10037 633264 : if (TREE_CODE (inside_init) == VECTOR_CST
10038 633361 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10039 97 : TYPE_MAIN_VARIANT (type)))
10040 : return inside_init;
10041 :
10042 633167 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
10043 : {
10044 : unsigned HOST_WIDE_INT ix;
10045 : tree value;
10046 4173675 : bool constant_p = true;
10047 :
10048 : /* Iterate through elements and check if all constructor
10049 : elements are *_CSTs. */
10050 4173675 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
10051 3540564 : if (!CONSTANT_CLASS_P (value))
10052 : {
10053 : constant_p = false;
10054 : break;
10055 : }
10056 :
10057 633167 : if (constant_p)
10058 633111 : return build_vector_from_ctor (type,
10059 633111 : CONSTRUCTOR_ELTS (inside_init));
10060 : }
10061 : }
10062 :
10063 16248669 : if (warn_sequence_point)
10064 2447859 : verify_sequence_points (inside_init);
10065 :
10066 : /* Any type can be initialized
10067 : from an expression of the same type, optionally with braces. */
10068 :
10069 16248669 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
10070 32497338 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10071 16248669 : TYPE_MAIN_VARIANT (type))
10072 3130621 : || (code == ARRAY_TYPE
10073 477 : && comptypes (TREE_TYPE (inside_init), type))
10074 3130621 : || (gnu_vector_type_p (type)
10075 189 : && comptypes (TREE_TYPE (inside_init), type))
10076 3130621 : || (code == POINTER_TYPE
10077 111933 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
10078 9289 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
10079 9289 : TREE_TYPE (type)))))
10080 : {
10081 13120024 : if (code == POINTER_TYPE)
10082 : {
10083 784575 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
10084 : {
10085 1976 : if (TREE_CODE (inside_init) == STRING_CST
10086 68 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10087 1973 : inside_init = array_to_pointer_conversion
10088 1973 : (init_loc, inside_init);
10089 : else
10090 : {
10091 3 : error_init (init_loc, "invalid use of non-lvalue array");
10092 3 : return error_mark_node;
10093 : }
10094 : }
10095 : }
10096 :
10097 13120021 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
10098 : /* Although the types are compatible, we may require a
10099 : conversion. */
10100 5677821 : inside_init = convert (type, inside_init);
10101 :
10102 13120021 : if ((code == RECORD_TYPE || code == UNION_TYPE)
10103 13120021 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
10104 : {
10105 0 : error_init (init_loc, "invalid initializer");
10106 0 : return error_mark_node;
10107 : }
10108 :
10109 13120021 : if (require_constant
10110 2297463 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10111 : {
10112 : /* As an extension, allow initializing objects with static storage
10113 : duration with compound literals (which are then treated just as
10114 : the brace enclosed list they contain). Also allow this for
10115 : vectors, as we can only assign them with compound literals. */
10116 31 : if (flag_isoc99 && code != VECTOR_TYPE)
10117 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
10118 : "is not constant");
10119 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
10120 31 : inside_init = DECL_INITIAL (decl);
10121 : }
10122 :
10123 13120021 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
10124 176156 : && TREE_CODE (inside_init) != CONSTRUCTOR)
10125 : {
10126 2 : error_init (init_loc, "array initialized from non-constant array "
10127 : "expression");
10128 2 : return error_mark_node;
10129 : }
10130 :
10131 : /* Compound expressions can only occur here if -Wpedantic or
10132 : -pedantic-errors is specified. In the later case, we always want
10133 : an error. In the former case, we simply want a warning. */
10134 13120019 : if (require_constant && pedantic
10135 20308 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
10136 : {
10137 1 : inside_init
10138 1 : = valid_compound_expr_initializer (inside_init,
10139 1 : TREE_TYPE (inside_init));
10140 1 : if (inside_init == error_mark_node)
10141 1 : error_init (init_loc, "initializer element is not constant");
10142 : else
10143 0 : pedwarn_init (init_loc, OPT_Wpedantic,
10144 : "initializer element is not constant");
10145 1 : if (flag_pedantic_errors)
10146 0 : inside_init = error_mark_node;
10147 : }
10148 2297462 : else if (require_constant
10149 2297462 : && !initializer_constant_valid_p (inside_init,
10150 2297462 : TREE_TYPE (inside_init)))
10151 : {
10152 94 : error_init (init_loc, "initializer element is not constant");
10153 94 : inside_init = error_mark_node;
10154 : }
10155 13119924 : else if (require_constant && !maybe_const)
10156 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10157 : "initializer element is not a constant expression");
10158 13119705 : else if (require_constexpr)
10159 584 : check_constexpr_init (init_loc, type, inside_init,
10160 : int_const_expr, arith_const_expr);
10161 :
10162 : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10163 13120019 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10164 853495 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10165 : type, inside_init, origtype,
10166 : (require_constant
10167 : ? ic_init_const
10168 : : ic_init), null_pointer_constant,
10169 : NULL_TREE, NULL_TREE, 0);
10170 13120019 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10171 253 : && c_inhibit_evaluation_warnings == 0
10172 253 : && warn_conversion
10173 1 : && !TYPE_UNSIGNED (type)
10174 13120020 : && TYPE_PRECISION (type) == CHAR_BIT)
10175 303 : for (unsigned int i = 0;
10176 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10177 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10178 10 : warning_at (init_loc, OPT_Wconversion,
10179 : "conversion from %qT to %qT changes value from "
10180 : "%qd to %qd",
10181 : integer_type_node, type,
10182 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10183 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10184 13120019 : return inside_init;
10185 : }
10186 :
10187 : /* Handle scalar types, including conversions. */
10188 :
10189 3128645 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10190 128740 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10191 11785 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10192 9722 : || code == BITINT_TYPE)
10193 : {
10194 3128157 : tree unconverted_init = inside_init;
10195 3128157 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10196 3128157 : && (TREE_CODE (init) == STRING_CST
10197 2 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10198 7314 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10199 3128157 : if (semantic_type)
10200 436 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10201 : inside_init);
10202 3128157 : inside_init
10203 5677710 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10204 : inside_init, origtype,
10205 : require_constant ? ic_init_const : ic_init,
10206 : null_pointer_constant, NULL_TREE, NULL_TREE,
10207 : 0);
10208 :
10209 : /* Check to see if we have already given an error message. */
10210 3128157 : if (inside_init == error_mark_node)
10211 : ;
10212 3127697 : else if (require_constant && !TREE_CONSTANT (inside_init))
10213 : {
10214 36 : error_init (init_loc, "initializer element is not constant");
10215 36 : inside_init = error_mark_node;
10216 : }
10217 3127661 : else if (require_constant
10218 3705837 : && !initializer_constant_valid_p (inside_init,
10219 578176 : TREE_TYPE (inside_init)))
10220 : {
10221 10 : error_init (init_loc, "initializer element is not computable at "
10222 : "load time");
10223 10 : inside_init = error_mark_node;
10224 : }
10225 3127651 : else if (require_constant && !maybe_const)
10226 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10227 : "initializer element is not a constant expression");
10228 3127646 : else if (require_constexpr)
10229 328 : check_constexpr_init (init_loc, type, unconverted_init,
10230 : int_const_expr, arith_const_expr);
10231 :
10232 3128157 : return inside_init;
10233 : }
10234 :
10235 : /* Come here only for records and arrays. */
10236 :
10237 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10238 : {
10239 0 : error_init (init_loc,
10240 : "variable-sized object may not be initialized except "
10241 : "with an empty initializer");
10242 0 : return error_mark_node;
10243 : }
10244 :
10245 488 : error_init (init_loc, "invalid initializer");
10246 488 : return error_mark_node;
10247 : }
10248 :
10249 : /* Handle initializers that use braces. */
10250 :
10251 : /* Type of object we are accumulating a constructor for.
10252 : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10253 : static tree constructor_type;
10254 :
10255 : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10256 : left to fill. */
10257 : static tree constructor_fields;
10258 :
10259 : /* For an ARRAY_TYPE, this is the specified index
10260 : at which to store the next element we get. */
10261 : static tree constructor_index;
10262 :
10263 : /* For an ARRAY_TYPE, this is the maximum index. */
10264 : static tree constructor_max_index;
10265 :
10266 : /* For a RECORD_TYPE, this is the first field not yet written out. */
10267 : static tree constructor_unfilled_fields;
10268 :
10269 : /* For an ARRAY_TYPE, this is the index of the first element
10270 : not yet written out. */
10271 : static tree constructor_unfilled_index;
10272 :
10273 : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10274 : This is so we can generate gaps between fields, when appropriate. */
10275 : static tree constructor_bit_index;
10276 :
10277 : /* If we are saving up the elements rather than allocating them,
10278 : this is the list of elements so far (in reverse order,
10279 : most recent first). */
10280 : static vec<constructor_elt, va_gc> *constructor_elements;
10281 :
10282 : /* 1 if constructor should be incrementally stored into a constructor chain,
10283 : 0 if all the elements should be kept in AVL tree. */
10284 : static int constructor_incremental;
10285 :
10286 : /* 1 if so far this constructor's elements are all compile-time constants. */
10287 : static int constructor_constant;
10288 :
10289 : /* 1 if so far this constructor's elements are all valid address constants. */
10290 : static int constructor_simple;
10291 :
10292 : /* 1 if this constructor has an element that cannot be part of a
10293 : constant expression. */
10294 : static int constructor_nonconst;
10295 :
10296 : /* 1 if this constructor is erroneous so far. */
10297 : static int constructor_erroneous;
10298 :
10299 : /* 1 if this constructor is the universal zero initializer { 0 }. */
10300 : static int constructor_zeroinit;
10301 :
10302 : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10303 : static bool constructor_zero_padding_bits;
10304 :
10305 : /* 1 if this constructor is a braced scalar initializer (further nested levels
10306 : of braces are an error). */
10307 : static bool constructor_braced_scalar;
10308 :
10309 : /* Structure for managing pending initializer elements, organized as an
10310 : AVL tree. */
10311 :
10312 : struct init_node
10313 : {
10314 : struct init_node *left, *right;
10315 : struct init_node *parent;
10316 : int balance;
10317 : tree purpose;
10318 : tree value;
10319 : tree origtype;
10320 : };
10321 :
10322 : /* Tree of pending elements at this constructor level.
10323 : These are elements encountered out of order
10324 : which belong at places we haven't reached yet in actually
10325 : writing the output.
10326 : Will never hold tree nodes across GC runs. */
10327 : static struct init_node *constructor_pending_elts;
10328 :
10329 : /* The SPELLING_DEPTH of this constructor. */
10330 : static int constructor_depth;
10331 :
10332 : /* DECL node for which an initializer is being read.
10333 : 0 means we are reading a constructor expression
10334 : such as (struct foo) {...}. */
10335 : static tree constructor_decl;
10336 :
10337 : /* Nonzero if there were any member designators in this initializer. */
10338 : static int constructor_designated;
10339 :
10340 : /* Nesting depth of designator list. */
10341 : static int designator_depth;
10342 :
10343 : /* Nonzero if there were diagnosed errors in this designator list. */
10344 : static int designator_erroneous;
10345 :
10346 :
10347 : /* This stack has a level for each implicit or explicit level of
10348 : structuring in the initializer, including the outermost one. It
10349 : saves the values of most of the variables above. */
10350 :
10351 : struct constructor_range_stack;
10352 :
10353 : struct constructor_stack
10354 : {
10355 : struct constructor_stack *next;
10356 : tree type;
10357 : tree fields;
10358 : tree index;
10359 : tree max_index;
10360 : tree unfilled_index;
10361 : tree unfilled_fields;
10362 : tree bit_index;
10363 : vec<constructor_elt, va_gc> *elements;
10364 : struct init_node *pending_elts;
10365 : int offset;
10366 : int depth;
10367 : /* If value nonzero, this value should replace the entire
10368 : constructor at this level. */
10369 : struct c_expr replacement_value;
10370 : struct constructor_range_stack *range_stack;
10371 : char constant;
10372 : char simple;
10373 : char nonconst;
10374 : char implicit;
10375 : char erroneous;
10376 : char outer;
10377 : char incremental;
10378 : char designated;
10379 : bool zero_padding_bits;
10380 : bool braced_scalar;
10381 : int designator_depth;
10382 : };
10383 :
10384 : static struct constructor_stack *constructor_stack;
10385 :
10386 : /* This stack represents designators from some range designator up to
10387 : the last designator in the list. */
10388 :
10389 : struct constructor_range_stack
10390 : {
10391 : struct constructor_range_stack *next, *prev;
10392 : struct constructor_stack *stack;
10393 : tree range_start;
10394 : tree index;
10395 : tree range_end;
10396 : tree fields;
10397 : };
10398 :
10399 : static struct constructor_range_stack *constructor_range_stack;
10400 :
10401 : /* This stack records separate initializers that are nested.
10402 : Nested initializers can't happen in ANSI C, but GNU C allows them
10403 : in cases like { ... (struct foo) { ... } ... }. */
10404 :
10405 : struct initializer_stack
10406 : {
10407 : struct initializer_stack *next;
10408 : tree decl;
10409 : struct constructor_stack *constructor_stack;
10410 : struct constructor_range_stack *constructor_range_stack;
10411 : vec<constructor_elt, va_gc> *elements;
10412 : struct spelling *spelling;
10413 : struct spelling *spelling_base;
10414 : int spelling_size;
10415 : char require_constant_value;
10416 : char require_constant_elements;
10417 : char require_constexpr_value;
10418 : char designated;
10419 : rich_location *missing_brace_richloc;
10420 : };
10421 :
10422 : static struct initializer_stack *initializer_stack;
10423 :
10424 : /* Prepare to parse and output the initializer for variable DECL. */
10425 :
10426 : void
10427 7269078 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10428 : bool init_require_constant, bool init_require_constexpr,
10429 : rich_location *richloc)
10430 : {
10431 7269078 : const char *locus;
10432 7269078 : struct initializer_stack *p = XNEW (struct initializer_stack);
10433 :
10434 7269078 : p->decl = constructor_decl;
10435 7269078 : p->require_constant_value = require_constant_value;
10436 7269078 : p->require_constant_elements = require_constant_elements;
10437 7269078 : p->require_constexpr_value = require_constexpr_value;
10438 7269078 : p->constructor_stack = constructor_stack;
10439 7269078 : p->constructor_range_stack = constructor_range_stack;
10440 7269078 : p->elements = constructor_elements;
10441 7269078 : p->spelling = spelling;
10442 7269078 : p->spelling_base = spelling_base;
10443 7269078 : p->spelling_size = spelling_size;
10444 7269078 : p->next = initializer_stack;
10445 7269078 : p->missing_brace_richloc = richloc;
10446 7269078 : p->designated = constructor_designated;
10447 7269078 : initializer_stack = p;
10448 :
10449 7269078 : constructor_decl = decl;
10450 7269078 : constructor_designated = 0;
10451 :
10452 7269078 : require_constant_value = init_require_constant;
10453 7269078 : require_constexpr_value = init_require_constexpr;
10454 7269078 : if (decl != NULL_TREE && decl != error_mark_node)
10455 : {
10456 6347376 : require_constant_elements
10457 6170759 : = ((init_require_constant || (pedantic && !flag_isoc99))
10458 : /* For a scalar, you can always use any value to initialize,
10459 : even within braces. */
10460 6359337 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10461 6347376 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10462 : }
10463 : else
10464 : {
10465 921702 : require_constant_elements = false;
10466 921702 : locus = _("(anonymous)");
10467 : }
10468 :
10469 7269078 : constructor_stack = 0;
10470 7269078 : constructor_range_stack = 0;
10471 :
10472 7269078 : found_missing_braces = 0;
10473 :
10474 7269078 : spelling_base = 0;
10475 7269078 : spelling_size = 0;
10476 7269078 : RESTORE_SPELLING_DEPTH (0);
10477 :
10478 7269078 : if (locus)
10479 7269078 : push_string (locus);
10480 7269078 : }
10481 :
10482 : void
10483 7269076 : finish_init (void)
10484 : {
10485 7269076 : struct initializer_stack *p = initializer_stack;
10486 :
10487 : /* Free the whole constructor stack of this initializer. */
10488 7269076 : while (constructor_stack)
10489 : {
10490 0 : struct constructor_stack *q = constructor_stack;
10491 0 : constructor_stack = q->next;
10492 0 : XDELETE (q);
10493 : }
10494 :
10495 7269076 : gcc_assert (!constructor_range_stack);
10496 :
10497 : /* Pop back to the data of the outer initializer (if any). */
10498 7269076 : XDELETE (spelling_base);
10499 :
10500 7269076 : constructor_decl = p->decl;
10501 7269076 : require_constant_value = p->require_constant_value;
10502 7269076 : require_constant_elements = p->require_constant_elements;
10503 7269076 : require_constexpr_value = p->require_constexpr_value;
10504 7269076 : constructor_stack = p->constructor_stack;
10505 7269076 : constructor_designated = p->designated;
10506 7269076 : constructor_range_stack = p->constructor_range_stack;
10507 7269076 : constructor_elements = p->elements;
10508 7269076 : spelling = p->spelling;
10509 7269076 : spelling_base = p->spelling_base;
10510 7269076 : spelling_size = p->spelling_size;
10511 7269076 : initializer_stack = p->next;
10512 7269076 : XDELETE (p);
10513 7269076 : }
10514 :
10515 : /* Call here when we see the initializer is surrounded by braces.
10516 : This is instead of a call to push_init_level;
10517 : it is matched by a call to pop_init_level.
10518 :
10519 : TYPE is the type to initialize, for a constructor expression.
10520 : For an initializer for a decl, TYPE is zero. */
10521 :
10522 : void
10523 1025273 : really_start_incremental_init (tree type)
10524 : {
10525 1025273 : struct constructor_stack *p = XNEW (struct constructor_stack);
10526 :
10527 1025273 : if (type == NULL_TREE)
10528 105535 : type = TREE_TYPE (constructor_decl);
10529 :
10530 1025273 : if (VECTOR_TYPE_P (type)
10531 1025273 : && TYPE_VECTOR_OPAQUE (type))
10532 0 : error ("opaque vector types cannot be initialized");
10533 :
10534 1025273 : p->type = constructor_type;
10535 1025273 : p->fields = constructor_fields;
10536 1025273 : p->index = constructor_index;
10537 1025273 : p->max_index = constructor_max_index;
10538 1025273 : p->unfilled_index = constructor_unfilled_index;
10539 1025273 : p->unfilled_fields = constructor_unfilled_fields;
10540 1025273 : p->bit_index = constructor_bit_index;
10541 1025273 : p->elements = constructor_elements;
10542 1025273 : p->constant = constructor_constant;
10543 1025273 : p->simple = constructor_simple;
10544 1025273 : p->nonconst = constructor_nonconst;
10545 1025273 : p->erroneous = constructor_erroneous;
10546 1025273 : p->pending_elts = constructor_pending_elts;
10547 1025273 : p->depth = constructor_depth;
10548 1025273 : p->replacement_value.value = 0;
10549 1025273 : p->replacement_value.original_code = ERROR_MARK;
10550 1025273 : p->replacement_value.original_type = NULL;
10551 1025273 : p->implicit = 0;
10552 1025273 : p->range_stack = 0;
10553 1025273 : p->outer = 0;
10554 1025273 : p->incremental = constructor_incremental;
10555 1025273 : p->designated = constructor_designated;
10556 1025273 : p->zero_padding_bits = constructor_zero_padding_bits;
10557 1025273 : p->braced_scalar = constructor_braced_scalar;
10558 1025273 : p->designator_depth = designator_depth;
10559 1025273 : p->next = 0;
10560 1025273 : constructor_stack = p;
10561 :
10562 1025273 : constructor_constant = 1;
10563 1025273 : constructor_simple = 1;
10564 1025273 : constructor_nonconst = 0;
10565 1025273 : constructor_depth = SPELLING_DEPTH ();
10566 1025273 : constructor_elements = NULL;
10567 1025273 : constructor_pending_elts = 0;
10568 1025273 : constructor_type = type;
10569 1025273 : constructor_incremental = 1;
10570 1025273 : constructor_designated = 0;
10571 1025273 : constructor_zero_padding_bits = false;
10572 1025273 : constructor_zeroinit = 1;
10573 1025273 : constructor_braced_scalar = false;
10574 1025273 : designator_depth = 0;
10575 1025273 : designator_erroneous = 0;
10576 :
10577 1025273 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10578 : {
10579 81460 : constructor_fields = TYPE_FIELDS (constructor_type);
10580 : /* Skip any nameless bit fields at the beginning. */
10581 81460 : while (constructor_fields != NULL_TREE
10582 81488 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10583 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10584 :
10585 81460 : constructor_unfilled_fields = constructor_fields;
10586 81460 : constructor_bit_index = bitsize_zero_node;
10587 : }
10588 943813 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10589 : {
10590 17912 : if (TYPE_DOMAIN (constructor_type))
10591 : {
10592 9466 : constructor_max_index
10593 9466 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10594 :
10595 : /* Detect non-empty initializations of zero-length arrays. */
10596 9466 : if (constructor_max_index == NULL_TREE
10597 9466 : && TYPE_SIZE (constructor_type))
10598 205 : constructor_max_index = integer_minus_one_node;
10599 :
10600 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10601 : to initialize VLAs with a nonempty initializer will cause a
10602 : proper error; avoid tree checking errors as well by setting a
10603 : safe value. */
10604 9466 : if (constructor_max_index
10605 9466 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10606 73 : constructor_max_index = integer_minus_one_node;
10607 :
10608 9466 : constructor_index
10609 9466 : = convert (bitsizetype,
10610 9466 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10611 : }
10612 : else
10613 : {
10614 8446 : constructor_index = bitsize_zero_node;
10615 8446 : constructor_max_index = NULL_TREE;
10616 : }
10617 :
10618 17912 : constructor_unfilled_index = constructor_index;
10619 : }
10620 925901 : else if (gnu_vector_type_p (constructor_type))
10621 : {
10622 : /* Vectors are like simple fixed-size arrays. */
10623 1850690 : constructor_max_index =
10624 925345 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10625 925345 : constructor_index = bitsize_zero_node;
10626 925345 : constructor_unfilled_index = constructor_index;
10627 : }
10628 : else
10629 : {
10630 : /* Handle the case of int x = {5}; */
10631 556 : constructor_fields = constructor_type;
10632 556 : constructor_unfilled_fields = constructor_type;
10633 556 : constructor_braced_scalar = true;
10634 : }
10635 1025273 : }
10636 :
10637 : extern location_t last_init_list_comma;
10638 :
10639 : /* Called when we see an open brace for a nested initializer. Finish
10640 : off any pending levels with implicit braces. */
10641 : void
10642 340365 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10643 : {
10644 340367 : while (constructor_stack->implicit)
10645 : {
10646 304 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10647 140 : && constructor_fields == NULL_TREE)
10648 0 : process_init_element (input_location,
10649 : pop_init_level (loc, 1, braced_init_obstack,
10650 : last_init_list_comma),
10651 : true, braced_init_obstack);
10652 304 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10653 164 : && constructor_max_index
10654 468 : && tree_int_cst_lt (constructor_max_index,
10655 : constructor_index))
10656 2 : process_init_element (input_location,
10657 : pop_init_level (loc, 1, braced_init_obstack,
10658 : last_init_list_comma),
10659 : true, braced_init_obstack);
10660 : else
10661 : break;
10662 : }
10663 340365 : }
10664 :
10665 : /* Push down into a subobject, for initialization.
10666 : If this is for an explicit set of braces, IMPLICIT is 0.
10667 : If it is because the next element belongs at a lower level,
10668 : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10669 :
10670 : void
10671 1043045 : push_init_level (location_t loc, int implicit,
10672 : struct obstack *braced_init_obstack)
10673 : {
10674 1043045 : struct constructor_stack *p;
10675 1043045 : tree value = NULL_TREE;
10676 :
10677 : /* Unless this is an explicit brace, we need to preserve previous
10678 : content if any. */
10679 1043045 : if (implicit)
10680 : {
10681 703622 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10682 798 : value = find_init_member (constructor_fields, braced_init_obstack);
10683 702824 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10684 702824 : value = find_init_member (constructor_index, braced_init_obstack);
10685 : }
10686 :
10687 1043045 : p = XNEW (struct constructor_stack);
10688 1043045 : p->type = constructor_type;
10689 1043045 : p->fields = constructor_fields;
10690 1043045 : p->index = constructor_index;
10691 1043045 : p->max_index = constructor_max_index;
10692 1043045 : p->unfilled_index = constructor_unfilled_index;
10693 1043045 : p->unfilled_fields = constructor_unfilled_fields;
10694 1043045 : p->bit_index = constructor_bit_index;
10695 1043045 : p->elements = constructor_elements;
10696 1043045 : p->constant = constructor_constant;
10697 1043045 : p->simple = constructor_simple;
10698 1043045 : p->nonconst = constructor_nonconst;
10699 1043045 : p->erroneous = constructor_erroneous;
10700 1043045 : p->pending_elts = constructor_pending_elts;
10701 1043045 : p->depth = constructor_depth;
10702 1043045 : p->replacement_value.value = NULL_TREE;
10703 1043045 : p->replacement_value.original_code = ERROR_MARK;
10704 1043045 : p->replacement_value.original_type = NULL;
10705 1043045 : p->implicit = implicit;
10706 1043045 : p->outer = 0;
10707 1043045 : p->incremental = constructor_incremental;
10708 1043045 : p->designated = constructor_designated;
10709 1043045 : p->zero_padding_bits = constructor_zero_padding_bits;
10710 1043045 : p->braced_scalar = constructor_braced_scalar;
10711 1043045 : p->designator_depth = designator_depth;
10712 1043045 : p->next = constructor_stack;
10713 1043045 : p->range_stack = 0;
10714 1043045 : constructor_stack = p;
10715 :
10716 1043045 : constructor_constant = 1;
10717 1043045 : constructor_simple = 1;
10718 1043045 : constructor_nonconst = 0;
10719 1043045 : constructor_depth = SPELLING_DEPTH ();
10720 1043045 : constructor_elements = NULL;
10721 1043045 : constructor_incremental = 1;
10722 : /* If the upper initializer is designated, then mark this as
10723 : designated too to prevent bogus warnings. */
10724 1043045 : constructor_designated = p->designated;
10725 : /* If the upper initializer has padding bits zeroed, that includes
10726 : all nested initializers as well. */
10727 1043045 : constructor_zero_padding_bits = p->zero_padding_bits;
10728 1043045 : constructor_braced_scalar = false;
10729 1043045 : constructor_pending_elts = 0;
10730 1043045 : if (!implicit)
10731 : {
10732 339423 : p->range_stack = constructor_range_stack;
10733 339423 : constructor_range_stack = 0;
10734 339423 : designator_depth = 0;
10735 339423 : designator_erroneous = 0;
10736 : }
10737 :
10738 : /* Don't die if an entire brace-pair level is superfluous
10739 : in the containing level. */
10740 1043045 : if (constructor_type == NULL_TREE)
10741 : ;
10742 1043045 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10743 : {
10744 : /* Don't die if there are extra init elts at the end. */
10745 158796 : if (constructor_fields == NULL_TREE)
10746 51 : constructor_type = NULL_TREE;
10747 : else
10748 : {
10749 158745 : constructor_type = TREE_TYPE (constructor_fields);
10750 158745 : push_member_name (constructor_fields);
10751 158745 : constructor_depth++;
10752 : }
10753 : }
10754 884249 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10755 : {
10756 884198 : constructor_type = TREE_TYPE (constructor_type);
10757 884198 : push_array_bounds (tree_to_uhwi (constructor_index));
10758 884198 : constructor_depth++;
10759 : }
10760 :
10761 1043045 : if (constructor_type == NULL_TREE)
10762 : {
10763 51 : error_init (loc, "extra brace group at end of initializer");
10764 51 : constructor_fields = NULL_TREE;
10765 51 : constructor_unfilled_fields = NULL_TREE;
10766 51 : return;
10767 : }
10768 :
10769 1042994 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10770 : {
10771 360 : constructor_constant = TREE_CONSTANT (value);
10772 360 : constructor_simple = TREE_STATIC (value);
10773 360 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10774 360 : constructor_elements = CONSTRUCTOR_ELTS (value);
10775 360 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10776 360 : if (!vec_safe_is_empty (constructor_elements)
10777 340 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10778 340 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10779 294 : set_nonincremental_init (braced_init_obstack);
10780 : }
10781 :
10782 1042994 : if (implicit == 1)
10783 : {
10784 702680 : found_missing_braces = 1;
10785 702680 : if (initializer_stack->missing_brace_richloc)
10786 702680 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10787 702680 : (loc, "{");
10788 : }
10789 :
10790 1042994 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10791 : {
10792 879596 : constructor_fields = TYPE_FIELDS (constructor_type);
10793 : /* Skip any nameless bit fields at the beginning. */
10794 879596 : while (constructor_fields != NULL_TREE
10795 879660 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10796 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10797 :
10798 879596 : constructor_unfilled_fields = constructor_fields;
10799 879596 : constructor_bit_index = bitsize_zero_node;
10800 : }
10801 163398 : else if (gnu_vector_type_p (constructor_type))
10802 : {
10803 : /* Vectors are like simple fixed-size arrays. */
10804 9602 : constructor_max_index =
10805 4801 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10806 4801 : constructor_index = bitsize_int (0);
10807 4801 : constructor_unfilled_index = constructor_index;
10808 : }
10809 158597 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10810 : {
10811 158517 : if (TYPE_DOMAIN (constructor_type))
10812 : {
10813 158517 : constructor_max_index
10814 158517 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10815 :
10816 : /* Detect non-empty initializations of zero-length arrays. */
10817 158517 : if (constructor_max_index == NULL_TREE
10818 158517 : && TYPE_SIZE (constructor_type))
10819 135 : constructor_max_index = integer_minus_one_node;
10820 :
10821 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10822 : to initialize VLAs will cause a proper error; avoid tree
10823 : checking errors as well by setting a safe value. */
10824 158517 : if (constructor_max_index
10825 158247 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10826 2 : constructor_max_index = integer_minus_one_node;
10827 :
10828 158517 : constructor_index
10829 158517 : = convert (bitsizetype,
10830 158517 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10831 : }
10832 : else
10833 0 : constructor_index = bitsize_zero_node;
10834 :
10835 158517 : constructor_unfilled_index = constructor_index;
10836 158517 : if (value && TREE_CODE (value) == STRING_CST)
10837 : {
10838 : /* We need to split the char/wchar array into individual
10839 : characters, so that we don't have to special case it
10840 : everywhere. */
10841 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10842 : }
10843 : }
10844 : else
10845 : {
10846 80 : if (constructor_type != error_mark_node)
10847 : {
10848 51 : if (p->braced_scalar)
10849 22 : permerror_init (input_location, 0,
10850 : "braces around scalar initializer");
10851 : else
10852 29 : warning_init (input_location, 0,
10853 : "braces around scalar initializer");
10854 51 : constructor_braced_scalar = true;
10855 : }
10856 80 : constructor_fields = constructor_type;
10857 80 : constructor_unfilled_fields = constructor_type;
10858 : }
10859 : }
10860 :
10861 : /* At the end of an implicit or explicit brace level,
10862 : finish up that level of constructor. If a single expression
10863 : with redundant braces initialized that level, return the
10864 : c_expr structure for that expression. Otherwise, the original_code
10865 : element is set to ERROR_MARK.
10866 : If we were outputting the elements as they are read, return 0 as the value
10867 : from inner levels (process_init_element ignores that),
10868 : but return error_mark_node as the value from the outermost level
10869 : (that's what we want to put in DECL_INITIAL).
10870 : Otherwise, return a CONSTRUCTOR expression as the value. */
10871 :
10872 : struct c_expr
10873 2068318 : pop_init_level (location_t loc, int implicit,
10874 : struct obstack *braced_init_obstack,
10875 : location_t insert_before)
10876 : {
10877 2068318 : struct constructor_stack *p;
10878 2068318 : struct c_expr ret;
10879 2068318 : ret.value = NULL_TREE;
10880 2068318 : ret.original_code = ERROR_MARK;
10881 2068318 : ret.original_type = NULL;
10882 2068318 : ret.m_decimal = 0;
10883 :
10884 2068318 : if (implicit == 0)
10885 : {
10886 : /* When we come to an explicit close brace,
10887 : pop any inner levels that didn't have explicit braces. */
10888 1365629 : while (constructor_stack->implicit)
10889 933 : process_init_element (input_location,
10890 : pop_init_level (loc, 1, braced_init_obstack,
10891 : insert_before),
10892 : true, braced_init_obstack);
10893 1364696 : gcc_assert (!constructor_range_stack);
10894 : }
10895 : else
10896 703622 : if (initializer_stack->missing_brace_richloc)
10897 703622 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10898 703622 : (insert_before, "}");
10899 :
10900 : /* Now output all pending elements. */
10901 2068318 : constructor_incremental = 1;
10902 2068318 : output_pending_init_elements (1, braced_init_obstack);
10903 :
10904 2068318 : p = constructor_stack;
10905 :
10906 : /* Error for initializing a flexible array member, or a zero-length
10907 : array member in an inappropriate context. */
10908 2068267 : if (constructor_type && constructor_fields
10909 159326 : && TREE_CODE (constructor_type) == ARRAY_TYPE
10910 152563 : && TYPE_DOMAIN (constructor_type)
10911 2220873 : && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
10912 : {
10913 : /* Silently discard empty initializations. The parser will
10914 : already have pedwarned for empty brackets for C17 and earlier. */
10915 300 : if (integer_zerop (constructor_unfilled_index))
10916 84 : constructor_type = NULL_TREE;
10917 300 : if (constructor_type || flag_isoc23)
10918 : {
10919 296 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10920 :
10921 296 : if (constructor_depth <= 2)
10922 249 : pedwarn_init (loc, OPT_Wpedantic,
10923 : "initialization of a flexible array member");
10924 47 : else if (constructor_type)
10925 33 : error_init (loc, "initialization of flexible array member "
10926 : "in a nested context");
10927 : else
10928 14 : pedwarn_init (loc, OPT_Wpedantic,
10929 : "initialization of flexible array member "
10930 : "in a nested context");
10931 :
10932 : /* We have already issued an error message for the existence
10933 : of a flexible array member not at the end of the structure.
10934 : Discard the initializer so that we do not die later. */
10935 296 : if (constructor_type
10936 216 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10937 302 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10938 0 : constructor_type = NULL_TREE;
10939 : }
10940 : }
10941 :
10942 2068318 : switch (vec_safe_length (constructor_elements))
10943 : {
10944 4689 : case 0:
10945 : /* Initialization with { } counts as zeroinit. */
10946 4689 : constructor_zeroinit = 1;
10947 4689 : break;
10948 915205 : case 1:
10949 : /* This might be zeroinit as well. */
10950 915205 : if (integer_zerop ((*constructor_elements)[0].value))
10951 2046 : constructor_zeroinit = 1;
10952 : break;
10953 1148424 : default:
10954 : /* If the constructor has more than one element, it can't be { 0 }. */
10955 1148424 : constructor_zeroinit = 0;
10956 1148424 : break;
10957 : }
10958 :
10959 : /* Warn when some structs are initialized with direct aggregation. */
10960 2068318 : if (!implicit && found_missing_braces && warn_missing_braces
10961 37 : && !constructor_zeroinit)
10962 : {
10963 18 : gcc_assert (initializer_stack->missing_brace_richloc);
10964 18 : warning_at (initializer_stack->missing_brace_richloc,
10965 18 : OPT_Wmissing_braces,
10966 : "missing braces around initializer");
10967 : }
10968 :
10969 : /* Warn when some struct elements are implicitly initialized to zero. */
10970 2068318 : if (warn_missing_field_initializers
10971 425443 : && constructor_type
10972 425415 : && TREE_CODE (constructor_type) == RECORD_TYPE
10973 193395 : && constructor_unfilled_fields)
10974 : {
10975 : /* Do not warn for flexible array members or zero-length arrays. */
10976 43 : while (constructor_unfilled_fields
10977 43 : && (!DECL_SIZE (constructor_unfilled_fields)
10978 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
10979 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
10980 :
10981 43 : if (constructor_unfilled_fields
10982 : /* Do not warn if this level of the initializer uses member
10983 : designators; it is likely to be deliberate. */
10984 43 : && !constructor_designated
10985 : /* Do not warn about initializing with { 0 } or with { }. */
10986 24 : && !constructor_zeroinit)
10987 : {
10988 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
10989 : "missing initializer for field %qD of %qT",
10990 : constructor_unfilled_fields,
10991 : constructor_type))
10992 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
10993 : "%qD declared here", constructor_unfilled_fields);
10994 : }
10995 : }
10996 :
10997 : /* Pad out the end of the structure. */
10998 2068318 : if (p->replacement_value.value)
10999 : /* If this closes a superfluous brace pair,
11000 : just pass out the element between them. */
11001 138 : ret = p->replacement_value;
11002 2068180 : else if (constructor_type == NULL_TREE)
11003 : ;
11004 2068053 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
11005 2068053 : && TREE_CODE (constructor_type) != ARRAY_TYPE
11006 2068053 : && !gnu_vector_type_p (constructor_type))
11007 : {
11008 : /* A nonincremental scalar initializer--just return
11009 : the element, after verifying there is just one.
11010 : Empty scalar initializers are supported in C23. */
11011 636 : if (vec_safe_is_empty (constructor_elements))
11012 : {
11013 201 : if (constructor_erroneous || constructor_type == error_mark_node)
11014 85 : ret.value = error_mark_node;
11015 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
11016 : {
11017 2 : error_init (loc, "invalid initializer");
11018 2 : ret.value = error_mark_node;
11019 : }
11020 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
11021 : /* Ensure this is a null pointer constant in the case of a
11022 : 'constexpr' object initialized with {}. */
11023 33 : ret.value = build_zero_cst (ptr_type_node);
11024 : else
11025 81 : ret.value = build_zero_cst (constructor_type);
11026 : }
11027 435 : else if (vec_safe_length (constructor_elements) != 1)
11028 : {
11029 0 : error_init (loc, "extra elements in scalar initializer");
11030 0 : ret.value = (*constructor_elements)[0].value;
11031 : }
11032 : else
11033 435 : ret.value = (*constructor_elements)[0].value;
11034 : }
11035 : else
11036 : {
11037 2067417 : if (constructor_erroneous)
11038 318 : ret.value = error_mark_node;
11039 : else
11040 : {
11041 2067099 : ret.value = build_constructor (constructor_type,
11042 : constructor_elements);
11043 2067099 : if (constructor_constant)
11044 1737649 : TREE_CONSTANT (ret.value) = 1;
11045 2067099 : if (constructor_constant && constructor_simple)
11046 1737610 : TREE_STATIC (ret.value) = 1;
11047 2067099 : if (constructor_nonconst)
11048 672 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
11049 2067099 : if (constructor_zero_padding_bits)
11050 20 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
11051 : }
11052 : }
11053 :
11054 2068318 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
11055 : {
11056 1092 : if (constructor_nonconst)
11057 15 : ret.original_code = C_MAYBE_CONST_EXPR;
11058 1077 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
11059 0 : ret.original_code = ERROR_MARK;
11060 : }
11061 :
11062 2068318 : constructor_type = p->type;
11063 2068318 : constructor_fields = p->fields;
11064 2068318 : constructor_index = p->index;
11065 2068318 : constructor_max_index = p->max_index;
11066 2068318 : constructor_unfilled_index = p->unfilled_index;
11067 2068318 : constructor_unfilled_fields = p->unfilled_fields;
11068 2068318 : constructor_bit_index = p->bit_index;
11069 2068318 : constructor_elements = p->elements;
11070 2068318 : constructor_constant = p->constant;
11071 2068318 : constructor_simple = p->simple;
11072 2068318 : constructor_nonconst = p->nonconst;
11073 2068318 : constructor_erroneous = p->erroneous;
11074 2068318 : constructor_incremental = p->incremental;
11075 2068318 : constructor_designated = p->designated;
11076 2068318 : constructor_zero_padding_bits = p->zero_padding_bits;
11077 2068318 : constructor_braced_scalar = p->braced_scalar;
11078 2068318 : designator_depth = p->designator_depth;
11079 2068318 : constructor_pending_elts = p->pending_elts;
11080 2068318 : constructor_depth = p->depth;
11081 2068318 : if (!p->implicit)
11082 1364696 : constructor_range_stack = p->range_stack;
11083 2068318 : RESTORE_SPELLING_DEPTH (constructor_depth);
11084 :
11085 2068318 : constructor_stack = p->next;
11086 2068318 : XDELETE (p);
11087 :
11088 2068318 : if (ret.value == NULL_TREE && constructor_stack == 0)
11089 0 : ret.value = error_mark_node;
11090 2068318 : return ret;
11091 : }
11092 :
11093 : /* Common handling for both array range and field name designators.
11094 : ARRAY argument is nonzero for array ranges. Returns false for success. */
11095 :
11096 : static bool
11097 34061 : set_designator (location_t loc, bool array,
11098 : struct obstack *braced_init_obstack)
11099 : {
11100 34061 : tree subtype;
11101 34061 : enum tree_code subcode;
11102 :
11103 : /* Don't die if an entire brace-pair level is superfluous
11104 : in the containing level, or for an erroneous type. */
11105 34061 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
11106 : return true;
11107 :
11108 : /* If there were errors in this designator list already, bail out
11109 : silently. */
11110 34052 : if (designator_erroneous)
11111 : return true;
11112 :
11113 : /* Likewise for an initializer for a variable-size type. Those are
11114 : diagnosed in the parser, except for empty initializer braces. */
11115 34052 : if (COMPLETE_TYPE_P (constructor_type)
11116 34052 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
11117 : return true;
11118 :
11119 34042 : if (!designator_depth)
11120 : {
11121 33424 : gcc_assert (!constructor_range_stack);
11122 :
11123 : /* Designator list starts at the level of closest explicit
11124 : braces. */
11125 33807 : while (constructor_stack->implicit)
11126 383 : process_init_element (input_location,
11127 : pop_init_level (loc, 1, braced_init_obstack,
11128 : last_init_list_comma),
11129 : true, braced_init_obstack);
11130 33424 : constructor_designated = 1;
11131 33424 : return false;
11132 : }
11133 :
11134 618 : switch (TREE_CODE (constructor_type))
11135 : {
11136 421 : case RECORD_TYPE:
11137 421 : case UNION_TYPE:
11138 421 : subtype = TREE_TYPE (constructor_fields);
11139 421 : if (subtype != error_mark_node)
11140 421 : subtype = TYPE_MAIN_VARIANT (subtype);
11141 : break;
11142 197 : case ARRAY_TYPE:
11143 197 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11144 197 : break;
11145 0 : default:
11146 0 : gcc_unreachable ();
11147 : }
11148 :
11149 618 : subcode = TREE_CODE (subtype);
11150 618 : if (array && subcode != ARRAY_TYPE)
11151 : {
11152 1 : error_init (loc, "array index in non-array initializer");
11153 1 : return true;
11154 : }
11155 617 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11156 : {
11157 0 : error_init (loc, "field name not in record or union initializer");
11158 0 : return true;
11159 : }
11160 :
11161 617 : constructor_designated = 1;
11162 617 : finish_implicit_inits (loc, braced_init_obstack);
11163 617 : push_init_level (loc, 2, braced_init_obstack);
11164 617 : return false;
11165 : }
11166 :
11167 : /* If there are range designators in designator list, push a new designator
11168 : to constructor_range_stack. RANGE_END is end of such stack range or
11169 : NULL_TREE if there is no range designator at this level. */
11170 :
11171 : static void
11172 390 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11173 : {
11174 390 : struct constructor_range_stack *p;
11175 :
11176 780 : p = (struct constructor_range_stack *)
11177 390 : obstack_alloc (braced_init_obstack,
11178 : sizeof (struct constructor_range_stack));
11179 390 : p->prev = constructor_range_stack;
11180 390 : p->next = 0;
11181 390 : p->fields = constructor_fields;
11182 390 : p->range_start = constructor_index;
11183 390 : p->index = constructor_index;
11184 390 : p->stack = constructor_stack;
11185 390 : p->range_end = range_end;
11186 390 : if (constructor_range_stack)
11187 19 : constructor_range_stack->next = p;
11188 390 : constructor_range_stack = p;
11189 390 : }
11190 :
11191 : /* Within an array initializer, specify the next index to be initialized.
11192 : FIRST is that index. If LAST is nonzero, then initialize a range
11193 : of indices, running from FIRST through LAST. */
11194 :
11195 : void
11196 1149 : set_init_index (location_t loc, tree first, tree last,
11197 : struct obstack *braced_init_obstack)
11198 : {
11199 1149 : if (set_designator (loc, true, braced_init_obstack))
11200 : return;
11201 :
11202 1146 : designator_erroneous = 1;
11203 :
11204 2292 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11205 2281 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11206 : {
11207 12 : error_init (loc, "array index in initializer not of integer type");
11208 12 : return;
11209 : }
11210 :
11211 1134 : if (TREE_CODE (first) != INTEGER_CST)
11212 : {
11213 6 : first = c_fully_fold (first, false, NULL);
11214 6 : if (TREE_CODE (first) == INTEGER_CST)
11215 5 : pedwarn_init (loc, OPT_Wpedantic,
11216 : "array index in initializer is not "
11217 : "an integer constant expression");
11218 : }
11219 :
11220 1134 : if (last && TREE_CODE (last) != INTEGER_CST)
11221 : {
11222 2 : last = c_fully_fold (last, false, NULL);
11223 2 : if (TREE_CODE (last) == INTEGER_CST)
11224 1 : pedwarn_init (loc, OPT_Wpedantic,
11225 : "array index in initializer is not "
11226 : "an integer constant expression");
11227 : }
11228 :
11229 1134 : if (TREE_CODE (first) != INTEGER_CST)
11230 1 : error_init (loc, "nonconstant array index in initializer");
11231 1133 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11232 1 : error_init (loc, "nonconstant array index in initializer");
11233 1132 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11234 9 : error_init (loc, "array index in non-array initializer");
11235 1123 : else if (tree_int_cst_sgn (first) == -1)
11236 15 : error_init (loc, "array index in initializer exceeds array bounds");
11237 1108 : else if (constructor_max_index
11238 1108 : && tree_int_cst_lt (constructor_max_index, first))
11239 7 : error_init (loc, "array index in initializer exceeds array bounds");
11240 : else
11241 : {
11242 1101 : constant_expression_warning (first);
11243 1101 : if (last)
11244 385 : constant_expression_warning (last);
11245 1101 : constructor_index = convert (bitsizetype, first);
11246 1101 : if (tree_int_cst_lt (constructor_index, first))
11247 : {
11248 0 : constructor_index = copy_node (constructor_index);
11249 0 : TREE_OVERFLOW (constructor_index) = 1;
11250 : }
11251 :
11252 1101 : if (last)
11253 : {
11254 385 : if (tree_int_cst_equal (first, last))
11255 : last = NULL_TREE;
11256 384 : else if (tree_int_cst_lt (last, first))
11257 : {
11258 2 : error_init (loc, "empty index range in initializer");
11259 2 : last = NULL_TREE;
11260 : }
11261 : else
11262 : {
11263 382 : last = convert (bitsizetype, last);
11264 382 : if (constructor_max_index != NULL_TREE
11265 382 : && tree_int_cst_lt (constructor_max_index, last))
11266 : {
11267 3 : error_init (loc, "array index range in initializer exceeds "
11268 : "array bounds");
11269 3 : last = NULL_TREE;
11270 : }
11271 : }
11272 : }
11273 :
11274 1101 : designator_depth++;
11275 1101 : designator_erroneous = 0;
11276 1101 : if (constructor_range_stack || last)
11277 379 : push_range_stack (last, braced_init_obstack);
11278 : }
11279 : }
11280 :
11281 : /* Within a struct initializer, specify the next field to be initialized. */
11282 :
11283 : void
11284 32867 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11285 : struct obstack *braced_init_obstack)
11286 : {
11287 32867 : tree field;
11288 :
11289 32867 : if (set_designator (loc, false, braced_init_obstack))
11290 : return;
11291 :
11292 32850 : designator_erroneous = 1;
11293 :
11294 32850 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11295 : {
11296 3 : error_init (loc, "field name not in record or union initializer");
11297 3 : return;
11298 : }
11299 :
11300 32847 : field = lookup_field (constructor_type, fieldname);
11301 :
11302 32847 : if (field == NULL_TREE)
11303 : {
11304 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11305 8 : if (guessed_id)
11306 : {
11307 4 : gcc_rich_location rich_loc (fieldname_loc);
11308 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11309 4 : error_at (&rich_loc,
11310 : "%qT has no member named %qE; did you mean %qE?",
11311 : constructor_type, fieldname, guessed_id);
11312 4 : }
11313 : else
11314 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11315 : constructor_type, fieldname);
11316 : }
11317 : else
11318 32884 : do
11319 : {
11320 32884 : constructor_fields = TREE_VALUE (field);
11321 32884 : designator_depth++;
11322 32884 : designator_erroneous = 0;
11323 32884 : if (constructor_range_stack)
11324 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11325 32884 : field = TREE_CHAIN (field);
11326 32884 : if (field)
11327 : {
11328 45 : if (set_designator (loc, false, braced_init_obstack))
11329 : return;
11330 : }
11331 : }
11332 32884 : while (field != NULL_TREE);
11333 : }
11334 :
11335 : /* Helper function for add_pending_init. Find inorder successor of P
11336 : in AVL tree. */
11337 : static struct init_node *
11338 129 : init_node_successor (struct init_node *p)
11339 : {
11340 129 : struct init_node *r;
11341 129 : if (p->right)
11342 : {
11343 : r = p->right;
11344 58 : while (r->left)
11345 : r = r->left;
11346 : return r;
11347 : }
11348 75 : r = p->parent;
11349 114 : while (r && p == r->right)
11350 : {
11351 39 : p = r;
11352 39 : r = r->parent;
11353 : }
11354 : return r;
11355 : }
11356 :
11357 : /* Add a new initializer to the tree of pending initializers. PURPOSE
11358 : identifies the initializer, either array index or field in a structure.
11359 : VALUE is the value of that index or field. If ORIGTYPE is not
11360 : NULL_TREE, it is the original type of VALUE.
11361 :
11362 : IMPLICIT is true if value comes from pop_init_level (1),
11363 : the new initializer has been merged with the existing one
11364 : and thus no warnings should be emitted about overriding an
11365 : existing initializer. */
11366 :
11367 : static void
11368 3269 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11369 : bool implicit, struct obstack *braced_init_obstack)
11370 : {
11371 3269 : struct init_node *p, **q, *r;
11372 :
11373 3269 : q = &constructor_pending_elts;
11374 3269 : p = 0;
11375 :
11376 3269 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11377 : {
11378 3610 : while (*q != 0)
11379 : {
11380 2412 : p = *q;
11381 2412 : if (tree_int_cst_lt (purpose, p->purpose))
11382 283 : q = &p->left;
11383 2129 : else if (tree_int_cst_lt (p->purpose, purpose))
11384 : {
11385 1960 : if (TREE_CODE (p->value) != RAW_DATA_CST
11386 1960 : || (p->right
11387 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11388 1830 : q = &p->right;
11389 : else
11390 : {
11391 130 : widest_int pp = wi::to_widest (p->purpose);
11392 130 : widest_int pw = wi::to_widest (purpose);
11393 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11394 98 : q = &p->right;
11395 : else
11396 : {
11397 : /* Override which should split the old RAW_DATA_CST
11398 : into 2 or 3 pieces. */
11399 32 : if (!implicit && warn_override_init)
11400 9 : warning_init (loc, OPT_Woverride_init,
11401 : "initialized field overwritten");
11402 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11403 32 : unsigned HOST_WIDE_INT len = 1;
11404 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11405 0 : len = RAW_DATA_LENGTH (value);
11406 32 : unsigned HOST_WIDE_INT end = 0;
11407 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11408 32 : gcc_checking_assert (start < plen && start);
11409 32 : if (plen - start > len)
11410 30 : end = plen - start - len;
11411 32 : tree v = p->value;
11412 32 : tree origtype = p->origtype;
11413 32 : if (start == 1)
11414 2 : p->value = build_int_cst (TREE_TYPE (v),
11415 2 : RAW_DATA_UCHAR_ELT (v, 0));
11416 : else
11417 : {
11418 30 : p->value = v;
11419 30 : if (end > 1)
11420 26 : v = copy_node (v);
11421 30 : RAW_DATA_LENGTH (p->value) = start;
11422 : }
11423 32 : if (end)
11424 : {
11425 30 : tree epurpose
11426 30 : = size_binop (PLUS_EXPR, purpose,
11427 : bitsize_int (len));
11428 30 : if (end > 1)
11429 : {
11430 28 : RAW_DATA_LENGTH (v) -= plen - end;
11431 28 : RAW_DATA_POINTER (v) += plen - end;
11432 : }
11433 : else
11434 2 : v = build_int_cst (TREE_TYPE (v),
11435 2 : RAW_DATA_UCHAR_ELT (v, plen
11436 : - end));
11437 30 : add_pending_init (loc, epurpose, v, origtype,
11438 : implicit, braced_init_obstack);
11439 : }
11440 32 : q = &constructor_pending_elts;
11441 32 : continue;
11442 32 : }
11443 130 : }
11444 : }
11445 : else
11446 : {
11447 169 : if (TREE_CODE (p->value) == RAW_DATA_CST
11448 179 : && (RAW_DATA_LENGTH (p->value)
11449 10 : > (TREE_CODE (value) == RAW_DATA_CST
11450 10 : ? RAW_DATA_LENGTH (value) : 1)))
11451 : {
11452 : /* Override which should split the old RAW_DATA_CST
11453 : into 2 pieces. */
11454 6 : if (!implicit && warn_override_init)
11455 3 : warning_init (loc, OPT_Woverride_init,
11456 : "initialized field overwritten");
11457 6 : unsigned HOST_WIDE_INT len = 1;
11458 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11459 2 : len = RAW_DATA_LENGTH (value);
11460 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11461 : {
11462 6 : RAW_DATA_LENGTH (p->value) -= len;
11463 6 : RAW_DATA_POINTER (p->value) += len;
11464 : }
11465 : else
11466 : {
11467 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11468 0 : p->value
11469 0 : = build_int_cst (TREE_TYPE (p->value),
11470 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11471 : }
11472 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11473 : bitsize_int (len));
11474 6 : continue;
11475 6 : }
11476 163 : if (TREE_CODE (value) == RAW_DATA_CST)
11477 : {
11478 8 : handle_raw_data:
11479 : /* RAW_DATA_CST value might overlap various further
11480 : prior initval entries. Find out how many. */
11481 13 : unsigned cnt = 0;
11482 13 : widest_int w
11483 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11484 13 : struct init_node *r = p, *last = NULL;
11485 13 : bool override_init = warn_override_init;
11486 13 : while ((r = init_node_successor (r))
11487 53 : && wi::to_widest (r->purpose) < w)
11488 : {
11489 40 : ++cnt;
11490 40 : if (TREE_SIDE_EFFECTS (r->value))
11491 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11492 : "initialized field with side-effects "
11493 : "overwritten");
11494 38 : else if (override_init)
11495 : {
11496 6 : warning_init (loc, OPT_Woverride_init,
11497 : "initialized field overwritten");
11498 6 : override_init = false;
11499 : }
11500 : last = r;
11501 : }
11502 13 : if (cnt)
11503 : {
11504 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11505 13 : && (wi::to_widest (last->purpose)
11506 13 : + RAW_DATA_LENGTH (last->value) > w))
11507 : {
11508 : /* The last overlapping prior initval overlaps
11509 : only partially. Shrink it and decrease cnt. */
11510 0 : unsigned int l = (wi::to_widest (last->purpose)
11511 0 : + RAW_DATA_LENGTH (last->value)
11512 0 : - w).to_uhwi ();
11513 0 : --cnt;
11514 0 : RAW_DATA_LENGTH (last->value) -= l;
11515 0 : RAW_DATA_POINTER (last->value) += l;
11516 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11517 0 : last->value
11518 0 : = build_int_cst (TREE_TYPE (last->value),
11519 0 : RAW_DATA_UCHAR_ELT (last->value,
11520 : 0));
11521 0 : last->purpose
11522 0 : = size_binop (PLUS_EXPR, last->purpose,
11523 : bitsize_int (l));
11524 : }
11525 : /* Instead of deleting cnt nodes from the AVL tree
11526 : and rebalancing, peel of last cnt bytes from the
11527 : RAW_DATA_CST. Overriding thousands of previously
11528 : initialized array elements with #embed needs to work,
11529 : but doesn't need to be super efficient. */
11530 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11531 : > cnt);
11532 13 : RAW_DATA_LENGTH (value) -= cnt;
11533 13 : const unsigned char *s
11534 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11535 13 : unsigned int o = RAW_DATA_LENGTH (value);
11536 53 : for (r = p; cnt--; ++o, ++s)
11537 : {
11538 40 : r = init_node_successor (r);
11539 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11540 : bitsize_int (o));
11541 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11542 40 : r->origtype = origtype;
11543 : }
11544 13 : if (RAW_DATA_LENGTH (value) == 1)
11545 0 : value = build_int_cst (TREE_TYPE (value),
11546 0 : RAW_DATA_UCHAR_ELT (value, 0));
11547 : }
11548 : }
11549 168 : if (!implicit)
11550 : {
11551 54 : if (TREE_SIDE_EFFECTS (p->value))
11552 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11553 : "initialized field with side-effects "
11554 : "overwritten");
11555 48 : else if (warn_override_init)
11556 13 : warning_init (loc, OPT_Woverride_init,
11557 : "initialized field overwritten");
11558 : }
11559 168 : p->value = value;
11560 168 : p->origtype = origtype;
11561 168 : return;
11562 : }
11563 : }
11564 1198 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11565 : {
11566 61 : struct init_node *r;
11567 61 : if (q == &p->left)
11568 : r = p;
11569 : else
11570 36 : r = init_node_successor (p);
11571 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11572 131 : + RAW_DATA_LENGTH (value)))
11573 : {
11574 : /* Overlap with at least one prior initval in the range but
11575 : not at the start. */
11576 5 : p = r;
11577 5 : p->purpose = purpose;
11578 5 : goto handle_raw_data;
11579 : }
11580 : }
11581 : }
11582 : else
11583 : {
11584 1908 : tree bitpos;
11585 :
11586 1908 : bitpos = bit_position (purpose);
11587 5430 : while (*q != NULL)
11588 : {
11589 1871 : p = *q;
11590 1871 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11591 43 : q = &p->left;
11592 1828 : else if (p->purpose != purpose)
11593 1571 : q = &p->right;
11594 : else
11595 : {
11596 257 : if (!implicit)
11597 : {
11598 33 : if (TREE_SIDE_EFFECTS (p->value))
11599 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11600 : "initialized field with side-effects "
11601 : "overwritten");
11602 29 : else if (warn_override_init)
11603 6 : warning_init (loc, OPT_Woverride_init,
11604 : "initialized field overwritten");
11605 : }
11606 257 : p->value = value;
11607 257 : p->origtype = origtype;
11608 257 : return;
11609 : }
11610 : }
11611 : }
11612 :
11613 2844 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11614 : sizeof (struct init_node));
11615 2844 : r->purpose = purpose;
11616 2844 : r->value = value;
11617 2844 : r->origtype = origtype;
11618 :
11619 2844 : *q = r;
11620 2844 : r->parent = p;
11621 2844 : r->left = 0;
11622 2844 : r->right = 0;
11623 2844 : r->balance = 0;
11624 :
11625 4878 : while (p)
11626 : {
11627 2826 : struct init_node *s;
11628 :
11629 2826 : if (r == p->left)
11630 : {
11631 237 : if (p->balance == 0)
11632 174 : p->balance = -1;
11633 63 : else if (p->balance < 0)
11634 : {
11635 45 : if (r->balance < 0)
11636 : {
11637 : /* L rotation. */
11638 40 : p->left = r->right;
11639 40 : if (p->left)
11640 2 : p->left->parent = p;
11641 40 : r->right = p;
11642 :
11643 40 : p->balance = 0;
11644 40 : r->balance = 0;
11645 :
11646 40 : s = p->parent;
11647 40 : p->parent = r;
11648 40 : r->parent = s;
11649 40 : if (s)
11650 : {
11651 31 : if (s->left == p)
11652 10 : s->left = r;
11653 : else
11654 21 : s->right = r;
11655 : }
11656 : else
11657 9 : constructor_pending_elts = r;
11658 : }
11659 : else
11660 : {
11661 : /* LR rotation. */
11662 5 : struct init_node *t = r->right;
11663 :
11664 5 : r->right = t->left;
11665 5 : if (r->right)
11666 0 : r->right->parent = r;
11667 5 : t->left = r;
11668 :
11669 5 : p->left = t->right;
11670 5 : if (p->left)
11671 0 : p->left->parent = p;
11672 5 : t->right = p;
11673 :
11674 5 : p->balance = t->balance < 0;
11675 5 : r->balance = -(t->balance > 0);
11676 5 : t->balance = 0;
11677 :
11678 5 : s = p->parent;
11679 5 : p->parent = t;
11680 5 : r->parent = t;
11681 5 : t->parent = s;
11682 5 : if (s)
11683 : {
11684 0 : if (s->left == p)
11685 0 : s->left = t;
11686 : else
11687 0 : s->right = t;
11688 : }
11689 : else
11690 5 : constructor_pending_elts = t;
11691 : }
11692 : break;
11693 : }
11694 : else
11695 : {
11696 : /* p->balance == +1; growth of left side balances the node. */
11697 18 : p->balance = 0;
11698 18 : break;
11699 : }
11700 : }
11701 : else /* r == p->right */
11702 : {
11703 2589 : if (p->balance == 0)
11704 : /* Growth propagation from right side. */
11705 1860 : p->balance++;
11706 729 : else if (p->balance > 0)
11707 : {
11708 700 : if (r->balance > 0)
11709 : {
11710 : /* R rotation. */
11711 698 : p->right = r->left;
11712 698 : if (p->right)
11713 173 : p->right->parent = p;
11714 698 : r->left = p;
11715 :
11716 698 : p->balance = 0;
11717 698 : r->balance = 0;
11718 :
11719 698 : s = p->parent;
11720 698 : p->parent = r;
11721 698 : r->parent = s;
11722 698 : if (s)
11723 : {
11724 378 : if (s->left == p)
11725 2 : s->left = r;
11726 : else
11727 376 : s->right = r;
11728 : }
11729 : else
11730 320 : constructor_pending_elts = r;
11731 : }
11732 : else /* r->balance == -1 */
11733 : {
11734 : /* RL rotation */
11735 2 : struct init_node *t = r->left;
11736 :
11737 2 : r->left = t->right;
11738 2 : if (r->left)
11739 2 : r->left->parent = r;
11740 2 : t->right = r;
11741 :
11742 2 : p->right = t->left;
11743 2 : if (p->right)
11744 2 : p->right->parent = p;
11745 2 : t->left = p;
11746 :
11747 2 : r->balance = (t->balance < 0);
11748 2 : p->balance = -(t->balance > 0);
11749 2 : t->balance = 0;
11750 :
11751 2 : s = p->parent;
11752 2 : p->parent = t;
11753 2 : r->parent = t;
11754 2 : t->parent = s;
11755 2 : if (s)
11756 : {
11757 0 : if (s->left == p)
11758 0 : s->left = t;
11759 : else
11760 0 : s->right = t;
11761 : }
11762 : else
11763 2 : constructor_pending_elts = t;
11764 : }
11765 : break;
11766 : }
11767 : else
11768 : {
11769 : /* p->balance == -1; growth of right side balances the node. */
11770 29 : p->balance = 0;
11771 29 : break;
11772 : }
11773 : }
11774 :
11775 2034 : r = p;
11776 2034 : p = p->parent;
11777 : }
11778 : }
11779 :
11780 : /* Build AVL tree from a sorted chain. */
11781 :
11782 : static void
11783 418 : set_nonincremental_init (struct obstack * braced_init_obstack)
11784 : {
11785 418 : unsigned HOST_WIDE_INT ix;
11786 418 : tree index, value;
11787 :
11788 418 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11789 418 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11790 : return;
11791 :
11792 1601 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11793 1183 : add_pending_init (input_location, index, value, NULL_TREE, true,
11794 : braced_init_obstack);
11795 418 : constructor_elements = NULL;
11796 418 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11797 : {
11798 200 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11799 : /* Skip any nameless bit fields at the beginning. */
11800 200 : while (constructor_unfilled_fields != NULL_TREE
11801 200 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11802 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11803 :
11804 : }
11805 218 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11806 : {
11807 218 : if (TYPE_DOMAIN (constructor_type))
11808 160 : constructor_unfilled_index
11809 160 : = convert (bitsizetype,
11810 160 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11811 : else
11812 58 : constructor_unfilled_index = bitsize_zero_node;
11813 : }
11814 418 : constructor_incremental = 0;
11815 : }
11816 :
11817 : /* Build AVL tree from a string constant. */
11818 :
11819 : static void
11820 7 : set_nonincremental_init_from_string (tree str,
11821 : struct obstack * braced_init_obstack)
11822 : {
11823 7 : tree value, purpose, type;
11824 7 : HOST_WIDE_INT val[2];
11825 7 : const char *p, *end;
11826 7 : int byte, wchar_bytes, charwidth, bitpos;
11827 :
11828 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11829 :
11830 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11831 7 : charwidth = TYPE_PRECISION (char_type_node);
11832 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11833 : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11834 7 : type = TREE_TYPE (constructor_type);
11835 7 : p = TREE_STRING_POINTER (str);
11836 7 : end = p + TREE_STRING_LENGTH (str);
11837 :
11838 7 : for (purpose = bitsize_zero_node;
11839 : p < end
11840 52 : && !(constructor_max_index
11841 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11842 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11843 : {
11844 25 : if (wchar_bytes == 1)
11845 : {
11846 9 : val[0] = (unsigned char) *p++;
11847 9 : val[1] = 0;
11848 : }
11849 : else
11850 : {
11851 16 : val[1] = 0;
11852 16 : val[0] = 0;
11853 64 : for (byte = 0; byte < wchar_bytes; byte++)
11854 : {
11855 48 : if (BYTES_BIG_ENDIAN)
11856 : bitpos = (wchar_bytes - byte - 1) * charwidth;
11857 : else
11858 48 : bitpos = byte * charwidth;
11859 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11860 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11861 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11862 : }
11863 : }
11864 :
11865 25 : if (!TYPE_UNSIGNED (type))
11866 : {
11867 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11868 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11869 : {
11870 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11871 : {
11872 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11873 0 : val[1] = -1;
11874 : }
11875 : }
11876 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11877 : {
11878 0 : if (val[0] < 0)
11879 0 : val[1] = -1;
11880 : }
11881 0 : else if (val[1] & (HOST_WIDE_INT_1
11882 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11883 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11884 : }
11885 :
11886 25 : value = wide_int_to_tree (type,
11887 25 : wide_int::from_array (val, 2,
11888 : HOST_BITS_PER_WIDE_INT * 2));
11889 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11890 : braced_init_obstack);
11891 : }
11892 :
11893 7 : constructor_incremental = 0;
11894 7 : }
11895 :
11896 : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11897 : not initialized yet. */
11898 :
11899 : static tree
11900 703622 : find_init_member (tree field, struct obstack * braced_init_obstack)
11901 : {
11902 703622 : struct init_node *p;
11903 :
11904 703622 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11905 : {
11906 702824 : if (constructor_incremental
11907 702824 : && tree_int_cst_lt (field, constructor_unfilled_index))
11908 16 : set_nonincremental_init (braced_init_obstack);
11909 :
11910 702824 : p = constructor_pending_elts;
11911 702947 : while (p)
11912 : {
11913 233 : if (tree_int_cst_lt (field, p->purpose))
11914 25 : p = p->left;
11915 208 : else if (tree_int_cst_lt (p->purpose, field))
11916 98 : p = p->right;
11917 : else
11918 110 : return p->value;
11919 : }
11920 : }
11921 798 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11922 : {
11923 675 : tree bitpos = bit_position (field);
11924 :
11925 675 : if (constructor_incremental
11926 675 : && (!constructor_unfilled_fields
11927 472 : || tree_int_cst_lt (bitpos,
11928 472 : bit_position (constructor_unfilled_fields))))
11929 51 : set_nonincremental_init (braced_init_obstack);
11930 :
11931 675 : p = constructor_pending_elts;
11932 815 : while (p)
11933 : {
11934 347 : if (field == p->purpose)
11935 207 : return p->value;
11936 140 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11937 7 : p = p->left;
11938 : else
11939 133 : p = p->right;
11940 : }
11941 : }
11942 123 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11943 : {
11944 123 : if (!vec_safe_is_empty (constructor_elements)
11945 50 : && (constructor_elements->last ().index == field))
11946 50 : return constructor_elements->last ().value;
11947 : }
11948 : return NULL_TREE;
11949 : }
11950 :
11951 : /* "Output" the next constructor element.
11952 : At top level, really output it to assembler code now.
11953 : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11954 : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11955 : TYPE is the data type that the containing data type wants here.
11956 : FIELD is the field (a FIELD_DECL) or the index that this element fills.
11957 : If VALUE is a string constant, STRICT_STRING is true if it is
11958 : unparenthesized or we should not warn here for it being parenthesized.
11959 : For other types of VALUE, STRICT_STRING is not used.
11960 :
11961 : PENDING if true means output pending elements that belong
11962 : right after this element. (PENDING is normally true;
11963 : it is false while outputting pending elements, to avoid recursion.)
11964 :
11965 : IMPLICIT is true if value comes from pop_init_level (1),
11966 : the new initializer has been merged with the existing one
11967 : and thus no warnings should be emitted about overriding an
11968 : existing initializer. */
11969 :
11970 : static void
11971 9623118 : output_init_element (location_t loc, tree value, tree origtype,
11972 : bool strict_string, tree type, tree field, bool pending,
11973 : bool implicit, struct obstack * braced_init_obstack)
11974 : {
11975 9623118 : tree semantic_type = NULL_TREE;
11976 9623118 : bool maybe_const = true;
11977 9623118 : bool npc, int_const_expr, arith_const_expr;
11978 :
11979 9623118 : if (type == error_mark_node || value == error_mark_node)
11980 : {
11981 290 : constructor_erroneous = 1;
11982 2415 : return;
11983 : }
11984 9622828 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
11985 200021 : && (TREE_CODE (value) == STRING_CST
11986 158611 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
11987 41418 : && !(TREE_CODE (value) == STRING_CST
11988 41410 : && TREE_CODE (type) == ARRAY_TYPE
11989 3898 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
11990 9660348 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
11991 37520 : TYPE_MAIN_VARIANT (type)))
11992 37520 : value = array_to_pointer_conversion (input_location, value);
11993 :
11994 9622828 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
11995 182 : && require_constant_value && pending)
11996 : {
11997 : /* As an extension, allow initializing objects with static storage
11998 : duration with compound literals (which are then treated just as
11999 : the brace enclosed list they contain). */
12000 114 : if (flag_isoc99)
12001 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
12002 : "constant");
12003 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
12004 114 : value = DECL_INITIAL (decl);
12005 : }
12006 :
12007 9622828 : npc = null_pointer_constant_p (value);
12008 19245656 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
12009 4218887 : && !TREE_OVERFLOW (value)
12010 13841714 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
12011 : /* Not fully determined before folding. */
12012 9622828 : arith_const_expr = true;
12013 9622828 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
12014 : {
12015 16 : semantic_type = TREE_TYPE (value);
12016 16 : value = TREE_OPERAND (value, 0);
12017 : }
12018 9622828 : value = c_fully_fold (value, require_constant_value, &maybe_const);
12019 : /* TODO: this may not detect all cases of expressions folding to
12020 : constants that are not arithmetic constant expressions. */
12021 9622828 : if (!maybe_const)
12022 : arith_const_expr = false;
12023 19243099 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
12024 3185023 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
12025 11463413 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
12026 : arith_const_expr = false;
12027 7795010 : else if (TREE_CODE (value) != INTEGER_CST
12028 : && TREE_CODE (value) != REAL_CST
12029 : && TREE_CODE (value) != COMPLEX_CST
12030 : && TREE_CODE (value) != RAW_DATA_CST)
12031 : arith_const_expr = false;
12032 4607824 : else if (TREE_OVERFLOW (value))
12033 5015005 : arith_const_expr = false;
12034 :
12035 9622828 : if (value == error_mark_node)
12036 0 : constructor_erroneous = 1;
12037 9622828 : else if (!TREE_CONSTANT (value))
12038 3207475 : constructor_constant = 0;
12039 6415353 : else if (!initializer_constant_valid_p (value,
12040 6415353 : TREE_TYPE (value),
12041 6415353 : AGGREGATE_TYPE_P (constructor_type)
12042 6415353 : && TYPE_REVERSE_STORAGE_ORDER
12043 : (constructor_type))
12044 6415353 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
12045 1037417 : && DECL_C_BIT_FIELD (field)
12046 7995 : && TREE_CODE (value) != INTEGER_CST))
12047 77 : constructor_simple = 0;
12048 9622828 : if (!maybe_const)
12049 1172 : constructor_nonconst = 1;
12050 :
12051 : /* Digest the initializer and issue any errors about incompatible
12052 : types before issuing errors about non-constant initializers. */
12053 9622828 : tree new_value = value;
12054 9622828 : if (semantic_type)
12055 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
12056 : /* In the case of braces around a scalar initializer, the result of
12057 : this initializer processing goes through digest_init again at the
12058 : outer level. In the case of a constexpr initializer for a
12059 : pointer, avoid converting a null pointer constant to something
12060 : that is not a null pointer constant to avoid a spurious error
12061 : from that second processing. */
12062 9622828 : if (!require_constexpr_value
12063 398 : || !npc
12064 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
12065 9622817 : new_value = digest_init (loc,
12066 9622817 : RECORD_OR_UNION_TYPE_P (constructor_type)
12067 8553206 : ? field : constructor_fields
12068 8553206 : ? constructor_fields : constructor_decl,
12069 : type, new_value, origtype, npc,
12070 : int_const_expr, arith_const_expr, strict_string,
12071 : require_constant_value, require_constexpr_value);
12072 9622828 : if (new_value == error_mark_node)
12073 : {
12074 76 : constructor_erroneous = 1;
12075 76 : return;
12076 : }
12077 9622752 : if (require_constant_value || require_constant_elements)
12078 2711864 : constant_expression_warning (new_value);
12079 :
12080 : /* Proceed to check the constness of the original initializer. */
12081 9622752 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
12082 : {
12083 3207501 : if (require_constant_value)
12084 : {
12085 0 : error_init (loc, "initializer element is not constant");
12086 0 : value = error_mark_node;
12087 : }
12088 3207501 : else if (require_constant_elements)
12089 15 : pedwarn (loc, OPT_Wpedantic,
12090 : "initializer element is not computable at load time");
12091 : }
12092 6415251 : else if (!maybe_const
12093 54 : && (require_constant_value || require_constant_elements))
12094 28 : pedwarn_init (loc, OPT_Wpedantic,
12095 : "initializer element is not a constant expression");
12096 : /* digest_init has already carried out the additional checks
12097 : required for 'constexpr' initializers (using the information
12098 : passed to it about whether the original initializer was certain
12099 : kinds of constant expression), so that check does not need to be
12100 : repeated here. */
12101 :
12102 : /* Issue -Wc++-compat warnings about initializing a bitfield with
12103 : enum type. */
12104 9622752 : if (warn_cxx_compat
12105 6895 : && field != NULL_TREE
12106 6895 : && TREE_CODE (field) == FIELD_DECL
12107 459 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
12108 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
12109 21 : != TYPE_MAIN_VARIANT (type))
12110 9622773 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
12111 : {
12112 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
12113 21 : if (checktype != error_mark_node
12114 21 : && (TYPE_MAIN_VARIANT (checktype)
12115 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
12116 9 : warning_init (loc, OPT_Wc___compat,
12117 : "enum conversion in initialization is invalid in C++");
12118 : }
12119 :
12120 : /* If this field is empty and does not have side effects (and is not at
12121 : the end of structure), don't do anything other than checking the
12122 : initializer. */
12123 9622752 : if (field
12124 9622752 : && (TREE_TYPE (field) == error_mark_node
12125 9622317 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
12126 9622008 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
12127 62 : && !TREE_SIDE_EFFECTS (new_value)
12128 33 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
12129 33 : || DECL_CHAIN (field)))))
12130 18 : return;
12131 :
12132 : /* Finally, set VALUE to the initializer value digested above. */
12133 9622734 : value = new_value;
12134 :
12135 : /* If this element doesn't come next in sequence,
12136 : put it on constructor_pending_elts. */
12137 9622734 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
12138 9622734 : && (!constructor_incremental
12139 1717628 : || !tree_int_cst_equal (field, constructor_unfilled_index)
12140 1717262 : || (TREE_CODE (value) == RAW_DATA_CST
12141 234 : && constructor_pending_elts
12142 66 : && pending)))
12143 : {
12144 627 : if (constructor_incremental
12145 627 : && (tree_int_cst_lt (field, constructor_unfilled_index)
12146 337 : || (TREE_CODE (value) == RAW_DATA_CST
12147 14 : && constructor_pending_elts
12148 14 : && pending)))
12149 48 : set_nonincremental_init (braced_init_obstack);
12150 :
12151 627 : add_pending_init (loc, field, value, origtype, implicit,
12152 : braced_init_obstack);
12153 627 : return;
12154 : }
12155 9622107 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12156 1038470 : && (!constructor_incremental
12157 1038130 : || field != constructor_unfilled_fields))
12158 : {
12159 : /* We do this for records but not for unions. In a union,
12160 : no matter which field is specified, it can be initialized
12161 : right away since it starts at the beginning of the union. */
12162 1404 : if (constructor_incremental)
12163 : {
12164 1064 : if (!constructor_unfilled_fields)
12165 5 : set_nonincremental_init (braced_init_obstack);
12166 : else
12167 : {
12168 1059 : tree bitpos, unfillpos;
12169 :
12170 1059 : bitpos = bit_position (field);
12171 1059 : unfillpos = bit_position (constructor_unfilled_fields);
12172 :
12173 1059 : if (tree_int_cst_lt (bitpos, unfillpos))
12174 4 : set_nonincremental_init (braced_init_obstack);
12175 : }
12176 : }
12177 :
12178 1404 : add_pending_init (loc, field, value, origtype, implicit,
12179 : braced_init_obstack);
12180 1404 : return;
12181 : }
12182 9620703 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12183 9620703 : && !vec_safe_is_empty (constructor_elements))
12184 : {
12185 62 : if (!implicit)
12186 : {
12187 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12188 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12189 : "initialized field with side-effects overwritten");
12190 9 : else if (warn_override_init)
12191 6 : warning_init (loc, OPT_Woverride_init,
12192 : "initialized field overwritten");
12193 : }
12194 :
12195 : /* We can have just one union field set. */
12196 62 : constructor_elements = NULL;
12197 : }
12198 :
12199 : /* Otherwise, output this element either to
12200 : constructor_elements or to the assembler file. */
12201 :
12202 9620703 : constructor_elt celt = {field, value};
12203 9620703 : vec_safe_push (constructor_elements, celt);
12204 :
12205 : /* Advance the variable that indicates sequential elements output. */
12206 9620703 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12207 : {
12208 1717257 : tree inc = bitsize_one_node;
12209 1717257 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12210 229 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12211 1717257 : constructor_unfilled_index
12212 1717257 : = size_binop_loc (input_location, PLUS_EXPR,
12213 : constructor_unfilled_index, inc);
12214 : }
12215 7903446 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12216 : {
12217 1037066 : constructor_unfilled_fields
12218 1037066 : = DECL_CHAIN (constructor_unfilled_fields);
12219 :
12220 : /* Skip any nameless bit fields. */
12221 1037066 : while (constructor_unfilled_fields != NULL_TREE
12222 1037167 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12223 101 : constructor_unfilled_fields
12224 101 : = DECL_CHAIN (constructor_unfilled_fields);
12225 : }
12226 6866380 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12227 31109 : constructor_unfilled_fields = NULL_TREE;
12228 :
12229 : /* Now output any pending elements which have become next. */
12230 9620703 : if (pending)
12231 9617838 : output_pending_init_elements (0, braced_init_obstack);
12232 : }
12233 :
12234 : /* For two FIELD_DECLs in the same chain, return -1 if field1
12235 : comes before field2, 1 if field1 comes after field2 and
12236 : 0 if field1 == field2. */
12237 :
12238 : static int
12239 4029 : init_field_decl_cmp (tree field1, tree field2)
12240 : {
12241 4029 : if (field1 == field2)
12242 : return 0;
12243 :
12244 2154 : tree bitpos1 = bit_position (field1);
12245 2154 : tree bitpos2 = bit_position (field2);
12246 2154 : if (tree_int_cst_equal (bitpos1, bitpos2))
12247 : {
12248 : /* If one of the fields has non-zero bitsize, then that
12249 : field must be the last one in a sequence of zero
12250 : sized fields, fields after it will have bigger
12251 : bit_position. */
12252 22 : if (TREE_TYPE (field1) != error_mark_node
12253 22 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12254 44 : && integer_nonzerop (TREE_TYPE (field1)))
12255 : return 1;
12256 22 : if (TREE_TYPE (field2) != error_mark_node
12257 22 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12258 37 : && integer_nonzerop (TREE_TYPE (field2)))
12259 : return -1;
12260 : /* Otherwise, fallback to DECL_CHAIN walk to find out
12261 : which field comes earlier. Walk chains of both
12262 : fields, so that if field1 and field2 are close to each
12263 : other in either order, it is found soon even for large
12264 : sequences of zero sized fields. */
12265 : tree f1 = field1, f2 = field2;
12266 22 : while (1)
12267 : {
12268 22 : f1 = DECL_CHAIN (f1);
12269 22 : f2 = DECL_CHAIN (f2);
12270 22 : if (f1 == NULL_TREE)
12271 : {
12272 7 : gcc_assert (f2);
12273 : return 1;
12274 : }
12275 15 : if (f2 == NULL_TREE)
12276 : return -1;
12277 1 : if (f1 == field2)
12278 : return -1;
12279 0 : if (f2 == field1)
12280 : return 1;
12281 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12282 : return 1;
12283 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12284 : return -1;
12285 : }
12286 : }
12287 2132 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12288 : return -1;
12289 : else
12290 : return 1;
12291 : }
12292 :
12293 : /* Output any pending elements which have become next.
12294 : As we output elements, constructor_unfilled_{fields,index}
12295 : advances, which may cause other elements to become next;
12296 : if so, they too are output.
12297 :
12298 : If ALL is 0, we return when there are
12299 : no more pending elements to output now.
12300 :
12301 : If ALL is 1, we output space as necessary so that
12302 : we can output all the pending elements. */
12303 : static void
12304 11686156 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12305 : {
12306 11686156 : struct init_node *elt = constructor_pending_elts;
12307 11687156 : tree next;
12308 :
12309 11687156 : retry:
12310 :
12311 : /* Look through the whole pending tree.
12312 : If we find an element that should be output now,
12313 : output it. Otherwise, set NEXT to the element
12314 : that comes first among those still pending. */
12315 :
12316 11687156 : next = NULL_TREE;
12317 11692806 : while (elt)
12318 : {
12319 7598 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12320 : {
12321 3004 : if (tree_int_cst_equal (elt->purpose,
12322 : constructor_unfilled_index))
12323 1197 : output_init_element (input_location, elt->value, elt->origtype,
12324 1197 : true, TREE_TYPE (constructor_type),
12325 : constructor_unfilled_index, false, false,
12326 : braced_init_obstack);
12327 1807 : else if (tree_int_cst_lt (constructor_unfilled_index,
12328 1807 : elt->purpose))
12329 : {
12330 : /* Advance to the next smaller node. */
12331 589 : if (elt->left)
12332 : elt = elt->left;
12333 : else
12334 : {
12335 : /* We have reached the smallest node bigger than the
12336 : current unfilled index. Fill the space first. */
12337 209 : next = elt->purpose;
12338 209 : break;
12339 : }
12340 : }
12341 : else
12342 : {
12343 : /* Advance to the next bigger node. */
12344 1218 : if (elt->right)
12345 : elt = elt->right;
12346 : else
12347 : {
12348 : /* We have reached the biggest node in a subtree. Find
12349 : the parent of it, which is the next bigger node. */
12350 1218 : while (elt->parent && elt->parent->right == elt)
12351 : elt = elt->parent;
12352 774 : elt = elt->parent;
12353 1149 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12354 375 : elt->purpose))
12355 : {
12356 22 : next = elt->purpose;
12357 22 : break;
12358 : }
12359 : }
12360 : }
12361 : }
12362 4594 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12363 : {
12364 : /* If the current record is complete we are done. */
12365 4594 : if (constructor_unfilled_fields == NULL_TREE)
12366 : break;
12367 :
12368 3755 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12369 : elt->purpose);
12370 3755 : if (cmp == 0)
12371 1668 : output_init_element (input_location, elt->value, elt->origtype,
12372 1668 : true, TREE_TYPE (elt->purpose),
12373 : elt->purpose, false, false,
12374 : braced_init_obstack);
12375 2087 : else if (cmp < 0)
12376 : {
12377 : /* Advance to the next smaller node. */
12378 1092 : if (elt->left)
12379 : elt = elt->left;
12380 : else
12381 : {
12382 : /* We have reached the smallest node bigger than the
12383 : current unfilled field. Fill the space first. */
12384 811 : next = elt->purpose;
12385 811 : break;
12386 : }
12387 : }
12388 : else
12389 : {
12390 : /* Advance to the next bigger node. */
12391 995 : if (elt->right)
12392 : elt = elt->right;
12393 : else
12394 : {
12395 : /* We have reached the biggest node in a subtree. Find
12396 : the parent of it, which is the next bigger node. */
12397 735 : while (elt->parent && elt->parent->right == elt)
12398 : elt = elt->parent;
12399 519 : elt = elt->parent;
12400 519 : if (elt
12401 519 : && init_field_decl_cmp (constructor_unfilled_fields,
12402 : elt->purpose) < 0)
12403 : {
12404 67 : next = elt->purpose;
12405 67 : break;
12406 : }
12407 : }
12408 : }
12409 : }
12410 : }
12411 :
12412 : /* Ordinarily return, but not if we want to output all
12413 : and there are elements left. */
12414 11687156 : if (!(all && next != NULL_TREE))
12415 11686156 : return;
12416 :
12417 : /* If it's not incremental, just skip over the gap, so that after
12418 : jumping to retry we will output the next successive element. */
12419 1000 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12420 808 : constructor_unfilled_fields = next;
12421 192 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12422 192 : constructor_unfilled_index = next;
12423 :
12424 : /* ELT now points to the node in the pending tree with the next
12425 : initializer to output. */
12426 1000 : goto retry;
12427 : }
12428 :
12429 : /* Expression VALUE coincides with the start of type TYPE in a braced
12430 : initializer. Return true if we should treat VALUE as initializing
12431 : the first element of TYPE, false if we should treat it as initializing
12432 : TYPE as a whole.
12433 :
12434 : If the initializer is clearly invalid, the question becomes:
12435 : which choice gives the best error message? */
12436 :
12437 : static bool
12438 3483904 : initialize_elementwise_p (tree type, tree value)
12439 : {
12440 3483904 : if (type == error_mark_node || value == error_mark_node)
12441 : return false;
12442 :
12443 3483635 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12444 :
12445 3483635 : tree value_type = TREE_TYPE (value);
12446 3483635 : if (value_type == error_mark_node)
12447 : return false;
12448 :
12449 : /* GNU vectors can be initialized elementwise. However, treat any
12450 : kind of vector value as initializing the vector type as a whole,
12451 : regardless of whether the value is a GNU vector. Such initializers
12452 : are valid if and only if they would have been valid in a non-braced
12453 : initializer like:
12454 :
12455 : TYPE foo = VALUE;
12456 :
12457 : so recursing into the vector type would be at best confusing or at
12458 : worst wrong. For example, when -flax-vector-conversions is in effect,
12459 : it's possible to initialize a V8HI from a V4SI, even though the vectors
12460 : have different element types and different numbers of elements. */
12461 3483635 : if (gnu_vector_type_p (type))
12462 19532 : return !VECTOR_TYPE_P (value_type);
12463 :
12464 3464103 : if (AGGREGATE_TYPE_P (type))
12465 1741155 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12466 :
12467 : return false;
12468 : }
12469 :
12470 : /* Helper function for process_init_element. Split first element of
12471 : RAW_DATA_CST and save the rest to *RAW_DATA. */
12472 :
12473 : static inline tree
12474 9620134 : maybe_split_raw_data (tree value, tree *raw_data)
12475 : {
12476 9620134 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12477 : return value;
12478 211 : *raw_data = value;
12479 211 : value = build_int_cst (integer_type_node,
12480 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12481 211 : ++RAW_DATA_POINTER (*raw_data);
12482 211 : --RAW_DATA_LENGTH (*raw_data);
12483 211 : return value;
12484 : }
12485 :
12486 : /* Return non-zero if c_parser_initval should attempt to optimize
12487 : large initializers into RAW_DATA_CST. In that case return how
12488 : many elements to optimize at most. */
12489 :
12490 : unsigned
12491 2991041 : c_maybe_optimize_large_byte_initializer (void)
12492 : {
12493 2991041 : if (!constructor_type
12494 2990971 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12495 208854 : || constructor_stack->implicit)
12496 : return 0;
12497 206089 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12498 206089 : if (TREE_CODE (elttype) != INTEGER_TYPE
12499 206089 : && TREE_CODE (elttype) != BITINT_TYPE)
12500 : return 0;
12501 183444 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12502 27869 : || constructor_stack->replacement_value.value
12503 27869 : || (COMPLETE_TYPE_P (constructor_type)
12504 26232 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12505 211313 : || constructor_range_stack)
12506 : return 0;
12507 27869 : if (constructor_max_index == NULL_TREE)
12508 : return INT_MAX;
12509 26232 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12510 26232 : || integer_all_onesp (constructor_max_index))
12511 4770 : return 0;
12512 21462 : widest_int w = wi::to_widest (constructor_max_index);
12513 21462 : w -= wi::to_widest (constructor_index);
12514 21462 : w += 1;
12515 21462 : if (w < 64)
12516 : return 0;
12517 6889 : if (w > INT_MAX)
12518 : return INT_MAX;
12519 6889 : return w.to_uhwi ();
12520 21462 : }
12521 :
12522 : /* Add one non-braced element to the current constructor level.
12523 : This adjusts the current position within the constructor's type.
12524 : This may also start or terminate implicit levels
12525 : to handle a partly-braced initializer.
12526 :
12527 : Once this has found the correct level for the new element,
12528 : it calls output_init_element.
12529 :
12530 : IMPLICIT is true if value comes from pop_init_level (1),
12531 : the new initializer has been merged with the existing one
12532 : and thus no warnings should be emitted about overriding an
12533 : existing initializer. */
12534 :
12535 : void
12536 9610427 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12537 : struct obstack * braced_init_obstack)
12538 : {
12539 9610427 : tree orig_value = value.value;
12540 19220854 : int string_flag
12541 9610427 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12542 9610427 : bool strict_string = value.original_code == STRING_CST;
12543 9610427 : bool was_designated = designator_depth != 0;
12544 9610427 : tree raw_data = NULL_TREE;
12545 :
12546 9610639 : retry:
12547 9610639 : designator_depth = 0;
12548 9610639 : designator_erroneous = 0;
12549 :
12550 9610639 : if (!implicit && value.value && !integer_zerop (value.value))
12551 7587874 : constructor_zeroinit = 0;
12552 :
12553 : /* Handle superfluous braces around string cst as in
12554 : char x[] = {"foo"}; */
12555 9610639 : if (constructor_type
12556 9610511 : && !was_designated
12557 9576525 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12558 1704920 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12559 10346519 : && integer_zerop (constructor_unfilled_index))
12560 : {
12561 163066 : if (constructor_stack->replacement_value.value)
12562 : {
12563 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12564 409 : return;
12565 : }
12566 163058 : else if (string_flag)
12567 : {
12568 138 : constructor_stack->replacement_value = value;
12569 138 : return;
12570 : }
12571 : }
12572 :
12573 9610493 : if (constructor_stack->replacement_value.value != NULL_TREE)
12574 : {
12575 0 : error_init (loc, "excess elements in struct initializer");
12576 0 : return;
12577 : }
12578 :
12579 : /* Ignore elements of a brace group if it is entirely superfluous
12580 : and has already been diagnosed, or if the type is erroneous. */
12581 9610493 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12582 : return;
12583 :
12584 : /* Ignore elements of an initializer for a variable-size type.
12585 : Those are diagnosed in the parser (empty initializer braces are OK). */
12586 9610296 : if (COMPLETE_TYPE_P (constructor_type)
12587 9610296 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12588 : return;
12589 :
12590 8906616 : if (!implicit && warn_designated_init && !was_designated
12591 8873247 : && TREE_CODE (constructor_type) == RECORD_TYPE
12592 10644631 : && lookup_attribute ("designated_init",
12593 1034393 : TYPE_ATTRIBUTES (constructor_type)))
12594 50 : warning_init (loc,
12595 : OPT_Wdesignated_init,
12596 : "positional initialization of field "
12597 : "in %<struct%> declared with %<designated_init%> attribute");
12598 :
12599 : /* If we've exhausted any levels that didn't have braces,
12600 : pop them now. */
12601 10312217 : while (constructor_stack->implicit)
12602 : {
12603 706849 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12604 702792 : && constructor_fields == NULL_TREE)
12605 701738 : process_init_element (loc,
12606 : pop_init_level (loc, 1, braced_init_obstack,
12607 : last_init_list_comma),
12608 : true, braced_init_obstack);
12609 5111 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12610 1698 : || gnu_vector_type_p (constructor_type))
12611 4057 : && constructor_max_index
12612 9141 : && tree_int_cst_lt (constructor_max_index,
12613 : constructor_index))
12614 241 : process_init_element (loc,
12615 : pop_init_level (loc, 1, braced_init_obstack,
12616 : last_init_list_comma),
12617 : true, braced_init_obstack);
12618 : else
12619 : break;
12620 : }
12621 :
12622 : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12623 9610238 : if (constructor_range_stack)
12624 : {
12625 : /* If value is a compound literal and we'll be just using its
12626 : content, don't put it into a SAVE_EXPR. */
12627 371 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12628 3 : || !require_constant_value)
12629 : {
12630 368 : tree semantic_type = NULL_TREE;
12631 368 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12632 : {
12633 0 : semantic_type = TREE_TYPE (value.value);
12634 0 : value.value = TREE_OPERAND (value.value, 0);
12635 : }
12636 368 : value.value = save_expr (value.value);
12637 368 : if (semantic_type)
12638 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12639 : value.value);
12640 : }
12641 : }
12642 :
12643 10324482 : while (1)
12644 : {
12645 10324482 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12646 : {
12647 1038542 : tree fieldtype;
12648 1038542 : enum tree_code fieldcode;
12649 :
12650 1038542 : if (constructor_fields == NULL_TREE)
12651 : {
12652 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12653 1280 : break;
12654 : }
12655 :
12656 1037262 : fieldtype = TREE_TYPE (constructor_fields);
12657 1037262 : if (fieldtype != error_mark_node)
12658 1037261 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12659 1037262 : fieldcode = TREE_CODE (fieldtype);
12660 :
12661 : /* Error for non-static initialization of a flexible array member. */
12662 1037262 : if (fieldcode == ARRAY_TYPE
12663 155216 : && !require_constant_value
12664 1128 : && TYPE_SIZE (fieldtype) == NULL_TREE
12665 1037272 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12666 : {
12667 10 : error_init (loc, "non-static initialization of a flexible "
12668 : "array member");
12669 10 : break;
12670 : }
12671 :
12672 : /* Error for initialization of a flexible array member with
12673 : a string constant if the structure is in an array. E.g.:
12674 : struct S { int x; char y[]; };
12675 : struct S s[] = { { 1, "foo" } };
12676 : is invalid. */
12677 1037252 : if (string_flag
12678 1037252 : && fieldcode == ARRAY_TYPE
12679 3353 : && constructor_depth > 1
12680 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12681 1037299 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12682 : {
12683 47 : bool in_array_p = false;
12684 47 : for (struct constructor_stack *p = constructor_stack;
12685 73 : p && p->type; p = p->next)
12686 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12687 : {
12688 : in_array_p = true;
12689 : break;
12690 : }
12691 47 : if (in_array_p)
12692 : {
12693 33 : error_init (loc, "initialization of flexible array "
12694 : "member in a nested context");
12695 33 : break;
12696 : }
12697 : }
12698 :
12699 : /* Accept a string constant to initialize a subarray. */
12700 1037219 : if (value.value != NULL_TREE
12701 1037146 : && fieldcode == ARRAY_TYPE
12702 155100 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12703 1191832 : && string_flag)
12704 : value.value = orig_value;
12705 : /* Otherwise, if we have come to a subaggregate,
12706 : and we don't have an element of its type, push into it. */
12707 1034203 : else if (value.value != NULL_TREE
12708 1033899 : && initialize_elementwise_p (fieldtype, value.value))
12709 : {
12710 304 : push_init_level (loc, 1, braced_init_obstack);
12711 304 : continue;
12712 : }
12713 :
12714 1036915 : value.value = maybe_split_raw_data (value.value, &raw_data);
12715 1036915 : if (value.value)
12716 : {
12717 1036842 : push_member_name (constructor_fields);
12718 1036842 : output_init_element (loc, value.value, value.original_type,
12719 : strict_string, fieldtype,
12720 : constructor_fields, true, implicit,
12721 : braced_init_obstack);
12722 1036842 : RESTORE_SPELLING_DEPTH (constructor_depth);
12723 : }
12724 : else
12725 : /* Do the bookkeeping for an element that was
12726 : directly output as a constructor. */
12727 : {
12728 : /* For a record, keep track of end position of last field. */
12729 73 : if (DECL_SIZE (constructor_fields))
12730 30 : constructor_bit_index
12731 30 : = size_binop_loc (input_location, PLUS_EXPR,
12732 : bit_position (constructor_fields),
12733 30 : DECL_SIZE (constructor_fields));
12734 :
12735 : /* If the current field was the first one not yet written out,
12736 : it isn't now, so update. */
12737 73 : if (constructor_unfilled_fields == constructor_fields)
12738 : {
12739 64 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12740 : /* Skip any nameless bit fields. */
12741 64 : while (constructor_unfilled_fields != 0
12742 64 : && (DECL_UNNAMED_BIT_FIELD
12743 : (constructor_unfilled_fields)))
12744 0 : constructor_unfilled_fields =
12745 0 : DECL_CHAIN (constructor_unfilled_fields);
12746 : }
12747 : }
12748 :
12749 1036915 : constructor_fields = DECL_CHAIN (constructor_fields);
12750 : /* Skip any nameless bit fields at the beginning. */
12751 1036915 : while (constructor_fields != NULL_TREE
12752 1037016 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12753 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12754 : }
12755 9285940 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12756 : {
12757 31172 : tree fieldtype;
12758 31172 : enum tree_code fieldcode;
12759 :
12760 31172 : if (constructor_fields == NULL_TREE)
12761 : {
12762 3 : pedwarn_init (loc, 0,
12763 : "excess elements in union initializer");
12764 3 : break;
12765 : }
12766 :
12767 31169 : fieldtype = TREE_TYPE (constructor_fields);
12768 31169 : if (fieldtype != error_mark_node)
12769 31169 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12770 31169 : fieldcode = TREE_CODE (fieldtype);
12771 :
12772 : /* Warn that traditional C rejects initialization of unions.
12773 : We skip the warning if the value is zero. This is done
12774 : under the assumption that the zero initializer in user
12775 : code appears conditioned on e.g. __STDC__ to avoid
12776 : "missing initializer" warnings and relies on default
12777 : initialization to zero in the traditional C case.
12778 : We also skip the warning if the initializer is designated,
12779 : again on the assumption that this must be conditional on
12780 : __STDC__ anyway (and we've already complained about the
12781 : member-designator already). */
12782 33414 : if (!in_system_header_at (input_location) && !constructor_designated
12783 32652 : && !(value.value && (integer_zerop (value.value)
12784 1376 : || real_zerop (value.value))))
12785 1369 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12786 : "of unions");
12787 :
12788 : /* Error for non-static initialization of a flexible array member. */
12789 31169 : if (fieldcode == ARRAY_TYPE
12790 766 : && !require_constant_value
12791 31226 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12792 : {
12793 3 : error_init (loc, "non-static initialization of a flexible "
12794 : "array member");
12795 3 : break;
12796 : }
12797 :
12798 : /* Error for initialization of a flexible array member with
12799 : a string constant if the structure is in an array. E.g.:
12800 : union U { int x; char y[]; };
12801 : union U s[] = { { 1, "foo" } };
12802 : is invalid. */
12803 31166 : if (string_flag
12804 31166 : && fieldcode == ARRAY_TYPE
12805 7 : && constructor_depth > 1
12806 31170 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12807 : {
12808 3 : bool in_array_p = false;
12809 3 : for (struct constructor_stack *p = constructor_stack;
12810 3 : p && p->type; p = p->next)
12811 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12812 : {
12813 : in_array_p = true;
12814 : break;
12815 : }
12816 3 : if (in_array_p)
12817 : {
12818 3 : error_init (loc, "initialization of flexible array "
12819 : "member in a nested context");
12820 3 : break;
12821 : }
12822 : }
12823 :
12824 : /* Accept a string constant to initialize a subarray. */
12825 31163 : if (value.value != NULL_TREE
12826 31163 : && fieldcode == ARRAY_TYPE
12827 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12828 31918 : && string_flag)
12829 : value.value = orig_value;
12830 : /* Otherwise, if we have come to a subaggregate,
12831 : and we don't have an element of its type, push into it. */
12832 31209 : else if (value.value != NULL_TREE
12833 31159 : && initialize_elementwise_p (fieldtype, value.value))
12834 : {
12835 50 : push_init_level (loc, 1, braced_init_obstack);
12836 50 : continue;
12837 : }
12838 :
12839 31113 : value.value = maybe_split_raw_data (value.value, &raw_data);
12840 31113 : if (value.value)
12841 : {
12842 31113 : push_member_name (constructor_fields);
12843 31113 : output_init_element (loc, value.value, value.original_type,
12844 : strict_string, fieldtype,
12845 : constructor_fields, true, implicit,
12846 : braced_init_obstack);
12847 31113 : RESTORE_SPELLING_DEPTH (constructor_depth);
12848 : }
12849 : else
12850 : /* Do the bookkeeping for an element that was
12851 : directly output as a constructor. */
12852 : {
12853 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12854 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12855 : }
12856 :
12857 31113 : constructor_fields = NULL_TREE;
12858 : }
12859 9254768 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12860 : {
12861 2419415 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12862 2419415 : enum tree_code eltcode = TREE_CODE (elttype);
12863 :
12864 : /* Accept a string constant to initialize a subarray. */
12865 2419415 : if (value.value != NULL_TREE
12866 2419415 : && eltcode == ARRAY_TYPE
12867 7031 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12868 2424963 : && string_flag)
12869 : value.value = orig_value;
12870 : /* Otherwise, if we have come to a subaggregate,
12871 : and we don't have an element of its type, push into it. */
12872 3121245 : else if (value.value != NULL_TREE
12873 2418919 : && initialize_elementwise_p (elttype, value.value))
12874 : {
12875 702326 : push_init_level (loc, 1, braced_init_obstack);
12876 702326 : continue;
12877 : }
12878 :
12879 1717089 : if (constructor_max_index != NULL_TREE
12880 674322 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12881 674207 : || integer_all_onesp (constructor_max_index))
12882 : /* For VLA we got an error already. */
12883 1717204 : && !C_TYPE_VARIABLE_SIZE (constructor_type))
12884 : {
12885 113 : pedwarn_init (loc, 0,
12886 : "excess elements in array initializer");
12887 113 : break;
12888 : }
12889 :
12890 1716976 : if (value.value
12891 1716976 : && TREE_CODE (value.value) == RAW_DATA_CST
12892 192 : && RAW_DATA_LENGTH (value.value) > 1
12893 192 : && (TREE_CODE (elttype) == INTEGER_TYPE
12894 192 : || TREE_CODE (elttype) == BITINT_TYPE)
12895 192 : && TYPE_PRECISION (elttype) == CHAR_BIT
12896 1717168 : && (constructor_max_index == NULL_TREE
12897 54 : || tree_int_cst_lt (constructor_index,
12898 : constructor_max_index)))
12899 : {
12900 192 : unsigned int len = RAW_DATA_LENGTH (value.value);
12901 192 : if (constructor_max_index)
12902 : {
12903 54 : widest_int w = wi::to_widest (constructor_max_index);
12904 54 : w -= wi::to_widest (constructor_index);
12905 54 : w += 1;
12906 54 : if (w < len)
12907 3 : len = w.to_uhwi ();
12908 54 : }
12909 192 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12910 : {
12911 3 : raw_data = copy_node (value.value);
12912 3 : RAW_DATA_LENGTH (raw_data) -= len;
12913 3 : RAW_DATA_POINTER (raw_data) += len;
12914 3 : RAW_DATA_LENGTH (value.value) = len;
12915 : }
12916 192 : TREE_TYPE (value.value) = elttype;
12917 192 : push_array_bounds (tree_to_uhwi (constructor_index));
12918 192 : output_init_element (loc, value.value, value.original_type,
12919 : false, elttype, constructor_index, true,
12920 : implicit, braced_init_obstack);
12921 192 : RESTORE_SPELLING_DEPTH (constructor_depth);
12922 192 : constructor_index
12923 192 : = size_binop_loc (input_location, PLUS_EXPR,
12924 192 : constructor_index, bitsize_int (len));
12925 : }
12926 : else
12927 : {
12928 1716784 : value.value = maybe_split_raw_data (value.value, &raw_data);
12929 : /* Now output the actual element. */
12930 1716784 : if (value.value)
12931 : {
12932 1716784 : push_array_bounds (tree_to_uhwi (constructor_index));
12933 1716784 : output_init_element (loc, value.value, value.original_type,
12934 : strict_string, elttype,
12935 : constructor_index, true, implicit,
12936 : braced_init_obstack);
12937 1716784 : RESTORE_SPELLING_DEPTH (constructor_depth);
12938 : }
12939 :
12940 1716784 : constructor_index
12941 1716784 : = size_binop_loc (input_location, PLUS_EXPR,
12942 : constructor_index, bitsize_one_node);
12943 :
12944 1716784 : if (!value.value)
12945 : /* If we are doing the bookkeeping for an element that was
12946 : directly output as a constructor, we must update
12947 : constructor_unfilled_index. */
12948 0 : constructor_unfilled_index = constructor_index;
12949 : }
12950 : }
12951 6835353 : else if (gnu_vector_type_p (constructor_type))
12952 : {
12953 6834861 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12954 :
12955 : /* Do a basic check of initializer size. Note that vectors
12956 : may not always have a fixed size derived from their type. */
12957 6834861 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12958 6834861 : tree_to_poly_uint64 (constructor_index)))
12959 : {
12960 : /* Diagose VLA out-of-bounds as errors. */
12961 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
12962 2 : pedwarn_init (loc, 0,
12963 : "excess elements in vector initializer");
12964 : else
12965 : error_init (loc, "excess elements in vector initializer");
12966 :
12967 : break;
12968 : }
12969 :
12970 6834859 : value.value = maybe_split_raw_data (value.value, &raw_data);
12971 : /* Now output the actual element. */
12972 6834859 : if (value.value)
12973 : {
12974 6834859 : if (TREE_CODE (value.value) == VECTOR_CST)
12975 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
12976 6834859 : output_init_element (loc, value.value, value.original_type,
12977 : strict_string, elttype,
12978 : constructor_index, true, implicit,
12979 : braced_init_obstack);
12980 : }
12981 :
12982 6834859 : constructor_index
12983 6834859 : = size_binop_loc (input_location,
12984 : PLUS_EXPR, constructor_index, bitsize_one_node);
12985 :
12986 6834859 : if (!value.value)
12987 : /* If we are doing the bookkeeping for an element that was
12988 : directly output as a constructor, we must update
12989 : constructor_unfilled_index. */
12990 0 : constructor_unfilled_index = constructor_index;
12991 : }
12992 :
12993 : /* Handle the sole element allowed in a braced initializer
12994 : for a scalar variable. */
12995 492 : else if (constructor_type != error_mark_node
12996 492 : && constructor_fields == NULL_TREE)
12997 : {
12998 29 : pedwarn_init (loc, 0,
12999 : "excess elements in scalar initializer");
13000 29 : break;
13001 : }
13002 : else
13003 : {
13004 463 : value.value = maybe_split_raw_data (value.value, &raw_data);
13005 463 : if (value.value)
13006 463 : output_init_element (loc, value.value, value.original_type,
13007 : strict_string, constructor_type,
13008 : NULL_TREE, true, implicit,
13009 : braced_init_obstack);
13010 463 : constructor_fields = NULL_TREE;
13011 : }
13012 :
13013 : /* Handle range initializers either at this level or anywhere higher
13014 : in the designator stack. */
13015 9620326 : if (constructor_range_stack)
13016 : {
13017 11564 : struct constructor_range_stack *p, *range_stack;
13018 11564 : int finish = 0;
13019 :
13020 11564 : range_stack = constructor_range_stack;
13021 11564 : constructor_range_stack = 0;
13022 11564 : while (constructor_stack != range_stack->stack)
13023 : {
13024 0 : gcc_assert (constructor_stack->implicit);
13025 0 : process_init_element (loc,
13026 : pop_init_level (loc, 1,
13027 : braced_init_obstack,
13028 : last_init_list_comma),
13029 : true, braced_init_obstack);
13030 : }
13031 325 : for (p = range_stack;
13032 11889 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
13033 325 : p = p->prev)
13034 : {
13035 325 : gcc_assert (constructor_stack->implicit);
13036 325 : process_init_element (loc,
13037 : pop_init_level (loc, 1,
13038 : braced_init_obstack,
13039 : last_init_list_comma),
13040 : true, braced_init_obstack);
13041 : }
13042 :
13043 11564 : p->index = size_binop_loc (input_location,
13044 : PLUS_EXPR, p->index, bitsize_one_node);
13045 11564 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
13046 379 : finish = 1;
13047 :
13048 11889 : while (1)
13049 : {
13050 11889 : constructor_index = p->index;
13051 11889 : constructor_fields = p->fields;
13052 11889 : if (finish && p->range_end && p->index == p->range_start)
13053 : {
13054 8 : finish = 0;
13055 8 : p->prev = 0;
13056 : }
13057 11889 : p = p->next;
13058 11889 : if (!p)
13059 : break;
13060 325 : finish_implicit_inits (loc, braced_init_obstack);
13061 325 : push_init_level (loc, 2, braced_init_obstack);
13062 325 : p->stack = constructor_stack;
13063 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
13064 33 : p->index = p->range_start;
13065 : }
13066 :
13067 11564 : if (!finish)
13068 11193 : constructor_range_stack = range_stack;
13069 11564 : continue;
13070 11564 : }
13071 :
13072 : break;
13073 : }
13074 :
13075 9610238 : constructor_range_stack = 0;
13076 :
13077 9610238 : if (raw_data && RAW_DATA_LENGTH (raw_data))
13078 : {
13079 212 : gcc_assert (!string_flag && !was_designated);
13080 212 : value.value = raw_data;
13081 212 : raw_data = NULL_TREE;
13082 212 : goto retry;
13083 : }
13084 : }
13085 :
13086 : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
13087 : (guaranteed to be 'volatile' or null) and ARGS (represented using
13088 : an ASM_EXPR node). */
13089 : tree
13090 204995 : build_asm_stmt (bool is_volatile, tree args)
13091 : {
13092 204995 : if (is_volatile)
13093 165199 : ASM_VOLATILE_P (args) = 1;
13094 204995 : return add_stmt (args);
13095 : }
13096 :
13097 : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
13098 : some INPUTS, and some CLOBBERS. The latter three may be NULL.
13099 : SIMPLE indicates whether there was anything at all after the
13100 : string in the asm expression -- asm("blah") and asm("blah" : )
13101 : are subtly different. We use a ASM_EXPR node to represent this.
13102 : LOC is the location of the asm, and IS_INLINE says whether this
13103 : is asm inline. */
13104 : tree
13105 205047 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
13106 : tree clobbers, tree labels, bool simple, bool is_inline)
13107 : {
13108 205047 : tree tail;
13109 205047 : tree args;
13110 205047 : int i;
13111 205047 : const char *constraint;
13112 205047 : const char **oconstraints;
13113 205047 : bool allows_mem, allows_reg, is_inout;
13114 205047 : int ninputs, noutputs;
13115 :
13116 205047 : ninputs = list_length (inputs);
13117 205047 : noutputs = list_length (outputs);
13118 205047 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
13119 :
13120 205047 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
13121 :
13122 : /* Remove output conversions that change the type but not the mode. */
13123 554339 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
13124 : {
13125 349292 : tree output = TREE_VALUE (tail);
13126 :
13127 349292 : output = c_fully_fold (output, false, NULL, true);
13128 :
13129 : /* ??? Really, this should not be here. Users should be using a
13130 : proper lvalue, dammit. But there's a long history of using casts
13131 : in the output operands. In cases like longlong.h, this becomes a
13132 : primitive form of typechecking -- if the cast can be removed, then
13133 : the output operand had a type of the proper width; otherwise we'll
13134 : get an error. Gross, but ... */
13135 349292 : STRIP_NOPS (output);
13136 :
13137 349292 : if (!lvalue_or_else (loc, output, lv_asm))
13138 4 : output = error_mark_node;
13139 :
13140 349292 : if (output != error_mark_node
13141 349292 : && (TREE_READONLY (output)
13142 349285 : || TYPE_READONLY (TREE_TYPE (output))
13143 349285 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
13144 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
13145 2 : readonly_error (loc, output, lv_asm);
13146 :
13147 349292 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13148 349292 : oconstraints[i] = constraint;
13149 :
13150 349292 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13151 : &allows_mem, &allows_reg, &is_inout,
13152 : nullptr))
13153 : {
13154 : /* If the operand is going to end up in memory,
13155 : mark it addressable. */
13156 349284 : if (!allows_reg && !c_mark_addressable (output))
13157 3 : output = error_mark_node;
13158 1419 : if (!(!allows_reg && allows_mem)
13159 347865 : && output != error_mark_node
13160 697147 : && VOID_TYPE_P (TREE_TYPE (output)))
13161 : {
13162 4 : error_at (loc, "invalid use of void expression");
13163 4 : output = error_mark_node;
13164 : }
13165 349284 : if (allows_reg && current_function_decl == NULL_TREE)
13166 : {
13167 1 : error_at (loc, "constraint allows registers outside of "
13168 : "a function");
13169 1 : output = error_mark_node;
13170 : }
13171 : }
13172 : else
13173 8 : output = error_mark_node;
13174 :
13175 349292 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13176 : {
13177 7 : if (TREE_SIDE_EFFECTS (output))
13178 : {
13179 0 : error_at (loc, "side-effects in output operand outside "
13180 : "of a function");
13181 0 : output = error_mark_node;
13182 : }
13183 : else
13184 : {
13185 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13186 7 : if (addr == error_mark_node)
13187 : output = error_mark_node;
13188 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13189 : {
13190 1 : error_at (loc, "output operand outside of a function is not "
13191 : "constant");
13192 1 : output = error_mark_node;
13193 : }
13194 : }
13195 : }
13196 349285 : else if (output != error_mark_node && strstr (constraint, "-"))
13197 : {
13198 1 : error_at (loc, "%<-%> modifier used inside of a function");
13199 1 : output = error_mark_node;
13200 : }
13201 :
13202 349292 : TREE_VALUE (tail) = output;
13203 : }
13204 :
13205 569719 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13206 : {
13207 364672 : tree input;
13208 :
13209 364672 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13210 364672 : input = TREE_VALUE (tail);
13211 :
13212 364672 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13213 : oconstraints, &allows_mem, &allows_reg,
13214 : nullptr))
13215 : {
13216 : /* If the operand is going to end up in memory,
13217 : mark it addressable. */
13218 364664 : if (!allows_reg && allows_mem)
13219 : {
13220 1364 : input = c_fully_fold (input, false, NULL, true);
13221 :
13222 : /* Strip the nops as we allow this case. FIXME, this really
13223 : should be rejected or made deprecated. */
13224 1364 : STRIP_NOPS (input);
13225 1364 : if (!c_mark_addressable (input))
13226 2 : input = error_mark_node;
13227 : }
13228 : else
13229 : {
13230 363300 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13231 : true, false),
13232 : false, NULL);
13233 :
13234 363300 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13235 : {
13236 4 : error_at (loc, "invalid use of void expression");
13237 4 : input = error_mark_node;
13238 : }
13239 : }
13240 364664 : if (allows_reg && current_function_decl == NULL_TREE)
13241 : {
13242 1 : error_at (loc, "constraint allows registers outside of "
13243 : "a function");
13244 1 : input = error_mark_node;
13245 : }
13246 364664 : if (constraint[0] == ':' && input != error_mark_node)
13247 : {
13248 35 : tree t = input;
13249 35 : STRIP_NOPS (t);
13250 35 : if (TREE_CODE (t) != ADDR_EXPR
13251 35 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13252 23 : || (VAR_P (TREE_OPERAND (t, 0))
13253 21 : && is_global_var (TREE_OPERAND (t, 0)))))
13254 : {
13255 5 : error_at (loc, "%<:%> constraint operand is not address "
13256 : "of a function or non-automatic variable");
13257 5 : input = error_mark_node;
13258 : }
13259 30 : else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
13260 10 : suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
13261 : }
13262 : }
13263 : else
13264 8 : input = error_mark_node;
13265 :
13266 364672 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13267 : {
13268 58 : if (TREE_SIDE_EFFECTS (input))
13269 : {
13270 2 : error_at (loc, "side-effects in input operand outside "
13271 : "of a function");
13272 2 : input = error_mark_node;
13273 : }
13274 : else
13275 : {
13276 56 : tree tem = input;
13277 56 : if (allows_mem && lvalue_p (input))
13278 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13279 56 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13280 : {
13281 2 : error_at (loc, "input operand outside of a function is not "
13282 : "constant");
13283 2 : input = error_mark_node;
13284 : }
13285 : }
13286 : }
13287 364614 : else if (input != error_mark_node && strstr (constraint, "-"))
13288 : {
13289 3 : error_at (loc, "%<-%> modifier used inside of a function");
13290 3 : input = error_mark_node;
13291 : }
13292 :
13293 364672 : TREE_VALUE (tail) = input;
13294 : }
13295 :
13296 205047 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13297 :
13298 : /* asm statements without outputs, including simple ones, are treated
13299 : as volatile. */
13300 205047 : ASM_BASIC_P (args) = simple;
13301 205047 : ASM_VOLATILE_P (args) = (noutputs == 0);
13302 205047 : ASM_INLINE_P (args) = is_inline;
13303 :
13304 205047 : return args;
13305 : }
13306 :
13307 : /* Generate a goto statement to LABEL. LOC is the location of the
13308 : GOTO. */
13309 :
13310 : tree
13311 82793 : c_finish_goto_label (location_t loc, tree label)
13312 : {
13313 82793 : tree decl = lookup_label_for_goto (loc, label);
13314 82793 : if (!decl)
13315 : return NULL_TREE;
13316 82793 : TREE_USED (decl) = 1;
13317 82793 : mark_decl_used (decl, false);
13318 82793 : {
13319 82793 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13320 82793 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13321 82793 : SET_EXPR_LOCATION (t, loc);
13322 82793 : return add_stmt (t);
13323 : }
13324 : }
13325 :
13326 : /* Generate a computed goto statement to EXPR. LOC is the location of
13327 : the GOTO. */
13328 :
13329 : tree
13330 942 : c_finish_goto_ptr (location_t loc, c_expr val)
13331 : {
13332 942 : tree expr = val.value;
13333 942 : tree t;
13334 942 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13335 942 : if (expr != error_mark_node
13336 941 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13337 947 : && !null_pointer_constant_p (expr))
13338 : {
13339 2 : error_at (val.get_location (),
13340 : "computed goto must be pointer type");
13341 2 : expr = build_zero_cst (ptr_type_node);
13342 : }
13343 942 : expr = c_fully_fold (expr, false, NULL);
13344 942 : expr = convert (ptr_type_node, expr);
13345 942 : t = build1 (GOTO_EXPR, void_type_node, expr);
13346 942 : SET_EXPR_LOCATION (t, loc);
13347 942 : return add_stmt (t);
13348 : }
13349 :
13350 : /* Generate a C `return' statement. RETVAL is the expression for what
13351 : to return, or a null pointer for `return;' with no value. LOC is
13352 : the location of the return statement, or the location of the expression,
13353 : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13354 : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13355 : attribute. */
13356 :
13357 : tree
13358 34651265 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13359 : {
13360 34651265 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13361 34651265 : bool no_warning = false;
13362 34651265 : bool npc = false;
13363 :
13364 : /* Use the expansion point to handle cases such as returning NULL
13365 : in a function returning void. */
13366 34651265 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13367 :
13368 34651265 : if (TREE_THIS_VOLATILE (current_function_decl))
13369 23 : warning_at (xloc, 0,
13370 : "function declared %<noreturn%> has a %<return%> statement");
13371 :
13372 34651265 : set_musttail_on_return (retval, xloc, musttail_p);
13373 :
13374 34651265 : if (retval)
13375 : {
13376 34629954 : tree semantic_type = NULL_TREE;
13377 34629954 : npc = null_pointer_constant_p (retval);
13378 34629954 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13379 : {
13380 92 : semantic_type = TREE_TYPE (retval);
13381 92 : retval = TREE_OPERAND (retval, 0);
13382 : }
13383 34629954 : retval = c_fully_fold (retval, false, NULL);
13384 34629954 : if (semantic_type
13385 34629954 : && valtype != NULL_TREE
13386 92 : && TREE_CODE (valtype) != VOID_TYPE)
13387 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13388 : }
13389 :
13390 34629954 : if (!retval)
13391 : {
13392 21311 : current_function_returns_null = 1;
13393 21311 : if ((warn_return_type >= 0 || flag_isoc99)
13394 21186 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13395 : {
13396 46 : no_warning = true;
13397 47 : if (emit_diagnostic (flag_isoc99
13398 : ? diagnostics::kind::permerror
13399 : : diagnostics::kind::warning,
13400 46 : loc, OPT_Wreturn_mismatch,
13401 : "%<return%> with no value,"
13402 : " in function returning non-void"))
13403 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13404 : "declared here");
13405 : }
13406 : }
13407 34629954 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13408 : {
13409 321 : current_function_returns_null = 1;
13410 321 : bool warned_here;
13411 321 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13412 64 : warned_here = permerror_opt
13413 64 : (xloc, OPT_Wreturn_mismatch,
13414 : "%<return%> with a value, in function returning void");
13415 : else
13416 257 : warned_here = pedwarn
13417 257 : (xloc, OPT_Wpedantic, "ISO C forbids "
13418 : "%<return%> with expression, in function returning void");
13419 321 : if (warned_here)
13420 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13421 : "declared here");
13422 : }
13423 : else
13424 : {
13425 34629633 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13426 : retval, origtype, ic_return,
13427 : npc, NULL_TREE, NULL_TREE, 0);
13428 34629633 : tree res = DECL_RESULT (current_function_decl);
13429 34629633 : tree inner;
13430 34629633 : bool save;
13431 :
13432 34629633 : current_function_returns_value = 1;
13433 34629633 : if (t == error_mark_node)
13434 : {
13435 : /* Suppress -Wreturn-type for this function. */
13436 299 : if (warn_return_type)
13437 298 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13438 299 : return NULL_TREE;
13439 : }
13440 :
13441 34629334 : save = in_late_binary_op;
13442 69250519 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13443 34620380 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13444 69248434 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13445 333378 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13446 333378 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13447 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13448 10234 : in_late_binary_op = true;
13449 34629334 : inner = t = convert (TREE_TYPE (res), t);
13450 34629334 : in_late_binary_op = save;
13451 :
13452 : /* Strip any conversions, additions, and subtractions, and see if
13453 : we are returning the address of a local variable. Warn if so. */
13454 37907145 : while (1)
13455 : {
13456 37907145 : switch (TREE_CODE (inner))
13457 : {
13458 3276385 : CASE_CONVERT:
13459 3276385 : case NON_LVALUE_EXPR:
13460 3276385 : case PLUS_EXPR:
13461 3276385 : case POINTER_PLUS_EXPR:
13462 3276385 : inner = TREE_OPERAND (inner, 0);
13463 3276385 : continue;
13464 :
13465 1431 : case MINUS_EXPR:
13466 : /* If the second operand of the MINUS_EXPR has a pointer
13467 : type (or is converted from it), this may be valid, so
13468 : don't give a warning. */
13469 1431 : {
13470 1431 : tree op1 = TREE_OPERAND (inner, 1);
13471 :
13472 3111 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13473 3365 : && (CONVERT_EXPR_P (op1)
13474 1426 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13475 254 : op1 = TREE_OPERAND (op1, 0);
13476 :
13477 1431 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13478 : break;
13479 :
13480 1426 : inner = TREE_OPERAND (inner, 0);
13481 1426 : continue;
13482 1426 : }
13483 :
13484 2932 : case ADDR_EXPR:
13485 2932 : inner = TREE_OPERAND (inner, 0);
13486 :
13487 2932 : while (REFERENCE_CLASS_P (inner)
13488 3818 : && !INDIRECT_REF_P (inner))
13489 886 : inner = TREE_OPERAND (inner, 0);
13490 :
13491 2932 : if (DECL_P (inner)
13492 1611 : && !DECL_EXTERNAL (inner)
13493 978 : && DECL_CONTEXT (inner) == current_function_decl
13494 3154 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13495 : {
13496 182 : if (TREE_CODE (inner) == LABEL_DECL)
13497 4 : warning_at (loc, OPT_Wreturn_local_addr,
13498 : "function returns address of label");
13499 178 : else if (TREE_CODE (inner) == FUNCTION_DECL
13500 178 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13501 9 : || !DECL_INITIAL (inner)))
13502 14 : warning_at (loc, OPT_Wreturn_local_addr,
13503 : "function returns address of nested function "
13504 : "referencing local context");
13505 164 : else if (TREE_CODE (inner) != FUNCTION_DECL
13506 156 : && !TREE_STATIC (inner))
13507 : {
13508 9 : warning_at (loc, OPT_Wreturn_local_addr,
13509 : "function returns address of local variable");
13510 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13511 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13512 : }
13513 : }
13514 : break;
13515 :
13516 : default:
13517 : break;
13518 3276385 : }
13519 :
13520 34629334 : break;
13521 : }
13522 :
13523 34629334 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13524 34629334 : SET_EXPR_LOCATION (retval, loc);
13525 :
13526 34629334 : if (warn_sequence_point)
13527 2964515 : verify_sequence_points (retval);
13528 : }
13529 :
13530 34650966 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13531 34650966 : if (no_warning)
13532 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13533 34650966 : return add_stmt (ret_stmt);
13534 : }
13535 :
13536 : struct c_switch {
13537 : /* The SWITCH_STMT being built. */
13538 : tree switch_stmt;
13539 :
13540 : /* The original type of the testing expression, i.e. before the
13541 : default conversion is applied. */
13542 : tree orig_type;
13543 :
13544 : /* A splay-tree mapping the low element of a case range to the high
13545 : element, or NULL_TREE if there is no high element. Used to
13546 : determine whether or not a new case label duplicates an old case
13547 : label. We need a tree, rather than simply a hash table, because
13548 : of the GNU case range extension. */
13549 : splay_tree cases;
13550 :
13551 : /* The bindings at the point of the switch. This is used for
13552 : warnings crossing decls when branching to a case label. */
13553 : struct c_spot_bindings *bindings;
13554 :
13555 : /* Whether the switch includes any break statements. */
13556 : bool break_stmt_seen_p;
13557 :
13558 : /* The next node on the stack. */
13559 : struct c_switch *next;
13560 :
13561 : /* Remember whether the controlling expression had boolean type
13562 : before integer promotions for the sake of -Wswitch-bool. */
13563 : bool bool_cond_p;
13564 : };
13565 :
13566 : /* A stack of the currently active switch statements. The innermost
13567 : switch statement is on the top of the stack. There is no need to
13568 : mark the stack for garbage collection because it is only active
13569 : during the processing of the body of a function, and we never
13570 : collect at that point. */
13571 :
13572 : struct c_switch *c_switch_stack;
13573 :
13574 : /* Start a C switch statement, testing expression EXP. Return the new
13575 : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13576 : SWITCH_COND_LOC is the location of the switch's condition.
13577 : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13578 :
13579 : tree
13580 37324 : c_start_switch (location_t switch_loc,
13581 : location_t switch_cond_loc,
13582 : tree exp, bool explicit_cast_p, tree switch_name)
13583 : {
13584 37324 : tree orig_type = error_mark_node;
13585 37324 : bool bool_cond_p = false;
13586 37324 : struct c_switch *cs;
13587 :
13588 37324 : if (exp != error_mark_node)
13589 : {
13590 37281 : orig_type = TREE_TYPE (exp);
13591 :
13592 37281 : if (!INTEGRAL_TYPE_P (orig_type))
13593 : {
13594 12 : if (orig_type != error_mark_node)
13595 : {
13596 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13597 12 : orig_type = error_mark_node;
13598 : }
13599 12 : exp = integer_zero_node;
13600 : }
13601 : else
13602 : {
13603 37269 : tree type = TYPE_MAIN_VARIANT (orig_type);
13604 37269 : tree e = exp;
13605 :
13606 : /* Warn if the condition has boolean value. */
13607 37272 : while (TREE_CODE (e) == COMPOUND_EXPR)
13608 3 : e = TREE_OPERAND (e, 1);
13609 :
13610 37244 : if ((C_BOOLEAN_TYPE_P (type)
13611 37244 : || truth_value_p (TREE_CODE (e)))
13612 : /* Explicit cast to int suppresses this warning. */
13613 37291 : && !(TREE_CODE (type) == INTEGER_TYPE
13614 : && explicit_cast_p))
13615 : bool_cond_p = true;
13616 :
13617 37269 : if (!in_system_header_at (input_location)
13618 37269 : && (type == long_integer_type_node
13619 11961 : || type == long_unsigned_type_node))
13620 282 : warning_at (switch_cond_loc,
13621 282 : OPT_Wtraditional, "%<long%> switch expression not "
13622 : "converted to %<int%> in ISO C");
13623 :
13624 37269 : exp = c_fully_fold (exp, false, NULL);
13625 37269 : exp = default_conversion (exp);
13626 :
13627 37269 : if (warn_sequence_point)
13628 6017 : verify_sequence_points (exp);
13629 : }
13630 : }
13631 :
13632 : /* Add this new SWITCH_STMT to the stack. */
13633 37324 : cs = XNEW (struct c_switch);
13634 37324 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13635 : NULL_TREE, orig_type, NULL_TREE, switch_name);
13636 37324 : cs->orig_type = orig_type;
13637 37324 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13638 37324 : cs->bindings = c_get_switch_bindings ();
13639 37324 : cs->break_stmt_seen_p = false;
13640 37324 : cs->bool_cond_p = bool_cond_p;
13641 37324 : cs->next = c_switch_stack;
13642 37324 : c_switch_stack = cs;
13643 :
13644 37324 : return add_stmt (cs->switch_stmt);
13645 : }
13646 :
13647 : /* Process a case label at location LOC, with attributes ATTRS. */
13648 :
13649 : tree
13650 1030366 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13651 : {
13652 1030366 : tree label = NULL_TREE;
13653 :
13654 1030366 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13655 : {
13656 87 : low_value = c_fully_fold (low_value, false, NULL);
13657 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13658 9 : pedwarn (loc, OPT_Wpedantic,
13659 : "case label is not an integer constant expression");
13660 : }
13661 :
13662 1030366 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13663 : {
13664 0 : high_value = c_fully_fold (high_value, false, NULL);
13665 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13666 0 : pedwarn (input_location, OPT_Wpedantic,
13667 : "case label is not an integer constant expression");
13668 : }
13669 :
13670 1030366 : if (c_switch_stack == NULL)
13671 : {
13672 3 : if (low_value)
13673 2 : error_at (loc, "case label not within a switch statement");
13674 : else
13675 1 : error_at (loc, "%<default%> label not within a switch statement");
13676 3 : return NULL_TREE;
13677 : }
13678 :
13679 1030363 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13680 1030363 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13681 : loc))
13682 : return NULL_TREE;
13683 :
13684 1030351 : label = c_add_case_label (loc, c_switch_stack->cases,
13685 1030351 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13686 : low_value, high_value, attrs);
13687 1030351 : if (label == error_mark_node)
13688 144 : label = NULL_TREE;
13689 : return label;
13690 : }
13691 :
13692 : /* Finish the switch statement. TYPE is the original type of the
13693 : controlling expression of the switch, or NULL_TREE. */
13694 :
13695 : void
13696 37324 : c_finish_switch (tree body, tree type)
13697 : {
13698 37324 : struct c_switch *cs = c_switch_stack;
13699 37324 : location_t switch_location;
13700 :
13701 37324 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13702 :
13703 : /* Emit warnings as needed. */
13704 37324 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13705 43514 : c_do_switch_warnings (cs->cases, switch_location,
13706 6190 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13707 37324 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13708 37324 : if (c_switch_covers_all_cases_p (cs->cases,
13709 37324 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13710 33413 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13711 37324 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13712 :
13713 : /* Pop the stack. */
13714 37324 : c_switch_stack = cs->next;
13715 37324 : splay_tree_delete (cs->cases);
13716 37324 : c_release_switch_bindings (cs->bindings);
13717 37324 : XDELETE (cs);
13718 37324 : }
13719 :
13720 : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13721 : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13722 : may be null. */
13723 :
13724 : void
13725 1269571 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13726 : tree else_block)
13727 : {
13728 1269571 : tree stmt;
13729 :
13730 1269571 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13731 1269571 : SET_EXPR_LOCATION (stmt, if_locus);
13732 1269571 : add_stmt (stmt);
13733 1269571 : }
13734 :
13735 : tree
13736 187405 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13737 : {
13738 : /* In switch statements break is sometimes stylistically used after
13739 : a return statement. This can lead to spurious warnings about
13740 : control reaching the end of a non-void function when it is
13741 : inlined. Note that we are calling block_may_fallthru with
13742 : language specific tree nodes; this works because
13743 : block_may_fallthru returns true when given something it does not
13744 : understand. */
13745 187405 : bool skip = !block_may_fallthru (cur_stmt_list);
13746 :
13747 187405 : if (is_break)
13748 174227 : switch (in_statement & ~IN_NAMED_STMT)
13749 : {
13750 9 : case 0:
13751 9 : error_at (loc, "break statement not within loop or switch");
13752 9 : return NULL_TREE;
13753 4 : case IN_OMP_BLOCK:
13754 4 : error_at (loc, "invalid exit from OpenMP structured block");
13755 4 : return NULL_TREE;
13756 12 : case IN_OMP_FOR:
13757 12 : error_at (loc, "break statement used with OpenMP for loop");
13758 12 : return NULL_TREE;
13759 : case IN_ITERATION_STMT:
13760 : case IN_OBJC_FOREACH:
13761 : break;
13762 158400 : default:
13763 158400 : gcc_assert (in_statement & IN_SWITCH_STMT);
13764 158400 : c_switch_stack->break_stmt_seen_p = true;
13765 158400 : break;
13766 : }
13767 : else
13768 13178 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13769 : {
13770 7 : case 0:
13771 7 : error_at (loc, "continue statement not within a loop");
13772 7 : return NULL_TREE;
13773 4 : case IN_OMP_BLOCK:
13774 4 : error_at (loc, "invalid exit from OpenMP structured block");
13775 4 : return NULL_TREE;
13776 : case IN_ITERATION_STMT:
13777 : case IN_OMP_FOR:
13778 : case IN_OBJC_FOREACH:
13779 : break;
13780 0 : default:
13781 0 : gcc_unreachable ();
13782 : }
13783 :
13784 187369 : if (skip)
13785 : return NULL_TREE;
13786 186923 : else if ((in_statement & IN_OBJC_FOREACH)
13787 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13788 0 : && name == NULL_TREE)
13789 : {
13790 : /* The foreach expander produces low-level code using gotos instead
13791 : of a structured loop construct. */
13792 0 : gcc_assert (label);
13793 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13794 : }
13795 187020 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13796 : {
13797 0 : label = DECL_CHAIN (name);
13798 0 : if (!is_break)
13799 0 : label = DECL_CHAIN (label);
13800 : /* Foreach expander from some outer level. */
13801 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13802 : }
13803 200090 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13804 186923 : name));
13805 : }
13806 :
13807 : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13808 :
13809 : static void
13810 6362796 : emit_side_effect_warnings (location_t loc, tree expr)
13811 : {
13812 6362796 : maybe_warn_nodiscard (loc, expr);
13813 6362796 : if (!warn_unused_value)
13814 : return;
13815 1294398 : if (expr == error_mark_node)
13816 : ;
13817 1294323 : else if (!TREE_SIDE_EFFECTS (expr))
13818 : {
13819 6446 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13820 6446 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13821 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13822 : }
13823 1287877 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13824 : {
13825 : tree r = expr;
13826 : location_t cloc = loc;
13827 11989 : while (TREE_CODE (r) == COMPOUND_EXPR)
13828 : {
13829 6010 : if (EXPR_HAS_LOCATION (r))
13830 5480 : cloc = EXPR_LOCATION (r);
13831 6010 : r = TREE_OPERAND (r, 1);
13832 : }
13833 5979 : if (!TREE_SIDE_EFFECTS (r)
13834 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13835 24 : && !CONVERT_EXPR_P (r)
13836 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13837 5991 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13838 8 : warning_at (cloc, OPT_Wunused_value,
13839 : "right-hand operand of comma expression has no effect");
13840 : }
13841 : else
13842 1281898 : warn_if_unused_value (expr, loc);
13843 : }
13844 :
13845 : /* Process an expression as if it were a complete statement. Emit
13846 : diagnostics, but do not call ADD_STMT. LOC is the location of the
13847 : statement. */
13848 :
13849 : tree
13850 6239392 : c_process_expr_stmt (location_t loc, tree expr)
13851 : {
13852 6239392 : tree exprv;
13853 :
13854 6239392 : if (!expr)
13855 : return NULL_TREE;
13856 :
13857 6235060 : expr = c_fully_fold (expr, false, NULL);
13858 :
13859 6235060 : if (warn_sequence_point)
13860 1290190 : verify_sequence_points (expr);
13861 :
13862 6235060 : if (TREE_TYPE (expr) != error_mark_node
13863 6232483 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13864 6235060 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13865 0 : error_at (loc, "expression statement has incomplete type");
13866 :
13867 : /* If we're not processing a statement expression, warn about unused values.
13868 : Warnings for statement expressions will be emitted later, once we figure
13869 : out which is the result. */
13870 6235060 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13871 6235060 : && (warn_unused_value || warn_unused_result))
13872 6149105 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13873 :
13874 : exprv = expr;
13875 6273909 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13876 38850 : exprv = TREE_OPERAND (exprv, 1);
13877 6246507 : while (CONVERT_EXPR_P (exprv))
13878 11448 : exprv = TREE_OPERAND (exprv, 0);
13879 6235059 : if (DECL_P (exprv)
13880 6235644 : || handled_component_p (exprv)
13881 12450317 : || TREE_CODE (exprv) == ADDR_EXPR)
13882 20386 : mark_exp_read (exprv);
13883 :
13884 : /* If the expression is not of a type to which we cannot assign a line
13885 : number, wrap the thing in a no-op NOP_EXPR. */
13886 6235059 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13887 : {
13888 14871 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13889 14871 : SET_EXPR_LOCATION (expr, loc);
13890 : }
13891 :
13892 : return expr;
13893 : }
13894 :
13895 : /* Emit an expression as a statement. LOC is the location of the
13896 : expression. */
13897 :
13898 : tree
13899 5938893 : c_finish_expr_stmt (location_t loc, tree expr)
13900 : {
13901 5938893 : if (expr)
13902 5926331 : return add_stmt (c_process_expr_stmt (loc, expr));
13903 : else
13904 : return NULL;
13905 : }
13906 :
13907 : /* Do the opposite and emit a statement as an expression. To begin,
13908 : create a new binding level and return it. */
13909 :
13910 : tree
13911 34875 : c_begin_stmt_expr (void)
13912 : {
13913 34875 : tree ret;
13914 :
13915 : /* We must force a BLOCK for this level so that, if it is not expanded
13916 : later, there is a way to turn off the entire subtree of blocks that
13917 : are contained in it. */
13918 34875 : keep_next_level ();
13919 34875 : ret = c_begin_compound_stmt (true);
13920 :
13921 34875 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13922 : ? NULL
13923 : : c_switch_stack->bindings);
13924 :
13925 : /* Mark the current statement list as belonging to a statement list. */
13926 34875 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13927 :
13928 34875 : return ret;
13929 : }
13930 :
13931 : /* LOC is the location of the compound statement to which this body
13932 : belongs. */
13933 :
13934 : tree
13935 34875 : c_finish_stmt_expr (location_t loc, tree body)
13936 : {
13937 34875 : tree last, type, tmp, val;
13938 34875 : tree *last_p;
13939 :
13940 34875 : body = c_end_compound_stmt (loc, body, true);
13941 :
13942 34875 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13943 : ? NULL
13944 : : c_switch_stack->bindings);
13945 :
13946 : /* Locate the last statement in BODY. See c_end_compound_stmt
13947 : about always returning a BIND_EXPR. */
13948 34875 : last_p = &BIND_EXPR_BODY (body);
13949 34875 : last = BIND_EXPR_BODY (body);
13950 :
13951 34875 : continue_searching:
13952 34875 : if (TREE_CODE (last) == STATEMENT_LIST)
13953 : {
13954 26061 : tree_stmt_iterator l = tsi_last (last);
13955 :
13956 26067 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13957 6 : tsi_prev (&l);
13958 :
13959 : /* This can happen with degenerate cases like ({ }). No value. */
13960 26061 : if (tsi_end_p (l))
13961 389 : return body;
13962 :
13963 : /* If we're supposed to generate side effects warnings, process
13964 : all of the statements except the last. */
13965 25672 : if (warn_unused_value || warn_unused_result)
13966 : {
13967 25672 : for (tree_stmt_iterator i = tsi_start (last);
13968 275711 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
13969 : {
13970 250039 : location_t tloc;
13971 250039 : tree t = tsi_stmt (i);
13972 :
13973 250039 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
13974 250039 : emit_side_effect_warnings (tloc, t);
13975 : }
13976 : }
13977 25672 : last_p = tsi_stmt_ptr (l);
13978 25672 : last = *last_p;
13979 : }
13980 :
13981 : /* If the end of the list is exception related, then the list was split
13982 : by a call to push_cleanup. Continue searching. */
13983 34486 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
13984 34486 : || TREE_CODE (last) == TRY_CATCH_EXPR)
13985 : {
13986 0 : last_p = &TREE_OPERAND (last, 0);
13987 0 : last = *last_p;
13988 0 : goto continue_searching;
13989 : }
13990 :
13991 34486 : if (last == error_mark_node)
13992 : return last;
13993 :
13994 : /* In the case that the BIND_EXPR is not necessary, return the
13995 : expression out from inside it. */
13996 34477 : if ((last == BIND_EXPR_BODY (body)
13997 : /* Skip nested debug stmts. */
13998 25671 : || last == expr_first (BIND_EXPR_BODY (body)))
13999 45484 : && BIND_EXPR_VARS (body) == NULL)
14000 : {
14001 : /* Even if this looks constant, do not allow it in a constant
14002 : expression. */
14003 10995 : last = c_wrap_maybe_const (last, true);
14004 : /* Do not warn if the return value of a statement expression is
14005 : unused. */
14006 10995 : suppress_warning (last, OPT_Wunused);
14007 10995 : return last;
14008 : }
14009 :
14010 : /* Extract the type of said expression. */
14011 23482 : type = TREE_TYPE (last);
14012 :
14013 : /* If we're not returning a value at all, then the BIND_EXPR that
14014 : we already have is a fine expression to return. */
14015 23482 : if (!type || VOID_TYPE_P (type))
14016 : return body;
14017 :
14018 : /* Now that we've located the expression containing the value, it seems
14019 : silly to make voidify_wrapper_expr repeat the process. Create a
14020 : temporary of the appropriate type and stick it in a TARGET_EXPR. */
14021 18158 : tmp = create_tmp_var_raw (type);
14022 :
14023 : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
14024 : tree_expr_nonnegative_p giving up immediately. */
14025 18158 : val = last;
14026 18158 : if (TREE_CODE (val) == NOP_EXPR
14027 18158 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
14028 3773 : val = TREE_OPERAND (val, 0);
14029 :
14030 18158 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
14031 18158 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
14032 :
14033 18158 : {
14034 18158 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
14035 18158 : SET_EXPR_LOCATION (t, loc);
14036 18158 : return t;
14037 : }
14038 : }
14039 :
14040 : /* Begin and end compound statements. This is as simple as pushing
14041 : and popping new statement lists from the tree. */
14042 :
14043 : tree
14044 41168958 : c_begin_compound_stmt (bool do_scope)
14045 : {
14046 41168958 : tree stmt = push_stmt_list ();
14047 41168958 : if (do_scope)
14048 41072904 : push_scope ();
14049 41168958 : return stmt;
14050 : }
14051 :
14052 : /* End a compound statement. STMT is the statement. LOC is the
14053 : location of the compound statement-- this is usually the location
14054 : of the opening brace. */
14055 :
14056 : tree
14057 41168957 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
14058 : {
14059 41168957 : tree block = NULL;
14060 :
14061 41168957 : if (do_scope)
14062 : {
14063 41072903 : if (c_dialect_objc ())
14064 0 : objc_clear_super_receiver ();
14065 41072903 : block = pop_scope ();
14066 : }
14067 :
14068 41168957 : stmt = pop_stmt_list (stmt);
14069 41168957 : stmt = c_build_bind_expr (loc, block, stmt);
14070 :
14071 : /* If this compound statement is nested immediately inside a statement
14072 : expression, then force a BIND_EXPR to be created. Otherwise we'll
14073 : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
14074 : STATEMENT_LISTs merge, and thus we can lose track of what statement
14075 : was really last. */
14076 82337914 : if (building_stmt_list_p ()
14077 41168872 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
14078 41291618 : && TREE_CODE (stmt) != BIND_EXPR)
14079 : {
14080 120918 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
14081 120918 : TREE_SIDE_EFFECTS (stmt) = 1;
14082 120918 : SET_EXPR_LOCATION (stmt, loc);
14083 : }
14084 :
14085 41168957 : return stmt;
14086 : }
14087 :
14088 : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
14089 : when the current scope is exited. EH_ONLY is true when this is not
14090 : meant to apply to normal control flow transfer. */
14091 :
14092 : void
14093 131 : push_cleanup (tree decl, tree cleanup, bool eh_only)
14094 : {
14095 131 : enum tree_code code;
14096 131 : tree stmt, list;
14097 131 : bool stmt_expr;
14098 :
14099 131 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
14100 131 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
14101 131 : add_stmt (stmt);
14102 131 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
14103 131 : list = push_stmt_list ();
14104 131 : TREE_OPERAND (stmt, 0) = list;
14105 131 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
14106 131 : }
14107 :
14108 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
14109 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
14110 :
14111 : static tree
14112 101006 : build_vec_cmp (tree_code code, tree type,
14113 : tree arg0, tree arg1)
14114 : {
14115 101006 : tree zero_vec = build_zero_cst (type);
14116 101006 : tree minus_one_vec = build_minus_one_cst (type);
14117 101006 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
14118 101006 : tree cmp = build2 (code, cmp_type, arg0, arg1);
14119 101006 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14120 : }
14121 :
14122 : /* Possibly warn about an address of OP never being NULL in a comparison
14123 : operation CODE involving null. */
14124 :
14125 : static void
14126 51517 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
14127 : {
14128 : /* Prevent warnings issued for macro expansion. */
14129 51517 : if (!warn_address
14130 30221 : || warning_suppressed_p (op, OPT_Waddress)
14131 81394 : || from_macro_expansion_at (loc))
14132 24396 : return;
14133 :
14134 27121 : if (TREE_CODE (op) == NOP_EXPR)
14135 : {
14136 : /* Allow casts to intptr_t to suppress the warning. */
14137 1133 : tree type = TREE_TYPE (op);
14138 1133 : if (TREE_CODE (type) == INTEGER_TYPE)
14139 : return;
14140 1133 : op = TREE_OPERAND (op, 0);
14141 : }
14142 :
14143 27121 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
14144 : {
14145 : /* Allow a cast to void* to suppress the warning. */
14146 24 : tree type = TREE_TYPE (TREE_TYPE (op));
14147 24 : if (VOID_TYPE_P (type))
14148 : return;
14149 :
14150 : /* Adding any value to a null pointer, including zero, is undefined
14151 : in C. This includes the expression &p[0] where p is the null
14152 : pointer, although &p[0] will have been folded to p by this point
14153 : and so not diagnosed. */
14154 24 : if (code == EQ_EXPR)
14155 22 : warning_at (loc, OPT_Waddress,
14156 : "the comparison will always evaluate as %<false%> "
14157 : "for the pointer operand in %qE must not be NULL",
14158 : op);
14159 : else
14160 2 : warning_at (loc, OPT_Waddress,
14161 : "the comparison will always evaluate as %<true%> "
14162 : "for the pointer operand in %qE must not be NULL",
14163 : op);
14164 :
14165 24 : return;
14166 : }
14167 :
14168 27097 : if (TREE_CODE (op) != ADDR_EXPR)
14169 : return;
14170 :
14171 254 : op = TREE_OPERAND (op, 0);
14172 :
14173 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14174 254 : || TREE_CODE (op) == REALPART_EXPR)
14175 : {
14176 : /* The address of either complex part may not be null. */
14177 14 : if (code == EQ_EXPR)
14178 9 : warning_at (loc, OPT_Waddress,
14179 : "the comparison will always evaluate as %<false%> "
14180 : "for the address of %qE will never be NULL",
14181 : op);
14182 : else
14183 5 : warning_at (loc, OPT_Waddress,
14184 : "the comparison will always evaluate as %<true%> "
14185 : "for the address of %qE will never be NULL",
14186 : op);
14187 14 : return;
14188 : }
14189 :
14190 : /* Set to true in the loop below if OP dereferences is operand.
14191 : In such a case the ultimate target need not be a decl for
14192 : the null [in]equality test to be constant. */
14193 : bool deref = false;
14194 :
14195 : /* Get the outermost array or object, or member. */
14196 294 : while (handled_component_p (op))
14197 : {
14198 72 : if (TREE_CODE (op) == COMPONENT_REF)
14199 : {
14200 : /* Get the member (its address is never null). */
14201 18 : op = TREE_OPERAND (op, 1);
14202 18 : break;
14203 : }
14204 :
14205 : /* Get the outer array/object to refer to in the warning. */
14206 54 : op = TREE_OPERAND (op, 0);
14207 54 : deref = true;
14208 : }
14209 :
14210 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14211 371 : || from_macro_expansion_at (loc))
14212 109 : return;
14213 :
14214 131 : bool w;
14215 131 : if (code == EQ_EXPR)
14216 93 : w = warning_at (loc, OPT_Waddress,
14217 : "the comparison will always evaluate as %<false%> "
14218 : "for the address of %qE will never be NULL",
14219 : op);
14220 : else
14221 38 : w = warning_at (loc, OPT_Waddress,
14222 : "the comparison will always evaluate as %<true%> "
14223 : "for the address of %qE will never be NULL",
14224 : op);
14225 :
14226 131 : if (w && DECL_P (op))
14227 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14228 : }
14229 :
14230 : /* Build a binary-operation expression without default conversions.
14231 : CODE is the kind of expression to build.
14232 : LOCATION is the operator's location.
14233 : This function differs from `build' in several ways:
14234 : the data type of the result is computed and recorded in it,
14235 : warnings are generated if arg data types are invalid,
14236 : special handling for addition and subtraction of pointers is known,
14237 : and some optimization is done (operations on narrow ints
14238 : are done in the narrower type when that gives the same result).
14239 : Constant folding is also done before the result is returned.
14240 :
14241 : Note that the operands will never have enumeral types, or function
14242 : or array types, because either they will have the default conversions
14243 : performed or they have both just been converted to some other type in which
14244 : the arithmetic is to be done. */
14245 :
14246 : tree
14247 16827527 : build_binary_op (location_t location, enum tree_code code,
14248 : tree orig_op0, tree orig_op1, bool convert_p)
14249 : {
14250 16827527 : tree type0, type1, orig_type0, orig_type1;
14251 16827527 : tree eptype;
14252 16827527 : enum tree_code code0, code1;
14253 16827527 : tree op0, op1;
14254 16827527 : tree ret = error_mark_node;
14255 16827527 : const char *invalid_op_diag;
14256 16827527 : bool op0_int_operands, op1_int_operands;
14257 16827527 : bool int_const, int_const_or_overflow, int_operands;
14258 :
14259 : /* Expression code to give to the expression when it is built.
14260 : Normally this is CODE, which is what the caller asked for,
14261 : but in some special cases we change it. */
14262 16827527 : enum tree_code resultcode = code;
14263 :
14264 : /* Data type in which the computation is to be performed.
14265 : In the simplest cases this is the common type of the arguments. */
14266 16827527 : tree result_type = NULL;
14267 :
14268 : /* When the computation is in excess precision, the type of the
14269 : final EXCESS_PRECISION_EXPR. */
14270 16827527 : tree semantic_result_type = NULL;
14271 :
14272 : /* Nonzero means operands have already been type-converted
14273 : in whatever way is necessary.
14274 : Zero means they need to be converted to RESULT_TYPE. */
14275 16827527 : int converted = 0;
14276 :
14277 : /* Nonzero means create the expression with this type, rather than
14278 : RESULT_TYPE. */
14279 16827527 : tree build_type = NULL_TREE;
14280 :
14281 : /* Nonzero means after finally constructing the expression
14282 : convert it to this type. */
14283 16827527 : tree final_type = NULL_TREE;
14284 :
14285 : /* Nonzero if this is an operation like MIN or MAX which can
14286 : safely be computed in short if both args are promoted shorts.
14287 : Also implies COMMON.
14288 : -1 indicates a bitwise operation; this makes a difference
14289 : in the exact conditions for when it is safe to do the operation
14290 : in a narrower mode. */
14291 16827527 : int shorten = 0;
14292 :
14293 : /* Nonzero if this is a comparison operation;
14294 : if both args are promoted shorts, compare the original shorts.
14295 : Also implies COMMON. */
14296 16827527 : int short_compare = 0;
14297 :
14298 : /* Nonzero if this is a right-shift operation, which can be computed on the
14299 : original short and then promoted if the operand is a promoted short. */
14300 16827527 : int short_shift = 0;
14301 :
14302 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14303 16827527 : int common = 0;
14304 :
14305 : /* True means types are compatible as far as ObjC is concerned. */
14306 16827527 : bool objc_ok;
14307 :
14308 : /* True means this is an arithmetic operation that may need excess
14309 : precision. */
14310 16827527 : bool may_need_excess_precision;
14311 :
14312 : /* True means this is a boolean operation that converts both its
14313 : operands to truth-values. */
14314 16827527 : bool boolean_op = false;
14315 :
14316 : /* Remember whether we're doing / or %. */
14317 16827527 : bool doing_div_or_mod = false;
14318 :
14319 : /* Remember whether we're doing << or >>. */
14320 16827527 : bool doing_shift = false;
14321 :
14322 : /* Tree holding instrumentation expression. */
14323 16827527 : tree instrument_expr = NULL;
14324 :
14325 16827527 : if (location == UNKNOWN_LOCATION)
14326 79 : location = input_location;
14327 :
14328 16827527 : op0 = orig_op0;
14329 16827527 : op1 = orig_op1;
14330 :
14331 16827527 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14332 8131446 : if (op0_int_operands)
14333 8131446 : op0 = remove_c_maybe_const_expr (op0);
14334 16827527 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14335 11396225 : if (op1_int_operands)
14336 11396225 : op1 = remove_c_maybe_const_expr (op1);
14337 28223752 : int_operands = (op0_int_operands && op1_int_operands);
14338 11396225 : if (int_operands)
14339 : {
14340 15906543 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14341 7968438 : && TREE_CODE (orig_op1) == INTEGER_CST);
14342 7938105 : int_const = (int_const_or_overflow
14343 7938105 : && !TREE_OVERFLOW (orig_op0)
14344 7938036 : && !TREE_OVERFLOW (orig_op1));
14345 : }
14346 : else
14347 : int_const = int_const_or_overflow = false;
14348 :
14349 : /* Do not apply default conversion in mixed vector/scalar expression. */
14350 16827527 : if (convert_p
14351 16827527 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14352 : {
14353 10125886 : op0 = default_conversion (op0);
14354 10125886 : op1 = default_conversion (op1);
14355 : }
14356 :
14357 16827527 : orig_type0 = type0 = TREE_TYPE (op0);
14358 :
14359 16827527 : orig_type1 = type1 = TREE_TYPE (op1);
14360 :
14361 : /* The expression codes of the data types of the arguments tell us
14362 : whether the arguments are integers, floating, pointers, etc. */
14363 16827527 : code0 = TREE_CODE (type0);
14364 16827527 : code1 = TREE_CODE (type1);
14365 :
14366 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14367 16827529 : STRIP_TYPE_NOPS (op0);
14368 16827530 : STRIP_TYPE_NOPS (op1);
14369 :
14370 : /* If an error was already reported for one of the arguments,
14371 : avoid reporting another error. */
14372 :
14373 16827527 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14374 710 : return error_mark_node;
14375 :
14376 16826817 : if (code0 == POINTER_TYPE
14377 16826817 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14378 11 : return error_mark_node;
14379 :
14380 16826806 : if (code1 == POINTER_TYPE
14381 16826806 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14382 11 : return error_mark_node;
14383 :
14384 33653590 : if ((invalid_op_diag
14385 16826795 : = targetm.invalid_binary_op (code, type0, type1)))
14386 : {
14387 0 : error_at (location, invalid_op_diag);
14388 0 : return error_mark_node;
14389 : }
14390 :
14391 16826795 : switch (code)
14392 : {
14393 : case PLUS_EXPR:
14394 : case MINUS_EXPR:
14395 : case MULT_EXPR:
14396 : case TRUNC_DIV_EXPR:
14397 : case CEIL_DIV_EXPR:
14398 : case FLOOR_DIV_EXPR:
14399 : case ROUND_DIV_EXPR:
14400 : case EXACT_DIV_EXPR:
14401 : may_need_excess_precision = true;
14402 : break;
14403 :
14404 2732764 : case EQ_EXPR:
14405 2732764 : case NE_EXPR:
14406 2732764 : case LE_EXPR:
14407 2732764 : case GE_EXPR:
14408 2732764 : case LT_EXPR:
14409 2732764 : case GT_EXPR:
14410 : /* Excess precision for implicit conversions of integers to
14411 : floating point in C11 and later. */
14412 2732764 : may_need_excess_precision = (flag_isoc11
14413 2732764 : && (ANY_INTEGRAL_TYPE_P (type0)
14414 486780 : || ANY_INTEGRAL_TYPE_P (type1)));
14415 : break;
14416 :
14417 : default:
14418 16826795 : may_need_excess_precision = false;
14419 : break;
14420 : }
14421 16826795 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14422 : {
14423 184 : op0 = TREE_OPERAND (op0, 0);
14424 184 : type0 = TREE_TYPE (op0);
14425 : }
14426 16826611 : else if (may_need_excess_precision
14427 16826611 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14428 : {
14429 771 : type0 = eptype;
14430 771 : op0 = convert (eptype, op0);
14431 : }
14432 16826795 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14433 : {
14434 889 : op1 = TREE_OPERAND (op1, 0);
14435 889 : type1 = TREE_TYPE (op1);
14436 : }
14437 16825906 : else if (may_need_excess_precision
14438 16825906 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14439 : {
14440 592 : type1 = eptype;
14441 592 : op1 = convert (eptype, op1);
14442 : }
14443 :
14444 16826795 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14445 :
14446 : /* In case when one of the operands of the binary operation is
14447 : a vector and another is a scalar -- convert scalar to vector. */
14448 18406688 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14449 18404791 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14450 : {
14451 2504 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14452 : orig_op1, true);
14453 :
14454 2504 : switch (convert_flag)
14455 : {
14456 12 : case stv_error:
14457 12 : return error_mark_node;
14458 594 : case stv_firstarg:
14459 594 : {
14460 594 : bool maybe_const = true;
14461 594 : tree sc;
14462 594 : sc = c_fully_fold (op0, false, &maybe_const);
14463 594 : sc = save_expr (sc);
14464 594 : sc = convert (TREE_TYPE (type1), sc);
14465 594 : op0 = build_vector_from_val (type1, sc);
14466 594 : if (!maybe_const)
14467 93 : op0 = c_wrap_maybe_const (op0, true);
14468 594 : orig_type0 = type0 = TREE_TYPE (op0);
14469 594 : code0 = TREE_CODE (type0);
14470 594 : converted = 1;
14471 594 : break;
14472 : }
14473 1056 : case stv_secondarg:
14474 1056 : {
14475 1056 : bool maybe_const = true;
14476 1056 : tree sc;
14477 1056 : sc = c_fully_fold (op1, false, &maybe_const);
14478 1056 : sc = save_expr (sc);
14479 1056 : sc = convert (TREE_TYPE (type0), sc);
14480 1056 : op1 = build_vector_from_val (type0, sc);
14481 1056 : if (!maybe_const)
14482 37 : op1 = c_wrap_maybe_const (op1, true);
14483 1056 : orig_type1 = type1 = TREE_TYPE (op1);
14484 1056 : code1 = TREE_CODE (type1);
14485 1056 : converted = 1;
14486 1056 : break;
14487 : }
14488 : default:
14489 : break;
14490 : }
14491 : }
14492 :
14493 16826783 : switch (code)
14494 : {
14495 8625111 : case PLUS_EXPR:
14496 : /* Handle the pointer + int case. */
14497 8625111 : if (code0 == POINTER_TYPE
14498 1250202 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14499 : {
14500 1250196 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14501 1250196 : goto return_build_binary_op;
14502 : }
14503 7374915 : else if (code1 == POINTER_TYPE
14504 1539 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14505 : {
14506 1533 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14507 1533 : goto return_build_binary_op;
14508 : }
14509 : else
14510 : common = 1;
14511 : break;
14512 :
14513 790973 : case MINUS_EXPR:
14514 : /* Subtraction of two similar pointers.
14515 : We must subtract them as integers, then divide by object size. */
14516 790973 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14517 790973 : && comp_target_types (location, type0, type1))
14518 : {
14519 3683 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14520 3683 : goto return_build_binary_op;
14521 : }
14522 : /* Handle pointer minus int. Just like pointer plus int. */
14523 787290 : else if (code0 == POINTER_TYPE
14524 16818 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14525 : {
14526 16806 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14527 16806 : goto return_build_binary_op;
14528 : }
14529 : else
14530 : common = 1;
14531 : break;
14532 :
14533 : case MULT_EXPR:
14534 : common = 1;
14535 : break;
14536 :
14537 274533 : case TRUNC_DIV_EXPR:
14538 274533 : case CEIL_DIV_EXPR:
14539 274533 : case FLOOR_DIV_EXPR:
14540 274533 : case ROUND_DIV_EXPR:
14541 274533 : case EXACT_DIV_EXPR:
14542 274533 : doing_div_or_mod = true;
14543 274533 : warn_for_div_by_zero (location, op1);
14544 :
14545 274532 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14546 49646 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14547 47632 : || code0 == COMPLEX_TYPE
14548 45585 : || gnu_vector_type_p (type0))
14549 324177 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14550 47212 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14551 47045 : || code1 == COMPLEX_TYPE
14552 45587 : || gnu_vector_type_p (type1)))
14553 : {
14554 274528 : enum tree_code tcode0 = code0, tcode1 = code1;
14555 :
14556 274528 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14557 47631 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14558 274528 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14559 47042 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14560 :
14561 274528 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
14562 69632 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
14563 204897 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
14564 12988 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE)))
14565 82618 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14566 : resultcode = RDIV_EXPR;
14567 : else
14568 : /* Although it would be tempting to shorten always here, that
14569 : loses on some targets, since the modulo instruction is
14570 : undefined if the quotient can't be represented in the
14571 : computation mode. We shorten only if unsigned or if
14572 : dividing by something we know != -1. */
14573 191910 : shorten = may_shorten_divmod (op0, op1);
14574 : common = 1;
14575 : }
14576 : break;
14577 :
14578 1585875 : case BIT_AND_EXPR:
14579 1585875 : case BIT_IOR_EXPR:
14580 1585875 : case BIT_XOR_EXPR:
14581 1585875 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14582 1050448 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14583 : shorten = -1;
14584 : /* Allow vector types which are not floating point types. */
14585 535463 : else if (gnu_vector_type_p (type0)
14586 535386 : && gnu_vector_type_p (type1)
14587 535386 : && !VECTOR_FLOAT_TYPE_P (type0)
14588 1070846 : && !VECTOR_FLOAT_TYPE_P (type1))
14589 : common = 1;
14590 : break;
14591 :
14592 58536 : case TRUNC_MOD_EXPR:
14593 58536 : case FLOOR_MOD_EXPR:
14594 58536 : doing_div_or_mod = true;
14595 58536 : warn_for_div_by_zero (location, op1);
14596 :
14597 58536 : if (gnu_vector_type_p (type0)
14598 297 : && gnu_vector_type_p (type1)
14599 297 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14600 58833 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14601 : common = 1;
14602 58239 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14603 58238 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14604 : {
14605 : /* Although it would be tempting to shorten always here, that loses
14606 : on some targets, since the modulo instruction is undefined if the
14607 : quotient can't be represented in the computation mode. We shorten
14608 : only if unsigned or if dividing by something we know != -1. */
14609 58238 : shorten = may_shorten_divmod (op0, op1);
14610 58238 : common = 1;
14611 : }
14612 : break;
14613 :
14614 511056 : case TRUTH_ANDIF_EXPR:
14615 511056 : case TRUTH_ORIF_EXPR:
14616 511056 : case TRUTH_AND_EXPR:
14617 511056 : case TRUTH_OR_EXPR:
14618 511056 : case TRUTH_XOR_EXPR:
14619 511056 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14620 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14621 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14622 0 : || code0 == BITINT_TYPE)
14623 511056 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14624 163 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14625 24 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14626 22 : || code1 == BITINT_TYPE))
14627 : {
14628 : /* Result of these operations is always an int,
14629 : but that does not mean the operands should be
14630 : converted to ints! */
14631 511056 : result_type = integer_type_node;
14632 511056 : if (op0_int_operands)
14633 : {
14634 110409 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14635 110409 : op0 = remove_c_maybe_const_expr (op0);
14636 : }
14637 : else
14638 400647 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14639 511056 : if (op1_int_operands)
14640 : {
14641 71410 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14642 71410 : op1 = remove_c_maybe_const_expr (op1);
14643 : }
14644 : else
14645 439646 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14646 : converted = 1;
14647 : boolean_op = true;
14648 : }
14649 511056 : if (code == TRUTH_ANDIF_EXPR)
14650 : {
14651 249969 : int_const_or_overflow = (int_operands
14652 63277 : && TREE_CODE (orig_op0) == INTEGER_CST
14653 249985 : && (op0 == truthvalue_false_node
14654 13202 : || TREE_CODE (orig_op1) == INTEGER_CST));
14655 : int_const = (int_const_or_overflow
14656 63250 : && !TREE_OVERFLOW (orig_op0)
14657 63250 : && (op0 == truthvalue_false_node
14658 13186 : || !TREE_OVERFLOW (orig_op1)));
14659 : }
14660 324337 : else if (code == TRUTH_ORIF_EXPR)
14661 : {
14662 331009 : int_const_or_overflow = (int_operands
14663 6823 : && TREE_CODE (orig_op0) == INTEGER_CST
14664 331032 : && (op0 == truthvalue_true_node
14665 2138 : || TREE_CODE (orig_op1) == INTEGER_CST));
14666 : int_const = (int_const_or_overflow
14667 6793 : && !TREE_OVERFLOW (orig_op0)
14668 6793 : && (op0 == truthvalue_true_node
14669 2115 : || !TREE_OVERFLOW (orig_op1)));
14670 : }
14671 : break;
14672 :
14673 : /* Shift operations: result has same type as first operand;
14674 : always convert second operand to int.
14675 : Also set SHORT_SHIFT if shifting rightward. */
14676 :
14677 231297 : case RSHIFT_EXPR:
14678 231297 : if (gnu_vector_type_p (type0)
14679 814 : && gnu_vector_type_p (type1)
14680 258 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14681 256 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14682 231553 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14683 : TYPE_VECTOR_SUBPARTS (type1)))
14684 : {
14685 : result_type = type0;
14686 : converted = 1;
14687 : }
14688 231043 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14689 1119 : || code0 == BITINT_TYPE
14690 564 : || (gnu_vector_type_p (type0)
14691 560 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14692 232155 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14693 : {
14694 231034 : doing_shift = true;
14695 231034 : if (TREE_CODE (op1) == INTEGER_CST)
14696 : {
14697 201614 : if (tree_int_cst_sgn (op1) < 0)
14698 : {
14699 269 : int_const = false;
14700 269 : if (c_inhibit_evaluation_warnings == 0)
14701 129 : warning_at (location, OPT_Wshift_count_negative,
14702 : "right shift count is negative");
14703 : }
14704 201345 : else if (code0 == VECTOR_TYPE)
14705 : {
14706 458 : if (compare_tree_int (op1,
14707 458 : TYPE_PRECISION (TREE_TYPE (type0)))
14708 : >= 0)
14709 : {
14710 16 : int_const = false;
14711 16 : if (c_inhibit_evaluation_warnings == 0)
14712 16 : warning_at (location, OPT_Wshift_count_overflow,
14713 : "right shift count >= width of vector element");
14714 : }
14715 : }
14716 : else
14717 : {
14718 200887 : if (!integer_zerop (op1))
14719 200529 : short_shift = 1;
14720 :
14721 200887 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14722 : {
14723 584 : int_const = false;
14724 584 : if (c_inhibit_evaluation_warnings == 0)
14725 50 : warning_at (location, OPT_Wshift_count_overflow,
14726 : "right shift count >= width of type");
14727 : }
14728 : }
14729 : }
14730 :
14731 : /* Use the type of the value to be shifted. */
14732 : result_type = type0;
14733 : /* Avoid converting op1 to result_type later. */
14734 : converted = 1;
14735 : }
14736 : break;
14737 :
14738 803160 : case LSHIFT_EXPR:
14739 803160 : if (gnu_vector_type_p (type0)
14740 596 : && gnu_vector_type_p (type1)
14741 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14742 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14743 803473 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14744 : TYPE_VECTOR_SUBPARTS (type1)))
14745 : {
14746 : result_type = type0;
14747 : converted = 1;
14748 : }
14749 802849 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14750 777 : || code0 == BITINT_TYPE
14751 291 : || (gnu_vector_type_p (type0)
14752 285 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14753 803617 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14754 : {
14755 802835 : doing_shift = true;
14756 802835 : if (TREE_CODE (op0) == INTEGER_CST
14757 438607 : && tree_int_cst_sgn (op0) < 0
14758 803584 : && !TYPE_OVERFLOW_WRAPS (type0))
14759 : {
14760 : /* Don't reject a left shift of a negative value in a context
14761 : where a constant expression is needed in C90. */
14762 712 : if (flag_isoc99)
14763 666 : int_const = false;
14764 712 : if (c_inhibit_evaluation_warnings == 0)
14765 712 : warning_at (location, OPT_Wshift_negative_value,
14766 : "left shift of negative value");
14767 : }
14768 802835 : if (TREE_CODE (op1) == INTEGER_CST)
14769 : {
14770 730382 : if (tree_int_cst_sgn (op1) < 0)
14771 : {
14772 313 : int_const = false;
14773 313 : if (c_inhibit_evaluation_warnings == 0)
14774 135 : warning_at (location, OPT_Wshift_count_negative,
14775 : "left shift count is negative");
14776 : }
14777 730069 : else if (code0 == VECTOR_TYPE)
14778 : {
14779 216 : if (compare_tree_int (op1,
14780 216 : TYPE_PRECISION (TREE_TYPE (type0)))
14781 : >= 0)
14782 : {
14783 6 : int_const = false;
14784 6 : if (c_inhibit_evaluation_warnings == 0)
14785 6 : warning_at (location, OPT_Wshift_count_overflow,
14786 : "left shift count >= width of vector element");
14787 : }
14788 : }
14789 729853 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14790 : {
14791 29733 : int_const = false;
14792 29733 : if (c_inhibit_evaluation_warnings == 0)
14793 113 : warning_at (location, OPT_Wshift_count_overflow,
14794 : "left shift count >= width of type");
14795 : }
14796 700120 : else if (TREE_CODE (op0) == INTEGER_CST
14797 364819 : && maybe_warn_shift_overflow (location, op0, op1)
14798 700748 : && flag_isoc99)
14799 : int_const = false;
14800 : }
14801 :
14802 : /* Use the type of the value to be shifted. */
14803 : result_type = type0;
14804 : /* Avoid converting op1 to result_type later. */
14805 : converted = 1;
14806 : }
14807 : break;
14808 :
14809 1736103 : case EQ_EXPR:
14810 1736103 : case NE_EXPR:
14811 1736103 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14812 : {
14813 41886 : tree intt;
14814 41886 : if (!vector_types_compatible_elements_p (type0, type1))
14815 : {
14816 4 : error_at (location, "comparing vectors with different "
14817 : "element types");
14818 4 : return error_mark_node;
14819 : }
14820 :
14821 41882 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14822 83764 : TYPE_VECTOR_SUBPARTS (type1)))
14823 : {
14824 1 : error_at (location, "comparing vectors with different "
14825 : "number of elements");
14826 1 : return error_mark_node;
14827 : }
14828 :
14829 : /* It's not precisely specified how the usual arithmetic
14830 : conversions apply to the vector types. Here, we use
14831 : the unsigned type if one of the operands is signed and
14832 : the other one is unsigned. */
14833 41881 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14834 : {
14835 4 : if (!TYPE_UNSIGNED (type0))
14836 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14837 : else
14838 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14839 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14840 : "types %qT and %qT", type0, type1);
14841 : }
14842 :
14843 : /* Always construct signed integer vector type. */
14844 41881 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
14845 : result_type = type0;
14846 : else
14847 : {
14848 41881 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
14849 :
14850 125643 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14851 83762 : (SCALAR_TYPE_MODE
14852 : (TREE_TYPE (type0))), 0);
14853 41881 : if (!intt)
14854 : {
14855 0 : error_at (location, "could not find an integer type "
14856 : "of the same size as %qT",
14857 0 : TREE_TYPE (type0));
14858 0 : return error_mark_node;
14859 : }
14860 41881 : result_type = build_opaque_vector_type (intt, nelts);
14861 : }
14862 41881 : converted = 1;
14863 41881 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14864 41881 : goto return_build_binary_op;
14865 : }
14866 1694217 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14867 254228 : warning_at (location,
14868 254228 : OPT_Wfloat_equal,
14869 : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14870 : /* Result of comparison is always int,
14871 : but don't convert the args to int! */
14872 1694217 : build_type = integer_type_node;
14873 1694217 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14874 156392 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14875 1573340 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14876 : || code1 == BITINT_TYPE
14877 : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14878 : short_compare = 1;
14879 121143 : else if (code0 == POINTER_TYPE
14880 121143 : && (code1 == NULLPTR_TYPE
14881 120766 : || null_pointer_constant_p (orig_op1)))
14882 : {
14883 51229 : maybe_warn_for_null_address (location, op0, code);
14884 51229 : result_type = type0;
14885 : }
14886 69914 : else if (code1 == POINTER_TYPE
14887 69914 : && (code0 == NULLPTR_TYPE
14888 69812 : || null_pointer_constant_p (orig_op0)))
14889 : {
14890 288 : maybe_warn_for_null_address (location, op1, code);
14891 288 : result_type = type1;
14892 : }
14893 69626 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14894 : {
14895 69536 : tree tt0 = TREE_TYPE (type0);
14896 69536 : tree tt1 = TREE_TYPE (type1);
14897 69536 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14898 69536 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14899 69536 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14900 :
14901 : /* Anything compares with void *. void * compares with anything.
14902 : Otherwise, the targets must be compatible
14903 : and both must be object or both incomplete. */
14904 69536 : if (comp_target_types (location, type0, type1))
14905 48241 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14906 21295 : else if (!addr_space_superset (as0, as1, &as_common))
14907 : {
14908 0 : error_at (location, "comparison of pointers to "
14909 : "disjoint address spaces");
14910 0 : return error_mark_node;
14911 : }
14912 21295 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14913 : {
14914 21086 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14915 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14916 : "comparison of %<void *%> with function pointer");
14917 : }
14918 209 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14919 : {
14920 161 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14921 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14922 : "comparison of %<void *%> with function pointer");
14923 : }
14924 : else
14925 : /* Avoid warning about the volatile ObjC EH puts on decls. */
14926 48 : if (!objc_ok)
14927 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14928 : "comparison of distinct pointer types lacks a cast");
14929 :
14930 48297 : if (result_type == NULL_TREE)
14931 : {
14932 21295 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14933 21295 : result_type = c_build_pointer_type
14934 21295 : (c_build_qualified_type (void_type_node, qual));
14935 : }
14936 : }
14937 90 : else if (code0 == POINTER_TYPE
14938 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14939 : {
14940 26 : result_type = type0;
14941 26 : pedwarn (location, 0, "comparison between pointer and integer");
14942 : }
14943 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14944 21 : && code1 == POINTER_TYPE)
14945 : {
14946 8 : result_type = type1;
14947 8 : pedwarn (location, 0, "comparison between pointer and integer");
14948 : }
14949 : /* 6.5.9: One of the following shall hold:
14950 : -- both operands have type nullptr_t; */
14951 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14952 : {
14953 22 : result_type = nullptr_type_node;
14954 : /* No need to convert the operands to result_type later. */
14955 22 : converted = 1;
14956 : }
14957 : /* -- one operand has type nullptr_t and the other is a null pointer
14958 : constant. We will have to convert the former to the type of the
14959 : latter, because during gimplification we can't have mismatching
14960 : comparison operand type. We convert from nullptr_t to the other
14961 : type, since only nullptr_t can be converted to nullptr_t. Also,
14962 : even a constant 0 is a null pointer constant, so we may have to
14963 : create a pointer type from its type. */
14964 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
14965 19 : result_type = (INTEGRAL_TYPE_P (type1)
14966 19 : ? c_build_pointer_type (type1) : type1);
14967 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
14968 11 : result_type = (INTEGRAL_TYPE_P (type0)
14969 11 : ? c_build_pointer_type (type0) : type0);
14970 5008113 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14971 3313871 : || truth_value_p (TREE_CODE (orig_op0)))
14972 3381163 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14973 3381159 : || truth_value_p (TREE_CODE (orig_op1))))
14974 71507 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14975 : break;
14976 :
14977 996657 : case LE_EXPR:
14978 996657 : case GE_EXPR:
14979 996657 : case LT_EXPR:
14980 996657 : case GT_EXPR:
14981 996657 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14982 : {
14983 59130 : tree intt;
14984 59130 : if (!vector_types_compatible_elements_p (type0, type1))
14985 : {
14986 5 : error_at (location, "comparing vectors with different "
14987 : "element types");
14988 5 : return error_mark_node;
14989 : }
14990 :
14991 59125 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14992 118250 : TYPE_VECTOR_SUBPARTS (type1)))
14993 : {
14994 0 : error_at (location, "comparing vectors with different "
14995 : "number of elements");
14996 0 : return error_mark_node;
14997 : }
14998 :
14999 : /* It's not precisely specified how the usual arithmetic
15000 : conversions apply to the vector types. Here, we use
15001 : the unsigned type if one of the operands is signed and
15002 : the other one is unsigned. */
15003 59125 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
15004 : {
15005 15 : if (!TYPE_UNSIGNED (type0))
15006 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
15007 : else
15008 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
15009 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
15010 : "types %qT and %qT", type0, type1);
15011 : }
15012 :
15013 : /* Always construct signed integer vector type. */
15014 177375 : intt = c_common_type_for_size (GET_MODE_BITSIZE
15015 118250 : (SCALAR_TYPE_MODE
15016 : (TREE_TYPE (type0))), 0);
15017 59125 : if (!intt)
15018 : {
15019 0 : error_at (location, "could not find an integer type "
15020 : "of the same size as %qT",
15021 0 : TREE_TYPE (type0));
15022 0 : return error_mark_node;
15023 : }
15024 59125 : result_type = build_opaque_vector_type (intt,
15025 59125 : TYPE_VECTOR_SUBPARTS (type0));
15026 59125 : converted = 1;
15027 59125 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
15028 59125 : goto return_build_binary_op;
15029 : }
15030 937527 : build_type = integer_type_node;
15031 937527 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
15032 64660 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
15033 873414 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15034 : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
15035 : short_compare = 1;
15036 64145 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15037 : {
15038 64078 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
15039 64078 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
15040 64078 : addr_space_t as_common;
15041 :
15042 64078 : if (comp_target_types (location, type0, type1))
15043 : {
15044 63965 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15045 63965 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
15046 63965 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
15047 32 : pedwarn_c99 (location, OPT_Wpedantic,
15048 : "comparison of complete and incomplete pointers");
15049 63933 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
15050 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15051 : "ordered comparisons of pointers to functions");
15052 63933 : else if (null_pointer_constant_p (orig_op0)
15053 63933 : || null_pointer_constant_p (orig_op1))
15054 8 : warning_at (location, OPT_Wextra,
15055 : "ordered comparison of pointer with null pointer");
15056 :
15057 : }
15058 113 : else if (!addr_space_superset (as0, as1, &as_common))
15059 : {
15060 0 : error_at (location, "comparison of pointers to "
15061 : "disjoint address spaces");
15062 0 : return error_mark_node;
15063 : }
15064 : else
15065 : {
15066 113 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
15067 113 : result_type = c_build_pointer_type
15068 113 : (c_build_qualified_type (void_type_node, qual));
15069 113 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15070 : "comparison of distinct pointer types lacks a cast");
15071 : }
15072 : }
15073 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
15074 : {
15075 17 : result_type = type0;
15076 17 : if (pedantic)
15077 11 : pedwarn (location, OPT_Wpedantic,
15078 : "ordered comparison of pointer with integer zero");
15079 6 : else if (extra_warnings)
15080 1 : warning_at (location, OPT_Wextra,
15081 : "ordered comparison of pointer with integer zero");
15082 : }
15083 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
15084 : {
15085 12 : result_type = type1;
15086 12 : if (pedantic)
15087 2 : pedwarn (location, OPT_Wpedantic,
15088 : "ordered comparison of pointer with integer zero");
15089 10 : else if (extra_warnings)
15090 1 : warning_at (location, OPT_Wextra,
15091 : "ordered comparison of pointer with integer zero");
15092 : }
15093 38 : else if (code0 == POINTER_TYPE
15094 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15095 : {
15096 18 : result_type = type0;
15097 18 : pedwarn (location, 0, "comparison between pointer and integer");
15098 : }
15099 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15100 20 : && code1 == POINTER_TYPE)
15101 : {
15102 18 : result_type = type1;
15103 18 : pedwarn (location, 0, "comparison between pointer and integer");
15104 : }
15105 :
15106 937527 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
15107 64143 : && current_function_decl != NULL_TREE
15108 1001659 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
15109 : {
15110 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
15111 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
15112 :
15113 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
15114 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
15115 : }
15116 :
15117 2812489 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15118 1874962 : || truth_value_p (TREE_CODE (orig_op0)))
15119 1874979 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15120 1874979 : || truth_value_p (TREE_CODE (orig_op1))))
15121 517 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15122 : break;
15123 :
15124 43 : case MIN_EXPR:
15125 43 : case MAX_EXPR:
15126 : /* Used for OpenMP atomics. */
15127 43 : gcc_assert (flag_openmp);
15128 : common = 1;
15129 : break;
15130 :
15131 0 : default:
15132 0 : gcc_unreachable ();
15133 : }
15134 :
15135 15453548 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
15136 0 : return error_mark_node;
15137 :
15138 15453548 : if (gnu_vector_type_p (type0)
15139 1479467 : && gnu_vector_type_p (type1)
15140 16932178 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
15141 1478610 : || !vector_types_compatible_elements_p (type0, type1)))
15142 : {
15143 26 : gcc_rich_location richloc (location);
15144 26 : maybe_range_label_for_tree_type_mismatch
15145 26 : label_for_op0 (orig_op0, orig_op1),
15146 26 : label_for_op1 (orig_op1, orig_op0);
15147 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
15148 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
15149 26 : binary_op_error (&richloc, code, type0, type1);
15150 26 : return error_mark_node;
15151 26 : }
15152 :
15153 15453522 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
15154 1733265 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
15155 1664846 : || gnu_vector_type_p (type0))
15156 17001382 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
15157 1572537 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
15158 1480713 : || gnu_vector_type_p (type1)))
15159 : {
15160 15266013 : bool first_complex = (code0 == COMPLEX_TYPE);
15161 15266013 : bool second_complex = (code1 == COMPLEX_TYPE);
15162 15266013 : int none_complex = (!first_complex && !second_complex);
15163 :
15164 15266013 : if (shorten || common || short_compare)
15165 : {
15166 13722238 : result_type = c_common_type (type0, type1);
15167 13722238 : do_warn_double_promotion (result_type, type0, type1,
15168 : "implicit conversion from %qT to %qT "
15169 : "to match other operand of binary "
15170 : "expression",
15171 : location);
15172 13722238 : if (result_type == error_mark_node)
15173 : return error_mark_node;
15174 : }
15175 :
15176 15265991 : if (first_complex != second_complex
15177 82043 : && (code == PLUS_EXPR
15178 : || code == MINUS_EXPR
15179 82043 : || code == MULT_EXPR
15180 17525 : || (code == TRUNC_DIV_EXPR && first_complex))
15181 65932 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15182 15328245 : && flag_signed_zeros)
15183 : {
15184 : /* An operation on mixed real/complex operands must be
15185 : handled specially, but the language-independent code can
15186 : more easily optimize the plain complex arithmetic if
15187 : -fno-signed-zeros. */
15188 61942 : tree real_type = TREE_TYPE (result_type);
15189 61942 : tree real, imag;
15190 61942 : if (type0 != orig_type0 || type1 != orig_type1)
15191 : {
15192 94 : gcc_assert (may_need_excess_precision && common);
15193 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15194 : }
15195 61942 : if (first_complex)
15196 : {
15197 8253 : if (TREE_TYPE (op0) != result_type)
15198 1787 : op0 = convert_and_check (location, result_type, op0);
15199 8253 : if (TREE_TYPE (op1) != real_type)
15200 4601 : op1 = convert_and_check (location, real_type, op1);
15201 : }
15202 : else
15203 : {
15204 53689 : if (TREE_TYPE (op0) != real_type)
15205 2698 : op0 = convert_and_check (location, real_type, op0);
15206 53689 : if (TREE_TYPE (op1) != result_type)
15207 1866 : op1 = convert_and_check (location, result_type, op1);
15208 : }
15209 61942 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15210 0 : return error_mark_node;
15211 61942 : if (first_complex)
15212 : {
15213 8253 : op0 = save_expr (op0);
15214 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15215 : op0, true);
15216 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15217 : op0, true);
15218 8253 : switch (code)
15219 : {
15220 3488 : case MULT_EXPR:
15221 3488 : case TRUNC_DIV_EXPR:
15222 3488 : op1 = save_expr (op1);
15223 3488 : imag = build2 (resultcode, real_type, imag, op1);
15224 : /* Fall through. */
15225 8253 : case PLUS_EXPR:
15226 8253 : case MINUS_EXPR:
15227 8253 : real = build2 (resultcode, real_type, real, op1);
15228 8253 : break;
15229 0 : default:
15230 0 : gcc_unreachable();
15231 : }
15232 : }
15233 : else
15234 : {
15235 53689 : op1 = save_expr (op1);
15236 53689 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15237 : op1, true);
15238 53689 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15239 : op1, true);
15240 53689 : switch (code)
15241 : {
15242 2008 : case MULT_EXPR:
15243 2008 : op0 = save_expr (op0);
15244 2008 : imag = build2 (resultcode, real_type, op0, imag);
15245 : /* Fall through. */
15246 31957 : case PLUS_EXPR:
15247 31957 : real = build2 (resultcode, real_type, op0, real);
15248 31957 : break;
15249 21732 : case MINUS_EXPR:
15250 21732 : real = build2 (resultcode, real_type, op0, real);
15251 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15252 21732 : break;
15253 0 : default:
15254 0 : gcc_unreachable();
15255 : }
15256 : }
15257 61942 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15258 61942 : goto return_build_binary_op;
15259 : }
15260 :
15261 : /* For certain operations (which identify themselves by shorten != 0)
15262 : if both args were extended from the same smaller type,
15263 : do the arithmetic in that type and then extend.
15264 :
15265 : shorten !=0 and !=1 indicates a bitwise operation.
15266 : For them, this optimization is safe only if
15267 : both args are zero-extended or both are sign-extended.
15268 : Otherwise, we might change the result.
15269 : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15270 : but calculated in (unsigned short) it would be (unsigned short)-1. */
15271 :
15272 15204049 : if (shorten && none_complex)
15273 : {
15274 1293856 : final_type = result_type;
15275 1293856 : result_type = shorten_binary_op (result_type, op0, op1,
15276 : shorten == -1);
15277 : }
15278 :
15279 : /* Shifts can be shortened if shifting right. */
15280 :
15281 15204049 : if (short_shift)
15282 : {
15283 200529 : int unsigned_arg;
15284 200529 : tree arg0 = get_narrower (op0, &unsigned_arg);
15285 :
15286 200529 : final_type = result_type;
15287 :
15288 200529 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15289 141046 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15290 :
15291 200529 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15292 2990 : && tree_int_cst_sgn (op1) > 0
15293 : /* We can shorten only if the shift count is less than the
15294 : number of bits in the smaller type size. */
15295 2990 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15296 : /* We cannot drop an unsigned shift after sign-extension. */
15297 203379 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15298 : {
15299 : /* Do an unsigned shift if the operand was zero-extended. */
15300 2835 : result_type
15301 2835 : = c_common_signed_or_unsigned_type (unsigned_arg,
15302 2835 : TREE_TYPE (arg0));
15303 : /* Convert value-to-be-shifted to that type. */
15304 2835 : if (TREE_TYPE (op0) != result_type)
15305 2835 : op0 = convert (result_type, op0);
15306 : converted = 1;
15307 : }
15308 : }
15309 :
15310 : /* Comparison operations are shortened too but differently.
15311 : They identify themselves by setting short_compare = 1. */
15312 :
15313 15204049 : if (short_compare)
15314 : {
15315 : /* Don't write &op0, etc., because that would prevent op0
15316 : from being kept in a register.
15317 : Instead, make copies of the our local variables and
15318 : pass the copies by reference, then copy them back afterward. */
15319 2446454 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15320 2446454 : enum tree_code xresultcode = resultcode;
15321 2446454 : tree val
15322 2446454 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15323 : &xresultcode);
15324 :
15325 2446454 : if (val != NULL_TREE)
15326 : {
15327 15793 : ret = val;
15328 15793 : goto return_build_binary_op;
15329 : }
15330 :
15331 2430661 : op0 = xop0, op1 = xop1;
15332 2430661 : converted = 1;
15333 2430661 : resultcode = xresultcode;
15334 :
15335 2430661 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15336 : {
15337 2282621 : bool op0_maybe_const = true;
15338 2282621 : bool op1_maybe_const = true;
15339 2282621 : tree orig_op0_folded, orig_op1_folded;
15340 :
15341 2282621 : if (in_late_binary_op)
15342 : {
15343 : orig_op0_folded = orig_op0;
15344 : orig_op1_folded = orig_op1;
15345 : }
15346 : else
15347 : {
15348 : /* Fold for the sake of possible warnings, as in
15349 : build_conditional_expr. This requires the
15350 : "original" values to be folded, not just op0 and
15351 : op1. */
15352 2274110 : c_inhibit_evaluation_warnings++;
15353 2274110 : op0 = c_fully_fold (op0, require_constant_value,
15354 : &op0_maybe_const);
15355 2274110 : op1 = c_fully_fold (op1, require_constant_value,
15356 : &op1_maybe_const);
15357 2274110 : c_inhibit_evaluation_warnings--;
15358 2274110 : orig_op0_folded = c_fully_fold (orig_op0,
15359 : require_constant_value,
15360 : NULL);
15361 2274110 : orig_op1_folded = c_fully_fold (orig_op1,
15362 : require_constant_value,
15363 : NULL);
15364 : }
15365 :
15366 2282621 : if (warn_sign_compare)
15367 373624 : warn_for_sign_compare (location, orig_op0_folded,
15368 : orig_op1_folded, op0, op1,
15369 : result_type, resultcode);
15370 2282621 : if (!in_late_binary_op && !int_operands)
15371 : {
15372 2097625 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15373 2094101 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15374 2097625 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15375 826331 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15376 : }
15377 : }
15378 : }
15379 : }
15380 :
15381 : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15382 : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15383 : Then the expression will be built.
15384 : It will be given type FINAL_TYPE if that is nonzero;
15385 : otherwise, it will be given type RESULT_TYPE. */
15386 :
15387 15375765 : if (!result_type)
15388 : {
15389 : /* Favor showing any expression locations that are available. */
15390 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15391 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15392 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15393 514 : return error_mark_node;
15394 514 : }
15395 :
15396 15375251 : if (build_type == NULL_TREE)
15397 : {
15398 12759308 : build_type = result_type;
15399 12759308 : if ((type0 != orig_type0 || type1 != orig_type1)
15400 797 : && !boolean_op)
15401 : {
15402 797 : gcc_assert (may_need_excess_precision && common);
15403 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15404 : }
15405 : }
15406 :
15407 15375251 : if (!converted)
15408 : {
15409 11397852 : op0 = ep_convert_and_check (location, result_type, op0,
15410 : semantic_result_type);
15411 11397852 : op1 = ep_convert_and_check (location, result_type, op1,
15412 : semantic_result_type);
15413 :
15414 : /* This can happen if one operand has a vector type, and the other
15415 : has a different type. */
15416 11397852 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15417 3 : return error_mark_node;
15418 : }
15419 :
15420 15375248 : if (sanitize_flags_p ((SANITIZE_SHIFT
15421 : | SANITIZE_DIVIDE
15422 : | SANITIZE_FLOAT_DIVIDE
15423 : | SANITIZE_SI_OVERFLOW))
15424 23365 : && current_function_decl != NULL_TREE
15425 19902 : && (doing_div_or_mod || doing_shift)
15426 15378173 : && !require_constant_value)
15427 : {
15428 : /* OP0 and/or OP1 might have side-effects. */
15429 2869 : op0 = save_expr (op0);
15430 2869 : op1 = save_expr (op1);
15431 2869 : op0 = c_fully_fold (op0, false, NULL);
15432 2869 : op1 = c_fully_fold (op1, false, NULL);
15433 2869 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15434 : | SANITIZE_FLOAT_DIVIDE
15435 : | SANITIZE_SI_OVERFLOW))))
15436 912 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15437 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15438 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15439 : }
15440 :
15441 : /* Treat expressions in initializers specially as they can't trap. */
15442 15375248 : if (int_const_or_overflow)
15443 7922676 : ret = (require_constant_value
15444 7922676 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15445 : op0, op1)
15446 7876557 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15447 : else
15448 7452572 : ret = build2 (resultcode, build_type, op0, op1);
15449 15375248 : if (final_type != NULL_TREE)
15450 1494385 : ret = convert (final_type, ret);
15451 :
15452 13880863 : return_build_binary_op:
15453 16826207 : gcc_assert (ret != error_mark_node);
15454 16826207 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15455 1756 : ret = (int_operands
15456 1756 : ? note_integer_operands (ret)
15457 436 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15458 16824451 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15459 60300 : && !in_late_binary_op)
15460 60300 : ret = note_integer_operands (ret);
15461 16826207 : protected_set_expr_location (ret, location);
15462 :
15463 16826207 : if (instrument_expr != NULL)
15464 1420 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15465 : instrument_expr, ret);
15466 :
15467 16826207 : if (semantic_result_type)
15468 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15469 : semantic_result_type, ret);
15470 :
15471 : return ret;
15472 : }
15473 :
15474 :
15475 : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15476 : purpose. LOCATION is the source location for the expression. */
15477 :
15478 : tree
15479 4123139 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15480 : {
15481 4123139 : bool int_const, int_operands;
15482 :
15483 4123139 : switch (TREE_CODE (TREE_TYPE (expr)))
15484 : {
15485 72 : case ARRAY_TYPE:
15486 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15487 72 : return error_mark_node;
15488 :
15489 86 : case RECORD_TYPE:
15490 86 : error_at (location, "used struct type value where scalar is required");
15491 86 : return error_mark_node;
15492 :
15493 83 : case UNION_TYPE:
15494 83 : error_at (location, "used union type value where scalar is required");
15495 83 : return error_mark_node;
15496 :
15497 22 : case VOID_TYPE:
15498 22 : error_at (location, "void value not ignored as it ought to be");
15499 22 : return error_mark_node;
15500 :
15501 30456 : case POINTER_TYPE:
15502 30456 : if (reject_gcc_builtin (expr))
15503 3 : return error_mark_node;
15504 : break;
15505 :
15506 0 : case FUNCTION_TYPE:
15507 0 : gcc_unreachable ();
15508 :
15509 8 : case VECTOR_TYPE:
15510 8 : error_at (location, "used vector type where scalar is required");
15511 8 : return error_mark_node;
15512 :
15513 : default:
15514 : break;
15515 : }
15516 :
15517 : /* Conversion of a floating constant to boolean goes through here
15518 : and yields an integer constant expression. Otherwise, the result
15519 : is only an integer constant expression if the argument is. */
15520 895404 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15521 4122941 : || ((TREE_CODE (expr) == REAL_CST
15522 3227163 : || TREE_CODE (expr) == COMPLEX_CST)
15523 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15524 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15525 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15526 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15527 : == BOOLEAN_TYPE)))));
15528 4122865 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15529 895564 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15530 : {
15531 319 : expr = remove_c_maybe_const_expr (expr);
15532 319 : expr = build2 (NE_EXPR, type, expr,
15533 319 : convert (TREE_TYPE (expr), integer_zero_node));
15534 319 : expr = note_integer_operands (expr);
15535 : }
15536 : else
15537 : {
15538 : /* ??? Should we also give an error for vectors rather than leaving
15539 : those to give errors later? */
15540 4122546 : expr = c_common_truthvalue_conversion (location, expr);
15541 4122546 : expr = fold_convert_loc (location, type, expr);
15542 : }
15543 :
15544 4122865 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15545 : {
15546 76 : if (TREE_OVERFLOW (expr))
15547 : return expr;
15548 : else
15549 76 : return note_integer_operands (expr);
15550 : }
15551 4122789 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15552 754 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15553 : return expr;
15554 : }
15555 :
15556 :
15557 : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15558 : required. */
15559 :
15560 : tree
15561 119414687 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15562 : {
15563 119414687 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15564 : {
15565 699 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15566 : /* Executing a compound literal inside a function reinitializes
15567 : it. */
15568 699 : if (!TREE_STATIC (decl))
15569 433 : *se = true;
15570 699 : return decl;
15571 : }
15572 : else
15573 : return expr;
15574 : }
15575 :
15576 : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15577 : statement. LOC is the location of the construct. */
15578 :
15579 : tree
15580 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15581 : tree clauses)
15582 : {
15583 2154 : body = c_end_compound_stmt (loc, body, true);
15584 :
15585 2154 : tree stmt = make_node (code);
15586 2154 : TREE_TYPE (stmt) = void_type_node;
15587 2154 : OMP_BODY (stmt) = body;
15588 2154 : OMP_CLAUSES (stmt) = clauses;
15589 2154 : SET_EXPR_LOCATION (stmt, loc);
15590 :
15591 2154 : return add_stmt (stmt);
15592 : }
15593 :
15594 : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15595 : statement. LOC is the location of the OACC_DATA. */
15596 :
15597 : tree
15598 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15599 : {
15600 497 : tree stmt;
15601 :
15602 497 : block = c_end_compound_stmt (loc, block, true);
15603 :
15604 497 : stmt = make_node (OACC_DATA);
15605 497 : TREE_TYPE (stmt) = void_type_node;
15606 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15607 497 : OACC_DATA_BODY (stmt) = block;
15608 497 : SET_EXPR_LOCATION (stmt, loc);
15609 :
15610 497 : return add_stmt (stmt);
15611 : }
15612 :
15613 : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15614 : statement. LOC is the location of the OACC_HOST_DATA. */
15615 :
15616 : tree
15617 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15618 : {
15619 21 : tree stmt;
15620 :
15621 21 : block = c_end_compound_stmt (loc, block, true);
15622 :
15623 21 : stmt = make_node (OACC_HOST_DATA);
15624 21 : TREE_TYPE (stmt) = void_type_node;
15625 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15626 21 : OACC_HOST_DATA_BODY (stmt) = block;
15627 21 : SET_EXPR_LOCATION (stmt, loc);
15628 :
15629 21 : return add_stmt (stmt);
15630 : }
15631 :
15632 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15633 :
15634 : tree
15635 12003 : c_begin_omp_parallel (void)
15636 : {
15637 12003 : tree block;
15638 :
15639 12003 : keep_next_level ();
15640 12003 : block = c_begin_compound_stmt (true);
15641 :
15642 12003 : return block;
15643 : }
15644 :
15645 : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15646 : statement. LOC is the location of the OMP_PARALLEL. */
15647 :
15648 : tree
15649 5677 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15650 : {
15651 5677 : tree stmt;
15652 :
15653 5677 : block = c_end_compound_stmt (loc, block, true);
15654 :
15655 5677 : stmt = make_node (OMP_PARALLEL);
15656 5677 : TREE_TYPE (stmt) = void_type_node;
15657 5677 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15658 5677 : OMP_PARALLEL_BODY (stmt) = block;
15659 5677 : SET_EXPR_LOCATION (stmt, loc);
15660 :
15661 5677 : return add_stmt (stmt);
15662 : }
15663 :
15664 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15665 :
15666 : tree
15667 898 : c_begin_omp_task (void)
15668 : {
15669 898 : tree block;
15670 :
15671 898 : keep_next_level ();
15672 898 : block = c_begin_compound_stmt (true);
15673 :
15674 898 : return block;
15675 : }
15676 :
15677 : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15678 : statement. LOC is the location of the #pragma. */
15679 :
15680 : tree
15681 898 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15682 : {
15683 898 : tree stmt;
15684 :
15685 898 : block = c_end_compound_stmt (loc, block, true);
15686 :
15687 898 : stmt = make_node (OMP_TASK);
15688 898 : TREE_TYPE (stmt) = void_type_node;
15689 898 : OMP_TASK_CLAUSES (stmt) = clauses;
15690 898 : OMP_TASK_BODY (stmt) = block;
15691 898 : SET_EXPR_LOCATION (stmt, loc);
15692 :
15693 898 : return add_stmt (stmt);
15694 : }
15695 :
15696 : /* Generate GOMP_cancel call for #pragma omp cancel. */
15697 :
15698 : void
15699 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15700 : {
15701 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15702 213 : int mask = 0;
15703 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15704 : mask = 1;
15705 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15706 : mask = 2;
15707 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15708 : mask = 4;
15709 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15710 : mask = 8;
15711 : else
15712 : {
15713 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15714 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15715 : "clauses");
15716 0 : return;
15717 : }
15718 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15719 213 : if (ifc != NULL_TREE)
15720 : {
15721 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15722 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15723 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15724 : "expected %<cancel%> %<if%> clause modifier");
15725 : else
15726 : {
15727 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15728 29 : if (ifc2 != NULL_TREE)
15729 : {
15730 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15731 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15732 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15733 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15734 : "expected %<cancel%> %<if%> clause modifier");
15735 : }
15736 : }
15737 :
15738 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15739 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15740 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15741 : build_zero_cst (type));
15742 : }
15743 : else
15744 182 : ifc = boolean_true_node;
15745 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15746 213 : build_int_cst (integer_type_node, mask),
15747 : ifc);
15748 213 : add_stmt (stmt);
15749 : }
15750 :
15751 : /* Generate GOMP_cancellation_point call for
15752 : #pragma omp cancellation point. */
15753 :
15754 : void
15755 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15756 : {
15757 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15758 167 : int mask = 0;
15759 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15760 : mask = 1;
15761 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15762 : mask = 2;
15763 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15764 : mask = 4;
15765 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15766 : mask = 8;
15767 : else
15768 : {
15769 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15770 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15771 : "clauses");
15772 1 : return;
15773 : }
15774 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15775 166 : build_int_cst (integer_type_node, mask));
15776 166 : add_stmt (stmt);
15777 : }
15778 :
15779 : /* Helper function for handle_omp_array_sections. Called recursively
15780 : to handle multiple array-section-subscripts. C is the clause,
15781 : T current expression (initially OMP_CLAUSE_DECL), which is either
15782 : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15783 : expression if specified, TREE_VALUE length expression if specified,
15784 : TREE_CHAIN is what it has been specified after, or some decl.
15785 : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15786 : set to true if any of the array-section-subscript could have length
15787 : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15788 : first array-section-subscript which is known not to have length
15789 : of one. Given say:
15790 : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15791 : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15792 : all are or may have length of 1, array-section-subscript [:2] is the
15793 : first one known not to have length 1. For array-section-subscript
15794 : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15795 : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15796 : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15797 : case though, as some lengths could be zero. */
15798 :
15799 : static tree
15800 6396 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15801 : bool &maybe_zero_len, unsigned int &first_non_one,
15802 : enum c_omp_region_type ort)
15803 : {
15804 6396 : tree ret, low_bound, length, type;
15805 6396 : bool openacc = (ort & C_ORT_ACC) != 0;
15806 6396 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15807 : {
15808 2979 : if (error_operand_p (t))
15809 2 : return error_mark_node;
15810 2977 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15811 2977 : ret = t;
15812 2977 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15813 2878 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15814 5541 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15815 : {
15816 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15817 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15818 6 : return error_mark_node;
15819 : }
15820 2971 : if (!ai.check_clause (c))
15821 0 : return error_mark_node;
15822 2971 : else if (ai.component_access_p ()
15823 3272 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15824 15 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15825 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15826 301 : t = ai.get_root_term (true);
15827 : else
15828 2670 : t = ai.unconverted_ref_origin ();
15829 2971 : if (t == error_mark_node)
15830 : return error_mark_node;
15831 2971 : if (!VAR_P (t)
15832 742 : && (ort == C_ORT_ACC || !EXPR_P (t))
15833 739 : && TREE_CODE (t) != PARM_DECL)
15834 : {
15835 5 : if (DECL_P (t))
15836 5 : error_at (OMP_CLAUSE_LOCATION (c),
15837 : "%qD is not a variable in %qs clause", t,
15838 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15839 : else
15840 0 : error_at (OMP_CLAUSE_LOCATION (c),
15841 : "%qE is not a variable in %qs clause", t,
15842 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15843 5 : return error_mark_node;
15844 : }
15845 2966 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15846 2868 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15847 5521 : && TYPE_ATOMIC (TREE_TYPE (t)))
15848 : {
15849 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15850 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15851 0 : return error_mark_node;
15852 : }
15853 2966 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15854 2868 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15855 2555 : && VAR_P (t)
15856 4972 : && DECL_THREAD_LOCAL_P (t))
15857 : {
15858 3 : error_at (OMP_CLAUSE_LOCATION (c),
15859 : "%qD is threadprivate variable in %qs clause", t,
15860 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15861 3 : return error_mark_node;
15862 : }
15863 2963 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15864 2865 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15865 411 : && TYPE_ATOMIC (TREE_TYPE (t))
15866 2965 : && POINTER_TYPE_P (TREE_TYPE (t)))
15867 : {
15868 : /* If the array section is pointer based and the pointer
15869 : itself is _Atomic qualified, we need to atomically load
15870 : the pointer. */
15871 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15872 : ret, false, false);
15873 : }
15874 2963 : return ret;
15875 2977 : }
15876 :
15877 3417 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15878 : maybe_zero_len, first_non_one, ort);
15879 3417 : if (ret == error_mark_node || ret == NULL_TREE)
15880 : return ret;
15881 :
15882 3352 : type = TREE_TYPE (ret);
15883 3352 : low_bound = TREE_OPERAND (t, 1);
15884 3352 : length = TREE_OPERAND (t, 2);
15885 :
15886 3352 : if (low_bound == error_mark_node || length == error_mark_node)
15887 : return error_mark_node;
15888 :
15889 3352 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15890 : {
15891 13 : error_at (OMP_CLAUSE_LOCATION (c),
15892 : "low bound %qE of array section does not have integral type",
15893 : low_bound);
15894 13 : return error_mark_node;
15895 : }
15896 3339 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15897 : {
15898 12 : error_at (OMP_CLAUSE_LOCATION (c),
15899 : "length %qE of array section does not have integral type",
15900 : length);
15901 12 : return error_mark_node;
15902 : }
15903 3327 : if (low_bound
15904 2743 : && TREE_CODE (low_bound) == INTEGER_CST
15905 5781 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15906 2454 : > TYPE_PRECISION (sizetype))
15907 0 : low_bound = fold_convert (sizetype, low_bound);
15908 3327 : if (length
15909 3139 : && TREE_CODE (length) == INTEGER_CST
15910 5521 : && TYPE_PRECISION (TREE_TYPE (length))
15911 2194 : > TYPE_PRECISION (sizetype))
15912 0 : length = fold_convert (sizetype, length);
15913 3327 : if (low_bound == NULL_TREE)
15914 584 : low_bound = integer_zero_node;
15915 3327 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15916 3327 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15917 1937 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15918 : {
15919 10 : if (length != integer_one_node)
15920 : {
15921 6 : error_at (OMP_CLAUSE_LOCATION (c),
15922 : "expected single pointer in %qs clause",
15923 : user_omp_clause_code_name (c, openacc));
15924 6 : return error_mark_node;
15925 : }
15926 : }
15927 3321 : if (length != NULL_TREE)
15928 : {
15929 3137 : if (!integer_nonzerop (length))
15930 : {
15931 974 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15932 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15933 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15934 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15935 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15936 : {
15937 180 : if (integer_zerop (length))
15938 : {
15939 12 : error_at (OMP_CLAUSE_LOCATION (c),
15940 : "zero length array section in %qs clause",
15941 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15942 12 : return error_mark_node;
15943 : }
15944 : }
15945 : else
15946 794 : maybe_zero_len = true;
15947 : }
15948 3125 : if (first_non_one == types.length ()
15949 3125 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15950 1598 : first_non_one++;
15951 : }
15952 3309 : if (TREE_CODE (type) == ARRAY_TYPE)
15953 : {
15954 1496 : if (length == NULL_TREE
15955 1496 : && (TYPE_DOMAIN (type) == NULL_TREE
15956 168 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15957 : {
15958 8 : error_at (OMP_CLAUSE_LOCATION (c),
15959 : "for unknown bound array type length expression must "
15960 : "be specified");
15961 8 : return error_mark_node;
15962 : }
15963 1488 : if (TREE_CODE (low_bound) == INTEGER_CST
15964 1488 : && tree_int_cst_sgn (low_bound) == -1)
15965 : {
15966 35 : error_at (OMP_CLAUSE_LOCATION (c),
15967 : "negative low bound in array section in %qs clause",
15968 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15969 35 : return error_mark_node;
15970 : }
15971 1453 : if (length != NULL_TREE
15972 1297 : && TREE_CODE (length) == INTEGER_CST
15973 2310 : && tree_int_cst_sgn (length) == -1)
15974 : {
15975 35 : error_at (OMP_CLAUSE_LOCATION (c),
15976 : "negative length in array section in %qs clause",
15977 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15978 35 : return error_mark_node;
15979 : }
15980 1418 : if (TYPE_DOMAIN (type)
15981 1407 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
15982 2825 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
15983 : == INTEGER_CST)
15984 : {
15985 1061 : tree size
15986 1061 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
15987 1061 : size = size_binop (PLUS_EXPR, size, size_one_node);
15988 1061 : if (TREE_CODE (low_bound) == INTEGER_CST)
15989 : {
15990 874 : if (tree_int_cst_lt (size, low_bound))
15991 : {
15992 10 : error_at (OMP_CLAUSE_LOCATION (c),
15993 : "low bound %qE above array section size "
15994 : "in %qs clause", low_bound,
15995 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15996 10 : return error_mark_node;
15997 : }
15998 864 : if (tree_int_cst_equal (size, low_bound))
15999 : {
16000 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
16001 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16002 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16003 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16004 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16005 : {
16006 5 : error_at (OMP_CLAUSE_LOCATION (c),
16007 : "zero length array section in %qs clause",
16008 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16009 5 : return error_mark_node;
16010 : }
16011 0 : maybe_zero_len = true;
16012 : }
16013 859 : else if (length == NULL_TREE
16014 256 : && first_non_one == types.length ()
16015 914 : && tree_int_cst_equal
16016 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
16017 : low_bound))
16018 30 : first_non_one++;
16019 : }
16020 187 : else if (length == NULL_TREE)
16021 : {
16022 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16023 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16024 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16025 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16026 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16027 0 : maybe_zero_len = true;
16028 6 : if (first_non_one == types.length ())
16029 1 : first_non_one++;
16030 : }
16031 1046 : if (length && TREE_CODE (length) == INTEGER_CST)
16032 : {
16033 799 : if (tree_int_cst_lt (size, length))
16034 : {
16035 11 : error_at (OMP_CLAUSE_LOCATION (c),
16036 : "length %qE above array section size "
16037 : "in %qs clause", length,
16038 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16039 11 : return error_mark_node;
16040 : }
16041 788 : if (TREE_CODE (low_bound) == INTEGER_CST)
16042 : {
16043 688 : tree lbpluslen
16044 688 : = size_binop (PLUS_EXPR,
16045 : fold_convert (sizetype, low_bound),
16046 : fold_convert (sizetype, length));
16047 688 : if (TREE_CODE (lbpluslen) == INTEGER_CST
16048 688 : && tree_int_cst_lt (size, lbpluslen))
16049 : {
16050 10 : error_at (OMP_CLAUSE_LOCATION (c),
16051 : "high bound %qE above array section size "
16052 : "in %qs clause", lbpluslen,
16053 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16054 10 : return error_mark_node;
16055 : }
16056 : }
16057 : }
16058 : }
16059 357 : else if (length == NULL_TREE)
16060 : {
16061 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16062 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16063 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16064 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16065 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16066 0 : maybe_zero_len = true;
16067 20 : if (first_non_one == types.length ())
16068 10 : first_non_one++;
16069 : }
16070 :
16071 : /* For [lb:] we will need to evaluate lb more than once. */
16072 929 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16073 : {
16074 127 : tree lb = save_expr (low_bound);
16075 127 : if (lb != low_bound)
16076 : {
16077 2 : TREE_OPERAND (t, 1) = lb;
16078 2 : low_bound = lb;
16079 : }
16080 : }
16081 : }
16082 1813 : else if (TREE_CODE (type) == POINTER_TYPE)
16083 : {
16084 1802 : if (length == NULL_TREE)
16085 : {
16086 10 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
16087 8 : error_at (OMP_CLAUSE_LOCATION (c),
16088 : "for array function parameter length expression "
16089 : "must be specified");
16090 : else
16091 2 : error_at (OMP_CLAUSE_LOCATION (c),
16092 : "for pointer type length expression must be specified");
16093 10 : return error_mark_node;
16094 : }
16095 1792 : if (length != NULL_TREE
16096 1792 : && TREE_CODE (length) == INTEGER_CST
16097 1287 : && tree_int_cst_sgn (length) == -1)
16098 : {
16099 20 : error_at (OMP_CLAUSE_LOCATION (c),
16100 : "negative length in array section in %qs clause",
16101 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16102 20 : return error_mark_node;
16103 : }
16104 : /* If there is a pointer type anywhere but in the very first
16105 : array-section-subscript, the array section could be non-contiguous. */
16106 1772 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16107 1611 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16108 3354 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
16109 : {
16110 : /* If any prior dimension has a non-one length, then deem this
16111 : array section as non-contiguous. */
16112 42 : for (tree d = TREE_OPERAND (t, 0);
16113 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
16114 40 : d = TREE_OPERAND (d, 0))
16115 : {
16116 44 : tree d_length = TREE_OPERAND (d, 2);
16117 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
16118 : {
16119 4 : error_at (OMP_CLAUSE_LOCATION (c),
16120 : "array section is not contiguous in %qs clause",
16121 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16122 4 : return error_mark_node;
16123 : }
16124 : }
16125 : }
16126 : }
16127 : else
16128 : {
16129 11 : error_at (OMP_CLAUSE_LOCATION (c),
16130 : "%qE does not have pointer or array type", ret);
16131 11 : return error_mark_node;
16132 : }
16133 3150 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16134 2834 : types.safe_push (TREE_TYPE (ret));
16135 : /* We will need to evaluate lb more than once. */
16136 3150 : tree lb = save_expr (low_bound);
16137 3150 : if (lb != low_bound)
16138 : {
16139 273 : TREE_OPERAND (t, 1) = lb;
16140 273 : low_bound = lb;
16141 : }
16142 3150 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
16143 3150 : return ret;
16144 : }
16145 :
16146 : /* Handle array sections for clause C. */
16147 :
16148 : static bool
16149 2979 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
16150 : {
16151 2979 : bool maybe_zero_len = false;
16152 2979 : unsigned int first_non_one = 0;
16153 2979 : auto_vec<tree, 10> types;
16154 2979 : tree *tp = &OMP_CLAUSE_DECL (c);
16155 2979 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16156 2665 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16157 3078 : && OMP_ITERATOR_DECL_P (*tp))
16158 66 : tp = &TREE_VALUE (*tp);
16159 2979 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16160 : maybe_zero_len, first_non_one,
16161 : ort);
16162 2979 : if (first == error_mark_node)
16163 : return true;
16164 2761 : if (first == NULL_TREE)
16165 : return false;
16166 2761 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16167 2761 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16168 : {
16169 336 : tree t = *tp;
16170 336 : tree tem = NULL_TREE;
16171 : /* Need to evaluate side effects in the length expressions
16172 : if any. */
16173 336 : while (TREE_CODE (t) == TREE_LIST)
16174 : {
16175 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16176 : {
16177 0 : if (tem == NULL_TREE)
16178 : tem = TREE_VALUE (t);
16179 : else
16180 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16181 0 : TREE_VALUE (t), tem);
16182 : }
16183 0 : t = TREE_CHAIN (t);
16184 : }
16185 336 : if (tem)
16186 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16187 336 : first = c_fully_fold (first, false, NULL, true);
16188 336 : *tp = first;
16189 : }
16190 : else
16191 : {
16192 2425 : unsigned int num = types.length (), i;
16193 2425 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16194 2425 : tree condition = NULL_TREE;
16195 :
16196 2425 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16197 3 : maybe_zero_len = true;
16198 :
16199 5050 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16200 2625 : t = TREE_OPERAND (t, 0))
16201 : {
16202 2640 : tree low_bound = TREE_OPERAND (t, 1);
16203 2640 : tree length = TREE_OPERAND (t, 2);
16204 :
16205 2640 : i--;
16206 2640 : if (low_bound
16207 2176 : && TREE_CODE (low_bound) == INTEGER_CST
16208 4630 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16209 1990 : > TYPE_PRECISION (sizetype))
16210 0 : low_bound = fold_convert (sizetype, low_bound);
16211 2640 : if (length
16212 2548 : && TREE_CODE (length) == INTEGER_CST
16213 4261 : && TYPE_PRECISION (TREE_TYPE (length))
16214 1621 : > TYPE_PRECISION (sizetype))
16215 0 : length = fold_convert (sizetype, length);
16216 2640 : if (low_bound == NULL_TREE)
16217 464 : low_bound = integer_zero_node;
16218 2640 : if (!maybe_zero_len && i > first_non_one)
16219 : {
16220 109 : if (integer_nonzerop (low_bound))
16221 6 : goto do_warn_noncontiguous;
16222 103 : if (length != NULL_TREE
16223 50 : && TREE_CODE (length) == INTEGER_CST
16224 50 : && TYPE_DOMAIN (types[i])
16225 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16226 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16227 : == INTEGER_CST)
16228 : {
16229 50 : tree size;
16230 50 : size = size_binop (PLUS_EXPR,
16231 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16232 : size_one_node);
16233 50 : if (!tree_int_cst_equal (length, size))
16234 : {
16235 7 : do_warn_noncontiguous:
16236 26 : error_at (OMP_CLAUSE_LOCATION (c),
16237 : "array section is not contiguous in %qs "
16238 : "clause",
16239 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16240 2425 : return true;
16241 : }
16242 : }
16243 96 : if (length != NULL_TREE
16244 96 : && TREE_SIDE_EFFECTS (length))
16245 : {
16246 0 : if (side_effects == NULL_TREE)
16247 : side_effects = length;
16248 : else
16249 0 : side_effects = build2 (COMPOUND_EXPR,
16250 0 : TREE_TYPE (side_effects),
16251 : length, side_effects);
16252 : }
16253 : }
16254 : else
16255 : {
16256 2531 : tree l;
16257 :
16258 2531 : if (i > first_non_one
16259 2531 : && ((length && integer_nonzerop (length))
16260 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16261 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16262 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16263 0 : continue;
16264 2531 : if (length)
16265 2498 : l = fold_convert (sizetype, length);
16266 : else
16267 : {
16268 33 : l = size_binop (PLUS_EXPR,
16269 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16270 : size_one_node);
16271 33 : l = size_binop (MINUS_EXPR, l,
16272 : fold_convert (sizetype, low_bound));
16273 : }
16274 2531 : if (i > first_non_one)
16275 : {
16276 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16277 : size_zero_node);
16278 0 : if (condition == NULL_TREE)
16279 : condition = l;
16280 : else
16281 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16282 : l, condition);
16283 : }
16284 2531 : else if (size == NULL_TREE)
16285 : {
16286 2412 : size = size_in_bytes (TREE_TYPE (types[i]));
16287 2412 : tree eltype = TREE_TYPE (types[num - 1]);
16288 2432 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16289 20 : eltype = TREE_TYPE (eltype);
16290 2412 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16291 2177 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16292 4329 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16293 : {
16294 539 : if (integer_zerop (size)
16295 1076 : || integer_zerop (size_in_bytes (eltype)))
16296 : {
16297 4 : error_at (OMP_CLAUSE_LOCATION (c),
16298 : "zero length array section in %qs clause",
16299 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16300 2 : return error_mark_node;
16301 : }
16302 537 : size = size_binop (EXACT_DIV_EXPR, size,
16303 : size_in_bytes (eltype));
16304 : }
16305 2410 : size = size_binop (MULT_EXPR, size, l);
16306 2410 : if (condition)
16307 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16308 : size, size_zero_node);
16309 : }
16310 : else
16311 119 : size = size_binop (MULT_EXPR, size, l);
16312 : }
16313 : }
16314 2410 : if (side_effects)
16315 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16316 2410 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16317 2177 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16318 4327 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16319 : {
16320 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16321 537 : size = c_fully_fold (size, false, NULL);
16322 537 : size = save_expr (size);
16323 537 : tree index_type = build_index_type (size);
16324 537 : tree eltype = TREE_TYPE (first);
16325 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16326 12 : eltype = TREE_TYPE (eltype);
16327 537 : tree type = c_build_array_type (eltype, index_type);
16328 537 : tree ptype = c_build_pointer_type (eltype);
16329 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16330 296 : t = build_fold_addr_expr (t);
16331 537 : tree t2 = build_fold_addr_expr (first);
16332 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16333 : ptrdiff_type_node, t2);
16334 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16335 : ptrdiff_type_node, t2,
16336 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16337 : ptrdiff_type_node, t));
16338 537 : t2 = c_fully_fold (t2, false, NULL);
16339 537 : if (tree_fits_shwi_p (t2))
16340 398 : t = build2 (MEM_REF, type, t,
16341 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16342 : else
16343 : {
16344 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16345 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16346 139 : TREE_TYPE (t), t, t2);
16347 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16348 : }
16349 537 : OMP_CLAUSE_DECL (c) = t;
16350 537 : return false;
16351 : }
16352 1873 : first = c_fully_fold (first, false, NULL);
16353 1873 : OMP_CLAUSE_DECL (c) = first;
16354 1873 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16355 : return false;
16356 : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16357 1862 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16358 1862 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16359 1722 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16360 1720 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16361 : {
16362 1858 : if (size)
16363 1858 : size = c_fully_fold (size, false, NULL);
16364 1858 : OMP_CLAUSE_SIZE (c) = size;
16365 : }
16366 :
16367 1862 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16368 : return false;
16369 :
16370 1724 : auto_vec<omp_addr_token *, 10> addr_tokens;
16371 :
16372 1724 : if (!omp_parse_expr (addr_tokens, first))
16373 1724 : return true;
16374 :
16375 1724 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16376 :
16377 1724 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16378 1724 : if (nc != error_mark_node)
16379 : {
16380 1724 : using namespace omp_addr_tokenizer;
16381 :
16382 1724 : if (ai.maybe_zero_length_array_section (c))
16383 1720 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16384 :
16385 : /* !!! If we're accessing a base decl via chained access
16386 : methods (e.g. multiple indirections), duplicate clause
16387 : detection won't work properly. Skip it in that case. */
16388 1724 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16389 1437 : || addr_tokens[0]->type == ARRAY_BASE)
16390 1724 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16391 1724 : && addr_tokens[1]->type == ACCESS_METHOD
16392 3448 : && omp_access_chain_p (addr_tokens, 1))
16393 34 : c = nc;
16394 :
16395 1724 : return false;
16396 : }
16397 : }
16398 : return false;
16399 2979 : }
16400 :
16401 : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16402 : an inline call. But, remap
16403 : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16404 : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16405 :
16406 : static tree
16407 510 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16408 : tree decl, tree placeholder)
16409 : {
16410 510 : copy_body_data id;
16411 510 : hash_map<tree, tree> decl_map;
16412 :
16413 510 : decl_map.put (omp_decl1, placeholder);
16414 510 : decl_map.put (omp_decl2, decl);
16415 510 : memset (&id, 0, sizeof (id));
16416 510 : id.src_fn = DECL_CONTEXT (omp_decl1);
16417 510 : id.dst_fn = current_function_decl;
16418 510 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16419 510 : id.decl_map = &decl_map;
16420 :
16421 510 : id.copy_decl = copy_decl_no_change;
16422 510 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16423 510 : id.transform_new_cfg = true;
16424 510 : id.transform_return_to_modify = false;
16425 510 : id.eh_lp_nr = 0;
16426 510 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16427 510 : return stmt;
16428 510 : }
16429 :
16430 : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16431 : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16432 :
16433 : static tree
16434 1123 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16435 : {
16436 1123 : if (*tp == (tree) data)
16437 28 : return *tp;
16438 : return NULL_TREE;
16439 : }
16440 :
16441 : /* Similarly, but also walk aggregate fields. */
16442 :
16443 : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16444 :
16445 : static tree
16446 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16447 : {
16448 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16449 : return *tp;
16450 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16451 : {
16452 2 : tree field;
16453 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16454 :
16455 2 : for (field = TYPE_FIELDS (*tp); field;
16456 0 : field = DECL_CHAIN (field))
16457 2 : if (TREE_CODE (field) == FIELD_DECL)
16458 : {
16459 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16460 : c_find_omp_var_r, data, pset);
16461 2 : if (ret)
16462 : return ret;
16463 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16464 2 : if (ret)
16465 : return ret;
16466 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16467 : pset);
16468 2 : if (ret)
16469 : return ret;
16470 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16471 2 : if (ret)
16472 : return ret;
16473 : }
16474 : }
16475 278 : else if (INTEGRAL_TYPE_P (*tp))
16476 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16477 : ((struct c_find_omp_var_s *) data)->pset);
16478 : return NULL_TREE;
16479 : }
16480 :
16481 : /* Finish OpenMP iterators ITER. Return true if they are errorneous
16482 : and clauses containing them should be removed. */
16483 :
16484 : static bool
16485 143 : c_omp_finish_iterators (tree iter)
16486 : {
16487 143 : bool ret = false;
16488 325 : for (tree it = iter; it; it = TREE_CHAIN (it))
16489 : {
16490 182 : tree var = TREE_VEC_ELT (it, 0);
16491 182 : tree begin = TREE_VEC_ELT (it, 1);
16492 182 : tree end = TREE_VEC_ELT (it, 2);
16493 182 : tree step = TREE_VEC_ELT (it, 3);
16494 182 : tree orig_step;
16495 182 : tree type = TREE_TYPE (var);
16496 182 : location_t loc = DECL_SOURCE_LOCATION (var);
16497 182 : if (type == error_mark_node)
16498 : {
16499 0 : ret = true;
16500 34 : continue;
16501 : }
16502 182 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16503 : {
16504 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16505 : var);
16506 6 : ret = true;
16507 6 : continue;
16508 : }
16509 176 : else if (TYPE_ATOMIC (type))
16510 : {
16511 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16512 2 : ret = true;
16513 2 : continue;
16514 : }
16515 174 : else if (TYPE_READONLY (type))
16516 : {
16517 4 : error_at (loc, "iterator %qD has const qualified type", var);
16518 4 : ret = true;
16519 4 : continue;
16520 : }
16521 170 : else if (step == error_mark_node
16522 170 : || TREE_TYPE (step) == error_mark_node)
16523 : {
16524 0 : ret = true;
16525 0 : continue;
16526 : }
16527 170 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16528 : {
16529 6 : error_at (EXPR_LOC_OR_LOC (step, loc),
16530 : "iterator step with non-integral type");
16531 4 : ret = true;
16532 4 : continue;
16533 : }
16534 166 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16535 166 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16536 166 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16537 166 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16538 166 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16539 166 : if (POINTER_TYPE_P (type))
16540 : {
16541 14 : begin = save_expr (begin);
16542 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16543 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16544 : fold_convert (sizetype, step),
16545 : fold_convert (sizetype, begin));
16546 14 : step = fold_convert (ssizetype, step);
16547 : }
16548 166 : if (integer_zerop (step))
16549 : {
16550 6 : error_at (loc, "iterator %qD has zero step", var);
16551 6 : ret = true;
16552 6 : continue;
16553 : }
16554 :
16555 160 : if (begin == error_mark_node
16556 158 : || end == error_mark_node
16557 156 : || step == error_mark_node
16558 156 : || orig_step == error_mark_node)
16559 : {
16560 4 : ret = true;
16561 4 : continue;
16562 : }
16563 156 : hash_set<tree> pset;
16564 156 : tree it2;
16565 197 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16566 : {
16567 49 : tree var2 = TREE_VEC_ELT (it2, 0);
16568 49 : tree begin2 = TREE_VEC_ELT (it2, 1);
16569 49 : tree end2 = TREE_VEC_ELT (it2, 2);
16570 49 : tree step2 = TREE_VEC_ELT (it2, 3);
16571 49 : tree type2 = TREE_TYPE (var2);
16572 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16573 49 : struct c_find_omp_var_s data = { var, &pset };
16574 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16575 : {
16576 2 : error_at (loc2,
16577 : "type of iterator %qD refers to outer iterator %qD",
16578 : var2, var);
16579 10 : break;
16580 : }
16581 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16582 : {
16583 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16584 : "begin expression refers to outer iterator %qD", var);
16585 2 : break;
16586 : }
16587 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16588 : {
16589 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16590 : "end expression refers to outer iterator %qD", var);
16591 2 : break;
16592 : }
16593 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16594 : {
16595 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16596 : "step expression refers to outer iterator %qD", var);
16597 2 : break;
16598 : }
16599 : }
16600 156 : if (it2)
16601 : {
16602 8 : ret = true;
16603 8 : continue;
16604 : }
16605 148 : TREE_VEC_ELT (it, 1) = begin;
16606 148 : TREE_VEC_ELT (it, 2) = end;
16607 148 : TREE_VEC_ELT (it, 3) = step;
16608 148 : TREE_VEC_ELT (it, 4) = orig_step;
16609 156 : }
16610 143 : return ret;
16611 : }
16612 :
16613 : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16614 : Return true if an error has been detected. */
16615 :
16616 : static bool
16617 9546 : c_oacc_check_attachments (tree c)
16618 : {
16619 9546 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16620 : return false;
16621 :
16622 : /* OpenACC attach / detach clauses must be pointers. */
16623 6551 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16624 6551 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16625 : {
16626 71 : tree t = OMP_CLAUSE_DECL (c);
16627 :
16628 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16629 6 : t = TREE_OPERAND (t, 0);
16630 :
16631 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16632 : {
16633 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16634 : user_omp_clause_code_name (c, true));
16635 6 : return true;
16636 : }
16637 : }
16638 :
16639 : return false;
16640 : }
16641 :
16642 : /* For all elements of CLAUSES, validate them against their constraints.
16643 : Remove any elements from the list that are invalid. */
16644 :
16645 : tree
16646 29480 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16647 : {
16648 29480 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16649 29480 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16650 29480 : bitmap_head oacc_reduction_head, is_on_device_head;
16651 29480 : tree c, t, type, *pc;
16652 29480 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16653 29480 : bool branch_seen = false;
16654 29480 : bool copyprivate_seen = false;
16655 29480 : bool mergeable_seen = false;
16656 29480 : tree *detach_seen = NULL;
16657 29480 : bool linear_variable_step_check = false;
16658 29480 : tree *nowait_clause = NULL;
16659 29480 : tree *grainsize_seen = NULL;
16660 29480 : bool num_tasks_seen = false;
16661 29480 : tree ordered_clause = NULL_TREE;
16662 29480 : tree schedule_clause = NULL_TREE;
16663 29480 : bool oacc_async = false;
16664 29480 : tree last_iterators = NULL_TREE;
16665 29480 : bool last_iterators_remove = false;
16666 29480 : tree *nogroup_seen = NULL;
16667 29480 : tree *order_clause = NULL;
16668 : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16669 : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16670 29480 : int reduction_seen = 0;
16671 29480 : bool allocate_seen = false;
16672 29480 : bool implicit_moved = false;
16673 29480 : bool target_in_reduction_seen = false;
16674 29480 : tree *full_seen = NULL;
16675 29480 : bool partial_seen = false;
16676 29480 : bool openacc = (ort & C_ORT_ACC) != 0;
16677 29480 : bool init_seen = false;
16678 29480 : bool init_use_destroy_seen = false;
16679 29480 : tree init_no_targetsync_clause = NULL_TREE;
16680 29480 : tree depend_clause = NULL_TREE;
16681 :
16682 29480 : bitmap_obstack_initialize (NULL);
16683 29480 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16684 29480 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16685 29480 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16686 29480 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16687 : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16688 29480 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16689 29480 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16690 29480 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16691 : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16692 : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16693 29480 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16694 29480 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16695 :
16696 29480 : if (openacc)
16697 12269 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16698 7258 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16699 : {
16700 : oacc_async = true;
16701 : break;
16702 : }
16703 :
16704 29480 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16705 :
16706 77476 : for (pc = &clauses, c = clauses; c ; c = *pc)
16707 : {
16708 47996 : bool remove = false;
16709 47996 : bool need_complete = false;
16710 47996 : bool need_implicitly_determined = false;
16711 :
16712 : /* We've reached the end of a list of expanded nodes. Reset the group
16713 : start pointer. */
16714 47996 : if (c == grp_sentinel)
16715 : {
16716 2512 : if (grp_start_p
16717 2512 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16718 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16719 22 : gc = OMP_CLAUSE_CHAIN (gc))
16720 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16721 : grp_start_p = NULL;
16722 : }
16723 :
16724 47996 : switch (OMP_CLAUSE_CODE (c))
16725 : {
16726 1139 : case OMP_CLAUSE_SHARED:
16727 1139 : need_implicitly_determined = true;
16728 1139 : goto check_dup_generic;
16729 :
16730 847 : case OMP_CLAUSE_PRIVATE:
16731 847 : need_complete = true;
16732 847 : need_implicitly_determined = true;
16733 847 : goto check_dup_generic;
16734 :
16735 3519 : case OMP_CLAUSE_REDUCTION:
16736 3519 : if (reduction_seen == 0)
16737 2920 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16738 599 : else if (reduction_seen != -2
16739 1198 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16740 599 : ? -1 : 1))
16741 : {
16742 2 : error_at (OMP_CLAUSE_LOCATION (c),
16743 : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16744 : "on the same construct");
16745 2 : reduction_seen = -2;
16746 : }
16747 : /* FALLTHRU */
16748 4183 : case OMP_CLAUSE_IN_REDUCTION:
16749 4183 : case OMP_CLAUSE_TASK_REDUCTION:
16750 4183 : need_implicitly_determined = true;
16751 4183 : t = OMP_CLAUSE_DECL (c);
16752 4183 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16753 : {
16754 553 : if (handle_omp_array_sections (c, ort))
16755 : {
16756 : remove = true;
16757 : break;
16758 : }
16759 :
16760 537 : t = OMP_CLAUSE_DECL (c);
16761 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16762 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16763 : {
16764 1 : error_at (OMP_CLAUSE_LOCATION (c),
16765 : "%<inscan%> %<reduction%> clause with array "
16766 : "section");
16767 1 : remove = true;
16768 1 : break;
16769 : }
16770 : }
16771 4166 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16772 4166 : if (t == error_mark_node)
16773 : {
16774 : remove = true;
16775 : break;
16776 : }
16777 4164 : if (oacc_async)
16778 3 : c_mark_addressable (t);
16779 4164 : type = TREE_TYPE (t);
16780 4164 : if (TREE_CODE (t) == MEM_REF)
16781 536 : type = TREE_TYPE (type);
16782 4164 : if (TREE_CODE (type) == ARRAY_TYPE)
16783 : {
16784 74 : tree oatype = type;
16785 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16786 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16787 74 : type = TREE_TYPE (type);
16788 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16789 : {
16790 1 : error_at (OMP_CLAUSE_LOCATION (c),
16791 : "%qD in %<reduction%> clause is a zero size array",
16792 : t);
16793 1 : remove = true;
16794 1 : break;
16795 : }
16796 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16797 : TYPE_SIZE_UNIT (type));
16798 73 : if (integer_zerop (size))
16799 : {
16800 1 : error_at (OMP_CLAUSE_LOCATION (c),
16801 : "%qD in %<reduction%> clause is a zero size array",
16802 : t);
16803 1 : remove = true;
16804 1 : break;
16805 : }
16806 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16807 72 : size = save_expr (size);
16808 72 : tree index_type = build_index_type (size);
16809 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16810 : index_type);
16811 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16812 72 : tree ptype = c_build_pointer_type (type);
16813 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16814 72 : t = build_fold_addr_expr (t);
16815 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16816 72 : OMP_CLAUSE_DECL (c) = t;
16817 : }
16818 4162 : if (TYPE_ATOMIC (type))
16819 : {
16820 3 : error_at (OMP_CLAUSE_LOCATION (c),
16821 : "%<_Atomic%> %qE in %<reduction%> clause", t);
16822 3 : remove = true;
16823 3 : break;
16824 : }
16825 4159 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16826 4159 : || OMP_CLAUSE_REDUCTION_TASK (c))
16827 : {
16828 : /* Disallow zero sized or potentially zero sized task
16829 : reductions. */
16830 871 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16831 : {
16832 6 : error_at (OMP_CLAUSE_LOCATION (c),
16833 : "zero sized type %qT in %qs clause", type,
16834 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16835 3 : remove = true;
16836 3 : break;
16837 : }
16838 868 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16839 : {
16840 0 : error_at (OMP_CLAUSE_LOCATION (c),
16841 : "variable sized type %qT in %qs clause", type,
16842 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16843 0 : remove = true;
16844 0 : break;
16845 : }
16846 : }
16847 4156 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16848 4156 : && (FLOAT_TYPE_P (type)
16849 3604 : || TREE_CODE (type) == COMPLEX_TYPE))
16850 : {
16851 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16852 340 : const char *r_name = NULL;
16853 :
16854 340 : switch (r_code)
16855 : {
16856 : case PLUS_EXPR:
16857 : case MULT_EXPR:
16858 : case MINUS_EXPR:
16859 : case TRUTH_ANDIF_EXPR:
16860 : case TRUTH_ORIF_EXPR:
16861 : break;
16862 13 : case MIN_EXPR:
16863 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16864 : r_name = "min";
16865 : break;
16866 38 : case MAX_EXPR:
16867 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16868 : r_name = "max";
16869 : break;
16870 3 : case BIT_AND_EXPR:
16871 3 : r_name = "&";
16872 3 : break;
16873 2 : case BIT_XOR_EXPR:
16874 2 : r_name = "^";
16875 2 : break;
16876 : case BIT_IOR_EXPR:
16877 : r_name = "|";
16878 : break;
16879 0 : default:
16880 0 : gcc_unreachable ();
16881 : }
16882 5 : if (r_name)
16883 : {
16884 12 : error_at (OMP_CLAUSE_LOCATION (c),
16885 : "%qE has invalid type for %<reduction(%s)%>",
16886 : t, r_name);
16887 12 : remove = true;
16888 12 : break;
16889 : }
16890 : }
16891 3816 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16892 : {
16893 2 : error_at (OMP_CLAUSE_LOCATION (c),
16894 : "user defined reduction not found for %qE", t);
16895 2 : remove = true;
16896 2 : break;
16897 : }
16898 3814 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16899 : {
16900 277 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16901 277 : type = TYPE_MAIN_VARIANT (type);
16902 277 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16903 : VAR_DECL, NULL_TREE, type);
16904 277 : tree decl_placeholder = NULL_TREE;
16905 277 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16906 277 : DECL_ARTIFICIAL (placeholder) = 1;
16907 277 : DECL_IGNORED_P (placeholder) = 1;
16908 277 : if (TREE_CODE (t) == MEM_REF)
16909 : {
16910 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16911 : VAR_DECL, NULL_TREE, type);
16912 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16913 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16914 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16915 : }
16916 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16917 42 : c_mark_addressable (placeholder);
16918 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16919 76 : c_mark_addressable (decl_placeholder ? decl_placeholder
16920 34 : : OMP_CLAUSE_DECL (c));
16921 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16922 775 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16923 277 : TREE_VEC_ELT (list, 0),
16924 277 : TREE_VEC_ELT (list, 1),
16925 : decl_placeholder ? decl_placeholder
16926 221 : : OMP_CLAUSE_DECL (c), placeholder);
16927 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16928 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16929 : void_type_node, NULL_TREE,
16930 277 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16931 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16932 277 : if (TREE_VEC_LENGTH (list) == 6)
16933 : {
16934 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16935 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16936 33 : : OMP_CLAUSE_DECL (c));
16937 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16938 27 : c_mark_addressable (placeholder);
16939 233 : tree init = TREE_VEC_ELT (list, 5);
16940 233 : if (init == error_mark_node)
16941 197 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16942 233 : OMP_CLAUSE_REDUCTION_INIT (c)
16943 651 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16944 233 : TREE_VEC_ELT (list, 3),
16945 : decl_placeholder ? decl_placeholder
16946 185 : : OMP_CLAUSE_DECL (c), placeholder);
16947 233 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16948 : {
16949 197 : tree v = decl_placeholder ? decl_placeholder : t;
16950 197 : OMP_CLAUSE_REDUCTION_INIT (c)
16951 394 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16952 197 : OMP_CLAUSE_REDUCTION_INIT (c));
16953 : }
16954 233 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16955 : c_find_omp_placeholder_r,
16956 : placeholder, NULL))
16957 28 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
16958 : }
16959 : else
16960 : {
16961 44 : tree init;
16962 44 : tree v = decl_placeholder ? decl_placeholder : t;
16963 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
16964 34 : init = build_constructor (TREE_TYPE (v), NULL);
16965 : else
16966 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
16967 44 : OMP_CLAUSE_REDUCTION_INIT (c)
16968 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
16969 : }
16970 277 : OMP_CLAUSE_REDUCTION_INIT (c)
16971 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16972 : void_type_node, NULL_TREE,
16973 277 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
16974 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
16975 : }
16976 4142 : if (TREE_CODE (t) == MEM_REF)
16977 : {
16978 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
16979 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
16980 : != INTEGER_CST)
16981 : {
16982 0 : sorry ("variable length element type in array "
16983 : "%<reduction%> clause");
16984 0 : remove = true;
16985 0 : break;
16986 : }
16987 607 : t = TREE_OPERAND (t, 0);
16988 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16989 139 : t = TREE_OPERAND (t, 0);
16990 607 : if (TREE_CODE (t) == ADDR_EXPR)
16991 367 : t = TREE_OPERAND (t, 0);
16992 : }
16993 4142 : goto check_dup_generic_t;
16994 :
16995 24 : case OMP_CLAUSE_COPYPRIVATE:
16996 24 : copyprivate_seen = true;
16997 24 : if (nowait_clause)
16998 : {
16999 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
17000 : "%<nowait%> clause must not be used together "
17001 : "with %<copyprivate%> clause");
17002 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
17003 1 : nowait_clause = NULL;
17004 : }
17005 24 : goto check_dup_generic;
17006 :
17007 99 : case OMP_CLAUSE_COPYIN:
17008 99 : t = OMP_CLAUSE_DECL (c);
17009 99 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
17010 : {
17011 5 : error_at (OMP_CLAUSE_LOCATION (c),
17012 : "%qE must be %<threadprivate%> for %<copyin%>", t);
17013 5 : remove = true;
17014 5 : break;
17015 : }
17016 94 : goto check_dup_generic;
17017 :
17018 480 : case OMP_CLAUSE_LINEAR:
17019 480 : if (ort != C_ORT_OMP_DECLARE_SIMD)
17020 327 : need_implicitly_determined = true;
17021 480 : t = OMP_CLAUSE_DECL (c);
17022 480 : if (ort != C_ORT_OMP_DECLARE_SIMD
17023 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
17024 485 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
17025 : {
17026 5 : error_at (OMP_CLAUSE_LOCATION (c),
17027 : "modifier should not be specified in %<linear%> "
17028 : "clause on %<simd%> or %<for%> constructs when not "
17029 : "using OpenMP 5.2 modifiers");
17030 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
17031 : }
17032 960 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
17033 503 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17034 : {
17035 1 : error_at (OMP_CLAUSE_LOCATION (c),
17036 : "linear clause applied to non-integral non-pointer "
17037 1 : "variable with type %qT", TREE_TYPE (t));
17038 1 : remove = true;
17039 1 : break;
17040 : }
17041 479 : if (TYPE_ATOMIC (TREE_TYPE (t)))
17042 : {
17043 3 : error_at (OMP_CLAUSE_LOCATION (c),
17044 : "%<_Atomic%> %qD in %<linear%> clause", t);
17045 3 : remove = true;
17046 3 : break;
17047 : }
17048 476 : if (ort == C_ORT_OMP_DECLARE_SIMD)
17049 : {
17050 152 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17051 152 : if (TREE_CODE (s) == PARM_DECL)
17052 : {
17053 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
17054 : /* map_head bitmap is used as uniform_head if
17055 : declare_simd. */
17056 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
17057 5 : linear_variable_step_check = true;
17058 9 : goto check_dup_generic;
17059 : }
17060 143 : if (TREE_CODE (s) != INTEGER_CST)
17061 : {
17062 7 : error_at (OMP_CLAUSE_LOCATION (c),
17063 : "%<linear%> clause step %qE is neither constant "
17064 : "nor a parameter", s);
17065 7 : remove = true;
17066 7 : break;
17067 : }
17068 : }
17069 460 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
17070 : {
17071 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17072 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
17073 20 : OMP_CLAUSE_DECL (c), s);
17074 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17075 : sizetype, fold_convert (sizetype, s),
17076 20 : fold_convert
17077 : (sizetype, OMP_CLAUSE_DECL (c)));
17078 20 : if (s == error_mark_node)
17079 0 : s = size_one_node;
17080 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
17081 : }
17082 : else
17083 440 : OMP_CLAUSE_LINEAR_STEP (c)
17084 880 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
17085 460 : goto check_dup_generic;
17086 :
17087 2887 : check_dup_generic:
17088 2887 : t = OMP_CLAUSE_DECL (c);
17089 7118 : check_dup_generic_t:
17090 7118 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17091 : {
17092 4 : error_at (OMP_CLAUSE_LOCATION (c),
17093 : "%qE is not a variable in clause %qs", t,
17094 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17095 2 : remove = true;
17096 : }
17097 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
17098 6077 : || (ort == C_ORT_OMP
17099 5358 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17100 5347 : || (OMP_CLAUSE_CODE (c)
17101 : == OMP_CLAUSE_USE_DEVICE_ADDR)))
17102 13146 : || (ort == C_ORT_OMP_TARGET
17103 347 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
17104 : {
17105 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
17106 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17107 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
17108 : {
17109 2 : error_at (OMP_CLAUSE_LOCATION (c),
17110 : "%qD appears more than once in data-sharing "
17111 : "clauses", t);
17112 2 : remove = true;
17113 2 : break;
17114 : }
17115 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
17116 108 : target_in_reduction_seen = true;
17117 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17118 : {
17119 4 : error_at (OMP_CLAUSE_LOCATION (c),
17120 : openacc
17121 : ? "%qD appears more than once in reduction clauses"
17122 : : "%qD appears more than once in data clauses",
17123 : t);
17124 2 : remove = true;
17125 : }
17126 : else
17127 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17128 : }
17129 5920 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17130 5897 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17131 5896 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
17132 11816 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17133 : {
17134 24 : error_at (OMP_CLAUSE_LOCATION (c),
17135 : "%qE appears more than once in data clauses", t);
17136 24 : remove = true;
17137 : }
17138 5896 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17139 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
17140 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17141 1047 : && bitmap_bit_p (&map_head, DECL_UID (t)))
17142 : {
17143 3 : if (openacc)
17144 0 : error_at (OMP_CLAUSE_LOCATION (c),
17145 : "%qD appears more than once in data clauses", t);
17146 : else
17147 3 : error_at (OMP_CLAUSE_LOCATION (c),
17148 : "%qD appears both in data and map clauses", t);
17149 : remove = true;
17150 : }
17151 : else
17152 5893 : bitmap_set_bit (&generic_head, DECL_UID (t));
17153 : break;
17154 :
17155 1333 : case OMP_CLAUSE_FIRSTPRIVATE:
17156 1333 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
17157 : {
17158 186 : move_implicit:
17159 186 : implicit_moved = true;
17160 : /* Move firstprivate and map clauses with
17161 : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17162 : clauses chain. */
17163 186 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17164 186 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17165 1072 : while (*pc1)
17166 886 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17167 886 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17168 : {
17169 113 : *pc3 = *pc1;
17170 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17171 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17172 : }
17173 773 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17174 773 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17175 : {
17176 164 : *pc2 = *pc1;
17177 164 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17178 164 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17179 : }
17180 : else
17181 609 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17182 186 : *pc3 = NULL;
17183 186 : *pc2 = cl2;
17184 186 : *pc1 = cl1;
17185 186 : continue;
17186 186 : }
17187 1238 : t = OMP_CLAUSE_DECL (c);
17188 1238 : need_complete = true;
17189 1238 : need_implicitly_determined = true;
17190 1238 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17191 : {
17192 1 : error_at (OMP_CLAUSE_LOCATION (c),
17193 : "%qE is not a variable in clause %<firstprivate%>", t);
17194 1 : remove = true;
17195 : }
17196 1237 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17197 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17198 1343 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17199 : remove = true;
17200 1237 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17201 1235 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17202 2471 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17203 : {
17204 4 : error_at (OMP_CLAUSE_LOCATION (c),
17205 : "%qE appears more than once in data clauses", t);
17206 4 : remove = true;
17207 : }
17208 1233 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17209 1233 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17210 : {
17211 7 : if (openacc)
17212 0 : error_at (OMP_CLAUSE_LOCATION (c),
17213 : "%qD appears more than once in data clauses", t);
17214 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17215 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17216 : /* Silently drop the clause. */;
17217 : else
17218 6 : error_at (OMP_CLAUSE_LOCATION (c),
17219 : "%qD appears both in data and map clauses", t);
17220 : remove = true;
17221 : }
17222 : else
17223 1226 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17224 : break;
17225 :
17226 1523 : case OMP_CLAUSE_LASTPRIVATE:
17227 1523 : t = OMP_CLAUSE_DECL (c);
17228 1523 : need_complete = true;
17229 1523 : need_implicitly_determined = true;
17230 1523 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17231 : {
17232 0 : error_at (OMP_CLAUSE_LOCATION (c),
17233 : "%qE is not a variable in clause %<lastprivate%>", t);
17234 0 : remove = true;
17235 : }
17236 1523 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17237 1523 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17238 : {
17239 3 : error_at (OMP_CLAUSE_LOCATION (c),
17240 : "%qE appears more than once in data clauses", t);
17241 3 : remove = true;
17242 : }
17243 : else
17244 1520 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17245 : break;
17246 :
17247 253 : case OMP_CLAUSE_ALIGNED:
17248 253 : t = OMP_CLAUSE_DECL (c);
17249 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17250 : {
17251 0 : error_at (OMP_CLAUSE_LOCATION (c),
17252 : "%qE is not a variable in %<aligned%> clause", t);
17253 0 : remove = true;
17254 : }
17255 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17256 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17257 : {
17258 7 : error_at (OMP_CLAUSE_LOCATION (c),
17259 : "%qE in %<aligned%> clause is neither a pointer nor "
17260 : "an array", t);
17261 7 : remove = true;
17262 : }
17263 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17264 : {
17265 1 : error_at (OMP_CLAUSE_LOCATION (c),
17266 : "%<_Atomic%> %qD in %<aligned%> clause", t);
17267 1 : remove = true;
17268 1 : break;
17269 : }
17270 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17271 : {
17272 0 : error_at (OMP_CLAUSE_LOCATION (c),
17273 : "%qE appears more than once in %<aligned%> clauses",
17274 : t);
17275 0 : remove = true;
17276 : }
17277 : else
17278 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17279 : break;
17280 :
17281 125 : case OMP_CLAUSE_NONTEMPORAL:
17282 125 : t = OMP_CLAUSE_DECL (c);
17283 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17284 : {
17285 0 : error_at (OMP_CLAUSE_LOCATION (c),
17286 : "%qE is not a variable in %<nontemporal%> clause", t);
17287 0 : remove = true;
17288 : }
17289 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17290 : {
17291 2 : error_at (OMP_CLAUSE_LOCATION (c),
17292 : "%qE appears more than once in %<nontemporal%> "
17293 : "clauses", t);
17294 2 : remove = true;
17295 : }
17296 : else
17297 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17298 : break;
17299 :
17300 802 : case OMP_CLAUSE_ALLOCATE:
17301 802 : t = OMP_CLAUSE_DECL (c);
17302 802 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17303 : {
17304 1 : error_at (OMP_CLAUSE_LOCATION (c),
17305 : "%qE is not a variable in %<allocate%> clause", t);
17306 1 : remove = true;
17307 : }
17308 801 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17309 : {
17310 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17311 : "%qE appears more than once in %<allocate%> clauses",
17312 : t);
17313 2 : remove = true;
17314 : }
17315 : else
17316 : {
17317 799 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17318 799 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17319 700 : allocate_seen = true;
17320 : }
17321 : break;
17322 :
17323 357 : case OMP_CLAUSE_DOACROSS:
17324 357 : t = OMP_CLAUSE_DECL (c);
17325 357 : if (t == NULL_TREE)
17326 : break;
17327 169 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17328 : {
17329 169 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17330 1315 : for (; t; t = TREE_CHAIN (t))
17331 : {
17332 1146 : tree decl = TREE_VALUE (t);
17333 1146 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17334 : {
17335 3 : tree offset = TREE_PURPOSE (t);
17336 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17337 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17338 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17339 : neg ? MINUS_EXPR : PLUS_EXPR,
17340 : decl, offset);
17341 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17342 : sizetype,
17343 : fold_convert (sizetype, t2),
17344 : fold_convert (sizetype, decl));
17345 3 : if (t2 == error_mark_node)
17346 : {
17347 : remove = true;
17348 : break;
17349 : }
17350 3 : TREE_PURPOSE (t) = t2;
17351 : }
17352 : }
17353 : break;
17354 : }
17355 0 : gcc_unreachable ();
17356 :
17357 36 : case OMP_CLAUSE_USES_ALLOCATORS:
17358 36 : t = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
17359 36 : if (t == error_mark_node)
17360 : {
17361 : remove = true;
17362 : break;
17363 : }
17364 6 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17365 33 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17366 27 : || bitmap_bit_p (&map_head, DECL_UID (t))
17367 27 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17368 26 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))))
17369 : {
17370 1 : error_at (OMP_CLAUSE_LOCATION (c),
17371 : "%qE appears more than once in data clauses", t);
17372 1 : remove = true;
17373 1 : break;
17374 : }
17375 : else
17376 31 : bitmap_set_bit (&generic_head, DECL_UID (t));
17377 31 : if (TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17378 59 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17379 : "omp_allocator_handle_t") != 0)
17380 : {
17381 3 : error_at (OMP_CLAUSE_LOCATION (c),
17382 : "allocator %qE must be of %<omp_allocator_handle_t%> "
17383 : "type", t);
17384 3 : remove = true;
17385 3 : break;
17386 : }
17387 28 : tree init;
17388 28 : if (!DECL_P (t)
17389 28 : || (TREE_CODE (t) == CONST_DECL
17390 5 : && ((init = DECL_INITIAL(t)) == nullptr
17391 5 : || TREE_CODE (init) != INTEGER_CST
17392 5 : || ((wi::to_widest (init) < 0
17393 5 : || wi::to_widest (init) > GOMP_OMP_PREDEF_ALLOC_MAX)
17394 0 : && (wi::to_widest (init) < GOMP_OMPX_PREDEF_ALLOC_MIN
17395 0 : || (wi::to_widest (init)
17396 0 : > GOMP_OMPX_PREDEF_ALLOC_MAX))))))
17397 : {
17398 0 : remove = true;
17399 0 : error_at (OMP_CLAUSE_LOCATION (c),
17400 : "allocator %qE must be either a variable or a "
17401 : "predefined allocator", t);
17402 0 : break;
17403 : }
17404 28 : else if (TREE_CODE (t) == CONST_DECL)
17405 : {
17406 : /* omp_null_allocator is ignored and for predefined allocators,
17407 : not special handling is required; thus, remove them removed. */
17408 5 : remove = true;
17409 :
17410 5 : if (OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c)
17411 5 : || OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c))
17412 : {
17413 0 : error_at (OMP_CLAUSE_LOCATION (c),
17414 : "modifiers cannot be used with predefined "
17415 : "allocator %qE", t);
17416 0 : break;
17417 : }
17418 : }
17419 28 : t = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
17420 28 : if (t == error_mark_node)
17421 : {
17422 : remove = true;
17423 : break;
17424 : }
17425 28 : if (t != NULL_TREE
17426 28 : && ((TREE_CODE (t) != CONST_DECL && TREE_CODE (t) != INTEGER_CST)
17427 5 : || TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17428 8 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17429 : "omp_memspace_handle_t") != 0))
17430 : {
17431 1 : error_at (OMP_CLAUSE_LOCATION (c), "memspace modifier %qE must be"
17432 : " constant enum of %<omp_memspace_handle_t%> type", t);
17433 1 : remove = true;
17434 1 : break;
17435 : }
17436 27 : t = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
17437 27 : if (t == error_mark_node)
17438 : {
17439 : remove = true;
17440 : break;
17441 : }
17442 27 : if (t != NULL_TREE
17443 : && t != error_mark_node
17444 27 : && (DECL_EXTERNAL (t)
17445 9 : || TREE_CODE (t) == PARM_DECL))
17446 : {
17447 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must be "
17448 : "defined in same scope as the construct on which the "
17449 : "clause appears", t);
17450 3 : remove = true;
17451 : }
17452 27 : if (t != NULL_TREE)
17453 : {
17454 11 : bool type_err = false;
17455 :
17456 11 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
17457 10 : || DECL_SIZE (t) == NULL_TREE
17458 20 : || !COMPLETE_TYPE_P (TREE_TYPE (t)))
17459 : type_err = true;
17460 : else
17461 : {
17462 9 : tree elem_t = TREE_TYPE (TREE_TYPE (t));
17463 9 : if (TREE_CODE (elem_t) != RECORD_TYPE
17464 18 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (elem_t)),
17465 : "omp_alloctrait_t") != 0
17466 18 : || !TYPE_READONLY (elem_t))
17467 : type_err = true;
17468 : }
17469 8 : if (type_err)
17470 : {
17471 3 : if (t != error_mark_node)
17472 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must "
17473 : "be of %<const omp_alloctrait_t []%> type", t);
17474 : else
17475 0 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must "
17476 : "be of %<const omp_alloctrait_t []%> type");
17477 : remove = true;
17478 : }
17479 : else
17480 : {
17481 8 : tree cst_val = decl_constant_value_1 (t, true);
17482 8 : if (cst_val == t)
17483 : {
17484 1 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must be "
17485 : "initialized with constants");
17486 :
17487 1 : remove = true;
17488 : }
17489 : }
17490 : }
17491 24 : if (remove)
17492 : break;
17493 18 : pc = &OMP_CLAUSE_CHAIN (c);
17494 18 : continue;
17495 713 : case OMP_CLAUSE_DEPEND:
17496 713 : depend_clause = c;
17497 : /* FALLTHRU */
17498 870 : case OMP_CLAUSE_AFFINITY:
17499 870 : t = OMP_CLAUSE_DECL (c);
17500 870 : if (OMP_ITERATOR_DECL_P (t))
17501 : {
17502 131 : if (TREE_PURPOSE (t) != last_iterators)
17503 111 : last_iterators_remove
17504 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17505 131 : last_iterators = TREE_PURPOSE (t);
17506 131 : t = TREE_VALUE (t);
17507 131 : if (last_iterators_remove)
17508 34 : t = error_mark_node;
17509 : }
17510 : else
17511 : last_iterators = NULL_TREE;
17512 870 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17513 : {
17514 413 : if (handle_omp_array_sections (c, ort))
17515 : remove = true;
17516 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17517 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17518 : {
17519 1 : error_at (OMP_CLAUSE_LOCATION (c),
17520 : "%<depend%> clause with %<depobj%> dependence "
17521 : "type on array section");
17522 1 : remove = true;
17523 : }
17524 : break;
17525 : }
17526 457 : if (t == error_mark_node)
17527 : remove = true;
17528 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17529 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17530 : {
17531 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17532 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17533 : {
17534 3 : error_at (OMP_CLAUSE_LOCATION (c),
17535 : "%<omp_all_memory%> used with %<depend%> kind "
17536 : "other than %<out%> or %<inout%>");
17537 3 : remove = true;
17538 : }
17539 : }
17540 408 : else if (!lvalue_p (t))
17541 : {
17542 6 : error_at (OMP_CLAUSE_LOCATION (c),
17543 : "%qE is not lvalue expression nor array section in "
17544 : "%qs clause", t,
17545 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17546 3 : remove = true;
17547 : }
17548 405 : else if (TREE_CODE (t) == COMPONENT_REF
17549 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17550 : {
17551 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17552 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17553 2 : error_at (OMP_CLAUSE_LOCATION (c),
17554 : "bit-field %qE in %qs clause", t,
17555 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17556 2 : remove = true;
17557 : }
17558 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17559 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17560 : {
17561 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17562 : {
17563 2 : error_at (OMP_CLAUSE_LOCATION (c),
17564 : "%qE does not have %<omp_depend_t%> type in "
17565 : "%<depend%> clause with %<depobj%> dependence "
17566 : "type", t);
17567 2 : remove = true;
17568 : }
17569 : }
17570 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17571 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17572 : {
17573 2 : error_at (OMP_CLAUSE_LOCATION (c),
17574 : "%qE should not have %<omp_depend_t%> type in "
17575 : "%<depend%> clause with dependence type other than "
17576 : "%<depobj%>", t);
17577 2 : remove = true;
17578 : }
17579 12 : if (!remove)
17580 : {
17581 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17582 12 : t = null_pointer_node;
17583 : else
17584 : {
17585 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17586 : ADDR_EXPR, t, false);
17587 399 : if (addr == error_mark_node)
17588 : {
17589 : remove = true;
17590 : break;
17591 : }
17592 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17593 : RO_UNARY_STAR);
17594 399 : if (t == error_mark_node)
17595 : {
17596 : remove = true;
17597 : break;
17598 : }
17599 : }
17600 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17601 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17602 : else
17603 380 : OMP_CLAUSE_DECL (c) = t;
17604 : }
17605 : break;
17606 :
17607 6678 : case OMP_CLAUSE_MAP:
17608 6678 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17609 91 : goto move_implicit;
17610 6587 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17611 6587 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17612 : {
17613 : remove = true;
17614 : break;
17615 : }
17616 : /* FALLTHRU */
17617 9440 : case OMP_CLAUSE_TO:
17618 9440 : case OMP_CLAUSE_FROM:
17619 9440 : if (OMP_CLAUSE_ITERATORS (c)
17620 9440 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17621 : {
17622 22858 : t = error_mark_node;
17623 : break;
17624 : }
17625 : /* FALLTHRU */
17626 9576 : case OMP_CLAUSE__CACHE_:
17627 9576 : {
17628 9576 : using namespace omp_addr_tokenizer;
17629 9576 : auto_vec<omp_addr_token *, 10> addr_tokens;
17630 :
17631 9576 : t = OMP_CLAUSE_DECL (c);
17632 9576 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17633 : {
17634 2002 : grp_start_p = pc;
17635 2002 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17636 :
17637 2002 : if (handle_omp_array_sections (c, ort))
17638 : remove = true;
17639 : else
17640 : {
17641 1862 : t = OMP_CLAUSE_DECL (c);
17642 1862 : if (!omp_mappable_type (TREE_TYPE (t)))
17643 : {
17644 2 : error_at (OMP_CLAUSE_LOCATION (c),
17645 : "array section does not have mappable type "
17646 : "in %qs clause",
17647 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17648 1 : remove = true;
17649 : }
17650 1861 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17651 : {
17652 2 : error_at (OMP_CLAUSE_LOCATION (c),
17653 : "%<_Atomic%> %qE in %qs clause", t,
17654 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17655 1 : remove = true;
17656 : }
17657 2511 : while (TREE_CODE (t) == ARRAY_REF)
17658 649 : t = TREE_OPERAND (t, 0);
17659 :
17660 1862 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17661 :
17662 1862 : if (!omp_parse_expr (addr_tokens, t))
17663 : {
17664 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17665 : "unsupported map expression %qE",
17666 0 : OMP_CLAUSE_DECL (c));
17667 0 : remove = true;
17668 0 : break;
17669 : }
17670 :
17671 : /* This check is to determine if this will be the only map
17672 : node created for this clause. Otherwise, we'll check
17673 : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17674 : node on the next iteration(s) of the loop. */
17675 3706 : if (addr_tokens.length () >= 4
17676 304 : && addr_tokens[0]->type == STRUCTURE_BASE
17677 302 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17678 302 : && addr_tokens[1]->type == ACCESS_METHOD
17679 302 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17680 302 : && addr_tokens[3]->type == ACCESS_METHOD
17681 2103 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17682 188 : || (addr_tokens[3]->u.access_kind
17683 : == ACCESS_INDEXED_ARRAY)))
17684 : {
17685 55 : tree rt = addr_tokens[1]->expr;
17686 :
17687 55 : gcc_assert (DECL_P (rt));
17688 :
17689 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17690 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17691 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17692 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17693 0 : || bitmap_bit_p (&map_firstprivate_head,
17694 0 : DECL_UID (rt))))
17695 : {
17696 : remove = true;
17697 : break;
17698 : }
17699 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17700 : break;
17701 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17702 : {
17703 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17704 0 : error_at (OMP_CLAUSE_LOCATION (c),
17705 : "%qD appears more than once in motion "
17706 : "clauses", rt);
17707 0 : else if (openacc)
17708 0 : error_at (OMP_CLAUSE_LOCATION (c),
17709 : "%qD appears more than once in data "
17710 : "clauses", rt);
17711 : else
17712 0 : error_at (OMP_CLAUSE_LOCATION (c),
17713 : "%qD appears more than once in map "
17714 : "clauses", rt);
17715 : remove = true;
17716 : }
17717 : else
17718 : {
17719 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17720 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17721 : }
17722 : }
17723 1862 : }
17724 1984 : if (c_oacc_check_attachments (c))
17725 2 : remove = true;
17726 1984 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17727 1808 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17728 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17729 2009 : && !OMP_CLAUSE_SIZE (c))
17730 : /* In this case, we have a single array element which is a
17731 : pointer, and we already set OMP_CLAUSE_SIZE in
17732 : handle_omp_array_sections above. For attach/detach
17733 : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17734 : to zero here. */
17735 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17736 : break;
17737 : }
17738 7574 : else if (!omp_parse_expr (addr_tokens, t))
17739 : {
17740 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17741 : "unsupported map expression %qE",
17742 0 : OMP_CLAUSE_DECL (c));
17743 0 : remove = true;
17744 0 : break;
17745 : }
17746 7574 : if (t == error_mark_node)
17747 : {
17748 : remove = true;
17749 : break;
17750 : }
17751 : /* OpenACC attach / detach clauses must be pointers. */
17752 7562 : if (c_oacc_check_attachments (c))
17753 : {
17754 : remove = true;
17755 : break;
17756 : }
17757 7558 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17758 4739 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17759 4712 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17760 7600 : && !OMP_CLAUSE_SIZE (c))
17761 : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17762 : bias) to zero here, so it is not set erroneously to the pointer
17763 : size later on in gimplify.cc. */
17764 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17765 :
17766 7558 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17767 :
17768 7558 : if (!ai.check_clause (c))
17769 : {
17770 : remove = true;
17771 : break;
17772 : }
17773 :
17774 7550 : if (!ai.map_supported_p ())
17775 : {
17776 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17777 : "unsupported map expression %qE",
17778 7 : OMP_CLAUSE_DECL (c));
17779 7 : remove = true;
17780 7 : break;
17781 : }
17782 :
17783 15086 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17784 : || addr_tokens[0]->type == STRUCTURE_BASE)
17785 : && addr_tokens[1]->type == ACCESS_METHOD);
17786 :
17787 7543 : t = addr_tokens[1]->expr;
17788 :
17789 7543 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17790 0 : goto skip_decl_checks;
17791 :
17792 : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17793 : mapping. OpenACC allows multiple fields of the same structure
17794 : to be written. */
17795 7543 : if (addr_tokens[0]->type == STRUCTURE_BASE
17796 7543 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17797 252 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17798 288 : goto skip_decl_checks;
17799 :
17800 7255 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17801 : {
17802 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17803 : break;
17804 :
17805 6 : error_at (OMP_CLAUSE_LOCATION (c),
17806 : "%qE is not a variable in %qs clause", t,
17807 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17808 3 : remove = true;
17809 : }
17810 7252 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17811 : {
17812 0 : error_at (OMP_CLAUSE_LOCATION (c),
17813 : "%qD is threadprivate variable in %qs clause", t,
17814 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17815 0 : remove = true;
17816 : }
17817 7252 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17818 4440 : || (OMP_CLAUSE_MAP_KIND (c)
17819 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17820 10648 : && !c_mark_addressable (t))
17821 : remove = true;
17822 7252 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17823 4440 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17824 4440 : || (OMP_CLAUSE_MAP_KIND (c)
17825 : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17826 3396 : || (OMP_CLAUSE_MAP_KIND (c)
17827 : == GOMP_MAP_FORCE_DEVICEPTR)))
17828 6150 : && t == OMP_CLAUSE_DECL (c)
17829 13152 : && !omp_mappable_type (TREE_TYPE (t)))
17830 : {
17831 16 : error_at (OMP_CLAUSE_LOCATION (c),
17832 : "%qD does not have a mappable type in %qs clause", t,
17833 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17834 8 : remove = true;
17835 : }
17836 7244 : else if (TREE_TYPE (t) == error_mark_node)
17837 : remove = true;
17838 7243 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17839 : {
17840 8 : error_at (OMP_CLAUSE_LOCATION (c),
17841 : "%<_Atomic%> %qE in %qs clause", t,
17842 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17843 4 : remove = true;
17844 : }
17845 7239 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17846 4430 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17847 7403 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17848 128 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17849 128 : || bitmap_bit_p (&map_firstprivate_head,
17850 128 : DECL_UID (t))))
17851 : remove = true;
17852 7202 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17853 7202 : && (OMP_CLAUSE_MAP_KIND (c)
17854 : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17855 : {
17856 1042 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17857 1042 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17858 2083 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17859 : {
17860 5 : error_at (OMP_CLAUSE_LOCATION (c),
17861 : "%qD appears more than once in data clauses", t);
17862 5 : remove = true;
17863 : }
17864 1037 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17865 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17866 1039 : && openacc)
17867 : {
17868 1 : error_at (OMP_CLAUSE_LOCATION (c),
17869 : "%qD appears more than once in data clauses", t);
17870 1 : remove = true;
17871 : }
17872 : else
17873 1036 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17874 : }
17875 6160 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17876 59 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17877 13 : && ort != C_ORT_OMP
17878 6173 : && ort != C_ORT_OMP_EXIT_DATA)
17879 : {
17880 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17881 0 : error_at (OMP_CLAUSE_LOCATION (c),
17882 : "%qD appears more than once in motion clauses", t);
17883 10 : else if (openacc)
17884 7 : error_at (OMP_CLAUSE_LOCATION (c),
17885 : "%qD appears more than once in data clauses", t);
17886 : else
17887 3 : error_at (OMP_CLAUSE_LOCATION (c),
17888 : "%qD appears more than once in map clauses", t);
17889 : remove = true;
17890 : }
17891 6150 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17892 : {
17893 0 : error_at (OMP_CLAUSE_LOCATION (c),
17894 : "%qD appears more than once in data clauses", t);
17895 0 : remove = true;
17896 : }
17897 6150 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17898 6150 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17899 : {
17900 9 : if (openacc)
17901 0 : error_at (OMP_CLAUSE_LOCATION (c),
17902 : "%qD appears more than once in data clauses", t);
17903 : else
17904 9 : error_at (OMP_CLAUSE_LOCATION (c),
17905 : "%qD appears both in data and map clauses", t);
17906 : remove = true;
17907 : }
17908 6141 : else if (!omp_access_chain_p (addr_tokens, 1))
17909 : {
17910 6141 : bitmap_set_bit (&map_head, DECL_UID (t));
17911 6141 : if (t != OMP_CLAUSE_DECL (c)
17912 6141 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17913 246 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17914 : }
17915 :
17916 0 : skip_decl_checks:
17917 : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17918 : the containing loop (here) iterates through the new nodes
17919 : created by that expansion. Avoid expanding those again (just
17920 : by checking the node type). */
17921 7543 : if (!remove
17922 7543 : && ort != C_ORT_DECLARE_SIMD
17923 7543 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17924 4650 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17925 3614 : && (OMP_CLAUSE_MAP_KIND (c)
17926 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17927 3614 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17928 3614 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17929 3314 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17930 3287 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17931 : {
17932 6087 : grp_start_p = pc;
17933 6087 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17934 6087 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17935 : addr_tokens, ort);
17936 6087 : if (nc != error_mark_node)
17937 6087 : c = nc;
17938 : }
17939 9591 : }
17940 7543 : break;
17941 :
17942 232 : case OMP_CLAUSE_ENTER:
17943 232 : case OMP_CLAUSE_LINK:
17944 232 : t = OMP_CLAUSE_DECL (c);
17945 232 : const char *cname;
17946 232 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17947 232 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17948 232 : && OMP_CLAUSE_ENTER_TO (c))
17949 : cname = "to";
17950 232 : if (TREE_CODE (t) == FUNCTION_DECL
17951 232 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17952 : ;
17953 151 : else if (!VAR_P (t))
17954 : {
17955 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17956 0 : error_at (OMP_CLAUSE_LOCATION (c),
17957 : "%qE is neither a variable nor a function name in "
17958 : "clause %qs", t, cname);
17959 : else
17960 1 : error_at (OMP_CLAUSE_LOCATION (c),
17961 : "%qE is not a variable in clause %qs", t, cname);
17962 : remove = true;
17963 : }
17964 150 : else if (DECL_THREAD_LOCAL_P (t))
17965 : {
17966 2 : error_at (OMP_CLAUSE_LOCATION (c),
17967 : "%qD is threadprivate variable in %qs clause", t,
17968 : cname);
17969 2 : remove = true;
17970 : }
17971 148 : else if (!omp_mappable_type (TREE_TYPE (t)))
17972 : {
17973 6 : error_at (OMP_CLAUSE_LOCATION (c),
17974 : "%qD does not have a mappable type in %qs clause", t,
17975 : cname);
17976 6 : remove = true;
17977 : }
17978 8 : if (remove)
17979 : break;
17980 223 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17981 : {
17982 8 : error_at (OMP_CLAUSE_LOCATION (c),
17983 : "%qE appears more than once on the same "
17984 : "%<declare target%> directive", t);
17985 8 : remove = true;
17986 : }
17987 : else
17988 215 : bitmap_set_bit (&generic_head, DECL_UID (t));
17989 : break;
17990 :
17991 117 : case OMP_CLAUSE_UNIFORM:
17992 117 : t = OMP_CLAUSE_DECL (c);
17993 117 : if (TREE_CODE (t) != PARM_DECL)
17994 : {
17995 0 : if (DECL_P (t))
17996 0 : error_at (OMP_CLAUSE_LOCATION (c),
17997 : "%qD is not an argument in %<uniform%> clause", t);
17998 : else
17999 0 : error_at (OMP_CLAUSE_LOCATION (c),
18000 : "%qE is not an argument in %<uniform%> clause", t);
18001 : remove = true;
18002 : break;
18003 : }
18004 : /* map_head bitmap is used as uniform_head if declare_simd. */
18005 117 : bitmap_set_bit (&map_head, DECL_UID (t));
18006 117 : goto check_dup_generic;
18007 :
18008 161 : case OMP_CLAUSE_IS_DEVICE_PTR:
18009 161 : case OMP_CLAUSE_USE_DEVICE_PTR:
18010 161 : t = OMP_CLAUSE_DECL (c);
18011 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
18012 123 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18013 161 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
18014 : {
18015 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
18016 21 : && !openacc)
18017 : {
18018 1 : error_at (OMP_CLAUSE_LOCATION (c),
18019 : "%qs variable is not a pointer",
18020 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18021 1 : remove = true;
18022 : }
18023 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
18024 : {
18025 4 : error_at (OMP_CLAUSE_LOCATION (c),
18026 : "%qs variable is neither a pointer nor an array",
18027 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18028 4 : remove = true;
18029 : }
18030 : }
18031 161 : goto check_dup_generic;
18032 :
18033 89 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
18034 89 : t = OMP_CLAUSE_DECL (c);
18035 89 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
18036 : {
18037 11 : if (handle_omp_array_sections (c, ort))
18038 : remove = true;
18039 : else
18040 : {
18041 11 : t = OMP_CLAUSE_DECL (c);
18042 24 : while (TREE_CODE (t) == ARRAY_REF)
18043 13 : t = TREE_OPERAND (t, 0);
18044 : }
18045 : }
18046 89 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18047 89 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18048 89 : c_mark_addressable (t);
18049 89 : goto check_dup_generic_t;
18050 :
18051 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
18052 36 : t = OMP_CLAUSE_DECL (c);
18053 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18054 36 : c_mark_addressable (t);
18055 36 : goto check_dup_generic;
18056 :
18057 4791 : case OMP_CLAUSE_NOWAIT:
18058 4791 : if (copyprivate_seen)
18059 : {
18060 1 : error_at (OMP_CLAUSE_LOCATION (c),
18061 : "%<nowait%> clause must not be used together "
18062 : "with %<copyprivate%> clause");
18063 1 : remove = true;
18064 1 : break;
18065 : }
18066 4790 : nowait_clause = pc;
18067 4790 : pc = &OMP_CLAUSE_CHAIN (c);
18068 4790 : continue;
18069 :
18070 523 : case OMP_CLAUSE_ORDER:
18071 523 : if (ordered_clause)
18072 : {
18073 2 : error_at (OMP_CLAUSE_LOCATION (c),
18074 : "%<order%> clause must not be used together "
18075 : "with %<ordered%> clause");
18076 2 : remove = true;
18077 2 : break;
18078 : }
18079 521 : else if (order_clause)
18080 : {
18081 : /* Silently remove duplicates. */
18082 : remove = true;
18083 : break;
18084 : }
18085 513 : order_clause = pc;
18086 513 : pc = &OMP_CLAUSE_CHAIN (c);
18087 513 : continue;
18088 :
18089 32 : case OMP_CLAUSE_DETACH:
18090 32 : t = OMP_CLAUSE_DECL (c);
18091 32 : if (detach_seen)
18092 : {
18093 1 : error_at (OMP_CLAUSE_LOCATION (c),
18094 : "too many %qs clauses on a task construct",
18095 : "detach");
18096 1 : remove = true;
18097 1 : break;
18098 : }
18099 31 : detach_seen = pc;
18100 31 : pc = &OMP_CLAUSE_CHAIN (c);
18101 31 : c_mark_addressable (t);
18102 31 : continue;
18103 :
18104 14583 : case OMP_CLAUSE_IF:
18105 14583 : case OMP_CLAUSE_SELF:
18106 14583 : case OMP_CLAUSE_NUM_THREADS:
18107 14583 : case OMP_CLAUSE_NUM_TEAMS:
18108 14583 : case OMP_CLAUSE_THREAD_LIMIT:
18109 14583 : case OMP_CLAUSE_DEFAULT:
18110 14583 : case OMP_CLAUSE_UNTIED:
18111 14583 : case OMP_CLAUSE_COLLAPSE:
18112 14583 : case OMP_CLAUSE_FINAL:
18113 14583 : case OMP_CLAUSE_DEVICE:
18114 14583 : case OMP_CLAUSE_DIST_SCHEDULE:
18115 14583 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
18116 14583 : case OMP_CLAUSE_PARALLEL:
18117 14583 : case OMP_CLAUSE_FOR:
18118 14583 : case OMP_CLAUSE_SECTIONS:
18119 14583 : case OMP_CLAUSE_TASKGROUP:
18120 14583 : case OMP_CLAUSE_PROC_BIND:
18121 14583 : case OMP_CLAUSE_DEVICE_TYPE:
18122 14583 : case OMP_CLAUSE_PRIORITY:
18123 14583 : case OMP_CLAUSE_THREADS:
18124 14583 : case OMP_CLAUSE_SIMD:
18125 14583 : case OMP_CLAUSE_HINT:
18126 14583 : case OMP_CLAUSE_FILTER:
18127 14583 : case OMP_CLAUSE_DEFAULTMAP:
18128 14583 : case OMP_CLAUSE_BIND:
18129 14583 : case OMP_CLAUSE_NUM_GANGS:
18130 14583 : case OMP_CLAUSE_NUM_WORKERS:
18131 14583 : case OMP_CLAUSE_VECTOR_LENGTH:
18132 14583 : case OMP_CLAUSE_ASYNC:
18133 14583 : case OMP_CLAUSE_WAIT:
18134 14583 : case OMP_CLAUSE_AUTO:
18135 14583 : case OMP_CLAUSE_INDEPENDENT:
18136 14583 : case OMP_CLAUSE_SEQ:
18137 14583 : case OMP_CLAUSE_GANG:
18138 14583 : case OMP_CLAUSE_WORKER:
18139 14583 : case OMP_CLAUSE_VECTOR:
18140 14583 : case OMP_CLAUSE_TILE:
18141 14583 : case OMP_CLAUSE_IF_PRESENT:
18142 14583 : case OMP_CLAUSE_FINALIZE:
18143 14583 : case OMP_CLAUSE_NOHOST:
18144 14583 : case OMP_CLAUSE_INDIRECT:
18145 14583 : case OMP_CLAUSE_NOVARIANTS:
18146 14583 : case OMP_CLAUSE_NOCONTEXT:
18147 14583 : pc = &OMP_CLAUSE_CHAIN (c);
18148 14583 : continue;
18149 :
18150 62 : case OMP_CLAUSE_GRAINSIZE:
18151 62 : grainsize_seen = pc;
18152 62 : pc = &OMP_CLAUSE_CHAIN (c);
18153 62 : continue;
18154 :
18155 50 : case OMP_CLAUSE_NUM_TASKS:
18156 50 : num_tasks_seen = true;
18157 50 : pc = &OMP_CLAUSE_CHAIN (c);
18158 50 : continue;
18159 :
18160 79 : case OMP_CLAUSE_MERGEABLE:
18161 79 : mergeable_seen = true;
18162 79 : pc = &OMP_CLAUSE_CHAIN (c);
18163 79 : continue;
18164 :
18165 18 : case OMP_CLAUSE_NOGROUP:
18166 18 : nogroup_seen = pc;
18167 18 : pc = &OMP_CLAUSE_CHAIN (c);
18168 18 : continue;
18169 :
18170 3465 : case OMP_CLAUSE_SCHEDULE:
18171 3465 : schedule_clause = c;
18172 3465 : pc = &OMP_CLAUSE_CHAIN (c);
18173 3465 : continue;
18174 :
18175 304 : case OMP_CLAUSE_ORDERED:
18176 304 : ordered_clause = c;
18177 304 : if (order_clause)
18178 : {
18179 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
18180 : "%<order%> clause must not be used together "
18181 : "with %<ordered%> clause");
18182 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
18183 2 : order_clause = NULL;
18184 : }
18185 304 : pc = &OMP_CLAUSE_CHAIN (c);
18186 304 : continue;
18187 :
18188 223 : case OMP_CLAUSE_SAFELEN:
18189 223 : safelen = c;
18190 223 : pc = &OMP_CLAUSE_CHAIN (c);
18191 223 : continue;
18192 320 : case OMP_CLAUSE_SIMDLEN:
18193 320 : simdlen = c;
18194 320 : pc = &OMP_CLAUSE_CHAIN (c);
18195 320 : continue;
18196 :
18197 69 : case OMP_CLAUSE_FULL:
18198 69 : full_seen = pc;
18199 69 : pc = &OMP_CLAUSE_CHAIN (c);
18200 69 : continue;
18201 :
18202 223 : case OMP_CLAUSE_PARTIAL:
18203 223 : partial_seen = true;
18204 223 : pc = &OMP_CLAUSE_CHAIN (c);
18205 223 : continue;
18206 :
18207 0 : case OMP_CLAUSE_SIZES:
18208 0 : pc = &OMP_CLAUSE_CHAIN (c);
18209 0 : continue;
18210 :
18211 205 : case OMP_CLAUSE_INBRANCH:
18212 205 : case OMP_CLAUSE_NOTINBRANCH:
18213 205 : if (branch_seen)
18214 : {
18215 1 : error_at (OMP_CLAUSE_LOCATION (c),
18216 : "%<inbranch%> clause is incompatible with "
18217 : "%<notinbranch%>");
18218 1 : remove = true;
18219 1 : break;
18220 : }
18221 204 : branch_seen = true;
18222 204 : pc = &OMP_CLAUSE_CHAIN (c);
18223 204 : continue;
18224 :
18225 367 : case OMP_CLAUSE_INCLUSIVE:
18226 367 : case OMP_CLAUSE_EXCLUSIVE:
18227 367 : need_complete = true;
18228 367 : need_implicitly_determined = true;
18229 367 : t = OMP_CLAUSE_DECL (c);
18230 367 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
18231 : {
18232 0 : error_at (OMP_CLAUSE_LOCATION (c),
18233 : "%qE is not a variable in clause %qs", t,
18234 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18235 0 : remove = true;
18236 : }
18237 : break;
18238 :
18239 152 : case OMP_CLAUSE_INIT:
18240 152 : init_seen = true;
18241 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
18242 71 : init_no_targetsync_clause = c;
18243 : /* FALLTHRU */
18244 242 : case OMP_CLAUSE_DESTROY:
18245 242 : case OMP_CLAUSE_USE:
18246 242 : init_use_destroy_seen = true;
18247 242 : t = OMP_CLAUSE_DECL (c);
18248 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18249 : {
18250 3 : error_at (OMP_CLAUSE_LOCATION (c),
18251 : "%qD appears more than once in action clauses", t);
18252 3 : remove = true;
18253 3 : break;
18254 : }
18255 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
18256 : /* FALLTHRU */
18257 298 : case OMP_CLAUSE_INTEROP:
18258 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
18259 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
18260 : {
18261 40 : error_at (OMP_CLAUSE_LOCATION (c),
18262 : "%qD must be of %<omp_interop_t%>",
18263 20 : OMP_CLAUSE_DECL (c));
18264 20 : remove = true;
18265 : }
18266 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
18267 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
18268 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
18269 : {
18270 12 : error_at (OMP_CLAUSE_LOCATION (c),
18271 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
18272 6 : remove = true;
18273 : }
18274 298 : pc = &OMP_CLAUSE_CHAIN (c);
18275 298 : break;
18276 0 : default:
18277 0 : gcc_unreachable ();
18278 24952 : }
18279 :
18280 22858 : if (!remove)
18281 : {
18282 22273 : t = OMP_CLAUSE_DECL (c);
18283 :
18284 22273 : if (need_complete)
18285 : {
18286 3953 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
18287 3953 : if (t == error_mark_node)
18288 7 : remove = true;
18289 : }
18290 :
18291 22273 : if (need_implicitly_determined)
18292 : {
18293 9552 : const char *share_name = NULL;
18294 :
18295 9552 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
18296 : share_name = "threadprivate";
18297 9546 : else switch (c_omp_predetermined_sharing (t))
18298 : {
18299 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18300 : break;
18301 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18302 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18303 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18304 28 : && c_omp_predefined_variable (t))
18305 : /* The __func__ variable and similar function-local
18306 : predefined variables may be listed in a shared or
18307 : firstprivate clause. */
18308 : break;
18309 : share_name = "shared";
18310 : break;
18311 : case OMP_CLAUSE_DEFAULT_PRIVATE:
18312 : share_name = "private";
18313 : break;
18314 0 : default:
18315 0 : gcc_unreachable ();
18316 : }
18317 : if (share_name)
18318 : {
18319 20 : error_at (OMP_CLAUSE_LOCATION (c),
18320 : "%qE is predetermined %qs for %qs",
18321 : t, share_name,
18322 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18323 10 : remove = true;
18324 : }
18325 9542 : else if (TREE_READONLY (t)
18326 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18327 9555 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18328 : {
18329 4 : error_at (OMP_CLAUSE_LOCATION (c),
18330 : "%<const%> qualified %qE may appear only in "
18331 : "%<shared%> or %<firstprivate%> clauses", t);
18332 4 : remove = true;
18333 : }
18334 : }
18335 : }
18336 :
18337 22273 : if (remove)
18338 : {
18339 606 : if (grp_start_p)
18340 : {
18341 : /* If we found a clause to remove, we want to remove the whole
18342 : expanded group, otherwise gimplify
18343 : (omp_resolve_clause_dependencies) can get confused. */
18344 154 : *grp_start_p = grp_sentinel;
18345 154 : pc = grp_start_p;
18346 154 : grp_start_p = NULL;
18347 : }
18348 : else
18349 452 : *pc = OMP_CLAUSE_CHAIN (c);
18350 : }
18351 : else
18352 22252 : pc = &OMP_CLAUSE_CHAIN (c);
18353 : }
18354 :
18355 29480 : if (grp_start_p
18356 29480 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18357 52 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18358 32 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18359 :
18360 29480 : if (simdlen
18361 29480 : && safelen
18362 29597 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18363 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18364 : {
18365 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18366 : "%<simdlen%> clause value is bigger than "
18367 : "%<safelen%> clause value");
18368 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18369 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18370 : }
18371 :
18372 29480 : if (ordered_clause
18373 29480 : && schedule_clause
18374 29480 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18375 : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18376 : {
18377 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18378 : "%<nonmonotonic%> schedule modifier specified together "
18379 : "with %<ordered%> clause");
18380 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18381 7 : = (enum omp_clause_schedule_kind)
18382 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18383 : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18384 : }
18385 :
18386 29480 : if (reduction_seen < 0 && ordered_clause)
18387 : {
18388 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18389 : "%qs clause specified together with %<inscan%> "
18390 : "%<reduction%> clause", "ordered");
18391 2 : reduction_seen = -2;
18392 : }
18393 :
18394 29480 : if (reduction_seen < 0 && schedule_clause)
18395 : {
18396 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18397 : "%qs clause specified together with %<inscan%> "
18398 : "%<reduction%> clause", "schedule");
18399 3 : reduction_seen = -2;
18400 : }
18401 :
18402 29480 : if (linear_variable_step_check
18403 29480 : || reduction_seen == -2
18404 : || allocate_seen
18405 29468 : || target_in_reduction_seen)
18406 5190 : for (pc = &clauses, c = clauses; c ; c = *pc)
18407 : {
18408 4589 : bool remove = false;
18409 4589 : if (allocate_seen)
18410 4431 : switch (OMP_CLAUSE_CODE (c))
18411 : {
18412 430 : case OMP_CLAUSE_REDUCTION:
18413 430 : case OMP_CLAUSE_IN_REDUCTION:
18414 430 : case OMP_CLAUSE_TASK_REDUCTION:
18415 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18416 : {
18417 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18418 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18419 0 : t = TREE_OPERAND (t, 0);
18420 23 : if (TREE_CODE (t) == ADDR_EXPR
18421 10 : || INDIRECT_REF_P (t))
18422 13 : t = TREE_OPERAND (t, 0);
18423 23 : if (DECL_P (t))
18424 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18425 : break;
18426 : }
18427 : /* FALLTHRU */
18428 1318 : case OMP_CLAUSE_PRIVATE:
18429 1318 : case OMP_CLAUSE_FIRSTPRIVATE:
18430 1318 : case OMP_CLAUSE_LASTPRIVATE:
18431 1318 : case OMP_CLAUSE_LINEAR:
18432 1318 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18433 2636 : bitmap_clear_bit (&aligned_head,
18434 1318 : DECL_UID (OMP_CLAUSE_DECL (c)));
18435 : break;
18436 : default:
18437 : break;
18438 : }
18439 4589 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18440 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18441 4596 : && !bitmap_bit_p (&map_head,
18442 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18443 : {
18444 2 : error_at (OMP_CLAUSE_LOCATION (c),
18445 : "%<linear%> clause step is a parameter %qD not "
18446 : "specified in %<uniform%> clause",
18447 2 : OMP_CLAUSE_LINEAR_STEP (c));
18448 2 : remove = true;
18449 : }
18450 4587 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18451 4587 : && reduction_seen == -2)
18452 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18453 4589 : if (target_in_reduction_seen
18454 4589 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18455 : {
18456 256 : tree t = OMP_CLAUSE_DECL (c);
18457 256 : while (handled_component_p (t)
18458 : || INDIRECT_REF_P (t)
18459 : || TREE_CODE (t) == ADDR_EXPR
18460 : || TREE_CODE (t) == MEM_REF
18461 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18462 32 : t = TREE_OPERAND (t, 0);
18463 256 : if (DECL_P (t)
18464 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18465 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18466 : }
18467 :
18468 4589 : if (remove)
18469 2 : *pc = OMP_CLAUSE_CHAIN (c);
18470 : else
18471 4587 : pc = &OMP_CLAUSE_CHAIN (c);
18472 : }
18473 :
18474 601 : if (allocate_seen)
18475 5006 : for (pc = &clauses, c = clauses; c ; c = *pc)
18476 : {
18477 4431 : bool remove = false;
18478 4431 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18479 720 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18480 5131 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18481 : {
18482 3 : error_at (OMP_CLAUSE_LOCATION (c),
18483 : "%qD specified in %<allocate%> clause but not in "
18484 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18485 3 : remove = true;
18486 : }
18487 4431 : if (remove)
18488 3 : *pc = OMP_CLAUSE_CHAIN (c);
18489 : else
18490 4428 : pc = &OMP_CLAUSE_CHAIN (c);
18491 : }
18492 :
18493 29480 : if (nogroup_seen && reduction_seen)
18494 : {
18495 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18496 : "%<nogroup%> clause must not be used together with "
18497 : "%<reduction%> clause");
18498 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18499 : }
18500 :
18501 29480 : if (grainsize_seen && num_tasks_seen)
18502 : {
18503 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18504 : "%<grainsize%> clause must not be used together with "
18505 : "%<num_tasks%> clause");
18506 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18507 : }
18508 :
18509 29480 : if (full_seen && partial_seen)
18510 : {
18511 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18512 : "%<full%> clause must not be used together with "
18513 : "%<partial%> clause");
18514 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18515 : }
18516 :
18517 29480 : if (detach_seen)
18518 : {
18519 31 : if (mergeable_seen)
18520 : {
18521 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18522 : "%<detach%> clause must not be used together with "
18523 : "%<mergeable%> clause");
18524 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18525 : }
18526 : else
18527 : {
18528 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18529 :
18530 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18531 : {
18532 50 : bool remove = false;
18533 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18534 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18535 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18536 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18537 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18538 : {
18539 2 : error_at (OMP_CLAUSE_LOCATION (c),
18540 : "the event handle of a %<detach%> clause "
18541 : "should not be in a data-sharing clause");
18542 2 : remove = true;
18543 : }
18544 50 : if (remove)
18545 2 : *pc = OMP_CLAUSE_CHAIN (c);
18546 : else
18547 48 : pc = &OMP_CLAUSE_CHAIN (c);
18548 : }
18549 : }
18550 : }
18551 :
18552 29480 : if (ort == C_ORT_OMP_INTEROP
18553 29480 : && depend_clause
18554 21 : && (!init_use_destroy_seen
18555 20 : || (init_seen && init_no_targetsync_clause)))
18556 : {
18557 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18558 : "%<depend%> clause requires action clauses with "
18559 : "%<targetsync%> interop-type");
18560 3 : if (init_no_targetsync_clause)
18561 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18562 : "%<init%> clause lacks the %<targetsync%> modifier");
18563 : }
18564 :
18565 29480 : bitmap_obstack_release (NULL);
18566 29480 : return clauses;
18567 : }
18568 :
18569 : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18570 : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18571 :
18572 : tree
18573 15 : c_omp_finish_mapper_clauses (tree clauses)
18574 : {
18575 15 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18576 : }
18577 :
18578 : /* Return code to initialize DST with a copy constructor from SRC.
18579 : C doesn't have copy constructors nor assignment operators, only for
18580 : _Atomic vars we need to perform __atomic_load from src into a temporary
18581 : followed by __atomic_store of the temporary to dst. */
18582 :
18583 : tree
18584 18735 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18585 : {
18586 18735 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18587 18731 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18588 :
18589 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18590 4 : tree type = TREE_TYPE (dst);
18591 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18592 4 : tree tmp = create_tmp_var (nonatomic_type);
18593 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18594 4 : TREE_ADDRESSABLE (tmp) = 1;
18595 4 : suppress_warning (tmp);
18596 4 : tree src_addr = build_fold_addr_expr (src);
18597 4 : tree dst_addr = build_fold_addr_expr (dst);
18598 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18599 4 : vec<tree, va_gc> *params;
18600 : /* Expansion of a generic atomic load may require an addition
18601 : element, so allocate enough to prevent a resize. */
18602 4 : vec_alloc (params, 4);
18603 :
18604 : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18605 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18606 4 : params->quick_push (src_addr);
18607 4 : params->quick_push (tmp_addr);
18608 4 : params->quick_push (seq_cst);
18609 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18610 :
18611 4 : vec_alloc (params, 4);
18612 :
18613 : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18614 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18615 4 : params->quick_push (dst_addr);
18616 4 : params->quick_push (tmp_addr);
18617 4 : params->quick_push (seq_cst);
18618 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18619 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18620 : }
18621 :
18622 : /* Create a transaction node. */
18623 :
18624 : tree
18625 135 : c_finish_transaction (location_t loc, tree block, int flags)
18626 : {
18627 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18628 135 : if (flags & TM_STMT_ATTR_OUTER)
18629 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18630 135 : if (flags & TM_STMT_ATTR_RELAXED)
18631 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18632 135 : return add_stmt (stmt);
18633 : }
18634 :
18635 : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18636 : down to the element type of an array. If ORIG_QUAL_TYPE is not
18637 : NULL, then it should be used as the qualified type
18638 : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18639 : preserve information about the typedef name from which an array
18640 : type was derived). */
18641 :
18642 : tree
18643 74577507 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18644 : size_t orig_qual_indirect)
18645 : {
18646 74577507 : if (type == error_mark_node)
18647 : return type;
18648 :
18649 74577355 : if (TREE_CODE (type) == ARRAY_TYPE)
18650 : {
18651 3163279 : tree t;
18652 3163279 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18653 : type_quals, orig_qual_type,
18654 : orig_qual_indirect - 1);
18655 :
18656 : /* See if we already have an identically qualified type. */
18657 3163279 : if (orig_qual_type && orig_qual_indirect == 0)
18658 : t = orig_qual_type;
18659 : else
18660 4605818 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18661 : {
18662 4466146 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18663 3044491 : && TYPE_NAME (t) == TYPE_NAME (type)
18664 3023566 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18665 7489712 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18666 3023566 : TYPE_ATTRIBUTES (type)))
18667 : break;
18668 : }
18669 3163238 : if (!t)
18670 : {
18671 139672 : tree domain = TYPE_DOMAIN (type);
18672 :
18673 139672 : t = build_variant_type_copy (type);
18674 139672 : TREE_TYPE (t) = element_type;
18675 139672 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18676 :
18677 139672 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18678 139672 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18679 196 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18680 139476 : else if (TYPE_CANONICAL (element_type) != element_type
18681 139476 : || (domain && TYPE_CANONICAL (domain) != domain))
18682 : {
18683 2747 : tree unqualified_canon
18684 5268 : = c_build_array_type (TYPE_CANONICAL (element_type),
18685 2521 : domain ? TYPE_CANONICAL (domain)
18686 : : NULL_TREE);
18687 2747 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18688 : {
18689 0 : unqualified_canon
18690 0 : = build_distinct_type_copy (unqualified_canon);
18691 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18692 : }
18693 2747 : TYPE_CANONICAL (t)
18694 5494 : = c_build_qualified_type (unqualified_canon, type_quals);
18695 : }
18696 : else
18697 136729 : TYPE_CANONICAL (t) = t;
18698 : }
18699 3163279 : return t;
18700 : }
18701 :
18702 : /* A restrict-qualified pointer type must be a pointer to object or
18703 : incomplete type. Note that the use of POINTER_TYPE_P also allows
18704 : REFERENCE_TYPEs, which is appropriate for C++. */
18705 71414076 : if ((type_quals & TYPE_QUAL_RESTRICT)
18706 71414076 : && (!POINTER_TYPE_P (type)
18707 3383407 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18708 : {
18709 3 : error ("invalid use of %<restrict%>");
18710 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18711 : }
18712 :
18713 71414076 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18714 71414076 : ? orig_qual_type
18715 71414045 : : build_qualified_type (type, type_quals));
18716 :
18717 71414076 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18718 : == C_TYPE_VARIABLE_SIZE (type));
18719 71414076 : gcc_checking_assert (c_type_variably_modified_p (var_type)
18720 : == c_type_variably_modified_p (type));
18721 :
18722 : /* A variant type does not inherit the list of incomplete vars from the
18723 : type main variant. */
18724 71414076 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18725 69491835 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18726 71519486 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18727 1413281 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18728 : return var_type;
18729 : }
18730 :
18731 : /* Build a VA_ARG_EXPR for the C parser. */
18732 :
18733 : tree
18734 19888 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type,
18735 : tree type_expr)
18736 : {
18737 19888 : if (error_operand_p (type))
18738 2 : return error_mark_node;
18739 : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18740 : order because it takes the address of the expression. */
18741 19886 : else if (handled_component_p (expr)
18742 31 : && reverse_storage_order_for_component_p (expr))
18743 : {
18744 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18745 0 : return error_mark_node;
18746 : }
18747 19886 : else if (!COMPLETE_TYPE_P (type))
18748 : {
18749 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18750 : "type %qT", type);
18751 11 : return error_mark_node;
18752 : }
18753 19875 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18754 : {
18755 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18756 : type);
18757 1 : return error_mark_node;
18758 : }
18759 6 : else if (TREE_CODE (type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (type)
18760 19877 : && !flag_isoc99)
18761 : {
18762 1 : error_at (loc2, "second argument to %<va_arg%> is an array type %qT",
18763 : type);
18764 1 : return error_mark_node;
18765 : }
18766 19873 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18767 1 : warning_at (loc2, OPT_Wc___compat,
18768 : "C++ requires promoted type, not enum type, in %<va_arg%>");
18769 :
18770 19873 : if (flag_isoc99 && TREE_CODE (type) == ARRAY_TYPE)
18771 : {
18772 3 : warning_at (loc2, 0, "second argument to %<va_arg%> is an array type %qT",
18773 : type);
18774 : /* We create a trap but evaluate side effects first. */
18775 3 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
18776 3 : trapfn = build_call_expr_loc (loc2, trapfn, 0);
18777 3 : tree e2 = build2 (COMPOUND_EXPR, void_type_node, expr, trapfn);
18778 : /* Return a compound literal of the right type. */
18779 3 : tree e1 = build_compound_literal (loc2, type, NULL, true, 0, NULL);
18780 3 : expr = build2 (COMPOUND_EXPR, type, e2, e1);
18781 3 : }
18782 : else
18783 19870 : expr = build_va_arg (loc2, expr, type);
18784 :
18785 19873 : if (type_expr)
18786 26 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), type_expr, expr);
18787 :
18788 : return expr;
18789 : }
18790 :
18791 : /* Return truthvalue of whether T1 is the same tree structure as T2.
18792 : Return 1 if they are the same. Return false if they are different. */
18793 :
18794 : bool
18795 1854 : c_tree_equal (tree t1, tree t2)
18796 : {
18797 1883 : enum tree_code code1, code2;
18798 :
18799 1883 : if (t1 == t2)
18800 : return true;
18801 661 : if (!t1 || !t2)
18802 : return false;
18803 :
18804 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18805 20 : code1 = TREE_CODE (t1))
18806 20 : t1 = TREE_OPERAND (t1, 0);
18807 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18808 21 : code2 = TREE_CODE (t2))
18809 21 : t2 = TREE_OPERAND (t2, 0);
18810 :
18811 : /* They might have become equal now. */
18812 661 : if (t1 == t2)
18813 : return true;
18814 :
18815 640 : if (code1 != code2)
18816 : return false;
18817 :
18818 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18819 : return false;
18820 :
18821 379 : switch (code1)
18822 : {
18823 2 : case INTEGER_CST:
18824 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18825 :
18826 10 : case REAL_CST:
18827 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18828 :
18829 0 : case STRING_CST:
18830 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18831 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18832 0 : TREE_STRING_LENGTH (t1));
18833 :
18834 0 : case FIXED_CST:
18835 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18836 : TREE_FIXED_CST (t2));
18837 :
18838 0 : case COMPLEX_CST:
18839 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18840 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18841 :
18842 0 : case VECTOR_CST:
18843 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18844 :
18845 0 : case CONSTRUCTOR:
18846 : /* We need to do this when determining whether or not two
18847 : non-type pointer to member function template arguments
18848 : are the same. */
18849 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18850 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18851 : return false;
18852 : {
18853 : tree field, value;
18854 : unsigned int i;
18855 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18856 : {
18857 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18858 0 : if (!c_tree_equal (field, elt2->index)
18859 0 : || !c_tree_equal (value, elt2->value))
18860 0 : return false;
18861 : }
18862 : }
18863 : return true;
18864 :
18865 0 : case TREE_LIST:
18866 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18867 : return false;
18868 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18869 : return false;
18870 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18871 :
18872 0 : case SAVE_EXPR:
18873 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18874 :
18875 25 : case CALL_EXPR:
18876 25 : {
18877 25 : tree arg1, arg2;
18878 25 : call_expr_arg_iterator iter1, iter2;
18879 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18880 : return false;
18881 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18882 25 : arg2 = first_call_expr_arg (t2, &iter2);
18883 25 : arg1 && arg2;
18884 0 : arg1 = next_call_expr_arg (&iter1),
18885 0 : arg2 = next_call_expr_arg (&iter2))
18886 0 : if (!c_tree_equal (arg1, arg2))
18887 : return false;
18888 25 : if (arg1 || arg2)
18889 : return false;
18890 : return true;
18891 : }
18892 :
18893 0 : case TARGET_EXPR:
18894 0 : {
18895 0 : tree o1 = TREE_OPERAND (t1, 0);
18896 0 : tree o2 = TREE_OPERAND (t2, 0);
18897 :
18898 : /* Special case: if either target is an unallocated VAR_DECL,
18899 : it means that it's going to be unified with whatever the
18900 : TARGET_EXPR is really supposed to initialize, so treat it
18901 : as being equivalent to anything. */
18902 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18903 0 : && !DECL_RTL_SET_P (o1))
18904 : /*Nop*/;
18905 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18906 0 : && !DECL_RTL_SET_P (o2))
18907 : /*Nop*/;
18908 0 : else if (!c_tree_equal (o1, o2))
18909 : return false;
18910 :
18911 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18912 : }
18913 :
18914 29 : case COMPONENT_REF:
18915 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18916 : return false;
18917 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18918 :
18919 : case PARM_DECL:
18920 : case VAR_DECL:
18921 : case CONST_DECL:
18922 : case FIELD_DECL:
18923 : case FUNCTION_DECL:
18924 : case IDENTIFIER_NODE:
18925 : case SSA_NAME:
18926 : return false;
18927 :
18928 0 : case TREE_VEC:
18929 0 : {
18930 0 : unsigned ix;
18931 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18932 : return false;
18933 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18934 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18935 0 : TREE_VEC_ELT (t2, ix)))
18936 : return false;
18937 : return true;
18938 : }
18939 :
18940 22 : CASE_CONVERT:
18941 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18942 : return false;
18943 : break;
18944 :
18945 : default:
18946 : break;
18947 : }
18948 :
18949 131 : switch (TREE_CODE_CLASS (code1))
18950 : {
18951 131 : case tcc_unary:
18952 131 : case tcc_binary:
18953 131 : case tcc_comparison:
18954 131 : case tcc_expression:
18955 131 : case tcc_vl_exp:
18956 131 : case tcc_reference:
18957 131 : case tcc_statement:
18958 131 : {
18959 131 : int i, n = TREE_OPERAND_LENGTH (t1);
18960 :
18961 131 : switch (code1)
18962 : {
18963 0 : case PREINCREMENT_EXPR:
18964 0 : case PREDECREMENT_EXPR:
18965 0 : case POSTINCREMENT_EXPR:
18966 0 : case POSTDECREMENT_EXPR:
18967 0 : n = 1;
18968 0 : break;
18969 16 : case ARRAY_REF:
18970 16 : n = 2;
18971 16 : break;
18972 : default:
18973 : break;
18974 : }
18975 :
18976 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
18977 131 : && n != TREE_OPERAND_LENGTH (t2))
18978 : return false;
18979 :
18980 305 : for (i = 0; i < n; ++i)
18981 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
18982 : return false;
18983 :
18984 : return true;
18985 : }
18986 :
18987 0 : case tcc_type:
18988 0 : return comptypes (t1, t2);
18989 0 : default:
18990 0 : gcc_unreachable ();
18991 : }
18992 : }
18993 :
18994 : /* Returns true when the function declaration FNDECL is implicit,
18995 : introduced as a result of a call to an otherwise undeclared
18996 : function, and false otherwise. */
18997 :
18998 : bool
18999 2013 : c_decl_implicit (const_tree fndecl)
19000 : {
19001 2013 : return C_DECL_IMPLICIT (fndecl);
19002 : }
|