Line data Source code
1 : /* Build expressions with type checking for C compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 :
21 : /* This file is part of the C front end.
22 : It contains routines to build C expressions given their operands,
23 : including computing the types of the result, C-specific error checks,
24 : and some optimization. */
25 :
26 : #include "config.h"
27 : #include "system.h"
28 : #include "coretypes.h"
29 : #include "memmodel.h"
30 : #include "target.h"
31 : #include "function.h"
32 : #include "bitmap.h"
33 : #include "c-tree.h"
34 : #include "gimple-expr.h"
35 : #include "predict.h"
36 : #include "stor-layout.h"
37 : #include "trans-mem.h"
38 : #include "varasm.h"
39 : #include "stmt.h"
40 : #include "langhooks.h"
41 : #include "c-lang.h"
42 : #include "intl.h"
43 : #include "tree-iterator.h"
44 : #include "gimplify.h"
45 : #include "tree-inline.h"
46 : #include "omp-general.h"
47 : #include "c-family/c-objc.h"
48 : #include "c-family/c-ubsan.h"
49 : #include "gomp-constants.h"
50 : #include "spellcheck-tree.h"
51 : #include "c-family/c-type-mismatch.h"
52 : #include "stringpool.h"
53 : #include "attribs.h"
54 : #include "asan.h"
55 : #include "realmpfr.h"
56 : #include "tree-pretty-print-markup.h"
57 : #include "gcc-urlifier.h"
58 :
59 : /* Possible cases of implicit conversions. Used to select diagnostic messages
60 : and control folding initializers in convert_for_assignment. */
61 : enum impl_conv {
62 : ic_argpass,
63 : ic_assign,
64 : ic_init,
65 : ic_init_const,
66 : ic_return
67 : };
68 :
69 : /* The level of nesting inside "__alignof__". */
70 : int in_alignof;
71 :
72 : /* The level of nesting inside "sizeof". */
73 : int in_sizeof;
74 :
75 : /* The level of nesting inside "countof". */
76 : int in_countof;
77 :
78 : /* The level of nesting inside "typeof". */
79 : int in_typeof;
80 :
81 : /* The level of nesting inside "_Generic". */
82 : int in_generic;
83 :
84 : /* True when parsing OpenMP loop expressions. */
85 : bool c_in_omp_for;
86 :
87 : /* True when parsing OpenMP map clause. */
88 : bool c_omp_array_section_p;
89 :
90 : /* The argument of last parsed sizeof expression, only to be tested
91 : if expr.original_code == SIZEOF_EXPR. */
92 : tree c_last_sizeof_arg;
93 : location_t c_last_sizeof_loc;
94 :
95 : /* Nonzero if we might need to print a "missing braces around
96 : initializer" message within this initializer. */
97 : static int found_missing_braces;
98 :
99 : static bool require_constant_value;
100 : static bool require_constant_elements;
101 : static bool require_constexpr_value;
102 :
103 : static tree qualify_type (tree, tree);
104 : struct comptypes_data;
105 : static bool tagged_types_tu_compatible_p (const_tree, const_tree,
106 : struct comptypes_data *);
107 : static bool comp_target_types (location_t, tree, tree);
108 : static bool function_types_compatible_p (const_tree, const_tree,
109 : struct comptypes_data *);
110 : static bool type_lists_compatible_p (const_tree, const_tree,
111 : struct comptypes_data *);
112 : static int convert_arguments (location_t, vec<location_t>, tree,
113 : vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
114 : tree);
115 : static tree pointer_diff (location_t, tree, tree, tree *);
116 : static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
117 : enum impl_conv, bool, tree, tree, int,
118 : int = 0);
119 : static tree valid_compound_expr_initializer (tree, tree);
120 : static void push_string (const char *);
121 : static void push_member_name (tree);
122 : static int spelling_length (void);
123 : static char *print_spelling (char *);
124 : static void warning_init (location_t, int, const char *);
125 : static tree digest_init (location_t, tree, tree, tree, tree, bool, bool, bool,
126 : bool, bool, bool);
127 : static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
128 : bool, struct obstack *);
129 : static void output_pending_init_elements (int, struct obstack *);
130 : static bool set_designator (location_t, bool, struct obstack *);
131 : static void push_range_stack (tree, struct obstack *);
132 : static void add_pending_init (location_t, tree, tree, tree, bool,
133 : struct obstack *);
134 : static void set_nonincremental_init (struct obstack *);
135 : static void set_nonincremental_init_from_string (tree, struct obstack *);
136 : static tree find_init_member (tree, struct obstack *);
137 : static void readonly_warning (tree, enum lvalue_use);
138 : static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
139 : static void record_maybe_used_decl (tree, bool address);
140 : static bool comptypes_internal (const_tree, const_tree,
141 : struct comptypes_data *data);
142 : static bool comptypes_check_for_composite (tree t1, tree t2);
143 : static bool handle_counted_by_p (tree);
144 : static bool top_array_vla_p (const_tree type);
145 :
146 :
147 : /* Return true if EXP is a null pointer constant, false otherwise. */
148 :
149 : bool
150 188810870 : null_pointer_constant_p (const_tree expr)
151 : {
152 : /* This should really operate on c_expr structures, but they aren't
153 : yet available everywhere required. */
154 188810870 : tree type = TREE_TYPE (expr);
155 :
156 : /* An integer constant expression with the value 0, such an expression
157 : cast to type void*, or the predefined constant nullptr, are a null
158 : pointer constant. */
159 188810870 : if (expr == nullptr_node)
160 : return true;
161 :
162 188810186 : return (TREE_CODE (expr) == INTEGER_CST
163 19135502 : && !TREE_OVERFLOW (expr)
164 19135405 : && integer_zerop (expr)
165 192755987 : && (INTEGRAL_TYPE_P (type)
166 338400 : || (TREE_CODE (type) == POINTER_TYPE
167 338398 : && VOID_TYPE_P (TREE_TYPE (type))
168 263093 : && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
169 : }
170 :
171 : /* EXPR may appear in an unevaluated part of an integer constant
172 : expression, but not in an evaluated part. Wrap it in a
173 : C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
174 : INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
175 :
176 : static tree
177 62368 : note_integer_operands (tree expr)
178 : {
179 62368 : tree ret;
180 62368 : if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
181 : {
182 22 : ret = copy_node (expr);
183 22 : TREE_OVERFLOW (ret) = 1;
184 : }
185 : else
186 : {
187 62346 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
188 62346 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
189 : }
190 62368 : return ret;
191 : }
192 :
193 : /* Having checked whether EXPR may appear in an unevaluated part of an
194 : integer constant expression and found that it may, remove any
195 : C_MAYBE_CONST_EXPR noting this fact and return the resulting
196 : expression. */
197 :
198 : static inline tree
199 33871363 : remove_c_maybe_const_expr (tree expr)
200 : {
201 33871363 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
202 61517 : return C_MAYBE_CONST_EXPR_EXPR (expr);
203 : else
204 : return expr;
205 : }
206 :
207 : /* This is a cache to hold if two types are seen. */
208 :
209 : struct tagged_tu_seen_cache {
210 : const struct tagged_tu_seen_cache * next;
211 : const_tree t1;
212 : const_tree t2;
213 : };
214 :
215 : /* Do `exp = require_complete_type (loc, exp);' to make sure exp
216 : does not have an incomplete type. (That includes void types.)
217 : LOC is the location of the use. */
218 :
219 : tree
220 774590191 : require_complete_type (location_t loc, tree value)
221 : {
222 774590191 : tree type = TREE_TYPE (value);
223 :
224 774590191 : if (error_operand_p (value))
225 8987 : return error_mark_node;
226 :
227 : /* First, detect a valid value with a complete type. */
228 774581204 : if (COMPLETE_TYPE_P (type))
229 : return value;
230 :
231 164 : c_incomplete_type_error (loc, value, type);
232 164 : return error_mark_node;
233 : }
234 :
235 : /* Print an error message for invalid use of an incomplete type.
236 : VALUE is the expression that was used (or 0 if that isn't known)
237 : and TYPE is the type that was invalid. LOC is the location for
238 : the error. */
239 :
240 : void
241 232 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
242 : {
243 : /* Avoid duplicate error message. */
244 232 : if (TREE_CODE (type) == ERROR_MARK)
245 : return;
246 :
247 232 : if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
248 98 : error_at (loc, "%qD has an incomplete type %qT", value, type);
249 : else
250 : {
251 134 : retry:
252 : /* We must print an error message. Be clever about what it says. */
253 :
254 134 : switch (TREE_CODE (type))
255 : {
256 90 : case RECORD_TYPE:
257 90 : case UNION_TYPE:
258 90 : case ENUMERAL_TYPE:
259 90 : break;
260 :
261 38 : case VOID_TYPE:
262 38 : error_at (loc, "invalid use of void expression");
263 38 : return;
264 :
265 6 : case ARRAY_TYPE:
266 6 : if (TYPE_DOMAIN (type))
267 : {
268 3 : if (flexible_array_member_type_p (type))
269 : {
270 3 : error_at (loc, "invalid use of flexible array member");
271 3 : return;
272 : }
273 0 : type = TREE_TYPE (type);
274 0 : goto retry;
275 : }
276 3 : error_at (loc, "invalid use of array with unspecified bounds");
277 3 : return;
278 :
279 0 : default:
280 0 : gcc_unreachable ();
281 : }
282 :
283 90 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
284 88 : error_at (loc, "invalid use of undefined type %qT", type);
285 : else
286 : /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
287 2 : error_at (loc, "invalid use of incomplete typedef %qT", type);
288 : }
289 : }
290 :
291 : /* Given a type, apply default promotions wrt unnamed function
292 : arguments and return the new type. */
293 :
294 : tree
295 125238205 : c_type_promotes_to (tree type)
296 : {
297 125238205 : tree ret = NULL_TREE;
298 :
299 125238205 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
300 1617604 : ret = double_type_node;
301 123620601 : else if (c_promoting_integer_type_p (type))
302 : {
303 : /* Preserve unsignedness if not really getting any wider. */
304 18672548 : if (TYPE_UNSIGNED (type)
305 18672548 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
306 0 : ret = unsigned_type_node;
307 : else
308 18672548 : ret = integer_type_node;
309 : }
310 :
311 20290152 : if (ret != NULL_TREE)
312 20290152 : return (TYPE_ATOMIC (type)
313 20290152 : ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
314 : : ret);
315 :
316 : return type;
317 : }
318 :
319 : /* Return true if between two named address spaces, whether there is a superset
320 : named address space that encompasses both address spaces. If there is a
321 : superset, return which address space is the superset. */
322 :
323 : static bool
324 7621525 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
325 : {
326 7621525 : if (as1 == as2)
327 : {
328 7621525 : *common = as1;
329 7621525 : return true;
330 : }
331 0 : else if (targetm.addr_space.subset_p (as1, as2))
332 : {
333 0 : *common = as2;
334 0 : return true;
335 : }
336 0 : else if (targetm.addr_space.subset_p (as2, as1))
337 : {
338 0 : *common = as1;
339 0 : return true;
340 : }
341 : else
342 : return false;
343 : }
344 :
345 : /* Return a variant of TYPE which has all the type qualifiers of LIKE
346 : as well as those of TYPE. */
347 :
348 : static tree
349 2848412 : qualify_type (tree type, tree like)
350 : {
351 2848412 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
352 2848412 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
353 2848412 : addr_space_t as_common;
354 :
355 : /* If the two named address spaces are different, determine the common
356 : superset address space. If there isn't one, raise an error. */
357 2848412 : if (!addr_space_superset (as_type, as_like, &as_common))
358 : {
359 0 : as_common = as_type;
360 0 : error ("%qT and %qT are in disjoint named address spaces",
361 : type, like);
362 : }
363 :
364 5696824 : return c_build_qualified_type (type,
365 2848412 : TYPE_QUALS_NO_ADDR_SPACE (type)
366 2848412 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
367 2848412 : | ENCODE_QUAL_ADDR_SPACE (as_common));
368 : }
369 :
370 :
371 : /* Check consistency of type TYPE. For derived types, we test that
372 : C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
373 : the requirements of the base type. We also check that arrays with a
374 : non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
375 : inconsistency is detected false is returned and true otherwise. */
376 :
377 : static bool
378 141437565 : c_verify_type (tree type)
379 : {
380 141437565 : switch (TREE_CODE (type))
381 : {
382 67356110 : case POINTER_TYPE:
383 67356110 : case FUNCTION_TYPE:
384 67356110 : case ARRAY_TYPE:
385 : /* Pointer, array, functions are variably modified if and only if the
386 : target, element, return type is variably modified. */
387 67356110 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
388 67356110 : && (C_TYPE_VARIABLY_MODIFIED (type)
389 66969943 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))))
390 : return false;
391 : /* If the target type is structural equality, the type should also. */
392 67356110 : if (!TYPE_STRUCTURAL_EQUALITY_P (type)
393 67356110 : && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (type)))
394 : return false;
395 141437565 : default:
396 141437565 : break;
397 : }
398 :
399 141437565 : switch (TREE_CODE (type))
400 : {
401 66436290 : case POINTER_TYPE:
402 66436290 : case FUNCTION_TYPE:
403 : /* Pointer and functions can not have variable size. */
404 66436290 : if (C_TYPE_VARIABLE_SIZE (type))
405 : return false;
406 : break;
407 919820 : case ARRAY_TYPE:
408 : /* An array has variable size if and only if it has a non-constant
409 : dimensions or its element type has variable size. */
410 919820 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
411 919820 : || top_array_vla_p (type))
412 919820 : != C_TYPE_VARIABLE_SIZE (type))
413 : return false;
414 : /* If the element type or the array has variable size, then the
415 : array has variable size and is variably modified. */
416 919820 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
417 919820 : || C_TYPE_VARIABLE_SIZE (type))
418 : {
419 27693 : if (!C_TYPE_VARIABLE_SIZE (type))
420 : return false;
421 27693 : if (!C_TYPE_VARIABLY_MODIFIED (type))
422 : return false;
423 : }
424 :
425 : /* va_list violates this, accept such types here. */
426 919820 : if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
427 : return true;
428 :
429 919816 : if (COMPLETE_TYPE_P (type))
430 : {
431 : /* If the size is unknown, it can not be complete. */
432 911609 : if (NULL_TREE == TYPE_DOMAIN (type))
433 : return false;
434 :
435 : /* Zero-length arrays must have size zero. */
436 911609 : if (zero_length_array_type_p (type)
437 911609 : && !integer_zerop (TYPE_SIZE (type)))
438 : return false;
439 : }
440 : else
441 : {
442 : /* If not complete but has a domain, it must be a FAM. */
443 8207 : if (NULL_TREE != TYPE_DOMAIN (type)
444 8207 : && !flexible_array_member_type_p (type))
445 : return false;
446 : }
447 : default:
448 : break;
449 : }
450 : return true;
451 : }
452 :
453 : /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
454 : from a base type to a newly built derived or qualified type. */
455 :
456 : static tree
457 134408147 : c_set_type_bits (tree new_type, tree old_type)
458 : {
459 134408147 : gcc_checking_assert (c_verify_type (old_type));
460 :
461 134408147 : if (c_type_variably_modified_p (old_type))
462 28342 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
463 :
464 134408147 : if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
465 : {
466 16808 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
467 16808 : C_TYPE_VARIABLE_SIZE (new_type) = true;
468 : }
469 134408147 : return new_type;
470 : }
471 :
472 : /* Build a pointer type using the default pointer mode. */
473 :
474 : tree
475 71742670 : c_build_pointer_type (tree to_type)
476 : {
477 71742670 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
478 71742670 : : TYPE_ADDR_SPACE (to_type);
479 71742670 : machine_mode pointer_mode;
480 :
481 71742670 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
482 71742670 : pointer_mode = targetm.addr_space.pointer_mode (as);
483 : else
484 : pointer_mode = c_default_pointer_mode;
485 :
486 71742670 : return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
487 : }
488 :
489 : /* Build a pointer type using the given mode. */
490 :
491 : tree
492 71991165 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
493 : {
494 71991165 : tree ret = build_pointer_type_for_mode (type, mode, m);
495 71991165 : return c_set_type_bits (ret, type);
496 : }
497 :
498 : /* Build a function type. */
499 :
500 : tree
501 53829345 : c_build_function_type (tree type, tree args, bool no)
502 : {
503 53829345 : tree ret = build_function_type (type, args, no);
504 53829345 : return c_set_type_bits (ret, type);
505 : }
506 :
507 : /* Build an array type. This sets typeless storage as required
508 : by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
509 : based on the element type and domain. */
510 :
511 : tree
512 1159671 : c_build_array_type (tree type, tree domain)
513 : {
514 1159671 : int type_quals = TYPE_QUALS (type);
515 :
516 : /* Identify typeless storage as introduced in C2Y
517 : and supported also in earlier language modes. */
518 1722984 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
519 1159671 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
520 :
521 1159671 : tree ret = build_array_type (type, domain, typeless);
522 :
523 1159671 : if (top_array_vla_p (ret))
524 : {
525 24270 : C_TYPE_VARIABLE_SIZE (ret) = 1;
526 24270 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
527 : }
528 :
529 1159671 : return c_set_type_bits (ret, type);
530 : }
531 :
532 : /* Build an array type of unspecified size. */
533 : tree
534 151 : c_build_array_type_unspecified (tree type)
535 : {
536 151 : tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
537 : integer_zero_node, size_zero_node);
538 151 : return c_build_array_type (type, build_index_type (upper));
539 : }
540 :
541 : /* Build an array type of zero size. */
542 : tree
543 2609 : c_build_array_type_zero_size (tree type)
544 : {
545 : /* The GCC extension for zero-length arrays differs from
546 : ISO flexible array members in that sizeof yields
547 : zero. */
548 2609 : type = c_build_array_type (type, build_index_type (NULL_TREE));
549 2609 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
550 2609 : TYPE_SIZE (type) = bitsize_zero_node;
551 2609 : TYPE_SIZE_UNIT (type) = size_zero_node;
552 2609 : SET_TYPE_STRUCTURAL_EQUALITY (type);
553 2609 : return type;
554 : }
555 :
556 : tree
557 7427966 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
558 : {
559 7427966 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
560 7427966 : return c_set_type_bits (ret, type);
561 : }
562 :
563 : tree
564 7238437 : c_build_type_attribute_variant (tree type, tree attrs)
565 : {
566 7238437 : return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
567 : }
568 :
569 : /* Reconstruct a complex derived type. This is used to re-construct types
570 : with the vector attribute. It is called via a langhook. */
571 :
572 : tree
573 841449 : c_reconstruct_complex_type (tree type, tree bottom)
574 : {
575 841449 : tree inner, outer;
576 :
577 841449 : if (TREE_CODE (type) == POINTER_TYPE)
578 : {
579 175156 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
580 175156 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
581 175156 : TYPE_REF_CAN_ALIAS_ALL (type));
582 : }
583 : else if (TREE_CODE (type) == ARRAY_TYPE)
584 : {
585 7395 : bool rso = TYPE_REVERSE_STORAGE_ORDER (type);
586 7395 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
587 7395 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
588 7395 : if (rso)
589 : {
590 1 : outer = build_distinct_type_copy (outer);
591 1 : TYPE_REVERSE_STORAGE_ORDER (outer) = 1;
592 : }
593 :
594 7395 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
595 : == C_TYPE_VARIABLE_SIZE (outer));
596 : }
597 : else if (TREE_CODE (type) == FUNCTION_TYPE)
598 : {
599 602 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
600 602 : outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
601 602 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
602 : }
603 : else if (TREE_CODE (type) == REFERENCE_TYPE
604 : || TREE_CODE (type) == OFFSET_TYPE)
605 0 : gcc_unreachable ();
606 : else
607 : return bottom;
608 :
609 183153 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
610 366306 : TYPE_QUALS (type));
611 : }
612 :
613 :
614 : /* Helper function for c_canonical_type. Check whether FIELD
615 : contains a pointer to a structure or union with tag,
616 : possibly nested in other type derivations, and return the
617 : type of this nested structure or union. */
618 : static tree
619 3443117 : ptr_to_tagged_member (tree field)
620 : {
621 3443117 : gcc_assert (FIELD_DECL == TREE_CODE (field));
622 3443117 : tree type = TREE_TYPE (field);
623 3443117 : bool ptr_seen = false;
624 3443117 : while (TREE_CODE (type) == ARRAY_TYPE
625 : || TREE_CODE (type) == FUNCTION_TYPE
626 4720656 : || TREE_CODE (type) == POINTER_TYPE)
627 : {
628 1277539 : if (TREE_CODE (type) == POINTER_TYPE)
629 700845 : ptr_seen = true;
630 1277539 : type = TREE_TYPE (type);
631 : }
632 :
633 3443117 : if (ptr_seen
634 680548 : && RECORD_OR_UNION_TYPE_P (type)
635 3691886 : && NULL_TREE != c_type_tag (type))
636 : return type;
637 :
638 : return NULL_TREE;
639 : }
640 :
641 : /* For a record or union type, make necessary adaptations so that the
642 : type can be used as TYPE_CANONICAL.
643 :
644 : If the TYPE contains a pointer (possibly nested in other type
645 : derivations) to a structure or union as a member, create a copy and
646 : change the nested type to an incomplete type. Otherwise, the middle-end
647 : gets confused when recording component aliases in the case where we
648 : have formed equivalency classes that include types for which these
649 : member pointers end up pointing to other structure or unions types
650 : which have the same tag but are not compatible. */
651 : tree
652 887759 : c_type_canonical (tree type)
653 : {
654 887759 : gcc_assert (RECORD_OR_UNION_TYPE_P (type));
655 :
656 887759 : bool needs_mod = false;
657 3635149 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
658 : {
659 2829201 : if (!ptr_to_tagged_member (x))
660 2747390 : continue;
661 : needs_mod = true;
662 : break;
663 : }
664 887759 : if (!needs_mod)
665 : return type;
666 :
667 : /* Construct new version with such members replaced. */
668 81811 : tree n = build_distinct_type_copy (type);
669 81811 : tree *fields = &TYPE_FIELDS (n);
670 695727 : for (tree x = TYPE_FIELDS (type); x; x = DECL_CHAIN (x))
671 : {
672 613916 : tree f = copy_node (x);
673 613916 : if (tree m = ptr_to_tagged_member (x))
674 : {
675 162803 : tree new_node = make_node (TREE_CODE (m));
676 162803 : TYPE_NAME (new_node) = TYPE_NAME (m);
677 162803 : SET_TYPE_STRUCTURAL_EQUALITY (new_node);
678 162803 : new_node = qualify_type (new_node, m);
679 162803 : TREE_TYPE (f) = c_reconstruct_complex_type (TREE_TYPE (x),
680 : new_node);
681 : }
682 613916 : *fields = f;
683 613916 : fields = &DECL_CHAIN (f);
684 : }
685 81811 : TYPE_CANONICAL (n) = n;
686 81811 : return n;
687 : }
688 :
689 :
690 : /* If NTYPE is a type of a non-variadic function with a prototype
691 : and OTYPE is a type of a function without a prototype and ATTRS
692 : contains attribute format, diagnosess and removes it from ATTRS.
693 : Returns the result of build_type_attribute_variant of NTYPE and
694 : the (possibly) modified ATTRS. */
695 :
696 : static tree
697 10765 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
698 : {
699 10765 : if (!prototype_p (otype)
700 10753 : && prototype_p (ntype)
701 21488 : && lookup_attribute ("format", attrs))
702 : {
703 14 : warning_at (input_location, OPT_Wattributes,
704 : "%qs attribute can only be applied to variadic functions",
705 : "format");
706 14 : attrs = remove_attribute ("format", attrs);
707 : }
708 10765 : return c_build_type_attribute_variant (ntype, attrs);
709 :
710 : }
711 :
712 : /* Given a type which could be a typedef name, make sure to return the
713 : original type. See set_underlying_type. */
714 : static const_tree
715 62500781 : c_type_original (const_tree t)
716 : {
717 : /* It may even be a typedef of a typedef...
718 : In the case of compiler-created builtin structs the TYPE_DECL
719 : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
720 62500781 : while (TYPE_NAME (t)
721 32137745 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
722 62545021 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
723 22118 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
724 62500781 : return t;
725 : }
726 :
727 : /* Return the tag for a tagged type. */
728 : tree
729 62500781 : c_type_tag (const_tree t)
730 : {
731 62500781 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
732 62500781 : const_tree orig = c_type_original (t);
733 62500781 : tree name = TYPE_NAME (orig);
734 62500781 : if (!name)
735 : return NULL_TREE;
736 32115627 : if (TREE_CODE (name) == TYPE_DECL)
737 : {
738 4 : if (!DECL_NAME (name))
739 : return NULL_TREE;
740 2 : name = DECL_NAME (name);
741 : }
742 32115625 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
743 : return name;
744 : }
745 :
746 : /* Remove qualifiers but not atomic. For arrays remove qualifiers
747 : on the element type but also do not remove atomic. */
748 : static tree
749 63885047 : remove_qualifiers (tree t)
750 : {
751 63885047 : if (!t || t == error_mark_node)
752 : return t;
753 63885022 : return TYPE_ATOMIC (strip_array_types (t))
754 63885022 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
755 63869447 : : TYPE_MAIN_VARIANT (t);
756 : }
757 :
758 :
759 : /* Helper function for composite_type_internal. Find a compatible type
760 : in a (transparent) union U compatible to T. If found, return the
761 : type of the corresponding member. Otherwise, return the union type U. */
762 : static tree
763 8444780 : transparent_union_replacement (tree u, tree t)
764 : {
765 8444775 : if (u == error_mark_node || t == error_mark_node
766 8444770 : || TREE_CODE (u) != UNION_TYPE
767 64 : || !(TYPE_TRANSPARENT_AGGR (u) || TYPE_NAME (u) == NULL_TREE)
768 8444844 : || comptypes (u, t))
769 8444716 : return u;
770 :
771 80 : for (tree memb = TYPE_FIELDS (u); memb; memb = DECL_CHAIN (memb))
772 : {
773 80 : tree m = remove_qualifiers (TREE_TYPE (memb));
774 80 : if (comptypes (m, t))
775 : {
776 64 : pedwarn (input_location, OPT_Wpedantic,
777 : "function types not truly compatible in ISO C");
778 64 : return m;
779 : }
780 : }
781 :
782 : return u;
783 : }
784 :
785 :
786 :
787 : /* Return the composite type of two compatible types.
788 :
789 : We assume that comptypes has already been done and returned
790 : nonzero; if that isn't so, this may crash. In particular, we
791 : assume that qualifiers match. */
792 :
793 : struct composite_cache {
794 : tree t1;
795 : tree t2;
796 : tree composite;
797 : struct composite_cache* next;
798 : };
799 :
800 : tree
801 13368006 : composite_type_internal (tree t1, tree t2, tree cond,
802 : struct composite_cache* cache)
803 : {
804 13368006 : enum tree_code code1;
805 13368006 : enum tree_code code2;
806 13368006 : tree attributes;
807 :
808 : /* Save time if the two types are the same. */
809 :
810 13368006 : if (t1 == t2) return t1;
811 :
812 : /* If one type is nonsense, use the other. */
813 2720552 : if (t1 == error_mark_node)
814 : return t2;
815 2720548 : if (t2 == error_mark_node)
816 : return t1;
817 :
818 2720546 : code1 = TREE_CODE (t1);
819 2720546 : code2 = TREE_CODE (t2);
820 :
821 : /* Merge the attributes. */
822 2720546 : attributes = targetm.merge_type_attributes (t1, t2);
823 :
824 : /* If one is an enumerated type and the other is the compatible
825 : integer type, the composite type might be either of the two
826 : (DR#013 question 3). For consistency, use the enumerated type as
827 : the composite type. */
828 :
829 2720546 : if (code1 == ENUMERAL_TYPE
830 194 : && (code2 == INTEGER_TYPE
831 194 : || code2 == BOOLEAN_TYPE))
832 : return t1;
833 2720352 : if (code2 == ENUMERAL_TYPE
834 69 : && (code1 == INTEGER_TYPE
835 69 : || code1 == BOOLEAN_TYPE))
836 : return t2;
837 :
838 2720283 : gcc_assert (code1 == code2);
839 :
840 2720283 : switch (code1)
841 : {
842 6376 : case POINTER_TYPE:
843 : /* For two pointers, do this recursively on the target type. */
844 6376 : {
845 6376 : gcc_checking_assert (TYPE_QUALS (t1) == TYPE_QUALS (t2));
846 6376 : tree target = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
847 : cond, cache);
848 6376 : tree n = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
849 12752 : return c_build_type_attribute_qual_variant (n, attributes,
850 6376 : TYPE_QUALS (t2));
851 : }
852 :
853 10996 : case ARRAY_TYPE:
854 10996 : {
855 10996 : tree d1 = TYPE_DOMAIN (t1);
856 10996 : tree d2 = TYPE_DOMAIN (t2);
857 :
858 : /* We should not have any type quals on arrays at all. */
859 10996 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
860 : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
861 :
862 10996 : bool d1_zero = zero_length_array_type_p (t1)
863 10996 : || flexible_array_member_type_p (t1);
864 10996 : bool d2_zero = zero_length_array_type_p (t2)
865 10996 : || flexible_array_member_type_p (t2);
866 :
867 10996 : bool d1_variable = top_array_vla_p (t1);
868 10996 : bool d2_variable = top_array_vla_p (t2);
869 :
870 10996 : bool use1 = d1 && (d2_variable || !d2 || d2_zero || !d1_variable);
871 10996 : bool use2 = d2 && (d1_variable || !d1 || d1_zero || !d2_variable);
872 :
873 : /* If the first is an unspecified size pick the other one. */
874 10996 : if (d2_variable && c_type_unspecified_p (t1))
875 : {
876 29 : gcc_assert (use1 && use2);
877 : use1 = false;
878 : }
879 :
880 : /* If both are VLAs but not unspecified and we are in the
881 : conditional operator, we create a conditional to select
882 : the size of the active branch. */
883 232 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
884 11225 : && d2_variable && !c_type_unspecified_p (t2);
885 :
886 10996 : tree td;
887 10996 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
888 : cond, cache);
889 :
890 10996 : if (!use0)
891 : {
892 : /* Save space: see if the result is identical to one of the args. */
893 10879 : if (elt == TREE_TYPE (t1) && use1)
894 10088 : return c_build_type_attribute_variant (t1, attributes);
895 791 : if (elt == TREE_TYPE (t2) && use2)
896 673 : return c_build_type_attribute_variant (t2, attributes);
897 :
898 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
899 10 : return c_build_type_attribute_variant (t1, attributes);
900 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
901 2 : return c_build_type_attribute_variant (t2, attributes);
902 :
903 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
904 : }
905 : else
906 : {
907 : /* Not used in C. */
908 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
909 117 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
910 :
911 117 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
912 117 : cond, TYPE_MAX_VALUE (d1),
913 117 : TYPE_MAX_VALUE (d2));
914 :
915 117 : td = build_index_type (d);
916 : }
917 :
918 : /* Merge the element types, and have a size if either arg has
919 : one. We may have qualifiers on the element types. To set
920 : up TYPE_MAIN_VARIANT correctly, we need to form the
921 : composite of the unqualified types and add the qualifiers
922 : back at the end. */
923 223 : int quals = TYPE_QUALS (strip_array_types (elt));
924 223 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
925 :
926 223 : if ((!d1 || d1_zero) && (!d2 || d2_zero) && (d1 || d2))
927 1 : t1 = c_build_array_type_zero_size (unqual_elt);
928 : else
929 222 : t1 = c_build_array_type (unqual_elt, td);
930 :
931 : /* Check that a type which has a varying outermost dimension
932 : got marked as having a variable size. */
933 223 : bool varsize = (d1_variable && d2_variable)
934 97 : || (d1_variable && !d2)
935 309 : || (d2_variable && !d1);
936 157 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
937 :
938 223 : t1 = c_build_qualified_type (t1, quals);
939 223 : return c_build_type_attribute_variant (t1, attributes);
940 : }
941 :
942 253 : case RECORD_TYPE:
943 253 : case UNION_TYPE:
944 253 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
945 : {
946 : /* Go to the original type to get the right tag. */
947 150 : tree tag = c_type_tag (t1);
948 :
949 150 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
950 150 : gcc_checking_assert (!tag || comptypes (t1, t2));
951 :
952 : /* If a composite type for these two types is already under
953 : construction, return it. */
954 :
955 376 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
956 246 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
957 20 : return c->composite;
958 :
959 : /* Otherwise, create a new type node and link it into the cache. */
960 :
961 130 : tree n = make_node (code1);
962 130 : SET_TYPE_STRUCTURAL_EQUALITY (n);
963 130 : TYPE_NAME (n) = tag;
964 :
965 130 : struct composite_cache cache2 = { t1, t2, n, cache };
966 130 : cache = &cache2;
967 :
968 130 : tree f1 = TYPE_FIELDS (t1);
969 130 : tree f2 = TYPE_FIELDS (t2);
970 130 : tree fields = NULL_TREE;
971 :
972 321 : for (tree a = f1, b = f2; a && b;
973 191 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
974 : {
975 191 : tree ta = TREE_TYPE (a);
976 191 : tree tb = TREE_TYPE (b);
977 :
978 191 : if (DECL_C_BIT_FIELD (a))
979 : {
980 19 : ta = DECL_BIT_FIELD_TYPE (a);
981 19 : tb = DECL_BIT_FIELD_TYPE (b);
982 : }
983 :
984 191 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
985 191 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
986 :
987 191 : tree t = composite_type_internal (ta, tb, cond, cache);
988 191 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
989 :
990 191 : DECL_PACKED (f) = DECL_PACKED (a);
991 191 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
992 191 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
993 191 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
994 :
995 191 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
996 :
997 191 : finish_decl (f, input_location, NULL, NULL, NULL);
998 :
999 191 : if (DECL_C_BIT_FIELD (a))
1000 : {
1001 : /* This will be processed by finish_struct. */
1002 19 : SET_DECL_C_BIT_FIELD (f);
1003 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
1004 19 : tree_to_uhwi (DECL_SIZE (a)));
1005 19 : DECL_NONADDRESSABLE_P (f) = true;
1006 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
1007 : }
1008 :
1009 191 : DECL_CHAIN (f) = fields;
1010 191 : fields = f;
1011 : }
1012 :
1013 130 : fields = nreverse (fields);
1014 :
1015 : /* Setup the struct/union type. Because we inherit all variably
1016 : modified components, we can ignore the size expression. */
1017 130 : tree expr = NULL_TREE;
1018 :
1019 : /* Set TYPE_STUB_DECL for debugging symbols. */
1020 130 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
1021 : NULL_TREE, n));
1022 :
1023 130 : n = finish_struct (input_location, n, fields, attributes, NULL,
1024 : &expr);
1025 :
1026 130 : return qualify_type (n, t1);
1027 : }
1028 : /* FALLTHRU */
1029 103 : case ENUMERAL_TYPE:
1030 103 : if (attributes != NULL)
1031 : {
1032 : /* Try harder not to create a new aggregate type. */
1033 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
1034 : return t1;
1035 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
1036 : return t2;
1037 : }
1038 99 : return c_build_type_attribute_variant (t1, attributes);
1039 :
1040 2661009 : case FUNCTION_TYPE:
1041 : /* Function types: prefer the one that specified arg types.
1042 : If both do, merge the arg types. Also merge the return types. */
1043 2661009 : {
1044 2661009 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1045 : cond, cache);
1046 2661009 : tree p1 = TYPE_ARG_TYPES (t1);
1047 2661009 : tree p2 = TYPE_ARG_TYPES (t2);
1048 :
1049 : /* Save space: see if the result is identical to one of the args. */
1050 2661009 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
1051 10775 : return c_build_functype_attribute_variant (t1, t2, attributes);
1052 2650769 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
1053 525 : return c_build_functype_attribute_variant (t2, t1, attributes);
1054 :
1055 : /* Simple way if one arg fails to specify argument types. */
1056 2650244 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
1057 : {
1058 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
1059 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
1060 9 : t1 = c_build_type_attribute_variant (t1, attributes);
1061 9 : return qualify_type (t1, t2);
1062 : }
1063 2650235 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
1064 : {
1065 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
1066 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
1067 1 : t1 = c_build_type_attribute_variant (t1, attributes);
1068 1 : return qualify_type (t1, t2);
1069 : }
1070 :
1071 : /* If both args specify argument types, we must merge the two
1072 : lists, argument by argument. */
1073 2650234 : tree newargs = NULL_TREE;
1074 2650234 : tree *endp = &newargs;
1075 :
1076 6872624 : for (; p1; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1077 : {
1078 6703944 : if (p1 == void_list_node)
1079 : {
1080 2481554 : *endp = void_list_node;
1081 2481554 : break;
1082 : }
1083 4222390 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
1084 4222390 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
1085 :
1086 4222390 : gcc_assert (mv1);
1087 4222390 : gcc_assert (mv2);
1088 :
1089 4222390 : mv1 = transparent_union_replacement (mv1, mv2);
1090 4222390 : mv2 = transparent_union_replacement (mv2, mv1);
1091 :
1092 4222390 : *endp = tree_cons (NULL_TREE,
1093 : composite_type_internal (mv1, mv2, cond, cache),
1094 : NULL_TREE);
1095 :
1096 4222390 : endp = &TREE_CHAIN (*endp);
1097 : }
1098 :
1099 2650234 : t1 = c_build_function_type (valtype, newargs);
1100 2650234 : t1 = qualify_type (t1, t2);
1101 : }
1102 : /* FALLTHRU */
1103 :
1104 2691883 : default:
1105 2691883 : return c_build_type_attribute_variant (t1, attributes);
1106 : }
1107 : }
1108 :
1109 : tree
1110 6467044 : composite_type_cond (tree t1, tree t2, tree cond)
1111 : {
1112 6467044 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1113 :
1114 6467044 : struct composite_cache cache = { };
1115 6467044 : tree n = composite_type_internal (t1, t2, cond, &cache);
1116 :
1117 6467044 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1118 6467044 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1119 6467044 : return n;
1120 : }
1121 :
1122 :
1123 : tree
1124 6462573 : composite_type (tree t1, tree t2)
1125 : {
1126 6462573 : return composite_type_cond (t1, t2, NULL_TREE);
1127 : }
1128 :
1129 : /* Return the type of a conditional expression between pointers to
1130 : possibly differently qualified versions of compatible types.
1131 :
1132 : We assume that comp_target_types has already been done and returned
1133 : true; if that isn't so, this may crash. */
1134 :
1135 : static tree
1136 116789 : common_pointer_type (tree t1, tree t2, tree cond)
1137 : {
1138 116789 : tree attributes;
1139 116789 : unsigned target_quals;
1140 116789 : addr_space_t as1, as2, as_common;
1141 116789 : int quals1, quals2;
1142 :
1143 : /* Save time if the two types are the same. */
1144 :
1145 116789 : if (t1 == t2) return t1;
1146 :
1147 : /* If one type is nonsense, use the other. */
1148 4471 : if (t1 == error_mark_node)
1149 : return t2;
1150 4471 : if (t2 == error_mark_node)
1151 : return t1;
1152 :
1153 4471 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1154 : && TREE_CODE (t2) == POINTER_TYPE);
1155 :
1156 : /* Merge the attributes. */
1157 4471 : attributes = targetm.merge_type_attributes (t1, t2);
1158 :
1159 : /* Find the composite type of the target types, and combine the
1160 : qualifiers of the two types' targets. */
1161 4471 : tree pointed_to_1 = TREE_TYPE (t1);
1162 4471 : tree pointed_to_2 = TREE_TYPE (t2);
1163 4471 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1164 4471 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1165 :
1166 : /* Strip array types to get correct qualifier for pointers to arrays */
1167 4471 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1168 4471 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1169 :
1170 : /* For function types do not merge const qualifiers, but drop them
1171 : if used inconsistently. The middle-end uses these to mark const
1172 : and noreturn functions. */
1173 4471 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1174 2129 : target_quals = (quals1 & quals2);
1175 : else
1176 2342 : target_quals = (quals1 | quals2);
1177 :
1178 : /* If the two named address spaces are different, determine the common
1179 : superset address space. This is guaranteed to exist due to the
1180 : assumption that comp_target_type returned true. */
1181 4471 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1182 4471 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1183 4471 : if (!addr_space_superset (as1, as2, &as_common))
1184 0 : gcc_unreachable ();
1185 :
1186 4471 : target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1187 :
1188 4471 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1189 4471 : return c_build_type_attribute_variant (t1, attributes);
1190 : }
1191 :
1192 : /* Return the common type for two arithmetic types under the usual
1193 : arithmetic conversions. The default conversions have already been
1194 : applied, and enumerated types converted to their compatible integer
1195 : types. The resulting type is unqualified and has no attributes.
1196 :
1197 : This is the type for the result of most arithmetic operations
1198 : if the operands have the given two types. */
1199 :
1200 : static tree
1201 14022016 : c_common_type (tree t1, tree t2)
1202 : {
1203 14022016 : enum tree_code code1;
1204 14022016 : enum tree_code code2;
1205 :
1206 : /* If one type is nonsense, use the other. */
1207 14022016 : if (t1 == error_mark_node)
1208 : return t2;
1209 14022016 : if (t2 == error_mark_node)
1210 : return t1;
1211 :
1212 14022016 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1213 37982 : t1 = TYPE_MAIN_VARIANT (t1);
1214 :
1215 14022016 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1216 49414 : t2 = TYPE_MAIN_VARIANT (t2);
1217 :
1218 14022016 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1219 : {
1220 188460 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1221 188460 : t1 = c_build_type_attribute_variant (t1, attrs);
1222 : }
1223 :
1224 14022016 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1225 : {
1226 187938 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1227 187938 : t2 = c_build_type_attribute_variant (t2, attrs);
1228 : }
1229 :
1230 : /* Save time if the two types are the same. */
1231 :
1232 14022016 : if (t1 == t2) return t1;
1233 :
1234 2336250 : code1 = TREE_CODE (t1);
1235 2336250 : code2 = TREE_CODE (t2);
1236 :
1237 2336250 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1238 : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1239 : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1240 2336250 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1241 : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1242 : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1243 :
1244 : /* When one operand is a decimal float type, the other operand cannot be
1245 : a generic float type or a complex type. We also disallow vector types
1246 : here. */
1247 2336250 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1248 2338673 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1249 : {
1250 1080 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1251 : {
1252 3 : error ("cannot mix operands of decimal floating and vector types");
1253 3 : return error_mark_node;
1254 : }
1255 1077 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1256 : {
1257 5 : error ("cannot mix operands of decimal floating and complex types");
1258 5 : return error_mark_node;
1259 : }
1260 1072 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1261 : {
1262 16 : error ("cannot mix operands of decimal floating "
1263 : "and other floating types");
1264 16 : return error_mark_node;
1265 : }
1266 : }
1267 :
1268 : /* If one type is a vector type, return that type. (How the usual
1269 : arithmetic conversions apply to the vector types extension is not
1270 : precisely specified.) */
1271 2336226 : if (code1 == VECTOR_TYPE)
1272 : return t1;
1273 :
1274 2335934 : if (code2 == VECTOR_TYPE)
1275 : return t2;
1276 :
1277 : /* If one type is complex, form the common type of the non-complex
1278 : components, then make that complex. Use T1 or T2 if it is the
1279 : required type. */
1280 2335931 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1281 : {
1282 89357 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1283 89357 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1284 89357 : tree subtype = c_common_type (subtype1, subtype2);
1285 :
1286 89357 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1287 : return t1;
1288 59242 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1289 : return t2;
1290 3258 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1291 : {
1292 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1293 4 : TYPE_PRECISION (subtype));
1294 6 : return code1 == COMPLEX_TYPE ? t1 : t2;
1295 : }
1296 : else
1297 3254 : return build_complex_type (subtype);
1298 : }
1299 :
1300 : /* If only one is real, use it as the result. */
1301 :
1302 2246574 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1303 : return t1;
1304 :
1305 2117566 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1306 : return t2;
1307 :
1308 : /* If both are real and either are decimal floating point types, use
1309 : the decimal floating point type with the greater precision. */
1310 :
1311 2086231 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1312 : {
1313 107531 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1314 107531 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1315 : return dfloat128_type_node;
1316 : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1317 : implementation the same mode. */
1318 106982 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1319 106982 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1320 : return dfloat64x_type_node;
1321 106978 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1322 106978 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1323 : return dfloat64_type_node;
1324 106533 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1325 106533 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1326 : return dfloat32_type_node;
1327 : }
1328 :
1329 : /* Deal with fixed-point types. */
1330 2084888 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1331 : {
1332 0 : unsigned int unsignedp = 0, satp = 0;
1333 0 : scalar_mode m1, m2;
1334 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1335 :
1336 0 : m1 = SCALAR_TYPE_MODE (t1);
1337 0 : m2 = SCALAR_TYPE_MODE (t2);
1338 :
1339 : /* If one input type is saturating, the result type is saturating. */
1340 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1341 : satp = 1;
1342 :
1343 : /* If both fixed-point types are unsigned, the result type is unsigned.
1344 : When mixing fixed-point and integer types, follow the sign of the
1345 : fixed-point type.
1346 : Otherwise, the result type is signed. */
1347 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1348 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1349 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1350 0 : && TYPE_UNSIGNED (t1))
1351 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1352 0 : && TYPE_UNSIGNED (t2)))
1353 : unsignedp = 1;
1354 :
1355 : /* The result type is signed. */
1356 0 : if (unsignedp == 0)
1357 : {
1358 : /* If the input type is unsigned, we need to convert to the
1359 : signed type. */
1360 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1361 : {
1362 0 : enum mode_class mclass = (enum mode_class) 0;
1363 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1364 : mclass = MODE_FRACT;
1365 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1366 : mclass = MODE_ACCUM;
1367 : else
1368 0 : gcc_unreachable ();
1369 0 : m1 = as_a <scalar_mode>
1370 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1371 : }
1372 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1373 : {
1374 0 : enum mode_class mclass = (enum mode_class) 0;
1375 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1376 : mclass = MODE_FRACT;
1377 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1378 : mclass = MODE_ACCUM;
1379 : else
1380 0 : gcc_unreachable ();
1381 0 : m2 = as_a <scalar_mode>
1382 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1383 : }
1384 : }
1385 :
1386 0 : if (code1 == FIXED_POINT_TYPE)
1387 : {
1388 0 : fbit1 = GET_MODE_FBIT (m1);
1389 0 : ibit1 = GET_MODE_IBIT (m1);
1390 : }
1391 : else
1392 : {
1393 0 : fbit1 = 0;
1394 : /* Signed integers need to subtract one sign bit. */
1395 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1396 : }
1397 :
1398 0 : if (code2 == FIXED_POINT_TYPE)
1399 : {
1400 0 : fbit2 = GET_MODE_FBIT (m2);
1401 0 : ibit2 = GET_MODE_IBIT (m2);
1402 : }
1403 : else
1404 : {
1405 0 : fbit2 = 0;
1406 : /* Signed integers need to subtract one sign bit. */
1407 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1408 : }
1409 :
1410 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1411 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1412 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1413 : satp);
1414 : }
1415 :
1416 : /* Both real or both integers; use the one with greater precision. */
1417 :
1418 2084888 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1419 : return t1;
1420 1111051 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1421 : return t2;
1422 :
1423 : /* Same precision. Prefer long longs to longs to ints when the
1424 : same precision, following the C99 rules on integer type rank
1425 : (which are equivalent to the C90 rules for C90 types). */
1426 :
1427 745825 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1428 745825 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1429 : return long_long_unsigned_type_node;
1430 :
1431 656573 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1432 656573 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1433 : {
1434 15318 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1435 361 : return long_long_unsigned_type_node;
1436 : else
1437 : return long_long_integer_type_node;
1438 : }
1439 :
1440 641255 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1441 641255 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1442 : return long_unsigned_type_node;
1443 :
1444 542036 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1445 542036 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1446 : {
1447 : /* But preserve unsignedness from the other type,
1448 : since long cannot hold all the values of an unsigned int. */
1449 26453 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1450 129 : return long_unsigned_type_node;
1451 : else
1452 : return long_integer_type_node;
1453 : }
1454 :
1455 : /* For floating types of the same TYPE_PRECISION (which we here
1456 : assume means either the same set of values, or sets of values
1457 : neither a subset of the other, with behavior being undefined in
1458 : the latter case), follow the rules from TS 18661-3: prefer
1459 : interchange types _FloatN, then standard types long double,
1460 : double, float, then extended types _FloatNx. For extended types,
1461 : check them starting with _Float128x as that seems most consistent
1462 : in spirit with preferring long double to double; for interchange
1463 : types, also check in that order for consistency although it's not
1464 : possible for more than one of them to have the same
1465 : precision. */
1466 515583 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1467 515583 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1468 :
1469 2577269 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1470 2061869 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1471 : return FLOATN_TYPE_NODE (i);
1472 :
1473 : /* Likewise, prefer long double to double even if same size. */
1474 515400 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1475 : return long_double_type_node;
1476 :
1477 : /* Likewise, prefer double to float even if same size.
1478 : We got a couple of embedded targets with 32 bit doubles, and the
1479 : pdp11 might have 64 bit floats. */
1480 515142 : if (mv1 == double_type_node || mv2 == double_type_node)
1481 : return double_type_node;
1482 :
1483 514570 : if (mv1 == float_type_node || mv2 == float_type_node)
1484 : return float_type_node;
1485 :
1486 2049540 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1487 1537155 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1488 : return FLOATNX_TYPE_NODE (i);
1489 :
1490 512385 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1491 : {
1492 : /* Prefer any other integral types over bit-precise integer types. */
1493 10 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1494 6 : return code1 == BITINT_TYPE ? t2 : t1;
1495 : /* If BITINT_TYPE is unsigned and the other type is signed
1496 : non-BITINT_TYPE with the same precision, the latter has higher rank.
1497 : In that case:
1498 : Otherwise, both operands are converted to the unsigned integer type
1499 : corresponding to the type of the operand with signed integer type. */
1500 8 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1501 5 : return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1502 : }
1503 :
1504 : /* Otherwise prefer the unsigned one. */
1505 :
1506 512377 : if (TYPE_UNSIGNED (t1))
1507 : return t1;
1508 : else
1509 : return t2;
1510 : }
1511 :
1512 : /* Wrapper around c_common_type that is used by c-common.cc and other
1513 : front end optimizations that remove promotions. ENUMERAL_TYPEs
1514 : are allowed here and are converted to their compatible integer types.
1515 : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1516 : preferably a non-Boolean type as the common type. */
1517 : tree
1518 103961 : common_type (tree t1, tree t2)
1519 : {
1520 103961 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1521 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1522 103961 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1523 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1524 :
1525 : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1526 103961 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1527 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1528 584 : return boolean_type_node;
1529 :
1530 : /* If either type is BOOLEAN_TYPE, then return the other. */
1531 103377 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1532 : return t2;
1533 103377 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1534 : return t1;
1535 :
1536 103377 : return c_common_type (t1, t2);
1537 : }
1538 :
1539 :
1540 :
1541 : /* Helper function for comptypes. For two compatible types, return 1
1542 : if they pass consistency checks. In particular we test that
1543 : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1544 :
1545 : static bool
1546 43058247 : comptypes_verify (tree type1, tree type2)
1547 : {
1548 43058247 : if (type1 == type2 || !type1 || !type2
1549 3514710 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1550 : return true;
1551 :
1552 3514709 : gcc_checking_assert (c_verify_type (type1));
1553 3514709 : gcc_checking_assert (c_verify_type (type2));
1554 :
1555 3514709 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1556 388082 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1557 3901300 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1558 : {
1559 : /* FIXME: check other types. */
1560 386280 : if (RECORD_OR_UNION_TYPE_P (type1)
1561 386280 : || TREE_CODE (type1) == ENUMERAL_TYPE
1562 386280 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1563 : return false;
1564 : }
1565 : return true;
1566 : }
1567 :
1568 : struct comptypes_data {
1569 :
1570 : /* output */
1571 : bool enum_and_int_p;
1572 : bool different_types_p;
1573 : bool warning_needed;
1574 :
1575 : /* context */
1576 : bool anon_field;
1577 : bool pointedto;
1578 :
1579 : /* configuration */
1580 : bool equiv;
1581 : bool ignore_promoting_args;
1582 :
1583 : const struct tagged_tu_seen_cache* cache;
1584 : };
1585 :
1586 :
1587 : /* Helper function for composite_type. This function ignores when the
1588 : function type of an old-style declaration is incompatible with a type
1589 : of a declaration with prototype because some are arguments are not
1590 : self-promoting. This is ignored only for function types but not
1591 : ignored in a nested context. */
1592 :
1593 : static bool
1594 19401132 : comptypes_check_for_composite (tree t1, tree t2)
1595 : {
1596 19401132 : struct comptypes_data data = { };
1597 19401132 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1598 19401132 : return comptypes_internal (t1, t2, &data);
1599 : }
1600 :
1601 :
1602 : /* C implementation of compatible_types_for_indirection_note_p. */
1603 :
1604 : bool
1605 762 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1606 : {
1607 762 : return comptypes (type1, type2);
1608 : }
1609 :
1610 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1611 : or various other operations. */
1612 :
1613 : bool
1614 56592541 : comptypes (tree type1, tree type2)
1615 : {
1616 56592541 : struct comptypes_data data = { };
1617 56592541 : bool ret = comptypes_internal (type1, type2, &data);
1618 :
1619 56592541 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1620 :
1621 56592541 : return ret;
1622 : }
1623 :
1624 :
1625 : /* Like comptypes, but it returns non-zero only for identical
1626 : types. */
1627 :
1628 : bool
1629 46273 : comptypes_same_p (tree type1, tree type2)
1630 : {
1631 46273 : struct comptypes_data data = { };
1632 46273 : bool ret = comptypes_internal (type1, type2, &data);
1633 :
1634 46273 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1635 :
1636 46273 : if (data.different_types_p)
1637 163 : return false;
1638 :
1639 : return ret;
1640 : }
1641 :
1642 :
1643 : /* Like comptypes, but if it returns true because enum and int are
1644 : compatible, it sets *ENUM_AND_INT_P to true. */
1645 :
1646 : bool
1647 5113351 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1648 : {
1649 5113351 : struct comptypes_data data = { };
1650 5113351 : bool ret = comptypes_internal (type1, type2, &data);
1651 5113351 : *enum_and_int_p = data.enum_and_int_p;
1652 :
1653 5113351 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1654 :
1655 5113351 : return ret;
1656 : }
1657 :
1658 :
1659 :
1660 : /* Like comptypes, but if it returns true for struct and union types
1661 : considered equivalent for aliasing purposes, i.e. for setting
1662 : TYPE_CANONICAL after completing a struct or union.
1663 :
1664 : This function must return false only for types which are not
1665 : compatible according to C language semantics (cf. comptypes),
1666 : otherwise the middle-end would make incorrect aliasing decisions.
1667 : It may return true for some similar types that are not compatible
1668 : according to those stricter rules.
1669 :
1670 : In particular, we ignore size expression in arrays so that the
1671 : following structs are in the same equivalence class:
1672 :
1673 : struct foo { char (*buf)[]; };
1674 : struct foo { char (*buf)[3]; };
1675 : struct foo { char (*buf)[4]; };
1676 :
1677 : We also treat unions / structs with members which are pointers to
1678 : structures or unions with the same tag as equivalent (if they are not
1679 : incompatible for other reasons). Although incomplete structure
1680 : or union types are not compatible to any other type, they may become
1681 : compatible to different types when completed. To avoid having to update
1682 : TYPE_CANONICAL at this point, we only consider the tag when forming
1683 : the equivalence classes. For example, the following types with tag
1684 : 'foo' are all considered equivalent:
1685 :
1686 : struct bar;
1687 : struct foo { struct bar *x };
1688 : struct foo { struct bar { int a; } *x };
1689 : struct foo { struct bar { char b; } *x }; */
1690 :
1691 : bool
1692 14674831 : comptypes_equiv_p (tree type1, tree type2)
1693 : {
1694 14674831 : struct comptypes_data data = { };
1695 14674831 : data.equiv = true;
1696 14674831 : bool ret = comptypes_internal (type1, type2, &data);
1697 :
1698 : /* check that different equivance classes are assigned only
1699 : to types that are not compatible. */
1700 14674831 : gcc_checking_assert (ret || !comptypes (type1, type2));
1701 :
1702 14674831 : return ret;
1703 : }
1704 :
1705 :
1706 : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1707 : or various other operations. If they are compatible but a warning may
1708 : be needed if you use them together, 'warning_needed' in DATA is set.
1709 : If one type is an enum and the other a compatible integer type, then
1710 : this sets 'enum_and_int_p' in DATA to true (it is never set to
1711 : false). If the types are compatible but different enough not to be
1712 : permitted in C11 typedef redeclarations, then this sets
1713 : 'different_types_p' in DATA to true; it is never set to
1714 : false, but may or may not be set if the types are incompatible.
1715 : If two functions types are not compatible only because one is
1716 : an old-style definition that does not have self-promoting arguments,
1717 : then this can be ignored by setting 'ignore_promoting_args_p'.
1718 : For 'equiv' we can compute equivalency classes (see above). */
1719 :
1720 : static bool
1721 134428798 : comptypes_internal (const_tree type1, const_tree type2,
1722 : struct comptypes_data *data)
1723 : {
1724 134713184 : const_tree t1 = type1;
1725 134713184 : const_tree t2 = type2;
1726 :
1727 : /* Suppress errors caused by previously reported errors. */
1728 :
1729 134713184 : if (t1 == t2 || !t1 || !t2
1730 45428224 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1731 : return true;
1732 :
1733 : /* Qualifiers must match. C99 6.7.3p9 */
1734 :
1735 45428221 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1736 : return false;
1737 :
1738 : /* Enumerated types are compatible with integer types, but this is
1739 : not transitive: two enumerated types in the same translation unit
1740 : are compatible with each other only if they are the same type. */
1741 :
1742 45388001 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1743 1002 : && COMPLETE_TYPE_P (t1)
1744 45388959 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1745 : {
1746 842 : t1 = ENUM_UNDERLYING_TYPE (t1);
1747 842 : if (TREE_CODE (t2) != VOID_TYPE)
1748 : {
1749 838 : data->enum_and_int_p = true;
1750 838 : data->different_types_p = true;
1751 : }
1752 : }
1753 45387159 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1754 4726 : && COMPLETE_TYPE_P (t2)
1755 45391879 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1756 : {
1757 4604 : t2 = ENUM_UNDERLYING_TYPE (t2);
1758 4604 : if (TREE_CODE (t1) != VOID_TYPE)
1759 : {
1760 3705 : data->enum_and_int_p = true;
1761 3705 : data->different_types_p = true;
1762 : }
1763 : }
1764 :
1765 45388001 : if (t1 == t2)
1766 : return true;
1767 :
1768 : /* Different classes of types can't be compatible. */
1769 :
1770 45386625 : if (TREE_CODE (t1) != TREE_CODE (t2))
1771 : return false;
1772 :
1773 : /* Allow for two different type nodes which have essentially the same
1774 : definition. Note that we already checked for equality of the type
1775 : qualifiers (just above). */
1776 :
1777 37388451 : if (TREE_CODE (t1) != ARRAY_TYPE
1778 37388451 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1779 : return true;
1780 :
1781 37050265 : int attrval;
1782 :
1783 : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1784 37050265 : if (!(attrval = comp_type_attributes (t1, t2)))
1785 : return false;
1786 :
1787 37049888 : if (2 == attrval)
1788 336 : data->warning_needed = true;
1789 :
1790 37049888 : switch (TREE_CODE (t1))
1791 : {
1792 3364331 : case INTEGER_TYPE:
1793 3364331 : case FIXED_POINT_TYPE:
1794 3364331 : case REAL_TYPE:
1795 3364331 : case BITINT_TYPE:
1796 : /* With these nodes, we can't determine type equivalence by
1797 : looking at what is stored in the nodes themselves, because
1798 : two nodes might have different TYPE_MAIN_VARIANTs but still
1799 : represent the same type. For example, wchar_t and int could
1800 : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1801 : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1802 : and are distinct types. On the other hand, int and the
1803 : following typedef
1804 :
1805 : typedef int INT __attribute((may_alias));
1806 :
1807 : have identical properties, different TYPE_MAIN_VARIANTs, but
1808 : represent the same type. The canonical type system keeps
1809 : track of equivalence in this case, so we fall back on it. */
1810 3364331 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1811 :
1812 284386 : case POINTER_TYPE:
1813 : /* Do not remove mode information. */
1814 284386 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1815 : return false;
1816 284386 : data->pointedto = true;
1817 284386 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1818 :
1819 9170944 : case FUNCTION_TYPE:
1820 9170944 : return function_types_compatible_p (t1, t2, data);
1821 :
1822 154782 : case ARRAY_TYPE:
1823 154782 : {
1824 154782 : tree d1 = TYPE_DOMAIN (t1);
1825 154782 : tree d2 = TYPE_DOMAIN (t2);
1826 :
1827 : /* Target types must match incl. qualifiers. */
1828 154782 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1829 : return false;
1830 :
1831 119822 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1832 6349 : data->different_types_p = true;
1833 : /* Ignore size mismatches when forming equivalence classes. */
1834 119822 : if (data->equiv)
1835 : return true;
1836 : /* Sizes must match unless one is missing or variable. */
1837 50400 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1838 : return true;
1839 :
1840 34127 : bool d1_variable = top_array_vla_p (t1);
1841 34127 : bool d2_variable = top_array_vla_p (t2);
1842 :
1843 34127 : if (d1_variable != d2_variable)
1844 943 : data->different_types_p = true;
1845 34127 : if (d1_variable || d2_variable)
1846 : return true;
1847 :
1848 4027 : bool d1_zero = zero_length_array_type_p (t1)
1849 4027 : || flexible_array_member_type_p (t1);
1850 :
1851 4027 : bool d2_zero = zero_length_array_type_p (t2)
1852 4027 : || flexible_array_member_type_p (t2);
1853 :
1854 93 : if (d1_zero && d2_zero)
1855 : return true;
1856 4027 : if (d1_zero || d2_zero)
1857 : return false;
1858 :
1859 3931 : if (!tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1860 3931 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1861 3931 : return false;
1862 :
1863 : return true;
1864 : }
1865 :
1866 23537208 : case ENUMERAL_TYPE:
1867 23537208 : case RECORD_TYPE:
1868 23537208 : case UNION_TYPE:
1869 :
1870 23537208 : if (!flag_isoc23)
1871 : return false;
1872 :
1873 23537161 : return tagged_types_tu_compatible_p (t1, t2, data);
1874 :
1875 537432 : case VECTOR_TYPE:
1876 1074864 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1877 537432 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1878 :
1879 : default:
1880 : return false;
1881 : }
1882 : gcc_unreachable ();
1883 : }
1884 :
1885 : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1886 : their qualifiers, except for named address spaces. If the pointers point to
1887 : different named addresses, then we must determine if one address space is a
1888 : subset of the other. */
1889 :
1890 : static bool
1891 1770984 : comp_target_types (location_t location, tree ttl, tree ttr)
1892 : {
1893 1770984 : tree mvl = TREE_TYPE (ttl);
1894 1770984 : tree mvr = TREE_TYPE (ttr);
1895 1770984 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1896 1770984 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1897 1770984 : addr_space_t as_common;
1898 :
1899 : /* Fail if pointers point to incompatible address spaces. */
1900 1770984 : if (!addr_space_superset (asl, asr, &as_common))
1901 : return 0;
1902 :
1903 : /* Qualifiers on element types of array types that are
1904 : pointer targets are also removed. */
1905 1770984 : struct comptypes_data data = { };
1906 1770984 : if (!comptypes_internal (remove_qualifiers (mvl),
1907 1770984 : remove_qualifiers (mvr), &data))
1908 : return false;
1909 :
1910 : /* For pedantic use comptypes on arrays before removing
1911 : qualifiers on the element type. */
1912 1669551 : if (TREE_CODE (mvl) == ARRAY_TYPE
1913 1891 : && TREE_CODE (mvr) == ARRAY_TYPE
1914 1671442 : && !comptypes (mvl, mvr))
1915 190 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays "
1916 : "with different qualifiers in ISO C "
1917 : "before C23");
1918 :
1919 1669551 : if (data.warning_needed)
1920 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1921 :
1922 1669551 : if (data.enum_and_int_p && warn_cxx_compat)
1923 2 : warning_at (location, OPT_Wc___compat,
1924 : "pointer target types incompatible in C++");
1925 :
1926 : return true;
1927 : }
1928 :
1929 : /* Subroutines of `comptypes'. */
1930 :
1931 : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1932 : compatible. The two types are not the same (which has been
1933 : checked earlier in comptypes_internal). */
1934 :
1935 : static bool
1936 23537161 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1937 : struct comptypes_data *data)
1938 : {
1939 23537161 : tree s1, s2;
1940 :
1941 23537161 : if (c_type_tag (t1) != c_type_tag (t2))
1942 : return false;
1943 :
1944 : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1945 : structs with the same tag as equivalent, but only when they are targets
1946 : of pointers inside other structs. */
1947 19435619 : if (data->equiv && data->pointedto && NULL_TREE != c_type_tag (t1))
1948 : return true;
1949 :
1950 : /* Different types without tag are incompatible except as an anonymous
1951 : field or when forming equivalence classes for TYPE_CANONICAL. */
1952 19435557 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1953 : return false;
1954 :
1955 14279600 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1956 14278539 : data->different_types_p = true;
1957 :
1958 : /* Incomplete types are incompatible inside a TU. */
1959 14279600 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1960 : return false;
1961 :
1962 14279590 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1963 14279590 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1964 14279535 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1965 : return false;
1966 :
1967 14279580 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1968 144995 : data->different_types_p = true;
1969 :
1970 : /* For types already being looked at in some active
1971 : invocation of this function, assume compatibility.
1972 : The cache is built as a linked list on the stack
1973 : with the head of the list passed downwards. */
1974 14279580 : for (const struct tagged_tu_seen_cache *t = data->cache;
1975 14289273 : t != NULL; t = t->next)
1976 9988 : if (t->t1 == t1 && t->t2 == t2)
1977 : return true;
1978 :
1979 14279285 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1980 :
1981 14279285 : switch (TREE_CODE (t1))
1982 : {
1983 55 : case ENUMERAL_TYPE:
1984 55 : {
1985 55 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1986 : return false;
1987 :
1988 : /* Speed up the case where the type values are in the same order. */
1989 51 : tree tv1 = TYPE_VALUES (t1);
1990 51 : tree tv2 = TYPE_VALUES (t2);
1991 :
1992 51 : if (tv1 == tv2)
1993 : return true;
1994 :
1995 97 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1996 : {
1997 58 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1998 : break;
1999 :
2000 51 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
2001 51 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
2002 : break;
2003 : }
2004 :
2005 51 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
2006 : return true;
2007 :
2008 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
2009 : return false;
2010 :
2011 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
2012 : return false;
2013 :
2014 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
2015 : {
2016 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
2017 :
2018 16 : if (s2 == NULL
2019 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
2020 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
2021 8 : return false;
2022 : }
2023 :
2024 : return true;
2025 : }
2026 :
2027 14279230 : case UNION_TYPE:
2028 14279230 : case RECORD_TYPE:
2029 :
2030 14279230 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
2031 : return false;
2032 :
2033 12331613 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
2034 13854183 : s1 && s2;
2035 1522570 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
2036 : {
2037 13788694 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
2038 13788694 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
2039 :
2040 13788694 : tree ft1 = TREE_TYPE (s1);
2041 13788694 : tree ft2 = TREE_TYPE (s2);
2042 :
2043 13788694 : if (DECL_NAME (s1) != DECL_NAME (s2))
2044 : return false;
2045 :
2046 12136702 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
2047 : return false;
2048 :
2049 3233761 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
2050 : return false;
2051 :
2052 3233741 : if (DECL_C_BIT_FIELD (s1))
2053 : {
2054 496 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2055 : return false;
2056 :
2057 342 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2058 342 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2059 : }
2060 :
2061 3233587 : if (!ft1 || !ft2)
2062 : return false;
2063 :
2064 3233587 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2065 : return false;
2066 :
2067 3233586 : data->anon_field = !DECL_NAME (s1);
2068 3233586 : data->pointedto = false;
2069 :
2070 3233586 : const struct tagged_tu_seen_cache *cache = data->cache;
2071 3233586 : data->cache = &entry;
2072 3233586 : bool ret = comptypes_internal (ft1, ft2, data);
2073 3233586 : data->cache = cache;
2074 3233586 : if (!ret)
2075 : return false;
2076 :
2077 1591880 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2078 1591880 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2079 :
2080 1591880 : if (data->equiv
2081 997596 : && st1 && TREE_CODE (st1) == INTEGER_CST
2082 997114 : && st2 && TREE_CODE (st2) == INTEGER_CST
2083 2588988 : && !tree_int_cst_equal (st1, st2))
2084 : return false;
2085 :
2086 1522604 : tree counted_by1 = lookup_attribute ("counted_by",
2087 1522604 : DECL_ATTRIBUTES (s1));
2088 1522604 : tree counted_by2 = lookup_attribute ("counted_by",
2089 1522604 : DECL_ATTRIBUTES (s2));
2090 : /* If there is no counted_by attribute for both fields. */
2091 1522604 : if (!counted_by1 && !counted_by2)
2092 1522540 : continue;
2093 :
2094 : /* If only one field has counted_by attribute. */
2095 64 : if ((counted_by1 && !counted_by2)
2096 64 : || (!counted_by1 && counted_by2))
2097 : return false;
2098 :
2099 : /* Now both s1 and s2 have counted_by attributes, check
2100 : whether they are the same. */
2101 :
2102 52 : tree counted_by_field1
2103 52 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2104 52 : tree counted_by_field2
2105 52 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2106 :
2107 52 : gcc_assert (counted_by_field1 && counted_by_field2);
2108 :
2109 94 : while (TREE_CHAIN (counted_by_field1))
2110 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2111 86 : while (TREE_CHAIN (counted_by_field2))
2112 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2113 :
2114 52 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2115 52 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2116 : return false;
2117 : }
2118 : return true;
2119 :
2120 0 : default:
2121 0 : gcc_unreachable ();
2122 : }
2123 : }
2124 :
2125 : /* Return true if two function types F1 and F2 are compatible.
2126 : If either type specifies no argument types,
2127 : the other must specify a fixed number of self-promoting arg types.
2128 : Otherwise, if one type specifies only the number of arguments,
2129 : the other must specify that number of self-promoting arg types.
2130 : Otherwise, the argument types must match. */
2131 :
2132 : static bool
2133 9170944 : function_types_compatible_p (const_tree f1, const_tree f2,
2134 : struct comptypes_data *data)
2135 : {
2136 9170944 : tree ret1 = TREE_TYPE (f1);
2137 9170944 : tree ret2 = TREE_TYPE (f2);
2138 :
2139 : /* 'volatile' qualifiers on a function's return type used to mean
2140 : the function is noreturn. */
2141 9170944 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2142 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2143 9170944 : if (TYPE_VOLATILE (ret1))
2144 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2145 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2146 9170944 : if (TYPE_VOLATILE (ret2))
2147 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2148 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2149 :
2150 9170944 : bool ignore_pargs = data->ignore_promoting_args;
2151 9170944 : data->ignore_promoting_args = false;
2152 :
2153 9170944 : if (!comptypes_internal (ret1, ret2, data))
2154 : return false;
2155 :
2156 9168349 : data->ignore_promoting_args = ignore_pargs;
2157 :
2158 9168349 : tree args1 = TYPE_ARG_TYPES (f1);
2159 9168349 : tree args2 = TYPE_ARG_TYPES (f2);
2160 :
2161 9168349 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2162 45241 : data->different_types_p = true;
2163 :
2164 : /* An unspecified parmlist matches any specified parmlist
2165 : whose argument types don't need default promotions. */
2166 :
2167 9168349 : if (args1 == NULL_TREE)
2168 : {
2169 23761 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2170 : return false;
2171 23758 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2172 : return false;
2173 23286 : data->ignore_promoting_args = false;
2174 : /* If one of these types comes from a non-prototype fn definition,
2175 : compare that with the other type's arglist.
2176 : If they don't match, ask for a warning (but no error). */
2177 23286 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2178 23286 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2179 12 : data->warning_needed = true;
2180 23286 : return true;
2181 : }
2182 9144588 : if (args2 == NULL_TREE)
2183 : {
2184 21562 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2185 : return false;
2186 21560 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2187 : return false;
2188 21499 : data->ignore_promoting_args = false;
2189 21499 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2190 21499 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2191 1 : data->warning_needed = true;
2192 21499 : return true;
2193 : }
2194 :
2195 : /* Both types have argument lists: compare them and propagate results. */
2196 9123026 : return type_lists_compatible_p (args1, args2, data);
2197 : }
2198 :
2199 : /* Check two lists of types for compatibility, returning false for
2200 : incompatible, true for compatible. */
2201 :
2202 : static bool
2203 9123102 : type_lists_compatible_p (const_tree args1, const_tree args2,
2204 : struct comptypes_data *data)
2205 : {
2206 56689268 : while (1)
2207 : {
2208 32906185 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2209 : return true;
2210 : /* If one list is shorter than the other,
2211 : they fail to match. */
2212 23928629 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2213 : return false;
2214 23928622 : tree a1 = TREE_VALUE (args1);
2215 23928622 : tree a2 = TREE_VALUE (args2);
2216 23928622 : tree mv1 = remove_qualifiers (a1);
2217 23928622 : tree mv2 = remove_qualifiers (a2);
2218 :
2219 23928622 : gcc_assert (mv2);
2220 23928622 : gcc_assert (mv2);
2221 :
2222 : /* If one of the lists has an error marker, ignore this arg. */
2223 23928622 : if (TREE_CODE (a1) == ERROR_MARK
2224 23928618 : || TREE_CODE (a2) == ERROR_MARK)
2225 : ;
2226 23928602 : else if (!comptypes_internal (mv1, mv2, data))
2227 : {
2228 145770 : data->different_types_p = true;
2229 : /* Allow wait (union {union wait *u; int *i} *)
2230 : and wait (union wait *) to be compatible. */
2231 145770 : if (TREE_CODE (a1) == UNION_TYPE
2232 130 : && (TYPE_NAME (a1) == NULL_TREE
2233 98 : || TYPE_TRANSPARENT_AGGR (a1))
2234 130 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2235 145900 : && tree_int_cst_equal (TYPE_SIZE (a1),
2236 130 : TYPE_SIZE (a2)))
2237 : {
2238 130 : tree memb;
2239 130 : for (memb = TYPE_FIELDS (a1);
2240 166 : memb; memb = DECL_CHAIN (memb))
2241 : {
2242 164 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2243 164 : if (comptypes_internal (mv3, mv2, data))
2244 : break;
2245 : }
2246 : if (memb == NULL_TREE)
2247 : return false;
2248 : }
2249 145640 : else if (TREE_CODE (a2) == UNION_TYPE
2250 105 : && (TYPE_NAME (a2) == NULL_TREE
2251 74 : || TYPE_TRANSPARENT_AGGR (a2))
2252 105 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2253 145745 : && tree_int_cst_equal (TYPE_SIZE (a2),
2254 105 : TYPE_SIZE (a1)))
2255 : {
2256 105 : tree memb;
2257 105 : for (memb = TYPE_FIELDS (a2);
2258 133 : memb; memb = DECL_CHAIN (memb))
2259 : {
2260 131 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2261 131 : if (comptypes_internal (mv3, mv1, data))
2262 : break;
2263 : }
2264 : if (memb == NULL_TREE)
2265 : return false;
2266 : }
2267 : else
2268 145535 : return false;
2269 : }
2270 :
2271 23783083 : args1 = TREE_CHAIN (args1);
2272 23783083 : args2 = TREE_CHAIN (args2);
2273 23783083 : }
2274 : }
2275 :
2276 : /* Compute the size to increment a pointer by. When a function type or void
2277 : type or incomplete type is passed, size_one_node is returned.
2278 : This function does not emit any diagnostics; the caller is responsible
2279 : for that. */
2280 :
2281 : static tree
2282 236734 : c_size_in_bytes (const_tree type)
2283 : {
2284 236734 : enum tree_code code = TREE_CODE (type);
2285 :
2286 236734 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2287 236734 : || !COMPLETE_TYPE_P (type))
2288 191 : return size_one_node;
2289 :
2290 : /* Convert in case a char is more than one unit. */
2291 236543 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2292 236543 : size_int (TYPE_PRECISION (char_type_node)
2293 : / BITS_PER_UNIT));
2294 : }
2295 :
2296 : /* Return either DECL or its known constant value (if it has one). */
2297 :
2298 : tree
2299 14374538 : decl_constant_value_1 (tree decl, bool in_init)
2300 : {
2301 14374538 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2302 14374538 : TREE_CODE (decl) != PARM_DECL
2303 14374538 : && !TREE_THIS_VOLATILE (decl)
2304 14030121 : && TREE_READONLY (decl)
2305 36826 : && DECL_INITIAL (decl) != NULL_TREE
2306 35816 : && !error_operand_p (DECL_INITIAL (decl))
2307 : /* This is invalid if initial value is not constant.
2308 : If it has either a function call, a memory reference,
2309 : or a variable, then re-evaluating it could give different results. */
2310 35807 : && TREE_CONSTANT (DECL_INITIAL (decl))
2311 : /* Check for cases where this is sub-optimal, even though valid. */
2312 14397237 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2313 17563 : return DECL_INITIAL (decl);
2314 : return decl;
2315 : }
2316 :
2317 : /* Return either DECL or its known constant value (if it has one).
2318 : Like the above, but always return decl outside of functions. */
2319 :
2320 : tree
2321 14374279 : decl_constant_value (tree decl)
2322 : {
2323 : /* Don't change a variable array bound or initial value to a constant
2324 : in a place where a variable is invalid. */
2325 14374279 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2326 : }
2327 :
2328 : /* Convert the array expression EXP to a pointer. */
2329 : static tree
2330 830278 : array_to_pointer_conversion (location_t loc, tree exp)
2331 : {
2332 830278 : tree orig_exp = exp;
2333 830278 : tree type = TREE_TYPE (exp);
2334 830278 : tree adr;
2335 830278 : tree restype = TREE_TYPE (type);
2336 830278 : tree ptrtype;
2337 :
2338 830278 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2339 :
2340 830278 : STRIP_TYPE_NOPS (exp);
2341 :
2342 830278 : copy_warning (exp, orig_exp);
2343 :
2344 830278 : ptrtype = c_build_pointer_type (restype);
2345 :
2346 830278 : if (INDIRECT_REF_P (exp))
2347 2906 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2348 :
2349 : /* In C++ array compound literals are temporary objects unless they are
2350 : const or appear in namespace scope, so they are destroyed too soon
2351 : to use them for much of anything (c++/53220). */
2352 827372 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2353 : {
2354 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2355 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2356 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2357 : "converting an array compound literal to a pointer "
2358 : "leads to a dangling pointer in C++");
2359 : }
2360 :
2361 827372 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2362 827372 : return convert (ptrtype, adr);
2363 : }
2364 :
2365 : /* Convert the function expression EXP to a pointer. */
2366 : static tree
2367 50647467 : function_to_pointer_conversion (location_t loc, tree exp)
2368 : {
2369 50647467 : tree orig_exp = exp;
2370 :
2371 50647467 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2372 :
2373 50647467 : STRIP_TYPE_NOPS (exp);
2374 :
2375 50647467 : copy_warning (exp, orig_exp);
2376 :
2377 50647467 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2378 :
2379 : /* If the function is defined and known to not require a non-local
2380 : context, make sure no trampoline is generated. */
2381 50647467 : if (TREE_CODE (exp) == FUNCTION_DECL
2382 50647467 : && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))
2383 16079566 : TREE_NO_TRAMPOLINE (exp2) = 1;
2384 :
2385 50647467 : return exp2;
2386 : }
2387 :
2388 : /* Mark EXP as read, not just set, for set but not used -Wunused
2389 : warning purposes. */
2390 :
2391 : void
2392 490578780 : mark_exp_read (tree exp)
2393 : {
2394 617065484 : switch (TREE_CODE (exp))
2395 : {
2396 203597624 : case VAR_DECL:
2397 203597624 : case PARM_DECL:
2398 203597624 : DECL_READ_P (exp) = 1;
2399 203597624 : break;
2400 19738246 : CASE_CONVERT:
2401 19738246 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2402 3805 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2403 : {
2404 : case PREINCREMENT_EXPR:
2405 : case PREDECREMENT_EXPR:
2406 : case POSTINCREMENT_EXPR:
2407 : case POSTDECREMENT_EXPR:
2408 : return;
2409 : default:
2410 : break;
2411 : }
2412 : /* FALLTHRU */
2413 123740171 : case ARRAY_REF:
2414 123740171 : case COMPONENT_REF:
2415 123740171 : case MODIFY_EXPR:
2416 123740171 : case REALPART_EXPR:
2417 123740171 : case IMAGPART_EXPR:
2418 123740171 : case ADDR_EXPR:
2419 123740171 : case VIEW_CONVERT_EXPR:
2420 123740171 : case PREINCREMENT_EXPR:
2421 123740171 : case PREDECREMENT_EXPR:
2422 123740171 : case POSTINCREMENT_EXPR:
2423 123740171 : case POSTDECREMENT_EXPR:
2424 123740171 : mark_exp_read (TREE_OPERAND (exp, 0));
2425 123740171 : break;
2426 236052 : case COMPOUND_EXPR:
2427 : /* Pattern match what build_atomic_assign produces with modifycode
2428 : NOP_EXPR. */
2429 236052 : if (VAR_P (TREE_OPERAND (exp, 1))
2430 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2431 263195 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2432 : {
2433 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2434 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2435 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2436 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2437 4410 : && TREE_CODE (t2) == CALL_EXPR)
2438 : {
2439 2200 : tree fndecl = get_callee_fndecl (t2);
2440 2200 : tree arg = NULL_TREE;
2441 2200 : if (fndecl
2442 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2443 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2444 4400 : && call_expr_nargs (t2) >= 2)
2445 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2446 : {
2447 130 : case BUILT_IN_ATOMIC_STORE:
2448 130 : arg = CALL_EXPR_ARG (t2, 1);
2449 130 : break;
2450 2070 : case BUILT_IN_ATOMIC_STORE_1:
2451 2070 : case BUILT_IN_ATOMIC_STORE_2:
2452 2070 : case BUILT_IN_ATOMIC_STORE_4:
2453 2070 : case BUILT_IN_ATOMIC_STORE_8:
2454 2070 : case BUILT_IN_ATOMIC_STORE_16:
2455 2070 : arg = CALL_EXPR_ARG (t2, 0);
2456 2070 : break;
2457 : default:
2458 : break;
2459 : }
2460 2200 : if (arg)
2461 : {
2462 2200 : STRIP_NOPS (arg);
2463 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2464 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2465 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2466 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2467 : }
2468 : }
2469 : }
2470 : /* FALLTHRU */
2471 2745989 : case C_MAYBE_CONST_EXPR:
2472 2745989 : mark_exp_read (TREE_OPERAND (exp, 1));
2473 2745989 : break;
2474 579 : case OMP_ARRAY_SECTION:
2475 579 : mark_exp_read (TREE_OPERAND (exp, 0));
2476 579 : if (TREE_OPERAND (exp, 1))
2477 385 : mark_exp_read (TREE_OPERAND (exp, 1));
2478 579 : if (TREE_OPERAND (exp, 2))
2479 544 : mark_exp_read (TREE_OPERAND (exp, 2));
2480 : break;
2481 : default:
2482 : break;
2483 : }
2484 : }
2485 :
2486 : /* Perform the default conversion of arrays and functions to pointers.
2487 : Return the result of converting EXP. For any other expression, just
2488 : return EXP.
2489 :
2490 : LOC is the location of the expression. */
2491 :
2492 : struct c_expr
2493 362598306 : default_function_array_conversion (location_t loc, struct c_expr exp)
2494 : {
2495 362598306 : tree orig_exp = exp.value;
2496 362598306 : tree type = TREE_TYPE (exp.value);
2497 362598306 : enum tree_code code = TREE_CODE (type);
2498 :
2499 362598306 : switch (code)
2500 : {
2501 : case ARRAY_TYPE:
2502 : {
2503 : bool not_lvalue = false;
2504 : bool lvalue_array_p;
2505 :
2506 783351 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2507 783351 : || CONVERT_EXPR_P (exp.value))
2508 783351 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2509 : {
2510 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2511 0 : not_lvalue = true;
2512 0 : exp.value = TREE_OPERAND (exp.value, 0);
2513 : }
2514 :
2515 783351 : copy_warning (exp.value, orig_exp);
2516 :
2517 783351 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2518 783351 : if (!flag_isoc99 && !lvalue_array_p)
2519 : {
2520 : /* Before C99, non-lvalue arrays do not decay to pointers.
2521 : Normally, using such an array would be invalid; but it can
2522 : be used correctly inside sizeof or as a statement expression.
2523 : Thus, do not give an error here; an error will result later. */
2524 109 : return exp;
2525 : }
2526 :
2527 783242 : exp.value = array_to_pointer_conversion (loc, exp.value);
2528 : }
2529 783242 : break;
2530 762356 : case FUNCTION_TYPE:
2531 762356 : exp.value = function_to_pointer_conversion (loc, exp.value);
2532 762356 : break;
2533 : default:
2534 : break;
2535 : }
2536 :
2537 362598197 : return exp;
2538 : }
2539 :
2540 : struct c_expr
2541 12675 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2542 : {
2543 12675 : mark_exp_read (exp.value);
2544 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2545 12675 : if (TREE_CODE (exp.value) == COMPONENT_REF
2546 12675 : && handle_counted_by_p (exp.value))
2547 4 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2548 12675 : return default_function_array_conversion (loc, exp);
2549 : }
2550 :
2551 : /* Return whether EXPR should be treated as an atomic lvalue for the
2552 : purposes of load and store handling. */
2553 :
2554 : static bool
2555 363708055 : really_atomic_lvalue (tree expr)
2556 : {
2557 363708055 : if (error_operand_p (expr))
2558 : return false;
2559 363700458 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2560 : return false;
2561 57393 : if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
2562 : return false;
2563 57390 : if (!lvalue_p (expr))
2564 : return false;
2565 :
2566 : /* Ignore _Atomic on register variables, since their addresses can't
2567 : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2568 : sequences wouldn't work. Ignore _Atomic on structures containing
2569 : bit-fields, since accessing elements of atomic structures or
2570 : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2571 : it's undefined at translation time or execution time, and the
2572 : normal atomic sequences again wouldn't work. */
2573 57532 : while (handled_component_p (expr))
2574 : {
2575 144 : if (TREE_CODE (expr) == COMPONENT_REF
2576 144 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2577 : return false;
2578 144 : expr = TREE_OPERAND (expr, 0);
2579 : }
2580 57388 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2581 1 : expr = COMPOUND_LITERAL_EXPR_DECL (expr);
2582 112572 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2583 : return false;
2584 : return true;
2585 : }
2586 :
2587 : /* If EXPR is a named constant (C23) derived from a constexpr variable
2588 : - that is, a reference to such a variable, or a member extracted by
2589 : a sequence of structure and union (but not array) member accesses
2590 : (where union member accesses must access the same member as
2591 : initialized) - then return the corresponding initializer;
2592 : otherwise, return NULL_TREE. */
2593 :
2594 : static tree
2595 360494555 : maybe_get_constexpr_init (tree expr)
2596 : {
2597 360494555 : tree decl = NULL_TREE;
2598 360494555 : if (TREE_CODE (expr) == VAR_DECL)
2599 : decl = expr;
2600 348750675 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2601 822591 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2602 822591 : if (decl
2603 12566471 : && C_DECL_DECLARED_CONSTEXPR (decl)
2604 378 : && DECL_INITIAL (decl) != NULL_TREE
2605 822969 : && !error_operand_p (DECL_INITIAL (decl)))
2606 378 : return DECL_INITIAL (decl);
2607 360494177 : if (TREE_CODE (expr) != COMPONENT_REF)
2608 : return NULL_TREE;
2609 877586 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2610 877586 : if (inner == NULL_TREE)
2611 : return NULL_TREE;
2612 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2613 47 : && !error_operand_p (inner)
2614 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2615 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2616 47 : inner = TREE_OPERAND (inner, 0);
2617 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2618 : return NULL_TREE;
2619 126 : tree field = TREE_OPERAND (expr, 1);
2620 126 : unsigned HOST_WIDE_INT cidx;
2621 126 : tree cfield, cvalue;
2622 126 : bool have_other_init = false;
2623 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2624 : {
2625 250 : if (cfield == field)
2626 : return cvalue;
2627 140 : have_other_init = true;
2628 : }
2629 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2630 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2631 : return NULL_TREE;
2632 : /* Return a default initializer. */
2633 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2634 5 : return build_constructor (TREE_TYPE (expr), NULL);
2635 8 : return build_zero_cst (TREE_TYPE (expr));
2636 : }
2637 :
2638 : /* Helper function for convert_lvalue_to_rvalue called via
2639 : walk_tree_without_duplicates. Find DATA inside of the expression. */
2640 :
2641 : static tree
2642 230295 : c_find_var_r (tree *tp, int *walk_subtrees, void *data)
2643 : {
2644 230295 : if (TYPE_P (*tp))
2645 0 : *walk_subtrees = 0;
2646 230295 : else if (*tp == (tree) data)
2647 : return *tp;
2648 : return NULL_TREE;
2649 : }
2650 :
2651 : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2652 : including converting functions and arrays to pointers if CONVERT_P.
2653 : If READ_P, also mark the expression as having been read. If
2654 : FOR_INIT, constexpr expressions of structure and union type should
2655 : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2656 : constexpr scalars (including elements of structures and unions) are
2657 : replaced by their initializers. */
2658 :
2659 : struct c_expr
2660 359804736 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2661 : bool convert_p, bool read_p, bool for_init)
2662 : {
2663 359804736 : bool force_non_npc = false;
2664 359804736 : if (read_p)
2665 315680924 : mark_exp_read (exp.value);
2666 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2667 315680924 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2668 785140 : && handle_counted_by_p (exp.value))
2669 785111 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2670 :
2671 359804736 : if (convert_p)
2672 359791453 : exp = default_function_array_conversion (loc, exp);
2673 359804736 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2674 359804736 : || (flag_isoc2y
2675 1574 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2676 357313861 : exp.value = require_complete_type (loc, exp.value);
2677 359804736 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2678 : {
2679 359616969 : tree init = maybe_get_constexpr_init (exp.value);
2680 359616969 : if (init != NULL_TREE)
2681 : {
2682 : /* A named constant of pointer type or type nullptr_t is not
2683 : a null pointer constant even if the initializer is
2684 : one. */
2685 375 : if (TREE_CODE (init) == INTEGER_CST
2686 240 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2687 401 : && integer_zerop (init))
2688 : force_non_npc = true;
2689 : exp.value = init;
2690 : }
2691 : }
2692 359804736 : if (really_atomic_lvalue (exp.value))
2693 : {
2694 26938 : vec<tree, va_gc> *params;
2695 26938 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2696 26938 : tree expr_type = TREE_TYPE (exp.value);
2697 26938 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2698 26938 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2699 :
2700 26938 : gcc_assert (TYPE_ATOMIC (expr_type));
2701 :
2702 : /* Expansion of a generic atomic load may require an addition
2703 : element, so allocate enough to prevent a resize. */
2704 26938 : vec_alloc (params, 4);
2705 :
2706 : /* Remove the qualifiers for the rest of the expressions and
2707 : create the VAL temp variable to hold the RHS. */
2708 26938 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2709 26938 : tmp = create_tmp_var_raw (nonatomic_type);
2710 26938 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2711 26938 : TREE_ADDRESSABLE (tmp) = 1;
2712 : /* Do not disable warnings for TMP even though it's artificial.
2713 : -Winvalid-memory-model depends on it. */
2714 :
2715 : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2716 26938 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2717 26938 : params->quick_push (expr_addr);
2718 26938 : params->quick_push (tmp_addr);
2719 26938 : params->quick_push (seq_cst);
2720 26938 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2721 :
2722 : /* EXPR is always read. */
2723 26938 : mark_exp_read (exp.value);
2724 :
2725 : /* Optimize the common case where c_build_function_call_vec
2726 : immediately folds __atomic_load (&expr, &tmp, SEQ_CST); into
2727 : tmp = __atomic_load_<N> (&expr, SEQ_CST);
2728 : In that case tmp is not addressable and can be initialized
2729 : fully by the rhs of the MODIFY_EXPR. */
2730 26938 : tree tem = func_call;
2731 26938 : if (CONVERT_EXPR_P (tem) && VOID_TYPE_P (TREE_TYPE (tem)))
2732 : {
2733 25683 : tem = TREE_OPERAND (tem, 0);
2734 25683 : if (TREE_CODE (tem) == MODIFY_EXPR
2735 25683 : && TREE_OPERAND (tem, 0) == tmp
2736 51366 : && !walk_tree_without_duplicates (&TREE_OPERAND (tem, 1),
2737 : c_find_var_r, tmp))
2738 : {
2739 25683 : TREE_ADDRESSABLE (tmp) = 0;
2740 25683 : func_call = TREE_OPERAND (tem, 1);
2741 : }
2742 : }
2743 :
2744 : /* Return tmp which contains the value loaded. */
2745 26938 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2746 : NULL_TREE, NULL_TREE);
2747 : }
2748 359791453 : if (convert_p && !error_operand_p (exp.value)
2749 719588636 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2750 359783791 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2751 359804736 : if (force_non_npc)
2752 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2753 :
2754 359804736 : {
2755 359804736 : tree false_value, true_value;
2756 359791453 : if (convert_p && !error_operand_p (exp.value)
2757 719588636 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2758 : &false_value, &true_value))
2759 : {
2760 9721 : tree t = save_expr (exp.value);
2761 :
2762 9721 : mark_exp_read (exp.value);
2763 :
2764 9721 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2765 9721 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2766 9721 : expr = build_compound_expr (loc, expr, boolean_true_node);
2767 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2768 : fold_build2_loc (loc, NE_EXPR,
2769 : boolean_type_node,
2770 : t, true_value),
2771 : expr, boolean_true_node);
2772 9721 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2773 : fold_build2_loc (loc, NE_EXPR,
2774 : boolean_type_node,
2775 : t, false_value),
2776 : expr, boolean_false_node);
2777 :
2778 9721 : exp.value = expr;
2779 : }
2780 : }
2781 :
2782 359804736 : return exp;
2783 : }
2784 :
2785 : /* Wrapper for the overload above, same arguments but for tree rather than
2786 : c_expr. This is important for hardbools to decay to bools. */
2787 :
2788 : static inline tree
2789 576439 : convert_lvalue_to_rvalue (location_t loc, tree val,
2790 : bool convert_p, bool read_p, bool for_init = false)
2791 : {
2792 576439 : struct c_expr expr;
2793 576439 : memset (&expr, 0, sizeof (expr));
2794 576439 : expr.value = val;
2795 576439 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2796 576439 : return expr.value;
2797 : }
2798 :
2799 : /* EXP is an expression of integer type. Apply the integer promotions
2800 : to it and return the promoted value. */
2801 :
2802 : tree
2803 77195584 : perform_integral_promotions (tree exp)
2804 : {
2805 77195584 : tree type = TREE_TYPE (exp);
2806 77195584 : enum tree_code code = TREE_CODE (type);
2807 :
2808 77195584 : gcc_assert (INTEGRAL_TYPE_P (type));
2809 :
2810 : /* Convert enums to the result of applying the integer promotions to
2811 : their underlying type. */
2812 77195584 : if (code == ENUMERAL_TYPE)
2813 : {
2814 651029 : type = ENUM_UNDERLYING_TYPE (type);
2815 651029 : if (c_promoting_integer_type_p (type))
2816 : {
2817 94 : if (TYPE_UNSIGNED (type)
2818 94 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2819 0 : type = unsigned_type_node;
2820 : else
2821 94 : type = integer_type_node;
2822 : }
2823 :
2824 651029 : return convert (type, exp);
2825 : }
2826 :
2827 : /* ??? This should no longer be needed now bit-fields have their
2828 : proper types. */
2829 76544555 : if (TREE_CODE (exp) == COMPONENT_REF
2830 76544555 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2831 : {
2832 62221 : if (BITINT_TYPE_P (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))))
2833 : {
2834 484 : tree btype = DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1));
2835 484 : if (TREE_CODE (btype) == BITINT_TYPE)
2836 454 : return convert (btype, exp);
2837 : else
2838 30 : return convert (ENUM_UNDERLYING_TYPE (btype), exp);
2839 : }
2840 : /* If it's thinner than an int, promote it like a
2841 : c_promoting_integer_type_p, otherwise leave it alone. */
2842 61737 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2843 61737 : TYPE_PRECISION (integer_type_node)) < 0)
2844 53475 : return convert (integer_type_node, exp);
2845 : }
2846 :
2847 76490596 : if (c_promoting_integer_type_p (type))
2848 : {
2849 : /* Preserve unsignedness if not really getting any wider. */
2850 650414 : if (TYPE_UNSIGNED (type)
2851 650414 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2852 0 : return convert (unsigned_type_node, exp);
2853 :
2854 650414 : return convert (integer_type_node, exp);
2855 : }
2856 :
2857 : return exp;
2858 : }
2859 :
2860 :
2861 : /* Perform default promotions for C data used in expressions.
2862 : Enumeral types or short or char are converted to int.
2863 : In addition, manifest constants symbols are replaced by their values. */
2864 :
2865 : tree
2866 86752296 : default_conversion (tree exp)
2867 : {
2868 86752296 : tree orig_exp;
2869 86752296 : tree type = TREE_TYPE (exp);
2870 86752296 : enum tree_code code = TREE_CODE (type);
2871 86752296 : tree promoted_type;
2872 :
2873 86752296 : mark_exp_read (exp);
2874 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2875 86752296 : if (TREE_CODE (exp) == COMPONENT_REF
2876 86752296 : && handle_counted_by_p (exp))
2877 602149 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2878 :
2879 : /* Functions and arrays have been converted during parsing. */
2880 86752296 : gcc_assert (code != FUNCTION_TYPE);
2881 86752296 : if (code == ARRAY_TYPE)
2882 : return exp;
2883 :
2884 : /* Constants can be used directly unless they're not loadable. */
2885 86752137 : if (TREE_CODE (exp) == CONST_DECL)
2886 0 : exp = DECL_INITIAL (exp);
2887 :
2888 : /* Strip no-op conversions. */
2889 86752137 : orig_exp = exp;
2890 87124049 : STRIP_TYPE_NOPS (exp);
2891 :
2892 86752137 : copy_warning (exp, orig_exp);
2893 :
2894 86752137 : if (code == VOID_TYPE)
2895 : {
2896 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2897 : "void value not ignored as it ought to be");
2898 2 : return error_mark_node;
2899 : }
2900 :
2901 86752135 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2902 86752135 : if (exp == error_mark_node)
2903 : return error_mark_node;
2904 :
2905 86751231 : promoted_type = targetm.promoted_type (type);
2906 86751231 : if (promoted_type)
2907 0 : return convert (promoted_type, exp);
2908 :
2909 86751231 : if (INTEGRAL_TYPE_P (type))
2910 76197836 : return perform_integral_promotions (exp);
2911 :
2912 : return exp;
2913 : }
2914 :
2915 : /* Look up COMPONENT in a structure or union TYPE.
2916 :
2917 : If the component name is not found, returns NULL_TREE. Otherwise,
2918 : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2919 : stepping down the chain to the component, which is in the last
2920 : TREE_VALUE of the list. Normally the list is of length one, but if
2921 : the component is embedded within (nested) anonymous structures or
2922 : unions, the list steps down the chain to the component. */
2923 :
2924 : tree
2925 2325579 : lookup_field (const_tree type, tree component)
2926 : {
2927 2325579 : tree field;
2928 :
2929 : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2930 : to the field elements. Use a binary search on this array to quickly
2931 : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2932 : will always be set for structures which have many elements.
2933 :
2934 : Duplicate field checking replaces duplicates with NULL_TREE so
2935 : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2936 : case just iterate using DECL_CHAIN. */
2937 :
2938 2627331 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2939 2627331 : && !seen_error ())
2940 : {
2941 301731 : int bot, top, half;
2942 301731 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2943 :
2944 301731 : field = TYPE_FIELDS (type);
2945 301731 : bot = 0;
2946 301731 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2947 1388458 : while (top - bot > 1)
2948 : {
2949 1359044 : half = (top - bot + 1) >> 1;
2950 1359044 : field = field_array[bot+half];
2951 :
2952 1359044 : if (DECL_NAME (field) == NULL_TREE)
2953 : {
2954 : /* Step through all anon unions in linear fashion. */
2955 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
2956 : {
2957 0 : field = field_array[bot++];
2958 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2959 : {
2960 0 : tree anon = lookup_field (TREE_TYPE (field), component);
2961 :
2962 0 : if (anon)
2963 0 : return tree_cons (NULL_TREE, field, anon);
2964 :
2965 : /* The Plan 9 compiler permits referring
2966 : directly to an anonymous struct/union field
2967 : using a typedef name. */
2968 0 : if (flag_plan9_extensions
2969 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2970 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2971 : == TYPE_DECL)
2972 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2973 : == component))
2974 : break;
2975 : }
2976 : }
2977 :
2978 : /* Entire record is only anon unions. */
2979 0 : if (bot > top)
2980 : return NULL_TREE;
2981 :
2982 : /* Restart the binary search, with new lower bound. */
2983 0 : continue;
2984 0 : }
2985 :
2986 1359044 : if (DECL_NAME (field) == component)
2987 : break;
2988 1086727 : if (DECL_NAME (field) < component)
2989 : bot += half;
2990 : else
2991 901005 : top = bot + half;
2992 : }
2993 :
2994 301731 : if (DECL_NAME (field_array[bot]) == component)
2995 : field = field_array[bot];
2996 272317 : else if (DECL_NAME (field) != component)
2997 : return NULL_TREE;
2998 : }
2999 : else
3000 : {
3001 5268921 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3002 : {
3003 5259476 : if (DECL_NAME (field) == NULL_TREE
3004 5259476 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3005 : {
3006 12357 : tree anon = lookup_field (TREE_TYPE (field), component);
3007 :
3008 12357 : if (anon)
3009 3007 : return tree_cons (NULL_TREE, field, anon);
3010 :
3011 : /* The Plan 9 compiler permits referring directly to an
3012 : anonymous struct/union field using a typedef
3013 : name. */
3014 9350 : if (flag_plan9_extensions
3015 174 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
3016 172 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
3017 9412 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
3018 : == component))
3019 : break;
3020 : }
3021 :
3022 5256465 : if (DECL_NAME (field) == component)
3023 : break;
3024 : }
3025 :
3026 2020841 : if (field == NULL_TREE)
3027 : return NULL_TREE;
3028 : }
3029 :
3030 2313127 : return tree_cons (NULL_TREE, field, NULL_TREE);
3031 : }
3032 :
3033 : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
3034 :
3035 : static void
3036 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
3037 : vec<tree> *candidates)
3038 : {
3039 101 : tree field;
3040 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3041 : {
3042 157 : if (DECL_NAME (field) == NULL_TREE
3043 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
3044 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
3045 : candidates);
3046 :
3047 157 : if (DECL_NAME (field))
3048 111 : candidates->safe_push (DECL_NAME (field));
3049 : }
3050 101 : }
3051 :
3052 : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
3053 : rather than returning a TREE_LIST for an exact match. */
3054 :
3055 : static tree
3056 71 : lookup_field_fuzzy (tree type, tree component)
3057 : {
3058 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
3059 :
3060 : /* First, gather a list of candidates. */
3061 71 : auto_vec <tree> candidates;
3062 :
3063 71 : lookup_field_fuzzy_find_candidates (type, component,
3064 : &candidates);
3065 :
3066 71 : return find_closest_identifier (component, &candidates);
3067 71 : }
3068 :
3069 : /* Support function for build_component_ref's error-handling.
3070 :
3071 : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
3072 : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
3073 :
3074 : static bool
3075 72 : should_suggest_deref_p (tree datum_type)
3076 : {
3077 : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
3078 : allows "." for ptrs; we could be handling a failed attempt
3079 : to access a property. */
3080 72 : if (c_dialect_objc ())
3081 : return false;
3082 :
3083 : /* Only suggest it for pointers... */
3084 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
3085 : return false;
3086 :
3087 : /* ...to structs/unions. */
3088 8 : tree underlying_type = TREE_TYPE (datum_type);
3089 8 : enum tree_code code = TREE_CODE (underlying_type);
3090 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
3091 : return true;
3092 : else
3093 1 : return false;
3094 : }
3095 :
3096 : /* Give a component ref REF, decide whether we should handle its counted_by
3097 : attribute based on its context:
3098 : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3099 :
3100 : static bool
3101 2212768 : handle_counted_by_p (tree ref)
3102 : {
3103 2212768 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3104 2212768 : tree datum = TREE_OPERAND (ref, 0);
3105 : /* If the component_ref is build for a offsetof, i.e., the datum
3106 : of the component_ref is a indirect_ref of null_pointer_node,
3107 : we should not generate call to .ACCESS_WITH_SIZE. */
3108 2212768 : if (TREE_CODE (datum) == INDIRECT_REF
3109 2212768 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3110 : return false;
3111 2212430 : if (in_typeof || in_alignof)
3112 8786 : return false;
3113 : return true;
3114 : }
3115 :
3116 : /* Given a component ref REF, if there is a counted_by attribute attached,
3117 : issue error when the element_type is a structure or union including a
3118 : flexible array member. */
3119 :
3120 : static void
3121 2279297 : check_counted_by_attribute (location_t loc, tree ref)
3122 : {
3123 2279297 : tree subdatum = TREE_OPERAND (ref, 1);
3124 2279297 : tree sub_type = TREE_TYPE (subdatum);
3125 :
3126 2279297 : if (!flexible_array_member_type_p (sub_type)
3127 2279297 : && TREE_CODE (sub_type) != POINTER_TYPE)
3128 : return;
3129 :
3130 389403 : tree element_type = TREE_TYPE (sub_type);
3131 :
3132 389403 : tree attr_counted_by = lookup_attribute ("counted_by",
3133 389403 : DECL_ATTRIBUTES (subdatum));
3134 389403 : if (attr_counted_by)
3135 : {
3136 : /* Issue error when the element_type is a structure or
3137 : union including a flexible array member. */
3138 881 : if (RECORD_OR_UNION_TYPE_P (element_type)
3139 881 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3140 : {
3141 2 : error_at (loc,
3142 : "%<counted_by%> attribute is not allowed for a pointer to"
3143 : " structure or union with flexible array member");
3144 2 : return;
3145 : }
3146 : }
3147 : }
3148 :
3149 : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3150 : to the object that represents its counted_by per the attribute
3151 : counted_by attached to this field if it's a flexible array member
3152 : or a pointer field, otherwise return NULL_TREE.
3153 : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3154 : For example, if:
3155 :
3156 : struct P {
3157 : int k;
3158 : int x[] __attribute__ ((counted_by (k)));
3159 : } *p;
3160 :
3161 : for:
3162 : p->x
3163 :
3164 : the ref to the object that represents its element count will be:
3165 :
3166 : &(p->k)
3167 : */
3168 :
3169 : static tree
3170 441604 : build_counted_by_ref (tree datum, tree subdatum,
3171 : tree *counted_by_type)
3172 : {
3173 441604 : tree sub_type = TREE_TYPE (subdatum);
3174 441604 : if (!flexible_array_member_type_p (sub_type)
3175 441604 : && TREE_CODE (sub_type) != POINTER_TYPE)
3176 : return NULL_TREE;
3177 :
3178 441604 : tree attr_counted_by = lookup_attribute ("counted_by",
3179 441604 : DECL_ATTRIBUTES (subdatum));
3180 441604 : if (!attr_counted_by)
3181 : return NULL_TREE;
3182 :
3183 660 : tree counted_by_ref = NULL_TREE;
3184 660 : *counted_by_type = NULL_TREE;
3185 :
3186 660 : tree type = TREE_TYPE (datum);
3187 :
3188 : /* If the type of the containing structure is an anonymous struct/union,
3189 : and this anonymous struct/union is not a root type, get the first
3190 : outer named structure/union type. */
3191 660 : while (TREE_CODE (datum) == COMPONENT_REF
3192 35 : && c_type_tag (type) == NULL_TREE
3193 730 : && DECL_NAME (TREE_OPERAND (datum, 1)) == NULL_TREE)
3194 : {
3195 35 : datum = TREE_OPERAND (datum, 0);
3196 35 : type = TREE_TYPE (datum);
3197 : }
3198 :
3199 660 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3200 660 : tree counted_by_field = lookup_field (type, field_id);
3201 660 : gcc_assert (counted_by_field);
3202 :
3203 1072 : tree counted_by_subdatum;
3204 1072 : do
3205 : {
3206 1072 : counted_by_subdatum = TREE_VALUE (counted_by_field);
3207 : /* Get the TYPE of the counted_by field. */
3208 1072 : *counted_by_type = TREE_TYPE (counted_by_subdatum);
3209 :
3210 1072 : counted_by_ref
3211 1072 : = build3 (COMPONENT_REF, TREE_TYPE (counted_by_subdatum),
3212 : datum, counted_by_subdatum, NULL_TREE);
3213 :
3214 1072 : datum = counted_by_ref;
3215 1072 : counted_by_field = TREE_CHAIN (counted_by_field);
3216 : }
3217 1072 : while (counted_by_field);
3218 :
3219 660 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3220 660 : return counted_by_ref;
3221 : }
3222 :
3223 : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3224 : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3225 : call to the internal function .ACCESS_WITH_SIZE.
3226 :
3227 : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3228 : the internal function .ACCESS_WITH_SIZE.
3229 :
3230 : REF
3231 :
3232 : to:
3233 :
3234 : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3235 : TYPE_SIZE_UNIT for element)
3236 :
3237 : NOTE: The return type of this function is the POINTER type pointing
3238 : to the original flexible array type. Then the type of the INDIRECT_REF
3239 : is the original flexible array type.
3240 : The type of the first argument of this function is a POINTER type
3241 : to the original flexible array type.
3242 :
3243 : B: For pointers with counted_by, generate a call to the internal function
3244 : .ACCESS_WITH_SIZE.
3245 :
3246 : REF
3247 :
3248 : to:
3249 :
3250 : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3251 : TYPE_SIZE_UNIT for element)
3252 :
3253 : NOTE: The return type of this function is the original pointer type.
3254 : The type of the first argument of this function is the original
3255 : pointer type.
3256 :
3257 : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3258 : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3259 :
3260 : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3261 : of the array.
3262 :
3263 : */
3264 : static tree
3265 660 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3266 : tree counted_by_ref,
3267 : tree counted_by_type)
3268 : {
3269 660 : gcc_assert (flexible_array_member_type_p (TREE_TYPE (ref))
3270 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3271 :
3272 660 : bool is_fam = flexible_array_member_type_p (TREE_TYPE (ref));
3273 :
3274 : /* The result type of the call is a pointer to the flexible array type;
3275 : or is the original pointer type to the pointer field with counted_by. */
3276 660 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3277 459 : : TREE_TYPE (ref);
3278 :
3279 660 : tree element_type = TREE_TYPE (TREE_TYPE (ref));
3280 660 : tree element_size = VOID_TYPE_P (element_type)
3281 660 : ? size_one_node
3282 660 : : TYPE_SIZE_UNIT (element_type);
3283 660 : if (element_size == NULL_TREE)
3284 : return ref;
3285 :
3286 646 : tree first_param = is_fam
3287 646 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3288 : false, NULL)
3289 445 : : c_fully_fold (ref, false, NULL);
3290 646 : tree second_param
3291 646 : = c_fully_fold (counted_by_ref, false, NULL);
3292 646 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3293 :
3294 646 : tree call
3295 646 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3296 : result_type, 4,
3297 : first_param,
3298 : second_param,
3299 : third_param,
3300 : element_size);
3301 :
3302 : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3303 646 : if (is_fam)
3304 201 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3305 646 : SET_EXPR_LOCATION (call, loc);
3306 646 : return call;
3307 : }
3308 :
3309 : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3310 : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3311 : function .ACCESS_WITH_SIZE.
3312 : Otherwise, return the ref itself. */
3313 :
3314 : tree
3315 2203724 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3316 : {
3317 2203724 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3318 2203724 : tree datum = TREE_OPERAND (ref, 0);
3319 2203724 : tree subdatum = TREE_OPERAND (ref, 1);
3320 2203724 : tree counted_by_type = NULL_TREE;
3321 :
3322 2203724 : if (!(flexible_array_member_type_p (TREE_TYPE (ref))
3323 2133253 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3324 : return ref;
3325 :
3326 441604 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3327 : &counted_by_type);
3328 441604 : if (counted_by_ref)
3329 660 : ref = build_access_with_size_for_counted_by (loc, ref,
3330 : counted_by_ref,
3331 : counted_by_type);
3332 : return ref;
3333 : }
3334 :
3335 : /* Make an expression to refer to the COMPONENT field of structure or
3336 : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3337 : location of the COMPONENT_REF. COMPONENT_LOC is the location
3338 : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3339 : it is from -> operator.
3340 : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3341 : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3342 :
3343 : tree
3344 2277179 : build_component_ref (location_t loc, tree datum, tree component,
3345 : location_t component_loc, location_t arrow_loc)
3346 : {
3347 2277179 : tree type = TREE_TYPE (datum);
3348 2277179 : enum tree_code code = TREE_CODE (type);
3349 2277179 : tree field = NULL;
3350 2277179 : tree ref;
3351 2277179 : bool datum_lvalue = lvalue_p (datum);
3352 :
3353 2277179 : if (!objc_is_public (datum, component))
3354 0 : return error_mark_node;
3355 :
3356 : /* Detect Objective-C property syntax object.property. */
3357 2277179 : if (c_dialect_objc ()
3358 2277179 : && (ref = objc_maybe_build_component_ref (datum, component)))
3359 : return ref;
3360 :
3361 : /* See if there is a field or component with name COMPONENT. */
3362 :
3363 2277179 : if (code == RECORD_TYPE || code == UNION_TYPE)
3364 : {
3365 2277107 : if (!COMPLETE_TYPE_P (type))
3366 : {
3367 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3368 19 : return error_mark_node;
3369 : }
3370 :
3371 2277088 : field = lookup_field (type, component);
3372 :
3373 2277088 : if (!field)
3374 : {
3375 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3376 63 : if (guessed_id)
3377 : {
3378 : /* Attempt to provide a fixit replacement hint, if
3379 : we have a valid range for the component. */
3380 0 : location_t reported_loc
3381 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3382 26 : gcc_rich_location rich_loc (reported_loc);
3383 26 : if (component_loc != UNKNOWN_LOCATION)
3384 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3385 26 : error_at (&rich_loc,
3386 : "%qT has no member named %qE; did you mean %qE?",
3387 : type, component, guessed_id);
3388 26 : }
3389 : else
3390 37 : error_at (loc, "%qT has no member named %qE", type, component);
3391 63 : return error_mark_node;
3392 : }
3393 :
3394 : /* Accessing elements of atomic structures or unions is undefined
3395 : behavior (C11 6.5.2.3#5). */
3396 2277025 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3397 : {
3398 18 : if (code == RECORD_TYPE)
3399 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3400 : "structure %qE", component, datum);
3401 : else
3402 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3403 : "union %qE", component, datum);
3404 : }
3405 :
3406 : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3407 : This might be better solved in future the way the C++ front
3408 : end does it - by giving the anonymous entities each a
3409 : separate name and type, and then have build_component_ref
3410 : recursively call itself. We can't do that here. */
3411 2279297 : do
3412 : {
3413 2279297 : tree subdatum = TREE_VALUE (field);
3414 2279297 : int quals;
3415 2279297 : tree subtype;
3416 2279297 : bool use_datum_quals;
3417 :
3418 2279297 : if (TREE_TYPE (subdatum) == error_mark_node)
3419 : return error_mark_node;
3420 :
3421 : /* If this is an rvalue, it does not have qualifiers in C
3422 : standard terms and we must avoid propagating such
3423 : qualifiers down to a non-lvalue array that is then
3424 : converted to a pointer. */
3425 4558594 : use_datum_quals = (datum_lvalue
3426 2279297 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3427 :
3428 2279297 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3429 2279297 : if (use_datum_quals)
3430 2279035 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3431 2279297 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3432 :
3433 2279297 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3434 : NULL_TREE);
3435 2279297 : SET_EXPR_LOCATION (ref, loc);
3436 :
3437 2279297 : check_counted_by_attribute (loc, ref);
3438 :
3439 2279297 : if (TREE_READONLY (subdatum)
3440 2279297 : || (use_datum_quals && TREE_READONLY (datum)))
3441 30607 : TREE_READONLY (ref) = 1;
3442 2279297 : if (TREE_THIS_VOLATILE (subdatum)
3443 2278303 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3444 2428 : TREE_THIS_VOLATILE (ref) = 1;
3445 :
3446 2279297 : if (TREE_UNAVAILABLE (subdatum))
3447 10 : error_unavailable_use (subdatum, NULL_TREE);
3448 2279287 : else if (TREE_DEPRECATED (subdatum))
3449 16 : warn_deprecated_use (subdatum, NULL_TREE);
3450 :
3451 2279297 : datum = ref;
3452 :
3453 2279297 : field = TREE_CHAIN (field);
3454 : }
3455 2279297 : while (field);
3456 :
3457 : return ref;
3458 : }
3459 72 : else if (should_suggest_deref_p (type))
3460 : {
3461 : /* Special-case the error message for "ptr.field" for the case
3462 : where the user has confused "." vs "->". */
3463 7 : rich_location richloc (line_table, loc);
3464 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3465 : {
3466 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3467 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3468 2 : error_at (&richloc,
3469 : "%qE is a pointer to pointer; did you mean to dereference "
3470 : "it before applying %<->%> to it?",
3471 2 : TREE_OPERAND (datum, 0));
3472 : }
3473 : else
3474 : {
3475 : /* "loc" should be the "." token. */
3476 5 : richloc.add_fixit_replace ("->");
3477 5 : error_at (&richloc,
3478 : "%qE is a pointer; did you mean to use %<->%>?",
3479 : datum);
3480 : }
3481 7 : return error_mark_node;
3482 7 : }
3483 65 : else if (code != ERROR_MARK)
3484 1 : error_at (loc,
3485 : "request for member %qE in something not a structure or union",
3486 : component);
3487 :
3488 65 : return error_mark_node;
3489 : }
3490 :
3491 : /* Given an expression PTR for a pointer, return an expression
3492 : for the value pointed to.
3493 : ERRORSTRING is the name of the operator to appear in error messages.
3494 :
3495 : LOC is the location to use for the generated tree. */
3496 :
3497 : tree
3498 2759549 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3499 : {
3500 2759549 : tree pointer = default_conversion (ptr);
3501 2759549 : tree type = TREE_TYPE (pointer);
3502 2759549 : tree ref;
3503 :
3504 2759549 : if (TREE_CODE (type) == POINTER_TYPE)
3505 : {
3506 2759195 : if (CONVERT_EXPR_P (pointer)
3507 1978162 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3508 : {
3509 : /* If a warning is issued, mark it to avoid duplicates from
3510 : the backend. This only needs to be done at
3511 : warn_strict_aliasing > 2. */
3512 781033 : if (warn_strict_aliasing > 2)
3513 335151 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3514 335151 : type, TREE_OPERAND (pointer, 0)))
3515 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3516 : }
3517 :
3518 2759195 : if (TREE_CODE (pointer) == ADDR_EXPR
3519 2759195 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3520 67439 : == TREE_TYPE (type)))
3521 : {
3522 67430 : ref = TREE_OPERAND (pointer, 0);
3523 67430 : protected_set_expr_location (ref, loc);
3524 67430 : return ref;
3525 : }
3526 : else
3527 : {
3528 2691765 : tree t = TREE_TYPE (type);
3529 :
3530 2691765 : ref = build1 (INDIRECT_REF, t, pointer);
3531 :
3532 2691765 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3533 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3534 :
3535 : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3536 : so that we get the proper error message if the result is used
3537 : to assign to. Also, &* is supposed to be a no-op.
3538 : And ANSI C seems to specify that the type of the result
3539 : should be the const type. */
3540 : /* A de-reference of a pointer to const is not a const. It is valid
3541 : to change it via some other pointer. */
3542 2691765 : TREE_READONLY (ref) = TYPE_READONLY (t);
3543 2691765 : TREE_SIDE_EFFECTS (ref)
3544 2691765 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3545 2691765 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3546 2691765 : protected_set_expr_location (ref, loc);
3547 2691765 : return ref;
3548 : }
3549 : }
3550 354 : else if (TREE_CODE (pointer) != ERROR_MARK)
3551 278 : invalid_indirection_error (loc, type, errstring);
3552 :
3553 354 : return error_mark_node;
3554 : }
3555 :
3556 : /* This handles expressions of the form "a[i]", which denotes
3557 : an array reference.
3558 :
3559 : This is logically equivalent in C to *(a+i), but we may do it differently.
3560 : If A is a variable or a member, we generate a primitive ARRAY_REF.
3561 : This avoids forcing the array out of registers, and can work on
3562 : arrays that are not lvalues (for example, members of structures returned
3563 : by functions).
3564 :
3565 : For vector types, allow vector[i] but not i[vector], and create
3566 : *(((type*)&vectortype) + i) for the expression.
3567 :
3568 : LOC is the location to use for the returned expression. */
3569 :
3570 : tree
3571 3481478 : build_array_ref (location_t loc, tree array, tree index)
3572 : {
3573 3481478 : tree ret;
3574 3481478 : bool swapped = false;
3575 3481478 : if (TREE_TYPE (array) == error_mark_node
3576 3481478 : || TREE_TYPE (index) == error_mark_node)
3577 : return error_mark_node;
3578 :
3579 3481370 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3580 1998934 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3581 : /* Allow vector[index] but not index[vector]. */
3582 4504732 : && !gnu_vector_type_p (TREE_TYPE (array)))
3583 : {
3584 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3585 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3586 : {
3587 2 : error_at (loc,
3588 : "subscripted value is neither array nor pointer nor vector");
3589 :
3590 2 : return error_mark_node;
3591 : }
3592 192 : std::swap (array, index);
3593 192 : swapped = true;
3594 : }
3595 :
3596 3481368 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3597 : {
3598 2 : error_at (loc, "array subscript is not an integer");
3599 2 : return error_mark_node;
3600 : }
3601 :
3602 3481366 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3603 : {
3604 10 : error_at (loc, "subscripted value is pointer to function");
3605 10 : return error_mark_node;
3606 : }
3607 :
3608 : /* ??? Existing practice has been to warn only when the char
3609 : index is syntactically the index, not for char[array]. */
3610 3481356 : if (!swapped)
3611 3481170 : warn_array_subscript_with_type_char (loc, index);
3612 :
3613 : /* Apply default promotions *after* noticing character types. */
3614 3481356 : index = default_conversion (index);
3615 3481356 : if (index == error_mark_node)
3616 : return error_mark_node;
3617 :
3618 3481355 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3619 : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3620 :
3621 3481355 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3622 3481355 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3623 :
3624 : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3625 3481355 : if (TREE_CODE (array) == COMPONENT_REF
3626 3481355 : && handle_counted_by_p (array))
3627 816380 : array = handle_counted_by_for_component_ref (loc, array);
3628 :
3629 3481355 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3630 : {
3631 2505611 : tree rval, type;
3632 :
3633 : /* An array that is indexed by a non-constant
3634 : cannot be stored in a register; we must be able to do
3635 : address arithmetic on its address.
3636 : Likewise an array of elements of variable size. */
3637 2505611 : if (TREE_CODE (index) != INTEGER_CST
3638 2505611 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3639 2025985 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3640 : {
3641 480366 : if (!c_mark_addressable (array, true, true))
3642 1 : return error_mark_node;
3643 : }
3644 : /* An array that is indexed by a constant value which is not within
3645 : the array bounds cannot be stored in a register either; because we
3646 : would get a crash in store_bit_field/extract_bit_field when trying
3647 : to access a non-existent part of the register. */
3648 2505610 : if (TREE_CODE (index) == INTEGER_CST
3649 2025985 : && TYPE_DOMAIN (TREE_TYPE (array))
3650 4528236 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3651 : {
3652 60655 : if (!c_mark_addressable (array, false, true))
3653 0 : return error_mark_node;
3654 : /* ISO C2Y disallows a negative integer constant expression index
3655 : when array subscripting has an operand of array type. */
3656 60655 : if (flag_isoc2y
3657 26 : && !TREE_OVERFLOW (index)
3658 60678 : && tree_int_cst_sgn (index) < 0)
3659 10 : error_at (loc, "array subscript is negative");
3660 : }
3661 :
3662 2505610 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3663 2482249 : && ! was_vector)
3664 : {
3665 1481722 : tree foo = array;
3666 2317174 : while (TREE_CODE (foo) == COMPONENT_REF)
3667 835452 : foo = TREE_OPERAND (foo, 0);
3668 982168 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3669 2463824 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3670 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3671 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3672 : "ISO C forbids subscripting %<register%> array "
3673 : "before C2Y");
3674 1481644 : else if (!lvalue_p (foo))
3675 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3676 : "ISO C90 forbids subscripting non-lvalue "
3677 : "array");
3678 : }
3679 :
3680 2505610 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3681 2505610 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3682 3 : index = fold_convert (sizetype, index);
3683 :
3684 2505610 : type = TREE_TYPE (TREE_TYPE (array));
3685 2505610 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3686 : /* Array ref is const/volatile if the array elements are
3687 : or if the array is. */
3688 7516830 : TREE_READONLY (rval)
3689 2505610 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3690 2505610 : | TREE_READONLY (array));
3691 7516830 : TREE_SIDE_EFFECTS (rval)
3692 2505610 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3693 2505610 : | TREE_SIDE_EFFECTS (array));
3694 5011220 : TREE_THIS_VOLATILE (rval)
3695 2505610 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3696 : /* This was added by rms on 16 Nov 91.
3697 : It fixes vol struct foo *a; a->elts[1]
3698 : in an inline function.
3699 : Hope it doesn't break something else. */
3700 2505610 : | TREE_THIS_VOLATILE (array));
3701 2505610 : ret = require_complete_type (loc, rval);
3702 2505610 : protected_set_expr_location (ret, loc);
3703 2505610 : if (non_lvalue)
3704 105200 : ret = non_lvalue_loc (loc, ret);
3705 2505610 : return ret;
3706 : }
3707 : else
3708 : {
3709 975744 : tree ar = default_conversion (array);
3710 :
3711 975744 : if (ar == error_mark_node)
3712 : return ar;
3713 :
3714 975744 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3715 975744 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3716 :
3717 975744 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3718 : index, false),
3719 : RO_ARRAY_INDEXING);
3720 975744 : if (non_lvalue)
3721 0 : ret = non_lvalue_loc (loc, ret);
3722 975744 : return ret;
3723 : }
3724 : }
3725 :
3726 : /* Build an OpenMP array section reference, creating an exact type for the
3727 : resulting expression based on the element type and bounds if possible. If
3728 : we have variable bounds, create an incomplete array type for the result
3729 : instead. */
3730 :
3731 : tree
3732 3953 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3733 : {
3734 3953 : tree type = TREE_TYPE (array);
3735 3953 : gcc_assert (type);
3736 :
3737 3953 : tree sectype, eltype = TREE_TYPE (type);
3738 :
3739 : /* It's not an array or pointer type. Just reuse the type of the original
3740 : expression as the type of the array section (an error will be raised
3741 : anyway, later). */
3742 3953 : if (eltype == NULL_TREE || error_operand_p (eltype))
3743 12 : sectype = TREE_TYPE (array);
3744 : else
3745 : {
3746 3941 : tree idxtype = NULL_TREE;
3747 :
3748 3941 : if (index != NULL_TREE
3749 3941 : && length != NULL_TREE
3750 3014 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3751 6950 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3752 : {
3753 3002 : tree low = fold_convert (sizetype, index);
3754 3002 : tree high = fold_convert (sizetype, length);
3755 3002 : high = size_binop (PLUS_EXPR, low, high);
3756 3002 : high = size_binop (MINUS_EXPR, high, size_one_node);
3757 3002 : idxtype = build_range_type (sizetype, low, high);
3758 : }
3759 135 : else if ((index == NULL_TREE || integer_zerop (index))
3760 829 : && length != NULL_TREE
3761 1616 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3762 660 : idxtype = build_index_type (length);
3763 :
3764 3941 : gcc_assert (!error_operand_p (idxtype));
3765 :
3766 3941 : sectype = c_build_array_type (eltype, idxtype);
3767 : }
3768 :
3769 3953 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3770 : }
3771 :
3772 :
3773 : /* Record that REF is used. This is relevant for undeclared static
3774 : function and declarations referenced from a non-local context.
3775 : ADDRESS indicates whether the address is taken. */
3776 :
3777 : void
3778 475726869 : mark_decl_used (tree ref, bool address)
3779 : {
3780 475726869 : if (!ref || !DECL_P (ref) || in_alignof)
3781 : return;
3782 :
3783 : /* Non-file-scope and non-local reference in nested function. */
3784 348673296 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3785 122310436 : && DECL_CONTEXT (current_function_decl)
3786 475692821 : && DECL_CONTEXT (ref) != current_function_decl;
3787 :
3788 : /* An undeclared static function. */
3789 475682723 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3790 102017468 : && DECL_INITIAL (ref) == NULL_TREE
3791 69794760 : && DECL_EXTERNAL (ref)
3792 545474787 : && !TREE_PUBLIC (ref);
3793 :
3794 475548629 : if (!static_p && !nonloc_p)
3795 : return;
3796 :
3797 : /* If we may be in an unevaluated context, delay the decision. */
3798 136733 : if (in_sizeof || in_typeof || in_countof || in_generic)
3799 273 : return record_maybe_used_decl (ref, address);
3800 :
3801 136460 : if (static_p)
3802 133838 : C_DECL_USED (ref) = 1;
3803 :
3804 136460 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3805 : || TREE_CODE (ref) == PARM_DECL))
3806 2059 : DECL_NONLOCAL (ref) = 1;
3807 :
3808 : /* Nothing to do anymore. */
3809 2622 : if (!nonloc_p || C_FUNC_NONLOCAL_CONTEXT (current_function_decl))
3810 135243 : return;
3811 :
3812 : /* Filter out the cases where referencing a non-local variable does not
3813 : require a non-local context passed via the static chain. */
3814 1217 : if (!c_type_variably_modified_p (TREE_TYPE (ref)))
3815 1145 : switch (TREE_CODE (ref))
3816 : {
3817 219 : case FUNCTION_DECL:
3818 : /* Use of another local function that requires no context is ok. */
3819 219 : if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref))
3820 : return;
3821 : break;
3822 440 : case VAR_DECL:
3823 : /* Static variables and constexpr are ok, but for the later only
3824 : when the address is not taken. */
3825 440 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3826 : return;
3827 : break;
3828 : case TYPE_DECL:
3829 : /* A typedef is ok when not for a variably-modified type. */
3830 : return;
3831 : case CONST_DECL:
3832 : /* An enumeration constant is ok. */
3833 : return;
3834 : case PARM_DECL:
3835 : break;
3836 : case LABEL_DECL:
3837 : break;
3838 0 : default:
3839 0 : gcc_unreachable ();
3840 : }
3841 :
3842 : /* Mark all parent functions up to the nesting level of the variable as
3843 : as needing the non-local context. */
3844 2027 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3845 : {
3846 2027 : if (cont == DECL_CONTEXT (ref))
3847 : break;
3848 :
3849 : /* There should not be any other type of context used for function
3850 : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3851 1019 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3852 :
3853 1019 : if (TREE_CODE (cont) == FUNCTION_DECL)
3854 1019 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3855 : }
3856 : }
3857 :
3858 :
3859 : /* Build an external reference to identifier ID. FUN indicates
3860 : whether this will be used for a function call. LOC is the source
3861 : location of the identifier. This sets *TYPE to the type of the
3862 : identifier, which is not the same as the type of the returned value
3863 : for CONST_DECLs defined as enum constants. If the type of the
3864 : identifier is not available, *TYPE is set to NULL. */
3865 : tree
3866 175140853 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3867 : {
3868 175140853 : tree ref;
3869 175140853 : tree decl = lookup_name (id);
3870 :
3871 : /* In Objective-C, an instance variable (ivar) may be preferred to
3872 : whatever lookup_name() found. */
3873 175140853 : decl = objc_lookup_ivar (decl, id);
3874 :
3875 175140853 : *type = NULL;
3876 175140853 : if (decl && decl != error_mark_node)
3877 : {
3878 175134660 : ref = decl;
3879 175134660 : *type = TREE_TYPE (ref);
3880 175134660 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3881 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3882 : decl);
3883 : }
3884 6193 : else if (fun)
3885 : /* Implicit function declaration. */
3886 4729 : ref = implicitly_declare (loc, id);
3887 1464 : else if (decl == error_mark_node)
3888 : /* Don't complain about something that's already been
3889 : complained about. */
3890 : return error_mark_node;
3891 : else
3892 : {
3893 1230 : undeclared_variable (loc, id);
3894 1230 : return error_mark_node;
3895 : }
3896 :
3897 : /* For an OpenMP map clause, we can get better diagnostics for decls with
3898 : unmappable types if we return the decl with an error_mark_node type,
3899 : rather than returning error_mark_node for the decl itself. */
3900 175139389 : if (TREE_TYPE (ref) == error_mark_node
3901 175139389 : && !c_omp_array_section_p)
3902 : return error_mark_node;
3903 :
3904 175139371 : if (TREE_UNAVAILABLE (ref))
3905 14 : error_unavailable_use (ref, NULL_TREE);
3906 175139357 : else if (TREE_DEPRECATED (ref))
3907 100 : warn_deprecated_use (ref, NULL_TREE);
3908 :
3909 : /* Recursive call does not count as usage. */
3910 175139371 : if (ref != current_function_decl)
3911 : {
3912 175136683 : TREE_USED (ref) = 1;
3913 : }
3914 :
3915 175139371 : mark_decl_used (ref, false);
3916 :
3917 175139371 : if (TREE_CODE (ref) == CONST_DECL)
3918 : {
3919 418633 : used_types_insert (TREE_TYPE (ref));
3920 :
3921 418633 : if (warn_cxx_compat
3922 429 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3923 419060 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3924 : {
3925 1 : warning_at (loc, OPT_Wc___compat,
3926 : "enum constant defined in struct or union "
3927 : "is not visible in C++");
3928 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3929 : }
3930 :
3931 418633 : ref = DECL_INITIAL (ref);
3932 418633 : TREE_CONSTANT (ref) = 1;
3933 : }
3934 : /* C99 6.7.4p3: An inline definition of a function with external
3935 : linkage ... shall not contain a reference to an identifier with
3936 : internal linkage. */
3937 174720738 : else if (current_function_decl != NULL_TREE
3938 173206453 : && DECL_DECLARED_INLINE_P (current_function_decl)
3939 159469087 : && DECL_EXTERNAL (current_function_decl)
3940 158691457 : && VAR_OR_FUNCTION_DECL_P (ref)
3941 56551887 : && (!VAR_P (ref) || TREE_STATIC (ref))
3942 47397436 : && ! TREE_PUBLIC (ref)
3943 174720772 : && DECL_CONTEXT (ref) != current_function_decl)
3944 25 : record_inline_static (loc, current_function_decl, ref,
3945 : csi_internal);
3946 :
3947 : return ref;
3948 : }
3949 :
3950 : static struct maybe_used_decl *maybe_used_decls;
3951 :
3952 : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
3953 : _Generic, might be used if the operand of sizeof is a VLA type or the
3954 : operand of typeof is a variably modified type or the operand of _Countof has
3955 : a variable number of elements or the operand of _Generic is the one selected
3956 : as the result. */
3957 :
3958 : static void
3959 273 : record_maybe_used_decl (tree decl, bool address)
3960 : {
3961 273 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3962 273 : t->decl = decl;
3963 273 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
3964 273 : t->address = address;
3965 273 : t->next = maybe_used_decls;
3966 273 : maybe_used_decls = t;
3967 273 : }
3968 :
3969 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3970 : _Generic. If USED is false, just discard them. If it is true, mark them
3971 : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
3972 : them to the next level up (if still inside sizeof or typeof or _Countof or
3973 : _Generic). */
3974 :
3975 : void
3976 1470986 : pop_maybe_used (bool used)
3977 : {
3978 1470986 : struct maybe_used_decl *p = maybe_used_decls;
3979 1470986 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3980 1471283 : while (p && p->level > cur_level)
3981 : {
3982 297 : if (used)
3983 : {
3984 77 : if (cur_level == 0)
3985 53 : mark_decl_used (p->decl, p->address);
3986 : else
3987 24 : p->level = cur_level;
3988 : }
3989 297 : p = p->next;
3990 : }
3991 1470986 : if (!used || cur_level == 0)
3992 1470905 : maybe_used_decls = p;
3993 1470986 : }
3994 :
3995 : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3996 : _Generic, without acting on them, and return the pointer to the previous top
3997 : of the stack. This for use at the end of a default generic association when
3998 : it is not yet known whether the expression is used. If it later turns out
3999 : the expression is used (or treated as used before C23), restore_maybe_used
4000 : should be called on the return value followed by pop_maybe_used (true);
4001 : otherwise, the return value can be discarded. */
4002 :
4003 : struct maybe_used_decl *
4004 1137 : save_maybe_used ()
4005 : {
4006 1137 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
4007 1137 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
4008 1169 : while (p && p->level > cur_level)
4009 32 : p = p->next;
4010 1137 : maybe_used_decls = p;
4011 1137 : return orig;
4012 : }
4013 :
4014 : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
4015 : or _Generic returned by save_maybe_used. It is required that the stack is
4016 : at exactly the point where it was left by save_maybe_used. */
4017 :
4018 : void
4019 279 : restore_maybe_used (struct maybe_used_decl *stack)
4020 : {
4021 279 : maybe_used_decls = stack;
4022 279 : }
4023 :
4024 : /* Return the result of sizeof applied to EXPR. */
4025 :
4026 : struct c_expr
4027 313396 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
4028 : {
4029 313396 : struct c_expr ret;
4030 313396 : if (expr.value == error_mark_node)
4031 : {
4032 111 : ret.value = error_mark_node;
4033 111 : ret.original_code = ERROR_MARK;
4034 111 : ret.original_type = NULL;
4035 111 : ret.m_decimal = 0;
4036 111 : pop_maybe_used (false);
4037 : }
4038 : else
4039 : {
4040 313285 : bool expr_const_operands = true;
4041 :
4042 313285 : if (TREE_CODE (expr.value) == PARM_DECL
4043 313285 : && C_ARRAY_PARAMETER (expr.value))
4044 : {
4045 55 : auto_diagnostic_group d;
4046 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
4047 : "%<sizeof%> on array function parameter %qE will "
4048 : "return size of %qT", expr.value,
4049 55 : TREE_TYPE (expr.value)))
4050 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
4051 55 : }
4052 313285 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4053 : &expr_const_operands);
4054 313285 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
4055 313285 : c_last_sizeof_arg = expr.value;
4056 313285 : c_last_sizeof_loc = loc;
4057 313285 : ret.original_code = SIZEOF_EXPR;
4058 313285 : ret.original_type = NULL;
4059 313285 : ret.m_decimal = 0;
4060 313285 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
4061 : {
4062 : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
4063 288 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4064 : folded_expr, ret.value);
4065 288 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4066 288 : SET_EXPR_LOCATION (ret.value, loc);
4067 : }
4068 313285 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
4069 : }
4070 313396 : return ret;
4071 : }
4072 :
4073 : /* Return the result of sizeof applied to T, a structure for the type
4074 : name passed to sizeof (rather than the type itself). LOC is the
4075 : location of the original expression. */
4076 :
4077 : struct c_expr
4078 332235 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
4079 : {
4080 332235 : tree type;
4081 332235 : struct c_expr ret;
4082 332235 : tree type_expr = NULL_TREE;
4083 332235 : bool type_expr_const = true;
4084 332235 : type = groktypename (t, &type_expr, &type_expr_const);
4085 332235 : ret.value = c_sizeof (loc, type);
4086 332235 : c_last_sizeof_arg = type;
4087 332235 : c_last_sizeof_loc = loc;
4088 332235 : ret.original_code = SIZEOF_EXPR;
4089 332235 : ret.original_type = NULL;
4090 332235 : ret.m_decimal = 0;
4091 332235 : if (type == error_mark_node)
4092 : {
4093 5 : ret.value = error_mark_node;
4094 5 : ret.original_code = ERROR_MARK;
4095 : }
4096 : else
4097 332188 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4098 664333 : && C_TYPE_VARIABLE_SIZE (type))
4099 : {
4100 : /* If the type is a [*] array, it is a VLA but is represented as
4101 : having a size of zero. In such a case we must ensure that
4102 : the result of sizeof does not get folded to a constant by
4103 : c_fully_fold, because if the size is evaluated the result is
4104 : not constant and so constraints on zero or negative size
4105 : arrays must not be applied when this sizeof call is inside
4106 : another array declarator. */
4107 41 : if (!type_expr)
4108 16 : type_expr = integer_zero_node;
4109 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4110 : type_expr, ret.value);
4111 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4112 : }
4113 332235 : pop_maybe_used (type != error_mark_node
4114 332235 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4115 332235 : return ret;
4116 : }
4117 :
4118 : static bool
4119 2153592 : top_array_vla_p (const_tree type)
4120 : {
4121 2153592 : if (TREE_CODE (type) != ARRAY_TYPE)
4122 : return false;
4123 2153574 : if (!COMPLETE_TYPE_P (type))
4124 : return false;
4125 :
4126 1968887 : tree d = TYPE_DOMAIN (type);
4127 :
4128 1968887 : if (!d)
4129 : return false;
4130 1968887 : if (!TYPE_MAX_VALUE (d))
4131 : return false;
4132 :
4133 1967820 : return TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST
4134 1967820 : || TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST;
4135 : }
4136 :
4137 : /* Return the result of countof applied to EXPR. */
4138 :
4139 : struct c_expr
4140 54 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4141 : {
4142 54 : struct c_expr ret;
4143 54 : if (expr.value == error_mark_node)
4144 : {
4145 1 : ret.value = error_mark_node;
4146 1 : ret.original_code = ERROR_MARK;
4147 1 : ret.original_type = NULL;
4148 1 : ret.m_decimal = 0;
4149 1 : pop_maybe_used (false);
4150 : }
4151 : else
4152 : {
4153 53 : bool expr_const_operands = true;
4154 :
4155 53 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4156 : &expr_const_operands);
4157 53 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4158 53 : c_last_sizeof_arg = expr.value;
4159 53 : c_last_sizeof_loc = loc;
4160 53 : ret.original_code = COUNTOF_EXPR;
4161 53 : ret.original_type = NULL;
4162 53 : ret.m_decimal = 0;
4163 53 : if (top_array_vla_p (TREE_TYPE (folded_expr)))
4164 : {
4165 : /* countof is evaluated when given a vla. */
4166 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4167 : folded_expr, ret.value);
4168 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4169 11 : SET_EXPR_LOCATION (ret.value, loc);
4170 : }
4171 53 : pop_maybe_used (top_array_vla_p (TREE_TYPE (folded_expr)));
4172 : }
4173 54 : return ret;
4174 : }
4175 :
4176 : /* Return the result of countof applied to T, a structure for the type
4177 : name passed to countof (rather than the type itself). LOC is the
4178 : location of the original expression. */
4179 :
4180 : struct c_expr
4181 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4182 : {
4183 18 : tree type;
4184 18 : struct c_expr ret;
4185 18 : tree type_expr = NULL_TREE;
4186 18 : bool type_expr_const = true;
4187 18 : type = groktypename (t, &type_expr, &type_expr_const);
4188 18 : ret.value = c_countof_type (loc, type);
4189 18 : c_last_sizeof_arg = type;
4190 18 : c_last_sizeof_loc = loc;
4191 18 : ret.original_code = COUNTOF_EXPR;
4192 18 : ret.original_type = NULL;
4193 18 : ret.m_decimal = 0;
4194 18 : if (type == error_mark_node)
4195 : {
4196 0 : ret.value = error_mark_node;
4197 0 : ret.original_code = ERROR_MARK;
4198 : }
4199 : else
4200 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4201 22 : && top_array_vla_p (type))
4202 : {
4203 : /* If the type is a [*] array, it is a VLA but is represented as
4204 : having a size of zero. In such a case we must ensure that
4205 : the result of countof does not get folded to a constant by
4206 : c_fully_fold, because if the number of elements is evaluated
4207 : the result is not constant and so
4208 : constraints on zero or negative size arrays must not be applied
4209 : when this countof call is inside another array declarator. */
4210 6 : if (!type_expr)
4211 0 : type_expr = integer_zero_node;
4212 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4213 : type_expr, ret.value);
4214 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4215 : }
4216 18 : pop_maybe_used (type != error_mark_node ? top_array_vla_p (type) : false);
4217 18 : return ret;
4218 : }
4219 :
4220 : /* Return the result of maxof applied to T, a structure for the type
4221 : name passed to maxof (rather than the type itself). LOC is the
4222 : location of the original expression. */
4223 :
4224 : struct c_expr
4225 46 : c_expr_maxof_type (location_t loc, struct c_type_name *t)
4226 : {
4227 46 : tree type;
4228 46 : struct c_expr ret;
4229 46 : tree type_expr = NULL_TREE;
4230 46 : bool type_expr_const = true;
4231 46 : type = groktypename (t, &type_expr, &type_expr_const);
4232 46 : ret.value = c_maxof_type (loc, type);
4233 46 : c_last_sizeof_arg = type;
4234 46 : c_last_sizeof_loc = loc;
4235 46 : ret.original_code = MAXOF_EXPR;
4236 46 : ret.original_type = NULL;
4237 46 : ret.m_decimal = 0;
4238 46 : if (type == error_mark_node)
4239 : {
4240 0 : ret.value = error_mark_node;
4241 0 : ret.original_code = ERROR_MARK;
4242 : }
4243 46 : pop_maybe_used (type != error_mark_node);
4244 46 : return ret;
4245 : }
4246 :
4247 : /* Return the result of minof applied to T, a structure for the type
4248 : name passed to minof (rather than the type itself). LOC is the
4249 : location of the original expression. */
4250 :
4251 : struct c_expr
4252 46 : c_expr_minof_type (location_t loc, struct c_type_name *t)
4253 : {
4254 46 : tree type;
4255 46 : struct c_expr ret;
4256 46 : tree type_expr = NULL_TREE;
4257 46 : bool type_expr_const = true;
4258 46 : type = groktypename (t, &type_expr, &type_expr_const);
4259 46 : ret.value = c_minof_type (loc, type);
4260 46 : c_last_sizeof_arg = type;
4261 46 : c_last_sizeof_loc = loc;
4262 46 : ret.original_code = MINOF_EXPR;
4263 46 : ret.original_type = NULL;
4264 46 : ret.m_decimal = 0;
4265 46 : if (type == error_mark_node)
4266 : {
4267 0 : ret.value = error_mark_node;
4268 0 : ret.original_code = ERROR_MARK;
4269 : }
4270 46 : pop_maybe_used (type != error_mark_node);
4271 46 : return ret;
4272 : }
4273 :
4274 : /* Build a function call to function FUNCTION with parameters PARAMS.
4275 : The function call is at LOC.
4276 : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4277 : TREE_VALUE of each node is a parameter-expression.
4278 : FUNCTION's data type may be a function type or a pointer-to-function. */
4279 :
4280 : tree
4281 0 : build_function_call (location_t loc, tree function, tree params)
4282 : {
4283 0 : vec<tree, va_gc> *v;
4284 0 : tree ret;
4285 :
4286 0 : vec_alloc (v, list_length (params));
4287 0 : for (; params; params = TREE_CHAIN (params))
4288 0 : v->quick_push (TREE_VALUE (params));
4289 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4290 0 : vec_free (v);
4291 0 : return ret;
4292 : }
4293 :
4294 : /* Give a note about the location of the declaration of DECL,
4295 : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4296 :
4297 : static void
4298 299 : inform_declaration (tree decl, tree function_expr)
4299 : {
4300 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4301 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4302 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4303 92 : else if (function_expr)
4304 92 : switch (TREE_CODE (function_expr))
4305 : {
4306 : default:
4307 : break;
4308 13 : case COMPONENT_REF:
4309 : /* Show the decl of the pertinent field (e.g. for callback
4310 : fields in a struct. */
4311 13 : {
4312 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4313 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4314 13 : inform (loc, "declared here");
4315 : }
4316 : break;
4317 : }
4318 299 : }
4319 :
4320 :
4321 : /* Build a function call to function FUNCTION with parameters PARAMS.
4322 : If FUNCTION is the result of resolving an overloaded target built-in,
4323 : ORIG_FUNDECL is the original function decl, otherwise it is null.
4324 : ORIGTYPES, if not NULL, is a vector of types; each element is
4325 : either NULL or the original type of the corresponding element in
4326 : PARAMS. The original type may differ from TREE_TYPE of the
4327 : parameter for enums. FUNCTION's data type may be a function type
4328 : or pointer-to-function. This function changes the elements of
4329 : PARAMS. */
4330 :
4331 : tree
4332 49948106 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4333 : tree function, vec<tree, va_gc> *params,
4334 : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4335 : {
4336 49948106 : tree fntype, fundecl = NULL_TREE;
4337 49948106 : tree name = NULL_TREE, result;
4338 49948106 : tree tem;
4339 49948106 : int nargs;
4340 49948106 : tree *argarray;
4341 :
4342 :
4343 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4344 49948106 : STRIP_TYPE_NOPS (function);
4345 :
4346 : /* Convert anything with function type to a pointer-to-function. */
4347 49948106 : if (TREE_CODE (function) == FUNCTION_DECL)
4348 : {
4349 49870572 : name = DECL_NAME (function);
4350 :
4351 49870572 : if (flag_tm)
4352 421 : tm_malloc_replacement (function);
4353 49870572 : fundecl = function;
4354 49870572 : if (!orig_fundecl)
4355 49870572 : orig_fundecl = fundecl;
4356 : /* Atomic functions have type checking/casting already done. They are
4357 : often rewritten and don't match the original parameter list. */
4358 99741144 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4359 : origtypes = NULL;
4360 : }
4361 49948106 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4362 49885111 : function = function_to_pointer_conversion (loc, function);
4363 :
4364 : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4365 : expressions, like those used for ObjC messenger dispatches. */
4366 49948106 : if (params && !params->is_empty ())
4367 38218539 : function = objc_rewrite_function_call (function, (*params)[0]);
4368 :
4369 49948106 : function = c_fully_fold (function, false, NULL);
4370 :
4371 49948106 : fntype = TREE_TYPE (function);
4372 :
4373 49948106 : if (TREE_CODE (fntype) == ERROR_MARK)
4374 5 : return error_mark_node;
4375 :
4376 49948101 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4377 49948067 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4378 : {
4379 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4380 33 : error_at (loc,
4381 : "called object %qE is not a function or function pointer",
4382 : function);
4383 4 : else if (DECL_P (function))
4384 : {
4385 1 : auto_diagnostic_group d;
4386 1 : error_at (loc,
4387 : "called object %qD is not a function or function pointer",
4388 : function);
4389 1 : inform_declaration (function, NULL_TREE);
4390 1 : }
4391 : else
4392 3 : error_at (loc,
4393 : "called object is not a function or function pointer");
4394 37 : return error_mark_node;
4395 : }
4396 :
4397 49948064 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4398 381886 : current_function_returns_abnormally = 1;
4399 :
4400 : /* fntype now gets the type of function pointed to. */
4401 49948064 : fntype = TREE_TYPE (fntype);
4402 49948064 : tree return_type = TREE_TYPE (fntype);
4403 :
4404 : /* Convert the parameters to the types declared in the
4405 : function prototype, or apply default promotions. */
4406 :
4407 49948064 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4408 : origtypes, function, fundecl);
4409 49948064 : if (nargs < 0)
4410 178 : return error_mark_node;
4411 :
4412 : /* Check that the function is called through a compatible prototype.
4413 : If it is not, warn. */
4414 49946979 : if (CONVERT_EXPR_P (function)
4415 923 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4416 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4417 49948071 : && !comptypes (fntype, TREE_TYPE (tem)))
4418 : {
4419 : /* This situation leads to run-time undefined behavior. We can't,
4420 : therefore, simply error unless we can prove that all possible
4421 : executions of the program must execute the code. */
4422 20 : warning_at (loc, 0, "function called through a non-compatible type");
4423 :
4424 20 : if (VOID_TYPE_P (return_type)
4425 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4426 1 : pedwarn (loc, 0,
4427 : "function with qualified void return type called");
4428 : }
4429 :
4430 49947886 : argarray = vec_safe_address (params);
4431 :
4432 : /* Check that arguments to builtin functions match the expectations. */
4433 49947886 : if (fundecl
4434 49870409 : && fndecl_built_in_p (fundecl)
4435 83227385 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4436 : orig_fundecl, nargs, argarray))
4437 338 : return error_mark_node;
4438 :
4439 : /* Check that the arguments to the function are valid. */
4440 49947548 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4441 : nargs, argarray, &arg_loc,
4442 : comptypes);
4443 :
4444 49947548 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4445 49947548 : && !VOID_TYPE_P (return_type))
4446 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4447 49947548 : if (name != NULL_TREE
4448 99817619 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4449 : {
4450 32548156 : if (require_constant_value)
4451 3464 : result
4452 3464 : = fold_build_call_array_initializer_loc (loc, return_type,
4453 : function, nargs, argarray);
4454 : else
4455 32544692 : result = fold_build_call_array_loc (loc, return_type,
4456 : function, nargs, argarray);
4457 32548156 : if (TREE_CODE (result) == NOP_EXPR
4458 32548156 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4459 26466 : STRIP_TYPE_NOPS (result);
4460 : }
4461 : else
4462 17399392 : result = build_call_array_loc (loc, return_type,
4463 : function, nargs, argarray);
4464 : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4465 : later. */
4466 49947548 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4467 215 : suppress_warning (result, OPT_Wnonnull);
4468 :
4469 : /* In this improbable scenario, a nested function returns a VM type.
4470 : Create a TARGET_EXPR so that the call always has a LHS, much as
4471 : what the C++ FE does for functions returning non-PODs. */
4472 49947548 : if (c_type_variably_modified_p (TREE_TYPE (fntype)))
4473 : {
4474 83 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4475 83 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4476 : NULL_TREE, NULL_TREE);
4477 : }
4478 :
4479 49947548 : if (VOID_TYPE_P (TREE_TYPE (result)))
4480 : {
4481 2400646 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4482 2 : pedwarn (loc, 0,
4483 : "function with qualified void return type called");
4484 2400646 : return result;
4485 : }
4486 47546902 : return require_complete_type (loc, result);
4487 : }
4488 :
4489 : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4490 :
4491 : tree
4492 49947663 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4493 : tree function, vec<tree, va_gc> *params,
4494 : vec<tree, va_gc> *origtypes)
4495 : {
4496 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4497 49947674 : STRIP_TYPE_NOPS (function);
4498 :
4499 : /* Convert anything with function type to a pointer-to-function. */
4500 49947663 : if (TREE_CODE (function) == FUNCTION_DECL)
4501 : {
4502 : /* Implement type-directed function overloading for builtins.
4503 : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4504 : handle all the type checking. The result is a complete expression
4505 : that implements this function call. */
4506 49870129 : tree tem = resolve_overloaded_builtin (loc, function, params);
4507 49870129 : if (tem)
4508 : return tem;
4509 : }
4510 49861563 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4511 : }
4512 :
4513 : /* Helper for convert_arguments called to convert the VALue of argument
4514 : number ARGNUM from ORIGTYPE to the corresponding parameter number
4515 : PARMNUM and TYPE.
4516 : PLOC is the location where the conversion is being performed.
4517 : FUNCTION and FUNDECL are the same as in convert_arguments.
4518 : VALTYPE is the original type of VAL before the conversion and,
4519 : for EXCESS_PRECISION_EXPR, the operand of the expression.
4520 : NPC is true if VAL represents the null pointer constant (VAL itself
4521 : will have been folded to an integer constant).
4522 : RNAME is the same as FUNCTION except in Objective C when it's
4523 : the function selector.
4524 : EXCESS_PRECISION is true when VAL was originally represented
4525 : as EXCESS_PRECISION_EXPR.
4526 : WARNOPT is the same as in convert_for_assignment. */
4527 :
4528 : static tree
4529 125706997 : convert_argument (location_t ploc, tree function, tree fundecl,
4530 : tree type, tree origtype, tree val, tree valtype,
4531 : bool npc, tree rname, int parmnum, int argnum,
4532 : bool excess_precision, int warnopt)
4533 : {
4534 : /* Formal parm type is specified by a function prototype. */
4535 :
4536 125706997 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4537 : {
4538 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4539 : parmnum + 1);
4540 3 : return error_mark_node;
4541 : }
4542 :
4543 : /* Optionally warn about conversions that differ from the default
4544 : conversions. */
4545 125706994 : if (warn_traditional_conversion || warn_traditional)
4546 : {
4547 193 : if (INTEGRAL_TYPE_P (type)
4548 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4549 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4550 : "passing argument %d of %qE as integer rather "
4551 : "than floating due to prototype",
4552 : argnum, rname);
4553 193 : if (INTEGRAL_TYPE_P (type)
4554 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4555 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4556 : "passing argument %d of %qE as integer rather "
4557 : "than complex due to prototype",
4558 : argnum, rname);
4559 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4560 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4561 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4562 : "passing argument %d of %qE as complex rather "
4563 : "than floating due to prototype",
4564 : argnum, rname);
4565 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4566 71 : && INTEGRAL_TYPE_P (valtype))
4567 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4568 : "passing argument %d of %qE as floating rather "
4569 : "than integer due to prototype",
4570 : argnum, rname);
4571 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4572 7 : && INTEGRAL_TYPE_P (valtype))
4573 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4574 : "passing argument %d of %qE as complex rather "
4575 : "than integer due to prototype",
4576 : argnum, rname);
4577 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4578 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4579 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4580 : "passing argument %d of %qE as floating rather "
4581 : "than complex due to prototype",
4582 : argnum, rname);
4583 : /* ??? At some point, messages should be written about
4584 : conversions between complex types, but that's too messy
4585 : to do now. */
4586 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4587 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4588 : {
4589 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4590 :
4591 : /* Warn if any argument is passed as `float',
4592 : since without a prototype it would be `double'. */
4593 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4594 45 : && type != dfloat32_type_node)
4595 7 : warning_at (ploc, 0,
4596 : "passing argument %d of %qE as %<float%> "
4597 : "rather than %<double%> due to prototype",
4598 : argnum, rname);
4599 :
4600 : /* Warn if mismatch between argument and prototype
4601 : for decimal float types. Warn of conversions with
4602 : binary float types and of precision narrowing due to
4603 : prototype. */
4604 38 : else if (type != valtype
4605 32 : && (type == dfloat32_type_node
4606 22 : || type == dfloat64_type_node
4607 12 : || type == dfloat128_type_node
4608 2 : || valtype == dfloat32_type_node
4609 2 : || valtype == dfloat64_type_node
4610 2 : || valtype == dfloat128_type_node)
4611 38 : && (formal_prec
4612 30 : <= TYPE_PRECISION (valtype)
4613 14 : || (type == dfloat128_type_node
4614 10 : && (valtype
4615 10 : != dfloat64_type_node
4616 8 : && (valtype
4617 : != dfloat32_type_node)))
4618 8 : || (type == dfloat64_type_node
4619 4 : && (valtype
4620 : != dfloat32_type_node))))
4621 24 : warning_at (ploc, 0,
4622 : "passing argument %d of %qE as %qT "
4623 : "rather than %qT due to prototype",
4624 : argnum, rname, type, valtype);
4625 :
4626 : }
4627 : /* Detect integer changing in width or signedness.
4628 : These warnings are only activated with
4629 : -Wtraditional-conversion, not with -Wtraditional. */
4630 105 : else if (warn_traditional_conversion
4631 105 : && INTEGRAL_TYPE_P (type)
4632 102 : && INTEGRAL_TYPE_P (valtype))
4633 : {
4634 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4635 83 : tree would_have_been = default_conversion (val);
4636 83 : tree type1 = TREE_TYPE (would_have_been);
4637 :
4638 83 : if (val == error_mark_node)
4639 : /* VAL could have been of incomplete type. */;
4640 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4641 83 : && (TYPE_MAIN_VARIANT (type)
4642 2 : == TYPE_MAIN_VARIANT (valtype)))
4643 : /* No warning if function asks for enum
4644 : and the actual arg is that enum type. */
4645 : ;
4646 81 : else if (formal_prec != TYPE_PRECISION (type1))
4647 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4648 : "passing argument %d of %qE "
4649 : "with different width due to prototype",
4650 : argnum, rname);
4651 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4652 : ;
4653 : /* Don't complain if the formal parameter type
4654 : is an enum, because we can't tell now whether
4655 : the value was an enum--even the same enum. */
4656 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4657 : ;
4658 32 : else if (TREE_CODE (val) == INTEGER_CST
4659 5 : && int_fits_type_p (val, type))
4660 : /* Change in signedness doesn't matter
4661 : if a constant value is unaffected. */
4662 : ;
4663 : /* If the value is extended from a narrower
4664 : unsigned type, it doesn't matter whether we
4665 : pass it as signed or unsigned; the value
4666 : certainly is the same either way. */
4667 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4668 32 : && TYPE_UNSIGNED (valtype))
4669 : ;
4670 32 : else if (TYPE_UNSIGNED (type))
4671 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4672 : "passing argument %d of %qE "
4673 : "as unsigned due to prototype",
4674 : argnum, rname);
4675 : else
4676 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4677 : "passing argument %d of %qE "
4678 : "as signed due to prototype",
4679 : argnum, rname);
4680 : }
4681 : }
4682 :
4683 : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4684 : sake of better warnings from convert_and_check. */
4685 125706994 : if (excess_precision)
4686 380 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4687 :
4688 125706994 : tree parmval = convert_for_assignment (ploc, ploc, type,
4689 : val, origtype, ic_argpass,
4690 : npc, fundecl, function,
4691 : parmnum + 1, warnopt);
4692 125706994 : return parmval;
4693 : }
4694 :
4695 : /* Convert the argument expressions in the vector VALUES
4696 : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4697 :
4698 : If the list is exhausted, or when an element has NULL as its type,
4699 : perform the default conversions.
4700 :
4701 : ORIGTYPES is the original types of the expressions in VALUES. This
4702 : holds the type of enum values which have been converted to integral
4703 : types. It may be NULL.
4704 :
4705 : FUNCTION is a tree for the called function. It is used only for
4706 : error messages, where it is formatted with %qE.
4707 :
4708 : This is also where warnings about wrong number of args are generated.
4709 :
4710 : ARG_LOC are locations of function arguments (if any).
4711 :
4712 : Returns the actual number of arguments processed (which may be less
4713 : than the length of VALUES in some error situations), or -1 on
4714 : failure. */
4715 :
4716 : static int
4717 49948064 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4718 : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4719 : tree function, tree fundecl)
4720 : {
4721 49948064 : tree typelist = TYPE_ARG_TYPES (fntype);
4722 49948064 : unsigned int parmnum;
4723 49948064 : bool error_args = false;
4724 49948064 : const bool type_generic = fundecl
4725 49948064 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4726 49948064 : bool type_generic_remove_excess_precision = false;
4727 49948064 : bool type_generic_overflow_p = false;
4728 49948064 : bool type_generic_bit_query = false;
4729 49948064 : tree selector;
4730 :
4731 : /* Change pointer to function to the function itself for
4732 : diagnostics. */
4733 49948064 : if (TREE_CODE (function) == ADDR_EXPR
4734 49948064 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4735 49872670 : function = TREE_OPERAND (function, 0);
4736 :
4737 : /* Handle an ObjC selector specially for diagnostics. */
4738 49948064 : selector = objc_message_selector ();
4739 :
4740 : /* For a call to a built-in function declared without a prototype,
4741 : set to the built-in function's argument list. */
4742 49948064 : tree builtin_typelist = NULL_TREE;
4743 :
4744 : /* For type-generic built-in functions, determine whether excess
4745 : precision should be removed (classification) or not
4746 : (comparison). */
4747 49948064 : if (fundecl
4748 49948064 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4749 : {
4750 1410631 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4751 1410631 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4752 : {
4753 : /* For a call to a built-in function declared without a prototype
4754 : use the types of the parameters of the internal built-in to
4755 : match those of the arguments to. */
4756 692615 : if (tree bdecl = builtin_decl_explicit (code))
4757 692615 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4758 : }
4759 :
4760 : /* For type-generic built-in functions, determine whether excess
4761 : precision should be removed (classification) or not
4762 : (comparison). */
4763 1410631 : if (type_generic)
4764 60994 : switch (code)
4765 : {
4766 5402 : case BUILT_IN_ISFINITE:
4767 5402 : case BUILT_IN_ISINF:
4768 5402 : case BUILT_IN_ISINF_SIGN:
4769 5402 : case BUILT_IN_ISNAN:
4770 5402 : case BUILT_IN_ISNORMAL:
4771 5402 : case BUILT_IN_ISSIGNALING:
4772 5402 : case BUILT_IN_FPCLASSIFY:
4773 5402 : type_generic_remove_excess_precision = true;
4774 5402 : break;
4775 :
4776 21026 : case BUILT_IN_ADD_OVERFLOW_P:
4777 21026 : case BUILT_IN_SUB_OVERFLOW_P:
4778 21026 : case BUILT_IN_MUL_OVERFLOW_P:
4779 : /* The last argument of these type-generic builtins
4780 : should not be promoted. */
4781 21026 : type_generic_overflow_p = true;
4782 21026 : break;
4783 :
4784 1348 : case BUILT_IN_CLZG:
4785 1348 : case BUILT_IN_CTZG:
4786 1348 : case BUILT_IN_CLRSBG:
4787 1348 : case BUILT_IN_FFSG:
4788 1348 : case BUILT_IN_PARITYG:
4789 1348 : case BUILT_IN_POPCOUNTG:
4790 : /* The first argument of these type-generic builtins
4791 : should not be promoted. */
4792 1348 : type_generic_bit_query = true;
4793 1348 : break;
4794 :
4795 : default:
4796 : break;
4797 : }
4798 : }
4799 :
4800 : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4801 : individual converted arguments. */
4802 :
4803 49948064 : tree typetail, builtin_typetail, val;
4804 49948064 : for (typetail = typelist,
4805 49948064 : builtin_typetail = builtin_typelist,
4806 49948064 : parmnum = 0;
4807 176593441 : values && values->iterate (parmnum, &val);
4808 : ++parmnum)
4809 : {
4810 : /* The type of the function parameter (if it was declared with one). */
4811 252350524 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4812 : /* The type of the built-in function parameter (if the function
4813 : is a built-in). Used to detect type incompatibilities in
4814 : calls to built-ins declared without a prototype. */
4815 126645413 : tree builtin_type = (builtin_typetail
4816 127685403 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4817 : /* The original type of the argument being passed to the function. */
4818 126645413 : tree valtype = TREE_TYPE (val);
4819 : /* The called function (or function selector in Objective C). */
4820 126645413 : tree rname = function;
4821 126645413 : int argnum = parmnum + 1;
4822 126645413 : const char *invalid_func_diag;
4823 : /* Set for EXCESS_PRECISION_EXPR arguments. */
4824 126645413 : bool excess_precision = false;
4825 : /* The value of the argument after conversion to the type
4826 : of the function parameter it is passed to. */
4827 126645413 : tree parmval;
4828 : /* Some __atomic_* builtins have additional hidden argument at
4829 : position 0. */
4830 126645413 : location_t ploc
4831 126376067 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4832 126375467 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4833 126645413 : : input_location;
4834 :
4835 126645413 : if (type == void_type_node)
4836 : {
4837 33 : auto_diagnostic_group d;
4838 33 : int num_expected = parmnum;
4839 33 : int num_actual = values->length ();
4840 33 : gcc_rich_location rich_loc (loc);
4841 33 : if (ploc != input_location)
4842 32 : rich_loc.add_range (ploc);
4843 33 : if (selector)
4844 0 : error_at (&rich_loc,
4845 : "too many arguments to method %qE; expected %i, have %i",
4846 : selector, num_expected, num_actual);
4847 : else
4848 33 : error_at (&rich_loc,
4849 : "too many arguments to function %qE; expected %i, have %i",
4850 : function, num_expected, num_actual);
4851 33 : inform_declaration (fundecl, function);
4852 65 : return error_args ? -1 : (int) parmnum;
4853 33 : }
4854 :
4855 126645380 : if (builtin_type == void_type_node)
4856 : {
4857 12 : auto_diagnostic_group d;
4858 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4859 : "too many arguments to built-in function %qE "
4860 : "expecting %d", function, parmnum))
4861 12 : inform_declaration (fundecl, function);
4862 12 : builtin_typetail = NULL_TREE;
4863 12 : }
4864 :
4865 90530 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4866 126735791 : && !(fundecl && fndecl_built_in_p (fundecl)))
4867 : {
4868 6961 : auto_diagnostic_group d;
4869 6961 : bool warned;
4870 6961 : if (selector)
4871 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4872 : "ISO C23 does not allow arguments"
4873 : " for method %qE declared without parameters",
4874 : function);
4875 : else
4876 6961 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4877 : "ISO C23 does not allow arguments"
4878 : " for function %qE declared without parameters",
4879 : function);
4880 6961 : if (warned)
4881 4 : inform_declaration (fundecl, function);
4882 6961 : }
4883 :
4884 126645380 : if (selector && argnum > 2)
4885 : {
4886 0 : rname = selector;
4887 0 : argnum -= 2;
4888 : }
4889 :
4890 : /* Determine if VAL is a null pointer constant before folding it. */
4891 126645380 : bool npc = null_pointer_constant_p (val);
4892 :
4893 : /* If there is excess precision and a prototype, convert once to
4894 : the required type rather than converting via the semantic
4895 : type. Likewise without a prototype a float value represented
4896 : as long double should be converted once to double. But for
4897 : type-generic classification functions excess precision must
4898 : be removed here. */
4899 126645380 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4900 433 : && (type || !type_generic || !type_generic_remove_excess_precision))
4901 : {
4902 413 : val = TREE_OPERAND (val, 0);
4903 413 : excess_precision = true;
4904 : }
4905 126645380 : val = c_fully_fold (val, false, NULL);
4906 253290761 : STRIP_TYPE_NOPS (val);
4907 :
4908 126645380 : val = require_complete_type (ploc, val);
4909 :
4910 : /* Some floating-point arguments must be promoted to double when
4911 : no type is specified by a prototype. This applies to
4912 : arguments of type float, and to architecture-specific types
4913 : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4914 126645380 : bool promote_float_arg = false;
4915 126645380 : if (type == NULL_TREE
4916 940302 : && TREE_CODE (valtype) == REAL_TYPE
4917 269598 : && (TYPE_PRECISION (valtype)
4918 269598 : <= TYPE_PRECISION (double_type_node))
4919 262352 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4920 131376 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4921 126776756 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4922 : {
4923 : /* Promote this argument, unless it has a _FloatN or
4924 : _FloatNx type. */
4925 1027510 : promote_float_arg = true;
4926 1027510 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4927 900144 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4928 : {
4929 : promote_float_arg = false;
4930 : break;
4931 : }
4932 : /* Don't promote __bf16 either. */
4933 130643 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4934 126514807 : promote_float_arg = false;
4935 : }
4936 :
4937 126645380 : if (type != NULL_TREE)
4938 : {
4939 125705078 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4940 125705078 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4941 : val, valtype, npc, rname, parmnum, argnum,
4942 : excess_precision, 0);
4943 : }
4944 : /* A NULLPTR type is just a nullptr always. */
4945 940302 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4946 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4947 940293 : else if (promote_float_arg)
4948 : {
4949 127296 : if (type_generic)
4950 : parmval = val;
4951 : else
4952 : {
4953 : /* Convert `float' to `double'. */
4954 124522 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4955 6 : warning_at (ploc, OPT_Wdouble_promotion,
4956 : "implicit conversion from %qT to %qT when passing "
4957 : "argument to function",
4958 : valtype, double_type_node);
4959 124522 : parmval = convert (double_type_node, val);
4960 : }
4961 : }
4962 812997 : else if ((excess_precision && !type_generic)
4963 812995 : || (type_generic_overflow_p && parmnum == 2)
4964 791973 : || (type_generic_bit_query && parmnum == 0))
4965 : /* A "double" argument with excess precision being passed
4966 : without a prototype or in variable arguments.
4967 : The last argument of __builtin_*_overflow_p should not be
4968 : promoted, similarly the first argument of
4969 : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4970 22366 : parmval = convert (valtype, val);
4971 1581262 : else if ((invalid_func_diag =
4972 790631 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4973 : {
4974 0 : error (invalid_func_diag);
4975 0 : return -1;
4976 : }
4977 790631 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4978 : {
4979 : return -1;
4980 : }
4981 : else
4982 : /* Convert `short' and `char' to full-size `int'. */
4983 790628 : parmval = default_conversion (val);
4984 :
4985 126645377 : (*values)[parmnum] = parmval;
4986 126645377 : if (parmval == error_mark_node)
4987 105 : error_args = true;
4988 :
4989 126645377 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4990 : {
4991 : /* For a call to a built-in function declared without a prototype,
4992 : perform the conversions from the argument to the expected type
4993 : but issue warnings rather than errors for any mismatches.
4994 : Ignore the converted argument and use the PARMVAL obtained
4995 : above by applying default conversions instead. */
4996 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4997 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4998 : val, valtype, npc, rname, parmnum, argnum,
4999 : excess_precision,
5000 : OPT_Wbuiltin_declaration_mismatch);
5001 : }
5002 :
5003 126645377 : if (typetail)
5004 125705078 : typetail = TREE_CHAIN (typetail);
5005 :
5006 126645377 : if (builtin_typetail)
5007 1039973 : builtin_typetail = TREE_CHAIN (builtin_typetail);
5008 : }
5009 :
5010 88166520 : gcc_assert (parmnum == vec_safe_length (values));
5011 :
5012 99563023 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
5013 : {
5014 : /* Not enough args.
5015 : Determine minimum number of arguments required. */
5016 : int min_expected_num = 0;
5017 231 : bool at_least_p = false;
5018 : tree iter = typelist;
5019 386 : while (true)
5020 : {
5021 231 : if (!iter)
5022 : {
5023 : /* Variadic arguments; stop iterating. */
5024 : at_least_p = true;
5025 : break;
5026 : }
5027 222 : if (iter == void_list_node)
5028 : /* End of arguments; stop iterating. */
5029 : break;
5030 155 : ++min_expected_num;
5031 155 : iter = TREE_CHAIN (iter);
5032 : }
5033 76 : auto_diagnostic_group d;
5034 76 : int actual_num = vec_safe_length (values);
5035 143 : error_at (loc,
5036 : at_least_p
5037 : ? G_("too few arguments to function %qE; expected at least %i, have %i")
5038 : : G_("too few arguments to function %qE; expected %i, have %i"),
5039 : function, min_expected_num, actual_num);
5040 76 : inform_declaration (fundecl, function);
5041 76 : return -1;
5042 76 : }
5043 :
5044 50604305 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
5045 : {
5046 : unsigned nargs = parmnum;
5047 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
5048 428 : ++nargs;
5049 :
5050 188 : auto_diagnostic_group d;
5051 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
5052 : "too few arguments to built-in function %qE "
5053 : "expecting %u", function, nargs - 1))
5054 173 : inform_declaration (fundecl, function);
5055 188 : }
5056 :
5057 49947952 : return error_args ? -1 : (int) parmnum;
5058 : }
5059 :
5060 : /* This is the entry point used by the parser to build unary operators
5061 : in the input. CODE, a tree_code, specifies the unary operator, and
5062 : ARG is the operand. For unary plus, the C parser currently uses
5063 : CONVERT_EXPR for code.
5064 :
5065 : LOC is the location to use for the tree generated.
5066 : */
5067 :
5068 : struct c_expr
5069 8176644 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5070 : {
5071 8176644 : struct c_expr result;
5072 :
5073 8176644 : result.original_code = code;
5074 8176644 : result.original_type = NULL;
5075 8176644 : result.m_decimal = 0;
5076 :
5077 8176644 : if (reject_gcc_builtin (arg.value))
5078 : {
5079 2 : result.value = error_mark_node;
5080 : }
5081 : else
5082 : {
5083 8176642 : result.value = build_unary_op (loc, code, arg.value, false);
5084 :
5085 8176642 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
5086 5 : overflow_warning (loc, result.value, arg.value);
5087 : }
5088 :
5089 : /* We are typically called when parsing a prefix token at LOC acting on
5090 : ARG. Reflect this by updating the source range of the result to
5091 : start at LOC and end at the end of ARG. */
5092 8176644 : set_c_expr_source_range (&result,
5093 : loc, arg.get_finish ());
5094 :
5095 8176644 : return result;
5096 : }
5097 :
5098 : /* Returns true if TYPE is a character type, *not* including wchar_t. */
5099 :
5100 : bool
5101 1270553 : char_type_p (tree type)
5102 : {
5103 1270553 : return (type == char_type_node
5104 979483 : || type == unsigned_char_type_node
5105 960384 : || type == signed_char_type_node
5106 959536 : || type == char16_type_node
5107 2009268 : || type == char32_type_node);
5108 : }
5109 :
5110 : /* This is the entry point used by the parser to build binary operators
5111 : in the input. CODE, a tree_code, specifies the binary operator, and
5112 : ARG1 and ARG2 are the operands. In addition to constructing the
5113 : expression, we check for operands that were written with other binary
5114 : operators in a way that is likely to confuse the user. ORIG_ARG1 is
5115 : the original first operand for TRUTH_{AND,OR}IF_EXPR before it is
5116 : converted to truth value, otherwise NULL_TREE.
5117 :
5118 : LOCATION is the location of the binary operator. */
5119 :
5120 : struct c_expr
5121 9370043 : parser_build_binary_op (location_t location, enum tree_code code,
5122 : struct c_expr arg1, struct c_expr arg2, tree orig_arg1)
5123 : {
5124 9370043 : struct c_expr result;
5125 9370043 : result.m_decimal = 0;
5126 :
5127 9370043 : enum tree_code code1 = arg1.original_code;
5128 9370043 : enum tree_code code2 = arg2.original_code;
5129 9370043 : tree type1 = (arg1.original_type
5130 9370043 : ? arg1.original_type
5131 6014690 : : TREE_TYPE (arg1.value));
5132 9370043 : tree type2 = (arg2.original_type
5133 9370043 : ? arg2.original_type
5134 6917701 : : TREE_TYPE (arg2.value));
5135 :
5136 9370043 : result.value = build_binary_op (location, code,
5137 : arg1.value, arg2.value, true);
5138 9370042 : result.original_code = code;
5139 9370042 : result.original_type = NULL;
5140 9370042 : result.m_decimal = 0;
5141 :
5142 9370042 : if (TREE_CODE (result.value) == ERROR_MARK)
5143 : {
5144 1297 : set_c_expr_source_range (&result,
5145 : arg1.get_start (),
5146 : arg2.get_finish ());
5147 1297 : return result;
5148 : }
5149 :
5150 9368745 : if (location != UNKNOWN_LOCATION)
5151 9368745 : protected_set_expr_location (result.value, location);
5152 :
5153 9368745 : set_c_expr_source_range (&result,
5154 : arg1.get_start (),
5155 : arg2.get_finish ());
5156 :
5157 : /* Check for cases such as x+y<<z which users are likely
5158 : to misinterpret. */
5159 9368745 : if (warn_parentheses)
5160 2013150 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5161 : arg2.value);
5162 :
5163 9368745 : if (warn_logical_op)
5164 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5165 : code1, arg1.value, code2, arg2.value);
5166 :
5167 9368745 : if (warn_constant_logical_operand
5168 2011565 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
5169 74492 : && INTEGRAL_NB_TYPE_P (type1)
5170 72691 : && INTEGRAL_NB_TYPE_P (type2))
5171 : {
5172 71098 : const char *name = code == TRUTH_ANDIF_EXPR ? "&&" : "||";
5173 71098 : if (orig_arg1 == NULL_TREE)
5174 0 : orig_arg1 = arg1.value;
5175 76495 : auto enum_other_than_0_1 = [] (tree type) {
5176 5397 : if (TREE_CODE (type) != ENUMERAL_TYPE)
5177 : return false;
5178 48 : for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
5179 : {
5180 40 : tree v = DECL_INITIAL (TREE_VALUE (l));
5181 40 : if (!integer_zerop (v) && !integer_onep (v))
5182 : return true;
5183 : }
5184 : return false;
5185 : };
5186 213278 : auto diagnose_constant_logical_operand = [=] (tree val, tree type) {
5187 142180 : if (TREE_CODE (val) != INTEGER_CST || integer_zerop (val))
5188 136759 : return false;
5189 5421 : if (integer_onep (val) && !enum_other_than_0_1 (type))
5190 : return false;
5191 32 : gcc_rich_location richloc (location);
5192 32 : richloc.add_fixit_replace (name + 1);
5193 32 : auto_diagnostic_group d;
5194 32 : if (warning_at (location, OPT_Wconstant_logical_operand,
5195 : "use of logical %qs with constant operand %qE",
5196 : name, val))
5197 32 : inform (&richloc, "use %qs for bitwise operation", name + 1);
5198 32 : return true;
5199 32 : };
5200 71098 : if (!diagnose_constant_logical_operand (arg2.value, type2))
5201 71082 : diagnose_constant_logical_operand (orig_arg1, type1);
5202 : }
5203 :
5204 9368745 : if (warn_tautological_compare)
5205 : {
5206 2011663 : tree lhs = arg1.value;
5207 2011663 : tree rhs = arg2.value;
5208 2011663 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5209 : {
5210 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5211 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5212 : lhs = NULL_TREE;
5213 : else
5214 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5215 : }
5216 2011663 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5217 : {
5218 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5219 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5220 : rhs = NULL_TREE;
5221 : else
5222 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5223 : }
5224 2011663 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5225 2011663 : warn_tautological_cmp (location, code, lhs, rhs);
5226 : }
5227 :
5228 9368745 : if (warn_logical_not_paren
5229 2011746 : && TREE_CODE_CLASS (code) == tcc_comparison
5230 410229 : && code1 == TRUTH_NOT_EXPR
5231 410229 : && code2 != TRUTH_NOT_EXPR
5232 : /* Avoid warning for !!x == y. */
5233 9368879 : && (TREE_CODE (arg1.value) != NE_EXPR
5234 27 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5235 : {
5236 : /* Avoid warning for !b == y where b has _Bool type. */
5237 107 : tree t = integer_zero_node;
5238 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5239 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5240 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5241 : {
5242 90 : t = TREE_OPERAND (arg1.value, 0);
5243 199 : do
5244 : {
5245 199 : if (TREE_TYPE (t) != integer_type_node)
5246 : break;
5247 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5248 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5249 90 : else if (CONVERT_EXPR_P (t))
5250 19 : t = TREE_OPERAND (t, 0);
5251 : else
5252 : break;
5253 : }
5254 : while (1);
5255 : }
5256 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5257 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5258 : }
5259 :
5260 : /* Warn about comparisons against string literals, with the exception
5261 : of testing for equality or inequality of a string literal with NULL. */
5262 9368745 : if (code == EQ_EXPR || code == NE_EXPR)
5263 : {
5264 1176676 : if ((code1 == STRING_CST
5265 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5266 1176687 : || (code2 == STRING_CST
5267 39 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5268 36 : warning_at (location, OPT_Waddress,
5269 : "comparison with string literal results in unspecified behavior");
5270 : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5271 1176676 : if (POINTER_TYPE_P (type1)
5272 92462 : && null_pointer_constant_p (arg2.value)
5273 1197993 : && char_type_p (type2))
5274 : {
5275 18 : auto_diagnostic_group d;
5276 18 : if (warning_at (location, OPT_Wpointer_compare,
5277 : "comparison between pointer and zero character "
5278 : "constant"))
5279 10 : inform (arg1.get_start (),
5280 : "did you mean to dereference the pointer?");
5281 18 : }
5282 1176658 : else if (POINTER_TYPE_P (type2)
5283 89805 : && null_pointer_constant_p (arg1.value)
5284 1176930 : && char_type_p (type1))
5285 : {
5286 10 : auto_diagnostic_group d;
5287 10 : if (warning_at (location, OPT_Wpointer_compare,
5288 : "comparison between pointer and zero character "
5289 : "constant"))
5290 10 : inform (arg2.get_start (),
5291 : "did you mean to dereference the pointer?");
5292 10 : }
5293 : }
5294 8192069 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5295 1006335 : && (code1 == STRING_CST || code2 == STRING_CST))
5296 0 : warning_at (location, OPT_Waddress,
5297 : "comparison with string literal results in unspecified "
5298 : "behavior");
5299 :
5300 9368745 : if (warn_zero_as_null_pointer_constant
5301 24 : && c_inhibit_evaluation_warnings == 0
5302 24 : && TREE_CODE_CLASS (code) == tcc_comparison)
5303 : {
5304 24 : if ((TREE_CODE (type1) == POINTER_TYPE
5305 17 : || TREE_CODE (type1) == NULLPTR_TYPE)
5306 9 : && INTEGRAL_TYPE_P (type2)
5307 33 : && null_pointer_constant_p (arg2.value))
5308 9 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5309 : "zero as null pointer constant");
5310 :
5311 24 : if ((TREE_CODE (type2) == POINTER_TYPE
5312 15 : || TREE_CODE (type2) == NULLPTR_TYPE)
5313 11 : && INTEGRAL_TYPE_P (type1)
5314 35 : && null_pointer_constant_p (arg1.value))
5315 11 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5316 : "zero as null pointer constant");
5317 : }
5318 :
5319 9368745 : if (warn_array_compare
5320 2011501 : && TREE_CODE_CLASS (code) == tcc_comparison
5321 410014 : && TREE_CODE (type1) == ARRAY_TYPE
5322 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5323 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5324 :
5325 2198210 : if (TREE_OVERFLOW_P (result.value)
5326 299 : && !TREE_OVERFLOW_P (arg1.value)
5327 9369037 : && !TREE_OVERFLOW_P (arg2.value))
5328 249 : overflow_warning (location, result.value);
5329 :
5330 : /* Warn about comparisons of different enum types. */
5331 9368744 : if (warn_enum_compare
5332 2031623 : && TREE_CODE_CLASS (code) == tcc_comparison
5333 414555 : && TREE_CODE (type1) == ENUMERAL_TYPE
5334 7253 : && TREE_CODE (type2) == ENUMERAL_TYPE
5335 9375933 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5336 2 : warning_at (location, OPT_Wenum_compare,
5337 : "comparison between %qT and %qT",
5338 : type1, type2);
5339 :
5340 9368744 : if (warn_xor_used_as_pow
5341 9368744 : && code == BIT_XOR_EXPR
5342 77303 : && arg1.m_decimal
5343 732 : && arg2.m_decimal)
5344 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5345 : location,
5346 : arg2.get_location (), arg2.value);
5347 :
5348 : return result;
5349 : }
5350 :
5351 : /* Return a tree for the difference of pointers OP0 and OP1.
5352 : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5353 : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5354 :
5355 : static tree
5356 3741 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5357 : {
5358 3741 : tree restype = ptrdiff_type_node;
5359 3741 : tree result, inttype;
5360 :
5361 3741 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5362 3741 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5363 3741 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5364 3741 : tree orig_op0 = op0;
5365 3741 : tree orig_op1 = op1;
5366 :
5367 : /* If the operands point into different address spaces, we need to
5368 : explicitly convert them to pointers into the common address space
5369 : before we can subtract the numerical address values. */
5370 3741 : if (as0 != as1)
5371 : {
5372 0 : addr_space_t as_common;
5373 0 : tree common_type;
5374 :
5375 : /* Determine the common superset address space. This is guaranteed
5376 : to exist because the caller verified that comp_target_types
5377 : returned non-zero. */
5378 0 : if (!addr_space_superset (as0, as1, &as_common))
5379 0 : gcc_unreachable ();
5380 :
5381 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5382 0 : op0 = convert (common_type, op0);
5383 0 : op1 = convert (common_type, op1);
5384 : }
5385 :
5386 : /* Determine integer type result of the subtraction. This will usually
5387 : be the same as the result type (ptrdiff_t), but may need to be a wider
5388 : type if pointers for the address space are wider than ptrdiff_t. */
5389 3741 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5390 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5391 : else
5392 : inttype = restype;
5393 :
5394 3741 : if (VOID_TYPE_P (target_type))
5395 146 : pedwarn (loc, OPT_Wpointer_arith,
5396 : "pointer of type %<void *%> used in subtraction");
5397 3741 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5398 4 : pedwarn (loc, OPT_Wpointer_arith,
5399 : "pointer to a function used in subtraction");
5400 :
5401 3741 : if (current_function_decl != NULL_TREE
5402 3741 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5403 : {
5404 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5405 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5406 :
5407 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5408 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5409 : }
5410 :
5411 : /* First do the subtraction, then build the divide operator
5412 : and only convert at the very end.
5413 : Do not do default conversions in case restype is a short type. */
5414 :
5415 : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5416 : pointers. If some platform cannot provide that, or has a larger
5417 : ptrdiff_type to support differences larger than half the address
5418 : space, cast the pointers to some larger integer type and do the
5419 : computations in that type. */
5420 3741 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5421 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5422 : convert (inttype, op1), false);
5423 : else
5424 : {
5425 : /* Cast away qualifiers. */
5426 3741 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5427 3741 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5428 3741 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5429 : }
5430 :
5431 : /* This generates an error if op1 is pointer to incomplete type. */
5432 3741 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5433 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5434 3737 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5435 3737 : TREE_TYPE (TREE_TYPE (orig_op0))))
5436 7474 : verify_type_context (loc, TCTX_POINTER_ARITH,
5437 3737 : TREE_TYPE (TREE_TYPE (orig_op1)));
5438 :
5439 3741 : op1 = c_size_in_bytes (target_type);
5440 :
5441 3741 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5442 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5443 :
5444 : /* Divide by the size, in easiest possible way. */
5445 3741 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5446 : op0, convert (inttype, op1));
5447 :
5448 : /* Convert to final result type if necessary. */
5449 3741 : return convert (restype, result);
5450 : }
5451 :
5452 : /* Expand atomic compound assignments into an appropriate sequence as
5453 : specified by the C11 standard section 6.5.16.2.
5454 :
5455 : _Atomic T1 E1
5456 : T2 E2
5457 : E1 op= E2
5458 :
5459 : This sequence is used for all types for which these operations are
5460 : supported.
5461 :
5462 : In addition, built-in versions of the 'fe' prefixed routines may
5463 : need to be invoked for floating point (real, complex or vector) when
5464 : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5465 :
5466 : T1 newval;
5467 : T1 old;
5468 : T1 *addr
5469 : T2 val
5470 : fenv_t fenv
5471 :
5472 : addr = &E1;
5473 : val = (E2);
5474 : __atomic_load (addr, &old, SEQ_CST);
5475 : feholdexcept (&fenv);
5476 : loop:
5477 : newval = old op val;
5478 : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5479 : SEQ_CST))
5480 : goto done;
5481 : feclearexcept (FE_ALL_EXCEPT);
5482 : goto loop:
5483 : done:
5484 : feupdateenv (&fenv);
5485 :
5486 : The compiler will issue the __atomic_fetch_* built-in when possible,
5487 : otherwise it will generate the generic form of the atomic operations.
5488 : This requires temp(s) and has their address taken. The atomic processing
5489 : is smart enough to figure out when the size of an object can utilize
5490 : a lock-free version, and convert the built-in call to the appropriate
5491 : lock-free routine. The optimizers will then dispose of any temps that
5492 : are no longer required, and lock-free implementations are utilized as
5493 : long as there is target support for the required size.
5494 :
5495 : If the operator is NOP_EXPR, then this is a simple assignment, and
5496 : an __atomic_store is issued to perform the assignment rather than
5497 : the above loop. */
5498 :
5499 : /* Build an atomic assignment at LOC, expanding into the proper
5500 : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5501 : the result of the operation, unless RETURN_OLD_P, in which case
5502 : return the old value of LHS (this is only for postincrement and
5503 : postdecrement). */
5504 :
5505 : static tree
5506 30433 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5507 : tree rhs, bool return_old_p)
5508 : {
5509 30433 : tree fndecl, func_call;
5510 30433 : vec<tree, va_gc> *params;
5511 30433 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5512 30433 : tree old, old_addr;
5513 30433 : tree compound_stmt = NULL_TREE;
5514 30433 : tree stmt, goto_stmt;
5515 30433 : tree loop_label, loop_decl, done_label, done_decl;
5516 :
5517 30433 : tree lhs_type = TREE_TYPE (lhs);
5518 30433 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5519 30433 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5520 30433 : tree rhs_semantic_type = TREE_TYPE (rhs);
5521 30433 : tree nonatomic_rhs_semantic_type;
5522 30433 : tree rhs_type;
5523 :
5524 30433 : gcc_assert (TYPE_ATOMIC (lhs_type));
5525 :
5526 30433 : if (return_old_p)
5527 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5528 :
5529 : /* Allocate enough vector items for a compare_exchange. */
5530 30433 : vec_alloc (params, 6);
5531 :
5532 : /* Create a compound statement to hold the sequence of statements
5533 : with a loop. */
5534 30433 : if (modifycode != NOP_EXPR)
5535 : {
5536 19954 : compound_stmt = c_begin_compound_stmt (false);
5537 :
5538 : /* For consistency with build_modify_expr on non-_Atomic,
5539 : mark the lhs as read. Also, it would be very hard to match
5540 : such expressions in mark_exp_read. */
5541 19954 : mark_exp_read (lhs);
5542 : }
5543 :
5544 : /* Remove any excess precision (which is only present here in the
5545 : case of compound assignments). */
5546 30433 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5547 : {
5548 0 : gcc_assert (modifycode != NOP_EXPR);
5549 0 : rhs = TREE_OPERAND (rhs, 0);
5550 : }
5551 30433 : rhs_type = TREE_TYPE (rhs);
5552 :
5553 : /* Fold the RHS if it hasn't already been folded. */
5554 30433 : if (modifycode != NOP_EXPR)
5555 19954 : rhs = c_fully_fold (rhs, false, NULL);
5556 :
5557 : /* Remove the qualifiers for the rest of the expressions and create
5558 : the VAL temp variable to hold the RHS. */
5559 30433 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5560 30433 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5561 30433 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5562 : TYPE_UNQUALIFIED);
5563 30433 : val = create_tmp_var_raw (nonatomic_rhs_type);
5564 30433 : TREE_ADDRESSABLE (val) = 1;
5565 30433 : suppress_warning (val);
5566 30433 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5567 : NULL_TREE);
5568 30433 : TREE_SIDE_EFFECTS (rhs) = 1;
5569 30433 : SET_EXPR_LOCATION (rhs, loc);
5570 30433 : if (modifycode != NOP_EXPR)
5571 19954 : add_stmt (rhs);
5572 :
5573 : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5574 : an atomic_store. */
5575 30433 : if (modifycode == NOP_EXPR)
5576 : {
5577 10479 : compound_stmt = rhs;
5578 : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5579 10479 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5580 10479 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5581 10479 : params->quick_push (lhs_addr);
5582 10479 : params->quick_push (rhs);
5583 10479 : params->quick_push (seq_cst);
5584 10479 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5585 :
5586 10479 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5587 : compound_stmt, func_call);
5588 :
5589 : /* VAL is the value which was stored, return a COMPOUND_STMT of
5590 : the statement and that value. */
5591 10479 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5592 : }
5593 :
5594 : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5595 : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5596 : isn't applicable for such builtins. ??? Do we want to handle enums? */
5597 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5598 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5599 : {
5600 11713 : built_in_function fncode;
5601 11713 : switch (modifycode)
5602 : {
5603 3075 : case PLUS_EXPR:
5604 3075 : case POINTER_PLUS_EXPR:
5605 2124 : fncode = (return_old_p
5606 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5607 : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5608 : break;
5609 2889 : case MINUS_EXPR:
5610 2094 : fncode = (return_old_p
5611 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5612 : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5613 : break;
5614 609 : case BIT_AND_EXPR:
5615 609 : fncode = (return_old_p
5616 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5617 : : BUILT_IN_ATOMIC_AND_FETCH_N);
5618 : break;
5619 609 : case BIT_IOR_EXPR:
5620 609 : fncode = (return_old_p
5621 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5622 : : BUILT_IN_ATOMIC_OR_FETCH_N);
5623 : break;
5624 609 : case BIT_XOR_EXPR:
5625 609 : fncode = (return_old_p
5626 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5627 : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5628 : break;
5629 3922 : default:
5630 3922 : goto cas_loop;
5631 : }
5632 :
5633 : /* We can only use "_1" through "_16" variants of the atomic fetch
5634 : built-ins. */
5635 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5636 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5637 0 : goto cas_loop;
5638 :
5639 : /* If this is a pointer type, we need to multiply by the size of
5640 : the pointer target type. */
5641 7791 : if (POINTER_TYPE_P (lhs_type))
5642 : {
5643 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5644 : /* ??? This would introduce -Wdiscarded-qualifiers
5645 : warning: __atomic_fetch_* expect volatile void *
5646 : type as the first argument. (Assignments between
5647 : atomic and non-atomic objects are OK.) */
5648 1018 : || TYPE_RESTRICT (lhs_type))
5649 17 : goto cas_loop;
5650 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5651 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5652 : convert (ptrdiff_type_node, rhs),
5653 : convert (ptrdiff_type_node, sz));
5654 : }
5655 :
5656 : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5657 : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5658 7774 : fndecl = builtin_decl_explicit (fncode);
5659 7774 : params->quick_push (lhs_addr);
5660 7774 : params->quick_push (rhs);
5661 7774 : params->quick_push (seq_cst);
5662 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5663 :
5664 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5665 7774 : TREE_ADDRESSABLE (newval) = 1;
5666 7774 : suppress_warning (newval);
5667 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5668 : NULL_TREE, NULL_TREE);
5669 7774 : SET_EXPR_LOCATION (rhs, loc);
5670 7774 : add_stmt (rhs);
5671 :
5672 : /* Finish the compound statement. */
5673 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5674 :
5675 : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5676 : the statement and that value. */
5677 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5678 : }
5679 :
5680 8241 : cas_loop:
5681 : /* Create the variables and labels required for the op= form. */
5682 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5683 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5684 12180 : TREE_ADDRESSABLE (old) = 1;
5685 12180 : suppress_warning (old);
5686 :
5687 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5688 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5689 12180 : TREE_ADDRESSABLE (newval) = 1;
5690 12180 : suppress_warning (newval);
5691 :
5692 12180 : loop_decl = create_artificial_label (loc);
5693 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5694 :
5695 12180 : done_decl = create_artificial_label (loc);
5696 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5697 :
5698 : /* __atomic_load (addr, &old, SEQ_CST). */
5699 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5700 12180 : params->quick_push (lhs_addr);
5701 12180 : params->quick_push (old_addr);
5702 12180 : params->quick_push (seq_cst);
5703 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5704 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5705 : NULL_TREE);
5706 12180 : add_stmt (old);
5707 12180 : params->truncate (0);
5708 :
5709 : /* Create the expressions for floating-point environment
5710 : manipulation, if required. */
5711 12180 : bool need_fenv = (flag_trapping_math
5712 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5713 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5714 12180 : if (need_fenv)
5715 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5716 :
5717 12180 : if (hold_call)
5718 7054 : add_stmt (hold_call);
5719 :
5720 : /* loop: */
5721 12180 : add_stmt (loop_label);
5722 :
5723 : /* newval = old + val; */
5724 12180 : if (rhs_type != rhs_semantic_type)
5725 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5726 12180 : rhs = build_binary_op (loc, modifycode,
5727 : convert_lvalue_to_rvalue (loc, old, true, true),
5728 : val, true);
5729 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5730 : {
5731 0 : tree eptype = TREE_TYPE (rhs);
5732 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5733 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5734 : }
5735 : else
5736 12180 : rhs = c_fully_fold (rhs, false, NULL);
5737 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5738 : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5739 : NULL_TREE, 0);
5740 : /* The temporary variable for NEWVAL is only gimplified correctly (in
5741 : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5742 : subsequent ICEs in nested function processing after an error, ensure such
5743 : a TARGET_EXPR is built even after an error. */
5744 12180 : if (rhs == error_mark_node)
5745 10 : rhs = old;
5746 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5747 : NULL_TREE);
5748 12180 : SET_EXPR_LOCATION (rhs, loc);
5749 12180 : add_stmt (rhs);
5750 :
5751 : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5752 : goto done; */
5753 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5754 12180 : params->quick_push (lhs_addr);
5755 12180 : params->quick_push (old_addr);
5756 12180 : params->quick_push (newval_addr);
5757 12180 : params->quick_push (integer_zero_node);
5758 12180 : params->quick_push (seq_cst);
5759 12180 : params->quick_push (seq_cst);
5760 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5761 :
5762 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5763 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5764 :
5765 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5766 12180 : SET_EXPR_LOCATION (stmt, loc);
5767 12180 : add_stmt (stmt);
5768 :
5769 12180 : if (clear_call)
5770 7054 : add_stmt (clear_call);
5771 :
5772 : /* goto loop; */
5773 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5774 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5775 12180 : add_stmt (goto_stmt);
5776 :
5777 : /* done: */
5778 12180 : add_stmt (done_label);
5779 :
5780 12180 : if (update_call)
5781 7054 : add_stmt (update_call);
5782 :
5783 : /* Finish the compound statement. */
5784 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5785 :
5786 : /* NEWVAL is the value that was successfully stored, return a
5787 : COMPOUND_EXPR of the statement and the appropriate value. */
5788 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5789 12180 : return_old_p ? old : newval);
5790 : }
5791 :
5792 : /* Construct and perhaps optimize a tree representation
5793 : for a unary operation. CODE, a tree_code, specifies the operation
5794 : and XARG is the operand.
5795 : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5796 : promotions (such as from short to int).
5797 : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5798 : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5799 : to pointers in C99.
5800 :
5801 : LOCATION is the location of the operator. */
5802 :
5803 : tree
5804 60602581 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5805 : bool noconvert)
5806 : {
5807 : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5808 60602581 : tree arg = xarg;
5809 60602581 : tree argtype = NULL_TREE;
5810 60602581 : enum tree_code typecode;
5811 60602581 : tree val;
5812 60602581 : tree ret = error_mark_node;
5813 60602581 : tree eptype = NULL_TREE;
5814 60602581 : const char *invalid_op_diag;
5815 :
5816 : /* If ARG is wrapped in a call to .ACCESS_WITH_SIZE — created when the
5817 : C parser rvalue-converts a counted_by-annotated member access (see
5818 : PR123569) — unwrap it here. Unary operators that consume an rvalue
5819 : (!, -, +) read the value itself rather than dereferencing into the
5820 : pointed-to data, so the bounds-checking wrapper is unnecessary and
5821 : would otherwise reach build_unary_op while it is not equipped to
5822 : handle the wrapped form. PR123569 fixed the ++/-- side of this by
5823 : suppressing wrapper creation in the parser; the rvalue-consuming
5824 : ops still receive the wrapper and need to strip it here. */
5825 60602581 : if (is_access_with_size_p (arg))
5826 1 : xarg = arg = get_ref_from_access_with_size (arg);
5827 :
5828 60602581 : gcc_checking_assert (!is_access_with_size_p (arg));
5829 :
5830 60602581 : bool int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5831 6468176 : if (int_operands)
5832 6468176 : arg = remove_c_maybe_const_expr (arg);
5833 :
5834 60602581 : if (code != ADDR_EXPR)
5835 8555650 : arg = require_complete_type (location, arg);
5836 :
5837 60602581 : typecode = TREE_CODE (TREE_TYPE (arg));
5838 60602581 : if (typecode == ERROR_MARK)
5839 74 : return error_mark_node;
5840 60602507 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5841 32292 : typecode = INTEGER_TYPE;
5842 :
5843 121205014 : if ((invalid_op_diag
5844 60602507 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5845 : {
5846 0 : error_at (location, invalid_op_diag);
5847 0 : return error_mark_node;
5848 : }
5849 :
5850 60602507 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5851 : {
5852 245 : eptype = TREE_TYPE (arg);
5853 245 : arg = TREE_OPERAND (arg, 0);
5854 : }
5855 :
5856 60602507 : switch (code)
5857 : {
5858 28760 : case CONVERT_EXPR:
5859 : /* This is used for unary plus, because a CONVERT_EXPR
5860 : is enough to prevent anybody from looking inside for
5861 : associativity, but won't generate any code. */
5862 28765 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5863 13 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5864 10 : || typecode == BITINT_TYPE
5865 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5866 : {
5867 1 : error_at (location, "wrong type argument to unary plus");
5868 1 : return error_mark_node;
5869 : }
5870 28759 : else if (!noconvert)
5871 28759 : arg = default_conversion (arg);
5872 28759 : arg = non_lvalue_loc (location, arg);
5873 28759 : break;
5874 :
5875 6704618 : case NEGATE_EXPR:
5876 7152326 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5877 484380 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5878 453113 : || typecode == BITINT_TYPE
5879 447708 : || gnu_vector_type_p (TREE_TYPE (arg))))
5880 : {
5881 1 : error_at (location, "wrong type argument to unary minus");
5882 1 : return error_mark_node;
5883 : }
5884 6704617 : else if (!noconvert)
5885 6704611 : arg = default_conversion (arg);
5886 : break;
5887 :
5888 295599 : case BIT_NOT_EXPR:
5889 : /* ~ works on integer types and non float vectors. */
5890 295599 : if (typecode == INTEGER_TYPE
5891 295599 : || typecode == BITINT_TYPE
5892 295599 : || (gnu_vector_type_p (TREE_TYPE (arg))
5893 2066 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5894 : {
5895 : tree e = arg;
5896 :
5897 : /* Warn if the expression has boolean value. */
5898 295000 : while (TREE_CODE (e) == COMPOUND_EXPR)
5899 11 : e = TREE_OPERAND (e, 1);
5900 :
5901 589962 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5902 589962 : || truth_value_p (TREE_CODE (e))))
5903 : {
5904 148 : auto_diagnostic_group d;
5905 148 : if (warning_at (location, OPT_Wbool_operation,
5906 : "%<~%> on a boolean expression"))
5907 : {
5908 12 : gcc_rich_location richloc (location);
5909 12 : richloc.add_fixit_insert_before (location, "!");
5910 12 : inform (&richloc, "did you mean to use logical not?");
5911 12 : }
5912 148 : }
5913 294989 : if (!noconvert)
5914 294986 : arg = default_conversion (arg);
5915 : }
5916 610 : else if (typecode == COMPLEX_TYPE)
5917 : {
5918 605 : code = CONJ_EXPR;
5919 605 : pedwarn (location, OPT_Wpedantic,
5920 : "ISO C does not support %<~%> for complex conjugation");
5921 605 : if (!noconvert)
5922 605 : arg = default_conversion (arg);
5923 : }
5924 : else
5925 : {
5926 5 : error_at (location, "wrong type argument to bit-complement");
5927 5 : return error_mark_node;
5928 : }
5929 : break;
5930 :
5931 3 : case ABS_EXPR:
5932 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5933 : {
5934 0 : error_at (location, "wrong type argument to abs");
5935 0 : return error_mark_node;
5936 : }
5937 3 : else if (!noconvert)
5938 0 : arg = default_conversion (arg);
5939 : break;
5940 :
5941 7 : case ABSU_EXPR:
5942 7 : if (!(typecode == INTEGER_TYPE))
5943 : {
5944 0 : error_at (location, "wrong type argument to absu");
5945 0 : return error_mark_node;
5946 : }
5947 7 : else if (!noconvert)
5948 0 : arg = default_conversion (arg);
5949 : break;
5950 :
5951 0 : case CONJ_EXPR:
5952 : /* Conjugating a real value is a no-op, but allow it anyway. */
5953 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5954 : || typecode == COMPLEX_TYPE))
5955 : {
5956 0 : error_at (location, "wrong type argument to conjugation");
5957 0 : return error_mark_node;
5958 : }
5959 0 : else if (!noconvert)
5960 0 : arg = default_conversion (arg);
5961 : break;
5962 :
5963 378533 : case TRUTH_NOT_EXPR:
5964 378533 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5965 7224 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5966 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5967 18 : && typecode != BITINT_TYPE)
5968 : {
5969 10 : error_at (location,
5970 : "wrong type argument to unary exclamation mark");
5971 10 : return error_mark_node;
5972 : }
5973 378523 : if (int_operands)
5974 : {
5975 139865 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5976 139865 : arg = remove_c_maybe_const_expr (arg);
5977 : }
5978 : else
5979 238658 : arg = c_objc_common_truthvalue_conversion (location, arg);
5980 378523 : ret = invert_truthvalue_loc (location, arg);
5981 : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5982 378523 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5983 238265 : location = EXPR_LOCATION (ret);
5984 378523 : goto return_build_unary_op;
5985 :
5986 203100 : case REALPART_EXPR:
5987 203100 : case IMAGPART_EXPR:
5988 203100 : ret = build_real_imag_expr (location, code, arg);
5989 203100 : if (ret == error_mark_node)
5990 : return error_mark_node;
5991 203092 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5992 2 : eptype = TREE_TYPE (eptype);
5993 203092 : goto return_build_unary_op;
5994 :
5995 944987 : case PREINCREMENT_EXPR:
5996 944987 : case POSTINCREMENT_EXPR:
5997 944987 : case PREDECREMENT_EXPR:
5998 944987 : case POSTDECREMENT_EXPR:
5999 :
6000 944987 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6001 : {
6002 2 : tree inner = build_unary_op (location, code,
6003 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
6004 : noconvert);
6005 2 : if (inner == error_mark_node)
6006 : return error_mark_node;
6007 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6008 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6009 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6010 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
6011 2 : goto return_build_unary_op;
6012 : }
6013 :
6014 : /* Complain about anything that is not a true lvalue. In
6015 : Objective-C, skip this check for property_refs. */
6016 944985 : if (!objc_is_property_ref (arg)
6017 1889970 : && !lvalue_or_else (location,
6018 944985 : arg, ((code == PREINCREMENT_EXPR
6019 944985 : || code == POSTINCREMENT_EXPR)
6020 : ? lv_increment
6021 : : lv_decrement)))
6022 22 : return error_mark_node;
6023 :
6024 944963 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
6025 : {
6026 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6027 2 : warning_at (location, OPT_Wc___compat,
6028 : "increment of enumeration value is invalid in C++");
6029 : else
6030 2 : warning_at (location, OPT_Wc___compat,
6031 : "decrement of enumeration value is invalid in C++");
6032 : }
6033 :
6034 944963 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6035 : {
6036 664 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6037 328 : warning_at (location, OPT_Wbool_operation,
6038 : "increment of a boolean expression");
6039 : else
6040 336 : warning_at (location, OPT_Wbool_operation,
6041 : "decrement of a boolean expression");
6042 : }
6043 :
6044 : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
6045 944963 : arg = c_fully_fold (arg, false, NULL, true);
6046 :
6047 944963 : bool atomic_op;
6048 944963 : atomic_op = really_atomic_lvalue (arg);
6049 :
6050 : /* Increment or decrement the real part of the value,
6051 : and don't change the imaginary part. */
6052 944963 : if (typecode == COMPLEX_TYPE)
6053 : {
6054 57 : tree real, imag;
6055 :
6056 57 : pedwarn_c23 (location, OPT_Wpedantic,
6057 : "ISO C does not support %<++%> and %<--%> on complex "
6058 : "types before C2Y");
6059 :
6060 57 : if (!atomic_op)
6061 : {
6062 53 : arg = stabilize_reference (arg);
6063 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
6064 : true);
6065 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
6066 : true);
6067 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
6068 53 : if (real == error_mark_node || imag == error_mark_node)
6069 : return error_mark_node;
6070 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
6071 : real, imag);
6072 53 : goto return_build_unary_op;
6073 : }
6074 : }
6075 :
6076 : /* Report invalid types. */
6077 :
6078 944910 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
6079 711917 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
6080 208 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
6081 944966 : && !gnu_vector_type_p (TREE_TYPE (arg)))
6082 : {
6083 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6084 3 : error_at (location, "wrong type argument to increment");
6085 : else
6086 2 : error_at (location, "wrong type argument to decrement");
6087 :
6088 5 : return error_mark_node;
6089 : }
6090 :
6091 944905 : {
6092 944905 : tree inc;
6093 :
6094 944905 : argtype = TREE_TYPE (arg);
6095 :
6096 : /* Compute the increment. */
6097 :
6098 944905 : if (typecode == POINTER_TYPE)
6099 : {
6100 : /* If pointer target is an incomplete type,
6101 : we just cannot know how to do the arithmetic. */
6102 232993 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
6103 : {
6104 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6105 17 : error_at (location,
6106 : "increment of pointer to an incomplete type %qT",
6107 17 : TREE_TYPE (argtype));
6108 : else
6109 10 : error_at (location,
6110 : "decrement of pointer to an incomplete type %qT",
6111 10 : TREE_TYPE (argtype));
6112 : }
6113 232966 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
6114 232966 : || VOID_TYPE_P (TREE_TYPE (argtype)))
6115 : {
6116 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
6117 6 : pedwarn (location, OPT_Wpointer_arith,
6118 : "wrong type argument to increment");
6119 : else
6120 4 : pedwarn (location, OPT_Wpointer_arith,
6121 : "wrong type argument to decrement");
6122 : }
6123 : else
6124 465912 : verify_type_context (location, TCTX_POINTER_ARITH,
6125 232956 : TREE_TYPE (argtype));
6126 :
6127 232993 : inc = c_size_in_bytes (TREE_TYPE (argtype));
6128 232993 : inc = convert_to_ptrofftype_loc (location, inc);
6129 : }
6130 711912 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
6131 : {
6132 : /* For signed fract types, we invert ++ to -- or
6133 : -- to ++, and change inc from 1 to -1, because
6134 : it is not possible to represent 1 in signed fract constants.
6135 : For unsigned fract types, the result always overflows and
6136 : we get an undefined (original) or the maximum value. */
6137 0 : if (code == PREINCREMENT_EXPR)
6138 : code = PREDECREMENT_EXPR;
6139 : else if (code == PREDECREMENT_EXPR)
6140 : code = PREINCREMENT_EXPR;
6141 : else if (code == POSTINCREMENT_EXPR)
6142 : code = POSTDECREMENT_EXPR;
6143 : else /* code == POSTDECREMENT_EXPR */
6144 0 : code = POSTINCREMENT_EXPR;
6145 :
6146 0 : inc = integer_minus_one_node;
6147 0 : inc = convert (argtype, inc);
6148 : }
6149 : else
6150 : {
6151 711861 : inc = VECTOR_TYPE_P (argtype)
6152 711912 : ? build_one_cst (argtype)
6153 : : integer_one_node;
6154 711912 : inc = convert (argtype, inc);
6155 : }
6156 :
6157 : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6158 : need to ask Objective-C to build the increment or decrement
6159 : expression for it. */
6160 944905 : if (objc_is_property_ref (arg))
6161 0 : return objc_build_incr_expr_for_property_ref (location, code,
6162 20 : arg, inc);
6163 :
6164 : /* Report a read-only lvalue. */
6165 944905 : if (TYPE_READONLY (argtype))
6166 : {
6167 20 : readonly_error (location, arg,
6168 20 : ((code == PREINCREMENT_EXPR
6169 20 : || code == POSTINCREMENT_EXPR)
6170 : ? lv_increment : lv_decrement));
6171 20 : return error_mark_node;
6172 : }
6173 944885 : else if (TREE_READONLY (arg))
6174 4 : readonly_warning (arg,
6175 4 : ((code == PREINCREMENT_EXPR
6176 4 : || code == POSTINCREMENT_EXPR)
6177 : ? lv_increment : lv_decrement));
6178 :
6179 : /* If the argument is atomic, use the special code sequences for
6180 : atomic compound assignment. */
6181 944885 : if (atomic_op)
6182 : {
6183 4469 : arg = stabilize_reference (arg);
6184 8938 : ret = build_atomic_assign (location, arg,
6185 4469 : ((code == PREINCREMENT_EXPR
6186 4469 : || code == POSTINCREMENT_EXPR)
6187 : ? PLUS_EXPR
6188 : : MINUS_EXPR),
6189 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
6190 : ? inc
6191 : : integer_one_node),
6192 : (code == POSTINCREMENT_EXPR
6193 4469 : || code == POSTDECREMENT_EXPR));
6194 944885 : goto return_build_unary_op;
6195 : }
6196 :
6197 940416 : tree true_res;
6198 940416 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6199 : {
6200 364 : tree larg = stabilize_reference (arg);
6201 364 : tree sarg = save_expr (larg);
6202 364 : switch (code)
6203 : {
6204 91 : case PREINCREMENT_EXPR:
6205 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6206 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6207 91 : break;
6208 91 : case POSTINCREMENT_EXPR:
6209 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6210 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6211 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6212 91 : break;
6213 91 : case PREDECREMENT_EXPR:
6214 91 : {
6215 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6216 : true, true);
6217 91 : rarg = invert_truthvalue_loc (location, rarg);
6218 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6219 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6220 : }
6221 91 : break;
6222 91 : case POSTDECREMENT_EXPR:
6223 91 : {
6224 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6225 : true, true);
6226 91 : rarg = invert_truthvalue_loc (location, rarg);
6227 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6228 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6229 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6230 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6231 : }
6232 91 : break;
6233 : default:
6234 : gcc_unreachable ();
6235 : }
6236 364 : TREE_SIDE_EFFECTS (val) = 1;
6237 : }
6238 940052 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6239 64 : val = boolean_increment (code, arg);
6240 : else
6241 939988 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6242 940416 : TREE_SIDE_EFFECTS (val) = 1;
6243 940416 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6244 3939 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6245 : TYPE_UNQUALIFIED);
6246 940416 : ret = val;
6247 940416 : goto return_build_unary_op;
6248 : }
6249 :
6250 52046890 : case ADDR_EXPR:
6251 : /* Note that this operation never does default_conversion. */
6252 :
6253 : /* The operand of unary '&' must be an lvalue (which excludes
6254 : expressions of type void), or, in C99, the result of a [] or
6255 : unary '*' operator. */
6256 52046890 : if (VOID_TYPE_P (TREE_TYPE (arg))
6257 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6258 52046909 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6259 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6260 :
6261 : /* Let &* cancel out to simplify resulting code. */
6262 52046890 : if (INDIRECT_REF_P (arg))
6263 : {
6264 : /* Don't let this be an lvalue. */
6265 22402 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6266 16858 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6267 5544 : ret = TREE_OPERAND (arg, 0);
6268 5544 : goto return_build_unary_op;
6269 : }
6270 :
6271 : /* Anything not already handled and not a true memory reference
6272 : or a non-lvalue array is an error. */
6273 52024488 : if (typecode != FUNCTION_TYPE && !noconvert
6274 52024488 : && !lvalue_or_else (location, arg, lv_addressof))
6275 17 : return error_mark_node;
6276 :
6277 : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6278 : folding later. */
6279 52024471 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6280 : {
6281 14 : tree inner = build_unary_op (location, code,
6282 14 : C_MAYBE_CONST_EXPR_EXPR (arg),
6283 : noconvert);
6284 28 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6285 14 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6286 14 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6287 14 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6288 14 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6289 14 : goto return_build_unary_op;
6290 : }
6291 :
6292 : /* Ordinary case; arg is a COMPONENT_REF or a decl. */
6293 :
6294 52024457 : argtype = TREE_TYPE (arg);
6295 :
6296 : /* If the lvalue is const or volatile, merge that into the type
6297 : to which the address will point. This is only needed
6298 : for function types. */
6299 52024457 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6300 51531591 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6301 82142905 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6302 : {
6303 30038276 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
6304 30038276 : int quals = orig_quals;
6305 :
6306 30038276 : if (TREE_READONLY (arg))
6307 29657116 : quals |= TYPE_QUAL_CONST;
6308 30038276 : if (TREE_THIS_VOLATILE (arg))
6309 382074 : quals |= TYPE_QUAL_VOLATILE;
6310 :
6311 30038276 : argtype = c_build_qualified_type (argtype, quals);
6312 : }
6313 :
6314 52024457 : switch (TREE_CODE (arg))
6315 : {
6316 65821 : case COMPONENT_REF:
6317 65821 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6318 : {
6319 8 : error_at (location, "cannot take address of bit-field %qD",
6320 8 : TREE_OPERAND (arg, 1));
6321 8 : return error_mark_node;
6322 : }
6323 :
6324 : /* fall through */
6325 :
6326 141616 : case ARRAY_REF:
6327 141616 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6328 : {
6329 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6330 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6331 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6332 : {
6333 6 : error_at (location, "cannot take address of scalar with "
6334 : "reverse storage order");
6335 6 : return error_mark_node;
6336 : }
6337 :
6338 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6339 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6340 58 : warning_at (location, OPT_Wscalar_storage_order,
6341 : "address of array with reverse scalar storage "
6342 : "order requested");
6343 : }
6344 :
6345 52024443 : default:
6346 52024443 : break;
6347 : }
6348 :
6349 52024443 : if (!c_mark_addressable (arg))
6350 20 : return error_mark_node;
6351 :
6352 52024423 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6353 : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6354 :
6355 52024423 : argtype = c_build_pointer_type (argtype);
6356 :
6357 : /* ??? Cope with user tricks that amount to offsetof. Delete this
6358 : when we have proper support for integer constant expressions. */
6359 52024423 : val = get_base_address (arg);
6360 52024423 : if (val && INDIRECT_REF_P (val)
6361 52048674 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6362 : {
6363 540 : ret = fold_offsetof (arg, argtype);
6364 540 : goto return_build_unary_op;
6365 : }
6366 :
6367 52023883 : val = build1 (ADDR_EXPR, argtype, arg);
6368 :
6369 52023883 : ret = val;
6370 52023883 : goto return_build_unary_op;
6371 :
6372 10 : case PAREN_EXPR:
6373 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6374 10 : goto return_build_unary_op;
6375 :
6376 0 : default:
6377 0 : gcc_unreachable ();
6378 : }
6379 :
6380 7028980 : if (argtype == NULL_TREE)
6381 7028980 : argtype = TREE_TYPE (arg);
6382 7028980 : if (TREE_CODE (arg) == INTEGER_CST)
6383 6323968 : ret = (require_constant_value
6384 6323968 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6385 6305377 : : fold_build1_loc (location, code, argtype, arg));
6386 : else
6387 705012 : ret = build1 (code, argtype, arg);
6388 60585526 : return_build_unary_op:
6389 60585526 : gcc_assert (ret != error_mark_node);
6390 6468652 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6391 67054169 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6392 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6393 60584964 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6394 74 : ret = note_integer_operands (ret);
6395 60585526 : if (eptype)
6396 245 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6397 60585526 : protected_set_expr_location (ret, location);
6398 60585526 : return ret;
6399 : }
6400 :
6401 : /* Return nonzero if REF is an lvalue valid for this language.
6402 : Lvalues can be assigned, unless their type has TYPE_READONLY.
6403 : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6404 :
6405 : bool
6406 178458117 : lvalue_p (const_tree ref)
6407 : {
6408 179965853 : const enum tree_code code = TREE_CODE (ref);
6409 :
6410 179965853 : switch (code)
6411 : {
6412 1482903 : case REALPART_EXPR:
6413 1482903 : case IMAGPART_EXPR:
6414 1482903 : case COMPONENT_REF:
6415 1482903 : return lvalue_p (TREE_OPERAND (ref, 0));
6416 :
6417 24833 : case C_MAYBE_CONST_EXPR:
6418 24833 : return lvalue_p (TREE_OPERAND (ref, 1));
6419 :
6420 : case COMPOUND_LITERAL_EXPR:
6421 : case STRING_CST:
6422 : return true;
6423 :
6424 25555127 : case MEM_REF:
6425 25555127 : case TARGET_MEM_REF:
6426 : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6427 : here as well. */
6428 25555127 : case INDIRECT_REF:
6429 25555127 : case ARRAY_REF:
6430 25555127 : case VAR_DECL:
6431 25555127 : case PARM_DECL:
6432 25555127 : case RESULT_DECL:
6433 25555127 : case ERROR_MARK:
6434 25555127 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6435 25555127 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6436 :
6437 6 : case BIND_EXPR:
6438 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6439 :
6440 7082961 : case CALL_EXPR:
6441 7082961 : return is_access_with_size_p (ref);
6442 :
6443 : default:
6444 : return false;
6445 : }
6446 : }
6447 :
6448 : /* Give a warning for storing in something that is read-only in GCC
6449 : terms but not const in ISO C terms. */
6450 :
6451 : static void
6452 5 : readonly_warning (tree arg, enum lvalue_use use)
6453 : {
6454 5 : switch (use)
6455 : {
6456 1 : case lv_assign:
6457 1 : warning (0, "assignment of read-only location %qE", arg);
6458 1 : break;
6459 2 : case lv_increment:
6460 2 : warning (0, "increment of read-only location %qE", arg);
6461 2 : break;
6462 2 : case lv_decrement:
6463 2 : warning (0, "decrement of read-only location %qE", arg);
6464 2 : break;
6465 0 : default:
6466 0 : gcc_unreachable ();
6467 : }
6468 5 : return;
6469 : }
6470 :
6471 :
6472 : /* Return nonzero if REF is an lvalue valid for this language;
6473 : otherwise, print an error message and return zero. USE says
6474 : how the lvalue is being used and so selects the error message.
6475 : LOCATION is the location at which any error should be reported. */
6476 :
6477 : static int
6478 4750244 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6479 : {
6480 4750244 : int win = lvalue_p (ref);
6481 :
6482 4750244 : if (!win)
6483 75 : lvalue_error (loc, use);
6484 :
6485 4750244 : return win;
6486 : }
6487 :
6488 : /* Mark EXP saying that we need to be able to take the
6489 : address of it; it should not be allocated in a register.
6490 : Returns true if successful. ARRAY_REF_P is true if this
6491 : is for ARRAY_REF construction - in that case we don't want
6492 : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6493 : it is fine to use ARRAY_REFs for vector subscripts on vector
6494 : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6495 : than producing an error for taking the address of a register. */
6496 :
6497 : bool
6498 52575305 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6499 : {
6500 52575305 : tree x = exp;
6501 :
6502 53157936 : while (1)
6503 53157936 : switch (TREE_CODE (x))
6504 : {
6505 10037 : case VIEW_CONVERT_EXPR:
6506 10037 : if (array_ref_p
6507 9927 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6508 19964 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6509 : return true;
6510 110 : x = TREE_OPERAND (x, 0);
6511 110 : break;
6512 :
6513 396764 : case COMPONENT_REF:
6514 396764 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6515 : {
6516 6 : error ("cannot take address of bit-field %qD",
6517 3 : TREE_OPERAND (x, 1));
6518 3 : return false;
6519 : }
6520 : /* FALLTHRU */
6521 582521 : case ADDR_EXPR:
6522 582521 : case ARRAY_REF:
6523 582521 : case REALPART_EXPR:
6524 582521 : case IMAGPART_EXPR:
6525 582521 : x = TREE_OPERAND (x, 0);
6526 582521 : break;
6527 :
6528 587 : case COMPOUND_LITERAL_EXPR:
6529 587 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6530 : {
6531 16 : if (override_register)
6532 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6533 : else
6534 : {
6535 4 : error ("address of register compound literal requested");
6536 4 : return false;
6537 : }
6538 : }
6539 583 : TREE_ADDRESSABLE (x) = 1;
6540 583 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6541 583 : return true;
6542 :
6543 0 : case CONSTRUCTOR:
6544 0 : TREE_ADDRESSABLE (x) = 1;
6545 0 : return true;
6546 :
6547 1334387 : case VAR_DECL:
6548 1334387 : case CONST_DECL:
6549 1334387 : case PARM_DECL:
6550 1334387 : case RESULT_DECL:
6551 1334387 : if (C_DECL_REGISTER (x)
6552 1334387 : && DECL_NONLOCAL (x))
6553 : {
6554 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6555 : {
6556 0 : error
6557 0 : ("global register variable %qD used in nested function", x);
6558 0 : return false;
6559 : }
6560 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6561 : }
6562 1334387 : else if (C_DECL_REGISTER (x))
6563 : {
6564 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6565 4 : error ("address of global register variable %qD requested", x);
6566 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6567 : {
6568 54 : DECL_REGISTER (x) = 0;
6569 54 : TREE_ADDRESSABLE (x) = 1;
6570 54 : mark_decl_used (x, true);
6571 54 : return true;
6572 : }
6573 : else
6574 15 : error ("address of register variable %qD requested", x);
6575 19 : return false;
6576 : }
6577 :
6578 : /* FALLTHRU */
6579 51996416 : case FUNCTION_DECL:
6580 51996416 : TREE_ADDRESSABLE (x) = 1;
6581 51996416 : mark_decl_used (x, true);
6582 : /* FALLTHRU */
6583 : default:
6584 : return true;
6585 : }
6586 : }
6587 :
6588 : /* Convert EXPR to TYPE, warning about conversion problems with
6589 : constants. SEMANTIC_TYPE is the type this conversion would use
6590 : without excess precision. If SEMANTIC_TYPE is NULL, this function
6591 : is equivalent to convert_and_check. This function is a wrapper that
6592 : handles conversions that may be different than
6593 : the usual ones because of excess precision. */
6594 :
6595 : static tree
6596 23705726 : ep_convert_and_check (location_t loc, tree type, tree expr,
6597 : tree semantic_type)
6598 : {
6599 23705726 : if (TREE_TYPE (expr) == type)
6600 : return expr;
6601 :
6602 : /* For C11, integer conversions may have results with excess
6603 : precision. */
6604 2064578 : if (flag_isoc11 || !semantic_type)
6605 2064574 : return convert_and_check (loc, type, expr);
6606 :
6607 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6608 4 : && TREE_TYPE (expr) != semantic_type)
6609 : {
6610 : /* For integers, we need to check the real conversion, not
6611 : the conversion to the excess precision type. */
6612 4 : expr = convert_and_check (loc, semantic_type, expr);
6613 : }
6614 : /* Result type is the excess precision type, which should be
6615 : large enough, so do not check. */
6616 4 : return convert (type, expr);
6617 : }
6618 :
6619 : /* If EXPR refers to a built-in declared without a prototype returns
6620 : the actual type of the built-in and, if non-null, set *BLTIN to
6621 : a pointer to the built-in. Otherwise return the type of EXPR
6622 : and clear *BLTIN if non-null. */
6623 :
6624 : static tree
6625 2829083 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6626 : {
6627 2829083 : tree dummy;
6628 2829083 : if (!bltin)
6629 0 : bltin = &dummy;
6630 :
6631 2829083 : *bltin = NULL_TREE;
6632 :
6633 2829083 : tree type = TREE_TYPE (expr);
6634 2829083 : if (TREE_CODE (expr) != ADDR_EXPR)
6635 : return type;
6636 :
6637 209545 : tree oper = TREE_OPERAND (expr, 0);
6638 209545 : if (!DECL_P (oper)
6639 170877 : || TREE_CODE (oper) != FUNCTION_DECL
6640 245980 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6641 : return type;
6642 :
6643 2086 : built_in_function code = DECL_FUNCTION_CODE (oper);
6644 2086 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6645 : return type;
6646 :
6647 1977 : if ((*bltin = builtin_decl_implicit (code)))
6648 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6649 :
6650 : return type;
6651 : }
6652 :
6653 : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6654 : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6655 : if folded to an integer constant then the unselected half may
6656 : contain arbitrary operations not normally permitted in constant
6657 : expressions. Set the location of the expression to LOC. */
6658 :
6659 : tree
6660 404485 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6661 : tree op1, tree op1_original_type, location_t op1_loc,
6662 : tree op2, tree op2_original_type, location_t op2_loc)
6663 : {
6664 404485 : tree type1;
6665 404485 : tree type2;
6666 404485 : tree result_type = NULL;
6667 404485 : tree semantic_result_type = NULL;
6668 404485 : tree orig_op1 = op1, orig_op2 = op2;
6669 404485 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6670 404485 : bool ifexp_int_operands;
6671 404485 : tree ret;
6672 :
6673 404485 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6674 110575 : if (op1_int_operands)
6675 110575 : op1 = remove_c_maybe_const_expr (op1);
6676 404485 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6677 145753 : if (op2_int_operands)
6678 145753 : op2 = remove_c_maybe_const_expr (op2);
6679 404485 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6680 242736 : if (ifexp_int_operands)
6681 242736 : ifexp = remove_c_maybe_const_expr (ifexp);
6682 :
6683 : /* Promote both alternatives. */
6684 :
6685 404485 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6686 400563 : op1 = default_conversion (op1);
6687 404485 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6688 374640 : op2 = default_conversion (op2);
6689 :
6690 404485 : if (TREE_CODE (ifexp) == ERROR_MARK
6691 404416 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6692 808887 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6693 83 : return error_mark_node;
6694 :
6695 404402 : tree bltin1 = NULL_TREE;
6696 404402 : tree bltin2 = NULL_TREE;
6697 404402 : type1 = type_or_builtin_type (op1, &bltin1);
6698 404402 : const enum tree_code code1 = TREE_CODE (type1);
6699 404402 : type2 = type_or_builtin_type (op2, &bltin2);
6700 404402 : const enum tree_code code2 = TREE_CODE (type2);
6701 :
6702 404402 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6703 1 : return error_mark_node;
6704 :
6705 404401 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6706 1 : return error_mark_node;
6707 :
6708 : /* C90 does not permit non-lvalue arrays in conditional expressions.
6709 : In C99 they will be pointers by now. */
6710 404400 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6711 : {
6712 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6713 1 : return error_mark_node;
6714 : }
6715 :
6716 404399 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6717 404393 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6718 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6719 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6720 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6721 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6722 : {
6723 7 : semantic_result_type = c_common_type (type1, type2);
6724 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6725 : {
6726 6 : op1 = TREE_OPERAND (op1, 0);
6727 6 : type1 = TREE_TYPE (op1);
6728 6 : gcc_assert (TREE_CODE (type1) == code1);
6729 : }
6730 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6731 : {
6732 1 : op2 = TREE_OPERAND (op2, 0);
6733 1 : type2 = TREE_TYPE (op2);
6734 1 : gcc_assert (TREE_CODE (type2) == code2);
6735 : }
6736 : }
6737 :
6738 404399 : if (warn_cxx_compat)
6739 : {
6740 420 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6741 420 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6742 :
6743 420 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6744 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6745 426 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6746 2 : warning_at (colon_loc, OPT_Wc___compat,
6747 : "different enum types in conditional is "
6748 : "invalid in C++: %qT vs %qT", t1, t2);
6749 : }
6750 :
6751 404399 : if (warn_zero_as_null_pointer_constant
6752 20 : && c_inhibit_evaluation_warnings == 0)
6753 : {
6754 20 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6755 20 : && INTEGRAL_TYPE_P (type2) && null_pointer_constant_p (orig_op2))
6756 5 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6757 : "zero as null pointer constant");
6758 :
6759 20 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6760 20 : && INTEGRAL_TYPE_P (type1) && null_pointer_constant_p (orig_op1))
6761 5 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6762 : "zero as null pointer constant");
6763 : }
6764 :
6765 : /* Quickly detect the usual case where op1 and op2 have the same type
6766 : after promotion. */
6767 404399 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6768 : {
6769 269251 : if (type1 == type2)
6770 : result_type = type1;
6771 : else
6772 6412 : result_type = TYPE_MAIN_VARIANT (type1);
6773 : }
6774 135148 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6775 80466 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6776 54730 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6777 26152 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6778 : {
6779 : /* In C11, a conditional expression between a floating-point
6780 : type and an integer type should convert the integer type to
6781 : the evaluation format of the floating-point type, with
6782 : possible excess precision. */
6783 28678 : tree eptype1 = type1;
6784 28678 : tree eptype2 = type2;
6785 28678 : if (flag_isoc11)
6786 : {
6787 28365 : tree eptype;
6788 11643 : if (ANY_INTEGRAL_TYPE_P (type1)
6789 28365 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6790 : {
6791 2 : eptype2 = eptype;
6792 2 : if (!semantic_result_type)
6793 2 : semantic_result_type = c_common_type (type1, type2);
6794 : }
6795 502 : else if (ANY_INTEGRAL_TYPE_P (type2)
6796 28363 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6797 : {
6798 4626 : eptype1 = eptype;
6799 4626 : if (!semantic_result_type)
6800 4626 : semantic_result_type = c_common_type (type1, type2);
6801 : }
6802 : }
6803 28678 : result_type = c_common_type (eptype1, eptype2);
6804 28678 : if (result_type == error_mark_node)
6805 : return error_mark_node;
6806 28676 : do_warn_double_promotion (result_type, type1, type2,
6807 : "implicit conversion from %qT to %qT to "
6808 : "match other result of conditional",
6809 : colon_loc);
6810 :
6811 : /* If -Wsign-compare, warn here if type1 and type2 have
6812 : different signedness. We'll promote the signed to unsigned
6813 : and later code won't know it used to be different.
6814 : Do this check on the original types, so that explicit casts
6815 : will be considered, but default promotions won't. */
6816 28676 : if (c_inhibit_evaluation_warnings == 0)
6817 : {
6818 28485 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6819 28485 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6820 :
6821 28485 : if (unsigned_op1 ^ unsigned_op2)
6822 : {
6823 : /* Do not warn if the result type is signed, since the
6824 : signed type will only be chosen if it can represent
6825 : all the values of the unsigned type. */
6826 13039 : if (!TYPE_UNSIGNED (result_type))
6827 : /* OK */;
6828 : else
6829 : {
6830 12935 : bool op1_maybe_const = true;
6831 12935 : bool op2_maybe_const = true;
6832 :
6833 : /* Do not warn if the signed quantity is an
6834 : unsuffixed integer literal (or some static
6835 : constant expression involving such literals) and
6836 : it is non-negative. This warning requires the
6837 : operands to be folded for best results, so do
6838 : that folding in this case even without
6839 : warn_sign_compare to avoid warning options
6840 : possibly affecting code generation. */
6841 12935 : c_inhibit_evaluation_warnings
6842 12935 : += (ifexp == truthvalue_false_node);
6843 12935 : op1 = c_fully_fold (op1, require_constant_value,
6844 : &op1_maybe_const);
6845 12935 : c_inhibit_evaluation_warnings
6846 12935 : -= (ifexp == truthvalue_false_node);
6847 :
6848 12935 : c_inhibit_evaluation_warnings
6849 12935 : += (ifexp == truthvalue_true_node);
6850 12935 : op2 = c_fully_fold (op2, require_constant_value,
6851 : &op2_maybe_const);
6852 12935 : c_inhibit_evaluation_warnings
6853 12935 : -= (ifexp == truthvalue_true_node);
6854 :
6855 12935 : if (warn_sign_compare)
6856 : {
6857 1245 : if ((unsigned_op2
6858 685 : && tree_expr_nonnegative_p (op1))
6859 1248 : || (unsigned_op1
6860 560 : && tree_expr_nonnegative_p (op2)))
6861 : /* OK */;
6862 10 : else if (unsigned_op2)
6863 3 : warning_at (op1_loc, OPT_Wsign_compare,
6864 : "operand of %<?:%> changes signedness from "
6865 : "%qT to %qT due to unsignedness of other "
6866 3 : "operand", TREE_TYPE (orig_op1),
6867 3 : TREE_TYPE (orig_op2));
6868 : else
6869 7 : warning_at (op2_loc, OPT_Wsign_compare,
6870 : "operand of %<?:%> changes signedness from "
6871 : "%qT to %qT due to unsignedness of other "
6872 7 : "operand", TREE_TYPE (orig_op2),
6873 7 : TREE_TYPE (orig_op1));
6874 : }
6875 12935 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6876 7577 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6877 12935 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6878 4396 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6879 : }
6880 : }
6881 : }
6882 : }
6883 106470 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6884 : {
6885 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6886 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6887 : "ISO C forbids conditional expr with only one void side");
6888 25995 : result_type = void_type_node;
6889 : }
6890 80475 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6891 : {
6892 80057 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6893 80057 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6894 80057 : addr_space_t as_common;
6895 :
6896 80057 : if (comp_target_types (colon_loc, type1, type2))
6897 : {
6898 2620 : ifexp = save_expr (ifexp);
6899 2620 : result_type = common_pointer_type (type1, type2, ifexp);
6900 : }
6901 77437 : else if (null_pointer_constant_p (orig_op1))
6902 : result_type = type2;
6903 73275 : else if (null_pointer_constant_p (orig_op2))
6904 : result_type = type1;
6905 35283 : else if (!addr_space_superset (as1, as2, &as_common))
6906 : {
6907 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6908 : "used in conditional expression");
6909 0 : return error_mark_node;
6910 : }
6911 35283 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6912 326 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6913 35285 : || (VOID_TYPE_P (TREE_TYPE (type2))
6914 34912 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6915 : {
6916 35235 : tree t1 = TREE_TYPE (type1);
6917 35235 : tree t2 = TREE_TYPE (type2);
6918 35235 : if (!(VOID_TYPE_P (t1)
6919 325 : && !TYPE_ATOMIC (t1)))
6920 : {
6921 : /* roles are swapped */
6922 34911 : t1 = t2;
6923 34911 : t2 = TREE_TYPE (type1);
6924 : }
6925 35235 : tree t2_stripped = strip_array_types (t2);
6926 35235 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6927 35235 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6928 : {
6929 18 : if (!flag_isoc23)
6930 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6931 : "pointer to array loses qualifier "
6932 : "in conditional expression");
6933 12 : else if (warn_c11_c23_compat > 0)
6934 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6935 : "pointer to array loses qualifier "
6936 : "in conditional expression in ISO C before C23");
6937 : }
6938 35235 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6939 4 : pedwarn (colon_loc, OPT_Wpedantic,
6940 : "ISO C forbids conditional expr between "
6941 : "%<void *%> and function pointer");
6942 : /* for array, use qualifiers of element type */
6943 35235 : if (flag_isoc23)
6944 11454 : t2 = t2_stripped;
6945 35235 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6946 : }
6947 : /* Objective-C pointer comparisons are a bit more lenient. */
6948 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6949 0 : result_type = objc_common_type (type1, type2);
6950 : else
6951 : {
6952 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6953 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6954 48 : if (!flag_isoc99)
6955 : /* This downgrade to a warning ensures that -std=gnu89
6956 : -pedantic-errors does not flag these mismatches between
6957 : builtins as errors (as diagnostics::kind::permerror would)
6958 : ISO C99 and later do not have implicit function declarations,
6959 : so the mismatch cannot occur naturally there. */
6960 5 : kind = (bltin1 && bltin2
6961 5 : ? diagnostics::kind::warning
6962 : : diagnostics::kind::pedwarn);
6963 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6964 : "pointer type mismatch "
6965 : "in conditional expression"))
6966 : {
6967 46 : inform (op1_loc, "first expression has type %qT", type1);
6968 46 : inform (op2_loc, "second expression has type %qT", type2);
6969 : }
6970 48 : result_type = c_build_pointer_type
6971 48 : (c_build_qualified_type (void_type_node, qual));
6972 : }
6973 : }
6974 418 : else if (code1 == POINTER_TYPE
6975 298 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6976 : {
6977 294 : if (!null_pointer_constant_p (orig_op2))
6978 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6979 : "pointer/integer type mismatch "
6980 : "in conditional expression");
6981 : else
6982 : {
6983 280 : op2 = null_pointer_node;
6984 : }
6985 : result_type = type1;
6986 : }
6987 124 : else if (code2 == POINTER_TYPE
6988 90 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6989 : {
6990 86 : if (!null_pointer_constant_p (orig_op1))
6991 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6992 : "pointer/integer type mismatch "
6993 : "in conditional expression");
6994 : else
6995 : {
6996 72 : op1 = null_pointer_node;
6997 : }
6998 : result_type = type2;
6999 : }
7000 : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
7001 : type nullptr_t and the other is a pointer, the result type is the pointer
7002 : type." */
7003 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
7004 : result_type = type2;
7005 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
7006 : result_type = type1;
7007 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
7008 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
7009 8 : TYPE_MAIN_VARIANT (type2)))
7010 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
7011 8 : TYPE_MAIN_VARIANT (type2));
7012 :
7013 404375 : if (!result_type)
7014 : {
7015 22 : if (flag_cond_mismatch)
7016 0 : result_type = void_type_node;
7017 : else
7018 : {
7019 22 : error_at (colon_loc, "type mismatch in conditional expression");
7020 22 : return error_mark_node;
7021 : }
7022 : }
7023 :
7024 : /* Merge const and volatile flags of the incoming types. */
7025 404375 : result_type
7026 404375 : = build_type_variant (result_type,
7027 : TYPE_READONLY (type1) || TYPE_READONLY (type2),
7028 : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
7029 :
7030 404375 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
7031 : semantic_result_type);
7032 404375 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
7033 : semantic_result_type);
7034 :
7035 404375 : if (ifexp_bcp && ifexp == truthvalue_true_node)
7036 : {
7037 10 : op2_int_operands = true;
7038 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
7039 : }
7040 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
7041 : {
7042 49 : op1_int_operands = true;
7043 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
7044 : }
7045 905854 : int_const = int_operands = (ifexp_int_operands
7046 404375 : && op1_int_operands
7047 404375 : && op2_int_operands);
7048 97104 : if (int_operands)
7049 : {
7050 97104 : int_const = ((ifexp == truthvalue_true_node
7051 58794 : && TREE_CODE (orig_op1) == INTEGER_CST
7052 58780 : && !TREE_OVERFLOW (orig_op1))
7053 97118 : || (ifexp == truthvalue_false_node
7054 38264 : && TREE_CODE (orig_op2) == INTEGER_CST
7055 38248 : && !TREE_OVERFLOW (orig_op2)));
7056 : }
7057 :
7058 : /* Need to convert condition operand into a vector mask. */
7059 404375 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
7060 : {
7061 0 : tree vectype = TREE_TYPE (ifexp);
7062 0 : tree elem_type = TREE_TYPE (vectype);
7063 0 : tree zero = build_int_cst (elem_type, 0);
7064 0 : tree zero_vec = build_vector_from_val (vectype, zero);
7065 0 : tree cmp_type = truth_type_for (vectype);
7066 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
7067 : }
7068 :
7069 404375 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
7070 97060 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
7071 : else
7072 : {
7073 307315 : if (int_operands)
7074 : {
7075 : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
7076 : nested inside of the expression. */
7077 76 : op1 = c_fully_fold (op1, false, NULL);
7078 76 : op2 = c_fully_fold (op2, false, NULL);
7079 : }
7080 307315 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
7081 307315 : if (int_operands)
7082 76 : ret = note_integer_operands (ret);
7083 : }
7084 404375 : if (semantic_result_type)
7085 4635 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
7086 :
7087 404375 : protected_set_expr_location (ret, colon_loc);
7088 :
7089 : /* If the OP1 and OP2 are the same and don't have side-effects,
7090 : warn here, because the COND_EXPR will be turned into OP1. */
7091 404375 : if (warn_duplicated_branches
7092 34 : && TREE_CODE (ret) == COND_EXPR
7093 404409 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
7094 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
7095 : "this condition has identical branches");
7096 :
7097 : return ret;
7098 : }
7099 :
7100 : /* EXPR is an expression, location LOC, whose result is discarded.
7101 : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
7102 : whose right-hand operand is such a call, possibly recursively). */
7103 :
7104 : static void
7105 6517039 : maybe_warn_nodiscard (location_t loc, tree expr)
7106 : {
7107 6517039 : if (VOID_TYPE_P (TREE_TYPE (expr)))
7108 : return;
7109 3993582 : while (TREE_CODE (expr) == COMPOUND_EXPR)
7110 : {
7111 51836 : expr = TREE_OPERAND (expr, 1);
7112 51836 : if (EXPR_HAS_LOCATION (expr))
7113 34735 : loc = EXPR_LOCATION (expr);
7114 : }
7115 3941746 : if (TREE_CODE (expr) != CALL_EXPR)
7116 : return;
7117 347369 : tree fn = CALL_EXPR_FN (expr);
7118 347369 : if (!fn)
7119 : return;
7120 347353 : tree attr;
7121 347353 : if (TREE_CODE (fn) == ADDR_EXPR
7122 346686 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
7123 694039 : && (attr = lookup_attribute ("nodiscard",
7124 346686 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
7125 : {
7126 3 : fn = TREE_OPERAND (fn, 0);
7127 3 : tree args = TREE_VALUE (attr);
7128 3 : if (args)
7129 1 : args = TREE_VALUE (args);
7130 3 : auto_diagnostic_group d;
7131 3 : auto_urlify_attributes sentinel;
7132 3 : int warned;
7133 3 : if (args)
7134 1 : warned = warning_at (loc, OPT_Wunused_result,
7135 : "ignoring return value of %qD, declared with "
7136 : "attribute %<nodiscard%>: %E", fn, args);
7137 : else
7138 2 : warned = warning_at (loc, OPT_Wunused_result,
7139 : "ignoring return value of %qD, declared with "
7140 : "attribute %<nodiscard%>", fn);
7141 3 : if (warned)
7142 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
7143 3 : }
7144 : else
7145 : {
7146 347350 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
7147 347350 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
7148 347350 : if (!attr)
7149 347338 : return;
7150 12 : tree args = TREE_VALUE (attr);
7151 12 : if (args)
7152 5 : args = TREE_VALUE (args);
7153 12 : auto_diagnostic_group d;
7154 12 : auto_urlify_attributes sentinel;
7155 12 : int warned;
7156 12 : if (args)
7157 5 : warned = warning_at (loc, OPT_Wunused_result,
7158 : "ignoring return value of type %qT, declared "
7159 : "with attribute %<nodiscard%>: %E",
7160 : rettype, args);
7161 : else
7162 7 : warned = warning_at (loc, OPT_Wunused_result,
7163 : "ignoring return value of type %qT, declared "
7164 : "with attribute %<nodiscard%>", rettype);
7165 12 : if (warned)
7166 : {
7167 12 : if (TREE_CODE (fn) == ADDR_EXPR)
7168 : {
7169 11 : fn = TREE_OPERAND (fn, 0);
7170 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
7171 11 : inform (DECL_SOURCE_LOCATION (fn),
7172 : "in call to %qD, declared here", fn);
7173 : }
7174 : }
7175 12 : }
7176 : }
7177 :
7178 : /* Return a compound expression that performs two expressions and
7179 : returns the value of the second of them.
7180 :
7181 : LOC is the location of the COMPOUND_EXPR. */
7182 :
7183 : tree
7184 114208 : build_compound_expr (location_t loc, tree expr1, tree expr2)
7185 : {
7186 114208 : bool expr1_int_operands, expr2_int_operands;
7187 114208 : tree eptype = NULL_TREE;
7188 114208 : tree ret;
7189 :
7190 114208 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
7191 326 : if (expr1_int_operands)
7192 326 : expr1 = remove_c_maybe_const_expr (expr1);
7193 114208 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7194 66132 : if (expr2_int_operands)
7195 66132 : expr2 = remove_c_maybe_const_expr (expr2);
7196 :
7197 114208 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7198 0 : expr1 = TREE_OPERAND (expr1, 0);
7199 114208 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7200 : {
7201 1 : eptype = TREE_TYPE (expr2);
7202 1 : expr2 = TREE_OPERAND (expr2, 0);
7203 : }
7204 :
7205 114208 : if (!TREE_SIDE_EFFECTS (expr1))
7206 : {
7207 : /* The left-hand operand of a comma expression is like an expression
7208 : statement: with -Wunused, we should warn if it doesn't have
7209 : any side-effects, unless it was explicitly cast to (void). */
7210 8433 : if (warn_unused_value)
7211 : {
7212 1284 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7213 1284 : && CONVERT_EXPR_P (expr1))
7214 : ; /* (void) a, b */
7215 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7216 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7217 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7218 : ; /* (void) a, (void) b, c */
7219 : else
7220 11 : warning_at (loc, OPT_Wunused_value,
7221 : "left-hand operand of comma expression has no effect");
7222 : }
7223 : }
7224 105775 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7225 13111 : && warn_unused_value)
7226 : {
7227 : tree r = expr1;
7228 : location_t cloc = loc;
7229 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7230 : {
7231 2417 : if (EXPR_HAS_LOCATION (r))
7232 2417 : cloc = EXPR_LOCATION (r);
7233 2417 : r = TREE_OPERAND (r, 1);
7234 : }
7235 2416 : if (!TREE_SIDE_EFFECTS (r)
7236 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7237 2420 : && !CONVERT_EXPR_P (r))
7238 4 : warning_at (cloc, OPT_Wunused_value,
7239 : "right-hand operand of comma expression has no effect");
7240 : }
7241 :
7242 : /* With -Wunused, we should also warn if the left-hand operand does have
7243 : side-effects, but computes a value which is not used. For example, in
7244 : `foo() + bar(), baz()' the result of the `+' operator is not used,
7245 : so we should issue a warning. */
7246 103359 : else if (warn_unused_value)
7247 35419 : warn_if_unused_value (expr1, loc);
7248 :
7249 114208 : maybe_warn_nodiscard (loc, expr1);
7250 :
7251 114208 : if (expr2 == error_mark_node)
7252 : return error_mark_node;
7253 :
7254 114195 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7255 :
7256 114195 : if (flag_isoc99
7257 : && expr1_int_operands
7258 113739 : && expr2_int_operands)
7259 153 : ret = note_integer_operands (ret);
7260 :
7261 114195 : if (eptype)
7262 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7263 :
7264 114195 : protected_set_expr_location (ret, loc);
7265 114195 : return ret;
7266 : }
7267 :
7268 : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7269 : which we are casting. OTYPE is the type of the expression being
7270 : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7271 : of the cast. -Wcast-qual appeared on the command line. Named
7272 : address space qualifiers are not handled here, because they result
7273 : in different warnings. */
7274 :
7275 : static void
7276 14056 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7277 : {
7278 14056 : tree in_type = type;
7279 14056 : tree in_otype = otype;
7280 14056 : int added = 0;
7281 14056 : int discarded = 0;
7282 14226 : bool is_const;
7283 :
7284 : /* Check that the qualifiers on IN_TYPE are a superset of the
7285 : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7286 : nodes is uninteresting and we stop as soon as we hit a
7287 : non-POINTER_TYPE node on either type. */
7288 14226 : do
7289 : {
7290 14226 : in_otype = TREE_TYPE (in_otype);
7291 14226 : in_type = TREE_TYPE (in_type);
7292 :
7293 : /* GNU C allows cv-qualified function types. 'const' means the
7294 : function is very pure, 'volatile' means it can't return. We
7295 : need to warn when such qualifiers are added, not when they're
7296 : taken away. */
7297 14226 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7298 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7299 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7300 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7301 : else
7302 14208 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7303 14208 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7304 : }
7305 14226 : while (TREE_CODE (in_type) == POINTER_TYPE
7306 14226 : && TREE_CODE (in_otype) == POINTER_TYPE);
7307 :
7308 14056 : if (added)
7309 2 : warning_at (loc, OPT_Wcast_qual,
7310 : "cast adds %q#v qualifier to function type", added);
7311 :
7312 14056 : if (discarded)
7313 : /* There are qualifiers present in IN_OTYPE that are not present
7314 : in IN_TYPE. */
7315 1203 : warning_at (loc, OPT_Wcast_qual,
7316 : "cast discards %qv qualifier from pointer target type",
7317 : discarded);
7318 :
7319 14056 : if (added || discarded)
7320 : return;
7321 :
7322 : /* A cast from **T to const **T is unsafe, because it can cause a
7323 : const value to be changed with no additional warning. We only
7324 : issue this warning if T is the same on both sides, and we only
7325 : issue the warning if there are the same number of pointers on
7326 : both sides, as otherwise the cast is clearly unsafe anyhow. A
7327 : cast is unsafe when a qualifier is added at one level and const
7328 : is not present at all outer levels.
7329 :
7330 : To issue this warning, we check at each level whether the cast
7331 : adds new qualifiers not already seen. We don't need to special
7332 : case function types, as they won't have the same
7333 : TYPE_MAIN_VARIANT. */
7334 :
7335 12851 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7336 : return;
7337 227 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7338 : return;
7339 :
7340 48 : in_type = type;
7341 48 : in_otype = otype;
7342 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7343 130 : do
7344 : {
7345 130 : in_type = TREE_TYPE (in_type);
7346 130 : in_otype = TREE_TYPE (in_otype);
7347 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7348 130 : && !is_const)
7349 : {
7350 27 : warning_at (loc, OPT_Wcast_qual,
7351 : "to be safe all intermediate pointers in cast from "
7352 : "%qT to %qT must be %<const%> qualified",
7353 : otype, type);
7354 27 : break;
7355 : }
7356 103 : if (is_const)
7357 72 : is_const = TYPE_READONLY (in_type);
7358 : }
7359 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7360 : }
7361 :
7362 : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7363 :
7364 : static bool
7365 1312 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7366 : {
7367 1312 : if (error_operand_p (t1) || error_operand_p (t2))
7368 : return true;
7369 :
7370 1311 : t1 = TYPE_MAIN_VARIANT (t1);
7371 1311 : t2 = TYPE_MAIN_VARIANT (t2);
7372 :
7373 1311 : if (TREE_CODE (t1) == POINTER_TYPE
7374 229 : && TREE_CODE (t2) == POINTER_TYPE)
7375 : return true;
7376 :
7377 : /* Only the precision of the parameter matters. This check should
7378 : make sure that the callee does not see undefined values in argument
7379 : registers. */
7380 1103 : if (INTEGRAL_TYPE_P (t1)
7381 913 : && INTEGRAL_TYPE_P (t2)
7382 1448 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7383 : return true;
7384 :
7385 938 : return comptypes (t1, t2);
7386 : }
7387 :
7388 : /* Check if a type cast between two function types can be considered safe. */
7389 :
7390 : static bool
7391 7559 : c_safe_function_type_cast_p (tree t1, tree t2)
7392 : {
7393 7559 : if (TREE_TYPE (t1) == void_type_node &&
7394 5561 : TYPE_ARG_TYPES (t1) == void_list_node)
7395 : return true;
7396 :
7397 3722 : if (TREE_TYPE (t2) == void_type_node &&
7398 2889 : TYPE_ARG_TYPES (t2) == void_list_node)
7399 : return true;
7400 :
7401 892 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7402 : return false;
7403 :
7404 197 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7405 513 : t1 && t2;
7406 316 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7407 420 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7408 : return false;
7409 :
7410 : return true;
7411 : }
7412 :
7413 : /* Build an expression representing a cast to type TYPE of expression EXPR.
7414 : LOC is the location of the cast-- typically the open paren of the cast. */
7415 :
7416 : tree
7417 120334366 : build_c_cast (location_t loc, tree type, tree expr)
7418 : {
7419 120334366 : tree value;
7420 :
7421 120334366 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7422 :
7423 120334366 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7424 66 : expr = TREE_OPERAND (expr, 0);
7425 :
7426 120334366 : value = expr;
7427 120334366 : if (int_operands)
7428 6877899 : value = remove_c_maybe_const_expr (value);
7429 :
7430 120334366 : if (type == error_mark_node || expr == error_mark_node)
7431 : return error_mark_node;
7432 :
7433 : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7434 : only in <protocol> qualifications. But when constructing cast expressions,
7435 : the protocols do matter and must be kept around. */
7436 120333346 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7437 0 : return build1 (NOP_EXPR, type, expr);
7438 :
7439 120333346 : type = TYPE_MAIN_VARIANT (type);
7440 :
7441 120333346 : if (TREE_CODE (type) == ARRAY_TYPE)
7442 : {
7443 13 : error_at (loc, "cast specifies array type");
7444 13 : return error_mark_node;
7445 : }
7446 :
7447 120333333 : if (TREE_CODE (type) == FUNCTION_TYPE)
7448 : {
7449 6 : error_at (loc, "cast specifies function type");
7450 6 : return error_mark_node;
7451 : }
7452 :
7453 120333327 : if (!VOID_TYPE_P (type))
7454 : {
7455 120288765 : value = require_complete_type (loc, value);
7456 120288765 : if (value == error_mark_node)
7457 : return error_mark_node;
7458 : }
7459 :
7460 120333326 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7461 : {
7462 18132350 : if (RECORD_OR_UNION_TYPE_P (type)
7463 18132350 : && pedwarn (loc, OPT_Wpedantic,
7464 : "ISO C forbids casting nonscalar to the same type"))
7465 : ;
7466 18132346 : else if (warn_useless_cast && c_inhibit_evaluation_warnings == 0)
7467 8 : warning_at (loc, OPT_Wuseless_cast,
7468 : "useless cast to type %qT", type);
7469 :
7470 : /* Convert to remove any qualifiers from VALUE's type. */
7471 18132350 : value = convert (type, value);
7472 : }
7473 102200976 : else if (TREE_CODE (type) == UNION_TYPE)
7474 : {
7475 137 : tree field;
7476 :
7477 192 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7478 185 : if (TREE_TYPE (field) != error_mark_node
7479 369 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7480 184 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7481 : break;
7482 :
7483 137 : if (field)
7484 : {
7485 130 : tree t;
7486 130 : bool maybe_const = true;
7487 :
7488 130 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7489 130 : t = c_fully_fold (value, false, &maybe_const);
7490 130 : t = build_constructor_single (type, field, t);
7491 130 : if (!maybe_const)
7492 15 : t = c_wrap_maybe_const (t, true);
7493 130 : t = digest_init (loc, field, type, t,
7494 : NULL_TREE, false, false, false, true, false, false);
7495 130 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7496 130 : return t;
7497 : }
7498 7 : error_at (loc, "cast to union type from type not present in union");
7499 7 : return error_mark_node;
7500 : }
7501 : else
7502 : {
7503 102200839 : tree otype, ovalue;
7504 :
7505 102200839 : if (type == void_type_node)
7506 : {
7507 18592 : tree t = build1 (CONVERT_EXPR, type, value);
7508 18592 : SET_EXPR_LOCATION (t, loc);
7509 18592 : return t;
7510 : }
7511 :
7512 102182247 : otype = TREE_TYPE (value);
7513 :
7514 : /* Optionally warn about potentially worrisome casts. */
7515 102182247 : if (warn_cast_qual
7516 201314 : && TREE_CODE (type) == POINTER_TYPE
7517 27388 : && TREE_CODE (otype) == POINTER_TYPE)
7518 14056 : handle_warn_cast_qual (loc, type, otype);
7519 :
7520 : /* Warn about conversions between pointers to disjoint
7521 : address spaces. */
7522 102182247 : if (TREE_CODE (type) == POINTER_TYPE
7523 3341870 : && TREE_CODE (otype) == POINTER_TYPE
7524 105177067 : && !null_pointer_constant_p (value))
7525 : {
7526 2940875 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7527 2940875 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7528 2940875 : addr_space_t as_common;
7529 :
7530 2940875 : if (!addr_space_superset (as_to, as_from, &as_common))
7531 : {
7532 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7533 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7534 : "from disjoint generic address space pointer",
7535 : c_addr_space_name (as_to));
7536 :
7537 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7538 0 : warning_at (loc, 0, "cast to generic address space pointer "
7539 : "from disjoint %qs address space pointer",
7540 : c_addr_space_name (as_from));
7541 :
7542 : else
7543 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7544 : "from disjoint %qs address space pointer",
7545 : c_addr_space_name (as_to),
7546 : c_addr_space_name (as_from));
7547 : }
7548 :
7549 : /* Warn of new allocations that are not big enough for the target
7550 : type. */
7551 2940875 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7552 1069 : if (tree fndecl = get_callee_fndecl (value))
7553 1065 : if (DECL_IS_MALLOC (fndecl))
7554 : {
7555 573 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7556 573 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7557 573 : if (alloc_size)
7558 215 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7559 : alloc_size);
7560 : }
7561 : }
7562 :
7563 : /* Warn about possible alignment problems. */
7564 102182247 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7565 7 : && TREE_CODE (type) == POINTER_TYPE
7566 7 : && TREE_CODE (otype) == POINTER_TYPE
7567 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7568 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7569 : /* Don't warn about opaque types, where the actual alignment
7570 : restriction is unknown. */
7571 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7572 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7573 102182261 : && min_align_of_type (TREE_TYPE (type))
7574 7 : > min_align_of_type (TREE_TYPE (otype)))
7575 5 : warning_at (loc, OPT_Wcast_align,
7576 : "cast increases required alignment of target type");
7577 :
7578 102182247 : if ((TREE_CODE (type) == INTEGER_TYPE
7579 102182247 : || TREE_CODE (type) == BITINT_TYPE)
7580 7093087 : && TREE_CODE (otype) == POINTER_TYPE
7581 102207589 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7582 : /* Unlike conversion of integers to pointers, where the
7583 : warning is disabled for converting constants because
7584 : of cases such as SIG_*, warn about converting constant
7585 : pointers to integers. In some cases it may cause unwanted
7586 : sign extension, and a warning is appropriate. */
7587 1464 : warning_at (loc, OPT_Wpointer_to_int_cast,
7588 : "cast from pointer to integer of different size");
7589 :
7590 102182247 : if ((TREE_CODE (value) == CALL_EXPR
7591 34689910 : && !is_access_with_size_p (value))
7592 136872153 : && TREE_CODE (type) != TREE_CODE (otype))
7593 2306 : warning_at (loc, OPT_Wbad_function_cast,
7594 : "cast from function call of type %qT "
7595 : "to non-matching type %qT", otype, type);
7596 :
7597 102182247 : if (TREE_CODE (type) == POINTER_TYPE
7598 3341870 : && (TREE_CODE (otype) == INTEGER_TYPE
7599 3341870 : || TREE_CODE (otype) == BITINT_TYPE)
7600 346964 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7601 : /* Don't warn about converting any constant. */
7602 102491279 : && !TREE_CONSTANT (value))
7603 407 : warning_at (loc,
7604 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7605 : "of different size");
7606 :
7607 102182247 : if (warn_strict_aliasing <= 2)
7608 93645309 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7609 :
7610 : /* If pedantic, warn for conversions between function and object
7611 : pointer types, except for converting a null pointer constant
7612 : to function pointer type. */
7613 102182247 : if (pedantic
7614 238455 : && TREE_CODE (type) == POINTER_TYPE
7615 143953 : && TREE_CODE (otype) == POINTER_TYPE
7616 2622 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7617 102182268 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7618 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7619 : "conversion of function pointer to object pointer type");
7620 :
7621 102182247 : if (pedantic
7622 238455 : && TREE_CODE (type) == POINTER_TYPE
7623 143953 : && TREE_CODE (otype) == POINTER_TYPE
7624 2622 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7625 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7626 102182257 : && !null_pointer_constant_p (value))
7627 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7628 : "conversion of object pointer to function pointer type");
7629 :
7630 102182247 : if (TREE_CODE (type) == POINTER_TYPE
7631 3341870 : && TREE_CODE (otype) == POINTER_TYPE
7632 2994820 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7633 8680 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7634 102189806 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7635 7559 : TREE_TYPE (otype)))
7636 799 : warning_at (loc, OPT_Wcast_function_type,
7637 : "cast between incompatible function types"
7638 : " from %qT to %qT", otype, type);
7639 :
7640 102182247 : ovalue = value;
7641 : /* If converting to boolean a value with integer operands that
7642 : is not itself represented as an INTEGER_CST, the call below
7643 : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7644 : build_binary_op as called by c_common_truthvalue_conversion
7645 : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7646 : subexpression has been fully folded. To avoid nested
7647 : C_MAYBE_CONST_EXPR, ensure that
7648 : c_objc_common_truthvalue_conversion receives an argument
7649 : properly marked as having integer operands in that case. */
7650 102182247 : if (int_operands
7651 6687578 : && TREE_CODE (value) != INTEGER_CST
7652 102182289 : && (TREE_CODE (type) == BOOLEAN_TYPE
7653 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7654 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7655 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7656 28 : value = note_integer_operands (value);
7657 102182247 : value = convert (type, value);
7658 :
7659 : /* Ignore any integer overflow caused by the cast. */
7660 102182247 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7661 : {
7662 6634300 : if (TREE_OVERFLOW_P (ovalue))
7663 : {
7664 9 : if (!TREE_OVERFLOW (value))
7665 : {
7666 : /* Avoid clobbering a shared constant. */
7667 0 : value = copy_node (value);
7668 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7669 : }
7670 : }
7671 6634291 : else if (TREE_OVERFLOW (value))
7672 : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7673 3093 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7674 : }
7675 : }
7676 :
7677 : /* Don't let a cast be an lvalue. */
7678 120314597 : if (lvalue_p (value))
7679 90762 : value = non_lvalue_loc (loc, value);
7680 :
7681 : /* Don't allow the results of casting to floating-point or complex
7682 : types be confused with actual constants, or casts involving
7683 : integer and pointer types other than direct integer-to-integer
7684 : and integer-to-pointer be confused with integer constant
7685 : expressions and null pointer constants. */
7686 120314597 : if (TREE_CODE (value) == REAL_CST
7687 120264654 : || TREE_CODE (value) == COMPLEX_CST
7688 240556628 : || (TREE_CODE (value) == INTEGER_CST
7689 6883416 : && !((TREE_CODE (expr) == INTEGER_CST
7690 6809731 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7691 66282 : || TREE_CODE (expr) == REAL_CST
7692 : || TREE_CODE (expr) == COMPLEX_CST)))
7693 134481 : value = build1 (NOP_EXPR, type, value);
7694 :
7695 : /* If the expression has integer operands and so can occur in an
7696 : unevaluated part of an integer constant expression, ensure the
7697 : return value reflects this. */
7698 120314597 : if (int_operands
7699 6863948 : && INTEGRAL_TYPE_P (type)
7700 6417305 : && value != error_mark_node
7701 126731901 : && !EXPR_INT_CONST_OPERANDS (value))
7702 11 : value = note_integer_operands (value);
7703 :
7704 120314597 : protected_set_expr_location (value, loc);
7705 120314597 : return value;
7706 : }
7707 :
7708 : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7709 : location of the open paren of the cast, or the position of the cast
7710 : expr. */
7711 : tree
7712 120333870 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7713 : {
7714 120333870 : tree type;
7715 120333870 : tree type_expr = NULL_TREE;
7716 120333870 : bool type_expr_const = true;
7717 120333870 : tree ret;
7718 120333870 : int saved_wsp = warn_strict_prototypes;
7719 :
7720 : /* This avoids warnings about unprototyped casts on
7721 : integers. E.g. "#define SIG_DFL (void(*)())0". */
7722 120333870 : if (TREE_CODE (expr) == INTEGER_CST)
7723 6931955 : warn_strict_prototypes = 0;
7724 120333870 : type = groktypename (type_name, &type_expr, &type_expr_const);
7725 120333870 : warn_strict_prototypes = saved_wsp;
7726 :
7727 765069 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7728 121098684 : && reject_gcc_builtin (expr))
7729 2 : return error_mark_node;
7730 :
7731 120333868 : ret = build_c_cast (loc, type, expr);
7732 120333868 : if (ret == error_mark_node)
7733 : return error_mark_node;
7734 :
7735 120332708 : if (type_expr)
7736 : {
7737 250 : bool inner_expr_const = true;
7738 250 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7739 250 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7740 500 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7741 145 : && inner_expr_const);
7742 250 : SET_EXPR_LOCATION (ret, loc);
7743 : }
7744 :
7745 120332708 : if (!EXPR_HAS_LOCATION (ret))
7746 6810198 : protected_set_expr_location (ret, loc);
7747 :
7748 : /* C++ does not permits types to be defined in a cast, but it
7749 : allows references to incomplete types. */
7750 120332708 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7751 1 : warning_at (loc, OPT_Wc___compat,
7752 : "defining a type in a cast is invalid in C++");
7753 :
7754 : return ret;
7755 : }
7756 :
7757 : /* Build an assignment expression of lvalue LHS from value RHS.
7758 : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7759 : may differ from TREE_TYPE (LHS) for an enum bitfield.
7760 : MODIFYCODE is the code for a binary operator that we use
7761 : to combine the old value of LHS with RHS to get the new value.
7762 : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7763 : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7764 : which may differ from TREE_TYPE (RHS) for an enum value.
7765 :
7766 : LOCATION is the location of the MODIFYCODE operator.
7767 : RHS_LOC is the location of the RHS. */
7768 :
7769 : tree
7770 2921360 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7771 : enum tree_code modifycode,
7772 : location_t rhs_loc, tree rhs, tree rhs_origtype)
7773 : {
7774 2921360 : tree result;
7775 2921360 : tree newrhs;
7776 2921360 : tree rhseval = NULL_TREE;
7777 2921360 : tree lhstype = TREE_TYPE (lhs);
7778 2921360 : tree olhstype = lhstype;
7779 2921360 : bool npc;
7780 2921360 : bool is_atomic_op;
7781 :
7782 : /* Types that aren't fully specified cannot be used in assignments. */
7783 2921360 : lhs = require_complete_type (location, lhs);
7784 2921360 : rhs = require_complete_type (location, rhs);
7785 :
7786 : /* Avoid duplicate error messages from operands that had errors. */
7787 2921360 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7788 473 : return error_mark_node;
7789 :
7790 : /* Ensure an error for assigning a non-lvalue array to an array in
7791 : C90. */
7792 2920887 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7793 : {
7794 1 : error_at (location, "assignment to expression with array type");
7795 1 : return error_mark_node;
7796 : }
7797 :
7798 : /* For ObjC properties, defer this check. */
7799 2920886 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7800 32 : return error_mark_node;
7801 :
7802 2920854 : is_atomic_op = really_atomic_lvalue (lhs);
7803 :
7804 2920854 : newrhs = rhs;
7805 :
7806 2920854 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7807 : {
7808 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7809 : lhs_origtype, modifycode, rhs_loc, rhs,
7810 : rhs_origtype);
7811 2 : if (inner == error_mark_node)
7812 : return error_mark_node;
7813 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7814 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7815 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7816 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7817 2 : protected_set_expr_location (result, location);
7818 2 : return result;
7819 : }
7820 :
7821 : /* If a binary op has been requested, combine the old LHS value with the RHS
7822 : producing the value we should actually store into the LHS. */
7823 :
7824 2920852 : if (modifycode != NOP_EXPR)
7825 : {
7826 215965 : lhs = c_fully_fold (lhs, false, NULL, true);
7827 215965 : lhs = stabilize_reference (lhs);
7828 :
7829 : /* Construct the RHS for any non-atomic compound assignment. */
7830 215965 : if (!is_atomic_op)
7831 : {
7832 : /* If in LHS op= RHS the RHS has side-effects, ensure they
7833 : are preevaluated before the rest of the assignment expression's
7834 : side-effects, because RHS could contain e.g. function calls
7835 : that modify LHS. */
7836 200480 : if (TREE_SIDE_EFFECTS (rhs))
7837 : {
7838 8831 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7839 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7840 : else
7841 8803 : newrhs = save_expr (rhs);
7842 8831 : rhseval = newrhs;
7843 8831 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7844 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7845 : newrhs);
7846 : }
7847 200480 : bool clear_decl_read = false;
7848 88719 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7849 154036 : && !DECL_READ_P (lhs)
7850 253009 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7851 : : warn_unused_but_set_parameter) > 2)
7852 : {
7853 21639 : mark_exp_read (newrhs);
7854 21639 : if (!DECL_READ_P (lhs))
7855 200480 : clear_decl_read = true;
7856 : }
7857 :
7858 200480 : newrhs = build_binary_op (location, modifycode,
7859 : convert_lvalue_to_rvalue (location, lhs,
7860 : true, true),
7861 : newrhs, true);
7862 200480 : if (clear_decl_read)
7863 21639 : DECL_READ_P (lhs) = 0;
7864 :
7865 : /* The original type of the right hand side is no longer
7866 : meaningful. */
7867 : rhs_origtype = NULL_TREE;
7868 : }
7869 : }
7870 :
7871 2920852 : if (c_dialect_objc ())
7872 : {
7873 : /* Check if we are modifying an Objective-C property reference;
7874 : if so, we need to generate setter calls. */
7875 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7876 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7877 : else
7878 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7879 0 : if (result)
7880 0 : goto return_result;
7881 :
7882 : /* Else, do the check that we postponed for Objective-C. */
7883 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7884 0 : return error_mark_node;
7885 : }
7886 :
7887 : /* Give an error for storing in something that is 'const'. */
7888 :
7889 2920852 : if (TYPE_READONLY (lhstype)
7890 2920852 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7891 25779 : && C_TYPE_FIELDS_READONLY (lhstype)))
7892 : {
7893 88 : readonly_error (location, lhs, lv_assign);
7894 88 : return error_mark_node;
7895 : }
7896 2920764 : else if (TREE_READONLY (lhs))
7897 1 : readonly_warning (lhs, lv_assign);
7898 :
7899 : /* If storing into a structure or union member,
7900 : it has probably been given type `int'.
7901 : Compute the type that would go with
7902 : the actual amount of storage the member occupies. */
7903 :
7904 2920764 : if (TREE_CODE (lhs) == COMPONENT_REF
7905 250499 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7906 250499 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7907 110410 : || SCALAR_FLOAT_TYPE_P (lhstype)
7908 87012 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7909 170491 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7910 :
7911 : /* If storing in a field that is in actuality a short or narrower than one,
7912 : we must store in the field in its actual type. */
7913 :
7914 2920764 : if (lhstype != TREE_TYPE (lhs))
7915 : {
7916 0 : lhs = copy_node (lhs);
7917 0 : TREE_TYPE (lhs) = lhstype;
7918 : }
7919 :
7920 : /* Issue -Wc++-compat warnings about an assignment to an enum type
7921 : when LHS does not have its original type. This happens for,
7922 : e.g., an enum bitfield in a struct. */
7923 2920764 : if (warn_cxx_compat
7924 3272 : && lhs_origtype != NULL_TREE
7925 3272 : && lhs_origtype != lhstype
7926 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7927 : {
7928 4 : tree checktype = (rhs_origtype != NULL_TREE
7929 4 : ? rhs_origtype
7930 0 : : TREE_TYPE (rhs));
7931 4 : if (checktype != error_mark_node
7932 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7933 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7934 2 : warning_at (location, OPT_Wc___compat,
7935 : "enum conversion in assignment is invalid in C++");
7936 : }
7937 :
7938 : /* Remove qualifiers. */
7939 2920764 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7940 2920764 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7941 :
7942 : /* Convert new value to destination type. Fold it first, then
7943 : restore any excess precision information, for the sake of
7944 : conversion warnings. */
7945 :
7946 2920764 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7947 : {
7948 2905279 : tree rhs_semantic_type = NULL_TREE;
7949 2905279 : if (!c_in_omp_for)
7950 : {
7951 2888942 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7952 : {
7953 5077 : rhs_semantic_type = TREE_TYPE (newrhs);
7954 5077 : newrhs = TREE_OPERAND (newrhs, 0);
7955 : }
7956 2888942 : npc = null_pointer_constant_p (newrhs);
7957 2888942 : newrhs = c_fully_fold (newrhs, false, NULL);
7958 2888942 : if (rhs_semantic_type)
7959 5077 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7960 : }
7961 : else
7962 16337 : npc = null_pointer_constant_p (newrhs);
7963 2905279 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7964 : rhs_origtype, ic_assign, npc,
7965 : NULL_TREE, NULL_TREE, 0);
7966 2905279 : if (TREE_CODE (newrhs) == ERROR_MARK)
7967 127 : return error_mark_node;
7968 : }
7969 :
7970 : /* Emit ObjC write barrier, if necessary. */
7971 2920637 : if (c_dialect_objc () && flag_objc_gc)
7972 : {
7973 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7974 0 : if (result)
7975 : {
7976 0 : protected_set_expr_location (result, location);
7977 0 : goto return_result;
7978 : }
7979 : }
7980 :
7981 : /* Scan operands. */
7982 :
7983 2920637 : if (is_atomic_op)
7984 25964 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7985 : else
7986 : {
7987 2894673 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7988 2894673 : TREE_SIDE_EFFECTS (result) = 1;
7989 2894673 : protected_set_expr_location (result, location);
7990 : }
7991 :
7992 : /* If we got the LHS in a different type for storing in,
7993 : convert the result back to the nominal type of LHS
7994 : so that the value we return always has the same type
7995 : as the LHS argument. */
7996 :
7997 2920637 : if (olhstype == TREE_TYPE (result))
7998 2920637 : goto return_result;
7999 :
8000 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
8001 : rhs_origtype, ic_assign, false, NULL_TREE,
8002 : NULL_TREE, 0);
8003 0 : protected_set_expr_location (result, location);
8004 :
8005 2920637 : return_result:
8006 2920637 : if (rhseval)
8007 8831 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
8008 : return result;
8009 : }
8010 :
8011 : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
8012 : This is used to implement -fplan9-extensions. */
8013 :
8014 : static bool
8015 26 : find_anonymous_field_with_type (tree struct_type, tree type)
8016 : {
8017 26 : tree field;
8018 26 : bool found;
8019 :
8020 26 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
8021 26 : found = false;
8022 26 : for (field = TYPE_FIELDS (struct_type);
8023 68 : field != NULL_TREE;
8024 42 : field = TREE_CHAIN (field))
8025 : {
8026 42 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8027 42 : if (DECL_NAME (field) == NULL
8028 42 : && comptypes (type, fieldtype))
8029 : {
8030 4 : if (found)
8031 : return false;
8032 : found = true;
8033 : }
8034 38 : else if (DECL_NAME (field) == NULL
8035 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
8036 40 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
8037 : {
8038 0 : if (found)
8039 : return false;
8040 : found = true;
8041 : }
8042 : }
8043 : return found;
8044 : }
8045 :
8046 : /* RHS is an expression whose type is pointer to struct. If there is
8047 : an anonymous field in RHS with type TYPE, then return a pointer to
8048 : that field in RHS. This is used with -fplan9-extensions. This
8049 : returns NULL if no conversion could be found. */
8050 :
8051 : static tree
8052 28 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
8053 : {
8054 28 : tree rhs_struct_type, lhs_main_type;
8055 28 : tree field, found_field;
8056 28 : bool found_sub_field;
8057 28 : tree ret;
8058 :
8059 28 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
8060 28 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
8061 28 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
8062 :
8063 28 : gcc_assert (POINTER_TYPE_P (type));
8064 28 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
8065 :
8066 28 : found_field = NULL_TREE;
8067 28 : found_sub_field = false;
8068 28 : for (field = TYPE_FIELDS (rhs_struct_type);
8069 172 : field != NULL_TREE;
8070 144 : field = TREE_CHAIN (field))
8071 : {
8072 148 : if (DECL_NAME (field) != NULL_TREE
8073 148 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
8074 96 : continue;
8075 52 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
8076 52 : if (comptypes (lhs_main_type, fieldtype))
8077 : {
8078 28 : if (found_field != NULL_TREE)
8079 : return NULL_TREE;
8080 : found_field = field;
8081 : }
8082 24 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
8083 : lhs_main_type))
8084 : {
8085 4 : if (found_field != NULL_TREE)
8086 : return NULL_TREE;
8087 : found_field = field;
8088 : found_sub_field = true;
8089 : }
8090 : }
8091 :
8092 24 : if (found_field == NULL_TREE)
8093 : return NULL_TREE;
8094 :
8095 24 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
8096 : build_fold_indirect_ref (rhs), found_field,
8097 : NULL_TREE);
8098 24 : ret = build_fold_addr_expr_loc (location, ret);
8099 :
8100 24 : if (found_sub_field)
8101 : {
8102 2 : ret = convert_to_anonymous_field (location, type, ret);
8103 2 : gcc_assert (ret != NULL_TREE);
8104 : }
8105 :
8106 : return ret;
8107 : }
8108 :
8109 : /* Issue an error message for a bad initializer component.
8110 : GMSGID identifies the message.
8111 : The component name is taken from the spelling stack. */
8112 :
8113 : static void ATTRIBUTE_GCC_DIAG (2,0)
8114 944 : error_init (location_t loc, const char *gmsgid, ...)
8115 : {
8116 944 : char *ofwhat;
8117 :
8118 944 : auto_diagnostic_group d;
8119 :
8120 : /* The gmsgid may be a format string with %< and %>. */
8121 944 : va_list ap;
8122 944 : va_start (ap, gmsgid);
8123 944 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
8124 944 : loc, -1, gmsgid, &ap);
8125 944 : va_end (ap);
8126 :
8127 944 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8128 944 : if (*ofwhat && warned)
8129 220 : inform (loc, "(near initialization for %qs)", ofwhat);
8130 944 : }
8131 :
8132 : /* Used to implement pedwarn_init and permerror_init. */
8133 :
8134 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8135 2478 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
8136 : va_list *ap, enum diagnostics::kind kind)
8137 : {
8138 : /* Use the location where a macro was expanded rather than where
8139 : it was defined to make sure macros defined in system headers
8140 : but used incorrectly elsewhere are diagnosed. */
8141 2478 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8142 2478 : auto_diagnostic_group d;
8143 2478 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
8144 2478 : if (warned)
8145 : {
8146 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8147 1815 : if (*ofwhat)
8148 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
8149 : }
8150 4956 : return warned;
8151 2478 : }
8152 :
8153 : /* Issue a pedantic warning for a bad initializer component. OPT is
8154 : the option OPT_* (from options.h) controlling this warning or 0 if
8155 : it is unconditionally given. GMSGID identifies the message. The
8156 : component name is taken from the spelling stack. */
8157 :
8158 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8159 2127 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
8160 : {
8161 2127 : va_list ap;
8162 2127 : va_start (ap, gmsgid);
8163 2127 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8164 : diagnostics::kind::pedwarn);
8165 2127 : va_end (ap);
8166 2127 : return warned;
8167 : }
8168 :
8169 : /* Like pedwarn_init, but issue a permerror. */
8170 :
8171 : static bool ATTRIBUTE_GCC_DIAG (3,0)
8172 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
8173 : {
8174 351 : va_list ap;
8175 351 : va_start (ap, gmsgid);
8176 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
8177 : diagnostics::kind::permerror);
8178 351 : va_end (ap);
8179 351 : return warned;
8180 : }
8181 :
8182 : /* Issue a warning for a bad initializer component.
8183 :
8184 : OPT is the OPT_W* value corresponding to the warning option that
8185 : controls this warning. GMSGID identifies the message. The
8186 : component name is taken from the spelling stack. */
8187 :
8188 : static void
8189 146 : warning_init (location_t loc, int opt, const char *gmsgid)
8190 : {
8191 146 : char *ofwhat;
8192 146 : bool warned;
8193 :
8194 146 : auto_diagnostic_group d;
8195 :
8196 : /* Use the location where a macro was expanded rather than where
8197 : it was defined to make sure macros defined in system headers
8198 : but used incorrectly elsewhere are diagnosed. */
8199 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8200 :
8201 : /* The gmsgid may be a format string with %< and %>. */
8202 146 : warned = warning_at (exploc, opt, gmsgid);
8203 146 : if (warned)
8204 : {
8205 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8206 133 : if (*ofwhat)
8207 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8208 : }
8209 146 : }
8210 :
8211 : /* If TYPE is an array type and EXPR is a parenthesized string
8212 : constant, warn if pedantic that EXPR is being used to initialize an
8213 : object of type TYPE. */
8214 :
8215 : void
8216 7288369 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8217 : {
8218 7288369 : if (pedantic
8219 80540 : && TREE_CODE (type) == ARRAY_TYPE
8220 1193 : && TREE_CODE (expr.value) == STRING_CST
8221 472 : && expr.original_code != STRING_CST)
8222 13 : pedwarn_init (loc, OPT_Wpedantic,
8223 : "array initialized from parenthesized string constant");
8224 7288369 : }
8225 :
8226 : /* Attempt to locate the parameter with the given index within FNDECL,
8227 : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8228 :
8229 : static location_t
8230 937 : get_fndecl_argument_location (tree fndecl, int argnum)
8231 : {
8232 937 : int i;
8233 937 : tree param;
8234 :
8235 : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8236 937 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8237 1016 : i < argnum && param;
8238 79 : i++, param = TREE_CHAIN (param))
8239 : ;
8240 :
8241 : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8242 : return DECL_SOURCE_LOCATION (FNDECL). */
8243 937 : if (param == NULL)
8244 653 : return DECL_SOURCE_LOCATION (fndecl);
8245 :
8246 284 : return DECL_SOURCE_LOCATION (param);
8247 : }
8248 :
8249 : /* Issue a note about a mismatching argument for parameter PARMNUM
8250 : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8251 : Attempt to issue the note at the pertinent parameter of the decl;
8252 : failing that issue it at the location of FUNDECL; failing that
8253 : issue it at PLOC.
8254 : Use highlight_colors::actual for the ACTUAL_TYPE
8255 : and highlight_colors::expected for EXPECTED_TYPE and the
8256 : parameter of FUNDECL*/
8257 :
8258 : static void
8259 1061 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8260 : tree expected_type, tree actual_type)
8261 : {
8262 1061 : location_t loc;
8263 1061 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8264 937 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8265 : else
8266 : loc = ploc;
8267 :
8268 1061 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8269 :
8270 1061 : pp_markup::element_expected_type elem_expected_type (expected_type);
8271 1061 : pp_markup::element_actual_type elem_actual_type (actual_type);
8272 1061 : inform (&richloc,
8273 : "expected %e but argument is of type %e",
8274 : &elem_expected_type, &elem_actual_type);
8275 1061 : }
8276 :
8277 : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8278 : function FUNDECL declared without prototype to parameter PARMNUM of
8279 : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8280 :
8281 : static void
8282 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8283 : tree parmtype, tree argtype)
8284 : {
8285 418 : tree_code parmcode = TREE_CODE (parmtype);
8286 418 : tree_code argcode = TREE_CODE (argtype);
8287 418 : tree promoted = c_type_promotes_to (argtype);
8288 :
8289 : /* Avoid warning for enum arguments that promote to an integer type
8290 : of the same size/mode. */
8291 418 : if (parmcode == INTEGER_TYPE
8292 418 : && argcode == ENUMERAL_TYPE
8293 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8294 : return;
8295 :
8296 417 : if ((parmcode == argcode
8297 222 : || (parmcode == INTEGER_TYPE
8298 : && argcode == ENUMERAL_TYPE))
8299 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8300 : return;
8301 :
8302 : /* This diagnoses even signed/unsigned mismatches. Those might be
8303 : safe in many cases but GCC may emit suboptimal code for them so
8304 : warning on those cases drives efficiency improvements. */
8305 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8306 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8307 : ? G_("%qD argument %d type is %qT where %qT is expected "
8308 : "in a call to built-in function declared without "
8309 : "prototype")
8310 : : G_("%qD argument %d promotes to %qT where %qT is expected "
8311 : "in a call to built-in function declared without "
8312 : "prototype"),
8313 : fundecl, parmnum, promoted, parmtype))
8314 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8315 : "built-in %qD declared here",
8316 : fundecl);
8317 : }
8318 :
8319 : /* Print a declaration in quotes, with the given highlight_color.
8320 : Analogous to handler for %qD, but with a specific highlight color. */
8321 :
8322 199 : class pp_element_quoted_decl : public pp_element
8323 : {
8324 : public:
8325 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8326 199 : : m_decl (decl),
8327 199 : m_highlight_color (highlight_color)
8328 : {
8329 : }
8330 :
8331 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8332 : {
8333 199 : ctxt.begin_quote ();
8334 199 : ctxt.begin_highlight_color (m_highlight_color);
8335 :
8336 199 : print_decl (ctxt);
8337 :
8338 199 : ctxt.end_highlight_color ();
8339 199 : ctxt.end_quote ();
8340 199 : }
8341 :
8342 199 : void print_decl (pp_markup::context &ctxt)
8343 : {
8344 199 : pretty_printer *const pp = &ctxt.m_pp;
8345 199 : pp->set_padding (pp_none);
8346 199 : if (DECL_NAME (m_decl))
8347 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8348 : else
8349 0 : pp_string (pp, _("({anonymous})"));
8350 199 : }
8351 :
8352 : private:
8353 : tree m_decl;
8354 : const char *m_highlight_color;
8355 : };
8356 :
8357 : /* If TYPE is from a typedef, issue a note showing the location
8358 : to the user.
8359 : Use HIGHLIGHT_COLOR as the highlight color. */
8360 :
8361 : static void
8362 1178 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8363 : {
8364 1178 : if (!typedef_variant_p (type))
8365 1144 : return;
8366 :
8367 34 : tree typedef_decl = TYPE_NAME (type);
8368 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8369 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8370 34 : nullptr, highlight_color);
8371 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8372 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8373 34 : }
8374 :
8375 : /* Convert value RHS to type TYPE as preparation for an assignment to
8376 : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8377 : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8378 : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8379 : constant before any folding.
8380 : The real work of conversion is done by `convert'.
8381 : The purpose of this function is to generate error messages
8382 : for assignments that are not allowed in C.
8383 : ERRTYPE says whether it is argument passing, assignment,
8384 : initialization or return.
8385 :
8386 : In the following example, '~' denotes where EXPR_LOC and '^' where
8387 : LOCATION point to:
8388 :
8389 : f (var); [ic_argpass]
8390 : ^ ~~~
8391 : x = var; [ic_assign]
8392 : ^ ~~~;
8393 : int x = var; [ic_init]
8394 : ^^^
8395 : return x; [ic_return]
8396 : ^
8397 :
8398 : FUNCTION is a tree for the function being called.
8399 : PARMNUM is the number of the argument, for printing in error messages.
8400 : WARNOPT may be set to a warning option to issue the corresponding warning
8401 : rather than an error for invalid conversions. Used for calls to built-in
8402 : functions declared without a prototype. */
8403 :
8404 : static tree
8405 167199532 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8406 : tree rhs, tree origtype, enum impl_conv errtype,
8407 : bool null_pointer_constant, tree fundecl,
8408 : tree function, int parmnum, int warnopt /* = 0 */)
8409 : {
8410 167199532 : enum tree_code codel = TREE_CODE (type);
8411 167199532 : tree orig_rhs = rhs;
8412 167199532 : tree rhstype;
8413 167199532 : enum tree_code coder;
8414 167199532 : tree rname = NULL_TREE;
8415 167199532 : bool objc_ok = false;
8416 :
8417 : /* Use the expansion point location to handle cases such as user's
8418 : function returning a wrong-type macro defined in a system header. */
8419 167199532 : location = expansion_point_location_if_in_system_header (location);
8420 :
8421 167199532 : if (errtype == ic_argpass)
8422 : {
8423 125707280 : tree selector;
8424 : /* Change pointer to function to the function itself for
8425 : diagnostics. */
8426 125707280 : if (TREE_CODE (function) == ADDR_EXPR
8427 125707280 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8428 0 : function = TREE_OPERAND (function, 0);
8429 :
8430 : /* Handle an ObjC selector specially for diagnostics. */
8431 125707280 : selector = objc_message_selector ();
8432 125707280 : rname = function;
8433 125707280 : if (selector && parmnum > 2)
8434 : {
8435 0 : rname = selector;
8436 0 : parmnum -= 2;
8437 : }
8438 : }
8439 :
8440 : /* This macro is used to emit diagnostics to ensure that all format
8441 : strings are complete sentences, visible to gettext and checked at
8442 : compile time. */
8443 : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8444 : do { \
8445 : switch (errtype) \
8446 : { \
8447 : case ic_argpass: \
8448 : { \
8449 : auto_diagnostic_group d; \
8450 : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8451 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8452 : } \
8453 : break; \
8454 : case ic_assign: \
8455 : pedwarn (LOCATION, OPT, AS); \
8456 : break; \
8457 : case ic_init: \
8458 : case ic_init_const: \
8459 : pedwarn_init (LOCATION, OPT, IN); \
8460 : break; \
8461 : case ic_return: \
8462 : pedwarn (LOCATION, OPT, RE); \
8463 : break; \
8464 : default: \
8465 : gcc_unreachable (); \
8466 : } \
8467 : } while (0)
8468 :
8469 : /* This macro is used to emit diagnostics to ensure that all format
8470 : strings are complete sentences, visible to gettext and checked at
8471 : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8472 : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8473 : do { \
8474 : switch (errtype) \
8475 : { \
8476 : case ic_argpass: \
8477 : { \
8478 : auto_diagnostic_group d; \
8479 : if (PEDWARN) { \
8480 : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8481 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8482 : } else { \
8483 : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8484 : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8485 : } \
8486 : } \
8487 : break; \
8488 : case ic_assign: \
8489 : if (PEDWARN) \
8490 : pedwarn (LOCATION, OPT, AS, QUALS); \
8491 : else \
8492 : warning_at (LOCATION, OPT, AS, QUALS); \
8493 : break; \
8494 : case ic_init: \
8495 : case ic_init_const: \
8496 : if (PEDWARN) \
8497 : pedwarn (LOCATION, OPT, IN, QUALS); \
8498 : else \
8499 : warning_at (LOCATION, OPT, IN, QUALS); \
8500 : break; \
8501 : case ic_return: \
8502 : if (PEDWARN) \
8503 : pedwarn (LOCATION, OPT, RE, QUALS); \
8504 : else \
8505 : warning_at (LOCATION, OPT, RE, QUALS); \
8506 : break; \
8507 : default: \
8508 : gcc_unreachable (); \
8509 : } \
8510 : } while (0)
8511 :
8512 : /* This macro is used to emit diagnostics to ensure that all format
8513 : strings are complete sentences, visible to gettext and checked at
8514 : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8515 : extra parameter to enumerate qualifiers. */
8516 : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8517 : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8518 :
8519 :
8520 167199532 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8521 5985 : rhs = TREE_OPERAND (rhs, 0);
8522 :
8523 167199532 : rhstype = TREE_TYPE (rhs);
8524 167199532 : coder = TREE_CODE (rhstype);
8525 :
8526 167199532 : if (coder == ERROR_MARK)
8527 347 : return error_mark_node;
8528 :
8529 167199185 : if (c_dialect_objc ())
8530 : {
8531 0 : int parmno;
8532 :
8533 0 : switch (errtype)
8534 : {
8535 : case ic_return:
8536 : parmno = 0;
8537 : break;
8538 :
8539 : case ic_assign:
8540 : parmno = -1;
8541 : break;
8542 :
8543 : case ic_init:
8544 : case ic_init_const:
8545 : parmno = -2;
8546 : break;
8547 :
8548 : default:
8549 0 : parmno = parmnum;
8550 : break;
8551 : }
8552 :
8553 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8554 : }
8555 :
8556 167199185 : if (warn_cxx_compat)
8557 : {
8558 29113 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8559 29113 : if (checktype != error_mark_node
8560 29113 : && TREE_CODE (type) == ENUMERAL_TYPE
8561 29219 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8562 45 : switch (errtype)
8563 : {
8564 8 : case ic_argpass:
8565 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8566 : "passing argument %d of %qE is invalid in C++",
8567 : parmnum, rname))
8568 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8569 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8570 : "expected %qT but argument is of type %qT",
8571 : type, rhstype);
8572 : break;
8573 10 : case ic_assign:
8574 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8575 : "%qT in assignment is invalid in C++", rhstype, type);
8576 10 : break;
8577 18 : case ic_init:
8578 18 : case ic_init_const:
8579 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8580 : "%qT to %qT in initialization is invalid in C++",
8581 : rhstype, type);
8582 18 : break;
8583 9 : case ic_return:
8584 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8585 : "%qT in return is invalid in C++", rhstype, type);
8586 9 : break;
8587 0 : default:
8588 0 : gcc_unreachable ();
8589 : }
8590 : }
8591 :
8592 167199185 : if (warn_enum_conversion)
8593 : {
8594 14602513 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8595 14602513 : if (checktype != error_mark_node
8596 14602513 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8597 12839 : && TREE_CODE (type) == ENUMERAL_TYPE
8598 14610805 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8599 : {
8600 7 : gcc_rich_location loc (location);
8601 7 : warning_at (&loc, OPT_Wenum_conversion,
8602 : "implicit conversion from %qT to %qT",
8603 : checktype, type);
8604 7 : }
8605 : }
8606 :
8607 167199185 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8608 : {
8609 148068152 : warn_for_address_of_packed_member (type, orig_rhs);
8610 148068152 : if (type != rhstype)
8611 : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8612 36887016 : rhs = convert (type, rhs);
8613 148068152 : return rhs;
8614 : }
8615 :
8616 19131033 : if (coder == VOID_TYPE)
8617 : {
8618 : /* Except for passing an argument to an unprototyped function,
8619 : this is a constraint violation. When passing an argument to
8620 : an unprototyped function, it is compile-time undefined;
8621 : making it a constraint in that case was rejected in
8622 : DR#252. */
8623 8 : const char msg[] = "void value not ignored as it ought to be";
8624 8 : if (warnopt)
8625 0 : warning_at (location, warnopt, msg);
8626 : else
8627 8 : error_at (location, msg);
8628 8 : return error_mark_node;
8629 : }
8630 19131025 : rhs = require_complete_type (location, rhs);
8631 19131025 : if (rhs == error_mark_node)
8632 : return error_mark_node;
8633 :
8634 19131025 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8635 6 : return error_mark_node;
8636 :
8637 : /* A non-reference type can convert to a reference. This handles
8638 : va_start, va_copy and possibly port built-ins. */
8639 19131019 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8640 : {
8641 286 : if (!lvalue_p (rhs))
8642 : {
8643 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8644 0 : if (warnopt)
8645 0 : warning_at (location, warnopt, msg);
8646 : else
8647 0 : error_at (location, msg);
8648 0 : return error_mark_node;
8649 : }
8650 286 : if (!c_mark_addressable (rhs))
8651 0 : return error_mark_node;
8652 286 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8653 286 : SET_EXPR_LOCATION (rhs, location);
8654 :
8655 286 : rhs = convert_for_assignment (location, expr_loc,
8656 286 : c_build_pointer_type (TREE_TYPE (type)),
8657 : rhs, origtype, errtype,
8658 : null_pointer_constant, fundecl, function,
8659 : parmnum, warnopt);
8660 286 : if (rhs == error_mark_node)
8661 : return error_mark_node;
8662 :
8663 286 : rhs = build1 (NOP_EXPR, type, rhs);
8664 286 : SET_EXPR_LOCATION (rhs, location);
8665 286 : return rhs;
8666 : }
8667 : /* Some types can interconvert without explicit casts. */
8668 19130733 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8669 19130733 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8670 9292949 : return convert (type, rhs);
8671 : /* Arithmetic types all interconvert, and enum is treated like int. */
8672 9837784 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8673 2284507 : || codel == FIXED_POINT_TYPE
8674 2284507 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8675 2197990 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8676 7738996 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8677 83604 : || coder == FIXED_POINT_TYPE
8678 83604 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8679 40503 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8680 : {
8681 7737622 : if (warnopt && errtype == ic_argpass)
8682 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8683 : rhstype);
8684 :
8685 7737622 : bool save = in_late_binary_op;
8686 7701554 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8687 15382011 : || (coder == REAL_TYPE
8688 276239 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8689 24199 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8690 99977 : in_late_binary_op = true;
8691 11121543 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8692 : ? expr_loc : location, type, orig_rhs,
8693 : errtype == ic_init_const);
8694 7737622 : in_late_binary_op = save;
8695 7737622 : return ret;
8696 : }
8697 :
8698 : /* Aggregates in different TUs might need conversion. */
8699 2100162 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8700 105 : && codel == coder
8701 2100184 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8702 9 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8703 9 : ? expr_loc : location, type, rhs);
8704 :
8705 : /* Conversion to a transparent union or record from its member types.
8706 : This applies only to function arguments. */
8707 2100153 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8708 96 : && TYPE_TRANSPARENT_AGGR (type))
8709 2100192 : && errtype == ic_argpass)
8710 : {
8711 39 : tree memb, marginal_memb = NULL_TREE;
8712 :
8713 50 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8714 : {
8715 50 : tree memb_type = TREE_TYPE (memb);
8716 :
8717 50 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8718 50 : TYPE_MAIN_VARIANT (rhstype)))
8719 : break;
8720 :
8721 27 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8722 1 : continue;
8723 :
8724 26 : if (coder == POINTER_TYPE)
8725 : {
8726 26 : tree ttl = TREE_TYPE (memb_type);
8727 26 : tree ttr = TREE_TYPE (rhstype);
8728 :
8729 : /* Any non-function converts to a [const][volatile] void *
8730 : and vice versa; otherwise, targets must be the same.
8731 : Meanwhile, the lhs target must have all the qualifiers of
8732 : the rhs. */
8733 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8734 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8735 45 : || comp_target_types (location, memb_type, rhstype))
8736 : {
8737 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8738 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8739 : /* If this type won't generate any warnings, use it. */
8740 16 : if (lquals == rquals
8741 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8742 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8743 0 : ? ((lquals | rquals) == rquals)
8744 16 : : ((lquals | rquals) == lquals)))
8745 : break;
8746 :
8747 : /* Keep looking for a better type, but remember this one. */
8748 0 : if (!marginal_memb)
8749 10 : marginal_memb = memb;
8750 : }
8751 : }
8752 :
8753 : /* Can convert integer zero to any pointer type. */
8754 10 : if (null_pointer_constant)
8755 : {
8756 0 : rhs = null_pointer_node;
8757 0 : break;
8758 : }
8759 : }
8760 :
8761 39 : if (memb || marginal_memb)
8762 : {
8763 39 : if (!memb)
8764 : {
8765 : /* We have only a marginally acceptable member type;
8766 : it needs a warning. */
8767 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8768 0 : tree ttr = TREE_TYPE (rhstype);
8769 :
8770 : /* Const and volatile mean something different for function
8771 : types, so the usual warnings are not appropriate. */
8772 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8773 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8774 : {
8775 : /* Because const and volatile on functions are
8776 : restrictions that say the function will not do
8777 : certain things, it is okay to use a const or volatile
8778 : function where an ordinary one is wanted, but not
8779 : vice-versa. */
8780 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8781 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8782 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8783 : OPT_Wdiscarded_qualifiers,
8784 : G_("passing argument %d of %qE "
8785 : "makes %q#v qualified function "
8786 : "pointer from unqualified"),
8787 : G_("assignment makes %q#v qualified "
8788 : "function pointer from "
8789 : "unqualified"),
8790 : G_("initialization makes %q#v qualified "
8791 : "function pointer from "
8792 : "unqualified"),
8793 : G_("return makes %q#v qualified function "
8794 : "pointer from unqualified"),
8795 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8796 : }
8797 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8798 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8799 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8800 : OPT_Wdiscarded_qualifiers,
8801 : G_("passing argument %d of %qE discards "
8802 : "%qv qualifier from pointer target type"),
8803 : G_("assignment discards %qv qualifier "
8804 : "from pointer target type"),
8805 : G_("initialization discards %qv qualifier "
8806 : "from pointer target type"),
8807 : G_("return discards %qv qualifier from "
8808 : "pointer target type"),
8809 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8810 :
8811 : memb = marginal_memb;
8812 : }
8813 :
8814 39 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8815 31 : pedwarn (location, OPT_Wpedantic,
8816 : "ISO C prohibits argument conversion to union type");
8817 :
8818 39 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8819 39 : return build_constructor_single (type, memb, rhs);
8820 : }
8821 : }
8822 :
8823 : /* Conversions among pointers */
8824 2100114 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8825 2098584 : && (coder == codel))
8826 : {
8827 : /* If RHS refers to a built-in declared without a prototype
8828 : BLTIN is the declaration of the built-in with a prototype
8829 : and RHSTYPE is set to the actual type of the built-in. */
8830 2020279 : tree bltin;
8831 2020279 : rhstype = type_or_builtin_type (rhs, &bltin);
8832 :
8833 2020279 : tree ttl = TREE_TYPE (type);
8834 2020279 : tree ttr = TREE_TYPE (rhstype);
8835 2020279 : bool is_opaque_pointer;
8836 2020279 : bool target_cmp = false; /* Cache comp_target_types () result. */
8837 2020279 : addr_space_t asl;
8838 2020279 : addr_space_t asr;
8839 :
8840 2020279 : tree mvl = remove_qualifiers (ttl);
8841 2020279 : tree mvr = remove_qualifiers (ttr);
8842 :
8843 : /* Opaque pointers are treated like void pointers. */
8844 2020279 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8845 :
8846 : /* The Plan 9 compiler permits a pointer to a struct to be
8847 : automatically converted into a pointer to an anonymous field
8848 : within the struct. */
8849 2020279 : if (flag_plan9_extensions
8850 50 : && RECORD_OR_UNION_TYPE_P (mvl)
8851 26 : && RECORD_OR_UNION_TYPE_P (mvr)
8852 26 : && mvl != mvr)
8853 : {
8854 26 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8855 26 : if (new_rhs != NULL_TREE)
8856 : {
8857 22 : rhs = new_rhs;
8858 22 : rhstype = TREE_TYPE (rhs);
8859 22 : coder = TREE_CODE (rhstype);
8860 22 : ttr = TREE_TYPE (rhstype);
8861 22 : mvr = TYPE_MAIN_VARIANT (ttr);
8862 : }
8863 : }
8864 :
8865 : /* C++ does not allow the implicit conversion void* -> T*. However,
8866 : for the purpose of reducing the number of false positives, we
8867 : tolerate the special case of
8868 :
8869 : int *p = NULL;
8870 :
8871 : where NULL is typically defined in C to be '(void *) 0'. */
8872 2020279 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8873 27391 : warning_at (errtype == ic_argpass ? expr_loc : location,
8874 14519 : OPT_Wc___compat,
8875 : "request for implicit conversion "
8876 : "from %qT to %qT not permitted in C++", rhstype, type);
8877 :
8878 : /* Warn of new allocations that are not big enough for the target
8879 : type. */
8880 2020279 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8881 5463 : if (tree fndecl = get_callee_fndecl (rhs))
8882 5463 : if (DECL_IS_MALLOC (fndecl))
8883 : {
8884 4614 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8885 4614 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8886 4614 : if (alloc_size)
8887 451 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8888 : }
8889 :
8890 : /* See if the pointers point to incompatible address spaces. */
8891 2020279 : asl = TYPE_ADDR_SPACE (ttl);
8892 2020279 : asr = TYPE_ADDR_SPACE (ttr);
8893 2020279 : if (!null_pointer_constant_p (rhs)
8894 2020279 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8895 : {
8896 3 : auto_diagnostic_group d;
8897 3 : bool diagnosed = true;
8898 3 : switch (errtype)
8899 : {
8900 1 : case ic_argpass:
8901 1 : {
8902 1 : const char msg[] = G_("passing argument %d of %qE from "
8903 : "pointer to non-enclosed address space");
8904 1 : if (warnopt)
8905 0 : diagnosed
8906 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8907 : else
8908 1 : error_at (expr_loc, msg, parmnum, rname);
8909 0 : break;
8910 : }
8911 0 : case ic_assign:
8912 0 : {
8913 0 : const char msg[] = G_("assignment from pointer to "
8914 : "non-enclosed address space");
8915 0 : if (warnopt)
8916 0 : diagnosed = warning_at (location, warnopt, msg);
8917 : else
8918 0 : error_at (location, msg);
8919 0 : break;
8920 : }
8921 0 : case ic_init:
8922 0 : case ic_init_const:
8923 0 : {
8924 0 : const char msg[] = G_("initialization from pointer to "
8925 : "non-enclosed address space");
8926 0 : if (warnopt)
8927 0 : diagnosed = warning_at (location, warnopt, msg);
8928 : else
8929 0 : error_at (location, msg);
8930 0 : break;
8931 : }
8932 2 : case ic_return:
8933 2 : {
8934 2 : const char msg[] = G_("return from pointer to "
8935 : "non-enclosed address space");
8936 2 : if (warnopt)
8937 0 : diagnosed = warning_at (location, warnopt, msg);
8938 : else
8939 2 : error_at (location, msg);
8940 0 : break;
8941 : }
8942 0 : default:
8943 0 : gcc_unreachable ();
8944 : }
8945 3 : if (diagnosed)
8946 : {
8947 3 : if (errtype == ic_argpass)
8948 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8949 : else
8950 2 : inform (location, "expected %qT but pointer is of type %qT",
8951 : type, rhstype);
8952 : }
8953 3 : return error_mark_node;
8954 3 : }
8955 :
8956 : /* Check if the right-hand side has a format attribute but the
8957 : left-hand side doesn't. */
8958 2020276 : if (warn_suggest_attribute_format
8959 2020276 : && check_missing_format_attribute (type, rhstype))
8960 : {
8961 16 : switch (errtype)
8962 : {
8963 4 : case ic_argpass:
8964 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8965 : "argument %d of %qE might be "
8966 : "a candidate for a format attribute",
8967 : parmnum, rname);
8968 4 : break;
8969 4 : case ic_assign:
8970 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8971 : "assignment left-hand side might be "
8972 : "a candidate for a format attribute");
8973 4 : break;
8974 4 : case ic_init:
8975 4 : case ic_init_const:
8976 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8977 : "initialization left-hand side might be "
8978 : "a candidate for a format attribute");
8979 4 : break;
8980 4 : case ic_return:
8981 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8982 : "return type might be "
8983 : "a candidate for a format attribute");
8984 4 : break;
8985 0 : default:
8986 0 : gcc_unreachable ();
8987 : }
8988 : }
8989 :
8990 : /* See if the pointers point to incompatible scalar storage orders. */
8991 2020276 : if (warn_scalar_storage_order
8992 2017360 : && !null_pointer_constant_p (rhs)
8993 5986236 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8994 1982980 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8995 : {
8996 18 : tree t;
8997 :
8998 18 : switch (errtype)
8999 : {
9000 11 : case ic_argpass:
9001 : /* Do not warn for built-in functions, for example memcpy, since we
9002 : control how they behave and they can be useful in this area. */
9003 11 : if (TREE_CODE (rname) != FUNCTION_DECL
9004 11 : || !fndecl_built_in_p (rname))
9005 1 : warning_at (location, OPT_Wscalar_storage_order,
9006 : "passing argument %d of %qE from incompatible "
9007 : "scalar storage order", parmnum, rname);
9008 : break;
9009 3 : case ic_assign:
9010 : /* Do not warn if the RHS is a call to a function that returns a
9011 : pointer that is not an alias. */
9012 3 : if (TREE_CODE (rhs) != CALL_EXPR
9013 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
9014 5 : || !DECL_IS_MALLOC (t))
9015 1 : warning_at (location, OPT_Wscalar_storage_order,
9016 : "assignment to %qT from pointer type %qT with "
9017 : "incompatible scalar storage order", type, rhstype);
9018 : break;
9019 3 : case ic_init:
9020 3 : case ic_init_const:
9021 : /* Likewise. */
9022 3 : if (TREE_CODE (rhs) != CALL_EXPR
9023 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
9024 5 : || !DECL_IS_MALLOC (t))
9025 1 : warning_at (location, OPT_Wscalar_storage_order,
9026 : "initialization of %qT from pointer type %qT with "
9027 : "incompatible scalar storage order", type, rhstype);
9028 : break;
9029 1 : case ic_return:
9030 1 : warning_at (location, OPT_Wscalar_storage_order,
9031 : "returning %qT from pointer type with incompatible "
9032 : "scalar storage order %qT", rhstype, type);
9033 1 : break;
9034 0 : default:
9035 0 : gcc_unreachable ();
9036 : }
9037 : }
9038 :
9039 : /* Any non-function converts to a [const][volatile] void *
9040 : and vice versa; otherwise, targets must be the same.
9041 : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
9042 420031 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
9043 1600246 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
9044 1551486 : || (target_cmp = comp_target_types (location, type, rhstype))
9045 2474 : || is_opaque_pointer
9046 2025224 : || ((c_common_unsigned_type (mvl)
9047 2474 : == c_common_unsigned_type (mvr))
9048 3396 : && (c_common_signed_type (mvl)
9049 1698 : == c_common_signed_type (mvr))
9050 1683 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
9051 : {
9052 : /* Warn about loss of qualifers from pointers to arrays with
9053 : qualifiers on the element type. */
9054 2019477 : if (TREE_CODE (ttr) == ARRAY_TYPE)
9055 : {
9056 2131 : ttr = strip_array_types (ttr);
9057 2131 : ttl = strip_array_types (ttl);
9058 :
9059 2131 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9060 2131 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
9061 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
9062 : location, expr_loc,
9063 : OPT_Wdiscarded_array_qualifiers,
9064 : G_("passing argument %d of %qE discards "
9065 : "%qv qualifier from pointer target type"),
9066 : G_("assignment discards %qv qualifier "
9067 : "from pointer target type"),
9068 : G_("initialization discards %qv qualifier "
9069 : "from pointer target type"),
9070 : G_("return discards %qv qualifier from "
9071 : "pointer target type"),
9072 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9073 : }
9074 2017346 : else if (pedantic
9075 146268 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
9076 146248 : ||
9077 : (VOID_TYPE_P (ttr)
9078 2915 : && !null_pointer_constant
9079 176 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
9080 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
9081 : G_("ISO C forbids passing argument %d of "
9082 : "%qE between function pointer "
9083 : "and %<void *%>"),
9084 : G_("ISO C forbids assignment between "
9085 : "function pointer and %<void *%>"),
9086 : G_("ISO C forbids initialization between "
9087 : "function pointer and %<void *%>"),
9088 : G_("ISO C forbids return between function "
9089 : "pointer and %<void *%>"));
9090 : /* Const and volatile mean something different for function types,
9091 : so the usual warnings are not appropriate. */
9092 2017312 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
9093 1984878 : && TREE_CODE (ttl) != FUNCTION_TYPE)
9094 : {
9095 : /* Assignments between atomic and non-atomic objects are OK. */
9096 1984143 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9097 1984143 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
9098 1984143 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
9099 1984143 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
9100 :
9101 : /* Don't warn about loss of qualifier for conversions from
9102 : qualified void* to pointers to arrays with corresponding
9103 : qualifier on the element type (except for pedantic before C23). */
9104 1984143 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
9105 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9106 : OPT_Wdiscarded_qualifiers,
9107 : G_("passing argument %d of %qE discards "
9108 : "%qv qualifier from pointer target type"),
9109 : G_("assignment discards %qv qualifier "
9110 : "from pointer target type"),
9111 : G_("initialization discards %qv qualifier "
9112 : "from pointer target type"),
9113 : G_("return discards %qv qualifier from "
9114 : "pointer target type"),
9115 : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
9116 11 : else if (warn_quals_ped)
9117 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
9118 : "array with qualifier on the element is not qualified before C23");
9119 :
9120 : /* If this is not a case of ignoring a mismatch in signedness,
9121 : no warning. */
9122 1983435 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
9123 1546564 : || target_cmp)
9124 : ;
9125 : /* If there is a mismatch, do warn. */
9126 1675 : else if (warn_pointer_sign)
9127 82 : switch (errtype)
9128 : {
9129 33 : case ic_argpass:
9130 33 : {
9131 33 : auto_diagnostic_group d;
9132 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
9133 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
9134 33 : highlight_colors::actual);
9135 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
9136 : "pointer targets in passing argument %d of "
9137 : "%qE differ in signedness", parmnum, rname))
9138 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
9139 : rhstype);
9140 33 : }
9141 33 : break;
9142 21 : case ic_assign:
9143 21 : pedwarn (location, OPT_Wpointer_sign,
9144 : "pointer targets in assignment from %qT to %qT "
9145 : "differ in signedness", rhstype, type);
9146 21 : break;
9147 14 : case ic_init:
9148 14 : case ic_init_const:
9149 14 : pedwarn_init (location, OPT_Wpointer_sign,
9150 : "pointer targets in initialization of %qT "
9151 : "from %qT differ in signedness", type,
9152 : rhstype);
9153 14 : break;
9154 14 : case ic_return:
9155 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
9156 : "returning %qT from a function with return type "
9157 : "%qT differ in signedness", rhstype, type);
9158 14 : break;
9159 0 : default:
9160 0 : gcc_unreachable ();
9161 : }
9162 : }
9163 33169 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
9164 4187 : && TREE_CODE (ttr) == FUNCTION_TYPE)
9165 : {
9166 : /* Because const and volatile on functions are restrictions
9167 : that say the function will not do certain things,
9168 : it is okay to use a const or volatile function
9169 : where an ordinary one is wanted, but not vice-versa. */
9170 3452 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
9171 3452 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
9172 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
9173 : OPT_Wdiscarded_qualifiers,
9174 : G_("passing argument %d of %qE makes "
9175 : "%q#v qualified function pointer "
9176 : "from unqualified"),
9177 : G_("assignment makes %q#v qualified function "
9178 : "pointer from unqualified"),
9179 : G_("initialization makes %q#v qualified "
9180 : "function pointer from unqualified"),
9181 : G_("return makes %q#v qualified function "
9182 : "pointer from unqualified"),
9183 : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
9184 : }
9185 : }
9186 : /* Avoid warning about the volatile ObjC EH puts on decls. */
9187 799 : else if (!objc_ok)
9188 : {
9189 799 : auto_diagnostic_group d;
9190 799 : bool warned = false;
9191 799 : pp_markup::element_expected_type e_type (type);
9192 799 : pp_markup::element_actual_type e_rhstype (rhstype);
9193 799 : switch (errtype)
9194 : {
9195 306 : case ic_argpass:
9196 306 : {
9197 306 : range_label_for_type_mismatch rhs_label (rhstype, type);
9198 306 : gcc_rich_location richloc (expr_loc, &rhs_label,
9199 306 : highlight_colors::actual);
9200 306 : warned
9201 306 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9202 : "passing argument %d of %qE from "
9203 : "incompatible pointer type",
9204 : parmnum, rname);
9205 306 : if (warned)
9206 174 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9207 306 : }
9208 306 : break;
9209 267 : case ic_assign:
9210 267 : if (bltin)
9211 11 : warned
9212 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9213 : "assignment to %e from pointer to "
9214 : "%qD with incompatible type %e",
9215 : &e_type, bltin, &e_rhstype);
9216 : else
9217 256 : warned
9218 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9219 : "assignment to %e from incompatible "
9220 : "pointer type %e",
9221 : &e_type, &e_rhstype);
9222 : break;
9223 167 : case ic_init:
9224 167 : case ic_init_const:
9225 167 : if (bltin)
9226 13 : warned
9227 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9228 : "initialization of %e from pointer to "
9229 : "%qD with incompatible type %e",
9230 : &e_type, bltin, &e_rhstype);
9231 : else
9232 154 : warned
9233 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9234 : "initialization of %e from incompatible "
9235 : "pointer type %e",
9236 : &e_type, &e_rhstype);
9237 : break;
9238 59 : case ic_return:
9239 59 : if (bltin)
9240 11 : warned
9241 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9242 : "returning pointer to %qD of type %e from "
9243 : "a function with incompatible type %e",
9244 : bltin, &e_rhstype, &e_type);
9245 : else
9246 48 : warned
9247 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9248 : "returning %e from a function with "
9249 : "incompatible return type %e",
9250 : &e_rhstype, &e_type);
9251 : break;
9252 0 : default:
9253 0 : gcc_unreachable ();
9254 : }
9255 799 : if (warned)
9256 : {
9257 : /* If the mismatching function type is a pointer to a function,
9258 : try to show the decl of the function. */
9259 589 : if (TREE_CODE (rhs) == ADDR_EXPR
9260 589 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9261 : {
9262 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9263 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9264 : {
9265 165 : gcc_rich_location richloc
9266 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9267 165 : highlight_colors::actual);
9268 165 : pp_element_quoted_decl e_rhs_fndecl
9269 165 : (rhs_fndecl, highlight_colors::actual);
9270 165 : inform (&richloc,
9271 : "%e declared here", &e_rhs_fndecl);
9272 165 : }
9273 : }
9274 : /* If either/both of the types are typedefs, show the decl. */
9275 589 : maybe_inform_typedef_location (type,
9276 : highlight_colors::expected);
9277 589 : maybe_inform_typedef_location (rhstype,
9278 : highlight_colors::actual);
9279 : }
9280 799 : }
9281 :
9282 : /* If RHS isn't an address, check pointer or array of packed
9283 : struct or union. */
9284 2020276 : warn_for_address_of_packed_member (type, orig_rhs);
9285 :
9286 2020276 : return convert (type, rhs);
9287 : }
9288 79835 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9289 : {
9290 : /* ??? This should not be an error when inlining calls to
9291 : unprototyped functions. */
9292 4 : const char msg[] = "invalid use of non-lvalue array";
9293 4 : if (warnopt)
9294 0 : warning_at (location, warnopt, msg);
9295 : else
9296 4 : error_at (location, msg);
9297 4 : return error_mark_node;
9298 : }
9299 79831 : else if (codel == POINTER_TYPE
9300 78301 : && (coder == INTEGER_TYPE
9301 78301 : || coder == ENUMERAL_TYPE
9302 616 : || coder == BOOLEAN_TYPE
9303 616 : || coder == NULLPTR_TYPE
9304 27 : || coder == BITINT_TYPE))
9305 : {
9306 78288 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9307 77290 : && coder != NULLPTR_TYPE)
9308 76730 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9309 : "zero as null pointer constant");
9310 : /* A NULLPTR type is just a nullptr always. */
9311 77728 : if (coder == NULLPTR_TYPE)
9312 575 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9313 : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9314 : or one that results from arithmetic, even including a cast to
9315 : integer type. */
9316 77713 : else if (!null_pointer_constant)
9317 972 : switch (errtype)
9318 : {
9319 800 : case ic_argpass:
9320 800 : {
9321 800 : auto_diagnostic_group d;
9322 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9323 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9324 800 : highlight_colors::actual);
9325 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9326 : "passing argument %d of %qE makes pointer "
9327 : "from integer without a cast", parmnum, rname))
9328 : {
9329 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9330 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9331 : }
9332 800 : }
9333 800 : break;
9334 93 : case ic_assign:
9335 93 : permerror_opt (location, OPT_Wint_conversion,
9336 : "assignment to %qT from %qT makes pointer from "
9337 : "integer without a cast", type, rhstype);
9338 93 : break;
9339 56 : case ic_init:
9340 56 : case ic_init_const:
9341 56 : permerror_init (location, OPT_Wint_conversion,
9342 : "initialization of %qT from %qT makes pointer "
9343 : "from integer without a cast", type, rhstype);
9344 56 : break;
9345 23 : case ic_return:
9346 23 : permerror_init (location, OPT_Wint_conversion,
9347 : "returning %qT from a function with return type "
9348 : "%qT makes pointer from integer without a cast",
9349 : rhstype, type);
9350 23 : break;
9351 0 : default:
9352 0 : gcc_unreachable ();
9353 : }
9354 :
9355 77713 : return convert (type, rhs);
9356 : }
9357 1543 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9358 540 : && coder == POINTER_TYPE)
9359 : {
9360 514 : switch (errtype)
9361 : {
9362 344 : case ic_argpass:
9363 344 : {
9364 344 : auto_diagnostic_group d;
9365 344 : range_label_for_type_mismatch rhs_label (rhstype, type);
9366 344 : gcc_rich_location richloc (expr_loc, &rhs_label,
9367 344 : highlight_colors::actual);
9368 344 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9369 : "passing argument %d of %qE makes integer from "
9370 : "pointer without a cast", parmnum, rname))
9371 : {
9372 323 : maybe_emit_indirection_note (expr_loc, rhs, type);
9373 323 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9374 : }
9375 344 : }
9376 344 : break;
9377 52 : case ic_assign:
9378 52 : permerror_opt (location, OPT_Wint_conversion,
9379 : "assignment to %qT from %qT makes integer from "
9380 : "pointer without a cast", type, rhstype);
9381 52 : break;
9382 83 : case ic_init:
9383 83 : case ic_init_const:
9384 83 : permerror_init (location, OPT_Wint_conversion,
9385 : "initialization of %qT from %qT makes integer "
9386 : "from pointer without a cast", type, rhstype);
9387 83 : break;
9388 35 : case ic_return:
9389 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9390 : "function with return type %qT makes integer from "
9391 : "pointer without a cast", rhstype, type);
9392 35 : break;
9393 0 : default:
9394 0 : gcc_unreachable ();
9395 : }
9396 :
9397 514 : return convert (type, rhs);
9398 : }
9399 626 : else if (C_BOOLEAN_TYPE_P (type)
9400 : /* The type nullptr_t may be converted to bool. The
9401 : result is false. */
9402 1031 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9403 : {
9404 405 : tree ret;
9405 405 : bool save = in_late_binary_op;
9406 405 : in_late_binary_op = true;
9407 405 : ret = convert (type, rhs);
9408 405 : in_late_binary_op = save;
9409 405 : return ret;
9410 : }
9411 624 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9412 6 : return convert (type, rhs);
9413 :
9414 618 : switch (errtype)
9415 : {
9416 35 : case ic_argpass:
9417 35 : {
9418 35 : auto_diagnostic_group d;
9419 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9420 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9421 35 : highlight_colors::actual);
9422 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9423 35 : if (warnopt)
9424 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9425 : else
9426 27 : error_at (&richloc, msg, parmnum, rname);
9427 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9428 35 : }
9429 35 : break;
9430 108 : case ic_assign:
9431 108 : {
9432 108 : const char msg[]
9433 : = G_("incompatible types when assigning to type %qT from type %qT");
9434 108 : if (warnopt)
9435 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9436 : else
9437 108 : error_at (expr_loc, msg, type, rhstype);
9438 108 : break;
9439 : }
9440 460 : case ic_init:
9441 460 : case ic_init_const:
9442 460 : {
9443 460 : const char msg[]
9444 : = G_("incompatible types when initializing type %qT using type %qT");
9445 460 : if (warnopt)
9446 0 : warning_at (location, 0, msg, type, rhstype);
9447 : else
9448 460 : error_at (location, msg, type, rhstype);
9449 460 : break;
9450 : }
9451 15 : case ic_return:
9452 15 : {
9453 15 : const char msg[]
9454 : = G_("incompatible types when returning type %qT but %qT was expected");
9455 15 : if (warnopt)
9456 0 : warning_at (location, 0, msg, rhstype, type);
9457 : else
9458 15 : error_at (location, msg, rhstype, type);
9459 15 : break;
9460 : }
9461 0 : default:
9462 0 : gcc_unreachable ();
9463 : }
9464 :
9465 618 : return error_mark_node;
9466 : }
9467 :
9468 : /* If VALUE is a compound expr all of whose expressions are constant, then
9469 : return its value. Otherwise, return error_mark_node.
9470 :
9471 : This is for handling COMPOUND_EXPRs as initializer elements
9472 : which is allowed with a warning when -pedantic is specified. */
9473 :
9474 : static tree
9475 2 : valid_compound_expr_initializer (tree value, tree endtype)
9476 : {
9477 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9478 : {
9479 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9480 1 : == error_mark_node)
9481 : return error_mark_node;
9482 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9483 0 : endtype);
9484 : }
9485 1 : else if (!initializer_constant_valid_p (value, endtype))
9486 1 : return error_mark_node;
9487 : else
9488 : return value;
9489 : }
9490 :
9491 : /* Perform appropriate conversions on the initial value of a variable,
9492 : store it in the declaration DECL,
9493 : and print any error messages that are appropriate.
9494 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9495 : If the init is invalid, store an ERROR_MARK.
9496 :
9497 : INIT_LOC is the location of the initial value. */
9498 :
9499 : void
9500 7279473 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9501 : {
9502 7279473 : tree value, type;
9503 7279473 : bool npc = false;
9504 7279473 : bool int_const_expr = false;
9505 7279473 : bool arith_const_expr = false;
9506 :
9507 : /* If variable's type was invalidly declared, just ignore it. */
9508 :
9509 7279473 : type = TREE_TYPE (decl);
9510 7279473 : if (TREE_CODE (type) == ERROR_MARK)
9511 : return;
9512 :
9513 : /* Digest the specified initializer into an expression. */
9514 :
9515 7279465 : if (init)
9516 : {
9517 7279462 : npc = null_pointer_constant_p (init);
9518 7279462 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9519 421187 : && !TREE_OVERFLOW (init)
9520 7700613 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9521 : /* Not fully determined before folding. */
9522 : arith_const_expr = true;
9523 : }
9524 7279465 : bool constexpr_p = (VAR_P (decl)
9525 7279465 : && C_DECL_DECLARED_CONSTEXPR (decl));
9526 7279465 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9527 : int_const_expr, arith_const_expr, true,
9528 7279465 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9529 :
9530 : /* Store the expression if valid; else report error. */
9531 :
9532 7279465 : if (!in_system_header_at (input_location)
9533 7279465 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9534 29004 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9535 : "aggregate initialization");
9536 :
9537 7279465 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9538 7279464 : DECL_INITIAL (decl) = value;
9539 :
9540 : /* ANSI wants warnings about out-of-range constant initializers. */
9541 7279476 : STRIP_TYPE_NOPS (value);
9542 7279465 : if (TREE_STATIC (decl))
9543 180768 : constant_expression_warning (value);
9544 :
9545 : /* Check if we need to set array size from compound literal size. */
9546 7279465 : if (TREE_CODE (type) == ARRAY_TYPE
9547 26643 : && TYPE_DOMAIN (type) == NULL_TREE
9548 7292052 : && value != error_mark_node)
9549 : {
9550 : tree inside_init = init;
9551 :
9552 11715 : STRIP_TYPE_NOPS (inside_init);
9553 11715 : inside_init = fold (inside_init);
9554 :
9555 11715 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9556 : {
9557 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9558 :
9559 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9560 : {
9561 : /* For int foo[] = (int [3]){1}; we need to set array size
9562 : now since later on array initializer will be just the
9563 : brace enclosed list of the compound literal. */
9564 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9565 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9566 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9567 13 : layout_type (type);
9568 13 : layout_decl (cldecl, 0);
9569 13 : TREE_TYPE (decl)
9570 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9571 : }
9572 : }
9573 : }
9574 : }
9575 :
9576 : /* Methods for storing and printing names for error messages. */
9577 :
9578 : /* Implement a spelling stack that allows components of a name to be pushed
9579 : and popped. Each element on the stack is this structure. */
9580 :
9581 : struct spelling
9582 : {
9583 : int kind;
9584 : union
9585 : {
9586 : unsigned HOST_WIDE_INT i;
9587 : const char *s;
9588 : } u;
9589 : };
9590 :
9591 : #define SPELLING_STRING 1
9592 : #define SPELLING_MEMBER 2
9593 : #define SPELLING_BOUNDS 3
9594 :
9595 : static struct spelling *spelling; /* Next stack element (unused). */
9596 : static struct spelling *spelling_base; /* Spelling stack base. */
9597 : static int spelling_size; /* Size of the spelling stack. */
9598 :
9599 : /* Macros to save and restore the spelling stack around push_... functions.
9600 : Alternative to SAVE_SPELLING_STACK. */
9601 :
9602 : #define SPELLING_DEPTH() (spelling - spelling_base)
9603 : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9604 :
9605 : /* Push an element on the spelling stack with type KIND and assign VALUE
9606 : to MEMBER. */
9607 :
9608 : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9609 : { \
9610 : int depth = SPELLING_DEPTH (); \
9611 : \
9612 : if (depth >= spelling_size) \
9613 : { \
9614 : spelling_size += 10; \
9615 : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9616 : spelling_size); \
9617 : RESTORE_SPELLING_DEPTH (depth); \
9618 : } \
9619 : \
9620 : spelling->kind = (KIND); \
9621 : spelling->MEMBER = (VALUE); \
9622 : spelling++; \
9623 : }
9624 :
9625 : /* Push STRING on the stack. Printed literally. */
9626 :
9627 : static void
9628 7276675 : push_string (const char *string)
9629 : {
9630 7276675 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9631 7276675 : }
9632 :
9633 : /* Push a member name on the stack. Printed as '.' STRING. */
9634 :
9635 : static void
9636 1230987 : push_member_name (tree decl)
9637 : {
9638 1230987 : const char *const string
9639 1230987 : = (DECL_NAME (decl)
9640 1230987 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9641 210 : : _("<anonymous>"));
9642 1230987 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9643 1230987 : }
9644 :
9645 : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9646 :
9647 : static void
9648 2605667 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9649 : {
9650 2605667 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9651 2605667 : }
9652 :
9653 : /* Compute the maximum size in bytes of the printed spelling. */
9654 :
9655 : static int
9656 2892 : spelling_length (void)
9657 : {
9658 2892 : int size = 0;
9659 2892 : struct spelling *p;
9660 :
9661 6416 : for (p = spelling_base; p < spelling; p++)
9662 : {
9663 3524 : if (p->kind == SPELLING_BOUNDS)
9664 1479 : size += 25;
9665 : else
9666 2045 : size += strlen (p->u.s) + 1;
9667 : }
9668 :
9669 2892 : return size;
9670 : }
9671 :
9672 : /* Print the spelling to BUFFER and return it. */
9673 :
9674 : static char *
9675 2892 : print_spelling (char *buffer)
9676 : {
9677 2892 : char *d = buffer;
9678 2892 : struct spelling *p;
9679 :
9680 6416 : for (p = spelling_base; p < spelling; p++)
9681 3524 : if (p->kind == SPELLING_BOUNDS)
9682 : {
9683 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9684 1479 : d += strlen (d);
9685 : }
9686 : else
9687 : {
9688 2045 : const char *s;
9689 2045 : if (p->kind == SPELLING_MEMBER)
9690 198 : *d++ = '.';
9691 16715 : for (s = p->u.s; (*d = *s++); d++)
9692 : ;
9693 : }
9694 2892 : *d++ = '\0';
9695 2892 : return buffer;
9696 : }
9697 :
9698 : /* Check whether INIT, a floating or integer constant, is
9699 : representable in TYPE, a real floating type with the same radix or
9700 : a decimal floating type initialized with a binary floating
9701 : constant. Return true if OK, false if not. */
9702 : static bool
9703 362 : constexpr_init_fits_real_type (tree type, tree init)
9704 : {
9705 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9706 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9707 362 : if (TREE_CODE (init) == REAL_CST
9708 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9709 : {
9710 : /* Same mode, no conversion required except for the case of
9711 : signaling NaNs if the types are incompatible (e.g. double and
9712 : long double with the same mode). */
9713 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9714 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9715 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9716 : return false;
9717 177 : return true;
9718 : }
9719 185 : if (TREE_CODE (init) == INTEGER_CST)
9720 : {
9721 54 : tree converted = build_real_from_int_cst (type, init);
9722 54 : bool fail = false;
9723 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9724 54 : TYPE_PRECISION (TREE_TYPE (init)));
9725 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9726 54 : }
9727 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9728 : return false;
9729 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9730 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9731 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9732 34 : && MODE_HAS_NANS (TYPE_MODE (type))))
9733 20 : return true;
9734 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9735 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9736 : {
9737 : /* This is valid if the real number represented by the
9738 : initializer can be exactly represented in the decimal
9739 : type. Compare the values using MPFR. */
9740 8 : REAL_VALUE_TYPE t;
9741 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9742 8 : mpfr_t bin_val, dec_val;
9743 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9744 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9745 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9746 8 : char string[256];
9747 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9748 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9749 8 : && mpfr_equal_p (bin_val, dec_val));
9750 8 : mpfr_clear (bin_val);
9751 8 : mpfr_clear (dec_val);
9752 8 : return res;
9753 : }
9754 : /* exact_real_truncate is not quite right here, since it doesn't
9755 : allow even an exact conversion to subnormal values. */
9756 86 : REAL_VALUE_TYPE t;
9757 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9758 86 : return real_identical (&t, &TREE_REAL_CST (init));
9759 : }
9760 :
9761 : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9762 : initializer for type TYPE, and give an error if not. INIT has
9763 : already been folded and verified to be constant. INT_CONST_EXPR
9764 : and ARITH_CONST_EXPR say whether it is an integer constant
9765 : expression or arithmetic constant expression, respectively. If
9766 : TYPE is not a scalar type, this function does nothing. */
9767 :
9768 : static void
9769 916 : check_constexpr_init (location_t loc, tree type, tree init,
9770 : bool int_const_expr, bool arith_const_expr)
9771 : {
9772 916 : if (POINTER_TYPE_P (type))
9773 : {
9774 : /* The initializer must be null. */
9775 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9776 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9777 86 : return;
9778 : }
9779 830 : if (INTEGRAL_TYPE_P (type))
9780 : {
9781 : /* The initializer must be an integer constant expression,
9782 : representable in the target type. */
9783 320 : if (!int_const_expr)
9784 : {
9785 13 : if (TREE_CODE (init) == RAW_DATA_CST
9786 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9787 : {
9788 4 : if (!TYPE_UNSIGNED (type))
9789 137 : for (unsigned int i = 0;
9790 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9791 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9792 : {
9793 1 : error_at (loc, "%<constexpr%> initializer not "
9794 : "representable in type of object");
9795 1 : break;
9796 : }
9797 : }
9798 : else
9799 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9800 : "integer constant expression");
9801 : }
9802 307 : else if (!int_fits_type_p (init, type))
9803 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9804 : "type of object");
9805 320 : return;
9806 : }
9807 : /* We don't apply any extra checks to extension types such as vector
9808 : or fixed-point types. */
9809 510 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9810 : return;
9811 353 : if (!arith_const_expr)
9812 : {
9813 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9814 : "constant expression");
9815 5 : return;
9816 : }
9817 : /* We don't apply any extra checks to complex integers. */
9818 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9819 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9820 : return;
9821 : /* Following N3082, a real type cannot be initialized from a complex
9822 : type and a binary type cannot be initialized from a decimal type
9823 : (but initializing a decimal type from a binary type is OK).
9824 : Signaling NaN initializers are OK only if the types are
9825 : compatible (not just the same mode); all quiet NaN and infinity
9826 : initializations are considered to preserve the value. */
9827 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9828 348 : && SCALAR_FLOAT_TYPE_P (type))
9829 : {
9830 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9831 : "complex type");
9832 6 : return;
9833 : }
9834 342 : if (SCALAR_FLOAT_TYPE_P (type)
9835 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9836 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9837 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9838 : {
9839 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9840 : "floating-point type is of decimal type");
9841 6 : return;
9842 : }
9843 336 : bool fits;
9844 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9845 : {
9846 42 : switch (TREE_CODE (init))
9847 : {
9848 10 : case INTEGER_CST:
9849 10 : case REAL_CST:
9850 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9851 10 : break;
9852 32 : case COMPLEX_CST:
9853 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9854 32 : TREE_REALPART (init))
9855 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9856 26 : TREE_IMAGPART (init)));
9857 : break;
9858 0 : default:
9859 0 : gcc_unreachable ();
9860 : }
9861 : }
9862 : else
9863 294 : fits = constexpr_init_fits_real_type (type, init);
9864 304 : if (!fits)
9865 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9866 : "type of object");
9867 : }
9868 :
9869 : /* Digest the parser output INIT as an initializer for type TYPE
9870 : initializing DECL.
9871 : Return a C expression of type TYPE to represent the initial value.
9872 :
9873 : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9874 :
9875 : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9876 : INT_CONST_EXPR is true if INIT is an integer constant expression,
9877 : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9878 : constant expression, false if it has already been determined in the
9879 : caller that it is not (but folding may have made the value passed here
9880 : indistinguishable from an arithmetic constant expression).
9881 :
9882 : If INIT is a string constant, STRICT_STRING is true if it is
9883 : unparenthesized or we should not warn here for it being parenthesized.
9884 : For other types of INIT, STRICT_STRING is not used.
9885 :
9886 : INIT_LOC is the location of the INIT.
9887 :
9888 : REQUIRE_CONSTANT requests an error if non-constant initializers or
9889 : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9890 : on initializers for 'constexpr' objects apply. */
9891 :
9892 : static tree
9893 16916342 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9894 : tree origtype, bool null_pointer_constant, bool int_const_expr,
9895 : bool arith_const_expr, bool strict_string,
9896 : bool require_constant, bool require_constexpr)
9897 : {
9898 16916342 : enum tree_code code = TREE_CODE (type);
9899 16916342 : tree inside_init = init;
9900 16916342 : tree semantic_type = NULL_TREE;
9901 16916342 : bool maybe_const = true;
9902 :
9903 16916342 : if (type == error_mark_node
9904 16916342 : || !init
9905 33832681 : || error_operand_p (init))
9906 : return error_mark_node;
9907 :
9908 16923780 : STRIP_TYPE_NOPS (inside_init);
9909 :
9910 : /* If require_constant is TRUE, when the initializer is a call to
9911 : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9912 : For example:
9913 : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9914 : will be converted to
9915 : y = &static_annotated.c. */
9916 :
9917 16915207 : if (require_constant
9918 2895161 : && TREE_CODE (inside_init) == NOP_EXPR
9919 736263 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9920 16915209 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9921 2 : inside_init
9922 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9923 :
9924 16915207 : if (!c_in_omp_for)
9925 : {
9926 16910455 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9927 : {
9928 441 : semantic_type = TREE_TYPE (inside_init);
9929 441 : inside_init = TREE_OPERAND (inside_init, 0);
9930 : }
9931 16910455 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9932 : }
9933 : /* TODO: this may not detect all cases of expressions folding to
9934 : constants that are not arithmetic constant expressions. */
9935 16915207 : if (!maybe_const)
9936 : arith_const_expr = false;
9937 25244615 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9938 5662155 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9939 16849570 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9940 : arith_const_expr = false;
9941 8423609 : else if (TREE_CODE (inside_init) != INTEGER_CST
9942 : && TREE_CODE (inside_init) != REAL_CST
9943 : && TREE_CODE (inside_init) != COMPLEX_CST
9944 : && TREE_CODE (inside_init) != RAW_DATA_CST)
9945 : arith_const_expr = false;
9946 5081702 : else if (TREE_OVERFLOW (inside_init))
9947 11833549 : arith_const_expr = false;
9948 :
9949 : /* Initialization of an array of chars from a string constant
9950 : optionally enclosed in braces. */
9951 :
9952 16915207 : if (code == ARRAY_TYPE && inside_init
9953 189589 : && TREE_CODE (inside_init) == STRING_CST)
9954 : {
9955 11797 : tree typ1
9956 11797 : = (TYPE_ATOMIC (TREE_TYPE (type))
9957 11797 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9958 : TYPE_QUAL_ATOMIC)
9959 11783 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9960 : /* Note that an array could be both an array of character type
9961 : and an array of wchar_t if wchar_t is signed char or unsigned
9962 : char. */
9963 23594 : bool char_array = (typ1 == char_type_node
9964 516 : || typ1 == signed_char_type_node
9965 12278 : || typ1 == unsigned_char_type_node);
9966 11797 : bool wchar_array = comptypes (typ1, wchar_type_node);
9967 11797 : bool char16_array = comptypes (typ1, char16_type_node);
9968 11797 : bool char32_array = comptypes (typ1, char32_type_node);
9969 :
9970 11797 : if (char_array || wchar_array || char16_array || char32_array)
9971 : {
9972 11768 : struct c_expr expr;
9973 11768 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9974 11768 : bool incompat_string_cst = false;
9975 11768 : expr.value = inside_init;
9976 11768 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9977 11768 : expr.original_type = NULL;
9978 11768 : expr.m_decimal = 0;
9979 11768 : maybe_warn_string_init (init_loc, type, expr);
9980 :
9981 11768 : if (flexible_array_member_type_p (type))
9982 61 : pedwarn_init (init_loc, OPT_Wpedantic,
9983 : "initialization of a flexible array member");
9984 :
9985 11768 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9986 11768 : TYPE_MAIN_VARIANT (type)))
9987 : return inside_init;
9988 :
9989 4186 : if (char_array)
9990 : {
9991 4075 : if (typ2 != char_type_node && typ2 != char8_type_node)
9992 : incompat_string_cst = true;
9993 : }
9994 111 : else if (!comptypes (typ1, typ2))
9995 : incompat_string_cst = true;
9996 :
9997 : if (incompat_string_cst)
9998 : {
9999 72 : error_init (init_loc, "cannot initialize array of %qT from "
10000 : "a string literal with type array of %qT",
10001 : typ1, typ2);
10002 72 : return error_mark_node;
10003 : }
10004 :
10005 4114 : if (require_constexpr
10006 4114 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
10007 : {
10008 : /* Check if all characters of the string can be
10009 : represented in the type of the constexpr object being
10010 : initialized. */
10011 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
10012 24 : const unsigned char *p =
10013 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
10014 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
10015 70 : for (unsigned i = 0; i < len; i++)
10016 58 : if (p[i] > 127)
10017 : {
10018 12 : error_init (init_loc, "%<constexpr%> initializer not "
10019 : "representable in type of object");
10020 12 : break;
10021 : }
10022 : }
10023 :
10024 4114 : if (TYPE_DOMAIN (type) != NULL_TREE
10025 4020 : && TYPE_SIZE (type) != NULL_TREE
10026 8075 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10027 : {
10028 3961 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
10029 :
10030 3961 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
10031 : {
10032 397 : unsigned HOST_WIDE_INT avail
10033 397 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10034 397 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
10035 397 : const char *p = TREE_STRING_POINTER (inside_init);
10036 :
10037 : /* Construct truncated string. */
10038 397 : inside_init = build_string (avail, p);
10039 :
10040 : /* Subtract the size of a single (possibly wide) character
10041 : because it may be ok to ignore the terminating NUL char
10042 : that is counted in the length of the constant. */
10043 397 : if (len - unit > avail)
10044 63 : pedwarn_init (init_loc, 0,
10045 : "initializer-string for array of %qT "
10046 : "is too long (%wu chars into %wu "
10047 : "available)", typ1, len, avail);
10048 334 : else if (warn_cxx_compat)
10049 9 : warning_at (init_loc, OPT_Wc___compat,
10050 : "initializer-string for array of %qT "
10051 : "is too long for C++ (%wu chars into %wu "
10052 : "available)", typ1, len, avail);
10053 325 : else if (warn_unterminated_string_initialization
10054 325 : && get_attr_nonstring_decl (decl) == NULL_TREE)
10055 22 : warning_at (init_loc,
10056 22 : OPT_Wunterminated_string_initialization,
10057 : "initializer-string for array of %qT "
10058 : "truncates NUL terminator but destination "
10059 : "lacks %qs attribute (%wu chars into %wu "
10060 : "available)", typ1, "nonstring", len, avail);
10061 : }
10062 : }
10063 :
10064 4114 : TREE_TYPE (inside_init) = type;
10065 4114 : return inside_init;
10066 : }
10067 29 : else if (INTEGRAL_TYPE_P (typ1))
10068 : {
10069 29 : error_init (init_loc, "array of inappropriate type initialized "
10070 : "from string constant");
10071 29 : return error_mark_node;
10072 : }
10073 : }
10074 :
10075 : /* Build a VECTOR_CST from a *constant* vector constructor. If the
10076 : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
10077 : below and handle as a constructor. */
10078 16903410 : if (code == VECTOR_TYPE
10079 6314990 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
10080 6314988 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
10081 23218360 : && TREE_CONSTANT (inside_init))
10082 : {
10083 633630 : if (TREE_CODE (inside_init) == VECTOR_CST
10084 633727 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10085 97 : TYPE_MAIN_VARIANT (type)))
10086 : return inside_init;
10087 :
10088 633533 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
10089 : {
10090 : unsigned HOST_WIDE_INT ix;
10091 : tree value;
10092 4175887 : bool constant_p = true;
10093 :
10094 : /* Iterate through elements and check if all constructor
10095 : elements are *_CSTs. */
10096 4175887 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
10097 3542410 : if (!CONSTANT_CLASS_P (value))
10098 : {
10099 : constant_p = false;
10100 : break;
10101 : }
10102 :
10103 633533 : if (constant_p)
10104 633477 : return build_vector_from_ctor (type,
10105 633477 : CONSTRUCTOR_ELTS (inside_init));
10106 : }
10107 : }
10108 :
10109 16269836 : if (warn_sequence_point)
10110 2449612 : verify_sequence_points (inside_init);
10111 :
10112 : /* Any type can be initialized
10113 : from an expression of the same type, optionally with braces. */
10114 :
10115 16269836 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
10116 32539672 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
10117 16269836 : TYPE_MAIN_VARIANT (type))
10118 3137429 : || (code == ARRAY_TYPE
10119 477 : && comptypes (TREE_TYPE (inside_init), type))
10120 3137429 : || (gnu_vector_type_p (type)
10121 189 : && comptypes (TREE_TYPE (inside_init), type))
10122 3137429 : || (code == POINTER_TYPE
10123 112734 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
10124 9296 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
10125 9296 : TREE_TYPE (type)))))
10126 : {
10127 13134385 : if (code == POINTER_TYPE)
10128 : {
10129 785290 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
10130 : {
10131 1978 : if (TREE_CODE (inside_init) == STRING_CST
10132 68 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10133 1975 : inside_init = array_to_pointer_conversion
10134 1975 : (init_loc, inside_init);
10135 : else
10136 : {
10137 3 : error_init (init_loc, "invalid use of non-lvalue array");
10138 3 : return error_mark_node;
10139 : }
10140 : }
10141 : }
10142 :
10143 13134382 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
10144 : /* Although the types are compatible, we may require a
10145 : conversion. */
10146 5681360 : inside_init = convert (type, inside_init);
10147 :
10148 13134382 : if ((code == RECORD_TYPE || code == UNION_TYPE)
10149 13134382 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
10150 : {
10151 0 : error_init (init_loc, "invalid initializer");
10152 0 : return error_mark_node;
10153 : }
10154 :
10155 13134382 : if (require_constant
10156 2298238 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
10157 : {
10158 : /* As an extension, allow initializing objects with static storage
10159 : duration with compound literals (which are then treated just as
10160 : the brace enclosed list they contain). Also allow this for
10161 : vectors, as we can only assign them with compound literals. */
10162 31 : if (flag_isoc99 && code != VECTOR_TYPE)
10163 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
10164 : "is not constant");
10165 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
10166 31 : inside_init = DECL_INITIAL (decl);
10167 : }
10168 :
10169 13134382 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
10170 177315 : && TREE_CODE (inside_init) != CONSTRUCTOR)
10171 : {
10172 2 : error_init (init_loc, "array initialized from non-constant array "
10173 : "expression");
10174 2 : return error_mark_node;
10175 : }
10176 :
10177 : /* Compound expressions can only occur here if -Wpedantic or
10178 : -pedantic-errors is specified. In the later case, we always want
10179 : an error. In the former case, we simply want a warning. */
10180 13134380 : if (require_constant && pedantic
10181 20308 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
10182 : {
10183 1 : inside_init
10184 1 : = valid_compound_expr_initializer (inside_init,
10185 1 : TREE_TYPE (inside_init));
10186 1 : if (inside_init == error_mark_node)
10187 1 : error_init (init_loc, "initializer element is not constant");
10188 : else
10189 0 : pedwarn_init (init_loc, OPT_Wpedantic,
10190 : "initializer element is not constant");
10191 1 : if (flag_pedantic_errors)
10192 0 : inside_init = error_mark_node;
10193 : }
10194 2298237 : else if (require_constant
10195 2298237 : && !initializer_constant_valid_p (inside_init,
10196 2298237 : TREE_TYPE (inside_init)))
10197 : {
10198 94 : error_init (init_loc, "initializer element is not constant");
10199 94 : inside_init = error_mark_node;
10200 : }
10201 13134285 : else if (require_constant && !maybe_const)
10202 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10203 : "initializer element is not a constant expression");
10204 13134066 : else if (require_constexpr)
10205 584 : check_constexpr_init (init_loc, type, inside_init,
10206 : int_const_expr, arith_const_expr);
10207 :
10208 : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10209 13134380 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10210 854830 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10211 : type, inside_init, origtype,
10212 : (require_constant
10213 : ? ic_init_const
10214 : : ic_init), null_pointer_constant,
10215 : NULL_TREE, NULL_TREE, 0);
10216 13134380 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10217 258 : && c_inhibit_evaluation_warnings == 0
10218 258 : && warn_conversion
10219 1 : && !TYPE_UNSIGNED (type)
10220 13134381 : && TYPE_PRECISION (type) == CHAR_BIT)
10221 303 : for (unsigned int i = 0;
10222 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10223 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10224 10 : warning_at (init_loc, OPT_Wconversion,
10225 : "conversion from %qT to %qT changes value from "
10226 : "%qd to %qd",
10227 : integer_type_node, type,
10228 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10229 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10230 13134380 : return inside_init;
10231 : }
10232 :
10233 : /* Handle scalar types, including conversions. */
10234 :
10235 3135451 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10236 129571 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10237 11785 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10238 9722 : || code == BITINT_TYPE)
10239 : {
10240 3134963 : tree unconverted_init = inside_init;
10241 3134963 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10242 3134963 : && (TREE_CODE (init) == STRING_CST
10243 2 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10244 7319 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10245 3134963 : if (semantic_type)
10246 436 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10247 : inside_init);
10248 3134963 : inside_init
10249 5687810 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10250 : inside_init, origtype,
10251 : require_constant ? ic_init_const : ic_init,
10252 : null_pointer_constant, NULL_TREE, NULL_TREE,
10253 : 0);
10254 :
10255 : /* Check to see if we have already given an error message. */
10256 3134963 : if (inside_init == error_mark_node)
10257 : ;
10258 3134503 : else if (require_constant && !TREE_CONSTANT (inside_init))
10259 : {
10260 36 : error_init (init_loc, "initializer element is not constant");
10261 36 : inside_init = error_mark_node;
10262 : }
10263 3134467 : else if (require_constant
10264 3716155 : && !initializer_constant_valid_p (inside_init,
10265 581688 : TREE_TYPE (inside_init)))
10266 : {
10267 10 : error_init (init_loc, "initializer element is not computable at "
10268 : "load time");
10269 10 : inside_init = error_mark_node;
10270 : }
10271 3134457 : else if (require_constant && !maybe_const)
10272 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10273 : "initializer element is not a constant expression");
10274 3134452 : else if (require_constexpr)
10275 332 : check_constexpr_init (init_loc, type, unconverted_init,
10276 : int_const_expr, arith_const_expr);
10277 :
10278 3134963 : return inside_init;
10279 : }
10280 :
10281 : /* Come here only for records and arrays. */
10282 :
10283 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10284 : {
10285 0 : error_init (init_loc,
10286 : "variable-sized object may not be initialized except "
10287 : "with an empty initializer");
10288 0 : return error_mark_node;
10289 : }
10290 :
10291 488 : error_init (init_loc, "invalid initializer");
10292 488 : return error_mark_node;
10293 : }
10294 :
10295 : /* Handle initializers that use braces. */
10296 :
10297 : /* Type of object we are accumulating a constructor for.
10298 : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10299 : static tree constructor_type;
10300 :
10301 : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10302 : left to fill. */
10303 : static tree constructor_fields;
10304 :
10305 : /* For an ARRAY_TYPE, this is the specified index
10306 : at which to store the next element we get. */
10307 : static tree constructor_index;
10308 :
10309 : /* For an ARRAY_TYPE, this is the maximum index. */
10310 : static tree constructor_max_index;
10311 :
10312 : /* For a RECORD_TYPE, this is the first field not yet written out. */
10313 : static tree constructor_unfilled_fields;
10314 :
10315 : /* For an ARRAY_TYPE, this is the index of the first element
10316 : not yet written out. */
10317 : static tree constructor_unfilled_index;
10318 :
10319 : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10320 : This is so we can generate gaps between fields, when appropriate. */
10321 : static tree constructor_bit_index;
10322 :
10323 : /* If we are saving up the elements rather than allocating them,
10324 : this is the list of elements so far (in reverse order,
10325 : most recent first). */
10326 : static vec<constructor_elt, va_gc> *constructor_elements;
10327 :
10328 : /* 1 if constructor should be incrementally stored into a constructor chain,
10329 : 0 if all the elements should be kept in AVL tree. */
10330 : static int constructor_incremental;
10331 :
10332 : /* 1 if so far this constructor's elements are all compile-time constants. */
10333 : static int constructor_constant;
10334 :
10335 : /* 1 if so far this constructor's elements are all valid address constants. */
10336 : static int constructor_simple;
10337 :
10338 : /* 1 if this constructor has an element that cannot be part of a
10339 : constant expression. */
10340 : static int constructor_nonconst;
10341 :
10342 : /* 1 if this constructor is erroneous so far. */
10343 : static int constructor_erroneous;
10344 :
10345 : /* 1 if this constructor is the universal zero initializer { 0 }. */
10346 : static int constructor_zeroinit;
10347 :
10348 : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10349 : static bool constructor_zero_padding_bits;
10350 :
10351 : /* 1 if this constructor is a braced scalar initializer (further nested levels
10352 : of braces are an error). */
10353 : static bool constructor_braced_scalar;
10354 :
10355 : /* Structure for managing pending initializer elements, organized as an
10356 : AVL tree. */
10357 :
10358 : struct init_node
10359 : {
10360 : struct init_node *left, *right;
10361 : struct init_node *parent;
10362 : int balance;
10363 : tree purpose;
10364 : tree value;
10365 : tree origtype;
10366 : };
10367 :
10368 : /* Tree of pending elements at this constructor level.
10369 : These are elements encountered out of order
10370 : which belong at places we haven't reached yet in actually
10371 : writing the output.
10372 : Will never hold tree nodes across GC runs. */
10373 : static struct init_node *constructor_pending_elts;
10374 :
10375 : /* The SPELLING_DEPTH of this constructor. */
10376 : static int constructor_depth;
10377 :
10378 : /* DECL node for which an initializer is being read.
10379 : 0 means we are reading a constructor expression
10380 : such as (struct foo) {...}. */
10381 : static tree constructor_decl;
10382 :
10383 : /* Nonzero if there were any member designators in this initializer. */
10384 : static int constructor_designated;
10385 :
10386 : /* Nesting depth of designator list. */
10387 : static int designator_depth;
10388 :
10389 : /* Nonzero if there were diagnosed errors in this designator list. */
10390 : static int designator_erroneous;
10391 :
10392 :
10393 : /* This stack has a level for each implicit or explicit level of
10394 : structuring in the initializer, including the outermost one. It
10395 : saves the values of most of the variables above. */
10396 :
10397 : struct constructor_range_stack;
10398 :
10399 : struct constructor_stack
10400 : {
10401 : struct constructor_stack *next;
10402 : tree type;
10403 : tree fields;
10404 : tree index;
10405 : tree max_index;
10406 : tree unfilled_index;
10407 : tree unfilled_fields;
10408 : tree bit_index;
10409 : vec<constructor_elt, va_gc> *elements;
10410 : struct init_node *pending_elts;
10411 : int offset;
10412 : int depth;
10413 : /* If value nonzero, this value should replace the entire
10414 : constructor at this level. */
10415 : struct c_expr replacement_value;
10416 : struct constructor_range_stack *range_stack;
10417 : char constant;
10418 : char simple;
10419 : char nonconst;
10420 : char implicit;
10421 : char erroneous;
10422 : char outer;
10423 : char incremental;
10424 : char designated;
10425 : bool zero_padding_bits;
10426 : bool braced_scalar;
10427 : int designator_depth;
10428 : };
10429 :
10430 : static struct constructor_stack *constructor_stack;
10431 :
10432 : /* This stack represents designators from some range designator up to
10433 : the last designator in the list. */
10434 :
10435 : struct constructor_range_stack
10436 : {
10437 : struct constructor_range_stack *next, *prev;
10438 : struct constructor_stack *stack;
10439 : tree range_start;
10440 : tree index;
10441 : tree range_end;
10442 : tree fields;
10443 : };
10444 :
10445 : static struct constructor_range_stack *constructor_range_stack;
10446 :
10447 : /* This stack records separate initializers that are nested.
10448 : Nested initializers can't happen in ANSI C, but GNU C allows them
10449 : in cases like { ... (struct foo) { ... } ... }. */
10450 :
10451 : struct initializer_stack
10452 : {
10453 : struct initializer_stack *next;
10454 : tree decl;
10455 : struct constructor_stack *constructor_stack;
10456 : struct constructor_range_stack *constructor_range_stack;
10457 : vec<constructor_elt, va_gc> *elements;
10458 : struct spelling *spelling;
10459 : struct spelling *spelling_base;
10460 : int spelling_size;
10461 : char require_constant_value;
10462 : char require_constant_elements;
10463 : char require_constexpr_value;
10464 : char designated;
10465 : rich_location *missing_brace_richloc;
10466 : };
10467 :
10468 : static struct initializer_stack *initializer_stack;
10469 :
10470 : /* Prepare to parse and output the initializer for variable DECL. */
10471 :
10472 : void
10473 7276675 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10474 : bool init_require_constant, bool init_require_constexpr,
10475 : rich_location *richloc)
10476 : {
10477 7276675 : const char *locus;
10478 7276675 : struct initializer_stack *p = XNEW (struct initializer_stack);
10479 :
10480 7276675 : p->decl = constructor_decl;
10481 7276675 : p->require_constant_value = require_constant_value;
10482 7276675 : p->require_constant_elements = require_constant_elements;
10483 7276675 : p->require_constexpr_value = require_constexpr_value;
10484 7276675 : p->constructor_stack = constructor_stack;
10485 7276675 : p->constructor_range_stack = constructor_range_stack;
10486 7276675 : p->elements = constructor_elements;
10487 7276675 : p->spelling = spelling;
10488 7276675 : p->spelling_base = spelling_base;
10489 7276675 : p->spelling_size = spelling_size;
10490 7276675 : p->next = initializer_stack;
10491 7276675 : p->missing_brace_richloc = richloc;
10492 7276675 : p->designated = constructor_designated;
10493 7276675 : initializer_stack = p;
10494 :
10495 7276675 : constructor_decl = decl;
10496 7276675 : constructor_designated = 0;
10497 :
10498 7276675 : require_constant_value = init_require_constant;
10499 7276675 : require_constexpr_value = init_require_constexpr;
10500 7276675 : if (decl != NULL_TREE && decl != error_mark_node)
10501 : {
10502 6354427 : require_constant_elements
10503 6176877 : = ((init_require_constant || (pedantic && !flag_isoc99))
10504 : /* For a scalar, you can always use any value to initialize,
10505 : even within braces. */
10506 6366392 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10507 6354427 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10508 : }
10509 : else
10510 : {
10511 922248 : require_constant_elements = false;
10512 922248 : locus = _("(anonymous)");
10513 : }
10514 :
10515 7276675 : constructor_stack = 0;
10516 7276675 : constructor_range_stack = 0;
10517 :
10518 7276675 : found_missing_braces = 0;
10519 :
10520 7276675 : spelling_base = 0;
10521 7276675 : spelling_size = 0;
10522 7276675 : RESTORE_SPELLING_DEPTH (0);
10523 :
10524 7276675 : if (locus)
10525 7276675 : push_string (locus);
10526 7276675 : }
10527 :
10528 : void
10529 7276673 : finish_init (void)
10530 : {
10531 7276673 : struct initializer_stack *p = initializer_stack;
10532 :
10533 : /* Free the whole constructor stack of this initializer. */
10534 7276673 : while (constructor_stack)
10535 : {
10536 0 : struct constructor_stack *q = constructor_stack;
10537 0 : constructor_stack = q->next;
10538 0 : XDELETE (q);
10539 : }
10540 :
10541 7276673 : gcc_assert (!constructor_range_stack);
10542 :
10543 : /* Pop back to the data of the outer initializer (if any). */
10544 7276673 : XDELETE (spelling_base);
10545 :
10546 7276673 : constructor_decl = p->decl;
10547 7276673 : require_constant_value = p->require_constant_value;
10548 7276673 : require_constant_elements = p->require_constant_elements;
10549 7276673 : require_constexpr_value = p->require_constexpr_value;
10550 7276673 : constructor_stack = p->constructor_stack;
10551 7276673 : constructor_designated = p->designated;
10552 7276673 : constructor_range_stack = p->constructor_range_stack;
10553 7276673 : constructor_elements = p->elements;
10554 7276673 : spelling = p->spelling;
10555 7276673 : spelling_base = p->spelling_base;
10556 7276673 : spelling_size = p->spelling_size;
10557 7276673 : initializer_stack = p->next;
10558 7276673 : XDELETE (p);
10559 7276673 : }
10560 :
10561 : /* Call here when we see the initializer is surrounded by braces.
10562 : This is instead of a call to push_init_level;
10563 : it is matched by a call to pop_init_level.
10564 :
10565 : TYPE is the type to initialize, for a constructor expression.
10566 : For an initializer for a decl, TYPE is zero. */
10567 :
10568 : void
10569 1026705 : really_start_incremental_init (tree type)
10570 : {
10571 1026705 : struct constructor_stack *p = XNEW (struct constructor_stack);
10572 :
10573 1026705 : if (type == NULL_TREE)
10574 106423 : type = TREE_TYPE (constructor_decl);
10575 :
10576 1026705 : if (VECTOR_TYPE_P (type)
10577 1026705 : && TYPE_VECTOR_OPAQUE (type))
10578 0 : error ("opaque vector types cannot be initialized");
10579 :
10580 1026705 : p->type = constructor_type;
10581 1026705 : p->fields = constructor_fields;
10582 1026705 : p->index = constructor_index;
10583 1026705 : p->max_index = constructor_max_index;
10584 1026705 : p->unfilled_index = constructor_unfilled_index;
10585 1026705 : p->unfilled_fields = constructor_unfilled_fields;
10586 1026705 : p->bit_index = constructor_bit_index;
10587 1026705 : p->elements = constructor_elements;
10588 1026705 : p->constant = constructor_constant;
10589 1026705 : p->simple = constructor_simple;
10590 1026705 : p->nonconst = constructor_nonconst;
10591 1026705 : p->erroneous = constructor_erroneous;
10592 1026705 : p->pending_elts = constructor_pending_elts;
10593 1026705 : p->depth = constructor_depth;
10594 1026705 : p->replacement_value.value = 0;
10595 1026705 : p->replacement_value.original_code = ERROR_MARK;
10596 1026705 : p->replacement_value.original_type = NULL;
10597 1026705 : p->implicit = 0;
10598 1026705 : p->range_stack = 0;
10599 1026705 : p->outer = 0;
10600 1026705 : p->incremental = constructor_incremental;
10601 1026705 : p->designated = constructor_designated;
10602 1026705 : p->zero_padding_bits = constructor_zero_padding_bits;
10603 1026705 : p->braced_scalar = constructor_braced_scalar;
10604 1026705 : p->designator_depth = designator_depth;
10605 1026705 : p->next = 0;
10606 1026705 : constructor_stack = p;
10607 :
10608 1026705 : constructor_constant = 1;
10609 1026705 : constructor_simple = 1;
10610 1026705 : constructor_nonconst = 0;
10611 1026705 : constructor_depth = SPELLING_DEPTH ();
10612 1026705 : constructor_elements = NULL;
10613 1026705 : constructor_pending_elts = 0;
10614 1026705 : constructor_type = type;
10615 1026705 : constructor_incremental = 1;
10616 1026705 : constructor_designated = 0;
10617 1026705 : constructor_zero_padding_bits = false;
10618 1026705 : constructor_zeroinit = 1;
10619 1026705 : constructor_braced_scalar = false;
10620 1026705 : designator_depth = 0;
10621 1026705 : designator_erroneous = 0;
10622 :
10623 1026705 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10624 : {
10625 82170 : constructor_fields = TYPE_FIELDS (constructor_type);
10626 : /* Skip any nameless bit fields at the beginning. */
10627 82170 : while (constructor_fields != NULL_TREE
10628 82198 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10629 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10630 :
10631 82170 : constructor_unfilled_fields = constructor_fields;
10632 82170 : constructor_bit_index = bitsize_zero_node;
10633 : }
10634 944535 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10635 : {
10636 18091 : if (TYPE_DOMAIN (constructor_type))
10637 : {
10638 9590 : constructor_max_index
10639 9590 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10640 :
10641 : /* Detect non-empty initializations of zero-length arrays. */
10642 9590 : if (constructor_max_index == NULL_TREE
10643 9590 : && TYPE_SIZE (constructor_type))
10644 207 : constructor_max_index = integer_minus_one_node;
10645 :
10646 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10647 : to initialize VLAs with a nonempty initializer will cause a
10648 : proper error; avoid tree checking errors as well by setting a
10649 : safe value. */
10650 9590 : if (constructor_max_index
10651 9590 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10652 73 : constructor_max_index = integer_minus_one_node;
10653 :
10654 9590 : constructor_index
10655 9590 : = convert (bitsizetype,
10656 9590 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10657 : }
10658 : else
10659 : {
10660 8501 : constructor_index = bitsize_zero_node;
10661 8501 : constructor_max_index = NULL_TREE;
10662 : }
10663 :
10664 18091 : constructor_unfilled_index = constructor_index;
10665 : }
10666 926444 : else if (gnu_vector_type_p (constructor_type))
10667 : {
10668 : /* Vectors are like simple fixed-size arrays. */
10669 1851776 : constructor_max_index =
10670 925888 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10671 925888 : constructor_index = bitsize_zero_node;
10672 925888 : constructor_unfilled_index = constructor_index;
10673 : }
10674 : else
10675 : {
10676 : /* Handle the case of int x = {5}; */
10677 556 : constructor_fields = constructor_type;
10678 556 : constructor_unfilled_fields = constructor_type;
10679 556 : constructor_braced_scalar = true;
10680 : }
10681 1026705 : }
10682 :
10683 : extern location_t last_init_list_comma;
10684 :
10685 : /* Called when we see an open brace for a nested initializer. Finish
10686 : off any pending levels with implicit braces. */
10687 : void
10688 342424 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10689 : {
10690 342426 : while (constructor_stack->implicit)
10691 : {
10692 1154 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10693 208 : && constructor_fields == NULL_TREE)
10694 0 : process_init_element (input_location,
10695 : pop_init_level (loc, 1, braced_init_obstack,
10696 : last_init_list_comma),
10697 : true, braced_init_obstack);
10698 1154 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10699 946 : && constructor_max_index
10700 2100 : && tree_int_cst_lt (constructor_max_index,
10701 : constructor_index))
10702 2 : process_init_element (input_location,
10703 : pop_init_level (loc, 1, braced_init_obstack,
10704 : last_init_list_comma),
10705 : true, braced_init_obstack);
10706 : else
10707 : break;
10708 : }
10709 342424 : }
10710 :
10711 : /* Push down into a subobject, for initialization.
10712 : If this is for an explicit set of braces, IMPLICIT is 0.
10713 : If it is because the next element belongs at a lower level,
10714 : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10715 :
10716 : void
10717 1045104 : push_init_level (location_t loc, int implicit,
10718 : struct obstack *braced_init_obstack)
10719 : {
10720 1045104 : struct constructor_stack *p;
10721 1045104 : tree value = NULL_TREE;
10722 :
10723 : /* Unless this is an explicit brace, we need to preserve previous
10724 : content if any. */
10725 1045104 : if (implicit)
10726 : {
10727 705323 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10728 1819 : value = find_init_member (constructor_fields, braced_init_obstack);
10729 703504 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10730 703504 : value = find_init_member (constructor_index, braced_init_obstack);
10731 : }
10732 :
10733 1045104 : p = XNEW (struct constructor_stack);
10734 1045104 : p->type = constructor_type;
10735 1045104 : p->fields = constructor_fields;
10736 1045104 : p->index = constructor_index;
10737 1045104 : p->max_index = constructor_max_index;
10738 1045104 : p->unfilled_index = constructor_unfilled_index;
10739 1045104 : p->unfilled_fields = constructor_unfilled_fields;
10740 1045104 : p->bit_index = constructor_bit_index;
10741 1045104 : p->elements = constructor_elements;
10742 1045104 : p->constant = constructor_constant;
10743 1045104 : p->simple = constructor_simple;
10744 1045104 : p->nonconst = constructor_nonconst;
10745 1045104 : p->erroneous = constructor_erroneous;
10746 1045104 : p->pending_elts = constructor_pending_elts;
10747 1045104 : p->depth = constructor_depth;
10748 1045104 : p->replacement_value.value = NULL_TREE;
10749 1045104 : p->replacement_value.original_code = ERROR_MARK;
10750 1045104 : p->replacement_value.original_type = NULL;
10751 1045104 : p->implicit = implicit;
10752 1045104 : p->outer = 0;
10753 1045104 : p->incremental = constructor_incremental;
10754 1045104 : p->designated = constructor_designated;
10755 1045104 : p->zero_padding_bits = constructor_zero_padding_bits;
10756 1045104 : p->braced_scalar = constructor_braced_scalar;
10757 1045104 : p->designator_depth = designator_depth;
10758 1045104 : p->next = constructor_stack;
10759 1045104 : p->range_stack = 0;
10760 1045104 : constructor_stack = p;
10761 :
10762 1045104 : constructor_constant = 1;
10763 1045104 : constructor_simple = 1;
10764 1045104 : constructor_nonconst = 0;
10765 1045104 : constructor_depth = SPELLING_DEPTH ();
10766 1045104 : constructor_elements = NULL;
10767 1045104 : constructor_incremental = 1;
10768 : /* If the upper initializer is designated, then mark this as
10769 : designated too to prevent bogus warnings. */
10770 1045104 : constructor_designated = p->designated;
10771 : /* If the upper initializer has padding bits zeroed, that includes
10772 : all nested initializers as well. */
10773 1045104 : constructor_zero_padding_bits = p->zero_padding_bits;
10774 1045104 : constructor_braced_scalar = false;
10775 1045104 : constructor_pending_elts = 0;
10776 1045104 : if (!implicit)
10777 : {
10778 339781 : p->range_stack = constructor_range_stack;
10779 339781 : constructor_range_stack = 0;
10780 339781 : designator_depth = 0;
10781 339781 : designator_erroneous = 0;
10782 : }
10783 :
10784 : /* Don't die if an entire brace-pair level is superfluous
10785 : in the containing level. */
10786 1045104 : if (constructor_type == NULL_TREE)
10787 : ;
10788 1045104 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10789 : {
10790 : /* Don't die if there are extra init elts at the end. */
10791 160057 : if (constructor_fields == NULL_TREE)
10792 51 : constructor_type = NULL_TREE;
10793 : else
10794 : {
10795 160006 : constructor_type = TREE_TYPE (constructor_fields);
10796 160006 : push_member_name (constructor_fields);
10797 160006 : constructor_depth++;
10798 : }
10799 : }
10800 885047 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10801 : {
10802 884996 : constructor_type = TREE_TYPE (constructor_type);
10803 884996 : push_array_bounds (tree_to_uhwi (constructor_index));
10804 884996 : constructor_depth++;
10805 : }
10806 :
10807 1045104 : if (constructor_type == NULL_TREE)
10808 : {
10809 51 : error_init (loc, "extra brace group at end of initializer");
10810 51 : constructor_fields = NULL_TREE;
10811 51 : constructor_unfilled_fields = NULL_TREE;
10812 51 : return;
10813 : }
10814 :
10815 1045053 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10816 : {
10817 1686 : constructor_constant = TREE_CONSTANT (value);
10818 1686 : constructor_simple = TREE_STATIC (value);
10819 1686 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10820 1686 : constructor_elements = CONSTRUCTOR_ELTS (value);
10821 1686 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10822 1686 : if (!vec_safe_is_empty (constructor_elements)
10823 1462 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10824 1462 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10825 1416 : set_nonincremental_init (braced_init_obstack);
10826 : }
10827 :
10828 1045053 : if (implicit == 1)
10829 : {
10830 702680 : found_missing_braces = 1;
10831 702680 : if (initializer_stack->missing_brace_richloc)
10832 702680 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10833 702680 : (loc, "{");
10834 : }
10835 :
10836 1045053 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10837 : {
10838 880845 : constructor_fields = TYPE_FIELDS (constructor_type);
10839 : /* Skip any nameless bit fields at the beginning. */
10840 880845 : while (constructor_fields != NULL_TREE
10841 880909 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10842 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10843 :
10844 880845 : constructor_unfilled_fields = constructor_fields;
10845 880845 : constructor_bit_index = bitsize_zero_node;
10846 : }
10847 164208 : else if (gnu_vector_type_p (constructor_type))
10848 : {
10849 : /* Vectors are like simple fixed-size arrays. */
10850 9602 : constructor_max_index =
10851 4801 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10852 4801 : constructor_index = bitsize_int (0);
10853 4801 : constructor_unfilled_index = constructor_index;
10854 : }
10855 159407 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10856 : {
10857 159327 : if (TYPE_DOMAIN (constructor_type))
10858 : {
10859 159327 : constructor_max_index
10860 159327 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10861 :
10862 : /* Detect non-empty initializations of zero-length arrays. */
10863 159327 : if (constructor_max_index == NULL_TREE
10864 159327 : && TYPE_SIZE (constructor_type))
10865 135 : constructor_max_index = integer_minus_one_node;
10866 :
10867 : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10868 : to initialize VLAs will cause a proper error; avoid tree
10869 : checking errors as well by setting a safe value. */
10870 159327 : if (constructor_max_index
10871 159057 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10872 2 : constructor_max_index = integer_minus_one_node;
10873 :
10874 159327 : constructor_index
10875 159327 : = convert (bitsizetype,
10876 159327 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10877 : }
10878 : else
10879 0 : constructor_index = bitsize_zero_node;
10880 :
10881 159327 : constructor_unfilled_index = constructor_index;
10882 159327 : if (value && TREE_CODE (value) == STRING_CST)
10883 : {
10884 : /* We need to split the char/wchar array into individual
10885 : characters, so that we don't have to special case it
10886 : everywhere. */
10887 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10888 : }
10889 : }
10890 : else
10891 : {
10892 80 : if (constructor_type != error_mark_node)
10893 : {
10894 51 : if (p->braced_scalar)
10895 22 : permerror_init (input_location, 0,
10896 : "braces around scalar initializer");
10897 : else
10898 29 : warning_init (input_location, 0,
10899 : "braces around scalar initializer");
10900 51 : constructor_braced_scalar = true;
10901 : }
10902 80 : constructor_fields = constructor_type;
10903 80 : constructor_unfilled_fields = constructor_type;
10904 : }
10905 : }
10906 :
10907 : /* At the end of an implicit or explicit brace level,
10908 : finish up that level of constructor. If a single expression
10909 : with redundant braces initialized that level, return the
10910 : c_expr structure for that expression. Otherwise, the original_code
10911 : element is set to ERROR_MARK.
10912 : If we were outputting the elements as they are read, return 0 as the value
10913 : from inner levels (process_init_element ignores that),
10914 : but return error_mark_node as the value from the outermost level
10915 : (that's what we want to put in DECL_INITIAL).
10916 : Otherwise, return a CONSTRUCTOR expression as the value. */
10917 :
10918 : struct c_expr
10919 2071809 : pop_init_level (location_t loc, int implicit,
10920 : struct obstack *braced_init_obstack,
10921 : location_t insert_before)
10922 : {
10923 2071809 : struct constructor_stack *p;
10924 2071809 : struct c_expr ret;
10925 2071809 : ret.value = NULL_TREE;
10926 2071809 : ret.original_code = ERROR_MARK;
10927 2071809 : ret.original_type = NULL;
10928 2071809 : ret.m_decimal = 0;
10929 :
10930 2071809 : if (implicit == 0)
10931 : {
10932 : /* When we come to an explicit close brace,
10933 : pop any inner levels that didn't have explicit braces. */
10934 1367828 : while (constructor_stack->implicit)
10935 1342 : process_init_element (input_location,
10936 : pop_init_level (loc, 1, braced_init_obstack,
10937 : insert_before),
10938 : true, braced_init_obstack);
10939 1366486 : gcc_assert (!constructor_range_stack);
10940 : }
10941 : else
10942 705323 : if (initializer_stack->missing_brace_richloc)
10943 705323 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10944 705323 : (insert_before, "}");
10945 :
10946 : /* Now output all pending elements. */
10947 2071809 : constructor_incremental = 1;
10948 2071809 : output_pending_init_elements (1, braced_init_obstack);
10949 :
10950 2071809 : p = constructor_stack;
10951 :
10952 : /* Error for initializing a flexible array member. */
10953 2071758 : if (constructor_type && constructor_fields
10954 2232830 : && flexible_array_member_type_p (constructor_type))
10955 : {
10956 : /* Silently discard empty initializations. The parser will
10957 : already have pedwarned for empty brackets for C17 and earlier. */
10958 270 : if (integer_zerop (constructor_unfilled_index))
10959 54 : constructor_type = NULL_TREE;
10960 270 : if (constructor_type || flag_isoc23)
10961 : {
10962 266 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10963 :
10964 266 : if (constructor_depth <= 2)
10965 219 : pedwarn_init (loc, OPT_Wpedantic,
10966 : "initialization of a flexible array member");
10967 47 : else if (constructor_type)
10968 33 : error_init (loc, "initialization of flexible array member "
10969 : "in a nested context");
10970 : else
10971 14 : pedwarn_init (loc, OPT_Wpedantic,
10972 : "initialization of flexible array member "
10973 : "in a nested context");
10974 :
10975 : /* We have already issued an error message for the existence
10976 : of a flexible array member not at the end of the structure.
10977 : Discard the initializer so that we do not die later. */
10978 266 : if (constructor_type
10979 216 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10980 272 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10981 0 : constructor_type = NULL_TREE;
10982 : }
10983 : }
10984 :
10985 2071809 : switch (vec_safe_length (constructor_elements))
10986 : {
10987 5096 : case 0:
10988 : /* Initialization with { } counts as zeroinit. */
10989 5096 : constructor_zeroinit = 1;
10990 5096 : break;
10991 916282 : case 1:
10992 : /* This might be zeroinit as well. */
10993 916282 : if (integer_zerop ((*constructor_elements)[0].value))
10994 2090 : constructor_zeroinit = 1;
10995 : break;
10996 1150431 : default:
10997 : /* If the constructor has more than one element, it can't be { 0 }. */
10998 1150431 : constructor_zeroinit = 0;
10999 1150431 : break;
11000 : }
11001 :
11002 : /* Warn when some structs are initialized with direct aggregation. */
11003 2071809 : if (!implicit && found_missing_braces && warn_missing_braces
11004 37 : && !constructor_zeroinit)
11005 : {
11006 18 : gcc_assert (initializer_stack->missing_brace_richloc);
11007 18 : warning_at (initializer_stack->missing_brace_richloc,
11008 18 : OPT_Wmissing_braces,
11009 : "missing braces around initializer");
11010 : }
11011 :
11012 : /* Warn when some struct elements are implicitly initialized to zero. */
11013 2071809 : if (warn_missing_field_initializers
11014 425477 : && constructor_type
11015 425463 : && TREE_CODE (constructor_type) == RECORD_TYPE
11016 193409 : && constructor_unfilled_fields)
11017 : {
11018 : /* Do not warn for flexible array members or zero-length arrays. */
11019 43 : while (constructor_unfilled_fields
11020 43 : && (!DECL_SIZE (constructor_unfilled_fields)
11021 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
11022 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
11023 :
11024 43 : if (constructor_unfilled_fields
11025 : /* Do not warn if this level of the initializer uses member
11026 : designators; it is likely to be deliberate. */
11027 43 : && !constructor_designated
11028 : /* Do not warn about initializing with { 0 } or with { }. */
11029 24 : && !constructor_zeroinit)
11030 : {
11031 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
11032 : "missing initializer for field %qD of %qT",
11033 : constructor_unfilled_fields,
11034 : constructor_type))
11035 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
11036 : "%qD declared here", constructor_unfilled_fields);
11037 : }
11038 : }
11039 :
11040 : /* Pad out the end of the structure. */
11041 2071809 : if (p->replacement_value.value)
11042 : /* If this closes a superfluous brace pair,
11043 : just pass out the element between them. */
11044 138 : ret = p->replacement_value;
11045 2071671 : else if (constructor_type == NULL_TREE)
11046 : ;
11047 2071574 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
11048 2071574 : && TREE_CODE (constructor_type) != ARRAY_TYPE
11049 2071574 : && !gnu_vector_type_p (constructor_type))
11050 : {
11051 : /* A nonincremental scalar initializer--just return
11052 : the element, after verifying there is just one.
11053 : Empty scalar initializers are supported in C23. */
11054 636 : if (vec_safe_is_empty (constructor_elements))
11055 : {
11056 201 : if (constructor_erroneous || constructor_type == error_mark_node)
11057 85 : ret.value = error_mark_node;
11058 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
11059 : {
11060 2 : error_init (loc, "invalid initializer");
11061 2 : ret.value = error_mark_node;
11062 : }
11063 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
11064 : /* Ensure this is a null pointer constant in the case of a
11065 : 'constexpr' object initialized with {}. */
11066 33 : ret.value = build_zero_cst (ptr_type_node);
11067 : else
11068 81 : ret.value = build_zero_cst (constructor_type);
11069 : }
11070 435 : else if (vec_safe_length (constructor_elements) != 1)
11071 : {
11072 0 : error_init (loc, "extra elements in scalar initializer");
11073 0 : ret.value = (*constructor_elements)[0].value;
11074 : }
11075 : else
11076 435 : ret.value = (*constructor_elements)[0].value;
11077 : }
11078 : else
11079 : {
11080 2070938 : if (constructor_erroneous)
11081 314 : ret.value = error_mark_node;
11082 : else
11083 : {
11084 2070624 : ret.value = build_constructor (constructor_type,
11085 : constructor_elements);
11086 2070624 : if (constructor_constant)
11087 1740406 : TREE_CONSTANT (ret.value) = 1;
11088 2070624 : if (constructor_constant && constructor_simple)
11089 1740367 : TREE_STATIC (ret.value) = 1;
11090 2070624 : if (constructor_nonconst)
11091 673 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
11092 2070624 : if (constructor_zero_padding_bits)
11093 687 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
11094 : }
11095 : }
11096 :
11097 2071809 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
11098 : {
11099 1088 : if (constructor_nonconst)
11100 15 : ret.original_code = C_MAYBE_CONST_EXPR;
11101 1073 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
11102 0 : ret.original_code = ERROR_MARK;
11103 : }
11104 :
11105 2071809 : constructor_type = p->type;
11106 2071809 : constructor_fields = p->fields;
11107 2071809 : constructor_index = p->index;
11108 2071809 : constructor_max_index = p->max_index;
11109 2071809 : constructor_unfilled_index = p->unfilled_index;
11110 2071809 : constructor_unfilled_fields = p->unfilled_fields;
11111 2071809 : constructor_bit_index = p->bit_index;
11112 2071809 : constructor_elements = p->elements;
11113 2071809 : constructor_constant = p->constant;
11114 2071809 : constructor_simple = p->simple;
11115 2071809 : constructor_nonconst = p->nonconst;
11116 2071809 : constructor_erroneous = p->erroneous;
11117 2071809 : constructor_incremental = p->incremental;
11118 2071809 : constructor_designated = p->designated;
11119 2071809 : constructor_zero_padding_bits = p->zero_padding_bits;
11120 2071809 : constructor_braced_scalar = p->braced_scalar;
11121 2071809 : designator_depth = p->designator_depth;
11122 2071809 : constructor_pending_elts = p->pending_elts;
11123 2071809 : constructor_depth = p->depth;
11124 2071809 : if (!p->implicit)
11125 1366486 : constructor_range_stack = p->range_stack;
11126 2071809 : RESTORE_SPELLING_DEPTH (constructor_depth);
11127 :
11128 2071809 : constructor_stack = p->next;
11129 2071809 : XDELETE (p);
11130 :
11131 2071809 : if (ret.value == NULL_TREE && constructor_stack == 0)
11132 0 : ret.value = error_mark_node;
11133 2071809 : return ret;
11134 : }
11135 :
11136 : /* Common handling for both array range and field name designators.
11137 : ARRAY argument is nonzero for array ranges. Returns false for success. */
11138 :
11139 : static bool
11140 36967 : set_designator (location_t loc, bool array,
11141 : struct obstack *braced_init_obstack)
11142 : {
11143 36967 : tree subtype;
11144 36967 : enum tree_code subcode;
11145 :
11146 : /* Don't die if an entire brace-pair level is superfluous
11147 : in the containing level, or for an erroneous type. */
11148 36967 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
11149 : return true;
11150 :
11151 : /* If there were errors in this designator list already, bail out
11152 : silently. */
11153 36958 : if (designator_erroneous)
11154 : return true;
11155 :
11156 : /* Likewise for an initializer for a variable-size type. Those are
11157 : diagnosed in the parser, except for empty initializer braces. */
11158 36958 : if (COMPLETE_TYPE_P (constructor_type)
11159 36958 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
11160 : return true;
11161 :
11162 36948 : if (!designator_depth)
11163 : {
11164 34629 : gcc_assert (!constructor_range_stack);
11165 :
11166 : /* Designator list starts at the level of closest explicit
11167 : braces. */
11168 36304 : while (constructor_stack->implicit)
11169 1675 : process_init_element (input_location,
11170 : pop_init_level (loc, 1, braced_init_obstack,
11171 : last_init_list_comma),
11172 : true, braced_init_obstack);
11173 34629 : constructor_designated = 1;
11174 34629 : return false;
11175 : }
11176 :
11177 2319 : switch (TREE_CODE (constructor_type))
11178 : {
11179 1442 : case RECORD_TYPE:
11180 1442 : case UNION_TYPE:
11181 1442 : subtype = TREE_TYPE (constructor_fields);
11182 1442 : if (subtype != error_mark_node)
11183 1442 : subtype = TYPE_MAIN_VARIANT (subtype);
11184 : break;
11185 877 : case ARRAY_TYPE:
11186 877 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
11187 877 : break;
11188 0 : default:
11189 0 : gcc_unreachable ();
11190 : }
11191 :
11192 2319 : subcode = TREE_CODE (subtype);
11193 2319 : if (array && subcode != ARRAY_TYPE)
11194 : {
11195 1 : error_init (loc, "array index in non-array initializer");
11196 1 : return true;
11197 : }
11198 2318 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11199 : {
11200 0 : error_init (loc, "field name not in record or union initializer");
11201 0 : return true;
11202 : }
11203 :
11204 2318 : constructor_designated = 1;
11205 2318 : finish_implicit_inits (loc, braced_init_obstack);
11206 2318 : push_init_level (loc, 2, braced_init_obstack);
11207 2318 : return false;
11208 : }
11209 :
11210 : /* If there are range designators in designator list, push a new designator
11211 : to constructor_range_stack. RANGE_END is end of such stack range or
11212 : NULL_TREE if there is no range designator at this level. */
11213 :
11214 : static void
11215 390 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11216 : {
11217 390 : struct constructor_range_stack *p;
11218 :
11219 780 : p = (struct constructor_range_stack *)
11220 390 : obstack_alloc (braced_init_obstack,
11221 : sizeof (struct constructor_range_stack));
11222 390 : p->prev = constructor_range_stack;
11223 390 : p->next = 0;
11224 390 : p->fields = constructor_fields;
11225 390 : p->range_start = constructor_index;
11226 390 : p->index = constructor_index;
11227 390 : p->stack = constructor_stack;
11228 390 : p->range_end = range_end;
11229 390 : if (constructor_range_stack)
11230 19 : constructor_range_stack->next = p;
11231 390 : constructor_range_stack = p;
11232 390 : }
11233 :
11234 : /* Within an array initializer, specify the next index to be initialized.
11235 : FIRST is that index. If LAST is nonzero, then initialize a range
11236 : of indices, running from FIRST through LAST. */
11237 :
11238 : void
11239 1931 : set_init_index (location_t loc, tree first, tree last,
11240 : struct obstack *braced_init_obstack)
11241 : {
11242 1931 : if (set_designator (loc, true, braced_init_obstack))
11243 : return;
11244 :
11245 1928 : designator_erroneous = 1;
11246 :
11247 3856 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11248 3845 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11249 : {
11250 12 : error_init (loc, "array index in initializer not of integer type");
11251 12 : return;
11252 : }
11253 :
11254 1916 : if (TREE_CODE (first) != INTEGER_CST)
11255 : {
11256 6 : first = c_fully_fold (first, false, NULL);
11257 6 : if (TREE_CODE (first) == INTEGER_CST)
11258 5 : pedwarn_init (loc, OPT_Wpedantic,
11259 : "array index in initializer is not "
11260 : "an integer constant expression");
11261 : }
11262 :
11263 1916 : if (last && TREE_CODE (last) != INTEGER_CST)
11264 : {
11265 2 : last = c_fully_fold (last, false, NULL);
11266 2 : if (TREE_CODE (last) == INTEGER_CST)
11267 1 : pedwarn_init (loc, OPT_Wpedantic,
11268 : "array index in initializer is not "
11269 : "an integer constant expression");
11270 : }
11271 :
11272 1916 : if (TREE_CODE (first) != INTEGER_CST)
11273 1 : error_init (loc, "nonconstant array index in initializer");
11274 1915 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11275 1 : error_init (loc, "nonconstant array index in initializer");
11276 1914 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11277 9 : error_init (loc, "array index in non-array initializer");
11278 1905 : else if (tree_int_cst_sgn (first) == -1)
11279 15 : error_init (loc, "array index in initializer exceeds array bounds");
11280 1890 : else if (constructor_max_index
11281 1890 : && tree_int_cst_lt (constructor_max_index, first))
11282 7 : error_init (loc, "array index in initializer exceeds array bounds");
11283 : else
11284 : {
11285 1883 : constant_expression_warning (first);
11286 1883 : if (last)
11287 385 : constant_expression_warning (last);
11288 1883 : constructor_index = convert (bitsizetype, first);
11289 1883 : if (tree_int_cst_lt (constructor_index, first))
11290 : {
11291 0 : constructor_index = copy_node (constructor_index);
11292 0 : TREE_OVERFLOW (constructor_index) = 1;
11293 : }
11294 :
11295 1883 : if (last)
11296 : {
11297 385 : if (tree_int_cst_equal (first, last))
11298 : last = NULL_TREE;
11299 384 : else if (tree_int_cst_lt (last, first))
11300 : {
11301 2 : error_init (loc, "empty index range in initializer");
11302 2 : last = NULL_TREE;
11303 : }
11304 : else
11305 : {
11306 382 : last = convert (bitsizetype, last);
11307 382 : if (constructor_max_index != NULL_TREE
11308 382 : && tree_int_cst_lt (constructor_max_index, last))
11309 : {
11310 3 : error_init (loc, "array index range in initializer exceeds "
11311 : "array bounds");
11312 3 : last = NULL_TREE;
11313 : }
11314 : }
11315 : }
11316 :
11317 1883 : designator_depth++;
11318 1883 : designator_erroneous = 0;
11319 1883 : if (constructor_range_stack || last)
11320 379 : push_range_stack (last, braced_init_obstack);
11321 : }
11322 : }
11323 :
11324 : /* Within a struct initializer, specify the next field to be initialized. */
11325 :
11326 : void
11327 34990 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11328 : struct obstack *braced_init_obstack)
11329 : {
11330 34990 : tree field;
11331 :
11332 34990 : if (set_designator (loc, false, braced_init_obstack))
11333 : return;
11334 :
11335 34973 : designator_erroneous = 1;
11336 :
11337 34973 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11338 : {
11339 3 : error_init (loc, "field name not in record or union initializer");
11340 3 : return;
11341 : }
11342 :
11343 34970 : field = lookup_field (constructor_type, fieldname);
11344 :
11345 34970 : if (field == NULL_TREE)
11346 : {
11347 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11348 8 : if (guessed_id)
11349 : {
11350 4 : gcc_rich_location rich_loc (fieldname_loc);
11351 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11352 4 : error_at (&rich_loc,
11353 : "%qT has no member named %qE; did you mean %qE?",
11354 : constructor_type, fieldname, guessed_id);
11355 4 : }
11356 : else
11357 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11358 : constructor_type, fieldname);
11359 : }
11360 : else
11361 35008 : do
11362 : {
11363 35008 : constructor_fields = TREE_VALUE (field);
11364 35008 : designator_depth++;
11365 35008 : designator_erroneous = 0;
11366 35008 : if (constructor_range_stack)
11367 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11368 35008 : field = TREE_CHAIN (field);
11369 35008 : if (field)
11370 : {
11371 46 : if (set_designator (loc, false, braced_init_obstack))
11372 : return;
11373 : }
11374 : }
11375 35008 : while (field != NULL_TREE);
11376 : }
11377 :
11378 : /* Helper function for add_pending_init. Find inorder successor of P
11379 : in AVL tree. */
11380 : static struct init_node *
11381 129 : init_node_successor (struct init_node *p)
11382 : {
11383 129 : struct init_node *r;
11384 129 : if (p->right)
11385 : {
11386 : r = p->right;
11387 58 : while (r->left)
11388 : r = r->left;
11389 : return r;
11390 : }
11391 75 : r = p->parent;
11392 114 : while (r && p == r->right)
11393 : {
11394 39 : p = r;
11395 39 : r = r->parent;
11396 : }
11397 : return r;
11398 : }
11399 :
11400 : /* Add a new initializer to the tree of pending initializers. PURPOSE
11401 : identifies the initializer, either array index or field in a structure.
11402 : VALUE is the value of that index or field. If ORIGTYPE is not
11403 : NULL_TREE, it is the original type of VALUE.
11404 :
11405 : IMPLICIT is true if value comes from pop_init_level (1),
11406 : the new initializer has been merged with the existing one
11407 : and thus no warnings should be emitted about overriding an
11408 : existing initializer. */
11409 :
11410 : static void
11411 7984 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11412 : bool implicit, struct obstack *braced_init_obstack)
11413 : {
11414 7984 : struct init_node *p, **q, *r;
11415 :
11416 7984 : q = &constructor_pending_elts;
11417 7984 : p = 0;
11418 :
11419 7984 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11420 : {
11421 7214 : while (*q != 0)
11422 : {
11423 4384 : p = *q;
11424 4384 : if (tree_int_cst_lt (purpose, p->purpose))
11425 419 : q = &p->left;
11426 3965 : else if (tree_int_cst_lt (p->purpose, purpose))
11427 : {
11428 3354 : if (TREE_CODE (p->value) != RAW_DATA_CST
11429 3354 : || (p->right
11430 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11431 3224 : q = &p->right;
11432 : else
11433 : {
11434 130 : widest_int pp = wi::to_widest (p->purpose);
11435 130 : widest_int pw = wi::to_widest (purpose);
11436 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11437 98 : q = &p->right;
11438 : else
11439 : {
11440 : /* Override which should split the old RAW_DATA_CST
11441 : into 2 or 3 pieces. */
11442 32 : if (!implicit && warn_override_init)
11443 9 : warning_init (loc, OPT_Woverride_init,
11444 : "initialized field overwritten");
11445 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11446 32 : unsigned HOST_WIDE_INT len = 1;
11447 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11448 0 : len = RAW_DATA_LENGTH (value);
11449 32 : unsigned HOST_WIDE_INT end = 0;
11450 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11451 32 : gcc_checking_assert (start < plen && start);
11452 32 : if (plen - start > len)
11453 30 : end = plen - start - len;
11454 32 : tree v = p->value;
11455 32 : tree origtype = p->origtype;
11456 32 : if (start == 1)
11457 2 : p->value = build_int_cst (TREE_TYPE (v),
11458 2 : RAW_DATA_UCHAR_ELT (v, 0));
11459 : else
11460 : {
11461 30 : p->value = v;
11462 30 : if (end > 1)
11463 26 : v = copy_node (v);
11464 30 : RAW_DATA_LENGTH (p->value) = start;
11465 : }
11466 32 : if (end)
11467 : {
11468 30 : tree epurpose
11469 30 : = size_binop (PLUS_EXPR, purpose,
11470 : bitsize_int (len));
11471 30 : if (end > 1)
11472 : {
11473 28 : RAW_DATA_LENGTH (v) -= plen - end;
11474 28 : RAW_DATA_POINTER (v) += plen - end;
11475 : }
11476 : else
11477 2 : v = build_int_cst (TREE_TYPE (v),
11478 2 : RAW_DATA_UCHAR_ELT (v, plen
11479 : - end));
11480 30 : add_pending_init (loc, epurpose, v, origtype,
11481 : implicit, braced_init_obstack);
11482 : }
11483 32 : q = &constructor_pending_elts;
11484 32 : continue;
11485 32 : }
11486 130 : }
11487 : }
11488 : else
11489 : {
11490 611 : if (TREE_CODE (p->value) == RAW_DATA_CST
11491 621 : && (RAW_DATA_LENGTH (p->value)
11492 10 : > (TREE_CODE (value) == RAW_DATA_CST
11493 10 : ? RAW_DATA_LENGTH (value) : 1)))
11494 : {
11495 : /* Override which should split the old RAW_DATA_CST
11496 : into 2 pieces. */
11497 6 : if (!implicit && warn_override_init)
11498 3 : warning_init (loc, OPT_Woverride_init,
11499 : "initialized field overwritten");
11500 6 : unsigned HOST_WIDE_INT len = 1;
11501 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11502 2 : len = RAW_DATA_LENGTH (value);
11503 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11504 : {
11505 6 : RAW_DATA_LENGTH (p->value) -= len;
11506 6 : RAW_DATA_POINTER (p->value) += len;
11507 : }
11508 : else
11509 : {
11510 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11511 0 : p->value
11512 0 : = build_int_cst (TREE_TYPE (p->value),
11513 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11514 : }
11515 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11516 : bitsize_int (len));
11517 6 : continue;
11518 6 : }
11519 605 : if (TREE_CODE (value) == RAW_DATA_CST)
11520 : {
11521 8 : handle_raw_data:
11522 : /* RAW_DATA_CST value might overlap various further
11523 : prior initval entries. Find out how many. */
11524 13 : unsigned cnt = 0;
11525 13 : widest_int w
11526 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11527 13 : struct init_node *r = p, *last = NULL;
11528 13 : bool override_init = warn_override_init;
11529 13 : while ((r = init_node_successor (r))
11530 53 : && wi::to_widest (r->purpose) < w)
11531 : {
11532 40 : ++cnt;
11533 40 : if (TREE_SIDE_EFFECTS (r->value))
11534 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11535 : "initialized field with side-effects "
11536 : "overwritten");
11537 38 : else if (override_init)
11538 : {
11539 6 : warning_init (loc, OPT_Woverride_init,
11540 : "initialized field overwritten");
11541 6 : override_init = false;
11542 : }
11543 : last = r;
11544 : }
11545 13 : if (cnt)
11546 : {
11547 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11548 13 : && (wi::to_widest (last->purpose)
11549 13 : + RAW_DATA_LENGTH (last->value) > w))
11550 : {
11551 : /* The last overlapping prior initval overlaps
11552 : only partially. Shrink it and decrease cnt. */
11553 0 : unsigned int l = (wi::to_widest (last->purpose)
11554 0 : + RAW_DATA_LENGTH (last->value)
11555 0 : - w).to_uhwi ();
11556 0 : --cnt;
11557 0 : RAW_DATA_LENGTH (last->value) -= l;
11558 0 : RAW_DATA_POINTER (last->value) += l;
11559 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11560 0 : last->value
11561 0 : = build_int_cst (TREE_TYPE (last->value),
11562 0 : RAW_DATA_UCHAR_ELT (last->value,
11563 : 0));
11564 0 : last->purpose
11565 0 : = size_binop (PLUS_EXPR, last->purpose,
11566 : bitsize_int (l));
11567 : }
11568 : /* Instead of deleting cnt nodes from the AVL tree
11569 : and rebalancing, peel of last cnt bytes from the
11570 : RAW_DATA_CST. Overriding thousands of previously
11571 : initialized array elements with #embed needs to work,
11572 : but doesn't need to be super efficient. */
11573 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11574 : > cnt);
11575 13 : RAW_DATA_LENGTH (value) -= cnt;
11576 13 : const unsigned char *s
11577 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11578 13 : unsigned int o = RAW_DATA_LENGTH (value);
11579 53 : for (r = p; cnt--; ++o, ++s)
11580 : {
11581 40 : r = init_node_successor (r);
11582 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11583 : bitsize_int (o));
11584 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11585 40 : r->origtype = origtype;
11586 : }
11587 13 : if (RAW_DATA_LENGTH (value) == 1)
11588 0 : value = build_int_cst (TREE_TYPE (value),
11589 0 : RAW_DATA_UCHAR_ELT (value, 0));
11590 : }
11591 : }
11592 610 : if (!implicit)
11593 : {
11594 54 : if (TREE_SIDE_EFFECTS (p->value))
11595 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11596 : "initialized field with side-effects "
11597 : "overwritten");
11598 48 : else if (warn_override_init)
11599 13 : warning_init (loc, OPT_Woverride_init,
11600 : "initialized field overwritten");
11601 : }
11602 610 : p->value = value;
11603 610 : p->origtype = origtype;
11604 610 : return;
11605 : }
11606 : }
11607 2830 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11608 : {
11609 61 : struct init_node *r;
11610 61 : if (q == &p->left)
11611 : r = p;
11612 : else
11613 36 : r = init_node_successor (p);
11614 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11615 131 : + RAW_DATA_LENGTH (value)))
11616 : {
11617 : /* Overlap with at least one prior initval in the range but
11618 : not at the start. */
11619 5 : p = r;
11620 5 : p->purpose = purpose;
11621 5 : goto handle_raw_data;
11622 : }
11623 : }
11624 : }
11625 : else
11626 : {
11627 4549 : tree bitpos;
11628 :
11629 4549 : bitpos = bit_position (purpose);
11630 12736 : while (*q != NULL)
11631 : {
11632 4881 : p = *q;
11633 4881 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11634 81 : q = &p->left;
11635 4800 : else if (p->purpose != purpose)
11636 3557 : q = &p->right;
11637 : else
11638 : {
11639 1243 : if (!implicit)
11640 : {
11641 101 : if (TREE_SIDE_EFFECTS (p->value))
11642 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11643 : "initialized field with side-effects "
11644 : "overwritten");
11645 97 : else if (warn_override_init)
11646 6 : warning_init (loc, OPT_Woverride_init,
11647 : "initialized field overwritten");
11648 : }
11649 1243 : p->value = value;
11650 1243 : p->origtype = origtype;
11651 1243 : return;
11652 : }
11653 : }
11654 : }
11655 :
11656 6131 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11657 : sizeof (struct init_node));
11658 6131 : r->purpose = purpose;
11659 6131 : r->value = value;
11660 6131 : r->origtype = origtype;
11661 :
11662 6131 : *q = r;
11663 6131 : r->parent = p;
11664 6131 : r->left = 0;
11665 6131 : r->right = 0;
11666 6131 : r->balance = 0;
11667 :
11668 9955 : while (p)
11669 : {
11670 5212 : struct init_node *s;
11671 :
11672 5212 : if (r == p->left)
11673 : {
11674 343 : if (p->balance == 0)
11675 246 : p->balance = -1;
11676 97 : else if (p->balance < 0)
11677 : {
11678 45 : if (r->balance < 0)
11679 : {
11680 : /* L rotation. */
11681 40 : p->left = r->right;
11682 40 : if (p->left)
11683 2 : p->left->parent = p;
11684 40 : r->right = p;
11685 :
11686 40 : p->balance = 0;
11687 40 : r->balance = 0;
11688 :
11689 40 : s = p->parent;
11690 40 : p->parent = r;
11691 40 : r->parent = s;
11692 40 : if (s)
11693 : {
11694 31 : if (s->left == p)
11695 10 : s->left = r;
11696 : else
11697 21 : s->right = r;
11698 : }
11699 : else
11700 9 : constructor_pending_elts = r;
11701 : }
11702 : else
11703 : {
11704 : /* LR rotation. */
11705 5 : struct init_node *t = r->right;
11706 :
11707 5 : r->right = t->left;
11708 5 : if (r->right)
11709 0 : r->right->parent = r;
11710 5 : t->left = r;
11711 :
11712 5 : p->left = t->right;
11713 5 : if (p->left)
11714 0 : p->left->parent = p;
11715 5 : t->right = p;
11716 :
11717 5 : p->balance = t->balance < 0;
11718 5 : r->balance = -(t->balance > 0);
11719 5 : t->balance = 0;
11720 :
11721 5 : s = p->parent;
11722 5 : p->parent = t;
11723 5 : r->parent = t;
11724 5 : t->parent = s;
11725 5 : if (s)
11726 : {
11727 0 : if (s->left == p)
11728 0 : s->left = t;
11729 : else
11730 0 : s->right = t;
11731 : }
11732 : else
11733 5 : constructor_pending_elts = t;
11734 : }
11735 : break;
11736 : }
11737 : else
11738 : {
11739 : /* p->balance == +1; growth of left side balances the node. */
11740 52 : p->balance = 0;
11741 52 : break;
11742 : }
11743 : }
11744 : else /* r == p->right */
11745 : {
11746 4869 : if (p->balance == 0)
11747 : /* Growth propagation from right side. */
11748 3578 : p->balance++;
11749 1291 : else if (p->balance > 0)
11750 : {
11751 1228 : if (r->balance > 0)
11752 : {
11753 : /* R rotation. */
11754 1222 : p->right = r->left;
11755 1222 : if (p->right)
11756 193 : p->right->parent = p;
11757 1222 : r->left = p;
11758 :
11759 1222 : p->balance = 0;
11760 1222 : r->balance = 0;
11761 :
11762 1222 : s = p->parent;
11763 1222 : p->parent = r;
11764 1222 : r->parent = s;
11765 1222 : if (s)
11766 : {
11767 414 : if (s->left == p)
11768 2 : s->left = r;
11769 : else
11770 412 : s->right = r;
11771 : }
11772 : else
11773 808 : constructor_pending_elts = r;
11774 : }
11775 : else /* r->balance == -1 */
11776 : {
11777 : /* RL rotation */
11778 6 : struct init_node *t = r->left;
11779 :
11780 6 : r->left = t->right;
11781 6 : if (r->left)
11782 2 : r->left->parent = r;
11783 6 : t->right = r;
11784 :
11785 6 : p->right = t->left;
11786 6 : if (p->right)
11787 2 : p->right->parent = p;
11788 6 : t->left = p;
11789 :
11790 6 : r->balance = (t->balance < 0);
11791 6 : p->balance = -(t->balance > 0);
11792 6 : t->balance = 0;
11793 :
11794 6 : s = p->parent;
11795 6 : p->parent = t;
11796 6 : r->parent = t;
11797 6 : t->parent = s;
11798 6 : if (s)
11799 : {
11800 4 : if (s->left == p)
11801 0 : s->left = t;
11802 : else
11803 4 : s->right = t;
11804 : }
11805 : else
11806 2 : constructor_pending_elts = t;
11807 : }
11808 : break;
11809 : }
11810 : else
11811 : {
11812 : /* p->balance == -1; growth of right side balances the node. */
11813 63 : p->balance = 0;
11814 63 : break;
11815 : }
11816 : }
11817 :
11818 3824 : r = p;
11819 3824 : p = p->parent;
11820 : }
11821 : }
11822 :
11823 : /* Build AVL tree from a sorted chain. */
11824 :
11825 : static void
11826 1812 : set_nonincremental_init (struct obstack * braced_init_obstack)
11827 : {
11828 1812 : unsigned HOST_WIDE_INT ix;
11829 1812 : tree index, value;
11830 :
11831 1812 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11832 1812 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11833 : return;
11834 :
11835 5511 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11836 3699 : add_pending_init (input_location, index, value, NULL_TREE, true,
11837 : braced_init_obstack);
11838 1812 : constructor_elements = NULL;
11839 1812 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11840 : {
11841 948 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11842 : /* Skip any nameless bit fields at the beginning. */
11843 948 : while (constructor_unfilled_fields != NULL_TREE
11844 948 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11845 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11846 :
11847 : }
11848 864 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11849 : {
11850 864 : if (TYPE_DOMAIN (constructor_type))
11851 806 : constructor_unfilled_index
11852 806 : = convert (bitsizetype,
11853 806 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11854 : else
11855 58 : constructor_unfilled_index = bitsize_zero_node;
11856 : }
11857 1812 : constructor_incremental = 0;
11858 : }
11859 :
11860 : /* Build AVL tree from a string constant. */
11861 :
11862 : static void
11863 7 : set_nonincremental_init_from_string (tree str,
11864 : struct obstack * braced_init_obstack)
11865 : {
11866 7 : tree value, purpose, type;
11867 7 : HOST_WIDE_INT val[2];
11868 7 : const char *p, *end;
11869 7 : int byte, wchar_bytes, charwidth, bitpos;
11870 :
11871 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11872 :
11873 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11874 7 : charwidth = TYPE_PRECISION (char_type_node);
11875 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11876 : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11877 7 : type = TREE_TYPE (constructor_type);
11878 7 : p = TREE_STRING_POINTER (str);
11879 7 : end = p + TREE_STRING_LENGTH (str);
11880 :
11881 7 : for (purpose = bitsize_zero_node;
11882 : p < end
11883 52 : && !(constructor_max_index
11884 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11885 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11886 : {
11887 25 : if (wchar_bytes == 1)
11888 : {
11889 9 : val[0] = (unsigned char) *p++;
11890 9 : val[1] = 0;
11891 : }
11892 : else
11893 : {
11894 16 : val[1] = 0;
11895 16 : val[0] = 0;
11896 64 : for (byte = 0; byte < wchar_bytes; byte++)
11897 : {
11898 48 : if (BYTES_BIG_ENDIAN)
11899 : bitpos = (wchar_bytes - byte - 1) * charwidth;
11900 : else
11901 48 : bitpos = byte * charwidth;
11902 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11903 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11904 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11905 : }
11906 : }
11907 :
11908 25 : if (!TYPE_UNSIGNED (type))
11909 : {
11910 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11911 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11912 : {
11913 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11914 : {
11915 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11916 0 : val[1] = -1;
11917 : }
11918 : }
11919 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11920 : {
11921 0 : if (val[0] < 0)
11922 0 : val[1] = -1;
11923 : }
11924 0 : else if (val[1] & (HOST_WIDE_INT_1
11925 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11926 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11927 : }
11928 :
11929 25 : value = wide_int_to_tree (type,
11930 25 : wide_int::from_array (val, 2,
11931 : HOST_BITS_PER_WIDE_INT * 2));
11932 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11933 : braced_init_obstack);
11934 : }
11935 :
11936 7 : constructor_incremental = 0;
11937 7 : }
11938 :
11939 : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11940 : not initialized yet. */
11941 :
11942 : static tree
11943 705323 : find_init_member (tree field, struct obstack * braced_init_obstack)
11944 : {
11945 705323 : struct init_node *p;
11946 :
11947 705323 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11948 : {
11949 703504 : if (constructor_incremental
11950 703504 : && tree_int_cst_lt (field, constructor_unfilled_index))
11951 16 : set_nonincremental_init (braced_init_obstack);
11952 :
11953 703504 : p = constructor_pending_elts;
11954 704103 : while (p)
11955 : {
11956 1151 : if (tree_int_cst_lt (field, p->purpose))
11957 93 : p = p->left;
11958 1058 : else if (tree_int_cst_lt (p->purpose, field))
11959 506 : p = p->right;
11960 : else
11961 552 : return p->value;
11962 : }
11963 : }
11964 1819 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11965 : {
11966 1696 : tree bitpos = bit_position (field);
11967 :
11968 1696 : if (constructor_incremental
11969 1696 : && (!constructor_unfilled_fields
11970 609 : || tree_int_cst_lt (bitpos,
11971 609 : bit_position (constructor_unfilled_fields))))
11972 289 : set_nonincremental_init (braced_init_obstack);
11973 :
11974 1696 : p = constructor_pending_elts;
11975 2652 : while (p)
11976 : {
11977 2047 : if (field == p->purpose)
11978 1091 : return p->value;
11979 956 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11980 7 : p = p->left;
11981 : else
11982 949 : p = p->right;
11983 : }
11984 : }
11985 123 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11986 : {
11987 123 : if (!vec_safe_is_empty (constructor_elements)
11988 50 : && (constructor_elements->last ().index == field))
11989 50 : return constructor_elements->last ().value;
11990 : }
11991 : return NULL_TREE;
11992 : }
11993 :
11994 : /* "Output" the next constructor element.
11995 : At top level, really output it to assembler code now.
11996 : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11997 : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11998 : TYPE is the data type that the containing data type wants here.
11999 : FIELD is the field (a FIELD_DECL) or the index that this element fills.
12000 : If VALUE is a string constant, STRICT_STRING is true if it is
12001 : unparenthesized or we should not warn here for it being parenthesized.
12002 : For other types of VALUE, STRICT_STRING is not used.
12003 :
12004 : PENDING if true means output pending elements that belong
12005 : right after this element. (PENDING is normally true;
12006 : it is false while outputting pending elements, to avoid recursion.)
12007 :
12008 : IMPLICIT is true if value comes from pop_init_level (1),
12009 : the new initializer has been merged with the existing one
12010 : and thus no warnings should be emitted about overriding an
12011 : existing initializer. */
12012 :
12013 : static void
12014 9637044 : output_init_element (location_t loc, tree value, tree origtype,
12015 : bool strict_string, tree type, tree field, bool pending,
12016 : bool implicit, struct obstack * braced_init_obstack)
12017 : {
12018 9637044 : tree semantic_type = NULL_TREE;
12019 9637044 : bool maybe_const = true;
12020 9637044 : bool npc, int_const_expr, arith_const_expr;
12021 :
12022 9637044 : if (type == error_mark_node || value == error_mark_node)
12023 : {
12024 286 : constructor_erroneous = 1;
12025 4620 : return;
12026 : }
12027 9636758 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
12028 201018 : && (TREE_CODE (value) == STRING_CST
12029 159587 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
12030 41439 : && !(TREE_CODE (value) == STRING_CST
12031 41431 : && TREE_CODE (type) == ARRAY_TYPE
12032 3898 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
12033 9674299 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
12034 37541 : TYPE_MAIN_VARIANT (type)))
12035 37541 : value = array_to_pointer_conversion (input_location, value);
12036 :
12037 9636758 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
12038 182 : && require_constant_value && pending)
12039 : {
12040 : /* As an extension, allow initializing objects with static storage
12041 : duration with compound literals (which are then treated just as
12042 : the brace enclosed list they contain). */
12043 114 : if (flag_isoc99)
12044 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
12045 : "constant");
12046 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
12047 114 : value = DECL_INITIAL (decl);
12048 : }
12049 :
12050 9636758 : npc = null_pointer_constant_p (value);
12051 19273516 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
12052 4225622 : && !TREE_OVERFLOW (value)
12053 13862379 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
12054 : /* Not fully determined before folding. */
12055 9636758 : arith_const_expr = true;
12056 9636758 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
12057 : {
12058 16 : semantic_type = TREE_TYPE (value);
12059 16 : value = TREE_OPERAND (value, 0);
12060 : }
12061 9636758 : value = c_fully_fold (value, require_constant_value, &maybe_const);
12062 : /* TODO: this may not detect all cases of expressions folding to
12063 : constants that are not arithmetic constant expressions. */
12064 9636758 : if (!maybe_const)
12065 : arith_const_expr = false;
12066 19270975 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
12067 3190087 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
12068 11481597 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
12069 : arith_const_expr = false;
12070 7804691 : else if (TREE_CODE (value) != INTEGER_CST
12071 : && TREE_CODE (value) != REAL_CST
12072 : && TREE_CODE (value) != COMPLEX_CST
12073 : && TREE_CODE (value) != RAW_DATA_CST)
12074 : arith_const_expr = false;
12075 4614770 : else if (TREE_OVERFLOW (value))
12076 5021989 : arith_const_expr = false;
12077 :
12078 9636758 : if (value == error_mark_node)
12079 0 : constructor_erroneous = 1;
12080 9636758 : else if (!TREE_CONSTANT (value))
12081 3210379 : constructor_constant = 0;
12082 6426379 : else if (!initializer_constant_valid_p (value,
12083 6426379 : TREE_TYPE (value),
12084 6426379 : AGGREGATE_TYPE_P (constructor_type)
12085 6426379 : && TYPE_REVERSE_STORAGE_ORDER
12086 : (constructor_type))
12087 6426379 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
12088 1041254 : && DECL_C_BIT_FIELD (field)
12089 7978 : && TREE_CODE (value) != INTEGER_CST))
12090 77 : constructor_simple = 0;
12091 9636758 : if (!maybe_const)
12092 1174 : constructor_nonconst = 1;
12093 :
12094 : /* Digest the initializer and issue any errors about incompatible
12095 : types before issuing errors about non-constant initializers. */
12096 9636758 : tree new_value = value;
12097 9636758 : if (semantic_type)
12098 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
12099 : /* In the case of braces around a scalar initializer, the result of
12100 : this initializer processing goes through digest_init again at the
12101 : outer level. In the case of a constexpr initializer for a
12102 : pointer, avoid converting a null pointer constant to something
12103 : that is not a null pointer constant to avoid a spurious error
12104 : from that second processing. */
12105 9636758 : if (!require_constexpr_value
12106 398 : || !npc
12107 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
12108 9636747 : new_value = digest_init (loc,
12109 9636747 : RECORD_OR_UNION_TYPE_P (constructor_type)
12110 8562421 : ? field : constructor_fields
12111 8562421 : ? constructor_fields : constructor_decl,
12112 : type, new_value, origtype, npc,
12113 : int_const_expr, arith_const_expr, strict_string,
12114 : require_constant_value, require_constexpr_value);
12115 9636758 : if (new_value == error_mark_node)
12116 : {
12117 76 : constructor_erroneous = 1;
12118 76 : return;
12119 : }
12120 9636682 : if (require_constant_value || require_constant_elements)
12121 2715214 : constant_expression_warning (new_value);
12122 :
12123 : /* Proceed to check the constness of the original initializer. */
12124 9636682 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
12125 : {
12126 3210405 : if (require_constant_value)
12127 : {
12128 0 : error_init (loc, "initializer element is not constant");
12129 0 : value = error_mark_node;
12130 : }
12131 3210405 : else if (require_constant_elements)
12132 15 : pedwarn (loc, OPT_Wpedantic,
12133 : "initializer element is not computable at load time");
12134 : }
12135 6426277 : else if (!maybe_const
12136 54 : && (require_constant_value || require_constant_elements))
12137 28 : pedwarn_init (loc, OPT_Wpedantic,
12138 : "initializer element is not a constant expression");
12139 : /* digest_init has already carried out the additional checks
12140 : required for 'constexpr' initializers (using the information
12141 : passed to it about whether the original initializer was certain
12142 : kinds of constant expression), so that check does not need to be
12143 : repeated here. */
12144 :
12145 : /* Issue -Wc++-compat warnings about initializing a bitfield with
12146 : enum type. */
12147 9636682 : if (warn_cxx_compat
12148 6900 : && field != NULL_TREE
12149 6900 : && TREE_CODE (field) == FIELD_DECL
12150 464 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
12151 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
12152 21 : != TYPE_MAIN_VARIANT (type))
12153 9636703 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
12154 : {
12155 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
12156 21 : if (checktype != error_mark_node
12157 21 : && (TYPE_MAIN_VARIANT (checktype)
12158 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
12159 9 : warning_init (loc, OPT_Wc___compat,
12160 : "enum conversion in initialization is invalid in C++");
12161 : }
12162 :
12163 : /* If this field is empty and does not have side effects (and is not at
12164 : the end of structure), don't do anything other than checking the
12165 : initializer. */
12166 9636682 : if (field
12167 9636682 : && (TREE_TYPE (field) == error_mark_node
12168 9636247 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
12169 9635938 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
12170 92 : && !TREE_SIDE_EFFECTS (new_value)
12171 63 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
12172 63 : || DECL_CHAIN (field)))))
12173 28 : return;
12174 :
12175 : /* Finally, set VALUE to the initializer value digested above. */
12176 9636654 : value = new_value;
12177 :
12178 : /* If this element doesn't come next in sequence,
12179 : put it on constructor_pending_elts. */
12180 9636654 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
12181 9636654 : && (!constructor_incremental
12182 1722313 : || !tree_int_cst_equal (field, constructor_unfilled_index)
12183 1721879 : || (TREE_CODE (value) == RAW_DATA_CST
12184 239 : && constructor_pending_elts
12185 66 : && pending)))
12186 : {
12187 1341 : if (constructor_incremental
12188 1341 : && (tree_int_cst_lt (field, constructor_unfilled_index)
12189 405 : || (TREE_CODE (value) == RAW_DATA_CST
12190 14 : && constructor_pending_elts
12191 14 : && pending)))
12192 48 : set_nonincremental_init (braced_init_obstack);
12193 :
12194 1341 : add_pending_init (loc, field, value, origtype, implicit,
12195 : braced_init_obstack);
12196 1341 : return;
12197 : }
12198 9635313 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12199 1042952 : && (!constructor_incremental
12200 1041286 : || field != constructor_unfilled_fields))
12201 : {
12202 : /* We do this for records but not for unions. In a union,
12203 : no matter which field is specified, it can be initialized
12204 : right away since it starts at the beginning of the union. */
12205 2889 : if (constructor_incremental)
12206 : {
12207 1223 : if (!constructor_unfilled_fields)
12208 39 : set_nonincremental_init (braced_init_obstack);
12209 : else
12210 : {
12211 1184 : tree bitpos, unfillpos;
12212 :
12213 1184 : bitpos = bit_position (field);
12214 1184 : unfillpos = bit_position (constructor_unfilled_fields);
12215 :
12216 1184 : if (tree_int_cst_lt (bitpos, unfillpos))
12217 4 : set_nonincremental_init (braced_init_obstack);
12218 : }
12219 : }
12220 :
12221 2889 : add_pending_init (loc, field, value, origtype, implicit,
12222 : braced_init_obstack);
12223 2889 : return;
12224 : }
12225 9632424 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12226 9632424 : && !vec_safe_is_empty (constructor_elements))
12227 : {
12228 62 : if (!implicit)
12229 : {
12230 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12231 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12232 : "initialized field with side-effects overwritten");
12233 9 : else if (warn_override_init)
12234 6 : warning_init (loc, OPT_Woverride_init,
12235 : "initialized field overwritten");
12236 : }
12237 :
12238 : /* We can have just one union field set. */
12239 62 : constructor_elements = NULL;
12240 : }
12241 :
12242 : /* Otherwise, output this element either to
12243 : constructor_elements or to the assembler file. */
12244 :
12245 9632424 : constructor_elt celt = {field, value};
12246 9632424 : vec_safe_push (constructor_elements, celt);
12247 :
12248 : /* Advance the variable that indicates sequential elements output. */
12249 9632424 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12250 : {
12251 1721874 : tree inc = bitsize_one_node;
12252 1721874 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12253 234 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12254 1721874 : constructor_unfilled_index
12255 1721874 : = size_binop_loc (input_location, PLUS_EXPR,
12256 : constructor_unfilled_index, inc);
12257 : }
12258 7910550 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12259 : {
12260 1040063 : constructor_unfilled_fields
12261 1040063 : = DECL_CHAIN (constructor_unfilled_fields);
12262 :
12263 : /* Skip any nameless bit fields. */
12264 1040063 : while (constructor_unfilled_fields != NULL_TREE
12265 1040164 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12266 101 : constructor_unfilled_fields
12267 101 : = DECL_CHAIN (constructor_unfilled_fields);
12268 : }
12269 6870487 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12270 31332 : constructor_unfilled_fields = NULL_TREE;
12271 :
12272 : /* Now output any pending elements which have become next. */
12273 9632424 : if (pending)
12274 9626238 : output_pending_init_elements (0, braced_init_obstack);
12275 : }
12276 :
12277 : /* For two FIELD_DECLs in the same chain, return -1 if field1
12278 : comes before field2, 1 if field1 comes after field2 and
12279 : 0 if field1 == field2. */
12280 :
12281 : static int
12282 7104 : init_field_decl_cmp (tree field1, tree field2)
12283 : {
12284 7104 : if (field1 == field2)
12285 : return 0;
12286 :
12287 3338 : tree bitpos1 = bit_position (field1);
12288 3338 : tree bitpos2 = bit_position (field2);
12289 3338 : if (tree_int_cst_equal (bitpos1, bitpos2))
12290 : {
12291 : /* If one of the fields has non-zero bitsize, then that
12292 : field must be the last one in a sequence of zero
12293 : sized fields, fields after it will have bigger
12294 : bit_position. */
12295 32 : if (TREE_TYPE (field1) != error_mark_node
12296 32 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12297 64 : && integer_nonzerop (TREE_TYPE (field1)))
12298 : return 1;
12299 32 : if (TREE_TYPE (field2) != error_mark_node
12300 32 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12301 57 : && integer_nonzerop (TREE_TYPE (field2)))
12302 : return -1;
12303 : /* Otherwise, fallback to DECL_CHAIN walk to find out
12304 : which field comes earlier. Walk chains of both
12305 : fields, so that if field1 and field2 are close to each
12306 : other in either order, it is found soon even for large
12307 : sequences of zero sized fields. */
12308 : tree f1 = field1, f2 = field2;
12309 32 : while (1)
12310 : {
12311 32 : f1 = DECL_CHAIN (f1);
12312 32 : f2 = DECL_CHAIN (f2);
12313 32 : if (f1 == NULL_TREE)
12314 : {
12315 7 : gcc_assert (f2);
12316 : return 1;
12317 : }
12318 25 : if (f2 == NULL_TREE)
12319 : return -1;
12320 9 : if (f1 == field2)
12321 : return -1;
12322 0 : if (f2 == field1)
12323 : return 1;
12324 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12325 : return 1;
12326 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12327 : return -1;
12328 : }
12329 : }
12330 3306 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12331 : return -1;
12332 : else
12333 : return 1;
12334 : }
12335 :
12336 : /* Output any pending elements which have become next.
12337 : As we output elements, constructor_unfilled_{fields,index}
12338 : advances, which may cause other elements to become next;
12339 : if so, they too are output.
12340 :
12341 : If ALL is 0, we return when there are
12342 : no more pending elements to output now.
12343 :
12344 : If ALL is 1, we output space as necessary so that
12345 : we can output all the pending elements. */
12346 : static void
12347 11698047 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12348 : {
12349 11698047 : struct init_node *elt = constructor_pending_elts;
12350 11699205 : tree next;
12351 :
12352 11699205 : retry:
12353 :
12354 : /* Look through the whole pending tree.
12355 : If we find an element that should be output now,
12356 : output it. Otherwise, set NEXT to the element
12357 : that comes first among those still pending. */
12358 :
12359 11699205 : next = NULL_TREE;
12360 11711378 : while (elt)
12361 : {
12362 15043 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12363 : {
12364 6812 : if (tree_int_cst_equal (elt->purpose,
12365 : constructor_unfilled_index))
12366 2829 : output_init_element (input_location, elt->value, elt->origtype,
12367 2829 : true, TREE_TYPE (constructor_type),
12368 : constructor_unfilled_index, false, false,
12369 : braced_init_obstack);
12370 3983 : else if (tree_int_cst_lt (constructor_unfilled_index,
12371 3983 : elt->purpose))
12372 : {
12373 : /* Advance to the next smaller node. */
12374 1133 : if (elt->left)
12375 : elt = elt->left;
12376 : else
12377 : {
12378 : /* We have reached the smallest node bigger than the
12379 : current unfilled index. Fill the space first. */
12380 345 : next = elt->purpose;
12381 345 : break;
12382 : }
12383 : }
12384 : else
12385 : {
12386 : /* Advance to the next bigger node. */
12387 2850 : if (elt->right)
12388 : elt = elt->right;
12389 : else
12390 : {
12391 : /* We have reached the biggest node in a subtree. Find
12392 : the parent of it, which is the next bigger node. */
12393 2850 : while (elt->parent && elt->parent->right == elt)
12394 : elt = elt->parent;
12395 1896 : elt = elt->parent;
12396 2679 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12397 783 : elt->purpose))
12398 : {
12399 22 : next = elt->purpose;
12400 22 : break;
12401 : }
12402 : }
12403 : }
12404 : }
12405 8231 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12406 : {
12407 : /* If the current record is complete we are done. */
12408 8231 : if (constructor_unfilled_fields == NULL_TREE)
12409 : break;
12410 :
12411 6628 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12412 : elt->purpose);
12413 6628 : if (cmp == 0)
12414 3357 : output_init_element (input_location, elt->value, elt->origtype,
12415 3357 : true, TREE_TYPE (elt->purpose),
12416 : elt->purpose, false, false,
12417 : braced_init_obstack);
12418 3271 : else if (cmp < 0)
12419 : {
12420 : /* Advance to the next smaller node. */
12421 1316 : if (elt->left)
12422 : elt = elt->left;
12423 : else
12424 : {
12425 : /* We have reached the smallest node bigger than the
12426 : current unfilled field. Fill the space first. */
12427 833 : next = elt->purpose;
12428 833 : break;
12429 : }
12430 : }
12431 : else
12432 : {
12433 : /* Advance to the next bigger node. */
12434 1955 : if (elt->right)
12435 : elt = elt->right;
12436 : else
12437 : {
12438 : /* We have reached the biggest node in a subtree. Find
12439 : the parent of it, which is the next bigger node. */
12440 1025 : while (elt->parent && elt->parent->right == elt)
12441 : elt = elt->parent;
12442 789 : elt = elt->parent;
12443 789 : if (elt
12444 789 : && init_field_decl_cmp (constructor_unfilled_fields,
12445 : elt->purpose) < 0)
12446 : {
12447 67 : next = elt->purpose;
12448 67 : break;
12449 : }
12450 : }
12451 : }
12452 : }
12453 : }
12454 :
12455 : /* Ordinarily return, but not if we want to output all
12456 : and there are elements left. */
12457 11699205 : if (!(all && next != NULL_TREE))
12458 11698047 : return;
12459 :
12460 : /* If it's not incremental, just skip over the gap, so that after
12461 : jumping to retry we will output the next successive element. */
12462 1158 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12463 830 : constructor_unfilled_fields = next;
12464 328 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12465 328 : constructor_unfilled_index = next;
12466 :
12467 : /* ELT now points to the node in the pending tree with the next
12468 : initializer to output. */
12469 1158 : goto retry;
12470 : }
12471 :
12472 : /* Expression VALUE coincides with the start of type TYPE in a braced
12473 : initializer. Return true if we should treat VALUE as initializing
12474 : the first element of TYPE, false if we should treat it as initializing
12475 : TYPE as a whole.
12476 :
12477 : If the initializer is clearly invalid, the question becomes:
12478 : which choice gives the best error message? */
12479 :
12480 : static bool
12481 3490625 : initialize_elementwise_p (tree type, tree value)
12482 : {
12483 3490625 : if (type == error_mark_node || value == error_mark_node)
12484 : return false;
12485 :
12486 3490360 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12487 :
12488 3490360 : tree value_type = TREE_TYPE (value);
12489 3490360 : if (value_type == error_mark_node)
12490 : return false;
12491 :
12492 : /* GNU vectors can be initialized elementwise. However, treat any
12493 : kind of vector value as initializing the vector type as a whole,
12494 : regardless of whether the value is a GNU vector. Such initializers
12495 : are valid if and only if they would have been valid in a non-braced
12496 : initializer like:
12497 :
12498 : TYPE foo = VALUE;
12499 :
12500 : so recursing into the vector type would be at best confusing or at
12501 : worst wrong. For example, when -flax-vector-conversions is in effect,
12502 : it's possible to initialize a V8HI from a V4SI, even though the vectors
12503 : have different element types and different numbers of elements. */
12504 3490360 : if (gnu_vector_type_p (type))
12505 19541 : return !VECTOR_TYPE_P (value_type);
12506 :
12507 3470819 : if (AGGREGATE_TYPE_P (type))
12508 1743246 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12509 :
12510 : return false;
12511 : }
12512 :
12513 : /* Helper function for process_init_element. Split first element of
12514 : RAW_DATA_CST and save the rest to *RAW_DATA. */
12515 :
12516 : static inline tree
12517 9630704 : maybe_split_raw_data (tree value, tree *raw_data)
12518 : {
12519 9630704 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12520 : return value;
12521 211 : *raw_data = value;
12522 211 : value = build_int_cst (integer_type_node,
12523 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12524 211 : ++RAW_DATA_POINTER (*raw_data);
12525 211 : --RAW_DATA_LENGTH (*raw_data);
12526 211 : return value;
12527 : }
12528 :
12529 : /* Return non-zero if c_parser_initval should attempt to optimize
12530 : large initializers into RAW_DATA_CST. In that case return how
12531 : many elements to optimize at most. */
12532 :
12533 : unsigned
12534 2995845 : c_maybe_optimize_large_byte_initializer (void)
12535 : {
12536 2995845 : if (!constructor_type
12537 2995775 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12538 211518 : || constructor_stack->implicit)
12539 : return 0;
12540 208753 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12541 208753 : if (TREE_CODE (elttype) != INTEGER_TYPE
12542 208753 : && TREE_CODE (elttype) != BITINT_TYPE)
12543 : return 0;
12544 185778 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12545 27955 : || constructor_stack->replacement_value.value
12546 27955 : || (COMPLETE_TYPE_P (constructor_type)
12547 26310 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12548 213733 : || constructor_range_stack)
12549 : return 0;
12550 27955 : if (constructor_max_index == NULL_TREE)
12551 : return INT_MAX;
12552 26310 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12553 26310 : || integer_all_onesp (constructor_max_index))
12554 4770 : return 0;
12555 21540 : widest_int w = wi::to_widest (constructor_max_index);
12556 21540 : w -= wi::to_widest (constructor_index);
12557 21540 : w += 1;
12558 21540 : if (w < 64)
12559 : return 0;
12560 6894 : if (w > INT_MAX)
12561 : return INT_MAX;
12562 6894 : return w.to_uhwi ();
12563 21540 : }
12564 :
12565 : /* Add one non-braced element to the current constructor level.
12566 : This adjusts the current position within the constructor's type.
12567 : This may also start or terminate implicit levels
12568 : to handle a partly-braced initializer.
12569 :
12570 : Once this has found the correct level for the new element,
12571 : it calls output_init_element.
12572 :
12573 : IMPLICIT is true if value comes from pop_init_level (1),
12574 : the new initializer has been merged with the existing one
12575 : and thus no warnings should be emitted about overriding an
12576 : existing initializer. */
12577 :
12578 : void
12579 9621002 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12580 : struct obstack * braced_init_obstack)
12581 : {
12582 9621002 : tree orig_value = value.value;
12583 19242004 : int string_flag
12584 9621002 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12585 9621002 : bool strict_string = value.original_code == STRING_CST;
12586 9621002 : bool was_designated = designator_depth != 0;
12587 9621002 : tree raw_data = NULL_TREE;
12588 :
12589 9621214 : retry:
12590 9621214 : designator_depth = 0;
12591 9621214 : designator_erroneous = 0;
12592 :
12593 9621214 : if (!implicit && value.value && !integer_zerop (value.value))
12594 7595859 : constructor_zeroinit = 0;
12595 :
12596 : /* Handle superfluous braces around string cst as in
12597 : char x[] = {"foo"}; */
12598 9621214 : if (constructor_type
12599 9621086 : && !was_designated
12600 9584194 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12601 1707833 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12602 10359512 : && integer_zerop (constructor_unfilled_index))
12603 : {
12604 163150 : if (constructor_stack->replacement_value.value)
12605 : {
12606 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12607 409 : return;
12608 : }
12609 163142 : else if (string_flag)
12610 : {
12611 138 : constructor_stack->replacement_value = value;
12612 138 : return;
12613 : }
12614 : }
12615 :
12616 9621068 : if (constructor_stack->replacement_value.value != NULL_TREE)
12617 : {
12618 0 : error_init (loc, "excess elements in struct initializer");
12619 0 : return;
12620 : }
12621 :
12622 : /* Ignore elements of a brace group if it is entirely superfluous
12623 : and has already been diagnosed, or if the type is erroneous. */
12624 9621068 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12625 : return;
12626 :
12627 : /* Ignore elements of an initializer for a variable-size type.
12628 : Those are diagnosed in the parser (empty initializer braces are OK). */
12629 9620871 : if (COMPLETE_TYPE_P (constructor_type)
12630 9620871 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12631 : return;
12632 :
12633 8915490 : if (!implicit && warn_designated_init && !was_designated
12634 8880916 : && TREE_CODE (constructor_type) == RECORD_TYPE
12635 10656010 : && lookup_attribute ("designated_init",
12636 1035197 : TYPE_ATTRIBUTES (constructor_type)))
12637 50 : warning_init (loc,
12638 : OPT_Wdesignated_init,
12639 : "positional initialization of field "
12640 : "in %<struct%> declared with %<designated_init%> attribute");
12641 :
12642 : /* If we've exhausted any levels that didn't have braces,
12643 : pop them now. */
12644 10322792 : while (constructor_stack->implicit)
12645 : {
12646 708550 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12647 703711 : && constructor_fields == NULL_TREE)
12648 701738 : process_init_element (loc,
12649 : pop_init_level (loc, 1, braced_init_obstack,
12650 : last_init_list_comma),
12651 : true, braced_init_obstack);
12652 6812 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12653 2617 : || gnu_vector_type_p (constructor_type))
12654 4839 : && constructor_max_index
12655 11624 : && tree_int_cst_lt (constructor_max_index,
12656 : constructor_index))
12657 241 : process_init_element (loc,
12658 : pop_init_level (loc, 1, braced_init_obstack,
12659 : last_init_list_comma),
12660 : true, braced_init_obstack);
12661 : else
12662 : break;
12663 : }
12664 :
12665 : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12666 9620813 : if (constructor_range_stack)
12667 : {
12668 : /* If value is a compound literal and we'll be just using its
12669 : content, don't put it into a SAVE_EXPR. */
12670 371 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12671 3 : || !require_constant_value)
12672 : {
12673 368 : tree semantic_type = NULL_TREE;
12674 368 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12675 : {
12676 0 : semantic_type = TREE_TYPE (value.value);
12677 0 : value.value = TREE_OPERAND (value.value, 0);
12678 : }
12679 368 : value.value = save_expr (value.value);
12680 368 : if (semantic_type)
12681 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12682 : value.value);
12683 : }
12684 : }
12685 :
12686 10335057 : while (1)
12687 : {
12688 10335057 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12689 : {
12690 1041315 : tree fieldtype;
12691 1041315 : enum tree_code fieldcode;
12692 :
12693 1041315 : if (constructor_fields == NULL_TREE)
12694 : {
12695 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12696 1280 : break;
12697 : }
12698 :
12699 1040035 : fieldtype = TREE_TYPE (constructor_fields);
12700 1040035 : if (fieldtype != error_mark_node)
12701 1040034 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12702 1040035 : fieldcode = TREE_CODE (fieldtype);
12703 :
12704 : /* Error for non-static initialization of a flexible array member. */
12705 1040035 : if (fieldcode == ARRAY_TYPE
12706 156026 : && !require_constant_value
12707 1892 : && TYPE_SIZE (fieldtype) == NULL_TREE
12708 1040045 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12709 : {
12710 10 : error_init (loc, "non-static initialization of a flexible "
12711 : "array member");
12712 10 : break;
12713 : }
12714 :
12715 : /* Error for initialization of a flexible array member with
12716 : a string constant if the structure is in an array. E.g.:
12717 : struct S { int x; char y[]; };
12718 : struct S s[] = { { 1, "foo" } };
12719 : is invalid. */
12720 1040025 : if (string_flag
12721 1040025 : && fieldcode == ARRAY_TYPE
12722 3353 : && constructor_depth > 1
12723 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12724 1040072 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12725 : {
12726 47 : bool in_array_p = false;
12727 47 : for (struct constructor_stack *p = constructor_stack;
12728 73 : p && p->type; p = p->next)
12729 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12730 : {
12731 : in_array_p = true;
12732 : break;
12733 : }
12734 47 : if (in_array_p)
12735 : {
12736 33 : error_init (loc, "initialization of flexible array "
12737 : "member in a nested context");
12738 33 : break;
12739 : }
12740 : }
12741 :
12742 : /* Accept a string constant to initialize a subarray. */
12743 1039992 : if (value.value != NULL_TREE
12744 1039949 : && fieldcode == ARRAY_TYPE
12745 155940 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12746 1194621 : && string_flag)
12747 : value.value = orig_value;
12748 : /* Otherwise, if we have come to a subaggregate,
12749 : and we don't have an element of its type, push into it. */
12750 1036976 : else if (value.value != NULL_TREE
12751 1036672 : && initialize_elementwise_p (fieldtype, value.value))
12752 : {
12753 304 : push_init_level (loc, 1, braced_init_obstack);
12754 304 : continue;
12755 : }
12756 :
12757 1039688 : value.value = maybe_split_raw_data (value.value, &raw_data);
12758 1039688 : if (value.value)
12759 : {
12760 1039645 : push_member_name (constructor_fields);
12761 1039645 : output_init_element (loc, value.value, value.original_type,
12762 : strict_string, fieldtype,
12763 : constructor_fields, true, implicit,
12764 : braced_init_obstack);
12765 1039645 : RESTORE_SPELLING_DEPTH (constructor_depth);
12766 : }
12767 : else
12768 : /* Do the bookkeeping for an element that was
12769 : directly output as a constructor. */
12770 : {
12771 : /* For a record, keep track of end position of last field. */
12772 43 : if (DECL_SIZE (constructor_fields))
12773 0 : constructor_bit_index
12774 0 : = size_binop_loc (input_location, PLUS_EXPR,
12775 : bit_position (constructor_fields),
12776 0 : DECL_SIZE (constructor_fields));
12777 :
12778 : /* If the current field was the first one not yet written out,
12779 : it isn't now, so update. */
12780 43 : if (constructor_unfilled_fields == constructor_fields)
12781 : {
12782 34 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12783 : /* Skip any nameless bit fields. */
12784 34 : while (constructor_unfilled_fields != 0
12785 34 : && (DECL_UNNAMED_BIT_FIELD
12786 : (constructor_unfilled_fields)))
12787 0 : constructor_unfilled_fields =
12788 0 : DECL_CHAIN (constructor_unfilled_fields);
12789 : }
12790 : }
12791 :
12792 1039688 : constructor_fields = DECL_CHAIN (constructor_fields);
12793 : /* Skip any nameless bit fields at the beginning. */
12794 1039688 : while (constructor_fields != NULL_TREE
12795 1039789 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12796 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12797 : }
12798 9293742 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12799 : {
12800 31395 : tree fieldtype;
12801 31395 : enum tree_code fieldcode;
12802 :
12803 31395 : if (constructor_fields == NULL_TREE)
12804 : {
12805 3 : pedwarn_init (loc, 0,
12806 : "excess elements in union initializer");
12807 3 : break;
12808 : }
12809 :
12810 31392 : fieldtype = TREE_TYPE (constructor_fields);
12811 31392 : if (fieldtype != error_mark_node)
12812 31392 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12813 31392 : fieldcode = TREE_CODE (fieldtype);
12814 :
12815 : /* Warn that traditional C rejects initialization of unions.
12816 : We skip the warning if the value is zero. This is done
12817 : under the assumption that the zero initializer in user
12818 : code appears conditioned on e.g. __STDC__ to avoid
12819 : "missing initializer" warnings and relies on default
12820 : initialization to zero in the traditional C case.
12821 : We also skip the warning if the initializer is designated,
12822 : again on the assumption that this must be conditional on
12823 : __STDC__ anyway (and we've already complained about the
12824 : member-designator already). */
12825 33842 : if (!in_system_header_at (input_location) && !constructor_designated
12826 32943 : && !(value.value && (integer_zerop (value.value)
12827 1444 : || real_zerop (value.value))))
12828 1437 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12829 : "of unions");
12830 :
12831 : /* Error for non-static initialization of a flexible array member. */
12832 31392 : if (fieldcode == ARRAY_TYPE
12833 766 : && !require_constant_value
12834 31449 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12835 : {
12836 3 : error_init (loc, "non-static initialization of a flexible "
12837 : "array member");
12838 3 : break;
12839 : }
12840 :
12841 : /* Error for initialization of a flexible array member with
12842 : a string constant if the structure is in an array. E.g.:
12843 : union U { int x; char y[]; };
12844 : union U s[] = { { 1, "foo" } };
12845 : is invalid. */
12846 31389 : if (string_flag
12847 31389 : && fieldcode == ARRAY_TYPE
12848 7 : && constructor_depth > 1
12849 31393 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12850 : {
12851 3 : bool in_array_p = false;
12852 3 : for (struct constructor_stack *p = constructor_stack;
12853 3 : p && p->type; p = p->next)
12854 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12855 : {
12856 : in_array_p = true;
12857 : break;
12858 : }
12859 3 : if (in_array_p)
12860 : {
12861 3 : error_init (loc, "initialization of flexible array "
12862 : "member in a nested context");
12863 3 : break;
12864 : }
12865 : }
12866 :
12867 : /* Accept a string constant to initialize a subarray. */
12868 31386 : if (value.value != NULL_TREE
12869 31386 : && fieldcode == ARRAY_TYPE
12870 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12871 32141 : && string_flag)
12872 : value.value = orig_value;
12873 : /* Otherwise, if we have come to a subaggregate,
12874 : and we don't have an element of its type, push into it. */
12875 31432 : else if (value.value != NULL_TREE
12876 31382 : && initialize_elementwise_p (fieldtype, value.value))
12877 : {
12878 50 : push_init_level (loc, 1, braced_init_obstack);
12879 50 : continue;
12880 : }
12881 :
12882 31336 : value.value = maybe_split_raw_data (value.value, &raw_data);
12883 31336 : if (value.value)
12884 : {
12885 31336 : push_member_name (constructor_fields);
12886 31336 : output_init_element (loc, value.value, value.original_type,
12887 : strict_string, fieldtype,
12888 : constructor_fields, true, implicit,
12889 : braced_init_obstack);
12890 31336 : RESTORE_SPELLING_DEPTH (constructor_depth);
12891 : }
12892 : else
12893 : /* Do the bookkeeping for an element that was
12894 : directly output as a constructor. */
12895 : {
12896 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12897 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12898 : }
12899 :
12900 31336 : constructor_fields = NULL_TREE;
12901 : }
12902 9262347 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12903 : {
12904 2423110 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12905 2423110 : enum tree_code eltcode = TREE_CODE (elttype);
12906 :
12907 : /* Accept a string constant to initialize a subarray. */
12908 2423110 : if (value.value != NULL_TREE
12909 2423110 : && eltcode == ARRAY_TYPE
12910 7031 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12911 2428658 : && string_flag)
12912 : value.value = orig_value;
12913 : /* Otherwise, if we have come to a subaggregate,
12914 : and we don't have an element of its type, push into it. */
12915 3124940 : else if (value.value != NULL_TREE
12916 2422614 : && initialize_elementwise_p (elttype, value.value))
12917 : {
12918 702326 : push_init_level (loc, 1, braced_init_obstack);
12919 702326 : continue;
12920 : }
12921 :
12922 1720784 : if (constructor_max_index != NULL_TREE
12923 677882 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12924 677767 : || integer_all_onesp (constructor_max_index))
12925 : /* For VLA we got an error already. */
12926 1720899 : && !C_TYPE_VARIABLE_SIZE (constructor_type))
12927 : {
12928 113 : pedwarn_init (loc, 0,
12929 : "excess elements in array initializer");
12930 113 : break;
12931 : }
12932 :
12933 1720671 : if (value.value
12934 1720671 : && TREE_CODE (value.value) == RAW_DATA_CST
12935 197 : && RAW_DATA_LENGTH (value.value) > 1
12936 197 : && (TREE_CODE (elttype) == INTEGER_TYPE
12937 197 : || TREE_CODE (elttype) == BITINT_TYPE)
12938 197 : && TYPE_PRECISION (elttype) == CHAR_BIT
12939 1720868 : && (constructor_max_index == NULL_TREE
12940 59 : || tree_int_cst_lt (constructor_index,
12941 : constructor_max_index)))
12942 : {
12943 197 : unsigned int len = RAW_DATA_LENGTH (value.value);
12944 197 : if (constructor_max_index)
12945 : {
12946 59 : widest_int w = wi::to_widest (constructor_max_index);
12947 59 : w -= wi::to_widest (constructor_index);
12948 59 : w += 1;
12949 59 : if (w < len)
12950 3 : len = w.to_uhwi ();
12951 59 : }
12952 197 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12953 : {
12954 3 : raw_data = copy_node (value.value);
12955 3 : RAW_DATA_LENGTH (raw_data) -= len;
12956 3 : RAW_DATA_POINTER (raw_data) += len;
12957 3 : RAW_DATA_LENGTH (value.value) = len;
12958 : }
12959 197 : TREE_TYPE (value.value) = elttype;
12960 197 : push_array_bounds (tree_to_uhwi (constructor_index));
12961 197 : output_init_element (loc, value.value, value.original_type,
12962 : false, elttype, constructor_index, true,
12963 : implicit, braced_init_obstack);
12964 197 : RESTORE_SPELLING_DEPTH (constructor_depth);
12965 197 : constructor_index
12966 197 : = size_binop_loc (input_location, PLUS_EXPR,
12967 197 : constructor_index, bitsize_int (len));
12968 : }
12969 : else
12970 : {
12971 1720474 : value.value = maybe_split_raw_data (value.value, &raw_data);
12972 : /* Now output the actual element. */
12973 1720474 : if (value.value)
12974 : {
12975 1720474 : push_array_bounds (tree_to_uhwi (constructor_index));
12976 1720474 : output_init_element (loc, value.value, value.original_type,
12977 : strict_string, elttype,
12978 : constructor_index, true, implicit,
12979 : braced_init_obstack);
12980 1720474 : RESTORE_SPELLING_DEPTH (constructor_depth);
12981 : }
12982 :
12983 1720474 : constructor_index
12984 1720474 : = size_binop_loc (input_location, PLUS_EXPR,
12985 : constructor_index, bitsize_one_node);
12986 :
12987 1720474 : if (!value.value)
12988 : /* If we are doing the bookkeeping for an element that was
12989 : directly output as a constructor, we must update
12990 : constructor_unfilled_index. */
12991 0 : constructor_unfilled_index = constructor_index;
12992 : }
12993 : }
12994 6839237 : else if (gnu_vector_type_p (constructor_type))
12995 : {
12996 6838745 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12997 :
12998 : /* Do a basic check of initializer size. Note that vectors
12999 : may not always have a fixed size derived from their type. */
13000 6838745 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
13001 6838745 : tree_to_poly_uint64 (constructor_index)))
13002 : {
13003 : /* Diagose VLA out-of-bounds as errors. */
13004 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
13005 2 : pedwarn_init (loc, 0,
13006 : "excess elements in vector initializer");
13007 : else
13008 : error_init (loc, "excess elements in vector initializer");
13009 :
13010 : break;
13011 : }
13012 :
13013 6838743 : value.value = maybe_split_raw_data (value.value, &raw_data);
13014 : /* Now output the actual element. */
13015 6838743 : if (value.value)
13016 : {
13017 6838743 : if (TREE_CODE (value.value) == VECTOR_CST)
13018 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
13019 6838743 : output_init_element (loc, value.value, value.original_type,
13020 : strict_string, elttype,
13021 : constructor_index, true, implicit,
13022 : braced_init_obstack);
13023 : }
13024 :
13025 6838743 : constructor_index
13026 6838743 : = size_binop_loc (input_location,
13027 : PLUS_EXPR, constructor_index, bitsize_one_node);
13028 :
13029 6838743 : if (!value.value)
13030 : /* If we are doing the bookkeeping for an element that was
13031 : directly output as a constructor, we must update
13032 : constructor_unfilled_index. */
13033 0 : constructor_unfilled_index = constructor_index;
13034 : }
13035 :
13036 : /* Handle the sole element allowed in a braced initializer
13037 : for a scalar variable. */
13038 492 : else if (constructor_type != error_mark_node
13039 492 : && constructor_fields == NULL_TREE)
13040 : {
13041 29 : pedwarn_init (loc, 0,
13042 : "excess elements in scalar initializer");
13043 29 : break;
13044 : }
13045 : else
13046 : {
13047 463 : value.value = maybe_split_raw_data (value.value, &raw_data);
13048 463 : if (value.value)
13049 463 : output_init_element (loc, value.value, value.original_type,
13050 : strict_string, constructor_type,
13051 : NULL_TREE, true, implicit,
13052 : braced_init_obstack);
13053 463 : constructor_fields = NULL_TREE;
13054 : }
13055 :
13056 : /* Handle range initializers either at this level or anywhere higher
13057 : in the designator stack. */
13058 9630901 : if (constructor_range_stack)
13059 : {
13060 11564 : struct constructor_range_stack *p, *range_stack;
13061 11564 : int finish = 0;
13062 :
13063 11564 : range_stack = constructor_range_stack;
13064 11564 : constructor_range_stack = 0;
13065 11564 : while (constructor_stack != range_stack->stack)
13066 : {
13067 0 : gcc_assert (constructor_stack->implicit);
13068 0 : process_init_element (loc,
13069 : pop_init_level (loc, 1,
13070 : braced_init_obstack,
13071 : last_init_list_comma),
13072 : true, braced_init_obstack);
13073 : }
13074 325 : for (p = range_stack;
13075 11889 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
13076 325 : p = p->prev)
13077 : {
13078 325 : gcc_assert (constructor_stack->implicit);
13079 325 : process_init_element (loc,
13080 : pop_init_level (loc, 1,
13081 : braced_init_obstack,
13082 : last_init_list_comma),
13083 : true, braced_init_obstack);
13084 : }
13085 :
13086 11564 : p->index = size_binop_loc (input_location,
13087 : PLUS_EXPR, p->index, bitsize_one_node);
13088 11564 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
13089 379 : finish = 1;
13090 :
13091 11889 : while (1)
13092 : {
13093 11889 : constructor_index = p->index;
13094 11889 : constructor_fields = p->fields;
13095 11889 : if (finish && p->range_end && p->index == p->range_start)
13096 : {
13097 8 : finish = 0;
13098 8 : p->prev = 0;
13099 : }
13100 11889 : p = p->next;
13101 11889 : if (!p)
13102 : break;
13103 325 : finish_implicit_inits (loc, braced_init_obstack);
13104 325 : push_init_level (loc, 2, braced_init_obstack);
13105 325 : p->stack = constructor_stack;
13106 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
13107 33 : p->index = p->range_start;
13108 : }
13109 :
13110 11564 : if (!finish)
13111 11193 : constructor_range_stack = range_stack;
13112 11564 : continue;
13113 11564 : }
13114 :
13115 : break;
13116 : }
13117 :
13118 9620813 : constructor_range_stack = 0;
13119 :
13120 9620813 : if (raw_data && RAW_DATA_LENGTH (raw_data))
13121 : {
13122 212 : gcc_assert (!string_flag && !was_designated);
13123 212 : value.value = raw_data;
13124 212 : raw_data = NULL_TREE;
13125 212 : goto retry;
13126 : }
13127 : }
13128 :
13129 : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
13130 : (guaranteed to be 'volatile' or null) and ARGS (represented using
13131 : an ASM_EXPR node). */
13132 : tree
13133 205074 : build_asm_stmt (bool is_volatile, tree args)
13134 : {
13135 205074 : if (is_volatile)
13136 165272 : ASM_VOLATILE_P (args) = 1;
13137 205074 : return add_stmt (args);
13138 : }
13139 :
13140 : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
13141 : some INPUTS, and some CLOBBERS. The latter three may be NULL.
13142 : SIMPLE indicates whether there was anything at all after the
13143 : string in the asm expression -- asm("blah") and asm("blah" : )
13144 : are subtly different. We use a ASM_EXPR node to represent this.
13145 : LOC is the location of the asm, and IS_INLINE says whether this
13146 : is asm inline. */
13147 : tree
13148 205126 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
13149 : tree clobbers, tree labels, bool simple, bool is_inline)
13150 : {
13151 205126 : tree tail;
13152 205126 : tree args;
13153 205126 : int i;
13154 205126 : const char *constraint;
13155 205126 : const char **oconstraints;
13156 205126 : bool allows_mem, allows_reg, is_inout;
13157 205126 : int ninputs, noutputs;
13158 :
13159 205126 : ninputs = list_length (inputs);
13160 205126 : noutputs = list_length (outputs);
13161 205126 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
13162 :
13163 205126 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
13164 :
13165 : /* Remove output conversions that change the type but not the mode. */
13166 554530 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
13167 : {
13168 349404 : tree output = TREE_VALUE (tail);
13169 :
13170 349404 : output = c_fully_fold (output, false, NULL, true);
13171 :
13172 : /* ??? Really, this should not be here. Users should be using a
13173 : proper lvalue, dammit. But there's a long history of using casts
13174 : in the output operands. In cases like longlong.h, this becomes a
13175 : primitive form of typechecking -- if the cast can be removed, then
13176 : the output operand had a type of the proper width; otherwise we'll
13177 : get an error. Gross, but ... */
13178 349404 : STRIP_NOPS (output);
13179 :
13180 349404 : if (!lvalue_or_else (loc, output, lv_asm))
13181 4 : output = error_mark_node;
13182 :
13183 349404 : if (output != error_mark_node
13184 349404 : && (TREE_READONLY (output)
13185 349397 : || TYPE_READONLY (TREE_TYPE (output))
13186 349397 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
13187 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
13188 2 : readonly_error (loc, output, lv_asm);
13189 :
13190 349404 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13191 349404 : oconstraints[i] = constraint;
13192 :
13193 349404 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13194 : &allows_mem, &allows_reg, &is_inout,
13195 : nullptr))
13196 : {
13197 : /* If the operand is going to end up in memory,
13198 : mark it addressable. */
13199 349396 : if (!allows_reg && !c_mark_addressable (output))
13200 3 : output = error_mark_node;
13201 1422 : if (!(!allows_reg && allows_mem)
13202 347974 : && output != error_mark_node
13203 697368 : && VOID_TYPE_P (TREE_TYPE (output)))
13204 : {
13205 4 : error_at (loc, "invalid use of void expression");
13206 4 : output = error_mark_node;
13207 : }
13208 349396 : if (allows_reg && current_function_decl == NULL_TREE)
13209 : {
13210 1 : error_at (loc, "constraint allows registers outside of "
13211 : "a function");
13212 1 : output = error_mark_node;
13213 : }
13214 : }
13215 : else
13216 8 : output = error_mark_node;
13217 :
13218 349404 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13219 : {
13220 7 : if (TREE_SIDE_EFFECTS (output))
13221 : {
13222 0 : error_at (loc, "side-effects in output operand outside "
13223 : "of a function");
13224 0 : output = error_mark_node;
13225 : }
13226 : else
13227 : {
13228 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13229 7 : if (addr == error_mark_node)
13230 : output = error_mark_node;
13231 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13232 : {
13233 1 : error_at (loc, "output operand outside of a function is not "
13234 : "constant");
13235 1 : output = error_mark_node;
13236 : }
13237 : }
13238 : }
13239 349397 : else if (output != error_mark_node && strstr (constraint, "-"))
13240 : {
13241 1 : error_at (loc, "%<-%> modifier used inside of a function");
13242 1 : output = error_mark_node;
13243 : }
13244 :
13245 349404 : TREE_VALUE (tail) = output;
13246 : }
13247 :
13248 570093 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13249 : {
13250 364967 : tree input;
13251 :
13252 364967 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13253 364967 : input = TREE_VALUE (tail);
13254 :
13255 364967 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13256 : oconstraints, &allows_mem, &allows_reg,
13257 : nullptr))
13258 : {
13259 : /* If the operand is going to end up in memory,
13260 : mark it addressable. */
13261 364959 : if (!allows_reg && allows_mem)
13262 : {
13263 1364 : input = c_fully_fold (input, false, NULL, true);
13264 :
13265 : /* Strip the nops as we allow this case. FIXME, this really
13266 : should be rejected or made deprecated. */
13267 1364 : STRIP_NOPS (input);
13268 1364 : if (!c_mark_addressable (input))
13269 2 : input = error_mark_node;
13270 : }
13271 : else
13272 : {
13273 363595 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13274 : true, false),
13275 : false, NULL);
13276 :
13277 363595 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13278 : {
13279 4 : error_at (loc, "invalid use of void expression");
13280 4 : input = error_mark_node;
13281 : }
13282 : }
13283 364959 : if (allows_reg && current_function_decl == NULL_TREE)
13284 : {
13285 1 : error_at (loc, "constraint allows registers outside of "
13286 : "a function");
13287 1 : input = error_mark_node;
13288 : }
13289 364959 : if (constraint[0] == ':' && input != error_mark_node)
13290 : {
13291 35 : tree t = input;
13292 35 : STRIP_NOPS (t);
13293 35 : if (TREE_CODE (t) != ADDR_EXPR
13294 35 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13295 23 : || (VAR_P (TREE_OPERAND (t, 0))
13296 21 : && is_global_var (TREE_OPERAND (t, 0)))))
13297 : {
13298 5 : error_at (loc, "%<:%> constraint operand is not address "
13299 : "of a function or non-automatic variable");
13300 5 : input = error_mark_node;
13301 : }
13302 30 : else if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
13303 10 : suppress_warning (TREE_OPERAND (t, 0), OPT_Wunused);
13304 : }
13305 : }
13306 : else
13307 8 : input = error_mark_node;
13308 :
13309 364967 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13310 : {
13311 58 : if (TREE_SIDE_EFFECTS (input))
13312 : {
13313 2 : error_at (loc, "side-effects in input operand outside "
13314 : "of a function");
13315 2 : input = error_mark_node;
13316 : }
13317 : else
13318 : {
13319 56 : tree tem = input;
13320 56 : if (allows_mem && lvalue_p (input))
13321 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13322 56 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13323 : {
13324 2 : error_at (loc, "input operand outside of a function is not "
13325 : "constant");
13326 2 : input = error_mark_node;
13327 : }
13328 : }
13329 : }
13330 364909 : else if (input != error_mark_node && strstr (constraint, "-"))
13331 : {
13332 3 : error_at (loc, "%<-%> modifier used inside of a function");
13333 3 : input = error_mark_node;
13334 : }
13335 :
13336 364967 : TREE_VALUE (tail) = input;
13337 : }
13338 :
13339 205126 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13340 :
13341 : /* asm statements without outputs, including simple ones, are treated
13342 : as volatile. */
13343 205126 : ASM_BASIC_P (args) = simple;
13344 205126 : ASM_VOLATILE_P (args) = (noutputs == 0);
13345 205126 : ASM_INLINE_P (args) = is_inline;
13346 :
13347 205126 : return args;
13348 : }
13349 :
13350 : /* Generate a goto statement to LABEL. LOC is the location of the
13351 : GOTO. */
13352 :
13353 : tree
13354 83099 : c_finish_goto_label (location_t loc, tree label)
13355 : {
13356 83099 : tree decl = lookup_label_for_goto (loc, label);
13357 83099 : if (!decl)
13358 : return NULL_TREE;
13359 83099 : TREE_USED (decl) = 1;
13360 83099 : mark_decl_used (decl, false);
13361 83099 : {
13362 83099 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13363 83099 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13364 83099 : SET_EXPR_LOCATION (t, loc);
13365 83099 : return add_stmt (t);
13366 : }
13367 : }
13368 :
13369 : /* Generate a computed goto statement to EXPR. LOC is the location of
13370 : the GOTO. */
13371 :
13372 : tree
13373 949 : c_finish_goto_ptr (location_t loc, c_expr val)
13374 : {
13375 949 : tree expr = val.value;
13376 949 : tree t;
13377 949 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13378 949 : if (expr != error_mark_node
13379 948 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13380 954 : && !null_pointer_constant_p (expr))
13381 : {
13382 2 : error_at (val.get_location (),
13383 : "computed goto must be pointer type");
13384 2 : expr = build_zero_cst (ptr_type_node);
13385 : }
13386 949 : expr = c_fully_fold (expr, false, NULL);
13387 949 : expr = convert (ptr_type_node, expr);
13388 949 : t = build1 (GOTO_EXPR, void_type_node, expr);
13389 949 : SET_EXPR_LOCATION (t, loc);
13390 949 : return add_stmt (t);
13391 : }
13392 :
13393 : /* Generate a C `return' statement. RETVAL is the expression for what
13394 : to return, or a null pointer for `return;' with no value. LOC is
13395 : the location of the return statement, or the location of the expression,
13396 : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13397 : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13398 : attribute. */
13399 :
13400 : tree
13401 34676390 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13402 : {
13403 34676390 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13404 34676390 : bool no_warning = false;
13405 34676390 : bool npc = false;
13406 :
13407 : /* Use the expansion point to handle cases such as returning NULL
13408 : in a function returning void. */
13409 34676390 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13410 :
13411 34676390 : if (TREE_THIS_VOLATILE (current_function_decl))
13412 23 : warning_at (xloc, 0,
13413 : "function declared %<noreturn%> has a %<return%> statement");
13414 :
13415 34676390 : set_musttail_on_return (retval, xloc, musttail_p);
13416 :
13417 34676390 : if (retval)
13418 : {
13419 34654870 : tree semantic_type = NULL_TREE;
13420 34654870 : npc = null_pointer_constant_p (retval);
13421 34654870 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13422 : {
13423 92 : semantic_type = TREE_TYPE (retval);
13424 92 : retval = TREE_OPERAND (retval, 0);
13425 : }
13426 34654870 : retval = c_fully_fold (retval, false, NULL);
13427 34654870 : if (semantic_type
13428 34654870 : && valtype != NULL_TREE
13429 92 : && TREE_CODE (valtype) != VOID_TYPE)
13430 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13431 : }
13432 :
13433 34654870 : if (!retval)
13434 : {
13435 21520 : current_function_returns_null = 1;
13436 21520 : if ((warn_return_type >= 0 || flag_isoc99)
13437 21395 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13438 : {
13439 46 : no_warning = true;
13440 47 : if (emit_diagnostic (flag_isoc99
13441 : ? diagnostics::kind::permerror
13442 : : diagnostics::kind::warning,
13443 46 : loc, OPT_Wreturn_mismatch,
13444 : "%<return%> with no value,"
13445 : " in function returning non-void"))
13446 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13447 : "declared here");
13448 : }
13449 : }
13450 34654870 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13451 : {
13452 325 : current_function_returns_null = 1;
13453 325 : bool warned_here;
13454 325 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13455 64 : warned_here = permerror_opt
13456 64 : (xloc, OPT_Wreturn_mismatch,
13457 : "%<return%> with a value, in function returning void");
13458 : else
13459 261 : warned_here = pedwarn
13460 261 : (xloc, OPT_Wpedantic, "ISO C forbids "
13461 : "%<return%> with expression, in function returning void");
13462 325 : if (warned_here)
13463 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13464 : "declared here");
13465 : }
13466 : else
13467 : {
13468 34654545 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13469 : retval, origtype, ic_return,
13470 : npc, NULL_TREE, NULL_TREE, 0);
13471 34654545 : tree res = DECL_RESULT (current_function_decl);
13472 34654545 : tree inner;
13473 34654545 : bool save;
13474 :
13475 34654545 : current_function_returns_value = 1;
13476 34654545 : if (t == error_mark_node)
13477 : {
13478 : /* Suppress -Wreturn-type for this function. */
13479 299 : if (warn_return_type)
13480 298 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13481 299 : return NULL_TREE;
13482 : }
13483 :
13484 34654246 : save = in_late_binary_op;
13485 69300185 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13486 34645134 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13487 69298081 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13488 333591 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13489 333591 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13490 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13491 10411 : in_late_binary_op = true;
13492 34654246 : inner = t = convert (TREE_TYPE (res), t);
13493 34654246 : in_late_binary_op = save;
13494 :
13495 : /* Strip any conversions, additions, and subtractions, and see if
13496 : we are returning the address of a local variable. Warn if so. */
13497 37934977 : while (1)
13498 : {
13499 37934977 : switch (TREE_CODE (inner))
13500 : {
13501 3279303 : CASE_CONVERT:
13502 3279303 : case NON_LVALUE_EXPR:
13503 3279303 : case PLUS_EXPR:
13504 3279303 : case POINTER_PLUS_EXPR:
13505 3279303 : inner = TREE_OPERAND (inner, 0);
13506 3279303 : continue;
13507 :
13508 1433 : case MINUS_EXPR:
13509 : /* If the second operand of the MINUS_EXPR has a pointer
13510 : type (or is converted from it), this may be valid, so
13511 : don't give a warning. */
13512 1433 : {
13513 1433 : tree op1 = TREE_OPERAND (inner, 1);
13514 :
13515 3115 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13516 3369 : && (CONVERT_EXPR_P (op1)
13517 1428 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13518 254 : op1 = TREE_OPERAND (op1, 0);
13519 :
13520 1433 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13521 : break;
13522 :
13523 1428 : inner = TREE_OPERAND (inner, 0);
13524 1428 : continue;
13525 1428 : }
13526 :
13527 2959 : case ADDR_EXPR:
13528 2959 : inner = TREE_OPERAND (inner, 0);
13529 :
13530 2959 : while (REFERENCE_CLASS_P (inner)
13531 3853 : && !INDIRECT_REF_P (inner))
13532 894 : inner = TREE_OPERAND (inner, 0);
13533 :
13534 2959 : if (DECL_P (inner)
13535 1628 : && !DECL_EXTERNAL (inner)
13536 978 : && DECL_CONTEXT (inner) == current_function_decl
13537 3181 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13538 : {
13539 182 : if (TREE_CODE (inner) == LABEL_DECL)
13540 4 : warning_at (loc, OPT_Wreturn_local_addr,
13541 : "function returns address of label");
13542 178 : else if (TREE_CODE (inner) == FUNCTION_DECL
13543 178 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13544 9 : || !DECL_INITIAL (inner)))
13545 14 : warning_at (loc, OPT_Wreturn_local_addr,
13546 : "function returns address of nested function "
13547 : "referencing local context");
13548 164 : else if (TREE_CODE (inner) != FUNCTION_DECL
13549 156 : && !TREE_STATIC (inner))
13550 : {
13551 9 : warning_at (loc, OPT_Wreturn_local_addr,
13552 : "function returns address of local variable");
13553 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13554 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13555 : }
13556 : }
13557 : break;
13558 :
13559 : default:
13560 : break;
13561 3279303 : }
13562 :
13563 34654246 : break;
13564 : }
13565 :
13566 34654246 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13567 34654246 : SET_EXPR_LOCATION (retval, loc);
13568 :
13569 34654246 : if (warn_sequence_point)
13570 2966800 : verify_sequence_points (retval);
13571 : }
13572 :
13573 34676091 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13574 34676091 : if (no_warning)
13575 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13576 34676091 : return add_stmt (ret_stmt);
13577 : }
13578 :
13579 : struct c_switch {
13580 : /* The SWITCH_STMT being built. */
13581 : tree switch_stmt;
13582 :
13583 : /* The original type of the testing expression, i.e. before the
13584 : default conversion is applied. */
13585 : tree orig_type;
13586 :
13587 : /* A splay-tree mapping the low element of a case range to the high
13588 : element, or NULL_TREE if there is no high element. Used to
13589 : determine whether or not a new case label duplicates an old case
13590 : label. We need a tree, rather than simply a hash table, because
13591 : of the GNU case range extension. */
13592 : splay_tree cases;
13593 :
13594 : /* The bindings at the point of the switch. This is used for
13595 : warnings crossing decls when branching to a case label. */
13596 : struct c_spot_bindings *bindings;
13597 :
13598 : /* Whether the switch includes any break statements. */
13599 : bool break_stmt_seen_p;
13600 :
13601 : /* The next node on the stack. */
13602 : struct c_switch *next;
13603 :
13604 : /* Remember whether the controlling expression had boolean type
13605 : before integer promotions for the sake of -Wswitch-bool. */
13606 : bool bool_cond_p;
13607 : };
13608 :
13609 : /* A stack of the currently active switch statements. The innermost
13610 : switch statement is on the top of the stack. There is no need to
13611 : mark the stack for garbage collection because it is only active
13612 : during the processing of the body of a function, and we never
13613 : collect at that point. */
13614 :
13615 : struct c_switch *c_switch_stack;
13616 :
13617 : /* Start a C switch statement, testing expression EXP. Return the new
13618 : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13619 : SWITCH_COND_LOC is the location of the switch's condition.
13620 : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13621 :
13622 : tree
13623 37418 : c_start_switch (location_t switch_loc,
13624 : location_t switch_cond_loc,
13625 : tree exp, bool explicit_cast_p, tree switch_name)
13626 : {
13627 37418 : tree orig_type = error_mark_node;
13628 37418 : bool bool_cond_p = false;
13629 37418 : struct c_switch *cs;
13630 :
13631 37418 : if (exp != error_mark_node)
13632 : {
13633 37375 : orig_type = TREE_TYPE (exp);
13634 :
13635 37375 : if (!INTEGRAL_TYPE_P (orig_type))
13636 : {
13637 12 : if (orig_type != error_mark_node)
13638 : {
13639 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13640 12 : orig_type = error_mark_node;
13641 : }
13642 12 : exp = integer_zero_node;
13643 : }
13644 : else
13645 : {
13646 37363 : tree type = TYPE_MAIN_VARIANT (orig_type);
13647 37363 : tree e = exp;
13648 :
13649 : /* Warn if the condition has boolean value. */
13650 37366 : while (TREE_CODE (e) == COMPOUND_EXPR)
13651 3 : e = TREE_OPERAND (e, 1);
13652 :
13653 37338 : if ((C_BOOLEAN_TYPE_P (type)
13654 37338 : || truth_value_p (TREE_CODE (e)))
13655 : /* Explicit cast to int suppresses this warning. */
13656 37385 : && !(TREE_CODE (type) == INTEGER_TYPE
13657 : && explicit_cast_p))
13658 : bool_cond_p = true;
13659 :
13660 37363 : if (!in_system_header_at (input_location)
13661 37363 : && (type == long_integer_type_node
13662 12028 : || type == long_unsigned_type_node))
13663 290 : warning_at (switch_cond_loc,
13664 290 : OPT_Wtraditional, "%<long%> switch expression not "
13665 : "converted to %<int%> in ISO C");
13666 :
13667 37363 : exp = c_fully_fold (exp, false, NULL);
13668 37363 : exp = default_conversion (exp);
13669 :
13670 37363 : if (warn_sequence_point)
13671 6038 : verify_sequence_points (exp);
13672 : }
13673 : }
13674 :
13675 : /* Add this new SWITCH_STMT to the stack. */
13676 37418 : cs = XNEW (struct c_switch);
13677 37418 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13678 : NULL_TREE, orig_type, NULL_TREE, switch_name);
13679 37418 : cs->orig_type = orig_type;
13680 37418 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13681 37418 : cs->bindings = c_get_switch_bindings ();
13682 37418 : cs->break_stmt_seen_p = false;
13683 37418 : cs->bool_cond_p = bool_cond_p;
13684 37418 : cs->next = c_switch_stack;
13685 37418 : c_switch_stack = cs;
13686 :
13687 37418 : return add_stmt (cs->switch_stmt);
13688 : }
13689 :
13690 : /* Process a case label at location LOC, with attributes ATTRS. */
13691 :
13692 : tree
13693 1030994 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13694 : {
13695 1030994 : tree label = NULL_TREE;
13696 :
13697 1030994 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13698 : {
13699 87 : low_value = c_fully_fold (low_value, false, NULL);
13700 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13701 9 : pedwarn (loc, OPT_Wpedantic,
13702 : "case label is not an integer constant expression");
13703 : }
13704 :
13705 1030994 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13706 : {
13707 0 : high_value = c_fully_fold (high_value, false, NULL);
13708 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13709 0 : pedwarn (input_location, OPT_Wpedantic,
13710 : "case label is not an integer constant expression");
13711 : }
13712 :
13713 1030994 : if (c_switch_stack == NULL)
13714 : {
13715 3 : if (low_value)
13716 2 : error_at (loc, "case label not within a switch statement");
13717 : else
13718 1 : error_at (loc, "%<default%> label not within a switch statement");
13719 3 : return NULL_TREE;
13720 : }
13721 :
13722 1030991 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13723 1030991 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13724 : loc))
13725 : return NULL_TREE;
13726 :
13727 1030979 : label = c_add_case_label (loc, c_switch_stack->cases,
13728 1030979 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13729 : low_value, high_value, attrs);
13730 1030979 : if (label == error_mark_node)
13731 144 : label = NULL_TREE;
13732 : return label;
13733 : }
13734 :
13735 : /* Finish the switch statement. TYPE is the original type of the
13736 : controlling expression of the switch, or NULL_TREE. */
13737 :
13738 : void
13739 37418 : c_finish_switch (tree body, tree type)
13740 : {
13741 37418 : struct c_switch *cs = c_switch_stack;
13742 37418 : location_t switch_location;
13743 :
13744 37418 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13745 :
13746 : /* Emit warnings as needed. */
13747 37418 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13748 43630 : c_do_switch_warnings (cs->cases, switch_location,
13749 6212 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13750 37418 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13751 37418 : if (c_switch_covers_all_cases_p (cs->cases,
13752 37418 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13753 33472 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13754 37418 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13755 :
13756 : /* Pop the stack. */
13757 37418 : c_switch_stack = cs->next;
13758 37418 : splay_tree_delete (cs->cases);
13759 37418 : c_release_switch_bindings (cs->bindings);
13760 37418 : XDELETE (cs);
13761 37418 : }
13762 :
13763 : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13764 : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13765 : may be null. */
13766 :
13767 : void
13768 1280844 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13769 : tree else_block)
13770 : {
13771 1280844 : tree stmt;
13772 :
13773 1280844 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13774 1280844 : SET_EXPR_LOCATION (stmt, if_locus);
13775 1280844 : add_stmt (stmt);
13776 1280844 : }
13777 :
13778 : tree
13779 187924 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13780 : {
13781 : /* In switch statements break is sometimes stylistically used after
13782 : a return statement. This can lead to spurious warnings about
13783 : control reaching the end of a non-void function when it is
13784 : inlined. Note that we are calling block_may_fallthru with
13785 : language specific tree nodes; this works because
13786 : block_may_fallthru returns true when given something it does not
13787 : understand. */
13788 187924 : bool skip = !block_may_fallthru (cur_stmt_list);
13789 :
13790 187924 : if (is_break)
13791 174687 : switch (in_statement & ~IN_NAMED_STMT)
13792 : {
13793 9 : case 0:
13794 9 : error_at (loc, "break statement not within loop or switch");
13795 9 : return NULL_TREE;
13796 4 : case IN_OMP_BLOCK:
13797 4 : error_at (loc, "invalid exit from OpenMP structured block");
13798 4 : return NULL_TREE;
13799 12 : case IN_OMP_FOR:
13800 12 : error_at (loc, "break statement used with OpenMP for loop");
13801 12 : return NULL_TREE;
13802 : case IN_ITERATION_STMT:
13803 : case IN_OBJC_FOREACH:
13804 : break;
13805 158745 : default:
13806 158745 : gcc_assert (in_statement & IN_SWITCH_STMT);
13807 158745 : c_switch_stack->break_stmt_seen_p = true;
13808 158745 : break;
13809 : }
13810 : else
13811 13237 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13812 : {
13813 7 : case 0:
13814 7 : error_at (loc, "continue statement not within a loop");
13815 7 : return NULL_TREE;
13816 4 : case IN_OMP_BLOCK:
13817 4 : error_at (loc, "invalid exit from OpenMP structured block");
13818 4 : return NULL_TREE;
13819 : case IN_ITERATION_STMT:
13820 : case IN_OMP_FOR:
13821 : case IN_OBJC_FOREACH:
13822 : break;
13823 0 : default:
13824 0 : gcc_unreachable ();
13825 : }
13826 :
13827 187888 : if (skip)
13828 : return NULL_TREE;
13829 187352 : else if ((in_statement & IN_OBJC_FOREACH)
13830 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13831 0 : && name == NULL_TREE)
13832 : {
13833 : /* The foreach expander produces low-level code using gotos instead
13834 : of a structured loop construct. */
13835 0 : gcc_assert (label);
13836 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13837 : }
13838 187449 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13839 : {
13840 0 : label = DECL_CHAIN (name);
13841 0 : if (!is_break)
13842 0 : label = DECL_CHAIN (label);
13843 : /* Foreach expander from some outer level. */
13844 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13845 : }
13846 200578 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13847 187352 : name));
13848 : }
13849 :
13850 : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13851 :
13852 : static void
13853 6402831 : emit_side_effect_warnings (location_t loc, tree expr)
13854 : {
13855 6402831 : maybe_warn_nodiscard (loc, expr);
13856 6402831 : if (!warn_unused_value)
13857 : return;
13858 1305002 : if (expr == error_mark_node)
13859 : ;
13860 1304927 : else if (!TREE_SIDE_EFFECTS (expr))
13861 : {
13862 6567 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13863 6567 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13864 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13865 : }
13866 1298360 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13867 : {
13868 : tree r = expr;
13869 : location_t cloc = loc;
13870 11987 : while (TREE_CODE (r) == COMPOUND_EXPR)
13871 : {
13872 6009 : if (EXPR_HAS_LOCATION (r))
13873 5479 : cloc = EXPR_LOCATION (r);
13874 6009 : r = TREE_OPERAND (r, 1);
13875 : }
13876 5978 : if (!TREE_SIDE_EFFECTS (r)
13877 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13878 24 : && !CONVERT_EXPR_P (r)
13879 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13880 5990 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13881 8 : warning_at (cloc, OPT_Wunused_value,
13882 : "right-hand operand of comma expression has no effect");
13883 : }
13884 : else
13885 1292382 : warn_if_unused_value (expr, loc);
13886 : }
13887 :
13888 : /* Process an expression as if it were a complete statement. Emit
13889 : diagnostics, but do not call ADD_STMT. LOC is the location of the
13890 : statement. */
13891 :
13892 : tree
13893 6279386 : c_process_expr_stmt (location_t loc, tree expr)
13894 : {
13895 6279386 : tree exprv;
13896 :
13897 6279386 : if (!expr)
13898 : return NULL_TREE;
13899 :
13900 6274998 : expr = c_fully_fold (expr, false, NULL);
13901 :
13902 6274998 : if (warn_sequence_point)
13903 1300703 : verify_sequence_points (expr);
13904 :
13905 6274998 : if (TREE_TYPE (expr) != error_mark_node
13906 6272344 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13907 6274998 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13908 0 : error_at (loc, "expression statement has incomplete type");
13909 :
13910 : /* If we're not processing a statement expression, warn about unused values.
13911 : Warnings for statement expressions will be emitted later, once we figure
13912 : out which is the result. */
13913 6274998 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13914 6274998 : && (warn_unused_value || warn_unused_result))
13915 6189078 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13916 :
13917 : exprv = expr;
13918 6316249 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13919 41252 : exprv = TREE_OPERAND (exprv, 1);
13920 6286491 : while (CONVERT_EXPR_P (exprv))
13921 11494 : exprv = TREE_OPERAND (exprv, 0);
13922 6274997 : if (DECL_P (exprv)
13923 6275583 : || handled_component_p (exprv)
13924 12530125 : || TREE_CODE (exprv) == ADDR_EXPR)
13925 20455 : mark_exp_read (exprv);
13926 :
13927 : /* If the expression is not of a type to which we cannot assign a line
13928 : number, wrap the thing in a no-op NOP_EXPR. */
13929 6274997 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13930 : {
13931 14889 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13932 14889 : SET_EXPR_LOCATION (expr, loc);
13933 : }
13934 :
13935 : return expr;
13936 : }
13937 :
13938 : /* Emit an expression as a statement. LOC is the location of the
13939 : expression. */
13940 :
13941 : tree
13942 5974123 : c_finish_expr_stmt (location_t loc, tree expr)
13943 : {
13944 5974123 : if (expr)
13945 5959955 : return add_stmt (c_process_expr_stmt (loc, expr));
13946 : else
13947 : return NULL;
13948 : }
13949 :
13950 : /* Do the opposite and emit a statement as an expression. To begin,
13951 : create a new binding level and return it. */
13952 :
13953 : tree
13954 34943 : c_begin_stmt_expr (void)
13955 : {
13956 34943 : tree ret;
13957 :
13958 : /* We must force a BLOCK for this level so that, if it is not expanded
13959 : later, there is a way to turn off the entire subtree of blocks that
13960 : are contained in it. */
13961 34943 : keep_next_level ();
13962 34943 : ret = c_begin_compound_stmt (true);
13963 :
13964 34943 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13965 : ? NULL
13966 : : c_switch_stack->bindings);
13967 :
13968 : /* Mark the current statement list as belonging to a statement list. */
13969 34943 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13970 :
13971 34943 : return ret;
13972 : }
13973 :
13974 : /* LOC is the location of the compound statement to which this body
13975 : belongs. */
13976 :
13977 : tree
13978 34943 : c_finish_stmt_expr (location_t loc, tree body)
13979 : {
13980 34943 : tree last, type, tmp, val;
13981 34943 : tree *last_p;
13982 :
13983 34943 : body = c_end_compound_stmt (loc, body, true);
13984 :
13985 34943 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13986 : ? NULL
13987 : : c_switch_stack->bindings);
13988 :
13989 : /* Locate the last statement in BODY. See c_end_compound_stmt
13990 : about always returning a BIND_EXPR. */
13991 34943 : last_p = &BIND_EXPR_BODY (body);
13992 34943 : last = BIND_EXPR_BODY (body);
13993 :
13994 34943 : continue_searching:
13995 34943 : if (TREE_CODE (last) == STATEMENT_LIST)
13996 : {
13997 26129 : tree_stmt_iterator l = tsi_last (last);
13998 :
13999 26135 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
14000 6 : tsi_prev (&l);
14001 :
14002 : /* This can happen with degenerate cases like ({ }). No value. */
14003 26129 : if (tsi_end_p (l))
14004 389 : return body;
14005 :
14006 : /* If we're supposed to generate side effects warnings, process
14007 : all of the statements except the last. */
14008 25740 : if (warn_unused_value || warn_unused_result)
14009 : {
14010 25740 : for (tree_stmt_iterator i = tsi_start (last);
14011 275933 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
14012 : {
14013 250193 : location_t tloc;
14014 250193 : tree t = tsi_stmt (i);
14015 :
14016 250193 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
14017 250193 : emit_side_effect_warnings (tloc, t);
14018 : }
14019 : }
14020 25740 : last_p = tsi_stmt_ptr (l);
14021 25740 : last = *last_p;
14022 : }
14023 :
14024 : /* If the end of the list is exception related, then the list was split
14025 : by a call to push_cleanup. Continue searching. */
14026 34554 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
14027 34554 : || TREE_CODE (last) == TRY_CATCH_EXPR)
14028 : {
14029 0 : last_p = &TREE_OPERAND (last, 0);
14030 0 : last = *last_p;
14031 0 : goto continue_searching;
14032 : }
14033 :
14034 34554 : if (last == error_mark_node)
14035 : return last;
14036 :
14037 : /* In the case that the BIND_EXPR is not necessary, return the
14038 : expression out from inside it. */
14039 34545 : if ((last == BIND_EXPR_BODY (body)
14040 : /* Skip nested debug stmts. */
14041 25739 : || last == expr_first (BIND_EXPR_BODY (body)))
14042 45579 : && BIND_EXPR_VARS (body) == NULL)
14043 : {
14044 : /* Even if this looks constant, do not allow it in a constant
14045 : expression. */
14046 11022 : last = c_wrap_maybe_const (last, true);
14047 : /* Do not warn if the return value of a statement expression is
14048 : unused. */
14049 11022 : suppress_warning (last, OPT_Wunused);
14050 11022 : return last;
14051 : }
14052 :
14053 : /* Extract the type of said expression. */
14054 23523 : type = TREE_TYPE (last);
14055 :
14056 : /* If we're not returning a value at all, then the BIND_EXPR that
14057 : we already have is a fine expression to return. */
14058 23523 : if (!type || VOID_TYPE_P (type))
14059 : return body;
14060 :
14061 : /* Now that we've located the expression containing the value, it seems
14062 : silly to make voidify_wrapper_expr repeat the process. Create a
14063 : temporary of the appropriate type and stick it in a TARGET_EXPR. */
14064 18199 : tmp = create_tmp_var_raw (type);
14065 :
14066 : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
14067 : tree_expr_nonnegative_p giving up immediately. */
14068 18199 : val = last;
14069 18199 : if (TREE_CODE (val) == NOP_EXPR
14070 18199 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
14071 3793 : val = TREE_OPERAND (val, 0);
14072 :
14073 18199 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
14074 18199 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
14075 :
14076 18199 : {
14077 18199 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
14078 18199 : SET_EXPR_LOCATION (t, loc);
14079 18199 : return t;
14080 : }
14081 : }
14082 :
14083 : /* Begin and end compound statements. This is as simple as pushing
14084 : and popping new statement lists from the tree. */
14085 :
14086 : tree
14087 41246079 : c_begin_compound_stmt (bool do_scope)
14088 : {
14089 41246079 : tree stmt = push_stmt_list ();
14090 41246079 : if (do_scope)
14091 41150008 : push_scope ();
14092 41246079 : return stmt;
14093 : }
14094 :
14095 : /* End a compound statement. STMT is the statement. LOC is the
14096 : location of the compound statement-- this is usually the location
14097 : of the opening brace. */
14098 :
14099 : tree
14100 41246078 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
14101 : {
14102 41246078 : tree block = NULL;
14103 :
14104 41246078 : if (do_scope)
14105 : {
14106 41150007 : if (c_dialect_objc ())
14107 0 : objc_clear_super_receiver ();
14108 41150007 : block = pop_scope ();
14109 : }
14110 :
14111 41246078 : stmt = pop_stmt_list (stmt);
14112 41246078 : stmt = c_build_bind_expr (loc, block, stmt);
14113 :
14114 : /* If this compound statement is nested immediately inside a statement
14115 : expression, then force a BIND_EXPR to be created. Otherwise we'll
14116 : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
14117 : STATEMENT_LISTs merge, and thus we can lose track of what statement
14118 : was really last. */
14119 82492156 : if (building_stmt_list_p ()
14120 41245993 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
14121 41368770 : && TREE_CODE (stmt) != BIND_EXPR)
14122 : {
14123 120949 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
14124 120949 : TREE_SIDE_EFFECTS (stmt) = 1;
14125 120949 : SET_EXPR_LOCATION (stmt, loc);
14126 : }
14127 :
14128 41246078 : return stmt;
14129 : }
14130 :
14131 : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
14132 : when the current scope is exited. EH_ONLY is true when this is not
14133 : meant to apply to normal control flow transfer. */
14134 :
14135 : void
14136 131 : push_cleanup (tree decl, tree cleanup, bool eh_only)
14137 : {
14138 131 : enum tree_code code;
14139 131 : tree stmt, list;
14140 131 : bool stmt_expr;
14141 :
14142 131 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
14143 131 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
14144 131 : add_stmt (stmt);
14145 131 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
14146 131 : list = push_stmt_list ();
14147 131 : TREE_OPERAND (stmt, 0) = list;
14148 131 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
14149 131 : }
14150 :
14151 : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
14152 : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
14153 :
14154 : static tree
14155 101107 : build_vec_cmp (tree_code code, tree type,
14156 : tree arg0, tree arg1)
14157 : {
14158 101107 : tree zero_vec = build_zero_cst (type);
14159 101107 : tree minus_one_vec = build_minus_one_cst (type);
14160 101107 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
14161 101107 : tree cmp = build2 (code, cmp_type, arg0, arg1);
14162 101107 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
14163 : }
14164 :
14165 : /* Possibly warn about an address of OP never being NULL in a comparison
14166 : operation CODE involving null. */
14167 :
14168 : static void
14169 52214 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
14170 : {
14171 : /* Prevent warnings issued for macro expansion. */
14172 52214 : if (!warn_address
14173 30626 : || warning_suppressed_p (op, OPT_Waddress)
14174 82464 : || from_macro_expansion_at (loc))
14175 24711 : return;
14176 :
14177 27503 : if (TREE_CODE (op) == NOP_EXPR)
14178 : {
14179 : /* Allow casts to intptr_t to suppress the warning. */
14180 1135 : tree type = TREE_TYPE (op);
14181 1135 : if (TREE_CODE (type) == INTEGER_TYPE)
14182 : return;
14183 1135 : op = TREE_OPERAND (op, 0);
14184 : }
14185 :
14186 27503 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
14187 : {
14188 : /* Allow a cast to void* to suppress the warning. */
14189 24 : tree type = TREE_TYPE (TREE_TYPE (op));
14190 24 : if (VOID_TYPE_P (type))
14191 : return;
14192 :
14193 : /* Adding any value to a null pointer, including zero, is undefined
14194 : in C. This includes the expression &p[0] where p is the null
14195 : pointer, although &p[0] will have been folded to p by this point
14196 : and so not diagnosed. */
14197 24 : if (code == EQ_EXPR)
14198 22 : warning_at (loc, OPT_Waddress,
14199 : "the comparison will always evaluate as %<false%> "
14200 : "for the pointer operand in %qE must not be NULL",
14201 : op);
14202 : else
14203 2 : warning_at (loc, OPT_Waddress,
14204 : "the comparison will always evaluate as %<true%> "
14205 : "for the pointer operand in %qE must not be NULL",
14206 : op);
14207 :
14208 24 : return;
14209 : }
14210 :
14211 27479 : if (TREE_CODE (op) != ADDR_EXPR)
14212 : return;
14213 :
14214 254 : op = TREE_OPERAND (op, 0);
14215 :
14216 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14217 254 : || TREE_CODE (op) == REALPART_EXPR)
14218 : {
14219 : /* The address of either complex part may not be null. */
14220 14 : if (code == EQ_EXPR)
14221 9 : warning_at (loc, OPT_Waddress,
14222 : "the comparison will always evaluate as %<false%> "
14223 : "for the address of %qE will never be NULL",
14224 : op);
14225 : else
14226 5 : warning_at (loc, OPT_Waddress,
14227 : "the comparison will always evaluate as %<true%> "
14228 : "for the address of %qE will never be NULL",
14229 : op);
14230 14 : return;
14231 : }
14232 :
14233 : /* Set to true in the loop below if OP dereferences is operand.
14234 : In such a case the ultimate target need not be a decl for
14235 : the null [in]equality test to be constant. */
14236 : bool deref = false;
14237 :
14238 : /* Get the outermost array or object, or member. */
14239 294 : while (handled_component_p (op))
14240 : {
14241 72 : if (TREE_CODE (op) == COMPONENT_REF)
14242 : {
14243 : /* Get the member (its address is never null). */
14244 18 : op = TREE_OPERAND (op, 1);
14245 18 : break;
14246 : }
14247 :
14248 : /* Get the outer array/object to refer to in the warning. */
14249 54 : op = TREE_OPERAND (op, 0);
14250 54 : deref = true;
14251 : }
14252 :
14253 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14254 371 : || from_macro_expansion_at (loc))
14255 109 : return;
14256 :
14257 131 : bool w;
14258 131 : if (code == EQ_EXPR)
14259 93 : w = warning_at (loc, OPT_Waddress,
14260 : "the comparison will always evaluate as %<false%> "
14261 : "for the address of %qE will never be NULL",
14262 : op);
14263 : else
14264 38 : w = warning_at (loc, OPT_Waddress,
14265 : "the comparison will always evaluate as %<true%> "
14266 : "for the address of %qE will never be NULL",
14267 : op);
14268 :
14269 131 : if (w && DECL_P (op))
14270 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14271 : }
14272 :
14273 : /* Build a binary-operation expression without default conversions.
14274 : CODE is the kind of expression to build.
14275 : LOCATION is the operator's location.
14276 : This function differs from `build' in several ways:
14277 : the data type of the result is computed and recorded in it,
14278 : warnings are generated if arg data types are invalid,
14279 : special handling for addition and subtraction of pointers is known,
14280 : and some optimization is done (operations on narrow ints
14281 : are done in the narrower type when that gives the same result).
14282 : Constant folding is also done before the result is returned.
14283 :
14284 : Note that the operands will never have enumeral types, or function
14285 : or array types, because either they will have the default conversions
14286 : performed or they have both just been converted to some other type in which
14287 : the arithmetic is to be done. */
14288 :
14289 : tree
14290 16903201 : build_binary_op (location_t location, enum tree_code code,
14291 : tree orig_op0, tree orig_op1, bool convert_p)
14292 : {
14293 16903201 : tree type0, type1, orig_type0, orig_type1;
14294 16903201 : tree eptype;
14295 16903201 : enum tree_code code0, code1;
14296 16903201 : tree op0, op1;
14297 16903201 : tree ret = error_mark_node;
14298 16903201 : const char *invalid_op_diag;
14299 16903201 : bool op0_int_operands, op1_int_operands;
14300 16903201 : bool int_const, int_const_or_overflow, int_operands;
14301 :
14302 : /* Expression code to give to the expression when it is built.
14303 : Normally this is CODE, which is what the caller asked for,
14304 : but in some special cases we change it. */
14305 16903201 : enum tree_code resultcode = code;
14306 :
14307 : /* Data type in which the computation is to be performed.
14308 : In the simplest cases this is the common type of the arguments. */
14309 16903201 : tree result_type = NULL;
14310 :
14311 : /* When the computation is in excess precision, the type of the
14312 : final EXCESS_PRECISION_EXPR. */
14313 16903201 : tree semantic_result_type = NULL;
14314 :
14315 : /* Nonzero means operands have already been type-converted
14316 : in whatever way is necessary.
14317 : Zero means they need to be converted to RESULT_TYPE. */
14318 16903201 : int converted = 0;
14319 :
14320 : /* Nonzero means create the expression with this type, rather than
14321 : RESULT_TYPE. */
14322 16903201 : tree build_type = NULL_TREE;
14323 :
14324 : /* Nonzero means after finally constructing the expression
14325 : convert it to this type. */
14326 16903201 : tree final_type = NULL_TREE;
14327 :
14328 : /* Nonzero if this is an operation like MIN or MAX which can
14329 : safely be computed in short if both args are promoted shorts.
14330 : Also implies COMMON.
14331 : -1 indicates a bitwise operation; this makes a difference
14332 : in the exact conditions for when it is safe to do the operation
14333 : in a narrower mode. */
14334 16903201 : int shorten = 0;
14335 :
14336 : /* Nonzero if this is a comparison operation;
14337 : if both args are promoted shorts, compare the original shorts.
14338 : Also implies COMMON. */
14339 16903201 : int short_compare = 0;
14340 :
14341 : /* Nonzero if this is a right-shift operation, which can be computed on the
14342 : original short and then promoted if the operand is a promoted short. */
14343 16903201 : int short_shift = 0;
14344 :
14345 : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14346 16903201 : int common = 0;
14347 :
14348 : /* True means types are compatible as far as ObjC is concerned. */
14349 16903201 : bool objc_ok;
14350 :
14351 : /* True means this is an arithmetic operation that may need excess
14352 : precision. */
14353 16903201 : bool may_need_excess_precision;
14354 :
14355 : /* True means this is a boolean operation that converts both its
14356 : operands to truth-values. */
14357 16903201 : bool boolean_op = false;
14358 :
14359 : /* Remember whether we're doing / or %. */
14360 16903201 : bool doing_div_or_mod = false;
14361 :
14362 : /* Remember whether we're doing << or >>. */
14363 16903201 : bool doing_shift = false;
14364 :
14365 : /* Tree holding instrumentation expression. */
14366 16903201 : tree instrument_expr = NULL;
14367 :
14368 16903201 : if (location == UNKNOWN_LOCATION)
14369 79 : location = input_location;
14370 :
14371 16903201 : op0 = orig_op0;
14372 16903201 : op1 = orig_op1;
14373 :
14374 16903201 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14375 8177386 : if (op0_int_operands)
14376 8177386 : op0 = remove_c_maybe_const_expr (op0);
14377 16903201 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14378 11460260 : if (op1_int_operands)
14379 11460260 : op1 = remove_c_maybe_const_expr (op1);
14380 28363461 : int_operands = (op0_int_operands && op1_int_operands);
14381 11460260 : if (int_operands)
14382 : {
14383 15997953 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14384 8014147 : && TREE_CODE (orig_op1) == INTEGER_CST);
14385 7983806 : int_const = (int_const_or_overflow
14386 7983806 : && !TREE_OVERFLOW (orig_op0)
14387 7983737 : && !TREE_OVERFLOW (orig_op1));
14388 : }
14389 : else
14390 : int_const = int_const_or_overflow = false;
14391 :
14392 : /* Do not apply default conversion in mixed vector/scalar expression. */
14393 16903201 : if (convert_p
14394 16903201 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14395 : {
14396 10170211 : op0 = default_conversion (op0);
14397 10170211 : op1 = default_conversion (op1);
14398 : }
14399 :
14400 16903201 : orig_type0 = type0 = TREE_TYPE (op0);
14401 :
14402 16903201 : orig_type1 = type1 = TREE_TYPE (op1);
14403 :
14404 : /* The expression codes of the data types of the arguments tell us
14405 : whether the arguments are integers, floating, pointers, etc. */
14406 16903201 : code0 = TREE_CODE (type0);
14407 16903201 : code1 = TREE_CODE (type1);
14408 :
14409 : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14410 16903203 : STRIP_TYPE_NOPS (op0);
14411 16903204 : STRIP_TYPE_NOPS (op1);
14412 :
14413 : /* If an error was already reported for one of the arguments,
14414 : avoid reporting another error. */
14415 :
14416 16903201 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14417 710 : return error_mark_node;
14418 :
14419 16902491 : if (code0 == POINTER_TYPE
14420 16902491 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14421 11 : return error_mark_node;
14422 :
14423 16902480 : if (code1 == POINTER_TYPE
14424 16902480 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14425 11 : return error_mark_node;
14426 :
14427 33804938 : if ((invalid_op_diag
14428 16902469 : = targetm.invalid_binary_op (code, type0, type1)))
14429 : {
14430 0 : error_at (location, invalid_op_diag);
14431 0 : return error_mark_node;
14432 : }
14433 :
14434 16902469 : switch (code)
14435 : {
14436 : case PLUS_EXPR:
14437 : case MINUS_EXPR:
14438 : case MULT_EXPR:
14439 : case TRUNC_DIV_EXPR:
14440 : case CEIL_DIV_EXPR:
14441 : case FLOOR_DIV_EXPR:
14442 : case ROUND_DIV_EXPR:
14443 : case EXACT_DIV_EXPR:
14444 : may_need_excess_precision = true;
14445 : break;
14446 :
14447 2753078 : case EQ_EXPR:
14448 2753078 : case NE_EXPR:
14449 2753078 : case LE_EXPR:
14450 2753078 : case GE_EXPR:
14451 2753078 : case LT_EXPR:
14452 2753078 : case GT_EXPR:
14453 : /* Excess precision for implicit conversions of integers to
14454 : floating point in C11 and later. */
14455 2753078 : may_need_excess_precision = (flag_isoc11
14456 2753078 : && (ANY_INTEGRAL_TYPE_P (type0)
14457 487657 : || ANY_INTEGRAL_TYPE_P (type1)));
14458 : break;
14459 :
14460 : default:
14461 16902469 : may_need_excess_precision = false;
14462 : break;
14463 : }
14464 16902469 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14465 : {
14466 184 : op0 = TREE_OPERAND (op0, 0);
14467 184 : type0 = TREE_TYPE (op0);
14468 : }
14469 16902285 : else if (may_need_excess_precision
14470 16902285 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14471 : {
14472 771 : type0 = eptype;
14473 771 : op0 = convert (eptype, op0);
14474 : }
14475 16902469 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14476 : {
14477 889 : op1 = TREE_OPERAND (op1, 0);
14478 889 : type1 = TREE_TYPE (op1);
14479 : }
14480 16901580 : else if (may_need_excess_precision
14481 16901580 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14482 : {
14483 592 : type1 = eptype;
14484 592 : op1 = convert (eptype, op1);
14485 : }
14486 :
14487 16902469 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14488 :
14489 : /* In case when one of the operands of the binary operation is
14490 : a vector and another is a scalar -- convert scalar to vector. */
14491 18483397 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14492 18481475 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14493 : {
14494 2537 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14495 : orig_op1, true);
14496 :
14497 2537 : switch (convert_flag)
14498 : {
14499 12 : case stv_error:
14500 12 : return error_mark_node;
14501 602 : case stv_firstarg:
14502 602 : {
14503 602 : bool maybe_const = true;
14504 602 : tree sc;
14505 602 : sc = c_fully_fold (op0, false, &maybe_const);
14506 602 : sc = save_expr (sc);
14507 602 : sc = convert (TREE_TYPE (type1), sc);
14508 602 : op0 = build_vector_from_val (type1, sc);
14509 602 : if (!maybe_const)
14510 93 : op0 = c_wrap_maybe_const (op0, true);
14511 602 : orig_type0 = type0 = TREE_TYPE (op0);
14512 602 : code0 = TREE_CODE (type0);
14513 602 : converted = 1;
14514 602 : break;
14515 : }
14516 1081 : case stv_secondarg:
14517 1081 : {
14518 1081 : bool maybe_const = true;
14519 1081 : tree sc;
14520 1081 : sc = c_fully_fold (op1, false, &maybe_const);
14521 1081 : sc = save_expr (sc);
14522 1081 : sc = convert (TREE_TYPE (type0), sc);
14523 1081 : op1 = build_vector_from_val (type0, sc);
14524 1081 : if (!maybe_const)
14525 38 : op1 = c_wrap_maybe_const (op1, true);
14526 1081 : orig_type1 = type1 = TREE_TYPE (op1);
14527 1081 : code1 = TREE_CODE (type1);
14528 1081 : converted = 1;
14529 1081 : break;
14530 : }
14531 : default:
14532 : break;
14533 : }
14534 : }
14535 :
14536 16902457 : switch (code)
14537 : {
14538 8662842 : case PLUS_EXPR:
14539 : /* Handle the pointer + int case. */
14540 8662842 : if (code0 == POINTER_TYPE
14541 1254470 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14542 : {
14543 1254464 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14544 1254464 : goto return_build_binary_op;
14545 : }
14546 7408378 : else if (code1 == POINTER_TYPE
14547 1536 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14548 : {
14549 1530 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14550 1530 : goto return_build_binary_op;
14551 : }
14552 : else
14553 : common = 1;
14554 : break;
14555 :
14556 798525 : case MINUS_EXPR:
14557 : /* Subtraction of two similar pointers.
14558 : We must subtract them as integers, then divide by object size. */
14559 798525 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14560 798525 : && comp_target_types (location, type0, type1))
14561 : {
14562 3741 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14563 3741 : goto return_build_binary_op;
14564 : }
14565 : /* Handle pointer minus int. Just like pointer plus int. */
14566 794784 : else if (code0 == POINTER_TYPE
14567 16864 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14568 : {
14569 16852 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14570 16852 : goto return_build_binary_op;
14571 : }
14572 : else
14573 : common = 1;
14574 : break;
14575 :
14576 : case MULT_EXPR:
14577 : common = 1;
14578 : break;
14579 :
14580 275352 : case TRUNC_DIV_EXPR:
14581 275352 : case CEIL_DIV_EXPR:
14582 275352 : case FLOOR_DIV_EXPR:
14583 275352 : case ROUND_DIV_EXPR:
14584 275352 : case EXACT_DIV_EXPR:
14585 275352 : doing_div_or_mod = true;
14586 275352 : warn_for_div_by_zero (location, op1);
14587 :
14588 275351 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14589 49674 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14590 47660 : || code0 == COMPLEX_TYPE
14591 45612 : || gnu_vector_type_p (type0))
14592 325024 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14593 47241 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14594 47074 : || code1 == COMPLEX_TYPE
14595 45614 : || gnu_vector_type_p (type1)))
14596 : {
14597 275347 : enum tree_code tcode0 = code0, tcode1 = code1;
14598 :
14599 275347 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14600 47659 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14601 275347 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14602 47071 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14603 :
14604 275347 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE
14605 69677 : || (tcode0 == ENUMERAL_TYPE && code0 == VECTOR_TYPE))
14606 205671 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE
14607 12988 : || (tcode1 == ENUMERAL_TYPE && code1 == VECTOR_TYPE)))
14608 82663 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14609 : resultcode = RDIV_EXPR;
14610 : else
14611 : /* Although it would be tempting to shorten always here, that
14612 : loses on some targets, since the modulo instruction is
14613 : undefined if the quotient can't be represented in the
14614 : computation mode. We shorten only if unsigned or if
14615 : dividing by something we know != -1. */
14616 192684 : shorten = may_shorten_divmod (op0, op1);
14617 : common = 1;
14618 : }
14619 : break;
14620 :
14621 1589198 : case BIT_AND_EXPR:
14622 1589198 : case BIT_IOR_EXPR:
14623 1589198 : case BIT_XOR_EXPR:
14624 1589198 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14625 1053426 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14626 : shorten = -1;
14627 : /* Allow vector types which are not floating point types. */
14628 535808 : else if (gnu_vector_type_p (type0)
14629 535731 : && gnu_vector_type_p (type1)
14630 535731 : && !VECTOR_FLOAT_TYPE_P (type0)
14631 1071536 : && !VECTOR_FLOAT_TYPE_P (type1))
14632 : common = 1;
14633 : break;
14634 :
14635 58586 : case TRUNC_MOD_EXPR:
14636 58586 : case FLOOR_MOD_EXPR:
14637 58586 : doing_div_or_mod = true;
14638 58586 : warn_for_div_by_zero (location, op1);
14639 :
14640 58586 : if (gnu_vector_type_p (type0)
14641 297 : && gnu_vector_type_p (type1)
14642 297 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14643 58883 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14644 : common = 1;
14645 58289 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14646 58288 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14647 : {
14648 : /* Although it would be tempting to shorten always here, that loses
14649 : on some targets, since the modulo instruction is undefined if the
14650 : quotient can't be represented in the computation mode. We shorten
14651 : only if unsigned or if dividing by something we know != -1. */
14652 58288 : shorten = may_shorten_divmod (op0, op1);
14653 58288 : common = 1;
14654 : }
14655 : break;
14656 :
14657 511727 : case TRUTH_ANDIF_EXPR:
14658 511727 : case TRUTH_ORIF_EXPR:
14659 511727 : case TRUTH_AND_EXPR:
14660 511727 : case TRUTH_OR_EXPR:
14661 511727 : case TRUTH_XOR_EXPR:
14662 511727 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14663 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14664 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14665 0 : || code0 == BITINT_TYPE)
14666 511727 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14667 164 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14668 24 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14669 22 : || code1 == BITINT_TYPE))
14670 : {
14671 : /* Result of these operations is always an int,
14672 : but that does not mean the operands should be
14673 : converted to ints! */
14674 511727 : result_type = integer_type_node;
14675 511727 : if (op0_int_operands)
14676 : {
14677 110469 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14678 110469 : op0 = remove_c_maybe_const_expr (op0);
14679 : }
14680 : else
14681 401258 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14682 511727 : if (op1_int_operands)
14683 : {
14684 71467 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14685 71467 : op1 = remove_c_maybe_const_expr (op1);
14686 : }
14687 : else
14688 440260 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14689 : converted = 1;
14690 : boolean_op = true;
14691 : }
14692 511727 : if (code == TRUTH_ANDIF_EXPR)
14693 : {
14694 250390 : int_const_or_overflow = (int_operands
14695 63281 : && TREE_CODE (orig_op0) == INTEGER_CST
14696 250406 : && (op0 == truthvalue_false_node
14697 13210 : || TREE_CODE (orig_op1) == INTEGER_CST));
14698 : int_const = (int_const_or_overflow
14699 63254 : && !TREE_OVERFLOW (orig_op0)
14700 63254 : && (op0 == truthvalue_false_node
14701 13194 : || !TREE_OVERFLOW (orig_op1)));
14702 : }
14703 324591 : else if (code == TRUTH_ORIF_EXPR)
14704 : {
14705 331271 : int_const_or_overflow = (int_operands
14706 6831 : && TREE_CODE (orig_op0) == INTEGER_CST
14707 331294 : && (op0 == truthvalue_true_node
14708 2146 : || TREE_CODE (orig_op1) == INTEGER_CST));
14709 : int_const = (int_const_or_overflow
14710 6801 : && !TREE_OVERFLOW (orig_op0)
14711 6801 : && (op0 == truthvalue_true_node
14712 2123 : || !TREE_OVERFLOW (orig_op1)));
14713 : }
14714 : break;
14715 :
14716 : /* Shift operations: result has same type as first operand;
14717 : always convert second operand to int.
14718 : Also set SHORT_SHIFT if shifting rightward. */
14719 :
14720 231661 : case RSHIFT_EXPR:
14721 231661 : if (gnu_vector_type_p (type0)
14722 814 : && gnu_vector_type_p (type1)
14723 258 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14724 256 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14725 231917 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14726 : TYPE_VECTOR_SUBPARTS (type1)))
14727 : {
14728 : result_type = type0;
14729 : converted = 1;
14730 : }
14731 231407 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14732 1119 : || code0 == BITINT_TYPE
14733 564 : || (gnu_vector_type_p (type0)
14734 560 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14735 232519 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14736 : {
14737 231398 : doing_shift = true;
14738 231398 : if (TREE_CODE (op1) == INTEGER_CST)
14739 : {
14740 201933 : if (tree_int_cst_sgn (op1) < 0)
14741 : {
14742 269 : int_const = false;
14743 269 : if (c_inhibit_evaluation_warnings == 0)
14744 129 : warning_at (location, OPT_Wshift_count_negative,
14745 : "right shift count is negative");
14746 : }
14747 201664 : else if (code0 == VECTOR_TYPE)
14748 : {
14749 458 : if (compare_tree_int (op1,
14750 458 : TYPE_PRECISION (TREE_TYPE (type0)))
14751 : >= 0)
14752 : {
14753 16 : int_const = false;
14754 16 : if (c_inhibit_evaluation_warnings == 0)
14755 16 : warning_at (location, OPT_Wshift_count_overflow,
14756 : "right shift count >= width of vector element");
14757 : }
14758 : }
14759 : else
14760 : {
14761 201206 : if (!integer_zerop (op1))
14762 200848 : short_shift = 1;
14763 :
14764 201206 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14765 : {
14766 586 : int_const = false;
14767 586 : if (c_inhibit_evaluation_warnings == 0)
14768 52 : warning_at (location, OPT_Wshift_count_overflow,
14769 : "right shift count >= width of type");
14770 : }
14771 : }
14772 : }
14773 :
14774 : /* Use the type of the value to be shifted. */
14775 : result_type = type0;
14776 : /* Avoid converting op1 to result_type later. */
14777 : converted = 1;
14778 : }
14779 : break;
14780 :
14781 805219 : case LSHIFT_EXPR:
14782 805219 : if (gnu_vector_type_p (type0)
14783 596 : && gnu_vector_type_p (type1)
14784 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14785 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14786 805532 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14787 : TYPE_VECTOR_SUBPARTS (type1)))
14788 : {
14789 : result_type = type0;
14790 : converted = 1;
14791 : }
14792 804908 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14793 781 : || code0 == BITINT_TYPE
14794 291 : || (gnu_vector_type_p (type0)
14795 285 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14796 805680 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14797 : {
14798 804894 : doing_shift = true;
14799 804894 : if (TREE_CODE (op0) == INTEGER_CST
14800 440450 : && tree_int_cst_sgn (op0) < 0
14801 805643 : && !TYPE_OVERFLOW_WRAPS (type0))
14802 : {
14803 : /* Don't reject a left shift of a negative value in a context
14804 : where a constant expression is needed in C90. */
14805 712 : if (flag_isoc99)
14806 666 : int_const = false;
14807 712 : if (c_inhibit_evaluation_warnings == 0)
14808 712 : warning_at (location, OPT_Wshift_negative_value,
14809 : "left shift of negative value");
14810 : }
14811 804894 : if (TREE_CODE (op1) == INTEGER_CST)
14812 : {
14813 732386 : if (tree_int_cst_sgn (op1) < 0)
14814 : {
14815 313 : int_const = false;
14816 313 : if (c_inhibit_evaluation_warnings == 0)
14817 135 : warning_at (location, OPT_Wshift_count_negative,
14818 : "left shift count is negative");
14819 : }
14820 732073 : else if (code0 == VECTOR_TYPE)
14821 : {
14822 216 : if (compare_tree_int (op1,
14823 216 : TYPE_PRECISION (TREE_TYPE (type0)))
14824 : >= 0)
14825 : {
14826 6 : int_const = false;
14827 6 : if (c_inhibit_evaluation_warnings == 0)
14828 6 : warning_at (location, OPT_Wshift_count_overflow,
14829 : "left shift count >= width of vector element");
14830 : }
14831 : }
14832 731857 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14833 : {
14834 29733 : int_const = false;
14835 29733 : if (c_inhibit_evaluation_warnings == 0)
14836 113 : warning_at (location, OPT_Wshift_count_overflow,
14837 : "left shift count >= width of type");
14838 : }
14839 702124 : else if (TREE_CODE (op0) == INTEGER_CST
14840 366623 : && maybe_warn_shift_overflow (location, op0, op1)
14841 702752 : && flag_isoc99)
14842 : int_const = false;
14843 : }
14844 :
14845 : /* Use the type of the value to be shifted. */
14846 : result_type = type0;
14847 : /* Avoid converting op1 to result_type later. */
14848 : converted = 1;
14849 : }
14850 : break;
14851 :
14852 1746687 : case EQ_EXPR:
14853 1746687 : case NE_EXPR:
14854 1746687 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14855 : {
14856 41923 : tree intt;
14857 41923 : if (!vector_types_compatible_elements_p (type0, type1))
14858 : {
14859 4 : error_at (location, "comparing vectors with different "
14860 : "element types");
14861 4 : return error_mark_node;
14862 : }
14863 :
14864 41919 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14865 83838 : TYPE_VECTOR_SUBPARTS (type1)))
14866 : {
14867 1 : error_at (location, "comparing vectors with different "
14868 : "number of elements");
14869 1 : return error_mark_node;
14870 : }
14871 :
14872 : /* It's not precisely specified how the usual arithmetic
14873 : conversions apply to the vector types. Here, we use
14874 : the unsigned type if one of the operands is signed and
14875 : the other one is unsigned. */
14876 41918 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14877 : {
14878 4 : if (!TYPE_UNSIGNED (type0))
14879 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14880 : else
14881 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14882 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14883 : "types %qT and %qT", type0, type1);
14884 : }
14885 :
14886 : /* Always construct signed integer vector type. */
14887 41918 : if (VECTOR_BOOLEAN_TYPE_P (type0) && VECTOR_BOOLEAN_TYPE_P (type1))
14888 : result_type = type0;
14889 : else
14890 : {
14891 41918 : auto nelts = TYPE_VECTOR_SUBPARTS (type0);
14892 :
14893 125754 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14894 83836 : (SCALAR_TYPE_MODE
14895 : (TREE_TYPE (type0))), 0);
14896 41918 : if (!intt)
14897 : {
14898 0 : error_at (location, "could not find an integer type "
14899 : "of the same size as %qT",
14900 0 : TREE_TYPE (type0));
14901 0 : return error_mark_node;
14902 : }
14903 41918 : result_type = build_opaque_vector_type (intt, nelts);
14904 : }
14905 41918 : converted = 1;
14906 41918 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14907 41918 : goto return_build_binary_op;
14908 : }
14909 1704764 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14910 254789 : warning_at (location,
14911 254789 : OPT_Wfloat_equal,
14912 : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14913 : /* Result of comparison is always int,
14914 : but don't convert the args to int! */
14915 1704764 : build_type = integer_type_node;
14916 1704764 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14917 158766 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14918 1581561 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14919 : || code1 == BITINT_TYPE
14920 : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14921 : short_compare = 1;
14922 123469 : else if (code0 == POINTER_TYPE
14923 123469 : && (code1 == NULLPTR_TYPE
14924 123092 : || null_pointer_constant_p (orig_op1)))
14925 : {
14926 51920 : maybe_warn_for_null_address (location, op0, code);
14927 51920 : result_type = type0;
14928 : }
14929 71549 : else if (code1 == POINTER_TYPE
14930 71549 : && (code0 == NULLPTR_TYPE
14931 71447 : || null_pointer_constant_p (orig_op0)))
14932 : {
14933 294 : maybe_warn_for_null_address (location, op1, code);
14934 294 : result_type = type1;
14935 : }
14936 71255 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14937 : {
14938 71165 : tree tt0 = TREE_TYPE (type0);
14939 71165 : tree tt1 = TREE_TYPE (type1);
14940 71165 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14941 71165 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14942 71165 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14943 :
14944 : /* Anything compares with void *. void * compares with anything.
14945 : Otherwise, the targets must be compatible
14946 : and both must be object or both incomplete. */
14947 71165 : if (comp_target_types (location, type0, type1))
14948 49830 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14949 21335 : else if (!addr_space_superset (as0, as1, &as_common))
14950 : {
14951 0 : error_at (location, "comparison of pointers to "
14952 : "disjoint address spaces");
14953 0 : return error_mark_node;
14954 : }
14955 21335 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14956 : {
14957 21105 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14958 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14959 : "comparison of %<void *%> with function pointer");
14960 : }
14961 230 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14962 : {
14963 182 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14964 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14965 : "comparison of %<void *%> with function pointer");
14966 : }
14967 : else
14968 : /* Avoid warning about the volatile ObjC EH puts on decls. */
14969 48 : if (!objc_ok)
14970 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14971 : "comparison of distinct pointer types lacks a cast");
14972 :
14973 49886 : if (result_type == NULL_TREE)
14974 : {
14975 21335 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14976 21335 : result_type = c_build_pointer_type
14977 21335 : (c_build_qualified_type (void_type_node, qual));
14978 : }
14979 : }
14980 90 : else if (code0 == POINTER_TYPE
14981 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14982 : {
14983 26 : result_type = type0;
14984 26 : pedwarn (location, 0, "comparison between pointer and integer");
14985 : }
14986 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14987 21 : && code1 == POINTER_TYPE)
14988 : {
14989 8 : result_type = type1;
14990 8 : pedwarn (location, 0, "comparison between pointer and integer");
14991 : }
14992 : /* 6.5.9: One of the following shall hold:
14993 : -- both operands have type nullptr_t; */
14994 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14995 : {
14996 22 : result_type = nullptr_type_node;
14997 : /* No need to convert the operands to result_type later. */
14998 22 : converted = 1;
14999 : }
15000 : /* -- one operand has type nullptr_t and the other is a null pointer
15001 : constant. We will have to convert the former to the type of the
15002 : latter, because during gimplification we can't have mismatching
15003 : comparison operand type. We convert from nullptr_t to the other
15004 : type, since only nullptr_t can be converted to nullptr_t. Also,
15005 : even a constant 0 is a null pointer constant, so we may have to
15006 : create a pointer type from its type. */
15007 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
15008 19 : result_type = (INTEGRAL_TYPE_P (type1)
15009 19 : ? c_build_pointer_type (type1) : type1);
15010 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
15011 11 : result_type = (INTEGRAL_TYPE_P (type0)
15012 11 : ? c_build_pointer_type (type0) : type0);
15013 5039461 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15014 3334672 : || truth_value_p (TREE_CODE (orig_op0)))
15015 3402242 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15016 3402238 : || truth_value_p (TREE_CODE (orig_op1))))
15017 71794 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15018 : break;
15019 :
15020 1006387 : case LE_EXPR:
15021 1006387 : case GE_EXPR:
15022 1006387 : case LT_EXPR:
15023 1006387 : case GT_EXPR:
15024 1006387 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
15025 : {
15026 59194 : tree intt;
15027 59194 : if (!vector_types_compatible_elements_p (type0, type1))
15028 : {
15029 5 : error_at (location, "comparing vectors with different "
15030 : "element types");
15031 5 : return error_mark_node;
15032 : }
15033 :
15034 59189 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
15035 118378 : TYPE_VECTOR_SUBPARTS (type1)))
15036 : {
15037 0 : error_at (location, "comparing vectors with different "
15038 : "number of elements");
15039 0 : return error_mark_node;
15040 : }
15041 :
15042 : /* It's not precisely specified how the usual arithmetic
15043 : conversions apply to the vector types. Here, we use
15044 : the unsigned type if one of the operands is signed and
15045 : the other one is unsigned. */
15046 59189 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
15047 : {
15048 15 : if (!TYPE_UNSIGNED (type0))
15049 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
15050 : else
15051 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
15052 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
15053 : "types %qT and %qT", type0, type1);
15054 : }
15055 :
15056 : /* Always construct signed integer vector type. */
15057 177567 : intt = c_common_type_for_size (GET_MODE_BITSIZE
15058 118378 : (SCALAR_TYPE_MODE
15059 : (TREE_TYPE (type0))), 0);
15060 59189 : if (!intt)
15061 : {
15062 0 : error_at (location, "could not find an integer type "
15063 : "of the same size as %qT",
15064 0 : TREE_TYPE (type0));
15065 0 : return error_mark_node;
15066 : }
15067 59189 : result_type = build_opaque_vector_type (intt,
15068 59189 : TYPE_VECTOR_SUBPARTS (type0));
15069 59189 : converted = 1;
15070 59189 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
15071 59189 : goto return_build_binary_op;
15072 : }
15073 947193 : build_type = integer_type_node;
15074 947193 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
15075 65088 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
15076 882654 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
15077 : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
15078 : short_compare = 1;
15079 64571 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
15080 : {
15081 64504 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
15082 64504 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
15083 64504 : addr_space_t as_common;
15084 :
15085 64504 : if (comp_target_types (location, type0, type1))
15086 : {
15087 64339 : result_type = common_pointer_type (type0, type1, NULL_TREE);
15088 64339 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
15089 64339 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
15090 32 : pedwarn_c99 (location, OPT_Wpedantic,
15091 : "comparison of complete and incomplete pointers");
15092 64307 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
15093 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
15094 : "ordered comparisons of pointers to functions");
15095 64307 : else if (null_pointer_constant_p (orig_op0)
15096 64307 : || null_pointer_constant_p (orig_op1))
15097 8 : warning_at (location, OPT_Wextra,
15098 : "ordered comparison of pointer with null pointer");
15099 :
15100 : }
15101 165 : else if (!addr_space_superset (as0, as1, &as_common))
15102 : {
15103 0 : error_at (location, "comparison of pointers to "
15104 : "disjoint address spaces");
15105 0 : return error_mark_node;
15106 : }
15107 : else
15108 : {
15109 165 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
15110 165 : result_type = c_build_pointer_type
15111 165 : (c_build_qualified_type (void_type_node, qual));
15112 165 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
15113 : "comparison of distinct pointer types lacks a cast");
15114 : }
15115 : }
15116 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
15117 : {
15118 17 : result_type = type0;
15119 17 : if (pedantic)
15120 11 : pedwarn (location, OPT_Wpedantic,
15121 : "ordered comparison of pointer with integer zero");
15122 6 : else if (extra_warnings)
15123 1 : warning_at (location, OPT_Wextra,
15124 : "ordered comparison of pointer with integer zero");
15125 : }
15126 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
15127 : {
15128 12 : result_type = type1;
15129 12 : if (pedantic)
15130 2 : pedwarn (location, OPT_Wpedantic,
15131 : "ordered comparison of pointer with integer zero");
15132 10 : else if (extra_warnings)
15133 1 : warning_at (location, OPT_Wextra,
15134 : "ordered comparison of pointer with integer zero");
15135 : }
15136 38 : else if (code0 == POINTER_TYPE
15137 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
15138 : {
15139 18 : result_type = type0;
15140 18 : pedwarn (location, 0, "comparison between pointer and integer");
15141 : }
15142 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
15143 20 : && code1 == POINTER_TYPE)
15144 : {
15145 18 : result_type = type1;
15146 18 : pedwarn (location, 0, "comparison between pointer and integer");
15147 : }
15148 :
15149 947193 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
15150 64569 : && current_function_decl != NULL_TREE
15151 1011751 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
15152 : {
15153 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
15154 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
15155 :
15156 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
15157 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
15158 : }
15159 :
15160 2841487 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
15161 1894294 : || truth_value_p (TREE_CODE (orig_op0)))
15162 1894311 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
15163 1894311 : || truth_value_p (TREE_CODE (orig_op1))))
15164 517 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
15165 : break;
15166 :
15167 43 : case MIN_EXPR:
15168 43 : case MAX_EXPR:
15169 : /* Used for OpenMP atomics. */
15170 43 : gcc_assert (flag_openmp);
15171 : common = 1;
15172 : break;
15173 :
15174 0 : default:
15175 0 : gcc_unreachable ();
15176 : }
15177 :
15178 15524752 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
15179 0 : return error_mark_node;
15180 :
15181 15524752 : if (gnu_vector_type_p (type0)
15182 1480409 : && gnu_vector_type_p (type1)
15183 17004324 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
15184 1479552 : || !vector_types_compatible_elements_p (type0, type1)))
15185 : {
15186 26 : gcc_rich_location richloc (location);
15187 26 : maybe_range_label_for_tree_type_mismatch
15188 26 : label_for_op0 (orig_op0, orig_op1),
15189 26 : label_for_op1 (orig_op1, orig_op0);
15190 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
15191 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
15192 26 : binary_op_error (&richloc, code, type0, type1);
15193 26 : return error_mark_node;
15194 26 : }
15195 :
15196 15524726 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
15197 1737092 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
15198 1668540 : || gnu_vector_type_p (type0))
15199 17073661 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
15200 1573654 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
15201 1481676 : || gnu_vector_type_p (type1)))
15202 : {
15203 15334444 : bool first_complex = (code0 == COMPLEX_TYPE);
15204 15334444 : bool second_complex = (code1 == COMPLEX_TYPE);
15205 15334444 : int none_complex = (!first_complex && !second_complex);
15206 :
15207 15334444 : if (shorten || common || short_compare)
15208 : {
15209 13787596 : result_type = c_common_type (type0, type1);
15210 13787596 : do_warn_double_promotion (result_type, type0, type1,
15211 : "implicit conversion from %qT to %qT "
15212 : "to match other operand of binary "
15213 : "expression",
15214 : location);
15215 13787596 : if (result_type == error_mark_node)
15216 : return error_mark_node;
15217 : }
15218 :
15219 15334422 : if (first_complex != second_complex
15220 82077 : && (code == PLUS_EXPR
15221 : || code == MINUS_EXPR
15222 82077 : || code == MULT_EXPR
15223 17526 : || (code == TRUNC_DIV_EXPR && first_complex))
15224 65965 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15225 15396676 : && flag_signed_zeros)
15226 : {
15227 : /* An operation on mixed real/complex operands must be
15228 : handled specially, but the language-independent code can
15229 : more easily optimize the plain complex arithmetic if
15230 : -fno-signed-zeros. */
15231 61942 : tree real_type = TREE_TYPE (result_type);
15232 61942 : tree real, imag;
15233 61942 : if (type0 != orig_type0 || type1 != orig_type1)
15234 : {
15235 94 : gcc_assert (may_need_excess_precision && common);
15236 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15237 : }
15238 61942 : if (first_complex)
15239 : {
15240 8253 : if (TREE_TYPE (op0) != result_type)
15241 1787 : op0 = convert_and_check (location, result_type, op0);
15242 8253 : if (TREE_TYPE (op1) != real_type)
15243 4601 : op1 = convert_and_check (location, real_type, op1);
15244 : }
15245 : else
15246 : {
15247 53689 : if (TREE_TYPE (op0) != real_type)
15248 2698 : op0 = convert_and_check (location, real_type, op0);
15249 53689 : if (TREE_TYPE (op1) != result_type)
15250 1866 : op1 = convert_and_check (location, result_type, op1);
15251 : }
15252 61942 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15253 0 : return error_mark_node;
15254 61942 : if (first_complex)
15255 : {
15256 8253 : op0 = save_expr (op0);
15257 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15258 : op0, true);
15259 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15260 : op0, true);
15261 8253 : switch (code)
15262 : {
15263 3488 : case MULT_EXPR:
15264 3488 : case TRUNC_DIV_EXPR:
15265 3488 : op1 = save_expr (op1);
15266 3488 : imag = build2 (resultcode, real_type, imag, op1);
15267 : /* Fall through. */
15268 8253 : case PLUS_EXPR:
15269 8253 : case MINUS_EXPR:
15270 8253 : real = build2 (resultcode, real_type, real, op1);
15271 8253 : break;
15272 0 : default:
15273 0 : gcc_unreachable();
15274 : }
15275 : }
15276 : else
15277 : {
15278 53689 : op1 = save_expr (op1);
15279 53689 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15280 : op1, true);
15281 53689 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15282 : op1, true);
15283 53689 : switch (code)
15284 : {
15285 2008 : case MULT_EXPR:
15286 2008 : op0 = save_expr (op0);
15287 2008 : imag = build2 (resultcode, real_type, op0, imag);
15288 : /* Fall through. */
15289 31957 : case PLUS_EXPR:
15290 31957 : real = build2 (resultcode, real_type, op0, real);
15291 31957 : break;
15292 21732 : case MINUS_EXPR:
15293 21732 : real = build2 (resultcode, real_type, op0, real);
15294 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15295 21732 : break;
15296 0 : default:
15297 0 : gcc_unreachable();
15298 : }
15299 : }
15300 61942 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15301 61942 : goto return_build_binary_op;
15302 : }
15303 :
15304 : /* For certain operations (which identify themselves by shorten != 0)
15305 : if both args were extended from the same smaller type,
15306 : do the arithmetic in that type and then extend.
15307 :
15308 : shorten !=0 and !=1 indicates a bitwise operation.
15309 : For them, this optimization is safe only if
15310 : both args are zero-extended or both are sign-extended.
15311 : Otherwise, we might change the result.
15312 : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15313 : but calculated in (unsigned short) it would be (unsigned short)-1. */
15314 :
15315 15272480 : if (shorten && none_complex)
15316 : {
15317 1297650 : final_type = result_type;
15318 1297650 : result_type = shorten_binary_op (result_type, op0, op1,
15319 : shorten == -1);
15320 : }
15321 :
15322 : /* Shifts can be shortened if shifting right. */
15323 :
15324 15272480 : if (short_shift)
15325 : {
15326 200848 : int unsigned_arg;
15327 200848 : tree arg0 = get_narrower (op0, &unsigned_arg);
15328 :
15329 200848 : final_type = result_type;
15330 :
15331 200848 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15332 141321 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15333 :
15334 200848 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15335 2998 : && tree_int_cst_sgn (op1) > 0
15336 : /* We can shorten only if the shift count is less than the
15337 : number of bits in the smaller type size. */
15338 2998 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15339 : /* We cannot drop an unsigned shift after sign-extension. */
15340 203706 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15341 : {
15342 : /* Do an unsigned shift if the operand was zero-extended. */
15343 2843 : result_type
15344 2843 : = c_common_signed_or_unsigned_type (unsigned_arg,
15345 2843 : TREE_TYPE (arg0));
15346 : /* Convert value-to-be-shifted to that type. */
15347 2843 : if (TREE_TYPE (op0) != result_type)
15348 2843 : op0 = convert (result_type, op0);
15349 : converted = 1;
15350 : }
15351 : }
15352 :
15353 : /* Comparison operations are shortened too but differently.
15354 : They identify themselves by setting short_compare = 1. */
15355 :
15356 15272480 : if (short_compare)
15357 : {
15358 : /* Don't write &op0, etc., because that would prevent op0
15359 : from being kept in a register.
15360 : Instead, make copies of the our local variables and
15361 : pass the copies by reference, then copy them back afterward. */
15362 2463915 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15363 2463915 : enum tree_code xresultcode = resultcode;
15364 2463915 : tree val
15365 2463915 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15366 : &xresultcode);
15367 :
15368 2463915 : if (val != NULL_TREE)
15369 : {
15370 15889 : ret = val;
15371 15889 : goto return_build_binary_op;
15372 : }
15373 :
15374 2448026 : op0 = xop0, op1 = xop1;
15375 2448026 : converted = 1;
15376 2448026 : resultcode = xresultcode;
15377 :
15378 2448026 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15379 : {
15380 2300483 : bool op0_maybe_const = true;
15381 2300483 : bool op1_maybe_const = true;
15382 2300483 : tree orig_op0_folded, orig_op1_folded;
15383 :
15384 2300483 : if (in_late_binary_op)
15385 : {
15386 : orig_op0_folded = orig_op0;
15387 : orig_op1_folded = orig_op1;
15388 : }
15389 : else
15390 : {
15391 : /* Fold for the sake of possible warnings, as in
15392 : build_conditional_expr. This requires the
15393 : "original" values to be folded, not just op0 and
15394 : op1. */
15395 2291912 : c_inhibit_evaluation_warnings++;
15396 2291912 : op0 = c_fully_fold (op0, require_constant_value,
15397 : &op0_maybe_const);
15398 2291912 : op1 = c_fully_fold (op1, require_constant_value,
15399 : &op1_maybe_const);
15400 2291912 : c_inhibit_evaluation_warnings--;
15401 2291912 : orig_op0_folded = c_fully_fold (orig_op0,
15402 : require_constant_value,
15403 : NULL);
15404 2291912 : orig_op1_folded = c_fully_fold (orig_op1,
15405 : require_constant_value,
15406 : NULL);
15407 : }
15408 :
15409 2300483 : if (warn_sign_compare)
15410 377391 : warn_for_sign_compare (location, orig_op0_folded,
15411 : orig_op1_folded, op0, op1,
15412 : result_type, resultcode);
15413 2300483 : if (!in_late_binary_op && !int_operands)
15414 : {
15415 2114255 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15416 2110745 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15417 2114255 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15418 829224 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15419 : }
15420 : }
15421 : }
15422 : }
15423 :
15424 : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15425 : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15426 : Then the expression will be built.
15427 : It will be given type FINAL_TYPE if that is nonzero;
15428 : otherwise, it will be given type RESULT_TYPE. */
15429 :
15430 15446873 : if (!result_type)
15431 : {
15432 : /* Favor showing any expression locations that are available. */
15433 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15434 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15435 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15436 514 : return error_mark_node;
15437 514 : }
15438 :
15439 15446359 : if (build_type == NULL_TREE)
15440 : {
15441 12810299 : build_type = result_type;
15442 12810299 : if ((type0 != orig_type0 || type1 != orig_type1)
15443 797 : && !boolean_op)
15444 : {
15445 797 : gcc_assert (may_need_excess_precision && common);
15446 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15447 : }
15448 : }
15449 :
15450 15446359 : if (!converted)
15451 : {
15452 11448488 : op0 = ep_convert_and_check (location, result_type, op0,
15453 : semantic_result_type);
15454 11448488 : op1 = ep_convert_and_check (location, result_type, op1,
15455 : semantic_result_type);
15456 :
15457 : /* This can happen if one operand has a vector type, and the other
15458 : has a different type. */
15459 11448488 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15460 3 : return error_mark_node;
15461 : }
15462 :
15463 15446356 : if (sanitize_flags_p ((SANITIZE_SHIFT
15464 : | SANITIZE_DIVIDE
15465 : | SANITIZE_FLOAT_DIVIDE
15466 : | SANITIZE_SI_OVERFLOW))
15467 23386 : && current_function_decl != NULL_TREE
15468 19923 : && (doing_div_or_mod || doing_shift)
15469 15449281 : && !require_constant_value)
15470 : {
15471 : /* OP0 and/or OP1 might have side-effects. */
15472 2869 : op0 = save_expr (op0);
15473 2869 : op1 = save_expr (op1);
15474 2869 : op0 = c_fully_fold (op0, false, NULL);
15475 2869 : op1 = c_fully_fold (op1, false, NULL);
15476 2869 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15477 : | SANITIZE_FLOAT_DIVIDE
15478 : | SANITIZE_SI_OVERFLOW))))
15479 912 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15480 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15481 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15482 : }
15483 :
15484 : /* Treat expressions in initializers specially as they can't trap. */
15485 15446356 : if (int_const_or_overflow)
15486 7968377 : ret = (require_constant_value
15487 7968377 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15488 : op0, op1)
15489 7922256 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15490 : else
15491 7477979 : ret = build2 (resultcode, build_type, op0, op1);
15492 15446356 : if (final_type != NULL_TREE)
15493 1498498 : ret = convert (final_type, ret);
15494 :
15495 13947858 : return_build_binary_op:
15496 16901881 : gcc_assert (ret != error_mark_node);
15497 16901881 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15498 1854 : ret = (int_operands
15499 1854 : ? note_integer_operands (ret)
15500 532 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15501 16900027 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15502 60309 : && !in_late_binary_op)
15503 60309 : ret = note_integer_operands (ret);
15504 16901881 : protected_set_expr_location (ret, location);
15505 :
15506 16901881 : if (instrument_expr != NULL)
15507 1420 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15508 : instrument_expr, ret);
15509 :
15510 16901881 : if (semantic_result_type)
15511 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15512 : semantic_result_type, ret);
15513 :
15514 : return ret;
15515 : }
15516 :
15517 :
15518 : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15519 : purpose. LOCATION is the source location for the expression. */
15520 :
15521 : tree
15522 4148937 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15523 : {
15524 4148937 : bool int_const, int_operands;
15525 :
15526 4148937 : switch (TREE_CODE (TREE_TYPE (expr)))
15527 : {
15528 72 : case ARRAY_TYPE:
15529 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15530 72 : return error_mark_node;
15531 :
15532 86 : case RECORD_TYPE:
15533 86 : error_at (location, "used struct type value where scalar is required");
15534 86 : return error_mark_node;
15535 :
15536 83 : case UNION_TYPE:
15537 83 : error_at (location, "used union type value where scalar is required");
15538 83 : return error_mark_node;
15539 :
15540 22 : case VOID_TYPE:
15541 22 : error_at (location, "void value not ignored as it ought to be");
15542 22 : return error_mark_node;
15543 :
15544 30901 : case POINTER_TYPE:
15545 30901 : if (reject_gcc_builtin (expr))
15546 3 : return error_mark_node;
15547 : break;
15548 :
15549 0 : case FUNCTION_TYPE:
15550 0 : gcc_unreachable ();
15551 :
15552 8 : case VECTOR_TYPE:
15553 8 : error_at (location, "used vector type where scalar is required");
15554 8 : return error_mark_node;
15555 :
15556 : default:
15557 : break;
15558 : }
15559 :
15560 : /* Conversion of a floating constant to boolean goes through here
15561 : and yields an integer constant expression. Otherwise, the result
15562 : is only an integer constant expression if the argument is. */
15563 899809 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15564 4148739 : || ((TREE_CODE (expr) == REAL_CST
15565 3248556 : || TREE_CODE (expr) == COMPLEX_CST)
15566 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15567 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15568 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15569 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15570 : == BOOLEAN_TYPE)))));
15571 4148663 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15572 899969 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15573 : {
15574 319 : expr = remove_c_maybe_const_expr (expr);
15575 319 : expr = build2 (NE_EXPR, type, expr,
15576 319 : convert (TREE_TYPE (expr), integer_zero_node));
15577 319 : expr = note_integer_operands (expr);
15578 : }
15579 : else
15580 : {
15581 : /* ??? Should we also give an error for vectors rather than leaving
15582 : those to give errors later? */
15583 4148344 : expr = c_common_truthvalue_conversion (location, expr);
15584 4148344 : expr = fold_convert_loc (location, type, expr);
15585 : }
15586 :
15587 4148663 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15588 : {
15589 76 : if (TREE_OVERFLOW (expr))
15590 : return expr;
15591 : else
15592 76 : return note_integer_operands (expr);
15593 : }
15594 4148587 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15595 857 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15596 : return expr;
15597 : }
15598 :
15599 :
15600 : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15601 : required. */
15602 :
15603 : tree
15604 125146174 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15605 : {
15606 125146174 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15607 : {
15608 699 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15609 : /* Executing a compound literal inside a function reinitializes
15610 : it. */
15611 699 : if (!TREE_STATIC (decl))
15612 433 : *se = true;
15613 699 : return decl;
15614 : }
15615 : else
15616 : return expr;
15617 : }
15618 :
15619 : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15620 : statement. LOC is the location of the construct. */
15621 :
15622 : tree
15623 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15624 : tree clauses)
15625 : {
15626 2154 : body = c_end_compound_stmt (loc, body, true);
15627 :
15628 2154 : tree stmt = make_node (code);
15629 2154 : TREE_TYPE (stmt) = void_type_node;
15630 2154 : OMP_BODY (stmt) = body;
15631 2154 : OMP_CLAUSES (stmt) = clauses;
15632 2154 : SET_EXPR_LOCATION (stmt, loc);
15633 :
15634 2154 : return add_stmt (stmt);
15635 : }
15636 :
15637 : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15638 : statement. LOC is the location of the OACC_DATA. */
15639 :
15640 : tree
15641 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15642 : {
15643 497 : tree stmt;
15644 :
15645 497 : block = c_end_compound_stmt (loc, block, true);
15646 :
15647 497 : stmt = make_node (OACC_DATA);
15648 497 : TREE_TYPE (stmt) = void_type_node;
15649 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15650 497 : OACC_DATA_BODY (stmt) = block;
15651 497 : SET_EXPR_LOCATION (stmt, loc);
15652 :
15653 497 : return add_stmt (stmt);
15654 : }
15655 :
15656 : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15657 : statement. LOC is the location of the OACC_HOST_DATA. */
15658 :
15659 : tree
15660 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15661 : {
15662 21 : tree stmt;
15663 :
15664 21 : block = c_end_compound_stmt (loc, block, true);
15665 :
15666 21 : stmt = make_node (OACC_HOST_DATA);
15667 21 : TREE_TYPE (stmt) = void_type_node;
15668 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15669 21 : OACC_HOST_DATA_BODY (stmt) = block;
15670 21 : SET_EXPR_LOCATION (stmt, loc);
15671 :
15672 21 : return add_stmt (stmt);
15673 : }
15674 :
15675 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15676 :
15677 : tree
15678 12025 : c_begin_omp_parallel (void)
15679 : {
15680 12025 : tree block;
15681 :
15682 12025 : keep_next_level ();
15683 12025 : block = c_begin_compound_stmt (true);
15684 :
15685 12025 : return block;
15686 : }
15687 :
15688 : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15689 : statement. LOC is the location of the OMP_PARALLEL. */
15690 :
15691 : tree
15692 5690 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15693 : {
15694 5690 : tree stmt;
15695 :
15696 5690 : block = c_end_compound_stmt (loc, block, true);
15697 :
15698 5690 : stmt = make_node (OMP_PARALLEL);
15699 5690 : TREE_TYPE (stmt) = void_type_node;
15700 5690 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15701 5690 : OMP_PARALLEL_BODY (stmt) = block;
15702 5690 : SET_EXPR_LOCATION (stmt, loc);
15703 :
15704 5690 : return add_stmt (stmt);
15705 : }
15706 :
15707 : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15708 :
15709 : tree
15710 898 : c_begin_omp_task (void)
15711 : {
15712 898 : tree block;
15713 :
15714 898 : keep_next_level ();
15715 898 : block = c_begin_compound_stmt (true);
15716 :
15717 898 : return block;
15718 : }
15719 :
15720 : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15721 : statement. LOC is the location of the #pragma. */
15722 :
15723 : tree
15724 898 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15725 : {
15726 898 : tree stmt;
15727 :
15728 898 : block = c_end_compound_stmt (loc, block, true);
15729 :
15730 898 : stmt = make_node (OMP_TASK);
15731 898 : TREE_TYPE (stmt) = void_type_node;
15732 898 : OMP_TASK_CLAUSES (stmt) = clauses;
15733 898 : OMP_TASK_BODY (stmt) = block;
15734 898 : SET_EXPR_LOCATION (stmt, loc);
15735 :
15736 898 : return add_stmt (stmt);
15737 : }
15738 :
15739 : /* Generate GOMP_cancel call for #pragma omp cancel. */
15740 :
15741 : void
15742 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15743 : {
15744 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15745 213 : int mask = 0;
15746 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15747 : mask = 1;
15748 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15749 : mask = 2;
15750 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15751 : mask = 4;
15752 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15753 : mask = 8;
15754 : else
15755 : {
15756 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15757 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15758 : "clauses");
15759 0 : return;
15760 : }
15761 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15762 213 : if (ifc != NULL_TREE)
15763 : {
15764 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15765 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15766 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15767 : "expected %<cancel%> %<if%> clause modifier");
15768 : else
15769 : {
15770 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15771 29 : if (ifc2 != NULL_TREE)
15772 : {
15773 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15774 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15775 : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15776 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15777 : "expected %<cancel%> %<if%> clause modifier");
15778 : }
15779 : }
15780 :
15781 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15782 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15783 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15784 : build_zero_cst (type));
15785 : }
15786 : else
15787 182 : ifc = boolean_true_node;
15788 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15789 213 : build_int_cst (integer_type_node, mask),
15790 : ifc);
15791 213 : add_stmt (stmt);
15792 : }
15793 :
15794 : /* Generate GOMP_cancellation_point call for
15795 : #pragma omp cancellation point. */
15796 :
15797 : void
15798 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15799 : {
15800 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15801 167 : int mask = 0;
15802 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15803 : mask = 1;
15804 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15805 : mask = 2;
15806 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15807 : mask = 4;
15808 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15809 : mask = 8;
15810 : else
15811 : {
15812 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15813 : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15814 : "clauses");
15815 1 : return;
15816 : }
15817 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15818 166 : build_int_cst (integer_type_node, mask));
15819 166 : add_stmt (stmt);
15820 : }
15821 :
15822 : /* Helper function for handle_omp_array_sections. Called recursively
15823 : to handle multiple array-section-subscripts. C is the clause,
15824 : T current expression (initially OMP_CLAUSE_DECL), which is either
15825 : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15826 : expression if specified, TREE_VALUE length expression if specified,
15827 : TREE_CHAIN is what it has been specified after, or some decl.
15828 : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15829 : set to true if any of the array-section-subscript could have length
15830 : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15831 : first array-section-subscript which is known not to have length
15832 : of one. Given say:
15833 : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15834 : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15835 : all are or may have length of 1, array-section-subscript [:2] is the
15836 : first one known not to have length 1. For array-section-subscript
15837 : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15838 : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15839 : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15840 : case though, as some lengths could be zero. */
15841 :
15842 : static tree
15843 6398 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15844 : bool &maybe_zero_len, unsigned int &first_non_one,
15845 : enum c_omp_region_type ort)
15846 : {
15847 6398 : tree ret, low_bound, length, type;
15848 6398 : bool openacc = (ort & C_ORT_ACC) != 0;
15849 6398 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15850 : {
15851 2980 : if (error_operand_p (t))
15852 2 : return error_mark_node;
15853 2978 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15854 2978 : ret = t;
15855 2978 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15856 2879 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15857 5543 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15858 : {
15859 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15860 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15861 6 : return error_mark_node;
15862 : }
15863 2972 : if (!ai.check_clause (c))
15864 0 : return error_mark_node;
15865 2972 : else if (ai.component_access_p ()
15866 3275 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15867 16 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15868 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15869 303 : t = ai.get_root_term (true);
15870 : else
15871 2669 : t = ai.unconverted_ref_origin ();
15872 2972 : if (t == error_mark_node)
15873 : return error_mark_node;
15874 2972 : if (!VAR_P (t)
15875 741 : && (ort == C_ORT_ACC || !EXPR_P (t))
15876 738 : && TREE_CODE (t) != PARM_DECL)
15877 : {
15878 5 : if (DECL_P (t))
15879 5 : error_at (OMP_CLAUSE_LOCATION (c),
15880 : "%qD is not a variable in %qs clause", t,
15881 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15882 : else
15883 0 : error_at (OMP_CLAUSE_LOCATION (c),
15884 : "%qE is not a variable in %qs clause", t,
15885 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15886 5 : return error_mark_node;
15887 : }
15888 2967 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15889 2869 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15890 5523 : && TYPE_ATOMIC (TREE_TYPE (t)))
15891 : {
15892 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15893 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15894 0 : return error_mark_node;
15895 : }
15896 2967 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15897 2869 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15898 2556 : && VAR_P (t)
15899 4975 : && DECL_THREAD_LOCAL_P (t))
15900 : {
15901 3 : error_at (OMP_CLAUSE_LOCATION (c),
15902 : "%qD is threadprivate variable in %qs clause", t,
15903 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15904 3 : return error_mark_node;
15905 : }
15906 2964 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15907 2866 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15908 411 : && TYPE_ATOMIC (TREE_TYPE (t))
15909 2966 : && POINTER_TYPE_P (TREE_TYPE (t)))
15910 : {
15911 : /* If the array section is pointer based and the pointer
15912 : itself is _Atomic qualified, we need to atomically load
15913 : the pointer. */
15914 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15915 : ret, false, false);
15916 : }
15917 2964 : return ret;
15918 2978 : }
15919 :
15920 3418 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15921 : maybe_zero_len, first_non_one, ort);
15922 3418 : if (ret == error_mark_node || ret == NULL_TREE)
15923 : return ret;
15924 :
15925 3353 : type = TREE_TYPE (ret);
15926 3353 : low_bound = TREE_OPERAND (t, 1);
15927 3353 : length = TREE_OPERAND (t, 2);
15928 :
15929 3353 : if (low_bound == error_mark_node || length == error_mark_node)
15930 : return error_mark_node;
15931 :
15932 3353 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15933 : {
15934 13 : error_at (OMP_CLAUSE_LOCATION (c),
15935 : "low bound %qE of array section does not have integral type",
15936 : low_bound);
15937 13 : return error_mark_node;
15938 : }
15939 3340 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15940 : {
15941 12 : error_at (OMP_CLAUSE_LOCATION (c),
15942 : "length %qE of array section does not have integral type",
15943 : length);
15944 12 : return error_mark_node;
15945 : }
15946 3328 : if (low_bound
15947 2744 : && TREE_CODE (low_bound) == INTEGER_CST
15948 5783 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15949 2455 : > TYPE_PRECISION (sizetype))
15950 0 : low_bound = fold_convert (sizetype, low_bound);
15951 3328 : if (length
15952 3140 : && TREE_CODE (length) == INTEGER_CST
15953 5521 : && TYPE_PRECISION (TREE_TYPE (length))
15954 2193 : > TYPE_PRECISION (sizetype))
15955 0 : length = fold_convert (sizetype, length);
15956 3328 : if (low_bound == NULL_TREE)
15957 584 : low_bound = integer_zero_node;
15958 3328 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15959 3328 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15960 1937 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15961 : {
15962 10 : if (length != integer_one_node)
15963 : {
15964 6 : error_at (OMP_CLAUSE_LOCATION (c),
15965 : "expected single pointer in %qs clause",
15966 : user_omp_clause_code_name (c, openacc));
15967 6 : return error_mark_node;
15968 : }
15969 : }
15970 3322 : if (length != NULL_TREE)
15971 : {
15972 3138 : if (!integer_nonzerop (length))
15973 : {
15974 976 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15975 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15976 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15977 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15978 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15979 : {
15980 180 : if (integer_zerop (length))
15981 : {
15982 12 : error_at (OMP_CLAUSE_LOCATION (c),
15983 : "zero length array section in %qs clause",
15984 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15985 12 : return error_mark_node;
15986 : }
15987 : }
15988 : else
15989 796 : maybe_zero_len = true;
15990 : }
15991 3126 : if (first_non_one == types.length ()
15992 3126 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15993 1599 : first_non_one++;
15994 : }
15995 3310 : if (TREE_CODE (type) == ARRAY_TYPE)
15996 : {
15997 1496 : if (length == NULL_TREE
15998 1496 : && (TYPE_DOMAIN (type) == NULL_TREE
15999 168 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
16000 : {
16001 8 : error_at (OMP_CLAUSE_LOCATION (c),
16002 : "for unknown bound array type length expression must "
16003 : "be specified");
16004 8 : return error_mark_node;
16005 : }
16006 1488 : if (TREE_CODE (low_bound) == INTEGER_CST
16007 1488 : && tree_int_cst_sgn (low_bound) == -1)
16008 : {
16009 35 : error_at (OMP_CLAUSE_LOCATION (c),
16010 : "negative low bound in array section in %qs clause",
16011 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16012 35 : return error_mark_node;
16013 : }
16014 1453 : if (length != NULL_TREE
16015 1297 : && TREE_CODE (length) == INTEGER_CST
16016 2310 : && tree_int_cst_sgn (length) == -1)
16017 : {
16018 35 : error_at (OMP_CLAUSE_LOCATION (c),
16019 : "negative length in array section in %qs clause",
16020 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16021 35 : return error_mark_node;
16022 : }
16023 1418 : if (TYPE_DOMAIN (type)
16024 1407 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
16025 2825 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
16026 : == INTEGER_CST)
16027 : {
16028 1061 : tree size
16029 1061 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
16030 1061 : size = size_binop (PLUS_EXPR, size, size_one_node);
16031 1061 : if (TREE_CODE (low_bound) == INTEGER_CST)
16032 : {
16033 874 : if (tree_int_cst_lt (size, low_bound))
16034 : {
16035 10 : error_at (OMP_CLAUSE_LOCATION (c),
16036 : "low bound %qE above array section size "
16037 : "in %qs clause", low_bound,
16038 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16039 10 : return error_mark_node;
16040 : }
16041 864 : if (tree_int_cst_equal (size, low_bound))
16042 : {
16043 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
16044 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16045 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16046 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16047 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16048 : {
16049 5 : error_at (OMP_CLAUSE_LOCATION (c),
16050 : "zero length array section in %qs clause",
16051 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16052 5 : return error_mark_node;
16053 : }
16054 0 : maybe_zero_len = true;
16055 : }
16056 859 : else if (length == NULL_TREE
16057 256 : && first_non_one == types.length ()
16058 914 : && tree_int_cst_equal
16059 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
16060 : low_bound))
16061 30 : first_non_one++;
16062 : }
16063 187 : else if (length == NULL_TREE)
16064 : {
16065 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16066 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16067 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16068 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16069 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16070 0 : maybe_zero_len = true;
16071 6 : if (first_non_one == types.length ())
16072 1 : first_non_one++;
16073 : }
16074 1046 : if (length && TREE_CODE (length) == INTEGER_CST)
16075 : {
16076 799 : if (tree_int_cst_lt (size, length))
16077 : {
16078 11 : error_at (OMP_CLAUSE_LOCATION (c),
16079 : "length %qE above array section size "
16080 : "in %qs clause", length,
16081 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16082 11 : return error_mark_node;
16083 : }
16084 788 : if (TREE_CODE (low_bound) == INTEGER_CST)
16085 : {
16086 688 : tree lbpluslen
16087 688 : = size_binop (PLUS_EXPR,
16088 : fold_convert (sizetype, low_bound),
16089 : fold_convert (sizetype, length));
16090 688 : if (TREE_CODE (lbpluslen) == INTEGER_CST
16091 688 : && tree_int_cst_lt (size, lbpluslen))
16092 : {
16093 10 : error_at (OMP_CLAUSE_LOCATION (c),
16094 : "high bound %qE above array section size "
16095 : "in %qs clause", lbpluslen,
16096 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16097 10 : return error_mark_node;
16098 : }
16099 : }
16100 : }
16101 : }
16102 357 : else if (length == NULL_TREE)
16103 : {
16104 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16105 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16106 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16107 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
16108 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
16109 0 : maybe_zero_len = true;
16110 20 : if (first_non_one == types.length ())
16111 10 : first_non_one++;
16112 : }
16113 :
16114 : /* For [lb:] we will need to evaluate lb more than once. */
16115 929 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16116 : {
16117 127 : tree lb = save_expr (low_bound);
16118 127 : if (lb != low_bound)
16119 : {
16120 2 : TREE_OPERAND (t, 1) = lb;
16121 2 : low_bound = lb;
16122 : }
16123 : }
16124 : }
16125 1814 : else if (TREE_CODE (type) == POINTER_TYPE)
16126 : {
16127 1803 : if (length == NULL_TREE)
16128 : {
16129 10 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
16130 8 : error_at (OMP_CLAUSE_LOCATION (c),
16131 : "for array function parameter length expression "
16132 : "must be specified");
16133 : else
16134 2 : error_at (OMP_CLAUSE_LOCATION (c),
16135 : "for pointer type length expression must be specified");
16136 10 : return error_mark_node;
16137 : }
16138 1793 : if (length != NULL_TREE
16139 1793 : && TREE_CODE (length) == INTEGER_CST
16140 1286 : && tree_int_cst_sgn (length) == -1)
16141 : {
16142 20 : error_at (OMP_CLAUSE_LOCATION (c),
16143 : "negative length in array section in %qs clause",
16144 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16145 20 : return error_mark_node;
16146 : }
16147 : /* If there is a pointer type anywhere but in the very first
16148 : array-section-subscript, the array section could be non-contiguous. */
16149 1773 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
16150 1612 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
16151 3356 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
16152 : {
16153 : /* If any prior dimension has a non-one length, then deem this
16154 : array section as non-contiguous. */
16155 42 : for (tree d = TREE_OPERAND (t, 0);
16156 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
16157 40 : d = TREE_OPERAND (d, 0))
16158 : {
16159 44 : tree d_length = TREE_OPERAND (d, 2);
16160 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
16161 : {
16162 4 : error_at (OMP_CLAUSE_LOCATION (c),
16163 : "array section is not contiguous in %qs clause",
16164 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16165 4 : return error_mark_node;
16166 : }
16167 : }
16168 : }
16169 : }
16170 : else
16171 : {
16172 11 : error_at (OMP_CLAUSE_LOCATION (c),
16173 : "%qE does not have pointer or array type", ret);
16174 11 : return error_mark_node;
16175 : }
16176 3151 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
16177 2835 : types.safe_push (TREE_TYPE (ret));
16178 : /* We will need to evaluate lb more than once. */
16179 3151 : tree lb = save_expr (low_bound);
16180 3151 : if (lb != low_bound)
16181 : {
16182 273 : TREE_OPERAND (t, 1) = lb;
16183 273 : low_bound = lb;
16184 : }
16185 3151 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
16186 3151 : return ret;
16187 : }
16188 :
16189 : /* Handle array sections for clause C. */
16190 :
16191 : static bool
16192 2980 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
16193 : {
16194 2980 : bool maybe_zero_len = false;
16195 2980 : unsigned int first_non_one = 0;
16196 2980 : auto_vec<tree, 10> types;
16197 2980 : tree *tp = &OMP_CLAUSE_DECL (c);
16198 2980 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16199 2666 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16200 3079 : && OMP_ITERATOR_DECL_P (*tp))
16201 66 : tp = &TREE_VALUE (*tp);
16202 2980 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16203 : maybe_zero_len, first_non_one,
16204 : ort);
16205 2980 : if (first == error_mark_node)
16206 : return true;
16207 2762 : if (first == NULL_TREE)
16208 : return false;
16209 2762 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16210 2762 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16211 : {
16212 336 : tree t = *tp;
16213 336 : tree tem = NULL_TREE;
16214 : /* Need to evaluate side effects in the length expressions
16215 : if any. */
16216 336 : while (TREE_CODE (t) == TREE_LIST)
16217 : {
16218 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16219 : {
16220 0 : if (tem == NULL_TREE)
16221 : tem = TREE_VALUE (t);
16222 : else
16223 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16224 0 : TREE_VALUE (t), tem);
16225 : }
16226 0 : t = TREE_CHAIN (t);
16227 : }
16228 336 : if (tem)
16229 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16230 336 : first = c_fully_fold (first, false, NULL, true);
16231 336 : *tp = first;
16232 : }
16233 : else
16234 : {
16235 2426 : unsigned int num = types.length (), i;
16236 2426 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16237 2426 : tree condition = NULL_TREE;
16238 :
16239 2426 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16240 3 : maybe_zero_len = true;
16241 :
16242 5052 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16243 2626 : t = TREE_OPERAND (t, 0))
16244 : {
16245 2641 : tree low_bound = TREE_OPERAND (t, 1);
16246 2641 : tree length = TREE_OPERAND (t, 2);
16247 :
16248 2641 : i--;
16249 2641 : if (low_bound
16250 2177 : && TREE_CODE (low_bound) == INTEGER_CST
16251 4632 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16252 1991 : > TYPE_PRECISION (sizetype))
16253 0 : low_bound = fold_convert (sizetype, low_bound);
16254 2641 : if (length
16255 2549 : && TREE_CODE (length) == INTEGER_CST
16256 4261 : && TYPE_PRECISION (TREE_TYPE (length))
16257 1620 : > TYPE_PRECISION (sizetype))
16258 0 : length = fold_convert (sizetype, length);
16259 2641 : if (low_bound == NULL_TREE)
16260 464 : low_bound = integer_zero_node;
16261 2641 : if (!maybe_zero_len && i > first_non_one)
16262 : {
16263 109 : if (integer_nonzerop (low_bound))
16264 6 : goto do_warn_noncontiguous;
16265 103 : if (length != NULL_TREE
16266 50 : && TREE_CODE (length) == INTEGER_CST
16267 50 : && TYPE_DOMAIN (types[i])
16268 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16269 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16270 : == INTEGER_CST)
16271 : {
16272 50 : tree size;
16273 50 : size = size_binop (PLUS_EXPR,
16274 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16275 : size_one_node);
16276 50 : if (!tree_int_cst_equal (length, size))
16277 : {
16278 7 : do_warn_noncontiguous:
16279 26 : error_at (OMP_CLAUSE_LOCATION (c),
16280 : "array section is not contiguous in %qs "
16281 : "clause",
16282 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16283 2426 : return true;
16284 : }
16285 : }
16286 96 : if (length != NULL_TREE
16287 96 : && TREE_SIDE_EFFECTS (length))
16288 : {
16289 0 : if (side_effects == NULL_TREE)
16290 : side_effects = length;
16291 : else
16292 0 : side_effects = build2 (COMPOUND_EXPR,
16293 0 : TREE_TYPE (side_effects),
16294 : length, side_effects);
16295 : }
16296 : }
16297 : else
16298 : {
16299 2532 : tree l;
16300 :
16301 2532 : if (i > first_non_one
16302 2532 : && ((length && integer_nonzerop (length))
16303 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16304 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16305 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16306 0 : continue;
16307 2532 : if (length)
16308 2499 : l = fold_convert (sizetype, length);
16309 : else
16310 : {
16311 33 : l = size_binop (PLUS_EXPR,
16312 : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16313 : size_one_node);
16314 33 : l = size_binop (MINUS_EXPR, l,
16315 : fold_convert (sizetype, low_bound));
16316 : }
16317 2532 : if (i > first_non_one)
16318 : {
16319 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16320 : size_zero_node);
16321 0 : if (condition == NULL_TREE)
16322 : condition = l;
16323 : else
16324 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16325 : l, condition);
16326 : }
16327 2532 : else if (size == NULL_TREE)
16328 : {
16329 2413 : size = size_in_bytes (TREE_TYPE (types[i]));
16330 2413 : tree eltype = TREE_TYPE (types[num - 1]);
16331 2433 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16332 20 : eltype = TREE_TYPE (eltype);
16333 2413 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16334 2178 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16335 4331 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16336 : {
16337 539 : if (integer_zerop (size)
16338 1076 : || integer_zerop (size_in_bytes (eltype)))
16339 : {
16340 4 : error_at (OMP_CLAUSE_LOCATION (c),
16341 : "zero length array section in %qs clause",
16342 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16343 2 : return error_mark_node;
16344 : }
16345 537 : size = size_binop (EXACT_DIV_EXPR, size,
16346 : size_in_bytes (eltype));
16347 : }
16348 2411 : size = size_binop (MULT_EXPR, size, l);
16349 2411 : if (condition)
16350 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16351 : size, size_zero_node);
16352 : }
16353 : else
16354 119 : size = size_binop (MULT_EXPR, size, l);
16355 : }
16356 : }
16357 2411 : if (side_effects)
16358 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16359 2411 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16360 2178 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16361 4329 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16362 : {
16363 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16364 537 : size = c_fully_fold (size, false, NULL);
16365 537 : size = save_expr (size);
16366 537 : tree index_type = build_index_type (size);
16367 537 : tree eltype = TREE_TYPE (first);
16368 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16369 12 : eltype = TREE_TYPE (eltype);
16370 537 : tree type = c_build_array_type (eltype, index_type);
16371 537 : tree ptype = c_build_pointer_type (eltype);
16372 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16373 296 : t = build_fold_addr_expr (t);
16374 537 : tree t2 = build_fold_addr_expr (first);
16375 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16376 : ptrdiff_type_node, t2);
16377 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16378 : ptrdiff_type_node, t2,
16379 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16380 : ptrdiff_type_node, t));
16381 537 : t2 = c_fully_fold (t2, false, NULL);
16382 537 : if (tree_fits_shwi_p (t2))
16383 398 : t = build2 (MEM_REF, type, t,
16384 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16385 : else
16386 : {
16387 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16388 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16389 139 : TREE_TYPE (t), t, t2);
16390 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16391 : }
16392 537 : OMP_CLAUSE_DECL (c) = t;
16393 537 : return false;
16394 : }
16395 1874 : first = c_fully_fold (first, false, NULL);
16396 1874 : OMP_CLAUSE_DECL (c) = first;
16397 1874 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16398 : return false;
16399 : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16400 1863 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16401 1863 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16402 1722 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16403 1720 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16404 : {
16405 1859 : if (size)
16406 1859 : size = c_fully_fold (size, false, NULL);
16407 1859 : OMP_CLAUSE_SIZE (c) = size;
16408 : }
16409 :
16410 1863 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16411 : return false;
16412 :
16413 1724 : auto_vec<omp_addr_token *, 10> addr_tokens;
16414 :
16415 1724 : if (!omp_parse_expr (addr_tokens, first))
16416 1724 : return true;
16417 :
16418 1724 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16419 :
16420 1724 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16421 1724 : if (nc != error_mark_node)
16422 : {
16423 1724 : using namespace omp_addr_tokenizer;
16424 :
16425 1724 : if (ai.maybe_zero_length_array_section (c))
16426 1720 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16427 :
16428 : /* !!! If we're accessing a base decl via chained access
16429 : methods (e.g. multiple indirections), duplicate clause
16430 : detection won't work properly. Skip it in that case. */
16431 1724 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16432 1436 : || addr_tokens[0]->type == ARRAY_BASE)
16433 1724 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16434 1724 : && addr_tokens[1]->type == ACCESS_METHOD
16435 3448 : && omp_access_chain_p (addr_tokens, 1))
16436 34 : c = nc;
16437 :
16438 1724 : return false;
16439 : }
16440 : }
16441 : return false;
16442 2980 : }
16443 :
16444 : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16445 : an inline call. But, remap
16446 : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16447 : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16448 :
16449 : static tree
16450 516 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16451 : tree decl, tree placeholder)
16452 : {
16453 516 : copy_body_data id;
16454 516 : hash_map<tree, tree> decl_map;
16455 :
16456 516 : decl_map.put (omp_decl1, placeholder);
16457 516 : decl_map.put (omp_decl2, decl);
16458 516 : memset (&id, 0, sizeof (id));
16459 516 : id.src_fn = DECL_CONTEXT (omp_decl1);
16460 516 : id.dst_fn = current_function_decl;
16461 516 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16462 516 : id.decl_map = &decl_map;
16463 :
16464 516 : id.copy_decl = copy_decl_no_change;
16465 516 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16466 516 : id.transform_new_cfg = true;
16467 516 : id.transform_return_to_modify = false;
16468 516 : id.eh_lp_nr = 0;
16469 516 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16470 516 : return stmt;
16471 516 : }
16472 :
16473 : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16474 : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16475 :
16476 : static tree
16477 1147 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16478 : {
16479 1147 : if (*tp == (tree) data)
16480 31 : return *tp;
16481 : return NULL_TREE;
16482 : }
16483 :
16484 : /* Similarly, but also walk aggregate fields. */
16485 :
16486 : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16487 :
16488 : static tree
16489 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16490 : {
16491 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16492 : return *tp;
16493 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16494 : {
16495 2 : tree field;
16496 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16497 :
16498 2 : for (field = TYPE_FIELDS (*tp); field;
16499 0 : field = DECL_CHAIN (field))
16500 2 : if (TREE_CODE (field) == FIELD_DECL)
16501 : {
16502 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16503 : c_find_omp_var_r, data, pset);
16504 2 : if (ret)
16505 : return ret;
16506 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16507 2 : if (ret)
16508 : return ret;
16509 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16510 : pset);
16511 2 : if (ret)
16512 : return ret;
16513 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16514 2 : if (ret)
16515 : return ret;
16516 : }
16517 : }
16518 278 : else if (INTEGRAL_TYPE_P (*tp))
16519 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16520 : ((struct c_find_omp_var_s *) data)->pset);
16521 : return NULL_TREE;
16522 : }
16523 :
16524 : /* Finish OpenMP iterators ITER. Return true if they are erroneous
16525 : and clauses containing them should be removed. */
16526 :
16527 : static bool
16528 143 : c_omp_finish_iterators (tree iter)
16529 : {
16530 143 : bool ret = false;
16531 325 : for (tree it = iter; it; it = TREE_CHAIN (it))
16532 : {
16533 182 : tree var = OMP_ITERATOR_VAR (it);
16534 182 : tree begin = OMP_ITERATOR_BEGIN (it);
16535 182 : tree end = OMP_ITERATOR_END (it);
16536 182 : tree step = OMP_ITERATOR_STEP (it);
16537 182 : tree orig_step;
16538 182 : tree type = TREE_TYPE (var);
16539 182 : location_t loc = DECL_SOURCE_LOCATION (var);
16540 182 : if (type == error_mark_node)
16541 : {
16542 0 : ret = true;
16543 34 : continue;
16544 : }
16545 182 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16546 : {
16547 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16548 : var);
16549 6 : ret = true;
16550 6 : continue;
16551 : }
16552 176 : else if (TYPE_ATOMIC (type))
16553 : {
16554 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16555 2 : ret = true;
16556 2 : continue;
16557 : }
16558 174 : else if (TYPE_READONLY (type))
16559 : {
16560 4 : error_at (loc, "iterator %qD has const qualified type", var);
16561 4 : ret = true;
16562 4 : continue;
16563 : }
16564 170 : else if (step == error_mark_node
16565 170 : || TREE_TYPE (step) == error_mark_node)
16566 : {
16567 0 : ret = true;
16568 0 : continue;
16569 : }
16570 170 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16571 : {
16572 6 : error_at (EXPR_LOC_OR_LOC (step, loc),
16573 : "iterator step with non-integral type");
16574 4 : ret = true;
16575 4 : continue;
16576 : }
16577 166 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16578 166 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16579 166 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16580 166 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16581 166 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16582 166 : if (POINTER_TYPE_P (type))
16583 : {
16584 14 : begin = save_expr (begin);
16585 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16586 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16587 : fold_convert (sizetype, step),
16588 : fold_convert (sizetype, begin));
16589 14 : step = fold_convert (ssizetype, step);
16590 : }
16591 166 : if (integer_zerop (step))
16592 : {
16593 6 : error_at (loc, "iterator %qD has zero step", var);
16594 6 : ret = true;
16595 6 : continue;
16596 : }
16597 :
16598 160 : if (begin == error_mark_node
16599 158 : || end == error_mark_node
16600 156 : || step == error_mark_node
16601 156 : || orig_step == error_mark_node)
16602 : {
16603 4 : ret = true;
16604 4 : continue;
16605 : }
16606 156 : hash_set<tree> pset;
16607 156 : tree it2;
16608 197 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16609 : {
16610 49 : tree var2 = OMP_ITERATOR_VAR (it2);
16611 49 : tree begin2 = OMP_ITERATOR_BEGIN (it2);
16612 49 : tree end2 = OMP_ITERATOR_END (it2);
16613 49 : tree step2 = OMP_ITERATOR_STEP (it2);
16614 49 : tree type2 = TREE_TYPE (var2);
16615 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16616 49 : struct c_find_omp_var_s data = { var, &pset };
16617 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16618 : {
16619 2 : error_at (loc2,
16620 : "type of iterator %qD refers to outer iterator %qD",
16621 : var2, var);
16622 10 : break;
16623 : }
16624 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16625 : {
16626 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16627 : "begin expression refers to outer iterator %qD", var);
16628 2 : break;
16629 : }
16630 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16631 : {
16632 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16633 : "end expression refers to outer iterator %qD", var);
16634 2 : break;
16635 : }
16636 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16637 : {
16638 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16639 : "step expression refers to outer iterator %qD", var);
16640 2 : break;
16641 : }
16642 : }
16643 156 : if (it2)
16644 : {
16645 8 : ret = true;
16646 8 : continue;
16647 : }
16648 148 : OMP_ITERATOR_BEGIN (it) = begin;
16649 148 : OMP_ITERATOR_END (it) = end;
16650 148 : OMP_ITERATOR_STEP (it) = step;
16651 148 : OMP_ITERATOR_ORIG_STEP (it) = orig_step;
16652 156 : }
16653 143 : return ret;
16654 : }
16655 :
16656 : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16657 : Return true if an error has been detected. */
16658 :
16659 : static bool
16660 9626 : c_oacc_check_attachments (tree c)
16661 : {
16662 9626 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16663 : return false;
16664 :
16665 : /* OpenACC attach / detach clauses must be pointers. */
16666 6601 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16667 6601 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16668 : {
16669 71 : tree t = OMP_CLAUSE_DECL (c);
16670 :
16671 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16672 6 : t = TREE_OPERAND (t, 0);
16673 :
16674 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16675 : {
16676 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16677 : user_omp_clause_code_name (c, true));
16678 6 : return true;
16679 : }
16680 : }
16681 :
16682 : return false;
16683 : }
16684 :
16685 : /* For all elements of CLAUSES, validate them against their constraints.
16686 : Remove any elements from the list that are invalid. */
16687 :
16688 : tree
16689 29626 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16690 : {
16691 29626 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16692 29626 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16693 29626 : bitmap_head oacc_reduction_head, is_on_device_head;
16694 29626 : tree c, t, type, *pc;
16695 29626 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16696 29626 : bool branch_seen = false;
16697 29626 : bool copyprivate_seen = false;
16698 29626 : bool mergeable_seen = false;
16699 29626 : tree *detach_seen = NULL;
16700 29626 : bool linear_variable_step_check = false;
16701 29626 : tree *nowait_clause = NULL;
16702 29626 : tree *grainsize_seen = NULL;
16703 29626 : bool num_tasks_seen = false;
16704 29626 : tree ordered_clause = NULL_TREE;
16705 29626 : tree schedule_clause = NULL_TREE;
16706 29626 : bool oacc_async = false;
16707 29626 : tree last_iterators = NULL_TREE;
16708 29626 : bool last_iterators_remove = false;
16709 29626 : tree *nogroup_seen = NULL;
16710 29626 : tree *order_clause = NULL;
16711 : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16712 : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16713 29626 : int reduction_seen = 0;
16714 29626 : bool allocate_seen = false;
16715 29626 : bool implicit_moved = false;
16716 29626 : bool target_in_reduction_seen = false;
16717 29626 : tree *full_seen = NULL;
16718 29626 : bool partial_seen = false;
16719 29626 : bool openacc = (ort & C_ORT_ACC) != 0;
16720 29626 : bool init_seen = false;
16721 29626 : bool init_use_destroy_seen = false;
16722 29626 : tree init_no_targetsync_clause = NULL_TREE;
16723 29626 : tree depend_clause = NULL_TREE;
16724 :
16725 29626 : if (!openacc)
16726 24389 : clauses = omp_remove_duplicate_maps (clauses, true);
16727 :
16728 29626 : bitmap_obstack_initialize (NULL);
16729 29626 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16730 29626 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16731 29626 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16732 29626 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16733 : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16734 29626 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16735 29626 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16736 29626 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16737 : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16738 : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16739 29626 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16740 29626 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16741 :
16742 29626 : if (openacc)
16743 12269 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16744 7258 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16745 : {
16746 : oacc_async = true;
16747 : break;
16748 : }
16749 :
16750 29626 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16751 :
16752 77756 : for (pc = &clauses, c = clauses; c ; c = *pc)
16753 : {
16754 48130 : bool remove = false;
16755 48130 : bool need_complete = false;
16756 48130 : bool need_implicitly_determined = false;
16757 :
16758 : /* We've reached the end of a list of expanded nodes. Reset the group
16759 : start pointer. */
16760 48130 : if (c == grp_sentinel)
16761 : {
16762 2517 : if (grp_start_p
16763 2517 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16764 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16765 22 : gc = OMP_CLAUSE_CHAIN (gc))
16766 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16767 : grp_start_p = NULL;
16768 : }
16769 :
16770 48130 : switch (OMP_CLAUSE_CODE (c))
16771 : {
16772 1139 : case OMP_CLAUSE_SHARED:
16773 1139 : need_implicitly_determined = true;
16774 1139 : goto check_dup_generic;
16775 :
16776 848 : case OMP_CLAUSE_PRIVATE:
16777 848 : need_complete = true;
16778 848 : need_implicitly_determined = true;
16779 848 : goto check_dup_generic;
16780 :
16781 3533 : case OMP_CLAUSE_REDUCTION:
16782 3533 : if (reduction_seen == 0)
16783 2929 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16784 604 : else if (reduction_seen != -2
16785 1208 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16786 604 : ? -1 : 1))
16787 : {
16788 2 : error_at (OMP_CLAUSE_LOCATION (c),
16789 : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16790 : "on the same construct");
16791 2 : reduction_seen = -2;
16792 : }
16793 : /* FALLTHRU */
16794 4197 : case OMP_CLAUSE_IN_REDUCTION:
16795 4197 : case OMP_CLAUSE_TASK_REDUCTION:
16796 4197 : need_implicitly_determined = true;
16797 4197 : t = OMP_CLAUSE_DECL (c);
16798 4197 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16799 : {
16800 553 : if (handle_omp_array_sections (c, ort))
16801 : {
16802 : remove = true;
16803 : break;
16804 : }
16805 :
16806 537 : t = OMP_CLAUSE_DECL (c);
16807 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16808 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16809 : {
16810 1 : error_at (OMP_CLAUSE_LOCATION (c),
16811 : "%<inscan%> %<reduction%> clause with array "
16812 : "section");
16813 1 : remove = true;
16814 1 : break;
16815 : }
16816 : }
16817 4180 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16818 4180 : if (t == error_mark_node)
16819 : {
16820 : remove = true;
16821 : break;
16822 : }
16823 4178 : if (oacc_async)
16824 3 : c_mark_addressable (t);
16825 4178 : type = TREE_TYPE (t);
16826 4178 : if (TREE_CODE (t) == MEM_REF)
16827 536 : type = TREE_TYPE (type);
16828 4178 : if (TREE_CODE (type) == ARRAY_TYPE)
16829 : {
16830 74 : tree oatype = type;
16831 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16832 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16833 74 : type = TREE_TYPE (type);
16834 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16835 : {
16836 1 : error_at (OMP_CLAUSE_LOCATION (c),
16837 : "%qD in %<reduction%> clause is a zero size array",
16838 : t);
16839 1 : remove = true;
16840 1 : break;
16841 : }
16842 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16843 : TYPE_SIZE_UNIT (type));
16844 73 : if (integer_zerop (size))
16845 : {
16846 1 : error_at (OMP_CLAUSE_LOCATION (c),
16847 : "%qD in %<reduction%> clause is a zero size array",
16848 : t);
16849 1 : remove = true;
16850 1 : break;
16851 : }
16852 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16853 72 : size = save_expr (size);
16854 72 : tree index_type = build_index_type (size);
16855 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16856 : index_type);
16857 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16858 72 : tree ptype = c_build_pointer_type (type);
16859 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16860 72 : t = build_fold_addr_expr (t);
16861 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16862 72 : OMP_CLAUSE_DECL (c) = t;
16863 : }
16864 4176 : if (TYPE_ATOMIC (type))
16865 : {
16866 3 : error_at (OMP_CLAUSE_LOCATION (c),
16867 : "%<_Atomic%> %qE in %<reduction%> clause", t);
16868 3 : remove = true;
16869 3 : break;
16870 : }
16871 4173 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16872 4173 : || OMP_CLAUSE_REDUCTION_TASK (c))
16873 : {
16874 : /* Disallow zero sized or potentially zero sized task
16875 : reductions. */
16876 871 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16877 : {
16878 6 : error_at (OMP_CLAUSE_LOCATION (c),
16879 : "zero sized type %qT in %qs clause", type,
16880 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16881 3 : remove = true;
16882 3 : break;
16883 : }
16884 868 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16885 : {
16886 0 : error_at (OMP_CLAUSE_LOCATION (c),
16887 : "variable sized type %qT in %qs clause", type,
16888 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16889 0 : remove = true;
16890 0 : break;
16891 : }
16892 : }
16893 4170 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16894 4170 : && (FLOAT_TYPE_P (type)
16895 3615 : || TREE_CODE (type) == COMPLEX_TYPE))
16896 : {
16897 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16898 340 : const char *r_name = NULL;
16899 :
16900 340 : switch (r_code)
16901 : {
16902 : case PLUS_EXPR:
16903 : case MULT_EXPR:
16904 : case MINUS_EXPR:
16905 : case TRUTH_ANDIF_EXPR:
16906 : case TRUTH_ORIF_EXPR:
16907 : break;
16908 13 : case MIN_EXPR:
16909 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16910 : r_name = "min";
16911 : break;
16912 38 : case MAX_EXPR:
16913 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16914 : r_name = "max";
16915 : break;
16916 3 : case BIT_AND_EXPR:
16917 3 : r_name = "&";
16918 3 : break;
16919 2 : case BIT_XOR_EXPR:
16920 2 : r_name = "^";
16921 2 : break;
16922 : case BIT_IOR_EXPR:
16923 : r_name = "|";
16924 : break;
16925 0 : default:
16926 0 : gcc_unreachable ();
16927 : }
16928 5 : if (r_name)
16929 : {
16930 12 : error_at (OMP_CLAUSE_LOCATION (c),
16931 : "%qE has invalid type for %<reduction(%s)%>",
16932 : t, r_name);
16933 12 : remove = true;
16934 12 : break;
16935 : }
16936 : }
16937 3830 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16938 : {
16939 2 : error_at (OMP_CLAUSE_LOCATION (c),
16940 : "user defined reduction not found for %qE", t);
16941 2 : remove = true;
16942 2 : break;
16943 : }
16944 3828 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16945 : {
16946 280 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16947 280 : type = TYPE_MAIN_VARIANT (type);
16948 280 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16949 : VAR_DECL, NULL_TREE, type);
16950 280 : tree decl_placeholder = NULL_TREE;
16951 280 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16952 280 : DECL_ARTIFICIAL (placeholder) = 1;
16953 280 : DECL_IGNORED_P (placeholder) = 1;
16954 280 : if (TREE_CODE (t) == MEM_REF)
16955 : {
16956 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16957 : VAR_DECL, NULL_TREE, type);
16958 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16959 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16960 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16961 : }
16962 280 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16963 45 : c_mark_addressable (placeholder);
16964 280 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16965 82 : c_mark_addressable (decl_placeholder ? decl_placeholder
16966 37 : : OMP_CLAUSE_DECL (c));
16967 280 : OMP_CLAUSE_REDUCTION_MERGE (c)
16968 784 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16969 280 : TREE_VEC_ELT (list, 0),
16970 280 : TREE_VEC_ELT (list, 1),
16971 : decl_placeholder ? decl_placeholder
16972 224 : : OMP_CLAUSE_DECL (c), placeholder);
16973 280 : OMP_CLAUSE_REDUCTION_MERGE (c)
16974 280 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16975 : void_type_node, NULL_TREE,
16976 280 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16977 280 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16978 280 : if (TREE_VEC_LENGTH (list) == 6)
16979 : {
16980 236 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16981 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16982 33 : : OMP_CLAUSE_DECL (c));
16983 236 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16984 30 : c_mark_addressable (placeholder);
16985 236 : tree init = TREE_VEC_ELT (list, 5);
16986 236 : if (init == error_mark_node)
16987 200 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16988 236 : OMP_CLAUSE_REDUCTION_INIT (c)
16989 660 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16990 236 : TREE_VEC_ELT (list, 3),
16991 : decl_placeholder ? decl_placeholder
16992 188 : : OMP_CLAUSE_DECL (c), placeholder);
16993 236 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16994 : {
16995 200 : tree v = decl_placeholder ? decl_placeholder : t;
16996 200 : OMP_CLAUSE_REDUCTION_INIT (c)
16997 400 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16998 200 : OMP_CLAUSE_REDUCTION_INIT (c));
16999 : }
17000 236 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
17001 : c_find_omp_placeholder_r,
17002 : placeholder, NULL))
17003 31 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
17004 : }
17005 : else
17006 : {
17007 44 : tree init;
17008 44 : tree v = decl_placeholder ? decl_placeholder : t;
17009 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
17010 34 : init = build_constructor (TREE_TYPE (v), NULL);
17011 : else
17012 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
17013 44 : OMP_CLAUSE_REDUCTION_INIT (c)
17014 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
17015 : }
17016 280 : OMP_CLAUSE_REDUCTION_INIT (c)
17017 280 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
17018 : void_type_node, NULL_TREE,
17019 280 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
17020 280 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
17021 : }
17022 4156 : if (TREE_CODE (t) == MEM_REF)
17023 : {
17024 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
17025 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
17026 : != INTEGER_CST)
17027 : {
17028 0 : sorry ("variable length element type in array "
17029 : "%<reduction%> clause");
17030 0 : remove = true;
17031 0 : break;
17032 : }
17033 607 : t = TREE_OPERAND (t, 0);
17034 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
17035 139 : t = TREE_OPERAND (t, 0);
17036 607 : if (TREE_CODE (t) == ADDR_EXPR)
17037 367 : t = TREE_OPERAND (t, 0);
17038 : }
17039 4156 : goto check_dup_generic_t;
17040 :
17041 24 : case OMP_CLAUSE_COPYPRIVATE:
17042 24 : copyprivate_seen = true;
17043 24 : if (nowait_clause)
17044 : {
17045 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
17046 : "%<nowait%> clause must not be used together "
17047 : "with %<copyprivate%> clause");
17048 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
17049 1 : nowait_clause = NULL;
17050 : }
17051 24 : goto check_dup_generic;
17052 :
17053 100 : case OMP_CLAUSE_COPYIN:
17054 100 : t = OMP_CLAUSE_DECL (c);
17055 100 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
17056 : {
17057 5 : error_at (OMP_CLAUSE_LOCATION (c),
17058 : "%qE must be %<threadprivate%> for %<copyin%>", t);
17059 5 : remove = true;
17060 5 : break;
17061 : }
17062 95 : goto check_dup_generic;
17063 :
17064 480 : case OMP_CLAUSE_LINEAR:
17065 480 : if (ort != C_ORT_OMP_DECLARE_SIMD)
17066 327 : need_implicitly_determined = true;
17067 480 : t = OMP_CLAUSE_DECL (c);
17068 480 : if (ort != C_ORT_OMP_DECLARE_SIMD
17069 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
17070 485 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
17071 : {
17072 5 : error_at (OMP_CLAUSE_LOCATION (c),
17073 : "modifier should not be specified in %<linear%> "
17074 : "clause on %<simd%> or %<for%> constructs when not "
17075 : "using OpenMP 5.2 modifiers");
17076 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
17077 : }
17078 960 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
17079 503 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17080 : {
17081 1 : error_at (OMP_CLAUSE_LOCATION (c),
17082 : "linear clause applied to non-integral non-pointer "
17083 1 : "variable with type %qT", TREE_TYPE (t));
17084 1 : remove = true;
17085 1 : break;
17086 : }
17087 479 : if (TYPE_ATOMIC (TREE_TYPE (t)))
17088 : {
17089 3 : error_at (OMP_CLAUSE_LOCATION (c),
17090 : "%<_Atomic%> %qD in %<linear%> clause", t);
17091 3 : remove = true;
17092 3 : break;
17093 : }
17094 476 : if (ort == C_ORT_OMP_DECLARE_SIMD)
17095 : {
17096 152 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17097 152 : if (TREE_CODE (s) == PARM_DECL)
17098 : {
17099 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
17100 : /* map_head bitmap is used as uniform_head if
17101 : declare_simd. */
17102 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
17103 5 : linear_variable_step_check = true;
17104 9 : goto check_dup_generic;
17105 : }
17106 143 : if (TREE_CODE (s) != INTEGER_CST)
17107 : {
17108 7 : error_at (OMP_CLAUSE_LOCATION (c),
17109 : "%<linear%> clause step %qE is neither constant "
17110 : "nor a parameter", s);
17111 7 : remove = true;
17112 7 : break;
17113 : }
17114 : }
17115 460 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
17116 : {
17117 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
17118 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
17119 20 : OMP_CLAUSE_DECL (c), s);
17120 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17121 : sizetype, fold_convert (sizetype, s),
17122 20 : fold_convert
17123 : (sizetype, OMP_CLAUSE_DECL (c)));
17124 20 : if (s == error_mark_node)
17125 0 : s = size_one_node;
17126 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
17127 : }
17128 : else
17129 440 : OMP_CLAUSE_LINEAR_STEP (c)
17130 880 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
17131 460 : goto check_dup_generic;
17132 :
17133 2889 : check_dup_generic:
17134 2889 : t = OMP_CLAUSE_DECL (c);
17135 7134 : check_dup_generic_t:
17136 7134 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17137 : {
17138 4 : error_at (OMP_CLAUSE_LOCATION (c),
17139 : "%qE is not a variable in clause %qs", t,
17140 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17141 2 : remove = true;
17142 : }
17143 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
17144 6093 : || (ort == C_ORT_OMP
17145 5374 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17146 5363 : || (OMP_CLAUSE_CODE (c)
17147 : == OMP_CLAUSE_USE_DEVICE_ADDR)))
17148 13178 : || (ort == C_ORT_OMP_TARGET
17149 347 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
17150 : {
17151 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
17152 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17153 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
17154 : {
17155 2 : error_at (OMP_CLAUSE_LOCATION (c),
17156 : "%qD appears more than once in data-sharing "
17157 : "clauses", t);
17158 2 : remove = true;
17159 2 : break;
17160 : }
17161 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
17162 108 : target_in_reduction_seen = true;
17163 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17164 : {
17165 4 : error_at (OMP_CLAUSE_LOCATION (c),
17166 : openacc
17167 : ? "%qD appears more than once in reduction clauses"
17168 : : "%qD appears more than once in data clauses",
17169 : t);
17170 2 : remove = true;
17171 : }
17172 : else
17173 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17174 : }
17175 5936 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17176 5913 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17177 5912 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
17178 11848 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17179 : {
17180 24 : error_at (OMP_CLAUSE_LOCATION (c),
17181 : "%qE appears more than once in data clauses", t);
17182 24 : remove = true;
17183 : }
17184 5912 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
17185 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
17186 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17187 1048 : && bitmap_bit_p (&map_head, DECL_UID (t)))
17188 : {
17189 3 : if (openacc)
17190 0 : error_at (OMP_CLAUSE_LOCATION (c),
17191 : "%qD appears more than once in data clauses", t);
17192 : else
17193 3 : error_at (OMP_CLAUSE_LOCATION (c),
17194 : "%qD appears both in data and map clauses", t);
17195 : remove = true;
17196 : }
17197 : else
17198 5909 : bitmap_set_bit (&generic_head, DECL_UID (t));
17199 : break;
17200 :
17201 1340 : case OMP_CLAUSE_FIRSTPRIVATE:
17202 1340 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
17203 : {
17204 153 : move_implicit:
17205 153 : implicit_moved = true;
17206 : /* Move firstprivate and map clauses with
17207 : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17208 : clauses chain. */
17209 153 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17210 153 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17211 972 : while (*pc1)
17212 819 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17213 819 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17214 : {
17215 113 : *pc3 = *pc1;
17216 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17217 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17218 : }
17219 706 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17220 706 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17221 : {
17222 131 : *pc2 = *pc1;
17223 131 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17224 131 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17225 : }
17226 : else
17227 575 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17228 153 : *pc3 = NULL;
17229 153 : *pc2 = cl2;
17230 153 : *pc1 = cl1;
17231 153 : continue;
17232 153 : }
17233 1245 : t = OMP_CLAUSE_DECL (c);
17234 1245 : need_complete = true;
17235 1245 : need_implicitly_determined = true;
17236 1245 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17237 : {
17238 1 : error_at (OMP_CLAUSE_LOCATION (c),
17239 : "%qE is not a variable in clause %<firstprivate%>", t);
17240 1 : remove = true;
17241 : }
17242 1244 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17243 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17244 1350 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17245 : remove = true;
17246 1244 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17247 1242 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17248 2485 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17249 : {
17250 4 : error_at (OMP_CLAUSE_LOCATION (c),
17251 : "%qE appears more than once in data clauses", t);
17252 4 : remove = true;
17253 : }
17254 1240 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17255 1240 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17256 : {
17257 7 : if (openacc)
17258 0 : error_at (OMP_CLAUSE_LOCATION (c),
17259 : "%qD appears more than once in data clauses", t);
17260 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17261 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17262 : /* Silently drop the clause. */;
17263 : else
17264 6 : error_at (OMP_CLAUSE_LOCATION (c),
17265 : "%qD appears both in data and map clauses", t);
17266 : remove = true;
17267 : }
17268 : else
17269 1233 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17270 : break;
17271 :
17272 1524 : case OMP_CLAUSE_LASTPRIVATE:
17273 1524 : t = OMP_CLAUSE_DECL (c);
17274 1524 : need_complete = true;
17275 1524 : need_implicitly_determined = true;
17276 1524 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17277 : {
17278 0 : error_at (OMP_CLAUSE_LOCATION (c),
17279 : "%qE is not a variable in clause %<lastprivate%>", t);
17280 0 : remove = true;
17281 : }
17282 1524 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17283 1524 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17284 : {
17285 3 : error_at (OMP_CLAUSE_LOCATION (c),
17286 : "%qE appears more than once in data clauses", t);
17287 3 : remove = true;
17288 : }
17289 : else
17290 1521 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17291 : break;
17292 :
17293 253 : case OMP_CLAUSE_ALIGNED:
17294 253 : t = OMP_CLAUSE_DECL (c);
17295 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17296 : {
17297 0 : error_at (OMP_CLAUSE_LOCATION (c),
17298 : "%qE is not a variable in %<aligned%> clause", t);
17299 0 : remove = true;
17300 : }
17301 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17302 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17303 : {
17304 7 : error_at (OMP_CLAUSE_LOCATION (c),
17305 : "%qE in %<aligned%> clause is neither a pointer nor "
17306 : "an array", t);
17307 7 : remove = true;
17308 : }
17309 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17310 : {
17311 1 : error_at (OMP_CLAUSE_LOCATION (c),
17312 : "%<_Atomic%> %qD in %<aligned%> clause", t);
17313 1 : remove = true;
17314 1 : break;
17315 : }
17316 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17317 : {
17318 0 : error_at (OMP_CLAUSE_LOCATION (c),
17319 : "%qE appears more than once in %<aligned%> clauses",
17320 : t);
17321 0 : remove = true;
17322 : }
17323 : else
17324 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17325 : break;
17326 :
17327 125 : case OMP_CLAUSE_NONTEMPORAL:
17328 125 : t = OMP_CLAUSE_DECL (c);
17329 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17330 : {
17331 0 : error_at (OMP_CLAUSE_LOCATION (c),
17332 : "%qE is not a variable in %<nontemporal%> clause", t);
17333 0 : remove = true;
17334 : }
17335 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17336 : {
17337 2 : error_at (OMP_CLAUSE_LOCATION (c),
17338 : "%qE appears more than once in %<nontemporal%> "
17339 : "clauses", t);
17340 2 : remove = true;
17341 : }
17342 : else
17343 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17344 : break;
17345 :
17346 808 : case OMP_CLAUSE_ALLOCATE:
17347 808 : t = OMP_CLAUSE_DECL (c);
17348 808 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17349 : {
17350 1 : error_at (OMP_CLAUSE_LOCATION (c),
17351 : "%qE is not a variable in %<allocate%> clause", t);
17352 1 : remove = true;
17353 : }
17354 807 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17355 : {
17356 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17357 : "%qE appears more than once in %<allocate%> clauses",
17358 : t);
17359 2 : remove = true;
17360 : }
17361 : else
17362 : {
17363 805 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17364 805 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17365 706 : allocate_seen = true;
17366 : }
17367 : break;
17368 :
17369 357 : case OMP_CLAUSE_DOACROSS:
17370 357 : t = OMP_CLAUSE_DECL (c);
17371 357 : if (t == NULL_TREE)
17372 : break;
17373 169 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17374 : {
17375 169 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17376 1315 : for (; t; t = TREE_CHAIN (t))
17377 : {
17378 1146 : tree decl = TREE_VALUE (t);
17379 1146 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17380 : {
17381 3 : tree offset = TREE_PURPOSE (t);
17382 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17383 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17384 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17385 : neg ? MINUS_EXPR : PLUS_EXPR,
17386 : decl, offset);
17387 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17388 : sizetype,
17389 : fold_convert (sizetype, t2),
17390 : fold_convert (sizetype, decl));
17391 3 : if (t2 == error_mark_node)
17392 : {
17393 : remove = true;
17394 : break;
17395 : }
17396 3 : TREE_PURPOSE (t) = t2;
17397 : }
17398 : }
17399 : break;
17400 : }
17401 0 : gcc_unreachable ();
17402 :
17403 58 : case OMP_CLAUSE_USES_ALLOCATORS:
17404 58 : t = OMP_CLAUSE_USES_ALLOCATORS_ALLOCATOR (c);
17405 58 : if (t == error_mark_node)
17406 : {
17407 : remove = true;
17408 : break;
17409 : }
17410 8 : if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17411 55 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
17412 47 : || bitmap_bit_p (&map_head, DECL_UID (t))
17413 47 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17414 46 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))))
17415 : {
17416 1 : error_at (OMP_CLAUSE_LOCATION (c),
17417 : "%qE appears more than once in data clauses", t);
17418 1 : remove = true;
17419 1 : break;
17420 : }
17421 : else
17422 53 : bitmap_set_bit (&generic_head, DECL_UID (t));
17423 53 : if (TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17424 103 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17425 : "omp_allocator_handle_t") != 0)
17426 : {
17427 3 : error_at (OMP_CLAUSE_LOCATION (c),
17428 : "allocator %qE must be of %<omp_allocator_handle_t%> "
17429 : "type", t);
17430 3 : remove = true;
17431 3 : break;
17432 : }
17433 50 : tree init;
17434 50 : if (!DECL_P (t)
17435 50 : || (TREE_CODE (t) == CONST_DECL
17436 7 : && ((init = DECL_INITIAL(t)) == nullptr
17437 7 : || TREE_CODE (init) != INTEGER_CST
17438 7 : || ((wi::to_widest (init) < 0
17439 7 : || wi::to_widest (init) > GOMP_OMP_PREDEF_ALLOC_MAX)
17440 0 : && (wi::to_widest (init) < GOMP_OMPX_PREDEF_ALLOC_MIN
17441 0 : || (wi::to_widest (init)
17442 0 : > GOMP_OMPX_PREDEF_ALLOC_MAX))))))
17443 : {
17444 0 : remove = true;
17445 0 : error_at (OMP_CLAUSE_LOCATION (c),
17446 : "allocator %qE must be either a variable or a "
17447 : "predefined allocator", t);
17448 0 : break;
17449 : }
17450 50 : else if (TREE_CODE (t) == CONST_DECL)
17451 : {
17452 : /* omp_null_allocator is ignored and for predefined allocators,
17453 : no special handling is required; thus, mark them removed. */
17454 7 : remove = true;
17455 :
17456 7 : if (OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c)
17457 7 : || OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c))
17458 : {
17459 0 : error_at (OMP_CLAUSE_LOCATION (c),
17460 : "modifiers cannot be used with predefined "
17461 : "allocator %qE", t);
17462 0 : break;
17463 : }
17464 : }
17465 50 : t = OMP_CLAUSE_USES_ALLOCATORS_MEMSPACE (c);
17466 50 : if (t == error_mark_node)
17467 : {
17468 : remove = true;
17469 : break;
17470 : }
17471 50 : if (t != NULL_TREE
17472 50 : && ((TREE_CODE (t) != CONST_DECL && TREE_CODE (t) != INTEGER_CST)
17473 8 : || TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE
17474 14 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (TREE_TYPE (t))),
17475 : "omp_memspace_handle_t") != 0))
17476 : {
17477 1 : error_at (OMP_CLAUSE_LOCATION (c), "memspace modifier %qE must be"
17478 : " constant enum of %<omp_memspace_handle_t%> type", t);
17479 1 : remove = true;
17480 1 : break;
17481 : }
17482 49 : t = OMP_CLAUSE_USES_ALLOCATORS_TRAITS (c);
17483 49 : if (t == error_mark_node)
17484 : {
17485 : remove = true;
17486 : break;
17487 : }
17488 49 : if (t != NULL_TREE
17489 : && t != error_mark_node
17490 49 : && (DECL_EXTERNAL (t)
17491 20 : || TREE_CODE (t) == PARM_DECL))
17492 : {
17493 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must be "
17494 : "defined in same scope as the construct on which the "
17495 : "clause appears", t);
17496 3 : remove = true;
17497 : }
17498 49 : if (t != NULL_TREE)
17499 : {
17500 22 : bool type_err = false;
17501 :
17502 22 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
17503 21 : || DECL_SIZE (t) == NULL_TREE
17504 42 : || !COMPLETE_TYPE_P (TREE_TYPE (t)))
17505 : type_err = true;
17506 : else
17507 : {
17508 20 : tree elem_t = TREE_TYPE (TREE_TYPE (t));
17509 20 : if (TREE_CODE (elem_t) != RECORD_TYPE
17510 40 : || strcmp (IDENTIFIER_POINTER (TYPE_IDENTIFIER (elem_t)),
17511 : "omp_alloctrait_t") != 0
17512 40 : || !TYPE_READONLY (elem_t))
17513 : type_err = true;
17514 : }
17515 19 : if (type_err)
17516 : {
17517 3 : if (t != error_mark_node)
17518 3 : error_at (OMP_CLAUSE_LOCATION (c), "traits array %qE must "
17519 : "be of %<const omp_alloctrait_t []%> type", t);
17520 : else
17521 0 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must "
17522 : "be of %<const omp_alloctrait_t []%> type");
17523 : remove = true;
17524 : }
17525 : else
17526 : {
17527 19 : tree cst_val = decl_constant_value_1 (t, true);
17528 19 : if (cst_val == t)
17529 : {
17530 1 : error_at (OMP_CLAUSE_LOCATION (c), "traits array must be "
17531 : "initialized with constants");
17532 :
17533 1 : remove = true;
17534 : }
17535 : }
17536 : }
17537 46 : if (remove)
17538 : break;
17539 38 : pc = &OMP_CLAUSE_CHAIN (c);
17540 38 : continue;
17541 713 : case OMP_CLAUSE_DEPEND:
17542 713 : depend_clause = c;
17543 : /* FALLTHRU */
17544 870 : case OMP_CLAUSE_AFFINITY:
17545 870 : t = OMP_CLAUSE_DECL (c);
17546 870 : if (OMP_ITERATOR_DECL_P (t))
17547 : {
17548 131 : if (TREE_PURPOSE (t) != last_iterators)
17549 111 : last_iterators_remove
17550 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17551 131 : last_iterators = TREE_PURPOSE (t);
17552 131 : t = TREE_VALUE (t);
17553 131 : if (last_iterators_remove)
17554 34 : t = error_mark_node;
17555 : }
17556 : else
17557 : last_iterators = NULL_TREE;
17558 870 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17559 : {
17560 413 : if (handle_omp_array_sections (c, ort))
17561 : remove = true;
17562 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17563 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17564 : {
17565 1 : error_at (OMP_CLAUSE_LOCATION (c),
17566 : "%<depend%> clause with %<depobj%> dependence "
17567 : "type on array section");
17568 1 : remove = true;
17569 : }
17570 : break;
17571 : }
17572 457 : if (t == error_mark_node)
17573 : remove = true;
17574 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17575 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17576 : {
17577 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17578 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17579 : {
17580 3 : error_at (OMP_CLAUSE_LOCATION (c),
17581 : "%<omp_all_memory%> used with %<depend%> kind "
17582 : "other than %<out%> or %<inout%>");
17583 3 : remove = true;
17584 : }
17585 : }
17586 408 : else if (!lvalue_p (t))
17587 : {
17588 6 : error_at (OMP_CLAUSE_LOCATION (c),
17589 : "%qE is not lvalue expression nor array section in "
17590 : "%qs clause", t,
17591 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17592 3 : remove = true;
17593 : }
17594 405 : else if (TREE_CODE (t) == COMPONENT_REF
17595 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17596 : {
17597 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17598 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17599 2 : error_at (OMP_CLAUSE_LOCATION (c),
17600 : "bit-field %qE in %qs clause", t,
17601 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17602 2 : remove = true;
17603 : }
17604 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17605 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17606 : {
17607 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17608 : {
17609 2 : error_at (OMP_CLAUSE_LOCATION (c),
17610 : "%qE does not have %<omp_depend_t%> type in "
17611 : "%<depend%> clause with %<depobj%> dependence "
17612 : "type", t);
17613 2 : remove = true;
17614 : }
17615 : }
17616 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17617 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17618 : {
17619 2 : error_at (OMP_CLAUSE_LOCATION (c),
17620 : "%qE should not have %<omp_depend_t%> type in "
17621 : "%<depend%> clause with dependence type other than "
17622 : "%<depobj%>", t);
17623 2 : remove = true;
17624 : }
17625 12 : if (!remove)
17626 : {
17627 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17628 12 : t = null_pointer_node;
17629 : else
17630 : {
17631 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17632 : ADDR_EXPR, t, false);
17633 399 : if (addr == error_mark_node)
17634 : {
17635 : remove = true;
17636 : break;
17637 : }
17638 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17639 : RO_UNARY_STAR);
17640 399 : if (t == error_mark_node)
17641 : {
17642 : remove = true;
17643 : break;
17644 : }
17645 : }
17646 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17647 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17648 : else
17649 380 : OMP_CLAUSE_DECL (c) = t;
17650 : }
17651 : break;
17652 :
17653 6707 : case OMP_CLAUSE_MAP:
17654 6707 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17655 58 : goto move_implicit;
17656 6649 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17657 6649 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17658 : {
17659 : remove = true;
17660 : break;
17661 : }
17662 : /* FALLTHRU */
17663 9520 : case OMP_CLAUSE_TO:
17664 9520 : case OMP_CLAUSE_FROM:
17665 9520 : if (OMP_CLAUSE_ITERATORS (c)
17666 9520 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17667 : {
17668 22983 : t = error_mark_node;
17669 : break;
17670 : }
17671 : /* FALLTHRU */
17672 9656 : case OMP_CLAUSE__CACHE_:
17673 9656 : {
17674 9656 : using namespace omp_addr_tokenizer;
17675 9656 : auto_vec<omp_addr_token *, 10> addr_tokens;
17676 :
17677 9656 : t = OMP_CLAUSE_DECL (c);
17678 9656 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17679 : {
17680 2003 : grp_start_p = pc;
17681 2003 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17682 :
17683 2003 : if (handle_omp_array_sections (c, ort))
17684 : remove = true;
17685 : else
17686 : {
17687 1863 : t = OMP_CLAUSE_DECL (c);
17688 1863 : if (!omp_mappable_type (TREE_TYPE (t)))
17689 : {
17690 2 : error_at (OMP_CLAUSE_LOCATION (c),
17691 : "array section does not have mappable type "
17692 : "in %qs clause",
17693 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17694 1 : remove = true;
17695 : }
17696 1862 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17697 : {
17698 2 : error_at (OMP_CLAUSE_LOCATION (c),
17699 : "%<_Atomic%> %qE in %qs clause", t,
17700 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17701 1 : remove = true;
17702 : }
17703 2512 : while (TREE_CODE (t) == ARRAY_REF)
17704 649 : t = TREE_OPERAND (t, 0);
17705 :
17706 1863 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17707 :
17708 1863 : if (!omp_parse_expr (addr_tokens, t))
17709 : {
17710 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17711 : "unsupported map expression %qE",
17712 0 : OMP_CLAUSE_DECL (c));
17713 0 : remove = true;
17714 0 : break;
17715 : }
17716 :
17717 : /* This check is to determine if this will be the only map
17718 : node created for this clause. Otherwise, we'll check
17719 : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17720 : node on the next iteration(s) of the loop. */
17721 3708 : if (addr_tokens.length () >= 4
17722 306 : && addr_tokens[0]->type == STRUCTURE_BASE
17723 304 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17724 304 : && addr_tokens[1]->type == ACCESS_METHOD
17725 304 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17726 304 : && addr_tokens[3]->type == ACCESS_METHOD
17727 2106 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17728 190 : || (addr_tokens[3]->u.access_kind
17729 : == ACCESS_INDEXED_ARRAY)))
17730 : {
17731 55 : tree rt = addr_tokens[1]->expr;
17732 :
17733 55 : gcc_assert (DECL_P (rt));
17734 :
17735 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17736 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17737 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17738 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17739 0 : || bitmap_bit_p (&map_firstprivate_head,
17740 0 : DECL_UID (rt))))
17741 : {
17742 : remove = true;
17743 : break;
17744 : }
17745 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17746 : break;
17747 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17748 : {
17749 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17750 0 : error_at (OMP_CLAUSE_LOCATION (c),
17751 : "%qD appears more than once in motion "
17752 : "clauses", rt);
17753 0 : else if (openacc)
17754 0 : error_at (OMP_CLAUSE_LOCATION (c),
17755 : "%qD appears more than once in data "
17756 : "clauses", rt);
17757 : else
17758 0 : error_at (OMP_CLAUSE_LOCATION (c),
17759 : "%qD appears more than once in map "
17760 : "clauses", rt);
17761 : remove = true;
17762 : }
17763 : else
17764 : {
17765 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17766 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17767 : }
17768 : }
17769 1863 : }
17770 1985 : if (c_oacc_check_attachments (c))
17771 2 : remove = true;
17772 1985 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17773 1808 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17774 1790 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17775 2010 : && !OMP_CLAUSE_SIZE (c))
17776 : /* In this case, we have a single array element which is a
17777 : pointer, and we already set OMP_CLAUSE_SIZE in
17778 : handle_omp_array_sections above. For attach/detach
17779 : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17780 : to zero here. */
17781 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17782 : break;
17783 : }
17784 7653 : else if (!omp_parse_expr (addr_tokens, t))
17785 : {
17786 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17787 : "unsupported map expression %qE",
17788 0 : OMP_CLAUSE_DECL (c));
17789 0 : remove = true;
17790 0 : break;
17791 : }
17792 7653 : if (t == error_mark_node)
17793 : {
17794 : remove = true;
17795 : break;
17796 : }
17797 : /* OpenACC attach / detach clauses must be pointers. */
17798 7641 : if (c_oacc_check_attachments (c))
17799 : {
17800 : remove = true;
17801 : break;
17802 : }
17803 7637 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17804 4789 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17805 4762 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17806 7679 : && !OMP_CLAUSE_SIZE (c))
17807 : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17808 : bias) to zero here, so it is not set erroneously to the pointer
17809 : size later on in gimplify.cc. */
17810 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17811 :
17812 7637 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17813 :
17814 7637 : if (!ai.check_clause (c))
17815 : {
17816 : remove = true;
17817 : break;
17818 : }
17819 :
17820 7629 : if (!ai.map_supported_p ())
17821 : {
17822 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17823 : "unsupported map expression %qE",
17824 7 : OMP_CLAUSE_DECL (c));
17825 7 : remove = true;
17826 7 : break;
17827 : }
17828 :
17829 15244 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17830 : || addr_tokens[0]->type == STRUCTURE_BASE)
17831 : && addr_tokens[1]->type == ACCESS_METHOD);
17832 :
17833 7622 : t = addr_tokens[1]->expr;
17834 :
17835 7622 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17836 0 : goto skip_decl_checks;
17837 :
17838 : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17839 : mapping. OpenACC allows multiple fields of the same structure
17840 : to be written. */
17841 7622 : if (addr_tokens[0]->type == STRUCTURE_BASE
17842 7622 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17843 261 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17844 307 : goto skip_decl_checks;
17845 :
17846 7315 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17847 : {
17848 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17849 : break;
17850 :
17851 6 : error_at (OMP_CLAUSE_LOCATION (c),
17852 : "%qE is not a variable in %qs clause", t,
17853 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17854 3 : remove = true;
17855 : }
17856 7312 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17857 : {
17858 0 : error_at (OMP_CLAUSE_LOCATION (c),
17859 : "%qD is threadprivate variable in %qs clause", t,
17860 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17861 0 : remove = true;
17862 : }
17863 7312 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17864 4491 : || (OMP_CLAUSE_MAP_KIND (c)
17865 : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17866 10760 : && !c_mark_addressable (t))
17867 : remove = true;
17868 7312 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17869 4491 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17870 4491 : || (OMP_CLAUSE_MAP_KIND (c)
17871 : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17872 3448 : || (OMP_CLAUSE_MAP_KIND (c)
17873 : == GOMP_MAP_FORCE_DEVICEPTR)))
17874 6211 : && t == OMP_CLAUSE_DECL (c)
17875 13264 : && !omp_mappable_type (TREE_TYPE (t)))
17876 : {
17877 16 : error_at (OMP_CLAUSE_LOCATION (c),
17878 : "%qD does not have a mappable type in %qs clause", t,
17879 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17880 8 : remove = true;
17881 : }
17882 7304 : else if (TREE_TYPE (t) == error_mark_node)
17883 : remove = true;
17884 7303 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17885 : {
17886 8 : error_at (OMP_CLAUSE_LOCATION (c),
17887 : "%<_Atomic%> %qE in %qs clause", t,
17888 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17889 4 : remove = true;
17890 : }
17891 7299 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17892 4481 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17893 7430 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17894 130 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17895 130 : || bitmap_bit_p (&map_firstprivate_head,
17896 130 : DECL_UID (t))))
17897 : remove = true;
17898 7297 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17899 7297 : && (OMP_CLAUSE_MAP_KIND (c)
17900 : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17901 : {
17902 1041 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17903 1041 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17904 2081 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17905 : {
17906 4 : error_at (OMP_CLAUSE_LOCATION (c),
17907 : "%qD appears more than once in data clauses", t);
17908 4 : remove = true;
17909 : }
17910 1037 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17911 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17912 1039 : && openacc)
17913 : {
17914 1 : error_at (OMP_CLAUSE_LOCATION (c),
17915 : "%qD appears more than once in data clauses", t);
17916 1 : remove = true;
17917 : }
17918 : else
17919 1036 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17920 : }
17921 6256 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17922 65 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17923 18 : && ort != C_ORT_OMP
17924 18 : && ort != C_ORT_OMP_TARGET
17925 6266 : && ort != C_ORT_OMP_EXIT_DATA)
17926 : {
17927 7 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17928 0 : error_at (OMP_CLAUSE_LOCATION (c),
17929 : "%qD appears more than once in motion clauses", t);
17930 7 : else if (openacc)
17931 7 : error_at (OMP_CLAUSE_LOCATION (c),
17932 : "%qD appears more than once in data clauses", t);
17933 : else
17934 0 : error_at (OMP_CLAUSE_LOCATION (c),
17935 : "%qD appears more than once in map clauses", t);
17936 : remove = true;
17937 : }
17938 6249 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17939 : {
17940 0 : error_at (OMP_CLAUSE_LOCATION (c),
17941 : "%qD appears more than once in data clauses", t);
17942 0 : remove = true;
17943 : }
17944 6249 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17945 6249 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17946 : {
17947 9 : if (openacc)
17948 0 : error_at (OMP_CLAUSE_LOCATION (c),
17949 : "%qD appears more than once in data clauses", t);
17950 : else
17951 9 : error_at (OMP_CLAUSE_LOCATION (c),
17952 : "%qD appears both in data and map clauses", t);
17953 : remove = true;
17954 : }
17955 6240 : else if (!omp_access_chain_p (addr_tokens, 1))
17956 : {
17957 6240 : bitmap_set_bit (&map_head, DECL_UID (t));
17958 6240 : if (t != OMP_CLAUSE_DECL (c)
17959 6240 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17960 255 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17961 : }
17962 :
17963 0 : skip_decl_checks:
17964 : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17965 : the containing loop (here) iterates through the new nodes
17966 : created by that expansion. Avoid expanding those again (just
17967 : by checking the node type). */
17968 7622 : if (!remove
17969 7622 : && ort != C_ORT_DECLARE_SIMD
17970 7622 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17971 4739 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17972 3703 : && (OMP_CLAUSE_MAP_KIND (c)
17973 : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17974 3703 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17975 3703 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17976 3402 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17977 3375 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17978 : {
17979 6204 : grp_start_p = pc;
17980 6204 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17981 6204 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17982 : addr_tokens, ort);
17983 6204 : if (nc != error_mark_node)
17984 6204 : c = nc;
17985 : }
17986 9671 : }
17987 7622 : break;
17988 :
17989 232 : case OMP_CLAUSE_ENTER:
17990 232 : case OMP_CLAUSE_LINK:
17991 232 : t = OMP_CLAUSE_DECL (c);
17992 232 : const char *cname;
17993 232 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17994 232 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17995 232 : && OMP_CLAUSE_ENTER_TO (c))
17996 : cname = "to";
17997 232 : if (TREE_CODE (t) == FUNCTION_DECL
17998 232 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17999 : ;
18000 151 : else if (!VAR_P (t))
18001 : {
18002 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
18003 0 : error_at (OMP_CLAUSE_LOCATION (c),
18004 : "%qE is neither a variable nor a function name in "
18005 : "clause %qs", t, cname);
18006 : else
18007 1 : error_at (OMP_CLAUSE_LOCATION (c),
18008 : "%qE is not a variable in clause %qs", t, cname);
18009 : remove = true;
18010 : }
18011 150 : else if (DECL_THREAD_LOCAL_P (t))
18012 : {
18013 2 : error_at (OMP_CLAUSE_LOCATION (c),
18014 : "%qD is threadprivate variable in %qs clause", t,
18015 : cname);
18016 2 : remove = true;
18017 : }
18018 148 : else if (!omp_mappable_type (TREE_TYPE (t)))
18019 : {
18020 6 : error_at (OMP_CLAUSE_LOCATION (c),
18021 : "%qD does not have a mappable type in %qs clause", t,
18022 : cname);
18023 6 : remove = true;
18024 : }
18025 8 : if (remove)
18026 : break;
18027 223 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18028 : {
18029 8 : error_at (OMP_CLAUSE_LOCATION (c),
18030 : "%qE appears more than once on the same "
18031 : "%<declare target%> directive", t);
18032 8 : remove = true;
18033 : }
18034 : else
18035 215 : bitmap_set_bit (&generic_head, DECL_UID (t));
18036 : break;
18037 :
18038 117 : case OMP_CLAUSE_UNIFORM:
18039 117 : t = OMP_CLAUSE_DECL (c);
18040 117 : if (TREE_CODE (t) != PARM_DECL)
18041 : {
18042 0 : if (DECL_P (t))
18043 0 : error_at (OMP_CLAUSE_LOCATION (c),
18044 : "%qD is not an argument in %<uniform%> clause", t);
18045 : else
18046 0 : error_at (OMP_CLAUSE_LOCATION (c),
18047 : "%qE is not an argument in %<uniform%> clause", t);
18048 : remove = true;
18049 : break;
18050 : }
18051 : /* map_head bitmap is used as uniform_head if declare_simd. */
18052 117 : bitmap_set_bit (&map_head, DECL_UID (t));
18053 117 : goto check_dup_generic;
18054 :
18055 161 : case OMP_CLAUSE_IS_DEVICE_PTR:
18056 161 : case OMP_CLAUSE_USE_DEVICE_PTR:
18057 161 : t = OMP_CLAUSE_DECL (c);
18058 161 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
18059 123 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18060 161 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
18061 : {
18062 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
18063 21 : && !openacc)
18064 : {
18065 1 : error_at (OMP_CLAUSE_LOCATION (c),
18066 : "%qs variable is not a pointer",
18067 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18068 1 : remove = true;
18069 : }
18070 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
18071 : {
18072 4 : error_at (OMP_CLAUSE_LOCATION (c),
18073 : "%qs variable is neither a pointer nor an array",
18074 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18075 4 : remove = true;
18076 : }
18077 : }
18078 161 : goto check_dup_generic;
18079 :
18080 89 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
18081 89 : t = OMP_CLAUSE_DECL (c);
18082 89 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
18083 : {
18084 11 : if (handle_omp_array_sections (c, ort))
18085 : remove = true;
18086 : else
18087 : {
18088 11 : t = OMP_CLAUSE_DECL (c);
18089 24 : while (TREE_CODE (t) == ARRAY_REF)
18090 13 : t = TREE_OPERAND (t, 0);
18091 : }
18092 : }
18093 89 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
18094 89 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18095 89 : c_mark_addressable (t);
18096 89 : goto check_dup_generic_t;
18097 :
18098 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
18099 36 : t = OMP_CLAUSE_DECL (c);
18100 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
18101 36 : c_mark_addressable (t);
18102 36 : goto check_dup_generic;
18103 :
18104 4791 : case OMP_CLAUSE_NOWAIT:
18105 4791 : if (copyprivate_seen)
18106 : {
18107 1 : error_at (OMP_CLAUSE_LOCATION (c),
18108 : "%<nowait%> clause must not be used together "
18109 : "with %<copyprivate%> clause");
18110 1 : remove = true;
18111 1 : break;
18112 : }
18113 4790 : nowait_clause = pc;
18114 4790 : pc = &OMP_CLAUSE_CHAIN (c);
18115 4790 : continue;
18116 :
18117 523 : case OMP_CLAUSE_ORDER:
18118 523 : if (ordered_clause)
18119 : {
18120 2 : error_at (OMP_CLAUSE_LOCATION (c),
18121 : "%<order%> clause must not be used together "
18122 : "with %<ordered%> clause");
18123 2 : remove = true;
18124 2 : break;
18125 : }
18126 521 : else if (order_clause)
18127 : {
18128 : /* Silently remove duplicates. */
18129 : remove = true;
18130 : break;
18131 : }
18132 513 : order_clause = pc;
18133 513 : pc = &OMP_CLAUSE_CHAIN (c);
18134 513 : continue;
18135 :
18136 32 : case OMP_CLAUSE_DETACH:
18137 32 : t = OMP_CLAUSE_DECL (c);
18138 32 : if (detach_seen)
18139 : {
18140 1 : error_at (OMP_CLAUSE_LOCATION (c),
18141 : "too many %qs clauses on a task construct",
18142 : "detach");
18143 1 : remove = true;
18144 1 : break;
18145 : }
18146 31 : detach_seen = pc;
18147 31 : pc = &OMP_CLAUSE_CHAIN (c);
18148 31 : c_mark_addressable (t);
18149 31 : continue;
18150 :
18151 14597 : case OMP_CLAUSE_IF:
18152 14597 : case OMP_CLAUSE_SELF:
18153 14597 : case OMP_CLAUSE_NUM_THREADS:
18154 14597 : case OMP_CLAUSE_NUM_TEAMS:
18155 14597 : case OMP_CLAUSE_THREAD_LIMIT:
18156 14597 : case OMP_CLAUSE_DEFAULT:
18157 14597 : case OMP_CLAUSE_UNTIED:
18158 14597 : case OMP_CLAUSE_COLLAPSE:
18159 14597 : case OMP_CLAUSE_FINAL:
18160 14597 : case OMP_CLAUSE_DEVICE:
18161 14597 : case OMP_CLAUSE_DIST_SCHEDULE:
18162 14597 : case OMP_CLAUSE_DYN_GROUPPRIVATE:
18163 14597 : case OMP_CLAUSE_PARALLEL:
18164 14597 : case OMP_CLAUSE_FOR:
18165 14597 : case OMP_CLAUSE_SECTIONS:
18166 14597 : case OMP_CLAUSE_TASKGROUP:
18167 14597 : case OMP_CLAUSE_PROC_BIND:
18168 14597 : case OMP_CLAUSE_DEVICE_TYPE:
18169 14597 : case OMP_CLAUSE_PRIORITY:
18170 14597 : case OMP_CLAUSE_THREADS:
18171 14597 : case OMP_CLAUSE_SIMD:
18172 14597 : case OMP_CLAUSE_HINT:
18173 14597 : case OMP_CLAUSE_FILTER:
18174 14597 : case OMP_CLAUSE_DEFAULTMAP:
18175 14597 : case OMP_CLAUSE_BIND:
18176 14597 : case OMP_CLAUSE_NUM_GANGS:
18177 14597 : case OMP_CLAUSE_NUM_WORKERS:
18178 14597 : case OMP_CLAUSE_VECTOR_LENGTH:
18179 14597 : case OMP_CLAUSE_ASYNC:
18180 14597 : case OMP_CLAUSE_WAIT:
18181 14597 : case OMP_CLAUSE_AUTO:
18182 14597 : case OMP_CLAUSE_INDEPENDENT:
18183 14597 : case OMP_CLAUSE_SEQ:
18184 14597 : case OMP_CLAUSE_GANG:
18185 14597 : case OMP_CLAUSE_WORKER:
18186 14597 : case OMP_CLAUSE_VECTOR:
18187 14597 : case OMP_CLAUSE_TILE:
18188 14597 : case OMP_CLAUSE_IF_PRESENT:
18189 14597 : case OMP_CLAUSE_FINALIZE:
18190 14597 : case OMP_CLAUSE_NOHOST:
18191 14597 : case OMP_CLAUSE_INDIRECT:
18192 14597 : case OMP_CLAUSE_NOVARIANTS:
18193 14597 : case OMP_CLAUSE_NOCONTEXT:
18194 14597 : pc = &OMP_CLAUSE_CHAIN (c);
18195 14597 : continue;
18196 :
18197 62 : case OMP_CLAUSE_GRAINSIZE:
18198 62 : grainsize_seen = pc;
18199 62 : pc = &OMP_CLAUSE_CHAIN (c);
18200 62 : continue;
18201 :
18202 50 : case OMP_CLAUSE_NUM_TASKS:
18203 50 : num_tasks_seen = true;
18204 50 : pc = &OMP_CLAUSE_CHAIN (c);
18205 50 : continue;
18206 :
18207 79 : case OMP_CLAUSE_MERGEABLE:
18208 79 : mergeable_seen = true;
18209 79 : pc = &OMP_CLAUSE_CHAIN (c);
18210 79 : continue;
18211 :
18212 18 : case OMP_CLAUSE_NOGROUP:
18213 18 : nogroup_seen = pc;
18214 18 : pc = &OMP_CLAUSE_CHAIN (c);
18215 18 : continue;
18216 :
18217 3473 : case OMP_CLAUSE_SCHEDULE:
18218 3473 : schedule_clause = c;
18219 3473 : pc = &OMP_CLAUSE_CHAIN (c);
18220 3473 : continue;
18221 :
18222 304 : case OMP_CLAUSE_ORDERED:
18223 304 : ordered_clause = c;
18224 304 : if (order_clause)
18225 : {
18226 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
18227 : "%<order%> clause must not be used together "
18228 : "with %<ordered%> clause");
18229 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
18230 2 : order_clause = NULL;
18231 : }
18232 304 : pc = &OMP_CLAUSE_CHAIN (c);
18233 304 : continue;
18234 :
18235 223 : case OMP_CLAUSE_SAFELEN:
18236 223 : safelen = c;
18237 223 : pc = &OMP_CLAUSE_CHAIN (c);
18238 223 : continue;
18239 320 : case OMP_CLAUSE_SIMDLEN:
18240 320 : simdlen = c;
18241 320 : pc = &OMP_CLAUSE_CHAIN (c);
18242 320 : continue;
18243 :
18244 69 : case OMP_CLAUSE_FULL:
18245 69 : full_seen = pc;
18246 69 : pc = &OMP_CLAUSE_CHAIN (c);
18247 69 : continue;
18248 :
18249 223 : case OMP_CLAUSE_PARTIAL:
18250 223 : partial_seen = true;
18251 223 : pc = &OMP_CLAUSE_CHAIN (c);
18252 223 : continue;
18253 :
18254 0 : case OMP_CLAUSE_SIZES:
18255 0 : pc = &OMP_CLAUSE_CHAIN (c);
18256 0 : continue;
18257 :
18258 205 : case OMP_CLAUSE_INBRANCH:
18259 205 : case OMP_CLAUSE_NOTINBRANCH:
18260 205 : if (branch_seen)
18261 : {
18262 1 : error_at (OMP_CLAUSE_LOCATION (c),
18263 : "%<inbranch%> clause is incompatible with "
18264 : "%<notinbranch%>");
18265 1 : remove = true;
18266 1 : break;
18267 : }
18268 204 : branch_seen = true;
18269 204 : pc = &OMP_CLAUSE_CHAIN (c);
18270 204 : continue;
18271 :
18272 368 : case OMP_CLAUSE_INCLUSIVE:
18273 368 : case OMP_CLAUSE_EXCLUSIVE:
18274 368 : need_complete = true;
18275 368 : need_implicitly_determined = true;
18276 368 : t = OMP_CLAUSE_DECL (c);
18277 368 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
18278 : {
18279 0 : error_at (OMP_CLAUSE_LOCATION (c),
18280 : "%qE is not a variable in clause %qs", t,
18281 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18282 0 : remove = true;
18283 : }
18284 : break;
18285 :
18286 152 : case OMP_CLAUSE_INIT:
18287 152 : init_seen = true;
18288 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
18289 71 : init_no_targetsync_clause = c;
18290 : /* FALLTHRU */
18291 242 : case OMP_CLAUSE_DESTROY:
18292 242 : case OMP_CLAUSE_USE:
18293 242 : init_use_destroy_seen = true;
18294 242 : t = OMP_CLAUSE_DECL (c);
18295 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
18296 : {
18297 3 : error_at (OMP_CLAUSE_LOCATION (c),
18298 : "%qD appears more than once in action clauses", t);
18299 3 : remove = true;
18300 3 : break;
18301 : }
18302 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
18303 : /* FALLTHRU */
18304 298 : case OMP_CLAUSE_INTEROP:
18305 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
18306 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
18307 : {
18308 40 : error_at (OMP_CLAUSE_LOCATION (c),
18309 : "%qD must be of %<omp_interop_t%>",
18310 20 : OMP_CLAUSE_DECL (c));
18311 20 : remove = true;
18312 : }
18313 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
18314 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
18315 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
18316 : {
18317 12 : error_at (OMP_CLAUSE_LOCATION (c),
18318 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
18319 6 : remove = true;
18320 : }
18321 298 : pc = &OMP_CLAUSE_CHAIN (c);
18322 298 : break;
18323 0 : default:
18324 0 : gcc_unreachable ();
18325 24994 : }
18326 :
18327 22983 : if (!remove)
18328 : {
18329 22423 : t = OMP_CLAUSE_DECL (c);
18330 :
18331 22423 : if (need_complete)
18332 : {
18333 3963 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
18334 3963 : if (t == error_mark_node)
18335 7 : remove = true;
18336 : }
18337 :
18338 22423 : if (need_implicitly_determined)
18339 : {
18340 9576 : const char *share_name = NULL;
18341 :
18342 9576 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
18343 : share_name = "threadprivate";
18344 9570 : else switch (c_omp_predetermined_sharing (t))
18345 : {
18346 : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18347 : break;
18348 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18349 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18350 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18351 28 : && c_omp_predefined_variable (t))
18352 : /* The __func__ variable and similar function-local
18353 : predefined variables may be listed in a shared or
18354 : firstprivate clause. */
18355 : break;
18356 : share_name = "shared";
18357 : break;
18358 : case OMP_CLAUSE_DEFAULT_PRIVATE:
18359 : share_name = "private";
18360 : break;
18361 0 : default:
18362 0 : gcc_unreachable ();
18363 : }
18364 : if (share_name)
18365 : {
18366 20 : error_at (OMP_CLAUSE_LOCATION (c),
18367 : "%qE is predetermined %qs for %qs",
18368 : t, share_name,
18369 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18370 10 : remove = true;
18371 : }
18372 9566 : else if (TREE_READONLY (t)
18373 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18374 9579 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18375 : {
18376 4 : error_at (OMP_CLAUSE_LOCATION (c),
18377 : "%<const%> qualified %qE may appear only in "
18378 : "%<shared%> or %<firstprivate%> clauses", t);
18379 4 : remove = true;
18380 : }
18381 : }
18382 : }
18383 :
18384 22423 : if (remove)
18385 : {
18386 581 : if (grp_start_p)
18387 : {
18388 : /* If we found a clause to remove, we want to remove the whole
18389 : expanded group, otherwise gimplify
18390 : (omp_resolve_clause_dependencies) can get confused. */
18391 153 : *grp_start_p = grp_sentinel;
18392 153 : pc = grp_start_p;
18393 153 : grp_start_p = NULL;
18394 : }
18395 : else
18396 428 : *pc = OMP_CLAUSE_CHAIN (c);
18397 : }
18398 : else
18399 22402 : pc = &OMP_CLAUSE_CHAIN (c);
18400 : }
18401 :
18402 29626 : if (grp_start_p
18403 29626 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18404 52 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18405 32 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18406 :
18407 29626 : if (simdlen
18408 29626 : && safelen
18409 29743 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18410 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18411 : {
18412 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18413 : "%<simdlen%> clause value is bigger than "
18414 : "%<safelen%> clause value");
18415 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18416 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18417 : }
18418 :
18419 29626 : if (ordered_clause
18420 29626 : && schedule_clause
18421 29626 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18422 : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18423 : {
18424 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18425 : "%<nonmonotonic%> schedule modifier specified together "
18426 : "with %<ordered%> clause");
18427 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18428 7 : = (enum omp_clause_schedule_kind)
18429 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18430 : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18431 : }
18432 :
18433 29626 : if (reduction_seen < 0 && ordered_clause)
18434 : {
18435 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18436 : "%qs clause specified together with %<inscan%> "
18437 : "%<reduction%> clause", "ordered");
18438 2 : reduction_seen = -2;
18439 : }
18440 :
18441 29626 : if (reduction_seen < 0 && schedule_clause)
18442 : {
18443 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18444 : "%qs clause specified together with %<inscan%> "
18445 : "%<reduction%> clause", "schedule");
18446 3 : reduction_seen = -2;
18447 : }
18448 :
18449 29626 : if (linear_variable_step_check
18450 29626 : || reduction_seen == -2
18451 : || allocate_seen
18452 29614 : || target_in_reduction_seen)
18453 5208 : for (pc = &clauses, c = clauses; c ; c = *pc)
18454 : {
18455 4604 : bool remove = false;
18456 4604 : if (allocate_seen)
18457 4446 : switch (OMP_CLAUSE_CODE (c))
18458 : {
18459 430 : case OMP_CLAUSE_REDUCTION:
18460 430 : case OMP_CLAUSE_IN_REDUCTION:
18461 430 : case OMP_CLAUSE_TASK_REDUCTION:
18462 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18463 : {
18464 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18465 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18466 0 : t = TREE_OPERAND (t, 0);
18467 23 : if (TREE_CODE (t) == ADDR_EXPR
18468 10 : || INDIRECT_REF_P (t))
18469 13 : t = TREE_OPERAND (t, 0);
18470 23 : if (DECL_P (t))
18471 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18472 : break;
18473 : }
18474 : /* FALLTHRU */
18475 1324 : case OMP_CLAUSE_PRIVATE:
18476 1324 : case OMP_CLAUSE_FIRSTPRIVATE:
18477 1324 : case OMP_CLAUSE_LASTPRIVATE:
18478 1324 : case OMP_CLAUSE_LINEAR:
18479 1324 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18480 2648 : bitmap_clear_bit (&aligned_head,
18481 1324 : DECL_UID (OMP_CLAUSE_DECL (c)));
18482 : break;
18483 : default:
18484 : break;
18485 : }
18486 4604 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18487 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18488 4611 : && !bitmap_bit_p (&map_head,
18489 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18490 : {
18491 2 : error_at (OMP_CLAUSE_LOCATION (c),
18492 : "%<linear%> clause step is a parameter %qD not "
18493 : "specified in %<uniform%> clause",
18494 2 : OMP_CLAUSE_LINEAR_STEP (c));
18495 2 : remove = true;
18496 : }
18497 4602 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18498 4602 : && reduction_seen == -2)
18499 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18500 4604 : if (target_in_reduction_seen
18501 4604 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18502 : {
18503 256 : tree t = OMP_CLAUSE_DECL (c);
18504 256 : while (handled_component_p (t)
18505 : || INDIRECT_REF_P (t)
18506 : || TREE_CODE (t) == ADDR_EXPR
18507 : || TREE_CODE (t) == MEM_REF
18508 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18509 32 : t = TREE_OPERAND (t, 0);
18510 256 : if (DECL_P (t)
18511 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18512 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18513 : }
18514 :
18515 4604 : if (remove)
18516 2 : *pc = OMP_CLAUSE_CHAIN (c);
18517 : else
18518 4602 : pc = &OMP_CLAUSE_CHAIN (c);
18519 : }
18520 :
18521 604 : if (allocate_seen)
18522 5024 : for (pc = &clauses, c = clauses; c ; c = *pc)
18523 : {
18524 4446 : bool remove = false;
18525 4446 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18526 726 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18527 5152 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18528 : {
18529 3 : error_at (OMP_CLAUSE_LOCATION (c),
18530 : "%qD specified in %<allocate%> clause but not in "
18531 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18532 3 : remove = true;
18533 : }
18534 4446 : if (remove)
18535 3 : *pc = OMP_CLAUSE_CHAIN (c);
18536 : else
18537 4443 : pc = &OMP_CLAUSE_CHAIN (c);
18538 : }
18539 :
18540 29626 : if (nogroup_seen && reduction_seen)
18541 : {
18542 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18543 : "%<nogroup%> clause must not be used together with "
18544 : "%<reduction%> clause");
18545 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18546 : }
18547 :
18548 29626 : if (grainsize_seen && num_tasks_seen)
18549 : {
18550 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18551 : "%<grainsize%> clause must not be used together with "
18552 : "%<num_tasks%> clause");
18553 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18554 : }
18555 :
18556 29626 : if (full_seen && partial_seen)
18557 : {
18558 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18559 : "%<full%> clause must not be used together with "
18560 : "%<partial%> clause");
18561 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18562 : }
18563 :
18564 29626 : if (detach_seen)
18565 : {
18566 31 : if (mergeable_seen)
18567 : {
18568 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18569 : "%<detach%> clause must not be used together with "
18570 : "%<mergeable%> clause");
18571 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18572 : }
18573 : else
18574 : {
18575 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18576 :
18577 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18578 : {
18579 50 : bool remove = false;
18580 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18581 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18582 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18583 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18584 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18585 : {
18586 2 : error_at (OMP_CLAUSE_LOCATION (c),
18587 : "the event handle of a %<detach%> clause "
18588 : "should not be in a data-sharing clause");
18589 2 : remove = true;
18590 : }
18591 50 : if (remove)
18592 2 : *pc = OMP_CLAUSE_CHAIN (c);
18593 : else
18594 48 : pc = &OMP_CLAUSE_CHAIN (c);
18595 : }
18596 : }
18597 : }
18598 :
18599 29626 : if (ort == C_ORT_OMP_INTEROP
18600 29626 : && depend_clause
18601 21 : && (!init_use_destroy_seen
18602 20 : || (init_seen && init_no_targetsync_clause)))
18603 : {
18604 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18605 : "%<depend%> clause requires action clauses with "
18606 : "%<targetsync%> interop-type");
18607 3 : if (init_no_targetsync_clause)
18608 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18609 : "%<init%> clause lacks the %<targetsync%> modifier");
18610 : }
18611 :
18612 29626 : bitmap_obstack_release (NULL);
18613 29626 : return clauses;
18614 : }
18615 :
18616 : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18617 : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18618 :
18619 : tree
18620 15 : c_omp_finish_mapper_clauses (tree clauses)
18621 : {
18622 15 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18623 : }
18624 :
18625 : /* Return code to initialize DST with a copy constructor from SRC.
18626 : C doesn't have copy constructors nor assignment operators, only for
18627 : _Atomic vars we need to perform __atomic_load from src into a temporary
18628 : followed by __atomic_store of the temporary to dst. */
18629 :
18630 : tree
18631 18753 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18632 : {
18633 18753 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18634 18749 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18635 :
18636 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18637 4 : tree type = TREE_TYPE (dst);
18638 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18639 4 : tree tmp = create_tmp_var (nonatomic_type);
18640 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18641 4 : TREE_ADDRESSABLE (tmp) = 1;
18642 4 : suppress_warning (tmp);
18643 4 : tree src_addr = build_fold_addr_expr (src);
18644 4 : tree dst_addr = build_fold_addr_expr (dst);
18645 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18646 4 : vec<tree, va_gc> *params;
18647 : /* Expansion of a generic atomic load may require an addition
18648 : element, so allocate enough to prevent a resize. */
18649 4 : vec_alloc (params, 4);
18650 :
18651 : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18652 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18653 4 : params->quick_push (src_addr);
18654 4 : params->quick_push (tmp_addr);
18655 4 : params->quick_push (seq_cst);
18656 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18657 :
18658 4 : vec_alloc (params, 4);
18659 :
18660 : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18661 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18662 4 : params->quick_push (dst_addr);
18663 4 : params->quick_push (tmp_addr);
18664 4 : params->quick_push (seq_cst);
18665 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18666 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18667 : }
18668 :
18669 : /* Create a transaction node. */
18670 :
18671 : tree
18672 135 : c_finish_transaction (location_t loc, tree block, int flags)
18673 : {
18674 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18675 135 : if (flags & TM_STMT_ATTR_OUTER)
18676 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18677 135 : if (flags & TM_STMT_ATTR_RELAXED)
18678 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18679 135 : return add_stmt (stmt);
18680 : }
18681 :
18682 : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18683 : down to the element type of an array. If ORIG_QUAL_TYPE is not
18684 : NULL, then it should be used as the qualified type
18685 : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18686 : preserve information about the typedef name from which an array
18687 : type was derived). */
18688 :
18689 : tree
18690 74980014 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18691 : size_t orig_qual_indirect)
18692 : {
18693 74980014 : if (type == error_mark_node)
18694 : return type;
18695 :
18696 74979862 : if (TREE_CODE (type) == ARRAY_TYPE)
18697 : {
18698 3150384 : tree t;
18699 3150384 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18700 : type_quals, orig_qual_type,
18701 : orig_qual_indirect - 1);
18702 :
18703 : /* See if we already have an identically qualified type. */
18704 3150384 : if (orig_qual_type && orig_qual_indirect == 0)
18705 : t = orig_qual_type;
18706 : else
18707 4593639 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18708 : {
18709 4453843 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18710 3031543 : && TYPE_NAME (t) == TYPE_NAME (type)
18711 3010547 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18712 7464390 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18713 3010547 : TYPE_ATTRIBUTES (type)))
18714 : break;
18715 : }
18716 3150343 : if (!t)
18717 : {
18718 139796 : tree domain = TYPE_DOMAIN (type);
18719 :
18720 139796 : t = build_variant_type_copy (type);
18721 139796 : TREE_TYPE (t) = element_type;
18722 139796 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18723 :
18724 139796 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18725 139796 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18726 196 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18727 139600 : else if (TYPE_CANONICAL (element_type) != element_type
18728 139600 : || (domain && TYPE_CANONICAL (domain) != domain))
18729 : {
18730 2779 : tree unqualified_canon
18731 5329 : = c_build_array_type (TYPE_CANONICAL (element_type),
18732 2550 : domain ? TYPE_CANONICAL (domain)
18733 : : NULL_TREE);
18734 2779 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18735 : {
18736 0 : unqualified_canon
18737 0 : = build_distinct_type_copy (unqualified_canon);
18738 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18739 : }
18740 2779 : TYPE_CANONICAL (t)
18741 5558 : = c_build_qualified_type (unqualified_canon, type_quals);
18742 : }
18743 : else
18744 136821 : TYPE_CANONICAL (t) = t;
18745 : }
18746 3150384 : return t;
18747 : }
18748 :
18749 : /* A restrict-qualified pointer type must be a pointer to object or
18750 : incomplete type. Note that the use of POINTER_TYPE_P also allows
18751 : REFERENCE_TYPEs, which is appropriate for C++. */
18752 71829478 : if ((type_quals & TYPE_QUAL_RESTRICT)
18753 71829478 : && (!POINTER_TYPE_P (type)
18754 3407971 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18755 : {
18756 3 : error ("invalid use of %<restrict%>");
18757 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18758 : }
18759 :
18760 71829478 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18761 71829478 : ? orig_qual_type
18762 71829447 : : build_qualified_type (type, type_quals));
18763 :
18764 71829478 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18765 : == C_TYPE_VARIABLE_SIZE (type));
18766 71829478 : gcc_checking_assert (c_type_variably_modified_p (var_type)
18767 : == c_type_variably_modified_p (type));
18768 :
18769 : /* A variant type does not inherit the list of incomplete vars from the
18770 : type main variant. */
18771 71829478 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18772 69743683 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18773 71935160 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18774 1413513 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18775 : return var_type;
18776 : }
18777 :
18778 : /* Build a VA_ARG_EXPR for the C parser. */
18779 :
18780 : tree
18781 19932 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type,
18782 : tree type_expr)
18783 : {
18784 19932 : if (error_operand_p (type))
18785 2 : return error_mark_node;
18786 : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18787 : order because it takes the address of the expression. */
18788 19930 : else if (handled_component_p (expr)
18789 31 : && reverse_storage_order_for_component_p (expr))
18790 : {
18791 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18792 0 : return error_mark_node;
18793 : }
18794 19930 : else if (!COMPLETE_TYPE_P (type))
18795 : {
18796 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18797 : "type %qT", type);
18798 11 : return error_mark_node;
18799 : }
18800 19919 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18801 : {
18802 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18803 : type);
18804 1 : return error_mark_node;
18805 : }
18806 6 : else if (TREE_CODE (type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (type)
18807 19921 : && !flag_isoc99)
18808 : {
18809 1 : error_at (loc2, "second argument to %<va_arg%> is an array type %qT",
18810 : type);
18811 1 : return error_mark_node;
18812 : }
18813 19917 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18814 1 : warning_at (loc2, OPT_Wc___compat,
18815 : "C++ requires promoted type, not enum type, in %<va_arg%>");
18816 :
18817 19917 : if (flag_isoc99 && TREE_CODE (type) == ARRAY_TYPE)
18818 : {
18819 3 : warning_at (loc2, 0, "second argument to %<va_arg%> is an array type %qT",
18820 : type);
18821 : /* We create a trap but evaluate side effects first. */
18822 3 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
18823 3 : trapfn = build_call_expr_loc (loc2, trapfn, 0);
18824 3 : tree e2 = build2 (COMPOUND_EXPR, void_type_node, expr, trapfn);
18825 : /* Return a compound literal of the right type. */
18826 3 : tree e1 = build_compound_literal (loc2, type, NULL, true, 0, NULL);
18827 3 : expr = build2 (COMPOUND_EXPR, type, e2, e1);
18828 3 : }
18829 : else
18830 19914 : expr = build_va_arg (loc2, expr, type);
18831 :
18832 19917 : if (type_expr)
18833 26 : expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), type_expr, expr);
18834 :
18835 : return expr;
18836 : }
18837 :
18838 : /* Return truthvalue of whether T1 is the same tree structure as T2.
18839 : Return 1 if they are the same. Return false if they are different. */
18840 :
18841 : bool
18842 1854 : c_tree_equal (tree t1, tree t2)
18843 : {
18844 1883 : enum tree_code code1, code2;
18845 :
18846 1883 : if (t1 == t2)
18847 : return true;
18848 661 : if (!t1 || !t2)
18849 : return false;
18850 :
18851 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18852 20 : code1 = TREE_CODE (t1))
18853 20 : t1 = TREE_OPERAND (t1, 0);
18854 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18855 21 : code2 = TREE_CODE (t2))
18856 21 : t2 = TREE_OPERAND (t2, 0);
18857 :
18858 : /* They might have become equal now. */
18859 661 : if (t1 == t2)
18860 : return true;
18861 :
18862 640 : if (code1 != code2)
18863 : return false;
18864 :
18865 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18866 : return false;
18867 :
18868 379 : switch (code1)
18869 : {
18870 2 : case INTEGER_CST:
18871 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18872 :
18873 10 : case REAL_CST:
18874 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18875 :
18876 0 : case STRING_CST:
18877 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18878 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18879 0 : TREE_STRING_LENGTH (t1));
18880 :
18881 0 : case FIXED_CST:
18882 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18883 : TREE_FIXED_CST (t2));
18884 :
18885 0 : case COMPLEX_CST:
18886 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18887 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18888 :
18889 0 : case VECTOR_CST:
18890 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18891 :
18892 0 : case CONSTRUCTOR:
18893 : /* We need to do this when determining whether or not two
18894 : non-type pointer to member function template arguments
18895 : are the same. */
18896 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18897 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18898 : return false;
18899 : {
18900 : tree field, value;
18901 : unsigned int i;
18902 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18903 : {
18904 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18905 0 : if (!c_tree_equal (field, elt2->index)
18906 0 : || !c_tree_equal (value, elt2->value))
18907 0 : return false;
18908 : }
18909 : }
18910 : return true;
18911 :
18912 0 : case TREE_LIST:
18913 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18914 : return false;
18915 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18916 : return false;
18917 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18918 :
18919 0 : case SAVE_EXPR:
18920 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18921 :
18922 25 : case CALL_EXPR:
18923 25 : {
18924 25 : tree arg1, arg2;
18925 25 : call_expr_arg_iterator iter1, iter2;
18926 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18927 : return false;
18928 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18929 25 : arg2 = first_call_expr_arg (t2, &iter2);
18930 25 : arg1 && arg2;
18931 0 : arg1 = next_call_expr_arg (&iter1),
18932 0 : arg2 = next_call_expr_arg (&iter2))
18933 0 : if (!c_tree_equal (arg1, arg2))
18934 : return false;
18935 25 : if (arg1 || arg2)
18936 : return false;
18937 : return true;
18938 : }
18939 :
18940 0 : case TARGET_EXPR:
18941 0 : {
18942 0 : tree o1 = TREE_OPERAND (t1, 0);
18943 0 : tree o2 = TREE_OPERAND (t2, 0);
18944 :
18945 : /* Special case: if either target is an unallocated VAR_DECL,
18946 : it means that it's going to be unified with whatever the
18947 : TARGET_EXPR is really supposed to initialize, so treat it
18948 : as being equivalent to anything. */
18949 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18950 0 : && !DECL_RTL_SET_P (o1))
18951 : /*Nop*/;
18952 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18953 0 : && !DECL_RTL_SET_P (o2))
18954 : /*Nop*/;
18955 0 : else if (!c_tree_equal (o1, o2))
18956 : return false;
18957 :
18958 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18959 : }
18960 :
18961 29 : case COMPONENT_REF:
18962 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18963 : return false;
18964 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18965 :
18966 : case PARM_DECL:
18967 : case VAR_DECL:
18968 : case CONST_DECL:
18969 : case FIELD_DECL:
18970 : case FUNCTION_DECL:
18971 : case IDENTIFIER_NODE:
18972 : case SSA_NAME:
18973 : return false;
18974 :
18975 0 : case TREE_VEC:
18976 0 : {
18977 0 : unsigned ix;
18978 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18979 : return false;
18980 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18981 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18982 0 : TREE_VEC_ELT (t2, ix)))
18983 : return false;
18984 : return true;
18985 : }
18986 :
18987 22 : CASE_CONVERT:
18988 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18989 : return false;
18990 : break;
18991 :
18992 : default:
18993 : break;
18994 : }
18995 :
18996 131 : switch (TREE_CODE_CLASS (code1))
18997 : {
18998 131 : case tcc_unary:
18999 131 : case tcc_binary:
19000 131 : case tcc_comparison:
19001 131 : case tcc_expression:
19002 131 : case tcc_vl_exp:
19003 131 : case tcc_reference:
19004 131 : case tcc_statement:
19005 131 : {
19006 131 : int i, n = TREE_OPERAND_LENGTH (t1);
19007 :
19008 131 : switch (code1)
19009 : {
19010 0 : case PREINCREMENT_EXPR:
19011 0 : case PREDECREMENT_EXPR:
19012 0 : case POSTINCREMENT_EXPR:
19013 0 : case POSTDECREMENT_EXPR:
19014 0 : n = 1;
19015 0 : break;
19016 16 : case ARRAY_REF:
19017 16 : n = 2;
19018 16 : break;
19019 : default:
19020 : break;
19021 : }
19022 :
19023 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
19024 131 : && n != TREE_OPERAND_LENGTH (t2))
19025 : return false;
19026 :
19027 305 : for (i = 0; i < n; ++i)
19028 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
19029 : return false;
19030 :
19031 : return true;
19032 : }
19033 :
19034 0 : case tcc_type:
19035 0 : return comptypes (t1, t2);
19036 0 : default:
19037 0 : gcc_unreachable ();
19038 : }
19039 : }
19040 :
19041 : /* Returns true when the function declaration FNDECL is implicit,
19042 : introduced as a result of a call to an otherwise undeclared
19043 : function, and false otherwise. */
19044 :
19045 : bool
19046 2013 : c_decl_implicit (const_tree fndecl)
19047 : {
19048 2013 : return C_DECL_IMPLICIT (fndecl);
19049 : }
|