Branch data Line data Source code
1 : : /* Build expressions with type checking for C compiler.
2 : : Copyright (C) 1987-2025 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 : : /* True when parsing OpenMP loop expressions. */
82 : : bool c_in_omp_for;
83 : :
84 : : /* True when parsing OpenMP map clause. */
85 : : bool c_omp_array_section_p;
86 : :
87 : : /* The argument of last parsed sizeof expression, only to be tested
88 : : if expr.original_code == SIZEOF_EXPR. */
89 : : tree c_last_sizeof_arg;
90 : : location_t c_last_sizeof_loc;
91 : :
92 : : /* Nonzero if we might need to print a "missing braces around
93 : : initializer" message within this initializer. */
94 : : static int found_missing_braces;
95 : :
96 : : static bool require_constant_value;
97 : : static bool require_constant_elements;
98 : : static bool require_constexpr_value;
99 : :
100 : : static tree qualify_type (tree, tree);
101 : : struct comptypes_data;
102 : : static bool tagged_types_tu_compatible_p (const_tree, const_tree,
103 : : struct comptypes_data *);
104 : : static bool comp_target_types (location_t, tree, tree);
105 : : static bool function_types_compatible_p (const_tree, const_tree,
106 : : struct comptypes_data *);
107 : : static bool type_lists_compatible_p (const_tree, const_tree,
108 : : struct comptypes_data *);
109 : : static int convert_arguments (location_t, vec<location_t>, tree,
110 : : vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
111 : : tree);
112 : : static tree pointer_diff (location_t, tree, tree, tree *);
113 : : static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
114 : : enum impl_conv, bool, tree, tree, int,
115 : : int = 0);
116 : : static tree valid_compound_expr_initializer (tree, tree);
117 : : static void push_string (const char *);
118 : : static void push_member_name (tree);
119 : : static int spelling_length (void);
120 : : static char *print_spelling (char *);
121 : : static void warning_init (location_t, int, const char *);
122 : : static tree digest_init (location_t, tree, tree, tree, tree, bool, bool, bool,
123 : : bool, bool, bool);
124 : : static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
125 : : bool, struct obstack *);
126 : : static void output_pending_init_elements (int, struct obstack *);
127 : : static bool set_designator (location_t, bool, struct obstack *);
128 : : static void push_range_stack (tree, struct obstack *);
129 : : static void add_pending_init (location_t, tree, tree, tree, bool,
130 : : struct obstack *);
131 : : static void set_nonincremental_init (struct obstack *);
132 : : static void set_nonincremental_init_from_string (tree, struct obstack *);
133 : : static tree find_init_member (tree, struct obstack *);
134 : : static void readonly_warning (tree, enum lvalue_use);
135 : : static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
136 : : static void record_maybe_used_decl (tree);
137 : : static bool comptypes_internal (const_tree, const_tree,
138 : : struct comptypes_data *data);
139 : : static bool comptypes_check_for_composite (tree t1, tree t2);
140 : : static bool handle_counted_by_p (tree);
141 : :
142 : : /* Return true if EXP is a null pointer constant, false otherwise. */
143 : :
144 : : bool
145 : 184007311 : null_pointer_constant_p (const_tree expr)
146 : : {
147 : : /* This should really operate on c_expr structures, but they aren't
148 : : yet available everywhere required. */
149 : 184007311 : tree type = TREE_TYPE (expr);
150 : :
151 : : /* An integer constant expression with the value 0, such an expression
152 : : cast to type void*, or the predefined constant nullptr, are a null
153 : : pointer constant. */
154 : 184007311 : if (expr == nullptr_node)
155 : : return true;
156 : :
157 : 184006663 : return (TREE_CODE (expr) == INTEGER_CST
158 : 18676011 : && !TREE_OVERFLOW (expr)
159 : 18675914 : && integer_zerop (expr)
160 : 187863185 : && (INTEGRAL_TYPE_P (type)
161 : 330062 : || (TREE_CODE (type) == POINTER_TYPE
162 : 330060 : && VOID_TYPE_P (TREE_TYPE (type))
163 : 254960 : && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
164 : : }
165 : :
166 : : /* EXPR may appear in an unevaluated part of an integer constant
167 : : expression, but not in an evaluated part. Wrap it in a
168 : : C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
169 : : INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
170 : :
171 : : static tree
172 : 62354 : note_integer_operands (tree expr)
173 : : {
174 : 62354 : tree ret;
175 : 62354 : if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
176 : : {
177 : 22 : ret = copy_node (expr);
178 : 22 : TREE_OVERFLOW (ret) = 1;
179 : : }
180 : : else
181 : : {
182 : 62332 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
183 : 62332 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
184 : : }
185 : 62354 : return ret;
186 : : }
187 : :
188 : : /* Having checked whether EXPR may appear in an unevaluated part of an
189 : : integer constant expression and found that it may, remove any
190 : : C_MAYBE_CONST_EXPR noting this fact and return the resulting
191 : : expression. */
192 : :
193 : : static inline tree
194 : 32971153 : remove_c_maybe_const_expr (tree expr)
195 : : {
196 : 32971153 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
197 : 61506 : return C_MAYBE_CONST_EXPR_EXPR (expr);
198 : : else
199 : : return expr;
200 : : }
201 : :
202 : : /* This is a cache to hold if two types are seen. */
203 : :
204 : : struct tagged_tu_seen_cache {
205 : : const struct tagged_tu_seen_cache * next;
206 : : const_tree t1;
207 : : const_tree t2;
208 : : };
209 : :
210 : : /* Do `exp = require_complete_type (loc, exp);' to make sure exp
211 : : does not have an incomplete type. (That includes void types.)
212 : : LOC is the location of the use. */
213 : :
214 : : tree
215 : 755498682 : require_complete_type (location_t loc, tree value)
216 : : {
217 : 755498682 : tree type = TREE_TYPE (value);
218 : :
219 : 755498682 : if (error_operand_p (value))
220 : 9038 : return error_mark_node;
221 : :
222 : : /* First, detect a valid value with a complete type. */
223 : 755489644 : if (COMPLETE_TYPE_P (type))
224 : : return value;
225 : :
226 : 142 : c_incomplete_type_error (loc, value, type);
227 : 142 : return error_mark_node;
228 : : }
229 : :
230 : : /* Print an error message for invalid use of an incomplete type.
231 : : VALUE is the expression that was used (or 0 if that isn't known)
232 : : and TYPE is the type that was invalid. LOC is the location for
233 : : the error. */
234 : :
235 : : void
236 : 198 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
237 : : {
238 : : /* Avoid duplicate error message. */
239 : 198 : if (TREE_CODE (type) == ERROR_MARK)
240 : : return;
241 : :
242 : 198 : if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
243 : 88 : error_at (loc, "%qD has an incomplete type %qT", value, type);
244 : : else
245 : : {
246 : 110 : retry:
247 : : /* We must print an error message. Be clever about what it says. */
248 : :
249 : 110 : switch (TREE_CODE (type))
250 : : {
251 : 70 : case RECORD_TYPE:
252 : 70 : case UNION_TYPE:
253 : 70 : case ENUMERAL_TYPE:
254 : 70 : break;
255 : :
256 : 34 : case VOID_TYPE:
257 : 34 : error_at (loc, "invalid use of void expression");
258 : 34 : return;
259 : :
260 : 6 : case ARRAY_TYPE:
261 : 6 : if (TYPE_DOMAIN (type))
262 : : {
263 : 3 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
264 : : {
265 : 3 : error_at (loc, "invalid use of flexible array member");
266 : 3 : return;
267 : : }
268 : 0 : type = TREE_TYPE (type);
269 : 0 : goto retry;
270 : : }
271 : 3 : error_at (loc, "invalid use of array with unspecified bounds");
272 : 3 : return;
273 : :
274 : 0 : default:
275 : 0 : gcc_unreachable ();
276 : : }
277 : :
278 : 70 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
279 : 68 : error_at (loc, "invalid use of undefined type %qT", type);
280 : : else
281 : : /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
282 : 2 : error_at (loc, "invalid use of incomplete typedef %qT", type);
283 : : }
284 : : }
285 : :
286 : : /* Given a type, apply default promotions wrt unnamed function
287 : : arguments and return the new type. */
288 : :
289 : : tree
290 : 121559352 : c_type_promotes_to (tree type)
291 : : {
292 : 121559352 : tree ret = NULL_TREE;
293 : :
294 : 121559352 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
295 : 1479487 : ret = double_type_node;
296 : 120079865 : else if (c_promoting_integer_type_p (type))
297 : : {
298 : : /* Preserve unsignedness if not really getting any wider. */
299 : 18190131 : if (TYPE_UNSIGNED (type)
300 : 18190131 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
301 : 0 : ret = unsigned_type_node;
302 : : else
303 : 18190131 : ret = integer_type_node;
304 : : }
305 : :
306 : 19669618 : if (ret != NULL_TREE)
307 : 19669618 : return (TYPE_ATOMIC (type)
308 : 19669618 : ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
309 : : : ret);
310 : :
311 : : return type;
312 : : }
313 : :
314 : : /* Return true if between two named address spaces, whether there is a superset
315 : : named address space that encompasses both address spaces. If there is a
316 : : superset, return which address space is the superset. */
317 : :
318 : : static bool
319 : 7250058 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
320 : : {
321 : 7250058 : if (as1 == as2)
322 : : {
323 : 7250058 : *common = as1;
324 : 7250058 : return true;
325 : : }
326 : 0 : else if (targetm.addr_space.subset_p (as1, as2))
327 : : {
328 : 0 : *common = as2;
329 : 0 : return true;
330 : : }
331 : 0 : else if (targetm.addr_space.subset_p (as2, as1))
332 : : {
333 : 0 : *common = as1;
334 : 0 : return true;
335 : : }
336 : : else
337 : : return false;
338 : : }
339 : :
340 : : /* Return a variant of TYPE which has all the type qualifiers of LIKE
341 : : as well as those of TYPE. */
342 : :
343 : : static tree
344 : 2569814 : qualify_type (tree type, tree like)
345 : : {
346 : 2569814 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
347 : 2569814 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
348 : 2569814 : addr_space_t as_common;
349 : :
350 : : /* If the two named address spaces are different, determine the common
351 : : superset address space. If there isn't one, raise an error. */
352 : 2569814 : if (!addr_space_superset (as_type, as_like, &as_common))
353 : : {
354 : 0 : as_common = as_type;
355 : 0 : error ("%qT and %qT are in disjoint named address spaces",
356 : : type, like);
357 : : }
358 : :
359 : 5139628 : return c_build_qualified_type (type,
360 : 2569814 : TYPE_QUALS_NO_ADDR_SPACE (type)
361 : 2569814 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
362 : 2569814 : | ENCODE_QUAL_ADDR_SPACE (as_common));
363 : : }
364 : :
365 : :
366 : : /* Check consistency of type TYPE. For derived types, we test that
367 : : C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
368 : : the requirements of the base type. We also check that arrays with a
369 : : non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
370 : : inconsistency is detected false is returned and true otherwise. */
371 : :
372 : : static bool
373 : 136959466 : c_verify_type (tree type)
374 : : {
375 : 136959466 : switch (TREE_CODE (type))
376 : : {
377 : 64389448 : case POINTER_TYPE:
378 : 64389448 : case FUNCTION_TYPE:
379 : : /* Pointer and funcions can not have variable size. */
380 : 64389448 : if (C_TYPE_VARIABLE_SIZE (type))
381 : : return false;
382 : : /* Pointer and funcions are variably modified if and only if the
383 : : return / target type is variably modified. */
384 : 64389448 : if (C_TYPE_VARIABLY_MODIFIED (type)
385 : 64389448 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type)))
386 : 0 : return false;
387 : : break;
388 : 897809 : case ARRAY_TYPE:
389 : : /* An array has variable size if and only if it has a non-constant
390 : : dimensions or its element type has variable size. */
391 : 897809 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
392 : 897809 : || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
393 : 872539 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
394 : : != INTEGER_CST))
395 : 897809 : != C_TYPE_VARIABLE_SIZE (type))
396 : : return false;
397 : : /* If the element type or the array has variable size, then the
398 : : array has variable size and is variably modified. */
399 : 897809 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
400 : 897809 : || C_TYPE_VARIABLE_SIZE (type))
401 : : {
402 : 28560 : if (!C_TYPE_VARIABLE_SIZE (type))
403 : : return false;
404 : 28560 : if (!C_TYPE_VARIABLY_MODIFIED (type))
405 : : return false;
406 : : }
407 : : /* If the element type is variably modified, then also the array. */
408 : 897809 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))
409 : 897809 : && !C_TYPE_VARIABLY_MODIFIED (type))
410 : 0 : return false;
411 : : break;
412 : : default:
413 : : break;
414 : : }
415 : : return true;
416 : : }
417 : :
418 : : /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
419 : : from a base type to a newly built derived or qualified type. */
420 : :
421 : : static tree
422 : 130074482 : c_set_type_bits (tree new_type, tree old_type)
423 : : {
424 : 130074482 : gcc_checking_assert (c_verify_type (old_type));
425 : :
426 : 130074482 : if (C_TYPE_VARIABLY_MODIFIED (old_type))
427 : 28163 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
428 : :
429 : 130074482 : if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
430 : : {
431 : 16775 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
432 : 16775 : C_TYPE_VARIABLE_SIZE (new_type) = true;
433 : : }
434 : 130074482 : return new_type;
435 : : }
436 : :
437 : : /* Build a pointer type using the default pointer mode. */
438 : :
439 : : tree
440 : 69891318 : c_build_pointer_type (tree to_type)
441 : : {
442 : 69891318 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
443 : 69891318 : : TYPE_ADDR_SPACE (to_type);
444 : 69891318 : machine_mode pointer_mode;
445 : :
446 : 69891318 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
447 : 69891318 : pointer_mode = targetm.addr_space.pointer_mode (as);
448 : : else
449 : : pointer_mode = c_default_pointer_mode;
450 : :
451 : 69891318 : return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
452 : : }
453 : :
454 : : /* Build a pointer type using the given mode. */
455 : :
456 : : tree
457 : 69900107 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
458 : : {
459 : 69900107 : tree ret = build_pointer_type_for_mode (type, mode, m);
460 : 69900107 : return c_set_type_bits (ret, type);
461 : : }
462 : :
463 : : /* Build a function type. */
464 : :
465 : : tree
466 : 52100615 : c_build_function_type (tree type, tree args, bool no)
467 : : {
468 : 52100615 : tree ret = build_function_type (type, args, no);
469 : 52100615 : return c_set_type_bits (ret, type);
470 : : }
471 : :
472 : : /* Build an array type. This sets typeless storage as required
473 : : by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
474 : : based on the element type and domain. */
475 : :
476 : : tree
477 : 1140489 : c_build_array_type (tree type, tree domain)
478 : : {
479 : 1140489 : int type_quals = TYPE_QUALS (type);
480 : :
481 : : /* Identify typeless storage as introduced in C2Y
482 : : and supported also in earlier language modes. */
483 : 1706367 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
484 : 1140489 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
485 : :
486 : 1140489 : tree ret = build_array_type (type, domain, typeless);
487 : :
488 : 1056249 : if (domain && TYPE_MAX_VALUE (domain)
489 : 2109102 : && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
490 : : {
491 : 24148 : C_TYPE_VARIABLE_SIZE (ret) = 1;
492 : 24148 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
493 : : }
494 : :
495 : 1140489 : return c_set_type_bits (ret, type);
496 : : }
497 : :
498 : :
499 : : /* Build an array type of unspecified size. */
500 : : tree
501 : 142 : c_build_array_type_unspecified (tree type)
502 : : {
503 : 142 : tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
504 : : integer_zero_node, size_zero_node);
505 : 142 : return c_build_array_type (type, build_index_type (upper));
506 : : }
507 : :
508 : :
509 : : tree
510 : 6933271 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
511 : : {
512 : 6933271 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
513 : 6933271 : return c_set_type_bits (ret, type);
514 : : }
515 : :
516 : : tree
517 : 6933072 : c_build_type_attribute_variant (tree type, tree attrs)
518 : : {
519 : 6933072 : return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
520 : : }
521 : :
522 : : /* Reconstruct a complex derived type. This is used to re-construct types
523 : : with the vector attribute. It is called via a langhook. */
524 : :
525 : : tree
526 : 483983 : c_reconstruct_complex_type (tree type, tree bottom)
527 : : {
528 : 483983 : tree inner, outer;
529 : :
530 : 483983 : if (TREE_CODE (type) == POINTER_TYPE)
531 : : {
532 : 16 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
533 : 16 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
534 : 16 : TYPE_REF_CAN_ALIAS_ALL (type));
535 : : }
536 : : else if (TREE_CODE (type) == ARRAY_TYPE)
537 : : {
538 : 13 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
539 : 13 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
540 : :
541 : 13 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
542 : : == C_TYPE_VARIABLE_SIZE (outer));
543 : 13 : gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (outer)
544 : : == C_TYPE_VARIABLY_MODIFIED (type));
545 : : }
546 : : else if (TREE_CODE (type) == FUNCTION_TYPE)
547 : : {
548 : 170 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
549 : 170 : outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
550 : 170 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
551 : : }
552 : : else if (TREE_CODE (type) == REFERENCE_TYPE
553 : : || TREE_CODE (type) == OFFSET_TYPE)
554 : 0 : gcc_unreachable ();
555 : : else
556 : : return bottom;
557 : :
558 : 199 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
559 : 398 : TYPE_QUALS (type));
560 : : }
561 : :
562 : : /* If NTYPE is a type of a non-variadic function with a prototype
563 : : and OTYPE is a type of a function without a prototype and ATTRS
564 : : contains attribute format, diagnosess and removes it from ATTRS.
565 : : Returns the result of build_type_attribute_variant of NTYPE and
566 : : the (possibly) modified ATTRS. */
567 : :
568 : : static tree
569 : 9695 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
570 : : {
571 : 9695 : if (!prototype_p (otype)
572 : 9683 : && prototype_p (ntype)
573 : 19348 : && lookup_attribute ("format", attrs))
574 : : {
575 : 14 : warning_at (input_location, OPT_Wattributes,
576 : : "%qs attribute can only be applied to variadic functions",
577 : : "format");
578 : 14 : attrs = remove_attribute ("format", attrs);
579 : : }
580 : 9695 : return c_build_type_attribute_variant (ntype, attrs);
581 : :
582 : : }
583 : :
584 : : /* Given a type which could be a typedef name, make sure to return the
585 : : original type. See set_underlying_type. */
586 : : static const_tree
587 : 60294566 : c_type_original (const_tree t)
588 : : {
589 : : /* It may even be a typedef of a typedef...
590 : : In the case of compiler-created builtin structs the TYPE_DECL
591 : : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
592 : 60294566 : while (TYPE_NAME (t)
593 : 31427026 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
594 : 60298446 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
595 : 1939 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
596 : 60294566 : return t;
597 : : }
598 : :
599 : : /* Return the tag for a tagged type. */
600 : : tree
601 : 60294566 : c_type_tag (const_tree t)
602 : : {
603 : 60294566 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
604 : 60294566 : const_tree orig = c_type_original (t);
605 : 60294566 : tree name = TYPE_NAME (orig);
606 : 60294566 : if (!name)
607 : : return NULL_TREE;
608 : 31425087 : if (TREE_CODE (name) == TYPE_DECL)
609 : : {
610 : : /* A TYPE_DECL added by add_decl_expr. */
611 : 2 : gcc_checking_assert (!DECL_NAME (name));
612 : : return NULL_TREE;
613 : : }
614 : 31425085 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
615 : : return name;
616 : : }
617 : :
618 : : /* Remove qualifiers but not atomic. For arrays remove qualifiers
619 : : on the element type but also do not remove atomic. */
620 : : static tree
621 : 61501871 : remove_qualifiers (tree t)
622 : : {
623 : 61501871 : if (!t || t == error_mark_node)
624 : : return t;
625 : 61501846 : return TYPE_ATOMIC (strip_array_types (t))
626 : 61501846 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
627 : 61486301 : : TYPE_MAIN_VARIANT (t);
628 : : }
629 : :
630 : :
631 : : /* Return the composite type of two compatible types.
632 : :
633 : : We assume that comptypes has already been done and returned
634 : : nonzero; if that isn't so, this may crash. In particular, we
635 : : assume that qualifiers match. */
636 : :
637 : : struct composite_cache {
638 : : tree t1;
639 : : tree t2;
640 : : tree composite;
641 : : struct composite_cache* next;
642 : : };
643 : :
644 : : tree
645 : 12833071 : composite_type_internal (tree t1, tree t2, tree cond,
646 : : struct composite_cache* cache)
647 : : {
648 : 12833071 : enum tree_code code1;
649 : 12833071 : enum tree_code code2;
650 : 12833071 : tree attributes;
651 : :
652 : : /* Save time if the two types are the same. */
653 : :
654 : 12833071 : if (t1 == t2) return t1;
655 : :
656 : : /* If one type is nonsense, use the other. */
657 : 2596955 : if (t1 == error_mark_node)
658 : : return t2;
659 : 2596951 : if (t2 == error_mark_node)
660 : : return t1;
661 : :
662 : 2596949 : code1 = TREE_CODE (t1);
663 : 2596949 : code2 = TREE_CODE (t2);
664 : :
665 : : /* Merge the attributes. */
666 : 2596949 : attributes = targetm.merge_type_attributes (t1, t2);
667 : :
668 : : /* If one is an enumerated type and the other is the compatible
669 : : integer type, the composite type might be either of the two
670 : : (DR#013 question 3). For consistency, use the enumerated type as
671 : : the composite type. */
672 : :
673 : 2596949 : if (code1 == ENUMERAL_TYPE
674 : 196 : && (code2 == INTEGER_TYPE
675 : 196 : || code2 == BOOLEAN_TYPE))
676 : : return t1;
677 : 2596753 : if (code2 == ENUMERAL_TYPE
678 : 69 : && (code1 == INTEGER_TYPE
679 : 69 : || code1 == BOOLEAN_TYPE))
680 : : return t2;
681 : :
682 : 2596684 : gcc_assert (code1 == code2);
683 : :
684 : 2596684 : switch (code1)
685 : : {
686 : 6341 : case POINTER_TYPE:
687 : : /* For two pointers, do this recursively on the target type. */
688 : 6341 : {
689 : 6341 : tree pointed_to_1 = TREE_TYPE (t1);
690 : 6341 : tree pointed_to_2 = TREE_TYPE (t2);
691 : 6341 : tree target = composite_type_internal (pointed_to_1, pointed_to_2,
692 : : cond, cache);
693 : 6341 : t1 = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
694 : 6341 : t1 = c_build_type_attribute_variant (t1, attributes);
695 : 6341 : return qualify_type (t1, t2);
696 : : }
697 : :
698 : 10943 : case ARRAY_TYPE:
699 : 10943 : {
700 : 10943 : tree d1 = TYPE_DOMAIN (t1);
701 : 10943 : tree d2 = TYPE_DOMAIN (t2);
702 : :
703 : : /* We should not have any type quals on arrays at all. */
704 : 10943 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
705 : : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
706 : :
707 : 10943 : bool t1_complete = COMPLETE_TYPE_P (t1);
708 : 10943 : bool t2_complete = COMPLETE_TYPE_P (t2);
709 : :
710 : 10943 : bool d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
711 : 10943 : bool d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
712 : :
713 : 10943 : bool d1_variable, d2_variable;
714 : :
715 : 21886 : d1_variable = (!d1_zero
716 : 10943 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
717 : 10355 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
718 : 21886 : d2_variable = (!d2_zero
719 : 10943 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
720 : 10613 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
721 : :
722 : 10943 : bool use1 = d1 && (d2_variable || d2_zero || !d1_variable);
723 : 10943 : bool use2 = d2 && (d1_variable || d1_zero || !d2_variable);
724 : :
725 : : /* If the first is an unspecified size pick the other one. */
726 : 8961 : if (d2_variable && c_type_unspecified_p (t1))
727 : : {
728 : 28 : gcc_assert (use1 && use2);
729 : : use1 = false;
730 : : }
731 : :
732 : : /* If both are VLAs but not unspecified and we are in the
733 : : conditional operator, we create a conditional to select
734 : : the size of the active branch. */
735 : 202 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
736 : 11142 : && d2_variable && !c_type_unspecified_p (t2);
737 : :
738 : 10943 : tree td;
739 : 10943 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
740 : : cond, cache);
741 : :
742 : 10943 : if (!use0)
743 : : {
744 : : /* Save space: see if the result is identical to one of the args. */
745 : 10850 : if (elt == TREE_TYPE (t1) && use1)
746 : 10069 : return c_build_type_attribute_variant (t1, attributes);
747 : 781 : if (elt == TREE_TYPE (t2) && use2)
748 : 663 : return c_build_type_attribute_variant (t2, attributes);
749 : :
750 : 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
751 : 10 : return c_build_type_attribute_variant (t1, attributes);
752 : 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
753 : 2 : return c_build_type_attribute_variant (t2, attributes);
754 : :
755 : 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
756 : : }
757 : : else
758 : : {
759 : : /* Not used in C. */
760 : 93 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
761 : 93 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
762 : :
763 : 93 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
764 : 93 : cond, TYPE_MAX_VALUE (d1),
765 : 93 : TYPE_MAX_VALUE (d2));
766 : :
767 : 93 : td = build_index_type (d);
768 : : }
769 : :
770 : : /* Merge the element types, and have a size if either arg has
771 : : one. We may have qualifiers on the element types. To set
772 : : up TYPE_MAIN_VARIANT correctly, we need to form the
773 : : composite of the unqualified types and add the qualifiers
774 : : back at the end. */
775 : 199 : int quals = TYPE_QUALS (strip_array_types (elt));
776 : 199 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
777 : :
778 : 199 : t1 = c_build_array_type (unqual_elt, td);
779 : :
780 : : /* Check that a type which has a varying outermost dimension
781 : : got marked has having a variable size. */
782 : 199 : bool varsize = (d1_variable && d2_variable)
783 : 97 : || (d1_variable && !t2_complete)
784 : 285 : || (d2_variable && !t1_complete);
785 : 133 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
786 : :
787 : : /* Ensure a composite type involving a zero-length array type
788 : : is a zero-length type not an incomplete type. */
789 : 199 : if (d1_zero && d2_zero
790 : 16 : && (t1_complete || t2_complete)
791 : 200 : && !COMPLETE_TYPE_P (t1))
792 : : {
793 : 1 : TYPE_SIZE (t1) = bitsize_zero_node;
794 : 1 : TYPE_SIZE_UNIT (t1) = size_zero_node;
795 : : }
796 : 199 : t1 = c_build_qualified_type (t1, quals);
797 : 199 : return c_build_type_attribute_variant (t1, attributes);
798 : : }
799 : :
800 : 162 : case RECORD_TYPE:
801 : 162 : case UNION_TYPE:
802 : 162 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
803 : : {
804 : : /* Go to the original type to get the right tag. */
805 : 101 : tree tag = c_type_tag (t1);
806 : :
807 : 101 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
808 : 101 : gcc_checking_assert (!tag || comptypes (t1, t2));
809 : :
810 : : /* If a composite type for these two types is already under
811 : : construction, return it. */
812 : :
813 : 246 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
814 : 152 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
815 : 7 : return c->composite;
816 : :
817 : : /* Otherwise, create a new type node and link it into the cache. */
818 : :
819 : 94 : tree n = make_node (code1);
820 : 94 : SET_TYPE_STRUCTURAL_EQUALITY (n);
821 : 94 : TYPE_NAME (n) = tag;
822 : :
823 : 94 : struct composite_cache cache2 = { t1, t2, n, cache };
824 : 94 : cache = &cache2;
825 : :
826 : 94 : tree f1 = TYPE_FIELDS (t1);
827 : 94 : tree f2 = TYPE_FIELDS (t2);
828 : 94 : tree fields = NULL_TREE;
829 : :
830 : 214 : for (tree a = f1, b = f2; a && b;
831 : 120 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
832 : : {
833 : 120 : tree ta = TREE_TYPE (a);
834 : 120 : tree tb = TREE_TYPE (b);
835 : :
836 : 120 : if (DECL_C_BIT_FIELD (a))
837 : : {
838 : 19 : ta = DECL_BIT_FIELD_TYPE (a);
839 : 19 : tb = DECL_BIT_FIELD_TYPE (b);
840 : : }
841 : :
842 : 120 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
843 : 120 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
844 : :
845 : 120 : tree t = composite_type_internal (ta, tb, cond, cache);
846 : 120 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
847 : :
848 : 120 : DECL_PACKED (f) = DECL_PACKED (a);
849 : 120 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
850 : 120 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
851 : 120 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
852 : :
853 : 120 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
854 : :
855 : 120 : finish_decl (f, input_location, NULL, NULL, NULL);
856 : :
857 : 120 : if (DECL_C_BIT_FIELD (a))
858 : : {
859 : : /* This will be processed by finish_struct. */
860 : 19 : SET_DECL_C_BIT_FIELD (f);
861 : 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
862 : 19 : tree_to_uhwi (DECL_SIZE (a)));
863 : 19 : DECL_NONADDRESSABLE_P (f) = true;
864 : 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
865 : : }
866 : :
867 : 120 : DECL_CHAIN (f) = fields;
868 : 120 : fields = f;
869 : : }
870 : :
871 : 94 : fields = nreverse (fields);
872 : :
873 : : /* Setup the struct/union type. Because we inherit all variably
874 : : modified components, we can ignore the size expression. */
875 : 94 : tree expr = NULL_TREE;
876 : :
877 : : /* Set TYPE_STUB_DECL for debugging symbols. */
878 : 94 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
879 : : NULL_TREE, n));
880 : :
881 : 94 : n = finish_struct (input_location, n, fields, attributes, NULL,
882 : : &expr);
883 : :
884 : 94 : return qualify_type (n, t1);
885 : : }
886 : : /* FALLTHRU */
887 : 61 : case ENUMERAL_TYPE:
888 : 61 : if (attributes != NULL)
889 : : {
890 : : /* Try harder not to create a new aggregate type. */
891 : 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
892 : : return t1;
893 : 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
894 : : return t2;
895 : : }
896 : 57 : return c_build_type_attribute_variant (t1, attributes);
897 : :
898 : 2538323 : case FUNCTION_TYPE:
899 : : /* Function types: prefer the one that specified arg types.
900 : : If both do, merge the arg types. Also merge the return types. */
901 : 2538323 : {
902 : 2538323 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
903 : : cond, cache);
904 : 2538323 : tree p1 = TYPE_ARG_TYPES (t1);
905 : 2538323 : tree p2 = TYPE_ARG_TYPES (t2);
906 : 2538323 : int len;
907 : 2538323 : tree newargs, n;
908 : 2538323 : int i;
909 : :
910 : : /* Save space: see if the result is identical to one of the args. */
911 : 2538323 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
912 : 9178 : return c_build_functype_attribute_variant (t1, t2, attributes);
913 : 2529145 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
914 : 517 : return c_build_functype_attribute_variant (t2, t1, attributes);
915 : :
916 : : /* Simple way if one arg fails to specify argument types. */
917 : 2528628 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
918 : : {
919 : 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
920 : 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
921 : 9 : t1 = c_build_type_attribute_variant (t1, attributes);
922 : 9 : return qualify_type (t1, t2);
923 : : }
924 : 2528619 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
925 : : {
926 : 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
927 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
928 : 1 : t1 = c_build_type_attribute_variant (t1, attributes);
929 : 1 : return qualify_type (t1, t2);
930 : : }
931 : :
932 : : /* If both args specify argument types, we must merge the two
933 : : lists, argument by argument. */
934 : :
935 : : for (len = 0, newargs = p1;
936 : 6579425 : newargs && newargs != void_list_node;
937 : 4050807 : len++, newargs = TREE_CHAIN (newargs))
938 : : ;
939 : :
940 : 6579425 : for (i = 0; i < len; i++)
941 : 4050807 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
942 : :
943 : : n = newargs;
944 : :
945 : 6579425 : for (; p1 && p1 != void_list_node;
946 : 4050807 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
947 : : {
948 : 4050807 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
949 : 4050807 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
950 : :
951 : : /* A null type means arg type is not specified.
952 : : Take whatever the other function type has. */
953 : 4050807 : if (TREE_VALUE (p1) == NULL_TREE)
954 : : {
955 : 0 : TREE_VALUE (n) = TREE_VALUE (p2);
956 : 0 : goto parm_done;
957 : : }
958 : 4050807 : if (TREE_VALUE (p2) == NULL_TREE)
959 : : {
960 : 0 : TREE_VALUE (n) = TREE_VALUE (p1);
961 : 0 : goto parm_done;
962 : : }
963 : :
964 : : /* Given wait (union {union wait *u; int *i} *)
965 : : and wait (union wait *),
966 : : prefer union wait * as type of parm. */
967 : 4050807 : if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
968 : 4050807 : && TREE_VALUE (p1) != TREE_VALUE (p2))
969 : : {
970 : 30 : tree memb;
971 : 30 : for (memb = TYPE_FIELDS (TREE_VALUE (p1));
972 : 38 : memb; memb = DECL_CHAIN (memb))
973 : : {
974 : 38 : tree mv3 = TREE_TYPE (memb);
975 : 38 : if (mv3 && mv3 != error_mark_node
976 : 38 : && TREE_CODE (mv3) != ARRAY_TYPE)
977 : 38 : mv3 = TYPE_MAIN_VARIANT (mv3);
978 : 38 : if (comptypes (mv3, mv2))
979 : : {
980 : 60 : TREE_VALUE (n) = composite_type_internal (TREE_TYPE (memb),
981 : 30 : TREE_VALUE (p2),
982 : : cond, cache);
983 : 30 : pedwarn (input_location, OPT_Wpedantic,
984 : : "function types not truly compatible in ISO C");
985 : 30 : goto parm_done;
986 : : }
987 : : }
988 : : }
989 : 4050777 : if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
990 : 4050777 : && TREE_VALUE (p2) != TREE_VALUE (p1))
991 : : {
992 : 30 : tree memb;
993 : 30 : for (memb = TYPE_FIELDS (TREE_VALUE (p2));
994 : 38 : memb; memb = DECL_CHAIN (memb))
995 : : {
996 : 38 : tree mv3 = TREE_TYPE (memb);
997 : 38 : if (mv3 && mv3 != error_mark_node
998 : 38 : && TREE_CODE (mv3) != ARRAY_TYPE)
999 : 38 : mv3 = TYPE_MAIN_VARIANT (mv3);
1000 : 38 : if (comptypes (mv3, mv1))
1001 : : {
1002 : 30 : TREE_VALUE (n)
1003 : 30 : = composite_type_internal (TREE_TYPE (memb),
1004 : 30 : TREE_VALUE (p1),
1005 : : cond, cache);
1006 : 30 : pedwarn (input_location, OPT_Wpedantic,
1007 : : "function types not truly compatible in ISO C");
1008 : 30 : goto parm_done;
1009 : : }
1010 : : }
1011 : : }
1012 : 4050747 : TREE_VALUE (n) = composite_type_internal (mv1, mv2, cond, cache);
1013 : 4050807 : parm_done: ;
1014 : : }
1015 : :
1016 : 2528618 : t1 = c_build_function_type (valtype, newargs);
1017 : 2528618 : t1 = qualify_type (t1, t2);
1018 : : }
1019 : : /* FALLTHRU */
1020 : :
1021 : 2569533 : default:
1022 : 2569533 : return c_build_type_attribute_variant (t1, attributes);
1023 : : }
1024 : : }
1025 : :
1026 : : tree
1027 : 6226537 : composite_type_cond (tree t1, tree t2, tree cond)
1028 : : {
1029 : 6226537 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1030 : :
1031 : 6226537 : struct composite_cache cache = { };
1032 : 6226537 : tree n = composite_type_internal (t1, t2, cond, &cache);
1033 : :
1034 : 6226537 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1035 : 6226537 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1036 : 6226537 : return n;
1037 : : }
1038 : :
1039 : :
1040 : : tree
1041 : 6222092 : composite_type (tree t1, tree t2)
1042 : : {
1043 : 6222092 : return composite_type_cond (t1, t2, NULL_TREE);
1044 : : }
1045 : :
1046 : : /* Return the type of a conditional expression between pointers to
1047 : : possibly differently qualified versions of compatible types.
1048 : :
1049 : : We assume that comp_target_types has already been done and returned
1050 : : true; if that isn't so, this may crash. */
1051 : :
1052 : : static tree
1053 : 113742 : common_pointer_type (tree t1, tree t2, tree cond)
1054 : : {
1055 : 113742 : tree attributes;
1056 : 113742 : unsigned target_quals;
1057 : 113742 : addr_space_t as1, as2, as_common;
1058 : 113742 : int quals1, quals2;
1059 : :
1060 : : /* Save time if the two types are the same. */
1061 : :
1062 : 113742 : if (t1 == t2) return t1;
1063 : :
1064 : : /* If one type is nonsense, use the other. */
1065 : 4445 : if (t1 == error_mark_node)
1066 : : return t2;
1067 : 4445 : if (t2 == error_mark_node)
1068 : : return t1;
1069 : :
1070 : 4445 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1071 : : && TREE_CODE (t2) == POINTER_TYPE);
1072 : :
1073 : : /* Merge the attributes. */
1074 : 4445 : attributes = targetm.merge_type_attributes (t1, t2);
1075 : :
1076 : : /* Find the composite type of the target types, and combine the
1077 : : qualifiers of the two types' targets. */
1078 : 4445 : tree pointed_to_1 = TREE_TYPE (t1);
1079 : 4445 : tree pointed_to_2 = TREE_TYPE (t2);
1080 : 4445 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1081 : 4445 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1082 : :
1083 : : /* Strip array types to get correct qualifier for pointers to arrays */
1084 : 4445 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1085 : 4445 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1086 : :
1087 : : /* For function types do not merge const qualifiers, but drop them
1088 : : if used inconsistently. The middle-end uses these to mark const
1089 : : and noreturn functions. */
1090 : 4445 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1091 : 2129 : target_quals = (quals1 & quals2);
1092 : : else
1093 : 2316 : target_quals = (quals1 | quals2);
1094 : :
1095 : : /* If the two named address spaces are different, determine the common
1096 : : superset address space. This is guaranteed to exist due to the
1097 : : assumption that comp_target_type returned true. */
1098 : 4445 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1099 : 4445 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1100 : 4445 : if (!addr_space_superset (as1, as2, &as_common))
1101 : 0 : gcc_unreachable ();
1102 : :
1103 : 4445 : target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1104 : :
1105 : 4445 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1106 : 4445 : return c_build_type_attribute_variant (t1, attributes);
1107 : : }
1108 : :
1109 : : /* Return the common type for two arithmetic types under the usual
1110 : : arithmetic conversions. The default conversions have already been
1111 : : applied, and enumerated types converted to their compatible integer
1112 : : types. The resulting type is unqualified and has no attributes.
1113 : :
1114 : : This is the type for the result of most arithmetic operations
1115 : : if the operands have the given two types. */
1116 : :
1117 : : static tree
1118 : 13632605 : c_common_type (tree t1, tree t2)
1119 : : {
1120 : 13632605 : enum tree_code code1;
1121 : 13632605 : enum tree_code code2;
1122 : :
1123 : : /* If one type is nonsense, use the other. */
1124 : 13632605 : if (t1 == error_mark_node)
1125 : : return t2;
1126 : 13632605 : if (t2 == error_mark_node)
1127 : : return t1;
1128 : :
1129 : 13632605 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1130 : 37761 : t1 = TYPE_MAIN_VARIANT (t1);
1131 : :
1132 : 13632605 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1133 : 47723 : t2 = TYPE_MAIN_VARIANT (t2);
1134 : :
1135 : 13632605 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1136 : : {
1137 : 183823 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1138 : 183823 : t1 = c_build_type_attribute_variant (t1, attrs);
1139 : : }
1140 : :
1141 : 13632605 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1142 : : {
1143 : 183302 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1144 : 183302 : t2 = c_build_type_attribute_variant (t2, attrs);
1145 : : }
1146 : :
1147 : : /* Save time if the two types are the same. */
1148 : :
1149 : 13632605 : if (t1 == t2) return t1;
1150 : :
1151 : 2286876 : code1 = TREE_CODE (t1);
1152 : 2286876 : code2 = TREE_CODE (t2);
1153 : :
1154 : 2286876 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1155 : : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1156 : : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1157 : 2286876 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1158 : : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1159 : : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1160 : :
1161 : : /* When one operand is a decimal float type, the other operand cannot be
1162 : : a generic float type or a complex type. We also disallow vector types
1163 : : here. */
1164 : 2286876 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1165 : 2289299 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1166 : : {
1167 : 1080 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1168 : : {
1169 : 3 : error ("cannot mix operands of decimal floating and vector types");
1170 : 3 : return error_mark_node;
1171 : : }
1172 : 1077 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1173 : : {
1174 : 5 : error ("cannot mix operands of decimal floating and complex types");
1175 : 5 : return error_mark_node;
1176 : : }
1177 : 1072 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1178 : : {
1179 : 16 : error ("cannot mix operands of decimal floating "
1180 : : "and other floating types");
1181 : 16 : return error_mark_node;
1182 : : }
1183 : : }
1184 : :
1185 : : /* If one type is a vector type, return that type. (How the usual
1186 : : arithmetic conversions apply to the vector types extension is not
1187 : : precisely specified.) */
1188 : 2286852 : if (code1 == VECTOR_TYPE)
1189 : : return t1;
1190 : :
1191 : 2286575 : if (code2 == VECTOR_TYPE)
1192 : : return t2;
1193 : :
1194 : : /* If one type is complex, form the common type of the non-complex
1195 : : components, then make that complex. Use T1 or T2 if it is the
1196 : : required type. */
1197 : 2286572 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1198 : : {
1199 : 89315 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1200 : 89315 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1201 : 89315 : tree subtype = c_common_type (subtype1, subtype2);
1202 : :
1203 : 89315 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1204 : : return t1;
1205 : 59200 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1206 : : return t2;
1207 : 3241 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1208 : : {
1209 : 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1210 : 4 : TYPE_PRECISION (subtype));
1211 : 6 : return code1 == COMPLEX_TYPE ? t1 : t2;
1212 : : }
1213 : : else
1214 : 3237 : return build_complex_type (subtype);
1215 : : }
1216 : :
1217 : : /* If only one is real, use it as the result. */
1218 : :
1219 : 2197257 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1220 : : return t1;
1221 : :
1222 : 2072778 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1223 : : return t2;
1224 : :
1225 : : /* If both are real and either are decimal floating point types, use
1226 : : the decimal floating point type with the greater precision. */
1227 : :
1228 : 2042476 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1229 : : {
1230 : 102626 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1231 : 102626 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1232 : : return dfloat128_type_node;
1233 : : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1234 : : implementation the same mode. */
1235 : 102077 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1236 : 102077 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1237 : : return dfloat64x_type_node;
1238 : 102073 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1239 : 102073 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1240 : : return dfloat64_type_node;
1241 : 101628 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1242 : 101628 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1243 : : return dfloat32_type_node;
1244 : : }
1245 : :
1246 : : /* Deal with fixed-point types. */
1247 : 2041133 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1248 : : {
1249 : 0 : unsigned int unsignedp = 0, satp = 0;
1250 : 0 : scalar_mode m1, m2;
1251 : 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1252 : :
1253 : 0 : m1 = SCALAR_TYPE_MODE (t1);
1254 : 0 : m2 = SCALAR_TYPE_MODE (t2);
1255 : :
1256 : : /* If one input type is saturating, the result type is saturating. */
1257 : 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1258 : : satp = 1;
1259 : :
1260 : : /* If both fixed-point types are unsigned, the result type is unsigned.
1261 : : When mixing fixed-point and integer types, follow the sign of the
1262 : : fixed-point type.
1263 : : Otherwise, the result type is signed. */
1264 : 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1265 : 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1266 : 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1267 : 0 : && TYPE_UNSIGNED (t1))
1268 : 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1269 : 0 : && TYPE_UNSIGNED (t2)))
1270 : : unsignedp = 1;
1271 : :
1272 : : /* The result type is signed. */
1273 : 0 : if (unsignedp == 0)
1274 : : {
1275 : : /* If the input type is unsigned, we need to convert to the
1276 : : signed type. */
1277 : 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1278 : : {
1279 : 0 : enum mode_class mclass = (enum mode_class) 0;
1280 : 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1281 : : mclass = MODE_FRACT;
1282 : 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1283 : : mclass = MODE_ACCUM;
1284 : : else
1285 : 0 : gcc_unreachable ();
1286 : 0 : m1 = as_a <scalar_mode>
1287 : 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1288 : : }
1289 : 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1290 : : {
1291 : 0 : enum mode_class mclass = (enum mode_class) 0;
1292 : 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1293 : : mclass = MODE_FRACT;
1294 : 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1295 : : mclass = MODE_ACCUM;
1296 : : else
1297 : 0 : gcc_unreachable ();
1298 : 0 : m2 = as_a <scalar_mode>
1299 : 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1300 : : }
1301 : : }
1302 : :
1303 : 0 : if (code1 == FIXED_POINT_TYPE)
1304 : : {
1305 : 0 : fbit1 = GET_MODE_FBIT (m1);
1306 : 0 : ibit1 = GET_MODE_IBIT (m1);
1307 : : }
1308 : : else
1309 : : {
1310 : 0 : fbit1 = 0;
1311 : : /* Signed integers need to subtract one sign bit. */
1312 : 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1313 : : }
1314 : :
1315 : 0 : if (code2 == FIXED_POINT_TYPE)
1316 : : {
1317 : 0 : fbit2 = GET_MODE_FBIT (m2);
1318 : 0 : ibit2 = GET_MODE_IBIT (m2);
1319 : : }
1320 : : else
1321 : : {
1322 : 0 : fbit2 = 0;
1323 : : /* Signed integers need to subtract one sign bit. */
1324 : 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1325 : : }
1326 : :
1327 : 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1328 : 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1329 : 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1330 : : satp);
1331 : : }
1332 : :
1333 : : /* Both real or both integers; use the one with greater precision. */
1334 : :
1335 : 2041133 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1336 : : return t1;
1337 : 1077037 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1338 : : return t2;
1339 : :
1340 : : /* Same precision. Prefer long longs to longs to ints when the
1341 : : same precision, following the C99 rules on integer type rank
1342 : : (which are equivalent to the C90 rules for C90 types). */
1343 : :
1344 : 724393 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1345 : 724393 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1346 : : return long_long_unsigned_type_node;
1347 : :
1348 : 637355 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1349 : 637355 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1350 : : {
1351 : 14729 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1352 : 361 : return long_long_unsigned_type_node;
1353 : : else
1354 : : return long_long_integer_type_node;
1355 : : }
1356 : :
1357 : 622626 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1358 : 622626 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1359 : : return long_unsigned_type_node;
1360 : :
1361 : 523597 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1362 : 523597 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1363 : : {
1364 : : /* But preserve unsignedness from the other type,
1365 : : since long cannot hold all the values of an unsigned int. */
1366 : 26199 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1367 : 131 : return long_unsigned_type_node;
1368 : : else
1369 : : return long_integer_type_node;
1370 : : }
1371 : :
1372 : : /* For floating types of the same TYPE_PRECISION (which we here
1373 : : assume means either the same set of values, or sets of values
1374 : : neither a subset of the other, with behavior being undefined in
1375 : : the latter case), follow the rules from TS 18661-3: prefer
1376 : : interchange types _FloatN, then standard types long double,
1377 : : double, float, then extended types _FloatNx. For extended types,
1378 : : check them starting with _Float128x as that seems most consistent
1379 : : in spirit with preferring long double to double; for interchange
1380 : : types, also check in that order for consistency although it's not
1381 : : possible for more than one of them to have the same
1382 : : precision. */
1383 : 497398 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1384 : 497398 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1385 : :
1386 : 2486424 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1387 : 1989189 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1388 : : return FLOATN_TYPE_NODE (i);
1389 : :
1390 : : /* Likewise, prefer long double to double even if same size. */
1391 : 497235 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1392 : : return long_double_type_node;
1393 : :
1394 : : /* Likewise, prefer double to float even if same size.
1395 : : We got a couple of embedded targets with 32 bit doubles, and the
1396 : : pdp11 might have 64 bit floats. */
1397 : 496993 : if (mv1 == double_type_node || mv2 == double_type_node)
1398 : : return double_type_node;
1399 : :
1400 : 496421 : if (mv1 == float_type_node || mv2 == float_type_node)
1401 : : return float_type_node;
1402 : :
1403 : 1977008 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1404 : 1482756 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1405 : : return FLOATNX_TYPE_NODE (i);
1406 : :
1407 : 494252 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1408 : : {
1409 : : /* Prefer any other integral types over bit-precise integer types. */
1410 : 10 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1411 : 6 : return code1 == BITINT_TYPE ? t2 : t1;
1412 : : /* If BITINT_TYPE is unsigned and the other type is signed
1413 : : non-BITINT_TYPE with the same precision, the latter has higher rank.
1414 : : In that case:
1415 : : Otherwise, both operands are converted to the unsigned integer type
1416 : : corresponding to the type of the operand with signed integer type. */
1417 : 8 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1418 : 5 : return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1419 : : }
1420 : :
1421 : : /* Otherwise prefer the unsigned one. */
1422 : :
1423 : 494244 : if (TYPE_UNSIGNED (t1))
1424 : : return t1;
1425 : : else
1426 : : return t2;
1427 : : }
1428 : :
1429 : : /* Wrapper around c_common_type that is used by c-common.cc and other
1430 : : front end optimizations that remove promotions. ENUMERAL_TYPEs
1431 : : are allowed here and are converted to their compatible integer types.
1432 : : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1433 : : preferably a non-Boolean type as the common type. */
1434 : : tree
1435 : 99973 : common_type (tree t1, tree t2)
1436 : : {
1437 : 99973 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1438 : 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1439 : 99973 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1440 : 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1441 : :
1442 : : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1443 : 99973 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1444 : 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1445 : 584 : return boolean_type_node;
1446 : :
1447 : : /* If either type is BOOLEAN_TYPE, then return the other. */
1448 : 99389 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1449 : : return t2;
1450 : 99389 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1451 : : return t1;
1452 : :
1453 : 99389 : return c_common_type (t1, t2);
1454 : : }
1455 : :
1456 : :
1457 : :
1458 : : /* Helper function for comptypes. For two compatible types, return 1
1459 : : if they pass consistency checks. In particular we test that
1460 : : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1461 : :
1462 : : static bool
1463 : 43549634 : comptypes_verify (tree type1, tree type2)
1464 : : {
1465 : 43549634 : if (type1 == type2 || !type1 || !type2
1466 : 3442493 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1467 : : return true;
1468 : :
1469 : 3442492 : gcc_checking_assert (c_verify_type (type1));
1470 : 3442492 : gcc_checking_assert (c_verify_type (type2));
1471 : :
1472 : 3442492 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1473 : 381521 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1474 : 3822352 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1475 : : {
1476 : : /* FIXME: check other types. */
1477 : 379528 : if (RECORD_OR_UNION_TYPE_P (type1)
1478 : 379528 : || TREE_CODE (type1) == ENUMERAL_TYPE
1479 : 379528 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1480 : 0 : return false;
1481 : : }
1482 : : return true;
1483 : : }
1484 : :
1485 : : struct comptypes_data {
1486 : :
1487 : : /* output */
1488 : : bool enum_and_int_p;
1489 : : bool different_types_p;
1490 : : bool warning_needed;
1491 : :
1492 : : /* context */
1493 : : bool anon_field;
1494 : : bool pointedto;
1495 : :
1496 : : /* configuration */
1497 : : bool equiv;
1498 : : bool ignore_promoting_args;
1499 : :
1500 : : const struct tagged_tu_seen_cache* cache;
1501 : : };
1502 : :
1503 : :
1504 : : /* Helper function for composite_type. This function ignores when the
1505 : : function type of an old-style declaration is incompatible with a type
1506 : : of a declaration with prototype because some are arguments are not
1507 : : self-promoting. This is ignored only for function types but not
1508 : : ignored in a nested context. */
1509 : :
1510 : : static bool
1511 : 18679611 : comptypes_check_for_composite (tree t1, tree t2)
1512 : : {
1513 : 18679611 : struct comptypes_data data = { };
1514 : 18679611 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1515 : 18679611 : return comptypes_internal (t1, t2, &data);
1516 : : }
1517 : :
1518 : :
1519 : : /* C implementation of compatible_types_for_indirection_note_p. */
1520 : :
1521 : : bool
1522 : 761 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1523 : : {
1524 : 761 : return comptypes (type1, type2) == 1;
1525 : : }
1526 : :
1527 : : /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1528 : : or various other operations. Return 2 if they are compatible
1529 : : but a warning may be needed if you use them together. */
1530 : :
1531 : : int
1532 : 55150162 : comptypes (tree type1, tree type2)
1533 : : {
1534 : 55150162 : struct comptypes_data data = { };
1535 : 55150162 : bool ret = comptypes_internal (type1, type2, &data);
1536 : :
1537 : 55150162 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1538 : :
1539 : 37104573 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1540 : : }
1541 : :
1542 : :
1543 : : /* Like comptypes, but it returns non-zero only for identical
1544 : : types. */
1545 : :
1546 : : bool
1547 : 280 : comptypes_same_p (tree type1, tree type2)
1548 : : {
1549 : 280 : struct comptypes_data data = { };
1550 : 280 : bool ret = comptypes_internal (type1, type2, &data);
1551 : :
1552 : 280 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1553 : :
1554 : 280 : if (data.different_types_p)
1555 : 103 : return false;
1556 : :
1557 : : return ret;
1558 : : }
1559 : :
1560 : :
1561 : : /* Like comptypes, but if it returns non-zero because enum and int are
1562 : : compatible, it sets *ENUM_AND_INT_P to true. */
1563 : :
1564 : : int
1565 : 6644042 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1566 : : {
1567 : 6644042 : struct comptypes_data data = { };
1568 : 6644042 : bool ret = comptypes_internal (type1, type2, &data);
1569 : 6644042 : *enum_and_int_p = data.enum_and_int_p;
1570 : :
1571 : 6644042 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1572 : :
1573 : 6400301 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1574 : : }
1575 : :
1576 : : /* Like comptypes, but if it returns nonzero for different types, it
1577 : : sets *DIFFERENT_TYPES_P to true. */
1578 : :
1579 : : int
1580 : 44520 : comptypes_check_different_types (tree type1, tree type2,
1581 : : bool *different_types_p)
1582 : : {
1583 : 44520 : struct comptypes_data data = { };
1584 : 44520 : bool ret = comptypes_internal (type1, type2, &data);
1585 : 44520 : *different_types_p = data.different_types_p;
1586 : :
1587 : 44520 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1588 : :
1589 : 44520 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1590 : : }
1591 : :
1592 : :
1593 : : /* Like comptypes, but if it returns true for struct and union types
1594 : : considered equivalent for aliasing purposes, i.e. for setting
1595 : : TYPE_CANONICAL after completing a struct or union.
1596 : :
1597 : : This function must return false only for types which are not
1598 : : compatible according to C language semantics (cf. comptypes),
1599 : : otherwise the middle-end would make incorrect aliasing decisions.
1600 : : It may return true for some similar types that are not compatible
1601 : : according to those stricter rules.
1602 : :
1603 : : In particular, we ignore size expression in arrays so that the
1604 : : following structs are in the same equivalence class:
1605 : :
1606 : : struct foo { char (*buf)[]; };
1607 : : struct foo { char (*buf)[3]; };
1608 : : struct foo { char (*buf)[4]; };
1609 : :
1610 : : We also treat unions / structs with members which are pointers to
1611 : : structures or unions with the same tag as equivalent (if they are not
1612 : : incompatible for other reasons). Although incomplete structure
1613 : : or union types are not compatible to any other type, they may become
1614 : : compatible to different types when completed. To avoid having to update
1615 : : TYPE_CANONICAL at this point, we only consider the tag when forming
1616 : : the equivalence classes. For example, the following types with tag
1617 : : 'foo' are all considered equivalent:
1618 : :
1619 : : struct bar;
1620 : : struct foo { struct bar *x };
1621 : : struct foo { struct bar { int a; } *x };
1622 : : struct foo { struct bar { char b; } *x }; */
1623 : :
1624 : : bool
1625 : 14234676 : comptypes_equiv_p (tree type1, tree type2)
1626 : : {
1627 : 14234676 : struct comptypes_data data = { };
1628 : 14234676 : data.equiv = true;
1629 : 14234676 : bool ret = comptypes_internal (type1, type2, &data);
1630 : :
1631 : : /* check that different equivance classes are assigned only
1632 : : to types that are not compatible. */
1633 : 14234676 : gcc_checking_assert (ret || !comptypes (type1, type2));
1634 : :
1635 : 14234676 : return ret;
1636 : : }
1637 : :
1638 : :
1639 : : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1640 : : or various other operations. If they are compatible but a warning may
1641 : : be needed if you use them together, 'warning_needed' in DATA is set.
1642 : : If one type is an enum and the other a compatible integer type, then
1643 : : this sets 'enum_and_int_p' in DATA to true (it is never set to
1644 : : false). If the types are compatible but different enough not to be
1645 : : permitted in C11 typedef redeclarations, then this sets
1646 : : 'different_types_p' in DATA to true; it is never set to
1647 : : false, but may or may not be set if the types are incompatible.
1648 : : If two functions types are not compatible only because one is
1649 : : an old-style definition that does not have self-promoting arguments,
1650 : : then this can be ignored by setting 'ignore_promoting_args_p'.
1651 : : For 'equiv' we can compute equivalency classes (see above).
1652 : : This differs from comptypes, in that we don't free the seen
1653 : : types. */
1654 : :
1655 : : static bool
1656 : 130186140 : comptypes_internal (const_tree type1, const_tree type2,
1657 : : struct comptypes_data *data)
1658 : : {
1659 : 130457463 : const_tree t1 = type1;
1660 : 130457463 : const_tree t2 = type2;
1661 : :
1662 : : /* Suppress errors caused by previously reported errors. */
1663 : :
1664 : 130457463 : if (t1 == t2 || !t1 || !t2
1665 : 44027817 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1666 : : return true;
1667 : :
1668 : : /* Enumerated types are compatible with integer types, but this is
1669 : : not transitive: two enumerated types in the same translation unit
1670 : : are compatible with each other only if they are the same type. */
1671 : :
1672 : 44027814 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1673 : 1012 : && COMPLETE_TYPE_P (t1)
1674 : 44028782 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1675 : : {
1676 : 856 : t1 = ENUM_UNDERLYING_TYPE (t1);
1677 : 856 : if (TREE_CODE (t2) != VOID_TYPE)
1678 : : {
1679 : 852 : data->enum_and_int_p = true;
1680 : 852 : data->different_types_p = true;
1681 : : }
1682 : : }
1683 : 44026958 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1684 : 4684 : && COMPLETE_TYPE_P (t2)
1685 : 44031636 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1686 : : {
1687 : 4566 : t2 = ENUM_UNDERLYING_TYPE (t2);
1688 : 4566 : if (TREE_CODE (t1) != VOID_TYPE)
1689 : : {
1690 : 3667 : data->enum_and_int_p = true;
1691 : 3667 : data->different_types_p = true;
1692 : : }
1693 : : }
1694 : :
1695 : 44027814 : if (t1 == t2)
1696 : : return true;
1697 : :
1698 : : /* Different classes of types can't be compatible. */
1699 : :
1700 : 44026436 : if (TREE_CODE (t1) != TREE_CODE (t2))
1701 : : return false;
1702 : :
1703 : : /* Qualifiers must match. C99 6.7.3p9 */
1704 : :
1705 : 36184182 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1706 : : return false;
1707 : :
1708 : : /* Allow for two different type nodes which have essentially the same
1709 : : definition. Note that we already checked for equality of the type
1710 : : qualifiers (just above). */
1711 : :
1712 : 36145004 : if (TREE_CODE (t1) != ARRAY_TYPE
1713 : 36145004 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1714 : : return true;
1715 : :
1716 : 35813244 : int attrval;
1717 : :
1718 : : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1719 : 35813244 : if (!(attrval = comp_type_attributes (t1, t2)))
1720 : : return false;
1721 : :
1722 : 35812883 : if (2 == attrval)
1723 : 336 : data->warning_needed = true;
1724 : :
1725 : 35812883 : switch (TREE_CODE (t1))
1726 : : {
1727 : 3297850 : case INTEGER_TYPE:
1728 : 3297850 : case FIXED_POINT_TYPE:
1729 : 3297850 : case REAL_TYPE:
1730 : 3297850 : case BITINT_TYPE:
1731 : : /* With these nodes, we can't determine type equivalence by
1732 : : looking at what is stored in the nodes themselves, because
1733 : : two nodes might have different TYPE_MAIN_VARIANTs but still
1734 : : represent the same type. For example, wchar_t and int could
1735 : : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1736 : : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1737 : : and are distinct types. On the other hand, int and the
1738 : : following typedef
1739 : :
1740 : : typedef int INT __attribute((may_alias));
1741 : :
1742 : : have identical properties, different TYPE_MAIN_VARIANTs, but
1743 : : represent the same type. The canonical type system keeps
1744 : : track of equivalence in this case, so we fall back on it. */
1745 : 3297850 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1746 : :
1747 : 271323 : case POINTER_TYPE:
1748 : : /* Do not remove mode information. */
1749 : 271323 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1750 : : return false;
1751 : 271323 : data->pointedto = true;
1752 : 271323 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1753 : :
1754 : 8777012 : case FUNCTION_TYPE:
1755 : 8777012 : return function_types_compatible_p (t1, t2, data);
1756 : :
1757 : 150918 : case ARRAY_TYPE:
1758 : 150918 : {
1759 : 150918 : tree d1 = TYPE_DOMAIN (t1);
1760 : 150918 : tree d2 = TYPE_DOMAIN (t2);
1761 : 150918 : bool d1_variable, d2_variable;
1762 : 150918 : bool d1_zero, d2_zero;
1763 : :
1764 : : /* Target types must match incl. qualifiers. */
1765 : 150918 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1766 : : return false;
1767 : :
1768 : 117364 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1769 : 6190 : data->different_types_p = true;
1770 : : /* Ignore size mismatches when forming equivalence classes. */
1771 : 117364 : if (data->equiv)
1772 : : return true;
1773 : : /* Sizes must match unless one is missing or variable. */
1774 : 49690 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1775 : : return true;
1776 : :
1777 : 33576 : d1_zero = !TYPE_MAX_VALUE (d1);
1778 : 33576 : d2_zero = !TYPE_MAX_VALUE (d2);
1779 : :
1780 : 67152 : d1_variable = (!d1_zero
1781 : 33576 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1782 : 33570 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1783 : 67152 : d2_variable = (!d2_zero
1784 : 33576 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1785 : 33480 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1786 : :
1787 : 33576 : if (d1_variable != d2_variable)
1788 : 764 : data->different_types_p = true;
1789 : 33576 : if (d1_variable || d2_variable)
1790 : : return true;
1791 : 4038 : if (d1_zero && d2_zero)
1792 : : return true;
1793 : 4038 : if (d1_zero || d2_zero
1794 : 3942 : || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1795 : 7980 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1796 : 4038 : return false;
1797 : :
1798 : : return true;
1799 : : }
1800 : :
1801 : 22798688 : case ENUMERAL_TYPE:
1802 : 22798688 : case RECORD_TYPE:
1803 : 22798688 : case UNION_TYPE:
1804 : :
1805 : 22798688 : if (!flag_isoc23)
1806 : : return false;
1807 : :
1808 : 22798642 : return tagged_types_tu_compatible_p (t1, t2, data);
1809 : :
1810 : 516527 : case VECTOR_TYPE:
1811 : 1033054 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1812 : 516527 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1813 : :
1814 : : default:
1815 : : return false;
1816 : : }
1817 : : gcc_unreachable ();
1818 : : }
1819 : :
1820 : : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1821 : : their qualifiers, except for named address spaces. If the pointers point to
1822 : : different named addresses, then we must determine if one address space is a
1823 : : subset of the other. */
1824 : :
1825 : : static bool
1826 : 1734709 : comp_target_types (location_t location, tree ttl, tree ttr)
1827 : : {
1828 : 1734709 : int val;
1829 : 1734709 : int val_ped;
1830 : 1734709 : tree mvl = TREE_TYPE (ttl);
1831 : 1734709 : tree mvr = TREE_TYPE (ttr);
1832 : 1734709 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1833 : 1734709 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1834 : 1734709 : addr_space_t as_common;
1835 : 1734709 : bool enum_and_int_p;
1836 : :
1837 : : /* Fail if pointers point to incompatible address spaces. */
1838 : 1734709 : if (!addr_space_superset (asl, asr, &as_common))
1839 : : return 0;
1840 : :
1841 : : /* For pedantic record result of comptypes on arrays before losing
1842 : : qualifiers on the element type below. */
1843 : 1734709 : val_ped = 1;
1844 : :
1845 : 1734709 : if (TREE_CODE (mvl) == ARRAY_TYPE
1846 : 1990 : && TREE_CODE (mvr) == ARRAY_TYPE)
1847 : 1962 : val_ped = comptypes (mvl, mvr);
1848 : :
1849 : : /* Qualifiers on element types of array types that are
1850 : : pointer targets are also removed. */
1851 : 1734709 : mvl = remove_qualifiers (mvl);
1852 : 1734709 : mvr = remove_qualifiers (mvr);
1853 : :
1854 : 1734709 : enum_and_int_p = false;
1855 : 1734709 : val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1856 : :
1857 : 1734709 : if (val == 1 && val_ped != 1)
1858 : 190 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1859 : : "in ISO C before C23");
1860 : :
1861 : 1734709 : if (val == 2)
1862 : 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1863 : :
1864 : 1734709 : if (val == 1 && enum_and_int_p && warn_cxx_compat)
1865 : 2 : warning_at (location, OPT_Wc___compat,
1866 : : "pointer target types incompatible in C++");
1867 : :
1868 : 1734709 : return val;
1869 : : }
1870 : :
1871 : : /* Subroutines of `comptypes'. */
1872 : :
1873 : : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1874 : : compatible. The two types are not the same (which has been
1875 : : checked earlier in comptypes_internal). */
1876 : :
1877 : : static bool
1878 : 22798642 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1879 : : struct comptypes_data *data)
1880 : : {
1881 : 22798642 : tree s1, s2;
1882 : :
1883 : 22798642 : if (c_type_tag (t1) != c_type_tag (t2))
1884 : : return false;
1885 : :
1886 : : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1887 : : structs with the same tag as equivalent, but only when they are targets
1888 : : of pointers inside other structs. */
1889 : 18911882 : if (data->equiv && data->pointedto)
1890 : : return true;
1891 : :
1892 : : /* Different types without tag are incompatible except as an anonymous
1893 : : field or when forming equivalence classes for TYPE_CANONICAL. */
1894 : 18911848 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1895 : : return false;
1896 : :
1897 : 14016425 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1898 : 14015432 : data->different_types_p = true;
1899 : :
1900 : : /* Incomplete types are incompatible inside a TU. */
1901 : 14016425 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1902 : : return false;
1903 : :
1904 : 14016415 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1905 : 14016415 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1906 : 14016360 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1907 : : return false;
1908 : :
1909 : 14016405 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1910 : 141079 : data->different_types_p = true;
1911 : :
1912 : : /* For types already being looked at in some active
1913 : : invocation of this function, assume compatibility.
1914 : : The cache is built as a linked list on the stack
1915 : : with the head of the list passed downwards. */
1916 : 14016405 : for (const struct tagged_tu_seen_cache *t = data->cache;
1917 : 14025268 : t != NULL; t = t->next)
1918 : 8974 : if (t->t1 == t1 && t->t2 == t2)
1919 : : return true;
1920 : :
1921 : 14016294 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1922 : :
1923 : 14016294 : switch (TREE_CODE (t1))
1924 : : {
1925 : 55 : case ENUMERAL_TYPE:
1926 : 55 : {
1927 : 55 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1928 : : return false;
1929 : :
1930 : : /* Speed up the case where the type values are in the same order. */
1931 : 51 : tree tv1 = TYPE_VALUES (t1);
1932 : 51 : tree tv2 = TYPE_VALUES (t2);
1933 : :
1934 : 51 : if (tv1 == tv2)
1935 : : return true;
1936 : :
1937 : 97 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1938 : : {
1939 : 58 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1940 : : break;
1941 : :
1942 : 51 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1943 : 51 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1944 : : break;
1945 : : }
1946 : :
1947 : 51 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1948 : : return true;
1949 : :
1950 : 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1951 : : return false;
1952 : :
1953 : 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1954 : : return false;
1955 : :
1956 : 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1957 : : {
1958 : 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1959 : :
1960 : 16 : if (s2 == NULL
1961 : 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
1962 : 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
1963 : 8 : return false;
1964 : : }
1965 : :
1966 : : return true;
1967 : : }
1968 : :
1969 : 14016239 : case UNION_TYPE:
1970 : 14016239 : case RECORD_TYPE:
1971 : :
1972 : 14016239 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1973 : : return false;
1974 : :
1975 : 12172444 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1976 : 13682314 : s1 && s2;
1977 : 1509870 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1978 : : {
1979 : 13619273 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
1980 : 13619273 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
1981 : :
1982 : 13619273 : tree ft1 = TREE_TYPE (s1);
1983 : 13619273 : tree ft2 = TREE_TYPE (s2);
1984 : :
1985 : 13619273 : if (DECL_NAME (s1) != DECL_NAME (s2))
1986 : : return false;
1987 : :
1988 : 12041872 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
1989 : : return false;
1990 : :
1991 : 3194069 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
1992 : : return false;
1993 : :
1994 : 3194049 : if (DECL_C_BIT_FIELD (s1))
1995 : : {
1996 : 496 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
1997 : : return false;
1998 : :
1999 : 342 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2000 : 342 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2001 : : }
2002 : :
2003 : 3193895 : if (!ft1 || !ft2)
2004 : : return false;
2005 : :
2006 : 3193895 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2007 : : return false;
2008 : :
2009 : 3193894 : data->anon_field = !DECL_NAME (s1);
2010 : 3193894 : data->pointedto = false;
2011 : :
2012 : 3193894 : const struct tagged_tu_seen_cache *cache = data->cache;
2013 : 3193894 : data->cache = &entry;
2014 : 3193894 : bool ret = comptypes_internal (ft1, ft2, data);
2015 : 3193894 : data->cache = cache;
2016 : 3193894 : if (!ret)
2017 : : return false;
2018 : :
2019 : 1577494 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2020 : 1577494 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2021 : :
2022 : 1577494 : if (data->equiv
2023 : 984311 : && st1 && TREE_CODE (st1) == INTEGER_CST
2024 : 984275 : && st2 && TREE_CODE (st2) == INTEGER_CST
2025 : 2561766 : && !tree_int_cst_equal (st1, st2))
2026 : : return false;
2027 : :
2028 : 1509904 : tree counted_by1 = lookup_attribute ("counted_by",
2029 : 1509904 : DECL_ATTRIBUTES (s1));
2030 : 1509904 : tree counted_by2 = lookup_attribute ("counted_by",
2031 : 1509904 : DECL_ATTRIBUTES (s2));
2032 : : /* If there is no counted_by attribute for both fields. */
2033 : 1509904 : if (!counted_by1 && !counted_by2)
2034 : 1509850 : continue;
2035 : :
2036 : : /* If only one field has counted_by attribute. */
2037 : 54 : if ((counted_by1 && !counted_by2)
2038 : 54 : || (!counted_by1 && counted_by2))
2039 : : return false;
2040 : :
2041 : : /* Now both s1 and s2 have counted_by attributes, check
2042 : : whether they are the same. */
2043 : :
2044 : 42 : tree counted_by_field1
2045 : 42 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2046 : 42 : tree counted_by_field2
2047 : 42 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2048 : :
2049 : 42 : gcc_assert (counted_by_field1 && counted_by_field2);
2050 : :
2051 : 84 : while (TREE_CHAIN (counted_by_field1))
2052 : 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2053 : 76 : while (TREE_CHAIN (counted_by_field2))
2054 : 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2055 : :
2056 : 42 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2057 : 42 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2058 : : return false;
2059 : : }
2060 : : return true;
2061 : :
2062 : 0 : default:
2063 : 0 : gcc_unreachable ();
2064 : : }
2065 : : }
2066 : :
2067 : : /* Return true if two function types F1 and F2 are compatible.
2068 : : If either type specifies no argument types,
2069 : : the other must specify a fixed number of self-promoting arg types.
2070 : : Otherwise, if one type specifies only the number of arguments,
2071 : : the other must specify that number of self-promoting arg types.
2072 : : Otherwise, the argument types must match. */
2073 : :
2074 : : static bool
2075 : 8777012 : function_types_compatible_p (const_tree f1, const_tree f2,
2076 : : struct comptypes_data *data)
2077 : : {
2078 : 8777012 : tree ret1 = TREE_TYPE (f1);
2079 : 8777012 : tree ret2 = TREE_TYPE (f2);
2080 : :
2081 : : /* 'volatile' qualifiers on a function's return type used to mean
2082 : : the function is noreturn. */
2083 : 8777012 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2084 : 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2085 : 8777012 : if (TYPE_VOLATILE (ret1))
2086 : 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2087 : 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2088 : 8777012 : if (TYPE_VOLATILE (ret2))
2089 : 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2090 : 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2091 : :
2092 : 8777012 : bool ignore_pargs = data->ignore_promoting_args;
2093 : 8777012 : data->ignore_promoting_args = false;
2094 : :
2095 : 8777012 : if (!comptypes_internal (ret1, ret2, data))
2096 : : return false;
2097 : :
2098 : 8774425 : data->ignore_promoting_args = ignore_pargs;
2099 : :
2100 : 8774425 : tree args1 = TYPE_ARG_TYPES (f1);
2101 : 8774425 : tree args2 = TYPE_ARG_TYPES (f2);
2102 : :
2103 : 8774425 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2104 : 40970 : data->different_types_p = true;
2105 : :
2106 : : /* An unspecified parmlist matches any specified parmlist
2107 : : whose argument types don't need default promotions. */
2108 : :
2109 : 8774425 : if (args1 == NULL_TREE)
2110 : : {
2111 : 21626 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2112 : : return false;
2113 : 21624 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2114 : : return false;
2115 : 21154 : data->ignore_promoting_args = false;
2116 : : /* If one of these types comes from a non-prototype fn definition,
2117 : : compare that with the other type's arglist.
2118 : : If they don't match, ask for a warning (but no error). */
2119 : 21154 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2120 : 21154 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2121 : 12 : data->warning_needed = true;
2122 : 21154 : return true;
2123 : : }
2124 : 8752799 : if (args2 == NULL_TREE)
2125 : : {
2126 : 19425 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2127 : : return false;
2128 : 19423 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2129 : : return false;
2130 : 19362 : data->ignore_promoting_args = false;
2131 : 19362 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2132 : 19362 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2133 : 1 : data->warning_needed = true;
2134 : 19362 : return true;
2135 : : }
2136 : :
2137 : : /* Both types have argument lists: compare them and propagate results. */
2138 : 8733374 : return type_lists_compatible_p (args1, args2, data);
2139 : : }
2140 : :
2141 : : /* Check two lists of types for compatibility, returning false for
2142 : : incompatible, true for compatible. */
2143 : :
2144 : : static bool
2145 : 8733450 : type_lists_compatible_p (const_tree args1, const_tree args2,
2146 : : struct comptypes_data *data)
2147 : : {
2148 : 54407142 : while (1)
2149 : : {
2150 : 31570296 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2151 : : return true;
2152 : : /* If one list is shorter than the other,
2153 : : they fail to match. */
2154 : 22979654 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2155 : : return false;
2156 : 22979647 : tree a1 = TREE_VALUE (args1);
2157 : 22979647 : tree a2 = TREE_VALUE (args2);
2158 : 22979647 : tree mv1 = remove_qualifiers (a1);
2159 : 22979647 : tree mv2 = remove_qualifiers (a2);
2160 : : /* A null pointer instead of a type
2161 : : means there is supposed to be an argument
2162 : : but nothing is specified about what type it has.
2163 : : So match anything that self-promotes. */
2164 : 22979647 : if ((a1 == NULL_TREE) != (a2 == NULL_TREE))
2165 : 0 : data->different_types_p = true;
2166 : 22979647 : if (a1 == NULL_TREE)
2167 : : {
2168 : 0 : if (c_type_promotes_to (a2) != a2)
2169 : : return false;
2170 : : }
2171 : 22979647 : else if (a2 == NULL_TREE)
2172 : : {
2173 : 0 : if (c_type_promotes_to (a1) != a1)
2174 : : return false;
2175 : : }
2176 : : /* If one of the lists has an error marker, ignore this arg. */
2177 : 22979647 : else if (TREE_CODE (a1) == ERROR_MARK
2178 : 22979643 : || TREE_CODE (a2) == ERROR_MARK)
2179 : : ;
2180 : 22979627 : else if (!comptypes_internal (mv1, mv2, data))
2181 : : {
2182 : 143018 : data->different_types_p = true;
2183 : : /* Allow wait (union {union wait *u; int *i} *)
2184 : : and wait (union wait *) to be compatible. */
2185 : 143018 : if (TREE_CODE (a1) == UNION_TYPE
2186 : 122 : && (TYPE_NAME (a1) == NULL_TREE
2187 : 94 : || TYPE_TRANSPARENT_AGGR (a1))
2188 : 122 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2189 : 143140 : && tree_int_cst_equal (TYPE_SIZE (a1),
2190 : 122 : TYPE_SIZE (a2)))
2191 : : {
2192 : 122 : tree memb;
2193 : 122 : for (memb = TYPE_FIELDS (a1);
2194 : 158 : memb; memb = DECL_CHAIN (memb))
2195 : : {
2196 : 156 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2197 : 156 : if (comptypes_internal (mv3, mv2, data))
2198 : : break;
2199 : : }
2200 : : if (memb == NULL_TREE)
2201 : : return false;
2202 : : }
2203 : 142896 : else if (TREE_CODE (a2) == UNION_TYPE
2204 : 99 : && (TYPE_NAME (a2) == NULL_TREE
2205 : 71 : || TYPE_TRANSPARENT_AGGR (a2))
2206 : 99 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2207 : 142995 : && tree_int_cst_equal (TYPE_SIZE (a2),
2208 : 99 : TYPE_SIZE (a1)))
2209 : : {
2210 : 99 : tree memb;
2211 : 99 : for (memb = TYPE_FIELDS (a2);
2212 : 127 : memb; memb = DECL_CHAIN (memb))
2213 : : {
2214 : 125 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2215 : 125 : if (comptypes_internal (mv3, mv1, data))
2216 : : break;
2217 : : }
2218 : : if (memb == NULL_TREE)
2219 : : return false;
2220 : : }
2221 : : else
2222 : 142797 : return false;
2223 : : }
2224 : :
2225 : 22836846 : args1 = TREE_CHAIN (args1);
2226 : 22836846 : args2 = TREE_CHAIN (args2);
2227 : 22836846 : }
2228 : : }
2229 : :
2230 : : /* Compute the size to increment a pointer by. When a function type or void
2231 : : type or incomplete type is passed, size_one_node is returned.
2232 : : This function does not emit any diagnostics; the caller is responsible
2233 : : for that. */
2234 : :
2235 : : static tree
2236 : 232658 : c_size_in_bytes (const_tree type)
2237 : : {
2238 : 232658 : enum tree_code code = TREE_CODE (type);
2239 : :
2240 : 232658 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2241 : 232658 : || !COMPLETE_TYPE_P (type))
2242 : 180 : return size_one_node;
2243 : :
2244 : : /* Convert in case a char is more than one unit. */
2245 : 232478 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2246 : 232478 : size_int (TYPE_PRECISION (char_type_node)
2247 : : / BITS_PER_UNIT));
2248 : : }
2249 : :
2250 : : /* Return either DECL or its known constant value (if it has one). */
2251 : :
2252 : : tree
2253 : 13981226 : decl_constant_value_1 (tree decl, bool in_init)
2254 : : {
2255 : 13981226 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2256 : 13981226 : TREE_CODE (decl) != PARM_DECL
2257 : 13981226 : && !TREE_THIS_VOLATILE (decl)
2258 : 13637651 : && TREE_READONLY (decl)
2259 : 36310 : && DECL_INITIAL (decl) != NULL_TREE
2260 : 35299 : && !error_operand_p (DECL_INITIAL (decl))
2261 : : /* This is invalid if initial value is not constant.
2262 : : If it has either a function call, a memory reference,
2263 : : or a variable, then re-evaluating it could give different results. */
2264 : 35290 : && TREE_CONSTANT (DECL_INITIAL (decl))
2265 : : /* Check for cases where this is sub-optimal, even though valid. */
2266 : 14003678 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2267 : 17253 : return DECL_INITIAL (decl);
2268 : : return decl;
2269 : : }
2270 : :
2271 : : /* Return either DECL or its known constant value (if it has one).
2272 : : Like the above, but always return decl outside of functions. */
2273 : :
2274 : : tree
2275 : 13980982 : decl_constant_value (tree decl)
2276 : : {
2277 : : /* Don't change a variable array bound or initial value to a constant
2278 : : in a place where a variable is invalid. */
2279 : 13980982 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2280 : : }
2281 : :
2282 : : /* Convert the array expression EXP to a pointer. */
2283 : : static tree
2284 : 814814 : array_to_pointer_conversion (location_t loc, tree exp)
2285 : : {
2286 : 814814 : tree orig_exp = exp;
2287 : 814814 : tree type = TREE_TYPE (exp);
2288 : 814814 : tree adr;
2289 : 814814 : tree restype = TREE_TYPE (type);
2290 : 814814 : tree ptrtype;
2291 : :
2292 : 814814 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2293 : :
2294 : 814814 : STRIP_TYPE_NOPS (exp);
2295 : :
2296 : 814814 : copy_warning (exp, orig_exp);
2297 : :
2298 : 814814 : ptrtype = c_build_pointer_type (restype);
2299 : :
2300 : 814814 : if (INDIRECT_REF_P (exp))
2301 : 2883 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2302 : :
2303 : : /* In C++ array compound literals are temporary objects unless they are
2304 : : const or appear in namespace scope, so they are destroyed too soon
2305 : : to use them for much of anything (c++/53220). */
2306 : 811931 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2307 : : {
2308 : 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2309 : 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2310 : 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2311 : : "converting an array compound literal to a pointer "
2312 : : "leads to a dangling pointer in C++");
2313 : : }
2314 : :
2315 : 811931 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2316 : 811931 : return convert (ptrtype, adr);
2317 : : }
2318 : :
2319 : : /* Convert the function expression EXP to a pointer. */
2320 : : static tree
2321 : 49321360 : function_to_pointer_conversion (location_t loc, tree exp)
2322 : : {
2323 : 49321360 : tree orig_exp = exp;
2324 : :
2325 : 49321360 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2326 : :
2327 : 49321360 : STRIP_TYPE_NOPS (exp);
2328 : :
2329 : 49321360 : copy_warning (exp, orig_exp);
2330 : :
2331 : 49321360 : return build_unary_op (loc, ADDR_EXPR, exp, false);
2332 : : }
2333 : :
2334 : : /* Mark EXP as read, not just set, for set but not used -Wunused
2335 : : warning purposes. */
2336 : :
2337 : : void
2338 : 479200100 : mark_exp_read (tree exp)
2339 : : {
2340 : 602347514 : switch (TREE_CODE (exp))
2341 : : {
2342 : 198408890 : case VAR_DECL:
2343 : 198408890 : case PARM_DECL:
2344 : 198408890 : DECL_READ_P (exp) = 1;
2345 : 198408890 : break;
2346 : 19245686 : CASE_CONVERT:
2347 : 19245686 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2348 : 3790 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2349 : : {
2350 : : case PREINCREMENT_EXPR:
2351 : : case PREDECREMENT_EXPR:
2352 : : case POSTINCREMENT_EXPR:
2353 : : case POSTDECREMENT_EXPR:
2354 : : return;
2355 : : default:
2356 : : break;
2357 : : }
2358 : : /* FALLTHRU */
2359 : 120461467 : case ARRAY_REF:
2360 : 120461467 : case COMPONENT_REF:
2361 : 120461467 : case MODIFY_EXPR:
2362 : 120461467 : case REALPART_EXPR:
2363 : 120461467 : case IMAGPART_EXPR:
2364 : 120461467 : case ADDR_EXPR:
2365 : 120461467 : case VIEW_CONVERT_EXPR:
2366 : 120461467 : case PREINCREMENT_EXPR:
2367 : 120461467 : case PREDECREMENT_EXPR:
2368 : 120461467 : case POSTINCREMENT_EXPR:
2369 : 120461467 : case POSTDECREMENT_EXPR:
2370 : 120461467 : mark_exp_read (TREE_OPERAND (exp, 0));
2371 : 120461467 : break;
2372 : 235933 : case COMPOUND_EXPR:
2373 : : /* Pattern match what build_atomic_assign produces with modifycode
2374 : : NOP_EXPR. */
2375 : 235933 : if (VAR_P (TREE_OPERAND (exp, 1))
2376 : 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2377 : 263076 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2378 : : {
2379 : 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2380 : 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2381 : 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2382 : 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2383 : 4410 : && TREE_CODE (t2) == CALL_EXPR)
2384 : : {
2385 : 2200 : tree fndecl = get_callee_fndecl (t2);
2386 : 2200 : tree arg = NULL_TREE;
2387 : 2200 : if (fndecl
2388 : 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2389 : 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2390 : 4400 : && call_expr_nargs (t2) >= 2)
2391 : 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2392 : : {
2393 : 130 : case BUILT_IN_ATOMIC_STORE:
2394 : 130 : arg = CALL_EXPR_ARG (t2, 1);
2395 : 130 : break;
2396 : 2070 : case BUILT_IN_ATOMIC_STORE_1:
2397 : 2070 : case BUILT_IN_ATOMIC_STORE_2:
2398 : 2070 : case BUILT_IN_ATOMIC_STORE_4:
2399 : 2070 : case BUILT_IN_ATOMIC_STORE_8:
2400 : 2070 : case BUILT_IN_ATOMIC_STORE_16:
2401 : 2070 : arg = CALL_EXPR_ARG (t2, 0);
2402 : 2070 : break;
2403 : : default:
2404 : : break;
2405 : : }
2406 : 2200 : if (arg)
2407 : : {
2408 : 2200 : STRIP_NOPS (arg);
2409 : 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2410 : 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2411 : 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2412 : 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2413 : : }
2414 : : }
2415 : : }
2416 : : /* FALLTHRU */
2417 : 2685412 : case C_MAYBE_CONST_EXPR:
2418 : 2685412 : mark_exp_read (TREE_OPERAND (exp, 1));
2419 : 2685412 : break;
2420 : 570 : case OMP_ARRAY_SECTION:
2421 : 570 : mark_exp_read (TREE_OPERAND (exp, 0));
2422 : 570 : if (TREE_OPERAND (exp, 1))
2423 : 377 : mark_exp_read (TREE_OPERAND (exp, 1));
2424 : 570 : if (TREE_OPERAND (exp, 2))
2425 : 535 : mark_exp_read (TREE_OPERAND (exp, 2));
2426 : : break;
2427 : : default:
2428 : : break;
2429 : : }
2430 : : }
2431 : :
2432 : : /* Perform the default conversion of arrays and functions to pointers.
2433 : : Return the result of converting EXP. For any other expression, just
2434 : : return EXP.
2435 : :
2436 : : LOC is the location of the expression. */
2437 : :
2438 : : struct c_expr
2439 : 353114535 : default_function_array_conversion (location_t loc, struct c_expr exp)
2440 : : {
2441 : 353114535 : tree orig_exp = exp.value;
2442 : 353114535 : tree type = TREE_TYPE (exp.value);
2443 : 353114535 : enum tree_code code = TREE_CODE (type);
2444 : :
2445 : 353114535 : switch (code)
2446 : : {
2447 : : case ARRAY_TYPE:
2448 : : {
2449 : : bool not_lvalue = false;
2450 : : bool lvalue_array_p;
2451 : :
2452 : 768006 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2453 : 768006 : || CONVERT_EXPR_P (exp.value))
2454 : 768006 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2455 : : {
2456 : 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2457 : 0 : not_lvalue = true;
2458 : 0 : exp.value = TREE_OPERAND (exp.value, 0);
2459 : : }
2460 : :
2461 : 768006 : copy_warning (exp.value, orig_exp);
2462 : :
2463 : 768006 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2464 : 768006 : if (!flag_isoc99 && !lvalue_array_p)
2465 : : {
2466 : : /* Before C99, non-lvalue arrays do not decay to pointers.
2467 : : Normally, using such an array would be invalid; but it can
2468 : : be used correctly inside sizeof or as a statement expression.
2469 : : Thus, do not give an error here; an error will result later. */
2470 : 108 : return exp;
2471 : : }
2472 : :
2473 : 767898 : exp.value = array_to_pointer_conversion (loc, exp.value);
2474 : : }
2475 : 767898 : break;
2476 : 761689 : case FUNCTION_TYPE:
2477 : 761689 : exp.value = function_to_pointer_conversion (loc, exp.value);
2478 : 761689 : break;
2479 : : default:
2480 : : break;
2481 : : }
2482 : :
2483 : 353114427 : return exp;
2484 : : }
2485 : :
2486 : : struct c_expr
2487 : 926700 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2488 : : {
2489 : 926700 : mark_exp_read (exp.value);
2490 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2491 : 926700 : if (TREE_CODE (exp.value) == COMPONENT_REF
2492 : 926700 : && handle_counted_by_p (exp.value))
2493 : 65656 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2494 : 926700 : return default_function_array_conversion (loc, exp);
2495 : : }
2496 : :
2497 : : /* Return whether EXPR should be treated as an atomic lvalue for the
2498 : : purposes of load and store handling. */
2499 : :
2500 : : static bool
2501 : 354186918 : really_atomic_lvalue (tree expr)
2502 : : {
2503 : 354186918 : if (error_operand_p (expr))
2504 : : return false;
2505 : 354179285 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2506 : : return false;
2507 : 57304 : if (!lvalue_p (expr))
2508 : : return false;
2509 : :
2510 : : /* Ignore _Atomic on register variables, since their addresses can't
2511 : : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2512 : : sequences wouldn't work. Ignore _Atomic on structures containing
2513 : : bit-fields, since accessing elements of atomic structures or
2514 : : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2515 : : it's undefined at translation time or execution time, and the
2516 : : normal atomic sequences again wouldn't work. */
2517 : 57366 : while (handled_component_p (expr))
2518 : : {
2519 : 64 : if (TREE_CODE (expr) == COMPONENT_REF
2520 : 64 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2521 : : return false;
2522 : 64 : expr = TREE_OPERAND (expr, 0);
2523 : : }
2524 : 57302 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2525 : : return false;
2526 : : return true;
2527 : : }
2528 : :
2529 : : /* If EXPR is a named constant (C23) derived from a constexpr variable
2530 : : - that is, a reference to such a variable, or a member extracted by
2531 : : a sequence of structure and union (but not array) member accesses
2532 : : (where union member accesses must access the same member as
2533 : : initialized) - then return the corresponding initializer;
2534 : : otherwise, return NULL_TREE. */
2535 : :
2536 : : static tree
2537 : 351062868 : maybe_get_constexpr_init (tree expr)
2538 : : {
2539 : 351062868 : tree decl = NULL_TREE;
2540 : 351062868 : if (TREE_CODE (expr) == VAR_DECL)
2541 : : decl = expr;
2542 : 339611153 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2543 : 803017 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2544 : 803017 : if (decl
2545 : 12254732 : && C_DECL_DECLARED_CONSTEXPR (decl)
2546 : 354 : && DECL_INITIAL (decl) != NULL_TREE
2547 : 803371 : && !error_operand_p (DECL_INITIAL (decl)))
2548 : 354 : return DECL_INITIAL (decl);
2549 : 351062514 : if (TREE_CODE (expr) != COMPONENT_REF)
2550 : : return NULL_TREE;
2551 : 857043 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2552 : 857043 : if (inner == NULL_TREE)
2553 : : return NULL_TREE;
2554 : 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2555 : 47 : && !error_operand_p (inner)
2556 : 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2557 : 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2558 : 47 : inner = TREE_OPERAND (inner, 0);
2559 : 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2560 : : return NULL_TREE;
2561 : 126 : tree field = TREE_OPERAND (expr, 1);
2562 : 126 : unsigned HOST_WIDE_INT cidx;
2563 : 126 : tree cfield, cvalue;
2564 : 126 : bool have_other_init = false;
2565 : 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2566 : : {
2567 : 250 : if (cfield == field)
2568 : : return cvalue;
2569 : 140 : have_other_init = true;
2570 : : }
2571 : 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2572 : 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2573 : : return NULL_TREE;
2574 : : /* Return a default initializer. */
2575 : 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2576 : 5 : return build_constructor (TREE_TYPE (expr), NULL);
2577 : 8 : return build_zero_cst (TREE_TYPE (expr));
2578 : : }
2579 : :
2580 : : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2581 : : including converting functions and arrays to pointers if CONVERT_P.
2582 : : If READ_P, also mark the expression as having been read. If
2583 : : FOR_INIT, constexpr expressions of structure and union type should
2584 : : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2585 : : constexpr scalars (including elements of structures and unions) are
2586 : : replaced by their initializers. */
2587 : :
2588 : : struct c_expr
2589 : 350391941 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2590 : : bool convert_p, bool read_p, bool for_init)
2591 : : {
2592 : 350391941 : bool force_non_npc = false;
2593 : 350391941 : if (read_p)
2594 : 307437687 : mark_exp_read (exp.value);
2595 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2596 : 307437687 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2597 : 767416 : && handle_counted_by_p (exp.value))
2598 : 767387 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2599 : :
2600 : 350391941 : if (convert_p)
2601 : 350378795 : exp = default_function_array_conversion (loc, exp);
2602 : 350391941 : if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2603 : 347953657 : exp.value = require_complete_type (loc, exp.value);
2604 : 350391941 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2605 : : {
2606 : 350205825 : tree init = maybe_get_constexpr_init (exp.value);
2607 : 350205825 : if (init != NULL_TREE)
2608 : : {
2609 : : /* A named constant of pointer type or type nullptr_t is not
2610 : : a null pointer constant even if the initializer is
2611 : : one. */
2612 : 351 : if (TREE_CODE (init) == INTEGER_CST
2613 : 216 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2614 : 377 : && integer_zerop (init))
2615 : : force_non_npc = true;
2616 : : exp.value = init;
2617 : : }
2618 : : }
2619 : 350391941 : if (really_atomic_lvalue (exp.value))
2620 : : {
2621 : 26861 : vec<tree, va_gc> *params;
2622 : 26861 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2623 : 26861 : tree expr_type = TREE_TYPE (exp.value);
2624 : 26861 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2625 : 26861 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2626 : :
2627 : 26861 : gcc_assert (TYPE_ATOMIC (expr_type));
2628 : :
2629 : : /* Expansion of a generic atomic load may require an addition
2630 : : element, so allocate enough to prevent a resize. */
2631 : 26861 : vec_alloc (params, 4);
2632 : :
2633 : : /* Remove the qualifiers for the rest of the expressions and
2634 : : create the VAL temp variable to hold the RHS. */
2635 : 26861 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2636 : 26861 : tmp = create_tmp_var_raw (nonatomic_type);
2637 : 26861 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2638 : 26861 : TREE_ADDRESSABLE (tmp) = 1;
2639 : : /* Do not disable warnings for TMP even though it's artificial.
2640 : : -Winvalid-memory-model depends on it. */
2641 : :
2642 : : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2643 : 26861 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2644 : 26861 : params->quick_push (expr_addr);
2645 : 26861 : params->quick_push (tmp_addr);
2646 : 26861 : params->quick_push (seq_cst);
2647 : 26861 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2648 : :
2649 : : /* EXPR is always read. */
2650 : 26861 : mark_exp_read (exp.value);
2651 : :
2652 : : /* Return tmp which contains the value loaded. */
2653 : 26861 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2654 : : NULL_TREE, NULL_TREE);
2655 : : }
2656 : 350378795 : if (convert_p && !error_operand_p (exp.value)
2657 : 700763147 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2658 : 350371098 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2659 : 350391941 : if (force_non_npc)
2660 : 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2661 : :
2662 : 350391941 : {
2663 : 350391941 : tree false_value, true_value;
2664 : 350378795 : if (convert_p && !error_operand_p (exp.value)
2665 : 700763147 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2666 : : &false_value, &true_value))
2667 : : {
2668 : 9717 : tree t = save_expr (exp.value);
2669 : :
2670 : 9717 : mark_exp_read (exp.value);
2671 : :
2672 : 9717 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2673 : 9717 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2674 : 9717 : expr = build_compound_expr (loc, expr, boolean_true_node);
2675 : 9717 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2676 : : fold_build2_loc (loc, NE_EXPR,
2677 : : boolean_type_node,
2678 : : t, true_value),
2679 : : expr, boolean_true_node);
2680 : 9717 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2681 : : fold_build2_loc (loc, NE_EXPR,
2682 : : boolean_type_node,
2683 : : t, false_value),
2684 : : expr, boolean_false_node);
2685 : :
2686 : 9717 : exp.value = expr;
2687 : : }
2688 : : }
2689 : :
2690 : 350391941 : return exp;
2691 : : }
2692 : :
2693 : : /* Wrapper for the overload above, same arguments but for tree rather than
2694 : : c_expr. This is important for hardbools to decay to bools. */
2695 : :
2696 : : static inline tree
2697 : 561381 : convert_lvalue_to_rvalue (location_t loc, tree val,
2698 : : bool convert_p, bool read_p, bool for_init = false)
2699 : : {
2700 : 561381 : struct c_expr expr;
2701 : 561381 : memset (&expr, 0, sizeof (expr));
2702 : 561381 : expr.value = val;
2703 : 561381 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2704 : 561381 : return expr.value;
2705 : : }
2706 : :
2707 : : /* EXP is an expression of integer type. Apply the integer promotions
2708 : : to it and return the promoted value. */
2709 : :
2710 : : tree
2711 : 76458902 : perform_integral_promotions (tree exp)
2712 : : {
2713 : 76458902 : tree type = TREE_TYPE (exp);
2714 : 76458902 : enum tree_code code = TREE_CODE (type);
2715 : :
2716 : 76458902 : gcc_assert (INTEGRAL_TYPE_P (type));
2717 : :
2718 : : /* Convert enums to the result of applying the integer promotions to
2719 : : their underlying type. */
2720 : 76458902 : if (code == ENUMERAL_TYPE)
2721 : : {
2722 : 634734 : type = ENUM_UNDERLYING_TYPE (type);
2723 : 634734 : if (c_promoting_integer_type_p (type))
2724 : : {
2725 : 94 : if (TYPE_UNSIGNED (type)
2726 : 94 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2727 : 0 : type = unsigned_type_node;
2728 : : else
2729 : 94 : type = integer_type_node;
2730 : : }
2731 : :
2732 : 634734 : return convert (type, exp);
2733 : : }
2734 : :
2735 : : /* ??? This should no longer be needed now bit-fields have their
2736 : : proper types. */
2737 : 75824168 : if (TREE_CODE (exp) == COMPONENT_REF
2738 : 75824168 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2739 : : {
2740 : 62165 : if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2741 : : == BITINT_TYPE)
2742 : 454 : return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2743 : : /* If it's thinner than an int, promote it like a
2744 : : c_promoting_integer_type_p, otherwise leave it alone. */
2745 : 61711 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2746 : 61711 : TYPE_PRECISION (integer_type_node)) < 0)
2747 : 53449 : return convert (integer_type_node, exp);
2748 : : }
2749 : :
2750 : 75770265 : if (c_promoting_integer_type_p (type))
2751 : : {
2752 : : /* Preserve unsignedness if not really getting any wider. */
2753 : 628757 : if (TYPE_UNSIGNED (type)
2754 : 628757 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2755 : 0 : return convert (unsigned_type_node, exp);
2756 : :
2757 : 628757 : return convert (integer_type_node, exp);
2758 : : }
2759 : :
2760 : : return exp;
2761 : : }
2762 : :
2763 : :
2764 : : /* Perform default promotions for C data used in expressions.
2765 : : Enumeral types or short or char are converted to int.
2766 : : In addition, manifest constants symbols are replaced by their values. */
2767 : :
2768 : : tree
2769 : 85756147 : default_conversion (tree exp)
2770 : : {
2771 : 85756147 : tree orig_exp;
2772 : 85756147 : tree type = TREE_TYPE (exp);
2773 : 85756147 : enum tree_code code = TREE_CODE (type);
2774 : 85756147 : tree promoted_type;
2775 : :
2776 : 85756147 : mark_exp_read (exp);
2777 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2778 : 85756147 : if (TREE_CODE (exp) == COMPONENT_REF
2779 : 85756147 : && handle_counted_by_p (exp))
2780 : 589282 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2781 : :
2782 : : /* Functions and arrays have been converted during parsing. */
2783 : 85756147 : gcc_assert (code != FUNCTION_TYPE);
2784 : 85756147 : if (code == ARRAY_TYPE)
2785 : : return exp;
2786 : :
2787 : : /* Constants can be used directly unless they're not loadable. */
2788 : 85755988 : if (TREE_CODE (exp) == CONST_DECL)
2789 : 0 : exp = DECL_INITIAL (exp);
2790 : :
2791 : : /* Strip no-op conversions. */
2792 : 85755988 : orig_exp = exp;
2793 : 86122266 : STRIP_TYPE_NOPS (exp);
2794 : :
2795 : 85755988 : copy_warning (exp, orig_exp);
2796 : :
2797 : 85755988 : if (code == VOID_TYPE)
2798 : : {
2799 : 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2800 : : "void value not ignored as it ought to be");
2801 : 2 : return error_mark_node;
2802 : : }
2803 : :
2804 : 85755986 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2805 : 85755986 : if (exp == error_mark_node)
2806 : : return error_mark_node;
2807 : :
2808 : 85755087 : promoted_type = targetm.promoted_type (type);
2809 : 85755087 : if (promoted_type)
2810 : 0 : return convert (promoted_type, exp);
2811 : :
2812 : 85755087 : if (INTEGRAL_TYPE_P (type))
2813 : 75466725 : return perform_integral_promotions (exp);
2814 : :
2815 : : return exp;
2816 : : }
2817 : :
2818 : : /* Look up COMPONENT in a structure or union TYPE.
2819 : :
2820 : : If the component name is not found, returns NULL_TREE. Otherwise,
2821 : : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2822 : : stepping down the chain to the component, which is in the last
2823 : : TREE_VALUE of the list. Normally the list is of length one, but if
2824 : : the component is embedded within (nested) anonymous structures or
2825 : : unions, the list steps down the chain to the component. */
2826 : :
2827 : : tree
2828 : 2269884 : lookup_field (const_tree type, tree component)
2829 : : {
2830 : 2269884 : tree field;
2831 : :
2832 : : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2833 : : to the field elements. Use a binary search on this array to quickly
2834 : : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2835 : : will always be set for structures which have many elements.
2836 : :
2837 : : Duplicate field checking replaces duplicates with NULL_TREE so
2838 : : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2839 : : case just iterate using DECL_CHAIN. */
2840 : :
2841 : 2566300 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2842 : 2566300 : && !seen_error ())
2843 : : {
2844 : 296395 : int bot, top, half;
2845 : 296395 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2846 : :
2847 : 296395 : field = TYPE_FIELDS (type);
2848 : 296395 : bot = 0;
2849 : 296395 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2850 : 1361265 : while (top - bot > 1)
2851 : : {
2852 : 1333055 : half = (top - bot + 1) >> 1;
2853 : 1333055 : field = field_array[bot+half];
2854 : :
2855 : 1333055 : if (DECL_NAME (field) == NULL_TREE)
2856 : : {
2857 : : /* Step through all anon unions in linear fashion. */
2858 : 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
2859 : : {
2860 : 0 : field = field_array[bot++];
2861 : 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2862 : : {
2863 : 0 : tree anon = lookup_field (TREE_TYPE (field), component);
2864 : :
2865 : 0 : if (anon)
2866 : 0 : return tree_cons (NULL_TREE, field, anon);
2867 : :
2868 : : /* The Plan 9 compiler permits referring
2869 : : directly to an anonymous struct/union field
2870 : : using a typedef name. */
2871 : 0 : if (flag_plan9_extensions
2872 : 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2873 : 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2874 : : == TYPE_DECL)
2875 : 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2876 : : == component))
2877 : : break;
2878 : : }
2879 : : }
2880 : :
2881 : : /* Entire record is only anon unions. */
2882 : 0 : if (bot > top)
2883 : : return NULL_TREE;
2884 : :
2885 : : /* Restart the binary search, with new lower bound. */
2886 : 0 : continue;
2887 : 0 : }
2888 : :
2889 : 1333055 : if (DECL_NAME (field) == component)
2890 : : break;
2891 : 1064870 : if (DECL_NAME (field) < component)
2892 : : bot += half;
2893 : : else
2894 : 880940 : top = bot + half;
2895 : : }
2896 : :
2897 : 296395 : if (DECL_NAME (field_array[bot]) == component)
2898 : : field = field_array[bot];
2899 : 268185 : else if (DECL_NAME (field) != component)
2900 : : return NULL_TREE;
2901 : : }
2902 : : else
2903 : : {
2904 : 5167333 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2905 : : {
2906 : 5158576 : if (DECL_NAME (field) == NULL_TREE
2907 : 5158576 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2908 : : {
2909 : 11092 : tree anon = lookup_field (TREE_TYPE (field), component);
2910 : :
2911 : 11092 : if (anon)
2912 : 2410 : return tree_cons (NULL_TREE, field, anon);
2913 : :
2914 : : /* The Plan 9 compiler permits referring directly to an
2915 : : anonymous struct/union field using a typedef
2916 : : name. */
2917 : 8682 : if (flag_plan9_extensions
2918 : 38 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2919 : 36 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2920 : 8688 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2921 : : == component))
2922 : : break;
2923 : : }
2924 : :
2925 : 5156162 : if (DECL_NAME (field) == component)
2926 : : break;
2927 : : }
2928 : :
2929 : 1971079 : if (field == NULL_TREE)
2930 : : return NULL_TREE;
2931 : : }
2932 : :
2933 : 2258717 : return tree_cons (NULL_TREE, field, NULL_TREE);
2934 : : }
2935 : :
2936 : : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2937 : :
2938 : : static void
2939 : 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
2940 : : vec<tree> *candidates)
2941 : : {
2942 : 101 : tree field;
2943 : 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2944 : : {
2945 : 157 : if (DECL_NAME (field) == NULL_TREE
2946 : 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2947 : 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2948 : : candidates);
2949 : :
2950 : 157 : if (DECL_NAME (field))
2951 : 111 : candidates->safe_push (DECL_NAME (field));
2952 : : }
2953 : 101 : }
2954 : :
2955 : : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2956 : : rather than returning a TREE_LIST for an exact match. */
2957 : :
2958 : : static tree
2959 : 71 : lookup_field_fuzzy (tree type, tree component)
2960 : : {
2961 : 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2962 : :
2963 : : /* First, gather a list of candidates. */
2964 : 71 : auto_vec <tree> candidates;
2965 : :
2966 : 71 : lookup_field_fuzzy_find_candidates (type, component,
2967 : : &candidates);
2968 : :
2969 : 71 : return find_closest_identifier (component, &candidates);
2970 : 71 : }
2971 : :
2972 : : /* Support function for build_component_ref's error-handling.
2973 : :
2974 : : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2975 : : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2976 : :
2977 : : static bool
2978 : 72 : should_suggest_deref_p (tree datum_type)
2979 : : {
2980 : : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2981 : : allows "." for ptrs; we could be handling a failed attempt
2982 : : to access a property. */
2983 : 72 : if (c_dialect_objc ())
2984 : : return false;
2985 : :
2986 : : /* Only suggest it for pointers... */
2987 : 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
2988 : : return false;
2989 : :
2990 : : /* ...to structs/unions. */
2991 : 8 : tree underlying_type = TREE_TYPE (datum_type);
2992 : 8 : enum tree_code code = TREE_CODE (underlying_type);
2993 : 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
2994 : : return true;
2995 : : else
2996 : 1 : return false;
2997 : : }
2998 : :
2999 : : /* Give a component ref REF, decide whether we should handle its counted_by
3000 : : attribute based on its context:
3001 : : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3002 : :
3003 : : static bool
3004 : 2232216 : handle_counted_by_p (tree ref)
3005 : : {
3006 : 2232216 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3007 : 2232216 : tree datum = TREE_OPERAND (ref, 0);
3008 : : /* If the component_ref is build for a offsetof, i.e., the datum
3009 : : of the component_ref is a indirect_ref of null_pointer_node,
3010 : : we should not generate call to .ACCESS_WITH_SIZE. */
3011 : 2232216 : if (TREE_CODE (datum) == INDIRECT_REF
3012 : 2232216 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3013 : : return false;
3014 : 2231890 : if (in_typeof || in_alignof)
3015 : 8783 : return false;
3016 : : return true;
3017 : : }
3018 : :
3019 : : /* Given a component ref REF, if there is a counted_by attribute attached,
3020 : : issue error when the element_type is a structure or union including a
3021 : : flexible array member. */
3022 : :
3023 : : static void
3024 : 2228437 : check_counted_by_attribute (location_t loc, tree ref)
3025 : : {
3026 : 2228437 : tree subdatum = TREE_OPERAND (ref, 1);
3027 : 2228437 : tree sub_type = TREE_TYPE (subdatum);
3028 : :
3029 : 2228437 : if (!c_flexible_array_member_type_p (sub_type)
3030 : 2228437 : && TREE_CODE (sub_type) != POINTER_TYPE)
3031 : : return;
3032 : :
3033 : 381097 : tree element_type = TREE_TYPE (sub_type);
3034 : :
3035 : 381097 : tree attr_counted_by = lookup_attribute ("counted_by",
3036 : 381097 : DECL_ATTRIBUTES (subdatum));
3037 : 381097 : if (attr_counted_by)
3038 : : {
3039 : : /* Issue error when the element_type is a structure or
3040 : : union including a flexible array member. */
3041 : 397 : if (RECORD_OR_UNION_TYPE_P (element_type)
3042 : 397 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3043 : : {
3044 : 2 : error_at (loc,
3045 : : "%<counted_by%> attribute is not allowed for a pointer to"
3046 : : " structure or union with flexible array member");
3047 : 2 : return;
3048 : : }
3049 : : }
3050 : : }
3051 : :
3052 : : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3053 : : to the object that represents its counted_by per the attribute
3054 : : counted_by attached to this field if it's a flexible array member
3055 : : or a pointer field, otherwise return NULL_TREE.
3056 : : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3057 : : For example, if:
3058 : :
3059 : : struct P {
3060 : : int k;
3061 : : int x[] __attribute__ ((counted_by (k)));
3062 : : } *p;
3063 : :
3064 : : for:
3065 : : p->x
3066 : :
3067 : : the ref to the object that represents its element count will be:
3068 : :
3069 : : &(p->k)
3070 : : */
3071 : :
3072 : : static tree
3073 : 492636 : build_counted_by_ref (tree datum, tree subdatum,
3074 : : tree *counted_by_type)
3075 : : {
3076 : 492636 : tree type = TREE_TYPE (datum);
3077 : 492636 : tree sub_type = TREE_TYPE (subdatum);
3078 : 492636 : if (!c_flexible_array_member_type_p (sub_type)
3079 : 492636 : && TREE_CODE (sub_type) != POINTER_TYPE)
3080 : : return NULL_TREE;
3081 : :
3082 : 492636 : tree attr_counted_by = lookup_attribute ("counted_by",
3083 : 492636 : DECL_ATTRIBUTES (subdatum));
3084 : 492636 : tree counted_by_ref = NULL_TREE;
3085 : 492636 : *counted_by_type = NULL_TREE;
3086 : 492636 : if (attr_counted_by)
3087 : : {
3088 : 298 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3089 : 298 : counted_by_ref
3090 : 298 : = build_component_ref (UNKNOWN_LOCATION,
3091 : : datum, field_id,
3092 : : UNKNOWN_LOCATION, UNKNOWN_LOCATION);
3093 : 298 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3094 : :
3095 : : /* Get the TYPE of the counted_by field. */
3096 : 298 : tree counted_by_field = lookup_field (type, field_id);
3097 : 298 : gcc_assert (counted_by_field);
3098 : :
3099 : 392 : do
3100 : : {
3101 : 392 : *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field));
3102 : 392 : counted_by_field = TREE_CHAIN (counted_by_field);
3103 : : }
3104 : 392 : while (counted_by_field);
3105 : : }
3106 : : return counted_by_ref;
3107 : : }
3108 : :
3109 : : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3110 : : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3111 : : call to the internal function .ACCESS_WITH_SIZE.
3112 : :
3113 : : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3114 : : the internal function .ACCESS_WITH_SIZE.
3115 : :
3116 : : REF
3117 : :
3118 : : to:
3119 : :
3120 : : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3121 : : TYPE_SIZE_UNIT for element)
3122 : :
3123 : : NOTE: The return type of this function is the POINTER type pointing
3124 : : to the original flexible array type. Then the type of the INDIRECT_REF
3125 : : is the original flexible array type.
3126 : : The type of the first argument of this function is a POINTER type
3127 : : to the original flexible array type.
3128 : :
3129 : : B: For pointers with counted_by, generate a call to the internal function
3130 : : .ACCESS_WITH_SIZE.
3131 : :
3132 : : REF
3133 : :
3134 : : to:
3135 : :
3136 : : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3137 : : TYPE_SIZE_UNIT for element)
3138 : :
3139 : : NOTE: The return type of this function is the original pointer type.
3140 : : The type of the first argument of this function is the original
3141 : : pointer type.
3142 : :
3143 : : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3144 : : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3145 : :
3146 : : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3147 : : of the array.
3148 : :
3149 : : */
3150 : : static tree
3151 : 298 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3152 : : tree counted_by_ref,
3153 : : tree counted_by_type)
3154 : : {
3155 : 298 : gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))
3156 : : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3157 : :
3158 : 298 : bool is_fam = c_flexible_array_member_type_p (TREE_TYPE (ref));
3159 : :
3160 : : /* The result type of the call is a pointer to the flexible array type;
3161 : : or is the original ponter type to the pointer field with counted_by. */
3162 : 298 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3163 : 144 : : TREE_TYPE (ref);
3164 : :
3165 : 298 : tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref)));
3166 : :
3167 : 298 : tree first_param = is_fam
3168 : 298 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3169 : : false, NULL)
3170 : : : ref;
3171 : 298 : tree second_param
3172 : 298 : = c_fully_fold (counted_by_ref, false, NULL);
3173 : 298 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3174 : :
3175 : 298 : tree call
3176 : 298 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3177 : : result_type, 4,
3178 : : first_param,
3179 : : second_param,
3180 : : third_param,
3181 : : element_size);
3182 : :
3183 : : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3184 : 298 : if (is_fam)
3185 : 154 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3186 : 298 : SET_EXPR_LOCATION (call, loc);
3187 : 298 : return call;
3188 : : }
3189 : :
3190 : : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3191 : : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3192 : : function .ACCESS_WITH_SIZE.
3193 : : Otherwise, return the ref itself. */
3194 : :
3195 : : tree
3196 : 2223147 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3197 : : {
3198 : 2223147 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3199 : 2223147 : tree datum = TREE_OPERAND (ref, 0);
3200 : 2223147 : tree subdatum = TREE_OPERAND (ref, 1);
3201 : 2223147 : tree counted_by_type = NULL_TREE;
3202 : :
3203 : 2223147 : if (!(c_flexible_array_member_type_p (TREE_TYPE (ref))
3204 : 2152935 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3205 : : return ref;
3206 : :
3207 : 492636 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3208 : : &counted_by_type);
3209 : 492636 : if (counted_by_ref)
3210 : 298 : ref = build_access_with_size_for_counted_by (loc, ref,
3211 : : counted_by_ref,
3212 : : counted_by_type);
3213 : : return ref;
3214 : : }
3215 : :
3216 : : /* Make an expression to refer to the COMPONENT field of structure or
3217 : : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3218 : : location of the COMPONENT_REF. COMPONENT_LOC is the location
3219 : : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3220 : : it is from -> operator.
3221 : : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3222 : : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3223 : :
3224 : : tree
3225 : 2226434 : build_component_ref (location_t loc, tree datum, tree component,
3226 : : location_t component_loc, location_t arrow_loc)
3227 : : {
3228 : 2226434 : tree type = TREE_TYPE (datum);
3229 : 2226434 : enum tree_code code = TREE_CODE (type);
3230 : 2226434 : tree field = NULL;
3231 : 2226434 : tree ref;
3232 : 2226434 : bool datum_lvalue = lvalue_p (datum);
3233 : :
3234 : 2226434 : if (!objc_is_public (datum, component))
3235 : 0 : return error_mark_node;
3236 : :
3237 : : /* Detect Objective-C property syntax object.property. */
3238 : 2226434 : if (c_dialect_objc ()
3239 : 2226434 : && (ref = objc_maybe_build_component_ref (datum, component)))
3240 : : return ref;
3241 : :
3242 : : /* See if there is a field or component with name COMPONENT. */
3243 : :
3244 : 2226434 : if (code == RECORD_TYPE || code == UNION_TYPE)
3245 : : {
3246 : 2226362 : if (!COMPLETE_TYPE_P (type))
3247 : : {
3248 : 15 : c_incomplete_type_error (loc, NULL_TREE, type);
3249 : 15 : return error_mark_node;
3250 : : }
3251 : :
3252 : 2226347 : field = lookup_field (type, component);
3253 : :
3254 : 2226347 : if (!field)
3255 : : {
3256 : 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3257 : 63 : if (guessed_id)
3258 : : {
3259 : : /* Attempt to provide a fixit replacement hint, if
3260 : : we have a valid range for the component. */
3261 : 0 : location_t reported_loc
3262 : 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3263 : 26 : gcc_rich_location rich_loc (reported_loc);
3264 : 26 : if (component_loc != UNKNOWN_LOCATION)
3265 : 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3266 : 26 : error_at (&rich_loc,
3267 : : "%qT has no member named %qE; did you mean %qE?",
3268 : : type, component, guessed_id);
3269 : 26 : }
3270 : : else
3271 : 37 : error_at (loc, "%qT has no member named %qE", type, component);
3272 : 63 : return error_mark_node;
3273 : : }
3274 : :
3275 : : /* Accessing elements of atomic structures or unions is undefined
3276 : : behavior (C11 6.5.2.3#5). */
3277 : 2226284 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3278 : : {
3279 : 18 : if (code == RECORD_TYPE)
3280 : 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3281 : : "structure %qE", component, datum);
3282 : : else
3283 : 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3284 : : "union %qE", component, datum);
3285 : : }
3286 : :
3287 : : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3288 : : This might be better solved in future the way the C++ front
3289 : : end does it - by giving the anonymous entities each a
3290 : : separate name and type, and then have build_component_ref
3291 : : recursively call itself. We can't do that here. */
3292 : 2228437 : do
3293 : : {
3294 : 2228437 : tree subdatum = TREE_VALUE (field);
3295 : 2228437 : int quals;
3296 : 2228437 : tree subtype;
3297 : 2228437 : bool use_datum_quals;
3298 : :
3299 : 2228437 : if (TREE_TYPE (subdatum) == error_mark_node)
3300 : : return error_mark_node;
3301 : :
3302 : : /* If this is an rvalue, it does not have qualifiers in C
3303 : : standard terms and we must avoid propagating such
3304 : : qualifiers down to a non-lvalue array that is then
3305 : : converted to a pointer. */
3306 : 4456874 : use_datum_quals = (datum_lvalue
3307 : 2228437 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3308 : :
3309 : 2228437 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3310 : 2228437 : if (use_datum_quals)
3311 : 2228175 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3312 : 2228437 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3313 : :
3314 : 2228437 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3315 : : NULL_TREE);
3316 : 2228437 : SET_EXPR_LOCATION (ref, loc);
3317 : :
3318 : 2228437 : check_counted_by_attribute (loc, ref);
3319 : :
3320 : 2228437 : if (TREE_READONLY (subdatum)
3321 : 2228437 : || (use_datum_quals && TREE_READONLY (datum)))
3322 : 30378 : TREE_READONLY (ref) = 1;
3323 : 2228437 : if (TREE_THIS_VOLATILE (subdatum)
3324 : 2227551 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3325 : 2320 : TREE_THIS_VOLATILE (ref) = 1;
3326 : :
3327 : 2228437 : if (TREE_UNAVAILABLE (subdatum))
3328 : 10 : error_unavailable_use (subdatum, NULL_TREE);
3329 : 2228427 : else if (TREE_DEPRECATED (subdatum))
3330 : 16 : warn_deprecated_use (subdatum, NULL_TREE);
3331 : :
3332 : 2228437 : datum = ref;
3333 : :
3334 : 2228437 : field = TREE_CHAIN (field);
3335 : : }
3336 : 2228437 : while (field);
3337 : :
3338 : : return ref;
3339 : : }
3340 : 72 : else if (should_suggest_deref_p (type))
3341 : : {
3342 : : /* Special-case the error message for "ptr.field" for the case
3343 : : where the user has confused "." vs "->". */
3344 : 7 : rich_location richloc (line_table, loc);
3345 : 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3346 : : {
3347 : 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3348 : 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3349 : 2 : error_at (&richloc,
3350 : : "%qE is a pointer to pointer; did you mean to dereference "
3351 : : "it before applying %<->%> to it?",
3352 : 2 : TREE_OPERAND (datum, 0));
3353 : : }
3354 : : else
3355 : : {
3356 : : /* "loc" should be the "." token. */
3357 : 5 : richloc.add_fixit_replace ("->");
3358 : 5 : error_at (&richloc,
3359 : : "%qE is a pointer; did you mean to use %<->%>?",
3360 : : datum);
3361 : : }
3362 : 7 : return error_mark_node;
3363 : 7 : }
3364 : 65 : else if (code != ERROR_MARK)
3365 : 1 : error_at (loc,
3366 : : "request for member %qE in something not a structure or union",
3367 : : component);
3368 : :
3369 : 65 : return error_mark_node;
3370 : : }
3371 : :
3372 : : /* Given an expression PTR for a pointer, return an expression
3373 : : for the value pointed to.
3374 : : ERRORSTRING is the name of the operator to appear in error messages.
3375 : :
3376 : : LOC is the location to use for the generated tree. */
3377 : :
3378 : : tree
3379 : 2680606 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3380 : : {
3381 : 2680606 : tree pointer = default_conversion (ptr);
3382 : 2680606 : tree type = TREE_TYPE (pointer);
3383 : 2680606 : tree ref;
3384 : :
3385 : 2680606 : if (TREE_CODE (type) == POINTER_TYPE)
3386 : : {
3387 : 2680255 : if (CONVERT_EXPR_P (pointer)
3388 : 1911494 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3389 : : {
3390 : : /* If a warning is issued, mark it to avoid duplicates from
3391 : : the backend. This only needs to be done at
3392 : : warn_strict_aliasing > 2. */
3393 : 768761 : if (warn_strict_aliasing > 2)
3394 : 334841 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3395 : 334841 : type, TREE_OPERAND (pointer, 0)))
3396 : 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3397 : : }
3398 : :
3399 : 2680255 : if (TREE_CODE (pointer) == ADDR_EXPR
3400 : 2680255 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3401 : 67316 : == TREE_TYPE (type)))
3402 : : {
3403 : 67307 : ref = TREE_OPERAND (pointer, 0);
3404 : 67307 : protected_set_expr_location (ref, loc);
3405 : 67307 : return ref;
3406 : : }
3407 : : else
3408 : : {
3409 : 2612948 : tree t = TREE_TYPE (type);
3410 : :
3411 : 2612948 : ref = build1 (INDIRECT_REF, t, pointer);
3412 : :
3413 : 2612948 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3414 : 147 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3415 : :
3416 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3417 : : so that we get the proper error message if the result is used
3418 : : to assign to. Also, &* is supposed to be a no-op.
3419 : : And ANSI C seems to specify that the type of the result
3420 : : should be the const type. */
3421 : : /* A de-reference of a pointer to const is not a const. It is valid
3422 : : to change it via some other pointer. */
3423 : 2612948 : TREE_READONLY (ref) = TYPE_READONLY (t);
3424 : 2612948 : TREE_SIDE_EFFECTS (ref)
3425 : 2612948 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3426 : 2612948 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3427 : 2612948 : protected_set_expr_location (ref, loc);
3428 : 2612948 : return ref;
3429 : : }
3430 : : }
3431 : 351 : else if (TREE_CODE (pointer) != ERROR_MARK)
3432 : 278 : invalid_indirection_error (loc, type, errstring);
3433 : :
3434 : 351 : return error_mark_node;
3435 : : }
3436 : :
3437 : : /* This handles expressions of the form "a[i]", which denotes
3438 : : an array reference.
3439 : :
3440 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3441 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3442 : : This avoids forcing the array out of registers, and can work on
3443 : : arrays that are not lvalues (for example, members of structures returned
3444 : : by functions).
3445 : :
3446 : : For vector types, allow vector[i] but not i[vector], and create
3447 : : *(((type*)&vectortype) + i) for the expression.
3448 : :
3449 : : LOC is the location to use for the returned expression. */
3450 : :
3451 : : tree
3452 : 3395724 : build_array_ref (location_t loc, tree array, tree index)
3453 : : {
3454 : 3395724 : tree ret;
3455 : 3395724 : bool swapped = false;
3456 : 3395724 : if (TREE_TYPE (array) == error_mark_node
3457 : 3395724 : || TREE_TYPE (index) == error_mark_node)
3458 : : return error_mark_node;
3459 : :
3460 : 3395616 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3461 : 1939895 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3462 : : /* Allow vector[index] but not index[vector]. */
3463 : 4393455 : && !gnu_vector_type_p (TREE_TYPE (array)))
3464 : : {
3465 : 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3466 : 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3467 : : {
3468 : 2 : error_at (loc,
3469 : : "subscripted value is neither array nor pointer nor vector");
3470 : :
3471 : 2 : return error_mark_node;
3472 : : }
3473 : 192 : std::swap (array, index);
3474 : 192 : swapped = true;
3475 : : }
3476 : :
3477 : 3395614 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3478 : : {
3479 : 2 : error_at (loc, "array subscript is not an integer");
3480 : 2 : return error_mark_node;
3481 : : }
3482 : :
3483 : 3395612 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3484 : : {
3485 : 10 : error_at (loc, "subscripted value is pointer to function");
3486 : 10 : return error_mark_node;
3487 : : }
3488 : :
3489 : : /* ??? Existing practice has been to warn only when the char
3490 : : index is syntactically the index, not for char[array]. */
3491 : 3395602 : if (!swapped)
3492 : 3395416 : warn_array_subscript_with_type_char (loc, index);
3493 : :
3494 : : /* Apply default promotions *after* noticing character types. */
3495 : 3395602 : index = default_conversion (index);
3496 : 3395602 : if (index == error_mark_node)
3497 : : return error_mark_node;
3498 : :
3499 : 3395601 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3500 : : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3501 : :
3502 : 3395601 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3503 : 3395601 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3504 : :
3505 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3506 : 3395601 : if (TREE_CODE (array) == COMPONENT_REF
3507 : 3395601 : && handle_counted_by_p (array))
3508 : 800782 : array = handle_counted_by_for_component_ref (loc, array);
3509 : :
3510 : 3395601 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3511 : : {
3512 : 2453373 : tree rval, type;
3513 : :
3514 : : /* An array that is indexed by a non-constant
3515 : : cannot be stored in a register; we must be able to do
3516 : : address arithmetic on its address.
3517 : : Likewise an array of elements of variable size. */
3518 : 2453373 : if (TREE_CODE (index) != INTEGER_CST
3519 : 2453373 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3520 : 1990788 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3521 : : {
3522 : 463322 : if (!c_mark_addressable (array, true))
3523 : 1 : return error_mark_node;
3524 : : }
3525 : : /* An array that is indexed by a constant value which is not within
3526 : : the array bounds cannot be stored in a register either; because we
3527 : : would get a crash in store_bit_field/extract_bit_field when trying
3528 : : to access a non-existent part of the register. */
3529 : 2453372 : if (TREE_CODE (index) == INTEGER_CST
3530 : 1990788 : && TYPE_DOMAIN (TREE_TYPE (array))
3531 : 4440804 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3532 : : {
3533 : 60556 : if (!c_mark_addressable (array))
3534 : 0 : return error_mark_node;
3535 : : }
3536 : :
3537 : 2453372 : if ((pedantic || warn_c90_c99_compat)
3538 : 2430512 : && ! was_vector)
3539 : : {
3540 : 1455015 : tree foo = array;
3541 : 2274952 : while (TREE_CODE (foo) == COMPONENT_REF)
3542 : 819937 : foo = TREE_OPERAND (foo, 0);
3543 : 1455015 : if (VAR_P (foo) && C_DECL_REGISTER (foo))
3544 : 0 : pedwarn (loc, OPT_Wpedantic,
3545 : : "ISO C forbids subscripting %<register%> array");
3546 : 1455015 : else if (!lvalue_p (foo))
3547 : 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3548 : : "ISO C90 forbids subscripting non-lvalue "
3549 : : "array");
3550 : : }
3551 : :
3552 : 2453372 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3553 : 2453372 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3554 : 3 : index = fold_convert (sizetype, index);
3555 : :
3556 : 2453372 : type = TREE_TYPE (TREE_TYPE (array));
3557 : 2453372 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3558 : : /* Array ref is const/volatile if the array elements are
3559 : : or if the array is. */
3560 : 7360116 : TREE_READONLY (rval)
3561 : 2453372 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3562 : 2453372 : | TREE_READONLY (array));
3563 : 7360116 : TREE_SIDE_EFFECTS (rval)
3564 : 2453372 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3565 : 2453372 : | TREE_SIDE_EFFECTS (array));
3566 : 4906744 : TREE_THIS_VOLATILE (rval)
3567 : 2453372 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3568 : : /* This was added by rms on 16 Nov 91.
3569 : : It fixes vol struct foo *a; a->elts[1]
3570 : : in an inline function.
3571 : : Hope it doesn't break something else. */
3572 : 2453372 : | TREE_THIS_VOLATILE (array));
3573 : 2453372 : ret = require_complete_type (loc, rval);
3574 : 2453372 : protected_set_expr_location (ret, loc);
3575 : 2453372 : if (non_lvalue)
3576 : 102872 : ret = non_lvalue_loc (loc, ret);
3577 : 2453372 : return ret;
3578 : : }
3579 : : else
3580 : : {
3581 : 942228 : tree ar = default_conversion (array);
3582 : :
3583 : 942228 : if (ar == error_mark_node)
3584 : : return ar;
3585 : :
3586 : 942228 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3587 : 942228 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3588 : :
3589 : 942228 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3590 : : index, false),
3591 : : RO_ARRAY_INDEXING);
3592 : 942228 : if (non_lvalue)
3593 : 0 : ret = non_lvalue_loc (loc, ret);
3594 : 942228 : return ret;
3595 : : }
3596 : : }
3597 : :
3598 : : /* Build an OpenMP array section reference, creating an exact type for the
3599 : : resulting expression based on the element type and bounds if possible. If
3600 : : we have variable bounds, create an incomplete array type for the result
3601 : : instead. */
3602 : :
3603 : : tree
3604 : 3933 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3605 : : {
3606 : 3933 : tree type = TREE_TYPE (array);
3607 : 3933 : gcc_assert (type);
3608 : :
3609 : 3933 : tree sectype, eltype = TREE_TYPE (type);
3610 : :
3611 : : /* It's not an array or pointer type. Just reuse the type of the original
3612 : : expression as the type of the array section (an error will be raised
3613 : : anyway, later). */
3614 : 3933 : if (eltype == NULL_TREE || error_operand_p (eltype))
3615 : 12 : sectype = TREE_TYPE (array);
3616 : : else
3617 : : {
3618 : 3921 : tree idxtype = NULL_TREE;
3619 : :
3620 : 3921 : if (index != NULL_TREE
3621 : 3921 : && length != NULL_TREE
3622 : 2996 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3623 : 6912 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3624 : : {
3625 : 2984 : tree low = fold_convert (sizetype, index);
3626 : 2984 : tree high = fold_convert (sizetype, length);
3627 : 2984 : high = size_binop (PLUS_EXPR, low, high);
3628 : 2984 : high = size_binop (MINUS_EXPR, high, size_one_node);
3629 : 2984 : idxtype = build_range_type (sizetype, low, high);
3630 : : }
3631 : 135 : else if ((index == NULL_TREE || integer_zerop (index))
3632 : 827 : && length != NULL_TREE
3633 : 1612 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3634 : 658 : idxtype = build_index_type (length);
3635 : :
3636 : 3921 : gcc_assert (!error_operand_p (idxtype));
3637 : :
3638 : 3921 : sectype = c_build_array_type (eltype, idxtype);
3639 : : }
3640 : :
3641 : 3933 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3642 : : }
3643 : :
3644 : :
3645 : : /* Build an external reference to identifier ID. FUN indicates
3646 : : whether this will be used for a function call. LOC is the source
3647 : : location of the identifier. This sets *TYPE to the type of the
3648 : : identifier, which is not the same as the type of the returned value
3649 : : for CONST_DECLs defined as enum constants. If the type of the
3650 : : identifier is not available, *TYPE is set to NULL. */
3651 : : tree
3652 : 170542513 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3653 : : {
3654 : 170542513 : tree ref;
3655 : 170542513 : tree decl = lookup_name (id);
3656 : :
3657 : : /* In Objective-C, an instance variable (ivar) may be preferred to
3658 : : whatever lookup_name() found. */
3659 : 170542513 : decl = objc_lookup_ivar (decl, id);
3660 : :
3661 : 170542513 : *type = NULL;
3662 : 170542513 : if (decl && decl != error_mark_node)
3663 : : {
3664 : 170536142 : ref = decl;
3665 : 170536142 : *type = TREE_TYPE (ref);
3666 : 170536142 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3667 : 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3668 : : decl);
3669 : : }
3670 : 6371 : else if (fun)
3671 : : /* Implicit function declaration. */
3672 : 4729 : ref = implicitly_declare (loc, id);
3673 : 1642 : else if (decl == error_mark_node)
3674 : : /* Don't complain about something that's already been
3675 : : complained about. */
3676 : : return error_mark_node;
3677 : : else
3678 : : {
3679 : 1412 : undeclared_variable (loc, id);
3680 : 1412 : return error_mark_node;
3681 : : }
3682 : :
3683 : : /* For an OpenMP map clause, we can get better diagnostics for decls with
3684 : : unmappable types if we return the decl with an error_mark_node type,
3685 : : rather than returning error_mark_node for the decl itself. */
3686 : 170540871 : if (TREE_TYPE (ref) == error_mark_node
3687 : 170540871 : && !c_omp_array_section_p)
3688 : : return error_mark_node;
3689 : :
3690 : 170540854 : if (TREE_UNAVAILABLE (ref))
3691 : 14 : error_unavailable_use (ref, NULL_TREE);
3692 : 170540840 : else if (TREE_DEPRECATED (ref))
3693 : 49 : warn_deprecated_use (ref, NULL_TREE);
3694 : :
3695 : : /* Recursive call does not count as usage. */
3696 : 170540854 : if (ref != current_function_decl)
3697 : : {
3698 : 170538243 : TREE_USED (ref) = 1;
3699 : : }
3700 : :
3701 : 170540854 : if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
3702 : : {
3703 : 50011387 : if (!in_sizeof && !in_typeof && !in_countof)
3704 : 49386758 : C_DECL_USED (ref) = 1;
3705 : 624629 : else if (DECL_INITIAL (ref) == NULL_TREE
3706 : 618509 : && DECL_EXTERNAL (ref)
3707 : 1243137 : && !TREE_PUBLIC (ref))
3708 : 100 : record_maybe_used_decl (ref);
3709 : : }
3710 : :
3711 : 170540854 : if (TREE_CODE (ref) == CONST_DECL)
3712 : : {
3713 : 411144 : used_types_insert (TREE_TYPE (ref));
3714 : :
3715 : 411144 : if (warn_cxx_compat
3716 : 377 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3717 : 411519 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3718 : : {
3719 : 1 : warning_at (loc, OPT_Wc___compat,
3720 : : ("enum constant defined in struct or union "
3721 : : "is not visible in C++"));
3722 : 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3723 : : }
3724 : :
3725 : 411144 : ref = DECL_INITIAL (ref);
3726 : 411144 : TREE_CONSTANT (ref) = 1;
3727 : : }
3728 : 170129710 : else if (current_function_decl != NULL_TREE
3729 : 168633975 : && !DECL_FILE_SCOPE_P (current_function_decl)
3730 : 170129710 : && (VAR_OR_FUNCTION_DECL_P (ref)
3731 : : || TREE_CODE (ref) == PARM_DECL))
3732 : : {
3733 : 10083 : tree context = decl_function_context (ref);
3734 : :
3735 : 10083 : if (context != NULL_TREE && context != current_function_decl)
3736 : 1612 : DECL_NONLOCAL (ref) = 1;
3737 : : }
3738 : : /* C99 6.7.4p3: An inline definition of a function with external
3739 : : linkage ... shall not contain a reference to an identifier with
3740 : : internal linkage. */
3741 : 170119627 : else if (current_function_decl != NULL_TREE
3742 : 168623892 : && DECL_DECLARED_INLINE_P (current_function_decl)
3743 : 155231312 : && DECL_EXTERNAL (current_function_decl)
3744 : 154475850 : && VAR_OR_FUNCTION_DECL_P (ref)
3745 : 55042008 : && (!VAR_P (ref) || TREE_STATIC (ref))
3746 : 46117050 : && ! TREE_PUBLIC (ref)
3747 : 170119661 : && DECL_CONTEXT (ref) != current_function_decl)
3748 : 25 : record_inline_static (loc, current_function_decl, ref,
3749 : : csi_internal);
3750 : :
3751 : : return ref;
3752 : : }
3753 : :
3754 : : /* Record details of decls possibly used inside sizeof or typeof. */
3755 : : struct maybe_used_decl
3756 : : {
3757 : : /* The decl. */
3758 : : tree decl;
3759 : : /* The level seen at (in_sizeof + in_typeof + in_countof). */
3760 : : int level;
3761 : : /* The next one at this level or above, or NULL. */
3762 : : struct maybe_used_decl *next;
3763 : : };
3764 : :
3765 : : static struct maybe_used_decl *maybe_used_decls;
3766 : :
3767 : : /* Record that DECL, an undefined static function reference seen
3768 : : inside sizeof or typeof, might be used if the operand of sizeof is
3769 : : a VLA type or the operand of typeof is a variably modified
3770 : : type. */
3771 : :
3772 : : static void
3773 : 100 : record_maybe_used_decl (tree decl)
3774 : : {
3775 : 100 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3776 : 100 : t->decl = decl;
3777 : 100 : t->level = in_sizeof + in_typeof + in_countof;
3778 : 100 : t->next = maybe_used_decls;
3779 : 100 : maybe_used_decls = t;
3780 : 100 : }
3781 : :
3782 : : /* Pop the stack of decls possibly used inside sizeof or typeof. If
3783 : : USED is false, just discard them. If it is true, mark them used
3784 : : (if no longer inside sizeof or typeof) or move them to the next
3785 : : level up (if still inside sizeof or typeof). */
3786 : :
3787 : : void
3788 : 1439641 : pop_maybe_used (bool used)
3789 : : {
3790 : 1439641 : struct maybe_used_decl *p = maybe_used_decls;
3791 : 1439641 : int cur_level = in_sizeof + in_typeof + in_countof;
3792 : 1439744 : while (p && p->level > cur_level)
3793 : : {
3794 : 103 : if (used)
3795 : : {
3796 : 13 : if (cur_level == 0)
3797 : 10 : C_DECL_USED (p->decl) = 1;
3798 : : else
3799 : 3 : p->level = cur_level;
3800 : : }
3801 : 103 : p = p->next;
3802 : : }
3803 : 1439641 : if (!used || cur_level == 0)
3804 : 1439625 : maybe_used_decls = p;
3805 : 1439641 : }
3806 : :
3807 : : /* Return the result of sizeof applied to EXPR. */
3808 : :
3809 : : struct c_expr
3810 : 302383 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3811 : : {
3812 : 302383 : struct c_expr ret;
3813 : 302383 : if (expr.value == error_mark_node)
3814 : : {
3815 : 109 : ret.value = error_mark_node;
3816 : 109 : ret.original_code = ERROR_MARK;
3817 : 109 : ret.original_type = NULL;
3818 : 109 : ret.m_decimal = 0;
3819 : 109 : pop_maybe_used (false);
3820 : : }
3821 : : else
3822 : : {
3823 : 302274 : bool expr_const_operands = true;
3824 : :
3825 : 302274 : if (TREE_CODE (expr.value) == PARM_DECL
3826 : 302274 : && C_ARRAY_PARAMETER (expr.value))
3827 : : {
3828 : 55 : auto_diagnostic_group d;
3829 : 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
3830 : : "%<sizeof%> on array function parameter %qE will "
3831 : : "return size of %qT", expr.value,
3832 : 55 : TREE_TYPE (expr.value)))
3833 : 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3834 : 55 : }
3835 : 302274 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3836 : : &expr_const_operands);
3837 : 302274 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3838 : 302274 : c_last_sizeof_arg = expr.value;
3839 : 302274 : c_last_sizeof_loc = loc;
3840 : 302274 : ret.original_code = SIZEOF_EXPR;
3841 : 302274 : ret.original_type = NULL;
3842 : 302274 : ret.m_decimal = 0;
3843 : 302274 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3844 : : {
3845 : : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3846 : 281 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3847 : : folded_expr, ret.value);
3848 : 281 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3849 : 281 : SET_EXPR_LOCATION (ret.value, loc);
3850 : : }
3851 : 302274 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3852 : : }
3853 : 302383 : return ret;
3854 : : }
3855 : :
3856 : : /* Return the result of sizeof applied to T, a structure for the type
3857 : : name passed to sizeof (rather than the type itself). LOC is the
3858 : : location of the original expression. */
3859 : :
3860 : : struct c_expr
3861 : 330096 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3862 : : {
3863 : 330096 : tree type;
3864 : 330096 : struct c_expr ret;
3865 : 330096 : tree type_expr = NULL_TREE;
3866 : 330096 : bool type_expr_const = true;
3867 : 330096 : type = groktypename (t, &type_expr, &type_expr_const);
3868 : 330096 : ret.value = c_sizeof (loc, type);
3869 : 330096 : c_last_sizeof_arg = type;
3870 : 330096 : c_last_sizeof_loc = loc;
3871 : 330096 : ret.original_code = SIZEOF_EXPR;
3872 : 330096 : ret.original_type = NULL;
3873 : 330096 : ret.m_decimal = 0;
3874 : 330096 : if (type == error_mark_node)
3875 : : {
3876 : 5 : ret.value = error_mark_node;
3877 : 5 : ret.original_code = ERROR_MARK;
3878 : : }
3879 : : else
3880 : 330049 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3881 : 660058 : && C_TYPE_VARIABLE_SIZE (type))
3882 : : {
3883 : : /* If the type is a [*] array, it is a VLA but is represented as
3884 : : having a size of zero. In such a case we must ensure that
3885 : : the result of sizeof does not get folded to a constant by
3886 : : c_fully_fold, because if the size is evaluated the result is
3887 : : not constant and so constraints on zero or negative size
3888 : : arrays must not be applied when this sizeof call is inside
3889 : : another array declarator. */
3890 : 41 : if (!type_expr)
3891 : 16 : type_expr = integer_zero_node;
3892 : 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3893 : : type_expr, ret.value);
3894 : 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3895 : : }
3896 : 330096 : pop_maybe_used (type != error_mark_node
3897 : 330096 : ? C_TYPE_VARIABLE_SIZE (type) : false);
3898 : 330096 : return ret;
3899 : : }
3900 : :
3901 : : static bool
3902 : 136 : is_top_array_vla (tree type)
3903 : : {
3904 : 136 : bool zero, var;
3905 : 136 : tree d;
3906 : :
3907 : 136 : if (TREE_CODE (type) != ARRAY_TYPE)
3908 : : return false;
3909 : 118 : if (!COMPLETE_TYPE_P (type))
3910 : : return false;
3911 : :
3912 : 114 : d = TYPE_DOMAIN (type);
3913 : 114 : zero = !TYPE_MAX_VALUE (d);
3914 : 114 : if (zero)
3915 : : return false;
3916 : :
3917 : 98 : var = (TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST
3918 : 98 : || TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST);
3919 : : return var;
3920 : : }
3921 : :
3922 : : /* Return the result of countof applied to EXPR. */
3923 : :
3924 : : struct c_expr
3925 : 53 : c_expr_countof_expr (location_t loc, struct c_expr expr)
3926 : : {
3927 : 53 : struct c_expr ret;
3928 : 53 : if (expr.value == error_mark_node)
3929 : : {
3930 : 1 : ret.value = error_mark_node;
3931 : 1 : ret.original_code = ERROR_MARK;
3932 : 1 : ret.original_type = NULL;
3933 : 1 : ret.m_decimal = 0;
3934 : 1 : pop_maybe_used (false);
3935 : : }
3936 : : else
3937 : : {
3938 : 52 : bool expr_const_operands = true;
3939 : :
3940 : 52 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3941 : : &expr_const_operands);
3942 : 52 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
3943 : 52 : c_last_sizeof_arg = expr.value;
3944 : 52 : c_last_sizeof_loc = loc;
3945 : 52 : ret.original_code = COUNTOF_EXPR;
3946 : 52 : ret.original_type = NULL;
3947 : 52 : ret.m_decimal = 0;
3948 : 52 : if (is_top_array_vla (TREE_TYPE (folded_expr)))
3949 : : {
3950 : : /* countof is evaluated when given a vla. */
3951 : 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3952 : : folded_expr, ret.value);
3953 : 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3954 : 11 : SET_EXPR_LOCATION (ret.value, loc);
3955 : : }
3956 : 52 : pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
3957 : : }
3958 : 53 : return ret;
3959 : : }
3960 : :
3961 : : /* Return the result of countof applied to T, a structure for the type
3962 : : name passed to countof (rather than the type itself). LOC is the
3963 : : location of the original expression. */
3964 : :
3965 : : struct c_expr
3966 : 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
3967 : : {
3968 : 18 : tree type;
3969 : 18 : struct c_expr ret;
3970 : 18 : tree type_expr = NULL_TREE;
3971 : 18 : bool type_expr_const = true;
3972 : 18 : type = groktypename (t, &type_expr, &type_expr_const);
3973 : 18 : ret.value = c_countof_type (loc, type);
3974 : 18 : c_last_sizeof_arg = type;
3975 : 18 : c_last_sizeof_loc = loc;
3976 : 18 : ret.original_code = COUNTOF_EXPR;
3977 : 18 : ret.original_type = NULL;
3978 : 18 : ret.m_decimal = 0;
3979 : 18 : if (type == error_mark_node)
3980 : : {
3981 : 0 : ret.value = error_mark_node;
3982 : 0 : ret.original_code = ERROR_MARK;
3983 : : }
3984 : : else
3985 : 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3986 : 22 : && is_top_array_vla (type))
3987 : : {
3988 : : /* If the type is a [*] array, it is a VLA but is represented as
3989 : : having a size of zero. In such a case we must ensure that
3990 : : the result of countof does not get folded to a constant by
3991 : : c_fully_fold, because if the number of elements is evaluated
3992 : : the result is not constant and so
3993 : : constraints on zero or negative size arrays must not be applied
3994 : : when this countof call is inside another array declarator. */
3995 : 6 : if (!type_expr)
3996 : 0 : type_expr = integer_zero_node;
3997 : 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3998 : : type_expr, ret.value);
3999 : 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4000 : : }
4001 : 18 : pop_maybe_used (type != error_mark_node ? is_top_array_vla (type) : false);
4002 : 18 : return ret;
4003 : : }
4004 : :
4005 : : /* Build a function call to function FUNCTION with parameters PARAMS.
4006 : : The function call is at LOC.
4007 : : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4008 : : TREE_VALUE of each node is a parameter-expression.
4009 : : FUNCTION's data type may be a function type or a pointer-to-function. */
4010 : :
4011 : : tree
4012 : 0 : build_function_call (location_t loc, tree function, tree params)
4013 : : {
4014 : 0 : vec<tree, va_gc> *v;
4015 : 0 : tree ret;
4016 : :
4017 : 0 : vec_alloc (v, list_length (params));
4018 : 0 : for (; params; params = TREE_CHAIN (params))
4019 : 0 : v->quick_push (TREE_VALUE (params));
4020 : 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4021 : 0 : vec_free (v);
4022 : 0 : return ret;
4023 : : }
4024 : :
4025 : : /* Give a note about the location of the declaration of DECL,
4026 : : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4027 : :
4028 : : static void
4029 : 299 : inform_declaration (tree decl, tree function_expr)
4030 : : {
4031 : 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4032 : 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4033 : 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4034 : 92 : else if (function_expr)
4035 : 92 : switch (TREE_CODE (function_expr))
4036 : : {
4037 : : default:
4038 : : break;
4039 : 13 : case COMPONENT_REF:
4040 : : /* Show the decl of the pertinent field (e.g. for callback
4041 : : fields in a struct. */
4042 : 13 : {
4043 : 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4044 : 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4045 : 13 : inform (loc, "declared here");
4046 : : }
4047 : : break;
4048 : : }
4049 : 299 : }
4050 : :
4051 : : /* C implementation of callback for use when checking param types. */
4052 : :
4053 : : static bool
4054 : 2 : comp_parm_types (tree wanted_type, tree actual_type)
4055 : : {
4056 : 2 : return comptypes (wanted_type, actual_type);
4057 : : }
4058 : :
4059 : : /* Build a function call to function FUNCTION with parameters PARAMS.
4060 : : If FUNCTION is the result of resolving an overloaded target built-in,
4061 : : ORIG_FUNDECL is the original function decl, otherwise it is null.
4062 : : ORIGTYPES, if not NULL, is a vector of types; each element is
4063 : : either NULL or the original type of the corresponding element in
4064 : : PARAMS. The original type may differ from TREE_TYPE of the
4065 : : parameter for enums. FUNCTION's data type may be a function type
4066 : : or pointer-to-function. This function changes the elements of
4067 : : PARAMS. */
4068 : :
4069 : : tree
4070 : 48622488 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4071 : : tree function, vec<tree, va_gc> *params,
4072 : : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4073 : : {
4074 : 48622488 : tree fntype, fundecl = NULL_TREE;
4075 : 48622488 : tree name = NULL_TREE, result;
4076 : 48622488 : tree tem;
4077 : 48622488 : int nargs;
4078 : 48622488 : tree *argarray;
4079 : :
4080 : :
4081 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4082 : 48622488 : STRIP_TYPE_NOPS (function);
4083 : :
4084 : : /* Convert anything with function type to a pointer-to-function. */
4085 : 48622488 : if (TREE_CODE (function) == FUNCTION_DECL)
4086 : : {
4087 : 48545410 : name = DECL_NAME (function);
4088 : :
4089 : 48545410 : if (flag_tm)
4090 : 421 : tm_malloc_replacement (function);
4091 : 48545410 : fundecl = function;
4092 : 48545410 : if (!orig_fundecl)
4093 : 48545410 : orig_fundecl = fundecl;
4094 : : /* Atomic functions have type checking/casting already done. They are
4095 : : often rewritten and don't match the original parameter list. */
4096 : 97090820 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4097 : : origtypes = NULL;
4098 : : }
4099 : 48622488 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4100 : 48559671 : function = function_to_pointer_conversion (loc, function);
4101 : :
4102 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4103 : : expressions, like those used for ObjC messenger dispatches. */
4104 : 48622488 : if (params && !params->is_empty ())
4105 : 37215087 : function = objc_rewrite_function_call (function, (*params)[0]);
4106 : :
4107 : 48622488 : function = c_fully_fold (function, false, NULL);
4108 : :
4109 : 48622488 : fntype = TREE_TYPE (function);
4110 : :
4111 : 48622488 : if (TREE_CODE (fntype) == ERROR_MARK)
4112 : 3 : return error_mark_node;
4113 : :
4114 : 48622485 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4115 : 48622451 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4116 : : {
4117 : 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4118 : 33 : error_at (loc,
4119 : : "called object %qE is not a function or function pointer",
4120 : : function);
4121 : 4 : else if (DECL_P (function))
4122 : : {
4123 : 1 : auto_diagnostic_group d;
4124 : 1 : error_at (loc,
4125 : : "called object %qD is not a function or function pointer",
4126 : : function);
4127 : 1 : inform_declaration (function, NULL_TREE);
4128 : 1 : }
4129 : : else
4130 : 3 : error_at (loc,
4131 : : "called object is not a function or function pointer");
4132 : 37 : return error_mark_node;
4133 : : }
4134 : :
4135 : 48622448 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4136 : 371818 : current_function_returns_abnormally = 1;
4137 : :
4138 : : /* fntype now gets the type of function pointed to. */
4139 : 48622448 : fntype = TREE_TYPE (fntype);
4140 : 48622448 : tree return_type = TREE_TYPE (fntype);
4141 : :
4142 : : /* Convert the parameters to the types declared in the
4143 : : function prototype, or apply default promotions. */
4144 : :
4145 : 48622448 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4146 : : origtypes, function, fundecl);
4147 : 48622448 : if (nargs < 0)
4148 : 178 : return error_mark_node;
4149 : :
4150 : : /* Check that the function is called through a compatible prototype.
4151 : : If it is not, warn. */
4152 : 48621407 : if (CONVERT_EXPR_P (function)
4153 : 879 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4154 : 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4155 : 48622455 : && !comptypes (fntype, TREE_TYPE (tem)))
4156 : : {
4157 : : /* This situation leads to run-time undefined behavior. We can't,
4158 : : therefore, simply error unless we can prove that all possible
4159 : : executions of the program must execute the code. */
4160 : 20 : warning_at (loc, 0, "function called through a non-compatible type");
4161 : :
4162 : 20 : if (VOID_TYPE_P (return_type)
4163 : 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4164 : 1 : pedwarn (loc, 0,
4165 : : "function with qualified void return type called");
4166 : : }
4167 : :
4168 : 48622270 : argarray = vec_safe_address (params);
4169 : :
4170 : : /* Check that arguments to builtin functions match the expectations. */
4171 : 48622270 : if (fundecl
4172 : 48545247 : && fndecl_built_in_p (fundecl)
4173 : 81017060 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4174 : : orig_fundecl, nargs, argarray))
4175 : 298 : return error_mark_node;
4176 : :
4177 : : /* Check that the arguments to the function are valid. */
4178 : 48621972 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4179 : : nargs, argarray, &arg_loc,
4180 : : comp_parm_types);
4181 : :
4182 : 48621972 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4183 : 48621972 : && !VOID_TYPE_P (return_type))
4184 : 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4185 : 48621972 : if (name != NULL_TREE
4186 : 97166921 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4187 : : {
4188 : 31678793 : if (require_constant_value)
4189 : 3488 : result
4190 : 3488 : = fold_build_call_array_initializer_loc (loc, return_type,
4191 : : function, nargs, argarray);
4192 : : else
4193 : 31675305 : result = fold_build_call_array_loc (loc, return_type,
4194 : : function, nargs, argarray);
4195 : 31678793 : if (TREE_CODE (result) == NOP_EXPR
4196 : 31678793 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4197 : 26526 : STRIP_TYPE_NOPS (result);
4198 : : }
4199 : : else
4200 : 16943179 : result = build_call_array_loc (loc, return_type,
4201 : : function, nargs, argarray);
4202 : : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4203 : : later. */
4204 : 48621972 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4205 : 215 : suppress_warning (result, OPT_Wnonnull);
4206 : :
4207 : : /* In this improbable scenario, a nested function returns a VM type.
4208 : : Create a TARGET_EXPR so that the call always has a LHS, much as
4209 : : what the C++ FE does for functions returning non-PODs. */
4210 : 48621972 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
4211 : : {
4212 : 79 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4213 : 79 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4214 : : NULL_TREE, NULL_TREE);
4215 : : }
4216 : :
4217 : 48621972 : if (VOID_TYPE_P (TREE_TYPE (result)))
4218 : : {
4219 : 2349042 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4220 : 2 : pedwarn (loc, 0,
4221 : : "function with qualified void return type called");
4222 : 2349042 : return result;
4223 : : }
4224 : 46272930 : return require_complete_type (loc, result);
4225 : : }
4226 : :
4227 : : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4228 : :
4229 : : tree
4230 : 48622045 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4231 : : tree function, vec<tree, va_gc> *params,
4232 : : vec<tree, va_gc> *origtypes)
4233 : : {
4234 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4235 : 48622056 : STRIP_TYPE_NOPS (function);
4236 : :
4237 : : /* Convert anything with function type to a pointer-to-function. */
4238 : 48622045 : if (TREE_CODE (function) == FUNCTION_DECL)
4239 : : {
4240 : : /* Implement type-directed function overloading for builtins.
4241 : : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4242 : : handle all the type checking. The result is a complete expression
4243 : : that implements this function call. */
4244 : 48544967 : tree tem = resolve_overloaded_builtin (loc, function, params);
4245 : 48544967 : if (tem)
4246 : : return tem;
4247 : : }
4248 : 48536578 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4249 : : }
4250 : :
4251 : : /* Helper for convert_arguments called to convert the VALue of argument
4252 : : number ARGNUM from ORIGTYPE to the corresponding parameter number
4253 : : PARMNUM and TYPE.
4254 : : PLOC is the location where the conversion is being performed.
4255 : : FUNCTION and FUNDECL are the same as in convert_arguments.
4256 : : VALTYPE is the original type of VAL before the conversion and,
4257 : : for EXCESS_PRECISION_EXPR, the operand of the expression.
4258 : : NPC is true if VAL represents the null pointer constant (VAL itself
4259 : : will have been folded to an integer constant).
4260 : : RNAME is the same as FUNCTION except in Objective C when it's
4261 : : the function selector.
4262 : : EXCESS_PRECISION is true when VAL was originally represented
4263 : : as EXCESS_PRECISION_EXPR.
4264 : : WARNOPT is the same as in convert_for_assignment. */
4265 : :
4266 : : static tree
4267 : 122403883 : convert_argument (location_t ploc, tree function, tree fundecl,
4268 : : tree type, tree origtype, tree val, tree valtype,
4269 : : bool npc, tree rname, int parmnum, int argnum,
4270 : : bool excess_precision, int warnopt)
4271 : : {
4272 : : /* Formal parm type is specified by a function prototype. */
4273 : :
4274 : 122403883 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4275 : : {
4276 : 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4277 : : parmnum + 1);
4278 : 3 : return error_mark_node;
4279 : : }
4280 : :
4281 : : /* Optionally warn about conversions that differ from the default
4282 : : conversions. */
4283 : 122403880 : if (warn_traditional_conversion || warn_traditional)
4284 : : {
4285 : 193 : if (INTEGRAL_TYPE_P (type)
4286 : 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4287 : 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4288 : : "passing argument %d of %qE as integer rather "
4289 : : "than floating due to prototype",
4290 : : argnum, rname);
4291 : 193 : if (INTEGRAL_TYPE_P (type)
4292 : 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4293 : 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4294 : : "passing argument %d of %qE as integer rather "
4295 : : "than complex due to prototype",
4296 : : argnum, rname);
4297 : 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4298 : 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4299 : 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4300 : : "passing argument %d of %qE as complex rather "
4301 : : "than floating due to prototype",
4302 : : argnum, rname);
4303 : 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4304 : 71 : && INTEGRAL_TYPE_P (valtype))
4305 : 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4306 : : "passing argument %d of %qE as floating rather "
4307 : : "than integer due to prototype",
4308 : : argnum, rname);
4309 : 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4310 : 7 : && INTEGRAL_TYPE_P (valtype))
4311 : 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4312 : : "passing argument %d of %qE as complex rather "
4313 : : "than integer due to prototype",
4314 : : argnum, rname);
4315 : 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4316 : 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4317 : 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4318 : : "passing argument %d of %qE as floating rather "
4319 : : "than complex due to prototype",
4320 : : argnum, rname);
4321 : : /* ??? At some point, messages should be written about
4322 : : conversions between complex types, but that's too messy
4323 : : to do now. */
4324 : 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4325 : 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4326 : : {
4327 : 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4328 : :
4329 : : /* Warn if any argument is passed as `float',
4330 : : since without a prototype it would be `double'. */
4331 : 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4332 : 45 : && type != dfloat32_type_node)
4333 : 7 : warning_at (ploc, 0,
4334 : : "passing argument %d of %qE as %<float%> "
4335 : : "rather than %<double%> due to prototype",
4336 : : argnum, rname);
4337 : :
4338 : : /* Warn if mismatch between argument and prototype
4339 : : for decimal float types. Warn of conversions with
4340 : : binary float types and of precision narrowing due to
4341 : : prototype. */
4342 : 38 : else if (type != valtype
4343 : 32 : && (type == dfloat32_type_node
4344 : 22 : || type == dfloat64_type_node
4345 : 12 : || type == dfloat128_type_node
4346 : 2 : || valtype == dfloat32_type_node
4347 : 2 : || valtype == dfloat64_type_node
4348 : 2 : || valtype == dfloat128_type_node)
4349 : 38 : && (formal_prec
4350 : 30 : <= TYPE_PRECISION (valtype)
4351 : 14 : || (type == dfloat128_type_node
4352 : 10 : && (valtype
4353 : 10 : != dfloat64_type_node
4354 : 8 : && (valtype
4355 : : != dfloat32_type_node)))
4356 : 8 : || (type == dfloat64_type_node
4357 : 4 : && (valtype
4358 : : != dfloat32_type_node))))
4359 : 24 : warning_at (ploc, 0,
4360 : : "passing argument %d of %qE as %qT "
4361 : : "rather than %qT due to prototype",
4362 : : argnum, rname, type, valtype);
4363 : :
4364 : : }
4365 : : /* Detect integer changing in width or signedness.
4366 : : These warnings are only activated with
4367 : : -Wtraditional-conversion, not with -Wtraditional. */
4368 : 105 : else if (warn_traditional_conversion
4369 : 105 : && INTEGRAL_TYPE_P (type)
4370 : 102 : && INTEGRAL_TYPE_P (valtype))
4371 : : {
4372 : 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4373 : 83 : tree would_have_been = default_conversion (val);
4374 : 83 : tree type1 = TREE_TYPE (would_have_been);
4375 : :
4376 : 83 : if (val == error_mark_node)
4377 : : /* VAL could have been of incomplete type. */;
4378 : 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4379 : 83 : && (TYPE_MAIN_VARIANT (type)
4380 : 2 : == TYPE_MAIN_VARIANT (valtype)))
4381 : : /* No warning if function asks for enum
4382 : : and the actual arg is that enum type. */
4383 : : ;
4384 : 81 : else if (formal_prec != TYPE_PRECISION (type1))
4385 : 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4386 : : "passing argument %d of %qE "
4387 : : "with different width due to prototype",
4388 : : argnum, rname);
4389 : 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4390 : : ;
4391 : : /* Don't complain if the formal parameter type
4392 : : is an enum, because we can't tell now whether
4393 : : the value was an enum--even the same enum. */
4394 : 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4395 : : ;
4396 : 32 : else if (TREE_CODE (val) == INTEGER_CST
4397 : 5 : && int_fits_type_p (val, type))
4398 : : /* Change in signedness doesn't matter
4399 : : if a constant value is unaffected. */
4400 : : ;
4401 : : /* If the value is extended from a narrower
4402 : : unsigned type, it doesn't matter whether we
4403 : : pass it as signed or unsigned; the value
4404 : : certainly is the same either way. */
4405 : 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4406 : 32 : && TYPE_UNSIGNED (valtype))
4407 : : ;
4408 : 32 : else if (TYPE_UNSIGNED (type))
4409 : 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4410 : : "passing argument %d of %qE "
4411 : : "as unsigned due to prototype",
4412 : : argnum, rname);
4413 : : else
4414 : 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4415 : : "passing argument %d of %qE "
4416 : : "as signed due to prototype",
4417 : : argnum, rname);
4418 : : }
4419 : : }
4420 : :
4421 : : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4422 : : sake of better warnings from convert_and_check. */
4423 : 122403880 : if (excess_precision)
4424 : 370 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4425 : :
4426 : 122403880 : tree parmval = convert_for_assignment (ploc, ploc, type,
4427 : : val, origtype, ic_argpass,
4428 : : npc, fundecl, function,
4429 : : parmnum + 1, warnopt);
4430 : 122403880 : return parmval;
4431 : : }
4432 : :
4433 : : /* Convert the argument expressions in the vector VALUES
4434 : : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4435 : :
4436 : : If the list is exhausted, or when an element has NULL as its type,
4437 : : perform the default conversions.
4438 : :
4439 : : ORIGTYPES is the original types of the expressions in VALUES. This
4440 : : holds the type of enum values which have been converted to integral
4441 : : types. It may be NULL.
4442 : :
4443 : : FUNCTION is a tree for the called function. It is used only for
4444 : : error messages, where it is formatted with %qE.
4445 : :
4446 : : This is also where warnings about wrong number of args are generated.
4447 : :
4448 : : ARG_LOC are locations of function arguments (if any).
4449 : :
4450 : : Returns the actual number of arguments processed (which may be less
4451 : : than the length of VALUES in some error situations), or -1 on
4452 : : failure. */
4453 : :
4454 : : static int
4455 : 48622448 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4456 : : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4457 : : tree function, tree fundecl)
4458 : : {
4459 : 48622448 : tree typelist = TYPE_ARG_TYPES (fntype);
4460 : 48622448 : unsigned int parmnum;
4461 : 48622448 : bool error_args = false;
4462 : 48622448 : const bool type_generic = fundecl
4463 : 48622448 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4464 : 48622448 : bool type_generic_remove_excess_precision = false;
4465 : 48622448 : bool type_generic_overflow_p = false;
4466 : 48622448 : bool type_generic_bit_query = false;
4467 : 48622448 : tree selector;
4468 : :
4469 : : /* Change pointer to function to the function itself for
4470 : : diagnostics. */
4471 : 48622448 : if (TREE_CODE (function) == ADDR_EXPR
4472 : 48622448 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4473 : 48547508 : function = TREE_OPERAND (function, 0);
4474 : :
4475 : : /* Handle an ObjC selector specially for diagnostics. */
4476 : 48622448 : selector = objc_message_selector ();
4477 : :
4478 : : /* For a call to a built-in function declared without a prototype,
4479 : : set to the built-in function's argument list. */
4480 : 48622448 : tree builtin_typelist = NULL_TREE;
4481 : :
4482 : : /* For type-generic built-in functions, determine whether excess
4483 : : precision should be removed (classification) or not
4484 : : (comparison). */
4485 : 48622448 : if (fundecl
4486 : 48622448 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4487 : : {
4488 : 1385585 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4489 : 1385585 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4490 : : {
4491 : : /* For a call to a built-in function declared without a prototype
4492 : : use the types of the parameters of the internal built-in to
4493 : : match those of the arguments to. */
4494 : 682944 : if (tree bdecl = builtin_decl_explicit (code))
4495 : 682944 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4496 : : }
4497 : :
4498 : : /* For type-generic built-in functions, determine whether excess
4499 : : precision should be removed (classification) or not
4500 : : (comparison). */
4501 : 1385585 : if (type_generic)
4502 : 60732 : switch (code)
4503 : : {
4504 : 5305 : case BUILT_IN_ISFINITE:
4505 : 5305 : case BUILT_IN_ISINF:
4506 : 5305 : case BUILT_IN_ISINF_SIGN:
4507 : 5305 : case BUILT_IN_ISNAN:
4508 : 5305 : case BUILT_IN_ISNORMAL:
4509 : 5305 : case BUILT_IN_ISSIGNALING:
4510 : 5305 : case BUILT_IN_FPCLASSIFY:
4511 : 5305 : type_generic_remove_excess_precision = true;
4512 : 5305 : break;
4513 : :
4514 : 21012 : case BUILT_IN_ADD_OVERFLOW_P:
4515 : 21012 : case BUILT_IN_SUB_OVERFLOW_P:
4516 : 21012 : case BUILT_IN_MUL_OVERFLOW_P:
4517 : : /* The last argument of these type-generic builtins
4518 : : should not be promoted. */
4519 : 21012 : type_generic_overflow_p = true;
4520 : 21012 : break;
4521 : :
4522 : 1278 : case BUILT_IN_CLZG:
4523 : 1278 : case BUILT_IN_CTZG:
4524 : 1278 : case BUILT_IN_CLRSBG:
4525 : 1278 : case BUILT_IN_FFSG:
4526 : 1278 : case BUILT_IN_PARITYG:
4527 : 1278 : case BUILT_IN_POPCOUNTG:
4528 : : /* The first argument of these type-generic builtins
4529 : : should not be promoted. */
4530 : 1278 : type_generic_bit_query = true;
4531 : 1278 : break;
4532 : :
4533 : : default:
4534 : : break;
4535 : : }
4536 : : }
4537 : :
4538 : : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4539 : : individual converted arguments. */
4540 : :
4541 : 48622448 : tree typetail, builtin_typetail, val;
4542 : 48622448 : for (typetail = typelist,
4543 : 48622448 : builtin_typetail = builtin_typelist,
4544 : 48622448 : parmnum = 0;
4545 : 171959270 : values && values->iterate (parmnum, &val);
4546 : : ++parmnum)
4547 : : {
4548 : : /* The type of the function parameter (if it was declared with one). */
4549 : 245738855 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4550 : : /* The type of the built-in function parameter (if the function
4551 : : is a built-in). Used to detect type incompatibilities in
4552 : : calls to built-ins declared without a prototype. */
4553 : 123336858 : tree builtin_type = (builtin_typetail
4554 : 124364733 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4555 : : /* The original type of the argument being passed to the function. */
4556 : 123336858 : tree valtype = TREE_TYPE (val);
4557 : : /* The called function (or function selector in Objective C). */
4558 : 123336858 : tree rname = function;
4559 : 123336858 : int argnum = parmnum + 1;
4560 : 123336858 : const char *invalid_func_diag;
4561 : : /* Set for EXCESS_PRECISION_EXPR arguments. */
4562 : 123336858 : bool excess_precision = false;
4563 : : /* The value of the argument after conversion to the type
4564 : : of the function parameter it is passed to. */
4565 : 123336858 : tree parmval;
4566 : : /* Some __atomic_* builtins have additional hidden argument at
4567 : : position 0. */
4568 : 123336858 : location_t ploc
4569 : 123069427 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4570 : 123068827 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4571 : 123336858 : : input_location;
4572 : :
4573 : 123336858 : if (type == void_type_node)
4574 : : {
4575 : 33 : auto_diagnostic_group d;
4576 : 33 : int num_expected = parmnum;
4577 : 33 : int num_actual = values->length ();
4578 : 33 : gcc_rich_location rich_loc (loc);
4579 : 33 : if (ploc != input_location)
4580 : 32 : rich_loc.add_range (ploc);
4581 : 33 : if (selector)
4582 : 0 : error_at (&rich_loc,
4583 : : "too many arguments to method %qE; expected %i, have %i",
4584 : : selector, num_expected, num_actual);
4585 : : else
4586 : 33 : error_at (&rich_loc,
4587 : : "too many arguments to function %qE; expected %i, have %i",
4588 : : function, num_expected, num_actual);
4589 : 33 : inform_declaration (fundecl, function);
4590 : 65 : return error_args ? -1 : (int) parmnum;
4591 : 33 : }
4592 : :
4593 : 123336825 : if (builtin_type == void_type_node)
4594 : : {
4595 : 12 : auto_diagnostic_group d;
4596 : 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4597 : : "too many arguments to built-in function %qE "
4598 : : "expecting %d", function, parmnum))
4599 : 12 : inform_declaration (fundecl, function);
4600 : 12 : builtin_typetail = NULL_TREE;
4601 : 12 : }
4602 : :
4603 : 89814 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4604 : 123426522 : && !(fundecl && fndecl_built_in_p (fundecl)))
4605 : : {
4606 : 6963 : auto_diagnostic_group d;
4607 : 6963 : bool warned;
4608 : 6963 : if (selector)
4609 : 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4610 : : "ISO C23 does not allow arguments"
4611 : : " for method %qE declared without parameters",
4612 : : function);
4613 : : else
4614 : 6963 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4615 : : "ISO C23 does not allow arguments"
4616 : : " for function %qE declared without parameters",
4617 : : function);
4618 : 6963 : if (warned)
4619 : 4 : inform_declaration (fundecl, function);
4620 : 6963 : }
4621 : :
4622 : 123336825 : if (selector && argnum > 2)
4623 : : {
4624 : 0 : rname = selector;
4625 : 0 : argnum -= 2;
4626 : : }
4627 : :
4628 : : /* Determine if VAL is a null pointer constant before folding it. */
4629 : 123336825 : bool npc = null_pointer_constant_p (val);
4630 : :
4631 : : /* If there is excess precision and a prototype, convert once to
4632 : : the required type rather than converting via the semantic
4633 : : type. Likewise without a prototype a float value represented
4634 : : as long double should be converted once to double. But for
4635 : : type-generic classification functions excess precision must
4636 : : be removed here. */
4637 : 123336825 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4638 : 423 : && (type || !type_generic || !type_generic_remove_excess_precision))
4639 : : {
4640 : 403 : val = TREE_OPERAND (val, 0);
4641 : 403 : excess_precision = true;
4642 : : }
4643 : 123336825 : val = c_fully_fold (val, false, NULL);
4644 : 246673651 : STRIP_TYPE_NOPS (val);
4645 : :
4646 : 123336825 : val = require_complete_type (ploc, val);
4647 : :
4648 : : /* Some floating-point arguments must be promoted to double when
4649 : : no type is specified by a prototype. This applies to
4650 : : arguments of type float, and to architecture-specific types
4651 : : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4652 : 123336825 : bool promote_float_arg = false;
4653 : 123336825 : if (type == NULL_TREE
4654 : 934861 : && TREE_CODE (valtype) == REAL_TYPE
4655 : 269617 : && (TYPE_PRECISION (valtype)
4656 : 269617 : <= TYPE_PRECISION (double_type_node))
4657 : 262335 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4658 : 131343 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4659 : 123468168 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4660 : : {
4661 : : /* Promote this argument, unless it has a _FloatN or
4662 : : _FloatNx type. */
4663 : 1027603 : promote_float_arg = true;
4664 : 1027603 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4665 : 900219 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4666 : : {
4667 : : promote_float_arg = false;
4668 : : break;
4669 : : }
4670 : : /* Don't promote __bf16 either. */
4671 : 130610 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4672 : 123206285 : promote_float_arg = false;
4673 : : }
4674 : :
4675 : 123336825 : if (type != NULL_TREE)
4676 : : {
4677 : 122401964 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4678 : 122401964 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4679 : : val, valtype, npc, rname, parmnum, argnum,
4680 : : excess_precision, 0);
4681 : : }
4682 : : /* A NULLPTR type is just a nullptr always. */
4683 : 934861 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4684 : 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4685 : 934852 : else if (promote_float_arg)
4686 : : {
4687 : 127314 : if (type_generic)
4688 : : parmval = val;
4689 : : else
4690 : : {
4691 : : /* Convert `float' to `double'. */
4692 : 124446 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4693 : 6 : warning_at (ploc, OPT_Wdouble_promotion,
4694 : : "implicit conversion from %qT to %qT when passing "
4695 : : "argument to function",
4696 : : valtype, double_type_node);
4697 : 124446 : parmval = convert (double_type_node, val);
4698 : : }
4699 : : }
4700 : 807538 : else if ((excess_precision && !type_generic)
4701 : 807536 : || (type_generic_overflow_p && parmnum == 2)
4702 : 786528 : || (type_generic_bit_query && parmnum == 0))
4703 : : /* A "double" argument with excess precision being passed
4704 : : without a prototype or in variable arguments.
4705 : : The last argument of __builtin_*_overflow_p should not be
4706 : : promoted, similarly the first argument of
4707 : : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4708 : 22282 : parmval = convert (valtype, val);
4709 : 1570512 : else if ((invalid_func_diag =
4710 : 785256 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4711 : : {
4712 : 0 : error (invalid_func_diag);
4713 : 0 : return -1;
4714 : : }
4715 : 785256 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4716 : : {
4717 : : return -1;
4718 : : }
4719 : : else
4720 : : /* Convert `short' and `char' to full-size `int'. */
4721 : 785253 : parmval = default_conversion (val);
4722 : :
4723 : 123336822 : (*values)[parmnum] = parmval;
4724 : 123336822 : if (parmval == error_mark_node)
4725 : 105 : error_args = true;
4726 : :
4727 : 123336822 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4728 : : {
4729 : : /* For a call to a built-in function declared without a prototype,
4730 : : perform the conversions from the argument to the expected type
4731 : : but issue warnings rather than errors for any mismatches.
4732 : : Ignore the converted argument and use the PARMVAL obtained
4733 : : above by applying default conversions instead. */
4734 : 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4735 : 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4736 : : val, valtype, npc, rname, parmnum, argnum,
4737 : : excess_precision,
4738 : : OPT_Wbuiltin_declaration_mismatch);
4739 : : }
4740 : :
4741 : 123336822 : if (typetail)
4742 : 122401964 : typetail = TREE_CHAIN (typetail);
4743 : :
4744 : 123336822 : if (builtin_typetail)
4745 : 1027858 : builtin_typetail = TREE_CHAIN (builtin_typetail);
4746 : : }
4747 : :
4748 : 85837454 : gcc_assert (parmnum == vec_safe_length (values));
4749 : :
4750 : 96916403 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
4751 : : {
4752 : : /* Not enough args.
4753 : : Determine minimum number of arguments required. */
4754 : : int min_expected_num = 0;
4755 : 231 : bool at_least_p = false;
4756 : : tree iter = typelist;
4757 : 386 : while (true)
4758 : : {
4759 : 231 : if (!iter)
4760 : : {
4761 : : /* Variadic arguments; stop iterating. */
4762 : : at_least_p = true;
4763 : : break;
4764 : : }
4765 : 222 : if (iter == void_list_node)
4766 : : /* End of arguments; stop iterating. */
4767 : : break;
4768 : 155 : ++min_expected_num;
4769 : 155 : iter = TREE_CHAIN (iter);
4770 : : }
4771 : 76 : auto_diagnostic_group d;
4772 : 76 : int actual_num = vec_safe_length (values);
4773 : 143 : error_at (loc,
4774 : : at_least_p
4775 : : ? G_("too few arguments to function %qE; expected at least %i, have %i")
4776 : : : G_("too few arguments to function %qE; expected %i, have %i"),
4777 : : function, min_expected_num, actual_num);
4778 : 76 : inform_declaration (fundecl, function);
4779 : 76 : return -1;
4780 : 76 : }
4781 : :
4782 : 49269542 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
4783 : : {
4784 : : unsigned nargs = parmnum;
4785 : 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
4786 : 428 : ++nargs;
4787 : :
4788 : 188 : auto_diagnostic_group d;
4789 : 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4790 : : "too few arguments to built-in function %qE "
4791 : : "expecting %u", function, nargs - 1))
4792 : 173 : inform_declaration (fundecl, function);
4793 : 188 : }
4794 : :
4795 : 48622336 : return error_args ? -1 : (int) parmnum;
4796 : : }
4797 : :
4798 : : /* This is the entry point used by the parser to build unary operators
4799 : : in the input. CODE, a tree_code, specifies the unary operator, and
4800 : : ARG is the operand. For unary plus, the C parser currently uses
4801 : : CONVERT_EXPR for code.
4802 : :
4803 : : LOC is the location to use for the tree generated.
4804 : : */
4805 : :
4806 : : struct c_expr
4807 : 7968900 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
4808 : : {
4809 : 7968900 : struct c_expr result;
4810 : :
4811 : 7968900 : result.original_code = code;
4812 : 7968900 : result.original_type = NULL;
4813 : 7968900 : result.m_decimal = 0;
4814 : :
4815 : 7968900 : if (reject_gcc_builtin (arg.value))
4816 : : {
4817 : 2 : result.value = error_mark_node;
4818 : : }
4819 : : else
4820 : : {
4821 : 7968898 : result.value = build_unary_op (loc, code, arg.value, false);
4822 : :
4823 : 7968898 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
4824 : 5 : overflow_warning (loc, result.value, arg.value);
4825 : : }
4826 : :
4827 : : /* We are typically called when parsing a prefix token at LOC acting on
4828 : : ARG. Reflect this by updating the source range of the result to
4829 : : start at LOC and end at the end of ARG. */
4830 : 7968900 : set_c_expr_source_range (&result,
4831 : : loc, arg.get_finish ());
4832 : :
4833 : 7968900 : return result;
4834 : : }
4835 : :
4836 : : /* Returns true if TYPE is a character type, *not* including wchar_t. */
4837 : :
4838 : : bool
4839 : 1245785 : char_type_p (tree type)
4840 : : {
4841 : 1245785 : return (type == char_type_node
4842 : 960488 : || type == unsigned_char_type_node
4843 : 926457 : || type == signed_char_type_node
4844 : 925618 : || type == char16_type_node
4845 : 1956810 : || type == char32_type_node);
4846 : : }
4847 : :
4848 : : /* This is the entry point used by the parser to build binary operators
4849 : : in the input. CODE, a tree_code, specifies the binary operator, and
4850 : : ARG1 and ARG2 are the operands. In addition to constructing the
4851 : : expression, we check for operands that were written with other binary
4852 : : operators in a way that is likely to confuse the user.
4853 : :
4854 : : LOCATION is the location of the binary operator. */
4855 : :
4856 : : struct c_expr
4857 : 9146073 : parser_build_binary_op (location_t location, enum tree_code code,
4858 : : struct c_expr arg1, struct c_expr arg2)
4859 : : {
4860 : 9146073 : struct c_expr result;
4861 : 9146073 : result.m_decimal = 0;
4862 : :
4863 : 9146073 : enum tree_code code1 = arg1.original_code;
4864 : 9146073 : enum tree_code code2 = arg2.original_code;
4865 : 9146073 : tree type1 = (arg1.original_type
4866 : 9146073 : ? arg1.original_type
4867 : 5881501 : : TREE_TYPE (arg1.value));
4868 : 9146073 : tree type2 = (arg2.original_type
4869 : 9146073 : ? arg2.original_type
4870 : 6751722 : : TREE_TYPE (arg2.value));
4871 : :
4872 : 9146073 : result.value = build_binary_op (location, code,
4873 : : arg1.value, arg2.value, true);
4874 : 9146072 : result.original_code = code;
4875 : 9146072 : result.original_type = NULL;
4876 : 9146072 : result.m_decimal = 0;
4877 : :
4878 : 9146072 : if (TREE_CODE (result.value) == ERROR_MARK)
4879 : : {
4880 : 1295 : set_c_expr_source_range (&result,
4881 : : arg1.get_start (),
4882 : : arg2.get_finish ());
4883 : 1295 : return result;
4884 : : }
4885 : :
4886 : 9144777 : if (location != UNKNOWN_LOCATION)
4887 : 9144777 : protected_set_expr_location (result.value, location);
4888 : :
4889 : 9144777 : set_c_expr_source_range (&result,
4890 : : arg1.get_start (),
4891 : : arg2.get_finish ());
4892 : :
4893 : : /* Check for cases such as x+y<<z which users are likely
4894 : : to misinterpret. */
4895 : 9144777 : if (warn_parentheses)
4896 : 2000257 : warn_about_parentheses (location, code, code1, arg1.value, code2,
4897 : : arg2.value);
4898 : :
4899 : 9144777 : if (warn_logical_op)
4900 : 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
4901 : : code1, arg1.value, code2, arg2.value);
4902 : :
4903 : 9144777 : if (warn_tautological_compare)
4904 : : {
4905 : 1998770 : tree lhs = arg1.value;
4906 : 1998770 : tree rhs = arg2.value;
4907 : 1998770 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4908 : : {
4909 : 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
4910 : 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
4911 : : lhs = NULL_TREE;
4912 : : else
4913 : 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
4914 : : }
4915 : 1998770 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
4916 : : {
4917 : 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
4918 : 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
4919 : : rhs = NULL_TREE;
4920 : : else
4921 : 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
4922 : : }
4923 : 1998770 : if (lhs != NULL_TREE && rhs != NULL_TREE)
4924 : 1998770 : warn_tautological_cmp (location, code, lhs, rhs);
4925 : : }
4926 : :
4927 : 9144777 : if (warn_logical_not_paren
4928 : 1998853 : && TREE_CODE_CLASS (code) == tcc_comparison
4929 : 404133 : && code1 == TRUTH_NOT_EXPR
4930 : 404133 : && code2 != TRUTH_NOT_EXPR
4931 : : /* Avoid warning for !!x == y. */
4932 : 9144903 : && (TREE_CODE (arg1.value) != NE_EXPR
4933 : 19 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
4934 : : {
4935 : : /* Avoid warning for !b == y where b has _Bool type. */
4936 : 107 : tree t = integer_zero_node;
4937 : 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
4938 : 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
4939 : 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4940 : : {
4941 : 90 : t = TREE_OPERAND (arg1.value, 0);
4942 : 199 : do
4943 : : {
4944 : 199 : if (TREE_TYPE (t) != integer_type_node)
4945 : : break;
4946 : 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4947 : 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
4948 : 90 : else if (CONVERT_EXPR_P (t))
4949 : 19 : t = TREE_OPERAND (t, 0);
4950 : : else
4951 : : break;
4952 : : }
4953 : : while (1);
4954 : : }
4955 : 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4956 : 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4957 : : }
4958 : :
4959 : : /* Warn about comparisons against string literals, with the exception
4960 : : of testing for equality or inequality of a string literal with NULL. */
4961 : 9144777 : if (code == EQ_EXPR || code == NE_EXPR)
4962 : : {
4963 : 1147286 : if ((code1 == STRING_CST
4964 : 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4965 : 1147297 : || (code2 == STRING_CST
4966 : 21 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4967 : 18 : warning_at (location, OPT_Waddress,
4968 : : "comparison with string literal results in unspecified behavior");
4969 : : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4970 : 1147286 : if (POINTER_TYPE_P (type1)
4971 : 90217 : && null_pointer_constant_p (arg2.value)
4972 : 1168120 : && char_type_p (type2))
4973 : : {
4974 : 18 : auto_diagnostic_group d;
4975 : 18 : if (warning_at (location, OPT_Wpointer_compare,
4976 : : "comparison between pointer and zero character "
4977 : : "constant"))
4978 : 10 : inform (arg1.get_start (),
4979 : : "did you mean to dereference the pointer?");
4980 : 18 : }
4981 : 1147268 : else if (POINTER_TYPE_P (type2)
4982 : 87550 : && null_pointer_constant_p (arg1.value)
4983 : 1147530 : && char_type_p (type1))
4984 : : {
4985 : 10 : auto_diagnostic_group d;
4986 : 10 : if (warning_at (location, OPT_Wpointer_compare,
4987 : : "comparison between pointer and zero character "
4988 : : "constant"))
4989 : 10 : inform (arg2.get_start (),
4990 : : "did you mean to dereference the pointer?");
4991 : 10 : }
4992 : : }
4993 : 7997491 : else if (TREE_CODE_CLASS (code) == tcc_comparison
4994 : 972910 : && (code1 == STRING_CST || code2 == STRING_CST))
4995 : 0 : warning_at (location, OPT_Waddress,
4996 : : "comparison with string literal results in unspecified "
4997 : : "behavior");
4998 : :
4999 : 9144777 : if (warn_zero_as_null_pointer_constant
5000 : 18 : && c_inhibit_evaluation_warnings == 0
5001 : 18 : && TREE_CODE_CLASS (code) == tcc_comparison)
5002 : : {
5003 : 18 : if ((TREE_CODE (type1) == POINTER_TYPE
5004 : 14 : || TREE_CODE (type1) == NULLPTR_TYPE)
5005 : 6 : && TREE_CODE (type2) == INTEGER_TYPE
5006 : 24 : && null_pointer_constant_p (arg2.value))
5007 : 6 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5008 : : "zero as null pointer constant");
5009 : :
5010 : 18 : if ((TREE_CODE (type2) == POINTER_TYPE
5011 : 12 : || TREE_CODE (type2) == NULLPTR_TYPE)
5012 : 8 : && TREE_CODE (type1) == INTEGER_TYPE
5013 : 26 : && null_pointer_constant_p (arg1.value))
5014 : 8 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5015 : : "zero as null pointer constant");
5016 : : }
5017 : :
5018 : 9144777 : if (warn_array_compare
5019 : 1998608 : && TREE_CODE_CLASS (code) == tcc_comparison
5020 : 403918 : && TREE_CODE (type1) == ARRAY_TYPE
5021 : 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5022 : 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5023 : :
5024 : 2151964 : if (TREE_OVERFLOW_P (result.value)
5025 : 290 : && !TREE_OVERFLOW_P (arg1.value)
5026 : 9145060 : && !TREE_OVERFLOW_P (arg2.value))
5027 : 240 : overflow_warning (location, result.value);
5028 : :
5029 : : /* Warn about comparisons of different enum types. */
5030 : 9144776 : if (warn_enum_compare
5031 : 2018545 : && TREE_CODE_CLASS (code) == tcc_comparison
5032 : 408343 : && TREE_CODE (type1) == ENUMERAL_TYPE
5033 : 6970 : && TREE_CODE (type2) == ENUMERAL_TYPE
5034 : 9151684 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5035 : 2 : warning_at (location, OPT_Wenum_compare,
5036 : : "comparison between %qT and %qT",
5037 : : type1, type2);
5038 : :
5039 : 9144776 : if (warn_xor_used_as_pow
5040 : 9144776 : && code == BIT_XOR_EXPR
5041 : 75041 : && arg1.m_decimal
5042 : 722 : && arg2.m_decimal)
5043 : 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5044 : : location,
5045 : : arg2.get_location (), arg2.value);
5046 : :
5047 : : return result;
5048 : : }
5049 : :
5050 : : /* Return a tree for the difference of pointers OP0 and OP1.
5051 : : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5052 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5053 : :
5054 : : static tree
5055 : 3639 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5056 : : {
5057 : 3639 : tree restype = ptrdiff_type_node;
5058 : 3639 : tree result, inttype;
5059 : :
5060 : 3639 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5061 : 3639 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5062 : 3639 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5063 : 3639 : tree orig_op0 = op0;
5064 : 3639 : tree orig_op1 = op1;
5065 : :
5066 : : /* If the operands point into different address spaces, we need to
5067 : : explicitly convert them to pointers into the common address space
5068 : : before we can subtract the numerical address values. */
5069 : 3639 : if (as0 != as1)
5070 : : {
5071 : 0 : addr_space_t as_common;
5072 : 0 : tree common_type;
5073 : :
5074 : : /* Determine the common superset address space. This is guaranteed
5075 : : to exist because the caller verified that comp_target_types
5076 : : returned non-zero. */
5077 : 0 : if (!addr_space_superset (as0, as1, &as_common))
5078 : 0 : gcc_unreachable ();
5079 : :
5080 : 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5081 : 0 : op0 = convert (common_type, op0);
5082 : 0 : op1 = convert (common_type, op1);
5083 : : }
5084 : :
5085 : : /* Determine integer type result of the subtraction. This will usually
5086 : : be the same as the result type (ptrdiff_t), but may need to be a wider
5087 : : type if pointers for the address space are wider than ptrdiff_t. */
5088 : 3639 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5089 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5090 : : else
5091 : : inttype = restype;
5092 : :
5093 : 3639 : if (VOID_TYPE_P (target_type))
5094 : 135 : pedwarn (loc, OPT_Wpointer_arith,
5095 : : "pointer of type %<void *%> used in subtraction");
5096 : 3639 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5097 : 4 : pedwarn (loc, OPT_Wpointer_arith,
5098 : : "pointer to a function used in subtraction");
5099 : :
5100 : 3639 : if (current_function_decl != NULL_TREE
5101 : 3639 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5102 : : {
5103 : 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5104 : 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5105 : :
5106 : 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5107 : 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5108 : : }
5109 : :
5110 : : /* First do the subtraction, then build the divide operator
5111 : : and only convert at the very end.
5112 : : Do not do default conversions in case restype is a short type. */
5113 : :
5114 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5115 : : pointers. If some platform cannot provide that, or has a larger
5116 : : ptrdiff_type to support differences larger than half the address
5117 : : space, cast the pointers to some larger integer type and do the
5118 : : computations in that type. */
5119 : 3639 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5120 : 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5121 : : convert (inttype, op1), false);
5122 : : else
5123 : : {
5124 : : /* Cast away qualifiers. */
5125 : 3639 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5126 : 3639 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5127 : 3639 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5128 : : }
5129 : :
5130 : : /* This generates an error if op1 is pointer to incomplete type. */
5131 : 3639 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5132 : 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5133 : 3635 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5134 : 3635 : TREE_TYPE (TREE_TYPE (orig_op0))))
5135 : 7270 : verify_type_context (loc, TCTX_POINTER_ARITH,
5136 : 3635 : TREE_TYPE (TREE_TYPE (orig_op1)));
5137 : :
5138 : 3639 : op1 = c_size_in_bytes (target_type);
5139 : :
5140 : 3639 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5141 : 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5142 : :
5143 : : /* Divide by the size, in easiest possible way. */
5144 : 3639 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5145 : : op0, convert (inttype, op1));
5146 : :
5147 : : /* Convert to final result type if necessary. */
5148 : 3639 : return convert (restype, result);
5149 : : }
5150 : :
5151 : : /* Expand atomic compound assignments into an appropriate sequence as
5152 : : specified by the C11 standard section 6.5.16.2.
5153 : :
5154 : : _Atomic T1 E1
5155 : : T2 E2
5156 : : E1 op= E2
5157 : :
5158 : : This sequence is used for all types for which these operations are
5159 : : supported.
5160 : :
5161 : : In addition, built-in versions of the 'fe' prefixed routines may
5162 : : need to be invoked for floating point (real, complex or vector) when
5163 : : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5164 : :
5165 : : T1 newval;
5166 : : T1 old;
5167 : : T1 *addr
5168 : : T2 val
5169 : : fenv_t fenv
5170 : :
5171 : : addr = &E1;
5172 : : val = (E2);
5173 : : __atomic_load (addr, &old, SEQ_CST);
5174 : : feholdexcept (&fenv);
5175 : : loop:
5176 : : newval = old op val;
5177 : : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5178 : : SEQ_CST))
5179 : : goto done;
5180 : : feclearexcept (FE_ALL_EXCEPT);
5181 : : goto loop:
5182 : : done:
5183 : : feupdateenv (&fenv);
5184 : :
5185 : : The compiler will issue the __atomic_fetch_* built-in when possible,
5186 : : otherwise it will generate the generic form of the atomic operations.
5187 : : This requires temp(s) and has their address taken. The atomic processing
5188 : : is smart enough to figure out when the size of an object can utilize
5189 : : a lock-free version, and convert the built-in call to the appropriate
5190 : : lock-free routine. The optimizers will then dispose of any temps that
5191 : : are no longer required, and lock-free implementations are utilized as
5192 : : long as there is target support for the required size.
5193 : :
5194 : : If the operator is NOP_EXPR, then this is a simple assignment, and
5195 : : an __atomic_store is issued to perform the assignment rather than
5196 : : the above loop. */
5197 : :
5198 : : /* Build an atomic assignment at LOC, expanding into the proper
5199 : : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5200 : : the result of the operation, unless RETURN_OLD_P, in which case
5201 : : return the old value of LHS (this is only for postincrement and
5202 : : postdecrement). */
5203 : :
5204 : : static tree
5205 : 30425 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5206 : : tree rhs, bool return_old_p)
5207 : : {
5208 : 30425 : tree fndecl, func_call;
5209 : 30425 : vec<tree, va_gc> *params;
5210 : 30425 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5211 : 30425 : tree old, old_addr;
5212 : 30425 : tree compound_stmt = NULL_TREE;
5213 : 30425 : tree stmt, goto_stmt;
5214 : 30425 : tree loop_label, loop_decl, done_label, done_decl;
5215 : :
5216 : 30425 : tree lhs_type = TREE_TYPE (lhs);
5217 : 30425 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5218 : 30425 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5219 : 30425 : tree rhs_semantic_type = TREE_TYPE (rhs);
5220 : 30425 : tree nonatomic_rhs_semantic_type;
5221 : 30425 : tree rhs_type;
5222 : :
5223 : 30425 : gcc_assert (TYPE_ATOMIC (lhs_type));
5224 : :
5225 : 30425 : if (return_old_p)
5226 : 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5227 : :
5228 : : /* Allocate enough vector items for a compare_exchange. */
5229 : 30425 : vec_alloc (params, 6);
5230 : :
5231 : : /* Create a compound statement to hold the sequence of statements
5232 : : with a loop. */
5233 : 30425 : if (modifycode != NOP_EXPR)
5234 : : {
5235 : 19954 : compound_stmt = c_begin_compound_stmt (false);
5236 : :
5237 : : /* For consistency with build_modify_expr on non-_Atomic,
5238 : : mark the lhs as read. Also, it would be very hard to match
5239 : : such expressions in mark_exp_read. */
5240 : 19954 : mark_exp_read (lhs);
5241 : : }
5242 : :
5243 : : /* Remove any excess precision (which is only present here in the
5244 : : case of compound assignments). */
5245 : 30425 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5246 : : {
5247 : 0 : gcc_assert (modifycode != NOP_EXPR);
5248 : 0 : rhs = TREE_OPERAND (rhs, 0);
5249 : : }
5250 : 30425 : rhs_type = TREE_TYPE (rhs);
5251 : :
5252 : : /* Fold the RHS if it hasn't already been folded. */
5253 : 30425 : if (modifycode != NOP_EXPR)
5254 : 19954 : rhs = c_fully_fold (rhs, false, NULL);
5255 : :
5256 : : /* Remove the qualifiers for the rest of the expressions and create
5257 : : the VAL temp variable to hold the RHS. */
5258 : 30425 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5259 : 30425 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5260 : 30425 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5261 : : TYPE_UNQUALIFIED);
5262 : 30425 : val = create_tmp_var_raw (nonatomic_rhs_type);
5263 : 30425 : TREE_ADDRESSABLE (val) = 1;
5264 : 30425 : suppress_warning (val);
5265 : 30425 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5266 : : NULL_TREE);
5267 : 30425 : TREE_SIDE_EFFECTS (rhs) = 1;
5268 : 30425 : SET_EXPR_LOCATION (rhs, loc);
5269 : 30425 : if (modifycode != NOP_EXPR)
5270 : 19954 : add_stmt (rhs);
5271 : :
5272 : : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5273 : : an atomic_store. */
5274 : 30425 : if (modifycode == NOP_EXPR)
5275 : : {
5276 : 10471 : compound_stmt = rhs;
5277 : : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5278 : 10471 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5279 : 10471 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5280 : 10471 : params->quick_push (lhs_addr);
5281 : 10471 : params->quick_push (rhs);
5282 : 10471 : params->quick_push (seq_cst);
5283 : 10471 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5284 : :
5285 : 10471 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5286 : : compound_stmt, func_call);
5287 : :
5288 : : /* VAL is the value which was stored, return a COMPOUND_STMT of
5289 : : the statement and that value. */
5290 : 10471 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5291 : : }
5292 : :
5293 : : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5294 : : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5295 : : isn't applicable for such builtins. ??? Do we want to handle enums? */
5296 : 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5297 : 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5298 : : {
5299 : 11713 : built_in_function fncode;
5300 : 11713 : switch (modifycode)
5301 : : {
5302 : 3075 : case PLUS_EXPR:
5303 : 3075 : case POINTER_PLUS_EXPR:
5304 : 2124 : fncode = (return_old_p
5305 : 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5306 : : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5307 : : break;
5308 : 2889 : case MINUS_EXPR:
5309 : 2094 : fncode = (return_old_p
5310 : 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5311 : : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5312 : : break;
5313 : 609 : case BIT_AND_EXPR:
5314 : 609 : fncode = (return_old_p
5315 : 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5316 : : : BUILT_IN_ATOMIC_AND_FETCH_N);
5317 : : break;
5318 : 609 : case BIT_IOR_EXPR:
5319 : 609 : fncode = (return_old_p
5320 : 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5321 : : : BUILT_IN_ATOMIC_OR_FETCH_N);
5322 : : break;
5323 : 609 : case BIT_XOR_EXPR:
5324 : 609 : fncode = (return_old_p
5325 : 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5326 : : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5327 : : break;
5328 : 3922 : default:
5329 : 3922 : goto cas_loop;
5330 : : }
5331 : :
5332 : : /* We can only use "_1" through "_16" variants of the atomic fetch
5333 : : built-ins. */
5334 : 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5335 : 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5336 : 0 : goto cas_loop;
5337 : :
5338 : : /* If this is a pointer type, we need to multiply by the size of
5339 : : the pointer target type. */
5340 : 7791 : if (POINTER_TYPE_P (lhs_type))
5341 : : {
5342 : 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5343 : : /* ??? This would introduce -Wdiscarded-qualifiers
5344 : : warning: __atomic_fetch_* expect volatile void *
5345 : : type as the first argument. (Assignments between
5346 : : atomic and non-atomic objects are OK.) */
5347 : 1018 : || TYPE_RESTRICT (lhs_type))
5348 : 17 : goto cas_loop;
5349 : 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5350 : 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5351 : : convert (ptrdiff_type_node, rhs),
5352 : : convert (ptrdiff_type_node, sz));
5353 : : }
5354 : :
5355 : : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5356 : : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5357 : 7774 : fndecl = builtin_decl_explicit (fncode);
5358 : 7774 : params->quick_push (lhs_addr);
5359 : 7774 : params->quick_push (rhs);
5360 : 7774 : params->quick_push (seq_cst);
5361 : 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5362 : :
5363 : 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5364 : 7774 : TREE_ADDRESSABLE (newval) = 1;
5365 : 7774 : suppress_warning (newval);
5366 : 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5367 : : NULL_TREE, NULL_TREE);
5368 : 7774 : SET_EXPR_LOCATION (rhs, loc);
5369 : 7774 : add_stmt (rhs);
5370 : :
5371 : : /* Finish the compound statement. */
5372 : 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5373 : :
5374 : : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5375 : : the statement and that value. */
5376 : 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5377 : : }
5378 : :
5379 : 8241 : cas_loop:
5380 : : /* Create the variables and labels required for the op= form. */
5381 : 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5382 : 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5383 : 12180 : TREE_ADDRESSABLE (old) = 1;
5384 : 12180 : suppress_warning (old);
5385 : :
5386 : 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5387 : 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5388 : 12180 : TREE_ADDRESSABLE (newval) = 1;
5389 : 12180 : suppress_warning (newval);
5390 : :
5391 : 12180 : loop_decl = create_artificial_label (loc);
5392 : 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5393 : :
5394 : 12180 : done_decl = create_artificial_label (loc);
5395 : 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5396 : :
5397 : : /* __atomic_load (addr, &old, SEQ_CST). */
5398 : 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5399 : 12180 : params->quick_push (lhs_addr);
5400 : 12180 : params->quick_push (old_addr);
5401 : 12180 : params->quick_push (seq_cst);
5402 : 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5403 : 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5404 : : NULL_TREE);
5405 : 12180 : add_stmt (old);
5406 : 12180 : params->truncate (0);
5407 : :
5408 : : /* Create the expressions for floating-point environment
5409 : : manipulation, if required. */
5410 : 12180 : bool need_fenv = (flag_trapping_math
5411 : 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5412 : 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5413 : 12180 : if (need_fenv)
5414 : 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5415 : :
5416 : 12180 : if (hold_call)
5417 : 7054 : add_stmt (hold_call);
5418 : :
5419 : : /* loop: */
5420 : 12180 : add_stmt (loop_label);
5421 : :
5422 : : /* newval = old + val; */
5423 : 12180 : if (rhs_type != rhs_semantic_type)
5424 : 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5425 : 12180 : rhs = build_binary_op (loc, modifycode,
5426 : : convert_lvalue_to_rvalue (loc, old, true, true),
5427 : : val, true);
5428 : 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5429 : : {
5430 : 0 : tree eptype = TREE_TYPE (rhs);
5431 : 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5432 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5433 : : }
5434 : : else
5435 : 12180 : rhs = c_fully_fold (rhs, false, NULL);
5436 : 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5437 : : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5438 : : NULL_TREE, 0);
5439 : : /* The temporary variable for NEWVAL is only gimplified correctly (in
5440 : : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5441 : : subsequent ICEs in nested function processing after an error, ensure such
5442 : : a TARGET_EXPR is built even after an error. */
5443 : 12180 : if (rhs == error_mark_node)
5444 : 10 : rhs = old;
5445 : 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5446 : : NULL_TREE);
5447 : 12180 : SET_EXPR_LOCATION (rhs, loc);
5448 : 12180 : add_stmt (rhs);
5449 : :
5450 : : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5451 : : goto done; */
5452 : 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5453 : 12180 : params->quick_push (lhs_addr);
5454 : 12180 : params->quick_push (old_addr);
5455 : 12180 : params->quick_push (newval_addr);
5456 : 12180 : params->quick_push (integer_zero_node);
5457 : 12180 : params->quick_push (seq_cst);
5458 : 12180 : params->quick_push (seq_cst);
5459 : 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5460 : :
5461 : 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5462 : 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5463 : :
5464 : 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5465 : 12180 : SET_EXPR_LOCATION (stmt, loc);
5466 : 12180 : add_stmt (stmt);
5467 : :
5468 : 12180 : if (clear_call)
5469 : 7054 : add_stmt (clear_call);
5470 : :
5471 : : /* goto loop; */
5472 : 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5473 : 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5474 : 12180 : add_stmt (goto_stmt);
5475 : :
5476 : : /* done: */
5477 : 12180 : add_stmt (done_label);
5478 : :
5479 : 12180 : if (update_call)
5480 : 7054 : add_stmt (update_call);
5481 : :
5482 : : /* Finish the compound statement. */
5483 : 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5484 : :
5485 : : /* NEWVAL is the value that was successfully stored, return a
5486 : : COMPOUND_EXPR of the statement and the appropriate value. */
5487 : 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5488 : 12180 : return_old_p ? old : newval);
5489 : : }
5490 : :
5491 : : /* Construct and perhaps optimize a tree representation
5492 : : for a unary operation. CODE, a tree_code, specifies the operation
5493 : : and XARG is the operand.
5494 : : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5495 : : promotions (such as from short to int).
5496 : : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5497 : : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5498 : : to pointers in C99.
5499 : :
5500 : : LOCATION is the location of the operator. */
5501 : :
5502 : : tree
5503 : 59023101 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5504 : : bool noconvert)
5505 : : {
5506 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5507 : 59023101 : tree arg = xarg;
5508 : 59023101 : tree argtype = NULL_TREE;
5509 : 59023101 : enum tree_code typecode;
5510 : 59023101 : tree val;
5511 : 59023101 : tree ret = error_mark_node;
5512 : 59023101 : tree eptype = NULL_TREE;
5513 : 59023101 : const char *invalid_op_diag;
5514 : 59023101 : bool int_operands;
5515 : :
5516 : 59023101 : int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5517 : 6291261 : if (int_operands)
5518 : 6291261 : arg = remove_c_maybe_const_expr (arg);
5519 : :
5520 : 59023101 : if (code != ADDR_EXPR)
5521 : 8325519 : arg = require_complete_type (location, arg);
5522 : :
5523 : 59023101 : typecode = TREE_CODE (TREE_TYPE (arg));
5524 : 59023101 : if (typecode == ERROR_MARK)
5525 : 74 : return error_mark_node;
5526 : 59023027 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5527 : 31500 : typecode = INTEGER_TYPE;
5528 : :
5529 : 118046054 : if ((invalid_op_diag
5530 : 59023027 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5531 : : {
5532 : 0 : error_at (location, invalid_op_diag);
5533 : 0 : return error_mark_node;
5534 : : }
5535 : :
5536 : 59023027 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5537 : : {
5538 : 235 : eptype = TREE_TYPE (arg);
5539 : 235 : arg = TREE_OPERAND (arg, 0);
5540 : : }
5541 : :
5542 : 59023027 : switch (code)
5543 : : {
5544 : 28531 : case CONVERT_EXPR:
5545 : : /* This is used for unary plus, because a CONVERT_EXPR
5546 : : is enough to prevent anybody from looking inside for
5547 : : associativity, but won't generate any code. */
5548 : 28536 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5549 : 8 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5550 : 5 : || typecode == BITINT_TYPE
5551 : 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5552 : : {
5553 : 1 : error_at (location, "wrong type argument to unary plus");
5554 : 1 : return error_mark_node;
5555 : : }
5556 : 28530 : else if (!noconvert)
5557 : 28530 : arg = default_conversion (arg);
5558 : 28530 : arg = non_lvalue_loc (location, arg);
5559 : 28530 : break;
5560 : :
5561 : 6518789 : case NEGATE_EXPR:
5562 : 6955224 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5563 : 472938 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5564 : 441671 : || typecode == BITINT_TYPE
5565 : 436435 : || gnu_vector_type_p (TREE_TYPE (arg))))
5566 : : {
5567 : 1 : error_at (location, "wrong type argument to unary minus");
5568 : 1 : return error_mark_node;
5569 : : }
5570 : 6518788 : else if (!noconvert)
5571 : 6518782 : arg = default_conversion (arg);
5572 : : break;
5573 : :
5574 : 288674 : case BIT_NOT_EXPR:
5575 : : /* ~ works on integer types and non float vectors. */
5576 : 288674 : if (typecode == INTEGER_TYPE
5577 : 288674 : || typecode == BITINT_TYPE
5578 : 288674 : || (gnu_vector_type_p (TREE_TYPE (arg))
5579 : 2057 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5580 : : {
5581 : : tree e = arg;
5582 : :
5583 : : /* Warn if the expression has boolean value. */
5584 : 288075 : while (TREE_CODE (e) == COMPOUND_EXPR)
5585 : 11 : e = TREE_OPERAND (e, 1);
5586 : :
5587 : 576114 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5588 : 576114 : || truth_value_p (TREE_CODE (e))))
5589 : : {
5590 : 145 : auto_diagnostic_group d;
5591 : 145 : if (warning_at (location, OPT_Wbool_operation,
5592 : : "%<~%> on a boolean expression"))
5593 : : {
5594 : 12 : gcc_rich_location richloc (location);
5595 : 12 : richloc.add_fixit_insert_before (location, "!");
5596 : 12 : inform (&richloc, "did you mean to use logical not?");
5597 : 12 : }
5598 : 145 : }
5599 : 288064 : if (!noconvert)
5600 : 288061 : arg = default_conversion (arg);
5601 : : }
5602 : 610 : else if (typecode == COMPLEX_TYPE)
5603 : : {
5604 : 605 : code = CONJ_EXPR;
5605 : 605 : pedwarn (location, OPT_Wpedantic,
5606 : : "ISO C does not support %<~%> for complex conjugation");
5607 : 605 : if (!noconvert)
5608 : 605 : arg = default_conversion (arg);
5609 : : }
5610 : : else
5611 : : {
5612 : 5 : error_at (location, "wrong type argument to bit-complement");
5613 : 5 : return error_mark_node;
5614 : : }
5615 : : break;
5616 : :
5617 : 3 : case ABS_EXPR:
5618 : 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5619 : : {
5620 : 0 : error_at (location, "wrong type argument to abs");
5621 : 0 : return error_mark_node;
5622 : : }
5623 : 3 : else if (!noconvert)
5624 : 0 : arg = default_conversion (arg);
5625 : : break;
5626 : :
5627 : 7 : case ABSU_EXPR:
5628 : 7 : if (!(typecode == INTEGER_TYPE))
5629 : : {
5630 : 0 : error_at (location, "wrong type argument to absu");
5631 : 0 : return error_mark_node;
5632 : : }
5633 : 7 : else if (!noconvert)
5634 : 0 : arg = default_conversion (arg);
5635 : : break;
5636 : :
5637 : 0 : case CONJ_EXPR:
5638 : : /* Conjugating a real value is a no-op, but allow it anyway. */
5639 : 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5640 : : || typecode == COMPLEX_TYPE))
5641 : : {
5642 : 0 : error_at (location, "wrong type argument to conjugation");
5643 : 0 : return error_mark_node;
5644 : : }
5645 : 0 : else if (!noconvert)
5646 : 0 : arg = default_conversion (arg);
5647 : : break;
5648 : :
5649 : 374233 : case TRUTH_NOT_EXPR:
5650 : 374233 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5651 : 7011 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5652 : 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5653 : 18 : && typecode != BITINT_TYPE)
5654 : : {
5655 : 10 : error_at (location,
5656 : : "wrong type argument to unary exclamation mark");
5657 : 10 : return error_mark_node;
5658 : : }
5659 : 374223 : if (int_operands)
5660 : : {
5661 : 139609 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5662 : 139609 : arg = remove_c_maybe_const_expr (arg);
5663 : : }
5664 : : else
5665 : 234614 : arg = c_objc_common_truthvalue_conversion (location, arg);
5666 : 374223 : ret = invert_truthvalue_loc (location, arg);
5667 : : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5668 : 374223 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5669 : 234219 : location = EXPR_LOCATION (ret);
5670 : 374223 : goto return_build_unary_op;
5671 : :
5672 : 201157 : case REALPART_EXPR:
5673 : 201157 : case IMAGPART_EXPR:
5674 : 201157 : ret = build_real_imag_expr (location, code, arg);
5675 : 201157 : if (ret == error_mark_node)
5676 : : return error_mark_node;
5677 : 201149 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5678 : 2 : eptype = TREE_TYPE (eptype);
5679 : 201149 : goto return_build_unary_op;
5680 : :
5681 : 914082 : case PREINCREMENT_EXPR:
5682 : 914082 : case POSTINCREMENT_EXPR:
5683 : 914082 : case PREDECREMENT_EXPR:
5684 : 914082 : case POSTDECREMENT_EXPR:
5685 : :
5686 : 914082 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5687 : : {
5688 : 2 : tree inner = build_unary_op (location, code,
5689 : 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
5690 : : noconvert);
5691 : 2 : if (inner == error_mark_node)
5692 : : return error_mark_node;
5693 : 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5694 : 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
5695 : 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5696 : 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
5697 : 2 : goto return_build_unary_op;
5698 : : }
5699 : :
5700 : : /* Complain about anything that is not a true lvalue. In
5701 : : Objective-C, skip this check for property_refs. */
5702 : 914080 : if (!objc_is_property_ref (arg)
5703 : 1828160 : && !lvalue_or_else (location,
5704 : 914080 : arg, ((code == PREINCREMENT_EXPR
5705 : 914080 : || code == POSTINCREMENT_EXPR)
5706 : : ? lv_increment
5707 : : : lv_decrement)))
5708 : 22 : return error_mark_node;
5709 : :
5710 : 914058 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
5711 : : {
5712 : 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5713 : 2 : warning_at (location, OPT_Wc___compat,
5714 : : "increment of enumeration value is invalid in C++");
5715 : : else
5716 : 2 : warning_at (location, OPT_Wc___compat,
5717 : : "decrement of enumeration value is invalid in C++");
5718 : : }
5719 : :
5720 : 914058 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5721 : : {
5722 : 656 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5723 : 328 : warning_at (location, OPT_Wbool_operation,
5724 : : "increment of a boolean expression");
5725 : : else
5726 : 328 : warning_at (location, OPT_Wbool_operation,
5727 : : "decrement of a boolean expression");
5728 : : }
5729 : :
5730 : : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
5731 : 914058 : arg = c_fully_fold (arg, false, NULL, true);
5732 : :
5733 : 914058 : bool atomic_op;
5734 : 914058 : atomic_op = really_atomic_lvalue (arg);
5735 : :
5736 : : /* Increment or decrement the real part of the value,
5737 : : and don't change the imaginary part. */
5738 : 914058 : if (typecode == COMPLEX_TYPE)
5739 : : {
5740 : 57 : tree real, imag;
5741 : :
5742 : 57 : pedwarn_c23 (location, OPT_Wpedantic,
5743 : : "ISO C does not support %<++%> and %<--%> on complex "
5744 : : "types before C2Y");
5745 : :
5746 : 57 : if (!atomic_op)
5747 : : {
5748 : 53 : arg = stabilize_reference (arg);
5749 : 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
5750 : : true);
5751 : 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
5752 : : true);
5753 : 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
5754 : 53 : if (real == error_mark_node || imag == error_mark_node)
5755 : : return error_mark_node;
5756 : 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5757 : : real, imag);
5758 : 53 : goto return_build_unary_op;
5759 : : }
5760 : : }
5761 : :
5762 : : /* Report invalid types. */
5763 : :
5764 : 914005 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
5765 : 684986 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
5766 : 151 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
5767 : 914054 : && !gnu_vector_type_p (TREE_TYPE (arg)))
5768 : : {
5769 : 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5770 : 3 : error_at (location, "wrong type argument to increment");
5771 : : else
5772 : 2 : error_at (location, "wrong type argument to decrement");
5773 : :
5774 : 5 : return error_mark_node;
5775 : : }
5776 : :
5777 : 914000 : {
5778 : 914000 : tree inc;
5779 : :
5780 : 914000 : argtype = TREE_TYPE (arg);
5781 : :
5782 : : /* Compute the increment. */
5783 : :
5784 : 914000 : if (typecode == POINTER_TYPE)
5785 : : {
5786 : : /* If pointer target is an incomplete type,
5787 : : we just cannot know how to do the arithmetic. */
5788 : 229019 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
5789 : : {
5790 : 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5791 : 17 : error_at (location,
5792 : : "increment of pointer to an incomplete type %qT",
5793 : 17 : TREE_TYPE (argtype));
5794 : : else
5795 : 10 : error_at (location,
5796 : : "decrement of pointer to an incomplete type %qT",
5797 : 10 : TREE_TYPE (argtype));
5798 : : }
5799 : 228992 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
5800 : 228992 : || VOID_TYPE_P (TREE_TYPE (argtype)))
5801 : : {
5802 : 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5803 : 6 : pedwarn (location, OPT_Wpointer_arith,
5804 : : "wrong type argument to increment");
5805 : : else
5806 : 4 : pedwarn (location, OPT_Wpointer_arith,
5807 : : "wrong type argument to decrement");
5808 : : }
5809 : : else
5810 : 457964 : verify_type_context (location, TCTX_POINTER_ARITH,
5811 : 228982 : TREE_TYPE (argtype));
5812 : :
5813 : 229019 : inc = c_size_in_bytes (TREE_TYPE (argtype));
5814 : 229019 : inc = convert_to_ptrofftype_loc (location, inc);
5815 : : }
5816 : 684981 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
5817 : : {
5818 : : /* For signed fract types, we invert ++ to -- or
5819 : : -- to ++, and change inc from 1 to -1, because
5820 : : it is not possible to represent 1 in signed fract constants.
5821 : : For unsigned fract types, the result always overflows and
5822 : : we get an undefined (original) or the maximum value. */
5823 : 0 : if (code == PREINCREMENT_EXPR)
5824 : : code = PREDECREMENT_EXPR;
5825 : : else if (code == PREDECREMENT_EXPR)
5826 : : code = PREINCREMENT_EXPR;
5827 : : else if (code == POSTINCREMENT_EXPR)
5828 : : code = POSTDECREMENT_EXPR;
5829 : : else /* code == POSTDECREMENT_EXPR */
5830 : 0 : code = POSTINCREMENT_EXPR;
5831 : :
5832 : 0 : inc = integer_minus_one_node;
5833 : 0 : inc = convert (argtype, inc);
5834 : : }
5835 : : else
5836 : : {
5837 : 684937 : inc = VECTOR_TYPE_P (argtype)
5838 : 684981 : ? build_one_cst (argtype)
5839 : : : integer_one_node;
5840 : 684981 : inc = convert (argtype, inc);
5841 : : }
5842 : :
5843 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5844 : : need to ask Objective-C to build the increment or decrement
5845 : : expression for it. */
5846 : 914000 : if (objc_is_property_ref (arg))
5847 : 0 : return objc_build_incr_expr_for_property_ref (location, code,
5848 : 20 : arg, inc);
5849 : :
5850 : : /* Report a read-only lvalue. */
5851 : 914000 : if (TYPE_READONLY (argtype))
5852 : : {
5853 : 20 : readonly_error (location, arg,
5854 : 20 : ((code == PREINCREMENT_EXPR
5855 : 20 : || code == POSTINCREMENT_EXPR)
5856 : : ? lv_increment : lv_decrement));
5857 : 20 : return error_mark_node;
5858 : : }
5859 : 913980 : else if (TREE_READONLY (arg))
5860 : 4 : readonly_warning (arg,
5861 : 4 : ((code == PREINCREMENT_EXPR
5862 : 4 : || code == POSTINCREMENT_EXPR)
5863 : : ? lv_increment : lv_decrement));
5864 : :
5865 : : /* If the argument is atomic, use the special code sequences for
5866 : : atomic compound assignment. */
5867 : 913980 : if (atomic_op)
5868 : : {
5869 : 4469 : arg = stabilize_reference (arg);
5870 : 8938 : ret = build_atomic_assign (location, arg,
5871 : 4469 : ((code == PREINCREMENT_EXPR
5872 : 4469 : || code == POSTINCREMENT_EXPR)
5873 : : ? PLUS_EXPR
5874 : : : MINUS_EXPR),
5875 : 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
5876 : : ? inc
5877 : : : integer_one_node),
5878 : : (code == POSTINCREMENT_EXPR
5879 : 4469 : || code == POSTDECREMENT_EXPR));
5880 : 913980 : goto return_build_unary_op;
5881 : : }
5882 : :
5883 : 909511 : tree true_res;
5884 : 909511 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
5885 : : {
5886 : 364 : tree larg = stabilize_reference (arg);
5887 : 364 : tree sarg = save_expr (larg);
5888 : 364 : switch (code)
5889 : : {
5890 : 91 : case PREINCREMENT_EXPR:
5891 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
5892 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
5893 : 91 : break;
5894 : 91 : case POSTINCREMENT_EXPR:
5895 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
5896 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
5897 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
5898 : 91 : break;
5899 : 91 : case PREDECREMENT_EXPR:
5900 : 91 : {
5901 : 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
5902 : : true, true);
5903 : 91 : rarg = invert_truthvalue_loc (location, rarg);
5904 : 91 : rarg = convert (TREE_TYPE (sarg), rarg);
5905 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
5906 : : }
5907 : 91 : break;
5908 : 91 : case POSTDECREMENT_EXPR:
5909 : 91 : {
5910 : 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
5911 : : true, true);
5912 : 91 : rarg = invert_truthvalue_loc (location, rarg);
5913 : 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
5914 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
5915 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
5916 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
5917 : : }
5918 : 91 : break;
5919 : : default:
5920 : : gcc_unreachable ();
5921 : : }
5922 : 364 : TREE_SIDE_EFFECTS (val) = 1;
5923 : : }
5924 : 909147 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5925 : 56 : val = boolean_increment (code, arg);
5926 : : else
5927 : 909091 : val = build2 (code, TREE_TYPE (arg), arg, inc);
5928 : 909511 : TREE_SIDE_EFFECTS (val) = 1;
5929 : 909511 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
5930 : 3933 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
5931 : : TYPE_UNQUALIFIED);
5932 : 909511 : ret = val;
5933 : 909511 : goto return_build_unary_op;
5934 : : }
5935 : :
5936 : 50697541 : case ADDR_EXPR:
5937 : : /* Note that this operation never does default_conversion. */
5938 : :
5939 : : /* The operand of unary '&' must be an lvalue (which excludes
5940 : : expressions of type void), or, in C99, the result of a [] or
5941 : : unary '*' operator. */
5942 : 50697541 : if (VOID_TYPE_P (TREE_TYPE (arg))
5943 : 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
5944 : 50697560 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
5945 : 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
5946 : :
5947 : : /* Let &* cancel out to simplify resulting code. */
5948 : 50697541 : if (INDIRECT_REF_P (arg))
5949 : : {
5950 : : /* Don't let this be an lvalue. */
5951 : 21952 : if (lvalue_p (TREE_OPERAND (arg, 0)))
5952 : 16556 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
5953 : 5396 : ret = TREE_OPERAND (arg, 0);
5954 : 5396 : goto return_build_unary_op;
5955 : : }
5956 : :
5957 : : /* Anything not already handled and not a true memory reference
5958 : : or a non-lvalue array is an error. */
5959 : 50675589 : if (typecode != FUNCTION_TYPE && !noconvert
5960 : 50675589 : && !lvalue_or_else (location, arg, lv_addressof))
5961 : 17 : return error_mark_node;
5962 : :
5963 : : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
5964 : : folding later. */
5965 : 50675572 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5966 : : {
5967 : 1 : tree inner = build_unary_op (location, code,
5968 : 1 : C_MAYBE_CONST_EXPR_EXPR (arg),
5969 : : noconvert);
5970 : 2 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5971 : 1 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
5972 : 1 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5973 : 1 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
5974 : 1 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
5975 : 1 : goto return_build_unary_op;
5976 : : }
5977 : :
5978 : : /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to
5979 : : .ACCESS_WITH_SIZE. */
5980 : 50675571 : if (is_access_with_size_p (arg))
5981 : 0 : arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0);
5982 : :
5983 : 50675571 : argtype = TREE_TYPE (arg);
5984 : :
5985 : : /* If the lvalue is const or volatile, merge that into the type
5986 : : to which the address will point. This is only needed
5987 : : for function types. */
5988 : 50675571 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
5989 : 50190725 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
5990 : 79982916 : && TREE_CODE (argtype) == FUNCTION_TYPE)
5991 : : {
5992 : 29227644 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
5993 : 29227644 : int quals = orig_quals;
5994 : :
5995 : 29227644 : if (TREE_READONLY (arg))
5996 : 28856474 : quals |= TYPE_QUAL_CONST;
5997 : 29227644 : if (TREE_THIS_VOLATILE (arg))
5998 : 372004 : quals |= TYPE_QUAL_VOLATILE;
5999 : :
6000 : 29227644 : argtype = c_build_qualified_type (argtype, quals);
6001 : : }
6002 : :
6003 : 50675571 : switch (TREE_CODE (arg))
6004 : : {
6005 : 63544 : case COMPONENT_REF:
6006 : 63544 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6007 : : {
6008 : 8 : error_at (location, "cannot take address of bit-field %qD",
6009 : 8 : TREE_OPERAND (arg, 1));
6010 : 8 : return error_mark_node;
6011 : : }
6012 : :
6013 : : /* fall through */
6014 : :
6015 : 137639 : case ARRAY_REF:
6016 : 137639 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6017 : : {
6018 : 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6019 : 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6020 : 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6021 : : {
6022 : 6 : error_at (location, "cannot take address of scalar with "
6023 : : "reverse storage order");
6024 : 6 : return error_mark_node;
6025 : : }
6026 : :
6027 : 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6028 : 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6029 : 58 : warning_at (location, OPT_Wscalar_storage_order,
6030 : : "address of array with reverse scalar storage "
6031 : : "order requested");
6032 : : }
6033 : :
6034 : 50675557 : default:
6035 : 50675557 : break;
6036 : : }
6037 : :
6038 : 50675557 : if (!c_mark_addressable (arg))
6039 : 10 : return error_mark_node;
6040 : :
6041 : 50675547 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6042 : : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6043 : :
6044 : 50675547 : argtype = c_build_pointer_type (argtype);
6045 : :
6046 : : /* ??? Cope with user tricks that amount to offsetof. Delete this
6047 : : when we have proper support for integer constant expressions. */
6048 : 50675547 : val = get_base_address (arg);
6049 : 50675547 : if (val && INDIRECT_REF_P (val)
6050 : 50698187 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6051 : : {
6052 : 538 : ret = fold_offsetof (arg, argtype);
6053 : 538 : goto return_build_unary_op;
6054 : : }
6055 : :
6056 : 50675009 : val = build1 (ADDR_EXPR, argtype, arg);
6057 : :
6058 : 50675009 : ret = val;
6059 : 50675009 : goto return_build_unary_op;
6060 : :
6061 : 10 : case PAREN_EXPR:
6062 : 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6063 : 10 : goto return_build_unary_op;
6064 : :
6065 : 0 : default:
6066 : 0 : gcc_unreachable ();
6067 : : }
6068 : :
6069 : 6835997 : if (argtype == NULL_TREE)
6070 : 6835997 : argtype = TREE_TYPE (arg);
6071 : 6835997 : if (TREE_CODE (arg) == INTEGER_CST)
6072 : 6147693 : ret = (require_constant_value
6073 : 6147693 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6074 : 6129394 : : fold_build1_loc (location, code, argtype, arg));
6075 : : else
6076 : 688304 : ret = build1 (code, argtype, arg);
6077 : 59006358 : return_build_unary_op:
6078 : 59006358 : gcc_assert (ret != error_mark_node);
6079 : 6291737 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6080 : 65298086 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6081 : 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6082 : 59005796 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6083 : 74 : ret = note_integer_operands (ret);
6084 : 59006358 : if (eptype)
6085 : 235 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6086 : 59006358 : protected_set_expr_location (ret, location);
6087 : 59006358 : return ret;
6088 : : }
6089 : :
6090 : : /* Return nonzero if REF is an lvalue valid for this language.
6091 : : Lvalues can be assigned, unless their type has TYPE_READONLY.
6092 : : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6093 : :
6094 : : bool
6095 : 174758425 : lvalue_p (const_tree ref)
6096 : : {
6097 : 176238279 : const enum tree_code code = TREE_CODE (ref);
6098 : :
6099 : 176238279 : switch (code)
6100 : : {
6101 : 1455940 : case REALPART_EXPR:
6102 : 1455940 : case IMAGPART_EXPR:
6103 : 1455940 : case COMPONENT_REF:
6104 : 1455940 : return lvalue_p (TREE_OPERAND (ref, 0));
6105 : :
6106 : 23914 : case C_MAYBE_CONST_EXPR:
6107 : 23914 : return lvalue_p (TREE_OPERAND (ref, 1));
6108 : :
6109 : : case COMPOUND_LITERAL_EXPR:
6110 : : case STRING_CST:
6111 : : return true;
6112 : :
6113 : 25255284 : case MEM_REF:
6114 : 25255284 : case TARGET_MEM_REF:
6115 : : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6116 : : here as well. */
6117 : 25255284 : case INDIRECT_REF:
6118 : 25255284 : case ARRAY_REF:
6119 : 25255284 : case VAR_DECL:
6120 : 25255284 : case PARM_DECL:
6121 : 25255284 : case RESULT_DECL:
6122 : 25255284 : case ERROR_MARK:
6123 : 25255284 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6124 : 25255284 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6125 : :
6126 : 6 : case BIND_EXPR:
6127 : 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6128 : :
6129 : 6994711 : case CALL_EXPR:
6130 : 6994711 : return is_access_with_size_p (ref);
6131 : :
6132 : : default:
6133 : : return false;
6134 : : }
6135 : : }
6136 : :
6137 : : /* Give a warning for storing in something that is read-only in GCC
6138 : : terms but not const in ISO C terms. */
6139 : :
6140 : : static void
6141 : 5 : readonly_warning (tree arg, enum lvalue_use use)
6142 : : {
6143 : 5 : switch (use)
6144 : : {
6145 : 1 : case lv_assign:
6146 : 1 : warning (0, "assignment of read-only location %qE", arg);
6147 : 1 : break;
6148 : 2 : case lv_increment:
6149 : 2 : warning (0, "increment of read-only location %qE", arg);
6150 : 2 : break;
6151 : 2 : case lv_decrement:
6152 : 2 : warning (0, "decrement of read-only location %qE", arg);
6153 : 2 : break;
6154 : 0 : default:
6155 : 0 : gcc_unreachable ();
6156 : : }
6157 : 5 : return;
6158 : : }
6159 : :
6160 : :
6161 : : /* Return nonzero if REF is an lvalue valid for this language;
6162 : : otherwise, print an error message and return zero. USE says
6163 : : how the lvalue is being used and so selects the error message.
6164 : : LOCATION is the location at which any error should be reported. */
6165 : :
6166 : : static int
6167 : 4624457 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6168 : : {
6169 : 4624457 : int win = lvalue_p (ref);
6170 : :
6171 : 4624457 : if (!win)
6172 : 74 : lvalue_error (loc, use);
6173 : :
6174 : 4624457 : return win;
6175 : : }
6176 : :
6177 : : /* Mark EXP saying that we need to be able to take the
6178 : : address of it; it should not be allocated in a register.
6179 : : Returns true if successful. ARRAY_REF_P is true if this
6180 : : is for ARRAY_REF construction - in that case we don't want
6181 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6182 : : it is fine to use ARRAY_REFs for vector subscripts on vector
6183 : : register variables. */
6184 : :
6185 : : bool
6186 : 51208466 : c_mark_addressable (tree exp, bool array_ref_p)
6187 : : {
6188 : 51208466 : tree x = exp;
6189 : :
6190 : 51777483 : while (1)
6191 : 51777483 : switch (TREE_CODE (x))
6192 : : {
6193 : 9949 : case VIEW_CONVERT_EXPR:
6194 : 9949 : if (array_ref_p
6195 : 9841 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6196 : 19790 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6197 : : return true;
6198 : 108 : x = TREE_OPERAND (x, 0);
6199 : 108 : break;
6200 : :
6201 : 385025 : case COMPONENT_REF:
6202 : 385025 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6203 : : {
6204 : 6 : error ("cannot take address of bit-field %qD",
6205 : 3 : TREE_OPERAND (x, 1));
6206 : 3 : return false;
6207 : : }
6208 : : /* FALLTHRU */
6209 : 568909 : case ADDR_EXPR:
6210 : 568909 : case ARRAY_REF:
6211 : 568909 : case REALPART_EXPR:
6212 : 568909 : case IMAGPART_EXPR:
6213 : 568909 : x = TREE_OPERAND (x, 0);
6214 : 568909 : break;
6215 : :
6216 : 534 : case COMPOUND_LITERAL_EXPR:
6217 : 534 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6218 : : {
6219 : 2 : error ("address of register compound literal requested");
6220 : 2 : return false;
6221 : : }
6222 : 532 : TREE_ADDRESSABLE (x) = 1;
6223 : 532 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6224 : 532 : return true;
6225 : :
6226 : 0 : case CONSTRUCTOR:
6227 : 0 : TREE_ADDRESSABLE (x) = 1;
6228 : 0 : return true;
6229 : :
6230 : 1303625 : case VAR_DECL:
6231 : 1303625 : case CONST_DECL:
6232 : 1303625 : case PARM_DECL:
6233 : 1303625 : case RESULT_DECL:
6234 : 1303625 : if (C_DECL_REGISTER (x)
6235 : 1303625 : && DECL_NONLOCAL (x))
6236 : : {
6237 : 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6238 : : {
6239 : 0 : error
6240 : 0 : ("global register variable %qD used in nested function", x);
6241 : 0 : return false;
6242 : : }
6243 : 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6244 : : }
6245 : 1303625 : else if (C_DECL_REGISTER (x))
6246 : : {
6247 : 11 : if (TREE_PUBLIC (x) || is_global_var (x))
6248 : 4 : error ("address of global register variable %qD requested", x);
6249 : : else
6250 : 7 : error ("address of register variable %qD requested", x);
6251 : 11 : return false;
6252 : : }
6253 : :
6254 : : /* FALLTHRU */
6255 : 50639851 : case FUNCTION_DECL:
6256 : 50639851 : TREE_ADDRESSABLE (x) = 1;
6257 : : /* FALLTHRU */
6258 : : default:
6259 : : return true;
6260 : : }
6261 : : }
6262 : :
6263 : : /* Convert EXPR to TYPE, warning about conversion problems with
6264 : : constants. SEMANTIC_TYPE is the type this conversion would use
6265 : : without excess precision. If SEMANTIC_TYPE is NULL, this function
6266 : : is equivalent to convert_and_check. This function is a wrapper that
6267 : : handles conversions that may be different than
6268 : : the usual ones because of excess precision. */
6269 : :
6270 : : static tree
6271 : 23059472 : ep_convert_and_check (location_t loc, tree type, tree expr,
6272 : : tree semantic_type)
6273 : : {
6274 : 23059472 : if (TREE_TYPE (expr) == type)
6275 : : return expr;
6276 : :
6277 : : /* For C11, integer conversions may have results with excess
6278 : : precision. */
6279 : 2024827 : if (flag_isoc11 || !semantic_type)
6280 : 2024823 : return convert_and_check (loc, type, expr);
6281 : :
6282 : 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6283 : 4 : && TREE_TYPE (expr) != semantic_type)
6284 : : {
6285 : : /* For integers, we need to check the real conversion, not
6286 : : the conversion to the excess precision type. */
6287 : 4 : expr = convert_and_check (loc, semantic_type, expr);
6288 : : }
6289 : : /* Result type is the excess precision type, which should be
6290 : : large enough, so do not check. */
6291 : 4 : return convert (type, expr);
6292 : : }
6293 : :
6294 : : /* If EXPR refers to a built-in declared without a prototype returns
6295 : : the actual type of the built-in and, if non-null, set *BLTIN to
6296 : : a pointer to the built-in. Otherwise return the type of EXPR
6297 : : and clear *BLTIN if non-null. */
6298 : :
6299 : : static tree
6300 : 2791727 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6301 : : {
6302 : 2791727 : tree dummy;
6303 : 2791727 : if (!bltin)
6304 : 0 : bltin = &dummy;
6305 : :
6306 : 2791727 : *bltin = NULL_TREE;
6307 : :
6308 : 2791727 : tree type = TREE_TYPE (expr);
6309 : 2791727 : if (TREE_CODE (expr) != ADDR_EXPR)
6310 : : return type;
6311 : :
6312 : 207435 : tree oper = TREE_OPERAND (expr, 0);
6313 : 207435 : if (!DECL_P (oper)
6314 : 169312 : || TREE_CODE (oper) != FUNCTION_DECL
6315 : 243821 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6316 : : return type;
6317 : :
6318 : 2084 : built_in_function code = DECL_FUNCTION_CODE (oper);
6319 : 2084 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6320 : : return type;
6321 : :
6322 : 1977 : if ((*bltin = builtin_decl_implicit (code)))
6323 : 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6324 : :
6325 : : return type;
6326 : : }
6327 : :
6328 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6329 : : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6330 : : if folded to an integer constant then the unselected half may
6331 : : contain arbitrary operations not normally permitted in constant
6332 : : expressions. Set the location of the expression to LOC. */
6333 : :
6334 : : tree
6335 : 403141 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6336 : : tree op1, tree op1_original_type, location_t op1_loc,
6337 : : tree op2, tree op2_original_type, location_t op2_loc)
6338 : : {
6339 : 403141 : tree type1;
6340 : 403141 : tree type2;
6341 : 403141 : enum tree_code code1;
6342 : 403141 : enum tree_code code2;
6343 : 403141 : tree result_type = NULL;
6344 : 403141 : tree semantic_result_type = NULL;
6345 : 403141 : tree orig_op1 = op1, orig_op2 = op2;
6346 : 403141 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6347 : 403141 : bool ifexp_int_operands;
6348 : 403141 : tree ret;
6349 : :
6350 : 403141 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6351 : 115746 : if (op1_int_operands)
6352 : 115746 : op1 = remove_c_maybe_const_expr (op1);
6353 : 403141 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6354 : 148626 : if (op2_int_operands)
6355 : 148626 : op2 = remove_c_maybe_const_expr (op2);
6356 : 403141 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6357 : 242135 : if (ifexp_int_operands)
6358 : 242135 : ifexp = remove_c_maybe_const_expr (ifexp);
6359 : :
6360 : : /* Promote both alternatives. */
6361 : :
6362 : 403141 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6363 : 399246 : op1 = default_conversion (op1);
6364 : 403141 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6365 : 373323 : op2 = default_conversion (op2);
6366 : :
6367 : 403141 : if (TREE_CODE (ifexp) == ERROR_MARK
6368 : 403072 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6369 : 806199 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6370 : 83 : return error_mark_node;
6371 : :
6372 : 403058 : tree bltin1 = NULL_TREE;
6373 : 403058 : tree bltin2 = NULL_TREE;
6374 : 403058 : type1 = type_or_builtin_type (op1, &bltin1);
6375 : 403058 : code1 = TREE_CODE (type1);
6376 : 403058 : type2 = type_or_builtin_type (op2, &bltin2);
6377 : 403058 : code2 = TREE_CODE (type2);
6378 : :
6379 : 403058 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6380 : 1 : return error_mark_node;
6381 : :
6382 : 403057 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6383 : 1 : return error_mark_node;
6384 : :
6385 : : /* C90 does not permit non-lvalue arrays in conditional expressions.
6386 : : In C99 they will be pointers by now. */
6387 : 403056 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6388 : : {
6389 : 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6390 : 1 : return error_mark_node;
6391 : : }
6392 : :
6393 : 403055 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6394 : 403049 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6395 : 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6396 : 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6397 : 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6398 : 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6399 : : {
6400 : 7 : semantic_result_type = c_common_type (type1, type2);
6401 : 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6402 : : {
6403 : 6 : op1 = TREE_OPERAND (op1, 0);
6404 : 6 : type1 = TREE_TYPE (op1);
6405 : 6 : gcc_assert (TREE_CODE (type1) == code1);
6406 : : }
6407 : 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6408 : : {
6409 : 1 : op2 = TREE_OPERAND (op2, 0);
6410 : 1 : type2 = TREE_TYPE (op2);
6411 : 1 : gcc_assert (TREE_CODE (type2) == code2);
6412 : : }
6413 : : }
6414 : :
6415 : 403055 : if (warn_cxx_compat)
6416 : : {
6417 : 416 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6418 : 416 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6419 : :
6420 : 416 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6421 : 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6422 : 422 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6423 : 2 : warning_at (colon_loc, OPT_Wc___compat,
6424 : : ("different enum types in conditional is "
6425 : : "invalid in C++: %qT vs %qT"),
6426 : : t1, t2);
6427 : : }
6428 : :
6429 : 403055 : if (warn_zero_as_null_pointer_constant
6430 : 14 : && c_inhibit_evaluation_warnings == 0)
6431 : : {
6432 : 14 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6433 : 14 : && code2 == INTEGER_TYPE && null_pointer_constant_p (orig_op2))
6434 : 2 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6435 : : "zero as null pointer constant");
6436 : :
6437 : 14 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6438 : 14 : && code1 == INTEGER_TYPE && null_pointer_constant_p (orig_op1))
6439 : 2 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6440 : : "zero as null pointer constant");
6441 : : }
6442 : :
6443 : : /* Quickly detect the usual case where op1 and op2 have the same type
6444 : : after promotion. */
6445 : 403055 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6446 : : {
6447 : 269969 : if (type1 == type2)
6448 : : result_type = type1;
6449 : : else
6450 : 6257 : result_type = TYPE_MAIN_VARIANT (type1);
6451 : : }
6452 : 133086 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6453 : 79891 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6454 : 53240 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6455 : 26148 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6456 : : {
6457 : : /* In C11, a conditional expression between a floating-point
6458 : : type and an integer type should convert the integer type to
6459 : : the evaluation format of the floating-point type, with
6460 : : possible excess precision. */
6461 : 27191 : tree eptype1 = type1;
6462 : 27191 : tree eptype2 = type2;
6463 : 27191 : if (flag_isoc11)
6464 : : {
6465 : 26879 : tree eptype;
6466 : 10834 : if (ANY_INTEGRAL_TYPE_P (type1)
6467 : 26879 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6468 : : {
6469 : 2 : eptype2 = eptype;
6470 : 2 : if (!semantic_result_type)
6471 : 2 : semantic_result_type = c_common_type (type1, type2);
6472 : : }
6473 : 369 : else if (ANY_INTEGRAL_TYPE_P (type2)
6474 : 26877 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6475 : : {
6476 : 4618 : eptype1 = eptype;
6477 : 4618 : if (!semantic_result_type)
6478 : 4618 : semantic_result_type = c_common_type (type1, type2);
6479 : : }
6480 : : }
6481 : 27191 : result_type = c_common_type (eptype1, eptype2);
6482 : 27191 : if (result_type == error_mark_node)
6483 : : return error_mark_node;
6484 : 27189 : do_warn_double_promotion (result_type, type1, type2,
6485 : : "implicit conversion from %qT to %qT to "
6486 : : "match other result of conditional",
6487 : : colon_loc);
6488 : :
6489 : : /* If -Wsign-compare, warn here if type1 and type2 have
6490 : : different signedness. We'll promote the signed to unsigned
6491 : : and later code won't know it used to be different.
6492 : : Do this check on the original types, so that explicit casts
6493 : : will be considered, but default promotions won't. */
6494 : 27189 : if (c_inhibit_evaluation_warnings == 0)
6495 : : {
6496 : 26998 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6497 : 26998 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6498 : :
6499 : 26998 : if (unsigned_op1 ^ unsigned_op2)
6500 : : {
6501 : 12589 : bool ovf;
6502 : :
6503 : : /* Do not warn if the result type is signed, since the
6504 : : signed type will only be chosen if it can represent
6505 : : all the values of the unsigned type. */
6506 : 12589 : if (!TYPE_UNSIGNED (result_type))
6507 : : /* OK */;
6508 : : else
6509 : : {
6510 : 12485 : bool op1_maybe_const = true;
6511 : 12485 : bool op2_maybe_const = true;
6512 : :
6513 : : /* Do not warn if the signed quantity is an
6514 : : unsuffixed integer literal (or some static
6515 : : constant expression involving such literals) and
6516 : : it is non-negative. This warning requires the
6517 : : operands to be folded for best results, so do
6518 : : that folding in this case even without
6519 : : warn_sign_compare to avoid warning options
6520 : : possibly affecting code generation. */
6521 : 12485 : c_inhibit_evaluation_warnings
6522 : 12485 : += (ifexp == truthvalue_false_node);
6523 : 12485 : op1 = c_fully_fold (op1, require_constant_value,
6524 : : &op1_maybe_const);
6525 : 12485 : c_inhibit_evaluation_warnings
6526 : 12485 : -= (ifexp == truthvalue_false_node);
6527 : :
6528 : 12485 : c_inhibit_evaluation_warnings
6529 : 12485 : += (ifexp == truthvalue_true_node);
6530 : 12485 : op2 = c_fully_fold (op2, require_constant_value,
6531 : : &op2_maybe_const);
6532 : 12485 : c_inhibit_evaluation_warnings
6533 : 12485 : -= (ifexp == truthvalue_true_node);
6534 : :
6535 : 12485 : if (warn_sign_compare)
6536 : : {
6537 : 1241 : if ((unsigned_op2
6538 : 681 : && tree_expr_nonnegative_warnv_p (op1, &ovf))
6539 : 1244 : || (unsigned_op1
6540 : 560 : && tree_expr_nonnegative_warnv_p (op2, &ovf)))
6541 : : /* OK */;
6542 : 10 : else if (unsigned_op2)
6543 : 3 : warning_at (op1_loc, OPT_Wsign_compare,
6544 : : "operand of %<?:%> changes signedness from "
6545 : : "%qT to %qT due to unsignedness of other "
6546 : 3 : "operand", TREE_TYPE (orig_op1),
6547 : 3 : TREE_TYPE (orig_op2));
6548 : : else
6549 : 7 : warning_at (op2_loc, OPT_Wsign_compare,
6550 : : "operand of %<?:%> changes signedness from "
6551 : : "%qT to %qT due to unsignedness of other "
6552 : 7 : "operand", TREE_TYPE (orig_op2),
6553 : 7 : TREE_TYPE (orig_op1));
6554 : : }
6555 : 12485 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6556 : 7145 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6557 : 12485 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6558 : 4375 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6559 : : }
6560 : : }
6561 : : }
6562 : : }
6563 : 105895 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6564 : : {
6565 : 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6566 : 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6567 : : "ISO C forbids conditional expr with only one void side");
6568 : 25995 : result_type = void_type_node;
6569 : : }
6570 : 79900 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6571 : : {
6572 : 79509 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6573 : 79509 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6574 : 79509 : addr_space_t as_common;
6575 : :
6576 : 79509 : if (comp_target_types (colon_loc, type1, type2))
6577 : : {
6578 : 2600 : ifexp = save_expr (ifexp);
6579 : 2600 : result_type = common_pointer_type (type1, type2, ifexp);
6580 : : }
6581 : 76909 : else if (null_pointer_constant_p (orig_op1))
6582 : : result_type = type2;
6583 : 72747 : else if (null_pointer_constant_p (orig_op2))
6584 : : result_type = type1;
6585 : 34799 : else if (!addr_space_superset (as1, as2, &as_common))
6586 : : {
6587 : 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6588 : : "used in conditional expression");
6589 : 0 : return error_mark_node;
6590 : : }
6591 : 34799 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6592 : 326 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6593 : 34801 : || (VOID_TYPE_P (TREE_TYPE (type2))
6594 : 34428 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6595 : : {
6596 : 34751 : tree t1 = TREE_TYPE (type1);
6597 : 34751 : tree t2 = TREE_TYPE (type2);
6598 : 34751 : if (!(VOID_TYPE_P (t1)
6599 : 325 : && !TYPE_ATOMIC (t1)))
6600 : : {
6601 : : /* roles are swapped */
6602 : 34427 : t1 = t2;
6603 : 34427 : t2 = TREE_TYPE (type1);
6604 : : }
6605 : 34751 : tree t2_stripped = strip_array_types (t2);
6606 : 34751 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6607 : 34751 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6608 : : {
6609 : 18 : if (!flag_isoc23)
6610 : 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6611 : : "pointer to array loses qualifier "
6612 : : "in conditional expression");
6613 : 12 : else if (warn_c11_c23_compat > 0)
6614 : 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6615 : : "pointer to array loses qualifier "
6616 : : "in conditional expression in ISO C before C23");
6617 : : }
6618 : 34751 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6619 : 4 : pedwarn (colon_loc, OPT_Wpedantic,
6620 : : "ISO C forbids conditional expr between "
6621 : : "%<void *%> and function pointer");
6622 : : /* for array, use qualifiers of element type */
6623 : 34751 : if (flag_isoc23)
6624 : 11002 : t2 = t2_stripped;
6625 : 34751 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6626 : : }
6627 : : /* Objective-C pointer comparisons are a bit more lenient. */
6628 : 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6629 : 0 : result_type = objc_common_type (type1, type2);
6630 : : else
6631 : : {
6632 : 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6633 : 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6634 : 48 : if (!flag_isoc99)
6635 : : /* This downgrade to a warning ensures that -std=gnu89
6636 : : -pedantic-errors does not flag these mismatches between
6637 : : builtins as errors (as diagnostics::kind::permerror would)
6638 : : ISO C99 and later do not have implicit function declarations,
6639 : : so the mismatch cannot occur naturally there. */
6640 : 5 : kind = (bltin1 && bltin2
6641 : 5 : ? diagnostics::kind::warning
6642 : : : diagnostics::kind::pedwarn);
6643 : 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6644 : : "pointer type mismatch "
6645 : : "in conditional expression"))
6646 : : {
6647 : 46 : inform (op1_loc, "first expression has type %qT", type1);
6648 : 46 : inform (op2_loc, "second expression has type %qT", type2);
6649 : : }
6650 : 48 : result_type = c_build_pointer_type
6651 : 48 : (c_build_qualified_type (void_type_node, qual));
6652 : : }
6653 : : }
6654 : 391 : else if (code1 == POINTER_TYPE
6655 : 274 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6656 : : {
6657 : 270 : if (!null_pointer_constant_p (orig_op2))
6658 : 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6659 : : "pointer/integer type mismatch "
6660 : : "in conditional expression");
6661 : : else
6662 : : {
6663 : 256 : op2 = null_pointer_node;
6664 : : }
6665 : : result_type = type1;
6666 : : }
6667 : 121 : else if (code2 == POINTER_TYPE
6668 : 87 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6669 : : {
6670 : 83 : if (!null_pointer_constant_p (orig_op1))
6671 : 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6672 : : "pointer/integer type mismatch "
6673 : : "in conditional expression");
6674 : : else
6675 : : {
6676 : 69 : op1 = null_pointer_node;
6677 : : }
6678 : : result_type = type2;
6679 : : }
6680 : : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6681 : : type nullptr_t and the other is a pointer, the result type is the pointer
6682 : : type." */
6683 : 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
6684 : : result_type = type2;
6685 : 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
6686 : : result_type = type1;
6687 : 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
6688 : 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
6689 : 8 : TYPE_MAIN_VARIANT (type2)))
6690 : 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
6691 : 8 : TYPE_MAIN_VARIANT (type2));
6692 : :
6693 : 403031 : if (!result_type)
6694 : : {
6695 : 22 : if (flag_cond_mismatch)
6696 : 0 : result_type = void_type_node;
6697 : : else
6698 : : {
6699 : 22 : error_at (colon_loc, "type mismatch in conditional expression");
6700 : 22 : return error_mark_node;
6701 : : }
6702 : : }
6703 : :
6704 : : /* Merge const and volatile flags of the incoming types. */
6705 : 403031 : result_type
6706 : 403031 : = build_type_variant (result_type,
6707 : : TYPE_READONLY (type1) || TYPE_READONLY (type2),
6708 : : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6709 : :
6710 : 403031 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
6711 : : semantic_result_type);
6712 : 403031 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
6713 : : semantic_result_type);
6714 : :
6715 : 403031 : if (ifexp_bcp && ifexp == truthvalue_true_node)
6716 : : {
6717 : 10 : op2_int_operands = true;
6718 : 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
6719 : : }
6720 : 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
6721 : : {
6722 : 49 : op1_int_operands = true;
6723 : 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
6724 : : }
6725 : 903112 : int_const = int_operands = (ifexp_int_operands
6726 : 403031 : && op1_int_operands
6727 : 403031 : && op2_int_operands);
6728 : 97050 : if (int_operands)
6729 : : {
6730 : 97050 : int_const = ((ifexp == truthvalue_true_node
6731 : 58635 : && TREE_CODE (orig_op1) == INTEGER_CST
6732 : 58621 : && !TREE_OVERFLOW (orig_op1))
6733 : 97064 : || (ifexp == truthvalue_false_node
6734 : 38369 : && TREE_CODE (orig_op2) == INTEGER_CST
6735 : 38353 : && !TREE_OVERFLOW (orig_op2)));
6736 : : }
6737 : :
6738 : : /* Need to convert condition operand into a vector mask. */
6739 : 403031 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
6740 : : {
6741 : 0 : tree vectype = TREE_TYPE (ifexp);
6742 : 0 : tree elem_type = TREE_TYPE (vectype);
6743 : 0 : tree zero = build_int_cst (elem_type, 0);
6744 : 0 : tree zero_vec = build_vector_from_val (vectype, zero);
6745 : 0 : tree cmp_type = truth_type_for (vectype);
6746 : 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
6747 : : }
6748 : :
6749 : 403031 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
6750 : 97006 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
6751 : : else
6752 : : {
6753 : 306025 : if (int_operands)
6754 : : {
6755 : : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
6756 : : nested inside of the expression. */
6757 : 76 : op1 = c_fully_fold (op1, false, NULL);
6758 : 76 : op2 = c_fully_fold (op2, false, NULL);
6759 : : }
6760 : 306025 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
6761 : 306025 : if (int_operands)
6762 : 76 : ret = note_integer_operands (ret);
6763 : : }
6764 : 403031 : if (semantic_result_type)
6765 : 4627 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
6766 : :
6767 : 403031 : protected_set_expr_location (ret, colon_loc);
6768 : :
6769 : : /* If the OP1 and OP2 are the same and don't have side-effects,
6770 : : warn here, because the COND_EXPR will be turned into OP1. */
6771 : 403031 : if (warn_duplicated_branches
6772 : 34 : && TREE_CODE (ret) == COND_EXPR
6773 : 403065 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
6774 : 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
6775 : : "this condition has identical branches");
6776 : :
6777 : : return ret;
6778 : : }
6779 : :
6780 : : /* EXPR is an expression, location LOC, whose result is discarded.
6781 : : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
6782 : : whose right-hand operand is such a call, possibly recursively). */
6783 : :
6784 : : static void
6785 : 6353172 : maybe_warn_nodiscard (location_t loc, tree expr)
6786 : : {
6787 : 6353172 : if (VOID_TYPE_P (TREE_TYPE (expr)))
6788 : : return;
6789 : 3879666 : while (TREE_CODE (expr) == COMPOUND_EXPR)
6790 : : {
6791 : 48968 : expr = TREE_OPERAND (expr, 1);
6792 : 48968 : if (EXPR_HAS_LOCATION (expr))
6793 : 31875 : loc = EXPR_LOCATION (expr);
6794 : : }
6795 : 3830698 : if (TREE_CODE (expr) != CALL_EXPR)
6796 : : return;
6797 : 342516 : tree fn = CALL_EXPR_FN (expr);
6798 : 342516 : if (!fn)
6799 : : return;
6800 : 342503 : tree attr;
6801 : 342503 : if (TREE_CODE (fn) == ADDR_EXPR
6802 : 341838 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6803 : 684341 : && (attr = lookup_attribute ("nodiscard",
6804 : 341838 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
6805 : : {
6806 : 3 : fn = TREE_OPERAND (fn, 0);
6807 : 3 : tree args = TREE_VALUE (attr);
6808 : 3 : if (args)
6809 : 1 : args = TREE_VALUE (args);
6810 : 3 : auto_diagnostic_group d;
6811 : 3 : auto_urlify_attributes sentinel;
6812 : 3 : int warned;
6813 : 3 : if (args)
6814 : 1 : warned = warning_at (loc, OPT_Wunused_result,
6815 : : "ignoring return value of %qD, declared with "
6816 : : "attribute %<nodiscard%>: %E", fn, args);
6817 : : else
6818 : 2 : warned = warning_at (loc, OPT_Wunused_result,
6819 : : "ignoring return value of %qD, declared with "
6820 : : "attribute %<nodiscard%>", fn);
6821 : 3 : if (warned)
6822 : 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
6823 : 3 : }
6824 : : else
6825 : : {
6826 : 342500 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
6827 : 342500 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
6828 : 342500 : if (!attr)
6829 : 342488 : return;
6830 : 12 : tree args = TREE_VALUE (attr);
6831 : 12 : if (args)
6832 : 5 : args = TREE_VALUE (args);
6833 : 12 : auto_diagnostic_group d;
6834 : 12 : auto_urlify_attributes sentinel;
6835 : 12 : int warned;
6836 : 12 : if (args)
6837 : 5 : warned = warning_at (loc, OPT_Wunused_result,
6838 : : "ignoring return value of type %qT, declared "
6839 : : "with attribute %<nodiscard%>: %E",
6840 : : rettype, args);
6841 : : else
6842 : 7 : warned = warning_at (loc, OPT_Wunused_result,
6843 : : "ignoring return value of type %qT, declared "
6844 : : "with attribute %<nodiscard%>", rettype);
6845 : 12 : if (warned)
6846 : : {
6847 : 12 : if (TREE_CODE (fn) == ADDR_EXPR)
6848 : : {
6849 : 11 : fn = TREE_OPERAND (fn, 0);
6850 : 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
6851 : 11 : inform (DECL_SOURCE_LOCATION (fn),
6852 : : "in call to %qD, declared here", fn);
6853 : : }
6854 : : }
6855 : 12 : }
6856 : : }
6857 : :
6858 : : /* Return a compound expression that performs two expressions and
6859 : : returns the value of the second of them.
6860 : :
6861 : : LOC is the location of the COMPOUND_EXPR. */
6862 : :
6863 : : tree
6864 : 111143 : build_compound_expr (location_t loc, tree expr1, tree expr2)
6865 : : {
6866 : 111143 : bool expr1_int_operands, expr2_int_operands;
6867 : 111143 : tree eptype = NULL_TREE;
6868 : 111143 : tree ret;
6869 : :
6870 : 111143 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
6871 : 325 : if (expr1_int_operands)
6872 : 325 : expr1 = remove_c_maybe_const_expr (expr1);
6873 : 111143 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
6874 : 66079 : if (expr2_int_operands)
6875 : 66079 : expr2 = remove_c_maybe_const_expr (expr2);
6876 : :
6877 : 111143 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
6878 : 0 : expr1 = TREE_OPERAND (expr1, 0);
6879 : 111143 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
6880 : : {
6881 : 1 : eptype = TREE_TYPE (expr2);
6882 : 1 : expr2 = TREE_OPERAND (expr2, 0);
6883 : : }
6884 : :
6885 : 111143 : if (!TREE_SIDE_EFFECTS (expr1))
6886 : : {
6887 : : /* The left-hand operand of a comma expression is like an expression
6888 : : statement: with -Wunused, we should warn if it doesn't have
6889 : : any side-effects, unless it was explicitly cast to (void). */
6890 : 8295 : if (warn_unused_value)
6891 : : {
6892 : 1241 : if (VOID_TYPE_P (TREE_TYPE (expr1))
6893 : 1241 : && CONVERT_EXPR_P (expr1))
6894 : : ; /* (void) a, b */
6895 : 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
6896 : 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
6897 : 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
6898 : : ; /* (void) a, (void) b, c */
6899 : : else
6900 : 11 : warning_at (loc, OPT_Wunused_value,
6901 : : "left-hand operand of comma expression has no effect");
6902 : : }
6903 : : }
6904 : 102848 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
6905 : 13104 : && warn_unused_value)
6906 : : {
6907 : : tree r = expr1;
6908 : : location_t cloc = loc;
6909 : 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
6910 : : {
6911 : 2417 : if (EXPR_HAS_LOCATION (r))
6912 : 2417 : cloc = EXPR_LOCATION (r);
6913 : 2417 : r = TREE_OPERAND (r, 1);
6914 : : }
6915 : 2416 : if (!TREE_SIDE_EFFECTS (r)
6916 : 4 : && !VOID_TYPE_P (TREE_TYPE (r))
6917 : 2420 : && !CONVERT_EXPR_P (r))
6918 : 4 : warning_at (cloc, OPT_Wunused_value,
6919 : : "right-hand operand of comma expression has no effect");
6920 : : }
6921 : :
6922 : : /* With -Wunused, we should also warn if the left-hand operand does have
6923 : : side-effects, but computes a value which is not used. For example, in
6924 : : `foo() + bar(), baz()' the result of the `+' operator is not used,
6925 : : so we should issue a warning. */
6926 : 100432 : else if (warn_unused_value)
6927 : 35380 : warn_if_unused_value (expr1, loc);
6928 : :
6929 : 111143 : maybe_warn_nodiscard (loc, expr1);
6930 : :
6931 : 111143 : if (expr2 == error_mark_node)
6932 : : return error_mark_node;
6933 : :
6934 : 111130 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
6935 : :
6936 : 111130 : if (flag_isoc99
6937 : : && expr1_int_operands
6938 : 110674 : && expr2_int_operands)
6939 : 152 : ret = note_integer_operands (ret);
6940 : :
6941 : 111130 : if (eptype)
6942 : 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6943 : :
6944 : 111130 : protected_set_expr_location (ret, loc);
6945 : 111130 : return ret;
6946 : : }
6947 : :
6948 : : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
6949 : : which we are casting. OTYPE is the type of the expression being
6950 : : cast. Both TYPE and OTYPE are pointer types. LOC is the location
6951 : : of the cast. -Wcast-qual appeared on the command line. Named
6952 : : address space qualifiers are not handled here, because they result
6953 : : in different warnings. */
6954 : :
6955 : : static void
6956 : 13405 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
6957 : : {
6958 : 13405 : tree in_type = type;
6959 : 13405 : tree in_otype = otype;
6960 : 13405 : int added = 0;
6961 : 13405 : int discarded = 0;
6962 : 13575 : bool is_const;
6963 : :
6964 : : /* Check that the qualifiers on IN_TYPE are a superset of the
6965 : : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
6966 : : nodes is uninteresting and we stop as soon as we hit a
6967 : : non-POINTER_TYPE node on either type. */
6968 : 13575 : do
6969 : : {
6970 : 13575 : in_otype = TREE_TYPE (in_otype);
6971 : 13575 : in_type = TREE_TYPE (in_type);
6972 : :
6973 : : /* GNU C allows cv-qualified function types. 'const' means the
6974 : : function is very pure, 'volatile' means it can't return. We
6975 : : need to warn when such qualifiers are added, not when they're
6976 : : taken away. */
6977 : 13575 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
6978 : 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
6979 : 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
6980 : 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
6981 : : else
6982 : 13557 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
6983 : 13557 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
6984 : : }
6985 : 13575 : while (TREE_CODE (in_type) == POINTER_TYPE
6986 : 13575 : && TREE_CODE (in_otype) == POINTER_TYPE);
6987 : :
6988 : 13405 : if (added)
6989 : 2 : warning_at (loc, OPT_Wcast_qual,
6990 : : "cast adds %q#v qualifier to function type", added);
6991 : :
6992 : 13405 : if (discarded)
6993 : : /* There are qualifiers present in IN_OTYPE that are not present
6994 : : in IN_TYPE. */
6995 : 1117 : warning_at (loc, OPT_Wcast_qual,
6996 : : "cast discards %qv qualifier from pointer target type",
6997 : : discarded);
6998 : :
6999 : 13405 : if (added || discarded)
7000 : : return;
7001 : :
7002 : : /* A cast from **T to const **T is unsafe, because it can cause a
7003 : : const value to be changed with no additional warning. We only
7004 : : issue this warning if T is the same on both sides, and we only
7005 : : issue the warning if there are the same number of pointers on
7006 : : both sides, as otherwise the cast is clearly unsafe anyhow. A
7007 : : cast is unsafe when a qualifier is added at one level and const
7008 : : is not present at all outer levels.
7009 : :
7010 : : To issue this warning, we check at each level whether the cast
7011 : : adds new qualifiers not already seen. We don't need to special
7012 : : case function types, as they won't have the same
7013 : : TYPE_MAIN_VARIANT. */
7014 : :
7015 : 12286 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7016 : : return;
7017 : 51 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7018 : : return;
7019 : :
7020 : 48 : in_type = type;
7021 : 48 : in_otype = otype;
7022 : 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7023 : 130 : do
7024 : : {
7025 : 130 : in_type = TREE_TYPE (in_type);
7026 : 130 : in_otype = TREE_TYPE (in_otype);
7027 : 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7028 : 130 : && !is_const)
7029 : : {
7030 : 27 : warning_at (loc, OPT_Wcast_qual,
7031 : : "to be safe all intermediate pointers in cast from "
7032 : : "%qT to %qT must be %<const%> qualified",
7033 : : otype, type);
7034 : 27 : break;
7035 : : }
7036 : 103 : if (is_const)
7037 : 72 : is_const = TYPE_READONLY (in_type);
7038 : : }
7039 : 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7040 : : }
7041 : :
7042 : : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7043 : :
7044 : : static bool
7045 : 1081 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7046 : : {
7047 : 1081 : if (error_operand_p (t1) || error_operand_p (t2))
7048 : : return true;
7049 : :
7050 : 1080 : t1 = TYPE_MAIN_VARIANT (t1);
7051 : 1080 : t2 = TYPE_MAIN_VARIANT (t2);
7052 : :
7053 : 1080 : if (TREE_CODE (t1) == POINTER_TYPE
7054 : 114 : && TREE_CODE (t2) == POINTER_TYPE)
7055 : : return true;
7056 : :
7057 : : /* Only the precision of the parameter matters. This check should
7058 : : make sure that the callee does not see undefined values in argument
7059 : : registers. */
7060 : 988 : if (INTEGRAL_TYPE_P (t1)
7061 : 869 : && INTEGRAL_TYPE_P (t2)
7062 : 1289 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7063 : : return true;
7064 : :
7065 : 867 : return comptypes (t1, t2);
7066 : : }
7067 : :
7068 : : /* Check if a type cast between two function types can be considered safe. */
7069 : :
7070 : : static bool
7071 : 7434 : c_safe_function_type_cast_p (tree t1, tree t2)
7072 : : {
7073 : 7434 : if (TREE_TYPE (t1) == void_type_node &&
7074 : 5451 : TYPE_ARG_TYPES (t1) == void_list_node)
7075 : : return true;
7076 : :
7077 : 3679 : if (TREE_TYPE (t2) == void_type_node &&
7078 : 2862 : TYPE_ARG_TYPES (t2) == void_list_node)
7079 : : return true;
7080 : :
7081 : 849 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7082 : : return false;
7083 : :
7084 : 153 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7085 : 281 : t1 && t2;
7086 : 128 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7087 : 232 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7088 : : return false;
7089 : :
7090 : : return true;
7091 : : }
7092 : :
7093 : : /* Build an expression representing a cast to type TYPE of expression EXPR.
7094 : : LOC is the location of the cast-- typically the open paren of the cast. */
7095 : :
7096 : : tree
7097 : 117075979 : build_c_cast (location_t loc, tree type, tree expr)
7098 : : {
7099 : 117075979 : tree value;
7100 : :
7101 : 117075979 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7102 : :
7103 : 117075979 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7104 : 66 : expr = TREE_OPERAND (expr, 0);
7105 : :
7106 : 117075979 : value = expr;
7107 : 117075979 : if (int_operands)
7108 : 6707003 : value = remove_c_maybe_const_expr (value);
7109 : :
7110 : 117075979 : if (type == error_mark_node || expr == error_mark_node)
7111 : : return error_mark_node;
7112 : :
7113 : : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7114 : : only in <protocol> qualifications. But when constructing cast expressions,
7115 : : the protocols do matter and must be kept around. */
7116 : 117074958 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7117 : 0 : return build1 (NOP_EXPR, type, expr);
7118 : :
7119 : 117074958 : type = TYPE_MAIN_VARIANT (type);
7120 : :
7121 : 117074958 : if (TREE_CODE (type) == ARRAY_TYPE)
7122 : : {
7123 : 13 : error_at (loc, "cast specifies array type");
7124 : 13 : return error_mark_node;
7125 : : }
7126 : :
7127 : 117074945 : if (TREE_CODE (type) == FUNCTION_TYPE)
7128 : : {
7129 : 6 : error_at (loc, "cast specifies function type");
7130 : 6 : return error_mark_node;
7131 : : }
7132 : :
7133 : 117074939 : if (!VOID_TYPE_P (type))
7134 : : {
7135 : 117030968 : value = require_complete_type (loc, value);
7136 : 117030968 : if (value == error_mark_node)
7137 : : return error_mark_node;
7138 : : }
7139 : :
7140 : 117074939 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7141 : : {
7142 : 17621573 : if (RECORD_OR_UNION_TYPE_P (type)
7143 : 17621573 : && pedwarn (loc, OPT_Wpedantic,
7144 : : "ISO C forbids casting nonscalar to the same type"))
7145 : : ;
7146 : 17621569 : else if (warn_useless_cast)
7147 : 7 : warning_at (loc, OPT_Wuseless_cast,
7148 : : "useless cast to type %qT", type);
7149 : :
7150 : : /* Convert to remove any qualifiers from VALUE's type. */
7151 : 17621573 : value = convert (type, value);
7152 : : }
7153 : 99453366 : else if (TREE_CODE (type) == UNION_TYPE)
7154 : : {
7155 : 128 : tree field;
7156 : :
7157 : 182 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7158 : 176 : if (TREE_TYPE (field) != error_mark_node
7159 : 351 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7160 : 175 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7161 : : break;
7162 : :
7163 : 128 : if (field)
7164 : : {
7165 : 122 : tree t;
7166 : 122 : bool maybe_const = true;
7167 : :
7168 : 122 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7169 : 122 : t = c_fully_fold (value, false, &maybe_const);
7170 : 122 : t = build_constructor_single (type, field, t);
7171 : 122 : if (!maybe_const)
7172 : 8 : t = c_wrap_maybe_const (t, true);
7173 : 122 : t = digest_init (loc, field, type, t,
7174 : : NULL_TREE, false, false, false, true, false, false);
7175 : 122 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7176 : 122 : return t;
7177 : : }
7178 : 6 : error_at (loc, "cast to union type from type not present in union");
7179 : 6 : return error_mark_node;
7180 : : }
7181 : : else
7182 : : {
7183 : 99453238 : tree otype, ovalue;
7184 : :
7185 : 99453238 : if (type == void_type_node)
7186 : : {
7187 : 18015 : tree t = build1 (CONVERT_EXPR, type, value);
7188 : 18015 : SET_EXPR_LOCATION (t, loc);
7189 : 18015 : return t;
7190 : : }
7191 : :
7192 : 99435223 : otype = TREE_TYPE (value);
7193 : :
7194 : : /* Optionally warn about potentially worrisome casts. */
7195 : 99435223 : if (warn_cast_qual
7196 : 179313 : && TREE_CODE (type) == POINTER_TYPE
7197 : 26463 : && TREE_CODE (otype) == POINTER_TYPE)
7198 : 13405 : handle_warn_cast_qual (loc, type, otype);
7199 : :
7200 : : /* Warn about conversions between pointers to disjoint
7201 : : address spaces. */
7202 : 99435223 : if (TREE_CODE (type) == POINTER_TYPE
7203 : 3279730 : && TREE_CODE (otype) == POINTER_TYPE
7204 : 102372915 : && !null_pointer_constant_p (value))
7205 : : {
7206 : 2884895 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7207 : 2884895 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7208 : 2884895 : addr_space_t as_common;
7209 : :
7210 : 2884895 : if (!addr_space_superset (as_to, as_from, &as_common))
7211 : : {
7212 : 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7213 : 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7214 : : "from disjoint generic address space pointer",
7215 : : c_addr_space_name (as_to));
7216 : :
7217 : 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7218 : 0 : warning_at (loc, 0, "cast to generic address space pointer "
7219 : : "from disjoint %qs address space pointer",
7220 : : c_addr_space_name (as_from));
7221 : :
7222 : : else
7223 : 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7224 : : "from disjoint %qs address space pointer",
7225 : : c_addr_space_name (as_to),
7226 : : c_addr_space_name (as_from));
7227 : : }
7228 : :
7229 : : /* Warn of new allocations that are not big enough for the target
7230 : : type. */
7231 : 2884895 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7232 : 805 : if (tree fndecl = get_callee_fndecl (value))
7233 : 801 : if (DECL_IS_MALLOC (fndecl))
7234 : : {
7235 : 517 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7236 : 517 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7237 : 517 : if (alloc_size)
7238 : 159 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7239 : : alloc_size);
7240 : : }
7241 : : }
7242 : :
7243 : : /* Warn about possible alignment problems. */
7244 : 99435223 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7245 : 7 : && TREE_CODE (type) == POINTER_TYPE
7246 : 7 : && TREE_CODE (otype) == POINTER_TYPE
7247 : 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7248 : 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7249 : : /* Don't warn about opaque types, where the actual alignment
7250 : : restriction is unknown. */
7251 : 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7252 : 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7253 : 99435237 : && min_align_of_type (TREE_TYPE (type))
7254 : 7 : > min_align_of_type (TREE_TYPE (otype)))
7255 : 5 : warning_at (loc, OPT_Wcast_align,
7256 : : "cast increases required alignment of target type");
7257 : :
7258 : 99435223 : if ((TREE_CODE (type) == INTEGER_TYPE
7259 : 99435223 : || TREE_CODE (type) == BITINT_TYPE)
7260 : 6910145 : && TREE_CODE (otype) == POINTER_TYPE
7261 : 99460106 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7262 : : /* Unlike conversion of integers to pointers, where the
7263 : : warning is disabled for converting constants because
7264 : : of cases such as SIG_*, warn about converting constant
7265 : : pointers to integers. In some cases it may cause unwanted
7266 : : sign extension, and a warning is appropriate. */
7267 : 1450 : warning_at (loc, OPT_Wpointer_to_int_cast,
7268 : : "cast from pointer to integer of different size");
7269 : :
7270 : 99435223 : if (TREE_CODE (value) == CALL_EXPR
7271 : 33755346 : && TREE_CODE (type) != TREE_CODE (otype))
7272 : 2282 : warning_at (loc, OPT_Wbad_function_cast,
7273 : : "cast from function call of type %qT "
7274 : : "to non-matching type %qT", otype, type);
7275 : :
7276 : 99435223 : if (TREE_CODE (type) == POINTER_TYPE
7277 : 3279730 : && (TREE_CODE (otype) == INTEGER_TYPE
7278 : 3279730 : || TREE_CODE (otype) == BITINT_TYPE)
7279 : 341960 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7280 : : /* Don't warn about converting any constant. */
7281 : 99740295 : && !TREE_CONSTANT (value))
7282 : 407 : warning_at (loc,
7283 : 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7284 : : "of different size");
7285 : :
7286 : 99435223 : if (warn_strict_aliasing <= 2)
7287 : 90922368 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7288 : :
7289 : : /* If pedantic, warn for conversions between function and object
7290 : : pointer types, except for converting a null pointer constant
7291 : : to function pointer type. */
7292 : 99435223 : if (pedantic
7293 : 238799 : && TREE_CODE (type) == POINTER_TYPE
7294 : 144158 : && TREE_CODE (otype) == POINTER_TYPE
7295 : 2769 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7296 : 99435244 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7297 : 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7298 : : "conversion of function pointer to object pointer type");
7299 : :
7300 : 99435223 : if (pedantic
7301 : 238799 : && TREE_CODE (type) == POINTER_TYPE
7302 : 144158 : && TREE_CODE (otype) == POINTER_TYPE
7303 : 2769 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7304 : 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7305 : 99435233 : && !null_pointer_constant_p (value))
7306 : 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7307 : : "conversion of object pointer to function pointer type");
7308 : :
7309 : 99435223 : if (TREE_CODE (type) == POINTER_TYPE
7310 : 3279730 : && TREE_CODE (otype) == POINTER_TYPE
7311 : 2937692 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7312 : 8496 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7313 : 99442657 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7314 : 7434 : TREE_TYPE (otype)))
7315 : 800 : warning_at (loc, OPT_Wcast_function_type,
7316 : : "cast between incompatible function types"
7317 : : " from %qT to %qT", otype, type);
7318 : :
7319 : 99435223 : ovalue = value;
7320 : : /* If converting to boolean a value with integer operands that
7321 : : is not itself represented as an INTEGER_CST, the call below
7322 : : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7323 : : build_binary_op as called by c_common_truthvalue_conversion
7324 : : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7325 : : subexpression has been fully folded. To avoid nested
7326 : : C_MAYBE_CONST_EXPR, ensure that
7327 : : c_objc_common_truthvalue_conversion receives an argument
7328 : : properly marked as having integer operands in that case. */
7329 : 99435223 : if (int_operands
7330 : 6517979 : && TREE_CODE (value) != INTEGER_CST
7331 : 99435265 : && (TREE_CODE (type) == BOOLEAN_TYPE
7332 : 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7333 : 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7334 : 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7335 : 28 : value = note_integer_operands (value);
7336 : 99435223 : value = convert (type, value);
7337 : :
7338 : : /* Ignore any integer overflow caused by the cast. */
7339 : 99435223 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7340 : : {
7341 : 6464667 : if (TREE_OVERFLOW_P (ovalue))
7342 : : {
7343 : 9 : if (!TREE_OVERFLOW (value))
7344 : : {
7345 : : /* Avoid clobbering a shared constant. */
7346 : 0 : value = copy_node (value);
7347 : 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7348 : : }
7349 : : }
7350 : 6464658 : else if (TREE_OVERFLOW (value))
7351 : : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7352 : 3085 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7353 : : }
7354 : : }
7355 : :
7356 : : /* Don't let a cast be an lvalue. */
7357 : 117056796 : if (lvalue_p (value))
7358 : 88574 : value = non_lvalue_loc (loc, value);
7359 : :
7360 : : /* Don't allow the results of casting to floating-point or complex
7361 : : types be confused with actual constants, or casts involving
7362 : : integer and pointer types other than direct integer-to-integer
7363 : : and integer-to-pointer be confused with integer constant
7364 : : expressions and null pointer constants. */
7365 : 117056796 : if (TREE_CODE (value) == REAL_CST
7366 : 117007018 : || TREE_CODE (value) == COMPLEX_CST
7367 : 234041191 : || (TREE_CODE (value) == INTEGER_CST
7368 : 6711501 : && !((TREE_CODE (expr) == INTEGER_CST
7369 : 6638968 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7370 : 65130 : || TREE_CODE (expr) == REAL_CST
7371 : : || TREE_CODE (expr) == COMPLEX_CST)))
7372 : 133164 : value = build1 (NOP_EXPR, type, value);
7373 : :
7374 : : /* If the expression has integer operands and so can occur in an
7375 : : unevaluated part of an integer constant expression, ensure the
7376 : : return value reflects this. */
7377 : 117056796 : if (int_operands
7378 : 6693219 : && INTEGRAL_TYPE_P (type)
7379 : 6252541 : && value != error_mark_node
7380 : 123309336 : && !EXPR_INT_CONST_OPERANDS (value))
7381 : 11 : value = note_integer_operands (value);
7382 : :
7383 : 117056796 : protected_set_expr_location (value, loc);
7384 : 117056796 : return value;
7385 : : }
7386 : :
7387 : : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7388 : : location of the open paren of the cast, or the position of the cast
7389 : : expr. */
7390 : : tree
7391 : 117075486 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7392 : : {
7393 : 117075486 : tree type;
7394 : 117075486 : tree type_expr = NULL_TREE;
7395 : 117075486 : bool type_expr_const = true;
7396 : 117075486 : tree ret;
7397 : 117075486 : int saved_wsp = warn_strict_prototypes;
7398 : :
7399 : : /* This avoids warnings about unprototyped casts on
7400 : : integers. E.g. "#define SIG_DFL (void(*)())0". */
7401 : 117075486 : if (TREE_CODE (expr) == INTEGER_CST)
7402 : 6759910 : warn_strict_prototypes = 0;
7403 : 117075486 : type = groktypename (type_name, &type_expr, &type_expr_const);
7404 : 117075486 : warn_strict_prototypes = saved_wsp;
7405 : :
7406 : 764517 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7407 : 117839766 : && reject_gcc_builtin (expr))
7408 : 2 : return error_mark_node;
7409 : :
7410 : 117075484 : ret = build_c_cast (loc, type, expr);
7411 : 117075484 : if (ret == error_mark_node)
7412 : : return error_mark_node;
7413 : :
7414 : 117074334 : if (type_expr)
7415 : : {
7416 : 249 : bool inner_expr_const = true;
7417 : 249 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7418 : 249 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7419 : 498 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7420 : 145 : && inner_expr_const);
7421 : 249 : SET_EXPR_LOCATION (ret, loc);
7422 : : }
7423 : :
7424 : 117074334 : if (!EXPR_HAS_LOCATION (ret))
7425 : 6639470 : protected_set_expr_location (ret, loc);
7426 : :
7427 : : /* C++ does not permits types to be defined in a cast, but it
7428 : : allows references to incomplete types. */
7429 : 117074334 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7430 : 1 : warning_at (loc, OPT_Wc___compat,
7431 : : "defining a type in a cast is invalid in C++");
7432 : :
7433 : : return ret;
7434 : : }
7435 : :
7436 : : /* Build an assignment expression of lvalue LHS from value RHS.
7437 : : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7438 : : may differ from TREE_TYPE (LHS) for an enum bitfield.
7439 : : MODIFYCODE is the code for a binary operator that we use
7440 : : to combine the old value of LHS with RHS to get the new value.
7441 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7442 : : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7443 : : which may differ from TREE_TYPE (RHS) for an enum value.
7444 : :
7445 : : LOCATION is the location of the MODIFYCODE operator.
7446 : : RHS_LOC is the location of the RHS. */
7447 : :
7448 : : tree
7449 : 2843991 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7450 : : enum tree_code modifycode,
7451 : : location_t rhs_loc, tree rhs, tree rhs_origtype)
7452 : : {
7453 : 2843991 : tree result;
7454 : 2843991 : tree newrhs;
7455 : 2843991 : tree rhseval = NULL_TREE;
7456 : 2843991 : tree lhstype = TREE_TYPE (lhs);
7457 : 2843991 : tree olhstype = lhstype;
7458 : 2843991 : bool npc;
7459 : 2843991 : bool is_atomic_op;
7460 : :
7461 : : /* Types that aren't fully specified cannot be used in assignments. */
7462 : 2843991 : lhs = require_complete_type (location, lhs);
7463 : 2843991 : rhs = require_complete_type (location, rhs);
7464 : :
7465 : : /* Avoid duplicate error messages from operands that had errors. */
7466 : 2843991 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7467 : 472 : return error_mark_node;
7468 : :
7469 : : /* Ensure an error for assigning a non-lvalue array to an array in
7470 : : C90. */
7471 : 2843519 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7472 : : {
7473 : 1 : error_at (location, "assignment to expression with array type");
7474 : 1 : return error_mark_node;
7475 : : }
7476 : :
7477 : : /* For ObjC properties, defer this check. */
7478 : 2843518 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7479 : 31 : return error_mark_node;
7480 : :
7481 : 2843487 : is_atomic_op = really_atomic_lvalue (lhs);
7482 : :
7483 : 2843487 : newrhs = rhs;
7484 : :
7485 : 2843487 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7486 : : {
7487 : 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7488 : : lhs_origtype, modifycode, rhs_loc, rhs,
7489 : : rhs_origtype);
7490 : 2 : if (inner == error_mark_node)
7491 : : return error_mark_node;
7492 : 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7493 : 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7494 : 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7495 : 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7496 : 2 : protected_set_expr_location (result, location);
7497 : 2 : return result;
7498 : : }
7499 : :
7500 : : /* If a binary op has been requested, combine the old LHS value with the RHS
7501 : : producing the value we should actually store into the LHS. */
7502 : :
7503 : 2843485 : if (modifycode != NOP_EXPR)
7504 : : {
7505 : 210409 : lhs = c_fully_fold (lhs, false, NULL, true);
7506 : 210409 : lhs = stabilize_reference (lhs);
7507 : :
7508 : : /* Construct the RHS for any non-atomic compound assignemnt. */
7509 : 210409 : if (!is_atomic_op)
7510 : : {
7511 : : /* If in LHS op= RHS the RHS has side-effects, ensure they
7512 : : are preevaluated before the rest of the assignment expression's
7513 : : side-effects, because RHS could contain e.g. function calls
7514 : : that modify LHS. */
7515 : 194924 : if (TREE_SIDE_EFFECTS (rhs))
7516 : : {
7517 : 8620 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7518 : 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7519 : : else
7520 : 8592 : newrhs = save_expr (rhs);
7521 : 8620 : rhseval = newrhs;
7522 : 8620 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7523 : 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7524 : : newrhs);
7525 : : }
7526 : 194924 : bool clear_decl_read = false;
7527 : 85771 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7528 : 150569 : && !DECL_READ_P (lhs)
7529 : 246158 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7530 : : : warn_unused_but_set_parameter) > 2)
7531 : : {
7532 : 21534 : mark_exp_read (newrhs);
7533 : 21534 : if (!DECL_READ_P (lhs))
7534 : 194924 : clear_decl_read = true;
7535 : : }
7536 : :
7537 : 194924 : newrhs = build_binary_op (location, modifycode,
7538 : : convert_lvalue_to_rvalue (location, lhs,
7539 : : true, true),
7540 : : newrhs, true);
7541 : 194924 : if (clear_decl_read)
7542 : 21534 : DECL_READ_P (lhs) = 0;
7543 : :
7544 : : /* The original type of the right hand side is no longer
7545 : : meaningful. */
7546 : : rhs_origtype = NULL_TREE;
7547 : : }
7548 : : }
7549 : :
7550 : 2843485 : if (c_dialect_objc ())
7551 : : {
7552 : : /* Check if we are modifying an Objective-C property reference;
7553 : : if so, we need to generate setter calls. */
7554 : 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7555 : 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7556 : : else
7557 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7558 : 0 : if (result)
7559 : 0 : goto return_result;
7560 : :
7561 : : /* Else, do the check that we postponed for Objective-C. */
7562 : 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7563 : 0 : return error_mark_node;
7564 : : }
7565 : :
7566 : : /* Give an error for storing in something that is 'const'. */
7567 : :
7568 : 2843485 : if (TYPE_READONLY (lhstype)
7569 : 2843485 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7570 : 25497 : && C_TYPE_FIELDS_READONLY (lhstype)))
7571 : : {
7572 : 88 : readonly_error (location, lhs, lv_assign);
7573 : 88 : return error_mark_node;
7574 : : }
7575 : 2843397 : else if (TREE_READONLY (lhs))
7576 : 1 : readonly_warning (lhs, lv_assign);
7577 : :
7578 : : /* If storing into a structure or union member,
7579 : : it has probably been given type `int'.
7580 : : Compute the type that would go with
7581 : : the actual amount of storage the member occupies. */
7582 : :
7583 : 2843397 : if (TREE_CODE (lhs) == COMPONENT_REF
7584 : 243989 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7585 : 243989 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7586 : 106773 : || SCALAR_FLOAT_TYPE_P (lhstype)
7587 : 84902 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7588 : 166071 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7589 : :
7590 : : /* If storing in a field that is in actuality a short or narrower than one,
7591 : : we must store in the field in its actual type. */
7592 : :
7593 : 2843397 : if (lhstype != TREE_TYPE (lhs))
7594 : : {
7595 : 0 : lhs = copy_node (lhs);
7596 : 0 : TREE_TYPE (lhs) = lhstype;
7597 : : }
7598 : :
7599 : : /* Issue -Wc++-compat warnings about an assignment to an enum type
7600 : : when LHS does not have its original type. This happens for,
7601 : : e.g., an enum bitfield in a struct. */
7602 : 2843397 : if (warn_cxx_compat
7603 : 3195 : && lhs_origtype != NULL_TREE
7604 : 3195 : && lhs_origtype != lhstype
7605 : 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7606 : : {
7607 : 4 : tree checktype = (rhs_origtype != NULL_TREE
7608 : 4 : ? rhs_origtype
7609 : 0 : : TREE_TYPE (rhs));
7610 : 4 : if (checktype != error_mark_node
7611 : 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7612 : 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7613 : 2 : warning_at (location, OPT_Wc___compat,
7614 : : "enum conversion in assignment is invalid in C++");
7615 : : }
7616 : :
7617 : : /* Remove qualifiers. */
7618 : 2843397 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7619 : 2843397 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7620 : :
7621 : : /* Convert new value to destination type. Fold it first, then
7622 : : restore any excess precision information, for the sake of
7623 : : conversion warnings. */
7624 : :
7625 : 2843397 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7626 : : {
7627 : 2827912 : tree rhs_semantic_type = NULL_TREE;
7628 : 2827912 : if (!c_in_omp_for)
7629 : : {
7630 : 2811616 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7631 : : {
7632 : 5069 : rhs_semantic_type = TREE_TYPE (newrhs);
7633 : 5069 : newrhs = TREE_OPERAND (newrhs, 0);
7634 : : }
7635 : 2811616 : npc = null_pointer_constant_p (newrhs);
7636 : 2811616 : newrhs = c_fully_fold (newrhs, false, NULL);
7637 : 2811616 : if (rhs_semantic_type)
7638 : 5069 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7639 : : }
7640 : : else
7641 : 16296 : npc = null_pointer_constant_p (newrhs);
7642 : 2827912 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7643 : : rhs_origtype, ic_assign, npc,
7644 : : NULL_TREE, NULL_TREE, 0);
7645 : 2827912 : if (TREE_CODE (newrhs) == ERROR_MARK)
7646 : 118 : return error_mark_node;
7647 : : }
7648 : :
7649 : : /* Emit ObjC write barrier, if necessary. */
7650 : 2843279 : if (c_dialect_objc () && flag_objc_gc)
7651 : : {
7652 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7653 : 0 : if (result)
7654 : : {
7655 : 0 : protected_set_expr_location (result, location);
7656 : 0 : goto return_result;
7657 : : }
7658 : : }
7659 : :
7660 : : /* Scan operands. */
7661 : :
7662 : 2843279 : if (is_atomic_op)
7663 : 25956 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7664 : : else
7665 : : {
7666 : 2817323 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7667 : 2817323 : TREE_SIDE_EFFECTS (result) = 1;
7668 : 2817323 : protected_set_expr_location (result, location);
7669 : : }
7670 : :
7671 : : /* If we got the LHS in a different type for storing in,
7672 : : convert the result back to the nominal type of LHS
7673 : : so that the value we return always has the same type
7674 : : as the LHS argument. */
7675 : :
7676 : 2843279 : if (olhstype == TREE_TYPE (result))
7677 : 2843279 : goto return_result;
7678 : :
7679 : 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
7680 : : rhs_origtype, ic_assign, false, NULL_TREE,
7681 : : NULL_TREE, 0);
7682 : 0 : protected_set_expr_location (result, location);
7683 : :
7684 : 2843279 : return_result:
7685 : 2843279 : if (rhseval)
7686 : 8620 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
7687 : : return result;
7688 : : }
7689 : :
7690 : : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
7691 : : This is used to implement -fplan9-extensions. */
7692 : :
7693 : : static bool
7694 : 10 : find_anonymous_field_with_type (tree struct_type, tree type)
7695 : : {
7696 : 10 : tree field;
7697 : 10 : bool found;
7698 : :
7699 : 10 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
7700 : 10 : found = false;
7701 : 10 : for (field = TYPE_FIELDS (struct_type);
7702 : 20 : field != NULL_TREE;
7703 : 10 : field = TREE_CHAIN (field))
7704 : : {
7705 : 10 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7706 : 10 : if (DECL_NAME (field) == NULL
7707 : 10 : && comptypes (type, fieldtype))
7708 : : {
7709 : 4 : if (found)
7710 : : return false;
7711 : : found = true;
7712 : : }
7713 : 6 : else if (DECL_NAME (field) == NULL
7714 : 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
7715 : 8 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
7716 : : {
7717 : 0 : if (found)
7718 : : return false;
7719 : : found = true;
7720 : : }
7721 : : }
7722 : : return found;
7723 : : }
7724 : :
7725 : : /* RHS is an expression whose type is pointer to struct. If there is
7726 : : an anonymous field in RHS with type TYPE, then return a pointer to
7727 : : that field in RHS. This is used with -fplan9-extensions. This
7728 : : returns NULL if no conversion could be found. */
7729 : :
7730 : : static tree
7731 : 12 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
7732 : : {
7733 : 12 : tree rhs_struct_type, lhs_main_type;
7734 : 12 : tree field, found_field;
7735 : 12 : bool found_sub_field;
7736 : 12 : tree ret;
7737 : :
7738 : 12 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
7739 : 12 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
7740 : 12 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
7741 : :
7742 : 12 : gcc_assert (POINTER_TYPE_P (type));
7743 : 12 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
7744 : :
7745 : 12 : found_field = NULL_TREE;
7746 : 12 : found_sub_field = false;
7747 : 12 : for (field = TYPE_FIELDS (rhs_struct_type);
7748 : 44 : field != NULL_TREE;
7749 : 32 : field = TREE_CHAIN (field))
7750 : : {
7751 : 36 : if (DECL_NAME (field) != NULL_TREE
7752 : 36 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
7753 : 16 : continue;
7754 : 20 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7755 : 20 : if (comptypes (lhs_main_type, fieldtype))
7756 : : {
7757 : 12 : if (found_field != NULL_TREE)
7758 : : return NULL_TREE;
7759 : : found_field = field;
7760 : : }
7761 : 8 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
7762 : : lhs_main_type))
7763 : : {
7764 : 4 : if (found_field != NULL_TREE)
7765 : : return NULL_TREE;
7766 : : found_field = field;
7767 : : found_sub_field = true;
7768 : : }
7769 : : }
7770 : :
7771 : 8 : if (found_field == NULL_TREE)
7772 : : return NULL_TREE;
7773 : :
7774 : 8 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
7775 : : build_fold_indirect_ref (rhs), found_field,
7776 : : NULL_TREE);
7777 : 8 : ret = build_fold_addr_expr_loc (location, ret);
7778 : :
7779 : 8 : if (found_sub_field)
7780 : : {
7781 : 2 : ret = convert_to_anonymous_field (location, type, ret);
7782 : 2 : gcc_assert (ret != NULL_TREE);
7783 : : }
7784 : :
7785 : : return ret;
7786 : : }
7787 : :
7788 : : /* Issue an error message for a bad initializer component.
7789 : : GMSGID identifies the message.
7790 : : The component name is taken from the spelling stack. */
7791 : :
7792 : : static void ATTRIBUTE_GCC_DIAG (2,0)
7793 : 906 : error_init (location_t loc, const char *gmsgid, ...)
7794 : : {
7795 : 906 : char *ofwhat;
7796 : :
7797 : 906 : auto_diagnostic_group d;
7798 : :
7799 : : /* The gmsgid may be a format string with %< and %>. */
7800 : 906 : va_list ap;
7801 : 906 : va_start (ap, gmsgid);
7802 : 906 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
7803 : 906 : loc, -1, gmsgid, &ap);
7804 : 906 : va_end (ap);
7805 : :
7806 : 906 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7807 : 906 : if (*ofwhat && warned)
7808 : 220 : inform (loc, "(near initialization for %qs)", ofwhat);
7809 : 906 : }
7810 : :
7811 : : /* Used to implement pedwarn_init and permerror_init. */
7812 : :
7813 : : static bool ATTRIBUTE_GCC_DIAG (3,0)
7814 : 2513 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
7815 : : va_list *ap, enum diagnostics::kind kind)
7816 : : {
7817 : : /* Use the location where a macro was expanded rather than where
7818 : : it was defined to make sure macros defined in system headers
7819 : : but used incorrectly elsewhere are diagnosed. */
7820 : 2513 : location_t exploc = expansion_point_location_if_in_system_header (loc);
7821 : 2513 : auto_diagnostic_group d;
7822 : 2513 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
7823 : 2513 : if (warned)
7824 : : {
7825 : 1800 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7826 : 1800 : if (*ofwhat)
7827 : 1479 : inform (exploc, "(near initialization for %qs)", ofwhat);
7828 : : }
7829 : 5026 : return warned;
7830 : 2513 : }
7831 : :
7832 : : /* Issue a pedantic warning for a bad initializer component. OPT is
7833 : : the option OPT_* (from options.h) controlling this warning or 0 if
7834 : : it is unconditionally given. GMSGID identifies the message. The
7835 : : component name is taken from the spelling stack. */
7836 : :
7837 : : static bool ATTRIBUTE_GCC_DIAG (3,0)
7838 : 2184 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
7839 : : {
7840 : 2184 : va_list ap;
7841 : 2184 : va_start (ap, gmsgid);
7842 : 2184 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
7843 : : diagnostics::kind::pedwarn);
7844 : 2184 : va_end (ap);
7845 : 2184 : return warned;
7846 : : }
7847 : :
7848 : : /* Like pedwarn_init, but issue a permerror. */
7849 : :
7850 : : static bool ATTRIBUTE_GCC_DIAG (3,0)
7851 : 329 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
7852 : : {
7853 : 329 : va_list ap;
7854 : 329 : va_start (ap, gmsgid);
7855 : 329 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
7856 : : diagnostics::kind::permerror);
7857 : 329 : va_end (ap);
7858 : 329 : return warned;
7859 : : }
7860 : :
7861 : : /* Issue a warning for a bad initializer component.
7862 : :
7863 : : OPT is the OPT_W* value corresponding to the warning option that
7864 : : controls this warning. GMSGID identifies the message. The
7865 : : component name is taken from the spelling stack. */
7866 : :
7867 : : static void
7868 : 146 : warning_init (location_t loc, int opt, const char *gmsgid)
7869 : : {
7870 : 146 : char *ofwhat;
7871 : 146 : bool warned;
7872 : :
7873 : 146 : auto_diagnostic_group d;
7874 : :
7875 : : /* Use the location where a macro was expanded rather than where
7876 : : it was defined to make sure macros defined in system headers
7877 : : but used incorrectly elsewhere are diagnosed. */
7878 : 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
7879 : :
7880 : : /* The gmsgid may be a format string with %< and %>. */
7881 : 146 : warned = warning_at (exploc, opt, gmsgid);
7882 : 146 : if (warned)
7883 : : {
7884 : 124 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7885 : 124 : if (*ofwhat)
7886 : 124 : inform (exploc, "(near initialization for %qs)", ofwhat);
7887 : : }
7888 : 146 : }
7889 : :
7890 : : /* If TYPE is an array type and EXPR is a parenthesized string
7891 : : constant, warn if pedantic that EXPR is being used to initialize an
7892 : : object of type TYPE. */
7893 : :
7894 : : void
7895 : 7106584 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
7896 : : {
7897 : 7106584 : if (pedantic
7898 : 80224 : && TREE_CODE (type) == ARRAY_TYPE
7899 : 1008 : && TREE_CODE (expr.value) == STRING_CST
7900 : 353 : && expr.original_code != STRING_CST)
7901 : 13 : pedwarn_init (loc, OPT_Wpedantic,
7902 : : "array initialized from parenthesized string constant");
7903 : 7106584 : }
7904 : :
7905 : : /* Attempt to locate the parameter with the given index within FNDECL,
7906 : : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
7907 : :
7908 : : static location_t
7909 : 936 : get_fndecl_argument_location (tree fndecl, int argnum)
7910 : : {
7911 : 936 : int i;
7912 : 936 : tree param;
7913 : :
7914 : : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
7915 : 936 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
7916 : 1015 : i < argnum && param;
7917 : 79 : i++, param = TREE_CHAIN (param))
7918 : : ;
7919 : :
7920 : : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
7921 : : return DECL_SOURCE_LOCATION (FNDECL). */
7922 : 936 : if (param == NULL)
7923 : 653 : return DECL_SOURCE_LOCATION (fndecl);
7924 : :
7925 : 283 : return DECL_SOURCE_LOCATION (param);
7926 : : }
7927 : :
7928 : : /* Issue a note about a mismatching argument for parameter PARMNUM
7929 : : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
7930 : : Attempt to issue the note at the pertinent parameter of the decl;
7931 : : failing that issue it at the location of FUNDECL; failing that
7932 : : issue it at PLOC.
7933 : : Use highlight_colors::actual for the ACTUAL_TYPE
7934 : : and highlight_colors::expected for EXPECTED_TYPE and the
7935 : : parameter of FUNDECL*/
7936 : :
7937 : : static void
7938 : 1060 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
7939 : : tree expected_type, tree actual_type)
7940 : : {
7941 : 1060 : location_t loc;
7942 : 1060 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7943 : 936 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
7944 : : else
7945 : : loc = ploc;
7946 : :
7947 : 1060 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
7948 : :
7949 : 1060 : pp_markup::element_expected_type elem_expected_type (expected_type);
7950 : 1060 : pp_markup::element_actual_type elem_actual_type (actual_type);
7951 : 1060 : inform (&richloc,
7952 : : "expected %e but argument is of type %e",
7953 : : &elem_expected_type, &elem_actual_type);
7954 : 1060 : }
7955 : :
7956 : : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
7957 : : function FUNDECL declared without prototype to parameter PARMNUM of
7958 : : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
7959 : :
7960 : : static void
7961 : 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
7962 : : tree parmtype, tree argtype)
7963 : : {
7964 : 418 : tree_code parmcode = TREE_CODE (parmtype);
7965 : 418 : tree_code argcode = TREE_CODE (argtype);
7966 : 418 : tree promoted = c_type_promotes_to (argtype);
7967 : :
7968 : : /* Avoid warning for enum arguments that promote to an integer type
7969 : : of the same size/mode. */
7970 : 418 : if (parmcode == INTEGER_TYPE
7971 : 418 : && argcode == ENUMERAL_TYPE
7972 : 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
7973 : : return;
7974 : :
7975 : 417 : if ((parmcode == argcode
7976 : 222 : || (parmcode == INTEGER_TYPE
7977 : : && argcode == ENUMERAL_TYPE))
7978 : 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
7979 : : return;
7980 : :
7981 : : /* This diagnoses even signed/unsigned mismatches. Those might be
7982 : : safe in many cases but GCC may emit suboptimal code for them so
7983 : : warning on those cases drives efficiency improvements. */
7984 : 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
7985 : 398 : TYPE_MAIN_VARIANT (promoted) == argtype
7986 : : ? G_("%qD argument %d type is %qT where %qT is expected "
7987 : : "in a call to built-in function declared without "
7988 : : "prototype")
7989 : : : G_("%qD argument %d promotes to %qT where %qT is expected "
7990 : : "in a call to built-in function declared without "
7991 : : "prototype"),
7992 : : fundecl, parmnum, promoted, parmtype))
7993 : 150 : inform (DECL_SOURCE_LOCATION (fundecl),
7994 : : "built-in %qD declared here",
7995 : : fundecl);
7996 : : }
7997 : :
7998 : : /* Print a declaration in quotes, with the given highlight_color.
7999 : : Analogous to handler for %qD, but with a specific highlight color. */
8000 : :
8001 : 199 : class pp_element_quoted_decl : public pp_element
8002 : : {
8003 : : public:
8004 : 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8005 : 199 : : m_decl (decl),
8006 : 199 : m_highlight_color (highlight_color)
8007 : : {
8008 : : }
8009 : :
8010 : 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8011 : : {
8012 : 199 : ctxt.begin_quote ();
8013 : 199 : ctxt.begin_highlight_color (m_highlight_color);
8014 : :
8015 : 199 : print_decl (ctxt);
8016 : :
8017 : 199 : ctxt.end_highlight_color ();
8018 : 199 : ctxt.end_quote ();
8019 : 199 : }
8020 : :
8021 : 199 : void print_decl (pp_markup::context &ctxt)
8022 : : {
8023 : 199 : pretty_printer *const pp = &ctxt.m_pp;
8024 : 199 : pp->set_padding (pp_none);
8025 : 199 : if (DECL_NAME (m_decl))
8026 : 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8027 : : else
8028 : 0 : pp_string (pp, _("({anonymous})"));
8029 : 199 : }
8030 : :
8031 : : private:
8032 : : tree m_decl;
8033 : : const char *m_highlight_color;
8034 : : };
8035 : :
8036 : : /* If TYPE is from a typedef, issue a note showing the location
8037 : : to the user.
8038 : : Use HIGHLIGHT_COLOR as the highlight color. */
8039 : :
8040 : : static void
8041 : 1178 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8042 : : {
8043 : 1178 : if (!typedef_variant_p (type))
8044 : 1144 : return;
8045 : :
8046 : 34 : tree typedef_decl = TYPE_NAME (type);
8047 : 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8048 : 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8049 : 34 : nullptr, highlight_color);
8050 : 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8051 : 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8052 : 34 : }
8053 : :
8054 : : /* Convert value RHS to type TYPE as preparation for an assignment to
8055 : : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8056 : : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8057 : : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8058 : : constant before any folding.
8059 : : The real work of conversion is done by `convert'.
8060 : : The purpose of this function is to generate error messages
8061 : : for assignments that are not allowed in C.
8062 : : ERRTYPE says whether it is argument passing, assignment,
8063 : : initialization or return.
8064 : :
8065 : : In the following example, '~' denotes where EXPR_LOC and '^' where
8066 : : LOCATION point to:
8067 : :
8068 : : f (var); [ic_argpass]
8069 : : ^ ~~~
8070 : : x = var; [ic_assign]
8071 : : ^ ~~~;
8072 : : int x = var; [ic_init]
8073 : : ^^^
8074 : : return x; [ic_return]
8075 : : ^
8076 : :
8077 : : FUNCTION is a tree for the function being called.
8078 : : PARMNUM is the number of the argument, for printing in error messages.
8079 : : WARNOPT may be set to a warning option to issue the corresponding warning
8080 : : rather than an error for invalid conversions. Used for calls to built-in
8081 : : functions declared without a prototype. */
8082 : :
8083 : : static tree
8084 : 162832643 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8085 : : tree rhs, tree origtype, enum impl_conv errtype,
8086 : : bool null_pointer_constant, tree fundecl,
8087 : : tree function, int parmnum, int warnopt /* = 0 */)
8088 : : {
8089 : 162832643 : enum tree_code codel = TREE_CODE (type);
8090 : 162832643 : tree orig_rhs = rhs;
8091 : 162832643 : tree rhstype;
8092 : 162832643 : enum tree_code coder;
8093 : 162832643 : tree rname = NULL_TREE;
8094 : 162832643 : bool objc_ok = false;
8095 : :
8096 : : /* Use the expansion point location to handle cases such as user's
8097 : : function returning a wrong-type macro defined in a system header. */
8098 : 162832643 : location = expansion_point_location_if_in_system_header (location);
8099 : :
8100 : 162832643 : if (errtype == ic_argpass)
8101 : : {
8102 : 122404150 : tree selector;
8103 : : /* Change pointer to function to the function itself for
8104 : : diagnostics. */
8105 : 122404150 : if (TREE_CODE (function) == ADDR_EXPR
8106 : 122404150 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8107 : 0 : function = TREE_OPERAND (function, 0);
8108 : :
8109 : : /* Handle an ObjC selector specially for diagnostics. */
8110 : 122404150 : selector = objc_message_selector ();
8111 : 122404150 : rname = function;
8112 : 122404150 : if (selector && parmnum > 2)
8113 : : {
8114 : 0 : rname = selector;
8115 : 0 : parmnum -= 2;
8116 : : }
8117 : : }
8118 : :
8119 : : /* This macro is used to emit diagnostics to ensure that all format
8120 : : strings are complete sentences, visible to gettext and checked at
8121 : : compile time. */
8122 : : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8123 : : do { \
8124 : : switch (errtype) \
8125 : : { \
8126 : : case ic_argpass: \
8127 : : { \
8128 : : auto_diagnostic_group d; \
8129 : : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8130 : : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8131 : : } \
8132 : : break; \
8133 : : case ic_assign: \
8134 : : pedwarn (LOCATION, OPT, AS); \
8135 : : break; \
8136 : : case ic_init: \
8137 : : case ic_init_const: \
8138 : : pedwarn_init (LOCATION, OPT, IN); \
8139 : : break; \
8140 : : case ic_return: \
8141 : : pedwarn (LOCATION, OPT, RE); \
8142 : : break; \
8143 : : default: \
8144 : : gcc_unreachable (); \
8145 : : } \
8146 : : } while (0)
8147 : :
8148 : : /* This macro is used to emit diagnostics to ensure that all format
8149 : : strings are complete sentences, visible to gettext and checked at
8150 : : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8151 : : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8152 : : do { \
8153 : : switch (errtype) \
8154 : : { \
8155 : : case ic_argpass: \
8156 : : { \
8157 : : auto_diagnostic_group d; \
8158 : : if (PEDWARN) { \
8159 : : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8160 : : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8161 : : } else { \
8162 : : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8163 : : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8164 : : } \
8165 : : } \
8166 : : break; \
8167 : : case ic_assign: \
8168 : : if (PEDWARN) \
8169 : : pedwarn (LOCATION, OPT, AS, QUALS); \
8170 : : else \
8171 : : warning_at (LOCATION, OPT, AS, QUALS); \
8172 : : break; \
8173 : : case ic_init: \
8174 : : case ic_init_const: \
8175 : : if (PEDWARN) \
8176 : : pedwarn (LOCATION, OPT, IN, QUALS); \
8177 : : else \
8178 : : warning_at (LOCATION, OPT, IN, QUALS); \
8179 : : break; \
8180 : : case ic_return: \
8181 : : if (PEDWARN) \
8182 : : pedwarn (LOCATION, OPT, RE, QUALS); \
8183 : : else \
8184 : : warning_at (LOCATION, OPT, RE, QUALS); \
8185 : : break; \
8186 : : default: \
8187 : : gcc_unreachable (); \
8188 : : } \
8189 : : } while (0)
8190 : :
8191 : : /* This macro is used to emit diagnostics to ensure that all format
8192 : : strings are complete sentences, visible to gettext and checked at
8193 : : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8194 : : extra parameter to enumerate qualifiers. */
8195 : : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8196 : : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8197 : :
8198 : :
8199 : 162832643 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8200 : 5963 : rhs = TREE_OPERAND (rhs, 0);
8201 : :
8202 : 162832643 : rhstype = TREE_TYPE (rhs);
8203 : 162832643 : coder = TREE_CODE (rhstype);
8204 : :
8205 : 162832643 : if (coder == ERROR_MARK)
8206 : 346 : return error_mark_node;
8207 : :
8208 : 162832297 : if (c_dialect_objc ())
8209 : : {
8210 : 0 : int parmno;
8211 : :
8212 : 0 : switch (errtype)
8213 : : {
8214 : : case ic_return:
8215 : : parmno = 0;
8216 : : break;
8217 : :
8218 : : case ic_assign:
8219 : : parmno = -1;
8220 : : break;
8221 : :
8222 : : case ic_init:
8223 : : case ic_init_const:
8224 : : parmno = -2;
8225 : : break;
8226 : :
8227 : : default:
8228 : 0 : parmno = parmnum;
8229 : : break;
8230 : : }
8231 : :
8232 : 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8233 : : }
8234 : :
8235 : 162832297 : if (warn_cxx_compat)
8236 : : {
8237 : 28850 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8238 : 28850 : if (checktype != error_mark_node
8239 : 28850 : && TREE_CODE (type) == ENUMERAL_TYPE
8240 : 28956 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8241 : 45 : switch (errtype)
8242 : : {
8243 : 8 : case ic_argpass:
8244 : 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8245 : : "passing argument %d of %qE is invalid in C++",
8246 : : parmnum, rname))
8247 : 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8248 : 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8249 : : "expected %qT but argument is of type %qT",
8250 : : type, rhstype);
8251 : : break;
8252 : 10 : case ic_assign:
8253 : 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8254 : : "%qT in assignment is invalid in C++", rhstype, type);
8255 : 10 : break;
8256 : 18 : case ic_init:
8257 : 18 : case ic_init_const:
8258 : 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8259 : : "%qT to %qT in initialization is invalid in C++",
8260 : : rhstype, type);
8261 : 18 : break;
8262 : 9 : case ic_return:
8263 : 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8264 : : "%qT in return is invalid in C++", rhstype, type);
8265 : 9 : break;
8266 : 0 : default:
8267 : 0 : gcc_unreachable ();
8268 : : }
8269 : : }
8270 : :
8271 : 162832297 : if (warn_enum_conversion)
8272 : : {
8273 : 14564289 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8274 : 14564289 : if (checktype != error_mark_node
8275 : 14564289 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8276 : 12669 : && TREE_CODE (type) == ENUMERAL_TYPE
8277 : 14572473 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8278 : : {
8279 : 3 : gcc_rich_location loc (location);
8280 : 3 : warning_at (&loc, OPT_Wenum_conversion,
8281 : : "implicit conversion from %qT to %qT",
8282 : : checktype, type);
8283 : 3 : }
8284 : : }
8285 : :
8286 : 162832297 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8287 : : {
8288 : 144158938 : warn_for_address_of_packed_member (type, orig_rhs);
8289 : 144158938 : if (type != rhstype)
8290 : : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8291 : 35905629 : rhs = convert (type, rhs);
8292 : 144158938 : return rhs;
8293 : : }
8294 : :
8295 : 18673359 : if (coder == VOID_TYPE)
8296 : : {
8297 : : /* Except for passing an argument to an unprototyped function,
8298 : : this is a constraint violation. When passing an argument to
8299 : : an unprototyped function, it is compile-time undefined;
8300 : : making it a constraint in that case was rejected in
8301 : : DR#252. */
8302 : 8 : const char msg[] = "void value not ignored as it ought to be";
8303 : 8 : if (warnopt)
8304 : 0 : warning_at (location, warnopt, msg);
8305 : : else
8306 : 8 : error_at (location, msg);
8307 : 8 : return error_mark_node;
8308 : : }
8309 : 18673351 : rhs = require_complete_type (location, rhs);
8310 : 18673351 : if (rhs == error_mark_node)
8311 : : return error_mark_node;
8312 : :
8313 : 18673351 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8314 : 6 : return error_mark_node;
8315 : :
8316 : : /* A non-reference type can convert to a reference. This handles
8317 : : va_start, va_copy and possibly port built-ins. */
8318 : 18673345 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8319 : : {
8320 : 270 : if (!lvalue_p (rhs))
8321 : : {
8322 : 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8323 : 0 : if (warnopt)
8324 : 0 : warning_at (location, warnopt, msg);
8325 : : else
8326 : 0 : error_at (location, msg);
8327 : 0 : return error_mark_node;
8328 : : }
8329 : 270 : if (!c_mark_addressable (rhs))
8330 : 0 : return error_mark_node;
8331 : 270 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8332 : 270 : SET_EXPR_LOCATION (rhs, location);
8333 : :
8334 : 270 : rhs = convert_for_assignment (location, expr_loc,
8335 : 270 : c_build_pointer_type (TREE_TYPE (type)),
8336 : : rhs, origtype, errtype,
8337 : : null_pointer_constant, fundecl, function,
8338 : : parmnum, warnopt);
8339 : 270 : if (rhs == error_mark_node)
8340 : : return error_mark_node;
8341 : :
8342 : 270 : rhs = build1 (NOP_EXPR, type, rhs);
8343 : 270 : SET_EXPR_LOCATION (rhs, location);
8344 : 270 : return rhs;
8345 : : }
8346 : : /* Some types can interconvert without explicit casts. */
8347 : 18673075 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8348 : 18673075 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8349 : 9056533 : return convert (type, rhs);
8350 : : /* Arithmetic types all interconvert, and enum is treated like int. */
8351 : 9616542 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8352 : 2240623 : || codel == FIXED_POINT_TYPE
8353 : 2240623 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8354 : 2154839 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8355 : 7552900 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8356 : 81545 : || coder == FIXED_POINT_TYPE
8357 : 81545 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8358 : 39540 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8359 : : {
8360 : 7551783 : if (warnopt && errtype == ic_argpass)
8361 : 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8362 : : rhstype);
8363 : :
8364 : 7551783 : bool save = in_late_binary_op;
8365 : 7521143 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8366 : 15016759 : || (coder == REAL_TYPE
8367 : 271535 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8368 : 24098 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8369 : 93551 : in_late_binary_op = true;
8370 : 10864530 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8371 : : ? expr_loc : location, type, orig_rhs,
8372 : : errtype == ic_init_const);
8373 : 7551783 : in_late_binary_op = save;
8374 : 7551783 : return ret;
8375 : : }
8376 : :
8377 : : /* Aggregates in different TUs might need conversion. */
8378 : 2064759 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8379 : 99 : && codel == coder
8380 : 2064780 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8381 : 8 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8382 : 8 : ? expr_loc : location, type, rhs);
8383 : :
8384 : : /* Conversion to a transparent union or record from its member types.
8385 : : This applies only to function arguments. */
8386 : 2064751 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8387 : 91 : && TYPE_TRANSPARENT_AGGR (type))
8388 : 2064789 : && errtype == ic_argpass)
8389 : : {
8390 : 38 : tree memb, marginal_memb = NULL_TREE;
8391 : :
8392 : 48 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8393 : : {
8394 : 48 : tree memb_type = TREE_TYPE (memb);
8395 : :
8396 : 48 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8397 : 48 : TYPE_MAIN_VARIANT (rhstype)))
8398 : : break;
8399 : :
8400 : 26 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8401 : 0 : continue;
8402 : :
8403 : 26 : if (coder == POINTER_TYPE)
8404 : : {
8405 : 26 : tree ttl = TREE_TYPE (memb_type);
8406 : 26 : tree ttr = TREE_TYPE (rhstype);
8407 : :
8408 : : /* Any non-function converts to a [const][volatile] void *
8409 : : and vice versa; otherwise, targets must be the same.
8410 : : Meanwhile, the lhs target must have all the qualifiers of
8411 : : the rhs. */
8412 : 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8413 : 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8414 : 45 : || comp_target_types (location, memb_type, rhstype))
8415 : : {
8416 : 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8417 : 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8418 : : /* If this type won't generate any warnings, use it. */
8419 : 16 : if (lquals == rquals
8420 : 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8421 : 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8422 : 0 : ? ((lquals | rquals) == rquals)
8423 : 16 : : ((lquals | rquals) == lquals)))
8424 : : break;
8425 : :
8426 : : /* Keep looking for a better type, but remember this one. */
8427 : 0 : if (!marginal_memb)
8428 : 10 : marginal_memb = memb;
8429 : : }
8430 : : }
8431 : :
8432 : : /* Can convert integer zero to any pointer type. */
8433 : 10 : if (null_pointer_constant)
8434 : : {
8435 : 0 : rhs = null_pointer_node;
8436 : 0 : break;
8437 : : }
8438 : : }
8439 : :
8440 : 38 : if (memb || marginal_memb)
8441 : : {
8442 : 38 : if (!memb)
8443 : : {
8444 : : /* We have only a marginally acceptable member type;
8445 : : it needs a warning. */
8446 : 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8447 : 0 : tree ttr = TREE_TYPE (rhstype);
8448 : :
8449 : : /* Const and volatile mean something different for function
8450 : : types, so the usual warnings are not appropriate. */
8451 : 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8452 : 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8453 : : {
8454 : : /* Because const and volatile on functions are
8455 : : restrictions that say the function will not do
8456 : : certain things, it is okay to use a const or volatile
8457 : : function where an ordinary one is wanted, but not
8458 : : vice-versa. */
8459 : 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8460 : 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8461 : 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8462 : : OPT_Wdiscarded_qualifiers,
8463 : : G_("passing argument %d of %qE "
8464 : : "makes %q#v qualified function "
8465 : : "pointer from unqualified"),
8466 : : G_("assignment makes %q#v qualified "
8467 : : "function pointer from "
8468 : : "unqualified"),
8469 : : G_("initialization makes %q#v qualified "
8470 : : "function pointer from "
8471 : : "unqualified"),
8472 : : G_("return makes %q#v qualified function "
8473 : : "pointer from unqualified"),
8474 : : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8475 : : }
8476 : 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8477 : 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8478 : 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8479 : : OPT_Wdiscarded_qualifiers,
8480 : : G_("passing argument %d of %qE discards "
8481 : : "%qv qualifier from pointer target type"),
8482 : : G_("assignment discards %qv qualifier "
8483 : : "from pointer target type"),
8484 : : G_("initialization discards %qv qualifier "
8485 : : "from pointer target type"),
8486 : : G_("return discards %qv qualifier from "
8487 : : "pointer target type"),
8488 : : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8489 : :
8490 : : memb = marginal_memb;
8491 : : }
8492 : :
8493 : 38 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8494 : 30 : pedwarn (location, OPT_Wpedantic,
8495 : : "ISO C prohibits argument conversion to union type");
8496 : :
8497 : 38 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8498 : 38 : return build_constructor_single (type, memb, rhs);
8499 : : }
8500 : : }
8501 : :
8502 : : /* Conversions among pointers */
8503 : 2064713 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8504 : 2063445 : && (coder == codel))
8505 : : {
8506 : : /* If RHS refers to a built-in declared without a prototype
8507 : : BLTIN is the declaration of the built-in with a prototype
8508 : : and RHSTYPE is set to the actual type of the built-in. */
8509 : 1985611 : tree bltin;
8510 : 1985611 : rhstype = type_or_builtin_type (rhs, &bltin);
8511 : :
8512 : 1985611 : tree ttl = TREE_TYPE (type);
8513 : 1985611 : tree ttr = TREE_TYPE (rhstype);
8514 : 1985611 : bool is_opaque_pointer;
8515 : 1985611 : bool target_cmp = false; /* Cache comp_target_types () result. */
8516 : 1985611 : addr_space_t asl;
8517 : 1985611 : addr_space_t asr;
8518 : :
8519 : 1985611 : tree mvl = remove_qualifiers (ttl);
8520 : 1985611 : tree mvr = remove_qualifiers (ttr);
8521 : :
8522 : : /* Opaque pointers are treated like void pointers. */
8523 : 1985611 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8524 : :
8525 : : /* The Plan 9 compiler permits a pointer to a struct to be
8526 : : automatically converted into a pointer to an anonymous field
8527 : : within the struct. */
8528 : 1985611 : if (flag_plan9_extensions
8529 : 10 : && RECORD_OR_UNION_TYPE_P (mvl)
8530 : 10 : && RECORD_OR_UNION_TYPE_P (mvr)
8531 : 10 : && mvl != mvr)
8532 : : {
8533 : 10 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8534 : 10 : if (new_rhs != NULL_TREE)
8535 : : {
8536 : 6 : rhs = new_rhs;
8537 : 6 : rhstype = TREE_TYPE (rhs);
8538 : 6 : coder = TREE_CODE (rhstype);
8539 : 6 : ttr = TREE_TYPE (rhstype);
8540 : 6 : mvr = TYPE_MAIN_VARIANT (ttr);
8541 : : }
8542 : : }
8543 : :
8544 : : /* C++ does not allow the implicit conversion void* -> T*. However,
8545 : : for the purpose of reducing the number of false positives, we
8546 : : tolerate the special case of
8547 : :
8548 : : int *p = NULL;
8549 : :
8550 : : where NULL is typically defined in C to be '(void *) 0'. */
8551 : 1985611 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8552 : 26437 : warning_at (errtype == ic_argpass ? expr_loc : location,
8553 : 14002 : OPT_Wc___compat,
8554 : : "request for implicit conversion "
8555 : : "from %qT to %qT not permitted in C++", rhstype, type);
8556 : :
8557 : : /* Warn of new allocations that are not big enough for the target
8558 : : type. */
8559 : 1985611 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8560 : 5455 : if (tree fndecl = get_callee_fndecl (rhs))
8561 : 5455 : if (DECL_IS_MALLOC (fndecl))
8562 : : {
8563 : 4601 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8564 : 4601 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8565 : 4601 : if (alloc_size)
8566 : 447 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8567 : : }
8568 : :
8569 : : /* See if the pointers point to incompatible address spaces. */
8570 : 1985611 : asl = TYPE_ADDR_SPACE (ttl);
8571 : 1985611 : asr = TYPE_ADDR_SPACE (ttr);
8572 : 1985611 : if (!null_pointer_constant_p (rhs)
8573 : 1985611 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8574 : : {
8575 : 3 : auto_diagnostic_group d;
8576 : 3 : bool diagnosed = true;
8577 : 3 : switch (errtype)
8578 : : {
8579 : 1 : case ic_argpass:
8580 : 1 : {
8581 : 1 : const char msg[] = G_("passing argument %d of %qE from "
8582 : : "pointer to non-enclosed address space");
8583 : 1 : if (warnopt)
8584 : 0 : diagnosed
8585 : 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8586 : : else
8587 : 1 : error_at (expr_loc, msg, parmnum, rname);
8588 : 0 : break;
8589 : : }
8590 : 0 : case ic_assign:
8591 : 0 : {
8592 : 0 : const char msg[] = G_("assignment from pointer to "
8593 : : "non-enclosed address space");
8594 : 0 : if (warnopt)
8595 : 0 : diagnosed = warning_at (location, warnopt, msg);
8596 : : else
8597 : 0 : error_at (location, msg);
8598 : 0 : break;
8599 : : }
8600 : 0 : case ic_init:
8601 : 0 : case ic_init_const:
8602 : 0 : {
8603 : 0 : const char msg[] = G_("initialization from pointer to "
8604 : : "non-enclosed address space");
8605 : 0 : if (warnopt)
8606 : 0 : diagnosed = warning_at (location, warnopt, msg);
8607 : : else
8608 : 0 : error_at (location, msg);
8609 : 0 : break;
8610 : : }
8611 : 2 : case ic_return:
8612 : 2 : {
8613 : 2 : const char msg[] = G_("return from pointer to "
8614 : : "non-enclosed address space");
8615 : 2 : if (warnopt)
8616 : 0 : diagnosed = warning_at (location, warnopt, msg);
8617 : : else
8618 : 2 : error_at (location, msg);
8619 : 0 : break;
8620 : : }
8621 : 0 : default:
8622 : 0 : gcc_unreachable ();
8623 : : }
8624 : 3 : if (diagnosed)
8625 : : {
8626 : 3 : if (errtype == ic_argpass)
8627 : 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8628 : : else
8629 : 2 : inform (location, "expected %qT but pointer is of type %qT",
8630 : : type, rhstype);
8631 : : }
8632 : 3 : return error_mark_node;
8633 : 3 : }
8634 : :
8635 : : /* Check if the right-hand side has a format attribute but the
8636 : : left-hand side doesn't. */
8637 : 1985608 : if (warn_suggest_attribute_format
8638 : 1985608 : && check_missing_format_attribute (type, rhstype))
8639 : : {
8640 : 16 : switch (errtype)
8641 : : {
8642 : 4 : case ic_argpass:
8643 : 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8644 : : "argument %d of %qE might be "
8645 : : "a candidate for a format attribute",
8646 : : parmnum, rname);
8647 : 4 : break;
8648 : 4 : case ic_assign:
8649 : 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8650 : : "assignment left-hand side might be "
8651 : : "a candidate for a format attribute");
8652 : 4 : break;
8653 : 4 : case ic_init:
8654 : 4 : case ic_init_const:
8655 : 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8656 : : "initialization left-hand side might be "
8657 : : "a candidate for a format attribute");
8658 : 4 : break;
8659 : 4 : case ic_return:
8660 : 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8661 : : "return type might be "
8662 : : "a candidate for a format attribute");
8663 : 4 : break;
8664 : 0 : default:
8665 : 0 : gcc_unreachable ();
8666 : : }
8667 : : }
8668 : :
8669 : : /* See if the pointers point to incompatible scalar storage orders. */
8670 : 1985608 : if (warn_scalar_storage_order
8671 : 1982692 : && !null_pointer_constant_p (rhs)
8672 : 5885824 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8673 : 1950108 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8674 : : {
8675 : 18 : tree t;
8676 : :
8677 : 18 : switch (errtype)
8678 : : {
8679 : 11 : case ic_argpass:
8680 : : /* Do not warn for built-in functions, for example memcpy, since we
8681 : : control how they behave and they can be useful in this area. */
8682 : 11 : if (TREE_CODE (rname) != FUNCTION_DECL
8683 : 11 : || !fndecl_built_in_p (rname))
8684 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8685 : : "passing argument %d of %qE from incompatible "
8686 : : "scalar storage order", parmnum, rname);
8687 : : break;
8688 : 3 : case ic_assign:
8689 : : /* Do not warn if the RHS is a call to a function that returns a
8690 : : pointer that is not an alias. */
8691 : 3 : if (TREE_CODE (rhs) != CALL_EXPR
8692 : 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8693 : 5 : || !DECL_IS_MALLOC (t))
8694 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8695 : : "assignment to %qT from pointer type %qT with "
8696 : : "incompatible scalar storage order", type, rhstype);
8697 : : break;
8698 : 3 : case ic_init:
8699 : 3 : case ic_init_const:
8700 : : /* Likewise. */
8701 : 3 : if (TREE_CODE (rhs) != CALL_EXPR
8702 : 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8703 : 5 : || !DECL_IS_MALLOC (t))
8704 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8705 : : "initialization of %qT from pointer type %qT with "
8706 : : "incompatible scalar storage order", type, rhstype);
8707 : : break;
8708 : 1 : case ic_return:
8709 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8710 : : "returning %qT from pointer type with incompatible "
8711 : : "scalar storage order %qT", rhstype, type);
8712 : 1 : break;
8713 : 0 : default:
8714 : 0 : gcc_unreachable ();
8715 : : }
8716 : : }
8717 : :
8718 : : /* Any non-function converts to a [const][volatile] void *
8719 : : and vice versa; otherwise, targets must be the same.
8720 : : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
8721 : 420167 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8722 : 1565442 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8723 : 1518992 : || (target_cmp = comp_target_types (location, type, rhstype))
8724 : 2249 : || is_opaque_pointer
8725 : 1990106 : || ((c_common_unsigned_type (mvl)
8726 : 2249 : == c_common_unsigned_type (mvr))
8727 : 2946 : && (c_common_signed_type (mvl)
8728 : 1473 : == c_common_signed_type (mvr))
8729 : 1458 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
8730 : : {
8731 : : /* Warn about loss of qualifers from pointers to arrays with
8732 : : qualifiers on the element type. */
8733 : 1984809 : if (TREE_CODE (ttr) == ARRAY_TYPE)
8734 : : {
8735 : 2128 : ttr = strip_array_types (ttr);
8736 : 2128 : ttl = strip_array_types (ttl);
8737 : :
8738 : 2128 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8739 : 2128 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
8740 : 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
8741 : : location, expr_loc,
8742 : : OPT_Wdiscarded_array_qualifiers,
8743 : : G_("passing argument %d of %qE discards "
8744 : : "%qv qualifier from pointer target type"),
8745 : : G_("assignment discards %qv qualifier "
8746 : : "from pointer target type"),
8747 : : G_("initialization discards %qv qualifier "
8748 : : "from pointer target type"),
8749 : : G_("return discards %qv qualifier from "
8750 : : "pointer target type"),
8751 : : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8752 : : }
8753 : 1982681 : else if (pedantic
8754 : 146033 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
8755 : 146013 : ||
8756 : : (VOID_TYPE_P (ttr)
8757 : 2952 : && !null_pointer_constant
8758 : 178 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
8759 : 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
8760 : : G_("ISO C forbids passing argument %d of "
8761 : : "%qE between function pointer "
8762 : : "and %<void *%>"),
8763 : : G_("ISO C forbids assignment between "
8764 : : "function pointer and %<void *%>"),
8765 : : G_("ISO C forbids initialization between "
8766 : : "function pointer and %<void *%>"),
8767 : : G_("ISO C forbids return between function "
8768 : : "pointer and %<void *%>"));
8769 : : /* Const and volatile mean something different for function types,
8770 : : so the usual warnings are not appropriate. */
8771 : 1982647 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
8772 : 1950233 : && TREE_CODE (ttl) != FUNCTION_TYPE)
8773 : : {
8774 : : /* Assignments between atomic and non-atomic objects are OK. */
8775 : 1949568 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8776 : 1949568 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
8777 : 1949568 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8778 : 1949568 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
8779 : :
8780 : : /* Don't warn about loss of qualifier for conversions from
8781 : : qualified void* to pointers to arrays with corresponding
8782 : : qualifier on the element type (except for pedantic before C23). */
8783 : 1949568 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
8784 : 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8785 : : OPT_Wdiscarded_qualifiers,
8786 : : G_("passing argument %d of %qE discards "
8787 : : "%qv qualifier from pointer target type"),
8788 : : G_("assignment discards %qv qualifier "
8789 : : "from pointer target type"),
8790 : : G_("initialization discards %qv qualifier "
8791 : : "from pointer target type"),
8792 : : G_("return discards %qv qualifier from "
8793 : : "pointer target type"),
8794 : : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8795 : 11 : else if (warn_quals_ped)
8796 : 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
8797 : : "array with qualifier on the element is not qualified before C23");
8798 : :
8799 : : /* If this is not a case of ignoring a mismatch in signedness,
8800 : : no warning. */
8801 : 1948860 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
8802 : 1514070 : || target_cmp)
8803 : : ;
8804 : : /* If there is a mismatch, do warn. */
8805 : 1450 : else if (warn_pointer_sign)
8806 : 82 : switch (errtype)
8807 : : {
8808 : 33 : case ic_argpass:
8809 : 33 : {
8810 : 33 : auto_diagnostic_group d;
8811 : 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
8812 : 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
8813 : 33 : highlight_colors::actual);
8814 : 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
8815 : : "pointer targets in passing argument %d of "
8816 : : "%qE differ in signedness", parmnum, rname))
8817 : 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
8818 : : rhstype);
8819 : 33 : }
8820 : 33 : break;
8821 : 21 : case ic_assign:
8822 : 21 : pedwarn (location, OPT_Wpointer_sign,
8823 : : "pointer targets in assignment from %qT to %qT "
8824 : : "differ in signedness", rhstype, type);
8825 : 21 : break;
8826 : 14 : case ic_init:
8827 : 14 : case ic_init_const:
8828 : 14 : pedwarn_init (location, OPT_Wpointer_sign,
8829 : : "pointer targets in initialization of %qT "
8830 : : "from %qT differ in signedness", type,
8831 : : rhstype);
8832 : 14 : break;
8833 : 14 : case ic_return:
8834 : 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
8835 : : "returning %qT from a function with return type "
8836 : : "%qT differ in signedness", rhstype, type);
8837 : 14 : break;
8838 : 0 : default:
8839 : 0 : gcc_unreachable ();
8840 : : }
8841 : : }
8842 : 33079 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
8843 : 4118 : && TREE_CODE (ttr) == FUNCTION_TYPE)
8844 : : {
8845 : : /* Because const and volatile on functions are restrictions
8846 : : that say the function will not do certain things,
8847 : : it is okay to use a const or volatile function
8848 : : where an ordinary one is wanted, but not vice-versa. */
8849 : 3453 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8850 : 3453 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8851 : 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8852 : : OPT_Wdiscarded_qualifiers,
8853 : : G_("passing argument %d of %qE makes "
8854 : : "%q#v qualified function pointer "
8855 : : "from unqualified"),
8856 : : G_("assignment makes %q#v qualified function "
8857 : : "pointer from unqualified"),
8858 : : G_("initialization makes %q#v qualified "
8859 : : "function pointer from unqualified"),
8860 : : G_("return makes %q#v qualified function "
8861 : : "pointer from unqualified"),
8862 : : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8863 : : }
8864 : : }
8865 : : /* Avoid warning about the volatile ObjC EH puts on decls. */
8866 : 799 : else if (!objc_ok)
8867 : : {
8868 : 799 : auto_diagnostic_group d;
8869 : 799 : bool warned = false;
8870 : 799 : pp_markup::element_expected_type e_type (type);
8871 : 799 : pp_markup::element_actual_type e_rhstype (rhstype);
8872 : 799 : switch (errtype)
8873 : : {
8874 : 306 : case ic_argpass:
8875 : 306 : {
8876 : 306 : range_label_for_type_mismatch rhs_label (rhstype, type);
8877 : 306 : gcc_rich_location richloc (expr_loc, &rhs_label,
8878 : 306 : highlight_colors::actual);
8879 : 306 : warned
8880 : 306 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
8881 : : "passing argument %d of %qE from "
8882 : : "incompatible pointer type",
8883 : : parmnum, rname);
8884 : 306 : if (warned)
8885 : 174 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8886 : 306 : }
8887 : 306 : break;
8888 : 267 : case ic_assign:
8889 : 267 : if (bltin)
8890 : 11 : warned
8891 : 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
8892 : : "assignment to %e from pointer to "
8893 : : "%qD with incompatible type %e",
8894 : : &e_type, bltin, &e_rhstype);
8895 : : else
8896 : 256 : warned
8897 : 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
8898 : : "assignment to %e from incompatible "
8899 : : "pointer type %e",
8900 : : &e_type, &e_rhstype);
8901 : : break;
8902 : 167 : case ic_init:
8903 : 167 : case ic_init_const:
8904 : 167 : if (bltin)
8905 : 13 : warned
8906 : 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
8907 : : "initialization of %e from pointer to "
8908 : : "%qD with incompatible type %e",
8909 : : &e_type, bltin, &e_rhstype);
8910 : : else
8911 : 154 : warned
8912 : 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
8913 : : "initialization of %e from incompatible "
8914 : : "pointer type %e",
8915 : : &e_type, &e_rhstype);
8916 : : break;
8917 : 59 : case ic_return:
8918 : 59 : if (bltin)
8919 : 11 : warned
8920 : 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
8921 : : "returning pointer to %qD of type %e from "
8922 : : "a function with incompatible type %e",
8923 : : bltin, &e_rhstype, &e_type);
8924 : : else
8925 : 48 : warned
8926 : 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
8927 : : "returning %e from a function with "
8928 : : "incompatible return type %e",
8929 : : &e_rhstype, &e_type);
8930 : : break;
8931 : 0 : default:
8932 : 0 : gcc_unreachable ();
8933 : : }
8934 : 799 : if (warned)
8935 : : {
8936 : : /* If the mismatching function type is a pointer to a function,
8937 : : try to show the decl of the function. */
8938 : 589 : if (TREE_CODE (rhs) == ADDR_EXPR
8939 : 589 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
8940 : : {
8941 : 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
8942 : 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
8943 : : {
8944 : 165 : gcc_rich_location richloc
8945 : 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
8946 : 165 : highlight_colors::actual);
8947 : 165 : pp_element_quoted_decl e_rhs_fndecl
8948 : 165 : (rhs_fndecl, highlight_colors::actual);
8949 : 165 : inform (&richloc,
8950 : : "%e declared here", &e_rhs_fndecl);
8951 : 165 : }
8952 : : }
8953 : : /* If either/both of the types are typedefs, show the decl. */
8954 : 589 : maybe_inform_typedef_location (type,
8955 : : highlight_colors::expected);
8956 : 589 : maybe_inform_typedef_location (rhstype,
8957 : : highlight_colors::actual);
8958 : : }
8959 : 799 : }
8960 : :
8961 : : /* If RHS isn't an address, check pointer or array of packed
8962 : : struct or union. */
8963 : 1985608 : warn_for_address_of_packed_member (type, orig_rhs);
8964 : :
8965 : 1985608 : return convert (type, rhs);
8966 : : }
8967 : 79102 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
8968 : : {
8969 : : /* ??? This should not be an error when inlining calls to
8970 : : unprototyped functions. */
8971 : 4 : const char msg[] = "invalid use of non-lvalue array";
8972 : 4 : if (warnopt)
8973 : 0 : warning_at (location, warnopt, msg);
8974 : : else
8975 : 4 : error_at (location, msg);
8976 : 4 : return error_mark_node;
8977 : : }
8978 : 79098 : else if (codel == POINTER_TYPE
8979 : 77830 : && (coder == INTEGER_TYPE
8980 : 77830 : || coder == ENUMERAL_TYPE
8981 : 600 : || coder == BOOLEAN_TYPE
8982 : 600 : || coder == NULLPTR_TYPE
8983 : 20 : || coder == BITINT_TYPE))
8984 : : {
8985 : 77822 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
8986 : 76824 : && coder == INTEGER_TYPE)
8987 : 76227 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
8988 : : "zero as null pointer constant");
8989 : : /* A NULLPTR type is just a nullptr always. */
8990 : 77822 : if (coder == NULLPTR_TYPE)
8991 : 568 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
8992 : : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
8993 : : or one that results from arithmetic, even including a cast to
8994 : : integer type. */
8995 : 77254 : else if (!null_pointer_constant)
8996 : 972 : switch (errtype)
8997 : : {
8998 : 800 : case ic_argpass:
8999 : 800 : {
9000 : 800 : auto_diagnostic_group d;
9001 : 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9002 : 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9003 : 800 : highlight_colors::actual);
9004 : 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9005 : : "passing argument %d of %qE makes pointer "
9006 : : "from integer without a cast", parmnum, rname))
9007 : : {
9008 : 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9009 : 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9010 : : }
9011 : 800 : }
9012 : 800 : break;
9013 : 93 : case ic_assign:
9014 : 93 : permerror_opt (location, OPT_Wint_conversion,
9015 : : "assignment to %qT from %qT makes pointer from "
9016 : : "integer without a cast", type, rhstype);
9017 : 93 : break;
9018 : 56 : case ic_init:
9019 : 56 : case ic_init_const:
9020 : 56 : permerror_init (location, OPT_Wint_conversion,
9021 : : "initialization of %qT from %qT makes pointer "
9022 : : "from integer without a cast", type, rhstype);
9023 : 56 : break;
9024 : 23 : case ic_return:
9025 : 23 : permerror_init (location, OPT_Wint_conversion,
9026 : : "returning %qT from a function with return type "
9027 : : "%qT makes pointer from integer without a cast",
9028 : : rhstype, type);
9029 : 23 : break;
9030 : 0 : default:
9031 : 0 : gcc_unreachable ();
9032 : : }
9033 : :
9034 : 77254 : return convert (type, rhs);
9035 : : }
9036 : 1276 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9037 : 539 : && coder == POINTER_TYPE)
9038 : : {
9039 : 513 : switch (errtype)
9040 : : {
9041 : 343 : case ic_argpass:
9042 : 343 : {
9043 : 343 : auto_diagnostic_group d;
9044 : 343 : range_label_for_type_mismatch rhs_label (rhstype, type);
9045 : 343 : gcc_rich_location richloc (expr_loc, &rhs_label,
9046 : 343 : highlight_colors::actual);
9047 : 343 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9048 : : "passing argument %d of %qE makes integer from "
9049 : : "pointer without a cast", parmnum, rname))
9050 : : {
9051 : 322 : maybe_emit_indirection_note (expr_loc, rhs, type);
9052 : 322 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9053 : : }
9054 : 343 : }
9055 : 343 : break;
9056 : 52 : case ic_assign:
9057 : 52 : permerror_opt (location, OPT_Wint_conversion,
9058 : : "assignment to %qT from %qT makes integer from "
9059 : : "pointer without a cast", type, rhstype);
9060 : 52 : break;
9061 : 83 : case ic_init:
9062 : 83 : case ic_init_const:
9063 : 83 : permerror_init (location, OPT_Wint_conversion,
9064 : : "initialization of %qT from %qT makes integer "
9065 : : "from pointer without a cast", type, rhstype);
9066 : 83 : break;
9067 : 35 : case ic_return:
9068 : 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9069 : : "function with return type %qT makes integer from "
9070 : : "pointer without a cast", rhstype, type);
9071 : 35 : break;
9072 : 0 : default:
9073 : 0 : gcc_unreachable ();
9074 : : }
9075 : :
9076 : 513 : return convert (type, rhs);
9077 : : }
9078 : 615 : else if (C_BOOLEAN_TYPE_P (type)
9079 : : /* The type nullptr_t may be converted to bool. The
9080 : : result is false. */
9081 : 765 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9082 : : {
9083 : 150 : tree ret;
9084 : 150 : bool save = in_late_binary_op;
9085 : 150 : in_late_binary_op = true;
9086 : 150 : ret = convert (type, rhs);
9087 : 150 : in_late_binary_op = save;
9088 : 150 : return ret;
9089 : : }
9090 : 613 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9091 : 6 : return convert (type, rhs);
9092 : :
9093 : 607 : switch (errtype)
9094 : : {
9095 : 35 : case ic_argpass:
9096 : 35 : {
9097 : 35 : auto_diagnostic_group d;
9098 : 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9099 : 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9100 : 35 : highlight_colors::actual);
9101 : 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9102 : 35 : if (warnopt)
9103 : 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9104 : : else
9105 : 27 : error_at (&richloc, msg, parmnum, rname);
9106 : 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9107 : 35 : }
9108 : 35 : break;
9109 : 99 : case ic_assign:
9110 : 99 : {
9111 : 99 : const char msg[]
9112 : : = G_("incompatible types when assigning to type %qT from type %qT");
9113 : 99 : if (warnopt)
9114 : 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9115 : : else
9116 : 99 : error_at (expr_loc, msg, type, rhstype);
9117 : 99 : break;
9118 : : }
9119 : 458 : case ic_init:
9120 : 458 : case ic_init_const:
9121 : 458 : {
9122 : 458 : const char msg[]
9123 : : = G_("incompatible types when initializing type %qT using type %qT");
9124 : 458 : if (warnopt)
9125 : 0 : warning_at (location, 0, msg, type, rhstype);
9126 : : else
9127 : 458 : error_at (location, msg, type, rhstype);
9128 : 458 : break;
9129 : : }
9130 : 15 : case ic_return:
9131 : 15 : {
9132 : 15 : const char msg[]
9133 : : = G_("incompatible types when returning type %qT but %qT was expected");
9134 : 15 : if (warnopt)
9135 : 0 : warning_at (location, 0, msg, rhstype, type);
9136 : : else
9137 : 15 : error_at (location, msg, rhstype, type);
9138 : 15 : break;
9139 : : }
9140 : 0 : default:
9141 : 0 : gcc_unreachable ();
9142 : : }
9143 : :
9144 : 607 : return error_mark_node;
9145 : : }
9146 : :
9147 : : /* If VALUE is a compound expr all of whose expressions are constant, then
9148 : : return its value. Otherwise, return error_mark_node.
9149 : :
9150 : : This is for handling COMPOUND_EXPRs as initializer elements
9151 : : which is allowed with a warning when -pedantic is specified. */
9152 : :
9153 : : static tree
9154 : 2 : valid_compound_expr_initializer (tree value, tree endtype)
9155 : : {
9156 : 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9157 : : {
9158 : 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9159 : 1 : == error_mark_node)
9160 : : return error_mark_node;
9161 : 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9162 : 0 : endtype);
9163 : : }
9164 : 1 : else if (!initializer_constant_valid_p (value, endtype))
9165 : 1 : return error_mark_node;
9166 : : else
9167 : : return value;
9168 : : }
9169 : :
9170 : : /* Perform appropriate conversions on the initial value of a variable,
9171 : : store it in the declaration DECL,
9172 : : and print any error messages that are appropriate.
9173 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9174 : : If the init is invalid, store an ERROR_MARK.
9175 : :
9176 : : INIT_LOC is the location of the initial value. */
9177 : :
9178 : : void
9179 : 7097830 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9180 : : {
9181 : 7097830 : tree value, type;
9182 : 7097830 : bool npc = false;
9183 : 7097830 : bool int_const_expr = false;
9184 : 7097830 : bool arith_const_expr = false;
9185 : :
9186 : : /* If variable's type was invalidly declared, just ignore it. */
9187 : :
9188 : 7097830 : type = TREE_TYPE (decl);
9189 : 7097830 : if (TREE_CODE (type) == ERROR_MARK)
9190 : : return;
9191 : :
9192 : : /* Digest the specified initializer into an expression. */
9193 : :
9194 : 7097823 : if (init)
9195 : : {
9196 : 7097823 : npc = null_pointer_constant_p (init);
9197 : 7097823 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9198 : 409136 : && !TREE_OVERFLOW (init)
9199 : 7506923 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9200 : : /* Not fully determined before folding. */
9201 : : arith_const_expr = true;
9202 : : }
9203 : 7097823 : bool constexpr_p = (VAR_P (decl)
9204 : 7097823 : && C_DECL_DECLARED_CONSTEXPR (decl));
9205 : 7097823 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9206 : : int_const_expr, arith_const_expr, true,
9207 : 7097823 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9208 : :
9209 : : /* Store the expression if valid; else report error. */
9210 : :
9211 : 7097823 : if (!in_system_header_at (input_location)
9212 : 7097823 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9213 : 27399 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9214 : : "aggregate initialization");
9215 : :
9216 : 7097823 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9217 : 7097822 : DECL_INITIAL (decl) = value;
9218 : :
9219 : : /* ANSI wants warnings about out-of-range constant initializers. */
9220 : 7097834 : STRIP_TYPE_NOPS (value);
9221 : 7097823 : if (TREE_STATIC (decl))
9222 : 179824 : constant_expression_warning (value);
9223 : :
9224 : : /* Check if we need to set array size from compound literal size. */
9225 : 7097823 : if (TREE_CODE (type) == ARRAY_TYPE
9226 : 26002 : && TYPE_DOMAIN (type) == NULL_TREE
9227 : 7110137 : && value != error_mark_node)
9228 : : {
9229 : : tree inside_init = init;
9230 : :
9231 : 11474 : STRIP_TYPE_NOPS (inside_init);
9232 : 11474 : inside_init = fold (inside_init);
9233 : :
9234 : 11474 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9235 : : {
9236 : 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9237 : :
9238 : 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9239 : : {
9240 : : /* For int foo[] = (int [3]){1}; we need to set array size
9241 : : now since later on array initializer will be just the
9242 : : brace enclosed list of the compound literal. */
9243 : 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9244 : 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9245 : 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9246 : 13 : layout_type (type);
9247 : 13 : layout_decl (cldecl, 0);
9248 : 13 : TREE_TYPE (decl)
9249 : 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9250 : : }
9251 : : }
9252 : : }
9253 : : }
9254 : :
9255 : : /* Methods for storing and printing names for error messages. */
9256 : :
9257 : : /* Implement a spelling stack that allows components of a name to be pushed
9258 : : and popped. Each element on the stack is this structure. */
9259 : :
9260 : : struct spelling
9261 : : {
9262 : : int kind;
9263 : : union
9264 : : {
9265 : : unsigned HOST_WIDE_INT i;
9266 : : const char *s;
9267 : : } u;
9268 : : };
9269 : :
9270 : : #define SPELLING_STRING 1
9271 : : #define SPELLING_MEMBER 2
9272 : : #define SPELLING_BOUNDS 3
9273 : :
9274 : : static struct spelling *spelling; /* Next stack element (unused). */
9275 : : static struct spelling *spelling_base; /* Spelling stack base. */
9276 : : static int spelling_size; /* Size of the spelling stack. */
9277 : :
9278 : : /* Macros to save and restore the spelling stack around push_... functions.
9279 : : Alternative to SAVE_SPELLING_STACK. */
9280 : :
9281 : : #define SPELLING_DEPTH() (spelling - spelling_base)
9282 : : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9283 : :
9284 : : /* Push an element on the spelling stack with type KIND and assign VALUE
9285 : : to MEMBER. */
9286 : :
9287 : : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9288 : : { \
9289 : : int depth = SPELLING_DEPTH (); \
9290 : : \
9291 : : if (depth >= spelling_size) \
9292 : : { \
9293 : : spelling_size += 10; \
9294 : : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9295 : : spelling_size); \
9296 : : RESTORE_SPELLING_DEPTH (depth); \
9297 : : } \
9298 : : \
9299 : : spelling->kind = (KIND); \
9300 : : spelling->MEMBER = (VALUE); \
9301 : : spelling++; \
9302 : : }
9303 : :
9304 : : /* Push STRING on the stack. Printed literally. */
9305 : :
9306 : : static void
9307 : 7095144 : push_string (const char *string)
9308 : : {
9309 : 7095144 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9310 : 7095144 : }
9311 : :
9312 : : /* Push a member name on the stack. Printed as '.' STRING. */
9313 : :
9314 : : static void
9315 : 1225427 : push_member_name (tree decl)
9316 : : {
9317 : 1225427 : const char *const string
9318 : 1225427 : = (DECL_NAME (decl)
9319 : 1225427 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9320 : 194 : : _("<anonymous>"));
9321 : 1225427 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9322 : 1225427 : }
9323 : :
9324 : : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9325 : :
9326 : : static void
9327 : 2598486 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9328 : : {
9329 : 2598486 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9330 : 2598486 : }
9331 : :
9332 : : /* Compute the maximum size in bytes of the printed spelling. */
9333 : :
9334 : : static int
9335 : 2830 : spelling_length (void)
9336 : : {
9337 : 2830 : int size = 0;
9338 : 2830 : struct spelling *p;
9339 : :
9340 : 6318 : for (p = spelling_base; p < spelling; p++)
9341 : : {
9342 : 3488 : if (p->kind == SPELLING_BOUNDS)
9343 : 1475 : size += 25;
9344 : : else
9345 : 2013 : size += strlen (p->u.s) + 1;
9346 : : }
9347 : :
9348 : 2830 : return size;
9349 : : }
9350 : :
9351 : : /* Print the spelling to BUFFER and return it. */
9352 : :
9353 : : static char *
9354 : 2830 : print_spelling (char *buffer)
9355 : : {
9356 : 2830 : char *d = buffer;
9357 : 2830 : struct spelling *p;
9358 : :
9359 : 6318 : for (p = spelling_base; p < spelling; p++)
9360 : 3488 : if (p->kind == SPELLING_BOUNDS)
9361 : : {
9362 : 1475 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9363 : 1475 : d += strlen (d);
9364 : : }
9365 : : else
9366 : : {
9367 : 2013 : const char *s;
9368 : 2013 : if (p->kind == SPELLING_MEMBER)
9369 : 190 : *d++ = '.';
9370 : 16603 : for (s = p->u.s; (*d = *s++); d++)
9371 : : ;
9372 : : }
9373 : 2830 : *d++ = '\0';
9374 : 2830 : return buffer;
9375 : : }
9376 : :
9377 : : /* Check whether INIT, a floating or integer constant, is
9378 : : representable in TYPE, a real floating type with the same radix or
9379 : : a decimal floating type initialized with a binary floating
9380 : : constant. Return true if OK, false if not. */
9381 : : static bool
9382 : 362 : constexpr_init_fits_real_type (tree type, tree init)
9383 : : {
9384 : 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9385 : 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9386 : 362 : if (TREE_CODE (init) == REAL_CST
9387 : 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9388 : : {
9389 : : /* Same mode, no conversion required except for the case of
9390 : : signaling NaNs if the types are incompatible (e.g. double and
9391 : : long double with the same mode). */
9392 : 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9393 : 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9394 : 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9395 : : return false;
9396 : 177 : return true;
9397 : : }
9398 : 185 : if (TREE_CODE (init) == INTEGER_CST)
9399 : : {
9400 : 54 : tree converted = build_real_from_int_cst (type, init);
9401 : 54 : bool fail = false;
9402 : 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9403 : 54 : TYPE_PRECISION (TREE_TYPE (init)));
9404 : 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9405 : 54 : }
9406 : 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9407 : : return false;
9408 : 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9409 : 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9410 : 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9411 : 34 : && MODE_HAS_NANS (TYPE_MODE (type))))
9412 : 20 : return true;
9413 : 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9414 : 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9415 : : {
9416 : : /* This is valid if the real number represented by the
9417 : : initializer can be exactly represented in the decimal
9418 : : type. Compare the values using MPFR. */
9419 : 8 : REAL_VALUE_TYPE t;
9420 : 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9421 : 8 : mpfr_t bin_val, dec_val;
9422 : 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9423 : 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9424 : 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9425 : 8 : char string[256];
9426 : 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9427 : 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9428 : 8 : && mpfr_equal_p (bin_val, dec_val));
9429 : 8 : mpfr_clear (bin_val);
9430 : 8 : mpfr_clear (dec_val);
9431 : 8 : return res;
9432 : : }
9433 : : /* exact_real_truncate is not quite right here, since it doesn't
9434 : : allow even an exact conversion to subnormal values. */
9435 : 86 : REAL_VALUE_TYPE t;
9436 : 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9437 : 86 : return real_identical (&t, &TREE_REAL_CST (init));
9438 : : }
9439 : :
9440 : : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9441 : : initializer for type TYPE, and give an error if not. INIT has
9442 : : already been folded and verified to be constant. INT_CONST_EXPR
9443 : : and ARITH_CONST_EXPR say whether it is an integer constant
9444 : : expression or arithmetic constant expression, respectively. If
9445 : : TYPE is not a scalar type, this function does nothing. */
9446 : :
9447 : : static void
9448 : 900 : check_constexpr_init (location_t loc, tree type, tree init,
9449 : : bool int_const_expr, bool arith_const_expr)
9450 : : {
9451 : 900 : if (POINTER_TYPE_P (type))
9452 : : {
9453 : : /* The initializer must be null. */
9454 : 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9455 : 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9456 : 86 : return;
9457 : : }
9458 : 814 : if (INTEGRAL_TYPE_P (type))
9459 : : {
9460 : : /* The initializer must be an integer constant expression,
9461 : : representable in the target type. */
9462 : 307 : if (!int_const_expr)
9463 : : {
9464 : 13 : if (TREE_CODE (init) == RAW_DATA_CST
9465 : 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9466 : : {
9467 : 4 : if (!TYPE_UNSIGNED (type))
9468 : 137 : for (unsigned int i = 0;
9469 : 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9470 : 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9471 : : {
9472 : 1 : error_at (loc, "%<constexpr%> initializer not "
9473 : : "representable in type of object");
9474 : 1 : break;
9475 : : }
9476 : : }
9477 : : else
9478 : 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9479 : : "integer constant expression");
9480 : : }
9481 : 294 : else if (!int_fits_type_p (init, type))
9482 : 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9483 : : "type of object");
9484 : 307 : return;
9485 : : }
9486 : : /* We don't apply any extra checks to extension types such as vector
9487 : : or fixed-point types. */
9488 : 507 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9489 : : return;
9490 : 353 : if (!arith_const_expr)
9491 : : {
9492 : 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9493 : : "constant expression");
9494 : 5 : return;
9495 : : }
9496 : : /* We don't apply any extra checks to complex integers. */
9497 : 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9498 : 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9499 : : return;
9500 : : /* Following N3082, a real type cannot be initialized from a complex
9501 : : type and a binary type cannot be initialized from a decimal type
9502 : : (but initializing a decimal type from a binary type is OK).
9503 : : Signaling NaN initializers are OK only if the types are
9504 : : compatible (not just the same mode); all quiet NaN and infinity
9505 : : initializations are considered to preserve the value. */
9506 : 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9507 : 348 : && SCALAR_FLOAT_TYPE_P (type))
9508 : : {
9509 : 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9510 : : "complex type");
9511 : 6 : return;
9512 : : }
9513 : 342 : if (SCALAR_FLOAT_TYPE_P (type)
9514 : 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9515 : 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9516 : 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9517 : : {
9518 : 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9519 : : "floating-point type is of decimal type");
9520 : 6 : return;
9521 : : }
9522 : 336 : bool fits;
9523 : 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9524 : : {
9525 : 42 : switch (TREE_CODE (init))
9526 : : {
9527 : 10 : case INTEGER_CST:
9528 : 10 : case REAL_CST:
9529 : 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9530 : 10 : break;
9531 : 32 : case COMPLEX_CST:
9532 : 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9533 : 32 : TREE_REALPART (init))
9534 : 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9535 : 26 : TREE_IMAGPART (init)));
9536 : : break;
9537 : 0 : default:
9538 : 0 : gcc_unreachable ();
9539 : : }
9540 : : }
9541 : : else
9542 : 294 : fits = constexpr_init_fits_real_type (type, init);
9543 : 304 : if (!fits)
9544 : 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9545 : : "type of object");
9546 : : }
9547 : :
9548 : : /* Digest the parser output INIT as an initializer for type TYPE
9549 : : initializing DECL.
9550 : : Return a C expression of type TYPE to represent the initial value.
9551 : :
9552 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9553 : :
9554 : : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9555 : : INT_CONST_EXPR is true if INIT is an integer constant expression,
9556 : : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9557 : : constant expression, false if it has already been determined in the
9558 : : caller that it is not (but folding may have made the value passed here
9559 : : indistinguishable from an arithmetic constant expression).
9560 : :
9561 : : If INIT is a string constant, STRICT_STRING is true if it is
9562 : : unparenthesized or we should not warn here for it being parenthesized.
9563 : : For other types of INIT, STRICT_STRING is not used.
9564 : :
9565 : : INIT_LOC is the location of the INIT.
9566 : :
9567 : : REQUIRE_CONSTANT requests an error if non-constant initializers or
9568 : : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9569 : : on initializers for 'constexpr' objects apply. */
9570 : :
9571 : : static tree
9572 : 16560523 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9573 : : tree origtype, bool null_pointer_constant, bool int_const_expr,
9574 : : bool arith_const_expr, bool strict_string,
9575 : : bool require_constant, bool require_constexpr)
9576 : : {
9577 : 16560523 : enum tree_code code = TREE_CODE (type);
9578 : 16560523 : tree inside_init = init;
9579 : 16560523 : tree semantic_type = NULL_TREE;
9580 : 16560523 : bool maybe_const = true;
9581 : :
9582 : 16560523 : if (type == error_mark_node
9583 : 16560523 : || !init
9584 : 33121046 : || error_operand_p (init))
9585 : : return error_mark_node;
9586 : :
9587 : 16567743 : STRIP_TYPE_NOPS (inside_init);
9588 : :
9589 : : /* If require_constant is TRUE, when the initializer is a call to
9590 : : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9591 : : For example:
9592 : : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9593 : : will be converted to
9594 : : y = &static_annotated.c. */
9595 : :
9596 : 16559198 : if (require_constant
9597 : 2890247 : && TREE_CODE (inside_init) == NOP_EXPR
9598 : 736216 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9599 : 16559200 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9600 : 2 : inside_init
9601 : 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9602 : :
9603 : 16559198 : if (!c_in_omp_for)
9604 : : {
9605 : 16554462 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9606 : : {
9607 : 437 : semantic_type = TREE_TYPE (inside_init);
9608 : 437 : inside_init = TREE_OPERAND (inside_init, 0);
9609 : : }
9610 : 16554462 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9611 : : }
9612 : : /* TODO: this may not detect all cases of expressions folding to
9613 : : constants that are not arithmetic constant expressions. */
9614 : 16559198 : if (!maybe_const)
9615 : : arith_const_expr = false;
9616 : 24744875 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9617 : 5565484 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9618 : 16535315 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9619 : : arith_const_expr = false;
9620 : 8237501 : else if (TREE_CODE (inside_init) != INTEGER_CST
9621 : : && TREE_CODE (inside_init) != REAL_CST
9622 : : && TREE_CODE (inside_init) != COMPLEX_CST
9623 : : && TREE_CODE (inside_init) != RAW_DATA_CST)
9624 : : arith_const_expr = false;
9625 : 4981669 : else if (TREE_OVERFLOW (inside_init))
9626 : 11577573 : arith_const_expr = false;
9627 : :
9628 : : /* Initialization of an array of chars from a string constant
9629 : : optionally enclosed in braces. */
9630 : :
9631 : 16559198 : if (code == ARRAY_TYPE && inside_init
9632 : 187673 : && TREE_CODE (inside_init) == STRING_CST)
9633 : : {
9634 : 11533 : tree typ1
9635 : 11533 : = (TYPE_ATOMIC (TREE_TYPE (type))
9636 : 11533 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9637 : : TYPE_QUAL_ATOMIC)
9638 : 11524 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9639 : : /* Note that an array could be both an array of character type
9640 : : and an array of wchar_t if wchar_t is signed char or unsigned
9641 : : char. */
9642 : 23066 : bool char_array = (typ1 == char_type_node
9643 : 462 : || typ1 == signed_char_type_node
9644 : 11966 : || typ1 == unsigned_char_type_node);
9645 : 11533 : bool wchar_array = !!comptypes (typ1, wchar_type_node);
9646 : 11533 : bool char16_array = !!comptypes (typ1, char16_type_node);
9647 : 11533 : bool char32_array = !!comptypes (typ1, char32_type_node);
9648 : :
9649 : 11533 : if (char_array || wchar_array || char16_array || char32_array)
9650 : : {
9651 : 11513 : struct c_expr expr;
9652 : 11513 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9653 : 11513 : bool incompat_string_cst = false;
9654 : 11513 : expr.value = inside_init;
9655 : 11513 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9656 : 11513 : expr.original_type = NULL;
9657 : 11513 : expr.m_decimal = 0;
9658 : 11513 : maybe_warn_string_init (init_loc, type, expr);
9659 : :
9660 : 11513 : if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
9661 : 91 : pedwarn_init (init_loc, OPT_Wpedantic,
9662 : : "initialization of a flexible array member");
9663 : :
9664 : 11513 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9665 : 11513 : TYPE_MAIN_VARIANT (type)))
9666 : : return inside_init;
9667 : :
9668 : 4137 : if (char_array)
9669 : : {
9670 : 4042 : if (typ2 != char_type_node && typ2 != char8_type_node)
9671 : : incompat_string_cst = true;
9672 : : }
9673 : 95 : else if (!comptypes (typ1, typ2))
9674 : : incompat_string_cst = true;
9675 : :
9676 : : if (incompat_string_cst)
9677 : : {
9678 : 50 : error_init (init_loc, "cannot initialize array of %qT from "
9679 : : "a string literal with type array of %qT",
9680 : : typ1, typ2);
9681 : 50 : return error_mark_node;
9682 : : }
9683 : :
9684 : 4087 : if (require_constexpr
9685 : 4087 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
9686 : : {
9687 : : /* Check if all characters of the string can be
9688 : : represented in the type of the constexpr object being
9689 : : initialized. */
9690 : 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9691 : 24 : const unsigned char *p =
9692 : 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
9693 : 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
9694 : 70 : for (unsigned i = 0; i < len; i++)
9695 : 58 : if (p[i] > 127)
9696 : : {
9697 : 12 : error_init (init_loc, "%<constexpr%> initializer not "
9698 : : "representable in type of object");
9699 : 12 : break;
9700 : : }
9701 : : }
9702 : :
9703 : 4087 : if (TYPE_DOMAIN (type) != NULL_TREE
9704 : 4010 : && TYPE_SIZE (type) != NULL_TREE
9705 : 8038 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9706 : : {
9707 : 3951 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9708 : :
9709 : 3951 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
9710 : : {
9711 : 395 : unsigned HOST_WIDE_INT avail
9712 : 395 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9713 : 395 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
9714 : 395 : const char *p = TREE_STRING_POINTER (inside_init);
9715 : :
9716 : : /* Construct truncated string. */
9717 : 395 : inside_init = build_string (avail, p);
9718 : :
9719 : : /* Subtract the size of a single (possibly wide) character
9720 : : because it may be ok to ignore the terminating NUL char
9721 : : that is counted in the length of the constant. */
9722 : 395 : if (len - unit > avail)
9723 : 63 : pedwarn_init (init_loc, 0,
9724 : : "initializer-string for array of %qT "
9725 : : "is too long (%wu chars into %wu "
9726 : : "available)", typ1, len, avail);
9727 : 332 : else if (warn_cxx_compat)
9728 : 9 : warning_at (init_loc, OPT_Wc___compat,
9729 : : "initializer-string for array of %qT "
9730 : : "is too long for C++ (%wu chars into %wu "
9731 : : "available)", typ1, len, avail);
9732 : 323 : else if (warn_unterminated_string_initialization
9733 : 323 : && get_attr_nonstring_decl (decl) == NULL_TREE)
9734 : 22 : warning_at (init_loc,
9735 : 22 : OPT_Wunterminated_string_initialization,
9736 : : "initializer-string for array of %qT "
9737 : : "truncates NUL terminator but destination "
9738 : : "lacks %qs attribute (%wu chars into %wu "
9739 : : "available)", typ1, "nonstring", len, avail);
9740 : : }
9741 : : }
9742 : :
9743 : 4087 : TREE_TYPE (inside_init) = type;
9744 : 4087 : return inside_init;
9745 : : }
9746 : 20 : else if (INTEGRAL_TYPE_P (typ1))
9747 : : {
9748 : 20 : error_init (init_loc, "array of inappropriate type initialized "
9749 : : "from string constant");
9750 : 20 : return error_mark_node;
9751 : : }
9752 : : }
9753 : :
9754 : : /* Build a VECTOR_CST from a *constant* vector constructor. If the
9755 : : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
9756 : : below and handle as a constructor. */
9757 : 16547665 : if (code == VECTOR_TYPE
9758 : 6155681 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
9759 : 6155679 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
9760 : 22703306 : && TREE_CONSTANT (inside_init))
9761 : : {
9762 : 618622 : if (TREE_CODE (inside_init) == VECTOR_CST
9763 : 618719 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9764 : 97 : TYPE_MAIN_VARIANT (type)))
9765 : : return inside_init;
9766 : :
9767 : 618525 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
9768 : : {
9769 : : unsigned HOST_WIDE_INT ix;
9770 : : tree value;
9771 : 4082563 : bool constant_p = true;
9772 : :
9773 : : /* Iterate through elements and check if all constructor
9774 : : elements are *_CSTs. */
9775 : 4082563 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
9776 : 3464094 : if (!CONSTANT_CLASS_P (value))
9777 : : {
9778 : : constant_p = false;
9779 : : break;
9780 : : }
9781 : :
9782 : 618525 : if (constant_p)
9783 : 618469 : return build_vector_from_ctor (type,
9784 : 618469 : CONSTRUCTOR_ELTS (inside_init));
9785 : : }
9786 : : }
9787 : :
9788 : 15929099 : if (warn_sequence_point)
9789 : 2447045 : verify_sequence_points (inside_init);
9790 : :
9791 : : /* Any type can be initialized
9792 : : from an expression of the same type, optionally with braces. */
9793 : :
9794 : 15929099 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
9795 : 31858198 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9796 : 15929099 : TYPE_MAIN_VARIANT (type))
9797 : 3074756 : || (code == ARRAY_TYPE
9798 : 476 : && comptypes (TREE_TYPE (inside_init), type))
9799 : 3074756 : || (gnu_vector_type_p (type)
9800 : 159 : && comptypes (TREE_TYPE (inside_init), type))
9801 : 3074756 : || (code == POINTER_TYPE
9802 : 111691 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
9803 : 9260 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
9804 : 9260 : TREE_TYPE (type)))))
9805 : : {
9806 : 12856317 : if (code == POINTER_TYPE)
9807 : : {
9808 : 783506 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
9809 : : {
9810 : 1974 : if (TREE_CODE (inside_init) == STRING_CST
9811 : 66 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9812 : 1972 : inside_init = array_to_pointer_conversion
9813 : 1972 : (init_loc, inside_init);
9814 : : else
9815 : : {
9816 : 2 : error_init (init_loc, "invalid use of non-lvalue array");
9817 : 2 : return error_mark_node;
9818 : : }
9819 : : }
9820 : : }
9821 : :
9822 : 12856315 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
9823 : : /* Although the types are compatible, we may require a
9824 : : conversion. */
9825 : 5537089 : inside_init = convert (type, inside_init);
9826 : :
9827 : 12856315 : if ((code == RECORD_TYPE || code == UNION_TYPE)
9828 : 12856315 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
9829 : : {
9830 : 0 : error_init (init_loc, "invalid initializer");
9831 : 0 : return error_mark_node;
9832 : : }
9833 : :
9834 : 12856315 : if (require_constant
9835 : 2296883 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9836 : : {
9837 : : /* As an extension, allow initializing objects with static storage
9838 : : duration with compound literals (which are then treated just as
9839 : : the brace enclosed list they contain). Also allow this for
9840 : : vectors, as we can only assign them with compound literals. */
9841 : 31 : if (flag_isoc99 && code != VECTOR_TYPE)
9842 : 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
9843 : : "is not constant");
9844 : 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9845 : 31 : inside_init = DECL_INITIAL (decl);
9846 : : }
9847 : :
9848 : 12856315 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
9849 : 175664 : && TREE_CODE (inside_init) != CONSTRUCTOR)
9850 : : {
9851 : 2 : error_init (init_loc, "array initialized from non-constant array "
9852 : : "expression");
9853 : 2 : return error_mark_node;
9854 : : }
9855 : :
9856 : : /* Compound expressions can only occur here if -Wpedantic or
9857 : : -pedantic-errors is specified. In the later case, we always want
9858 : : an error. In the former case, we simply want a warning. */
9859 : 12856313 : if (require_constant && pedantic
9860 : 20192 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
9861 : : {
9862 : 1 : inside_init
9863 : 1 : = valid_compound_expr_initializer (inside_init,
9864 : 1 : TREE_TYPE (inside_init));
9865 : 1 : if (inside_init == error_mark_node)
9866 : 1 : error_init (init_loc, "initializer element is not constant");
9867 : : else
9868 : 0 : pedwarn_init (init_loc, OPT_Wpedantic,
9869 : : "initializer element is not constant");
9870 : 1 : if (flag_pedantic_errors)
9871 : 0 : inside_init = error_mark_node;
9872 : : }
9873 : 2296882 : else if (require_constant
9874 : 2296882 : && !initializer_constant_valid_p (inside_init,
9875 : 2296882 : TREE_TYPE (inside_init)))
9876 : : {
9877 : 94 : error_init (init_loc, "initializer element is not constant");
9878 : 94 : inside_init = error_mark_node;
9879 : : }
9880 : 12856218 : else if (require_constant && !maybe_const)
9881 : 219 : pedwarn_init (init_loc, OPT_Wpedantic,
9882 : : "initializer element is not a constant expression");
9883 : 12855999 : else if (require_constexpr)
9884 : 574 : check_constexpr_init (init_loc, type, inside_init,
9885 : : int_const_expr, arith_const_expr);
9886 : :
9887 : : /* Added to enable additional -Wsuggest-attribute=format warnings. */
9888 : 12856313 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
9889 : 851355 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
9890 : : type, inside_init, origtype,
9891 : : (require_constant
9892 : : ? ic_init_const
9893 : : : ic_init), null_pointer_constant,
9894 : : NULL_TREE, NULL_TREE, 0);
9895 : 12856313 : if (TREE_CODE (inside_init) == RAW_DATA_CST
9896 : 254 : && c_inhibit_evaluation_warnings == 0
9897 : 254 : && warn_conversion
9898 : 1 : && !TYPE_UNSIGNED (type)
9899 : 12856314 : && TYPE_PRECISION (type) == CHAR_BIT)
9900 : 303 : for (unsigned int i = 0;
9901 : 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
9902 : 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
9903 : 10 : warning_at (init_loc, OPT_Wconversion,
9904 : : "conversion from %qT to %qT changes value from "
9905 : : "%qd to %qd",
9906 : : integer_type_node, type,
9907 : 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
9908 : 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
9909 : 12856313 : return inside_init;
9910 : : }
9911 : :
9912 : : /* Handle scalar types, including conversions. */
9913 : :
9914 : 3072782 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
9915 : 127681 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
9916 : 11037 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
9917 : 9005 : || code == BITINT_TYPE)
9918 : : {
9919 : 3072300 : tree unconverted_init = inside_init;
9920 : 3072300 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
9921 : 3072300 : && (TREE_CODE (init) == STRING_CST
9922 : 1 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
9923 : 7287 : inside_init = init = array_to_pointer_conversion (init_loc, init);
9924 : 3072300 : if (semantic_type)
9925 : 432 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9926 : : inside_init);
9927 : 3072300 : inside_init
9928 : 5565877 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
9929 : : inside_init, origtype,
9930 : : require_constant ? ic_init_const : ic_init,
9931 : : null_pointer_constant, NULL_TREE, NULL_TREE,
9932 : : 0);
9933 : :
9934 : : /* Check to see if we have already given an error message. */
9935 : 3072300 : if (inside_init == error_mark_node)
9936 : : ;
9937 : 3071842 : else if (require_constant && !TREE_CONSTANT (inside_init))
9938 : : {
9939 : 36 : error_init (init_loc, "initializer element is not constant");
9940 : 36 : inside_init = error_mark_node;
9941 : : }
9942 : 3071806 : else if (require_constant
9943 : 3650101 : && !initializer_constant_valid_p (inside_init,
9944 : 578295 : TREE_TYPE (inside_init)))
9945 : : {
9946 : 10 : error_init (init_loc, "initializer element is not computable at "
9947 : : "load time");
9948 : 10 : inside_init = error_mark_node;
9949 : : }
9950 : 3071796 : else if (require_constant && !maybe_const)
9951 : 5 : pedwarn_init (init_loc, OPT_Wpedantic,
9952 : : "initializer element is not a constant expression");
9953 : 3071791 : else if (require_constexpr)
9954 : 326 : check_constexpr_init (init_loc, type, unconverted_init,
9955 : : int_const_expr, arith_const_expr);
9956 : :
9957 : 3072300 : return inside_init;
9958 : : }
9959 : :
9960 : : /* Come here only for records and arrays. */
9961 : :
9962 : 482 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
9963 : : {
9964 : 0 : error_init (init_loc,
9965 : : "variable-sized object may not be initialized except "
9966 : : "with an empty initializer");
9967 : 0 : return error_mark_node;
9968 : : }
9969 : :
9970 : 482 : error_init (init_loc, "invalid initializer");
9971 : 482 : return error_mark_node;
9972 : : }
9973 : :
9974 : : /* Handle initializers that use braces. */
9975 : :
9976 : : /* Type of object we are accumulating a constructor for.
9977 : : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
9978 : : static tree constructor_type;
9979 : :
9980 : : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
9981 : : left to fill. */
9982 : : static tree constructor_fields;
9983 : :
9984 : : /* For an ARRAY_TYPE, this is the specified index
9985 : : at which to store the next element we get. */
9986 : : static tree constructor_index;
9987 : :
9988 : : /* For an ARRAY_TYPE, this is the maximum index. */
9989 : : static tree constructor_max_index;
9990 : :
9991 : : /* For a RECORD_TYPE, this is the first field not yet written out. */
9992 : : static tree constructor_unfilled_fields;
9993 : :
9994 : : /* For an ARRAY_TYPE, this is the index of the first element
9995 : : not yet written out. */
9996 : : static tree constructor_unfilled_index;
9997 : :
9998 : : /* In a RECORD_TYPE, the byte index of the next consecutive field.
9999 : : This is so we can generate gaps between fields, when appropriate. */
10000 : : static tree constructor_bit_index;
10001 : :
10002 : : /* If we are saving up the elements rather than allocating them,
10003 : : this is the list of elements so far (in reverse order,
10004 : : most recent first). */
10005 : : static vec<constructor_elt, va_gc> *constructor_elements;
10006 : :
10007 : : /* 1 if constructor should be incrementally stored into a constructor chain,
10008 : : 0 if all the elements should be kept in AVL tree. */
10009 : : static int constructor_incremental;
10010 : :
10011 : : /* 1 if so far this constructor's elements are all compile-time constants. */
10012 : : static int constructor_constant;
10013 : :
10014 : : /* 1 if so far this constructor's elements are all valid address constants. */
10015 : : static int constructor_simple;
10016 : :
10017 : : /* 1 if this constructor has an element that cannot be part of a
10018 : : constant expression. */
10019 : : static int constructor_nonconst;
10020 : :
10021 : : /* 1 if this constructor is erroneous so far. */
10022 : : static int constructor_erroneous;
10023 : :
10024 : : /* 1 if this constructor is the universal zero initializer { 0 }. */
10025 : : static int constructor_zeroinit;
10026 : :
10027 : : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10028 : : static bool constructor_zero_padding_bits;
10029 : :
10030 : : /* Structure for managing pending initializer elements, organized as an
10031 : : AVL tree. */
10032 : :
10033 : : struct init_node
10034 : : {
10035 : : struct init_node *left, *right;
10036 : : struct init_node *parent;
10037 : : int balance;
10038 : : tree purpose;
10039 : : tree value;
10040 : : tree origtype;
10041 : : };
10042 : :
10043 : : /* Tree of pending elements at this constructor level.
10044 : : These are elements encountered out of order
10045 : : which belong at places we haven't reached yet in actually
10046 : : writing the output.
10047 : : Will never hold tree nodes across GC runs. */
10048 : : static struct init_node *constructor_pending_elts;
10049 : :
10050 : : /* The SPELLING_DEPTH of this constructor. */
10051 : : static int constructor_depth;
10052 : :
10053 : : /* DECL node for which an initializer is being read.
10054 : : 0 means we are reading a constructor expression
10055 : : such as (struct foo) {...}. */
10056 : : static tree constructor_decl;
10057 : :
10058 : : /* Nonzero if there were any member designators in this initializer. */
10059 : : static int constructor_designated;
10060 : :
10061 : : /* Nesting depth of designator list. */
10062 : : static int designator_depth;
10063 : :
10064 : : /* Nonzero if there were diagnosed errors in this designator list. */
10065 : : static int designator_erroneous;
10066 : :
10067 : :
10068 : : /* This stack has a level for each implicit or explicit level of
10069 : : structuring in the initializer, including the outermost one. It
10070 : : saves the values of most of the variables above. */
10071 : :
10072 : : struct constructor_range_stack;
10073 : :
10074 : : struct constructor_stack
10075 : : {
10076 : : struct constructor_stack *next;
10077 : : tree type;
10078 : : tree fields;
10079 : : tree index;
10080 : : tree max_index;
10081 : : tree unfilled_index;
10082 : : tree unfilled_fields;
10083 : : tree bit_index;
10084 : : vec<constructor_elt, va_gc> *elements;
10085 : : struct init_node *pending_elts;
10086 : : int offset;
10087 : : int depth;
10088 : : /* If value nonzero, this value should replace the entire
10089 : : constructor at this level. */
10090 : : struct c_expr replacement_value;
10091 : : struct constructor_range_stack *range_stack;
10092 : : char constant;
10093 : : char simple;
10094 : : char nonconst;
10095 : : char implicit;
10096 : : char erroneous;
10097 : : char outer;
10098 : : char incremental;
10099 : : char designated;
10100 : : bool zero_padding_bits;
10101 : : int designator_depth;
10102 : : };
10103 : :
10104 : : static struct constructor_stack *constructor_stack;
10105 : :
10106 : : /* This stack represents designators from some range designator up to
10107 : : the last designator in the list. */
10108 : :
10109 : : struct constructor_range_stack
10110 : : {
10111 : : struct constructor_range_stack *next, *prev;
10112 : : struct constructor_stack *stack;
10113 : : tree range_start;
10114 : : tree index;
10115 : : tree range_end;
10116 : : tree fields;
10117 : : };
10118 : :
10119 : : static struct constructor_range_stack *constructor_range_stack;
10120 : :
10121 : : /* This stack records separate initializers that are nested.
10122 : : Nested initializers can't happen in ANSI C, but GNU C allows them
10123 : : in cases like { ... (struct foo) { ... } ... }. */
10124 : :
10125 : : struct initializer_stack
10126 : : {
10127 : : struct initializer_stack *next;
10128 : : tree decl;
10129 : : struct constructor_stack *constructor_stack;
10130 : : struct constructor_range_stack *constructor_range_stack;
10131 : : vec<constructor_elt, va_gc> *elements;
10132 : : struct spelling *spelling;
10133 : : struct spelling *spelling_base;
10134 : : int spelling_size;
10135 : : char require_constant_value;
10136 : : char require_constant_elements;
10137 : : char require_constexpr_value;
10138 : : char designated;
10139 : : rich_location *missing_brace_richloc;
10140 : : };
10141 : :
10142 : : static struct initializer_stack *initializer_stack;
10143 : :
10144 : : /* Prepare to parse and output the initializer for variable DECL. */
10145 : :
10146 : : void
10147 : 7095144 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10148 : : bool init_require_constant, bool init_require_constexpr,
10149 : : rich_location *richloc)
10150 : : {
10151 : 7095144 : const char *locus;
10152 : 7095144 : struct initializer_stack *p = XNEW (struct initializer_stack);
10153 : :
10154 : 7095144 : p->decl = constructor_decl;
10155 : 7095144 : p->require_constant_value = require_constant_value;
10156 : 7095144 : p->require_constant_elements = require_constant_elements;
10157 : 7095144 : p->require_constexpr_value = require_constexpr_value;
10158 : 7095144 : p->constructor_stack = constructor_stack;
10159 : 7095144 : p->constructor_range_stack = constructor_range_stack;
10160 : 7095144 : p->elements = constructor_elements;
10161 : 7095144 : p->spelling = spelling;
10162 : 7095144 : p->spelling_base = spelling_base;
10163 : 7095144 : p->spelling_size = spelling_size;
10164 : 7095144 : p->next = initializer_stack;
10165 : 7095144 : p->missing_brace_richloc = richloc;
10166 : 7095144 : p->designated = constructor_designated;
10167 : 7095144 : initializer_stack = p;
10168 : :
10169 : 7095144 : constructor_decl = decl;
10170 : 7095144 : constructor_designated = 0;
10171 : :
10172 : 7095144 : require_constant_value = init_require_constant;
10173 : 7095144 : require_constexpr_value = init_require_constexpr;
10174 : 7095144 : if (decl != NULL_TREE && decl != error_mark_node)
10175 : : {
10176 : 6195150 : require_constant_elements
10177 : 6018451 : = ((init_require_constant || (pedantic && !flag_isoc99))
10178 : : /* For a scalar, you can always use any value to initialize,
10179 : : even within braces. */
10180 : 6207104 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10181 : 6195150 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10182 : : }
10183 : : else
10184 : : {
10185 : 899994 : require_constant_elements = false;
10186 : 899994 : locus = _("(anonymous)");
10187 : : }
10188 : :
10189 : 7095144 : constructor_stack = 0;
10190 : 7095144 : constructor_range_stack = 0;
10191 : :
10192 : 7095144 : found_missing_braces = 0;
10193 : :
10194 : 7095144 : spelling_base = 0;
10195 : 7095144 : spelling_size = 0;
10196 : 7095144 : RESTORE_SPELLING_DEPTH (0);
10197 : :
10198 : 7095144 : if (locus)
10199 : 7095144 : push_string (locus);
10200 : 7095144 : }
10201 : :
10202 : : void
10203 : 7095142 : finish_init (void)
10204 : : {
10205 : 7095142 : struct initializer_stack *p = initializer_stack;
10206 : :
10207 : : /* Free the whole constructor stack of this initializer. */
10208 : 7095142 : while (constructor_stack)
10209 : : {
10210 : 0 : struct constructor_stack *q = constructor_stack;
10211 : 0 : constructor_stack = q->next;
10212 : 0 : XDELETE (q);
10213 : : }
10214 : :
10215 : 7095142 : gcc_assert (!constructor_range_stack);
10216 : :
10217 : : /* Pop back to the data of the outer initializer (if any). */
10218 : 7095142 : XDELETE (spelling_base);
10219 : :
10220 : 7095142 : constructor_decl = p->decl;
10221 : 7095142 : require_constant_value = p->require_constant_value;
10222 : 7095142 : require_constant_elements = p->require_constant_elements;
10223 : 7095142 : require_constexpr_value = p->require_constexpr_value;
10224 : 7095142 : constructor_stack = p->constructor_stack;
10225 : 7095142 : constructor_designated = p->designated;
10226 : 7095142 : constructor_range_stack = p->constructor_range_stack;
10227 : 7095142 : constructor_elements = p->elements;
10228 : 7095142 : spelling = p->spelling;
10229 : 7095142 : spelling_base = p->spelling_base;
10230 : 7095142 : spelling_size = p->spelling_size;
10231 : 7095142 : initializer_stack = p->next;
10232 : 7095142 : XDELETE (p);
10233 : 7095142 : }
10234 : :
10235 : : /* Call here when we see the initializer is surrounded by braces.
10236 : : This is instead of a call to push_init_level;
10237 : : it is matched by a call to pop_init_level.
10238 : :
10239 : : TYPE is the type to initialize, for a constructor expression.
10240 : : For an initializer for a decl, TYPE is zero. */
10241 : :
10242 : : void
10243 : 1002297 : really_start_incremental_init (tree type)
10244 : : {
10245 : 1002297 : struct constructor_stack *p = XNEW (struct constructor_stack);
10246 : :
10247 : 1002297 : if (type == NULL_TREE)
10248 : 104215 : type = TREE_TYPE (constructor_decl);
10249 : :
10250 : 1002297 : if (VECTOR_TYPE_P (type)
10251 : 1002297 : && TYPE_VECTOR_OPAQUE (type))
10252 : 0 : error ("opaque vector types cannot be initialized");
10253 : :
10254 : 1002297 : p->type = constructor_type;
10255 : 1002297 : p->fields = constructor_fields;
10256 : 1002297 : p->index = constructor_index;
10257 : 1002297 : p->max_index = constructor_max_index;
10258 : 1002297 : p->unfilled_index = constructor_unfilled_index;
10259 : 1002297 : p->unfilled_fields = constructor_unfilled_fields;
10260 : 1002297 : p->bit_index = constructor_bit_index;
10261 : 1002297 : p->elements = constructor_elements;
10262 : 1002297 : p->constant = constructor_constant;
10263 : 1002297 : p->simple = constructor_simple;
10264 : 1002297 : p->nonconst = constructor_nonconst;
10265 : 1002297 : p->erroneous = constructor_erroneous;
10266 : 1002297 : p->pending_elts = constructor_pending_elts;
10267 : 1002297 : p->depth = constructor_depth;
10268 : 1002297 : p->replacement_value.value = 0;
10269 : 1002297 : p->replacement_value.original_code = ERROR_MARK;
10270 : 1002297 : p->replacement_value.original_type = NULL;
10271 : 1002297 : p->implicit = 0;
10272 : 1002297 : p->range_stack = 0;
10273 : 1002297 : p->outer = 0;
10274 : 1002297 : p->incremental = constructor_incremental;
10275 : 1002297 : p->designated = constructor_designated;
10276 : 1002297 : p->zero_padding_bits = constructor_zero_padding_bits;
10277 : 1002297 : p->designator_depth = designator_depth;
10278 : 1002297 : p->next = 0;
10279 : 1002297 : constructor_stack = p;
10280 : :
10281 : 1002297 : constructor_constant = 1;
10282 : 1002297 : constructor_simple = 1;
10283 : 1002297 : constructor_nonconst = 0;
10284 : 1002297 : constructor_depth = SPELLING_DEPTH ();
10285 : 1002297 : constructor_elements = NULL;
10286 : 1002297 : constructor_pending_elts = 0;
10287 : 1002297 : constructor_type = type;
10288 : 1002297 : constructor_incremental = 1;
10289 : 1002297 : constructor_designated = 0;
10290 : 1002297 : constructor_zero_padding_bits = false;
10291 : 1002297 : constructor_zeroinit = 1;
10292 : 1002297 : designator_depth = 0;
10293 : 1002297 : designator_erroneous = 0;
10294 : :
10295 : 1002297 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10296 : : {
10297 : 80319 : constructor_fields = TYPE_FIELDS (constructor_type);
10298 : : /* Skip any nameless bit fields at the beginning. */
10299 : 80319 : while (constructor_fields != NULL_TREE
10300 : 80347 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10301 : 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10302 : :
10303 : 80319 : constructor_unfilled_fields = constructor_fields;
10304 : 80319 : constructor_bit_index = bitsize_zero_node;
10305 : : }
10306 : 921978 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10307 : : {
10308 : 17680 : if (TYPE_DOMAIN (constructor_type))
10309 : : {
10310 : 9334 : constructor_max_index
10311 : 9334 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10312 : :
10313 : : /* Detect non-empty initializations of zero-length arrays. */
10314 : 9334 : if (constructor_max_index == NULL_TREE
10315 : 9334 : && TYPE_SIZE (constructor_type))
10316 : 205 : constructor_max_index = integer_minus_one_node;
10317 : :
10318 : : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10319 : : to initialize VLAs with a nonempty initializer will cause a
10320 : : proper error; avoid tree checking errors as well by setting a
10321 : : safe value. */
10322 : 9334 : if (constructor_max_index
10323 : 9334 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10324 : 60 : constructor_max_index = integer_minus_one_node;
10325 : :
10326 : 9334 : constructor_index
10327 : 9334 : = convert (bitsizetype,
10328 : 9334 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10329 : : }
10330 : : else
10331 : : {
10332 : 8346 : constructor_index = bitsize_zero_node;
10333 : 8346 : constructor_max_index = NULL_TREE;
10334 : : }
10335 : :
10336 : 17680 : constructor_unfilled_index = constructor_index;
10337 : : }
10338 : 904298 : else if (gnu_vector_type_p (constructor_type))
10339 : : {
10340 : : /* Vectors are like simple fixed-size arrays. */
10341 : 1807524 : constructor_max_index =
10342 : 903762 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10343 : 903762 : constructor_index = bitsize_zero_node;
10344 : 903762 : constructor_unfilled_index = constructor_index;
10345 : : }
10346 : : else
10347 : : {
10348 : : /* Handle the case of int x = {5}; */
10349 : 536 : constructor_fields = constructor_type;
10350 : 536 : constructor_unfilled_fields = constructor_type;
10351 : : }
10352 : 1002297 : }
10353 : :
10354 : : extern location_t last_init_list_comma;
10355 : :
10356 : : /* Called when we see an open brace for a nested initializer. Finish
10357 : : off any pending levels with implicit braces. */
10358 : : void
10359 : 340318 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10360 : : {
10361 : 340320 : while (constructor_stack->implicit)
10362 : : {
10363 : 304 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10364 : 140 : && constructor_fields == NULL_TREE)
10365 : 0 : process_init_element (input_location,
10366 : : pop_init_level (loc, 1, braced_init_obstack,
10367 : : last_init_list_comma),
10368 : : true, braced_init_obstack);
10369 : 304 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10370 : 164 : && constructor_max_index
10371 : 468 : && tree_int_cst_lt (constructor_max_index,
10372 : : constructor_index))
10373 : 2 : process_init_element (input_location,
10374 : : pop_init_level (loc, 1, braced_init_obstack,
10375 : : last_init_list_comma),
10376 : : true, braced_init_obstack);
10377 : : else
10378 : : break;
10379 : : }
10380 : 340318 : }
10381 : :
10382 : : /* Push down into a subobject, for initialization.
10383 : : If this is for an explicit set of braces, IMPLICIT is 0.
10384 : : If it is because the next element belongs at a lower level,
10385 : : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10386 : :
10387 : : void
10388 : 1042941 : push_init_level (location_t loc, int implicit,
10389 : : struct obstack *braced_init_obstack)
10390 : : {
10391 : 1042941 : struct constructor_stack *p;
10392 : 1042941 : tree value = NULL_TREE;
10393 : :
10394 : : /* Unless this is an explicit brace, we need to preserve previous
10395 : : content if any. */
10396 : 1042941 : if (implicit)
10397 : : {
10398 : 703565 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10399 : 779 : value = find_init_member (constructor_fields, braced_init_obstack);
10400 : 702786 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10401 : 702786 : value = find_init_member (constructor_index, braced_init_obstack);
10402 : : }
10403 : :
10404 : 1042941 : p = XNEW (struct constructor_stack);
10405 : 1042941 : p->type = constructor_type;
10406 : 1042941 : p->fields = constructor_fields;
10407 : 1042941 : p->index = constructor_index;
10408 : 1042941 : p->max_index = constructor_max_index;
10409 : 1042941 : p->unfilled_index = constructor_unfilled_index;
10410 : 1042941 : p->unfilled_fields = constructor_unfilled_fields;
10411 : 1042941 : p->bit_index = constructor_bit_index;
10412 : 1042941 : p->elements = constructor_elements;
10413 : 1042941 : p->constant = constructor_constant;
10414 : 1042941 : p->simple = constructor_simple;
10415 : 1042941 : p->nonconst = constructor_nonconst;
10416 : 1042941 : p->erroneous = constructor_erroneous;
10417 : 1042941 : p->pending_elts = constructor_pending_elts;
10418 : 1042941 : p->depth = constructor_depth;
10419 : 1042941 : p->replacement_value.value = NULL_TREE;
10420 : 1042941 : p->replacement_value.original_code = ERROR_MARK;
10421 : 1042941 : p->replacement_value.original_type = NULL;
10422 : 1042941 : p->implicit = implicit;
10423 : 1042941 : p->outer = 0;
10424 : 1042941 : p->incremental = constructor_incremental;
10425 : 1042941 : p->designated = constructor_designated;
10426 : 1042941 : p->zero_padding_bits = constructor_zero_padding_bits;
10427 : 1042941 : p->designator_depth = designator_depth;
10428 : 1042941 : p->next = constructor_stack;
10429 : 1042941 : p->range_stack = 0;
10430 : 1042941 : constructor_stack = p;
10431 : :
10432 : 1042941 : constructor_constant = 1;
10433 : 1042941 : constructor_simple = 1;
10434 : 1042941 : constructor_nonconst = 0;
10435 : 1042941 : constructor_depth = SPELLING_DEPTH ();
10436 : 1042941 : constructor_elements = NULL;
10437 : 1042941 : constructor_incremental = 1;
10438 : : /* If the upper initializer is designated, then mark this as
10439 : : designated too to prevent bogus warnings. */
10440 : 1042941 : constructor_designated = p->designated;
10441 : : /* If the upper initializer has padding bits zeroed, that includes
10442 : : all nested initializers as well. */
10443 : 1042941 : constructor_zero_padding_bits = p->zero_padding_bits;
10444 : 1042941 : constructor_pending_elts = 0;
10445 : 1042941 : if (!implicit)
10446 : : {
10447 : 339376 : p->range_stack = constructor_range_stack;
10448 : 339376 : constructor_range_stack = 0;
10449 : 339376 : designator_depth = 0;
10450 : 339376 : designator_erroneous = 0;
10451 : : }
10452 : :
10453 : : /* Don't die if an entire brace-pair level is superfluous
10454 : : in the containing level. */
10455 : 1042941 : if (constructor_type == NULL_TREE)
10456 : : ;
10457 : 1042941 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10458 : : {
10459 : : /* Don't die if there are extra init elts at the end. */
10460 : 158775 : if (constructor_fields == NULL_TREE)
10461 : 51 : constructor_type = NULL_TREE;
10462 : : else
10463 : : {
10464 : 158724 : constructor_type = TREE_TYPE (constructor_fields);
10465 : 158724 : push_member_name (constructor_fields);
10466 : 158724 : constructor_depth++;
10467 : : }
10468 : : }
10469 : 884166 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10470 : : {
10471 : 884128 : constructor_type = TREE_TYPE (constructor_type);
10472 : 884128 : push_array_bounds (tree_to_uhwi (constructor_index));
10473 : 884128 : constructor_depth++;
10474 : : }
10475 : :
10476 : 1042941 : if (constructor_type == NULL_TREE)
10477 : : {
10478 : 51 : error_init (loc, "extra brace group at end of initializer");
10479 : 51 : constructor_fields = NULL_TREE;
10480 : 51 : constructor_unfilled_fields = NULL_TREE;
10481 : 51 : return;
10482 : : }
10483 : :
10484 : 1042890 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10485 : : {
10486 : 360 : constructor_constant = TREE_CONSTANT (value);
10487 : 360 : constructor_simple = TREE_STATIC (value);
10488 : 360 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10489 : 360 : constructor_elements = CONSTRUCTOR_ELTS (value);
10490 : 360 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10491 : 360 : if (!vec_safe_is_empty (constructor_elements)
10492 : 340 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10493 : 340 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10494 : 294 : set_nonincremental_init (braced_init_obstack);
10495 : : }
10496 : :
10497 : 1042890 : if (implicit == 1)
10498 : : {
10499 : 702623 : found_missing_braces = 1;
10500 : 702623 : if (initializer_stack->missing_brace_richloc)
10501 : 702623 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10502 : 702623 : (loc, "{");
10503 : : }
10504 : :
10505 : 1042890 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10506 : : {
10507 : 879624 : constructor_fields = TYPE_FIELDS (constructor_type);
10508 : : /* Skip any nameless bit fields at the beginning. */
10509 : 879624 : while (constructor_fields != NULL_TREE
10510 : 879688 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10511 : 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10512 : :
10513 : 879624 : constructor_unfilled_fields = constructor_fields;
10514 : 879624 : constructor_bit_index = bitsize_zero_node;
10515 : : }
10516 : 163266 : else if (gnu_vector_type_p (constructor_type))
10517 : : {
10518 : : /* Vectors are like simple fixed-size arrays. */
10519 : 9600 : constructor_max_index =
10520 : 4800 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10521 : 4800 : constructor_index = bitsize_int (0);
10522 : 4800 : constructor_unfilled_index = constructor_index;
10523 : : }
10524 : 158466 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10525 : : {
10526 : 158408 : if (TYPE_DOMAIN (constructor_type))
10527 : : {
10528 : 158408 : constructor_max_index
10529 : 158408 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10530 : :
10531 : : /* Detect non-empty initializations of zero-length arrays. */
10532 : 158408 : if (constructor_max_index == NULL_TREE
10533 : 158408 : && TYPE_SIZE (constructor_type))
10534 : 135 : constructor_max_index = integer_minus_one_node;
10535 : :
10536 : : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10537 : : to initialize VLAs will cause a proper error; avoid tree
10538 : : checking errors as well by setting a safe value. */
10539 : 158408 : if (constructor_max_index
10540 : 158139 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10541 : 0 : constructor_max_index = integer_minus_one_node;
10542 : :
10543 : 158408 : constructor_index
10544 : 158408 : = convert (bitsizetype,
10545 : 158408 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10546 : : }
10547 : : else
10548 : 0 : constructor_index = bitsize_zero_node;
10549 : :
10550 : 158408 : constructor_unfilled_index = constructor_index;
10551 : 158408 : if (value && TREE_CODE (value) == STRING_CST)
10552 : : {
10553 : : /* We need to split the char/wchar array into individual
10554 : : characters, so that we don't have to special case it
10555 : : everywhere. */
10556 : 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10557 : : }
10558 : : }
10559 : : else
10560 : : {
10561 : 58 : if (constructor_type != error_mark_node)
10562 : 29 : warning_init (input_location, 0, "braces around scalar initializer");
10563 : 58 : constructor_fields = constructor_type;
10564 : 58 : constructor_unfilled_fields = constructor_type;
10565 : : }
10566 : : }
10567 : :
10568 : : /* At the end of an implicit or explicit brace level,
10569 : : finish up that level of constructor. If a single expression
10570 : : with redundant braces initialized that level, return the
10571 : : c_expr structure for that expression. Otherwise, the original_code
10572 : : element is set to ERROR_MARK.
10573 : : If we were outputting the elements as they are read, return 0 as the value
10574 : : from inner levels (process_init_element ignores that),
10575 : : but return error_mark_node as the value from the outermost level
10576 : : (that's what we want to put in DECL_INITIAL).
10577 : : Otherwise, return a CONSTRUCTOR expression as the value. */
10578 : :
10579 : : struct c_expr
10580 : 2045238 : pop_init_level (location_t loc, int implicit,
10581 : : struct obstack *braced_init_obstack,
10582 : : location_t insert_before)
10583 : : {
10584 : 2045238 : struct constructor_stack *p;
10585 : 2045238 : struct c_expr ret;
10586 : 2045238 : ret.value = NULL_TREE;
10587 : 2045238 : ret.original_code = ERROR_MARK;
10588 : 2045238 : ret.original_type = NULL;
10589 : 2045238 : ret.m_decimal = 0;
10590 : :
10591 : 2045238 : if (implicit == 0)
10592 : : {
10593 : : /* When we come to an explicit close brace,
10594 : : pop any inner levels that didn't have explicit braces. */
10595 : 1342559 : while (constructor_stack->implicit)
10596 : 886 : process_init_element (input_location,
10597 : : pop_init_level (loc, 1, braced_init_obstack,
10598 : : insert_before),
10599 : : true, braced_init_obstack);
10600 : 1341673 : gcc_assert (!constructor_range_stack);
10601 : : }
10602 : : else
10603 : 703565 : if (initializer_stack->missing_brace_richloc)
10604 : 703565 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10605 : 703565 : (insert_before, "}");
10606 : :
10607 : : /* Now output all pending elements. */
10608 : 2045238 : constructor_incremental = 1;
10609 : 2045238 : output_pending_init_elements (1, braced_init_obstack);
10610 : :
10611 : 2045238 : p = constructor_stack;
10612 : :
10613 : : /* Error for initializing a flexible array member, or a zero-length
10614 : : array member in an inappropriate context. */
10615 : 2045187 : if (constructor_type && constructor_fields
10616 : 159247 : && TREE_CODE (constructor_type) == ARRAY_TYPE
10617 : 152548 : && TYPE_DOMAIN (constructor_type)
10618 : 2197778 : && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
10619 : : {
10620 : : /* Silently discard empty initializations. The parser will
10621 : : already have pedwarned for empty brackets for C17 and earlier. */
10622 : 299 : if (integer_zerop (constructor_unfilled_index))
10623 : 84 : constructor_type = NULL_TREE;
10624 : 299 : if (constructor_type || flag_isoc23)
10625 : : {
10626 : 295 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10627 : :
10628 : 295 : if (constructor_depth <= 2)
10629 : 248 : pedwarn_init (loc, OPT_Wpedantic,
10630 : : "initialization of a flexible array member");
10631 : 47 : else if (constructor_type)
10632 : 33 : error_init (loc, "initialization of flexible array member "
10633 : : "in a nested context");
10634 : : else
10635 : 14 : pedwarn_init (loc, OPT_Wpedantic,
10636 : : "initialization of flexible array member "
10637 : : "in a nested context");
10638 : :
10639 : : /* We have already issued an error message for the existence
10640 : : of a flexible array member not at the end of the structure.
10641 : : Discard the initializer so that we do not die later. */
10642 : 295 : if (constructor_type
10643 : 215 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10644 : 301 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10645 : 0 : constructor_type = NULL_TREE;
10646 : : }
10647 : : }
10648 : :
10649 : 2045238 : switch (vec_safe_length (constructor_elements))
10650 : : {
10651 : 4617 : case 0:
10652 : : /* Initialization with { } counts as zeroinit. */
10653 : 4617 : constructor_zeroinit = 1;
10654 : 4617 : break;
10655 : 913910 : case 1:
10656 : : /* This might be zeroinit as well. */
10657 : 913910 : if (integer_zerop ((*constructor_elements)[0].value))
10658 : 1864 : constructor_zeroinit = 1;
10659 : : break;
10660 : 1126711 : default:
10661 : : /* If the constructor has more than one element, it can't be { 0 }. */
10662 : 1126711 : constructor_zeroinit = 0;
10663 : 1126711 : break;
10664 : : }
10665 : :
10666 : : /* Warn when some structs are initialized with direct aggregation. */
10667 : 2045238 : if (!implicit && found_missing_braces && warn_missing_braces
10668 : 37 : && !constructor_zeroinit)
10669 : : {
10670 : 18 : gcc_assert (initializer_stack->missing_brace_richloc);
10671 : 18 : warning_at (initializer_stack->missing_brace_richloc,
10672 : 18 : OPT_Wmissing_braces,
10673 : : "missing braces around initializer");
10674 : : }
10675 : :
10676 : : /* Warn when some struct elements are implicitly initialized to zero. */
10677 : 2045238 : if (warn_missing_field_initializers
10678 : 425443 : && constructor_type
10679 : 425415 : && TREE_CODE (constructor_type) == RECORD_TYPE
10680 : 193395 : && constructor_unfilled_fields)
10681 : : {
10682 : : /* Do not warn for flexible array members or zero-length arrays. */
10683 : 43 : while (constructor_unfilled_fields
10684 : 43 : && (!DECL_SIZE (constructor_unfilled_fields)
10685 : 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
10686 : 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
10687 : :
10688 : 43 : if (constructor_unfilled_fields
10689 : : /* Do not warn if this level of the initializer uses member
10690 : : designators; it is likely to be deliberate. */
10691 : 43 : && !constructor_designated
10692 : : /* Do not warn about initializing with { 0 } or with { }. */
10693 : 24 : && !constructor_zeroinit)
10694 : : {
10695 : 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
10696 : : "missing initializer for field %qD of %qT",
10697 : : constructor_unfilled_fields,
10698 : : constructor_type))
10699 : 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
10700 : : "%qD declared here", constructor_unfilled_fields);
10701 : : }
10702 : : }
10703 : :
10704 : : /* Pad out the end of the structure. */
10705 : 2045238 : if (p->replacement_value.value)
10706 : : /* If this closes a superfluous brace pair,
10707 : : just pass out the element between them. */
10708 : 102 : ret = p->replacement_value;
10709 : 2045136 : else if (constructor_type == NULL_TREE)
10710 : : ;
10711 : 2045009 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
10712 : 2045009 : && TREE_CODE (constructor_type) != ARRAY_TYPE
10713 : 2045009 : && !gnu_vector_type_p (constructor_type))
10714 : : {
10715 : : /* A nonincremental scalar initializer--just return
10716 : : the element, after verifying there is just one.
10717 : : Empty scalar initializers are supported in C23. */
10718 : 594 : if (vec_safe_is_empty (constructor_elements))
10719 : : {
10720 : 207 : if (constructor_erroneous || constructor_type == error_mark_node)
10721 : 94 : ret.value = error_mark_node;
10722 : 113 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
10723 : : {
10724 : 2 : error_init (loc, "invalid initializer");
10725 : 2 : ret.value = error_mark_node;
10726 : : }
10727 : 111 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
10728 : : /* Ensure this is a null pointer constant in the case of a
10729 : : 'constexpr' object initialized with {}. */
10730 : 33 : ret.value = build_zero_cst (ptr_type_node);
10731 : : else
10732 : 78 : ret.value = build_zero_cst (constructor_type);
10733 : : }
10734 : 387 : else if (vec_safe_length (constructor_elements) != 1)
10735 : : {
10736 : 0 : error_init (loc, "extra elements in scalar initializer");
10737 : 0 : ret.value = (*constructor_elements)[0].value;
10738 : : }
10739 : : else
10740 : 387 : ret.value = (*constructor_elements)[0].value;
10741 : : }
10742 : : else
10743 : : {
10744 : 2044415 : if (constructor_erroneous)
10745 : 505 : ret.value = error_mark_node;
10746 : : else
10747 : : {
10748 : 2043910 : ret.value = build_constructor (constructor_type,
10749 : : constructor_elements);
10750 : 2043910 : if (constructor_constant)
10751 : 1722466 : TREE_CONSTANT (ret.value) = 1;
10752 : 2043910 : if (constructor_constant && constructor_simple)
10753 : 1722427 : TREE_STATIC (ret.value) = 1;
10754 : 2043910 : if (constructor_nonconst)
10755 : 641 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
10756 : 2043910 : if (constructor_zero_padding_bits)
10757 : 20 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
10758 : : }
10759 : : }
10760 : :
10761 : 2045238 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
10762 : : {
10763 : 1201 : if (constructor_nonconst)
10764 : 15 : ret.original_code = C_MAYBE_CONST_EXPR;
10765 : 1186 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
10766 : 0 : ret.original_code = ERROR_MARK;
10767 : : }
10768 : :
10769 : 2045238 : constructor_type = p->type;
10770 : 2045238 : constructor_fields = p->fields;
10771 : 2045238 : constructor_index = p->index;
10772 : 2045238 : constructor_max_index = p->max_index;
10773 : 2045238 : constructor_unfilled_index = p->unfilled_index;
10774 : 2045238 : constructor_unfilled_fields = p->unfilled_fields;
10775 : 2045238 : constructor_bit_index = p->bit_index;
10776 : 2045238 : constructor_elements = p->elements;
10777 : 2045238 : constructor_constant = p->constant;
10778 : 2045238 : constructor_simple = p->simple;
10779 : 2045238 : constructor_nonconst = p->nonconst;
10780 : 2045238 : constructor_erroneous = p->erroneous;
10781 : 2045238 : constructor_incremental = p->incremental;
10782 : 2045238 : constructor_designated = p->designated;
10783 : 2045238 : constructor_zero_padding_bits = p->zero_padding_bits;
10784 : 2045238 : designator_depth = p->designator_depth;
10785 : 2045238 : constructor_pending_elts = p->pending_elts;
10786 : 2045238 : constructor_depth = p->depth;
10787 : 2045238 : if (!p->implicit)
10788 : 1341673 : constructor_range_stack = p->range_stack;
10789 : 2045238 : RESTORE_SPELLING_DEPTH (constructor_depth);
10790 : :
10791 : 2045238 : constructor_stack = p->next;
10792 : 2045238 : XDELETE (p);
10793 : :
10794 : 2045238 : if (ret.value == NULL_TREE && constructor_stack == 0)
10795 : 0 : ret.value = error_mark_node;
10796 : 2045238 : return ret;
10797 : : }
10798 : :
10799 : : /* Common handling for both array range and field name designators.
10800 : : ARRAY argument is nonzero for array ranges. Returns false for success. */
10801 : :
10802 : : static bool
10803 : 33100 : set_designator (location_t loc, bool array,
10804 : : struct obstack *braced_init_obstack)
10805 : : {
10806 : 33100 : tree subtype;
10807 : 33100 : enum tree_code subcode;
10808 : :
10809 : : /* Don't die if an entire brace-pair level is superfluous
10810 : : in the containing level, or for an erroneous type. */
10811 : 33100 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10812 : : return true;
10813 : :
10814 : : /* If there were errors in this designator list already, bail out
10815 : : silently. */
10816 : 33091 : if (designator_erroneous)
10817 : : return true;
10818 : :
10819 : : /* Likewise for an initializer for a variable-size type. Those are
10820 : : diagnosed in the parser, except for empty initializer braces. */
10821 : 33091 : if (COMPLETE_TYPE_P (constructor_type)
10822 : 33091 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
10823 : : return true;
10824 : :
10825 : 33081 : if (!designator_depth)
10826 : : {
10827 : 32463 : gcc_assert (!constructor_range_stack);
10828 : :
10829 : : /* Designator list starts at the level of closest explicit
10830 : : braces. */
10831 : 32846 : while (constructor_stack->implicit)
10832 : 383 : process_init_element (input_location,
10833 : : pop_init_level (loc, 1, braced_init_obstack,
10834 : : last_init_list_comma),
10835 : : true, braced_init_obstack);
10836 : 32463 : constructor_designated = 1;
10837 : 32463 : return false;
10838 : : }
10839 : :
10840 : 618 : switch (TREE_CODE (constructor_type))
10841 : : {
10842 : 421 : case RECORD_TYPE:
10843 : 421 : case UNION_TYPE:
10844 : 421 : subtype = TREE_TYPE (constructor_fields);
10845 : 421 : if (subtype != error_mark_node)
10846 : 421 : subtype = TYPE_MAIN_VARIANT (subtype);
10847 : : break;
10848 : 197 : case ARRAY_TYPE:
10849 : 197 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10850 : 197 : break;
10851 : 0 : default:
10852 : 0 : gcc_unreachable ();
10853 : : }
10854 : :
10855 : 618 : subcode = TREE_CODE (subtype);
10856 : 618 : if (array && subcode != ARRAY_TYPE)
10857 : : {
10858 : 1 : error_init (loc, "array index in non-array initializer");
10859 : 1 : return true;
10860 : : }
10861 : 617 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
10862 : : {
10863 : 0 : error_init (loc, "field name not in record or union initializer");
10864 : 0 : return true;
10865 : : }
10866 : :
10867 : 617 : constructor_designated = 1;
10868 : 617 : finish_implicit_inits (loc, braced_init_obstack);
10869 : 617 : push_init_level (loc, 2, braced_init_obstack);
10870 : 617 : return false;
10871 : : }
10872 : :
10873 : : /* If there are range designators in designator list, push a new designator
10874 : : to constructor_range_stack. RANGE_END is end of such stack range or
10875 : : NULL_TREE if there is no range designator at this level. */
10876 : :
10877 : : static void
10878 : 384 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
10879 : : {
10880 : 384 : struct constructor_range_stack *p;
10881 : :
10882 : 768 : p = (struct constructor_range_stack *)
10883 : 384 : obstack_alloc (braced_init_obstack,
10884 : : sizeof (struct constructor_range_stack));
10885 : 384 : p->prev = constructor_range_stack;
10886 : 384 : p->next = 0;
10887 : 384 : p->fields = constructor_fields;
10888 : 384 : p->range_start = constructor_index;
10889 : 384 : p->index = constructor_index;
10890 : 384 : p->stack = constructor_stack;
10891 : 384 : p->range_end = range_end;
10892 : 384 : if (constructor_range_stack)
10893 : 19 : constructor_range_stack->next = p;
10894 : 384 : constructor_range_stack = p;
10895 : 384 : }
10896 : :
10897 : : /* Within an array initializer, specify the next index to be initialized.
10898 : : FIRST is that index. If LAST is nonzero, then initialize a range
10899 : : of indices, running from FIRST through LAST. */
10900 : :
10901 : : void
10902 : 1143 : set_init_index (location_t loc, tree first, tree last,
10903 : : struct obstack *braced_init_obstack)
10904 : : {
10905 : 1143 : if (set_designator (loc, true, braced_init_obstack))
10906 : : return;
10907 : :
10908 : 1140 : designator_erroneous = 1;
10909 : :
10910 : 2280 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
10911 : 2269 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
10912 : : {
10913 : 12 : error_init (loc, "array index in initializer not of integer type");
10914 : 12 : return;
10915 : : }
10916 : :
10917 : 1128 : if (TREE_CODE (first) != INTEGER_CST)
10918 : : {
10919 : 6 : first = c_fully_fold (first, false, NULL);
10920 : 6 : if (TREE_CODE (first) == INTEGER_CST)
10921 : 5 : pedwarn_init (loc, OPT_Wpedantic,
10922 : : "array index in initializer is not "
10923 : : "an integer constant expression");
10924 : : }
10925 : :
10926 : 1128 : if (last && TREE_CODE (last) != INTEGER_CST)
10927 : : {
10928 : 2 : last = c_fully_fold (last, false, NULL);
10929 : 2 : if (TREE_CODE (last) == INTEGER_CST)
10930 : 1 : pedwarn_init (loc, OPT_Wpedantic,
10931 : : "array index in initializer is not "
10932 : : "an integer constant expression");
10933 : : }
10934 : :
10935 : 1128 : if (TREE_CODE (first) != INTEGER_CST)
10936 : 1 : error_init (loc, "nonconstant array index in initializer");
10937 : 1127 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
10938 : 1 : error_init (loc, "nonconstant array index in initializer");
10939 : 1126 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
10940 : 9 : error_init (loc, "array index in non-array initializer");
10941 : 1117 : else if (tree_int_cst_sgn (first) == -1)
10942 : 15 : error_init (loc, "array index in initializer exceeds array bounds");
10943 : 1102 : else if (constructor_max_index
10944 : 1102 : && tree_int_cst_lt (constructor_max_index, first))
10945 : 7 : error_init (loc, "array index in initializer exceeds array bounds");
10946 : : else
10947 : : {
10948 : 1095 : constant_expression_warning (first);
10949 : 1095 : if (last)
10950 : 379 : constant_expression_warning (last);
10951 : 1095 : constructor_index = convert (bitsizetype, first);
10952 : 1095 : if (tree_int_cst_lt (constructor_index, first))
10953 : : {
10954 : 0 : constructor_index = copy_node (constructor_index);
10955 : 0 : TREE_OVERFLOW (constructor_index) = 1;
10956 : : }
10957 : :
10958 : 1095 : if (last)
10959 : : {
10960 : 379 : if (tree_int_cst_equal (first, last))
10961 : : last = NULL_TREE;
10962 : 378 : else if (tree_int_cst_lt (last, first))
10963 : : {
10964 : 2 : error_init (loc, "empty index range in initializer");
10965 : 2 : last = NULL_TREE;
10966 : : }
10967 : : else
10968 : : {
10969 : 376 : last = convert (bitsizetype, last);
10970 : 376 : if (constructor_max_index != NULL_TREE
10971 : 376 : && tree_int_cst_lt (constructor_max_index, last))
10972 : : {
10973 : 3 : error_init (loc, "array index range in initializer exceeds "
10974 : : "array bounds");
10975 : 3 : last = NULL_TREE;
10976 : : }
10977 : : }
10978 : : }
10979 : :
10980 : 1095 : designator_depth++;
10981 : 1095 : designator_erroneous = 0;
10982 : 1095 : if (constructor_range_stack || last)
10983 : 373 : push_range_stack (last, braced_init_obstack);
10984 : : }
10985 : : }
10986 : :
10987 : : /* Within a struct initializer, specify the next field to be initialized. */
10988 : :
10989 : : void
10990 : 31912 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
10991 : : struct obstack *braced_init_obstack)
10992 : : {
10993 : 31912 : tree field;
10994 : :
10995 : 31912 : if (set_designator (loc, false, braced_init_obstack))
10996 : : return;
10997 : :
10998 : 31895 : designator_erroneous = 1;
10999 : :
11000 : 31895 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11001 : : {
11002 : 3 : error_init (loc, "field name not in record or union initializer");
11003 : 3 : return;
11004 : : }
11005 : :
11006 : 31892 : field = lookup_field (constructor_type, fieldname);
11007 : :
11008 : 31892 : if (field == NULL_TREE)
11009 : : {
11010 : 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11011 : 8 : if (guessed_id)
11012 : : {
11013 : 4 : gcc_rich_location rich_loc (fieldname_loc);
11014 : 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11015 : 4 : error_at (&rich_loc,
11016 : : "%qT has no member named %qE; did you mean %qE?",
11017 : : constructor_type, fieldname, guessed_id);
11018 : 4 : }
11019 : : else
11020 : 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11021 : : constructor_type, fieldname);
11022 : : }
11023 : : else
11024 : 31929 : do
11025 : : {
11026 : 31929 : constructor_fields = TREE_VALUE (field);
11027 : 31929 : designator_depth++;
11028 : 31929 : designator_erroneous = 0;
11029 : 31929 : if (constructor_range_stack)
11030 : 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11031 : 31929 : field = TREE_CHAIN (field);
11032 : 31929 : if (field)
11033 : : {
11034 : 45 : if (set_designator (loc, false, braced_init_obstack))
11035 : : return;
11036 : : }
11037 : : }
11038 : 31929 : while (field != NULL_TREE);
11039 : : }
11040 : :
11041 : : /* Helper function for add_pending_init. Find inorder successor of P
11042 : : in AVL tree. */
11043 : : static struct init_node *
11044 : 129 : init_node_successor (struct init_node *p)
11045 : : {
11046 : 129 : struct init_node *r;
11047 : 129 : if (p->right)
11048 : : {
11049 : : r = p->right;
11050 : 58 : while (r->left)
11051 : : r = r->left;
11052 : : return r;
11053 : : }
11054 : 75 : r = p->parent;
11055 : 114 : while (r && p == r->right)
11056 : : {
11057 : 39 : p = r;
11058 : 39 : r = r->parent;
11059 : : }
11060 : : return r;
11061 : : }
11062 : :
11063 : : /* Add a new initializer to the tree of pending initializers. PURPOSE
11064 : : identifies the initializer, either array index or field in a structure.
11065 : : VALUE is the value of that index or field. If ORIGTYPE is not
11066 : : NULL_TREE, it is the original type of VALUE.
11067 : :
11068 : : IMPLICIT is true if value comes from pop_init_level (1),
11069 : : the new initializer has been merged with the existing one
11070 : : and thus no warnings should be emitted about overriding an
11071 : : existing initializer. */
11072 : :
11073 : : static void
11074 : 3261 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11075 : : bool implicit, struct obstack *braced_init_obstack)
11076 : : {
11077 : 3261 : struct init_node *p, **q, *r;
11078 : :
11079 : 3261 : q = &constructor_pending_elts;
11080 : 3261 : p = 0;
11081 : :
11082 : 3261 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11083 : : {
11084 : 3610 : while (*q != 0)
11085 : : {
11086 : 2412 : p = *q;
11087 : 2412 : if (tree_int_cst_lt (purpose, p->purpose))
11088 : 283 : q = &p->left;
11089 : 2129 : else if (tree_int_cst_lt (p->purpose, purpose))
11090 : : {
11091 : 1960 : if (TREE_CODE (p->value) != RAW_DATA_CST
11092 : 1960 : || (p->right
11093 : 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11094 : 1830 : q = &p->right;
11095 : : else
11096 : : {
11097 : 130 : widest_int pp = wi::to_widest (p->purpose);
11098 : 130 : widest_int pw = wi::to_widest (purpose);
11099 : 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11100 : 98 : q = &p->right;
11101 : : else
11102 : : {
11103 : : /* Override which should split the old RAW_DATA_CST
11104 : : into 2 or 3 pieces. */
11105 : 32 : if (!implicit && warn_override_init)
11106 : 9 : warning_init (loc, OPT_Woverride_init,
11107 : : "initialized field overwritten");
11108 : 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11109 : 32 : unsigned HOST_WIDE_INT len = 1;
11110 : 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11111 : 0 : len = RAW_DATA_LENGTH (value);
11112 : 32 : unsigned HOST_WIDE_INT end = 0;
11113 : 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11114 : 32 : gcc_checking_assert (start < plen && start);
11115 : 32 : if (plen - start > len)
11116 : 30 : end = plen - start - len;
11117 : 32 : tree v = p->value;
11118 : 32 : tree origtype = p->origtype;
11119 : 32 : if (start == 1)
11120 : 2 : p->value = build_int_cst (TREE_TYPE (v),
11121 : 2 : RAW_DATA_UCHAR_ELT (v, 0));
11122 : : else
11123 : : {
11124 : 30 : p->value = v;
11125 : 30 : if (end > 1)
11126 : 26 : v = copy_node (v);
11127 : 30 : RAW_DATA_LENGTH (p->value) = start;
11128 : : }
11129 : 32 : if (end)
11130 : : {
11131 : 30 : tree epurpose
11132 : 30 : = size_binop (PLUS_EXPR, purpose,
11133 : : bitsize_int (len));
11134 : 30 : if (end > 1)
11135 : : {
11136 : 28 : RAW_DATA_LENGTH (v) -= plen - end;
11137 : 28 : RAW_DATA_POINTER (v) += plen - end;
11138 : : }
11139 : : else
11140 : 2 : v = build_int_cst (TREE_TYPE (v),
11141 : 2 : RAW_DATA_UCHAR_ELT (v, plen
11142 : : - end));
11143 : 30 : add_pending_init (loc, epurpose, v, origtype,
11144 : : implicit, braced_init_obstack);
11145 : : }
11146 : 32 : q = &constructor_pending_elts;
11147 : 32 : continue;
11148 : 32 : }
11149 : 130 : }
11150 : : }
11151 : : else
11152 : : {
11153 : 169 : if (TREE_CODE (p->value) == RAW_DATA_CST
11154 : 179 : && (RAW_DATA_LENGTH (p->value)
11155 : 10 : > (TREE_CODE (value) == RAW_DATA_CST
11156 : 10 : ? RAW_DATA_LENGTH (value) : 1)))
11157 : : {
11158 : : /* Override which should split the old RAW_DATA_CST
11159 : : into 2 pieces. */
11160 : 6 : if (!implicit && warn_override_init)
11161 : 3 : warning_init (loc, OPT_Woverride_init,
11162 : : "initialized field overwritten");
11163 : 6 : unsigned HOST_WIDE_INT len = 1;
11164 : 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11165 : 2 : len = RAW_DATA_LENGTH (value);
11166 : 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11167 : : {
11168 : 6 : RAW_DATA_LENGTH (p->value) -= len;
11169 : 6 : RAW_DATA_POINTER (p->value) += len;
11170 : : }
11171 : : else
11172 : : {
11173 : 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11174 : 0 : p->value
11175 : 0 : = build_int_cst (TREE_TYPE (p->value),
11176 : 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11177 : : }
11178 : 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11179 : : bitsize_int (len));
11180 : 6 : continue;
11181 : 6 : }
11182 : 163 : if (TREE_CODE (value) == RAW_DATA_CST)
11183 : : {
11184 : 8 : handle_raw_data:
11185 : : /* RAW_DATA_CST value might overlap various further
11186 : : prior initval entries. Find out how many. */
11187 : 13 : unsigned cnt = 0;
11188 : 13 : widest_int w
11189 : 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11190 : 13 : struct init_node *r = p, *last = NULL;
11191 : 13 : bool override_init = warn_override_init;
11192 : 13 : while ((r = init_node_successor (r))
11193 : 53 : && wi::to_widest (r->purpose) < w)
11194 : : {
11195 : 40 : ++cnt;
11196 : 40 : if (TREE_SIDE_EFFECTS (r->value))
11197 : 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11198 : : "initialized field with side-effects "
11199 : : "overwritten");
11200 : 38 : else if (override_init)
11201 : : {
11202 : 6 : warning_init (loc, OPT_Woverride_init,
11203 : : "initialized field overwritten");
11204 : 6 : override_init = false;
11205 : : }
11206 : : last = r;
11207 : : }
11208 : 13 : if (cnt)
11209 : : {
11210 : 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11211 : 13 : && (wi::to_widest (last->purpose)
11212 : 13 : + RAW_DATA_LENGTH (last->value) > w))
11213 : : {
11214 : : /* The last overlapping prior initval overlaps
11215 : : only partially. Shrink it and decrease cnt. */
11216 : 0 : unsigned int l = (wi::to_widest (last->purpose)
11217 : 0 : + RAW_DATA_LENGTH (last->value)
11218 : 0 : - w).to_uhwi ();
11219 : 0 : --cnt;
11220 : 0 : RAW_DATA_LENGTH (last->value) -= l;
11221 : 0 : RAW_DATA_POINTER (last->value) += l;
11222 : 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11223 : 0 : last->value
11224 : 0 : = build_int_cst (TREE_TYPE (last->value),
11225 : 0 : RAW_DATA_UCHAR_ELT (last->value,
11226 : : 0));
11227 : 0 : last->purpose
11228 : 0 : = size_binop (PLUS_EXPR, last->purpose,
11229 : : bitsize_int (l));
11230 : : }
11231 : : /* Instead of deleting cnt nodes from the AVL tree
11232 : : and rebalancing, peel of last cnt bytes from the
11233 : : RAW_DATA_CST. Overriding thousands of previously
11234 : : initialized array elements with #embed needs to work,
11235 : : but doesn't need to be super efficient. */
11236 : 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11237 : : > cnt);
11238 : 13 : RAW_DATA_LENGTH (value) -= cnt;
11239 : 13 : const unsigned char *s
11240 : 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11241 : 13 : unsigned int o = RAW_DATA_LENGTH (value);
11242 : 53 : for (r = p; cnt--; ++o, ++s)
11243 : : {
11244 : 40 : r = init_node_successor (r);
11245 : 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11246 : : bitsize_int (o));
11247 : 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11248 : 40 : r->origtype = origtype;
11249 : : }
11250 : 13 : if (RAW_DATA_LENGTH (value) == 1)
11251 : 0 : value = build_int_cst (TREE_TYPE (value),
11252 : 0 : RAW_DATA_UCHAR_ELT (value, 0));
11253 : : }
11254 : : }
11255 : 168 : if (!implicit)
11256 : : {
11257 : 54 : if (TREE_SIDE_EFFECTS (p->value))
11258 : 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11259 : : "initialized field with side-effects "
11260 : : "overwritten");
11261 : 48 : else if (warn_override_init)
11262 : 13 : warning_init (loc, OPT_Woverride_init,
11263 : : "initialized field overwritten");
11264 : : }
11265 : 168 : p->value = value;
11266 : 168 : p->origtype = origtype;
11267 : 168 : return;
11268 : : }
11269 : : }
11270 : 1198 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11271 : : {
11272 : 61 : struct init_node *r;
11273 : 61 : if (q == &p->left)
11274 : : r = p;
11275 : : else
11276 : 36 : r = init_node_successor (p);
11277 : 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11278 : 131 : + RAW_DATA_LENGTH (value)))
11279 : : {
11280 : : /* Overlap with at least one prior initval in the range but
11281 : : not at the start. */
11282 : 5 : p = r;
11283 : 5 : p->purpose = purpose;
11284 : 5 : goto handle_raw_data;
11285 : : }
11286 : : }
11287 : : }
11288 : : else
11289 : : {
11290 : 1900 : tree bitpos;
11291 : :
11292 : 1900 : bitpos = bit_position (purpose);
11293 : 5386 : while (*q != NULL)
11294 : : {
11295 : 1843 : p = *q;
11296 : 1843 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11297 : 43 : q = &p->left;
11298 : 1800 : else if (p->purpose != purpose)
11299 : 1543 : q = &p->right;
11300 : : else
11301 : : {
11302 : 257 : if (!implicit)
11303 : : {
11304 : 33 : if (TREE_SIDE_EFFECTS (p->value))
11305 : 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11306 : : "initialized field with side-effects "
11307 : : "overwritten");
11308 : 29 : else if (warn_override_init)
11309 : 6 : warning_init (loc, OPT_Woverride_init,
11310 : : "initialized field overwritten");
11311 : : }
11312 : 257 : p->value = value;
11313 : 257 : p->origtype = origtype;
11314 : 257 : return;
11315 : : }
11316 : : }
11317 : : }
11318 : :
11319 : 2836 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11320 : : sizeof (struct init_node));
11321 : 2836 : r->purpose = purpose;
11322 : 2836 : r->value = value;
11323 : 2836 : r->origtype = origtype;
11324 : :
11325 : 2836 : *q = r;
11326 : 2836 : r->parent = p;
11327 : 2836 : r->left = 0;
11328 : 2836 : r->right = 0;
11329 : 2836 : r->balance = 0;
11330 : :
11331 : 4854 : while (p)
11332 : : {
11333 : 2806 : struct init_node *s;
11334 : :
11335 : 2806 : if (r == p->left)
11336 : : {
11337 : 237 : if (p->balance == 0)
11338 : 174 : p->balance = -1;
11339 : 63 : else if (p->balance < 0)
11340 : : {
11341 : 45 : if (r->balance < 0)
11342 : : {
11343 : : /* L rotation. */
11344 : 40 : p->left = r->right;
11345 : 40 : if (p->left)
11346 : 2 : p->left->parent = p;
11347 : 40 : r->right = p;
11348 : :
11349 : 40 : p->balance = 0;
11350 : 40 : r->balance = 0;
11351 : :
11352 : 40 : s = p->parent;
11353 : 40 : p->parent = r;
11354 : 40 : r->parent = s;
11355 : 40 : if (s)
11356 : : {
11357 : 31 : if (s->left == p)
11358 : 10 : s->left = r;
11359 : : else
11360 : 21 : s->right = r;
11361 : : }
11362 : : else
11363 : 9 : constructor_pending_elts = r;
11364 : : }
11365 : : else
11366 : : {
11367 : : /* LR rotation. */
11368 : 5 : struct init_node *t = r->right;
11369 : :
11370 : 5 : r->right = t->left;
11371 : 5 : if (r->right)
11372 : 0 : r->right->parent = r;
11373 : 5 : t->left = r;
11374 : :
11375 : 5 : p->left = t->right;
11376 : 5 : if (p->left)
11377 : 0 : p->left->parent = p;
11378 : 5 : t->right = p;
11379 : :
11380 : 5 : p->balance = t->balance < 0;
11381 : 5 : r->balance = -(t->balance > 0);
11382 : 5 : t->balance = 0;
11383 : :
11384 : 5 : s = p->parent;
11385 : 5 : p->parent = t;
11386 : 5 : r->parent = t;
11387 : 5 : t->parent = s;
11388 : 5 : if (s)
11389 : : {
11390 : 0 : if (s->left == p)
11391 : 0 : s->left = t;
11392 : : else
11393 : 0 : s->right = t;
11394 : : }
11395 : : else
11396 : 5 : constructor_pending_elts = t;
11397 : : }
11398 : : break;
11399 : : }
11400 : : else
11401 : : {
11402 : : /* p->balance == +1; growth of left side balances the node. */
11403 : 18 : p->balance = 0;
11404 : 18 : break;
11405 : : }
11406 : : }
11407 : : else /* r == p->right */
11408 : : {
11409 : 2569 : if (p->balance == 0)
11410 : : /* Growth propagation from right side. */
11411 : 1844 : p->balance++;
11412 : 725 : else if (p->balance > 0)
11413 : : {
11414 : 696 : if (r->balance > 0)
11415 : : {
11416 : : /* R rotation. */
11417 : 694 : p->right = r->left;
11418 : 694 : if (p->right)
11419 : 173 : p->right->parent = p;
11420 : 694 : r->left = p;
11421 : :
11422 : 694 : p->balance = 0;
11423 : 694 : r->balance = 0;
11424 : :
11425 : 694 : s = p->parent;
11426 : 694 : p->parent = r;
11427 : 694 : r->parent = s;
11428 : 694 : if (s)
11429 : : {
11430 : 374 : if (s->left == p)
11431 : 2 : s->left = r;
11432 : : else
11433 : 372 : s->right = r;
11434 : : }
11435 : : else
11436 : 320 : constructor_pending_elts = r;
11437 : : }
11438 : : else /* r->balance == -1 */
11439 : : {
11440 : : /* RL rotation */
11441 : 2 : struct init_node *t = r->left;
11442 : :
11443 : 2 : r->left = t->right;
11444 : 2 : if (r->left)
11445 : 2 : r->left->parent = r;
11446 : 2 : t->right = r;
11447 : :
11448 : 2 : p->right = t->left;
11449 : 2 : if (p->right)
11450 : 2 : p->right->parent = p;
11451 : 2 : t->left = p;
11452 : :
11453 : 2 : r->balance = (t->balance < 0);
11454 : 2 : p->balance = -(t->balance > 0);
11455 : 2 : t->balance = 0;
11456 : :
11457 : 2 : s = p->parent;
11458 : 2 : p->parent = t;
11459 : 2 : r->parent = t;
11460 : 2 : t->parent = s;
11461 : 2 : if (s)
11462 : : {
11463 : 0 : if (s->left == p)
11464 : 0 : s->left = t;
11465 : : else
11466 : 0 : s->right = t;
11467 : : }
11468 : : else
11469 : 2 : constructor_pending_elts = t;
11470 : : }
11471 : : break;
11472 : : }
11473 : : else
11474 : : {
11475 : : /* p->balance == -1; growth of right side balances the node. */
11476 : 29 : p->balance = 0;
11477 : 29 : break;
11478 : : }
11479 : : }
11480 : :
11481 : 2018 : r = p;
11482 : 2018 : p = p->parent;
11483 : : }
11484 : : }
11485 : :
11486 : : /* Build AVL tree from a sorted chain. */
11487 : :
11488 : : static void
11489 : 418 : set_nonincremental_init (struct obstack * braced_init_obstack)
11490 : : {
11491 : 418 : unsigned HOST_WIDE_INT ix;
11492 : 418 : tree index, value;
11493 : :
11494 : 418 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11495 : 418 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11496 : : return;
11497 : :
11498 : 1601 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11499 : 1183 : add_pending_init (input_location, index, value, NULL_TREE, true,
11500 : : braced_init_obstack);
11501 : 418 : constructor_elements = NULL;
11502 : 418 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11503 : : {
11504 : 200 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11505 : : /* Skip any nameless bit fields at the beginning. */
11506 : 200 : while (constructor_unfilled_fields != NULL_TREE
11507 : 200 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11508 : 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11509 : :
11510 : : }
11511 : 218 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11512 : : {
11513 : 218 : if (TYPE_DOMAIN (constructor_type))
11514 : 160 : constructor_unfilled_index
11515 : 160 : = convert (bitsizetype,
11516 : 160 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11517 : : else
11518 : 58 : constructor_unfilled_index = bitsize_zero_node;
11519 : : }
11520 : 418 : constructor_incremental = 0;
11521 : : }
11522 : :
11523 : : /* Build AVL tree from a string constant. */
11524 : :
11525 : : static void
11526 : 7 : set_nonincremental_init_from_string (tree str,
11527 : : struct obstack * braced_init_obstack)
11528 : : {
11529 : 7 : tree value, purpose, type;
11530 : 7 : HOST_WIDE_INT val[2];
11531 : 7 : const char *p, *end;
11532 : 7 : int byte, wchar_bytes, charwidth, bitpos;
11533 : :
11534 : 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11535 : :
11536 : 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11537 : 7 : charwidth = TYPE_PRECISION (char_type_node);
11538 : 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11539 : : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11540 : 7 : type = TREE_TYPE (constructor_type);
11541 : 7 : p = TREE_STRING_POINTER (str);
11542 : 7 : end = p + TREE_STRING_LENGTH (str);
11543 : :
11544 : 7 : for (purpose = bitsize_zero_node;
11545 : : p < end
11546 : 52 : && !(constructor_max_index
11547 : 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11548 : 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11549 : : {
11550 : 25 : if (wchar_bytes == 1)
11551 : : {
11552 : 9 : val[0] = (unsigned char) *p++;
11553 : 9 : val[1] = 0;
11554 : : }
11555 : : else
11556 : : {
11557 : 16 : val[1] = 0;
11558 : 16 : val[0] = 0;
11559 : 64 : for (byte = 0; byte < wchar_bytes; byte++)
11560 : : {
11561 : 48 : if (BYTES_BIG_ENDIAN)
11562 : : bitpos = (wchar_bytes - byte - 1) * charwidth;
11563 : : else
11564 : 48 : bitpos = byte * charwidth;
11565 : 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11566 : 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11567 : 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11568 : : }
11569 : : }
11570 : :
11571 : 25 : if (!TYPE_UNSIGNED (type))
11572 : : {
11573 : 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11574 : 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11575 : : {
11576 : 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11577 : : {
11578 : 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11579 : 0 : val[1] = -1;
11580 : : }
11581 : : }
11582 : 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11583 : : {
11584 : 0 : if (val[0] < 0)
11585 : 0 : val[1] = -1;
11586 : : }
11587 : 0 : else if (val[1] & (HOST_WIDE_INT_1
11588 : 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11589 : 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11590 : : }
11591 : :
11592 : 25 : value = wide_int_to_tree (type,
11593 : 25 : wide_int::from_array (val, 2,
11594 : : HOST_BITS_PER_WIDE_INT * 2));
11595 : 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11596 : : braced_init_obstack);
11597 : : }
11598 : :
11599 : 7 : constructor_incremental = 0;
11600 : 7 : }
11601 : :
11602 : : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11603 : : not initialized yet. */
11604 : :
11605 : : static tree
11606 : 703565 : find_init_member (tree field, struct obstack * braced_init_obstack)
11607 : : {
11608 : 703565 : struct init_node *p;
11609 : :
11610 : 703565 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11611 : : {
11612 : 702786 : if (constructor_incremental
11613 : 702786 : && tree_int_cst_lt (field, constructor_unfilled_index))
11614 : 16 : set_nonincremental_init (braced_init_obstack);
11615 : :
11616 : 702786 : p = constructor_pending_elts;
11617 : 702909 : while (p)
11618 : : {
11619 : 233 : if (tree_int_cst_lt (field, p->purpose))
11620 : 25 : p = p->left;
11621 : 208 : else if (tree_int_cst_lt (p->purpose, field))
11622 : 98 : p = p->right;
11623 : : else
11624 : 110 : return p->value;
11625 : : }
11626 : : }
11627 : 779 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11628 : : {
11629 : 660 : tree bitpos = bit_position (field);
11630 : :
11631 : 660 : if (constructor_incremental
11632 : 660 : && (!constructor_unfilled_fields
11633 : 457 : || tree_int_cst_lt (bitpos,
11634 : 457 : bit_position (constructor_unfilled_fields))))
11635 : 51 : set_nonincremental_init (braced_init_obstack);
11636 : :
11637 : 660 : p = constructor_pending_elts;
11638 : 800 : while (p)
11639 : : {
11640 : 347 : if (field == p->purpose)
11641 : 207 : return p->value;
11642 : 140 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11643 : 7 : p = p->left;
11644 : : else
11645 : 133 : p = p->right;
11646 : : }
11647 : : }
11648 : 119 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11649 : : {
11650 : 119 : if (!vec_safe_is_empty (constructor_elements)
11651 : 50 : && (constructor_elements->last ().index == field))
11652 : 50 : return constructor_elements->last ().value;
11653 : : }
11654 : : return NULL_TREE;
11655 : : }
11656 : :
11657 : : /* "Output" the next constructor element.
11658 : : At top level, really output it to assembler code now.
11659 : : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11660 : : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11661 : : TYPE is the data type that the containing data type wants here.
11662 : : FIELD is the field (a FIELD_DECL) or the index that this element fills.
11663 : : If VALUE is a string constant, STRICT_STRING is true if it is
11664 : : unparenthesized or we should not warn here for it being parenthesized.
11665 : : For other types of VALUE, STRICT_STRING is not used.
11666 : :
11667 : : PENDING if true means output pending elements that belong
11668 : : right after this element. (PENDING is normally true;
11669 : : it is false while outputting pending elements, to avoid recursion.)
11670 : :
11671 : : IMPLICIT is true if value comes from pop_init_level (1),
11672 : : the new initializer has been merged with the existing one
11673 : : and thus no warnings should be emitted about overriding an
11674 : : existing initializer. */
11675 : :
11676 : : static void
11677 : 9463066 : output_init_element (location_t loc, tree value, tree origtype,
11678 : : bool strict_string, tree type, tree field, bool pending,
11679 : : bool implicit, struct obstack * braced_init_obstack)
11680 : : {
11681 : 9463066 : tree semantic_type = NULL_TREE;
11682 : 9463066 : bool maybe_const = true;
11683 : 9463066 : bool npc, int_const_expr, arith_const_expr;
11684 : :
11685 : 9463066 : if (type == error_mark_node || value == error_mark_node)
11686 : : {
11687 : 477 : constructor_erroneous = 1;
11688 : 2594 : return;
11689 : : }
11690 : 9462589 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
11691 : 199893 : && (TREE_CODE (value) == STRING_CST
11692 : 158502 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
11693 : 41399 : && !(TREE_CODE (value) == STRING_CST
11694 : 41391 : && TREE_CODE (type) == ARRAY_TYPE
11695 : 3896 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
11696 : 9500092 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
11697 : 37503 : TYPE_MAIN_VARIANT (type)))
11698 : 37503 : value = array_to_pointer_conversion (input_location, value);
11699 : :
11700 : 9462589 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
11701 : 178 : && require_constant_value && pending)
11702 : : {
11703 : : /* As an extension, allow initializing objects with static storage
11704 : : duration with compound literals (which are then treated just as
11705 : : the brace enclosed list they contain). */
11706 : 114 : if (flag_isoc99)
11707 : 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
11708 : : "constant");
11709 : 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
11710 : 114 : value = DECL_INITIAL (decl);
11711 : : }
11712 : :
11713 : 9462589 : npc = null_pointer_constant_p (value);
11714 : 18925178 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
11715 : 4144721 : && !TREE_OVERFLOW (value)
11716 : 13607309 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
11717 : : /* Not fully determined before folding. */
11718 : 9462589 : arith_const_expr = true;
11719 : 9462589 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
11720 : : {
11721 : 16 : semantic_type = TREE_TYPE (value);
11722 : 16 : value = TREE_OPERAND (value, 0);
11723 : : }
11724 : 9462589 : value = c_fully_fold (value, require_constant_value, &maybe_const);
11725 : : /* TODO: this may not detect all cases of expressions folding to
11726 : : constants that are not arithmetic constant expressions. */
11727 : 9462589 : if (!maybe_const)
11728 : : arith_const_expr = false;
11729 : 18922697 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
11730 : 3153109 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
11731 : 11302115 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
11732 : : arith_const_expr = false;
11733 : 7635543 : else if (TREE_CODE (value) != INTEGER_CST
11734 : : && TREE_CODE (value) != REAL_CST
11735 : : && TREE_CODE (value) != COMPLEX_CST
11736 : : && TREE_CODE (value) != RAW_DATA_CST)
11737 : : arith_const_expr = false;
11738 : 4525855 : else if (TREE_OVERFLOW (value))
11739 : 4936735 : arith_const_expr = false;
11740 : :
11741 : 9462589 : if (value == error_mark_node)
11742 : 0 : constructor_erroneous = 1;
11743 : 9462589 : else if (!TREE_CONSTANT (value))
11744 : 3129320 : constructor_constant = 0;
11745 : 6333269 : else if (!initializer_constant_valid_p (value,
11746 : 6333269 : TREE_TYPE (value),
11747 : 6333269 : AGGREGATE_TYPE_P (constructor_type)
11748 : 6333269 : && TYPE_REVERSE_STORAGE_ORDER
11749 : : (constructor_type))
11750 : 6333269 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
11751 : 1037336 : && DECL_C_BIT_FIELD (field)
11752 : 8103 : && TREE_CODE (value) != INTEGER_CST))
11753 : 77 : constructor_simple = 0;
11754 : 9462589 : if (!maybe_const)
11755 : 1137 : constructor_nonconst = 1;
11756 : :
11757 : : /* Digest the initializer and issue any errors about incompatible
11758 : : types before issuing errors about non-constant initializers. */
11759 : 9462589 : tree new_value = value;
11760 : 9462589 : if (semantic_type)
11761 : 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
11762 : : /* In the case of braces around a scalar initializer, the result of
11763 : : this initializer processing goes through digest_init again at the
11764 : : outer level. In the case of a constexpr initializer for a
11765 : : pointer, avoid converting a null pointer constant to something
11766 : : that is not a null pointer constant to avoid a spurious error
11767 : : from that second processing. */
11768 : 9462589 : if (!require_constexpr_value
11769 : 397 : || !npc
11770 : 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
11771 : 9462578 : new_value = digest_init (loc,
11772 : 9462578 : RECORD_OR_UNION_TYPE_P (constructor_type)
11773 : 8394227 : ? field : constructor_fields
11774 : 8394227 : ? constructor_fields : constructor_decl,
11775 : : type, new_value, origtype, npc,
11776 : : int_const_expr, arith_const_expr, strict_string,
11777 : : require_constant_value, require_constexpr_value);
11778 : 9462589 : if (new_value == error_mark_node)
11779 : : {
11780 : 76 : constructor_erroneous = 1;
11781 : 76 : return;
11782 : : }
11783 : 9462513 : if (require_constant_value || require_constant_elements)
11784 : 2711450 : constant_expression_warning (new_value);
11785 : :
11786 : : /* Proceed to check the constness of the original initializer. */
11787 : 9462513 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
11788 : : {
11789 : 3129346 : if (require_constant_value)
11790 : : {
11791 : 0 : error_init (loc, "initializer element is not constant");
11792 : 0 : value = error_mark_node;
11793 : : }
11794 : 3129346 : else if (require_constant_elements)
11795 : 15 : pedwarn (loc, OPT_Wpedantic,
11796 : : "initializer element is not computable at load time");
11797 : : }
11798 : 6333167 : else if (!maybe_const
11799 : 54 : && (require_constant_value || require_constant_elements))
11800 : 28 : pedwarn_init (loc, OPT_Wpedantic,
11801 : : "initializer element is not a constant expression");
11802 : : /* digest_init has already carried out the additional checks
11803 : : required for 'constexpr' initializers (using the information
11804 : : passed to it about whether the original initializer was certain
11805 : : kinds of constant expression), so that check does not need to be
11806 : : repeated here. */
11807 : :
11808 : : /* Issue -Wc++-compat warnings about initializing a bitfield with
11809 : : enum type. */
11810 : 9462513 : if (warn_cxx_compat
11811 : 6895 : && field != NULL_TREE
11812 : 6895 : && TREE_CODE (field) == FIELD_DECL
11813 : 459 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
11814 : 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
11815 : 21 : != TYPE_MAIN_VARIANT (type))
11816 : 9462534 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
11817 : : {
11818 : 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
11819 : 21 : if (checktype != error_mark_node
11820 : 21 : && (TYPE_MAIN_VARIANT (checktype)
11821 : 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
11822 : 9 : warning_init (loc, OPT_Wc___compat,
11823 : : "enum conversion in initialization is invalid in C++");
11824 : : }
11825 : :
11826 : : /* If this field is empty and does not have side effects (and is not at
11827 : : the end of structure), don't do anything other than checking the
11828 : : initializer. */
11829 : 9462513 : if (field
11830 : 9462513 : && (TREE_TYPE (field) == error_mark_node
11831 : 9462126 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
11832 : 9461818 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
11833 : 62 : && !TREE_SIDE_EFFECTS (new_value)
11834 : 33 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
11835 : 33 : || DECL_CHAIN (field)))))
11836 : 18 : return;
11837 : :
11838 : : /* Finally, set VALUE to the initializer value digested above. */
11839 : 9462495 : value = new_value;
11840 : :
11841 : : /* If this element doesn't come next in sequence,
11842 : : put it on constructor_pending_elts. */
11843 : 9462495 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
11844 : 9462495 : && (!constructor_incremental
11845 : 1714823 : || !tree_int_cst_equal (field, constructor_unfilled_index)
11846 : 1714457 : || (TREE_CODE (value) == RAW_DATA_CST
11847 : 235 : && constructor_pending_elts
11848 : 66 : && pending)))
11849 : : {
11850 : 627 : if (constructor_incremental
11851 : 627 : && (tree_int_cst_lt (field, constructor_unfilled_index)
11852 : 337 : || (TREE_CODE (value) == RAW_DATA_CST
11853 : 14 : && constructor_pending_elts
11854 : 14 : && pending)))
11855 : 48 : set_nonincremental_init (braced_init_obstack);
11856 : :
11857 : 627 : add_pending_init (loc, field, value, origtype, implicit,
11858 : : braced_init_obstack);
11859 : 627 : return;
11860 : : }
11861 : 9461868 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
11862 : 1038118 : && (!constructor_incremental
11863 : 1037778 : || field != constructor_unfilled_fields))
11864 : : {
11865 : : /* We do this for records but not for unions. In a union,
11866 : : no matter which field is specified, it can be initialized
11867 : : right away since it starts at the beginning of the union. */
11868 : 1396 : if (constructor_incremental)
11869 : : {
11870 : 1056 : if (!constructor_unfilled_fields)
11871 : 5 : set_nonincremental_init (braced_init_obstack);
11872 : : else
11873 : : {
11874 : 1051 : tree bitpos, unfillpos;
11875 : :
11876 : 1051 : bitpos = bit_position (field);
11877 : 1051 : unfillpos = bit_position (constructor_unfilled_fields);
11878 : :
11879 : 1051 : if (tree_int_cst_lt (bitpos, unfillpos))
11880 : 4 : set_nonincremental_init (braced_init_obstack);
11881 : : }
11882 : : }
11883 : :
11884 : 1396 : add_pending_init (loc, field, value, origtype, implicit,
11885 : : braced_init_obstack);
11886 : 1396 : return;
11887 : : }
11888 : 9460472 : else if (TREE_CODE (constructor_type) == UNION_TYPE
11889 : 9460472 : && !vec_safe_is_empty (constructor_elements))
11890 : : {
11891 : 62 : if (!implicit)
11892 : : {
11893 : 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
11894 : 3 : warning_init (loc, OPT_Woverride_init_side_effects,
11895 : : "initialized field with side-effects overwritten");
11896 : 9 : else if (warn_override_init)
11897 : 6 : warning_init (loc, OPT_Woverride_init,
11898 : : "initialized field overwritten");
11899 : : }
11900 : :
11901 : : /* We can have just one union field set. */
11902 : 62 : constructor_elements = NULL;
11903 : : }
11904 : :
11905 : : /* Otherwise, output this element either to
11906 : : constructor_elements or to the assembler file. */
11907 : :
11908 : 9460472 : constructor_elt celt = {field, value};
11909 : 9460472 : vec_safe_push (constructor_elements, celt);
11910 : :
11911 : : /* Advance the variable that indicates sequential elements output. */
11912 : 9460472 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11913 : : {
11914 : 1714452 : tree inc = bitsize_one_node;
11915 : 1714452 : if (value && TREE_CODE (value) == RAW_DATA_CST)
11916 : 230 : inc = bitsize_int (RAW_DATA_LENGTH (value));
11917 : 1714452 : constructor_unfilled_index
11918 : 1714452 : = size_binop_loc (input_location, PLUS_EXPR,
11919 : : constructor_unfilled_index, inc);
11920 : : }
11921 : 7746020 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11922 : : {
11923 : 1036722 : constructor_unfilled_fields
11924 : 1036722 : = DECL_CHAIN (constructor_unfilled_fields);
11925 : :
11926 : : /* Skip any nameless bit fields. */
11927 : 1036722 : while (constructor_unfilled_fields != NULL_TREE
11928 : 1036823 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11929 : 101 : constructor_unfilled_fields
11930 : 101 : = DECL_CHAIN (constructor_unfilled_fields);
11931 : : }
11932 : 6709298 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11933 : 30201 : constructor_unfilled_fields = NULL_TREE;
11934 : :
11935 : : /* Now output any pending elements which have become next. */
11936 : 9460472 : if (pending)
11937 : 9457615 : output_pending_init_elements (0, braced_init_obstack);
11938 : : }
11939 : :
11940 : : /* For two FIELD_DECLs in the same chain, return -1 if field1
11941 : : comes before field2, 1 if field1 comes after field2 and
11942 : : 0 if field1 == field2. */
11943 : :
11944 : : static int
11945 : 4001 : init_field_decl_cmp (tree field1, tree field2)
11946 : : {
11947 : 4001 : if (field1 == field2)
11948 : : return 0;
11949 : :
11950 : 2138 : tree bitpos1 = bit_position (field1);
11951 : 2138 : tree bitpos2 = bit_position (field2);
11952 : 2138 : if (tree_int_cst_equal (bitpos1, bitpos2))
11953 : : {
11954 : : /* If one of the fields has non-zero bitsize, then that
11955 : : field must be the last one in a sequence of zero
11956 : : sized fields, fields after it will have bigger
11957 : : bit_position. */
11958 : 22 : if (TREE_TYPE (field1) != error_mark_node
11959 : 22 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
11960 : 44 : && integer_nonzerop (TREE_TYPE (field1)))
11961 : : return 1;
11962 : 22 : if (TREE_TYPE (field2) != error_mark_node
11963 : 22 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
11964 : 37 : && integer_nonzerop (TREE_TYPE (field2)))
11965 : : return -1;
11966 : : /* Otherwise, fallback to DECL_CHAIN walk to find out
11967 : : which field comes earlier. Walk chains of both
11968 : : fields, so that if field1 and field2 are close to each
11969 : : other in either order, it is found soon even for large
11970 : : sequences of zero sized fields. */
11971 : : tree f1 = field1, f2 = field2;
11972 : 22 : while (1)
11973 : : {
11974 : 22 : f1 = DECL_CHAIN (f1);
11975 : 22 : f2 = DECL_CHAIN (f2);
11976 : 22 : if (f1 == NULL_TREE)
11977 : : {
11978 : 7 : gcc_assert (f2);
11979 : : return 1;
11980 : : }
11981 : 15 : if (f2 == NULL_TREE)
11982 : : return -1;
11983 : 1 : if (f1 == field2)
11984 : : return -1;
11985 : 0 : if (f2 == field1)
11986 : : return 1;
11987 : 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
11988 : : return 1;
11989 : 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
11990 : : return -1;
11991 : : }
11992 : : }
11993 : 2116 : else if (tree_int_cst_lt (bitpos1, bitpos2))
11994 : : return -1;
11995 : : else
11996 : : return 1;
11997 : : }
11998 : :
11999 : : /* Output any pending elements which have become next.
12000 : : As we output elements, constructor_unfilled_{fields,index}
12001 : : advances, which may cause other elements to become next;
12002 : : if so, they too are output.
12003 : :
12004 : : If ALL is 0, we return when there are
12005 : : no more pending elements to output now.
12006 : :
12007 : : If ALL is 1, we output space as necessary so that
12008 : : we can output all the pending elements. */
12009 : : static void
12010 : 11502853 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12011 : : {
12012 : 11502853 : struct init_node *elt = constructor_pending_elts;
12013 : 11503849 : tree next;
12014 : :
12015 : 11503849 : retry:
12016 : :
12017 : : /* Look through the whole pending tree.
12018 : : If we find an element that should be output now,
12019 : : output it. Otherwise, set NEXT to the element
12020 : : that comes first among those still pending. */
12021 : :
12022 : 11503849 : next = NULL_TREE;
12023 : 11509479 : while (elt)
12024 : : {
12025 : 7574 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12026 : : {
12027 : 3004 : if (tree_int_cst_equal (elt->purpose,
12028 : : constructor_unfilled_index))
12029 : 1197 : output_init_element (input_location, elt->value, elt->origtype,
12030 : 1197 : true, TREE_TYPE (constructor_type),
12031 : : constructor_unfilled_index, false, false,
12032 : : braced_init_obstack);
12033 : 1807 : else if (tree_int_cst_lt (constructor_unfilled_index,
12034 : 1807 : elt->purpose))
12035 : : {
12036 : : /* Advance to the next smaller node. */
12037 : 589 : if (elt->left)
12038 : : elt = elt->left;
12039 : : else
12040 : : {
12041 : : /* We have reached the smallest node bigger than the
12042 : : current unfilled index. Fill the space first. */
12043 : 209 : next = elt->purpose;
12044 : 209 : break;
12045 : : }
12046 : : }
12047 : : else
12048 : : {
12049 : : /* Advance to the next bigger node. */
12050 : 1218 : if (elt->right)
12051 : : elt = elt->right;
12052 : : else
12053 : : {
12054 : : /* We have reached the biggest node in a subtree. Find
12055 : : the parent of it, which is the next bigger node. */
12056 : 1218 : while (elt->parent && elt->parent->right == elt)
12057 : : elt = elt->parent;
12058 : 774 : elt = elt->parent;
12059 : 1149 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12060 : 375 : elt->purpose))
12061 : : {
12062 : 22 : next = elt->purpose;
12063 : 22 : break;
12064 : : }
12065 : : }
12066 : : }
12067 : : }
12068 : 4570 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12069 : : {
12070 : : /* If the current record is complete we are done. */
12071 : 4570 : if (constructor_unfilled_fields == NULL_TREE)
12072 : : break;
12073 : :
12074 : 3731 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12075 : : elt->purpose);
12076 : 3731 : if (cmp == 0)
12077 : 1660 : output_init_element (input_location, elt->value, elt->origtype,
12078 : 1660 : true, TREE_TYPE (elt->purpose),
12079 : : elt->purpose, false, false,
12080 : : braced_init_obstack);
12081 : 2071 : else if (cmp < 0)
12082 : : {
12083 : : /* Advance to the next smaller node. */
12084 : 1084 : if (elt->left)
12085 : : elt = elt->left;
12086 : : else
12087 : : {
12088 : : /* We have reached the smallest node bigger than the
12089 : : current unfilled field. Fill the space first. */
12090 : 807 : next = elt->purpose;
12091 : 807 : break;
12092 : : }
12093 : : }
12094 : : else
12095 : : {
12096 : : /* Advance to the next bigger node. */
12097 : 987 : if (elt->right)
12098 : : elt = elt->right;
12099 : : else
12100 : : {
12101 : : /* We have reached the biggest node in a subtree. Find
12102 : : the parent of it, which is the next bigger node. */
12103 : 731 : while (elt->parent && elt->parent->right == elt)
12104 : : elt = elt->parent;
12105 : 515 : elt = elt->parent;
12106 : 515 : if (elt
12107 : 515 : && init_field_decl_cmp (constructor_unfilled_fields,
12108 : : elt->purpose) < 0)
12109 : : {
12110 : 67 : next = elt->purpose;
12111 : 67 : break;
12112 : : }
12113 : : }
12114 : : }
12115 : : }
12116 : : }
12117 : :
12118 : : /* Ordinarily return, but not if we want to output all
12119 : : and there are elements left. */
12120 : 11503849 : if (!(all && next != NULL_TREE))
12121 : 11502853 : return;
12122 : :
12123 : : /* If it's not incremental, just skip over the gap, so that after
12124 : : jumping to retry we will output the next successive element. */
12125 : 996 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12126 : 804 : constructor_unfilled_fields = next;
12127 : 192 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12128 : 192 : constructor_unfilled_index = next;
12129 : :
12130 : : /* ELT now points to the node in the pending tree with the next
12131 : : initializer to output. */
12132 : 996 : goto retry;
12133 : : }
12134 : :
12135 : : /* Expression VALUE coincides with the start of type TYPE in a braced
12136 : : initializer. Return true if we should treat VALUE as initializing
12137 : : the first element of TYPE, false if we should treat it as initializing
12138 : : TYPE as a whole.
12139 : :
12140 : : If the initializer is clearly invalid, the question becomes:
12141 : : which choice gives the best error message? */
12142 : :
12143 : : static bool
12144 : 3479979 : initialize_elementwise_p (tree type, tree value)
12145 : : {
12146 : 3479979 : if (type == error_mark_node || value == error_mark_node)
12147 : : return false;
12148 : :
12149 : 3479523 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12150 : :
12151 : 3479523 : tree value_type = TREE_TYPE (value);
12152 : 3479523 : if (value_type == error_mark_node)
12153 : : return false;
12154 : :
12155 : : /* GNU vectors can be initialized elementwise. However, treat any
12156 : : kind of vector value as initializing the vector type as a whole,
12157 : : regardless of whether the value is a GNU vector. Such initializers
12158 : : are valid if and only if they would have been valid in a non-braced
12159 : : initializer like:
12160 : :
12161 : : TYPE foo = VALUE;
12162 : :
12163 : : so recursing into the vector type would be at best confusing or at
12164 : : worst wrong. For example, when -flax-vector-conversions is in effect,
12165 : : it's possible to initialize a V8HI from a V4SI, even though the vectors
12166 : : have different element types and different numbers of elements. */
12167 : 3479523 : if (gnu_vector_type_p (type))
12168 : 19172 : return !VECTOR_TYPE_P (value_type);
12169 : :
12170 : 3460351 : if (AGGREGATE_TYPE_P (type))
12171 : 1740996 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12172 : :
12173 : : return false;
12174 : : }
12175 : :
12176 : : /* Helper function for process_init_element. Split first element of
12177 : : RAW_DATA_CST and save the rest to *RAW_DATA. */
12178 : :
12179 : : static inline tree
12180 : 9460089 : maybe_split_raw_data (tree value, tree *raw_data)
12181 : : {
12182 : 9460089 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12183 : : return value;
12184 : 211 : *raw_data = value;
12185 : 211 : value = build_int_cst (integer_type_node,
12186 : 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12187 : 211 : ++RAW_DATA_POINTER (*raw_data);
12188 : 211 : --RAW_DATA_LENGTH (*raw_data);
12189 : 211 : return value;
12190 : : }
12191 : :
12192 : : /* Return non-zero if c_parser_initval should attempt to optimize
12193 : : large initializers into RAW_DATA_CST. In that case return how
12194 : : many elements to optimize at most. */
12195 : :
12196 : : unsigned
12197 : 2932212 : c_maybe_optimize_large_byte_initializer (void)
12198 : : {
12199 : 2932212 : if (!constructor_type
12200 : 2932142 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12201 : 207314 : || constructor_stack->implicit)
12202 : : return 0;
12203 : 204579 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12204 : 204579 : if (TREE_CODE (elttype) != INTEGER_TYPE
12205 : 204579 : && TREE_CODE (elttype) != BITINT_TYPE)
12206 : : return 0;
12207 : 182184 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12208 : 27874 : || constructor_stack->replacement_value.value
12209 : 27874 : || (COMPLETE_TYPE_P (constructor_type)
12210 : 26237 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12211 : 210058 : || constructor_range_stack)
12212 : : return 0;
12213 : 27874 : if (constructor_max_index == NULL_TREE)
12214 : : return INT_MAX;
12215 : 26237 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12216 : 26237 : || integer_all_onesp (constructor_max_index))
12217 : 4758 : return 0;
12218 : 21479 : widest_int w = wi::to_widest (constructor_max_index);
12219 : 21479 : w -= wi::to_widest (constructor_index);
12220 : 21479 : w += 1;
12221 : 21479 : if (w < 64)
12222 : : return 0;
12223 : 6890 : if (w > INT_MAX)
12224 : : return INT_MAX;
12225 : 6890 : return w.to_uhwi ();
12226 : 21479 : }
12227 : :
12228 : : /* Add one non-braced element to the current constructor level.
12229 : : This adjusts the current position within the constructor's type.
12230 : : This may also start or terminate implicit levels
12231 : : to handle a partly-braced initializer.
12232 : :
12233 : : Once this has found the correct level for the new element,
12234 : : it calls output_init_element.
12235 : :
12236 : : IMPLICIT is true if value comes from pop_init_level (1),
12237 : : the new initializer has been merged with the existing one
12238 : : and thus no warnings should be emitted about overriding an
12239 : : existing initializer. */
12240 : :
12241 : : void
12242 : 9451110 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12243 : : struct obstack * braced_init_obstack)
12244 : : {
12245 : 9451110 : tree orig_value = value.value;
12246 : 18902220 : int string_flag
12247 : 9451110 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12248 : 9451110 : bool strict_string = value.original_code == STRING_CST;
12249 : 9451110 : bool was_designated = designator_depth != 0;
12250 : 9451110 : tree raw_data = NULL_TREE;
12251 : :
12252 : 9451322 : retry:
12253 : 9451322 : designator_depth = 0;
12254 : 9451322 : designator_erroneous = 0;
12255 : :
12256 : 9451322 : if (!implicit && value.value && !integer_zerop (value.value))
12257 : 7451855 : constructor_zeroinit = 0;
12258 : :
12259 : : /* Handle superfluous braces around string cst as in
12260 : : char x[] = {"foo"}; */
12261 : 9451322 : if (constructor_type
12262 : 9451194 : && !was_designated
12263 : 9418169 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12264 : 1703026 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12265 : 10185705 : && integer_zerop (constructor_unfilled_index))
12266 : : {
12267 : 162902 : if (constructor_stack->replacement_value.value)
12268 : : {
12269 : 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12270 : 376 : return;
12271 : : }
12272 : 162894 : else if (string_flag)
12273 : : {
12274 : 102 : constructor_stack->replacement_value = value;
12275 : 102 : return;
12276 : : }
12277 : : }
12278 : :
12279 : 9451212 : if (constructor_stack->replacement_value.value != NULL_TREE)
12280 : : {
12281 : 0 : error_init (loc, "excess elements in struct initializer");
12282 : 0 : return;
12283 : : }
12284 : :
12285 : : /* Ignore elements of a brace group if it is entirely superfluous
12286 : : and has already been diagnosed, or if the type is erroneous. */
12287 : 9451212 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12288 : : return;
12289 : :
12290 : : /* Ignore elements of an initializer for a variable-size type.
12291 : : Those are diagnosed in the parser (empty initializer braces are OK). */
12292 : 9451007 : if (COMPLETE_TYPE_P (constructor_type)
12293 : 9451007 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12294 : : return;
12295 : :
12296 : 8747389 : if (!implicit && warn_designated_init && !was_designated
12297 : 8714981 : && TREE_CODE (constructor_type) == RECORD_TYPE
12298 : 10485116 : && lookup_attribute ("designated_init",
12299 : 1034162 : TYPE_ATTRIBUTES (constructor_type)))
12300 : 50 : warning_init (loc,
12301 : : OPT_Wdesignated_init,
12302 : : "positional initialization of field "
12303 : : "in %<struct%> declared with %<designated_init%> attribute");
12304 : :
12305 : : /* If we've exhausted any levels that didn't have braces,
12306 : : pop them now. */
12307 : 10152923 : while (constructor_stack->implicit)
12308 : : {
12309 : 706782 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12310 : 702779 : && constructor_fields == NULL_TREE)
12311 : 701728 : process_init_element (loc,
12312 : : pop_init_level (loc, 1, braced_init_obstack,
12313 : : last_init_list_comma),
12314 : : true, braced_init_obstack);
12315 : 5054 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12316 : 1695 : || gnu_vector_type_p (constructor_type))
12317 : 4003 : && constructor_max_index
12318 : 9030 : && tree_int_cst_lt (constructor_max_index,
12319 : : constructor_index))
12320 : 241 : process_init_element (loc,
12321 : : pop_init_level (loc, 1, braced_init_obstack,
12322 : : last_init_list_comma),
12323 : : true, braced_init_obstack);
12324 : : else
12325 : : break;
12326 : : }
12327 : :
12328 : : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12329 : 9450954 : if (constructor_range_stack)
12330 : : {
12331 : : /* If value is a compound literal and we'll be just using its
12332 : : content, don't put it into a SAVE_EXPR. */
12333 : 365 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12334 : 3 : || !require_constant_value)
12335 : : {
12336 : 362 : tree semantic_type = NULL_TREE;
12337 : 362 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12338 : : {
12339 : 0 : semantic_type = TREE_TYPE (value.value);
12340 : 0 : value.value = TREE_OPERAND (value.value, 0);
12341 : : }
12342 : 362 : value.value = save_expr (value.value);
12343 : 362 : if (semantic_type)
12344 : 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12345 : : value.value);
12346 : : }
12347 : : }
12348 : :
12349 : 10164379 : while (1)
12350 : : {
12351 : 10164379 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12352 : : {
12353 : 1038183 : tree fieldtype;
12354 : 1038183 : enum tree_code fieldcode;
12355 : :
12356 : 1038183 : if (constructor_fields == NULL_TREE)
12357 : : {
12358 : 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12359 : 1280 : break;
12360 : : }
12361 : :
12362 : 1036903 : fieldtype = TREE_TYPE (constructor_fields);
12363 : 1036903 : if (fieldtype != error_mark_node)
12364 : 1036902 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12365 : 1036903 : fieldcode = TREE_CODE (fieldtype);
12366 : :
12367 : : /* Error for non-static initialization of a flexible array member. */
12368 : 1036903 : if (fieldcode == ARRAY_TYPE
12369 : 155242 : && !require_constant_value
12370 : 1100 : && TYPE_SIZE (fieldtype) == NULL_TREE
12371 : 1036913 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12372 : : {
12373 : 10 : error_init (loc, "non-static initialization of a flexible "
12374 : : "array member");
12375 : 10 : break;
12376 : : }
12377 : :
12378 : : /* Error for initialization of a flexible array member with
12379 : : a string constant if the structure is in an array. E.g.:
12380 : : struct S { int x; char y[]; };
12381 : : struct S s[] = { { 1, "foo" } };
12382 : : is invalid. */
12383 : 1036893 : if (string_flag
12384 : 1036893 : && fieldcode == ARRAY_TYPE
12385 : 3352 : && constructor_depth > 1
12386 : 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12387 : 1036940 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12388 : : {
12389 : 47 : bool in_array_p = false;
12390 : 47 : for (struct constructor_stack *p = constructor_stack;
12391 : 73 : p && p->type; p = p->next)
12392 : 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12393 : : {
12394 : : in_array_p = true;
12395 : : break;
12396 : : }
12397 : 47 : if (in_array_p)
12398 : : {
12399 : 33 : error_init (loc, "initialization of flexible array "
12400 : : "member in a nested context");
12401 : 33 : break;
12402 : : }
12403 : : }
12404 : :
12405 : : /* Accept a string constant to initialize a subarray. */
12406 : 1036860 : if (value.value != NULL_TREE
12407 : 1036787 : && fieldcode == ARRAY_TYPE
12408 : 155126 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12409 : 1191509 : && string_flag)
12410 : : value.value = orig_value;
12411 : : /* Otherwise, if we have come to a subaggregate,
12412 : : and we don't have an element of its type, push into it. */
12413 : 1033830 : else if (value.value != NULL_TREE
12414 : 1033541 : && initialize_elementwise_p (fieldtype, value.value))
12415 : : {
12416 : 289 : push_init_level (loc, 1, braced_init_obstack);
12417 : 289 : continue;
12418 : : }
12419 : :
12420 : 1036571 : value.value = maybe_split_raw_data (value.value, &raw_data);
12421 : 1036571 : if (value.value)
12422 : : {
12423 : 1036498 : push_member_name (constructor_fields);
12424 : 1036498 : output_init_element (loc, value.value, value.original_type,
12425 : : strict_string, fieldtype,
12426 : : constructor_fields, true, implicit,
12427 : : braced_init_obstack);
12428 : 1036498 : RESTORE_SPELLING_DEPTH (constructor_depth);
12429 : : }
12430 : : else
12431 : : /* Do the bookkeeping for an element that was
12432 : : directly output as a constructor. */
12433 : : {
12434 : : /* For a record, keep track of end position of last field. */
12435 : 73 : if (DECL_SIZE (constructor_fields))
12436 : 30 : constructor_bit_index
12437 : 30 : = size_binop_loc (input_location, PLUS_EXPR,
12438 : : bit_position (constructor_fields),
12439 : 30 : DECL_SIZE (constructor_fields));
12440 : :
12441 : : /* If the current field was the first one not yet written out,
12442 : : it isn't now, so update. */
12443 : 73 : if (constructor_unfilled_fields == constructor_fields)
12444 : : {
12445 : 64 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12446 : : /* Skip any nameless bit fields. */
12447 : 64 : while (constructor_unfilled_fields != 0
12448 : 64 : && (DECL_UNNAMED_BIT_FIELD
12449 : : (constructor_unfilled_fields)))
12450 : 0 : constructor_unfilled_fields =
12451 : 0 : DECL_CHAIN (constructor_unfilled_fields);
12452 : : }
12453 : : }
12454 : :
12455 : 1036571 : constructor_fields = DECL_CHAIN (constructor_fields);
12456 : : /* Skip any nameless bit fields at the beginning. */
12457 : 1036571 : while (constructor_fields != NULL_TREE
12458 : 1036672 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12459 : 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12460 : : }
12461 : 9126196 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12462 : : {
12463 : 30260 : tree fieldtype;
12464 : 30260 : enum tree_code fieldcode;
12465 : :
12466 : 30260 : if (constructor_fields == NULL_TREE)
12467 : : {
12468 : 3 : pedwarn_init (loc, 0,
12469 : : "excess elements in union initializer");
12470 : 3 : break;
12471 : : }
12472 : :
12473 : 30257 : fieldtype = TREE_TYPE (constructor_fields);
12474 : 30257 : if (fieldtype != error_mark_node)
12475 : 30257 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12476 : 30257 : fieldcode = TREE_CODE (fieldtype);
12477 : :
12478 : : /* Warn that traditional C rejects initialization of unions.
12479 : : We skip the warning if the value is zero. This is done
12480 : : under the assumption that the zero initializer in user
12481 : : code appears conditioned on e.g. __STDC__ to avoid
12482 : : "missing initializer" warnings and relies on default
12483 : : initialization to zero in the traditional C case.
12484 : : We also skip the warning if the initializer is designated,
12485 : : again on the assumption that this must be conditional on
12486 : : __STDC__ anyway (and we've already complained about the
12487 : : member-designator already). */
12488 : 32304 : if (!in_system_header_at (input_location) && !constructor_designated
12489 : 31688 : && !(value.value && (integer_zerop (value.value)
12490 : 1333 : || real_zerop (value.value))))
12491 : 1326 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12492 : : "of unions");
12493 : :
12494 : : /* Error for non-static initialization of a flexible array member. */
12495 : 30257 : if (fieldcode == ARRAY_TYPE
12496 : 766 : && !require_constant_value
12497 : 30314 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12498 : : {
12499 : 3 : error_init (loc, "non-static initialization of a flexible "
12500 : : "array member");
12501 : 3 : break;
12502 : : }
12503 : :
12504 : : /* Error for initialization of a flexible array member with
12505 : : a string constant if the structure is in an array. E.g.:
12506 : : union U { int x; char y[]; };
12507 : : union U s[] = { { 1, "foo" } };
12508 : : is invalid. */
12509 : 30254 : if (string_flag
12510 : 30254 : && fieldcode == ARRAY_TYPE
12511 : 7 : && constructor_depth > 1
12512 : 30258 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12513 : : {
12514 : 3 : bool in_array_p = false;
12515 : 3 : for (struct constructor_stack *p = constructor_stack;
12516 : 3 : p && p->type; p = p->next)
12517 : 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12518 : : {
12519 : : in_array_p = true;
12520 : : break;
12521 : : }
12522 : 3 : if (in_array_p)
12523 : : {
12524 : 3 : error_init (loc, "initialization of flexible array "
12525 : : "member in a nested context");
12526 : 3 : break;
12527 : : }
12528 : : }
12529 : :
12530 : : /* Accept a string constant to initialize a subarray. */
12531 : 30251 : if (value.value != NULL_TREE
12532 : 30251 : && fieldcode == ARRAY_TYPE
12533 : 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12534 : 31006 : && string_flag)
12535 : : value.value = orig_value;
12536 : : /* Otherwise, if we have come to a subaggregate,
12537 : : and we don't have an element of its type, push into it. */
12538 : 30293 : else if (value.value != NULL_TREE
12539 : 30247 : && initialize_elementwise_p (fieldtype, value.value))
12540 : : {
12541 : 46 : push_init_level (loc, 1, braced_init_obstack);
12542 : 46 : continue;
12543 : : }
12544 : :
12545 : 30205 : value.value = maybe_split_raw_data (value.value, &raw_data);
12546 : 30205 : if (value.value)
12547 : : {
12548 : 30205 : push_member_name (constructor_fields);
12549 : 30205 : output_init_element (loc, value.value, value.original_type,
12550 : : strict_string, fieldtype,
12551 : : constructor_fields, true, implicit,
12552 : : braced_init_obstack);
12553 : 30205 : RESTORE_SPELLING_DEPTH (constructor_depth);
12554 : : }
12555 : : else
12556 : : /* Do the bookkeeping for an element that was
12557 : : directly output as a constructor. */
12558 : : {
12559 : 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12560 : 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12561 : : }
12562 : :
12563 : 30205 : constructor_fields = NULL_TREE;
12564 : : }
12565 : 9095936 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12566 : : {
12567 : 2416759 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12568 : 2416759 : enum tree_code eltcode = TREE_CODE (elttype);
12569 : :
12570 : : /* Accept a string constant to initialize a subarray. */
12571 : 2416759 : if (value.value != NULL_TREE
12572 : 2416759 : && eltcode == ARRAY_TYPE
12573 : 6867 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12574 : 2422190 : && string_flag)
12575 : : value.value = orig_value;
12576 : : /* Otherwise, if we have come to a subaggregate,
12577 : : and we don't have an element of its type, push into it. */
12578 : 3118552 : else if (value.value != NULL_TREE
12579 : 2416264 : && initialize_elementwise_p (elttype, value.value))
12580 : : {
12581 : 702288 : push_init_level (loc, 1, braced_init_obstack);
12582 : 702288 : continue;
12583 : : }
12584 : :
12585 : 1714471 : if (constructor_max_index != NULL_TREE
12586 : 1714471 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12587 : 671796 : || integer_all_onesp (constructor_max_index)))
12588 : : {
12589 : 113 : pedwarn_init (loc, 0,
12590 : : "excess elements in array initializer");
12591 : 113 : break;
12592 : : }
12593 : :
12594 : 1714358 : if (value.value
12595 : 1714358 : && TREE_CODE (value.value) == RAW_DATA_CST
12596 : 193 : && RAW_DATA_LENGTH (value.value) > 1
12597 : 193 : && (TREE_CODE (elttype) == INTEGER_TYPE
12598 : 193 : || TREE_CODE (elttype) == BITINT_TYPE)
12599 : 193 : && TYPE_PRECISION (elttype) == CHAR_BIT
12600 : 1714551 : && (constructor_max_index == NULL_TREE
12601 : 55 : || tree_int_cst_lt (constructor_index,
12602 : : constructor_max_index)))
12603 : : {
12604 : 193 : unsigned int len = RAW_DATA_LENGTH (value.value);
12605 : 193 : if (constructor_max_index)
12606 : : {
12607 : 55 : widest_int w = wi::to_widest (constructor_max_index);
12608 : 55 : w -= wi::to_widest (constructor_index);
12609 : 55 : w += 1;
12610 : 55 : if (w < len)
12611 : 3 : len = w.to_uhwi ();
12612 : 55 : }
12613 : 193 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12614 : : {
12615 : 3 : raw_data = copy_node (value.value);
12616 : 3 : RAW_DATA_LENGTH (raw_data) -= len;
12617 : 3 : RAW_DATA_POINTER (raw_data) += len;
12618 : 3 : RAW_DATA_LENGTH (value.value) = len;
12619 : : }
12620 : 193 : TREE_TYPE (value.value) = elttype;
12621 : 193 : push_array_bounds (tree_to_uhwi (constructor_index));
12622 : 193 : output_init_element (loc, value.value, value.original_type,
12623 : : false, elttype, constructor_index, true,
12624 : : implicit, braced_init_obstack);
12625 : 193 : RESTORE_SPELLING_DEPTH (constructor_depth);
12626 : 193 : constructor_index
12627 : 193 : = size_binop_loc (input_location, PLUS_EXPR,
12628 : 193 : constructor_index, bitsize_int (len));
12629 : : }
12630 : : else
12631 : : {
12632 : 1714165 : value.value = maybe_split_raw_data (value.value, &raw_data);
12633 : : /* Now output the actual element. */
12634 : 1714165 : if (value.value)
12635 : : {
12636 : 1714165 : push_array_bounds (tree_to_uhwi (constructor_index));
12637 : 1714165 : output_init_element (loc, value.value, value.original_type,
12638 : : strict_string, elttype,
12639 : : constructor_index, true, implicit,
12640 : : braced_init_obstack);
12641 : 1714165 : RESTORE_SPELLING_DEPTH (constructor_depth);
12642 : : }
12643 : :
12644 : 1714165 : constructor_index
12645 : 1714165 : = size_binop_loc (input_location, PLUS_EXPR,
12646 : : constructor_index, bitsize_one_node);
12647 : :
12648 : 1714165 : if (!value.value)
12649 : : /* If we are doing the bookkeeping for an element that was
12650 : : directly output as a constructor, we must update
12651 : : constructor_unfilled_index. */
12652 : 0 : constructor_unfilled_index = constructor_index;
12653 : : }
12654 : : }
12655 : 6679177 : else if (gnu_vector_type_p (constructor_type))
12656 : : {
12657 : 6678735 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12658 : :
12659 : : /* Do a basic check of initializer size. Note that vectors
12660 : : may not always have a fixed size derived from their type. */
12661 : 6678735 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12662 : 6678735 : tree_to_poly_uint64 (constructor_index)))
12663 : : {
12664 : : /* Diagose VLA out-of-bounds as errors. */
12665 : 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
12666 : 2 : pedwarn_init (loc, 0,
12667 : : "excess elements in vector initializer");
12668 : : else
12669 : : error_init (loc, "excess elements in vector initializer");
12670 : :
12671 : : break;
12672 : : }
12673 : :
12674 : 6678733 : value.value = maybe_split_raw_data (value.value, &raw_data);
12675 : : /* Now output the actual element. */
12676 : 6678733 : if (value.value)
12677 : : {
12678 : 6678733 : if (TREE_CODE (value.value) == VECTOR_CST)
12679 : 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
12680 : 6678733 : output_init_element (loc, value.value, value.original_type,
12681 : : strict_string, elttype,
12682 : : constructor_index, true, implicit,
12683 : : braced_init_obstack);
12684 : : }
12685 : :
12686 : 6678733 : constructor_index
12687 : 6678733 : = size_binop_loc (input_location,
12688 : : PLUS_EXPR, constructor_index, bitsize_one_node);
12689 : :
12690 : 6678733 : if (!value.value)
12691 : : /* If we are doing the bookkeeping for an element that was
12692 : : directly output as a constructor, we must update
12693 : : constructor_unfilled_index. */
12694 : 0 : constructor_unfilled_index = constructor_index;
12695 : : }
12696 : :
12697 : : /* Handle the sole element allowed in a braced initializer
12698 : : for a scalar variable. */
12699 : 442 : else if (constructor_type != error_mark_node
12700 : 442 : && constructor_fields == NULL_TREE)
12701 : : {
12702 : 27 : pedwarn_init (loc, 0,
12703 : : "excess elements in scalar initializer");
12704 : 27 : break;
12705 : : }
12706 : : else
12707 : : {
12708 : 415 : value.value = maybe_split_raw_data (value.value, &raw_data);
12709 : 415 : if (value.value)
12710 : 415 : output_init_element (loc, value.value, value.original_type,
12711 : : strict_string, constructor_type,
12712 : : NULL_TREE, true, implicit,
12713 : : braced_init_obstack);
12714 : 415 : constructor_fields = NULL_TREE;
12715 : : }
12716 : :
12717 : : /* Handle range initializers either at this level or anywhere higher
12718 : : in the designator stack. */
12719 : 9460282 : if (constructor_range_stack)
12720 : : {
12721 : 10802 : struct constructor_range_stack *p, *range_stack;
12722 : 10802 : int finish = 0;
12723 : :
12724 : 10802 : range_stack = constructor_range_stack;
12725 : 10802 : constructor_range_stack = 0;
12726 : 10802 : while (constructor_stack != range_stack->stack)
12727 : : {
12728 : 0 : gcc_assert (constructor_stack->implicit);
12729 : 0 : process_init_element (loc,
12730 : : pop_init_level (loc, 1,
12731 : : braced_init_obstack,
12732 : : last_init_list_comma),
12733 : : true, braced_init_obstack);
12734 : : }
12735 : 325 : for (p = range_stack;
12736 : 11127 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
12737 : 325 : p = p->prev)
12738 : : {
12739 : 325 : gcc_assert (constructor_stack->implicit);
12740 : 325 : process_init_element (loc,
12741 : : pop_init_level (loc, 1,
12742 : : braced_init_obstack,
12743 : : last_init_list_comma),
12744 : : true, braced_init_obstack);
12745 : : }
12746 : :
12747 : 10802 : p->index = size_binop_loc (input_location,
12748 : : PLUS_EXPR, p->index, bitsize_one_node);
12749 : 10802 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
12750 : 373 : finish = 1;
12751 : :
12752 : 11127 : while (1)
12753 : : {
12754 : 11127 : constructor_index = p->index;
12755 : 11127 : constructor_fields = p->fields;
12756 : 11127 : if (finish && p->range_end && p->index == p->range_start)
12757 : : {
12758 : 8 : finish = 0;
12759 : 8 : p->prev = 0;
12760 : : }
12761 : 11127 : p = p->next;
12762 : 11127 : if (!p)
12763 : : break;
12764 : 325 : finish_implicit_inits (loc, braced_init_obstack);
12765 : 325 : push_init_level (loc, 2, braced_init_obstack);
12766 : 325 : p->stack = constructor_stack;
12767 : 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
12768 : 33 : p->index = p->range_start;
12769 : : }
12770 : :
12771 : 10802 : if (!finish)
12772 : 10437 : constructor_range_stack = range_stack;
12773 : 10802 : continue;
12774 : 10802 : }
12775 : :
12776 : : break;
12777 : : }
12778 : :
12779 : 9450954 : constructor_range_stack = 0;
12780 : :
12781 : 9450954 : if (raw_data && RAW_DATA_LENGTH (raw_data))
12782 : : {
12783 : 212 : gcc_assert (!string_flag && !was_designated);
12784 : 212 : value.value = raw_data;
12785 : 212 : raw_data = NULL_TREE;
12786 : 212 : goto retry;
12787 : : }
12788 : : }
12789 : :
12790 : : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
12791 : : (guaranteed to be 'volatile' or null) and ARGS (represented using
12792 : : an ASM_EXPR node). */
12793 : : tree
12794 : 200924 : build_asm_stmt (bool is_volatile, tree args)
12795 : : {
12796 : 200924 : if (is_volatile)
12797 : 161286 : ASM_VOLATILE_P (args) = 1;
12798 : 200924 : return add_stmt (args);
12799 : : }
12800 : :
12801 : : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
12802 : : some INPUTS, and some CLOBBERS. The latter three may be NULL.
12803 : : SIMPLE indicates whether there was anything at all after the
12804 : : string in the asm expression -- asm("blah") and asm("blah" : )
12805 : : are subtly different. We use a ASM_EXPR node to represent this.
12806 : : LOC is the location of the asm, and IS_INLINE says whether this
12807 : : is asm inline. */
12808 : : tree
12809 : 200953 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
12810 : : tree clobbers, tree labels, bool simple, bool is_inline)
12811 : : {
12812 : 200953 : tree tail;
12813 : 200953 : tree args;
12814 : 200953 : int i;
12815 : 200953 : const char *constraint;
12816 : 200953 : const char **oconstraints;
12817 : 200953 : bool allows_mem, allows_reg, is_inout;
12818 : 200953 : int ninputs, noutputs;
12819 : :
12820 : 200953 : ninputs = list_length (inputs);
12821 : 200953 : noutputs = list_length (outputs);
12822 : 200953 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
12823 : :
12824 : 200953 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
12825 : :
12826 : : /* Remove output conversions that change the type but not the mode. */
12827 : 540409 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
12828 : : {
12829 : 339456 : tree output = TREE_VALUE (tail);
12830 : :
12831 : 339456 : output = c_fully_fold (output, false, NULL, true);
12832 : :
12833 : : /* ??? Really, this should not be here. Users should be using a
12834 : : proper lvalue, dammit. But there's a long history of using casts
12835 : : in the output operands. In cases like longlong.h, this becomes a
12836 : : primitive form of typechecking -- if the cast can be removed, then
12837 : : the output operand had a type of the proper width; otherwise we'll
12838 : : get an error. Gross, but ... */
12839 : 339456 : STRIP_NOPS (output);
12840 : :
12841 : 339456 : if (!lvalue_or_else (loc, output, lv_asm))
12842 : 4 : output = error_mark_node;
12843 : :
12844 : 339456 : if (output != error_mark_node
12845 : 339456 : && (TREE_READONLY (output)
12846 : 339450 : || TYPE_READONLY (TREE_TYPE (output))
12847 : 339450 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
12848 : 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
12849 : 2 : readonly_error (loc, output, lv_asm);
12850 : :
12851 : 339456 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
12852 : 339456 : oconstraints[i] = constraint;
12853 : :
12854 : 339456 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
12855 : : &allows_mem, &allows_reg, &is_inout,
12856 : : nullptr))
12857 : : {
12858 : : /* If the operand is going to end up in memory,
12859 : : mark it addressable. */
12860 : 339448 : if (!allows_reg && !c_mark_addressable (output))
12861 : 3 : output = error_mark_node;
12862 : 1088 : if (!(!allows_reg && allows_mem)
12863 : 338360 : && output != error_mark_node
12864 : 677807 : && VOID_TYPE_P (TREE_TYPE (output)))
12865 : : {
12866 : 4 : error_at (loc, "invalid use of void expression");
12867 : 4 : output = error_mark_node;
12868 : : }
12869 : 339448 : if (allows_reg && current_function_decl == NULL_TREE)
12870 : : {
12871 : 1 : error_at (loc, "constraint allows registers outside of "
12872 : : "a function");
12873 : 1 : output = error_mark_node;
12874 : : }
12875 : : }
12876 : : else
12877 : 8 : output = error_mark_node;
12878 : :
12879 : 339456 : if (current_function_decl == NULL_TREE && output != error_mark_node)
12880 : : {
12881 : 7 : if (TREE_SIDE_EFFECTS (output))
12882 : : {
12883 : 0 : error_at (loc, "side-effects in output operand outside "
12884 : : "of a function");
12885 : 0 : output = error_mark_node;
12886 : : }
12887 : : else
12888 : : {
12889 : 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
12890 : 7 : if (addr == error_mark_node)
12891 : : output = error_mark_node;
12892 : 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
12893 : : {
12894 : 1 : error_at (loc, "output operand outside of a function is not "
12895 : : "constant");
12896 : 1 : output = error_mark_node;
12897 : : }
12898 : : }
12899 : : }
12900 : 339449 : else if (output != error_mark_node && strstr (constraint, "-"))
12901 : : {
12902 : 1 : error_at (loc, "%<-%> modifier used inside of a function");
12903 : 1 : output = error_mark_node;
12904 : : }
12905 : :
12906 : 339456 : TREE_VALUE (tail) = output;
12907 : : }
12908 : :
12909 : 556133 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
12910 : : {
12911 : 355180 : tree input;
12912 : :
12913 : 355180 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
12914 : 355180 : input = TREE_VALUE (tail);
12915 : :
12916 : 355180 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
12917 : : oconstraints, &allows_mem, &allows_reg,
12918 : : nullptr))
12919 : : {
12920 : : /* If the operand is going to end up in memory,
12921 : : mark it addressable. */
12922 : 355173 : if (!allows_reg && allows_mem)
12923 : : {
12924 : 1080 : input = c_fully_fold (input, false, NULL, true);
12925 : :
12926 : : /* Strip the nops as we allow this case. FIXME, this really
12927 : : should be rejected or made deprecated. */
12928 : 1080 : STRIP_NOPS (input);
12929 : 1080 : if (!c_mark_addressable (input))
12930 : 2 : input = error_mark_node;
12931 : : }
12932 : : else
12933 : : {
12934 : 354093 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
12935 : : true, false),
12936 : : false, NULL);
12937 : :
12938 : 354093 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
12939 : : {
12940 : 4 : error_at (loc, "invalid use of void expression");
12941 : 4 : input = error_mark_node;
12942 : : }
12943 : : }
12944 : 355173 : if (allows_reg && current_function_decl == NULL_TREE)
12945 : : {
12946 : 1 : error_at (loc, "constraint allows registers outside of "
12947 : : "a function");
12948 : 1 : input = error_mark_node;
12949 : : }
12950 : 355173 : if (constraint[0] == ':' && input != error_mark_node)
12951 : : {
12952 : 12 : tree t = input;
12953 : 12 : STRIP_NOPS (t);
12954 : 12 : if (TREE_CODE (t) != ADDR_EXPR
12955 : 12 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
12956 : 8 : || (VAR_P (TREE_OPERAND (t, 0))
12957 : 6 : && is_global_var (TREE_OPERAND (t, 0)))))
12958 : : {
12959 : 5 : error_at (loc, "%<:%> constraint operand is not address "
12960 : : "of a function or non-automatic variable");
12961 : 5 : input = error_mark_node;
12962 : : }
12963 : : }
12964 : : }
12965 : : else
12966 : 7 : input = error_mark_node;
12967 : :
12968 : 355180 : if (current_function_decl == NULL_TREE && input != error_mark_node)
12969 : : {
12970 : 30 : if (TREE_SIDE_EFFECTS (input))
12971 : : {
12972 : 2 : error_at (loc, "side-effects in input operand outside "
12973 : : "of a function");
12974 : 2 : input = error_mark_node;
12975 : : }
12976 : : else
12977 : : {
12978 : 28 : tree tem = input;
12979 : 28 : if (allows_mem && lvalue_p (input))
12980 : 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
12981 : 28 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
12982 : : {
12983 : 2 : error_at (loc, "input operand outside of a function is not "
12984 : : "constant");
12985 : 2 : input = error_mark_node;
12986 : : }
12987 : : }
12988 : : }
12989 : 355150 : else if (input != error_mark_node && strstr (constraint, "-"))
12990 : : {
12991 : 3 : error_at (loc, "%<-%> modifier used inside of a function");
12992 : 3 : input = error_mark_node;
12993 : : }
12994 : :
12995 : 355180 : TREE_VALUE (tail) = input;
12996 : : }
12997 : :
12998 : 200953 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
12999 : :
13000 : : /* asm statements without outputs, including simple ones, are treated
13001 : : as volatile. */
13002 : 200953 : ASM_BASIC_P (args) = simple;
13003 : 200953 : ASM_VOLATILE_P (args) = (noutputs == 0);
13004 : 200953 : ASM_INLINE_P (args) = is_inline;
13005 : :
13006 : 200953 : return args;
13007 : : }
13008 : :
13009 : : /* Generate a goto statement to LABEL. LOC is the location of the
13010 : : GOTO. */
13011 : :
13012 : : tree
13013 : 82509 : c_finish_goto_label (location_t loc, tree label)
13014 : : {
13015 : 82509 : tree decl = lookup_label_for_goto (loc, label);
13016 : 82509 : if (!decl)
13017 : : return NULL_TREE;
13018 : 82509 : TREE_USED (decl) = 1;
13019 : 82509 : {
13020 : 82509 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13021 : 82509 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13022 : 82509 : SET_EXPR_LOCATION (t, loc);
13023 : 82509 : return add_stmt (t);
13024 : : }
13025 : : }
13026 : :
13027 : : /* Generate a computed goto statement to EXPR. LOC is the location of
13028 : : the GOTO. */
13029 : :
13030 : : tree
13031 : 930 : c_finish_goto_ptr (location_t loc, c_expr val)
13032 : : {
13033 : 930 : tree expr = val.value;
13034 : 930 : tree t;
13035 : 930 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13036 : 930 : if (expr != error_mark_node
13037 : 929 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13038 : 935 : && !null_pointer_constant_p (expr))
13039 : : {
13040 : 2 : error_at (val.get_location (),
13041 : : "computed goto must be pointer type");
13042 : 2 : expr = build_zero_cst (ptr_type_node);
13043 : : }
13044 : 930 : expr = c_fully_fold (expr, false, NULL);
13045 : 930 : expr = convert (ptr_type_node, expr);
13046 : 930 : t = build1 (GOTO_EXPR, void_type_node, expr);
13047 : 930 : SET_EXPR_LOCATION (t, loc);
13048 : 930 : return add_stmt (t);
13049 : : }
13050 : :
13051 : : /* Generate a C `return' statement. RETVAL is the expression for what
13052 : : to return, or a null pointer for `return;' with no value. LOC is
13053 : : the location of the return statement, or the location of the expression,
13054 : : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13055 : : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13056 : : attribute. */
13057 : :
13058 : : tree
13059 : 33753927 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13060 : : {
13061 : 33753927 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13062 : 33753927 : bool no_warning = false;
13063 : 33753927 : bool npc = false;
13064 : :
13065 : : /* Use the expansion point to handle cases such as returning NULL
13066 : : in a function returning void. */
13067 : 33753927 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13068 : :
13069 : 33753927 : if (TREE_THIS_VOLATILE (current_function_decl))
13070 : 23 : warning_at (xloc, 0,
13071 : : "function declared %<noreturn%> has a %<return%> statement");
13072 : :
13073 : 33753927 : set_musttail_on_return (retval, xloc, musttail_p);
13074 : :
13075 : 33753927 : if (retval)
13076 : : {
13077 : 33732920 : tree semantic_type = NULL_TREE;
13078 : 33732920 : npc = null_pointer_constant_p (retval);
13079 : 33732920 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13080 : : {
13081 : 92 : semantic_type = TREE_TYPE (retval);
13082 : 92 : retval = TREE_OPERAND (retval, 0);
13083 : : }
13084 : 33732920 : retval = c_fully_fold (retval, false, NULL);
13085 : 33732920 : if (semantic_type
13086 : 33732920 : && valtype != NULL_TREE
13087 : 92 : && TREE_CODE (valtype) != VOID_TYPE)
13088 : 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13089 : : }
13090 : :
13091 : 33732920 : if (!retval)
13092 : : {
13093 : 21007 : current_function_returns_null = 1;
13094 : 21007 : if ((warn_return_type >= 0 || flag_isoc99)
13095 : 20882 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13096 : : {
13097 : 46 : no_warning = true;
13098 : 47 : if (emit_diagnostic (flag_isoc99
13099 : : ? diagnostics::kind::permerror
13100 : : : diagnostics::kind::warning,
13101 : 46 : loc, OPT_Wreturn_mismatch,
13102 : : "%<return%> with no value,"
13103 : : " in function returning non-void"))
13104 : 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13105 : : "declared here");
13106 : : }
13107 : : }
13108 : 33732920 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13109 : : {
13110 : 321 : current_function_returns_null = 1;
13111 : 321 : bool warned_here;
13112 : 321 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13113 : 64 : warned_here = permerror_opt
13114 : 64 : (xloc, OPT_Wreturn_mismatch,
13115 : : "%<return%> with a value, in function returning void");
13116 : : else
13117 : 257 : warned_here = pedwarn
13118 : 257 : (xloc, OPT_Wpedantic, "ISO C forbids "
13119 : : "%<return%> with expression, in function returning void");
13120 : 321 : if (warned_here)
13121 : 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13122 : : "declared here");
13123 : : }
13124 : : else
13125 : : {
13126 : 33732599 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13127 : : retval, origtype, ic_return,
13128 : : npc, NULL_TREE, NULL_TREE, 0);
13129 : 33732599 : tree res = DECL_RESULT (current_function_decl);
13130 : 33732599 : tree inner;
13131 : 33732599 : bool save;
13132 : :
13133 : 33732599 : current_function_returns_value = 1;
13134 : 33732599 : if (t == error_mark_node)
13135 : : {
13136 : : /* Suppress -Wreturn-type for this function. */
13137 : 298 : if (warn_return_type)
13138 : 297 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13139 : 298 : return NULL_TREE;
13140 : : }
13141 : :
13142 : 33732301 : save = in_late_binary_op;
13143 : 67457076 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13144 : 33723970 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13145 : 67454991 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13146 : 327774 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13147 : 327774 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13148 : 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13149 : 9611 : in_late_binary_op = true;
13150 : 33732301 : inner = t = convert (TREE_TYPE (res), t);
13151 : 33732301 : in_late_binary_op = save;
13152 : :
13153 : : /* Strip any conversions, additions, and subtractions, and see if
13154 : : we are returning the address of a local variable. Warn if so. */
13155 : 36930334 : while (1)
13156 : : {
13157 : 36930334 : switch (TREE_CODE (inner))
13158 : : {
13159 : 3196624 : CASE_CONVERT:
13160 : 3196624 : case NON_LVALUE_EXPR:
13161 : 3196624 : case PLUS_EXPR:
13162 : 3196624 : case POINTER_PLUS_EXPR:
13163 : 3196624 : inner = TREE_OPERAND (inner, 0);
13164 : 3196624 : continue;
13165 : :
13166 : 1414 : case MINUS_EXPR:
13167 : : /* If the second operand of the MINUS_EXPR has a pointer
13168 : : type (or is converted from it), this may be valid, so
13169 : : don't give a warning. */
13170 : 1414 : {
13171 : 1414 : tree op1 = TREE_OPERAND (inner, 1);
13172 : :
13173 : 3068 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13174 : 3313 : && (CONVERT_EXPR_P (op1)
13175 : 1409 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13176 : 245 : op1 = TREE_OPERAND (op1, 0);
13177 : :
13178 : 1414 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13179 : : break;
13180 : :
13181 : 1409 : inner = TREE_OPERAND (inner, 0);
13182 : 1409 : continue;
13183 : 1409 : }
13184 : :
13185 : 2867 : case ADDR_EXPR:
13186 : 2867 : inner = TREE_OPERAND (inner, 0);
13187 : :
13188 : 2867 : while (REFERENCE_CLASS_P (inner)
13189 : 3740 : && !INDIRECT_REF_P (inner))
13190 : 873 : inner = TREE_OPERAND (inner, 0);
13191 : :
13192 : 2867 : if (DECL_P (inner)
13193 : 1559 : && !DECL_EXTERNAL (inner)
13194 : 931 : && !TREE_STATIC (inner)
13195 : 132 : && DECL_CONTEXT (inner) == current_function_decl
13196 : 2920 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13197 : : {
13198 : 13 : if (TREE_CODE (inner) == LABEL_DECL)
13199 : 4 : warning_at (loc, OPT_Wreturn_local_addr,
13200 : : "function returns address of label");
13201 : : else
13202 : : {
13203 : 9 : warning_at (loc, OPT_Wreturn_local_addr,
13204 : : "function returns address of local variable");
13205 : 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13206 : 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13207 : : }
13208 : : }
13209 : : break;
13210 : :
13211 : : default:
13212 : : break;
13213 : 3196624 : }
13214 : :
13215 : 33732301 : break;
13216 : : }
13217 : :
13218 : 33732301 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13219 : 33732301 : SET_EXPR_LOCATION (retval, loc);
13220 : :
13221 : 33732301 : if (warn_sequence_point)
13222 : 2958332 : verify_sequence_points (retval);
13223 : : }
13224 : :
13225 : 33753629 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13226 : 33753629 : if (no_warning)
13227 : 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13228 : 33753629 : return add_stmt (ret_stmt);
13229 : : }
13230 : :
13231 : : struct c_switch {
13232 : : /* The SWITCH_STMT being built. */
13233 : : tree switch_stmt;
13234 : :
13235 : : /* The original type of the testing expression, i.e. before the
13236 : : default conversion is applied. */
13237 : : tree orig_type;
13238 : :
13239 : : /* A splay-tree mapping the low element of a case range to the high
13240 : : element, or NULL_TREE if there is no high element. Used to
13241 : : determine whether or not a new case label duplicates an old case
13242 : : label. We need a tree, rather than simply a hash table, because
13243 : : of the GNU case range extension. */
13244 : : splay_tree cases;
13245 : :
13246 : : /* The bindings at the point of the switch. This is used for
13247 : : warnings crossing decls when branching to a case label. */
13248 : : struct c_spot_bindings *bindings;
13249 : :
13250 : : /* Whether the switch includes any break statements. */
13251 : : bool break_stmt_seen_p;
13252 : :
13253 : : /* The next node on the stack. */
13254 : : struct c_switch *next;
13255 : :
13256 : : /* Remember whether the controlling expression had boolean type
13257 : : before integer promotions for the sake of -Wswitch-bool. */
13258 : : bool bool_cond_p;
13259 : : };
13260 : :
13261 : : /* A stack of the currently active switch statements. The innermost
13262 : : switch statement is on the top of the stack. There is no need to
13263 : : mark the stack for garbage collection because it is only active
13264 : : during the processing of the body of a function, and we never
13265 : : collect at that point. */
13266 : :
13267 : : struct c_switch *c_switch_stack;
13268 : :
13269 : : /* Start a C switch statement, testing expression EXP. Return the new
13270 : : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13271 : : SWITCH_COND_LOC is the location of the switch's condition.
13272 : : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13273 : :
13274 : : tree
13275 : 36632 : c_start_switch (location_t switch_loc,
13276 : : location_t switch_cond_loc,
13277 : : tree exp, bool explicit_cast_p, tree switch_name)
13278 : : {
13279 : 36632 : tree orig_type = error_mark_node;
13280 : 36632 : bool bool_cond_p = false;
13281 : 36632 : struct c_switch *cs;
13282 : :
13283 : 36632 : if (exp != error_mark_node)
13284 : : {
13285 : 36589 : orig_type = TREE_TYPE (exp);
13286 : :
13287 : 36589 : if (!INTEGRAL_TYPE_P (orig_type))
13288 : : {
13289 : 12 : if (orig_type != error_mark_node)
13290 : : {
13291 : 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13292 : 12 : orig_type = error_mark_node;
13293 : : }
13294 : 12 : exp = integer_zero_node;
13295 : : }
13296 : : else
13297 : : {
13298 : 36577 : tree type = TYPE_MAIN_VARIANT (orig_type);
13299 : 36577 : tree e = exp;
13300 : :
13301 : : /* Warn if the condition has boolean value. */
13302 : 36580 : while (TREE_CODE (e) == COMPOUND_EXPR)
13303 : 3 : e = TREE_OPERAND (e, 1);
13304 : :
13305 : 36552 : if ((C_BOOLEAN_TYPE_P (type)
13306 : 36552 : || truth_value_p (TREE_CODE (e)))
13307 : : /* Explicit cast to int suppresses this warning. */
13308 : 36599 : && !(TREE_CODE (type) == INTEGER_TYPE
13309 : : && explicit_cast_p))
13310 : : bool_cond_p = true;
13311 : :
13312 : 36577 : if (!in_system_header_at (input_location)
13313 : 36577 : && (type == long_integer_type_node
13314 : 11866 : || type == long_unsigned_type_node))
13315 : 258 : warning_at (switch_cond_loc,
13316 : 258 : OPT_Wtraditional, "%<long%> switch expression not "
13317 : : "converted to %<int%> in ISO C");
13318 : :
13319 : 36577 : exp = c_fully_fold (exp, false, NULL);
13320 : 36577 : exp = default_conversion (exp);
13321 : :
13322 : 36577 : if (warn_sequence_point)
13323 : 6005 : verify_sequence_points (exp);
13324 : : }
13325 : : }
13326 : :
13327 : : /* Add this new SWITCH_STMT to the stack. */
13328 : 36632 : cs = XNEW (struct c_switch);
13329 : 36632 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13330 : : NULL_TREE, orig_type, NULL_TREE, switch_name);
13331 : 36632 : cs->orig_type = orig_type;
13332 : 36632 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13333 : 36632 : cs->bindings = c_get_switch_bindings ();
13334 : 36632 : cs->break_stmt_seen_p = false;
13335 : 36632 : cs->bool_cond_p = bool_cond_p;
13336 : 36632 : cs->next = c_switch_stack;
13337 : 36632 : c_switch_stack = cs;
13338 : :
13339 : 36632 : return add_stmt (cs->switch_stmt);
13340 : : }
13341 : :
13342 : : /* Process a case label at location LOC, with attributes ATTRS. */
13343 : :
13344 : : tree
13345 : 1024695 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13346 : : {
13347 : 1024695 : tree label = NULL_TREE;
13348 : :
13349 : 1024695 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13350 : : {
13351 : 87 : low_value = c_fully_fold (low_value, false, NULL);
13352 : 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13353 : 9 : pedwarn (loc, OPT_Wpedantic,
13354 : : "case label is not an integer constant expression");
13355 : : }
13356 : :
13357 : 1024695 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13358 : : {
13359 : 0 : high_value = c_fully_fold (high_value, false, NULL);
13360 : 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13361 : 0 : pedwarn (input_location, OPT_Wpedantic,
13362 : : "case label is not an integer constant expression");
13363 : : }
13364 : :
13365 : 1024695 : if (c_switch_stack == NULL)
13366 : : {
13367 : 3 : if (low_value)
13368 : 2 : error_at (loc, "case label not within a switch statement");
13369 : : else
13370 : 1 : error_at (loc, "%<default%> label not within a switch statement");
13371 : 3 : return NULL_TREE;
13372 : : }
13373 : :
13374 : 1024692 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13375 : 1024692 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13376 : : loc))
13377 : : return NULL_TREE;
13378 : :
13379 : 1024680 : label = c_add_case_label (loc, c_switch_stack->cases,
13380 : 1024680 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13381 : : low_value, high_value, attrs);
13382 : 1024680 : if (label == error_mark_node)
13383 : 144 : label = NULL_TREE;
13384 : : return label;
13385 : : }
13386 : :
13387 : : /* Finish the switch statement. TYPE is the original type of the
13388 : : controlling expression of the switch, or NULL_TREE. */
13389 : :
13390 : : void
13391 : 36632 : c_finish_switch (tree body, tree type)
13392 : : {
13393 : 36632 : struct c_switch *cs = c_switch_stack;
13394 : 36632 : location_t switch_location;
13395 : :
13396 : 36632 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13397 : :
13398 : : /* Emit warnings as needed. */
13399 : 36632 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13400 : 42733 : c_do_switch_warnings (cs->cases, switch_location,
13401 : 6101 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13402 : 36632 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13403 : 36632 : if (c_switch_covers_all_cases_p (cs->cases,
13404 : 36632 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13405 : 32743 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13406 : 36632 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13407 : :
13408 : : /* Pop the stack. */
13409 : 36632 : c_switch_stack = cs->next;
13410 : 36632 : splay_tree_delete (cs->cases);
13411 : 36632 : c_release_switch_bindings (cs->bindings);
13412 : 36632 : XDELETE (cs);
13413 : 36632 : }
13414 : :
13415 : : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13416 : : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13417 : : may be null. */
13418 : :
13419 : : void
13420 : 1247514 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13421 : : tree else_block)
13422 : : {
13423 : 1247514 : tree stmt;
13424 : :
13425 : 1247514 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13426 : 1247514 : SET_EXPR_LOCATION (stmt, if_locus);
13427 : 1247514 : add_stmt (stmt);
13428 : 1247514 : }
13429 : :
13430 : : tree
13431 : 183506 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13432 : : {
13433 : : /* In switch statements break is sometimes stylistically used after
13434 : : a return statement. This can lead to spurious warnings about
13435 : : control reaching the end of a non-void function when it is
13436 : : inlined. Note that we are calling block_may_fallthru with
13437 : : language specific tree nodes; this works because
13438 : : block_may_fallthru returns true when given something it does not
13439 : : understand. */
13440 : 183506 : bool skip = !block_may_fallthru (cur_stmt_list);
13441 : :
13442 : 183506 : if (is_break)
13443 : 171261 : switch (in_statement & ~IN_NAMED_STMT)
13444 : : {
13445 : 9 : case 0:
13446 : 9 : error_at (loc, "break statement not within loop or switch");
13447 : 9 : return NULL_TREE;
13448 : 4 : case IN_OMP_BLOCK:
13449 : 4 : error_at (loc, "invalid exit from OpenMP structured block");
13450 : 4 : return NULL_TREE;
13451 : 12 : case IN_OMP_FOR:
13452 : 12 : error_at (loc, "break statement used with OpenMP for loop");
13453 : 12 : return NULL_TREE;
13454 : : case IN_ITERATION_STMT:
13455 : : case IN_OBJC_FOREACH:
13456 : : break;
13457 : 155810 : default:
13458 : 155810 : gcc_assert (in_statement & IN_SWITCH_STMT);
13459 : 155810 : c_switch_stack->break_stmt_seen_p = true;
13460 : 155810 : break;
13461 : : }
13462 : : else
13463 : 12245 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13464 : : {
13465 : 7 : case 0:
13466 : 7 : error_at (loc, "continue statement not within a loop");
13467 : 7 : return NULL_TREE;
13468 : 4 : case IN_OMP_BLOCK:
13469 : 4 : error_at (loc, "invalid exit from OpenMP structured block");
13470 : 4 : return NULL_TREE;
13471 : : case IN_ITERATION_STMT:
13472 : : case IN_OMP_FOR:
13473 : : case IN_OBJC_FOREACH:
13474 : : break;
13475 : 0 : default:
13476 : 0 : gcc_unreachable ();
13477 : : }
13478 : :
13479 : 183470 : if (skip)
13480 : : return NULL_TREE;
13481 : 182966 : else if ((in_statement & IN_OBJC_FOREACH)
13482 : 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13483 : 0 : && name == NULL_TREE)
13484 : : {
13485 : : /* The foreach expander produces low-level code using gotos instead
13486 : : of a structured loop construct. */
13487 : 0 : gcc_assert (label);
13488 : 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13489 : : }
13490 : 183063 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13491 : : {
13492 : 0 : label = DECL_CHAIN (name);
13493 : 0 : if (!is_break)
13494 : 0 : label = DECL_CHAIN (label);
13495 : : /* Foreach expander from some outer level. */
13496 : 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13497 : : }
13498 : 195200 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13499 : 182966 : name));
13500 : : }
13501 : :
13502 : : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13503 : :
13504 : : static void
13505 : 6242029 : emit_side_effect_warnings (location_t loc, tree expr)
13506 : : {
13507 : 6242029 : maybe_warn_nodiscard (loc, expr);
13508 : 6242029 : if (!warn_unused_value)
13509 : : return;
13510 : 1290859 : if (expr == error_mark_node)
13511 : : ;
13512 : 1290784 : else if (!TREE_SIDE_EFFECTS (expr))
13513 : : {
13514 : 6408 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13515 : 6408 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13516 : 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13517 : : }
13518 : 1284376 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13519 : : {
13520 : : tree r = expr;
13521 : : location_t cloc = loc;
13522 : 11991 : while (TREE_CODE (r) == COMPOUND_EXPR)
13523 : : {
13524 : 6011 : if (EXPR_HAS_LOCATION (r))
13525 : 5481 : cloc = EXPR_LOCATION (r);
13526 : 6011 : r = TREE_OPERAND (r, 1);
13527 : : }
13528 : 5980 : if (!TREE_SIDE_EFFECTS (r)
13529 : 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13530 : 24 : && !CONVERT_EXPR_P (r)
13531 : 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13532 : 5992 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13533 : 8 : warning_at (cloc, OPT_Wunused_value,
13534 : : "right-hand operand of comma expression has no effect");
13535 : : }
13536 : : else
13537 : 1278396 : warn_if_unused_value (expr, loc);
13538 : : }
13539 : :
13540 : : /* Process an expression as if it were a complete statement. Emit
13541 : : diagnostics, but do not call ADD_STMT. LOC is the location of the
13542 : : statement. */
13543 : :
13544 : : tree
13545 : 6118714 : c_process_expr_stmt (location_t loc, tree expr)
13546 : : {
13547 : 6118714 : tree exprv;
13548 : :
13549 : 6118714 : if (!expr)
13550 : : return NULL_TREE;
13551 : :
13552 : 6114516 : expr = c_fully_fold (expr, false, NULL);
13553 : :
13554 : 6114516 : if (warn_sequence_point)
13555 : 1286699 : verify_sequence_points (expr);
13556 : :
13557 : 6114516 : if (TREE_TYPE (expr) != error_mark_node
13558 : 6112009 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13559 : 6114516 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13560 : 0 : error_at (loc, "expression statement has incomplete type");
13561 : :
13562 : : /* If we're not processing a statement expression, warn about unused values.
13563 : : Warnings for statement expressions will be emitted later, once we figure
13564 : : out which is the result. */
13565 : 6114516 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13566 : 6114516 : && (warn_unused_value || warn_unused_result))
13567 : 6028132 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13568 : :
13569 : : exprv = expr;
13570 : 6152889 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13571 : 38374 : exprv = TREE_OPERAND (exprv, 1);
13572 : 6125454 : while (CONVERT_EXPR_P (exprv))
13573 : 10939 : exprv = TREE_OPERAND (exprv, 0);
13574 : 6114515 : if (DECL_P (exprv)
13575 : 6115062 : || handled_component_p (exprv)
13576 : 12209676 : || TREE_CODE (exprv) == ADDR_EXPR)
13577 : 19901 : mark_exp_read (exprv);
13578 : :
13579 : : /* If the expression is not of a type to which we cannot assign a line
13580 : : number, wrap the thing in a no-op NOP_EXPR. */
13581 : 6114515 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13582 : : {
13583 : 14608 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13584 : 14608 : SET_EXPR_LOCATION (expr, loc);
13585 : : }
13586 : :
13587 : : return expr;
13588 : : }
13589 : :
13590 : : /* Emit an expression as a statement. LOC is the location of the
13591 : : expression. */
13592 : :
13593 : : tree
13594 : 5829100 : c_finish_expr_stmt (location_t loc, tree expr)
13595 : : {
13596 : 5829100 : if (expr)
13597 : 5816863 : return add_stmt (c_process_expr_stmt (loc, expr));
13598 : : else
13599 : : return NULL;
13600 : : }
13601 : :
13602 : : /* Do the opposite and emit a statement as an expression. To begin,
13603 : : create a new binding level and return it. */
13604 : :
13605 : : tree
13606 : 34725 : c_begin_stmt_expr (void)
13607 : : {
13608 : 34725 : tree ret;
13609 : :
13610 : : /* We must force a BLOCK for this level so that, if it is not expanded
13611 : : later, there is a way to turn off the entire subtree of blocks that
13612 : : are contained in it. */
13613 : 34725 : keep_next_level ();
13614 : 34725 : ret = c_begin_compound_stmt (true);
13615 : :
13616 : 34725 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13617 : : ? NULL
13618 : : : c_switch_stack->bindings);
13619 : :
13620 : : /* Mark the current statement list as belonging to a statement list. */
13621 : 34725 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13622 : :
13623 : 34725 : return ret;
13624 : : }
13625 : :
13626 : : /* LOC is the location of the compound statement to which this body
13627 : : belongs. */
13628 : :
13629 : : tree
13630 : 34725 : c_finish_stmt_expr (location_t loc, tree body)
13631 : : {
13632 : 34725 : tree last, type, tmp, val;
13633 : 34725 : tree *last_p;
13634 : :
13635 : 34725 : body = c_end_compound_stmt (loc, body, true);
13636 : :
13637 : 34725 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13638 : : ? NULL
13639 : : : c_switch_stack->bindings);
13640 : :
13641 : : /* Locate the last statement in BODY. See c_end_compound_stmt
13642 : : about always returning a BIND_EXPR. */
13643 : 34725 : last_p = &BIND_EXPR_BODY (body);
13644 : 34725 : last = BIND_EXPR_BODY (body);
13645 : :
13646 : 34725 : continue_searching:
13647 : 34725 : if (TREE_CODE (last) == STATEMENT_LIST)
13648 : : {
13649 : 25933 : tree_stmt_iterator l = tsi_last (last);
13650 : :
13651 : 25939 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13652 : 6 : tsi_prev (&l);
13653 : :
13654 : : /* This can happen with degenerate cases like ({ }). No value. */
13655 : 25933 : if (tsi_end_p (l))
13656 : 389 : return body;
13657 : :
13658 : : /* If we're supposed to generate side effects warnings, process
13659 : : all of the statements except the last. */
13660 : 25544 : if (warn_unused_value || warn_unused_result)
13661 : : {
13662 : 25544 : for (tree_stmt_iterator i = tsi_start (last);
13663 : 275294 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
13664 : : {
13665 : 249750 : location_t tloc;
13666 : 249750 : tree t = tsi_stmt (i);
13667 : :
13668 : 249750 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
13669 : 249750 : emit_side_effect_warnings (tloc, t);
13670 : : }
13671 : : }
13672 : 25544 : last_p = tsi_stmt_ptr (l);
13673 : 25544 : last = *last_p;
13674 : : }
13675 : :
13676 : : /* If the end of the list is exception related, then the list was split
13677 : : by a call to push_cleanup. Continue searching. */
13678 : 34336 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
13679 : 34336 : || TREE_CODE (last) == TRY_CATCH_EXPR)
13680 : : {
13681 : 0 : last_p = &TREE_OPERAND (last, 0);
13682 : 0 : last = *last_p;
13683 : 0 : goto continue_searching;
13684 : : }
13685 : :
13686 : 34336 : if (last == error_mark_node)
13687 : : return last;
13688 : :
13689 : : /* In the case that the BIND_EXPR is not necessary, return the
13690 : : expression out from inside it. */
13691 : 34327 : if ((last == BIND_EXPR_BODY (body)
13692 : : /* Skip nested debug stmts. */
13693 : 25543 : || last == expr_first (BIND_EXPR_BODY (body)))
13694 : 45256 : && BIND_EXPR_VARS (body) == NULL)
13695 : : {
13696 : : /* Even if this looks constant, do not allow it in a constant
13697 : : expression. */
13698 : 10917 : last = c_wrap_maybe_const (last, true);
13699 : : /* Do not warn if the return value of a statement expression is
13700 : : unused. */
13701 : 10917 : suppress_warning (last, OPT_Wunused);
13702 : 10917 : return last;
13703 : : }
13704 : :
13705 : : /* Extract the type of said expression. */
13706 : 23410 : type = TREE_TYPE (last);
13707 : :
13708 : : /* If we're not returning a value at all, then the BIND_EXPR that
13709 : : we already have is a fine expression to return. */
13710 : 23410 : if (!type || VOID_TYPE_P (type))
13711 : : return body;
13712 : :
13713 : : /* Now that we've located the expression containing the value, it seems
13714 : : silly to make voidify_wrapper_expr repeat the process. Create a
13715 : : temporary of the appropriate type and stick it in a TARGET_EXPR. */
13716 : 18102 : tmp = create_tmp_var_raw (type);
13717 : :
13718 : : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
13719 : : tree_expr_nonnegative_p giving up immediately. */
13720 : 18102 : val = last;
13721 : 18102 : if (TREE_CODE (val) == NOP_EXPR
13722 : 18102 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
13723 : 3728 : val = TREE_OPERAND (val, 0);
13724 : :
13725 : 18102 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
13726 : 18102 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
13727 : :
13728 : 18102 : {
13729 : 18102 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
13730 : 18102 : SET_EXPR_LOCATION (t, loc);
13731 : 18102 : return t;
13732 : : }
13733 : : }
13734 : :
13735 : : /* Begin and end compound statements. This is as simple as pushing
13736 : : and popping new statement lists from the tree. */
13737 : :
13738 : : tree
13739 : 40130314 : c_begin_compound_stmt (bool do_scope)
13740 : : {
13741 : 40130314 : tree stmt = push_stmt_list ();
13742 : 40130314 : if (do_scope)
13743 : 40034339 : push_scope ();
13744 : 40130314 : return stmt;
13745 : : }
13746 : :
13747 : : /* End a compound statement. STMT is the statement. LOC is the
13748 : : location of the compound statement-- this is usually the location
13749 : : of the opening brace. */
13750 : :
13751 : : tree
13752 : 40130313 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
13753 : : {
13754 : 40130313 : tree block = NULL;
13755 : :
13756 : 40130313 : if (do_scope)
13757 : : {
13758 : 40034338 : if (c_dialect_objc ())
13759 : 0 : objc_clear_super_receiver ();
13760 : 40034338 : block = pop_scope ();
13761 : : }
13762 : :
13763 : 40130313 : stmt = pop_stmt_list (stmt);
13764 : 40130313 : stmt = c_build_bind_expr (loc, block, stmt);
13765 : :
13766 : : /* If this compound statement is nested immediately inside a statement
13767 : : expression, then force a BIND_EXPR to be created. Otherwise we'll
13768 : : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
13769 : : STATEMENT_LISTs merge, and thus we can lose track of what statement
13770 : : was really last. */
13771 : 80260626 : if (building_stmt_list_p ()
13772 : 40130228 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13773 : 40252847 : && TREE_CODE (stmt) != BIND_EXPR)
13774 : : {
13775 : 120794 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
13776 : 120794 : TREE_SIDE_EFFECTS (stmt) = 1;
13777 : 120794 : SET_EXPR_LOCATION (stmt, loc);
13778 : : }
13779 : :
13780 : 40130313 : return stmt;
13781 : : }
13782 : :
13783 : : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
13784 : : when the current scope is exited. EH_ONLY is true when this is not
13785 : : meant to apply to normal control flow transfer. */
13786 : :
13787 : : void
13788 : 95 : push_cleanup (tree decl, tree cleanup, bool eh_only)
13789 : : {
13790 : 95 : enum tree_code code;
13791 : 95 : tree stmt, list;
13792 : 95 : bool stmt_expr;
13793 : :
13794 : 95 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
13795 : 95 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
13796 : 95 : add_stmt (stmt);
13797 : 95 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
13798 : 95 : list = push_stmt_list ();
13799 : 95 : TREE_OPERAND (stmt, 0) = list;
13800 : 95 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
13801 : 95 : }
13802 : :
13803 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
13804 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
13805 : :
13806 : : static tree
13807 : 98689 : build_vec_cmp (tree_code code, tree type,
13808 : : tree arg0, tree arg1)
13809 : : {
13810 : 98689 : tree zero_vec = build_zero_cst (type);
13811 : 98689 : tree minus_one_vec = build_minus_one_cst (type);
13812 : 98689 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
13813 : 98689 : tree cmp = build2 (code, cmp_type, arg0, arg1);
13814 : 98689 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
13815 : : }
13816 : :
13817 : : /* Possibly warn about an address of OP never being NULL in a comparison
13818 : : operation CODE involving null. */
13819 : :
13820 : : static void
13821 : 50531 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
13822 : : {
13823 : : /* Prevent warnings issued for macro expansion. */
13824 : 50531 : if (!warn_address
13825 : 29781 : || warning_suppressed_p (op, OPT_Waddress)
13826 : 79988 : || from_macro_expansion_at (loc))
13827 : 23805 : return;
13828 : :
13829 : 26726 : if (TREE_CODE (op) == NOP_EXPR)
13830 : : {
13831 : : /* Allow casts to intptr_t to suppress the warning. */
13832 : 1133 : tree type = TREE_TYPE (op);
13833 : 1133 : if (TREE_CODE (type) == INTEGER_TYPE)
13834 : : return;
13835 : 1133 : op = TREE_OPERAND (op, 0);
13836 : : }
13837 : :
13838 : 26726 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
13839 : : {
13840 : : /* Allow a cast to void* to suppress the warning. */
13841 : 24 : tree type = TREE_TYPE (TREE_TYPE (op));
13842 : 24 : if (VOID_TYPE_P (type))
13843 : : return;
13844 : :
13845 : : /* Adding any value to a null pointer, including zero, is undefined
13846 : : in C. This includes the expression &p[0] where p is the null
13847 : : pointer, although &p[0] will have been folded to p by this point
13848 : : and so not diagnosed. */
13849 : 24 : if (code == EQ_EXPR)
13850 : 22 : warning_at (loc, OPT_Waddress,
13851 : : "the comparison will always evaluate as %<false%> "
13852 : : "for the pointer operand in %qE must not be NULL",
13853 : : op);
13854 : : else
13855 : 2 : warning_at (loc, OPT_Waddress,
13856 : : "the comparison will always evaluate as %<true%> "
13857 : : "for the pointer operand in %qE must not be NULL",
13858 : : op);
13859 : :
13860 : 24 : return;
13861 : : }
13862 : :
13863 : 26702 : if (TREE_CODE (op) != ADDR_EXPR)
13864 : : return;
13865 : :
13866 : 254 : op = TREE_OPERAND (op, 0);
13867 : :
13868 : 254 : if (TREE_CODE (op) == IMAGPART_EXPR
13869 : 254 : || TREE_CODE (op) == REALPART_EXPR)
13870 : : {
13871 : : /* The address of either complex part may not be null. */
13872 : 14 : if (code == EQ_EXPR)
13873 : 9 : warning_at (loc, OPT_Waddress,
13874 : : "the comparison will always evaluate as %<false%> "
13875 : : "for the address of %qE will never be NULL",
13876 : : op);
13877 : : else
13878 : 5 : warning_at (loc, OPT_Waddress,
13879 : : "the comparison will always evaluate as %<true%> "
13880 : : "for the address of %qE will never be NULL",
13881 : : op);
13882 : 14 : return;
13883 : : }
13884 : :
13885 : : /* Set to true in the loop below if OP dereferences is operand.
13886 : : In such a case the ultimate target need not be a decl for
13887 : : the null [in]equality test to be constant. */
13888 : : bool deref = false;
13889 : :
13890 : : /* Get the outermost array or object, or member. */
13891 : 294 : while (handled_component_p (op))
13892 : : {
13893 : 72 : if (TREE_CODE (op) == COMPONENT_REF)
13894 : : {
13895 : : /* Get the member (its address is never null). */
13896 : 18 : op = TREE_OPERAND (op, 1);
13897 : 18 : break;
13898 : : }
13899 : :
13900 : : /* Get the outer array/object to refer to in the warning. */
13901 : 54 : op = TREE_OPERAND (op, 0);
13902 : 54 : deref = true;
13903 : : }
13904 : :
13905 : 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
13906 : 371 : || from_macro_expansion_at (loc))
13907 : 109 : return;
13908 : :
13909 : 131 : bool w;
13910 : 131 : if (code == EQ_EXPR)
13911 : 93 : w = warning_at (loc, OPT_Waddress,
13912 : : "the comparison will always evaluate as %<false%> "
13913 : : "for the address of %qE will never be NULL",
13914 : : op);
13915 : : else
13916 : 38 : w = warning_at (loc, OPT_Waddress,
13917 : : "the comparison will always evaluate as %<true%> "
13918 : : "for the address of %qE will never be NULL",
13919 : : op);
13920 : :
13921 : 131 : if (w && DECL_P (op))
13922 : 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
13923 : : }
13924 : :
13925 : : /* Build a binary-operation expression without default conversions.
13926 : : CODE is the kind of expression to build.
13927 : : LOCATION is the operator's location.
13928 : : This function differs from `build' in several ways:
13929 : : the data type of the result is computed and recorded in it,
13930 : : warnings are generated if arg data types are invalid,
13931 : : special handling for addition and subtraction of pointers is known,
13932 : : and some optimization is done (operations on narrow ints
13933 : : are done in the narrower type when that gives the same result).
13934 : : Constant folding is also done before the result is returned.
13935 : :
13936 : : Note that the operands will never have enumeral types, or function
13937 : : or array types, because either they will have the default conversions
13938 : : performed or they have both just been converted to some other type in which
13939 : : the arithmetic is to be done. */
13940 : :
13941 : : tree
13942 : 16440127 : build_binary_op (location_t location, enum tree_code code,
13943 : : tree orig_op0, tree orig_op1, bool convert_p)
13944 : : {
13945 : 16440127 : tree type0, type1, orig_type0, orig_type1;
13946 : 16440127 : tree eptype;
13947 : 16440127 : enum tree_code code0, code1;
13948 : 16440127 : tree op0, op1;
13949 : 16440127 : tree ret = error_mark_node;
13950 : 16440127 : const char *invalid_op_diag;
13951 : 16440127 : bool op0_int_operands, op1_int_operands;
13952 : 16440127 : bool int_const, int_const_or_overflow, int_operands;
13953 : :
13954 : : /* Expression code to give to the expression when it is built.
13955 : : Normally this is CODE, which is what the caller asked for,
13956 : : but in some special cases we change it. */
13957 : 16440127 : enum tree_code resultcode = code;
13958 : :
13959 : : /* Data type in which the computation is to be performed.
13960 : : In the simplest cases this is the common type of the arguments. */
13961 : 16440127 : tree result_type = NULL;
13962 : :
13963 : : /* When the computation is in excess precision, the type of the
13964 : : final EXCESS_PRECISION_EXPR. */
13965 : 16440127 : tree semantic_result_type = NULL;
13966 : :
13967 : : /* Nonzero means operands have already been type-converted
13968 : : in whatever way is necessary.
13969 : : Zero means they need to be converted to RESULT_TYPE. */
13970 : 16440127 : int converted = 0;
13971 : :
13972 : : /* Nonzero means create the expression with this type, rather than
13973 : : RESULT_TYPE. */
13974 : 16440127 : tree build_type = NULL_TREE;
13975 : :
13976 : : /* Nonzero means after finally constructing the expression
13977 : : convert it to this type. */
13978 : 16440127 : tree final_type = NULL_TREE;
13979 : :
13980 : : /* Nonzero if this is an operation like MIN or MAX which can
13981 : : safely be computed in short if both args are promoted shorts.
13982 : : Also implies COMMON.
13983 : : -1 indicates a bitwise operation; this makes a difference
13984 : : in the exact conditions for when it is safe to do the operation
13985 : : in a narrower mode. */
13986 : 16440127 : int shorten = 0;
13987 : :
13988 : : /* Nonzero if this is a comparison operation;
13989 : : if both args are promoted shorts, compare the original shorts.
13990 : : Also implies COMMON. */
13991 : 16440127 : int short_compare = 0;
13992 : :
13993 : : /* Nonzero if this is a right-shift operation, which can be computed on the
13994 : : original short and then promoted if the operand is a promoted short. */
13995 : 16440127 : int short_shift = 0;
13996 : :
13997 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
13998 : 16440127 : int common = 0;
13999 : :
14000 : : /* True means types are compatible as far as ObjC is concerned. */
14001 : 16440127 : bool objc_ok;
14002 : :
14003 : : /* True means this is an arithmetic operation that may need excess
14004 : : precision. */
14005 : 16440127 : bool may_need_excess_precision;
14006 : :
14007 : : /* True means this is a boolean operation that converts both its
14008 : : operands to truth-values. */
14009 : 16440127 : bool boolean_op = false;
14010 : :
14011 : : /* Remember whether we're doing / or %. */
14012 : 16440127 : bool doing_div_or_mod = false;
14013 : :
14014 : : /* Remember whether we're doing << or >>. */
14015 : 16440127 : bool doing_shift = false;
14016 : :
14017 : : /* Tree holding instrumentation expression. */
14018 : 16440127 : tree instrument_expr = NULL;
14019 : :
14020 : 16440127 : if (location == UNKNOWN_LOCATION)
14021 : 79 : location = input_location;
14022 : :
14023 : 16440127 : op0 = orig_op0;
14024 : 16440127 : op1 = orig_op1;
14025 : :
14026 : 16440127 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14027 : 7937437 : if (op0_int_operands)
14028 : 7937437 : op0 = remove_c_maybe_const_expr (op0);
14029 : 16440127 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14030 : 11139592 : if (op1_int_operands)
14031 : 11139592 : op1 = remove_c_maybe_const_expr (op1);
14032 : 27579719 : int_operands = (op0_int_operands && op1_int_operands);
14033 : 11139592 : if (int_operands)
14034 : : {
14035 : 15528731 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14036 : 7779532 : && TREE_CODE (orig_op1) == INTEGER_CST);
14037 : 7749199 : int_const = (int_const_or_overflow
14038 : 7749199 : && !TREE_OVERFLOW (orig_op0)
14039 : 7749130 : && !TREE_OVERFLOW (orig_op1));
14040 : : }
14041 : : else
14042 : : int_const = int_const_or_overflow = false;
14043 : :
14044 : : /* Do not apply default conversion in mixed vector/scalar expression. */
14045 : 16440127 : if (convert_p
14046 : 16440127 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14047 : : {
14048 : 9929124 : op0 = default_conversion (op0);
14049 : 9929124 : op1 = default_conversion (op1);
14050 : : }
14051 : :
14052 : 16440127 : orig_type0 = type0 = TREE_TYPE (op0);
14053 : :
14054 : 16440127 : orig_type1 = type1 = TREE_TYPE (op1);
14055 : :
14056 : : /* The expression codes of the data types of the arguments tell us
14057 : : whether the arguments are integers, floating, pointers, etc. */
14058 : 16440127 : code0 = TREE_CODE (type0);
14059 : 16440127 : code1 = TREE_CODE (type1);
14060 : :
14061 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14062 : 16440129 : STRIP_TYPE_NOPS (op0);
14063 : 16440130 : STRIP_TYPE_NOPS (op1);
14064 : :
14065 : : /* If an error was already reported for one of the arguments,
14066 : : avoid reporting another error. */
14067 : :
14068 : 16440127 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14069 : 708 : return error_mark_node;
14070 : :
14071 : 16439419 : if (code0 == POINTER_TYPE
14072 : 16439419 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14073 : 11 : return error_mark_node;
14074 : :
14075 : 16439408 : if (code1 == POINTER_TYPE
14076 : 16439408 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14077 : 11 : return error_mark_node;
14078 : :
14079 : 32878794 : if ((invalid_op_diag
14080 : 16439397 : = targetm.invalid_binary_op (code, type0, type1)))
14081 : : {
14082 : 0 : error_at (location, invalid_op_diag);
14083 : 0 : return error_mark_node;
14084 : : }
14085 : :
14086 : 16439397 : switch (code)
14087 : : {
14088 : : case PLUS_EXPR:
14089 : : case MINUS_EXPR:
14090 : : case MULT_EXPR:
14091 : : case TRUNC_DIV_EXPR:
14092 : : case CEIL_DIV_EXPR:
14093 : : case FLOOR_DIV_EXPR:
14094 : : case ROUND_DIV_EXPR:
14095 : : case EXACT_DIV_EXPR:
14096 : : may_need_excess_precision = true;
14097 : : break;
14098 : :
14099 : 2679173 : case EQ_EXPR:
14100 : 2679173 : case NE_EXPR:
14101 : 2679173 : case LE_EXPR:
14102 : 2679173 : case GE_EXPR:
14103 : 2679173 : case LT_EXPR:
14104 : 2679173 : case GT_EXPR:
14105 : : /* Excess precision for implicit conversions of integers to
14106 : : floating point in C11 and later. */
14107 : 2679173 : may_need_excess_precision = (flag_isoc11
14108 : 2679173 : && (ANY_INTEGRAL_TYPE_P (type0)
14109 : 470787 : || ANY_INTEGRAL_TYPE_P (type1)));
14110 : : break;
14111 : :
14112 : : default:
14113 : 16439397 : may_need_excess_precision = false;
14114 : : break;
14115 : : }
14116 : 16439397 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14117 : : {
14118 : 184 : op0 = TREE_OPERAND (op0, 0);
14119 : 184 : type0 = TREE_TYPE (op0);
14120 : : }
14121 : 16439213 : else if (may_need_excess_precision
14122 : 16439213 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14123 : : {
14124 : 771 : type0 = eptype;
14125 : 771 : op0 = convert (eptype, op0);
14126 : : }
14127 : 16439397 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14128 : : {
14129 : 887 : op1 = TREE_OPERAND (op1, 0);
14130 : 887 : type1 = TREE_TYPE (op1);
14131 : : }
14132 : 16438510 : else if (may_need_excess_precision
14133 : 16438510 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14134 : : {
14135 : 592 : type1 = eptype;
14136 : 592 : op1 = convert (eptype, op1);
14137 : : }
14138 : :
14139 : 16439397 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14140 : :
14141 : : /* In case when one of the operands of the binary operation is
14142 : : a vector and another is a scalar -- convert scalar to vector. */
14143 : 17981060 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14144 : 17979240 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14145 : : {
14146 : 2423 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14147 : : orig_op1, true);
14148 : :
14149 : 2423 : switch (convert_flag)
14150 : : {
14151 : 12 : case stv_error:
14152 : 12 : return error_mark_node;
14153 : 590 : case stv_firstarg:
14154 : 590 : {
14155 : 590 : bool maybe_const = true;
14156 : 590 : tree sc;
14157 : 590 : sc = c_fully_fold (op0, false, &maybe_const);
14158 : 590 : sc = save_expr (sc);
14159 : 590 : sc = convert (TREE_TYPE (type1), sc);
14160 : 590 : op0 = build_vector_from_val (type1, sc);
14161 : 590 : if (!maybe_const)
14162 : 93 : op0 = c_wrap_maybe_const (op0, true);
14163 : 590 : orig_type0 = type0 = TREE_TYPE (op0);
14164 : 590 : code0 = TREE_CODE (type0);
14165 : 590 : converted = 1;
14166 : 590 : break;
14167 : : }
14168 : 998 : case stv_secondarg:
14169 : 998 : {
14170 : 998 : bool maybe_const = true;
14171 : 998 : tree sc;
14172 : 998 : sc = c_fully_fold (op1, false, &maybe_const);
14173 : 998 : sc = save_expr (sc);
14174 : 998 : sc = convert (TREE_TYPE (type0), sc);
14175 : 998 : op1 = build_vector_from_val (type0, sc);
14176 : 998 : if (!maybe_const)
14177 : 36 : op1 = c_wrap_maybe_const (op1, true);
14178 : 998 : orig_type1 = type1 = TREE_TYPE (op1);
14179 : 998 : code1 = TREE_CODE (type1);
14180 : 998 : converted = 1;
14181 : 998 : break;
14182 : : }
14183 : : default:
14184 : : break;
14185 : : }
14186 : : }
14187 : :
14188 : 16439385 : switch (code)
14189 : : {
14190 : 8415052 : case PLUS_EXPR:
14191 : : /* Handle the pointer + int case. */
14192 : 8415052 : if (code0 == POINTER_TYPE
14193 : 1218751 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14194 : : {
14195 : 1218745 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14196 : 1218745 : goto return_build_binary_op;
14197 : : }
14198 : 7196307 : else if (code1 == POINTER_TYPE
14199 : 1530 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14200 : : {
14201 : 1524 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14202 : 1524 : goto return_build_binary_op;
14203 : : }
14204 : : else
14205 : : common = 1;
14206 : : break;
14207 : :
14208 : 780854 : case MINUS_EXPR:
14209 : : /* Subtraction of two similar pointers.
14210 : : We must subtract them as integers, then divide by object size. */
14211 : 780854 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14212 : 780854 : && comp_target_types (location, type0, type1))
14213 : : {
14214 : 3639 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14215 : 3639 : goto return_build_binary_op;
14216 : : }
14217 : : /* Handle pointer minus int. Just like pointer plus int. */
14218 : 777215 : else if (code0 == POINTER_TYPE
14219 : 16663 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14220 : : {
14221 : 16651 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14222 : 16651 : goto return_build_binary_op;
14223 : : }
14224 : : else
14225 : : common = 1;
14226 : : break;
14227 : :
14228 : : case MULT_EXPR:
14229 : : common = 1;
14230 : : break;
14231 : :
14232 : 263118 : case TRUNC_DIV_EXPR:
14233 : 263118 : case CEIL_DIV_EXPR:
14234 : 263118 : case FLOOR_DIV_EXPR:
14235 : 263118 : case ROUND_DIV_EXPR:
14236 : 263118 : case EXACT_DIV_EXPR:
14237 : 263118 : doing_div_or_mod = true;
14238 : 263118 : warn_for_div_by_zero (location, op1);
14239 : :
14240 : 263117 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14241 : 48530 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14242 : 46552 : || code0 == COMPLEX_TYPE
14243 : 44504 : || gnu_vector_type_p (type0))
14244 : 311646 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14245 : 46104 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14246 : 45965 : || code1 == COMPLEX_TYPE
14247 : 44506 : || gnu_vector_type_p (type1)))
14248 : : {
14249 : 263113 : enum tree_code tcode0 = code0, tcode1 = code1;
14250 : :
14251 : 263113 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14252 : 46551 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14253 : 263113 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14254 : 45962 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14255 : :
14256 : 263113 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
14257 : 195365 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
14258 : 79777 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14259 : : resultcode = RDIV_EXPR;
14260 : : else
14261 : : /* Although it would be tempting to shorten always here, that
14262 : : loses on some targets, since the modulo instruction is
14263 : : undefined if the quotient can't be represented in the
14264 : : computation mode. We shorten only if unsigned or if
14265 : : dividing by something we know != -1. */
14266 : 183336 : shorten = may_shorten_divmod (op0, op1);
14267 : : common = 1;
14268 : : }
14269 : : break;
14270 : :
14271 : 1539184 : case BIT_AND_EXPR:
14272 : 1539184 : case BIT_IOR_EXPR:
14273 : 1539184 : case BIT_XOR_EXPR:
14274 : 1539184 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14275 : 1016760 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14276 : : shorten = -1;
14277 : : /* Allow vector types which are not floating point types. */
14278 : 522460 : else if (gnu_vector_type_p (type0)
14279 : 522383 : && gnu_vector_type_p (type1)
14280 : 522383 : && !VECTOR_FLOAT_TYPE_P (type0)
14281 : 1044840 : && !VECTOR_FLOAT_TYPE_P (type1))
14282 : : common = 1;
14283 : : break;
14284 : :
14285 : 56486 : case TRUNC_MOD_EXPR:
14286 : 56486 : case FLOOR_MOD_EXPR:
14287 : 56486 : doing_div_or_mod = true;
14288 : 56486 : warn_for_div_by_zero (location, op1);
14289 : :
14290 : 56486 : if (gnu_vector_type_p (type0)
14291 : 297 : && gnu_vector_type_p (type1)
14292 : 297 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14293 : 56783 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14294 : : common = 1;
14295 : 56189 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14296 : 56188 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14297 : : {
14298 : : /* Although it would be tempting to shorten always here, that loses
14299 : : on some targets, since the modulo instruction is undefined if the
14300 : : quotient can't be represented in the computation mode. We shorten
14301 : : only if unsigned or if dividing by something we know != -1. */
14302 : 56188 : shorten = may_shorten_divmod (op0, op1);
14303 : 56188 : common = 1;
14304 : : }
14305 : : break;
14306 : :
14307 : 502592 : case TRUTH_ANDIF_EXPR:
14308 : 502592 : case TRUTH_ORIF_EXPR:
14309 : 502592 : case TRUTH_AND_EXPR:
14310 : 502592 : case TRUTH_OR_EXPR:
14311 : 502592 : case TRUTH_XOR_EXPR:
14312 : 502592 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14313 : 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14314 : 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14315 : 0 : || code0 == BITINT_TYPE)
14316 : 502592 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14317 : 159 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14318 : 20 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14319 : 18 : || code1 == BITINT_TYPE))
14320 : : {
14321 : : /* Result of these operations is always an int,
14322 : : but that does not mean the operands should be
14323 : : converted to ints! */
14324 : 502592 : result_type = integer_type_node;
14325 : 502592 : if (op0_int_operands)
14326 : : {
14327 : 110756 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14328 : 110756 : op0 = remove_c_maybe_const_expr (op0);
14329 : : }
14330 : : else
14331 : 391836 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14332 : 502592 : if (op1_int_operands)
14333 : : {
14334 : 72265 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14335 : 72265 : op1 = remove_c_maybe_const_expr (op1);
14336 : : }
14337 : : else
14338 : 430327 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14339 : : converted = 1;
14340 : : boolean_op = true;
14341 : : }
14342 : 502592 : if (code == TRUTH_ANDIF_EXPR)
14343 : : {
14344 : 245392 : int_const_or_overflow = (int_operands
14345 : 64141 : && TREE_CODE (orig_op0) == INTEGER_CST
14346 : 245408 : && (op0 == truthvalue_false_node
14347 : 13844 : || TREE_CODE (orig_op1) == INTEGER_CST));
14348 : : int_const = (int_const_or_overflow
14349 : 64114 : && !TREE_OVERFLOW (orig_op0)
14350 : 64114 : && (op0 == truthvalue_false_node
14351 : 13828 : || !TREE_OVERFLOW (orig_op1)));
14352 : : }
14353 : 321314 : else if (code == TRUTH_ORIF_EXPR)
14354 : : {
14355 : 327979 : int_const_or_overflow = (int_operands
14356 : 6816 : && TREE_CODE (orig_op0) == INTEGER_CST
14357 : 328002 : && (op0 == truthvalue_true_node
14358 : 2133 : || TREE_CODE (orig_op1) == INTEGER_CST));
14359 : : int_const = (int_const_or_overflow
14360 : 6786 : && !TREE_OVERFLOW (orig_op0)
14361 : 6786 : && (op0 == truthvalue_true_node
14362 : 2110 : || !TREE_OVERFLOW (orig_op1)));
14363 : : }
14364 : : break;
14365 : :
14366 : : /* Shift operations: result has same type as first operand;
14367 : : always convert second operand to int.
14368 : : Also set SHORT_SHIFT if shifting rightward. */
14369 : :
14370 : 226557 : case RSHIFT_EXPR:
14371 : 226557 : if (gnu_vector_type_p (type0)
14372 : 795 : && gnu_vector_type_p (type1)
14373 : 257 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14374 : 255 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14375 : 226812 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14376 : : TYPE_VECTOR_SUBPARTS (type1)))
14377 : : {
14378 : : result_type = type0;
14379 : : converted = 1;
14380 : : }
14381 : 226304 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14382 : 1070 : || code0 == BITINT_TYPE
14383 : 546 : || (gnu_vector_type_p (type0)
14384 : 542 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14385 : 227367 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14386 : : {
14387 : 226295 : doing_shift = true;
14388 : 226295 : if (TREE_CODE (op1) == INTEGER_CST)
14389 : : {
14390 : 197443 : if (tree_int_cst_sgn (op1) < 0)
14391 : : {
14392 : 269 : int_const = false;
14393 : 269 : if (c_inhibit_evaluation_warnings == 0)
14394 : 129 : warning_at (location, OPT_Wshift_count_negative,
14395 : : "right shift count is negative");
14396 : : }
14397 : 197174 : else if (code0 == VECTOR_TYPE)
14398 : : {
14399 : 454 : if (compare_tree_int (op1,
14400 : 454 : TYPE_PRECISION (TREE_TYPE (type0)))
14401 : : >= 0)
14402 : : {
14403 : 16 : int_const = false;
14404 : 16 : if (c_inhibit_evaluation_warnings == 0)
14405 : 16 : warning_at (location, OPT_Wshift_count_overflow,
14406 : : "right shift count >= width of vector element");
14407 : : }
14408 : : }
14409 : : else
14410 : : {
14411 : 196720 : if (!integer_zerop (op1))
14412 : 196362 : short_shift = 1;
14413 : :
14414 : 196720 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14415 : : {
14416 : 584 : int_const = false;
14417 : 584 : if (c_inhibit_evaluation_warnings == 0)
14418 : 50 : warning_at (location, OPT_Wshift_count_overflow,
14419 : : "right shift count >= width of type");
14420 : : }
14421 : : }
14422 : : }
14423 : :
14424 : : /* Use the type of the value to be shifted. */
14425 : : result_type = type0;
14426 : : /* Avoid converting op1 to result_type later. */
14427 : : converted = 1;
14428 : : }
14429 : : break;
14430 : :
14431 : 783328 : case LSHIFT_EXPR:
14432 : 783328 : if (gnu_vector_type_p (type0)
14433 : 595 : && gnu_vector_type_p (type1)
14434 : 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14435 : 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14436 : 783641 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14437 : : TYPE_VECTOR_SUBPARTS (type1)))
14438 : : {
14439 : : result_type = type0;
14440 : : converted = 1;
14441 : : }
14442 : 783017 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14443 : 749 : || code0 == BITINT_TYPE
14444 : 290 : || (gnu_vector_type_p (type0)
14445 : 284 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14446 : 783757 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14447 : : {
14448 : 783003 : doing_shift = true;
14449 : 783003 : if (TREE_CODE (op0) == INTEGER_CST
14450 : 427326 : && tree_int_cst_sgn (op0) < 0
14451 : 783752 : && !TYPE_OVERFLOW_WRAPS (type0))
14452 : : {
14453 : : /* Don't reject a left shift of a negative value in a context
14454 : : where a constant expression is needed in C90. */
14455 : 712 : if (flag_isoc99)
14456 : 666 : int_const = false;
14457 : 712 : if (c_inhibit_evaluation_warnings == 0)
14458 : 712 : warning_at (location, OPT_Wshift_negative_value,
14459 : : "left shift of negative value");
14460 : : }
14461 : 783003 : if (TREE_CODE (op1) == INTEGER_CST)
14462 : : {
14463 : 713611 : if (tree_int_cst_sgn (op1) < 0)
14464 : : {
14465 : 313 : int_const = false;
14466 : 313 : if (c_inhibit_evaluation_warnings == 0)
14467 : 135 : warning_at (location, OPT_Wshift_count_negative,
14468 : : "left shift count is negative");
14469 : : }
14470 : 713298 : else if (code0 == VECTOR_TYPE)
14471 : : {
14472 : 215 : if (compare_tree_int (op1,
14473 : 215 : TYPE_PRECISION (TREE_TYPE (type0)))
14474 : : >= 0)
14475 : : {
14476 : 6 : int_const = false;
14477 : 6 : if (c_inhibit_evaluation_warnings == 0)
14478 : 6 : warning_at (location, OPT_Wshift_count_overflow,
14479 : : "left shift count >= width of vector element");
14480 : : }
14481 : : }
14482 : 713083 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14483 : : {
14484 : 29731 : int_const = false;
14485 : 29731 : if (c_inhibit_evaluation_warnings == 0)
14486 : 111 : warning_at (location, OPT_Wshift_count_overflow,
14487 : : "left shift count >= width of type");
14488 : : }
14489 : 683352 : else if (TREE_CODE (op0) == INTEGER_CST
14490 : 356002 : && maybe_warn_shift_overflow (location, op0, op1)
14491 : 683980 : && flag_isoc99)
14492 : : int_const = false;
14493 : : }
14494 : :
14495 : : /* Use the type of the value to be shifted. */
14496 : : result_type = type0;
14497 : : /* Avoid converting op1 to result_type later. */
14498 : : converted = 1;
14499 : : }
14500 : : break;
14501 : :
14502 : 1706207 : case EQ_EXPR:
14503 : 1706207 : case NE_EXPR:
14504 : 1706207 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14505 : : {
14506 : 40893 : tree intt;
14507 : 40893 : if (!vector_types_compatible_elements_p (type0, type1))
14508 : : {
14509 : 4 : error_at (location, "comparing vectors with different "
14510 : : "element types");
14511 : 4 : return error_mark_node;
14512 : : }
14513 : :
14514 : 40889 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14515 : 81778 : TYPE_VECTOR_SUBPARTS (type1)))
14516 : : {
14517 : 1 : error_at (location, "comparing vectors with different "
14518 : : "number of elements");
14519 : 1 : return error_mark_node;
14520 : : }
14521 : :
14522 : : /* It's not precisely specified how the usual arithmetic
14523 : : conversions apply to the vector types. Here, we use
14524 : : the unsigned type if one of the operands is signed and
14525 : : the other one is unsigned. */
14526 : 40888 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14527 : : {
14528 : 4 : if (!TYPE_UNSIGNED (type0))
14529 : 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14530 : : else
14531 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14532 : 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14533 : : "types %qT and %qT", type0, type1);
14534 : : }
14535 : :
14536 : : /* Always construct signed integer vector type. */
14537 : 122664 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14538 : 81776 : (SCALAR_TYPE_MODE
14539 : : (TREE_TYPE (type0))), 0);
14540 : 40888 : if (!intt)
14541 : : {
14542 : 0 : error_at (location, "could not find an integer type "
14543 : : "of the same size as %qT",
14544 : 0 : TREE_TYPE (type0));
14545 : 0 : return error_mark_node;
14546 : : }
14547 : 40888 : result_type = build_opaque_vector_type (intt,
14548 : 40888 : TYPE_VECTOR_SUBPARTS (type0));
14549 : 40888 : converted = 1;
14550 : 40888 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14551 : 40888 : goto return_build_binary_op;
14552 : : }
14553 : 1665314 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14554 : 245309 : warning_at (location,
14555 : 245309 : OPT_Wfloat_equal,
14556 : : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14557 : : /* Result of comparison is always int,
14558 : : but don't convert the args to int! */
14559 : 1665314 : build_type = integer_type_node;
14560 : 1665314 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14561 : 155057 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14562 : 1545554 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14563 : : || code1 == BITINT_TYPE
14564 : : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14565 : : short_compare = 1;
14566 : 120023 : else if (code0 == POINTER_TYPE
14567 : 120023 : && (code1 == NULLPTR_TYPE
14568 : 119678 : || null_pointer_constant_p (orig_op1)))
14569 : : {
14570 : 50247 : maybe_warn_for_null_address (location, op0, code);
14571 : 50247 : result_type = type0;
14572 : : }
14573 : 69776 : else if (code1 == POINTER_TYPE
14574 : 69776 : && (code0 == NULLPTR_TYPE
14575 : 69674 : || null_pointer_constant_p (orig_op0)))
14576 : : {
14577 : 284 : maybe_warn_for_null_address (location, op1, code);
14578 : 284 : result_type = type1;
14579 : : }
14580 : 69492 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14581 : : {
14582 : 69402 : tree tt0 = TREE_TYPE (type0);
14583 : 69402 : tree tt1 = TREE_TYPE (type1);
14584 : 69402 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14585 : 69402 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14586 : 69402 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14587 : :
14588 : : /* Anything compares with void *. void * compares with anything.
14589 : : Otherwise, the targets must be compatible
14590 : : and both must be object or both incomplete. */
14591 : 69402 : if (comp_target_types (location, type0, type1))
14592 : 48119 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14593 : 21283 : else if (!addr_space_superset (as0, as1, &as_common))
14594 : : {
14595 : 0 : error_at (location, "comparison of pointers to "
14596 : : "disjoint address spaces");
14597 : 0 : return error_mark_node;
14598 : : }
14599 : 21283 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14600 : : {
14601 : 21086 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14602 : 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14603 : : "comparison of %<void *%> with function pointer");
14604 : : }
14605 : 197 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14606 : : {
14607 : 149 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14608 : 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14609 : : "comparison of %<void *%> with function pointer");
14610 : : }
14611 : : else
14612 : : /* Avoid warning about the volatile ObjC EH puts on decls. */
14613 : 48 : if (!objc_ok)
14614 : 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14615 : : "comparison of distinct pointer types lacks a cast");
14616 : :
14617 : 48175 : if (result_type == NULL_TREE)
14618 : : {
14619 : 21283 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14620 : 21283 : result_type = c_build_pointer_type
14621 : 21283 : (c_build_qualified_type (void_type_node, qual));
14622 : : }
14623 : : }
14624 : 90 : else if (code0 == POINTER_TYPE
14625 : 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14626 : : {
14627 : 26 : result_type = type0;
14628 : 26 : pedwarn (location, 0, "comparison between pointer and integer");
14629 : : }
14630 : 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14631 : 21 : && code1 == POINTER_TYPE)
14632 : : {
14633 : 8 : result_type = type1;
14634 : 8 : pedwarn (location, 0, "comparison between pointer and integer");
14635 : : }
14636 : : /* 6.5.9: One of the following shall hold:
14637 : : -- both operands have type nullptr_t; */
14638 : 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14639 : : {
14640 : 22 : result_type = nullptr_type_node;
14641 : : /* No need to convert the operands to result_type later. */
14642 : 22 : converted = 1;
14643 : : }
14644 : : /* -- one operand has type nullptr_t and the other is a null pointer
14645 : : constant. We will have to convert the former to the type of the
14646 : : latter, because during gimplification we can't have mismatching
14647 : : comparison operand type. We convert from nullptr_t to the other
14648 : : type, since only nullptr_t can be converted to nullptr_t. Also,
14649 : : even a constant 0 is a null pointer constant, so we may have to
14650 : : create a pointer type from its type. */
14651 : 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
14652 : 19 : result_type = (INTEGRAL_TYPE_P (type1)
14653 : 19 : ? c_build_pointer_type (type1) : type1);
14654 : 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
14655 : 11 : result_type = (INTEGRAL_TYPE_P (type0)
14656 : 11 : ? c_build_pointer_type (type0) : type0);
14657 : 4922393 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14658 : 3257054 : || truth_value_p (TREE_CODE (orig_op0)))
14659 : 3323368 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14660 : 3323364 : || truth_value_p (TREE_CODE (orig_op1))))
14661 : 70595 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14662 : : break;
14663 : :
14664 : 972962 : case LE_EXPR:
14665 : 972962 : case GE_EXPR:
14666 : 972962 : case LT_EXPR:
14667 : 972962 : case GT_EXPR:
14668 : 972962 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14669 : : {
14670 : 57806 : tree intt;
14671 : 57806 : if (!vector_types_compatible_elements_p (type0, type1))
14672 : : {
14673 : 5 : error_at (location, "comparing vectors with different "
14674 : : "element types");
14675 : 5 : return error_mark_node;
14676 : : }
14677 : :
14678 : 57801 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14679 : 115602 : TYPE_VECTOR_SUBPARTS (type1)))
14680 : : {
14681 : 0 : error_at (location, "comparing vectors with different "
14682 : : "number of elements");
14683 : 0 : return error_mark_node;
14684 : : }
14685 : :
14686 : : /* It's not precisely specified how the usual arithmetic
14687 : : conversions apply to the vector types. Here, we use
14688 : : the unsigned type if one of the operands is signed and
14689 : : the other one is unsigned. */
14690 : 57801 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14691 : : {
14692 : 15 : if (!TYPE_UNSIGNED (type0))
14693 : 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14694 : : else
14695 : 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14696 : 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
14697 : : "types %qT and %qT", type0, type1);
14698 : : }
14699 : :
14700 : : /* Always construct signed integer vector type. */
14701 : 173403 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14702 : 115602 : (SCALAR_TYPE_MODE
14703 : : (TREE_TYPE (type0))), 0);
14704 : 57801 : if (!intt)
14705 : : {
14706 : 0 : error_at (location, "could not find an integer type "
14707 : : "of the same size as %qT",
14708 : 0 : TREE_TYPE (type0));
14709 : 0 : return error_mark_node;
14710 : : }
14711 : 57801 : result_type = build_opaque_vector_type (intt,
14712 : 57801 : TYPE_VECTOR_SUBPARTS (type0));
14713 : 57801 : converted = 1;
14714 : 57801 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14715 : 57801 : goto return_build_binary_op;
14716 : : }
14717 : 915156 : build_type = integer_type_node;
14718 : 915156 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14719 : 63708 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
14720 : 851985 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14721 : : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
14722 : : short_compare = 1;
14723 : 63203 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14724 : : {
14725 : 63136 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
14726 : 63136 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
14727 : 63136 : addr_space_t as_common;
14728 : :
14729 : 63136 : if (comp_target_types (location, type0, type1))
14730 : : {
14731 : 63023 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14732 : 63023 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
14733 : 63023 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
14734 : 32 : pedwarn_c99 (location, OPT_Wpedantic,
14735 : : "comparison of complete and incomplete pointers");
14736 : 62991 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
14737 : 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14738 : : "ordered comparisons of pointers to functions");
14739 : 62991 : else if (null_pointer_constant_p (orig_op0)
14740 : 62991 : || null_pointer_constant_p (orig_op1))
14741 : 8 : warning_at (location, OPT_Wextra,
14742 : : "ordered comparison of pointer with null pointer");
14743 : :
14744 : : }
14745 : 113 : else if (!addr_space_superset (as0, as1, &as_common))
14746 : : {
14747 : 0 : error_at (location, "comparison of pointers to "
14748 : : "disjoint address spaces");
14749 : 0 : return error_mark_node;
14750 : : }
14751 : : else
14752 : : {
14753 : 113 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14754 : 113 : result_type = c_build_pointer_type
14755 : 113 : (c_build_qualified_type (void_type_node, qual));
14756 : 113 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14757 : : "comparison of distinct pointer types lacks a cast");
14758 : : }
14759 : : }
14760 : 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
14761 : : {
14762 : 17 : result_type = type0;
14763 : 17 : if (pedantic)
14764 : 11 : pedwarn (location, OPT_Wpedantic,
14765 : : "ordered comparison of pointer with integer zero");
14766 : 6 : else if (extra_warnings)
14767 : 1 : warning_at (location, OPT_Wextra,
14768 : : "ordered comparison of pointer with integer zero");
14769 : : }
14770 : 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
14771 : : {
14772 : 12 : result_type = type1;
14773 : 12 : if (pedantic)
14774 : 2 : pedwarn (location, OPT_Wpedantic,
14775 : : "ordered comparison of pointer with integer zero");
14776 : 10 : else if (extra_warnings)
14777 : 1 : warning_at (location, OPT_Wextra,
14778 : : "ordered comparison of pointer with integer zero");
14779 : : }
14780 : 38 : else if (code0 == POINTER_TYPE
14781 : 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14782 : : {
14783 : 18 : result_type = type0;
14784 : 18 : pedwarn (location, 0, "comparison between pointer and integer");
14785 : : }
14786 : 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14787 : 20 : && code1 == POINTER_TYPE)
14788 : : {
14789 : 18 : result_type = type1;
14790 : 18 : pedwarn (location, 0, "comparison between pointer and integer");
14791 : : }
14792 : :
14793 : 915156 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
14794 : 63201 : && current_function_decl != NULL_TREE
14795 : 978346 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
14796 : : {
14797 : 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
14798 : 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
14799 : :
14800 : 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
14801 : 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
14802 : : }
14803 : :
14804 : 2745378 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14805 : 1830222 : || truth_value_p (TREE_CODE (orig_op0)))
14806 : 1830237 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14807 : 1830237 : || truth_value_p (TREE_CODE (orig_op1))))
14808 : 509 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14809 : : break;
14810 : :
14811 : 43 : case MIN_EXPR:
14812 : 43 : case MAX_EXPR:
14813 : : /* Used for OpenMP atomics. */
14814 : 43 : gcc_assert (flag_openmp);
14815 : : common = 1;
14816 : : break;
14817 : :
14818 : 0 : default:
14819 : 0 : gcc_unreachable ();
14820 : : }
14821 : :
14822 : 15100126 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14823 : 0 : return error_mark_node;
14824 : :
14825 : 15100126 : if (gnu_vector_type_p (type0)
14826 : 1443550 : && gnu_vector_type_p (type1)
14827 : 16542858 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
14828 : 1442712 : || !vector_types_compatible_elements_p (type0, type1)))
14829 : : {
14830 : 26 : gcc_rich_location richloc (location);
14831 : 26 : maybe_range_label_for_tree_type_mismatch
14832 : 26 : label_for_op0 (orig_op0, orig_op1),
14833 : 26 : label_for_op1 (orig_op1, orig_op0);
14834 : 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
14835 : 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
14836 : 26 : binary_op_error (&richloc, code, type0, type1);
14837 : 26 : return error_mark_node;
14838 : 26 : }
14839 : :
14840 : 15100100 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14841 : 1694611 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14842 : 1626870 : || gnu_vector_type_p (type0))
14843 : 16611365 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14844 : 1536113 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14845 : 1444782 : || gnu_vector_type_p (type1)))
14846 : : {
14847 : 14914683 : bool first_complex = (code0 == COMPLEX_TYPE);
14848 : 14914683 : bool second_complex = (code1 == COMPLEX_TYPE);
14849 : 14914683 : int none_complex = (!first_complex && !second_complex);
14850 : :
14851 : 14914683 : if (shorten || common || short_compare)
14852 : : {
14853 : 13403914 : result_type = c_common_type (type0, type1);
14854 : 13403914 : do_warn_double_promotion (result_type, type0, type1,
14855 : : "implicit conversion from %qT to %qT "
14856 : : "to match other operand of binary "
14857 : : "expression",
14858 : : location);
14859 : 13403914 : if (result_type == error_mark_node)
14860 : : return error_mark_node;
14861 : : }
14862 : :
14863 : 14914661 : if (first_complex != second_complex
14864 : 82043 : && (code == PLUS_EXPR
14865 : : || code == MINUS_EXPR
14866 : 82043 : || code == MULT_EXPR
14867 : 17525 : || (code == TRUNC_DIV_EXPR && first_complex))
14868 : 65932 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
14869 : 14976915 : && flag_signed_zeros)
14870 : : {
14871 : : /* An operation on mixed real/complex operands must be
14872 : : handled specially, but the language-independent code can
14873 : : more easily optimize the plain complex arithmetic if
14874 : : -fno-signed-zeros. */
14875 : 61942 : tree real_type = TREE_TYPE (result_type);
14876 : 61942 : tree real, imag;
14877 : 61942 : if (type0 != orig_type0 || type1 != orig_type1)
14878 : : {
14879 : 94 : gcc_assert (may_need_excess_precision && common);
14880 : 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
14881 : : }
14882 : 61942 : if (first_complex)
14883 : : {
14884 : 8253 : if (TREE_TYPE (op0) != result_type)
14885 : 1787 : op0 = convert_and_check (location, result_type, op0);
14886 : 8253 : if (TREE_TYPE (op1) != real_type)
14887 : 4601 : op1 = convert_and_check (location, real_type, op1);
14888 : : }
14889 : : else
14890 : : {
14891 : 53689 : if (TREE_TYPE (op0) != real_type)
14892 : 2698 : op0 = convert_and_check (location, real_type, op0);
14893 : 53689 : if (TREE_TYPE (op1) != result_type)
14894 : 1866 : op1 = convert_and_check (location, result_type, op1);
14895 : : }
14896 : 61942 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
14897 : 0 : return error_mark_node;
14898 : 61942 : if (first_complex)
14899 : : {
14900 : 8253 : op0 = save_expr (op0);
14901 : 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
14902 : : op0, true);
14903 : 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
14904 : : op0, true);
14905 : 8253 : switch (code)
14906 : : {
14907 : 3488 : case MULT_EXPR:
14908 : 3488 : case TRUNC_DIV_EXPR:
14909 : 3488 : op1 = save_expr (op1);
14910 : 3488 : imag = build2 (resultcode, real_type, imag, op1);
14911 : : /* Fall through. */
14912 : 8253 : case PLUS_EXPR:
14913 : 8253 : case MINUS_EXPR:
14914 : 8253 : real = build2 (resultcode, real_type, real, op1);
14915 : 8253 : break;
14916 : 0 : default:
14917 : 0 : gcc_unreachable();
14918 : : }
14919 : : }
14920 : : else
14921 : : {
14922 : 53689 : op1 = save_expr (op1);
14923 : 53689 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
14924 : : op1, true);
14925 : 53689 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
14926 : : op1, true);
14927 : 53689 : switch (code)
14928 : : {
14929 : 2008 : case MULT_EXPR:
14930 : 2008 : op0 = save_expr (op0);
14931 : 2008 : imag = build2 (resultcode, real_type, op0, imag);
14932 : : /* Fall through. */
14933 : 31957 : case PLUS_EXPR:
14934 : 31957 : real = build2 (resultcode, real_type, op0, real);
14935 : 31957 : break;
14936 : 21732 : case MINUS_EXPR:
14937 : 21732 : real = build2 (resultcode, real_type, op0, real);
14938 : 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
14939 : 21732 : break;
14940 : 0 : default:
14941 : 0 : gcc_unreachable();
14942 : : }
14943 : : }
14944 : 61942 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
14945 : 61942 : goto return_build_binary_op;
14946 : : }
14947 : :
14948 : : /* For certain operations (which identify themselves by shorten != 0)
14949 : : if both args were extended from the same smaller type,
14950 : : do the arithmetic in that type and then extend.
14951 : :
14952 : : shorten !=0 and !=1 indicates a bitwise operation.
14953 : : For them, this optimization is safe only if
14954 : : both args are zero-extended or both are sign-extended.
14955 : : Otherwise, we might change the result.
14956 : : Eg, (short)-1 | (unsigned short)-1 is (int)-1
14957 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
14958 : :
14959 : 14852719 : if (shorten && none_complex)
14960 : : {
14961 : 1250177 : final_type = result_type;
14962 : 1250177 : result_type = shorten_binary_op (result_type, op0, op1,
14963 : : shorten == -1);
14964 : : }
14965 : :
14966 : : /* Shifts can be shortened if shifting right. */
14967 : :
14968 : 14852719 : if (short_shift)
14969 : : {
14970 : 196362 : int unsigned_arg;
14971 : 196362 : tree arg0 = get_narrower (op0, &unsigned_arg);
14972 : :
14973 : 196362 : final_type = result_type;
14974 : :
14975 : 196362 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
14976 : 138451 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
14977 : :
14978 : 196362 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
14979 : 2810 : && tree_int_cst_sgn (op1) > 0
14980 : : /* We can shorten only if the shift count is less than the
14981 : : number of bits in the smaller type size. */
14982 : 2810 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
14983 : : /* We cannot drop an unsigned shift after sign-extension. */
14984 : 199032 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
14985 : : {
14986 : : /* Do an unsigned shift if the operand was zero-extended. */
14987 : 2656 : result_type
14988 : 2656 : = c_common_signed_or_unsigned_type (unsigned_arg,
14989 : 2656 : TREE_TYPE (arg0));
14990 : : /* Convert value-to-be-shifted to that type. */
14991 : 2656 : if (TREE_TYPE (op0) != result_type)
14992 : 2656 : op0 = convert (result_type, op0);
14993 : : converted = 1;
14994 : : }
14995 : : }
14996 : :
14997 : : /* Comparison operations are shortened too but differently.
14998 : : They identify themselves by setting short_compare = 1. */
14999 : :
15000 : 14852719 : if (short_compare)
15001 : : {
15002 : : /* Don't write &op0, etc., because that would prevent op0
15003 : : from being kept in a register.
15004 : : Instead, make copies of the our local variables and
15005 : : pass the copies by reference, then copy them back afterward. */
15006 : 2397242 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15007 : 2397242 : enum tree_code xresultcode = resultcode;
15008 : 2397242 : tree val
15009 : 2397242 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15010 : : &xresultcode);
15011 : :
15012 : 2397242 : if (val != NULL_TREE)
15013 : : {
15014 : 15785 : ret = val;
15015 : 15785 : goto return_build_binary_op;
15016 : : }
15017 : :
15018 : 2381457 : op0 = xop0, op1 = xop1;
15019 : 2381457 : converted = 1;
15020 : 2381457 : resultcode = xresultcode;
15021 : :
15022 : 2381457 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15023 : : {
15024 : 2233679 : bool op0_maybe_const = true;
15025 : 2233679 : bool op1_maybe_const = true;
15026 : 2233679 : tree orig_op0_folded, orig_op1_folded;
15027 : :
15028 : 2233679 : if (in_late_binary_op)
15029 : : {
15030 : : orig_op0_folded = orig_op0;
15031 : : orig_op1_folded = orig_op1;
15032 : : }
15033 : : else
15034 : : {
15035 : : /* Fold for the sake of possible warnings, as in
15036 : : build_conditional_expr. This requires the
15037 : : "original" values to be folded, not just op0 and
15038 : : op1. */
15039 : 2225391 : c_inhibit_evaluation_warnings++;
15040 : 2225391 : op0 = c_fully_fold (op0, require_constant_value,
15041 : : &op0_maybe_const);
15042 : 2225391 : op1 = c_fully_fold (op1, require_constant_value,
15043 : : &op1_maybe_const);
15044 : 2225391 : c_inhibit_evaluation_warnings--;
15045 : 2225391 : orig_op0_folded = c_fully_fold (orig_op0,
15046 : : require_constant_value,
15047 : : NULL);
15048 : 2225391 : orig_op1_folded = c_fully_fold (orig_op1,
15049 : : require_constant_value,
15050 : : NULL);
15051 : : }
15052 : :
15053 : 2233679 : if (warn_sign_compare)
15054 : 372791 : warn_for_sign_compare (location, orig_op0_folded,
15055 : : orig_op1_folded, op0, op1,
15056 : : result_type, resultcode);
15057 : 2233679 : if (!in_late_binary_op && !int_operands)
15058 : : {
15059 : 2048951 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15060 : 2045437 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15061 : 2048951 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15062 : 800490 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15063 : : }
15064 : : }
15065 : : }
15066 : : }
15067 : :
15068 : : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15069 : : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15070 : : Then the expression will be built.
15071 : : It will be given type FINAL_TYPE if that is nonzero;
15072 : : otherwise, it will be given type RESULT_TYPE. */
15073 : :
15074 : 15022351 : if (!result_type)
15075 : : {
15076 : : /* Favor showing any expression locations that are available. */
15077 : 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15078 : 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15079 : 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15080 : 514 : return error_mark_node;
15081 : 514 : }
15082 : :
15083 : 15021837 : if (build_type == NULL_TREE)
15084 : : {
15085 : 12457160 : build_type = result_type;
15086 : 12457160 : if ((type0 != orig_type0 || type1 != orig_type1)
15087 : 797 : && !boolean_op)
15088 : : {
15089 : 797 : gcc_assert (may_need_excess_precision && common);
15090 : 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15091 : : }
15092 : : }
15093 : :
15094 : 15021837 : if (!converted)
15095 : : {
15096 : 11126705 : op0 = ep_convert_and_check (location, result_type, op0,
15097 : : semantic_result_type);
15098 : 11126705 : op1 = ep_convert_and_check (location, result_type, op1,
15099 : : semantic_result_type);
15100 : :
15101 : : /* This can happen if one operand has a vector type, and the other
15102 : : has a different type. */
15103 : 11126705 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15104 : 3 : return error_mark_node;
15105 : : }
15106 : :
15107 : 15021834 : if (sanitize_flags_p ((SANITIZE_SHIFT
15108 : : | SANITIZE_DIVIDE
15109 : : | SANITIZE_FLOAT_DIVIDE
15110 : : | SANITIZE_SI_OVERFLOW))
15111 : 23306 : && current_function_decl != NULL_TREE
15112 : 19843 : && (doing_div_or_mod || doing_shift)
15113 : 15024759 : && !require_constant_value)
15114 : : {
15115 : : /* OP0 and/or OP1 might have side-effects. */
15116 : 2869 : op0 = save_expr (op0);
15117 : 2869 : op1 = save_expr (op1);
15118 : 2869 : op0 = c_fully_fold (op0, false, NULL);
15119 : 2869 : op1 = c_fully_fold (op1, false, NULL);
15120 : 2869 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15121 : : | SANITIZE_FLOAT_DIVIDE
15122 : : | SANITIZE_SI_OVERFLOW))))
15123 : 912 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15124 : 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15125 : 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15126 : : }
15127 : :
15128 : : /* Treat expressions in initializers specially as they can't trap. */
15129 : 15021834 : if (int_const_or_overflow)
15130 : 7733770 : ret = (require_constant_value
15131 : 7733770 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15132 : : op0, op1)
15133 : 7687649 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15134 : : else
15135 : 7288064 : ret = build2 (resultcode, build_type, op0, op1);
15136 : 15021834 : if (final_type != NULL_TREE)
15137 : 1446539 : ret = convert (final_type, ret);
15138 : :
15139 : 13575295 : return_build_binary_op:
15140 : 16438809 : gcc_assert (ret != error_mark_node);
15141 : 16438809 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15142 : 1740 : ret = (int_operands
15143 : 1740 : ? note_integer_operands (ret)
15144 : 422 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15145 : 16437069 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15146 : 60300 : && !in_late_binary_op)
15147 : 60300 : ret = note_integer_operands (ret);
15148 : 16438809 : protected_set_expr_location (ret, location);
15149 : :
15150 : 16438809 : if (instrument_expr != NULL)
15151 : 1420 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15152 : : instrument_expr, ret);
15153 : :
15154 : 16438809 : if (semantic_result_type)
15155 : 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15156 : : semantic_result_type, ret);
15157 : :
15158 : : return ret;
15159 : : }
15160 : :
15161 : :
15162 : : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15163 : : purpose. LOCATION is the source location for the expression. */
15164 : :
15165 : : tree
15166 : 4054799 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15167 : : {
15168 : 4054799 : bool int_const, int_operands;
15169 : :
15170 : 4054799 : switch (TREE_CODE (TREE_TYPE (expr)))
15171 : : {
15172 : 72 : case ARRAY_TYPE:
15173 : 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15174 : 72 : return error_mark_node;
15175 : :
15176 : 86 : case RECORD_TYPE:
15177 : 86 : error_at (location, "used struct type value where scalar is required");
15178 : 86 : return error_mark_node;
15179 : :
15180 : 83 : case UNION_TYPE:
15181 : 83 : error_at (location, "used union type value where scalar is required");
15182 : 83 : return error_mark_node;
15183 : :
15184 : 22 : case VOID_TYPE:
15185 : 22 : error_at (location, "void value not ignored as it ought to be");
15186 : 22 : return error_mark_node;
15187 : :
15188 : 29704 : case POINTER_TYPE:
15189 : 29704 : if (reject_gcc_builtin (expr))
15190 : 3 : return error_mark_node;
15191 : : break;
15192 : :
15193 : 0 : case FUNCTION_TYPE:
15194 : 0 : gcc_unreachable ();
15195 : :
15196 : 8 : case VECTOR_TYPE:
15197 : 8 : error_at (location, "used vector type where scalar is required");
15198 : 8 : return error_mark_node;
15199 : :
15200 : : default:
15201 : : break;
15202 : : }
15203 : :
15204 : : /* Conversion of a floating constant to boolean goes through here
15205 : : and yields an integer constant expression. Otherwise, the result
15206 : : is only an integer constant expression if the argument is. */
15207 : 894786 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15208 : 4054601 : || ((TREE_CODE (expr) == REAL_CST
15209 : 3159441 : || TREE_CODE (expr) == COMPLEX_CST)
15210 : 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15211 : 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15212 : 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15213 : 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15214 : : == BOOLEAN_TYPE)))));
15215 : 4054525 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15216 : 894946 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15217 : : {
15218 : 319 : expr = remove_c_maybe_const_expr (expr);
15219 : 319 : expr = build2 (NE_EXPR, type, expr,
15220 : 319 : convert (TREE_TYPE (expr), integer_zero_node));
15221 : 319 : expr = note_integer_operands (expr);
15222 : : }
15223 : : else
15224 : : {
15225 : : /* ??? Should we also give an error for vectors rather than leaving
15226 : : those to give errors later? */
15227 : 4054206 : expr = c_common_truthvalue_conversion (location, expr);
15228 : 4054206 : expr = fold_convert_loc (location, type, expr);
15229 : : }
15230 : :
15231 : 4054525 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15232 : : {
15233 : 76 : if (TREE_OVERFLOW (expr))
15234 : : return expr;
15235 : : else
15236 : 76 : return note_integer_operands (expr);
15237 : : }
15238 : 4054449 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15239 : 754 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15240 : : return expr;
15241 : : }
15242 : :
15243 : :
15244 : : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15245 : : required. */
15246 : :
15247 : : tree
15248 : 116370442 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15249 : : {
15250 : 116370442 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15251 : : {
15252 : 655 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15253 : : /* Executing a compound literal inside a function reinitializes
15254 : : it. */
15255 : 655 : if (!TREE_STATIC (decl))
15256 : 399 : *se = true;
15257 : 655 : return decl;
15258 : : }
15259 : : else
15260 : : return expr;
15261 : : }
15262 : :
15263 : : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15264 : : statement. LOC is the location of the construct. */
15265 : :
15266 : : tree
15267 : 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15268 : : tree clauses)
15269 : : {
15270 : 2154 : body = c_end_compound_stmt (loc, body, true);
15271 : :
15272 : 2154 : tree stmt = make_node (code);
15273 : 2154 : TREE_TYPE (stmt) = void_type_node;
15274 : 2154 : OMP_BODY (stmt) = body;
15275 : 2154 : OMP_CLAUSES (stmt) = clauses;
15276 : 2154 : SET_EXPR_LOCATION (stmt, loc);
15277 : :
15278 : 2154 : return add_stmt (stmt);
15279 : : }
15280 : :
15281 : : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15282 : : statement. LOC is the location of the OACC_DATA. */
15283 : :
15284 : : tree
15285 : 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15286 : : {
15287 : 497 : tree stmt;
15288 : :
15289 : 497 : block = c_end_compound_stmt (loc, block, true);
15290 : :
15291 : 497 : stmt = make_node (OACC_DATA);
15292 : 497 : TREE_TYPE (stmt) = void_type_node;
15293 : 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15294 : 497 : OACC_DATA_BODY (stmt) = block;
15295 : 497 : SET_EXPR_LOCATION (stmt, loc);
15296 : :
15297 : 497 : return add_stmt (stmt);
15298 : : }
15299 : :
15300 : : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15301 : : statement. LOC is the location of the OACC_HOST_DATA. */
15302 : :
15303 : : tree
15304 : 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15305 : : {
15306 : 21 : tree stmt;
15307 : :
15308 : 21 : block = c_end_compound_stmt (loc, block, true);
15309 : :
15310 : 21 : stmt = make_node (OACC_HOST_DATA);
15311 : 21 : TREE_TYPE (stmt) = void_type_node;
15312 : 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15313 : 21 : OACC_HOST_DATA_BODY (stmt) = block;
15314 : 21 : SET_EXPR_LOCATION (stmt, loc);
15315 : :
15316 : 21 : return add_stmt (stmt);
15317 : : }
15318 : :
15319 : : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15320 : :
15321 : : tree
15322 : 11989 : c_begin_omp_parallel (void)
15323 : : {
15324 : 11989 : tree block;
15325 : :
15326 : 11989 : keep_next_level ();
15327 : 11989 : block = c_begin_compound_stmt (true);
15328 : :
15329 : 11989 : return block;
15330 : : }
15331 : :
15332 : : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15333 : : statement. LOC is the location of the OMP_PARALLEL. */
15334 : :
15335 : : tree
15336 : 5664 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15337 : : {
15338 : 5664 : tree stmt;
15339 : :
15340 : 5664 : block = c_end_compound_stmt (loc, block, true);
15341 : :
15342 : 5664 : stmt = make_node (OMP_PARALLEL);
15343 : 5664 : TREE_TYPE (stmt) = void_type_node;
15344 : 5664 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15345 : 5664 : OMP_PARALLEL_BODY (stmt) = block;
15346 : 5664 : SET_EXPR_LOCATION (stmt, loc);
15347 : :
15348 : 5664 : return add_stmt (stmt);
15349 : : }
15350 : :
15351 : : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15352 : :
15353 : : tree
15354 : 894 : c_begin_omp_task (void)
15355 : : {
15356 : 894 : tree block;
15357 : :
15358 : 894 : keep_next_level ();
15359 : 894 : block = c_begin_compound_stmt (true);
15360 : :
15361 : 894 : return block;
15362 : : }
15363 : :
15364 : : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15365 : : statement. LOC is the location of the #pragma. */
15366 : :
15367 : : tree
15368 : 894 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15369 : : {
15370 : 894 : tree stmt;
15371 : :
15372 : 894 : block = c_end_compound_stmt (loc, block, true);
15373 : :
15374 : 894 : stmt = make_node (OMP_TASK);
15375 : 894 : TREE_TYPE (stmt) = void_type_node;
15376 : 894 : OMP_TASK_CLAUSES (stmt) = clauses;
15377 : 894 : OMP_TASK_BODY (stmt) = block;
15378 : 894 : SET_EXPR_LOCATION (stmt, loc);
15379 : :
15380 : 894 : return add_stmt (stmt);
15381 : : }
15382 : :
15383 : : /* Generate GOMP_cancel call for #pragma omp cancel. */
15384 : :
15385 : : void
15386 : 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15387 : : {
15388 : 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15389 : 213 : int mask = 0;
15390 : 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15391 : : mask = 1;
15392 : 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15393 : : mask = 2;
15394 : 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15395 : : mask = 4;
15396 : 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15397 : : mask = 8;
15398 : : else
15399 : : {
15400 : 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15401 : : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15402 : : "clauses");
15403 : 0 : return;
15404 : : }
15405 : 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15406 : 213 : if (ifc != NULL_TREE)
15407 : : {
15408 : 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15409 : 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15410 : 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15411 : : "expected %<cancel%> %<if%> clause modifier");
15412 : : else
15413 : : {
15414 : 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15415 : 29 : if (ifc2 != NULL_TREE)
15416 : : {
15417 : 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15418 : : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15419 : : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15420 : 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15421 : : "expected %<cancel%> %<if%> clause modifier");
15422 : : }
15423 : : }
15424 : :
15425 : 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15426 : 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15427 : 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15428 : : build_zero_cst (type));
15429 : : }
15430 : : else
15431 : 182 : ifc = boolean_true_node;
15432 : 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15433 : 213 : build_int_cst (integer_type_node, mask),
15434 : : ifc);
15435 : 213 : add_stmt (stmt);
15436 : : }
15437 : :
15438 : : /* Generate GOMP_cancellation_point call for
15439 : : #pragma omp cancellation point. */
15440 : :
15441 : : void
15442 : 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15443 : : {
15444 : 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15445 : 167 : int mask = 0;
15446 : 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15447 : : mask = 1;
15448 : 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15449 : : mask = 2;
15450 : 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15451 : : mask = 4;
15452 : 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15453 : : mask = 8;
15454 : : else
15455 : : {
15456 : 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15457 : : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15458 : : "clauses");
15459 : 1 : return;
15460 : : }
15461 : 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15462 : 166 : build_int_cst (integer_type_node, mask));
15463 : 166 : add_stmt (stmt);
15464 : : }
15465 : :
15466 : : /* Helper function for handle_omp_array_sections. Called recursively
15467 : : to handle multiple array-section-subscripts. C is the clause,
15468 : : T current expression (initially OMP_CLAUSE_DECL), which is either
15469 : : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15470 : : expression if specified, TREE_VALUE length expression if specified,
15471 : : TREE_CHAIN is what it has been specified after, or some decl.
15472 : : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15473 : : set to true if any of the array-section-subscript could have length
15474 : : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15475 : : first array-section-subscript which is known not to have length
15476 : : of one. Given say:
15477 : : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15478 : : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15479 : : all are or may have length of 1, array-section-subscript [:2] is the
15480 : : first one known not to have length 1. For array-section-subscript
15481 : : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15482 : : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15483 : : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15484 : : case though, as some lengths could be zero. */
15485 : :
15486 : : static tree
15487 : 6378 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15488 : : bool &maybe_zero_len, unsigned int &first_non_one,
15489 : : enum c_omp_region_type ort)
15490 : : {
15491 : 6378 : tree ret, low_bound, length, type;
15492 : 6378 : bool openacc = (ort & C_ORT_ACC) != 0;
15493 : 6378 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15494 : : {
15495 : 2970 : if (error_operand_p (t))
15496 : 2 : return error_mark_node;
15497 : 2968 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15498 : 2968 : ret = t;
15499 : 2968 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15500 : 2869 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15501 : 5523 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15502 : : {
15503 : 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15504 : 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15505 : 6 : return error_mark_node;
15506 : : }
15507 : 2962 : if (!ai.check_clause (c))
15508 : 0 : return error_mark_node;
15509 : 2962 : else if (ai.component_access_p ()
15510 : 3263 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15511 : 15 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15512 : 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15513 : 301 : t = ai.get_root_term (true);
15514 : : else
15515 : 2661 : t = ai.unconverted_ref_origin ();
15516 : 2962 : if (t == error_mark_node)
15517 : : return error_mark_node;
15518 : 2962 : if (!VAR_P (t)
15519 : 740 : && (ort == C_ORT_ACC || !EXPR_P (t))
15520 : 737 : && TREE_CODE (t) != PARM_DECL)
15521 : : {
15522 : 5 : if (DECL_P (t))
15523 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
15524 : : "%qD is not a variable in %qs clause", t,
15525 : 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15526 : : else
15527 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
15528 : : "%qE is not a variable in %qs clause", t,
15529 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15530 : 5 : return error_mark_node;
15531 : : }
15532 : 2957 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15533 : 2859 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15534 : 5503 : && TYPE_ATOMIC (TREE_TYPE (t)))
15535 : : {
15536 : 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15537 : 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15538 : 0 : return error_mark_node;
15539 : : }
15540 : 2957 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15541 : 2859 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15542 : 2546 : && VAR_P (t)
15543 : 4956 : && DECL_THREAD_LOCAL_P (t))
15544 : : {
15545 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
15546 : : "%qD is threadprivate variable in %qs clause", t,
15547 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15548 : 3 : return error_mark_node;
15549 : : }
15550 : 2954 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15551 : 2856 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15552 : 411 : && TYPE_ATOMIC (TREE_TYPE (t))
15553 : 2956 : && POINTER_TYPE_P (TREE_TYPE (t)))
15554 : : {
15555 : : /* If the array section is pointer based and the pointer
15556 : : itself is _Atomic qualified, we need to atomically load
15557 : : the pointer. */
15558 : 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15559 : : ret, false, false);
15560 : : }
15561 : 2954 : return ret;
15562 : 2968 : }
15563 : :
15564 : 3408 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15565 : : maybe_zero_len, first_non_one, ort);
15566 : 3408 : if (ret == error_mark_node || ret == NULL_TREE)
15567 : : return ret;
15568 : :
15569 : 3343 : type = TREE_TYPE (ret);
15570 : 3343 : low_bound = TREE_OPERAND (t, 1);
15571 : 3343 : length = TREE_OPERAND (t, 2);
15572 : :
15573 : 3343 : if (low_bound == error_mark_node || length == error_mark_node)
15574 : : return error_mark_node;
15575 : :
15576 : 3343 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15577 : : {
15578 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
15579 : : "low bound %qE of array section does not have integral type",
15580 : : low_bound);
15581 : 13 : return error_mark_node;
15582 : : }
15583 : 3330 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15584 : : {
15585 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
15586 : : "length %qE of array section does not have integral type",
15587 : : length);
15588 : 12 : return error_mark_node;
15589 : : }
15590 : 3318 : if (low_bound
15591 : 2735 : && TREE_CODE (low_bound) == INTEGER_CST
15592 : 5764 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15593 : 2446 : > TYPE_PRECISION (sizetype))
15594 : 0 : low_bound = fold_convert (sizetype, low_bound);
15595 : 3318 : if (length
15596 : 3130 : && TREE_CODE (length) == INTEGER_CST
15597 : 5504 : && TYPE_PRECISION (TREE_TYPE (length))
15598 : 2186 : > TYPE_PRECISION (sizetype))
15599 : 0 : length = fold_convert (sizetype, length);
15600 : 3318 : if (low_bound == NULL_TREE)
15601 : 583 : low_bound = integer_zero_node;
15602 : 3318 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15603 : 3318 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15604 : 1928 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15605 : : {
15606 : 10 : if (length != integer_one_node)
15607 : : {
15608 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
15609 : : "expected single pointer in %qs clause",
15610 : : user_omp_clause_code_name (c, openacc));
15611 : 6 : return error_mark_node;
15612 : : }
15613 : : }
15614 : 3312 : if (length != NULL_TREE)
15615 : : {
15616 : 3128 : if (!integer_nonzerop (length))
15617 : : {
15618 : 972 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15619 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15620 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15621 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15622 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15623 : : {
15624 : 180 : if (integer_zerop (length))
15625 : : {
15626 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
15627 : : "zero length array section in %qs clause",
15628 : 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15629 : 12 : return error_mark_node;
15630 : : }
15631 : : }
15632 : : else
15633 : 792 : maybe_zero_len = true;
15634 : : }
15635 : 3116 : if (first_non_one == types.length ()
15636 : 3116 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15637 : 1596 : first_non_one++;
15638 : : }
15639 : 3300 : if (TREE_CODE (type) == ARRAY_TYPE)
15640 : : {
15641 : 1490 : if (length == NULL_TREE
15642 : 1490 : && (TYPE_DOMAIN (type) == NULL_TREE
15643 : 168 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15644 : : {
15645 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
15646 : : "for unknown bound array type length expression must "
15647 : : "be specified");
15648 : 8 : return error_mark_node;
15649 : : }
15650 : 1482 : if (TREE_CODE (low_bound) == INTEGER_CST
15651 : 1482 : && tree_int_cst_sgn (low_bound) == -1)
15652 : : {
15653 : 35 : error_at (OMP_CLAUSE_LOCATION (c),
15654 : : "negative low bound in array section in %qs clause",
15655 : 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15656 : 35 : return error_mark_node;
15657 : : }
15658 : 1447 : if (length != NULL_TREE
15659 : 1291 : && TREE_CODE (length) == INTEGER_CST
15660 : 2298 : && tree_int_cst_sgn (length) == -1)
15661 : : {
15662 : 35 : error_at (OMP_CLAUSE_LOCATION (c),
15663 : : "negative length in array section in %qs clause",
15664 : 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15665 : 35 : return error_mark_node;
15666 : : }
15667 : 1412 : if (TYPE_DOMAIN (type)
15668 : 1401 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
15669 : 2813 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
15670 : : == INTEGER_CST)
15671 : : {
15672 : 1055 : tree size
15673 : 1055 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
15674 : 1055 : size = size_binop (PLUS_EXPR, size, size_one_node);
15675 : 1055 : if (TREE_CODE (low_bound) == INTEGER_CST)
15676 : : {
15677 : 868 : if (tree_int_cst_lt (size, low_bound))
15678 : : {
15679 : 10 : error_at (OMP_CLAUSE_LOCATION (c),
15680 : : "low bound %qE above array section size "
15681 : : "in %qs clause", low_bound,
15682 : 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15683 : 10 : return error_mark_node;
15684 : : }
15685 : 858 : if (tree_int_cst_equal (size, low_bound))
15686 : : {
15687 : 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15688 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15689 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15690 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15691 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15692 : : {
15693 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
15694 : : "zero length array section in %qs clause",
15695 : 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15696 : 5 : return error_mark_node;
15697 : : }
15698 : 0 : maybe_zero_len = true;
15699 : : }
15700 : 853 : else if (length == NULL_TREE
15701 : 256 : && first_non_one == types.length ()
15702 : 908 : && tree_int_cst_equal
15703 : 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
15704 : : low_bound))
15705 : 30 : first_non_one++;
15706 : : }
15707 : 187 : else if (length == NULL_TREE)
15708 : : {
15709 : 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15710 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15711 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
15712 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
15713 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
15714 : 0 : maybe_zero_len = true;
15715 : 6 : if (first_non_one == types.length ())
15716 : 1 : first_non_one++;
15717 : : }
15718 : 1040 : if (length && TREE_CODE (length) == INTEGER_CST)
15719 : : {
15720 : 793 : if (tree_int_cst_lt (size, length))
15721 : : {
15722 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
15723 : : "length %qE above array section size "
15724 : : "in %qs clause", length,
15725 : 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15726 : 11 : return error_mark_node;
15727 : : }
15728 : 782 : if (TREE_CODE (low_bound) == INTEGER_CST)
15729 : : {
15730 : 682 : tree lbpluslen
15731 : 682 : = size_binop (PLUS_EXPR,
15732 : : fold_convert (sizetype, low_bound),
15733 : : fold_convert (sizetype, length));
15734 : 682 : if (TREE_CODE (lbpluslen) == INTEGER_CST
15735 : 682 : && tree_int_cst_lt (size, lbpluslen))
15736 : : {
15737 : 10 : error_at (OMP_CLAUSE_LOCATION (c),
15738 : : "high bound %qE above array section size "
15739 : : "in %qs clause", lbpluslen,
15740 : 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15741 : 10 : return error_mark_node;
15742 : : }
15743 : : }
15744 : : }
15745 : : }
15746 : 357 : else if (length == NULL_TREE)
15747 : : {
15748 : 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15749 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15750 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
15751 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
15752 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
15753 : 0 : maybe_zero_len = true;
15754 : 20 : if (first_non_one == types.length ())
15755 : 10 : first_non_one++;
15756 : : }
15757 : :
15758 : : /* For [lb:] we will need to evaluate lb more than once. */
15759 : 923 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
15760 : : {
15761 : 127 : tree lb = save_expr (low_bound);
15762 : 127 : if (lb != low_bound)
15763 : : {
15764 : 2 : TREE_OPERAND (t, 1) = lb;
15765 : 2 : low_bound = lb;
15766 : : }
15767 : : }
15768 : : }
15769 : 1810 : else if (TREE_CODE (type) == POINTER_TYPE)
15770 : : {
15771 : 1799 : if (length == NULL_TREE)
15772 : : {
15773 : 10 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
15774 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
15775 : : "for array function parameter length expression "
15776 : : "must be specified");
15777 : : else
15778 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
15779 : : "for pointer type length expression must be specified");
15780 : 10 : return error_mark_node;
15781 : : }
15782 : 1789 : if (length != NULL_TREE
15783 : 1789 : && TREE_CODE (length) == INTEGER_CST
15784 : 1285 : && tree_int_cst_sgn (length) == -1)
15785 : : {
15786 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
15787 : : "negative length in array section in %qs clause",
15788 : 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15789 : 20 : return error_mark_node;
15790 : : }
15791 : : /* If there is a pointer type anywhere but in the very first
15792 : : array-section-subscript, the array section could be non-contiguous. */
15793 : 1769 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15794 : 1608 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15795 : 3348 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
15796 : : {
15797 : : /* If any prior dimension has a non-one length, then deem this
15798 : : array section as non-contiguous. */
15799 : 42 : for (tree d = TREE_OPERAND (t, 0);
15800 : 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
15801 : 40 : d = TREE_OPERAND (d, 0))
15802 : : {
15803 : 44 : tree d_length = TREE_OPERAND (d, 2);
15804 : 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
15805 : : {
15806 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15807 : : "array section is not contiguous in %qs clause",
15808 : 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15809 : 4 : return error_mark_node;
15810 : : }
15811 : : }
15812 : : }
15813 : : }
15814 : : else
15815 : : {
15816 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
15817 : : "%qE does not have pointer or array type", ret);
15818 : 11 : return error_mark_node;
15819 : : }
15820 : 3141 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
15821 : 2825 : types.safe_push (TREE_TYPE (ret));
15822 : : /* We will need to evaluate lb more than once. */
15823 : 3141 : tree lb = save_expr (low_bound);
15824 : 3141 : if (lb != low_bound)
15825 : : {
15826 : 273 : TREE_OPERAND (t, 1) = lb;
15827 : 273 : low_bound = lb;
15828 : : }
15829 : 3141 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
15830 : 3141 : return ret;
15831 : : }
15832 : :
15833 : : /* Handle array sections for clause C. */
15834 : :
15835 : : static bool
15836 : 2970 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
15837 : : {
15838 : 2970 : bool maybe_zero_len = false;
15839 : 2970 : unsigned int first_non_one = 0;
15840 : 2970 : auto_vec<tree, 10> types;
15841 : 2970 : tree *tp = &OMP_CLAUSE_DECL (c);
15842 : 2970 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15843 : 2656 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
15844 : 3069 : && OMP_ITERATOR_DECL_P (*tp))
15845 : 66 : tp = &TREE_VALUE (*tp);
15846 : 2970 : tree first = handle_omp_array_sections_1 (c, *tp, types,
15847 : : maybe_zero_len, first_non_one,
15848 : : ort);
15849 : 2970 : if (first == error_mark_node)
15850 : : return true;
15851 : 2752 : if (first == NULL_TREE)
15852 : : return false;
15853 : 2752 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15854 : 2752 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
15855 : : {
15856 : 336 : tree t = *tp;
15857 : 336 : tree tem = NULL_TREE;
15858 : : /* Need to evaluate side effects in the length expressions
15859 : : if any. */
15860 : 336 : while (TREE_CODE (t) == TREE_LIST)
15861 : : {
15862 : 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
15863 : : {
15864 : 0 : if (tem == NULL_TREE)
15865 : : tem = TREE_VALUE (t);
15866 : : else
15867 : 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
15868 : 0 : TREE_VALUE (t), tem);
15869 : : }
15870 : 0 : t = TREE_CHAIN (t);
15871 : : }
15872 : 336 : if (tem)
15873 : 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
15874 : 336 : first = c_fully_fold (first, false, NULL, true);
15875 : 336 : *tp = first;
15876 : : }
15877 : : else
15878 : : {
15879 : 2416 : unsigned int num = types.length (), i;
15880 : 2416 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
15881 : 2416 : tree condition = NULL_TREE;
15882 : :
15883 : 2416 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
15884 : 3 : maybe_zero_len = true;
15885 : :
15886 : 5032 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
15887 : 2616 : t = TREE_OPERAND (t, 0))
15888 : : {
15889 : 2631 : tree low_bound = TREE_OPERAND (t, 1);
15890 : 2631 : tree length = TREE_OPERAND (t, 2);
15891 : :
15892 : 2631 : i--;
15893 : 2631 : if (low_bound
15894 : 2168 : && TREE_CODE (low_bound) == INTEGER_CST
15895 : 4613 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15896 : 1982 : > TYPE_PRECISION (sizetype))
15897 : 0 : low_bound = fold_convert (sizetype, low_bound);
15898 : 2631 : if (length
15899 : 2539 : && TREE_CODE (length) == INTEGER_CST
15900 : 4244 : && TYPE_PRECISION (TREE_TYPE (length))
15901 : 1613 : > TYPE_PRECISION (sizetype))
15902 : 0 : length = fold_convert (sizetype, length);
15903 : 2631 : if (low_bound == NULL_TREE)
15904 : 463 : low_bound = integer_zero_node;
15905 : 2631 : if (!maybe_zero_len && i > first_non_one)
15906 : : {
15907 : 109 : if (integer_nonzerop (low_bound))
15908 : 6 : goto do_warn_noncontiguous;
15909 : 103 : if (length != NULL_TREE
15910 : 50 : && TREE_CODE (length) == INTEGER_CST
15911 : 50 : && TYPE_DOMAIN (types[i])
15912 : 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
15913 : 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
15914 : : == INTEGER_CST)
15915 : : {
15916 : 50 : tree size;
15917 : 50 : size = size_binop (PLUS_EXPR,
15918 : : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
15919 : : size_one_node);
15920 : 50 : if (!tree_int_cst_equal (length, size))
15921 : : {
15922 : 7 : do_warn_noncontiguous:
15923 : 26 : error_at (OMP_CLAUSE_LOCATION (c),
15924 : : "array section is not contiguous in %qs "
15925 : : "clause",
15926 : 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15927 : 2416 : return true;
15928 : : }
15929 : : }
15930 : 96 : if (length != NULL_TREE
15931 : 96 : && TREE_SIDE_EFFECTS (length))
15932 : : {
15933 : 0 : if (side_effects == NULL_TREE)
15934 : : side_effects = length;
15935 : : else
15936 : 0 : side_effects = build2 (COMPOUND_EXPR,
15937 : 0 : TREE_TYPE (side_effects),
15938 : : length, side_effects);
15939 : : }
15940 : : }
15941 : : else
15942 : : {
15943 : 2522 : tree l;
15944 : :
15945 : 2522 : if (i > first_non_one
15946 : 2522 : && ((length && integer_nonzerop (length))
15947 : 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15948 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15949 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
15950 : 0 : continue;
15951 : 2522 : if (length)
15952 : 2489 : l = fold_convert (sizetype, length);
15953 : : else
15954 : : {
15955 : 33 : l = size_binop (PLUS_EXPR,
15956 : : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
15957 : : size_one_node);
15958 : 33 : l = size_binop (MINUS_EXPR, l,
15959 : : fold_convert (sizetype, low_bound));
15960 : : }
15961 : 2522 : if (i > first_non_one)
15962 : : {
15963 : 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
15964 : : size_zero_node);
15965 : 0 : if (condition == NULL_TREE)
15966 : : condition = l;
15967 : : else
15968 : 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
15969 : : l, condition);
15970 : : }
15971 : 2522 : else if (size == NULL_TREE)
15972 : : {
15973 : 2403 : size = size_in_bytes (TREE_TYPE (types[i]));
15974 : 2403 : tree eltype = TREE_TYPE (types[num - 1]);
15975 : 2423 : while (TREE_CODE (eltype) == ARRAY_TYPE)
15976 : 20 : eltype = TREE_TYPE (eltype);
15977 : 2403 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15978 : 2168 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15979 : 4311 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15980 : : {
15981 : 539 : if (integer_zerop (size)
15982 : 1076 : || integer_zerop (size_in_bytes (eltype)))
15983 : : {
15984 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15985 : : "zero length array section in %qs clause",
15986 : 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15987 : 2 : return error_mark_node;
15988 : : }
15989 : 537 : size = size_binop (EXACT_DIV_EXPR, size,
15990 : : size_in_bytes (eltype));
15991 : : }
15992 : 2401 : size = size_binop (MULT_EXPR, size, l);
15993 : 2401 : if (condition)
15994 : 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
15995 : : size, size_zero_node);
15996 : : }
15997 : : else
15998 : 119 : size = size_binop (MULT_EXPR, size, l);
15999 : : }
16000 : : }
16001 : 2401 : if (side_effects)
16002 : 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16003 : 2401 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16004 : 2168 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16005 : 4309 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16006 : : {
16007 : 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16008 : 537 : size = c_fully_fold (size, false, NULL);
16009 : 537 : size = save_expr (size);
16010 : 537 : tree index_type = build_index_type (size);
16011 : 537 : tree eltype = TREE_TYPE (first);
16012 : 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16013 : 12 : eltype = TREE_TYPE (eltype);
16014 : 537 : tree type = c_build_array_type (eltype, index_type);
16015 : 537 : tree ptype = c_build_pointer_type (eltype);
16016 : 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16017 : 296 : t = build_fold_addr_expr (t);
16018 : 537 : tree t2 = build_fold_addr_expr (first);
16019 : 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16020 : : ptrdiff_type_node, t2);
16021 : 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16022 : : ptrdiff_type_node, t2,
16023 : 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16024 : : ptrdiff_type_node, t));
16025 : 537 : t2 = c_fully_fold (t2, false, NULL);
16026 : 537 : if (tree_fits_shwi_p (t2))
16027 : 398 : t = build2 (MEM_REF, type, t,
16028 : 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16029 : : else
16030 : : {
16031 : 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16032 : 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16033 : 139 : TREE_TYPE (t), t, t2);
16034 : 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16035 : : }
16036 : 537 : OMP_CLAUSE_DECL (c) = t;
16037 : 537 : return false;
16038 : : }
16039 : 1864 : first = c_fully_fold (first, false, NULL);
16040 : 1864 : OMP_CLAUSE_DECL (c) = first;
16041 : 1864 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16042 : : return false;
16043 : : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16044 : 1853 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16045 : 1853 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16046 : 1713 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16047 : 1711 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16048 : : {
16049 : 1849 : if (size)
16050 : 1849 : size = c_fully_fold (size, false, NULL);
16051 : 1849 : OMP_CLAUSE_SIZE (c) = size;
16052 : : }
16053 : :
16054 : 1853 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16055 : : return false;
16056 : :
16057 : 1715 : auto_vec<omp_addr_token *, 10> addr_tokens;
16058 : :
16059 : 1715 : if (!omp_parse_expr (addr_tokens, first))
16060 : 1715 : return true;
16061 : :
16062 : 1715 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16063 : :
16064 : 1715 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16065 : 1715 : if (nc != error_mark_node)
16066 : : {
16067 : 1715 : using namespace omp_addr_tokenizer;
16068 : :
16069 : 1715 : if (ai.maybe_zero_length_array_section (c))
16070 : 1711 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16071 : :
16072 : : /* !!! If we're accessing a base decl via chained access
16073 : : methods (e.g. multiple indirections), duplicate clause
16074 : : detection won't work properly. Skip it in that case. */
16075 : 1715 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16076 : 1428 : || addr_tokens[0]->type == ARRAY_BASE)
16077 : 1715 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16078 : 1715 : && addr_tokens[1]->type == ACCESS_METHOD
16079 : 3430 : && omp_access_chain_p (addr_tokens, 1))
16080 : 34 : c = nc;
16081 : :
16082 : 1715 : return false;
16083 : : }
16084 : : }
16085 : : return false;
16086 : 2970 : }
16087 : :
16088 : : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16089 : : an inline call. But, remap
16090 : : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16091 : : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16092 : :
16093 : : static tree
16094 : 510 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16095 : : tree decl, tree placeholder)
16096 : : {
16097 : 510 : copy_body_data id;
16098 : 510 : hash_map<tree, tree> decl_map;
16099 : :
16100 : 510 : decl_map.put (omp_decl1, placeholder);
16101 : 510 : decl_map.put (omp_decl2, decl);
16102 : 510 : memset (&id, 0, sizeof (id));
16103 : 510 : id.src_fn = DECL_CONTEXT (omp_decl1);
16104 : 510 : id.dst_fn = current_function_decl;
16105 : 510 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16106 : 510 : id.decl_map = &decl_map;
16107 : :
16108 : 510 : id.copy_decl = copy_decl_no_change;
16109 : 510 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16110 : 510 : id.transform_new_cfg = true;
16111 : 510 : id.transform_return_to_modify = false;
16112 : 510 : id.eh_lp_nr = 0;
16113 : 510 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16114 : 510 : return stmt;
16115 : 510 : }
16116 : :
16117 : : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16118 : : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16119 : :
16120 : : static tree
16121 : 1123 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16122 : : {
16123 : 1123 : if (*tp == (tree) data)
16124 : 28 : return *tp;
16125 : : return NULL_TREE;
16126 : : }
16127 : :
16128 : : /* Similarly, but also walk aggregate fields. */
16129 : :
16130 : : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16131 : :
16132 : : static tree
16133 : 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16134 : : {
16135 : 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16136 : : return *tp;
16137 : 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16138 : : {
16139 : 2 : tree field;
16140 : 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16141 : :
16142 : 2 : for (field = TYPE_FIELDS (*tp); field;
16143 : 0 : field = DECL_CHAIN (field))
16144 : 2 : if (TREE_CODE (field) == FIELD_DECL)
16145 : : {
16146 : 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16147 : : c_find_omp_var_r, data, pset);
16148 : 2 : if (ret)
16149 : : return ret;
16150 : 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16151 : 2 : if (ret)
16152 : : return ret;
16153 : 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16154 : : pset);
16155 : 2 : if (ret)
16156 : : return ret;
16157 : 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16158 : 2 : if (ret)
16159 : : return ret;
16160 : : }
16161 : : }
16162 : 278 : else if (INTEGRAL_TYPE_P (*tp))
16163 : 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16164 : : ((struct c_find_omp_var_s *) data)->pset);
16165 : : return NULL_TREE;
16166 : : }
16167 : :
16168 : : /* Finish OpenMP iterators ITER. Return true if they are errorneous
16169 : : and clauses containing them should be removed. */
16170 : :
16171 : : static bool
16172 : 142 : c_omp_finish_iterators (tree iter)
16173 : : {
16174 : 142 : bool ret = false;
16175 : 323 : for (tree it = iter; it; it = TREE_CHAIN (it))
16176 : : {
16177 : 181 : tree var = TREE_VEC_ELT (it, 0);
16178 : 181 : tree begin = TREE_VEC_ELT (it, 1);
16179 : 181 : tree end = TREE_VEC_ELT (it, 2);
16180 : 181 : tree step = TREE_VEC_ELT (it, 3);
16181 : 181 : tree orig_step;
16182 : 181 : tree type = TREE_TYPE (var);
16183 : 181 : location_t loc = DECL_SOURCE_LOCATION (var);
16184 : 181 : if (type == error_mark_node)
16185 : : {
16186 : 0 : ret = true;
16187 : 34 : continue;
16188 : : }
16189 : 181 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16190 : : {
16191 : 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16192 : : var);
16193 : 6 : ret = true;
16194 : 6 : continue;
16195 : : }
16196 : 175 : else if (TYPE_ATOMIC (type))
16197 : : {
16198 : 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16199 : 2 : ret = true;
16200 : 2 : continue;
16201 : : }
16202 : 173 : else if (TYPE_READONLY (type))
16203 : : {
16204 : 4 : error_at (loc, "iterator %qD has const qualified type", var);
16205 : 4 : ret = true;
16206 : 4 : continue;
16207 : : }
16208 : 169 : else if (step == error_mark_node
16209 : 169 : || TREE_TYPE (step) == error_mark_node)
16210 : : {
16211 : 0 : ret = true;
16212 : 0 : continue;
16213 : : }
16214 : 169 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16215 : : {
16216 : 6 : error_at (EXPR_LOC_OR_LOC (step, loc),
16217 : : "iterator step with non-integral type");
16218 : 4 : ret = true;
16219 : 4 : continue;
16220 : : }
16221 : 165 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16222 : 165 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16223 : 165 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16224 : 165 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16225 : 165 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16226 : 165 : if (POINTER_TYPE_P (type))
16227 : : {
16228 : 14 : begin = save_expr (begin);
16229 : 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16230 : 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16231 : : fold_convert (sizetype, step),
16232 : : fold_convert (sizetype, begin));
16233 : 14 : step = fold_convert (ssizetype, step);
16234 : : }
16235 : 165 : if (integer_zerop (step))
16236 : : {
16237 : 6 : error_at (loc, "iterator %qD has zero step", var);
16238 : 6 : ret = true;
16239 : 6 : continue;
16240 : : }
16241 : :
16242 : 159 : if (begin == error_mark_node
16243 : 157 : || end == error_mark_node
16244 : 155 : || step == error_mark_node
16245 : 155 : || orig_step == error_mark_node)
16246 : : {
16247 : 4 : ret = true;
16248 : 4 : continue;
16249 : : }
16250 : 155 : hash_set<tree> pset;
16251 : 155 : tree it2;
16252 : 196 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16253 : : {
16254 : 49 : tree var2 = TREE_VEC_ELT (it2, 0);
16255 : 49 : tree begin2 = TREE_VEC_ELT (it2, 1);
16256 : 49 : tree end2 = TREE_VEC_ELT (it2, 2);
16257 : 49 : tree step2 = TREE_VEC_ELT (it2, 3);
16258 : 49 : tree type2 = TREE_TYPE (var2);
16259 : 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16260 : 49 : struct c_find_omp_var_s data = { var, &pset };
16261 : 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16262 : : {
16263 : 2 : error_at (loc2,
16264 : : "type of iterator %qD refers to outer iterator %qD",
16265 : : var2, var);
16266 : 10 : break;
16267 : : }
16268 : 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16269 : : {
16270 : 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16271 : : "begin expression refers to outer iterator %qD", var);
16272 : 2 : break;
16273 : : }
16274 : 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16275 : : {
16276 : 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16277 : : "end expression refers to outer iterator %qD", var);
16278 : 2 : break;
16279 : : }
16280 : 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16281 : : {
16282 : 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16283 : : "step expression refers to outer iterator %qD", var);
16284 : 2 : break;
16285 : : }
16286 : : }
16287 : 155 : if (it2)
16288 : : {
16289 : 8 : ret = true;
16290 : 8 : continue;
16291 : : }
16292 : 147 : TREE_VEC_ELT (it, 1) = begin;
16293 : 147 : TREE_VEC_ELT (it, 2) = end;
16294 : 147 : TREE_VEC_ELT (it, 3) = step;
16295 : 147 : TREE_VEC_ELT (it, 4) = orig_step;
16296 : 155 : }
16297 : 142 : return ret;
16298 : : }
16299 : :
16300 : : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16301 : : Return true if an error has been detected. */
16302 : :
16303 : : static bool
16304 : 9382 : c_oacc_check_attachments (tree c)
16305 : : {
16306 : 9382 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16307 : : return false;
16308 : :
16309 : : /* OpenACC attach / detach clauses must be pointers. */
16310 : 6387 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16311 : 6387 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16312 : : {
16313 : 71 : tree t = OMP_CLAUSE_DECL (c);
16314 : :
16315 : 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16316 : 6 : t = TREE_OPERAND (t, 0);
16317 : :
16318 : 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16319 : : {
16320 : 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16321 : : user_omp_clause_code_name (c, true));
16322 : 6 : return true;
16323 : : }
16324 : : }
16325 : :
16326 : : return false;
16327 : : }
16328 : :
16329 : : /* For all elements of CLAUSES, validate them against their constraints.
16330 : : Remove any elements from the list that are invalid. */
16331 : :
16332 : : tree
16333 : 29209 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16334 : : {
16335 : 29209 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16336 : 29209 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16337 : 29209 : bitmap_head oacc_reduction_head, is_on_device_head;
16338 : 29209 : tree c, t, type, *pc;
16339 : 29209 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16340 : 29209 : bool branch_seen = false;
16341 : 29209 : bool copyprivate_seen = false;
16342 : 29209 : bool mergeable_seen = false;
16343 : 29209 : tree *detach_seen = NULL;
16344 : 29209 : bool linear_variable_step_check = false;
16345 : 29209 : tree *nowait_clause = NULL;
16346 : 29209 : tree *grainsize_seen = NULL;
16347 : 29209 : bool num_tasks_seen = false;
16348 : 29209 : tree ordered_clause = NULL_TREE;
16349 : 29209 : tree schedule_clause = NULL_TREE;
16350 : 29209 : bool oacc_async = false;
16351 : 29209 : tree last_iterators = NULL_TREE;
16352 : 29209 : bool last_iterators_remove = false;
16353 : 29209 : tree *nogroup_seen = NULL;
16354 : 29209 : tree *order_clause = NULL;
16355 : : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16356 : : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16357 : 29209 : int reduction_seen = 0;
16358 : 29209 : bool allocate_seen = false;
16359 : 29209 : bool implicit_moved = false;
16360 : 29209 : bool target_in_reduction_seen = false;
16361 : 29209 : tree *full_seen = NULL;
16362 : 29209 : bool partial_seen = false;
16363 : 29209 : bool openacc = (ort & C_ORT_ACC) != 0;
16364 : 29209 : bool init_seen = false;
16365 : 29209 : bool init_use_destroy_seen = false;
16366 : 29209 : tree init_no_targetsync_clause = NULL_TREE;
16367 : 29209 : tree depend_clause = NULL_TREE;
16368 : :
16369 : 29209 : bitmap_obstack_initialize (NULL);
16370 : 29209 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16371 : 29209 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16372 : 29209 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16373 : 29209 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16374 : : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16375 : 29209 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16376 : 29209 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16377 : 29209 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16378 : : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16379 : : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16380 : 29209 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16381 : 29209 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16382 : :
16383 : 29209 : if (openacc)
16384 : 12261 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16385 : 7250 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16386 : : {
16387 : : oacc_async = true;
16388 : : break;
16389 : : }
16390 : :
16391 : 29209 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16392 : :
16393 : 76852 : for (pc = &clauses, c = clauses; c ; c = *pc)
16394 : : {
16395 : 47643 : bool remove = false;
16396 : 47643 : bool need_complete = false;
16397 : 47643 : bool need_implicitly_determined = false;
16398 : :
16399 : : /* We've reached the end of a list of expanded nodes. Reset the group
16400 : : start pointer. */
16401 : 47643 : if (c == grp_sentinel)
16402 : : {
16403 : 2467 : if (grp_start_p
16404 : 2467 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16405 : 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16406 : 22 : gc = OMP_CLAUSE_CHAIN (gc))
16407 : 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16408 : : grp_start_p = NULL;
16409 : : }
16410 : :
16411 : 47643 : switch (OMP_CLAUSE_CODE (c))
16412 : : {
16413 : 1137 : case OMP_CLAUSE_SHARED:
16414 : 1137 : need_implicitly_determined = true;
16415 : 1137 : goto check_dup_generic;
16416 : :
16417 : 837 : case OMP_CLAUSE_PRIVATE:
16418 : 837 : need_complete = true;
16419 : 837 : need_implicitly_determined = true;
16420 : 837 : goto check_dup_generic;
16421 : :
16422 : 3516 : case OMP_CLAUSE_REDUCTION:
16423 : 3516 : if (reduction_seen == 0)
16424 : 2917 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16425 : 599 : else if (reduction_seen != -2
16426 : 1198 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16427 : 599 : ? -1 : 1))
16428 : : {
16429 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16430 : : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16431 : : "on the same construct");
16432 : 2 : reduction_seen = -2;
16433 : : }
16434 : : /* FALLTHRU */
16435 : 4179 : case OMP_CLAUSE_IN_REDUCTION:
16436 : 4179 : case OMP_CLAUSE_TASK_REDUCTION:
16437 : 4179 : need_implicitly_determined = true;
16438 : 4179 : t = OMP_CLAUSE_DECL (c);
16439 : 4179 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16440 : : {
16441 : 553 : if (handle_omp_array_sections (c, ort))
16442 : : {
16443 : : remove = true;
16444 : : break;
16445 : : }
16446 : :
16447 : 537 : t = OMP_CLAUSE_DECL (c);
16448 : 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16449 : 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16450 : : {
16451 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16452 : : "%<inscan%> %<reduction%> clause with array "
16453 : : "section");
16454 : 1 : remove = true;
16455 : 1 : break;
16456 : : }
16457 : : }
16458 : 4162 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16459 : 4162 : if (t == error_mark_node)
16460 : : {
16461 : : remove = true;
16462 : : break;
16463 : : }
16464 : 4160 : if (oacc_async)
16465 : 3 : c_mark_addressable (t);
16466 : 4160 : type = TREE_TYPE (t);
16467 : 4160 : if (TREE_CODE (t) == MEM_REF)
16468 : 536 : type = TREE_TYPE (type);
16469 : 4160 : if (TREE_CODE (type) == ARRAY_TYPE)
16470 : : {
16471 : 74 : tree oatype = type;
16472 : 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16473 : 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16474 : 74 : type = TREE_TYPE (type);
16475 : 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16476 : : {
16477 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16478 : : "%qD in %<reduction%> clause is a zero size array",
16479 : : t);
16480 : 1 : remove = true;
16481 : 1 : break;
16482 : : }
16483 : 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16484 : : TYPE_SIZE_UNIT (type));
16485 : 73 : if (integer_zerop (size))
16486 : : {
16487 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16488 : : "%qD in %<reduction%> clause is a zero size array",
16489 : : t);
16490 : 1 : remove = true;
16491 : 1 : break;
16492 : : }
16493 : 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16494 : 72 : size = save_expr (size);
16495 : 72 : tree index_type = build_index_type (size);
16496 : 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16497 : : index_type);
16498 : 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16499 : 72 : tree ptype = c_build_pointer_type (type);
16500 : 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16501 : 72 : t = build_fold_addr_expr (t);
16502 : 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16503 : 72 : OMP_CLAUSE_DECL (c) = t;
16504 : : }
16505 : 4158 : if (TYPE_ATOMIC (type))
16506 : : {
16507 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16508 : : "%<_Atomic%> %qE in %<reduction%> clause", t);
16509 : 3 : remove = true;
16510 : 3 : break;
16511 : : }
16512 : 4155 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16513 : 4155 : || OMP_CLAUSE_REDUCTION_TASK (c))
16514 : : {
16515 : : /* Disallow zero sized or potentially zero sized task
16516 : : reductions. */
16517 : 870 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16518 : : {
16519 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
16520 : : "zero sized type %qT in %qs clause", type,
16521 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16522 : 3 : remove = true;
16523 : 3 : break;
16524 : : }
16525 : 867 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16526 : : {
16527 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16528 : : "variable sized type %qT in %qs clause", type,
16529 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16530 : 0 : remove = true;
16531 : 0 : break;
16532 : : }
16533 : : }
16534 : 4152 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16535 : 4152 : && (FLOAT_TYPE_P (type)
16536 : 3600 : || TREE_CODE (type) == COMPLEX_TYPE))
16537 : : {
16538 : 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16539 : 340 : const char *r_name = NULL;
16540 : :
16541 : 340 : switch (r_code)
16542 : : {
16543 : : case PLUS_EXPR:
16544 : : case MULT_EXPR:
16545 : : case MINUS_EXPR:
16546 : : case TRUTH_ANDIF_EXPR:
16547 : : case TRUTH_ORIF_EXPR:
16548 : : break;
16549 : 13 : case MIN_EXPR:
16550 : 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16551 : : r_name = "min";
16552 : : break;
16553 : 38 : case MAX_EXPR:
16554 : 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16555 : : r_name = "max";
16556 : : break;
16557 : 3 : case BIT_AND_EXPR:
16558 : 3 : r_name = "&";
16559 : 3 : break;
16560 : 2 : case BIT_XOR_EXPR:
16561 : 2 : r_name = "^";
16562 : 2 : break;
16563 : : case BIT_IOR_EXPR:
16564 : : r_name = "|";
16565 : : break;
16566 : 0 : default:
16567 : 0 : gcc_unreachable ();
16568 : : }
16569 : 5 : if (r_name)
16570 : : {
16571 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
16572 : : "%qE has invalid type for %<reduction(%s)%>",
16573 : : t, r_name);
16574 : 12 : remove = true;
16575 : 12 : break;
16576 : : }
16577 : : }
16578 : 3812 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16579 : : {
16580 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16581 : : "user defined reduction not found for %qE", t);
16582 : 2 : remove = true;
16583 : 2 : break;
16584 : : }
16585 : 3810 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16586 : : {
16587 : 277 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16588 : 277 : type = TYPE_MAIN_VARIANT (type);
16589 : 277 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16590 : : VAR_DECL, NULL_TREE, type);
16591 : 277 : tree decl_placeholder = NULL_TREE;
16592 : 277 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16593 : 277 : DECL_ARTIFICIAL (placeholder) = 1;
16594 : 277 : DECL_IGNORED_P (placeholder) = 1;
16595 : 277 : if (TREE_CODE (t) == MEM_REF)
16596 : : {
16597 : 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16598 : : VAR_DECL, NULL_TREE, type);
16599 : 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16600 : 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16601 : 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16602 : : }
16603 : 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16604 : 42 : c_mark_addressable (placeholder);
16605 : 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16606 : 76 : c_mark_addressable (decl_placeholder ? decl_placeholder
16607 : 34 : : OMP_CLAUSE_DECL (c));
16608 : 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16609 : 775 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16610 : 277 : TREE_VEC_ELT (list, 0),
16611 : 277 : TREE_VEC_ELT (list, 1),
16612 : : decl_placeholder ? decl_placeholder
16613 : 221 : : OMP_CLAUSE_DECL (c), placeholder);
16614 : 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16615 : 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16616 : : void_type_node, NULL_TREE,
16617 : 277 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16618 : 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16619 : 277 : if (TREE_VEC_LENGTH (list) == 6)
16620 : : {
16621 : 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16622 : 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16623 : 33 : : OMP_CLAUSE_DECL (c));
16624 : 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16625 : 27 : c_mark_addressable (placeholder);
16626 : 233 : tree init = TREE_VEC_ELT (list, 5);
16627 : 233 : if (init == error_mark_node)
16628 : 197 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16629 : 233 : OMP_CLAUSE_REDUCTION_INIT (c)
16630 : 651 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16631 : 233 : TREE_VEC_ELT (list, 3),
16632 : : decl_placeholder ? decl_placeholder
16633 : 185 : : OMP_CLAUSE_DECL (c), placeholder);
16634 : 233 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16635 : : {
16636 : 197 : tree v = decl_placeholder ? decl_placeholder : t;
16637 : 197 : OMP_CLAUSE_REDUCTION_INIT (c)
16638 : 394 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16639 : 197 : OMP_CLAUSE_REDUCTION_INIT (c));
16640 : : }
16641 : 233 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16642 : : c_find_omp_placeholder_r,
16643 : : placeholder, NULL))
16644 : 28 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
16645 : : }
16646 : : else
16647 : : {
16648 : 44 : tree init;
16649 : 44 : tree v = decl_placeholder ? decl_placeholder : t;
16650 : 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
16651 : 34 : init = build_constructor (TREE_TYPE (v), NULL);
16652 : : else
16653 : 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
16654 : 44 : OMP_CLAUSE_REDUCTION_INIT (c)
16655 : 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
16656 : : }
16657 : 277 : OMP_CLAUSE_REDUCTION_INIT (c)
16658 : 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16659 : : void_type_node, NULL_TREE,
16660 : 277 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
16661 : 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
16662 : : }
16663 : 4138 : if (TREE_CODE (t) == MEM_REF)
16664 : : {
16665 : 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
16666 : 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
16667 : : != INTEGER_CST)
16668 : : {
16669 : 0 : sorry ("variable length element type in array "
16670 : : "%<reduction%> clause");
16671 : 0 : remove = true;
16672 : 0 : break;
16673 : : }
16674 : 607 : t = TREE_OPERAND (t, 0);
16675 : 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16676 : 139 : t = TREE_OPERAND (t, 0);
16677 : 607 : if (TREE_CODE (t) == ADDR_EXPR)
16678 : 367 : t = TREE_OPERAND (t, 0);
16679 : : }
16680 : 4138 : goto check_dup_generic_t;
16681 : :
16682 : 24 : case OMP_CLAUSE_COPYPRIVATE:
16683 : 24 : copyprivate_seen = true;
16684 : 24 : if (nowait_clause)
16685 : : {
16686 : 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
16687 : : "%<nowait%> clause must not be used together "
16688 : : "with %<copyprivate%> clause");
16689 : 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
16690 : 1 : nowait_clause = NULL;
16691 : : }
16692 : 24 : goto check_dup_generic;
16693 : :
16694 : 99 : case OMP_CLAUSE_COPYIN:
16695 : 99 : t = OMP_CLAUSE_DECL (c);
16696 : 99 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
16697 : : {
16698 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
16699 : : "%qE must be %<threadprivate%> for %<copyin%>", t);
16700 : 5 : remove = true;
16701 : 5 : break;
16702 : : }
16703 : 94 : goto check_dup_generic;
16704 : :
16705 : 478 : case OMP_CLAUSE_LINEAR:
16706 : 478 : if (ort != C_ORT_OMP_DECLARE_SIMD)
16707 : 327 : need_implicitly_determined = true;
16708 : 478 : t = OMP_CLAUSE_DECL (c);
16709 : 478 : if (ort != C_ORT_OMP_DECLARE_SIMD
16710 : 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
16711 : 483 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
16712 : : {
16713 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
16714 : : "modifier should not be specified in %<linear%> "
16715 : : "clause on %<simd%> or %<for%> constructs when not "
16716 : : "using OpenMP 5.2 modifiers");
16717 : 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
16718 : : }
16719 : 956 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
16720 : 501 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16721 : : {
16722 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16723 : : "linear clause applied to non-integral non-pointer "
16724 : 1 : "variable with type %qT", TREE_TYPE (t));
16725 : 1 : remove = true;
16726 : 1 : break;
16727 : : }
16728 : 477 : if (TYPE_ATOMIC (TREE_TYPE (t)))
16729 : : {
16730 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16731 : : "%<_Atomic%> %qD in %<linear%> clause", t);
16732 : 3 : remove = true;
16733 : 3 : break;
16734 : : }
16735 : 474 : if (ort == C_ORT_OMP_DECLARE_SIMD)
16736 : : {
16737 : 150 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
16738 : 150 : if (TREE_CODE (s) == PARM_DECL)
16739 : : {
16740 : 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
16741 : : /* map_head bitmap is used as uniform_head if
16742 : : declare_simd. */
16743 : 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
16744 : 5 : linear_variable_step_check = true;
16745 : 9 : goto check_dup_generic;
16746 : : }
16747 : 141 : if (TREE_CODE (s) != INTEGER_CST)
16748 : : {
16749 : 7 : error_at (OMP_CLAUSE_LOCATION (c),
16750 : : "%<linear%> clause step %qE is neither constant "
16751 : : "nor a parameter", s);
16752 : 7 : remove = true;
16753 : 7 : break;
16754 : : }
16755 : : }
16756 : 458 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
16757 : : {
16758 : 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
16759 : 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
16760 : 20 : OMP_CLAUSE_DECL (c), s);
16761 : 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16762 : : sizetype, fold_convert (sizetype, s),
16763 : 20 : fold_convert
16764 : : (sizetype, OMP_CLAUSE_DECL (c)));
16765 : 20 : if (s == error_mark_node)
16766 : 0 : s = size_one_node;
16767 : 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
16768 : : }
16769 : : else
16770 : 438 : OMP_CLAUSE_LINEAR_STEP (c)
16771 : 876 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
16772 : 458 : goto check_dup_generic;
16773 : :
16774 : 2872 : check_dup_generic:
16775 : 2872 : t = OMP_CLAUSE_DECL (c);
16776 : 7098 : check_dup_generic_t:
16777 : 7098 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16778 : : {
16779 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
16780 : : "%qE is not a variable in clause %qs", t,
16781 : 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16782 : 2 : remove = true;
16783 : : }
16784 : 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
16785 : 6057 : || (ort == C_ORT_OMP
16786 : 5349 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
16787 : 5338 : || (OMP_CLAUSE_CODE (c)
16788 : : == OMP_CLAUSE_USE_DEVICE_ADDR)))
16789 : 13106 : || (ort == C_ORT_OMP_TARGET
16790 : 338 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
16791 : : {
16792 : 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16793 : 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
16794 : 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
16795 : : {
16796 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16797 : : "%qD appears more than once in data-sharing "
16798 : : "clauses", t);
16799 : 2 : remove = true;
16800 : 2 : break;
16801 : : }
16802 : 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
16803 : 108 : target_in_reduction_seen = true;
16804 : 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16805 : : {
16806 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
16807 : : openacc
16808 : : ? "%qD appears more than once in reduction clauses"
16809 : : : "%qD appears more than once in data clauses",
16810 : : t);
16811 : 2 : remove = true;
16812 : : }
16813 : : else
16814 : 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
16815 : : }
16816 : 5900 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16817 : 5878 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
16818 : 5877 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
16819 : 11777 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16820 : : {
16821 : 23 : error_at (OMP_CLAUSE_LOCATION (c),
16822 : : "%qE appears more than once in data clauses", t);
16823 : 23 : remove = true;
16824 : : }
16825 : 5877 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16826 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
16827 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
16828 : 1036 : && bitmap_bit_p (&map_head, DECL_UID (t)))
16829 : : {
16830 : 3 : if (openacc)
16831 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16832 : : "%qD appears more than once in data clauses", t);
16833 : : else
16834 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16835 : : "%qD appears both in data and map clauses", t);
16836 : : remove = true;
16837 : : }
16838 : : else
16839 : 5874 : bitmap_set_bit (&generic_head, DECL_UID (t));
16840 : : break;
16841 : :
16842 : 1320 : case OMP_CLAUSE_FIRSTPRIVATE:
16843 : 1320 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
16844 : : {
16845 : 186 : move_implicit:
16846 : 186 : implicit_moved = true;
16847 : : /* Move firstprivate and map clauses with
16848 : : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
16849 : : clauses chain. */
16850 : 186 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
16851 : 186 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
16852 : 1072 : while (*pc1)
16853 : 886 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
16854 : 886 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
16855 : : {
16856 : 113 : *pc3 = *pc1;
16857 : 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
16858 : 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
16859 : : }
16860 : 773 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
16861 : 773 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
16862 : : {
16863 : 164 : *pc2 = *pc1;
16864 : 164 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
16865 : 164 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
16866 : : }
16867 : : else
16868 : 609 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
16869 : 186 : *pc3 = NULL;
16870 : 186 : *pc2 = cl2;
16871 : 186 : *pc1 = cl1;
16872 : 186 : continue;
16873 : 186 : }
16874 : 1225 : t = OMP_CLAUSE_DECL (c);
16875 : 1225 : need_complete = true;
16876 : 1225 : need_implicitly_determined = true;
16877 : 1225 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16878 : : {
16879 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16880 : : "%qE is not a variable in clause %<firstprivate%>", t);
16881 : 1 : remove = true;
16882 : : }
16883 : 1224 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
16884 : 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
16885 : 1330 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16886 : : remove = true;
16887 : 1224 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16888 : 1222 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
16889 : 2445 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16890 : : {
16891 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
16892 : : "%qE appears more than once in data clauses", t);
16893 : 4 : remove = true;
16894 : : }
16895 : 1220 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
16896 : 1220 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
16897 : : {
16898 : 7 : if (openacc)
16899 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16900 : : "%qD appears more than once in data clauses", t);
16901 : 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
16902 : 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
16903 : : /* Silently drop the clause. */;
16904 : : else
16905 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
16906 : : "%qD appears both in data and map clauses", t);
16907 : : remove = true;
16908 : : }
16909 : : else
16910 : 1213 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
16911 : : break;
16912 : :
16913 : 1522 : case OMP_CLAUSE_LASTPRIVATE:
16914 : 1522 : t = OMP_CLAUSE_DECL (c);
16915 : 1522 : need_complete = true;
16916 : 1522 : need_implicitly_determined = true;
16917 : 1522 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16918 : : {
16919 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16920 : : "%qE is not a variable in clause %<lastprivate%>", t);
16921 : 0 : remove = true;
16922 : : }
16923 : 1522 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16924 : 1522 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
16925 : : {
16926 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16927 : : "%qE appears more than once in data clauses", t);
16928 : 3 : remove = true;
16929 : : }
16930 : : else
16931 : 1519 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
16932 : : break;
16933 : :
16934 : 253 : case OMP_CLAUSE_ALIGNED:
16935 : 253 : t = OMP_CLAUSE_DECL (c);
16936 : 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16937 : : {
16938 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16939 : : "%qE is not a variable in %<aligned%> clause", t);
16940 : 0 : remove = true;
16941 : : }
16942 : 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
16943 : 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
16944 : : {
16945 : 7 : error_at (OMP_CLAUSE_LOCATION (c),
16946 : : "%qE in %<aligned%> clause is neither a pointer nor "
16947 : : "an array", t);
16948 : 7 : remove = true;
16949 : : }
16950 : 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
16951 : : {
16952 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16953 : : "%<_Atomic%> %qD in %<aligned%> clause", t);
16954 : 1 : remove = true;
16955 : 1 : break;
16956 : : }
16957 : 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
16958 : : {
16959 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16960 : : "%qE appears more than once in %<aligned%> clauses",
16961 : : t);
16962 : 0 : remove = true;
16963 : : }
16964 : : else
16965 : 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
16966 : : break;
16967 : :
16968 : 125 : case OMP_CLAUSE_NONTEMPORAL:
16969 : 125 : t = OMP_CLAUSE_DECL (c);
16970 : 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16971 : : {
16972 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16973 : : "%qE is not a variable in %<nontemporal%> clause", t);
16974 : 0 : remove = true;
16975 : : }
16976 : 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16977 : : {
16978 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16979 : : "%qE appears more than once in %<nontemporal%> "
16980 : : "clauses", t);
16981 : 2 : remove = true;
16982 : : }
16983 : : else
16984 : 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
16985 : : break;
16986 : :
16987 : 786 : case OMP_CLAUSE_ALLOCATE:
16988 : 786 : t = OMP_CLAUSE_DECL (c);
16989 : 786 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16990 : : {
16991 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16992 : : "%qE is not a variable in %<allocate%> clause", t);
16993 : 1 : remove = true;
16994 : : }
16995 : 785 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
16996 : : {
16997 : 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
16998 : : "%qE appears more than once in %<allocate%> clauses",
16999 : : t);
17000 : 2 : remove = true;
17001 : : }
17002 : : else
17003 : : {
17004 : 783 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17005 : 783 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17006 : 684 : allocate_seen = true;
17007 : : }
17008 : : break;
17009 : :
17010 : 355 : case OMP_CLAUSE_DOACROSS:
17011 : 355 : t = OMP_CLAUSE_DECL (c);
17012 : 355 : if (t == NULL_TREE)
17013 : : break;
17014 : 168 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17015 : : {
17016 : 168 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17017 : 1313 : for (; t; t = TREE_CHAIN (t))
17018 : : {
17019 : 1145 : tree decl = TREE_VALUE (t);
17020 : 1145 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17021 : : {
17022 : 3 : tree offset = TREE_PURPOSE (t);
17023 : 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17024 : 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17025 : 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17026 : : neg ? MINUS_EXPR : PLUS_EXPR,
17027 : : decl, offset);
17028 : 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17029 : : sizetype,
17030 : : fold_convert (sizetype, t2),
17031 : : fold_convert (sizetype, decl));
17032 : 3 : if (t2 == error_mark_node)
17033 : : {
17034 : : remove = true;
17035 : : break;
17036 : : }
17037 : 3 : TREE_PURPOSE (t) = t2;
17038 : : }
17039 : : }
17040 : : break;
17041 : : }
17042 : 0 : gcc_unreachable ();
17043 : 713 : case OMP_CLAUSE_DEPEND:
17044 : 713 : depend_clause = c;
17045 : : /* FALLTHRU */
17046 : 870 : case OMP_CLAUSE_AFFINITY:
17047 : 870 : t = OMP_CLAUSE_DECL (c);
17048 : 870 : if (OMP_ITERATOR_DECL_P (t))
17049 : : {
17050 : 131 : if (TREE_PURPOSE (t) != last_iterators)
17051 : 111 : last_iterators_remove
17052 : 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17053 : 131 : last_iterators = TREE_PURPOSE (t);
17054 : 131 : t = TREE_VALUE (t);
17055 : 131 : if (last_iterators_remove)
17056 : 34 : t = error_mark_node;
17057 : : }
17058 : : else
17059 : : last_iterators = NULL_TREE;
17060 : 870 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17061 : : {
17062 : 413 : if (handle_omp_array_sections (c, ort))
17063 : : remove = true;
17064 : 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17065 : 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17066 : : {
17067 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17068 : : "%<depend%> clause with %<depobj%> dependence "
17069 : : "type on array section");
17070 : 1 : remove = true;
17071 : : }
17072 : : break;
17073 : : }
17074 : 457 : if (t == error_mark_node)
17075 : : remove = true;
17076 : 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17077 : 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17078 : : {
17079 : 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17080 : 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17081 : : {
17082 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17083 : : "%<omp_all_memory%> used with %<depend%> kind "
17084 : : "other than %<out%> or %<inout%>");
17085 : 3 : remove = true;
17086 : : }
17087 : : }
17088 : 408 : else if (!lvalue_p (t))
17089 : : {
17090 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17091 : : "%qE is not lvalue expression nor array section in "
17092 : : "%qs clause", t,
17093 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17094 : 3 : remove = true;
17095 : : }
17096 : 405 : else if (TREE_CODE (t) == COMPONENT_REF
17097 : 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17098 : : {
17099 : 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17100 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17101 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17102 : : "bit-field %qE in %qs clause", t,
17103 : 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17104 : 2 : remove = true;
17105 : : }
17106 : 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17107 : 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17108 : : {
17109 : 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17110 : : {
17111 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17112 : : "%qE does not have %<omp_depend_t%> type in "
17113 : : "%<depend%> clause with %<depobj%> dependence "
17114 : : "type", t);
17115 : 2 : remove = true;
17116 : : }
17117 : : }
17118 : 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17119 : 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17120 : : {
17121 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17122 : : "%qE should not have %<omp_depend_t%> type in "
17123 : : "%<depend%> clause with dependence type other than "
17124 : : "%<depobj%>", t);
17125 : 2 : remove = true;
17126 : : }
17127 : 12 : if (!remove)
17128 : : {
17129 : 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17130 : 12 : t = null_pointer_node;
17131 : : else
17132 : : {
17133 : 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17134 : : ADDR_EXPR, t, false);
17135 : 399 : if (addr == error_mark_node)
17136 : : {
17137 : : remove = true;
17138 : : break;
17139 : : }
17140 : 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17141 : : RO_UNARY_STAR);
17142 : 399 : if (t == error_mark_node)
17143 : : {
17144 : : remove = true;
17145 : : break;
17146 : : }
17147 : : }
17148 : 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17149 : 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17150 : : else
17151 : 380 : OMP_CLAUSE_DECL (c) = t;
17152 : : }
17153 : : break;
17154 : :
17155 : 6508 : case OMP_CLAUSE_MAP:
17156 : 6508 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17157 : 91 : goto move_implicit;
17158 : 6417 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17159 : 6417 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17160 : : {
17161 : : remove = true;
17162 : : break;
17163 : : }
17164 : : /* FALLTHRU */
17165 : 9270 : case OMP_CLAUSE_TO:
17166 : 9270 : case OMP_CLAUSE_FROM:
17167 : 9270 : if (OMP_CLAUSE_ITERATORS (c)
17168 : 9270 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17169 : : {
17170 : 22609 : t = error_mark_node;
17171 : : break;
17172 : : }
17173 : : /* FALLTHRU */
17174 : 9406 : case OMP_CLAUSE__CACHE_:
17175 : 9406 : {
17176 : 9406 : using namespace omp_addr_tokenizer;
17177 : 9406 : auto_vec<omp_addr_token *, 10> addr_tokens;
17178 : :
17179 : 9406 : t = OMP_CLAUSE_DECL (c);
17180 : 9406 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17181 : : {
17182 : 1993 : grp_start_p = pc;
17183 : 1993 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17184 : :
17185 : 1993 : if (handle_omp_array_sections (c, ort))
17186 : : remove = true;
17187 : : else
17188 : : {
17189 : 1853 : t = OMP_CLAUSE_DECL (c);
17190 : 1853 : if (!omp_mappable_type (TREE_TYPE (t)))
17191 : : {
17192 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17193 : : "array section does not have mappable type "
17194 : : "in %qs clause",
17195 : 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17196 : 1 : remove = true;
17197 : : }
17198 : 1852 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17199 : : {
17200 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17201 : : "%<_Atomic%> %qE in %qs clause", t,
17202 : 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17203 : 1 : remove = true;
17204 : : }
17205 : 2496 : while (TREE_CODE (t) == ARRAY_REF)
17206 : 643 : t = TREE_OPERAND (t, 0);
17207 : :
17208 : 1853 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17209 : :
17210 : 1853 : if (!omp_parse_expr (addr_tokens, t))
17211 : : {
17212 : 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17213 : : "unsupported map expression %qE",
17214 : 0 : OMP_CLAUSE_DECL (c));
17215 : 0 : remove = true;
17216 : 0 : break;
17217 : : }
17218 : :
17219 : : /* This check is to determine if this will be the only map
17220 : : node created for this clause. Otherwise, we'll check
17221 : : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17222 : : node on the next iteration(s) of the loop. */
17223 : 3688 : if (addr_tokens.length () >= 4
17224 : 304 : && addr_tokens[0]->type == STRUCTURE_BASE
17225 : 302 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17226 : 302 : && addr_tokens[1]->type == ACCESS_METHOD
17227 : 302 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17228 : 302 : && addr_tokens[3]->type == ACCESS_METHOD
17229 : 2094 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17230 : 188 : || (addr_tokens[3]->u.access_kind
17231 : : == ACCESS_INDEXED_ARRAY)))
17232 : : {
17233 : 55 : tree rt = addr_tokens[1]->expr;
17234 : :
17235 : 55 : gcc_assert (DECL_P (rt));
17236 : :
17237 : 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17238 : 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17239 : 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17240 : 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17241 : 0 : || bitmap_bit_p (&map_firstprivate_head,
17242 : 0 : DECL_UID (rt))))
17243 : : {
17244 : : remove = true;
17245 : : break;
17246 : : }
17247 : 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17248 : : break;
17249 : 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17250 : : {
17251 : 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17252 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17253 : : "%qD appears more than once in motion "
17254 : : "clauses", rt);
17255 : 0 : else if (openacc)
17256 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17257 : : "%qD appears more than once in data "
17258 : : "clauses", rt);
17259 : : else
17260 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17261 : : "%qD appears more than once in map "
17262 : : "clauses", rt);
17263 : : remove = true;
17264 : : }
17265 : : else
17266 : : {
17267 : 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17268 : 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17269 : : }
17270 : : }
17271 : 1853 : }
17272 : 1975 : if (c_oacc_check_attachments (c))
17273 : 2 : remove = true;
17274 : 1975 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17275 : 1799 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17276 : 1781 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17277 : 2000 : && !OMP_CLAUSE_SIZE (c))
17278 : : /* In this case, we have a single array element which is a
17279 : : pointer, and we already set OMP_CLAUSE_SIZE in
17280 : : handle_omp_array_sections above. For attach/detach
17281 : : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17282 : : to zero here. */
17283 : 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17284 : : break;
17285 : : }
17286 : 7413 : else if (!omp_parse_expr (addr_tokens, t))
17287 : : {
17288 : 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17289 : : "unsupported map expression %qE",
17290 : 0 : OMP_CLAUSE_DECL (c));
17291 : 0 : remove = true;
17292 : 0 : break;
17293 : : }
17294 : 7413 : if (t == error_mark_node)
17295 : : {
17296 : : remove = true;
17297 : : break;
17298 : : }
17299 : : /* OpenACC attach / detach clauses must be pointers. */
17300 : 7407 : if (c_oacc_check_attachments (c))
17301 : : {
17302 : : remove = true;
17303 : : break;
17304 : : }
17305 : 7403 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17306 : 4584 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17307 : 4557 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17308 : 7445 : && !OMP_CLAUSE_SIZE (c))
17309 : : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17310 : : bias) to zero here, so it is not set erroneously to the pointer
17311 : : size later on in gimplify.cc. */
17312 : 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17313 : :
17314 : 7403 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17315 : :
17316 : 7403 : if (!ai.check_clause (c))
17317 : : {
17318 : : remove = true;
17319 : : break;
17320 : : }
17321 : :
17322 : 7395 : if (!ai.map_supported_p ())
17323 : : {
17324 : 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17325 : : "unsupported map expression %qE",
17326 : 7 : OMP_CLAUSE_DECL (c));
17327 : 7 : remove = true;
17328 : 7 : break;
17329 : : }
17330 : :
17331 : 14776 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17332 : : || addr_tokens[0]->type == STRUCTURE_BASE)
17333 : : && addr_tokens[1]->type == ACCESS_METHOD);
17334 : :
17335 : 7388 : t = addr_tokens[1]->expr;
17336 : :
17337 : 7388 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17338 : 0 : goto skip_decl_checks;
17339 : :
17340 : : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17341 : : mapping. OpenACC allows multiple fields of the same structure
17342 : : to be written. */
17343 : 7388 : if (addr_tokens[0]->type == STRUCTURE_BASE
17344 : 7388 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17345 : 238 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17346 : 246 : goto skip_decl_checks;
17347 : :
17348 : 7142 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17349 : : {
17350 : 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17351 : : break;
17352 : :
17353 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17354 : : "%qE is not a variable in %qs clause", t,
17355 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17356 : 3 : remove = true;
17357 : : }
17358 : 7139 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17359 : : {
17360 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17361 : : "%qD is threadprivate variable in %qs clause", t,
17362 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17363 : 0 : remove = true;
17364 : : }
17365 : 7139 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17366 : 4327 : || (OMP_CLAUSE_MAP_KIND (c)
17367 : : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17368 : 10430 : && !c_mark_addressable (t))
17369 : : remove = true;
17370 : 7139 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17371 : 4327 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17372 : 4327 : || (OMP_CLAUSE_MAP_KIND (c)
17373 : : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17374 : 3291 : || (OMP_CLAUSE_MAP_KIND (c)
17375 : : == GOMP_MAP_FORCE_DEVICEPTR)))
17376 : 6045 : && t == OMP_CLAUSE_DECL (c)
17377 : 12948 : && !omp_mappable_type (TREE_TYPE (t)))
17378 : : {
17379 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
17380 : : "%qD does not have a mappable type in %qs clause", t,
17381 : 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17382 : 8 : remove = true;
17383 : : }
17384 : 7131 : else if (TREE_TYPE (t) == error_mark_node)
17385 : : remove = true;
17386 : 7130 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17387 : : {
17388 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
17389 : : "%<_Atomic%> %qE in %qs clause", t,
17390 : 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17391 : 4 : remove = true;
17392 : : }
17393 : 7126 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17394 : 4317 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17395 : 7290 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17396 : 128 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17397 : 128 : || bitmap_bit_p (&map_firstprivate_head,
17398 : 128 : DECL_UID (t))))
17399 : : remove = true;
17400 : 7089 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17401 : 7089 : && (OMP_CLAUSE_MAP_KIND (c)
17402 : : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17403 : : {
17404 : 1034 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17405 : 1034 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17406 : 2067 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17407 : : {
17408 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
17409 : : "%qD appears more than once in data clauses", t);
17410 : 5 : remove = true;
17411 : : }
17412 : 1029 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17413 : 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17414 : 1031 : && openacc)
17415 : : {
17416 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17417 : : "%qD appears more than once in data clauses", t);
17418 : 1 : remove = true;
17419 : : }
17420 : : else
17421 : 1028 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17422 : : }
17423 : 6055 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17424 : 59 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17425 : 13 : && ort != C_ORT_OMP
17426 : 6068 : && ort != C_ORT_OMP_EXIT_DATA)
17427 : : {
17428 : 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17429 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17430 : : "%qD appears more than once in motion clauses", t);
17431 : 10 : else if (openacc)
17432 : 7 : error_at (OMP_CLAUSE_LOCATION (c),
17433 : : "%qD appears more than once in data clauses", t);
17434 : : else
17435 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17436 : : "%qD appears more than once in map clauses", t);
17437 : : remove = true;
17438 : : }
17439 : 6045 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17440 : : {
17441 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17442 : : "%qD appears more than once in data clauses", t);
17443 : 0 : remove = true;
17444 : : }
17445 : 6045 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17446 : 6045 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17447 : : {
17448 : 9 : if (openacc)
17449 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17450 : : "%qD appears more than once in data clauses", t);
17451 : : else
17452 : 9 : error_at (OMP_CLAUSE_LOCATION (c),
17453 : : "%qD appears both in data and map clauses", t);
17454 : : remove = true;
17455 : : }
17456 : 6036 : else if (!omp_access_chain_p (addr_tokens, 1))
17457 : : {
17458 : 6036 : bitmap_set_bit (&map_head, DECL_UID (t));
17459 : 6036 : if (t != OMP_CLAUSE_DECL (c)
17460 : 6036 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17461 : 232 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17462 : : }
17463 : :
17464 : 0 : skip_decl_checks:
17465 : : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17466 : : the containing loop (here) iterates through the new nodes
17467 : : created by that expansion. Avoid expanding those again (just
17468 : : by checking the node type). */
17469 : 7388 : if (!remove
17470 : 7388 : && ort != C_ORT_DECLARE_SIMD
17471 : 7388 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17472 : 4495 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17473 : 3467 : && (OMP_CLAUSE_MAP_KIND (c)
17474 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17475 : 3467 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17476 : 3467 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17477 : 3168 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17478 : 3141 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17479 : : {
17480 : 5941 : grp_start_p = pc;
17481 : 5941 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17482 : 5941 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17483 : : addr_tokens, ort);
17484 : 5941 : if (nc != error_mark_node)
17485 : 5941 : c = nc;
17486 : : }
17487 : 9421 : }
17488 : 7388 : break;
17489 : :
17490 : 223 : case OMP_CLAUSE_ENTER:
17491 : 223 : case OMP_CLAUSE_LINK:
17492 : 223 : t = OMP_CLAUSE_DECL (c);
17493 : 223 : const char *cname;
17494 : 223 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17495 : 223 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17496 : 223 : && OMP_CLAUSE_ENTER_TO (c))
17497 : : cname = "to";
17498 : 223 : if (TREE_CODE (t) == FUNCTION_DECL
17499 : 223 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17500 : : ;
17501 : 145 : else if (!VAR_P (t))
17502 : : {
17503 : 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17504 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17505 : : "%qE is neither a variable nor a function name in "
17506 : : "clause %qs", t, cname);
17507 : : else
17508 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17509 : : "%qE is not a variable in clause %qs", t, cname);
17510 : : remove = true;
17511 : : }
17512 : 144 : else if (DECL_THREAD_LOCAL_P (t))
17513 : : {
17514 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17515 : : "%qD is threadprivate variable in %qs clause", t,
17516 : : cname);
17517 : 2 : remove = true;
17518 : : }
17519 : 142 : else if (!omp_mappable_type (TREE_TYPE (t)))
17520 : : {
17521 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17522 : : "%qD does not have a mappable type in %qs clause", t,
17523 : : cname);
17524 : 6 : remove = true;
17525 : : }
17526 : 8 : if (remove)
17527 : : break;
17528 : 214 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17529 : : {
17530 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
17531 : : "%qE appears more than once on the same "
17532 : : "%<declare target%> directive", t);
17533 : 8 : remove = true;
17534 : : }
17535 : : else
17536 : 206 : bitmap_set_bit (&generic_head, DECL_UID (t));
17537 : : break;
17538 : :
17539 : 117 : case OMP_CLAUSE_UNIFORM:
17540 : 117 : t = OMP_CLAUSE_DECL (c);
17541 : 117 : if (TREE_CODE (t) != PARM_DECL)
17542 : : {
17543 : 0 : if (DECL_P (t))
17544 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17545 : : "%qD is not an argument in %<uniform%> clause", t);
17546 : : else
17547 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17548 : : "%qE is not an argument in %<uniform%> clause", t);
17549 : : remove = true;
17550 : : break;
17551 : : }
17552 : : /* map_head bitmap is used as uniform_head if declare_simd. */
17553 : 117 : bitmap_set_bit (&map_head, DECL_UID (t));
17554 : 117 : goto check_dup_generic;
17555 : :
17556 : 160 : case OMP_CLAUSE_IS_DEVICE_PTR:
17557 : 160 : case OMP_CLAUSE_USE_DEVICE_PTR:
17558 : 160 : t = OMP_CLAUSE_DECL (c);
17559 : 160 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17560 : 122 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
17561 : 160 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17562 : : {
17563 : 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17564 : 21 : && !openacc)
17565 : : {
17566 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17567 : : "%qs variable is not a pointer",
17568 : 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17569 : 1 : remove = true;
17570 : : }
17571 : 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17572 : : {
17573 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
17574 : : "%qs variable is neither a pointer nor an array",
17575 : 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17576 : 4 : remove = true;
17577 : : }
17578 : : }
17579 : 160 : goto check_dup_generic;
17580 : :
17581 : 88 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
17582 : 88 : t = OMP_CLAUSE_DECL (c);
17583 : 88 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17584 : : {
17585 : 11 : if (handle_omp_array_sections (c, ort))
17586 : : remove = true;
17587 : : else
17588 : : {
17589 : 11 : t = OMP_CLAUSE_DECL (c);
17590 : 24 : while (TREE_CODE (t) == ARRAY_REF)
17591 : 13 : t = TREE_OPERAND (t, 0);
17592 : : }
17593 : : }
17594 : 88 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
17595 : 88 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17596 : 88 : c_mark_addressable (t);
17597 : 88 : goto check_dup_generic_t;
17598 : :
17599 : 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
17600 : 36 : t = OMP_CLAUSE_DECL (c);
17601 : 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17602 : 36 : c_mark_addressable (t);
17603 : 36 : goto check_dup_generic;
17604 : :
17605 : 4789 : case OMP_CLAUSE_NOWAIT:
17606 : 4789 : if (copyprivate_seen)
17607 : : {
17608 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17609 : : "%<nowait%> clause must not be used together "
17610 : : "with %<copyprivate%> clause");
17611 : 1 : remove = true;
17612 : 1 : break;
17613 : : }
17614 : 4788 : nowait_clause = pc;
17615 : 4788 : pc = &OMP_CLAUSE_CHAIN (c);
17616 : 4788 : continue;
17617 : :
17618 : 522 : case OMP_CLAUSE_ORDER:
17619 : 522 : if (ordered_clause)
17620 : : {
17621 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17622 : : "%<order%> clause must not be used together "
17623 : : "with %<ordered%> clause");
17624 : 2 : remove = true;
17625 : 2 : break;
17626 : : }
17627 : 520 : else if (order_clause)
17628 : : {
17629 : : /* Silently remove duplicates. */
17630 : : remove = true;
17631 : : break;
17632 : : }
17633 : 512 : order_clause = pc;
17634 : 512 : pc = &OMP_CLAUSE_CHAIN (c);
17635 : 512 : continue;
17636 : :
17637 : 32 : case OMP_CLAUSE_DETACH:
17638 : 32 : t = OMP_CLAUSE_DECL (c);
17639 : 32 : if (detach_seen)
17640 : : {
17641 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17642 : : "too many %qs clauses on a task construct",
17643 : : "detach");
17644 : 1 : remove = true;
17645 : 1 : break;
17646 : : }
17647 : 31 : detach_seen = pc;
17648 : 31 : pc = &OMP_CLAUSE_CHAIN (c);
17649 : 31 : c_mark_addressable (t);
17650 : 31 : continue;
17651 : :
17652 : 14518 : case OMP_CLAUSE_IF:
17653 : 14518 : case OMP_CLAUSE_SELF:
17654 : 14518 : case OMP_CLAUSE_NUM_THREADS:
17655 : 14518 : case OMP_CLAUSE_NUM_TEAMS:
17656 : 14518 : case OMP_CLAUSE_THREAD_LIMIT:
17657 : 14518 : case OMP_CLAUSE_DEFAULT:
17658 : 14518 : case OMP_CLAUSE_UNTIED:
17659 : 14518 : case OMP_CLAUSE_COLLAPSE:
17660 : 14518 : case OMP_CLAUSE_FINAL:
17661 : 14518 : case OMP_CLAUSE_DEVICE:
17662 : 14518 : case OMP_CLAUSE_DIST_SCHEDULE:
17663 : 14518 : case OMP_CLAUSE_PARALLEL:
17664 : 14518 : case OMP_CLAUSE_FOR:
17665 : 14518 : case OMP_CLAUSE_SECTIONS:
17666 : 14518 : case OMP_CLAUSE_TASKGROUP:
17667 : 14518 : case OMP_CLAUSE_PROC_BIND:
17668 : 14518 : case OMP_CLAUSE_DEVICE_TYPE:
17669 : 14518 : case OMP_CLAUSE_PRIORITY:
17670 : 14518 : case OMP_CLAUSE_THREADS:
17671 : 14518 : case OMP_CLAUSE_SIMD:
17672 : 14518 : case OMP_CLAUSE_HINT:
17673 : 14518 : case OMP_CLAUSE_FILTER:
17674 : 14518 : case OMP_CLAUSE_DEFAULTMAP:
17675 : 14518 : case OMP_CLAUSE_BIND:
17676 : 14518 : case OMP_CLAUSE_NUM_GANGS:
17677 : 14518 : case OMP_CLAUSE_NUM_WORKERS:
17678 : 14518 : case OMP_CLAUSE_VECTOR_LENGTH:
17679 : 14518 : case OMP_CLAUSE_ASYNC:
17680 : 14518 : case OMP_CLAUSE_WAIT:
17681 : 14518 : case OMP_CLAUSE_AUTO:
17682 : 14518 : case OMP_CLAUSE_INDEPENDENT:
17683 : 14518 : case OMP_CLAUSE_SEQ:
17684 : 14518 : case OMP_CLAUSE_GANG:
17685 : 14518 : case OMP_CLAUSE_WORKER:
17686 : 14518 : case OMP_CLAUSE_VECTOR:
17687 : 14518 : case OMP_CLAUSE_TILE:
17688 : 14518 : case OMP_CLAUSE_IF_PRESENT:
17689 : 14518 : case OMP_CLAUSE_FINALIZE:
17690 : 14518 : case OMP_CLAUSE_NOHOST:
17691 : 14518 : case OMP_CLAUSE_INDIRECT:
17692 : 14518 : case OMP_CLAUSE_NOVARIANTS:
17693 : 14518 : case OMP_CLAUSE_NOCONTEXT:
17694 : 14518 : pc = &OMP_CLAUSE_CHAIN (c);
17695 : 14518 : continue;
17696 : :
17697 : 62 : case OMP_CLAUSE_GRAINSIZE:
17698 : 62 : grainsize_seen = pc;
17699 : 62 : pc = &OMP_CLAUSE_CHAIN (c);
17700 : 62 : continue;
17701 : :
17702 : 49 : case OMP_CLAUSE_NUM_TASKS:
17703 : 49 : num_tasks_seen = true;
17704 : 49 : pc = &OMP_CLAUSE_CHAIN (c);
17705 : 49 : continue;
17706 : :
17707 : 78 : case OMP_CLAUSE_MERGEABLE:
17708 : 78 : mergeable_seen = true;
17709 : 78 : pc = &OMP_CLAUSE_CHAIN (c);
17710 : 78 : continue;
17711 : :
17712 : 18 : case OMP_CLAUSE_NOGROUP:
17713 : 18 : nogroup_seen = pc;
17714 : 18 : pc = &OMP_CLAUSE_CHAIN (c);
17715 : 18 : continue;
17716 : :
17717 : 3463 : case OMP_CLAUSE_SCHEDULE:
17718 : 3463 : schedule_clause = c;
17719 : 3463 : pc = &OMP_CLAUSE_CHAIN (c);
17720 : 3463 : continue;
17721 : :
17722 : 303 : case OMP_CLAUSE_ORDERED:
17723 : 303 : ordered_clause = c;
17724 : 303 : if (order_clause)
17725 : : {
17726 : 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
17727 : : "%<order%> clause must not be used together "
17728 : : "with %<ordered%> clause");
17729 : 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
17730 : 2 : order_clause = NULL;
17731 : : }
17732 : 303 : pc = &OMP_CLAUSE_CHAIN (c);
17733 : 303 : continue;
17734 : :
17735 : 223 : case OMP_CLAUSE_SAFELEN:
17736 : 223 : safelen = c;
17737 : 223 : pc = &OMP_CLAUSE_CHAIN (c);
17738 : 223 : continue;
17739 : 313 : case OMP_CLAUSE_SIMDLEN:
17740 : 313 : simdlen = c;
17741 : 313 : pc = &OMP_CLAUSE_CHAIN (c);
17742 : 313 : continue;
17743 : :
17744 : 69 : case OMP_CLAUSE_FULL:
17745 : 69 : full_seen = pc;
17746 : 69 : pc = &OMP_CLAUSE_CHAIN (c);
17747 : 69 : continue;
17748 : :
17749 : 223 : case OMP_CLAUSE_PARTIAL:
17750 : 223 : partial_seen = true;
17751 : 223 : pc = &OMP_CLAUSE_CHAIN (c);
17752 : 223 : continue;
17753 : :
17754 : 0 : case OMP_CLAUSE_SIZES:
17755 : 0 : pc = &OMP_CLAUSE_CHAIN (c);
17756 : 0 : continue;
17757 : :
17758 : 199 : case OMP_CLAUSE_INBRANCH:
17759 : 199 : case OMP_CLAUSE_NOTINBRANCH:
17760 : 199 : if (branch_seen)
17761 : : {
17762 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17763 : : "%<inbranch%> clause is incompatible with "
17764 : : "%<notinbranch%>");
17765 : 1 : remove = true;
17766 : 1 : break;
17767 : : }
17768 : 198 : branch_seen = true;
17769 : 198 : pc = &OMP_CLAUSE_CHAIN (c);
17770 : 198 : continue;
17771 : :
17772 : 367 : case OMP_CLAUSE_INCLUSIVE:
17773 : 367 : case OMP_CLAUSE_EXCLUSIVE:
17774 : 367 : need_complete = true;
17775 : 367 : need_implicitly_determined = true;
17776 : 367 : t = OMP_CLAUSE_DECL (c);
17777 : 367 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17778 : : {
17779 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17780 : : "%qE is not a variable in clause %qs", t,
17781 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17782 : 0 : remove = true;
17783 : : }
17784 : : break;
17785 : :
17786 : 152 : case OMP_CLAUSE_INIT:
17787 : 152 : init_seen = true;
17788 : 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
17789 : 71 : init_no_targetsync_clause = c;
17790 : : /* FALLTHRU */
17791 : 242 : case OMP_CLAUSE_DESTROY:
17792 : 242 : case OMP_CLAUSE_USE:
17793 : 242 : init_use_destroy_seen = true;
17794 : 242 : t = OMP_CLAUSE_DECL (c);
17795 : 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17796 : : {
17797 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17798 : : "%qD appears more than once in action clauses", t);
17799 : 3 : remove = true;
17800 : 3 : break;
17801 : : }
17802 : 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
17803 : : /* FALLTHRU */
17804 : 298 : case OMP_CLAUSE_INTEROP:
17805 : 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
17806 : 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
17807 : : {
17808 : 40 : error_at (OMP_CLAUSE_LOCATION (c),
17809 : : "%qD must be of %<omp_interop_t%>",
17810 : 20 : OMP_CLAUSE_DECL (c));
17811 : 20 : remove = true;
17812 : : }
17813 : 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
17814 : 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
17815 : 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
17816 : : {
17817 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
17818 : 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
17819 : 6 : remove = true;
17820 : : }
17821 : 298 : pc = &OMP_CLAUSE_CHAIN (c);
17822 : 298 : break;
17823 : 0 : default:
17824 : 0 : gcc_unreachable ();
17825 : 24848 : }
17826 : :
17827 : 22609 : if (!remove)
17828 : : {
17829 : 22049 : t = OMP_CLAUSE_DECL (c);
17830 : :
17831 : 22049 : if (need_complete)
17832 : : {
17833 : 3930 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
17834 : 3930 : if (t == error_mark_node)
17835 : 7 : remove = true;
17836 : : }
17837 : :
17838 : 22049 : if (need_implicitly_determined)
17839 : : {
17840 : 9523 : const char *share_name = NULL;
17841 : :
17842 : 9523 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17843 : : share_name = "threadprivate";
17844 : 9517 : else switch (c_omp_predetermined_sharing (t))
17845 : : {
17846 : : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
17847 : : break;
17848 : 20 : case OMP_CLAUSE_DEFAULT_SHARED:
17849 : 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
17850 : 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
17851 : 28 : && c_omp_predefined_variable (t))
17852 : : /* The __func__ variable and similar function-local
17853 : : predefined variables may be listed in a shared or
17854 : : firstprivate clause. */
17855 : : break;
17856 : : share_name = "shared";
17857 : : break;
17858 : : case OMP_CLAUSE_DEFAULT_PRIVATE:
17859 : : share_name = "private";
17860 : : break;
17861 : 0 : default:
17862 : 0 : gcc_unreachable ();
17863 : : }
17864 : : if (share_name)
17865 : : {
17866 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
17867 : : "%qE is predetermined %qs for %qs",
17868 : : t, share_name,
17869 : 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17870 : 10 : remove = true;
17871 : : }
17872 : 9513 : else if (TREE_READONLY (t)
17873 : 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
17874 : 9526 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
17875 : : {
17876 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
17877 : : "%<const%> qualified %qE may appear only in "
17878 : : "%<shared%> or %<firstprivate%> clauses", t);
17879 : 4 : remove = true;
17880 : : }
17881 : : }
17882 : : }
17883 : :
17884 : 22049 : if (remove)
17885 : : {
17886 : 581 : if (grp_start_p)
17887 : : {
17888 : : /* If we found a clause to remove, we want to remove the whole
17889 : : expanded group, otherwise gimplify
17890 : : (omp_resolve_clause_dependencies) can get confused. */
17891 : 154 : *grp_start_p = grp_sentinel;
17892 : 154 : pc = grp_start_p;
17893 : 154 : grp_start_p = NULL;
17894 : : }
17895 : : else
17896 : 427 : *pc = OMP_CLAUSE_CHAIN (c);
17897 : : }
17898 : : else
17899 : 22028 : pc = &OMP_CLAUSE_CHAIN (c);
17900 : : }
17901 : :
17902 : 29209 : if (grp_start_p
17903 : 29209 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
17904 : 49 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
17905 : 30 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
17906 : :
17907 : 29209 : if (simdlen
17908 : 29209 : && safelen
17909 : 29326 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
17910 : 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
17911 : : {
17912 : 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
17913 : : "%<simdlen%> clause value is bigger than "
17914 : : "%<safelen%> clause value");
17915 : 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
17916 : 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
17917 : : }
17918 : :
17919 : 29209 : if (ordered_clause
17920 : 29209 : && schedule_clause
17921 : 29209 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
17922 : : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
17923 : : {
17924 : 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
17925 : : "%<nonmonotonic%> schedule modifier specified together "
17926 : : "with %<ordered%> clause");
17927 : 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
17928 : 7 : = (enum omp_clause_schedule_kind)
17929 : 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
17930 : : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
17931 : : }
17932 : :
17933 : 29209 : if (reduction_seen < 0 && ordered_clause)
17934 : : {
17935 : 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
17936 : : "%qs clause specified together with %<inscan%> "
17937 : : "%<reduction%> clause", "ordered");
17938 : 2 : reduction_seen = -2;
17939 : : }
17940 : :
17941 : 29209 : if (reduction_seen < 0 && schedule_clause)
17942 : : {
17943 : 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
17944 : : "%qs clause specified together with %<inscan%> "
17945 : : "%<reduction%> clause", "schedule");
17946 : 3 : reduction_seen = -2;
17947 : : }
17948 : :
17949 : 29209 : if (linear_variable_step_check
17950 : 29209 : || reduction_seen == -2
17951 : : || allocate_seen
17952 : 29197 : || target_in_reduction_seen)
17953 : 5145 : for (pc = &clauses, c = clauses; c ; c = *pc)
17954 : : {
17955 : 4556 : bool remove = false;
17956 : 4556 : if (allocate_seen)
17957 : 4398 : switch (OMP_CLAUSE_CODE (c))
17958 : : {
17959 : 430 : case OMP_CLAUSE_REDUCTION:
17960 : 430 : case OMP_CLAUSE_IN_REDUCTION:
17961 : 430 : case OMP_CLAUSE_TASK_REDUCTION:
17962 : 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
17963 : : {
17964 : 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
17965 : 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
17966 : 0 : t = TREE_OPERAND (t, 0);
17967 : 23 : if (TREE_CODE (t) == ADDR_EXPR
17968 : 10 : || INDIRECT_REF_P (t))
17969 : 13 : t = TREE_OPERAND (t, 0);
17970 : 23 : if (DECL_P (t))
17971 : 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
17972 : : break;
17973 : : }
17974 : : /* FALLTHRU */
17975 : 1302 : case OMP_CLAUSE_PRIVATE:
17976 : 1302 : case OMP_CLAUSE_FIRSTPRIVATE:
17977 : 1302 : case OMP_CLAUSE_LASTPRIVATE:
17978 : 1302 : case OMP_CLAUSE_LINEAR:
17979 : 1302 : if (DECL_P (OMP_CLAUSE_DECL (c)))
17980 : 2604 : bitmap_clear_bit (&aligned_head,
17981 : 1302 : DECL_UID (OMP_CLAUSE_DECL (c)));
17982 : : break;
17983 : : default:
17984 : : break;
17985 : : }
17986 : 4556 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
17987 : 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
17988 : 4563 : && !bitmap_bit_p (&map_head,
17989 : 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
17990 : : {
17991 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17992 : : "%<linear%> clause step is a parameter %qD not "
17993 : : "specified in %<uniform%> clause",
17994 : 2 : OMP_CLAUSE_LINEAR_STEP (c));
17995 : 2 : remove = true;
17996 : : }
17997 : 4554 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
17998 : 4554 : && reduction_seen == -2)
17999 : 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18000 : 4556 : if (target_in_reduction_seen
18001 : 4556 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18002 : : {
18003 : 256 : tree t = OMP_CLAUSE_DECL (c);
18004 : 256 : while (handled_component_p (t)
18005 : : || INDIRECT_REF_P (t)
18006 : : || TREE_CODE (t) == ADDR_EXPR
18007 : : || TREE_CODE (t) == MEM_REF
18008 : 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18009 : 32 : t = TREE_OPERAND (t, 0);
18010 : 256 : if (DECL_P (t)
18011 : 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18012 : 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18013 : : }
18014 : :
18015 : 4556 : if (remove)
18016 : 2 : *pc = OMP_CLAUSE_CHAIN (c);
18017 : : else
18018 : 4554 : pc = &OMP_CLAUSE_CHAIN (c);
18019 : : }
18020 : :
18021 : 589 : if (allocate_seen)
18022 : 4961 : for (pc = &clauses, c = clauses; c ; c = *pc)
18023 : : {
18024 : 4398 : bool remove = false;
18025 : 4398 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18026 : 704 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18027 : 5082 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18028 : : {
18029 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
18030 : : "%qD specified in %<allocate%> clause but not in "
18031 : 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18032 : 3 : remove = true;
18033 : : }
18034 : 4398 : if (remove)
18035 : 3 : *pc = OMP_CLAUSE_CHAIN (c);
18036 : : else
18037 : 4395 : pc = &OMP_CLAUSE_CHAIN (c);
18038 : : }
18039 : :
18040 : 29209 : if (nogroup_seen && reduction_seen)
18041 : : {
18042 : 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18043 : : "%<nogroup%> clause must not be used together with "
18044 : : "%<reduction%> clause");
18045 : 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18046 : : }
18047 : :
18048 : 29209 : if (grainsize_seen && num_tasks_seen)
18049 : : {
18050 : 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18051 : : "%<grainsize%> clause must not be used together with "
18052 : : "%<num_tasks%> clause");
18053 : 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18054 : : }
18055 : :
18056 : 29209 : if (full_seen && partial_seen)
18057 : : {
18058 : 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18059 : : "%<full%> clause must not be used together with "
18060 : : "%<partial%> clause");
18061 : 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18062 : : }
18063 : :
18064 : 29209 : if (detach_seen)
18065 : : {
18066 : 31 : if (mergeable_seen)
18067 : : {
18068 : 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18069 : : "%<detach%> clause must not be used together with "
18070 : : "%<mergeable%> clause");
18071 : 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18072 : : }
18073 : : else
18074 : : {
18075 : 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18076 : :
18077 : 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18078 : : {
18079 : 50 : bool remove = false;
18080 : 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18081 : 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18082 : 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18083 : 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18084 : 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18085 : : {
18086 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
18087 : : "the event handle of a %<detach%> clause "
18088 : : "should not be in a data-sharing clause");
18089 : 2 : remove = true;
18090 : : }
18091 : 50 : if (remove)
18092 : 2 : *pc = OMP_CLAUSE_CHAIN (c);
18093 : : else
18094 : 48 : pc = &OMP_CLAUSE_CHAIN (c);
18095 : : }
18096 : : }
18097 : : }
18098 : :
18099 : 29209 : if (ort == C_ORT_OMP_INTEROP
18100 : 29209 : && depend_clause
18101 : 21 : && (!init_use_destroy_seen
18102 : 20 : || (init_seen && init_no_targetsync_clause)))
18103 : : {
18104 : 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18105 : : "%<depend%> clause requires action clauses with "
18106 : : "%<targetsync%> interop-type");
18107 : 3 : if (init_no_targetsync_clause)
18108 : 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18109 : : "%<init%> clause lacks the %<targetsync%> modifier");
18110 : : }
18111 : :
18112 : 29209 : bitmap_obstack_release (NULL);
18113 : 29209 : return clauses;
18114 : : }
18115 : :
18116 : : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18117 : : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18118 : :
18119 : : tree
18120 : 13 : c_omp_finish_mapper_clauses (tree clauses)
18121 : : {
18122 : 13 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18123 : : }
18124 : :
18125 : : /* Return code to initialize DST with a copy constructor from SRC.
18126 : : C doesn't have copy constructors nor assignment operators, only for
18127 : : _Atomic vars we need to perform __atomic_load from src into a temporary
18128 : : followed by __atomic_store of the temporary to dst. */
18129 : :
18130 : : tree
18131 : 18718 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18132 : : {
18133 : 18718 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18134 : 18714 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18135 : :
18136 : 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18137 : 4 : tree type = TREE_TYPE (dst);
18138 : 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18139 : 4 : tree tmp = create_tmp_var (nonatomic_type);
18140 : 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18141 : 4 : TREE_ADDRESSABLE (tmp) = 1;
18142 : 4 : suppress_warning (tmp);
18143 : 4 : tree src_addr = build_fold_addr_expr (src);
18144 : 4 : tree dst_addr = build_fold_addr_expr (dst);
18145 : 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18146 : 4 : vec<tree, va_gc> *params;
18147 : : /* Expansion of a generic atomic load may require an addition
18148 : : element, so allocate enough to prevent a resize. */
18149 : 4 : vec_alloc (params, 4);
18150 : :
18151 : : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18152 : 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18153 : 4 : params->quick_push (src_addr);
18154 : 4 : params->quick_push (tmp_addr);
18155 : 4 : params->quick_push (seq_cst);
18156 : 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18157 : :
18158 : 4 : vec_alloc (params, 4);
18159 : :
18160 : : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18161 : 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18162 : 4 : params->quick_push (dst_addr);
18163 : 4 : params->quick_push (tmp_addr);
18164 : 4 : params->quick_push (seq_cst);
18165 : 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18166 : 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18167 : : }
18168 : :
18169 : : /* Create a transaction node. */
18170 : :
18171 : : tree
18172 : 135 : c_finish_transaction (location_t loc, tree block, int flags)
18173 : : {
18174 : 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18175 : 135 : if (flags & TM_STMT_ATTR_OUTER)
18176 : 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18177 : 135 : if (flags & TM_STMT_ATTR_RELAXED)
18178 : 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18179 : 135 : return add_stmt (stmt);
18180 : : }
18181 : :
18182 : : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18183 : : down to the element type of an array. If ORIG_QUAL_TYPE is not
18184 : : NULL, then it should be used as the qualified type
18185 : : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18186 : : preserve information about the typedef name from which an array
18187 : : type was derived). */
18188 : :
18189 : : tree
18190 : 72963463 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18191 : : size_t orig_qual_indirect)
18192 : : {
18193 : 72963463 : if (type == error_mark_node)
18194 : : return type;
18195 : :
18196 : 72963315 : if (TREE_CODE (type) == ARRAY_TYPE)
18197 : : {
18198 : 3117248 : tree t;
18199 : 3117248 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18200 : : type_quals, orig_qual_type,
18201 : : orig_qual_indirect - 1);
18202 : :
18203 : : /* See if we already have an identically qualified type. */
18204 : 3117248 : if (orig_qual_type && orig_qual_indirect == 0)
18205 : : t = orig_qual_type;
18206 : : else
18207 : 4553076 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18208 : : {
18209 : 4413714 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18210 : 2998489 : && TYPE_NAME (t) == TYPE_NAME (type)
18211 : 2977845 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18212 : 7391559 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18213 : 2977845 : TYPE_ATTRIBUTES (type)))
18214 : : break;
18215 : : }
18216 : 3117207 : if (!t)
18217 : : {
18218 : 139362 : tree domain = TYPE_DOMAIN (type);
18219 : :
18220 : 139362 : t = build_variant_type_copy (type);
18221 : 139362 : TREE_TYPE (t) = element_type;
18222 : 139362 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18223 : :
18224 : 139362 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18225 : 139362 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18226 : 194 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18227 : 139168 : else if (TYPE_CANONICAL (element_type) != element_type
18228 : 139168 : || (domain && TYPE_CANONICAL (domain) != domain))
18229 : : {
18230 : 2696 : tree unqualified_canon
18231 : 5184 : = c_build_array_type (TYPE_CANONICAL (element_type),
18232 : 2488 : domain ? TYPE_CANONICAL (domain)
18233 : : : NULL_TREE);
18234 : 2696 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18235 : : {
18236 : 0 : unqualified_canon
18237 : 0 : = build_distinct_type_copy (unqualified_canon);
18238 : 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18239 : : }
18240 : 2696 : TYPE_CANONICAL (t)
18241 : 5392 : = c_build_qualified_type (unqualified_canon, type_quals);
18242 : : }
18243 : : else
18244 : 136472 : TYPE_CANONICAL (t) = t;
18245 : : }
18246 : 3117248 : return t;
18247 : : }
18248 : :
18249 : : /* A restrict-qualified pointer type must be a pointer to object or
18250 : : incomplete type. Note that the use of POINTER_TYPE_P also allows
18251 : : REFERENCE_TYPEs, which is appropriate for C++. */
18252 : 69846067 : if ((type_quals & TYPE_QUAL_RESTRICT)
18253 : 69846067 : && (!POINTER_TYPE_P (type)
18254 : 3336611 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18255 : : {
18256 : 3 : error ("invalid use of %<restrict%>");
18257 : 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18258 : : }
18259 : :
18260 : 69846067 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18261 : 69846067 : ? orig_qual_type
18262 : 69846036 : : build_qualified_type (type, type_quals));
18263 : :
18264 : 69846067 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18265 : : == C_TYPE_VARIABLE_SIZE (type));
18266 : 69846067 : gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (var_type)
18267 : : == C_TYPE_VARIABLY_MODIFIED (type));
18268 : :
18269 : : /* A variant type does not inherit the list of incomplete vars from the
18270 : : type main variant. */
18271 : 69846067 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18272 : 67958614 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18273 : 69950090 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18274 : 1386931 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18275 : : return var_type;
18276 : : }
18277 : :
18278 : : /* Build a VA_ARG_EXPR for the C parser. */
18279 : :
18280 : : tree
18281 : 19894 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
18282 : : {
18283 : 19894 : if (error_operand_p (type))
18284 : 2 : return error_mark_node;
18285 : : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18286 : : order because it takes the address of the expression. */
18287 : 19892 : else if (handled_component_p (expr)
18288 : 31 : && reverse_storage_order_for_component_p (expr))
18289 : : {
18290 : 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18291 : 0 : return error_mark_node;
18292 : : }
18293 : 19892 : else if (!COMPLETE_TYPE_P (type))
18294 : : {
18295 : 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18296 : : "type %qT", type);
18297 : 11 : return error_mark_node;
18298 : : }
18299 : 19881 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18300 : : {
18301 : 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18302 : : type);
18303 : 1 : return error_mark_node;
18304 : : }
18305 : 19880 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18306 : 1 : warning_at (loc2, OPT_Wc___compat,
18307 : : "C++ requires promoted type, not enum type, in %<va_arg%>");
18308 : 19880 : return build_va_arg (loc2, expr, type);
18309 : : }
18310 : :
18311 : : /* Return truthvalue of whether T1 is the same tree structure as T2.
18312 : : Return 1 if they are the same. Return false if they are different. */
18313 : :
18314 : : bool
18315 : 1854 : c_tree_equal (tree t1, tree t2)
18316 : : {
18317 : 1883 : enum tree_code code1, code2;
18318 : :
18319 : 1883 : if (t1 == t2)
18320 : : return true;
18321 : 661 : if (!t1 || !t2)
18322 : : return false;
18323 : :
18324 : 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18325 : 20 : code1 = TREE_CODE (t1))
18326 : 20 : t1 = TREE_OPERAND (t1, 0);
18327 : 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18328 : 21 : code2 = TREE_CODE (t2))
18329 : 21 : t2 = TREE_OPERAND (t2, 0);
18330 : :
18331 : : /* They might have become equal now. */
18332 : 661 : if (t1 == t2)
18333 : : return true;
18334 : :
18335 : 640 : if (code1 != code2)
18336 : : return false;
18337 : :
18338 : 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18339 : : return false;
18340 : :
18341 : 379 : switch (code1)
18342 : : {
18343 : 2 : case INTEGER_CST:
18344 : 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18345 : :
18346 : 10 : case REAL_CST:
18347 : 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18348 : :
18349 : 0 : case STRING_CST:
18350 : 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18351 : 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18352 : 0 : TREE_STRING_LENGTH (t1));
18353 : :
18354 : 0 : case FIXED_CST:
18355 : 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18356 : : TREE_FIXED_CST (t2));
18357 : :
18358 : 0 : case COMPLEX_CST:
18359 : 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18360 : 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18361 : :
18362 : 0 : case VECTOR_CST:
18363 : 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18364 : :
18365 : 0 : case CONSTRUCTOR:
18366 : : /* We need to do this when determining whether or not two
18367 : : non-type pointer to member function template arguments
18368 : : are the same. */
18369 : 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18370 : 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18371 : : return false;
18372 : : {
18373 : : tree field, value;
18374 : : unsigned int i;
18375 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18376 : : {
18377 : 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18378 : 0 : if (!c_tree_equal (field, elt2->index)
18379 : 0 : || !c_tree_equal (value, elt2->value))
18380 : 0 : return false;
18381 : : }
18382 : : }
18383 : : return true;
18384 : :
18385 : 0 : case TREE_LIST:
18386 : 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18387 : : return false;
18388 : 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18389 : : return false;
18390 : 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18391 : :
18392 : 0 : case SAVE_EXPR:
18393 : 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18394 : :
18395 : 25 : case CALL_EXPR:
18396 : 25 : {
18397 : 25 : tree arg1, arg2;
18398 : 25 : call_expr_arg_iterator iter1, iter2;
18399 : 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18400 : : return false;
18401 : 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18402 : 25 : arg2 = first_call_expr_arg (t2, &iter2);
18403 : 25 : arg1 && arg2;
18404 : 0 : arg1 = next_call_expr_arg (&iter1),
18405 : 0 : arg2 = next_call_expr_arg (&iter2))
18406 : 0 : if (!c_tree_equal (arg1, arg2))
18407 : : return false;
18408 : 25 : if (arg1 || arg2)
18409 : : return false;
18410 : : return true;
18411 : : }
18412 : :
18413 : 0 : case TARGET_EXPR:
18414 : 0 : {
18415 : 0 : tree o1 = TREE_OPERAND (t1, 0);
18416 : 0 : tree o2 = TREE_OPERAND (t2, 0);
18417 : :
18418 : : /* Special case: if either target is an unallocated VAR_DECL,
18419 : : it means that it's going to be unified with whatever the
18420 : : TARGET_EXPR is really supposed to initialize, so treat it
18421 : : as being equivalent to anything. */
18422 : 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18423 : 0 : && !DECL_RTL_SET_P (o1))
18424 : : /*Nop*/;
18425 : 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18426 : 0 : && !DECL_RTL_SET_P (o2))
18427 : : /*Nop*/;
18428 : 0 : else if (!c_tree_equal (o1, o2))
18429 : : return false;
18430 : :
18431 : 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18432 : : }
18433 : :
18434 : 29 : case COMPONENT_REF:
18435 : 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18436 : : return false;
18437 : 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18438 : :
18439 : : case PARM_DECL:
18440 : : case VAR_DECL:
18441 : : case CONST_DECL:
18442 : : case FIELD_DECL:
18443 : : case FUNCTION_DECL:
18444 : : case IDENTIFIER_NODE:
18445 : : case SSA_NAME:
18446 : : return false;
18447 : :
18448 : 0 : case TREE_VEC:
18449 : 0 : {
18450 : 0 : unsigned ix;
18451 : 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18452 : : return false;
18453 : 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18454 : 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18455 : 0 : TREE_VEC_ELT (t2, ix)))
18456 : : return false;
18457 : : return true;
18458 : : }
18459 : :
18460 : 22 : CASE_CONVERT:
18461 : 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18462 : : return false;
18463 : : break;
18464 : :
18465 : : default:
18466 : : break;
18467 : : }
18468 : :
18469 : 131 : switch (TREE_CODE_CLASS (code1))
18470 : : {
18471 : 131 : case tcc_unary:
18472 : 131 : case tcc_binary:
18473 : 131 : case tcc_comparison:
18474 : 131 : case tcc_expression:
18475 : 131 : case tcc_vl_exp:
18476 : 131 : case tcc_reference:
18477 : 131 : case tcc_statement:
18478 : 131 : {
18479 : 131 : int i, n = TREE_OPERAND_LENGTH (t1);
18480 : :
18481 : 131 : switch (code1)
18482 : : {
18483 : 0 : case PREINCREMENT_EXPR:
18484 : 0 : case PREDECREMENT_EXPR:
18485 : 0 : case POSTINCREMENT_EXPR:
18486 : 0 : case POSTDECREMENT_EXPR:
18487 : 0 : n = 1;
18488 : 0 : break;
18489 : 16 : case ARRAY_REF:
18490 : 16 : n = 2;
18491 : 16 : break;
18492 : : default:
18493 : : break;
18494 : : }
18495 : :
18496 : 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
18497 : 131 : && n != TREE_OPERAND_LENGTH (t2))
18498 : : return false;
18499 : :
18500 : 305 : for (i = 0; i < n; ++i)
18501 : 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
18502 : : return false;
18503 : :
18504 : : return true;
18505 : : }
18506 : :
18507 : 0 : case tcc_type:
18508 : 0 : return comptypes (t1, t2);
18509 : 0 : default:
18510 : 0 : gcc_unreachable ();
18511 : : }
18512 : : }
18513 : :
18514 : : /* Returns true when the function declaration FNDECL is implicit,
18515 : : introduced as a result of a call to an otherwise undeclared
18516 : : function, and false otherwise. */
18517 : :
18518 : : bool
18519 : 2013 : c_decl_implicit (const_tree fndecl)
18520 : : {
18521 : 2013 : return C_DECL_IMPLICIT (fndecl);
18522 : : }
|