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