]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/typeck.c
PR translation/80280 - Missing closing quote (%>) c/semantics.c and c/c-typeck.c
[thirdparty/gcc.git] / gcc / cp / typeck.c
CommitLineData
8d08fdba 1/* Build expressions with type checking for C++ compiler.
cbe34bb5 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
8d08fdba
MS
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
f5adbb8d 5This file is part of GCC.
8d08fdba 6
f5adbb8d 7GCC is free software; you can redistribute it and/or modify
8d08fdba 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
10any later version.
11
f5adbb8d 12GCC is distributed in the hope that it will be useful,
8d08fdba
MS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
8d08fdba
MS
20
21
22/* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
5088b058 25 checks, and some optimization. */
8d08fdba 26
8d08fdba 27#include "config.h"
8d052bc7 28#include "system.h"
4977bab6 29#include "coretypes.h"
2adfab87 30#include "target.h"
2adfab87 31#include "cp-tree.h"
d8a2d370
DN
32#include "stor-layout.h"
33#include "varasm.h"
ff84991f 34#include "intl.h"
7b6d72fc 35#include "convert.h"
61d3ce20 36#include "c-family/c-objc.h"
de5a5fa1 37#include "c-family/c-ubsan.h"
06d40de8 38#include "params.h"
264757fb 39#include "gcc-rich-location.h"
8d08fdba 40
882de214
KT
41static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
42static tree cp_build_function_call (tree, tree, tsubst_flags_t);
b5791fdc 43static tree pfn_from_ptrmemfunc (tree);
a298680c 44static tree delta_from_ptrmemfunc (tree);
2f5b91f5 45static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
e57d93c6 46 tsubst_flags_t, int);
4f2e1536
MP
47static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
48 tsubst_flags_t);
5ade1ed2
DG
49static tree rationalize_conditional_expr (enum tree_code, tree,
50 tsubst_flags_t);
acd8e2d0 51static int comp_ptr_ttypes_real (tree, tree, int);
acd8e2d0 52static bool comp_except_types (tree, tree, bool);
58f9752a 53static bool comp_array_types (const_tree, const_tree, bool);
4f2e1536 54static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t);
135d47df 55static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
b40e334f
PC
56static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
57static bool casts_away_constness (tree, tree, tsubst_flags_t);
b4dfdc11 58static bool maybe_warn_about_returning_address_of_local (tree);
76545796 59static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
09bae928 60static void error_args_num (location_t, tree, bool);
9771b263 61static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
5ade1ed2 62 tsubst_flags_t);
8d08fdba 63
8d08fdba 64/* Do `exp = require_complete_type (exp);' to make sure exp
ae58fa02 65 does not have an incomplete type. (That includes void types.)
79fe346e 66 Returns error_mark_node if the VALUE does not have
ae58fa02 67 complete type when this function returns. */
8d08fdba
MS
68
69tree
79fe346e 70require_complete_type_sfinae (tree value, tsubst_flags_t complain)
8d08fdba 71{
5566b478
MS
72 tree type;
73
66543169 74 if (processing_template_decl || value == error_mark_node)
5566b478
MS
75 return value;
76
2c73f9f5
ML
77 if (TREE_CODE (value) == OVERLOAD)
78 type = unknown_type_node;
79 else
80 type = TREE_TYPE (value);
8d08fdba 81
ae5cbc33
RS
82 if (type == error_mark_node)
83 return error_mark_node;
84
8d08fdba 85 /* First, detect a valid value with a complete type. */
d0f062fb 86 if (COMPLETE_TYPE_P (type))
8d08fdba
MS
87 return value;
88
79fe346e 89 if (complete_type_or_maybe_complain (type, value, complain))
8f259df3
MM
90 return value;
91 else
ae58fa02 92 return error_mark_node;
8d08fdba
MS
93}
94
79fe346e
JM
95tree
96require_complete_type (tree value)
97{
98 return require_complete_type_sfinae (value, tf_warning_or_error);
99}
100
8f259df3
MM
101/* Try to complete TYPE, if it is incomplete. For example, if TYPE is
102 a template instantiation, do the instantiation. Returns TYPE,
103 whether or not it could be completed, unless something goes
104 horribly wrong, in which case the error_mark_node is returned. */
105
5566b478 106tree
acd8e2d0 107complete_type (tree type)
5566b478 108{
8857f91e
MM
109 if (type == NULL_TREE)
110 /* Rather than crash, we return something sure to cause an error
111 at some point. */
112 return error_mark_node;
113
d0f062fb 114 if (type == error_mark_node || COMPLETE_TYPE_P (type))
5566b478 115 ;
f65e97fd 116 else if (TREE_CODE (type) == ARRAY_TYPE)
5566b478
MS
117 {
118 tree t = complete_type (TREE_TYPE (type));
73bebd55 119 unsigned int needs_constructing, has_nontrivial_dtor;
1e2e9f54 120 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
6467930b 121 layout_type (type);
73bebd55 122 needs_constructing
c73964b2 123 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
73bebd55 124 has_nontrivial_dtor
834c6dff 125 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
73bebd55
JJ
126 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
127 {
128 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
129 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
130 }
5566b478 131 }
7ddedda4 132 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
e76a2646 133 instantiate_class_template (TYPE_MAIN_VARIANT (type));
5566b478
MS
134
135 return type;
136}
137
5aa3396c 138/* Like complete_type, but issue an error if the TYPE cannot be completed.
be20e673 139 VALUE is used for informative diagnostics.
66543169 140 Returns NULL_TREE if the type cannot be made complete. */
8f259df3
MM
141
142tree
309714d4 143complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
8f259df3
MM
144{
145 type = complete_type (type);
ae58fa02
MM
146 if (type == error_mark_node)
147 /* We already issued an error. */
148 return NULL_TREE;
d0f062fb 149 else if (!COMPLETE_TYPE_P (type))
8f259df3 150 {
309714d4
JM
151 if (complain & tf_error)
152 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
8f259df3
MM
153 return NULL_TREE;
154 }
155 else
156 return type;
157}
158
309714d4
JM
159tree
160complete_type_or_else (tree type, tree value)
161{
162 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
163}
164
8d08fdba 165/* Return truthvalue of whether type of EXP is instantiated. */
e92cc029 166
8d08fdba 167int
58f9752a 168type_unknown_p (const_tree exp)
8d08fdba 169{
26bcf8fc 170 return (TREE_CODE (exp) == TREE_LIST
a5ac359a 171 || TREE_TYPE (exp) == unknown_type_node);
8d08fdba
MS
172}
173
8d08fdba
MS
174\f
175/* Return the common type of two parameter lists.
96d84882 176 We assume that comptypes has already been done and returned 1;
8d08fdba
MS
177 if that isn't so, this may crash.
178
179 As an optimization, free the space we allocate if the parameter
180 lists are already common. */
181
10b2bcdd 182static tree
5671bf27 183commonparms (tree p1, tree p2)
8d08fdba
MS
184{
185 tree oldargs = p1, newargs, n;
186 int i, len;
187 int any_change = 0;
8d08fdba
MS
188
189 len = list_length (p1);
190 newargs = tree_last (p1);
191
192 if (newargs == void_list_node)
193 i = 1;
194 else
195 {
196 i = 0;
197 newargs = 0;
198 }
199
200 for (; i < len; i++)
201 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
202
203 n = newargs;
204
205 for (i = 0; p1;
206 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
207 {
208 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
209 {
8d08fdba
MS
210 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
211 any_change = 1;
212 }
213 else if (! TREE_PURPOSE (p1))
214 {
215 if (TREE_PURPOSE (p2))
216 {
217 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
218 any_change = 1;
219 }
220 }
221 else
222 {
1743ca29 223 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
a4443a08 224 any_change = 1;
8d08fdba
MS
225 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
226 }
227 if (TREE_VALUE (p1) != TREE_VALUE (p2))
228 {
229 any_change = 1;
6da794e8 230 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
8d08fdba
MS
231 }
232 else
233 TREE_VALUE (n) = TREE_VALUE (p1);
234 }
235 if (! any_change)
970d6386 236 return oldargs;
8d08fdba
MS
237
238 return newargs;
239}
240
f2ee215b
BK
241/* Given a type, perhaps copied for a typedef,
242 find the "original" version of it. */
993acaec 243static tree
5671bf27 244original_type (tree t)
f2ee215b 245{
8e30dcf3 246 int quals = cp_type_quals (t);
2b643eda
MM
247 while (t != error_mark_node
248 && TYPE_NAME (t) != NULL_TREE)
f2ee215b
BK
249 {
250 tree x = TYPE_NAME (t);
251 if (TREE_CODE (x) != TYPE_DECL)
252 break;
253 x = DECL_ORIGINAL_TYPE (x);
254 if (x == NULL_TREE)
255 break;
256 t = x;
257 }
8e30dcf3 258 return cp_build_qualified_type (t, quals);
f2ee215b
BK
259}
260
98f2f3a2
MLI
261/* Return the common type for two arithmetic types T1 and T2 under the
262 usual arithmetic conversions. The default conversions have already
263 been applied, and enumerated types converted to their compatible
264 integer types. */
a7a64a77 265
98f2f3a2
MLI
266static tree
267cp_common_type (tree t1, tree t2)
a7a64a77
MM
268{
269 enum tree_code code1 = TREE_CODE (t1);
270 enum tree_code code2 = TREE_CODE (t2);
271 tree attributes;
78a7c317 272 int i;
a7a64a77 273
c5ee1358 274
550a799d
JM
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
277 attributes. */
278 attributes = (*targetm.merge_type_attributes) (t1, t2);
279
280 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
281 {
282 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
283 return build_type_attribute_variant (t1, attributes);
284 else
285 return NULL_TREE;
286 }
287
a7a64a77 288 /* FIXME: Attributes. */
c8094d83 289 gcc_assert (ARITHMETIC_TYPE_P (t1)
b55b02ea 290 || VECTOR_TYPE_P (t1)
adf2edec 291 || UNSCOPED_ENUM_P (t1));
c8094d83 292 gcc_assert (ARITHMETIC_TYPE_P (t2)
b55b02ea 293 || VECTOR_TYPE_P (t2)
adf2edec 294 || UNSCOPED_ENUM_P (t2));
a7a64a77 295
6da794e8
JM
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
298 required type. */
299 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
300 {
301 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
302 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
303 tree subtype
304 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
305
306 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
307 return build_type_attribute_variant (t1, attributes);
308 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
309 return build_type_attribute_variant (t2, attributes);
310 else
311 return build_type_attribute_variant (build_complex_type (subtype),
312 attributes);
313 }
314
73042643
AH
315 if (code1 == VECTOR_TYPE)
316 {
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1))
320 return build_type_attribute_variant (t1, attributes);
321 else
322 return build_type_attribute_variant (t2, attributes);
323 }
324
a7a64a77
MM
325 /* If only one is real, use it as the result. */
326 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
327 return build_type_attribute_variant (t1, attributes);
328 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
329 return build_type_attribute_variant (t2, attributes);
330
a7a64a77
MM
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
333 return build_type_attribute_variant (t1, attributes);
334 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
335 return build_type_attribute_variant (t2, attributes);
336
2f4d058f
AH
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
339 return build_type_attribute_variant (t1, attributes);
340
a7a64a77
MM
341 if (code1 != REAL_TYPE)
342 {
343 /* If one is unsigned long long, then convert the other to unsigned
344 long long. */
345 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
346 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
347 return build_type_attribute_variant (long_long_unsigned_type_node,
348 attributes);
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
353 other to long long.
c8094d83 354
a7a64a77
MM
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
358 case. */
359 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
360 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
361 {
8df83eae 362 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
c8094d83 363 ? long_long_unsigned_type_node
a7a64a77
MM
364 : long_long_integer_type_node);
365 return build_type_attribute_variant (t, attributes);
366 }
c8094d83 367
a7a64a77
MM
368 /* Go through the same procedure, but for longs. */
369 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
370 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
371 return build_type_attribute_variant (long_unsigned_type_node,
372 attributes);
373 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
374 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
375 {
8df83eae 376 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
a7a64a77
MM
377 ? long_unsigned_type_node : long_integer_type_node);
378 return build_type_attribute_variant (t, attributes);
379 }
9edc6e4c
DD
380
381 /* For __intN types, either the type is __int128 (and is lower
382 priority than the types checked above, but higher than other
383 128-bit types) or it's known to not be the same size as other
384 types (enforced in toplev.c). Prefer the unsigned type. */
385 for (i = 0; i < NUM_INT_N_ENTS; i ++)
386 {
387 if (int_n_enabled_p [i]
388 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
389 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
390 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
391 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
392 {
393 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
394 ? int_n_trees[i].unsigned_type
395 : int_n_trees[i].signed_type);
396 return build_type_attribute_variant (t, attributes);
397 }
398 }
399
a7a64a77 400 /* Otherwise prefer the unsigned one. */
8df83eae 401 if (TYPE_UNSIGNED (t1))
a7a64a77
MM
402 return build_type_attribute_variant (t1, attributes);
403 else
404 return build_type_attribute_variant (t2, attributes);
405 }
406 else
407 {
408 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
409 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
410 return build_type_attribute_variant (long_double_type_node,
411 attributes);
412 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
413 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
414 return build_type_attribute_variant (double_type_node,
415 attributes);
2f4d058f
AH
416 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
417 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
a7a64a77
MM
418 return build_type_attribute_variant (float_type_node,
419 attributes);
c8094d83 420
2f4d058f 421 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
0cbd7506
MS
422 the standard C++ floating-point types. Logic earlier in this
423 function has already eliminated the possibility that
424 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
425 compelling reason to choose one or the other. */
2f4d058f 426 return build_type_attribute_variant (t1, attributes);
a7a64a77
MM
427 }
428}
429
98f2f3a2
MLI
430/* T1 and T2 are arithmetic or enumeration types. Return the type
431 that will result from the "usual arithmetic conversions" on T1 and
432 T2 as described in [expr]. */
433
434tree
435type_after_usual_arithmetic_conversions (tree t1, tree t2)
436{
437 gcc_assert (ARITHMETIC_TYPE_P (t1)
b55b02ea 438 || VECTOR_TYPE_P (t1)
98f2f3a2
MLI
439 || UNSCOPED_ENUM_P (t1));
440 gcc_assert (ARITHMETIC_TYPE_P (t2)
b55b02ea 441 || VECTOR_TYPE_P (t2)
98f2f3a2
MLI
442 || UNSCOPED_ENUM_P (t2));
443
444 /* Perform the integral promotions. We do not promote real types here. */
445 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
6f9b088b 446 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
98f2f3a2
MLI
447 {
448 t1 = type_promotes_to (t1);
449 t2 = type_promotes_to (t2);
450 }
451
452 return cp_common_type (t1, t2);
453}
454
083586b8
NF
455static void
456composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
457 composite_pointer_operation operation)
458{
459 switch (operation)
460 {
461 case CPO_COMPARISON:
462 emit_diagnostic (kind, input_location, 0,
463 "comparison between "
464 "distinct pointer types %qT and %qT lacks a cast",
465 t1, t2);
466 break;
467 case CPO_CONVERSION:
468 emit_diagnostic (kind, input_location, 0,
469 "conversion between "
470 "distinct pointer types %qT and %qT lacks a cast",
471 t1, t2);
472 break;
473 case CPO_CONDITIONAL_EXPR:
474 emit_diagnostic (kind, input_location, 0,
475 "conditional expression between "
476 "distinct pointer types %qT and %qT lacks a cast",
477 t1, t2);
478 break;
479 default:
480 gcc_unreachable ();
481 }
482}
483
a5ac359a 484/* Subroutine of composite_pointer_type to implement the recursive
c86818cf 485 case. See that function for documentation of the parameters. */
a5ac359a
MM
486
487static tree
c86818cf
SZ
488composite_pointer_type_r (tree t1, tree t2,
489 composite_pointer_operation operation,
5ade1ed2 490 tsubst_flags_t complain)
a5ac359a
MM
491{
492 tree pointee1;
493 tree pointee2;
494 tree result_type;
495 tree attributes;
496
497 /* Determine the types pointed to by T1 and T2. */
50e10fa8 498 if (TYPE_PTR_P (t1))
a5ac359a
MM
499 {
500 pointee1 = TREE_TYPE (t1);
501 pointee2 = TREE_TYPE (t2);
502 }
503 else
504 {
505 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
506 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
507 }
508
509 /* [expr.rel]
510
511 Otherwise, the composite pointer type is a pointer type
512 similar (_conv.qual_) to the type of one of the operands,
513 with a cv-qualification signature (_conv.qual_) that is the
514 union of the cv-qualification signatures of the operand
515 types. */
516 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
517 result_type = pointee1;
50e10fa8 518 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
66b1156a 519 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
df9ee5c8
PC
520 {
521 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
522 complain);
523 if (result_type == error_mark_node)
524 return error_mark_node;
525 }
a5ac359a
MM
526 else
527 {
5ade1ed2 528 if (complain & tf_error)
083586b8 529 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
7a1db261
JM
530 else
531 return error_mark_node;
a5ac359a
MM
532 result_type = void_type_node;
533 }
534 result_type = cp_build_qualified_type (result_type,
535 (cp_type_quals (pointee1)
536 | cp_type_quals (pointee2)));
a5ac359a
MM
537 /* If the original types were pointers to members, so is the
538 result. */
66b1156a 539 if (TYPE_PTRMEM_P (t1))
a5ac359a
MM
540 {
541 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
7a1db261
JM
542 TYPE_PTRMEM_CLASS_TYPE (t2)))
543 {
544 if (complain & tf_error)
545 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
546 else
547 return error_mark_node;
548 }
a5ac359a
MM
549 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
550 result_type);
551 }
9d363a56
MM
552 else
553 result_type = build_pointer_type (result_type);
a5ac359a
MM
554
555 /* Merge the attributes. */
556 attributes = (*targetm.merge_type_attributes) (t1, t2);
557 return build_type_attribute_variant (result_type, attributes);
558}
559
a7a64a77 560/* Return the composite pointer type (see [expr.rel]) for T1 and T2.
c86818cf
SZ
561 ARG1 and ARG2 are the values with those types. The OPERATION is to
562 describe the operation between the pointer types,
563 in case an error occurs.
a5ac359a
MM
564
565 This routine also implements the computation of a common type for
566 pointers-to-members as per [expr.eq]. */
a7a64a77 567
c8094d83 568tree
5671bf27 569composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
c86818cf
SZ
570 composite_pointer_operation operation,
571 tsubst_flags_t complain)
a7a64a77 572{
a5ac359a
MM
573 tree class1;
574 tree class2;
a7a64a77
MM
575
576 /* [expr.rel]
577
578 If one operand is a null pointer constant, the composite pointer
579 type is the type of the other operand. */
580 if (null_ptr_cst_p (arg1))
581 return t2;
582 if (null_ptr_cst_p (arg2))
583 return t1;
c8094d83 584
634790f4
MM
585 /* We have:
586
587 [expr.rel]
588
589 If one of the operands has type "pointer to cv1 void*", then
590 the other has type "pointer to cv2T", and the composite pointer
591 type is "pointer to cv12 void", where cv12 is the union of cv1
592 and cv2.
593
594 If either type is a pointer to void, make sure it is T1. */
50e10fa8 595 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
fab27f52 596 std::swap (t1, t2);
a5ac359a 597
634790f4 598 /* Now, if T1 is a pointer to void, merge the qualifiers. */
50e10fa8 599 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
a7a64a77 600 {
a5ac359a
MM
601 tree attributes;
602 tree result_type;
603
aa0a659c
PC
604 if (TYPE_PTRFN_P (t2))
605 {
606 if (complain & tf_error)
607 {
608 switch (operation)
609 {
610 case CPO_COMPARISON:
611 pedwarn (input_location, OPT_Wpedantic,
612 "ISO C++ forbids comparison between pointer "
613 "of type %<void *%> and pointer-to-function");
614 break;
615 case CPO_CONVERSION:
616 pedwarn (input_location, OPT_Wpedantic,
617 "ISO C++ forbids conversion between pointer "
618 "of type %<void *%> and pointer-to-function");
619 break;
620 case CPO_CONDITIONAL_EXPR:
621 pedwarn (input_location, OPT_Wpedantic,
622 "ISO C++ forbids conditional expression between "
623 "pointer of type %<void *%> and "
624 "pointer-to-function");
625 break;
626 default:
627 gcc_unreachable ();
628 }
629 }
630 else
631 return error_mark_node;
c86818cf 632 }
c8094d83 633 result_type
a5ac359a
MM
634 = cp_build_qualified_type (void_type_node,
635 (cp_type_quals (TREE_TYPE (t1))
636 | cp_type_quals (TREE_TYPE (t2))));
634790f4 637 result_type = build_pointer_type (result_type);
a5ac359a
MM
638 /* Merge the attributes. */
639 attributes = (*targetm.merge_type_attributes) (t1, t2);
640 return build_type_attribute_variant (result_type, attributes);
641 }
642
50e10fa8
PC
643 if (c_dialect_objc () && TYPE_PTR_P (t1)
644 && TYPE_PTR_P (t2))
660845bf 645 {
b581b85b
NP
646 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
647 return objc_common_type (t1, t2);
660845bf
ZL
648 }
649
51dc6603
JM
650 /* if T1 or T2 is "pointer to noexcept function" and the other type is
651 "pointer to function", where the function types are otherwise the same,
652 "pointer to function" */
653 if (fnptr_conv_p (t1, t2))
654 return t1;
655 if (fnptr_conv_p (t2, t1))
656 return t2;
657
a5ac359a
MM
658 /* [expr.eq] permits the application of a pointer conversion to
659 bring the pointers to a common type. */
50e10fa8 660 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
a5ac359a
MM
661 && CLASS_TYPE_P (TREE_TYPE (t1))
662 && CLASS_TYPE_P (TREE_TYPE (t2))
663 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
664 TREE_TYPE (t2)))
665 {
666 class1 = TREE_TYPE (t1);
667 class2 = TREE_TYPE (t2);
668
660845bf 669 if (DERIVED_FROM_P (class1, class2))
c8094d83 670 t2 = (build_pointer_type
a3360e77 671 (cp_build_qualified_type (class1, cp_type_quals (class2))));
660845bf 672 else if (DERIVED_FROM_P (class2, class1))
c8094d83 673 t1 = (build_pointer_type
a3360e77 674 (cp_build_qualified_type (class2, cp_type_quals (class1))));
a5ac359a 675 else
c86818cf
SZ
676 {
677 if (complain & tf_error)
083586b8 678 composite_pointer_error (DK_ERROR, t1, t2, operation);
c86818cf
SZ
679 return error_mark_node;
680 }
a7a64a77 681 }
a5ac359a
MM
682 /* [expr.eq] permits the application of a pointer-to-member
683 conversion to change the class type of one of the types. */
66b1156a 684 else if (TYPE_PTRMEM_P (t1)
c86818cf 685 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
a5ac359a 686 TYPE_PTRMEM_CLASS_TYPE (t2)))
708cae97 687 {
a5ac359a
MM
688 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
689 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
708cae97 690
a5ac359a
MM
691 if (DERIVED_FROM_P (class1, class2))
692 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
693 else if (DERIVED_FROM_P (class2, class1))
694 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
a7a64a77 695 else
c86818cf
SZ
696 {
697 if (complain & tf_error)
698 switch (operation)
699 {
700 case CPO_COMPARISON:
701 error ("comparison between distinct "
702 "pointer-to-member types %qT and %qT lacks a cast",
703 t1, t2);
704 break;
705 case CPO_CONVERSION:
706 error ("conversion between distinct "
707 "pointer-to-member types %qT and %qT lacks a cast",
708 t1, t2);
709 break;
710 case CPO_CONDITIONAL_EXPR:
711 error ("conditional expression between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
713 t1, t2);
714 break;
715 default:
716 gcc_unreachable ();
717 }
718 return error_mark_node;
719 }
720 }
721
722 return composite_pointer_type_r (t1, t2, operation, complain);
a7a64a77
MM
723}
724
6da794e8 725/* Return the merged type of two types.
96d84882 726 We assume that comptypes has already been done and returned 1;
8d08fdba
MS
727 if that isn't so, this may crash.
728
6da794e8
JM
729 This just combines attributes and default arguments; any other
730 differences would cause the two types to compare unalike. */
8d08fdba
MS
731
732tree
5671bf27 733merge_types (tree t1, tree t2)
8d08fdba 734{
926ce8bd
KH
735 enum tree_code code1;
736 enum tree_code code2;
2986ae00 737 tree attributes;
8d08fdba
MS
738
739 /* Save time if the two types are the same. */
f2ee215b 740 if (t1 == t2)
9076e292 741 return t1;
6da794e8 742 if (original_type (t1) == original_type (t2))
9076e292 743 return t1;
8d08fdba
MS
744
745 /* If one type is nonsense, use the other. */
746 if (t1 == error_mark_node)
9076e292 747 return t2;
8d08fdba 748 if (t2 == error_mark_node)
9076e292 749 return t1;
8d08fdba 750
852497a3
JM
751 /* Handle merging an auto redeclaration with a previous deduced
752 return type. */
753 if (is_auto (t1))
754 return t2;
755
d9525bec 756 /* Merge the attributes. */
f6897b10 757 attributes = (*targetm.merge_type_attributes) (t1, t2);
2986ae00 758
5566b478
MS
759 if (TYPE_PTRMEMFUNC_P (t1))
760 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
761 if (TYPE_PTRMEMFUNC_P (t2))
762 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
763
8d08fdba
MS
764 code1 = TREE_CODE (t1);
765 code2 = TREE_CODE (t2);
e350dbbd
PB
766 if (code1 != code2)
767 {
768 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
769 if (code1 == TYPENAME_TYPE)
770 {
771 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
772 code1 = TREE_CODE (t1);
773 }
774 else
775 {
776 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
777 code2 = TREE_CODE (t2);
778 }
779 }
8d08fdba
MS
780
781 switch (code1)
782 {
8d08fdba
MS
783 case POINTER_TYPE:
784 case REFERENCE_TYPE:
6da794e8 785 /* For two pointers, do this recursively on the target type. */
8d08fdba 786 {
6da794e8 787 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
77adef84 788 int quals = cp_type_quals (t1);
5b163de4 789
8d08fdba 790 if (code1 == POINTER_TYPE)
62245e6f
JM
791 {
792 t1 = build_pointer_type (target);
793 if (TREE_CODE (target) == METHOD_TYPE)
794 t1 = build_ptrmemfunc_type (t1);
795 }
8d08fdba 796 else
86089be5 797 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
71851aaa 798 t1 = build_type_attribute_variant (t1, attributes);
77adef84 799 t1 = cp_build_qualified_type (t1, quals);
71851aaa 800
71851aaa 801 return t1;
8d08fdba 802 }
8d08fdba 803
6da794e8
JM
804 case OFFSET_TYPE:
805 {
fe0378ed
MM
806 int quals;
807 tree pointee;
808 quals = cp_type_quals (t1);
809 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
810 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
811 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
812 pointee);
813 t1 = cp_build_qualified_type (t1, quals);
6da794e8
JM
814 break;
815 }
816
8d08fdba
MS
817 case ARRAY_TYPE:
818 {
6da794e8 819 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
8d08fdba
MS
820 /* Save space: see if the result is identical to one of the args. */
821 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
2986ae00 822 return build_type_attribute_variant (t1, attributes);
8d08fdba 823 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
2986ae00 824 return build_type_attribute_variant (t2, attributes);
8d08fdba 825 /* Merge the element types, and have a size if either arg has one. */
e349ee73
MS
826 t1 = build_cplus_array_type
827 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
6da794e8 828 break;
8d08fdba
MS
829 }
830
831 case FUNCTION_TYPE:
832 /* Function types: prefer the one that specified arg types.
833 If both do, merge the arg types. Also merge the return types. */
834 {
6da794e8 835 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
8d08fdba
MS
836 tree p1 = TYPE_ARG_TYPES (t1);
837 tree p2 = TYPE_ARG_TYPES (t2);
a0685b73 838 tree parms;
8d08fdba 839 tree rval, raises;
cab421f4 840 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
8d08fdba
MS
841
842 /* Save space: see if the result is identical to one of the args. */
843 if (valtype == TREE_TYPE (t1) && ! p2)
e9525111 844 return cp_build_type_attribute_variant (t1, attributes);
8d08fdba 845 if (valtype == TREE_TYPE (t2) && ! p1)
e9525111 846 return cp_build_type_attribute_variant (t2, attributes);
8d08fdba
MS
847
848 /* Simple way if one arg fails to specify argument types. */
849 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
d8a0d13e 850 parms = p2;
a0685b73 851 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
d8a0d13e 852 parms = p1;
a0685b73 853 else
d8a0d13e 854 parms = commonparms (p1, p2);
8d08fdba 855
a0685b73
JM
856 rval = build_function_type (valtype, parms);
857 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
2eed8e37
BK
858 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
859 rval = apply_memfn_quals (rval,
860 type_memfn_quals (t1),
861 type_memfn_rqual (t1));
d8a0d13e 862 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
b15ea309 863 TYPE_RAISES_EXCEPTIONS (t2));
6da794e8 864 t1 = build_exception_variant (rval, raises);
cab421f4
PC
865 if (late_return_type_p)
866 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
6da794e8 867 break;
8d08fdba
MS
868 }
869
6da794e8
JM
870 case METHOD_TYPE:
871 {
872 /* Get this value the long way, since TYPE_METHOD_BASETYPE
873 is just the main variant of this. */
7e1352fe 874 tree basetype = class_of_this_parm (t2);
d8a0d13e 875 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
b15ea309 876 TYPE_RAISES_EXCEPTIONS (t2));
51543dc7 877 cp_ref_qualifier rqual = type_memfn_rqual (t1);
6da794e8 878 tree t3;
cab421f4
PC
879 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
880 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
6da794e8
JM
881
882 /* If this was a member function type, get back to the
883 original type of type member function (i.e., without
884 the class instance variable up front. */
885 t1 = build_function_type (TREE_TYPE (t1),
886 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
887 t2 = build_function_type (TREE_TYPE (t2),
888 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
889 t3 = merge_types (t1, t2);
43dc123f
MM
890 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
891 TYPE_ARG_TYPES (t3));
6da794e8 892 t1 = build_exception_variant (t3, raises);
51543dc7 893 t1 = build_ref_qualified_type (t1, rqual);
cab421f4
PC
894 if (late_return_type_1_p)
895 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
896 if (late_return_type_2_p)
897 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
6da794e8
JM
898 break;
899 }
8d08fdba 900
b1a95e0b
MM
901 case TYPENAME_TYPE:
902 /* There is no need to merge attributes into a TYPENAME_TYPE.
903 When the type is instantiated it will have whatever
904 attributes result from the instantiation. */
905 return t1;
906
6da794e8
JM
907 default:;
908 }
8e30dcf3
JM
909
910 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
911 return t1;
912 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
913 return t2;
914 else
915 return cp_build_type_attribute_variant (t1, attributes);
6da794e8 916}
8d08fdba 917
48326487
JM
918/* Return the ARRAY_TYPE type without its domain. */
919
920tree
921strip_array_domain (tree type)
922{
923 tree t2;
924 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
925 if (TYPE_DOMAIN (type) == NULL_TREE)
926 return type;
927 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
928 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
929}
930
98f2f3a2
MLI
931/* Wrapper around cp_common_type that is used by c-common.c and other
932 front end optimizations that remove promotions.
71851aaa 933
98f2f3a2
MLI
934 Return the common type for two arithmetic types T1 and T2 under the
935 usual arithmetic conversions. The default conversions have already
936 been applied, and enumerated types converted to their compatible
937 integer types. */
6da794e8
JM
938
939tree
5671bf27 940common_type (tree t1, tree t2)
6da794e8 941{
98f2f3a2
MLI
942 /* If one type is nonsense, use the other */
943 if (t1 == error_mark_node)
944 return t2;
945 if (t2 == error_mark_node)
946 return t1;
2986ae00 947
98f2f3a2
MLI
948 return cp_common_type (t1, t2);
949}
8d08fdba 950
98f2f3a2
MLI
951/* Return the common type of two pointer types T1 and T2. This is the
952 type for the result of most arithmetic operations if the operands
953 have the given two types.
954
955 We assume that comp_target_types has already been done and returned
956 nonzero; if that isn't so, this may crash. */
8d08fdba 957
98f2f3a2
MLI
958tree
959common_pointer_type (tree t1, tree t2)
960{
961 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
66b1156a 962 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
98f2f3a2
MLI
963 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
964
965 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
c86818cf 966 CPO_CONVERSION, tf_warning_or_error);
8d08fdba
MS
967}
968\f
4cc1d462 969/* Compare two exception specifier types for exactness or subsetness, if
acd8e2d0
NS
970 allowed. Returns false for mismatch, true for match (same, or
971 derived and !exact).
c8094d83 972
4cc1d462 973 [except.spec] "If a class X ... objects of class X or any class publicly
34cd5ae7 974 and unambiguously derived from X. Similarly, if a pointer type Y * ...
4cc1d462 975 exceptions of type Y * or that are pointers to any type publicly and
34cd5ae7 976 unambiguously derived from Y. Otherwise a function only allows exceptions
4cc1d462
NS
977 that have the same type ..."
978 This does not mention cv qualifiers and is different to what throw
979 [except.throw] and catch [except.catch] will do. They will ignore the
980 top level cv qualifiers, and allow qualifiers in the pointer to class
981 example.
c8094d83 982
4cc1d462
NS
983 We implement the letter of the standard. */
984
acd8e2d0
NS
985static bool
986comp_except_types (tree a, tree b, bool exact)
4cc1d462
NS
987{
988 if (same_type_p (a, b))
acd8e2d0 989 return true;
4cc1d462
NS
990 else if (!exact)
991 {
89d684bb 992 if (cp_type_quals (a) || cp_type_quals (b))
0cbd7506 993 return false;
c8094d83 994
50e10fa8 995 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
0cbd7506
MS
996 {
997 a = TREE_TYPE (a);
998 b = TREE_TYPE (b);
999 if (cp_type_quals (a) || cp_type_quals (b))
1000 return false;
1001 }
c8094d83 1002
4cc1d462 1003 if (TREE_CODE (a) != RECORD_TYPE
0cbd7506
MS
1004 || TREE_CODE (b) != RECORD_TYPE)
1005 return false;
c8094d83 1006
22854930 1007 if (publicly_uniquely_derived_p (a, b))
0cbd7506 1008 return true;
4cc1d462 1009 }
acd8e2d0 1010 return false;
4cc1d462
NS
1011}
1012
acd8e2d0 1013/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
3a55fb4c 1014 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
51dc6603 1015 If EXACT is ce_type, the C++17 type compatibility rules apply.
3a55fb4c
JM
1016 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1017 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1018 are unordered, but we've already filtered out duplicates. Most lists will
1019 be in order, we should try to make use of that. */
e92cc029 1020
acd8e2d0 1021bool
3a55fb4c 1022comp_except_specs (const_tree t1, const_tree t2, int exact)
8d08fdba 1023{
58f9752a
KG
1024 const_tree probe;
1025 const_tree base;
4cc1d462
NS
1026 int length = 0;
1027
1028 if (t1 == t2)
acd8e2d0 1029 return true;
c8094d83 1030
6d812dd3
JM
1031 /* First handle noexcept. */
1032 if (exact < ce_exact)
3a55fb4c 1033 {
51dc6603
JM
1034 if (exact == ce_type
1035 && (canonical_eh_spec (CONST_CAST_TREE (t1))
1036 == canonical_eh_spec (CONST_CAST_TREE (t2))))
1037 return true;
1038
5e3f417f 1039 /* noexcept(false) is compatible with no exception-specification,
51dc6603 1040 and less strict than any spec. */
6d812dd3 1041 if (t1 == noexcept_false_spec)
5e3f417f
JM
1042 return t2 == NULL_TREE || exact == ce_derived;
1043 /* Even a derived noexcept(false) is compatible with no
1044 exception-specification. */
6d812dd3 1045 if (t2 == noexcept_false_spec)
5e3f417f 1046 return t1 == NULL_TREE;
6d812dd3
JM
1047
1048 /* Otherwise, if we aren't looking for an exact match, noexcept is
1049 equivalent to throw(). */
1050 if (t1 == noexcept_true_spec)
1051 t1 = empty_except_spec;
1052 if (t2 == noexcept_true_spec)
1053 t2 = empty_except_spec;
3a55fb4c
JM
1054 }
1055
6d812dd3
JM
1056 /* If any noexcept is left, it is only comparable to itself;
1057 either we're looking for an exact match or we're redeclaring a
1058 template with dependent noexcept. */
1059 if ((t1 && TREE_PURPOSE (t1))
1060 || (t2 && TREE_PURPOSE (t2)))
1061 return (t1 && t2
1062 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1063
0cbd7506 1064 if (t1 == NULL_TREE) /* T1 is ... */
3a55fb4c 1065 return t2 == NULL_TREE || exact == ce_derived;
0cbd7506 1066 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
4cc1d462 1067 return t2 != NULL_TREE && !TREE_VALUE (t2);
0cbd7506 1068 if (t2 == NULL_TREE) /* T2 is ... */
acd8e2d0 1069 return false;
dffa4176 1070 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
3a55fb4c 1071 return exact == ce_derived;
c8094d83 1072
4cc1d462
NS
1073 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1074 Count how many we find, to determine exactness. For exact matching and
1075 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1076 O(nm). */
1077 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1078 {
1079 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
0cbd7506
MS
1080 {
1081 tree a = TREE_VALUE (probe);
1082 tree b = TREE_VALUE (t2);
1083
1084 if (comp_except_types (a, b, exact))
1085 {
3a55fb4c 1086 if (probe == base && exact > ce_derived)
0cbd7506
MS
1087 base = TREE_CHAIN (probe);
1088 length++;
1089 break;
1090 }
1091 }
4cc1d462 1092 if (probe == NULL_TREE)
0cbd7506 1093 return false;
4cc1d462 1094 }
3a55fb4c 1095 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
8d08fdba
MS
1096}
1097
acd8e2d0
NS
1098/* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1099 [] can match [size]. */
3bfdc719 1100
c8a209ca 1101static bool
58f9752a 1102comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
8d08fdba 1103{
3bfdc719
MM
1104 tree d1;
1105 tree d2;
30a03508 1106 tree max1, max2;
3bfdc719
MM
1107
1108 if (t1 == t2)
acd8e2d0 1109 return true;
8d08fdba 1110
3bfdc719 1111 /* The type of the array elements must be the same. */
acd8e2d0 1112 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
c8a209ca 1113 return false;
8d08fdba 1114
3bfdc719
MM
1115 d1 = TYPE_DOMAIN (t1);
1116 d2 = TYPE_DOMAIN (t2);
1117
1118 if (d1 == d2)
c8a209ca 1119 return true;
8d08fdba 1120
3bfdc719 1121 /* If one of the arrays is dimensionless, and the other has a
0e339752 1122 dimension, they are of different types. However, it is valid to
3bfdc719
MM
1123 write:
1124
1125 extern int a[];
1126 int a[3];
1127
c8094d83 1128 by [basic.link]:
3bfdc719
MM
1129
1130 declarations for an array object can specify
1131 array types that differ by the presence or absence of a major
1132 array bound (_dcl.array_). */
1133 if (!d1 || !d2)
acd8e2d0 1134 return allow_redeclaration;
3bfdc719
MM
1135
1136 /* Check that the dimensions are the same. */
30a03508
NS
1137
1138 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1139 return false;
1140 max1 = TYPE_MAX_VALUE (d1);
1141 max2 = TYPE_MAX_VALUE (d2);
30a03508
NS
1142
1143 if (!cp_tree_equal (max1, max2))
1144 return false;
1145
1146 return true;
8d08fdba
MS
1147}
1148
e96ce650
DS
1149/* Compare the relative position of T1 and T2 into their respective
1150 template parameter list.
1151 T1 and T2 must be template parameter types.
1152 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1153
1154static bool
1155comp_template_parms_position (tree t1, tree t2)
1156{
6f1abb06 1157 tree index1, index2;
e96ce650
DS
1158 gcc_assert (t1 && t2
1159 && TREE_CODE (t1) == TREE_CODE (t2)
1160 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1161 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1162 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1163
6f1abb06
DS
1164 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1165 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
9cf10655 1166
6f1abb06
DS
1167 /* Then compare their relative position. */
1168 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1169 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1170 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1171 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
9cf10655
DS
1172 return false;
1173
d08a068b
JM
1174 /* In C++14 we can end up comparing 'auto' to a normal template
1175 parameter. Don't confuse them. */
e4276ba5 1176 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
d08a068b
JM
1177 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1178
6f1abb06 1179 return true;
9cf10655
DS
1180}
1181
96d84882 1182/* Subroutine in comptypes. */
e92cc029 1183
06d40de8
DG
1184static bool
1185structural_comptypes (tree t1, tree t2, int strict)
8d08fdba 1186{
8d08fdba 1187 if (t1 == t2)
c8a209ca
NS
1188 return true;
1189
f4f206f4 1190 /* Suppress errors caused by previously reported errors. */
ae8803a8 1191 if (t1 == error_mark_node || t2 == error_mark_node)
c8a209ca 1192 return false;
c8094d83 1193
50bc768d 1194 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
c8094d83 1195
c8a209ca
NS
1196 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1197 current instantiation. */
1198 if (TREE_CODE (t1) == TYPENAME_TYPE)
4195a767 1199 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
c8094d83 1200
c8a209ca 1201 if (TREE_CODE (t2) == TYPENAME_TYPE)
4195a767 1202 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
8d08fdba 1203
5566b478
MS
1204 if (TYPE_PTRMEMFUNC_P (t1))
1205 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1206 if (TYPE_PTRMEMFUNC_P (t2))
1207 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1208
8d08fdba 1209 /* Different classes of types can't be compatible. */
8d08fdba 1210 if (TREE_CODE (t1) != TREE_CODE (t2))
c8a209ca 1211 return false;
8d08fdba 1212
c353b8e3
MM
1213 /* Qualifiers must match. For array types, we will check when we
1214 recur on the array element types. */
1215 if (TREE_CODE (t1) != ARRAY_TYPE
a3360e77
JM
1216 && cp_type_quals (t1) != cp_type_quals (t2))
1217 return false;
1218 if (TREE_CODE (t1) == FUNCTION_TYPE
1219 && type_memfn_quals (t1) != type_memfn_quals (t2))
c8a209ca 1220 return false;
2eed8e37
BK
1221 /* Need to check this before TYPE_MAIN_VARIANT.
1222 FIXME function qualifiers should really change the main variant. */
51dc6603
JM
1223 if (TREE_CODE (t1) == FUNCTION_TYPE
1224 || TREE_CODE (t1) == METHOD_TYPE)
1225 {
1226 if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
1227 return false;
1228 if (flag_noexcept_type
1229 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
1230 TYPE_RAISES_EXCEPTIONS (t2),
1231 ce_type))
1232 return false;
1233 }
8d08fdba
MS
1234
1235 /* Allow for two different type nodes which have essentially the same
1236 definition. Note that we already checked for equality of the type
38e01259 1237 qualifiers (just above). */
8d08fdba 1238
c353b8e3
MM
1239 if (TREE_CODE (t1) != ARRAY_TYPE
1240 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
c8a209ca 1241 return true;
8d08fdba 1242
20c202f3 1243
7866705a 1244 /* Compare the types. Break out if they could be the same. */
8d08fdba
MS
1245 switch (TREE_CODE (t1))
1246 {
0451301c
DG
1247 case VOID_TYPE:
1248 case BOOLEAN_TYPE:
1249 /* All void and bool types are the same. */
1250 break;
1251
1252 case INTEGER_TYPE:
1253 case FIXED_POINT_TYPE:
1254 case REAL_TYPE:
1255 /* With these nodes, we can't determine type equivalence by
1256 looking at what is stored in the nodes themselves, because
1257 two nodes might have different TYPE_MAIN_VARIANTs but still
1258 represent the same type. For example, wchar_t and int could
1259 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1260 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1261 and are distinct types. On the other hand, int and the
1262 following typedef
1263
1264 typedef int INT __attribute((may_alias));
1265
1266 have identical properties, different TYPE_MAIN_VARIANTs, but
1267 represent the same type. The canonical type system keeps
1268 track of equivalence in this case, so we fall back on it. */
1269 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1270
e7e66632 1271 case TEMPLATE_TEMPLATE_PARM:
a1281f45 1272 case BOUND_TEMPLATE_TEMPLATE_PARM:
e96ce650 1273 if (!comp_template_parms_position (t1, t2))
c8a209ca
NS
1274 return false;
1275 if (!comp_template_parms
1276 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1277 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1278 return false;
a1281f45 1279 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
7866705a 1280 break;
e7e66632 1281 /* Don't check inheritance. */
acd8e2d0 1282 strict = COMPARE_STRICT;
f4f206f4 1283 /* Fall through. */
e7e66632 1284
8d08fdba
MS
1285 case RECORD_TYPE:
1286 case UNION_TYPE:
7ddedda4
MM
1287 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1288 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
c8a209ca
NS
1289 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1290 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
7866705a 1291 break;
c8094d83 1292
acd8e2d0 1293 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
7866705a 1294 break;
acd8e2d0 1295 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
7866705a 1296 break;
c8094d83 1297
c8a209ca 1298 return false;
8d08fdba
MS
1299
1300 case OFFSET_TYPE:
96d84882
PB
1301 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1302 strict & ~COMPARE_REDECLARATION))
c8a209ca 1303 return false;
7866705a
SB
1304 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1305 return false;
1306 break;
8d08fdba 1307
8d08fdba 1308 case REFERENCE_TYPE:
8af2fec4
RY
1309 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1310 return false;
1311 /* fall through to checks for pointer types */
81fea426 1312 gcc_fallthrough ();
8af2fec4
RY
1313
1314 case POINTER_TYPE:
7866705a 1315 if (TYPE_MODE (t1) != TYPE_MODE (t2)
7866705a
SB
1316 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1317 return false;
1318 break;
8d08fdba 1319
dffa4176 1320 case METHOD_TYPE:
8d08fdba 1321 case FUNCTION_TYPE:
acd8e2d0 1322 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
c8a209ca 1323 return false;
7866705a
SB
1324 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1325 return false;
1326 break;
8d08fdba
MS
1327
1328 case ARRAY_TYPE:
9bcb9aae 1329 /* Target types must match incl. qualifiers. */
7866705a
SB
1330 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1331 return false;
1332 break;
8d08fdba 1333
51c184be 1334 case TEMPLATE_TYPE_PARM:
6f1abb06
DS
1335 /* If T1 and T2 don't have the same relative position in their
1336 template parameters set, they can't be equal. */
1337 if (!comp_template_parms_position (t1, t2))
1338 return false;
4fea442d
JM
1339 /* Constrained 'auto's are distinct from parms that don't have the same
1340 constraints. */
1341 if (!equivalent_placeholder_constraints (t1, t2))
1342 return false;
7866705a 1343 break;
e76a2646
MS
1344
1345 case TYPENAME_TYPE:
c8a209ca
NS
1346 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1347 TYPENAME_TYPE_FULLNAME (t2)))
0cbd7506 1348 return false;
69d28b4f
JM
1349 /* Qualifiers don't matter on scopes. */
1350 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1351 TYPE_CONTEXT (t2)))
7866705a
SB
1352 return false;
1353 break;
7f85441b 1354
b8c6534b 1355 case UNBOUND_CLASS_TEMPLATE:
c8a209ca 1356 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
0cbd7506 1357 return false;
7866705a
SB
1358 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1359 return false;
1360 break;
b8c6534b 1361
a7a64a77 1362 case COMPLEX_TYPE:
7866705a
SB
1363 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 break;
a7a64a77 1366
cc27e657 1367 case VECTOR_TYPE:
7866705a
SB
1368 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1369 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1370 return false;
cc27e657
PB
1371 break;
1372
5d80a306 1373 case TYPE_PACK_EXPANSION:
c67dd256
JM
1374 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1375 PACK_EXPANSION_PATTERN (t2))
1376 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1377 PACK_EXPANSION_EXTRA_ARGS (t2)));
5d80a306 1378
3ad6a8e1
DG
1379 case DECLTYPE_TYPE:
1380 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1381 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
d5f4eddd
JM
1382 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1383 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
9b8662c2
JM
1384 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1385 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
3ad6a8e1
DG
1386 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1387 DECLTYPE_TYPE_EXPR (t2)))
1388 return false;
1389 break;
1390
a0d260fc
PC
1391 case UNDERLYING_TYPE:
1392 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1393 UNDERLYING_TYPE_TYPE (t2));
1394
7f85441b 1395 default:
7866705a 1396 return false;
8d08fdba 1397 }
7866705a
SB
1398
1399 /* If we get here, we know that from a target independent POV the
1400 types are the same. Make sure the target attributes are also
1401 the same. */
ac9a30ae 1402 return comp_type_attributes (t1, t2);
8d08fdba
MS
1403}
1404
06d40de8
DG
1405/* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1406 is a bitwise-or of the COMPARE_* flags. */
1407
1408bool
96d84882 1409comptypes (tree t1, tree t2, int strict)
06d40de8
DG
1410{
1411 if (strict == COMPARE_STRICT)
1412 {
06d40de8
DG
1413 if (t1 == t2)
1414 return true;
1415
1416 if (t1 == error_mark_node || t2 == error_mark_node)
1417 return false;
1418
1419 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1420 /* At least one of the types requires structural equality, so
1421 perform a deep check. */
1422 return structural_comptypes (t1, t2, strict);
1423
595278be 1424 if (flag_checking && USE_CANONICAL_TYPES)
06d40de8 1425 {
7313518b
DG
1426 bool result = structural_comptypes (t1, t2, strict);
1427
06d40de8 1428 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
7313518b
DG
1429 /* The two types are structurally equivalent, but their
1430 canonical types were different. This is a failure of the
1431 canonical type propagation code.*/
1432 internal_error
0f2c4a8f 1433 ("canonical types differ for identical types %qT and %qT",
7313518b 1434 t1, t2);
06d40de8 1435 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
7313518b
DG
1436 /* Two types are structurally different, but the canonical
1437 types are the same. This means we were over-eager in
1438 assigning canonical types. */
1439 internal_error
0f2c4a8f 1440 ("same canonical type node for different types %qT and %qT",
7313518b 1441 t1, t2);
06d40de8
DG
1442
1443 return result;
1444 }
595278be 1445 if (!flag_checking && USE_CANONICAL_TYPES)
06d40de8 1446 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
7313518b
DG
1447 else
1448 return structural_comptypes (t1, t2, strict);
06d40de8
DG
1449 }
1450 else if (strict == COMPARE_STRUCTURAL)
1451 return structural_comptypes (t1, t2, COMPARE_STRICT);
1452 else
1453 return structural_comptypes (t1, t2, strict);
1454}
1455
10746f37
JM
1456/* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1457 top-level qualifiers. */
1458
1459bool
1460same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1461{
1462 if (type1 == error_mark_node || type2 == error_mark_node)
1463 return false;
1464
452811eb
JM
1465 type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
1466 type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
1467 return same_type_p (type1, type2);
10746f37
JM
1468}
1469
91063b51
MM
1470/* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1471
acd8e2d0 1472bool
58f9752a 1473at_least_as_qualified_p (const_tree type1, const_tree type2)
91063b51 1474{
acd8e2d0
NS
1475 int q1 = cp_type_quals (type1);
1476 int q2 = cp_type_quals (type2);
c8094d83 1477
91063b51 1478 /* All qualifiers for TYPE2 must also appear in TYPE1. */
acd8e2d0 1479 return (q1 & q2) == q2;
91063b51
MM
1480}
1481
ceab47eb
MM
1482/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1483 more cv-qualified that TYPE1, and 0 otherwise. */
1484
1485int
d02f620d 1486comp_cv_qualification (int q1, int q2)
ceab47eb 1487{
acd8e2d0 1488 if (q1 == q2)
ceab47eb
MM
1489 return 0;
1490
acd8e2d0 1491 if ((q1 & q2) == q2)
ceab47eb 1492 return 1;
acd8e2d0 1493 else if ((q1 & q2) == q1)
ceab47eb
MM
1494 return -1;
1495
1496 return 0;
1497}
1498
d02f620d
JM
1499int
1500comp_cv_qualification (const_tree type1, const_tree type2)
1501{
1502 int q1 = cp_type_quals (type1);
1503 int q2 = cp_type_quals (type2);
1504 return comp_cv_qualification (q1, q2);
1505}
1506
ceab47eb
MM
1507/* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1508 subset of the cv-qualification signature of TYPE2, and the types
1509 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1510
1511int
acd8e2d0 1512comp_cv_qual_signature (tree type1, tree type2)
ceab47eb
MM
1513{
1514 if (comp_ptr_ttypes_real (type2, type1, -1))
1515 return 1;
1516 else if (comp_ptr_ttypes_real (type1, type2, -1))
1517 return -1;
1518 else
1519 return 0;
1520}
8d08fdba 1521\f
96d84882 1522/* Subroutines of `comptypes'. */
8d08fdba 1523
acd8e2d0 1524/* Return true if two parameter type lists PARMS1 and PARMS2 are
03017874
MM
1525 equivalent in the sense that functions with those parameter types
1526 can have equivalent types. The two lists must be equivalent,
acd8e2d0 1527 element by element. */
8d08fdba 1528
acd8e2d0 1529bool
58f9752a 1530compparms (const_tree parms1, const_tree parms2)
8d08fdba 1531{
58f9752a 1532 const_tree t1, t2;
8d08fdba
MS
1533
1534 /* An unspecified parmlist matches any specified parmlist
1535 whose argument types don't need default promotions. */
1536
acd8e2d0
NS
1537 for (t1 = parms1, t2 = parms2;
1538 t1 || t2;
1539 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
8d08fdba 1540 {
8d08fdba 1541 /* If one parmlist is shorter than the other,
4daa9c14 1542 they fail to match. */
acd8e2d0
NS
1543 if (!t1 || !t2)
1544 return false;
ae8803a8 1545 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
acd8e2d0 1546 return false;
8d08fdba 1547 }
acd8e2d0 1548 return true;
8d08fdba
MS
1549}
1550
8d08fdba 1551\f
7a18b933
NS
1552/* Process a sizeof or alignof expression where the operand is a
1553 type. */
1554
8d08fdba 1555tree
7a18b933 1556cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
8d08fdba 1557{
fa72b064 1558 tree value;
b4c74ba2 1559 bool dependent_p;
8d08fdba 1560
50bc768d 1561 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
7a18b933
NS
1562 if (type == error_mark_node)
1563 return error_mark_node;
c8094d83 1564
ee76b931 1565 type = non_reference (type);
3c17e16e 1566 if (TREE_CODE (type) == METHOD_TYPE)
8d08fdba 1567 {
fcf73884 1568 if (complain)
44d90fe1 1569 pedwarn (input_location, OPT_Wpointer_arith,
fcf73884 1570 "invalid application of %qs to a member function",
3c17e16e 1571 operator_name_info[(int) op].name);
aa0a659c
PC
1572 else
1573 return error_mark_node;
fa72b064 1574 value = size_one_node;
8d08fdba 1575 }
8d08fdba 1576
b4c74ba2
MM
1577 dependent_p = dependent_type_p (type);
1578 if (!dependent_p)
1579 complete_type (type);
1580 if (dependent_p
3c17e16e
MM
1581 /* VLA types will have a non-constant size. In the body of an
1582 uninstantiated template, we don't need to try to compute the
1583 value, because the sizeof expression is not an integral
1584 constant expression in that case. And, if we do try to
1585 compute the value, we'll likely end up with SAVE_EXPRs, which
1586 the template substitution machinery does not expect to see. */
b4c74ba2
MM
1587 || (processing_template_decl
1588 && COMPLETE_TYPE_P (type)
1589 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
3c17e16e
MM
1590 {
1591 value = build_min (op, size_type_node, type);
1592 TREE_READONLY (value) = 1;
1593 return value;
1594 }
1595
c2255bc4 1596 return c_sizeof_or_alignof_type (input_location, complete_type (type),
296674db 1597 op == SIZEOF_EXPR, false,
3c17e16e 1598 complain);
8d08fdba
MS
1599}
1600
5ade1ed2
DG
1601/* Return the size of the type, without producing any warnings for
1602 types whose size cannot be taken. This routine should be used only
1603 in some other routine that has already produced a diagnostic about
1604 using the size of such a type. */
1605tree
1606cxx_sizeof_nowarn (tree type)
1607{
1608 if (TREE_CODE (type) == FUNCTION_TYPE
50e10fa8 1609 || VOID_TYPE_P (type)
5ade1ed2
DG
1610 || TREE_CODE (type) == ERROR_MARK)
1611 return size_one_node;
1612 else if (!COMPLETE_TYPE_P (type))
1613 return size_zero_node;
1614 else
1615 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1616}
1617
229970c4 1618/* Process a sizeof expression where the operand is an expression. */
7a18b933 1619
229970c4 1620static tree
5ade1ed2 1621cxx_sizeof_expr (tree e, tsubst_flags_t complain)
5566b478 1622{
7a18b933
NS
1623 if (e == error_mark_node)
1624 return error_mark_node;
c8094d83 1625
5156628f 1626 if (processing_template_decl)
7a18b933 1627 {
229970c4 1628 e = build_min (SIZEOF_EXPR, size_type_node, e);
7a18b933
NS
1629 TREE_SIDE_EFFECTS (e) = 0;
1630 TREE_READONLY (e) = 1;
c8094d83 1631
7a18b933
NS
1632 return e;
1633 }
c8094d83 1634
48326487
JM
1635 /* To get the size of a static data member declared as an array of
1636 unknown bound, we need to instantiate it. */
5a6ccc94 1637 if (VAR_P (e)
48326487
JM
1638 && VAR_HAD_UNKNOWN_BOUND (e)
1639 && DECL_TEMPLATE_INSTANTIATION (e))
1640 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1641
773ec47f
MP
1642 if (TREE_CODE (e) == PARM_DECL
1643 && DECL_ARRAY_PARAMETER_P (e)
1644 && (complain & tf_warning))
1645 {
1646 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1647 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1648 inform (DECL_SOURCE_LOCATION (e), "declared here");
1649 }
1650
03a904b5
JJ
1651 e = mark_type_use (e);
1652
47e5d7cc 1653 if (bitfield_p (e))
5566b478 1654 {
5ade1ed2
DG
1655 if (complain & tf_error)
1656 error ("invalid application of %<sizeof%> to a bit-field");
1657 else
1658 return error_mark_node;
7a18b933
NS
1659 e = char_type_node;
1660 }
1661 else if (is_overloaded_fn (e))
1662 {
5ade1ed2 1663 if (complain & tf_error)
cbe5f3b3 1664 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
37ec60ed 1665 "function type");
5ade1ed2
DG
1666 else
1667 return error_mark_node;
7a18b933 1668 e = char_type_node;
5566b478 1669 }
05e0b2f4
JM
1670 else if (type_unknown_p (e))
1671 {
5ade1ed2
DG
1672 if (complain & tf_error)
1673 cxx_incomplete_type_error (e, TREE_TYPE (e));
1674 else
1675 return error_mark_node;
7a18b933 1676 e = char_type_node;
05e0b2f4 1677 }
7a18b933
NS
1678 else
1679 e = TREE_TYPE (e);
c8094d83 1680
5ade1ed2 1681 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
5566b478 1682}
c8094d83 1683
229970c4 1684/* Implement the __alignof keyword: Return the minimum required
69d1a403 1685 alignment of E, measured in bytes. For VAR_DECL's and
229970c4
JM
1686 FIELD_DECL's return DECL_ALIGN (which can be set from an
1687 "aligned" __attribute__ specification). */
1688
1689static tree
5ade1ed2 1690cxx_alignof_expr (tree e, tsubst_flags_t complain)
229970c4
JM
1691{
1692 tree t;
3db45ab5 1693
229970c4
JM
1694 if (e == error_mark_node)
1695 return error_mark_node;
1696
1697 if (processing_template_decl)
1698 {
1699 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1700 TREE_SIDE_EFFECTS (e) = 0;
1701 TREE_READONLY (e) = 1;
1702
1703 return e;
1704 }
1705
03a904b5
JJ
1706 e = mark_type_use (e);
1707
5a6ccc94 1708 if (VAR_P (e))
229970c4 1709 t = size_int (DECL_ALIGN_UNIT (e));
47e5d7cc 1710 else if (bitfield_p (e))
229970c4 1711 {
5ade1ed2
DG
1712 if (complain & tf_error)
1713 error ("invalid application of %<__alignof%> to a bit-field");
1714 else
1715 return error_mark_node;
229970c4
JM
1716 t = size_one_node;
1717 }
1718 else if (TREE_CODE (e) == COMPONENT_REF
1719 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1720 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1721 else if (is_overloaded_fn (e))
1722 {
5ade1ed2 1723 if (complain & tf_error)
cbe5f3b3 1724 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
37ec60ed 1725 "function type");
5ade1ed2
DG
1726 else
1727 return error_mark_node;
837edd5f
GK
1728 if (TREE_CODE (e) == FUNCTION_DECL)
1729 t = size_int (DECL_ALIGN_UNIT (e));
1730 else
1731 t = size_one_node;
229970c4
JM
1732 }
1733 else if (type_unknown_p (e))
1734 {
5ade1ed2
DG
1735 if (complain & tf_error)
1736 cxx_incomplete_type_error (e, TREE_TYPE (e));
1737 else
1738 return error_mark_node;
229970c4
JM
1739 t = size_one_node;
1740 }
1741 else
5ade1ed2
DG
1742 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1743 complain & tf_error);
229970c4
JM
1744
1745 return fold_convert (size_type_node, t);
1746}
1747
69d1a403
JM
1748/* Process a sizeof or alignof expression E with code OP where the operand
1749 is an expression. */
229970c4
JM
1750
1751tree
5ade1ed2 1752cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
229970c4
JM
1753{
1754 if (op == SIZEOF_EXPR)
5ade1ed2 1755 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
229970c4 1756 else
5ade1ed2 1757 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
229970c4 1758}
e28d52cf
DS
1759
1760/* Build a representation of an expression 'alignas(E).' Return the
1761 folded integer value of E if it is an integral constant expression
1762 that resolves to a valid alignment. If E depends on a template
1763 parameter, return a syntactic representation tree of kind
1764 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1765 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1766
1767tree
1768cxx_alignas_expr (tree e)
1769{
1770 if (e == NULL_TREE || e == error_mark_node
1771 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1772 return e;
1773
1774 if (TYPE_P (e))
1775 /* [dcl.align]/3:
1776
1777 When the alignment-specifier is of the form
1778 alignas(type-id ), it shall have the same effect as
17365662 1779 alignas(alignof(type-id )). */
e28d52cf
DS
1780
1781 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1782
e28d52cf
DS
1783 /* If we reach this point, it means the alignas expression if of
1784 the form "alignas(assignment-expression)", so we should follow
1785 what is stated by [dcl.align]/2. */
1786
17365662
JM
1787 if (value_dependent_expression_p (e))
1788 /* Leave value-dependent expression alone for now. */
1789 return e;
1790
234bef96 1791 e = instantiate_non_dependent_expr (e);
e28d52cf
DS
1792 e = mark_rvalue_use (e);
1793
1794 /* [dcl.align]/2 says:
1795
1796 the assignment-expression shall be an integral constant
1797 expression. */
f46e2bc9
PC
1798
1799 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
1800 {
1801 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
1802 return error_mark_node;
1803 }
e28d52cf 1804
17365662 1805 return cxx_constant_value (e);
e28d52cf
DS
1806}
1807
8d08fdba 1808\f
c8b2e872
MM
1809/* EXPR is being used in a context that is not a function call.
1810 Enforce:
1811
c8094d83 1812 [expr.ref]
c8b2e872
MM
1813
1814 The expression can be used only as the left-hand operand of a
c8094d83 1815 member function call.
c8b2e872
MM
1816
1817 [expr.mptr.operator]
1818
1819 If the result of .* or ->* is a function, then that result can be
c8094d83 1820 used only as the operand for the function call operator ().
c8b2e872
MM
1821
1822 by issuing an error message if appropriate. Returns true iff EXPR
1823 violates these rules. */
1824
1825bool
d3ea4c06 1826invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
c8b2e872 1827{
6c74ff23
JM
1828 if (expr == NULL_TREE)
1829 return false;
1830 /* Don't enforce this in MS mode. */
1831 if (flag_ms_extensions)
1832 return false;
1833 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1834 expr = get_first_fn (expr);
1835 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
c8b2e872 1836 {
5ade1ed2 1837 if (complain & tf_error)
d3ea4c06
MLI
1838 {
1839 if (DECL_P (expr))
1840 {
1841 error_at (loc, "invalid use of non-static member function %qD",
1842 expr);
1843 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1844 }
1845 else
1846 error_at (loc, "invalid use of non-static member function of "
1847 "type %qT", TREE_TYPE (expr));
1848 }
c8b2e872
MM
1849 return true;
1850 }
1851 return false;
1852}
1853
38a4afee
MM
1854/* If EXP is a reference to a bitfield, and the type of EXP does not
1855 match the declared type of the bitfield, return the declared type
1856 of the bitfield. Otherwise, return NULL_TREE. */
1857
1858tree
58f9752a 1859is_bitfield_expr_with_lowered_type (const_tree exp)
38a4afee 1860{
f9aa54d3 1861 switch (TREE_CODE (exp))
38a4afee 1862 {
f9aa54d3 1863 case COND_EXPR:
42924ed7
JJ
1864 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1865 ? TREE_OPERAND (exp, 1)
1866 : TREE_OPERAND (exp, 0)))
38a4afee
MM
1867 return NULL_TREE;
1868 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
f9aa54d3
MM
1869
1870 case COMPOUND_EXPR:
1871 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1872
1873 case MODIFY_EXPR:
1874 case SAVE_EXPR:
1875 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1876
1877 case COMPONENT_REF:
1878 {
1879 tree field;
1880
1881 field = TREE_OPERAND (exp, 1);
e76d7cc7 1882 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
f9aa54d3
MM
1883 return NULL_TREE;
1884 if (same_type_ignoring_top_level_qualifiers_p
1885 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1886 return NULL_TREE;
1887 return DECL_BIT_FIELD_TYPE (field);
1888 }
1889
70f40fea
JJ
1890 case VAR_DECL:
1891 if (DECL_HAS_VALUE_EXPR_P (exp))
1892 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1893 (CONST_CAST_TREE (exp)));
1894 return NULL_TREE;
1895
5f9e56b3 1896 CASE_CONVERT:
be2b5483
RG
1897 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1898 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1899 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1900 /* Fallthrough. */
1901
f9aa54d3
MM
1902 default:
1903 return NULL_TREE;
38a4afee 1904 }
f9aa54d3
MM
1905}
1906
1907/* Like is_bitfield_with_lowered_type, except that if EXP is not a
1908 bitfield with a lowered type, the type of EXP is returned, rather
1909 than NULL_TREE. */
1910
1911tree
58f9752a 1912unlowered_expr_type (const_tree exp)
f9aa54d3
MM
1913{
1914 tree type;
c1e41527 1915 tree etype = TREE_TYPE (exp);
f9aa54d3
MM
1916
1917 type = is_bitfield_expr_with_lowered_type (exp);
c1e41527
JM
1918 if (type)
1919 type = cp_build_qualified_type (type, cp_type_quals (etype));
1920 else
1921 type = etype;
f9aa54d3
MM
1922
1923 return type;
38a4afee
MM
1924}
1925
0a72704b
MM
1926/* Perform the conversions in [expr] that apply when an lvalue appears
1927 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
56233bd6
JM
1928 function-to-pointer conversions. In addition, bitfield references are
1929 converted to their declared types. Note that this function does not perform
1930 the lvalue-to-rvalue conversion for class types. If you need that conversion
1931 for class types, then you probably need to use force_rvalue.
8d08fdba 1932
41990f96
MM
1933 Although the returned value is being used as an rvalue, this
1934 function does not wrap the returned expression in a
1935 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1936 that the return value is no longer an lvalue. */
8d08fdba
MS
1937
1938tree
1807ffc1
MS
1939decay_conversion (tree exp,
1940 tsubst_flags_t complain,
1941 bool reject_builtin /* = true */)
8d08fdba 1942{
926ce8bd
KH
1943 tree type;
1944 enum tree_code code;
8400e75e 1945 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
8d08fdba 1946
a359be75 1947 type = TREE_TYPE (exp);
07c88314
MM
1948 if (type == error_mark_node)
1949 return error_mark_node;
1950
46f0d909 1951 exp = resolve_nondeduced_context (exp, complain);
40260429
NS
1952 if (type_unknown_p (exp))
1953 {
89fcabaf
PC
1954 if (complain & tf_error)
1955 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
40260429
NS
1956 return error_mark_node;
1957 }
8d08fdba 1958
9bdae013
DS
1959 code = TREE_CODE (type);
1960
69eb4fde
JM
1961 if (error_operand_p (exp))
1962 return error_mark_node;
c8094d83 1963
ec62cbe1 1964 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
0c32fb95
JM
1965 return nullptr_node;
1966
8d08fdba
MS
1967 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1968 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
8d08fdba
MS
1969 if (code == VOID_TYPE)
1970 {
89fcabaf 1971 if (complain & tf_error)
5a3c9cf2 1972 error_at (loc, "void value not ignored as it ought to be");
8d08fdba
MS
1973 return error_mark_node;
1974 }
d3ea4c06 1975 if (invalid_nonstatic_memfn_p (loc, exp, complain))
c8b2e872 1976 return error_mark_node;
e6e174e5 1977 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
56233bd6
JM
1978 {
1979 exp = mark_lvalue_use (exp);
1980 if (reject_builtin && reject_gcc_builtin (exp, loc))
1981 return error_mark_node;
1982 return cp_build_addr_expr (exp, complain);
1983 }
8d08fdba
MS
1984 if (code == ARRAY_TYPE)
1985 {
926ce8bd 1986 tree adr;
8d08fdba 1987 tree ptrtype;
8d08fdba 1988
56233bd6
JM
1989 exp = mark_lvalue_use (exp);
1990
591cb3cf 1991 if (INDIRECT_REF_P (exp))
c8094d83 1992 return build_nop (build_pointer_type (TREE_TYPE (type)),
7b6d72fc 1993 TREE_OPERAND (exp, 0));
8d08fdba
MS
1994
1995 if (TREE_CODE (exp) == COMPOUND_EXPR)
1996 {
89fcabaf
PC
1997 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1998 if (op1 == error_mark_node)
1999 return error_mark_node;
f293ce4b
RS
2000 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
2001 TREE_OPERAND (exp, 0), op1);
8d08fdba
MS
2002 }
2003
bb19d4af 2004 if (!obvalue_p (exp)
8d08fdba
MS
2005 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
2006 {
89fcabaf 2007 if (complain & tf_error)
5a3c9cf2 2008 error_at (loc, "invalid use of non-lvalue array");
8d08fdba
MS
2009 return error_mark_node;
2010 }
2011
1f37c583
JM
2012 /* Don't let an array compound literal decay to a pointer. It can
2013 still be used to initialize an array or bind to a reference. */
2014 if (TREE_CODE (exp) == TARGET_EXPR)
2015 {
2016 if (complain & tf_error)
2017 error_at (loc, "taking address of temporary array");
2018 return error_mark_node;
2019 }
2020
01240200 2021 ptrtype = build_pointer_type (TREE_TYPE (type));
8d08fdba 2022
5a6ccc94 2023 if (VAR_P (exp))
8d08fdba 2024 {
dffd7eb6 2025 if (!cxx_mark_addressable (exp))
8d08fdba 2026 return error_mark_node;
0e8c9b28 2027 adr = build_nop (ptrtype, build_address (exp));
8d08fdba
MS
2028 return adr;
2029 }
2030 /* This way is better for a COMPONENT_REF since it can
2031 simplify the offset for a component. */
89fcabaf 2032 adr = cp_build_addr_expr (exp, complain);
4b978f96 2033 return cp_convert (ptrtype, adr, complain);
8d08fdba 2034 }
3db45ab5 2035
56233bd6
JM
2036 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2037 exp = mark_rvalue_use (exp, loc, reject_builtin);
2038
41990f96
MM
2039 /* If a bitfield is used in a context where integral promotion
2040 applies, then the caller is expected to have used
2041 default_conversion. That function promotes bitfields correctly
2042 before calling this function. At this point, if we have a
2043 bitfield referenced, we may assume that is not subject to
2044 promotion, and that, therefore, the type of the resulting rvalue
2045 is the declared type of the bitfield. */
2046 exp = convert_bitfield_to_declared_type (exp);
a9aedbc2 2047
41990f96
MM
2048 /* We do not call rvalue() here because we do not want to wrap EXP
2049 in a NON_LVALUE_EXPR. */
2050
2051 /* [basic.lval]
2052
2053 Non-class rvalues always have cv-unqualified types. */
2054 type = TREE_TYPE (exp);
36c37128 2055 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
164247b0 2056 exp = build_nop (cv_unqualified (type), exp);
ba9c33e8 2057
56233bd6
JM
2058 if (!complete_type_or_maybe_complain (type, exp, complain))
2059 return error_mark_node;
2060
a9aedbc2
MS
2061 return exp;
2062}
2063
39a13be5 2064/* Perform preparatory conversions, as part of the "usual arithmetic
41990f96
MM
2065 conversions". In particular, as per [expr]:
2066
2067 Whenever an lvalue expression appears as an operand of an
2068 operator that expects the rvalue for that operand, the
2069 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2070 standard conversions are applied to convert the expression to an
2071 rvalue.
2072
2073 In addition, we perform integral promotions here, as those are
2074 applied to both operands to a binary operator before determining
2075 what additional conversions should apply. */
2076
89fcabaf
PC
2077static tree
2078cp_default_conversion (tree exp, tsubst_flags_t complain)
a9aedbc2 2079{
40449a90
SL
2080 /* Check for target-specific promotions. */
2081 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2082 if (promoted_type)
4b978f96 2083 exp = cp_convert (promoted_type, exp, complain);
41990f96
MM
2084 /* Perform the integral promotions first so that bitfield
2085 expressions (which may promote to "int", even if the bitfield is
3db45ab5 2086 declared "unsigned") are promoted correctly. */
40449a90 2087 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
4b978f96 2088 exp = cp_perform_integral_promotions (exp, complain);
41990f96 2089 /* Perform the other conversions. */
89fcabaf 2090 exp = decay_conversion (exp, complain);
a9aedbc2 2091
8d08fdba
MS
2092 return exp;
2093}
6b5fbb55 2094
89fcabaf
PC
2095/* C version. */
2096
2097tree
2098default_conversion (tree exp)
2099{
2100 return cp_default_conversion (exp, tf_warning_or_error);
2101}
2102
0a72704b
MM
2103/* EXPR is an expression with an integral or enumeration type.
2104 Perform the integral promotions in [conv.prom], and return the
2105 converted value. */
2106
2107tree
4b978f96 2108cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
0a72704b
MM
2109{
2110 tree type;
2111 tree promoted_type;
2112
03a904b5
JJ
2113 expr = mark_rvalue_use (expr);
2114
efe1ad46
MM
2115 /* [conv.prom]
2116
2117 If the bitfield has an enumerated type, it is treated as any
2118 other value of that type for promotion purposes. */
2119 type = is_bitfield_expr_with_lowered_type (expr);
2120 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2121 type = TREE_TYPE (expr);
50bc768d 2122 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
3c395ecf
JM
2123 /* Scoped enums don't promote. */
2124 if (SCOPED_ENUM_P (type))
2125 return expr;
0a72704b
MM
2126 promoted_type = type_promotes_to (type);
2127 if (type != promoted_type)
4b978f96 2128 expr = cp_convert (promoted_type, expr, complain);
0a72704b
MM
2129 return expr;
2130}
2131
4b978f96
PC
2132/* C version. */
2133
2134tree
2135perform_integral_promotions (tree expr)
2136{
2137 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2138}
2139
d9cf7c82
JM
2140/* Returns nonzero iff exp is a STRING_CST or the result of applying
2141 decay_conversion to one. */
2142
2143int
58f9752a 2144string_conv_p (const_tree totype, const_tree exp, int warn)
d9cf7c82
JM
2145{
2146 tree t;
2147
50e10fa8 2148 if (!TYPE_PTR_P (totype))
d9cf7c82
JM
2149 return 0;
2150
2151 t = TREE_TYPE (totype);
3bfdc719 2152 if (!same_type_p (t, char_type_node)
b6baa67d
KVH
2153 && !same_type_p (t, char16_type_node)
2154 && !same_type_p (t, char32_type_node)
3bfdc719 2155 && !same_type_p (t, wchar_type_node))
d9cf7c82
JM
2156 return 0;
2157
848b92e1 2158 if (TREE_CODE (exp) == STRING_CST)
d9cf7c82 2159 {
b6baa67d 2160 /* Make sure that we don't try to convert between char and wide chars. */
6e176bd6 2161 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
848b92e1
JM
2162 return 0;
2163 }
2164 else
2165 {
2166 /* Is this a string constant which has decayed to 'const char *'? */
a3360e77 2167 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
3bfdc719 2168 if (!same_type_p (TREE_TYPE (exp), t))
d9cf7c82
JM
2169 return 0;
2170 STRIP_NOPS (exp);
2171 if (TREE_CODE (exp) != ADDR_EXPR
2172 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2173 return 0;
2174 }
b323323f 2175 if (warn)
aaf176ca
VV
2176 {
2177 if (cxx_dialect >= cxx11)
7278b0a2 2178 pedwarn (input_location, OPT_Wwrite_strings,
aaf176ca
VV
2179 "ISO C++ forbids converting a string constant to %qT",
2180 totype);
2181 else
2182 warning (OPT_Wwrite_strings,
2183 "deprecated conversion from string constant to %qT",
2184 totype);
2185 }
d9cf7c82
JM
2186
2187 return 1;
2188}
8d08fdba 2189
e9a25f70
JL
2190/* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2191 can, for example, use as an lvalue. This code used to be in
2192 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2193 expressions, where we're dealing with aggregates. But now it's again only
2194 called from unary_complex_lvalue. The case (in particular) that led to
2195 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2196 get it there. */
e92cc029 2197
8d08fdba 2198static tree
5ade1ed2
DG
2199rationalize_conditional_expr (enum tree_code code, tree t,
2200 tsubst_flags_t complain)
8d08fdba 2201{
8400e75e
DM
2202 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2203
e9a25f70
JL
2204 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2205 the first operand is always the one to be used if both operands
2206 are equal, so we know what conditional expression this used to be. */
2207 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2208 {
2a67bec2
ILT
2209 tree op0 = TREE_OPERAND (t, 0);
2210 tree op1 = TREE_OPERAND (t, 1);
2211
d211a298 2212 /* The following code is incorrect if either operand side-effects. */
2a67bec2
ILT
2213 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2214 && !TREE_SIDE_EFFECTS (op1));
e9a25f70 2215 return
8400e75e
DM
2216 build_conditional_expr (loc,
2217 build_x_binary_op (loc,
4fe977f2 2218 (TREE_CODE (t) == MIN_EXPR
e9a25f70 2219 ? LE_EXPR : GE_EXPR),
2a67bec2
ILT
2220 op0, TREE_CODE (op0),
2221 op1, TREE_CODE (op1),
6904f4b4 2222 /*overload=*/NULL,
5ade1ed2 2223 complain),
e51fbec3
MP
2224 cp_build_unary_op (code, op0, false, complain),
2225 cp_build_unary_op (code, op1, false, complain),
5ade1ed2 2226 complain);
e9a25f70
JL
2227 }
2228
8d08fdba 2229 return
8400e75e 2230 build_conditional_expr (loc, TREE_OPERAND (t, 0),
e51fbec3 2231 cp_build_unary_op (code, TREE_OPERAND (t, 1), false,
5ade1ed2 2232 complain),
e51fbec3 2233 cp_build_unary_op (code, TREE_OPERAND (t, 2), false,
5ade1ed2
DG
2234 complain),
2235 complain);
8d08fdba
MS
2236}
2237
9e9ff709
MS
2238/* Given the TYPE of an anonymous union field inside T, return the
2239 FIELD_DECL for the field. If not found return NULL_TREE. Because
2240 anonymous unions can nest, we must also search all anonymous unions
2241 that are directly reachable. */
e92cc029 2242
b3dd05b1 2243tree
acd8e2d0 2244lookup_anon_field (tree t, tree type)
9e9ff709
MS
2245{
2246 tree field;
2247
b75cebc4
MP
2248 t = TYPE_MAIN_VARIANT (t);
2249
910ad8de 2250 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
9e9ff709
MS
2251 {
2252 if (TREE_STATIC (field))
2253 continue;
17bbb839 2254 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
9e9ff709
MS
2255 continue;
2256
2257 /* If we find it directly, return the field. */
2258 if (DECL_NAME (field) == NULL_TREE
71631a1f 2259 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
9e9ff709
MS
2260 {
2261 return field;
2262 }
2263
2264 /* Otherwise, it could be nested, search harder. */
2265 if (DECL_NAME (field) == NULL_TREE
6bdb8141 2266 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
9e9ff709
MS
2267 {
2268 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2269 if (subfield)
2270 return subfield;
2271 }
2272 }
2273 return NULL_TREE;
2274}
2275
50ad9642
MM
2276/* Build an expression representing OBJECT.MEMBER. OBJECT is an
2277 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2278 non-NULL, it indicates the path to the base used to name MEMBER.
2279 If PRESERVE_REFERENCE is true, the expression returned will have
2280 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
c8094d83 2281 returned will have the type referred to by the reference.
50ad9642
MM
2282
2283 This function does not perform access control; that is either done
2284 earlier by the parser when the name of MEMBER is resolved to MEMBER
2285 itself, or later when overload resolution selects one of the
2286 functions indicated by MEMBER. */
e92cc029 2287
8d08fdba 2288tree
e87eed2a 2289build_class_member_access_expr (cp_expr object, tree member,
5ade1ed2
DG
2290 tree access_path, bool preserve_reference,
2291 tsubst_flags_t complain)
8d08fdba 2292{
50ad9642
MM
2293 tree object_type;
2294 tree member_scope;
2295 tree result = NULL_TREE;
557831a9 2296 tree using_decl = NULL_TREE;
8d08fdba 2297
2e5d2970 2298 if (error_operand_p (object) || error_operand_p (member))
01240200 2299 return error_mark_node;
5566b478 2300
50bc768d 2301 gcc_assert (DECL_P (member) || BASELINK_P (member));
8d08fdba 2302
50ad9642 2303 /* [expr.ref]
5566b478 2304
50ad9642
MM
2305 The type of the first expression shall be "class object" (of a
2306 complete type). */
2307 object_type = TREE_TYPE (object);
c8094d83 2308 if (!currently_open_class (object_type)
309714d4 2309 && !complete_type_or_maybe_complain (object_type, object, complain))
50ad9642
MM
2310 return error_mark_node;
2311 if (!CLASS_TYPE_P (object_type))
fc378698 2312 {
5ade1ed2 2313 if (complain & tf_error)
a7248d5f
PC
2314 {
2315 if (POINTER_TYPE_P (object_type)
2316 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2317 error ("request for member %qD in %qE, which is of pointer "
2318 "type %qT (maybe you meant to use %<->%> ?)",
e87eed2a 2319 member, object.get_value (), object_type);
a7248d5f
PC
2320 else
2321 error ("request for member %qD in %qE, which is of non-class "
e87eed2a 2322 "type %qT", member, object.get_value (), object_type);
a7248d5f 2323 }
50ad9642 2324 return error_mark_node;
fc378698 2325 }
8d08fdba 2326
50ad9642
MM
2327 /* The standard does not seem to actually say that MEMBER must be a
2328 member of OBJECT_TYPE. However, that is clearly what is
2329 intended. */
2330 if (DECL_P (member))
8d08fdba 2331 {
50ad9642 2332 member_scope = DECL_CLASS_CONTEXT (member);
9f635aba
PC
2333 if (!mark_used (member, complain) && !(complain & tf_error))
2334 return error_mark_node;
50ad9642 2335 if (TREE_DEPRECATED (member))
9b86d6bb 2336 warn_deprecated_use (member, NULL_TREE);
50ad9642
MM
2337 }
2338 else
0197270c 2339 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
50ad9642
MM
2340 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2341 presently be the anonymous union. Go outwards until we find a
2342 type related to OBJECT_TYPE. */
8d0d1915 2343 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
50ad9642
MM
2344 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2345 object_type))
2346 member_scope = TYPE_CONTEXT (member_scope);
2347 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2348 {
5ade1ed2
DG
2349 if (complain & tf_error)
2350 {
2351 if (TREE_CODE (member) == FIELD_DECL)
2352 error ("invalid use of nonstatic data member %qE", member);
2353 else
2354 error ("%qD is not a member of %qT", member, object_type);
2355 }
50ad9642 2356 return error_mark_node;
8d08fdba 2357 }
8d08fdba 2358
f576dfc4
JM
2359 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2360 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
3b426391 2361 in the front end; only _DECLs and _REFs are lvalues in the back end. */
f576dfc4
JM
2362 {
2363 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2364 if (temp)
dd865ef6 2365 object = cp_build_indirect_ref (temp, RO_NULL, complain);
f576dfc4 2366 }
2050a1bb 2367
50ad9642
MM
2368 /* In [expr.ref], there is an explicit list of the valid choices for
2369 MEMBER. We check for each of those cases here. */
5a6ccc94 2370 if (VAR_P (member))
8d08fdba 2371 {
50ad9642
MM
2372 /* A static data member. */
2373 result = member;
2aa64966 2374 mark_exp_read (object);
50ad9642
MM
2375 /* If OBJECT has side-effects, they are supposed to occur. */
2376 if (TREE_SIDE_EFFECTS (object))
f293ce4b 2377 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
8d08fdba 2378 }
50ad9642
MM
2379 else if (TREE_CODE (member) == FIELD_DECL)
2380 {
2381 /* A non-static data member. */
2382 bool null_object_p;
2383 int type_quals;
2384 tree member_type;
8d08fdba 2385
1a161cd7
MS
2386 if (INDIRECT_REF_P (object))
2387 null_object_p =
2388 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2389 else
2390 null_object_p = false;
8d08fdba 2391
50ad9642 2392 /* Convert OBJECT to the type of MEMBER. */
bdaa131b
JM
2393 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2394 TYPE_MAIN_VARIANT (member_scope)))
8d08fdba 2395 {
50ad9642
MM
2396 tree binfo;
2397 base_kind kind;
2398
2399 binfo = lookup_base (access_path ? access_path : object_type,
22854930 2400 member_scope, ba_unique, &kind, complain);
50ad9642
MM
2401 if (binfo == error_mark_node)
2402 return error_mark_node;
2403
bdaa131b 2404 /* It is invalid to try to get to a virtual base of a
50ad9642
MM
2405 NULL object. The most common cause is invalid use of
2406 offsetof macro. */
2407 if (null_object_p && kind == bk_via_virtual)
2408 {
5ade1ed2
DG
2409 if (complain & tf_error)
2410 {
8591575f
JM
2411 error ("invalid access to non-static data member %qD in "
2412 "virtual base of NULL object", member);
5ade1ed2 2413 }
50ad9642
MM
2414 return error_mark_node;
2415 }
2416
2417 /* Convert to the base. */
c8094d83 2418 object = build_base_path (PLUS_EXPR, object, binfo,
a271590a 2419 /*nonnull=*/1, complain);
50ad9642
MM
2420 /* If we found the base successfully then we should be able
2421 to convert to it successfully. */
50bc768d 2422 gcc_assert (object != error_mark_node);
8d08fdba 2423 }
3b5b4904 2424
50ad9642
MM
2425 /* If MEMBER is from an anonymous aggregate, we have converted
2426 OBJECT so that it refers to the class containing the
2427 anonymous union. Generate a reference to the anonymous union
2428 itself, and recur to find MEMBER. */
0ca7178c 2429 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
a723baf1
MM
2430 /* When this code is called from build_field_call, the
2431 object already has the type of the anonymous union.
2432 That is because the COMPONENT_REF was already
2433 constructed, and was then disassembled before calling
2434 build_field_call. After the function-call code is
2435 cleaned up, this waste can be eliminated. */
c8094d83 2436 && (!same_type_ignoring_top_level_qualifiers_p
a723baf1 2437 (TREE_TYPE (object), DECL_CONTEXT (member))))
50ad9642
MM
2438 {
2439 tree anonymous_union;
2440
2441 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2442 DECL_CONTEXT (member));
2443 object = build_class_member_access_expr (object,
2444 anonymous_union,
2445 /*access_path=*/NULL_TREE,
5ade1ed2
DG
2446 preserve_reference,
2447 complain);
50ad9642
MM
2448 }
2449
2450 /* Compute the type of the field, as described in [expr.ref]. */
2451 type_quals = TYPE_UNQUALIFIED;
2452 member_type = TREE_TYPE (member);
2453 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2454 {
c8094d83 2455 type_quals = (cp_type_quals (member_type)
50ad9642 2456 | cp_type_quals (object_type));
c8094d83 2457
50ad9642
MM
2458 /* A field is const (volatile) if the enclosing object, or the
2459 field itself, is const (volatile). But, a mutable field is
2460 not const, even within a const object. */
2461 if (DECL_MUTABLE_P (member))
2462 type_quals &= ~TYPE_QUAL_CONST;
2463 member_type = cp_build_qualified_type (member_type, type_quals);
2464 }
2465
35228ac7
JJ
2466 result = build3_loc (input_location, COMPONENT_REF, member_type,
2467 object, member, NULL_TREE);
50ad9642
MM
2468
2469 /* Mark the expression const or volatile, as appropriate. Even
2470 though we've dealt with the type above, we still have to mark the
2471 expression itself. */
2472 if (type_quals & TYPE_QUAL_CONST)
2473 TREE_READONLY (result) = 1;
12a669d1 2474 if (type_quals & TYPE_QUAL_VOLATILE)
50ad9642
MM
2475 TREE_THIS_VOLATILE (result) = 1;
2476 }
2477 else if (BASELINK_P (member))
9e9ff709 2478 {
50ad9642
MM
2479 /* The member is a (possibly overloaded) member function. */
2480 tree functions;
a723baf1 2481 tree type;
50ad9642
MM
2482
2483 /* If the MEMBER is exactly one static member function, then we
2484 know the type of the expression. Otherwise, we must wait
2485 until overload resolution has been performed. */
2486 functions = BASELINK_FUNCTIONS (member);
2487 if (TREE_CODE (functions) == FUNCTION_DECL
2488 && DECL_STATIC_FUNCTION_P (functions))
a723baf1 2489 type = TREE_TYPE (functions);
50ad9642 2490 else
a723baf1
MM
2491 type = unknown_type_node;
2492 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2493 base. That will happen when the function is called. */
f293ce4b 2494 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
9e9ff709 2495 }
50ad9642 2496 else if (TREE_CODE (member) == CONST_DECL)
b928a651 2497 {
50ad9642
MM
2498 /* The member is an enumerator. */
2499 result = member;
2500 /* If OBJECT has side-effects, they are supposed to occur. */
2501 if (TREE_SIDE_EFFECTS (object))
f293ce4b
RS
2502 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2503 object, result);
b928a651 2504 }
557831a9
FC
2505 else if ((using_decl = strip_using_decl (member)) != member)
2506 result = build_class_member_access_expr (object,
2507 using_decl,
2508 access_path, preserve_reference,
2509 complain);
8d08fdba
MS
2510 else
2511 {
5ade1ed2
DG
2512 if (complain & tf_error)
2513 error ("invalid use of %qD", member);
50ad9642
MM
2514 return error_mark_node;
2515 }
8d08fdba 2516
50ad9642
MM
2517 if (!preserve_reference)
2518 /* [expr.ref]
c8094d83 2519
50ad9642
MM
2520 If E2 is declared to have type "reference to T", then ... the
2521 type of E1.E2 is T. */
2522 result = convert_from_reference (result);
e6f62286 2523
50ad9642
MM
2524 return result;
2525}
8d08fdba 2526
05134211
JM
2527/* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2528 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
a723baf1
MM
2529
2530static tree
76545796
PC
2531lookup_destructor (tree object, tree scope, tree dtor_name,
2532 tsubst_flags_t complain)
a723baf1
MM
2533{
2534 tree object_type = TREE_TYPE (object);
2535 tree dtor_type = TREE_OPERAND (dtor_name, 0);
4546865e 2536 tree expr;
a723baf1 2537
9ca57929
PC
2538 /* We've already complained about this destructor. */
2539 if (dtor_type == error_mark_node)
2540 return error_mark_node;
2541
1b3d28a8 2542 if (scope && !check_dtor_name (scope, dtor_type))
a723baf1 2543 {
76545796
PC
2544 if (complain & tf_error)
2545 error ("qualified type %qT does not match destructor name ~%qT",
2546 scope, dtor_type);
a723baf1
MM
2547 return error_mark_node;
2548 }
3df70c62
JM
2549 if (is_auto (dtor_type))
2550 dtor_type = object_type;
2551 else if (identifier_p (dtor_type))
05134211
JM
2552 {
2553 /* In a template, names we can't find a match for are still accepted
2554 destructor names, and we check them here. */
2555 if (check_dtor_name (object_type, dtor_type))
2556 dtor_type = object_type;
2557 else
2558 {
76545796
PC
2559 if (complain & tf_error)
2560 error ("object type %qT does not match destructor name ~%qT",
2561 object_type, dtor_type);
05134211
JM
2562 return error_mark_node;
2563 }
2564
2565 }
2566 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
a723baf1 2567 {
76545796
PC
2568 if (complain & tf_error)
2569 error ("the type being destroyed is %qT, but the destructor "
2570 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
a723baf1
MM
2571 return error_mark_node;
2572 }
4546865e 2573 expr = lookup_member (dtor_type, complete_dtor_identifier,
db422ace
PC
2574 /*protect=*/1, /*want_type=*/false,
2575 tf_warning_or_error);
e383e095
KT
2576 if (!expr)
2577 {
2578 if (complain & tf_error)
2579 cxx_incomplete_type_error (dtor_name, dtor_type);
2580 return error_mark_node;
2581 }
4546865e
MM
2582 expr = (adjust_result_of_qualified_name_lookup
2583 (expr, dtor_type, object_type));
917c1602
JM
2584 if (scope == NULL_TREE)
2585 /* We need to call adjust_result_of_qualified_name_lookup in case the
2586 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2587 that we still get virtual function binding. */
2588 BASELINK_QUALIFIED_P (expr) = false;
4546865e 2589 return expr;
a723baf1
MM
2590}
2591
02ed62dd
MM
2592/* An expression of the form "A::template B" has been resolved to
2593 DECL. Issue a diagnostic if B is not a template or template
2594 specialization. */
2595
2596void
2597check_template_keyword (tree decl)
2598{
2599 /* The standard says:
2600
2601 [temp.names]
2602
2603 If a name prefixed by the keyword template is not a member
2604 template, the program is ill-formed.
2605
2606 DR 228 removed the restriction that the template be a member
3db45ab5
MS
2607 template.
2608
dd36d4e1 2609 DR 96, if accepted would add the further restriction that explicit
02ed62dd
MM
2610 template arguments must be provided if the template keyword is
2611 used, but, as of 2005-10-16, that DR is still in "drafting". If
2612 this DR is accepted, then the semantic checks here can be
2613 simplified, as the entity named must in fact be a template
2614 specialization, rather than, as at present, a set of overloaded
2615 functions containing at least one template function. */
2616 if (TREE_CODE (decl) != TEMPLATE_DECL
2617 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2618 {
38d795d2
PP
2619 if (VAR_P (decl))
2620 {
2621 if (DECL_USE_TEMPLATE (decl)
2622 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2623 ;
2624 else
2625 permerror (input_location, "%qD is not a template", decl);
2626 }
2627 else if (!is_overloaded_fn (decl))
cbe5f3b3 2628 permerror (input_location, "%qD is not a template", decl);
02ed62dd
MM
2629 else
2630 {
2631 tree fns;
459051a0
MM
2632 fns = decl;
2633 if (BASELINK_P (fns))
2634 fns = BASELINK_FUNCTIONS (fns);
02ed62dd
MM
2635 while (fns)
2636 {
2637 tree fn = OVL_CURRENT (fns);
2638 if (TREE_CODE (fn) == TEMPLATE_DECL
2639 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2640 break;
2641 if (TREE_CODE (fn) == FUNCTION_DECL
2642 && DECL_USE_TEMPLATE (fn)
2643 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2644 break;
2645 fns = OVL_NEXT (fns);
2646 }
2647 if (!fns)
cbe5f3b3 2648 permerror (input_location, "%qD is not a template", decl);
02ed62dd
MM
2649 }
2650 }
2651}
2652
50ad9642
MM
2653/* This function is called by the parser to process a class member
2654 access expression of the form OBJECT.NAME. NAME is a node used by
2655 the parser to represent a name; it is not yet a DECL. It may,
2656 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2657 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2658 there is no reason to do the lookup twice, so the parser keeps the
02ed62dd
MM
2659 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2660 be a template via the use of the "A::template B" syntax. */
8d08fdba 2661
50ad9642 2662tree
e87eed2a 2663finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
5ade1ed2 2664 tsubst_flags_t complain)
50ad9642 2665{
d17811fd 2666 tree expr;
50ad9642
MM
2667 tree object_type;
2668 tree member;
2669 tree access_path = NULL_TREE;
d17811fd
MM
2670 tree orig_object = object;
2671 tree orig_name = name;
3c8c2a0a 2672
50ad9642
MM
2673 if (object == error_mark_node || name == error_mark_node)
2674 return error_mark_node;
3c8c2a0a 2675
e58a9aa1
ZL
2676 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2677 if (!objc_is_public (object, name))
2678 return error_mark_node;
2679
d17811fd
MM
2680 object_type = TREE_TYPE (object);
2681
50ad9642 2682 if (processing_template_decl)
d17811fd 2683 {
a2b4cfaa 2684 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
23cb7266 2685 type_dependent_object_expression_p (object)
d17811fd
MM
2686 /* If NAME is "f<args>", where either 'f' or 'args' is
2687 dependent, then the expression is dependent. */
2688 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2689 && dependent_template_id_p (TREE_OPERAND (name, 0),
2690 TREE_OPERAND (name, 1)))
2691 /* If NAME is "T::X" where "T" is dependent, then the
2692 expression is dependent. */
2693 || (TREE_CODE (name) == SCOPE_REF
2694 && TYPE_P (TREE_OPERAND (name, 0))
23cb7266
JM
2695 && dependent_scope_p (TREE_OPERAND (name, 0))))
2696 {
2697 dependent:
2698 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
99676625 2699 orig_object, orig_name, NULL_TREE);
23cb7266 2700 }
d17811fd
MM
2701 object = build_non_dependent_expr (object);
2702 }
668ea4b1 2703 else if (c_dialect_objc ()
9dc6f476 2704 && identifier_p (name)
46a88c12 2705 && (expr = objc_maybe_build_component_ref (object, name)))
668ea4b1
IS
2706 return expr;
2707
50ad9642 2708 /* [expr.ref]
e23bd218 2709
50ad9642
MM
2710 The type of the first expression shall be "class object" (of a
2711 complete type). */
c8094d83 2712 if (!currently_open_class (object_type)
309714d4 2713 && !complete_type_or_maybe_complain (object_type, object, complain))
50ad9642
MM
2714 return error_mark_node;
2715 if (!CLASS_TYPE_P (object_type))
2716 {
5ade1ed2 2717 if (complain & tf_error)
a7248d5f
PC
2718 {
2719 if (POINTER_TYPE_P (object_type)
2720 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2721 error ("request for member %qD in %qE, which is of pointer "
2722 "type %qT (maybe you meant to use %<->%> ?)",
e87eed2a 2723 name, object.get_value (), object_type);
a7248d5f
PC
2724 else
2725 error ("request for member %qD in %qE, which is of non-class "
e87eed2a 2726 "type %qT", name, object.get_value (), object_type);
a7248d5f 2727 }
50ad9642
MM
2728 return error_mark_node;
2729 }
f2d9afec 2730
50ad9642 2731 if (BASELINK_P (name))
fdeff563
NS
2732 /* A member function that has already been looked up. */
2733 member = name;
50ad9642
MM
2734 else
2735 {
2736 bool is_template_id = false;
2737 tree template_args = NULL_TREE;
e6b8075c 2738 tree scope = NULL_TREE;
2ee887f2 2739
e6b8075c 2740 access_path = object_type;
f2d9afec 2741
50ad9642
MM
2742 if (TREE_CODE (name) == SCOPE_REF)
2743 {
e13d2b4e
MM
2744 /* A qualified name. The qualifying class or namespace `S'
2745 has already been looked up; it is either a TYPE or a
2746 NAMESPACE_DECL. */
50ad9642
MM
2747 scope = TREE_OPERAND (name, 0);
2748 name = TREE_OPERAND (name, 1);
50ad9642
MM
2749
2750 /* If SCOPE is a namespace, then the qualified name does not
2751 name a member of OBJECT_TYPE. */
2752 if (TREE_CODE (scope) == NAMESPACE_DECL)
2ee887f2 2753 {
5ade1ed2
DG
2754 if (complain & tf_error)
2755 error ("%<%D::%D%> is not a member of %qT",
2756 scope, name, object_type);
2ee887f2
MS
2757 return error_mark_node;
2758 }
e6b8075c
NS
2759 }
2760
2761 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2762 {
2763 is_template_id = true;
2764 template_args = TREE_OPERAND (name, 1);
2765 name = TREE_OPERAND (name, 0);
2766
2767 if (TREE_CODE (name) == OVERLOAD)
2768 name = DECL_NAME (get_first_fn (name));
2769 else if (DECL_P (name))
2770 name = DECL_NAME (name);
2771 }
50ad9642 2772
e6b8075c
NS
2773 if (scope)
2774 {
c53d966d
JM
2775 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2776 {
e6b8075c 2777 gcc_assert (!is_template_id);
c53d966d
JM
2778 /* Looking up a member enumerator (c++/56793). */
2779 if (!TYPE_CLASS_SCOPE_P (scope)
2780 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2781 {
2782 if (complain & tf_error)
2783 error ("%<%D::%D%> is not a member of %qT",
2784 scope, name, object_type);
2785 return error_mark_node;
2786 }
2787 tree val = lookup_enumerator (scope, name);
09b59d4d
NS
2788 if (!val)
2789 {
2790 if (complain & tf_error)
2791 error ("%qD is not a member of %qD",
2792 name, scope);
2793 return error_mark_node;
2794 }
2795
c53d966d
JM
2796 if (TREE_SIDE_EFFECTS (object))
2797 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2798 return val;
2799 }
2800
e13d2b4e 2801 gcc_assert (CLASS_TYPE_P (scope));
9dc6f476 2802 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
e13d2b4e 2803
abd5730b
JM
2804 if (constructor_name_p (name, scope))
2805 {
2806 if (complain & tf_error)
2807 error ("cannot call constructor %<%T::%D%> directly",
2808 scope, name);
2809 return error_mark_node;
2810 }
2811
50ad9642 2812 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
22854930
PC
2813 access_path = lookup_base (object_type, scope, ba_check,
2814 NULL, complain);
c8a65a25 2815 if (access_path == error_mark_node)
50ad9642 2816 return error_mark_node;
c8a65a25
MM
2817 if (!access_path)
2818 {
92354dc7
JM
2819 if (any_dependent_bases_p (object_type))
2820 goto dependent;
5ade1ed2
DG
2821 if (complain & tf_error)
2822 error ("%qT is not a base of %qT", scope, object_type);
c8a65a25
MM
2823 return error_mark_node;
2824 }
2ee887f2 2825 }
d17811fd
MM
2826
2827 if (TREE_CODE (name) == BIT_NOT_EXPR)
23cb7266
JM
2828 {
2829 if (dependent_type_p (object_type))
2830 /* The destructor isn't declared yet. */
2831 goto dependent;
2832 member = lookup_destructor (object, scope, name, complain);
2833 }
d17811fd 2834 else
9e9ff709 2835 {
d17811fd 2836 /* Look up the member. */
c8094d83 2837 member = lookup_member (access_path, name, /*protect=*/1,
db422ace 2838 /*want_type=*/false, complain);
8a5f4379
GDR
2839 if (member == NULL_TREE)
2840 {
23cb7266
JM
2841 if (dependent_type_p (object_type))
2842 /* Try again at instantiation time. */
2843 goto dependent;
5ade1ed2 2844 if (complain & tf_error)
8ece8dfb
DM
2845 {
2846 tree guessed_id = lookup_member_fuzzy (access_path, name,
2847 /*want_type=*/false);
2848 if (guessed_id)
bc1aee87
DM
2849 {
2850 location_t bogus_component_loc = input_location;
264757fb
DM
2851 gcc_rich_location rich_loc (bogus_component_loc);
2852 rich_loc.add_fixit_misspelled_id (bogus_component_loc,
2853 guessed_id);
bc1aee87
DM
2854 error_at_rich_loc
2855 (&rich_loc,
2856 "%q#T has no member named %qE; did you mean %qE?",
2857 TREE_CODE (access_path) == TREE_BINFO
2858 ? TREE_TYPE (access_path) : object_type, name,
2859 guessed_id);
2860 }
8ece8dfb
DM
2861 else
2862 error ("%q#T has no member named %qE",
2863 TREE_CODE (access_path) == TREE_BINFO
2864 ? TREE_TYPE (access_path) : object_type, name);
2865 }
8a5f4379
GDR
2866 return error_mark_node;
2867 }
d17811fd 2868 if (member == error_mark_node)
50ad9642 2869 return error_mark_node;
f3365c12
JM
2870 if (DECL_P (member)
2871 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
2872 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2873 wrong, so don't use it. */
2874 goto dependent;
23cb7266
JM
2875 if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
2876 goto dependent;
50ad9642 2877 }
c8094d83 2878
50ad9642
MM
2879 if (is_template_id)
2880 {
be93747e 2881 tree templ = member;
c8094d83 2882
be93747e 2883 if (BASELINK_P (templ))
8b1346a8
JM
2884 member = lookup_template_function (templ, template_args);
2885 else if (variable_template_p (templ))
2886 member = (lookup_and_finish_template_variable
2887 (templ, template_args, complain));
50ad9642
MM
2888 else
2889 {
5ade1ed2
DG
2890 if (complain & tf_error)
2891 error ("%qD is not a member template function", name);
50ad9642
MM
2892 return error_mark_node;
2893 }
9e9ff709
MS
2894 }
2895 }
2896
a723baf1 2897 if (TREE_DEPRECATED (member))
9b86d6bb 2898 warn_deprecated_use (member, NULL_TREE);
a723baf1 2899
02ed62dd
MM
2900 if (template_p)
2901 check_template_keyword (member);
2902
d17811fd 2903 expr = build_class_member_access_expr (object, member, access_path,
5ade1ed2
DG
2904 /*preserve_reference=*/false,
2905 complain);
d17811fd 2906 if (processing_template_decl && expr != error_mark_node)
61e71a9e
NS
2907 {
2908 if (BASELINK_P (member))
2909 {
2910 if (TREE_CODE (orig_name) == SCOPE_REF)
2911 BASELINK_QUALIFIED_P (member) = 1;
2912 orig_name = member;
2913 }
2914 return build_min_non_dep (COMPONENT_REF, expr,
2915 orig_object, orig_name,
2916 NULL_TREE);
2917 }
3db45ab5 2918
d17811fd 2919 return expr;
8d08fdba 2920}
5156628f 2921
0138d6b2
JM
2922/* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2923 type. */
2924
2925tree
2926build_simple_component_ref (tree object, tree member)
2927{
2928 tree type = cp_build_qualified_type (TREE_TYPE (member),
2929 cp_type_quals (TREE_TYPE (object)));
cda0a029
JM
2930 return build3_loc (input_location,
2931 COMPONENT_REF, type,
2932 object, member, NULL_TREE);
0138d6b2
JM
2933}
2934
50ad9642
MM
2935/* Return an expression for the MEMBER_NAME field in the internal
2936 representation of PTRMEM, a pointer-to-member function. (Each
2937 pointer-to-member function type gets its own RECORD_TYPE so it is
2938 more convenient to access the fields by name than by FIELD_DECL.)
2939 This routine converts the NAME to a FIELD_DECL and then creates the
2940 node for the complete expression. */
5156628f
MS
2941
2942tree
50ad9642 2943build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
5156628f 2944{
50ad9642
MM
2945 tree ptrmem_type;
2946 tree member;
50ad9642
MM
2947
2948 /* This code is a stripped down version of
2949 build_class_member_access_expr. It does not work to use that
2950 routine directly because it expects the object to be of class
2951 type. */
2952 ptrmem_type = TREE_TYPE (ptrmem);
50bc768d 2953 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
1d7e2ec7
JM
2954 for (member = TYPE_FIELDS (ptrmem_type); member;
2955 member = DECL_CHAIN (member))
2956 if (DECL_NAME (member) == member_name)
2957 break;
5ae37bdf
NS
2958 tree res = build_simple_component_ref (ptrmem, member);
2959
2960 TREE_NO_WARNING (res) = 1;
2961 return res;
5156628f 2962}
50ad9642 2963
8d08fdba
MS
2964/* Given an expression PTR for a pointer, return an expression
2965 for the value pointed to.
2966 ERRORSTRING is the name of the operator to appear in error messages.
2967
2968 This function may need to overload OPERATOR_FNNAME.
2969 Must also handle REFERENCE_TYPEs for C++. */
2970
2971tree
4fe977f2 2972build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
5ade1ed2 2973 tsubst_flags_t complain)
8d08fdba 2974{
d17811fd 2975 tree orig_expr = expr;
5566b478 2976 tree rval;
fcb9363e 2977 tree overload = NULL_TREE;
5566b478 2978
5156628f 2979 if (processing_template_decl)
d17811fd 2980 {
d84572a4 2981 /* Retain the type if we know the operand is a pointer. */
ccafc19b
JM
2982 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2983 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
d17811fd 2984 if (type_dependent_expression_p (expr))
f330f599 2985 return build_min_nt_loc (loc, INDIRECT_REF, expr);
d17811fd
MM
2986 expr = build_non_dependent_expr (expr);
2987 }
5566b478 2988
4fe977f2 2989 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
fcb9363e 2990 NULL_TREE, NULL_TREE, &overload, complain);
d17811fd 2991 if (!rval)
5ade1ed2 2992 rval = cp_build_indirect_ref (expr, errorstring, complain);
d17811fd
MM
2993
2994 if (processing_template_decl && rval != error_mark_node)
fcb9363e
PP
2995 {
2996 if (overload != NULL_TREE)
2997 return (build_min_non_dep_op_overload
2998 (INDIRECT_REF, rval, overload, orig_expr));
2999
3000 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
3001 }
d17811fd 3002 else
8d08fdba 3003 return rval;
8d08fdba
MS
3004}
3005
5ade1ed2 3006/* Helper function called from c-common. */
8d08fdba 3007tree
12308bc6 3008build_indirect_ref (location_t /*loc*/,
dd865ef6 3009 tree ptr, ref_operator errorstring)
5ade1ed2
DG
3010{
3011 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
3012}
3013
3014tree
dd865ef6 3015cp_build_indirect_ref (tree ptr, ref_operator errorstring,
5ade1ed2 3016 tsubst_flags_t complain)
8d08fdba 3017{
926ce8bd 3018 tree pointer, type;
c11b6f21 3019
374b2837
JM
3020 if (ptr == current_class_ptr
3021 || (TREE_CODE (ptr) == NOP_EXPR
3022 && TREE_OPERAND (ptr, 0) == current_class_ptr
3023 && (same_type_ignoring_top_level_qualifiers_p
3024 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
90e734a8
JM
3025 return current_class_ref;
3026
c11b6f21 3027 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
89fcabaf
PC
3028 ? ptr : decay_conversion (ptr, complain));
3029 if (pointer == error_mark_node)
3030 return error_mark_node;
3031
c11b6f21 3032 type = TREE_TYPE (pointer);
8d08fdba 3033
db24eb1f 3034 if (POINTER_TYPE_P (type))
8d08fdba 3035 {
01240200 3036 /* [expr.unary.op]
c8094d83 3037
01240200 3038 If the type of the expression is "pointer to T," the type
cd41d410
DS
3039 of the result is "T." */
3040 tree t = TREE_TYPE (type);
01240200 3041
5ae98e82
RB
3042 if ((CONVERT_EXPR_P (ptr)
3043 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
3044 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
79bedddc
SR
3045 {
3046 /* If a warning is issued, mark it to avoid duplicates from
3047 the backend. This only needs to be done at
3048 warn_strict_aliasing > 2. */
3049 if (warn_strict_aliasing > 2)
3050 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3051 type, TREE_OPERAND (ptr, 0)))
3052 TREE_NO_WARNING (ptr) = 1;
3053 }
3054
b72801e2 3055 if (VOID_TYPE_P (t))
0cbd7506
MS
3056 {
3057 /* A pointer to incomplete type (other than cv void) can be
3058 dereferenced [expr.unary.op]/1 */
5ade1ed2
DG
3059 if (complain & tf_error)
3060 error ("%qT is not a pointer-to-object type", type);
0cbd7506
MS
3061 return error_mark_node;
3062 }
a9183fef 3063 else if (TREE_CODE (pointer) == ADDR_EXPR
688f6688 3064 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
01240200
MM
3065 /* The POINTER was something like `&x'. We simplify `*&x' to
3066 `x'. */
8d08fdba
MS
3067 return TREE_OPERAND (pointer, 0);
3068 else
3069 {
01240200 3070 tree ref = build1 (INDIRECT_REF, t, pointer);
8d08fdba 3071
b0d75c1e
BK
3072 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3073 so that we get the proper error message if the result is used
3074 to assign to. Also, &* is supposed to be a no-op. */
91063b51
MM
3075 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3076 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
8d08fdba 3077 TREE_SIDE_EFFECTS (ref)
271bd540 3078 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
8d08fdba
MS
3079 return ref;
3080 }
3081 }
5ade1ed2
DG
3082 else if (!(complain & tf_error))
3083 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3084 ;
8d08fdba
MS
3085 /* `pointer' won't be an error_mark_node if we were given a
3086 pointer to member, so it's cool to check for this here. */
66b1156a 3087 else if (TYPE_PTRMEM_P (type))
dd865ef6
SZ
3088 switch (errorstring)
3089 {
3090 case RO_ARRAY_INDEXING:
3091 error ("invalid use of array indexing on pointer to member");
3092 break;
3093 case RO_UNARY_STAR:
3094 error ("invalid use of unary %<*%> on pointer to member");
3095 break;
3096 case RO_IMPLICIT_CONVERSION:
3097 error ("invalid use of implicit conversion on pointer to member");
3098 break;
5e54f81d
MG
3099 case RO_ARROW_STAR:
3100 error ("left hand operand of %<->*%> must be a pointer to class, "
3101 "but is a pointer to member of type %qT", type);
3102 break;
dd865ef6
SZ
3103 default:
3104 gcc_unreachable ();
3105 }
8d08fdba 3106 else if (pointer != error_mark_node)
7a6daeb0
NF
3107 invalid_indirection_error (input_location, type, errorstring);
3108
8d08fdba
MS
3109 return error_mark_node;
3110}
3111
3112/* This handles expressions of the form "a[i]", which denotes
3113 an array reference.
3114
3115 This is logically equivalent in C to *(a+i), but we may do it differently.
3116 If A is a variable or a member, we generate a primitive ARRAY_REF.
3117 This avoids forcing the array out of registers, and can work on
3118 arrays that are not lvalues (for example, members of structures returned
3119 by functions).
3120
3121 If INDEX is of some user-defined type, it must be converted to
3122 integer type. Otherwise, to make a compatible PLUS_EXPR, it
a63068b6
AH
3123 will inherit the type of the array, which will be some pointer type.
3124
3125 LOC is the location to use in building the array reference. */
8d08fdba 3126
8d08fdba 3127tree
3a11c665
JM
3128cp_build_array_ref (location_t loc, tree array, tree idx,
3129 tsubst_flags_t complain)
8d08fdba 3130{
a63068b6
AH
3131 tree ret;
3132
8d08fdba
MS
3133 if (idx == 0)
3134 {
73647d75
JM
3135 if (complain & tf_error)
3136 error_at (loc, "subscript missing in array reference");
8d08fdba
MS
3137 return error_mark_node;
3138 }
3139
2ce86d2e
BI
3140 /* If an array's index is an array notation, then its rank cannot be
3141 greater than one. */
b72271b9 3142 if (flag_cilkplus && contains_array_notation_expr (idx))
2ce86d2e
BI
3143 {
3144 size_t rank = 0;
3145
3146 /* If find_rank returns false, then it should have reported an error,
3147 thus it is unnecessary for repetition. */
3148 if (!find_rank (loc, idx, idx, true, &rank))
3149 return error_mark_node;
3150 if (rank > 1)
3151 {
3152 error_at (loc, "rank of the array%'s index is greater than 1");
3153 return error_mark_node;
3154 }
3155 }
8d08fdba
MS
3156 if (TREE_TYPE (array) == error_mark_node
3157 || TREE_TYPE (idx) == error_mark_node)
3158 return error_mark_node;
3159
0450d74d
RH
3160 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3161 inside it. */
3162 switch (TREE_CODE (array))
3163 {
3164 case COMPOUND_EXPR:
3165 {
3a11c665
JM
3166 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3167 complain);
a63068b6
AH
3168 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3169 TREE_OPERAND (array, 0), value);
3170 SET_EXPR_LOCATION (ret, loc);
3171 return ret;
0450d74d
RH
3172 }
3173
3174 case COND_EXPR:
a63068b6 3175 ret = build_conditional_expr
4cbc4bd7 3176 (loc, TREE_OPERAND (array, 0),
3a11c665
JM
3177 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3178 complain),
3179 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3180 complain),
a3299120 3181 complain);
8aff539b 3182 protected_set_expr_location (ret, loc);
a63068b6 3183 return ret;
0450d74d
RH
3184
3185 default:
3186 break;
3187 }
3188
f17a223d 3189 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
7edaa4d2 3190
6de9cd9a 3191 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
8d08fdba 3192 {
e4d35515 3193 tree rval, type;
8d08fdba 3194
5bd012f8 3195 warn_array_subscript_with_type_char (loc, idx);
8d08fdba 3196
adf2edec 3197 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
8d08fdba 3198 {
73647d75
JM
3199 if (complain & tf_error)
3200 error_at (loc, "array subscript is not an integer");
8d08fdba
MS
3201 return error_mark_node;
3202 }
3203
0a72704b
MM
3204 /* Apply integral promotions *after* noticing character types.
3205 (It is unclear why we do these promotions -- the standard
6ccf2f7d 3206 does not say that we should. In fact, the natural thing would
0a72704b
MM
3207 seem to be to convert IDX to ptrdiff_t; we're performing
3208 pointer arithmetic.) */
4b978f96 3209 idx = cp_perform_integral_promotions (idx, complain);
0a72704b 3210
8d08fdba
MS
3211 /* An array that is indexed by a non-constant
3212 cannot be stored in a register; we must be able to do
3213 address arithmetic on its address.
3214 Likewise an array of elements of variable size. */
3215 if (TREE_CODE (idx) != INTEGER_CST
d0f062fb 3216 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3c215895
JM
3217 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3218 != INTEGER_CST)))
8d08fdba 3219 {
a9e4a1a5 3220 if (!cxx_mark_addressable (array, true))
8d08fdba
MS
3221 return error_mark_node;
3222 }
0450d74d 3223
8d08fdba
MS
3224 /* An array that is indexed by a constant value which is not within
3225 the array bounds cannot be stored in a register either; because we
3226 would get a crash in store_bit_field/extract_bit_field when trying
3227 to access a non-existent part of the register. */
3228 if (TREE_CODE (idx) == INTEGER_CST
eb34af89
RK
3229 && TYPE_DOMAIN (TREE_TYPE (array))
3230 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
8d08fdba 3231 {
dffd7eb6 3232 if (!cxx_mark_addressable (array))
8d08fdba
MS
3233 return error_mark_node;
3234 }
3235
b7484fbe
MS
3236 /* Note in C++ it is valid to subscript a `register' array, since
3237 it is valid to take the address of something with that
3238 storage specification. */
3239 if (extra_warnings)
8d08fdba
MS
3240 {
3241 tree foo = array;
3242 while (TREE_CODE (foo) == COMPONENT_REF)
3243 foo = TREE_OPERAND (foo, 0);
5a6ccc94 3244 if (VAR_P (foo) && DECL_REGISTER (foo)
73647d75 3245 && (complain & tf_warning))
a63068b6
AH
3246 warning_at (loc, OPT_Wextra,
3247 "subscripting array declared %<register%>");
8d08fdba
MS
3248 }
3249
01240200 3250 type = TREE_TYPE (TREE_TYPE (array));
f293ce4b 3251 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
8d08fdba
MS
3252 /* Array ref is const/volatile if the array elements are
3253 or if the array is.. */
3254 TREE_READONLY (rval)
91063b51 3255 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
8d08fdba 3256 TREE_SIDE_EFFECTS (rval)
91063b51 3257 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
8d08fdba 3258 TREE_THIS_VOLATILE (rval)
91063b51 3259 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
cda0a029 3260 ret = require_complete_type_sfinae (rval, complain);
8aff539b 3261 protected_set_expr_location (ret, loc);
aa7da51a
JJ
3262 if (non_lvalue)
3263 ret = non_lvalue_loc (loc, ret);
a63068b6 3264 return ret;
8d08fdba
MS
3265 }
3266
3267 {
89fcabaf
PC
3268 tree ar = cp_default_conversion (array, complain);
3269 tree ind = cp_default_conversion (idx, complain);
8d08fdba
MS
3270
3271 /* Put the integer in IND to simplify error checking. */
3272 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
6b4db501 3273 std::swap (ar, ind);
8d08fdba 3274
89fcabaf
PC
3275 if (ar == error_mark_node || ind == error_mark_node)
3276 return error_mark_node;
8d08fdba 3277
50e10fa8 3278 if (!TYPE_PTR_P (TREE_TYPE (ar)))
8d08fdba 3279 {
73647d75
JM
3280 if (complain & tf_error)
3281 error_at (loc, "subscripted value is neither array nor pointer");
8d08fdba
MS
3282 return error_mark_node;
3283 }
3284 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3285 {
73647d75
JM
3286 if (complain & tf_error)
3287 error_at (loc, "array subscript is not an integer");
8d08fdba
MS
3288 return error_mark_node;
3289 }
3290
5bd012f8 3291 warn_array_subscript_with_type_char (loc, idx);
0958b0d3 3292
ba47d38d
AH
3293 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3294 PLUS_EXPR, ar, ind,
73647d75 3295 complain),
dd865ef6 3296 RO_ARRAY_INDEXING,
73647d75 3297 complain);
a63068b6 3298 protected_set_expr_location (ret, loc);
aa7da51a
JJ
3299 if (non_lvalue)
3300 ret = non_lvalue_loc (loc, ret);
a63068b6 3301 return ret;
8d08fdba
MS
3302 }
3303}
3a11c665
JM
3304
3305/* Entry point for Obj-C++. */
3306
3307tree
3308build_array_ref (location_t loc, tree array, tree idx)
3309{
3310 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3311}
8d08fdba 3312\f
8d08fdba 3313/* Resolve a pointer to member function. INSTANCE is the object
c87978aa
JM
3314 instance to use, if the member points to a virtual member.
3315
3316 This used to avoid checking for virtual functions if basetype
3317 has no virtual functions, according to an earlier ANSI draft.
3318 With the final ISO C++ rules, such an optimization is
3319 incorrect: A pointer to a derived member can be static_cast
3320 to pointer-to-base-member, as long as the dynamic object
e18724aa
JM
3321 later has the right member. So now we only do this optimization
3322 when we know the dynamic type of the object. */
8d08fdba
MS
3323
3324tree
89fcabaf
PC
3325get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3326 tsubst_flags_t complain)
8d08fdba
MS
3327{
3328 if (TREE_CODE (function) == OFFSET_REF)
338d90b8 3329 function = TREE_OPERAND (function, 1);
8d08fdba
MS
3330
3331 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3332 {
e18724aa
JM
3333 tree idx, delta, e1, e2, e3, vtbl;
3334 bool nonvirtual;
c87978aa 3335 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
e18724aa 3336 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
f30432d7 3337
b7484fbe 3338 tree instance_ptr = *instance_ptrptr;
1c83ea9f 3339 tree instance_save_expr = 0;
c87978aa 3340 if (instance_ptr == error_mark_node)
32facac8 3341 {
c87978aa
JM
3342 if (TREE_CODE (function) == PTRMEM_CST)
3343 {
3344 /* Extracting the function address from a pmf is only
3345 allowed with -Wno-pmf-conversions. It only works for
c6002625 3346 pmf constants. */
89fcabaf 3347 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
c87978aa
JM
3348 e1 = convert (fntype, e1);
3349 return e1;
3350 }
3351 else
3352 {
89fcabaf
PC
3353 if (complain & tf_error)
3354 error ("object missing in use of %qE", function);
c87978aa
JM
3355 return error_mark_node;
3356 }
32facac8
ML
3357 }
3358
e18724aa
JM
3359 /* True if we know that the dynamic type of the object doesn't have
3360 virtual functions, so we can assume the PFN field is a pointer. */
3361 nonvirtual = (COMPLETE_TYPE_P (basetype)
3362 && !TYPE_POLYMORPHIC_P (basetype)
3363 && resolves_to_fixed_type_p (instance_ptr, 0));
3364
f9873232
JM
3365 /* If we don't really have an object (i.e. in an ill-formed
3366 conversion from PMF to pointer), we can't resolve virtual
3367 functions anyway. */
3368 if (!nonvirtual && is_dummy_object (instance_ptr))
3369 nonvirtual = true;
3370
b7484fbe 3371 if (TREE_SIDE_EFFECTS (instance_ptr))
1c83ea9f 3372 instance_ptr = instance_save_expr = save_expr (instance_ptr);
b7484fbe 3373
f30432d7
MS
3374 if (TREE_SIDE_EFFECTS (function))
3375 function = save_expr (function);
3376
c87978aa 3377 /* Start by extracting all the information from the PMF itself. */
10b2bcdd 3378 e3 = pfn_from_ptrmemfunc (function);
a298680c 3379 delta = delta_from_ptrmemfunc (function);
85b7def6 3380 idx = build1 (NOP_EXPR, vtable_index_type, e3);
b5fbde92 3381 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
cb7fdde2 3382 {
ec043522 3383 int flag_sanitize_save;
cb7fdde2 3384 case ptrmemfunc_vbit_in_pfn:
ba47d38d
AH
3385 e1 = cp_build_binary_op (input_location,
3386 BIT_AND_EXPR, idx, integer_one_node,
89fcabaf 3387 complain);
ba47d38d
AH
3388 idx = cp_build_binary_op (input_location,
3389 MINUS_EXPR, idx, integer_one_node,
89fcabaf
PC
3390 complain);
3391 if (idx == error_mark_node)
3392 return error_mark_node;
cb7fdde2
AO
3393 break;
3394
3395 case ptrmemfunc_vbit_in_delta:
ba47d38d
AH
3396 e1 = cp_build_binary_op (input_location,
3397 BIT_AND_EXPR, delta, integer_one_node,
89fcabaf 3398 complain);
ec043522
MP
3399 /* Don't instrument the RSHIFT_EXPR we're about to create because
3400 we're going to use DELTA number of times, and that wouldn't play
3401 well with SAVE_EXPRs therein. */
3402 flag_sanitize_save = flag_sanitize;
3403 flag_sanitize = 0;
ba47d38d
AH
3404 delta = cp_build_binary_op (input_location,
3405 RSHIFT_EXPR, delta, integer_one_node,
89fcabaf 3406 complain);
ec043522 3407 flag_sanitize = flag_sanitize_save;
89fcabaf
PC
3408 if (delta == error_mark_node)
3409 return error_mark_node;
cb7fdde2
AO
3410 break;
3411
3412 default:
315fb5db 3413 gcc_unreachable ();
cb7fdde2 3414 }
03b256e4 3415
89fcabaf
PC
3416 if (e1 == error_mark_node)
3417 return error_mark_node;
3418
81ae598b
MM
3419 /* Convert down to the right base before using the instance. A
3420 special case is that in a pointer to member of class C, C may
3421 be incomplete. In that case, the function will of course be
3422 a member of C, and no conversion is required. In fact,
3423 lookup_base will fail in that case, because incomplete
c8094d83 3424 classes do not have BINFOs. */
c8094d83 3425 if (!same_type_ignoring_top_level_qualifiers_p
81ae598b
MM
3426 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3427 {
3428 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
22854930 3429 basetype, ba_check, NULL, complain);
c8094d83 3430 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
89fcabaf 3431 1, complain);
81ae598b
MM
3432 if (instance_ptr == error_mark_node)
3433 return error_mark_node;
3434 }
c87978aa 3435 /* ...and then the delta in the PMF. */
5d49b6a7 3436 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
c87978aa
JM
3437
3438 /* Hand back the adjusted 'this' argument to our caller. */
3439 *instance_ptrptr = instance_ptr;
3440
e18724aa
JM
3441 if (nonvirtual)
3442 /* Now just return the pointer. */
3443 return e3;
3444
c87978aa
JM
3445 /* Next extract the vtable pointer from the object. */
3446 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3447 instance_ptr);
89fcabaf
PC
3448 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3449 if (vtbl == error_mark_node)
3450 return error_mark_node;
3451
c87978aa 3452 /* Finally, extract the function pointer from the vtable. */
5d49b6a7 3453 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
89fcabaf
PC
3454 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3455 if (e2 == error_mark_node)
3456 return error_mark_node;
f63ab951 3457 TREE_CONSTANT (e2) = 1;
c7e266a6 3458
67231816
RH
3459 /* When using function descriptors, the address of the
3460 vtable entry is treated as a function pointer. */
3461 if (TARGET_VTABLE_USES_DESCRIPTORS)
3462 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
89fcabaf 3463 cp_build_addr_expr (e2, complain));
67231816 3464
a489b1f0 3465 e2 = fold_convert (TREE_TYPE (e3), e2);
4cbc4bd7 3466 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
89fcabaf
PC
3467 if (e1 == error_mark_node)
3468 return error_mark_node;
c8094d83 3469
03b256e4
ML
3470 /* Make sure this doesn't get evaluated first inside one of the
3471 branches of the COND_EXPR. */
1c83ea9f 3472 if (instance_save_expr)
f293ce4b
RS
3473 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3474 instance_save_expr, e1);
03b256e4 3475
f49422da 3476 function = e1;
8d08fdba
MS
3477 }
3478 return function;
3479}
3480
5ade1ed2 3481/* Used by the C-common bits. */
8d08fdba 3482tree
12308bc6 3483build_function_call (location_t /*loc*/,
c2255bc4 3484 tree function, tree params)
5ade1ed2
DG
3485{
3486 return cp_build_function_call (function, params, tf_warning_or_error);
3487}
3488
bbbbb16a
ILT
3489/* Used by the C-common bits. */
3490tree
81e5eca8 3491build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
9771b263
DN
3492 tree function, vec<tree, va_gc> *params,
3493 vec<tree, va_gc> * /*origtypes*/)
bbbbb16a 3494{
9771b263 3495 vec<tree, va_gc> *orig_params = params;
c166b898
ILT
3496 tree ret = cp_build_function_call_vec (function, &params,
3497 tf_warning_or_error);
bbbbb16a 3498
c166b898
ILT
3499 /* cp_build_function_call_vec can reallocate PARAMS by adding
3500 default arguments. That should never happen here. Verify
3501 that. */
3502 gcc_assert (params == orig_params);
3503
3504 return ret;
bbbbb16a
ILT
3505}
3506
c166b898
ILT
3507/* Build a function call using a tree list of arguments. */
3508
882de214 3509static tree
5ade1ed2 3510cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
c166b898 3511{
9771b263 3512 vec<tree, va_gc> *vec;
c166b898
ILT
3513 tree ret;
3514
3515 vec = make_tree_vector ();
3516 for (; params != NULL_TREE; params = TREE_CHAIN (params))
9771b263 3517 vec_safe_push (vec, TREE_VALUE (params));
c166b898
ILT
3518 ret = cp_build_function_call_vec (function, &vec, complain);
3519 release_tree_vector (vec);
3520 return ret;
3521}
3522
450f4293
NF
3523/* Build a function call using varargs. */
3524
3525tree
3526cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3527{
9771b263 3528 vec<tree, va_gc> *vec;
450f4293
NF
3529 va_list args;
3530 tree ret, t;
3531
3532 vec = make_tree_vector ();
3533 va_start (args, complain);
3534 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
9771b263 3535 vec_safe_push (vec, t);
450f4293
NF
3536 va_end (args);
3537 ret = cp_build_function_call_vec (function, &vec, complain);
3538 release_tree_vector (vec);
3539 return ret;
3540}
3541
c166b898
ILT
3542/* Build a function call using a vector of arguments. PARAMS may be
3543 NULL if there are no parameters. This changes the contents of
3544 PARAMS. */
3545
3546tree
9771b263 3547cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
c166b898 3548 tsubst_flags_t complain)
8d08fdba 3549{
926ce8bd 3550 tree fntype, fndecl;
8d08fdba 3551 int is_method;
2e4cf9dc 3552 tree original = function;
c166b898 3553 int nargs;
94a0dd7b
SL
3554 tree *argarray;
3555 tree parm_types;
9771b263 3556 vec<tree, va_gc> *allocated = NULL;
c166b898 3557 tree ret;
8d08fdba 3558
e58a9aa1
ZL
3559 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3560 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
3561 if (params != NULL && !vec_safe_is_empty (*params))
3562 function = objc_rewrite_function_call (function, (**params)[0]);
e58a9aa1 3563
8d08fdba
MS
3564 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3565 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3566 if (TREE_CODE (function) == NOP_EXPR
3567 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3568 function = TREE_OPERAND (function, 0);
3569
3570 if (TREE_CODE (function) == FUNCTION_DECL)
3571 {
971e17ff
AS
3572 /* If the function is a non-template member function
3573 or a non-template friend, then we need to check the
3574 constraints.
3575
3576 Note that if overload resolution failed with a single
3577 candidate this function will be used to explicitly diagnose
3578 the failure for the single call expression. The check is
3579 technically redundant since we also would have failed in
3580 add_function_candidate. */
3581 if (flag_concepts
3582 && (complain & tf_error)
3583 && !constraints_satisfied_p (function))
3584 {
3585 error ("cannot call function %qD", function);
3586 location_t loc = DECL_SOURCE_LOCATION (function);
3587 diagnose_constraints (loc, function, NULL_TREE);
3588 return error_mark_node;
3589 }
3590
9f635aba
PC
3591 if (!mark_used (function, complain) && !(complain & tf_error))
3592 return error_mark_node;
8d08fdba
MS
3593 fndecl = function;
3594
3595 /* Convert anything with function type to a pointer-to-function. */
aa0a659c
PC
3596 if (DECL_MAIN_P (function))
3597 {
3598 if (complain & tf_error)
3599 pedwarn (input_location, OPT_Wpedantic,
3600 "ISO C++ forbids calling %<::main%> from within program");
3601 else
3602 return error_mark_node;
3603 }
89fcabaf 3604 function = build_addr_func (function, complain);
8d08fdba
MS
3605 }
3606 else
3607 {
3608 fndecl = NULL_TREE;
3609
89fcabaf 3610 function = build_addr_func (function, complain);
8d08fdba
MS
3611 }
3612
4ac14744
MS
3613 if (function == error_mark_node)
3614 return error_mark_node;
3615
8d08fdba
MS
3616 fntype = TREE_TYPE (function);
3617
3618 if (TYPE_PTRMEMFUNC_P (fntype))
3619 {
5ade1ed2
DG
3620 if (complain & tf_error)
3621 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
7b98a725
JW
3622 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3623 original, original);
4ac14744 3624 return error_mark_node;
8d08fdba
MS
3625 }
3626
50e10fa8 3627 is_method = (TYPE_PTR_P (fntype)
8d08fdba
MS
3628 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3629
50e10fa8 3630 if (!(TYPE_PTRFN_P (fntype)
386b8a85
JM
3631 || is_method
3632 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
8d08fdba 3633 {
5ade1ed2 3634 if (complain & tf_error)
ae52741c
MLI
3635 {
3636 if (!flag_diagnostics_show_caret)
3637 error_at (input_location,
3638 "%qE cannot be used as a function", original);
3639 else if (DECL_P (original))
3640 error_at (input_location,
3641 "%qD cannot be used as a function", original);
3642 else
3643 error_at (input_location,
3644 "expression cannot be used as a function");
3645 }
3646
8d08fdba
MS
3647 return error_mark_node;
3648 }
3649
3650 /* fntype now gets the type of function pointed to. */
3651 fntype = TREE_TYPE (fntype);
94a0dd7b
SL
3652 parm_types = TYPE_ARG_TYPES (fntype);
3653
c166b898
ILT
3654 if (params == NULL)
3655 {
3656 allocated = make_tree_vector ();
3657 params = &allocated;
3658 }
3659
2ce86d2e
BI
3660 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3661 complain);
94a0dd7b 3662 if (nargs < 0)
125e6594 3663 return error_mark_node;
863adfc0 3664
9771b263 3665 argarray = (*params)->address ();
c166b898 3666
dd66b8e8
JM
3667 /* Check for errors in format strings and inappropriately
3668 null parameters. */
4227c9ad 3669 bool warned_p = check_function_arguments (input_location, fndecl, fntype,
0dba7960 3670 nargs, argarray);
8d08fdba 3671
e2e03032 3672 ret = build_cxx_call (function, nargs, argarray, complain);
c166b898 3673
0dba7960
JJ
3674 if (warned_p)
3675 {
3676 tree c = extract_call_expr (ret);
3677 if (TREE_CODE (c) == CALL_EXPR)
3678 TREE_NO_WARNING (c) = 1;
3679 }
3680
c166b898
ILT
3681 if (allocated != NULL)
3682 release_tree_vector (allocated);
3683
3684 return ret;
8d08fdba 3685}
8d08fdba 3686\f
8cdea6ab 3687/* Subroutine of convert_arguments.
09bae928 3688 Print an error message about a wrong number of arguments. */
8cdea6ab
SZ
3689
3690static void
09bae928 3691error_args_num (location_t loc, tree fndecl, bool too_many_p)
8cdea6ab
SZ
3692{
3693 if (fndecl)
3694 {
3695 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3696 {
3697 if (DECL_NAME (fndecl) == NULL_TREE
3698 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3699 error_at (loc,
3700 too_many_p
3701 ? G_("too many arguments to constructor %q#D")
3702 : G_("too few arguments to constructor %q#D"),
3703 fndecl);
3704 else
3705 error_at (loc,
3706 too_many_p
3707 ? G_("too many arguments to member function %q#D")
3708 : G_("too few arguments to member function %q#D"),
3709 fndecl);
3710 }
3711 else
3712 error_at (loc,
3713 too_many_p
3714 ? G_("too many arguments to function %q#D")
3715 : G_("too few arguments to function %q#D"),
3716 fndecl);
6b6dd1aa
MP
3717 if (!DECL_IS_BUILTIN (fndecl))
3718 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
8cdea6ab
SZ
3719 }
3720 else
19dc6d01
NP
3721 {
3722 if (c_dialect_objc () && objc_message_selector ())
3723 error_at (loc,
3724 too_many_p
3725 ? G_("too many arguments to method %q#D")
3726 : G_("too few arguments to method %q#D"),
3727 objc_message_selector ());
3728 else
3729 error_at (loc, too_many_p ? G_("too many arguments to function")
3730 : G_("too few arguments to function"));
3731 }
8cdea6ab
SZ
3732}
3733
c166b898
ILT
3734/* Convert the actual parameter expressions in the list VALUES to the
3735 types in the list TYPELIST. The converted expressions are stored
3736 back in the VALUES vector.
8d08fdba
MS
3737 If parmdecls is exhausted, or when an element has NULL as its type,
3738 perform the default conversions.
3739
8d08fdba
MS
3740 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3741
3742 This is also where warnings about wrong number of args are generated.
c8094d83 3743
94a0dd7b 3744 Returns the actual number of arguments processed (which might be less
c166b898 3745 than the length of the vector), or -1 on error.
8d08fdba
MS
3746
3747 In C++, unspecified trailing parameters can be filled in with their
3748 default arguments, if such were specified. Do so here. */
3749
94a0dd7b 3750static int
9771b263 3751convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
c166b898 3752 int flags, tsubst_flags_t complain)
8d08fdba 3753{
c166b898 3754 tree typetail;
c166b898 3755 unsigned int i;
8d08fdba 3756
faf5394a
MS
3757 /* Argument passing is always copy-initialization. */
3758 flags |= LOOKUP_ONLYCONVERTING;
3759
c166b898 3760 for (i = 0, typetail = typelist;
9771b263 3761 i < vec_safe_length (*values);
c166b898 3762 i++)
8d08fdba 3763 {
926ce8bd 3764 tree type = typetail ? TREE_VALUE (typetail) : 0;
9771b263 3765 tree val = (**values)[i];
8d08fdba 3766
6af455d7 3767 if (val == error_mark_node || type == error_mark_node)
94a0dd7b 3768 return -1;
8ccc31eb 3769
8d08fdba
MS
3770 if (type == void_type_node)
3771 {
5ade1ed2
DG
3772 if (complain & tf_error)
3773 {
09bae928 3774 error_args_num (input_location, fndecl, /*too_many_p=*/true);
5ade1ed2
DG
3775 return i;
3776 }
3777 else
3778 return -1;
8d08fdba
MS
3779 }
3780
8d08fdba
MS
3781 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3782 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3783 if (TREE_CODE (val) == NOP_EXPR
a0a33927
MS
3784 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3785 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
8d08fdba
MS
3786 val = TREE_OPERAND (val, 0);
3787
00595019
MS
3788 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3789 {
3790 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
8d08fdba 3791 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
00595019 3792 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
89fcabaf 3793 val = decay_conversion (val, complain);
00595019 3794 }
8d08fdba
MS
3795
3796 if (val == error_mark_node)
94a0dd7b 3797 return -1;
8d08fdba 3798
8d08fdba
MS
3799 if (type != 0)
3800 {
3801 /* Formal parm type is specified by a function prototype. */
3802 tree parmval;
3803
d0f062fb 3804 if (!COMPLETE_TYPE_P (complete_type (type)))
8d08fdba 3805 {
5ade1ed2
DG
3806 if (complain & tf_error)
3807 {
3808 if (fndecl)
3809 error ("parameter %P of %qD has incomplete type %qT",
3810 i, fndecl, type);
3811 else
3812 error ("parameter %P has incomplete type %qT", i, type);
3813 }
3afd2e20 3814 parmval = error_mark_node;
8d08fdba
MS
3815 }
3816 else
3817 {
9a3b49ac 3818 parmval = convert_for_initialization
56ae6d77 3819 (NULL_TREE, type, val, flags,
2f5b91f5 3820 ICR_ARGPASS, fndecl, i, complain);
b40e334f 3821 parmval = convert_for_arg_passing (type, parmval, complain);
8d08fdba 3822 }
878cd289
MS
3823
3824 if (parmval == error_mark_node)
94a0dd7b 3825 return -1;
878cd289 3826
9771b263 3827 (**values)[i] = parmval;
8d08fdba
MS
3828 }
3829 else
3830 {
1141ed3f 3831 if (fndecl && magic_varargs_p (fndecl))
4eaf1d5b 3832 /* Don't do ellipsis conversion for __built_in_constant_p
c32097d8 3833 as this will result in spurious errors for non-trivial
4eaf1d5b 3834 types. */
79fe346e 3835 val = require_complete_type_sfinae (val, complain);
4eaf1d5b 3836 else
b40e334f 3837 val = convert_arg_to_ellipsis (val, complain);
4eaf1d5b 3838
9771b263 3839 (**values)[i] = val;
8d08fdba
MS
3840 }
3841
8d08fdba
MS
3842 if (typetail)
3843 typetail = TREE_CHAIN (typetail);
3844 }
3845
3846 if (typetail != 0 && typetail != void_list_node)
3847 {
823c22f9
NS
3848 /* See if there are default arguments that can be used. Because
3849 we hold default arguments in the FUNCTION_TYPE (which is so
3850 wrong), we can see default parameters here from deduced
3851 contexts (and via typeof) for indirect function calls.
3852 Fortunately we know whether we have a function decl to
3853 provide default arguments in a language conformant
3854 manner. */
3855 if (fndecl && TREE_PURPOSE (typetail)
a723baf1 3856 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
8d08fdba 3857 {
f376e137 3858 for (; typetail != void_list_node; ++i)
8d08fdba 3859 {
559f2bbc
JM
3860 /* After DR777, with explicit template args we can end up with a
3861 default argument followed by no default argument. */
3862 if (!TREE_PURPOSE (typetail))
3863 break;
c8094d83
MS
3864 tree parmval
3865 = convert_default_arg (TREE_VALUE (typetail),
3866 TREE_PURPOSE (typetail),
b40e334f 3867 fndecl, i, complain);
8d08fdba 3868
878cd289 3869 if (parmval == error_mark_node)
94a0dd7b 3870 return -1;
878cd289 3871
9771b263 3872 vec_safe_push (*values, parmval);
8d08fdba
MS
3873 typetail = TREE_CHAIN (typetail);
3874 /* ends with `...'. */
3875 if (typetail == NULL_TREE)
3876 break;
3877 }
3878 }
559f2bbc
JM
3879
3880 if (typetail && typetail != void_list_node)
8d08fdba 3881 {
559f2bbc 3882 if (complain & tf_error)
09bae928 3883 error_args_num (input_location, fndecl, /*too_many_p=*/false);
94a0dd7b 3884 return -1;
8d08fdba
MS
3885 }
3886 }
8d08fdba 3887
c166b898 3888 return (int) i;
8d08fdba
MS
3889}
3890\f
3891/* Build a binary-operation expression, after performing default
2a67bec2
ILT
3892 conversions on the operands. CODE is the kind of expression to
3893 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3894 are the tree codes which correspond to ARG1 and ARG2 when issuing
3895 warnings about possibly misplaced parentheses. They may differ
3896 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3897 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3898 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3899 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3900 ARG2_CODE as ERROR_MARK. */
8d08fdba
MS
3901
3902tree
4fe977f2
PC
3903build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3904 enum tree_code arg1_code, tree arg2,
fcb9363e 3905 enum tree_code arg2_code, tree *overload_p,
5ade1ed2 3906 tsubst_flags_t complain)
8d08fdba 3907{
d17811fd
MM
3908 tree orig_arg1;
3909 tree orig_arg2;
3910 tree expr;
fcb9363e 3911 tree overload = NULL_TREE;
5566b478 3912
d17811fd
MM
3913 orig_arg1 = arg1;
3914 orig_arg2 = arg2;
d6b4ea85 3915
d17811fd 3916 if (processing_template_decl)
14d22dd6 3917 {
d17811fd
MM
3918 if (type_dependent_expression_p (arg1)
3919 || type_dependent_expression_p (arg2))
f330f599 3920 return build_min_nt_loc (loc, code, arg1, arg2);
d17811fd
MM
3921 arg1 = build_non_dependent_expr (arg1);
3922 arg2 = build_non_dependent_expr (arg2);
3923 }
14d22dd6 3924
d17811fd 3925 if (code == DOTSTAR_EXPR)
89fcabaf 3926 expr = build_m_component_ref (arg1, arg2, complain);
d17811fd 3927 else
4fe977f2 3928 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
fcb9363e
PP
3929 &overload, complain);
3930
3931 if (overload_p != NULL)
3932 *overload_p = overload;
14d22dd6 3933
2a67bec2
ILT
3934 /* Check for cases such as x+y<<z which users are likely to
3935 misinterpret. But don't warn about obj << x + y, since that is a
3936 common idiom for I/O. */
3937 if (warn_parentheses
aa9b7569 3938 && (complain & tf_warning)
2a67bec2
ILT
3939 && !processing_template_decl
3940 && !error_operand_p (arg1)
3941 && !error_operand_p (arg2)
3942 && (code != LSHIFT_EXPR
3943 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
5d9de0d0
PC
3944 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3945 arg2_code, orig_arg2);
2a67bec2 3946
d17811fd 3947 if (processing_template_decl && expr != error_mark_node)
fcb9363e
PP
3948 {
3949 if (overload != NULL_TREE)
3950 return (build_min_non_dep_op_overload
3951 (code, expr, overload, orig_arg1, orig_arg2));
3952
3953 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3954 }
c8094d83 3955
d17811fd 3956 return expr;
14d22dd6
MM
3957}
3958
493e377c
PC
3959/* Build and return an ARRAY_REF expression. */
3960
3961tree
f330f599
PC
3962build_x_array_ref (location_t loc, tree arg1, tree arg2,
3963 tsubst_flags_t complain)
493e377c
PC
3964{
3965 tree orig_arg1 = arg1;
3966 tree orig_arg2 = arg2;
3967 tree expr;
fcb9363e 3968 tree overload = NULL_TREE;
493e377c
PC
3969
3970 if (processing_template_decl)
3971 {
3972 if (type_dependent_expression_p (arg1)
3973 || type_dependent_expression_p (arg2))
f330f599
PC
3974 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3975 NULL_TREE, NULL_TREE);
493e377c
PC
3976 arg1 = build_non_dependent_expr (arg1);
3977 arg2 = build_non_dependent_expr (arg2);
3978 }
3979
f330f599 3980 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
fcb9363e 3981 NULL_TREE, &overload, complain);
493e377c
PC
3982
3983 if (processing_template_decl && expr != error_mark_node)
fcb9363e
PP
3984 {
3985 if (overload != NULL_TREE)
3986 return (build_min_non_dep_op_overload
3987 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
3988
3989 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3990 NULL_TREE, NULL_TREE);
3991 }
493e377c
PC
3992 return expr;
3993}
3994
12448f77
ILT
3995/* Return whether OP is an expression of enum type cast to integer
3996 type. In C++ even unsigned enum types are cast to signed integer
3997 types. We do not want to issue warnings about comparisons between
3998 signed and unsigned types when one of the types is an enum type.
3999 Those warnings are always false positives in practice. */
4000
4001static bool
4002enum_cast_to_int (tree op)
4003{
f764304c 4004 if (CONVERT_EXPR_P (op)
12448f77
ILT
4005 && TREE_TYPE (op) == integer_type_node
4006 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
4007 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
4008 return true;
4009
4010 /* The cast may have been pushed into a COND_EXPR. */
4011 if (TREE_CODE (op) == COND_EXPR)
4012 return (enum_cast_to_int (TREE_OPERAND (op, 1))
4013 || enum_cast_to_int (TREE_OPERAND (op, 2)));
4014
4015 return false;
4016}
4017
5ade1ed2
DG
4018/* For the c-common bits. */
4019tree
ba47d38d 4020build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
12308bc6 4021 int /*convert_p*/)
5ade1ed2 4022{
ba47d38d 4023 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
5ade1ed2
DG
4024}
4025
9f47c7e5
IE
4026/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4027 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4028
4029static tree
4030build_vec_cmp (tree_code code, tree type,
4031 tree arg0, tree arg1)
4032{
4033 tree zero_vec = build_zero_cst (type);
4034 tree minus_one_vec = build_minus_one_cst (type);
4035 tree cmp_type = build_same_sized_truth_vector_type(type);
4036 tree cmp = build2 (code, cmp_type, arg0, arg1);
9f47c7e5
IE
4037 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
4038}
5ade1ed2 4039
b1970801
MP
4040/* Possibly warn about an address never being NULL. */
4041
4042static void
4043warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
4044{
4045 if (!warn_address
4046 || (complain & tf_warning) == 0
4047 || c_inhibit_evaluation_warnings != 0
4048 || TREE_NO_WARNING (op))
4049 return;
4050
4051 tree cop = fold_non_dependent_expr (op);
4052
4053 if (TREE_CODE (cop) == ADDR_EXPR
4054 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
4055 && !TREE_NO_WARNING (cop))
4056 warning_at (location, OPT_Waddress, "the address of %qD will never "
4057 "be NULL", TREE_OPERAND (cop, 0));
4058
4059 if (CONVERT_EXPR_P (op)
4060 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4061 {
4062 tree inner_op = op;
4063 STRIP_NOPS (inner_op);
4064
4065 if (DECL_P (inner_op))
4066 warning_at (location, OPT_Waddress,
4067 "the compiler can assume that the address of "
4068 "%qD will never be NULL", inner_op);
4069 }
4070}
4071
8d08fdba
MS
4072/* Build a binary-operation expression without default conversions.
4073 CODE is the kind of expression to build.
ba47d38d 4074 LOCATION is the location_t of the operator in the source code.
8d08fdba
MS
4075 This function differs from `build' in several ways:
4076 the data type of the result is computed and recorded in it,
4077 warnings are generated if arg data types are invalid,
4078 special handling for addition and subtraction of pointers is known,
4079 and some optimization is done (operations on narrow ints
4080 are done in the narrower type when that gives the same result).
4081 Constant folding is also done before the result is returned.
4082
8d08fdba
MS
4083 Note that the operands will never have enumeral types
4084 because either they have just had the default conversions performed
4085 or they have both just been converted to some other type in which
4086 the arithmetic is to be done.
4087
4088 C++: must do special pointer arithmetic when implementing
4089 multiple inheritance, and deal with pointer to member functions. */
4090
4091tree
ba47d38d
AH
4092cp_build_binary_op (location_t location,
4093 enum tree_code code, tree orig_op0, tree orig_op1,
5ade1ed2 4094 tsubst_flags_t complain)
8d08fdba 4095{
39211cd5 4096 tree op0, op1;
926ce8bd 4097 enum tree_code code0, code1;
39211cd5 4098 tree type0, type1;
4de67c26 4099 const char *invalid_op_diag;
8d08fdba
MS
4100
4101 /* Expression code to give to the expression when it is built.
4102 Normally this is CODE, which is what the caller asked for,
4103 but in some special cases we change it. */
926ce8bd 4104 enum tree_code resultcode = code;
8d08fdba
MS
4105
4106 /* Data type in which the computation is to be performed.
4107 In the simplest cases this is the common type of the arguments. */
98a7a34e 4108 tree result_type = NULL_TREE;
8d08fdba
MS
4109
4110 /* Nonzero means operands have already been type-converted
4111 in whatever way is necessary.
4112 Zero means they need to be converted to RESULT_TYPE. */
4113 int converted = 0;
4114
28cbf42c
MS
4115 /* Nonzero means create the expression with this type, rather than
4116 RESULT_TYPE. */
4117 tree build_type = 0;
4118
8d08fdba 4119 /* Nonzero means after finally constructing the expression
faae18ab 4120 convert it to this type. */
8d08fdba
MS
4121 tree final_type = 0;
4122
cda0a029 4123 tree result, result_ovl;
455f19cb 4124
8d08fdba
MS
4125 /* Nonzero if this is an operation like MIN or MAX which can
4126 safely be computed in short if both args are promoted shorts.
4127 Also implies COMMON.
4128 -1 indicates a bitwise operation; this makes a difference
4129 in the exact conditions for when it is safe to do the operation
4130 in a narrower mode. */
4131 int shorten = 0;
4132
4133 /* Nonzero if this is a comparison operation;
4134 if both args are promoted shorts, compare the original shorts.
4135 Also implies COMMON. */
4136 int short_compare = 0;
4137
8d08fdba
MS
4138 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4139 int common = 0;
4140
455f19cb
MM
4141 /* True if both operands have arithmetic type. */
4142 bool arithmetic_types_p;
4143
39211cd5 4144 /* Apply default conversions. */
40260429
NS
4145 op0 = orig_op0;
4146 op1 = orig_op1;
c8094d83 4147
de5a5fa1
MP
4148 /* Remember whether we're doing / or %. */
4149 bool doing_div_or_mod = false;
4150
4151 /* Remember whether we're doing << or >>. */
4152 bool doing_shift = false;
4153
4154 /* Tree holding instrumentation expression. */
98a7a34e 4155 tree instrument_expr = NULL_TREE;
de5a5fa1 4156
a9aedbc2
MS
4157 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4158 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4159 || code == TRUTH_XOR_EXPR)
4160 {
a66e8081 4161 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
89fcabaf 4162 op0 = decay_conversion (op0, complain);
a66e8081 4163 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
89fcabaf 4164 op1 = decay_conversion (op1, complain);
a9aedbc2
MS
4165 }
4166 else
4167 {
a66e8081 4168 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
89fcabaf 4169 op0 = cp_default_conversion (op0, complain);
a66e8081 4170 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
89fcabaf 4171 op1 = cp_default_conversion (op1, complain);
a9aedbc2 4172 }
39211cd5 4173
a359be75
JM
4174 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4175 STRIP_TYPE_NOPS (op0);
4176 STRIP_TYPE_NOPS (op1);
4177
4178 /* DTRT if one side is an overloaded function, but complain about it. */
4179 if (type_unknown_p (op0))
4180 {
c2ea3a40 4181 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
a359be75
JM
4182 if (t != error_mark_node)
4183 {
5ade1ed2 4184 if (complain & tf_error)
cbe5f3b3 4185 permerror (input_location, "assuming cast to type %qT from overloaded function",
37ec60ed 4186 TREE_TYPE (t));
a359be75
JM
4187 op0 = t;
4188 }
4189 }
4190 if (type_unknown_p (op1))
4191 {
c2ea3a40 4192 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
a359be75
JM
4193 if (t != error_mark_node)
4194 {
5ade1ed2 4195 if (complain & tf_error)
cbe5f3b3 4196 permerror (input_location, "assuming cast to type %qT from overloaded function",
37ec60ed 4197 TREE_TYPE (t));
a359be75
JM
4198 op1 = t;
4199 }
4200 }
4201
1141ed3f
BI
4202 type0 = TREE_TYPE (op0);
4203 type1 = TREE_TYPE (op1);
39211cd5
MS
4204
4205 /* The expression codes of the data types of the arguments tell us
4206 whether the arguments are integers, floating, pointers, etc. */
4207 code0 = TREE_CODE (type0);
4208 code1 = TREE_CODE (type1);
4209
8d08fdba
MS
4210 /* If an error was already reported for one of the arguments,
4211 avoid reporting another error. */
8d08fdba
MS
4212 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4213 return error_mark_node;
4214
4de67c26
JM
4215 if ((invalid_op_diag
4216 = targetm.invalid_binary_op (code, type0, type1)))
4217 {
76545796
PC
4218 if (complain & tf_error)
4219 error (invalid_op_diag);
4de67c26
JM
4220 return error_mark_node;
4221 }
4222
ef3ddd4f
MM
4223 /* Issue warnings about peculiar, but valid, uses of NULL. */
4224 if ((orig_op0 == null_node || orig_op1 == null_node)
4225 /* It's reasonable to use pointer values as operands of &&
4226 and ||, so NULL is no exception. */
4227 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4228 && ( /* Both are NULL (or 0) and the operation was not a
4229 comparison or a pointer subtraction. */
4230 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4231 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4232 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4233 || (!null_ptr_cst_p (orig_op0)
66b1156a 4234 && !TYPE_PTR_OR_PTRMEM_P (type0))
ef3ddd4f 4235 || (!null_ptr_cst_p (orig_op1)
66b1156a 4236 && !TYPE_PTR_OR_PTRMEM_P (type1)))
ef3ddd4f 4237 && (complain & tf_warning))
70dc395a
DS
4238 {
4239 source_location loc =
4240 expansion_point_location_if_in_system_header (input_location);
4241
4242 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4243 }
ef3ddd4f 4244
a212e43f
MG
4245 /* In case when one of the operands of the binary operation is
4246 a vector and another is a scalar -- convert scalar to vector. */
4247 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4248 {
4249 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4250 complain & tf_error);
4251
4252 switch (convert_flag)
4253 {
4254 case stv_error:
4255 return error_mark_node;
4256 case stv_firstarg:
4257 {
4258 op0 = convert (TREE_TYPE (type1), op0);
3c9aabbd 4259 op0 = save_expr (op0);
a212e43f
MG
4260 op0 = build_vector_from_val (type1, op0);
4261 type0 = TREE_TYPE (op0);
4262 code0 = TREE_CODE (type0);
4263 converted = 1;
4264 break;
4265 }
4266 case stv_secondarg:
4267 {
4268 op1 = convert (TREE_TYPE (type0), op1);
3c9aabbd 4269 op1 = save_expr (op1);
a212e43f
MG
4270 op1 = build_vector_from_val (type0, op1);
4271 type1 = TREE_TYPE (op1);
4272 code1 = TREE_CODE (type1);
4273 converted = 1;
4274 break;
4275 }
4276 default:
4277 break;
4278 }
4279 }
4280
8d08fdba
MS
4281 switch (code)
4282 {
8d08fdba
MS
4283 case MINUS_EXPR:
4284 /* Subtraction of two similar pointers.
4285 We must subtract them as integers, then divide by object size. */
4286 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
a5ac359a
MM
4287 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4288 TREE_TYPE (type1)))
4f2e1536
MP
4289 return pointer_diff (location, op0, op1,
4290 common_pointer_type (type0, type1), complain);
0225a19d 4291 /* In all other cases except pointer - int, the usual arithmetic
39a13be5 4292 rules apply. */
0225a19d
MM
4293 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4294 {
4295 common = 1;
4296 break;
4297 }
4298 /* The pointer - int case is just like pointer + int; fall
4299 through. */
81fea426 4300 gcc_fallthrough ();
0225a19d
MM
4301 case PLUS_EXPR:
4302 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4303 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4304 {
4305 tree ptr_operand;
4306 tree int_operand;
4307 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4308 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4309 if (processing_template_decl)
4310 {
4311 result_type = TREE_TYPE (ptr_operand);
4312 break;
4313 }
4f2e1536
MP
4314 return cp_pointer_int_sum (location, code,
4315 ptr_operand,
fd9b0f32
PC
4316 int_operand,
4317 complain);
0225a19d
MM
4318 }
4319 common = 1;
8d08fdba
MS
4320 break;
4321
4322 case MULT_EXPR:
4323 common = 1;
4324 break;
4325
4326 case TRUNC_DIV_EXPR:
4327 case CEIL_DIV_EXPR:
4328 case FLOOR_DIV_EXPR:
4329 case ROUND_DIV_EXPR:
4330 case EXACT_DIV_EXPR:
37c46b43 4331 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3a021db2 4332 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
37c46b43 4333 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3a021db2 4334 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
8d08fdba 4335 {
01c15146 4336 enum tree_code tcode0 = code0, tcode1 = code1;
234bef96 4337 tree cop1 = fold_non_dependent_expr (op1);
f8ed5150 4338 doing_div_or_mod = true;
de5a5fa1 4339 warn_for_div_by_zero (location, cop1);
c8094d83 4340
01c15146
VR
4341 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4342 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4343 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4344 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 4345
01c15146 4346 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
8d08fdba
MS
4347 resultcode = RDIV_EXPR;
4348 else
4349 /* When dividing two signed integers, we have to promote to int.
ddd5a7c1 4350 unless we divide by a constant != -1. Note that default
8d08fdba
MS
4351 conversion will have been performed on the operands at this
4352 point, so we have to dig out the original type to find out if
4353 it was unsigned. */
4354 shorten = ((TREE_CODE (op0) == NOP_EXPR
8df83eae 4355 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
8d08fdba 4356 || (TREE_CODE (op1) == INTEGER_CST
05bccae2
RK
4357 && ! integer_all_onesp (op1)));
4358
8d08fdba
MS
4359 common = 1;
4360 }
4361 break;
4362
4363 case BIT_AND_EXPR:
8d08fdba
MS
4364 case BIT_IOR_EXPR:
4365 case BIT_XOR_EXPR:
73042643 4366 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9ef0c8d9
AP
4367 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4368 && !VECTOR_FLOAT_TYPE_P (type0)
4369 && !VECTOR_FLOAT_TYPE_P (type1)))
8d08fdba 4370 shorten = -1;
8d08fdba
MS
4371 break;
4372
4373 case TRUNC_MOD_EXPR:
4374 case FLOOR_MOD_EXPR:
f7e8cd25 4375 {
234bef96 4376 tree cop1 = fold_non_dependent_expr (op1);
f8ed5150 4377 doing_div_or_mod = true;
de5a5fa1 4378 warn_for_div_by_zero (location, cop1);
f7e8cd25 4379 }
c8094d83 4380
5cfd5d9b
AP
4381 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4382 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4383 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4384 common = 1;
4385 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8d08fdba
MS
4386 {
4387 /* Although it would be tempting to shorten always here, that loses
4388 on some targets, since the modulo instruction is undefined if the
4389 quotient can't be represented in the computation mode. We shorten
4390 only if unsigned or if dividing by something we know != -1. */
4391 shorten = ((TREE_CODE (op0) == NOP_EXPR
8df83eae 4392 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
8d08fdba 4393 || (TREE_CODE (op1) == INTEGER_CST
05bccae2 4394 && ! integer_all_onesp (op1)));
8d08fdba
MS
4395 common = 1;
4396 }
4397 break;
4398
4399 case TRUTH_ANDIF_EXPR:
4400 case TRUTH_ORIF_EXPR:
4401 case TRUTH_AND_EXPR:
4402 case TRUTH_OR_EXPR:
3c9aabbd 4403 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
abb839ec 4404 {
3c9aabbd
MG
4405 if (!COMPARISON_CLASS_P (op1))
4406 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4407 build_zero_cst (type1), complain);
4408 if (code == TRUTH_ANDIF_EXPR)
4409 {
4410 tree z = build_zero_cst (TREE_TYPE (op1));
4411 return build_conditional_expr (location, op0, op1, z, complain);
4412 }
4413 else if (code == TRUTH_ORIF_EXPR)
4414 {
4415 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4416 return build_conditional_expr (location, op0, m1, op1, complain);
4417 }
4418 else
4419 gcc_unreachable ();
4420 }
4421 if (VECTOR_TYPE_P (type0))
4422 {
4423 if (!COMPARISON_CLASS_P (op0))
4424 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4425 build_zero_cst (type0), complain);
4426 if (!VECTOR_TYPE_P (type1))
4427 {
4428 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4429 tree z = build_zero_cst (TREE_TYPE (op0));
efc3536f 4430 op1 = build_conditional_expr (location, op1, m1, z, complain);
3c9aabbd
MG
4431 }
4432 else if (!COMPARISON_CLASS_P (op1))
4433 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4434 build_zero_cst (type1), complain);
4435
4436 if (code == TRUTH_ANDIF_EXPR)
4437 code = BIT_AND_EXPR;
4438 else if (code == TRUTH_ORIF_EXPR)
4439 code = BIT_IOR_EXPR;
4440 else
4441 gcc_unreachable ();
4442
4443 return cp_build_binary_op (location, code, op0, op1, complain);
abb839ec 4444 }
3c9aabbd 4445
255512c1 4446 result_type = boolean_type_node;
8d08fdba
MS
4447 break;
4448
4449 /* Shift operations: result has same type as first operand;
4450 always convert second operand to int.
4451 Also set SHORT_SHIFT if shifting rightward. */
4452
4453 case RSHIFT_EXPR:
a212e43f
MG
4454 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4455 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4456 {
4457 result_type = type0;
4458 converted = 1;
4459 }
4460 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
31ed6226
MG
4461 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4462 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4463 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4464 {
4465 result_type = type0;
4466 converted = 1;
4467 }
4468 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8d08fdba 4469 {
234bef96 4470 tree const_op1 = fold_non_dependent_expr (op1);
cc83c823
JJ
4471 if (TREE_CODE (const_op1) != INTEGER_CST)
4472 const_op1 = op1;
8d08fdba 4473 result_type = type0;
de5a5fa1 4474 doing_shift = true;
cc83c823 4475 if (TREE_CODE (const_op1) == INTEGER_CST)
8d08fdba 4476 {
cc83c823 4477 if (tree_int_cst_lt (const_op1, integer_zero_node))
5ade1ed2 4478 {
7d882b83
ILT
4479 if ((complain & tf_warning)
4480 && c_inhibit_evaluation_warnings == 0)
13c21655
PC
4481 warning (OPT_Wshift_count_negative,
4482 "right shift count is negative");
5ade1ed2 4483 }
8d08fdba
MS
4484 else
4485 {
cc83c823 4486 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
7d882b83
ILT
4487 && (complain & tf_warning)
4488 && c_inhibit_evaluation_warnings == 0)
13c21655
PC
4489 warning (OPT_Wshift_count_overflow,
4490 "right shift count >= width of type");
8d08fdba
MS
4491 }
4492 }
8d08fdba
MS
4493 /* Avoid converting op1 to result_type later. */
4494 converted = 1;
4495 }
4496 break;
4497
4498 case LSHIFT_EXPR:
a212e43f
MG
4499 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4500 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4501 {
4502 result_type = type0;
4503 converted = 1;
4504 }
4505 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
31ed6226
MG
4506 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4507 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4508 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4509 {
4510 result_type = type0;
4511 converted = 1;
4512 }
4513 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
8d08fdba 4514 {
0173bd2a
MP
4515 tree const_op0 = fold_non_dependent_expr (op0);
4516 if (TREE_CODE (const_op0) != INTEGER_CST)
4517 const_op0 = op0;
234bef96 4518 tree const_op1 = fold_non_dependent_expr (op1);
cc83c823
JJ
4519 if (TREE_CODE (const_op1) != INTEGER_CST)
4520 const_op1 = op1;
8d08fdba 4521 result_type = type0;
de5a5fa1 4522 doing_shift = true;
0173bd2a
MP
4523 if (TREE_CODE (const_op0) == INTEGER_CST
4524 && tree_int_cst_sgn (const_op0) < 0
4525 && (complain & tf_warning)
4526 && c_inhibit_evaluation_warnings == 0)
4527 warning (OPT_Wshift_negative_value,
4528 "left shift of negative value");
cc83c823 4529 if (TREE_CODE (const_op1) == INTEGER_CST)
8d08fdba 4530 {
cc83c823 4531 if (tree_int_cst_lt (const_op1, integer_zero_node))
5ade1ed2 4532 {
7d882b83
ILT
4533 if ((complain & tf_warning)
4534 && c_inhibit_evaluation_warnings == 0)
13c21655
PC
4535 warning (OPT_Wshift_count_negative,
4536 "left shift count is negative");
5ade1ed2 4537 }
cc83c823
JJ
4538 else if (compare_tree_int (const_op1,
4539 TYPE_PRECISION (type0)) >= 0)
5ade1ed2 4540 {
7d882b83
ILT
4541 if ((complain & tf_warning)
4542 && c_inhibit_evaluation_warnings == 0)
13c21655
PC
4543 warning (OPT_Wshift_count_overflow,
4544 "left shift count >= width of type");
5ade1ed2 4545 }
451b5e48
MP
4546 else if (TREE_CODE (const_op0) == INTEGER_CST
4547 && (complain & tf_warning))
4548 maybe_warn_shift_overflow (location, const_op0, const_op1);
8d08fdba 4549 }
8d08fdba
MS
4550 /* Avoid converting op1 to result_type later. */
4551 converted = 1;
4552 }
4553 break;
4554
4555 case RROTATE_EXPR:
4556 case LROTATE_EXPR:
4557 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4558 {
4559 result_type = type0;
4560 if (TREE_CODE (op1) == INTEGER_CST)
4561 {
4562 if (tree_int_cst_lt (op1, integer_zero_node))
5ade1ed2
DG
4563 {
4564 if (complain & tf_warning)
4565 warning (0, (code == LROTATE_EXPR)
4566 ? G_("left rotate count is negative")
4567 : G_("right rotate count is negative"));
4568 }
665f2503 4569 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
5ade1ed2
DG
4570 {
4571 if (complain & tf_warning)
4572 warning (0, (code == LROTATE_EXPR)
4573 ? G_("left rotate count >= width of type")
4574 : G_("right rotate count >= width of type"));
4575 }
8d08fdba
MS
4576 }
4577 /* Convert the shift-count to an integer, regardless of
4578 size of value being shifted. */
4579 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4b978f96 4580 op1 = cp_convert (integer_type_node, op1, complain);
8d08fdba
MS
4581 }
4582 break;
4583
4584 case EQ_EXPR:
4585 case NE_EXPR:
31ed6226
MG
4586 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4587 goto vector_compare;
5ade1ed2
DG
4588 if ((complain & tf_warning)
4589 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
3db45ab5
MS
4590 warning (OPT_Wfloat_equal,
4591 "comparing floating point with == or != is unsafe");
5ade1ed2 4592 if ((complain & tf_warning)
66756373
JJ
4593 && ((TREE_CODE (orig_op0) == STRING_CST
4594 && !integer_zerop (cp_fully_fold (op1)))
4595 || (TREE_CODE (orig_op1) == STRING_CST
4596 && !integer_zerop (cp_fully_fold (op0)))))
4597 warning (OPT_Waddress, "comparison with string literal results "
9c582551 4598 "in unspecified behavior");
1bdba2c0 4599
c8094d83 4600 build_type = boolean_type_node;
37c46b43 4601 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
550a799d 4602 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
37c46b43 4603 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
550a799d 4604 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
8d08fdba 4605 short_compare = 1;
81caef48 4606 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
f41f1ceb 4607 && null_ptr_cst_p (orig_op1))
81caef48
PC
4608 /* Handle, eg, (void*)0 (c++/43906), and more. */
4609 || (code0 == POINTER_TYPE
4610 && TYPE_PTR_P (type1) && integer_zerop (op1)))
b3c6d2ea 4611 {
81caef48
PC
4612 if (TYPE_PTR_P (type1))
4613 result_type = composite_pointer_type (type0, type1, op0, op1,
4614 CPO_COMPARISON, complain);
4615 else
4616 result_type = type0;
4617
a9342885
MP
4618 if (char_type_p (TREE_TYPE (orig_op1))
4619 && warning (OPT_Wpointer_compare,
4620 "comparison between pointer and zero character "
4621 "constant"))
4622 inform (input_location,
4623 "did you mean to dereference the pointer?");
b1970801 4624 warn_for_null_address (location, op0, complain);
b3c6d2ea 4625 }
81caef48 4626 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
f41f1ceb 4627 && null_ptr_cst_p (orig_op0))
81caef48
PC
4628 /* Handle, eg, (void*)0 (c++/43906), and more. */
4629 || (code1 == POINTER_TYPE
4630 && TYPE_PTR_P (type0) && integer_zerop (op0)))
b3c6d2ea 4631 {
81caef48
PC
4632 if (TYPE_PTR_P (type0))
4633 result_type = composite_pointer_type (type0, type1, op0, op1,
4634 CPO_COMPARISON, complain);
4635 else
4636 result_type = type1;
4637
a9342885
MP
4638 if (char_type_p (TREE_TYPE (orig_op0))
4639 && warning (OPT_Wpointer_compare,
4640 "comparison between pointer and zero character "
4641 "constant"))
4642 inform (input_location,
4643 "did you mean to dereference the pointer?");
b1970801 4644 warn_for_null_address (location, op1, complain);
b3c6d2ea 4645 }
81caef48
PC
4646 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4647 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4648 result_type = composite_pointer_type (type0, type1, op0, op1,
4649 CPO_COMPARISON, complain);
f41f1ceb 4650 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
14c2101d
JM
4651 /* One of the operands must be of nullptr_t type. */
4652 result_type = TREE_TYPE (nullptr_node);
8d08fdba
MS
4653 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4654 {
faae18ab 4655 result_type = type0;
5ade1ed2 4656 if (complain & tf_error)
cbe5f3b3 4657 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5ade1ed2
DG
4658 else
4659 return error_mark_node;
8d08fdba
MS
4660 }
4661 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4662 {
faae18ab 4663 result_type = type1;
5ade1ed2 4664 if (complain & tf_error)
cbe5f3b3 4665 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5ade1ed2
DG
4666 else
4667 return error_mark_node;
8d08fdba 4668 }
f41f1ceb 4669 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
8d08fdba 4670 {
c3a88be8
RM
4671 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4672 == ptrmemfunc_vbit_in_delta)
4673 {
20265464
ZC
4674 tree pfn0, delta0, e1, e2;
4675
4676 if (TREE_SIDE_EFFECTS (op0))
4677 op0 = save_expr (op0);
4678
4679 pfn0 = pfn_from_ptrmemfunc (op0);
4680 delta0 = delta_from_ptrmemfunc (op0);
4681 e1 = cp_build_binary_op (location,
4682 EQ_EXPR,
4683 pfn0,
4684 build_zero_cst (TREE_TYPE (pfn0)),
4685 complain);
4686 e2 = cp_build_binary_op (location,
4687 BIT_AND_EXPR,
4688 delta0,
4689 integer_one_node,
4690 complain);
30b07d03
PC
4691
4692 if (complain & tf_warning)
4693 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
51e355aa 4694
ba47d38d
AH
4695 e2 = cp_build_binary_op (location,
4696 EQ_EXPR, e2, integer_zero_node,
5ade1ed2 4697 complain);
ba47d38d
AH
4698 op0 = cp_build_binary_op (location,
4699 TRUTH_ANDIF_EXPR, e1, e2,
5ade1ed2 4700 complain);
cda0a029 4701 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
c3a88be8
RM
4702 }
4703 else
4704 {
4705 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4b978f96 4706 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
c3a88be8 4707 }
faae18ab 4708 result_type = TREE_TYPE (op0);
8d08fdba 4709 }
f41f1ceb 4710 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
ba47d38d 4711 return cp_build_binary_op (location, code, op1, op0, complain);
867f133e 4712 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
8d08fdba 4713 {
867f133e 4714 tree type;
c7e266a6
MM
4715 /* E will be the final comparison. */
4716 tree e;
4717 /* E1 and E2 are for scratch. */
4718 tree e1;
4719 tree e2;
1f84ec23
MM
4720 tree pfn0;
4721 tree pfn1;
4722 tree delta0;
4723 tree delta1;
c7e266a6 4724
c86818cf
SZ
4725 type = composite_pointer_type (type0, type1, op0, op1,
4726 CPO_COMPARISON, complain);
867f133e
OW
4727
4728 if (!same_type_p (TREE_TYPE (op0), type))
4b978f96 4729 op0 = cp_convert_and_check (type, op0, complain);
867f133e 4730 if (!same_type_p (TREE_TYPE (op1), type))
4b978f96 4731 op1 = cp_convert_and_check (type, op1, complain);
867f133e
OW
4732
4733 if (op0 == error_mark_node || op1 == error_mark_node)
4734 return error_mark_node;
4735
ebb1abc3
JM
4736 if (TREE_SIDE_EFFECTS (op0))
4737 op0 = save_expr (op0);
4738 if (TREE_SIDE_EFFECTS (op1))
4739 op1 = save_expr (op1);
4740
1f84ec23 4741 pfn0 = pfn_from_ptrmemfunc (op0);
cda0a029 4742 pfn0 = cp_fully_fold (pfn0);
457d0ace
PC
4743 /* Avoid -Waddress warnings (c++/64877). */
4744 if (TREE_CODE (pfn0) == ADDR_EXPR)
4745 TREE_NO_WARNING (pfn0) = 1;
1f84ec23 4746 pfn1 = pfn_from_ptrmemfunc (op1);
cda0a029 4747 pfn1 = cp_fully_fold (pfn1);
a298680c
OW
4748 delta0 = delta_from_ptrmemfunc (op0);
4749 delta1 = delta_from_ptrmemfunc (op1);
c3a88be8
RM
4750 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4751 == ptrmemfunc_vbit_in_delta)
4752 {
4753 /* We generate:
4754
4755 (op0.pfn == op1.pfn
4756 && ((op0.delta == op1.delta)
4757 || (!op0.pfn && op0.delta & 1 == 0
4758 && op1.delta & 1 == 0))
4759
4760 The reason for the `!op0.pfn' bit is that a NULL
4761 pointer-to-member is any member with a zero PFN and
4762 LSB of the DELTA field is 0. */
4763
ba47d38d 4764 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
c3a88be8 4765 delta0,
5ade1ed2
DG
4766 integer_one_node,
4767 complain);
ba47d38d
AH
4768 e1 = cp_build_binary_op (location,
4769 EQ_EXPR, e1, integer_zero_node,
5ade1ed2 4770 complain);
ba47d38d 4771 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
c3a88be8 4772 delta1,
5ade1ed2
DG
4773 integer_one_node,
4774 complain);
ba47d38d
AH
4775 e2 = cp_build_binary_op (location,
4776 EQ_EXPR, e2, integer_zero_node,
5ade1ed2 4777 complain);
ba47d38d
AH
4778 e1 = cp_build_binary_op (location,
4779 TRUTH_ANDIF_EXPR, e2, e1,
5ade1ed2 4780 complain);
ba47d38d 4781 e2 = cp_build_binary_op (location, EQ_EXPR,
c3a88be8 4782 pfn0,
e8160c9a 4783 build_zero_cst (TREE_TYPE (pfn0)),
5ade1ed2 4784 complain);
ba47d38d
AH
4785 e2 = cp_build_binary_op (location,
4786 TRUTH_ANDIF_EXPR, e2, e1, complain);
4787 e1 = cp_build_binary_op (location,
4788 EQ_EXPR, delta0, delta1, complain);
4789 e1 = cp_build_binary_op (location,
4790 TRUTH_ORIF_EXPR, e1, e2, complain);
c3a88be8
RM
4791 }
4792 else
4793 {
4794 /* We generate:
4795
4796 (op0.pfn == op1.pfn
4797 && (!op0.pfn || op0.delta == op1.delta))
4798
4799 The reason for the `!op0.pfn' bit is that a NULL
4800 pointer-to-member is any member with a zero PFN; the
4801 DELTA field is unspecified. */
4802
ba47d38d
AH
4803 e1 = cp_build_binary_op (location,
4804 EQ_EXPR, delta0, delta1, complain);
4805 e2 = cp_build_binary_op (location,
4806 EQ_EXPR,
c3a88be8 4807 pfn0,
e8160c9a 4808 build_zero_cst (TREE_TYPE (pfn0)),
5ade1ed2 4809 complain);
ba47d38d
AH
4810 e1 = cp_build_binary_op (location,
4811 TRUTH_ORIF_EXPR, e1, e2, complain);
c3a88be8 4812 }
f293ce4b 4813 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
ba47d38d
AH
4814 e = cp_build_binary_op (location,
4815 TRUTH_ANDIF_EXPR, e2, e1, complain);
8d08fdba 4816 if (code == EQ_EXPR)
c7e266a6 4817 return e;
ba47d38d
AH
4818 return cp_build_binary_op (location,
4819 EQ_EXPR, e, integer_zero_node, complain);
8d08fdba 4820 }
315fb5db
NS
4821 else
4822 {
4823 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4824 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4825 type1));
4826 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4827 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4828 type0));
4829 }
c8094d83 4830
8d08fdba
MS
4831 break;
4832
4833 case MAX_EXPR:
4834 case MIN_EXPR:
4835 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4836 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4837 shorten = 1;
4838 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
708cae97 4839 result_type = composite_pointer_type (type0, type1, op0, op1,
c86818cf 4840 CPO_COMPARISON, complain);
8d08fdba
MS
4841 break;
4842
4843 case LE_EXPR:
4844 case GE_EXPR:
4845 case LT_EXPR:
4846 case GT_EXPR:
eda0cd98
RS
4847 if (TREE_CODE (orig_op0) == STRING_CST
4848 || TREE_CODE (orig_op1) == STRING_CST)
5ade1ed2
DG
4849 {
4850 if (complain & tf_warning)
9c582551
JJ
4851 warning (OPT_Waddress, "comparison with string literal results "
4852 "in unspecified behavior");
5ade1ed2 4853 }
eda0cd98 4854
31ed6226
MG
4855 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4856 {
4857 vector_compare:
4858 tree intt;
4859 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
0af94e6f
JR
4860 TREE_TYPE (type1))
4861 && !vector_types_compatible_elements_p (type0, type1))
31ed6226 4862 {
07298ffd
MG
4863 if (complain & tf_error)
4864 {
4865 error_at (location, "comparing vectors with different "
4866 "element types");
4867 inform (location, "operand types are %qT and %qT",
4868 type0, type1);
4869 }
31ed6226
MG
4870 return error_mark_node;
4871 }
4872
4873 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4874 {
07298ffd
MG
4875 if (complain & tf_error)
4876 {
4877 error_at (location, "comparing vectors with different "
4878 "number of elements");
4879 inform (location, "operand types are %qT and %qT",
4880 type0, type1);
4881 }
31ed6226
MG
4882 return error_mark_node;
4883 }
4884
fa74a4bc
MP
4885 /* It's not precisely specified how the usual arithmetic
4886 conversions apply to the vector types. Here, we use
4887 the unsigned type if one of the operands is signed and
4888 the other one is unsigned. */
4889 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
4890 {
4891 if (!TYPE_UNSIGNED (type0))
4892 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
4893 else
4894 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
4895 warning_at (location, OPT_Wsign_compare, "comparison between "
4896 "types %qT and %qT", type0, type1);
4897 }
4898
31ed6226
MG
4899 /* Always construct signed integer vector type. */
4900 intt = c_common_type_for_size (GET_MODE_BITSIZE
4901 (TYPE_MODE (TREE_TYPE (type0))), 0);
07298ffd
MG
4902 if (!intt)
4903 {
4904 if (complain & tf_error)
4905 error_at (location, "could not find an integer type "
4906 "of the same size as %qT", TREE_TYPE (type0));
4907 return error_mark_node;
4908 }
31ed6226
MG
4909 result_type = build_opaque_vector_type (intt,
4910 TYPE_VECTOR_SUBPARTS (type0));
4911 converted = 1;
9f47c7e5 4912 return build_vec_cmp (resultcode, result_type, op0, op1);
31ed6226 4913 }
28cbf42c 4914 build_type = boolean_type_node;
b5c0a77e
JM
4915 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4916 || code0 == ENUMERAL_TYPE)
4917 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4918 || code1 == ENUMERAL_TYPE))
8d08fdba
MS
4919 short_compare = 1;
4920 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
708cae97 4921 result_type = composite_pointer_type (type0, type1, op0, op1,
c86818cf 4922 CPO_COMPARISON, complain);
f41f1ceb 4923 else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
6f94398e
PC
4924 {
4925 result_type = type0;
4926 if (extra_warnings && (complain & tf_warning))
4927 warning (OPT_Wextra,
4928 "ordered comparison of pointer with integer zero");
4929 }
f41f1ceb 4930 else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
6f94398e
PC
4931 {
4932 result_type = type1;
4933 if (extra_warnings && (complain & tf_warning))
4934 warning (OPT_Wextra,
4935 "ordered comparison of pointer with integer zero");
4936 }
f41f1ceb 4937 else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
14c2101d
JM
4938 /* One of the operands must be of nullptr_t type. */
4939 result_type = TREE_TYPE (nullptr_node);
8d08fdba
MS
4940 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4941 {
faae18ab 4942 result_type = type0;
5ade1ed2 4943 if (complain & tf_error)
cbe5f3b3 4944 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5ade1ed2
DG
4945 else
4946 return error_mark_node;
8d08fdba
MS
4947 }
4948 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4949 {
faae18ab 4950 result_type = type1;
5ade1ed2 4951 if (complain & tf_error)
cbe5f3b3 4952 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
5ade1ed2
DG
4953 else
4954 return error_mark_node;
8d08fdba 4955 }
8d08fdba 4956 break;
7f85441b 4957
1eb8759b
RH
4958 case UNORDERED_EXPR:
4959 case ORDERED_EXPR:
4960 case UNLT_EXPR:
4961 case UNLE_EXPR:
4962 case UNGT_EXPR:
4963 case UNGE_EXPR:
4964 case UNEQ_EXPR:
1eb8759b
RH
4965 build_type = integer_type_node;
4966 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4967 {
5ade1ed2
DG
4968 if (complain & tf_error)
4969 error ("unordered comparison on non-floating point argument");
1eb8759b
RH
4970 return error_mark_node;
4971 }
4972 common = 1;
4973 break;
4974
7f85441b
KG
4975 default:
4976 break;
8d08fdba
MS
4977 }
4978
550a799d
JM
4979 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4980 || code0 == ENUMERAL_TYPE)
c8094d83 4981 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
550a799d 4982 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
73042643
AH
4983 arithmetic_types_p = 1;
4984 else
4985 {
4986 arithmetic_types_p = 0;
4987 /* Vector arithmetic is only allowed when both sides are vectors. */
4988 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4989 {
f6d7e7d8 4990 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 4991 || !vector_types_compatible_elements_p (type0, type1))
f6d7e7d8 4992 {
76545796 4993 if (complain & tf_error)
745e411d
DM
4994 {
4995 /* "location" already embeds the locations of the
4996 operands, so we don't need to add them separately
4997 to richloc. */
4998 rich_location richloc (line_table, location);
4999 binary_op_error (&richloc, code, type0, type1);
5000 }
f6d7e7d8
AH
5001 return error_mark_node;
5002 }
73042643
AH
5003 arithmetic_types_p = 1;
5004 }
5005 }
455f19cb
MM
5006 /* Determine the RESULT_TYPE, if it is not already known. */
5007 if (!result_type
c8094d83 5008 && arithmetic_types_p
455f19cb 5009 && (shorten || common || short_compare))
c5ee1358
MM
5010 {
5011 result_type = cp_common_type (type0, type1);
76545796
PC
5012 if (complain & tf_warning)
5013 do_warn_double_promotion (result_type, type0, type1,
5014 "implicit conversion from %qT to %qT "
5015 "to match other operand of binary "
5016 "expression",
5017 location);
c5ee1358 5018 }
455f19cb
MM
5019
5020 if (!result_type)
8d08fdba 5021 {
5ade1ed2 5022 if (complain & tf_error)
745e411d
DM
5023 error_at (location,
5024 "invalid operands of types %qT and %qT to binary %qO",
5025 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
455f19cb
MM
5026 return error_mark_node;
5027 }
37c46b43 5028
455f19cb
MM
5029 /* If we're in a template, the only thing we need to know is the
5030 RESULT_TYPE. */
5031 if (processing_template_decl)
5be014d5
AP
5032 {
5033 /* Since the middle-end checks the type when doing a build2, we
5034 need to build the tree in pieces. This built tree will never
5035 get out of the front-end as we replace it when instantiating
5036 the template. */
5037 tree tmp = build2 (resultcode,
5038 build_type ? build_type : result_type,
5039 NULL_TREE, op1);
5040 TREE_OPERAND (tmp, 0) = op0;
5041 return tmp;
5042 }
455f19cb 5043
98a7a34e
MP
5044 /* Remember the original type; RESULT_TYPE might be changed later on
5045 by shorten_binary_op. */
5046 tree orig_type = result_type;
5047
455f19cb
MM
5048 if (arithmetic_types_p)
5049 {
1ff6b2c8
JM
5050 bool first_complex = (code0 == COMPLEX_TYPE);
5051 bool second_complex = (code1 == COMPLEX_TYPE);
5052 int none_complex = (!first_complex && !second_complex);
5053
5054 /* Adapted from patch for c/24581. */
5055 if (first_complex != second_complex
5056 && (code == PLUS_EXPR
5057 || code == MINUS_EXPR
5058 || code == MULT_EXPR
5059 || (code == TRUNC_DIV_EXPR && first_complex))
5060 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
5061 && flag_signed_zeros)
5062 {
5063 /* An operation on mixed real/complex operands must be
5064 handled specially, but the language-independent code can
5065 more easily optimize the plain complex arithmetic if
5066 -fno-signed-zeros. */
5067 tree real_type = TREE_TYPE (result_type);
5068 tree real, imag;
5069 if (first_complex)
5070 {
5071 if (TREE_TYPE (op0) != result_type)
4b978f96 5072 op0 = cp_convert_and_check (result_type, op0, complain);
1ff6b2c8 5073 if (TREE_TYPE (op1) != real_type)
4b978f96 5074 op1 = cp_convert_and_check (real_type, op1, complain);
1ff6b2c8
JM
5075 }
5076 else
5077 {
5078 if (TREE_TYPE (op0) != real_type)
4b978f96 5079 op0 = cp_convert_and_check (real_type, op0, complain);
1ff6b2c8 5080 if (TREE_TYPE (op1) != result_type)
4b978f96 5081 op1 = cp_convert_and_check (result_type, op1, complain);
1ff6b2c8
JM
5082 }
5083 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5084 return error_mark_node;
5085 if (first_complex)
5086 {
5087 op0 = save_expr (op0);
e51fbec3
MP
5088 real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
5089 imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
1ff6b2c8
JM
5090 switch (code)
5091 {
5092 case MULT_EXPR:
5093 case TRUNC_DIV_EXPR:
e834d848 5094 op1 = save_expr (op1);
1ff6b2c8
JM
5095 imag = build2 (resultcode, real_type, imag, op1);
5096 /* Fall through. */
5097 case PLUS_EXPR:
5098 case MINUS_EXPR:
5099 real = build2 (resultcode, real_type, real, op1);
5100 break;
5101 default:
5102 gcc_unreachable();
5103 }
5104 }
5105 else
5106 {
5107 op1 = save_expr (op1);
e51fbec3
MP
5108 real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
5109 imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
1ff6b2c8
JM
5110 switch (code)
5111 {
5112 case MULT_EXPR:
e834d848 5113 op0 = save_expr (op0);
1ff6b2c8
JM
5114 imag = build2 (resultcode, real_type, op0, imag);
5115 /* Fall through. */
5116 case PLUS_EXPR:
5117 real = build2 (resultcode, real_type, op0, real);
5118 break;
5119 case MINUS_EXPR:
5120 real = build2 (resultcode, real_type, op0, real);
5121 imag = build1 (NEGATE_EXPR, real_type, imag);
5122 break;
5123 default:
5124 gcc_unreachable();
5125 }
5126 }
68a29e1c 5127 result = build2 (COMPLEX_EXPR, result_type, real, imag);
68a29e1c 5128 return result;
1ff6b2c8 5129 }
8d08fdba
MS
5130
5131 /* For certain operations (which identify themselves by shorten != 0)
5132 if both args were extended from the same smaller type,
5133 do the arithmetic in that type and then extend.
5134
5135 shorten !=0 and !=1 indicates a bitwise operation.
5136 For them, this optimization is safe only if
5137 both args are zero-extended or both are sign-extended.
5138 Otherwise, we might change the result.
39a13be5 5139 E.g., (short)-1 | (unsigned short)-1 is (int)-1
8d08fdba
MS
5140 but calculated in (unsigned short) it would be (unsigned short)-1. */
5141
37c46b43 5142 if (shorten && none_complex)
8d08fdba 5143 {
8d08fdba 5144 final_type = result_type;
de5a5fa1 5145 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 5146 shorten == -1);
8d08fdba
MS
5147 }
5148
8d08fdba
MS
5149 /* Comparison operations are shortened too but differently.
5150 They identify themselves by setting short_compare = 1. */
5151
5152 if (short_compare)
5153 {
cda0a029
JM
5154 /* We call shorten_compare only for diagnostic-reason. */
5155 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5156 xresult_type = result_type;
8d08fdba 5157 enum tree_code xresultcode = resultcode;
cda0a029 5158 shorten_compare (location, &xop0, &xop1, &xresult_type,
393e8e8b 5159 &xresultcode);
8d08fdba
MS
5160 }
5161
8d3b081e 5162 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
7d48af30
MM
5163 && warn_sign_compare
5164 /* Do not warn until the template is instantiated; we cannot
5165 bound the ranges of the arguments until that point. */
2d12797c 5166 && !processing_template_decl
98f80e91 5167 && (complain & tf_warning)
12448f77
ILT
5168 && c_inhibit_evaluation_warnings == 0
5169 /* Even unsigned enum types promote to signed int. We don't
5170 want to issue -Wsign-compare warnings for this case. */
5171 && !enum_cast_to_int (orig_op0)
5172 && !enum_cast_to_int (orig_op1))
8d08fdba 5173 {
0d23cf7a
JJ
5174 tree oop0 = maybe_constant_value (orig_op0);
5175 tree oop1 = maybe_constant_value (orig_op1);
5176
5177 if (TREE_CODE (oop0) != INTEGER_CST)
cda0a029 5178 oop0 = cp_fully_fold (orig_op0);
0d23cf7a 5179 if (TREE_CODE (oop1) != INTEGER_CST)
cda0a029 5180 oop1 = cp_fully_fold (orig_op1);
0d23cf7a 5181 warn_for_sign_compare (location, oop0, oop1, op0, op1,
ba47d38d 5182 result_type, resultcode);
8d08fdba
MS
5183 }
5184 }
5185
b9edb4b1
MLI
5186 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5187 Then the expression will be built.
5188 It will be given type FINAL_TYPE if that is nonzero;
5189 otherwise, it will be given type RESULT_TYPE. */
8d08fdba
MS
5190 if (! converted)
5191 {
5192 if (TREE_TYPE (op0) != result_type)
4b978f96 5193 op0 = cp_convert_and_check (result_type, op0, complain);
8d08fdba 5194 if (TREE_TYPE (op1) != result_type)
4b978f96 5195 op1 = cp_convert_and_check (result_type, op1, complain);
848b92e1
JM
5196
5197 if (op0 == error_mark_node || op1 == error_mark_node)
5198 return error_mark_node;
8d08fdba
MS
5199 }
5200
28cbf42c
MS
5201 if (build_type == NULL_TREE)
5202 build_type = result_type;
5203
f8ed5150
MP
5204 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5205 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 5206 && !processing_template_decl
f5481fc4 5207 && do_ubsan_in_current_function ()
de5a5fa1
MP
5208 && (doing_div_or_mod || doing_shift))
5209 {
5210 /* OP0 and/or OP1 might have side-effects. */
5211 op0 = cp_save_expr (op0);
5212 op1 = cp_save_expr (op1);
234bef96
PC
5213 op0 = fold_non_dependent_expr (op0);
5214 op1 = fold_non_dependent_expr (op1);
f8ed5150
MP
5215 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5216 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1
MP
5217 {
5218 /* For diagnostics we want to use the promoted types without
5219 shorten_binary_op. So convert the arguments to the
5220 original result_type. */
5221 tree cop0 = op0;
5222 tree cop1 = op1;
98a7a34e
MP
5223 if (TREE_TYPE (cop0) != orig_type)
5224 cop0 = cp_convert (orig_type, op0, complain);
5225 if (TREE_TYPE (cop1) != orig_type)
5226 cop1 = cp_convert (orig_type, op1, complain);
de5a5fa1
MP
5227 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5228 }
a24d975c 5229 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
5230 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5231 }
5232
e87eed2a 5233 result = build2_loc (location, resultcode, build_type, op0, op1);
455f19cb 5234 if (final_type != 0)
4b978f96 5235 result = cp_convert (final_type, result, complain);
16fd4d2d 5236
a24d975c 5237 if (instrument_expr != NULL)
cda0a029
JM
5238 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5239 instrument_expr, result);
5240
5241 if (!processing_template_decl)
5242 {
5243 op0 = cp_fully_fold (op0);
5244 /* Only consider the second argument if the first isn't overflowed. */
5245 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5246 return result;
5247 op1 = cp_fully_fold (op1);
5248 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5249 return result;
5250 }
5251 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5252 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5253 return result;
5254
5255 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5256 if (TREE_OVERFLOW_P (result_ovl))
5257 overflow_warning (location, result_ovl);
de5a5fa1 5258
455f19cb 5259 return result;
8d08fdba 5260}
bedc293e
MG
5261
5262/* Build a VEC_PERM_EXPR.
5263 This is a simple wrapper for c_build_vec_perm_expr. */
5264tree
5265build_x_vec_perm_expr (location_t loc,
5266 tree arg0, tree arg1, tree arg2,
5267 tsubst_flags_t complain)
5268{
4939c5f3
MG
5269 tree orig_arg0 = arg0;
5270 tree orig_arg1 = arg1;
5271 tree orig_arg2 = arg2;
5272 if (processing_template_decl)
5273 {
5274 if (type_dependent_expression_p (arg0)
bedc293e 5275 || type_dependent_expression_p (arg1)
4939c5f3
MG
5276 || type_dependent_expression_p (arg2))
5277 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5278 arg0 = build_non_dependent_expr (arg0);
5279 if (arg1)
5280 arg1 = build_non_dependent_expr (arg1);
5281 arg2 = build_non_dependent_expr (arg2);
5282 }
5283 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5284 if (processing_template_decl && exp != error_mark_node)
5285 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5286 orig_arg1, orig_arg2);
5287 return exp;
bedc293e 5288}
8d08fdba
MS
5289\f
5290/* Return a tree for the sum or difference (RESULTCODE says which)
5291 of pointer PTROP and integer INTOP. */
5292
5293static tree
4f2e1536
MP
5294cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
5295 tree intop, tsubst_flags_t complain)
8d08fdba 5296{
bfba94bd
MM
5297 tree res_type = TREE_TYPE (ptrop);
5298
5299 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5300 in certain circumstance (when it's valid to do so). So we need
5301 to make sure it's complete. We don't need to check here, if we
5302 can actually complete it at all, as those checks will be done in
5303 pointer_int_sum() anyway. */
5304 complete_type (TREE_TYPE (res_type));
8f259df3 5305
4f2e1536 5306 return pointer_int_sum (loc, resultcode, ptrop,
cda0a029 5307 intop, complain & tf_warning_or_error);
8d08fdba
MS
5308}
5309
5310/* Return a tree for the difference of pointers OP0 and OP1.
5311 The resulting tree has type int. */
5312
5313static tree
4f2e1536
MP
5314pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
5315 tsubst_flags_t complain)
8d08fdba 5316{
6de9cd9a 5317 tree result;
8d08fdba 5318 tree restype = ptrdiff_type_node;
79a7c7fa 5319 tree target_type = TREE_TYPE (ptrtype);
8d08fdba 5320
66543169 5321 if (!complete_type_or_else (target_type, NULL_TREE))
8f259df3
MM
5322 return error_mark_node;
5323
50e10fa8 5324 if (VOID_TYPE_P (target_type))
4b978f96
PC
5325 {
5326 if (complain & tf_error)
4f2e1536 5327 permerror (loc, "ISO C++ forbids using pointer of "
4b978f96
PC
5328 "type %<void *%> in subtraction");
5329 else
5330 return error_mark_node;
5331 }
fcf73884 5332 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4b978f96
PC
5333 {
5334 if (complain & tf_error)
4f2e1536 5335 permerror (loc, "ISO C++ forbids using pointer to "
4b978f96
PC
5336 "a function in subtraction");
5337 else
5338 return error_mark_node;
5339 }
fcf73884 5340 if (TREE_CODE (target_type) == METHOD_TYPE)
4b978f96
PC
5341 {
5342 if (complain & tf_error)
4f2e1536 5343 permerror (loc, "ISO C++ forbids using pointer to "
4b978f96
PC
5344 "a method in subtraction");
5345 else
5346 return error_mark_node;
5347 }
8d08fdba
MS
5348
5349 /* First do the subtraction as integers;
5350 then drop through to build the divide operator. */
5351
4f2e1536 5352 op0 = cp_build_binary_op (loc,
ba47d38d 5353 MINUS_EXPR,
4b978f96
PC
5354 cp_convert (restype, op0, complain),
5355 cp_convert (restype, op1, complain),
5356 complain);
8d08fdba
MS
5357
5358 /* This generates an error if op1 is a pointer to an incomplete type. */
d0f062fb 5359 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
4b978f96
PC
5360 {
5361 if (complain & tf_error)
4f2e1536
MP
5362 error_at (loc, "invalid use of a pointer to an incomplete type in "
5363 "pointer arithmetic");
4b978f96
PC
5364 else
5365 return error_mark_node;
5366 }
8d08fdba 5367
f04dda30
MP
5368 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5369 {
5370 if (complain & tf_error)
4f2e1536 5371 error_at (loc, "arithmetic on pointer to an empty aggregate");
f04dda30
MP
5372 else
5373 return error_mark_node;
5374 }
5375
c8094d83 5376 op1 = (TYPE_PTROB_P (ptrtype)
4f2e1536 5377 ? size_in_bytes_loc (loc, target_type)
a5ac359a 5378 : integer_one_node);
8d08fdba
MS
5379
5380 /* Do the division. */
5381
4f2e1536
MP
5382 result = build2_loc (loc, EXACT_DIV_EXPR, restype, op0,
5383 cp_convert (restype, op1, complain));
cda0a029 5384 return result;
8d08fdba
MS
5385}
5386\f
8d08fdba
MS
5387/* Construct and perhaps optimize a tree representation
5388 for a unary operation. CODE, a tree_code, specifies the operation
5389 and XARG is the operand. */
5390
5391tree
e87eed2a 5392build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
4fe977f2 5393 tsubst_flags_t complain)
8d08fdba 5394{
d17811fd 5395 tree orig_expr = xarg;
19420d00
NS
5396 tree exp;
5397 int ptrmem = 0;
fcb9363e 5398 tree overload = NULL_TREE;
c8094d83 5399
5156628f 5400 if (processing_template_decl)
d17811fd
MM
5401 {
5402 if (type_dependent_expression_p (xarg))
e87eed2a 5403 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
3601f003 5404
d17811fd
MM
5405 xarg = build_non_dependent_expr (xarg);
5406 }
5407
5408 exp = NULL_TREE;
5566b478 5409
1e2e9f54
MM
5410 /* [expr.unary.op] says:
5411
5412 The address of an object of incomplete type can be taken.
5413
5414 (And is just the ordinary address operator, not an overloaded
5415 "operator &".) However, if the type is a template
5416 specialization, we must complete the type at this point so that
5417 an overloaded "operator &" will be available if required. */
db5ae43f 5418 if (code == ADDR_EXPR
386b8a85 5419 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
1e2e9f54
MM
5420 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5421 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
db5ae43f 5422 || (TREE_CODE (xarg) == OFFSET_REF)))
f4f206f4 5423 /* Don't look for a function. */;
db5ae43f 5424 else
4fe977f2 5425 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
fcb9363e
PP
5426 NULL_TREE, &overload, complain);
5427
d17811fd 5428 if (!exp && code == ADDR_EXPR)
c91a56d2 5429 {
7e361ae6
JM
5430 if (is_overloaded_fn (xarg))
5431 {
5432 tree fn = get_first_fn (xarg);
5433 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5434 {
76545796
PC
5435 if (complain & tf_error)
5436 error (DECL_CONSTRUCTOR_P (fn)
5437 ? G_("taking address of constructor %qE")
5438 : G_("taking address of destructor %qE"),
e87eed2a 5439 xarg.get_value ());
7e361ae6
JM
5440 return error_mark_node;
5441 }
5442 }
5443
5444 /* A pointer to member-function can be formed only by saying
5445 &X::mf. */
1e14c7f0
GDR
5446 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5447 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5448 {
5ebbc0ee
NS
5449 if (TREE_CODE (xarg) != OFFSET_REF
5450 || !TYPE_P (TREE_OPERAND (xarg, 0)))
1e14c7f0 5451 {
76545796
PC
5452 if (complain & tf_error)
5453 {
5454 error ("invalid use of %qE to form a "
e87eed2a 5455 "pointer-to-member-function", xarg.get_value ());
76545796
PC
5456 if (TREE_CODE (xarg) != OFFSET_REF)
5457 inform (input_location, " a qualified-id is required");
5458 }
1e14c7f0
GDR
5459 return error_mark_node;
5460 }
5461 else
5462 {
76545796
PC
5463 if (complain & tf_error)
5464 error ("parentheses around %qE cannot be used to form a"
5465 " pointer-to-member-function",
e87eed2a 5466 xarg.get_value ());
76545796
PC
5467 else
5468 return error_mark_node;
1e14c7f0
GDR
5469 PTRMEM_OK_P (xarg) = 1;
5470 }
5471 }
c8094d83 5472
19420d00 5473 if (TREE_CODE (xarg) == OFFSET_REF)
0cbd7506
MS
5474 {
5475 ptrmem = PTRMEM_OK_P (xarg);
c8094d83 5476
0cbd7506
MS
5477 if (!ptrmem && !flag_ms_extensions
5478 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
4542128e
NS
5479 {
5480 /* A single non-static member, make sure we don't allow a
0cbd7506 5481 pointer-to-member. */
f293ce4b
RS
5482 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5483 TREE_OPERAND (xarg, 0),
5484 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
4542128e 5485 PTRMEM_OK_P (xarg) = ptrmem;
c8094d83 5486 }
0cbd7506 5487 }
93c0e0bb
JM
5488
5489 exp = cp_build_addr_expr_strict (xarg, complain);
c91a56d2
MS
5490 }
5491
d17811fd 5492 if (processing_template_decl && exp != error_mark_node)
fcb9363e
PP
5493 {
5494 if (overload != NULL_TREE)
5495 return (build_min_non_dep_op_overload
5496 (code, exp, overload, orig_expr, integer_zero_node));
5497
5498 exp = build_min_non_dep (code, exp, orig_expr,
5499 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5500 }
6439fffd
KL
5501 if (TREE_CODE (exp) == ADDR_EXPR)
5502 PTRMEM_OK_P (exp) = ptrmem;
19420d00 5503 return exp;
8d08fdba
MS
5504}
5505
be845b04
JJ
5506/* Construct and perhaps optimize a tree representation
5507 for __builtin_addressof operation. ARG specifies the operand. */
5508
5509tree
5510cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
5511{
5512 tree orig_expr = arg;
5513
5514 if (processing_template_decl)
5515 {
5516 if (type_dependent_expression_p (arg))
5517 return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
5518
5519 arg = build_non_dependent_expr (arg);
5520 }
5521
5522 tree exp = cp_build_addr_expr_strict (arg, complain);
5523
5524 if (processing_template_decl && exp != error_mark_node)
5525 exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
5526 return exp;
5527}
5528
78ef5b89
NB
5529/* Like c_common_truthvalue_conversion, but handle pointer-to-member
5530 constants, where a null value is represented by an INTEGER_CST of
5531 -1. */
96d6c610
JM
5532
5533tree
5671bf27 5534cp_truthvalue_conversion (tree expr)
96d6c610
JM
5535{
5536 tree type = TREE_TYPE (expr);
451dcc66 5537 if (TYPE_PTR_OR_PTRMEM_P (type)
9cca4e3d
JM
5538 /* Avoid ICE on invalid use of non-static member function. */
5539 || TREE_CODE (expr) == FUNCTION_DECL)
451dcc66 5540 return build_binary_op (input_location, NE_EXPR, expr, nullptr_node, 1);
96d6c610 5541 else
ba47d38d 5542 return c_common_truthvalue_conversion (input_location, expr);
96d6c610
JM
5543}
5544
5545/* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
c8094d83 5546
2986ae00 5547tree
5671bf27 5548condition_conversion (tree expr)
2986ae00 5549{
5566b478 5550 tree t;
5156628f 5551 if (processing_template_decl)
5566b478 5552 return expr;
1dad57e6
JM
5553 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5554 tf_warning_or_error, LOOKUP_NORMAL);
0ad28dde 5555 t = fold_build_cleanup_point_expr (boolean_type_node, t);
8ccc31eb 5556 return t;
2986ae00 5557}
c8094d83 5558
d3e1e89e
JM
5559/* Returns the address of T. This function will fold away
5560 ADDR_EXPR of INDIRECT_REF. */
7993382e
MM
5561
5562tree
5563build_address (tree t)
5564{
7993382e
MM
5565 if (error_operand_p (t) || !cxx_mark_addressable (t))
5566 return error_mark_node;
f2acb8ad 5567 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
d3e1e89e
JM
5568 t = build_fold_addr_expr (t);
5569 if (TREE_CODE (t) != ADDR_EXPR)
5570 t = rvalue (t);
5571 return t;
7993382e
MM
5572}
5573
5574/* Return a NOP_EXPR converting EXPR to TYPE. */
5575
5576tree
5577build_nop (tree type, tree expr)
5578{
7993382e
MM
5579 if (type == error_mark_node || error_operand_p (expr))
5580 return expr;
e87eed2a 5581 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
7993382e
MM
5582}
5583
93c0e0bb
JM
5584/* Take the address of ARG, whatever that means under C++ semantics.
5585 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5586 and class rvalues as well.
5587
5588 Nothing should call this function directly; instead, callers should use
5589 cp_build_addr_expr or cp_build_addr_expr_strict. */
5590
5591static tree
5592cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5593{
5594 tree argtype;
5595 tree val;
5596
5597 if (!arg || error_operand_p (arg))
5598 return error_mark_node;
5599
5600 arg = mark_lvalue_use (arg);
5601 argtype = lvalue_type (arg);
5602
9dc6f476 5603 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
93c0e0bb
JM
5604
5605 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5606 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5607 {
5608 /* They're trying to take the address of a unique non-static
5609 member function. This is ill-formed (except in MS-land),
5610 but let's try to DTRT.
5611 Note: We only handle unique functions here because we don't
5612 want to complain if there's a static overload; non-unique
5613 cases will be handled by instantiate_type. But we need to
5614 handle this case here to allow casts on the resulting PMF.
5615 We could defer this in non-MS mode, but it's easier to give
5616 a useful error here. */
5617
5618 /* Inside constant member functions, the `this' pointer
5619 contains an extra const qualifier. TYPE_MAIN_VARIANT
5620 is used here to remove this const from the diagnostics
5621 and the created OFFSET_REF. */
5622 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5623 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
9f635aba
PC
5624 if (!mark_used (fn, complain) && !(complain & tf_error))
5625 return error_mark_node;
93c0e0bb
JM
5626
5627 if (! flag_ms_extensions)
5628 {
5629 tree name = DECL_NAME (fn);
5630 if (!(complain & tf_error))
5631 return error_mark_node;
5632 else if (current_class_type
5633 && TREE_OPERAND (arg, 0) == current_class_ref)
5634 /* An expression like &memfn. */
5635 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5636 " or parenthesized non-static member function to form"
5637 " a pointer to member function. Say %<&%T::%D%>",
5638 base, name);
5639 else
5640 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5641 " function to form a pointer to member function."
5642 " Say %<&%T::%D%>",
5643 base, name);
5644 }
a378996b 5645 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
93c0e0bb
JM
5646 }
5647
5648 /* Uninstantiated types are all functions. Taking the
5649 address of a function is a no-op, so just return the
5650 argument. */
5651 if (type_unknown_p (arg))
5652 return build1 (ADDR_EXPR, unknown_type_node, arg);
5653
5654 if (TREE_CODE (arg) == OFFSET_REF)
5655 /* We want a pointer to member; bypass all the code for actually taking
5656 the address of something. */
5657 goto offset_ref;
5658
5659 /* Anything not already handled and not a true memory reference
5660 is an error. */
5661 if (TREE_CODE (argtype) != FUNCTION_TYPE
5662 && TREE_CODE (argtype) != METHOD_TYPE)
5663 {
4e9ca9b0
JM
5664 cp_lvalue_kind kind = lvalue_kind (arg);
5665 if (kind == clk_none)
93c0e0bb
JM
5666 {
5667 if (complain & tf_error)
7bd11157 5668 lvalue_error (input_location, lv_addressof);
93c0e0bb
JM
5669 return error_mark_node;
5670 }
4e9ca9b0
JM
5671 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5672 {
5673 if (!(complain & tf_error))
5674 return error_mark_node;
5675 if (kind & clk_class)
5676 /* Make this a permerror because we used to accept it. */
5677 permerror (input_location, "taking address of temporary");
5678 else
5679 error ("taking address of xvalue (rvalue reference)");
5680 }
93c0e0bb
JM
5681 }
5682
5683 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5684 {
5685 tree type = build_pointer_type (TREE_TYPE (argtype));
5686 arg = build1 (CONVERT_EXPR, type, arg);
5687 return arg;
5688 }
5689 else if (pedantic && DECL_MAIN_P (arg))
5690 {
5691 /* ARM $3.4 */
5692 /* Apparently a lot of autoconf scripts for C++ packages do this,
c1771a20 5693 so only complain if -Wpedantic. */
93c0e0bb 5694 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
c1771a20 5695 pedwarn (input_location, OPT_Wpedantic,
93c0e0bb
JM
5696 "ISO C++ forbids taking address of function %<::main%>");
5697 else if (flag_pedantic_errors)
5698 return error_mark_node;
5699 }
5700
5701 /* Let &* cancel out to simplify resulting code. */
591cb3cf 5702 if (INDIRECT_REF_P (arg))
93c0e0bb 5703 {
93c0e0bb
JM
5704 arg = TREE_OPERAND (arg, 0);
5705 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5706 {
5707 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5708 arg = build1 (CONVERT_EXPR, type, arg);
5709 }
5710 else
5711 /* Don't let this be an lvalue. */
5712 arg = rvalue (arg);
5713 return arg;
5714 }
5715
5716 /* ??? Cope with user tricks that amount to offsetof. */
5717 if (TREE_CODE (argtype) != FUNCTION_TYPE
5718 && TREE_CODE (argtype) != METHOD_TYPE
5719 && argtype != unknown_type_node
5720 && (val = get_base_address (arg))
ccd2b322 5721 && COMPLETE_TYPE_P (TREE_TYPE (val))
591cb3cf 5722 && INDIRECT_REF_P (val)
93c0e0bb
JM
5723 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5724 {
5725 tree type = build_pointer_type (argtype);
cf9e9959 5726 return fold_convert (type, fold_offsetof_1 (arg));
93c0e0bb
JM
5727 }
5728
5729 /* Handle complex lvalues (when permitted)
5730 by reduction to simpler cases. */
5731 val = unary_complex_lvalue (ADDR_EXPR, arg);
5732 if (val != 0)
5733 return val;
5734
5735 switch (TREE_CODE (arg))
5736 {
5737 CASE_CONVERT:
5738 case FLOAT_EXPR:
5739 case FIX_TRUNC_EXPR:
bb19d4af
JM
5740 /* We should have handled this above in the lvalue_kind check. */
5741 gcc_unreachable ();
93c0e0bb
JM
5742 break;
5743
5744 case BASELINK:
5745 arg = BASELINK_FUNCTIONS (arg);
5746 /* Fall through. */
5747
5748 case OVERLOAD:
5749 arg = OVL_CURRENT (arg);
5750 break;
5751
5752 case OFFSET_REF:
5753 offset_ref:
5754 /* Turn a reference to a non-static data member into a
5755 pointer-to-member. */
5756 {
5757 tree type;
5758 tree t;
5759
5760 gcc_assert (PTRMEM_OK_P (arg));
5761
5762 t = TREE_OPERAND (arg, 1);
5763 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5764 {
5765 if (complain & tf_error)
5766 error ("cannot create pointer to reference member %qD", t);
5767 return error_mark_node;
5768 }
5769
5770 type = build_ptrmem_type (context_for_name_lookup (t),
5771 TREE_TYPE (t));
5772 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5773 return t;
5774 }
5775
5776 default:
5777 break;
5778 }
5779
5780 if (argtype != error_mark_node)
94a073b2 5781 argtype = build_pointer_type (argtype);
93c0e0bb 5782
47e5d7cc
JM
5783 if (bitfield_p (arg))
5784 {
5785 if (complain & tf_error)
5786 error ("attempt to take address of bit-field");
5787 return error_mark_node;
5788 }
5789
93c0e0bb
JM
5790 /* In a template, we are processing a non-dependent expression
5791 so we can just form an ADDR_EXPR with the correct type. */
5792 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5793 {
5794 val = build_address (arg);
5795 if (TREE_CODE (arg) == OFFSET_REF)
5796 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5797 }
c5ce25ce 5798 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
93c0e0bb
JM
5799 {
5800 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5801
5802 /* We can only get here with a single static member
5803 function. */
5804 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5805 && DECL_STATIC_FUNCTION_P (fn));
9f635aba
PC
5806 if (!mark_used (fn, complain) && !(complain & tf_error))
5807 return error_mark_node;
93c0e0bb
JM
5808 val = build_address (fn);
5809 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5810 /* Do not lose object's side effects. */
5811 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5812 TREE_OPERAND (arg, 0), val);
5813 }
93c0e0bb
JM
5814 else
5815 {
5816 tree object = TREE_OPERAND (arg, 0);
5817 tree field = TREE_OPERAND (arg, 1);
5818 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5819 (TREE_TYPE (object), decl_type_context (field)));
5820 val = build_address (arg);
5821 }
5822
50e10fa8 5823 if (TYPE_PTR_P (argtype)
93c0e0bb
JM
5824 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5825 {
5826 build_ptrmemfunc_type (argtype);
5827 val = build_ptrmemfunc (argtype, val, 0,
5828 /*c_cast_p=*/false,
a3299120 5829 complain);
93c0e0bb
JM
5830 }
5831
5832 return val;
5833}
5834
5835/* Take the address of ARG if it has one, even if it's an rvalue. */
5836
5837tree
5838cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5839{
5840 return cp_build_addr_expr_1 (arg, 0, complain);
5841}
5842
5843/* Take the address of ARG, but only if it's an lvalue. */
5844
882de214 5845static tree
93c0e0bb
JM
5846cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5847{
5848 return cp_build_addr_expr_1 (arg, 1, complain);
5849}
5850
8d08fdba
MS
5851/* C++: Must handle pointers to members.
5852
5853 Perhaps type instantiation should be extended to handle conversion
5854 from aggregates to types we don't yet know we want? (Or are those
5855 cases typically errors which should be reported?)
5856
e51fbec3 5857 NOCONVERT suppresses the default promotions (such as from short to int). */
e92cc029 5858
8d08fdba 5859tree
e51fbec3 5860cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
5ade1ed2 5861 tsubst_flags_t complain)
8d08fdba
MS
5862{
5863 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
926ce8bd 5864 tree arg = xarg;
9a2300e9 5865 location_t location = EXPR_LOC_OR_LOC (arg, input_location);
926ce8bd 5866 tree argtype = 0;
d8e178a0 5867 const char *errstring = NULL;
8d08fdba 5868 tree val;
4de67c26 5869 const char *invalid_op_diag;
8d08fdba 5870
fb80065c 5871 if (!arg || error_operand_p (arg))
8d08fdba
MS
5872 return error_mark_node;
5873
4de67c26
JM
5874 if ((invalid_op_diag
5875 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5876 ? CONVERT_EXPR
5877 : code),
5878 TREE_TYPE (xarg))))
5879 {
76545796
PC
5880 if (complain & tf_error)
5881 error (invalid_op_diag);
4de67c26
JM
5882 return error_mark_node;
5883 }
5884
8d08fdba
MS
5885 switch (code)
5886 {
392e3d51 5887 case UNARY_PLUS_EXPR:
b7484fbe 5888 case NEGATE_EXPR:
8a21aa30
KC
5889 {
5890 int flags = WANT_ARITH | WANT_ENUM;
5891 /* Unary plus (but not unary minus) is allowed on pointers. */
392e3d51 5892 if (code == UNARY_PLUS_EXPR)
8a21aa30
KC
5893 flags |= WANT_POINTER;
5894 arg = build_expr_type_conversion (flags, arg, true);
e99f332f
GB
5895 if (!arg)
5896 errstring = (code == NEGATE_EXPR
4cd5a50a
PB
5897 ? _("wrong type argument to unary minus")
5898 : _("wrong type argument to unary plus"));
e99f332f
GB
5899 else
5900 {
5901 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
4b978f96 5902 arg = cp_perform_integral_promotions (arg, complain);
e99f332f 5903
13a44ee0 5904 /* Make sure the result is not an lvalue: a unary plus or minus
e99f332f 5905 expression is always a rvalue. */
5cc53d4e 5906 arg = rvalue (arg);
e99f332f
GB
5907 }
5908 }
8d08fdba
MS
5909 break;
5910
5911 case BIT_NOT_EXPR:
37c46b43
MS
5912 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5913 {
5914 code = CONJ_EXPR;
5915 if (!noconvert)
89fcabaf
PC
5916 {
5917 arg = cp_default_conversion (arg, complain);
5918 if (arg == error_mark_node)
5919 return error_mark_node;
5920 }
37c46b43 5921 }
4576ceaf 5922 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
1ff6b2c8 5923 | WANT_VECTOR_OR_COMPLEX,
b746c5dc 5924 arg, true)))
4cd5a50a 5925 errstring = _("wrong type argument to bit-complement");
bd289f54 5926 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9a2300e9
MP
5927 {
5928 /* Warn if the expression has boolean value. */
5929 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
5930 && warning_at (location, OPT_Wbool_operation,
5931 "%<~%> on an expression of type bool"))
5932 inform (location, "did you mean to use logical not (%<!%>)?");
5933 arg = cp_perform_integral_promotions (arg, complain);
5934 }
abec4284
JJ
5935 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
5936 arg = mark_rvalue_use (arg);
8d08fdba
MS
5937 break;
5938
5939 case ABS_EXPR:
b746c5dc 5940 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4cd5a50a 5941 errstring = _("wrong type argument to abs");
8d08fdba 5942 else if (!noconvert)
89fcabaf
PC
5943 {
5944 arg = cp_default_conversion (arg, complain);
5945 if (arg == error_mark_node)
5946 return error_mark_node;
5947 }
8d08fdba
MS
5948 break;
5949
37c46b43
MS
5950 case CONJ_EXPR:
5951 /* Conjugating a real value is a no-op, but allow it anyway. */
b746c5dc 5952 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
4cd5a50a 5953 errstring = _("wrong type argument to conjugation");
37c46b43 5954 else if (!noconvert)
89fcabaf
PC
5955 {
5956 arg = cp_default_conversion (arg, complain);
5957 if (arg == error_mark_node)
5958 return error_mark_node;
5959 }
37c46b43
MS
5960 break;
5961
8d08fdba 5962 case TRUTH_NOT_EXPR:
48d3af12 5963 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
3c9aabbd
MG
5964 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5965 build_zero_cst (TREE_TYPE (arg)), complain);
5ade1ed2
DG
5966 arg = perform_implicit_conversion (boolean_type_node, arg,
5967 complain);
db3927fb 5968 val = invert_truthvalue_loc (input_location, arg);
2986ae00
MS
5969 if (arg != error_mark_node)
5970 return val;
4cd5a50a 5971 errstring = _("in argument to unary !");
8d08fdba
MS
5972 break;
5973
5974 case NOP_EXPR:
5975 break;
c8094d83 5976
37c46b43 5977 case REALPART_EXPR:
37c46b43 5978 case IMAGPART_EXPR:
fb52b50a 5979 arg = build_real_imag_expr (input_location, code, arg);
cda0a029 5980 return arg;
c8094d83 5981
8d08fdba
MS
5982 case PREINCREMENT_EXPR:
5983 case POSTINCREMENT_EXPR:
5984 case PREDECREMENT_EXPR:
5985 case POSTDECREMENT_EXPR:
5986 /* Handle complex lvalues (when permitted)
5987 by reduction to simpler cases. */
5988
5989 val = unary_complex_lvalue (code, arg);
5990 if (val != 0)
5991 return val;
5992
03a904b5
JJ
5993 arg = mark_lvalue_use (arg);
5994
37c46b43
MS
5995 /* Increment or decrement the real part of the value,
5996 and don't change the imaginary part. */
5997 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5998 {
5999 tree real, imag;
6000
5a0802ea 6001 arg = cp_stabilize_reference (arg);
e51fbec3
MP
6002 real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
6003 imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
6004 real = cp_build_unary_op (code, real, true, complain);
a87db577
AP
6005 if (real == error_mark_node || imag == error_mark_node)
6006 return error_mark_node;
f293ce4b 6007 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
a87db577 6008 real, imag);
37c46b43
MS
6009 }
6010
8d08fdba
MS
6011 /* Report invalid types. */
6012
b7484fbe 6013 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
b746c5dc 6014 arg, true)))
8d08fdba
MS
6015 {
6016 if (code == PREINCREMENT_EXPR)
4cd5a50a 6017 errstring = _("no pre-increment operator for type");
8d08fdba 6018 else if (code == POSTINCREMENT_EXPR)
4cd5a50a 6019 errstring = _("no post-increment operator for type");
8d08fdba 6020 else if (code == PREDECREMENT_EXPR)
4cd5a50a 6021 errstring = _("no pre-decrement operator for type");
8d08fdba 6022 else
4cd5a50a 6023 errstring = _("no post-decrement operator for type");
8d08fdba
MS
6024 break;
6025 }
affb3cb2
JJ
6026 else if (arg == error_mark_node)
6027 return error_mark_node;
8d08fdba
MS
6028
6029 /* Report something read-only. */
6030
91063b51 6031 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5ade1ed2
DG
6032 || TREE_READONLY (arg))
6033 {
6034 if (complain & tf_error)
4816c593
NF
6035 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
6036 || code == POSTINCREMENT_EXPR)
6037 ? lv_increment : lv_decrement));
5ade1ed2
DG
6038 else
6039 return error_mark_node;
6040 }
8d08fdba
MS
6041
6042 {
926ce8bd 6043 tree inc;
b70cef5d 6044 tree declared_type = unlowered_expr_type (arg);
8d08fdba 6045
8d08fdba
MS
6046 argtype = TREE_TYPE (arg);
6047
6048 /* ARM $5.2.5 last annotation says this should be forbidden. */
6049 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5ade1ed2
DG
6050 {
6051 if (complain & tf_error)
cbe5f3b3 6052 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
37ec60ed
JW
6053 ? G_("ISO C++ forbids incrementing an enum")
6054 : G_("ISO C++ forbids decrementing an enum"));
5ade1ed2
DG
6055 else
6056 return error_mark_node;
6057 }
c8094d83 6058
8d08fdba
MS
6059 /* Compute the increment. */
6060
50e10fa8 6061 if (TYPE_PTR_P (argtype))
8d08fdba 6062 {
d0f062fb 6063 tree type = complete_type (TREE_TYPE (argtype));
c8094d83 6064
d0f062fb 6065 if (!COMPLETE_OR_VOID_TYPE_P (type))
5ade1ed2
DG
6066 {
6067 if (complain & tf_error)
6068 error (((code == PREINCREMENT_EXPR
6069 || code == POSTINCREMENT_EXPR))
6070 ? G_("cannot increment a pointer to incomplete type %qT")
6071 : G_("cannot decrement a pointer to incomplete type %qT"),
6072 TREE_TYPE (argtype));
6073 else
6074 return error_mark_node;
6075 }
c42a832a 6076 else if (!TYPE_PTROB_P (argtype))
5ade1ed2
DG
6077 {
6078 if (complain & tf_error)
44d90fe1 6079 pedwarn (input_location, OPT_Wpointer_arith,
c42a832a 6080 (code == PREINCREMENT_EXPR
37ec60ed 6081 || code == POSTINCREMENT_EXPR)
c42a832a
PC
6082 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6083 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6084 argtype);
5ade1ed2
DG
6085 else
6086 return error_mark_node;
6087 }
6088
ea793912 6089 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
8d08fdba
MS
6090 }
6091 else
241b71bb
TV
6092 inc = VECTOR_TYPE_P (argtype)
6093 ? build_one_cst (argtype)
6094 : integer_one_node;
8d08fdba 6095
4b978f96 6096 inc = cp_convert (argtype, inc, complain);
8d08fdba 6097
925e8657
NP
6098 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6099 need to ask Objective-C to build the increment or decrement
6100 expression for it. */
6101 if (objc_is_property_ref (arg))
6102 return objc_build_incr_expr_for_property_ref (input_location, code,
6103 arg, inc);
6104
8d08fdba
MS
6105 /* Complain about anything else that is not a true lvalue. */
6106 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6107 || code == POSTINCREMENT_EXPR)
5ade1ed2
DG
6108 ? lv_increment : lv_decrement),
6109 complain))
8d08fdba
MS
6110 return error_mark_node;
6111
20a1e5b8 6112 /* Forbid using -- or ++ in C++17 on `bool'. */
66be89f0 6113 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
b7484fbe
MS
6114 {
6115 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6116 {
5ade1ed2 6117 if (complain & tf_error)
20a1e5b8
MP
6118 error ("use of an operand of type %qT in %<operator--%> "
6119 "is forbidden", boolean_type_node);
b7484fbe
MS
6120 return error_mark_node;
6121 }
20a1e5b8
MP
6122 else
6123 {
6124 if (cxx_dialect >= cxx1z)
6125 {
6126 if (complain & tf_error)
6127 error ("use of an operand of type %qT in "
6128 "%<operator++%> is forbidden in C++1z",
6129 boolean_type_node);
6130 return error_mark_node;
6131 }
6132 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6133 else if (!in_system_header_at (input_location))
6134 warning (OPT_Wdeprecated, "use of an operand of type %qT "
6135 "in %<operator++%> is deprecated",
6136 boolean_type_node);
6137 }
19552aa5 6138 val = boolean_increment (code, arg);
b7484fbe 6139 }
9127e994
JM
6140 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6141 /* An rvalue has no cv-qualifiers. */
6142 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
b7484fbe 6143 else
f293ce4b 6144 val = build2 (code, TREE_TYPE (arg), arg, inc);
b7484fbe 6145
8d08fdba 6146 TREE_SIDE_EFFECTS (val) = 1;
b70cef5d 6147 return val;
8d08fdba
MS
6148 }
6149
6150 case ADDR_EXPR:
6151 /* Note that this operation never does default_conversion
6152 regardless of NOCONVERT. */
93c0e0bb 6153 return cp_build_addr_expr (arg, complain);
7f85441b
KG
6154
6155 default:
6156 break;
8d08fdba
MS
6157 }
6158
6159 if (!errstring)
6160 {
6161 if (argtype == 0)
6162 argtype = TREE_TYPE (arg);
cda0a029 6163 return build1 (code, argtype, arg);
8d08fdba
MS
6164 }
6165
5ade1ed2
DG
6166 if (complain & tf_error)
6167 error ("%s", errstring);
8d08fdba
MS
6168 return error_mark_node;
6169}
6170
5ade1ed2
DG
6171/* Hook for the c-common bits that build a unary op. */
6172tree
12308bc6 6173build_unary_op (location_t /*location*/,
e51fbec3 6174 enum tree_code code, tree xarg, bool noconvert)
5ade1ed2
DG
6175{
6176 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6177}
6178
8d08fdba
MS
6179/* Apply unary lvalue-demanding operator CODE to the expression ARG
6180 for certain kinds of expressions which are not really lvalues
6181 but which we can accept as lvalues.
6182
dfb5c523
MM
6183 If ARG is not a kind of expression we can handle, return
6184 NULL_TREE. */
c8094d83 6185
8d08fdba 6186tree
acd8e2d0 6187unary_complex_lvalue (enum tree_code code, tree arg)
8d08fdba 6188{
dfb5c523
MM
6189 /* Inside a template, making these kinds of adjustments is
6190 pointless; we are only concerned with the type of the
6191 expression. */
6192 if (processing_template_decl)
6193 return NULL_TREE;
6194
8d08fdba
MS
6195 /* Handle (a, b) used as an "lvalue". */
6196 if (TREE_CODE (arg) == COMPOUND_EXPR)
6197 {
e51fbec3 6198 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
5ade1ed2 6199 tf_warning_or_error);
f293ce4b
RS
6200 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6201 TREE_OPERAND (arg, 0), real_result);
8d08fdba
MS
6202 }
6203
6204 /* Handle (a ? b : c) used as an "lvalue". */
e9a25f70
JL
6205 if (TREE_CODE (arg) == COND_EXPR
6206 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5ade1ed2 6207 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
a4443a08 6208
ae818d3b 6209 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
e1cd6e56
MS
6210 if (TREE_CODE (arg) == MODIFY_EXPR
6211 || TREE_CODE (arg) == PREINCREMENT_EXPR
6212 || TREE_CODE (arg) == PREDECREMENT_EXPR)
ae818d3b
ER
6213 {
6214 tree lvalue = TREE_OPERAND (arg, 0);
6215 if (TREE_SIDE_EFFECTS (lvalue))
6216 {
5a0802ea 6217 lvalue = cp_stabilize_reference (lvalue);
f293ce4b
RS
6218 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6219 lvalue, TREE_OPERAND (arg, 1));
ae818d3b
ER
6220 }
6221 return unary_complex_lvalue
f293ce4b 6222 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
ae818d3b 6223 }
8d08fdba
MS
6224
6225 if (code != ADDR_EXPR)
33fd4f49 6226 return NULL_TREE;
8d08fdba
MS
6227
6228 /* Handle (a = b) used as an "lvalue" for `&'. */
6229 if (TREE_CODE (arg) == MODIFY_EXPR
6230 || TREE_CODE (arg) == INIT_EXPR)
6231 {
e51fbec3 6232 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
5ade1ed2 6233 tf_warning_or_error);
f293ce4b
RS
6234 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6235 arg, real_result);
6de9cd9a 6236 TREE_NO_WARNING (arg) = 1;
faf5394a 6237 return arg;
8d08fdba
MS
6238 }
6239
8d08fdba
MS
6240 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6241 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
a5ac359a 6242 || TREE_CODE (arg) == OFFSET_REF)
6d05585b 6243 return NULL_TREE;
c8094d83 6244
8d08fdba
MS
6245 /* We permit compiler to make function calls returning
6246 objects of aggregate type look like lvalues. */
6247 {
6248 tree targ = arg;
6249
6250 if (TREE_CODE (targ) == SAVE_EXPR)
6251 targ = TREE_OPERAND (targ, 0);
6252
9e1e64ec 6253 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
8d08fdba
MS
6254 {
6255 if (TREE_CODE (arg) == SAVE_EXPR)
6256 targ = arg;
6257 else
362115a9 6258 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
f30432d7 6259 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
8d08fdba
MS
6260 }
6261
591cb3cf 6262 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
f293ce4b 6263 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
8d08fdba 6264 TREE_OPERAND (targ, 0), current_function_decl, NULL);
8d08fdba
MS
6265 }
6266
6267 /* Don't let anything else be handled specially. */
33fd4f49 6268 return NULL_TREE;
8d08fdba 6269}
8d08fdba
MS
6270\f
6271/* Mark EXP saying that we need to be able to take the
6272 address of it; it should not be allocated in a register.
a9e4a1a5
JJ
6273 Value is true if successful. ARRAY_REF_P is true if this
6274 is for ARRAY_REF construction - in that case we don't want
6275 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6276 it is fine to use ARRAY_REFs for vector subscripts on vector
6277 register variables.
8d08fdba 6278
4ac14744 6279 C++: we do not allow `current_class_ptr' to be addressable. */
8d08fdba 6280
dffd7eb6 6281bool
a9e4a1a5 6282cxx_mark_addressable (tree exp, bool array_ref_p)
8d08fdba 6283{
926ce8bd 6284 tree x = exp;
8d08fdba 6285
8d08fdba
MS
6286 while (1)
6287 switch (TREE_CODE (x))
6288 {
a9e4a1a5
JJ
6289 case VIEW_CONVERT_EXPR:
6290 if (array_ref_p
6291 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
6292 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
6293 return true;
6294 /* FALLTHRU */
8d08fdba
MS
6295 case ADDR_EXPR:
6296 case COMPONENT_REF:
6297 case ARRAY_REF:
37c46b43
MS
6298 case REALPART_EXPR:
6299 case IMAGPART_EXPR:
8d08fdba
MS
6300 x = TREE_OPERAND (x, 0);
6301 break;
6302
6303 case PARM_DECL:
4ac14744 6304 if (x == current_class_ptr)
8d08fdba 6305 {
0cbd7506 6306 error ("cannot take the address of %<this%>, which is an rvalue expression");
f4f206f4 6307 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
dffd7eb6 6308 return true;
8d08fdba 6309 }
f4f206f4 6310 /* Fall through. */
aa8dea09 6311
8d08fdba 6312 case VAR_DECL:
8d08fdba
MS
6313 /* Caller should not be trying to mark initialized
6314 constant fields addressable. */
50bc768d
NS
6315 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6316 || DECL_IN_AGGR_P (x) == 0
6317 || TREE_STATIC (x)
6318 || DECL_EXTERNAL (x));
f4f206f4 6319 /* Fall through. */
8d08fdba 6320
8d08fdba 6321 case RESULT_DECL:
9a3b49ac 6322 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
1b8d7c9a 6323 && !DECL_ARTIFICIAL (x))
8ef36086 6324 {
5a6ccc94 6325 if (VAR_P (x) && DECL_HARD_REGISTER (x))
8ef36086
AP
6326 {
6327 error
6328 ("address of explicit register variable %qD requested", x);
6329 return false;
6330 }
778f6a08 6331 else if (extra_warnings)
8ef36086 6332 warning
b323323f 6333 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
8ef36086 6334 }
8d08fdba 6335 TREE_ADDRESSABLE (x) = 1;
dffd7eb6 6336 return true;
8d08fdba 6337
81c1113a 6338 case CONST_DECL:
8d08fdba 6339 case FUNCTION_DECL:
8d08fdba 6340 TREE_ADDRESSABLE (x) = 1;
dffd7eb6 6341 return true;
8d08fdba 6342
e92cc029
MS
6343 case CONSTRUCTOR:
6344 TREE_ADDRESSABLE (x) = 1;
dffd7eb6 6345 return true;
e92cc029 6346
9a3b49ac
MS
6347 case TARGET_EXPR:
6348 TREE_ADDRESSABLE (x) = 1;
dffd7eb6
NB
6349 cxx_mark_addressable (TREE_OPERAND (x, 0));
6350 return true;
9a3b49ac 6351
8d08fdba 6352 default:
dffd7eb6 6353 return true;
8d08fdba
MS
6354 }
6355}
6356\f
6357/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6358
6359tree
f330f599 6360build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
5ade1ed2 6361 tsubst_flags_t complain)
8d08fdba 6362{
d17811fd
MM
6363 tree orig_ifexp = ifexp;
6364 tree orig_op1 = op1;
6365 tree orig_op2 = op2;
6366 tree expr;
6367
5156628f 6368 if (processing_template_decl)
d17811fd
MM
6369 {
6370 /* The standard says that the expression is type-dependent if
6371 IFEXP is type-dependent, even though the eventual type of the
6372 expression doesn't dependent on IFEXP. */
6373 if (type_dependent_expression_p (ifexp)
18fd68a8
MM
6374 /* As a GNU extension, the middle operand may be omitted. */
6375 || (op1 && type_dependent_expression_p (op1))
d17811fd 6376 || type_dependent_expression_p (op2))
f330f599 6377 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
d17811fd 6378 ifexp = build_non_dependent_expr (ifexp);
18fd68a8
MM
6379 if (op1)
6380 op1 = build_non_dependent_expr (op1);
d17811fd
MM
6381 op2 = build_non_dependent_expr (op2);
6382 }
5566b478 6383
4cbc4bd7 6384 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
f2111a36 6385 if (processing_template_decl && expr != error_mark_node)
ca8dc274
JM
6386 {
6387 tree min = build_min_non_dep (COND_EXPR, expr,
6388 orig_ifexp, orig_op1, orig_op2);
92886d3e 6389 /* Remember that the result is an lvalue or xvalue. */
c3edc633 6390 if (glvalue_p (expr) && !glvalue_p (min))
ca8dc274 6391 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
72b3e203 6392 !lvalue_p (expr));
ca8dc274
JM
6393 expr = convert_from_reference (min);
6394 }
d17811fd 6395 return expr;
8d08fdba
MS
6396}
6397\f
c7b62f14 6398/* Given a list of expressions, return a compound expression
04c06002 6399 that performs them all and returns the value of the last of them. */
c7b62f14 6400
2f5b91f5 6401tree
d555b1c7
PC
6402build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6403 tsubst_flags_t complain)
c7b62f14
NS
6404{
6405 tree expr = TREE_VALUE (list);
c8094d83 6406
d3de8016
JM
6407 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6408 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6409 {
6410 if (complain & tf_error)
8400e75e
DM
6411 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6412 "list-initializer for non-class type must not "
6413 "be parenthesized");
d3de8016
JM
6414 else
6415 return error_mark_node;
6416 }
6417
c7b62f14
NS
6418 if (TREE_CHAIN (list))
6419 {
d555b1c7
PC
6420 if (complain & tf_error)
6421 switch (exp)
6422 {
2f5b91f5
SZ
6423 case ELK_INIT:
6424 permerror (input_location, "expression list treated as compound "
6425 "expression in initializer");
6426 break;
6427 case ELK_MEM_INIT:
6428 permerror (input_location, "expression list treated as compound "
6429 "expression in mem-initializer");
6430 break;
6431 case ELK_FUNC_CAST:
6432 permerror (input_location, "expression list treated as compound "
6433 "expression in functional cast");
6434 break;
6435 default:
6436 gcc_unreachable ();
d555b1c7 6437 }
6dab6cf1
JM
6438 else
6439 return error_mark_node;
c7b62f14
NS
6440
6441 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
f330f599
PC
6442 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6443 expr, TREE_VALUE (list), complain);
c7b62f14 6444 }
c8094d83 6445
c7b62f14
NS
6446 return expr;
6447}
6448
c166b898
ILT
6449/* Like build_x_compound_expr_from_list, but using a VEC. */
6450
6451tree
9771b263 6452build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
e2e03032 6453 tsubst_flags_t complain)
c166b898 6454{
9771b263 6455 if (vec_safe_is_empty (vec))
c166b898 6456 return NULL_TREE;
9771b263
DN
6457 else if (vec->length () == 1)
6458 return (*vec)[0];
c166b898
ILT
6459 else
6460 {
6461 tree expr;
6462 unsigned int ix;
6463 tree t;
6464
6465 if (msg != NULL)
e2e03032
PC
6466 {
6467 if (complain & tf_error)
6468 permerror (input_location,
6469 "%s expression list treated as compound expression",
6470 msg);
6471 else
6472 return error_mark_node;
6473 }
c166b898 6474
9771b263
DN
6475 expr = (*vec)[0];
6476 for (ix = 1; vec->iterate (ix, &t); ++ix)
f330f599 6477 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
e2e03032 6478 t, complain);
c166b898
ILT
6479
6480 return expr;
6481 }
6482}
6483
c7b62f14 6484/* Handle overloading of the ',' operator when needed. */
e92cc029 6485
8d08fdba 6486tree
f330f599
PC
6487build_x_compound_expr (location_t loc, tree op1, tree op2,
6488 tsubst_flags_t complain)
8d08fdba 6489{
8d08fdba 6490 tree result;
d17811fd
MM
6491 tree orig_op1 = op1;
6492 tree orig_op2 = op2;
fcb9363e 6493 tree overload = NULL_TREE;
8d08fdba 6494
5156628f 6495 if (processing_template_decl)
d17811fd
MM
6496 {
6497 if (type_dependent_expression_p (op1)
6498 || type_dependent_expression_p (op2))
f330f599 6499 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
d17811fd
MM
6500 op1 = build_non_dependent_expr (op1);
6501 op2 = build_non_dependent_expr (op2);
6502 }
b19b4a78 6503
f330f599 6504 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
fcb9363e 6505 NULL_TREE, &overload, complain);
d17811fd 6506 if (!result)
525521b6 6507 result = cp_build_compound_expr (op1, op2, complain);
b19b4a78 6508
d17811fd 6509 if (processing_template_decl && result != error_mark_node)
fcb9363e
PP
6510 {
6511 if (overload != NULL_TREE)
6512 return (build_min_non_dep_op_overload
6513 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6514
6515 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6516 }
c8094d83 6517
d17811fd 6518 return result;
8d08fdba
MS
6519}
6520
525521b6
DG
6521/* Like cp_build_compound_expr, but for the c-common bits. */
6522
6523tree
12308bc6 6524build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
525521b6
DG
6525{
6526 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6527}
6528
04c06002 6529/* Build a compound expression. */
8d08fdba
MS
6530
6531tree
525521b6 6532cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
8d08fdba 6533{
ebeb2c24 6534 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
c8094d83 6535
c7b62f14 6536 if (lhs == error_mark_node || rhs == error_mark_node)
66543169 6537 return error_mark_node;
c8094d83 6538
b72271b9 6539 if (flag_cilkplus
12893402
BI
6540 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6541 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6542 {
6543 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6544 : EXPR_LOCATION (rhs));
6545 error_at (loc,
6546 "spawned function call cannot be part of a comma expression");
6547 return error_mark_node;
6548 }
6549
d340e53f
NS
6550 if (TREE_CODE (rhs) == TARGET_EXPR)
6551 {
6552 /* If the rhs is a TARGET_EXPR, then build the compound
0cbd7506 6553 expression inside the target_expr's initializer. This
cd0be382 6554 helps the compiler to eliminate unnecessary temporaries. */
d340e53f 6555 tree init = TREE_OPERAND (rhs, 1);
c8094d83 6556
f293ce4b 6557 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
d340e53f 6558 TREE_OPERAND (rhs, 1) = init;
c8094d83 6559
d340e53f
NS
6560 return rhs;
6561 }
c8094d83 6562
95e20768
NS
6563 if (type_unknown_p (rhs))
6564 {
76545796
PC
6565 if (complain & tf_error)
6566 error ("no context to resolve type of %qE", rhs);
95e20768
NS
6567 return error_mark_node;
6568 }
6569
f293ce4b 6570 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
8d08fdba
MS
6571}
6572
33c25e5c 6573/* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
3f89235e
JM
6574 casts away constness. CAST gives the type of cast. Returns true
6575 if the cast is ill-formed, false if it is well-formed.
36ef7262
MLI
6576
6577 ??? This function warns for casting away any qualifier not just
6578 const. We would like to specify exactly what qualifiers are casted
6579 away.
6580*/
3fe18f1d 6581
3f89235e 6582static bool
a5ac359a 6583check_for_casting_away_constness (tree src_type, tree dest_type,
3f89235e 6584 enum tree_code cast, tsubst_flags_t complain)
3fe18f1d 6585{
86b7b98b
MLI
6586 /* C-style casts are allowed to cast away constness. With
6587 WARN_CAST_QUAL, we still want to issue a warning. */
6588 if (cast == CAST_EXPR && !warn_cast_qual)
3f89235e 6589 return false;
86b7b98b 6590
b40e334f 6591 if (!casts_away_constness (src_type, dest_type, complain))
3f89235e 6592 return false;
36ef7262
MLI
6593
6594 switch (cast)
6595 {
6596 case CAST_EXPR:
3f89235e
JM
6597 if (complain & tf_warning)
6598 warning (OPT_Wcast_qual,
6599 "cast from type %qT to type %qT casts away qualifiers",
6600 src_type, dest_type);
6601 return false;
36ef7262
MLI
6602
6603 case STATIC_CAST_EXPR:
3f89235e
JM
6604 if (complain & tf_error)
6605 error ("static_cast from type %qT to type %qT casts away qualifiers",
6606 src_type, dest_type);
6607 return true;
36ef7262
MLI
6608
6609 case REINTERPRET_CAST_EXPR:
3f89235e
JM
6610 if (complain & tf_error)
6611 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6612 src_type, dest_type);
6613 return true;
6614
36ef7262
MLI
6615 default:
6616 gcc_unreachable();
6617 }
3fe18f1d
MM
6618}
6619
04398fa8
PC
6620/*
6621 Warns if the cast from expression EXPR to type TYPE is useless.
6622 */
6623void
6624maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6625{
6626 if (warn_useless_cast
ca7e759d 6627 && complain & tf_warning)
04398fa8 6628 {
04398fa8
PC
6629 if ((TREE_CODE (type) == REFERENCE_TYPE
6630 && (TYPE_REF_IS_RVALUE (type)
72b3e203 6631 ? xvalue_p (expr) : lvalue_p (expr))
04398fa8
PC
6632 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6633 || same_type_p (TREE_TYPE (expr), type))
6634 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6635 }
6636}
6637
08e17d9d
MM
6638/* Convert EXPR (an expression with pointer-to-member type) to TYPE
6639 (another pointer-to-member type in the same hierarchy) and return
6640 the converted expression. If ALLOW_INVERSE_P is permitted, a
6641 pointer-to-derived may be converted to pointer-to-base; otherwise,
6642 only the other direction is permitted. If C_CAST_P is true, this
6643 conversion is taking place as part of a C-style cast. */
6644
c8094d83 6645tree
08e17d9d 6646convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
135d47df 6647 bool c_cast_p, tsubst_flags_t complain)
08e17d9d 6648{
66b1156a 6649 if (TYPE_PTRDATAMEM_P (type))
08e17d9d
MM
6650 {
6651 tree delta;
6652
6653 if (TREE_CODE (expr) == PTRMEM_CST)
6654 expr = cplus_expand_constant (expr);
6655 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
c8094d83 6656 TYPE_PTRMEM_CLASS_TYPE (type),
08e17d9d 6657 allow_inverse_p,
135d47df
PC
6658 c_cast_p, complain);
6659 if (delta == error_mark_node)
6660 return error_mark_node;
6661
08e17d9d 6662 if (!integer_zerop (delta))
6e03b280
OW
6663 {
6664 tree cond, op1, op2;
6665
ba47d38d
AH
6666 cond = cp_build_binary_op (input_location,
6667 EQ_EXPR,
6e03b280 6668 expr,
5ade1ed2 6669 build_int_cst (TREE_TYPE (expr), -1),
a3299120 6670 complain);
6e03b280 6671 op1 = build_nop (ptrdiff_type_node, expr);
ba47d38d
AH
6672 op2 = cp_build_binary_op (input_location,
6673 PLUS_EXPR, op1, delta,
a3299120 6674 complain);
6e03b280 6675
db3927fb
AH
6676 expr = fold_build3_loc (input_location,
6677 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6e03b280
OW
6678
6679 }
6680
08e17d9d
MM
6681 return build_nop (type, expr);
6682 }
6683 else
c8094d83 6684 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
135d47df 6685 allow_inverse_p, c_cast_p, complain);
08e17d9d
MM
6686}
6687
33c25e5c
MM
6688/* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6689 this static_cast is being attempted as one of the possible casts
6690 allowed by a C-style cast. (In that case, accessibility of base
6691 classes is not considered, and it is OK to cast away
6692 constness.) Return the result of the cast. *VALID_P is set to
6693 indicate whether or not the cast was valid. */
3fe18f1d 6694
33c25e5c
MM
6695static tree
6696build_static_cast_1 (tree type, tree expr, bool c_cast_p,
5ade1ed2 6697 bool *valid_p, tsubst_flags_t complain)
a4443a08 6698{
00bb3dad 6699 tree intype;
3fe18f1d 6700 tree result;
645c7a55 6701 cp_lvalue_kind clk;
c11b6f21 6702
33c25e5c
MM
6703 /* Assume the cast is valid. */
6704 *valid_p = true;
c11b6f21 6705
645c7a55 6706 intype = unlowered_expr_type (expr);
c11b6f21 6707
8d8d1a28
AH
6708 /* Save casted types in the function's used types hash table. */
6709 used_types_insert (type);
6710
3fe18f1d 6711 /* [expr.static.cast]
c11b6f21 6712
3fe18f1d
MM
6713 An lvalue of type "cv1 B", where B is a class type, can be cast
6714 to type "reference to cv2 D", where D is a class derived (clause
6715 _class.derived_) from B, if a valid standard conversion from
6716 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6717 same cv-qualification as, or greater cv-qualification than, cv1,
6718 and B is not a virtual base class of D. */
0a72704b
MM
6719 /* We check this case before checking the validity of "TYPE t =
6720 EXPR;" below because for this case:
6721
6722 struct B {};
6723 struct D : public B { D(const B&); };
6724 extern B& b;
6725 void f() { static_cast<const D&>(b); }
6726
6727 we want to avoid constructing a new D. The standard is not
6728 completely clear about this issue, but our interpretation is
6729 consistent with other compilers. */
3fe18f1d
MM
6730 if (TREE_CODE (type) == REFERENCE_TYPE
6731 && CLASS_TYPE_P (TREE_TYPE (type))
6732 && CLASS_TYPE_P (intype)
72b3e203 6733 && (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
3fe18f1d
MM
6734 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6735 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
c8094d83 6736 build_pointer_type (TYPE_MAIN_VARIANT
b40e334f
PC
6737 (TREE_TYPE (type))),
6738 complain)
33c25e5c
MM
6739 && (c_cast_p
6740 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
c11b6f21 6741 {
33c25e5c
MM
6742 tree base;
6743
385bce06 6744 /* There is a standard conversion from "D*" to "B*" even if "B"
33c25e5c
MM
6745 is ambiguous or inaccessible. If this is really a
6746 static_cast, then we check both for inaccessibility and
6747 ambiguity. However, if this is a static_cast being performed
6748 because the user wrote a C-style cast, then accessibility is
6749 not considered. */
c8094d83
MS
6750 base = lookup_base (TREE_TYPE (type), intype,
6751 c_cast_p ? ba_unique : ba_check,
22854930 6752 NULL, complain);
35228ac7
JJ
6753 expr = build_address (expr);
6754
6755 if (flag_sanitize & SANITIZE_VPTR)
6756 {
6757 tree ubsan_check
62775f0d
JJ
6758 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6759 intype, expr);
35228ac7
JJ
6760 if (ubsan_check)
6761 expr = ubsan_check;
6762 }
3fe18f1d 6763
385bce06
MM
6764 /* Convert from "B*" to "D*". This function will check that "B"
6765 is not a virtual base of "D". */
35228ac7
JJ
6766 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6767 complain);
6768
3a73bcc6 6769 /* Convert the pointer to a reference -- but then remember that
9e115cec
JM
6770 there are no expressions with reference type in C++.
6771
6772 We call rvalue so that there's an actual tree code
6773 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6774 is a variable with the same type, the conversion would get folded
6775 away, leaving just the variable and causing lvalue_kind to give
6776 the wrong answer. */
6777 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
c11b6f21 6778 }
3fe18f1d 6779
8e3fb544 6780 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
e7f1930f
JM
6781 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6782 if (TREE_CODE (type) == REFERENCE_TYPE
6783 && TYPE_REF_IS_RVALUE (type)
645c7a55 6784 && (clk = real_lvalue_p (expr))
e7f1930f
JM
6785 && reference_related_p (TREE_TYPE (type), intype)
6786 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6787 {
645c7a55
JM
6788 if (clk == clk_ordinary)
6789 {
6790 /* Handle the (non-bit-field) lvalue case here by casting to
6791 lvalue reference and then changing it to an rvalue reference.
6792 Casting an xvalue to rvalue reference will be handled by the
6793 main code path. */
6794 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6795 result = (perform_direct_initialization_if_possible
6796 (lref, expr, c_cast_p, complain));
16dc6b17 6797 result = build1 (NON_LVALUE_EXPR, type, result);
645c7a55
JM
6798 return convert_from_reference (result);
6799 }
6800 else
6801 /* For a bit-field or packed field, bind to a temporary. */
6802 expr = rvalue (expr);
e7f1930f
JM
6803 }
6804
7bead48f
JM
6805 /* Resolve overloaded address here rather than once in
6806 implicit_conversion and again in the inverse code below. */
6807 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6808 {
6809 expr = instantiate_type (type, expr, complain);
6810 intype = TREE_TYPE (expr);
6811 }
6812
ca73dc29
MG
6813 /* [expr.static.cast]
6814
6815 Any expression can be explicitly converted to type cv void. */
50e10fa8 6816 if (VOID_TYPE_P (type))
ca73dc29
MG
6817 return convert_to_void (expr, ICV_CAST, complain);
6818
2df663cc
JM
6819 /* [class.abstract]
6820 An abstract class shall not be used ... as the type of an explicit
6821 conversion. */
6822 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6823 return error_mark_node;
6824
0a72704b
MM
6825 /* [expr.static.cast]
6826
6827 An expression e can be explicitly converted to a type T using a
6828 static_cast of the form static_cast<T>(e) if the declaration T
6829 t(e);" is well-formed, for some invented temporary variable
6830 t. */
33c25e5c 6831 result = perform_direct_initialization_if_possible (type, expr,
5ade1ed2 6832 c_cast_p, complain);
0a72704b 6833 if (result)
109e0040
MM
6834 {
6835 result = convert_from_reference (result);
33c25e5c 6836
109e0040
MM
6837 /* [expr.static.cast]
6838
0cbd7506 6839 If T is a reference type, the result is an lvalue; otherwise,
109e0040 6840 the result is an rvalue. */
5cc53d4e 6841 if (TREE_CODE (type) != REFERENCE_TYPE)
0465369f
JM
6842 {
6843 result = rvalue (result);
6844
6845 if (result == expr && SCALAR_TYPE_P (type))
6846 /* Leave some record of the cast. */
6847 result = build_nop (type, expr);
6848 }
109e0040
MM
6849 return result;
6850 }
c8094d83 6851
3fe18f1d
MM
6852 /* [expr.static.cast]
6853
6854 The inverse of any standard conversion sequence (clause _conv_),
6855 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6856 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6857 (_conv.bool_) conversions, can be performed explicitly using
6858 static_cast subject to the restriction that the explicit
6859 conversion does not cast away constness (_expr.const.cast_), and
6860 the following additional rules for specific cases: */
6861 /* For reference, the conversions not excluded are: integral
6862 promotions, floating point promotion, integral conversions,
6863 floating point conversions, floating-integral conversions,
6864 pointer conversions, and pointer to member conversions. */
a10ce2f8 6865 /* DR 128
c8094d83 6866
a10ce2f8
GK
6867 A value of integral _or enumeration_ type can be explicitly
6868 converted to an enumeration type. */
6869 /* The effect of all that is that any conversion between any two
6870 types which are integral, floating, or enumeration types can be
6871 performed. */
550a799d
JM
6872 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6873 || SCALAR_FLOAT_TYPE_P (type))
6874 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6875 || SCALAR_FLOAT_TYPE_P (intype)))
4b978f96 6876 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
33c25e5c 6877
3fe18f1d
MM
6878 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6879 && CLASS_TYPE_P (TREE_TYPE (type))
6880 && CLASS_TYPE_P (TREE_TYPE (intype))
c8094d83
MS
6881 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6882 (TREE_TYPE (intype))),
6883 build_pointer_type (TYPE_MAIN_VARIANT
b40e334f
PC
6884 (TREE_TYPE (type))),
6885 complain))
999cc24c 6886 {
3fe18f1d
MM
6887 tree base;
6888
3f89235e
JM
6889 if (!c_cast_p
6890 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6891 complain))
6892 return error_mark_node;
c8094d83
MS
6893 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6894 c_cast_p ? ba_unique : ba_check,
22854930 6895 NULL, complain);
a271590a
PC
6896 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6897 complain);
35228ac7
JJ
6898
6899 if (flag_sanitize & SANITIZE_VPTR)
6900 {
6901 tree ubsan_check
62775f0d
JJ
6902 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6903 intype, expr);
35228ac7
JJ
6904 if (ubsan_check)
6905 expr = ubsan_check;
6906 }
6907
6908 return cp_fold_convert (type, expr);
999cc24c 6909 }
c8094d83 6910
66b1156a 6911 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
3fe18f1d
MM
6912 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6913 {
6914 tree c1;
6915 tree c2;
6916 tree t1;
6917 tree t2;
c11b6f21 6918
3fe18f1d
MM
6919 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6920 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6921
66b1156a 6922 if (TYPE_PTRDATAMEM_P (type))
3fe18f1d 6923 {
c8094d83 6924 t1 = (build_ptrmem_type
3fe18f1d
MM
6925 (c1,
6926 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
c8094d83 6927 t2 = (build_ptrmem_type
3fe18f1d
MM
6928 (c2,
6929 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6930 }
6931 else
6932 {
6933 t1 = intype;
6934 t2 = type;
6935 }
b40e334f 6936 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
3fe18f1d 6937 {
3f89235e
JM
6938 if (!c_cast_p
6939 && check_for_casting_away_constness (intype, type,
6940 STATIC_CAST_EXPR,
6941 complain))
6942 return error_mark_node;
08e17d9d 6943 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
3f89235e 6944 c_cast_p, complain);
3fe18f1d
MM
6945 }
6946 }
c8094d83 6947
af7b9902
MM
6948 /* [expr.static.cast]
6949
3fe18f1d
MM
6950 An rvalue of type "pointer to cv void" can be explicitly
6951 converted to a pointer to object type. A value of type pointer
6952 to object converted to "pointer to cv void" and back to the
6953 original pointer type will have its original value. */
50e10fa8 6954 if (TYPE_PTR_P (intype)
3fe18f1d
MM
6955 && VOID_TYPE_P (TREE_TYPE (intype))
6956 && TYPE_PTROB_P (type))
af7b9902 6957 {
3f89235e
JM
6958 if (!c_cast_p
6959 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6960 complain))
6961 return error_mark_node;
3fe18f1d 6962 return build_nop (type, expr);
af7b9902
MM
6963 }
6964
33c25e5c 6965 *valid_p = false;
c11b6f21 6966 return error_mark_node;
a4443a08
MS
6967}
6968
33c25e5c
MM
6969/* Return an expression representing static_cast<TYPE>(EXPR). */
6970
cdf5b885 6971tree
5ade1ed2 6972build_static_cast (tree type, tree expr, tsubst_flags_t complain)
a4443a08 6973{
33c25e5c
MM
6974 tree result;
6975 bool valid_p;
c11b6f21
MS
6976
6977 if (type == error_mark_node || expr == error_mark_node)
6978 return error_mark_node;
6979
5156628f 6980 if (processing_template_decl)
5566b478 6981 {
33c25e5c
MM
6982 expr = build_min (STATIC_CAST_EXPR, type, expr);
6983 /* We don't know if it will or will not have side effects. */
6984 TREE_SIDE_EFFECTS (expr) = 1;
e8c66fe0 6985 return convert_from_reference (expr);
5566b478
MS
6986 }
6987
33c25e5c
MM
6988 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6989 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6990 if (TREE_CODE (type) != REFERENCE_TYPE
6991 && TREE_CODE (expr) == NOP_EXPR
6992 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6993 expr = TREE_OPERAND (expr, 0);
c11b6f21 6994
5ade1ed2
DG
6995 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6996 complain);
33c25e5c 6997 if (valid_p)
04398fa8
PC
6998 {
6999 if (result != error_mark_node)
7000 maybe_warn_about_useless_cast (type, expr, complain);
7001 return result;
7002 }
33c25e5c 7003
5ade1ed2
DG
7004 if (complain & tf_error)
7005 error ("invalid static_cast from type %qT to type %qT",
7006 TREE_TYPE (expr), type);
33c25e5c
MM
7007 return error_mark_node;
7008}
7009
7010/* EXPR is an expression with member function or pointer-to-member
7011 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7012 not permitted by ISO C++, but we accept it in some modes. If we
7013 are not in one of those modes, issue a diagnostic. Return the
7014 converted expression. */
7015
7016tree
89fcabaf 7017convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
33c25e5c
MM
7018{
7019 tree intype;
7020 tree decl;
c11b6f21 7021
c11b6f21 7022 intype = TREE_TYPE (expr);
33c25e5c
MM
7023 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
7024 || TREE_CODE (intype) == METHOD_TYPE);
a80e4195 7025
89fcabaf
PC
7026 if (!(complain & tf_warning_or_error))
7027 return error_mark_node;
7028
33c25e5c 7029 if (pedantic || warn_pmf2ptr)
c1771a20 7030 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
fcf73884 7031 "converting from %qT to %qT", intype, type);
c8094d83 7032
33c25e5c 7033 if (TREE_CODE (intype) == METHOD_TYPE)
89fcabaf 7034 expr = build_addr_func (expr, complain);
33c25e5c
MM
7035 else if (TREE_CODE (expr) == PTRMEM_CST)
7036 expr = build_address (PTRMEM_CST_MEMBER (expr));
7037 else
7038 {
7039 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
7040 decl = build_address (decl);
89fcabaf 7041 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
33c25e5c
MM
7042 }
7043
89fcabaf
PC
7044 if (expr == error_mark_node)
7045 return error_mark_node;
7046
33c25e5c
MM
7047 return build_nop (type, expr);
7048}
7049
7050/* Return a representation for a reinterpret_cast from EXPR to TYPE.
7051 If C_CAST_P is true, this reinterpret cast is being done as part of
7052 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7053 indicate whether or not reinterpret_cast was valid. */
7054
7055static tree
7056build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
5ade1ed2 7057 bool *valid_p, tsubst_flags_t complain)
33c25e5c
MM
7058{
7059 tree intype;
7060
7061 /* Assume the cast is invalid. */
7062 if (valid_p)
7063 *valid_p = true;
7064
7065 if (type == error_mark_node || error_operand_p (expr))
a8c2c492
AP
7066 return error_mark_node;
7067
33c25e5c
MM
7068 intype = TREE_TYPE (expr);
7069
8d8d1a28
AH
7070 /* Save casted types in the function's used types hash table. */
7071 used_types_insert (type);
7072
33c25e5c
MM
7073 /* [expr.reinterpret.cast]
7074 An lvalue expression of type T1 can be cast to the type
7075 "reference to T2" if an expression of type "pointer to T1" can be
7076 explicitly converted to the type "pointer to T2" using a
7077 reinterpret_cast. */
c11b6f21
MS
7078 if (TREE_CODE (type) == REFERENCE_TYPE)
7079 {
72b3e203 7080 if (! lvalue_p (expr))
c11b6f21 7081 {
5ade1ed2
DG
7082 if (complain & tf_error)
7083 error ("invalid cast of an rvalue expression of type "
7084 "%qT to type %qT",
7085 intype, type);
c11b6f21
MS
7086 return error_mark_node;
7087 }
33c25e5c
MM
7088
7089 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7090 "B" are related class types; the reinterpret_cast does not
7091 adjust the pointer. */
7092 if (TYPE_PTR_P (intype)
5ade1ed2 7093 && (complain & tf_warning)
96d84882
PB
7094 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
7095 COMPARE_BASE | COMPARE_DERIVED)))
d4ee4d25 7096 warning (0, "casting %qT to %qT does not dereference pointer",
33c25e5c
MM
7097 intype, type);
7098
93c0e0bb 7099 expr = cp_build_addr_expr (expr, complain);
45fe7947
JM
7100
7101 if (warn_strict_aliasing > 2)
7102 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
7103
c11b6f21 7104 if (expr != error_mark_node)
33c25e5c 7105 expr = build_reinterpret_cast_1
ec7e5618 7106 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
5ade1ed2 7107 valid_p, complain);
c11b6f21 7108 if (expr != error_mark_node)
45fe7947
JM
7109 /* cp_build_indirect_ref isn't right for rvalue refs. */
7110 expr = convert_from_reference (fold_convert (type, expr));
c11b6f21
MS
7111 return expr;
7112 }
8ccc31eb 7113
33c25e5c
MM
7114 /* As a G++ extension, we consider conversions from member
7115 functions, and pointers to member functions to
7116 pointer-to-function and pointer-to-void types. If
7117 -Wno-pmf-conversions has not been specified,
7118 convert_member_func_to_ptr will issue an error message. */
c8094d83 7119 if ((TYPE_PTRMEMFUNC_P (intype)
33c25e5c
MM
7120 || TREE_CODE (intype) == METHOD_TYPE)
7121 && TYPE_PTR_P (type)
7122 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7123 || VOID_TYPE_P (TREE_TYPE (type))))
89fcabaf 7124 return convert_member_func_to_ptr (type, expr, complain);
33c25e5c 7125
5acd0bed 7126 /* If the cast is not to a reference type, the lvalue-to-rvalue,
33c25e5c
MM
7127 array-to-pointer, and function-to-pointer conversions are
7128 performed. */
89fcabaf 7129 expr = decay_conversion (expr, complain);
c8094d83 7130
33c25e5c
MM
7131 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7132 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7133 if (TREE_CODE (expr) == NOP_EXPR
7134 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7135 expr = TREE_OPERAND (expr, 0);
7136
7137 if (error_operand_p (expr))
7138 return error_mark_node;
7139
7140 intype = TREE_TYPE (expr);
7141
7142 /* [expr.reinterpret.cast]
7143 A pointer can be converted to any integral type large enough to
14c2101d
JM
7144 hold it. ... A value of type std::nullptr_t can be converted to
7145 an integral type; the conversion has the same meaning and
7146 validity as a conversion of (void*)0 to the integral type. */
7147 if (CP_INTEGRAL_TYPE_P (type)
5116acc6 7148 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
8ccc31eb 7149 {
c11b6f21 7150 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5ade1ed2
DG
7151 {
7152 if (complain & tf_error)
cbe5f3b3 7153 permerror (input_location, "cast from %qT to %qT loses precision",
37ec60ed 7154 intype, type);
5ade1ed2
DG
7155 else
7156 return error_mark_node;
7157 }
5116acc6 7158 if (NULLPTR_TYPE_P (intype))
14c2101d 7159 return build_int_cst (type, 0);
8ccc31eb 7160 }
33c25e5c
MM
7161 /* [expr.reinterpret.cast]
7162 A value of integral or enumeration type can be explicitly
7163 converted to a pointer. */
7164 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7165 /* OK */
7166 ;
9f88ff5a 7167 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
66b1156a 7168 || TYPE_PTR_OR_PTRMEM_P (type))
9f88ff5a
PC
7169 && same_type_p (type, intype))
7170 /* DR 799 */
6d1301f2 7171 return rvalue (expr);
c11b6f21
MS
7172 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7173 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
cda0a029 7174 return build_nop (type, expr);
66b1156a 7175 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
c11b6f21 7176 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
8ccc31eb 7177 {
de9c56a4
RG
7178 tree sexpr = expr;
7179
3f89235e
JM
7180 if (!c_cast_p
7181 && check_for_casting_away_constness (intype, type,
7182 REINTERPRET_CAST_EXPR,
7183 complain))
7184 return error_mark_node;
33c25e5c
MM
7185 /* Warn about possible alignment problems. */
7186 if (STRICT_ALIGNMENT && warn_cast_align
5ade1ed2 7187 && (complain & tf_warning)
33c25e5c
MM
7188 && !VOID_TYPE_P (type)
7189 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7190 && COMPLETE_TYPE_P (TREE_TYPE (type))
7191 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7192 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
683d6ff9
MLI
7193 warning (OPT_Wcast_align, "cast from %qT to %qT "
7194 "increases required alignment of target type", intype, type);
c8094d83 7195
3b426391 7196 /* We need to strip nops here, because the front end likes to
de9c56a4
RG
7197 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7198 STRIP_NOPS (sexpr);
79bedddc
SR
7199 if (warn_strict_aliasing <= 2)
7200 strict_aliasing_warning (intype, type, sexpr);
de9c56a4 7201
cda0a029 7202 return build_nop (type, expr);
8ccc31eb 7203 }
ffb690bd 7204 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
a06d48ef 7205 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
ffb690bd 7206 {
dfeadaa0
PC
7207 if (complain & tf_warning)
7208 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7209 object pointer type or vice versa is conditionally-supported." */
7210 warning (OPT_Wconditionally_supported,
7211 "casting between pointer-to-function and pointer-to-object "
7212 "is conditionally-supported");
cda0a029 7213 return build_nop (type, expr);
ffb690bd 7214 }
b55b02ea 7215 else if (VECTOR_TYPE_P (type))
cda0a029 7216 return convert_to_vector (type, expr);
b55b02ea 7217 else if (VECTOR_TYPE_P (intype)
550a799d 7218 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
cda0a029 7219 return convert_to_integer_nofold (type, expr);
c11b6f21 7220 else
8ccc31eb 7221 {
33c25e5c
MM
7222 if (valid_p)
7223 *valid_p = false;
5ade1ed2
DG
7224 if (complain & tf_error)
7225 error ("invalid cast from type %qT to type %qT", intype, type);
8ccc31eb
MS
7226 return error_mark_node;
7227 }
c8094d83 7228
4b978f96 7229 return cp_convert (type, expr, complain);
a4443a08
MS
7230}
7231
cdf5b885 7232tree
5ade1ed2 7233build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
a4443a08 7234{
04398fa8
PC
7235 tree r;
7236
f30432d7
MS
7237 if (type == error_mark_node || expr == error_mark_node)
7238 return error_mark_node;
7239
5156628f 7240 if (processing_template_decl)
e92cc029 7241 {
33c25e5c 7242 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
c8094d83 7243
8e1daa34
NS
7244 if (!TREE_SIDE_EFFECTS (t)
7245 && type_dependent_expression_p (expr))
7246 /* There might turn out to be side effects inside expr. */
7247 TREE_SIDE_EFFECTS (t) = 1;
e8c66fe0 7248 return convert_from_reference (t);
e92cc029
MS
7249 }
7250
04398fa8
PC
7251 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7252 /*valid_p=*/NULL, complain);
7253 if (r != error_mark_node)
7254 maybe_warn_about_useless_cast (type, expr, complain);
7255 return r;
33c25e5c
MM
7256}
7257
7258/* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7259 return an appropriate expression. Otherwise, return
7260 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7735d402
MM
7261 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7262 performing a C-style cast, its value upon return will indicate
7263 whether or not the conversion succeeded. */
33c25e5c
MM
7264
7265static tree
3f89235e 7266build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
33c25e5c
MM
7267 bool *valid_p)
7268{
7269 tree src_type;
7270 tree reference_type;
7271
7272 /* Callers are responsible for handling error_mark_node as a
7273 destination type. */
7274 gcc_assert (dst_type != error_mark_node);
7275 /* In a template, callers should be building syntactic
7276 representations of casts, not using this machinery. */
7277 gcc_assert (!processing_template_decl);
7278
7279 /* Assume the conversion is invalid. */
7280 if (valid_p)
7281 *valid_p = false;
7282
66b1156a 7283 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
33c25e5c 7284 {
3f89235e 7285 if (complain & tf_error)
33c25e5c
MM
7286 error ("invalid use of const_cast with type %qT, "
7287 "which is not a pointer, "
7288 "reference, nor a pointer-to-data-member type", dst_type);
3fd91cbd
MM
7289 return error_mark_node;
7290 }
7291
33c25e5c 7292 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
8ccc31eb 7293 {
3f89235e 7294 if (complain & tf_error)
33c25e5c
MM
7295 error ("invalid use of const_cast with type %qT, which is a pointer "
7296 "or reference to a function type", dst_type);
7297 return error_mark_node;
8ccc31eb
MS
7298 }
7299
8d8d1a28
AH
7300 /* Save casted types in the function's used types hash table. */
7301 used_types_insert (dst_type);
7302
33c25e5c
MM
7303 src_type = TREE_TYPE (expr);
7304 /* Expressions do not really have reference types. */
7305 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7306 src_type = TREE_TYPE (src_type);
7307
7308 /* [expr.const.cast]
7309
66d83eee
JM
7310 For two object types T1 and T2, if a pointer to T1 can be explicitly
7311 converted to the type "pointer to T2" using a const_cast, then the
7312 following conversions can also be made:
7313
7314 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7315 type T2 using the cast const_cast<T2&>;
7316
7317 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7318 type T2 using the cast const_cast<T2&&>; and
7319
7320 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7321 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7322
33c25e5c 7323 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
8ccc31eb 7324 {
33c25e5c 7325 reference_type = dst_type;
66d83eee 7326 if (!TYPE_REF_IS_RVALUE (dst_type)
72b3e203 7327 ? lvalue_p (expr)
bb19d4af 7328 : obvalue_p (expr))
66d83eee
JM
7329 /* OK. */;
7330 else
8ccc31eb 7331 {
3f89235e 7332 if (complain & tf_error)
33c25e5c
MM
7333 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7334 src_type, dst_type);
8ccc31eb
MS
7335 return error_mark_node;
7336 }
33c25e5c
MM
7337 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7338 src_type = build_pointer_type (src_type);
7339 }
7340 else
7341 {
7342 reference_type = NULL_TREE;
7343 /* If the destination type is not a reference type, the
7344 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7345 conversions are performed. */
7346 src_type = type_decays_to (src_type);
7347 if (src_type == error_mark_node)
7348 return error_mark_node;
7349 }
8ccc31eb 7350
66b1156a 7351 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
33c25e5c 7352 {
2d4e2a68 7353 if (comp_ptr_ttypes_const (dst_type, src_type))
c2840dfb 7354 {
2d4e2a68
PC
7355 if (valid_p)
7356 {
7357 *valid_p = true;
7358 /* This cast is actually a C-style cast. Issue a warning if
7359 the user is making a potentially unsafe cast. */
7360 check_for_casting_away_constness (src_type, dst_type,
7361 CAST_EXPR, complain);
7362 }
7363 if (reference_type)
7364 {
7365 expr = cp_build_addr_expr (expr, complain);
89fcabaf
PC
7366 if (expr == error_mark_node)
7367 return error_mark_node;
2d4e2a68
PC
7368 expr = build_nop (reference_type, expr);
7369 return convert_from_reference (expr);
7370 }
7371 else
7372 {
89fcabaf
PC
7373 expr = decay_conversion (expr, complain);
7374 if (expr == error_mark_node)
7375 return error_mark_node;
7376
2d4e2a68
PC
7377 /* build_c_cast puts on a NOP_EXPR to make the result not an
7378 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7379 non-lvalue context. */
7380 if (TREE_CODE (expr) == NOP_EXPR
7381 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7382 expr = TREE_OPERAND (expr, 0);
7383 return build_nop (dst_type, expr);
7384 }
33c25e5c 7385 }
2d4e2a68
PC
7386 else if (valid_p
7387 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7388 TREE_TYPE (src_type)))
7389 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7390 complain);
8ccc31eb
MS
7391 }
7392
3f89235e 7393 if (complain & tf_error)
c8094d83 7394 error ("invalid const_cast from type %qT to type %qT",
33c25e5c 7395 src_type, dst_type);
c11b6f21 7396 return error_mark_node;
a4443a08
MS
7397}
7398
33c25e5c 7399tree
5ade1ed2 7400build_const_cast (tree type, tree expr, tsubst_flags_t complain)
33c25e5c 7401{
04398fa8
PC
7402 tree r;
7403
71bd7186 7404 if (type == error_mark_node || error_operand_p (expr))
33c25e5c 7405 return error_mark_node;
6060a796 7406
33c25e5c
MM
7407 if (processing_template_decl)
7408 {
7409 tree t = build_min (CONST_CAST_EXPR, type, expr);
c8094d83 7410
33c25e5c
MM
7411 if (!TREE_SIDE_EFFECTS (t)
7412 && type_dependent_expression_p (expr))
7413 /* There might turn out to be side effects inside expr. */
7414 TREE_SIDE_EFFECTS (t) = 1;
e8c66fe0 7415 return convert_from_reference (t);
33c25e5c
MM
7416 }
7417
04398fa8
PC
7418 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7419 if (r != error_mark_node)
7420 maybe_warn_about_useless_cast (type, expr, complain);
7421 return r;
33c25e5c
MM
7422}
7423
525521b6
DG
7424/* Like cp_build_c_cast, but for the c-common bits. */
7425
7426tree
12308bc6 7427build_c_cast (location_t /*loc*/, tree type, tree expr)
525521b6
DG
7428{
7429 return cp_build_c_cast (type, expr, tf_warning_or_error);
7430}
7431
e87eed2a
DM
7432/* Like the "build_c_cast" used for c-common, but using cp_expr to
7433 preserve location information even for tree nodes that don't
7434 support it. */
7435
7436cp_expr
7437build_c_cast (location_t loc, tree type, cp_expr expr)
7438{
7439 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7440 result.set_location (loc);
7441 return result;
7442}
7443
33c25e5c
MM
7444/* Build an expression representing an explicit C-style cast to type
7445 TYPE of expression EXPR. */
8d08fdba
MS
7446
7447tree
525521b6 7448cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
8d08fdba 7449{
926ce8bd 7450 tree value = expr;
33c25e5c
MM
7451 tree result;
7452 bool valid_p;
8d08fdba 7453
33c25e5c 7454 if (type == error_mark_node || error_operand_p (expr))
8d08fdba
MS
7455 return error_mark_node;
7456
0949f723
NS
7457 if (processing_template_decl)
7458 {
7459 tree t = build_min (CAST_EXPR, type,
7460 tree_cons (NULL_TREE, value, NULL_TREE));
04c06002 7461 /* We don't know if it will or will not have side effects. */
8e1daa34 7462 TREE_SIDE_EFFECTS (t) = 1;
e8c66fe0 7463 return convert_from_reference (t);
0949f723
NS
7464 }
7465
2e2da467
ZL
7466 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7467 'Class') should always be retained, because this information aids
7468 in method lookup. */
7469 if (objc_is_object_ptr (type)
7470 && objc_is_object_ptr (TREE_TYPE (expr)))
7471 return build_nop (type, expr);
7472
8d08fdba 7473 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8ccc31eb
MS
7474 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7475 if (TREE_CODE (type) != REFERENCE_TYPE
7476 && TREE_CODE (value) == NOP_EXPR
8d08fdba
MS
7477 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7478 value = TREE_OPERAND (value, 0);
7479
8d08fdba
MS
7480 if (TREE_CODE (type) == ARRAY_TYPE)
7481 {
7482 /* Allow casting from T1* to T2[] because Cfront allows it.
cab1f180 7483 NIHCL uses it. It is not valid ISO C++ however. */
50e10fa8 7484 if (TYPE_PTR_P (TREE_TYPE (expr)))
8d08fdba 7485 {
5ade1ed2 7486 if (complain & tf_error)
cbe5f3b3 7487 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
5ade1ed2
DG
7488 else
7489 return error_mark_node;
8d08fdba
MS
7490 type = build_pointer_type (TREE_TYPE (type));
7491 }
7492 else
7493 {
5ade1ed2
DG
7494 if (complain & tf_error)
7495 error ("ISO C++ forbids casting to an array type %qT", type);
8d08fdba
MS
7496 return error_mark_node;
7497 }
7498 }
7499
a0a33927
MS
7500 if (TREE_CODE (type) == FUNCTION_TYPE
7501 || TREE_CODE (type) == METHOD_TYPE)
7502 {
5ade1ed2
DG
7503 if (complain & tf_error)
7504 error ("invalid cast to function type %qT", type);
a0a33927
MS
7505 return error_mark_node;
7506 }
7507
50e10fa8 7508 if (TYPE_PTR_P (type)
dc8d2739
MLI
7509 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7510 /* Casting to an integer of smaller size is an error detected elsewhere. */
7511 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7512 /* Don't warn about converting any constant. */
7513 && !TREE_CONSTANT (value))
7514 warning_at (input_location, OPT_Wint_to_pointer_cast,
7515 "cast to pointer from integer of different size");
7516
33c25e5c 7517 /* A C-style cast can be a const_cast. */
3f89235e 7518 result = build_const_cast_1 (type, value, complain & tf_warning,
33c25e5c
MM
7519 &valid_p);
7520 if (valid_p)
04398fa8
PC
7521 {
7522 if (result != error_mark_node)
7523 maybe_warn_about_useless_cast (type, value, complain);
7524 return result;
7525 }
8d08fdba 7526
33c25e5c
MM
7527 /* Or a static cast. */
7528 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
5ade1ed2 7529 &valid_p, complain);
33c25e5c
MM
7530 /* Or a reinterpret_cast. */
7531 if (!valid_p)
7532 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
5ade1ed2 7533 &valid_p, complain);
33c25e5c
MM
7534 /* The static_cast or reinterpret_cast may be followed by a
7535 const_cast. */
c8094d83 7536 if (valid_p
33c25e5c 7537 /* A valid cast may result in errors if, for example, a
04398fa8 7538 conversion to an ambiguous base class is required. */
33c25e5c 7539 && !error_operand_p (result))
e6e174e5 7540 {
33c25e5c 7541 tree result_type;
8ccc31eb 7542
04398fa8
PC
7543 maybe_warn_about_useless_cast (type, value, complain);
7544
33c25e5c
MM
7545 /* Non-class rvalues always have cv-unqualified type. */
7546 if (!CLASS_TYPE_P (type))
7547 type = TYPE_MAIN_VARIANT (type);
7548 result_type = TREE_TYPE (result);
8e3fb544 7549 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
33c25e5c
MM
7550 result_type = TYPE_MAIN_VARIANT (result_type);
7551 /* If the type of RESULT does not match TYPE, perform a
7552 const_cast to make it match. If the static_cast or
7553 reinterpret_cast succeeded, we will differ by at most
7554 cv-qualification, so the follow-on const_cast is guaranteed
7555 to succeed. */
7556 if (!same_type_p (non_reference (type), non_reference (result_type)))
e6e174e5 7557 {
33c25e5c
MM
7558 result = build_const_cast_1 (type, result, false, &valid_p);
7559 gcc_assert (valid_p);
8d08fdba 7560 }
33c25e5c 7561 return result;
8d08fdba
MS
7562 }
7563
33c25e5c 7564 return error_mark_node;
8d08fdba
MS
7565}
7566\f
5ade1ed2
DG
7567/* For use from the C common bits. */
7568tree
4f2e1536 7569build_modify_expr (location_t location,
12308bc6 7570 tree lhs, tree /*lhs_origtype*/,
c2255bc4 7571 enum tree_code modifycode,
12308bc6
PC
7572 location_t /*rhs_location*/, tree rhs,
7573 tree /*rhs_origtype*/)
5ade1ed2 7574{
4f2e1536
MP
7575 return cp_build_modify_expr (location, lhs, modifycode, rhs,
7576 tf_warning_or_error);
5ade1ed2
DG
7577}
7578
8d08fdba
MS
7579/* Build an assignment expression of lvalue LHS from value RHS.
7580 MODIFYCODE is the code for a binary operator that we use
7581 to combine the old value of LHS with RHS to get the new value.
7582 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7583
e92cc029
MS
7584 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7585
8d08fdba 7586tree
4f2e1536
MP
7587cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7588 tree rhs, tsubst_flags_t complain)
8d08fdba 7589{
b84702c0 7590 tree result = NULL_TREE;
8d08fdba
MS
7591 tree newrhs = rhs;
7592 tree lhstype = TREE_TYPE (lhs);
b84702c0 7593 tree olhs = lhs;
8d08fdba 7594 tree olhstype = lhstype;
487a92fe 7595 bool plain_assign = (modifycode == NOP_EXPR);
b84702c0
JJ
7596 bool compound_side_effects_p = false;
7597 tree preeval = NULL_TREE;
8d08fdba 7598
8d08fdba 7599 /* Avoid duplicate error messages from operands that had errors. */
1bd229b7 7600 if (error_operand_p (lhs) || error_operand_p (rhs))
8d08fdba
MS
7601 return error_mark_node;
7602
b84702c0
JJ
7603 while (TREE_CODE (lhs) == COMPOUND_EXPR)
7604 {
7605 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7606 compound_side_effects_p = true;
7607 lhs = TREE_OPERAND (lhs, 1);
7608 }
7609
a25bd9e6
JM
7610 /* Handle control structure constructs used as "lvalues". Note that we
7611 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8d08fdba
MS
7612 switch (TREE_CODE (lhs))
7613 {
f4f206f4 7614 /* Handle --foo = 5; as these are valid constructs in C++. */
8d08fdba
MS
7615 case PREDECREMENT_EXPR:
7616 case PREINCREMENT_EXPR:
b84702c0
JJ
7617 if (compound_side_effects_p)
7618 newrhs = rhs = stabilize_expr (rhs, &preeval);
8d08fdba 7619 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
f293ce4b 7620 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5a0802ea 7621 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
f293ce4b 7622 TREE_OPERAND (lhs, 1));
a25bd9e6 7623 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
b84702c0
JJ
7624 maybe_add_compound:
7625 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7626 and looked through the COMPOUND_EXPRs, readd them now around
7627 the resulting lhs. */
7628 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7629 {
7630 lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
7631 tree *ptr = &TREE_OPERAND (lhs, 1);
7632 for (olhs = TREE_OPERAND (olhs, 1);
7633 TREE_CODE (olhs) == COMPOUND_EXPR;
7634 olhs = TREE_OPERAND (olhs, 1))
7635 {
7636 *ptr = build2 (COMPOUND_EXPR, lhstype,
7637 TREE_OPERAND (olhs, 0), *ptr);
7638 ptr = &TREE_OPERAND (*ptr, 1);
7639 }
7640 }
a25bd9e6 7641 break;
8d08fdba 7642
a4443a08 7643 case MODIFY_EXPR:
b84702c0
JJ
7644 if (compound_side_effects_p)
7645 newrhs = rhs = stabilize_expr (rhs, &preeval);
a65cddcf 7646 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
f293ce4b 7647 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
5a0802ea 7648 cp_stabilize_reference (TREE_OPERAND (lhs, 0)),
f293ce4b 7649 TREE_OPERAND (lhs, 1));
a25bd9e6 7650 lhs = build2 (COMPOUND_EXPR, lhstype, lhs, TREE_OPERAND (lhs, 0));
b84702c0 7651 goto maybe_add_compound;
a4443a08 7652
d211a298
RS
7653 case MIN_EXPR:
7654 case MAX_EXPR:
7655 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7656 when neither operand has side-effects. */
5ade1ed2 7657 if (!lvalue_or_else (lhs, lv_assign, complain))
d211a298
RS
7658 return error_mark_node;
7659
7660 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7661 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7662
7663 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7664 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7665 boolean_type_node,
7666 TREE_OPERAND (lhs, 0),
7667 TREE_OPERAND (lhs, 1)),
7668 TREE_OPERAND (lhs, 0),
7669 TREE_OPERAND (lhs, 1));
81fea426 7670 gcc_fallthrough ();
d211a298 7671
8d08fdba
MS
7672 /* Handle (a ? b : c) used as an "lvalue". */
7673 case COND_EXPR:
8d08fdba
MS
7674 {
7675 /* Produce (a ? (b = rhs) : (c = rhs))
7676 except that the RHS goes through a save-expr
7677 so the code to compute it is only emitted once. */
df39af7d
JM
7678 tree cond;
7679
df3473fa
JJ
7680 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7681 {
5ade1ed2
DG
7682 if (complain & tf_error)
7683 error ("void value not ignored as it ought to be");
df3473fa
JJ
7684 return error_mark_node;
7685 }
7686
a77a9a18 7687 rhs = stabilize_expr (rhs, &preeval);
c8094d83 7688
df39af7d
JM
7689 /* Check this here to avoid odd errors when trying to convert
7690 a throw to the type of the COND_EXPR. */
5ade1ed2 7691 if (!lvalue_or_else (lhs, lv_assign, complain))
df39af7d
JM
7692 return error_mark_node;
7693
7694 cond = build_conditional_expr
4cbc4bd7 7695 (input_location, TREE_OPERAND (lhs, 0),
4f2e1536 7696 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 1),
5ade1ed2 7697 modifycode, rhs, complain),
4f2e1536 7698 cp_build_modify_expr (loc, TREE_OPERAND (lhs, 2),
5ade1ed2
DG
7699 modifycode, rhs, complain),
7700 complain);
df39af7d 7701
bd6dd845 7702 if (cond == error_mark_node)
8d08fdba 7703 return cond;
b84702c0
JJ
7704 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7705 and looked through the COMPOUND_EXPRs, readd them now around
7706 the resulting cond before adding the preevaluated rhs. */
7707 if (TREE_CODE (olhs) == COMPOUND_EXPR)
7708 {
7709 cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7710 TREE_OPERAND (olhs, 0), cond);
7711 tree *ptr = &TREE_OPERAND (cond, 1);
7712 for (olhs = TREE_OPERAND (olhs, 1);
7713 TREE_CODE (olhs) == COMPOUND_EXPR;
7714 olhs = TREE_OPERAND (olhs, 1))
7715 {
7716 *ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
7717 TREE_OPERAND (olhs, 0), *ptr);
7718 ptr = &TREE_OPERAND (*ptr, 1);
7719 }
7720 }
8d08fdba
MS
7721 /* Make sure the code to compute the rhs comes out
7722 before the split. */
b84702c0
JJ
7723 result = cond;
7724 goto ret;
8d08fdba 7725 }
c8094d83 7726
7f85441b 7727 default:
b84702c0 7728 lhs = olhs;
7f85441b 7729 break;
8d08fdba
MS
7730 }
7731
8d08fdba
MS
7732 if (modifycode == INIT_EXPR)
7733 {
4ea08463
JM
7734 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7735 /* Do the default thing. */;
7736 else if (TREE_CODE (rhs) == CONSTRUCTOR)
f30efcb7 7737 {
4ea08463 7738 /* Compound literal. */
182609b5
JM
7739 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7740 /* Call convert to generate an error; see PR 11063. */
7741 rhs = convert (lhstype, rhs);
f293ce4b 7742 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
f30efcb7 7743 TREE_SIDE_EFFECTS (result) = 1;
b84702c0 7744 goto ret;
f30efcb7 7745 }
9e1e64ec 7746 else if (! MAYBE_CLASS_TYPE_P (lhstype))
f4f206f4 7747 /* Do the default thing. */;
db5ae43f 7748 else
8d08fdba 7749 {
9771b263 7750 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
4ba126e4 7751 result = build_special_member_call (lhs, complete_ctor_identifier,
c166b898 7752 &rhs_vec, lhstype, LOOKUP_NORMAL,
5ade1ed2 7753 complain);
c166b898 7754 release_tree_vector (rhs_vec);
8d08fdba
MS
7755 if (result == NULL_TREE)
7756 return error_mark_node;
b84702c0 7757 goto ret;
8d08fdba
MS
7758 }
7759 }
a56ca899 7760 else
8d08fdba 7761 {
79fe346e 7762 lhs = require_complete_type_sfinae (lhs, complain);
a56ca899
NS
7763 if (lhs == error_mark_node)
7764 return error_mark_node;
7765
7766 if (modifycode == NOP_EXPR)
c91a56d2 7767 {
668ea4b1
IS
7768 if (c_dialect_objc ())
7769 {
46a88c12 7770 result = objc_maybe_build_modify_expr (lhs, rhs);
668ea4b1 7771 if (result)
b84702c0 7772 goto ret;
668ea4b1
IS
7773 }
7774
a56ca899 7775 /* `operator=' is not an inheritable operator. */
9e1e64ec 7776 if (! MAYBE_CLASS_TYPE_P (lhstype))
f4f206f4 7777 /* Do the default thing. */;
a56ca899
NS
7778 else
7779 {
4fe977f2
PC
7780 result = build_new_op (input_location, MODIFY_EXPR,
7781 LOOKUP_NORMAL, lhs, rhs,
7782 make_node (NOP_EXPR), /*overload=*/NULL,
5ade1ed2 7783 complain);
a56ca899
NS
7784 if (result == NULL_TREE)
7785 return error_mark_node;
b84702c0 7786 goto ret;
a56ca899
NS
7787 }
7788 lhstype = olhstype;
7789 }
7790 else
7791 {
4063e61b
JM
7792 tree init = NULL_TREE;
7793
a56ca899 7794 /* A binary op has been requested. Combine the old LHS
0cbd7506
MS
7795 value with the RHS producing the value we should actually
7796 store into the LHS. */
9e1e64ec
PC
7797 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7798 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7799 || MAYBE_CLASS_TYPE_P (lhstype)));
a56ca899 7800
4063e61b
JM
7801 /* Preevaluate the RHS to make sure its evaluation is complete
7802 before the lvalue-to-rvalue conversion of the LHS:
7803
7804 [expr.ass] With respect to an indeterminately-sequenced
7805 function call, the operation of a compound assignment is a
7806 single evaluation. [ Note: Therefore, a function call shall
7807 not intervene between the lvalue-to-rvalue conversion and the
7808 side effect associated with any single compound assignment
7809 operator. -- end note ] */
5a0802ea 7810 lhs = cp_stabilize_reference (lhs);
30f6b784 7811 rhs = rvalue (rhs);
4063e61b 7812 rhs = stabilize_expr (rhs, &init);
4f2e1536 7813 newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
a56ca899
NS
7814 if (newrhs == error_mark_node)
7815 {
5ade1ed2
DG
7816 if (complain & tf_error)
7817 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7818 TREE_TYPE (lhs), TREE_TYPE (rhs));
a56ca899
NS
7819 return error_mark_node;
7820 }
c8094d83 7821
4063e61b
JM
7822 if (init)
7823 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7824
a56ca899
NS
7825 /* Now it looks like a plain assignment. */
7826 modifycode = NOP_EXPR;
668ea4b1
IS
7827 if (c_dialect_objc ())
7828 {
46a88c12 7829 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 7830 if (result)
b84702c0 7831 goto ret;
668ea4b1 7832 }
c91a56d2 7833 }
50bc768d
NS
7834 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7835 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
8d08fdba
MS
7836 }
7837
719f407a 7838 /* The left-hand side must be an lvalue. */
5ade1ed2 7839 if (!lvalue_or_else (lhs, lv_assign, complain))
8d08fdba
MS
7840 return error_mark_node;
7841
a56ca899
NS
7842 /* Warn about modifying something that is `const'. Don't warn if
7843 this is initialization. */
8d08fdba 7844 if (modifycode != INIT_EXPR
91063b51 7845 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
69851283
MM
7846 /* Functions are not modifiable, even though they are
7847 lvalues. */
7848 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
a56ca899
NS
7849 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7850 /* If it's an aggregate and any field is const, then it is
7851 effectively const. */
5552b43c 7852 || (CLASS_TYPE_P (lhstype)
a56ca899 7853 && C_TYPE_FIELDS_READONLY (lhstype))))
5ade1ed2
DG
7854 {
7855 if (complain & tf_error)
4816c593 7856 cxx_readonly_error (lhs, lv_assign);
5ade1ed2
DG
7857 else
7858 return error_mark_node;
7859 }
8d08fdba 7860
41b81065
JM
7861 /* If storing into a structure or union member, it may have been given a
7862 lowered bitfield type. We need to convert to the declared type first,
7863 so retrieve it now. */
8d08fdba 7864
41b81065 7865 olhstype = unlowered_expr_type (lhs);
8d08fdba 7866
8d08fdba
MS
7867 /* Convert new value to destination type. */
7868
7869 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7870 {
f376e137 7871 int from_array;
c8094d83 7872
5666bb32 7873 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
84526801 7874 {
eaa9d009
JM
7875 if (modifycode != INIT_EXPR)
7876 {
7877 if (complain & tf_error)
7878 error ("assigning to an array from an initializer list");
7879 return error_mark_node;
7880 }
5666bb32 7881 if (check_array_initializer (lhs, lhstype, newrhs))
84526801 7882 return error_mark_node;
754af126 7883 newrhs = digest_init (lhstype, newrhs, complain);
625f85e9
PC
7884 if (newrhs == error_mark_node)
7885 return error_mark_node;
84526801 7886 }
25357d1e 7887
82b3da6a
PC
7888 /* C++11 8.5/17: "If the destination type is an array of characters,
7889 an array of char16_t, an array of char32_t, or an array of wchar_t,
7890 and the initializer is a string literal...". */
7891 else if (TREE_CODE (newrhs) == STRING_CST
7892 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7893 && modifycode == INIT_EXPR)
7894 {
7895 newrhs = digest_init (lhstype, newrhs, complain);
7896 if (newrhs == error_mark_node)
7897 return error_mark_node;
7898 }
7899
25357d1e 7900 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
5666bb32 7901 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
44a8d0b3 7902 {
5ade1ed2
DG
7903 if (complain & tf_error)
7904 error ("incompatible types in assignment of %qT to %qT",
7905 TREE_TYPE (rhs), lhstype);
44a8d0b3
MS
7906 return error_mark_node;
7907 }
7908
39211cd5 7909 /* Allow array assignment in compiler-generated code. */
25357d1e 7910 else if (!current_function_decl
5e242863 7911 || !DECL_DEFAULTED_FN (current_function_decl))
cf24598a 7912 {
decebe51
GDR
7913 /* This routine is used for both initialization and assignment.
7914 Make sure the diagnostic message differentiates the context. */
5ade1ed2
DG
7915 if (complain & tf_error)
7916 {
7917 if (modifycode == INIT_EXPR)
7918 error ("array used as initializer");
7919 else
7920 error ("invalid array assignment");
7921 }
cf24598a
MM
7922 return error_mark_node;
7923 }
39211cd5 7924
f376e137 7925 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
0cbd7506 7926 ? 1 + (modifycode != INIT_EXPR): 0;
b84702c0
JJ
7927 result = build_vec_init (lhs, NULL_TREE, newrhs,
7928 /*explicit_value_init_p=*/false,
7929 from_array, complain);
7930 goto ret;
8d08fdba
MS
7931 }
7932
7933 if (modifycode == INIT_EXPR)
e57d93c6
JM
7934 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7935 LOOKUP_ONLYCONVERTING. */
41b81065 7936 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
2f5b91f5 7937 ICR_INIT, NULL_TREE, 0,
5ade1ed2 7938 complain);
8d08fdba 7939 else
2f5b91f5 7940 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
e57d93c6 7941 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
41b81065
JM
7942
7943 if (!same_type_p (lhstype, olhstype))
4b978f96 7944 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
41b81065
JM
7945
7946 if (modifycode != INIT_EXPR)
8d08fdba 7947 {
b7484fbe
MS
7948 if (TREE_CODE (newrhs) == CALL_EXPR
7949 && TYPE_NEEDS_CONSTRUCTING (lhstype))
362115a9 7950 newrhs = build_cplus_new (lhstype, newrhs, complain);
b7484fbe 7951
e8abc66f 7952 /* Can't initialize directly from a TARGET_EXPR, since that would
07674418
MS
7953 cause the lhs to be constructed twice, and possibly result in
7954 accidental self-initialization. So we force the TARGET_EXPR to be
be99da77 7955 expanded without a target. */
b7484fbe 7956 if (TREE_CODE (newrhs) == TARGET_EXPR)
f293ce4b
RS
7957 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7958 TREE_OPERAND (newrhs, 0));
8d08fdba
MS
7959 }
7960
bd6dd845 7961 if (newrhs == error_mark_node)
8d08fdba
MS
7962 return error_mark_node;
7963
e58a9aa1
ZL
7964 if (c_dialect_objc () && flag_objc_gc)
7965 {
7966 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7967
7968 if (result)
b84702c0 7969 goto ret;
e58a9aa1
ZL
7970 }
7971
f293ce4b
RS
7972 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7973 lhstype, lhs, newrhs);
2ee887f2 7974
8d08fdba 7975 TREE_SIDE_EFFECTS (result) = 1;
487a92fe
JM
7976 if (!plain_assign)
7977 TREE_NO_WARNING (result) = 1;
8d08fdba 7978
b84702c0
JJ
7979 ret:
7980 if (preeval)
7981 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
41b81065 7982 return result;
8d08fdba
MS
7983}
7984
e87eed2a 7985cp_expr
f330f599
PC
7986build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7987 tree rhs, tsubst_flags_t complain)
5566b478 7988{
fcb9363e
PP
7989 tree orig_lhs = lhs;
7990 tree orig_rhs = rhs;
7991 tree overload = NULL_TREE;
7992 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
7993
5156628f 7994 if (processing_template_decl)
fcb9363e
PP
7995 {
7996 if (modifycode == NOP_EXPR
7997 || type_dependent_expression_p (lhs)
7998 || type_dependent_expression_p (rhs))
7999 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
8000 build_min_nt_loc (loc, modifycode, NULL_TREE,
8001 NULL_TREE), rhs);
8002
8003 lhs = build_non_dependent_expr (lhs);
8004 rhs = build_non_dependent_expr (rhs);
8005 }
5566b478
MS
8006
8007 if (modifycode != NOP_EXPR)
8008 {
fcb9363e
PP
8009 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
8010 lhs, rhs, op, &overload, complain);
5566b478 8011 if (rval)
487a92fe 8012 {
fcb9363e
PP
8013 if (rval == error_mark_node)
8014 return rval;
487a92fe 8015 TREE_NO_WARNING (rval) = 1;
fcb9363e
PP
8016 if (processing_template_decl)
8017 {
8018 if (overload != NULL_TREE)
8019 return (build_min_non_dep_op_overload
8020 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
8021
8022 return (build_min_non_dep
8023 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
8024 }
487a92fe
JM
8025 return rval;
8026 }
5566b478 8027 }
4f2e1536 8028 return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
5566b478 8029}
8d08fdba 8030
6e03b280
OW
8031/* Helper function for get_delta_difference which assumes FROM is a base
8032 class of TO. Returns a delta for the conversion of pointer-to-member
135d47df
PC
8033 of FROM to pointer-to-member of TO. If the conversion is invalid and
8034 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
6e03b280 8035 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
135d47df
PC
8036 If C_CAST_P is true, this conversion is taking place as part of a
8037 C-style cast. */
6e03b280
OW
8038
8039static tree
135d47df
PC
8040get_delta_difference_1 (tree from, tree to, bool c_cast_p,
8041 tsubst_flags_t complain)
6e03b280
OW
8042{
8043 tree binfo;
8044 base_kind kind;
135d47df 8045
22854930
PC
8046 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
8047 &kind, complain);
135d47df 8048
22854930 8049 if (binfo == error_mark_node)
6e03b280 8050 {
135d47df
PC
8051 if (!(complain & tf_error))
8052 return error_mark_node;
8053
6e03b280
OW
8054 error (" in pointer to member function conversion");
8055 return size_zero_node;
8056 }
8057 else if (binfo)
8058 {
8059 if (kind != bk_via_virtual)
8060 return BINFO_OFFSET (binfo);
8061 else
8062 /* FROM is a virtual base class of TO. Issue an error or warning
8063 depending on whether or not this is a reinterpret cast. */
8064 {
135d47df
PC
8065 if (!(complain & tf_error))
8066 return error_mark_node;
8067
6e03b280
OW
8068 error ("pointer to member conversion via virtual base %qT",
8069 BINFO_TYPE (binfo_from_vbase (binfo)));
8070
8071 return size_zero_node;
8072 }
8073 }
135d47df
PC
8074 else
8075 return NULL_TREE;
6e03b280
OW
8076}
8077
ddd5a7c1 8078/* Get difference in deltas for different pointer to member function
135d47df
PC
8079 types. If the conversion is invalid and tf_error is not set in
8080 COMPLAIN, returns error_mark_node, otherwise returns an integer
8081 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8082 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8083 conversions as well. If C_CAST_P is true this conversion is taking
8084 place as part of a C-style cast.
3927874d
JM
8085
8086 Note that the naming of FROM and TO is kind of backwards; the return
8087 value is what we add to a TO in order to get a FROM. They are named
8088 this way because we call this function to find out how to convert from
8089 a pointer to member of FROM to a pointer to member of TO. */
e92cc029 8090
51c184be 8091static tree
c8094d83 8092get_delta_difference (tree from, tree to,
08e17d9d 8093 bool allow_inverse_p,
135d47df 8094 bool c_cast_p, tsubst_flags_t complain)
51c184be 8095{
e8632777
MM
8096 tree result;
8097
6e03b280
OW
8098 if (same_type_ignoring_top_level_qualifiers_p (from, to))
8099 /* Pointer to member of incomplete class is permitted*/
8100 result = size_zero_node;
f879d298 8101 else
135d47df
PC
8102 result = get_delta_difference_1 (from, to, c_cast_p, complain);
8103
8104 if (result == error_mark_node)
8105 return error_mark_node;
c8094d83 8106
6e03b280
OW
8107 if (!result)
8108 {
8109 if (!allow_inverse_p)
8110 {
135d47df
PC
8111 if (!(complain & tf_error))
8112 return error_mark_node;
8113
6e03b280
OW
8114 error_not_base_type (from, to);
8115 error (" in pointer to member conversion");
135d47df 8116 result = size_zero_node;
6e03b280
OW
8117 }
8118 else
8119 {
135d47df
PC
8120 result = get_delta_difference_1 (to, from, c_cast_p, complain);
8121
8122 if (result == error_mark_node)
8123 return error_mark_node;
6e03b280
OW
8124
8125 if (result)
db3927fb 8126 result = size_diffop_loc (input_location,
135d47df 8127 size_zero_node, result);
6e03b280
OW
8128 else
8129 {
135d47df
PC
8130 if (!(complain & tf_error))
8131 return error_mark_node;
8132
6e03b280
OW
8133 error_not_base_type (from, to);
8134 error (" in pointer to member conversion");
8135 result = size_zero_node;
8136 }
8137 }
8138 }
32e02ee0 8139
cda0a029 8140 return convert_to_integer (ptrdiff_type_node, result);
51c184be
MS
8141}
8142
c7e266a6
MM
8143/* Return a constructor for the pointer-to-member-function TYPE using
8144 the other components as specified. */
8145
e08a8f45 8146tree
acd8e2d0 8147build_ptrmemfunc1 (tree type, tree delta, tree pfn)
594740f3 8148{
f78c7bc6 8149 tree u = NULL_TREE;
c7e266a6 8150 tree delta_field;
c7e266a6 8151 tree pfn_field;
9771b263 8152 vec<constructor_elt, va_gc> *v;
594740f3 8153
c7e266a6 8154 /* Pull the FIELD_DECLs out of the type. */
1f84ec23 8155 pfn_field = TYPE_FIELDS (type);
910ad8de 8156 delta_field = DECL_CHAIN (pfn_field);
594740f3 8157
c7e266a6 8158 /* Make sure DELTA has the type we want. */
68fca595 8159 delta = convert_and_check (input_location, delta_type_node, delta);
594740f3 8160
a489b1f0
RG
8161 /* Convert to the correct target type if necessary. */
8162 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8163
c7e266a6 8164 /* Finish creating the initializer. */
9771b263 8165 vec_alloc (v, 2);
4038c495
GB
8166 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8167 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8168 u = build_constructor (type, v);
6de9cd9a 8169 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
530ec96d
MM
8170 TREE_STATIC (u) = (TREE_CONSTANT (u)
8171 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8172 != NULL_TREE)
c8094d83 8173 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
530ec96d 8174 != NULL_TREE));
594740f3 8175 return u;
594740f3
MS
8176}
8177
8d08fdba
MS
8178/* Build a constructor for a pointer to member function. It can be
8179 used to initialize global variables, local variable, or used
8180 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8181 want to be.
8182
838dfd8a 8183 If FORCE is nonzero, then force this conversion, even if
8d08fdba 8184 we would rather not do it. Usually set when using an explicit
08e17d9d 8185 cast. A C-style cast is being processed iff C_CAST_P is true.
51c184be
MS
8186
8187 Return error_mark_node, if something goes wrong. */
8d08fdba
MS
8188
8189tree
135d47df
PC
8190build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8191 tsubst_flags_t complain)
8d08fdba 8192{
e08a8f45 8193 tree fn;
7993382e
MM
8194 tree pfn_type;
8195 tree to_type;
8196
8197 if (error_operand_p (pfn))
8198 return error_mark_node;
8199
8200 pfn_type = TREE_TYPE (pfn);
8201 to_type = build_ptrmemfunc_type (type);
b928a651 8202
e92cc029 8203 /* Handle multiple conversions of pointer to member functions. */
7993382e 8204 if (TYPE_PTRMEMFUNC_P (pfn_type))
8d08fdba 8205 {
530ec96d 8206 tree delta = NULL_TREE;
7133357a 8207 tree npfn = NULL_TREE;
1f84ec23 8208 tree n;
594740f3 8209
c8094d83 8210 if (!force
b40e334f
PC
8211 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8212 LOOKUP_NORMAL, complain))
76545796
PC
8213 {
8214 if (complain & tf_error)
8215 error ("invalid conversion to type %qT from type %qT",
8216 to_type, pfn_type);
8217 else
8218 return error_mark_node;
8219 }
9ca21c0a 8220
3f2b640a
JM
8221 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8222 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
08e17d9d 8223 force,
135d47df
PC
8224 c_cast_p, complain);
8225 if (n == error_mark_node)
8226 return error_mark_node;
3f2b640a 8227
c7e266a6
MM
8228 /* We don't have to do any conversion to convert a
8229 pointer-to-member to its own type. But, we don't want to
8230 just return a PTRMEM_CST if there's an explicit cast; that
8231 cast should make the expression an invalid template argument. */
3f2b640a
JM
8232 if (TREE_CODE (pfn) != PTRMEM_CST)
8233 {
8234 if (same_type_p (to_type, pfn_type))
8235 return pfn;
8236 else if (integer_zerop (n))
5ade1ed2 8237 return build_reinterpret_cast (to_type, pfn,
a3299120 8238 complain);
3f2b640a 8239 }
c7e266a6 8240
7def1251
JM
8241 if (TREE_SIDE_EFFECTS (pfn))
8242 pfn = save_expr (pfn);
8243
530ec96d
MM
8244 /* Obtain the function pointer and the current DELTA. */
8245 if (TREE_CODE (pfn) == PTRMEM_CST)
18ae7f63 8246 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
530ec96d
MM
8247 else
8248 {
50ad9642
MM
8249 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8250 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
530ec96d
MM
8251 }
8252
3461fba7 8253 /* Just adjust the DELTA field. */
50bc768d
NS
8254 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8255 (TREE_TYPE (delta), ptrdiff_type_node));
cb7fdde2 8256 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
ba47d38d
AH
8257 n = cp_build_binary_op (input_location,
8258 LSHIFT_EXPR, n, integer_one_node,
a3299120 8259 complain);
ba47d38d 8260 delta = cp_build_binary_op (input_location,
a3299120 8261 PLUS_EXPR, delta, n, complain);
18ae7f63 8262 return build_ptrmemfunc1 (to_type, delta, npfn);
8d08fdba 8263 }
51c184be 8264
e92cc029 8265 /* Handle null pointer to member function conversions. */
33c2474d 8266 if (null_ptr_cst_p (pfn))
51c184be 8267 {
f61af651 8268 pfn = cp_build_c_cast (type, pfn, complain);
b928a651 8269 return build_ptrmemfunc1 (to_type,
c8094d83 8270 integer_zero_node,
18ae7f63 8271 pfn);
51c184be
MS
8272 }
8273
e6e174e5 8274 if (type_unknown_p (pfn))
a3299120 8275 return instantiate_type (type, pfn, complain);
a0a33927 8276
e08a8f45 8277 fn = TREE_OPERAND (pfn, 0);
37048601
NS
8278 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8279 /* In a template, we will have preserved the
8280 OFFSET_REF. */
8281 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
b928a651 8282 return make_ptrmem_cst (to_type, fn);
e08a8f45 8283}
8d08fdba 8284
18ae7f63 8285/* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
fed3cef0
RK
8286 given by CST.
8287
8288 ??? There is no consistency as to the types returned for the above
852dcbdd 8289 values. Some code acts as if it were a sizetype and some as if it were
fed3cef0 8290 integer_type_node. */
8d08fdba 8291
e08a8f45 8292void
acd8e2d0 8293expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
e08a8f45
MM
8294{
8295 tree type = TREE_TYPE (cst);
8296 tree fn = PTRMEM_CST_MEMBER (cst);
3927874d 8297 tree ptr_class, fn_class;
8d08fdba 8298
50bc768d 8299 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
3927874d
JM
8300
8301 /* The class that the function belongs to. */
4f1c5b7d 8302 fn_class = DECL_CONTEXT (fn);
3927874d
JM
8303
8304 /* The class that we're creating a pointer to member of. */
8305 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8306
8307 /* First, calculate the adjustment to the function's class. */
08e17d9d 8308 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
135d47df 8309 /*c_cast_p=*/0, tf_warning_or_error);
3927874d 8310
e08a8f45 8311 if (!DECL_VIRTUAL_P (fn))
89fcabaf
PC
8312 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8313 build_addr_func (fn, tf_warning_or_error));
8d08fdba 8314 else
51c184be 8315 {
3927874d 8316 /* If we're dealing with a virtual function, we have to adjust 'this'
0cbd7506
MS
8317 again, to point to the base which provides the vtable entry for
8318 fn; the call will do the opposite adjustment. */
e93ee644 8319 tree orig_class = DECL_CONTEXT (fn);
3927874d 8320 tree binfo = binfo_or_else (orig_class, fn_class);
cda0a029
JM
8321 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8322 *delta, BINFO_OFFSET (binfo));
3927874d 8323
3461fba7
NS
8324 /* We set PFN to the vtable offset at which the function can be
8325 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8326 case delta is shifted left, and then incremented). */
cb7fdde2 8327 *pfn = DECL_VINDEX (fn);
cda0a029
JM
8328 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8329 TYPE_SIZE_UNIT (vtable_entry_type));
cb7fdde2
AO
8330
8331 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8332 {
8333 case ptrmemfunc_vbit_in_pfn:
cda0a029
JM
8334 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8335 integer_one_node);
cb7fdde2
AO
8336 break;
8337
8338 case ptrmemfunc_vbit_in_delta:
cda0a029
JM
8339 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8340 *delta, integer_one_node);
8341 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8342 *delta, integer_one_node);
cb7fdde2
AO
8343 break;
8344
8345 default:
315fb5db 8346 gcc_unreachable ();
cb7fdde2
AO
8347 }
8348
cda0a029 8349 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
e08a8f45
MM
8350 }
8351}
8d08fdba 8352
e08a8f45
MM
8353/* Return an expression for PFN from the pointer-to-member function
8354 given by T. */
8355
b5791fdc 8356static tree
acd8e2d0 8357pfn_from_ptrmemfunc (tree t)
e08a8f45
MM
8358{
8359 if (TREE_CODE (t) == PTRMEM_CST)
8360 {
8361 tree delta;
e08a8f45 8362 tree pfn;
c8094d83 8363
18ae7f63 8364 expand_ptrmemfunc_cst (t, &delta, &pfn);
e08a8f45
MM
8365 if (pfn)
8366 return pfn;
51c184be 8367 }
8d08fdba 8368
50ad9642 8369 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8d08fdba
MS
8370}
8371
a298680c
OW
8372/* Return an expression for DELTA from the pointer-to-member function
8373 given by T. */
8374
8375static tree
8376delta_from_ptrmemfunc (tree t)
8377{
8378 if (TREE_CODE (t) == PTRMEM_CST)
8379 {
8380 tree delta;
8381 tree pfn;
8382
8383 expand_ptrmemfunc_cst (t, &delta, &pfn);
8384 if (delta)
8385 return delta;
8386 }
8387
8388 return build_ptrmemfunc_access_expr (t, delta_identifier);
8389}
8390
a7a64a77 8391/* Convert value RHS to type TYPE as preparation for an assignment to
9d9f52d3
SZ
8392 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8393 implicit conversion is. If FNDECL is non-NULL, we are doing the
8394 conversion in order to pass the PARMNUMth argument of FNDECL.
8395 If FNDECL is NULL, we are doing the conversion in function pointer
8396 argument passing, conversion in initialization, etc. */
8d08fdba
MS
8397
8398static tree
acd8e2d0 8399convert_for_assignment (tree type, tree rhs,
2f5b91f5 8400 impl_conv_rhs errtype, tree fndecl, int parmnum,
e57d93c6 8401 tsubst_flags_t complain, int flags)
8d08fdba 8402{
926ce8bd
KH
8403 tree rhstype;
8404 enum tree_code coder;
8d08fdba 8405
8d08fdba
MS
8406 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8407 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8408 rhs = TREE_OPERAND (rhs, 0);
8409
d664d76d
JJ
8410 /* Handle [dcl.init.list] direct-list-initialization from
8411 single element of enumeration with a fixed underlying type. */
8412 if (is_direct_enum_init (type, rhs))
8413 {
8414 tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
8415 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
8416 rhs = cp_build_c_cast (type, elt, complain);
8417 else
8418 rhs = error_mark_node;
8419 }
8420
a7a64a77
MM
8421 rhstype = TREE_TYPE (rhs);
8422 coder = TREE_CODE (rhstype);
8423
b55b02ea 8424 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
00c8e9f6 8425 && vector_types_convertible_p (type, rhstype, true))
416f380b
JJ
8426 {
8427 rhs = mark_rvalue_use (rhs);
8428 return convert (type, rhs);
8429 }
7d149679 8430
a7a64a77 8431 if (rhs == error_mark_node || rhstype == error_mark_node)
8d08fdba 8432 return error_mark_node;
2c73f9f5 8433 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8d08fdba
MS
8434 return error_mark_node;
8435
a7a64a77 8436 /* The RHS of an assignment cannot have void type. */
8d08fdba
MS
8437 if (coder == VOID_TYPE)
8438 {
5ade1ed2
DG
8439 if (complain & tf_error)
8440 error ("void value not ignored as it ought to be");
8d08fdba
MS
8441 return error_mark_node;
8442 }
8d08fdba 8443
660845bf
ZL
8444 if (c_dialect_objc ())
8445 {
8446 int parmno;
2f5b91f5 8447 tree selector;
660845bf
ZL
8448 tree rname = fndecl;
8449
2f5b91f5
SZ
8450 switch (errtype)
8451 {
8452 case ICR_ASSIGN:
8453 parmno = -1;
8454 break;
8455 case ICR_INIT:
8456 parmno = -2;
8457 break;
8458 default:
8459 selector = objc_message_selector ();
8460 parmno = parmnum;
8461 if (selector && parmno > 1)
8462 {
8463 rname = selector;
8464 parmno -= 1;
8465 }
660845bf
ZL
8466 }
8467
8468 if (objc_compare_types (type, rhstype, parmno, rname))
416f380b
JJ
8469 {
8470 rhs = mark_rvalue_use (rhs);
8471 return convert (type, rhs);
8472 }
660845bf
ZL
8473 }
8474
a7a64a77
MM
8475 /* [expr.ass]
8476
8477 The expression is implicitly converted (clause _conv_) to the
72a08131
JM
8478 cv-unqualified type of the left operand.
8479
8480 We allow bad conversions here because by the time we get to this point
8481 we are committed to doing the conversion. If we end up doing a bad
8482 conversion, convert_like will complain. */
b40e334f 8483 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
a7a64a77 8484 {
32facac8 8485 /* When -Wno-pmf-conversions is use, we just silently allow
a7a64a77
MM
8486 conversions from pointers-to-members to plain pointers. If
8487 the conversion doesn't work, cp_convert will complain. */
c8094d83
MS
8488 if (!warn_pmf2ptr
8489 && TYPE_PTR_P (type)
a7a64a77 8490 && TYPE_PTRMEMFUNC_P (rhstype))
4b978f96 8491 rhs = cp_convert (strip_top_quals (type), rhs, complain);
72a08131 8492 else
32facac8 8493 {
5ade1ed2
DG
8494 if (complain & tf_error)
8495 {
8496 /* If the right-hand side has unknown type, then it is an
8497 overloaded function. Call instantiate_type to get error
8498 messages. */
8499 if (rhstype == unknown_type_node)
559a77e1
JM
8500 {
8501 tree r = instantiate_type (type, rhs, tf_warning_or_error);
8502 /* -fpermissive might allow this. */
8503 if (!seen_error ())
8504 return r;
8505 }
5ade1ed2
DG
8506 else if (fndecl)
8507 error ("cannot convert %qT to %qT for argument %qP to %qD",
8508 rhstype, type, parmnum, fndecl);
8509 else
2f5b91f5
SZ
8510 switch (errtype)
8511 {
8512 case ICR_DEFAULT_ARGUMENT:
8513 error ("cannot convert %qT to %qT in default argument",
8514 rhstype, type);
8515 break;
8516 case ICR_ARGPASS:
8517 error ("cannot convert %qT to %qT in argument passing",
8518 rhstype, type);
8519 break;
8520 case ICR_CONVERTING:
8521 error ("cannot convert %qT to %qT",
8522 rhstype, type);
8523 break;
8524 case ICR_INIT:
8525 error ("cannot convert %qT to %qT in initialization",
8526 rhstype, type);
8527 break;
8528 case ICR_RETURN:
8529 error ("cannot convert %qT to %qT in return",
8530 rhstype, type);
8531 break;
8532 case ICR_ASSIGN:
8533 error ("cannot convert %qT to %qT in assignment",
8534 rhstype, type);
8535 break;
8536 default:
8537 gcc_unreachable();
8538 }
7e422c4f
PC
8539 if (TYPE_PTR_P (rhstype)
8540 && TYPE_PTR_P (type)
8541 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8542 && CLASS_TYPE_P (TREE_TYPE (type))
8543 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8544 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8545 (TREE_TYPE (rhstype))),
8546 "class type %qT is incomplete", TREE_TYPE (rhstype));
5ade1ed2 8547 }
32facac8
ML
8548 return error_mark_node;
8549 }
51c184be 8550 }
90137d8f 8551 if (warn_suggest_attribute_format)
104f8784
KG
8552 {
8553 const enum tree_code codel = TREE_CODE (type);
8554 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8555 && coder == codel
5ade1ed2
DG
8556 && check_missing_format_attribute (type, rhstype)
8557 && (complain & tf_warning))
2f5b91f5
SZ
8558 switch (errtype)
8559 {
8560 case ICR_ARGPASS:
8561 case ICR_DEFAULT_ARGUMENT:
8562 if (fndecl)
90137d8f 8563 warning (OPT_Wsuggest_attribute_format,
9d9f52d3 8564 "parameter %qP of %qD might be a candidate "
2f5b91f5
SZ
8565 "for a format attribute", parmnum, fndecl);
8566 else
90137d8f 8567 warning (OPT_Wsuggest_attribute_format,
2f5b91f5
SZ
8568 "parameter might be a candidate "
8569 "for a format attribute");
8570 break;
8571 case ICR_CONVERTING:
90137d8f 8572 warning (OPT_Wsuggest_attribute_format,
59a67d73 8573 "target of conversion might be a candidate "
2f5b91f5
SZ
8574 "for a format attribute");
8575 break;
8576 case ICR_INIT:
90137d8f 8577 warning (OPT_Wsuggest_attribute_format,
2f5b91f5
SZ
8578 "target of initialization might be a candidate "
8579 "for a format attribute");
8580 break;
8581 case ICR_RETURN:
90137d8f 8582 warning (OPT_Wsuggest_attribute_format,
2f5b91f5
SZ
8583 "return type might be a candidate "
8584 "for a format attribute");
8585 break;
8586 case ICR_ASSIGN:
90137d8f 8587 warning (OPT_Wsuggest_attribute_format,
2f5b91f5
SZ
8588 "left-hand side of assignment might be a candidate "
8589 "for a format attribute");
8590 break;
8591 default:
8592 gcc_unreachable();
8593 }
104f8784 8594 }
3db45ab5 8595
a99e5cb4
ILT
8596 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8597 does not. */
fbc8d2d3 8598 if (warn_parentheses
66be89f0 8599 && TREE_CODE (type) == BOOLEAN_TYPE
fbc8d2d3 8600 && TREE_CODE (rhs) == MODIFY_EXPR
a99e5cb4 8601 && !TREE_NO_WARNING (rhs)
69fb9832 8602 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
5ade1ed2 8603 && (complain & tf_warning))
fbc8d2d3 8604 {
8400e75e 8605 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
69fb9832
MLI
8606
8607 warning_at (loc, OPT_Wparentheses,
8608 "suggest parentheses around assignment used as truth value");
fbc8d2d3
ILT
8609 TREE_NO_WARNING (rhs) = 1;
8610 }
8611
e57d93c6
JM
8612 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8613 complain, flags);
8d08fdba
MS
8614}
8615
56ae6d77 8616/* Convert RHS to be of type TYPE.
838dfd8a 8617 If EXP is nonzero, it is the target of the initialization.
9d9f52d3 8618 ERRTYPE indicates what kind of error the implicit conversion is.
8d08fdba
MS
8619
8620 Two major differences between the behavior of
8621 `convert_for_assignment' and `convert_for_initialization'
8622 are that references are bashed in the former, while
8623 copied in the latter, and aggregates are assigned in
8624 the former (operator=) while initialized in the
8625 latter (X(X&)).
8626
8627 If using constructor make sure no conversion operator exists, if one does
4b978f96 8628 exist, an ambiguity exists. */
e92cc029 8629
8d08fdba 8630tree
acd8e2d0 8631convert_for_initialization (tree exp, tree type, tree rhs, int flags,
2f5b91f5 8632 impl_conv_rhs errtype, tree fndecl, int parmnum,
5ade1ed2 8633 tsubst_flags_t complain)
8d08fdba 8634{
926ce8bd
KH
8635 enum tree_code codel = TREE_CODE (type);
8636 tree rhstype;
8637 enum tree_code coder;
8d08fdba
MS
8638
8639 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8640 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8641 if (TREE_CODE (rhs) == NOP_EXPR
a0a33927
MS
8642 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8643 && codel != REFERENCE_TYPE)
8d08fdba
MS
8644 rhs = TREE_OPERAND (rhs, 0);
8645
57b52959
VR
8646 if (type == error_mark_node
8647 || rhs == error_mark_node
8d08fdba
MS
8648 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8649 return error_mark_node;
8650
f85e1317
PP
8651 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8652 ;
8653 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8654 && TREE_CODE (type) != ARRAY_TYPE
8655 && (TREE_CODE (type) != REFERENCE_TYPE
8656 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8657 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8658 && !TYPE_REFFN_P (type))
8659 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
89fcabaf 8660 rhs = decay_conversion (rhs, complain);
8d08fdba
MS
8661
8662 rhstype = TREE_TYPE (rhs);
8663 coder = TREE_CODE (rhstype);
8664
8d08fdba
MS
8665 if (coder == ERROR_MARK)
8666 return error_mark_node;
8667
8d08fdba
MS
8668 /* We accept references to incomplete types, so we can
8669 return here before checking if RHS is of complete type. */
c8094d83 8670
8d08fdba 8671 if (codel == REFERENCE_TYPE)
2986ae00
MS
8672 {
8673 /* This should eventually happen in convert_arguments. */
a703fb38 8674 int savew = 0, savee = 0;
2986ae00
MS
8675
8676 if (fndecl)
37e99116 8677 savew = warningcount + werrorcount, savee = errorcount;
b25dd954 8678 rhs = initialize_reference (type, rhs, flags, complain);
7abebba7
PC
8679
8680 if (fndecl
8681 && (warningcount + werrorcount > savew || errorcount > savee))
af718670
PC
8682 inform (DECL_SOURCE_LOCATION (fndecl),
8683 "in passing argument %P of %qD", parmnum, fndecl);
7abebba7 8684
2986ae00 8685 return rhs;
c8094d83 8686 }
8d08fdba 8687
66543169 8688 if (exp != 0)
79fe346e 8689 exp = require_complete_type_sfinae (exp, complain);
8d08fdba
MS
8690 if (exp == error_mark_node)
8691 return error_mark_node;
8692
ee76b931 8693 rhstype = non_reference (rhstype);
8d08fdba 8694
6467930b
MS
8695 type = complete_type (type);
8696
37a7519a
JM
8697 if (DIRECT_INIT_EXPR_P (type, rhs))
8698 /* Don't try to do copy-initialization if we already have
8699 direct-initialization. */
8700 return rhs;
8701
9e1e64ec 8702 if (MAYBE_CLASS_TYPE_P (type))
f8f12278 8703 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8d08fdba 8704
5ade1ed2 8705 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
e57d93c6 8706 complain, flags);
8d08fdba
MS
8707}
8708\f
efee38a9 8709/* If RETVAL is the address of, or a reference to, a local variable or
b4dfdc11 8710 temporary give an appropriate warning and return true. */
8d08fdba 8711
b4dfdc11 8712static bool
acd8e2d0 8713maybe_warn_about_returning_address_of_local (tree retval)
efee38a9
MM
8714{
8715 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
f4bb5c17 8716 tree whats_returned = fold_for_warn (retval);
8d08fdba 8717
2bfa73e4 8718 for (;;)
efee38a9 8719 {
efee38a9 8720 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
2bfa73e4 8721 whats_returned = TREE_OPERAND (whats_returned, 1);
5f9e56b3
TB
8722 else if (CONVERT_EXPR_P (whats_returned)
8723 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
efee38a9 8724 whats_returned = TREE_OPERAND (whats_returned, 0);
2bfa73e4
BS
8725 else
8726 break;
8727 }
8728
8729 if (TREE_CODE (whats_returned) != ADDR_EXPR)
b4dfdc11 8730 return false;
c8094d83 8731 whats_returned = TREE_OPERAND (whats_returned, 0);
2bfa73e4 8732
75d850f3
PP
8733 while (TREE_CODE (whats_returned) == COMPONENT_REF
8734 || TREE_CODE (whats_returned) == ARRAY_REF)
8735 whats_returned = TREE_OPERAND (whats_returned, 0);
8736
2bfa73e4
BS
8737 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8738 {
8739 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8740 || TREE_CODE (whats_returned) == TARGET_EXPR)
efee38a9 8741 {
880661a4 8742 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
b4dfdc11 8743 return true;
efee38a9 8744 }
5a6ccc94 8745 if (VAR_P (whats_returned)
2bfa73e4
BS
8746 && DECL_NAME (whats_returned)
8747 && TEMP_NAME_P (DECL_NAME (whats_returned)))
efee38a9 8748 {
880661a4 8749 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
b4dfdc11 8750 return true;
efee38a9
MM
8751 }
8752 }
efee38a9 8753
a2f1f4c3 8754 if (DECL_P (whats_returned)
2bfa73e4
BS
8755 && DECL_NAME (whats_returned)
8756 && DECL_FUNCTION_SCOPE_P (whats_returned)
5ced939e 8757 && !is_capture_proxy (whats_returned)
2bfa73e4
BS
8758 && !(TREE_STATIC (whats_returned)
8759 || TREE_PUBLIC (whats_returned)))
8760 {
8761 if (TREE_CODE (valtype) == REFERENCE_TYPE)
af718670
PC
8762 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8763 OPT_Wreturn_local_addr,
8764 "reference to local variable %qD returned",
8765 whats_returned);
19fc9faa 8766 else if (TREE_CODE (whats_returned) == LABEL_DECL)
af718670
PC
8767 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8768 OPT_Wreturn_local_addr, "address of label %qD returned",
8769 whats_returned);
2bfa73e4 8770 else
af718670
PC
8771 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8772 OPT_Wreturn_local_addr, "address of local variable %qD "
8773 "returned", whats_returned);
b4dfdc11 8774 return true;
efee38a9 8775 }
b4dfdc11
MG
8776
8777 return false;
efee38a9
MM
8778}
8779
0e339752 8780/* Check that returning RETVAL from the current function is valid.
efee38a9
MM
8781 Return an expression explicitly showing all conversions required to
8782 change RETVAL into the function return type, and to assign it to
0c9b182b
JJ
8783 the DECL_RESULT for the function. Set *NO_WARNING to true if
8784 code reaches end of non-void function warning shouldn't be issued
8785 on this RETURN_EXPR. */
efee38a9
MM
8786
8787tree
0c9b182b 8788check_return_expr (tree retval, bool *no_warning)
8d08fdba 8789{
efee38a9 8790 tree result;
852497a3 8791 /* The type actually returned by the function. */
efee38a9 8792 tree valtype;
852497a3
JM
8793 /* The type the function is declared to return, or void if
8794 the declared type is incomplete. */
8795 tree functype;
efee38a9 8796 int fn_returns_value_p;
8af2fec4 8797 bool named_return_value_okay_p;
efee38a9 8798
0c9b182b
JJ
8799 *no_warning = false;
8800
3af9c5e9 8801 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
12893402
BI
8802 {
8803 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8804 "statement is not allowed");
8805 return NULL_TREE;
8806 }
8807
efee38a9
MM
8808 /* A `volatile' function is one that isn't supposed to return, ever.
8809 (This is a G++ extension, used to get better code for functions
8810 that call the `volatile' function.) */
8d08fdba 8811 if (TREE_THIS_VOLATILE (current_function_decl))
d4ee4d25 8812 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8d08fdba 8813
efee38a9 8814 /* Check for various simple errors. */
a0de9d20 8815 if (DECL_DESTRUCTOR_P (current_function_decl))
8d08fdba 8816 {
1c2c08a5 8817 if (retval)
8251199e 8818 error ("returning a value from a destructor");
efee38a9 8819 return NULL_TREE;
1c2c08a5 8820 }
90418208 8821 else if (DECL_CONSTRUCTOR_P (current_function_decl))
0dde4175 8822 {
90418208
JM
8823 if (in_function_try_handler)
8824 /* If a return statement appears in a handler of the
c6002625 8825 function-try-block of a constructor, the program is ill-formed. */
90418208
JM
8826 error ("cannot return from a handler of a function-try-block of a constructor");
8827 else if (retval)
8828 /* You can't return a value from a constructor. */
8829 error ("returning a value from a constructor");
8830 return NULL_TREE;
efee38a9 8831 }
efee38a9 8832
11f2c78a
PP
8833 const tree saved_retval = retval;
8834
efc7052d
JM
8835 if (processing_template_decl)
8836 {
8837 current_function_returns_value = 1;
11f2c78a 8838
7b3e2d46 8839 if (check_for_bare_parameter_packs (retval))
11f2c78a
PP
8840 return error_mark_node;
8841
8842 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8843 || (retval != NULL_TREE
8844 && type_dependent_expression_p (retval)))
8845 return retval;
efc7052d 8846 }
c8094d83 8847
852497a3
JM
8848 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8849
8850 /* Deduce auto return type from a return statement. */
8851 if (current_function_auto_return_pattern)
8852 {
8853 tree auto_node;
8854 tree type;
8855
8856 if (!retval && !is_auto (current_function_auto_return_pattern))
8857 {
8858 /* Give a helpful error message. */
8859 error ("return-statement with no value, in function returning %qT",
8860 current_function_auto_return_pattern);
8861 inform (input_location, "only plain %<auto%> return type can be "
8862 "deduced to %<void%>");
8863 type = error_mark_node;
8864 }
2aa953d0
JM
8865 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8866 {
8867 error ("returning initializer list");
8868 type = error_mark_node;
8869 }
852497a3
JM
8870 else
8871 {
8872 if (!retval)
632f2871 8873 retval = void_node;
852497a3
JM
8874 auto_node = type_uses_auto (current_function_auto_return_pattern);
8875 type = do_auto_deduction (current_function_auto_return_pattern,
8876 retval, auto_node);
8877 }
8878
8879 if (type == error_mark_node)
8880 /* Leave it. */;
8881 else if (functype == current_function_auto_return_pattern)
8882 apply_deduced_return_type (current_function_decl, type);
5dc58049
JM
8883 else if (!same_type_p (type, functype))
8884 {
8885 if (LAMBDA_FUNCTION_P (current_function_decl))
8886 error ("inconsistent types %qT and %qT deduced for "
8887 "lambda return type", functype, type);
8888 else
8889 error ("inconsistent deduction for auto return type: "
8890 "%qT and then %qT", functype, type);
8891 }
852497a3
JM
8892 functype = type;
8893 }
8894
efee38a9
MM
8895 result = DECL_RESULT (current_function_decl);
8896 valtype = TREE_TYPE (result);
50bc768d 8897 gcc_assert (valtype != NULL_TREE);
b72801e2 8898 fn_returns_value_p = !VOID_TYPE_P (valtype);
efee38a9
MM
8899
8900 /* Check for a return statement with no return value in a function
8901 that's supposed to return a value. */
8902 if (!retval && fn_returns_value_p)
8903 {
852497a3
JM
8904 if (functype != error_mark_node)
8905 permerror (input_location, "return-statement with no value, in "
8906 "function returning %qT", valtype);
8907 /* Remember that this function did return. */
8908 current_function_returns_value = 1;
0c9b182b 8909 /* And signal caller that TREE_NO_WARNING should be set on the
3db45ab5
MS
8910 RETURN_EXPR to avoid control reaches end of non-void function
8911 warnings in tree-cfg.c. */
0c9b182b 8912 *no_warning = true;
efee38a9
MM
8913 }
8914 /* Check for a return statement with a value in a function that
8915 isn't supposed to return a value. */
8916 else if (retval && !fn_returns_value_p)
c8094d83 8917 {
b72801e2 8918 if (VOID_TYPE_P (TREE_TYPE (retval)))
efee38a9
MM
8919 /* You can return a `void' value from a function of `void'
8920 type. In that case, we have to evaluate the expression for
8921 its side-effects. */
8922 finish_expr_stmt (retval);
8923 else
cbe5f3b3 8924 permerror (input_location, "return-statement with a value, in function "
37ec60ed 8925 "returning 'void'");
efee38a9
MM
8926 current_function_returns_null = 1;
8927
8928 /* There's really no value to return, after all. */
8929 return NULL_TREE;
0dde4175 8930 }
efee38a9
MM
8931 else if (!retval)
8932 /* Remember that this function can sometimes return without a
8933 value. */
8934 current_function_returns_null = 1;
90418208
JM
8935 else
8936 /* Remember that this function did return a value. */
8937 current_function_returns_value = 1;
1c2c08a5 8938
276318a5 8939 /* Check for erroneous operands -- but after giving ourselves a
5ae9ba3e
MM
8940 chance to provide an error about returning a value from a void
8941 function. */
8942 if (error_operand_p (retval))
8943 {
8944 current_function_return_value = error_mark_node;
8945 return error_mark_node;
8946 }
8947
7f477e81 8948 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
596ea4e5
AS
8949 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8950 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
7f477e81 8951 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
708cae97 8952 && ! flag_check_new
aedfac0e 8953 && retval && null_ptr_cst_p (retval))
d4ee4d25 8954 warning (0, "%<operator new%> must not return NULL unless it is "
0cbd7506 8955 "declared %<throw()%> (or -fcheck-new is in effect)");
8d08fdba 8956
824b9a4c 8957 /* Effective C++ rule 15. See also start_function. */
eb448459 8958 if (warn_ecpp
8db29d88 8959 && DECL_NAME (current_function_decl) == cp_assignment_operator_id (NOP_EXPR))
47293da3
GB
8960 {
8961 bool warn = true;
8962
8963 /* The function return type must be a reference to the current
8964 class. */
8965 if (TREE_CODE (valtype) == REFERENCE_TYPE
8966 && same_type_ignoring_top_level_qualifiers_p
8967 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8968 {
8969 /* Returning '*this' is obviously OK. */
8970 if (retval == current_class_ref)
8971 warn = false;
8972 /* If we are calling a function whose return type is the same of
8973 the current class reference, it is ok. */
591cb3cf 8974 else if (INDIRECT_REF_P (retval)
47293da3 8975 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
616adc47 8976 warn = false;
47293da3
GB
8977 }
8978
8979 if (warn)
b323323f 8980 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
47293da3 8981 }
824b9a4c 8982
11f2c78a
PP
8983 if (processing_template_decl)
8984 {
8985 /* We should not have changed the return value. */
8986 gcc_assert (retval == saved_retval);
8987 return retval;
8988 }
8989
07b2f2fd
JM
8990 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8991
8992 [...] For a function with a class return type, if the expression
8993 in the return statement is the name of a local object, and the cv-
8994 unqualified type of the local object is the same as the function
8995 return type, an implementation is permitted to omit creating the tem-
8996 porary object to hold the function return value [...]
8997
8998 So, if this is a value-returning function that always returns the same
8999 local variable, remember it.
0d97bf4c
JM
9000
9001 It might be nice to be more flexible, and choose the first suitable
9002 variable even if the function sometimes returns something else, but
9003 then we run the risk of clobbering the variable we chose if the other
9004 returned expression uses the chosen variable somehow. And people expect
07b2f2fd
JM
9005 this restriction, anyway. (jason 2000-11-19)
9006
324f9dfb 9007 See finish_function and finalize_nrv for the rest of this optimization. */
0d97bf4c 9008
8af2fec4
RY
9009 named_return_value_okay_p =
9010 (retval != NULL_TREE
9011 /* Must be a local, automatic variable. */
5a6ccc94 9012 && VAR_P (retval)
8af2fec4
RY
9013 && DECL_CONTEXT (retval) == current_function_decl
9014 && ! TREE_STATIC (retval)
f9f91ddc
JM
9015 /* And not a lambda or anonymous union proxy. */
9016 && !DECL_HAS_VALUE_EXPR_P (retval)
5657d966 9017 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8af2fec4
RY
9018 /* The cv-unqualified type of the returned value must be the
9019 same as the cv-unqualified return type of the
9020 function. */
9021 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
852497a3 9022 (TYPE_MAIN_VARIANT (functype)))
0e95aec1
CB
9023 /* And the returned value must be non-volatile. */
9024 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8af2fec4 9025
01f9e964 9026 if (fn_returns_value_p && flag_elide_constructors)
0d97bf4c 9027 {
8af2fec4
RY
9028 if (named_return_value_okay_p
9029 && (current_function_return_value == NULL_TREE
9030 || current_function_return_value == retval))
0d97bf4c
JM
9031 current_function_return_value = retval;
9032 else
9033 current_function_return_value = error_mark_node;
9034 }
9035
efee38a9
MM
9036 /* We don't need to do any conversions when there's nothing being
9037 returned. */
5ae9ba3e
MM
9038 if (!retval)
9039 return NULL_TREE;
efee38a9
MM
9040
9041 /* Do any required conversions. */
9042 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
9043 /* No conversions are required. */
9044 ;
8d08fdba
MS
9045 else
9046 {
8af2fec4 9047 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
cce2be43 9048
51b15ede
NS
9049 /* The functype's return type will have been set to void, if it
9050 was an incomplete type. Just treat this as 'return;' */
9051 if (VOID_TYPE_P (functype))
9052 return error_mark_node;
3db45ab5 9053
96e780c0
JM
9054 /* If we had an id-expression obfuscated by force_paren_expr, we need
9055 to undo it so we can try to treat it as an rvalue below. */
1137001c 9056 retval = maybe_undo_parenthesized_ref (retval);
96e780c0 9057
fb682f94 9058 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8af2fec4 9059 treated as an rvalue for the purposes of overload resolution to
532c679e
JM
9060 favor move constructors over copy constructors.
9061
9062 Note that these conditions are similar to, but not as strict as,
9063 the conditions for the named return value optimization. */
9064 if ((cxx_dialect != cxx98)
f9f91ddc 9065 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
532c679e
JM
9066 || TREE_CODE (retval) == PARM_DECL)
9067 && DECL_CONTEXT (retval) == current_function_decl
9068 && !TREE_STATIC (retval)
532c679e 9069 /* This is only interesting for class type. */
852497a3 9070 && CLASS_TYPE_P (functype))
8af2fec4
RY
9071 flags = flags | LOOKUP_PREFER_RVALUE;
9072
cce2be43
JM
9073 /* First convert the value to the function's return type, then
9074 to the type of return value's location to handle the
0cbd7506 9075 case that functype is smaller than the valtype. */
c1bc6829 9076 retval = convert_for_initialization
2f5b91f5 9077 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
5ade1ed2 9078 tf_warning_or_error);
cce2be43
JM
9079 retval = convert (valtype, retval);
9080
efee38a9 9081 /* If the conversion failed, treat this just like `return;'. */
691c003d 9082 if (retval == error_mark_node)
90418208 9083 return retval;
02531345 9084 /* We can't initialize a register from a AGGR_INIT_EXPR. */
e3b5732b 9085 else if (! cfun->returns_struct
c1bc6829 9086 && TREE_CODE (retval) == TARGET_EXPR
02531345 9087 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
f293ce4b
RS
9088 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9089 TREE_OPERAND (retval, 0));
b4dfdc11
MG
9090 else if (maybe_warn_about_returning_address_of_local (retval))
9091 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
9092 build_zero_cst (TREE_TYPE (retval)));
8d08fdba 9093 }
c8094d83 9094
efee38a9 9095 /* Actually copy the value returned into the appropriate location. */
691c003d 9096 if (retval && retval != result)
f293ce4b 9097 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
b88c08b6 9098
efee38a9
MM
9099 return retval;
9100}
9101
8d08fdba 9102\f
838dfd8a 9103/* Returns nonzero if the pointer-type FROM can be converted to the
ceab47eb 9104 pointer-type TO via a qualification conversion. If CONSTP is -1,
838dfd8a 9105 then we return nonzero if the pointers are similar, and the
ceab47eb
MM
9106 cv-qualification signature of FROM is a proper subset of that of TO.
9107
9108 If CONSTP is positive, then all outer pointers have been
9109 const-qualified. */
e92cc029 9110
bd6dd845 9111static int
acd8e2d0 9112comp_ptr_ttypes_real (tree to, tree from, int constp)
a0a33927 9113{
a5ac359a 9114 bool to_more_cv_qualified = false;
f83c7f63 9115 bool is_opaque_pointer = false;
ceab47eb 9116
a0a33927
MS
9117 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9118 {
9119 if (TREE_CODE (to) != TREE_CODE (from))
9120 return 0;
9121
d11ad92e 9122 if (TREE_CODE (from) == OFFSET_TYPE
b7a78333
MM
9123 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
9124 TYPE_OFFSET_BASETYPE (to)))
9125 return 0;
d11ad92e 9126
f30432d7
MS
9127 /* Const and volatile mean something different for function types,
9128 so the usual checks are not appropriate. */
9129 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
9130 {
6c39e757 9131 if (!at_least_as_qualified_p (to, from))
02020185 9132 return 0;
a0a33927 9133
6c39e757 9134 if (!at_least_as_qualified_p (from, to))
02020185 9135 {
ceab47eb
MM
9136 if (constp == 0)
9137 return 0;
a5ac359a 9138 to_more_cv_qualified = true;
ceab47eb
MM
9139 }
9140
9141 if (constp > 0)
6c39e757 9142 constp &= TYPE_READONLY (to);
f30432d7 9143 }
a0a33927 9144
b55b02ea 9145 if (VECTOR_TYPE_P (to))
f83c7f63
DJ
9146 is_opaque_pointer = vector_targets_convertible_p (to, from);
9147
50e10fa8 9148 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8de9bb0e 9149 return ((constp >= 0 || to_more_cv_qualified)
f83c7f63
DJ
9150 && (is_opaque_pointer
9151 || same_type_ignoring_top_level_qualifiers_p (to, from)));
a0a33927
MS
9152 }
9153}
9154
8de9bb0e
NS
9155/* When comparing, say, char ** to char const **, this function takes
9156 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9157 types to this function. */
e92cc029 9158
a0a33927 9159int
acd8e2d0 9160comp_ptr_ttypes (tree to, tree from)
a0a33927
MS
9161{
9162 return comp_ptr_ttypes_real (to, from, 1);
9163}
d11ad92e 9164
d4684c00
JM
9165/* Returns true iff FNTYPE is a non-class type that involves
9166 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9167 if a parameter type is ill-formed. */
9168
9169bool
9170error_type_p (const_tree type)
9171{
9172 tree t;
9173
9174 switch (TREE_CODE (type))
9175 {
9176 case ERROR_MARK:
9177 return true;
9178
9179 case POINTER_TYPE:
9180 case REFERENCE_TYPE:
9181 case OFFSET_TYPE:
9182 return error_type_p (TREE_TYPE (type));
9183
9184 case FUNCTION_TYPE:
9185 case METHOD_TYPE:
9186 if (error_type_p (TREE_TYPE (type)))
9187 return true;
9188 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9189 if (error_type_p (TREE_VALUE (t)))
9190 return true;
9191 return false;
9192
9193 case RECORD_TYPE:
9194 if (TYPE_PTRMEMFUNC_P (type))
9195 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9196 return false;
9197
9198 default:
9199 return false;
9200 }
9201}
9202
3f2d352c 9203/* Returns true if to and from are (possibly multi-level) pointers to the same
d11ad92e
MS
9204 type or inheritance-related types, regardless of cv-quals. */
9205
3f2d352c 9206bool
58f9752a 9207ptr_reasonably_similar (const_tree to, const_tree from)
d11ad92e
MS
9208{
9209 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9210 {
72a08131 9211 /* Any target type is similar enough to void. */
50e10fa8 9212 if (VOID_TYPE_P (to))
d4684c00 9213 return !error_type_p (from);
50e10fa8 9214 if (VOID_TYPE_P (from))
d4684c00 9215 return !error_type_p (to);
72a08131 9216
d11ad92e 9217 if (TREE_CODE (to) != TREE_CODE (from))
3f2d352c 9218 return false;
d11ad92e
MS
9219
9220 if (TREE_CODE (from) == OFFSET_TYPE
96d84882
PB
9221 && comptypes (TYPE_OFFSET_BASETYPE (to),
9222 TYPE_OFFSET_BASETYPE (from),
9223 COMPARE_BASE | COMPARE_DERIVED))
d11ad92e
MS
9224 continue;
9225
b55b02ea 9226 if (VECTOR_TYPE_P (to)
00c8e9f6 9227 && vector_types_convertible_p (to, from, false))
3f2d352c 9228 return true;
d70b8c3a 9229
8a2b77e7
JM
9230 if (TREE_CODE (to) == INTEGER_TYPE
9231 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
3f2d352c 9232 return true;
8a2b77e7
JM
9233
9234 if (TREE_CODE (to) == FUNCTION_TYPE)
d4684c00 9235 return !error_type_p (to) && !error_type_p (from);
8a2b77e7 9236
50e10fa8 9237 if (!TYPE_PTR_P (to))
3f2d352c
PC
9238 {
9239 /* When either type is incomplete avoid DERIVED_FROM_P,
9240 which may call complete_type (c++/57942). */
9241 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9242 return comptypes
9243 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9244 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9245 }
d11ad92e
MS
9246 }
9247}
c11b6f21 9248
34b5375f
MM
9249/* Return true if TO and FROM (both of which are POINTER_TYPEs or
9250 pointer-to-member types) are the same, ignoring cv-qualification at
9251 all levels. */
c11b6f21 9252
34b5375f 9253bool
acd8e2d0 9254comp_ptr_ttypes_const (tree to, tree from)
c11b6f21 9255{
f83c7f63
DJ
9256 bool is_opaque_pointer = false;
9257
c11b6f21
MS
9258 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9259 {
9260 if (TREE_CODE (to) != TREE_CODE (from))
34b5375f 9261 return false;
c11b6f21
MS
9262
9263 if (TREE_CODE (from) == OFFSET_TYPE
3bfdc719
MM
9264 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9265 TYPE_OFFSET_BASETYPE (to)))
c11b6f21
MS
9266 continue;
9267
b55b02ea 9268 if (VECTOR_TYPE_P (to))
f83c7f63
DJ
9269 is_opaque_pointer = vector_targets_convertible_p (to, from);
9270
50e10fa8 9271 if (!TYPE_PTR_P (to))
f83c7f63
DJ
9272 return (is_opaque_pointer
9273 || same_type_ignoring_top_level_qualifiers_p (to, from));
c11b6f21
MS
9274 }
9275}
9276
89d684bb
BM
9277/* Returns the type qualifiers for this type, including the qualifiers on the
9278 elements for an array type. */
27778b73
MM
9279
9280int
ac7d7749 9281cp_type_quals (const_tree type)
27778b73 9282{
2872152c 9283 int quals;
4e9b57fa 9284 /* This CONST_CAST is okay because strip_array_types returns its
75547801 9285 argument unmodified and we assign it to a const_tree. */
2872152c
JM
9286 type = strip_array_types (CONST_CAST_TREE (type));
9287 if (type == error_mark_node
9288 /* Quals on a FUNCTION_TYPE are memfn quals. */
9289 || TREE_CODE (type) == FUNCTION_TYPE)
328de7c2 9290 return TYPE_UNQUALIFIED;
2872152c
JM
9291 quals = TYPE_QUALS (type);
9292 /* METHOD and REFERENCE_TYPEs should never have quals. */
9293 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9294 && TREE_CODE (type) != REFERENCE_TYPE)
9295 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9296 == TYPE_UNQUALIFIED));
9297 return quals;
9298}
9299
2eed8e37
BK
9300/* Returns the function-ref-qualifier for TYPE */
9301
9302cp_ref_qualifier
9303type_memfn_rqual (const_tree type)
9304{
9305 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9306 || TREE_CODE (type) == METHOD_TYPE);
9307
9308 if (!FUNCTION_REF_QUALIFIED (type))
9309 return REF_QUAL_NONE;
9310 else if (FUNCTION_RVALUE_QUALIFIED (type))
9311 return REF_QUAL_RVALUE;
9312 else
9313 return REF_QUAL_LVALUE;
9314}
9315
2872152c
JM
9316/* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9317 METHOD_TYPE. */
9318
9319int
9320type_memfn_quals (const_tree type)
9321{
9322 if (TREE_CODE (type) == FUNCTION_TYPE)
9323 return TYPE_QUALS (type);
9324 else if (TREE_CODE (type) == METHOD_TYPE)
7e1352fe 9325 return cp_type_quals (class_of_this_parm (type));
2872152c
JM
9326 else
9327 gcc_unreachable ();
9328}
9329
9330/* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
2eed8e37 9331 MEMFN_QUALS and its ref-qualifier to RQUAL. */
2872152c
JM
9332
9333tree
2eed8e37 9334apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
2872152c
JM
9335{
9336 /* Could handle METHOD_TYPE here if necessary. */
9337 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
2eed8e37
BK
9338 if (TYPE_QUALS (type) == memfn_quals
9339 && type_memfn_rqual (type) == rqual)
2872152c 9340 return type;
2eed8e37 9341
2872152c
JM
9342 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9343 complex. */
2eed8e37 9344 tree result = build_qualified_type (type, memfn_quals);
2eed8e37 9345 return build_ref_qualified_type (result, rqual);
91063b51 9346}
a7a7710d 9347
36c37128
JM
9348/* Returns nonzero if TYPE is const or volatile. */
9349
9350bool
9351cv_qualified_p (const_tree type)
9352{
9353 int quals = cp_type_quals (type);
9354 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9355}
9356
f4f206f4 9357/* Returns nonzero if the TYPE contains a mutable member. */
a7a7710d 9358
acd8e2d0 9359bool
58f9752a 9360cp_has_mutable_p (const_tree type)
a7a7710d 9361{
4e9b57fa 9362 /* This CONST_CAST is okay because strip_array_types returns its
75547801 9363 argument unmodified and we assign it to a const_tree. */
b1d5455a 9364 type = strip_array_types (CONST_CAST_TREE(type));
a7a7710d
NS
9365
9366 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9367}
af7b9902 9368
15896502
MM
9369/* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9370 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9371 approximation. In particular, consider:
9372
9373 int f();
9374 struct S { int i; };
9375 const S s = { f(); }
9376
9377 Here, we will make "s" as TREE_READONLY (because it is declared
9378 "const") -- only to reverse ourselves upon seeing that the
9379 initializer is non-constant. */
9380
9804209d
DG
9381void
9382cp_apply_type_quals_to_decl (int type_quals, tree decl)
9383{
9384 tree type = TREE_TYPE (decl);
9385
9386 if (type == error_mark_node)
9387 return;
9388
0d9c0892
JM
9389 if (TREE_CODE (decl) == TYPE_DECL)
9390 return;
9391
93e1ddcf
JM
9392 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9393 && type_quals != TYPE_UNQUALIFIED));
9804209d 9394
67935995 9395 /* Avoid setting TREE_READONLY incorrectly. */
329af3c7
JM
9396 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9397 constructor can produce constant init, so rely on cp_finish_decl to
9398 clear TREE_READONLY if the variable has non-constant init. */
859f6cb4 9399
6d876e0b
JM
9400 /* If the type has (or might have) a mutable component, that component
9401 might be modified. */
9402 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
67935995
MM
9403 type_quals &= ~TYPE_QUAL_CONST;
9404
9804209d
DG
9405 c_apply_type_quals_to_decl (type_quals, decl);
9406}
9407
af7b9902 9408/* Subroutine of casts_away_constness. Make T1 and T2 point at
33c25e5c 9409 exemplar types such that casting T1 to T2 is casting away constness
af7b9902
MM
9410 if and only if there is no implicit conversion from T1 to T2. */
9411
9412static void
b40e334f 9413casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
af7b9902
MM
9414{
9415 int quals1;
9416 int quals2;
9417
9418 /* [expr.const.cast]
9419
9420 For multi-level pointer to members and multi-level mixed pointers
9421 and pointers to members (conv.qual), the "member" aspect of a
9422 pointer to member level is ignored when determining if a const
9423 cv-qualifier has been cast away. */
af7b9902
MM
9424 /* [expr.const.cast]
9425
9426 For two pointer types:
9427
0cbd7506
MS
9428 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9429 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9430 K is min(N,M)
af7b9902
MM
9431
9432 casting from X1 to X2 casts away constness if, for a non-pointer
9433 type T there does not exist an implicit conversion (clause
9434 _conv_) from:
9435
0cbd7506 9436 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
c8094d83 9437
af7b9902
MM
9438 to
9439
0cbd7506 9440 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
66b1156a
PC
9441 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9442 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
af7b9902
MM
9443 {
9444 *t1 = cp_build_qualified_type (void_type_node,
89d684bb 9445 cp_type_quals (*t1));
af7b9902 9446 *t2 = cp_build_qualified_type (void_type_node,
89d684bb 9447 cp_type_quals (*t2));
af7b9902
MM
9448 return;
9449 }
c8094d83 9450
89d684bb
BM
9451 quals1 = cp_type_quals (*t1);
9452 quals2 = cp_type_quals (*t2);
dad732fa 9453
66b1156a 9454 if (TYPE_PTRDATAMEM_P (*t1))
dad732fa
MM
9455 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9456 else
9457 *t1 = TREE_TYPE (*t1);
66b1156a 9458 if (TYPE_PTRDATAMEM_P (*t2))
dad732fa
MM
9459 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9460 else
9461 *t2 = TREE_TYPE (*t2);
9462
b40e334f 9463 casts_away_constness_r (t1, t2, complain);
af7b9902
MM
9464 *t1 = build_pointer_type (*t1);
9465 *t2 = build_pointer_type (*t2);
9466 *t1 = cp_build_qualified_type (*t1, quals1);
9467 *t2 = cp_build_qualified_type (*t2, quals2);
9468}
9469
838dfd8a 9470/* Returns nonzero if casting from TYPE1 to TYPE2 casts away
36ef7262
MLI
9471 constness.
9472
9473 ??? This function returns non-zero if casting away qualifiers not
9474 just const. We would like to return to the caller exactly which
9475 qualifiers are casted away to give more accurate diagnostics.
9476*/
af7b9902 9477
acd8e2d0 9478static bool
b40e334f 9479casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
af7b9902
MM
9480{
9481 if (TREE_CODE (t2) == REFERENCE_TYPE)
9482 {
9483 /* [expr.const.cast]
c8094d83 9484
af7b9902
MM
9485 Casting from an lvalue of type T1 to an lvalue of type T2
9486 using a reference cast casts away constness if a cast from an
9487 rvalue of type "pointer to T1" to the type "pointer to T2"
9488 casts away constness. */
acd8e2d0 9489 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
af7b9902 9490 return casts_away_constness (build_pointer_type (t1),
b40e334f
PC
9491 build_pointer_type (TREE_TYPE (t2)),
9492 complain);
af7b9902
MM
9493 }
9494
66b1156a 9495 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
af7b9902 9496 /* [expr.const.cast]
c8094d83 9497
af7b9902
MM
9498 Casting from an rvalue of type "pointer to data member of X
9499 of type T1" to the type "pointer to data member of Y of type
9500 T2" casts away constness if a cast from an rvalue of type
aba649ba 9501 "pointer to T1" to the type "pointer to T2" casts away
af7b9902 9502 constness. */
3e0b4710 9503 return casts_away_constness
a5ac359a 9504 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
b40e334f
PC
9505 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9506 complain);
af7b9902
MM
9507
9508 /* Casting away constness is only something that makes sense for
9509 pointer or reference types. */
50e10fa8 9510 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
acd8e2d0 9511 return false;
af7b9902
MM
9512
9513 /* Top-level qualifiers don't matter. */
9514 t1 = TYPE_MAIN_VARIANT (t1);
9515 t2 = TYPE_MAIN_VARIANT (t2);
b40e334f
PC
9516 casts_away_constness_r (&t1, &t2, complain);
9517 if (!can_convert (t2, t1, complain))
acd8e2d0 9518 return true;
af7b9902 9519
acd8e2d0 9520 return false;
af7b9902 9521}
6816f040 9522
ee76b931
MM
9523/* If T is a REFERENCE_TYPE return the type to which T refers.
9524 Otherwise, return T itself. */
9525
9526tree
9527non_reference (tree t)
9528{
c5ce25ce 9529 if (t && TREE_CODE (t) == REFERENCE_TYPE)
ee76b931
MM
9530 t = TREE_TYPE (t);
9531 return t;
9532}
37dc0d8d
JM
9533
9534
9535/* Return nonzero if REF is an lvalue valid for this language;
9536 otherwise, print an error message and return zero. USE says
9537 how the lvalue is being used and so selects the error message. */
9538
9539int
4af9e878 9540lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
37dc0d8d 9541{
4e9ca9b0 9542 cp_lvalue_kind kind = lvalue_kind (ref);
37dc0d8d 9543
4e9ca9b0
JM
9544 if (kind == clk_none)
9545 {
9546 if (complain & tf_error)
7bd11157 9547 lvalue_error (input_location, use);
4e9ca9b0
JM
9548 return 0;
9549 }
9550 else if (kind & (clk_rvalueref|clk_class))
9551 {
9552 if (!(complain & tf_error))
9553 return 0;
9554 if (kind & clk_class)
9555 /* Make this a permerror because we used to accept it. */
9556 permerror (input_location, "using temporary as lvalue");
9557 else
9558 error ("using xvalue (rvalue reference) as lvalue");
9559 }
9560 return 1;
37dc0d8d 9561}
03a904b5 9562
3ce4f9e4
ESR
9563/* Return true if a user-defined literal operator is a raw operator. */
9564
9565bool
9566check_raw_literal_operator (const_tree decl)
9567{
9568 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9569 tree argtype;
9570 int arity;
9571 bool maybe_raw_p = false;
9572
9573 /* Count the number and type of arguments and check for ellipsis. */
9574 for (argtype = argtypes, arity = 0;
9575 argtype && argtype != void_list_node;
9576 ++arity, argtype = TREE_CHAIN (argtype))
9577 {
9578 tree t = TREE_VALUE (argtype);
9579
9580 if (same_type_p (t, const_string_type_node))
9581 maybe_raw_p = true;
9582 }
9583 if (!argtype)
9584 return false; /* Found ellipsis. */
9585
9586 if (!maybe_raw_p || arity != 1)
9587 return false;
9588
9589 return true;
9590}
9591
9592
9593/* Return true if a user-defined literal operator has one of the allowed
9594 argument types. */
9595
9596bool
9597check_literal_operator_args (const_tree decl,
9598 bool *long_long_unsigned_p, bool *long_double_p)
9599{
9600 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
6e887223
JJ
9601
9602 *long_long_unsigned_p = false;
9603 *long_double_p = false;
d2027d2a
ESR
9604 if (processing_template_decl || processing_specialization)
9605 return argtypes == void_list_node;
3ce4f9e4
ESR
9606 else
9607 {
9608 tree argtype;
9609 int arity;
9610 int max_arity = 2;
3ce4f9e4 9611
3ce4f9e4
ESR
9612 /* Count the number and type of arguments and check for ellipsis. */
9613 for (argtype = argtypes, arity = 0;
9614 argtype && argtype != void_list_node;
9615 argtype = TREE_CHAIN (argtype))
9616 {
9617 tree t = TREE_VALUE (argtype);
9618 ++arity;
9619
50e10fa8 9620 if (TYPE_PTR_P (t))
3ce4f9e4 9621 {
f05eec4a 9622 bool maybe_raw_p = false;
24847495
JM
9623 t = TREE_TYPE (t);
9624 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9625 return false;
9626 t = TYPE_MAIN_VARIANT (t);
f05eec4a
ESR
9627 if ((maybe_raw_p = same_type_p (t, char_type_node))
9628 || same_type_p (t, wchar_type_node)
9629 || same_type_p (t, char16_type_node)
9630 || same_type_p (t, char32_type_node))
24847495 9631 {
f05eec4a
ESR
9632 argtype = TREE_CHAIN (argtype);
9633 if (!argtype)
9634 return false;
9635 t = TREE_VALUE (argtype);
9636 if (maybe_raw_p && argtype == void_list_node)
9637 return true;
9638 else if (same_type_p (t, size_type_node))
9639 {
9640 ++arity;
9641 continue;
9642 }
9643 else
9644 return false;
24847495 9645 }
3ce4f9e4
ESR
9646 }
9647 else if (same_type_p (t, long_long_unsigned_type_node))
9648 {
9649 max_arity = 1;
9650 *long_long_unsigned_p = true;
9651 }
9652 else if (same_type_p (t, long_double_type_node))
9653 {
9654 max_arity = 1;
9655 *long_double_p = true;
9656 }
9657 else if (same_type_p (t, char_type_node))
9658 max_arity = 1;
9659 else if (same_type_p (t, wchar_type_node))
9660 max_arity = 1;
9661 else if (same_type_p (t, char16_type_node))
9662 max_arity = 1;
9663 else if (same_type_p (t, char32_type_node))
9664 max_arity = 1;
9665 else
9666 return false;
9667 }
9668 if (!argtype)
9669 return false; /* Found ellipsis. */
9670
d2027d2a 9671 if (arity != max_arity)
3ce4f9e4
ESR
9672 return false;
9673
3ce4f9e4
ESR
9674 return true;
9675 }
9676}
1807ffc1
MS
9677
9678/* Always returns false since unlike C90, C++ has no concept of implicit
9679 function declarations. */
9680
9681bool
9682c_decl_implicit (const_tree)
9683{
9684 return false;
9685}