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 : : /* The level of nesting inside "_Generic". */
82 : : int in_generic;
83 : :
84 : : /* True when parsing OpenMP loop expressions. */
85 : : bool c_in_omp_for;
86 : :
87 : : /* True when parsing OpenMP map clause. */
88 : : bool c_omp_array_section_p;
89 : :
90 : : /* The argument of last parsed sizeof expression, only to be tested
91 : : if expr.original_code == SIZEOF_EXPR. */
92 : : tree c_last_sizeof_arg;
93 : : location_t c_last_sizeof_loc;
94 : :
95 : : /* Nonzero if we might need to print a "missing braces around
96 : : initializer" message within this initializer. */
97 : : static int found_missing_braces;
98 : :
99 : : static bool require_constant_value;
100 : : static bool require_constant_elements;
101 : : static bool require_constexpr_value;
102 : :
103 : : static tree qualify_type (tree, tree);
104 : : struct comptypes_data;
105 : : static bool tagged_types_tu_compatible_p (const_tree, const_tree,
106 : : struct comptypes_data *);
107 : : static bool comp_target_types (location_t, tree, tree);
108 : : static bool function_types_compatible_p (const_tree, const_tree,
109 : : struct comptypes_data *);
110 : : static bool type_lists_compatible_p (const_tree, const_tree,
111 : : struct comptypes_data *);
112 : : static int convert_arguments (location_t, vec<location_t>, tree,
113 : : vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
114 : : tree);
115 : : static tree pointer_diff (location_t, tree, tree, tree *);
116 : : static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
117 : : enum impl_conv, bool, tree, tree, int,
118 : : int = 0);
119 : : static tree valid_compound_expr_initializer (tree, tree);
120 : : static void push_string (const char *);
121 : : static void push_member_name (tree);
122 : : static int spelling_length (void);
123 : : static char *print_spelling (char *);
124 : : static void warning_init (location_t, int, const char *);
125 : : static tree digest_init (location_t, tree, tree, tree, tree, bool, bool, bool,
126 : : bool, bool, bool);
127 : : static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
128 : : bool, struct obstack *);
129 : : static void output_pending_init_elements (int, struct obstack *);
130 : : static bool set_designator (location_t, bool, struct obstack *);
131 : : static void push_range_stack (tree, struct obstack *);
132 : : static void add_pending_init (location_t, tree, tree, tree, bool,
133 : : struct obstack *);
134 : : static void set_nonincremental_init (struct obstack *);
135 : : static void set_nonincremental_init_from_string (tree, struct obstack *);
136 : : static tree find_init_member (tree, struct obstack *);
137 : : static void readonly_warning (tree, enum lvalue_use);
138 : : static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
139 : : static void record_maybe_used_decl (tree, bool address);
140 : : static bool comptypes_internal (const_tree, const_tree,
141 : : struct comptypes_data *data);
142 : : static bool comptypes_check_for_composite (tree t1, tree t2);
143 : : static bool handle_counted_by_p (tree);
144 : :
145 : : /* Return true if EXP is a null pointer constant, false otherwise. */
146 : :
147 : : bool
148 : 183981580 : null_pointer_constant_p (const_tree expr)
149 : : {
150 : : /* This should really operate on c_expr structures, but they aren't
151 : : yet available everywhere required. */
152 : 183981580 : tree type = TREE_TYPE (expr);
153 : :
154 : : /* An integer constant expression with the value 0, such an expression
155 : : cast to type void*, or the predefined constant nullptr, are a null
156 : : pointer constant. */
157 : 183981580 : if (expr == nullptr_node)
158 : : return true;
159 : :
160 : 183980930 : return (TREE_CODE (expr) == INTEGER_CST
161 : 18675112 : && !TREE_OVERFLOW (expr)
162 : 18675015 : && integer_zerop (expr)
163 : 187838531 : && (INTEGRAL_TYPE_P (type)
164 : 330026 : || (TREE_CODE (type) == POINTER_TYPE
165 : 330024 : && VOID_TYPE_P (TREE_TYPE (type))
166 : 254857 : && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
167 : : }
168 : :
169 : : /* EXPR may appear in an unevaluated part of an integer constant
170 : : expression, but not in an evaluated part. Wrap it in a
171 : : C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
172 : : INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
173 : :
174 : : static tree
175 : 62354 : note_integer_operands (tree expr)
176 : : {
177 : 62354 : tree ret;
178 : 62354 : if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
179 : : {
180 : 22 : ret = copy_node (expr);
181 : 22 : TREE_OVERFLOW (ret) = 1;
182 : : }
183 : : else
184 : : {
185 : 62332 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
186 : 62332 : C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
187 : : }
188 : 62354 : return ret;
189 : : }
190 : :
191 : : /* Having checked whether EXPR may appear in an unevaluated part of an
192 : : integer constant expression and found that it may, remove any
193 : : C_MAYBE_CONST_EXPR noting this fact and return the resulting
194 : : expression. */
195 : :
196 : : static inline tree
197 : 32989175 : remove_c_maybe_const_expr (tree expr)
198 : : {
199 : 32989175 : if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
200 : 61506 : return C_MAYBE_CONST_EXPR_EXPR (expr);
201 : : else
202 : : return expr;
203 : : }
204 : :
205 : : /* This is a cache to hold if two types are seen. */
206 : :
207 : : struct tagged_tu_seen_cache {
208 : : const struct tagged_tu_seen_cache * next;
209 : : const_tree t1;
210 : : const_tree t2;
211 : : };
212 : :
213 : : /* Do `exp = require_complete_type (loc, exp);' to make sure exp
214 : : does not have an incomplete type. (That includes void types.)
215 : : LOC is the location of the use. */
216 : :
217 : : tree
218 : 755756386 : require_complete_type (location_t loc, tree value)
219 : : {
220 : 755756386 : tree type = TREE_TYPE (value);
221 : :
222 : 755756386 : if (error_operand_p (value))
223 : 9083 : return error_mark_node;
224 : :
225 : : /* First, detect a valid value with a complete type. */
226 : 755747303 : if (COMPLETE_TYPE_P (type))
227 : : return value;
228 : :
229 : 164 : c_incomplete_type_error (loc, value, type);
230 : 164 : return error_mark_node;
231 : : }
232 : :
233 : : /* Print an error message for invalid use of an incomplete type.
234 : : VALUE is the expression that was used (or 0 if that isn't known)
235 : : and TYPE is the type that was invalid. LOC is the location for
236 : : the error. */
237 : :
238 : : void
239 : 224 : c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
240 : : {
241 : : /* Avoid duplicate error message. */
242 : 224 : if (TREE_CODE (type) == ERROR_MARK)
243 : : return;
244 : :
245 : 224 : if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
246 : 98 : error_at (loc, "%qD has an incomplete type %qT", value, type);
247 : : else
248 : : {
249 : 126 : retry:
250 : : /* We must print an error message. Be clever about what it says. */
251 : :
252 : 126 : switch (TREE_CODE (type))
253 : : {
254 : 82 : case RECORD_TYPE:
255 : 82 : case UNION_TYPE:
256 : 82 : case ENUMERAL_TYPE:
257 : 82 : break;
258 : :
259 : 38 : case VOID_TYPE:
260 : 38 : error_at (loc, "invalid use of void expression");
261 : 38 : return;
262 : :
263 : 6 : case ARRAY_TYPE:
264 : 6 : if (TYPE_DOMAIN (type))
265 : : {
266 : 3 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
267 : : {
268 : 3 : error_at (loc, "invalid use of flexible array member");
269 : 3 : return;
270 : : }
271 : 0 : type = TREE_TYPE (type);
272 : 0 : goto retry;
273 : : }
274 : 3 : error_at (loc, "invalid use of array with unspecified bounds");
275 : 3 : return;
276 : :
277 : 0 : default:
278 : 0 : gcc_unreachable ();
279 : : }
280 : :
281 : 82 : if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
282 : 80 : error_at (loc, "invalid use of undefined type %qT", type);
283 : : else
284 : : /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
285 : 2 : error_at (loc, "invalid use of incomplete typedef %qT", type);
286 : : }
287 : : }
288 : :
289 : : /* Given a type, apply default promotions wrt unnamed function
290 : : arguments and return the new type. */
291 : :
292 : : tree
293 : 121551186 : c_type_promotes_to (tree type)
294 : : {
295 : 121551186 : tree ret = NULL_TREE;
296 : :
297 : 121551186 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
298 : 1479684 : ret = double_type_node;
299 : 120071502 : else if (c_promoting_integer_type_p (type))
300 : : {
301 : : /* Preserve unsignedness if not really getting any wider. */
302 : 18186287 : if (TYPE_UNSIGNED (type)
303 : 18186287 : && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
304 : 0 : ret = unsigned_type_node;
305 : : else
306 : 18186287 : ret = integer_type_node;
307 : : }
308 : :
309 : 19665971 : if (ret != NULL_TREE)
310 : 19665971 : return (TYPE_ATOMIC (type)
311 : 19665971 : ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
312 : : : ret);
313 : :
314 : : return type;
315 : : }
316 : :
317 : : /* Return true if between two named address spaces, whether there is a superset
318 : : named address space that encompasses both address spaces. If there is a
319 : : superset, return which address space is the superset. */
320 : :
321 : : static bool
322 : 7249555 : addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
323 : : {
324 : 7249555 : if (as1 == as2)
325 : : {
326 : 7249555 : *common = as1;
327 : 7249555 : return true;
328 : : }
329 : 0 : else if (targetm.addr_space.subset_p (as1, as2))
330 : : {
331 : 0 : *common = as2;
332 : 0 : return true;
333 : : }
334 : 0 : else if (targetm.addr_space.subset_p (as2, as1))
335 : : {
336 : 0 : *common = as1;
337 : 0 : return true;
338 : : }
339 : : else
340 : : return false;
341 : : }
342 : :
343 : : /* Return a variant of TYPE which has all the type qualifiers of LIKE
344 : : as well as those of TYPE. */
345 : :
346 : : static tree
347 : 2569582 : qualify_type (tree type, tree like)
348 : : {
349 : 2569582 : addr_space_t as_type = TYPE_ADDR_SPACE (type);
350 : 2569582 : addr_space_t as_like = TYPE_ADDR_SPACE (like);
351 : 2569582 : addr_space_t as_common;
352 : :
353 : : /* If the two named address spaces are different, determine the common
354 : : superset address space. If there isn't one, raise an error. */
355 : 2569582 : if (!addr_space_superset (as_type, as_like, &as_common))
356 : : {
357 : 0 : as_common = as_type;
358 : 0 : error ("%qT and %qT are in disjoint named address spaces",
359 : : type, like);
360 : : }
361 : :
362 : 5139164 : return c_build_qualified_type (type,
363 : 2569582 : TYPE_QUALS_NO_ADDR_SPACE (type)
364 : 2569582 : | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
365 : 2569582 : | ENCODE_QUAL_ADDR_SPACE (as_common));
366 : : }
367 : :
368 : :
369 : : /* Check consistency of type TYPE. For derived types, we test that
370 : : C_TYPE_VARIABLE_SIZE and C_TYPE_VARIABLY_MODIFIED are consistent with
371 : : the requirements of the base type. We also check that arrays with a
372 : : non-constant length are marked with C_TYPE_VARIABLE_SIZE. If any
373 : : inconsistency is detected false is returned and true otherwise. */
374 : :
375 : : static bool
376 : 136964005 : c_verify_type (tree type)
377 : : {
378 : 136964005 : switch (TREE_CODE (type))
379 : : {
380 : 64381865 : case POINTER_TYPE:
381 : 64381865 : case FUNCTION_TYPE:
382 : : /* Pointer and funcions can not have variable size. */
383 : 64381865 : if (C_TYPE_VARIABLE_SIZE (type))
384 : : return false;
385 : : /* Pointer and funcions are variably modified if and only if the
386 : : return / target type is variably modified. */
387 : 64381865 : if (C_TYPE_VARIABLY_MODIFIED (type)
388 : 64381865 : != C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type)))
389 : 0 : return false;
390 : : break;
391 : 898616 : case ARRAY_TYPE:
392 : : /* An array has variable size if and only if it has a non-constant
393 : : dimensions or its element type has variable size. */
394 : 898616 : if ((C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
395 : 898616 : || (TYPE_DOMAIN (type) && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
396 : 873301 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
397 : : != INTEGER_CST))
398 : 898616 : != C_TYPE_VARIABLE_SIZE (type))
399 : : return false;
400 : : /* If the element type or the array has variable size, then the
401 : : array has variable size and is variably modified. */
402 : 898616 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (type))
403 : 898616 : || C_TYPE_VARIABLE_SIZE (type))
404 : : {
405 : 28572 : if (!C_TYPE_VARIABLE_SIZE (type))
406 : : return false;
407 : 28572 : if (!C_TYPE_VARIABLY_MODIFIED (type))
408 : : return false;
409 : : }
410 : : /* If the element type is variably modified, then also the array. */
411 : 898616 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (type))
412 : 898616 : && !C_TYPE_VARIABLY_MODIFIED (type))
413 : 0 : return false;
414 : : break;
415 : : default:
416 : : break;
417 : : }
418 : : return true;
419 : : }
420 : :
421 : : /* Propagate C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
422 : : from a base type to a newly built derived or qualified type. */
423 : :
424 : : static tree
425 : 130079295 : c_set_type_bits (tree new_type, tree old_type)
426 : : {
427 : 130079295 : gcc_checking_assert (c_verify_type (old_type));
428 : :
429 : 130079295 : if (C_TYPE_VARIABLY_MODIFIED (old_type))
430 : 28181 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
431 : :
432 : 130079295 : if (TREE_CODE (new_type) == ARRAY_TYPE && C_TYPE_VARIABLE_SIZE (old_type))
433 : : {
434 : 16775 : C_TYPE_VARIABLY_MODIFIED (new_type) = true;
435 : 16775 : C_TYPE_VARIABLE_SIZE (new_type) = true;
436 : : }
437 : 130079295 : return new_type;
438 : : }
439 : :
440 : : /* Build a pointer type using the default pointer mode. */
441 : :
442 : : tree
443 : 69893947 : c_build_pointer_type (tree to_type)
444 : : {
445 : 69893947 : addr_space_t as = to_type == error_mark_node ? ADDR_SPACE_GENERIC
446 : 69893947 : : TYPE_ADDR_SPACE (to_type);
447 : 69893947 : machine_mode pointer_mode;
448 : :
449 : 69893947 : if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
450 : 69893947 : pointer_mode = targetm.addr_space.pointer_mode (as);
451 : : else
452 : : pointer_mode = c_default_pointer_mode;
453 : :
454 : 69893947 : return c_build_pointer_type_for_mode (to_type, pointer_mode, false);
455 : : }
456 : :
457 : : /* Build a pointer type using the given mode. */
458 : :
459 : : tree
460 : 69902802 : c_build_pointer_type_for_mode (tree type, machine_mode mode, bool m)
461 : : {
462 : 69902802 : tree ret = build_pointer_type_for_mode (type, mode, m);
463 : 69902802 : return c_set_type_bits (ret, type);
464 : : }
465 : :
466 : : /* Build a function type. */
467 : :
468 : : tree
469 : 52100314 : c_build_function_type (tree type, tree args, bool no)
470 : : {
471 : 52100314 : tree ret = build_function_type (type, args, no);
472 : 52100314 : return c_set_type_bits (ret, type);
473 : : }
474 : :
475 : : /* Build an array type. This sets typeless storage as required
476 : : by C2Y and C_TYPE_VARIABLY_MODIFIED and C_TYPE_VARIABLE_SIZE
477 : : based on the element type and domain. */
478 : :
479 : : tree
480 : 1143628 : c_build_array_type (tree type, tree domain)
481 : : {
482 : 1143628 : int type_quals = TYPE_QUALS (type);
483 : :
484 : : /* Identify typeless storage as introduced in C2Y
485 : : and supported also in earlier language modes. */
486 : 1710865 : bool typeless = (char_type_p (type) && !(type_quals & TYPE_QUAL_ATOMIC))
487 : 1143628 : || (AGGREGATE_TYPE_P (type) && TYPE_TYPELESS_STORAGE (type));
488 : :
489 : 1143628 : tree ret = build_array_type (type, domain, typeless);
490 : :
491 : 1059255 : if (domain && TYPE_MAX_VALUE (domain)
492 : 2115249 : && TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
493 : : {
494 : 24172 : C_TYPE_VARIABLE_SIZE (ret) = 1;
495 : 24172 : C_TYPE_VARIABLY_MODIFIED (ret) = 1;
496 : : }
497 : :
498 : 1143628 : return c_set_type_bits (ret, type);
499 : : }
500 : :
501 : :
502 : : /* Build an array type of unspecified size. */
503 : : tree
504 : 142 : c_build_array_type_unspecified (tree type)
505 : : {
506 : 142 : tree upper = build2 (COMPOUND_EXPR, TREE_TYPE (size_zero_node),
507 : : integer_zero_node, size_zero_node);
508 : 142 : return c_build_array_type (type, build_index_type (upper));
509 : : }
510 : :
511 : :
512 : : tree
513 : 6932551 : c_build_type_attribute_qual_variant (tree type, tree attrs, int quals)
514 : : {
515 : 6932551 : tree ret = build_type_attribute_qual_variant (type, attrs, quals);
516 : 6932551 : return c_set_type_bits (ret, type);
517 : : }
518 : :
519 : : tree
520 : 6932352 : c_build_type_attribute_variant (tree type, tree attrs)
521 : : {
522 : 6932352 : return c_build_type_attribute_qual_variant (type, attrs, TYPE_QUALS (type));
523 : : }
524 : :
525 : : /* Reconstruct a complex derived type. This is used to re-construct types
526 : : with the vector attribute. It is called via a langhook. */
527 : :
528 : : tree
529 : 483843 : c_reconstruct_complex_type (tree type, tree bottom)
530 : : {
531 : 483843 : tree inner, outer;
532 : :
533 : 483843 : if (TREE_CODE (type) == POINTER_TYPE)
534 : : {
535 : 16 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
536 : 16 : outer = c_build_pointer_type_for_mode (inner, TYPE_MODE (type),
537 : 16 : TYPE_REF_CAN_ALIAS_ALL (type));
538 : : }
539 : : else if (TREE_CODE (type) == ARRAY_TYPE)
540 : : {
541 : 13 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
542 : 13 : outer = c_build_array_type (inner, TYPE_DOMAIN (type));
543 : :
544 : 13 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (type)
545 : : == C_TYPE_VARIABLE_SIZE (outer));
546 : 13 : gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (outer)
547 : : == C_TYPE_VARIABLY_MODIFIED (type));
548 : : }
549 : : else if (TREE_CODE (type) == FUNCTION_TYPE)
550 : : {
551 : 170 : inner = c_reconstruct_complex_type (TREE_TYPE (type), bottom);
552 : 170 : outer = c_build_function_type (inner, TYPE_ARG_TYPES (type),
553 : 170 : TYPE_NO_NAMED_ARGS_STDARG_P (type));
554 : : }
555 : : else if (TREE_CODE (type) == REFERENCE_TYPE
556 : : || TREE_CODE (type) == OFFSET_TYPE)
557 : 0 : gcc_unreachable ();
558 : : else
559 : : return bottom;
560 : :
561 : 199 : return c_build_type_attribute_qual_variant (outer, TYPE_ATTRIBUTES (type),
562 : 398 : TYPE_QUALS (type));
563 : : }
564 : :
565 : : /* If NTYPE is a type of a non-variadic function with a prototype
566 : : and OTYPE is a type of a function without a prototype and ATTRS
567 : : contains attribute format, diagnosess and removes it from ATTRS.
568 : : Returns the result of build_type_attribute_variant of NTYPE and
569 : : the (possibly) modified ATTRS. */
570 : :
571 : : static tree
572 : 9697 : c_build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
573 : : {
574 : 9697 : if (!prototype_p (otype)
575 : 9685 : && prototype_p (ntype)
576 : 19352 : && lookup_attribute ("format", attrs))
577 : : {
578 : 14 : warning_at (input_location, OPT_Wattributes,
579 : : "%qs attribute can only be applied to variadic functions",
580 : : "format");
581 : 14 : attrs = remove_attribute ("format", attrs);
582 : : }
583 : 9697 : return c_build_type_attribute_variant (ntype, attrs);
584 : :
585 : : }
586 : :
587 : : /* Given a type which could be a typedef name, make sure to return the
588 : : original type. See set_underlying_type. */
589 : : static const_tree
590 : 60591245 : c_type_original (const_tree t)
591 : : {
592 : : /* It may even be a typedef of a typedef...
593 : : In the case of compiler-created builtin structs the TYPE_DECL
594 : : may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
595 : 60591245 : while (TYPE_NAME (t)
596 : 31637298 : && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
597 : 60595125 : && DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
598 : 1939 : t = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
599 : 60591245 : return t;
600 : : }
601 : :
602 : : /* Return the tag for a tagged type. */
603 : : tree
604 : 60591245 : c_type_tag (const_tree t)
605 : : {
606 : 60591245 : gcc_assert (RECORD_OR_UNION_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE);
607 : 60591245 : const_tree orig = c_type_original (t);
608 : 60591245 : tree name = TYPE_NAME (orig);
609 : 60591245 : if (!name)
610 : : return NULL_TREE;
611 : 31635359 : if (TREE_CODE (name) == TYPE_DECL)
612 : : {
613 : : /* A TYPE_DECL added by add_decl_expr. */
614 : 2 : gcc_checking_assert (!DECL_NAME (name));
615 : : return NULL_TREE;
616 : : }
617 : 31635357 : gcc_checking_assert (TREE_CODE (name) == IDENTIFIER_NODE);
618 : : return name;
619 : : }
620 : :
621 : : /* Remove qualifiers but not atomic. For arrays remove qualifiers
622 : : on the element type but also do not remove atomic. */
623 : : static tree
624 : 61494271 : remove_qualifiers (tree t)
625 : : {
626 : 61494271 : if (!t || t == error_mark_node)
627 : : return t;
628 : 61494246 : return TYPE_ATOMIC (strip_array_types (t))
629 : 61494246 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (t), TYPE_QUAL_ATOMIC)
630 : 61478701 : : TYPE_MAIN_VARIANT (t);
631 : : }
632 : :
633 : :
634 : : /* Return the composite type of two compatible types.
635 : :
636 : : We assume that comptypes has already been done and returned
637 : : nonzero; if that isn't so, this may crash. In particular, we
638 : : assume that qualifiers match. */
639 : :
640 : : struct composite_cache {
641 : : tree t1;
642 : : tree t2;
643 : : tree composite;
644 : : struct composite_cache* next;
645 : : };
646 : :
647 : : tree
648 : 12832552 : composite_type_internal (tree t1, tree t2, tree cond,
649 : : struct composite_cache* cache)
650 : : {
651 : 12832552 : enum tree_code code1;
652 : 12832552 : enum tree_code code2;
653 : 12832552 : tree attributes;
654 : :
655 : : /* Save time if the two types are the same. */
656 : :
657 : 12832552 : if (t1 == t2) return t1;
658 : :
659 : : /* If one type is nonsense, use the other. */
660 : 2596489 : if (t1 == error_mark_node)
661 : : return t2;
662 : 2596485 : if (t2 == error_mark_node)
663 : : return t1;
664 : :
665 : 2596483 : code1 = TREE_CODE (t1);
666 : 2596483 : code2 = TREE_CODE (t2);
667 : :
668 : : /* Merge the attributes. */
669 : 2596483 : attributes = targetm.merge_type_attributes (t1, t2);
670 : :
671 : : /* If one is an enumerated type and the other is the compatible
672 : : integer type, the composite type might be either of the two
673 : : (DR#013 question 3). For consistency, use the enumerated type as
674 : : the composite type. */
675 : :
676 : 2596483 : if (code1 == ENUMERAL_TYPE
677 : 194 : && (code2 == INTEGER_TYPE
678 : 194 : || code2 == BOOLEAN_TYPE))
679 : : return t1;
680 : 2596289 : if (code2 == ENUMERAL_TYPE
681 : 69 : && (code1 == INTEGER_TYPE
682 : 69 : || code1 == BOOLEAN_TYPE))
683 : : return t2;
684 : :
685 : 2596220 : gcc_assert (code1 == code2);
686 : :
687 : 2596220 : switch (code1)
688 : : {
689 : 6341 : case POINTER_TYPE:
690 : : /* For two pointers, do this recursively on the target type. */
691 : 6341 : {
692 : 6341 : tree pointed_to_1 = TREE_TYPE (t1);
693 : 6341 : tree pointed_to_2 = TREE_TYPE (t2);
694 : 6341 : tree target = composite_type_internal (pointed_to_1, pointed_to_2,
695 : : cond, cache);
696 : 6341 : t1 = c_build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
697 : 6341 : t1 = c_build_type_attribute_variant (t1, attributes);
698 : 6341 : return qualify_type (t1, t2);
699 : : }
700 : :
701 : 10958 : case ARRAY_TYPE:
702 : 10958 : {
703 : 10958 : tree d1 = TYPE_DOMAIN (t1);
704 : 10958 : tree d2 = TYPE_DOMAIN (t2);
705 : :
706 : : /* We should not have any type quals on arrays at all. */
707 : 10958 : gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
708 : : && !TYPE_QUALS_NO_ADDR_SPACE (t2));
709 : :
710 : 10958 : bool t1_complete = COMPLETE_TYPE_P (t1);
711 : 10958 : bool t2_complete = COMPLETE_TYPE_P (t2);
712 : :
713 : 10958 : bool d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
714 : 10958 : bool d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
715 : :
716 : 10958 : bool d1_variable, d2_variable;
717 : :
718 : 21916 : d1_variable = (!d1_zero
719 : 10958 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
720 : 10365 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
721 : 21916 : d2_variable = (!d2_zero
722 : 10958 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
723 : 10618 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
724 : :
725 : 10958 : bool use1 = d1 && (d2_variable || d2_zero || !d1_variable);
726 : 10958 : bool use2 = d2 && (d1_variable || d1_zero || !d2_variable);
727 : :
728 : : /* If the first is an unspecified size pick the other one. */
729 : 8976 : if (d2_variable && c_type_unspecified_p (t1))
730 : : {
731 : 28 : gcc_assert (use1 && use2);
732 : : use1 = false;
733 : : }
734 : :
735 : : /* If both are VLAs but not unspecified and we are in the
736 : : conditional operator, we create a conditional to select
737 : : the size of the active branch. */
738 : 202 : bool use0 = cond && d1_variable && !c_type_unspecified_p (t1)
739 : 11157 : && d2_variable && !c_type_unspecified_p (t2);
740 : :
741 : 10958 : tree td;
742 : 10958 : tree elt = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
743 : : cond, cache);
744 : :
745 : 10958 : if (!use0)
746 : : {
747 : : /* Save space: see if the result is identical to one of the args. */
748 : 10865 : if (elt == TREE_TYPE (t1) && use1)
749 : 10079 : return c_build_type_attribute_variant (t1, attributes);
750 : 786 : if (elt == TREE_TYPE (t2) && use2)
751 : 668 : return c_build_type_attribute_variant (t2, attributes);
752 : :
753 : 161 : if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
754 : 10 : return c_build_type_attribute_variant (t1, attributes);
755 : 116 : if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
756 : 2 : return c_build_type_attribute_variant (t2, attributes);
757 : :
758 : 212 : td = TYPE_DOMAIN (use1 ? t1 : t2);
759 : : }
760 : : else
761 : : {
762 : : /* Not used in C. */
763 : 93 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d1));
764 : 93 : gcc_assert (size_zero_node == TYPE_MIN_VALUE (d2));
765 : :
766 : 93 : tree d = fold_build3_loc (UNKNOWN_LOCATION, COND_EXPR, sizetype,
767 : 93 : cond, TYPE_MAX_VALUE (d1),
768 : 93 : TYPE_MAX_VALUE (d2));
769 : :
770 : 93 : td = build_index_type (d);
771 : : }
772 : :
773 : : /* Merge the element types, and have a size if either arg has
774 : : one. We may have qualifiers on the element types. To set
775 : : up TYPE_MAIN_VARIANT correctly, we need to form the
776 : : composite of the unqualified types and add the qualifiers
777 : : back at the end. */
778 : 199 : int quals = TYPE_QUALS (strip_array_types (elt));
779 : 199 : tree unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
780 : :
781 : 199 : t1 = c_build_array_type (unqual_elt, td);
782 : :
783 : : /* Check that a type which has a varying outermost dimension
784 : : got marked has having a variable size. */
785 : 199 : bool varsize = (d1_variable && d2_variable)
786 : 97 : || (d1_variable && !t2_complete)
787 : 285 : || (d2_variable && !t1_complete);
788 : 133 : gcc_checking_assert (!varsize || C_TYPE_VARIABLE_SIZE (t1));
789 : :
790 : : /* Ensure a composite type involving a zero-length array type
791 : : is a zero-length type not an incomplete type. */
792 : 199 : if (d1_zero && d2_zero
793 : 16 : && (t1_complete || t2_complete)
794 : 200 : && !COMPLETE_TYPE_P (t1))
795 : : {
796 : 1 : TYPE_SIZE (t1) = bitsize_zero_node;
797 : 1 : TYPE_SIZE_UNIT (t1) = size_zero_node;
798 : : }
799 : 199 : t1 = c_build_qualified_type (t1, quals);
800 : 199 : return c_build_type_attribute_variant (t1, attributes);
801 : : }
802 : :
803 : 162 : case RECORD_TYPE:
804 : 162 : case UNION_TYPE:
805 : 162 : if (flag_isoc23 && !comptypes_same_p (t1, t2))
806 : : {
807 : : /* Go to the original type to get the right tag. */
808 : 101 : tree tag = c_type_tag (t1);
809 : :
810 : 101 : gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2));
811 : 101 : gcc_checking_assert (!tag || comptypes (t1, t2));
812 : :
813 : : /* If a composite type for these two types is already under
814 : : construction, return it. */
815 : :
816 : 246 : for (struct composite_cache *c = cache; c != NULL; c = c->next)
817 : 152 : if ((c->t1 == t1 && c->t2 == t2) || (c->t1 == t2 && c->t2 == t1))
818 : 7 : return c->composite;
819 : :
820 : : /* Otherwise, create a new type node and link it into the cache. */
821 : :
822 : 94 : tree n = make_node (code1);
823 : 94 : SET_TYPE_STRUCTURAL_EQUALITY (n);
824 : 94 : TYPE_NAME (n) = tag;
825 : :
826 : 94 : struct composite_cache cache2 = { t1, t2, n, cache };
827 : 94 : cache = &cache2;
828 : :
829 : 94 : tree f1 = TYPE_FIELDS (t1);
830 : 94 : tree f2 = TYPE_FIELDS (t2);
831 : 94 : tree fields = NULL_TREE;
832 : :
833 : 214 : for (tree a = f1, b = f2; a && b;
834 : 120 : a = DECL_CHAIN (a), b = DECL_CHAIN (b))
835 : : {
836 : 120 : tree ta = TREE_TYPE (a);
837 : 120 : tree tb = TREE_TYPE (b);
838 : :
839 : 120 : if (DECL_C_BIT_FIELD (a))
840 : : {
841 : 19 : ta = DECL_BIT_FIELD_TYPE (a);
842 : 19 : tb = DECL_BIT_FIELD_TYPE (b);
843 : : }
844 : :
845 : 120 : gcc_assert (DECL_NAME (a) == DECL_NAME (b));
846 : 120 : gcc_checking_assert (!DECL_NAME (a) || comptypes (ta, tb));
847 : :
848 : 120 : tree t = composite_type_internal (ta, tb, cond, cache);
849 : 120 : tree f = build_decl (input_location, FIELD_DECL, DECL_NAME (a), t);
850 : :
851 : 120 : DECL_PACKED (f) = DECL_PACKED (a);
852 : 120 : SET_DECL_ALIGN (f, DECL_ALIGN (a));
853 : 120 : DECL_ATTRIBUTES (f) = DECL_ATTRIBUTES (a);
854 : 120 : C_DECL_VARIABLE_SIZE (f) = C_TYPE_VARIABLE_SIZE (t);
855 : :
856 : 120 : decl_attributes (&f, DECL_ATTRIBUTES (f), 0);
857 : :
858 : 120 : finish_decl (f, input_location, NULL, NULL, NULL);
859 : :
860 : 120 : if (DECL_C_BIT_FIELD (a))
861 : : {
862 : : /* This will be processed by finish_struct. */
863 : 19 : SET_DECL_C_BIT_FIELD (f);
864 : 19 : DECL_INITIAL (f) = build_int_cst (integer_type_node,
865 : 19 : tree_to_uhwi (DECL_SIZE (a)));
866 : 19 : DECL_NONADDRESSABLE_P (f) = true;
867 : 19 : DECL_PADDING_P (f) = !DECL_NAME (a);
868 : : }
869 : :
870 : 120 : DECL_CHAIN (f) = fields;
871 : 120 : fields = f;
872 : : }
873 : :
874 : 94 : fields = nreverse (fields);
875 : :
876 : : /* Setup the struct/union type. Because we inherit all variably
877 : : modified components, we can ignore the size expression. */
878 : 94 : tree expr = NULL_TREE;
879 : :
880 : : /* Set TYPE_STUB_DECL for debugging symbols. */
881 : 94 : TYPE_STUB_DECL (n) = pushdecl (build_decl (input_location, TYPE_DECL,
882 : : NULL_TREE, n));
883 : :
884 : 94 : n = finish_struct (input_location, n, fields, attributes, NULL,
885 : : &expr);
886 : :
887 : 94 : return qualify_type (n, t1);
888 : : }
889 : : /* FALLTHRU */
890 : 61 : case ENUMERAL_TYPE:
891 : 61 : if (attributes != NULL)
892 : : {
893 : : /* Try harder not to create a new aggregate type. */
894 : 4 : if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
895 : : return t1;
896 : 0 : if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
897 : : return t2;
898 : : }
899 : 57 : return c_build_type_attribute_variant (t1, attributes);
900 : :
901 : 2538093 : case FUNCTION_TYPE:
902 : : /* Function types: prefer the one that specified arg types.
903 : : If both do, merge the arg types. Also merge the return types. */
904 : 2538093 : {
905 : 2538093 : tree valtype = composite_type_internal (TREE_TYPE (t1), TREE_TYPE (t2),
906 : : cond, cache);
907 : 2538093 : tree p1 = TYPE_ARG_TYPES (t1);
908 : 2538093 : tree p2 = TYPE_ARG_TYPES (t2);
909 : 2538093 : int len;
910 : 2538093 : tree newargs, n;
911 : 2538093 : int i;
912 : :
913 : : /* Save space: see if the result is identical to one of the args. */
914 : 2538093 : if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
915 : 9180 : return c_build_functype_attribute_variant (t1, t2, attributes);
916 : 2528913 : if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
917 : 517 : return c_build_functype_attribute_variant (t2, t1, attributes);
918 : :
919 : : /* Simple way if one arg fails to specify argument types. */
920 : 2528396 : if (TYPE_ARG_TYPES (t1) == NULL_TREE)
921 : : {
922 : 9 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t2),
923 : 9 : TYPE_NO_NAMED_ARGS_STDARG_P (t2));
924 : 9 : t1 = c_build_type_attribute_variant (t1, attributes);
925 : 9 : return qualify_type (t1, t2);
926 : : }
927 : 2528387 : if (TYPE_ARG_TYPES (t2) == NULL_TREE)
928 : : {
929 : 1 : t1 = c_build_function_type (valtype, TYPE_ARG_TYPES (t1),
930 : 1 : TYPE_NO_NAMED_ARGS_STDARG_P (t1));
931 : 1 : t1 = c_build_type_attribute_variant (t1, attributes);
932 : 1 : return qualify_type (t1, t2);
933 : : }
934 : :
935 : : /* If both args specify argument types, we must merge the two
936 : : lists, argument by argument. */
937 : :
938 : : for (len = 0, newargs = p1;
939 : 6578814 : newargs && newargs != void_list_node;
940 : 4050428 : len++, newargs = TREE_CHAIN (newargs))
941 : : ;
942 : :
943 : 6578814 : for (i = 0; i < len; i++)
944 : 4050428 : newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
945 : :
946 : : n = newargs;
947 : :
948 : 6578814 : for (; p1 && p1 != void_list_node;
949 : 4050428 : p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
950 : : {
951 : 4050428 : tree mv1 = remove_qualifiers (TREE_VALUE (p1));
952 : 4050428 : tree mv2 = remove_qualifiers (TREE_VALUE (p2));
953 : :
954 : : /* A null type means arg type is not specified.
955 : : Take whatever the other function type has. */
956 : 4050428 : if (TREE_VALUE (p1) == NULL_TREE)
957 : : {
958 : 0 : TREE_VALUE (n) = TREE_VALUE (p2);
959 : 0 : goto parm_done;
960 : : }
961 : 4050428 : if (TREE_VALUE (p2) == NULL_TREE)
962 : : {
963 : 0 : TREE_VALUE (n) = TREE_VALUE (p1);
964 : 0 : goto parm_done;
965 : : }
966 : :
967 : : /* Given wait (union {union wait *u; int *i} *)
968 : : and wait (union wait *),
969 : : prefer union wait * as type of parm. */
970 : 4050428 : if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
971 : 4050428 : && TREE_VALUE (p1) != TREE_VALUE (p2))
972 : : {
973 : 30 : tree memb;
974 : 30 : for (memb = TYPE_FIELDS (TREE_VALUE (p1));
975 : 38 : memb; memb = DECL_CHAIN (memb))
976 : : {
977 : 38 : tree mv3 = TREE_TYPE (memb);
978 : 38 : if (mv3 && mv3 != error_mark_node
979 : 38 : && TREE_CODE (mv3) != ARRAY_TYPE)
980 : 38 : mv3 = TYPE_MAIN_VARIANT (mv3);
981 : 38 : if (comptypes (mv3, mv2))
982 : : {
983 : 60 : TREE_VALUE (n) = composite_type_internal (TREE_TYPE (memb),
984 : 30 : TREE_VALUE (p2),
985 : : cond, cache);
986 : 30 : pedwarn (input_location, OPT_Wpedantic,
987 : : "function types not truly compatible in ISO C");
988 : 30 : goto parm_done;
989 : : }
990 : : }
991 : : }
992 : 4050398 : if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
993 : 4050398 : && TREE_VALUE (p2) != TREE_VALUE (p1))
994 : : {
995 : 30 : tree memb;
996 : 30 : for (memb = TYPE_FIELDS (TREE_VALUE (p2));
997 : 38 : memb; memb = DECL_CHAIN (memb))
998 : : {
999 : 38 : tree mv3 = TREE_TYPE (memb);
1000 : 38 : if (mv3 && mv3 != error_mark_node
1001 : 38 : && TREE_CODE (mv3) != ARRAY_TYPE)
1002 : 38 : mv3 = TYPE_MAIN_VARIANT (mv3);
1003 : 38 : if (comptypes (mv3, mv1))
1004 : : {
1005 : 30 : TREE_VALUE (n)
1006 : 30 : = composite_type_internal (TREE_TYPE (memb),
1007 : 30 : TREE_VALUE (p1),
1008 : : cond, cache);
1009 : 30 : pedwarn (input_location, OPT_Wpedantic,
1010 : : "function types not truly compatible in ISO C");
1011 : 30 : goto parm_done;
1012 : : }
1013 : : }
1014 : : }
1015 : 4050368 : TREE_VALUE (n) = composite_type_internal (mv1, mv2, cond, cache);
1016 : 4050428 : parm_done: ;
1017 : : }
1018 : :
1019 : 2528386 : t1 = c_build_function_type (valtype, newargs);
1020 : 2528386 : t1 = qualify_type (t1, t2);
1021 : : }
1022 : : /* FALLTHRU */
1023 : :
1024 : 2569052 : default:
1025 : 2569052 : return c_build_type_attribute_variant (t1, attributes);
1026 : : }
1027 : : }
1028 : :
1029 : : tree
1030 : 6226612 : composite_type_cond (tree t1, tree t2, tree cond)
1031 : : {
1032 : 6226612 : gcc_checking_assert (comptypes_check_for_composite (t1, t2));
1033 : :
1034 : 6226612 : struct composite_cache cache = { };
1035 : 6226612 : tree n = composite_type_internal (t1, t2, cond, &cache);
1036 : :
1037 : 6226612 : gcc_checking_assert (comptypes_check_for_composite (n, t1));
1038 : 6226612 : gcc_checking_assert (comptypes_check_for_composite (n, t2));
1039 : 6226612 : return n;
1040 : : }
1041 : :
1042 : :
1043 : : tree
1044 : 6222167 : composite_type (tree t1, tree t2)
1045 : : {
1046 : 6222167 : return composite_type_cond (t1, t2, NULL_TREE);
1047 : : }
1048 : :
1049 : : /* Return the type of a conditional expression between pointers to
1050 : : possibly differently qualified versions of compatible types.
1051 : :
1052 : : We assume that comp_target_types has already been done and returned
1053 : : true; if that isn't so, this may crash. */
1054 : :
1055 : : static tree
1056 : 113726 : common_pointer_type (tree t1, tree t2, tree cond)
1057 : : {
1058 : 113726 : tree attributes;
1059 : 113726 : unsigned target_quals;
1060 : 113726 : addr_space_t as1, as2, as_common;
1061 : 113726 : int quals1, quals2;
1062 : :
1063 : : /* Save time if the two types are the same. */
1064 : :
1065 : 113726 : if (t1 == t2) return t1;
1066 : :
1067 : : /* If one type is nonsense, use the other. */
1068 : 4445 : if (t1 == error_mark_node)
1069 : : return t2;
1070 : 4445 : if (t2 == error_mark_node)
1071 : : return t1;
1072 : :
1073 : 4445 : gcc_assert (TREE_CODE (t1) == POINTER_TYPE
1074 : : && TREE_CODE (t2) == POINTER_TYPE);
1075 : :
1076 : : /* Merge the attributes. */
1077 : 4445 : attributes = targetm.merge_type_attributes (t1, t2);
1078 : :
1079 : : /* Find the composite type of the target types, and combine the
1080 : : qualifiers of the two types' targets. */
1081 : 4445 : tree pointed_to_1 = TREE_TYPE (t1);
1082 : 4445 : tree pointed_to_2 = TREE_TYPE (t2);
1083 : 4445 : tree target = composite_type_cond (TYPE_MAIN_VARIANT (pointed_to_1),
1084 : 4445 : TYPE_MAIN_VARIANT (pointed_to_2), cond);
1085 : :
1086 : : /* Strip array types to get correct qualifier for pointers to arrays */
1087 : 4445 : quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
1088 : 4445 : quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
1089 : :
1090 : : /* For function types do not merge const qualifiers, but drop them
1091 : : if used inconsistently. The middle-end uses these to mark const
1092 : : and noreturn functions. */
1093 : 4445 : if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
1094 : 2129 : target_quals = (quals1 & quals2);
1095 : : else
1096 : 2316 : target_quals = (quals1 | quals2);
1097 : :
1098 : : /* If the two named address spaces are different, determine the common
1099 : : superset address space. This is guaranteed to exist due to the
1100 : : assumption that comp_target_type returned true. */
1101 : 4445 : as1 = TYPE_ADDR_SPACE (pointed_to_1);
1102 : 4445 : as2 = TYPE_ADDR_SPACE (pointed_to_2);
1103 : 4445 : if (!addr_space_superset (as1, as2, &as_common))
1104 : 0 : gcc_unreachable ();
1105 : :
1106 : 4445 : target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
1107 : :
1108 : 4445 : t1 = c_build_pointer_type (c_build_qualified_type (target, target_quals));
1109 : 4445 : return c_build_type_attribute_variant (t1, attributes);
1110 : : }
1111 : :
1112 : : /* Return the common type for two arithmetic types under the usual
1113 : : arithmetic conversions. The default conversions have already been
1114 : : applied, and enumerated types converted to their compatible integer
1115 : : types. The resulting type is unqualified and has no attributes.
1116 : :
1117 : : This is the type for the result of most arithmetic operations
1118 : : if the operands have the given two types. */
1119 : :
1120 : : static tree
1121 : 13647104 : c_common_type (tree t1, tree t2)
1122 : : {
1123 : 13647104 : enum tree_code code1;
1124 : 13647104 : enum tree_code code2;
1125 : :
1126 : : /* If one type is nonsense, use the other. */
1127 : 13647104 : if (t1 == error_mark_node)
1128 : : return t2;
1129 : 13647104 : if (t2 == error_mark_node)
1130 : : return t1;
1131 : :
1132 : 13647104 : if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
1133 : 37763 : t1 = TYPE_MAIN_VARIANT (t1);
1134 : :
1135 : 13647104 : if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
1136 : 47723 : t2 = TYPE_MAIN_VARIANT (t2);
1137 : :
1138 : 13647104 : if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
1139 : : {
1140 : 183786 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
1141 : 183786 : t1 = c_build_type_attribute_variant (t1, attrs);
1142 : : }
1143 : :
1144 : 13647104 : if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
1145 : : {
1146 : 183264 : tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
1147 : 183264 : t2 = c_build_type_attribute_variant (t2, attrs);
1148 : : }
1149 : :
1150 : : /* Save time if the two types are the same. */
1151 : :
1152 : 13647104 : if (t1 == t2) return t1;
1153 : :
1154 : 2288874 : code1 = TREE_CODE (t1);
1155 : 2288874 : code2 = TREE_CODE (t2);
1156 : :
1157 : 2288874 : gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
1158 : : || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
1159 : : || code1 == INTEGER_TYPE || code1 == BITINT_TYPE);
1160 : 2288874 : gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
1161 : : || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
1162 : : || code2 == INTEGER_TYPE || code2 == BITINT_TYPE);
1163 : :
1164 : : /* When one operand is a decimal float type, the other operand cannot be
1165 : : a generic float type or a complex type. We also disallow vector types
1166 : : here. */
1167 : 2288874 : if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
1168 : 2291297 : && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
1169 : : {
1170 : 1080 : if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
1171 : : {
1172 : 3 : error ("cannot mix operands of decimal floating and vector types");
1173 : 3 : return error_mark_node;
1174 : : }
1175 : 1077 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1176 : : {
1177 : 5 : error ("cannot mix operands of decimal floating and complex types");
1178 : 5 : return error_mark_node;
1179 : : }
1180 : 1072 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1181 : : {
1182 : 16 : error ("cannot mix operands of decimal floating "
1183 : : "and other floating types");
1184 : 16 : return error_mark_node;
1185 : : }
1186 : : }
1187 : :
1188 : : /* If one type is a vector type, return that type. (How the usual
1189 : : arithmetic conversions apply to the vector types extension is not
1190 : : precisely specified.) */
1191 : 2288850 : if (code1 == VECTOR_TYPE)
1192 : : return t1;
1193 : :
1194 : 2288573 : if (code2 == VECTOR_TYPE)
1195 : : return t2;
1196 : :
1197 : : /* If one type is complex, form the common type of the non-complex
1198 : : components, then make that complex. Use T1 or T2 if it is the
1199 : : required type. */
1200 : 2288570 : if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
1201 : : {
1202 : 89315 : tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
1203 : 89315 : tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
1204 : 89315 : tree subtype = c_common_type (subtype1, subtype2);
1205 : :
1206 : 89315 : if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
1207 : : return t1;
1208 : 59200 : else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
1209 : : return t2;
1210 : 3241 : else if (TREE_CODE (subtype) == BITINT_TYPE)
1211 : : {
1212 : 8 : sorry ("%<_Complex _BitInt(%d)%> unsupported",
1213 : 4 : TYPE_PRECISION (subtype));
1214 : 6 : return code1 == COMPLEX_TYPE ? t1 : t2;
1215 : : }
1216 : : else
1217 : 3237 : return build_complex_type (subtype);
1218 : : }
1219 : :
1220 : : /* If only one is real, use it as the result. */
1221 : :
1222 : 2199255 : if (code1 == REAL_TYPE && code2 != REAL_TYPE)
1223 : : return t1;
1224 : :
1225 : 2074771 : if (code2 == REAL_TYPE && code1 != REAL_TYPE)
1226 : : return t2;
1227 : :
1228 : : /* If both are real and either are decimal floating point types, use
1229 : : the decimal floating point type with the greater precision. */
1230 : :
1231 : 2044469 : if (code1 == REAL_TYPE && code2 == REAL_TYPE)
1232 : : {
1233 : 102628 : if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
1234 : 102628 : || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
1235 : : return dfloat128_type_node;
1236 : : /* And prefer _Decimal128 over _Decimal64x which has in GCC's
1237 : : implementation the same mode. */
1238 : 102079 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64x_type_node
1239 : 102079 : || TYPE_MAIN_VARIANT (t2) == dfloat64x_type_node)
1240 : : return dfloat64x_type_node;
1241 : 102075 : else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
1242 : 102075 : || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
1243 : : return dfloat64_type_node;
1244 : 101630 : else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
1245 : 101630 : || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
1246 : : return dfloat32_type_node;
1247 : : }
1248 : :
1249 : : /* Deal with fixed-point types. */
1250 : 2043126 : if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
1251 : : {
1252 : 0 : unsigned int unsignedp = 0, satp = 0;
1253 : 0 : scalar_mode m1, m2;
1254 : 0 : unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
1255 : :
1256 : 0 : m1 = SCALAR_TYPE_MODE (t1);
1257 : 0 : m2 = SCALAR_TYPE_MODE (t2);
1258 : :
1259 : : /* If one input type is saturating, the result type is saturating. */
1260 : 0 : if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
1261 : : satp = 1;
1262 : :
1263 : : /* If both fixed-point types are unsigned, the result type is unsigned.
1264 : : When mixing fixed-point and integer types, follow the sign of the
1265 : : fixed-point type.
1266 : : Otherwise, the result type is signed. */
1267 : 0 : if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
1268 : 0 : && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
1269 : 0 : || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
1270 : 0 : && TYPE_UNSIGNED (t1))
1271 : 0 : || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
1272 : 0 : && TYPE_UNSIGNED (t2)))
1273 : : unsignedp = 1;
1274 : :
1275 : : /* The result type is signed. */
1276 : 0 : if (unsignedp == 0)
1277 : : {
1278 : : /* If the input type is unsigned, we need to convert to the
1279 : : signed type. */
1280 : 0 : if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
1281 : : {
1282 : 0 : enum mode_class mclass = (enum mode_class) 0;
1283 : 0 : if (GET_MODE_CLASS (m1) == MODE_UFRACT)
1284 : : mclass = MODE_FRACT;
1285 : 0 : else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
1286 : : mclass = MODE_ACCUM;
1287 : : else
1288 : 0 : gcc_unreachable ();
1289 : 0 : m1 = as_a <scalar_mode>
1290 : 0 : (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
1291 : : }
1292 : 0 : if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
1293 : : {
1294 : 0 : enum mode_class mclass = (enum mode_class) 0;
1295 : 0 : if (GET_MODE_CLASS (m2) == MODE_UFRACT)
1296 : : mclass = MODE_FRACT;
1297 : 0 : else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
1298 : : mclass = MODE_ACCUM;
1299 : : else
1300 : 0 : gcc_unreachable ();
1301 : 0 : m2 = as_a <scalar_mode>
1302 : 0 : (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
1303 : : }
1304 : : }
1305 : :
1306 : 0 : if (code1 == FIXED_POINT_TYPE)
1307 : : {
1308 : 0 : fbit1 = GET_MODE_FBIT (m1);
1309 : 0 : ibit1 = GET_MODE_IBIT (m1);
1310 : : }
1311 : : else
1312 : : {
1313 : 0 : fbit1 = 0;
1314 : : /* Signed integers need to subtract one sign bit. */
1315 : 0 : ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
1316 : : }
1317 : :
1318 : 0 : if (code2 == FIXED_POINT_TYPE)
1319 : : {
1320 : 0 : fbit2 = GET_MODE_FBIT (m2);
1321 : 0 : ibit2 = GET_MODE_IBIT (m2);
1322 : : }
1323 : : else
1324 : : {
1325 : 0 : fbit2 = 0;
1326 : : /* Signed integers need to subtract one sign bit. */
1327 : 0 : ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
1328 : : }
1329 : :
1330 : 0 : max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
1331 : 0 : max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
1332 : 0 : return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
1333 : : satp);
1334 : : }
1335 : :
1336 : : /* Both real or both integers; use the one with greater precision. */
1337 : :
1338 : 2043126 : if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
1339 : : return t1;
1340 : 1078808 : else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
1341 : : return t2;
1342 : :
1343 : : /* Same precision. Prefer long longs to longs to ints when the
1344 : : same precision, following the C99 rules on integer type rank
1345 : : (which are equivalent to the C90 rules for C90 types). */
1346 : :
1347 : 725878 : if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
1348 : 725878 : || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
1349 : : return long_long_unsigned_type_node;
1350 : :
1351 : 638839 : if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
1352 : 638839 : || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
1353 : : {
1354 : 14745 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1355 : 361 : return long_long_unsigned_type_node;
1356 : : else
1357 : : return long_long_integer_type_node;
1358 : : }
1359 : :
1360 : 624094 : if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
1361 : 624094 : || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
1362 : : return long_unsigned_type_node;
1363 : :
1364 : 525075 : if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
1365 : 525075 : || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
1366 : : {
1367 : : /* But preserve unsignedness from the other type,
1368 : : since long cannot hold all the values of an unsigned int. */
1369 : 26199 : if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
1370 : 131 : return long_unsigned_type_node;
1371 : : else
1372 : : return long_integer_type_node;
1373 : : }
1374 : :
1375 : : /* For floating types of the same TYPE_PRECISION (which we here
1376 : : assume means either the same set of values, or sets of values
1377 : : neither a subset of the other, with behavior being undefined in
1378 : : the latter case), follow the rules from TS 18661-3: prefer
1379 : : interchange types _FloatN, then standard types long double,
1380 : : double, float, then extended types _FloatNx. For extended types,
1381 : : check them starting with _Float128x as that seems most consistent
1382 : : in spirit with preferring long double to double; for interchange
1383 : : types, also check in that order for consistency although it's not
1384 : : possible for more than one of them to have the same
1385 : : precision. */
1386 : 498876 : tree mv1 = TYPE_MAIN_VARIANT (t1);
1387 : 498876 : tree mv2 = TYPE_MAIN_VARIANT (t2);
1388 : :
1389 : 2493814 : for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
1390 : 1995101 : if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
1391 : : return FLOATN_TYPE_NODE (i);
1392 : :
1393 : : /* Likewise, prefer long double to double even if same size. */
1394 : 498713 : if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1395 : : return long_double_type_node;
1396 : :
1397 : : /* Likewise, prefer double to float even if same size.
1398 : : We got a couple of embedded targets with 32 bit doubles, and the
1399 : : pdp11 might have 64 bit floats. */
1400 : 498471 : if (mv1 == double_type_node || mv2 == double_type_node)
1401 : : return double_type_node;
1402 : :
1403 : 497899 : if (mv1 == float_type_node || mv2 == float_type_node)
1404 : : return float_type_node;
1405 : :
1406 : 1982920 : for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1407 : 1487190 : if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1408 : : return FLOATNX_TYPE_NODE (i);
1409 : :
1410 : 495730 : if ((code1 == BITINT_TYPE || code2 == BITINT_TYPE) && code1 != code2)
1411 : : {
1412 : : /* Prefer any other integral types over bit-precise integer types. */
1413 : 10 : if (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2))
1414 : 6 : return code1 == BITINT_TYPE ? t2 : t1;
1415 : : /* If BITINT_TYPE is unsigned and the other type is signed
1416 : : non-BITINT_TYPE with the same precision, the latter has higher rank.
1417 : : In that case:
1418 : : Otherwise, both operands are converted to the unsigned integer type
1419 : : corresponding to the type of the operand with signed integer type. */
1420 : 8 : if (TYPE_UNSIGNED (code1 == BITINT_TYPE ? t1 : t2))
1421 : 5 : return c_common_unsigned_type (code1 == BITINT_TYPE ? t2 : t1);
1422 : : }
1423 : :
1424 : : /* Otherwise prefer the unsigned one. */
1425 : :
1426 : 495722 : if (TYPE_UNSIGNED (t1))
1427 : : return t1;
1428 : : else
1429 : : return t2;
1430 : : }
1431 : :
1432 : : /* Wrapper around c_common_type that is used by c-common.cc and other
1433 : : front end optimizations that remove promotions. ENUMERAL_TYPEs
1434 : : are allowed here and are converted to their compatible integer types.
1435 : : BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1436 : : preferably a non-Boolean type as the common type. */
1437 : : tree
1438 : 100093 : common_type (tree t1, tree t2)
1439 : : {
1440 : 100093 : if (TREE_CODE (t1) == ENUMERAL_TYPE)
1441 : 0 : t1 = ENUM_UNDERLYING_TYPE (t1);
1442 : 100093 : if (TREE_CODE (t2) == ENUMERAL_TYPE)
1443 : 1 : t2 = ENUM_UNDERLYING_TYPE (t2);
1444 : :
1445 : : /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1446 : 100093 : if (TREE_CODE (t1) == BOOLEAN_TYPE
1447 : 584 : && TREE_CODE (t2) == BOOLEAN_TYPE)
1448 : 584 : return boolean_type_node;
1449 : :
1450 : : /* If either type is BOOLEAN_TYPE, then return the other. */
1451 : 99509 : if (TREE_CODE (t1) == BOOLEAN_TYPE)
1452 : : return t2;
1453 : 99509 : if (TREE_CODE (t2) == BOOLEAN_TYPE)
1454 : : return t1;
1455 : :
1456 : 99509 : return c_common_type (t1, t2);
1457 : : }
1458 : :
1459 : :
1460 : :
1461 : : /* Helper function for comptypes. For two compatible types, return 1
1462 : : if they pass consistency checks. In particular we test that
1463 : : TYPE_CANONICAL is set correctly, i.e. the two types can alias. */
1464 : :
1465 : : static bool
1466 : 43547058 : comptypes_verify (tree type1, tree type2)
1467 : : {
1468 : 43547058 : if (type1 == type2 || !type1 || !type2
1469 : 3442356 : || TREE_CODE (type1) == ERROR_MARK || TREE_CODE (type2) == ERROR_MARK)
1470 : : return true;
1471 : :
1472 : 3442355 : gcc_checking_assert (c_verify_type (type1));
1473 : 3442355 : gcc_checking_assert (c_verify_type (type2));
1474 : :
1475 : 3442355 : if (TYPE_CANONICAL (type1) != TYPE_CANONICAL (type2)
1476 : 381569 : && !TYPE_STRUCTURAL_EQUALITY_P (type1)
1477 : 3822263 : && !TYPE_STRUCTURAL_EQUALITY_P (type2))
1478 : : {
1479 : : /* FIXME: check other types. */
1480 : 379576 : if (RECORD_OR_UNION_TYPE_P (type1)
1481 : 379576 : || TREE_CODE (type1) == ENUMERAL_TYPE
1482 : 379576 : || TREE_CODE (type2) == ENUMERAL_TYPE)
1483 : 0 : return false;
1484 : : }
1485 : : return true;
1486 : : }
1487 : :
1488 : : struct comptypes_data {
1489 : :
1490 : : /* output */
1491 : : bool enum_and_int_p;
1492 : : bool different_types_p;
1493 : : bool warning_needed;
1494 : :
1495 : : /* context */
1496 : : bool anon_field;
1497 : : bool pointedto;
1498 : :
1499 : : /* configuration */
1500 : : bool equiv;
1501 : : bool ignore_promoting_args;
1502 : :
1503 : : const struct tagged_tu_seen_cache* cache;
1504 : : };
1505 : :
1506 : :
1507 : : /* Helper function for composite_type. This function ignores when the
1508 : : function type of an old-style declaration is incompatible with a type
1509 : : of a declaration with prototype because some are arguments are not
1510 : : self-promoting. This is ignored only for function types but not
1511 : : ignored in a nested context. */
1512 : :
1513 : : static bool
1514 : 18679836 : comptypes_check_for_composite (tree t1, tree t2)
1515 : : {
1516 : 18679836 : struct comptypes_data data = { };
1517 : 18679836 : data.ignore_promoting_args = FUNCTION_TYPE == TREE_CODE (t1);
1518 : 18679836 : return comptypes_internal (t1, t2, &data);
1519 : : }
1520 : :
1521 : :
1522 : : /* C implementation of compatible_types_for_indirection_note_p. */
1523 : :
1524 : : bool
1525 : 761 : compatible_types_for_indirection_note_p (tree type1, tree type2)
1526 : : {
1527 : 761 : return comptypes (type1, type2) == 1;
1528 : : }
1529 : :
1530 : : /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1531 : : or various other operations. Return 2 if they are compatible
1532 : : but a warning may be needed if you use them together. */
1533 : :
1534 : : int
1535 : 55225790 : comptypes (tree type1, tree type2)
1536 : : {
1537 : 55225790 : struct comptypes_data data = { };
1538 : 55225790 : bool ret = comptypes_internal (type1, type2, &data);
1539 : :
1540 : 55225790 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1541 : :
1542 : 37101657 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1543 : : }
1544 : :
1545 : :
1546 : : /* Like comptypes, but it returns non-zero only for identical
1547 : : types. */
1548 : :
1549 : : bool
1550 : 280 : comptypes_same_p (tree type1, tree type2)
1551 : : {
1552 : 280 : struct comptypes_data data = { };
1553 : 280 : bool ret = comptypes_internal (type1, type2, &data);
1554 : :
1555 : 280 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1556 : :
1557 : 280 : if (data.different_types_p)
1558 : 103 : return false;
1559 : :
1560 : : return ret;
1561 : : }
1562 : :
1563 : :
1564 : : /* Like comptypes, but if it returns non-zero because enum and int are
1565 : : compatible, it sets *ENUM_AND_INT_P to true. */
1566 : :
1567 : : int
1568 : 6644136 : comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1569 : : {
1570 : 6644136 : struct comptypes_data data = { };
1571 : 6644136 : bool ret = comptypes_internal (type1, type2, &data);
1572 : 6644136 : *enum_and_int_p = data.enum_and_int_p;
1573 : :
1574 : 6644136 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1575 : :
1576 : 6400406 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1577 : : }
1578 : :
1579 : : /* Like comptypes, but if it returns nonzero for different types, it
1580 : : sets *DIFFERENT_TYPES_P to true. */
1581 : :
1582 : : int
1583 : 44755 : comptypes_check_different_types (tree type1, tree type2,
1584 : : bool *different_types_p)
1585 : : {
1586 : 44755 : struct comptypes_data data = { };
1587 : 44755 : bool ret = comptypes_internal (type1, type2, &data);
1588 : 44755 : *different_types_p = data.different_types_p;
1589 : :
1590 : 44755 : gcc_checking_assert (!ret || comptypes_verify (type1, type2));
1591 : :
1592 : 44755 : return ret ? (data.warning_needed ? 2 : 1) : 0;
1593 : : }
1594 : :
1595 : :
1596 : : /* Like comptypes, but if it returns true for struct and union types
1597 : : considered equivalent for aliasing purposes, i.e. for setting
1598 : : TYPE_CANONICAL after completing a struct or union.
1599 : :
1600 : : This function must return false only for types which are not
1601 : : compatible according to C language semantics (cf. comptypes),
1602 : : otherwise the middle-end would make incorrect aliasing decisions.
1603 : : It may return true for some similar types that are not compatible
1604 : : according to those stricter rules.
1605 : :
1606 : : In particular, we ignore size expression in arrays so that the
1607 : : following structs are in the same equivalence class:
1608 : :
1609 : : struct foo { char (*buf)[]; };
1610 : : struct foo { char (*buf)[3]; };
1611 : : struct foo { char (*buf)[4]; };
1612 : :
1613 : : We also treat unions / structs with members which are pointers to
1614 : : structures or unions with the same tag as equivalent (if they are not
1615 : : incompatible for other reasons). Although incomplete structure
1616 : : or union types are not compatible to any other type, they may become
1617 : : compatible to different types when completed. To avoid having to update
1618 : : TYPE_CANONICAL at this point, we only consider the tag when forming
1619 : : the equivalence classes. For example, the following types with tag
1620 : : 'foo' are all considered equivalent:
1621 : :
1622 : : struct bar;
1623 : : struct foo { struct bar *x };
1624 : : struct foo { struct bar { int a; } *x };
1625 : : struct foo { struct bar { char b; } *x }; */
1626 : :
1627 : : bool
1628 : 14313466 : comptypes_equiv_p (tree type1, tree type2)
1629 : : {
1630 : 14313466 : struct comptypes_data data = { };
1631 : 14313466 : data.equiv = true;
1632 : 14313466 : bool ret = comptypes_internal (type1, type2, &data);
1633 : :
1634 : : /* check that different equivance classes are assigned only
1635 : : to types that are not compatible. */
1636 : 14313466 : gcc_checking_assert (ret || !comptypes (type1, type2));
1637 : :
1638 : 14313466 : return ret;
1639 : : }
1640 : :
1641 : :
1642 : : /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1643 : : or various other operations. If they are compatible but a warning may
1644 : : be needed if you use them together, 'warning_needed' in DATA is set.
1645 : : If one type is an enum and the other a compatible integer type, then
1646 : : this sets 'enum_and_int_p' in DATA to true (it is never set to
1647 : : false). If the types are compatible but different enough not to be
1648 : : permitted in C11 typedef redeclarations, then this sets
1649 : : 'different_types_p' in DATA to true; it is never set to
1650 : : false, but may or may not be set if the types are incompatible.
1651 : : If two functions types are not compatible only because one is
1652 : : an old-style definition that does not have self-promoting arguments,
1653 : : then this can be ignored by setting 'ignore_promoting_args_p'.
1654 : : For 'equiv' we can compute equivalency classes (see above).
1655 : : This differs from comptypes, in that we don't free the seen
1656 : : types. */
1657 : :
1658 : : static bool
1659 : 130337288 : comptypes_internal (const_tree type1, const_tree type2,
1660 : : struct comptypes_data *data)
1661 : : {
1662 : 130608652 : const_tree t1 = type1;
1663 : 130608652 : const_tree t2 = type2;
1664 : :
1665 : : /* Suppress errors caused by previously reported errors. */
1666 : :
1667 : 130608652 : if (t1 == t2 || !t1 || !t2
1668 : 44183884 : || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1669 : : return true;
1670 : :
1671 : : /* Enumerated types are compatible with integer types, but this is
1672 : : not transitive: two enumerated types in the same translation unit
1673 : : are compatible with each other only if they are the same type. */
1674 : :
1675 : 44183881 : if (TREE_CODE (t1) == ENUMERAL_TYPE
1676 : 1008 : && COMPLETE_TYPE_P (t1)
1677 : 44184845 : && TREE_CODE (t2) != ENUMERAL_TYPE)
1678 : : {
1679 : 852 : t1 = ENUM_UNDERLYING_TYPE (t1);
1680 : 852 : if (TREE_CODE (t2) != VOID_TYPE)
1681 : : {
1682 : 848 : data->enum_and_int_p = true;
1683 : 848 : data->different_types_p = true;
1684 : : }
1685 : : }
1686 : 44183029 : else if (TREE_CODE (t2) == ENUMERAL_TYPE
1687 : 4688 : && COMPLETE_TYPE_P (t2)
1688 : 44187711 : && TREE_CODE (t1) != ENUMERAL_TYPE)
1689 : : {
1690 : 4570 : t2 = ENUM_UNDERLYING_TYPE (t2);
1691 : 4570 : if (TREE_CODE (t1) != VOID_TYPE)
1692 : : {
1693 : 3671 : data->enum_and_int_p = true;
1694 : 3671 : data->different_types_p = true;
1695 : : }
1696 : : }
1697 : :
1698 : 44183881 : if (t1 == t2)
1699 : : return true;
1700 : :
1701 : : /* Different classes of types can't be compatible. */
1702 : :
1703 : 44182511 : if (TREE_CODE (t1) != TREE_CODE (t2))
1704 : : return false;
1705 : :
1706 : : /* Qualifiers must match. C99 6.7.3p9 */
1707 : :
1708 : 36295677 : if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1709 : : return false;
1710 : :
1711 : : /* Allow for two different type nodes which have essentially the same
1712 : : definition. Note that we already checked for equality of the type
1713 : : qualifiers (just above). */
1714 : :
1715 : 36256454 : if (TREE_CODE (t1) != ARRAY_TYPE
1716 : 36256454 : && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1717 : : return true;
1718 : :
1719 : 35925197 : int attrval;
1720 : :
1721 : : /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1722 : 35925197 : if (!(attrval = comp_type_attributes (t1, t2)))
1723 : : return false;
1724 : :
1725 : 35924836 : if (2 == attrval)
1726 : 336 : data->warning_needed = true;
1727 : :
1728 : 35924836 : switch (TREE_CODE (t1))
1729 : : {
1730 : 3297661 : case INTEGER_TYPE:
1731 : 3297661 : case FIXED_POINT_TYPE:
1732 : 3297661 : case REAL_TYPE:
1733 : 3297661 : case BITINT_TYPE:
1734 : : /* With these nodes, we can't determine type equivalence by
1735 : : looking at what is stored in the nodes themselves, because
1736 : : two nodes might have different TYPE_MAIN_VARIANTs but still
1737 : : represent the same type. For example, wchar_t and int could
1738 : : have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1739 : : TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1740 : : and are distinct types. On the other hand, int and the
1741 : : following typedef
1742 : :
1743 : : typedef int INT __attribute((may_alias));
1744 : :
1745 : : have identical properties, different TYPE_MAIN_VARIANTs, but
1746 : : represent the same type. The canonical type system keeps
1747 : : track of equivalence in this case, so we fall back on it. */
1748 : 3297661 : return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1749 : :
1750 : 271364 : case POINTER_TYPE:
1751 : : /* Do not remove mode information. */
1752 : 271364 : if (TYPE_MODE (t1) != TYPE_MODE (t2))
1753 : : return false;
1754 : 271364 : data->pointedto = true;
1755 : 271364 : return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1756 : :
1757 : 8775637 : case FUNCTION_TYPE:
1758 : 8775637 : return function_types_compatible_p (t1, t2, data);
1759 : :
1760 : 151267 : case ARRAY_TYPE:
1761 : 151267 : {
1762 : 151267 : tree d1 = TYPE_DOMAIN (t1);
1763 : 151267 : tree d2 = TYPE_DOMAIN (t2);
1764 : 151267 : bool d1_variable, d2_variable;
1765 : 151267 : bool d1_zero, d2_zero;
1766 : :
1767 : : /* Target types must match incl. qualifiers. */
1768 : 151267 : if (!comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data))
1769 : : return false;
1770 : :
1771 : 117677 : if ((d1 == NULL_TREE) != (d2 == NULL_TREE))
1772 : 6265 : data->different_types_p = true;
1773 : : /* Ignore size mismatches when forming equivalence classes. */
1774 : 117677 : if (data->equiv)
1775 : : return true;
1776 : : /* Sizes must match unless one is missing or variable. */
1777 : 49765 : if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1778 : : return true;
1779 : :
1780 : 33576 : d1_zero = !TYPE_MAX_VALUE (d1);
1781 : 33576 : d2_zero = !TYPE_MAX_VALUE (d2);
1782 : :
1783 : 67152 : d1_variable = (!d1_zero
1784 : 33576 : && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1785 : 33570 : || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1786 : 67152 : d2_variable = (!d2_zero
1787 : 33576 : && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1788 : 33480 : || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1789 : :
1790 : 33576 : if (d1_variable != d2_variable)
1791 : 764 : data->different_types_p = true;
1792 : 33576 : if (d1_variable || d2_variable)
1793 : : return true;
1794 : 4038 : if (d1_zero && d2_zero)
1795 : : return true;
1796 : 4038 : if (d1_zero || d2_zero
1797 : 3942 : || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1798 : 7980 : || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1799 : 4038 : return false;
1800 : :
1801 : : return true;
1802 : : }
1803 : :
1804 : 22911812 : case ENUMERAL_TYPE:
1805 : 22911812 : case RECORD_TYPE:
1806 : 22911812 : case UNION_TYPE:
1807 : :
1808 : 22911812 : if (!flag_isoc23)
1809 : : return false;
1810 : :
1811 : 22911766 : return tagged_types_tu_compatible_p (t1, t2, data);
1812 : :
1813 : 516530 : case VECTOR_TYPE:
1814 : 1033060 : return known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1815 : 516530 : && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
1816 : :
1817 : : default:
1818 : : return false;
1819 : : }
1820 : : gcc_unreachable ();
1821 : : }
1822 : :
1823 : : /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1824 : : their qualifiers, except for named address spaces. If the pointers point to
1825 : : different named addresses, then we must determine if one address space is a
1826 : : subset of the other. */
1827 : :
1828 : : static bool
1829 : 1734755 : comp_target_types (location_t location, tree ttl, tree ttr)
1830 : : {
1831 : 1734755 : int val;
1832 : 1734755 : int val_ped;
1833 : 1734755 : tree mvl = TREE_TYPE (ttl);
1834 : 1734755 : tree mvr = TREE_TYPE (ttr);
1835 : 1734755 : addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1836 : 1734755 : addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1837 : 1734755 : addr_space_t as_common;
1838 : 1734755 : bool enum_and_int_p;
1839 : :
1840 : : /* Fail if pointers point to incompatible address spaces. */
1841 : 1734755 : if (!addr_space_superset (asl, asr, &as_common))
1842 : : return 0;
1843 : :
1844 : : /* For pedantic record result of comptypes on arrays before losing
1845 : : qualifiers on the element type below. */
1846 : 1734755 : val_ped = 1;
1847 : :
1848 : 1734755 : if (TREE_CODE (mvl) == ARRAY_TYPE
1849 : 1990 : && TREE_CODE (mvr) == ARRAY_TYPE)
1850 : 1962 : val_ped = comptypes (mvl, mvr);
1851 : :
1852 : : /* Qualifiers on element types of array types that are
1853 : : pointer targets are also removed. */
1854 : 1734755 : mvl = remove_qualifiers (mvl);
1855 : 1734755 : mvr = remove_qualifiers (mvr);
1856 : :
1857 : 1734755 : enum_and_int_p = false;
1858 : 1734755 : val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1859 : :
1860 : 1734755 : if (val == 1 && val_ped != 1)
1861 : 190 : pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1862 : : "in ISO C before C23");
1863 : :
1864 : 1734755 : if (val == 2)
1865 : 126 : pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1866 : :
1867 : 1734755 : if (val == 1 && enum_and_int_p && warn_cxx_compat)
1868 : 2 : warning_at (location, OPT_Wc___compat,
1869 : : "pointer target types incompatible in C++");
1870 : :
1871 : 1734755 : return val;
1872 : : }
1873 : :
1874 : : /* Subroutines of `comptypes'. */
1875 : :
1876 : : /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1877 : : compatible. The two types are not the same (which has been
1878 : : checked earlier in comptypes_internal). */
1879 : :
1880 : : static bool
1881 : 22911766 : tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1882 : : struct comptypes_data *data)
1883 : : {
1884 : 22911766 : tree s1, s2;
1885 : :
1886 : 22911766 : if (c_type_tag (t1) != c_type_tag (t2))
1887 : : return false;
1888 : :
1889 : : /* When forming equivalence classes for TYPE_CANONICAL in C23, we treat
1890 : : structs with the same tag as equivalent, but only when they are targets
1891 : : of pointers inside other structs. */
1892 : 18939063 : if (data->equiv && data->pointedto)
1893 : : return true;
1894 : :
1895 : : /* Different types without tag are incompatible except as an anonymous
1896 : : field or when forming equivalence classes for TYPE_CANONICAL. */
1897 : 18939029 : if (!data->anon_field && !data->equiv && NULL_TREE == c_type_tag (t1))
1898 : : return false;
1899 : :
1900 : 14030090 : if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
1901 : 14029097 : data->different_types_p = true;
1902 : :
1903 : : /* Incomplete types are incompatible inside a TU. */
1904 : 14030090 : if (TYPE_SIZE (t1) == NULL || TYPE_SIZE (t2) == NULL)
1905 : : return false;
1906 : :
1907 : 14030080 : if (ENUMERAL_TYPE != TREE_CODE (t1)
1908 : 14030080 : && (TYPE_REVERSE_STORAGE_ORDER (t1)
1909 : 14030025 : != TYPE_REVERSE_STORAGE_ORDER (t2)))
1910 : : return false;
1911 : :
1912 : 14030070 : if (TYPE_USER_ALIGN (t1) != TYPE_USER_ALIGN (t2))
1913 : 141129 : data->different_types_p = true;
1914 : :
1915 : : /* For types already being looked at in some active
1916 : : invocation of this function, assume compatibility.
1917 : : The cache is built as a linked list on the stack
1918 : : with the head of the list passed downwards. */
1919 : 14030070 : for (const struct tagged_tu_seen_cache *t = data->cache;
1920 : 14038933 : t != NULL; t = t->next)
1921 : 8974 : if (t->t1 == t1 && t->t2 == t2)
1922 : : return true;
1923 : :
1924 : 14029959 : const struct tagged_tu_seen_cache entry = { data->cache, t1, t2 };
1925 : :
1926 : 14029959 : switch (TREE_CODE (t1))
1927 : : {
1928 : 55 : case ENUMERAL_TYPE:
1929 : 55 : {
1930 : 55 : if (!comptypes (ENUM_UNDERLYING_TYPE (t1), ENUM_UNDERLYING_TYPE (t2)))
1931 : : return false;
1932 : :
1933 : : /* Speed up the case where the type values are in the same order. */
1934 : 51 : tree tv1 = TYPE_VALUES (t1);
1935 : 51 : tree tv2 = TYPE_VALUES (t2);
1936 : :
1937 : 51 : if (tv1 == tv2)
1938 : : return true;
1939 : :
1940 : 97 : for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1941 : : {
1942 : 58 : if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1943 : : break;
1944 : :
1945 : 51 : if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1)),
1946 : 51 : DECL_INITIAL (TREE_VALUE (tv2))) != 1)
1947 : : break;
1948 : : }
1949 : :
1950 : 51 : if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1951 : : return true;
1952 : :
1953 : 12 : if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1954 : : return false;
1955 : :
1956 : 12 : if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1957 : : return false;
1958 : :
1959 : 20 : for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1960 : : {
1961 : 16 : s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1962 : :
1963 : 16 : if (s2 == NULL
1964 : 29 : || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1)),
1965 : 13 : DECL_INITIAL (TREE_VALUE (s2))) != 1)
1966 : 8 : return false;
1967 : : }
1968 : :
1969 : : return true;
1970 : : }
1971 : :
1972 : 14029904 : case UNION_TYPE:
1973 : 14029904 : case RECORD_TYPE:
1974 : :
1975 : 14029904 : if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1976 : : return false;
1977 : :
1978 : 12176656 : for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1979 : 13686831 : s1 && s2;
1980 : 1510175 : s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1981 : : {
1982 : 13623641 : gcc_assert (TREE_CODE (s1) == FIELD_DECL);
1983 : 13623641 : gcc_assert (TREE_CODE (s2) == FIELD_DECL);
1984 : :
1985 : 13623641 : tree ft1 = TREE_TYPE (s1);
1986 : 13623641 : tree ft2 = TREE_TYPE (s2);
1987 : :
1988 : 13623641 : if (DECL_NAME (s1) != DECL_NAME (s2))
1989 : : return false;
1990 : :
1991 : 12042733 : if (DECL_ALIGN (s1) != DECL_ALIGN (s2))
1992 : : return false;
1993 : :
1994 : 3194852 : if (DECL_C_BIT_FIELD (s1) != DECL_C_BIT_FIELD (s2))
1995 : : return false;
1996 : :
1997 : 3194832 : if (DECL_C_BIT_FIELD (s1))
1998 : : {
1999 : 496 : if (!tree_int_cst_equal (DECL_SIZE (s1), DECL_SIZE (s2)))
2000 : : return false;
2001 : :
2002 : 342 : ft1 = DECL_BIT_FIELD_TYPE (s1);
2003 : 342 : ft2 = DECL_BIT_FIELD_TYPE (s2);
2004 : : }
2005 : :
2006 : 3194678 : if (!ft1 || !ft2)
2007 : : return false;
2008 : :
2009 : 3194678 : if (TREE_CODE (ft1) == ERROR_MARK || TREE_CODE (ft2) == ERROR_MARK)
2010 : : return false;
2011 : :
2012 : 3194677 : data->anon_field = !DECL_NAME (s1);
2013 : 3194677 : data->pointedto = false;
2014 : :
2015 : 3194677 : const struct tagged_tu_seen_cache *cache = data->cache;
2016 : 3194677 : data->cache = &entry;
2017 : 3194677 : bool ret = comptypes_internal (ft1, ft2, data);
2018 : 3194677 : data->cache = cache;
2019 : 3194677 : if (!ret)
2020 : : return false;
2021 : :
2022 : 1578037 : tree st1 = TYPE_SIZE (TREE_TYPE (s1));
2023 : 1578037 : tree st2 = TYPE_SIZE (TREE_TYPE (s2));
2024 : :
2025 : 1578037 : if (data->equiv
2026 : 984854 : && st1 && TREE_CODE (st1) == INTEGER_CST
2027 : 984818 : && st2 && TREE_CODE (st2) == INTEGER_CST
2028 : 2562852 : && !tree_int_cst_equal (st1, st2))
2029 : : return false;
2030 : :
2031 : 1510209 : tree counted_by1 = lookup_attribute ("counted_by",
2032 : 1510209 : DECL_ATTRIBUTES (s1));
2033 : 1510209 : tree counted_by2 = lookup_attribute ("counted_by",
2034 : 1510209 : DECL_ATTRIBUTES (s2));
2035 : : /* If there is no counted_by attribute for both fields. */
2036 : 1510209 : if (!counted_by1 && !counted_by2)
2037 : 1510155 : continue;
2038 : :
2039 : : /* If only one field has counted_by attribute. */
2040 : 54 : if ((counted_by1 && !counted_by2)
2041 : 54 : || (!counted_by1 && counted_by2))
2042 : : return false;
2043 : :
2044 : : /* Now both s1 and s2 have counted_by attributes, check
2045 : : whether they are the same. */
2046 : :
2047 : 42 : tree counted_by_field1
2048 : 42 : = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1)));
2049 : 42 : tree counted_by_field2
2050 : 42 : = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2)));
2051 : :
2052 : 42 : gcc_assert (counted_by_field1 && counted_by_field2);
2053 : :
2054 : 84 : while (TREE_CHAIN (counted_by_field1))
2055 : 42 : counted_by_field1 = TREE_CHAIN (counted_by_field1);
2056 : 76 : while (TREE_CHAIN (counted_by_field2))
2057 : 34 : counted_by_field2 = TREE_CHAIN (counted_by_field2);
2058 : :
2059 : 42 : if (DECL_NAME (TREE_VALUE (counted_by_field1))
2060 : 42 : != DECL_NAME (TREE_VALUE (counted_by_field2)))
2061 : : return false;
2062 : : }
2063 : : return true;
2064 : :
2065 : 0 : default:
2066 : 0 : gcc_unreachable ();
2067 : : }
2068 : : }
2069 : :
2070 : : /* Return true if two function types F1 and F2 are compatible.
2071 : : If either type specifies no argument types,
2072 : : the other must specify a fixed number of self-promoting arg types.
2073 : : Otherwise, if one type specifies only the number of arguments,
2074 : : the other must specify that number of self-promoting arg types.
2075 : : Otherwise, the argument types must match. */
2076 : :
2077 : : static bool
2078 : 8775637 : function_types_compatible_p (const_tree f1, const_tree f2,
2079 : : struct comptypes_data *data)
2080 : : {
2081 : 8775637 : tree ret1 = TREE_TYPE (f1);
2082 : 8775637 : tree ret2 = TREE_TYPE (f2);
2083 : :
2084 : : /* 'volatile' qualifiers on a function's return type used to mean
2085 : : the function is noreturn. */
2086 : 8775637 : if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
2087 : 29 : pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
2088 : 8775637 : if (TYPE_VOLATILE (ret1))
2089 : 15 : ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
2090 : 15 : TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
2091 : 8775637 : if (TYPE_VOLATILE (ret2))
2092 : 18 : ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
2093 : 18 : TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
2094 : :
2095 : 8775637 : bool ignore_pargs = data->ignore_promoting_args;
2096 : 8775637 : data->ignore_promoting_args = false;
2097 : :
2098 : 8775637 : if (!comptypes_internal (ret1, ret2, data))
2099 : : return false;
2100 : :
2101 : 8773050 : data->ignore_promoting_args = ignore_pargs;
2102 : :
2103 : 8773050 : tree args1 = TYPE_ARG_TYPES (f1);
2104 : 8773050 : tree args2 = TYPE_ARG_TYPES (f2);
2105 : :
2106 : 8773050 : if ((args1 == NULL_TREE) != (args2 == NULL_TREE))
2107 : 40978 : data->different_types_p = true;
2108 : :
2109 : : /* An unspecified parmlist matches any specified parmlist
2110 : : whose argument types don't need default promotions. */
2111 : :
2112 : 8773050 : if (args1 == NULL_TREE)
2113 : : {
2114 : 21630 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2115 : : return false;
2116 : 21628 : if (!(data->ignore_promoting_args || self_promoting_args_p (args2)))
2117 : : return false;
2118 : 21158 : data->ignore_promoting_args = false;
2119 : : /* If one of these types comes from a non-prototype fn definition,
2120 : : compare that with the other type's arglist.
2121 : : If they don't match, ask for a warning (but no error). */
2122 : 21158 : if (TYPE_ACTUAL_ARG_TYPES (f1)
2123 : 21158 : && !type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1), data))
2124 : 12 : data->warning_needed = true;
2125 : 21158 : return true;
2126 : : }
2127 : 8751420 : if (args2 == NULL_TREE)
2128 : : {
2129 : 19429 : if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
2130 : : return false;
2131 : 19427 : if (!(data->ignore_promoting_args || self_promoting_args_p (args1)))
2132 : : return false;
2133 : 19366 : data->ignore_promoting_args = false;
2134 : 19366 : if (TYPE_ACTUAL_ARG_TYPES (f2)
2135 : 19366 : && !type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2), data))
2136 : 1 : data->warning_needed = true;
2137 : 19366 : return true;
2138 : : }
2139 : :
2140 : : /* Both types have argument lists: compare them and propagate results. */
2141 : 8731991 : return type_lists_compatible_p (args1, args2, data);
2142 : : }
2143 : :
2144 : : /* Check two lists of types for compatibility, returning false for
2145 : : incompatible, true for compatible. */
2146 : :
2147 : : static bool
2148 : 8732067 : type_lists_compatible_p (const_tree args1, const_tree args2,
2149 : : struct comptypes_data *data)
2150 : : {
2151 : 54398551 : while (1)
2152 : : {
2153 : 31565309 : if (args1 == NULL_TREE && args2 == NULL_TREE)
2154 : : return true;
2155 : : /* If one list is shorter than the other,
2156 : : they fail to match. */
2157 : 22976069 : if (args1 == NULL_TREE || args2 == NULL_TREE)
2158 : : return false;
2159 : 22976062 : tree a1 = TREE_VALUE (args1);
2160 : 22976062 : tree a2 = TREE_VALUE (args2);
2161 : 22976062 : tree mv1 = remove_qualifiers (a1);
2162 : 22976062 : tree mv2 = remove_qualifiers (a2);
2163 : : /* A null pointer instead of a type
2164 : : means there is supposed to be an argument
2165 : : but nothing is specified about what type it has.
2166 : : So match anything that self-promotes. */
2167 : 22976062 : if ((a1 == NULL_TREE) != (a2 == NULL_TREE))
2168 : 0 : data->different_types_p = true;
2169 : 22976062 : if (a1 == NULL_TREE)
2170 : : {
2171 : 0 : if (c_type_promotes_to (a2) != a2)
2172 : : return false;
2173 : : }
2174 : 22976062 : else if (a2 == NULL_TREE)
2175 : : {
2176 : 0 : if (c_type_promotes_to (a1) != a1)
2177 : : return false;
2178 : : }
2179 : : /* If one of the lists has an error marker, ignore this arg. */
2180 : 22976062 : else if (TREE_CODE (a1) == ERROR_MARK
2181 : 22976058 : || TREE_CODE (a2) == ERROR_MARK)
2182 : : ;
2183 : 22976042 : else if (!comptypes_internal (mv1, mv2, data))
2184 : : {
2185 : 143037 : data->different_types_p = true;
2186 : : /* Allow wait (union {union wait *u; int *i} *)
2187 : : and wait (union wait *) to be compatible. */
2188 : 143037 : if (TREE_CODE (a1) == UNION_TYPE
2189 : 122 : && (TYPE_NAME (a1) == NULL_TREE
2190 : 94 : || TYPE_TRANSPARENT_AGGR (a1))
2191 : 122 : && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
2192 : 143159 : && tree_int_cst_equal (TYPE_SIZE (a1),
2193 : 122 : TYPE_SIZE (a2)))
2194 : : {
2195 : 122 : tree memb;
2196 : 122 : for (memb = TYPE_FIELDS (a1);
2197 : 158 : memb; memb = DECL_CHAIN (memb))
2198 : : {
2199 : 156 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2200 : 156 : if (comptypes_internal (mv3, mv2, data))
2201 : : break;
2202 : : }
2203 : : if (memb == NULL_TREE)
2204 : : return false;
2205 : : }
2206 : 142915 : else if (TREE_CODE (a2) == UNION_TYPE
2207 : 99 : && (TYPE_NAME (a2) == NULL_TREE
2208 : 71 : || TYPE_TRANSPARENT_AGGR (a2))
2209 : 99 : && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
2210 : 143014 : && tree_int_cst_equal (TYPE_SIZE (a2),
2211 : 99 : TYPE_SIZE (a1)))
2212 : : {
2213 : 99 : tree memb;
2214 : 99 : for (memb = TYPE_FIELDS (a2);
2215 : 127 : memb; memb = DECL_CHAIN (memb))
2216 : : {
2217 : 125 : tree mv3 = remove_qualifiers (TREE_TYPE (memb));
2218 : 125 : if (comptypes_internal (mv3, mv1, data))
2219 : : break;
2220 : : }
2221 : : if (memb == NULL_TREE)
2222 : : return false;
2223 : : }
2224 : : else
2225 : 142816 : return false;
2226 : : }
2227 : :
2228 : 22833242 : args1 = TREE_CHAIN (args1);
2229 : 22833242 : args2 = TREE_CHAIN (args2);
2230 : 22833242 : }
2231 : : }
2232 : :
2233 : : /* Compute the size to increment a pointer by. When a function type or void
2234 : : type or incomplete type is passed, size_one_node is returned.
2235 : : This function does not emit any diagnostics; the caller is responsible
2236 : : for that. */
2237 : :
2238 : : static tree
2239 : 232627 : c_size_in_bytes (const_tree type)
2240 : : {
2241 : 232627 : enum tree_code code = TREE_CODE (type);
2242 : :
2243 : 232627 : if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
2244 : 232627 : || !COMPLETE_TYPE_P (type))
2245 : 180 : return size_one_node;
2246 : :
2247 : : /* Convert in case a char is more than one unit. */
2248 : 232447 : return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
2249 : 232447 : size_int (TYPE_PRECISION (char_type_node)
2250 : : / BITS_PER_UNIT));
2251 : : }
2252 : :
2253 : : /* Return either DECL or its known constant value (if it has one). */
2254 : :
2255 : : tree
2256 : 13989098 : decl_constant_value_1 (tree decl, bool in_init)
2257 : : {
2258 : 13989098 : if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
2259 : 13989098 : TREE_CODE (decl) != PARM_DECL
2260 : 13989098 : && !TREE_THIS_VOLATILE (decl)
2261 : 13645523 : && TREE_READONLY (decl)
2262 : 36300 : && DECL_INITIAL (decl) != NULL_TREE
2263 : 35305 : && !error_operand_p (DECL_INITIAL (decl))
2264 : : /* This is invalid if initial value is not constant.
2265 : : If it has either a function call, a memory reference,
2266 : : or a variable, then re-evaluating it could give different results. */
2267 : 35296 : && TREE_CONSTANT (DECL_INITIAL (decl))
2268 : : /* Check for cases where this is sub-optimal, even though valid. */
2269 : 14011556 : && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
2270 : 17253 : return DECL_INITIAL (decl);
2271 : : return decl;
2272 : : }
2273 : :
2274 : : /* Return either DECL or its known constant value (if it has one).
2275 : : Like the above, but always return decl outside of functions. */
2276 : :
2277 : : tree
2278 : 13988854 : decl_constant_value (tree decl)
2279 : : {
2280 : : /* Don't change a variable array bound or initial value to a constant
2281 : : in a place where a variable is invalid. */
2282 : 13988854 : return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
2283 : : }
2284 : :
2285 : : /* Convert the array expression EXP to a pointer. */
2286 : : static tree
2287 : 815421 : array_to_pointer_conversion (location_t loc, tree exp)
2288 : : {
2289 : 815421 : tree orig_exp = exp;
2290 : 815421 : tree type = TREE_TYPE (exp);
2291 : 815421 : tree adr;
2292 : 815421 : tree restype = TREE_TYPE (type);
2293 : 815421 : tree ptrtype;
2294 : :
2295 : 815421 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2296 : :
2297 : 815421 : STRIP_TYPE_NOPS (exp);
2298 : :
2299 : 815421 : copy_warning (exp, orig_exp);
2300 : :
2301 : 815421 : ptrtype = c_build_pointer_type (restype);
2302 : :
2303 : 815421 : if (INDIRECT_REF_P (exp))
2304 : 2883 : return convert (ptrtype, TREE_OPERAND (exp, 0));
2305 : :
2306 : : /* In C++ array compound literals are temporary objects unless they are
2307 : : const or appear in namespace scope, so they are destroyed too soon
2308 : : to use them for much of anything (c++/53220). */
2309 : 812538 : if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
2310 : : {
2311 : 48 : tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2312 : 48 : if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
2313 : 46 : warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
2314 : : "converting an array compound literal to a pointer "
2315 : : "leads to a dangling pointer in C++");
2316 : : }
2317 : :
2318 : 812538 : adr = build_unary_op (loc, ADDR_EXPR, exp, true);
2319 : 812538 : return convert (ptrtype, adr);
2320 : : }
2321 : :
2322 : : /* Convert the function expression EXP to a pointer. */
2323 : : static tree
2324 : 49313717 : function_to_pointer_conversion (location_t loc, tree exp)
2325 : : {
2326 : 49313717 : tree orig_exp = exp;
2327 : :
2328 : 49313717 : gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
2329 : :
2330 : 49313717 : STRIP_TYPE_NOPS (exp);
2331 : :
2332 : 49313717 : copy_warning (exp, orig_exp);
2333 : :
2334 : 49313717 : tree exp2 = build_unary_op (loc, ADDR_EXPR, exp, false);
2335 : :
2336 : : /* If the function is defined and known to not to require a non-local
2337 : : context, make sure no trampoline is generated. */
2338 : 49313717 : if (TREE_CODE (exp) == FUNCTION_DECL
2339 : 49313717 : && DECL_INITIAL (exp) && !C_FUNC_NONLOCAL_CONTEXT (exp))
2340 : 15650192 : TREE_NO_TRAMPOLINE (exp2) = 1;
2341 : :
2342 : 49313717 : return exp2;
2343 : : }
2344 : :
2345 : : /* Mark EXP as read, not just set, for set but not used -Wunused
2346 : : warning purposes. */
2347 : :
2348 : : void
2349 : 479497467 : mark_exp_read (tree exp)
2350 : : {
2351 : 602622126 : switch (TREE_CODE (exp))
2352 : : {
2353 : 198396443 : case VAR_DECL:
2354 : 198396443 : case PARM_DECL:
2355 : 198396443 : DECL_READ_P (exp) = 1;
2356 : 198396443 : break;
2357 : 19242364 : CASE_CONVERT:
2358 : 19242364 : if (VOID_TYPE_P (TREE_TYPE (exp)))
2359 : 3791 : switch (TREE_CODE (TREE_OPERAND (exp, 0)))
2360 : : {
2361 : : case PREINCREMENT_EXPR:
2362 : : case PREDECREMENT_EXPR:
2363 : : case POSTINCREMENT_EXPR:
2364 : : case POSTDECREMENT_EXPR:
2365 : : return;
2366 : : default:
2367 : : break;
2368 : : }
2369 : : /* FALLTHRU */
2370 : 120439198 : case ARRAY_REF:
2371 : 120439198 : case COMPONENT_REF:
2372 : 120439198 : case MODIFY_EXPR:
2373 : 120439198 : case REALPART_EXPR:
2374 : 120439198 : case IMAGPART_EXPR:
2375 : 120439198 : case ADDR_EXPR:
2376 : 120439198 : case VIEW_CONVERT_EXPR:
2377 : 120439198 : case PREINCREMENT_EXPR:
2378 : 120439198 : case PREDECREMENT_EXPR:
2379 : 120439198 : case POSTINCREMENT_EXPR:
2380 : 120439198 : case POSTDECREMENT_EXPR:
2381 : 120439198 : mark_exp_read (TREE_OPERAND (exp, 0));
2382 : 120439198 : break;
2383 : 235939 : case COMPOUND_EXPR:
2384 : : /* Pattern match what build_atomic_assign produces with modifycode
2385 : : NOP_EXPR. */
2386 : 235939 : if (VAR_P (TREE_OPERAND (exp, 1))
2387 : 27227 : && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
2388 : 263082 : && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
2389 : : {
2390 : 2210 : tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2391 : 2210 : tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2392 : 2210 : if (TREE_CODE (t1) == TARGET_EXPR
2393 : 2202 : && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2394 : 4410 : && TREE_CODE (t2) == CALL_EXPR)
2395 : : {
2396 : 2200 : tree fndecl = get_callee_fndecl (t2);
2397 : 2200 : tree arg = NULL_TREE;
2398 : 2200 : if (fndecl
2399 : 2200 : && TREE_CODE (fndecl) == FUNCTION_DECL
2400 : 2200 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2401 : 4400 : && call_expr_nargs (t2) >= 2)
2402 : 2200 : switch (DECL_FUNCTION_CODE (fndecl))
2403 : : {
2404 : 130 : case BUILT_IN_ATOMIC_STORE:
2405 : 130 : arg = CALL_EXPR_ARG (t2, 1);
2406 : 130 : break;
2407 : 2070 : case BUILT_IN_ATOMIC_STORE_1:
2408 : 2070 : case BUILT_IN_ATOMIC_STORE_2:
2409 : 2070 : case BUILT_IN_ATOMIC_STORE_4:
2410 : 2070 : case BUILT_IN_ATOMIC_STORE_8:
2411 : 2070 : case BUILT_IN_ATOMIC_STORE_16:
2412 : 2070 : arg = CALL_EXPR_ARG (t2, 0);
2413 : 2070 : break;
2414 : : default:
2415 : : break;
2416 : : }
2417 : 2200 : if (arg)
2418 : : {
2419 : 2200 : STRIP_NOPS (arg);
2420 : 2200 : if (TREE_CODE (arg) == ADDR_EXPR
2421 : 2200 : && DECL_P (TREE_OPERAND (arg, 0))
2422 : 4400 : && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2423 : 2200 : mark_exp_read (TREE_OPERAND (arg, 0));
2424 : : }
2425 : : }
2426 : : }
2427 : : /* FALLTHRU */
2428 : 2684926 : case C_MAYBE_CONST_EXPR:
2429 : 2684926 : mark_exp_read (TREE_OPERAND (exp, 1));
2430 : 2684926 : break;
2431 : 570 : case OMP_ARRAY_SECTION:
2432 : 570 : mark_exp_read (TREE_OPERAND (exp, 0));
2433 : 570 : if (TREE_OPERAND (exp, 1))
2434 : 377 : mark_exp_read (TREE_OPERAND (exp, 1));
2435 : 570 : if (TREE_OPERAND (exp, 2))
2436 : 535 : mark_exp_read (TREE_OPERAND (exp, 2));
2437 : : break;
2438 : : default:
2439 : : break;
2440 : : }
2441 : : }
2442 : :
2443 : : /* Perform the default conversion of arrays and functions to pointers.
2444 : : Return the result of converting EXP. For any other expression, just
2445 : : return EXP.
2446 : :
2447 : : LOC is the location of the expression. */
2448 : :
2449 : : struct c_expr
2450 : 353092250 : default_function_array_conversion (location_t loc, struct c_expr exp)
2451 : : {
2452 : 353092250 : tree orig_exp = exp.value;
2453 : 353092250 : tree type = TREE_TYPE (exp.value);
2454 : 353092250 : enum tree_code code = TREE_CODE (type);
2455 : :
2456 : 353092250 : switch (code)
2457 : : {
2458 : : case ARRAY_TYPE:
2459 : : {
2460 : : bool not_lvalue = false;
2461 : : bool lvalue_array_p;
2462 : :
2463 : 768609 : while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2464 : 768609 : || CONVERT_EXPR_P (exp.value))
2465 : 768609 : && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2466 : : {
2467 : 0 : if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2468 : 0 : not_lvalue = true;
2469 : 0 : exp.value = TREE_OPERAND (exp.value, 0);
2470 : : }
2471 : :
2472 : 768609 : copy_warning (exp.value, orig_exp);
2473 : :
2474 : 768609 : lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2475 : 768609 : if (!flag_isoc99 && !lvalue_array_p)
2476 : : {
2477 : : /* Before C99, non-lvalue arrays do not decay to pointers.
2478 : : Normally, using such an array would be invalid; but it can
2479 : : be used correctly inside sizeof or as a statement expression.
2480 : : Thus, do not give an error here; an error will result later. */
2481 : 108 : return exp;
2482 : : }
2483 : :
2484 : 768501 : exp.value = array_to_pointer_conversion (loc, exp.value);
2485 : : }
2486 : 768501 : break;
2487 : 761795 : case FUNCTION_TYPE:
2488 : 761795 : exp.value = function_to_pointer_conversion (loc, exp.value);
2489 : 761795 : break;
2490 : : default:
2491 : : break;
2492 : : }
2493 : :
2494 : 353092142 : return exp;
2495 : : }
2496 : :
2497 : : struct c_expr
2498 : 927649 : default_function_array_read_conversion (location_t loc, struct c_expr exp)
2499 : : {
2500 : 927649 : mark_exp_read (exp.value);
2501 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2502 : 927649 : if (TREE_CODE (exp.value) == COMPONENT_REF
2503 : 927649 : && handle_counted_by_p (exp.value))
2504 : 65656 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2505 : 927649 : return default_function_array_conversion (loc, exp);
2506 : : }
2507 : :
2508 : : /* Return whether EXPR should be treated as an atomic lvalue for the
2509 : : purposes of load and store handling. */
2510 : :
2511 : : static bool
2512 : 354166607 : really_atomic_lvalue (tree expr)
2513 : : {
2514 : 354166607 : if (error_operand_p (expr))
2515 : : return false;
2516 : 354158913 : if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2517 : : return false;
2518 : 57308 : if (!COMPLETE_TYPE_P (TREE_TYPE (expr)))
2519 : : return false;
2520 : 57305 : if (!lvalue_p (expr))
2521 : : return false;
2522 : :
2523 : : /* Ignore _Atomic on register variables, since their addresses can't
2524 : : be taken so (a) atomicity is irrelevant and (b) the normal atomic
2525 : : sequences wouldn't work. Ignore _Atomic on structures containing
2526 : : bit-fields, since accessing elements of atomic structures or
2527 : : unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2528 : : it's undefined at translation time or execution time, and the
2529 : : normal atomic sequences again wouldn't work. */
2530 : 57367 : while (handled_component_p (expr))
2531 : : {
2532 : 64 : if (TREE_CODE (expr) == COMPONENT_REF
2533 : 64 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2534 : : return false;
2535 : 64 : expr = TREE_OPERAND (expr, 0);
2536 : : }
2537 : 57303 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2538 : 1 : expr = COMPOUND_LITERAL_EXPR_DECL (expr);
2539 : 112486 : if (DECL_P (expr) && C_DECL_REGISTER (expr))
2540 : : return false;
2541 : : return true;
2542 : : }
2543 : :
2544 : : /* If EXPR is a named constant (C23) derived from a constexpr variable
2545 : : - that is, a reference to such a variable, or a member extracted by
2546 : : a sequence of structure and union (but not array) member accesses
2547 : : (where union member accesses must access the same member as
2548 : : initialized) - then return the corresponding initializer;
2549 : : otherwise, return NULL_TREE. */
2550 : :
2551 : : static tree
2552 : 351039326 : maybe_get_constexpr_init (tree expr)
2553 : : {
2554 : 351039326 : tree decl = NULL_TREE;
2555 : 351039326 : if (TREE_CODE (expr) == VAR_DECL)
2556 : : decl = expr;
2557 : 339583319 : else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2558 : 802857 : decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2559 : 802857 : if (decl
2560 : 12258864 : && C_DECL_DECLARED_CONSTEXPR (decl)
2561 : 356 : && DECL_INITIAL (decl) != NULL_TREE
2562 : 803213 : && !error_operand_p (DECL_INITIAL (decl)))
2563 : 356 : return DECL_INITIAL (decl);
2564 : 351038970 : if (TREE_CODE (expr) != COMPONENT_REF)
2565 : : return NULL_TREE;
2566 : 856930 : tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2567 : 856930 : if (inner == NULL_TREE)
2568 : : return NULL_TREE;
2569 : 126 : while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2570 : 47 : && !error_operand_p (inner)
2571 : 220 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2572 : 47 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2573 : 47 : inner = TREE_OPERAND (inner, 0);
2574 : 126 : if (TREE_CODE (inner) != CONSTRUCTOR)
2575 : : return NULL_TREE;
2576 : 126 : tree field = TREE_OPERAND (expr, 1);
2577 : 126 : unsigned HOST_WIDE_INT cidx;
2578 : 126 : tree cfield, cvalue;
2579 : 126 : bool have_other_init = false;
2580 : 266 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2581 : : {
2582 : 250 : if (cfield == field)
2583 : : return cvalue;
2584 : 140 : have_other_init = true;
2585 : : }
2586 : 16 : if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2587 : 16 : && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2588 : : return NULL_TREE;
2589 : : /* Return a default initializer. */
2590 : 13 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2591 : 5 : return build_constructor (TREE_TYPE (expr), NULL);
2592 : 8 : return build_zero_cst (TREE_TYPE (expr));
2593 : : }
2594 : :
2595 : : /* Convert expression EXP (location LOC) from lvalue to rvalue,
2596 : : including converting functions and arrays to pointers if CONVERT_P.
2597 : : If READ_P, also mark the expression as having been read. If
2598 : : FOR_INIT, constexpr expressions of structure and union type should
2599 : : be replaced by the corresponding CONSTRUCTOR; otherwise, only
2600 : : constexpr scalars (including elements of structures and unions) are
2601 : : replaced by their initializers. */
2602 : :
2603 : : struct c_expr
2604 : 350368582 : convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2605 : : bool convert_p, bool read_p, bool for_init)
2606 : : {
2607 : 350368582 : bool force_non_npc = false;
2608 : 350368582 : if (read_p)
2609 : 307412516 : mark_exp_read (exp.value);
2610 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2611 : 307412516 : if (read_p && TREE_CODE (exp.value) == COMPONENT_REF
2612 : 767331 : && handle_counted_by_p (exp.value))
2613 : 767302 : exp.value = handle_counted_by_for_component_ref (loc, exp.value);
2614 : :
2615 : 350368582 : if (convert_p)
2616 : 350355435 : exp = default_function_array_conversion (loc, exp);
2617 : 350368582 : if (!VOID_TYPE_P (TREE_TYPE (exp.value))
2618 : 350368582 : || (flag_isoc2y
2619 : 1249 : && TYPE_QUALS (TREE_TYPE (exp.value)) != TYPE_UNQUALIFIED))
2620 : 347929961 : exp.value = require_complete_type (loc, exp.value);
2621 : 350368582 : if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2622 : : {
2623 : 350182396 : tree init = maybe_get_constexpr_init (exp.value);
2624 : 350182396 : if (init != NULL_TREE)
2625 : : {
2626 : : /* A named constant of pointer type or type nullptr_t is not
2627 : : a null pointer constant even if the initializer is
2628 : : one. */
2629 : 353 : if (TREE_CODE (init) == INTEGER_CST
2630 : 218 : && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2631 : 379 : && integer_zerop (init))
2632 : : force_non_npc = true;
2633 : : exp.value = init;
2634 : : }
2635 : : }
2636 : 350368582 : if (really_atomic_lvalue (exp.value))
2637 : : {
2638 : 26861 : vec<tree, va_gc> *params;
2639 : 26861 : tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2640 : 26861 : tree expr_type = TREE_TYPE (exp.value);
2641 : 26861 : tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2642 : 26861 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2643 : :
2644 : 26861 : gcc_assert (TYPE_ATOMIC (expr_type));
2645 : :
2646 : : /* Expansion of a generic atomic load may require an addition
2647 : : element, so allocate enough to prevent a resize. */
2648 : 26861 : vec_alloc (params, 4);
2649 : :
2650 : : /* Remove the qualifiers for the rest of the expressions and
2651 : : create the VAL temp variable to hold the RHS. */
2652 : 26861 : nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2653 : 26861 : tmp = create_tmp_var_raw (nonatomic_type);
2654 : 26861 : tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2655 : 26861 : TREE_ADDRESSABLE (tmp) = 1;
2656 : : /* Do not disable warnings for TMP even though it's artificial.
2657 : : -Winvalid-memory-model depends on it. */
2658 : :
2659 : : /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2660 : 26861 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2661 : 26861 : params->quick_push (expr_addr);
2662 : 26861 : params->quick_push (tmp_addr);
2663 : 26861 : params->quick_push (seq_cst);
2664 : 26861 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2665 : :
2666 : : /* EXPR is always read. */
2667 : 26861 : mark_exp_read (exp.value);
2668 : :
2669 : : /* Return tmp which contains the value loaded. */
2670 : 26861 : exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2671 : : NULL_TREE, NULL_TREE);
2672 : : }
2673 : 350355435 : if (convert_p && !error_operand_p (exp.value)
2674 : 700716367 : && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2675 : 350347677 : exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2676 : 350368582 : if (force_non_npc)
2677 : 24 : exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2678 : :
2679 : 350368582 : {
2680 : 350368582 : tree false_value, true_value;
2681 : 350355435 : if (convert_p && !error_operand_p (exp.value)
2682 : 700716367 : && c_hardbool_type_attr (TREE_TYPE (exp.value),
2683 : : &false_value, &true_value))
2684 : : {
2685 : 9717 : tree t = save_expr (exp.value);
2686 : :
2687 : 9717 : mark_exp_read (exp.value);
2688 : :
2689 : 9717 : tree trapfn = builtin_decl_explicit (BUILT_IN_TRAP);
2690 : 9717 : tree expr = build_call_expr_loc (loc, trapfn, 0);
2691 : 9717 : expr = build_compound_expr (loc, expr, boolean_true_node);
2692 : 9717 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2693 : : fold_build2_loc (loc, NE_EXPR,
2694 : : boolean_type_node,
2695 : : t, true_value),
2696 : : expr, boolean_true_node);
2697 : 9717 : expr = fold_build3_loc (loc, COND_EXPR, boolean_type_node,
2698 : : fold_build2_loc (loc, NE_EXPR,
2699 : : boolean_type_node,
2700 : : t, false_value),
2701 : : expr, boolean_false_node);
2702 : :
2703 : 9717 : exp.value = expr;
2704 : : }
2705 : : }
2706 : :
2707 : 350368582 : return exp;
2708 : : }
2709 : :
2710 : : /* Wrapper for the overload above, same arguments but for tree rather than
2711 : : c_expr. This is important for hardbools to decay to bools. */
2712 : :
2713 : : static inline tree
2714 : 562806 : convert_lvalue_to_rvalue (location_t loc, tree val,
2715 : : bool convert_p, bool read_p, bool for_init = false)
2716 : : {
2717 : 562806 : struct c_expr expr;
2718 : 562806 : memset (&expr, 0, sizeof (expr));
2719 : 562806 : expr.value = val;
2720 : 562806 : expr = convert_lvalue_to_rvalue (loc, expr, convert_p, read_p, for_init);
2721 : 562806 : return expr.value;
2722 : : }
2723 : :
2724 : : /* EXP is an expression of integer type. Apply the integer promotions
2725 : : to it and return the promoted value. */
2726 : :
2727 : : tree
2728 : 76789304 : perform_integral_promotions (tree exp)
2729 : : {
2730 : 76789304 : tree type = TREE_TYPE (exp);
2731 : 76789304 : enum tree_code code = TREE_CODE (type);
2732 : :
2733 : 76789304 : gcc_assert (INTEGRAL_TYPE_P (type));
2734 : :
2735 : : /* Convert enums to the result of applying the integer promotions to
2736 : : their underlying type. */
2737 : 76789304 : if (code == ENUMERAL_TYPE)
2738 : : {
2739 : 634609 : type = ENUM_UNDERLYING_TYPE (type);
2740 : 634609 : if (c_promoting_integer_type_p (type))
2741 : : {
2742 : 94 : if (TYPE_UNSIGNED (type)
2743 : 94 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2744 : 0 : type = unsigned_type_node;
2745 : : else
2746 : 94 : type = integer_type_node;
2747 : : }
2748 : :
2749 : 634609 : return convert (type, exp);
2750 : : }
2751 : :
2752 : : /* ??? This should no longer be needed now bit-fields have their
2753 : : proper types. */
2754 : 76154695 : if (TREE_CODE (exp) == COMPONENT_REF
2755 : 76154695 : && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1)))
2756 : : {
2757 : 62165 : if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)))
2758 : : == BITINT_TYPE)
2759 : 454 : return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1)), exp);
2760 : : /* If it's thinner than an int, promote it like a
2761 : : c_promoting_integer_type_p, otherwise leave it alone. */
2762 : 61711 : if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2763 : 61711 : TYPE_PRECISION (integer_type_node)) < 0)
2764 : 53449 : return convert (integer_type_node, exp);
2765 : : }
2766 : :
2767 : 76100792 : if (c_promoting_integer_type_p (type))
2768 : : {
2769 : : /* Preserve unsignedness if not really getting any wider. */
2770 : 629425 : if (TYPE_UNSIGNED (type)
2771 : 629425 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2772 : 0 : return convert (unsigned_type_node, exp);
2773 : :
2774 : 629425 : return convert (integer_type_node, exp);
2775 : : }
2776 : :
2777 : : return exp;
2778 : : }
2779 : :
2780 : :
2781 : : /* Perform default promotions for C data used in expressions.
2782 : : Enumeral types or short or char are converted to int.
2783 : : In addition, manifest constants symbols are replaced by their values. */
2784 : :
2785 : : tree
2786 : 86088300 : default_conversion (tree exp)
2787 : : {
2788 : 86088300 : tree orig_exp;
2789 : 86088300 : tree type = TREE_TYPE (exp);
2790 : 86088300 : enum tree_code code = TREE_CODE (type);
2791 : 86088300 : tree promoted_type;
2792 : :
2793 : 86088300 : mark_exp_read (exp);
2794 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
2795 : 86088300 : if (TREE_CODE (exp) == COMPONENT_REF
2796 : 86088300 : && handle_counted_by_p (exp))
2797 : 589190 : exp = handle_counted_by_for_component_ref (EXPR_LOCATION (exp), exp);
2798 : :
2799 : : /* Functions and arrays have been converted during parsing. */
2800 : 86088300 : gcc_assert (code != FUNCTION_TYPE);
2801 : 86088300 : if (code == ARRAY_TYPE)
2802 : : return exp;
2803 : :
2804 : : /* Constants can be used directly unless they're not loadable. */
2805 : 86088141 : if (TREE_CODE (exp) == CONST_DECL)
2806 : 0 : exp = DECL_INITIAL (exp);
2807 : :
2808 : : /* Strip no-op conversions. */
2809 : 86088141 : orig_exp = exp;
2810 : 86454533 : STRIP_TYPE_NOPS (exp);
2811 : :
2812 : 86088141 : copy_warning (exp, orig_exp);
2813 : :
2814 : 86088141 : if (code == VOID_TYPE)
2815 : : {
2816 : 2 : error_at (EXPR_LOC_OR_LOC (exp, input_location),
2817 : : "void value not ignored as it ought to be");
2818 : 2 : return error_mark_node;
2819 : : }
2820 : :
2821 : 86088139 : exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2822 : 86088139 : if (exp == error_mark_node)
2823 : : return error_mark_node;
2824 : :
2825 : 86087236 : promoted_type = targetm.promoted_type (type);
2826 : 86087236 : if (promoted_type)
2827 : 0 : return convert (promoted_type, exp);
2828 : :
2829 : 86087236 : if (INTEGRAL_TYPE_P (type))
2830 : 75797234 : return perform_integral_promotions (exp);
2831 : :
2832 : : return exp;
2833 : : }
2834 : :
2835 : : /* Look up COMPONENT in a structure or union TYPE.
2836 : :
2837 : : If the component name is not found, returns NULL_TREE. Otherwise,
2838 : : the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2839 : : stepping down the chain to the component, which is in the last
2840 : : TREE_VALUE of the list. Normally the list is of length one, but if
2841 : : the component is embedded within (nested) anonymous structures or
2842 : : unions, the list steps down the chain to the component. */
2843 : :
2844 : : tree
2845 : 2269738 : lookup_field (const_tree type, tree component)
2846 : : {
2847 : 2269738 : tree field;
2848 : :
2849 : : /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2850 : : to the field elements. Use a binary search on this array to quickly
2851 : : find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2852 : : will always be set for structures which have many elements.
2853 : :
2854 : : Duplicate field checking replaces duplicates with NULL_TREE so
2855 : : TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2856 : : case just iterate using DECL_CHAIN. */
2857 : :
2858 : 2566154 : if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2859 : 2566154 : && !seen_error ())
2860 : : {
2861 : 296395 : int bot, top, half;
2862 : 296395 : tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2863 : :
2864 : 296395 : field = TYPE_FIELDS (type);
2865 : 296395 : bot = 0;
2866 : 296395 : top = TYPE_LANG_SPECIFIC (type)->s->len;
2867 : 1361072 : while (top - bot > 1)
2868 : : {
2869 : 1332881 : half = (top - bot + 1) >> 1;
2870 : 1332881 : field = field_array[bot+half];
2871 : :
2872 : 1332881 : if (DECL_NAME (field) == NULL_TREE)
2873 : : {
2874 : : /* Step through all anon unions in linear fashion. */
2875 : 0 : while (DECL_NAME (field_array[bot]) == NULL_TREE)
2876 : : {
2877 : 0 : field = field_array[bot++];
2878 : 0 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2879 : : {
2880 : 0 : tree anon = lookup_field (TREE_TYPE (field), component);
2881 : :
2882 : 0 : if (anon)
2883 : 0 : return tree_cons (NULL_TREE, field, anon);
2884 : :
2885 : : /* The Plan 9 compiler permits referring
2886 : : directly to an anonymous struct/union field
2887 : : using a typedef name. */
2888 : 0 : if (flag_plan9_extensions
2889 : 0 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2890 : 0 : && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2891 : : == TYPE_DECL)
2892 : 0 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2893 : : == component))
2894 : : break;
2895 : : }
2896 : : }
2897 : :
2898 : : /* Entire record is only anon unions. */
2899 : 0 : if (bot > top)
2900 : : return NULL_TREE;
2901 : :
2902 : : /* Restart the binary search, with new lower bound. */
2903 : 0 : continue;
2904 : 0 : }
2905 : :
2906 : 1332881 : if (DECL_NAME (field) == component)
2907 : : break;
2908 : 1064677 : if (DECL_NAME (field) < component)
2909 : : bot += half;
2910 : : else
2911 : 880491 : top = bot + half;
2912 : : }
2913 : :
2914 : 296395 : if (DECL_NAME (field_array[bot]) == component)
2915 : : field = field_array[bot];
2916 : 268204 : else if (DECL_NAME (field) != component)
2917 : : return NULL_TREE;
2918 : : }
2919 : : else
2920 : : {
2921 : 5166464 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2922 : : {
2923 : 5157706 : if (DECL_NAME (field) == NULL_TREE
2924 : 5157706 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2925 : : {
2926 : 11098 : tree anon = lookup_field (TREE_TYPE (field), component);
2927 : :
2928 : 11098 : if (anon)
2929 : 2415 : return tree_cons (NULL_TREE, field, anon);
2930 : :
2931 : : /* The Plan 9 compiler permits referring directly to an
2932 : : anonymous struct/union field using a typedef
2933 : : name. */
2934 : 8683 : if (flag_plan9_extensions
2935 : 38 : && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2936 : 36 : && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2937 : 8689 : && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2938 : : == component))
2939 : : break;
2940 : : }
2941 : :
2942 : 5155287 : if (DECL_NAME (field) == component)
2943 : : break;
2944 : : }
2945 : :
2946 : 1970928 : if (field == NULL_TREE)
2947 : : return NULL_TREE;
2948 : : }
2949 : :
2950 : 2258565 : return tree_cons (NULL_TREE, field, NULL_TREE);
2951 : : }
2952 : :
2953 : : /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2954 : :
2955 : : static void
2956 : 101 : lookup_field_fuzzy_find_candidates (tree type, tree component,
2957 : : vec<tree> *candidates)
2958 : : {
2959 : 101 : tree field;
2960 : 258 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2961 : : {
2962 : 157 : if (DECL_NAME (field) == NULL_TREE
2963 : 157 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2964 : 30 : lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2965 : : candidates);
2966 : :
2967 : 157 : if (DECL_NAME (field))
2968 : 111 : candidates->safe_push (DECL_NAME (field));
2969 : : }
2970 : 101 : }
2971 : :
2972 : : /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2973 : : rather than returning a TREE_LIST for an exact match. */
2974 : :
2975 : : static tree
2976 : 71 : lookup_field_fuzzy (tree type, tree component)
2977 : : {
2978 : 71 : gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2979 : :
2980 : : /* First, gather a list of candidates. */
2981 : 71 : auto_vec <tree> candidates;
2982 : :
2983 : 71 : lookup_field_fuzzy_find_candidates (type, component,
2984 : : &candidates);
2985 : :
2986 : 71 : return find_closest_identifier (component, &candidates);
2987 : 71 : }
2988 : :
2989 : : /* Support function for build_component_ref's error-handling.
2990 : :
2991 : : Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2992 : : struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2993 : :
2994 : : static bool
2995 : 72 : should_suggest_deref_p (tree datum_type)
2996 : : {
2997 : : /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2998 : : allows "." for ptrs; we could be handling a failed attempt
2999 : : to access a property. */
3000 : 72 : if (c_dialect_objc ())
3001 : : return false;
3002 : :
3003 : : /* Only suggest it for pointers... */
3004 : 72 : if (TREE_CODE (datum_type) != POINTER_TYPE)
3005 : : return false;
3006 : :
3007 : : /* ...to structs/unions. */
3008 : 8 : tree underlying_type = TREE_TYPE (datum_type);
3009 : 8 : enum tree_code code = TREE_CODE (underlying_type);
3010 : 8 : if (code == RECORD_TYPE || code == UNION_TYPE)
3011 : : return true;
3012 : : else
3013 : 1 : return false;
3014 : : }
3015 : :
3016 : : /* Give a component ref REF, decide whether we should handle its counted_by
3017 : : attribute based on its context:
3018 : : Do not handle counted_by when in offsetof, typeof and alignof operator. */
3019 : :
3020 : : static bool
3021 : 2232098 : handle_counted_by_p (tree ref)
3022 : : {
3023 : 2232098 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3024 : 2232098 : tree datum = TREE_OPERAND (ref, 0);
3025 : : /* If the component_ref is build for a offsetof, i.e., the datum
3026 : : of the component_ref is a indirect_ref of null_pointer_node,
3027 : : we should not generate call to .ACCESS_WITH_SIZE. */
3028 : 2232098 : if (TREE_CODE (datum) == INDIRECT_REF
3029 : 2232098 : && TREE_OPERAND (datum, 0) == null_pointer_node)
3030 : : return false;
3031 : 2231772 : if (in_typeof || in_alignof)
3032 : 8783 : return false;
3033 : : return true;
3034 : : }
3035 : :
3036 : : /* Given a component ref REF, if there is a counted_by attribute attached,
3037 : : issue error when the element_type is a structure or union including a
3038 : : flexible array member. */
3039 : :
3040 : : static void
3041 : 2228296 : check_counted_by_attribute (location_t loc, tree ref)
3042 : : {
3043 : 2228296 : tree subdatum = TREE_OPERAND (ref, 1);
3044 : 2228296 : tree sub_type = TREE_TYPE (subdatum);
3045 : :
3046 : 2228296 : if (!c_flexible_array_member_type_p (sub_type)
3047 : 2228296 : && TREE_CODE (sub_type) != POINTER_TYPE)
3048 : : return;
3049 : :
3050 : 381005 : tree element_type = TREE_TYPE (sub_type);
3051 : :
3052 : 381005 : tree attr_counted_by = lookup_attribute ("counted_by",
3053 : 381005 : DECL_ATTRIBUTES (subdatum));
3054 : 381005 : if (attr_counted_by)
3055 : : {
3056 : : /* Issue error when the element_type is a structure or
3057 : : union including a flexible array member. */
3058 : 397 : if (RECORD_OR_UNION_TYPE_P (element_type)
3059 : 397 : && TYPE_INCLUDES_FLEXARRAY (element_type))
3060 : : {
3061 : 2 : error_at (loc,
3062 : : "%<counted_by%> attribute is not allowed for a pointer to"
3063 : : " structure or union with flexible array member");
3064 : 2 : return;
3065 : : }
3066 : : }
3067 : : }
3068 : :
3069 : : /* For a SUBDATUM field of a structure or union DATUM, generate a REF
3070 : : to the object that represents its counted_by per the attribute
3071 : : counted_by attached to this field if it's a flexible array member
3072 : : or a pointer field, otherwise return NULL_TREE.
3073 : : Set COUNTED_BY_TYPE to the TYPE of the counted_by field.
3074 : : For example, if:
3075 : :
3076 : : struct P {
3077 : : int k;
3078 : : int x[] __attribute__ ((counted_by (k)));
3079 : : } *p;
3080 : :
3081 : : for:
3082 : : p->x
3083 : :
3084 : : the ref to the object that represents its element count will be:
3085 : :
3086 : : &(p->k)
3087 : : */
3088 : :
3089 : : static tree
3090 : 492524 : build_counted_by_ref (tree datum, tree subdatum,
3091 : : tree *counted_by_type)
3092 : : {
3093 : 492524 : tree type = TREE_TYPE (datum);
3094 : 492524 : tree sub_type = TREE_TYPE (subdatum);
3095 : 492524 : if (!c_flexible_array_member_type_p (sub_type)
3096 : 492524 : && TREE_CODE (sub_type) != POINTER_TYPE)
3097 : : return NULL_TREE;
3098 : :
3099 : 492524 : tree attr_counted_by = lookup_attribute ("counted_by",
3100 : 492524 : DECL_ATTRIBUTES (subdatum));
3101 : 492524 : tree counted_by_ref = NULL_TREE;
3102 : 492524 : *counted_by_type = NULL_TREE;
3103 : 492524 : if (attr_counted_by)
3104 : : {
3105 : 298 : tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by));
3106 : 298 : counted_by_ref
3107 : 298 : = build_component_ref (UNKNOWN_LOCATION,
3108 : : datum, field_id,
3109 : : UNKNOWN_LOCATION, UNKNOWN_LOCATION);
3110 : 298 : counted_by_ref = build_fold_addr_expr (counted_by_ref);
3111 : :
3112 : : /* Get the TYPE of the counted_by field. */
3113 : 298 : tree counted_by_field = lookup_field (type, field_id);
3114 : 298 : gcc_assert (counted_by_field);
3115 : :
3116 : 392 : do
3117 : : {
3118 : 392 : *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field));
3119 : 392 : counted_by_field = TREE_CHAIN (counted_by_field);
3120 : : }
3121 : 392 : while (counted_by_field);
3122 : : }
3123 : : return counted_by_ref;
3124 : : }
3125 : :
3126 : : /* Given a COMPONENT_REF REF with the location LOC, the corresponding
3127 : : COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate the corresponding
3128 : : call to the internal function .ACCESS_WITH_SIZE.
3129 : :
3130 : : A: For the Flexible Array Member, Generate an INDIRECT_REF to a call to
3131 : : the internal function .ACCESS_WITH_SIZE.
3132 : :
3133 : : REF
3134 : :
3135 : : to:
3136 : :
3137 : : (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3138 : : TYPE_SIZE_UNIT for element)
3139 : :
3140 : : NOTE: The return type of this function is the POINTER type pointing
3141 : : to the original flexible array type. Then the type of the INDIRECT_REF
3142 : : is the original flexible array type.
3143 : : The type of the first argument of this function is a POINTER type
3144 : : to the original flexible array type.
3145 : :
3146 : : B: For pointers with counted_by, generate a call to the internal function
3147 : : .ACCESS_WITH_SIZE.
3148 : :
3149 : : REF
3150 : :
3151 : : to:
3152 : :
3153 : : .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, (* TYPE_OF_SIZE)0,
3154 : : TYPE_SIZE_UNIT for element)
3155 : :
3156 : : NOTE: The return type of this function is the original pointer type.
3157 : : The type of the first argument of this function is the original
3158 : : pointer type.
3159 : :
3160 : : The 3rd argument of the call is a constant 0 with the pointer TYPE whose
3161 : : pointee type is the TYPE of the object pointed by COUNTED_BY_REF.
3162 : :
3163 : : The 4th argument of the call is the TYPE_SIZE_UNIT of the element TYPE
3164 : : of the array.
3165 : :
3166 : : */
3167 : : static tree
3168 : 298 : build_access_with_size_for_counted_by (location_t loc, tree ref,
3169 : : tree counted_by_ref,
3170 : : tree counted_by_type)
3171 : : {
3172 : 298 : gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))
3173 : : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE);
3174 : :
3175 : 298 : bool is_fam = c_flexible_array_member_type_p (TREE_TYPE (ref));
3176 : :
3177 : : /* The result type of the call is a pointer to the flexible array type;
3178 : : or is the original ponter type to the pointer field with counted_by. */
3179 : 298 : tree result_type = is_fam ? c_build_pointer_type (TREE_TYPE (ref))
3180 : 144 : : TREE_TYPE (ref);
3181 : :
3182 : 298 : tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref)));
3183 : :
3184 : 298 : tree first_param = is_fam
3185 : 298 : ? c_fully_fold (array_to_pointer_conversion (loc, ref),
3186 : : false, NULL)
3187 : : : ref;
3188 : 298 : tree second_param
3189 : 298 : = c_fully_fold (counted_by_ref, false, NULL);
3190 : 298 : tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
3191 : :
3192 : 298 : tree call
3193 : 298 : = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE,
3194 : : result_type, 4,
3195 : : first_param,
3196 : : second_param,
3197 : : third_param,
3198 : : element_size);
3199 : :
3200 : : /* Wrap the call with an INDIRECT_REF with the flexible array type. */
3201 : 298 : if (is_fam)
3202 : 154 : call = build1 (INDIRECT_REF, TREE_TYPE (ref), call);
3203 : 298 : SET_EXPR_LOCATION (call, loc);
3204 : 298 : return call;
3205 : : }
3206 : :
3207 : : /* For the COMPONENT_REF ref, check whether it has a counted_by attribute,
3208 : : if so, wrap this COMPONENT_REF with the corresponding CALL to the
3209 : : function .ACCESS_WITH_SIZE.
3210 : : Otherwise, return the ref itself. */
3211 : :
3212 : : tree
3213 : 2223029 : handle_counted_by_for_component_ref (location_t loc, tree ref)
3214 : : {
3215 : 2223029 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3216 : 2223029 : tree datum = TREE_OPERAND (ref, 0);
3217 : 2223029 : tree subdatum = TREE_OPERAND (ref, 1);
3218 : 2223029 : tree counted_by_type = NULL_TREE;
3219 : :
3220 : 2223029 : if (!(c_flexible_array_member_type_p (TREE_TYPE (ref))
3221 : 2152817 : || TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE))
3222 : : return ref;
3223 : :
3224 : 492524 : tree counted_by_ref = build_counted_by_ref (datum, subdatum,
3225 : : &counted_by_type);
3226 : 492524 : if (counted_by_ref)
3227 : 298 : ref = build_access_with_size_for_counted_by (loc, ref,
3228 : : counted_by_ref,
3229 : : counted_by_type);
3230 : : return ref;
3231 : : }
3232 : :
3233 : : /* Make an expression to refer to the COMPONENT field of structure or
3234 : : union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
3235 : : location of the COMPONENT_REF. COMPONENT_LOC is the location
3236 : : of COMPONENT. ARROW_LOC is the location of the first -> operand if
3237 : : it is from -> operator.
3238 : : If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate
3239 : : a call to .ACCESS_WITH_SIZE. Otherwise, ignore the attribute. */
3240 : :
3241 : : tree
3242 : 2226292 : build_component_ref (location_t loc, tree datum, tree component,
3243 : : location_t component_loc, location_t arrow_loc)
3244 : : {
3245 : 2226292 : tree type = TREE_TYPE (datum);
3246 : 2226292 : enum tree_code code = TREE_CODE (type);
3247 : 2226292 : tree field = NULL;
3248 : 2226292 : tree ref;
3249 : 2226292 : bool datum_lvalue = lvalue_p (datum);
3250 : :
3251 : 2226292 : if (!objc_is_public (datum, component))
3252 : 0 : return error_mark_node;
3253 : :
3254 : : /* Detect Objective-C property syntax object.property. */
3255 : 2226292 : if (c_dialect_objc ()
3256 : 2226292 : && (ref = objc_maybe_build_component_ref (datum, component)))
3257 : : return ref;
3258 : :
3259 : : /* See if there is a field or component with name COMPONENT. */
3260 : :
3261 : 2226292 : if (code == RECORD_TYPE || code == UNION_TYPE)
3262 : : {
3263 : 2226220 : if (!COMPLETE_TYPE_P (type))
3264 : : {
3265 : 19 : c_incomplete_type_error (loc, NULL_TREE, type);
3266 : 19 : return error_mark_node;
3267 : : }
3268 : :
3269 : 2226201 : field = lookup_field (type, component);
3270 : :
3271 : 2226201 : if (!field)
3272 : : {
3273 : 63 : tree guessed_id = lookup_field_fuzzy (type, component);
3274 : 63 : if (guessed_id)
3275 : : {
3276 : : /* Attempt to provide a fixit replacement hint, if
3277 : : we have a valid range for the component. */
3278 : 0 : location_t reported_loc
3279 : 26 : = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
3280 : 26 : gcc_rich_location rich_loc (reported_loc);
3281 : 26 : if (component_loc != UNKNOWN_LOCATION)
3282 : 26 : rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
3283 : 26 : error_at (&rich_loc,
3284 : : "%qT has no member named %qE; did you mean %qE?",
3285 : : type, component, guessed_id);
3286 : 26 : }
3287 : : else
3288 : 37 : error_at (loc, "%qT has no member named %qE", type, component);
3289 : 63 : return error_mark_node;
3290 : : }
3291 : :
3292 : : /* Accessing elements of atomic structures or unions is undefined
3293 : : behavior (C11 6.5.2.3#5). */
3294 : 2226138 : if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
3295 : : {
3296 : 18 : if (code == RECORD_TYPE)
3297 : 12 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3298 : : "structure %qE", component, datum);
3299 : : else
3300 : 6 : warning_at (loc, 0, "accessing a member %qE of an atomic "
3301 : : "union %qE", component, datum);
3302 : : }
3303 : :
3304 : : /* Chain the COMPONENT_REFs if necessary down to the FIELD.
3305 : : This might be better solved in future the way the C++ front
3306 : : end does it - by giving the anonymous entities each a
3307 : : separate name and type, and then have build_component_ref
3308 : : recursively call itself. We can't do that here. */
3309 : 2228296 : do
3310 : : {
3311 : 2228296 : tree subdatum = TREE_VALUE (field);
3312 : 2228296 : int quals;
3313 : 2228296 : tree subtype;
3314 : 2228296 : bool use_datum_quals;
3315 : :
3316 : 2228296 : if (TREE_TYPE (subdatum) == error_mark_node)
3317 : : return error_mark_node;
3318 : :
3319 : : /* If this is an rvalue, it does not have qualifiers in C
3320 : : standard terms and we must avoid propagating such
3321 : : qualifiers down to a non-lvalue array that is then
3322 : : converted to a pointer. */
3323 : 4456592 : use_datum_quals = (datum_lvalue
3324 : 2228296 : || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
3325 : :
3326 : 2228296 : quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
3327 : 2228296 : if (use_datum_quals)
3328 : 2228034 : quals |= TYPE_QUALS (TREE_TYPE (datum));
3329 : 2228296 : subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
3330 : :
3331 : 2228296 : ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
3332 : : NULL_TREE);
3333 : 2228296 : SET_EXPR_LOCATION (ref, loc);
3334 : :
3335 : 2228296 : check_counted_by_attribute (loc, ref);
3336 : :
3337 : 2228296 : if (TREE_READONLY (subdatum)
3338 : 2228296 : || (use_datum_quals && TREE_READONLY (datum)))
3339 : 30378 : TREE_READONLY (ref) = 1;
3340 : 2228296 : if (TREE_THIS_VOLATILE (subdatum)
3341 : 2227410 : || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
3342 : 2320 : TREE_THIS_VOLATILE (ref) = 1;
3343 : :
3344 : 2228296 : if (TREE_UNAVAILABLE (subdatum))
3345 : 10 : error_unavailable_use (subdatum, NULL_TREE);
3346 : 2228286 : else if (TREE_DEPRECATED (subdatum))
3347 : 16 : warn_deprecated_use (subdatum, NULL_TREE);
3348 : :
3349 : 2228296 : datum = ref;
3350 : :
3351 : 2228296 : field = TREE_CHAIN (field);
3352 : : }
3353 : 2228296 : while (field);
3354 : :
3355 : : return ref;
3356 : : }
3357 : 72 : else if (should_suggest_deref_p (type))
3358 : : {
3359 : : /* Special-case the error message for "ptr.field" for the case
3360 : : where the user has confused "." vs "->". */
3361 : 7 : rich_location richloc (line_table, loc);
3362 : 7 : if (INDIRECT_REF_P (datum) && arrow_loc != UNKNOWN_LOCATION)
3363 : : {
3364 : 2 : richloc.add_fixit_insert_before (arrow_loc, "(*");
3365 : 2 : richloc.add_fixit_insert_after (arrow_loc, ")");
3366 : 2 : error_at (&richloc,
3367 : : "%qE is a pointer to pointer; did you mean to dereference "
3368 : : "it before applying %<->%> to it?",
3369 : 2 : TREE_OPERAND (datum, 0));
3370 : : }
3371 : : else
3372 : : {
3373 : : /* "loc" should be the "." token. */
3374 : 5 : richloc.add_fixit_replace ("->");
3375 : 5 : error_at (&richloc,
3376 : : "%qE is a pointer; did you mean to use %<->%>?",
3377 : : datum);
3378 : : }
3379 : 7 : return error_mark_node;
3380 : 7 : }
3381 : 65 : else if (code != ERROR_MARK)
3382 : 1 : error_at (loc,
3383 : : "request for member %qE in something not a structure or union",
3384 : : component);
3385 : :
3386 : 65 : return error_mark_node;
3387 : : }
3388 : :
3389 : : /* Given an expression PTR for a pointer, return an expression
3390 : : for the value pointed to.
3391 : : ERRORSTRING is the name of the operator to appear in error messages.
3392 : :
3393 : : LOC is the location to use for the generated tree. */
3394 : :
3395 : : tree
3396 : 2681593 : build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
3397 : : {
3398 : 2681593 : tree pointer = default_conversion (ptr);
3399 : 2681593 : tree type = TREE_TYPE (pointer);
3400 : 2681593 : tree ref;
3401 : :
3402 : 2681593 : if (TREE_CODE (type) == POINTER_TYPE)
3403 : : {
3404 : 2681240 : if (CONVERT_EXPR_P (pointer)
3405 : 1912562 : || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
3406 : : {
3407 : : /* If a warning is issued, mark it to avoid duplicates from
3408 : : the backend. This only needs to be done at
3409 : : warn_strict_aliasing > 2. */
3410 : 768678 : if (warn_strict_aliasing > 2)
3411 : 334841 : if (strict_aliasing_warning (EXPR_LOCATION (pointer),
3412 : 334841 : type, TREE_OPERAND (pointer, 0)))
3413 : 7 : suppress_warning (pointer, OPT_Wstrict_aliasing_);
3414 : : }
3415 : :
3416 : 2681240 : if (TREE_CODE (pointer) == ADDR_EXPR
3417 : 2681240 : && (TREE_TYPE (TREE_OPERAND (pointer, 0))
3418 : 67318 : == TREE_TYPE (type)))
3419 : : {
3420 : 67309 : ref = TREE_OPERAND (pointer, 0);
3421 : 67309 : protected_set_expr_location (ref, loc);
3422 : 67309 : return ref;
3423 : : }
3424 : : else
3425 : : {
3426 : 2613931 : tree t = TREE_TYPE (type);
3427 : :
3428 : 2613931 : ref = build1 (INDIRECT_REF, t, pointer);
3429 : :
3430 : 2613931 : if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
3431 : 151 : warning_at (loc, 0, "dereferencing %<void *%> pointer");
3432 : :
3433 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3434 : : so that we get the proper error message if the result is used
3435 : : to assign to. Also, &* is supposed to be a no-op.
3436 : : And ANSI C seems to specify that the type of the result
3437 : : should be the const type. */
3438 : : /* A de-reference of a pointer to const is not a const. It is valid
3439 : : to change it via some other pointer. */
3440 : 2613931 : TREE_READONLY (ref) = TYPE_READONLY (t);
3441 : 2613931 : TREE_SIDE_EFFECTS (ref)
3442 : 2613931 : = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
3443 : 2613931 : TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
3444 : 2613931 : protected_set_expr_location (ref, loc);
3445 : 2613931 : return ref;
3446 : : }
3447 : : }
3448 : 353 : else if (TREE_CODE (pointer) != ERROR_MARK)
3449 : 278 : invalid_indirection_error (loc, type, errstring);
3450 : :
3451 : 353 : return error_mark_node;
3452 : : }
3453 : :
3454 : : /* This handles expressions of the form "a[i]", which denotes
3455 : : an array reference.
3456 : :
3457 : : This is logically equivalent in C to *(a+i), but we may do it differently.
3458 : : If A is a variable or a member, we generate a primitive ARRAY_REF.
3459 : : This avoids forcing the array out of registers, and can work on
3460 : : arrays that are not lvalues (for example, members of structures returned
3461 : : by functions).
3462 : :
3463 : : For vector types, allow vector[i] but not i[vector], and create
3464 : : *(((type*)&vectortype) + i) for the expression.
3465 : :
3466 : : LOC is the location to use for the returned expression. */
3467 : :
3468 : : tree
3469 : 3396830 : build_array_ref (location_t loc, tree array, tree index)
3470 : : {
3471 : 3396830 : tree ret;
3472 : 3396830 : bool swapped = false;
3473 : 3396830 : if (TREE_TYPE (array) == error_mark_node
3474 : 3396830 : || TREE_TYPE (index) == error_mark_node)
3475 : : return error_mark_node;
3476 : :
3477 : 3396722 : if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
3478 : 1940397 : && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
3479 : : /* Allow vector[index] but not index[vector]. */
3480 : 4394356 : && !gnu_vector_type_p (TREE_TYPE (array)))
3481 : : {
3482 : 194 : if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
3483 : 194 : && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
3484 : : {
3485 : 2 : error_at (loc,
3486 : : "subscripted value is neither array nor pointer nor vector");
3487 : :
3488 : 2 : return error_mark_node;
3489 : : }
3490 : 192 : std::swap (array, index);
3491 : 192 : swapped = true;
3492 : : }
3493 : :
3494 : 3396720 : if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
3495 : : {
3496 : 2 : error_at (loc, "array subscript is not an integer");
3497 : 2 : return error_mark_node;
3498 : : }
3499 : :
3500 : 3396718 : if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
3501 : : {
3502 : 10 : error_at (loc, "subscripted value is pointer to function");
3503 : 10 : return error_mark_node;
3504 : : }
3505 : :
3506 : : /* ??? Existing practice has been to warn only when the char
3507 : : index is syntactically the index, not for char[array]. */
3508 : 3396708 : if (!swapped)
3509 : 3396522 : warn_array_subscript_with_type_char (loc, index);
3510 : :
3511 : : /* Apply default promotions *after* noticing character types. */
3512 : 3396708 : index = default_conversion (index);
3513 : 3396708 : if (index == error_mark_node)
3514 : : return error_mark_node;
3515 : :
3516 : 3396707 : gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
3517 : : || TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE);
3518 : :
3519 : 3396707 : bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
3520 : 3396707 : bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
3521 : :
3522 : : /* We only generate a call to .ACCESS_WITH_SIZE when it is a read. */
3523 : 3396707 : if (TREE_CODE (array) == COMPONENT_REF
3524 : 3396707 : && handle_counted_by_p (array))
3525 : 800841 : array = handle_counted_by_for_component_ref (loc, array);
3526 : :
3527 : 3396707 : if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3528 : : {
3529 : 2453772 : tree rval, type;
3530 : :
3531 : : /* An array that is indexed by a non-constant
3532 : : cannot be stored in a register; we must be able to do
3533 : : address arithmetic on its address.
3534 : : Likewise an array of elements of variable size. */
3535 : 2453772 : if (TREE_CODE (index) != INTEGER_CST
3536 : 2453772 : || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3537 : 1990910 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
3538 : : {
3539 : 463599 : if (!c_mark_addressable (array, true, true))
3540 : 1 : return error_mark_node;
3541 : : }
3542 : : /* An array that is indexed by a constant value which is not within
3543 : : the array bounds cannot be stored in a register either; because we
3544 : : would get a crash in store_bit_field/extract_bit_field when trying
3545 : : to access a non-existent part of the register. */
3546 : 2453771 : if (TREE_CODE (index) == INTEGER_CST
3547 : 1990910 : && TYPE_DOMAIN (TREE_TYPE (array))
3548 : 4441325 : && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
3549 : : {
3550 : 60638 : if (!c_mark_addressable (array, false, true))
3551 : 0 : return error_mark_node;
3552 : : /* ISO C2Y disallows a negative integer constant expression index
3553 : : when array subscripting has an operand of array type. */
3554 : 60638 : if (flag_isoc2y
3555 : 26 : && !TREE_OVERFLOW (index)
3556 : 60661 : && tree_int_cst_sgn (index) < 0)
3557 : 10 : error_at (loc, "array subscript is negative");
3558 : : }
3559 : :
3560 : 2453771 : if ((pedantic || warn_c90_c99_compat || warn_c23_c2y_compat)
3561 : 2430915 : && ! was_vector)
3562 : : {
3563 : 1455619 : tree foo = array;
3564 : 2275647 : while (TREE_CODE (foo) == COMPONENT_REF)
3565 : 820028 : foo = TREE_OPERAND (foo, 0);
3566 : 969518 : if ((VAR_P (foo) && C_DECL_REGISTER (foo))
3567 : 2425071 : || (TREE_CODE (foo) == COMPOUND_LITERAL_EXPR
3568 : 72 : && C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (foo))))
3569 : 78 : pedwarn_c23 (loc, OPT_Wpedantic,
3570 : : "ISO C forbids subscripting %<register%> array "
3571 : : "before C2Y");
3572 : 1455541 : else if (!lvalue_p (foo))
3573 : 118 : pedwarn_c90 (loc, OPT_Wpedantic,
3574 : : "ISO C90 forbids subscripting non-lvalue "
3575 : : "array");
3576 : : }
3577 : :
3578 : 2453771 : if (TREE_CODE (TREE_TYPE (index)) == BITINT_TYPE
3579 : 2453771 : && TYPE_PRECISION (TREE_TYPE (index)) > TYPE_PRECISION (sizetype))
3580 : 3 : index = fold_convert (sizetype, index);
3581 : :
3582 : 2453771 : type = TREE_TYPE (TREE_TYPE (array));
3583 : 2453771 : rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
3584 : : /* Array ref is const/volatile if the array elements are
3585 : : or if the array is. */
3586 : 7361313 : TREE_READONLY (rval)
3587 : 2453771 : |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
3588 : 2453771 : | TREE_READONLY (array));
3589 : 7361313 : TREE_SIDE_EFFECTS (rval)
3590 : 2453771 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3591 : 2453771 : | TREE_SIDE_EFFECTS (array));
3592 : 4907542 : TREE_THIS_VOLATILE (rval)
3593 : 2453771 : |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
3594 : : /* This was added by rms on 16 Nov 91.
3595 : : It fixes vol struct foo *a; a->elts[1]
3596 : : in an inline function.
3597 : : Hope it doesn't break something else. */
3598 : 2453771 : | TREE_THIS_VOLATILE (array));
3599 : 2453771 : ret = require_complete_type (loc, rval);
3600 : 2453771 : protected_set_expr_location (ret, loc);
3601 : 2453771 : if (non_lvalue)
3602 : 102853 : ret = non_lvalue_loc (loc, ret);
3603 : 2453771 : return ret;
3604 : : }
3605 : : else
3606 : : {
3607 : 942935 : tree ar = default_conversion (array);
3608 : :
3609 : 942935 : if (ar == error_mark_node)
3610 : : return ar;
3611 : :
3612 : 942935 : gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
3613 : 942935 : gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
3614 : :
3615 : 942935 : ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
3616 : : index, false),
3617 : : RO_ARRAY_INDEXING);
3618 : 942935 : if (non_lvalue)
3619 : 0 : ret = non_lvalue_loc (loc, ret);
3620 : 942935 : return ret;
3621 : : }
3622 : : }
3623 : :
3624 : : /* Build an OpenMP array section reference, creating an exact type for the
3625 : : resulting expression based on the element type and bounds if possible. If
3626 : : we have variable bounds, create an incomplete array type for the result
3627 : : instead. */
3628 : :
3629 : : tree
3630 : 3933 : build_omp_array_section (location_t loc, tree array, tree index, tree length)
3631 : : {
3632 : 3933 : tree type = TREE_TYPE (array);
3633 : 3933 : gcc_assert (type);
3634 : :
3635 : 3933 : tree sectype, eltype = TREE_TYPE (type);
3636 : :
3637 : : /* It's not an array or pointer type. Just reuse the type of the original
3638 : : expression as the type of the array section (an error will be raised
3639 : : anyway, later). */
3640 : 3933 : if (eltype == NULL_TREE || error_operand_p (eltype))
3641 : 12 : sectype = TREE_TYPE (array);
3642 : : else
3643 : : {
3644 : 3921 : tree idxtype = NULL_TREE;
3645 : :
3646 : 3921 : if (index != NULL_TREE
3647 : 3921 : && length != NULL_TREE
3648 : 2996 : && INTEGRAL_TYPE_P (TREE_TYPE (index))
3649 : 6912 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3650 : : {
3651 : 2984 : tree low = fold_convert (sizetype, index);
3652 : 2984 : tree high = fold_convert (sizetype, length);
3653 : 2984 : high = size_binop (PLUS_EXPR, low, high);
3654 : 2984 : high = size_binop (MINUS_EXPR, high, size_one_node);
3655 : 2984 : idxtype = build_range_type (sizetype, low, high);
3656 : : }
3657 : 135 : else if ((index == NULL_TREE || integer_zerop (index))
3658 : 827 : && length != NULL_TREE
3659 : 1612 : && INTEGRAL_TYPE_P (TREE_TYPE (length)))
3660 : 658 : idxtype = build_index_type (length);
3661 : :
3662 : 3921 : gcc_assert (!error_operand_p (idxtype));
3663 : :
3664 : 3921 : sectype = c_build_array_type (eltype, idxtype);
3665 : : }
3666 : :
3667 : 3933 : return build3_loc (loc, OMP_ARRAY_SECTION, sectype, array, index, length);
3668 : : }
3669 : :
3670 : :
3671 : : /* Record that REF is used. This is relevant for undeclared static
3672 : : function and declarations referenced from a non-local context.
3673 : : ADDRESS indicates whether the address is taken. */
3674 : :
3675 : : void
3676 : 460782652 : mark_decl_used (tree ref, bool address)
3677 : : {
3678 : 460782652 : if (!ref || !DECL_P (ref) || in_alignof)
3679 : : return;
3680 : :
3681 : : /* Non-file-scope and non-local reference in nested function. */
3682 : 339141499 : bool nonloc_p = current_function_decl && DECL_CONTEXT (ref)
3683 : 118942332 : && DECL_CONTEXT (current_function_decl)
3684 : 460748867 : && DECL_CONTEXT (ref) != current_function_decl;
3685 : :
3686 : : /* An undeclared static function. */
3687 : 460738884 : bool static_p = TREE_CODE (ref) == FUNCTION_DECL
3688 : 99332362 : && DECL_INITIAL (ref) == NULL_TREE
3689 : 67968505 : && DECL_EXTERNAL (ref)
3690 : 528704863 : && !TREE_PUBLIC (ref);
3691 : :
3692 : 460605748 : if (!static_p && !nonloc_p)
3693 : : return;
3694 : :
3695 : : /* If we may be in an unevaluated context, delay the decision. */
3696 : 135728 : if (in_sizeof || in_typeof || in_countof || in_generic)
3697 : 270 : return record_maybe_used_decl (ref, address);
3698 : :
3699 : 135458 : if (static_p)
3700 : 132880 : C_DECL_USED (ref) = 1;
3701 : :
3702 : 135458 : if (nonloc_p && (VAR_OR_FUNCTION_DECL_P (ref)
3703 : : || TREE_CODE (ref) == PARM_DECL))
3704 : 2036 : DECL_NONLOCAL (ref) = 1;
3705 : :
3706 : : /* Nothing to do anymore. */
3707 : 2578 : if (!nonloc_p || C_FUNC_NONLOCAL_CONTEXT (current_function_decl))
3708 : 134279 : return;
3709 : :
3710 : : /* Filter out the cases where referencing a non-local variable does not
3711 : : require a non-local context passed via the static chain. */
3712 : 1179 : if (!C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (ref)))
3713 : 1123 : switch (TREE_CODE (ref))
3714 : : {
3715 : 215 : case FUNCTION_DECL:
3716 : : /* Use of another local function that requires no context is ok. */
3717 : 215 : if (!C_FUNC_NONLOCAL_CONTEXT (ref) && DECL_INITIAL (ref))
3718 : : return;
3719 : : break;
3720 : 435 : case VAR_DECL:
3721 : : /* Static variables and constexpr are ok, but for the later only
3722 : : when the address is not taken. */
3723 : 435 : if (TREE_STATIC (ref) || (C_DECL_DECLARED_CONSTEXPR (ref) && !address))
3724 : : return;
3725 : : break;
3726 : : case TYPE_DECL:
3727 : : /* A typedef is ok when not for a variably-modified type. */
3728 : : return;
3729 : : case CONST_DECL:
3730 : : /* An enumeration constant is ok. */
3731 : : return;
3732 : : case PARM_DECL:
3733 : : break;
3734 : : case LABEL_DECL:
3735 : : break;
3736 : 0 : default:
3737 : 0 : gcc_unreachable ();
3738 : : }
3739 : :
3740 : : /* Mark all parent functions up to the nesting level of the variable as
3741 : : as needing the non-local context. */
3742 : 1963 : for (tree cont = current_function_decl; cont; cont = DECL_CONTEXT (cont))
3743 : : {
3744 : 1963 : if (cont == DECL_CONTEXT (ref))
3745 : : break;
3746 : :
3747 : : /* There should not be any other type of context used for function
3748 : : except TRANSLATION_UNIT_DECL which we should be able to reach. */
3749 : 987 : gcc_checking_assert (TREE_CODE (cont) == FUNCTION_DECL);
3750 : :
3751 : 987 : if (TREE_CODE (cont) == FUNCTION_DECL)
3752 : 987 : C_FUNC_NONLOCAL_CONTEXT (cont) = 1;
3753 : : }
3754 : : }
3755 : :
3756 : :
3757 : : /* Build an external reference to identifier ID. FUN indicates
3758 : : whether this will be used for a function call. LOC is the source
3759 : : location of the identifier. This sets *TYPE to the type of the
3760 : : identifier, which is not the same as the type of the returned value
3761 : : for CONST_DECLs defined as enum constants. If the type of the
3762 : : identifier is not available, *TYPE is set to NULL. */
3763 : : tree
3764 : 170529052 : build_external_ref (location_t loc, tree id, bool fun, tree *type)
3765 : : {
3766 : 170529052 : tree ref;
3767 : 170529052 : tree decl = lookup_name (id);
3768 : :
3769 : : /* In Objective-C, an instance variable (ivar) may be preferred to
3770 : : whatever lookup_name() found. */
3771 : 170529052 : decl = objc_lookup_ivar (decl, id);
3772 : :
3773 : 170529052 : *type = NULL;
3774 : 170529052 : if (decl && decl != error_mark_node)
3775 : : {
3776 : 170522678 : ref = decl;
3777 : 170522678 : *type = TREE_TYPE (ref);
3778 : 170522678 : if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
3779 : 4 : error_at (loc, "underspecified %qD referenced in its initializer",
3780 : : decl);
3781 : : }
3782 : 6374 : else if (fun)
3783 : : /* Implicit function declaration. */
3784 : 4729 : ref = implicitly_declare (loc, id);
3785 : 1645 : else if (decl == error_mark_node)
3786 : : /* Don't complain about something that's already been
3787 : : complained about. */
3788 : : return error_mark_node;
3789 : : else
3790 : : {
3791 : 1415 : undeclared_variable (loc, id);
3792 : 1415 : return error_mark_node;
3793 : : }
3794 : :
3795 : : /* For an OpenMP map clause, we can get better diagnostics for decls with
3796 : : unmappable types if we return the decl with an error_mark_node type,
3797 : : rather than returning error_mark_node for the decl itself. */
3798 : 170527407 : if (TREE_TYPE (ref) == error_mark_node
3799 : 170527407 : && !c_omp_array_section_p)
3800 : : return error_mark_node;
3801 : :
3802 : 170527390 : if (TREE_UNAVAILABLE (ref))
3803 : 14 : error_unavailable_use (ref, NULL_TREE);
3804 : 170527376 : else if (TREE_DEPRECATED (ref))
3805 : 49 : warn_deprecated_use (ref, NULL_TREE);
3806 : :
3807 : : /* Recursive call does not count as usage. */
3808 : 170527390 : if (ref != current_function_decl)
3809 : : {
3810 : 170524774 : TREE_USED (ref) = 1;
3811 : : }
3812 : :
3813 : 170527390 : mark_decl_used (ref, false);
3814 : :
3815 : 170527390 : if (TREE_CODE (ref) == CONST_DECL)
3816 : : {
3817 : 411133 : used_types_insert (TREE_TYPE (ref));
3818 : :
3819 : 411133 : if (warn_cxx_compat
3820 : 377 : && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
3821 : 411508 : && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
3822 : : {
3823 : 1 : warning_at (loc, OPT_Wc___compat,
3824 : : "enum constant defined in struct or union "
3825 : : "is not visible in C++");
3826 : 1 : inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
3827 : : }
3828 : :
3829 : 411133 : ref = DECL_INITIAL (ref);
3830 : 411133 : TREE_CONSTANT (ref) = 1;
3831 : : }
3832 : : /* C99 6.7.4p3: An inline definition of a function with external
3833 : : linkage ... shall not contain a reference to an identifier with
3834 : : internal linkage. */
3835 : 170116257 : else if (current_function_decl != NULL_TREE
3836 : 168620466 : && DECL_DECLARED_INLINE_P (current_function_decl)
3837 : 155203481 : && DECL_EXTERNAL (current_function_decl)
3838 : 154443291 : && VAR_OR_FUNCTION_DECL_P (ref)
3839 : 55030463 : && (!VAR_P (ref) || TREE_STATIC (ref))
3840 : 46107315 : && ! TREE_PUBLIC (ref)
3841 : 170116291 : && DECL_CONTEXT (ref) != current_function_decl)
3842 : 25 : record_inline_static (loc, current_function_decl, ref,
3843 : : csi_internal);
3844 : :
3845 : : return ref;
3846 : : }
3847 : :
3848 : : static struct maybe_used_decl *maybe_used_decls;
3849 : :
3850 : : /* Record that DECL, a reference seen inside sizeof or typeof or _Countof or
3851 : : _Generic, might be used if the operand of sizeof is a VLA type or the
3852 : : operand of typeof is a variably modified type or the operand of _Countof has
3853 : : a variable number of elements or the operand of _Generic is the one selected
3854 : : as the result. */
3855 : :
3856 : : static void
3857 : 270 : record_maybe_used_decl (tree decl, bool address)
3858 : : {
3859 : 270 : struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3860 : 270 : t->decl = decl;
3861 : 270 : t->level = in_sizeof + in_typeof + in_countof + in_generic;
3862 : 270 : t->address = address;
3863 : 270 : t->next = maybe_used_decls;
3864 : 270 : maybe_used_decls = t;
3865 : 270 : }
3866 : :
3867 : : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3868 : : _Generic. If USED is false, just discard them. If it is true, mark them
3869 : : used (if no longer inside sizeof or typeof or _Countof or _Generic) or move
3870 : : them to the next level up (if still inside sizeof or typeof or _Countof or
3871 : : _Generic). */
3872 : :
3873 : : void
3874 : 1441340 : pop_maybe_used (bool used)
3875 : : {
3876 : 1441340 : struct maybe_used_decl *p = maybe_used_decls;
3877 : 1441340 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3878 : 1441634 : while (p && p->level > cur_level)
3879 : : {
3880 : 294 : if (used)
3881 : : {
3882 : 74 : if (cur_level == 0)
3883 : 50 : mark_decl_used (p->decl, p->address);
3884 : : else
3885 : 24 : p->level = cur_level;
3886 : : }
3887 : 294 : p = p->next;
3888 : : }
3889 : 1441340 : if (!used || cur_level == 0)
3890 : 1441297 : maybe_used_decls = p;
3891 : 1441340 : }
3892 : :
3893 : : /* Pop the stack of decls possibly used inside sizeof or typeof or _Countof or
3894 : : _Generic, without acting on them, and return the pointer to the previous top
3895 : : of the stack. This for use at the end of a default generic association when
3896 : : it is not yet known whether the expression is used. If it later turns out
3897 : : the expression is used (or treated as used before C23), restore_maybe_used
3898 : : should be called on the return value followed by pop_maybe_used (true);
3899 : : otherwise, the return value can be discarded. */
3900 : :
3901 : : struct maybe_used_decl *
3902 : 492 : save_maybe_used ()
3903 : : {
3904 : 492 : struct maybe_used_decl *p = maybe_used_decls, *orig = p;
3905 : 492 : int cur_level = in_sizeof + in_typeof + in_countof + in_generic;
3906 : 524 : while (p && p->level > cur_level)
3907 : 32 : p = p->next;
3908 : 492 : maybe_used_decls = p;
3909 : 492 : return orig;
3910 : : }
3911 : :
3912 : : /* Restore the stack of decls possibly used inside sizeof or typeof or _Countof
3913 : : or _Generic returned by save_maybe_used. It is required that the stack is
3914 : : at exactly the point where it was left by save_maybe_used. */
3915 : :
3916 : : void
3917 : 57 : restore_maybe_used (struct maybe_used_decl *stack)
3918 : : {
3919 : 57 : maybe_used_decls = stack;
3920 : 57 : }
3921 : :
3922 : : /* Return the result of sizeof applied to EXPR. */
3923 : :
3924 : : struct c_expr
3925 : 302434 : c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3926 : : {
3927 : 302434 : struct c_expr ret;
3928 : 302434 : if (expr.value == error_mark_node)
3929 : : {
3930 : 113 : ret.value = error_mark_node;
3931 : 113 : ret.original_code = ERROR_MARK;
3932 : 113 : ret.original_type = NULL;
3933 : 113 : ret.m_decimal = 0;
3934 : 113 : pop_maybe_used (false);
3935 : : }
3936 : : else
3937 : : {
3938 : 302321 : bool expr_const_operands = true;
3939 : :
3940 : 302321 : if (TREE_CODE (expr.value) == PARM_DECL
3941 : 302321 : && C_ARRAY_PARAMETER (expr.value))
3942 : : {
3943 : 55 : auto_diagnostic_group d;
3944 : 55 : if (warning_at (loc, OPT_Wsizeof_array_argument,
3945 : : "%<sizeof%> on array function parameter %qE will "
3946 : : "return size of %qT", expr.value,
3947 : 55 : TREE_TYPE (expr.value)))
3948 : 19 : inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3949 : 55 : }
3950 : 302321 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3951 : : &expr_const_operands);
3952 : 302321 : ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3953 : 302321 : c_last_sizeof_arg = expr.value;
3954 : 302321 : c_last_sizeof_loc = loc;
3955 : 302321 : ret.original_code = SIZEOF_EXPR;
3956 : 302321 : ret.original_type = NULL;
3957 : 302321 : ret.m_decimal = 0;
3958 : 302321 : if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3959 : : {
3960 : : /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3961 : 287 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3962 : : folded_expr, ret.value);
3963 : 287 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3964 : 287 : SET_EXPR_LOCATION (ret.value, loc);
3965 : : }
3966 : 302321 : pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3967 : : }
3968 : 302434 : return ret;
3969 : : }
3970 : :
3971 : : /* Return the result of sizeof applied to T, a structure for the type
3972 : : name passed to sizeof (rather than the type itself). LOC is the
3973 : : location of the original expression. */
3974 : :
3975 : : struct c_expr
3976 : 330310 : c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3977 : : {
3978 : 330310 : tree type;
3979 : 330310 : struct c_expr ret;
3980 : 330310 : tree type_expr = NULL_TREE;
3981 : 330310 : bool type_expr_const = true;
3982 : 330310 : type = groktypename (t, &type_expr, &type_expr_const);
3983 : 330310 : ret.value = c_sizeof (loc, type);
3984 : 330310 : c_last_sizeof_arg = type;
3985 : 330310 : c_last_sizeof_loc = loc;
3986 : 330310 : ret.original_code = SIZEOF_EXPR;
3987 : 330310 : ret.original_type = NULL;
3988 : 330310 : ret.m_decimal = 0;
3989 : 330310 : if (type == error_mark_node)
3990 : : {
3991 : 5 : ret.value = error_mark_node;
3992 : 5 : ret.original_code = ERROR_MARK;
3993 : : }
3994 : : else
3995 : 330263 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3996 : 660484 : && C_TYPE_VARIABLE_SIZE (type))
3997 : : {
3998 : : /* If the type is a [*] array, it is a VLA but is represented as
3999 : : having a size of zero. In such a case we must ensure that
4000 : : the result of sizeof does not get folded to a constant by
4001 : : c_fully_fold, because if the size is evaluated the result is
4002 : : not constant and so constraints on zero or negative size
4003 : : arrays must not be applied when this sizeof call is inside
4004 : : another array declarator. */
4005 : 41 : if (!type_expr)
4006 : 16 : type_expr = integer_zero_node;
4007 : 41 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4008 : : type_expr, ret.value);
4009 : 41 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4010 : : }
4011 : 330310 : pop_maybe_used (type != error_mark_node
4012 : 330310 : ? C_TYPE_VARIABLE_SIZE (type) : false);
4013 : 330310 : return ret;
4014 : : }
4015 : :
4016 : : static bool
4017 : 136 : is_top_array_vla (tree type)
4018 : : {
4019 : 136 : bool zero, var;
4020 : 136 : tree d;
4021 : :
4022 : 136 : if (TREE_CODE (type) != ARRAY_TYPE)
4023 : : return false;
4024 : 118 : if (!COMPLETE_TYPE_P (type))
4025 : : return false;
4026 : :
4027 : 114 : d = TYPE_DOMAIN (type);
4028 : 114 : zero = !TYPE_MAX_VALUE (d);
4029 : 114 : if (zero)
4030 : : return false;
4031 : :
4032 : 98 : var = (TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST
4033 : 98 : || TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST);
4034 : : return var;
4035 : : }
4036 : :
4037 : : /* Return the result of countof applied to EXPR. */
4038 : :
4039 : : struct c_expr
4040 : 53 : c_expr_countof_expr (location_t loc, struct c_expr expr)
4041 : : {
4042 : 53 : struct c_expr ret;
4043 : 53 : if (expr.value == error_mark_node)
4044 : : {
4045 : 1 : ret.value = error_mark_node;
4046 : 1 : ret.original_code = ERROR_MARK;
4047 : 1 : ret.original_type = NULL;
4048 : 1 : ret.m_decimal = 0;
4049 : 1 : pop_maybe_used (false);
4050 : : }
4051 : : else
4052 : : {
4053 : 52 : bool expr_const_operands = true;
4054 : :
4055 : 52 : tree folded_expr = c_fully_fold (expr.value, require_constant_value,
4056 : : &expr_const_operands);
4057 : 52 : ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
4058 : 52 : c_last_sizeof_arg = expr.value;
4059 : 52 : c_last_sizeof_loc = loc;
4060 : 52 : ret.original_code = COUNTOF_EXPR;
4061 : 52 : ret.original_type = NULL;
4062 : 52 : ret.m_decimal = 0;
4063 : 52 : if (is_top_array_vla (TREE_TYPE (folded_expr)))
4064 : : {
4065 : : /* countof is evaluated when given a vla. */
4066 : 11 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4067 : : folded_expr, ret.value);
4068 : 11 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
4069 : 11 : SET_EXPR_LOCATION (ret.value, loc);
4070 : : }
4071 : 52 : pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
4072 : : }
4073 : 53 : return ret;
4074 : : }
4075 : :
4076 : : /* Return the result of countof applied to T, a structure for the type
4077 : : name passed to countof (rather than the type itself). LOC is the
4078 : : location of the original expression. */
4079 : :
4080 : : struct c_expr
4081 : 18 : c_expr_countof_type (location_t loc, struct c_type_name *t)
4082 : : {
4083 : 18 : tree type;
4084 : 18 : struct c_expr ret;
4085 : 18 : tree type_expr = NULL_TREE;
4086 : 18 : bool type_expr_const = true;
4087 : 18 : type = groktypename (t, &type_expr, &type_expr_const);
4088 : 18 : ret.value = c_countof_type (loc, type);
4089 : 18 : c_last_sizeof_arg = type;
4090 : 18 : c_last_sizeof_loc = loc;
4091 : 18 : ret.original_code = COUNTOF_EXPR;
4092 : 18 : ret.original_type = NULL;
4093 : 18 : ret.m_decimal = 0;
4094 : 18 : if (type == error_mark_node)
4095 : : {
4096 : 0 : ret.value = error_mark_node;
4097 : 0 : ret.original_code = ERROR_MARK;
4098 : : }
4099 : : else
4100 : 8 : if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
4101 : 22 : && is_top_array_vla (type))
4102 : : {
4103 : : /* If the type is a [*] array, it is a VLA but is represented as
4104 : : having a size of zero. In such a case we must ensure that
4105 : : the result of countof does not get folded to a constant by
4106 : : c_fully_fold, because if the number of elements is evaluated
4107 : : the result is not constant and so
4108 : : constraints on zero or negative size arrays must not be applied
4109 : : when this countof call is inside another array declarator. */
4110 : 6 : if (!type_expr)
4111 : 0 : type_expr = integer_zero_node;
4112 : 6 : ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
4113 : : type_expr, ret.value);
4114 : 6 : C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
4115 : : }
4116 : 18 : pop_maybe_used (type != error_mark_node ? is_top_array_vla (type) : false);
4117 : 18 : return ret;
4118 : : }
4119 : :
4120 : : /* Build a function call to function FUNCTION with parameters PARAMS.
4121 : : The function call is at LOC.
4122 : : PARAMS is a list--a chain of TREE_LIST nodes--in which the
4123 : : TREE_VALUE of each node is a parameter-expression.
4124 : : FUNCTION's data type may be a function type or a pointer-to-function. */
4125 : :
4126 : : tree
4127 : 0 : build_function_call (location_t loc, tree function, tree params)
4128 : : {
4129 : 0 : vec<tree, va_gc> *v;
4130 : 0 : tree ret;
4131 : :
4132 : 0 : vec_alloc (v, list_length (params));
4133 : 0 : for (; params; params = TREE_CHAIN (params))
4134 : 0 : v->quick_push (TREE_VALUE (params));
4135 : 0 : ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
4136 : 0 : vec_free (v);
4137 : 0 : return ret;
4138 : : }
4139 : :
4140 : : /* Give a note about the location of the declaration of DECL,
4141 : : or, failing that, a pertinent declaration for FUNCTION_EXPR. */
4142 : :
4143 : : static void
4144 : 299 : inform_declaration (tree decl, tree function_expr)
4145 : : {
4146 : 299 : if (decl && (TREE_CODE (decl) != FUNCTION_DECL
4147 : 278 : || !DECL_IS_UNDECLARED_BUILTIN (decl)))
4148 : 207 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
4149 : 92 : else if (function_expr)
4150 : 92 : switch (TREE_CODE (function_expr))
4151 : : {
4152 : : default:
4153 : : break;
4154 : 13 : case COMPONENT_REF:
4155 : : /* Show the decl of the pertinent field (e.g. for callback
4156 : : fields in a struct. */
4157 : 13 : {
4158 : 13 : tree field_decl = TREE_OPERAND (function_expr, 1);
4159 : 13 : if (location_t loc = DECL_SOURCE_LOCATION (field_decl))
4160 : 13 : inform (loc, "declared here");
4161 : : }
4162 : : break;
4163 : : }
4164 : 299 : }
4165 : :
4166 : : /* C implementation of callback for use when checking param types. */
4167 : :
4168 : : static bool
4169 : 2 : comp_parm_types (tree wanted_type, tree actual_type)
4170 : : {
4171 : 2 : return comptypes (wanted_type, actual_type);
4172 : : }
4173 : :
4174 : : /* Build a function call to function FUNCTION with parameters PARAMS.
4175 : : If FUNCTION is the result of resolving an overloaded target built-in,
4176 : : ORIG_FUNDECL is the original function decl, otherwise it is null.
4177 : : ORIGTYPES, if not NULL, is a vector of types; each element is
4178 : : either NULL or the original type of the corresponding element in
4179 : : PARAMS. The original type may differ from TREE_TYPE of the
4180 : : parameter for enums. FUNCTION's data type may be a function type
4181 : : or pointer-to-function. This function changes the elements of
4182 : : PARAMS. */
4183 : :
4184 : : tree
4185 : 48614739 : build_function_call_vec (location_t loc, vec<location_t> arg_loc,
4186 : : tree function, vec<tree, va_gc> *params,
4187 : : vec<tree, va_gc> *origtypes, tree orig_fundecl)
4188 : : {
4189 : 48614739 : tree fntype, fundecl = NULL_TREE;
4190 : 48614739 : tree name = NULL_TREE, result;
4191 : 48614739 : tree tem;
4192 : 48614739 : int nargs;
4193 : 48614739 : tree *argarray;
4194 : :
4195 : :
4196 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4197 : 48614739 : STRIP_TYPE_NOPS (function);
4198 : :
4199 : : /* Convert anything with function type to a pointer-to-function. */
4200 : 48614739 : if (TREE_CODE (function) == FUNCTION_DECL)
4201 : : {
4202 : 48537656 : name = DECL_NAME (function);
4203 : :
4204 : 48537656 : if (flag_tm)
4205 : 421 : tm_malloc_replacement (function);
4206 : 48537656 : fundecl = function;
4207 : 48537656 : if (!orig_fundecl)
4208 : 48537656 : orig_fundecl = fundecl;
4209 : : /* Atomic functions have type checking/casting already done. They are
4210 : : often rewritten and don't match the original parameter list. */
4211 : 97075312 : if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
4212 : : origtypes = NULL;
4213 : : }
4214 : 48614739 : if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
4215 : 48551922 : function = function_to_pointer_conversion (loc, function);
4216 : :
4217 : : /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4218 : : expressions, like those used for ObjC messenger dispatches. */
4219 : 48614739 : if (params && !params->is_empty ())
4220 : 37209462 : function = objc_rewrite_function_call (function, (*params)[0]);
4221 : :
4222 : 48614739 : function = c_fully_fold (function, false, NULL);
4223 : :
4224 : 48614739 : fntype = TREE_TYPE (function);
4225 : :
4226 : 48614739 : if (TREE_CODE (fntype) == ERROR_MARK)
4227 : 3 : return error_mark_node;
4228 : :
4229 : 48614736 : if (!(TREE_CODE (fntype) == POINTER_TYPE
4230 : 48614702 : && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
4231 : : {
4232 : 37 : if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
4233 : 33 : error_at (loc,
4234 : : "called object %qE is not a function or function pointer",
4235 : : function);
4236 : 4 : else if (DECL_P (function))
4237 : : {
4238 : 1 : auto_diagnostic_group d;
4239 : 1 : error_at (loc,
4240 : : "called object %qD is not a function or function pointer",
4241 : : function);
4242 : 1 : inform_declaration (function, NULL_TREE);
4243 : 1 : }
4244 : : else
4245 : 3 : error_at (loc,
4246 : : "called object is not a function or function pointer");
4247 : 37 : return error_mark_node;
4248 : : }
4249 : :
4250 : 48614699 : if (fundecl && TREE_THIS_VOLATILE (fundecl))
4251 : 372051 : current_function_returns_abnormally = 1;
4252 : :
4253 : : /* fntype now gets the type of function pointed to. */
4254 : 48614699 : fntype = TREE_TYPE (fntype);
4255 : 48614699 : tree return_type = TREE_TYPE (fntype);
4256 : :
4257 : : /* Convert the parameters to the types declared in the
4258 : : function prototype, or apply default promotions. */
4259 : :
4260 : 48614699 : nargs = convert_arguments (loc, arg_loc, fntype, params,
4261 : : origtypes, function, fundecl);
4262 : 48614699 : if (nargs < 0)
4263 : 178 : return error_mark_node;
4264 : :
4265 : : /* Check that the function is called through a compatible prototype.
4266 : : If it is not, warn. */
4267 : 48613658 : if (CONVERT_EXPR_P (function)
4268 : 879 : && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
4269 : 215 : && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
4270 : 48614706 : && !comptypes (fntype, TREE_TYPE (tem)))
4271 : : {
4272 : : /* This situation leads to run-time undefined behavior. We can't,
4273 : : therefore, simply error unless we can prove that all possible
4274 : : executions of the program must execute the code. */
4275 : 20 : warning_at (loc, 0, "function called through a non-compatible type");
4276 : :
4277 : 20 : if (VOID_TYPE_P (return_type)
4278 : 20 : && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
4279 : 1 : pedwarn (loc, 0,
4280 : : "function with qualified void return type called");
4281 : : }
4282 : :
4283 : 48614521 : argarray = vec_safe_address (params);
4284 : :
4285 : : /* Check that arguments to builtin functions match the expectations. */
4286 : 48614521 : if (fundecl
4287 : 48537493 : && fndecl_built_in_p (fundecl)
4288 : 81003761 : && !check_builtin_function_arguments (loc, arg_loc, fundecl,
4289 : : orig_fundecl, nargs, argarray))
4290 : 298 : return error_mark_node;
4291 : :
4292 : : /* Check that the arguments to the function are valid. */
4293 : 48614223 : bool warned_p = check_function_arguments (loc, fundecl, fntype,
4294 : : nargs, argarray, &arg_loc,
4295 : : comp_parm_types);
4296 : :
4297 : 48614223 : if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
4298 : 48614223 : && !VOID_TYPE_P (return_type))
4299 : 11 : return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
4300 : 48614223 : if (name != NULL_TREE
4301 : 97151418 : && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
4302 : : {
4303 : 31672679 : if (require_constant_value)
4304 : 3488 : result
4305 : 3488 : = fold_build_call_array_initializer_loc (loc, return_type,
4306 : : function, nargs, argarray);
4307 : : else
4308 : 31669191 : result = fold_build_call_array_loc (loc, return_type,
4309 : : function, nargs, argarray);
4310 : 31672679 : if (TREE_CODE (result) == NOP_EXPR
4311 : 31672679 : && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
4312 : 26530 : STRIP_TYPE_NOPS (result);
4313 : : }
4314 : : else
4315 : 16941544 : result = build_call_array_loc (loc, return_type,
4316 : : function, nargs, argarray);
4317 : : /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
4318 : : later. */
4319 : 48614223 : if (warned_p && TREE_CODE (result) == CALL_EXPR)
4320 : 215 : suppress_warning (result, OPT_Wnonnull);
4321 : :
4322 : : /* In this improbable scenario, a nested function returns a VM type.
4323 : : Create a TARGET_EXPR so that the call always has a LHS, much as
4324 : : what the C++ FE does for functions returning non-PODs. */
4325 : 48614223 : if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype)))
4326 : : {
4327 : 81 : tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
4328 : 81 : result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
4329 : : NULL_TREE, NULL_TREE);
4330 : : }
4331 : :
4332 : 48614223 : if (VOID_TYPE_P (TREE_TYPE (result)))
4333 : : {
4334 : 2349300 : if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
4335 : 2 : pedwarn (loc, 0,
4336 : : "function with qualified void return type called");
4337 : 2349300 : return result;
4338 : : }
4339 : 46264923 : return require_complete_type (loc, result);
4340 : : }
4341 : :
4342 : : /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
4343 : :
4344 : : tree
4345 : 48614296 : c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
4346 : : tree function, vec<tree, va_gc> *params,
4347 : : vec<tree, va_gc> *origtypes)
4348 : : {
4349 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4350 : 48614307 : STRIP_TYPE_NOPS (function);
4351 : :
4352 : : /* Convert anything with function type to a pointer-to-function. */
4353 : 48614296 : if (TREE_CODE (function) == FUNCTION_DECL)
4354 : : {
4355 : : /* Implement type-directed function overloading for builtins.
4356 : : resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
4357 : : handle all the type checking. The result is a complete expression
4358 : : that implements this function call. */
4359 : 48537213 : tree tem = resolve_overloaded_builtin (loc, function, params);
4360 : 48537213 : if (tem)
4361 : : return tem;
4362 : : }
4363 : 48528829 : return build_function_call_vec (loc, arg_loc, function, params, origtypes);
4364 : : }
4365 : :
4366 : : /* Helper for convert_arguments called to convert the VALue of argument
4367 : : number ARGNUM from ORIGTYPE to the corresponding parameter number
4368 : : PARMNUM and TYPE.
4369 : : PLOC is the location where the conversion is being performed.
4370 : : FUNCTION and FUNDECL are the same as in convert_arguments.
4371 : : VALTYPE is the original type of VAL before the conversion and,
4372 : : for EXCESS_PRECISION_EXPR, the operand of the expression.
4373 : : NPC is true if VAL represents the null pointer constant (VAL itself
4374 : : will have been folded to an integer constant).
4375 : : RNAME is the same as FUNCTION except in Objective C when it's
4376 : : the function selector.
4377 : : EXCESS_PRECISION is true when VAL was originally represented
4378 : : as EXCESS_PRECISION_EXPR.
4379 : : WARNOPT is the same as in convert_for_assignment. */
4380 : :
4381 : : static tree
4382 : 122381529 : convert_argument (location_t ploc, tree function, tree fundecl,
4383 : : tree type, tree origtype, tree val, tree valtype,
4384 : : bool npc, tree rname, int parmnum, int argnum,
4385 : : bool excess_precision, int warnopt)
4386 : : {
4387 : : /* Formal parm type is specified by a function prototype. */
4388 : :
4389 : 122381529 : if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4390 : : {
4391 : 3 : error_at (ploc, "type of formal parameter %d is incomplete",
4392 : : parmnum + 1);
4393 : 3 : return error_mark_node;
4394 : : }
4395 : :
4396 : : /* Optionally warn about conversions that differ from the default
4397 : : conversions. */
4398 : 122381526 : if (warn_traditional_conversion || warn_traditional)
4399 : : {
4400 : 193 : if (INTEGRAL_TYPE_P (type)
4401 : 107 : && SCALAR_FLOAT_TYPE_P (valtype))
4402 : 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4403 : : "passing argument %d of %qE as integer rather "
4404 : : "than floating due to prototype",
4405 : : argnum, rname);
4406 : 193 : if (INTEGRAL_TYPE_P (type)
4407 : 107 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4408 : 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4409 : : "passing argument %d of %qE as integer rather "
4410 : : "than complex due to prototype",
4411 : : argnum, rname);
4412 : 188 : else if (TREE_CODE (type) == COMPLEX_TYPE
4413 : 14 : && SCALAR_FLOAT_TYPE_P (valtype))
4414 : 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4415 : : "passing argument %d of %qE as complex rather "
4416 : : "than floating due to prototype",
4417 : : argnum, rname);
4418 : 181 : else if (SCALAR_FLOAT_TYPE_P (type)
4419 : 71 : && INTEGRAL_TYPE_P (valtype))
4420 : 19 : warning_at (ploc, OPT_Wtraditional_conversion,
4421 : : "passing argument %d of %qE as floating rather "
4422 : : "than integer due to prototype",
4423 : : argnum, rname);
4424 : 162 : else if (TREE_CODE (type) == COMPLEX_TYPE
4425 : 7 : && INTEGRAL_TYPE_P (valtype))
4426 : 5 : warning_at (ploc, OPT_Wtraditional_conversion,
4427 : : "passing argument %d of %qE as complex rather "
4428 : : "than integer due to prototype",
4429 : : argnum, rname);
4430 : 157 : else if (SCALAR_FLOAT_TYPE_P (type)
4431 : 52 : && TREE_CODE (valtype) == COMPLEX_TYPE)
4432 : 7 : warning_at (ploc, OPT_Wtraditional_conversion,
4433 : : "passing argument %d of %qE as floating rather "
4434 : : "than complex due to prototype",
4435 : : argnum, rname);
4436 : : /* ??? At some point, messages should be written about
4437 : : conversions between complex types, but that's too messy
4438 : : to do now. */
4439 : 150 : else if (SCALAR_FLOAT_TYPE_P (type)
4440 : 45 : && SCALAR_FLOAT_TYPE_P (valtype))
4441 : : {
4442 : 45 : unsigned int formal_prec = TYPE_PRECISION (type);
4443 : :
4444 : : /* Warn if any argument is passed as `float',
4445 : : since without a prototype it would be `double'. */
4446 : 45 : if (formal_prec == TYPE_PRECISION (float_type_node)
4447 : 45 : && type != dfloat32_type_node)
4448 : 7 : warning_at (ploc, 0,
4449 : : "passing argument %d of %qE as %<float%> "
4450 : : "rather than %<double%> due to prototype",
4451 : : argnum, rname);
4452 : :
4453 : : /* Warn if mismatch between argument and prototype
4454 : : for decimal float types. Warn of conversions with
4455 : : binary float types and of precision narrowing due to
4456 : : prototype. */
4457 : 38 : else if (type != valtype
4458 : 32 : && (type == dfloat32_type_node
4459 : 22 : || type == dfloat64_type_node
4460 : 12 : || type == dfloat128_type_node
4461 : 2 : || valtype == dfloat32_type_node
4462 : 2 : || valtype == dfloat64_type_node
4463 : 2 : || valtype == dfloat128_type_node)
4464 : 38 : && (formal_prec
4465 : 30 : <= TYPE_PRECISION (valtype)
4466 : 14 : || (type == dfloat128_type_node
4467 : 10 : && (valtype
4468 : 10 : != dfloat64_type_node
4469 : 8 : && (valtype
4470 : : != dfloat32_type_node)))
4471 : 8 : || (type == dfloat64_type_node
4472 : 4 : && (valtype
4473 : : != dfloat32_type_node))))
4474 : 24 : warning_at (ploc, 0,
4475 : : "passing argument %d of %qE as %qT "
4476 : : "rather than %qT due to prototype",
4477 : : argnum, rname, type, valtype);
4478 : :
4479 : : }
4480 : : /* Detect integer changing in width or signedness.
4481 : : These warnings are only activated with
4482 : : -Wtraditional-conversion, not with -Wtraditional. */
4483 : 105 : else if (warn_traditional_conversion
4484 : 105 : && INTEGRAL_TYPE_P (type)
4485 : 102 : && INTEGRAL_TYPE_P (valtype))
4486 : : {
4487 : 83 : unsigned int formal_prec = TYPE_PRECISION (type);
4488 : 83 : tree would_have_been = default_conversion (val);
4489 : 83 : tree type1 = TREE_TYPE (would_have_been);
4490 : :
4491 : 83 : if (val == error_mark_node)
4492 : : /* VAL could have been of incomplete type. */;
4493 : 83 : else if (TREE_CODE (type) == ENUMERAL_TYPE
4494 : 83 : && (TYPE_MAIN_VARIANT (type)
4495 : 2 : == TYPE_MAIN_VARIANT (valtype)))
4496 : : /* No warning if function asks for enum
4497 : : and the actual arg is that enum type. */
4498 : : ;
4499 : 81 : else if (formal_prec != TYPE_PRECISION (type1))
4500 : 14 : warning_at (ploc, OPT_Wtraditional_conversion,
4501 : : "passing argument %d of %qE "
4502 : : "with different width due to prototype",
4503 : : argnum, rname);
4504 : 67 : else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
4505 : : ;
4506 : : /* Don't complain if the formal parameter type
4507 : : is an enum, because we can't tell now whether
4508 : : the value was an enum--even the same enum. */
4509 : 32 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
4510 : : ;
4511 : 32 : else if (TREE_CODE (val) == INTEGER_CST
4512 : 5 : && int_fits_type_p (val, type))
4513 : : /* Change in signedness doesn't matter
4514 : : if a constant value is unaffected. */
4515 : : ;
4516 : : /* If the value is extended from a narrower
4517 : : unsigned type, it doesn't matter whether we
4518 : : pass it as signed or unsigned; the value
4519 : : certainly is the same either way. */
4520 : 32 : else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
4521 : 32 : && TYPE_UNSIGNED (valtype))
4522 : : ;
4523 : 32 : else if (TYPE_UNSIGNED (type))
4524 : 17 : warning_at (ploc, OPT_Wtraditional_conversion,
4525 : : "passing argument %d of %qE "
4526 : : "as unsigned due to prototype",
4527 : : argnum, rname);
4528 : : else
4529 : 15 : warning_at (ploc, OPT_Wtraditional_conversion,
4530 : : "passing argument %d of %qE "
4531 : : "as signed due to prototype",
4532 : : argnum, rname);
4533 : : }
4534 : : }
4535 : :
4536 : : /* Possibly restore an EXCESS_PRECISION_EXPR for the
4537 : : sake of better warnings from convert_and_check. */
4538 : 122381526 : if (excess_precision)
4539 : 370 : val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
4540 : :
4541 : 122381526 : tree parmval = convert_for_assignment (ploc, ploc, type,
4542 : : val, origtype, ic_argpass,
4543 : : npc, fundecl, function,
4544 : : parmnum + 1, warnopt);
4545 : 122381526 : return parmval;
4546 : : }
4547 : :
4548 : : /* Convert the argument expressions in the vector VALUES
4549 : : to the types in the list TYPE_ARG_TYPES (FNTYPE).
4550 : :
4551 : : If the list is exhausted, or when an element has NULL as its type,
4552 : : perform the default conversions.
4553 : :
4554 : : ORIGTYPES is the original types of the expressions in VALUES. This
4555 : : holds the type of enum values which have been converted to integral
4556 : : types. It may be NULL.
4557 : :
4558 : : FUNCTION is a tree for the called function. It is used only for
4559 : : error messages, where it is formatted with %qE.
4560 : :
4561 : : This is also where warnings about wrong number of args are generated.
4562 : :
4563 : : ARG_LOC are locations of function arguments (if any).
4564 : :
4565 : : Returns the actual number of arguments processed (which may be less
4566 : : than the length of VALUES in some error situations), or -1 on
4567 : : failure. */
4568 : :
4569 : : static int
4570 : 48614699 : convert_arguments (location_t loc, vec<location_t> arg_loc, tree fntype,
4571 : : vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
4572 : : tree function, tree fundecl)
4573 : : {
4574 : 48614699 : tree typelist = TYPE_ARG_TYPES (fntype);
4575 : 48614699 : unsigned int parmnum;
4576 : 48614699 : bool error_args = false;
4577 : 48614699 : const bool type_generic = fundecl
4578 : 48614699 : && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
4579 : 48614699 : bool type_generic_remove_excess_precision = false;
4580 : 48614699 : bool type_generic_overflow_p = false;
4581 : 48614699 : bool type_generic_bit_query = false;
4582 : 48614699 : tree selector;
4583 : :
4584 : : /* Change pointer to function to the function itself for
4585 : : diagnostics. */
4586 : 48614699 : if (TREE_CODE (function) == ADDR_EXPR
4587 : 48614699 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4588 : 48539754 : function = TREE_OPERAND (function, 0);
4589 : :
4590 : : /* Handle an ObjC selector specially for diagnostics. */
4591 : 48614699 : selector = objc_message_selector ();
4592 : :
4593 : : /* For a call to a built-in function declared without a prototype,
4594 : : set to the built-in function's argument list. */
4595 : 48614699 : tree builtin_typelist = NULL_TREE;
4596 : :
4597 : : /* For type-generic built-in functions, determine whether excess
4598 : : precision should be removed (classification) or not
4599 : : (comparison). */
4600 : 48614699 : if (fundecl
4601 : 48614699 : && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
4602 : : {
4603 : 1386564 : built_in_function code = DECL_FUNCTION_CODE (fundecl);
4604 : 1386564 : if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
4605 : : {
4606 : : /* For a call to a built-in function declared without a prototype
4607 : : use the types of the parameters of the internal built-in to
4608 : : match those of the arguments to. */
4609 : 683341 : if (tree bdecl = builtin_decl_explicit (code))
4610 : 683341 : builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
4611 : : }
4612 : :
4613 : : /* For type-generic built-in functions, determine whether excess
4614 : : precision should be removed (classification) or not
4615 : : (comparison). */
4616 : 1386564 : if (type_generic)
4617 : 60750 : switch (code)
4618 : : {
4619 : 5305 : case BUILT_IN_ISFINITE:
4620 : 5305 : case BUILT_IN_ISINF:
4621 : 5305 : case BUILT_IN_ISINF_SIGN:
4622 : 5305 : case BUILT_IN_ISNAN:
4623 : 5305 : case BUILT_IN_ISNORMAL:
4624 : 5305 : case BUILT_IN_ISSIGNALING:
4625 : 5305 : case BUILT_IN_FPCLASSIFY:
4626 : 5305 : type_generic_remove_excess_precision = true;
4627 : 5305 : break;
4628 : :
4629 : 21012 : case BUILT_IN_ADD_OVERFLOW_P:
4630 : 21012 : case BUILT_IN_SUB_OVERFLOW_P:
4631 : 21012 : case BUILT_IN_MUL_OVERFLOW_P:
4632 : : /* The last argument of these type-generic builtins
4633 : : should not be promoted. */
4634 : 21012 : type_generic_overflow_p = true;
4635 : 21012 : break;
4636 : :
4637 : 1294 : case BUILT_IN_CLZG:
4638 : 1294 : case BUILT_IN_CTZG:
4639 : 1294 : case BUILT_IN_CLRSBG:
4640 : 1294 : case BUILT_IN_FFSG:
4641 : 1294 : case BUILT_IN_PARITYG:
4642 : 1294 : case BUILT_IN_POPCOUNTG:
4643 : : /* The first argument of these type-generic builtins
4644 : : should not be promoted. */
4645 : 1294 : type_generic_bit_query = true;
4646 : 1294 : break;
4647 : :
4648 : : default:
4649 : : break;
4650 : : }
4651 : : }
4652 : :
4653 : : /* Scan the given expressions (VALUES) and types (TYPELIST), producing
4654 : : individual converted arguments. */
4655 : :
4656 : 48614699 : tree typetail, builtin_typetail, val;
4657 : 48614699 : for (typetail = typelist,
4658 : 48614699 : builtin_typetail = builtin_typelist,
4659 : 48614699 : parmnum = 0;
4660 : 171929549 : values && values->iterate (parmnum, &val);
4661 : : ++parmnum)
4662 : : {
4663 : : /* The type of the function parameter (if it was declared with one). */
4664 : 245694529 : tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
4665 : : /* The type of the built-in function parameter (if the function
4666 : : is a built-in). Used to detect type incompatibilities in
4667 : : calls to built-ins declared without a prototype. */
4668 : 123314886 : tree builtin_type = (builtin_typetail
4669 : 124343243 : ? TREE_VALUE (builtin_typetail) : NULL_TREE);
4670 : : /* The original type of the argument being passed to the function. */
4671 : 123314886 : tree valtype = TREE_TYPE (val);
4672 : : /* The called function (or function selector in Objective C). */
4673 : 123314886 : tree rname = function;
4674 : 123314886 : int argnum = parmnum + 1;
4675 : 123314886 : const char *invalid_func_diag;
4676 : : /* Set for EXCESS_PRECISION_EXPR arguments. */
4677 : 123314886 : bool excess_precision = false;
4678 : : /* The value of the argument after conversion to the type
4679 : : of the function parameter it is passed to. */
4680 : 123314886 : tree parmval;
4681 : : /* Some __atomic_* builtins have additional hidden argument at
4682 : : position 0. */
4683 : 123314886 : location_t ploc
4684 : 123047455 : = !arg_loc.is_empty () && values->length () == arg_loc.length ()
4685 : 123046855 : ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
4686 : 123314886 : : input_location;
4687 : :
4688 : 123314886 : if (type == void_type_node)
4689 : : {
4690 : 33 : auto_diagnostic_group d;
4691 : 33 : int num_expected = parmnum;
4692 : 33 : int num_actual = values->length ();
4693 : 33 : gcc_rich_location rich_loc (loc);
4694 : 33 : if (ploc != input_location)
4695 : 32 : rich_loc.add_range (ploc);
4696 : 33 : if (selector)
4697 : 0 : error_at (&rich_loc,
4698 : : "too many arguments to method %qE; expected %i, have %i",
4699 : : selector, num_expected, num_actual);
4700 : : else
4701 : 33 : error_at (&rich_loc,
4702 : : "too many arguments to function %qE; expected %i, have %i",
4703 : : function, num_expected, num_actual);
4704 : 33 : inform_declaration (fundecl, function);
4705 : 65 : return error_args ? -1 : (int) parmnum;
4706 : 33 : }
4707 : :
4708 : 123314853 : if (builtin_type == void_type_node)
4709 : : {
4710 : 12 : auto_diagnostic_group d;
4711 : 12 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4712 : : "too many arguments to built-in function %qE "
4713 : : "expecting %d", function, parmnum))
4714 : 12 : inform_declaration (fundecl, function);
4715 : 12 : builtin_typetail = NULL_TREE;
4716 : 12 : }
4717 : :
4718 : 89832 : if (!typetail && parmnum == 0 && !TYPE_NO_NAMED_ARGS_STDARG_P (fntype)
4719 : 123404568 : && !(fundecl && fndecl_built_in_p (fundecl)))
4720 : : {
4721 : 6963 : auto_diagnostic_group d;
4722 : 6963 : bool warned;
4723 : 6963 : if (selector)
4724 : 0 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4725 : : "ISO C23 does not allow arguments"
4726 : : " for method %qE declared without parameters",
4727 : : function);
4728 : : else
4729 : 6963 : warned = warning_at (loc, OPT_Wdeprecated_non_prototype,
4730 : : "ISO C23 does not allow arguments"
4731 : : " for function %qE declared without parameters",
4732 : : function);
4733 : 6963 : if (warned)
4734 : 4 : inform_declaration (fundecl, function);
4735 : 6963 : }
4736 : :
4737 : 123314853 : if (selector && argnum > 2)
4738 : : {
4739 : 0 : rname = selector;
4740 : 0 : argnum -= 2;
4741 : : }
4742 : :
4743 : : /* Determine if VAL is a null pointer constant before folding it. */
4744 : 123314853 : bool npc = null_pointer_constant_p (val);
4745 : :
4746 : : /* If there is excess precision and a prototype, convert once to
4747 : : the required type rather than converting via the semantic
4748 : : type. Likewise without a prototype a float value represented
4749 : : as long double should be converted once to double. But for
4750 : : type-generic classification functions excess precision must
4751 : : be removed here. */
4752 : 123314853 : if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
4753 : 423 : && (type || !type_generic || !type_generic_remove_excess_precision))
4754 : : {
4755 : 403 : val = TREE_OPERAND (val, 0);
4756 : 403 : excess_precision = true;
4757 : : }
4758 : 123314853 : val = c_fully_fold (val, false, NULL);
4759 : 246629707 : STRIP_TYPE_NOPS (val);
4760 : :
4761 : 123314853 : val = require_complete_type (ploc, val);
4762 : :
4763 : : /* Some floating-point arguments must be promoted to double when
4764 : : no type is specified by a prototype. This applies to
4765 : : arguments of type float, and to architecture-specific types
4766 : : (ARM __fp16), but not to _FloatN or _FloatNx types. */
4767 : 123314853 : bool promote_float_arg = false;
4768 : 123314853 : if (type == NULL_TREE
4769 : 935243 : && TREE_CODE (valtype) == REAL_TYPE
4770 : 269617 : && (TYPE_PRECISION (valtype)
4771 : 269617 : <= TYPE_PRECISION (double_type_node))
4772 : 262335 : && TYPE_MAIN_VARIANT (valtype) != double_type_node
4773 : 131343 : && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
4774 : 123446196 : && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
4775 : : {
4776 : : /* Promote this argument, unless it has a _FloatN or
4777 : : _FloatNx type. */
4778 : 1027603 : promote_float_arg = true;
4779 : 1027603 : for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4780 : 900219 : if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
4781 : : {
4782 : : promote_float_arg = false;
4783 : : break;
4784 : : }
4785 : : /* Don't promote __bf16 either. */
4786 : 130610 : if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
4787 : 123184313 : promote_float_arg = false;
4788 : : }
4789 : :
4790 : 123314853 : if (type != NULL_TREE)
4791 : : {
4792 : 122379610 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4793 : 122379610 : parmval = convert_argument (ploc, function, fundecl, type, origtype,
4794 : : val, valtype, npc, rname, parmnum, argnum,
4795 : : excess_precision, 0);
4796 : : }
4797 : : /* A NULLPTR type is just a nullptr always. */
4798 : 935243 : else if (TREE_CODE (TREE_TYPE (val)) == NULLPTR_TYPE)
4799 : 9 : parmval = omit_one_operand_loc (ploc, TREE_TYPE (val), nullptr_node, val);
4800 : 935234 : else if (promote_float_arg)
4801 : : {
4802 : 127314 : if (type_generic)
4803 : : parmval = val;
4804 : : else
4805 : : {
4806 : : /* Convert `float' to `double'. */
4807 : 124446 : if (warn_double_promotion && !c_inhibit_evaluation_warnings)
4808 : 6 : warning_at (ploc, OPT_Wdouble_promotion,
4809 : : "implicit conversion from %qT to %qT when passing "
4810 : : "argument to function",
4811 : : valtype, double_type_node);
4812 : 124446 : parmval = convert (double_type_node, val);
4813 : : }
4814 : : }
4815 : 807920 : else if ((excess_precision && !type_generic)
4816 : 807918 : || (type_generic_overflow_p && parmnum == 2)
4817 : 786910 : || (type_generic_bit_query && parmnum == 0))
4818 : : /* A "double" argument with excess precision being passed
4819 : : without a prototype or in variable arguments.
4820 : : The last argument of __builtin_*_overflow_p should not be
4821 : : promoted, similarly the first argument of
4822 : : __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
4823 : 22298 : parmval = convert (valtype, val);
4824 : 1571244 : else if ((invalid_func_diag =
4825 : 785622 : targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4826 : : {
4827 : 0 : error (invalid_func_diag);
4828 : 0 : return -1;
4829 : : }
4830 : 785622 : else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
4831 : : {
4832 : : return -1;
4833 : : }
4834 : : else
4835 : : /* Convert `short' and `char' to full-size `int'. */
4836 : 785619 : parmval = default_conversion (val);
4837 : :
4838 : 123314850 : (*values)[parmnum] = parmval;
4839 : 123314850 : if (parmval == error_mark_node)
4840 : 105 : error_args = true;
4841 : :
4842 : 123314850 : if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
4843 : : {
4844 : : /* For a call to a built-in function declared without a prototype,
4845 : : perform the conversions from the argument to the expected type
4846 : : but issue warnings rather than errors for any mismatches.
4847 : : Ignore the converted argument and use the PARMVAL obtained
4848 : : above by applying default conversions instead. */
4849 : 1919 : tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
4850 : 1919 : convert_argument (ploc, function, fundecl, builtin_type, origtype,
4851 : : val, valtype, npc, rname, parmnum, argnum,
4852 : : excess_precision,
4853 : : OPT_Wbuiltin_declaration_mismatch);
4854 : : }
4855 : :
4856 : 123314850 : if (typetail)
4857 : 122379610 : typetail = TREE_CHAIN (typetail);
4858 : :
4859 : 123314850 : if (builtin_typetail)
4860 : 1028340 : builtin_typetail = TREE_CHAIN (builtin_typetail);
4861 : : }
4862 : :
4863 : 85824080 : gcc_assert (parmnum == vec_safe_length (values));
4864 : :
4865 : 96900563 : if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
4866 : : {
4867 : : /* Not enough args.
4868 : : Determine minimum number of arguments required. */
4869 : : int min_expected_num = 0;
4870 : 231 : bool at_least_p = false;
4871 : : tree iter = typelist;
4872 : 386 : while (true)
4873 : : {
4874 : 231 : if (!iter)
4875 : : {
4876 : : /* Variadic arguments; stop iterating. */
4877 : : at_least_p = true;
4878 : : break;
4879 : : }
4880 : 222 : if (iter == void_list_node)
4881 : : /* End of arguments; stop iterating. */
4882 : : break;
4883 : 155 : ++min_expected_num;
4884 : 155 : iter = TREE_CHAIN (iter);
4885 : : }
4886 : 76 : auto_diagnostic_group d;
4887 : 76 : int actual_num = vec_safe_length (values);
4888 : 143 : error_at (loc,
4889 : : at_least_p
4890 : : ? G_("too few arguments to function %qE; expected at least %i, have %i")
4891 : : : G_("too few arguments to function %qE; expected %i, have %i"),
4892 : : function, min_expected_num, actual_num);
4893 : 76 : inform_declaration (fundecl, function);
4894 : 76 : return -1;
4895 : 76 : }
4896 : :
4897 : 49261949 : if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
4898 : : {
4899 : : unsigned nargs = parmnum;
4900 : 616 : for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
4901 : 428 : ++nargs;
4902 : :
4903 : 188 : auto_diagnostic_group d;
4904 : 188 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
4905 : : "too few arguments to built-in function %qE "
4906 : : "expecting %u", function, nargs - 1))
4907 : 173 : inform_declaration (fundecl, function);
4908 : 188 : }
4909 : :
4910 : 48614587 : return error_args ? -1 : (int) parmnum;
4911 : : }
4912 : :
4913 : : /* This is the entry point used by the parser to build unary operators
4914 : : in the input. CODE, a tree_code, specifies the unary operator, and
4915 : : ARG is the operand. For unary plus, the C parser currently uses
4916 : : CONVERT_EXPR for code.
4917 : :
4918 : : LOC is the location to use for the tree generated.
4919 : : */
4920 : :
4921 : : struct c_expr
4922 : 7968980 : parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
4923 : : {
4924 : 7968980 : struct c_expr result;
4925 : :
4926 : 7968980 : result.original_code = code;
4927 : 7968980 : result.original_type = NULL;
4928 : 7968980 : result.m_decimal = 0;
4929 : :
4930 : 7968980 : if (reject_gcc_builtin (arg.value))
4931 : : {
4932 : 2 : result.value = error_mark_node;
4933 : : }
4934 : : else
4935 : : {
4936 : 7968978 : result.value = build_unary_op (loc, code, arg.value, false);
4937 : :
4938 : 7968978 : if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
4939 : 5 : overflow_warning (loc, result.value, arg.value);
4940 : : }
4941 : :
4942 : : /* We are typically called when parsing a prefix token at LOC acting on
4943 : : ARG. Reflect this by updating the source range of the result to
4944 : : start at LOC and end at the end of ARG. */
4945 : 7968980 : set_c_expr_source_range (&result,
4946 : : loc, arg.get_finish ());
4947 : :
4948 : 7968980 : return result;
4949 : : }
4950 : :
4951 : : /* Returns true if TYPE is a character type, *not* including wchar_t. */
4952 : :
4953 : : bool
4954 : 1248911 : char_type_p (tree type)
4955 : : {
4956 : 1248911 : return (type == char_type_node
4957 : 962928 : || type == unsigned_char_type_node
4958 : 928818 : || type == signed_char_type_node
4959 : 927971 : || type == char16_type_node
4960 : 1961911 : || type == char32_type_node);
4961 : : }
4962 : :
4963 : : /* This is the entry point used by the parser to build binary operators
4964 : : in the input. CODE, a tree_code, specifies the binary operator, and
4965 : : ARG1 and ARG2 are the operands. In addition to constructing the
4966 : : expression, we check for operands that were written with other binary
4967 : : operators in a way that is likely to confuse the user.
4968 : :
4969 : : LOCATION is the location of the binary operator. */
4970 : :
4971 : : struct c_expr
4972 : 9152691 : parser_build_binary_op (location_t location, enum tree_code code,
4973 : : struct c_expr arg1, struct c_expr arg2)
4974 : : {
4975 : 9152691 : struct c_expr result;
4976 : 9152691 : result.m_decimal = 0;
4977 : :
4978 : 9152691 : enum tree_code code1 = arg1.original_code;
4979 : 9152691 : enum tree_code code2 = arg2.original_code;
4980 : 9152691 : tree type1 = (arg1.original_type
4981 : 9152691 : ? arg1.original_type
4982 : 5883880 : : TREE_TYPE (arg1.value));
4983 : 9152691 : tree type2 = (arg2.original_type
4984 : 9152691 : ? arg2.original_type
4985 : 6756143 : : TREE_TYPE (arg2.value));
4986 : :
4987 : 9152691 : result.value = build_binary_op (location, code,
4988 : : arg1.value, arg2.value, true);
4989 : 9152690 : result.original_code = code;
4990 : 9152690 : result.original_type = NULL;
4991 : 9152690 : result.m_decimal = 0;
4992 : :
4993 : 9152690 : if (TREE_CODE (result.value) == ERROR_MARK)
4994 : : {
4995 : 1297 : set_c_expr_source_range (&result,
4996 : : arg1.get_start (),
4997 : : arg2.get_finish ());
4998 : 1297 : return result;
4999 : : }
5000 : :
5001 : 9151393 : if (location != UNKNOWN_LOCATION)
5002 : 9151393 : protected_set_expr_location (result.value, location);
5003 : :
5004 : 9151393 : set_c_expr_source_range (&result,
5005 : : arg1.get_start (),
5006 : : arg2.get_finish ());
5007 : :
5008 : : /* Check for cases such as x+y<<z which users are likely
5009 : : to misinterpret. */
5010 : 9151393 : if (warn_parentheses)
5011 : 2000670 : warn_about_parentheses (location, code, code1, arg1.value, code2,
5012 : : arg2.value);
5013 : :
5014 : 9151393 : if (warn_logical_op)
5015 : 340 : warn_logical_operator (location, code, TREE_TYPE (result.value),
5016 : : code1, arg1.value, code2, arg2.value);
5017 : :
5018 : 9151393 : if (warn_tautological_compare)
5019 : : {
5020 : 1999183 : tree lhs = arg1.value;
5021 : 1999183 : tree rhs = arg2.value;
5022 : 1999183 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5023 : : {
5024 : 2 : if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
5025 : 2 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
5026 : : lhs = NULL_TREE;
5027 : : else
5028 : 2 : lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
5029 : : }
5030 : 1999183 : if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
5031 : : {
5032 : 12 : if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
5033 : 12 : && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
5034 : : rhs = NULL_TREE;
5035 : : else
5036 : 12 : rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
5037 : : }
5038 : 1999183 : if (lhs != NULL_TREE && rhs != NULL_TREE)
5039 : 1999183 : warn_tautological_cmp (location, code, lhs, rhs);
5040 : : }
5041 : :
5042 : 9151393 : if (warn_logical_not_paren
5043 : 1999266 : && TREE_CODE_CLASS (code) == tcc_comparison
5044 : 404375 : && code1 == TRUTH_NOT_EXPR
5045 : 404375 : && code2 != TRUTH_NOT_EXPR
5046 : : /* Avoid warning for !!x == y. */
5047 : 9151519 : && (TREE_CODE (arg1.value) != NE_EXPR
5048 : 19 : || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
5049 : : {
5050 : : /* Avoid warning for !b == y where b has _Bool type. */
5051 : 107 : tree t = integer_zero_node;
5052 : 107 : if (TREE_CODE (arg1.value) == EQ_EXPR
5053 : 98 : && integer_zerop (TREE_OPERAND (arg1.value, 1))
5054 : 205 : && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
5055 : : {
5056 : 90 : t = TREE_OPERAND (arg1.value, 0);
5057 : 199 : do
5058 : : {
5059 : 199 : if (TREE_TYPE (t) != integer_type_node)
5060 : : break;
5061 : 180 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
5062 : 90 : t = C_MAYBE_CONST_EXPR_EXPR (t);
5063 : 90 : else if (CONVERT_EXPR_P (t))
5064 : 19 : t = TREE_OPERAND (t, 0);
5065 : : else
5066 : : break;
5067 : : }
5068 : : while (1);
5069 : : }
5070 : 107 : if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
5071 : 88 : warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
5072 : : }
5073 : :
5074 : : /* Warn about comparisons against string literals, with the exception
5075 : : of testing for equality or inequality of a string literal with NULL. */
5076 : 9151393 : if (code == EQ_EXPR || code == NE_EXPR)
5077 : : {
5078 : 1148518 : if ((code1 == STRING_CST
5079 : 16 : && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
5080 : 1148529 : || (code2 == STRING_CST
5081 : 21 : && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
5082 : 18 : warning_at (location, OPT_Waddress,
5083 : : "comparison with string literal results in unspecified behavior");
5084 : : /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
5085 : 1148518 : if (POINTER_TYPE_P (type1)
5086 : 90202 : && null_pointer_constant_p (arg2.value)
5087 : 1169339 : && char_type_p (type2))
5088 : : {
5089 : 18 : auto_diagnostic_group d;
5090 : 18 : if (warning_at (location, OPT_Wpointer_compare,
5091 : : "comparison between pointer and zero character "
5092 : : "constant"))
5093 : 10 : inform (arg1.get_start (),
5094 : : "did you mean to dereference the pointer?");
5095 : 18 : }
5096 : 1148500 : else if (POINTER_TYPE_P (type2)
5097 : 87534 : && null_pointer_constant_p (arg1.value)
5098 : 1148762 : && char_type_p (type1))
5099 : : {
5100 : 10 : auto_diagnostic_group d;
5101 : 10 : if (warning_at (location, OPT_Wpointer_compare,
5102 : : "comparison between pointer and zero character "
5103 : : "constant"))
5104 : 10 : inform (arg2.get_start (),
5105 : : "did you mean to dereference the pointer?");
5106 : 10 : }
5107 : : }
5108 : 8002875 : else if (TREE_CODE_CLASS (code) == tcc_comparison
5109 : 974623 : && (code1 == STRING_CST || code2 == STRING_CST))
5110 : 0 : warning_at (location, OPT_Waddress,
5111 : : "comparison with string literal results in unspecified "
5112 : : "behavior");
5113 : :
5114 : 9151393 : if (warn_zero_as_null_pointer_constant
5115 : 18 : && c_inhibit_evaluation_warnings == 0
5116 : 18 : && TREE_CODE_CLASS (code) == tcc_comparison)
5117 : : {
5118 : 18 : if ((TREE_CODE (type1) == POINTER_TYPE
5119 : 14 : || TREE_CODE (type1) == NULLPTR_TYPE)
5120 : 6 : && TREE_CODE (type2) == INTEGER_TYPE
5121 : 24 : && null_pointer_constant_p (arg2.value))
5122 : 6 : warning_at (arg2.get_location(), OPT_Wzero_as_null_pointer_constant,
5123 : : "zero as null pointer constant");
5124 : :
5125 : 18 : if ((TREE_CODE (type2) == POINTER_TYPE
5126 : 12 : || TREE_CODE (type2) == NULLPTR_TYPE)
5127 : 8 : && TREE_CODE (type1) == INTEGER_TYPE
5128 : 26 : && null_pointer_constant_p (arg1.value))
5129 : 8 : warning_at (arg1.get_location(), OPT_Wzero_as_null_pointer_constant,
5130 : : "zero as null pointer constant");
5131 : : }
5132 : :
5133 : 9151393 : if (warn_array_compare
5134 : 1999021 : && TREE_CODE_CLASS (code) == tcc_comparison
5135 : 404160 : && TREE_CODE (type1) == ARRAY_TYPE
5136 : 38 : && TREE_CODE (type2) == ARRAY_TYPE)
5137 : 15 : do_warn_array_compare (location, code, arg1.value, arg2.value);
5138 : :
5139 : 2152512 : if (TREE_OVERFLOW_P (result.value)
5140 : 299 : && !TREE_OVERFLOW_P (arg1.value)
5141 : 9151685 : && !TREE_OVERFLOW_P (arg2.value))
5142 : 249 : overflow_warning (location, result.value);
5143 : :
5144 : : /* Warn about comparisons of different enum types. */
5145 : 9151392 : if (warn_enum_compare
5146 : 2018958 : && TREE_CODE_CLASS (code) == tcc_comparison
5147 : 408585 : && TREE_CODE (type1) == ENUMERAL_TYPE
5148 : 6970 : && TREE_CODE (type2) == ENUMERAL_TYPE
5149 : 9158300 : && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
5150 : 2 : warning_at (location, OPT_Wenum_compare,
5151 : : "comparison between %qT and %qT",
5152 : : type1, type2);
5153 : :
5154 : 9151392 : if (warn_xor_used_as_pow
5155 : 9151392 : && code == BIT_XOR_EXPR
5156 : 75390 : && arg1.m_decimal
5157 : 722 : && arg2.m_decimal)
5158 : 427 : check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
5159 : : location,
5160 : : arg2.get_location (), arg2.value);
5161 : :
5162 : : return result;
5163 : : }
5164 : :
5165 : : /* Return a tree for the difference of pointers OP0 and OP1.
5166 : : The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
5167 : : enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5168 : :
5169 : : static tree
5170 : 3625 : pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
5171 : : {
5172 : 3625 : tree restype = ptrdiff_type_node;
5173 : 3625 : tree result, inttype;
5174 : :
5175 : 3625 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
5176 : 3625 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
5177 : 3625 : tree target_type = TREE_TYPE (TREE_TYPE (op0));
5178 : 3625 : tree orig_op0 = op0;
5179 : 3625 : tree orig_op1 = op1;
5180 : :
5181 : : /* If the operands point into different address spaces, we need to
5182 : : explicitly convert them to pointers into the common address space
5183 : : before we can subtract the numerical address values. */
5184 : 3625 : if (as0 != as1)
5185 : : {
5186 : 0 : addr_space_t as_common;
5187 : 0 : tree common_type;
5188 : :
5189 : : /* Determine the common superset address space. This is guaranteed
5190 : : to exist because the caller verified that comp_target_types
5191 : : returned non-zero. */
5192 : 0 : if (!addr_space_superset (as0, as1, &as_common))
5193 : 0 : gcc_unreachable ();
5194 : :
5195 : 0 : common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1), NULL_TREE);
5196 : 0 : op0 = convert (common_type, op0);
5197 : 0 : op1 = convert (common_type, op1);
5198 : : }
5199 : :
5200 : : /* Determine integer type result of the subtraction. This will usually
5201 : : be the same as the result type (ptrdiff_t), but may need to be a wider
5202 : : type if pointers for the address space are wider than ptrdiff_t. */
5203 : 3625 : if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
5204 : 0 : inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
5205 : : else
5206 : : inttype = restype;
5207 : :
5208 : 3625 : if (VOID_TYPE_P (target_type))
5209 : 135 : pedwarn (loc, OPT_Wpointer_arith,
5210 : : "pointer of type %<void *%> used in subtraction");
5211 : 3625 : if (TREE_CODE (target_type) == FUNCTION_TYPE)
5212 : 4 : pedwarn (loc, OPT_Wpointer_arith,
5213 : : "pointer to a function used in subtraction");
5214 : :
5215 : 3625 : if (current_function_decl != NULL_TREE
5216 : 3625 : && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
5217 : : {
5218 : 70 : op0 = save_expr (c_fully_fold (op0, false, NULL));
5219 : 70 : op1 = save_expr (c_fully_fold (op1, false, NULL));
5220 : :
5221 : 70 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
5222 : 70 : *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
5223 : : }
5224 : :
5225 : : /* First do the subtraction, then build the divide operator
5226 : : and only convert at the very end.
5227 : : Do not do default conversions in case restype is a short type. */
5228 : :
5229 : : /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5230 : : pointers. If some platform cannot provide that, or has a larger
5231 : : ptrdiff_type to support differences larger than half the address
5232 : : space, cast the pointers to some larger integer type and do the
5233 : : computations in that type. */
5234 : 3625 : if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
5235 : 0 : op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
5236 : : convert (inttype, op1), false);
5237 : : else
5238 : : {
5239 : : /* Cast away qualifiers. */
5240 : 3625 : op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
5241 : 3625 : op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
5242 : 3625 : op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
5243 : : }
5244 : :
5245 : : /* This generates an error if op1 is pointer to incomplete type. */
5246 : 3625 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
5247 : 4 : error_at (loc, "arithmetic on pointer to an incomplete type");
5248 : 3621 : else if (verify_type_context (loc, TCTX_POINTER_ARITH,
5249 : 3621 : TREE_TYPE (TREE_TYPE (orig_op0))))
5250 : 7242 : verify_type_context (loc, TCTX_POINTER_ARITH,
5251 : 3621 : TREE_TYPE (TREE_TYPE (orig_op1)));
5252 : :
5253 : 3625 : op1 = c_size_in_bytes (target_type);
5254 : :
5255 : 3625 : if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
5256 : 4 : error_at (loc, "arithmetic on pointer to an empty aggregate");
5257 : :
5258 : : /* Divide by the size, in easiest possible way. */
5259 : 3625 : result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
5260 : : op0, convert (inttype, op1));
5261 : :
5262 : : /* Convert to final result type if necessary. */
5263 : 3625 : return convert (restype, result);
5264 : : }
5265 : :
5266 : : /* Expand atomic compound assignments into an appropriate sequence as
5267 : : specified by the C11 standard section 6.5.16.2.
5268 : :
5269 : : _Atomic T1 E1
5270 : : T2 E2
5271 : : E1 op= E2
5272 : :
5273 : : This sequence is used for all types for which these operations are
5274 : : supported.
5275 : :
5276 : : In addition, built-in versions of the 'fe' prefixed routines may
5277 : : need to be invoked for floating point (real, complex or vector) when
5278 : : floating-point exceptions are supported. See 6.5.16.2 footnote 113.
5279 : :
5280 : : T1 newval;
5281 : : T1 old;
5282 : : T1 *addr
5283 : : T2 val
5284 : : fenv_t fenv
5285 : :
5286 : : addr = &E1;
5287 : : val = (E2);
5288 : : __atomic_load (addr, &old, SEQ_CST);
5289 : : feholdexcept (&fenv);
5290 : : loop:
5291 : : newval = old op val;
5292 : : if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
5293 : : SEQ_CST))
5294 : : goto done;
5295 : : feclearexcept (FE_ALL_EXCEPT);
5296 : : goto loop:
5297 : : done:
5298 : : feupdateenv (&fenv);
5299 : :
5300 : : The compiler will issue the __atomic_fetch_* built-in when possible,
5301 : : otherwise it will generate the generic form of the atomic operations.
5302 : : This requires temp(s) and has their address taken. The atomic processing
5303 : : is smart enough to figure out when the size of an object can utilize
5304 : : a lock-free version, and convert the built-in call to the appropriate
5305 : : lock-free routine. The optimizers will then dispose of any temps that
5306 : : are no longer required, and lock-free implementations are utilized as
5307 : : long as there is target support for the required size.
5308 : :
5309 : : If the operator is NOP_EXPR, then this is a simple assignment, and
5310 : : an __atomic_store is issued to perform the assignment rather than
5311 : : the above loop. */
5312 : :
5313 : : /* Build an atomic assignment at LOC, expanding into the proper
5314 : : sequence to store LHS MODIFYCODE= RHS. Return a value representing
5315 : : the result of the operation, unless RETURN_OLD_P, in which case
5316 : : return the old value of LHS (this is only for postincrement and
5317 : : postdecrement). */
5318 : :
5319 : : static tree
5320 : 30425 : build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
5321 : : tree rhs, bool return_old_p)
5322 : : {
5323 : 30425 : tree fndecl, func_call;
5324 : 30425 : vec<tree, va_gc> *params;
5325 : 30425 : tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
5326 : 30425 : tree old, old_addr;
5327 : 30425 : tree compound_stmt = NULL_TREE;
5328 : 30425 : tree stmt, goto_stmt;
5329 : 30425 : tree loop_label, loop_decl, done_label, done_decl;
5330 : :
5331 : 30425 : tree lhs_type = TREE_TYPE (lhs);
5332 : 30425 : tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
5333 : 30425 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
5334 : 30425 : tree rhs_semantic_type = TREE_TYPE (rhs);
5335 : 30425 : tree nonatomic_rhs_semantic_type;
5336 : 30425 : tree rhs_type;
5337 : :
5338 : 30425 : gcc_assert (TYPE_ATOMIC (lhs_type));
5339 : :
5340 : 30425 : if (return_old_p)
5341 : 2313 : gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
5342 : :
5343 : : /* Allocate enough vector items for a compare_exchange. */
5344 : 30425 : vec_alloc (params, 6);
5345 : :
5346 : : /* Create a compound statement to hold the sequence of statements
5347 : : with a loop. */
5348 : 30425 : if (modifycode != NOP_EXPR)
5349 : : {
5350 : 19954 : compound_stmt = c_begin_compound_stmt (false);
5351 : :
5352 : : /* For consistency with build_modify_expr on non-_Atomic,
5353 : : mark the lhs as read. Also, it would be very hard to match
5354 : : such expressions in mark_exp_read. */
5355 : 19954 : mark_exp_read (lhs);
5356 : : }
5357 : :
5358 : : /* Remove any excess precision (which is only present here in the
5359 : : case of compound assignments). */
5360 : 30425 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5361 : : {
5362 : 0 : gcc_assert (modifycode != NOP_EXPR);
5363 : 0 : rhs = TREE_OPERAND (rhs, 0);
5364 : : }
5365 : 30425 : rhs_type = TREE_TYPE (rhs);
5366 : :
5367 : : /* Fold the RHS if it hasn't already been folded. */
5368 : 30425 : if (modifycode != NOP_EXPR)
5369 : 19954 : rhs = c_fully_fold (rhs, false, NULL);
5370 : :
5371 : : /* Remove the qualifiers for the rest of the expressions and create
5372 : : the VAL temp variable to hold the RHS. */
5373 : 30425 : nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
5374 : 30425 : nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5375 : 30425 : nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
5376 : : TYPE_UNQUALIFIED);
5377 : 30425 : val = create_tmp_var_raw (nonatomic_rhs_type);
5378 : 30425 : TREE_ADDRESSABLE (val) = 1;
5379 : 30425 : suppress_warning (val);
5380 : 30425 : rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
5381 : : NULL_TREE);
5382 : 30425 : TREE_SIDE_EFFECTS (rhs) = 1;
5383 : 30425 : SET_EXPR_LOCATION (rhs, loc);
5384 : 30425 : if (modifycode != NOP_EXPR)
5385 : 19954 : add_stmt (rhs);
5386 : :
5387 : : /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
5388 : : an atomic_store. */
5389 : 30425 : if (modifycode == NOP_EXPR)
5390 : : {
5391 : 10471 : compound_stmt = rhs;
5392 : : /* Build __atomic_store (&lhs, &val, SEQ_CST) */
5393 : 10471 : rhs = build_unary_op (loc, ADDR_EXPR, val, false);
5394 : 10471 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
5395 : 10471 : params->quick_push (lhs_addr);
5396 : 10471 : params->quick_push (rhs);
5397 : 10471 : params->quick_push (seq_cst);
5398 : 10471 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5399 : :
5400 : 10471 : compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
5401 : : compound_stmt, func_call);
5402 : :
5403 : : /* VAL is the value which was stored, return a COMPOUND_STMT of
5404 : : the statement and that value. */
5405 : 10471 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
5406 : : }
5407 : :
5408 : : /* Attempt to implement the atomic operation as an __atomic_fetch_* or
5409 : : __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
5410 : : isn't applicable for such builtins. ??? Do we want to handle enums? */
5411 : 19954 : if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
5412 : 13941 : && TREE_CODE (rhs_type) == INTEGER_TYPE)
5413 : : {
5414 : 11713 : built_in_function fncode;
5415 : 11713 : switch (modifycode)
5416 : : {
5417 : 3075 : case PLUS_EXPR:
5418 : 3075 : case POINTER_PLUS_EXPR:
5419 : 2124 : fncode = (return_old_p
5420 : 3075 : ? BUILT_IN_ATOMIC_FETCH_ADD_N
5421 : : : BUILT_IN_ATOMIC_ADD_FETCH_N);
5422 : : break;
5423 : 2889 : case MINUS_EXPR:
5424 : 2094 : fncode = (return_old_p
5425 : 2889 : ? BUILT_IN_ATOMIC_FETCH_SUB_N
5426 : : : BUILT_IN_ATOMIC_SUB_FETCH_N);
5427 : : break;
5428 : 609 : case BIT_AND_EXPR:
5429 : 609 : fncode = (return_old_p
5430 : 609 : ? BUILT_IN_ATOMIC_FETCH_AND_N
5431 : : : BUILT_IN_ATOMIC_AND_FETCH_N);
5432 : : break;
5433 : 609 : case BIT_IOR_EXPR:
5434 : 609 : fncode = (return_old_p
5435 : 609 : ? BUILT_IN_ATOMIC_FETCH_OR_N
5436 : : : BUILT_IN_ATOMIC_OR_FETCH_N);
5437 : : break;
5438 : 609 : case BIT_XOR_EXPR:
5439 : 609 : fncode = (return_old_p
5440 : 609 : ? BUILT_IN_ATOMIC_FETCH_XOR_N
5441 : : : BUILT_IN_ATOMIC_XOR_FETCH_N);
5442 : : break;
5443 : 3922 : default:
5444 : 3922 : goto cas_loop;
5445 : : }
5446 : :
5447 : : /* We can only use "_1" through "_16" variants of the atomic fetch
5448 : : built-ins. */
5449 : 7791 : unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
5450 : 7791 : if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
5451 : 0 : goto cas_loop;
5452 : :
5453 : : /* If this is a pointer type, we need to multiply by the size of
5454 : : the pointer target type. */
5455 : 7791 : if (POINTER_TYPE_P (lhs_type))
5456 : : {
5457 : 1018 : if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
5458 : : /* ??? This would introduce -Wdiscarded-qualifiers
5459 : : warning: __atomic_fetch_* expect volatile void *
5460 : : type as the first argument. (Assignments between
5461 : : atomic and non-atomic objects are OK.) */
5462 : 1018 : || TYPE_RESTRICT (lhs_type))
5463 : 17 : goto cas_loop;
5464 : 1001 : tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
5465 : 1001 : rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
5466 : : convert (ptrdiff_type_node, rhs),
5467 : : convert (ptrdiff_type_node, sz));
5468 : : }
5469 : :
5470 : : /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
5471 : : __atomic_*_fetch (&lhs, &val, SEQ_CST). */
5472 : 7774 : fndecl = builtin_decl_explicit (fncode);
5473 : 7774 : params->quick_push (lhs_addr);
5474 : 7774 : params->quick_push (rhs);
5475 : 7774 : params->quick_push (seq_cst);
5476 : 7774 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5477 : :
5478 : 7774 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5479 : 7774 : TREE_ADDRESSABLE (newval) = 1;
5480 : 7774 : suppress_warning (newval);
5481 : 7774 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
5482 : : NULL_TREE, NULL_TREE);
5483 : 7774 : SET_EXPR_LOCATION (rhs, loc);
5484 : 7774 : add_stmt (rhs);
5485 : :
5486 : : /* Finish the compound statement. */
5487 : 7774 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5488 : :
5489 : : /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
5490 : : the statement and that value. */
5491 : 7774 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
5492 : : }
5493 : :
5494 : 8241 : cas_loop:
5495 : : /* Create the variables and labels required for the op= form. */
5496 : 12180 : old = create_tmp_var_raw (nonatomic_lhs_type);
5497 : 12180 : old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
5498 : 12180 : TREE_ADDRESSABLE (old) = 1;
5499 : 12180 : suppress_warning (old);
5500 : :
5501 : 12180 : newval = create_tmp_var_raw (nonatomic_lhs_type);
5502 : 12180 : newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
5503 : 12180 : TREE_ADDRESSABLE (newval) = 1;
5504 : 12180 : suppress_warning (newval);
5505 : :
5506 : 12180 : loop_decl = create_artificial_label (loc);
5507 : 12180 : loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
5508 : :
5509 : 12180 : done_decl = create_artificial_label (loc);
5510 : 12180 : done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
5511 : :
5512 : : /* __atomic_load (addr, &old, SEQ_CST). */
5513 : 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
5514 : 12180 : params->quick_push (lhs_addr);
5515 : 12180 : params->quick_push (old_addr);
5516 : 12180 : params->quick_push (seq_cst);
5517 : 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5518 : 12180 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
5519 : : NULL_TREE);
5520 : 12180 : add_stmt (old);
5521 : 12180 : params->truncate (0);
5522 : :
5523 : : /* Create the expressions for floating-point environment
5524 : : manipulation, if required. */
5525 : 12180 : bool need_fenv = (flag_trapping_math
5526 : 12180 : && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
5527 : 12180 : tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
5528 : 12180 : if (need_fenv)
5529 : 7054 : targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
5530 : :
5531 : 12180 : if (hold_call)
5532 : 7054 : add_stmt (hold_call);
5533 : :
5534 : : /* loop: */
5535 : 12180 : add_stmt (loop_label);
5536 : :
5537 : : /* newval = old + val; */
5538 : 12180 : if (rhs_type != rhs_semantic_type)
5539 : 0 : val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
5540 : 12180 : rhs = build_binary_op (loc, modifycode,
5541 : : convert_lvalue_to_rvalue (loc, old, true, true),
5542 : : val, true);
5543 : 12180 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5544 : : {
5545 : 0 : tree eptype = TREE_TYPE (rhs);
5546 : 0 : rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
5547 : 0 : rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
5548 : : }
5549 : : else
5550 : 12180 : rhs = c_fully_fold (rhs, false, NULL);
5551 : 12180 : rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
5552 : : rhs, NULL_TREE, ic_assign, false, NULL_TREE,
5553 : : NULL_TREE, 0);
5554 : : /* The temporary variable for NEWVAL is only gimplified correctly (in
5555 : : particular, given a DECL_CONTEXT) if used in a TARGET_EXPR. To avoid
5556 : : subsequent ICEs in nested function processing after an error, ensure such
5557 : : a TARGET_EXPR is built even after an error. */
5558 : 12180 : if (rhs == error_mark_node)
5559 : 10 : rhs = old;
5560 : 12180 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
5561 : : NULL_TREE);
5562 : 12180 : SET_EXPR_LOCATION (rhs, loc);
5563 : 12180 : add_stmt (rhs);
5564 : :
5565 : : /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
5566 : : goto done; */
5567 : 12180 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
5568 : 12180 : params->quick_push (lhs_addr);
5569 : 12180 : params->quick_push (old_addr);
5570 : 12180 : params->quick_push (newval_addr);
5571 : 12180 : params->quick_push (integer_zero_node);
5572 : 12180 : params->quick_push (seq_cst);
5573 : 12180 : params->quick_push (seq_cst);
5574 : 12180 : func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5575 : :
5576 : 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
5577 : 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5578 : :
5579 : 12180 : stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
5580 : 12180 : SET_EXPR_LOCATION (stmt, loc);
5581 : 12180 : add_stmt (stmt);
5582 : :
5583 : 12180 : if (clear_call)
5584 : 7054 : add_stmt (clear_call);
5585 : :
5586 : : /* goto loop; */
5587 : 12180 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
5588 : 12180 : SET_EXPR_LOCATION (goto_stmt, loc);
5589 : 12180 : add_stmt (goto_stmt);
5590 : :
5591 : : /* done: */
5592 : 12180 : add_stmt (done_label);
5593 : :
5594 : 12180 : if (update_call)
5595 : 7054 : add_stmt (update_call);
5596 : :
5597 : : /* Finish the compound statement. */
5598 : 12180 : compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
5599 : :
5600 : : /* NEWVAL is the value that was successfully stored, return a
5601 : : COMPOUND_EXPR of the statement and the appropriate value. */
5602 : 23787 : return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
5603 : 12180 : return_old_p ? old : newval);
5604 : : }
5605 : :
5606 : : /* Construct and perhaps optimize a tree representation
5607 : : for a unary operation. CODE, a tree_code, specifies the operation
5608 : : and XARG is the operand.
5609 : : For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
5610 : : promotions (such as from short to int).
5611 : : For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
5612 : : non-lvalues; this is only used to handle conversion of non-lvalue arrays
5613 : : to pointers in C99.
5614 : :
5615 : : LOCATION is the location of the operator. */
5616 : :
5617 : : tree
5618 : 59017107 : build_unary_op (location_t location, enum tree_code code, tree xarg,
5619 : : bool noconvert)
5620 : : {
5621 : : /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5622 : 59017107 : tree arg = xarg;
5623 : 59017107 : tree argtype = NULL_TREE;
5624 : 59017107 : enum tree_code typecode;
5625 : 59017107 : tree val;
5626 : 59017107 : tree ret = error_mark_node;
5627 : 59017107 : tree eptype = NULL_TREE;
5628 : 59017107 : const char *invalid_op_diag;
5629 : 59017107 : bool int_operands;
5630 : :
5631 : 59017107 : int_operands = EXPR_INT_CONST_OPERANDS (xarg);
5632 : 6290857 : if (int_operands)
5633 : 6290857 : arg = remove_c_maybe_const_expr (arg);
5634 : :
5635 : 59017107 : if (code != ADDR_EXPR)
5636 : 8326175 : arg = require_complete_type (location, arg);
5637 : :
5638 : 59017107 : typecode = TREE_CODE (TREE_TYPE (arg));
5639 : 59017107 : if (typecode == ERROR_MARK)
5640 : 74 : return error_mark_node;
5641 : 59017033 : if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
5642 : 31508 : typecode = INTEGER_TYPE;
5643 : :
5644 : 118034066 : if ((invalid_op_diag
5645 : 59017033 : = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
5646 : : {
5647 : 0 : error_at (location, invalid_op_diag);
5648 : 0 : return error_mark_node;
5649 : : }
5650 : :
5651 : 59017033 : if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
5652 : : {
5653 : 235 : eptype = TREE_TYPE (arg);
5654 : 235 : arg = TREE_OPERAND (arg, 0);
5655 : : }
5656 : :
5657 : 59017033 : switch (code)
5658 : : {
5659 : 28531 : case CONVERT_EXPR:
5660 : : /* This is used for unary plus, because a CONVERT_EXPR
5661 : : is enough to prevent anybody from looking inside for
5662 : : associativity, but won't generate any code. */
5663 : 28536 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5664 : 8 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5665 : 5 : || typecode == BITINT_TYPE
5666 : 5 : || gnu_vector_type_p (TREE_TYPE (arg))))
5667 : : {
5668 : 1 : error_at (location, "wrong type argument to unary plus");
5669 : 1 : return error_mark_node;
5670 : : }
5671 : 28530 : else if (!noconvert)
5672 : 28530 : arg = default_conversion (arg);
5673 : 28530 : arg = non_lvalue_loc (location, arg);
5674 : 28530 : break;
5675 : :
5676 : 6518171 : case NEGATE_EXPR:
5677 : 6954514 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5678 : 472846 : || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
5679 : 441579 : || typecode == BITINT_TYPE
5680 : 436343 : || gnu_vector_type_p (TREE_TYPE (arg))))
5681 : : {
5682 : 1 : error_at (location, "wrong type argument to unary minus");
5683 : 1 : return error_mark_node;
5684 : : }
5685 : 6518170 : else if (!noconvert)
5686 : 6518164 : arg = default_conversion (arg);
5687 : : break;
5688 : :
5689 : 288958 : case BIT_NOT_EXPR:
5690 : : /* ~ works on integer types and non float vectors. */
5691 : 288958 : if (typecode == INTEGER_TYPE
5692 : 288958 : || typecode == BITINT_TYPE
5693 : 288958 : || (gnu_vector_type_p (TREE_TYPE (arg))
5694 : 2057 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
5695 : : {
5696 : : tree e = arg;
5697 : :
5698 : : /* Warn if the expression has boolean value. */
5699 : 288359 : while (TREE_CODE (e) == COMPOUND_EXPR)
5700 : 11 : e = TREE_OPERAND (e, 1);
5701 : :
5702 : 576682 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
5703 : 576682 : || truth_value_p (TREE_CODE (e))))
5704 : : {
5705 : 146 : auto_diagnostic_group d;
5706 : 146 : if (warning_at (location, OPT_Wbool_operation,
5707 : : "%<~%> on a boolean expression"))
5708 : : {
5709 : 12 : gcc_rich_location richloc (location);
5710 : 12 : richloc.add_fixit_insert_before (location, "!");
5711 : 12 : inform (&richloc, "did you mean to use logical not?");
5712 : 12 : }
5713 : 146 : }
5714 : 288348 : if (!noconvert)
5715 : 288345 : arg = default_conversion (arg);
5716 : : }
5717 : 610 : else if (typecode == COMPLEX_TYPE)
5718 : : {
5719 : 605 : code = CONJ_EXPR;
5720 : 605 : pedwarn (location, OPT_Wpedantic,
5721 : : "ISO C does not support %<~%> for complex conjugation");
5722 : 605 : if (!noconvert)
5723 : 605 : arg = default_conversion (arg);
5724 : : }
5725 : : else
5726 : : {
5727 : 5 : error_at (location, "wrong type argument to bit-complement");
5728 : 5 : return error_mark_node;
5729 : : }
5730 : : break;
5731 : :
5732 : 3 : case ABS_EXPR:
5733 : 3 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
5734 : : {
5735 : 0 : error_at (location, "wrong type argument to abs");
5736 : 0 : return error_mark_node;
5737 : : }
5738 : 3 : else if (!noconvert)
5739 : 0 : arg = default_conversion (arg);
5740 : : break;
5741 : :
5742 : 7 : case ABSU_EXPR:
5743 : 7 : if (!(typecode == INTEGER_TYPE))
5744 : : {
5745 : 0 : error_at (location, "wrong type argument to absu");
5746 : 0 : return error_mark_node;
5747 : : }
5748 : 7 : else if (!noconvert)
5749 : 0 : arg = default_conversion (arg);
5750 : : break;
5751 : :
5752 : 0 : case CONJ_EXPR:
5753 : : /* Conjugating a real value is a no-op, but allow it anyway. */
5754 : 0 : if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
5755 : : || typecode == COMPLEX_TYPE))
5756 : : {
5757 : 0 : error_at (location, "wrong type argument to conjugation");
5758 : 0 : return error_mark_node;
5759 : : }
5760 : 0 : else if (!noconvert)
5761 : 0 : arg = default_conversion (arg);
5762 : : break;
5763 : :
5764 : 374272 : case TRUTH_NOT_EXPR:
5765 : 374272 : if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
5766 : 6996 : && typecode != REAL_TYPE && typecode != POINTER_TYPE
5767 : 21 : && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE
5768 : 18 : && typecode != BITINT_TYPE)
5769 : : {
5770 : 10 : error_at (location,
5771 : : "wrong type argument to unary exclamation mark");
5772 : 10 : return error_mark_node;
5773 : : }
5774 : 374262 : if (int_operands)
5775 : : {
5776 : 139609 : arg = c_objc_common_truthvalue_conversion (location, xarg);
5777 : 139609 : arg = remove_c_maybe_const_expr (arg);
5778 : : }
5779 : : else
5780 : 234653 : arg = c_objc_common_truthvalue_conversion (location, arg);
5781 : 374262 : ret = invert_truthvalue_loc (location, arg);
5782 : : /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
5783 : 374262 : if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
5784 : 234258 : location = EXPR_LOCATION (ret);
5785 : 374262 : goto return_build_unary_op;
5786 : :
5787 : 201159 : case REALPART_EXPR:
5788 : 201159 : case IMAGPART_EXPR:
5789 : 201159 : ret = build_real_imag_expr (location, code, arg);
5790 : 201159 : if (ret == error_mark_node)
5791 : : return error_mark_node;
5792 : 201151 : if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
5793 : 2 : eptype = TREE_TYPE (eptype);
5794 : 201151 : goto return_build_unary_op;
5795 : :
5796 : 915031 : case PREINCREMENT_EXPR:
5797 : 915031 : case POSTINCREMENT_EXPR:
5798 : 915031 : case PREDECREMENT_EXPR:
5799 : 915031 : case POSTDECREMENT_EXPR:
5800 : :
5801 : 915031 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
5802 : : {
5803 : 2 : tree inner = build_unary_op (location, code,
5804 : 2 : C_MAYBE_CONST_EXPR_EXPR (arg),
5805 : : noconvert);
5806 : 2 : if (inner == error_mark_node)
5807 : : return error_mark_node;
5808 : 4 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5809 : 2 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
5810 : 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
5811 : 2 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
5812 : 2 : goto return_build_unary_op;
5813 : : }
5814 : :
5815 : : /* Complain about anything that is not a true lvalue. In
5816 : : Objective-C, skip this check for property_refs. */
5817 : 915029 : if (!objc_is_property_ref (arg)
5818 : 1830058 : && !lvalue_or_else (location,
5819 : 915029 : arg, ((code == PREINCREMENT_EXPR
5820 : 915029 : || code == POSTINCREMENT_EXPR)
5821 : : ? lv_increment
5822 : : : lv_decrement)))
5823 : 22 : return error_mark_node;
5824 : :
5825 : 915007 : if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
5826 : : {
5827 : 4 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5828 : 2 : warning_at (location, OPT_Wc___compat,
5829 : : "increment of enumeration value is invalid in C++");
5830 : : else
5831 : 2 : warning_at (location, OPT_Wc___compat,
5832 : : "decrement of enumeration value is invalid in C++");
5833 : : }
5834 : :
5835 : 915007 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
5836 : : {
5837 : 656 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5838 : 328 : warning_at (location, OPT_Wbool_operation,
5839 : : "increment of a boolean expression");
5840 : : else
5841 : 328 : warning_at (location, OPT_Wbool_operation,
5842 : : "decrement of a boolean expression");
5843 : : }
5844 : :
5845 : : /* Ensure the argument is fully folded inside any SAVE_EXPR. */
5846 : 915007 : arg = c_fully_fold (arg, false, NULL, true);
5847 : :
5848 : 915007 : bool atomic_op;
5849 : 915007 : atomic_op = really_atomic_lvalue (arg);
5850 : :
5851 : : /* Increment or decrement the real part of the value,
5852 : : and don't change the imaginary part. */
5853 : 915007 : if (typecode == COMPLEX_TYPE)
5854 : : {
5855 : 57 : tree real, imag;
5856 : :
5857 : 57 : pedwarn_c23 (location, OPT_Wpedantic,
5858 : : "ISO C does not support %<++%> and %<--%> on complex "
5859 : : "types before C2Y");
5860 : :
5861 : 57 : if (!atomic_op)
5862 : : {
5863 : 53 : arg = stabilize_reference (arg);
5864 : 53 : real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
5865 : : true);
5866 : 53 : imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
5867 : : true);
5868 : 53 : real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
5869 : 53 : if (real == error_mark_node || imag == error_mark_node)
5870 : : return error_mark_node;
5871 : 53 : ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5872 : : real, imag);
5873 : 53 : goto return_build_unary_op;
5874 : : }
5875 : : }
5876 : :
5877 : : /* Report invalid types. */
5878 : :
5879 : 914954 : if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
5880 : 685952 : && typecode != INTEGER_TYPE && typecode != REAL_TYPE
5881 : 151 : && typecode != COMPLEX_TYPE && typecode != BITINT_TYPE
5882 : 915003 : && !gnu_vector_type_p (TREE_TYPE (arg)))
5883 : : {
5884 : 5 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5885 : 3 : error_at (location, "wrong type argument to increment");
5886 : : else
5887 : 2 : error_at (location, "wrong type argument to decrement");
5888 : :
5889 : 5 : return error_mark_node;
5890 : : }
5891 : :
5892 : 914949 : {
5893 : 914949 : tree inc;
5894 : :
5895 : 914949 : argtype = TREE_TYPE (arg);
5896 : :
5897 : : /* Compute the increment. */
5898 : :
5899 : 914949 : if (typecode == POINTER_TYPE)
5900 : : {
5901 : : /* If pointer target is an incomplete type,
5902 : : we just cannot know how to do the arithmetic. */
5903 : 229002 : if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
5904 : : {
5905 : 27 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5906 : 17 : error_at (location,
5907 : : "increment of pointer to an incomplete type %qT",
5908 : 17 : TREE_TYPE (argtype));
5909 : : else
5910 : 10 : error_at (location,
5911 : : "decrement of pointer to an incomplete type %qT",
5912 : 10 : TREE_TYPE (argtype));
5913 : : }
5914 : 228975 : else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
5915 : 228975 : || VOID_TYPE_P (TREE_TYPE (argtype)))
5916 : : {
5917 : 10 : if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5918 : 6 : pedwarn (location, OPT_Wpointer_arith,
5919 : : "wrong type argument to increment");
5920 : : else
5921 : 4 : pedwarn (location, OPT_Wpointer_arith,
5922 : : "wrong type argument to decrement");
5923 : : }
5924 : : else
5925 : 457930 : verify_type_context (location, TCTX_POINTER_ARITH,
5926 : 228965 : TREE_TYPE (argtype));
5927 : :
5928 : 229002 : inc = c_size_in_bytes (TREE_TYPE (argtype));
5929 : 229002 : inc = convert_to_ptrofftype_loc (location, inc);
5930 : : }
5931 : 685947 : else if (FRACT_MODE_P (TYPE_MODE (argtype)))
5932 : : {
5933 : : /* For signed fract types, we invert ++ to -- or
5934 : : -- to ++, and change inc from 1 to -1, because
5935 : : it is not possible to represent 1 in signed fract constants.
5936 : : For unsigned fract types, the result always overflows and
5937 : : we get an undefined (original) or the maximum value. */
5938 : 0 : if (code == PREINCREMENT_EXPR)
5939 : : code = PREDECREMENT_EXPR;
5940 : : else if (code == PREDECREMENT_EXPR)
5941 : : code = PREINCREMENT_EXPR;
5942 : : else if (code == POSTINCREMENT_EXPR)
5943 : : code = POSTDECREMENT_EXPR;
5944 : : else /* code == POSTDECREMENT_EXPR */
5945 : 0 : code = POSTINCREMENT_EXPR;
5946 : :
5947 : 0 : inc = integer_minus_one_node;
5948 : 0 : inc = convert (argtype, inc);
5949 : : }
5950 : : else
5951 : : {
5952 : 685903 : inc = VECTOR_TYPE_P (argtype)
5953 : 685947 : ? build_one_cst (argtype)
5954 : : : integer_one_node;
5955 : 685947 : inc = convert (argtype, inc);
5956 : : }
5957 : :
5958 : : /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5959 : : need to ask Objective-C to build the increment or decrement
5960 : : expression for it. */
5961 : 914949 : if (objc_is_property_ref (arg))
5962 : 0 : return objc_build_incr_expr_for_property_ref (location, code,
5963 : 20 : arg, inc);
5964 : :
5965 : : /* Report a read-only lvalue. */
5966 : 914949 : if (TYPE_READONLY (argtype))
5967 : : {
5968 : 20 : readonly_error (location, arg,
5969 : 20 : ((code == PREINCREMENT_EXPR
5970 : 20 : || code == POSTINCREMENT_EXPR)
5971 : : ? lv_increment : lv_decrement));
5972 : 20 : return error_mark_node;
5973 : : }
5974 : 914929 : else if (TREE_READONLY (arg))
5975 : 4 : readonly_warning (arg,
5976 : 4 : ((code == PREINCREMENT_EXPR
5977 : 4 : || code == POSTINCREMENT_EXPR)
5978 : : ? lv_increment : lv_decrement));
5979 : :
5980 : : /* If the argument is atomic, use the special code sequences for
5981 : : atomic compound assignment. */
5982 : 914929 : if (atomic_op)
5983 : : {
5984 : 4469 : arg = stabilize_reference (arg);
5985 : 8938 : ret = build_atomic_assign (location, arg,
5986 : 4469 : ((code == PREINCREMENT_EXPR
5987 : 4469 : || code == POSTINCREMENT_EXPR)
5988 : : ? PLUS_EXPR
5989 : : : MINUS_EXPR),
5990 : 4469 : (FRACT_MODE_P (TYPE_MODE (argtype))
5991 : : ? inc
5992 : : : integer_one_node),
5993 : : (code == POSTINCREMENT_EXPR
5994 : 4469 : || code == POSTDECREMENT_EXPR));
5995 : 914929 : goto return_build_unary_op;
5996 : : }
5997 : :
5998 : 910460 : tree true_res;
5999 : 910460 : if (c_hardbool_type_attr (TREE_TYPE (arg), NULL, &true_res))
6000 : : {
6001 : 364 : tree larg = stabilize_reference (arg);
6002 : 364 : tree sarg = save_expr (larg);
6003 : 364 : switch (code)
6004 : : {
6005 : 91 : case PREINCREMENT_EXPR:
6006 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6007 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6008 : 91 : break;
6009 : 91 : case POSTINCREMENT_EXPR:
6010 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, true_res);
6011 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6012 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6013 : 91 : break;
6014 : 91 : case PREDECREMENT_EXPR:
6015 : 91 : {
6016 : 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6017 : : true, true);
6018 : 91 : rarg = invert_truthvalue_loc (location, rarg);
6019 : 91 : rarg = convert (TREE_TYPE (sarg), rarg);
6020 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, rarg);
6021 : : }
6022 : 91 : break;
6023 : 91 : case POSTDECREMENT_EXPR:
6024 : 91 : {
6025 : 91 : tree rarg = convert_lvalue_to_rvalue (location, sarg,
6026 : : true, true);
6027 : 91 : rarg = invert_truthvalue_loc (location, rarg);
6028 : 91 : tree iarg = convert (TREE_TYPE (larg), rarg);
6029 : 91 : val = build2 (MODIFY_EXPR, TREE_TYPE (larg), larg, iarg);
6030 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), val, sarg);
6031 : 91 : val = build2 (COMPOUND_EXPR, TREE_TYPE (larg), sarg, val);
6032 : : }
6033 : 91 : break;
6034 : : default:
6035 : : gcc_unreachable ();
6036 : : }
6037 : 364 : TREE_SIDE_EFFECTS (val) = 1;
6038 : : }
6039 : 910096 : else if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
6040 : 56 : val = boolean_increment (code, arg);
6041 : : else
6042 : 910040 : val = build2 (code, TREE_TYPE (arg), arg, inc);
6043 : 910460 : TREE_SIDE_EFFECTS (val) = 1;
6044 : 910460 : if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
6045 : 3933 : TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
6046 : : TYPE_UNQUALIFIED);
6047 : 910460 : ret = val;
6048 : 910460 : goto return_build_unary_op;
6049 : : }
6050 : :
6051 : 50690891 : case ADDR_EXPR:
6052 : : /* Note that this operation never does default_conversion. */
6053 : :
6054 : : /* The operand of unary '&' must be an lvalue (which excludes
6055 : : expressions of type void), or, in C99, the result of a [] or
6056 : : unary '*' operator. */
6057 : 50690891 : if (VOID_TYPE_P (TREE_TYPE (arg))
6058 : 25 : && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
6059 : 50690910 : && (!INDIRECT_REF_P (arg) || !flag_isoc99))
6060 : 18 : pedwarn (location, 0, "taking address of expression of type %<void%>");
6061 : :
6062 : : /* Let &* cancel out to simplify resulting code. */
6063 : 50690891 : if (INDIRECT_REF_P (arg))
6064 : : {
6065 : : /* Don't let this be an lvalue. */
6066 : 21935 : if (lvalue_p (TREE_OPERAND (arg, 0)))
6067 : 16558 : return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
6068 : 5377 : ret = TREE_OPERAND (arg, 0);
6069 : 5377 : goto return_build_unary_op;
6070 : : }
6071 : :
6072 : : /* Anything not already handled and not a true memory reference
6073 : : or a non-lvalue array is an error. */
6074 : 50668956 : if (typecode != FUNCTION_TYPE && !noconvert
6075 : 50668956 : && !lvalue_or_else (location, arg, lv_addressof))
6076 : 17 : return error_mark_node;
6077 : :
6078 : : /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
6079 : : folding later. */
6080 : 50668939 : if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
6081 : : {
6082 : 1 : tree inner = build_unary_op (location, code,
6083 : 1 : C_MAYBE_CONST_EXPR_EXPR (arg),
6084 : : noconvert);
6085 : 2 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6086 : 1 : C_MAYBE_CONST_EXPR_PRE (arg), inner);
6087 : 1 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
6088 : 1 : C_MAYBE_CONST_EXPR_NON_CONST (ret)
6089 : 1 : = C_MAYBE_CONST_EXPR_NON_CONST (arg);
6090 : 1 : goto return_build_unary_op;
6091 : : }
6092 : :
6093 : : /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to
6094 : : .ACCESS_WITH_SIZE. */
6095 : 50668938 : if (is_access_with_size_p (arg))
6096 : 0 : arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0);
6097 : :
6098 : 50668938 : argtype = TREE_TYPE (arg);
6099 : :
6100 : : /* If the lvalue is const or volatile, merge that into the type
6101 : : to which the address will point. This is only needed
6102 : : for function types. */
6103 : 50668938 : if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
6104 : 50183639 : && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
6105 : 79970935 : && TREE_CODE (argtype) == FUNCTION_TYPE)
6106 : : {
6107 : 29222275 : int orig_quals = TYPE_QUALS (strip_array_types (argtype));
6108 : 29222275 : int quals = orig_quals;
6109 : :
6110 : 29222275 : if (TREE_READONLY (arg))
6111 : 28850873 : quals |= TYPE_QUAL_CONST;
6112 : 29222275 : if (TREE_THIS_VOLATILE (arg))
6113 : 372237 : quals |= TYPE_QUAL_VOLATILE;
6114 : :
6115 : 29222275 : argtype = c_build_qualified_type (argtype, quals);
6116 : : }
6117 : :
6118 : 50668938 : switch (TREE_CODE (arg))
6119 : : {
6120 : 63553 : case COMPONENT_REF:
6121 : 63553 : if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
6122 : : {
6123 : 8 : error_at (location, "cannot take address of bit-field %qD",
6124 : 8 : TREE_OPERAND (arg, 1));
6125 : 8 : return error_mark_node;
6126 : : }
6127 : :
6128 : : /* fall through */
6129 : :
6130 : 137664 : case ARRAY_REF:
6131 : 137664 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
6132 : : {
6133 : 78 : if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
6134 : 7 : && !POINTER_TYPE_P (TREE_TYPE (arg))
6135 : 75 : && !VECTOR_TYPE_P (TREE_TYPE (arg)))
6136 : : {
6137 : 6 : error_at (location, "cannot take address of scalar with "
6138 : : "reverse storage order");
6139 : 6 : return error_mark_node;
6140 : : }
6141 : :
6142 : 63 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
6143 : 63 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
6144 : 58 : warning_at (location, OPT_Wscalar_storage_order,
6145 : : "address of array with reverse scalar storage "
6146 : : "order requested");
6147 : : }
6148 : :
6149 : 50668924 : default:
6150 : 50668924 : break;
6151 : : }
6152 : :
6153 : 50668924 : if (!c_mark_addressable (arg))
6154 : 18 : return error_mark_node;
6155 : :
6156 : 50668906 : gcc_assert (TREE_CODE (arg) != COMPONENT_REF
6157 : : || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
6158 : :
6159 : 50668906 : argtype = c_build_pointer_type (argtype);
6160 : :
6161 : : /* ??? Cope with user tricks that amount to offsetof. Delete this
6162 : : when we have proper support for integer constant expressions. */
6163 : 50668906 : val = get_base_address (arg);
6164 : 50668906 : if (val && INDIRECT_REF_P (val)
6165 : 50691548 : && TREE_CONSTANT (TREE_OPERAND (val, 0)))
6166 : : {
6167 : 538 : ret = fold_offsetof (arg, argtype);
6168 : 538 : goto return_build_unary_op;
6169 : : }
6170 : :
6171 : 50668368 : val = build1 (ADDR_EXPR, argtype, arg);
6172 : :
6173 : 50668368 : ret = val;
6174 : 50668368 : goto return_build_unary_op;
6175 : :
6176 : 10 : case PAREN_EXPR:
6177 : 10 : ret = build1 (code, TREE_TYPE (arg), arg);
6178 : 10 : goto return_build_unary_op;
6179 : :
6180 : 0 : default:
6181 : 0 : gcc_unreachable ();
6182 : : }
6183 : :
6184 : 6835663 : if (argtype == NULL_TREE)
6185 : 6835663 : argtype = TREE_TYPE (arg);
6186 : 6835663 : if (TREE_CODE (arg) == INTEGER_CST)
6187 : 6147289 : ret = (require_constant_value
6188 : 6147289 : ? fold_build1_initializer_loc (location, code, argtype, arg)
6189 : 6128990 : : fold_build1_loc (location, code, argtype, arg));
6190 : : else
6191 : 688374 : ret = build1 (code, argtype, arg);
6192 : 59000354 : return_build_unary_op:
6193 : 59000354 : gcc_assert (ret != error_mark_node);
6194 : 6291333 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
6195 : 65291678 : && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
6196 : 562 : ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
6197 : 58999792 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
6198 : 74 : ret = note_integer_operands (ret);
6199 : 59000354 : if (eptype)
6200 : 235 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
6201 : 59000354 : protected_set_expr_location (ret, location);
6202 : 59000354 : return ret;
6203 : : }
6204 : :
6205 : : /* Return nonzero if REF is an lvalue valid for this language.
6206 : : Lvalues can be assigned, unless their type has TYPE_READONLY.
6207 : : Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
6208 : :
6209 : : bool
6210 : 174747524 : lvalue_p (const_tree ref)
6211 : : {
6212 : 176227342 : const enum tree_code code = TREE_CODE (ref);
6213 : :
6214 : 176227342 : switch (code)
6215 : : {
6216 : 1455908 : case REALPART_EXPR:
6217 : 1455908 : case IMAGPART_EXPR:
6218 : 1455908 : case COMPONENT_REF:
6219 : 1455908 : return lvalue_p (TREE_OPERAND (ref, 0));
6220 : :
6221 : 23910 : case C_MAYBE_CONST_EXPR:
6222 : 23910 : return lvalue_p (TREE_OPERAND (ref, 1));
6223 : :
6224 : : case COMPOUND_LITERAL_EXPR:
6225 : : case STRING_CST:
6226 : : return true;
6227 : :
6228 : 25263560 : case MEM_REF:
6229 : 25263560 : case TARGET_MEM_REF:
6230 : : /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
6231 : : here as well. */
6232 : 25263560 : case INDIRECT_REF:
6233 : 25263560 : case ARRAY_REF:
6234 : 25263560 : case VAR_DECL:
6235 : 25263560 : case PARM_DECL:
6236 : 25263560 : case RESULT_DECL:
6237 : 25263560 : case ERROR_MARK:
6238 : 25263560 : return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
6239 : 25263560 : && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
6240 : :
6241 : 6 : case BIND_EXPR:
6242 : 6 : return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
6243 : :
6244 : 6994243 : case CALL_EXPR:
6245 : 6994243 : return is_access_with_size_p (ref);
6246 : :
6247 : : default:
6248 : : return false;
6249 : : }
6250 : : }
6251 : :
6252 : : /* Give a warning for storing in something that is read-only in GCC
6253 : : terms but not const in ISO C terms. */
6254 : :
6255 : : static void
6256 : 5 : readonly_warning (tree arg, enum lvalue_use use)
6257 : : {
6258 : 5 : switch (use)
6259 : : {
6260 : 1 : case lv_assign:
6261 : 1 : warning (0, "assignment of read-only location %qE", arg);
6262 : 1 : break;
6263 : 2 : case lv_increment:
6264 : 2 : warning (0, "increment of read-only location %qE", arg);
6265 : 2 : break;
6266 : 2 : case lv_decrement:
6267 : 2 : warning (0, "decrement of read-only location %qE", arg);
6268 : 2 : break;
6269 : 0 : default:
6270 : 0 : gcc_unreachable ();
6271 : : }
6272 : 5 : return;
6273 : : }
6274 : :
6275 : :
6276 : : /* Return nonzero if REF is an lvalue valid for this language;
6277 : : otherwise, print an error message and return zero. USE says
6278 : : how the lvalue is being used and so selects the error message.
6279 : : LOCATION is the location at which any error should be reported. */
6280 : :
6281 : : static int
6282 : 4629256 : lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
6283 : : {
6284 : 4629256 : int win = lvalue_p (ref);
6285 : :
6286 : 4629256 : if (!win)
6287 : 74 : lvalue_error (loc, use);
6288 : :
6289 : 4629256 : return win;
6290 : : }
6291 : :
6292 : : /* Mark EXP saying that we need to be able to take the
6293 : : address of it; it should not be allocated in a register.
6294 : : Returns true if successful. ARRAY_REF_P is true if this
6295 : : is for ARRAY_REF construction - in that case we don't want
6296 : : to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6297 : : it is fine to use ARRAY_REFs for vector subscripts on vector
6298 : : register variables. If OVERRIDE_REGISTER, clear DECL_REGISTER rather
6299 : : than producing an error for taking the address of a register. */
6300 : :
6301 : : bool
6302 : 51202838 : c_mark_addressable (tree exp, bool array_ref_p, bool override_register)
6303 : : {
6304 : 51202838 : tree x = exp;
6305 : :
6306 : 51771962 : while (1)
6307 : 51771962 : switch (TREE_CODE (x))
6308 : : {
6309 : 9949 : case VIEW_CONVERT_EXPR:
6310 : 9949 : if (array_ref_p
6311 : 9841 : && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6312 : 19790 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6313 : : return true;
6314 : 108 : x = TREE_OPERAND (x, 0);
6315 : 108 : break;
6316 : :
6317 : 385106 : case COMPONENT_REF:
6318 : 385106 : if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
6319 : : {
6320 : 6 : error ("cannot take address of bit-field %qD",
6321 : 3 : TREE_OPERAND (x, 1));
6322 : 3 : return false;
6323 : : }
6324 : : /* FALLTHRU */
6325 : 569016 : case ADDR_EXPR:
6326 : 569016 : case ARRAY_REF:
6327 : 569016 : case REALPART_EXPR:
6328 : 569016 : case IMAGPART_EXPR:
6329 : 569016 : x = TREE_OPERAND (x, 0);
6330 : 569016 : break;
6331 : :
6332 : 550 : case COMPOUND_LITERAL_EXPR:
6333 : 550 : if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
6334 : : {
6335 : 14 : if (override_register)
6336 : 12 : DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)) = 0;
6337 : : else
6338 : : {
6339 : 2 : error ("address of register compound literal requested");
6340 : 2 : return false;
6341 : : }
6342 : : }
6343 : 548 : TREE_ADDRESSABLE (x) = 1;
6344 : 548 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
6345 : 548 : return true;
6346 : :
6347 : 0 : case CONSTRUCTOR:
6348 : 0 : TREE_ADDRESSABLE (x) = 1;
6349 : 0 : return true;
6350 : :
6351 : 1305166 : case VAR_DECL:
6352 : 1305166 : case CONST_DECL:
6353 : 1305166 : case PARM_DECL:
6354 : 1305166 : case RESULT_DECL:
6355 : 1305166 : if (C_DECL_REGISTER (x)
6356 : 1305166 : && DECL_NONLOCAL (x))
6357 : : {
6358 : 0 : if (TREE_PUBLIC (x) || is_global_var (x))
6359 : : {
6360 : 0 : error
6361 : 0 : ("global register variable %qD used in nested function", x);
6362 : 0 : return false;
6363 : : }
6364 : 0 : pedwarn (input_location, 0, "register variable %qD used in nested function", x);
6365 : : }
6366 : 1305166 : else if (C_DECL_REGISTER (x))
6367 : : {
6368 : 73 : if (TREE_PUBLIC (x) || is_global_var (x))
6369 : 4 : error ("address of global register variable %qD requested", x);
6370 : 123 : else if (override_register && !DECL_HARD_REGISTER (x))
6371 : : {
6372 : 54 : DECL_REGISTER (x) = 0;
6373 : 54 : TREE_ADDRESSABLE (x) = 1;
6374 : 54 : mark_decl_used (x, true);
6375 : 54 : return true;
6376 : : }
6377 : : else
6378 : 15 : error ("address of register variable %qD requested", x);
6379 : 19 : return false;
6380 : : }
6381 : :
6382 : : /* FALLTHRU */
6383 : 50633683 : case FUNCTION_DECL:
6384 : 50633683 : TREE_ADDRESSABLE (x) = 1;
6385 : 50633683 : mark_decl_used (x, true);
6386 : : /* FALLTHRU */
6387 : : default:
6388 : : return true;
6389 : : }
6390 : : }
6391 : :
6392 : : /* Convert EXPR to TYPE, warning about conversion problems with
6393 : : constants. SEMANTIC_TYPE is the type this conversion would use
6394 : : without excess precision. If SEMANTIC_TYPE is NULL, this function
6395 : : is equivalent to convert_and_check. This function is a wrapper that
6396 : : handles conversions that may be different than
6397 : : the usual ones because of excess precision. */
6398 : :
6399 : : static tree
6400 : 23081442 : ep_convert_and_check (location_t loc, tree type, tree expr,
6401 : : tree semantic_type)
6402 : : {
6403 : 23081442 : if (TREE_TYPE (expr) == type)
6404 : : return expr;
6405 : :
6406 : : /* For C11, integer conversions may have results with excess
6407 : : precision. */
6408 : 2025804 : if (flag_isoc11 || !semantic_type)
6409 : 2025800 : return convert_and_check (loc, type, expr);
6410 : :
6411 : 4 : if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
6412 : 4 : && TREE_TYPE (expr) != semantic_type)
6413 : : {
6414 : : /* For integers, we need to check the real conversion, not
6415 : : the conversion to the excess precision type. */
6416 : 4 : expr = convert_and_check (loc, semantic_type, expr);
6417 : : }
6418 : : /* Result type is the excess precision type, which should be
6419 : : large enough, so do not check. */
6420 : 4 : return convert (type, expr);
6421 : : }
6422 : :
6423 : : /* If EXPR refers to a built-in declared without a prototype returns
6424 : : the actual type of the built-in and, if non-null, set *BLTIN to
6425 : : a pointer to the built-in. Otherwise return the type of EXPR
6426 : : and clear *BLTIN if non-null. */
6427 : :
6428 : : static tree
6429 : 2791833 : type_or_builtin_type (tree expr, tree *bltin = NULL)
6430 : : {
6431 : 2791833 : tree dummy;
6432 : 2791833 : if (!bltin)
6433 : 0 : bltin = &dummy;
6434 : :
6435 : 2791833 : *bltin = NULL_TREE;
6436 : :
6437 : 2791833 : tree type = TREE_TYPE (expr);
6438 : 2791833 : if (TREE_CODE (expr) != ADDR_EXPR)
6439 : : return type;
6440 : :
6441 : 207443 : tree oper = TREE_OPERAND (expr, 0);
6442 : 207443 : if (!DECL_P (oper)
6443 : 169318 : || TREE_CODE (oper) != FUNCTION_DECL
6444 : 243830 : || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
6445 : : return type;
6446 : :
6447 : 2084 : built_in_function code = DECL_FUNCTION_CODE (oper);
6448 : 2084 : if (!C_DECL_BUILTIN_PROTOTYPE (oper))
6449 : : return type;
6450 : :
6451 : 1977 : if ((*bltin = builtin_decl_implicit (code)))
6452 : 991 : type = c_build_pointer_type (TREE_TYPE (*bltin));
6453 : :
6454 : : return type;
6455 : : }
6456 : :
6457 : : /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
6458 : : IFEXP_BCP then the condition is a call to __builtin_constant_p, and
6459 : : if folded to an integer constant then the unselected half may
6460 : : contain arbitrary operations not normally permitted in constant
6461 : : expressions. Set the location of the expression to LOC. */
6462 : :
6463 : : tree
6464 : 403135 : build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6465 : : tree op1, tree op1_original_type, location_t op1_loc,
6466 : : tree op2, tree op2_original_type, location_t op2_loc)
6467 : : {
6468 : 403135 : tree type1;
6469 : 403135 : tree type2;
6470 : 403135 : enum tree_code code1;
6471 : 403135 : enum tree_code code2;
6472 : 403135 : tree result_type = NULL;
6473 : 403135 : tree semantic_result_type = NULL;
6474 : 403135 : tree orig_op1 = op1, orig_op2 = op2;
6475 : 403135 : bool int_const, op1_int_operands, op2_int_operands, int_operands;
6476 : 403135 : bool ifexp_int_operands;
6477 : 403135 : tree ret;
6478 : :
6479 : 403135 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
6480 : 115763 : if (op1_int_operands)
6481 : 115763 : op1 = remove_c_maybe_const_expr (op1);
6482 : 403135 : op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
6483 : 148643 : if (op2_int_operands)
6484 : 148643 : op2 = remove_c_maybe_const_expr (op2);
6485 : 403135 : ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
6486 : 242154 : if (ifexp_int_operands)
6487 : 242154 : ifexp = remove_c_maybe_const_expr (ifexp);
6488 : :
6489 : : /* Promote both alternatives. */
6490 : :
6491 : 403135 : if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
6492 : 399241 : op1 = default_conversion (op1);
6493 : 403135 : if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
6494 : 373318 : op2 = default_conversion (op2);
6495 : :
6496 : 403135 : if (TREE_CODE (ifexp) == ERROR_MARK
6497 : 403066 : || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
6498 : 806187 : || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
6499 : 83 : return error_mark_node;
6500 : :
6501 : 403052 : tree bltin1 = NULL_TREE;
6502 : 403052 : tree bltin2 = NULL_TREE;
6503 : 403052 : type1 = type_or_builtin_type (op1, &bltin1);
6504 : 403052 : code1 = TREE_CODE (type1);
6505 : 403052 : type2 = type_or_builtin_type (op2, &bltin2);
6506 : 403052 : code2 = TREE_CODE (type2);
6507 : :
6508 : 403052 : if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
6509 : 1 : return error_mark_node;
6510 : :
6511 : 403051 : if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
6512 : 1 : return error_mark_node;
6513 : :
6514 : : /* C90 does not permit non-lvalue arrays in conditional expressions.
6515 : : In C99 they will be pointers by now. */
6516 : 403050 : if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
6517 : : {
6518 : 1 : error_at (colon_loc, "non-lvalue array in conditional expression");
6519 : 1 : return error_mark_node;
6520 : : }
6521 : :
6522 : 403049 : if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
6523 : 403043 : || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6524 : 7 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6525 : 0 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6526 : 7 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6527 : 0 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6528 : : {
6529 : 7 : semantic_result_type = c_common_type (type1, type2);
6530 : 7 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
6531 : : {
6532 : 6 : op1 = TREE_OPERAND (op1, 0);
6533 : 6 : type1 = TREE_TYPE (op1);
6534 : 6 : gcc_assert (TREE_CODE (type1) == code1);
6535 : : }
6536 : 7 : if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
6537 : : {
6538 : 1 : op2 = TREE_OPERAND (op2, 0);
6539 : 1 : type2 = TREE_TYPE (op2);
6540 : 1 : gcc_assert (TREE_CODE (type2) == code2);
6541 : : }
6542 : : }
6543 : :
6544 : 403049 : if (warn_cxx_compat)
6545 : : {
6546 : 418 : tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
6547 : 418 : tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
6548 : :
6549 : 418 : if (TREE_CODE (t1) == ENUMERAL_TYPE
6550 : 6 : && TREE_CODE (t2) == ENUMERAL_TYPE
6551 : 424 : && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
6552 : 2 : warning_at (colon_loc, OPT_Wc___compat,
6553 : : "different enum types in conditional is "
6554 : : "invalid in C++: %qT vs %qT", t1, t2);
6555 : : }
6556 : :
6557 : 403049 : if (warn_zero_as_null_pointer_constant
6558 : 14 : && c_inhibit_evaluation_warnings == 0)
6559 : : {
6560 : 14 : if ((code1 == POINTER_TYPE || code1 == NULLPTR_TYPE)
6561 : 14 : && code2 == INTEGER_TYPE && null_pointer_constant_p (orig_op2))
6562 : 2 : warning_at (op2_loc, OPT_Wzero_as_null_pointer_constant,
6563 : : "zero as null pointer constant");
6564 : :
6565 : 14 : if ((code2 == POINTER_TYPE || code2 == NULLPTR_TYPE)
6566 : 14 : && code1 == INTEGER_TYPE && null_pointer_constant_p (orig_op1))
6567 : 2 : warning_at (op1_loc, OPT_Wzero_as_null_pointer_constant,
6568 : : "zero as null pointer constant");
6569 : : }
6570 : :
6571 : : /* Quickly detect the usual case where op1 and op2 have the same type
6572 : : after promotion. */
6573 : 403049 : if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
6574 : : {
6575 : 269995 : if (type1 == type2)
6576 : : result_type = type1;
6577 : : else
6578 : 6255 : result_type = TYPE_MAIN_VARIANT (type1);
6579 : : }
6580 : 133054 : else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
6581 : 79859 : || code1 == COMPLEX_TYPE || code1 == BITINT_TYPE)
6582 : 53240 : && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
6583 : 26148 : || code2 == COMPLEX_TYPE || code2 == BITINT_TYPE))
6584 : : {
6585 : : /* In C11, a conditional expression between a floating-point
6586 : : type and an integer type should convert the integer type to
6587 : : the evaluation format of the floating-point type, with
6588 : : possible excess precision. */
6589 : 27191 : tree eptype1 = type1;
6590 : 27191 : tree eptype2 = type2;
6591 : 27191 : if (flag_isoc11)
6592 : : {
6593 : 26879 : tree eptype;
6594 : 10834 : if (ANY_INTEGRAL_TYPE_P (type1)
6595 : 26879 : && (eptype = excess_precision_type (type2)) != NULL_TREE)
6596 : : {
6597 : 2 : eptype2 = eptype;
6598 : 2 : if (!semantic_result_type)
6599 : 2 : semantic_result_type = c_common_type (type1, type2);
6600 : : }
6601 : 369 : else if (ANY_INTEGRAL_TYPE_P (type2)
6602 : 26877 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
6603 : : {
6604 : 4618 : eptype1 = eptype;
6605 : 4618 : if (!semantic_result_type)
6606 : 4618 : semantic_result_type = c_common_type (type1, type2);
6607 : : }
6608 : : }
6609 : 27191 : result_type = c_common_type (eptype1, eptype2);
6610 : 27191 : if (result_type == error_mark_node)
6611 : : return error_mark_node;
6612 : 27189 : do_warn_double_promotion (result_type, type1, type2,
6613 : : "implicit conversion from %qT to %qT to "
6614 : : "match other result of conditional",
6615 : : colon_loc);
6616 : :
6617 : : /* If -Wsign-compare, warn here if type1 and type2 have
6618 : : different signedness. We'll promote the signed to unsigned
6619 : : and later code won't know it used to be different.
6620 : : Do this check on the original types, so that explicit casts
6621 : : will be considered, but default promotions won't. */
6622 : 27189 : if (c_inhibit_evaluation_warnings == 0)
6623 : : {
6624 : 26998 : int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
6625 : 26998 : int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
6626 : :
6627 : 26998 : if (unsigned_op1 ^ unsigned_op2)
6628 : : {
6629 : 12589 : bool ovf;
6630 : :
6631 : : /* Do not warn if the result type is signed, since the
6632 : : signed type will only be chosen if it can represent
6633 : : all the values of the unsigned type. */
6634 : 12589 : if (!TYPE_UNSIGNED (result_type))
6635 : : /* OK */;
6636 : : else
6637 : : {
6638 : 12485 : bool op1_maybe_const = true;
6639 : 12485 : bool op2_maybe_const = true;
6640 : :
6641 : : /* Do not warn if the signed quantity is an
6642 : : unsuffixed integer literal (or some static
6643 : : constant expression involving such literals) and
6644 : : it is non-negative. This warning requires the
6645 : : operands to be folded for best results, so do
6646 : : that folding in this case even without
6647 : : warn_sign_compare to avoid warning options
6648 : : possibly affecting code generation. */
6649 : 12485 : c_inhibit_evaluation_warnings
6650 : 12485 : += (ifexp == truthvalue_false_node);
6651 : 12485 : op1 = c_fully_fold (op1, require_constant_value,
6652 : : &op1_maybe_const);
6653 : 12485 : c_inhibit_evaluation_warnings
6654 : 12485 : -= (ifexp == truthvalue_false_node);
6655 : :
6656 : 12485 : c_inhibit_evaluation_warnings
6657 : 12485 : += (ifexp == truthvalue_true_node);
6658 : 12485 : op2 = c_fully_fold (op2, require_constant_value,
6659 : : &op2_maybe_const);
6660 : 12485 : c_inhibit_evaluation_warnings
6661 : 12485 : -= (ifexp == truthvalue_true_node);
6662 : :
6663 : 12485 : if (warn_sign_compare)
6664 : : {
6665 : 1241 : if ((unsigned_op2
6666 : 681 : && tree_expr_nonnegative_warnv_p (op1, &ovf))
6667 : 1244 : || (unsigned_op1
6668 : 560 : && tree_expr_nonnegative_warnv_p (op2, &ovf)))
6669 : : /* OK */;
6670 : 10 : else if (unsigned_op2)
6671 : 3 : warning_at (op1_loc, OPT_Wsign_compare,
6672 : : "operand of %<?:%> changes signedness from "
6673 : : "%qT to %qT due to unsignedness of other "
6674 : 3 : "operand", TREE_TYPE (orig_op1),
6675 : 3 : TREE_TYPE (orig_op2));
6676 : : else
6677 : 7 : warning_at (op2_loc, OPT_Wsign_compare,
6678 : : "operand of %<?:%> changes signedness from "
6679 : : "%qT to %qT due to unsignedness of other "
6680 : 7 : "operand", TREE_TYPE (orig_op2),
6681 : 7 : TREE_TYPE (orig_op1));
6682 : : }
6683 : 12485 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
6684 : 7145 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
6685 : 12485 : if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
6686 : 4375 : op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
6687 : : }
6688 : : }
6689 : : }
6690 : : }
6691 : 105863 : else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
6692 : : {
6693 : 25995 : if (code1 != VOID_TYPE || code2 != VOID_TYPE)
6694 : 25995 : pedwarn (colon_loc, OPT_Wpedantic,
6695 : : "ISO C forbids conditional expr with only one void side");
6696 : 25995 : result_type = void_type_node;
6697 : : }
6698 : 79868 : else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
6699 : : {
6700 : 79477 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
6701 : 79477 : addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
6702 : 79477 : addr_space_t as_common;
6703 : :
6704 : 79477 : if (comp_target_types (colon_loc, type1, type2))
6705 : : {
6706 : 2600 : ifexp = save_expr (ifexp);
6707 : 2600 : result_type = common_pointer_type (type1, type2, ifexp);
6708 : : }
6709 : 76877 : else if (null_pointer_constant_p (orig_op1))
6710 : : result_type = type2;
6711 : 72715 : else if (null_pointer_constant_p (orig_op2))
6712 : : result_type = type1;
6713 : 34799 : else if (!addr_space_superset (as1, as2, &as_common))
6714 : : {
6715 : 0 : error_at (colon_loc, "pointers to disjoint address spaces "
6716 : : "used in conditional expression");
6717 : 0 : return error_mark_node;
6718 : : }
6719 : 34799 : else if ((VOID_TYPE_P (TREE_TYPE (type1))
6720 : 326 : && !TYPE_ATOMIC (TREE_TYPE (type1)))
6721 : 34801 : || (VOID_TYPE_P (TREE_TYPE (type2))
6722 : 34428 : && !TYPE_ATOMIC (TREE_TYPE (type2))))
6723 : : {
6724 : 34751 : tree t1 = TREE_TYPE (type1);
6725 : 34751 : tree t2 = TREE_TYPE (type2);
6726 : 34751 : if (!(VOID_TYPE_P (t1)
6727 : 325 : && !TYPE_ATOMIC (t1)))
6728 : : {
6729 : : /* roles are swapped */
6730 : 34427 : t1 = t2;
6731 : 34427 : t2 = TREE_TYPE (type1);
6732 : : }
6733 : 34751 : tree t2_stripped = strip_array_types (t2);
6734 : 34751 : if ((TREE_CODE (t2) == ARRAY_TYPE)
6735 : 34751 : && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
6736 : : {
6737 : 18 : if (!flag_isoc23)
6738 : 6 : warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
6739 : : "pointer to array loses qualifier "
6740 : : "in conditional expression");
6741 : 12 : else if (warn_c11_c23_compat > 0)
6742 : 6 : warning_at (colon_loc, OPT_Wc11_c23_compat,
6743 : : "pointer to array loses qualifier "
6744 : : "in conditional expression in ISO C before C23");
6745 : : }
6746 : 34751 : if (TREE_CODE (t2) == FUNCTION_TYPE)
6747 : 4 : pedwarn (colon_loc, OPT_Wpedantic,
6748 : : "ISO C forbids conditional expr between "
6749 : : "%<void *%> and function pointer");
6750 : : /* for array, use qualifiers of element type */
6751 : 34751 : if (flag_isoc23)
6752 : 11002 : t2 = t2_stripped;
6753 : 34751 : result_type = c_build_pointer_type (qualify_type (t1, t2));
6754 : : }
6755 : : /* Objective-C pointer comparisons are a bit more lenient. */
6756 : 48 : else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
6757 : 0 : result_type = objc_common_type (type1, type2);
6758 : : else
6759 : : {
6760 : 48 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
6761 : 48 : enum diagnostics::kind kind = diagnostics::kind::permerror;
6762 : 48 : if (!flag_isoc99)
6763 : : /* This downgrade to a warning ensures that -std=gnu89
6764 : : -pedantic-errors does not flag these mismatches between
6765 : : builtins as errors (as diagnostics::kind::permerror would)
6766 : : ISO C99 and later do not have implicit function declarations,
6767 : : so the mismatch cannot occur naturally there. */
6768 : 5 : kind = (bltin1 && bltin2
6769 : 5 : ? diagnostics::kind::warning
6770 : : : diagnostics::kind::pedwarn);
6771 : 48 : if (emit_diagnostic (kind, colon_loc, OPT_Wincompatible_pointer_types,
6772 : : "pointer type mismatch "
6773 : : "in conditional expression"))
6774 : : {
6775 : 46 : inform (op1_loc, "first expression has type %qT", type1);
6776 : 46 : inform (op2_loc, "second expression has type %qT", type2);
6777 : : }
6778 : 48 : result_type = c_build_pointer_type
6779 : 48 : (c_build_qualified_type (void_type_node, qual));
6780 : : }
6781 : : }
6782 : 391 : else if (code1 == POINTER_TYPE
6783 : 274 : && (code2 == INTEGER_TYPE || code2 == BITINT_TYPE))
6784 : : {
6785 : 270 : if (!null_pointer_constant_p (orig_op2))
6786 : 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6787 : : "pointer/integer type mismatch "
6788 : : "in conditional expression");
6789 : : else
6790 : : {
6791 : 256 : op2 = null_pointer_node;
6792 : : }
6793 : : result_type = type1;
6794 : : }
6795 : 121 : else if (code2 == POINTER_TYPE
6796 : 87 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6797 : : {
6798 : 83 : if (!null_pointer_constant_p (orig_op1))
6799 : 14 : permerror_opt (colon_loc, OPT_Wint_conversion,
6800 : : "pointer/integer type mismatch "
6801 : : "in conditional expression");
6802 : : else
6803 : : {
6804 : 69 : op1 = null_pointer_node;
6805 : : }
6806 : : result_type = type2;
6807 : : }
6808 : : /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
6809 : : type nullptr_t and the other is a pointer, the result type is the pointer
6810 : : type." */
6811 : 38 : else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
6812 : : result_type = type2;
6813 : 34 : else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
6814 : : result_type = type1;
6815 : 8 : else if (RECORD_OR_UNION_TYPE_P (type1) && RECORD_OR_UNION_TYPE_P (type2)
6816 : 38 : && comptypes (TYPE_MAIN_VARIANT (type1),
6817 : 8 : TYPE_MAIN_VARIANT (type2)))
6818 : 8 : result_type = composite_type (TYPE_MAIN_VARIANT (type1),
6819 : 8 : TYPE_MAIN_VARIANT (type2));
6820 : :
6821 : 403025 : if (!result_type)
6822 : : {
6823 : 22 : if (flag_cond_mismatch)
6824 : 0 : result_type = void_type_node;
6825 : : else
6826 : : {
6827 : 22 : error_at (colon_loc, "type mismatch in conditional expression");
6828 : 22 : return error_mark_node;
6829 : : }
6830 : : }
6831 : :
6832 : : /* Merge const and volatile flags of the incoming types. */
6833 : 403025 : result_type
6834 : 403025 : = build_type_variant (result_type,
6835 : : TYPE_READONLY (type1) || TYPE_READONLY (type2),
6836 : : TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6837 : :
6838 : 403025 : op1 = ep_convert_and_check (colon_loc, result_type, op1,
6839 : : semantic_result_type);
6840 : 403025 : op2 = ep_convert_and_check (colon_loc, result_type, op2,
6841 : : semantic_result_type);
6842 : :
6843 : 403025 : if (ifexp_bcp && ifexp == truthvalue_true_node)
6844 : : {
6845 : 10 : op2_int_operands = true;
6846 : 10 : op1 = c_fully_fold (op1, require_constant_value, NULL);
6847 : : }
6848 : 59 : if (ifexp_bcp && ifexp == truthvalue_false_node)
6849 : : {
6850 : 49 : op1_int_operands = true;
6851 : 49 : op2 = c_fully_fold (op2, require_constant_value, NULL);
6852 : : }
6853 : 903119 : int_const = int_operands = (ifexp_int_operands
6854 : 403025 : && op1_int_operands
6855 : 403025 : && op2_int_operands);
6856 : 97069 : if (int_operands)
6857 : : {
6858 : 97069 : int_const = ((ifexp == truthvalue_true_node
6859 : 58647 : && TREE_CODE (orig_op1) == INTEGER_CST
6860 : 58633 : && !TREE_OVERFLOW (orig_op1))
6861 : 97083 : || (ifexp == truthvalue_false_node
6862 : 38376 : && TREE_CODE (orig_op2) == INTEGER_CST
6863 : 38360 : && !TREE_OVERFLOW (orig_op2)));
6864 : : }
6865 : :
6866 : : /* Need to convert condition operand into a vector mask. */
6867 : 403025 : if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
6868 : : {
6869 : 0 : tree vectype = TREE_TYPE (ifexp);
6870 : 0 : tree elem_type = TREE_TYPE (vectype);
6871 : 0 : tree zero = build_int_cst (elem_type, 0);
6872 : 0 : tree zero_vec = build_vector_from_val (vectype, zero);
6873 : 0 : tree cmp_type = truth_type_for (vectype);
6874 : 0 : ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
6875 : : }
6876 : :
6877 : 403025 : if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
6878 : 97025 : ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
6879 : : else
6880 : : {
6881 : 306000 : if (int_operands)
6882 : : {
6883 : : /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
6884 : : nested inside of the expression. */
6885 : 76 : op1 = c_fully_fold (op1, false, NULL);
6886 : 76 : op2 = c_fully_fold (op2, false, NULL);
6887 : : }
6888 : 306000 : ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
6889 : 306000 : if (int_operands)
6890 : 76 : ret = note_integer_operands (ret);
6891 : : }
6892 : 403025 : if (semantic_result_type)
6893 : 4627 : ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
6894 : :
6895 : 403025 : protected_set_expr_location (ret, colon_loc);
6896 : :
6897 : : /* If the OP1 and OP2 are the same and don't have side-effects,
6898 : : warn here, because the COND_EXPR will be turned into OP1. */
6899 : 403025 : if (warn_duplicated_branches
6900 : 34 : && TREE_CODE (ret) == COND_EXPR
6901 : 403059 : && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
6902 : 13 : warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
6903 : : "this condition has identical branches");
6904 : :
6905 : : return ret;
6906 : : }
6907 : :
6908 : : /* EXPR is an expression, location LOC, whose result is discarded.
6909 : : Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
6910 : : whose right-hand operand is such a call, possibly recursively). */
6911 : :
6912 : : static void
6913 : 6357068 : maybe_warn_nodiscard (location_t loc, tree expr)
6914 : : {
6915 : 6357068 : if (VOID_TYPE_P (TREE_TYPE (expr)))
6916 : : return;
6917 : 3883294 : while (TREE_CODE (expr) == COMPOUND_EXPR)
6918 : : {
6919 : 49037 : expr = TREE_OPERAND (expr, 1);
6920 : 49037 : if (EXPR_HAS_LOCATION (expr))
6921 : 31944 : loc = EXPR_LOCATION (expr);
6922 : : }
6923 : 3834257 : if (TREE_CODE (expr) != CALL_EXPR)
6924 : : return;
6925 : 342963 : tree fn = CALL_EXPR_FN (expr);
6926 : 342963 : if (!fn)
6927 : : return;
6928 : 342950 : tree attr;
6929 : 342950 : if (TREE_CODE (fn) == ADDR_EXPR
6930 : 342285 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6931 : 685235 : && (attr = lookup_attribute ("nodiscard",
6932 : 342285 : DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
6933 : : {
6934 : 3 : fn = TREE_OPERAND (fn, 0);
6935 : 3 : tree args = TREE_VALUE (attr);
6936 : 3 : if (args)
6937 : 1 : args = TREE_VALUE (args);
6938 : 3 : auto_diagnostic_group d;
6939 : 3 : auto_urlify_attributes sentinel;
6940 : 3 : int warned;
6941 : 3 : if (args)
6942 : 1 : warned = warning_at (loc, OPT_Wunused_result,
6943 : : "ignoring return value of %qD, declared with "
6944 : : "attribute %<nodiscard%>: %E", fn, args);
6945 : : else
6946 : 2 : warned = warning_at (loc, OPT_Wunused_result,
6947 : : "ignoring return value of %qD, declared with "
6948 : : "attribute %<nodiscard%>", fn);
6949 : 3 : if (warned)
6950 : 3 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
6951 : 3 : }
6952 : : else
6953 : : {
6954 : 342947 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
6955 : 342947 : attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
6956 : 342947 : if (!attr)
6957 : 342935 : return;
6958 : 12 : tree args = TREE_VALUE (attr);
6959 : 12 : if (args)
6960 : 5 : args = TREE_VALUE (args);
6961 : 12 : auto_diagnostic_group d;
6962 : 12 : auto_urlify_attributes sentinel;
6963 : 12 : int warned;
6964 : 12 : if (args)
6965 : 5 : warned = warning_at (loc, OPT_Wunused_result,
6966 : : "ignoring return value of type %qT, declared "
6967 : : "with attribute %<nodiscard%>: %E",
6968 : : rettype, args);
6969 : : else
6970 : 7 : warned = warning_at (loc, OPT_Wunused_result,
6971 : : "ignoring return value of type %qT, declared "
6972 : : "with attribute %<nodiscard%>", rettype);
6973 : 12 : if (warned)
6974 : : {
6975 : 12 : if (TREE_CODE (fn) == ADDR_EXPR)
6976 : : {
6977 : 11 : fn = TREE_OPERAND (fn, 0);
6978 : 11 : if (TREE_CODE (fn) == FUNCTION_DECL)
6979 : 11 : inform (DECL_SOURCE_LOCATION (fn),
6980 : : "in call to %qD, declared here", fn);
6981 : : }
6982 : : }
6983 : 12 : }
6984 : : }
6985 : :
6986 : : /* Return a compound expression that performs two expressions and
6987 : : returns the value of the second of them.
6988 : :
6989 : : LOC is the location of the COMPOUND_EXPR. */
6990 : :
6991 : : tree
6992 : 111212 : build_compound_expr (location_t loc, tree expr1, tree expr2)
6993 : : {
6994 : 111212 : bool expr1_int_operands, expr2_int_operands;
6995 : 111212 : tree eptype = NULL_TREE;
6996 : 111212 : tree ret;
6997 : :
6998 : 111212 : expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
6999 : 325 : if (expr1_int_operands)
7000 : 325 : expr1 = remove_c_maybe_const_expr (expr1);
7001 : 111212 : expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
7002 : 66079 : if (expr2_int_operands)
7003 : 66079 : expr2 = remove_c_maybe_const_expr (expr2);
7004 : :
7005 : 111212 : if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
7006 : 0 : expr1 = TREE_OPERAND (expr1, 0);
7007 : 111212 : if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
7008 : : {
7009 : 1 : eptype = TREE_TYPE (expr2);
7010 : 1 : expr2 = TREE_OPERAND (expr2, 0);
7011 : : }
7012 : :
7013 : 111212 : if (!TREE_SIDE_EFFECTS (expr1))
7014 : : {
7015 : : /* The left-hand operand of a comma expression is like an expression
7016 : : statement: with -Wunused, we should warn if it doesn't have
7017 : : any side-effects, unless it was explicitly cast to (void). */
7018 : 8295 : if (warn_unused_value)
7019 : : {
7020 : 1241 : if (VOID_TYPE_P (TREE_TYPE (expr1))
7021 : 1241 : && CONVERT_EXPR_P (expr1))
7022 : : ; /* (void) a, b */
7023 : 14 : else if (VOID_TYPE_P (TREE_TYPE (expr1))
7024 : 4 : && TREE_CODE (expr1) == COMPOUND_EXPR
7025 : 17 : && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
7026 : : ; /* (void) a, (void) b, c */
7027 : : else
7028 : 11 : warning_at (loc, OPT_Wunused_value,
7029 : : "left-hand operand of comma expression has no effect");
7030 : : }
7031 : : }
7032 : 102917 : else if (TREE_CODE (expr1) == COMPOUND_EXPR
7033 : 13104 : && warn_unused_value)
7034 : : {
7035 : : tree r = expr1;
7036 : : location_t cloc = loc;
7037 : 4833 : while (TREE_CODE (r) == COMPOUND_EXPR)
7038 : : {
7039 : 2417 : if (EXPR_HAS_LOCATION (r))
7040 : 2417 : cloc = EXPR_LOCATION (r);
7041 : 2417 : r = TREE_OPERAND (r, 1);
7042 : : }
7043 : 2416 : if (!TREE_SIDE_EFFECTS (r)
7044 : 4 : && !VOID_TYPE_P (TREE_TYPE (r))
7045 : 2420 : && !CONVERT_EXPR_P (r))
7046 : 4 : warning_at (cloc, OPT_Wunused_value,
7047 : : "right-hand operand of comma expression has no effect");
7048 : : }
7049 : :
7050 : : /* With -Wunused, we should also warn if the left-hand operand does have
7051 : : side-effects, but computes a value which is not used. For example, in
7052 : : `foo() + bar(), baz()' the result of the `+' operator is not used,
7053 : : so we should issue a warning. */
7054 : 100501 : else if (warn_unused_value)
7055 : 35380 : warn_if_unused_value (expr1, loc);
7056 : :
7057 : 111212 : maybe_warn_nodiscard (loc, expr1);
7058 : :
7059 : 111212 : if (expr2 == error_mark_node)
7060 : : return error_mark_node;
7061 : :
7062 : 111199 : ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
7063 : :
7064 : 111199 : if (flag_isoc99
7065 : : && expr1_int_operands
7066 : 110743 : && expr2_int_operands)
7067 : 152 : ret = note_integer_operands (ret);
7068 : :
7069 : 111199 : if (eptype)
7070 : 1 : ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
7071 : :
7072 : 111199 : protected_set_expr_location (ret, loc);
7073 : 111199 : return ret;
7074 : : }
7075 : :
7076 : : /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
7077 : : which we are casting. OTYPE is the type of the expression being
7078 : : cast. Both TYPE and OTYPE are pointer types. LOC is the location
7079 : : of the cast. -Wcast-qual appeared on the command line. Named
7080 : : address space qualifiers are not handled here, because they result
7081 : : in different warnings. */
7082 : :
7083 : : static void
7084 : 13405 : handle_warn_cast_qual (location_t loc, tree type, tree otype)
7085 : : {
7086 : 13405 : tree in_type = type;
7087 : 13405 : tree in_otype = otype;
7088 : 13405 : int added = 0;
7089 : 13405 : int discarded = 0;
7090 : 13575 : bool is_const;
7091 : :
7092 : : /* Check that the qualifiers on IN_TYPE are a superset of the
7093 : : qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
7094 : : nodes is uninteresting and we stop as soon as we hit a
7095 : : non-POINTER_TYPE node on either type. */
7096 : 13575 : do
7097 : : {
7098 : 13575 : in_otype = TREE_TYPE (in_otype);
7099 : 13575 : in_type = TREE_TYPE (in_type);
7100 : :
7101 : : /* GNU C allows cv-qualified function types. 'const' means the
7102 : : function is very pure, 'volatile' means it can't return. We
7103 : : need to warn when such qualifiers are added, not when they're
7104 : : taken away. */
7105 : 13575 : if (TREE_CODE (in_otype) == FUNCTION_TYPE
7106 : 26 : && TREE_CODE (in_type) == FUNCTION_TYPE)
7107 : 18 : added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
7108 : 18 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
7109 : : else
7110 : 13557 : discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
7111 : 13557 : & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
7112 : : }
7113 : 13575 : while (TREE_CODE (in_type) == POINTER_TYPE
7114 : 13575 : && TREE_CODE (in_otype) == POINTER_TYPE);
7115 : :
7116 : 13405 : if (added)
7117 : 2 : warning_at (loc, OPT_Wcast_qual,
7118 : : "cast adds %q#v qualifier to function type", added);
7119 : :
7120 : 13405 : if (discarded)
7121 : : /* There are qualifiers present in IN_OTYPE that are not present
7122 : : in IN_TYPE. */
7123 : 1117 : warning_at (loc, OPT_Wcast_qual,
7124 : : "cast discards %qv qualifier from pointer target type",
7125 : : discarded);
7126 : :
7127 : 13405 : if (added || discarded)
7128 : : return;
7129 : :
7130 : : /* A cast from **T to const **T is unsafe, because it can cause a
7131 : : const value to be changed with no additional warning. We only
7132 : : issue this warning if T is the same on both sides, and we only
7133 : : issue the warning if there are the same number of pointers on
7134 : : both sides, as otherwise the cast is clearly unsafe anyhow. A
7135 : : cast is unsafe when a qualifier is added at one level and const
7136 : : is not present at all outer levels.
7137 : :
7138 : : To issue this warning, we check at each level whether the cast
7139 : : adds new qualifiers not already seen. We don't need to special
7140 : : case function types, as they won't have the same
7141 : : TYPE_MAIN_VARIANT. */
7142 : :
7143 : 12286 : if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
7144 : : return;
7145 : 51 : if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
7146 : : return;
7147 : :
7148 : 48 : in_type = type;
7149 : 48 : in_otype = otype;
7150 : 48 : is_const = TYPE_READONLY (TREE_TYPE (in_type));
7151 : 130 : do
7152 : : {
7153 : 130 : in_type = TREE_TYPE (in_type);
7154 : 130 : in_otype = TREE_TYPE (in_otype);
7155 : 130 : if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
7156 : 130 : && !is_const)
7157 : : {
7158 : 27 : warning_at (loc, OPT_Wcast_qual,
7159 : : "to be safe all intermediate pointers in cast from "
7160 : : "%qT to %qT must be %<const%> qualified",
7161 : : otype, type);
7162 : 27 : break;
7163 : : }
7164 : 103 : if (is_const)
7165 : 72 : is_const = TYPE_READONLY (in_type);
7166 : : }
7167 : 103 : while (TREE_CODE (in_type) == POINTER_TYPE);
7168 : : }
7169 : :
7170 : : /* Heuristic check if two parameter types can be considered ABI-equivalent. */
7171 : :
7172 : : static bool
7173 : 1081 : c_safe_arg_type_equiv_p (tree t1, tree t2)
7174 : : {
7175 : 1081 : if (error_operand_p (t1) || error_operand_p (t2))
7176 : : return true;
7177 : :
7178 : 1080 : t1 = TYPE_MAIN_VARIANT (t1);
7179 : 1080 : t2 = TYPE_MAIN_VARIANT (t2);
7180 : :
7181 : 1080 : if (TREE_CODE (t1) == POINTER_TYPE
7182 : 114 : && TREE_CODE (t2) == POINTER_TYPE)
7183 : : return true;
7184 : :
7185 : : /* Only the precision of the parameter matters. This check should
7186 : : make sure that the callee does not see undefined values in argument
7187 : : registers. */
7188 : 988 : if (INTEGRAL_TYPE_P (t1)
7189 : 869 : && INTEGRAL_TYPE_P (t2)
7190 : 1289 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7191 : : return true;
7192 : :
7193 : 867 : return comptypes (t1, t2);
7194 : : }
7195 : :
7196 : : /* Check if a type cast between two function types can be considered safe. */
7197 : :
7198 : : static bool
7199 : 7434 : c_safe_function_type_cast_p (tree t1, tree t2)
7200 : : {
7201 : 7434 : if (TREE_TYPE (t1) == void_type_node &&
7202 : 5451 : TYPE_ARG_TYPES (t1) == void_list_node)
7203 : : return true;
7204 : :
7205 : 3679 : if (TREE_TYPE (t2) == void_type_node &&
7206 : 2862 : TYPE_ARG_TYPES (t2) == void_list_node)
7207 : : return true;
7208 : :
7209 : 849 : if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
7210 : : return false;
7211 : :
7212 : 153 : for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
7213 : 281 : t1 && t2;
7214 : 128 : t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
7215 : 232 : if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
7216 : : return false;
7217 : :
7218 : : return true;
7219 : : }
7220 : :
7221 : : /* Build an expression representing a cast to type TYPE of expression EXPR.
7222 : : LOC is the location of the cast-- typically the open paren of the cast. */
7223 : :
7224 : : tree
7225 : 117051813 : build_c_cast (location_t loc, tree type, tree expr)
7226 : : {
7227 : 117051813 : tree value;
7228 : :
7229 : 117051813 : bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
7230 : :
7231 : 117051813 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
7232 : 66 : expr = TREE_OPERAND (expr, 0);
7233 : :
7234 : 117051813 : value = expr;
7235 : 117051813 : if (int_operands)
7236 : 6705825 : value = remove_c_maybe_const_expr (value);
7237 : :
7238 : 117051813 : if (type == error_mark_node || expr == error_mark_node)
7239 : : return error_mark_node;
7240 : :
7241 : : /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
7242 : : only in <protocol> qualifications. But when constructing cast expressions,
7243 : : the protocols do matter and must be kept around. */
7244 : 117050790 : if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
7245 : 0 : return build1 (NOP_EXPR, type, expr);
7246 : :
7247 : 117050790 : type = TYPE_MAIN_VARIANT (type);
7248 : :
7249 : 117050790 : if (TREE_CODE (type) == ARRAY_TYPE)
7250 : : {
7251 : 13 : error_at (loc, "cast specifies array type");
7252 : 13 : return error_mark_node;
7253 : : }
7254 : :
7255 : 117050777 : if (TREE_CODE (type) == FUNCTION_TYPE)
7256 : : {
7257 : 6 : error_at (loc, "cast specifies function type");
7258 : 6 : return error_mark_node;
7259 : : }
7260 : :
7261 : 117050771 : if (!VOID_TYPE_P (type))
7262 : : {
7263 : 117006725 : value = require_complete_type (loc, value);
7264 : 117006725 : if (value == error_mark_node)
7265 : : return error_mark_node;
7266 : : }
7267 : :
7268 : 117050770 : if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
7269 : : {
7270 : 17617852 : if (RECORD_OR_UNION_TYPE_P (type)
7271 : 17617852 : && pedwarn (loc, OPT_Wpedantic,
7272 : : "ISO C forbids casting nonscalar to the same type"))
7273 : : ;
7274 : 17617848 : else if (warn_useless_cast)
7275 : 7 : warning_at (loc, OPT_Wuseless_cast,
7276 : : "useless cast to type %qT", type);
7277 : :
7278 : : /* Convert to remove any qualifiers from VALUE's type. */
7279 : 17617852 : value = convert (type, value);
7280 : : }
7281 : 99432918 : else if (TREE_CODE (type) == UNION_TYPE)
7282 : : {
7283 : 130 : tree field;
7284 : :
7285 : 185 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7286 : 178 : if (TREE_TYPE (field) != error_mark_node
7287 : 355 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
7288 : 177 : TYPE_MAIN_VARIANT (TREE_TYPE (value))))
7289 : : break;
7290 : :
7291 : 130 : if (field)
7292 : : {
7293 : 123 : tree t;
7294 : 123 : bool maybe_const = true;
7295 : :
7296 : 123 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
7297 : 123 : t = c_fully_fold (value, false, &maybe_const);
7298 : 123 : t = build_constructor_single (type, field, t);
7299 : 123 : if (!maybe_const)
7300 : 8 : t = c_wrap_maybe_const (t, true);
7301 : 123 : t = digest_init (loc, field, type, t,
7302 : : NULL_TREE, false, false, false, true, false, false);
7303 : 123 : TREE_CONSTANT (t) = TREE_CONSTANT (value);
7304 : 123 : return t;
7305 : : }
7306 : 7 : error_at (loc, "cast to union type from type not present in union");
7307 : 7 : return error_mark_node;
7308 : : }
7309 : : else
7310 : : {
7311 : 99432788 : tree otype, ovalue;
7312 : :
7313 : 99432788 : if (type == void_type_node)
7314 : : {
7315 : 18090 : tree t = build1 (CONVERT_EXPR, type, value);
7316 : 18090 : SET_EXPR_LOCATION (t, loc);
7317 : 18090 : return t;
7318 : : }
7319 : :
7320 : 99414698 : otype = TREE_TYPE (value);
7321 : :
7322 : : /* Optionally warn about potentially worrisome casts. */
7323 : 99414698 : if (warn_cast_qual
7324 : 179313 : && TREE_CODE (type) == POINTER_TYPE
7325 : 26463 : && TREE_CODE (otype) == POINTER_TYPE)
7326 : 13405 : handle_warn_cast_qual (loc, type, otype);
7327 : :
7328 : : /* Warn about conversions between pointers to disjoint
7329 : : address spaces. */
7330 : 99414698 : if (TREE_CODE (type) == POINTER_TYPE
7331 : 3279471 : && TREE_CODE (otype) == POINTER_TYPE
7332 : 102352093 : && !null_pointer_constant_p (value))
7333 : : {
7334 : 2884578 : addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7335 : 2884578 : addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
7336 : 2884578 : addr_space_t as_common;
7337 : :
7338 : 2884578 : if (!addr_space_superset (as_to, as_from, &as_common))
7339 : : {
7340 : 0 : if (ADDR_SPACE_GENERIC_P (as_from))
7341 : 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7342 : : "from disjoint generic address space pointer",
7343 : : c_addr_space_name (as_to));
7344 : :
7345 : 0 : else if (ADDR_SPACE_GENERIC_P (as_to))
7346 : 0 : warning_at (loc, 0, "cast to generic address space pointer "
7347 : : "from disjoint %qs address space pointer",
7348 : : c_addr_space_name (as_from));
7349 : :
7350 : : else
7351 : 0 : warning_at (loc, 0, "cast to %qs address space pointer "
7352 : : "from disjoint %qs address space pointer",
7353 : : c_addr_space_name (as_to),
7354 : : c_addr_space_name (as_from));
7355 : : }
7356 : :
7357 : : /* Warn of new allocations that are not big enough for the target
7358 : : type. */
7359 : 2884578 : if (warn_alloc_size && TREE_CODE (value) == CALL_EXPR)
7360 : 805 : if (tree fndecl = get_callee_fndecl (value))
7361 : 801 : if (DECL_IS_MALLOC (fndecl))
7362 : : {
7363 : 517 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
7364 : 517 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
7365 : 517 : if (alloc_size)
7366 : 159 : warn_for_alloc_size (loc, TREE_TYPE (type), value,
7367 : : alloc_size);
7368 : : }
7369 : : }
7370 : :
7371 : : /* Warn about possible alignment problems. */
7372 : 99414698 : if ((STRICT_ALIGNMENT || warn_cast_align == 2)
7373 : 7 : && TREE_CODE (type) == POINTER_TYPE
7374 : 7 : && TREE_CODE (otype) == POINTER_TYPE
7375 : 7 : && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
7376 : 7 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7377 : : /* Don't warn about opaque types, where the actual alignment
7378 : : restriction is unknown. */
7379 : 12 : && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
7380 : 5 : && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
7381 : 99414712 : && min_align_of_type (TREE_TYPE (type))
7382 : 7 : > min_align_of_type (TREE_TYPE (otype)))
7383 : 5 : warning_at (loc, OPT_Wcast_align,
7384 : : "cast increases required alignment of target type");
7385 : :
7386 : 99414698 : if ((TREE_CODE (type) == INTEGER_TYPE
7387 : 99414698 : || TREE_CODE (type) == BITINT_TYPE)
7388 : 6908788 : && TREE_CODE (otype) == POINTER_TYPE
7389 : 99439587 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
7390 : : /* Unlike conversion of integers to pointers, where the
7391 : : warning is disabled for converting constants because
7392 : : of cases such as SIG_*, warn about converting constant
7393 : : pointers to integers. In some cases it may cause unwanted
7394 : : sign extension, and a warning is appropriate. */
7395 : 1450 : warning_at (loc, OPT_Wpointer_to_int_cast,
7396 : : "cast from pointer to integer of different size");
7397 : :
7398 : 99414698 : if (TREE_CODE (value) == CALL_EXPR
7399 : 33748157 : && TREE_CODE (type) != TREE_CODE (otype))
7400 : 2282 : warning_at (loc, OPT_Wbad_function_cast,
7401 : : "cast from function call of type %qT "
7402 : : "to non-matching type %qT", otype, type);
7403 : :
7404 : 99414698 : if (TREE_CODE (type) == POINTER_TYPE
7405 : 3279471 : && (TREE_CODE (otype) == INTEGER_TYPE
7406 : 3279471 : || TREE_CODE (otype) == BITINT_TYPE)
7407 : 341990 : && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
7408 : : /* Don't warn about converting any constant. */
7409 : 99719793 : && !TREE_CONSTANT (value))
7410 : 407 : warning_at (loc,
7411 : 407 : OPT_Wint_to_pointer_cast, "cast to pointer from integer "
7412 : : "of different size");
7413 : :
7414 : 99414698 : if (warn_strict_aliasing <= 2)
7415 : 90901823 : strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
7416 : :
7417 : : /* If pedantic, warn for conversions between function and object
7418 : : pointer types, except for converting a null pointer constant
7419 : : to function pointer type. */
7420 : 99414698 : if (pedantic
7421 : 238829 : && TREE_CODE (type) == POINTER_TYPE
7422 : 144179 : && TREE_CODE (otype) == POINTER_TYPE
7423 : 2781 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7424 : 99414719 : && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7425 : 18 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7426 : : "conversion of function pointer to object pointer type");
7427 : :
7428 : 99414698 : if (pedantic
7429 : 238829 : && TREE_CODE (type) == POINTER_TYPE
7430 : 144179 : && TREE_CODE (otype) == POINTER_TYPE
7431 : 2781 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7432 : 13 : && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
7433 : 99414708 : && !null_pointer_constant_p (value))
7434 : 4 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
7435 : : "conversion of object pointer to function pointer type");
7436 : :
7437 : 99414698 : if (TREE_CODE (type) == POINTER_TYPE
7438 : 3279471 : && TREE_CODE (otype) == POINTER_TYPE
7439 : 2937395 : && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7440 : 8496 : && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
7441 : 99422132 : && !c_safe_function_type_cast_p (TREE_TYPE (type),
7442 : 7434 : TREE_TYPE (otype)))
7443 : 800 : warning_at (loc, OPT_Wcast_function_type,
7444 : : "cast between incompatible function types"
7445 : : " from %qT to %qT", otype, type);
7446 : :
7447 : 99414698 : ovalue = value;
7448 : : /* If converting to boolean a value with integer operands that
7449 : : is not itself represented as an INTEGER_CST, the call below
7450 : : to note_integer_operands may insert a C_MAYBE_CONST_EXPR, but
7451 : : build_binary_op as called by c_common_truthvalue_conversion
7452 : : may also insert a C_MAYBE_CONST_EXPR to indicate that a
7453 : : subexpression has been fully folded. To avoid nested
7454 : : C_MAYBE_CONST_EXPR, ensure that
7455 : : c_objc_common_truthvalue_conversion receives an argument
7456 : : properly marked as having integer operands in that case. */
7457 : 99414698 : if (int_operands
7458 : 6516801 : && TREE_CODE (value) != INTEGER_CST
7459 : 99414740 : && (TREE_CODE (type) == BOOLEAN_TYPE
7460 : 28 : || (TREE_CODE (type) == ENUMERAL_TYPE
7461 : 14 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
7462 : 14 : && TREE_CODE (ENUM_UNDERLYING_TYPE (type)) == BOOLEAN_TYPE)))
7463 : 28 : value = note_integer_operands (value);
7464 : 99414698 : value = convert (type, value);
7465 : :
7466 : : /* Ignore any integer overflow caused by the cast. */
7467 : 99414698 : if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
7468 : : {
7469 : 6463522 : if (TREE_OVERFLOW_P (ovalue))
7470 : : {
7471 : 9 : if (!TREE_OVERFLOW (value))
7472 : : {
7473 : : /* Avoid clobbering a shared constant. */
7474 : 0 : value = copy_node (value);
7475 : 0 : TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
7476 : : }
7477 : : }
7478 : 6463513 : else if (TREE_OVERFLOW (value))
7479 : : /* Reset VALUE's overflow flags, ensuring constant sharing. */
7480 : 3085 : value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
7481 : : }
7482 : : }
7483 : :
7484 : : /* Don't let a cast be an lvalue. */
7485 : 117032550 : if (lvalue_p (value))
7486 : 88549 : value = non_lvalue_loc (loc, value);
7487 : :
7488 : : /* Don't allow the results of casting to floating-point or complex
7489 : : types be confused with actual constants, or casts involving
7490 : : integer and pointer types other than direct integer-to-integer
7491 : : and integer-to-pointer be confused with integer constant
7492 : : expressions and null pointer constants. */
7493 : 117032550 : if (TREE_CODE (value) == REAL_CST
7494 : 116982772 : || TREE_CODE (value) == COMPLEX_CST
7495 : 233992699 : || (TREE_CODE (value) == INTEGER_CST
7496 : 6710380 : && !((TREE_CODE (expr) == INTEGER_CST
7497 : 6637823 : && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
7498 : 65154 : || TREE_CODE (expr) == REAL_CST
7499 : : || TREE_CODE (expr) == COMPLEX_CST)))
7500 : 133188 : value = build1 (NOP_EXPR, type, value);
7501 : :
7502 : : /* If the expression has integer operands and so can occur in an
7503 : : unevaluated part of an integer constant expression, ensure the
7504 : : return value reflects this. */
7505 : 117032550 : if (int_operands
7506 : 6692041 : && INTEGRAL_TYPE_P (type)
7507 : 6251348 : && value != error_mark_node
7508 : 123283897 : && !EXPR_INT_CONST_OPERANDS (value))
7509 : 11 : value = note_integer_operands (value);
7510 : :
7511 : 117032550 : protected_set_expr_location (value, loc);
7512 : 117032550 : return value;
7513 : : }
7514 : :
7515 : : /* Interpret a cast of expression EXPR to type TYPE. LOC is the
7516 : : location of the open paren of the cast, or the position of the cast
7517 : : expr. */
7518 : : tree
7519 : 117051320 : c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
7520 : : {
7521 : 117051320 : tree type;
7522 : 117051320 : tree type_expr = NULL_TREE;
7523 : 117051320 : bool type_expr_const = true;
7524 : 117051320 : tree ret;
7525 : 117051320 : int saved_wsp = warn_strict_prototypes;
7526 : :
7527 : : /* This avoids warnings about unprototyped casts on
7528 : : integers. E.g. "#define SIG_DFL (void(*)())0". */
7529 : 117051320 : if (TREE_CODE (expr) == INTEGER_CST)
7530 : 6758756 : warn_strict_prototypes = 0;
7531 : 117051320 : type = groktypename (type_name, &type_expr, &type_expr_const);
7532 : 117051320 : warn_strict_prototypes = saved_wsp;
7533 : :
7534 : 764577 : if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
7535 : 117815660 : && reject_gcc_builtin (expr))
7536 : 2 : return error_mark_node;
7537 : :
7538 : 117051318 : ret = build_c_cast (loc, type, expr);
7539 : 117051318 : if (ret == error_mark_node)
7540 : : return error_mark_node;
7541 : :
7542 : 117050155 : if (type_expr)
7543 : : {
7544 : 249 : bool inner_expr_const = true;
7545 : 249 : ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
7546 : 249 : ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
7547 : 498 : C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
7548 : 145 : && inner_expr_const);
7549 : 249 : SET_EXPR_LOCATION (ret, loc);
7550 : : }
7551 : :
7552 : 117050155 : if (!EXPR_HAS_LOCATION (ret))
7553 : 6638293 : protected_set_expr_location (ret, loc);
7554 : :
7555 : : /* C++ does not permits types to be defined in a cast, but it
7556 : : allows references to incomplete types. */
7557 : 117050155 : if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
7558 : 1 : warning_at (loc, OPT_Wc___compat,
7559 : : "defining a type in a cast is invalid in C++");
7560 : :
7561 : : return ret;
7562 : : }
7563 : :
7564 : : /* Build an assignment expression of lvalue LHS from value RHS.
7565 : : If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
7566 : : may differ from TREE_TYPE (LHS) for an enum bitfield.
7567 : : MODIFYCODE is the code for a binary operator that we use
7568 : : to combine the old value of LHS with RHS to get the new value.
7569 : : Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7570 : : If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
7571 : : which may differ from TREE_TYPE (RHS) for an enum value.
7572 : :
7573 : : LOCATION is the location of the MODIFYCODE operator.
7574 : : RHS_LOC is the location of the RHS. */
7575 : :
7576 : : tree
7577 : 2846087 : build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
7578 : : enum tree_code modifycode,
7579 : : location_t rhs_loc, tree rhs, tree rhs_origtype)
7580 : : {
7581 : 2846087 : tree result;
7582 : 2846087 : tree newrhs;
7583 : 2846087 : tree rhseval = NULL_TREE;
7584 : 2846087 : tree lhstype = TREE_TYPE (lhs);
7585 : 2846087 : tree olhstype = lhstype;
7586 : 2846087 : bool npc;
7587 : 2846087 : bool is_atomic_op;
7588 : :
7589 : : /* Types that aren't fully specified cannot be used in assignments. */
7590 : 2846087 : lhs = require_complete_type (location, lhs);
7591 : 2846087 : rhs = require_complete_type (location, rhs);
7592 : :
7593 : : /* Avoid duplicate error messages from operands that had errors. */
7594 : 2846087 : if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
7595 : 473 : return error_mark_node;
7596 : :
7597 : : /* Ensure an error for assigning a non-lvalue array to an array in
7598 : : C90. */
7599 : 2845614 : if (TREE_CODE (lhstype) == ARRAY_TYPE)
7600 : : {
7601 : 1 : error_at (location, "assignment to expression with array type");
7602 : 1 : return error_mark_node;
7603 : : }
7604 : :
7605 : : /* For ObjC properties, defer this check. */
7606 : 2845613 : if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
7607 : 31 : return error_mark_node;
7608 : :
7609 : 2845582 : is_atomic_op = really_atomic_lvalue (lhs);
7610 : :
7611 : 2845582 : newrhs = rhs;
7612 : :
7613 : 2845582 : if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
7614 : : {
7615 : 2 : tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
7616 : : lhs_origtype, modifycode, rhs_loc, rhs,
7617 : : rhs_origtype);
7618 : 2 : if (inner == error_mark_node)
7619 : : return error_mark_node;
7620 : 4 : result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
7621 : 2 : C_MAYBE_CONST_EXPR_PRE (lhs), inner);
7622 : 2 : gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
7623 : 2 : C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
7624 : 2 : protected_set_expr_location (result, location);
7625 : 2 : return result;
7626 : : }
7627 : :
7628 : : /* If a binary op has been requested, combine the old LHS value with the RHS
7629 : : producing the value we should actually store into the LHS. */
7630 : :
7631 : 2845580 : if (modifycode != NOP_EXPR)
7632 : : {
7633 : 211520 : lhs = c_fully_fold (lhs, false, NULL, true);
7634 : 211520 : lhs = stabilize_reference (lhs);
7635 : :
7636 : : /* Construct the RHS for any non-atomic compound assignemnt. */
7637 : 211520 : if (!is_atomic_op)
7638 : : {
7639 : : /* If in LHS op= RHS the RHS has side-effects, ensure they
7640 : : are preevaluated before the rest of the assignment expression's
7641 : : side-effects, because RHS could contain e.g. function calls
7642 : : that modify LHS. */
7643 : 196035 : if (TREE_SIDE_EFFECTS (rhs))
7644 : : {
7645 : 8620 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7646 : 28 : newrhs = save_expr (TREE_OPERAND (rhs, 0));
7647 : : else
7648 : 8592 : newrhs = save_expr (rhs);
7649 : 8620 : rhseval = newrhs;
7650 : 8620 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7651 : 28 : newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
7652 : : newrhs);
7653 : : }
7654 : 196035 : bool clear_decl_read = false;
7655 : 85823 : if ((VAR_P (lhs) || TREE_CODE (lhs) == PARM_DECL)
7656 : 151658 : && !DECL_READ_P (lhs)
7657 : 247290 : && (VAR_P (lhs) ? warn_unused_but_set_variable
7658 : : : warn_unused_but_set_parameter) > 2)
7659 : : {
7660 : 21534 : mark_exp_read (newrhs);
7661 : 21534 : if (!DECL_READ_P (lhs))
7662 : 196035 : clear_decl_read = true;
7663 : : }
7664 : :
7665 : 196035 : newrhs = build_binary_op (location, modifycode,
7666 : : convert_lvalue_to_rvalue (location, lhs,
7667 : : true, true),
7668 : : newrhs, true);
7669 : 196035 : if (clear_decl_read)
7670 : 21534 : DECL_READ_P (lhs) = 0;
7671 : :
7672 : : /* The original type of the right hand side is no longer
7673 : : meaningful. */
7674 : : rhs_origtype = NULL_TREE;
7675 : : }
7676 : : }
7677 : :
7678 : 2845580 : if (c_dialect_objc ())
7679 : : {
7680 : : /* Check if we are modifying an Objective-C property reference;
7681 : : if so, we need to generate setter calls. */
7682 : 0 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7683 : 0 : result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
7684 : : else
7685 : 0 : result = objc_maybe_build_modify_expr (lhs, newrhs);
7686 : 0 : if (result)
7687 : 0 : goto return_result;
7688 : :
7689 : : /* Else, do the check that we postponed for Objective-C. */
7690 : 0 : if (!lvalue_or_else (location, lhs, lv_assign))
7691 : 0 : return error_mark_node;
7692 : : }
7693 : :
7694 : : /* Give an error for storing in something that is 'const'. */
7695 : :
7696 : 2845580 : if (TYPE_READONLY (lhstype)
7697 : 2845580 : || (RECORD_OR_UNION_TYPE_P (lhstype)
7698 : 25529 : && C_TYPE_FIELDS_READONLY (lhstype)))
7699 : : {
7700 : 88 : readonly_error (location, lhs, lv_assign);
7701 : 88 : return error_mark_node;
7702 : : }
7703 : 2845492 : else if (TREE_READONLY (lhs))
7704 : 1 : readonly_warning (lhs, lv_assign);
7705 : :
7706 : : /* If storing into a structure or union member,
7707 : : it has probably been given type `int'.
7708 : : Compute the type that would go with
7709 : : the actual amount of storage the member occupies. */
7710 : :
7711 : 2845492 : if (TREE_CODE (lhs) == COMPONENT_REF
7712 : 243893 : && (TREE_CODE (lhstype) == INTEGER_TYPE
7713 : 243893 : || TREE_CODE (lhstype) == BOOLEAN_TYPE
7714 : 106759 : || SCALAR_FLOAT_TYPE_P (lhstype)
7715 : 84885 : || TREE_CODE (lhstype) == ENUMERAL_TYPE))
7716 : 165992 : lhstype = TREE_TYPE (get_unwidened (lhs, 0));
7717 : :
7718 : : /* If storing in a field that is in actuality a short or narrower than one,
7719 : : we must store in the field in its actual type. */
7720 : :
7721 : 2845492 : if (lhstype != TREE_TYPE (lhs))
7722 : : {
7723 : 0 : lhs = copy_node (lhs);
7724 : 0 : TREE_TYPE (lhs) = lhstype;
7725 : : }
7726 : :
7727 : : /* Issue -Wc++-compat warnings about an assignment to an enum type
7728 : : when LHS does not have its original type. This happens for,
7729 : : e.g., an enum bitfield in a struct. */
7730 : 2845492 : if (warn_cxx_compat
7731 : 3195 : && lhs_origtype != NULL_TREE
7732 : 3195 : && lhs_origtype != lhstype
7733 : 7 : && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
7734 : : {
7735 : 4 : tree checktype = (rhs_origtype != NULL_TREE
7736 : 4 : ? rhs_origtype
7737 : 0 : : TREE_TYPE (rhs));
7738 : 4 : if (checktype != error_mark_node
7739 : 4 : && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
7740 : 2 : || (is_atomic_op && modifycode != NOP_EXPR)))
7741 : 2 : warning_at (location, OPT_Wc___compat,
7742 : : "enum conversion in assignment is invalid in C++");
7743 : : }
7744 : :
7745 : : /* Remove qualifiers. */
7746 : 2845492 : lhstype = c_build_qualified_type (lhstype, TYPE_UNQUALIFIED);
7747 : 2845492 : olhstype = c_build_qualified_type (olhstype, TYPE_UNQUALIFIED);
7748 : :
7749 : : /* Convert new value to destination type. Fold it first, then
7750 : : restore any excess precision information, for the sake of
7751 : : conversion warnings. */
7752 : :
7753 : 2845492 : if (!(is_atomic_op && modifycode != NOP_EXPR))
7754 : : {
7755 : 2830007 : tree rhs_semantic_type = NULL_TREE;
7756 : 2830007 : if (!c_in_omp_for)
7757 : : {
7758 : 2813710 : if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
7759 : : {
7760 : 5069 : rhs_semantic_type = TREE_TYPE (newrhs);
7761 : 5069 : newrhs = TREE_OPERAND (newrhs, 0);
7762 : : }
7763 : 2813710 : npc = null_pointer_constant_p (newrhs);
7764 : 2813710 : newrhs = c_fully_fold (newrhs, false, NULL);
7765 : 2813710 : if (rhs_semantic_type)
7766 : 5069 : newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
7767 : : }
7768 : : else
7769 : 16297 : npc = null_pointer_constant_p (newrhs);
7770 : 2830007 : newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
7771 : : rhs_origtype, ic_assign, npc,
7772 : : NULL_TREE, NULL_TREE, 0);
7773 : 2830007 : if (TREE_CODE (newrhs) == ERROR_MARK)
7774 : 127 : return error_mark_node;
7775 : : }
7776 : :
7777 : : /* Emit ObjC write barrier, if necessary. */
7778 : 2845365 : if (c_dialect_objc () && flag_objc_gc)
7779 : : {
7780 : 0 : result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7781 : 0 : if (result)
7782 : : {
7783 : 0 : protected_set_expr_location (result, location);
7784 : 0 : goto return_result;
7785 : : }
7786 : : }
7787 : :
7788 : : /* Scan operands. */
7789 : :
7790 : 2845365 : if (is_atomic_op)
7791 : 25956 : result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
7792 : : else
7793 : : {
7794 : 2819409 : result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
7795 : 2819409 : TREE_SIDE_EFFECTS (result) = 1;
7796 : 2819409 : protected_set_expr_location (result, location);
7797 : : }
7798 : :
7799 : : /* If we got the LHS in a different type for storing in,
7800 : : convert the result back to the nominal type of LHS
7801 : : so that the value we return always has the same type
7802 : : as the LHS argument. */
7803 : :
7804 : 2845365 : if (olhstype == TREE_TYPE (result))
7805 : 2845365 : goto return_result;
7806 : :
7807 : 0 : result = convert_for_assignment (location, rhs_loc, olhstype, result,
7808 : : rhs_origtype, ic_assign, false, NULL_TREE,
7809 : : NULL_TREE, 0);
7810 : 0 : protected_set_expr_location (result, location);
7811 : :
7812 : 2845365 : return_result:
7813 : 2845365 : if (rhseval)
7814 : 8620 : result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
7815 : : return result;
7816 : : }
7817 : :
7818 : : /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
7819 : : This is used to implement -fplan9-extensions. */
7820 : :
7821 : : static bool
7822 : 10 : find_anonymous_field_with_type (tree struct_type, tree type)
7823 : : {
7824 : 10 : tree field;
7825 : 10 : bool found;
7826 : :
7827 : 10 : gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
7828 : 10 : found = false;
7829 : 10 : for (field = TYPE_FIELDS (struct_type);
7830 : 20 : field != NULL_TREE;
7831 : 10 : field = TREE_CHAIN (field))
7832 : : {
7833 : 10 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7834 : 10 : if (DECL_NAME (field) == NULL
7835 : 10 : && comptypes (type, fieldtype))
7836 : : {
7837 : 4 : if (found)
7838 : : return false;
7839 : : found = true;
7840 : : }
7841 : 6 : else if (DECL_NAME (field) == NULL
7842 : 2 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
7843 : 8 : && find_anonymous_field_with_type (TREE_TYPE (field), type))
7844 : : {
7845 : 0 : if (found)
7846 : : return false;
7847 : : found = true;
7848 : : }
7849 : : }
7850 : : return found;
7851 : : }
7852 : :
7853 : : /* RHS is an expression whose type is pointer to struct. If there is
7854 : : an anonymous field in RHS with type TYPE, then return a pointer to
7855 : : that field in RHS. This is used with -fplan9-extensions. This
7856 : : returns NULL if no conversion could be found. */
7857 : :
7858 : : static tree
7859 : 12 : convert_to_anonymous_field (location_t location, tree type, tree rhs)
7860 : : {
7861 : 12 : tree rhs_struct_type, lhs_main_type;
7862 : 12 : tree field, found_field;
7863 : 12 : bool found_sub_field;
7864 : 12 : tree ret;
7865 : :
7866 : 12 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
7867 : 12 : rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
7868 : 12 : gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
7869 : :
7870 : 12 : gcc_assert (POINTER_TYPE_P (type));
7871 : 12 : lhs_main_type = remove_qualifiers (TREE_TYPE (type));
7872 : :
7873 : 12 : found_field = NULL_TREE;
7874 : 12 : found_sub_field = false;
7875 : 12 : for (field = TYPE_FIELDS (rhs_struct_type);
7876 : 44 : field != NULL_TREE;
7877 : 32 : field = TREE_CHAIN (field))
7878 : : {
7879 : 36 : if (DECL_NAME (field) != NULL_TREE
7880 : 36 : || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
7881 : 16 : continue;
7882 : 20 : tree fieldtype = remove_qualifiers (TREE_TYPE (field));
7883 : 20 : if (comptypes (lhs_main_type, fieldtype))
7884 : : {
7885 : 12 : if (found_field != NULL_TREE)
7886 : : return NULL_TREE;
7887 : : found_field = field;
7888 : : }
7889 : 8 : else if (find_anonymous_field_with_type (TREE_TYPE (field),
7890 : : lhs_main_type))
7891 : : {
7892 : 4 : if (found_field != NULL_TREE)
7893 : : return NULL_TREE;
7894 : : found_field = field;
7895 : : found_sub_field = true;
7896 : : }
7897 : : }
7898 : :
7899 : 8 : if (found_field == NULL_TREE)
7900 : : return NULL_TREE;
7901 : :
7902 : 8 : ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
7903 : : build_fold_indirect_ref (rhs), found_field,
7904 : : NULL_TREE);
7905 : 8 : ret = build_fold_addr_expr_loc (location, ret);
7906 : :
7907 : 8 : if (found_sub_field)
7908 : : {
7909 : 2 : ret = convert_to_anonymous_field (location, type, ret);
7910 : 2 : gcc_assert (ret != NULL_TREE);
7911 : : }
7912 : :
7913 : : return ret;
7914 : : }
7915 : :
7916 : : /* Issue an error message for a bad initializer component.
7917 : : GMSGID identifies the message.
7918 : : The component name is taken from the spelling stack. */
7919 : :
7920 : : static void ATTRIBUTE_GCC_DIAG (2,0)
7921 : 943 : error_init (location_t loc, const char *gmsgid, ...)
7922 : : {
7923 : 943 : char *ofwhat;
7924 : :
7925 : 943 : auto_diagnostic_group d;
7926 : :
7927 : : /* The gmsgid may be a format string with %< and %>. */
7928 : 943 : va_list ap;
7929 : 943 : va_start (ap, gmsgid);
7930 : 943 : bool warned = emit_diagnostic_valist (diagnostics::kind::error,
7931 : 943 : loc, -1, gmsgid, &ap);
7932 : 943 : va_end (ap);
7933 : :
7934 : 943 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7935 : 943 : if (*ofwhat && warned)
7936 : 220 : inform (loc, "(near initialization for %qs)", ofwhat);
7937 : 943 : }
7938 : :
7939 : : /* Used to implement pedwarn_init and permerror_init. */
7940 : :
7941 : : static bool ATTRIBUTE_GCC_DIAG (3,0)
7942 : 2537 : pedwarn_permerror_init (location_t loc, int opt, const char *gmsgid,
7943 : : va_list *ap, enum diagnostics::kind kind)
7944 : : {
7945 : : /* Use the location where a macro was expanded rather than where
7946 : : it was defined to make sure macros defined in system headers
7947 : : but used incorrectly elsewhere are diagnosed. */
7948 : 2537 : location_t exploc = expansion_point_location_if_in_system_header (loc);
7949 : 2537 : auto_diagnostic_group d;
7950 : 2537 : bool warned = emit_diagnostic_valist (kind, exploc, opt, gmsgid, ap);
7951 : 2537 : if (warned)
7952 : : {
7953 : 1815 : char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
7954 : 1815 : if (*ofwhat)
7955 : 1494 : inform (exploc, "(near initialization for %qs)", ofwhat);
7956 : : }
7957 : 5074 : return warned;
7958 : 2537 : }
7959 : :
7960 : : /* Issue a pedantic warning for a bad initializer component. OPT is
7961 : : the option OPT_* (from options.h) controlling this warning or 0 if
7962 : : it is unconditionally given. GMSGID identifies the message. The
7963 : : component name is taken from the spelling stack. */
7964 : :
7965 : : static bool ATTRIBUTE_GCC_DIAG (3,0)
7966 : 2186 : pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
7967 : : {
7968 : 2186 : va_list ap;
7969 : 2186 : va_start (ap, gmsgid);
7970 : 2186 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
7971 : : diagnostics::kind::pedwarn);
7972 : 2186 : va_end (ap);
7973 : 2186 : return warned;
7974 : : }
7975 : :
7976 : : /* Like pedwarn_init, but issue a permerror. */
7977 : :
7978 : : static bool ATTRIBUTE_GCC_DIAG (3,0)
7979 : 351 : permerror_init (location_t loc, int opt, const char *gmsgid, ...)
7980 : : {
7981 : 351 : va_list ap;
7982 : 351 : va_start (ap, gmsgid);
7983 : 351 : bool warned = pedwarn_permerror_init (loc, opt, gmsgid, &ap,
7984 : : diagnostics::kind::permerror);
7985 : 351 : va_end (ap);
7986 : 351 : return warned;
7987 : : }
7988 : :
7989 : : /* Issue a warning for a bad initializer component.
7990 : :
7991 : : OPT is the OPT_W* value corresponding to the warning option that
7992 : : controls this warning. GMSGID identifies the message. The
7993 : : component name is taken from the spelling stack. */
7994 : :
7995 : : static void
7996 : 146 : warning_init (location_t loc, int opt, const char *gmsgid)
7997 : : {
7998 : 146 : char *ofwhat;
7999 : 146 : bool warned;
8000 : :
8001 : 146 : auto_diagnostic_group d;
8002 : :
8003 : : /* Use the location where a macro was expanded rather than where
8004 : : it was defined to make sure macros defined in system headers
8005 : : but used incorrectly elsewhere are diagnosed. */
8006 : 146 : location_t exploc = expansion_point_location_if_in_system_header (loc);
8007 : :
8008 : : /* The gmsgid may be a format string with %< and %>. */
8009 : 146 : warned = warning_at (exploc, opt, gmsgid);
8010 : 146 : if (warned)
8011 : : {
8012 : 133 : ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
8013 : 133 : if (*ofwhat)
8014 : 133 : inform (exploc, "(near initialization for %qs)", ofwhat);
8015 : : }
8016 : 146 : }
8017 : :
8018 : : /* If TYPE is an array type and EXPR is a parenthesized string
8019 : : constant, warn if pedantic that EXPR is being used to initialize an
8020 : : object of type TYPE. */
8021 : :
8022 : : void
8023 : 7107991 : maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
8024 : : {
8025 : 7107991 : if (pedantic
8026 : 80450 : && TREE_CODE (type) == ARRAY_TYPE
8027 : 1138 : && TREE_CODE (expr.value) == STRING_CST
8028 : 472 : && expr.original_code != STRING_CST)
8029 : 13 : pedwarn_init (loc, OPT_Wpedantic,
8030 : : "array initialized from parenthesized string constant");
8031 : 7107991 : }
8032 : :
8033 : : /* Attempt to locate the parameter with the given index within FNDECL,
8034 : : returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
8035 : :
8036 : : static location_t
8037 : 936 : get_fndecl_argument_location (tree fndecl, int argnum)
8038 : : {
8039 : 936 : int i;
8040 : 936 : tree param;
8041 : :
8042 : : /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8043 : 936 : for (i = 0, param = DECL_ARGUMENTS (fndecl);
8044 : 1015 : i < argnum && param;
8045 : 79 : i++, param = TREE_CHAIN (param))
8046 : : ;
8047 : :
8048 : : /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8049 : : return DECL_SOURCE_LOCATION (FNDECL). */
8050 : 936 : if (param == NULL)
8051 : 653 : return DECL_SOURCE_LOCATION (fndecl);
8052 : :
8053 : 283 : return DECL_SOURCE_LOCATION (param);
8054 : : }
8055 : :
8056 : : /* Issue a note about a mismatching argument for parameter PARMNUM
8057 : : to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
8058 : : Attempt to issue the note at the pertinent parameter of the decl;
8059 : : failing that issue it at the location of FUNDECL; failing that
8060 : : issue it at PLOC.
8061 : : Use highlight_colors::actual for the ACTUAL_TYPE
8062 : : and highlight_colors::expected for EXPECTED_TYPE and the
8063 : : parameter of FUNDECL*/
8064 : :
8065 : : static void
8066 : 1060 : inform_for_arg (tree fundecl, location_t ploc, int parmnum,
8067 : : tree expected_type, tree actual_type)
8068 : : {
8069 : 1060 : location_t loc;
8070 : 1060 : if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8071 : 936 : loc = get_fndecl_argument_location (fundecl, parmnum - 1);
8072 : : else
8073 : : loc = ploc;
8074 : :
8075 : 1060 : gcc_rich_location richloc (loc, nullptr, highlight_colors::expected);
8076 : :
8077 : 1060 : pp_markup::element_expected_type elem_expected_type (expected_type);
8078 : 1060 : pp_markup::element_actual_type elem_actual_type (actual_type);
8079 : 1060 : inform (&richloc,
8080 : : "expected %e but argument is of type %e",
8081 : : &elem_expected_type, &elem_actual_type);
8082 : 1060 : }
8083 : :
8084 : : /* Issue a warning when an argument of ARGTYPE is passed to a built-in
8085 : : function FUNDECL declared without prototype to parameter PARMNUM of
8086 : : PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
8087 : :
8088 : : static void
8089 : 418 : maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
8090 : : tree parmtype, tree argtype)
8091 : : {
8092 : 418 : tree_code parmcode = TREE_CODE (parmtype);
8093 : 418 : tree_code argcode = TREE_CODE (argtype);
8094 : 418 : tree promoted = c_type_promotes_to (argtype);
8095 : :
8096 : : /* Avoid warning for enum arguments that promote to an integer type
8097 : : of the same size/mode. */
8098 : 418 : if (parmcode == INTEGER_TYPE
8099 : 418 : && argcode == ENUMERAL_TYPE
8100 : 418 : && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
8101 : : return;
8102 : :
8103 : 417 : if ((parmcode == argcode
8104 : 222 : || (parmcode == INTEGER_TYPE
8105 : : && argcode == ENUMERAL_TYPE))
8106 : 418 : && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
8107 : : return;
8108 : :
8109 : : /* This diagnoses even signed/unsigned mismatches. Those might be
8110 : : safe in many cases but GCC may emit suboptimal code for them so
8111 : : warning on those cases drives efficiency improvements. */
8112 : 398 : if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
8113 : 398 : TYPE_MAIN_VARIANT (promoted) == argtype
8114 : : ? G_("%qD argument %d type is %qT where %qT is expected "
8115 : : "in a call to built-in function declared without "
8116 : : "prototype")
8117 : : : G_("%qD argument %d promotes to %qT where %qT is expected "
8118 : : "in a call to built-in function declared without "
8119 : : "prototype"),
8120 : : fundecl, parmnum, promoted, parmtype))
8121 : 150 : inform (DECL_SOURCE_LOCATION (fundecl),
8122 : : "built-in %qD declared here",
8123 : : fundecl);
8124 : : }
8125 : :
8126 : : /* Print a declaration in quotes, with the given highlight_color.
8127 : : Analogous to handler for %qD, but with a specific highlight color. */
8128 : :
8129 : 199 : class pp_element_quoted_decl : public pp_element
8130 : : {
8131 : : public:
8132 : 199 : pp_element_quoted_decl (tree decl, const char *highlight_color)
8133 : 199 : : m_decl (decl),
8134 : 199 : m_highlight_color (highlight_color)
8135 : : {
8136 : : }
8137 : :
8138 : 199 : void add_to_phase_2 (pp_markup::context &ctxt) override
8139 : : {
8140 : 199 : ctxt.begin_quote ();
8141 : 199 : ctxt.begin_highlight_color (m_highlight_color);
8142 : :
8143 : 199 : print_decl (ctxt);
8144 : :
8145 : 199 : ctxt.end_highlight_color ();
8146 : 199 : ctxt.end_quote ();
8147 : 199 : }
8148 : :
8149 : 199 : void print_decl (pp_markup::context &ctxt)
8150 : : {
8151 : 199 : pretty_printer *const pp = &ctxt.m_pp;
8152 : 199 : pp->set_padding (pp_none);
8153 : 199 : if (DECL_NAME (m_decl))
8154 : 199 : pp_identifier (pp, lang_hooks.decl_printable_name (m_decl, 2));
8155 : : else
8156 : 0 : pp_string (pp, _("({anonymous})"));
8157 : 199 : }
8158 : :
8159 : : private:
8160 : : tree m_decl;
8161 : : const char *m_highlight_color;
8162 : : };
8163 : :
8164 : : /* If TYPE is from a typedef, issue a note showing the location
8165 : : to the user.
8166 : : Use HIGHLIGHT_COLOR as the highlight color. */
8167 : :
8168 : : static void
8169 : 1178 : maybe_inform_typedef_location (tree type, const char *highlight_color)
8170 : : {
8171 : 1178 : if (!typedef_variant_p (type))
8172 : 1144 : return;
8173 : :
8174 : 34 : tree typedef_decl = TYPE_NAME (type);
8175 : 34 : gcc_assert (TREE_CODE (typedef_decl) == TYPE_DECL);
8176 : 34 : gcc_rich_location richloc (DECL_SOURCE_LOCATION (typedef_decl),
8177 : 34 : nullptr, highlight_color);
8178 : 34 : pp_element_quoted_decl e_typedef_decl (typedef_decl, highlight_color);
8179 : 34 : inform (&richloc, "%e declared here", &e_typedef_decl);
8180 : 34 : }
8181 : :
8182 : : /* Convert value RHS to type TYPE as preparation for an assignment to
8183 : : an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
8184 : : original type of RHS; this differs from TREE_TYPE (RHS) for enum
8185 : : types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
8186 : : constant before any folding.
8187 : : The real work of conversion is done by `convert'.
8188 : : The purpose of this function is to generate error messages
8189 : : for assignments that are not allowed in C.
8190 : : ERRTYPE says whether it is argument passing, assignment,
8191 : : initialization or return.
8192 : :
8193 : : In the following example, '~' denotes where EXPR_LOC and '^' where
8194 : : LOCATION point to:
8195 : :
8196 : : f (var); [ic_argpass]
8197 : : ^ ~~~
8198 : : x = var; [ic_assign]
8199 : : ^ ~~~;
8200 : : int x = var; [ic_init]
8201 : : ^^^
8202 : : return x; [ic_return]
8203 : : ^
8204 : :
8205 : : FUNCTION is a tree for the function being called.
8206 : : PARMNUM is the number of the argument, for printing in error messages.
8207 : : WARNOPT may be set to a warning option to issue the corresponding warning
8208 : : rather than an error for invalid conversions. Used for calls to built-in
8209 : : functions declared without a prototype. */
8210 : :
8211 : : static tree
8212 : 162806936 : convert_for_assignment (location_t location, location_t expr_loc, tree type,
8213 : : tree rhs, tree origtype, enum impl_conv errtype,
8214 : : bool null_pointer_constant, tree fundecl,
8215 : : tree function, int parmnum, int warnopt /* = 0 */)
8216 : : {
8217 : 162806936 : enum tree_code codel = TREE_CODE (type);
8218 : 162806936 : tree orig_rhs = rhs;
8219 : 162806936 : tree rhstype;
8220 : 162806936 : enum tree_code coder;
8221 : 162806936 : tree rname = NULL_TREE;
8222 : 162806936 : bool objc_ok = false;
8223 : :
8224 : : /* Use the expansion point location to handle cases such as user's
8225 : : function returning a wrong-type macro defined in a system header. */
8226 : 162806936 : location = expansion_point_location_if_in_system_header (location);
8227 : :
8228 : 162806936 : if (errtype == ic_argpass)
8229 : : {
8230 : 122381796 : tree selector;
8231 : : /* Change pointer to function to the function itself for
8232 : : diagnostics. */
8233 : 122381796 : if (TREE_CODE (function) == ADDR_EXPR
8234 : 122381796 : && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
8235 : 0 : function = TREE_OPERAND (function, 0);
8236 : :
8237 : : /* Handle an ObjC selector specially for diagnostics. */
8238 : 122381796 : selector = objc_message_selector ();
8239 : 122381796 : rname = function;
8240 : 122381796 : if (selector && parmnum > 2)
8241 : : {
8242 : 0 : rname = selector;
8243 : 0 : parmnum -= 2;
8244 : : }
8245 : : }
8246 : :
8247 : : /* This macro is used to emit diagnostics to ensure that all format
8248 : : strings are complete sentences, visible to gettext and checked at
8249 : : compile time. */
8250 : : #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
8251 : : do { \
8252 : : switch (errtype) \
8253 : : { \
8254 : : case ic_argpass: \
8255 : : { \
8256 : : auto_diagnostic_group d; \
8257 : : if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
8258 : : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8259 : : } \
8260 : : break; \
8261 : : case ic_assign: \
8262 : : pedwarn (LOCATION, OPT, AS); \
8263 : : break; \
8264 : : case ic_init: \
8265 : : case ic_init_const: \
8266 : : pedwarn_init (LOCATION, OPT, IN); \
8267 : : break; \
8268 : : case ic_return: \
8269 : : pedwarn (LOCATION, OPT, RE); \
8270 : : break; \
8271 : : default: \
8272 : : gcc_unreachable (); \
8273 : : } \
8274 : : } while (0)
8275 : :
8276 : : /* This macro is used to emit diagnostics to ensure that all format
8277 : : strings are complete sentences, visible to gettext and checked at
8278 : : compile time. It can be called with 'pedwarn' or 'warning_at'. */
8279 : : #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8280 : : do { \
8281 : : switch (errtype) \
8282 : : { \
8283 : : case ic_argpass: \
8284 : : { \
8285 : : auto_diagnostic_group d; \
8286 : : if (PEDWARN) { \
8287 : : if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8288 : : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8289 : : } else { \
8290 : : if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
8291 : : inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
8292 : : } \
8293 : : } \
8294 : : break; \
8295 : : case ic_assign: \
8296 : : if (PEDWARN) \
8297 : : pedwarn (LOCATION, OPT, AS, QUALS); \
8298 : : else \
8299 : : warning_at (LOCATION, OPT, AS, QUALS); \
8300 : : break; \
8301 : : case ic_init: \
8302 : : case ic_init_const: \
8303 : : if (PEDWARN) \
8304 : : pedwarn (LOCATION, OPT, IN, QUALS); \
8305 : : else \
8306 : : warning_at (LOCATION, OPT, IN, QUALS); \
8307 : : break; \
8308 : : case ic_return: \
8309 : : if (PEDWARN) \
8310 : : pedwarn (LOCATION, OPT, RE, QUALS); \
8311 : : else \
8312 : : warning_at (LOCATION, OPT, RE, QUALS); \
8313 : : break; \
8314 : : default: \
8315 : : gcc_unreachable (); \
8316 : : } \
8317 : : } while (0)
8318 : :
8319 : : /* This macro is used to emit diagnostics to ensure that all format
8320 : : strings are complete sentences, visible to gettext and checked at
8321 : : compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
8322 : : extra parameter to enumerate qualifiers. */
8323 : : #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
8324 : : WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
8325 : :
8326 : :
8327 : 162806936 : if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
8328 : 5963 : rhs = TREE_OPERAND (rhs, 0);
8329 : :
8330 : 162806936 : rhstype = TREE_TYPE (rhs);
8331 : 162806936 : coder = TREE_CODE (rhstype);
8332 : :
8333 : 162806936 : if (coder == ERROR_MARK)
8334 : 346 : return error_mark_node;
8335 : :
8336 : 162806590 : if (c_dialect_objc ())
8337 : : {
8338 : 0 : int parmno;
8339 : :
8340 : 0 : switch (errtype)
8341 : : {
8342 : : case ic_return:
8343 : : parmno = 0;
8344 : : break;
8345 : :
8346 : : case ic_assign:
8347 : : parmno = -1;
8348 : : break;
8349 : :
8350 : : case ic_init:
8351 : : case ic_init_const:
8352 : : parmno = -2;
8353 : : break;
8354 : :
8355 : : default:
8356 : 0 : parmno = parmnum;
8357 : : break;
8358 : : }
8359 : :
8360 : 0 : objc_ok = objc_compare_types (type, rhstype, parmno, rname);
8361 : : }
8362 : :
8363 : 162806590 : if (warn_cxx_compat)
8364 : : {
8365 : 28856 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8366 : 28856 : if (checktype != error_mark_node
8367 : 28856 : && TREE_CODE (type) == ENUMERAL_TYPE
8368 : 28962 : && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
8369 : 45 : switch (errtype)
8370 : : {
8371 : 8 : case ic_argpass:
8372 : 8 : if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
8373 : : "passing argument %d of %qE is invalid in C++",
8374 : : parmnum, rname))
8375 : 16 : inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
8376 : 8 : ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
8377 : : "expected %qT but argument is of type %qT",
8378 : : type, rhstype);
8379 : : break;
8380 : 10 : case ic_assign:
8381 : 10 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8382 : : "%qT in assignment is invalid in C++", rhstype, type);
8383 : 10 : break;
8384 : 18 : case ic_init:
8385 : 18 : case ic_init_const:
8386 : 18 : pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
8387 : : "%qT to %qT in initialization is invalid in C++",
8388 : : rhstype, type);
8389 : 18 : break;
8390 : 9 : case ic_return:
8391 : 9 : pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
8392 : : "%qT in return is invalid in C++", rhstype, type);
8393 : 9 : break;
8394 : 0 : default:
8395 : 0 : gcc_unreachable ();
8396 : : }
8397 : : }
8398 : :
8399 : 162806590 : if (warn_enum_conversion)
8400 : : {
8401 : 14565040 : tree checktype = origtype != NULL_TREE ? origtype : rhstype;
8402 : 14565040 : if (checktype != error_mark_node
8403 : 14565040 : && TREE_CODE (checktype) == ENUMERAL_TYPE
8404 : 12669 : && TREE_CODE (type) == ENUMERAL_TYPE
8405 : 14573224 : && !comptypes (TYPE_MAIN_VARIANT (checktype), TYPE_MAIN_VARIANT (type)))
8406 : : {
8407 : 3 : gcc_rich_location loc (location);
8408 : 3 : warning_at (&loc, OPT_Wenum_conversion,
8409 : : "implicit conversion from %qT to %qT",
8410 : : checktype, type);
8411 : 3 : }
8412 : : }
8413 : :
8414 : 162806590 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
8415 : : {
8416 : 144135009 : warn_for_address_of_packed_member (type, orig_rhs);
8417 : 144135009 : if (type != rhstype)
8418 : : /* Convert RHS to TYPE in order to not lose TYPE in diagnostics. */
8419 : 35898255 : rhs = convert (type, rhs);
8420 : 144135009 : return rhs;
8421 : : }
8422 : :
8423 : 18671581 : if (coder == VOID_TYPE)
8424 : : {
8425 : : /* Except for passing an argument to an unprototyped function,
8426 : : this is a constraint violation. When passing an argument to
8427 : : an unprototyped function, it is compile-time undefined;
8428 : : making it a constraint in that case was rejected in
8429 : : DR#252. */
8430 : 8 : const char msg[] = "void value not ignored as it ought to be";
8431 : 8 : if (warnopt)
8432 : 0 : warning_at (location, warnopt, msg);
8433 : : else
8434 : 8 : error_at (location, msg);
8435 : 8 : return error_mark_node;
8436 : : }
8437 : 18671573 : rhs = require_complete_type (location, rhs);
8438 : 18671573 : if (rhs == error_mark_node)
8439 : : return error_mark_node;
8440 : :
8441 : 18671573 : if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
8442 : 6 : return error_mark_node;
8443 : :
8444 : : /* A non-reference type can convert to a reference. This handles
8445 : : va_start, va_copy and possibly port built-ins. */
8446 : 18671567 : if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
8447 : : {
8448 : 270 : if (!lvalue_p (rhs))
8449 : : {
8450 : 0 : const char msg[] = "cannot pass rvalue to reference parameter";
8451 : 0 : if (warnopt)
8452 : 0 : warning_at (location, warnopt, msg);
8453 : : else
8454 : 0 : error_at (location, msg);
8455 : 0 : return error_mark_node;
8456 : : }
8457 : 270 : if (!c_mark_addressable (rhs))
8458 : 0 : return error_mark_node;
8459 : 270 : rhs = build1 (ADDR_EXPR, c_build_pointer_type (TREE_TYPE (rhs)), rhs);
8460 : 270 : SET_EXPR_LOCATION (rhs, location);
8461 : :
8462 : 270 : rhs = convert_for_assignment (location, expr_loc,
8463 : 270 : c_build_pointer_type (TREE_TYPE (type)),
8464 : : rhs, origtype, errtype,
8465 : : null_pointer_constant, fundecl, function,
8466 : : parmnum, warnopt);
8467 : 270 : if (rhs == error_mark_node)
8468 : : return error_mark_node;
8469 : :
8470 : 270 : rhs = build1 (NOP_EXPR, type, rhs);
8471 : 270 : SET_EXPR_LOCATION (rhs, location);
8472 : 270 : return rhs;
8473 : : }
8474 : : /* Some types can interconvert without explicit casts. */
8475 : 18671297 : else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
8476 : 18671297 : && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
8477 : 9054631 : return convert (type, rhs);
8478 : : /* Arithmetic types all interconvert, and enum is treated like int. */
8479 : 9616666 : else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
8480 : 2240924 : || codel == FIXED_POINT_TYPE
8481 : 2240924 : || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
8482 : 2155124 : || codel == BOOLEAN_TYPE || codel == BITINT_TYPE)
8483 : 7552761 : && (coder == INTEGER_TYPE || coder == REAL_TYPE
8484 : 81549 : || coder == FIXED_POINT_TYPE
8485 : 81549 : || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
8486 : 39553 : || coder == BOOLEAN_TYPE || coder == BITINT_TYPE))
8487 : : {
8488 : 7551643 : if (warnopt && errtype == ic_argpass)
8489 : 418 : maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
8490 : : rhstype);
8491 : :
8492 : 7551643 : bool save = in_late_binary_op;
8493 : 7520992 : if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
8494 : 15016467 : || (coder == REAL_TYPE
8495 : 271499 : && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
8496 : 24098 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
8497 : 93563 : in_late_binary_op = true;
8498 : 10864026 : tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
8499 : : ? expr_loc : location, type, orig_rhs,
8500 : : errtype == ic_init_const);
8501 : 7551643 : in_late_binary_op = save;
8502 : 7551643 : return ret;
8503 : : }
8504 : :
8505 : : /* Aggregates in different TUs might need conversion. */
8506 : 2065023 : if ((codel == RECORD_TYPE || codel == UNION_TYPE)
8507 : 103 : && codel == coder
8508 : 2065044 : && comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (rhstype)))
8509 : 8 : return convert_and_check (expr_loc != UNKNOWN_LOCATION
8510 : 8 : ? expr_loc : location, type, rhs);
8511 : :
8512 : : /* Conversion to a transparent union or record from its member types.
8513 : : This applies only to function arguments. */
8514 : 2065015 : if (((codel == UNION_TYPE || codel == RECORD_TYPE)
8515 : 95 : && TYPE_TRANSPARENT_AGGR (type))
8516 : 2065053 : && errtype == ic_argpass)
8517 : : {
8518 : 38 : tree memb, marginal_memb = NULL_TREE;
8519 : :
8520 : 48 : for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
8521 : : {
8522 : 48 : tree memb_type = TREE_TYPE (memb);
8523 : :
8524 : 48 : if (comptypes (TYPE_MAIN_VARIANT (memb_type),
8525 : 48 : TYPE_MAIN_VARIANT (rhstype)))
8526 : : break;
8527 : :
8528 : 26 : if (TREE_CODE (memb_type) != POINTER_TYPE)
8529 : 0 : continue;
8530 : :
8531 : 26 : if (coder == POINTER_TYPE)
8532 : : {
8533 : 26 : tree ttl = TREE_TYPE (memb_type);
8534 : 26 : tree ttr = TREE_TYPE (rhstype);
8535 : :
8536 : : /* Any non-function converts to a [const][volatile] void *
8537 : : and vice versa; otherwise, targets must be the same.
8538 : : Meanwhile, the lhs target must have all the qualifiers of
8539 : : the rhs. */
8540 : 0 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8541 : 26 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8542 : 45 : || comp_target_types (location, memb_type, rhstype))
8543 : : {
8544 : 16 : int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
8545 : 16 : int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
8546 : : /* If this type won't generate any warnings, use it. */
8547 : 16 : if (lquals == rquals
8548 : 16 : || ((TREE_CODE (ttr) == FUNCTION_TYPE
8549 : 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8550 : 0 : ? ((lquals | rquals) == rquals)
8551 : 16 : : ((lquals | rquals) == lquals)))
8552 : : break;
8553 : :
8554 : : /* Keep looking for a better type, but remember this one. */
8555 : 0 : if (!marginal_memb)
8556 : 10 : marginal_memb = memb;
8557 : : }
8558 : : }
8559 : :
8560 : : /* Can convert integer zero to any pointer type. */
8561 : 10 : if (null_pointer_constant)
8562 : : {
8563 : 0 : rhs = null_pointer_node;
8564 : 0 : break;
8565 : : }
8566 : : }
8567 : :
8568 : 38 : if (memb || marginal_memb)
8569 : : {
8570 : 38 : if (!memb)
8571 : : {
8572 : : /* We have only a marginally acceptable member type;
8573 : : it needs a warning. */
8574 : 0 : tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
8575 : 0 : tree ttr = TREE_TYPE (rhstype);
8576 : :
8577 : : /* Const and volatile mean something different for function
8578 : : types, so the usual warnings are not appropriate. */
8579 : 0 : if (TREE_CODE (ttr) == FUNCTION_TYPE
8580 : 0 : && TREE_CODE (ttl) == FUNCTION_TYPE)
8581 : : {
8582 : : /* Because const and volatile on functions are
8583 : : restrictions that say the function will not do
8584 : : certain things, it is okay to use a const or volatile
8585 : : function where an ordinary one is wanted, but not
8586 : : vice-versa. */
8587 : 0 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8588 : 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8589 : 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8590 : : OPT_Wdiscarded_qualifiers,
8591 : : G_("passing argument %d of %qE "
8592 : : "makes %q#v qualified function "
8593 : : "pointer from unqualified"),
8594 : : G_("assignment makes %q#v qualified "
8595 : : "function pointer from "
8596 : : "unqualified"),
8597 : : G_("initialization makes %q#v qualified "
8598 : : "function pointer from "
8599 : : "unqualified"),
8600 : : G_("return makes %q#v qualified function "
8601 : : "pointer from unqualified"),
8602 : : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8603 : : }
8604 : 0 : else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
8605 : 0 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8606 : 0 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8607 : : OPT_Wdiscarded_qualifiers,
8608 : : G_("passing argument %d of %qE discards "
8609 : : "%qv qualifier from pointer target type"),
8610 : : G_("assignment discards %qv qualifier "
8611 : : "from pointer target type"),
8612 : : G_("initialization discards %qv qualifier "
8613 : : "from pointer target type"),
8614 : : G_("return discards %qv qualifier from "
8615 : : "pointer target type"),
8616 : : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8617 : :
8618 : : memb = marginal_memb;
8619 : : }
8620 : :
8621 : 38 : if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
8622 : 30 : pedwarn (location, OPT_Wpedantic,
8623 : : "ISO C prohibits argument conversion to union type");
8624 : :
8625 : 38 : rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
8626 : 38 : return build_constructor_single (type, memb, rhs);
8627 : : }
8628 : : }
8629 : :
8630 : : /* Conversions among pointers */
8631 : 2064977 : else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8632 : 2063703 : && (coder == codel))
8633 : : {
8634 : : /* If RHS refers to a built-in declared without a prototype
8635 : : BLTIN is the declaration of the built-in with a prototype
8636 : : and RHSTYPE is set to the actual type of the built-in. */
8637 : 1985729 : tree bltin;
8638 : 1985729 : rhstype = type_or_builtin_type (rhs, &bltin);
8639 : :
8640 : 1985729 : tree ttl = TREE_TYPE (type);
8641 : 1985729 : tree ttr = TREE_TYPE (rhstype);
8642 : 1985729 : bool is_opaque_pointer;
8643 : 1985729 : bool target_cmp = false; /* Cache comp_target_types () result. */
8644 : 1985729 : addr_space_t asl;
8645 : 1985729 : addr_space_t asr;
8646 : :
8647 : 1985729 : tree mvl = remove_qualifiers (ttl);
8648 : 1985729 : tree mvr = remove_qualifiers (ttr);
8649 : :
8650 : : /* Opaque pointers are treated like void pointers. */
8651 : 1985729 : is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
8652 : :
8653 : : /* The Plan 9 compiler permits a pointer to a struct to be
8654 : : automatically converted into a pointer to an anonymous field
8655 : : within the struct. */
8656 : 1985729 : if (flag_plan9_extensions
8657 : 10 : && RECORD_OR_UNION_TYPE_P (mvl)
8658 : 10 : && RECORD_OR_UNION_TYPE_P (mvr)
8659 : 10 : && mvl != mvr)
8660 : : {
8661 : 10 : tree new_rhs = convert_to_anonymous_field (location, type, rhs);
8662 : 10 : if (new_rhs != NULL_TREE)
8663 : : {
8664 : 6 : rhs = new_rhs;
8665 : 6 : rhstype = TREE_TYPE (rhs);
8666 : 6 : coder = TREE_CODE (rhstype);
8667 : 6 : ttr = TREE_TYPE (rhstype);
8668 : 6 : mvr = TYPE_MAIN_VARIANT (ttr);
8669 : : }
8670 : : }
8671 : :
8672 : : /* C++ does not allow the implicit conversion void* -> T*. However,
8673 : : for the purpose of reducing the number of false positives, we
8674 : : tolerate the special case of
8675 : :
8676 : : int *p = NULL;
8677 : :
8678 : : where NULL is typically defined in C to be '(void *) 0'. */
8679 : 1985729 : if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8680 : 26461 : warning_at (errtype == ic_argpass ? expr_loc : location,
8681 : 14014 : OPT_Wc___compat,
8682 : : "request for implicit conversion "
8683 : : "from %qT to %qT not permitted in C++", rhstype, type);
8684 : :
8685 : : /* Warn of new allocations that are not big enough for the target
8686 : : type. */
8687 : 1985729 : if (warn_alloc_size && TREE_CODE (rhs) == CALL_EXPR)
8688 : 5455 : if (tree fndecl = get_callee_fndecl (rhs))
8689 : 5455 : if (DECL_IS_MALLOC (fndecl))
8690 : : {
8691 : 4601 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
8692 : 4601 : tree alloc_size = lookup_attribute ("alloc_size", attrs);
8693 : 4601 : if (alloc_size)
8694 : 447 : warn_for_alloc_size (location, ttl, rhs, alloc_size);
8695 : : }
8696 : :
8697 : : /* See if the pointers point to incompatible address spaces. */
8698 : 1985729 : asl = TYPE_ADDR_SPACE (ttl);
8699 : 1985729 : asr = TYPE_ADDR_SPACE (ttr);
8700 : 1985729 : if (!null_pointer_constant_p (rhs)
8701 : 1985729 : && asr != asl && !targetm.addr_space.subset_p (asr, asl))
8702 : : {
8703 : 3 : auto_diagnostic_group d;
8704 : 3 : bool diagnosed = true;
8705 : 3 : switch (errtype)
8706 : : {
8707 : 1 : case ic_argpass:
8708 : 1 : {
8709 : 1 : const char msg[] = G_("passing argument %d of %qE from "
8710 : : "pointer to non-enclosed address space");
8711 : 1 : if (warnopt)
8712 : 0 : diagnosed
8713 : 0 : = warning_at (expr_loc, warnopt, msg, parmnum, rname);
8714 : : else
8715 : 1 : error_at (expr_loc, msg, parmnum, rname);
8716 : 0 : break;
8717 : : }
8718 : 0 : case ic_assign:
8719 : 0 : {
8720 : 0 : const char msg[] = G_("assignment from pointer to "
8721 : : "non-enclosed address space");
8722 : 0 : if (warnopt)
8723 : 0 : diagnosed = warning_at (location, warnopt, msg);
8724 : : else
8725 : 0 : error_at (location, msg);
8726 : 0 : break;
8727 : : }
8728 : 0 : case ic_init:
8729 : 0 : case ic_init_const:
8730 : 0 : {
8731 : 0 : const char msg[] = G_("initialization from pointer to "
8732 : : "non-enclosed address space");
8733 : 0 : if (warnopt)
8734 : 0 : diagnosed = warning_at (location, warnopt, msg);
8735 : : else
8736 : 0 : error_at (location, msg);
8737 : 0 : break;
8738 : : }
8739 : 2 : case ic_return:
8740 : 2 : {
8741 : 2 : const char msg[] = G_("return from pointer to "
8742 : : "non-enclosed address space");
8743 : 2 : if (warnopt)
8744 : 0 : diagnosed = warning_at (location, warnopt, msg);
8745 : : else
8746 : 2 : error_at (location, msg);
8747 : 0 : break;
8748 : : }
8749 : 0 : default:
8750 : 0 : gcc_unreachable ();
8751 : : }
8752 : 3 : if (diagnosed)
8753 : : {
8754 : 3 : if (errtype == ic_argpass)
8755 : 1 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
8756 : : else
8757 : 2 : inform (location, "expected %qT but pointer is of type %qT",
8758 : : type, rhstype);
8759 : : }
8760 : 3 : return error_mark_node;
8761 : 3 : }
8762 : :
8763 : : /* Check if the right-hand side has a format attribute but the
8764 : : left-hand side doesn't. */
8765 : 1985726 : if (warn_suggest_attribute_format
8766 : 1985726 : && check_missing_format_attribute (type, rhstype))
8767 : : {
8768 : 16 : switch (errtype)
8769 : : {
8770 : 4 : case ic_argpass:
8771 : 4 : warning_at (expr_loc, OPT_Wsuggest_attribute_format,
8772 : : "argument %d of %qE might be "
8773 : : "a candidate for a format attribute",
8774 : : parmnum, rname);
8775 : 4 : break;
8776 : 4 : case ic_assign:
8777 : 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8778 : : "assignment left-hand side might be "
8779 : : "a candidate for a format attribute");
8780 : 4 : break;
8781 : 4 : case ic_init:
8782 : 4 : case ic_init_const:
8783 : 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8784 : : "initialization left-hand side might be "
8785 : : "a candidate for a format attribute");
8786 : 4 : break;
8787 : 4 : case ic_return:
8788 : 4 : warning_at (location, OPT_Wsuggest_attribute_format,
8789 : : "return type might be "
8790 : : "a candidate for a format attribute");
8791 : 4 : break;
8792 : 0 : default:
8793 : 0 : gcc_unreachable ();
8794 : : }
8795 : : }
8796 : :
8797 : : /* See if the pointers point to incompatible scalar storage orders. */
8798 : 1985726 : if (warn_scalar_storage_order
8799 : 1982810 : && !null_pointer_constant_p (rhs)
8800 : 5886224 : && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
8801 : 1950249 : != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
8802 : : {
8803 : 18 : tree t;
8804 : :
8805 : 18 : switch (errtype)
8806 : : {
8807 : 11 : case ic_argpass:
8808 : : /* Do not warn for built-in functions, for example memcpy, since we
8809 : : control how they behave and they can be useful in this area. */
8810 : 11 : if (TREE_CODE (rname) != FUNCTION_DECL
8811 : 11 : || !fndecl_built_in_p (rname))
8812 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8813 : : "passing argument %d of %qE from incompatible "
8814 : : "scalar storage order", parmnum, rname);
8815 : : break;
8816 : 3 : case ic_assign:
8817 : : /* Do not warn if the RHS is a call to a function that returns a
8818 : : pointer that is not an alias. */
8819 : 3 : if (TREE_CODE (rhs) != CALL_EXPR
8820 : 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8821 : 5 : || !DECL_IS_MALLOC (t))
8822 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8823 : : "assignment to %qT from pointer type %qT with "
8824 : : "incompatible scalar storage order", type, rhstype);
8825 : : break;
8826 : 3 : case ic_init:
8827 : 3 : case ic_init_const:
8828 : : /* Likewise. */
8829 : 3 : if (TREE_CODE (rhs) != CALL_EXPR
8830 : 2 : || (t = get_callee_fndecl (rhs)) == NULL_TREE
8831 : 5 : || !DECL_IS_MALLOC (t))
8832 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8833 : : "initialization of %qT from pointer type %qT with "
8834 : : "incompatible scalar storage order", type, rhstype);
8835 : : break;
8836 : 1 : case ic_return:
8837 : 1 : warning_at (location, OPT_Wscalar_storage_order,
8838 : : "returning %qT from pointer type with incompatible "
8839 : : "scalar storage order %qT", rhstype, type);
8840 : 1 : break;
8841 : 0 : default:
8842 : 0 : gcc_unreachable ();
8843 : : }
8844 : : }
8845 : :
8846 : : /* Any non-function converts to a [const][volatile] void *
8847 : : and vice versa; otherwise, targets must be the same.
8848 : : Meanwhile, the lhs target must have all the qualifiers of the rhs. */
8849 : 420188 : if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
8850 : 1565539 : || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
8851 : 1519100 : || (target_cmp = comp_target_types (location, type, rhstype))
8852 : 2251 : || is_opaque_pointer
8853 : 1990228 : || ((c_common_unsigned_type (mvl)
8854 : 2251 : == c_common_unsigned_type (mvr))
8855 : 2950 : && (c_common_signed_type (mvl)
8856 : 1475 : == c_common_signed_type (mvr))
8857 : 1460 : && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
8858 : : {
8859 : : /* Warn about loss of qualifers from pointers to arrays with
8860 : : qualifiers on the element type. */
8861 : 1984927 : if (TREE_CODE (ttr) == ARRAY_TYPE)
8862 : : {
8863 : 2129 : ttr = strip_array_types (ttr);
8864 : 2129 : ttl = strip_array_types (ttl);
8865 : :
8866 : 2129 : if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8867 : 2129 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
8868 : 115 : WARNING_FOR_QUALIFIERS (flag_isoc23,
8869 : : location, expr_loc,
8870 : : OPT_Wdiscarded_array_qualifiers,
8871 : : G_("passing argument %d of %qE discards "
8872 : : "%qv qualifier from pointer target type"),
8873 : : G_("assignment discards %qv qualifier "
8874 : : "from pointer target type"),
8875 : : G_("initialization discards %qv qualifier "
8876 : : "from pointer target type"),
8877 : : G_("return discards %qv qualifier from "
8878 : : "pointer target type"),
8879 : : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8880 : : }
8881 : 1982798 : else if (pedantic
8882 : 146032 : && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
8883 : 146012 : ||
8884 : : (VOID_TYPE_P (ttr)
8885 : 2952 : && !null_pointer_constant
8886 : 178 : && TREE_CODE (ttl) == FUNCTION_TYPE)))
8887 : 34 : PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
8888 : : G_("ISO C forbids passing argument %d of "
8889 : : "%qE between function pointer "
8890 : : "and %<void *%>"),
8891 : : G_("ISO C forbids assignment between "
8892 : : "function pointer and %<void *%>"),
8893 : : G_("ISO C forbids initialization between "
8894 : : "function pointer and %<void *%>"),
8895 : : G_("ISO C forbids return between function "
8896 : : "pointer and %<void *%>"));
8897 : : /* Const and volatile mean something different for function types,
8898 : : so the usual warnings are not appropriate. */
8899 : 1982764 : else if (TREE_CODE (ttr) != FUNCTION_TYPE
8900 : 1950349 : && TREE_CODE (ttl) != FUNCTION_TYPE)
8901 : : {
8902 : : /* Assignments between atomic and non-atomic objects are OK. */
8903 : 1949684 : bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8904 : 1949684 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
8905 : 1949684 : bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
8906 : 1949684 : & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
8907 : :
8908 : : /* Don't warn about loss of qualifier for conversions from
8909 : : qualified void* to pointers to arrays with corresponding
8910 : : qualifier on the element type (except for pedantic before C23). */
8911 : 1949684 : if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc23))
8912 : 697 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8913 : : OPT_Wdiscarded_qualifiers,
8914 : : G_("passing argument %d of %qE discards "
8915 : : "%qv qualifier from pointer target type"),
8916 : : G_("assignment discards %qv qualifier "
8917 : : "from pointer target type"),
8918 : : G_("initialization discards %qv qualifier "
8919 : : "from pointer target type"),
8920 : : G_("return discards %qv qualifier from "
8921 : : "pointer target type"),
8922 : : TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
8923 : 11 : else if (warn_quals_ped)
8924 : 11 : pedwarn_c11 (location, OPT_Wc11_c23_compat,
8925 : : "array with qualifier on the element is not qualified before C23");
8926 : :
8927 : : /* If this is not a case of ignoring a mismatch in signedness,
8928 : : no warning. */
8929 : 1948976 : else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
8930 : 1514177 : || target_cmp)
8931 : : ;
8932 : : /* If there is a mismatch, do warn. */
8933 : 1452 : else if (warn_pointer_sign)
8934 : 82 : switch (errtype)
8935 : : {
8936 : 33 : case ic_argpass:
8937 : 33 : {
8938 : 33 : auto_diagnostic_group d;
8939 : 33 : range_label_for_type_mismatch rhs_label (rhstype, type);
8940 : 33 : gcc_rich_location richloc (expr_loc, &rhs_label,
8941 : 33 : highlight_colors::actual);
8942 : 33 : if (pedwarn (&richloc, OPT_Wpointer_sign,
8943 : : "pointer targets in passing argument %d of "
8944 : : "%qE differ in signedness", parmnum, rname))
8945 : 33 : inform_for_arg (fundecl, expr_loc, parmnum, type,
8946 : : rhstype);
8947 : 33 : }
8948 : 33 : break;
8949 : 21 : case ic_assign:
8950 : 21 : pedwarn (location, OPT_Wpointer_sign,
8951 : : "pointer targets in assignment from %qT to %qT "
8952 : : "differ in signedness", rhstype, type);
8953 : 21 : break;
8954 : 14 : case ic_init:
8955 : 14 : case ic_init_const:
8956 : 14 : pedwarn_init (location, OPT_Wpointer_sign,
8957 : : "pointer targets in initialization of %qT "
8958 : : "from %qT differ in signedness", type,
8959 : : rhstype);
8960 : 14 : break;
8961 : 14 : case ic_return:
8962 : 14 : pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
8963 : : "returning %qT from a function with return type "
8964 : : "%qT differ in signedness", rhstype, type);
8965 : 14 : break;
8966 : 0 : default:
8967 : 0 : gcc_unreachable ();
8968 : : }
8969 : : }
8970 : 33080 : else if (TREE_CODE (ttl) == FUNCTION_TYPE
8971 : 4119 : && TREE_CODE (ttr) == FUNCTION_TYPE)
8972 : : {
8973 : : /* Because const and volatile on functions are restrictions
8974 : : that say the function will not do certain things,
8975 : : it is okay to use a const or volatile function
8976 : : where an ordinary one is wanted, but not vice-versa. */
8977 : 3454 : if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
8978 : 3454 : & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8979 : 18 : PEDWARN_FOR_QUALIFIERS (location, expr_loc,
8980 : : OPT_Wdiscarded_qualifiers,
8981 : : G_("passing argument %d of %qE makes "
8982 : : "%q#v qualified function pointer "
8983 : : "from unqualified"),
8984 : : G_("assignment makes %q#v qualified function "
8985 : : "pointer from unqualified"),
8986 : : G_("initialization makes %q#v qualified "
8987 : : "function pointer from unqualified"),
8988 : : G_("return makes %q#v qualified function "
8989 : : "pointer from unqualified"),
8990 : : TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
8991 : : }
8992 : : }
8993 : : /* Avoid warning about the volatile ObjC EH puts on decls. */
8994 : 799 : else if (!objc_ok)
8995 : : {
8996 : 799 : auto_diagnostic_group d;
8997 : 799 : bool warned = false;
8998 : 799 : pp_markup::element_expected_type e_type (type);
8999 : 799 : pp_markup::element_actual_type e_rhstype (rhstype);
9000 : 799 : switch (errtype)
9001 : : {
9002 : 306 : case ic_argpass:
9003 : 306 : {
9004 : 306 : range_label_for_type_mismatch rhs_label (rhstype, type);
9005 : 306 : gcc_rich_location richloc (expr_loc, &rhs_label,
9006 : 306 : highlight_colors::actual);
9007 : 306 : warned
9008 : 306 : = permerror_opt (&richloc, OPT_Wincompatible_pointer_types,
9009 : : "passing argument %d of %qE from "
9010 : : "incompatible pointer type",
9011 : : parmnum, rname);
9012 : 306 : if (warned)
9013 : 174 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9014 : 306 : }
9015 : 306 : break;
9016 : 267 : case ic_assign:
9017 : 267 : if (bltin)
9018 : 11 : warned
9019 : 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9020 : : "assignment to %e from pointer to "
9021 : : "%qD with incompatible type %e",
9022 : : &e_type, bltin, &e_rhstype);
9023 : : else
9024 : 256 : warned
9025 : 256 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9026 : : "assignment to %e from incompatible "
9027 : : "pointer type %e",
9028 : : &e_type, &e_rhstype);
9029 : : break;
9030 : 167 : case ic_init:
9031 : 167 : case ic_init_const:
9032 : 167 : if (bltin)
9033 : 13 : warned
9034 : 13 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9035 : : "initialization of %e from pointer to "
9036 : : "%qD with incompatible type %e",
9037 : : &e_type, bltin, &e_rhstype);
9038 : : else
9039 : 154 : warned
9040 : 154 : = permerror_init (location, OPT_Wincompatible_pointer_types,
9041 : : "initialization of %e from incompatible "
9042 : : "pointer type %e",
9043 : : &e_type, &e_rhstype);
9044 : : break;
9045 : 59 : case ic_return:
9046 : 59 : if (bltin)
9047 : 11 : warned
9048 : 11 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9049 : : "returning pointer to %qD of type %e from "
9050 : : "a function with incompatible type %e",
9051 : : bltin, &e_rhstype, &e_type);
9052 : : else
9053 : 48 : warned
9054 : 48 : = permerror_opt (location, OPT_Wincompatible_pointer_types,
9055 : : "returning %e from a function with "
9056 : : "incompatible return type %e",
9057 : : &e_rhstype, &e_type);
9058 : : break;
9059 : 0 : default:
9060 : 0 : gcc_unreachable ();
9061 : : }
9062 : 799 : if (warned)
9063 : : {
9064 : : /* If the mismatching function type is a pointer to a function,
9065 : : try to show the decl of the function. */
9066 : 589 : if (TREE_CODE (rhs) == ADDR_EXPR
9067 : 589 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL)
9068 : : {
9069 : 191 : tree rhs_fndecl = TREE_OPERAND (rhs, 0);
9070 : 191 : if (!DECL_IS_UNDECLARED_BUILTIN (rhs_fndecl))
9071 : : {
9072 : 165 : gcc_rich_location richloc
9073 : 165 : (DECL_SOURCE_LOCATION (rhs_fndecl), nullptr,
9074 : 165 : highlight_colors::actual);
9075 : 165 : pp_element_quoted_decl e_rhs_fndecl
9076 : 165 : (rhs_fndecl, highlight_colors::actual);
9077 : 165 : inform (&richloc,
9078 : : "%e declared here", &e_rhs_fndecl);
9079 : 165 : }
9080 : : }
9081 : : /* If either/both of the types are typedefs, show the decl. */
9082 : 589 : maybe_inform_typedef_location (type,
9083 : : highlight_colors::expected);
9084 : 589 : maybe_inform_typedef_location (rhstype,
9085 : : highlight_colors::actual);
9086 : : }
9087 : 799 : }
9088 : :
9089 : : /* If RHS isn't an address, check pointer or array of packed
9090 : : struct or union. */
9091 : 1985726 : warn_for_address_of_packed_member (type, orig_rhs);
9092 : :
9093 : 1985726 : return convert (type, rhs);
9094 : : }
9095 : 79248 : else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
9096 : : {
9097 : : /* ??? This should not be an error when inlining calls to
9098 : : unprototyped functions. */
9099 : 4 : const char msg[] = "invalid use of non-lvalue array";
9100 : 4 : if (warnopt)
9101 : 0 : warning_at (location, warnopt, msg);
9102 : : else
9103 : 4 : error_at (location, msg);
9104 : 4 : return error_mark_node;
9105 : : }
9106 : 79244 : else if (codel == POINTER_TYPE
9107 : 77970 : && (coder == INTEGER_TYPE
9108 : 77970 : || coder == ENUMERAL_TYPE
9109 : 607 : || coder == BOOLEAN_TYPE
9110 : 607 : || coder == NULLPTR_TYPE
9111 : 25 : || coder == BITINT_TYPE))
9112 : : {
9113 : 77957 : if (null_pointer_constant && c_inhibit_evaluation_warnings == 0
9114 : 76959 : && coder == INTEGER_TYPE)
9115 : 76360 : warning_at (location, OPT_Wzero_as_null_pointer_constant,
9116 : : "zero as null pointer constant");
9117 : : /* A NULLPTR type is just a nullptr always. */
9118 : 77957 : if (coder == NULLPTR_TYPE)
9119 : 570 : return omit_one_operand_loc (expr_loc, type, nullptr_node, rhs);
9120 : : /* An explicit constant 0 or type nullptr_t can convert to a pointer,
9121 : : or one that results from arithmetic, even including a cast to
9122 : : integer type. */
9123 : 77387 : else if (!null_pointer_constant)
9124 : 972 : switch (errtype)
9125 : : {
9126 : 800 : case ic_argpass:
9127 : 800 : {
9128 : 800 : auto_diagnostic_group d;
9129 : 800 : range_label_for_type_mismatch rhs_label (rhstype, type);
9130 : 800 : gcc_rich_location richloc (expr_loc, &rhs_label,
9131 : 800 : highlight_colors::actual);
9132 : 800 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9133 : : "passing argument %d of %qE makes pointer "
9134 : : "from integer without a cast", parmnum, rname))
9135 : : {
9136 : 439 : maybe_emit_indirection_note (expr_loc, rhs, type);
9137 : 439 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9138 : : }
9139 : 800 : }
9140 : 800 : break;
9141 : 93 : case ic_assign:
9142 : 93 : permerror_opt (location, OPT_Wint_conversion,
9143 : : "assignment to %qT from %qT makes pointer from "
9144 : : "integer without a cast", type, rhstype);
9145 : 93 : break;
9146 : 56 : case ic_init:
9147 : 56 : case ic_init_const:
9148 : 56 : permerror_init (location, OPT_Wint_conversion,
9149 : : "initialization of %qT from %qT makes pointer "
9150 : : "from integer without a cast", type, rhstype);
9151 : 56 : break;
9152 : 23 : case ic_return:
9153 : 23 : permerror_init (location, OPT_Wint_conversion,
9154 : : "returning %qT from a function with return type "
9155 : : "%qT makes pointer from integer without a cast",
9156 : : rhstype, type);
9157 : 23 : break;
9158 : 0 : default:
9159 : 0 : gcc_unreachable ();
9160 : : }
9161 : :
9162 : 77387 : return convert (type, rhs);
9163 : : }
9164 : 1287 : else if ((codel == INTEGER_TYPE || codel == BITINT_TYPE)
9165 : 539 : && coder == POINTER_TYPE)
9166 : : {
9167 : 513 : switch (errtype)
9168 : : {
9169 : 343 : case ic_argpass:
9170 : 343 : {
9171 : 343 : auto_diagnostic_group d;
9172 : 343 : range_label_for_type_mismatch rhs_label (rhstype, type);
9173 : 343 : gcc_rich_location richloc (expr_loc, &rhs_label,
9174 : 343 : highlight_colors::actual);
9175 : 343 : if (permerror_opt (&richloc, OPT_Wint_conversion,
9176 : : "passing argument %d of %qE makes integer from "
9177 : : "pointer without a cast", parmnum, rname))
9178 : : {
9179 : 322 : maybe_emit_indirection_note (expr_loc, rhs, type);
9180 : 322 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9181 : : }
9182 : 343 : }
9183 : 343 : break;
9184 : 52 : case ic_assign:
9185 : 52 : permerror_opt (location, OPT_Wint_conversion,
9186 : : "assignment to %qT from %qT makes integer from "
9187 : : "pointer without a cast", type, rhstype);
9188 : 52 : break;
9189 : 83 : case ic_init:
9190 : 83 : case ic_init_const:
9191 : 83 : permerror_init (location, OPT_Wint_conversion,
9192 : : "initialization of %qT from %qT makes integer "
9193 : : "from pointer without a cast", type, rhstype);
9194 : 83 : break;
9195 : 35 : case ic_return:
9196 : 35 : permerror_opt (location, OPT_Wint_conversion, "returning %qT from a "
9197 : : "function with return type %qT makes integer from "
9198 : : "pointer without a cast", rhstype, type);
9199 : 35 : break;
9200 : 0 : default:
9201 : 0 : gcc_unreachable ();
9202 : : }
9203 : :
9204 : 513 : return convert (type, rhs);
9205 : : }
9206 : 626 : else if (C_BOOLEAN_TYPE_P (type)
9207 : : /* The type nullptr_t may be converted to bool. The
9208 : : result is false. */
9209 : 776 : && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
9210 : : {
9211 : 150 : tree ret;
9212 : 150 : bool save = in_late_binary_op;
9213 : 150 : in_late_binary_op = true;
9214 : 150 : ret = convert (type, rhs);
9215 : 150 : in_late_binary_op = save;
9216 : 150 : return ret;
9217 : : }
9218 : 624 : else if (codel == NULLPTR_TYPE && null_pointer_constant)
9219 : 6 : return convert (type, rhs);
9220 : :
9221 : 618 : switch (errtype)
9222 : : {
9223 : 35 : case ic_argpass:
9224 : 35 : {
9225 : 35 : auto_diagnostic_group d;
9226 : 35 : range_label_for_type_mismatch rhs_label (rhstype, type);
9227 : 35 : gcc_rich_location richloc (expr_loc, &rhs_label,
9228 : 35 : highlight_colors::actual);
9229 : 35 : const char msg[] = G_("incompatible type for argument %d of %qE");
9230 : 35 : if (warnopt)
9231 : 8 : warning_at (expr_loc, warnopt, msg, parmnum, rname);
9232 : : else
9233 : 27 : error_at (&richloc, msg, parmnum, rname);
9234 : 35 : inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
9235 : 35 : }
9236 : 35 : break;
9237 : 108 : case ic_assign:
9238 : 108 : {
9239 : 108 : const char msg[]
9240 : : = G_("incompatible types when assigning to type %qT from type %qT");
9241 : 108 : if (warnopt)
9242 : 0 : warning_at (expr_loc, 0, msg, type, rhstype);
9243 : : else
9244 : 108 : error_at (expr_loc, msg, type, rhstype);
9245 : 108 : break;
9246 : : }
9247 : 460 : case ic_init:
9248 : 460 : case ic_init_const:
9249 : 460 : {
9250 : 460 : const char msg[]
9251 : : = G_("incompatible types when initializing type %qT using type %qT");
9252 : 460 : if (warnopt)
9253 : 0 : warning_at (location, 0, msg, type, rhstype);
9254 : : else
9255 : 460 : error_at (location, msg, type, rhstype);
9256 : 460 : break;
9257 : : }
9258 : 15 : case ic_return:
9259 : 15 : {
9260 : 15 : const char msg[]
9261 : : = G_("incompatible types when returning type %qT but %qT was expected");
9262 : 15 : if (warnopt)
9263 : 0 : warning_at (location, 0, msg, rhstype, type);
9264 : : else
9265 : 15 : error_at (location, msg, rhstype, type);
9266 : 15 : break;
9267 : : }
9268 : 0 : default:
9269 : 0 : gcc_unreachable ();
9270 : : }
9271 : :
9272 : 618 : return error_mark_node;
9273 : : }
9274 : :
9275 : : /* If VALUE is a compound expr all of whose expressions are constant, then
9276 : : return its value. Otherwise, return error_mark_node.
9277 : :
9278 : : This is for handling COMPOUND_EXPRs as initializer elements
9279 : : which is allowed with a warning when -pedantic is specified. */
9280 : :
9281 : : static tree
9282 : 2 : valid_compound_expr_initializer (tree value, tree endtype)
9283 : : {
9284 : 2 : if (TREE_CODE (value) == COMPOUND_EXPR)
9285 : : {
9286 : 1 : if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
9287 : 1 : == error_mark_node)
9288 : : return error_mark_node;
9289 : 0 : return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
9290 : 0 : endtype);
9291 : : }
9292 : 1 : else if (!initializer_constant_valid_p (value, endtype))
9293 : 1 : return error_mark_node;
9294 : : else
9295 : : return value;
9296 : : }
9297 : :
9298 : : /* Perform appropriate conversions on the initial value of a variable,
9299 : : store it in the declaration DECL,
9300 : : and print any error messages that are appropriate.
9301 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9302 : : If the init is invalid, store an ERROR_MARK.
9303 : :
9304 : : INIT_LOC is the location of the initial value. */
9305 : :
9306 : : void
9307 : 7099180 : store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
9308 : : {
9309 : 7099180 : tree value, type;
9310 : 7099180 : bool npc = false;
9311 : 7099180 : bool int_const_expr = false;
9312 : 7099180 : bool arith_const_expr = false;
9313 : :
9314 : : /* If variable's type was invalidly declared, just ignore it. */
9315 : :
9316 : 7099180 : type = TREE_TYPE (decl);
9317 : 7099180 : if (TREE_CODE (type) == ERROR_MARK)
9318 : : return;
9319 : :
9320 : : /* Digest the specified initializer into an expression. */
9321 : :
9322 : 7099173 : if (init)
9323 : : {
9324 : 7099173 : npc = null_pointer_constant_p (init);
9325 : 7099173 : int_const_expr = (TREE_CODE (init) == INTEGER_CST
9326 : 409687 : && !TREE_OVERFLOW (init)
9327 : 7508824 : && INTEGRAL_TYPE_P (TREE_TYPE (init)));
9328 : : /* Not fully determined before folding. */
9329 : : arith_const_expr = true;
9330 : : }
9331 : 7099173 : bool constexpr_p = (VAR_P (decl)
9332 : 7099173 : && C_DECL_DECLARED_CONSTEXPR (decl));
9333 : 7099173 : value = digest_init (init_loc, decl, type, init, origtype, npc,
9334 : : int_const_expr, arith_const_expr, true,
9335 : 7099173 : TREE_STATIC (decl) || constexpr_p, constexpr_p);
9336 : :
9337 : : /* Store the expression if valid; else report error. */
9338 : :
9339 : 7099173 : if (!in_system_header_at (input_location)
9340 : 7099173 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
9341 : 27490 : warning (OPT_Wtraditional, "traditional C rejects automatic "
9342 : : "aggregate initialization");
9343 : :
9344 : 7099173 : if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
9345 : 7099172 : DECL_INITIAL (decl) = value;
9346 : :
9347 : : /* ANSI wants warnings about out-of-range constant initializers. */
9348 : 7099184 : STRIP_TYPE_NOPS (value);
9349 : 7099173 : if (TREE_STATIC (decl))
9350 : 179951 : constant_expression_warning (value);
9351 : :
9352 : : /* Check if we need to set array size from compound literal size. */
9353 : 7099173 : if (TREE_CODE (type) == ARRAY_TYPE
9354 : 26135 : && TYPE_DOMAIN (type) == NULL_TREE
9355 : 7111593 : && value != error_mark_node)
9356 : : {
9357 : : tree inside_init = init;
9358 : :
9359 : 11548 : STRIP_TYPE_NOPS (inside_init);
9360 : 11548 : inside_init = fold (inside_init);
9361 : :
9362 : 11548 : if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9363 : : {
9364 : 13 : tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9365 : :
9366 : 13 : if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
9367 : : {
9368 : : /* For int foo[] = (int [3]){1}; we need to set array size
9369 : : now since later on array initializer will be just the
9370 : : brace enclosed list of the compound literal. */
9371 : 13 : tree etype = strip_array_types (TREE_TYPE (decl));
9372 : 13 : type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
9373 : 13 : TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
9374 : 13 : layout_type (type);
9375 : 13 : layout_decl (cldecl, 0);
9376 : 13 : TREE_TYPE (decl)
9377 : 26 : = c_build_qualified_type (type, TYPE_QUALS (etype));
9378 : : }
9379 : : }
9380 : : }
9381 : : }
9382 : :
9383 : : /* Methods for storing and printing names for error messages. */
9384 : :
9385 : : /* Implement a spelling stack that allows components of a name to be pushed
9386 : : and popped. Each element on the stack is this structure. */
9387 : :
9388 : : struct spelling
9389 : : {
9390 : : int kind;
9391 : : union
9392 : : {
9393 : : unsigned HOST_WIDE_INT i;
9394 : : const char *s;
9395 : : } u;
9396 : : };
9397 : :
9398 : : #define SPELLING_STRING 1
9399 : : #define SPELLING_MEMBER 2
9400 : : #define SPELLING_BOUNDS 3
9401 : :
9402 : : static struct spelling *spelling; /* Next stack element (unused). */
9403 : : static struct spelling *spelling_base; /* Spelling stack base. */
9404 : : static int spelling_size; /* Size of the spelling stack. */
9405 : :
9406 : : /* Macros to save and restore the spelling stack around push_... functions.
9407 : : Alternative to SAVE_SPELLING_STACK. */
9408 : :
9409 : : #define SPELLING_DEPTH() (spelling - spelling_base)
9410 : : #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
9411 : :
9412 : : /* Push an element on the spelling stack with type KIND and assign VALUE
9413 : : to MEMBER. */
9414 : :
9415 : : #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
9416 : : { \
9417 : : int depth = SPELLING_DEPTH (); \
9418 : : \
9419 : : if (depth >= spelling_size) \
9420 : : { \
9421 : : spelling_size += 10; \
9422 : : spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
9423 : : spelling_size); \
9424 : : RESTORE_SPELLING_DEPTH (depth); \
9425 : : } \
9426 : : \
9427 : : spelling->kind = (KIND); \
9428 : : spelling->MEMBER = (VALUE); \
9429 : : spelling++; \
9430 : : }
9431 : :
9432 : : /* Push STRING on the stack. Printed literally. */
9433 : :
9434 : : static void
9435 : 7096490 : push_string (const char *string)
9436 : : {
9437 : 7096490 : PUSH_SPELLING (SPELLING_STRING, string, u.s);
9438 : 7096490 : }
9439 : :
9440 : : /* Push a member name on the stack. Printed as '.' STRING. */
9441 : :
9442 : : static void
9443 : 1225487 : push_member_name (tree decl)
9444 : : {
9445 : 1225487 : const char *const string
9446 : 1225487 : = (DECL_NAME (decl)
9447 : 1225487 : ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
9448 : 204 : : _("<anonymous>"));
9449 : 1225487 : PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
9450 : 1225487 : }
9451 : :
9452 : : /* Push an array bounds on the stack. Printed as [BOUNDS]. */
9453 : :
9454 : : static void
9455 : 2597909 : push_array_bounds (unsigned HOST_WIDE_INT bounds)
9456 : : {
9457 : 2597909 : PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
9458 : 2597909 : }
9459 : :
9460 : : /* Compute the maximum size in bytes of the printed spelling. */
9461 : :
9462 : : static int
9463 : 2891 : spelling_length (void)
9464 : : {
9465 : 2891 : int size = 0;
9466 : 2891 : struct spelling *p;
9467 : :
9468 : 6415 : for (p = spelling_base; p < spelling; p++)
9469 : : {
9470 : 3524 : if (p->kind == SPELLING_BOUNDS)
9471 : 1479 : size += 25;
9472 : : else
9473 : 2045 : size += strlen (p->u.s) + 1;
9474 : : }
9475 : :
9476 : 2891 : return size;
9477 : : }
9478 : :
9479 : : /* Print the spelling to BUFFER and return it. */
9480 : :
9481 : : static char *
9482 : 2891 : print_spelling (char *buffer)
9483 : : {
9484 : 2891 : char *d = buffer;
9485 : 2891 : struct spelling *p;
9486 : :
9487 : 6415 : for (p = spelling_base; p < spelling; p++)
9488 : 3524 : if (p->kind == SPELLING_BOUNDS)
9489 : : {
9490 : 1479 : sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
9491 : 1479 : d += strlen (d);
9492 : : }
9493 : : else
9494 : : {
9495 : 2045 : const char *s;
9496 : 2045 : if (p->kind == SPELLING_MEMBER)
9497 : 198 : *d++ = '.';
9498 : 16715 : for (s = p->u.s; (*d = *s++); d++)
9499 : : ;
9500 : : }
9501 : 2891 : *d++ = '\0';
9502 : 2891 : return buffer;
9503 : : }
9504 : :
9505 : : /* Check whether INIT, a floating or integer constant, is
9506 : : representable in TYPE, a real floating type with the same radix or
9507 : : a decimal floating type initialized with a binary floating
9508 : : constant. Return true if OK, false if not. */
9509 : : static bool
9510 : 362 : constexpr_init_fits_real_type (tree type, tree init)
9511 : : {
9512 : 362 : gcc_assert (SCALAR_FLOAT_TYPE_P (type));
9513 : 362 : gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
9514 : 362 : if (TREE_CODE (init) == REAL_CST
9515 : 362 : && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
9516 : : {
9517 : : /* Same mode, no conversion required except for the case of
9518 : : signaling NaNs if the types are incompatible (e.g. double and
9519 : : long double with the same mode). */
9520 : 177 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init))
9521 : 195 : && !comptypes (TYPE_MAIN_VARIANT (type),
9522 : 18 : TYPE_MAIN_VARIANT (TREE_TYPE (init))))
9523 : : return false;
9524 : 177 : return true;
9525 : : }
9526 : 185 : if (TREE_CODE (init) == INTEGER_CST)
9527 : : {
9528 : 54 : tree converted = build_real_from_int_cst (type, init);
9529 : 54 : bool fail = false;
9530 : 108 : wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
9531 : 54 : TYPE_PRECISION (TREE_TYPE (init)));
9532 : 68 : return !fail && wi::eq_p (w, wi::to_wide (init));
9533 : 54 : }
9534 : 131 : if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init)))
9535 : : return false;
9536 : 114 : if ((REAL_VALUE_ISINF (TREE_REAL_CST (init))
9537 : 34 : && MODE_HAS_INFINITIES (TYPE_MODE (type)))
9538 : 218 : || (REAL_VALUE_ISNAN (TREE_REAL_CST (init))
9539 : 34 : && MODE_HAS_NANS (TYPE_MODE (type))))
9540 : 20 : return true;
9541 : 94 : if (DECIMAL_FLOAT_TYPE_P (type)
9542 : 134 : && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
9543 : : {
9544 : : /* This is valid if the real number represented by the
9545 : : initializer can be exactly represented in the decimal
9546 : : type. Compare the values using MPFR. */
9547 : 8 : REAL_VALUE_TYPE t;
9548 : 8 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9549 : 8 : mpfr_t bin_val, dec_val;
9550 : 8 : mpfr_init2 (bin_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9551 : 8 : mpfr_init2 (dec_val, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init)))->p);
9552 : 8 : mpfr_from_real (bin_val, &TREE_REAL_CST (init), MPFR_RNDN);
9553 : 8 : char string[256];
9554 : 8 : real_to_decimal (string, &t, sizeof string, 0, 1);
9555 : 8 : bool res = (mpfr_strtofr (dec_val, string, NULL, 10, MPFR_RNDN) == 0
9556 : 8 : && mpfr_equal_p (bin_val, dec_val));
9557 : 8 : mpfr_clear (bin_val);
9558 : 8 : mpfr_clear (dec_val);
9559 : 8 : return res;
9560 : : }
9561 : : /* exact_real_truncate is not quite right here, since it doesn't
9562 : : allow even an exact conversion to subnormal values. */
9563 : 86 : REAL_VALUE_TYPE t;
9564 : 86 : real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
9565 : 86 : return real_identical (&t, &TREE_REAL_CST (init));
9566 : : }
9567 : :
9568 : : /* Check whether INIT (location LOC) is valid as a 'constexpr'
9569 : : initializer for type TYPE, and give an error if not. INIT has
9570 : : already been folded and verified to be constant. INT_CONST_EXPR
9571 : : and ARITH_CONST_EXPR say whether it is an integer constant
9572 : : expression or arithmetic constant expression, respectively. If
9573 : : TYPE is not a scalar type, this function does nothing. */
9574 : :
9575 : : static void
9576 : 906 : check_constexpr_init (location_t loc, tree type, tree init,
9577 : : bool int_const_expr, bool arith_const_expr)
9578 : : {
9579 : 906 : if (POINTER_TYPE_P (type))
9580 : : {
9581 : : /* The initializer must be null. */
9582 : 86 : if (TREE_CODE (init) != INTEGER_CST || !integer_zerop (init))
9583 : 8 : error_at (loc, "%<constexpr%> pointer initializer is not null");
9584 : 86 : return;
9585 : : }
9586 : 820 : if (INTEGRAL_TYPE_P (type))
9587 : : {
9588 : : /* The initializer must be an integer constant expression,
9589 : : representable in the target type. */
9590 : 312 : if (!int_const_expr)
9591 : : {
9592 : 13 : if (TREE_CODE (init) == RAW_DATA_CST
9593 : 13 : && TYPE_PRECISION (type) == CHAR_BIT)
9594 : : {
9595 : 4 : if (!TYPE_UNSIGNED (type))
9596 : 137 : for (unsigned int i = 0;
9597 : 140 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
9598 : 138 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
9599 : : {
9600 : 1 : error_at (loc, "%<constexpr%> initializer not "
9601 : : "representable in type of object");
9602 : 1 : break;
9603 : : }
9604 : : }
9605 : : else
9606 : 9 : error_at (loc, "%<constexpr%> integer initializer is not an "
9607 : : "integer constant expression");
9608 : : }
9609 : 299 : else if (!int_fits_type_p (init, type))
9610 : 6 : error_at (loc, "%<constexpr%> initializer not representable in "
9611 : : "type of object");
9612 : 312 : return;
9613 : : }
9614 : : /* We don't apply any extra checks to extension types such as vector
9615 : : or fixed-point types. */
9616 : 508 : if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
9617 : : return;
9618 : 353 : if (!arith_const_expr)
9619 : : {
9620 : 5 : error_at (loc, "%<constexpr%> initializer is not an arithmetic "
9621 : : "constant expression");
9622 : 5 : return;
9623 : : }
9624 : : /* We don't apply any extra checks to complex integers. */
9625 : 348 : if (TREE_CODE (type) == COMPLEX_TYPE
9626 : 348 : && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
9627 : : return;
9628 : : /* Following N3082, a real type cannot be initialized from a complex
9629 : : type and a binary type cannot be initialized from a decimal type
9630 : : (but initializing a decimal type from a binary type is OK).
9631 : : Signaling NaN initializers are OK only if the types are
9632 : : compatible (not just the same mode); all quiet NaN and infinity
9633 : : initializations are considered to preserve the value. */
9634 : 348 : if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
9635 : 348 : && SCALAR_FLOAT_TYPE_P (type))
9636 : : {
9637 : 6 : error_at (loc, "%<constexpr%> initializer for a real type is of "
9638 : : "complex type");
9639 : 6 : return;
9640 : : }
9641 : 342 : if (SCALAR_FLOAT_TYPE_P (type)
9642 : 300 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init))
9643 : 250 : && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
9644 : 481 : && !DECIMAL_FLOAT_TYPE_P (type))
9645 : : {
9646 : 6 : error_at (loc, "%<constexpr%> initializer for a binary "
9647 : : "floating-point type is of decimal type");
9648 : 6 : return;
9649 : : }
9650 : 336 : bool fits;
9651 : 336 : if (TREE_CODE (type) == COMPLEX_TYPE)
9652 : : {
9653 : 42 : switch (TREE_CODE (init))
9654 : : {
9655 : 10 : case INTEGER_CST:
9656 : 10 : case REAL_CST:
9657 : 10 : fits = constexpr_init_fits_real_type (TREE_TYPE (type), init);
9658 : 10 : break;
9659 : 32 : case COMPLEX_CST:
9660 : 32 : fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
9661 : 32 : TREE_REALPART (init))
9662 : 58 : && constexpr_init_fits_real_type (TREE_TYPE (type),
9663 : 26 : TREE_IMAGPART (init)));
9664 : : break;
9665 : 0 : default:
9666 : 0 : gcc_unreachable ();
9667 : : }
9668 : : }
9669 : : else
9670 : 294 : fits = constexpr_init_fits_real_type (type, init);
9671 : 304 : if (!fits)
9672 : 65 : error_at (loc, "%<constexpr%> initializer not representable in "
9673 : : "type of object");
9674 : : }
9675 : :
9676 : : /* Digest the parser output INIT as an initializer for type TYPE
9677 : : initializing DECL.
9678 : : Return a C expression of type TYPE to represent the initial value.
9679 : :
9680 : : If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
9681 : :
9682 : : NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
9683 : : INT_CONST_EXPR is true if INIT is an integer constant expression,
9684 : : and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
9685 : : constant expression, false if it has already been determined in the
9686 : : caller that it is not (but folding may have made the value passed here
9687 : : indistinguishable from an arithmetic constant expression).
9688 : :
9689 : : If INIT is a string constant, STRICT_STRING is true if it is
9690 : : unparenthesized or we should not warn here for it being parenthesized.
9691 : : For other types of INIT, STRICT_STRING is not used.
9692 : :
9693 : : INIT_LOC is the location of the INIT.
9694 : :
9695 : : REQUIRE_CONSTANT requests an error if non-constant initializers or
9696 : : elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
9697 : : on initializers for 'constexpr' objects apply. */
9698 : :
9699 : : static tree
9700 : 16559963 : digest_init (location_t init_loc, tree decl, tree type, tree init,
9701 : : tree origtype, bool null_pointer_constant, bool int_const_expr,
9702 : : bool arith_const_expr, bool strict_string,
9703 : : bool require_constant, bool require_constexpr)
9704 : : {
9705 : 16559963 : enum tree_code code = TREE_CODE (type);
9706 : 16559963 : tree inside_init = init;
9707 : 16559963 : tree semantic_type = NULL_TREE;
9708 : 16559963 : bool maybe_const = true;
9709 : :
9710 : 16559963 : if (type == error_mark_node
9711 : 16559963 : || !init
9712 : 33119926 : || error_operand_p (init))
9713 : : return error_mark_node;
9714 : :
9715 : 16567185 : STRIP_TYPE_NOPS (inside_init);
9716 : :
9717 : : /* If require_constant is TRUE, when the initializer is a call to
9718 : : .ACCESS_WITH_SIZE, use the first argument as the initializer.
9719 : : For example:
9720 : : y = (char *) .ACCESS_WITH_SIZE ((char *) &static_annotated.c,...)
9721 : : will be converted to
9722 : : y = &static_annotated.c. */
9723 : :
9724 : 16558637 : if (require_constant
9725 : 2889794 : && TREE_CODE (inside_init) == NOP_EXPR
9726 : 736217 : && TREE_CODE (TREE_OPERAND (inside_init, 0)) == CALL_EXPR
9727 : 16558639 : && is_access_with_size_p (TREE_OPERAND (inside_init, 0)))
9728 : 2 : inside_init
9729 : 2 : = get_ref_from_access_with_size (TREE_OPERAND (inside_init, 0));
9730 : :
9731 : 16558637 : if (!c_in_omp_for)
9732 : : {
9733 : 16553900 : if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
9734 : : {
9735 : 437 : semantic_type = TREE_TYPE (inside_init);
9736 : 437 : inside_init = TREE_OPERAND (inside_init, 0);
9737 : : }
9738 : 16553900 : inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
9739 : : }
9740 : : /* TODO: this may not detect all cases of expressions folding to
9741 : : constants that are not arithmetic constant expressions. */
9742 : 16558637 : if (!maybe_const)
9743 : : arith_const_expr = false;
9744 : 24744981 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
9745 : 5565093 : && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
9746 : 16535239 : && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
9747 : : arith_const_expr = false;
9748 : 8237678 : else if (TREE_CODE (inside_init) != INTEGER_CST
9749 : : && TREE_CODE (inside_init) != REAL_CST
9750 : : && TREE_CODE (inside_init) != COMPLEX_CST
9751 : : && TREE_CODE (inside_init) != RAW_DATA_CST)
9752 : : arith_const_expr = false;
9753 : 4980907 : else if (TREE_OVERFLOW (inside_init))
9754 : 11577774 : arith_const_expr = false;
9755 : :
9756 : : /* Initialization of an array of chars from a string constant
9757 : : optionally enclosed in braces. */
9758 : :
9759 : 16558637 : if (code == ARRAY_TYPE && inside_init
9760 : 187812 : && TREE_CODE (inside_init) == STRING_CST)
9761 : : {
9762 : 11603 : tree typ1
9763 : 11603 : = (TYPE_ATOMIC (TREE_TYPE (type))
9764 : 11603 : ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
9765 : : TYPE_QUAL_ATOMIC)
9766 : 11589 : : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
9767 : : /* Note that an array could be both an array of character type
9768 : : and an array of wchar_t if wchar_t is signed char or unsigned
9769 : : char. */
9770 : 23206 : bool char_array = (typ1 == char_type_node
9771 : 514 : || typ1 == signed_char_type_node
9772 : 12082 : || typ1 == unsigned_char_type_node);
9773 : 11603 : bool wchar_array = !!comptypes (typ1, wchar_type_node);
9774 : 11603 : bool char16_array = !!comptypes (typ1, char16_type_node);
9775 : 11603 : bool char32_array = !!comptypes (typ1, char32_type_node);
9776 : :
9777 : 11603 : if (char_array || wchar_array || char16_array || char32_array)
9778 : : {
9779 : 11574 : struct c_expr expr;
9780 : 11574 : tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
9781 : 11574 : bool incompat_string_cst = false;
9782 : 11574 : expr.value = inside_init;
9783 : 11574 : expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
9784 : 11574 : expr.original_type = NULL;
9785 : 11574 : expr.m_decimal = 0;
9786 : 11574 : maybe_warn_string_init (init_loc, type, expr);
9787 : :
9788 : 11574 : if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
9789 : 91 : pedwarn_init (init_loc, OPT_Wpedantic,
9790 : : "initialization of a flexible array member");
9791 : :
9792 : 11574 : if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9793 : 11574 : TYPE_MAIN_VARIANT (type)))
9794 : : return inside_init;
9795 : :
9796 : 4174 : if (char_array)
9797 : : {
9798 : 4063 : if (typ2 != char_type_node && typ2 != char8_type_node)
9799 : : incompat_string_cst = true;
9800 : : }
9801 : 111 : else if (!comptypes (typ1, typ2))
9802 : : incompat_string_cst = true;
9803 : :
9804 : : if (incompat_string_cst)
9805 : : {
9806 : 72 : error_init (init_loc, "cannot initialize array of %qT from "
9807 : : "a string literal with type array of %qT",
9808 : : typ1, typ2);
9809 : 72 : return error_mark_node;
9810 : : }
9811 : :
9812 : 4102 : if (require_constexpr
9813 : 4102 : && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
9814 : : {
9815 : : /* Check if all characters of the string can be
9816 : : represented in the type of the constexpr object being
9817 : : initialized. */
9818 : 24 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9819 : 24 : const unsigned char *p =
9820 : 24 : (const unsigned char *) TREE_STRING_POINTER (inside_init);
9821 : 24 : gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
9822 : 70 : for (unsigned i = 0; i < len; i++)
9823 : 58 : if (p[i] > 127)
9824 : : {
9825 : 12 : error_init (init_loc, "%<constexpr%> initializer not "
9826 : : "representable in type of object");
9827 : 12 : break;
9828 : : }
9829 : : }
9830 : :
9831 : 4102 : if (TYPE_DOMAIN (type) != NULL_TREE
9832 : 4010 : && TYPE_SIZE (type) != NULL_TREE
9833 : 8053 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9834 : : {
9835 : 3951 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
9836 : :
9837 : 3951 : if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
9838 : : {
9839 : 395 : unsigned HOST_WIDE_INT avail
9840 : 395 : = tree_to_uhwi (TYPE_SIZE_UNIT (type));
9841 : 395 : unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
9842 : 395 : const char *p = TREE_STRING_POINTER (inside_init);
9843 : :
9844 : : /* Construct truncated string. */
9845 : 395 : inside_init = build_string (avail, p);
9846 : :
9847 : : /* Subtract the size of a single (possibly wide) character
9848 : : because it may be ok to ignore the terminating NUL char
9849 : : that is counted in the length of the constant. */
9850 : 395 : if (len - unit > avail)
9851 : 63 : pedwarn_init (init_loc, 0,
9852 : : "initializer-string for array of %qT "
9853 : : "is too long (%wu chars into %wu "
9854 : : "available)", typ1, len, avail);
9855 : 332 : else if (warn_cxx_compat)
9856 : 9 : warning_at (init_loc, OPT_Wc___compat,
9857 : : "initializer-string for array of %qT "
9858 : : "is too long for C++ (%wu chars into %wu "
9859 : : "available)", typ1, len, avail);
9860 : 323 : else if (warn_unterminated_string_initialization
9861 : 323 : && get_attr_nonstring_decl (decl) == NULL_TREE)
9862 : 22 : warning_at (init_loc,
9863 : 22 : OPT_Wunterminated_string_initialization,
9864 : : "initializer-string for array of %qT "
9865 : : "truncates NUL terminator but destination "
9866 : : "lacks %qs attribute (%wu chars into %wu "
9867 : : "available)", typ1, "nonstring", len, avail);
9868 : : }
9869 : : }
9870 : :
9871 : 4102 : TREE_TYPE (inside_init) = type;
9872 : 4102 : return inside_init;
9873 : : }
9874 : 29 : else if (INTEGRAL_TYPE_P (typ1))
9875 : : {
9876 : 29 : error_init (init_loc, "array of inappropriate type initialized "
9877 : : "from string constant");
9878 : 29 : return error_mark_node;
9879 : : }
9880 : : }
9881 : :
9882 : : /* Build a VECTOR_CST from a *constant* vector constructor. If the
9883 : : vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
9884 : : below and handle as a constructor. */
9885 : 16547034 : if (code == VECTOR_TYPE
9886 : 6154416 : && VECTOR_TYPE_P (TREE_TYPE (inside_init))
9887 : 6154414 : && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
9888 : 22701410 : && TREE_CONSTANT (inside_init))
9889 : : {
9890 : 618498 : if (TREE_CODE (inside_init) == VECTOR_CST
9891 : 618595 : && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9892 : 97 : TYPE_MAIN_VARIANT (type)))
9893 : : return inside_init;
9894 : :
9895 : 618401 : if (TREE_CODE (inside_init) == CONSTRUCTOR)
9896 : : {
9897 : : unsigned HOST_WIDE_INT ix;
9898 : : tree value;
9899 : 4081709 : bool constant_p = true;
9900 : :
9901 : : /* Iterate through elements and check if all constructor
9902 : : elements are *_CSTs. */
9903 : 4081709 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
9904 : 3463364 : if (!CONSTANT_CLASS_P (value))
9905 : : {
9906 : : constant_p = false;
9907 : : break;
9908 : : }
9909 : :
9910 : 618401 : if (constant_p)
9911 : 618345 : return build_vector_from_ctor (type,
9912 : 618345 : CONSTRUCTOR_ELTS (inside_init));
9913 : : }
9914 : : }
9915 : :
9916 : 15928592 : if (warn_sequence_point)
9917 : 2447057 : verify_sequence_points (inside_init);
9918 : :
9919 : : /* Any type can be initialized
9920 : : from an expression of the same type, optionally with braces. */
9921 : :
9922 : 15928592 : if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
9923 : 31857184 : && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
9924 : 15928592 : TYPE_MAIN_VARIANT (type))
9925 : 3074332 : || (code == ARRAY_TYPE
9926 : 477 : && comptypes (TREE_TYPE (inside_init), type))
9927 : 3074332 : || (gnu_vector_type_p (type)
9928 : 189 : && comptypes (TREE_TYPE (inside_init), type))
9929 : 3074332 : || (code == POINTER_TYPE
9930 : 111701 : && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
9931 : 9264 : && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
9932 : 9264 : TREE_TYPE (type)))))
9933 : : {
9934 : 12856234 : if (code == POINTER_TYPE)
9935 : : {
9936 : 783616 : if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
9937 : : {
9938 : 1974 : if (TREE_CODE (inside_init) == STRING_CST
9939 : 66 : || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9940 : 1972 : inside_init = array_to_pointer_conversion
9941 : 1972 : (init_loc, inside_init);
9942 : : else
9943 : : {
9944 : 2 : error_init (init_loc, "invalid use of non-lvalue array");
9945 : 2 : return error_mark_node;
9946 : : }
9947 : : }
9948 : : }
9949 : :
9950 : 12856232 : if (code == VECTOR_TYPE || c_hardbool_type_attr (type))
9951 : : /* Although the types are compatible, we may require a
9952 : : conversion. */
9953 : 5535918 : inside_init = convert (type, inside_init);
9954 : :
9955 : 12856232 : if ((code == RECORD_TYPE || code == UNION_TYPE)
9956 : 12856232 : && !comptypes (TYPE_MAIN_VARIANT (type), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init))))
9957 : : {
9958 : 0 : error_init (init_loc, "invalid initializer");
9959 : 0 : return error_mark_node;
9960 : : }
9961 : :
9962 : 12856232 : if (require_constant
9963 : 2297038 : && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
9964 : : {
9965 : : /* As an extension, allow initializing objects with static storage
9966 : : duration with compound literals (which are then treated just as
9967 : : the brace enclosed list they contain). Also allow this for
9968 : : vectors, as we can only assign them with compound literals. */
9969 : 31 : if (flag_isoc99 && code != VECTOR_TYPE)
9970 : 8 : pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
9971 : : "is not constant");
9972 : 31 : tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
9973 : 31 : inside_init = DECL_INITIAL (decl);
9974 : : }
9975 : :
9976 : 12856232 : if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
9977 : 175732 : && TREE_CODE (inside_init) != CONSTRUCTOR)
9978 : : {
9979 : 2 : error_init (init_loc, "array initialized from non-constant array "
9980 : : "expression");
9981 : 2 : return error_mark_node;
9982 : : }
9983 : :
9984 : : /* Compound expressions can only occur here if -Wpedantic or
9985 : : -pedantic-errors is specified. In the later case, we always want
9986 : : an error. In the former case, we simply want a warning. */
9987 : 12856230 : if (require_constant && pedantic
9988 : 20284 : && TREE_CODE (inside_init) == COMPOUND_EXPR)
9989 : : {
9990 : 1 : inside_init
9991 : 1 : = valid_compound_expr_initializer (inside_init,
9992 : 1 : TREE_TYPE (inside_init));
9993 : 1 : if (inside_init == error_mark_node)
9994 : 1 : error_init (init_loc, "initializer element is not constant");
9995 : : else
9996 : 0 : pedwarn_init (init_loc, OPT_Wpedantic,
9997 : : "initializer element is not constant");
9998 : 1 : if (flag_pedantic_errors)
9999 : 0 : inside_init = error_mark_node;
10000 : : }
10001 : 2297037 : else if (require_constant
10002 : 2297037 : && !initializer_constant_valid_p (inside_init,
10003 : 2297037 : TREE_TYPE (inside_init)))
10004 : : {
10005 : 94 : error_init (init_loc, "initializer element is not constant");
10006 : 94 : inside_init = error_mark_node;
10007 : : }
10008 : 12856135 : else if (require_constant && !maybe_const)
10009 : 219 : pedwarn_init (init_loc, OPT_Wpedantic,
10010 : : "initializer element is not a constant expression");
10011 : 12855916 : else if (require_constexpr)
10012 : 580 : check_constexpr_init (init_loc, type, inside_init,
10013 : : int_const_expr, arith_const_expr);
10014 : :
10015 : : /* Added to enable additional -Wsuggest-attribute=format warnings. */
10016 : 12856230 : if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
10017 : 851552 : inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
10018 : : type, inside_init, origtype,
10019 : : (require_constant
10020 : : ? ic_init_const
10021 : : : ic_init), null_pointer_constant,
10022 : : NULL_TREE, NULL_TREE, 0);
10023 : 12856230 : if (TREE_CODE (inside_init) == RAW_DATA_CST
10024 : 252 : && c_inhibit_evaluation_warnings == 0
10025 : 252 : && warn_conversion
10026 : 1 : && !TYPE_UNSIGNED (type)
10027 : 12856231 : && TYPE_PRECISION (type) == CHAR_BIT)
10028 : 303 : for (unsigned int i = 0;
10029 : 304 : i < (unsigned) RAW_DATA_LENGTH (inside_init); ++i)
10030 : 303 : if (RAW_DATA_SCHAR_ELT (inside_init, i) < 0)
10031 : 10 : warning_at (init_loc, OPT_Wconversion,
10032 : : "conversion from %qT to %qT changes value from "
10033 : : "%qd to %qd",
10034 : : integer_type_node, type,
10035 : 10 : RAW_DATA_UCHAR_ELT (inside_init, i),
10036 : 10 : RAW_DATA_SCHAR_ELT (inside_init, i));
10037 : 12856230 : return inside_init;
10038 : : }
10039 : :
10040 : : /* Handle scalar types, including conversions. */
10041 : :
10042 : 3072358 : if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
10043 : 127749 : || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
10044 : 11085 : || code == COMPLEX_TYPE || code == VECTOR_TYPE || code == NULLPTR_TYPE
10045 : 9022 : || code == BITINT_TYPE)
10046 : : {
10047 : 3071870 : tree unconverted_init = inside_init;
10048 : 3071870 : if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
10049 : 3071870 : && (TREE_CODE (init) == STRING_CST
10050 : 1 : || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
10051 : 7291 : inside_init = init = array_to_pointer_conversion (init_loc, init);
10052 : 3071870 : if (semantic_type)
10053 : 432 : inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10054 : : inside_init);
10055 : 3071870 : inside_init
10056 : 5565693 : = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
10057 : : inside_init, origtype,
10058 : : require_constant ? ic_init_const : ic_init,
10059 : : null_pointer_constant, NULL_TREE, NULL_TREE,
10060 : : 0);
10061 : :
10062 : : /* Check to see if we have already given an error message. */
10063 : 3071870 : if (inside_init == error_mark_node)
10064 : : ;
10065 : 3071410 : else if (require_constant && !TREE_CONSTANT (inside_init))
10066 : : {
10067 : 36 : error_init (init_loc, "initializer element is not constant");
10068 : 36 : inside_init = error_mark_node;
10069 : : }
10070 : 3071374 : else if (require_constant
10071 : 3648993 : && !initializer_constant_valid_p (inside_init,
10072 : 577619 : TREE_TYPE (inside_init)))
10073 : : {
10074 : 10 : error_init (init_loc, "initializer element is not computable at "
10075 : : "load time");
10076 : 10 : inside_init = error_mark_node;
10077 : : }
10078 : 3071364 : else if (require_constant && !maybe_const)
10079 : 5 : pedwarn_init (init_loc, OPT_Wpedantic,
10080 : : "initializer element is not a constant expression");
10081 : 3071359 : else if (require_constexpr)
10082 : 326 : check_constexpr_init (init_loc, type, unconverted_init,
10083 : : int_const_expr, arith_const_expr);
10084 : :
10085 : 3071870 : return inside_init;
10086 : : }
10087 : :
10088 : : /* Come here only for records and arrays. */
10089 : :
10090 : 488 : if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
10091 : : {
10092 : 0 : error_init (init_loc,
10093 : : "variable-sized object may not be initialized except "
10094 : : "with an empty initializer");
10095 : 0 : return error_mark_node;
10096 : : }
10097 : :
10098 : 488 : error_init (init_loc, "invalid initializer");
10099 : 488 : return error_mark_node;
10100 : : }
10101 : :
10102 : : /* Handle initializers that use braces. */
10103 : :
10104 : : /* Type of object we are accumulating a constructor for.
10105 : : This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
10106 : : static tree constructor_type;
10107 : :
10108 : : /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
10109 : : left to fill. */
10110 : : static tree constructor_fields;
10111 : :
10112 : : /* For an ARRAY_TYPE, this is the specified index
10113 : : at which to store the next element we get. */
10114 : : static tree constructor_index;
10115 : :
10116 : : /* For an ARRAY_TYPE, this is the maximum index. */
10117 : : static tree constructor_max_index;
10118 : :
10119 : : /* For a RECORD_TYPE, this is the first field not yet written out. */
10120 : : static tree constructor_unfilled_fields;
10121 : :
10122 : : /* For an ARRAY_TYPE, this is the index of the first element
10123 : : not yet written out. */
10124 : : static tree constructor_unfilled_index;
10125 : :
10126 : : /* In a RECORD_TYPE, the byte index of the next consecutive field.
10127 : : This is so we can generate gaps between fields, when appropriate. */
10128 : : static tree constructor_bit_index;
10129 : :
10130 : : /* If we are saving up the elements rather than allocating them,
10131 : : this is the list of elements so far (in reverse order,
10132 : : most recent first). */
10133 : : static vec<constructor_elt, va_gc> *constructor_elements;
10134 : :
10135 : : /* 1 if constructor should be incrementally stored into a constructor chain,
10136 : : 0 if all the elements should be kept in AVL tree. */
10137 : : static int constructor_incremental;
10138 : :
10139 : : /* 1 if so far this constructor's elements are all compile-time constants. */
10140 : : static int constructor_constant;
10141 : :
10142 : : /* 1 if so far this constructor's elements are all valid address constants. */
10143 : : static int constructor_simple;
10144 : :
10145 : : /* 1 if this constructor has an element that cannot be part of a
10146 : : constant expression. */
10147 : : static int constructor_nonconst;
10148 : :
10149 : : /* 1 if this constructor is erroneous so far. */
10150 : : static int constructor_erroneous;
10151 : :
10152 : : /* 1 if this constructor is the universal zero initializer { 0 }. */
10153 : : static int constructor_zeroinit;
10154 : :
10155 : : /* 1 if this constructor should have padding bits zeroed (C23 {}. */
10156 : : static bool constructor_zero_padding_bits;
10157 : :
10158 : : /* 1 if this constructor is a braced scalar initializer (further nested levels
10159 : : of braces are an error). */
10160 : : static bool constructor_braced_scalar;
10161 : :
10162 : : /* Structure for managing pending initializer elements, organized as an
10163 : : AVL tree. */
10164 : :
10165 : : struct init_node
10166 : : {
10167 : : struct init_node *left, *right;
10168 : : struct init_node *parent;
10169 : : int balance;
10170 : : tree purpose;
10171 : : tree value;
10172 : : tree origtype;
10173 : : };
10174 : :
10175 : : /* Tree of pending elements at this constructor level.
10176 : : These are elements encountered out of order
10177 : : which belong at places we haven't reached yet in actually
10178 : : writing the output.
10179 : : Will never hold tree nodes across GC runs. */
10180 : : static struct init_node *constructor_pending_elts;
10181 : :
10182 : : /* The SPELLING_DEPTH of this constructor. */
10183 : : static int constructor_depth;
10184 : :
10185 : : /* DECL node for which an initializer is being read.
10186 : : 0 means we are reading a constructor expression
10187 : : such as (struct foo) {...}. */
10188 : : static tree constructor_decl;
10189 : :
10190 : : /* Nonzero if there were any member designators in this initializer. */
10191 : : static int constructor_designated;
10192 : :
10193 : : /* Nesting depth of designator list. */
10194 : : static int designator_depth;
10195 : :
10196 : : /* Nonzero if there were diagnosed errors in this designator list. */
10197 : : static int designator_erroneous;
10198 : :
10199 : :
10200 : : /* This stack has a level for each implicit or explicit level of
10201 : : structuring in the initializer, including the outermost one. It
10202 : : saves the values of most of the variables above. */
10203 : :
10204 : : struct constructor_range_stack;
10205 : :
10206 : : struct constructor_stack
10207 : : {
10208 : : struct constructor_stack *next;
10209 : : tree type;
10210 : : tree fields;
10211 : : tree index;
10212 : : tree max_index;
10213 : : tree unfilled_index;
10214 : : tree unfilled_fields;
10215 : : tree bit_index;
10216 : : vec<constructor_elt, va_gc> *elements;
10217 : : struct init_node *pending_elts;
10218 : : int offset;
10219 : : int depth;
10220 : : /* If value nonzero, this value should replace the entire
10221 : : constructor at this level. */
10222 : : struct c_expr replacement_value;
10223 : : struct constructor_range_stack *range_stack;
10224 : : char constant;
10225 : : char simple;
10226 : : char nonconst;
10227 : : char implicit;
10228 : : char erroneous;
10229 : : char outer;
10230 : : char incremental;
10231 : : char designated;
10232 : : bool zero_padding_bits;
10233 : : bool braced_scalar;
10234 : : int designator_depth;
10235 : : };
10236 : :
10237 : : static struct constructor_stack *constructor_stack;
10238 : :
10239 : : /* This stack represents designators from some range designator up to
10240 : : the last designator in the list. */
10241 : :
10242 : : struct constructor_range_stack
10243 : : {
10244 : : struct constructor_range_stack *next, *prev;
10245 : : struct constructor_stack *stack;
10246 : : tree range_start;
10247 : : tree index;
10248 : : tree range_end;
10249 : : tree fields;
10250 : : };
10251 : :
10252 : : static struct constructor_range_stack *constructor_range_stack;
10253 : :
10254 : : /* This stack records separate initializers that are nested.
10255 : : Nested initializers can't happen in ANSI C, but GNU C allows them
10256 : : in cases like { ... (struct foo) { ... } ... }. */
10257 : :
10258 : : struct initializer_stack
10259 : : {
10260 : : struct initializer_stack *next;
10261 : : tree decl;
10262 : : struct constructor_stack *constructor_stack;
10263 : : struct constructor_range_stack *constructor_range_stack;
10264 : : vec<constructor_elt, va_gc> *elements;
10265 : : struct spelling *spelling;
10266 : : struct spelling *spelling_base;
10267 : : int spelling_size;
10268 : : char require_constant_value;
10269 : : char require_constant_elements;
10270 : : char require_constexpr_value;
10271 : : char designated;
10272 : : rich_location *missing_brace_richloc;
10273 : : };
10274 : :
10275 : : static struct initializer_stack *initializer_stack;
10276 : :
10277 : : /* Prepare to parse and output the initializer for variable DECL. */
10278 : :
10279 : : void
10280 : 7096490 : start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
10281 : : bool init_require_constant, bool init_require_constexpr,
10282 : : rich_location *richloc)
10283 : : {
10284 : 7096490 : const char *locus;
10285 : 7096490 : struct initializer_stack *p = XNEW (struct initializer_stack);
10286 : :
10287 : 7096490 : p->decl = constructor_decl;
10288 : 7096490 : p->require_constant_value = require_constant_value;
10289 : 7096490 : p->require_constant_elements = require_constant_elements;
10290 : 7096490 : p->require_constexpr_value = require_constexpr_value;
10291 : 7096490 : p->constructor_stack = constructor_stack;
10292 : 7096490 : p->constructor_range_stack = constructor_range_stack;
10293 : 7096490 : p->elements = constructor_elements;
10294 : 7096490 : p->spelling = spelling;
10295 : 7096490 : p->spelling_base = spelling_base;
10296 : 7096490 : p->spelling_size = spelling_size;
10297 : 7096490 : p->next = initializer_stack;
10298 : 7096490 : p->missing_brace_richloc = richloc;
10299 : 7096490 : p->designated = constructor_designated;
10300 : 7096490 : initializer_stack = p;
10301 : :
10302 : 7096490 : constructor_decl = decl;
10303 : 7096490 : constructor_designated = 0;
10304 : :
10305 : 7096490 : require_constant_value = init_require_constant;
10306 : 7096490 : require_constexpr_value = init_require_constexpr;
10307 : 7096490 : if (decl != NULL_TREE && decl != error_mark_node)
10308 : : {
10309 : 6196654 : require_constant_elements
10310 : 6019831 : = ((init_require_constant || (pedantic && !flag_isoc99))
10311 : : /* For a scalar, you can always use any value to initialize,
10312 : : even within braces. */
10313 : 6208608 : && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
10314 : 6196654 : locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
10315 : : }
10316 : : else
10317 : : {
10318 : 899836 : require_constant_elements = false;
10319 : 899836 : locus = _("(anonymous)");
10320 : : }
10321 : :
10322 : 7096490 : constructor_stack = 0;
10323 : 7096490 : constructor_range_stack = 0;
10324 : :
10325 : 7096490 : found_missing_braces = 0;
10326 : :
10327 : 7096490 : spelling_base = 0;
10328 : 7096490 : spelling_size = 0;
10329 : 7096490 : RESTORE_SPELLING_DEPTH (0);
10330 : :
10331 : 7096490 : if (locus)
10332 : 7096490 : push_string (locus);
10333 : 7096490 : }
10334 : :
10335 : : void
10336 : 7096488 : finish_init (void)
10337 : : {
10338 : 7096488 : struct initializer_stack *p = initializer_stack;
10339 : :
10340 : : /* Free the whole constructor stack of this initializer. */
10341 : 7096488 : while (constructor_stack)
10342 : : {
10343 : 0 : struct constructor_stack *q = constructor_stack;
10344 : 0 : constructor_stack = q->next;
10345 : 0 : XDELETE (q);
10346 : : }
10347 : :
10348 : 7096488 : gcc_assert (!constructor_range_stack);
10349 : :
10350 : : /* Pop back to the data of the outer initializer (if any). */
10351 : 7096488 : XDELETE (spelling_base);
10352 : :
10353 : 7096488 : constructor_decl = p->decl;
10354 : 7096488 : require_constant_value = p->require_constant_value;
10355 : 7096488 : require_constant_elements = p->require_constant_elements;
10356 : 7096488 : require_constexpr_value = p->require_constexpr_value;
10357 : 7096488 : constructor_stack = p->constructor_stack;
10358 : 7096488 : constructor_designated = p->designated;
10359 : 7096488 : constructor_range_stack = p->constructor_range_stack;
10360 : 7096488 : constructor_elements = p->elements;
10361 : 7096488 : spelling = p->spelling;
10362 : 7096488 : spelling_base = p->spelling_base;
10363 : 7096488 : spelling_size = p->spelling_size;
10364 : 7096488 : initializer_stack = p->next;
10365 : 7096488 : XDELETE (p);
10366 : 7096488 : }
10367 : :
10368 : : /* Call here when we see the initializer is surrounded by braces.
10369 : : This is instead of a call to push_init_level;
10370 : : it is matched by a call to pop_init_level.
10371 : :
10372 : : TYPE is the type to initialize, for a constructor expression.
10373 : : For an initializer for a decl, TYPE is zero. */
10374 : :
10375 : : void
10376 : 1002256 : really_start_incremental_init (tree type)
10377 : : {
10378 : 1002256 : struct constructor_stack *p = XNEW (struct constructor_stack);
10379 : :
10380 : 1002256 : if (type == NULL_TREE)
10381 : 104332 : type = TREE_TYPE (constructor_decl);
10382 : :
10383 : 1002256 : if (VECTOR_TYPE_P (type)
10384 : 1002256 : && TYPE_VECTOR_OPAQUE (type))
10385 : 0 : error ("opaque vector types cannot be initialized");
10386 : :
10387 : 1002256 : p->type = constructor_type;
10388 : 1002256 : p->fields = constructor_fields;
10389 : 1002256 : p->index = constructor_index;
10390 : 1002256 : p->max_index = constructor_max_index;
10391 : 1002256 : p->unfilled_index = constructor_unfilled_index;
10392 : 1002256 : p->unfilled_fields = constructor_unfilled_fields;
10393 : 1002256 : p->bit_index = constructor_bit_index;
10394 : 1002256 : p->elements = constructor_elements;
10395 : 1002256 : p->constant = constructor_constant;
10396 : 1002256 : p->simple = constructor_simple;
10397 : 1002256 : p->nonconst = constructor_nonconst;
10398 : 1002256 : p->erroneous = constructor_erroneous;
10399 : 1002256 : p->pending_elts = constructor_pending_elts;
10400 : 1002256 : p->depth = constructor_depth;
10401 : 1002256 : p->replacement_value.value = 0;
10402 : 1002256 : p->replacement_value.original_code = ERROR_MARK;
10403 : 1002256 : p->replacement_value.original_type = NULL;
10404 : 1002256 : p->implicit = 0;
10405 : 1002256 : p->range_stack = 0;
10406 : 1002256 : p->outer = 0;
10407 : 1002256 : p->incremental = constructor_incremental;
10408 : 1002256 : p->designated = constructor_designated;
10409 : 1002256 : p->zero_padding_bits = constructor_zero_padding_bits;
10410 : 1002256 : p->braced_scalar = constructor_braced_scalar;
10411 : 1002256 : p->designator_depth = designator_depth;
10412 : 1002256 : p->next = 0;
10413 : 1002256 : constructor_stack = p;
10414 : :
10415 : 1002256 : constructor_constant = 1;
10416 : 1002256 : constructor_simple = 1;
10417 : 1002256 : constructor_nonconst = 0;
10418 : 1002256 : constructor_depth = SPELLING_DEPTH ();
10419 : 1002256 : constructor_elements = NULL;
10420 : 1002256 : constructor_pending_elts = 0;
10421 : 1002256 : constructor_type = type;
10422 : 1002256 : constructor_incremental = 1;
10423 : 1002256 : constructor_designated = 0;
10424 : 1002256 : constructor_zero_padding_bits = false;
10425 : 1002256 : constructor_zeroinit = 1;
10426 : 1002256 : constructor_braced_scalar = false;
10427 : 1002256 : designator_depth = 0;
10428 : 1002256 : designator_erroneous = 0;
10429 : :
10430 : 1002256 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10431 : : {
10432 : 80349 : constructor_fields = TYPE_FIELDS (constructor_type);
10433 : : /* Skip any nameless bit fields at the beginning. */
10434 : 80349 : while (constructor_fields != NULL_TREE
10435 : 80377 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10436 : 28 : constructor_fields = DECL_CHAIN (constructor_fields);
10437 : :
10438 : 80349 : constructor_unfilled_fields = constructor_fields;
10439 : 80349 : constructor_bit_index = bitsize_zero_node;
10440 : : }
10441 : 921907 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10442 : : {
10443 : 17777 : if (TYPE_DOMAIN (constructor_type))
10444 : : {
10445 : 9357 : constructor_max_index
10446 : 9357 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10447 : :
10448 : : /* Detect non-empty initializations of zero-length arrays. */
10449 : 9357 : if (constructor_max_index == NULL_TREE
10450 : 9357 : && TYPE_SIZE (constructor_type))
10451 : 205 : constructor_max_index = integer_minus_one_node;
10452 : :
10453 : : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10454 : : to initialize VLAs with a nonempty initializer will cause a
10455 : : proper error; avoid tree checking errors as well by setting a
10456 : : safe value. */
10457 : 9357 : if (constructor_max_index
10458 : 9357 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10459 : 60 : constructor_max_index = integer_minus_one_node;
10460 : :
10461 : 9357 : constructor_index
10462 : 9357 : = convert (bitsizetype,
10463 : 9357 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10464 : : }
10465 : : else
10466 : : {
10467 : 8420 : constructor_index = bitsize_zero_node;
10468 : 8420 : constructor_max_index = NULL_TREE;
10469 : : }
10470 : :
10471 : 17777 : constructor_unfilled_index = constructor_index;
10472 : : }
10473 : 904130 : else if (gnu_vector_type_p (constructor_type))
10474 : : {
10475 : : /* Vectors are like simple fixed-size arrays. */
10476 : 1807160 : constructor_max_index =
10477 : 903580 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10478 : 903580 : constructor_index = bitsize_zero_node;
10479 : 903580 : constructor_unfilled_index = constructor_index;
10480 : : }
10481 : : else
10482 : : {
10483 : : /* Handle the case of int x = {5}; */
10484 : 550 : constructor_fields = constructor_type;
10485 : 550 : constructor_unfilled_fields = constructor_type;
10486 : 550 : constructor_braced_scalar = true;
10487 : : }
10488 : 1002256 : }
10489 : :
10490 : : extern location_t last_init_list_comma;
10491 : :
10492 : : /* Called when we see an open brace for a nested initializer. Finish
10493 : : off any pending levels with implicit braces. */
10494 : : void
10495 : 340343 : finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
10496 : : {
10497 : 340345 : while (constructor_stack->implicit)
10498 : : {
10499 : 304 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
10500 : 140 : && constructor_fields == NULL_TREE)
10501 : 0 : process_init_element (input_location,
10502 : : pop_init_level (loc, 1, braced_init_obstack,
10503 : : last_init_list_comma),
10504 : : true, braced_init_obstack);
10505 : 304 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE
10506 : 164 : && constructor_max_index
10507 : 468 : && tree_int_cst_lt (constructor_max_index,
10508 : : constructor_index))
10509 : 2 : process_init_element (input_location,
10510 : : pop_init_level (loc, 1, braced_init_obstack,
10511 : : last_init_list_comma),
10512 : : true, braced_init_obstack);
10513 : : else
10514 : : break;
10515 : : }
10516 : 340343 : }
10517 : :
10518 : : /* Push down into a subobject, for initialization.
10519 : : If this is for an explicit set of braces, IMPLICIT is 0.
10520 : : If it is because the next element belongs at a lower level,
10521 : : IMPLICIT is 1 (or 2 if the push is because of designator list). */
10522 : :
10523 : : void
10524 : 1042982 : push_init_level (location_t loc, int implicit,
10525 : : struct obstack *braced_init_obstack)
10526 : : {
10527 : 1042982 : struct constructor_stack *p;
10528 : 1042982 : tree value = NULL_TREE;
10529 : :
10530 : : /* Unless this is an explicit brace, we need to preserve previous
10531 : : content if any. */
10532 : 1042982 : if (implicit)
10533 : : {
10534 : 703581 : if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
10535 : 795 : value = find_init_member (constructor_fields, braced_init_obstack);
10536 : 702786 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10537 : 702786 : value = find_init_member (constructor_index, braced_init_obstack);
10538 : : }
10539 : :
10540 : 1042982 : p = XNEW (struct constructor_stack);
10541 : 1042982 : p->type = constructor_type;
10542 : 1042982 : p->fields = constructor_fields;
10543 : 1042982 : p->index = constructor_index;
10544 : 1042982 : p->max_index = constructor_max_index;
10545 : 1042982 : p->unfilled_index = constructor_unfilled_index;
10546 : 1042982 : p->unfilled_fields = constructor_unfilled_fields;
10547 : 1042982 : p->bit_index = constructor_bit_index;
10548 : 1042982 : p->elements = constructor_elements;
10549 : 1042982 : p->constant = constructor_constant;
10550 : 1042982 : p->simple = constructor_simple;
10551 : 1042982 : p->nonconst = constructor_nonconst;
10552 : 1042982 : p->erroneous = constructor_erroneous;
10553 : 1042982 : p->pending_elts = constructor_pending_elts;
10554 : 1042982 : p->depth = constructor_depth;
10555 : 1042982 : p->replacement_value.value = NULL_TREE;
10556 : 1042982 : p->replacement_value.original_code = ERROR_MARK;
10557 : 1042982 : p->replacement_value.original_type = NULL;
10558 : 1042982 : p->implicit = implicit;
10559 : 1042982 : p->outer = 0;
10560 : 1042982 : p->incremental = constructor_incremental;
10561 : 1042982 : p->designated = constructor_designated;
10562 : 1042982 : p->zero_padding_bits = constructor_zero_padding_bits;
10563 : 1042982 : p->braced_scalar = constructor_braced_scalar;
10564 : 1042982 : p->designator_depth = designator_depth;
10565 : 1042982 : p->next = constructor_stack;
10566 : 1042982 : p->range_stack = 0;
10567 : 1042982 : constructor_stack = p;
10568 : :
10569 : 1042982 : constructor_constant = 1;
10570 : 1042982 : constructor_simple = 1;
10571 : 1042982 : constructor_nonconst = 0;
10572 : 1042982 : constructor_depth = SPELLING_DEPTH ();
10573 : 1042982 : constructor_elements = NULL;
10574 : 1042982 : constructor_incremental = 1;
10575 : : /* If the upper initializer is designated, then mark this as
10576 : : designated too to prevent bogus warnings. */
10577 : 1042982 : constructor_designated = p->designated;
10578 : : /* If the upper initializer has padding bits zeroed, that includes
10579 : : all nested initializers as well. */
10580 : 1042982 : constructor_zero_padding_bits = p->zero_padding_bits;
10581 : 1042982 : constructor_braced_scalar = false;
10582 : 1042982 : constructor_pending_elts = 0;
10583 : 1042982 : if (!implicit)
10584 : : {
10585 : 339401 : p->range_stack = constructor_range_stack;
10586 : 339401 : constructor_range_stack = 0;
10587 : 339401 : designator_depth = 0;
10588 : 339401 : designator_erroneous = 0;
10589 : : }
10590 : :
10591 : : /* Don't die if an entire brace-pair level is superfluous
10592 : : in the containing level. */
10593 : 1042982 : if (constructor_type == NULL_TREE)
10594 : : ;
10595 : 1042982 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10596 : : {
10597 : : /* Don't die if there are extra init elts at the end. */
10598 : 158797 : if (constructor_fields == NULL_TREE)
10599 : 51 : constructor_type = NULL_TREE;
10600 : : else
10601 : : {
10602 : 158746 : constructor_type = TREE_TYPE (constructor_fields);
10603 : 158746 : push_member_name (constructor_fields);
10604 : 158746 : constructor_depth++;
10605 : : }
10606 : : }
10607 : 884185 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10608 : : {
10609 : 884134 : constructor_type = TREE_TYPE (constructor_type);
10610 : 884134 : push_array_bounds (tree_to_uhwi (constructor_index));
10611 : 884134 : constructor_depth++;
10612 : : }
10613 : :
10614 : 1042982 : if (constructor_type == NULL_TREE)
10615 : : {
10616 : 51 : error_init (loc, "extra brace group at end of initializer");
10617 : 51 : constructor_fields = NULL_TREE;
10618 : 51 : constructor_unfilled_fields = NULL_TREE;
10619 : 51 : return;
10620 : : }
10621 : :
10622 : 1042931 : if (value && TREE_CODE (value) == CONSTRUCTOR)
10623 : : {
10624 : 360 : constructor_constant = TREE_CONSTANT (value);
10625 : 360 : constructor_simple = TREE_STATIC (value);
10626 : 360 : constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
10627 : 360 : constructor_elements = CONSTRUCTOR_ELTS (value);
10628 : 360 : constructor_zero_padding_bits |= CONSTRUCTOR_ZERO_PADDING_BITS (value);
10629 : 360 : if (!vec_safe_is_empty (constructor_elements)
10630 : 340 : && (TREE_CODE (constructor_type) == RECORD_TYPE
10631 : 340 : || TREE_CODE (constructor_type) == ARRAY_TYPE))
10632 : 294 : set_nonincremental_init (braced_init_obstack);
10633 : : }
10634 : :
10635 : 1042931 : if (implicit == 1)
10636 : : {
10637 : 702639 : found_missing_braces = 1;
10638 : 702639 : if (initializer_stack->missing_brace_richloc)
10639 : 702639 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10640 : 702639 : (loc, "{");
10641 : : }
10642 : :
10643 : 1042931 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
10644 : : {
10645 : 879636 : constructor_fields = TYPE_FIELDS (constructor_type);
10646 : : /* Skip any nameless bit fields at the beginning. */
10647 : 879636 : while (constructor_fields != NULL_TREE
10648 : 879700 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10649 : 64 : constructor_fields = DECL_CHAIN (constructor_fields);
10650 : :
10651 : 879636 : constructor_unfilled_fields = constructor_fields;
10652 : 879636 : constructor_bit_index = bitsize_zero_node;
10653 : : }
10654 : 163295 : else if (gnu_vector_type_p (constructor_type))
10655 : : {
10656 : : /* Vectors are like simple fixed-size arrays. */
10657 : 9600 : constructor_max_index =
10658 : 4800 : bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
10659 : 4800 : constructor_index = bitsize_int (0);
10660 : 4800 : constructor_unfilled_index = constructor_index;
10661 : : }
10662 : 158495 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10663 : : {
10664 : 158415 : if (TYPE_DOMAIN (constructor_type))
10665 : : {
10666 : 158415 : constructor_max_index
10667 : 158415 : = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
10668 : :
10669 : : /* Detect non-empty initializations of zero-length arrays. */
10670 : 158415 : if (constructor_max_index == NULL_TREE
10671 : 158415 : && TYPE_SIZE (constructor_type))
10672 : 135 : constructor_max_index = integer_minus_one_node;
10673 : :
10674 : : /* constructor_max_index needs to be an INTEGER_CST. Attempts
10675 : : to initialize VLAs will cause a proper error; avoid tree
10676 : : checking errors as well by setting a safe value. */
10677 : 158415 : if (constructor_max_index
10678 : 158146 : && TREE_CODE (constructor_max_index) != INTEGER_CST)
10679 : 0 : constructor_max_index = integer_minus_one_node;
10680 : :
10681 : 158415 : constructor_index
10682 : 158415 : = convert (bitsizetype,
10683 : 158415 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
10684 : : }
10685 : : else
10686 : 0 : constructor_index = bitsize_zero_node;
10687 : :
10688 : 158415 : constructor_unfilled_index = constructor_index;
10689 : 158415 : if (value && TREE_CODE (value) == STRING_CST)
10690 : : {
10691 : : /* We need to split the char/wchar array into individual
10692 : : characters, so that we don't have to special case it
10693 : : everywhere. */
10694 : 7 : set_nonincremental_init_from_string (value, braced_init_obstack);
10695 : : }
10696 : : }
10697 : : else
10698 : : {
10699 : 80 : if (constructor_type != error_mark_node)
10700 : : {
10701 : 51 : if (p->braced_scalar)
10702 : 22 : permerror_init (input_location, 0,
10703 : : "braces around scalar initializer");
10704 : : else
10705 : 29 : warning_init (input_location, 0,
10706 : : "braces around scalar initializer");
10707 : 51 : constructor_braced_scalar = true;
10708 : : }
10709 : 80 : constructor_fields = constructor_type;
10710 : 80 : constructor_unfilled_fields = constructor_type;
10711 : : }
10712 : : }
10713 : :
10714 : : /* At the end of an implicit or explicit brace level,
10715 : : finish up that level of constructor. If a single expression
10716 : : with redundant braces initialized that level, return the
10717 : : c_expr structure for that expression. Otherwise, the original_code
10718 : : element is set to ERROR_MARK.
10719 : : If we were outputting the elements as they are read, return 0 as the value
10720 : : from inner levels (process_init_element ignores that),
10721 : : but return error_mark_node as the value from the outermost level
10722 : : (that's what we want to put in DECL_INITIAL).
10723 : : Otherwise, return a CONSTRUCTOR expression as the value. */
10724 : :
10725 : : struct c_expr
10726 : 2045238 : pop_init_level (location_t loc, int implicit,
10727 : : struct obstack *braced_init_obstack,
10728 : : location_t insert_before)
10729 : : {
10730 : 2045238 : struct constructor_stack *p;
10731 : 2045238 : struct c_expr ret;
10732 : 2045238 : ret.value = NULL_TREE;
10733 : 2045238 : ret.original_code = ERROR_MARK;
10734 : 2045238 : ret.original_type = NULL;
10735 : 2045238 : ret.m_decimal = 0;
10736 : :
10737 : 2045238 : if (implicit == 0)
10738 : : {
10739 : : /* When we come to an explicit close brace,
10740 : : pop any inner levels that didn't have explicit braces. */
10741 : 1342557 : while (constructor_stack->implicit)
10742 : 900 : process_init_element (input_location,
10743 : : pop_init_level (loc, 1, braced_init_obstack,
10744 : : insert_before),
10745 : : true, braced_init_obstack);
10746 : 1341657 : gcc_assert (!constructor_range_stack);
10747 : : }
10748 : : else
10749 : 703581 : if (initializer_stack->missing_brace_richloc)
10750 : 703581 : initializer_stack->missing_brace_richloc->add_fixit_insert_before
10751 : 703581 : (insert_before, "}");
10752 : :
10753 : : /* Now output all pending elements. */
10754 : 2045238 : constructor_incremental = 1;
10755 : 2045238 : output_pending_init_elements (1, braced_init_obstack);
10756 : :
10757 : 2045238 : p = constructor_stack;
10758 : :
10759 : : /* Error for initializing a flexible array member, or a zero-length
10760 : : array member in an inappropriate context. */
10761 : 2045187 : if (constructor_type && constructor_fields
10762 : 159268 : && TREE_CODE (constructor_type) == ARRAY_TYPE
10763 : 152555 : && TYPE_DOMAIN (constructor_type)
10764 : 2197785 : && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
10765 : : {
10766 : : /* Silently discard empty initializations. The parser will
10767 : : already have pedwarned for empty brackets for C17 and earlier. */
10768 : 299 : if (integer_zerop (constructor_unfilled_index))
10769 : 84 : constructor_type = NULL_TREE;
10770 : 299 : if (constructor_type || flag_isoc23)
10771 : : {
10772 : 295 : gcc_assert (!constructor_type || !TYPE_SIZE (constructor_type));
10773 : :
10774 : 295 : if (constructor_depth <= 2)
10775 : 248 : pedwarn_init (loc, OPT_Wpedantic,
10776 : : "initialization of a flexible array member");
10777 : 47 : else if (constructor_type)
10778 : 33 : error_init (loc, "initialization of flexible array member "
10779 : : "in a nested context");
10780 : : else
10781 : 14 : pedwarn_init (loc, OPT_Wpedantic,
10782 : : "initialization of flexible array member "
10783 : : "in a nested context");
10784 : :
10785 : : /* We have already issued an error message for the existence
10786 : : of a flexible array member not at the end of the structure.
10787 : : Discard the initializer so that we do not die later. */
10788 : 295 : if (constructor_type
10789 : 215 : && DECL_CHAIN (constructor_fields) != NULL_TREE
10790 : 301 : && (!p->type || TREE_CODE (p->type) != UNION_TYPE))
10791 : 0 : constructor_type = NULL_TREE;
10792 : : }
10793 : : }
10794 : :
10795 : 2045238 : switch (vec_safe_length (constructor_elements))
10796 : : {
10797 : 4687 : case 0:
10798 : : /* Initialization with { } counts as zeroinit. */
10799 : 4687 : constructor_zeroinit = 1;
10800 : 4687 : break;
10801 : 914006 : case 1:
10802 : : /* This might be zeroinit as well. */
10803 : 914006 : if (integer_zerop ((*constructor_elements)[0].value))
10804 : 1868 : constructor_zeroinit = 1;
10805 : : break;
10806 : 1126545 : default:
10807 : : /* If the constructor has more than one element, it can't be { 0 }. */
10808 : 1126545 : constructor_zeroinit = 0;
10809 : 1126545 : break;
10810 : : }
10811 : :
10812 : : /* Warn when some structs are initialized with direct aggregation. */
10813 : 2045238 : if (!implicit && found_missing_braces && warn_missing_braces
10814 : 37 : && !constructor_zeroinit)
10815 : : {
10816 : 18 : gcc_assert (initializer_stack->missing_brace_richloc);
10817 : 18 : warning_at (initializer_stack->missing_brace_richloc,
10818 : 18 : OPT_Wmissing_braces,
10819 : : "missing braces around initializer");
10820 : : }
10821 : :
10822 : : /* Warn when some struct elements are implicitly initialized to zero. */
10823 : 2045238 : if (warn_missing_field_initializers
10824 : 425443 : && constructor_type
10825 : 425415 : && TREE_CODE (constructor_type) == RECORD_TYPE
10826 : 193395 : && constructor_unfilled_fields)
10827 : : {
10828 : : /* Do not warn for flexible array members or zero-length arrays. */
10829 : 43 : while (constructor_unfilled_fields
10830 : 43 : && (!DECL_SIZE (constructor_unfilled_fields)
10831 : 43 : || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
10832 : 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
10833 : :
10834 : 43 : if (constructor_unfilled_fields
10835 : : /* Do not warn if this level of the initializer uses member
10836 : : designators; it is likely to be deliberate. */
10837 : 43 : && !constructor_designated
10838 : : /* Do not warn about initializing with { 0 } or with { }. */
10839 : 24 : && !constructor_zeroinit)
10840 : : {
10841 : 10 : if (warning_at (input_location, OPT_Wmissing_field_initializers,
10842 : : "missing initializer for field %qD of %qT",
10843 : : constructor_unfilled_fields,
10844 : : constructor_type))
10845 : 10 : inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
10846 : : "%qD declared here", constructor_unfilled_fields);
10847 : : }
10848 : : }
10849 : :
10850 : : /* Pad out the end of the structure. */
10851 : 2045238 : if (p->replacement_value.value)
10852 : : /* If this closes a superfluous brace pair,
10853 : : just pass out the element between them. */
10854 : 137 : ret = p->replacement_value;
10855 : 2045101 : else if (constructor_type == NULL_TREE)
10856 : : ;
10857 : 2044974 : else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
10858 : 2044974 : && TREE_CODE (constructor_type) != ARRAY_TYPE
10859 : 2044974 : && !gnu_vector_type_p (constructor_type))
10860 : : {
10861 : : /* A nonincremental scalar initializer--just return
10862 : : the element, after verifying there is just one.
10863 : : Empty scalar initializers are supported in C23. */
10864 : 630 : if (vec_safe_is_empty (constructor_elements))
10865 : : {
10866 : 210 : if (constructor_erroneous || constructor_type == error_mark_node)
10867 : 94 : ret.value = error_mark_node;
10868 : 116 : else if (TREE_CODE (constructor_type) == FUNCTION_TYPE)
10869 : : {
10870 : 2 : error_init (loc, "invalid initializer");
10871 : 2 : ret.value = error_mark_node;
10872 : : }
10873 : 114 : else if (TREE_CODE (constructor_type) == POINTER_TYPE)
10874 : : /* Ensure this is a null pointer constant in the case of a
10875 : : 'constexpr' object initialized with {}. */
10876 : 33 : ret.value = build_zero_cst (ptr_type_node);
10877 : : else
10878 : 81 : ret.value = build_zero_cst (constructor_type);
10879 : : }
10880 : 420 : else if (vec_safe_length (constructor_elements) != 1)
10881 : : {
10882 : 0 : error_init (loc, "extra elements in scalar initializer");
10883 : 0 : ret.value = (*constructor_elements)[0].value;
10884 : : }
10885 : : else
10886 : 420 : ret.value = (*constructor_elements)[0].value;
10887 : : }
10888 : : else
10889 : : {
10890 : 2044344 : if (constructor_erroneous)
10891 : 506 : ret.value = error_mark_node;
10892 : : else
10893 : : {
10894 : 2043838 : ret.value = build_constructor (constructor_type,
10895 : : constructor_elements);
10896 : 2043838 : if (constructor_constant)
10897 : 1722420 : TREE_CONSTANT (ret.value) = 1;
10898 : 2043838 : if (constructor_constant && constructor_simple)
10899 : 1722381 : TREE_STATIC (ret.value) = 1;
10900 : 2043838 : if (constructor_nonconst)
10901 : 641 : CONSTRUCTOR_NON_CONST (ret.value) = 1;
10902 : 2043838 : if (constructor_zero_padding_bits)
10903 : 20 : CONSTRUCTOR_ZERO_PADDING_BITS (ret.value) = 1;
10904 : : }
10905 : : }
10906 : :
10907 : 2045238 : if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
10908 : : {
10909 : 1273 : if (constructor_nonconst)
10910 : 15 : ret.original_code = C_MAYBE_CONST_EXPR;
10911 : 1258 : else if (ret.original_code == C_MAYBE_CONST_EXPR)
10912 : 0 : ret.original_code = ERROR_MARK;
10913 : : }
10914 : :
10915 : 2045238 : constructor_type = p->type;
10916 : 2045238 : constructor_fields = p->fields;
10917 : 2045238 : constructor_index = p->index;
10918 : 2045238 : constructor_max_index = p->max_index;
10919 : 2045238 : constructor_unfilled_index = p->unfilled_index;
10920 : 2045238 : constructor_unfilled_fields = p->unfilled_fields;
10921 : 2045238 : constructor_bit_index = p->bit_index;
10922 : 2045238 : constructor_elements = p->elements;
10923 : 2045238 : constructor_constant = p->constant;
10924 : 2045238 : constructor_simple = p->simple;
10925 : 2045238 : constructor_nonconst = p->nonconst;
10926 : 2045238 : constructor_erroneous = p->erroneous;
10927 : 2045238 : constructor_incremental = p->incremental;
10928 : 2045238 : constructor_designated = p->designated;
10929 : 2045238 : constructor_zero_padding_bits = p->zero_padding_bits;
10930 : 2045238 : constructor_braced_scalar = p->braced_scalar;
10931 : 2045238 : designator_depth = p->designator_depth;
10932 : 2045238 : constructor_pending_elts = p->pending_elts;
10933 : 2045238 : constructor_depth = p->depth;
10934 : 2045238 : if (!p->implicit)
10935 : 1341657 : constructor_range_stack = p->range_stack;
10936 : 2045238 : RESTORE_SPELLING_DEPTH (constructor_depth);
10937 : :
10938 : 2045238 : constructor_stack = p->next;
10939 : 2045238 : XDELETE (p);
10940 : :
10941 : 2045238 : if (ret.value == NULL_TREE && constructor_stack == 0)
10942 : 0 : ret.value = error_mark_node;
10943 : 2045238 : return ret;
10944 : : }
10945 : :
10946 : : /* Common handling for both array range and field name designators.
10947 : : ARRAY argument is nonzero for array ranges. Returns false for success. */
10948 : :
10949 : : static bool
10950 : 33094 : set_designator (location_t loc, bool array,
10951 : : struct obstack *braced_init_obstack)
10952 : : {
10953 : 33094 : tree subtype;
10954 : 33094 : enum tree_code subcode;
10955 : :
10956 : : /* Don't die if an entire brace-pair level is superfluous
10957 : : in the containing level, or for an erroneous type. */
10958 : 33094 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10959 : : return true;
10960 : :
10961 : : /* If there were errors in this designator list already, bail out
10962 : : silently. */
10963 : 33085 : if (designator_erroneous)
10964 : : return true;
10965 : :
10966 : : /* Likewise for an initializer for a variable-size type. Those are
10967 : : diagnosed in the parser, except for empty initializer braces. */
10968 : 33085 : if (COMPLETE_TYPE_P (constructor_type)
10969 : 33085 : && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
10970 : : return true;
10971 : :
10972 : 33075 : if (!designator_depth)
10973 : : {
10974 : 32457 : gcc_assert (!constructor_range_stack);
10975 : :
10976 : : /* Designator list starts at the level of closest explicit
10977 : : braces. */
10978 : 32840 : while (constructor_stack->implicit)
10979 : 383 : process_init_element (input_location,
10980 : : pop_init_level (loc, 1, braced_init_obstack,
10981 : : last_init_list_comma),
10982 : : true, braced_init_obstack);
10983 : 32457 : constructor_designated = 1;
10984 : 32457 : return false;
10985 : : }
10986 : :
10987 : 618 : switch (TREE_CODE (constructor_type))
10988 : : {
10989 : 421 : case RECORD_TYPE:
10990 : 421 : case UNION_TYPE:
10991 : 421 : subtype = TREE_TYPE (constructor_fields);
10992 : 421 : if (subtype != error_mark_node)
10993 : 421 : subtype = TYPE_MAIN_VARIANT (subtype);
10994 : : break;
10995 : 197 : case ARRAY_TYPE:
10996 : 197 : subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10997 : 197 : break;
10998 : 0 : default:
10999 : 0 : gcc_unreachable ();
11000 : : }
11001 : :
11002 : 618 : subcode = TREE_CODE (subtype);
11003 : 618 : if (array && subcode != ARRAY_TYPE)
11004 : : {
11005 : 1 : error_init (loc, "array index in non-array initializer");
11006 : 1 : return true;
11007 : : }
11008 : 617 : else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
11009 : : {
11010 : 0 : error_init (loc, "field name not in record or union initializer");
11011 : 0 : return true;
11012 : : }
11013 : :
11014 : 617 : constructor_designated = 1;
11015 : 617 : finish_implicit_inits (loc, braced_init_obstack);
11016 : 617 : push_init_level (loc, 2, braced_init_obstack);
11017 : 617 : return false;
11018 : : }
11019 : :
11020 : : /* If there are range designators in designator list, push a new designator
11021 : : to constructor_range_stack. RANGE_END is end of such stack range or
11022 : : NULL_TREE if there is no range designator at this level. */
11023 : :
11024 : : static void
11025 : 384 : push_range_stack (tree range_end, struct obstack * braced_init_obstack)
11026 : : {
11027 : 384 : struct constructor_range_stack *p;
11028 : :
11029 : 768 : p = (struct constructor_range_stack *)
11030 : 384 : obstack_alloc (braced_init_obstack,
11031 : : sizeof (struct constructor_range_stack));
11032 : 384 : p->prev = constructor_range_stack;
11033 : 384 : p->next = 0;
11034 : 384 : p->fields = constructor_fields;
11035 : 384 : p->range_start = constructor_index;
11036 : 384 : p->index = constructor_index;
11037 : 384 : p->stack = constructor_stack;
11038 : 384 : p->range_end = range_end;
11039 : 384 : if (constructor_range_stack)
11040 : 19 : constructor_range_stack->next = p;
11041 : 384 : constructor_range_stack = p;
11042 : 384 : }
11043 : :
11044 : : /* Within an array initializer, specify the next index to be initialized.
11045 : : FIRST is that index. If LAST is nonzero, then initialize a range
11046 : : of indices, running from FIRST through LAST. */
11047 : :
11048 : : void
11049 : 1143 : set_init_index (location_t loc, tree first, tree last,
11050 : : struct obstack *braced_init_obstack)
11051 : : {
11052 : 1143 : if (set_designator (loc, true, braced_init_obstack))
11053 : : return;
11054 : :
11055 : 1140 : designator_erroneous = 1;
11056 : :
11057 : 2280 : if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
11058 : 2269 : || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
11059 : : {
11060 : 12 : error_init (loc, "array index in initializer not of integer type");
11061 : 12 : return;
11062 : : }
11063 : :
11064 : 1128 : if (TREE_CODE (first) != INTEGER_CST)
11065 : : {
11066 : 6 : first = c_fully_fold (first, false, NULL);
11067 : 6 : if (TREE_CODE (first) == INTEGER_CST)
11068 : 5 : pedwarn_init (loc, OPT_Wpedantic,
11069 : : "array index in initializer is not "
11070 : : "an integer constant expression");
11071 : : }
11072 : :
11073 : 1128 : if (last && TREE_CODE (last) != INTEGER_CST)
11074 : : {
11075 : 2 : last = c_fully_fold (last, false, NULL);
11076 : 2 : if (TREE_CODE (last) == INTEGER_CST)
11077 : 1 : pedwarn_init (loc, OPT_Wpedantic,
11078 : : "array index in initializer is not "
11079 : : "an integer constant expression");
11080 : : }
11081 : :
11082 : 1128 : if (TREE_CODE (first) != INTEGER_CST)
11083 : 1 : error_init (loc, "nonconstant array index in initializer");
11084 : 1127 : else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
11085 : 1 : error_init (loc, "nonconstant array index in initializer");
11086 : 1126 : else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
11087 : 9 : error_init (loc, "array index in non-array initializer");
11088 : 1117 : else if (tree_int_cst_sgn (first) == -1)
11089 : 15 : error_init (loc, "array index in initializer exceeds array bounds");
11090 : 1102 : else if (constructor_max_index
11091 : 1102 : && tree_int_cst_lt (constructor_max_index, first))
11092 : 7 : error_init (loc, "array index in initializer exceeds array bounds");
11093 : : else
11094 : : {
11095 : 1095 : constant_expression_warning (first);
11096 : 1095 : if (last)
11097 : 379 : constant_expression_warning (last);
11098 : 1095 : constructor_index = convert (bitsizetype, first);
11099 : 1095 : if (tree_int_cst_lt (constructor_index, first))
11100 : : {
11101 : 0 : constructor_index = copy_node (constructor_index);
11102 : 0 : TREE_OVERFLOW (constructor_index) = 1;
11103 : : }
11104 : :
11105 : 1095 : if (last)
11106 : : {
11107 : 379 : if (tree_int_cst_equal (first, last))
11108 : : last = NULL_TREE;
11109 : 378 : else if (tree_int_cst_lt (last, first))
11110 : : {
11111 : 2 : error_init (loc, "empty index range in initializer");
11112 : 2 : last = NULL_TREE;
11113 : : }
11114 : : else
11115 : : {
11116 : 376 : last = convert (bitsizetype, last);
11117 : 376 : if (constructor_max_index != NULL_TREE
11118 : 376 : && tree_int_cst_lt (constructor_max_index, last))
11119 : : {
11120 : 3 : error_init (loc, "array index range in initializer exceeds "
11121 : : "array bounds");
11122 : 3 : last = NULL_TREE;
11123 : : }
11124 : : }
11125 : : }
11126 : :
11127 : 1095 : designator_depth++;
11128 : 1095 : designator_erroneous = 0;
11129 : 1095 : if (constructor_range_stack || last)
11130 : 373 : push_range_stack (last, braced_init_obstack);
11131 : : }
11132 : : }
11133 : :
11134 : : /* Within a struct initializer, specify the next field to be initialized. */
11135 : :
11136 : : void
11137 : 31906 : set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
11138 : : struct obstack *braced_init_obstack)
11139 : : {
11140 : 31906 : tree field;
11141 : :
11142 : 31906 : if (set_designator (loc, false, braced_init_obstack))
11143 : : return;
11144 : :
11145 : 31889 : designator_erroneous = 1;
11146 : :
11147 : 31889 : if (!RECORD_OR_UNION_TYPE_P (constructor_type))
11148 : : {
11149 : 3 : error_init (loc, "field name not in record or union initializer");
11150 : 3 : return;
11151 : : }
11152 : :
11153 : 31886 : field = lookup_field (constructor_type, fieldname);
11154 : :
11155 : 31886 : if (field == NULL_TREE)
11156 : : {
11157 : 8 : tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
11158 : 8 : if (guessed_id)
11159 : : {
11160 : 4 : gcc_rich_location rich_loc (fieldname_loc);
11161 : 4 : rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
11162 : 4 : error_at (&rich_loc,
11163 : : "%qT has no member named %qE; did you mean %qE?",
11164 : : constructor_type, fieldname, guessed_id);
11165 : 4 : }
11166 : : else
11167 : 4 : error_at (fieldname_loc, "%qT has no member named %qE",
11168 : : constructor_type, fieldname);
11169 : : }
11170 : : else
11171 : 31923 : do
11172 : : {
11173 : 31923 : constructor_fields = TREE_VALUE (field);
11174 : 31923 : designator_depth++;
11175 : 31923 : designator_erroneous = 0;
11176 : 31923 : if (constructor_range_stack)
11177 : 11 : push_range_stack (NULL_TREE, braced_init_obstack);
11178 : 31923 : field = TREE_CHAIN (field);
11179 : 31923 : if (field)
11180 : : {
11181 : 45 : if (set_designator (loc, false, braced_init_obstack))
11182 : : return;
11183 : : }
11184 : : }
11185 : 31923 : while (field != NULL_TREE);
11186 : : }
11187 : :
11188 : : /* Helper function for add_pending_init. Find inorder successor of P
11189 : : in AVL tree. */
11190 : : static struct init_node *
11191 : 129 : init_node_successor (struct init_node *p)
11192 : : {
11193 : 129 : struct init_node *r;
11194 : 129 : if (p->right)
11195 : : {
11196 : : r = p->right;
11197 : 58 : while (r->left)
11198 : : r = r->left;
11199 : : return r;
11200 : : }
11201 : 75 : r = p->parent;
11202 : 114 : while (r && p == r->right)
11203 : : {
11204 : 39 : p = r;
11205 : 39 : r = r->parent;
11206 : : }
11207 : : return r;
11208 : : }
11209 : :
11210 : : /* Add a new initializer to the tree of pending initializers. PURPOSE
11211 : : identifies the initializer, either array index or field in a structure.
11212 : : VALUE is the value of that index or field. If ORIGTYPE is not
11213 : : NULL_TREE, it is the original type of VALUE.
11214 : :
11215 : : IMPLICIT is true if value comes from pop_init_level (1),
11216 : : the new initializer has been merged with the existing one
11217 : : and thus no warnings should be emitted about overriding an
11218 : : existing initializer. */
11219 : :
11220 : : static void
11221 : 3261 : add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
11222 : : bool implicit, struct obstack *braced_init_obstack)
11223 : : {
11224 : 3261 : struct init_node *p, **q, *r;
11225 : :
11226 : 3261 : q = &constructor_pending_elts;
11227 : 3261 : p = 0;
11228 : :
11229 : 3261 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11230 : : {
11231 : 3610 : while (*q != 0)
11232 : : {
11233 : 2412 : p = *q;
11234 : 2412 : if (tree_int_cst_lt (purpose, p->purpose))
11235 : 283 : q = &p->left;
11236 : 2129 : else if (tree_int_cst_lt (p->purpose, purpose))
11237 : : {
11238 : 1960 : if (TREE_CODE (p->value) != RAW_DATA_CST
11239 : 1960 : || (p->right
11240 : 112 : && tree_int_cst_le (p->right->purpose, purpose)))
11241 : 1830 : q = &p->right;
11242 : : else
11243 : : {
11244 : 130 : widest_int pp = wi::to_widest (p->purpose);
11245 : 130 : widest_int pw = wi::to_widest (purpose);
11246 : 130 : if (pp + RAW_DATA_LENGTH (p->value) <= pw)
11247 : 98 : q = &p->right;
11248 : : else
11249 : : {
11250 : : /* Override which should split the old RAW_DATA_CST
11251 : : into 2 or 3 pieces. */
11252 : 32 : if (!implicit && warn_override_init)
11253 : 9 : warning_init (loc, OPT_Woverride_init,
11254 : : "initialized field overwritten");
11255 : 32 : unsigned HOST_WIDE_INT start = (pw - pp).to_uhwi ();
11256 : 32 : unsigned HOST_WIDE_INT len = 1;
11257 : 32 : if (TREE_CODE (value) == RAW_DATA_CST)
11258 : 0 : len = RAW_DATA_LENGTH (value);
11259 : 32 : unsigned HOST_WIDE_INT end = 0;
11260 : 32 : unsigned plen = RAW_DATA_LENGTH (p->value);
11261 : 32 : gcc_checking_assert (start < plen && start);
11262 : 32 : if (plen - start > len)
11263 : 30 : end = plen - start - len;
11264 : 32 : tree v = p->value;
11265 : 32 : tree origtype = p->origtype;
11266 : 32 : if (start == 1)
11267 : 2 : p->value = build_int_cst (TREE_TYPE (v),
11268 : 2 : RAW_DATA_UCHAR_ELT (v, 0));
11269 : : else
11270 : : {
11271 : 30 : p->value = v;
11272 : 30 : if (end > 1)
11273 : 26 : v = copy_node (v);
11274 : 30 : RAW_DATA_LENGTH (p->value) = start;
11275 : : }
11276 : 32 : if (end)
11277 : : {
11278 : 30 : tree epurpose
11279 : 30 : = size_binop (PLUS_EXPR, purpose,
11280 : : bitsize_int (len));
11281 : 30 : if (end > 1)
11282 : : {
11283 : 28 : RAW_DATA_LENGTH (v) -= plen - end;
11284 : 28 : RAW_DATA_POINTER (v) += plen - end;
11285 : : }
11286 : : else
11287 : 2 : v = build_int_cst (TREE_TYPE (v),
11288 : 2 : RAW_DATA_UCHAR_ELT (v, plen
11289 : : - end));
11290 : 30 : add_pending_init (loc, epurpose, v, origtype,
11291 : : implicit, braced_init_obstack);
11292 : : }
11293 : 32 : q = &constructor_pending_elts;
11294 : 32 : continue;
11295 : 32 : }
11296 : 130 : }
11297 : : }
11298 : : else
11299 : : {
11300 : 169 : if (TREE_CODE (p->value) == RAW_DATA_CST
11301 : 179 : && (RAW_DATA_LENGTH (p->value)
11302 : 10 : > (TREE_CODE (value) == RAW_DATA_CST
11303 : 10 : ? RAW_DATA_LENGTH (value) : 1)))
11304 : : {
11305 : : /* Override which should split the old RAW_DATA_CST
11306 : : into 2 pieces. */
11307 : 6 : if (!implicit && warn_override_init)
11308 : 3 : warning_init (loc, OPT_Woverride_init,
11309 : : "initialized field overwritten");
11310 : 6 : unsigned HOST_WIDE_INT len = 1;
11311 : 6 : if (TREE_CODE (value) == RAW_DATA_CST)
11312 : 2 : len = RAW_DATA_LENGTH (value);
11313 : 6 : if ((unsigned) RAW_DATA_LENGTH (p->value) > len + 1)
11314 : : {
11315 : 6 : RAW_DATA_LENGTH (p->value) -= len;
11316 : 6 : RAW_DATA_POINTER (p->value) += len;
11317 : : }
11318 : : else
11319 : : {
11320 : 0 : unsigned int l = RAW_DATA_LENGTH (p->value) - 1;
11321 : 0 : p->value
11322 : 0 : = build_int_cst (TREE_TYPE (p->value),
11323 : 0 : RAW_DATA_UCHAR_ELT (p->value, l));
11324 : : }
11325 : 6 : p->purpose = size_binop (PLUS_EXPR, p->purpose,
11326 : : bitsize_int (len));
11327 : 6 : continue;
11328 : 6 : }
11329 : 163 : if (TREE_CODE (value) == RAW_DATA_CST)
11330 : : {
11331 : 8 : handle_raw_data:
11332 : : /* RAW_DATA_CST value might overlap various further
11333 : : prior initval entries. Find out how many. */
11334 : 13 : unsigned cnt = 0;
11335 : 13 : widest_int w
11336 : 26 : = wi::to_widest (purpose) + RAW_DATA_LENGTH (value);
11337 : 13 : struct init_node *r = p, *last = NULL;
11338 : 13 : bool override_init = warn_override_init;
11339 : 13 : while ((r = init_node_successor (r))
11340 : 53 : && wi::to_widest (r->purpose) < w)
11341 : : {
11342 : 40 : ++cnt;
11343 : 40 : if (TREE_SIDE_EFFECTS (r->value))
11344 : 2 : warning_init (loc, OPT_Woverride_init_side_effects,
11345 : : "initialized field with side-effects "
11346 : : "overwritten");
11347 : 38 : else if (override_init)
11348 : : {
11349 : 6 : warning_init (loc, OPT_Woverride_init,
11350 : : "initialized field overwritten");
11351 : 6 : override_init = false;
11352 : : }
11353 : : last = r;
11354 : : }
11355 : 13 : if (cnt)
11356 : : {
11357 : 26 : if (TREE_CODE (last->value) == RAW_DATA_CST
11358 : 13 : && (wi::to_widest (last->purpose)
11359 : 13 : + RAW_DATA_LENGTH (last->value) > w))
11360 : : {
11361 : : /* The last overlapping prior initval overlaps
11362 : : only partially. Shrink it and decrease cnt. */
11363 : 0 : unsigned int l = (wi::to_widest (last->purpose)
11364 : 0 : + RAW_DATA_LENGTH (last->value)
11365 : 0 : - w).to_uhwi ();
11366 : 0 : --cnt;
11367 : 0 : RAW_DATA_LENGTH (last->value) -= l;
11368 : 0 : RAW_DATA_POINTER (last->value) += l;
11369 : 0 : if (RAW_DATA_LENGTH (last->value) == 1)
11370 : 0 : last->value
11371 : 0 : = build_int_cst (TREE_TYPE (last->value),
11372 : 0 : RAW_DATA_UCHAR_ELT (last->value,
11373 : : 0));
11374 : 0 : last->purpose
11375 : 0 : = size_binop (PLUS_EXPR, last->purpose,
11376 : : bitsize_int (l));
11377 : : }
11378 : : /* Instead of deleting cnt nodes from the AVL tree
11379 : : and rebalancing, peel of last cnt bytes from the
11380 : : RAW_DATA_CST. Overriding thousands of previously
11381 : : initialized array elements with #embed needs to work,
11382 : : but doesn't need to be super efficient. */
11383 : 13 : gcc_checking_assert ((unsigned) RAW_DATA_LENGTH (value)
11384 : : > cnt);
11385 : 13 : RAW_DATA_LENGTH (value) -= cnt;
11386 : 13 : const unsigned char *s
11387 : 13 : = &RAW_DATA_UCHAR_ELT (value, RAW_DATA_LENGTH (value));
11388 : 13 : unsigned int o = RAW_DATA_LENGTH (value);
11389 : 53 : for (r = p; cnt--; ++o, ++s)
11390 : : {
11391 : 40 : r = init_node_successor (r);
11392 : 40 : r->purpose = size_binop (PLUS_EXPR, purpose,
11393 : : bitsize_int (o));
11394 : 40 : r->value = build_int_cst (TREE_TYPE (value), *s);
11395 : 40 : r->origtype = origtype;
11396 : : }
11397 : 13 : if (RAW_DATA_LENGTH (value) == 1)
11398 : 0 : value = build_int_cst (TREE_TYPE (value),
11399 : 0 : RAW_DATA_UCHAR_ELT (value, 0));
11400 : : }
11401 : : }
11402 : 168 : if (!implicit)
11403 : : {
11404 : 54 : if (TREE_SIDE_EFFECTS (p->value))
11405 : 6 : warning_init (loc, OPT_Woverride_init_side_effects,
11406 : : "initialized field with side-effects "
11407 : : "overwritten");
11408 : 48 : else if (warn_override_init)
11409 : 13 : warning_init (loc, OPT_Woverride_init,
11410 : : "initialized field overwritten");
11411 : : }
11412 : 168 : p->value = value;
11413 : 168 : p->origtype = origtype;
11414 : 168 : return;
11415 : : }
11416 : : }
11417 : 1198 : if (TREE_CODE (value) == RAW_DATA_CST && p)
11418 : : {
11419 : 61 : struct init_node *r;
11420 : 61 : if (q == &p->left)
11421 : : r = p;
11422 : : else
11423 : 36 : r = init_node_successor (p);
11424 : 131 : if (r && wi::to_widest (r->purpose) < (wi::to_widest (purpose)
11425 : 131 : + RAW_DATA_LENGTH (value)))
11426 : : {
11427 : : /* Overlap with at least one prior initval in the range but
11428 : : not at the start. */
11429 : 5 : p = r;
11430 : 5 : p->purpose = purpose;
11431 : 5 : goto handle_raw_data;
11432 : : }
11433 : : }
11434 : : }
11435 : : else
11436 : : {
11437 : 1900 : tree bitpos;
11438 : :
11439 : 1900 : bitpos = bit_position (purpose);
11440 : 5386 : while (*q != NULL)
11441 : : {
11442 : 1843 : p = *q;
11443 : 1843 : if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11444 : 43 : q = &p->left;
11445 : 1800 : else if (p->purpose != purpose)
11446 : 1543 : q = &p->right;
11447 : : else
11448 : : {
11449 : 257 : if (!implicit)
11450 : : {
11451 : 33 : if (TREE_SIDE_EFFECTS (p->value))
11452 : 4 : warning_init (loc, OPT_Woverride_init_side_effects,
11453 : : "initialized field with side-effects "
11454 : : "overwritten");
11455 : 29 : else if (warn_override_init)
11456 : 6 : warning_init (loc, OPT_Woverride_init,
11457 : : "initialized field overwritten");
11458 : : }
11459 : 257 : p->value = value;
11460 : 257 : p->origtype = origtype;
11461 : 257 : return;
11462 : : }
11463 : : }
11464 : : }
11465 : :
11466 : 2836 : r = (struct init_node *) obstack_alloc (braced_init_obstack,
11467 : : sizeof (struct init_node));
11468 : 2836 : r->purpose = purpose;
11469 : 2836 : r->value = value;
11470 : 2836 : r->origtype = origtype;
11471 : :
11472 : 2836 : *q = r;
11473 : 2836 : r->parent = p;
11474 : 2836 : r->left = 0;
11475 : 2836 : r->right = 0;
11476 : 2836 : r->balance = 0;
11477 : :
11478 : 4854 : while (p)
11479 : : {
11480 : 2806 : struct init_node *s;
11481 : :
11482 : 2806 : if (r == p->left)
11483 : : {
11484 : 237 : if (p->balance == 0)
11485 : 174 : p->balance = -1;
11486 : 63 : else if (p->balance < 0)
11487 : : {
11488 : 45 : if (r->balance < 0)
11489 : : {
11490 : : /* L rotation. */
11491 : 40 : p->left = r->right;
11492 : 40 : if (p->left)
11493 : 2 : p->left->parent = p;
11494 : 40 : r->right = p;
11495 : :
11496 : 40 : p->balance = 0;
11497 : 40 : r->balance = 0;
11498 : :
11499 : 40 : s = p->parent;
11500 : 40 : p->parent = r;
11501 : 40 : r->parent = s;
11502 : 40 : if (s)
11503 : : {
11504 : 31 : if (s->left == p)
11505 : 10 : s->left = r;
11506 : : else
11507 : 21 : s->right = r;
11508 : : }
11509 : : else
11510 : 9 : constructor_pending_elts = r;
11511 : : }
11512 : : else
11513 : : {
11514 : : /* LR rotation. */
11515 : 5 : struct init_node *t = r->right;
11516 : :
11517 : 5 : r->right = t->left;
11518 : 5 : if (r->right)
11519 : 0 : r->right->parent = r;
11520 : 5 : t->left = r;
11521 : :
11522 : 5 : p->left = t->right;
11523 : 5 : if (p->left)
11524 : 0 : p->left->parent = p;
11525 : 5 : t->right = p;
11526 : :
11527 : 5 : p->balance = t->balance < 0;
11528 : 5 : r->balance = -(t->balance > 0);
11529 : 5 : t->balance = 0;
11530 : :
11531 : 5 : s = p->parent;
11532 : 5 : p->parent = t;
11533 : 5 : r->parent = t;
11534 : 5 : t->parent = s;
11535 : 5 : if (s)
11536 : : {
11537 : 0 : if (s->left == p)
11538 : 0 : s->left = t;
11539 : : else
11540 : 0 : s->right = t;
11541 : : }
11542 : : else
11543 : 5 : constructor_pending_elts = t;
11544 : : }
11545 : : break;
11546 : : }
11547 : : else
11548 : : {
11549 : : /* p->balance == +1; growth of left side balances the node. */
11550 : 18 : p->balance = 0;
11551 : 18 : break;
11552 : : }
11553 : : }
11554 : : else /* r == p->right */
11555 : : {
11556 : 2569 : if (p->balance == 0)
11557 : : /* Growth propagation from right side. */
11558 : 1844 : p->balance++;
11559 : 725 : else if (p->balance > 0)
11560 : : {
11561 : 696 : if (r->balance > 0)
11562 : : {
11563 : : /* R rotation. */
11564 : 694 : p->right = r->left;
11565 : 694 : if (p->right)
11566 : 173 : p->right->parent = p;
11567 : 694 : r->left = p;
11568 : :
11569 : 694 : p->balance = 0;
11570 : 694 : r->balance = 0;
11571 : :
11572 : 694 : s = p->parent;
11573 : 694 : p->parent = r;
11574 : 694 : r->parent = s;
11575 : 694 : if (s)
11576 : : {
11577 : 374 : if (s->left == p)
11578 : 2 : s->left = r;
11579 : : else
11580 : 372 : s->right = r;
11581 : : }
11582 : : else
11583 : 320 : constructor_pending_elts = r;
11584 : : }
11585 : : else /* r->balance == -1 */
11586 : : {
11587 : : /* RL rotation */
11588 : 2 : struct init_node *t = r->left;
11589 : :
11590 : 2 : r->left = t->right;
11591 : 2 : if (r->left)
11592 : 2 : r->left->parent = r;
11593 : 2 : t->right = r;
11594 : :
11595 : 2 : p->right = t->left;
11596 : 2 : if (p->right)
11597 : 2 : p->right->parent = p;
11598 : 2 : t->left = p;
11599 : :
11600 : 2 : r->balance = (t->balance < 0);
11601 : 2 : p->balance = -(t->balance > 0);
11602 : 2 : t->balance = 0;
11603 : :
11604 : 2 : s = p->parent;
11605 : 2 : p->parent = t;
11606 : 2 : r->parent = t;
11607 : 2 : t->parent = s;
11608 : 2 : if (s)
11609 : : {
11610 : 0 : if (s->left == p)
11611 : 0 : s->left = t;
11612 : : else
11613 : 0 : s->right = t;
11614 : : }
11615 : : else
11616 : 2 : constructor_pending_elts = t;
11617 : : }
11618 : : break;
11619 : : }
11620 : : else
11621 : : {
11622 : : /* p->balance == -1; growth of right side balances the node. */
11623 : 29 : p->balance = 0;
11624 : 29 : break;
11625 : : }
11626 : : }
11627 : :
11628 : 2018 : r = p;
11629 : 2018 : p = p->parent;
11630 : : }
11631 : : }
11632 : :
11633 : : /* Build AVL tree from a sorted chain. */
11634 : :
11635 : : static void
11636 : 418 : set_nonincremental_init (struct obstack * braced_init_obstack)
11637 : : {
11638 : 418 : unsigned HOST_WIDE_INT ix;
11639 : 418 : tree index, value;
11640 : :
11641 : 418 : if (TREE_CODE (constructor_type) != RECORD_TYPE
11642 : 418 : && TREE_CODE (constructor_type) != ARRAY_TYPE)
11643 : : return;
11644 : :
11645 : 1601 : FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
11646 : 1183 : add_pending_init (input_location, index, value, NULL_TREE, true,
11647 : : braced_init_obstack);
11648 : 418 : constructor_elements = NULL;
11649 : 418 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
11650 : : {
11651 : 200 : constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
11652 : : /* Skip any nameless bit fields at the beginning. */
11653 : 200 : while (constructor_unfilled_fields != NULL_TREE
11654 : 200 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
11655 : 0 : constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
11656 : :
11657 : : }
11658 : 218 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11659 : : {
11660 : 218 : if (TYPE_DOMAIN (constructor_type))
11661 : 160 : constructor_unfilled_index
11662 : 160 : = convert (bitsizetype,
11663 : 160 : TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
11664 : : else
11665 : 58 : constructor_unfilled_index = bitsize_zero_node;
11666 : : }
11667 : 418 : constructor_incremental = 0;
11668 : : }
11669 : :
11670 : : /* Build AVL tree from a string constant. */
11671 : :
11672 : : static void
11673 : 7 : set_nonincremental_init_from_string (tree str,
11674 : : struct obstack * braced_init_obstack)
11675 : : {
11676 : 7 : tree value, purpose, type;
11677 : 7 : HOST_WIDE_INT val[2];
11678 : 7 : const char *p, *end;
11679 : 7 : int byte, wchar_bytes, charwidth, bitpos;
11680 : :
11681 : 7 : gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
11682 : :
11683 : 7 : wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
11684 : 7 : charwidth = TYPE_PRECISION (char_type_node);
11685 : 7 : gcc_assert ((size_t) wchar_bytes * charwidth
11686 : : <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
11687 : 7 : type = TREE_TYPE (constructor_type);
11688 : 7 : p = TREE_STRING_POINTER (str);
11689 : 7 : end = p + TREE_STRING_LENGTH (str);
11690 : :
11691 : 7 : for (purpose = bitsize_zero_node;
11692 : : p < end
11693 : 52 : && !(constructor_max_index
11694 : 20 : && tree_int_cst_lt (constructor_max_index, purpose));
11695 : 25 : purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
11696 : : {
11697 : 25 : if (wchar_bytes == 1)
11698 : : {
11699 : 9 : val[0] = (unsigned char) *p++;
11700 : 9 : val[1] = 0;
11701 : : }
11702 : : else
11703 : : {
11704 : 16 : val[1] = 0;
11705 : 16 : val[0] = 0;
11706 : 64 : for (byte = 0; byte < wchar_bytes; byte++)
11707 : : {
11708 : 48 : if (BYTES_BIG_ENDIAN)
11709 : : bitpos = (wchar_bytes - byte - 1) * charwidth;
11710 : : else
11711 : 48 : bitpos = byte * charwidth;
11712 : 48 : val[bitpos / HOST_BITS_PER_WIDE_INT]
11713 : 48 : |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
11714 : 48 : << (bitpos % HOST_BITS_PER_WIDE_INT);
11715 : : }
11716 : : }
11717 : :
11718 : 25 : if (!TYPE_UNSIGNED (type))
11719 : : {
11720 : 13 : bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
11721 : 13 : if (bitpos < HOST_BITS_PER_WIDE_INT)
11722 : : {
11723 : 13 : if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
11724 : : {
11725 : 0 : val[0] |= HOST_WIDE_INT_M1U << bitpos;
11726 : 0 : val[1] = -1;
11727 : : }
11728 : : }
11729 : 0 : else if (bitpos == HOST_BITS_PER_WIDE_INT)
11730 : : {
11731 : 0 : if (val[0] < 0)
11732 : 0 : val[1] = -1;
11733 : : }
11734 : 0 : else if (val[1] & (HOST_WIDE_INT_1
11735 : 0 : << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
11736 : 0 : val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
11737 : : }
11738 : :
11739 : 25 : value = wide_int_to_tree (type,
11740 : 25 : wide_int::from_array (val, 2,
11741 : : HOST_BITS_PER_WIDE_INT * 2));
11742 : 25 : add_pending_init (input_location, purpose, value, NULL_TREE, true,
11743 : : braced_init_obstack);
11744 : : }
11745 : :
11746 : 7 : constructor_incremental = 0;
11747 : 7 : }
11748 : :
11749 : : /* Return value of FIELD in pending initializer or NULL_TREE if the field was
11750 : : not initialized yet. */
11751 : :
11752 : : static tree
11753 : 703581 : find_init_member (tree field, struct obstack * braced_init_obstack)
11754 : : {
11755 : 703581 : struct init_node *p;
11756 : :
11757 : 703581 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
11758 : : {
11759 : 702786 : if (constructor_incremental
11760 : 702786 : && tree_int_cst_lt (field, constructor_unfilled_index))
11761 : 16 : set_nonincremental_init (braced_init_obstack);
11762 : :
11763 : 702786 : p = constructor_pending_elts;
11764 : 702909 : while (p)
11765 : : {
11766 : 233 : if (tree_int_cst_lt (field, p->purpose))
11767 : 25 : p = p->left;
11768 : 208 : else if (tree_int_cst_lt (p->purpose, field))
11769 : 98 : p = p->right;
11770 : : else
11771 : 110 : return p->value;
11772 : : }
11773 : : }
11774 : 795 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
11775 : : {
11776 : 673 : tree bitpos = bit_position (field);
11777 : :
11778 : 673 : if (constructor_incremental
11779 : 673 : && (!constructor_unfilled_fields
11780 : 470 : || tree_int_cst_lt (bitpos,
11781 : 470 : bit_position (constructor_unfilled_fields))))
11782 : 51 : set_nonincremental_init (braced_init_obstack);
11783 : :
11784 : 673 : p = constructor_pending_elts;
11785 : 813 : while (p)
11786 : : {
11787 : 347 : if (field == p->purpose)
11788 : 207 : return p->value;
11789 : 140 : else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
11790 : 7 : p = p->left;
11791 : : else
11792 : 133 : p = p->right;
11793 : : }
11794 : : }
11795 : 122 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
11796 : : {
11797 : 122 : if (!vec_safe_is_empty (constructor_elements)
11798 : 50 : && (constructor_elements->last ().index == field))
11799 : 50 : return constructor_elements->last ().value;
11800 : : }
11801 : : return NULL_TREE;
11802 : : }
11803 : :
11804 : : /* "Output" the next constructor element.
11805 : : At top level, really output it to assembler code now.
11806 : : Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
11807 : : If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
11808 : : TYPE is the data type that the containing data type wants here.
11809 : : FIELD is the field (a FIELD_DECL) or the index that this element fills.
11810 : : If VALUE is a string constant, STRICT_STRING is true if it is
11811 : : unparenthesized or we should not warn here for it being parenthesized.
11812 : : For other types of VALUE, STRICT_STRING is not used.
11813 : :
11814 : : PENDING if true means output pending elements that belong
11815 : : right after this element. (PENDING is normally true;
11816 : : it is false while outputting pending elements, to avoid recursion.)
11817 : :
11818 : : IMPLICIT is true if value comes from pop_init_level (1),
11819 : : the new initializer has been merged with the existing one
11820 : : and thus no warnings should be emitted about overriding an
11821 : : existing initializer. */
11822 : :
11823 : : static void
11824 : 9461156 : output_init_element (location_t loc, tree value, tree origtype,
11825 : : bool strict_string, tree type, tree field, bool pending,
11826 : : bool implicit, struct obstack * braced_init_obstack)
11827 : : {
11828 : 9461156 : tree semantic_type = NULL_TREE;
11829 : 9461156 : bool maybe_const = true;
11830 : 9461156 : bool npc, int_const_expr, arith_const_expr;
11831 : :
11832 : 9461156 : if (type == error_mark_node || value == error_mark_node)
11833 : : {
11834 : 478 : constructor_erroneous = 1;
11835 : 2595 : return;
11836 : : }
11837 : 9460678 : if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
11838 : 199900 : && (TREE_CODE (value) == STRING_CST
11839 : 158509 : || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
11840 : 41399 : && !(TREE_CODE (value) == STRING_CST
11841 : 41391 : && TREE_CODE (type) == ARRAY_TYPE
11842 : 3896 : && INTEGRAL_TYPE_P (TREE_TYPE (type)))
11843 : 9498181 : && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
11844 : 37503 : TYPE_MAIN_VARIANT (type)))
11845 : 37503 : value = array_to_pointer_conversion (input_location, value);
11846 : :
11847 : 9460678 : if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
11848 : 178 : && require_constant_value && pending)
11849 : : {
11850 : : /* As an extension, allow initializing objects with static storage
11851 : : duration with compound literals (which are then treated just as
11852 : : the brace enclosed list they contain). */
11853 : 114 : if (flag_isoc99)
11854 : 24 : pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
11855 : : "constant");
11856 : 114 : tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
11857 : 114 : value = DECL_INITIAL (decl);
11858 : : }
11859 : :
11860 : 9460678 : npc = null_pointer_constant_p (value);
11861 : 18921356 : int_const_expr = (TREE_CODE (value) == INTEGER_CST
11862 : 4143474 : && !TREE_OVERFLOW (value)
11863 : 13604151 : && INTEGRAL_TYPE_P (TREE_TYPE (value)));
11864 : : /* Not fully determined before folding. */
11865 : 9460678 : arith_const_expr = true;
11866 : 9460678 : if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
11867 : : {
11868 : 16 : semantic_type = TREE_TYPE (value);
11869 : 16 : value = TREE_OPERAND (value, 0);
11870 : : }
11871 : 9460678 : value = c_fully_fold (value, require_constant_value, &maybe_const);
11872 : : /* TODO: this may not detect all cases of expressions folding to
11873 : : constants that are not arithmetic constant expressions. */
11874 : 9460678 : if (!maybe_const)
11875 : : arith_const_expr = false;
11876 : 18918875 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
11877 : 3152870 : && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
11878 : 11300225 : && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
11879 : : arith_const_expr = false;
11880 : 7633608 : else if (TREE_CODE (value) != INTEGER_CST
11881 : : && TREE_CODE (value) != REAL_CST
11882 : : && TREE_CODE (value) != COMPLEX_CST
11883 : : && TREE_CODE (value) != RAW_DATA_CST)
11884 : : arith_const_expr = false;
11885 : 4524538 : else if (TREE_OVERFLOW (value))
11886 : 4936141 : arith_const_expr = false;
11887 : :
11888 : 9460678 : if (value == error_mark_node)
11889 : 0 : constructor_erroneous = 1;
11890 : 9460678 : else if (!TREE_CONSTANT (value))
11891 : 3128707 : constructor_constant = 0;
11892 : 6331971 : else if (!initializer_constant_valid_p (value,
11893 : 6331971 : TREE_TYPE (value),
11894 : 6331971 : AGGREGATE_TYPE_P (constructor_type)
11895 : 6331971 : && TYPE_REVERSE_STORAGE_ORDER
11896 : : (constructor_type))
11897 : 6331971 : || (RECORD_OR_UNION_TYPE_P (constructor_type)
11898 : 1037372 : && DECL_C_BIT_FIELD (field)
11899 : 8103 : && TREE_CODE (value) != INTEGER_CST))
11900 : 77 : constructor_simple = 0;
11901 : 9460678 : if (!maybe_const)
11902 : 1137 : constructor_nonconst = 1;
11903 : :
11904 : : /* Digest the initializer and issue any errors about incompatible
11905 : : types before issuing errors about non-constant initializers. */
11906 : 9460678 : tree new_value = value;
11907 : 9460678 : if (semantic_type)
11908 : 16 : new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
11909 : : /* In the case of braces around a scalar initializer, the result of
11910 : : this initializer processing goes through digest_init again at the
11911 : : outer level. In the case of a constexpr initializer for a
11912 : : pointer, avoid converting a null pointer constant to something
11913 : : that is not a null pointer constant to avoid a spurious error
11914 : : from that second processing. */
11915 : 9460678 : if (!require_constexpr_value
11916 : 398 : || !npc
11917 : 69 : || TREE_CODE (constructor_type) != POINTER_TYPE)
11918 : 9460667 : new_value = digest_init (loc,
11919 : 9460667 : RECORD_OR_UNION_TYPE_P (constructor_type)
11920 : 8392278 : ? field : constructor_fields
11921 : 8392278 : ? constructor_fields : constructor_decl,
11922 : : type, new_value, origtype, npc,
11923 : : int_const_expr, arith_const_expr, strict_string,
11924 : : require_constant_value, require_constexpr_value);
11925 : 9460678 : if (new_value == error_mark_node)
11926 : : {
11927 : 76 : constructor_erroneous = 1;
11928 : 76 : return;
11929 : : }
11930 : 9460602 : if (require_constant_value || require_constant_elements)
11931 : 2710866 : constant_expression_warning (new_value);
11932 : :
11933 : : /* Proceed to check the constness of the original initializer. */
11934 : 9460602 : if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
11935 : : {
11936 : 3128733 : if (require_constant_value)
11937 : : {
11938 : 0 : error_init (loc, "initializer element is not constant");
11939 : 0 : value = error_mark_node;
11940 : : }
11941 : 3128733 : else if (require_constant_elements)
11942 : 15 : pedwarn (loc, OPT_Wpedantic,
11943 : : "initializer element is not computable at load time");
11944 : : }
11945 : 6331869 : else if (!maybe_const
11946 : 54 : && (require_constant_value || require_constant_elements))
11947 : 28 : pedwarn_init (loc, OPT_Wpedantic,
11948 : : "initializer element is not a constant expression");
11949 : : /* digest_init has already carried out the additional checks
11950 : : required for 'constexpr' initializers (using the information
11951 : : passed to it about whether the original initializer was certain
11952 : : kinds of constant expression), so that check does not need to be
11953 : : repeated here. */
11954 : :
11955 : : /* Issue -Wc++-compat warnings about initializing a bitfield with
11956 : : enum type. */
11957 : 9460602 : if (warn_cxx_compat
11958 : 6895 : && field != NULL_TREE
11959 : 6895 : && TREE_CODE (field) == FIELD_DECL
11960 : 459 : && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
11961 : 21 : && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
11962 : 21 : != TYPE_MAIN_VARIANT (type))
11963 : 9460623 : && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
11964 : : {
11965 : 21 : tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
11966 : 21 : if (checktype != error_mark_node
11967 : 21 : && (TYPE_MAIN_VARIANT (checktype)
11968 : 21 : != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
11969 : 9 : warning_init (loc, OPT_Wc___compat,
11970 : : "enum conversion in initialization is invalid in C++");
11971 : : }
11972 : :
11973 : : /* If this field is empty and does not have side effects (and is not at
11974 : : the end of structure), don't do anything other than checking the
11975 : : initializer. */
11976 : 9460602 : if (field
11977 : 9460602 : && (TREE_TYPE (field) == error_mark_node
11978 : 9460182 : || (COMPLETE_TYPE_P (TREE_TYPE (field))
11979 : 9459874 : && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
11980 : 62 : && !TREE_SIDE_EFFECTS (new_value)
11981 : 33 : && (TREE_CODE (constructor_type) == ARRAY_TYPE
11982 : 33 : || DECL_CHAIN (field)))))
11983 : 18 : return;
11984 : :
11985 : : /* Finally, set VALUE to the initializer value digested above. */
11986 : 9460584 : value = new_value;
11987 : :
11988 : : /* If this element doesn't come next in sequence,
11989 : : put it on constructor_pending_elts. */
11990 : 9460584 : if (TREE_CODE (constructor_type) == ARRAY_TYPE
11991 : 9460584 : && (!constructor_incremental
11992 : 1714239 : || !tree_int_cst_equal (field, constructor_unfilled_index)
11993 : 1713873 : || (TREE_CODE (value) == RAW_DATA_CST
11994 : 233 : && constructor_pending_elts
11995 : 66 : && pending)))
11996 : : {
11997 : 627 : if (constructor_incremental
11998 : 627 : && (tree_int_cst_lt (field, constructor_unfilled_index)
11999 : 337 : || (TREE_CODE (value) == RAW_DATA_CST
12000 : 14 : && constructor_pending_elts
12001 : 14 : && pending)))
12002 : 48 : set_nonincremental_init (braced_init_obstack);
12003 : :
12004 : 627 : add_pending_init (loc, field, value, origtype, implicit,
12005 : : braced_init_obstack);
12006 : 627 : return;
12007 : : }
12008 : 9459957 : else if (TREE_CODE (constructor_type) == RECORD_TYPE
12009 : 1038146 : && (!constructor_incremental
12010 : 1037806 : || field != constructor_unfilled_fields))
12011 : : {
12012 : : /* We do this for records but not for unions. In a union,
12013 : : no matter which field is specified, it can be initialized
12014 : : right away since it starts at the beginning of the union. */
12015 : 1396 : if (constructor_incremental)
12016 : : {
12017 : 1056 : if (!constructor_unfilled_fields)
12018 : 5 : set_nonincremental_init (braced_init_obstack);
12019 : : else
12020 : : {
12021 : 1051 : tree bitpos, unfillpos;
12022 : :
12023 : 1051 : bitpos = bit_position (field);
12024 : 1051 : unfillpos = bit_position (constructor_unfilled_fields);
12025 : :
12026 : 1051 : if (tree_int_cst_lt (bitpos, unfillpos))
12027 : 4 : set_nonincremental_init (braced_init_obstack);
12028 : : }
12029 : : }
12030 : :
12031 : 1396 : add_pending_init (loc, field, value, origtype, implicit,
12032 : : braced_init_obstack);
12033 : 1396 : return;
12034 : : }
12035 : 9458561 : else if (TREE_CODE (constructor_type) == UNION_TYPE
12036 : 9458561 : && !vec_safe_is_empty (constructor_elements))
12037 : : {
12038 : 62 : if (!implicit)
12039 : : {
12040 : 12 : if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
12041 : 3 : warning_init (loc, OPT_Woverride_init_side_effects,
12042 : : "initialized field with side-effects overwritten");
12043 : 9 : else if (warn_override_init)
12044 : 6 : warning_init (loc, OPT_Woverride_init,
12045 : : "initialized field overwritten");
12046 : : }
12047 : :
12048 : : /* We can have just one union field set. */
12049 : 62 : constructor_elements = NULL;
12050 : : }
12051 : :
12052 : : /* Otherwise, output this element either to
12053 : : constructor_elements or to the assembler file. */
12054 : :
12055 : 9458561 : constructor_elt celt = {field, value};
12056 : 9458561 : vec_safe_push (constructor_elements, celt);
12057 : :
12058 : : /* Advance the variable that indicates sequential elements output. */
12059 : 9458561 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12060 : : {
12061 : 1713868 : tree inc = bitsize_one_node;
12062 : 1713868 : if (value && TREE_CODE (value) == RAW_DATA_CST)
12063 : 228 : inc = bitsize_int (RAW_DATA_LENGTH (value));
12064 : 1713868 : constructor_unfilled_index
12065 : 1713868 : = size_binop_loc (input_location, PLUS_EXPR,
12066 : : constructor_unfilled_index, inc);
12067 : : }
12068 : 7744693 : else if (TREE_CODE (constructor_type) == RECORD_TYPE)
12069 : : {
12070 : 1036750 : constructor_unfilled_fields
12071 : 1036750 : = DECL_CHAIN (constructor_unfilled_fields);
12072 : :
12073 : : /* Skip any nameless bit fields. */
12074 : 1036750 : while (constructor_unfilled_fields != NULL_TREE
12075 : 1036851 : && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
12076 : 101 : constructor_unfilled_fields
12077 : 101 : = DECL_CHAIN (constructor_unfilled_fields);
12078 : : }
12079 : 6707943 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12080 : 30211 : constructor_unfilled_fields = NULL_TREE;
12081 : :
12082 : : /* Now output any pending elements which have become next. */
12083 : 9458561 : if (pending)
12084 : 9455704 : output_pending_init_elements (0, braced_init_obstack);
12085 : : }
12086 : :
12087 : : /* For two FIELD_DECLs in the same chain, return -1 if field1
12088 : : comes before field2, 1 if field1 comes after field2 and
12089 : : 0 if field1 == field2. */
12090 : :
12091 : : static int
12092 : 4001 : init_field_decl_cmp (tree field1, tree field2)
12093 : : {
12094 : 4001 : if (field1 == field2)
12095 : : return 0;
12096 : :
12097 : 2138 : tree bitpos1 = bit_position (field1);
12098 : 2138 : tree bitpos2 = bit_position (field2);
12099 : 2138 : if (tree_int_cst_equal (bitpos1, bitpos2))
12100 : : {
12101 : : /* If one of the fields has non-zero bitsize, then that
12102 : : field must be the last one in a sequence of zero
12103 : : sized fields, fields after it will have bigger
12104 : : bit_position. */
12105 : 22 : if (TREE_TYPE (field1) != error_mark_node
12106 : 22 : && COMPLETE_TYPE_P (TREE_TYPE (field1))
12107 : 44 : && integer_nonzerop (TREE_TYPE (field1)))
12108 : : return 1;
12109 : 22 : if (TREE_TYPE (field2) != error_mark_node
12110 : 22 : && COMPLETE_TYPE_P (TREE_TYPE (field2))
12111 : 37 : && integer_nonzerop (TREE_TYPE (field2)))
12112 : : return -1;
12113 : : /* Otherwise, fallback to DECL_CHAIN walk to find out
12114 : : which field comes earlier. Walk chains of both
12115 : : fields, so that if field1 and field2 are close to each
12116 : : other in either order, it is found soon even for large
12117 : : sequences of zero sized fields. */
12118 : : tree f1 = field1, f2 = field2;
12119 : 22 : while (1)
12120 : : {
12121 : 22 : f1 = DECL_CHAIN (f1);
12122 : 22 : f2 = DECL_CHAIN (f2);
12123 : 22 : if (f1 == NULL_TREE)
12124 : : {
12125 : 7 : gcc_assert (f2);
12126 : : return 1;
12127 : : }
12128 : 15 : if (f2 == NULL_TREE)
12129 : : return -1;
12130 : 1 : if (f1 == field2)
12131 : : return -1;
12132 : 0 : if (f2 == field1)
12133 : : return 1;
12134 : 0 : if (!tree_int_cst_equal (bit_position (f1), bitpos1))
12135 : : return 1;
12136 : 0 : if (!tree_int_cst_equal (bit_position (f2), bitpos1))
12137 : : return -1;
12138 : : }
12139 : : }
12140 : 2116 : else if (tree_int_cst_lt (bitpos1, bitpos2))
12141 : : return -1;
12142 : : else
12143 : : return 1;
12144 : : }
12145 : :
12146 : : /* Output any pending elements which have become next.
12147 : : As we output elements, constructor_unfilled_{fields,index}
12148 : : advances, which may cause other elements to become next;
12149 : : if so, they too are output.
12150 : :
12151 : : If ALL is 0, we return when there are
12152 : : no more pending elements to output now.
12153 : :
12154 : : If ALL is 1, we output space as necessary so that
12155 : : we can output all the pending elements. */
12156 : : static void
12157 : 11500942 : output_pending_init_elements (int all, struct obstack * braced_init_obstack)
12158 : : {
12159 : 11500942 : struct init_node *elt = constructor_pending_elts;
12160 : 11501938 : tree next;
12161 : :
12162 : 11501938 : retry:
12163 : :
12164 : : /* Look through the whole pending tree.
12165 : : If we find an element that should be output now,
12166 : : output it. Otherwise, set NEXT to the element
12167 : : that comes first among those still pending. */
12168 : :
12169 : 11501938 : next = NULL_TREE;
12170 : 11507568 : while (elt)
12171 : : {
12172 : 7574 : if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12173 : : {
12174 : 3004 : if (tree_int_cst_equal (elt->purpose,
12175 : : constructor_unfilled_index))
12176 : 1197 : output_init_element (input_location, elt->value, elt->origtype,
12177 : 1197 : true, TREE_TYPE (constructor_type),
12178 : : constructor_unfilled_index, false, false,
12179 : : braced_init_obstack);
12180 : 1807 : else if (tree_int_cst_lt (constructor_unfilled_index,
12181 : 1807 : elt->purpose))
12182 : : {
12183 : : /* Advance to the next smaller node. */
12184 : 589 : if (elt->left)
12185 : : elt = elt->left;
12186 : : else
12187 : : {
12188 : : /* We have reached the smallest node bigger than the
12189 : : current unfilled index. Fill the space first. */
12190 : 209 : next = elt->purpose;
12191 : 209 : break;
12192 : : }
12193 : : }
12194 : : else
12195 : : {
12196 : : /* Advance to the next bigger node. */
12197 : 1218 : if (elt->right)
12198 : : elt = elt->right;
12199 : : else
12200 : : {
12201 : : /* We have reached the biggest node in a subtree. Find
12202 : : the parent of it, which is the next bigger node. */
12203 : 1218 : while (elt->parent && elt->parent->right == elt)
12204 : : elt = elt->parent;
12205 : 774 : elt = elt->parent;
12206 : 1149 : if (elt && tree_int_cst_lt (constructor_unfilled_index,
12207 : 375 : elt->purpose))
12208 : : {
12209 : 22 : next = elt->purpose;
12210 : 22 : break;
12211 : : }
12212 : : }
12213 : : }
12214 : : }
12215 : 4570 : else if (RECORD_OR_UNION_TYPE_P (constructor_type))
12216 : : {
12217 : : /* If the current record is complete we are done. */
12218 : 4570 : if (constructor_unfilled_fields == NULL_TREE)
12219 : : break;
12220 : :
12221 : 3731 : int cmp = init_field_decl_cmp (constructor_unfilled_fields,
12222 : : elt->purpose);
12223 : 3731 : if (cmp == 0)
12224 : 1660 : output_init_element (input_location, elt->value, elt->origtype,
12225 : 1660 : true, TREE_TYPE (elt->purpose),
12226 : : elt->purpose, false, false,
12227 : : braced_init_obstack);
12228 : 2071 : else if (cmp < 0)
12229 : : {
12230 : : /* Advance to the next smaller node. */
12231 : 1084 : if (elt->left)
12232 : : elt = elt->left;
12233 : : else
12234 : : {
12235 : : /* We have reached the smallest node bigger than the
12236 : : current unfilled field. Fill the space first. */
12237 : 807 : next = elt->purpose;
12238 : 807 : break;
12239 : : }
12240 : : }
12241 : : else
12242 : : {
12243 : : /* Advance to the next bigger node. */
12244 : 987 : if (elt->right)
12245 : : elt = elt->right;
12246 : : else
12247 : : {
12248 : : /* We have reached the biggest node in a subtree. Find
12249 : : the parent of it, which is the next bigger node. */
12250 : 731 : while (elt->parent && elt->parent->right == elt)
12251 : : elt = elt->parent;
12252 : 515 : elt = elt->parent;
12253 : 515 : if (elt
12254 : 515 : && init_field_decl_cmp (constructor_unfilled_fields,
12255 : : elt->purpose) < 0)
12256 : : {
12257 : 67 : next = elt->purpose;
12258 : 67 : break;
12259 : : }
12260 : : }
12261 : : }
12262 : : }
12263 : : }
12264 : :
12265 : : /* Ordinarily return, but not if we want to output all
12266 : : and there are elements left. */
12267 : 11501938 : if (!(all && next != NULL_TREE))
12268 : 11500942 : return;
12269 : :
12270 : : /* If it's not incremental, just skip over the gap, so that after
12271 : : jumping to retry we will output the next successive element. */
12272 : 996 : if (RECORD_OR_UNION_TYPE_P (constructor_type))
12273 : 804 : constructor_unfilled_fields = next;
12274 : 192 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12275 : 192 : constructor_unfilled_index = next;
12276 : :
12277 : : /* ELT now points to the node in the pending tree with the next
12278 : : initializer to output. */
12279 : 996 : goto retry;
12280 : : }
12281 : :
12282 : : /* Expression VALUE coincides with the start of type TYPE in a braced
12283 : : initializer. Return true if we should treat VALUE as initializing
12284 : : the first element of TYPE, false if we should treat it as initializing
12285 : : TYPE as a whole.
12286 : :
12287 : : If the initializer is clearly invalid, the question becomes:
12288 : : which choice gives the best error message? */
12289 : :
12290 : : static bool
12291 : 3479450 : initialize_elementwise_p (tree type, tree value)
12292 : : {
12293 : 3479450 : if (type == error_mark_node || value == error_mark_node)
12294 : : return false;
12295 : :
12296 : 3478993 : gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
12297 : :
12298 : 3478993 : tree value_type = TREE_TYPE (value);
12299 : 3478993 : if (value_type == error_mark_node)
12300 : : return false;
12301 : :
12302 : : /* GNU vectors can be initialized elementwise. However, treat any
12303 : : kind of vector value as initializing the vector type as a whole,
12304 : : regardless of whether the value is a GNU vector. Such initializers
12305 : : are valid if and only if they would have been valid in a non-braced
12306 : : initializer like:
12307 : :
12308 : : TYPE foo = VALUE;
12309 : :
12310 : : so recursing into the vector type would be at best confusing or at
12311 : : worst wrong. For example, when -flax-vector-conversions is in effect,
12312 : : it's possible to initialize a V8HI from a V4SI, even though the vectors
12313 : : have different element types and different numbers of elements. */
12314 : 3478993 : if (gnu_vector_type_p (type))
12315 : 19169 : return !VECTOR_TYPE_P (value_type);
12316 : :
12317 : 3459824 : if (AGGREGATE_TYPE_P (type))
12318 : 1741037 : return !comptypes (type, TYPE_MAIN_VARIANT (value_type));
12319 : :
12320 : : return false;
12321 : : }
12322 : :
12323 : : /* Helper function for process_init_element. Split first element of
12324 : : RAW_DATA_CST and save the rest to *RAW_DATA. */
12325 : :
12326 : : static inline tree
12327 : 9458181 : maybe_split_raw_data (tree value, tree *raw_data)
12328 : : {
12329 : 9458181 : if (value == NULL_TREE || TREE_CODE (value) != RAW_DATA_CST)
12330 : : return value;
12331 : 211 : *raw_data = value;
12332 : 211 : value = build_int_cst (integer_type_node,
12333 : 211 : RAW_DATA_UCHAR_ELT (*raw_data, 0));
12334 : 211 : ++RAW_DATA_POINTER (*raw_data);
12335 : 211 : --RAW_DATA_LENGTH (*raw_data);
12336 : 211 : return value;
12337 : : }
12338 : :
12339 : : /* Return non-zero if c_parser_initval should attempt to optimize
12340 : : large initializers into RAW_DATA_CST. In that case return how
12341 : : many elements to optimize at most. */
12342 : :
12343 : : unsigned
12344 : 2931029 : c_maybe_optimize_large_byte_initializer (void)
12345 : : {
12346 : 2931029 : if (!constructor_type
12347 : 2930959 : || TREE_CODE (constructor_type) != ARRAY_TYPE
12348 : 206681 : || constructor_stack->implicit)
12349 : : return 0;
12350 : 203940 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12351 : 203940 : if (TREE_CODE (elttype) != INTEGER_TYPE
12352 : 203940 : && TREE_CODE (elttype) != BITINT_TYPE)
12353 : : return 0;
12354 : 181545 : if (TYPE_PRECISION (elttype) != CHAR_BIT
12355 : 27832 : || constructor_stack->replacement_value.value
12356 : 27832 : || (COMPLETE_TYPE_P (constructor_type)
12357 : 26195 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12358 : 209377 : || constructor_range_stack)
12359 : : return 0;
12360 : 27832 : if (constructor_max_index == NULL_TREE)
12361 : : return INT_MAX;
12362 : 26195 : if (tree_int_cst_le (constructor_max_index, constructor_index)
12363 : 26195 : || integer_all_onesp (constructor_max_index))
12364 : 4756 : return 0;
12365 : 21439 : widest_int w = wi::to_widest (constructor_max_index);
12366 : 21439 : w -= wi::to_widest (constructor_index);
12367 : 21439 : w += 1;
12368 : 21439 : if (w < 64)
12369 : : return 0;
12370 : 6888 : if (w > INT_MAX)
12371 : : return INT_MAX;
12372 : 6888 : return w.to_uhwi ();
12373 : 21439 : }
12374 : :
12375 : : /* Add one non-braced element to the current constructor level.
12376 : : This adjusts the current position within the constructor's type.
12377 : : This may also start or terminate implicit levels
12378 : : to handle a partly-braced initializer.
12379 : :
12380 : : Once this has found the correct level for the new element,
12381 : : it calls output_init_element.
12382 : :
12383 : : IMPLICIT is true if value comes from pop_init_level (1),
12384 : : the new initializer has been merged with the existing one
12385 : : and thus no warnings should be emitted about overriding an
12386 : : existing initializer. */
12387 : :
12388 : : void
12389 : 9449237 : process_init_element (location_t loc, struct c_expr value, bool implicit,
12390 : : struct obstack * braced_init_obstack)
12391 : : {
12392 : 9449237 : tree orig_value = value.value;
12393 : 18898474 : int string_flag
12394 : 9449237 : = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
12395 : 9449237 : bool strict_string = value.original_code == STRING_CST;
12396 : 9449237 : bool was_designated = designator_depth != 0;
12397 : 9449237 : tree raw_data = NULL_TREE;
12398 : :
12399 : 9449449 : retry:
12400 : 9449449 : designator_depth = 0;
12401 : 9449449 : designator_erroneous = 0;
12402 : :
12403 : 9449449 : if (!implicit && value.value && !integer_zerop (value.value))
12404 : 7450349 : constructor_zeroinit = 0;
12405 : :
12406 : : /* Handle superfluous braces around string cst as in
12407 : : char x[] = {"foo"}; */
12408 : 9449449 : if (constructor_type
12409 : 9449321 : && !was_designated
12410 : 9416302 : && TREE_CODE (constructor_type) == ARRAY_TYPE
12411 : 1702471 : && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
12412 : 10183272 : && integer_zerop (constructor_unfilled_index))
12413 : : {
12414 : 162975 : if (constructor_stack->replacement_value.value)
12415 : : {
12416 : 8 : error_init (loc, "excess elements in %qT initializer", constructor_type);
12417 : 411 : return;
12418 : : }
12419 : 162967 : else if (string_flag)
12420 : : {
12421 : 137 : constructor_stack->replacement_value = value;
12422 : 137 : return;
12423 : : }
12424 : : }
12425 : :
12426 : 9449304 : if (constructor_stack->replacement_value.value != NULL_TREE)
12427 : : {
12428 : 0 : error_init (loc, "excess elements in struct initializer");
12429 : 0 : return;
12430 : : }
12431 : :
12432 : : /* Ignore elements of a brace group if it is entirely superfluous
12433 : : and has already been diagnosed, or if the type is erroneous. */
12434 : 9449304 : if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
12435 : : return;
12436 : :
12437 : : /* Ignore elements of an initializer for a variable-size type.
12438 : : Those are diagnosed in the parser (empty initializer braces are OK). */
12439 : 9449099 : if (COMPLETE_TYPE_P (constructor_type)
12440 : 9449099 : && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
12441 : : return;
12442 : :
12443 : 8745465 : if (!implicit && warn_designated_init && !was_designated
12444 : 8713063 : && TREE_CODE (constructor_type) == RECORD_TYPE
12445 : 10483231 : && lookup_attribute ("designated_init",
12446 : 1034185 : TYPE_ATTRIBUTES (constructor_type)))
12447 : 50 : warning_init (loc,
12448 : : OPT_Wdesignated_init,
12449 : : "positional initialization of field "
12450 : : "in %<struct%> declared with %<designated_init%> attribute");
12451 : :
12452 : : /* If we've exhausted any levels that didn't have braces,
12453 : : pop them now. */
12454 : 10151017 : while (constructor_stack->implicit)
12455 : : {
12456 : 706793 : if (RECORD_OR_UNION_TYPE_P (constructor_type)
12457 : 702784 : && constructor_fields == NULL_TREE)
12458 : 701730 : process_init_element (loc,
12459 : : pop_init_level (loc, 1, braced_init_obstack,
12460 : : last_init_list_comma),
12461 : : true, braced_init_obstack);
12462 : 5063 : else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
12463 : 1698 : || gnu_vector_type_p (constructor_type))
12464 : 4009 : && constructor_max_index
12465 : 9045 : && tree_int_cst_lt (constructor_max_index,
12466 : : constructor_index))
12467 : 241 : process_init_element (loc,
12468 : : pop_init_level (loc, 1, braced_init_obstack,
12469 : : last_init_list_comma),
12470 : : true, braced_init_obstack);
12471 : : else
12472 : : break;
12473 : : }
12474 : :
12475 : : /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
12476 : 9449046 : if (constructor_range_stack)
12477 : : {
12478 : : /* If value is a compound literal and we'll be just using its
12479 : : content, don't put it into a SAVE_EXPR. */
12480 : 365 : if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
12481 : 3 : || !require_constant_value)
12482 : : {
12483 : 362 : tree semantic_type = NULL_TREE;
12484 : 362 : if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
12485 : : {
12486 : 0 : semantic_type = TREE_TYPE (value.value);
12487 : 0 : value.value = TREE_OPERAND (value.value, 0);
12488 : : }
12489 : 362 : value.value = save_expr (value.value);
12490 : 362 : if (semantic_type)
12491 : 0 : value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
12492 : : value.value);
12493 : : }
12494 : : }
12495 : :
12496 : 10162487 : while (1)
12497 : : {
12498 : 10162487 : if (TREE_CODE (constructor_type) == RECORD_TYPE)
12499 : : {
12500 : 1038224 : tree fieldtype;
12501 : 1038224 : enum tree_code fieldcode;
12502 : :
12503 : 1038224 : if (constructor_fields == NULL_TREE)
12504 : : {
12505 : 1280 : pedwarn_init (loc, 0, "excess elements in struct initializer");
12506 : 1280 : break;
12507 : : }
12508 : :
12509 : 1036944 : fieldtype = TREE_TYPE (constructor_fields);
12510 : 1036944 : if (fieldtype != error_mark_node)
12511 : 1036943 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12512 : 1036944 : fieldcode = TREE_CODE (fieldtype);
12513 : :
12514 : : /* Error for non-static initialization of a flexible array member. */
12515 : 1036944 : if (fieldcode == ARRAY_TYPE
12516 : 155256 : && !require_constant_value
12517 : 1112 : && TYPE_SIZE (fieldtype) == NULL_TREE
12518 : 1036954 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12519 : : {
12520 : 10 : error_init (loc, "non-static initialization of a flexible "
12521 : : "array member");
12522 : 10 : break;
12523 : : }
12524 : :
12525 : : /* Error for initialization of a flexible array member with
12526 : : a string constant if the structure is in an array. E.g.:
12527 : : struct S { int x; char y[]; };
12528 : : struct S s[] = { { 1, "foo" } };
12529 : : is invalid. */
12530 : 1036934 : if (string_flag
12531 : 1036934 : && fieldcode == ARRAY_TYPE
12532 : 3352 : && constructor_depth > 1
12533 : 489 : && TYPE_SIZE (fieldtype) == NULL_TREE
12534 : 1036981 : && DECL_CHAIN (constructor_fields) == NULL_TREE)
12535 : : {
12536 : 47 : bool in_array_p = false;
12537 : 47 : for (struct constructor_stack *p = constructor_stack;
12538 : 73 : p && p->type; p = p->next)
12539 : 59 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12540 : : {
12541 : : in_array_p = true;
12542 : : break;
12543 : : }
12544 : 47 : if (in_array_p)
12545 : : {
12546 : 33 : error_init (loc, "initialization of flexible array "
12547 : : "member in a nested context");
12548 : 33 : break;
12549 : : }
12550 : : }
12551 : :
12552 : : /* Accept a string constant to initialize a subarray. */
12553 : 1036901 : if (value.value != NULL_TREE
12554 : 1036828 : && fieldcode == ARRAY_TYPE
12555 : 155140 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12556 : 1191564 : && string_flag)
12557 : : value.value = orig_value;
12558 : : /* Otherwise, if we have come to a subaggregate,
12559 : : and we don't have an element of its type, push into it. */
12560 : 1033884 : else if (value.value != NULL_TREE
12561 : 1033582 : && initialize_elementwise_p (fieldtype, value.value))
12562 : : {
12563 : 302 : push_init_level (loc, 1, braced_init_obstack);
12564 : 302 : continue;
12565 : : }
12566 : :
12567 : 1036599 : value.value = maybe_split_raw_data (value.value, &raw_data);
12568 : 1036599 : if (value.value)
12569 : : {
12570 : 1036526 : push_member_name (constructor_fields);
12571 : 1036526 : output_init_element (loc, value.value, value.original_type,
12572 : : strict_string, fieldtype,
12573 : : constructor_fields, true, implicit,
12574 : : braced_init_obstack);
12575 : 1036526 : RESTORE_SPELLING_DEPTH (constructor_depth);
12576 : : }
12577 : : else
12578 : : /* Do the bookkeeping for an element that was
12579 : : directly output as a constructor. */
12580 : : {
12581 : : /* For a record, keep track of end position of last field. */
12582 : 73 : if (DECL_SIZE (constructor_fields))
12583 : 30 : constructor_bit_index
12584 : 30 : = size_binop_loc (input_location, PLUS_EXPR,
12585 : : bit_position (constructor_fields),
12586 : 30 : DECL_SIZE (constructor_fields));
12587 : :
12588 : : /* If the current field was the first one not yet written out,
12589 : : it isn't now, so update. */
12590 : 73 : if (constructor_unfilled_fields == constructor_fields)
12591 : : {
12592 : 64 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12593 : : /* Skip any nameless bit fields. */
12594 : 64 : while (constructor_unfilled_fields != 0
12595 : 64 : && (DECL_UNNAMED_BIT_FIELD
12596 : : (constructor_unfilled_fields)))
12597 : 0 : constructor_unfilled_fields =
12598 : 0 : DECL_CHAIN (constructor_unfilled_fields);
12599 : : }
12600 : : }
12601 : :
12602 : 1036599 : constructor_fields = DECL_CHAIN (constructor_fields);
12603 : : /* Skip any nameless bit fields at the beginning. */
12604 : 1036599 : while (constructor_fields != NULL_TREE
12605 : 1036700 : && DECL_UNNAMED_BIT_FIELD (constructor_fields))
12606 : 101 : constructor_fields = DECL_CHAIN (constructor_fields);
12607 : : }
12608 : 9124263 : else if (TREE_CODE (constructor_type) == UNION_TYPE)
12609 : : {
12610 : 30273 : tree fieldtype;
12611 : 30273 : enum tree_code fieldcode;
12612 : :
12613 : 30273 : if (constructor_fields == NULL_TREE)
12614 : : {
12615 : 3 : pedwarn_init (loc, 0,
12616 : : "excess elements in union initializer");
12617 : 3 : break;
12618 : : }
12619 : :
12620 : 30270 : fieldtype = TREE_TYPE (constructor_fields);
12621 : 30270 : if (fieldtype != error_mark_node)
12622 : 30270 : fieldtype = TYPE_MAIN_VARIANT (fieldtype);
12623 : 30270 : fieldcode = TREE_CODE (fieldtype);
12624 : :
12625 : : /* Warn that traditional C rejects initialization of unions.
12626 : : We skip the warning if the value is zero. This is done
12627 : : under the assumption that the zero initializer in user
12628 : : code appears conditioned on e.g. __STDC__ to avoid
12629 : : "missing initializer" warnings and relies on default
12630 : : initialization to zero in the traditional C case.
12631 : : We also skip the warning if the initializer is designated,
12632 : : again on the assumption that this must be conditional on
12633 : : __STDC__ anyway (and we've already complained about the
12634 : : member-designator already). */
12635 : 32336 : if (!in_system_header_at (input_location) && !constructor_designated
12636 : 31720 : && !(value.value && (integer_zerop (value.value)
12637 : 1351 : || real_zerop (value.value))))
12638 : 1344 : warning (OPT_Wtraditional, "traditional C rejects initialization "
12639 : : "of unions");
12640 : :
12641 : : /* Error for non-static initialization of a flexible array member. */
12642 : 30270 : if (fieldcode == ARRAY_TYPE
12643 : 766 : && !require_constant_value
12644 : 30327 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12645 : : {
12646 : 3 : error_init (loc, "non-static initialization of a flexible "
12647 : : "array member");
12648 : 3 : break;
12649 : : }
12650 : :
12651 : : /* Error for initialization of a flexible array member with
12652 : : a string constant if the structure is in an array. E.g.:
12653 : : union U { int x; char y[]; };
12654 : : union U s[] = { { 1, "foo" } };
12655 : : is invalid. */
12656 : 30267 : if (string_flag
12657 : 30267 : && fieldcode == ARRAY_TYPE
12658 : 7 : && constructor_depth > 1
12659 : 30271 : && TYPE_SIZE (fieldtype) == NULL_TREE)
12660 : : {
12661 : 3 : bool in_array_p = false;
12662 : 3 : for (struct constructor_stack *p = constructor_stack;
12663 : 3 : p && p->type; p = p->next)
12664 : 3 : if (TREE_CODE (p->type) == ARRAY_TYPE)
12665 : : {
12666 : : in_array_p = true;
12667 : : break;
12668 : : }
12669 : 3 : if (in_array_p)
12670 : : {
12671 : 3 : error_init (loc, "initialization of flexible array "
12672 : : "member in a nested context");
12673 : 3 : break;
12674 : : }
12675 : : }
12676 : :
12677 : : /* Accept a string constant to initialize a subarray. */
12678 : 30264 : if (value.value != NULL_TREE
12679 : 30264 : && fieldcode == ARRAY_TYPE
12680 : 760 : && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
12681 : 31019 : && string_flag)
12682 : : value.value = orig_value;
12683 : : /* Otherwise, if we have come to a subaggregate,
12684 : : and we don't have an element of its type, push into it. */
12685 : 30309 : else if (value.value != NULL_TREE
12686 : 30260 : && initialize_elementwise_p (fieldtype, value.value))
12687 : : {
12688 : 49 : push_init_level (loc, 1, braced_init_obstack);
12689 : 49 : continue;
12690 : : }
12691 : :
12692 : 30215 : value.value = maybe_split_raw_data (value.value, &raw_data);
12693 : 30215 : if (value.value)
12694 : : {
12695 : 30215 : push_member_name (constructor_fields);
12696 : 30215 : output_init_element (loc, value.value, value.original_type,
12697 : : strict_string, fieldtype,
12698 : : constructor_fields, true, implicit,
12699 : : braced_init_obstack);
12700 : 30215 : RESTORE_SPELLING_DEPTH (constructor_depth);
12701 : : }
12702 : : else
12703 : : /* Do the bookkeeping for an element that was
12704 : : directly output as a constructor. */
12705 : : {
12706 : 0 : constructor_bit_index = DECL_SIZE (constructor_fields);
12707 : 0 : constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
12708 : : }
12709 : :
12710 : 30215 : constructor_fields = NULL_TREE;
12711 : : }
12712 : 9093990 : else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
12713 : : {
12714 : 2416176 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12715 : 2416176 : enum tree_code eltcode = TREE_CODE (elttype);
12716 : :
12717 : : /* Accept a string constant to initialize a subarray. */
12718 : 2416176 : if (value.value != NULL_TREE
12719 : 2416176 : && eltcode == ARRAY_TYPE
12720 : 6867 : && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
12721 : 2421607 : && string_flag)
12722 : : value.value = orig_value;
12723 : : /* Otherwise, if we have come to a subaggregate,
12724 : : and we don't have an element of its type, push into it. */
12725 : 3117969 : else if (value.value != NULL_TREE
12726 : 2415681 : && initialize_elementwise_p (elttype, value.value))
12727 : : {
12728 : 702288 : push_init_level (loc, 1, braced_init_obstack);
12729 : 702288 : continue;
12730 : : }
12731 : :
12732 : 1713888 : if (constructor_max_index != NULL_TREE
12733 : 1713888 : && (tree_int_cst_lt (constructor_max_index, constructor_index)
12734 : 671180 : || integer_all_onesp (constructor_max_index)))
12735 : : {
12736 : 113 : pedwarn_init (loc, 0,
12737 : : "excess elements in array initializer");
12738 : 113 : break;
12739 : : }
12740 : :
12741 : 1713775 : if (value.value
12742 : 1713775 : && TREE_CODE (value.value) == RAW_DATA_CST
12743 : 191 : && RAW_DATA_LENGTH (value.value) > 1
12744 : 191 : && (TREE_CODE (elttype) == INTEGER_TYPE
12745 : 191 : || TREE_CODE (elttype) == BITINT_TYPE)
12746 : 191 : && TYPE_PRECISION (elttype) == CHAR_BIT
12747 : 1713966 : && (constructor_max_index == NULL_TREE
12748 : 53 : || tree_int_cst_lt (constructor_index,
12749 : : constructor_max_index)))
12750 : : {
12751 : 191 : unsigned int len = RAW_DATA_LENGTH (value.value);
12752 : 191 : if (constructor_max_index)
12753 : : {
12754 : 53 : widest_int w = wi::to_widest (constructor_max_index);
12755 : 53 : w -= wi::to_widest (constructor_index);
12756 : 53 : w += 1;
12757 : 53 : if (w < len)
12758 : 3 : len = w.to_uhwi ();
12759 : 53 : }
12760 : 191 : if (len < (unsigned) RAW_DATA_LENGTH (value.value))
12761 : : {
12762 : 3 : raw_data = copy_node (value.value);
12763 : 3 : RAW_DATA_LENGTH (raw_data) -= len;
12764 : 3 : RAW_DATA_POINTER (raw_data) += len;
12765 : 3 : RAW_DATA_LENGTH (value.value) = len;
12766 : : }
12767 : 191 : TREE_TYPE (value.value) = elttype;
12768 : 191 : push_array_bounds (tree_to_uhwi (constructor_index));
12769 : 191 : output_init_element (loc, value.value, value.original_type,
12770 : : false, elttype, constructor_index, true,
12771 : : implicit, braced_init_obstack);
12772 : 191 : RESTORE_SPELLING_DEPTH (constructor_depth);
12773 : 191 : constructor_index
12774 : 191 : = size_binop_loc (input_location, PLUS_EXPR,
12775 : 191 : constructor_index, bitsize_int (len));
12776 : : }
12777 : : else
12778 : : {
12779 : 1713584 : value.value = maybe_split_raw_data (value.value, &raw_data);
12780 : : /* Now output the actual element. */
12781 : 1713584 : if (value.value)
12782 : : {
12783 : 1713584 : push_array_bounds (tree_to_uhwi (constructor_index));
12784 : 1713584 : output_init_element (loc, value.value, value.original_type,
12785 : : strict_string, elttype,
12786 : : constructor_index, true, implicit,
12787 : : braced_init_obstack);
12788 : 1713584 : RESTORE_SPELLING_DEPTH (constructor_depth);
12789 : : }
12790 : :
12791 : 1713584 : constructor_index
12792 : 1713584 : = size_binop_loc (input_location, PLUS_EXPR,
12793 : : constructor_index, bitsize_one_node);
12794 : :
12795 : 1713584 : if (!value.value)
12796 : : /* If we are doing the bookkeeping for an element that was
12797 : : directly output as a constructor, we must update
12798 : : constructor_unfilled_index. */
12799 : 0 : constructor_unfilled_index = constructor_index;
12800 : : }
12801 : : }
12802 : 6677814 : else if (gnu_vector_type_p (constructor_type))
12803 : : {
12804 : 6677337 : tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
12805 : :
12806 : : /* Do a basic check of initializer size. Note that vectors
12807 : : may not always have a fixed size derived from their type. */
12808 : 6677337 : if (maybe_lt (tree_to_poly_uint64 (constructor_max_index),
12809 : 6677337 : tree_to_poly_uint64 (constructor_index)))
12810 : : {
12811 : : /* Diagose VLA out-of-bounds as errors. */
12812 : 2 : if (tree_to_poly_uint64 (constructor_max_index).is_constant())
12813 : 2 : pedwarn_init (loc, 0,
12814 : : "excess elements in vector initializer");
12815 : : else
12816 : : error_init (loc, "excess elements in vector initializer");
12817 : :
12818 : : break;
12819 : : }
12820 : :
12821 : 6677335 : value.value = maybe_split_raw_data (value.value, &raw_data);
12822 : : /* Now output the actual element. */
12823 : 6677335 : if (value.value)
12824 : : {
12825 : 6677335 : if (TREE_CODE (value.value) == VECTOR_CST)
12826 : 0 : elttype = TYPE_MAIN_VARIANT (constructor_type);
12827 : 6677335 : output_init_element (loc, value.value, value.original_type,
12828 : : strict_string, elttype,
12829 : : constructor_index, true, implicit,
12830 : : braced_init_obstack);
12831 : : }
12832 : :
12833 : 6677335 : constructor_index
12834 : 6677335 : = size_binop_loc (input_location,
12835 : : PLUS_EXPR, constructor_index, bitsize_one_node);
12836 : :
12837 : 6677335 : if (!value.value)
12838 : : /* If we are doing the bookkeeping for an element that was
12839 : : directly output as a constructor, we must update
12840 : : constructor_unfilled_index. */
12841 : 0 : constructor_unfilled_index = constructor_index;
12842 : : }
12843 : :
12844 : : /* Handle the sole element allowed in a braced initializer
12845 : : for a scalar variable. */
12846 : 477 : else if (constructor_type != error_mark_node
12847 : 477 : && constructor_fields == NULL_TREE)
12848 : : {
12849 : 29 : pedwarn_init (loc, 0,
12850 : : "excess elements in scalar initializer");
12851 : 29 : break;
12852 : : }
12853 : : else
12854 : : {
12855 : 448 : value.value = maybe_split_raw_data (value.value, &raw_data);
12856 : 448 : if (value.value)
12857 : 448 : output_init_element (loc, value.value, value.original_type,
12858 : : strict_string, constructor_type,
12859 : : NULL_TREE, true, implicit,
12860 : : braced_init_obstack);
12861 : 448 : constructor_fields = NULL_TREE;
12862 : : }
12863 : :
12864 : : /* Handle range initializers either at this level or anywhere higher
12865 : : in the designator stack. */
12866 : 9458372 : if (constructor_range_stack)
12867 : : {
12868 : 10802 : struct constructor_range_stack *p, *range_stack;
12869 : 10802 : int finish = 0;
12870 : :
12871 : 10802 : range_stack = constructor_range_stack;
12872 : 10802 : constructor_range_stack = 0;
12873 : 10802 : while (constructor_stack != range_stack->stack)
12874 : : {
12875 : 0 : gcc_assert (constructor_stack->implicit);
12876 : 0 : process_init_element (loc,
12877 : : pop_init_level (loc, 1,
12878 : : braced_init_obstack,
12879 : : last_init_list_comma),
12880 : : true, braced_init_obstack);
12881 : : }
12882 : 325 : for (p = range_stack;
12883 : 11127 : !p->range_end || tree_int_cst_equal (p->index, p->range_end);
12884 : 325 : p = p->prev)
12885 : : {
12886 : 325 : gcc_assert (constructor_stack->implicit);
12887 : 325 : process_init_element (loc,
12888 : : pop_init_level (loc, 1,
12889 : : braced_init_obstack,
12890 : : last_init_list_comma),
12891 : : true, braced_init_obstack);
12892 : : }
12893 : :
12894 : 10802 : p->index = size_binop_loc (input_location,
12895 : : PLUS_EXPR, p->index, bitsize_one_node);
12896 : 10802 : if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
12897 : 373 : finish = 1;
12898 : :
12899 : 11127 : while (1)
12900 : : {
12901 : 11127 : constructor_index = p->index;
12902 : 11127 : constructor_fields = p->fields;
12903 : 11127 : if (finish && p->range_end && p->index == p->range_start)
12904 : : {
12905 : 8 : finish = 0;
12906 : 8 : p->prev = 0;
12907 : : }
12908 : 11127 : p = p->next;
12909 : 11127 : if (!p)
12910 : : break;
12911 : 325 : finish_implicit_inits (loc, braced_init_obstack);
12912 : 325 : push_init_level (loc, 2, braced_init_obstack);
12913 : 325 : p->stack = constructor_stack;
12914 : 325 : if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
12915 : 33 : p->index = p->range_start;
12916 : : }
12917 : :
12918 : 10802 : if (!finish)
12919 : 10437 : constructor_range_stack = range_stack;
12920 : 10802 : continue;
12921 : 10802 : }
12922 : :
12923 : : break;
12924 : : }
12925 : :
12926 : 9449046 : constructor_range_stack = 0;
12927 : :
12928 : 9449046 : if (raw_data && RAW_DATA_LENGTH (raw_data))
12929 : : {
12930 : 212 : gcc_assert (!string_flag && !was_designated);
12931 : 212 : value.value = raw_data;
12932 : 212 : raw_data = NULL_TREE;
12933 : 212 : goto retry;
12934 : : }
12935 : : }
12936 : :
12937 : : /* Build a complete asm-statement, whose components are a CV_QUALIFIER
12938 : : (guaranteed to be 'volatile' or null) and ARGS (represented using
12939 : : an ASM_EXPR node). */
12940 : : tree
12941 : 201801 : build_asm_stmt (bool is_volatile, tree args)
12942 : : {
12943 : 201801 : if (is_volatile)
12944 : 162145 : ASM_VOLATILE_P (args) = 1;
12945 : 201801 : return add_stmt (args);
12946 : : }
12947 : :
12948 : : /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
12949 : : some INPUTS, and some CLOBBERS. The latter three may be NULL.
12950 : : SIMPLE indicates whether there was anything at all after the
12951 : : string in the asm expression -- asm("blah") and asm("blah" : )
12952 : : are subtly different. We use a ASM_EXPR node to represent this.
12953 : : LOC is the location of the asm, and IS_INLINE says whether this
12954 : : is asm inline. */
12955 : : tree
12956 : 201831 : build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
12957 : : tree clobbers, tree labels, bool simple, bool is_inline)
12958 : : {
12959 : 201831 : tree tail;
12960 : 201831 : tree args;
12961 : 201831 : int i;
12962 : 201831 : const char *constraint;
12963 : 201831 : const char **oconstraints;
12964 : 201831 : bool allows_mem, allows_reg, is_inout;
12965 : 201831 : int ninputs, noutputs;
12966 : :
12967 : 201831 : ninputs = list_length (inputs);
12968 : 201831 : noutputs = list_length (outputs);
12969 : 201831 : oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
12970 : :
12971 : 201831 : string = resolve_asm_operand_names (string, outputs, inputs, labels);
12972 : :
12973 : : /* Remove output conversions that change the type but not the mode. */
12974 : 542649 : for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
12975 : : {
12976 : 340818 : tree output = TREE_VALUE (tail);
12977 : :
12978 : 340818 : output = c_fully_fold (output, false, NULL, true);
12979 : :
12980 : : /* ??? Really, this should not be here. Users should be using a
12981 : : proper lvalue, dammit. But there's a long history of using casts
12982 : : in the output operands. In cases like longlong.h, this becomes a
12983 : : primitive form of typechecking -- if the cast can be removed, then
12984 : : the output operand had a type of the proper width; otherwise we'll
12985 : : get an error. Gross, but ... */
12986 : 340818 : STRIP_NOPS (output);
12987 : :
12988 : 340818 : if (!lvalue_or_else (loc, output, lv_asm))
12989 : 4 : output = error_mark_node;
12990 : :
12991 : 340818 : if (output != error_mark_node
12992 : 340818 : && (TREE_READONLY (output)
12993 : 340811 : || TYPE_READONLY (TREE_TYPE (output))
12994 : 340811 : || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
12995 : 112 : && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
12996 : 2 : readonly_error (loc, output, lv_asm);
12997 : :
12998 : 340818 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
12999 : 340818 : oconstraints[i] = constraint;
13000 : :
13001 : 340818 : if (parse_output_constraint (&constraint, i, ninputs, noutputs,
13002 : : &allows_mem, &allows_reg, &is_inout,
13003 : : nullptr))
13004 : : {
13005 : : /* If the operand is going to end up in memory,
13006 : : mark it addressable. */
13007 : 340810 : if (!allows_reg && !c_mark_addressable (output))
13008 : 3 : output = error_mark_node;
13009 : 1419 : if (!(!allows_reg && allows_mem)
13010 : 339391 : && output != error_mark_node
13011 : 680199 : && VOID_TYPE_P (TREE_TYPE (output)))
13012 : : {
13013 : 4 : error_at (loc, "invalid use of void expression");
13014 : 4 : output = error_mark_node;
13015 : : }
13016 : 340810 : if (allows_reg && current_function_decl == NULL_TREE)
13017 : : {
13018 : 1 : error_at (loc, "constraint allows registers outside of "
13019 : : "a function");
13020 : 1 : output = error_mark_node;
13021 : : }
13022 : : }
13023 : : else
13024 : 8 : output = error_mark_node;
13025 : :
13026 : 340818 : if (current_function_decl == NULL_TREE && output != error_mark_node)
13027 : : {
13028 : 7 : if (TREE_SIDE_EFFECTS (output))
13029 : : {
13030 : 0 : error_at (loc, "side-effects in output operand outside "
13031 : : "of a function");
13032 : 0 : output = error_mark_node;
13033 : : }
13034 : : else
13035 : : {
13036 : 7 : tree addr = build_unary_op (loc, ADDR_EXPR, output, false);
13037 : 7 : if (addr == error_mark_node)
13038 : : output = error_mark_node;
13039 : 7 : else if (!initializer_constant_valid_p (addr, TREE_TYPE (addr)))
13040 : : {
13041 : 1 : error_at (loc, "output operand outside of a function is not "
13042 : : "constant");
13043 : 1 : output = error_mark_node;
13044 : : }
13045 : : }
13046 : : }
13047 : 340811 : else if (output != error_mark_node && strstr (constraint, "-"))
13048 : : {
13049 : 1 : error_at (loc, "%<-%> modifier used inside of a function");
13050 : 1 : output = error_mark_node;
13051 : : }
13052 : :
13053 : 340818 : TREE_VALUE (tail) = output;
13054 : : }
13055 : :
13056 : 557640 : for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
13057 : : {
13058 : 355809 : tree input;
13059 : :
13060 : 355809 : constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
13061 : 355809 : input = TREE_VALUE (tail);
13062 : :
13063 : 355809 : if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
13064 : : oconstraints, &allows_mem, &allows_reg,
13065 : : nullptr))
13066 : : {
13067 : : /* If the operand is going to end up in memory,
13068 : : mark it addressable. */
13069 : 355802 : if (!allows_reg && allows_mem)
13070 : : {
13071 : 1395 : input = c_fully_fold (input, false, NULL, true);
13072 : :
13073 : : /* Strip the nops as we allow this case. FIXME, this really
13074 : : should be rejected or made deprecated. */
13075 : 1395 : STRIP_NOPS (input);
13076 : 1395 : if (!c_mark_addressable (input))
13077 : 2 : input = error_mark_node;
13078 : : }
13079 : : else
13080 : : {
13081 : 354407 : input = c_fully_fold (convert_lvalue_to_rvalue (loc, input,
13082 : : true, false),
13083 : : false, NULL);
13084 : :
13085 : 354407 : if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
13086 : : {
13087 : 4 : error_at (loc, "invalid use of void expression");
13088 : 4 : input = error_mark_node;
13089 : : }
13090 : : }
13091 : 355802 : if (allows_reg && current_function_decl == NULL_TREE)
13092 : : {
13093 : 1 : error_at (loc, "constraint allows registers outside of "
13094 : : "a function");
13095 : 1 : input = error_mark_node;
13096 : : }
13097 : 355802 : if (constraint[0] == ':' && input != error_mark_node)
13098 : : {
13099 : 15 : tree t = input;
13100 : 15 : STRIP_NOPS (t);
13101 : 15 : if (TREE_CODE (t) != ADDR_EXPR
13102 : 15 : || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
13103 : 10 : || (VAR_P (TREE_OPERAND (t, 0))
13104 : 8 : && is_global_var (TREE_OPERAND (t, 0)))))
13105 : : {
13106 : 5 : error_at (loc, "%<:%> constraint operand is not address "
13107 : : "of a function or non-automatic variable");
13108 : 5 : input = error_mark_node;
13109 : : }
13110 : : }
13111 : : }
13112 : : else
13113 : 7 : input = error_mark_node;
13114 : :
13115 : 355809 : if (current_function_decl == NULL_TREE && input != error_mark_node)
13116 : : {
13117 : 36 : if (TREE_SIDE_EFFECTS (input))
13118 : : {
13119 : 2 : error_at (loc, "side-effects in input operand outside "
13120 : : "of a function");
13121 : 2 : input = error_mark_node;
13122 : : }
13123 : : else
13124 : : {
13125 : 34 : tree tem = input;
13126 : 34 : if (allows_mem && lvalue_p (input))
13127 : 7 : tem = build_unary_op (loc, ADDR_EXPR, input, false);
13128 : 34 : if (!initializer_constant_valid_p (tem, TREE_TYPE (tem)))
13129 : : {
13130 : 2 : error_at (loc, "input operand outside of a function is not "
13131 : : "constant");
13132 : 2 : input = error_mark_node;
13133 : : }
13134 : : }
13135 : : }
13136 : 355773 : else if (input != error_mark_node && strstr (constraint, "-"))
13137 : : {
13138 : 3 : error_at (loc, "%<-%> modifier used inside of a function");
13139 : 3 : input = error_mark_node;
13140 : : }
13141 : :
13142 : 355809 : TREE_VALUE (tail) = input;
13143 : : }
13144 : :
13145 : 201831 : args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
13146 : :
13147 : : /* asm statements without outputs, including simple ones, are treated
13148 : : as volatile. */
13149 : 201831 : ASM_BASIC_P (args) = simple;
13150 : 201831 : ASM_VOLATILE_P (args) = (noutputs == 0);
13151 : 201831 : ASM_INLINE_P (args) = is_inline;
13152 : :
13153 : 201831 : return args;
13154 : : }
13155 : :
13156 : : /* Generate a goto statement to LABEL. LOC is the location of the
13157 : : GOTO. */
13158 : :
13159 : : tree
13160 : 82618 : c_finish_goto_label (location_t loc, tree label)
13161 : : {
13162 : 82618 : tree decl = lookup_label_for_goto (loc, label);
13163 : 82618 : if (!decl)
13164 : : return NULL_TREE;
13165 : 82618 : TREE_USED (decl) = 1;
13166 : 82618 : mark_decl_used (decl, false);
13167 : 82618 : {
13168 : 82618 : add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
13169 : 82618 : tree t = build1 (GOTO_EXPR, void_type_node, decl);
13170 : 82618 : SET_EXPR_LOCATION (t, loc);
13171 : 82618 : return add_stmt (t);
13172 : : }
13173 : : }
13174 : :
13175 : : /* Generate a computed goto statement to EXPR. LOC is the location of
13176 : : the GOTO. */
13177 : :
13178 : : tree
13179 : 931 : c_finish_goto_ptr (location_t loc, c_expr val)
13180 : : {
13181 : 931 : tree expr = val.value;
13182 : 931 : tree t;
13183 : 931 : pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
13184 : 931 : if (expr != error_mark_node
13185 : 930 : && !POINTER_TYPE_P (TREE_TYPE (expr))
13186 : 936 : && !null_pointer_constant_p (expr))
13187 : : {
13188 : 2 : error_at (val.get_location (),
13189 : : "computed goto must be pointer type");
13190 : 2 : expr = build_zero_cst (ptr_type_node);
13191 : : }
13192 : 931 : expr = c_fully_fold (expr, false, NULL);
13193 : 931 : expr = convert (ptr_type_node, expr);
13194 : 931 : t = build1 (GOTO_EXPR, void_type_node, expr);
13195 : 931 : SET_EXPR_LOCATION (t, loc);
13196 : 931 : return add_stmt (t);
13197 : : }
13198 : :
13199 : : /* Generate a C `return' statement. RETVAL is the expression for what
13200 : : to return, or a null pointer for `return;' with no value. LOC is
13201 : : the location of the return statement, or the location of the expression,
13202 : : if the statement has any. If ORIGTYPE is not NULL_TREE, it
13203 : : is the original type of RETVAL. MUSTTAIL_P indicates a musttail
13204 : : attribute. */
13205 : :
13206 : : tree
13207 : 33748781 : c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
13208 : : {
13209 : 33748781 : tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
13210 : 33748781 : bool no_warning = false;
13211 : 33748781 : bool npc = false;
13212 : :
13213 : : /* Use the expansion point to handle cases such as returning NULL
13214 : : in a function returning void. */
13215 : 33748781 : location_t xloc = expansion_point_location_if_in_system_header (loc);
13216 : :
13217 : 33748781 : if (TREE_THIS_VOLATILE (current_function_decl))
13218 : 23 : warning_at (xloc, 0,
13219 : : "function declared %<noreturn%> has a %<return%> statement");
13220 : :
13221 : 33748781 : set_musttail_on_return (retval, xloc, musttail_p);
13222 : :
13223 : 33748781 : if (retval)
13224 : : {
13225 : 33727792 : tree semantic_type = NULL_TREE;
13226 : 33727792 : npc = null_pointer_constant_p (retval);
13227 : 33727792 : if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
13228 : : {
13229 : 92 : semantic_type = TREE_TYPE (retval);
13230 : 92 : retval = TREE_OPERAND (retval, 0);
13231 : : }
13232 : 33727792 : retval = c_fully_fold (retval, false, NULL);
13233 : 33727792 : if (semantic_type
13234 : 33727792 : && valtype != NULL_TREE
13235 : 92 : && TREE_CODE (valtype) != VOID_TYPE)
13236 : 92 : retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
13237 : : }
13238 : :
13239 : 33727792 : if (!retval)
13240 : : {
13241 : 20989 : current_function_returns_null = 1;
13242 : 20989 : if ((warn_return_type >= 0 || flag_isoc99)
13243 : 20864 : && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
13244 : : {
13245 : 46 : no_warning = true;
13246 : 47 : if (emit_diagnostic (flag_isoc99
13247 : : ? diagnostics::kind::permerror
13248 : : : diagnostics::kind::warning,
13249 : 46 : loc, OPT_Wreturn_mismatch,
13250 : : "%<return%> with no value,"
13251 : : " in function returning non-void"))
13252 : 27 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13253 : : "declared here");
13254 : : }
13255 : : }
13256 : 33727792 : else if (valtype == NULL_TREE || VOID_TYPE_P (valtype))
13257 : : {
13258 : 321 : current_function_returns_null = 1;
13259 : 321 : bool warned_here;
13260 : 321 : if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
13261 : 64 : warned_here = permerror_opt
13262 : 64 : (xloc, OPT_Wreturn_mismatch,
13263 : : "%<return%> with a value, in function returning void");
13264 : : else
13265 : 257 : warned_here = pedwarn
13266 : 257 : (xloc, OPT_Wpedantic, "ISO C forbids "
13267 : : "%<return%> with expression, in function returning void");
13268 : 321 : if (warned_here)
13269 : 43 : inform (DECL_SOURCE_LOCATION (current_function_decl),
13270 : : "declared here");
13271 : : }
13272 : : else
13273 : : {
13274 : 33727471 : tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
13275 : : retval, origtype, ic_return,
13276 : : npc, NULL_TREE, NULL_TREE, 0);
13277 : 33727471 : tree res = DECL_RESULT (current_function_decl);
13278 : 33727471 : tree inner;
13279 : 33727471 : bool save;
13280 : :
13281 : 33727471 : current_function_returns_value = 1;
13282 : 33727471 : if (t == error_mark_node)
13283 : : {
13284 : : /* Suppress -Wreturn-type for this function. */
13285 : 298 : if (warn_return_type)
13286 : 297 : suppress_warning (current_function_decl, OPT_Wreturn_type);
13287 : 298 : return NULL_TREE;
13288 : : }
13289 : :
13290 : 33727173 : save = in_late_binary_op;
13291 : 67446820 : if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
13292 : 33718842 : || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
13293 : 67444735 : || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13294 : 327752 : && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
13295 : 327752 : || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
13296 : 0 : && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
13297 : 9611 : in_late_binary_op = true;
13298 : 33727173 : inner = t = convert (TREE_TYPE (res), t);
13299 : 33727173 : in_late_binary_op = save;
13300 : :
13301 : : /* Strip any conversions, additions, and subtractions, and see if
13302 : : we are returning the address of a local variable. Warn if so. */
13303 : 36924648 : while (1)
13304 : : {
13305 : 36924648 : switch (TREE_CODE (inner))
13306 : : {
13307 : 3196059 : CASE_CONVERT:
13308 : 3196059 : case NON_LVALUE_EXPR:
13309 : 3196059 : case PLUS_EXPR:
13310 : 3196059 : case POINTER_PLUS_EXPR:
13311 : 3196059 : inner = TREE_OPERAND (inner, 0);
13312 : 3196059 : continue;
13313 : :
13314 : 1421 : case MINUS_EXPR:
13315 : : /* If the second operand of the MINUS_EXPR has a pointer
13316 : : type (or is converted from it), this may be valid, so
13317 : : don't give a warning. */
13318 : 1421 : {
13319 : 1421 : tree op1 = TREE_OPERAND (inner, 1);
13320 : :
13321 : 3089 : while (!POINTER_TYPE_P (TREE_TYPE (op1))
13322 : 3341 : && (CONVERT_EXPR_P (op1)
13323 : 1416 : || TREE_CODE (op1) == NON_LVALUE_EXPR))
13324 : 252 : op1 = TREE_OPERAND (op1, 0);
13325 : :
13326 : 1421 : if (POINTER_TYPE_P (TREE_TYPE (op1)))
13327 : : break;
13328 : :
13329 : 1416 : inner = TREE_OPERAND (inner, 0);
13330 : 1416 : continue;
13331 : 1416 : }
13332 : :
13333 : 2889 : case ADDR_EXPR:
13334 : 2889 : inner = TREE_OPERAND (inner, 0);
13335 : :
13336 : 2889 : while (REFERENCE_CLASS_P (inner)
13337 : 3763 : && !INDIRECT_REF_P (inner))
13338 : 874 : inner = TREE_OPERAND (inner, 0);
13339 : :
13340 : 2889 : if (DECL_P (inner)
13341 : 1580 : && !DECL_EXTERNAL (inner)
13342 : 951 : && DECL_CONTEXT (inner) == current_function_decl
13343 : 3106 : && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
13344 : : {
13345 : 177 : if (TREE_CODE (inner) == LABEL_DECL)
13346 : 4 : warning_at (loc, OPT_Wreturn_local_addr,
13347 : : "function returns address of label");
13348 : 173 : else if (TREE_CODE (inner) == FUNCTION_DECL
13349 : 173 : && (C_FUNC_NONLOCAL_CONTEXT (inner)
13350 : 8 : || !DECL_INITIAL (inner)))
13351 : 10 : warning_at (loc, OPT_Wreturn_local_addr,
13352 : : "function returns address of nested function "
13353 : : "referencing local context");
13354 : 163 : else if (TREE_CODE (inner) != FUNCTION_DECL
13355 : 156 : && !TREE_STATIC (inner))
13356 : : {
13357 : 9 : warning_at (loc, OPT_Wreturn_local_addr,
13358 : : "function returns address of local variable");
13359 : 9 : tree zero = build_zero_cst (TREE_TYPE (res));
13360 : 9 : t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
13361 : : }
13362 : : }
13363 : : break;
13364 : :
13365 : : default:
13366 : : break;
13367 : 3196059 : }
13368 : :
13369 : 33727173 : break;
13370 : : }
13371 : :
13372 : 33727173 : retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
13373 : 33727173 : SET_EXPR_LOCATION (retval, loc);
13374 : :
13375 : 33727173 : if (warn_sequence_point)
13376 : 2958376 : verify_sequence_points (retval);
13377 : : }
13378 : :
13379 : 33748483 : ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
13380 : 33748483 : if (no_warning)
13381 : 46 : suppress_warning (ret_stmt, OPT_Wreturn_type);
13382 : 33748483 : return add_stmt (ret_stmt);
13383 : : }
13384 : :
13385 : : struct c_switch {
13386 : : /* The SWITCH_STMT being built. */
13387 : : tree switch_stmt;
13388 : :
13389 : : /* The original type of the testing expression, i.e. before the
13390 : : default conversion is applied. */
13391 : : tree orig_type;
13392 : :
13393 : : /* A splay-tree mapping the low element of a case range to the high
13394 : : element, or NULL_TREE if there is no high element. Used to
13395 : : determine whether or not a new case label duplicates an old case
13396 : : label. We need a tree, rather than simply a hash table, because
13397 : : of the GNU case range extension. */
13398 : : splay_tree cases;
13399 : :
13400 : : /* The bindings at the point of the switch. This is used for
13401 : : warnings crossing decls when branching to a case label. */
13402 : : struct c_spot_bindings *bindings;
13403 : :
13404 : : /* Whether the switch includes any break statements. */
13405 : : bool break_stmt_seen_p;
13406 : :
13407 : : /* The next node on the stack. */
13408 : : struct c_switch *next;
13409 : :
13410 : : /* Remember whether the controlling expression had boolean type
13411 : : before integer promotions for the sake of -Wswitch-bool. */
13412 : : bool bool_cond_p;
13413 : : };
13414 : :
13415 : : /* A stack of the currently active switch statements. The innermost
13416 : : switch statement is on the top of the stack. There is no need to
13417 : : mark the stack for garbage collection because it is only active
13418 : : during the processing of the body of a function, and we never
13419 : : collect at that point. */
13420 : :
13421 : : struct c_switch *c_switch_stack;
13422 : :
13423 : : /* Start a C switch statement, testing expression EXP. Return the new
13424 : : SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
13425 : : SWITCH_COND_LOC is the location of the switch's condition.
13426 : : EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
13427 : :
13428 : : tree
13429 : 36617 : c_start_switch (location_t switch_loc,
13430 : : location_t switch_cond_loc,
13431 : : tree exp, bool explicit_cast_p, tree switch_name)
13432 : : {
13433 : 36617 : tree orig_type = error_mark_node;
13434 : 36617 : bool bool_cond_p = false;
13435 : 36617 : struct c_switch *cs;
13436 : :
13437 : 36617 : if (exp != error_mark_node)
13438 : : {
13439 : 36574 : orig_type = TREE_TYPE (exp);
13440 : :
13441 : 36574 : if (!INTEGRAL_TYPE_P (orig_type))
13442 : : {
13443 : 12 : if (orig_type != error_mark_node)
13444 : : {
13445 : 12 : error_at (switch_cond_loc, "switch quantity not an integer");
13446 : 12 : orig_type = error_mark_node;
13447 : : }
13448 : 12 : exp = integer_zero_node;
13449 : : }
13450 : : else
13451 : : {
13452 : 36562 : tree type = TYPE_MAIN_VARIANT (orig_type);
13453 : 36562 : tree e = exp;
13454 : :
13455 : : /* Warn if the condition has boolean value. */
13456 : 36565 : while (TREE_CODE (e) == COMPOUND_EXPR)
13457 : 3 : e = TREE_OPERAND (e, 1);
13458 : :
13459 : 36537 : if ((C_BOOLEAN_TYPE_P (type)
13460 : 36537 : || truth_value_p (TREE_CODE (e)))
13461 : : /* Explicit cast to int suppresses this warning. */
13462 : 36584 : && !(TREE_CODE (type) == INTEGER_TYPE
13463 : : && explicit_cast_p))
13464 : : bool_cond_p = true;
13465 : :
13466 : 36562 : if (!in_system_header_at (input_location)
13467 : 36562 : && (type == long_integer_type_node
13468 : 11856 : || type == long_unsigned_type_node))
13469 : 258 : warning_at (switch_cond_loc,
13470 : 258 : OPT_Wtraditional, "%<long%> switch expression not "
13471 : : "converted to %<int%> in ISO C");
13472 : :
13473 : 36562 : exp = c_fully_fold (exp, false, NULL);
13474 : 36562 : exp = default_conversion (exp);
13475 : :
13476 : 36562 : if (warn_sequence_point)
13477 : 6005 : verify_sequence_points (exp);
13478 : : }
13479 : : }
13480 : :
13481 : : /* Add this new SWITCH_STMT to the stack. */
13482 : 36617 : cs = XNEW (struct c_switch);
13483 : 36617 : cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
13484 : : NULL_TREE, orig_type, NULL_TREE, switch_name);
13485 : 36617 : cs->orig_type = orig_type;
13486 : 36617 : cs->cases = splay_tree_new (case_compare, NULL, NULL);
13487 : 36617 : cs->bindings = c_get_switch_bindings ();
13488 : 36617 : cs->break_stmt_seen_p = false;
13489 : 36617 : cs->bool_cond_p = bool_cond_p;
13490 : 36617 : cs->next = c_switch_stack;
13491 : 36617 : c_switch_stack = cs;
13492 : :
13493 : 36617 : return add_stmt (cs->switch_stmt);
13494 : : }
13495 : :
13496 : : /* Process a case label at location LOC, with attributes ATTRS. */
13497 : :
13498 : : tree
13499 : 1024581 : do_case (location_t loc, tree low_value, tree high_value, tree attrs)
13500 : : {
13501 : 1024581 : tree label = NULL_TREE;
13502 : :
13503 : 1024581 : if (low_value && TREE_CODE (low_value) != INTEGER_CST)
13504 : : {
13505 : 87 : low_value = c_fully_fold (low_value, false, NULL);
13506 : 87 : if (TREE_CODE (low_value) == INTEGER_CST)
13507 : 9 : pedwarn (loc, OPT_Wpedantic,
13508 : : "case label is not an integer constant expression");
13509 : : }
13510 : :
13511 : 1024581 : if (high_value && TREE_CODE (high_value) != INTEGER_CST)
13512 : : {
13513 : 0 : high_value = c_fully_fold (high_value, false, NULL);
13514 : 0 : if (TREE_CODE (high_value) == INTEGER_CST)
13515 : 0 : pedwarn (input_location, OPT_Wpedantic,
13516 : : "case label is not an integer constant expression");
13517 : : }
13518 : :
13519 : 1024581 : if (c_switch_stack == NULL)
13520 : : {
13521 : 3 : if (low_value)
13522 : 2 : error_at (loc, "case label not within a switch statement");
13523 : : else
13524 : 1 : error_at (loc, "%<default%> label not within a switch statement");
13525 : 3 : return NULL_TREE;
13526 : : }
13527 : :
13528 : 1024578 : if (c_check_switch_jump_warnings (c_switch_stack->bindings,
13529 : 1024578 : EXPR_LOCATION (c_switch_stack->switch_stmt),
13530 : : loc))
13531 : : return NULL_TREE;
13532 : :
13533 : 1024566 : label = c_add_case_label (loc, c_switch_stack->cases,
13534 : 1024566 : SWITCH_STMT_COND (c_switch_stack->switch_stmt),
13535 : : low_value, high_value, attrs);
13536 : 1024566 : if (label == error_mark_node)
13537 : 144 : label = NULL_TREE;
13538 : : return label;
13539 : : }
13540 : :
13541 : : /* Finish the switch statement. TYPE is the original type of the
13542 : : controlling expression of the switch, or NULL_TREE. */
13543 : :
13544 : : void
13545 : 36617 : c_finish_switch (tree body, tree type)
13546 : : {
13547 : 36617 : struct c_switch *cs = c_switch_stack;
13548 : 36617 : location_t switch_location;
13549 : :
13550 : 36617 : SWITCH_STMT_BODY (cs->switch_stmt) = body;
13551 : :
13552 : : /* Emit warnings as needed. */
13553 : 36617 : switch_location = EXPR_LOCATION (cs->switch_stmt);
13554 : 42714 : c_do_switch_warnings (cs->cases, switch_location,
13555 : 6097 : type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
13556 : 36617 : SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
13557 : 36617 : if (c_switch_covers_all_cases_p (cs->cases,
13558 : 36617 : SWITCH_STMT_TYPE (cs->switch_stmt)))
13559 : 32736 : SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
13560 : 36617 : SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
13561 : :
13562 : : /* Pop the stack. */
13563 : 36617 : c_switch_stack = cs->next;
13564 : 36617 : splay_tree_delete (cs->cases);
13565 : 36617 : c_release_switch_bindings (cs->bindings);
13566 : 36617 : XDELETE (cs);
13567 : 36617 : }
13568 : :
13569 : : /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
13570 : : THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
13571 : : may be null. */
13572 : :
13573 : : void
13574 : 1249759 : c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
13575 : : tree else_block)
13576 : : {
13577 : 1249759 : tree stmt;
13578 : :
13579 : 1249759 : stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
13580 : 1249759 : SET_EXPR_LOCATION (stmt, if_locus);
13581 : 1249759 : add_stmt (stmt);
13582 : 1249759 : }
13583 : :
13584 : : tree
13585 : 183500 : c_finish_bc_stmt (location_t loc, tree label, bool is_break, tree name)
13586 : : {
13587 : : /* In switch statements break is sometimes stylistically used after
13588 : : a return statement. This can lead to spurious warnings about
13589 : : control reaching the end of a non-void function when it is
13590 : : inlined. Note that we are calling block_may_fallthru with
13591 : : language specific tree nodes; this works because
13592 : : block_may_fallthru returns true when given something it does not
13593 : : understand. */
13594 : 183500 : bool skip = !block_may_fallthru (cur_stmt_list);
13595 : :
13596 : 183500 : if (is_break)
13597 : 171239 : switch (in_statement & ~IN_NAMED_STMT)
13598 : : {
13599 : 9 : case 0:
13600 : 9 : error_at (loc, "break statement not within loop or switch");
13601 : 9 : return NULL_TREE;
13602 : 4 : case IN_OMP_BLOCK:
13603 : 4 : error_at (loc, "invalid exit from OpenMP structured block");
13604 : 4 : return NULL_TREE;
13605 : 12 : case IN_OMP_FOR:
13606 : 12 : error_at (loc, "break statement used with OpenMP for loop");
13607 : 12 : return NULL_TREE;
13608 : : case IN_ITERATION_STMT:
13609 : : case IN_OBJC_FOREACH:
13610 : : break;
13611 : 155728 : default:
13612 : 155728 : gcc_assert (in_statement & IN_SWITCH_STMT);
13613 : 155728 : c_switch_stack->break_stmt_seen_p = true;
13614 : 155728 : break;
13615 : : }
13616 : : else
13617 : 12261 : switch (in_statement & ~(IN_SWITCH_STMT | IN_NAMED_STMT))
13618 : : {
13619 : 7 : case 0:
13620 : 7 : error_at (loc, "continue statement not within a loop");
13621 : 7 : return NULL_TREE;
13622 : 4 : case IN_OMP_BLOCK:
13623 : 4 : error_at (loc, "invalid exit from OpenMP structured block");
13624 : 4 : return NULL_TREE;
13625 : : case IN_ITERATION_STMT:
13626 : : case IN_OMP_FOR:
13627 : : case IN_OBJC_FOREACH:
13628 : : break;
13629 : 0 : default:
13630 : 0 : gcc_unreachable ();
13631 : : }
13632 : :
13633 : 183464 : if (skip)
13634 : : return NULL_TREE;
13635 : 182996 : else if ((in_statement & IN_OBJC_FOREACH)
13636 : 0 : && !(is_break && (in_statement & IN_SWITCH_STMT))
13637 : 0 : && name == NULL_TREE)
13638 : : {
13639 : : /* The foreach expander produces low-level code using gotos instead
13640 : : of a structured loop construct. */
13641 : 0 : gcc_assert (label);
13642 : 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13643 : : }
13644 : 183093 : else if (name && C_DECL_LOOP_NAME (name) && C_DECL_SWITCH_NAME (name))
13645 : : {
13646 : 0 : label = DECL_CHAIN (name);
13647 : 0 : if (!is_break)
13648 : 0 : label = DECL_CHAIN (label);
13649 : : /* Foreach expander from some outer level. */
13650 : 0 : return add_stmt (build_stmt (loc, GOTO_EXPR, label));
13651 : : }
13652 : 195246 : return add_stmt (build_stmt (loc, is_break ? BREAK_STMT : CONTINUE_STMT,
13653 : 182996 : name));
13654 : : }
13655 : :
13656 : : /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
13657 : :
13658 : : static void
13659 : 6245856 : emit_side_effect_warnings (location_t loc, tree expr)
13660 : : {
13661 : 6245856 : maybe_warn_nodiscard (loc, expr);
13662 : 6245856 : if (!warn_unused_value)
13663 : : return;
13664 : 1291779 : if (expr == error_mark_node)
13665 : : ;
13666 : 1291704 : else if (!TREE_SIDE_EFFECTS (expr))
13667 : : {
13668 : 6408 : if (!VOID_TYPE_P (TREE_TYPE (expr))
13669 : 6408 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13670 : 28 : warning_at (loc, OPT_Wunused_value, "statement with no effect");
13671 : : }
13672 : 1285296 : else if (TREE_CODE (expr) == COMPOUND_EXPR)
13673 : : {
13674 : : tree r = expr;
13675 : : location_t cloc = loc;
13676 : 11991 : while (TREE_CODE (r) == COMPOUND_EXPR)
13677 : : {
13678 : 6011 : if (EXPR_HAS_LOCATION (r))
13679 : 5481 : cloc = EXPR_LOCATION (r);
13680 : 6011 : r = TREE_OPERAND (r, 1);
13681 : : }
13682 : 5980 : if (!TREE_SIDE_EFFECTS (r)
13683 : 33 : && !VOID_TYPE_P (TREE_TYPE (r))
13684 : 24 : && !CONVERT_EXPR_P (r)
13685 : 24 : && !warning_suppressed_p (r, OPT_Wunused_value)
13686 : 5992 : && !warning_suppressed_p (expr, OPT_Wunused_value))
13687 : 8 : warning_at (cloc, OPT_Wunused_value,
13688 : : "right-hand operand of comma expression has no effect");
13689 : : }
13690 : : else
13691 : 1279316 : warn_if_unused_value (expr, loc);
13692 : : }
13693 : :
13694 : : /* Process an expression as if it were a complete statement. Emit
13695 : : diagnostics, but do not call ADD_STMT. LOC is the location of the
13696 : : statement. */
13697 : :
13698 : : tree
13699 : 6122560 : c_process_expr_stmt (location_t loc, tree expr)
13700 : : {
13701 : 6122560 : tree exprv;
13702 : :
13703 : 6122560 : if (!expr)
13704 : : return NULL_TREE;
13705 : :
13706 : 6118343 : expr = c_fully_fold (expr, false, NULL);
13707 : :
13708 : 6118343 : if (warn_sequence_point)
13709 : 1287619 : verify_sequence_points (expr);
13710 : :
13711 : 6118343 : if (TREE_TYPE (expr) != error_mark_node
13712 : 6115787 : && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
13713 : 6118343 : && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
13714 : 0 : error_at (loc, "expression statement has incomplete type");
13715 : :
13716 : : /* If we're not processing a statement expression, warn about unused values.
13717 : : Warnings for statement expressions will be emitted later, once we figure
13718 : : out which is the result. */
13719 : 6118343 : if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13720 : 6118343 : && (warn_unused_value || warn_unused_result))
13721 : 6032043 : emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
13722 : :
13723 : : exprv = expr;
13724 : 6156785 : while (TREE_CODE (exprv) == COMPOUND_EXPR)
13725 : 38443 : exprv = TREE_OPERAND (exprv, 1);
13726 : 6129363 : while (CONVERT_EXPR_P (exprv))
13727 : 11021 : exprv = TREE_OPERAND (exprv, 0);
13728 : 6118342 : if (DECL_P (exprv)
13729 : 6118889 : || handled_component_p (exprv)
13730 : 12217255 : || TREE_CODE (exprv) == ADDR_EXPR)
13731 : 19976 : mark_exp_read (exprv);
13732 : :
13733 : : /* If the expression is not of a type to which we cannot assign a line
13734 : : number, wrap the thing in a no-op NOP_EXPR. */
13735 : 6118342 : if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
13736 : : {
13737 : 14638 : expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13738 : 14638 : SET_EXPR_LOCATION (expr, loc);
13739 : : }
13740 : :
13741 : : return expr;
13742 : : }
13743 : :
13744 : : /* Emit an expression as a statement. LOC is the location of the
13745 : : expression. */
13746 : :
13747 : : tree
13748 : 5832591 : c_finish_expr_stmt (location_t loc, tree expr)
13749 : : {
13750 : 5832591 : if (expr)
13751 : 5820308 : return add_stmt (c_process_expr_stmt (loc, expr));
13752 : : else
13753 : : return NULL;
13754 : : }
13755 : :
13756 : : /* Do the opposite and emit a statement as an expression. To begin,
13757 : : create a new binding level and return it. */
13758 : :
13759 : : tree
13760 : 34725 : c_begin_stmt_expr (void)
13761 : : {
13762 : 34725 : tree ret;
13763 : :
13764 : : /* We must force a BLOCK for this level so that, if it is not expanded
13765 : : later, there is a way to turn off the entire subtree of blocks that
13766 : : are contained in it. */
13767 : 34725 : keep_next_level ();
13768 : 34725 : ret = c_begin_compound_stmt (true);
13769 : :
13770 : 34725 : c_bindings_start_stmt_expr (c_switch_stack == NULL
13771 : : ? NULL
13772 : : : c_switch_stack->bindings);
13773 : :
13774 : : /* Mark the current statement list as belonging to a statement list. */
13775 : 34725 : STATEMENT_LIST_STMT_EXPR (ret) = 1;
13776 : :
13777 : 34725 : return ret;
13778 : : }
13779 : :
13780 : : /* LOC is the location of the compound statement to which this body
13781 : : belongs. */
13782 : :
13783 : : tree
13784 : 34725 : c_finish_stmt_expr (location_t loc, tree body)
13785 : : {
13786 : 34725 : tree last, type, tmp, val;
13787 : 34725 : tree *last_p;
13788 : :
13789 : 34725 : body = c_end_compound_stmt (loc, body, true);
13790 : :
13791 : 34725 : c_bindings_end_stmt_expr (c_switch_stack == NULL
13792 : : ? NULL
13793 : : : c_switch_stack->bindings);
13794 : :
13795 : : /* Locate the last statement in BODY. See c_end_compound_stmt
13796 : : about always returning a BIND_EXPR. */
13797 : 34725 : last_p = &BIND_EXPR_BODY (body);
13798 : 34725 : last = BIND_EXPR_BODY (body);
13799 : :
13800 : 34725 : continue_searching:
13801 : 34725 : if (TREE_CODE (last) == STATEMENT_LIST)
13802 : : {
13803 : 25933 : tree_stmt_iterator l = tsi_last (last);
13804 : :
13805 : 25939 : while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
13806 : 6 : tsi_prev (&l);
13807 : :
13808 : : /* This can happen with degenerate cases like ({ }). No value. */
13809 : 25933 : if (tsi_end_p (l))
13810 : 389 : return body;
13811 : :
13812 : : /* If we're supposed to generate side effects warnings, process
13813 : : all of the statements except the last. */
13814 : 25544 : if (warn_unused_value || warn_unused_result)
13815 : : {
13816 : 25544 : for (tree_stmt_iterator i = tsi_start (last);
13817 : 275294 : tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
13818 : : {
13819 : 249750 : location_t tloc;
13820 : 249750 : tree t = tsi_stmt (i);
13821 : :
13822 : 249750 : tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
13823 : 249750 : emit_side_effect_warnings (tloc, t);
13824 : : }
13825 : : }
13826 : 25544 : last_p = tsi_stmt_ptr (l);
13827 : 25544 : last = *last_p;
13828 : : }
13829 : :
13830 : : /* If the end of the list is exception related, then the list was split
13831 : : by a call to push_cleanup. Continue searching. */
13832 : 34336 : if (TREE_CODE (last) == TRY_FINALLY_EXPR
13833 : 34336 : || TREE_CODE (last) == TRY_CATCH_EXPR)
13834 : : {
13835 : 0 : last_p = &TREE_OPERAND (last, 0);
13836 : 0 : last = *last_p;
13837 : 0 : goto continue_searching;
13838 : : }
13839 : :
13840 : 34336 : if (last == error_mark_node)
13841 : : return last;
13842 : :
13843 : : /* In the case that the BIND_EXPR is not necessary, return the
13844 : : expression out from inside it. */
13845 : 34327 : if ((last == BIND_EXPR_BODY (body)
13846 : : /* Skip nested debug stmts. */
13847 : 25543 : || last == expr_first (BIND_EXPR_BODY (body)))
13848 : 45256 : && BIND_EXPR_VARS (body) == NULL)
13849 : : {
13850 : : /* Even if this looks constant, do not allow it in a constant
13851 : : expression. */
13852 : 10917 : last = c_wrap_maybe_const (last, true);
13853 : : /* Do not warn if the return value of a statement expression is
13854 : : unused. */
13855 : 10917 : suppress_warning (last, OPT_Wunused);
13856 : 10917 : return last;
13857 : : }
13858 : :
13859 : : /* Extract the type of said expression. */
13860 : 23410 : type = TREE_TYPE (last);
13861 : :
13862 : : /* If we're not returning a value at all, then the BIND_EXPR that
13863 : : we already have is a fine expression to return. */
13864 : 23410 : if (!type || VOID_TYPE_P (type))
13865 : : return body;
13866 : :
13867 : : /* Now that we've located the expression containing the value, it seems
13868 : : silly to make voidify_wrapper_expr repeat the process. Create a
13869 : : temporary of the appropriate type and stick it in a TARGET_EXPR. */
13870 : 18102 : tmp = create_tmp_var_raw (type);
13871 : :
13872 : : /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
13873 : : tree_expr_nonnegative_p giving up immediately. */
13874 : 18102 : val = last;
13875 : 18102 : if (TREE_CODE (val) == NOP_EXPR
13876 : 18102 : && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
13877 : 3728 : val = TREE_OPERAND (val, 0);
13878 : :
13879 : 18102 : *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
13880 : 18102 : SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
13881 : :
13882 : 18102 : {
13883 : 18102 : tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
13884 : 18102 : SET_EXPR_LOCATION (t, loc);
13885 : 18102 : return t;
13886 : : }
13887 : : }
13888 : :
13889 : : /* Begin and end compound statements. This is as simple as pushing
13890 : : and popping new statement lists from the tree. */
13891 : :
13892 : : tree
13893 : 40132548 : c_begin_compound_stmt (bool do_scope)
13894 : : {
13895 : 40132548 : tree stmt = push_stmt_list ();
13896 : 40132548 : if (do_scope)
13897 : 40036571 : push_scope ();
13898 : 40132548 : return stmt;
13899 : : }
13900 : :
13901 : : /* End a compound statement. STMT is the statement. LOC is the
13902 : : location of the compound statement-- this is usually the location
13903 : : of the opening brace. */
13904 : :
13905 : : tree
13906 : 40132547 : c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
13907 : : {
13908 : 40132547 : tree block = NULL;
13909 : :
13910 : 40132547 : if (do_scope)
13911 : : {
13912 : 40036570 : if (c_dialect_objc ())
13913 : 0 : objc_clear_super_receiver ();
13914 : 40036570 : block = pop_scope ();
13915 : : }
13916 : :
13917 : 40132547 : stmt = pop_stmt_list (stmt);
13918 : 40132547 : stmt = c_build_bind_expr (loc, block, stmt);
13919 : :
13920 : : /* If this compound statement is nested immediately inside a statement
13921 : : expression, then force a BIND_EXPR to be created. Otherwise we'll
13922 : : do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
13923 : : STATEMENT_LISTs merge, and thus we can lose track of what statement
13924 : : was really last. */
13925 : 80265094 : if (building_stmt_list_p ()
13926 : 40132462 : && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
13927 : 40255081 : && TREE_CODE (stmt) != BIND_EXPR)
13928 : : {
13929 : 120794 : stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
13930 : 120794 : TREE_SIDE_EFFECTS (stmt) = 1;
13931 : 120794 : SET_EXPR_LOCATION (stmt, loc);
13932 : : }
13933 : :
13934 : 40132547 : return stmt;
13935 : : }
13936 : :
13937 : : /* Queue a cleanup. CLEANUP is an expression/statement to be executed
13938 : : when the current scope is exited. EH_ONLY is true when this is not
13939 : : meant to apply to normal control flow transfer. */
13940 : :
13941 : : void
13942 : 95 : push_cleanup (tree decl, tree cleanup, bool eh_only)
13943 : : {
13944 : 95 : enum tree_code code;
13945 : 95 : tree stmt, list;
13946 : 95 : bool stmt_expr;
13947 : :
13948 : 95 : code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
13949 : 95 : stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
13950 : 95 : add_stmt (stmt);
13951 : 95 : stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
13952 : 95 : list = push_stmt_list ();
13953 : 95 : TREE_OPERAND (stmt, 0) = list;
13954 : 95 : STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
13955 : 95 : }
13956 : :
13957 : : /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
13958 : : into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
13959 : :
13960 : : static tree
13961 : 98700 : build_vec_cmp (tree_code code, tree type,
13962 : : tree arg0, tree arg1)
13963 : : {
13964 : 98700 : tree zero_vec = build_zero_cst (type);
13965 : 98700 : tree minus_one_vec = build_minus_one_cst (type);
13966 : 98700 : tree cmp_type = truth_type_for (TREE_TYPE (arg0));
13967 : 98700 : tree cmp = build2 (code, cmp_type, arg0, arg1);
13968 : 98700 : return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
13969 : : }
13970 : :
13971 : : /* Possibly warn about an address of OP never being NULL in a comparison
13972 : : operation CODE involving null. */
13973 : :
13974 : : static void
13975 : 50553 : maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
13976 : : {
13977 : : /* Prevent warnings issued for macro expansion. */
13978 : 50553 : if (!warn_address
13979 : 29815 : || warning_suppressed_p (op, OPT_Waddress)
13980 : 80044 : || from_macro_expansion_at (loc))
13981 : 23795 : return;
13982 : :
13983 : 26758 : if (TREE_CODE (op) == NOP_EXPR)
13984 : : {
13985 : : /* Allow casts to intptr_t to suppress the warning. */
13986 : 1133 : tree type = TREE_TYPE (op);
13987 : 1133 : if (TREE_CODE (type) == INTEGER_TYPE)
13988 : : return;
13989 : 1133 : op = TREE_OPERAND (op, 0);
13990 : : }
13991 : :
13992 : 26758 : if (TREE_CODE (op) == POINTER_PLUS_EXPR)
13993 : : {
13994 : : /* Allow a cast to void* to suppress the warning. */
13995 : 24 : tree type = TREE_TYPE (TREE_TYPE (op));
13996 : 24 : if (VOID_TYPE_P (type))
13997 : : return;
13998 : :
13999 : : /* Adding any value to a null pointer, including zero, is undefined
14000 : : in C. This includes the expression &p[0] where p is the null
14001 : : pointer, although &p[0] will have been folded to p by this point
14002 : : and so not diagnosed. */
14003 : 24 : if (code == EQ_EXPR)
14004 : 22 : warning_at (loc, OPT_Waddress,
14005 : : "the comparison will always evaluate as %<false%> "
14006 : : "for the pointer operand in %qE must not be NULL",
14007 : : op);
14008 : : else
14009 : 2 : warning_at (loc, OPT_Waddress,
14010 : : "the comparison will always evaluate as %<true%> "
14011 : : "for the pointer operand in %qE must not be NULL",
14012 : : op);
14013 : :
14014 : 24 : return;
14015 : : }
14016 : :
14017 : 26734 : if (TREE_CODE (op) != ADDR_EXPR)
14018 : : return;
14019 : :
14020 : 254 : op = TREE_OPERAND (op, 0);
14021 : :
14022 : 254 : if (TREE_CODE (op) == IMAGPART_EXPR
14023 : 254 : || TREE_CODE (op) == REALPART_EXPR)
14024 : : {
14025 : : /* The address of either complex part may not be null. */
14026 : 14 : if (code == EQ_EXPR)
14027 : 9 : warning_at (loc, OPT_Waddress,
14028 : : "the comparison will always evaluate as %<false%> "
14029 : : "for the address of %qE will never be NULL",
14030 : : op);
14031 : : else
14032 : 5 : warning_at (loc, OPT_Waddress,
14033 : : "the comparison will always evaluate as %<true%> "
14034 : : "for the address of %qE will never be NULL",
14035 : : op);
14036 : 14 : return;
14037 : : }
14038 : :
14039 : : /* Set to true in the loop below if OP dereferences is operand.
14040 : : In such a case the ultimate target need not be a decl for
14041 : : the null [in]equality test to be constant. */
14042 : : bool deref = false;
14043 : :
14044 : : /* Get the outermost array or object, or member. */
14045 : 294 : while (handled_component_p (op))
14046 : : {
14047 : 72 : if (TREE_CODE (op) == COMPONENT_REF)
14048 : : {
14049 : : /* Get the member (its address is never null). */
14050 : 18 : op = TREE_OPERAND (op, 1);
14051 : 18 : break;
14052 : : }
14053 : :
14054 : : /* Get the outer array/object to refer to in the warning. */
14055 : 54 : op = TREE_OPERAND (op, 0);
14056 : 54 : deref = true;
14057 : : }
14058 : :
14059 : 192 : if ((!deref && !decl_with_nonnull_addr_p (op))
14060 : 371 : || from_macro_expansion_at (loc))
14061 : 109 : return;
14062 : :
14063 : 131 : bool w;
14064 : 131 : if (code == EQ_EXPR)
14065 : 93 : w = warning_at (loc, OPT_Waddress,
14066 : : "the comparison will always evaluate as %<false%> "
14067 : : "for the address of %qE will never be NULL",
14068 : : op);
14069 : : else
14070 : 38 : w = warning_at (loc, OPT_Waddress,
14071 : : "the comparison will always evaluate as %<true%> "
14072 : : "for the address of %qE will never be NULL",
14073 : : op);
14074 : :
14075 : 131 : if (w && DECL_P (op))
14076 : 120 : inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
14077 : : }
14078 : :
14079 : : /* Build a binary-operation expression without default conversions.
14080 : : CODE is the kind of expression to build.
14081 : : LOCATION is the operator's location.
14082 : : This function differs from `build' in several ways:
14083 : : the data type of the result is computed and recorded in it,
14084 : : warnings are generated if arg data types are invalid,
14085 : : special handling for addition and subtraction of pointers is known,
14086 : : and some optimization is done (operations on narrow ints
14087 : : are done in the narrower type when that gives the same result).
14088 : : Constant folding is also done before the result is returned.
14089 : :
14090 : : Note that the operands will never have enumeral types, or function
14091 : : or array types, because either they will have the default conversions
14092 : : performed or they have both just been converted to some other type in which
14093 : : the arithmetic is to be done. */
14094 : :
14095 : : tree
14096 : 16456263 : build_binary_op (location_t location, enum tree_code code,
14097 : : tree orig_op0, tree orig_op1, bool convert_p)
14098 : : {
14099 : 16456263 : tree type0, type1, orig_type0, orig_type1;
14100 : 16456263 : tree eptype;
14101 : 16456263 : enum tree_code code0, code1;
14102 : 16456263 : tree op0, op1;
14103 : 16456263 : tree ret = error_mark_node;
14104 : 16456263 : const char *invalid_op_diag;
14105 : 16456263 : bool op0_int_operands, op1_int_operands;
14106 : 16456263 : bool int_const, int_const_or_overflow, int_operands;
14107 : :
14108 : : /* Expression code to give to the expression when it is built.
14109 : : Normally this is CODE, which is what the caller asked for,
14110 : : but in some special cases we change it. */
14111 : 16456263 : enum tree_code resultcode = code;
14112 : :
14113 : : /* Data type in which the computation is to be performed.
14114 : : In the simplest cases this is the common type of the arguments. */
14115 : 16456263 : tree result_type = NULL;
14116 : :
14117 : : /* When the computation is in excess precision, the type of the
14118 : : final EXCESS_PRECISION_EXPR. */
14119 : 16456263 : tree semantic_result_type = NULL;
14120 : :
14121 : : /* Nonzero means operands have already been type-converted
14122 : : in whatever way is necessary.
14123 : : Zero means they need to be converted to RESULT_TYPE. */
14124 : 16456263 : int converted = 0;
14125 : :
14126 : : /* Nonzero means create the expression with this type, rather than
14127 : : RESULT_TYPE. */
14128 : 16456263 : tree build_type = NULL_TREE;
14129 : :
14130 : : /* Nonzero means after finally constructing the expression
14131 : : convert it to this type. */
14132 : 16456263 : tree final_type = NULL_TREE;
14133 : :
14134 : : /* Nonzero if this is an operation like MIN or MAX which can
14135 : : safely be computed in short if both args are promoted shorts.
14136 : : Also implies COMMON.
14137 : : -1 indicates a bitwise operation; this makes a difference
14138 : : in the exact conditions for when it is safe to do the operation
14139 : : in a narrower mode. */
14140 : 16456263 : int shorten = 0;
14141 : :
14142 : : /* Nonzero if this is a comparison operation;
14143 : : if both args are promoted shorts, compare the original shorts.
14144 : : Also implies COMMON. */
14145 : 16456263 : int short_compare = 0;
14146 : :
14147 : : /* Nonzero if this is a right-shift operation, which can be computed on the
14148 : : original short and then promoted if the operand is a promoted short. */
14149 : 16456263 : int short_shift = 0;
14150 : :
14151 : : /* Nonzero means set RESULT_TYPE to the common type of the args. */
14152 : 16456263 : int common = 0;
14153 : :
14154 : : /* True means types are compatible as far as ObjC is concerned. */
14155 : 16456263 : bool objc_ok;
14156 : :
14157 : : /* True means this is an arithmetic operation that may need excess
14158 : : precision. */
14159 : 16456263 : bool may_need_excess_precision;
14160 : :
14161 : : /* True means this is a boolean operation that converts both its
14162 : : operands to truth-values. */
14163 : 16456263 : bool boolean_op = false;
14164 : :
14165 : : /* Remember whether we're doing / or %. */
14166 : 16456263 : bool doing_div_or_mod = false;
14167 : :
14168 : : /* Remember whether we're doing << or >>. */
14169 : 16456263 : bool doing_shift = false;
14170 : :
14171 : : /* Tree holding instrumentation expression. */
14172 : 16456263 : tree instrument_expr = NULL;
14173 : :
14174 : 16456263 : if (location == UNKNOWN_LOCATION)
14175 : 79 : location = input_location;
14176 : :
14177 : 16456263 : op0 = orig_op0;
14178 : 16456263 : op1 = orig_op1;
14179 : :
14180 : 16456263 : op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
14181 : 7945322 : if (op0_int_operands)
14182 : 7945322 : op0 = remove_c_maybe_const_expr (op0);
14183 : 16456263 : op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
14184 : 11151251 : if (op1_int_operands)
14185 : 11151251 : op1 = remove_c_maybe_const_expr (op1);
14186 : 27607514 : int_operands = (op0_int_operands && op1_int_operands);
14187 : 11151251 : if (int_operands)
14188 : : {
14189 : 15544231 : int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
14190 : 7787282 : && TREE_CODE (orig_op1) == INTEGER_CST);
14191 : 7756949 : int_const = (int_const_or_overflow
14192 : 7756949 : && !TREE_OVERFLOW (orig_op0)
14193 : 7756880 : && !TREE_OVERFLOW (orig_op1));
14194 : : }
14195 : : else
14196 : : int_const = int_const_or_overflow = false;
14197 : :
14198 : : /* Do not apply default conversion in mixed vector/scalar expression. */
14199 : 16456263 : if (convert_p
14200 : 16456263 : && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
14201 : : {
14202 : 9937336 : op0 = default_conversion (op0);
14203 : 9937336 : op1 = default_conversion (op1);
14204 : : }
14205 : :
14206 : 16456263 : orig_type0 = type0 = TREE_TYPE (op0);
14207 : :
14208 : 16456263 : orig_type1 = type1 = TREE_TYPE (op1);
14209 : :
14210 : : /* The expression codes of the data types of the arguments tell us
14211 : : whether the arguments are integers, floating, pointers, etc. */
14212 : 16456263 : code0 = TREE_CODE (type0);
14213 : 16456263 : code1 = TREE_CODE (type1);
14214 : :
14215 : : /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
14216 : 16456265 : STRIP_TYPE_NOPS (op0);
14217 : 16456266 : STRIP_TYPE_NOPS (op1);
14218 : :
14219 : : /* If an error was already reported for one of the arguments,
14220 : : avoid reporting another error. */
14221 : :
14222 : 16456263 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14223 : 710 : return error_mark_node;
14224 : :
14225 : 16455553 : if (code0 == POINTER_TYPE
14226 : 16455553 : && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
14227 : 11 : return error_mark_node;
14228 : :
14229 : 16455542 : if (code1 == POINTER_TYPE
14230 : 16455542 : && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
14231 : 11 : return error_mark_node;
14232 : :
14233 : 32911062 : if ((invalid_op_diag
14234 : 16455531 : = targetm.invalid_binary_op (code, type0, type1)))
14235 : : {
14236 : 0 : error_at (location, invalid_op_diag);
14237 : 0 : return error_mark_node;
14238 : : }
14239 : :
14240 : 16455531 : switch (code)
14241 : : {
14242 : : case PLUS_EXPR:
14243 : : case MINUS_EXPR:
14244 : : case MULT_EXPR:
14245 : : case TRUNC_DIV_EXPR:
14246 : : case CEIL_DIV_EXPR:
14247 : : case FLOOR_DIV_EXPR:
14248 : : case ROUND_DIV_EXPR:
14249 : : case EXACT_DIV_EXPR:
14250 : : may_need_excess_precision = true;
14251 : : break;
14252 : :
14253 : 2682612 : case EQ_EXPR:
14254 : 2682612 : case NE_EXPR:
14255 : 2682612 : case LE_EXPR:
14256 : 2682612 : case GE_EXPR:
14257 : 2682612 : case LT_EXPR:
14258 : 2682612 : case GT_EXPR:
14259 : : /* Excess precision for implicit conversions of integers to
14260 : : floating point in C11 and later. */
14261 : 2682612 : may_need_excess_precision = (flag_isoc11
14262 : 2682612 : && (ANY_INTEGRAL_TYPE_P (type0)
14263 : 471093 : || ANY_INTEGRAL_TYPE_P (type1)));
14264 : : break;
14265 : :
14266 : : default:
14267 : 16455531 : may_need_excess_precision = false;
14268 : : break;
14269 : : }
14270 : 16455531 : if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
14271 : : {
14272 : 184 : op0 = TREE_OPERAND (op0, 0);
14273 : 184 : type0 = TREE_TYPE (op0);
14274 : : }
14275 : 16455347 : else if (may_need_excess_precision
14276 : 16455347 : && (eptype = excess_precision_type (type0)) != NULL_TREE)
14277 : : {
14278 : 771 : type0 = eptype;
14279 : 771 : op0 = convert (eptype, op0);
14280 : : }
14281 : 16455531 : if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
14282 : : {
14283 : 887 : op1 = TREE_OPERAND (op1, 0);
14284 : 887 : type1 = TREE_TYPE (op1);
14285 : : }
14286 : 16454644 : else if (may_need_excess_precision
14287 : 16454644 : && (eptype = excess_precision_type (type1)) != NULL_TREE)
14288 : : {
14289 : 592 : type1 = eptype;
14290 : 592 : op1 = convert (eptype, op1);
14291 : : }
14292 : :
14293 : 16455531 : objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
14294 : :
14295 : : /* In case when one of the operands of the binary operation is
14296 : : a vector and another is a scalar -- convert scalar to vector. */
14297 : 17996934 : if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
14298 : 17995099 : || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
14299 : : {
14300 : 2438 : enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
14301 : : orig_op1, true);
14302 : :
14303 : 2438 : switch (convert_flag)
14304 : : {
14305 : 12 : case stv_error:
14306 : 12 : return error_mark_node;
14307 : 590 : case stv_firstarg:
14308 : 590 : {
14309 : 590 : bool maybe_const = true;
14310 : 590 : tree sc;
14311 : 590 : sc = c_fully_fold (op0, false, &maybe_const);
14312 : 590 : sc = save_expr (sc);
14313 : 590 : sc = convert (TREE_TYPE (type1), sc);
14314 : 590 : op0 = build_vector_from_val (type1, sc);
14315 : 590 : if (!maybe_const)
14316 : 93 : op0 = c_wrap_maybe_const (op0, true);
14317 : 590 : orig_type0 = type0 = TREE_TYPE (op0);
14318 : 590 : code0 = TREE_CODE (type0);
14319 : 590 : converted = 1;
14320 : 590 : break;
14321 : : }
14322 : 1013 : case stv_secondarg:
14323 : 1013 : {
14324 : 1013 : bool maybe_const = true;
14325 : 1013 : tree sc;
14326 : 1013 : sc = c_fully_fold (op1, false, &maybe_const);
14327 : 1013 : sc = save_expr (sc);
14328 : 1013 : sc = convert (TREE_TYPE (type0), sc);
14329 : 1013 : op1 = build_vector_from_val (type0, sc);
14330 : 1013 : if (!maybe_const)
14331 : 36 : op1 = c_wrap_maybe_const (op1, true);
14332 : 1013 : orig_type1 = type1 = TREE_TYPE (op1);
14333 : 1013 : code1 = TREE_CODE (type1);
14334 : 1013 : converted = 1;
14335 : 1013 : break;
14336 : : }
14337 : : default:
14338 : : break;
14339 : : }
14340 : : }
14341 : :
14342 : 16455519 : switch (code)
14343 : : {
14344 : 8423249 : case PLUS_EXPR:
14345 : : /* Handle the pointer + int case. */
14346 : 8423249 : if (code0 == POINTER_TYPE
14347 : 1219460 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14348 : : {
14349 : 1219454 : ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
14350 : 1219454 : goto return_build_binary_op;
14351 : : }
14352 : 7203795 : else if (code1 == POINTER_TYPE
14353 : 1531 : && (code0 == INTEGER_TYPE || code0 == BITINT_TYPE))
14354 : : {
14355 : 1525 : ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
14356 : 1525 : goto return_build_binary_op;
14357 : : }
14358 : : else
14359 : : common = 1;
14360 : : break;
14361 : :
14362 : 781687 : case MINUS_EXPR:
14363 : : /* Subtraction of two similar pointers.
14364 : : We must subtract them as integers, then divide by object size. */
14365 : 781687 : if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
14366 : 781687 : && comp_target_types (location, type0, type1))
14367 : : {
14368 : 3625 : ret = pointer_diff (location, op0, op1, &instrument_expr);
14369 : 3625 : goto return_build_binary_op;
14370 : : }
14371 : : /* Handle pointer minus int. Just like pointer plus int. */
14372 : 778062 : else if (code0 == POINTER_TYPE
14373 : 16664 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14374 : : {
14375 : 16652 : ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
14376 : 16652 : goto return_build_binary_op;
14377 : : }
14378 : : else
14379 : : common = 1;
14380 : : break;
14381 : :
14382 : : case MULT_EXPR:
14383 : : common = 1;
14384 : : break;
14385 : :
14386 : 263782 : case TRUNC_DIV_EXPR:
14387 : 263782 : case CEIL_DIV_EXPR:
14388 : 263782 : case FLOOR_DIV_EXPR:
14389 : 263782 : case ROUND_DIV_EXPR:
14390 : 263782 : case EXACT_DIV_EXPR:
14391 : 263782 : doing_div_or_mod = true;
14392 : 263782 : warn_for_div_by_zero (location, op1);
14393 : :
14394 : 263781 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14395 : 48521 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14396 : 46543 : || code0 == COMPLEX_TYPE
14397 : 44495 : || gnu_vector_type_p (type0))
14398 : 312301 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14399 : 46095 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14400 : 45956 : || code1 == COMPLEX_TYPE
14401 : 44497 : || gnu_vector_type_p (type1)))
14402 : : {
14403 : 263777 : enum tree_code tcode0 = code0, tcode1 = code1;
14404 : :
14405 : 263777 : if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
14406 : 46542 : tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
14407 : 263777 : if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
14408 : 45953 : tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
14409 : :
14410 : 263777 : if (!(((tcode0 == INTEGER_TYPE || tcode0 == BITINT_TYPE)
14411 : 196043 : && (tcode1 == INTEGER_TYPE || tcode1 == BITINT_TYPE))
14412 : 79763 : || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
14413 : : resultcode = RDIV_EXPR;
14414 : : else
14415 : : /* Although it would be tempting to shorten always here, that
14416 : : loses on some targets, since the modulo instruction is
14417 : : undefined if the quotient can't be represented in the
14418 : : computation mode. We shorten only if unsigned or if
14419 : : dividing by something we know != -1. */
14420 : 184014 : shorten = may_shorten_divmod (op0, op1);
14421 : : common = 1;
14422 : : }
14423 : : break;
14424 : :
14425 : 1540558 : case BIT_AND_EXPR:
14426 : 1540558 : case BIT_IOR_EXPR:
14427 : 1540558 : case BIT_XOR_EXPR:
14428 : 1540558 : if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14429 : 1018213 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14430 : : shorten = -1;
14431 : : /* Allow vector types which are not floating point types. */
14432 : 522381 : else if (gnu_vector_type_p (type0)
14433 : 522304 : && gnu_vector_type_p (type1)
14434 : 522304 : && !VECTOR_FLOAT_TYPE_P (type0)
14435 : 1044682 : && !VECTOR_FLOAT_TYPE_P (type1))
14436 : : common = 1;
14437 : : break;
14438 : :
14439 : 57089 : case TRUNC_MOD_EXPR:
14440 : 57089 : case FLOOR_MOD_EXPR:
14441 : 57089 : doing_div_or_mod = true;
14442 : 57089 : warn_for_div_by_zero (location, op1);
14443 : :
14444 : 57089 : if (gnu_vector_type_p (type0)
14445 : 297 : && gnu_vector_type_p (type1)
14446 : 297 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14447 : 57386 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
14448 : : common = 1;
14449 : 56792 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14450 : 56791 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14451 : : {
14452 : : /* Although it would be tempting to shorten always here, that loses
14453 : : on some targets, since the modulo instruction is undefined if the
14454 : : quotient can't be represented in the computation mode. We shorten
14455 : : only if unsigned or if dividing by something we know != -1. */
14456 : 56791 : shorten = may_shorten_divmod (op0, op1);
14457 : 56791 : common = 1;
14458 : : }
14459 : : break;
14460 : :
14461 : 503128 : case TRUTH_ANDIF_EXPR:
14462 : 503128 : case TRUTH_ORIF_EXPR:
14463 : 503128 : case TRUTH_AND_EXPR:
14464 : 503128 : case TRUTH_OR_EXPR:
14465 : 503128 : case TRUTH_XOR_EXPR:
14466 : 503128 : if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
14467 : 0 : || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14468 : 0 : || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE
14469 : 0 : || code0 == BITINT_TYPE)
14470 : 503128 : && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
14471 : 159 : || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14472 : 20 : || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE
14473 : 18 : || code1 == BITINT_TYPE))
14474 : : {
14475 : : /* Result of these operations is always an int,
14476 : : but that does not mean the operands should be
14477 : : converted to ints! */
14478 : 503128 : result_type = integer_type_node;
14479 : 503128 : if (op0_int_operands)
14480 : : {
14481 : 110759 : op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
14482 : 110759 : op0 = remove_c_maybe_const_expr (op0);
14483 : : }
14484 : : else
14485 : 392369 : op0 = c_objc_common_truthvalue_conversion (location, op0);
14486 : 503128 : if (op1_int_operands)
14487 : : {
14488 : 72269 : op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
14489 : 72269 : op1 = remove_c_maybe_const_expr (op1);
14490 : : }
14491 : : else
14492 : 430859 : op1 = c_objc_common_truthvalue_conversion (location, op1);
14493 : : converted = 1;
14494 : : boolean_op = true;
14495 : : }
14496 : 503128 : if (code == TRUTH_ANDIF_EXPR)
14497 : : {
14498 : 245671 : int_const_or_overflow = (int_operands
14499 : 64145 : && TREE_CODE (orig_op0) == INTEGER_CST
14500 : 245687 : && (op0 == truthvalue_false_node
14501 : 13845 : || TREE_CODE (orig_op1) == INTEGER_CST));
14502 : : int_const = (int_const_or_overflow
14503 : 64118 : && !TREE_OVERFLOW (orig_op0)
14504 : 64118 : && (op0 == truthvalue_false_node
14505 : 13829 : || !TREE_OVERFLOW (orig_op1)));
14506 : : }
14507 : 321575 : else if (code == TRUTH_ORIF_EXPR)
14508 : : {
14509 : 328240 : int_const_or_overflow = (int_operands
14510 : 6816 : && TREE_CODE (orig_op0) == INTEGER_CST
14511 : 328263 : && (op0 == truthvalue_true_node
14512 : 2133 : || TREE_CODE (orig_op1) == INTEGER_CST));
14513 : : int_const = (int_const_or_overflow
14514 : 6786 : && !TREE_OVERFLOW (orig_op0)
14515 : 6786 : && (op0 == truthvalue_true_node
14516 : 2110 : || !TREE_OVERFLOW (orig_op1)));
14517 : : }
14518 : : break;
14519 : :
14520 : : /* Shift operations: result has same type as first operand;
14521 : : always convert second operand to int.
14522 : : Also set SHORT_SHIFT if shifting rightward. */
14523 : :
14524 : 226883 : case RSHIFT_EXPR:
14525 : 226883 : if (gnu_vector_type_p (type0)
14526 : 795 : && gnu_vector_type_p (type1)
14527 : 257 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14528 : 255 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14529 : 227138 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14530 : : TYPE_VECTOR_SUBPARTS (type1)))
14531 : : {
14532 : : result_type = type0;
14533 : : converted = 1;
14534 : : }
14535 : 226630 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14536 : 1070 : || code0 == BITINT_TYPE
14537 : 546 : || (gnu_vector_type_p (type0)
14538 : 542 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14539 : 227693 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14540 : : {
14541 : 226621 : doing_shift = true;
14542 : 226621 : if (TREE_CODE (op1) == INTEGER_CST)
14543 : : {
14544 : 197773 : if (tree_int_cst_sgn (op1) < 0)
14545 : : {
14546 : 269 : int_const = false;
14547 : 269 : if (c_inhibit_evaluation_warnings == 0)
14548 : 129 : warning_at (location, OPT_Wshift_count_negative,
14549 : : "right shift count is negative");
14550 : : }
14551 : 197504 : else if (code0 == VECTOR_TYPE)
14552 : : {
14553 : 454 : if (compare_tree_int (op1,
14554 : 454 : TYPE_PRECISION (TREE_TYPE (type0)))
14555 : : >= 0)
14556 : : {
14557 : 16 : int_const = false;
14558 : 16 : if (c_inhibit_evaluation_warnings == 0)
14559 : 16 : warning_at (location, OPT_Wshift_count_overflow,
14560 : : "right shift count >= width of vector element");
14561 : : }
14562 : : }
14563 : : else
14564 : : {
14565 : 197050 : if (!integer_zerop (op1))
14566 : 196692 : short_shift = 1;
14567 : :
14568 : 197050 : if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14569 : : {
14570 : 584 : int_const = false;
14571 : 584 : if (c_inhibit_evaluation_warnings == 0)
14572 : 50 : warning_at (location, OPT_Wshift_count_overflow,
14573 : : "right shift count >= width of type");
14574 : : }
14575 : : }
14576 : : }
14577 : :
14578 : : /* Use the type of the value to be shifted. */
14579 : : result_type = type0;
14580 : : /* Avoid converting op1 to result_type later. */
14581 : : converted = 1;
14582 : : }
14583 : : break;
14584 : :
14585 : 783479 : case LSHIFT_EXPR:
14586 : 783479 : if (gnu_vector_type_p (type0)
14587 : 595 : && gnu_vector_type_p (type1)
14588 : 316 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
14589 : 314 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
14590 : 783792 : && known_eq (TYPE_VECTOR_SUBPARTS (type0),
14591 : : TYPE_VECTOR_SUBPARTS (type1)))
14592 : : {
14593 : : result_type = type0;
14594 : : converted = 1;
14595 : : }
14596 : 783168 : else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
14597 : 749 : || code0 == BITINT_TYPE
14598 : 290 : || (gnu_vector_type_p (type0)
14599 : 284 : && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
14600 : 783908 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14601 : : {
14602 : 783154 : doing_shift = true;
14603 : 783154 : if (TREE_CODE (op0) == INTEGER_CST
14604 : 427434 : && tree_int_cst_sgn (op0) < 0
14605 : 783903 : && !TYPE_OVERFLOW_WRAPS (type0))
14606 : : {
14607 : : /* Don't reject a left shift of a negative value in a context
14608 : : where a constant expression is needed in C90. */
14609 : 712 : if (flag_isoc99)
14610 : 666 : int_const = false;
14611 : 712 : if (c_inhibit_evaluation_warnings == 0)
14612 : 712 : warning_at (location, OPT_Wshift_negative_value,
14613 : : "left shift of negative value");
14614 : : }
14615 : 783154 : if (TREE_CODE (op1) == INTEGER_CST)
14616 : : {
14617 : 713729 : if (tree_int_cst_sgn (op1) < 0)
14618 : : {
14619 : 313 : int_const = false;
14620 : 313 : if (c_inhibit_evaluation_warnings == 0)
14621 : 135 : warning_at (location, OPT_Wshift_count_negative,
14622 : : "left shift count is negative");
14623 : : }
14624 : 713416 : else if (code0 == VECTOR_TYPE)
14625 : : {
14626 : 215 : if (compare_tree_int (op1,
14627 : 215 : TYPE_PRECISION (TREE_TYPE (type0)))
14628 : : >= 0)
14629 : : {
14630 : 6 : int_const = false;
14631 : 6 : if (c_inhibit_evaluation_warnings == 0)
14632 : 6 : warning_at (location, OPT_Wshift_count_overflow,
14633 : : "left shift count >= width of vector element");
14634 : : }
14635 : : }
14636 : 713201 : else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
14637 : : {
14638 : 29731 : int_const = false;
14639 : 29731 : if (c_inhibit_evaluation_warnings == 0)
14640 : 111 : warning_at (location, OPT_Wshift_count_overflow,
14641 : : "left shift count >= width of type");
14642 : : }
14643 : 683470 : else if (TREE_CODE (op0) == INTEGER_CST
14644 : 356086 : && maybe_warn_shift_overflow (location, op0, op1)
14645 : 684098 : && flag_isoc99)
14646 : : int_const = false;
14647 : : }
14648 : :
14649 : : /* Use the type of the value to be shifted. */
14650 : : result_type = type0;
14651 : : /* Avoid converting op1 to result_type later. */
14652 : : converted = 1;
14653 : : }
14654 : : break;
14655 : :
14656 : 1707933 : case EQ_EXPR:
14657 : 1707933 : case NE_EXPR:
14658 : 1707933 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14659 : : {
14660 : 40915 : tree intt;
14661 : 40915 : if (!vector_types_compatible_elements_p (type0, type1))
14662 : : {
14663 : 4 : error_at (location, "comparing vectors with different "
14664 : : "element types");
14665 : 4 : return error_mark_node;
14666 : : }
14667 : :
14668 : 40911 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14669 : 81822 : TYPE_VECTOR_SUBPARTS (type1)))
14670 : : {
14671 : 1 : error_at (location, "comparing vectors with different "
14672 : : "number of elements");
14673 : 1 : return error_mark_node;
14674 : : }
14675 : :
14676 : : /* It's not precisely specified how the usual arithmetic
14677 : : conversions apply to the vector types. Here, we use
14678 : : the unsigned type if one of the operands is signed and
14679 : : the other one is unsigned. */
14680 : 40910 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14681 : : {
14682 : 4 : if (!TYPE_UNSIGNED (type0))
14683 : 4 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14684 : : else
14685 : 0 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14686 : 4 : warning_at (location, OPT_Wsign_compare, "comparison between "
14687 : : "types %qT and %qT", type0, type1);
14688 : : }
14689 : :
14690 : : /* Always construct signed integer vector type. */
14691 : 122730 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14692 : 81820 : (SCALAR_TYPE_MODE
14693 : : (TREE_TYPE (type0))), 0);
14694 : 40910 : if (!intt)
14695 : : {
14696 : 0 : error_at (location, "could not find an integer type "
14697 : : "of the same size as %qT",
14698 : 0 : TREE_TYPE (type0));
14699 : 0 : return error_mark_node;
14700 : : }
14701 : 40910 : result_type = build_opaque_vector_type (intt,
14702 : 40910 : TYPE_VECTOR_SUBPARTS (type0));
14703 : 40910 : converted = 1;
14704 : 40910 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14705 : 40910 : goto return_build_binary_op;
14706 : : }
14707 : 1667018 : if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
14708 : 245411 : warning_at (location,
14709 : 245411 : OPT_Wfloat_equal,
14710 : : "comparing floating-point with %<==%> or %<!=%> is unsafe");
14711 : : /* Result of comparison is always int,
14712 : : but don't convert the args to int! */
14713 : 1667018 : build_type = integer_type_node;
14714 : 1667018 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == BITINT_TYPE
14715 : 155077 : || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
14716 : 1547238 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14717 : : || code1 == BITINT_TYPE
14718 : : || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
14719 : : short_compare = 1;
14720 : 120043 : else if (code0 == POINTER_TYPE
14721 : 120043 : && (code1 == NULLPTR_TYPE
14722 : 119698 : || null_pointer_constant_p (orig_op1)))
14723 : : {
14724 : 50269 : maybe_warn_for_null_address (location, op0, code);
14725 : 50269 : result_type = type0;
14726 : : }
14727 : 69774 : else if (code1 == POINTER_TYPE
14728 : 69774 : && (code0 == NULLPTR_TYPE
14729 : 69672 : || null_pointer_constant_p (orig_op0)))
14730 : : {
14731 : 284 : maybe_warn_for_null_address (location, op1, code);
14732 : 284 : result_type = type1;
14733 : : }
14734 : 69490 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14735 : : {
14736 : 69400 : tree tt0 = TREE_TYPE (type0);
14737 : 69400 : tree tt1 = TREE_TYPE (type1);
14738 : 69400 : addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
14739 : 69400 : addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
14740 : 69400 : addr_space_t as_common = ADDR_SPACE_GENERIC;
14741 : :
14742 : : /* Anything compares with void *. void * compares with anything.
14743 : : Otherwise, the targets must be compatible
14744 : : and both must be object or both incomplete. */
14745 : 69400 : if (comp_target_types (location, type0, type1))
14746 : 48117 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14747 : 21283 : else if (!addr_space_superset (as0, as1, &as_common))
14748 : : {
14749 : 0 : error_at (location, "comparison of pointers to "
14750 : : "disjoint address spaces");
14751 : 0 : return error_mark_node;
14752 : : }
14753 : 21283 : else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
14754 : : {
14755 : 21086 : if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
14756 : 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14757 : : "comparison of %<void *%> with function pointer");
14758 : : }
14759 : 197 : else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
14760 : : {
14761 : 149 : if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
14762 : 4 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14763 : : "comparison of %<void *%> with function pointer");
14764 : : }
14765 : : else
14766 : : /* Avoid warning about the volatile ObjC EH puts on decls. */
14767 : 48 : if (!objc_ok)
14768 : 48 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14769 : : "comparison of distinct pointer types lacks a cast");
14770 : :
14771 : 48173 : if (result_type == NULL_TREE)
14772 : : {
14773 : 21283 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14774 : 21283 : result_type = c_build_pointer_type
14775 : 21283 : (c_build_qualified_type (void_type_node, qual));
14776 : : }
14777 : : }
14778 : 90 : else if (code0 == POINTER_TYPE
14779 : 26 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14780 : : {
14781 : 26 : result_type = type0;
14782 : 26 : pedwarn (location, 0, "comparison between pointer and integer");
14783 : : }
14784 : 64 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14785 : 21 : && code1 == POINTER_TYPE)
14786 : : {
14787 : 8 : result_type = type1;
14788 : 8 : pedwarn (location, 0, "comparison between pointer and integer");
14789 : : }
14790 : : /* 6.5.9: One of the following shall hold:
14791 : : -- both operands have type nullptr_t; */
14792 : 56 : else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
14793 : : {
14794 : 22 : result_type = nullptr_type_node;
14795 : : /* No need to convert the operands to result_type later. */
14796 : 22 : converted = 1;
14797 : : }
14798 : : /* -- one operand has type nullptr_t and the other is a null pointer
14799 : : constant. We will have to convert the former to the type of the
14800 : : latter, because during gimplification we can't have mismatching
14801 : : comparison operand type. We convert from nullptr_t to the other
14802 : : type, since only nullptr_t can be converted to nullptr_t. Also,
14803 : : even a constant 0 is a null pointer constant, so we may have to
14804 : : create a pointer type from its type. */
14805 : 34 : else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
14806 : 19 : result_type = (INTEGRAL_TYPE_P (type1)
14807 : 19 : ? c_build_pointer_type (type1) : type1);
14808 : 15 : else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
14809 : 11 : result_type = (INTEGRAL_TYPE_P (type0)
14810 : 11 : ? c_build_pointer_type (type0) : type0);
14811 : 4927499 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14812 : 3260456 : || truth_value_p (TREE_CODE (orig_op0)))
14813 : 3326778 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14814 : 3326774 : || truth_value_p (TREE_CODE (orig_op1))))
14815 : 70599 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14816 : : break;
14817 : :
14818 : 974675 : case LE_EXPR:
14819 : 974675 : case GE_EXPR:
14820 : 974675 : case LT_EXPR:
14821 : 974675 : case GT_EXPR:
14822 : 974675 : if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
14823 : : {
14824 : 57795 : tree intt;
14825 : 57795 : if (!vector_types_compatible_elements_p (type0, type1))
14826 : : {
14827 : 5 : error_at (location, "comparing vectors with different "
14828 : : "element types");
14829 : 5 : return error_mark_node;
14830 : : }
14831 : :
14832 : 57790 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
14833 : 115580 : TYPE_VECTOR_SUBPARTS (type1)))
14834 : : {
14835 : 0 : error_at (location, "comparing vectors with different "
14836 : : "number of elements");
14837 : 0 : return error_mark_node;
14838 : : }
14839 : :
14840 : : /* It's not precisely specified how the usual arithmetic
14841 : : conversions apply to the vector types. Here, we use
14842 : : the unsigned type if one of the operands is signed and
14843 : : the other one is unsigned. */
14844 : 57790 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
14845 : : {
14846 : 15 : if (!TYPE_UNSIGNED (type0))
14847 : 8 : op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
14848 : : else
14849 : 7 : op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
14850 : 15 : warning_at (location, OPT_Wsign_compare, "comparison between "
14851 : : "types %qT and %qT", type0, type1);
14852 : : }
14853 : :
14854 : : /* Always construct signed integer vector type. */
14855 : 173370 : intt = c_common_type_for_size (GET_MODE_BITSIZE
14856 : 115580 : (SCALAR_TYPE_MODE
14857 : : (TREE_TYPE (type0))), 0);
14858 : 57790 : if (!intt)
14859 : : {
14860 : 0 : error_at (location, "could not find an integer type "
14861 : : "of the same size as %qT",
14862 : 0 : TREE_TYPE (type0));
14863 : 0 : return error_mark_node;
14864 : : }
14865 : 57790 : result_type = build_opaque_vector_type (intt,
14866 : 57790 : TYPE_VECTOR_SUBPARTS (type0));
14867 : 57790 : converted = 1;
14868 : 57790 : ret = build_vec_cmp (resultcode, result_type, op0, op1);
14869 : 57790 : goto return_build_binary_op;
14870 : : }
14871 : 916880 : build_type = integer_type_node;
14872 : 916880 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
14873 : 63694 : || code0 == BITINT_TYPE || code0 == FIXED_POINT_TYPE)
14874 : 853723 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
14875 : : || code1 == BITINT_TYPE || code1 == FIXED_POINT_TYPE))
14876 : : short_compare = 1;
14877 : 63189 : else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
14878 : : {
14879 : 63122 : addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
14880 : 63122 : addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
14881 : 63122 : addr_space_t as_common;
14882 : :
14883 : 63122 : if (comp_target_types (location, type0, type1))
14884 : : {
14885 : 63009 : result_type = common_pointer_type (type0, type1, NULL_TREE);
14886 : 63009 : if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
14887 : 63009 : != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
14888 : 32 : pedwarn_c99 (location, OPT_Wpedantic,
14889 : : "comparison of complete and incomplete pointers");
14890 : 62977 : else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
14891 : 0 : pedwarn (location, OPT_Wpedantic, "ISO C forbids "
14892 : : "ordered comparisons of pointers to functions");
14893 : 62977 : else if (null_pointer_constant_p (orig_op0)
14894 : 62977 : || null_pointer_constant_p (orig_op1))
14895 : 8 : warning_at (location, OPT_Wextra,
14896 : : "ordered comparison of pointer with null pointer");
14897 : :
14898 : : }
14899 : 113 : else if (!addr_space_superset (as0, as1, &as_common))
14900 : : {
14901 : 0 : error_at (location, "comparison of pointers to "
14902 : : "disjoint address spaces");
14903 : 0 : return error_mark_node;
14904 : : }
14905 : : else
14906 : : {
14907 : 113 : int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
14908 : 113 : result_type = c_build_pointer_type
14909 : 113 : (c_build_qualified_type (void_type_node, qual));
14910 : 113 : pedwarn (location, OPT_Wcompare_distinct_pointer_types,
14911 : : "comparison of distinct pointer types lacks a cast");
14912 : : }
14913 : : }
14914 : 67 : else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
14915 : : {
14916 : 17 : result_type = type0;
14917 : 17 : if (pedantic)
14918 : 11 : pedwarn (location, OPT_Wpedantic,
14919 : : "ordered comparison of pointer with integer zero");
14920 : 6 : else if (extra_warnings)
14921 : 1 : warning_at (location, OPT_Wextra,
14922 : : "ordered comparison of pointer with integer zero");
14923 : : }
14924 : 50 : else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
14925 : : {
14926 : 12 : result_type = type1;
14927 : 12 : if (pedantic)
14928 : 2 : pedwarn (location, OPT_Wpedantic,
14929 : : "ordered comparison of pointer with integer zero");
14930 : 10 : else if (extra_warnings)
14931 : 1 : warning_at (location, OPT_Wextra,
14932 : : "ordered comparison of pointer with integer zero");
14933 : : }
14934 : 38 : else if (code0 == POINTER_TYPE
14935 : 18 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
14936 : : {
14937 : 18 : result_type = type0;
14938 : 18 : pedwarn (location, 0, "comparison between pointer and integer");
14939 : : }
14940 : 20 : else if ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
14941 : 20 : && code1 == POINTER_TYPE)
14942 : : {
14943 : 18 : result_type = type1;
14944 : 18 : pedwarn (location, 0, "comparison between pointer and integer");
14945 : : }
14946 : :
14947 : 916880 : if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
14948 : 63187 : && current_function_decl != NULL_TREE
14949 : 980056 : && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
14950 : : {
14951 : 35 : op0 = save_expr (c_fully_fold (op0, false, NULL));
14952 : 35 : op1 = save_expr (c_fully_fold (op1, false, NULL));
14953 : :
14954 : 35 : tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
14955 : 35 : instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
14956 : : }
14957 : :
14958 : 2750550 : if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
14959 : 1833670 : || truth_value_p (TREE_CODE (orig_op0)))
14960 : 1833685 : ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
14961 : 1833685 : || truth_value_p (TREE_CODE (orig_op1))))
14962 : 509 : maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
14963 : : break;
14964 : :
14965 : 43 : case MIN_EXPR:
14966 : 43 : case MAX_EXPR:
14967 : : /* Used for OpenMP atomics. */
14968 : 43 : gcc_assert (flag_openmp);
14969 : : common = 1;
14970 : : break;
14971 : :
14972 : 0 : default:
14973 : 0 : gcc_unreachable ();
14974 : : }
14975 : :
14976 : 15115552 : if (code0 == ERROR_MARK || code1 == ERROR_MARK)
14977 : 0 : return error_mark_node;
14978 : :
14979 : 15115552 : if (gnu_vector_type_p (type0)
14980 : 1443279 : && gnu_vector_type_p (type1)
14981 : 16558013 : && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
14982 : 1442441 : || !vector_types_compatible_elements_p (type0, type1)))
14983 : : {
14984 : 26 : gcc_rich_location richloc (location);
14985 : 26 : maybe_range_label_for_tree_type_mismatch
14986 : 26 : label_for_op0 (orig_op0, orig_op1),
14987 : 26 : label_for_op1 (orig_op1, orig_op0);
14988 : 26 : richloc.maybe_add_expr (orig_op0, &label_for_op0, highlight_colors::lhs);
14989 : 26 : richloc.maybe_add_expr (orig_op1, &label_for_op1, highlight_colors::rhs);
14990 : 26 : binary_op_error (&richloc, code, type0, type1);
14991 : 26 : return error_mark_node;
14992 : 26 : }
14993 : :
14994 : 15115526 : if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
14995 : 1694346 : || code0 == FIXED_POINT_TYPE || code0 == BITINT_TYPE
14996 : 1626605 : || gnu_vector_type_p (type0))
14997 : 16626520 : && (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
14998 : 1535845 : || code1 == FIXED_POINT_TYPE || code1 == BITINT_TYPE
14999 : 1444514 : || gnu_vector_type_p (type1)))
15000 : : {
15001 : 14930100 : bool first_complex = (code0 == COMPLEX_TYPE);
15002 : 14930100 : bool second_complex = (code1 == COMPLEX_TYPE);
15003 : 14930100 : int none_complex = (!first_complex && !second_complex);
15004 : :
15005 : 14930100 : if (shorten || common || short_compare)
15006 : : {
15007 : 13418321 : result_type = c_common_type (type0, type1);
15008 : 13418321 : do_warn_double_promotion (result_type, type0, type1,
15009 : : "implicit conversion from %qT to %qT "
15010 : : "to match other operand of binary "
15011 : : "expression",
15012 : : location);
15013 : 13418321 : if (result_type == error_mark_node)
15014 : : return error_mark_node;
15015 : : }
15016 : :
15017 : 14930078 : if (first_complex != second_complex
15018 : 82043 : && (code == PLUS_EXPR
15019 : : || code == MINUS_EXPR
15020 : 82043 : || code == MULT_EXPR
15021 : 17525 : || (code == TRUNC_DIV_EXPR && first_complex))
15022 : 65932 : && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
15023 : 14992332 : && flag_signed_zeros)
15024 : : {
15025 : : /* An operation on mixed real/complex operands must be
15026 : : handled specially, but the language-independent code can
15027 : : more easily optimize the plain complex arithmetic if
15028 : : -fno-signed-zeros. */
15029 : 61942 : tree real_type = TREE_TYPE (result_type);
15030 : 61942 : tree real, imag;
15031 : 61942 : if (type0 != orig_type0 || type1 != orig_type1)
15032 : : {
15033 : 94 : gcc_assert (may_need_excess_precision && common);
15034 : 94 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15035 : : }
15036 : 61942 : if (first_complex)
15037 : : {
15038 : 8253 : if (TREE_TYPE (op0) != result_type)
15039 : 1787 : op0 = convert_and_check (location, result_type, op0);
15040 : 8253 : if (TREE_TYPE (op1) != real_type)
15041 : 4601 : op1 = convert_and_check (location, real_type, op1);
15042 : : }
15043 : : else
15044 : : {
15045 : 53689 : if (TREE_TYPE (op0) != real_type)
15046 : 2698 : op0 = convert_and_check (location, real_type, op0);
15047 : 53689 : if (TREE_TYPE (op1) != result_type)
15048 : 1866 : op1 = convert_and_check (location, result_type, op1);
15049 : : }
15050 : 61942 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15051 : 0 : return error_mark_node;
15052 : 61942 : if (first_complex)
15053 : : {
15054 : 8253 : op0 = save_expr (op0);
15055 : 8253 : real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
15056 : : op0, true);
15057 : 8253 : imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
15058 : : op0, true);
15059 : 8253 : switch (code)
15060 : : {
15061 : 3488 : case MULT_EXPR:
15062 : 3488 : case TRUNC_DIV_EXPR:
15063 : 3488 : op1 = save_expr (op1);
15064 : 3488 : imag = build2 (resultcode, real_type, imag, op1);
15065 : : /* Fall through. */
15066 : 8253 : case PLUS_EXPR:
15067 : 8253 : case MINUS_EXPR:
15068 : 8253 : real = build2 (resultcode, real_type, real, op1);
15069 : 8253 : break;
15070 : 0 : default:
15071 : 0 : gcc_unreachable();
15072 : : }
15073 : : }
15074 : : else
15075 : : {
15076 : 53689 : op1 = save_expr (op1);
15077 : 53689 : real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
15078 : : op1, true);
15079 : 53689 : imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
15080 : : op1, true);
15081 : 53689 : switch (code)
15082 : : {
15083 : 2008 : case MULT_EXPR:
15084 : 2008 : op0 = save_expr (op0);
15085 : 2008 : imag = build2 (resultcode, real_type, op0, imag);
15086 : : /* Fall through. */
15087 : 31957 : case PLUS_EXPR:
15088 : 31957 : real = build2 (resultcode, real_type, op0, real);
15089 : 31957 : break;
15090 : 21732 : case MINUS_EXPR:
15091 : 21732 : real = build2 (resultcode, real_type, op0, real);
15092 : 21732 : imag = build1 (NEGATE_EXPR, real_type, imag);
15093 : 21732 : break;
15094 : 0 : default:
15095 : 0 : gcc_unreachable();
15096 : : }
15097 : : }
15098 : 61942 : ret = build2 (COMPLEX_EXPR, result_type, real, imag);
15099 : 61942 : goto return_build_binary_op;
15100 : : }
15101 : :
15102 : : /* For certain operations (which identify themselves by shorten != 0)
15103 : : if both args were extended from the same smaller type,
15104 : : do the arithmetic in that type and then extend.
15105 : :
15106 : : shorten !=0 and !=1 indicates a bitwise operation.
15107 : : For them, this optimization is safe only if
15108 : : both args are zero-extended or both are sign-extended.
15109 : : Otherwise, we might change the result.
15110 : : Eg, (short)-1 | (unsigned short)-1 is (int)-1
15111 : : but calculated in (unsigned short) it would be (unsigned short)-1. */
15112 : :
15113 : 14868136 : if (shorten && none_complex)
15114 : : {
15115 : 1252366 : final_type = result_type;
15116 : 1252366 : result_type = shorten_binary_op (result_type, op0, op1,
15117 : : shorten == -1);
15118 : : }
15119 : :
15120 : : /* Shifts can be shortened if shifting right. */
15121 : :
15122 : 14868136 : if (short_shift)
15123 : : {
15124 : 196692 : int unsigned_arg;
15125 : 196692 : tree arg0 = get_narrower (op0, &unsigned_arg);
15126 : :
15127 : 196692 : final_type = result_type;
15128 : :
15129 : 196692 : if (arg0 == op0 && final_type == TREE_TYPE (op0))
15130 : 138792 : unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
15131 : :
15132 : 196692 : if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
15133 : 2811 : && tree_int_cst_sgn (op1) > 0
15134 : : /* We can shorten only if the shift count is less than the
15135 : : number of bits in the smaller type size. */
15136 : 2811 : && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
15137 : : /* We cannot drop an unsigned shift after sign-extension. */
15138 : 199363 : && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
15139 : : {
15140 : : /* Do an unsigned shift if the operand was zero-extended. */
15141 : 2657 : result_type
15142 : 2657 : = c_common_signed_or_unsigned_type (unsigned_arg,
15143 : 2657 : TREE_TYPE (arg0));
15144 : : /* Convert value-to-be-shifted to that type. */
15145 : 2657 : if (TREE_TYPE (op0) != result_type)
15146 : 2657 : op0 = convert (result_type, op0);
15147 : : converted = 1;
15148 : : }
15149 : : }
15150 : :
15151 : : /* Comparison operations are shortened too but differently.
15152 : : They identify themselves by setting short_compare = 1. */
15153 : :
15154 : 14868136 : if (short_compare)
15155 : : {
15156 : : /* Don't write &op0, etc., because that would prevent op0
15157 : : from being kept in a register.
15158 : : Instead, make copies of the our local variables and
15159 : : pass the copies by reference, then copy them back afterward. */
15160 : 2400664 : tree xop0 = op0, xop1 = op1, xresult_type = result_type;
15161 : 2400664 : enum tree_code xresultcode = resultcode;
15162 : 2400664 : tree val
15163 : 2400664 : = shorten_compare (location, &xop0, &xop1, &xresult_type,
15164 : : &xresultcode);
15165 : :
15166 : 2400664 : if (val != NULL_TREE)
15167 : : {
15168 : 15787 : ret = val;
15169 : 15787 : goto return_build_binary_op;
15170 : : }
15171 : :
15172 : 2384877 : op0 = xop0, op1 = xop1;
15173 : 2384877 : converted = 1;
15174 : 2384877 : resultcode = xresultcode;
15175 : :
15176 : 2384877 : if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
15177 : : {
15178 : 2237095 : bool op0_maybe_const = true;
15179 : 2237095 : bool op1_maybe_const = true;
15180 : 2237095 : tree orig_op0_folded, orig_op1_folded;
15181 : :
15182 : 2237095 : if (in_late_binary_op)
15183 : : {
15184 : : orig_op0_folded = orig_op0;
15185 : : orig_op1_folded = orig_op1;
15186 : : }
15187 : : else
15188 : : {
15189 : : /* Fold for the sake of possible warnings, as in
15190 : : build_conditional_expr. This requires the
15191 : : "original" values to be folded, not just op0 and
15192 : : op1. */
15193 : 2228805 : c_inhibit_evaluation_warnings++;
15194 : 2228805 : op0 = c_fully_fold (op0, require_constant_value,
15195 : : &op0_maybe_const);
15196 : 2228805 : op1 = c_fully_fold (op1, require_constant_value,
15197 : : &op1_maybe_const);
15198 : 2228805 : c_inhibit_evaluation_warnings--;
15199 : 2228805 : orig_op0_folded = c_fully_fold (orig_op0,
15200 : : require_constant_value,
15201 : : NULL);
15202 : 2228805 : orig_op1_folded = c_fully_fold (orig_op1,
15203 : : require_constant_value,
15204 : : NULL);
15205 : : }
15206 : :
15207 : 2237095 : if (warn_sign_compare)
15208 : 373124 : warn_for_sign_compare (location, orig_op0_folded,
15209 : : orig_op1_folded, op0, op1,
15210 : : result_type, resultcode);
15211 : 2237095 : if (!in_late_binary_op && !int_operands)
15212 : : {
15213 : 2052343 : if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
15214 : 2048829 : op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
15215 : 2052343 : if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
15216 : 801617 : op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
15217 : : }
15218 : : }
15219 : : }
15220 : : }
15221 : :
15222 : : /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
15223 : : If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
15224 : : Then the expression will be built.
15225 : : It will be given type FINAL_TYPE if that is nonzero;
15226 : : otherwise, it will be given type RESULT_TYPE. */
15227 : :
15228 : 15037775 : if (!result_type)
15229 : : {
15230 : : /* Favor showing any expression locations that are available. */
15231 : 514 : op_location_t oploc (location, UNKNOWN_LOCATION);
15232 : 514 : binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
15233 : 514 : binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
15234 : 514 : return error_mark_node;
15235 : 514 : }
15236 : :
15237 : 15037261 : if (build_type == NULL_TREE)
15238 : : {
15239 : 12469158 : build_type = result_type;
15240 : 12469158 : if ((type0 != orig_type0 || type1 != orig_type1)
15241 : 797 : && !boolean_op)
15242 : : {
15243 : 797 : gcc_assert (may_need_excess_precision && common);
15244 : 797 : semantic_result_type = c_common_type (orig_type0, orig_type1);
15245 : : }
15246 : : }
15247 : :
15248 : 15037261 : if (!converted)
15249 : : {
15250 : 11137696 : op0 = ep_convert_and_check (location, result_type, op0,
15251 : : semantic_result_type);
15252 : 11137696 : op1 = ep_convert_and_check (location, result_type, op1,
15253 : : semantic_result_type);
15254 : :
15255 : : /* This can happen if one operand has a vector type, and the other
15256 : : has a different type. */
15257 : 11137696 : if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
15258 : 3 : return error_mark_node;
15259 : : }
15260 : :
15261 : 15037258 : if (sanitize_flags_p ((SANITIZE_SHIFT
15262 : : | SANITIZE_DIVIDE
15263 : : | SANITIZE_FLOAT_DIVIDE
15264 : : | SANITIZE_SI_OVERFLOW))
15265 : 23306 : && current_function_decl != NULL_TREE
15266 : 19843 : && (doing_div_or_mod || doing_shift)
15267 : 15040183 : && !require_constant_value)
15268 : : {
15269 : : /* OP0 and/or OP1 might have side-effects. */
15270 : 2869 : op0 = save_expr (op0);
15271 : 2869 : op1 = save_expr (op1);
15272 : 2869 : op0 = c_fully_fold (op0, false, NULL);
15273 : 2869 : op1 = c_fully_fold (op1, false, NULL);
15274 : 2869 : if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
15275 : : | SANITIZE_FLOAT_DIVIDE
15276 : : | SANITIZE_SI_OVERFLOW))))
15277 : 912 : instrument_expr = ubsan_instrument_division (location, op0, op1);
15278 : 1957 : else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
15279 : 1777 : instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
15280 : : }
15281 : :
15282 : : /* Treat expressions in initializers specially as they can't trap. */
15283 : 15037258 : if (int_const_or_overflow)
15284 : 7741520 : ret = (require_constant_value
15285 : 7741520 : ? fold_build2_initializer_loc (location, resultcode, build_type,
15286 : : op0, op1)
15287 : 7695399 : : fold_build2_loc (location, resultcode, build_type, op0, op1));
15288 : : else
15289 : 7295738 : ret = build2 (resultcode, build_type, op0, op1);
15290 : 15037258 : if (final_type != NULL_TREE)
15291 : 1449058 : ret = convert (final_type, ret);
15292 : :
15293 : 13588200 : return_build_binary_op:
15294 : 16454943 : gcc_assert (ret != error_mark_node);
15295 : 16454943 : if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
15296 : 1740 : ret = (int_operands
15297 : 1740 : ? note_integer_operands (ret)
15298 : 422 : : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
15299 : 16453203 : else if (TREE_CODE (ret) != INTEGER_CST && int_operands
15300 : 60300 : && !in_late_binary_op)
15301 : 60300 : ret = note_integer_operands (ret);
15302 : 16454943 : protected_set_expr_location (ret, location);
15303 : :
15304 : 16454943 : if (instrument_expr != NULL)
15305 : 1420 : ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
15306 : : instrument_expr, ret);
15307 : :
15308 : 16454943 : if (semantic_result_type)
15309 : 891 : ret = build1_loc (location, EXCESS_PRECISION_EXPR,
15310 : : semantic_result_type, ret);
15311 : :
15312 : : return ret;
15313 : : }
15314 : :
15315 : :
15316 : : /* Convert EXPR to be a truth-value (type TYPE), validating its type for this
15317 : : purpose. LOCATION is the source location for the expression. */
15318 : :
15319 : : tree
15320 : 4059592 : c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type)
15321 : : {
15322 : 4059592 : bool int_const, int_operands;
15323 : :
15324 : 4059592 : switch (TREE_CODE (TREE_TYPE (expr)))
15325 : : {
15326 : 72 : case ARRAY_TYPE:
15327 : 72 : error_at (location, "used array that cannot be converted to pointer where scalar is required");
15328 : 72 : return error_mark_node;
15329 : :
15330 : 86 : case RECORD_TYPE:
15331 : 86 : error_at (location, "used struct type value where scalar is required");
15332 : 86 : return error_mark_node;
15333 : :
15334 : 83 : case UNION_TYPE:
15335 : 83 : error_at (location, "used union type value where scalar is required");
15336 : 83 : return error_mark_node;
15337 : :
15338 : 22 : case VOID_TYPE:
15339 : 22 : error_at (location, "void value not ignored as it ought to be");
15340 : 22 : return error_mark_node;
15341 : :
15342 : 29739 : case POINTER_TYPE:
15343 : 29739 : if (reject_gcc_builtin (expr))
15344 : 3 : return error_mark_node;
15345 : : break;
15346 : :
15347 : 0 : case FUNCTION_TYPE:
15348 : 0 : gcc_unreachable ();
15349 : :
15350 : 8 : case VECTOR_TYPE:
15351 : 8 : error_at (location, "used vector type where scalar is required");
15352 : 8 : return error_mark_node;
15353 : :
15354 : : default:
15355 : : break;
15356 : : }
15357 : :
15358 : : /* Conversion of a floating constant to boolean goes through here
15359 : : and yields an integer constant expression. Otherwise, the result
15360 : : is only an integer constant expression if the argument is. */
15361 : 895314 : int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr))
15362 : 4059394 : || ((TREE_CODE (expr) == REAL_CST
15363 : 3163706 : || TREE_CODE (expr) == COMPLEX_CST)
15364 : 374 : && (TREE_CODE (type) == BOOLEAN_TYPE
15365 : 15 : || (TREE_CODE (type) == ENUMERAL_TYPE
15366 : 5 : && ENUM_UNDERLYING_TYPE (type) != NULL_TREE
15367 : 5 : && (TREE_CODE (ENUM_UNDERLYING_TYPE (type))
15368 : : == BOOLEAN_TYPE)))));
15369 : 4059318 : int_operands = EXPR_INT_CONST_OPERANDS (expr);
15370 : 895474 : if (int_operands && TREE_CODE (expr) != INTEGER_CST)
15371 : : {
15372 : 319 : expr = remove_c_maybe_const_expr (expr);
15373 : 319 : expr = build2 (NE_EXPR, type, expr,
15374 : 319 : convert (TREE_TYPE (expr), integer_zero_node));
15375 : 319 : expr = note_integer_operands (expr);
15376 : : }
15377 : : else
15378 : : {
15379 : : /* ??? Should we also give an error for vectors rather than leaving
15380 : : those to give errors later? */
15381 : 4058999 : expr = c_common_truthvalue_conversion (location, expr);
15382 : 4058999 : expr = fold_convert_loc (location, type, expr);
15383 : : }
15384 : :
15385 : 4059318 : if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
15386 : : {
15387 : 76 : if (TREE_OVERFLOW (expr))
15388 : : return expr;
15389 : : else
15390 : 76 : return note_integer_operands (expr);
15391 : : }
15392 : 4059242 : if (TREE_CODE (expr) == INTEGER_CST && !int_const)
15393 : 754 : return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
15394 : : return expr;
15395 : : }
15396 : :
15397 : :
15398 : : /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
15399 : : required. */
15400 : :
15401 : : tree
15402 : 116327415 : c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
15403 : : {
15404 : 116327415 : if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
15405 : : {
15406 : 663 : tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
15407 : : /* Executing a compound literal inside a function reinitializes
15408 : : it. */
15409 : 663 : if (!TREE_STATIC (decl))
15410 : 399 : *se = true;
15411 : 663 : return decl;
15412 : : }
15413 : : else
15414 : : return expr;
15415 : : }
15416 : :
15417 : : /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
15418 : : statement. LOC is the location of the construct. */
15419 : :
15420 : : tree
15421 : 2154 : c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
15422 : : tree clauses)
15423 : : {
15424 : 2154 : body = c_end_compound_stmt (loc, body, true);
15425 : :
15426 : 2154 : tree stmt = make_node (code);
15427 : 2154 : TREE_TYPE (stmt) = void_type_node;
15428 : 2154 : OMP_BODY (stmt) = body;
15429 : 2154 : OMP_CLAUSES (stmt) = clauses;
15430 : 2154 : SET_EXPR_LOCATION (stmt, loc);
15431 : :
15432 : 2154 : return add_stmt (stmt);
15433 : : }
15434 : :
15435 : : /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
15436 : : statement. LOC is the location of the OACC_DATA. */
15437 : :
15438 : : tree
15439 : 497 : c_finish_oacc_data (location_t loc, tree clauses, tree block)
15440 : : {
15441 : 497 : tree stmt;
15442 : :
15443 : 497 : block = c_end_compound_stmt (loc, block, true);
15444 : :
15445 : 497 : stmt = make_node (OACC_DATA);
15446 : 497 : TREE_TYPE (stmt) = void_type_node;
15447 : 497 : OACC_DATA_CLAUSES (stmt) = clauses;
15448 : 497 : OACC_DATA_BODY (stmt) = block;
15449 : 497 : SET_EXPR_LOCATION (stmt, loc);
15450 : :
15451 : 497 : return add_stmt (stmt);
15452 : : }
15453 : :
15454 : : /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
15455 : : statement. LOC is the location of the OACC_HOST_DATA. */
15456 : :
15457 : : tree
15458 : 21 : c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
15459 : : {
15460 : 21 : tree stmt;
15461 : :
15462 : 21 : block = c_end_compound_stmt (loc, block, true);
15463 : :
15464 : 21 : stmt = make_node (OACC_HOST_DATA);
15465 : 21 : TREE_TYPE (stmt) = void_type_node;
15466 : 21 : OACC_HOST_DATA_CLAUSES (stmt) = clauses;
15467 : 21 : OACC_HOST_DATA_BODY (stmt) = block;
15468 : 21 : SET_EXPR_LOCATION (stmt, loc);
15469 : :
15470 : 21 : return add_stmt (stmt);
15471 : : }
15472 : :
15473 : : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15474 : :
15475 : : tree
15476 : 11990 : c_begin_omp_parallel (void)
15477 : : {
15478 : 11990 : tree block;
15479 : :
15480 : 11990 : keep_next_level ();
15481 : 11990 : block = c_begin_compound_stmt (true);
15482 : :
15483 : 11990 : return block;
15484 : : }
15485 : :
15486 : : /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
15487 : : statement. LOC is the location of the OMP_PARALLEL. */
15488 : :
15489 : : tree
15490 : 5665 : c_finish_omp_parallel (location_t loc, tree clauses, tree block)
15491 : : {
15492 : 5665 : tree stmt;
15493 : :
15494 : 5665 : block = c_end_compound_stmt (loc, block, true);
15495 : :
15496 : 5665 : stmt = make_node (OMP_PARALLEL);
15497 : 5665 : TREE_TYPE (stmt) = void_type_node;
15498 : 5665 : OMP_PARALLEL_CLAUSES (stmt) = clauses;
15499 : 5665 : OMP_PARALLEL_BODY (stmt) = block;
15500 : 5665 : SET_EXPR_LOCATION (stmt, loc);
15501 : :
15502 : 5665 : return add_stmt (stmt);
15503 : : }
15504 : :
15505 : : /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
15506 : :
15507 : : tree
15508 : 896 : c_begin_omp_task (void)
15509 : : {
15510 : 896 : tree block;
15511 : :
15512 : 896 : keep_next_level ();
15513 : 896 : block = c_begin_compound_stmt (true);
15514 : :
15515 : 896 : return block;
15516 : : }
15517 : :
15518 : : /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
15519 : : statement. LOC is the location of the #pragma. */
15520 : :
15521 : : tree
15522 : 896 : c_finish_omp_task (location_t loc, tree clauses, tree block)
15523 : : {
15524 : 896 : tree stmt;
15525 : :
15526 : 896 : block = c_end_compound_stmt (loc, block, true);
15527 : :
15528 : 896 : stmt = make_node (OMP_TASK);
15529 : 896 : TREE_TYPE (stmt) = void_type_node;
15530 : 896 : OMP_TASK_CLAUSES (stmt) = clauses;
15531 : 896 : OMP_TASK_BODY (stmt) = block;
15532 : 896 : SET_EXPR_LOCATION (stmt, loc);
15533 : :
15534 : 896 : return add_stmt (stmt);
15535 : : }
15536 : :
15537 : : /* Generate GOMP_cancel call for #pragma omp cancel. */
15538 : :
15539 : : void
15540 : 213 : c_finish_omp_cancel (location_t loc, tree clauses)
15541 : : {
15542 : 213 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
15543 : 213 : int mask = 0;
15544 : 213 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15545 : : mask = 1;
15546 : 150 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15547 : : mask = 2;
15548 : 101 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15549 : : mask = 4;
15550 : 58 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15551 : : mask = 8;
15552 : : else
15553 : : {
15554 : 0 : error_at (loc, "%<#pragma omp cancel%> must specify one of "
15555 : : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15556 : : "clauses");
15557 : 0 : return;
15558 : : }
15559 : 213 : tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
15560 : 213 : if (ifc != NULL_TREE)
15561 : : {
15562 : 31 : if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
15563 : 31 : && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
15564 : 2 : error_at (OMP_CLAUSE_LOCATION (ifc),
15565 : : "expected %<cancel%> %<if%> clause modifier");
15566 : : else
15567 : : {
15568 : 29 : tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
15569 : 29 : if (ifc2 != NULL_TREE)
15570 : : {
15571 : 1 : gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
15572 : : && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
15573 : : && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
15574 : 1 : error_at (OMP_CLAUSE_LOCATION (ifc2),
15575 : : "expected %<cancel%> %<if%> clause modifier");
15576 : : }
15577 : : }
15578 : :
15579 : 31 : tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
15580 : 62 : ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
15581 : 31 : boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
15582 : : build_zero_cst (type));
15583 : : }
15584 : : else
15585 : 182 : ifc = boolean_true_node;
15586 : 213 : tree stmt = build_call_expr_loc (loc, fn, 2,
15587 : 213 : build_int_cst (integer_type_node, mask),
15588 : : ifc);
15589 : 213 : add_stmt (stmt);
15590 : : }
15591 : :
15592 : : /* Generate GOMP_cancellation_point call for
15593 : : #pragma omp cancellation point. */
15594 : :
15595 : : void
15596 : 167 : c_finish_omp_cancellation_point (location_t loc, tree clauses)
15597 : : {
15598 : 167 : tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
15599 : 167 : int mask = 0;
15600 : 167 : if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
15601 : : mask = 1;
15602 : 123 : else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
15603 : : mask = 2;
15604 : 88 : else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
15605 : : mask = 4;
15606 : 53 : else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
15607 : : mask = 8;
15608 : : else
15609 : : {
15610 : 1 : error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
15611 : : "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
15612 : : "clauses");
15613 : 1 : return;
15614 : : }
15615 : 166 : tree stmt = build_call_expr_loc (loc, fn, 1,
15616 : 166 : build_int_cst (integer_type_node, mask));
15617 : 166 : add_stmt (stmt);
15618 : : }
15619 : :
15620 : : /* Helper function for handle_omp_array_sections. Called recursively
15621 : : to handle multiple array-section-subscripts. C is the clause,
15622 : : T current expression (initially OMP_CLAUSE_DECL), which is either
15623 : : a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
15624 : : expression if specified, TREE_VALUE length expression if specified,
15625 : : TREE_CHAIN is what it has been specified after, or some decl.
15626 : : TYPES vector is populated with array section types, MAYBE_ZERO_LEN
15627 : : set to true if any of the array-section-subscript could have length
15628 : : of zero (explicit or implicit), FIRST_NON_ONE is the index of the
15629 : : first array-section-subscript which is known not to have length
15630 : : of one. Given say:
15631 : : map(a[:b][2:1][:c][:2][:d][e:f][2:5])
15632 : : FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
15633 : : all are or may have length of 1, array-section-subscript [:2] is the
15634 : : first one known not to have length 1. For array-section-subscript
15635 : : <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
15636 : : 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
15637 : : can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
15638 : : case though, as some lengths could be zero. */
15639 : :
15640 : : static tree
15641 : 6378 : handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
15642 : : bool &maybe_zero_len, unsigned int &first_non_one,
15643 : : enum c_omp_region_type ort)
15644 : : {
15645 : 6378 : tree ret, low_bound, length, type;
15646 : 6378 : bool openacc = (ort & C_ORT_ACC) != 0;
15647 : 6378 : if (TREE_CODE (t) != OMP_ARRAY_SECTION)
15648 : : {
15649 : 2970 : if (error_operand_p (t))
15650 : 2 : return error_mark_node;
15651 : 2968 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
15652 : 2968 : ret = t;
15653 : 2968 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15654 : 2869 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15655 : 5523 : && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15656 : : {
15657 : 6 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
15658 : 6 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15659 : 6 : return error_mark_node;
15660 : : }
15661 : 2962 : if (!ai.check_clause (c))
15662 : 0 : return error_mark_node;
15663 : 2962 : else if (ai.component_access_p ()
15664 : 3263 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15665 : 15 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
15666 : 11 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
15667 : 301 : t = ai.get_root_term (true);
15668 : : else
15669 : 2661 : t = ai.unconverted_ref_origin ();
15670 : 2962 : if (t == error_mark_node)
15671 : : return error_mark_node;
15672 : 2962 : if (!VAR_P (t)
15673 : 740 : && (ort == C_ORT_ACC || !EXPR_P (t))
15674 : 737 : && TREE_CODE (t) != PARM_DECL)
15675 : : {
15676 : 5 : if (DECL_P (t))
15677 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
15678 : : "%qD is not a variable in %qs clause", t,
15679 : 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15680 : : else
15681 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
15682 : : "%qE is not a variable in %qs clause", t,
15683 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15684 : 5 : return error_mark_node;
15685 : : }
15686 : 2957 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15687 : 2859 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15688 : 5503 : && TYPE_ATOMIC (TREE_TYPE (t)))
15689 : : {
15690 : 0 : error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
15691 : 0 : t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15692 : 0 : return error_mark_node;
15693 : : }
15694 : 2957 : else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15695 : 2859 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15696 : 2546 : && VAR_P (t)
15697 : 4956 : && DECL_THREAD_LOCAL_P (t))
15698 : : {
15699 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
15700 : : "%qD is threadprivate variable in %qs clause", t,
15701 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15702 : 3 : return error_mark_node;
15703 : : }
15704 : 2954 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15705 : 2856 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
15706 : 411 : && TYPE_ATOMIC (TREE_TYPE (t))
15707 : 2956 : && POINTER_TYPE_P (TREE_TYPE (t)))
15708 : : {
15709 : : /* If the array section is pointer based and the pointer
15710 : : itself is _Atomic qualified, we need to atomically load
15711 : : the pointer. */
15712 : 2 : ret = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
15713 : : ret, false, false);
15714 : : }
15715 : 2954 : return ret;
15716 : 2968 : }
15717 : :
15718 : 3408 : ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
15719 : : maybe_zero_len, first_non_one, ort);
15720 : 3408 : if (ret == error_mark_node || ret == NULL_TREE)
15721 : : return ret;
15722 : :
15723 : 3343 : type = TREE_TYPE (ret);
15724 : 3343 : low_bound = TREE_OPERAND (t, 1);
15725 : 3343 : length = TREE_OPERAND (t, 2);
15726 : :
15727 : 3343 : if (low_bound == error_mark_node || length == error_mark_node)
15728 : : return error_mark_node;
15729 : :
15730 : 3343 : if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
15731 : : {
15732 : 13 : error_at (OMP_CLAUSE_LOCATION (c),
15733 : : "low bound %qE of array section does not have integral type",
15734 : : low_bound);
15735 : 13 : return error_mark_node;
15736 : : }
15737 : 3330 : if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
15738 : : {
15739 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
15740 : : "length %qE of array section does not have integral type",
15741 : : length);
15742 : 12 : return error_mark_node;
15743 : : }
15744 : 3318 : if (low_bound
15745 : 2735 : && TREE_CODE (low_bound) == INTEGER_CST
15746 : 5764 : && TYPE_PRECISION (TREE_TYPE (low_bound))
15747 : 2446 : > TYPE_PRECISION (sizetype))
15748 : 0 : low_bound = fold_convert (sizetype, low_bound);
15749 : 3318 : if (length
15750 : 3130 : && TREE_CODE (length) == INTEGER_CST
15751 : 5504 : && TYPE_PRECISION (TREE_TYPE (length))
15752 : 2186 : > TYPE_PRECISION (sizetype))
15753 : 0 : length = fold_convert (sizetype, length);
15754 : 3318 : if (low_bound == NULL_TREE)
15755 : 583 : low_bound = integer_zero_node;
15756 : 3318 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15757 : 3318 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15758 : 1928 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15759 : : {
15760 : 10 : if (length != integer_one_node)
15761 : : {
15762 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
15763 : : "expected single pointer in %qs clause",
15764 : : user_omp_clause_code_name (c, openacc));
15765 : 6 : return error_mark_node;
15766 : : }
15767 : : }
15768 : 3312 : if (length != NULL_TREE)
15769 : : {
15770 : 3128 : if (!integer_nonzerop (length))
15771 : : {
15772 : 972 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15773 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15774 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15775 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15776 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15777 : : {
15778 : 180 : if (integer_zerop (length))
15779 : : {
15780 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
15781 : : "zero length array section in %qs clause",
15782 : 12 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15783 : 12 : return error_mark_node;
15784 : : }
15785 : : }
15786 : : else
15787 : 792 : maybe_zero_len = true;
15788 : : }
15789 : 3116 : if (first_non_one == types.length ()
15790 : 3116 : && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
15791 : 1596 : first_non_one++;
15792 : : }
15793 : 3300 : if (TREE_CODE (type) == ARRAY_TYPE)
15794 : : {
15795 : 1490 : if (length == NULL_TREE
15796 : 1490 : && (TYPE_DOMAIN (type) == NULL_TREE
15797 : 168 : || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
15798 : : {
15799 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
15800 : : "for unknown bound array type length expression must "
15801 : : "be specified");
15802 : 8 : return error_mark_node;
15803 : : }
15804 : 1482 : if (TREE_CODE (low_bound) == INTEGER_CST
15805 : 1482 : && tree_int_cst_sgn (low_bound) == -1)
15806 : : {
15807 : 35 : error_at (OMP_CLAUSE_LOCATION (c),
15808 : : "negative low bound in array section in %qs clause",
15809 : 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15810 : 35 : return error_mark_node;
15811 : : }
15812 : 1447 : if (length != NULL_TREE
15813 : 1291 : && TREE_CODE (length) == INTEGER_CST
15814 : 2298 : && tree_int_cst_sgn (length) == -1)
15815 : : {
15816 : 35 : error_at (OMP_CLAUSE_LOCATION (c),
15817 : : "negative length in array section in %qs clause",
15818 : 35 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15819 : 35 : return error_mark_node;
15820 : : }
15821 : 1412 : if (TYPE_DOMAIN (type)
15822 : 1401 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
15823 : 2813 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
15824 : : == INTEGER_CST)
15825 : : {
15826 : 1055 : tree size
15827 : 1055 : = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
15828 : 1055 : size = size_binop (PLUS_EXPR, size, size_one_node);
15829 : 1055 : if (TREE_CODE (low_bound) == INTEGER_CST)
15830 : : {
15831 : 868 : if (tree_int_cst_lt (size, low_bound))
15832 : : {
15833 : 10 : error_at (OMP_CLAUSE_LOCATION (c),
15834 : : "low bound %qE above array section size "
15835 : : "in %qs clause", low_bound,
15836 : 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15837 : 10 : return error_mark_node;
15838 : : }
15839 : 858 : if (tree_int_cst_equal (size, low_bound))
15840 : : {
15841 : 5 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
15842 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15843 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15844 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
15845 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
15846 : : {
15847 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
15848 : : "zero length array section in %qs clause",
15849 : 5 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15850 : 5 : return error_mark_node;
15851 : : }
15852 : 0 : maybe_zero_len = true;
15853 : : }
15854 : 853 : else if (length == NULL_TREE
15855 : 256 : && first_non_one == types.length ()
15856 : 908 : && tree_int_cst_equal
15857 : 55 : (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
15858 : : low_bound))
15859 : 30 : first_non_one++;
15860 : : }
15861 : 187 : else if (length == NULL_TREE)
15862 : : {
15863 : 3 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15864 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15865 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
15866 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
15867 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
15868 : 0 : maybe_zero_len = true;
15869 : 6 : if (first_non_one == types.length ())
15870 : 1 : first_non_one++;
15871 : : }
15872 : 1040 : if (length && TREE_CODE (length) == INTEGER_CST)
15873 : : {
15874 : 793 : if (tree_int_cst_lt (size, length))
15875 : : {
15876 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
15877 : : "length %qE above array section size "
15878 : : "in %qs clause", length,
15879 : 11 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15880 : 11 : return error_mark_node;
15881 : : }
15882 : 782 : if (TREE_CODE (low_bound) == INTEGER_CST)
15883 : : {
15884 : 682 : tree lbpluslen
15885 : 682 : = size_binop (PLUS_EXPR,
15886 : : fold_convert (sizetype, low_bound),
15887 : : fold_convert (sizetype, length));
15888 : 682 : if (TREE_CODE (lbpluslen) == INTEGER_CST
15889 : 682 : && tree_int_cst_lt (size, lbpluslen))
15890 : : {
15891 : 10 : error_at (OMP_CLAUSE_LOCATION (c),
15892 : : "high bound %qE above array section size "
15893 : : "in %qs clause", lbpluslen,
15894 : 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15895 : 10 : return error_mark_node;
15896 : : }
15897 : : }
15898 : : }
15899 : : }
15900 : 357 : else if (length == NULL_TREE)
15901 : : {
15902 : 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15903 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15904 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
15905 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
15906 : : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
15907 : 0 : maybe_zero_len = true;
15908 : 20 : if (first_non_one == types.length ())
15909 : 10 : first_non_one++;
15910 : : }
15911 : :
15912 : : /* For [lb:] we will need to evaluate lb more than once. */
15913 : 923 : if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
15914 : : {
15915 : 127 : tree lb = save_expr (low_bound);
15916 : 127 : if (lb != low_bound)
15917 : : {
15918 : 2 : TREE_OPERAND (t, 1) = lb;
15919 : 2 : low_bound = lb;
15920 : : }
15921 : : }
15922 : : }
15923 : 1810 : else if (TREE_CODE (type) == POINTER_TYPE)
15924 : : {
15925 : 1799 : if (length == NULL_TREE)
15926 : : {
15927 : 10 : if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
15928 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
15929 : : "for array function parameter length expression "
15930 : : "must be specified");
15931 : : else
15932 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
15933 : : "for pointer type length expression must be specified");
15934 : 10 : return error_mark_node;
15935 : : }
15936 : 1789 : if (length != NULL_TREE
15937 : 1789 : && TREE_CODE (length) == INTEGER_CST
15938 : 1285 : && tree_int_cst_sgn (length) == -1)
15939 : : {
15940 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
15941 : : "negative length in array section in %qs clause",
15942 : 20 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15943 : 20 : return error_mark_node;
15944 : : }
15945 : : /* If there is a pointer type anywhere but in the very first
15946 : : array-section-subscript, the array section could be non-contiguous. */
15947 : 1769 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
15948 : 1608 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
15949 : 3348 : && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
15950 : : {
15951 : : /* If any prior dimension has a non-one length, then deem this
15952 : : array section as non-contiguous. */
15953 : 42 : for (tree d = TREE_OPERAND (t, 0);
15954 : 82 : TREE_CODE (d) == OMP_ARRAY_SECTION;
15955 : 40 : d = TREE_OPERAND (d, 0))
15956 : : {
15957 : 44 : tree d_length = TREE_OPERAND (d, 2);
15958 : 44 : if (d_length == NULL_TREE || !integer_onep (d_length))
15959 : : {
15960 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
15961 : : "array section is not contiguous in %qs clause",
15962 : 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15963 : 4 : return error_mark_node;
15964 : : }
15965 : : }
15966 : : }
15967 : : }
15968 : : else
15969 : : {
15970 : 11 : error_at (OMP_CLAUSE_LOCATION (c),
15971 : : "%qE does not have pointer or array type", ret);
15972 : 11 : return error_mark_node;
15973 : : }
15974 : 3141 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
15975 : 2825 : types.safe_push (TREE_TYPE (ret));
15976 : : /* We will need to evaluate lb more than once. */
15977 : 3141 : tree lb = save_expr (low_bound);
15978 : 3141 : if (lb != low_bound)
15979 : : {
15980 : 273 : TREE_OPERAND (t, 1) = lb;
15981 : 273 : low_bound = lb;
15982 : : }
15983 : 3141 : ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
15984 : 3141 : return ret;
15985 : : }
15986 : :
15987 : : /* Handle array sections for clause C. */
15988 : :
15989 : : static bool
15990 : 2970 : handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
15991 : : {
15992 : 2970 : bool maybe_zero_len = false;
15993 : 2970 : unsigned int first_non_one = 0;
15994 : 2970 : auto_vec<tree, 10> types;
15995 : 2970 : tree *tp = &OMP_CLAUSE_DECL (c);
15996 : 2970 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15997 : 2656 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
15998 : 3069 : && OMP_ITERATOR_DECL_P (*tp))
15999 : 66 : tp = &TREE_VALUE (*tp);
16000 : 2970 : tree first = handle_omp_array_sections_1 (c, *tp, types,
16001 : : maybe_zero_len, first_non_one,
16002 : : ort);
16003 : 2970 : if (first == error_mark_node)
16004 : : return true;
16005 : 2752 : if (first == NULL_TREE)
16006 : : return false;
16007 : 2752 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
16008 : 2752 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
16009 : : {
16010 : 336 : tree t = *tp;
16011 : 336 : tree tem = NULL_TREE;
16012 : : /* Need to evaluate side effects in the length expressions
16013 : : if any. */
16014 : 336 : while (TREE_CODE (t) == TREE_LIST)
16015 : : {
16016 : 0 : if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
16017 : : {
16018 : 0 : if (tem == NULL_TREE)
16019 : : tem = TREE_VALUE (t);
16020 : : else
16021 : 0 : tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
16022 : 0 : TREE_VALUE (t), tem);
16023 : : }
16024 : 0 : t = TREE_CHAIN (t);
16025 : : }
16026 : 336 : if (tem)
16027 : 0 : first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
16028 : 336 : first = c_fully_fold (first, false, NULL, true);
16029 : 336 : *tp = first;
16030 : : }
16031 : : else
16032 : : {
16033 : 2416 : unsigned int num = types.length (), i;
16034 : 2416 : tree t, side_effects = NULL_TREE, size = NULL_TREE;
16035 : 2416 : tree condition = NULL_TREE;
16036 : :
16037 : 2416 : if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
16038 : 3 : maybe_zero_len = true;
16039 : :
16040 : 5032 : for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
16041 : 2616 : t = TREE_OPERAND (t, 0))
16042 : : {
16043 : 2631 : tree low_bound = TREE_OPERAND (t, 1);
16044 : 2631 : tree length = TREE_OPERAND (t, 2);
16045 : :
16046 : 2631 : i--;
16047 : 2631 : if (low_bound
16048 : 2168 : && TREE_CODE (low_bound) == INTEGER_CST
16049 : 4613 : && TYPE_PRECISION (TREE_TYPE (low_bound))
16050 : 1982 : > TYPE_PRECISION (sizetype))
16051 : 0 : low_bound = fold_convert (sizetype, low_bound);
16052 : 2631 : if (length
16053 : 2539 : && TREE_CODE (length) == INTEGER_CST
16054 : 4244 : && TYPE_PRECISION (TREE_TYPE (length))
16055 : 1613 : > TYPE_PRECISION (sizetype))
16056 : 0 : length = fold_convert (sizetype, length);
16057 : 2631 : if (low_bound == NULL_TREE)
16058 : 463 : low_bound = integer_zero_node;
16059 : 2631 : if (!maybe_zero_len && i > first_non_one)
16060 : : {
16061 : 109 : if (integer_nonzerop (low_bound))
16062 : 6 : goto do_warn_noncontiguous;
16063 : 103 : if (length != NULL_TREE
16064 : 50 : && TREE_CODE (length) == INTEGER_CST
16065 : 50 : && TYPE_DOMAIN (types[i])
16066 : 50 : && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
16067 : 153 : && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
16068 : : == INTEGER_CST)
16069 : : {
16070 : 50 : tree size;
16071 : 50 : size = size_binop (PLUS_EXPR,
16072 : : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16073 : : size_one_node);
16074 : 50 : if (!tree_int_cst_equal (length, size))
16075 : : {
16076 : 7 : do_warn_noncontiguous:
16077 : 26 : error_at (OMP_CLAUSE_LOCATION (c),
16078 : : "array section is not contiguous in %qs "
16079 : : "clause",
16080 : 13 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16081 : 2416 : return true;
16082 : : }
16083 : : }
16084 : 96 : if (length != NULL_TREE
16085 : 96 : && TREE_SIDE_EFFECTS (length))
16086 : : {
16087 : 0 : if (side_effects == NULL_TREE)
16088 : : side_effects = length;
16089 : : else
16090 : 0 : side_effects = build2 (COMPOUND_EXPR,
16091 : 0 : TREE_TYPE (side_effects),
16092 : : length, side_effects);
16093 : : }
16094 : : }
16095 : : else
16096 : : {
16097 : 2522 : tree l;
16098 : :
16099 : 2522 : if (i > first_non_one
16100 : 2522 : && ((length && integer_nonzerop (length))
16101 : 0 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16102 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16103 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
16104 : 0 : continue;
16105 : 2522 : if (length)
16106 : 2489 : l = fold_convert (sizetype, length);
16107 : : else
16108 : : {
16109 : 33 : l = size_binop (PLUS_EXPR,
16110 : : TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
16111 : : size_one_node);
16112 : 33 : l = size_binop (MINUS_EXPR, l,
16113 : : fold_convert (sizetype, low_bound));
16114 : : }
16115 : 2522 : if (i > first_non_one)
16116 : : {
16117 : 0 : l = fold_build2 (NE_EXPR, boolean_type_node, l,
16118 : : size_zero_node);
16119 : 0 : if (condition == NULL_TREE)
16120 : : condition = l;
16121 : : else
16122 : 0 : condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
16123 : : l, condition);
16124 : : }
16125 : 2522 : else if (size == NULL_TREE)
16126 : : {
16127 : 2403 : size = size_in_bytes (TREE_TYPE (types[i]));
16128 : 2403 : tree eltype = TREE_TYPE (types[num - 1]);
16129 : 2423 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16130 : 20 : eltype = TREE_TYPE (eltype);
16131 : 2403 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16132 : 2168 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16133 : 4311 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16134 : : {
16135 : 539 : if (integer_zerop (size)
16136 : 1076 : || integer_zerop (size_in_bytes (eltype)))
16137 : : {
16138 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
16139 : : "zero length array section in %qs clause",
16140 : 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16141 : 2 : return error_mark_node;
16142 : : }
16143 : 537 : size = size_binop (EXACT_DIV_EXPR, size,
16144 : : size_in_bytes (eltype));
16145 : : }
16146 : 2401 : size = size_binop (MULT_EXPR, size, l);
16147 : 2401 : if (condition)
16148 : 0 : size = fold_build3 (COND_EXPR, sizetype, condition,
16149 : : size, size_zero_node);
16150 : : }
16151 : : else
16152 : 119 : size = size_binop (MULT_EXPR, size, l);
16153 : : }
16154 : : }
16155 : 2401 : if (side_effects)
16156 : 0 : size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
16157 : 2401 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16158 : 2168 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16159 : 4309 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
16160 : : {
16161 : 537 : size = size_binop (MINUS_EXPR, size, size_one_node);
16162 : 537 : size = c_fully_fold (size, false, NULL);
16163 : 537 : size = save_expr (size);
16164 : 537 : tree index_type = build_index_type (size);
16165 : 537 : tree eltype = TREE_TYPE (first);
16166 : 549 : while (TREE_CODE (eltype) == ARRAY_TYPE)
16167 : 12 : eltype = TREE_TYPE (eltype);
16168 : 537 : tree type = c_build_array_type (eltype, index_type);
16169 : 537 : tree ptype = c_build_pointer_type (eltype);
16170 : 537 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16171 : 296 : t = build_fold_addr_expr (t);
16172 : 537 : tree t2 = build_fold_addr_expr (first);
16173 : 537 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16174 : : ptrdiff_type_node, t2);
16175 : 537 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16176 : : ptrdiff_type_node, t2,
16177 : 537 : fold_convert_loc (OMP_CLAUSE_LOCATION (c),
16178 : : ptrdiff_type_node, t));
16179 : 537 : t2 = c_fully_fold (t2, false, NULL);
16180 : 537 : if (tree_fits_shwi_p (t2))
16181 : 398 : t = build2 (MEM_REF, type, t,
16182 : 398 : build_int_cst (ptype, tree_to_shwi (t2)));
16183 : : else
16184 : : {
16185 : 139 : t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
16186 : 139 : t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
16187 : 139 : TREE_TYPE (t), t, t2);
16188 : 139 : t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
16189 : : }
16190 : 537 : OMP_CLAUSE_DECL (c) = t;
16191 : 537 : return false;
16192 : : }
16193 : 1864 : first = c_fully_fold (first, false, NULL);
16194 : 1864 : OMP_CLAUSE_DECL (c) = first;
16195 : 1864 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
16196 : : return false;
16197 : : /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
16198 : 1853 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
16199 : 1853 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
16200 : 1713 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
16201 : 1711 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
16202 : : {
16203 : 1849 : if (size)
16204 : 1849 : size = c_fully_fold (size, false, NULL);
16205 : 1849 : OMP_CLAUSE_SIZE (c) = size;
16206 : : }
16207 : :
16208 : 1853 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16209 : : return false;
16210 : :
16211 : 1715 : auto_vec<omp_addr_token *, 10> addr_tokens;
16212 : :
16213 : 1715 : if (!omp_parse_expr (addr_tokens, first))
16214 : 1715 : return true;
16215 : :
16216 : 1715 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
16217 : :
16218 : 1715 : tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
16219 : 1715 : if (nc != error_mark_node)
16220 : : {
16221 : 1715 : using namespace omp_addr_tokenizer;
16222 : :
16223 : 1715 : if (ai.maybe_zero_length_array_section (c))
16224 : 1711 : OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
16225 : :
16226 : : /* !!! If we're accessing a base decl via chained access
16227 : : methods (e.g. multiple indirections), duplicate clause
16228 : : detection won't work properly. Skip it in that case. */
16229 : 1715 : if ((addr_tokens[0]->type == STRUCTURE_BASE
16230 : 1428 : || addr_tokens[0]->type == ARRAY_BASE)
16231 : 1715 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
16232 : 1715 : && addr_tokens[1]->type == ACCESS_METHOD
16233 : 3430 : && omp_access_chain_p (addr_tokens, 1))
16234 : 34 : c = nc;
16235 : :
16236 : 1715 : return false;
16237 : : }
16238 : : }
16239 : : return false;
16240 : 2970 : }
16241 : :
16242 : : /* Helper function of finish_omp_clauses. Clone STMT as if we were making
16243 : : an inline call. But, remap
16244 : : the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
16245 : : and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
16246 : :
16247 : : static tree
16248 : 510 : c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
16249 : : tree decl, tree placeholder)
16250 : : {
16251 : 510 : copy_body_data id;
16252 : 510 : hash_map<tree, tree> decl_map;
16253 : :
16254 : 510 : decl_map.put (omp_decl1, placeholder);
16255 : 510 : decl_map.put (omp_decl2, decl);
16256 : 510 : memset (&id, 0, sizeof (id));
16257 : 510 : id.src_fn = DECL_CONTEXT (omp_decl1);
16258 : 510 : id.dst_fn = current_function_decl;
16259 : 510 : id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
16260 : 510 : id.decl_map = &decl_map;
16261 : :
16262 : 510 : id.copy_decl = copy_decl_no_change;
16263 : 510 : id.transform_call_graph_edges = CB_CGE_DUPLICATE;
16264 : 510 : id.transform_new_cfg = true;
16265 : 510 : id.transform_return_to_modify = false;
16266 : 510 : id.eh_lp_nr = 0;
16267 : 510 : walk_tree (&stmt, copy_tree_body_r, &id, NULL);
16268 : 510 : return stmt;
16269 : 510 : }
16270 : :
16271 : : /* Helper function of c_finish_omp_clauses, called via walk_tree.
16272 : : Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
16273 : :
16274 : : static tree
16275 : 1123 : c_find_omp_placeholder_r (tree *tp, int *, void *data)
16276 : : {
16277 : 1123 : if (*tp == (tree) data)
16278 : 28 : return *tp;
16279 : : return NULL_TREE;
16280 : : }
16281 : :
16282 : : /* Similarly, but also walk aggregate fields. */
16283 : :
16284 : : struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
16285 : :
16286 : : static tree
16287 : 288 : c_find_omp_var_r (tree *tp, int *, void *data)
16288 : : {
16289 : 288 : if (*tp == ((struct c_find_omp_var_s *) data)->var)
16290 : : return *tp;
16291 : 280 : if (RECORD_OR_UNION_TYPE_P (*tp))
16292 : : {
16293 : 2 : tree field;
16294 : 2 : hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
16295 : :
16296 : 2 : for (field = TYPE_FIELDS (*tp); field;
16297 : 0 : field = DECL_CHAIN (field))
16298 : 2 : if (TREE_CODE (field) == FIELD_DECL)
16299 : : {
16300 : 2 : tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
16301 : : c_find_omp_var_r, data, pset);
16302 : 2 : if (ret)
16303 : : return ret;
16304 : 2 : ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
16305 : 2 : if (ret)
16306 : : return ret;
16307 : 2 : ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
16308 : : pset);
16309 : 2 : if (ret)
16310 : : return ret;
16311 : 2 : ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
16312 : 2 : if (ret)
16313 : : return ret;
16314 : : }
16315 : : }
16316 : 278 : else if (INTEGRAL_TYPE_P (*tp))
16317 : 45 : return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
16318 : : ((struct c_find_omp_var_s *) data)->pset);
16319 : : return NULL_TREE;
16320 : : }
16321 : :
16322 : : /* Finish OpenMP iterators ITER. Return true if they are errorneous
16323 : : and clauses containing them should be removed. */
16324 : :
16325 : : static bool
16326 : 142 : c_omp_finish_iterators (tree iter)
16327 : : {
16328 : 142 : bool ret = false;
16329 : 323 : for (tree it = iter; it; it = TREE_CHAIN (it))
16330 : : {
16331 : 181 : tree var = TREE_VEC_ELT (it, 0);
16332 : 181 : tree begin = TREE_VEC_ELT (it, 1);
16333 : 181 : tree end = TREE_VEC_ELT (it, 2);
16334 : 181 : tree step = TREE_VEC_ELT (it, 3);
16335 : 181 : tree orig_step;
16336 : 181 : tree type = TREE_TYPE (var);
16337 : 181 : location_t loc = DECL_SOURCE_LOCATION (var);
16338 : 181 : if (type == error_mark_node)
16339 : : {
16340 : 0 : ret = true;
16341 : 34 : continue;
16342 : : }
16343 : 181 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16344 : : {
16345 : 6 : error_at (loc, "iterator %qD has neither integral nor pointer type",
16346 : : var);
16347 : 6 : ret = true;
16348 : 6 : continue;
16349 : : }
16350 : 175 : else if (TYPE_ATOMIC (type))
16351 : : {
16352 : 2 : error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
16353 : 2 : ret = true;
16354 : 2 : continue;
16355 : : }
16356 : 173 : else if (TYPE_READONLY (type))
16357 : : {
16358 : 4 : error_at (loc, "iterator %qD has const qualified type", var);
16359 : 4 : ret = true;
16360 : 4 : continue;
16361 : : }
16362 : 169 : else if (step == error_mark_node
16363 : 169 : || TREE_TYPE (step) == error_mark_node)
16364 : : {
16365 : 0 : ret = true;
16366 : 0 : continue;
16367 : : }
16368 : 169 : else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
16369 : : {
16370 : 6 : error_at (EXPR_LOC_OR_LOC (step, loc),
16371 : : "iterator step with non-integral type");
16372 : 4 : ret = true;
16373 : 4 : continue;
16374 : : }
16375 : 165 : begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
16376 : 165 : end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
16377 : 165 : orig_step = save_expr (c_fully_fold (step, false, NULL));
16378 : 165 : tree stype = POINTER_TYPE_P (type) ? sizetype : type;
16379 : 165 : step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
16380 : 165 : if (POINTER_TYPE_P (type))
16381 : : {
16382 : 14 : begin = save_expr (begin);
16383 : 14 : step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
16384 : 14 : step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
16385 : : fold_convert (sizetype, step),
16386 : : fold_convert (sizetype, begin));
16387 : 14 : step = fold_convert (ssizetype, step);
16388 : : }
16389 : 165 : if (integer_zerop (step))
16390 : : {
16391 : 6 : error_at (loc, "iterator %qD has zero step", var);
16392 : 6 : ret = true;
16393 : 6 : continue;
16394 : : }
16395 : :
16396 : 159 : if (begin == error_mark_node
16397 : 157 : || end == error_mark_node
16398 : 155 : || step == error_mark_node
16399 : 155 : || orig_step == error_mark_node)
16400 : : {
16401 : 4 : ret = true;
16402 : 4 : continue;
16403 : : }
16404 : 155 : hash_set<tree> pset;
16405 : 155 : tree it2;
16406 : 196 : for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
16407 : : {
16408 : 49 : tree var2 = TREE_VEC_ELT (it2, 0);
16409 : 49 : tree begin2 = TREE_VEC_ELT (it2, 1);
16410 : 49 : tree end2 = TREE_VEC_ELT (it2, 2);
16411 : 49 : tree step2 = TREE_VEC_ELT (it2, 3);
16412 : 49 : tree type2 = TREE_TYPE (var2);
16413 : 49 : location_t loc2 = DECL_SOURCE_LOCATION (var2);
16414 : 49 : struct c_find_omp_var_s data = { var, &pset };
16415 : 49 : if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
16416 : : {
16417 : 2 : error_at (loc2,
16418 : : "type of iterator %qD refers to outer iterator %qD",
16419 : : var2, var);
16420 : 10 : break;
16421 : : }
16422 : 47 : else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
16423 : : {
16424 : 2 : error_at (EXPR_LOC_OR_LOC (begin2, loc2),
16425 : : "begin expression refers to outer iterator %qD", var);
16426 : 2 : break;
16427 : : }
16428 : 45 : else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
16429 : : {
16430 : 2 : error_at (EXPR_LOC_OR_LOC (end2, loc2),
16431 : : "end expression refers to outer iterator %qD", var);
16432 : 2 : break;
16433 : : }
16434 : 43 : else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
16435 : : {
16436 : 2 : error_at (EXPR_LOC_OR_LOC (step2, loc2),
16437 : : "step expression refers to outer iterator %qD", var);
16438 : 2 : break;
16439 : : }
16440 : : }
16441 : 155 : if (it2)
16442 : : {
16443 : 8 : ret = true;
16444 : 8 : continue;
16445 : : }
16446 : 147 : TREE_VEC_ELT (it, 1) = begin;
16447 : 147 : TREE_VEC_ELT (it, 2) = end;
16448 : 147 : TREE_VEC_ELT (it, 3) = step;
16449 : 147 : TREE_VEC_ELT (it, 4) = orig_step;
16450 : 155 : }
16451 : 142 : return ret;
16452 : : }
16453 : :
16454 : : /* Ensure that pointers are used in OpenACC attach and detach clauses.
16455 : : Return true if an error has been detected. */
16456 : :
16457 : : static bool
16458 : 9382 : c_oacc_check_attachments (tree c)
16459 : : {
16460 : 9382 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
16461 : : return false;
16462 : :
16463 : : /* OpenACC attach / detach clauses must be pointers. */
16464 : 6387 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
16465 : 6387 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
16466 : : {
16467 : 71 : tree t = OMP_CLAUSE_DECL (c);
16468 : :
16469 : 77 : while (TREE_CODE (t) == OMP_ARRAY_SECTION)
16470 : 6 : t = TREE_OPERAND (t, 0);
16471 : :
16472 : 71 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16473 : : {
16474 : 6 : error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
16475 : : user_omp_clause_code_name (c, true));
16476 : 6 : return true;
16477 : : }
16478 : : }
16479 : :
16480 : : return false;
16481 : : }
16482 : :
16483 : : /* For all elements of CLAUSES, validate them against their constraints.
16484 : : Remove any elements from the list that are invalid. */
16485 : :
16486 : : tree
16487 : 29212 : c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
16488 : : {
16489 : 29212 : bitmap_head generic_head, firstprivate_head, lastprivate_head;
16490 : 29212 : bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
16491 : 29212 : bitmap_head oacc_reduction_head, is_on_device_head;
16492 : 29212 : tree c, t, type, *pc;
16493 : 29212 : tree simdlen = NULL_TREE, safelen = NULL_TREE;
16494 : 29212 : bool branch_seen = false;
16495 : 29212 : bool copyprivate_seen = false;
16496 : 29212 : bool mergeable_seen = false;
16497 : 29212 : tree *detach_seen = NULL;
16498 : 29212 : bool linear_variable_step_check = false;
16499 : 29212 : tree *nowait_clause = NULL;
16500 : 29212 : tree *grainsize_seen = NULL;
16501 : 29212 : bool num_tasks_seen = false;
16502 : 29212 : tree ordered_clause = NULL_TREE;
16503 : 29212 : tree schedule_clause = NULL_TREE;
16504 : 29212 : bool oacc_async = false;
16505 : 29212 : tree last_iterators = NULL_TREE;
16506 : 29212 : bool last_iterators_remove = false;
16507 : 29212 : tree *nogroup_seen = NULL;
16508 : 29212 : tree *order_clause = NULL;
16509 : : /* 1 if normal/task reduction has been seen, -1 if inscan reduction
16510 : : has been seen, -2 if mixed inscan/normal reduction diagnosed. */
16511 : 29212 : int reduction_seen = 0;
16512 : 29212 : bool allocate_seen = false;
16513 : 29212 : bool implicit_moved = false;
16514 : 29212 : bool target_in_reduction_seen = false;
16515 : 29212 : tree *full_seen = NULL;
16516 : 29212 : bool partial_seen = false;
16517 : 29212 : bool openacc = (ort & C_ORT_ACC) != 0;
16518 : 29212 : bool init_seen = false;
16519 : 29212 : bool init_use_destroy_seen = false;
16520 : 29212 : tree init_no_targetsync_clause = NULL_TREE;
16521 : 29212 : tree depend_clause = NULL_TREE;
16522 : :
16523 : 29212 : bitmap_obstack_initialize (NULL);
16524 : 29212 : bitmap_initialize (&generic_head, &bitmap_default_obstack);
16525 : 29212 : bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
16526 : 29212 : bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
16527 : 29212 : bitmap_initialize (&aligned_head, &bitmap_default_obstack);
16528 : : /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
16529 : 29212 : bitmap_initialize (&map_head, &bitmap_default_obstack);
16530 : 29212 : bitmap_initialize (&map_field_head, &bitmap_default_obstack);
16531 : 29212 : bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
16532 : : /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
16533 : : instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
16534 : 29212 : bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
16535 : 29212 : bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
16536 : :
16537 : 29212 : if (openacc)
16538 : 12261 : for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16539 : 7250 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
16540 : : {
16541 : : oacc_async = true;
16542 : : break;
16543 : : }
16544 : :
16545 : 29212 : tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
16546 : :
16547 : 76856 : for (pc = &clauses, c = clauses; c ; c = *pc)
16548 : : {
16549 : 47644 : bool remove = false;
16550 : 47644 : bool need_complete = false;
16551 : 47644 : bool need_implicitly_determined = false;
16552 : :
16553 : : /* We've reached the end of a list of expanded nodes. Reset the group
16554 : : start pointer. */
16555 : 47644 : if (c == grp_sentinel)
16556 : : {
16557 : 2467 : if (grp_start_p
16558 : 2467 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
16559 : 32 : for (tree gc = *grp_start_p; gc != grp_sentinel;
16560 : 22 : gc = OMP_CLAUSE_CHAIN (gc))
16561 : 22 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
16562 : : grp_start_p = NULL;
16563 : : }
16564 : :
16565 : 47644 : switch (OMP_CLAUSE_CODE (c))
16566 : : {
16567 : 1137 : case OMP_CLAUSE_SHARED:
16568 : 1137 : need_implicitly_determined = true;
16569 : 1137 : goto check_dup_generic;
16570 : :
16571 : 837 : case OMP_CLAUSE_PRIVATE:
16572 : 837 : need_complete = true;
16573 : 837 : need_implicitly_determined = true;
16574 : 837 : goto check_dup_generic;
16575 : :
16576 : 3516 : case OMP_CLAUSE_REDUCTION:
16577 : 3516 : if (reduction_seen == 0)
16578 : 2917 : reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
16579 : 599 : else if (reduction_seen != -2
16580 : 1198 : && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
16581 : 599 : ? -1 : 1))
16582 : : {
16583 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16584 : : "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
16585 : : "on the same construct");
16586 : 2 : reduction_seen = -2;
16587 : : }
16588 : : /* FALLTHRU */
16589 : 4179 : case OMP_CLAUSE_IN_REDUCTION:
16590 : 4179 : case OMP_CLAUSE_TASK_REDUCTION:
16591 : 4179 : need_implicitly_determined = true;
16592 : 4179 : t = OMP_CLAUSE_DECL (c);
16593 : 4179 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
16594 : : {
16595 : 553 : if (handle_omp_array_sections (c, ort))
16596 : : {
16597 : : remove = true;
16598 : : break;
16599 : : }
16600 : :
16601 : 537 : t = OMP_CLAUSE_DECL (c);
16602 : 537 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16603 : 537 : && OMP_CLAUSE_REDUCTION_INSCAN (c))
16604 : : {
16605 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16606 : : "%<inscan%> %<reduction%> clause with array "
16607 : : "section");
16608 : 1 : remove = true;
16609 : 1 : break;
16610 : : }
16611 : : }
16612 : 4162 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
16613 : 4162 : if (t == error_mark_node)
16614 : : {
16615 : : remove = true;
16616 : : break;
16617 : : }
16618 : 4160 : if (oacc_async)
16619 : 3 : c_mark_addressable (t);
16620 : 4160 : type = TREE_TYPE (t);
16621 : 4160 : if (TREE_CODE (t) == MEM_REF)
16622 : 536 : type = TREE_TYPE (type);
16623 : 4160 : if (TREE_CODE (type) == ARRAY_TYPE)
16624 : : {
16625 : 74 : tree oatype = type;
16626 : 74 : gcc_assert (TREE_CODE (t) != MEM_REF);
16627 : 148 : while (TREE_CODE (type) == ARRAY_TYPE)
16628 : 74 : type = TREE_TYPE (type);
16629 : 74 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16630 : : {
16631 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16632 : : "%qD in %<reduction%> clause is a zero size array",
16633 : : t);
16634 : 1 : remove = true;
16635 : 1 : break;
16636 : : }
16637 : 73 : tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
16638 : : TYPE_SIZE_UNIT (type));
16639 : 73 : if (integer_zerop (size))
16640 : : {
16641 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16642 : : "%qD in %<reduction%> clause is a zero size array",
16643 : : t);
16644 : 1 : remove = true;
16645 : 1 : break;
16646 : : }
16647 : 72 : size = size_binop (MINUS_EXPR, size, size_one_node);
16648 : 72 : size = save_expr (size);
16649 : 72 : tree index_type = build_index_type (size);
16650 : 72 : tree atype = c_build_array_type (TYPE_MAIN_VARIANT (type),
16651 : : index_type);
16652 : 72 : atype = c_build_qualified_type (atype, TYPE_QUALS (type));
16653 : 72 : tree ptype = c_build_pointer_type (type);
16654 : 72 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
16655 : 72 : t = build_fold_addr_expr (t);
16656 : 72 : t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
16657 : 72 : OMP_CLAUSE_DECL (c) = t;
16658 : : }
16659 : 4158 : if (TYPE_ATOMIC (type))
16660 : : {
16661 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16662 : : "%<_Atomic%> %qE in %<reduction%> clause", t);
16663 : 3 : remove = true;
16664 : 3 : break;
16665 : : }
16666 : 4155 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
16667 : 4155 : || OMP_CLAUSE_REDUCTION_TASK (c))
16668 : : {
16669 : : /* Disallow zero sized or potentially zero sized task
16670 : : reductions. */
16671 : 870 : if (integer_zerop (TYPE_SIZE_UNIT (type)))
16672 : : {
16673 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
16674 : : "zero sized type %qT in %qs clause", type,
16675 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16676 : 3 : remove = true;
16677 : 3 : break;
16678 : : }
16679 : 867 : else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
16680 : : {
16681 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16682 : : "variable sized type %qT in %qs clause", type,
16683 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16684 : 0 : remove = true;
16685 : 0 : break;
16686 : : }
16687 : : }
16688 : 4152 : if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
16689 : 4152 : && (FLOAT_TYPE_P (type)
16690 : 3600 : || TREE_CODE (type) == COMPLEX_TYPE))
16691 : : {
16692 : 340 : enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
16693 : 340 : const char *r_name = NULL;
16694 : :
16695 : 340 : switch (r_code)
16696 : : {
16697 : : case PLUS_EXPR:
16698 : : case MULT_EXPR:
16699 : : case MINUS_EXPR:
16700 : : case TRUTH_ANDIF_EXPR:
16701 : : case TRUTH_ORIF_EXPR:
16702 : : break;
16703 : 13 : case MIN_EXPR:
16704 : 13 : if (TREE_CODE (type) == COMPLEX_TYPE)
16705 : : r_name = "min";
16706 : : break;
16707 : 38 : case MAX_EXPR:
16708 : 38 : if (TREE_CODE (type) == COMPLEX_TYPE)
16709 : : r_name = "max";
16710 : : break;
16711 : 3 : case BIT_AND_EXPR:
16712 : 3 : r_name = "&";
16713 : 3 : break;
16714 : 2 : case BIT_XOR_EXPR:
16715 : 2 : r_name = "^";
16716 : 2 : break;
16717 : : case BIT_IOR_EXPR:
16718 : : r_name = "|";
16719 : : break;
16720 : 0 : default:
16721 : 0 : gcc_unreachable ();
16722 : : }
16723 : 5 : if (r_name)
16724 : : {
16725 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
16726 : : "%qE has invalid type for %<reduction(%s)%>",
16727 : : t, r_name);
16728 : 12 : remove = true;
16729 : 12 : break;
16730 : : }
16731 : : }
16732 : 3812 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
16733 : : {
16734 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16735 : : "user defined reduction not found for %qE", t);
16736 : 2 : remove = true;
16737 : 2 : break;
16738 : : }
16739 : 3810 : else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
16740 : : {
16741 : 277 : tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
16742 : 277 : type = TYPE_MAIN_VARIANT (type);
16743 : 277 : tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16744 : : VAR_DECL, NULL_TREE, type);
16745 : 277 : tree decl_placeholder = NULL_TREE;
16746 : 277 : OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
16747 : 277 : DECL_ARTIFICIAL (placeholder) = 1;
16748 : 277 : DECL_IGNORED_P (placeholder) = 1;
16749 : 277 : if (TREE_CODE (t) == MEM_REF)
16750 : : {
16751 : 56 : decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
16752 : : VAR_DECL, NULL_TREE, type);
16753 : 56 : OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
16754 : 56 : DECL_ARTIFICIAL (decl_placeholder) = 1;
16755 : 56 : DECL_IGNORED_P (decl_placeholder) = 1;
16756 : : }
16757 : 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
16758 : 42 : c_mark_addressable (placeholder);
16759 : 277 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
16760 : 76 : c_mark_addressable (decl_placeholder ? decl_placeholder
16761 : 34 : : OMP_CLAUSE_DECL (c));
16762 : 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16763 : 775 : = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
16764 : 277 : TREE_VEC_ELT (list, 0),
16765 : 277 : TREE_VEC_ELT (list, 1),
16766 : : decl_placeholder ? decl_placeholder
16767 : 221 : : OMP_CLAUSE_DECL (c), placeholder);
16768 : 277 : OMP_CLAUSE_REDUCTION_MERGE (c)
16769 : 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16770 : : void_type_node, NULL_TREE,
16771 : 277 : OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
16772 : 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
16773 : 277 : if (TREE_VEC_LENGTH (list) == 6)
16774 : : {
16775 : 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
16776 : 70 : c_mark_addressable (decl_placeholder ? decl_placeholder
16777 : 33 : : OMP_CLAUSE_DECL (c));
16778 : 233 : if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
16779 : 27 : c_mark_addressable (placeholder);
16780 : 233 : tree init = TREE_VEC_ELT (list, 5);
16781 : 233 : if (init == error_mark_node)
16782 : 197 : init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
16783 : 233 : OMP_CLAUSE_REDUCTION_INIT (c)
16784 : 651 : = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
16785 : 233 : TREE_VEC_ELT (list, 3),
16786 : : decl_placeholder ? decl_placeholder
16787 : 185 : : OMP_CLAUSE_DECL (c), placeholder);
16788 : 233 : if (TREE_VEC_ELT (list, 5) == error_mark_node)
16789 : : {
16790 : 197 : tree v = decl_placeholder ? decl_placeholder : t;
16791 : 197 : OMP_CLAUSE_REDUCTION_INIT (c)
16792 : 394 : = build2 (INIT_EXPR, TREE_TYPE (v), v,
16793 : 197 : OMP_CLAUSE_REDUCTION_INIT (c));
16794 : : }
16795 : 233 : if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
16796 : : c_find_omp_placeholder_r,
16797 : : placeholder, NULL))
16798 : 28 : OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
16799 : : }
16800 : : else
16801 : : {
16802 : 44 : tree init;
16803 : 44 : tree v = decl_placeholder ? decl_placeholder : t;
16804 : 44 : if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
16805 : 34 : init = build_constructor (TREE_TYPE (v), NULL);
16806 : : else
16807 : 10 : init = fold_convert (TREE_TYPE (v), integer_zero_node);
16808 : 44 : OMP_CLAUSE_REDUCTION_INIT (c)
16809 : 88 : = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
16810 : : }
16811 : 277 : OMP_CLAUSE_REDUCTION_INIT (c)
16812 : 277 : = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
16813 : : void_type_node, NULL_TREE,
16814 : 277 : OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
16815 : 277 : TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
16816 : : }
16817 : 4138 : if (TREE_CODE (t) == MEM_REF)
16818 : : {
16819 : 607 : if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
16820 : 607 : || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
16821 : : != INTEGER_CST)
16822 : : {
16823 : 0 : sorry ("variable length element type in array "
16824 : : "%<reduction%> clause");
16825 : 0 : remove = true;
16826 : 0 : break;
16827 : : }
16828 : 607 : t = TREE_OPERAND (t, 0);
16829 : 607 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16830 : 139 : t = TREE_OPERAND (t, 0);
16831 : 607 : if (TREE_CODE (t) == ADDR_EXPR)
16832 : 367 : t = TREE_OPERAND (t, 0);
16833 : : }
16834 : 4138 : goto check_dup_generic_t;
16835 : :
16836 : 24 : case OMP_CLAUSE_COPYPRIVATE:
16837 : 24 : copyprivate_seen = true;
16838 : 24 : if (nowait_clause)
16839 : : {
16840 : 1 : error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
16841 : : "%<nowait%> clause must not be used together "
16842 : : "with %<copyprivate%> clause");
16843 : 1 : *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
16844 : 1 : nowait_clause = NULL;
16845 : : }
16846 : 24 : goto check_dup_generic;
16847 : :
16848 : 99 : case OMP_CLAUSE_COPYIN:
16849 : 99 : t = OMP_CLAUSE_DECL (c);
16850 : 99 : if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
16851 : : {
16852 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
16853 : : "%qE must be %<threadprivate%> for %<copyin%>", t);
16854 : 5 : remove = true;
16855 : 5 : break;
16856 : : }
16857 : 94 : goto check_dup_generic;
16858 : :
16859 : 478 : case OMP_CLAUSE_LINEAR:
16860 : 478 : if (ort != C_ORT_OMP_DECLARE_SIMD)
16861 : 327 : need_implicitly_determined = true;
16862 : 478 : t = OMP_CLAUSE_DECL (c);
16863 : 478 : if (ort != C_ORT_OMP_DECLARE_SIMD
16864 : 327 : && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
16865 : 483 : && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
16866 : : {
16867 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
16868 : : "modifier should not be specified in %<linear%> "
16869 : : "clause on %<simd%> or %<for%> constructs when not "
16870 : : "using OpenMP 5.2 modifiers");
16871 : 5 : OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
16872 : : }
16873 : 956 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
16874 : 501 : && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
16875 : : {
16876 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
16877 : : "linear clause applied to non-integral non-pointer "
16878 : 1 : "variable with type %qT", TREE_TYPE (t));
16879 : 1 : remove = true;
16880 : 1 : break;
16881 : : }
16882 : 477 : if (TYPE_ATOMIC (TREE_TYPE (t)))
16883 : : {
16884 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16885 : : "%<_Atomic%> %qD in %<linear%> clause", t);
16886 : 3 : remove = true;
16887 : 3 : break;
16888 : : }
16889 : 474 : if (ort == C_ORT_OMP_DECLARE_SIMD)
16890 : : {
16891 : 150 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
16892 : 150 : if (TREE_CODE (s) == PARM_DECL)
16893 : : {
16894 : 9 : OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
16895 : : /* map_head bitmap is used as uniform_head if
16896 : : declare_simd. */
16897 : 9 : if (!bitmap_bit_p (&map_head, DECL_UID (s)))
16898 : 5 : linear_variable_step_check = true;
16899 : 9 : goto check_dup_generic;
16900 : : }
16901 : 141 : if (TREE_CODE (s) != INTEGER_CST)
16902 : : {
16903 : 7 : error_at (OMP_CLAUSE_LOCATION (c),
16904 : : "%<linear%> clause step %qE is neither constant "
16905 : : "nor a parameter", s);
16906 : 7 : remove = true;
16907 : 7 : break;
16908 : : }
16909 : : }
16910 : 458 : if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
16911 : : {
16912 : 20 : tree s = OMP_CLAUSE_LINEAR_STEP (c);
16913 : 20 : s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
16914 : 20 : OMP_CLAUSE_DECL (c), s);
16915 : 20 : s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
16916 : : sizetype, fold_convert (sizetype, s),
16917 : 20 : fold_convert
16918 : : (sizetype, OMP_CLAUSE_DECL (c)));
16919 : 20 : if (s == error_mark_node)
16920 : 0 : s = size_one_node;
16921 : 20 : OMP_CLAUSE_LINEAR_STEP (c) = s;
16922 : : }
16923 : : else
16924 : 438 : OMP_CLAUSE_LINEAR_STEP (c)
16925 : 876 : = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
16926 : 458 : goto check_dup_generic;
16927 : :
16928 : 2872 : check_dup_generic:
16929 : 2872 : t = OMP_CLAUSE_DECL (c);
16930 : 7098 : check_dup_generic_t:
16931 : 7098 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
16932 : : {
16933 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
16934 : : "%qE is not a variable in clause %qs", t,
16935 : 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
16936 : 2 : remove = true;
16937 : : }
16938 : 1149 : else if ((openacc && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
16939 : 6057 : || (ort == C_ORT_OMP
16940 : 5349 : && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
16941 : 5338 : || (OMP_CLAUSE_CODE (c)
16942 : : == OMP_CLAUSE_USE_DEVICE_ADDR)))
16943 : 13106 : || (ort == C_ORT_OMP_TARGET
16944 : 338 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
16945 : : {
16946 : 1196 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
16947 : 1196 : && (bitmap_bit_p (&generic_head, DECL_UID (t))
16948 : 109 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
16949 : : {
16950 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
16951 : : "%qD appears more than once in data-sharing "
16952 : : "clauses", t);
16953 : 2 : remove = true;
16954 : 2 : break;
16955 : : }
16956 : 1194 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
16957 : 108 : target_in_reduction_seen = true;
16958 : 1194 : if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16959 : : {
16960 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
16961 : : openacc
16962 : : ? "%qD appears more than once in reduction clauses"
16963 : : : "%qD appears more than once in data clauses",
16964 : : t);
16965 : 2 : remove = true;
16966 : : }
16967 : : else
16968 : 1192 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
16969 : : }
16970 : 5900 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
16971 : 5878 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
16972 : 5877 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
16973 : 11777 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
16974 : : {
16975 : 23 : error_at (OMP_CLAUSE_LOCATION (c),
16976 : : "%qE appears more than once in data clauses", t);
16977 : 23 : remove = true;
16978 : : }
16979 : 5877 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16980 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
16981 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
16982 : 1036 : && bitmap_bit_p (&map_head, DECL_UID (t)))
16983 : : {
16984 : 3 : if (openacc)
16985 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
16986 : : "%qD appears more than once in data clauses", t);
16987 : : else
16988 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
16989 : : "%qD appears both in data and map clauses", t);
16990 : : remove = true;
16991 : : }
16992 : : else
16993 : 5874 : bitmap_set_bit (&generic_head, DECL_UID (t));
16994 : : break;
16995 : :
16996 : 1320 : case OMP_CLAUSE_FIRSTPRIVATE:
16997 : 1320 : if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
16998 : : {
16999 : 186 : move_implicit:
17000 : 186 : implicit_moved = true;
17001 : : /* Move firstprivate and map clauses with
17002 : : OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
17003 : : clauses chain. */
17004 : 186 : tree cl1 = NULL_TREE, cl2 = NULL_TREE;
17005 : 186 : tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
17006 : 1072 : while (*pc1)
17007 : 886 : if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
17008 : 886 : && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
17009 : : {
17010 : 113 : *pc3 = *pc1;
17011 : 113 : pc3 = &OMP_CLAUSE_CHAIN (*pc3);
17012 : 113 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17013 : : }
17014 : 773 : else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
17015 : 773 : && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
17016 : : {
17017 : 164 : *pc2 = *pc1;
17018 : 164 : pc2 = &OMP_CLAUSE_CHAIN (*pc2);
17019 : 164 : *pc1 = OMP_CLAUSE_CHAIN (*pc1);
17020 : : }
17021 : : else
17022 : 609 : pc1 = &OMP_CLAUSE_CHAIN (*pc1);
17023 : 186 : *pc3 = NULL;
17024 : 186 : *pc2 = cl2;
17025 : 186 : *pc1 = cl1;
17026 : 186 : continue;
17027 : 186 : }
17028 : 1225 : t = OMP_CLAUSE_DECL (c);
17029 : 1225 : need_complete = true;
17030 : 1225 : need_implicitly_determined = true;
17031 : 1225 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17032 : : {
17033 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17034 : : "%qE is not a variable in clause %<firstprivate%>", t);
17035 : 1 : remove = true;
17036 : : }
17037 : 1224 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17038 : 113 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
17039 : 1330 : && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17040 : : remove = true;
17041 : 1224 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17042 : 1222 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17043 : 2445 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17044 : : {
17045 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
17046 : : "%qE appears more than once in data clauses", t);
17047 : 4 : remove = true;
17048 : : }
17049 : 1220 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17050 : 1220 : || bitmap_bit_p (&map_field_head, DECL_UID (t)))
17051 : : {
17052 : 7 : if (openacc)
17053 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17054 : : "%qD appears more than once in data clauses", t);
17055 : 7 : else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
17056 : 7 : && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
17057 : : /* Silently drop the clause. */;
17058 : : else
17059 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17060 : : "%qD appears both in data and map clauses", t);
17061 : : remove = true;
17062 : : }
17063 : : else
17064 : 1213 : bitmap_set_bit (&firstprivate_head, DECL_UID (t));
17065 : : break;
17066 : :
17067 : 1522 : case OMP_CLAUSE_LASTPRIVATE:
17068 : 1522 : t = OMP_CLAUSE_DECL (c);
17069 : 1522 : need_complete = true;
17070 : 1522 : need_implicitly_determined = true;
17071 : 1522 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17072 : : {
17073 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17074 : : "%qE is not a variable in clause %<lastprivate%>", t);
17075 : 0 : remove = true;
17076 : : }
17077 : 1522 : else if (bitmap_bit_p (&generic_head, DECL_UID (t))
17078 : 1522 : || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
17079 : : {
17080 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17081 : : "%qE appears more than once in data clauses", t);
17082 : 3 : remove = true;
17083 : : }
17084 : : else
17085 : 1519 : bitmap_set_bit (&lastprivate_head, DECL_UID (t));
17086 : : break;
17087 : :
17088 : 253 : case OMP_CLAUSE_ALIGNED:
17089 : 253 : t = OMP_CLAUSE_DECL (c);
17090 : 253 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17091 : : {
17092 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17093 : : "%qE is not a variable in %<aligned%> clause", t);
17094 : 0 : remove = true;
17095 : : }
17096 : 289 : else if (!POINTER_TYPE_P (TREE_TYPE (t))
17097 : 289 : && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17098 : : {
17099 : 7 : error_at (OMP_CLAUSE_LOCATION (c),
17100 : : "%qE in %<aligned%> clause is neither a pointer nor "
17101 : : "an array", t);
17102 : 7 : remove = true;
17103 : : }
17104 : 246 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17105 : : {
17106 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17107 : : "%<_Atomic%> %qD in %<aligned%> clause", t);
17108 : 1 : remove = true;
17109 : 1 : break;
17110 : : }
17111 : 245 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17112 : : {
17113 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17114 : : "%qE appears more than once in %<aligned%> clauses",
17115 : : t);
17116 : 0 : remove = true;
17117 : : }
17118 : : else
17119 : 245 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17120 : : break;
17121 : :
17122 : 125 : case OMP_CLAUSE_NONTEMPORAL:
17123 : 125 : t = OMP_CLAUSE_DECL (c);
17124 : 125 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17125 : : {
17126 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17127 : : "%qE is not a variable in %<nontemporal%> clause", t);
17128 : 0 : remove = true;
17129 : : }
17130 : 125 : else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
17131 : : {
17132 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17133 : : "%qE appears more than once in %<nontemporal%> "
17134 : : "clauses", t);
17135 : 2 : remove = true;
17136 : : }
17137 : : else
17138 : 123 : bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
17139 : : break;
17140 : :
17141 : 786 : case OMP_CLAUSE_ALLOCATE:
17142 : 786 : t = OMP_CLAUSE_DECL (c);
17143 : 786 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17144 : : {
17145 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17146 : : "%qE is not a variable in %<allocate%> clause", t);
17147 : 1 : remove = true;
17148 : : }
17149 : 785 : else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
17150 : : {
17151 : 2 : warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
17152 : : "%qE appears more than once in %<allocate%> clauses",
17153 : : t);
17154 : 2 : remove = true;
17155 : : }
17156 : : else
17157 : : {
17158 : 783 : bitmap_set_bit (&aligned_head, DECL_UID (t));
17159 : 783 : if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
17160 : 684 : allocate_seen = true;
17161 : : }
17162 : : break;
17163 : :
17164 : 355 : case OMP_CLAUSE_DOACROSS:
17165 : 355 : t = OMP_CLAUSE_DECL (c);
17166 : 355 : if (t == NULL_TREE)
17167 : : break;
17168 : 168 : if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
17169 : : {
17170 : 168 : gcc_assert (TREE_CODE (t) == TREE_LIST);
17171 : 1313 : for (; t; t = TREE_CHAIN (t))
17172 : : {
17173 : 1145 : tree decl = TREE_VALUE (t);
17174 : 1145 : if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
17175 : : {
17176 : 3 : tree offset = TREE_PURPOSE (t);
17177 : 3 : bool neg = wi::neg_p (wi::to_wide (offset));
17178 : 3 : offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
17179 : 6 : tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
17180 : : neg ? MINUS_EXPR : PLUS_EXPR,
17181 : : decl, offset);
17182 : 3 : t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
17183 : : sizetype,
17184 : : fold_convert (sizetype, t2),
17185 : : fold_convert (sizetype, decl));
17186 : 3 : if (t2 == error_mark_node)
17187 : : {
17188 : : remove = true;
17189 : : break;
17190 : : }
17191 : 3 : TREE_PURPOSE (t) = t2;
17192 : : }
17193 : : }
17194 : : break;
17195 : : }
17196 : 0 : gcc_unreachable ();
17197 : 713 : case OMP_CLAUSE_DEPEND:
17198 : 713 : depend_clause = c;
17199 : : /* FALLTHRU */
17200 : 870 : case OMP_CLAUSE_AFFINITY:
17201 : 870 : t = OMP_CLAUSE_DECL (c);
17202 : 870 : if (OMP_ITERATOR_DECL_P (t))
17203 : : {
17204 : 131 : if (TREE_PURPOSE (t) != last_iterators)
17205 : 111 : last_iterators_remove
17206 : 111 : = c_omp_finish_iterators (TREE_PURPOSE (t));
17207 : 131 : last_iterators = TREE_PURPOSE (t);
17208 : 131 : t = TREE_VALUE (t);
17209 : 131 : if (last_iterators_remove)
17210 : 34 : t = error_mark_node;
17211 : : }
17212 : : else
17213 : : last_iterators = NULL_TREE;
17214 : 870 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17215 : : {
17216 : 413 : if (handle_omp_array_sections (c, ort))
17217 : : remove = true;
17218 : 336 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17219 : 336 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17220 : : {
17221 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17222 : : "%<depend%> clause with %<depobj%> dependence "
17223 : : "type on array section");
17224 : 1 : remove = true;
17225 : : }
17226 : : break;
17227 : : }
17228 : 457 : if (t == error_mark_node)
17229 : : remove = true;
17230 : 423 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17231 : 423 : && t == ridpointers[RID_OMP_ALL_MEMORY])
17232 : : {
17233 : 15 : if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
17234 : 15 : && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
17235 : : {
17236 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17237 : : "%<omp_all_memory%> used with %<depend%> kind "
17238 : : "other than %<out%> or %<inout%>");
17239 : 3 : remove = true;
17240 : : }
17241 : : }
17242 : 408 : else if (!lvalue_p (t))
17243 : : {
17244 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17245 : : "%qE is not lvalue expression nor array section in "
17246 : : "%qs clause", t,
17247 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17248 : 3 : remove = true;
17249 : : }
17250 : 405 : else if (TREE_CODE (t) == COMPONENT_REF
17251 : 405 : && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
17252 : : {
17253 : 2 : gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17254 : : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
17255 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17256 : : "bit-field %qE in %qs clause", t,
17257 : 2 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17258 : 2 : remove = true;
17259 : : }
17260 : 403 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17261 : 403 : && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
17262 : : {
17263 : 20 : if (!c_omp_depend_t_p (TREE_TYPE (t)))
17264 : : {
17265 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17266 : : "%qE does not have %<omp_depend_t%> type in "
17267 : : "%<depend%> clause with %<depobj%> dependence "
17268 : : "type", t);
17269 : 2 : remove = true;
17270 : : }
17271 : : }
17272 : 383 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
17273 : 383 : && c_omp_depend_t_p (TREE_TYPE (t)))
17274 : : {
17275 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17276 : : "%qE should not have %<omp_depend_t%> type in "
17277 : : "%<depend%> clause with dependence type other than "
17278 : : "%<depobj%>", t);
17279 : 2 : remove = true;
17280 : : }
17281 : 12 : if (!remove)
17282 : : {
17283 : 411 : if (t == ridpointers[RID_OMP_ALL_MEMORY])
17284 : 12 : t = null_pointer_node;
17285 : : else
17286 : : {
17287 : 399 : tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
17288 : : ADDR_EXPR, t, false);
17289 : 399 : if (addr == error_mark_node)
17290 : : {
17291 : : remove = true;
17292 : : break;
17293 : : }
17294 : 399 : t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
17295 : : RO_UNARY_STAR);
17296 : 399 : if (t == error_mark_node)
17297 : : {
17298 : : remove = true;
17299 : : break;
17300 : : }
17301 : : }
17302 : 411 : if (OMP_ITERATOR_DECL_P (OMP_CLAUSE_DECL (c)))
17303 : 31 : TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
17304 : : else
17305 : 380 : OMP_CLAUSE_DECL (c) = t;
17306 : : }
17307 : : break;
17308 : :
17309 : 6508 : case OMP_CLAUSE_MAP:
17310 : 6508 : if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
17311 : 91 : goto move_implicit;
17312 : 6417 : if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_PUSH_MAPPER_NAME
17313 : 6417 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POP_MAPPER_NAME)
17314 : : {
17315 : : remove = true;
17316 : : break;
17317 : : }
17318 : : /* FALLTHRU */
17319 : 9270 : case OMP_CLAUSE_TO:
17320 : 9270 : case OMP_CLAUSE_FROM:
17321 : 9270 : if (OMP_CLAUSE_ITERATORS (c)
17322 : 9270 : && c_omp_finish_iterators (OMP_CLAUSE_ITERATORS (c)))
17323 : : {
17324 : 22609 : t = error_mark_node;
17325 : : break;
17326 : : }
17327 : : /* FALLTHRU */
17328 : 9406 : case OMP_CLAUSE__CACHE_:
17329 : 9406 : {
17330 : 9406 : using namespace omp_addr_tokenizer;
17331 : 9406 : auto_vec<omp_addr_token *, 10> addr_tokens;
17332 : :
17333 : 9406 : t = OMP_CLAUSE_DECL (c);
17334 : 9406 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17335 : : {
17336 : 1993 : grp_start_p = pc;
17337 : 1993 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17338 : :
17339 : 1993 : if (handle_omp_array_sections (c, ort))
17340 : : remove = true;
17341 : : else
17342 : : {
17343 : 1853 : t = OMP_CLAUSE_DECL (c);
17344 : 1853 : if (!omp_mappable_type (TREE_TYPE (t)))
17345 : : {
17346 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17347 : : "array section does not have mappable type "
17348 : : "in %qs clause",
17349 : 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17350 : 1 : remove = true;
17351 : : }
17352 : 1852 : else if (TYPE_ATOMIC (TREE_TYPE (t)))
17353 : : {
17354 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17355 : : "%<_Atomic%> %qE in %qs clause", t,
17356 : 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17357 : 1 : remove = true;
17358 : : }
17359 : 2496 : while (TREE_CODE (t) == ARRAY_REF)
17360 : 643 : t = TREE_OPERAND (t, 0);
17361 : :
17362 : 1853 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17363 : :
17364 : 1853 : if (!omp_parse_expr (addr_tokens, t))
17365 : : {
17366 : 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17367 : : "unsupported map expression %qE",
17368 : 0 : OMP_CLAUSE_DECL (c));
17369 : 0 : remove = true;
17370 : 0 : break;
17371 : : }
17372 : :
17373 : : /* This check is to determine if this will be the only map
17374 : : node created for this clause. Otherwise, we'll check
17375 : : the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
17376 : : node on the next iteration(s) of the loop. */
17377 : 3688 : if (addr_tokens.length () >= 4
17378 : 304 : && addr_tokens[0]->type == STRUCTURE_BASE
17379 : 302 : && addr_tokens[0]->u.structure_base_kind == BASE_DECL
17380 : 302 : && addr_tokens[1]->type == ACCESS_METHOD
17381 : 302 : && addr_tokens[2]->type == COMPONENT_SELECTOR
17382 : 302 : && addr_tokens[3]->type == ACCESS_METHOD
17383 : 2094 : && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
17384 : 188 : || (addr_tokens[3]->u.access_kind
17385 : : == ACCESS_INDEXED_ARRAY)))
17386 : : {
17387 : 55 : tree rt = addr_tokens[1]->expr;
17388 : :
17389 : 55 : gcc_assert (DECL_P (rt));
17390 : :
17391 : 55 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17392 : 50 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17393 : 55 : && (bitmap_bit_p (&map_head, DECL_UID (rt))
17394 : 0 : || bitmap_bit_p (&map_field_head, DECL_UID (rt))
17395 : 0 : || bitmap_bit_p (&map_firstprivate_head,
17396 : 0 : DECL_UID (rt))))
17397 : : {
17398 : : remove = true;
17399 : : break;
17400 : : }
17401 : 55 : if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
17402 : : break;
17403 : 37 : if (bitmap_bit_p (&map_head, DECL_UID (rt)))
17404 : : {
17405 : 0 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17406 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17407 : : "%qD appears more than once in motion "
17408 : : "clauses", rt);
17409 : 0 : else if (openacc)
17410 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17411 : : "%qD appears more than once in data "
17412 : : "clauses", rt);
17413 : : else
17414 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17415 : : "%qD appears more than once in map "
17416 : : "clauses", rt);
17417 : : remove = true;
17418 : : }
17419 : : else
17420 : : {
17421 : 37 : bitmap_set_bit (&map_head, DECL_UID (rt));
17422 : 37 : bitmap_set_bit (&map_field_head, DECL_UID (rt));
17423 : : }
17424 : : }
17425 : 1853 : }
17426 : 1975 : if (c_oacc_check_attachments (c))
17427 : 2 : remove = true;
17428 : 1975 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17429 : 1799 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17430 : 1781 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17431 : 2000 : && !OMP_CLAUSE_SIZE (c))
17432 : : /* In this case, we have a single array element which is a
17433 : : pointer, and we already set OMP_CLAUSE_SIZE in
17434 : : handle_omp_array_sections above. For attach/detach
17435 : : clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
17436 : : to zero here. */
17437 : 10 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17438 : : break;
17439 : : }
17440 : 7413 : else if (!omp_parse_expr (addr_tokens, t))
17441 : : {
17442 : 0 : sorry_at (OMP_CLAUSE_LOCATION (c),
17443 : : "unsupported map expression %qE",
17444 : 0 : OMP_CLAUSE_DECL (c));
17445 : 0 : remove = true;
17446 : 0 : break;
17447 : : }
17448 : 7413 : if (t == error_mark_node)
17449 : : {
17450 : : remove = true;
17451 : : break;
17452 : : }
17453 : : /* OpenACC attach / detach clauses must be pointers. */
17454 : 7407 : if (c_oacc_check_attachments (c))
17455 : : {
17456 : : remove = true;
17457 : : break;
17458 : : }
17459 : 7403 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17460 : 4584 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
17461 : 4557 : || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
17462 : 7445 : && !OMP_CLAUSE_SIZE (c))
17463 : : /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
17464 : : bias) to zero here, so it is not set erroneously to the pointer
17465 : : size later on in gimplify.cc. */
17466 : 28 : OMP_CLAUSE_SIZE (c) = size_zero_node;
17467 : :
17468 : 7403 : c_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
17469 : :
17470 : 7403 : if (!ai.check_clause (c))
17471 : : {
17472 : : remove = true;
17473 : : break;
17474 : : }
17475 : :
17476 : 7395 : if (!ai.map_supported_p ())
17477 : : {
17478 : 14 : sorry_at (OMP_CLAUSE_LOCATION (c),
17479 : : "unsupported map expression %qE",
17480 : 7 : OMP_CLAUSE_DECL (c));
17481 : 7 : remove = true;
17482 : 7 : break;
17483 : : }
17484 : :
17485 : 14776 : gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
17486 : : || addr_tokens[0]->type == STRUCTURE_BASE)
17487 : : && addr_tokens[1]->type == ACCESS_METHOD);
17488 : :
17489 : 7388 : t = addr_tokens[1]->expr;
17490 : :
17491 : 7388 : if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
17492 : 0 : goto skip_decl_checks;
17493 : :
17494 : : /* For OpenMP, we can access a struct "t" and "t.d" on the same
17495 : : mapping. OpenACC allows multiple fields of the same structure
17496 : : to be written. */
17497 : 7388 : if (addr_tokens[0]->type == STRUCTURE_BASE
17498 : 7388 : && (bitmap_bit_p (&map_field_head, DECL_UID (t))
17499 : 238 : || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
17500 : 246 : goto skip_decl_checks;
17501 : :
17502 : 7142 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17503 : : {
17504 : 3 : if (ort != C_ORT_ACC && EXPR_P (t))
17505 : : break;
17506 : :
17507 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17508 : : "%qE is not a variable in %qs clause", t,
17509 : 3 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17510 : 3 : remove = true;
17511 : : }
17512 : 7139 : else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17513 : : {
17514 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17515 : : "%qD is threadprivate variable in %qs clause", t,
17516 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17517 : 0 : remove = true;
17518 : : }
17519 : 7139 : else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17520 : 4327 : || (OMP_CLAUSE_MAP_KIND (c)
17521 : : != GOMP_MAP_FIRSTPRIVATE_POINTER))
17522 : 10430 : && !c_mark_addressable (t))
17523 : : remove = true;
17524 : 7139 : else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17525 : 4327 : && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
17526 : 4327 : || (OMP_CLAUSE_MAP_KIND (c)
17527 : : == GOMP_MAP_FIRSTPRIVATE_POINTER)
17528 : 3291 : || (OMP_CLAUSE_MAP_KIND (c)
17529 : : == GOMP_MAP_FORCE_DEVICEPTR)))
17530 : 6045 : && t == OMP_CLAUSE_DECL (c)
17531 : 12948 : && !omp_mappable_type (TREE_TYPE (t)))
17532 : : {
17533 : 16 : error_at (OMP_CLAUSE_LOCATION (c),
17534 : : "%qD does not have a mappable type in %qs clause", t,
17535 : 8 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17536 : 8 : remove = true;
17537 : : }
17538 : 7131 : else if (TREE_TYPE (t) == error_mark_node)
17539 : : remove = true;
17540 : 7130 : else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
17541 : : {
17542 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
17543 : : "%<_Atomic%> %qE in %qs clause", t,
17544 : 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17545 : 4 : remove = true;
17546 : : }
17547 : 7126 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17548 : 4317 : && OMP_CLAUSE_MAP_IMPLICIT (c)
17549 : 7290 : && (bitmap_bit_p (&map_head, DECL_UID (t))
17550 : 128 : || bitmap_bit_p (&map_field_head, DECL_UID (t))
17551 : 128 : || bitmap_bit_p (&map_firstprivate_head,
17552 : 128 : DECL_UID (t))))
17553 : : remove = true;
17554 : 7089 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
17555 : 7089 : && (OMP_CLAUSE_MAP_KIND (c)
17556 : : == GOMP_MAP_FIRSTPRIVATE_POINTER))
17557 : : {
17558 : 1034 : if (bitmap_bit_p (&generic_head, DECL_UID (t))
17559 : 1034 : || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17560 : 2067 : || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
17561 : : {
17562 : 5 : error_at (OMP_CLAUSE_LOCATION (c),
17563 : : "%qD appears more than once in data clauses", t);
17564 : 5 : remove = true;
17565 : : }
17566 : 1029 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17567 : 4 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17568 : 1031 : && openacc)
17569 : : {
17570 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17571 : : "%qD appears more than once in data clauses", t);
17572 : 1 : remove = true;
17573 : : }
17574 : : else
17575 : 1028 : bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
17576 : : }
17577 : 6055 : else if (bitmap_bit_p (&map_head, DECL_UID (t))
17578 : 59 : && !bitmap_bit_p (&map_field_head, DECL_UID (t))
17579 : 13 : && ort != C_ORT_OMP
17580 : 6068 : && ort != C_ORT_OMP_EXIT_DATA)
17581 : : {
17582 : 10 : if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
17583 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17584 : : "%qD appears more than once in motion clauses", t);
17585 : 10 : else if (openacc)
17586 : 7 : error_at (OMP_CLAUSE_LOCATION (c),
17587 : : "%qD appears more than once in data clauses", t);
17588 : : else
17589 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17590 : : "%qD appears more than once in map clauses", t);
17591 : : remove = true;
17592 : : }
17593 : 6045 : else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
17594 : : {
17595 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17596 : : "%qD appears more than once in data clauses", t);
17597 : 0 : remove = true;
17598 : : }
17599 : 6045 : else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
17600 : 6045 : || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
17601 : : {
17602 : 9 : if (openacc)
17603 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17604 : : "%qD appears more than once in data clauses", t);
17605 : : else
17606 : 9 : error_at (OMP_CLAUSE_LOCATION (c),
17607 : : "%qD appears both in data and map clauses", t);
17608 : : remove = true;
17609 : : }
17610 : 6036 : else if (!omp_access_chain_p (addr_tokens, 1))
17611 : : {
17612 : 6036 : bitmap_set_bit (&map_head, DECL_UID (t));
17613 : 6036 : if (t != OMP_CLAUSE_DECL (c)
17614 : 6036 : && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
17615 : 232 : bitmap_set_bit (&map_field_head, DECL_UID (t));
17616 : : }
17617 : :
17618 : 0 : skip_decl_checks:
17619 : : /* If we call omp_expand_map_clause in handle_omp_array_sections,
17620 : : the containing loop (here) iterates through the new nodes
17621 : : created by that expansion. Avoid expanding those again (just
17622 : : by checking the node type). */
17623 : 7388 : if (!remove
17624 : 7388 : && ort != C_ORT_DECLARE_SIMD
17625 : 7388 : && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
17626 : 4495 : || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
17627 : 3467 : && (OMP_CLAUSE_MAP_KIND (c)
17628 : : != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
17629 : 3467 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
17630 : 3467 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
17631 : 3168 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
17632 : 3141 : && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
17633 : : {
17634 : 5941 : grp_start_p = pc;
17635 : 5941 : grp_sentinel = OMP_CLAUSE_CHAIN (c);
17636 : 5941 : tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
17637 : : addr_tokens, ort);
17638 : 5941 : if (nc != error_mark_node)
17639 : 5941 : c = nc;
17640 : : }
17641 : 9421 : }
17642 : 7388 : break;
17643 : :
17644 : 223 : case OMP_CLAUSE_ENTER:
17645 : 223 : case OMP_CLAUSE_LINK:
17646 : 223 : t = OMP_CLAUSE_DECL (c);
17647 : 223 : const char *cname;
17648 : 223 : cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
17649 : 223 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
17650 : 223 : && OMP_CLAUSE_ENTER_TO (c))
17651 : : cname = "to";
17652 : 223 : if (TREE_CODE (t) == FUNCTION_DECL
17653 : 223 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17654 : : ;
17655 : 145 : else if (!VAR_P (t))
17656 : : {
17657 : 1 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
17658 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17659 : : "%qE is neither a variable nor a function name in "
17660 : : "clause %qs", t, cname);
17661 : : else
17662 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17663 : : "%qE is not a variable in clause %qs", t, cname);
17664 : : remove = true;
17665 : : }
17666 : 144 : else if (DECL_THREAD_LOCAL_P (t))
17667 : : {
17668 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17669 : : "%qD is threadprivate variable in %qs clause", t,
17670 : : cname);
17671 : 2 : remove = true;
17672 : : }
17673 : 142 : else if (!omp_mappable_type (TREE_TYPE (t)))
17674 : : {
17675 : 6 : error_at (OMP_CLAUSE_LOCATION (c),
17676 : : "%qD does not have a mappable type in %qs clause", t,
17677 : : cname);
17678 : 6 : remove = true;
17679 : : }
17680 : 8 : if (remove)
17681 : : break;
17682 : 214 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17683 : : {
17684 : 8 : error_at (OMP_CLAUSE_LOCATION (c),
17685 : : "%qE appears more than once on the same "
17686 : : "%<declare target%> directive", t);
17687 : 8 : remove = true;
17688 : : }
17689 : : else
17690 : 206 : bitmap_set_bit (&generic_head, DECL_UID (t));
17691 : : break;
17692 : :
17693 : 117 : case OMP_CLAUSE_UNIFORM:
17694 : 117 : t = OMP_CLAUSE_DECL (c);
17695 : 117 : if (TREE_CODE (t) != PARM_DECL)
17696 : : {
17697 : 0 : if (DECL_P (t))
17698 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17699 : : "%qD is not an argument in %<uniform%> clause", t);
17700 : : else
17701 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17702 : : "%qE is not an argument in %<uniform%> clause", t);
17703 : : remove = true;
17704 : : break;
17705 : : }
17706 : : /* map_head bitmap is used as uniform_head if declare_simd. */
17707 : 117 : bitmap_set_bit (&map_head, DECL_UID (t));
17708 : 117 : goto check_dup_generic;
17709 : :
17710 : 160 : case OMP_CLAUSE_IS_DEVICE_PTR:
17711 : 160 : case OMP_CLAUSE_USE_DEVICE_PTR:
17712 : 160 : t = OMP_CLAUSE_DECL (c);
17713 : 160 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
17714 : 122 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
17715 : 160 : if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
17716 : : {
17717 : 21 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
17718 : 21 : && !openacc)
17719 : : {
17720 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17721 : : "%qs variable is not a pointer",
17722 : 1 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17723 : 1 : remove = true;
17724 : : }
17725 : 20 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
17726 : : {
17727 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
17728 : : "%qs variable is neither a pointer nor an array",
17729 : 4 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17730 : 4 : remove = true;
17731 : : }
17732 : : }
17733 : 160 : goto check_dup_generic;
17734 : :
17735 : 88 : case OMP_CLAUSE_HAS_DEVICE_ADDR:
17736 : 88 : t = OMP_CLAUSE_DECL (c);
17737 : 88 : if (TREE_CODE (t) == OMP_ARRAY_SECTION)
17738 : : {
17739 : 11 : if (handle_omp_array_sections (c, ort))
17740 : : remove = true;
17741 : : else
17742 : : {
17743 : 11 : t = OMP_CLAUSE_DECL (c);
17744 : 24 : while (TREE_CODE (t) == ARRAY_REF)
17745 : 13 : t = TREE_OPERAND (t, 0);
17746 : : }
17747 : : }
17748 : 88 : bitmap_set_bit (&is_on_device_head, DECL_UID (t));
17749 : 88 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17750 : 88 : c_mark_addressable (t);
17751 : 88 : goto check_dup_generic_t;
17752 : :
17753 : 36 : case OMP_CLAUSE_USE_DEVICE_ADDR:
17754 : 36 : t = OMP_CLAUSE_DECL (c);
17755 : 36 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
17756 : 36 : c_mark_addressable (t);
17757 : 36 : goto check_dup_generic;
17758 : :
17759 : 4790 : case OMP_CLAUSE_NOWAIT:
17760 : 4790 : if (copyprivate_seen)
17761 : : {
17762 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17763 : : "%<nowait%> clause must not be used together "
17764 : : "with %<copyprivate%> clause");
17765 : 1 : remove = true;
17766 : 1 : break;
17767 : : }
17768 : 4789 : nowait_clause = pc;
17769 : 4789 : pc = &OMP_CLAUSE_CHAIN (c);
17770 : 4789 : continue;
17771 : :
17772 : 522 : case OMP_CLAUSE_ORDER:
17773 : 522 : if (ordered_clause)
17774 : : {
17775 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
17776 : : "%<order%> clause must not be used together "
17777 : : "with %<ordered%> clause");
17778 : 2 : remove = true;
17779 : 2 : break;
17780 : : }
17781 : 520 : else if (order_clause)
17782 : : {
17783 : : /* Silently remove duplicates. */
17784 : : remove = true;
17785 : : break;
17786 : : }
17787 : 512 : order_clause = pc;
17788 : 512 : pc = &OMP_CLAUSE_CHAIN (c);
17789 : 512 : continue;
17790 : :
17791 : 32 : case OMP_CLAUSE_DETACH:
17792 : 32 : t = OMP_CLAUSE_DECL (c);
17793 : 32 : if (detach_seen)
17794 : : {
17795 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17796 : : "too many %qs clauses on a task construct",
17797 : : "detach");
17798 : 1 : remove = true;
17799 : 1 : break;
17800 : : }
17801 : 31 : detach_seen = pc;
17802 : 31 : pc = &OMP_CLAUSE_CHAIN (c);
17803 : 31 : c_mark_addressable (t);
17804 : 31 : continue;
17805 : :
17806 : 14518 : case OMP_CLAUSE_IF:
17807 : 14518 : case OMP_CLAUSE_SELF:
17808 : 14518 : case OMP_CLAUSE_NUM_THREADS:
17809 : 14518 : case OMP_CLAUSE_NUM_TEAMS:
17810 : 14518 : case OMP_CLAUSE_THREAD_LIMIT:
17811 : 14518 : case OMP_CLAUSE_DEFAULT:
17812 : 14518 : case OMP_CLAUSE_UNTIED:
17813 : 14518 : case OMP_CLAUSE_COLLAPSE:
17814 : 14518 : case OMP_CLAUSE_FINAL:
17815 : 14518 : case OMP_CLAUSE_DEVICE:
17816 : 14518 : case OMP_CLAUSE_DIST_SCHEDULE:
17817 : 14518 : case OMP_CLAUSE_PARALLEL:
17818 : 14518 : case OMP_CLAUSE_FOR:
17819 : 14518 : case OMP_CLAUSE_SECTIONS:
17820 : 14518 : case OMP_CLAUSE_TASKGROUP:
17821 : 14518 : case OMP_CLAUSE_PROC_BIND:
17822 : 14518 : case OMP_CLAUSE_DEVICE_TYPE:
17823 : 14518 : case OMP_CLAUSE_PRIORITY:
17824 : 14518 : case OMP_CLAUSE_THREADS:
17825 : 14518 : case OMP_CLAUSE_SIMD:
17826 : 14518 : case OMP_CLAUSE_HINT:
17827 : 14518 : case OMP_CLAUSE_FILTER:
17828 : 14518 : case OMP_CLAUSE_DEFAULTMAP:
17829 : 14518 : case OMP_CLAUSE_BIND:
17830 : 14518 : case OMP_CLAUSE_NUM_GANGS:
17831 : 14518 : case OMP_CLAUSE_NUM_WORKERS:
17832 : 14518 : case OMP_CLAUSE_VECTOR_LENGTH:
17833 : 14518 : case OMP_CLAUSE_ASYNC:
17834 : 14518 : case OMP_CLAUSE_WAIT:
17835 : 14518 : case OMP_CLAUSE_AUTO:
17836 : 14518 : case OMP_CLAUSE_INDEPENDENT:
17837 : 14518 : case OMP_CLAUSE_SEQ:
17838 : 14518 : case OMP_CLAUSE_GANG:
17839 : 14518 : case OMP_CLAUSE_WORKER:
17840 : 14518 : case OMP_CLAUSE_VECTOR:
17841 : 14518 : case OMP_CLAUSE_TILE:
17842 : 14518 : case OMP_CLAUSE_IF_PRESENT:
17843 : 14518 : case OMP_CLAUSE_FINALIZE:
17844 : 14518 : case OMP_CLAUSE_NOHOST:
17845 : 14518 : case OMP_CLAUSE_INDIRECT:
17846 : 14518 : case OMP_CLAUSE_NOVARIANTS:
17847 : 14518 : case OMP_CLAUSE_NOCONTEXT:
17848 : 14518 : pc = &OMP_CLAUSE_CHAIN (c);
17849 : 14518 : continue;
17850 : :
17851 : 62 : case OMP_CLAUSE_GRAINSIZE:
17852 : 62 : grainsize_seen = pc;
17853 : 62 : pc = &OMP_CLAUSE_CHAIN (c);
17854 : 62 : continue;
17855 : :
17856 : 49 : case OMP_CLAUSE_NUM_TASKS:
17857 : 49 : num_tasks_seen = true;
17858 : 49 : pc = &OMP_CLAUSE_CHAIN (c);
17859 : 49 : continue;
17860 : :
17861 : 78 : case OMP_CLAUSE_MERGEABLE:
17862 : 78 : mergeable_seen = true;
17863 : 78 : pc = &OMP_CLAUSE_CHAIN (c);
17864 : 78 : continue;
17865 : :
17866 : 18 : case OMP_CLAUSE_NOGROUP:
17867 : 18 : nogroup_seen = pc;
17868 : 18 : pc = &OMP_CLAUSE_CHAIN (c);
17869 : 18 : continue;
17870 : :
17871 : 3463 : case OMP_CLAUSE_SCHEDULE:
17872 : 3463 : schedule_clause = c;
17873 : 3463 : pc = &OMP_CLAUSE_CHAIN (c);
17874 : 3463 : continue;
17875 : :
17876 : 303 : case OMP_CLAUSE_ORDERED:
17877 : 303 : ordered_clause = c;
17878 : 303 : if (order_clause)
17879 : : {
17880 : 2 : error_at (OMP_CLAUSE_LOCATION (*order_clause),
17881 : : "%<order%> clause must not be used together "
17882 : : "with %<ordered%> clause");
17883 : 2 : *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
17884 : 2 : order_clause = NULL;
17885 : : }
17886 : 303 : pc = &OMP_CLAUSE_CHAIN (c);
17887 : 303 : continue;
17888 : :
17889 : 223 : case OMP_CLAUSE_SAFELEN:
17890 : 223 : safelen = c;
17891 : 223 : pc = &OMP_CLAUSE_CHAIN (c);
17892 : 223 : continue;
17893 : 313 : case OMP_CLAUSE_SIMDLEN:
17894 : 313 : simdlen = c;
17895 : 313 : pc = &OMP_CLAUSE_CHAIN (c);
17896 : 313 : continue;
17897 : :
17898 : 69 : case OMP_CLAUSE_FULL:
17899 : 69 : full_seen = pc;
17900 : 69 : pc = &OMP_CLAUSE_CHAIN (c);
17901 : 69 : continue;
17902 : :
17903 : 223 : case OMP_CLAUSE_PARTIAL:
17904 : 223 : partial_seen = true;
17905 : 223 : pc = &OMP_CLAUSE_CHAIN (c);
17906 : 223 : continue;
17907 : :
17908 : 0 : case OMP_CLAUSE_SIZES:
17909 : 0 : pc = &OMP_CLAUSE_CHAIN (c);
17910 : 0 : continue;
17911 : :
17912 : 199 : case OMP_CLAUSE_INBRANCH:
17913 : 199 : case OMP_CLAUSE_NOTINBRANCH:
17914 : 199 : if (branch_seen)
17915 : : {
17916 : 1 : error_at (OMP_CLAUSE_LOCATION (c),
17917 : : "%<inbranch%> clause is incompatible with "
17918 : : "%<notinbranch%>");
17919 : 1 : remove = true;
17920 : 1 : break;
17921 : : }
17922 : 198 : branch_seen = true;
17923 : 198 : pc = &OMP_CLAUSE_CHAIN (c);
17924 : 198 : continue;
17925 : :
17926 : 367 : case OMP_CLAUSE_INCLUSIVE:
17927 : 367 : case OMP_CLAUSE_EXCLUSIVE:
17928 : 367 : need_complete = true;
17929 : 367 : need_implicitly_determined = true;
17930 : 367 : t = OMP_CLAUSE_DECL (c);
17931 : 367 : if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
17932 : : {
17933 : 0 : error_at (OMP_CLAUSE_LOCATION (c),
17934 : : "%qE is not a variable in clause %qs", t,
17935 : 0 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
17936 : 0 : remove = true;
17937 : : }
17938 : : break;
17939 : :
17940 : 152 : case OMP_CLAUSE_INIT:
17941 : 152 : init_seen = true;
17942 : 152 : if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
17943 : 71 : init_no_targetsync_clause = c;
17944 : : /* FALLTHRU */
17945 : 242 : case OMP_CLAUSE_DESTROY:
17946 : 242 : case OMP_CLAUSE_USE:
17947 : 242 : init_use_destroy_seen = true;
17948 : 242 : t = OMP_CLAUSE_DECL (c);
17949 : 242 : if (bitmap_bit_p (&generic_head, DECL_UID (t)))
17950 : : {
17951 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
17952 : : "%qD appears more than once in action clauses", t);
17953 : 3 : remove = true;
17954 : 3 : break;
17955 : : }
17956 : 239 : bitmap_set_bit (&generic_head, DECL_UID (t));
17957 : : /* FALLTHRU */
17958 : 298 : case OMP_CLAUSE_INTEROP:
17959 : 298 : if (/* ort == C_ORT_OMP_INTEROP [uncomment for depobj init] */
17960 : 298 : !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
17961 : : {
17962 : 40 : error_at (OMP_CLAUSE_LOCATION (c),
17963 : : "%qD must be of %<omp_interop_t%>",
17964 : 20 : OMP_CLAUSE_DECL (c));
17965 : 20 : remove = true;
17966 : : }
17967 : 278 : else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
17968 : 133 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
17969 : 318 : && TREE_READONLY (OMP_CLAUSE_DECL (c)))
17970 : : {
17971 : 12 : error_at (OMP_CLAUSE_LOCATION (c),
17972 : 6 : "%qD shall not be const", OMP_CLAUSE_DECL (c));
17973 : 6 : remove = true;
17974 : : }
17975 : 298 : pc = &OMP_CLAUSE_CHAIN (c);
17976 : 298 : break;
17977 : 0 : default:
17978 : 0 : gcc_unreachable ();
17979 : 24849 : }
17980 : :
17981 : 22609 : if (!remove)
17982 : : {
17983 : 22049 : t = OMP_CLAUSE_DECL (c);
17984 : :
17985 : 22049 : if (need_complete)
17986 : : {
17987 : 3930 : t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
17988 : 3930 : if (t == error_mark_node)
17989 : 7 : remove = true;
17990 : : }
17991 : :
17992 : 22049 : if (need_implicitly_determined)
17993 : : {
17994 : 9523 : const char *share_name = NULL;
17995 : :
17996 : 9523 : if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
17997 : : share_name = "threadprivate";
17998 : 9517 : else switch (c_omp_predetermined_sharing (t))
17999 : : {
18000 : : case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
18001 : : break;
18002 : 20 : case OMP_CLAUSE_DEFAULT_SHARED:
18003 : 20 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18004 : 12 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
18005 : 28 : && c_omp_predefined_variable (t))
18006 : : /* The __func__ variable and similar function-local
18007 : : predefined variables may be listed in a shared or
18008 : : firstprivate clause. */
18009 : : break;
18010 : : share_name = "shared";
18011 : : break;
18012 : : case OMP_CLAUSE_DEFAULT_PRIVATE:
18013 : : share_name = "private";
18014 : : break;
18015 : 0 : default:
18016 : 0 : gcc_unreachable ();
18017 : : }
18018 : : if (share_name)
18019 : : {
18020 : 20 : error_at (OMP_CLAUSE_LOCATION (c),
18021 : : "%qE is predetermined %qs for %qs",
18022 : : t, share_name,
18023 : 10 : omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
18024 : 10 : remove = true;
18025 : : }
18026 : 9513 : else if (TREE_READONLY (t)
18027 : 23 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
18028 : 9526 : && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
18029 : : {
18030 : 4 : error_at (OMP_CLAUSE_LOCATION (c),
18031 : : "%<const%> qualified %qE may appear only in "
18032 : : "%<shared%> or %<firstprivate%> clauses", t);
18033 : 4 : remove = true;
18034 : : }
18035 : : }
18036 : : }
18037 : :
18038 : 22049 : if (remove)
18039 : : {
18040 : 581 : if (grp_start_p)
18041 : : {
18042 : : /* If we found a clause to remove, we want to remove the whole
18043 : : expanded group, otherwise gimplify
18044 : : (omp_resolve_clause_dependencies) can get confused. */
18045 : 154 : *grp_start_p = grp_sentinel;
18046 : 154 : pc = grp_start_p;
18047 : 154 : grp_start_p = NULL;
18048 : : }
18049 : : else
18050 : 427 : *pc = OMP_CLAUSE_CHAIN (c);
18051 : : }
18052 : : else
18053 : 22028 : pc = &OMP_CLAUSE_CHAIN (c);
18054 : : }
18055 : :
18056 : 29212 : if (grp_start_p
18057 : 29212 : && OMP_CLAUSE_HAS_ITERATORS (*grp_start_p))
18058 : 49 : for (tree gc = *grp_start_p; gc; gc = OMP_CLAUSE_CHAIN (gc))
18059 : 30 : OMP_CLAUSE_ITERATORS (gc) = OMP_CLAUSE_ITERATORS (*grp_start_p);
18060 : :
18061 : 29212 : if (simdlen
18062 : 29212 : && safelen
18063 : 29329 : && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
18064 : 117 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
18065 : : {
18066 : 0 : error_at (OMP_CLAUSE_LOCATION (simdlen),
18067 : : "%<simdlen%> clause value is bigger than "
18068 : : "%<safelen%> clause value");
18069 : 0 : OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
18070 : 0 : = OMP_CLAUSE_SAFELEN_EXPR (safelen);
18071 : : }
18072 : :
18073 : 29212 : if (ordered_clause
18074 : 29212 : && schedule_clause
18075 : 29212 : && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18076 : : & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
18077 : : {
18078 : 7 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18079 : : "%<nonmonotonic%> schedule modifier specified together "
18080 : : "with %<ordered%> clause");
18081 : 14 : OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18082 : 7 : = (enum omp_clause_schedule_kind)
18083 : 7 : (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
18084 : : & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
18085 : : }
18086 : :
18087 : 29212 : if (reduction_seen < 0 && ordered_clause)
18088 : : {
18089 : 2 : error_at (OMP_CLAUSE_LOCATION (ordered_clause),
18090 : : "%qs clause specified together with %<inscan%> "
18091 : : "%<reduction%> clause", "ordered");
18092 : 2 : reduction_seen = -2;
18093 : : }
18094 : :
18095 : 29212 : if (reduction_seen < 0 && schedule_clause)
18096 : : {
18097 : 3 : error_at (OMP_CLAUSE_LOCATION (schedule_clause),
18098 : : "%qs clause specified together with %<inscan%> "
18099 : : "%<reduction%> clause", "schedule");
18100 : 3 : reduction_seen = -2;
18101 : : }
18102 : :
18103 : 29212 : if (linear_variable_step_check
18104 : 29212 : || reduction_seen == -2
18105 : : || allocate_seen
18106 : 29200 : || target_in_reduction_seen)
18107 : 5145 : for (pc = &clauses, c = clauses; c ; c = *pc)
18108 : : {
18109 : 4556 : bool remove = false;
18110 : 4556 : if (allocate_seen)
18111 : 4398 : switch (OMP_CLAUSE_CODE (c))
18112 : : {
18113 : 430 : case OMP_CLAUSE_REDUCTION:
18114 : 430 : case OMP_CLAUSE_IN_REDUCTION:
18115 : 430 : case OMP_CLAUSE_TASK_REDUCTION:
18116 : 430 : if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
18117 : : {
18118 : 23 : t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
18119 : 23 : if (TREE_CODE (t) == POINTER_PLUS_EXPR)
18120 : 0 : t = TREE_OPERAND (t, 0);
18121 : 23 : if (TREE_CODE (t) == ADDR_EXPR
18122 : 10 : || INDIRECT_REF_P (t))
18123 : 13 : t = TREE_OPERAND (t, 0);
18124 : 23 : if (DECL_P (t))
18125 : 23 : bitmap_clear_bit (&aligned_head, DECL_UID (t));
18126 : : break;
18127 : : }
18128 : : /* FALLTHRU */
18129 : 1302 : case OMP_CLAUSE_PRIVATE:
18130 : 1302 : case OMP_CLAUSE_FIRSTPRIVATE:
18131 : 1302 : case OMP_CLAUSE_LASTPRIVATE:
18132 : 1302 : case OMP_CLAUSE_LINEAR:
18133 : 1302 : if (DECL_P (OMP_CLAUSE_DECL (c)))
18134 : 2604 : bitmap_clear_bit (&aligned_head,
18135 : 1302 : DECL_UID (OMP_CLAUSE_DECL (c)));
18136 : : break;
18137 : : default:
18138 : : break;
18139 : : }
18140 : 4556 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
18141 : 29 : && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
18142 : 4563 : && !bitmap_bit_p (&map_head,
18143 : 7 : DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
18144 : : {
18145 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
18146 : : "%<linear%> clause step is a parameter %qD not "
18147 : : "specified in %<uniform%> clause",
18148 : 2 : OMP_CLAUSE_LINEAR_STEP (c));
18149 : 2 : remove = true;
18150 : : }
18151 : 4554 : else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
18152 : 4554 : && reduction_seen == -2)
18153 : 9 : OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
18154 : 4556 : if (target_in_reduction_seen
18155 : 4556 : && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
18156 : : {
18157 : 256 : tree t = OMP_CLAUSE_DECL (c);
18158 : 256 : while (handled_component_p (t)
18159 : : || INDIRECT_REF_P (t)
18160 : : || TREE_CODE (t) == ADDR_EXPR
18161 : : || TREE_CODE (t) == MEM_REF
18162 : 288 : || TREE_CODE (t) == NON_LVALUE_EXPR)
18163 : 32 : t = TREE_OPERAND (t, 0);
18164 : 256 : if (DECL_P (t)
18165 : 256 : && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
18166 : 138 : OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
18167 : : }
18168 : :
18169 : 4556 : if (remove)
18170 : 2 : *pc = OMP_CLAUSE_CHAIN (c);
18171 : : else
18172 : 4554 : pc = &OMP_CLAUSE_CHAIN (c);
18173 : : }
18174 : :
18175 : 589 : if (allocate_seen)
18176 : 4961 : for (pc = &clauses, c = clauses; c ; c = *pc)
18177 : : {
18178 : 4398 : bool remove = false;
18179 : 4398 : if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
18180 : 704 : && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
18181 : 5082 : && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
18182 : : {
18183 : 3 : error_at (OMP_CLAUSE_LOCATION (c),
18184 : : "%qD specified in %<allocate%> clause but not in "
18185 : 3 : "an explicit privatization clause", OMP_CLAUSE_DECL (c));
18186 : 3 : remove = true;
18187 : : }
18188 : 4398 : if (remove)
18189 : 3 : *pc = OMP_CLAUSE_CHAIN (c);
18190 : : else
18191 : 4395 : pc = &OMP_CLAUSE_CHAIN (c);
18192 : : }
18193 : :
18194 : 29212 : if (nogroup_seen && reduction_seen)
18195 : : {
18196 : 1 : error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
18197 : : "%<nogroup%> clause must not be used together with "
18198 : : "%<reduction%> clause");
18199 : 1 : *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
18200 : : }
18201 : :
18202 : 29212 : if (grainsize_seen && num_tasks_seen)
18203 : : {
18204 : 2 : error_at (OMP_CLAUSE_LOCATION (*grainsize_seen),
18205 : : "%<grainsize%> clause must not be used together with "
18206 : : "%<num_tasks%> clause");
18207 : 2 : *grainsize_seen = OMP_CLAUSE_CHAIN (*grainsize_seen);
18208 : : }
18209 : :
18210 : 29212 : if (full_seen && partial_seen)
18211 : : {
18212 : 4 : error_at (OMP_CLAUSE_LOCATION (*full_seen),
18213 : : "%<full%> clause must not be used together with "
18214 : : "%<partial%> clause");
18215 : 4 : *full_seen = OMP_CLAUSE_CHAIN (*full_seen);
18216 : : }
18217 : :
18218 : 29212 : if (detach_seen)
18219 : : {
18220 : 31 : if (mergeable_seen)
18221 : : {
18222 : 2 : error_at (OMP_CLAUSE_LOCATION (*detach_seen),
18223 : : "%<detach%> clause must not be used together with "
18224 : : "%<mergeable%> clause");
18225 : 2 : *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
18226 : : }
18227 : : else
18228 : : {
18229 : 29 : tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
18230 : :
18231 : 79 : for (pc = &clauses, c = clauses; c ; c = *pc)
18232 : : {
18233 : 50 : bool remove = false;
18234 : 50 : if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
18235 : 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
18236 : 48 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
18237 : 44 : || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
18238 : 54 : && OMP_CLAUSE_DECL (c) == detach_decl)
18239 : : {
18240 : 2 : error_at (OMP_CLAUSE_LOCATION (c),
18241 : : "the event handle of a %<detach%> clause "
18242 : : "should not be in a data-sharing clause");
18243 : 2 : remove = true;
18244 : : }
18245 : 50 : if (remove)
18246 : 2 : *pc = OMP_CLAUSE_CHAIN (c);
18247 : : else
18248 : 48 : pc = &OMP_CLAUSE_CHAIN (c);
18249 : : }
18250 : : }
18251 : : }
18252 : :
18253 : 29212 : if (ort == C_ORT_OMP_INTEROP
18254 : 29212 : && depend_clause
18255 : 21 : && (!init_use_destroy_seen
18256 : 20 : || (init_seen && init_no_targetsync_clause)))
18257 : : {
18258 : 3 : error_at (OMP_CLAUSE_LOCATION (depend_clause),
18259 : : "%<depend%> clause requires action clauses with "
18260 : : "%<targetsync%> interop-type");
18261 : 3 : if (init_no_targetsync_clause)
18262 : 2 : inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
18263 : : "%<init%> clause lacks the %<targetsync%> modifier");
18264 : : }
18265 : :
18266 : 29212 : bitmap_obstack_release (NULL);
18267 : 29212 : return clauses;
18268 : : }
18269 : :
18270 : : /* Do processing necessary to make CLAUSES well-formed, where CLAUSES result
18271 : : from implicit instantiation of user-defined mappers (in gimplify.cc). */
18272 : :
18273 : : tree
18274 : 13 : c_omp_finish_mapper_clauses (tree clauses)
18275 : : {
18276 : 13 : return c_finish_omp_clauses (clauses, C_ORT_OMP);
18277 : : }
18278 : :
18279 : : /* Return code to initialize DST with a copy constructor from SRC.
18280 : : C doesn't have copy constructors nor assignment operators, only for
18281 : : _Atomic vars we need to perform __atomic_load from src into a temporary
18282 : : followed by __atomic_store of the temporary to dst. */
18283 : :
18284 : : tree
18285 : 18720 : c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
18286 : : {
18287 : 18720 : if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
18288 : 18716 : return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
18289 : :
18290 : 4 : location_t loc = OMP_CLAUSE_LOCATION (clause);
18291 : 4 : tree type = TREE_TYPE (dst);
18292 : 4 : tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
18293 : 4 : tree tmp = create_tmp_var (nonatomic_type);
18294 : 4 : tree tmp_addr = build_fold_addr_expr (tmp);
18295 : 4 : TREE_ADDRESSABLE (tmp) = 1;
18296 : 4 : suppress_warning (tmp);
18297 : 4 : tree src_addr = build_fold_addr_expr (src);
18298 : 4 : tree dst_addr = build_fold_addr_expr (dst);
18299 : 4 : tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
18300 : 4 : vec<tree, va_gc> *params;
18301 : : /* Expansion of a generic atomic load may require an addition
18302 : : element, so allocate enough to prevent a resize. */
18303 : 4 : vec_alloc (params, 4);
18304 : :
18305 : : /* Build __atomic_load (&src, &tmp, SEQ_CST); */
18306 : 4 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
18307 : 4 : params->quick_push (src_addr);
18308 : 4 : params->quick_push (tmp_addr);
18309 : 4 : params->quick_push (seq_cst);
18310 : 4 : tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18311 : :
18312 : 4 : vec_alloc (params, 4);
18313 : :
18314 : : /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
18315 : 4 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
18316 : 4 : params->quick_push (dst_addr);
18317 : 4 : params->quick_push (tmp_addr);
18318 : 4 : params->quick_push (seq_cst);
18319 : 4 : tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
18320 : 4 : return build2 (COMPOUND_EXPR, void_type_node, load, store);
18321 : : }
18322 : :
18323 : : /* Create a transaction node. */
18324 : :
18325 : : tree
18326 : 135 : c_finish_transaction (location_t loc, tree block, int flags)
18327 : : {
18328 : 135 : tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
18329 : 135 : if (flags & TM_STMT_ATTR_OUTER)
18330 : 10 : TRANSACTION_EXPR_OUTER (stmt) = 1;
18331 : 135 : if (flags & TM_STMT_ATTR_RELAXED)
18332 : 31 : TRANSACTION_EXPR_RELAXED (stmt) = 1;
18333 : 135 : return add_stmt (stmt);
18334 : : }
18335 : :
18336 : : /* Make a variant type in the proper way for C/C++, propagating qualifiers
18337 : : down to the element type of an array. If ORIG_QUAL_TYPE is not
18338 : : NULL, then it should be used as the qualified type
18339 : : ORIG_QUAL_INDIRECT levels down in array type derivation (to
18340 : : preserve information about the typedef name from which an array
18341 : : type was derived). */
18342 : :
18343 : : tree
18344 : 72986729 : c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
18345 : : size_t orig_qual_indirect)
18346 : : {
18347 : 72986729 : if (type == error_mark_node)
18348 : : return type;
18349 : :
18350 : 72986581 : if (TREE_CODE (type) == ARRAY_TYPE)
18351 : : {
18352 : 3121788 : tree t;
18353 : 3121788 : tree element_type = c_build_qualified_type (TREE_TYPE (type),
18354 : : type_quals, orig_qual_type,
18355 : : orig_qual_indirect - 1);
18356 : :
18357 : : /* See if we already have an identically qualified type. */
18358 : 3121788 : if (orig_qual_type && orig_qual_indirect == 0)
18359 : : t = orig_qual_type;
18360 : : else
18361 : 4559144 : for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
18362 : : {
18363 : 4419741 : if (TYPE_QUALS (strip_array_types (t)) == type_quals
18364 : 3003066 : && TYPE_NAME (t) == TYPE_NAME (type)
18365 : 2982344 : && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
18366 : 7402085 : && attribute_list_equal (TYPE_ATTRIBUTES (t),
18367 : 2982344 : TYPE_ATTRIBUTES (type)))
18368 : : break;
18369 : : }
18370 : 3121747 : if (!t)
18371 : : {
18372 : 139403 : tree domain = TYPE_DOMAIN (type);
18373 : :
18374 : 139403 : t = build_variant_type_copy (type);
18375 : 139403 : TREE_TYPE (t) = element_type;
18376 : 139403 : TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (element_type);
18377 : :
18378 : 139403 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
18379 : 139403 : || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
18380 : 194 : SET_TYPE_STRUCTURAL_EQUALITY (t);
18381 : 139209 : else if (TYPE_CANONICAL (element_type) != element_type
18382 : 139209 : || (domain && TYPE_CANONICAL (domain) != domain))
18383 : : {
18384 : 2701 : tree unqualified_canon
18385 : 5190 : = c_build_array_type (TYPE_CANONICAL (element_type),
18386 : 2489 : domain ? TYPE_CANONICAL (domain)
18387 : : : NULL_TREE);
18388 : 2701 : if (TYPE_REVERSE_STORAGE_ORDER (type))
18389 : : {
18390 : 0 : unqualified_canon
18391 : 0 : = build_distinct_type_copy (unqualified_canon);
18392 : 0 : TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
18393 : : }
18394 : 2701 : TYPE_CANONICAL (t)
18395 : 5402 : = c_build_qualified_type (unqualified_canon, type_quals);
18396 : : }
18397 : : else
18398 : 136508 : TYPE_CANONICAL (t) = t;
18399 : : }
18400 : 3121788 : return t;
18401 : : }
18402 : :
18403 : : /* A restrict-qualified pointer type must be a pointer to object or
18404 : : incomplete type. Note that the use of POINTER_TYPE_P also allows
18405 : : REFERENCE_TYPEs, which is appropriate for C++. */
18406 : 69864793 : if ((type_quals & TYPE_QUAL_RESTRICT)
18407 : 69864793 : && (!POINTER_TYPE_P (type)
18408 : 3337991 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
18409 : : {
18410 : 3 : error ("invalid use of %<restrict%>");
18411 : 3 : type_quals &= ~TYPE_QUAL_RESTRICT;
18412 : : }
18413 : :
18414 : 69864793 : tree var_type = (orig_qual_type && orig_qual_indirect == 0
18415 : 69864793 : ? orig_qual_type
18416 : 69864762 : : build_qualified_type (type, type_quals));
18417 : :
18418 : 69864793 : gcc_checking_assert (C_TYPE_VARIABLE_SIZE (var_type)
18419 : : == C_TYPE_VARIABLE_SIZE (type));
18420 : 69864793 : gcc_checking_assert (C_TYPE_VARIABLY_MODIFIED (var_type)
18421 : : == C_TYPE_VARIABLY_MODIFIED (type));
18422 : :
18423 : : /* A variant type does not inherit the list of incomplete vars from the
18424 : : type main variant. */
18425 : 69864793 : if ((RECORD_OR_UNION_TYPE_P (var_type)
18426 : 67973890 : || TREE_CODE (var_type) == ENUMERAL_TYPE)
18427 : 69968781 : && TYPE_MAIN_VARIANT (var_type) != var_type)
18428 : 1388445 : C_TYPE_INCOMPLETE_VARS (var_type) = 0;
18429 : : return var_type;
18430 : : }
18431 : :
18432 : : /* Build a VA_ARG_EXPR for the C parser. */
18433 : :
18434 : : tree
18435 : 19886 : c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
18436 : : {
18437 : 19886 : if (error_operand_p (type))
18438 : 2 : return error_mark_node;
18439 : : /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
18440 : : order because it takes the address of the expression. */
18441 : 19884 : else if (handled_component_p (expr)
18442 : 31 : && reverse_storage_order_for_component_p (expr))
18443 : : {
18444 : 0 : error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
18445 : 0 : return error_mark_node;
18446 : : }
18447 : 19884 : else if (!COMPLETE_TYPE_P (type))
18448 : : {
18449 : 11 : error_at (loc2, "second argument to %<va_arg%> is of incomplete "
18450 : : "type %qT", type);
18451 : 11 : return error_mark_node;
18452 : : }
18453 : 19873 : else if (TREE_CODE (type) == FUNCTION_TYPE)
18454 : : {
18455 : 1 : error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
18456 : : type);
18457 : 1 : return error_mark_node;
18458 : : }
18459 : 19872 : else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
18460 : 1 : warning_at (loc2, OPT_Wc___compat,
18461 : : "C++ requires promoted type, not enum type, in %<va_arg%>");
18462 : 19872 : return build_va_arg (loc2, expr, type);
18463 : : }
18464 : :
18465 : : /* Return truthvalue of whether T1 is the same tree structure as T2.
18466 : : Return 1 if they are the same. Return false if they are different. */
18467 : :
18468 : : bool
18469 : 1854 : c_tree_equal (tree t1, tree t2)
18470 : : {
18471 : 1883 : enum tree_code code1, code2;
18472 : :
18473 : 1883 : if (t1 == t2)
18474 : : return true;
18475 : 661 : if (!t1 || !t2)
18476 : : return false;
18477 : :
18478 : 681 : for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
18479 : 20 : code1 = TREE_CODE (t1))
18480 : 20 : t1 = TREE_OPERAND (t1, 0);
18481 : 682 : for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
18482 : 21 : code2 = TREE_CODE (t2))
18483 : 21 : t2 = TREE_OPERAND (t2, 0);
18484 : :
18485 : : /* They might have become equal now. */
18486 : 661 : if (t1 == t2)
18487 : : return true;
18488 : :
18489 : 640 : if (code1 != code2)
18490 : : return false;
18491 : :
18492 : 379 : if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18493 : : return false;
18494 : :
18495 : 379 : switch (code1)
18496 : : {
18497 : 2 : case INTEGER_CST:
18498 : 2 : return wi::to_wide (t1) == wi::to_wide (t2);
18499 : :
18500 : 10 : case REAL_CST:
18501 : 10 : return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
18502 : :
18503 : 0 : case STRING_CST:
18504 : 0 : return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
18505 : 0 : && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
18506 : 0 : TREE_STRING_LENGTH (t1));
18507 : :
18508 : 0 : case FIXED_CST:
18509 : 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
18510 : : TREE_FIXED_CST (t2));
18511 : :
18512 : 0 : case COMPLEX_CST:
18513 : 0 : return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
18514 : 0 : && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
18515 : :
18516 : 0 : case VECTOR_CST:
18517 : 0 : return operand_equal_p (t1, t2, OEP_ONLY_CONST);
18518 : :
18519 : 0 : case CONSTRUCTOR:
18520 : : /* We need to do this when determining whether or not two
18521 : : non-type pointer to member function template arguments
18522 : : are the same. */
18523 : 0 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
18524 : 0 : || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
18525 : : return false;
18526 : : {
18527 : : tree field, value;
18528 : : unsigned int i;
18529 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
18530 : : {
18531 : 0 : constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
18532 : 0 : if (!c_tree_equal (field, elt2->index)
18533 : 0 : || !c_tree_equal (value, elt2->value))
18534 : 0 : return false;
18535 : : }
18536 : : }
18537 : : return true;
18538 : :
18539 : 0 : case TREE_LIST:
18540 : 0 : if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
18541 : : return false;
18542 : 0 : if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
18543 : : return false;
18544 : 0 : return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
18545 : :
18546 : 0 : case SAVE_EXPR:
18547 : 0 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18548 : :
18549 : 25 : case CALL_EXPR:
18550 : 25 : {
18551 : 25 : tree arg1, arg2;
18552 : 25 : call_expr_arg_iterator iter1, iter2;
18553 : 25 : if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
18554 : : return false;
18555 : 50 : for (arg1 = first_call_expr_arg (t1, &iter1),
18556 : 25 : arg2 = first_call_expr_arg (t2, &iter2);
18557 : 25 : arg1 && arg2;
18558 : 0 : arg1 = next_call_expr_arg (&iter1),
18559 : 0 : arg2 = next_call_expr_arg (&iter2))
18560 : 0 : if (!c_tree_equal (arg1, arg2))
18561 : : return false;
18562 : 25 : if (arg1 || arg2)
18563 : : return false;
18564 : : return true;
18565 : : }
18566 : :
18567 : 0 : case TARGET_EXPR:
18568 : 0 : {
18569 : 0 : tree o1 = TREE_OPERAND (t1, 0);
18570 : 0 : tree o2 = TREE_OPERAND (t2, 0);
18571 : :
18572 : : /* Special case: if either target is an unallocated VAR_DECL,
18573 : : it means that it's going to be unified with whatever the
18574 : : TARGET_EXPR is really supposed to initialize, so treat it
18575 : : as being equivalent to anything. */
18576 : 0 : if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
18577 : 0 : && !DECL_RTL_SET_P (o1))
18578 : : /*Nop*/;
18579 : 0 : else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
18580 : 0 : && !DECL_RTL_SET_P (o2))
18581 : : /*Nop*/;
18582 : 0 : else if (!c_tree_equal (o1, o2))
18583 : : return false;
18584 : :
18585 : 0 : return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
18586 : : }
18587 : :
18588 : 29 : case COMPONENT_REF:
18589 : 29 : if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
18590 : : return false;
18591 : 29 : return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
18592 : :
18593 : : case PARM_DECL:
18594 : : case VAR_DECL:
18595 : : case CONST_DECL:
18596 : : case FIELD_DECL:
18597 : : case FUNCTION_DECL:
18598 : : case IDENTIFIER_NODE:
18599 : : case SSA_NAME:
18600 : : return false;
18601 : :
18602 : 0 : case TREE_VEC:
18603 : 0 : {
18604 : 0 : unsigned ix;
18605 : 0 : if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
18606 : : return false;
18607 : 0 : for (ix = TREE_VEC_LENGTH (t1); ix--;)
18608 : 0 : if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
18609 : 0 : TREE_VEC_ELT (t2, ix)))
18610 : : return false;
18611 : : return true;
18612 : : }
18613 : :
18614 : 22 : CASE_CONVERT:
18615 : 22 : if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
18616 : : return false;
18617 : : break;
18618 : :
18619 : : default:
18620 : : break;
18621 : : }
18622 : :
18623 : 131 : switch (TREE_CODE_CLASS (code1))
18624 : : {
18625 : 131 : case tcc_unary:
18626 : 131 : case tcc_binary:
18627 : 131 : case tcc_comparison:
18628 : 131 : case tcc_expression:
18629 : 131 : case tcc_vl_exp:
18630 : 131 : case tcc_reference:
18631 : 131 : case tcc_statement:
18632 : 131 : {
18633 : 131 : int i, n = TREE_OPERAND_LENGTH (t1);
18634 : :
18635 : 131 : switch (code1)
18636 : : {
18637 : 0 : case PREINCREMENT_EXPR:
18638 : 0 : case PREDECREMENT_EXPR:
18639 : 0 : case POSTINCREMENT_EXPR:
18640 : 0 : case POSTDECREMENT_EXPR:
18641 : 0 : n = 1;
18642 : 0 : break;
18643 : 16 : case ARRAY_REF:
18644 : 16 : n = 2;
18645 : 16 : break;
18646 : : default:
18647 : : break;
18648 : : }
18649 : :
18650 : 131 : if (TREE_CODE_CLASS (code1) == tcc_vl_exp
18651 : 131 : && n != TREE_OPERAND_LENGTH (t2))
18652 : : return false;
18653 : :
18654 : 305 : for (i = 0; i < n; ++i)
18655 : 186 : if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
18656 : : return false;
18657 : :
18658 : : return true;
18659 : : }
18660 : :
18661 : 0 : case tcc_type:
18662 : 0 : return comptypes (t1, t2);
18663 : 0 : default:
18664 : 0 : gcc_unreachable ();
18665 : : }
18666 : : }
18667 : :
18668 : : /* Returns true when the function declaration FNDECL is implicit,
18669 : : introduced as a result of a call to an otherwise undeclared
18670 : : function, and false otherwise. */
18671 : :
18672 : : bool
18673 : 2013 : c_decl_implicit (const_tree fndecl)
18674 : : {
18675 : 2013 : return C_DECL_IMPLICIT (fndecl);
18676 : : }
|