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