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