* tree.h (TYPE_ALIGN, DECL_ALIGN): Return shifted amount.
[platform/upstream/linaro-gcc.git] / gcc / cp / tree.c
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2    Copyright (C) 1987-2016 Free Software Foundation, Inc.
3    Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "cp-tree.h"
26 #include "gimple-expr.h"
27 #include "cgraph.h"
28 #include "stor-layout.h"
29 #include "print-tree.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "debug.h"
33 #include "convert.h"
34 #include "gimplify.h"
35 #include "attribs.h"
36
37 static tree bot_manip (tree *, int *, void *);
38 static tree bot_replace (tree *, int *, void *);
39 static hashval_t list_hash_pieces (tree, tree, tree);
40 static tree build_target_expr (tree, tree, tsubst_flags_t);
41 static tree count_trees_r (tree *, int *, void *);
42 static tree verify_stmt_tree_r (tree *, int *, void *);
43 static tree build_local_temp (tree);
44
45 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
46 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
49
50 /* If REF is an lvalue, returns the kind of lvalue that REF is.
51    Otherwise, returns clk_none.  */
52
53 cp_lvalue_kind
54 lvalue_kind (const_tree ref)
55 {
56   cp_lvalue_kind op1_lvalue_kind = clk_none;
57   cp_lvalue_kind op2_lvalue_kind = clk_none;
58
59   /* Expressions of reference type are sometimes wrapped in
60      INDIRECT_REFs.  INDIRECT_REFs are just internal compiler
61      representation, not part of the language, so we have to look
62      through them.  */
63   if (REFERENCE_REF_P (ref))
64     return lvalue_kind (TREE_OPERAND (ref, 0));
65
66   if (TREE_TYPE (ref)
67       && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
68     {
69       /* unnamed rvalue references are rvalues */
70       if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
71           && TREE_CODE (ref) != PARM_DECL
72           && !VAR_P (ref)
73           && TREE_CODE (ref) != COMPONENT_REF
74           /* Functions are always lvalues.  */
75           && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
76         return clk_rvalueref;
77
78       /* lvalue references and named rvalue references are lvalues.  */
79       return clk_ordinary;
80     }
81
82   if (ref == current_class_ptr)
83     return clk_none;
84
85   switch (TREE_CODE (ref))
86     {
87     case SAVE_EXPR:
88       return clk_none;
89       /* preincrements and predecrements are valid lvals, provided
90          what they refer to are valid lvals.  */
91     case PREINCREMENT_EXPR:
92     case PREDECREMENT_EXPR:
93     case TRY_CATCH_EXPR:
94     case WITH_CLEANUP_EXPR:
95     case REALPART_EXPR:
96     case IMAGPART_EXPR:
97       return lvalue_kind (TREE_OPERAND (ref, 0));
98
99     case MEMBER_REF:
100     case DOTSTAR_EXPR:
101       if (TREE_CODE (ref) == MEMBER_REF)
102         op1_lvalue_kind = clk_ordinary;
103       else
104         op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
105       if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
106         op1_lvalue_kind = clk_none;
107       return op1_lvalue_kind;
108
109     case COMPONENT_REF:
110       op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
111       /* Look at the member designator.  */
112       if (!op1_lvalue_kind)
113         ;
114       else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
115         /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
116            situations.  If we're seeing a COMPONENT_REF, it's a non-static
117            member, so it isn't an lvalue. */
118         op1_lvalue_kind = clk_none;
119       else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
120         /* This can be IDENTIFIER_NODE in a template.  */;
121       else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
122         {
123           /* Clear the ordinary bit.  If this object was a class
124              rvalue we want to preserve that information.  */
125           op1_lvalue_kind &= ~clk_ordinary;
126           /* The lvalue is for a bitfield.  */
127           op1_lvalue_kind |= clk_bitfield;
128         }
129       else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
130         op1_lvalue_kind |= clk_packed;
131
132       return op1_lvalue_kind;
133
134     case STRING_CST:
135     case COMPOUND_LITERAL_EXPR:
136       return clk_ordinary;
137
138     case CONST_DECL:
139       /* CONST_DECL without TREE_STATIC are enumeration values and
140          thus not lvalues.  With TREE_STATIC they are used by ObjC++
141          in objc_build_string_object and need to be considered as
142          lvalues.  */
143       if (! TREE_STATIC (ref))
144         return clk_none;
145     case VAR_DECL:
146       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
147           && DECL_LANG_SPECIFIC (ref)
148           && DECL_IN_AGGR_P (ref))
149         return clk_none;
150     case INDIRECT_REF:
151     case ARROW_EXPR:
152     case ARRAY_REF:
153     case ARRAY_NOTATION_REF:
154     case PARM_DECL:
155     case RESULT_DECL:
156     case PLACEHOLDER_EXPR:
157       return clk_ordinary;
158
159       /* A scope ref in a template, left as SCOPE_REF to support later
160          access checking.  */
161     case SCOPE_REF:
162       gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
163       {
164         tree op = TREE_OPERAND (ref, 1);
165         if (TREE_CODE (op) == FIELD_DECL)
166           return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
167         else
168           return lvalue_kind (op);
169       }
170
171     case MAX_EXPR:
172     case MIN_EXPR:
173       /* Disallow <? and >? as lvalues if either argument side-effects.  */
174       if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
175           || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
176         return clk_none;
177       op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
178       op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
179       break;
180
181     case COND_EXPR:
182       op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
183                                     ? TREE_OPERAND (ref, 1)
184                                     : TREE_OPERAND (ref, 0));
185       op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
186       break;
187
188     case MODOP_EXPR:
189       /* We expect to see unlowered MODOP_EXPRs only during
190          template processing.  */
191       gcc_assert (processing_template_decl);
192       return clk_ordinary;
193
194     case MODIFY_EXPR:
195     case TYPEID_EXPR:
196       return clk_ordinary;
197
198     case COMPOUND_EXPR:
199       return lvalue_kind (TREE_OPERAND (ref, 1));
200
201     case TARGET_EXPR:
202       return clk_class;
203
204     case VA_ARG_EXPR:
205       return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
206
207     case CALL_EXPR:
208       /* We can see calls outside of TARGET_EXPR in templates.  */
209       if (CLASS_TYPE_P (TREE_TYPE (ref)))
210         return clk_class;
211       return clk_none;
212
213     case FUNCTION_DECL:
214       /* All functions (except non-static-member functions) are
215          lvalues.  */
216       return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
217               ? clk_none : clk_ordinary);
218
219     case BASELINK:
220       /* We now represent a reference to a single static member function
221          with a BASELINK.  */
222       /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
223          its argument unmodified and we assign it to a const_tree.  */
224       return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
225
226     case NON_DEPENDENT_EXPR:
227       return lvalue_kind (TREE_OPERAND (ref, 0));
228
229     default:
230       if (!TREE_TYPE (ref))
231         return clk_none;
232       if (CLASS_TYPE_P (TREE_TYPE (ref)))
233         return clk_class;
234       break;
235     }
236
237   /* If one operand is not an lvalue at all, then this expression is
238      not an lvalue.  */
239   if (!op1_lvalue_kind || !op2_lvalue_kind)
240     return clk_none;
241
242   /* Otherwise, it's an lvalue, and it has all the odd properties
243      contributed by either operand.  */
244   op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
245   /* It's not an ordinary lvalue if it involves any other kind.  */
246   if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
247     op1_lvalue_kind &= ~clk_ordinary;
248   /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
249      A COND_EXPR of those should be wrapped in a TARGET_EXPR.  */
250   if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
251       && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
252     op1_lvalue_kind = clk_none;
253   return op1_lvalue_kind;
254 }
255
256 /* Returns the kind of lvalue that REF is, in the sense of
257    [basic.lval].  This function should really be named lvalue_p; it
258    computes the C++ definition of lvalue.  */
259
260 cp_lvalue_kind
261 real_lvalue_p (const_tree ref)
262 {
263   cp_lvalue_kind kind = lvalue_kind (ref);
264   if (kind & (clk_rvalueref|clk_class))
265     return clk_none;
266   else
267     return kind;
268 }
269
270 /* This differs from real_lvalue_p in that class rvalues are considered
271    lvalues.  */
272
273 bool
274 lvalue_p (const_tree ref)
275 {
276   return (lvalue_kind (ref) != clk_none);
277 }
278
279 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
280    rvalue references are considered rvalues.  */
281
282 bool
283 lvalue_or_rvalue_with_address_p (const_tree ref)
284 {
285   cp_lvalue_kind kind = lvalue_kind (ref);
286   if (kind & clk_class)
287     return false;
288   else
289     return (kind != clk_none);
290 }
291
292 /* Returns true if REF is an xvalue, false otherwise.  */
293
294 bool
295 xvalue_p (const_tree ref)
296 {
297   return (lvalue_kind (ref) == clk_rvalueref);
298 }
299
300 /* Test whether DECL is a builtin that may appear in a
301    constant-expression. */
302
303 bool
304 builtin_valid_in_constant_expr_p (const_tree decl)
305 {
306   if (!(TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)))
307     /* Not a built-in.  */
308     return false;
309   switch (DECL_FUNCTION_CODE (decl))
310     {
311     case BUILT_IN_CONSTANT_P:
312     case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
313       /* These have constant results even if their operands are
314          non-constant.  */
315       return true;
316     default:
317       return false;
318     }
319 }
320
321 /* Build a TARGET_EXPR, initializing the DECL with the VALUE.  */
322
323 static tree
324 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
325 {
326   tree t;
327   tree type = TREE_TYPE (decl);
328
329   gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
330                        || TREE_TYPE (decl) == TREE_TYPE (value)
331                        /* On ARM ctors return 'this'.  */
332                        || (TYPE_PTR_P (TREE_TYPE (value))
333                            && TREE_CODE (value) == CALL_EXPR)
334                        || useless_type_conversion_p (TREE_TYPE (decl),
335                                                      TREE_TYPE (value)));
336
337   t = cxx_maybe_build_cleanup (decl, complain);
338   if (t == error_mark_node)
339     return error_mark_node;
340   t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
341   if (EXPR_HAS_LOCATION (value))
342     SET_EXPR_LOCATION (t, EXPR_LOCATION (value));
343   /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
344      ignore the TARGET_EXPR.  If there really turn out to be no
345      side-effects, then the optimizer should be able to get rid of
346      whatever code is generated anyhow.  */
347   TREE_SIDE_EFFECTS (t) = 1;
348
349   return t;
350 }
351
352 /* Return an undeclared local temporary of type TYPE for use in building a
353    TARGET_EXPR.  */
354
355 static tree
356 build_local_temp (tree type)
357 {
358   tree slot = build_decl (input_location,
359                           VAR_DECL, NULL_TREE, type);
360   DECL_ARTIFICIAL (slot) = 1;
361   DECL_IGNORED_P (slot) = 1;
362   DECL_CONTEXT (slot) = current_function_decl;
363   layout_decl (slot, 0);
364   return slot;
365 }
366
367 /* Set various status flags when building an AGGR_INIT_EXPR object T.  */
368
369 static void
370 process_aggr_init_operands (tree t)
371 {
372   bool side_effects;
373
374   side_effects = TREE_SIDE_EFFECTS (t);
375   if (!side_effects)
376     {
377       int i, n;
378       n = TREE_OPERAND_LENGTH (t);
379       for (i = 1; i < n; i++)
380         {
381           tree op = TREE_OPERAND (t, i);
382           if (op && TREE_SIDE_EFFECTS (op))
383             {
384               side_effects = 1;
385               break;
386             }
387         }
388     }
389   TREE_SIDE_EFFECTS (t) = side_effects;
390 }
391
392 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
393    FN, and SLOT.  NARGS is the number of call arguments which are specified
394    as a tree array ARGS.  */
395
396 static tree
397 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
398                        tree *args)
399 {
400   tree t;
401   int i;
402
403   t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
404   TREE_TYPE (t) = return_type;
405   AGGR_INIT_EXPR_FN (t) = fn;
406   AGGR_INIT_EXPR_SLOT (t) = slot;
407   for (i = 0; i < nargs; i++)
408     AGGR_INIT_EXPR_ARG (t, i) = args[i];
409   process_aggr_init_operands (t);
410   return t;
411 }
412
413 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
414    target.  TYPE is the type to be initialized.
415
416    Build an AGGR_INIT_EXPR to represent the initialization.  This function
417    differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
418    to initialize another object, whereas a TARGET_EXPR can either
419    initialize another object or create its own temporary object, and as a
420    result building up a TARGET_EXPR requires that the type's destructor be
421    callable.  */
422
423 tree
424 build_aggr_init_expr (tree type, tree init)
425 {
426   tree fn;
427   tree slot;
428   tree rval;
429   int is_ctor;
430
431   /* Don't build AGGR_INIT_EXPR in a template.  */
432   if (processing_template_decl)
433     return init;
434
435   if (TREE_CODE (init) == CALL_EXPR)
436     fn = CALL_EXPR_FN (init);
437   else if (TREE_CODE (init) == AGGR_INIT_EXPR)
438     fn = AGGR_INIT_EXPR_FN (init);
439   else
440     return convert (type, init);
441
442   is_ctor = (TREE_CODE (fn) == ADDR_EXPR
443              && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
444              && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
445
446   /* We split the CALL_EXPR into its function and its arguments here.
447      Then, in expand_expr, we put them back together.  The reason for
448      this is that this expression might be a default argument
449      expression.  In that case, we need a new temporary every time the
450      expression is used.  That's what break_out_target_exprs does; it
451      replaces every AGGR_INIT_EXPR with a copy that uses a fresh
452      temporary slot.  Then, expand_expr builds up a call-expression
453      using the new slot.  */
454
455   /* If we don't need to use a constructor to create an object of this
456      type, don't mess with AGGR_INIT_EXPR.  */
457   if (is_ctor || TREE_ADDRESSABLE (type))
458     {
459       slot = build_local_temp (type);
460
461       if (TREE_CODE (init) == CALL_EXPR)
462         {
463           rval = build_aggr_init_array (void_type_node, fn, slot,
464                                         call_expr_nargs (init),
465                                         CALL_EXPR_ARGP (init));
466           AGGR_INIT_FROM_THUNK_P (rval)
467             = CALL_FROM_THUNK_P (init);
468         }
469       else
470         {
471           rval = build_aggr_init_array (void_type_node, fn, slot,
472                                         aggr_init_expr_nargs (init),
473                                         AGGR_INIT_EXPR_ARGP (init));
474           AGGR_INIT_FROM_THUNK_P (rval)
475             = AGGR_INIT_FROM_THUNK_P (init);
476         }
477       TREE_SIDE_EFFECTS (rval) = 1;
478       AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
479       TREE_NOTHROW (rval) = TREE_NOTHROW (init);
480       CALL_EXPR_LIST_INIT_P (rval) = CALL_EXPR_LIST_INIT_P (init);
481     }
482   else
483     rval = init;
484
485   return rval;
486 }
487
488 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
489    target.  TYPE is the type that this initialization should appear to
490    have.
491
492    Build an encapsulation of the initialization to perform
493    and return it so that it can be processed by language-independent
494    and language-specific expression expanders.  */
495
496 tree
497 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
498 {
499   tree rval = build_aggr_init_expr (type, init);
500   tree slot;
501
502   if (!complete_type_or_maybe_complain (type, init, complain))
503     return error_mark_node;
504
505   /* Make sure that we're not trying to create an instance of an
506      abstract class.  */
507   if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
508     return error_mark_node;
509
510   if (TREE_CODE (rval) == AGGR_INIT_EXPR)
511     slot = AGGR_INIT_EXPR_SLOT (rval);
512   else if (TREE_CODE (rval) == CALL_EXPR
513            || TREE_CODE (rval) == CONSTRUCTOR)
514     slot = build_local_temp (type);
515   else
516     return rval;
517
518   rval = build_target_expr (slot, rval, complain);
519
520   if (rval != error_mark_node)
521     TARGET_EXPR_IMPLICIT_P (rval) = 1;
522
523   return rval;
524 }
525
526 /* Subroutine of build_vec_init_expr: Build up a single element
527    intialization as a proxy for the full array initialization to get things
528    marked as used and any appropriate diagnostics.
529
530    Since we're deferring building the actual constructor calls until
531    gimplification time, we need to build one now and throw it away so
532    that the relevant constructor gets mark_used before cgraph decides
533    what functions are needed.  Here we assume that init is either
534    NULL_TREE, void_type_node (indicating value-initialization), or
535    another array to copy.  */
536
537 static tree
538 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
539 {
540   tree inner_type = strip_array_types (type);
541   vec<tree, va_gc> *argvec;
542
543   if (integer_zerop (array_type_nelts_total (type))
544       || !CLASS_TYPE_P (inner_type))
545     /* No interesting initialization to do.  */
546     return integer_zero_node;
547   else if (init == void_type_node)
548     return build_value_init (inner_type, complain);
549
550   gcc_assert (init == NULL_TREE
551               || (same_type_ignoring_top_level_qualifiers_p
552                   (type, TREE_TYPE (init))));
553
554   argvec = make_tree_vector ();
555   if (init)
556     {
557       tree init_type = strip_array_types (TREE_TYPE (init));
558       tree dummy = build_dummy_object (init_type);
559       if (!real_lvalue_p (init))
560         dummy = move (dummy);
561       argvec->quick_push (dummy);
562     }
563   init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
564                                     &argvec, inner_type, LOOKUP_NORMAL,
565                                     complain);
566   release_tree_vector (argvec);
567
568   /* For a trivial constructor, build_over_call creates a TARGET_EXPR.  But
569      we don't want one here because we aren't creating a temporary.  */
570   if (TREE_CODE (init) == TARGET_EXPR)
571     init = TARGET_EXPR_INITIAL (init);
572
573   return init;
574 }
575
576 /* Return a TARGET_EXPR which expresses the initialization of an array to
577    be named later, either default-initialization or copy-initialization
578    from another array of the same type.  */
579
580 tree
581 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
582 {
583   tree slot;
584   bool value_init = false;
585   tree elt_init = build_vec_init_elt (type, init, complain);
586
587   if (init == void_type_node)
588     {
589       value_init = true;
590       init = NULL_TREE;
591     }
592
593   slot = build_local_temp (type);
594   init = build2 (VEC_INIT_EXPR, type, slot, init);
595   TREE_SIDE_EFFECTS (init) = true;
596   SET_EXPR_LOCATION (init, input_location);
597
598   if (cxx_dialect >= cxx11
599       && potential_constant_expression (elt_init))
600     VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
601   VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
602
603   return init;
604 }
605
606 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
607    that requires a constant expression.  */
608
609 void
610 diagnose_non_constexpr_vec_init (tree expr)
611 {
612   tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
613   tree init, elt_init;
614   if (VEC_INIT_EXPR_VALUE_INIT (expr))
615     init = void_type_node;
616   else
617     init = VEC_INIT_EXPR_INIT (expr);
618
619   elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
620   require_potential_constant_expression (elt_init);
621 }
622
623 tree
624 build_array_copy (tree init)
625 {
626   return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
627 }
628
629 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
630    indicated TYPE.  */
631
632 tree
633 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
634 {
635   gcc_assert (!VOID_TYPE_P (type));
636
637   if (TREE_CODE (init) == TARGET_EXPR
638       || init == error_mark_node)
639     return init;
640   else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
641            && !VOID_TYPE_P (TREE_TYPE (init))
642            && TREE_CODE (init) != COND_EXPR
643            && TREE_CODE (init) != CONSTRUCTOR
644            && TREE_CODE (init) != VA_ARG_EXPR)
645     /* We need to build up a copy constructor call.  A void initializer
646        means we're being called from bot_manip.  COND_EXPR is a special
647        case because we already have copies on the arms and we don't want
648        another one here.  A CONSTRUCTOR is aggregate initialization, which
649        is handled separately.  A VA_ARG_EXPR is magic creation of an
650        aggregate; there's no additional work to be done.  */
651     return force_rvalue (init, complain);
652
653   return force_target_expr (type, init, complain);
654 }
655
656 /* Like the above function, but without the checking.  This function should
657    only be used by code which is deliberately trying to subvert the type
658    system, such as call_builtin_trap.  Or build_over_call, to avoid
659    infinite recursion.  */
660
661 tree
662 force_target_expr (tree type, tree init, tsubst_flags_t complain)
663 {
664   tree slot;
665
666   gcc_assert (!VOID_TYPE_P (type));
667
668   slot = build_local_temp (type);
669   return build_target_expr (slot, init, complain);
670 }
671
672 /* Like build_target_expr_with_type, but use the type of INIT.  */
673
674 tree
675 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
676 {
677   if (TREE_CODE (init) == AGGR_INIT_EXPR)
678     return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
679   else if (TREE_CODE (init) == VEC_INIT_EXPR)
680     return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
681   else
682     return build_target_expr_with_type (init, TREE_TYPE (init), complain);
683 }
684
685 tree
686 get_target_expr (tree init)
687 {
688   return get_target_expr_sfinae (init, tf_warning_or_error);
689 }
690
691 /* If EXPR is a bitfield reference, convert it to the declared type of
692    the bitfield, and return the resulting expression.  Otherwise,
693    return EXPR itself.  */
694
695 tree
696 convert_bitfield_to_declared_type (tree expr)
697 {
698   tree bitfield_type;
699
700   bitfield_type = is_bitfield_expr_with_lowered_type (expr);
701   if (bitfield_type)
702     expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
703                                       expr);
704   return expr;
705 }
706
707 /* EXPR is being used in an rvalue context.  Return a version of EXPR
708    that is marked as an rvalue.  */
709
710 tree
711 rvalue (tree expr)
712 {
713   tree type;
714
715   if (error_operand_p (expr))
716     return expr;
717
718   expr = mark_rvalue_use (expr);
719
720   /* [basic.lval]
721
722      Non-class rvalues always have cv-unqualified types.  */
723   type = TREE_TYPE (expr);
724   if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
725     type = cv_unqualified (type);
726
727   /* We need to do this for rvalue refs as well to get the right answer
728      from decltype; see c++/36628.  */
729   if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
730     expr = build1 (NON_LVALUE_EXPR, type, expr);
731   else if (type != TREE_TYPE (expr))
732     expr = build_nop (type, expr);
733
734   return expr;
735 }
736
737 \f
738 struct cplus_array_info
739 {
740   tree type;
741   tree domain;
742 };
743
744 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
745 {
746   typedef cplus_array_info *compare_type;
747
748   static hashval_t hash (tree t);
749   static bool equal (tree, cplus_array_info *);
750 };
751
752 /* Hash an ARRAY_TYPE.  K is really of type `tree'.  */
753
754 hashval_t
755 cplus_array_hasher::hash (tree t)
756 {
757   hashval_t hash;
758
759   hash = TYPE_UID (TREE_TYPE (t));
760   if (TYPE_DOMAIN (t))
761     hash ^= TYPE_UID (TYPE_DOMAIN (t));
762   return hash;
763 }
764
765 /* Compare two ARRAY_TYPEs.  K1 is really of type `tree', K2 is really
766    of type `cplus_array_info*'. */
767
768 bool
769 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
770 {
771   return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
772 }
773
774 /* Hash table containing dependent array types, which are unsuitable for
775    the language-independent type hash table.  */
776 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
777
778 /* Build an ARRAY_TYPE without laying it out.  */
779
780 static tree
781 build_min_array_type (tree elt_type, tree index_type)
782 {
783   tree t = cxx_make_type (ARRAY_TYPE);
784   TREE_TYPE (t) = elt_type;
785   TYPE_DOMAIN (t) = index_type;
786   return t;
787 }
788
789 /* Set TYPE_CANONICAL like build_array_type_1, but using
790    build_cplus_array_type.  */
791
792 static void
793 set_array_type_canon (tree t, tree elt_type, tree index_type)
794 {
795   /* Set the canonical type for this new node.  */
796   if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
797       || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
798     SET_TYPE_STRUCTURAL_EQUALITY (t);
799   else if (TYPE_CANONICAL (elt_type) != elt_type
800            || (index_type && TYPE_CANONICAL (index_type) != index_type))
801     TYPE_CANONICAL (t)
802       = build_cplus_array_type (TYPE_CANONICAL (elt_type),
803                                 index_type
804                                 ? TYPE_CANONICAL (index_type) : index_type);
805   else
806     TYPE_CANONICAL (t) = t;
807 }
808
809 /* Like build_array_type, but handle special C++ semantics: an array of a
810    variant element type is a variant of the array of the main variant of
811    the element type.  */
812
813 tree
814 build_cplus_array_type (tree elt_type, tree index_type)
815 {
816   tree t;
817
818   if (elt_type == error_mark_node || index_type == error_mark_node)
819     return error_mark_node;
820
821   bool dependent = (uses_template_parms (elt_type)
822                     || (index_type && uses_template_parms (index_type)));
823
824   if (elt_type != TYPE_MAIN_VARIANT (elt_type))
825     /* Start with an array of the TYPE_MAIN_VARIANT.  */
826     t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
827                                 index_type);
828   else if (dependent)
829     {
830       /* Since type_hash_canon calls layout_type, we need to use our own
831          hash table.  */
832       cplus_array_info cai;
833       hashval_t hash;
834
835       if (cplus_array_htab == NULL)
836         cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
837       
838       hash = TYPE_UID (elt_type);
839       if (index_type)
840         hash ^= TYPE_UID (index_type);
841       cai.type = elt_type;
842       cai.domain = index_type;
843
844       tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT); 
845       if (*e)
846         /* We have found the type: we're done.  */
847         return (tree) *e;
848       else
849         {
850           /* Build a new array type.  */
851           t = build_min_array_type (elt_type, index_type);
852
853           /* Store it in the hash table. */
854           *e = t;
855
856           /* Set the canonical type for this new node.  */
857           set_array_type_canon (t, elt_type, index_type);
858         }
859     }
860   else
861     {
862       t = build_array_type (elt_type, index_type);
863     }
864
865   /* Now check whether we already have this array variant.  */
866   if (elt_type != TYPE_MAIN_VARIANT (elt_type))
867     {
868       tree m = t;
869       for (t = m; t; t = TYPE_NEXT_VARIANT (t))
870         if (TREE_TYPE (t) == elt_type
871             && TYPE_NAME (t) == NULL_TREE
872             && TYPE_ATTRIBUTES (t) == NULL_TREE)
873           break;
874       if (!t)
875         {
876           t = build_min_array_type (elt_type, index_type);
877           set_array_type_canon (t, elt_type, index_type);
878           if (!dependent)
879             {
880               layout_type (t);
881               /* Make sure sizes are shared with the main variant.
882                  layout_type can't be called after setting TYPE_NEXT_VARIANT,
883                  as it will overwrite alignment etc. of all variants.  */
884               TYPE_SIZE (t) = TYPE_SIZE (m);
885               TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
886             }
887
888           TYPE_MAIN_VARIANT (t) = m;
889           TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
890           TYPE_NEXT_VARIANT (m) = t;
891         }
892     }
893
894   /* Avoid spurious warnings with VLAs (c++/54583).  */
895   if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
896     TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
897
898   /* Push these needs up to the ARRAY_TYPE so that initialization takes
899      place more easily.  */
900   bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
901                      = TYPE_NEEDS_CONSTRUCTING (elt_type));
902   bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
903                      = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
904
905   if (!dependent && t == TYPE_MAIN_VARIANT (t)
906       && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
907     {
908       /* The element type has been completed since the last time we saw
909          this array type; update the layout and 'tor flags for any variants
910          that need it.  */
911       layout_type (t);
912       for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
913         {
914           TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
915           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
916         }
917     }
918
919   return t;
920 }
921
922 /* Return an ARRAY_TYPE with element type ELT and length N.  */
923
924 tree
925 build_array_of_n_type (tree elt, int n)
926 {
927   return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
928 }
929
930 /* True iff T is an N3639 array of runtime bound (VLA).  These were
931    approved for C++14 but then removed.  */
932
933 bool
934 array_of_runtime_bound_p (tree t)
935 {
936   if (!t || TREE_CODE (t) != ARRAY_TYPE)
937     return false;
938   tree dom = TYPE_DOMAIN (t);
939   if (!dom)
940     return false;
941   tree max = TYPE_MAX_VALUE (dom);
942   return (!potential_rvalue_constant_expression (max)
943           || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
944 }
945
946 /* Return a reference type node referring to TO_TYPE.  If RVAL is
947    true, return an rvalue reference type, otherwise return an lvalue
948    reference type.  If a type node exists, reuse it, otherwise create
949    a new one.  */
950 tree
951 cp_build_reference_type (tree to_type, bool rval)
952 {
953   tree lvalue_ref, t;
954   lvalue_ref = build_reference_type (to_type);
955   if (!rval)
956     return lvalue_ref;
957
958   /* This code to create rvalue reference types is based on and tied
959      to the code creating lvalue reference types in the middle-end
960      functions build_reference_type_for_mode and build_reference_type.
961
962      It works by putting the rvalue reference type nodes after the
963      lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
964      they will effectively be ignored by the middle end.  */
965
966   for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
967     if (TYPE_REF_IS_RVALUE (t))
968       return t;
969
970   t = build_distinct_type_copy (lvalue_ref);
971
972   TYPE_REF_IS_RVALUE (t) = true;
973   TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
974   TYPE_NEXT_REF_TO (lvalue_ref) = t;
975
976   if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
977     SET_TYPE_STRUCTURAL_EQUALITY (t);
978   else if (TYPE_CANONICAL (to_type) != to_type)
979     TYPE_CANONICAL (t) 
980       = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
981   else
982     TYPE_CANONICAL (t) = t;
983
984   layout_type (t);
985
986   return t;
987
988 }
989
990 /* Returns EXPR cast to rvalue reference type, like std::move.  */
991
992 tree
993 move (tree expr)
994 {
995   tree type = TREE_TYPE (expr);
996   gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
997   type = cp_build_reference_type (type, /*rval*/true);
998   return build_static_cast (type, expr, tf_warning_or_error);
999 }
1000
1001 /* Used by the C++ front end to build qualified array types.  However,
1002    the C version of this function does not properly maintain canonical
1003    types (which are not used in C).  */
1004 tree
1005 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1006                         size_t /* orig_qual_indirect */)
1007 {
1008   return cp_build_qualified_type (type, type_quals);
1009 }
1010
1011 \f
1012 /* Make a variant of TYPE, qualified with the TYPE_QUALS.  Handles
1013    arrays correctly.  In particular, if TYPE is an array of T's, and
1014    TYPE_QUALS is non-empty, returns an array of qualified T's.
1015
1016    FLAGS determines how to deal with ill-formed qualifications. If
1017    tf_ignore_bad_quals is set, then bad qualifications are dropped
1018    (this is permitted if TYPE was introduced via a typedef or template
1019    type parameter). If bad qualifications are dropped and tf_warning
1020    is set, then a warning is issued for non-const qualifications.  If
1021    tf_ignore_bad_quals is not set and tf_error is not set, we
1022    return error_mark_node. Otherwise, we issue an error, and ignore
1023    the qualifications.
1024
1025    Qualification of a reference type is valid when the reference came
1026    via a typedef or template type argument. [dcl.ref] No such
1027    dispensation is provided for qualifying a function type.  [dcl.fct]
1028    DR 295 queries this and the proposed resolution brings it into line
1029    with qualifying a reference.  We implement the DR.  We also behave
1030    in a similar manner for restricting non-pointer types.  */
1031
1032 tree
1033 cp_build_qualified_type_real (tree type,
1034                               int type_quals,
1035                               tsubst_flags_t complain)
1036 {
1037   tree result;
1038   int bad_quals = TYPE_UNQUALIFIED;
1039
1040   if (type == error_mark_node)
1041     return type;
1042
1043   if (type_quals == cp_type_quals (type))
1044     return type;
1045
1046   if (TREE_CODE (type) == ARRAY_TYPE)
1047     {
1048       /* In C++, the qualification really applies to the array element
1049          type.  Obtain the appropriately qualified element type.  */
1050       tree t;
1051       tree element_type
1052         = cp_build_qualified_type_real (TREE_TYPE (type),
1053                                         type_quals,
1054                                         complain);
1055
1056       if (element_type == error_mark_node)
1057         return error_mark_node;
1058
1059       /* See if we already have an identically qualified type.  Tests
1060          should be equivalent to those in check_qualified_type.  */
1061       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1062         if (TREE_TYPE (t) == element_type
1063             && TYPE_NAME (t) == TYPE_NAME (type)
1064             && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1065             && attribute_list_equal (TYPE_ATTRIBUTES (t),
1066                                      TYPE_ATTRIBUTES (type)))
1067           break;
1068
1069       if (!t)
1070         {
1071           t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1072
1073           /* Keep the typedef name.  */
1074           if (TYPE_NAME (t) != TYPE_NAME (type))
1075             {
1076               t = build_variant_type_copy (t);
1077               TYPE_NAME (t) = TYPE_NAME (type);
1078               SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1079               TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1080             }
1081         }
1082
1083       /* Even if we already had this variant, we update
1084          TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1085          they changed since the variant was originally created.
1086
1087          This seems hokey; if there is some way to use a previous
1088          variant *without* coming through here,
1089          TYPE_NEEDS_CONSTRUCTING will never be updated.  */
1090       TYPE_NEEDS_CONSTRUCTING (t)
1091         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1092       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1093         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1094       return t;
1095     }
1096   else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1097     {
1098       tree t = PACK_EXPANSION_PATTERN (type);
1099
1100       t = cp_build_qualified_type_real (t, type_quals, complain);
1101       return make_pack_expansion (t);
1102     }
1103
1104   /* A reference or method type shall not be cv-qualified.
1105      [dcl.ref], [dcl.fct].  This used to be an error, but as of DR 295
1106      (in CD1) we always ignore extra cv-quals on functions.  */
1107   if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1108       && (TREE_CODE (type) == REFERENCE_TYPE
1109           || TREE_CODE (type) == FUNCTION_TYPE
1110           || TREE_CODE (type) == METHOD_TYPE))
1111     {
1112       if (TREE_CODE (type) == REFERENCE_TYPE)
1113         bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1114       type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1115     }
1116
1117   /* But preserve any function-cv-quals on a FUNCTION_TYPE.  */
1118   if (TREE_CODE (type) == FUNCTION_TYPE)
1119     type_quals |= type_memfn_quals (type);
1120
1121   /* A restrict-qualified type must be a pointer (or reference)
1122      to object or incomplete type. */
1123   if ((type_quals & TYPE_QUAL_RESTRICT)
1124       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1125       && TREE_CODE (type) != TYPENAME_TYPE
1126       && !POINTER_TYPE_P (type))
1127     {
1128       bad_quals |= TYPE_QUAL_RESTRICT;
1129       type_quals &= ~TYPE_QUAL_RESTRICT;
1130     }
1131
1132   if (bad_quals == TYPE_UNQUALIFIED
1133       || (complain & tf_ignore_bad_quals))
1134     /*OK*/;
1135   else if (!(complain & tf_error))
1136     return error_mark_node;
1137   else
1138     {
1139       tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1140       error ("%qV qualifiers cannot be applied to %qT",
1141              bad_type, type);
1142     }
1143
1144   /* Retrieve (or create) the appropriately qualified variant.  */
1145   result = build_qualified_type (type, type_quals);
1146
1147   /* Preserve exception specs and ref-qualifier since build_qualified_type
1148      doesn't know about them.  */
1149   if (TREE_CODE (result) == FUNCTION_TYPE
1150       || TREE_CODE (result) == METHOD_TYPE)
1151     {
1152       result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1153       result = build_ref_qualified_type (result, type_memfn_rqual (type));
1154     }
1155
1156   return result;
1157 }
1158
1159 /* Return TYPE with const and volatile removed.  */
1160
1161 tree
1162 cv_unqualified (tree type)
1163 {
1164   int quals;
1165
1166   if (type == error_mark_node)
1167     return type;
1168
1169   quals = cp_type_quals (type);
1170   quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1171   return cp_build_qualified_type (type, quals);
1172 }
1173
1174 /* Subroutine of strip_typedefs.  We want to apply to RESULT the attributes
1175    from ATTRIBS that affect type identity, and no others.  If any are not
1176    applied, set *remove_attributes to true.  */
1177
1178 static tree
1179 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1180 {
1181   tree first_ident = NULL_TREE;
1182   tree new_attribs = NULL_TREE;
1183   tree *p = &new_attribs;
1184
1185   if (OVERLOAD_TYPE_P (result))
1186     {
1187       /* On classes and enums all attributes are ingrained.  */
1188       gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1189       return result;
1190     }
1191
1192   for (tree a = attribs; a; a = TREE_CHAIN (a))
1193     {
1194       const attribute_spec *as
1195         = lookup_attribute_spec (get_attribute_name (a));
1196       if (as && as->affects_type_identity)
1197         {
1198           if (!first_ident)
1199             first_ident = a;
1200           else if (first_ident == error_mark_node)
1201             {
1202               *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1203               p = &TREE_CHAIN (*p);
1204             }
1205         }
1206       else if (first_ident)
1207         {
1208           for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1209             {
1210               *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1211               p = &TREE_CHAIN (*p);
1212             }
1213           first_ident = error_mark_node;
1214         }
1215     }
1216   if (first_ident != error_mark_node)
1217     new_attribs = first_ident;
1218
1219   if (first_ident == attribs)
1220     /* All attributes affected type identity.  */;
1221   else
1222     *remove_attributes = true;
1223
1224   return cp_build_type_attribute_variant (result, new_attribs);
1225 }
1226
1227 /* Builds a qualified variant of T that is not a typedef variant.
1228    E.g. consider the following declarations:
1229      typedef const int ConstInt;
1230      typedef ConstInt* PtrConstInt;
1231    If T is PtrConstInt, this function returns a type representing
1232      const int*.
1233    In other words, if T is a typedef, the function returns the underlying type.
1234    The cv-qualification and attributes of the type returned match the
1235    input type.
1236    They will always be compatible types.
1237    The returned type is built so that all of its subtypes
1238    recursively have their typedefs stripped as well.
1239
1240    This is different from just returning TYPE_CANONICAL (T)
1241    Because of several reasons:
1242     * If T is a type that needs structural equality
1243       its TYPE_CANONICAL (T) will be NULL.
1244     * TYPE_CANONICAL (T) desn't carry type attributes
1245       and loses template parameter names.
1246
1247    If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1248    affect type identity, and set the referent to true if any were
1249    stripped.  */
1250
1251 tree
1252 strip_typedefs (tree t, bool *remove_attributes)
1253 {
1254   tree result = NULL, type = NULL, t0 = NULL;
1255
1256   if (!t || t == error_mark_node)
1257     return t;
1258
1259   if (TREE_CODE (t) == TREE_LIST)
1260     {
1261       bool changed = false;
1262       vec<tree,va_gc> *vec = make_tree_vector ();
1263       tree r = t;
1264       for (; t; t = TREE_CHAIN (t))
1265         {
1266           gcc_assert (!TREE_PURPOSE (t));
1267           tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
1268           if (elt != TREE_VALUE (t))
1269             changed = true;
1270           vec_safe_push (vec, elt);
1271         }
1272       if (changed)
1273         r = build_tree_list_vec (vec);
1274       release_tree_vector (vec);
1275       return r;
1276     }
1277
1278   gcc_assert (TYPE_P (t));
1279
1280   if (t == TYPE_CANONICAL (t))
1281     return t;
1282
1283   if (dependent_alias_template_spec_p (t))
1284     /* DR 1558: However, if the template-id is dependent, subsequent
1285        template argument substitution still applies to the template-id.  */
1286     return t;
1287
1288   switch (TREE_CODE (t))
1289     {
1290     case POINTER_TYPE:
1291       type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1292       result = build_pointer_type (type);
1293       break;
1294     case REFERENCE_TYPE:
1295       type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1296       result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1297       break;
1298     case OFFSET_TYPE:
1299       t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1300       type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1301       result = build_offset_type (t0, type);
1302       break;
1303     case RECORD_TYPE:
1304       if (TYPE_PTRMEMFUNC_P (t))
1305         {
1306           t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
1307           result = build_ptrmemfunc_type (t0);
1308         }
1309       break;
1310     case ARRAY_TYPE:
1311       type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1312       t0  = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
1313       result = build_cplus_array_type (type, t0);
1314       break;
1315     case FUNCTION_TYPE:
1316     case METHOD_TYPE:
1317       {
1318         tree arg_types = NULL, arg_node, arg_node2, arg_type;
1319         bool changed;
1320
1321         /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1322            around the compiler (e.g. cp_parser_late_parsing_default_args), we
1323            can't expect that re-hashing a function type will find a previous
1324            equivalent type, so try to reuse the input type if nothing has
1325            changed.  If the type is itself a variant, that will change.  */
1326         bool is_variant = typedef_variant_p (t);
1327         if (remove_attributes
1328             && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1329           is_variant = true;
1330
1331         type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1332         changed = type != TREE_TYPE (t) || is_variant;
1333
1334         for (arg_node = TYPE_ARG_TYPES (t);
1335              arg_node;
1336              arg_node = TREE_CHAIN (arg_node))
1337           {
1338             if (arg_node == void_list_node)
1339               break;
1340             arg_type = strip_typedefs (TREE_VALUE (arg_node),
1341                                        remove_attributes);
1342             gcc_assert (arg_type);
1343             if (arg_type == TREE_VALUE (arg_node) && !changed)
1344               continue;
1345
1346             if (!changed)
1347               {
1348                 changed = true;
1349                 for (arg_node2 = TYPE_ARG_TYPES (t);
1350                      arg_node2 != arg_node;
1351                      arg_node2 = TREE_CHAIN (arg_node2))
1352                   arg_types
1353                     = tree_cons (TREE_PURPOSE (arg_node2),
1354                                  TREE_VALUE (arg_node2), arg_types);
1355               }
1356
1357             arg_types
1358               = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1359           }
1360
1361         if (!changed)
1362           return t;
1363
1364         if (arg_types)
1365           arg_types = nreverse (arg_types);
1366
1367         /* A list of parameters not ending with an ellipsis
1368            must end with void_list_node.  */
1369         if (arg_node)
1370           arg_types = chainon (arg_types, void_list_node);
1371
1372         if (TREE_CODE (t) == METHOD_TYPE)
1373           {
1374             tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1375             gcc_assert (class_type);
1376             result =
1377               build_method_type_directly (class_type, type,
1378                                           TREE_CHAIN (arg_types));
1379             result
1380               = build_ref_qualified_type (result, type_memfn_rqual (t));
1381           }
1382         else
1383           {
1384             result = build_function_type (type,
1385                                           arg_types);
1386             result = apply_memfn_quals (result,
1387                                         type_memfn_quals (t),
1388                                         type_memfn_rqual (t));
1389           }
1390
1391         if (TYPE_RAISES_EXCEPTIONS (t))
1392           result = build_exception_variant (result,
1393                                             TYPE_RAISES_EXCEPTIONS (t));
1394         if (TYPE_HAS_LATE_RETURN_TYPE (t))
1395           TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
1396       }
1397       break;
1398     case TYPENAME_TYPE:
1399       {
1400         tree fullname = TYPENAME_TYPE_FULLNAME (t);
1401         if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1402             && TREE_OPERAND (fullname, 1))
1403           {
1404             tree args = TREE_OPERAND (fullname, 1);
1405             tree new_args = copy_node (args);
1406             bool changed = false;
1407             for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1408               {
1409                 tree arg = TREE_VEC_ELT (args, i);
1410                 tree strip_arg;
1411                 if (TYPE_P (arg))
1412                   strip_arg = strip_typedefs (arg, remove_attributes);
1413                 else
1414                   strip_arg = strip_typedefs_expr (arg, remove_attributes);
1415                 TREE_VEC_ELT (new_args, i) = strip_arg;
1416                 if (strip_arg != arg)
1417                   changed = true;
1418               }
1419             if (changed)
1420               {
1421                 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1422                   = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1423                 fullname
1424                   = lookup_template_function (TREE_OPERAND (fullname, 0),
1425                                               new_args);
1426               }
1427             else
1428               ggc_free (new_args);
1429           }
1430         result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t),
1431                                                      remove_attributes),
1432                                      fullname, typename_type, tf_none);
1433         /* Handle 'typedef typename A::N N;'  */
1434         if (typedef_variant_p (result))
1435           result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (result)));
1436       }
1437       break;
1438     case DECLTYPE_TYPE:
1439       result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1440                                     remove_attributes);
1441       if (result == DECLTYPE_TYPE_EXPR (t))
1442         result = NULL_TREE;
1443       else
1444         result = (finish_decltype_type
1445                   (result,
1446                    DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1447                    tf_none));
1448       break;
1449     default:
1450       break;
1451     }
1452
1453   if (!result)
1454     {
1455       if (typedef_variant_p (t))
1456         {
1457           /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1458              strip typedefs with attributes.  */
1459           result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1460           result = strip_typedefs (result);
1461         }
1462       else
1463         result = TYPE_MAIN_VARIANT (t);
1464     }
1465   gcc_assert (!typedef_variant_p (result));
1466   if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1467       || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1468     {
1469       gcc_assert (TYPE_USER_ALIGN (t));
1470       if (remove_attributes)
1471         *remove_attributes = true;
1472       else
1473         {
1474           if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1475             result = build_variant_type_copy (result);
1476           else
1477             result = build_aligned_type (result, TYPE_ALIGN (t));
1478           TYPE_USER_ALIGN (result) = true;
1479         }
1480     }
1481   if (TYPE_ATTRIBUTES (t))
1482     {
1483       if (remove_attributes)
1484         result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1485                                             remove_attributes);
1486       else
1487         result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1488     }
1489   return cp_build_qualified_type (result, cp_type_quals (t));
1490 }
1491
1492 /* Like strip_typedefs above, but works on expressions, so that in
1493
1494    template<class T> struct A
1495    {
1496      typedef T TT;
1497      B<sizeof(TT)> b;
1498    };
1499
1500    sizeof(TT) is replaced by sizeof(T).  */
1501
1502 tree
1503 strip_typedefs_expr (tree t, bool *remove_attributes)
1504 {
1505   unsigned i,n;
1506   tree r, type, *ops;
1507   enum tree_code code;
1508
1509   if (t == NULL_TREE || t == error_mark_node)
1510     return t;
1511
1512   if (DECL_P (t) || CONSTANT_CLASS_P (t))
1513     return t;
1514
1515   /* Some expressions have type operands, so let's handle types here rather
1516      than check TYPE_P in multiple places below.  */
1517   if (TYPE_P (t))
1518     return strip_typedefs (t, remove_attributes);
1519
1520   code = TREE_CODE (t);
1521   switch (code)
1522     {
1523     case IDENTIFIER_NODE:
1524     case TEMPLATE_PARM_INDEX:
1525     case OVERLOAD:
1526     case BASELINK:
1527     case ARGUMENT_PACK_SELECT:
1528       return t;
1529
1530     case TRAIT_EXPR:
1531       {
1532         tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1533         tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
1534         if (type1 == TRAIT_EXPR_TYPE1 (t)
1535             && type2 == TRAIT_EXPR_TYPE2 (t))
1536           return t;
1537         r = copy_node (t);
1538         TRAIT_EXPR_TYPE1 (r) = type1;
1539         TRAIT_EXPR_TYPE2 (r) = type2;
1540         return r;
1541       }
1542
1543     case TREE_LIST:
1544       {
1545         vec<tree, va_gc> *vec = make_tree_vector ();
1546         bool changed = false;
1547         tree it;
1548         for (it = t; it; it = TREE_CHAIN (it))
1549           {
1550             tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
1551             vec_safe_push (vec, val);
1552             if (val != TREE_VALUE (t))
1553               changed = true;
1554             gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1555           }
1556         if (changed)
1557           {
1558             r = NULL_TREE;
1559             FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1560               r = tree_cons (NULL_TREE, it, r);
1561           }
1562         else
1563           r = t;
1564         release_tree_vector (vec);
1565         return r;
1566       }
1567
1568     case TREE_VEC:
1569       {
1570         bool changed = false;
1571         vec<tree, va_gc> *vec = make_tree_vector ();
1572         n = TREE_VEC_LENGTH (t);
1573         vec_safe_reserve (vec, n);
1574         for (i = 0; i < n; ++i)
1575           {
1576             tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1577                                            remove_attributes);
1578             vec->quick_push (op);
1579             if (op != TREE_VEC_ELT (t, i))
1580               changed = true;
1581           }
1582         if (changed)
1583           {
1584             r = copy_node (t);
1585             for (i = 0; i < n; ++i)
1586               TREE_VEC_ELT (r, i) = (*vec)[i];
1587             NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1588               = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1589           }
1590         else
1591           r = t;
1592         release_tree_vector (vec);
1593         return r;
1594       }
1595
1596     case CONSTRUCTOR:
1597       {
1598         bool changed = false;
1599         vec<constructor_elt, va_gc> *vec
1600           = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1601         n = CONSTRUCTOR_NELTS (t);
1602         type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1603         for (i = 0; i < n; ++i)
1604           {
1605             constructor_elt *e = &(*vec)[i];
1606             tree op = strip_typedefs_expr (e->value, remove_attributes);
1607             if (op != e->value)
1608               {
1609                 changed = true;
1610                 e->value = op;
1611               }
1612             gcc_checking_assert
1613               (e->index == strip_typedefs_expr (e->index, remove_attributes));
1614           }
1615
1616         if (!changed && type == TREE_TYPE (t))
1617           {
1618             vec_free (vec);
1619             return t;
1620           }
1621         else
1622           {
1623             r = copy_node (t);
1624             TREE_TYPE (r) = type;
1625             CONSTRUCTOR_ELTS (r) = vec;
1626             return r;
1627           }
1628       }
1629
1630     case LAMBDA_EXPR:
1631       error ("lambda-expression in a constant expression");
1632       return error_mark_node;
1633
1634     default:
1635       break;
1636     }
1637
1638   gcc_assert (EXPR_P (t));
1639
1640   n = TREE_OPERAND_LENGTH (t);
1641   ops = XALLOCAVEC (tree, n);
1642   type = TREE_TYPE (t);
1643
1644   switch (code)
1645     {
1646     CASE_CONVERT:
1647     case IMPLICIT_CONV_EXPR:
1648     case DYNAMIC_CAST_EXPR:
1649     case STATIC_CAST_EXPR:
1650     case CONST_CAST_EXPR:
1651     case REINTERPRET_CAST_EXPR:
1652     case CAST_EXPR:
1653     case NEW_EXPR:
1654       type = strip_typedefs (type, remove_attributes);
1655       /* fallthrough */
1656
1657     default:
1658       for (i = 0; i < n; ++i)
1659         ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
1660       break;
1661     }
1662
1663   /* If nothing changed, return t.  */
1664   for (i = 0; i < n; ++i)
1665     if (ops[i] != TREE_OPERAND (t, i))
1666       break;
1667   if (i == n && type == TREE_TYPE (t))
1668     return t;
1669
1670   r = copy_node (t);
1671   TREE_TYPE (r) = type;
1672   for (i = 0; i < n; ++i)
1673     TREE_OPERAND (r, i) = ops[i];
1674   return r;
1675 }
1676
1677 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1678    graph dominated by T.  If BINFO is NULL, TYPE is a dependent base,
1679    and we do a shallow copy.  If BINFO is non-NULL, we do a deep copy.
1680    VIRT indicates whether TYPE is inherited virtually or not.
1681    IGO_PREV points at the previous binfo of the inheritance graph
1682    order chain.  The newly copied binfo's TREE_CHAIN forms this
1683    ordering.
1684
1685    The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1686    correct order. That is in the order the bases themselves should be
1687    constructed in.
1688
1689    The BINFO_INHERITANCE of a virtual base class points to the binfo
1690    of the most derived type. ??? We could probably change this so that
1691    BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1692    remove a field.  They currently can only differ for primary virtual
1693    virtual bases.  */
1694
1695 tree
1696 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1697 {
1698   tree new_binfo;
1699
1700   if (virt)
1701     {
1702       /* See if we've already made this virtual base.  */
1703       new_binfo = binfo_for_vbase (type, t);
1704       if (new_binfo)
1705         return new_binfo;
1706     }
1707
1708   new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1709   BINFO_TYPE (new_binfo) = type;
1710
1711   /* Chain it into the inheritance graph.  */
1712   TREE_CHAIN (*igo_prev) = new_binfo;
1713   *igo_prev = new_binfo;
1714
1715   if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1716     {
1717       int ix;
1718       tree base_binfo;
1719
1720       gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1721
1722       BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1723       BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1724
1725       /* We do not need to copy the accesses, as they are read only.  */
1726       BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1727
1728       /* Recursively copy base binfos of BINFO.  */
1729       for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1730         {
1731           tree new_base_binfo;
1732           new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1733                                        t, igo_prev,
1734                                        BINFO_VIRTUAL_P (base_binfo));
1735
1736           if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1737             BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1738           BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1739         }
1740     }
1741   else
1742     BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1743
1744   if (virt)
1745     {
1746       /* Push it onto the list after any virtual bases it contains
1747          will have been pushed.  */
1748       CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1749       BINFO_VIRTUAL_P (new_binfo) = 1;
1750       BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1751     }
1752
1753   return new_binfo;
1754 }
1755 \f
1756 /* Hashing of lists so that we don't make duplicates.
1757    The entry point is `list_hash_canon'.  */
1758
1759 struct list_proxy
1760 {
1761   tree purpose;
1762   tree value;
1763   tree chain;
1764 };
1765
1766 struct list_hasher : ggc_ptr_hash<tree_node>
1767 {
1768   typedef list_proxy *compare_type;
1769
1770   static hashval_t hash (tree);
1771   static bool equal (tree, list_proxy *);
1772 };
1773
1774 /* Now here is the hash table.  When recording a list, it is added
1775    to the slot whose index is the hash code mod the table size.
1776    Note that the hash table is used for several kinds of lists.
1777    While all these live in the same table, they are completely independent,
1778    and the hash code is computed differently for each of these.  */
1779
1780 static GTY (()) hash_table<list_hasher> *list_hash_table;
1781
1782 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1783    for a node we are thinking about adding).  */
1784
1785 bool
1786 list_hasher::equal (tree t, list_proxy *proxy)
1787 {
1788   return (TREE_VALUE (t) == proxy->value
1789           && TREE_PURPOSE (t) == proxy->purpose
1790           && TREE_CHAIN (t) == proxy->chain);
1791 }
1792
1793 /* Compute a hash code for a list (chain of TREE_LIST nodes
1794    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1795    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
1796
1797 static hashval_t
1798 list_hash_pieces (tree purpose, tree value, tree chain)
1799 {
1800   hashval_t hashcode = 0;
1801
1802   if (chain)
1803     hashcode += TREE_HASH (chain);
1804
1805   if (value)
1806     hashcode += TREE_HASH (value);
1807   else
1808     hashcode += 1007;
1809   if (purpose)
1810     hashcode += TREE_HASH (purpose);
1811   else
1812     hashcode += 1009;
1813   return hashcode;
1814 }
1815
1816 /* Hash an already existing TREE_LIST.  */
1817
1818 hashval_t
1819 list_hasher::hash (tree t)
1820 {
1821   return list_hash_pieces (TREE_PURPOSE (t),
1822                            TREE_VALUE (t),
1823                            TREE_CHAIN (t));
1824 }
1825
1826 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1827    object for an identical list if one already exists.  Otherwise, build a
1828    new one, and record it as the canonical object.  */
1829
1830 tree
1831 hash_tree_cons (tree purpose, tree value, tree chain)
1832 {
1833   int hashcode = 0;
1834   tree *slot;
1835   struct list_proxy proxy;
1836
1837   /* Hash the list node.  */
1838   hashcode = list_hash_pieces (purpose, value, chain);
1839   /* Create a proxy for the TREE_LIST we would like to create.  We
1840      don't actually create it so as to avoid creating garbage.  */
1841   proxy.purpose = purpose;
1842   proxy.value = value;
1843   proxy.chain = chain;
1844   /* See if it is already in the table.  */
1845   slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
1846   /* If not, create a new node.  */
1847   if (!*slot)
1848     *slot = tree_cons (purpose, value, chain);
1849   return (tree) *slot;
1850 }
1851
1852 /* Constructor for hashed lists.  */
1853
1854 tree
1855 hash_tree_chain (tree value, tree chain)
1856 {
1857   return hash_tree_cons (NULL_TREE, value, chain);
1858 }
1859 \f
1860 void
1861 debug_binfo (tree elem)
1862 {
1863   HOST_WIDE_INT n;
1864   tree virtuals;
1865
1866   fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1867            "\nvtable type:\n",
1868            TYPE_NAME_STRING (BINFO_TYPE (elem)),
1869            TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1870   debug_tree (BINFO_TYPE (elem));
1871   if (BINFO_VTABLE (elem))
1872     fprintf (stderr, "vtable decl \"%s\"\n",
1873              IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1874   else
1875     fprintf (stderr, "no vtable decl yet\n");
1876   fprintf (stderr, "virtuals:\n");
1877   virtuals = BINFO_VIRTUALS (elem);
1878   n = 0;
1879
1880   while (virtuals)
1881     {
1882       tree fndecl = TREE_VALUE (virtuals);
1883       fprintf (stderr, "%s [%ld =? %ld]\n",
1884                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1885                (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1886       ++n;
1887       virtuals = TREE_CHAIN (virtuals);
1888     }
1889 }
1890
1891 /* Build a representation for the qualified name SCOPE::NAME.  TYPE is
1892    the type of the result expression, if known, or NULL_TREE if the
1893    resulting expression is type-dependent.  If TEMPLATE_P is true,
1894    NAME is known to be a template because the user explicitly used the
1895    "template" keyword after the "::".
1896
1897    All SCOPE_REFs should be built by use of this function.  */
1898
1899 tree
1900 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1901 {
1902   tree t;
1903   if (type == error_mark_node
1904       || scope == error_mark_node
1905       || name == error_mark_node)
1906     return error_mark_node;
1907   t = build2 (SCOPE_REF, type, scope, name);
1908   QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1909   PTRMEM_OK_P (t) = true;
1910   if (type)
1911     t = convert_from_reference (t);
1912   return t;
1913 }
1914
1915 /* Like check_qualified_type, but also check ref-qualifier and exception
1916    specification.  */
1917
1918 static bool
1919 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
1920                          cp_ref_qualifier rqual, tree raises)
1921 {
1922   return (check_qualified_type (cand, base, type_quals)
1923           && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
1924                                 ce_exact)
1925           && type_memfn_rqual (cand) == rqual);
1926 }
1927
1928 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL.  */
1929
1930 tree
1931 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
1932 {
1933   tree t;
1934
1935   if (rqual == type_memfn_rqual (type))
1936     return type;
1937
1938   int type_quals = TYPE_QUALS (type);
1939   tree raises = TYPE_RAISES_EXCEPTIONS (type);
1940   for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1941     if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
1942       return t;
1943
1944   t = build_variant_type_copy (type);
1945   switch (rqual)
1946     {
1947     case REF_QUAL_RVALUE:
1948       FUNCTION_RVALUE_QUALIFIED (t) = 1;
1949       FUNCTION_REF_QUALIFIED (t) = 1;
1950       break;
1951     case REF_QUAL_LVALUE:
1952       FUNCTION_RVALUE_QUALIFIED (t) = 0;
1953       FUNCTION_REF_QUALIFIED (t) = 1;
1954       break;
1955     default:
1956       FUNCTION_REF_QUALIFIED (t) = 0;
1957       break;
1958     }
1959
1960   if (TYPE_STRUCTURAL_EQUALITY_P (type))
1961     /* Propagate structural equality. */
1962     SET_TYPE_STRUCTURAL_EQUALITY (t);
1963   else if (TYPE_CANONICAL (type) != type)
1964     /* Build the underlying canonical type, since it is different
1965        from TYPE. */
1966     TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
1967                                                    rqual);
1968   else
1969     /* T is its own canonical type. */
1970     TYPE_CANONICAL (t) = t;
1971
1972   return t;
1973 }
1974
1975 /* Returns nonzero if X is an expression for a (possibly overloaded)
1976    function.  If "f" is a function or function template, "f", "c->f",
1977    "c.f", "C::f", and "f<int>" will all be considered possibly
1978    overloaded functions.  Returns 2 if the function is actually
1979    overloaded, i.e., if it is impossible to know the type of the
1980    function without performing overload resolution.  */
1981  
1982 int
1983 is_overloaded_fn (tree x)
1984 {
1985   /* A baselink is also considered an overloaded function.  */
1986   if (TREE_CODE (x) == OFFSET_REF
1987       || TREE_CODE (x) == COMPONENT_REF)
1988     x = TREE_OPERAND (x, 1);
1989   if (BASELINK_P (x))
1990     x = BASELINK_FUNCTIONS (x);
1991   if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1992     x = TREE_OPERAND (x, 0);
1993   if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1994       || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1995     return 2;
1996   return  (TREE_CODE (x) == FUNCTION_DECL
1997            || TREE_CODE (x) == OVERLOAD);
1998 }
1999
2000 /* X is the CALL_EXPR_FN of a CALL_EXPR.  If X represents a dependent name
2001    (14.6.2), return the IDENTIFIER_NODE for that name.  Otherwise, return
2002    NULL_TREE.  */
2003
2004 tree
2005 dependent_name (tree x)
2006 {
2007   if (identifier_p (x))
2008     return x;
2009   if (TREE_CODE (x) != COMPONENT_REF
2010       && TREE_CODE (x) != OFFSET_REF
2011       && TREE_CODE (x) != BASELINK
2012       && is_overloaded_fn (x))
2013     return DECL_NAME (get_first_fn (x));
2014   return NULL_TREE;
2015 }
2016
2017 /* Returns true iff X is an expression for an overloaded function
2018    whose type cannot be known without performing overload
2019    resolution.  */
2020
2021 bool
2022 really_overloaded_fn (tree x)
2023 {
2024   return is_overloaded_fn (x) == 2;
2025 }
2026
2027 tree
2028 get_fns (tree from)
2029 {
2030   gcc_assert (is_overloaded_fn (from));
2031   /* A baselink is also considered an overloaded function.  */
2032   if (TREE_CODE (from) == OFFSET_REF
2033       || TREE_CODE (from) == COMPONENT_REF)
2034     from = TREE_OPERAND (from, 1);
2035   if (BASELINK_P (from))
2036     from = BASELINK_FUNCTIONS (from);
2037   if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2038     from = TREE_OPERAND (from, 0);
2039   return from;
2040 }
2041
2042 tree
2043 get_first_fn (tree from)
2044 {
2045   return OVL_CURRENT (get_fns (from));
2046 }
2047
2048 /* Return a new OVL node, concatenating it with the old one.  */
2049
2050 tree
2051 ovl_cons (tree decl, tree chain)
2052 {
2053   tree result = make_node (OVERLOAD);
2054   TREE_TYPE (result) = unknown_type_node;
2055   OVL_FUNCTION (result) = decl;
2056   TREE_CHAIN (result) = chain;
2057
2058   return result;
2059 }
2060
2061 /* Build a new overloaded function. If this is the first one,
2062    just return it; otherwise, ovl_cons the _DECLs */
2063
2064 tree
2065 build_overload (tree decl, tree chain)
2066 {
2067   if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
2068     return decl;
2069   return ovl_cons (decl, chain);
2070 }
2071
2072 /* Return the scope where the overloaded functions OVL were found.  */
2073
2074 tree
2075 ovl_scope (tree ovl)
2076 {
2077   if (TREE_CODE (ovl) == OFFSET_REF
2078       || TREE_CODE (ovl) == COMPONENT_REF)
2079     ovl = TREE_OPERAND (ovl, 1);
2080   if (TREE_CODE (ovl) == BASELINK)
2081     return BINFO_TYPE (BASELINK_BINFO (ovl));
2082   if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2083     ovl = TREE_OPERAND (ovl, 0);
2084   /* Skip using-declarations.  */
2085   while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
2086     ovl = OVL_CHAIN (ovl);
2087   return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
2088 }
2089
2090 /* Return TRUE if FN is a non-static member function, FALSE otherwise.
2091    This function looks into BASELINK and OVERLOAD nodes.  */
2092
2093 bool
2094 non_static_member_function_p (tree fn)
2095 {
2096   if (fn == NULL_TREE)
2097     return false;
2098
2099   if (is_overloaded_fn (fn))
2100     fn = get_first_fn (fn);
2101
2102   return (DECL_P (fn)
2103           && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn));
2104 }
2105
2106 \f
2107 #define PRINT_RING_SIZE 4
2108
2109 static const char *
2110 cxx_printable_name_internal (tree decl, int v, bool translate)
2111 {
2112   static unsigned int uid_ring[PRINT_RING_SIZE];
2113   static char *print_ring[PRINT_RING_SIZE];
2114   static bool trans_ring[PRINT_RING_SIZE];
2115   static int ring_counter;
2116   int i;
2117
2118   /* Only cache functions.  */
2119   if (v < 2
2120       || TREE_CODE (decl) != FUNCTION_DECL
2121       || DECL_LANG_SPECIFIC (decl) == 0)
2122     return lang_decl_name (decl, v, translate);
2123
2124   /* See if this print name is lying around.  */
2125   for (i = 0; i < PRINT_RING_SIZE; i++)
2126     if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2127       /* yes, so return it.  */
2128       return print_ring[i];
2129
2130   if (++ring_counter == PRINT_RING_SIZE)
2131     ring_counter = 0;
2132
2133   if (current_function_decl != NULL_TREE)
2134     {
2135       /* There may be both translated and untranslated versions of the
2136          name cached.  */
2137       for (i = 0; i < 2; i++)
2138         {
2139           if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2140             ring_counter += 1;
2141           if (ring_counter == PRINT_RING_SIZE)
2142             ring_counter = 0;
2143         }
2144       gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2145     }
2146
2147   free (print_ring[ring_counter]);
2148
2149   print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2150   uid_ring[ring_counter] = DECL_UID (decl);
2151   trans_ring[ring_counter] = translate;
2152   return print_ring[ring_counter];
2153 }
2154
2155 const char *
2156 cxx_printable_name (tree decl, int v)
2157 {
2158   return cxx_printable_name_internal (decl, v, false);
2159 }
2160
2161 const char *
2162 cxx_printable_name_translate (tree decl, int v)
2163 {
2164   return cxx_printable_name_internal (decl, v, true);
2165 }
2166 \f
2167 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2168    listed in RAISES.  */
2169
2170 tree
2171 build_exception_variant (tree type, tree raises)
2172 {
2173   tree v;
2174   int type_quals;
2175
2176   if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2177     return type;
2178
2179   type_quals = TYPE_QUALS (type);
2180   cp_ref_qualifier rqual = type_memfn_rqual (type);
2181   for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2182     if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2183       return v;
2184
2185   /* Need to build a new variant.  */
2186   v = build_variant_type_copy (type);
2187   TYPE_RAISES_EXCEPTIONS (v) = raises;
2188   return v;
2189 }
2190
2191 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2192    BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2193    arguments.  */
2194
2195 tree
2196 bind_template_template_parm (tree t, tree newargs)
2197 {
2198   tree decl = TYPE_NAME (t);
2199   tree t2;
2200
2201   t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2202   decl = build_decl (input_location,
2203                      TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2204
2205   /* These nodes have to be created to reflect new TYPE_DECL and template
2206      arguments.  */
2207   TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2208   TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2209   TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2210     = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2211
2212   TREE_TYPE (decl) = t2;
2213   TYPE_NAME (t2) = decl;
2214   TYPE_STUB_DECL (t2) = decl;
2215   TYPE_SIZE (t2) = 0;
2216   SET_TYPE_STRUCTURAL_EQUALITY (t2);
2217
2218   return t2;
2219 }
2220
2221 /* Called from count_trees via walk_tree.  */
2222
2223 static tree
2224 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2225 {
2226   ++*((int *) data);
2227
2228   if (TYPE_P (*tp))
2229     *walk_subtrees = 0;
2230
2231   return NULL_TREE;
2232 }
2233
2234 /* Debugging function for measuring the rough complexity of a tree
2235    representation.  */
2236
2237 int
2238 count_trees (tree t)
2239 {
2240   int n_trees = 0;
2241   cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2242   return n_trees;
2243 }
2244
2245 /* Called from verify_stmt_tree via walk_tree.  */
2246
2247 static tree
2248 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2249 {
2250   tree t = *tp;
2251   hash_table<nofree_ptr_hash <tree_node> > *statements
2252       = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2253   tree_node **slot;
2254
2255   if (!STATEMENT_CODE_P (TREE_CODE (t)))
2256     return NULL_TREE;
2257
2258   /* If this statement is already present in the hash table, then
2259      there is a circularity in the statement tree.  */
2260   gcc_assert (!statements->find (t));
2261
2262   slot = statements->find_slot (t, INSERT);
2263   *slot = t;
2264
2265   return NULL_TREE;
2266 }
2267
2268 /* Debugging function to check that the statement T has not been
2269    corrupted.  For now, this function simply checks that T contains no
2270    circularities.  */
2271
2272 void
2273 verify_stmt_tree (tree t)
2274 {
2275   hash_table<nofree_ptr_hash <tree_node> > statements (37);
2276   cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2277 }
2278
2279 /* Check if the type T depends on a type with no linkage and if so, return
2280    it.  If RELAXED_P then do not consider a class type declared within
2281    a vague-linkage function to have no linkage.  */
2282
2283 tree
2284 no_linkage_check (tree t, bool relaxed_p)
2285 {
2286   tree r;
2287
2288   /* There's no point in checking linkage on template functions; we
2289      can't know their complete types.  */
2290   if (processing_template_decl)
2291     return NULL_TREE;
2292
2293   switch (TREE_CODE (t))
2294     {
2295     case RECORD_TYPE:
2296       if (TYPE_PTRMEMFUNC_P (t))
2297         goto ptrmem;
2298       /* Lambda types that don't have mangling scope have no linkage.  We
2299          check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2300          when we get here from pushtag none of the lambda information is
2301          set up yet, so we want to assume that the lambda has linkage and
2302          fix it up later if not.  */
2303       if (CLASSTYPE_LAMBDA_EXPR (t)
2304           && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2305           && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2306         return t;
2307       /* Fall through.  */
2308     case UNION_TYPE:
2309       if (!CLASS_TYPE_P (t))
2310         return NULL_TREE;
2311       /* Fall through.  */
2312     case ENUMERAL_TYPE:
2313       /* Only treat anonymous types as having no linkage if they're at
2314          namespace scope.  This is core issue 966.  */
2315       if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2316         return t;
2317
2318       for (r = CP_TYPE_CONTEXT (t); ; )
2319         {
2320           /* If we're a nested type of a !TREE_PUBLIC class, we might not
2321              have linkage, or we might just be in an anonymous namespace.
2322              If we're in a TREE_PUBLIC class, we have linkage.  */
2323           if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2324             return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2325           else if (TREE_CODE (r) == FUNCTION_DECL)
2326             {
2327               if (!relaxed_p || !vague_linkage_p (r))
2328                 return t;
2329               else
2330                 r = CP_DECL_CONTEXT (r);
2331             }
2332           else
2333             break;
2334         }
2335
2336       return NULL_TREE;
2337
2338     case ARRAY_TYPE:
2339     case POINTER_TYPE:
2340     case REFERENCE_TYPE:
2341     case VECTOR_TYPE:
2342       return no_linkage_check (TREE_TYPE (t), relaxed_p);
2343
2344     case OFFSET_TYPE:
2345     ptrmem:
2346       r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2347                             relaxed_p);
2348       if (r)
2349         return r;
2350       return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2351
2352     case METHOD_TYPE:
2353     case FUNCTION_TYPE:
2354       {
2355         tree parm = TYPE_ARG_TYPES (t);
2356         if (TREE_CODE (t) == METHOD_TYPE)
2357           /* The 'this' pointer isn't interesting; a method has the same
2358              linkage (or lack thereof) as its enclosing class.  */
2359           parm = TREE_CHAIN (parm);
2360         for (;
2361              parm && parm != void_list_node;
2362              parm = TREE_CHAIN (parm))
2363           {
2364             r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2365             if (r)
2366               return r;
2367           }
2368         return no_linkage_check (TREE_TYPE (t), relaxed_p);
2369       }
2370
2371     default:
2372       return NULL_TREE;
2373     }
2374 }
2375
2376 extern int depth_reached;
2377
2378 void
2379 cxx_print_statistics (void)
2380 {
2381   print_search_statistics ();
2382   print_class_statistics ();
2383   print_template_statistics ();
2384   if (GATHER_STATISTICS)
2385     fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2386              depth_reached);
2387 }
2388
2389 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2390    (which is an ARRAY_TYPE).  This counts only elements of the top
2391    array.  */
2392
2393 tree
2394 array_type_nelts_top (tree type)
2395 {
2396   return fold_build2_loc (input_location,
2397                       PLUS_EXPR, sizetype,
2398                       array_type_nelts (type),
2399                       size_one_node);
2400 }
2401
2402 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2403    (which is an ARRAY_TYPE).  This one is a recursive count of all
2404    ARRAY_TYPEs that are clumped together.  */
2405
2406 tree
2407 array_type_nelts_total (tree type)
2408 {
2409   tree sz = array_type_nelts_top (type);
2410   type = TREE_TYPE (type);
2411   while (TREE_CODE (type) == ARRAY_TYPE)
2412     {
2413       tree n = array_type_nelts_top (type);
2414       sz = fold_build2_loc (input_location,
2415                         MULT_EXPR, sizetype, sz, n);
2416       type = TREE_TYPE (type);
2417     }
2418   return sz;
2419 }
2420
2421 /* Called from break_out_target_exprs via mapcar.  */
2422
2423 static tree
2424 bot_manip (tree* tp, int* walk_subtrees, void* data)
2425 {
2426   splay_tree target_remap = ((splay_tree) data);
2427   tree t = *tp;
2428
2429   if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2430     {
2431       /* There can't be any TARGET_EXPRs or their slot variables below this
2432          point.  But we must make a copy, in case subsequent processing
2433          alters any part of it.  For example, during gimplification a cast
2434          of the form (T) &X::f (where "f" is a member function) will lead
2435          to replacing the PTRMEM_CST for &X::f with a VAR_DECL.  */
2436       *walk_subtrees = 0;
2437       *tp = unshare_expr (t);
2438       return NULL_TREE;
2439     }
2440   if (TREE_CODE (t) == TARGET_EXPR)
2441     {
2442       tree u;
2443
2444       if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2445         {
2446           u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2447                                tf_warning_or_error);
2448           if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2449             AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2450         }
2451       else
2452         u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2453                                          tf_warning_or_error);
2454
2455       TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2456       TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2457       TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2458
2459       /* Map the old variable to the new one.  */
2460       splay_tree_insert (target_remap,
2461                          (splay_tree_key) TREE_OPERAND (t, 0),
2462                          (splay_tree_value) TREE_OPERAND (u, 0));
2463
2464       TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2465
2466       /* Replace the old expression with the new version.  */
2467       *tp = u;
2468       /* We don't have to go below this point; the recursive call to
2469          break_out_target_exprs will have handled anything below this
2470          point.  */
2471       *walk_subtrees = 0;
2472       return NULL_TREE;
2473     }
2474   if (TREE_CODE (*tp) == SAVE_EXPR)
2475     {
2476       t = *tp;
2477       splay_tree_node n = splay_tree_lookup (target_remap,
2478                                              (splay_tree_key) t);
2479       if (n)
2480         {
2481           *tp = (tree)n->value;
2482           *walk_subtrees = 0;
2483         }
2484       else
2485         {
2486           copy_tree_r (tp, walk_subtrees, NULL);
2487           splay_tree_insert (target_remap,
2488                              (splay_tree_key)t,
2489                              (splay_tree_value)*tp);
2490           /* Make sure we don't remap an already-remapped SAVE_EXPR.  */
2491           splay_tree_insert (target_remap,
2492                              (splay_tree_key)*tp,
2493                              (splay_tree_value)*tp);
2494         }
2495       return NULL_TREE;
2496     }
2497
2498   /* Make a copy of this node.  */
2499   t = copy_tree_r (tp, walk_subtrees, NULL);
2500   if (TREE_CODE (*tp) == CALL_EXPR)
2501     {
2502       set_flags_from_callee (*tp);
2503
2504       /* builtin_LINE and builtin_FILE get the location where the default
2505          argument is expanded, not where the call was written.  */
2506       tree callee = get_callee_fndecl (*tp);
2507       if (callee && DECL_BUILT_IN (callee))
2508         switch (DECL_FUNCTION_CODE (callee))
2509           {
2510           case BUILT_IN_FILE:
2511           case BUILT_IN_LINE:
2512             SET_EXPR_LOCATION (*tp, input_location);
2513           default:
2514             break;
2515           }
2516     }
2517   return t;
2518 }
2519
2520 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2521    DATA is really a splay-tree mapping old variables to new
2522    variables.  */
2523
2524 static tree
2525 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2526 {
2527   splay_tree target_remap = ((splay_tree) data);
2528
2529   if (VAR_P (*t))
2530     {
2531       splay_tree_node n = splay_tree_lookup (target_remap,
2532                                              (splay_tree_key) *t);
2533       if (n)
2534         *t = (tree) n->value;
2535     }
2536   else if (TREE_CODE (*t) == PARM_DECL
2537            && DECL_NAME (*t) == this_identifier
2538            && !DECL_CONTEXT (*t))
2539     {
2540       /* In an NSDMI we need to replace the 'this' parameter we used for
2541          parsing with the real one for this function.  */
2542       *t = current_class_ptr;
2543     }
2544   else if (TREE_CODE (*t) == CONVERT_EXPR
2545            && CONVERT_EXPR_VBASE_PATH (*t))
2546     {
2547       /* In an NSDMI build_base_path defers building conversions to virtual
2548          bases, and we handle it here.  */
2549       tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2550       vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2551       int i; tree binfo;
2552       FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2553         if (BINFO_TYPE (binfo) == basetype)
2554           break;
2555       *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2556                             tf_warning_or_error);
2557     }
2558
2559   return NULL_TREE;
2560 }
2561
2562 /* When we parse a default argument expression, we may create
2563    temporary variables via TARGET_EXPRs.  When we actually use the
2564    default-argument expression, we make a copy of the expression
2565    and replace the temporaries with appropriate local versions.  */
2566
2567 tree
2568 break_out_target_exprs (tree t)
2569 {
2570   static int target_remap_count;
2571   static splay_tree target_remap;
2572
2573   if (!target_remap_count++)
2574     target_remap = splay_tree_new (splay_tree_compare_pointers,
2575                                    /*splay_tree_delete_key_fn=*/NULL,
2576                                    /*splay_tree_delete_value_fn=*/NULL);
2577   cp_walk_tree (&t, bot_manip, target_remap, NULL);
2578   cp_walk_tree (&t, bot_replace, target_remap, NULL);
2579
2580   if (!--target_remap_count)
2581     {
2582       splay_tree_delete (target_remap);
2583       target_remap = NULL;
2584     }
2585
2586   return t;
2587 }
2588
2589 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
2590    which we expect to have type TYPE.  */
2591
2592 tree
2593 build_ctor_subob_ref (tree index, tree type, tree obj)
2594 {
2595   if (index == NULL_TREE)
2596     /* Can't refer to a particular member of a vector.  */
2597     obj = NULL_TREE;
2598   else if (TREE_CODE (index) == INTEGER_CST)
2599     obj = cp_build_array_ref (input_location, obj, index, tf_none);
2600   else
2601     obj = build_class_member_access_expr (obj, index, NULL_TREE,
2602                                           /*reference*/false, tf_none);
2603   if (obj)
2604     {
2605       tree objtype = TREE_TYPE (obj);
2606       if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
2607         {
2608           /* When the destination object refers to a flexible array member
2609              verify that it matches the type of the source object except
2610              for its domain and qualifiers.  */
2611           gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
2612                                  TYPE_MAIN_VARIANT (objtype),
2613                                  COMPARE_REDECLARATION));
2614         }
2615       else
2616         gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
2617     }
2618
2619   return obj;
2620 }
2621
2622 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
2623    build up subexpressions as we go deeper.  */
2624
2625 static tree
2626 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
2627 {
2628   tree obj = static_cast<tree>(data_);
2629
2630   if (TREE_CONSTANT (*t))
2631     {
2632       *walk_subtrees = false;
2633       return NULL_TREE;
2634     }
2635
2636   switch (TREE_CODE (*t))
2637     {
2638     case PLACEHOLDER_EXPR:
2639       {
2640         tree x = obj;
2641         for (; !(same_type_ignoring_top_level_qualifiers_p
2642                  (TREE_TYPE (*t), TREE_TYPE (x)));
2643              x = TREE_OPERAND (x, 0))
2644           gcc_assert (TREE_CODE (x) == COMPONENT_REF);
2645         *t = x;
2646         *walk_subtrees = false;
2647       }
2648       break;
2649
2650     case CONSTRUCTOR:
2651       {
2652         constructor_elt *ce;
2653         vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
2654         for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
2655           {
2656             tree *valp = &ce->value;
2657             tree type = TREE_TYPE (*valp);
2658             tree subob = obj;
2659
2660             if (TREE_CODE (*valp) == CONSTRUCTOR
2661                 && AGGREGATE_TYPE_P (type))
2662               {
2663                 /* If we're looking at the initializer for OBJ, then build
2664                    a sub-object reference.  If we're looking at an
2665                    initializer for another object, just pass OBJ down.  */
2666                 if (same_type_ignoring_top_level_qualifiers_p
2667                     (TREE_TYPE (*t), TREE_TYPE (obj)))
2668                   subob = build_ctor_subob_ref (ce->index, type, obj);
2669                 if (TREE_CODE (*valp) == TARGET_EXPR)
2670                   valp = &TARGET_EXPR_INITIAL (*valp);
2671               }
2672
2673             cp_walk_tree (valp, replace_placeholders_r,
2674                           subob, NULL);
2675           }
2676         *walk_subtrees = false;
2677         break;
2678       }
2679
2680     default:
2681       break;
2682     }
2683
2684   return NULL_TREE;
2685 }
2686
2687 tree
2688 replace_placeholders (tree exp, tree obj)
2689 {
2690   tree *tp = &exp;
2691   if (TREE_CODE (exp) == TARGET_EXPR)
2692     tp = &TARGET_EXPR_INITIAL (exp);
2693   cp_walk_tree (tp, replace_placeholders_r, obj, NULL);
2694   return exp;
2695 }
2696
2697 /* Similar to `build_nt', but for template definitions of dependent
2698    expressions  */
2699
2700 tree
2701 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2702 {
2703   tree t;
2704   int length;
2705   int i;
2706   va_list p;
2707
2708   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2709
2710   va_start (p, code);
2711
2712   t = make_node (code);
2713   SET_EXPR_LOCATION (t, loc);
2714   length = TREE_CODE_LENGTH (code);
2715
2716   for (i = 0; i < length; i++)
2717     {
2718       tree x = va_arg (p, tree);
2719       TREE_OPERAND (t, i) = x;
2720     }
2721
2722   va_end (p);
2723   return t;
2724 }
2725
2726
2727 /* Similar to `build', but for template definitions.  */
2728
2729 tree
2730 build_min (enum tree_code code, tree tt, ...)
2731 {
2732   tree t;
2733   int length;
2734   int i;
2735   va_list p;
2736
2737   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2738
2739   va_start (p, tt);
2740
2741   t = make_node (code);
2742   length = TREE_CODE_LENGTH (code);
2743   TREE_TYPE (t) = tt;
2744
2745   for (i = 0; i < length; i++)
2746     {
2747       tree x = va_arg (p, tree);
2748       TREE_OPERAND (t, i) = x;
2749       if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2750         TREE_SIDE_EFFECTS (t) = 1;
2751     }
2752
2753   va_end (p);
2754   return t;
2755 }
2756
2757 /* Similar to `build', but for template definitions of non-dependent
2758    expressions. NON_DEP is the non-dependent expression that has been
2759    built.  */
2760
2761 tree
2762 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2763 {
2764   tree t;
2765   int length;
2766   int i;
2767   va_list p;
2768
2769   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2770
2771   va_start (p, non_dep);
2772
2773   if (REFERENCE_REF_P (non_dep))
2774     non_dep = TREE_OPERAND (non_dep, 0);
2775
2776   t = make_node (code);
2777   length = TREE_CODE_LENGTH (code);
2778   TREE_TYPE (t) = TREE_TYPE (non_dep);
2779   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2780
2781   for (i = 0; i < length; i++)
2782     {
2783       tree x = va_arg (p, tree);
2784       TREE_OPERAND (t, i) = x;
2785     }
2786
2787   if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2788     /* This should not be considered a COMPOUND_EXPR, because it
2789        resolves to an overload.  */
2790     COMPOUND_EXPR_OVERLOADED (t) = 1;
2791
2792   va_end (p);
2793   return convert_from_reference (t);
2794 }
2795
2796 /* Similar to `build_nt_call_vec', but for template definitions of
2797    non-dependent expressions. NON_DEP is the non-dependent expression
2798    that has been built.  */
2799
2800 tree
2801 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2802 {
2803   tree t = build_nt_call_vec (fn, argvec);
2804   if (REFERENCE_REF_P (non_dep))
2805     non_dep = TREE_OPERAND (non_dep, 0);
2806   TREE_TYPE (t) = TREE_TYPE (non_dep);
2807   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2808   return convert_from_reference (t);
2809 }
2810
2811 /* Similar to build_min_non_dep, but for expressions that have been resolved to
2812    a call to an operator overload.  OP is the operator that has been
2813    overloaded.  NON_DEP is the non-dependent expression that's been built,
2814    which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR.  OVERLOAD is
2815    the overload that NON_DEP is calling.  */
2816
2817 tree
2818 build_min_non_dep_op_overload (enum tree_code op,
2819                                tree non_dep,
2820                                tree overload, ...)
2821 {
2822   va_list p;
2823   int nargs, expected_nargs;
2824   tree fn, call;
2825   vec<tree, va_gc> *args;
2826
2827   if (REFERENCE_REF_P (non_dep))
2828     non_dep = TREE_OPERAND (non_dep, 0);
2829
2830   nargs = call_expr_nargs (non_dep);
2831
2832   expected_nargs = cp_tree_code_length (op);
2833   if (op == POSTINCREMENT_EXPR
2834       || op == POSTDECREMENT_EXPR)
2835     expected_nargs += 1;
2836   gcc_assert (nargs == expected_nargs);
2837
2838   args = make_tree_vector ();
2839   va_start (p, overload);
2840
2841   if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
2842     {
2843       fn = overload;
2844       for (int i = 0; i < nargs; i++)
2845         {
2846           tree arg = va_arg (p, tree);
2847           vec_safe_push (args, arg);
2848         }
2849     }
2850   else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
2851     {
2852       tree object = va_arg (p, tree);
2853       tree binfo = TYPE_BINFO (TREE_TYPE (object));
2854       tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
2855       fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
2856                       object, method, NULL_TREE);
2857       for (int i = 1; i < nargs; i++)
2858         {
2859           tree arg = va_arg (p, tree);
2860           vec_safe_push (args, arg);
2861         }
2862     }
2863   else
2864    gcc_unreachable ();
2865
2866   va_end (p);
2867   call = build_min_non_dep_call_vec (non_dep, fn, args);
2868   release_tree_vector (args);
2869
2870   tree call_expr = call;
2871   if (REFERENCE_REF_P (call_expr))
2872     call_expr = TREE_OPERAND (call_expr, 0);
2873   KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
2874
2875   return call;
2876 }
2877
2878 tree
2879 get_type_decl (tree t)
2880 {
2881   if (TREE_CODE (t) == TYPE_DECL)
2882     return t;
2883   if (TYPE_P (t))
2884     return TYPE_STUB_DECL (t);
2885   gcc_assert (t == error_mark_node);
2886   return t;
2887 }
2888
2889 /* Returns the namespace that contains DECL, whether directly or
2890    indirectly.  */
2891
2892 tree
2893 decl_namespace_context (tree decl)
2894 {
2895   while (1)
2896     {
2897       if (TREE_CODE (decl) == NAMESPACE_DECL)
2898         return decl;
2899       else if (TYPE_P (decl))
2900         decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2901       else
2902         decl = CP_DECL_CONTEXT (decl);
2903     }
2904 }
2905
2906 /* Returns true if decl is within an anonymous namespace, however deeply
2907    nested, or false otherwise.  */
2908
2909 bool
2910 decl_anon_ns_mem_p (const_tree decl)
2911 {
2912   while (1)
2913     {
2914       if (decl == NULL_TREE || decl == error_mark_node)
2915         return false;
2916       if (TREE_CODE (decl) == NAMESPACE_DECL
2917           && DECL_NAME (decl) == NULL_TREE)
2918         return true;
2919       /* Classes and namespaces inside anonymous namespaces have
2920          TREE_PUBLIC == 0, so we can shortcut the search.  */
2921       else if (TYPE_P (decl))
2922         return (TREE_PUBLIC (TYPE_MAIN_DECL (decl)) == 0);
2923       else if (TREE_CODE (decl) == NAMESPACE_DECL)
2924         return (TREE_PUBLIC (decl) == 0);
2925       else
2926         decl = DECL_CONTEXT (decl);
2927     }
2928 }
2929
2930 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
2931    CALL_EXPRS.  Return whether they are equivalent.  */
2932
2933 static bool
2934 called_fns_equal (tree t1, tree t2)
2935 {
2936   /* Core 1321: dependent names are equivalent even if the overload sets
2937      are different.  But do compare explicit template arguments.  */
2938   tree name1 = dependent_name (t1);
2939   tree name2 = dependent_name (t2);
2940   if (name1 || name2)
2941     {
2942       tree targs1 = NULL_TREE, targs2 = NULL_TREE;
2943
2944       if (name1 != name2)
2945         return false;
2946
2947       if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
2948         targs1 = TREE_OPERAND (t1, 1);
2949       if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
2950         targs2 = TREE_OPERAND (t2, 1);
2951       return cp_tree_equal (targs1, targs2);
2952     }
2953   else
2954     return cp_tree_equal (t1, t2);
2955 }
2956
2957 /* Return truthvalue of whether T1 is the same tree structure as T2.
2958    Return 1 if they are the same. Return 0 if they are different.  */
2959
2960 bool
2961 cp_tree_equal (tree t1, tree t2)
2962 {
2963   enum tree_code code1, code2;
2964
2965   if (t1 == t2)
2966     return true;
2967   if (!t1 || !t2)
2968     return false;
2969
2970   code1 = TREE_CODE (t1);
2971   code2 = TREE_CODE (t2);
2972
2973   if (code1 != code2)
2974     return false;
2975
2976   switch (code1)
2977     {
2978     case VOID_CST:
2979       /* There's only a single VOID_CST node, so we should never reach
2980          here.  */
2981       gcc_unreachable ();
2982
2983     case INTEGER_CST:
2984       return tree_int_cst_equal (t1, t2);
2985
2986     case REAL_CST:
2987       return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
2988
2989     case STRING_CST:
2990       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2991         && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2992                     TREE_STRING_LENGTH (t1));
2993
2994     case FIXED_CST:
2995       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2996                                      TREE_FIXED_CST (t2));
2997
2998     case COMPLEX_CST:
2999       return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3000         && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3001
3002     case VECTOR_CST:
3003       return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3004
3005     case CONSTRUCTOR:
3006       /* We need to do this when determining whether or not two
3007          non-type pointer to member function template arguments
3008          are the same.  */
3009       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3010           || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3011         return false;
3012       {
3013         tree field, value;
3014         unsigned int i;
3015         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3016           {
3017             constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3018             if (!cp_tree_equal (field, elt2->index)
3019                 || !cp_tree_equal (value, elt2->value))
3020               return false;
3021           }
3022       }
3023       return true;
3024
3025     case TREE_LIST:
3026       if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3027         return false;
3028       if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3029         return false;
3030       return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3031
3032     case SAVE_EXPR:
3033       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3034
3035     case CALL_EXPR:
3036       {
3037         tree arg1, arg2;
3038         call_expr_arg_iterator iter1, iter2;
3039         if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3040           return false;
3041         for (arg1 = first_call_expr_arg (t1, &iter1),
3042                arg2 = first_call_expr_arg (t2, &iter2);
3043              arg1 && arg2;
3044              arg1 = next_call_expr_arg (&iter1),
3045                arg2 = next_call_expr_arg (&iter2))
3046           if (!cp_tree_equal (arg1, arg2))
3047             return false;
3048         if (arg1 || arg2)
3049           return false;
3050         return true;
3051       }
3052
3053     case TARGET_EXPR:
3054       {
3055         tree o1 = TREE_OPERAND (t1, 0);
3056         tree o2 = TREE_OPERAND (t2, 0);
3057
3058         /* Special case: if either target is an unallocated VAR_DECL,
3059            it means that it's going to be unified with whatever the
3060            TARGET_EXPR is really supposed to initialize, so treat it
3061            as being equivalent to anything.  */
3062         if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3063             && !DECL_RTL_SET_P (o1))
3064           /*Nop*/;
3065         else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3066                  && !DECL_RTL_SET_P (o2))
3067           /*Nop*/;
3068         else if (!cp_tree_equal (o1, o2))
3069           return false;
3070
3071         return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3072       }
3073
3074     case WITH_CLEANUP_EXPR:
3075       if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3076         return false;
3077       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3078
3079     case COMPONENT_REF:
3080       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
3081         return false;
3082       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3083
3084     case PARM_DECL:
3085       /* For comparing uses of parameters in late-specified return types
3086          with an out-of-class definition of the function, but can also come
3087          up for expressions that involve 'this' in a member function
3088          template.  */
3089
3090       if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
3091         /* When comparing hash table entries, only an exact match is
3092            good enough; we don't want to replace 'this' with the
3093            version from another function.  But be more flexible
3094            with local parameters in a requires-expression.  */
3095         return false;
3096
3097       if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3098         {
3099           if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3100             return false;
3101           if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3102             return false;
3103           if (DECL_ARTIFICIAL (t1)
3104               || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3105                   && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3106             return true;
3107         }
3108       return false;
3109
3110     case VAR_DECL:
3111     case CONST_DECL:
3112     case FIELD_DECL:
3113     case FUNCTION_DECL:
3114     case TEMPLATE_DECL:
3115     case IDENTIFIER_NODE:
3116     case SSA_NAME:
3117       return false;
3118
3119     case BASELINK:
3120       return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3121               && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
3122               && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
3123               && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3124                                 BASELINK_FUNCTIONS (t2)));
3125
3126     case TEMPLATE_PARM_INDEX:
3127       return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3128               && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
3129               && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3130                   == TEMPLATE_PARM_PARAMETER_PACK (t2))
3131               && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3132                               TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
3133
3134     case TEMPLATE_ID_EXPR:
3135       return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3136               && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3137
3138     case CONSTRAINT_INFO:
3139       return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3140                             CI_ASSOCIATED_CONSTRAINTS (t2));
3141
3142     case CHECK_CONSTR:
3143       return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3144               && comp_template_args (CHECK_CONSTR_ARGS (t1),
3145                                      CHECK_CONSTR_ARGS (t2)));
3146
3147     case TREE_VEC:
3148       {
3149         unsigned ix;
3150         if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3151           return false;
3152         for (ix = TREE_VEC_LENGTH (t1); ix--;)
3153           if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3154                               TREE_VEC_ELT (t2, ix)))
3155             return false;
3156         return true;
3157       }
3158
3159     case SIZEOF_EXPR:
3160     case ALIGNOF_EXPR:
3161       {
3162         tree o1 = TREE_OPERAND (t1, 0);
3163         tree o2 = TREE_OPERAND (t2, 0);
3164
3165         if (code1 == SIZEOF_EXPR)
3166           {
3167             if (SIZEOF_EXPR_TYPE_P (t1))
3168               o1 = TREE_TYPE (o1);
3169             if (SIZEOF_EXPR_TYPE_P (t2))
3170               o2 = TREE_TYPE (o2);
3171           }
3172         if (TREE_CODE (o1) != TREE_CODE (o2))
3173           return false;
3174         if (TYPE_P (o1))
3175           return same_type_p (o1, o2);
3176         else
3177           return cp_tree_equal (o1, o2);
3178       }
3179
3180     case MODOP_EXPR:
3181       {
3182         tree t1_op1, t2_op1;
3183
3184         if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3185           return false;
3186
3187         t1_op1 = TREE_OPERAND (t1, 1);
3188         t2_op1 = TREE_OPERAND (t2, 1);
3189         if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3190           return false;
3191
3192         return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3193       }
3194
3195     case PTRMEM_CST:
3196       /* Two pointer-to-members are the same if they point to the same
3197          field or function in the same class.  */
3198       if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3199         return false;
3200
3201       return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
3202
3203     case OVERLOAD:
3204       if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
3205         return false;
3206       return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
3207
3208     case TRAIT_EXPR:
3209       if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3210         return false;
3211       return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
3212         && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
3213
3214     case CAST_EXPR:
3215     case STATIC_CAST_EXPR:
3216     case REINTERPRET_CAST_EXPR:
3217     case CONST_CAST_EXPR:
3218     case DYNAMIC_CAST_EXPR:
3219     case IMPLICIT_CONV_EXPR:
3220     case NEW_EXPR:
3221     CASE_CONVERT:
3222     case NON_LVALUE_EXPR:
3223     case VIEW_CONVERT_EXPR:
3224       if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3225         return false;
3226       /* Now compare operands as usual.  */
3227       break;
3228
3229     case DEFERRED_NOEXCEPT:
3230       return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3231                              DEFERRED_NOEXCEPT_PATTERN (t2))
3232               && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3233                                      DEFERRED_NOEXCEPT_ARGS (t2)));
3234       break;
3235
3236     default:
3237       break;
3238     }
3239
3240   switch (TREE_CODE_CLASS (code1))
3241     {
3242     case tcc_unary:
3243     case tcc_binary:
3244     case tcc_comparison:
3245     case tcc_expression:
3246     case tcc_vl_exp:
3247     case tcc_reference:
3248     case tcc_statement:
3249       {
3250         int i, n;
3251
3252         n = cp_tree_operand_length (t1);
3253         if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3254             && n != TREE_OPERAND_LENGTH (t2))
3255           return false;
3256
3257         for (i = 0; i < n; ++i)
3258           if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3259             return false;
3260
3261         return true;
3262       }
3263
3264     case tcc_type:
3265       return same_type_p (t1, t2);
3266     default:
3267       gcc_unreachable ();
3268     }
3269   /* We can get here with --disable-checking.  */
3270   return false;
3271 }
3272
3273 /* The type of ARG when used as an lvalue.  */
3274
3275 tree
3276 lvalue_type (tree arg)
3277 {
3278   tree type = TREE_TYPE (arg);
3279   return type;
3280 }
3281
3282 /* The type of ARG for printing error messages; denote lvalues with
3283    reference types.  */
3284
3285 tree
3286 error_type (tree arg)
3287 {
3288   tree type = TREE_TYPE (arg);
3289
3290   if (TREE_CODE (type) == ARRAY_TYPE)
3291     ;
3292   else if (TREE_CODE (type) == ERROR_MARK)
3293     ;
3294   else if (real_lvalue_p (arg))
3295     type = build_reference_type (lvalue_type (arg));
3296   else if (MAYBE_CLASS_TYPE_P (type))
3297     type = lvalue_type (arg);
3298
3299   return type;
3300 }
3301
3302 /* Does FUNCTION use a variable-length argument list?  */
3303
3304 int
3305 varargs_function_p (const_tree function)
3306 {
3307   return stdarg_p (TREE_TYPE (function));
3308 }
3309
3310 /* Returns 1 if decl is a member of a class.  */
3311
3312 int
3313 member_p (const_tree decl)
3314 {
3315   const_tree const ctx = DECL_CONTEXT (decl);
3316   return (ctx && TYPE_P (ctx));
3317 }
3318
3319 /* Create a placeholder for member access where we don't actually have an
3320    object that the access is against.  */
3321
3322 tree
3323 build_dummy_object (tree type)
3324 {
3325   tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
3326   return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
3327 }
3328
3329 /* We've gotten a reference to a member of TYPE.  Return *this if appropriate,
3330    or a dummy object otherwise.  If BINFOP is non-0, it is filled with the
3331    binfo path from current_class_type to TYPE, or 0.  */
3332
3333 tree
3334 maybe_dummy_object (tree type, tree* binfop)
3335 {
3336   tree decl, context;
3337   tree binfo;
3338   tree current = current_nonlambda_class_type ();
3339
3340   if (current
3341       && (binfo = lookup_base (current, type, ba_any, NULL,
3342                                tf_warning_or_error)))
3343     context = current;
3344   else
3345     {
3346       /* Reference from a nested class member function.  */
3347       context = type;
3348       binfo = TYPE_BINFO (type);
3349     }
3350
3351   if (binfop)
3352     *binfop = binfo;
3353
3354   if (current_class_ref
3355       /* current_class_ref might not correspond to current_class_type if
3356          we're in tsubst_default_argument or a lambda-declarator; in either
3357          case, we want to use current_class_ref if it matches CONTEXT.  */
3358       && (same_type_ignoring_top_level_qualifiers_p
3359           (TREE_TYPE (current_class_ref), context)))
3360     decl = current_class_ref;
3361   else
3362     decl = build_dummy_object (context);
3363
3364   return decl;
3365 }
3366
3367 /* Returns 1 if OB is a placeholder object, or a pointer to one.  */
3368
3369 int
3370 is_dummy_object (const_tree ob)
3371 {
3372   if (INDIRECT_REF_P (ob))
3373     ob = TREE_OPERAND (ob, 0);
3374   return (TREE_CODE (ob) == CONVERT_EXPR
3375           && TREE_OPERAND (ob, 0) == void_node);
3376 }
3377
3378 /* Returns 1 iff type T is something we want to treat as a scalar type for
3379    the purpose of deciding whether it is trivial/POD/standard-layout.  */
3380
3381 bool
3382 scalarish_type_p (const_tree t)
3383 {
3384   if (t == error_mark_node)
3385     return 1;
3386
3387   return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
3388 }
3389
3390 /* Returns true iff T requires non-trivial default initialization.  */
3391
3392 bool
3393 type_has_nontrivial_default_init (const_tree t)
3394 {
3395   t = strip_array_types (CONST_CAST_TREE (t));
3396
3397   if (CLASS_TYPE_P (t))
3398     return TYPE_HAS_COMPLEX_DFLT (t);
3399   else
3400     return 0;
3401 }
3402
3403 /* Returns true iff copying an object of type T (including via move
3404    constructor) is non-trivial.  That is, T has no non-trivial copy
3405    constructors and no non-trivial move constructors.  */
3406
3407 bool
3408 type_has_nontrivial_copy_init (const_tree t)
3409 {
3410   t = strip_array_types (CONST_CAST_TREE (t));
3411
3412   if (CLASS_TYPE_P (t))
3413     {
3414       gcc_assert (COMPLETE_TYPE_P (t));
3415       return ((TYPE_HAS_COPY_CTOR (t)
3416                && TYPE_HAS_COMPLEX_COPY_CTOR (t))
3417               || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
3418     }
3419   else
3420     return 0;
3421 }
3422
3423 /* Returns 1 iff type T is a trivially copyable type, as defined in
3424    [basic.types] and [class].  */
3425
3426 bool
3427 trivially_copyable_p (const_tree t)
3428 {
3429   t = strip_array_types (CONST_CAST_TREE (t));
3430
3431   if (CLASS_TYPE_P (t))
3432     return ((!TYPE_HAS_COPY_CTOR (t)
3433              || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
3434             && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
3435             && (!TYPE_HAS_COPY_ASSIGN (t)
3436                 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
3437             && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
3438             && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
3439   else
3440     return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
3441 }
3442
3443 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
3444    [class].  */
3445
3446 bool
3447 trivial_type_p (const_tree t)
3448 {
3449   t = strip_array_types (CONST_CAST_TREE (t));
3450
3451   if (CLASS_TYPE_P (t))
3452     return (TYPE_HAS_TRIVIAL_DFLT (t)
3453             && trivially_copyable_p (t));
3454   else
3455     return scalarish_type_p (t);
3456 }
3457
3458 /* Returns 1 iff type T is a POD type, as defined in [basic.types].  */
3459
3460 bool
3461 pod_type_p (const_tree t)
3462 {
3463   /* This CONST_CAST is okay because strip_array_types returns its
3464      argument unmodified and we assign it to a const_tree.  */
3465   t = strip_array_types (CONST_CAST_TREE(t));
3466
3467   if (!CLASS_TYPE_P (t))
3468     return scalarish_type_p (t);
3469   else if (cxx_dialect > cxx98)
3470     /* [class]/10: A POD struct is a class that is both a trivial class and a
3471        standard-layout class, and has no non-static data members of type
3472        non-POD struct, non-POD union (or array of such types).
3473
3474        We don't need to check individual members because if a member is
3475        non-std-layout or non-trivial, the class will be too.  */
3476     return (std_layout_type_p (t) && trivial_type_p (t));
3477   else
3478     /* The C++98 definition of POD is different.  */
3479     return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3480 }
3481
3482 /* Returns true iff T is POD for the purpose of layout, as defined in the
3483    C++ ABI.  */
3484
3485 bool
3486 layout_pod_type_p (const_tree t)
3487 {
3488   t = strip_array_types (CONST_CAST_TREE (t));
3489
3490   if (CLASS_TYPE_P (t))
3491     return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3492   else
3493     return scalarish_type_p (t);
3494 }
3495
3496 /* Returns true iff T is a standard-layout type, as defined in
3497    [basic.types].  */
3498
3499 bool
3500 std_layout_type_p (const_tree t)
3501 {
3502   t = strip_array_types (CONST_CAST_TREE (t));
3503
3504   if (CLASS_TYPE_P (t))
3505     return !CLASSTYPE_NON_STD_LAYOUT (t);
3506   else
3507     return scalarish_type_p (t);
3508 }
3509
3510 /* Nonzero iff type T is a class template implicit specialization.  */
3511
3512 bool
3513 class_tmpl_impl_spec_p (const_tree t)
3514 {
3515   return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
3516 }
3517
3518 /* Returns 1 iff zero initialization of type T means actually storing
3519    zeros in it.  */
3520
3521 int
3522 zero_init_p (const_tree t)
3523 {
3524   /* This CONST_CAST is okay because strip_array_types returns its
3525      argument unmodified and we assign it to a const_tree.  */
3526   t = strip_array_types (CONST_CAST_TREE(t));
3527
3528   if (t == error_mark_node)
3529     return 1;
3530
3531   /* NULL pointers to data members are initialized with -1.  */
3532   if (TYPE_PTRDATAMEM_P (t))
3533     return 0;
3534
3535   /* Classes that contain types that can't be zero-initialized, cannot
3536      be zero-initialized themselves.  */
3537   if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
3538     return 0;
3539
3540   return 1;
3541 }
3542
3543 /* Table of valid C++ attributes.  */
3544 const struct attribute_spec cxx_attribute_table[] =
3545 {
3546   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3547        affects_type_identity } */
3548   { "java_interface", 0, 0, false, false, false,
3549     handle_java_interface_attribute, false },
3550   { "com_interface",  0, 0, false, false, false,
3551     handle_com_interface_attribute, false },
3552   { "init_priority",  1, 1, true,  false, false,
3553     handle_init_priority_attribute, false },
3554   { "abi_tag", 1, -1, false, false, false,
3555     handle_abi_tag_attribute, true },
3556   { NULL,             0, 0, false, false, false, NULL, false }
3557 };
3558
3559 /* Handle a "java_interface" attribute; arguments as in
3560    struct attribute_spec.handler.  */
3561 static tree
3562 handle_java_interface_attribute (tree* node,
3563                                  tree name,
3564                                  tree /*args*/,
3565                                  int flags,
3566                                  bool* no_add_attrs)
3567 {
3568   if (DECL_P (*node)
3569       || !CLASS_TYPE_P (*node)
3570       || !TYPE_FOR_JAVA (*node))
3571     {
3572       error ("%qE attribute can only be applied to Java class definitions",
3573              name);
3574       *no_add_attrs = true;
3575       return NULL_TREE;
3576     }
3577   if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3578     *node = build_variant_type_copy (*node);
3579   TYPE_JAVA_INTERFACE (*node) = 1;
3580
3581   return NULL_TREE;
3582 }
3583
3584 /* Handle a "com_interface" attribute; arguments as in
3585    struct attribute_spec.handler.  */
3586 static tree
3587 handle_com_interface_attribute (tree* node,
3588                                 tree name,
3589                                 tree /*args*/,
3590                                 int /*flags*/,
3591                                 bool* no_add_attrs)
3592 {
3593   static int warned;
3594
3595   *no_add_attrs = true;
3596
3597   if (DECL_P (*node)
3598       || !CLASS_TYPE_P (*node)
3599       || *node != TYPE_MAIN_VARIANT (*node))
3600     {
3601       warning (OPT_Wattributes, "%qE attribute can only be applied "
3602                "to class definitions", name);
3603       return NULL_TREE;
3604     }
3605
3606   if (!warned++)
3607     warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
3608              name);
3609
3610   return NULL_TREE;
3611 }
3612
3613 /* Handle an "init_priority" attribute; arguments as in
3614    struct attribute_spec.handler.  */
3615 static tree
3616 handle_init_priority_attribute (tree* node,
3617                                 tree name,
3618                                 tree args,
3619                                 int /*flags*/,
3620                                 bool* no_add_attrs)
3621 {
3622   tree initp_expr = TREE_VALUE (args);
3623   tree decl = *node;
3624   tree type = TREE_TYPE (decl);
3625   int pri;
3626
3627   STRIP_NOPS (initp_expr);
3628   initp_expr = default_conversion (initp_expr);
3629   if (initp_expr)
3630     initp_expr = maybe_constant_value (initp_expr);
3631
3632   if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3633     {
3634       error ("requested init_priority is not an integer constant");
3635       cxx_constant_value (initp_expr);
3636       *no_add_attrs = true;
3637       return NULL_TREE;
3638     }
3639
3640   pri = TREE_INT_CST_LOW (initp_expr);
3641
3642   type = strip_array_types (type);
3643
3644   if (decl == NULL_TREE
3645       || !VAR_P (decl)
3646       || !TREE_STATIC (decl)
3647       || DECL_EXTERNAL (decl)
3648       || (TREE_CODE (type) != RECORD_TYPE
3649           && TREE_CODE (type) != UNION_TYPE)
3650       /* Static objects in functions are initialized the
3651          first time control passes through that
3652          function. This is not precise enough to pin down an
3653          init_priority value, so don't allow it.  */
3654       || current_function_decl)
3655     {
3656       error ("can only use %qE attribute on file-scope definitions "
3657              "of objects of class type", name);
3658       *no_add_attrs = true;
3659       return NULL_TREE;
3660     }
3661
3662   if (pri > MAX_INIT_PRIORITY || pri <= 0)
3663     {
3664       error ("requested init_priority is out of range");
3665       *no_add_attrs = true;
3666       return NULL_TREE;
3667     }
3668
3669   /* Check for init_priorities that are reserved for
3670      language and runtime support implementations.*/
3671   if (pri <= MAX_RESERVED_INIT_PRIORITY)
3672     {
3673       warning
3674         (0, "requested init_priority is reserved for internal use");
3675     }
3676
3677   if (SUPPORTS_INIT_PRIORITY)
3678     {
3679       SET_DECL_INIT_PRIORITY (decl, pri);
3680       DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3681       return NULL_TREE;
3682     }
3683   else
3684     {
3685       error ("%qE attribute is not supported on this platform", name);
3686       *no_add_attrs = true;
3687       return NULL_TREE;
3688     }
3689 }
3690
3691 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3692    and the new one has the tags in NEW_.  Give an error if there are tags
3693    in NEW_ that weren't in OLD.  */
3694
3695 bool
3696 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3697 {
3698   if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3699     old = TREE_VALUE (old);
3700   if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3701     new_ = TREE_VALUE (new_);
3702   bool err = false;
3703   for (const_tree t = new_; t; t = TREE_CHAIN (t))
3704     {
3705       tree str = TREE_VALUE (t);
3706       for (const_tree in = old; in; in = TREE_CHAIN (in))
3707         {
3708           tree ostr = TREE_VALUE (in);
3709           if (cp_tree_equal (str, ostr))
3710             goto found;
3711         }
3712       error ("redeclaration of %qD adds abi tag %E", decl, str);
3713       err = true;
3714     found:;
3715     }
3716   if (err)
3717     {
3718       inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3719       return false;
3720     }
3721   return true;
3722 }
3723
3724 /* The abi_tag attribute with the name NAME was given ARGS.  If they are
3725    ill-formed, give an error and return false; otherwise, return true.  */
3726
3727 bool
3728 check_abi_tag_args (tree args, tree name)
3729 {
3730   if (!args)
3731     {
3732       error ("the %qE attribute requires arguments", name);
3733       return false;
3734     }
3735   for (tree arg = args; arg; arg = TREE_CHAIN (arg))
3736     {
3737       tree elt = TREE_VALUE (arg);
3738       if (TREE_CODE (elt) != STRING_CST
3739           || (!same_type_ignoring_top_level_qualifiers_p
3740               (strip_array_types (TREE_TYPE (elt)),
3741                char_type_node)))
3742         {
3743           error ("arguments to the %qE attribute must be narrow string "
3744                  "literals", name);
3745           return false;
3746         }
3747       const char *begin = TREE_STRING_POINTER (elt);
3748       const char *end = begin + TREE_STRING_LENGTH (elt);
3749       for (const char *p = begin; p != end; ++p)
3750         {
3751           char c = *p;
3752           if (p == begin)
3753             {
3754               if (!ISALPHA (c) && c != '_')
3755                 {
3756                   error ("arguments to the %qE attribute must contain valid "
3757                          "identifiers", name);
3758                   inform (input_location, "%<%c%> is not a valid first "
3759                           "character for an identifier", c);
3760                   return false;
3761                 }
3762             }
3763           else if (p == end - 1)
3764             gcc_assert (c == 0);
3765           else
3766             {
3767               if (!ISALNUM (c) && c != '_')
3768                 {
3769                   error ("arguments to the %qE attribute must contain valid "
3770                          "identifiers", name);
3771                   inform (input_location, "%<%c%> is not a valid character "
3772                           "in an identifier", c);
3773                   return false;
3774                 }
3775             }
3776         }
3777     }
3778   return true;
3779 }
3780
3781 /* Handle an "abi_tag" attribute; arguments as in
3782    struct attribute_spec.handler.  */
3783
3784 static tree
3785 handle_abi_tag_attribute (tree* node, tree name, tree args,
3786                           int flags, bool* no_add_attrs)
3787 {
3788   if (!check_abi_tag_args (args, name))
3789     goto fail;
3790
3791   if (TYPE_P (*node))
3792     {
3793       if (!OVERLOAD_TYPE_P (*node))
3794         {
3795           error ("%qE attribute applied to non-class, non-enum type %qT",
3796                  name, *node);
3797           goto fail;
3798         }
3799       else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
3800         {
3801           error ("%qE attribute applied to %qT after its definition",
3802                  name, *node);
3803           goto fail;
3804         }
3805       else if (CLASS_TYPE_P (*node)
3806                && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
3807         {
3808           warning (OPT_Wattributes, "ignoring %qE attribute applied to "
3809                    "template instantiation %qT", name, *node);
3810           goto fail;
3811         }
3812       else if (CLASS_TYPE_P (*node)
3813                && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
3814         {
3815           warning (OPT_Wattributes, "ignoring %qE attribute applied to "
3816                    "template specialization %qT", name, *node);
3817           goto fail;
3818         }
3819
3820       tree attributes = TYPE_ATTRIBUTES (*node);
3821       tree decl = TYPE_NAME (*node);
3822
3823       /* Make sure all declarations have the same abi tags.  */
3824       if (DECL_SOURCE_LOCATION (decl) != input_location)
3825         {
3826           if (!check_abi_tag_redeclaration (decl,
3827                                             lookup_attribute ("abi_tag",
3828                                                               attributes),
3829                                             args))
3830             goto fail;
3831         }
3832     }
3833   else
3834     {
3835       if (!VAR_OR_FUNCTION_DECL_P (*node))
3836         {
3837           error ("%qE attribute applied to non-function, non-variable %qD",
3838                  name, *node);
3839           goto fail;
3840         }
3841       else if (DECL_LANGUAGE (*node) == lang_c)
3842         {
3843           error ("%qE attribute applied to extern \"C\" declaration %qD",
3844                  name, *node);
3845           goto fail;
3846         }
3847     }
3848
3849   return NULL_TREE;
3850
3851  fail:
3852   *no_add_attrs = true;
3853   return NULL_TREE;
3854 }
3855
3856 /* Return a new PTRMEM_CST of the indicated TYPE.  The MEMBER is the
3857    thing pointed to by the constant.  */
3858
3859 tree
3860 make_ptrmem_cst (tree type, tree member)
3861 {
3862   tree ptrmem_cst = make_node (PTRMEM_CST);
3863   TREE_TYPE (ptrmem_cst) = type;
3864   PTRMEM_CST_MEMBER (ptrmem_cst) = member;
3865   return ptrmem_cst;
3866 }
3867
3868 /* Build a variant of TYPE that has the indicated ATTRIBUTES.  May
3869    return an existing type if an appropriate type already exists.  */
3870
3871 tree
3872 cp_build_type_attribute_variant (tree type, tree attributes)
3873 {
3874   tree new_type;
3875
3876   new_type = build_type_attribute_variant (type, attributes);
3877   if (TREE_CODE (new_type) == FUNCTION_TYPE
3878       || TREE_CODE (new_type) == METHOD_TYPE)
3879     {
3880       new_type = build_exception_variant (new_type,
3881                                           TYPE_RAISES_EXCEPTIONS (type));
3882       new_type = build_ref_qualified_type (new_type,
3883                                            type_memfn_rqual (type));
3884     }
3885
3886   /* Making a new main variant of a class type is broken.  */
3887   gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
3888     
3889   return new_type;
3890 }
3891
3892 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
3893    Called only after doing all language independent checks.  Only
3894    to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
3895    compared in type_hash_eq.  */
3896
3897 bool
3898 cxx_type_hash_eq (const_tree typea, const_tree typeb)
3899 {
3900   gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
3901               || TREE_CODE (typea) == METHOD_TYPE);
3902
3903   return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3904                             TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
3905 }
3906
3907 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
3908    traversal.  Called from walk_tree.  */
3909
3910 tree
3911 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
3912                   void *data, hash_set<tree> *pset)
3913 {
3914   enum tree_code code = TREE_CODE (*tp);
3915   tree result;
3916
3917 #define WALK_SUBTREE(NODE)                              \
3918   do                                                    \
3919     {                                                   \
3920       result = cp_walk_tree (&(NODE), func, data, pset);        \
3921       if (result) goto out;                             \
3922     }                                                   \
3923   while (0)
3924
3925   /* Not one of the easy cases.  We must explicitly go through the
3926      children.  */
3927   result = NULL_TREE;
3928   switch (code)
3929     {
3930     case DEFAULT_ARG:
3931     case TEMPLATE_TEMPLATE_PARM:
3932     case BOUND_TEMPLATE_TEMPLATE_PARM:
3933     case UNBOUND_CLASS_TEMPLATE:
3934     case TEMPLATE_PARM_INDEX:
3935     case TEMPLATE_TYPE_PARM:
3936     case TYPENAME_TYPE:
3937     case TYPEOF_TYPE:
3938     case UNDERLYING_TYPE:
3939       /* None of these have subtrees other than those already walked
3940          above.  */
3941       *walk_subtrees_p = 0;
3942       break;
3943
3944     case BASELINK:
3945       WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
3946       *walk_subtrees_p = 0;
3947       break;
3948
3949     case PTRMEM_CST:
3950       WALK_SUBTREE (TREE_TYPE (*tp));
3951       *walk_subtrees_p = 0;
3952       break;
3953
3954     case TREE_LIST:
3955       WALK_SUBTREE (TREE_PURPOSE (*tp));
3956       break;
3957
3958     case OVERLOAD:
3959       WALK_SUBTREE (OVL_FUNCTION (*tp));
3960       WALK_SUBTREE (OVL_CHAIN (*tp));
3961       *walk_subtrees_p = 0;
3962       break;
3963
3964     case USING_DECL:
3965       WALK_SUBTREE (DECL_NAME (*tp));
3966       WALK_SUBTREE (USING_DECL_SCOPE (*tp));
3967       WALK_SUBTREE (USING_DECL_DECLS (*tp));
3968       *walk_subtrees_p = 0;
3969       break;
3970
3971     case RECORD_TYPE:
3972       if (TYPE_PTRMEMFUNC_P (*tp))
3973         WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
3974       break;
3975
3976     case TYPE_ARGUMENT_PACK:
3977     case NONTYPE_ARGUMENT_PACK:
3978       {
3979         tree args = ARGUMENT_PACK_ARGS (*tp);
3980         int i, len = TREE_VEC_LENGTH (args);
3981         for (i = 0; i < len; i++)
3982           WALK_SUBTREE (TREE_VEC_ELT (args, i));
3983       }
3984       break;
3985
3986     case TYPE_PACK_EXPANSION:
3987       WALK_SUBTREE (TREE_TYPE (*tp));
3988       WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3989       *walk_subtrees_p = 0;
3990       break;
3991       
3992     case EXPR_PACK_EXPANSION:
3993       WALK_SUBTREE (TREE_OPERAND (*tp, 0));
3994       WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3995       *walk_subtrees_p = 0;
3996       break;
3997
3998     case CAST_EXPR:
3999     case REINTERPRET_CAST_EXPR:
4000     case STATIC_CAST_EXPR:
4001     case CONST_CAST_EXPR:
4002     case DYNAMIC_CAST_EXPR:
4003     case IMPLICIT_CONV_EXPR:
4004       if (TREE_TYPE (*tp))
4005         WALK_SUBTREE (TREE_TYPE (*tp));
4006
4007       {
4008         int i;
4009         for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4010           WALK_SUBTREE (TREE_OPERAND (*tp, i));
4011       }
4012       *walk_subtrees_p = 0;
4013       break;
4014
4015     case TRAIT_EXPR:
4016       WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4017       WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4018       *walk_subtrees_p = 0;
4019       break;
4020
4021     case DECLTYPE_TYPE:
4022       WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4023       *walk_subtrees_p = 0;
4024       break;
4025  
4026     case REQUIRES_EXPR:
4027       // Only recurse through the nested expression. Do not
4028       // walk the parameter list. Doing so causes false
4029       // positives in the pack expansion checker since the
4030       // requires parameters are introduced as pack expansions.
4031       WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4032       *walk_subtrees_p = 0;
4033       break;
4034
4035     default:
4036       return NULL_TREE;
4037     }
4038
4039   /* We didn't find what we were looking for.  */
4040  out:
4041   return result;
4042
4043 #undef WALK_SUBTREE
4044 }
4045
4046 /* Like save_expr, but for C++.  */
4047
4048 tree
4049 cp_save_expr (tree expr)
4050 {
4051   /* There is no reason to create a SAVE_EXPR within a template; if
4052      needed, we can create the SAVE_EXPR when instantiating the
4053      template.  Furthermore, the middle-end cannot handle C++-specific
4054      tree codes.  */
4055   if (processing_template_decl)
4056     return expr;
4057   return save_expr (expr);
4058 }
4059
4060 /* Initialize tree.c.  */
4061
4062 void
4063 init_tree (void)
4064 {
4065   list_hash_table = hash_table<list_hasher>::create_ggc (61);
4066 }
4067
4068 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
4069    is.  Note that sfk_none is zero, so this function can be used as a
4070    predicate to test whether or not DECL is a special function.  */
4071
4072 special_function_kind
4073 special_function_p (const_tree decl)
4074 {
4075   /* Rather than doing all this stuff with magic names, we should
4076      probably have a field of type `special_function_kind' in
4077      DECL_LANG_SPECIFIC.  */
4078   if (DECL_INHERITED_CTOR_BASE (decl))
4079     return sfk_inheriting_constructor;
4080   if (DECL_COPY_CONSTRUCTOR_P (decl))
4081     return sfk_copy_constructor;
4082   if (DECL_MOVE_CONSTRUCTOR_P (decl))
4083     return sfk_move_constructor;
4084   if (DECL_CONSTRUCTOR_P (decl))
4085     return sfk_constructor;
4086   if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
4087     {
4088       if (copy_fn_p (decl))
4089         return sfk_copy_assignment;
4090       if (move_fn_p (decl))
4091         return sfk_move_assignment;
4092     }
4093   if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4094     return sfk_destructor;
4095   if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4096     return sfk_complete_destructor;
4097   if (DECL_BASE_DESTRUCTOR_P (decl))
4098     return sfk_base_destructor;
4099   if (DECL_DELETING_DESTRUCTOR_P (decl))
4100     return sfk_deleting_destructor;
4101   if (DECL_CONV_FN_P (decl))
4102     return sfk_conversion;
4103
4104   return sfk_none;
4105 }
4106
4107 /* Returns nonzero if TYPE is a character type, including wchar_t.  */
4108
4109 int
4110 char_type_p (tree type)
4111 {
4112   return (same_type_p (type, char_type_node)
4113           || same_type_p (type, unsigned_char_type_node)
4114           || same_type_p (type, signed_char_type_node)
4115           || same_type_p (type, char16_type_node)
4116           || same_type_p (type, char32_type_node)
4117           || same_type_p (type, wchar_type_node));
4118 }
4119
4120 /* Returns the kind of linkage associated with the indicated DECL.  Th
4121    value returned is as specified by the language standard; it is
4122    independent of implementation details regarding template
4123    instantiation, etc.  For example, it is possible that a declaration
4124    to which this function assigns external linkage would not show up
4125    as a global symbol when you run `nm' on the resulting object file.  */
4126
4127 linkage_kind
4128 decl_linkage (tree decl)
4129 {
4130   /* This function doesn't attempt to calculate the linkage from first
4131      principles as given in [basic.link].  Instead, it makes use of
4132      the fact that we have already set TREE_PUBLIC appropriately, and
4133      then handles a few special cases.  Ideally, we would calculate
4134      linkage first, and then transform that into a concrete
4135      implementation.  */
4136
4137   /* Things that don't have names have no linkage.  */
4138   if (!DECL_NAME (decl))
4139     return lk_none;
4140
4141   /* Fields have no linkage.  */
4142   if (TREE_CODE (decl) == FIELD_DECL)
4143     return lk_none;
4144
4145   /* Things that are TREE_PUBLIC have external linkage.  */
4146   if (TREE_PUBLIC (decl))
4147     return lk_external;
4148
4149   /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
4150      check one of the "clones" for the real linkage.  */
4151   if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
4152        || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
4153       && DECL_CHAIN (decl)
4154       && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
4155     return decl_linkage (DECL_CHAIN (decl));
4156
4157   if (TREE_CODE (decl) == NAMESPACE_DECL)
4158     return lk_external;
4159
4160   /* Linkage of a CONST_DECL depends on the linkage of the enumeration
4161      type.  */
4162   if (TREE_CODE (decl) == CONST_DECL)
4163     return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
4164
4165   /* Things in local scope do not have linkage, if they don't have
4166      TREE_PUBLIC set.  */
4167   if (decl_function_context (decl))
4168     return lk_none;
4169
4170   /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
4171      are considered to have external linkage for language purposes, as do
4172      template instantiations on targets without weak symbols.  DECLs really
4173      meant to have internal linkage have DECL_THIS_STATIC set.  */
4174   if (TREE_CODE (decl) == TYPE_DECL)
4175     return lk_external;
4176   if (VAR_OR_FUNCTION_DECL_P (decl))
4177     {
4178       if (!DECL_THIS_STATIC (decl))
4179         return lk_external;
4180
4181       /* Static data members and static member functions from classes
4182          in anonymous namespace also don't have TREE_PUBLIC set.  */
4183       if (DECL_CLASS_CONTEXT (decl))
4184         return lk_external;
4185     }
4186
4187   /* Everything else has internal linkage.  */
4188   return lk_internal;
4189 }
4190
4191 /* Returns the storage duration of the object or reference associated with
4192    the indicated DECL, which should be a VAR_DECL or PARM_DECL.  */
4193
4194 duration_kind
4195 decl_storage_duration (tree decl)
4196 {
4197   if (TREE_CODE (decl) == PARM_DECL)
4198     return dk_auto;
4199   if (TREE_CODE (decl) == FUNCTION_DECL)
4200     return dk_static;
4201   gcc_assert (VAR_P (decl));
4202   if (!TREE_STATIC (decl)
4203       && !DECL_EXTERNAL (decl))
4204     return dk_auto;
4205   if (CP_DECL_THREAD_LOCAL_P (decl))
4206     return dk_thread;
4207   return dk_static;
4208 }
4209 \f
4210 /* EXP is an expression that we want to pre-evaluate.  Returns (in
4211    *INITP) an expression that will perform the pre-evaluation.  The
4212    value returned by this function is a side-effect free expression
4213    equivalent to the pre-evaluated expression.  Callers must ensure
4214    that *INITP is evaluated before EXP.  */
4215
4216 tree
4217 stabilize_expr (tree exp, tree* initp)
4218 {
4219   tree init_expr;
4220
4221   if (!TREE_SIDE_EFFECTS (exp))
4222     init_expr = NULL_TREE;
4223   else if (VOID_TYPE_P (TREE_TYPE (exp)))
4224     {
4225       init_expr = exp;
4226       exp = void_node;
4227     }
4228   /* There are no expressions with REFERENCE_TYPE, but there can be call
4229      arguments with such a type; just treat it as a pointer.  */
4230   else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
4231            || SCALAR_TYPE_P (TREE_TYPE (exp))
4232            || !lvalue_or_rvalue_with_address_p (exp))
4233     {
4234       init_expr = get_target_expr (exp);
4235       exp = TARGET_EXPR_SLOT (init_expr);
4236       if (CLASS_TYPE_P (TREE_TYPE (exp)))
4237         exp = move (exp);
4238       else
4239         exp = rvalue (exp);
4240     }
4241   else
4242     {
4243       bool xval = !real_lvalue_p (exp);
4244       exp = cp_build_addr_expr (exp, tf_warning_or_error);
4245       init_expr = get_target_expr (exp);
4246       exp = TARGET_EXPR_SLOT (init_expr);
4247       exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
4248       if (xval)
4249         exp = move (exp);
4250     }
4251   *initp = init_expr;
4252
4253   gcc_assert (!TREE_SIDE_EFFECTS (exp));
4254   return exp;
4255 }
4256
4257 /* Add NEW_EXPR, an expression whose value we don't care about, after the
4258    similar expression ORIG.  */
4259
4260 tree
4261 add_stmt_to_compound (tree orig, tree new_expr)
4262 {
4263   if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
4264     return orig;
4265   if (!orig || !TREE_SIDE_EFFECTS (orig))
4266     return new_expr;
4267   return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
4268 }
4269
4270 /* Like stabilize_expr, but for a call whose arguments we want to
4271    pre-evaluate.  CALL is modified in place to use the pre-evaluated
4272    arguments, while, upon return, *INITP contains an expression to
4273    compute the arguments.  */
4274
4275 void
4276 stabilize_call (tree call, tree *initp)
4277 {
4278   tree inits = NULL_TREE;
4279   int i;
4280   int nargs = call_expr_nargs (call);
4281
4282   if (call == error_mark_node || processing_template_decl)
4283     {
4284       *initp = NULL_TREE;
4285       return;
4286     }
4287
4288   gcc_assert (TREE_CODE (call) == CALL_EXPR);
4289
4290   for (i = 0; i < nargs; i++)
4291     {
4292       tree init;
4293       CALL_EXPR_ARG (call, i) =
4294         stabilize_expr (CALL_EXPR_ARG (call, i), &init);
4295       inits = add_stmt_to_compound (inits, init);
4296     }
4297
4298   *initp = inits;
4299 }
4300
4301 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
4302    to pre-evaluate.  CALL is modified in place to use the pre-evaluated
4303    arguments, while, upon return, *INITP contains an expression to
4304    compute the arguments.  */
4305
4306 static void
4307 stabilize_aggr_init (tree call, tree *initp)
4308 {
4309   tree inits = NULL_TREE;
4310   int i;
4311   int nargs = aggr_init_expr_nargs (call);
4312
4313   if (call == error_mark_node)
4314     return;
4315
4316   gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
4317
4318   for (i = 0; i < nargs; i++)
4319     {
4320       tree init;
4321       AGGR_INIT_EXPR_ARG (call, i) =
4322         stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
4323       inits = add_stmt_to_compound (inits, init);
4324     }
4325
4326   *initp = inits;
4327 }
4328
4329 /* Like stabilize_expr, but for an initialization.  
4330
4331    If the initialization is for an object of class type, this function
4332    takes care not to introduce additional temporaries.
4333
4334    Returns TRUE iff the expression was successfully pre-evaluated,
4335    i.e., if INIT is now side-effect free, except for, possibly, a
4336    single call to a constructor.  */
4337
4338 bool
4339 stabilize_init (tree init, tree *initp)
4340 {
4341   tree t = init;
4342
4343   *initp = NULL_TREE;
4344
4345   if (t == error_mark_node || processing_template_decl)
4346     return true;
4347
4348   if (TREE_CODE (t) == INIT_EXPR)
4349     t = TREE_OPERAND (t, 1);
4350   if (TREE_CODE (t) == TARGET_EXPR)
4351     t = TARGET_EXPR_INITIAL (t);
4352
4353   /* If the RHS can be stabilized without breaking copy elision, stabilize
4354      it.  We specifically don't stabilize class prvalues here because that
4355      would mean an extra copy, but they might be stabilized below.  */
4356   if (TREE_CODE (init) == INIT_EXPR
4357       && TREE_CODE (t) != CONSTRUCTOR
4358       && TREE_CODE (t) != AGGR_INIT_EXPR
4359       && (SCALAR_TYPE_P (TREE_TYPE (t))
4360           || lvalue_or_rvalue_with_address_p (t)))
4361     {
4362       TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
4363       return true;
4364     }
4365
4366   if (TREE_CODE (t) == COMPOUND_EXPR
4367       && TREE_CODE (init) == INIT_EXPR)
4368     {
4369       tree last = expr_last (t);
4370       /* Handle stabilizing the EMPTY_CLASS_EXPR pattern.  */
4371       if (!TREE_SIDE_EFFECTS (last))
4372         {
4373           *initp = t;
4374           TREE_OPERAND (init, 1) = last;
4375           return true;
4376         }
4377     }
4378
4379   if (TREE_CODE (t) == CONSTRUCTOR)
4380     {
4381       /* Aggregate initialization: stabilize each of the field
4382          initializers.  */
4383       unsigned i;
4384       constructor_elt *ce;
4385       bool good = true;
4386       vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4387       for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4388         {
4389           tree type = TREE_TYPE (ce->value);
4390           tree subinit;
4391           if (TREE_CODE (type) == REFERENCE_TYPE
4392               || SCALAR_TYPE_P (type))
4393             ce->value = stabilize_expr (ce->value, &subinit);
4394           else if (!stabilize_init (ce->value, &subinit))
4395             good = false;
4396           *initp = add_stmt_to_compound (*initp, subinit);
4397         }
4398       return good;
4399     }
4400
4401   if (TREE_CODE (t) == CALL_EXPR)
4402     {
4403       stabilize_call (t, initp);
4404       return true;
4405     }
4406
4407   if (TREE_CODE (t) == AGGR_INIT_EXPR)
4408     {
4409       stabilize_aggr_init (t, initp);
4410       return true;
4411     }
4412
4413   /* The initialization is being performed via a bitwise copy -- and
4414      the item copied may have side effects.  */
4415   return !TREE_SIDE_EFFECTS (init);
4416 }
4417
4418 /* Returns true if a cast to TYPE may appear in an integral constant
4419    expression.  */
4420
4421 bool
4422 cast_valid_in_integral_constant_expression_p (tree type)
4423 {
4424   return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4425           || cxx_dialect >= cxx11
4426           || dependent_type_p (type)
4427           || type == error_mark_node);
4428 }
4429
4430 /* Return true if we need to fix linkage information of DECL.  */
4431
4432 static bool
4433 cp_fix_function_decl_p (tree decl)
4434 {
4435   /* Skip if DECL is not externally visible.  */
4436   if (!TREE_PUBLIC (decl))
4437     return false;
4438
4439   /* We need to fix DECL if it a appears to be exported but with no
4440      function body.  Thunks do not have CFGs and we may need to
4441      handle them specially later.   */
4442   if (!gimple_has_body_p (decl)
4443       && !DECL_THUNK_P (decl)
4444       && !DECL_EXTERNAL (decl))
4445     {
4446       struct cgraph_node *node = cgraph_node::get (decl);
4447
4448       /* Don't fix same_body aliases.  Although they don't have their own
4449          CFG, they share it with what they alias to.  */
4450       if (!node || !node->alias
4451           || !vec_safe_length (node->ref_list.references))
4452         return true;
4453     }
4454
4455   return false;
4456 }
4457
4458 /* Clean the C++ specific parts of the tree T. */
4459
4460 void
4461 cp_free_lang_data (tree t)
4462 {
4463   if (TREE_CODE (t) == METHOD_TYPE
4464       || TREE_CODE (t) == FUNCTION_TYPE)
4465     {
4466       /* Default args are not interesting anymore.  */
4467       tree argtypes = TYPE_ARG_TYPES (t);
4468       while (argtypes)
4469         {
4470           TREE_PURPOSE (argtypes) = 0;
4471           argtypes = TREE_CHAIN (argtypes);
4472         }
4473     }
4474   else if (TREE_CODE (t) == FUNCTION_DECL
4475            && cp_fix_function_decl_p (t))
4476     {
4477       /* If T is used in this translation unit at all,  the definition
4478          must exist somewhere else since we have decided to not emit it
4479          in this TU.  So make it an external reference.  */
4480       DECL_EXTERNAL (t) = 1;
4481       TREE_STATIC (t) = 0;
4482     }
4483   if (TREE_CODE (t) == NAMESPACE_DECL)
4484     {
4485       /* The list of users of a namespace isn't useful for the middle-end
4486          or debug generators.  */
4487       DECL_NAMESPACE_USERS (t) = NULL_TREE;
4488       /* Neither do we need the leftover chaining of namespaces
4489          from the binding level.  */
4490       DECL_CHAIN (t) = NULL_TREE;
4491     }
4492 }
4493
4494 /* Stub for c-common.  Please keep in sync with c-decl.c.
4495    FIXME: If address space support is target specific, then this
4496    should be a C target hook.  But currently this is not possible,
4497    because this function is called via REGISTER_TARGET_PRAGMAS.  */
4498 void
4499 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
4500 {
4501 }
4502
4503 /* Return the number of operands in T that we care about for things like
4504    mangling.  */
4505
4506 int
4507 cp_tree_operand_length (const_tree t)
4508 {
4509   enum tree_code code = TREE_CODE (t);
4510
4511   if (TREE_CODE_CLASS (code) == tcc_vl_exp)
4512     return VL_EXP_OPERAND_LENGTH (t);
4513
4514   return cp_tree_code_length (code);
4515 }
4516
4517 /* Like cp_tree_operand_length, but takes a tree_code CODE.  */
4518
4519 int
4520 cp_tree_code_length (enum tree_code code)
4521 {
4522   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4523
4524   switch (code)
4525     {
4526     case PREINCREMENT_EXPR:
4527     case PREDECREMENT_EXPR:
4528     case POSTINCREMENT_EXPR:
4529     case POSTDECREMENT_EXPR:
4530       return 1;
4531
4532     case ARRAY_REF:
4533       return 2;
4534
4535     case EXPR_PACK_EXPANSION:
4536       return 1;
4537
4538     default:
4539       return TREE_CODE_LENGTH (code);
4540     }
4541 }
4542
4543 /* Implement -Wzero_as_null_pointer_constant.  Return true if the
4544    conditions for the warning hold, false otherwise.  */
4545 bool
4546 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
4547 {
4548   if (c_inhibit_evaluation_warnings == 0
4549       && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
4550     {
4551       warning_at (loc, OPT_Wzero_as_null_pointer_constant,
4552                   "zero as null pointer constant");
4553       return true;
4554     }
4555   return false;
4556 }
4557 \f
4558 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4559 /* Complain that some language-specific thing hanging off a tree
4560    node has been accessed improperly.  */
4561
4562 void
4563 lang_check_failed (const char* file, int line, const char* function)
4564 {
4565   internal_error ("lang_* check: failed in %s, at %s:%d",
4566                   function, trim_filename (file), line);
4567 }
4568 #endif /* ENABLE_TREE_CHECKING */
4569
4570 #include "gt-cp-tree.h"