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