OSDN Git Service

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