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