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