Update gcc-50 to SVN version 221572
[dragonfly.git] / contrib / gcc-5.0 / gcc / cp / typeck.c
CommitLineData
dda118e3
JM
1/* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
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
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
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
25 checks, and some optimization. */
26
27#include "config.h"
28#include "system.h"
29#include "coretypes.h"
30#include "tm.h"
31#include "hash-set.h"
32#include "machmode.h"
33#include "vec.h"
34#include "double-int.h"
35#include "input.h"
36#include "alias.h"
37#include "symtab.h"
38#include "wide-int.h"
39#include "inchash.h"
40#include "tree.h"
41#include "fold-const.h"
42#include "stor-layout.h"
43#include "varasm.h"
44#include "cp-tree.h"
45#include "flags.h"
46#include "diagnostic.h"
47#include "intl.h"
48#include "target.h"
49#include "convert.h"
50#include "c-family/c-common.h"
51#include "c-family/c-objc.h"
52#include "c-family/c-ubsan.h"
53#include "params.h"
54
55static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
56static tree cp_build_function_call (tree, tree, tsubst_flags_t);
57static tree pfn_from_ptrmemfunc (tree);
58static tree delta_from_ptrmemfunc (tree);
59static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
60 tsubst_flags_t, int);
61static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
62static tree rationalize_conditional_expr (enum tree_code, tree,
63 tsubst_flags_t);
64static int comp_ptr_ttypes_real (tree, tree, int);
65static bool comp_except_types (tree, tree, bool);
66static bool comp_array_types (const_tree, const_tree, bool);
67static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
68static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
69static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
70static bool casts_away_constness (tree, tree, tsubst_flags_t);
71static bool maybe_warn_about_returning_address_of_local (tree);
72static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
73static void warn_args_num (location_t, tree, bool);
74static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
75 tsubst_flags_t);
76
77/* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.)
79 Returns error_mark_node if the VALUE does not have
80 complete type when this function returns. */
81
82tree
83require_complete_type_sfinae (tree value, tsubst_flags_t complain)
84{
85 tree type;
86
87 if (processing_template_decl || value == error_mark_node)
88 return value;
89
90 if (TREE_CODE (value) == OVERLOAD)
91 type = unknown_type_node;
92 else
93 type = TREE_TYPE (value);
94
95 if (type == error_mark_node)
96 return error_mark_node;
97
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
100 return value;
101
102 if (complete_type_or_maybe_complain (type, value, complain))
103 return value;
104 else
105 return error_mark_node;
106}
107
108tree
109require_complete_type (tree value)
110{
111 return require_complete_type_sfinae (value, tf_warning_or_error);
112}
113
114/* Try to complete TYPE, if it is incomplete. For example, if TYPE is
115 a template instantiation, do the instantiation. Returns TYPE,
116 whether or not it could be completed, unless something goes
117 horribly wrong, in which case the error_mark_node is returned. */
118
119tree
120complete_type (tree type)
121{
122 if (type == NULL_TREE)
123 /* Rather than crash, we return something sure to cause an error
124 at some point. */
125 return error_mark_node;
126
127 if (type == error_mark_node || COMPLETE_TYPE_P (type))
128 ;
129 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
130 {
131 tree t = complete_type (TREE_TYPE (type));
132 unsigned int needs_constructing, has_nontrivial_dtor;
133 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
134 layout_type (type);
135 needs_constructing
136 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
137 has_nontrivial_dtor
138 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
139 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
140 {
141 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
142 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
143 }
144 }
145 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
146 instantiate_class_template (TYPE_MAIN_VARIANT (type));
147
148 return type;
149}
150
151/* Like complete_type, but issue an error if the TYPE cannot be completed.
152 VALUE is used for informative diagnostics.
153 Returns NULL_TREE if the type cannot be made complete. */
154
155tree
156complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
157{
158 type = complete_type (type);
159 if (type == error_mark_node)
160 /* We already issued an error. */
161 return NULL_TREE;
162 else if (!COMPLETE_TYPE_P (type))
163 {
164 if (complain & tf_error)
165 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
166 return NULL_TREE;
167 }
168 else
169 return type;
170}
171
172tree
173complete_type_or_else (tree type, tree value)
174{
175 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
176}
177
178/* Return truthvalue of whether type of EXP is instantiated. */
179
180int
181type_unknown_p (const_tree exp)
182{
183 return (TREE_CODE (exp) == TREE_LIST
184 || TREE_TYPE (exp) == unknown_type_node);
185}
186
187\f
188/* Return the common type of two parameter lists.
189 We assume that comptypes has already been done and returned 1;
190 if that isn't so, this may crash.
191
192 As an optimization, free the space we allocate if the parameter
193 lists are already common. */
194
195static tree
196commonparms (tree p1, tree p2)
197{
198 tree oldargs = p1, newargs, n;
199 int i, len;
200 int any_change = 0;
201
202 len = list_length (p1);
203 newargs = tree_last (p1);
204
205 if (newargs == void_list_node)
206 i = 1;
207 else
208 {
209 i = 0;
210 newargs = 0;
211 }
212
213 for (; i < len; i++)
214 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
215
216 n = newargs;
217
218 for (i = 0; p1;
219 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
220 {
221 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
222 {
223 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
224 any_change = 1;
225 }
226 else if (! TREE_PURPOSE (p1))
227 {
228 if (TREE_PURPOSE (p2))
229 {
230 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
231 any_change = 1;
232 }
233 }
234 else
235 {
236 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
237 any_change = 1;
238 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
239 }
240 if (TREE_VALUE (p1) != TREE_VALUE (p2))
241 {
242 any_change = 1;
243 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
244 }
245 else
246 TREE_VALUE (n) = TREE_VALUE (p1);
247 }
248 if (! any_change)
249 return oldargs;
250
251 return newargs;
252}
253
254/* Given a type, perhaps copied for a typedef,
255 find the "original" version of it. */
256static tree
257original_type (tree t)
258{
259 int quals = cp_type_quals (t);
260 while (t != error_mark_node
261 && TYPE_NAME (t) != NULL_TREE)
262 {
263 tree x = TYPE_NAME (t);
264 if (TREE_CODE (x) != TYPE_DECL)
265 break;
266 x = DECL_ORIGINAL_TYPE (x);
267 if (x == NULL_TREE)
268 break;
269 t = x;
270 }
271 return cp_build_qualified_type (t, quals);
272}
273
274/* Return the common type for two arithmetic types T1 and T2 under the
275 usual arithmetic conversions. The default conversions have already
276 been applied, and enumerated types converted to their compatible
277 integer types. */
278
279static tree
280cp_common_type (tree t1, tree t2)
281{
282 enum tree_code code1 = TREE_CODE (t1);
283 enum tree_code code2 = TREE_CODE (t2);
284 tree attributes;
285 int i;
286
287
288 /* In what follows, we slightly generalize the rules given in [expr] so
289 as to deal with `long long' and `complex'. First, merge the
290 attributes. */
291 attributes = (*targetm.merge_type_attributes) (t1, t2);
292
293 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
294 {
295 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
296 return build_type_attribute_variant (t1, attributes);
297 else
298 return NULL_TREE;
299 }
300
301 /* FIXME: Attributes. */
302 gcc_assert (ARITHMETIC_TYPE_P (t1)
303 || TREE_CODE (t1) == VECTOR_TYPE
304 || UNSCOPED_ENUM_P (t1));
305 gcc_assert (ARITHMETIC_TYPE_P (t2)
306 || TREE_CODE (t2) == VECTOR_TYPE
307 || UNSCOPED_ENUM_P (t2));
308
309 /* If one type is complex, form the common type of the non-complex
310 components, then make that complex. Use T1 or T2 if it is the
311 required type. */
312 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
313 {
314 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
315 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
316 tree subtype
317 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
318
319 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
320 return build_type_attribute_variant (t1, attributes);
321 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
322 return build_type_attribute_variant (t2, attributes);
323 else
324 return build_type_attribute_variant (build_complex_type (subtype),
325 attributes);
326 }
327
328 if (code1 == VECTOR_TYPE)
329 {
330 /* When we get here we should have two vectors of the same size.
331 Just prefer the unsigned one if present. */
332 if (TYPE_UNSIGNED (t1))
333 return build_type_attribute_variant (t1, attributes);
334 else
335 return build_type_attribute_variant (t2, attributes);
336 }
337
338 /* If only one is real, use it as the result. */
339 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
340 return build_type_attribute_variant (t1, attributes);
341 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
342 return build_type_attribute_variant (t2, attributes);
343
344 /* Both real or both integers; use the one with greater precision. */
345 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
346 return build_type_attribute_variant (t1, attributes);
347 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
348 return build_type_attribute_variant (t2, attributes);
349
350 /* The types are the same; no need to do anything fancy. */
351 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
352 return build_type_attribute_variant (t1, attributes);
353
354 if (code1 != REAL_TYPE)
355 {
356 /* If one is unsigned long long, then convert the other to unsigned
357 long long. */
358 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
359 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
360 return build_type_attribute_variant (long_long_unsigned_type_node,
361 attributes);
362 /* If one is a long long, and the other is an unsigned long, and
363 long long can represent all the values of an unsigned long, then
364 convert to a long long. Otherwise, convert to an unsigned long
365 long. Otherwise, if either operand is long long, convert the
366 other to long long.
367
368 Since we're here, we know the TYPE_PRECISION is the same;
369 therefore converting to long long cannot represent all the values
370 of an unsigned long, so we choose unsigned long long in that
371 case. */
372 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
373 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
374 {
375 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
376 ? long_long_unsigned_type_node
377 : long_long_integer_type_node);
378 return build_type_attribute_variant (t, attributes);
379 }
380
381 /* Go through the same procedure, but for longs. */
382 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
384 return build_type_attribute_variant (long_unsigned_type_node,
385 attributes);
386 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
388 {
389 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
390 ? long_unsigned_type_node : long_integer_type_node);
391 return build_type_attribute_variant (t, attributes);
392 }
393
394 /* For __intN types, either the type is __int128 (and is lower
395 priority than the types checked above, but higher than other
396 128-bit types) or it's known to not be the same size as other
397 types (enforced in toplev.c). Prefer the unsigned type. */
398 for (i = 0; i < NUM_INT_N_ENTS; i ++)
399 {
400 if (int_n_enabled_p [i]
401 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
402 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
403 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
405 {
406 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
407 ? int_n_trees[i].unsigned_type
408 : int_n_trees[i].signed_type);
409 return build_type_attribute_variant (t, attributes);
410 }
411 }
412
413 /* Otherwise prefer the unsigned one. */
414 if (TYPE_UNSIGNED (t1))
415 return build_type_attribute_variant (t1, attributes);
416 else
417 return build_type_attribute_variant (t2, attributes);
418 }
419 else
420 {
421 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
422 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
423 return build_type_attribute_variant (long_double_type_node,
424 attributes);
425 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
426 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
427 return build_type_attribute_variant (double_type_node,
428 attributes);
429 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
430 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
431 return build_type_attribute_variant (float_type_node,
432 attributes);
433
434 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
435 the standard C++ floating-point types. Logic earlier in this
436 function has already eliminated the possibility that
437 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
438 compelling reason to choose one or the other. */
439 return build_type_attribute_variant (t1, attributes);
440 }
441}
442
443/* T1 and T2 are arithmetic or enumeration types. Return the type
444 that will result from the "usual arithmetic conversions" on T1 and
445 T2 as described in [expr]. */
446
447tree
448type_after_usual_arithmetic_conversions (tree t1, tree t2)
449{
450 gcc_assert (ARITHMETIC_TYPE_P (t1)
451 || TREE_CODE (t1) == VECTOR_TYPE
452 || UNSCOPED_ENUM_P (t1));
453 gcc_assert (ARITHMETIC_TYPE_P (t2)
454 || TREE_CODE (t2) == VECTOR_TYPE
455 || UNSCOPED_ENUM_P (t2));
456
457 /* Perform the integral promotions. We do not promote real types here. */
458 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
459 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
460 {
461 t1 = type_promotes_to (t1);
462 t2 = type_promotes_to (t2);
463 }
464
465 return cp_common_type (t1, t2);
466}
467
468static void
469composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
470 composite_pointer_operation operation)
471{
472 switch (operation)
473 {
474 case CPO_COMPARISON:
475 emit_diagnostic (kind, input_location, 0,
476 "comparison between "
477 "distinct pointer types %qT and %qT lacks a cast",
478 t1, t2);
479 break;
480 case CPO_CONVERSION:
481 emit_diagnostic (kind, input_location, 0,
482 "conversion between "
483 "distinct pointer types %qT and %qT lacks a cast",
484 t1, t2);
485 break;
486 case CPO_CONDITIONAL_EXPR:
487 emit_diagnostic (kind, input_location, 0,
488 "conditional expression between "
489 "distinct pointer types %qT and %qT lacks a cast",
490 t1, t2);
491 break;
492 default:
493 gcc_unreachable ();
494 }
495}
496
497/* Subroutine of composite_pointer_type to implement the recursive
498 case. See that function for documentation of the parameters. */
499
500static tree
501composite_pointer_type_r (tree t1, tree t2,
502 composite_pointer_operation operation,
503 tsubst_flags_t complain)
504{
505 tree pointee1;
506 tree pointee2;
507 tree result_type;
508 tree attributes;
509
510 /* Determine the types pointed to by T1 and T2. */
511 if (TYPE_PTR_P (t1))
512 {
513 pointee1 = TREE_TYPE (t1);
514 pointee2 = TREE_TYPE (t2);
515 }
516 else
517 {
518 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
519 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
520 }
521
522 /* [expr.rel]
523
524 Otherwise, the composite pointer type is a pointer type
525 similar (_conv.qual_) to the type of one of the operands,
526 with a cv-qualification signature (_conv.qual_) that is the
527 union of the cv-qualification signatures of the operand
528 types. */
529 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
530 result_type = pointee1;
531 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
532 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
533 {
534 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
535 complain);
536 if (result_type == error_mark_node)
537 return error_mark_node;
538 }
539 else
540 {
541 if (complain & tf_error)
542 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
543 else
544 return error_mark_node;
545 result_type = void_type_node;
546 }
547 result_type = cp_build_qualified_type (result_type,
548 (cp_type_quals (pointee1)
549 | cp_type_quals (pointee2)));
550 /* If the original types were pointers to members, so is the
551 result. */
552 if (TYPE_PTRMEM_P (t1))
553 {
554 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
555 TYPE_PTRMEM_CLASS_TYPE (t2)))
556 {
557 if (complain & tf_error)
558 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
559 else
560 return error_mark_node;
561 }
562 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
563 result_type);
564 }
565 else
566 result_type = build_pointer_type (result_type);
567
568 /* Merge the attributes. */
569 attributes = (*targetm.merge_type_attributes) (t1, t2);
570 return build_type_attribute_variant (result_type, attributes);
571}
572
573/* Return the composite pointer type (see [expr.rel]) for T1 and T2.
574 ARG1 and ARG2 are the values with those types. The OPERATION is to
575 describe the operation between the pointer types,
576 in case an error occurs.
577
578 This routine also implements the computation of a common type for
579 pointers-to-members as per [expr.eq]. */
580
581tree
582composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
583 composite_pointer_operation operation,
584 tsubst_flags_t complain)
585{
586 tree class1;
587 tree class2;
588
589 /* [expr.rel]
590
591 If one operand is a null pointer constant, the composite pointer
592 type is the type of the other operand. */
593 if (null_ptr_cst_p (arg1))
594 return t2;
595 if (null_ptr_cst_p (arg2))
596 return t1;
597
598 /* We have:
599
600 [expr.rel]
601
602 If one of the operands has type "pointer to cv1 void*", then
603 the other has type "pointer to cv2T", and the composite pointer
604 type is "pointer to cv12 void", where cv12 is the union of cv1
605 and cv2.
606
607 If either type is a pointer to void, make sure it is T1. */
608 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
609 {
610 tree t;
611 t = t1;
612 t1 = t2;
613 t2 = t;
614 }
615
616 /* Now, if T1 is a pointer to void, merge the qualifiers. */
617 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
618 {
619 tree attributes;
620 tree result_type;
621
622 if (TYPE_PTRFN_P (t2))
623 {
624 if (complain & tf_error)
625 {
626 switch (operation)
627 {
628 case CPO_COMPARISON:
629 pedwarn (input_location, OPT_Wpedantic,
630 "ISO C++ forbids comparison between pointer "
631 "of type %<void *%> and pointer-to-function");
632 break;
633 case CPO_CONVERSION:
634 pedwarn (input_location, OPT_Wpedantic,
635 "ISO C++ forbids conversion between pointer "
636 "of type %<void *%> and pointer-to-function");
637 break;
638 case CPO_CONDITIONAL_EXPR:
639 pedwarn (input_location, OPT_Wpedantic,
640 "ISO C++ forbids conditional expression between "
641 "pointer of type %<void *%> and "
642 "pointer-to-function");
643 break;
644 default:
645 gcc_unreachable ();
646 }
647 }
648 else
649 return error_mark_node;
650 }
651 result_type
652 = cp_build_qualified_type (void_type_node,
653 (cp_type_quals (TREE_TYPE (t1))
654 | cp_type_quals (TREE_TYPE (t2))));
655 result_type = build_pointer_type (result_type);
656 /* Merge the attributes. */
657 attributes = (*targetm.merge_type_attributes) (t1, t2);
658 return build_type_attribute_variant (result_type, attributes);
659 }
660
661 if (c_dialect_objc () && TYPE_PTR_P (t1)
662 && TYPE_PTR_P (t2))
663 {
664 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
665 return objc_common_type (t1, t2);
666 }
667
668 /* [expr.eq] permits the application of a pointer conversion to
669 bring the pointers to a common type. */
670 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
671 && CLASS_TYPE_P (TREE_TYPE (t1))
672 && CLASS_TYPE_P (TREE_TYPE (t2))
673 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
674 TREE_TYPE (t2)))
675 {
676 class1 = TREE_TYPE (t1);
677 class2 = TREE_TYPE (t2);
678
679 if (DERIVED_FROM_P (class1, class2))
680 t2 = (build_pointer_type
681 (cp_build_qualified_type (class1, cp_type_quals (class2))));
682 else if (DERIVED_FROM_P (class2, class1))
683 t1 = (build_pointer_type
684 (cp_build_qualified_type (class2, cp_type_quals (class1))));
685 else
686 {
687 if (complain & tf_error)
688 composite_pointer_error (DK_ERROR, t1, t2, operation);
689 return error_mark_node;
690 }
691 }
692 /* [expr.eq] permits the application of a pointer-to-member
693 conversion to change the class type of one of the types. */
694 else if (TYPE_PTRMEM_P (t1)
695 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
696 TYPE_PTRMEM_CLASS_TYPE (t2)))
697 {
698 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
699 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
700
701 if (DERIVED_FROM_P (class1, class2))
702 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
703 else if (DERIVED_FROM_P (class2, class1))
704 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
705 else
706 {
707 if (complain & tf_error)
708 switch (operation)
709 {
710 case CPO_COMPARISON:
711 error ("comparison between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
713 t1, t2);
714 break;
715 case CPO_CONVERSION:
716 error ("conversion between distinct "
717 "pointer-to-member types %qT and %qT lacks a cast",
718 t1, t2);
719 break;
720 case CPO_CONDITIONAL_EXPR:
721 error ("conditional expression between distinct "
722 "pointer-to-member types %qT and %qT lacks a cast",
723 t1, t2);
724 break;
725 default:
726 gcc_unreachable ();
727 }
728 return error_mark_node;
729 }
730 }
731
732 return composite_pointer_type_r (t1, t2, operation, complain);
733}
734
735/* Return the merged type of two types.
736 We assume that comptypes has already been done and returned 1;
737 if that isn't so, this may crash.
738
739 This just combines attributes and default arguments; any other
740 differences would cause the two types to compare unalike. */
741
742tree
743merge_types (tree t1, tree t2)
744{
745 enum tree_code code1;
746 enum tree_code code2;
747 tree attributes;
748
749 /* Save time if the two types are the same. */
750 if (t1 == t2)
751 return t1;
752 if (original_type (t1) == original_type (t2))
753 return t1;
754
755 /* If one type is nonsense, use the other. */
756 if (t1 == error_mark_node)
757 return t2;
758 if (t2 == error_mark_node)
759 return t1;
760
761 /* Handle merging an auto redeclaration with a previous deduced
762 return type. */
763 if (is_auto (t1))
764 return t2;
765
766 /* Merge the attributes. */
767 attributes = (*targetm.merge_type_attributes) (t1, t2);
768
769 if (TYPE_PTRMEMFUNC_P (t1))
770 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
771 if (TYPE_PTRMEMFUNC_P (t2))
772 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
773
774 code1 = TREE_CODE (t1);
775 code2 = TREE_CODE (t2);
776 if (code1 != code2)
777 {
778 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
779 if (code1 == TYPENAME_TYPE)
780 {
781 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
782 code1 = TREE_CODE (t1);
783 }
784 else
785 {
786 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
787 code2 = TREE_CODE (t2);
788 }
789 }
790
791 switch (code1)
792 {
793 case POINTER_TYPE:
794 case REFERENCE_TYPE:
795 /* For two pointers, do this recursively on the target type. */
796 {
797 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798 int quals = cp_type_quals (t1);
799
800 if (code1 == POINTER_TYPE)
801 t1 = build_pointer_type (target);
802 else
803 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
804 t1 = build_type_attribute_variant (t1, attributes);
805 t1 = cp_build_qualified_type (t1, quals);
806
807 if (TREE_CODE (target) == METHOD_TYPE)
808 t1 = build_ptrmemfunc_type (t1);
809
810 return t1;
811 }
812
813 case OFFSET_TYPE:
814 {
815 int quals;
816 tree pointee;
817 quals = cp_type_quals (t1);
818 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
819 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
820 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
821 pointee);
822 t1 = cp_build_qualified_type (t1, quals);
823 break;
824 }
825
826 case ARRAY_TYPE:
827 {
828 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
829 /* Save space: see if the result is identical to one of the args. */
830 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
831 return build_type_attribute_variant (t1, attributes);
832 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
833 return build_type_attribute_variant (t2, attributes);
834 /* Merge the element types, and have a size if either arg has one. */
835 t1 = build_cplus_array_type
836 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
837 break;
838 }
839
840 case FUNCTION_TYPE:
841 /* Function types: prefer the one that specified arg types.
842 If both do, merge the arg types. Also merge the return types. */
843 {
844 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
845 tree p1 = TYPE_ARG_TYPES (t1);
846 tree p2 = TYPE_ARG_TYPES (t2);
847 tree parms;
848 tree rval, raises;
849 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
850
851 /* Save space: see if the result is identical to one of the args. */
852 if (valtype == TREE_TYPE (t1) && ! p2)
853 return cp_build_type_attribute_variant (t1, attributes);
854 if (valtype == TREE_TYPE (t2) && ! p1)
855 return cp_build_type_attribute_variant (t2, attributes);
856
857 /* Simple way if one arg fails to specify argument types. */
858 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
859 parms = p2;
860 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
861 parms = p1;
862 else
863 parms = commonparms (p1, p2);
864
865 rval = build_function_type (valtype, parms);
866 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
867 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
868 rval = apply_memfn_quals (rval,
869 type_memfn_quals (t1),
870 type_memfn_rqual (t1));
871 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
872 TYPE_RAISES_EXCEPTIONS (t2));
873 t1 = build_exception_variant (rval, raises);
874 if (late_return_type_p)
875 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
876 break;
877 }
878
879 case METHOD_TYPE:
880 {
881 /* Get this value the long way, since TYPE_METHOD_BASETYPE
882 is just the main variant of this. */
883 tree basetype = class_of_this_parm (t2);
884 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
885 TYPE_RAISES_EXCEPTIONS (t2));
886 cp_ref_qualifier rqual = type_memfn_rqual (t1);
887 tree t3;
888 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
889 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
890
891 /* If this was a member function type, get back to the
892 original type of type member function (i.e., without
893 the class instance variable up front. */
894 t1 = build_function_type (TREE_TYPE (t1),
895 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
896 t2 = build_function_type (TREE_TYPE (t2),
897 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
898 t3 = merge_types (t1, t2);
899 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
900 TYPE_ARG_TYPES (t3));
901 t1 = build_exception_variant (t3, raises);
902 t1 = build_ref_qualified_type (t1, rqual);
903 if (late_return_type_1_p)
904 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
905 if (late_return_type_2_p)
906 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
907 break;
908 }
909
910 case TYPENAME_TYPE:
911 /* There is no need to merge attributes into a TYPENAME_TYPE.
912 When the type is instantiated it will have whatever
913 attributes result from the instantiation. */
914 return t1;
915
916 default:;
917 }
918
919 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
920 return t1;
921 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
922 return t2;
923 else
924 return cp_build_type_attribute_variant (t1, attributes);
925}
926
927/* Return the ARRAY_TYPE type without its domain. */
928
929tree
930strip_array_domain (tree type)
931{
932 tree t2;
933 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
934 if (TYPE_DOMAIN (type) == NULL_TREE)
935 return type;
936 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
937 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
938}
939
940/* Wrapper around cp_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions.
942
943 Return the common type for two arithmetic types T1 and T2 under the
944 usual arithmetic conversions. The default conversions have already
945 been applied, and enumerated types converted to their compatible
946 integer types. */
947
948tree
949common_type (tree t1, tree t2)
950{
951 /* If one type is nonsense, use the other */
952 if (t1 == error_mark_node)
953 return t2;
954 if (t2 == error_mark_node)
955 return t1;
956
957 return cp_common_type (t1, t2);
958}
959
960/* Return the common type of two pointer types T1 and T2. This is the
961 type for the result of most arithmetic operations if the operands
962 have the given two types.
963
964 We assume that comp_target_types has already been done and returned
965 nonzero; if that isn't so, this may crash. */
966
967tree
968common_pointer_type (tree t1, tree t2)
969{
970 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
971 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
972 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
973
974 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
975 CPO_CONVERSION, tf_warning_or_error);
976}
977\f
978/* Compare two exception specifier types for exactness or subsetness, if
979 allowed. Returns false for mismatch, true for match (same, or
980 derived and !exact).
981
982 [except.spec] "If a class X ... objects of class X or any class publicly
983 and unambiguously derived from X. Similarly, if a pointer type Y * ...
984 exceptions of type Y * or that are pointers to any type publicly and
985 unambiguously derived from Y. Otherwise a function only allows exceptions
986 that have the same type ..."
987 This does not mention cv qualifiers and is different to what throw
988 [except.throw] and catch [except.catch] will do. They will ignore the
989 top level cv qualifiers, and allow qualifiers in the pointer to class
990 example.
991
992 We implement the letter of the standard. */
993
994static bool
995comp_except_types (tree a, tree b, bool exact)
996{
997 if (same_type_p (a, b))
998 return true;
999 else if (!exact)
1000 {
1001 if (cp_type_quals (a) || cp_type_quals (b))
1002 return false;
1003
1004 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1005 {
1006 a = TREE_TYPE (a);
1007 b = TREE_TYPE (b);
1008 if (cp_type_quals (a) || cp_type_quals (b))
1009 return false;
1010 }
1011
1012 if (TREE_CODE (a) != RECORD_TYPE
1013 || TREE_CODE (b) != RECORD_TYPE)
1014 return false;
1015
1016 if (publicly_uniquely_derived_p (a, b))
1017 return true;
1018 }
1019 return false;
1020}
1021
1022/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1023 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1024 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1025 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1026 are unordered, but we've already filtered out duplicates. Most lists will
1027 be in order, we should try to make use of that. */
1028
1029bool
1030comp_except_specs (const_tree t1, const_tree t2, int exact)
1031{
1032 const_tree probe;
1033 const_tree base;
1034 int length = 0;
1035
1036 if (t1 == t2)
1037 return true;
1038
1039 /* First handle noexcept. */
1040 if (exact < ce_exact)
1041 {
1042 /* noexcept(false) is compatible with no exception-specification,
1043 and stricter than any spec. */
1044 if (t1 == noexcept_false_spec)
1045 return t2 == NULL_TREE || exact == ce_derived;
1046 /* Even a derived noexcept(false) is compatible with no
1047 exception-specification. */
1048 if (t2 == noexcept_false_spec)
1049 return t1 == NULL_TREE;
1050
1051 /* Otherwise, if we aren't looking for an exact match, noexcept is
1052 equivalent to throw(). */
1053 if (t1 == noexcept_true_spec)
1054 t1 = empty_except_spec;
1055 if (t2 == noexcept_true_spec)
1056 t2 = empty_except_spec;
1057 }
1058
1059 /* If any noexcept is left, it is only comparable to itself;
1060 either we're looking for an exact match or we're redeclaring a
1061 template with dependent noexcept. */
1062 if ((t1 && TREE_PURPOSE (t1))
1063 || (t2 && TREE_PURPOSE (t2)))
1064 return (t1 && t2
1065 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1066
1067 if (t1 == NULL_TREE) /* T1 is ... */
1068 return t2 == NULL_TREE || exact == ce_derived;
1069 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1070 return t2 != NULL_TREE && !TREE_VALUE (t2);
1071 if (t2 == NULL_TREE) /* T2 is ... */
1072 return false;
1073 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1074 return exact == ce_derived;
1075
1076 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1077 Count how many we find, to determine exactness. For exact matching and
1078 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1079 O(nm). */
1080 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1081 {
1082 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1083 {
1084 tree a = TREE_VALUE (probe);
1085 tree b = TREE_VALUE (t2);
1086
1087 if (comp_except_types (a, b, exact))
1088 {
1089 if (probe == base && exact > ce_derived)
1090 base = TREE_CHAIN (probe);
1091 length++;
1092 break;
1093 }
1094 }
1095 if (probe == NULL_TREE)
1096 return false;
1097 }
1098 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1099}
1100
1101/* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1102 [] can match [size]. */
1103
1104static bool
1105comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1106{
1107 tree d1;
1108 tree d2;
1109 tree max1, max2;
1110
1111 if (t1 == t2)
1112 return true;
1113
1114 /* The type of the array elements must be the same. */
1115 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1116 return false;
1117
1118 d1 = TYPE_DOMAIN (t1);
1119 d2 = TYPE_DOMAIN (t2);
1120
1121 if (d1 == d2)
1122 return true;
1123
1124 /* If one of the arrays is dimensionless, and the other has a
1125 dimension, they are of different types. However, it is valid to
1126 write:
1127
1128 extern int a[];
1129 int a[3];
1130
1131 by [basic.link]:
1132
1133 declarations for an array object can specify
1134 array types that differ by the presence or absence of a major
1135 array bound (_dcl.array_). */
1136 if (!d1 || !d2)
1137 return allow_redeclaration;
1138
1139 /* Check that the dimensions are the same. */
1140
1141 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1142 return false;
1143 max1 = TYPE_MAX_VALUE (d1);
1144 max2 = TYPE_MAX_VALUE (d2);
1145
1146 if (!cp_tree_equal (max1, max2))
1147 return false;
1148
1149 return true;
1150}
1151
1152/* Compare the relative position of T1 and T2 into their respective
1153 template parameter list.
1154 T1 and T2 must be template parameter types.
1155 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1156
1157static bool
1158comp_template_parms_position (tree t1, tree t2)
1159{
1160 tree index1, index2;
1161 gcc_assert (t1 && t2
1162 && TREE_CODE (t1) == TREE_CODE (t2)
1163 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1164 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1165 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1166
1167 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1168 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1169
1170 /* Then compare their relative position. */
1171 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1172 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1173 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1174 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1175 return false;
1176
1177 /* In C++14 we can end up comparing 'auto' to a normal template
1178 parameter. Don't confuse them. */
1179 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1180 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1181
1182 return true;
1183}
1184
1185/* Subroutine in comptypes. */
1186
1187static bool
1188structural_comptypes (tree t1, tree t2, int strict)
1189{
1190 if (t1 == t2)
1191 return true;
1192
1193 /* Suppress errors caused by previously reported errors. */
1194 if (t1 == error_mark_node || t2 == error_mark_node)
1195 return false;
1196
1197 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1198
1199 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1200 current instantiation. */
1201 if (TREE_CODE (t1) == TYPENAME_TYPE)
1202 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1203
1204 if (TREE_CODE (t2) == TYPENAME_TYPE)
1205 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1206
1207 if (TYPE_PTRMEMFUNC_P (t1))
1208 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1209 if (TYPE_PTRMEMFUNC_P (t2))
1210 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1211
1212 /* Different classes of types can't be compatible. */
1213 if (TREE_CODE (t1) != TREE_CODE (t2))
1214 return false;
1215
1216 /* Qualifiers must match. For array types, we will check when we
1217 recur on the array element types. */
1218 if (TREE_CODE (t1) != ARRAY_TYPE
1219 && cp_type_quals (t1) != cp_type_quals (t2))
1220 return false;
1221 if (TREE_CODE (t1) == FUNCTION_TYPE
1222 && type_memfn_quals (t1) != type_memfn_quals (t2))
1223 return false;
1224 /* Need to check this before TYPE_MAIN_VARIANT.
1225 FIXME function qualifiers should really change the main variant. */
1226 if ((TREE_CODE (t1) == FUNCTION_TYPE
1227 || TREE_CODE (t1) == METHOD_TYPE)
1228 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1229 return false;
1230 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1231 return false;
1232
1233 /* Allow for two different type nodes which have essentially the same
1234 definition. Note that we already checked for equality of the type
1235 qualifiers (just above). */
1236
1237 if (TREE_CODE (t1) != ARRAY_TYPE
1238 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1239 return true;
1240
1241
1242 /* Compare the types. Break out if they could be the same. */
1243 switch (TREE_CODE (t1))
1244 {
1245 case VOID_TYPE:
1246 case BOOLEAN_TYPE:
1247 /* All void and bool types are the same. */
1248 break;
1249
1250 case INTEGER_TYPE:
1251 case FIXED_POINT_TYPE:
1252 case REAL_TYPE:
1253 /* With these nodes, we can't determine type equivalence by
1254 looking at what is stored in the nodes themselves, because
1255 two nodes might have different TYPE_MAIN_VARIANTs but still
1256 represent the same type. For example, wchar_t and int could
1257 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1258 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1259 and are distinct types. On the other hand, int and the
1260 following typedef
1261
1262 typedef int INT __attribute((may_alias));
1263
1264 have identical properties, different TYPE_MAIN_VARIANTs, but
1265 represent the same type. The canonical type system keeps
1266 track of equivalence in this case, so we fall back on it. */
1267 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1268
1269 case TEMPLATE_TEMPLATE_PARM:
1270 case BOUND_TEMPLATE_TEMPLATE_PARM:
1271 if (!comp_template_parms_position (t1, t2))
1272 return false;
1273 if (!comp_template_parms
1274 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1275 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1276 return false;
1277 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1278 break;
1279 /* Don't check inheritance. */
1280 strict = COMPARE_STRICT;
1281 /* Fall through. */
1282
1283 case RECORD_TYPE:
1284 case UNION_TYPE:
1285 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1286 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1287 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1288 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1289 break;
1290
1291 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1292 break;
1293 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1294 break;
1295
1296 return false;
1297
1298 case OFFSET_TYPE:
1299 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1300 strict & ~COMPARE_REDECLARATION))
1301 return false;
1302 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1303 return false;
1304 break;
1305
1306 case REFERENCE_TYPE:
1307 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1308 return false;
1309 /* fall through to checks for pointer types */
1310
1311 case POINTER_TYPE:
1312 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1313 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1314 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1315 return false;
1316 break;
1317
1318 case METHOD_TYPE:
1319 case FUNCTION_TYPE:
1320 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1321 return false;
1322 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1323 return false;
1324 break;
1325
1326 case ARRAY_TYPE:
1327 /* Target types must match incl. qualifiers. */
1328 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1329 return false;
1330 break;
1331
1332 case TEMPLATE_TYPE_PARM:
1333 /* If T1 and T2 don't have the same relative position in their
1334 template parameters set, they can't be equal. */
1335 if (!comp_template_parms_position (t1, t2))
1336 return false;
1337 break;
1338
1339 case TYPENAME_TYPE:
1340 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1341 TYPENAME_TYPE_FULLNAME (t2)))
1342 return false;
1343 /* Qualifiers don't matter on scopes. */
1344 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1345 TYPE_CONTEXT (t2)))
1346 return false;
1347 break;
1348
1349 case UNBOUND_CLASS_TEMPLATE:
1350 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1351 return false;
1352 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1353 return false;
1354 break;
1355
1356 case COMPLEX_TYPE:
1357 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1358 return false;
1359 break;
1360
1361 case VECTOR_TYPE:
1362 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1363 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1364 return false;
1365 break;
1366
1367 case TYPE_PACK_EXPANSION:
1368 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1369 PACK_EXPANSION_PATTERN (t2))
1370 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1371 PACK_EXPANSION_EXTRA_ARGS (t2)));
1372
1373 case DECLTYPE_TYPE:
1374 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1375 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1376 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1377 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1378 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1379 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1380 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1381 DECLTYPE_TYPE_EXPR (t2)))
1382 return false;
1383 break;
1384
1385 case UNDERLYING_TYPE:
1386 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1387 UNDERLYING_TYPE_TYPE (t2));
1388
1389 default:
1390 return false;
1391 }
1392
1393 /* If we get here, we know that from a target independent POV the
1394 types are the same. Make sure the target attributes are also
1395 the same. */
1396 return comp_type_attributes (t1, t2);
1397}
1398
1399/* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1400 is a bitwise-or of the COMPARE_* flags. */
1401
1402bool
1403comptypes (tree t1, tree t2, int strict)
1404{
1405 if (strict == COMPARE_STRICT)
1406 {
1407 if (t1 == t2)
1408 return true;
1409
1410 if (t1 == error_mark_node || t2 == error_mark_node)
1411 return false;
1412
1413 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1414 /* At least one of the types requires structural equality, so
1415 perform a deep check. */
1416 return structural_comptypes (t1, t2, strict);
1417
1418#ifdef ENABLE_CHECKING
1419 if (USE_CANONICAL_TYPES)
1420 {
1421 bool result = structural_comptypes (t1, t2, strict);
1422
1423 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1424 /* The two types are structurally equivalent, but their
1425 canonical types were different. This is a failure of the
1426 canonical type propagation code.*/
1427 internal_error
1428 ("canonical types differ for identical types %T and %T",
1429 t1, t2);
1430 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1431 /* Two types are structurally different, but the canonical
1432 types are the same. This means we were over-eager in
1433 assigning canonical types. */
1434 internal_error
1435 ("same canonical type node for different types %T and %T",
1436 t1, t2);
1437
1438 return result;
1439 }
1440#else
1441 if (USE_CANONICAL_TYPES)
1442 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1443#endif
1444 else
1445 return structural_comptypes (t1, t2, strict);
1446 }
1447 else if (strict == COMPARE_STRUCTURAL)
1448 return structural_comptypes (t1, t2, COMPARE_STRICT);
1449 else
1450 return structural_comptypes (t1, t2, strict);
1451}
1452
1453/* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1454 top-level qualifiers. */
1455
1456bool
1457same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1458{
1459 if (type1 == error_mark_node || type2 == error_mark_node)
1460 return false;
1461
1462 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1463}
1464
1465/* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1466
1467bool
1468at_least_as_qualified_p (const_tree type1, const_tree type2)
1469{
1470 int q1 = cp_type_quals (type1);
1471 int q2 = cp_type_quals (type2);
1472
1473 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1474 return (q1 & q2) == q2;
1475}
1476
1477/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1478 more cv-qualified that TYPE1, and 0 otherwise. */
1479
1480int
1481comp_cv_qualification (int q1, int q2)
1482{
1483 if (q1 == q2)
1484 return 0;
1485
1486 if ((q1 & q2) == q2)
1487 return 1;
1488 else if ((q1 & q2) == q1)
1489 return -1;
1490
1491 return 0;
1492}
1493
1494int
1495comp_cv_qualification (const_tree type1, const_tree type2)
1496{
1497 int q1 = cp_type_quals (type1);
1498 int q2 = cp_type_quals (type2);
1499 return comp_cv_qualification (q1, q2);
1500}
1501
1502/* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1503 subset of the cv-qualification signature of TYPE2, and the types
1504 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1505
1506int
1507comp_cv_qual_signature (tree type1, tree type2)
1508{
1509 if (comp_ptr_ttypes_real (type2, type1, -1))
1510 return 1;
1511 else if (comp_ptr_ttypes_real (type1, type2, -1))
1512 return -1;
1513 else
1514 return 0;
1515}
1516\f
1517/* Subroutines of `comptypes'. */
1518
1519/* Return true if two parameter type lists PARMS1 and PARMS2 are
1520 equivalent in the sense that functions with those parameter types
1521 can have equivalent types. The two lists must be equivalent,
1522 element by element. */
1523
1524bool
1525compparms (const_tree parms1, const_tree parms2)
1526{
1527 const_tree t1, t2;
1528
1529 /* An unspecified parmlist matches any specified parmlist
1530 whose argument types don't need default promotions. */
1531
1532 for (t1 = parms1, t2 = parms2;
1533 t1 || t2;
1534 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1535 {
1536 /* If one parmlist is shorter than the other,
1537 they fail to match. */
1538 if (!t1 || !t2)
1539 return false;
1540 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1541 return false;
1542 }
1543 return true;
1544}
1545
1546\f
1547/* Process a sizeof or alignof expression where the operand is a
1548 type. */
1549
1550tree
1551cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1552{
1553 tree value;
1554 bool dependent_p;
1555
1556 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1557 if (type == error_mark_node)
1558 return error_mark_node;
1559
1560 type = non_reference (type);
1561 if (TREE_CODE (type) == METHOD_TYPE)
1562 {
1563 if (complain)
1564 pedwarn (input_location, OPT_Wpointer_arith,
1565 "invalid application of %qs to a member function",
1566 operator_name_info[(int) op].name);
1567 else
1568 return error_mark_node;
1569 value = size_one_node;
1570 }
1571
1572 dependent_p = dependent_type_p (type);
1573 if (!dependent_p)
1574 complete_type (type);
1575 if (dependent_p
1576 /* VLA types will have a non-constant size. In the body of an
1577 uninstantiated template, we don't need to try to compute the
1578 value, because the sizeof expression is not an integral
1579 constant expression in that case. And, if we do try to
1580 compute the value, we'll likely end up with SAVE_EXPRs, which
1581 the template substitution machinery does not expect to see. */
1582 || (processing_template_decl
1583 && COMPLETE_TYPE_P (type)
1584 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1585 {
1586 value = build_min (op, size_type_node, type);
1587 TREE_READONLY (value) = 1;
1588 return value;
1589 }
1590
1591 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1592 op == SIZEOF_EXPR, false,
1593 complain);
1594}
1595
1596/* Return the size of the type, without producing any warnings for
1597 types whose size cannot be taken. This routine should be used only
1598 in some other routine that has already produced a diagnostic about
1599 using the size of such a type. */
1600tree
1601cxx_sizeof_nowarn (tree type)
1602{
1603 if (TREE_CODE (type) == FUNCTION_TYPE
1604 || VOID_TYPE_P (type)
1605 || TREE_CODE (type) == ERROR_MARK)
1606 return size_one_node;
1607 else if (!COMPLETE_TYPE_P (type))
1608 return size_zero_node;
1609 else
1610 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1611}
1612
1613/* Process a sizeof expression where the operand is an expression. */
1614
1615static tree
1616cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1617{
1618 if (e == error_mark_node)
1619 return error_mark_node;
1620
1621 if (processing_template_decl)
1622 {
1623 e = build_min (SIZEOF_EXPR, size_type_node, e);
1624 TREE_SIDE_EFFECTS (e) = 0;
1625 TREE_READONLY (e) = 1;
1626
1627 return e;
1628 }
1629
1630 /* To get the size of a static data member declared as an array of
1631 unknown bound, we need to instantiate it. */
1632 if (VAR_P (e)
1633 && VAR_HAD_UNKNOWN_BOUND (e)
1634 && DECL_TEMPLATE_INSTANTIATION (e))
1635 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1636
1637 if (TREE_CODE (e) == PARM_DECL
1638 && DECL_ARRAY_PARAMETER_P (e)
1639 && (complain & tf_warning))
1640 {
1641 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1642 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1643 inform (DECL_SOURCE_LOCATION (e), "declared here");
1644 }
1645
1646 e = mark_type_use (e);
1647
1648 if (TREE_CODE (e) == COMPONENT_REF
1649 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1650 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1651 {
1652 if (complain & tf_error)
1653 error ("invalid application of %<sizeof%> to a bit-field");
1654 else
1655 return error_mark_node;
1656 e = char_type_node;
1657 }
1658 else if (is_overloaded_fn (e))
1659 {
1660 if (complain & tf_error)
1661 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1662 "function type");
1663 else
1664 return error_mark_node;
1665 e = char_type_node;
1666 }
1667 else if (type_unknown_p (e))
1668 {
1669 if (complain & tf_error)
1670 cxx_incomplete_type_error (e, TREE_TYPE (e));
1671 else
1672 return error_mark_node;
1673 e = char_type_node;
1674 }
1675 else
1676 e = TREE_TYPE (e);
1677
1678 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1679}
1680
1681/* Implement the __alignof keyword: Return the minimum required
1682 alignment of E, measured in bytes. For VAR_DECL's and
1683 FIELD_DECL's return DECL_ALIGN (which can be set from an
1684 "aligned" __attribute__ specification). */
1685
1686static tree
1687cxx_alignof_expr (tree e, tsubst_flags_t complain)
1688{
1689 tree t;
1690
1691 if (e == error_mark_node)
1692 return error_mark_node;
1693
1694 if (processing_template_decl)
1695 {
1696 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1697 TREE_SIDE_EFFECTS (e) = 0;
1698 TREE_READONLY (e) = 1;
1699
1700 return e;
1701 }
1702
1703 e = mark_type_use (e);
1704
1705 if (VAR_P (e))
1706 t = size_int (DECL_ALIGN_UNIT (e));
1707 else if (TREE_CODE (e) == COMPONENT_REF
1708 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1709 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1710 {
1711 if (complain & tf_error)
1712 error ("invalid application of %<__alignof%> to a bit-field");
1713 else
1714 return error_mark_node;
1715 t = size_one_node;
1716 }
1717 else if (TREE_CODE (e) == COMPONENT_REF
1718 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1719 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1720 else if (is_overloaded_fn (e))
1721 {
1722 if (complain & tf_error)
1723 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1724 "function type");
1725 else
1726 return error_mark_node;
1727 if (TREE_CODE (e) == FUNCTION_DECL)
1728 t = size_int (DECL_ALIGN_UNIT (e));
1729 else
1730 t = size_one_node;
1731 }
1732 else if (type_unknown_p (e))
1733 {
1734 if (complain & tf_error)
1735 cxx_incomplete_type_error (e, TREE_TYPE (e));
1736 else
1737 return error_mark_node;
1738 t = size_one_node;
1739 }
1740 else
1741 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1742 complain & tf_error);
1743
1744 return fold_convert (size_type_node, t);
1745}
1746
1747/* Process a sizeof or alignof expression E with code OP where the operand
1748 is an expression. */
1749
1750tree
1751cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1752{
1753 if (op == SIZEOF_EXPR)
1754 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1755 else
1756 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1757}
1758
1759/* Build a representation of an expression 'alignas(E).' Return the
1760 folded integer value of E if it is an integral constant expression
1761 that resolves to a valid alignment. If E depends on a template
1762 parameter, return a syntactic representation tree of kind
1763 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1764 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1765
1766tree
1767cxx_alignas_expr (tree e)
1768{
1769 if (e == NULL_TREE || e == error_mark_node
1770 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1771 return e;
1772
1773 if (TYPE_P (e))
1774 /* [dcl.align]/3:
1775
1776 When the alignment-specifier is of the form
1777 alignas(type-id ), it shall have the same effect as
1778 alignas(alignof(type-id )). */
1779
1780 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1781
1782 /* If we reach this point, it means the alignas expression if of
1783 the form "alignas(assignment-expression)", so we should follow
1784 what is stated by [dcl.align]/2. */
1785
1786 if (value_dependent_expression_p (e))
1787 /* Leave value-dependent expression alone for now. */
1788 return e;
1789
1790 e = instantiate_non_dependent_expr (e);
1791 e = mark_rvalue_use (e);
1792
1793 /* [dcl.align]/2 says:
1794
1795 the assignment-expression shall be an integral constant
1796 expression. */
1797
1798 return cxx_constant_value (e);
1799}
1800
1801\f
1802/* EXPR is being used in a context that is not a function call.
1803 Enforce:
1804
1805 [expr.ref]
1806
1807 The expression can be used only as the left-hand operand of a
1808 member function call.
1809
1810 [expr.mptr.operator]
1811
1812 If the result of .* or ->* is a function, then that result can be
1813 used only as the operand for the function call operator ().
1814
1815 by issuing an error message if appropriate. Returns true iff EXPR
1816 violates these rules. */
1817
1818bool
1819invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1820{
1821 if (expr == NULL_TREE)
1822 return false;
1823 /* Don't enforce this in MS mode. */
1824 if (flag_ms_extensions)
1825 return false;
1826 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1827 expr = get_first_fn (expr);
1828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1829 {
1830 if (complain & tf_error)
1831 error ("invalid use of non-static member function");
1832 return true;
1833 }
1834 return false;
1835}
1836
1837/* If EXP is a reference to a bitfield, and the type of EXP does not
1838 match the declared type of the bitfield, return the declared type
1839 of the bitfield. Otherwise, return NULL_TREE. */
1840
1841tree
1842is_bitfield_expr_with_lowered_type (const_tree exp)
1843{
1844 switch (TREE_CODE (exp))
1845 {
1846 case COND_EXPR:
1847 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1848 ? TREE_OPERAND (exp, 1)
1849 : TREE_OPERAND (exp, 0)))
1850 return NULL_TREE;
1851 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1852
1853 case COMPOUND_EXPR:
1854 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1855
1856 case MODIFY_EXPR:
1857 case SAVE_EXPR:
1858 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1859
1860 case COMPONENT_REF:
1861 {
1862 tree field;
1863
1864 field = TREE_OPERAND (exp, 1);
1865 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1866 return NULL_TREE;
1867 if (same_type_ignoring_top_level_qualifiers_p
1868 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1869 return NULL_TREE;
1870 return DECL_BIT_FIELD_TYPE (field);
1871 }
1872
1873 CASE_CONVERT:
1874 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1875 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1876 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1877 /* Fallthrough. */
1878
1879 default:
1880 return NULL_TREE;
1881 }
1882}
1883
1884/* Like is_bitfield_with_lowered_type, except that if EXP is not a
1885 bitfield with a lowered type, the type of EXP is returned, rather
1886 than NULL_TREE. */
1887
1888tree
1889unlowered_expr_type (const_tree exp)
1890{
1891 tree type;
1892 tree etype = TREE_TYPE (exp);
1893
1894 type = is_bitfield_expr_with_lowered_type (exp);
1895 if (type)
1896 type = cp_build_qualified_type (type, cp_type_quals (etype));
1897 else
1898 type = etype;
1899
1900 return type;
1901}
1902
1903/* Perform the conversions in [expr] that apply when an lvalue appears
1904 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1905 function-to-pointer conversions. In addition, manifest constants
1906 are replaced by their values, and bitfield references are converted
1907 to their declared types. Note that this function does not perform the
1908 lvalue-to-rvalue conversion for class types. If you need that conversion
1909 to for class types, then you probably need to use force_rvalue.
1910
1911 Although the returned value is being used as an rvalue, this
1912 function does not wrap the returned expression in a
1913 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1914 that the return value is no longer an lvalue. */
1915
1916tree
1917decay_conversion (tree exp, tsubst_flags_t complain)
1918{
1919 tree type;
1920 enum tree_code code;
1921 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1922
1923 type = TREE_TYPE (exp);
1924 if (type == error_mark_node)
1925 return error_mark_node;
1926
1927 exp = mark_rvalue_use (exp);
1928
1929 exp = resolve_nondeduced_context (exp);
1930 if (type_unknown_p (exp))
1931 {
1932 if (complain & tf_error)
1933 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1934 return error_mark_node;
1935 }
1936
1937 code = TREE_CODE (type);
1938
1939 /* FIXME remove for delayed folding. */
1940 exp = scalar_constant_value (exp);
1941 if (error_operand_p (exp))
1942 return error_mark_node;
1943
1944 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1945 return nullptr_node;
1946
1947 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1948 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1949 if (code == VOID_TYPE)
1950 {
1951 if (complain & tf_error)
1952 error_at (loc, "void value not ignored as it ought to be");
1953 return error_mark_node;
1954 }
1955 if (invalid_nonstatic_memfn_p (exp, complain))
1956 return error_mark_node;
1957 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1958 return cp_build_addr_expr (exp, complain);
1959 if (code == ARRAY_TYPE)
1960 {
1961 tree adr;
1962 tree ptrtype;
1963
1964 if (INDIRECT_REF_P (exp))
1965 return build_nop (build_pointer_type (TREE_TYPE (type)),
1966 TREE_OPERAND (exp, 0));
1967
1968 if (TREE_CODE (exp) == COMPOUND_EXPR)
1969 {
1970 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1971 if (op1 == error_mark_node)
1972 return error_mark_node;
1973 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1974 TREE_OPERAND (exp, 0), op1);
1975 }
1976
1977 if (!lvalue_p (exp)
1978 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1979 {
1980 if (complain & tf_error)
1981 error_at (loc, "invalid use of non-lvalue array");
1982 return error_mark_node;
1983 }
1984
1985 /* Don't let an array compound literal decay to a pointer. It can
1986 still be used to initialize an array or bind to a reference. */
1987 if (TREE_CODE (exp) == TARGET_EXPR)
1988 {
1989 if (complain & tf_error)
1990 error_at (loc, "taking address of temporary array");
1991 return error_mark_node;
1992 }
1993
1994 ptrtype = build_pointer_type (TREE_TYPE (type));
1995
1996 if (VAR_P (exp))
1997 {
1998 if (!cxx_mark_addressable (exp))
1999 return error_mark_node;
2000 adr = build_nop (ptrtype, build_address (exp));
2001 return adr;
2002 }
2003 /* This way is better for a COMPONENT_REF since it can
2004 simplify the offset for a component. */
2005 adr = cp_build_addr_expr (exp, complain);
2006 return cp_convert (ptrtype, adr, complain);
2007 }
2008
2009 /* If a bitfield is used in a context where integral promotion
2010 applies, then the caller is expected to have used
2011 default_conversion. That function promotes bitfields correctly
2012 before calling this function. At this point, if we have a
2013 bitfield referenced, we may assume that is not subject to
2014 promotion, and that, therefore, the type of the resulting rvalue
2015 is the declared type of the bitfield. */
2016 exp = convert_bitfield_to_declared_type (exp);
2017
2018 /* We do not call rvalue() here because we do not want to wrap EXP
2019 in a NON_LVALUE_EXPR. */
2020
2021 /* [basic.lval]
2022
2023 Non-class rvalues always have cv-unqualified types. */
2024 type = TREE_TYPE (exp);
2025 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2026 exp = build_nop (cv_unqualified (type), exp);
2027
2028 return exp;
2029}
2030
2031/* Perform preparatory conversions, as part of the "usual arithmetic
2032 conversions". In particular, as per [expr]:
2033
2034 Whenever an lvalue expression appears as an operand of an
2035 operator that expects the rvalue for that operand, the
2036 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2037 standard conversions are applied to convert the expression to an
2038 rvalue.
2039
2040 In addition, we perform integral promotions here, as those are
2041 applied to both operands to a binary operator before determining
2042 what additional conversions should apply. */
2043
2044static tree
2045cp_default_conversion (tree exp, tsubst_flags_t complain)
2046{
2047 /* Check for target-specific promotions. */
2048 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2049 if (promoted_type)
2050 exp = cp_convert (promoted_type, exp, complain);
2051 /* Perform the integral promotions first so that bitfield
2052 expressions (which may promote to "int", even if the bitfield is
2053 declared "unsigned") are promoted correctly. */
2054 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2055 exp = cp_perform_integral_promotions (exp, complain);
2056 /* Perform the other conversions. */
2057 exp = decay_conversion (exp, complain);
2058
2059 return exp;
2060}
2061
2062/* C version. */
2063
2064tree
2065default_conversion (tree exp)
2066{
2067 return cp_default_conversion (exp, tf_warning_or_error);
2068}
2069
2070/* EXPR is an expression with an integral or enumeration type.
2071 Perform the integral promotions in [conv.prom], and return the
2072 converted value. */
2073
2074tree
2075cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2076{
2077 tree type;
2078 tree promoted_type;
2079
2080 expr = mark_rvalue_use (expr);
2081
2082 /* [conv.prom]
2083
2084 If the bitfield has an enumerated type, it is treated as any
2085 other value of that type for promotion purposes. */
2086 type = is_bitfield_expr_with_lowered_type (expr);
2087 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2088 type = TREE_TYPE (expr);
2089 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2090 /* Scoped enums don't promote. */
2091 if (SCOPED_ENUM_P (type))
2092 return expr;
2093 promoted_type = type_promotes_to (type);
2094 if (type != promoted_type)
2095 expr = cp_convert (promoted_type, expr, complain);
2096 return expr;
2097}
2098
2099/* C version. */
2100
2101tree
2102perform_integral_promotions (tree expr)
2103{
2104 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2105}
2106
2107/* Returns nonzero iff exp is a STRING_CST or the result of applying
2108 decay_conversion to one. */
2109
2110int
2111string_conv_p (const_tree totype, const_tree exp, int warn)
2112{
2113 tree t;
2114
2115 if (!TYPE_PTR_P (totype))
2116 return 0;
2117
2118 t = TREE_TYPE (totype);
2119 if (!same_type_p (t, char_type_node)
2120 && !same_type_p (t, char16_type_node)
2121 && !same_type_p (t, char32_type_node)
2122 && !same_type_p (t, wchar_type_node))
2123 return 0;
2124
2125 if (TREE_CODE (exp) == STRING_CST)
2126 {
2127 /* Make sure that we don't try to convert between char and wide chars. */
2128 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2129 return 0;
2130 }
2131 else
2132 {
2133 /* Is this a string constant which has decayed to 'const char *'? */
2134 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2135 if (!same_type_p (TREE_TYPE (exp), t))
2136 return 0;
2137 STRIP_NOPS (exp);
2138 if (TREE_CODE (exp) != ADDR_EXPR
2139 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2140 return 0;
2141 }
2142 if (warn)
2143 {
2144 if (cxx_dialect >= cxx11)
2145 pedwarn (input_location,
2146 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2147 "ISO C++ forbids converting a string constant to %qT",
2148 totype);
2149 else
2150 warning (OPT_Wwrite_strings,
2151 "deprecated conversion from string constant to %qT",
2152 totype);
2153 }
2154
2155 return 1;
2156}
2157
2158/* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2159 can, for example, use as an lvalue. This code used to be in
2160 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2161 expressions, where we're dealing with aggregates. But now it's again only
2162 called from unary_complex_lvalue. The case (in particular) that led to
2163 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2164 get it there. */
2165
2166static tree
2167rationalize_conditional_expr (enum tree_code code, tree t,
2168 tsubst_flags_t complain)
2169{
2170 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2171
2172 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2173 the first operand is always the one to be used if both operands
2174 are equal, so we know what conditional expression this used to be. */
2175 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2176 {
2177 tree op0 = TREE_OPERAND (t, 0);
2178 tree op1 = TREE_OPERAND (t, 1);
2179
2180 /* The following code is incorrect if either operand side-effects. */
2181 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2182 && !TREE_SIDE_EFFECTS (op1));
2183 return
2184 build_conditional_expr (loc,
2185 build_x_binary_op (loc,
2186 (TREE_CODE (t) == MIN_EXPR
2187 ? LE_EXPR : GE_EXPR),
2188 op0, TREE_CODE (op0),
2189 op1, TREE_CODE (op1),
2190 /*overload=*/NULL,
2191 complain),
2192 cp_build_unary_op (code, op0, 0, complain),
2193 cp_build_unary_op (code, op1, 0, complain),
2194 complain);
2195 }
2196
2197 return
2198 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2199 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2200 complain),
2201 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2202 complain),
2203 complain);
2204}
2205
2206/* Given the TYPE of an anonymous union field inside T, return the
2207 FIELD_DECL for the field. If not found return NULL_TREE. Because
2208 anonymous unions can nest, we must also search all anonymous unions
2209 that are directly reachable. */
2210
2211tree
2212lookup_anon_field (tree t, tree type)
2213{
2214 tree field;
2215
38c0c85b
JM
2216 t = TYPE_MAIN_VARIANT (t);
2217
dda118e3
JM
2218 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2219 {
2220 if (TREE_STATIC (field))
2221 continue;
2222 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2223 continue;
2224
2225 /* If we find it directly, return the field. */
2226 if (DECL_NAME (field) == NULL_TREE
2227 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2228 {
2229 return field;
2230 }
2231
2232 /* Otherwise, it could be nested, search harder. */
2233 if (DECL_NAME (field) == NULL_TREE
2234 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2235 {
2236 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2237 if (subfield)
2238 return subfield;
2239 }
2240 }
2241 return NULL_TREE;
2242}
2243
2244/* Build an expression representing OBJECT.MEMBER. OBJECT is an
2245 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2246 non-NULL, it indicates the path to the base used to name MEMBER.
2247 If PRESERVE_REFERENCE is true, the expression returned will have
2248 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2249 returned will have the type referred to by the reference.
2250
2251 This function does not perform access control; that is either done
2252 earlier by the parser when the name of MEMBER is resolved to MEMBER
2253 itself, or later when overload resolution selects one of the
2254 functions indicated by MEMBER. */
2255
2256tree
2257build_class_member_access_expr (tree object, tree member,
2258 tree access_path, bool preserve_reference,
2259 tsubst_flags_t complain)
2260{
2261 tree object_type;
2262 tree member_scope;
2263 tree result = NULL_TREE;
2264 tree using_decl = NULL_TREE;
2265
2266 if (error_operand_p (object) || error_operand_p (member))
2267 return error_mark_node;
2268
2269 gcc_assert (DECL_P (member) || BASELINK_P (member));
2270
2271 /* [expr.ref]
2272
2273 The type of the first expression shall be "class object" (of a
2274 complete type). */
2275 object_type = TREE_TYPE (object);
2276 if (!currently_open_class (object_type)
2277 && !complete_type_or_maybe_complain (object_type, object, complain))
2278 return error_mark_node;
2279 if (!CLASS_TYPE_P (object_type))
2280 {
2281 if (complain & tf_error)
2282 {
2283 if (POINTER_TYPE_P (object_type)
2284 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2285 error ("request for member %qD in %qE, which is of pointer "
2286 "type %qT (maybe you meant to use %<->%> ?)",
2287 member, object, object_type);
2288 else
2289 error ("request for member %qD in %qE, which is of non-class "
2290 "type %qT", member, object, object_type);
2291 }
2292 return error_mark_node;
2293 }
2294
2295 /* The standard does not seem to actually say that MEMBER must be a
2296 member of OBJECT_TYPE. However, that is clearly what is
2297 intended. */
2298 if (DECL_P (member))
2299 {
2300 member_scope = DECL_CLASS_CONTEXT (member);
2301 mark_used (member);
2302 if (TREE_DEPRECATED (member))
2303 warn_deprecated_use (member, NULL_TREE);
2304 }
2305 else
2306 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2307 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2308 presently be the anonymous union. Go outwards until we find a
2309 type related to OBJECT_TYPE. */
2310 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2311 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2312 object_type))
2313 member_scope = TYPE_CONTEXT (member_scope);
2314 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2315 {
2316 if (complain & tf_error)
2317 {
2318 if (TREE_CODE (member) == FIELD_DECL)
2319 error ("invalid use of nonstatic data member %qE", member);
2320 else
2321 error ("%qD is not a member of %qT", member, object_type);
2322 }
2323 return error_mark_node;
2324 }
2325
2326 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2327 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2328 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2329 {
2330 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2331 if (temp)
2332 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2333 }
2334
2335 /* In [expr.ref], there is an explicit list of the valid choices for
2336 MEMBER. We check for each of those cases here. */
2337 if (VAR_P (member))
2338 {
2339 /* A static data member. */
2340 result = member;
2341 mark_exp_read (object);
2342 /* If OBJECT has side-effects, they are supposed to occur. */
2343 if (TREE_SIDE_EFFECTS (object))
2344 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2345 }
2346 else if (TREE_CODE (member) == FIELD_DECL)
2347 {
2348 /* A non-static data member. */
2349 bool null_object_p;
2350 int type_quals;
2351 tree member_type;
2352
2353 null_object_p = (INDIRECT_REF_P (object)
2354 && integer_zerop (TREE_OPERAND (object, 0)));
2355
2356 /* Convert OBJECT to the type of MEMBER. */
2357 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2358 TYPE_MAIN_VARIANT (member_scope)))
2359 {
2360 tree binfo;
2361 base_kind kind;
2362
2363 binfo = lookup_base (access_path ? access_path : object_type,
2364 member_scope, ba_unique, &kind, complain);
2365 if (binfo == error_mark_node)
2366 return error_mark_node;
2367
2368 /* It is invalid to try to get to a virtual base of a
2369 NULL object. The most common cause is invalid use of
2370 offsetof macro. */
2371 if (null_object_p && kind == bk_via_virtual)
2372 {
2373 if (complain & tf_error)
2374 {
2375 error ("invalid access to non-static data member %qD in "
2376 "virtual base of NULL object", member);
2377 }
2378 return error_mark_node;
2379 }
2380
2381 /* Convert to the base. */
2382 object = build_base_path (PLUS_EXPR, object, binfo,
2383 /*nonnull=*/1, complain);
2384 /* If we found the base successfully then we should be able
2385 to convert to it successfully. */
2386 gcc_assert (object != error_mark_node);
2387 }
2388
2389 /* If MEMBER is from an anonymous aggregate, we have converted
2390 OBJECT so that it refers to the class containing the
2391 anonymous union. Generate a reference to the anonymous union
2392 itself, and recur to find MEMBER. */
2393 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2394 /* When this code is called from build_field_call, the
2395 object already has the type of the anonymous union.
2396 That is because the COMPONENT_REF was already
2397 constructed, and was then disassembled before calling
2398 build_field_call. After the function-call code is
2399 cleaned up, this waste can be eliminated. */
2400 && (!same_type_ignoring_top_level_qualifiers_p
2401 (TREE_TYPE (object), DECL_CONTEXT (member))))
2402 {
2403 tree anonymous_union;
2404
2405 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2406 DECL_CONTEXT (member));
2407 object = build_class_member_access_expr (object,
2408 anonymous_union,
2409 /*access_path=*/NULL_TREE,
2410 preserve_reference,
2411 complain);
2412 }
2413
2414 /* Compute the type of the field, as described in [expr.ref]. */
2415 type_quals = TYPE_UNQUALIFIED;
2416 member_type = TREE_TYPE (member);
2417 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2418 {
2419 type_quals = (cp_type_quals (member_type)
2420 | cp_type_quals (object_type));
2421
2422 /* A field is const (volatile) if the enclosing object, or the
2423 field itself, is const (volatile). But, a mutable field is
2424 not const, even within a const object. */
2425 if (DECL_MUTABLE_P (member))
2426 type_quals &= ~TYPE_QUAL_CONST;
2427 member_type = cp_build_qualified_type (member_type, type_quals);
2428 }
2429
2430 result = build3_loc (input_location, COMPONENT_REF, member_type,
2431 object, member, NULL_TREE);
2432 result = fold_if_not_in_template (result);
2433
2434 /* Mark the expression const or volatile, as appropriate. Even
2435 though we've dealt with the type above, we still have to mark the
2436 expression itself. */
2437 if (type_quals & TYPE_QUAL_CONST)
2438 TREE_READONLY (result) = 1;
2439 if (type_quals & TYPE_QUAL_VOLATILE)
2440 TREE_THIS_VOLATILE (result) = 1;
2441 }
2442 else if (BASELINK_P (member))
2443 {
2444 /* The member is a (possibly overloaded) member function. */
2445 tree functions;
2446 tree type;
2447
2448 /* If the MEMBER is exactly one static member function, then we
2449 know the type of the expression. Otherwise, we must wait
2450 until overload resolution has been performed. */
2451 functions = BASELINK_FUNCTIONS (member);
2452 if (TREE_CODE (functions) == FUNCTION_DECL
2453 && DECL_STATIC_FUNCTION_P (functions))
2454 type = TREE_TYPE (functions);
2455 else
2456 type = unknown_type_node;
2457 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2458 base. That will happen when the function is called. */
2459 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2460 }
2461 else if (TREE_CODE (member) == CONST_DECL)
2462 {
2463 /* The member is an enumerator. */
2464 result = member;
2465 /* If OBJECT has side-effects, they are supposed to occur. */
2466 if (TREE_SIDE_EFFECTS (object))
2467 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2468 object, result);
2469 }
2470 else if ((using_decl = strip_using_decl (member)) != member)
2471 result = build_class_member_access_expr (object,
2472 using_decl,
2473 access_path, preserve_reference,
2474 complain);
2475 else
2476 {
2477 if (complain & tf_error)
2478 error ("invalid use of %qD", member);
2479 return error_mark_node;
2480 }
2481
2482 if (!preserve_reference)
2483 /* [expr.ref]
2484
2485 If E2 is declared to have type "reference to T", then ... the
2486 type of E1.E2 is T. */
2487 result = convert_from_reference (result);
2488
2489 return result;
2490}
2491
2492/* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2493 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2494
2495static tree
2496lookup_destructor (tree object, tree scope, tree dtor_name,
2497 tsubst_flags_t complain)
2498{
2499 tree object_type = TREE_TYPE (object);
2500 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2501 tree expr;
2502
2503 /* We've already complained about this destructor. */
2504 if (dtor_type == error_mark_node)
2505 return error_mark_node;
2506
2507 if (scope && !check_dtor_name (scope, dtor_type))
2508 {
2509 if (complain & tf_error)
2510 error ("qualified type %qT does not match destructor name ~%qT",
2511 scope, dtor_type);
2512 return error_mark_node;
2513 }
2514 if (is_auto (dtor_type))
2515 dtor_type = object_type;
2516 else if (identifier_p (dtor_type))
2517 {
2518 /* In a template, names we can't find a match for are still accepted
2519 destructor names, and we check them here. */
2520 if (check_dtor_name (object_type, dtor_type))
2521 dtor_type = object_type;
2522 else
2523 {
2524 if (complain & tf_error)
2525 error ("object type %qT does not match destructor name ~%qT",
2526 object_type, dtor_type);
2527 return error_mark_node;
2528 }
2529
2530 }
2531 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2532 {
2533 if (complain & tf_error)
2534 error ("the type being destroyed is %qT, but the destructor "
2535 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2536 return error_mark_node;
2537 }
2538 expr = lookup_member (dtor_type, complete_dtor_identifier,
2539 /*protect=*/1, /*want_type=*/false,
2540 tf_warning_or_error);
2541 if (!expr)
2542 {
2543 if (complain & tf_error)
2544 cxx_incomplete_type_error (dtor_name, dtor_type);
2545 return error_mark_node;
2546 }
2547 expr = (adjust_result_of_qualified_name_lookup
2548 (expr, dtor_type, object_type));
2549 if (scope == NULL_TREE)
2550 /* We need to call adjust_result_of_qualified_name_lookup in case the
2551 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2552 that we still get virtual function binding. */
2553 BASELINK_QUALIFIED_P (expr) = false;
2554 return expr;
2555}
2556
2557/* An expression of the form "A::template B" has been resolved to
2558 DECL. Issue a diagnostic if B is not a template or template
2559 specialization. */
2560
2561void
2562check_template_keyword (tree decl)
2563{
2564 /* The standard says:
2565
2566 [temp.names]
2567
2568 If a name prefixed by the keyword template is not a member
2569 template, the program is ill-formed.
2570
2571 DR 228 removed the restriction that the template be a member
2572 template.
2573
2574 DR 96, if accepted would add the further restriction that explicit
2575 template arguments must be provided if the template keyword is
2576 used, but, as of 2005-10-16, that DR is still in "drafting". If
2577 this DR is accepted, then the semantic checks here can be
2578 simplified, as the entity named must in fact be a template
2579 specialization, rather than, as at present, a set of overloaded
2580 functions containing at least one template function. */
2581 if (TREE_CODE (decl) != TEMPLATE_DECL
2582 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2583 {
2584 if (!is_overloaded_fn (decl))
2585 permerror (input_location, "%qD is not a template", decl);
2586 else
2587 {
2588 tree fns;
2589 fns = decl;
2590 if (BASELINK_P (fns))
2591 fns = BASELINK_FUNCTIONS (fns);
2592 while (fns)
2593 {
2594 tree fn = OVL_CURRENT (fns);
2595 if (TREE_CODE (fn) == TEMPLATE_DECL
2596 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2597 break;
2598 if (TREE_CODE (fn) == FUNCTION_DECL
2599 && DECL_USE_TEMPLATE (fn)
2600 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2601 break;
2602 fns = OVL_NEXT (fns);
2603 }
2604 if (!fns)
2605 permerror (input_location, "%qD is not a template", decl);
2606 }
2607 }
2608}
2609
2610/* This function is called by the parser to process a class member
2611 access expression of the form OBJECT.NAME. NAME is a node used by
2612 the parser to represent a name; it is not yet a DECL. It may,
2613 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2614 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2615 there is no reason to do the lookup twice, so the parser keeps the
2616 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2617 be a template via the use of the "A::template B" syntax. */
2618
2619tree
2620finish_class_member_access_expr (tree object, tree name, bool template_p,
2621 tsubst_flags_t complain)
2622{
2623 tree expr;
2624 tree object_type;
2625 tree member;
2626 tree access_path = NULL_TREE;
2627 tree orig_object = object;
2628 tree orig_name = name;
2629
2630 if (object == error_mark_node || name == error_mark_node)
2631 return error_mark_node;
2632
2633 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2634 if (!objc_is_public (object, name))
2635 return error_mark_node;
2636
2637 object_type = TREE_TYPE (object);
2638
2639 if (processing_template_decl)
2640 {
2641 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2642 dependent_type_p (object_type)
2643 /* If NAME is just an IDENTIFIER_NODE, then the expression
2644 is dependent. */
2645 || identifier_p (object)
2646 /* If NAME is "f<args>", where either 'f' or 'args' is
2647 dependent, then the expression is dependent. */
2648 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2649 && dependent_template_id_p (TREE_OPERAND (name, 0),
2650 TREE_OPERAND (name, 1)))
2651 /* If NAME is "T::X" where "T" is dependent, then the
2652 expression is dependent. */
2653 || (TREE_CODE (name) == SCOPE_REF
2654 && TYPE_P (TREE_OPERAND (name, 0))
2655 && dependent_type_p (TREE_OPERAND (name, 0))))
2656 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2657 object, name, NULL_TREE);
2658 object = build_non_dependent_expr (object);
2659 }
2660 else if (c_dialect_objc ()
2661 && identifier_p (name)
2662 && (expr = objc_maybe_build_component_ref (object, name)))
2663 return expr;
2664
2665 /* [expr.ref]
2666
2667 The type of the first expression shall be "class object" (of a
2668 complete type). */
2669 if (!currently_open_class (object_type)
2670 && !complete_type_or_maybe_complain (object_type, object, complain))
2671 return error_mark_node;
2672 if (!CLASS_TYPE_P (object_type))
2673 {
2674 if (complain & tf_error)
2675 {
2676 if (POINTER_TYPE_P (object_type)
2677 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2678 error ("request for member %qD in %qE, which is of pointer "
2679 "type %qT (maybe you meant to use %<->%> ?)",
2680 name, object, object_type);
2681 else
2682 error ("request for member %qD in %qE, which is of non-class "
2683 "type %qT", name, object, object_type);
2684 }
2685 return error_mark_node;
2686 }
2687
2688 if (BASELINK_P (name))
2689 /* A member function that has already been looked up. */
2690 member = name;
2691 else
2692 {
2693 bool is_template_id = false;
2694 tree template_args = NULL_TREE;
2695 tree scope;
2696
2697 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2698 {
2699 is_template_id = true;
2700 template_args = TREE_OPERAND (name, 1);
2701 name = TREE_OPERAND (name, 0);
2702
2703 if (TREE_CODE (name) == OVERLOAD)
2704 name = DECL_NAME (get_first_fn (name));
2705 else if (DECL_P (name))
2706 name = DECL_NAME (name);
2707 }
2708
2709 if (TREE_CODE (name) == SCOPE_REF)
2710 {
2711 /* A qualified name. The qualifying class or namespace `S'
2712 has already been looked up; it is either a TYPE or a
2713 NAMESPACE_DECL. */
2714 scope = TREE_OPERAND (name, 0);
2715 name = TREE_OPERAND (name, 1);
2716
2717 /* If SCOPE is a namespace, then the qualified name does not
2718 name a member of OBJECT_TYPE. */
2719 if (TREE_CODE (scope) == NAMESPACE_DECL)
2720 {
2721 if (complain & tf_error)
2722 error ("%<%D::%D%> is not a member of %qT",
2723 scope, name, object_type);
2724 return error_mark_node;
2725 }
2726
2727 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2728 {
2729 /* Looking up a member enumerator (c++/56793). */
2730 if (!TYPE_CLASS_SCOPE_P (scope)
2731 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2732 {
2733 if (complain & tf_error)
2734 error ("%<%D::%D%> is not a member of %qT",
2735 scope, name, object_type);
2736 return error_mark_node;
2737 }
2738 tree val = lookup_enumerator (scope, name);
2739 if (TREE_SIDE_EFFECTS (object))
2740 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2741 return val;
2742 }
2743
2744 gcc_assert (CLASS_TYPE_P (scope));
2745 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2746
2747 if (constructor_name_p (name, scope))
2748 {
2749 if (complain & tf_error)
2750 error ("cannot call constructor %<%T::%D%> directly",
2751 scope, name);
2752 return error_mark_node;
2753 }
2754
2755 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2756 access_path = lookup_base (object_type, scope, ba_check,
2757 NULL, complain);
2758 if (access_path == error_mark_node)
2759 return error_mark_node;
2760 if (!access_path)
2761 {
2762 if (complain & tf_error)
2763 error ("%qT is not a base of %qT", scope, object_type);
2764 return error_mark_node;
2765 }
2766 }
2767 else
2768 {
2769 scope = NULL_TREE;
2770 access_path = object_type;
2771 }
2772
2773 if (TREE_CODE (name) == BIT_NOT_EXPR)
2774 member = lookup_destructor (object, scope, name, complain);
2775 else
2776 {
2777 /* Look up the member. */
2778 member = lookup_member (access_path, name, /*protect=*/1,
2779 /*want_type=*/false, complain);
2780 if (member == NULL_TREE)
2781 {
2782 if (complain & tf_error)
2783 error ("%q#T has no member named %qE",
2784 TREE_CODE (access_path) == TREE_BINFO
2785 ? TREE_TYPE (access_path) : object_type, name);
2786 return error_mark_node;
2787 }
2788 if (member == error_mark_node)
2789 return error_mark_node;
2790 }
2791
2792 if (is_template_id)
2793 {
2794 tree templ = member;
2795
2796 if (BASELINK_P (templ))
2797 templ = lookup_template_function (templ, template_args);
2798 else
2799 {
2800 if (complain & tf_error)
2801 error ("%qD is not a member template function", name);
2802 return error_mark_node;
2803 }
2804 }
2805 }
2806
2807 if (TREE_DEPRECATED (member))
2808 warn_deprecated_use (member, NULL_TREE);
2809
2810 if (template_p)
2811 check_template_keyword (member);
2812
2813 expr = build_class_member_access_expr (object, member, access_path,
2814 /*preserve_reference=*/false,
2815 complain);
2816 if (processing_template_decl && expr != error_mark_node)
2817 {
2818 if (BASELINK_P (member))
2819 {
2820 if (TREE_CODE (orig_name) == SCOPE_REF)
2821 BASELINK_QUALIFIED_P (member) = 1;
2822 orig_name = member;
2823 }
2824 return build_min_non_dep (COMPONENT_REF, expr,
2825 orig_object, orig_name,
2826 NULL_TREE);
2827 }
2828
2829 return expr;
2830}
2831
2832/* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2833 type. */
2834
2835tree
2836build_simple_component_ref (tree object, tree member)
2837{
2838 tree type = cp_build_qualified_type (TREE_TYPE (member),
2839 cp_type_quals (TREE_TYPE (object)));
2840 return fold_build3_loc (input_location,
2841 COMPONENT_REF, type,
2842 object, member, NULL_TREE);
2843}
2844
2845/* Return an expression for the MEMBER_NAME field in the internal
2846 representation of PTRMEM, a pointer-to-member function. (Each
2847 pointer-to-member function type gets its own RECORD_TYPE so it is
2848 more convenient to access the fields by name than by FIELD_DECL.)
2849 This routine converts the NAME to a FIELD_DECL and then creates the
2850 node for the complete expression. */
2851
2852tree
2853build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2854{
2855 tree ptrmem_type;
2856 tree member;
2857
2858 /* This code is a stripped down version of
2859 build_class_member_access_expr. It does not work to use that
2860 routine directly because it expects the object to be of class
2861 type. */
2862 ptrmem_type = TREE_TYPE (ptrmem);
2863 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2864 for (member = TYPE_FIELDS (ptrmem_type); member;
2865 member = DECL_CHAIN (member))
2866 if (DECL_NAME (member) == member_name)
2867 break;
2868 return build_simple_component_ref (ptrmem, member);
2869}
2870
2871/* Given an expression PTR for a pointer, return an expression
2872 for the value pointed to.
2873 ERRORSTRING is the name of the operator to appear in error messages.
2874
2875 This function may need to overload OPERATOR_FNNAME.
2876 Must also handle REFERENCE_TYPEs for C++. */
2877
2878tree
2879build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2880 tsubst_flags_t complain)
2881{
2882 tree orig_expr = expr;
2883 tree rval;
2884
2885 if (processing_template_decl)
2886 {
2887 /* Retain the type if we know the operand is a pointer. */
2888 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2889 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2890 if (type_dependent_expression_p (expr))
2891 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2892 expr = build_non_dependent_expr (expr);
2893 }
2894
2895 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2896 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2897 if (!rval)
2898 rval = cp_build_indirect_ref (expr, errorstring, complain);
2899
2900 if (processing_template_decl && rval != error_mark_node)
2901 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2902 else
2903 return rval;
2904}
2905
2906/* Helper function called from c-common. */
2907tree
2908build_indirect_ref (location_t /*loc*/,
2909 tree ptr, ref_operator errorstring)
2910{
2911 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2912}
2913
2914tree
2915cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2916 tsubst_flags_t complain)
2917{
2918 tree pointer, type;
2919
2920 if (ptr == current_class_ptr
2921 || (TREE_CODE (ptr) == NOP_EXPR
2922 && TREE_OPERAND (ptr, 0) == current_class_ptr
2923 && (same_type_ignoring_top_level_qualifiers_p
2924 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2925 return current_class_ref;
2926
2927 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2928 ? ptr : decay_conversion (ptr, complain));
2929 if (pointer == error_mark_node)
2930 return error_mark_node;
2931
2932 type = TREE_TYPE (pointer);
2933
2934 if (POINTER_TYPE_P (type))
2935 {
2936 /* [expr.unary.op]
2937
2938 If the type of the expression is "pointer to T," the type
2939 of the result is "T." */
2940 tree t = TREE_TYPE (type);
2941
2942 if ((CONVERT_EXPR_P (ptr)
2943 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2944 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2945 {
2946 /* If a warning is issued, mark it to avoid duplicates from
2947 the backend. This only needs to be done at
2948 warn_strict_aliasing > 2. */
2949 if (warn_strict_aliasing > 2)
2950 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2951 type, TREE_OPERAND (ptr, 0)))
2952 TREE_NO_WARNING (ptr) = 1;
2953 }
2954
2955 if (VOID_TYPE_P (t))
2956 {
2957 /* A pointer to incomplete type (other than cv void) can be
2958 dereferenced [expr.unary.op]/1 */
2959 if (complain & tf_error)
2960 error ("%qT is not a pointer-to-object type", type);
2961 return error_mark_node;
2962 }
2963 else if (TREE_CODE (pointer) == ADDR_EXPR
2964 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2965 /* The POINTER was something like `&x'. We simplify `*&x' to
2966 `x'. */
2967 return TREE_OPERAND (pointer, 0);
2968 else
2969 {
2970 tree ref = build1 (INDIRECT_REF, t, pointer);
2971
2972 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2973 so that we get the proper error message if the result is used
2974 to assign to. Also, &* is supposed to be a no-op. */
2975 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2976 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2977 TREE_SIDE_EFFECTS (ref)
2978 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2979 return ref;
2980 }
2981 }
2982 else if (!(complain & tf_error))
2983 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2984 ;
2985 /* `pointer' won't be an error_mark_node if we were given a
2986 pointer to member, so it's cool to check for this here. */
2987 else if (TYPE_PTRMEM_P (type))
2988 switch (errorstring)
2989 {
2990 case RO_ARRAY_INDEXING:
2991 error ("invalid use of array indexing on pointer to member");
2992 break;
2993 case RO_UNARY_STAR:
2994 error ("invalid use of unary %<*%> on pointer to member");
2995 break;
2996 case RO_IMPLICIT_CONVERSION:
2997 error ("invalid use of implicit conversion on pointer to member");
2998 break;
2999 case RO_ARROW_STAR:
3000 error ("left hand operand of %<->*%> must be a pointer to class, "
3001 "but is a pointer to member of type %qT", type);
3002 break;
3003 default:
3004 gcc_unreachable ();
3005 }
3006 else if (pointer != error_mark_node)
3007 invalid_indirection_error (input_location, type, errorstring);
3008
3009 return error_mark_node;
3010}
3011
3012/* This handles expressions of the form "a[i]", which denotes
3013 an array reference.
3014
3015 This is logically equivalent in C to *(a+i), but we may do it differently.
3016 If A is a variable or a member, we generate a primitive ARRAY_REF.
3017 This avoids forcing the array out of registers, and can work on
3018 arrays that are not lvalues (for example, members of structures returned
3019 by functions).
3020
3021 If INDEX is of some user-defined type, it must be converted to
3022 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3023 will inherit the type of the array, which will be some pointer type.
3024
3025 LOC is the location to use in building the array reference. */
3026
3027tree
3028cp_build_array_ref (location_t loc, tree array, tree idx,
3029 tsubst_flags_t complain)
3030{
3031 tree ret;
3032
3033 if (idx == 0)
3034 {
3035 if (complain & tf_error)
3036 error_at (loc, "subscript missing in array reference");
3037 return error_mark_node;
3038 }
3039
3040 /* If an array's index is an array notation, then its rank cannot be
3041 greater than one. */
3042 if (flag_cilkplus && contains_array_notation_expr (idx))
3043 {
3044 size_t rank = 0;
3045
3046 /* If find_rank returns false, then it should have reported an error,
3047 thus it is unnecessary for repetition. */
3048 if (!find_rank (loc, idx, idx, true, &rank))
3049 return error_mark_node;
3050 if (rank > 1)
3051 {
3052 error_at (loc, "rank of the array%'s index is greater than 1");
3053 return error_mark_node;
3054 }
3055 }
3056 if (TREE_TYPE (array) == error_mark_node
3057 || TREE_TYPE (idx) == error_mark_node)
3058 return error_mark_node;
3059
3060 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3061 inside it. */
3062 switch (TREE_CODE (array))
3063 {
3064 case COMPOUND_EXPR:
3065 {
3066 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3067 complain);
3068 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3069 TREE_OPERAND (array, 0), value);
3070 SET_EXPR_LOCATION (ret, loc);
3071 return ret;
3072 }
3073
3074 case COND_EXPR:
3075 ret = build_conditional_expr
3076 (loc, TREE_OPERAND (array, 0),
3077 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3078 complain),
3079 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3080 complain),
3081 complain);
3082 protected_set_expr_location (ret, loc);
3083 return ret;
3084
3085 default:
3086 break;
3087 }
3088
3089 bool non_lvalue
3090 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3091
3092 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3093 {
3094 tree rval, type;
3095
3096 warn_array_subscript_with_type_char (loc, idx);
3097
3098 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3099 {
3100 if (complain & tf_error)
3101 error_at (loc, "array subscript is not an integer");
3102 return error_mark_node;
3103 }
3104
3105 /* Apply integral promotions *after* noticing character types.
3106 (It is unclear why we do these promotions -- the standard
3107 does not say that we should. In fact, the natural thing would
3108 seem to be to convert IDX to ptrdiff_t; we're performing
3109 pointer arithmetic.) */
3110 idx = cp_perform_integral_promotions (idx, complain);
3111
3112 /* An array that is indexed by a non-constant
3113 cannot be stored in a register; we must be able to do
3114 address arithmetic on its address.
3115 Likewise an array of elements of variable size. */
3116 if (TREE_CODE (idx) != INTEGER_CST
3117 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3118 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3119 != INTEGER_CST)))
3120 {
3121 if (!cxx_mark_addressable (array))
3122 return error_mark_node;
3123 }
3124
3125 /* An array that is indexed by a constant value which is not within
3126 the array bounds cannot be stored in a register either; because we
3127 would get a crash in store_bit_field/extract_bit_field when trying
3128 to access a non-existent part of the register. */
3129 if (TREE_CODE (idx) == INTEGER_CST
3130 && TYPE_DOMAIN (TREE_TYPE (array))
3131 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3132 {
3133 if (!cxx_mark_addressable (array))
3134 return error_mark_node;
3135 }
3136
3137 if (!lvalue_p (array))
3138 {
3139 if (complain & tf_error)
3140 pedwarn (loc, OPT_Wpedantic,
3141 "ISO C++ forbids subscripting non-lvalue array");
3142 else
3143 return error_mark_node;
3144 }
3145
3146 /* Note in C++ it is valid to subscript a `register' array, since
3147 it is valid to take the address of something with that
3148 storage specification. */
3149 if (extra_warnings)
3150 {
3151 tree foo = array;
3152 while (TREE_CODE (foo) == COMPONENT_REF)
3153 foo = TREE_OPERAND (foo, 0);
3154 if (VAR_P (foo) && DECL_REGISTER (foo)
3155 && (complain & tf_warning))
3156 warning_at (loc, OPT_Wextra,
3157 "subscripting array declared %<register%>");
3158 }
3159
3160 type = TREE_TYPE (TREE_TYPE (array));
3161 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3162 /* Array ref is const/volatile if the array elements are
3163 or if the array is.. */
3164 TREE_READONLY (rval)
3165 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3166 TREE_SIDE_EFFECTS (rval)
3167 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3168 TREE_THIS_VOLATILE (rval)
3169 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3170 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3171 complain);
3172 protected_set_expr_location (ret, loc);
3173 if (non_lvalue)
3174 ret = non_lvalue_loc (loc, ret);
3175 return ret;
3176 }
3177
3178 {
3179 tree ar = cp_default_conversion (array, complain);
3180 tree ind = cp_default_conversion (idx, complain);
3181
3182 /* Put the integer in IND to simplify error checking. */
3183 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3184 {
3185 tree temp = ar;
3186 ar = ind;
3187 ind = temp;
3188 }
3189
3190 if (ar == error_mark_node || ind == error_mark_node)
3191 return error_mark_node;
3192
3193 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3194 {
3195 if (complain & tf_error)
3196 error_at (loc, "subscripted value is neither array nor pointer");
3197 return error_mark_node;
3198 }
3199 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3200 {
3201 if (complain & tf_error)
3202 error_at (loc, "array subscript is not an integer");
3203 return error_mark_node;
3204 }
3205
3206 warn_array_subscript_with_type_char (loc, idx);
3207
3208 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3209 PLUS_EXPR, ar, ind,
3210 complain),
3211 RO_ARRAY_INDEXING,
3212 complain);
3213 protected_set_expr_location (ret, loc);
3214 if (non_lvalue)
3215 ret = non_lvalue_loc (loc, ret);
3216 return ret;
3217 }
3218}
3219
3220/* Entry point for Obj-C++. */
3221
3222tree
3223build_array_ref (location_t loc, tree array, tree idx)
3224{
3225 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3226}
3227\f
3228/* Resolve a pointer to member function. INSTANCE is the object
3229 instance to use, if the member points to a virtual member.
3230
3231 This used to avoid checking for virtual functions if basetype
3232 has no virtual functions, according to an earlier ANSI draft.
3233 With the final ISO C++ rules, such an optimization is
3234 incorrect: A pointer to a derived member can be static_cast
3235 to pointer-to-base-member, as long as the dynamic object
3236 later has the right member. So now we only do this optimization
3237 when we know the dynamic type of the object. */
3238
3239tree
3240get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3241 tsubst_flags_t complain)
3242{
3243 if (TREE_CODE (function) == OFFSET_REF)
3244 function = TREE_OPERAND (function, 1);
3245
3246 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3247 {
3248 tree idx, delta, e1, e2, e3, vtbl;
3249 bool nonvirtual;
3250 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3251 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3252
3253 tree instance_ptr = *instance_ptrptr;
3254 tree instance_save_expr = 0;
3255 if (instance_ptr == error_mark_node)
3256 {
3257 if (TREE_CODE (function) == PTRMEM_CST)
3258 {
3259 /* Extracting the function address from a pmf is only
3260 allowed with -Wno-pmf-conversions. It only works for
3261 pmf constants. */
3262 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3263 e1 = convert (fntype, e1);
3264 return e1;
3265 }
3266 else
3267 {
3268 if (complain & tf_error)
3269 error ("object missing in use of %qE", function);
3270 return error_mark_node;
3271 }
3272 }
3273
3274 /* True if we know that the dynamic type of the object doesn't have
3275 virtual functions, so we can assume the PFN field is a pointer. */
3276 nonvirtual = (COMPLETE_TYPE_P (basetype)
3277 && !TYPE_POLYMORPHIC_P (basetype)
3278 && resolves_to_fixed_type_p (instance_ptr, 0));
3279
3280 /* If we don't really have an object (i.e. in an ill-formed
3281 conversion from PMF to pointer), we can't resolve virtual
3282 functions anyway. */
3283 if (!nonvirtual && is_dummy_object (instance_ptr))
3284 nonvirtual = true;
3285
3286 if (TREE_SIDE_EFFECTS (instance_ptr))
3287 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3288
3289 if (TREE_SIDE_EFFECTS (function))
3290 function = save_expr (function);
3291
3292 /* Start by extracting all the information from the PMF itself. */
3293 e3 = pfn_from_ptrmemfunc (function);
3294 delta = delta_from_ptrmemfunc (function);
3295 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3296 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3297 {
3298 case ptrmemfunc_vbit_in_pfn:
3299 e1 = cp_build_binary_op (input_location,
3300 BIT_AND_EXPR, idx, integer_one_node,
3301 complain);
3302 idx = cp_build_binary_op (input_location,
3303 MINUS_EXPR, idx, integer_one_node,
3304 complain);
3305 if (idx == error_mark_node)
3306 return error_mark_node;
3307 break;
3308
3309 case ptrmemfunc_vbit_in_delta:
3310 e1 = cp_build_binary_op (input_location,
3311 BIT_AND_EXPR, delta, integer_one_node,
3312 complain);
3313 delta = cp_build_binary_op (input_location,
3314 RSHIFT_EXPR, delta, integer_one_node,
3315 complain);
3316 if (delta == error_mark_node)
3317 return error_mark_node;
3318 break;
3319
3320 default:
3321 gcc_unreachable ();
3322 }
3323
3324 if (e1 == error_mark_node)
3325 return error_mark_node;
3326
3327 /* Convert down to the right base before using the instance. A
3328 special case is that in a pointer to member of class C, C may
3329 be incomplete. In that case, the function will of course be
3330 a member of C, and no conversion is required. In fact,
3331 lookup_base will fail in that case, because incomplete
3332 classes do not have BINFOs. */
3333 if (!same_type_ignoring_top_level_qualifiers_p
3334 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3335 {
3336 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3337 basetype, ba_check, NULL, complain);
3338 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3339 1, complain);
3340 if (instance_ptr == error_mark_node)
3341 return error_mark_node;
3342 }
3343 /* ...and then the delta in the PMF. */
3344 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3345
3346 /* Hand back the adjusted 'this' argument to our caller. */
3347 *instance_ptrptr = instance_ptr;
3348
3349 if (nonvirtual)
3350 /* Now just return the pointer. */
3351 return e3;
3352
3353 /* Next extract the vtable pointer from the object. */
3354 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3355 instance_ptr);
3356 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3357 if (vtbl == error_mark_node)
3358 return error_mark_node;
3359
3360 /* Finally, extract the function pointer from the vtable. */
3361 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3362 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3363 if (e2 == error_mark_node)
3364 return error_mark_node;
3365 TREE_CONSTANT (e2) = 1;
3366
3367 /* When using function descriptors, the address of the
3368 vtable entry is treated as a function pointer. */
3369 if (TARGET_VTABLE_USES_DESCRIPTORS)
3370 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3371 cp_build_addr_expr (e2, complain));
3372
3373 e2 = fold_convert (TREE_TYPE (e3), e2);
3374 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3375 if (e1 == error_mark_node)
3376 return error_mark_node;
3377
3378 /* Make sure this doesn't get evaluated first inside one of the
3379 branches of the COND_EXPR. */
3380 if (instance_save_expr)
3381 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3382 instance_save_expr, e1);
3383
3384 function = e1;
3385 }
3386 return function;
3387}
3388
3389/* Used by the C-common bits. */
3390tree
3391build_function_call (location_t /*loc*/,
3392 tree function, tree params)
3393{
3394 return cp_build_function_call (function, params, tf_warning_or_error);
3395}
3396
3397/* Used by the C-common bits. */
3398tree
3399build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3400 tree function, vec<tree, va_gc> *params,
3401 vec<tree, va_gc> * /*origtypes*/)
3402{
3403 vec<tree, va_gc> *orig_params = params;
3404 tree ret = cp_build_function_call_vec (function, &params,
3405 tf_warning_or_error);
3406
3407 /* cp_build_function_call_vec can reallocate PARAMS by adding
3408 default arguments. That should never happen here. Verify
3409 that. */
3410 gcc_assert (params == orig_params);
3411
3412 return ret;
3413}
3414
3415/* Build a function call using a tree list of arguments. */
3416
3417static tree
3418cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3419{
3420 vec<tree, va_gc> *vec;
3421 tree ret;
3422
3423 vec = make_tree_vector ();
3424 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3425 vec_safe_push (vec, TREE_VALUE (params));
3426 ret = cp_build_function_call_vec (function, &vec, complain);
3427 release_tree_vector (vec);
3428 return ret;
3429}
3430
3431/* Build a function call using varargs. */
3432
3433tree
3434cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3435{
3436 vec<tree, va_gc> *vec;
3437 va_list args;
3438 tree ret, t;
3439
3440 vec = make_tree_vector ();
3441 va_start (args, complain);
3442 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3443 vec_safe_push (vec, t);
3444 va_end (args);
3445 ret = cp_build_function_call_vec (function, &vec, complain);
3446 release_tree_vector (vec);
3447 return ret;
3448}
3449
3450/* Build a function call using a vector of arguments. PARAMS may be
3451 NULL if there are no parameters. This changes the contents of
3452 PARAMS. */
3453
3454tree
3455cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3456 tsubst_flags_t complain)
3457{
3458 tree fntype, fndecl;
3459 int is_method;
3460 tree original = function;
3461 int nargs;
3462 tree *argarray;
3463 tree parm_types;
3464 vec<tree, va_gc> *allocated = NULL;
3465 tree ret;
3466
3467 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3468 expressions, like those used for ObjC messenger dispatches. */
3469 if (params != NULL && !vec_safe_is_empty (*params))
3470 function = objc_rewrite_function_call (function, (**params)[0]);
3471
3472 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3473 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3474 if (TREE_CODE (function) == NOP_EXPR
3475 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3476 function = TREE_OPERAND (function, 0);
3477
3478 if (TREE_CODE (function) == FUNCTION_DECL)
3479 {
3480 mark_used (function);
3481 fndecl = function;
3482
3483 /* Convert anything with function type to a pointer-to-function. */
3484 if (DECL_MAIN_P (function))
3485 {
3486 if (complain & tf_error)
3487 pedwarn (input_location, OPT_Wpedantic,
3488 "ISO C++ forbids calling %<::main%> from within program");
3489 else
3490 return error_mark_node;
3491 }
3492 function = build_addr_func (function, complain);
3493 }
3494 else
3495 {
3496 fndecl = NULL_TREE;
3497
3498 function = build_addr_func (function, complain);
3499 }
3500
3501 if (function == error_mark_node)
3502 return error_mark_node;
3503
3504 fntype = TREE_TYPE (function);
3505
3506 if (TYPE_PTRMEMFUNC_P (fntype))
3507 {
3508 if (complain & tf_error)
3509 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3510 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3511 original, original);
3512 return error_mark_node;
3513 }
3514
3515 is_method = (TYPE_PTR_P (fntype)
3516 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3517
3518 if (!(TYPE_PTRFN_P (fntype)
3519 || is_method
3520 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3521 {
3522 if (complain & tf_error)
3523 {
3524 if (!flag_diagnostics_show_caret)
3525 error_at (input_location,
3526 "%qE cannot be used as a function", original);
3527 else if (DECL_P (original))
3528 error_at (input_location,
3529 "%qD cannot be used as a function", original);
3530 else
3531 error_at (input_location,
3532 "expression cannot be used as a function");
3533 }
3534
3535 return error_mark_node;
3536 }
3537
3538 /* fntype now gets the type of function pointed to. */
3539 fntype = TREE_TYPE (fntype);
3540 parm_types = TYPE_ARG_TYPES (fntype);
3541
3542 if (params == NULL)
3543 {
3544 allocated = make_tree_vector ();
3545 params = &allocated;
3546 }
3547
3548 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3549 complain);
3550 if (nargs < 0)
3551 return error_mark_node;
3552
3553 argarray = (*params)->address ();
3554
3555 /* Check for errors in format strings and inappropriately
3556 null parameters. */
3557 check_function_arguments (fntype, nargs, argarray);
3558
3559 ret = build_cxx_call (function, nargs, argarray, complain);
3560
3561 if (allocated != NULL)
3562 release_tree_vector (allocated);
3563
3564 return ret;
3565}
3566\f
3567/* Subroutine of convert_arguments.
3568 Warn about wrong number of args are genereted. */
3569
3570static void
3571warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3572{
3573 if (fndecl)
3574 {
3575 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3576 {
3577 if (DECL_NAME (fndecl) == NULL_TREE
3578 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3579 error_at (loc,
3580 too_many_p
3581 ? G_("too many arguments to constructor %q#D")
3582 : G_("too few arguments to constructor %q#D"),
3583 fndecl);
3584 else
3585 error_at (loc,
3586 too_many_p
3587 ? G_("too many arguments to member function %q#D")
3588 : G_("too few arguments to member function %q#D"),
3589 fndecl);
3590 }
3591 else
3592 error_at (loc,
3593 too_many_p
3594 ? G_("too many arguments to function %q#D")
3595 : G_("too few arguments to function %q#D"),
3596 fndecl);
3597 inform (DECL_SOURCE_LOCATION (fndecl),
3598 "declared here");
3599 }
3600 else
3601 {
3602 if (c_dialect_objc () && objc_message_selector ())
3603 error_at (loc,
3604 too_many_p
3605 ? G_("too many arguments to method %q#D")
3606 : G_("too few arguments to method %q#D"),
3607 objc_message_selector ());
3608 else
3609 error_at (loc, too_many_p ? G_("too many arguments to function")
3610 : G_("too few arguments to function"));
3611 }
3612}
3613
3614/* Convert the actual parameter expressions in the list VALUES to the
3615 types in the list TYPELIST. The converted expressions are stored
3616 back in the VALUES vector.
3617 If parmdecls is exhausted, or when an element has NULL as its type,
3618 perform the default conversions.
3619
3620 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3621
3622 This is also where warnings about wrong number of args are generated.
3623
3624 Returns the actual number of arguments processed (which might be less
3625 than the length of the vector), or -1 on error.
3626
3627 In C++, unspecified trailing parameters can be filled in with their
3628 default arguments, if such were specified. Do so here. */
3629
3630static int
3631convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3632 int flags, tsubst_flags_t complain)
3633{
3634 tree typetail;
3635 unsigned int i;
3636
3637 /* Argument passing is always copy-initialization. */
3638 flags |= LOOKUP_ONLYCONVERTING;
3639
3640 for (i = 0, typetail = typelist;
3641 i < vec_safe_length (*values);
3642 i++)
3643 {
3644 tree type = typetail ? TREE_VALUE (typetail) : 0;
3645 tree val = (**values)[i];
3646
3647 if (val == error_mark_node || type == error_mark_node)
3648 return -1;
3649
3650 if (type == void_type_node)
3651 {
3652 if (complain & tf_error)
3653 {
3654 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3655 return i;
3656 }
3657 else
3658 return -1;
3659 }
3660
3661 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3662 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3663 if (TREE_CODE (val) == NOP_EXPR
3664 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3665 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3666 val = TREE_OPERAND (val, 0);
3667
3668 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3669 {
3670 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3671 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3672 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3673 val = decay_conversion (val, complain);
3674 }
3675
3676 if (val == error_mark_node)
3677 return -1;
3678
3679 if (type != 0)
3680 {
3681 /* Formal parm type is specified by a function prototype. */
3682 tree parmval;
3683
3684 if (!COMPLETE_TYPE_P (complete_type (type)))
3685 {
3686 if (complain & tf_error)
3687 {
3688 if (fndecl)
3689 error ("parameter %P of %qD has incomplete type %qT",
3690 i, fndecl, type);
3691 else
3692 error ("parameter %P has incomplete type %qT", i, type);
3693 }
3694 parmval = error_mark_node;
3695 }
3696 else
3697 {
3698 parmval = convert_for_initialization
3699 (NULL_TREE, type, val, flags,
3700 ICR_ARGPASS, fndecl, i, complain);
3701 parmval = convert_for_arg_passing (type, parmval, complain);
3702 }
3703
3704 if (parmval == error_mark_node)
3705 return -1;
3706
3707 (**values)[i] = parmval;
3708 }
3709 else
3710 {
3711 if (fndecl && magic_varargs_p (fndecl))
3712 /* Don't do ellipsis conversion for __built_in_constant_p
3713 as this will result in spurious errors for non-trivial
3714 types. */
3715 val = require_complete_type_sfinae (val, complain);
3716 else
3717 val = convert_arg_to_ellipsis (val, complain);
3718
3719 (**values)[i] = val;
3720 }
3721
3722 if (typetail)
3723 typetail = TREE_CHAIN (typetail);
3724 }
3725
3726 if (typetail != 0 && typetail != void_list_node)
3727 {
3728 /* See if there are default arguments that can be used. Because
3729 we hold default arguments in the FUNCTION_TYPE (which is so
3730 wrong), we can see default parameters here from deduced
3731 contexts (and via typeof) for indirect function calls.
3732 Fortunately we know whether we have a function decl to
3733 provide default arguments in a language conformant
3734 manner. */
3735 if (fndecl && TREE_PURPOSE (typetail)
3736 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3737 {
3738 for (; typetail != void_list_node; ++i)
3739 {
3740 tree parmval
3741 = convert_default_arg (TREE_VALUE (typetail),
3742 TREE_PURPOSE (typetail),
3743 fndecl, i, complain);
3744
3745 if (parmval == error_mark_node)
3746 return -1;
3747
3748 vec_safe_push (*values, parmval);
3749 typetail = TREE_CHAIN (typetail);
3750 /* ends with `...'. */
3751 if (typetail == NULL_TREE)
3752 break;
3753 }
3754 }
3755 else
3756 {
3757 if (complain & tf_error)
3758 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3759 return -1;
3760 }
3761 }
3762
3763 return (int) i;
3764}
3765\f
3766/* Build a binary-operation expression, after performing default
3767 conversions on the operands. CODE is the kind of expression to
3768 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3769 are the tree codes which correspond to ARG1 and ARG2 when issuing
3770 warnings about possibly misplaced parentheses. They may differ
3771 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3772 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3773 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3774 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3775 ARG2_CODE as ERROR_MARK. */
3776
3777tree
3778build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3779 enum tree_code arg1_code, tree arg2,
3780 enum tree_code arg2_code, tree *overload,
3781 tsubst_flags_t complain)
3782{
3783 tree orig_arg1;
3784 tree orig_arg2;
3785 tree expr;
3786
3787 orig_arg1 = arg1;
3788 orig_arg2 = arg2;
3789
3790 if (processing_template_decl)
3791 {
3792 if (type_dependent_expression_p (arg1)