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