xref: /openbsd/gnu/gcc/gcc/cp/tree.c (revision 404b540a)
1*404b540aSrobert /* Language-dependent node constructors for parse phase of GNU compiler.
2*404b540aSrobert    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*404b540aSrobert    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert    Hacked by Michael Tiemann (tiemann@cygnus.com)
5*404b540aSrobert 
6*404b540aSrobert This file is part of GCC.
7*404b540aSrobert 
8*404b540aSrobert GCC is free software; you can redistribute it and/or modify
9*404b540aSrobert it under the terms of the GNU General Public License as published by
10*404b540aSrobert the Free Software Foundation; either version 2, or (at your option)
11*404b540aSrobert any later version.
12*404b540aSrobert 
13*404b540aSrobert GCC is distributed in the hope that it will be useful,
14*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*404b540aSrobert GNU General Public License for more details.
17*404b540aSrobert 
18*404b540aSrobert You should have received a copy of the GNU General Public License
19*404b540aSrobert along with GCC; see the file COPYING.  If not, write to
20*404b540aSrobert the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21*404b540aSrobert Boston, MA 02110-1301, USA.  */
22*404b540aSrobert 
23*404b540aSrobert #include "config.h"
24*404b540aSrobert #include "system.h"
25*404b540aSrobert #include "coretypes.h"
26*404b540aSrobert #include "tm.h"
27*404b540aSrobert #include "tree.h"
28*404b540aSrobert #include "cp-tree.h"
29*404b540aSrobert #include "flags.h"
30*404b540aSrobert #include "real.h"
31*404b540aSrobert #include "rtl.h"
32*404b540aSrobert #include "toplev.h"
33*404b540aSrobert #include "insn-config.h"
34*404b540aSrobert #include "integrate.h"
35*404b540aSrobert #include "tree-inline.h"
36*404b540aSrobert #include "debug.h"
37*404b540aSrobert #include "target.h"
38*404b540aSrobert #include "convert.h"
39*404b540aSrobert 
40*404b540aSrobert static tree bot_manip (tree *, int *, void *);
41*404b540aSrobert static tree bot_replace (tree *, int *, void *);
42*404b540aSrobert static tree build_cplus_array_type_1 (tree, tree);
43*404b540aSrobert static int list_hash_eq (const void *, const void *);
44*404b540aSrobert static hashval_t list_hash_pieces (tree, tree, tree);
45*404b540aSrobert static hashval_t list_hash (const void *);
46*404b540aSrobert static cp_lvalue_kind lvalue_p_1 (tree, int);
47*404b540aSrobert static tree build_target_expr (tree, tree);
48*404b540aSrobert static tree count_trees_r (tree *, int *, void *);
49*404b540aSrobert static tree verify_stmt_tree_r (tree *, int *, void *);
50*404b540aSrobert static tree build_local_temp (tree);
51*404b540aSrobert 
52*404b540aSrobert static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
53*404b540aSrobert static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
54*404b540aSrobert static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
55*404b540aSrobert 
56*404b540aSrobert /* If REF is an lvalue, returns the kind of lvalue that REF is.
57*404b540aSrobert    Otherwise, returns clk_none.  If TREAT_CLASS_RVALUES_AS_LVALUES is
58*404b540aSrobert    nonzero, rvalues of class type are considered lvalues.  */
59*404b540aSrobert 
60*404b540aSrobert static cp_lvalue_kind
lvalue_p_1(tree ref,int treat_class_rvalues_as_lvalues)61*404b540aSrobert lvalue_p_1 (tree ref,
62*404b540aSrobert 	    int treat_class_rvalues_as_lvalues)
63*404b540aSrobert {
64*404b540aSrobert   cp_lvalue_kind op1_lvalue_kind = clk_none;
65*404b540aSrobert   cp_lvalue_kind op2_lvalue_kind = clk_none;
66*404b540aSrobert 
67*404b540aSrobert   if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
68*404b540aSrobert     return clk_ordinary;
69*404b540aSrobert 
70*404b540aSrobert   if (ref == current_class_ptr)
71*404b540aSrobert     return clk_none;
72*404b540aSrobert 
73*404b540aSrobert   switch (TREE_CODE (ref))
74*404b540aSrobert     {
75*404b540aSrobert       /* preincrements and predecrements are valid lvals, provided
76*404b540aSrobert 	 what they refer to are valid lvals.  */
77*404b540aSrobert     case PREINCREMENT_EXPR:
78*404b540aSrobert     case PREDECREMENT_EXPR:
79*404b540aSrobert     case SAVE_EXPR:
80*404b540aSrobert     case TRY_CATCH_EXPR:
81*404b540aSrobert     case WITH_CLEANUP_EXPR:
82*404b540aSrobert     case REALPART_EXPR:
83*404b540aSrobert     case IMAGPART_EXPR:
84*404b540aSrobert       return lvalue_p_1 (TREE_OPERAND (ref, 0),
85*404b540aSrobert 			 treat_class_rvalues_as_lvalues);
86*404b540aSrobert 
87*404b540aSrobert     case COMPONENT_REF:
88*404b540aSrobert       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
89*404b540aSrobert 				    treat_class_rvalues_as_lvalues);
90*404b540aSrobert       /* Look at the member designator.  */
91*404b540aSrobert       if (!op1_lvalue_kind
92*404b540aSrobert 	  /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
93*404b540aSrobert 	     situations.  */
94*404b540aSrobert 	  || TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
95*404b540aSrobert 	;
96*404b540aSrobert       else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
97*404b540aSrobert 	{
98*404b540aSrobert 	  /* Clear the ordinary bit.  If this object was a class
99*404b540aSrobert 	     rvalue we want to preserve that information.  */
100*404b540aSrobert 	  op1_lvalue_kind &= ~clk_ordinary;
101*404b540aSrobert 	  /* The lvalue is for a bitfield.  */
102*404b540aSrobert 	  op1_lvalue_kind |= clk_bitfield;
103*404b540aSrobert 	}
104*404b540aSrobert       else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
105*404b540aSrobert 	op1_lvalue_kind |= clk_packed;
106*404b540aSrobert 
107*404b540aSrobert       return op1_lvalue_kind;
108*404b540aSrobert 
109*404b540aSrobert     case STRING_CST:
110*404b540aSrobert       return clk_ordinary;
111*404b540aSrobert 
112*404b540aSrobert     case CONST_DECL:
113*404b540aSrobert     case VAR_DECL:
114*404b540aSrobert       if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
115*404b540aSrobert 	  && DECL_LANG_SPECIFIC (ref)
116*404b540aSrobert 	  && DECL_IN_AGGR_P (ref))
117*404b540aSrobert 	return clk_none;
118*404b540aSrobert     case INDIRECT_REF:
119*404b540aSrobert     case ARRAY_REF:
120*404b540aSrobert     case PARM_DECL:
121*404b540aSrobert     case RESULT_DECL:
122*404b540aSrobert       if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
123*404b540aSrobert 	return clk_ordinary;
124*404b540aSrobert       break;
125*404b540aSrobert 
126*404b540aSrobert       /* A currently unresolved scope ref.  */
127*404b540aSrobert     case SCOPE_REF:
128*404b540aSrobert       gcc_unreachable ();
129*404b540aSrobert     case MAX_EXPR:
130*404b540aSrobert     case MIN_EXPR:
131*404b540aSrobert       /* Disallow <? and >? as lvalues if either argument side-effects.  */
132*404b540aSrobert       if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
133*404b540aSrobert 	  || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
134*404b540aSrobert 	return clk_none;
135*404b540aSrobert       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
136*404b540aSrobert 				    treat_class_rvalues_as_lvalues);
137*404b540aSrobert       op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
138*404b540aSrobert 				    treat_class_rvalues_as_lvalues);
139*404b540aSrobert       break;
140*404b540aSrobert 
141*404b540aSrobert     case COND_EXPR:
142*404b540aSrobert       op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
143*404b540aSrobert 				    treat_class_rvalues_as_lvalues);
144*404b540aSrobert       op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
145*404b540aSrobert 				    treat_class_rvalues_as_lvalues);
146*404b540aSrobert       break;
147*404b540aSrobert 
148*404b540aSrobert     case MODIFY_EXPR:
149*404b540aSrobert       return clk_ordinary;
150*404b540aSrobert 
151*404b540aSrobert     case COMPOUND_EXPR:
152*404b540aSrobert       return lvalue_p_1 (TREE_OPERAND (ref, 1),
153*404b540aSrobert 			 treat_class_rvalues_as_lvalues);
154*404b540aSrobert 
155*404b540aSrobert     case TARGET_EXPR:
156*404b540aSrobert       return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
157*404b540aSrobert 
158*404b540aSrobert     case VA_ARG_EXPR:
159*404b540aSrobert       return (treat_class_rvalues_as_lvalues
160*404b540aSrobert 	      && CLASS_TYPE_P (TREE_TYPE (ref))
161*404b540aSrobert 	      ? clk_class : clk_none);
162*404b540aSrobert 
163*404b540aSrobert     case CALL_EXPR:
164*404b540aSrobert       /* Any class-valued call would be wrapped in a TARGET_EXPR.  */
165*404b540aSrobert       return clk_none;
166*404b540aSrobert 
167*404b540aSrobert     case FUNCTION_DECL:
168*404b540aSrobert       /* All functions (except non-static-member functions) are
169*404b540aSrobert 	 lvalues.  */
170*404b540aSrobert       return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
171*404b540aSrobert 	      ? clk_none : clk_ordinary);
172*404b540aSrobert 
173*404b540aSrobert     case NON_DEPENDENT_EXPR:
174*404b540aSrobert       /* We must consider NON_DEPENDENT_EXPRs to be lvalues so that
175*404b540aSrobert 	 things like "&E" where "E" is an expression with a
176*404b540aSrobert 	 non-dependent type work. It is safe to be lenient because an
177*404b540aSrobert 	 error will be issued when the template is instantiated if "E"
178*404b540aSrobert 	 is not an lvalue.  */
179*404b540aSrobert       return clk_ordinary;
180*404b540aSrobert 
181*404b540aSrobert     default:
182*404b540aSrobert       break;
183*404b540aSrobert     }
184*404b540aSrobert 
185*404b540aSrobert   /* If one operand is not an lvalue at all, then this expression is
186*404b540aSrobert      not an lvalue.  */
187*404b540aSrobert   if (!op1_lvalue_kind || !op2_lvalue_kind)
188*404b540aSrobert     return clk_none;
189*404b540aSrobert 
190*404b540aSrobert   /* Otherwise, it's an lvalue, and it has all the odd properties
191*404b540aSrobert      contributed by either operand.  */
192*404b540aSrobert   op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
193*404b540aSrobert   /* It's not an ordinary lvalue if it involves either a bit-field or
194*404b540aSrobert      a class rvalue.  */
195*404b540aSrobert   if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
196*404b540aSrobert     op1_lvalue_kind &= ~clk_ordinary;
197*404b540aSrobert   return op1_lvalue_kind;
198*404b540aSrobert }
199*404b540aSrobert 
200*404b540aSrobert /* Returns the kind of lvalue that REF is, in the sense of
201*404b540aSrobert    [basic.lval].  This function should really be named lvalue_p; it
202*404b540aSrobert    computes the C++ definition of lvalue.  */
203*404b540aSrobert 
204*404b540aSrobert cp_lvalue_kind
real_lvalue_p(tree ref)205*404b540aSrobert real_lvalue_p (tree ref)
206*404b540aSrobert {
207*404b540aSrobert   return lvalue_p_1 (ref,
208*404b540aSrobert 		     /*treat_class_rvalues_as_lvalues=*/0);
209*404b540aSrobert }
210*404b540aSrobert 
211*404b540aSrobert /* This differs from real_lvalue_p in that class rvalues are
212*404b540aSrobert    considered lvalues.  */
213*404b540aSrobert 
214*404b540aSrobert int
lvalue_p(tree ref)215*404b540aSrobert lvalue_p (tree ref)
216*404b540aSrobert {
217*404b540aSrobert   return
218*404b540aSrobert     (lvalue_p_1 (ref, /*class rvalue ok*/ 1) != clk_none);
219*404b540aSrobert }
220*404b540aSrobert 
221*404b540aSrobert /* Test whether DECL is a builtin that may appear in a
222*404b540aSrobert    constant-expression. */
223*404b540aSrobert 
224*404b540aSrobert bool
builtin_valid_in_constant_expr_p(tree decl)225*404b540aSrobert builtin_valid_in_constant_expr_p (tree decl)
226*404b540aSrobert {
227*404b540aSrobert   /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
228*404b540aSrobert      in constant-expressions.  We may want to add other builtins later. */
229*404b540aSrobert   return DECL_IS_BUILTIN_CONSTANT_P (decl);
230*404b540aSrobert }
231*404b540aSrobert 
232*404b540aSrobert /* Build a TARGET_EXPR, initializing the DECL with the VALUE.  */
233*404b540aSrobert 
234*404b540aSrobert static tree
build_target_expr(tree decl,tree value)235*404b540aSrobert build_target_expr (tree decl, tree value)
236*404b540aSrobert {
237*404b540aSrobert   tree t;
238*404b540aSrobert 
239*404b540aSrobert   t = build4 (TARGET_EXPR, TREE_TYPE (decl), decl, value,
240*404b540aSrobert 	      cxx_maybe_build_cleanup (decl), NULL_TREE);
241*404b540aSrobert   /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
242*404b540aSrobert      ignore the TARGET_EXPR.  If there really turn out to be no
243*404b540aSrobert      side-effects, then the optimizer should be able to get rid of
244*404b540aSrobert      whatever code is generated anyhow.  */
245*404b540aSrobert   TREE_SIDE_EFFECTS (t) = 1;
246*404b540aSrobert 
247*404b540aSrobert   return t;
248*404b540aSrobert }
249*404b540aSrobert 
250*404b540aSrobert /* Return an undeclared local temporary of type TYPE for use in building a
251*404b540aSrobert    TARGET_EXPR.  */
252*404b540aSrobert 
253*404b540aSrobert static tree
build_local_temp(tree type)254*404b540aSrobert build_local_temp (tree type)
255*404b540aSrobert {
256*404b540aSrobert   tree slot = build_decl (VAR_DECL, NULL_TREE, type);
257*404b540aSrobert   DECL_ARTIFICIAL (slot) = 1;
258*404b540aSrobert   DECL_IGNORED_P (slot) = 1;
259*404b540aSrobert   DECL_CONTEXT (slot) = current_function_decl;
260*404b540aSrobert   layout_decl (slot, 0);
261*404b540aSrobert   return slot;
262*404b540aSrobert }
263*404b540aSrobert 
264*404b540aSrobert /* INIT is a CALL_EXPR which needs info about its target.
265*404b540aSrobert    TYPE is the type that this initialization should appear to have.
266*404b540aSrobert 
267*404b540aSrobert    Build an encapsulation of the initialization to perform
268*404b540aSrobert    and return it so that it can be processed by language-independent
269*404b540aSrobert    and language-specific expression expanders.  */
270*404b540aSrobert 
271*404b540aSrobert tree
build_cplus_new(tree type,tree init)272*404b540aSrobert build_cplus_new (tree type, tree init)
273*404b540aSrobert {
274*404b540aSrobert   tree fn;
275*404b540aSrobert   tree slot;
276*404b540aSrobert   tree rval;
277*404b540aSrobert   int is_ctor;
278*404b540aSrobert 
279*404b540aSrobert   /* Make sure that we're not trying to create an instance of an
280*404b540aSrobert      abstract class.  */
281*404b540aSrobert   abstract_virtuals_error (NULL_TREE, type);
282*404b540aSrobert 
283*404b540aSrobert   if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
284*404b540aSrobert     return convert (type, init);
285*404b540aSrobert 
286*404b540aSrobert   fn = TREE_OPERAND (init, 0);
287*404b540aSrobert   is_ctor = (TREE_CODE (fn) == ADDR_EXPR
288*404b540aSrobert 	     && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
289*404b540aSrobert 	     && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
290*404b540aSrobert 
291*404b540aSrobert   slot = build_local_temp (type);
292*404b540aSrobert 
293*404b540aSrobert   /* We split the CALL_EXPR into its function and its arguments here.
294*404b540aSrobert      Then, in expand_expr, we put them back together.  The reason for
295*404b540aSrobert      this is that this expression might be a default argument
296*404b540aSrobert      expression.  In that case, we need a new temporary every time the
297*404b540aSrobert      expression is used.  That's what break_out_target_exprs does; it
298*404b540aSrobert      replaces every AGGR_INIT_EXPR with a copy that uses a fresh
299*404b540aSrobert      temporary slot.  Then, expand_expr builds up a call-expression
300*404b540aSrobert      using the new slot.  */
301*404b540aSrobert 
302*404b540aSrobert   /* If we don't need to use a constructor to create an object of this
303*404b540aSrobert      type, don't mess with AGGR_INIT_EXPR.  */
304*404b540aSrobert   if (is_ctor || TREE_ADDRESSABLE (type))
305*404b540aSrobert     {
306*404b540aSrobert       rval = build3 (AGGR_INIT_EXPR, void_type_node, fn,
307*404b540aSrobert 		     TREE_OPERAND (init, 1), slot);
308*404b540aSrobert       TREE_SIDE_EFFECTS (rval) = 1;
309*404b540aSrobert       AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
310*404b540aSrobert     }
311*404b540aSrobert   else
312*404b540aSrobert     rval = init;
313*404b540aSrobert 
314*404b540aSrobert   rval = build_target_expr (slot, rval);
315*404b540aSrobert   TARGET_EXPR_IMPLICIT_P (rval) = 1;
316*404b540aSrobert 
317*404b540aSrobert   return rval;
318*404b540aSrobert }
319*404b540aSrobert 
320*404b540aSrobert /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
321*404b540aSrobert    indicated TYPE.  */
322*404b540aSrobert 
323*404b540aSrobert tree
build_target_expr_with_type(tree init,tree type)324*404b540aSrobert build_target_expr_with_type (tree init, tree type)
325*404b540aSrobert {
326*404b540aSrobert   gcc_assert (!VOID_TYPE_P (type));
327*404b540aSrobert 
328*404b540aSrobert   if (TREE_CODE (init) == TARGET_EXPR)
329*404b540aSrobert     return init;
330*404b540aSrobert   else if (CLASS_TYPE_P (type) && !TYPE_HAS_TRIVIAL_INIT_REF (type)
331*404b540aSrobert 	   && TREE_CODE (init) != COND_EXPR
332*404b540aSrobert 	   && TREE_CODE (init) != CONSTRUCTOR
333*404b540aSrobert 	   && TREE_CODE (init) != VA_ARG_EXPR)
334*404b540aSrobert     /* We need to build up a copy constructor call.  COND_EXPR is a special
335*404b540aSrobert        case because we already have copies on the arms and we don't want
336*404b540aSrobert        another one here.  A CONSTRUCTOR is aggregate initialization, which
337*404b540aSrobert        is handled separately.  A VA_ARG_EXPR is magic creation of an
338*404b540aSrobert        aggregate; there's no additional work to be done.  */
339*404b540aSrobert     return force_rvalue (init);
340*404b540aSrobert 
341*404b540aSrobert   return force_target_expr (type, init);
342*404b540aSrobert }
343*404b540aSrobert 
344*404b540aSrobert /* Like the above function, but without the checking.  This function should
345*404b540aSrobert    only be used by code which is deliberately trying to subvert the type
346*404b540aSrobert    system, such as call_builtin_trap.  */
347*404b540aSrobert 
348*404b540aSrobert tree
force_target_expr(tree type,tree init)349*404b540aSrobert force_target_expr (tree type, tree init)
350*404b540aSrobert {
351*404b540aSrobert   tree slot;
352*404b540aSrobert 
353*404b540aSrobert   gcc_assert (!VOID_TYPE_P (type));
354*404b540aSrobert 
355*404b540aSrobert   slot = build_local_temp (type);
356*404b540aSrobert   return build_target_expr (slot, init);
357*404b540aSrobert }
358*404b540aSrobert 
359*404b540aSrobert /* Like build_target_expr_with_type, but use the type of INIT.  */
360*404b540aSrobert 
361*404b540aSrobert tree
get_target_expr(tree init)362*404b540aSrobert get_target_expr (tree init)
363*404b540aSrobert {
364*404b540aSrobert   return build_target_expr_with_type (init, TREE_TYPE (init));
365*404b540aSrobert }
366*404b540aSrobert 
367*404b540aSrobert /* If EXPR is a bitfield reference, convert it to the declared type of
368*404b540aSrobert    the bitfield, and return the resulting expression.  Otherwise,
369*404b540aSrobert    return EXPR itself.  */
370*404b540aSrobert 
371*404b540aSrobert tree
convert_bitfield_to_declared_type(tree expr)372*404b540aSrobert convert_bitfield_to_declared_type (tree expr)
373*404b540aSrobert {
374*404b540aSrobert   tree bitfield_type;
375*404b540aSrobert 
376*404b540aSrobert   bitfield_type = is_bitfield_expr_with_lowered_type (expr);
377*404b540aSrobert   if (bitfield_type)
378*404b540aSrobert     expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
379*404b540aSrobert 			       expr);
380*404b540aSrobert   return expr;
381*404b540aSrobert }
382*404b540aSrobert 
383*404b540aSrobert /* EXPR is being used in an rvalue context.  Return a version of EXPR
384*404b540aSrobert    that is marked as an rvalue.  */
385*404b540aSrobert 
386*404b540aSrobert tree
rvalue(tree expr)387*404b540aSrobert rvalue (tree expr)
388*404b540aSrobert {
389*404b540aSrobert   tree type;
390*404b540aSrobert 
391*404b540aSrobert   if (error_operand_p (expr))
392*404b540aSrobert     return expr;
393*404b540aSrobert 
394*404b540aSrobert   /* [basic.lval]
395*404b540aSrobert 
396*404b540aSrobert      Non-class rvalues always have cv-unqualified types.  */
397*404b540aSrobert   type = TREE_TYPE (expr);
398*404b540aSrobert   if (!CLASS_TYPE_P (type) && cp_type_quals (type))
399*404b540aSrobert     type = TYPE_MAIN_VARIANT (type);
400*404b540aSrobert 
401*404b540aSrobert   if (!processing_template_decl && real_lvalue_p (expr))
402*404b540aSrobert     expr = build1 (NON_LVALUE_EXPR, type, expr);
403*404b540aSrobert   else if (type != TREE_TYPE (expr))
404*404b540aSrobert     expr = build_nop (type, expr);
405*404b540aSrobert 
406*404b540aSrobert   return expr;
407*404b540aSrobert }
408*404b540aSrobert 
409*404b540aSrobert 
410*404b540aSrobert static tree
build_cplus_array_type_1(tree elt_type,tree index_type)411*404b540aSrobert build_cplus_array_type_1 (tree elt_type, tree index_type)
412*404b540aSrobert {
413*404b540aSrobert   tree t;
414*404b540aSrobert 
415*404b540aSrobert   if (elt_type == error_mark_node || index_type == error_mark_node)
416*404b540aSrobert     return error_mark_node;
417*404b540aSrobert 
418*404b540aSrobert   if (dependent_type_p (elt_type)
419*404b540aSrobert       || (index_type
420*404b540aSrobert 	  && value_dependent_expression_p (TYPE_MAX_VALUE (index_type))))
421*404b540aSrobert     {
422*404b540aSrobert       t = make_node (ARRAY_TYPE);
423*404b540aSrobert       TREE_TYPE (t) = elt_type;
424*404b540aSrobert       TYPE_DOMAIN (t) = index_type;
425*404b540aSrobert     }
426*404b540aSrobert   else
427*404b540aSrobert     t = build_array_type (elt_type, index_type);
428*404b540aSrobert 
429*404b540aSrobert   /* Push these needs up so that initialization takes place
430*404b540aSrobert      more easily.  */
431*404b540aSrobert   TYPE_NEEDS_CONSTRUCTING (t)
432*404b540aSrobert     = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
433*404b540aSrobert   TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
434*404b540aSrobert     = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
435*404b540aSrobert   return t;
436*404b540aSrobert }
437*404b540aSrobert 
438*404b540aSrobert tree
build_cplus_array_type(tree elt_type,tree index_type)439*404b540aSrobert build_cplus_array_type (tree elt_type, tree index_type)
440*404b540aSrobert {
441*404b540aSrobert   tree t;
442*404b540aSrobert   int type_quals = cp_type_quals (elt_type);
443*404b540aSrobert 
444*404b540aSrobert   if (type_quals != TYPE_UNQUALIFIED)
445*404b540aSrobert     elt_type = cp_build_qualified_type (elt_type, TYPE_UNQUALIFIED);
446*404b540aSrobert 
447*404b540aSrobert   t = build_cplus_array_type_1 (elt_type, index_type);
448*404b540aSrobert 
449*404b540aSrobert   if (type_quals != TYPE_UNQUALIFIED)
450*404b540aSrobert     t = cp_build_qualified_type (t, type_quals);
451*404b540aSrobert 
452*404b540aSrobert   return t;
453*404b540aSrobert }
454*404b540aSrobert 
455*404b540aSrobert /* Make a variant of TYPE, qualified with the TYPE_QUALS.  Handles
456*404b540aSrobert    arrays correctly.  In particular, if TYPE is an array of T's, and
457*404b540aSrobert    TYPE_QUALS is non-empty, returns an array of qualified T's.
458*404b540aSrobert 
459*404b540aSrobert    FLAGS determines how to deal with illformed qualifications. If
460*404b540aSrobert    tf_ignore_bad_quals is set, then bad qualifications are dropped
461*404b540aSrobert    (this is permitted if TYPE was introduced via a typedef or template
462*404b540aSrobert    type parameter). If bad qualifications are dropped and tf_warning
463*404b540aSrobert    is set, then a warning is issued for non-const qualifications.  If
464*404b540aSrobert    tf_ignore_bad_quals is not set and tf_error is not set, we
465*404b540aSrobert    return error_mark_node. Otherwise, we issue an error, and ignore
466*404b540aSrobert    the qualifications.
467*404b540aSrobert 
468*404b540aSrobert    Qualification of a reference type is valid when the reference came
469*404b540aSrobert    via a typedef or template type argument. [dcl.ref] No such
470*404b540aSrobert    dispensation is provided for qualifying a function type.  [dcl.fct]
471*404b540aSrobert    DR 295 queries this and the proposed resolution brings it into line
472*404b540aSrobert    with qualifying a reference.  We implement the DR.  We also behave
473*404b540aSrobert    in a similar manner for restricting non-pointer types.  */
474*404b540aSrobert 
475*404b540aSrobert tree
cp_build_qualified_type_real(tree type,int type_quals,tsubst_flags_t complain)476*404b540aSrobert cp_build_qualified_type_real (tree type,
477*404b540aSrobert 			      int type_quals,
478*404b540aSrobert 			      tsubst_flags_t complain)
479*404b540aSrobert {
480*404b540aSrobert   tree result;
481*404b540aSrobert   int bad_quals = TYPE_UNQUALIFIED;
482*404b540aSrobert 
483*404b540aSrobert   if (type == error_mark_node)
484*404b540aSrobert     return type;
485*404b540aSrobert 
486*404b540aSrobert   if (type_quals == cp_type_quals (type))
487*404b540aSrobert     return type;
488*404b540aSrobert 
489*404b540aSrobert   if (TREE_CODE (type) == ARRAY_TYPE)
490*404b540aSrobert     {
491*404b540aSrobert       /* In C++, the qualification really applies to the array element
492*404b540aSrobert 	 type.  Obtain the appropriately qualified element type.  */
493*404b540aSrobert       tree t;
494*404b540aSrobert       tree element_type
495*404b540aSrobert 	= cp_build_qualified_type_real (TREE_TYPE (type),
496*404b540aSrobert 					type_quals,
497*404b540aSrobert 					complain);
498*404b540aSrobert 
499*404b540aSrobert       if (element_type == error_mark_node)
500*404b540aSrobert 	return error_mark_node;
501*404b540aSrobert 
502*404b540aSrobert       /* See if we already have an identically qualified type.  */
503*404b540aSrobert       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
504*404b540aSrobert 	if (cp_type_quals (t) == type_quals
505*404b540aSrobert 	    && TYPE_NAME (t) == TYPE_NAME (type)
506*404b540aSrobert 	    && TYPE_CONTEXT (t) == TYPE_CONTEXT (type))
507*404b540aSrobert 	  break;
508*404b540aSrobert 
509*404b540aSrobert       if (!t)
510*404b540aSrobert 	{
511*404b540aSrobert 	  /* Make a new array type, just like the old one, but with the
512*404b540aSrobert 	     appropriately qualified element type.  */
513*404b540aSrobert 	  t = build_variant_type_copy (type);
514*404b540aSrobert 	  TREE_TYPE (t) = element_type;
515*404b540aSrobert 	}
516*404b540aSrobert 
517*404b540aSrobert       /* Even if we already had this variant, we update
518*404b540aSrobert 	 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
519*404b540aSrobert 	 they changed since the variant was originally created.
520*404b540aSrobert 
521*404b540aSrobert 	 This seems hokey; if there is some way to use a previous
522*404b540aSrobert 	 variant *without* coming through here,
523*404b540aSrobert 	 TYPE_NEEDS_CONSTRUCTING will never be updated.  */
524*404b540aSrobert       TYPE_NEEDS_CONSTRUCTING (t)
525*404b540aSrobert 	= TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
526*404b540aSrobert       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
527*404b540aSrobert 	= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
528*404b540aSrobert       return t;
529*404b540aSrobert     }
530*404b540aSrobert   else if (TYPE_PTRMEMFUNC_P (type))
531*404b540aSrobert     {
532*404b540aSrobert       /* For a pointer-to-member type, we can't just return a
533*404b540aSrobert 	 cv-qualified version of the RECORD_TYPE.  If we do, we
534*404b540aSrobert 	 haven't changed the field that contains the actual pointer to
535*404b540aSrobert 	 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong.  */
536*404b540aSrobert       tree t;
537*404b540aSrobert 
538*404b540aSrobert       t = TYPE_PTRMEMFUNC_FN_TYPE (type);
539*404b540aSrobert       t = cp_build_qualified_type_real (t, type_quals, complain);
540*404b540aSrobert       return build_ptrmemfunc_type (t);
541*404b540aSrobert     }
542*404b540aSrobert 
543*404b540aSrobert   /* A reference or method type shall not be cv qualified.
544*404b540aSrobert      [dcl.ref], [dct.fct]  */
545*404b540aSrobert   if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
546*404b540aSrobert       && (TREE_CODE (type) == REFERENCE_TYPE
547*404b540aSrobert 	  || TREE_CODE (type) == METHOD_TYPE))
548*404b540aSrobert     {
549*404b540aSrobert       bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
550*404b540aSrobert       type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
551*404b540aSrobert     }
552*404b540aSrobert 
553*404b540aSrobert   /* A restrict-qualified type must be a pointer (or reference)
554*404b540aSrobert      to object or incomplete type, or a function type. */
555*404b540aSrobert   if ((type_quals & TYPE_QUAL_RESTRICT)
556*404b540aSrobert       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
557*404b540aSrobert       && TREE_CODE (type) != TYPENAME_TYPE
558*404b540aSrobert       && TREE_CODE (type) != FUNCTION_TYPE
559*404b540aSrobert       && !POINTER_TYPE_P (type))
560*404b540aSrobert     {
561*404b540aSrobert       bad_quals |= TYPE_QUAL_RESTRICT;
562*404b540aSrobert       type_quals &= ~TYPE_QUAL_RESTRICT;
563*404b540aSrobert     }
564*404b540aSrobert 
565*404b540aSrobert   if (bad_quals == TYPE_UNQUALIFIED)
566*404b540aSrobert     /*OK*/;
567*404b540aSrobert   else if (!(complain & (tf_error | tf_ignore_bad_quals)))
568*404b540aSrobert     return error_mark_node;
569*404b540aSrobert   else
570*404b540aSrobert     {
571*404b540aSrobert       if (complain & tf_ignore_bad_quals)
572*404b540aSrobert 	/* We're not going to warn about constifying things that can't
573*404b540aSrobert 	   be constified.  */
574*404b540aSrobert 	bad_quals &= ~TYPE_QUAL_CONST;
575*404b540aSrobert       if (bad_quals)
576*404b540aSrobert 	{
577*404b540aSrobert 	  tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
578*404b540aSrobert 
579*404b540aSrobert 	  if (!(complain & tf_ignore_bad_quals))
580*404b540aSrobert 	    error ("%qV qualifiers cannot be applied to %qT",
581*404b540aSrobert 		   bad_type, type);
582*404b540aSrobert 	}
583*404b540aSrobert     }
584*404b540aSrobert 
585*404b540aSrobert   /* Retrieve (or create) the appropriately qualified variant.  */
586*404b540aSrobert   result = build_qualified_type (type, type_quals);
587*404b540aSrobert 
588*404b540aSrobert   /* If this was a pointer-to-method type, and we just made a copy,
589*404b540aSrobert      then we need to unshare the record that holds the cached
590*404b540aSrobert      pointer-to-member-function type, because these will be distinct
591*404b540aSrobert      between the unqualified and qualified types.  */
592*404b540aSrobert   if (result != type
593*404b540aSrobert       && TREE_CODE (type) == POINTER_TYPE
594*404b540aSrobert       && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
595*404b540aSrobert     TYPE_LANG_SPECIFIC (result) = NULL;
596*404b540aSrobert 
597*404b540aSrobert   return result;
598*404b540aSrobert }
599*404b540aSrobert 
600*404b540aSrobert /* Returns the canonical version of TYPE.  In other words, if TYPE is
601*404b540aSrobert    a typedef, returns the underlying type.  The cv-qualification of
602*404b540aSrobert    the type returned matches the type input; they will always be
603*404b540aSrobert    compatible types.  */
604*404b540aSrobert 
605*404b540aSrobert tree
canonical_type_variant(tree t)606*404b540aSrobert canonical_type_variant (tree t)
607*404b540aSrobert {
608*404b540aSrobert   return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), cp_type_quals (t));
609*404b540aSrobert }
610*404b540aSrobert 
611*404b540aSrobert /* Makes a copy of BINFO and TYPE, which is to be inherited into a
612*404b540aSrobert    graph dominated by T.  If BINFO is NULL, TYPE is a dependent base,
613*404b540aSrobert    and we do a shallow copy.  If BINFO is non-NULL, we do a deep copy.
614*404b540aSrobert    VIRT indicates whether TYPE is inherited virtually or not.
615*404b540aSrobert    IGO_PREV points at the previous binfo of the inheritance graph
616*404b540aSrobert    order chain.  The newly copied binfo's TREE_CHAIN forms this
617*404b540aSrobert    ordering.
618*404b540aSrobert 
619*404b540aSrobert    The CLASSTYPE_VBASECLASSES vector of T is constructed in the
620*404b540aSrobert    correct order. That is in the order the bases themselves should be
621*404b540aSrobert    constructed in.
622*404b540aSrobert 
623*404b540aSrobert    The BINFO_INHERITANCE of a virtual base class points to the binfo
624*404b540aSrobert    of the most derived type. ??? We could probably change this so that
625*404b540aSrobert    BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
626*404b540aSrobert    remove a field.  They currently can only differ for primary virtual
627*404b540aSrobert    virtual bases.  */
628*404b540aSrobert 
629*404b540aSrobert tree
copy_binfo(tree binfo,tree type,tree t,tree * igo_prev,int virt)630*404b540aSrobert copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
631*404b540aSrobert {
632*404b540aSrobert   tree new_binfo;
633*404b540aSrobert 
634*404b540aSrobert   if (virt)
635*404b540aSrobert     {
636*404b540aSrobert       /* See if we've already made this virtual base.  */
637*404b540aSrobert       new_binfo = binfo_for_vbase (type, t);
638*404b540aSrobert       if (new_binfo)
639*404b540aSrobert 	return new_binfo;
640*404b540aSrobert     }
641*404b540aSrobert 
642*404b540aSrobert   new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
643*404b540aSrobert   BINFO_TYPE (new_binfo) = type;
644*404b540aSrobert 
645*404b540aSrobert   /* Chain it into the inheritance graph.  */
646*404b540aSrobert   TREE_CHAIN (*igo_prev) = new_binfo;
647*404b540aSrobert   *igo_prev = new_binfo;
648*404b540aSrobert 
649*404b540aSrobert   if (binfo)
650*404b540aSrobert     {
651*404b540aSrobert       int ix;
652*404b540aSrobert       tree base_binfo;
653*404b540aSrobert 
654*404b540aSrobert       gcc_assert (!BINFO_DEPENDENT_BASE_P (binfo));
655*404b540aSrobert       gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
656*404b540aSrobert 
657*404b540aSrobert       BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
658*404b540aSrobert       BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
659*404b540aSrobert 
660*404b540aSrobert       /* We do not need to copy the accesses, as they are read only.  */
661*404b540aSrobert       BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
662*404b540aSrobert 
663*404b540aSrobert       /* Recursively copy base binfos of BINFO.  */
664*404b540aSrobert       for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
665*404b540aSrobert 	{
666*404b540aSrobert 	  tree new_base_binfo;
667*404b540aSrobert 
668*404b540aSrobert 	  gcc_assert (!BINFO_DEPENDENT_BASE_P (base_binfo));
669*404b540aSrobert 	  new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
670*404b540aSrobert 				       t, igo_prev,
671*404b540aSrobert 				       BINFO_VIRTUAL_P (base_binfo));
672*404b540aSrobert 
673*404b540aSrobert 	  if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
674*404b540aSrobert 	    BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
675*404b540aSrobert 	  BINFO_BASE_APPEND (new_binfo, new_base_binfo);
676*404b540aSrobert 	}
677*404b540aSrobert     }
678*404b540aSrobert   else
679*404b540aSrobert     BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
680*404b540aSrobert 
681*404b540aSrobert   if (virt)
682*404b540aSrobert     {
683*404b540aSrobert       /* Push it onto the list after any virtual bases it contains
684*404b540aSrobert 	 will have been pushed.  */
685*404b540aSrobert       VEC_quick_push (tree, CLASSTYPE_VBASECLASSES (t), new_binfo);
686*404b540aSrobert       BINFO_VIRTUAL_P (new_binfo) = 1;
687*404b540aSrobert       BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
688*404b540aSrobert     }
689*404b540aSrobert 
690*404b540aSrobert   return new_binfo;
691*404b540aSrobert }
692*404b540aSrobert 
693*404b540aSrobert /* Hashing of lists so that we don't make duplicates.
694*404b540aSrobert    The entry point is `list_hash_canon'.  */
695*404b540aSrobert 
696*404b540aSrobert /* Now here is the hash table.  When recording a list, it is added
697*404b540aSrobert    to the slot whose index is the hash code mod the table size.
698*404b540aSrobert    Note that the hash table is used for several kinds of lists.
699*404b540aSrobert    While all these live in the same table, they are completely independent,
700*404b540aSrobert    and the hash code is computed differently for each of these.  */
701*404b540aSrobert 
702*404b540aSrobert static GTY ((param_is (union tree_node))) htab_t list_hash_table;
703*404b540aSrobert 
704*404b540aSrobert struct list_proxy
705*404b540aSrobert {
706*404b540aSrobert   tree purpose;
707*404b540aSrobert   tree value;
708*404b540aSrobert   tree chain;
709*404b540aSrobert };
710*404b540aSrobert 
711*404b540aSrobert /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
712*404b540aSrobert    for a node we are thinking about adding).  */
713*404b540aSrobert 
714*404b540aSrobert static int
list_hash_eq(const void * entry,const void * data)715*404b540aSrobert list_hash_eq (const void* entry, const void* data)
716*404b540aSrobert {
717*404b540aSrobert   tree t = (tree) entry;
718*404b540aSrobert   struct list_proxy *proxy = (struct list_proxy *) data;
719*404b540aSrobert 
720*404b540aSrobert   return (TREE_VALUE (t) == proxy->value
721*404b540aSrobert 	  && TREE_PURPOSE (t) == proxy->purpose
722*404b540aSrobert 	  && TREE_CHAIN (t) == proxy->chain);
723*404b540aSrobert }
724*404b540aSrobert 
725*404b540aSrobert /* Compute a hash code for a list (chain of TREE_LIST nodes
726*404b540aSrobert    with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
727*404b540aSrobert    TREE_COMMON slots), by adding the hash codes of the individual entries.  */
728*404b540aSrobert 
729*404b540aSrobert static hashval_t
list_hash_pieces(tree purpose,tree value,tree chain)730*404b540aSrobert list_hash_pieces (tree purpose, tree value, tree chain)
731*404b540aSrobert {
732*404b540aSrobert   hashval_t hashcode = 0;
733*404b540aSrobert 
734*404b540aSrobert   if (chain)
735*404b540aSrobert     hashcode += TREE_HASH (chain);
736*404b540aSrobert 
737*404b540aSrobert   if (value)
738*404b540aSrobert     hashcode += TREE_HASH (value);
739*404b540aSrobert   else
740*404b540aSrobert     hashcode += 1007;
741*404b540aSrobert   if (purpose)
742*404b540aSrobert     hashcode += TREE_HASH (purpose);
743*404b540aSrobert   else
744*404b540aSrobert     hashcode += 1009;
745*404b540aSrobert   return hashcode;
746*404b540aSrobert }
747*404b540aSrobert 
748*404b540aSrobert /* Hash an already existing TREE_LIST.  */
749*404b540aSrobert 
750*404b540aSrobert static hashval_t
list_hash(const void * p)751*404b540aSrobert list_hash (const void* p)
752*404b540aSrobert {
753*404b540aSrobert   tree t = (tree) p;
754*404b540aSrobert   return list_hash_pieces (TREE_PURPOSE (t),
755*404b540aSrobert 			   TREE_VALUE (t),
756*404b540aSrobert 			   TREE_CHAIN (t));
757*404b540aSrobert }
758*404b540aSrobert 
759*404b540aSrobert /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
760*404b540aSrobert    object for an identical list if one already exists.  Otherwise, build a
761*404b540aSrobert    new one, and record it as the canonical object.  */
762*404b540aSrobert 
763*404b540aSrobert tree
hash_tree_cons(tree purpose,tree value,tree chain)764*404b540aSrobert hash_tree_cons (tree purpose, tree value, tree chain)
765*404b540aSrobert {
766*404b540aSrobert   int hashcode = 0;
767*404b540aSrobert   void **slot;
768*404b540aSrobert   struct list_proxy proxy;
769*404b540aSrobert 
770*404b540aSrobert   /* Hash the list node.  */
771*404b540aSrobert   hashcode = list_hash_pieces (purpose, value, chain);
772*404b540aSrobert   /* Create a proxy for the TREE_LIST we would like to create.  We
773*404b540aSrobert      don't actually create it so as to avoid creating garbage.  */
774*404b540aSrobert   proxy.purpose = purpose;
775*404b540aSrobert   proxy.value = value;
776*404b540aSrobert   proxy.chain = chain;
777*404b540aSrobert   /* See if it is already in the table.  */
778*404b540aSrobert   slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
779*404b540aSrobert 				   INSERT);
780*404b540aSrobert   /* If not, create a new node.  */
781*404b540aSrobert   if (!*slot)
782*404b540aSrobert     *slot = tree_cons (purpose, value, chain);
783*404b540aSrobert   return (tree) *slot;
784*404b540aSrobert }
785*404b540aSrobert 
786*404b540aSrobert /* Constructor for hashed lists.  */
787*404b540aSrobert 
788*404b540aSrobert tree
hash_tree_chain(tree value,tree chain)789*404b540aSrobert hash_tree_chain (tree value, tree chain)
790*404b540aSrobert {
791*404b540aSrobert   return hash_tree_cons (NULL_TREE, value, chain);
792*404b540aSrobert }
793*404b540aSrobert 
794*404b540aSrobert void
debug_binfo(tree elem)795*404b540aSrobert debug_binfo (tree elem)
796*404b540aSrobert {
797*404b540aSrobert   HOST_WIDE_INT n;
798*404b540aSrobert   tree virtuals;
799*404b540aSrobert 
800*404b540aSrobert   fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
801*404b540aSrobert 	   "\nvtable type:\n",
802*404b540aSrobert 	   TYPE_NAME_STRING (BINFO_TYPE (elem)),
803*404b540aSrobert 	   TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
804*404b540aSrobert   debug_tree (BINFO_TYPE (elem));
805*404b540aSrobert   if (BINFO_VTABLE (elem))
806*404b540aSrobert     fprintf (stderr, "vtable decl \"%s\"\n",
807*404b540aSrobert 	     IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
808*404b540aSrobert   else
809*404b540aSrobert     fprintf (stderr, "no vtable decl yet\n");
810*404b540aSrobert   fprintf (stderr, "virtuals:\n");
811*404b540aSrobert   virtuals = BINFO_VIRTUALS (elem);
812*404b540aSrobert   n = 0;
813*404b540aSrobert 
814*404b540aSrobert   while (virtuals)
815*404b540aSrobert     {
816*404b540aSrobert       tree fndecl = TREE_VALUE (virtuals);
817*404b540aSrobert       fprintf (stderr, "%s [%ld =? %ld]\n",
818*404b540aSrobert 	       IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
819*404b540aSrobert 	       (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
820*404b540aSrobert       ++n;
821*404b540aSrobert       virtuals = TREE_CHAIN (virtuals);
822*404b540aSrobert     }
823*404b540aSrobert }
824*404b540aSrobert 
825*404b540aSrobert /* Build a representation for the qualified name SCOPE::NAME.  TYPE is
826*404b540aSrobert    the type of the result expression, if known, or NULL_TREE if the
827*404b540aSrobert    resulting expression is type-dependent.  If TEMPLATE_P is true,
828*404b540aSrobert    NAME is known to be a template because the user explicitly used the
829*404b540aSrobert    "template" keyword after the "::".
830*404b540aSrobert 
831*404b540aSrobert    All SCOPE_REFs should be built by use of this function.  */
832*404b540aSrobert 
833*404b540aSrobert tree
build_qualified_name(tree type,tree scope,tree name,bool template_p)834*404b540aSrobert build_qualified_name (tree type, tree scope, tree name, bool template_p)
835*404b540aSrobert {
836*404b540aSrobert   tree t;
837*404b540aSrobert   if (type == error_mark_node
838*404b540aSrobert       || scope == error_mark_node
839*404b540aSrobert       || name == error_mark_node)
840*404b540aSrobert     return error_mark_node;
841*404b540aSrobert   t = build2 (SCOPE_REF, type, scope, name);
842*404b540aSrobert   QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
843*404b540aSrobert   return t;
844*404b540aSrobert }
845*404b540aSrobert 
846*404b540aSrobert /* Returns non-zero if X is an expression for a (possibly overloaded)
847*404b540aSrobert    function.  If "f" is a function or function template, "f", "c->f",
848*404b540aSrobert    "c.f", "C::f", and "f<int>" will all be considered possibly
849*404b540aSrobert    overloaded functions.  Returns 2 if the function is actually
850*404b540aSrobert    overloaded, i.e., if it is impossible to know the the type of the
851*404b540aSrobert    function without performing overload resolution.  */
852*404b540aSrobert 
853*404b540aSrobert int
is_overloaded_fn(tree x)854*404b540aSrobert is_overloaded_fn (tree x)
855*404b540aSrobert {
856*404b540aSrobert   /* A baselink is also considered an overloaded function.  */
857*404b540aSrobert   if (TREE_CODE (x) == OFFSET_REF
858*404b540aSrobert       || TREE_CODE (x) == COMPONENT_REF)
859*404b540aSrobert     x = TREE_OPERAND (x, 1);
860*404b540aSrobert   if (BASELINK_P (x))
861*404b540aSrobert     x = BASELINK_FUNCTIONS (x);
862*404b540aSrobert   if (TREE_CODE (x) == TEMPLATE_ID_EXPR
863*404b540aSrobert       || DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
864*404b540aSrobert       || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
865*404b540aSrobert     return 2;
866*404b540aSrobert   return  (TREE_CODE (x) == FUNCTION_DECL
867*404b540aSrobert 	   || TREE_CODE (x) == OVERLOAD);
868*404b540aSrobert }
869*404b540aSrobert 
870*404b540aSrobert /* Returns true iff X is an expression for an overloaded function
871*404b540aSrobert    whose type cannot be known without performing overload
872*404b540aSrobert    resolution.  */
873*404b540aSrobert 
874*404b540aSrobert bool
really_overloaded_fn(tree x)875*404b540aSrobert really_overloaded_fn (tree x)
876*404b540aSrobert {
877*404b540aSrobert   return is_overloaded_fn (x) == 2;
878*404b540aSrobert }
879*404b540aSrobert 
880*404b540aSrobert tree
get_first_fn(tree from)881*404b540aSrobert get_first_fn (tree from)
882*404b540aSrobert {
883*404b540aSrobert   gcc_assert (is_overloaded_fn (from));
884*404b540aSrobert   /* A baselink is also considered an overloaded function.  */
885*404b540aSrobert   if (TREE_CODE (from) == COMPONENT_REF)
886*404b540aSrobert     from = TREE_OPERAND (from, 1);
887*404b540aSrobert   if (BASELINK_P (from))
888*404b540aSrobert     from = BASELINK_FUNCTIONS (from);
889*404b540aSrobert   return OVL_CURRENT (from);
890*404b540aSrobert }
891*404b540aSrobert 
892*404b540aSrobert /* Return a new OVL node, concatenating it with the old one.  */
893*404b540aSrobert 
894*404b540aSrobert tree
ovl_cons(tree decl,tree chain)895*404b540aSrobert ovl_cons (tree decl, tree chain)
896*404b540aSrobert {
897*404b540aSrobert   tree result = make_node (OVERLOAD);
898*404b540aSrobert   TREE_TYPE (result) = unknown_type_node;
899*404b540aSrobert   OVL_FUNCTION (result) = decl;
900*404b540aSrobert   TREE_CHAIN (result) = chain;
901*404b540aSrobert 
902*404b540aSrobert   return result;
903*404b540aSrobert }
904*404b540aSrobert 
905*404b540aSrobert /* Build a new overloaded function. If this is the first one,
906*404b540aSrobert    just return it; otherwise, ovl_cons the _DECLs */
907*404b540aSrobert 
908*404b540aSrobert tree
build_overload(tree decl,tree chain)909*404b540aSrobert build_overload (tree decl, tree chain)
910*404b540aSrobert {
911*404b540aSrobert   if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
912*404b540aSrobert     return decl;
913*404b540aSrobert   if (chain && TREE_CODE (chain) != OVERLOAD)
914*404b540aSrobert     chain = ovl_cons (chain, NULL_TREE);
915*404b540aSrobert   return ovl_cons (decl, chain);
916*404b540aSrobert }
917*404b540aSrobert 
918*404b540aSrobert 
919*404b540aSrobert #define PRINT_RING_SIZE 4
920*404b540aSrobert 
921*404b540aSrobert const char *
cxx_printable_name(tree decl,int v)922*404b540aSrobert cxx_printable_name (tree decl, int v)
923*404b540aSrobert {
924*404b540aSrobert   static tree decl_ring[PRINT_RING_SIZE];
925*404b540aSrobert   static char *print_ring[PRINT_RING_SIZE];
926*404b540aSrobert   static int ring_counter;
927*404b540aSrobert   int i;
928*404b540aSrobert 
929*404b540aSrobert   /* Only cache functions.  */
930*404b540aSrobert   if (v < 2
931*404b540aSrobert       || TREE_CODE (decl) != FUNCTION_DECL
932*404b540aSrobert       || DECL_LANG_SPECIFIC (decl) == 0)
933*404b540aSrobert     return lang_decl_name (decl, v);
934*404b540aSrobert 
935*404b540aSrobert   /* See if this print name is lying around.  */
936*404b540aSrobert   for (i = 0; i < PRINT_RING_SIZE; i++)
937*404b540aSrobert     if (decl_ring[i] == decl)
938*404b540aSrobert       /* yes, so return it.  */
939*404b540aSrobert       return print_ring[i];
940*404b540aSrobert 
941*404b540aSrobert   if (++ring_counter == PRINT_RING_SIZE)
942*404b540aSrobert     ring_counter = 0;
943*404b540aSrobert 
944*404b540aSrobert   if (current_function_decl != NULL_TREE)
945*404b540aSrobert     {
946*404b540aSrobert       if (decl_ring[ring_counter] == current_function_decl)
947*404b540aSrobert 	ring_counter += 1;
948*404b540aSrobert       if (ring_counter == PRINT_RING_SIZE)
949*404b540aSrobert 	ring_counter = 0;
950*404b540aSrobert       gcc_assert (decl_ring[ring_counter] != current_function_decl);
951*404b540aSrobert     }
952*404b540aSrobert 
953*404b540aSrobert   if (print_ring[ring_counter])
954*404b540aSrobert     free (print_ring[ring_counter]);
955*404b540aSrobert 
956*404b540aSrobert   print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
957*404b540aSrobert   decl_ring[ring_counter] = decl;
958*404b540aSrobert   return print_ring[ring_counter];
959*404b540aSrobert }
960*404b540aSrobert 
961*404b540aSrobert /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
962*404b540aSrobert    listed in RAISES.  */
963*404b540aSrobert 
964*404b540aSrobert tree
build_exception_variant(tree type,tree raises)965*404b540aSrobert build_exception_variant (tree type, tree raises)
966*404b540aSrobert {
967*404b540aSrobert   tree v = TYPE_MAIN_VARIANT (type);
968*404b540aSrobert   int type_quals = TYPE_QUALS (type);
969*404b540aSrobert 
970*404b540aSrobert   for (; v; v = TYPE_NEXT_VARIANT (v))
971*404b540aSrobert     if (check_qualified_type (v, type, type_quals)
972*404b540aSrobert 	&& comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
973*404b540aSrobert       return v;
974*404b540aSrobert 
975*404b540aSrobert   /* Need to build a new variant.  */
976*404b540aSrobert   v = build_variant_type_copy (type);
977*404b540aSrobert   TYPE_RAISES_EXCEPTIONS (v) = raises;
978*404b540aSrobert   return v;
979*404b540aSrobert }
980*404b540aSrobert 
981*404b540aSrobert /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
982*404b540aSrobert    BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
983*404b540aSrobert    arguments.  */
984*404b540aSrobert 
985*404b540aSrobert tree
bind_template_template_parm(tree t,tree newargs)986*404b540aSrobert bind_template_template_parm (tree t, tree newargs)
987*404b540aSrobert {
988*404b540aSrobert   tree decl = TYPE_NAME (t);
989*404b540aSrobert   tree t2;
990*404b540aSrobert 
991*404b540aSrobert   t2 = make_aggr_type (BOUND_TEMPLATE_TEMPLATE_PARM);
992*404b540aSrobert   decl = build_decl (TYPE_DECL, DECL_NAME (decl), NULL_TREE);
993*404b540aSrobert 
994*404b540aSrobert   /* These nodes have to be created to reflect new TYPE_DECL and template
995*404b540aSrobert      arguments.  */
996*404b540aSrobert   TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
997*404b540aSrobert   TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
998*404b540aSrobert   TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
999*404b540aSrobert     = tree_cons (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t),
1000*404b540aSrobert 		 newargs, NULL_TREE);
1001*404b540aSrobert 
1002*404b540aSrobert   TREE_TYPE (decl) = t2;
1003*404b540aSrobert   TYPE_NAME (t2) = decl;
1004*404b540aSrobert   TYPE_STUB_DECL (t2) = decl;
1005*404b540aSrobert   TYPE_SIZE (t2) = 0;
1006*404b540aSrobert 
1007*404b540aSrobert   return t2;
1008*404b540aSrobert }
1009*404b540aSrobert 
1010*404b540aSrobert /* Called from count_trees via walk_tree.  */
1011*404b540aSrobert 
1012*404b540aSrobert static tree
count_trees_r(tree * tp,int * walk_subtrees,void * data)1013*404b540aSrobert count_trees_r (tree *tp, int *walk_subtrees, void *data)
1014*404b540aSrobert {
1015*404b540aSrobert   ++*((int *) data);
1016*404b540aSrobert 
1017*404b540aSrobert   if (TYPE_P (*tp))
1018*404b540aSrobert     *walk_subtrees = 0;
1019*404b540aSrobert 
1020*404b540aSrobert   return NULL_TREE;
1021*404b540aSrobert }
1022*404b540aSrobert 
1023*404b540aSrobert /* Debugging function for measuring the rough complexity of a tree
1024*404b540aSrobert    representation.  */
1025*404b540aSrobert 
1026*404b540aSrobert int
count_trees(tree t)1027*404b540aSrobert count_trees (tree t)
1028*404b540aSrobert {
1029*404b540aSrobert   int n_trees = 0;
1030*404b540aSrobert   walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1031*404b540aSrobert   return n_trees;
1032*404b540aSrobert }
1033*404b540aSrobert 
1034*404b540aSrobert /* Called from verify_stmt_tree via walk_tree.  */
1035*404b540aSrobert 
1036*404b540aSrobert static tree
verify_stmt_tree_r(tree * tp,int * walk_subtrees ATTRIBUTE_UNUSED,void * data)1037*404b540aSrobert verify_stmt_tree_r (tree* tp,
1038*404b540aSrobert 		    int* walk_subtrees ATTRIBUTE_UNUSED ,
1039*404b540aSrobert 		    void* data)
1040*404b540aSrobert {
1041*404b540aSrobert   tree t = *tp;
1042*404b540aSrobert   htab_t *statements = (htab_t *) data;
1043*404b540aSrobert   void **slot;
1044*404b540aSrobert 
1045*404b540aSrobert   if (!STATEMENT_CODE_P (TREE_CODE (t)))
1046*404b540aSrobert     return NULL_TREE;
1047*404b540aSrobert 
1048*404b540aSrobert   /* If this statement is already present in the hash table, then
1049*404b540aSrobert      there is a circularity in the statement tree.  */
1050*404b540aSrobert   gcc_assert (!htab_find (*statements, t));
1051*404b540aSrobert 
1052*404b540aSrobert   slot = htab_find_slot (*statements, t, INSERT);
1053*404b540aSrobert   *slot = t;
1054*404b540aSrobert 
1055*404b540aSrobert   return NULL_TREE;
1056*404b540aSrobert }
1057*404b540aSrobert 
1058*404b540aSrobert /* Debugging function to check that the statement T has not been
1059*404b540aSrobert    corrupted.  For now, this function simply checks that T contains no
1060*404b540aSrobert    circularities.  */
1061*404b540aSrobert 
1062*404b540aSrobert void
verify_stmt_tree(tree t)1063*404b540aSrobert verify_stmt_tree (tree t)
1064*404b540aSrobert {
1065*404b540aSrobert   htab_t statements;
1066*404b540aSrobert   statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1067*404b540aSrobert   walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1068*404b540aSrobert   htab_delete (statements);
1069*404b540aSrobert }
1070*404b540aSrobert 
1071*404b540aSrobert /* Check if the type T depends on a type with no linkage and if so, return
1072*404b540aSrobert    it.  If RELAXED_P then do not consider a class type declared within
1073*404b540aSrobert    a TREE_PUBLIC function to have no linkage.  */
1074*404b540aSrobert 
1075*404b540aSrobert tree
no_linkage_check(tree t,bool relaxed_p)1076*404b540aSrobert no_linkage_check (tree t, bool relaxed_p)
1077*404b540aSrobert {
1078*404b540aSrobert   tree r;
1079*404b540aSrobert 
1080*404b540aSrobert   /* There's no point in checking linkage on template functions; we
1081*404b540aSrobert      can't know their complete types.  */
1082*404b540aSrobert   if (processing_template_decl)
1083*404b540aSrobert     return NULL_TREE;
1084*404b540aSrobert 
1085*404b540aSrobert   switch (TREE_CODE (t))
1086*404b540aSrobert     {
1087*404b540aSrobert       tree fn;
1088*404b540aSrobert 
1089*404b540aSrobert     case RECORD_TYPE:
1090*404b540aSrobert       if (TYPE_PTRMEMFUNC_P (t))
1091*404b540aSrobert 	goto ptrmem;
1092*404b540aSrobert       /* Fall through.  */
1093*404b540aSrobert     case UNION_TYPE:
1094*404b540aSrobert       if (!CLASS_TYPE_P (t))
1095*404b540aSrobert 	return NULL_TREE;
1096*404b540aSrobert       /* Fall through.  */
1097*404b540aSrobert     case ENUMERAL_TYPE:
1098*404b540aSrobert       if (TYPE_ANONYMOUS_P (t))
1099*404b540aSrobert 	return t;
1100*404b540aSrobert       fn = decl_function_context (TYPE_MAIN_DECL (t));
1101*404b540aSrobert       if (fn && (!relaxed_p || !TREE_PUBLIC (fn)))
1102*404b540aSrobert 	return t;
1103*404b540aSrobert       return NULL_TREE;
1104*404b540aSrobert 
1105*404b540aSrobert     case ARRAY_TYPE:
1106*404b540aSrobert     case POINTER_TYPE:
1107*404b540aSrobert     case REFERENCE_TYPE:
1108*404b540aSrobert       return no_linkage_check (TREE_TYPE (t), relaxed_p);
1109*404b540aSrobert 
1110*404b540aSrobert     case OFFSET_TYPE:
1111*404b540aSrobert     ptrmem:
1112*404b540aSrobert       r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
1113*404b540aSrobert 			    relaxed_p);
1114*404b540aSrobert       if (r)
1115*404b540aSrobert 	return r;
1116*404b540aSrobert       return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
1117*404b540aSrobert 
1118*404b540aSrobert     case METHOD_TYPE:
1119*404b540aSrobert       r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
1120*404b540aSrobert       if (r)
1121*404b540aSrobert 	return r;
1122*404b540aSrobert       /* Fall through.  */
1123*404b540aSrobert     case FUNCTION_TYPE:
1124*404b540aSrobert       {
1125*404b540aSrobert 	tree parm;
1126*404b540aSrobert 	for (parm = TYPE_ARG_TYPES (t);
1127*404b540aSrobert 	     parm && parm != void_list_node;
1128*404b540aSrobert 	     parm = TREE_CHAIN (parm))
1129*404b540aSrobert 	  {
1130*404b540aSrobert 	    r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
1131*404b540aSrobert 	    if (r)
1132*404b540aSrobert 	      return r;
1133*404b540aSrobert 	  }
1134*404b540aSrobert 	return no_linkage_check (TREE_TYPE (t), relaxed_p);
1135*404b540aSrobert       }
1136*404b540aSrobert 
1137*404b540aSrobert     default:
1138*404b540aSrobert       return NULL_TREE;
1139*404b540aSrobert     }
1140*404b540aSrobert }
1141*404b540aSrobert 
1142*404b540aSrobert #ifdef GATHER_STATISTICS
1143*404b540aSrobert extern int depth_reached;
1144*404b540aSrobert #endif
1145*404b540aSrobert 
1146*404b540aSrobert void
cxx_print_statistics(void)1147*404b540aSrobert cxx_print_statistics (void)
1148*404b540aSrobert {
1149*404b540aSrobert   print_search_statistics ();
1150*404b540aSrobert   print_class_statistics ();
1151*404b540aSrobert #ifdef GATHER_STATISTICS
1152*404b540aSrobert   fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1153*404b540aSrobert 	   depth_reached);
1154*404b540aSrobert #endif
1155*404b540aSrobert }
1156*404b540aSrobert 
1157*404b540aSrobert /* Return, as an INTEGER_CST node, the number of elements for TYPE
1158*404b540aSrobert    (which is an ARRAY_TYPE).  This counts only elements of the top
1159*404b540aSrobert    array.  */
1160*404b540aSrobert 
1161*404b540aSrobert tree
array_type_nelts_top(tree type)1162*404b540aSrobert array_type_nelts_top (tree type)
1163*404b540aSrobert {
1164*404b540aSrobert   return fold_build2 (PLUS_EXPR, sizetype,
1165*404b540aSrobert 		      array_type_nelts (type),
1166*404b540aSrobert 		      integer_one_node);
1167*404b540aSrobert }
1168*404b540aSrobert 
1169*404b540aSrobert /* Return, as an INTEGER_CST node, the number of elements for TYPE
1170*404b540aSrobert    (which is an ARRAY_TYPE).  This one is a recursive count of all
1171*404b540aSrobert    ARRAY_TYPEs that are clumped together.  */
1172*404b540aSrobert 
1173*404b540aSrobert tree
array_type_nelts_total(tree type)1174*404b540aSrobert array_type_nelts_total (tree type)
1175*404b540aSrobert {
1176*404b540aSrobert   tree sz = array_type_nelts_top (type);
1177*404b540aSrobert   type = TREE_TYPE (type);
1178*404b540aSrobert   while (TREE_CODE (type) == ARRAY_TYPE)
1179*404b540aSrobert     {
1180*404b540aSrobert       tree n = array_type_nelts_top (type);
1181*404b540aSrobert       sz = fold_build2 (MULT_EXPR, sizetype, sz, n);
1182*404b540aSrobert       type = TREE_TYPE (type);
1183*404b540aSrobert     }
1184*404b540aSrobert   return sz;
1185*404b540aSrobert }
1186*404b540aSrobert 
1187*404b540aSrobert /* Called from break_out_target_exprs via mapcar.  */
1188*404b540aSrobert 
1189*404b540aSrobert static tree
bot_manip(tree * tp,int * walk_subtrees,void * data)1190*404b540aSrobert bot_manip (tree* tp, int* walk_subtrees, void* data)
1191*404b540aSrobert {
1192*404b540aSrobert   splay_tree target_remap = ((splay_tree) data);
1193*404b540aSrobert   tree t = *tp;
1194*404b540aSrobert 
1195*404b540aSrobert   if (!TYPE_P (t) && TREE_CONSTANT (t))
1196*404b540aSrobert     {
1197*404b540aSrobert       /* There can't be any TARGET_EXPRs or their slot variables below
1198*404b540aSrobert 	 this point.  We used to check !TREE_SIDE_EFFECTS, but then we
1199*404b540aSrobert 	 failed to copy an ADDR_EXPR of the slot VAR_DECL.  */
1200*404b540aSrobert       *walk_subtrees = 0;
1201*404b540aSrobert       return NULL_TREE;
1202*404b540aSrobert     }
1203*404b540aSrobert   if (TREE_CODE (t) == TARGET_EXPR)
1204*404b540aSrobert     {
1205*404b540aSrobert       tree u;
1206*404b540aSrobert 
1207*404b540aSrobert       if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1208*404b540aSrobert 	u = build_cplus_new
1209*404b540aSrobert 	  (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1210*404b540aSrobert       else
1211*404b540aSrobert 	u = build_target_expr_with_type
1212*404b540aSrobert 	  (break_out_target_exprs (TREE_OPERAND (t, 1)), TREE_TYPE (t));
1213*404b540aSrobert 
1214*404b540aSrobert       /* Map the old variable to the new one.  */
1215*404b540aSrobert       splay_tree_insert (target_remap,
1216*404b540aSrobert 			 (splay_tree_key) TREE_OPERAND (t, 0),
1217*404b540aSrobert 			 (splay_tree_value) TREE_OPERAND (u, 0));
1218*404b540aSrobert 
1219*404b540aSrobert       /* Replace the old expression with the new version.  */
1220*404b540aSrobert       *tp = u;
1221*404b540aSrobert       /* We don't have to go below this point; the recursive call to
1222*404b540aSrobert 	 break_out_target_exprs will have handled anything below this
1223*404b540aSrobert 	 point.  */
1224*404b540aSrobert       *walk_subtrees = 0;
1225*404b540aSrobert       return NULL_TREE;
1226*404b540aSrobert     }
1227*404b540aSrobert 
1228*404b540aSrobert   /* Make a copy of this node.  */
1229*404b540aSrobert   return copy_tree_r (tp, walk_subtrees, NULL);
1230*404b540aSrobert }
1231*404b540aSrobert 
1232*404b540aSrobert /* Replace all remapped VAR_DECLs in T with their new equivalents.
1233*404b540aSrobert    DATA is really a splay-tree mapping old variables to new
1234*404b540aSrobert    variables.  */
1235*404b540aSrobert 
1236*404b540aSrobert static tree
bot_replace(tree * t,int * walk_subtrees ATTRIBUTE_UNUSED,void * data)1237*404b540aSrobert bot_replace (tree* t,
1238*404b540aSrobert 	     int* walk_subtrees ATTRIBUTE_UNUSED ,
1239*404b540aSrobert 	     void* data)
1240*404b540aSrobert {
1241*404b540aSrobert   splay_tree target_remap = ((splay_tree) data);
1242*404b540aSrobert 
1243*404b540aSrobert   if (TREE_CODE (*t) == VAR_DECL)
1244*404b540aSrobert     {
1245*404b540aSrobert       splay_tree_node n = splay_tree_lookup (target_remap,
1246*404b540aSrobert 					     (splay_tree_key) *t);
1247*404b540aSrobert       if (n)
1248*404b540aSrobert 	*t = (tree) n->value;
1249*404b540aSrobert     }
1250*404b540aSrobert 
1251*404b540aSrobert   return NULL_TREE;
1252*404b540aSrobert }
1253*404b540aSrobert 
1254*404b540aSrobert /* When we parse a default argument expression, we may create
1255*404b540aSrobert    temporary variables via TARGET_EXPRs.  When we actually use the
1256*404b540aSrobert    default-argument expression, we make a copy of the expression, but
1257*404b540aSrobert    we must replace the temporaries with appropriate local versions.  */
1258*404b540aSrobert 
1259*404b540aSrobert tree
break_out_target_exprs(tree t)1260*404b540aSrobert break_out_target_exprs (tree t)
1261*404b540aSrobert {
1262*404b540aSrobert   static int target_remap_count;
1263*404b540aSrobert   static splay_tree target_remap;
1264*404b540aSrobert 
1265*404b540aSrobert   if (!target_remap_count++)
1266*404b540aSrobert     target_remap = splay_tree_new (splay_tree_compare_pointers,
1267*404b540aSrobert 				   /*splay_tree_delete_key_fn=*/NULL,
1268*404b540aSrobert 				   /*splay_tree_delete_value_fn=*/NULL);
1269*404b540aSrobert   walk_tree (&t, bot_manip, target_remap, NULL);
1270*404b540aSrobert   walk_tree (&t, bot_replace, target_remap, NULL);
1271*404b540aSrobert 
1272*404b540aSrobert   if (!--target_remap_count)
1273*404b540aSrobert     {
1274*404b540aSrobert       splay_tree_delete (target_remap);
1275*404b540aSrobert       target_remap = NULL;
1276*404b540aSrobert     }
1277*404b540aSrobert 
1278*404b540aSrobert   return t;
1279*404b540aSrobert }
1280*404b540aSrobert 
1281*404b540aSrobert /* Similar to `build_nt', but for template definitions of dependent
1282*404b540aSrobert    expressions  */
1283*404b540aSrobert 
1284*404b540aSrobert tree
build_min_nt(enum tree_code code,...)1285*404b540aSrobert build_min_nt (enum tree_code code, ...)
1286*404b540aSrobert {
1287*404b540aSrobert   tree t;
1288*404b540aSrobert   int length;
1289*404b540aSrobert   int i;
1290*404b540aSrobert   va_list p;
1291*404b540aSrobert 
1292*404b540aSrobert   va_start (p, code);
1293*404b540aSrobert 
1294*404b540aSrobert   t = make_node (code);
1295*404b540aSrobert   length = TREE_CODE_LENGTH (code);
1296*404b540aSrobert 
1297*404b540aSrobert   for (i = 0; i < length; i++)
1298*404b540aSrobert     {
1299*404b540aSrobert       tree x = va_arg (p, tree);
1300*404b540aSrobert       TREE_OPERAND (t, i) = x;
1301*404b540aSrobert     }
1302*404b540aSrobert 
1303*404b540aSrobert   va_end (p);
1304*404b540aSrobert   return t;
1305*404b540aSrobert }
1306*404b540aSrobert 
1307*404b540aSrobert /* Similar to `build', but for template definitions.  */
1308*404b540aSrobert 
1309*404b540aSrobert tree
build_min(enum tree_code code,tree tt,...)1310*404b540aSrobert build_min (enum tree_code code, tree tt, ...)
1311*404b540aSrobert {
1312*404b540aSrobert   tree t;
1313*404b540aSrobert   int length;
1314*404b540aSrobert   int i;
1315*404b540aSrobert   va_list p;
1316*404b540aSrobert 
1317*404b540aSrobert   va_start (p, tt);
1318*404b540aSrobert 
1319*404b540aSrobert   t = make_node (code);
1320*404b540aSrobert   length = TREE_CODE_LENGTH (code);
1321*404b540aSrobert   TREE_TYPE (t) = tt;
1322*404b540aSrobert 
1323*404b540aSrobert   for (i = 0; i < length; i++)
1324*404b540aSrobert     {
1325*404b540aSrobert       tree x = va_arg (p, tree);
1326*404b540aSrobert       TREE_OPERAND (t, i) = x;
1327*404b540aSrobert       if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
1328*404b540aSrobert 	TREE_SIDE_EFFECTS (t) = 1;
1329*404b540aSrobert     }
1330*404b540aSrobert 
1331*404b540aSrobert   va_end (p);
1332*404b540aSrobert   return t;
1333*404b540aSrobert }
1334*404b540aSrobert 
1335*404b540aSrobert /* Similar to `build', but for template definitions of non-dependent
1336*404b540aSrobert    expressions. NON_DEP is the non-dependent expression that has been
1337*404b540aSrobert    built.  */
1338*404b540aSrobert 
1339*404b540aSrobert tree
build_min_non_dep(enum tree_code code,tree non_dep,...)1340*404b540aSrobert build_min_non_dep (enum tree_code code, tree non_dep, ...)
1341*404b540aSrobert {
1342*404b540aSrobert   tree t;
1343*404b540aSrobert   int length;
1344*404b540aSrobert   int i;
1345*404b540aSrobert   va_list p;
1346*404b540aSrobert 
1347*404b540aSrobert   va_start (p, non_dep);
1348*404b540aSrobert 
1349*404b540aSrobert   t = make_node (code);
1350*404b540aSrobert   length = TREE_CODE_LENGTH (code);
1351*404b540aSrobert   TREE_TYPE (t) = TREE_TYPE (non_dep);
1352*404b540aSrobert   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1353*404b540aSrobert 
1354*404b540aSrobert   for (i = 0; i < length; i++)
1355*404b540aSrobert     {
1356*404b540aSrobert       tree x = va_arg (p, tree);
1357*404b540aSrobert       TREE_OPERAND (t, i) = x;
1358*404b540aSrobert     }
1359*404b540aSrobert 
1360*404b540aSrobert   if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
1361*404b540aSrobert     /* This should not be considered a COMPOUND_EXPR, because it
1362*404b540aSrobert        resolves to an overload.  */
1363*404b540aSrobert     COMPOUND_EXPR_OVERLOADED (t) = 1;
1364*404b540aSrobert 
1365*404b540aSrobert   va_end (p);
1366*404b540aSrobert   return t;
1367*404b540aSrobert }
1368*404b540aSrobert 
1369*404b540aSrobert tree
get_type_decl(tree t)1370*404b540aSrobert get_type_decl (tree t)
1371*404b540aSrobert {
1372*404b540aSrobert   if (TREE_CODE (t) == TYPE_DECL)
1373*404b540aSrobert     return t;
1374*404b540aSrobert   if (TYPE_P (t))
1375*404b540aSrobert     return TYPE_STUB_DECL (t);
1376*404b540aSrobert   gcc_assert (t == error_mark_node);
1377*404b540aSrobert   return t;
1378*404b540aSrobert }
1379*404b540aSrobert 
1380*404b540aSrobert /* Returns the namespace that contains DECL, whether directly or
1381*404b540aSrobert    indirectly.  */
1382*404b540aSrobert 
1383*404b540aSrobert tree
decl_namespace_context(tree decl)1384*404b540aSrobert decl_namespace_context (tree decl)
1385*404b540aSrobert {
1386*404b540aSrobert   while (1)
1387*404b540aSrobert     {
1388*404b540aSrobert       if (TREE_CODE (decl) == NAMESPACE_DECL)
1389*404b540aSrobert 	return decl;
1390*404b540aSrobert       else if (TYPE_P (decl))
1391*404b540aSrobert 	decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1392*404b540aSrobert       else
1393*404b540aSrobert 	decl = CP_DECL_CONTEXT (decl);
1394*404b540aSrobert     }
1395*404b540aSrobert }
1396*404b540aSrobert 
1397*404b540aSrobert /* Returns true if decl is within an anonymous namespace, however deeply
1398*404b540aSrobert    nested, or false otherwise.  */
1399*404b540aSrobert 
1400*404b540aSrobert bool
decl_anon_ns_mem_p(tree decl)1401*404b540aSrobert decl_anon_ns_mem_p (tree decl)
1402*404b540aSrobert {
1403*404b540aSrobert   while (1)
1404*404b540aSrobert     {
1405*404b540aSrobert       if (decl == NULL_TREE || decl == error_mark_node)
1406*404b540aSrobert 	return false;
1407*404b540aSrobert       if (TREE_CODE (decl) == NAMESPACE_DECL
1408*404b540aSrobert 	  && DECL_NAME (decl) == NULL_TREE)
1409*404b540aSrobert 	return true;
1410*404b540aSrobert       /* Classes and namespaces inside anonymous namespaces have
1411*404b540aSrobert          TREE_PUBLIC == 0, so we can shortcut the search.  */
1412*404b540aSrobert       else if (TYPE_P (decl))
1413*404b540aSrobert 	return (TREE_PUBLIC (TYPE_NAME (decl)) == 0);
1414*404b540aSrobert       else if (TREE_CODE (decl) == NAMESPACE_DECL)
1415*404b540aSrobert 	return (TREE_PUBLIC (decl) == 0);
1416*404b540aSrobert       else
1417*404b540aSrobert 	decl = DECL_CONTEXT (decl);
1418*404b540aSrobert     }
1419*404b540aSrobert }
1420*404b540aSrobert 
1421*404b540aSrobert /* Return truthvalue of whether T1 is the same tree structure as T2.
1422*404b540aSrobert    Return 1 if they are the same. Return 0 if they are different.  */
1423*404b540aSrobert 
1424*404b540aSrobert bool
cp_tree_equal(tree t1,tree t2)1425*404b540aSrobert cp_tree_equal (tree t1, tree t2)
1426*404b540aSrobert {
1427*404b540aSrobert   enum tree_code code1, code2;
1428*404b540aSrobert 
1429*404b540aSrobert   if (t1 == t2)
1430*404b540aSrobert     return true;
1431*404b540aSrobert   if (!t1 || !t2)
1432*404b540aSrobert     return false;
1433*404b540aSrobert 
1434*404b540aSrobert   for (code1 = TREE_CODE (t1);
1435*404b540aSrobert        code1 == NOP_EXPR || code1 == CONVERT_EXPR
1436*404b540aSrobert 	 || code1 == NON_LVALUE_EXPR;
1437*404b540aSrobert        code1 = TREE_CODE (t1))
1438*404b540aSrobert     t1 = TREE_OPERAND (t1, 0);
1439*404b540aSrobert   for (code2 = TREE_CODE (t2);
1440*404b540aSrobert        code2 == NOP_EXPR || code2 == CONVERT_EXPR
1441*404b540aSrobert 	 || code1 == NON_LVALUE_EXPR;
1442*404b540aSrobert        code2 = TREE_CODE (t2))
1443*404b540aSrobert     t2 = TREE_OPERAND (t2, 0);
1444*404b540aSrobert 
1445*404b540aSrobert   /* They might have become equal now.  */
1446*404b540aSrobert   if (t1 == t2)
1447*404b540aSrobert     return true;
1448*404b540aSrobert 
1449*404b540aSrobert   if (code1 != code2)
1450*404b540aSrobert     return false;
1451*404b540aSrobert 
1452*404b540aSrobert   switch (code1)
1453*404b540aSrobert     {
1454*404b540aSrobert     case INTEGER_CST:
1455*404b540aSrobert       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1456*404b540aSrobert 	&& TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1457*404b540aSrobert 
1458*404b540aSrobert     case REAL_CST:
1459*404b540aSrobert       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1460*404b540aSrobert 
1461*404b540aSrobert     case STRING_CST:
1462*404b540aSrobert       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1463*404b540aSrobert 	&& !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1464*404b540aSrobert 		    TREE_STRING_LENGTH (t1));
1465*404b540aSrobert 
1466*404b540aSrobert     case COMPLEX_CST:
1467*404b540aSrobert       return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
1468*404b540aSrobert 	&& cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1469*404b540aSrobert 
1470*404b540aSrobert     case CONSTRUCTOR:
1471*404b540aSrobert       /* We need to do this when determining whether or not two
1472*404b540aSrobert 	 non-type pointer to member function template arguments
1473*404b540aSrobert 	 are the same.  */
1474*404b540aSrobert       if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1475*404b540aSrobert 	    /* The first operand is RTL.  */
1476*404b540aSrobert 	    && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
1477*404b540aSrobert 	return false;
1478*404b540aSrobert       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1479*404b540aSrobert 
1480*404b540aSrobert     case TREE_LIST:
1481*404b540aSrobert       if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
1482*404b540aSrobert 	return false;
1483*404b540aSrobert       if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
1484*404b540aSrobert 	return false;
1485*404b540aSrobert       return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1486*404b540aSrobert 
1487*404b540aSrobert     case SAVE_EXPR:
1488*404b540aSrobert       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1489*404b540aSrobert 
1490*404b540aSrobert     case CALL_EXPR:
1491*404b540aSrobert       if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
1492*404b540aSrobert 	return false;
1493*404b540aSrobert       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1494*404b540aSrobert 
1495*404b540aSrobert     case TARGET_EXPR:
1496*404b540aSrobert       {
1497*404b540aSrobert 	tree o1 = TREE_OPERAND (t1, 0);
1498*404b540aSrobert 	tree o2 = TREE_OPERAND (t2, 0);
1499*404b540aSrobert 
1500*404b540aSrobert 	/* Special case: if either target is an unallocated VAR_DECL,
1501*404b540aSrobert 	   it means that it's going to be unified with whatever the
1502*404b540aSrobert 	   TARGET_EXPR is really supposed to initialize, so treat it
1503*404b540aSrobert 	   as being equivalent to anything.  */
1504*404b540aSrobert 	if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
1505*404b540aSrobert 	    && !DECL_RTL_SET_P (o1))
1506*404b540aSrobert 	  /*Nop*/;
1507*404b540aSrobert 	else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
1508*404b540aSrobert 		 && !DECL_RTL_SET_P (o2))
1509*404b540aSrobert 	  /*Nop*/;
1510*404b540aSrobert 	else if (!cp_tree_equal (o1, o2))
1511*404b540aSrobert 	  return false;
1512*404b540aSrobert 
1513*404b540aSrobert 	return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1514*404b540aSrobert       }
1515*404b540aSrobert 
1516*404b540aSrobert     case WITH_CLEANUP_EXPR:
1517*404b540aSrobert       if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
1518*404b540aSrobert 	return false;
1519*404b540aSrobert       return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
1520*404b540aSrobert 
1521*404b540aSrobert     case COMPONENT_REF:
1522*404b540aSrobert       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
1523*404b540aSrobert 	return false;
1524*404b540aSrobert       return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1525*404b540aSrobert 
1526*404b540aSrobert     case VAR_DECL:
1527*404b540aSrobert     case PARM_DECL:
1528*404b540aSrobert     case CONST_DECL:
1529*404b540aSrobert     case FUNCTION_DECL:
1530*404b540aSrobert     case TEMPLATE_DECL:
1531*404b540aSrobert     case IDENTIFIER_NODE:
1532*404b540aSrobert     case SSA_NAME:
1533*404b540aSrobert       return false;
1534*404b540aSrobert 
1535*404b540aSrobert     case BASELINK:
1536*404b540aSrobert       return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
1537*404b540aSrobert 	      && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
1538*404b540aSrobert 	      && cp_tree_equal (BASELINK_FUNCTIONS (t1),
1539*404b540aSrobert 				BASELINK_FUNCTIONS (t2)));
1540*404b540aSrobert 
1541*404b540aSrobert     case TEMPLATE_PARM_INDEX:
1542*404b540aSrobert       return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1543*404b540aSrobert 	      && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
1544*404b540aSrobert 	      && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
1545*404b540aSrobert 			      TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
1546*404b540aSrobert 
1547*404b540aSrobert     case TEMPLATE_ID_EXPR:
1548*404b540aSrobert       {
1549*404b540aSrobert 	unsigned ix;
1550*404b540aSrobert 	tree vec1, vec2;
1551*404b540aSrobert 
1552*404b540aSrobert 	if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
1553*404b540aSrobert 	  return false;
1554*404b540aSrobert 	vec1 = TREE_OPERAND (t1, 1);
1555*404b540aSrobert 	vec2 = TREE_OPERAND (t2, 1);
1556*404b540aSrobert 
1557*404b540aSrobert 	if (!vec1 || !vec2)
1558*404b540aSrobert 	  return !vec1 && !vec2;
1559*404b540aSrobert 
1560*404b540aSrobert 	if (TREE_VEC_LENGTH (vec1) != TREE_VEC_LENGTH (vec2))
1561*404b540aSrobert 	  return false;
1562*404b540aSrobert 
1563*404b540aSrobert 	for (ix = TREE_VEC_LENGTH (vec1); ix--;)
1564*404b540aSrobert 	  if (!cp_tree_equal (TREE_VEC_ELT (vec1, ix),
1565*404b540aSrobert 			      TREE_VEC_ELT (vec2, ix)))
1566*404b540aSrobert 	    return false;
1567*404b540aSrobert 
1568*404b540aSrobert 	return true;
1569*404b540aSrobert       }
1570*404b540aSrobert 
1571*404b540aSrobert     case SIZEOF_EXPR:
1572*404b540aSrobert     case ALIGNOF_EXPR:
1573*404b540aSrobert       {
1574*404b540aSrobert 	tree o1 = TREE_OPERAND (t1, 0);
1575*404b540aSrobert 	tree o2 = TREE_OPERAND (t2, 0);
1576*404b540aSrobert 
1577*404b540aSrobert 	if (TREE_CODE (o1) != TREE_CODE (o2))
1578*404b540aSrobert 	  return false;
1579*404b540aSrobert 	if (TYPE_P (o1))
1580*404b540aSrobert 	  return same_type_p (o1, o2);
1581*404b540aSrobert 	else
1582*404b540aSrobert 	  return cp_tree_equal (o1, o2);
1583*404b540aSrobert       }
1584*404b540aSrobert 
1585*404b540aSrobert     case PTRMEM_CST:
1586*404b540aSrobert       /* Two pointer-to-members are the same if they point to the same
1587*404b540aSrobert 	 field or function in the same class.  */
1588*404b540aSrobert       if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
1589*404b540aSrobert 	return false;
1590*404b540aSrobert 
1591*404b540aSrobert       return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
1592*404b540aSrobert 
1593*404b540aSrobert     case OVERLOAD:
1594*404b540aSrobert       if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
1595*404b540aSrobert 	return false;
1596*404b540aSrobert       return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
1597*404b540aSrobert 
1598*404b540aSrobert     default:
1599*404b540aSrobert       break;
1600*404b540aSrobert     }
1601*404b540aSrobert 
1602*404b540aSrobert   switch (TREE_CODE_CLASS (code1))
1603*404b540aSrobert     {
1604*404b540aSrobert     case tcc_unary:
1605*404b540aSrobert     case tcc_binary:
1606*404b540aSrobert     case tcc_comparison:
1607*404b540aSrobert     case tcc_expression:
1608*404b540aSrobert     case tcc_reference:
1609*404b540aSrobert     case tcc_statement:
1610*404b540aSrobert       {
1611*404b540aSrobert 	int i;
1612*404b540aSrobert 
1613*404b540aSrobert 	for (i = 0; i < TREE_CODE_LENGTH (code1); ++i)
1614*404b540aSrobert 	  if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
1615*404b540aSrobert 	    return false;
1616*404b540aSrobert 
1617*404b540aSrobert 	return true;
1618*404b540aSrobert       }
1619*404b540aSrobert 
1620*404b540aSrobert     case tcc_type:
1621*404b540aSrobert       return same_type_p (t1, t2);
1622*404b540aSrobert     default:
1623*404b540aSrobert       gcc_unreachable ();
1624*404b540aSrobert     }
1625*404b540aSrobert   /* We can get here with --disable-checking.  */
1626*404b540aSrobert   return false;
1627*404b540aSrobert }
1628*404b540aSrobert 
1629*404b540aSrobert /* The type of ARG when used as an lvalue.  */
1630*404b540aSrobert 
1631*404b540aSrobert tree
lvalue_type(tree arg)1632*404b540aSrobert lvalue_type (tree arg)
1633*404b540aSrobert {
1634*404b540aSrobert   tree type = TREE_TYPE (arg);
1635*404b540aSrobert   return type;
1636*404b540aSrobert }
1637*404b540aSrobert 
1638*404b540aSrobert /* The type of ARG for printing error messages; denote lvalues with
1639*404b540aSrobert    reference types.  */
1640*404b540aSrobert 
1641*404b540aSrobert tree
error_type(tree arg)1642*404b540aSrobert error_type (tree arg)
1643*404b540aSrobert {
1644*404b540aSrobert   tree type = TREE_TYPE (arg);
1645*404b540aSrobert 
1646*404b540aSrobert   if (TREE_CODE (type) == ARRAY_TYPE)
1647*404b540aSrobert     ;
1648*404b540aSrobert   else if (TREE_CODE (type) == ERROR_MARK)
1649*404b540aSrobert     ;
1650*404b540aSrobert   else if (real_lvalue_p (arg))
1651*404b540aSrobert     type = build_reference_type (lvalue_type (arg));
1652*404b540aSrobert   else if (IS_AGGR_TYPE (type))
1653*404b540aSrobert     type = lvalue_type (arg);
1654*404b540aSrobert 
1655*404b540aSrobert   return type;
1656*404b540aSrobert }
1657*404b540aSrobert 
1658*404b540aSrobert /* Does FUNCTION use a variable-length argument list?  */
1659*404b540aSrobert 
1660*404b540aSrobert int
varargs_function_p(tree function)1661*404b540aSrobert varargs_function_p (tree function)
1662*404b540aSrobert {
1663*404b540aSrobert   tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
1664*404b540aSrobert   for (; parm; parm = TREE_CHAIN (parm))
1665*404b540aSrobert     if (TREE_VALUE (parm) == void_type_node)
1666*404b540aSrobert       return 0;
1667*404b540aSrobert   return 1;
1668*404b540aSrobert }
1669*404b540aSrobert 
1670*404b540aSrobert /* Returns 1 if decl is a member of a class.  */
1671*404b540aSrobert 
1672*404b540aSrobert int
member_p(tree decl)1673*404b540aSrobert member_p (tree decl)
1674*404b540aSrobert {
1675*404b540aSrobert   const tree ctx = DECL_CONTEXT (decl);
1676*404b540aSrobert   return (ctx && TYPE_P (ctx));
1677*404b540aSrobert }
1678*404b540aSrobert 
1679*404b540aSrobert /* Create a placeholder for member access where we don't actually have an
1680*404b540aSrobert    object that the access is against.  */
1681*404b540aSrobert 
1682*404b540aSrobert tree
build_dummy_object(tree type)1683*404b540aSrobert build_dummy_object (tree type)
1684*404b540aSrobert {
1685*404b540aSrobert   tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
1686*404b540aSrobert   return build_indirect_ref (decl, NULL);
1687*404b540aSrobert }
1688*404b540aSrobert 
1689*404b540aSrobert /* We've gotten a reference to a member of TYPE.  Return *this if appropriate,
1690*404b540aSrobert    or a dummy object otherwise.  If BINFOP is non-0, it is filled with the
1691*404b540aSrobert    binfo path from current_class_type to TYPE, or 0.  */
1692*404b540aSrobert 
1693*404b540aSrobert tree
maybe_dummy_object(tree type,tree * binfop)1694*404b540aSrobert maybe_dummy_object (tree type, tree* binfop)
1695*404b540aSrobert {
1696*404b540aSrobert   tree decl, context;
1697*404b540aSrobert   tree binfo;
1698*404b540aSrobert 
1699*404b540aSrobert   if (current_class_type
1700*404b540aSrobert       && (binfo = lookup_base (current_class_type, type,
1701*404b540aSrobert 			       ba_unique | ba_quiet, NULL)))
1702*404b540aSrobert     context = current_class_type;
1703*404b540aSrobert   else
1704*404b540aSrobert     {
1705*404b540aSrobert       /* Reference from a nested class member function.  */
1706*404b540aSrobert       context = type;
1707*404b540aSrobert       binfo = TYPE_BINFO (type);
1708*404b540aSrobert     }
1709*404b540aSrobert 
1710*404b540aSrobert   if (binfop)
1711*404b540aSrobert     *binfop = binfo;
1712*404b540aSrobert 
1713*404b540aSrobert   if (current_class_ref && context == current_class_type
1714*404b540aSrobert       /* Kludge: Make sure that current_class_type is actually
1715*404b540aSrobert 	 correct.  It might not be if we're in the middle of
1716*404b540aSrobert 	 tsubst_default_argument.  */
1717*404b540aSrobert       && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)),
1718*404b540aSrobert 		      current_class_type))
1719*404b540aSrobert     decl = current_class_ref;
1720*404b540aSrobert   else
1721*404b540aSrobert     decl = build_dummy_object (context);
1722*404b540aSrobert 
1723*404b540aSrobert   return decl;
1724*404b540aSrobert }
1725*404b540aSrobert 
1726*404b540aSrobert /* Returns 1 if OB is a placeholder object, or a pointer to one.  */
1727*404b540aSrobert 
1728*404b540aSrobert int
is_dummy_object(tree ob)1729*404b540aSrobert is_dummy_object (tree ob)
1730*404b540aSrobert {
1731*404b540aSrobert   if (TREE_CODE (ob) == INDIRECT_REF)
1732*404b540aSrobert     ob = TREE_OPERAND (ob, 0);
1733*404b540aSrobert   return (TREE_CODE (ob) == NOP_EXPR
1734*404b540aSrobert 	  && TREE_OPERAND (ob, 0) == void_zero_node);
1735*404b540aSrobert }
1736*404b540aSrobert 
1737*404b540aSrobert /* Returns 1 iff type T is a POD type, as defined in [basic.types].  */
1738*404b540aSrobert 
1739*404b540aSrobert int
pod_type_p(tree t)1740*404b540aSrobert pod_type_p (tree t)
1741*404b540aSrobert {
1742*404b540aSrobert   t = strip_array_types (t);
1743*404b540aSrobert 
1744*404b540aSrobert   if (t == error_mark_node)
1745*404b540aSrobert     return 1;
1746*404b540aSrobert   if (INTEGRAL_TYPE_P (t))
1747*404b540aSrobert     return 1;  /* integral, character or enumeral type */
1748*404b540aSrobert   if (FLOAT_TYPE_P (t))
1749*404b540aSrobert     return 1;
1750*404b540aSrobert   if (TYPE_PTR_P (t))
1751*404b540aSrobert     return 1; /* pointer to non-member */
1752*404b540aSrobert   if (TYPE_PTR_TO_MEMBER_P (t))
1753*404b540aSrobert     return 1; /* pointer to member */
1754*404b540aSrobert 
1755*404b540aSrobert   if (TREE_CODE (t) == VECTOR_TYPE)
1756*404b540aSrobert     return 1; /* vectors are (small) arrays of scalars */
1757*404b540aSrobert 
1758*404b540aSrobert   if (! CLASS_TYPE_P (t))
1759*404b540aSrobert     return 0; /* other non-class type (reference or function) */
1760*404b540aSrobert   if (CLASSTYPE_NON_POD_P (t))
1761*404b540aSrobert     return 0;
1762*404b540aSrobert   return 1;
1763*404b540aSrobert }
1764*404b540aSrobert 
1765*404b540aSrobert /* Returns 1 iff zero initialization of type T means actually storing
1766*404b540aSrobert    zeros in it.  */
1767*404b540aSrobert 
1768*404b540aSrobert int
zero_init_p(tree t)1769*404b540aSrobert zero_init_p (tree t)
1770*404b540aSrobert {
1771*404b540aSrobert   t = strip_array_types (t);
1772*404b540aSrobert 
1773*404b540aSrobert   if (t == error_mark_node)
1774*404b540aSrobert     return 1;
1775*404b540aSrobert 
1776*404b540aSrobert   /* NULL pointers to data members are initialized with -1.  */
1777*404b540aSrobert   if (TYPE_PTRMEM_P (t))
1778*404b540aSrobert     return 0;
1779*404b540aSrobert 
1780*404b540aSrobert   /* Classes that contain types that can't be zero-initialized, cannot
1781*404b540aSrobert      be zero-initialized themselves.  */
1782*404b540aSrobert   if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
1783*404b540aSrobert     return 0;
1784*404b540aSrobert 
1785*404b540aSrobert   return 1;
1786*404b540aSrobert }
1787*404b540aSrobert 
1788*404b540aSrobert /* Table of valid C++ attributes.  */
1789*404b540aSrobert const struct attribute_spec cxx_attribute_table[] =
1790*404b540aSrobert {
1791*404b540aSrobert   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1792*404b540aSrobert   { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute },
1793*404b540aSrobert   { "com_interface",  0, 0, false, false, false, handle_com_interface_attribute },
1794*404b540aSrobert   { "init_priority",  1, 1, true,  false, false, handle_init_priority_attribute },
1795*404b540aSrobert   { NULL,	      0, 0, false, false, false, NULL }
1796*404b540aSrobert };
1797*404b540aSrobert 
1798*404b540aSrobert /* Handle a "java_interface" attribute; arguments as in
1799*404b540aSrobert    struct attribute_spec.handler.  */
1800*404b540aSrobert static tree
handle_java_interface_attribute(tree * node,tree name,tree args ATTRIBUTE_UNUSED,int flags,bool * no_add_attrs)1801*404b540aSrobert handle_java_interface_attribute (tree* node,
1802*404b540aSrobert 				 tree name,
1803*404b540aSrobert 				 tree args ATTRIBUTE_UNUSED ,
1804*404b540aSrobert 				 int flags,
1805*404b540aSrobert 				 bool* no_add_attrs)
1806*404b540aSrobert {
1807*404b540aSrobert   if (DECL_P (*node)
1808*404b540aSrobert       || !CLASS_TYPE_P (*node)
1809*404b540aSrobert       || !TYPE_FOR_JAVA (*node))
1810*404b540aSrobert     {
1811*404b540aSrobert       error ("%qE attribute can only be applied to Java class definitions",
1812*404b540aSrobert 	     name);
1813*404b540aSrobert       *no_add_attrs = true;
1814*404b540aSrobert       return NULL_TREE;
1815*404b540aSrobert     }
1816*404b540aSrobert   if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
1817*404b540aSrobert     *node = build_variant_type_copy (*node);
1818*404b540aSrobert   TYPE_JAVA_INTERFACE (*node) = 1;
1819*404b540aSrobert 
1820*404b540aSrobert   return NULL_TREE;
1821*404b540aSrobert }
1822*404b540aSrobert 
1823*404b540aSrobert /* Handle a "com_interface" attribute; arguments as in
1824*404b540aSrobert    struct attribute_spec.handler.  */
1825*404b540aSrobert static tree
handle_com_interface_attribute(tree * node,tree name,tree args ATTRIBUTE_UNUSED,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)1826*404b540aSrobert handle_com_interface_attribute (tree* node,
1827*404b540aSrobert 				tree name,
1828*404b540aSrobert 				tree args ATTRIBUTE_UNUSED ,
1829*404b540aSrobert 				int flags ATTRIBUTE_UNUSED ,
1830*404b540aSrobert 				bool* no_add_attrs)
1831*404b540aSrobert {
1832*404b540aSrobert   static int warned;
1833*404b540aSrobert 
1834*404b540aSrobert   *no_add_attrs = true;
1835*404b540aSrobert 
1836*404b540aSrobert   if (DECL_P (*node)
1837*404b540aSrobert       || !CLASS_TYPE_P (*node)
1838*404b540aSrobert       || *node != TYPE_MAIN_VARIANT (*node))
1839*404b540aSrobert     {
1840*404b540aSrobert       warning (OPT_Wattributes, "%qE attribute can only be applied "
1841*404b540aSrobert 	       "to class definitions", name);
1842*404b540aSrobert       return NULL_TREE;
1843*404b540aSrobert     }
1844*404b540aSrobert 
1845*404b540aSrobert   if (!warned++)
1846*404b540aSrobert     warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
1847*404b540aSrobert 	     name);
1848*404b540aSrobert 
1849*404b540aSrobert   return NULL_TREE;
1850*404b540aSrobert }
1851*404b540aSrobert 
1852*404b540aSrobert /* Handle an "init_priority" attribute; arguments as in
1853*404b540aSrobert    struct attribute_spec.handler.  */
1854*404b540aSrobert static tree
handle_init_priority_attribute(tree * node,tree name,tree args,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)1855*404b540aSrobert handle_init_priority_attribute (tree* node,
1856*404b540aSrobert 				tree name,
1857*404b540aSrobert 				tree args,
1858*404b540aSrobert 				int flags ATTRIBUTE_UNUSED ,
1859*404b540aSrobert 				bool* no_add_attrs)
1860*404b540aSrobert {
1861*404b540aSrobert   tree initp_expr = TREE_VALUE (args);
1862*404b540aSrobert   tree decl = *node;
1863*404b540aSrobert   tree type = TREE_TYPE (decl);
1864*404b540aSrobert   int pri;
1865*404b540aSrobert 
1866*404b540aSrobert   STRIP_NOPS (initp_expr);
1867*404b540aSrobert 
1868*404b540aSrobert   if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
1869*404b540aSrobert     {
1870*404b540aSrobert       error ("requested init_priority is not an integer constant");
1871*404b540aSrobert       *no_add_attrs = true;
1872*404b540aSrobert       return NULL_TREE;
1873*404b540aSrobert     }
1874*404b540aSrobert 
1875*404b540aSrobert   pri = TREE_INT_CST_LOW (initp_expr);
1876*404b540aSrobert 
1877*404b540aSrobert   type = strip_array_types (type);
1878*404b540aSrobert 
1879*404b540aSrobert   if (decl == NULL_TREE
1880*404b540aSrobert       || TREE_CODE (decl) != VAR_DECL
1881*404b540aSrobert       || !TREE_STATIC (decl)
1882*404b540aSrobert       || DECL_EXTERNAL (decl)
1883*404b540aSrobert       || (TREE_CODE (type) != RECORD_TYPE
1884*404b540aSrobert 	  && TREE_CODE (type) != UNION_TYPE)
1885*404b540aSrobert       /* Static objects in functions are initialized the
1886*404b540aSrobert 	 first time control passes through that
1887*404b540aSrobert 	 function. This is not precise enough to pin down an
1888*404b540aSrobert 	 init_priority value, so don't allow it.  */
1889*404b540aSrobert       || current_function_decl)
1890*404b540aSrobert     {
1891*404b540aSrobert       error ("can only use %qE attribute on file-scope definitions "
1892*404b540aSrobert 	     "of objects of class type", name);
1893*404b540aSrobert       *no_add_attrs = true;
1894*404b540aSrobert       return NULL_TREE;
1895*404b540aSrobert     }
1896*404b540aSrobert 
1897*404b540aSrobert   if (pri > MAX_INIT_PRIORITY || pri <= 0)
1898*404b540aSrobert     {
1899*404b540aSrobert       error ("requested init_priority is out of range");
1900*404b540aSrobert       *no_add_attrs = true;
1901*404b540aSrobert       return NULL_TREE;
1902*404b540aSrobert     }
1903*404b540aSrobert 
1904*404b540aSrobert   /* Check for init_priorities that are reserved for
1905*404b540aSrobert      language and runtime support implementations.*/
1906*404b540aSrobert   if (pri <= MAX_RESERVED_INIT_PRIORITY)
1907*404b540aSrobert     {
1908*404b540aSrobert       warning
1909*404b540aSrobert 	(0, "requested init_priority is reserved for internal use");
1910*404b540aSrobert     }
1911*404b540aSrobert 
1912*404b540aSrobert   if (SUPPORTS_INIT_PRIORITY)
1913*404b540aSrobert     {
1914*404b540aSrobert       SET_DECL_INIT_PRIORITY (decl, pri);
1915*404b540aSrobert       DECL_HAS_INIT_PRIORITY_P (decl) = 1;
1916*404b540aSrobert       return NULL_TREE;
1917*404b540aSrobert     }
1918*404b540aSrobert   else
1919*404b540aSrobert     {
1920*404b540aSrobert       error ("%qE attribute is not supported on this platform", name);
1921*404b540aSrobert       *no_add_attrs = true;
1922*404b540aSrobert       return NULL_TREE;
1923*404b540aSrobert     }
1924*404b540aSrobert }
1925*404b540aSrobert 
1926*404b540aSrobert /* Return a new PTRMEM_CST of the indicated TYPE.  The MEMBER is the
1927*404b540aSrobert    thing pointed to by the constant.  */
1928*404b540aSrobert 
1929*404b540aSrobert tree
make_ptrmem_cst(tree type,tree member)1930*404b540aSrobert make_ptrmem_cst (tree type, tree member)
1931*404b540aSrobert {
1932*404b540aSrobert   tree ptrmem_cst = make_node (PTRMEM_CST);
1933*404b540aSrobert   TREE_TYPE (ptrmem_cst) = type;
1934*404b540aSrobert   PTRMEM_CST_MEMBER (ptrmem_cst) = member;
1935*404b540aSrobert   return ptrmem_cst;
1936*404b540aSrobert }
1937*404b540aSrobert 
1938*404b540aSrobert /* Build a variant of TYPE that has the indicated ATTRIBUTES.  May
1939*404b540aSrobert    return an existing type of an appropriate type already exists.  */
1940*404b540aSrobert 
1941*404b540aSrobert tree
cp_build_type_attribute_variant(tree type,tree attributes)1942*404b540aSrobert cp_build_type_attribute_variant (tree type, tree attributes)
1943*404b540aSrobert {
1944*404b540aSrobert   tree new_type;
1945*404b540aSrobert 
1946*404b540aSrobert   new_type = build_type_attribute_variant (type, attributes);
1947*404b540aSrobert   if (TREE_CODE (new_type) == FUNCTION_TYPE
1948*404b540aSrobert       && (TYPE_RAISES_EXCEPTIONS (new_type)
1949*404b540aSrobert 	  != TYPE_RAISES_EXCEPTIONS (type)))
1950*404b540aSrobert     new_type = build_exception_variant (new_type,
1951*404b540aSrobert 					TYPE_RAISES_EXCEPTIONS (type));
1952*404b540aSrobert 
1953*404b540aSrobert   /* Making a new main variant of a class type is broken.  */
1954*404b540aSrobert   gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
1955*404b540aSrobert 
1956*404b540aSrobert   return new_type;
1957*404b540aSrobert }
1958*404b540aSrobert 
1959*404b540aSrobert /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
1960*404b540aSrobert    traversal.  Called from walk_tree.  */
1961*404b540aSrobert 
1962*404b540aSrobert tree
cp_walk_subtrees(tree * tp,int * walk_subtrees_p,walk_tree_fn func,void * data,struct pointer_set_t * pset)1963*404b540aSrobert cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
1964*404b540aSrobert 		  void *data, struct pointer_set_t *pset)
1965*404b540aSrobert {
1966*404b540aSrobert   enum tree_code code = TREE_CODE (*tp);
1967*404b540aSrobert   location_t save_locus;
1968*404b540aSrobert   tree result;
1969*404b540aSrobert 
1970*404b540aSrobert #define WALK_SUBTREE(NODE)				\
1971*404b540aSrobert   do							\
1972*404b540aSrobert     {							\
1973*404b540aSrobert       result = walk_tree (&(NODE), func, data, pset);	\
1974*404b540aSrobert       if (result) goto out;				\
1975*404b540aSrobert     }							\
1976*404b540aSrobert   while (0)
1977*404b540aSrobert 
1978*404b540aSrobert   /* Set input_location here so we get the right instantiation context
1979*404b540aSrobert      if we call instantiate_decl from inlinable_function_p.  */
1980*404b540aSrobert   save_locus = input_location;
1981*404b540aSrobert   if (EXPR_HAS_LOCATION (*tp))
1982*404b540aSrobert     input_location = EXPR_LOCATION (*tp);
1983*404b540aSrobert 
1984*404b540aSrobert   /* Not one of the easy cases.  We must explicitly go through the
1985*404b540aSrobert      children.  */
1986*404b540aSrobert   result = NULL_TREE;
1987*404b540aSrobert   switch (code)
1988*404b540aSrobert     {
1989*404b540aSrobert     case DEFAULT_ARG:
1990*404b540aSrobert     case TEMPLATE_TEMPLATE_PARM:
1991*404b540aSrobert     case BOUND_TEMPLATE_TEMPLATE_PARM:
1992*404b540aSrobert     case UNBOUND_CLASS_TEMPLATE:
1993*404b540aSrobert     case TEMPLATE_PARM_INDEX:
1994*404b540aSrobert     case TEMPLATE_TYPE_PARM:
1995*404b540aSrobert     case TYPENAME_TYPE:
1996*404b540aSrobert     case TYPEOF_TYPE:
1997*404b540aSrobert     case BASELINK:
1998*404b540aSrobert       /* None of these have subtrees other than those already walked
1999*404b540aSrobert 	 above.  */
2000*404b540aSrobert       *walk_subtrees_p = 0;
2001*404b540aSrobert       break;
2002*404b540aSrobert 
2003*404b540aSrobert     case TINST_LEVEL:
2004*404b540aSrobert       WALK_SUBTREE (TINST_DECL (*tp));
2005*404b540aSrobert       *walk_subtrees_p = 0;
2006*404b540aSrobert       break;
2007*404b540aSrobert 
2008*404b540aSrobert     case PTRMEM_CST:
2009*404b540aSrobert       WALK_SUBTREE (TREE_TYPE (*tp));
2010*404b540aSrobert       *walk_subtrees_p = 0;
2011*404b540aSrobert       break;
2012*404b540aSrobert 
2013*404b540aSrobert     case TREE_LIST:
2014*404b540aSrobert       WALK_SUBTREE (TREE_PURPOSE (*tp));
2015*404b540aSrobert       break;
2016*404b540aSrobert 
2017*404b540aSrobert     case OVERLOAD:
2018*404b540aSrobert       WALK_SUBTREE (OVL_FUNCTION (*tp));
2019*404b540aSrobert       WALK_SUBTREE (OVL_CHAIN (*tp));
2020*404b540aSrobert       *walk_subtrees_p = 0;
2021*404b540aSrobert       break;
2022*404b540aSrobert 
2023*404b540aSrobert     case RECORD_TYPE:
2024*404b540aSrobert       if (TYPE_PTRMEMFUNC_P (*tp))
2025*404b540aSrobert 	WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
2026*404b540aSrobert       break;
2027*404b540aSrobert 
2028*404b540aSrobert     default:
2029*404b540aSrobert       input_location = save_locus;
2030*404b540aSrobert       return NULL_TREE;
2031*404b540aSrobert     }
2032*404b540aSrobert 
2033*404b540aSrobert   /* We didn't find what we were looking for.  */
2034*404b540aSrobert  out:
2035*404b540aSrobert   input_location = save_locus;
2036*404b540aSrobert   return result;
2037*404b540aSrobert 
2038*404b540aSrobert #undef WALK_SUBTREE
2039*404b540aSrobert }
2040*404b540aSrobert 
2041*404b540aSrobert /* Decide whether there are language-specific reasons to not inline a
2042*404b540aSrobert    function as a tree.  */
2043*404b540aSrobert 
2044*404b540aSrobert int
cp_cannot_inline_tree_fn(tree * fnp)2045*404b540aSrobert cp_cannot_inline_tree_fn (tree* fnp)
2046*404b540aSrobert {
2047*404b540aSrobert   tree fn = *fnp;
2048*404b540aSrobert 
2049*404b540aSrobert   /* We can inline a template instantiation only if it's fully
2050*404b540aSrobert      instantiated.  */
2051*404b540aSrobert   if (DECL_TEMPLATE_INFO (fn)
2052*404b540aSrobert       && TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
2053*404b540aSrobert     {
2054*404b540aSrobert       /* Don't instantiate functions that are not going to be
2055*404b540aSrobert 	 inlined.  */
2056*404b540aSrobert       if (!DECL_INLINE (DECL_TEMPLATE_RESULT
2057*404b540aSrobert 			(template_for_substitution (fn))))
2058*404b540aSrobert 	return 1;
2059*404b540aSrobert 
2060*404b540aSrobert       fn = *fnp = instantiate_decl (fn, /*defer_ok=*/0, /*undefined_ok=*/0);
2061*404b540aSrobert 
2062*404b540aSrobert       if (TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
2063*404b540aSrobert 	return 1;
2064*404b540aSrobert     }
2065*404b540aSrobert 
2066*404b540aSrobert   if (flag_really_no_inline
2067*404b540aSrobert       && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) == NULL)
2068*404b540aSrobert     return 1;
2069*404b540aSrobert 
2070*404b540aSrobert   /* Don't auto-inline functions that might be replaced at link-time
2071*404b540aSrobert      with an alternative definition.  */
2072*404b540aSrobert   if (!DECL_DECLARED_INLINE_P (fn) && DECL_REPLACEABLE_P (fn))
2073*404b540aSrobert     {
2074*404b540aSrobert       DECL_UNINLINABLE (fn) = 1;
2075*404b540aSrobert       return 1;
2076*404b540aSrobert     }
2077*404b540aSrobert 
2078*404b540aSrobert   if (varargs_function_p (fn))
2079*404b540aSrobert     {
2080*404b540aSrobert       DECL_UNINLINABLE (fn) = 1;
2081*404b540aSrobert       return 1;
2082*404b540aSrobert     }
2083*404b540aSrobert 
2084*404b540aSrobert   if (! function_attribute_inlinable_p (fn))
2085*404b540aSrobert     {
2086*404b540aSrobert       DECL_UNINLINABLE (fn) = 1;
2087*404b540aSrobert       return 1;
2088*404b540aSrobert     }
2089*404b540aSrobert 
2090*404b540aSrobert   return 0;
2091*404b540aSrobert }
2092*404b540aSrobert 
2093*404b540aSrobert /* Add any pending functions other than the current function (already
2094*404b540aSrobert    handled by the caller), that thus cannot be inlined, to FNS_P, then
2095*404b540aSrobert    return the latest function added to the array, PREV_FN.  */
2096*404b540aSrobert 
2097*404b540aSrobert tree
cp_add_pending_fn_decls(void * fns_p,tree prev_fn)2098*404b540aSrobert cp_add_pending_fn_decls (void* fns_p, tree prev_fn)
2099*404b540aSrobert {
2100*404b540aSrobert   varray_type *fnsp = (varray_type *)fns_p;
2101*404b540aSrobert   struct saved_scope *s;
2102*404b540aSrobert 
2103*404b540aSrobert   for (s = scope_chain; s; s = s->prev)
2104*404b540aSrobert     if (s->function_decl && s->function_decl != prev_fn)
2105*404b540aSrobert       {
2106*404b540aSrobert 	VARRAY_PUSH_TREE (*fnsp, s->function_decl);
2107*404b540aSrobert 	prev_fn = s->function_decl;
2108*404b540aSrobert       }
2109*404b540aSrobert 
2110*404b540aSrobert   return prev_fn;
2111*404b540aSrobert }
2112*404b540aSrobert 
2113*404b540aSrobert /* Determine whether VAR is a declaration of an automatic variable in
2114*404b540aSrobert    function FN.  */
2115*404b540aSrobert 
2116*404b540aSrobert int
cp_auto_var_in_fn_p(tree var,tree fn)2117*404b540aSrobert cp_auto_var_in_fn_p (tree var, tree fn)
2118*404b540aSrobert {
2119*404b540aSrobert   return (DECL_P (var) && DECL_CONTEXT (var) == fn
2120*404b540aSrobert 	  && nonstatic_local_decl_p (var));
2121*404b540aSrobert }
2122*404b540aSrobert 
2123*404b540aSrobert /* Like save_expr, but for C++.  */
2124*404b540aSrobert 
2125*404b540aSrobert tree
cp_save_expr(tree expr)2126*404b540aSrobert cp_save_expr (tree expr)
2127*404b540aSrobert {
2128*404b540aSrobert   /* There is no reason to create a SAVE_EXPR within a template; if
2129*404b540aSrobert      needed, we can create the SAVE_EXPR when instantiating the
2130*404b540aSrobert      template.  Furthermore, the middle-end cannot handle C++-specific
2131*404b540aSrobert      tree codes.  */
2132*404b540aSrobert   if (processing_template_decl)
2133*404b540aSrobert     return expr;
2134*404b540aSrobert   return save_expr (expr);
2135*404b540aSrobert }
2136*404b540aSrobert 
2137*404b540aSrobert /* Initialize tree.c.  */
2138*404b540aSrobert 
2139*404b540aSrobert void
init_tree(void)2140*404b540aSrobert init_tree (void)
2141*404b540aSrobert {
2142*404b540aSrobert   list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
2143*404b540aSrobert }
2144*404b540aSrobert 
2145*404b540aSrobert /* Returns the kind of special function that DECL (a FUNCTION_DECL)
2146*404b540aSrobert    is.  Note that sfk_none is zero, so this function can be used as a
2147*404b540aSrobert    predicate to test whether or not DECL is a special function.  */
2148*404b540aSrobert 
2149*404b540aSrobert special_function_kind
special_function_p(tree decl)2150*404b540aSrobert special_function_p (tree decl)
2151*404b540aSrobert {
2152*404b540aSrobert   /* Rather than doing all this stuff with magic names, we should
2153*404b540aSrobert      probably have a field of type `special_function_kind' in
2154*404b540aSrobert      DECL_LANG_SPECIFIC.  */
2155*404b540aSrobert   if (DECL_COPY_CONSTRUCTOR_P (decl))
2156*404b540aSrobert     return sfk_copy_constructor;
2157*404b540aSrobert   if (DECL_CONSTRUCTOR_P (decl))
2158*404b540aSrobert     return sfk_constructor;
2159*404b540aSrobert   if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2160*404b540aSrobert     return sfk_assignment_operator;
2161*404b540aSrobert   if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2162*404b540aSrobert     return sfk_destructor;
2163*404b540aSrobert   if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2164*404b540aSrobert     return sfk_complete_destructor;
2165*404b540aSrobert   if (DECL_BASE_DESTRUCTOR_P (decl))
2166*404b540aSrobert     return sfk_base_destructor;
2167*404b540aSrobert   if (DECL_DELETING_DESTRUCTOR_P (decl))
2168*404b540aSrobert     return sfk_deleting_destructor;
2169*404b540aSrobert   if (DECL_CONV_FN_P (decl))
2170*404b540aSrobert     return sfk_conversion;
2171*404b540aSrobert 
2172*404b540aSrobert   return sfk_none;
2173*404b540aSrobert }
2174*404b540aSrobert 
2175*404b540aSrobert /* Returns nonzero if TYPE is a character type, including wchar_t.  */
2176*404b540aSrobert 
2177*404b540aSrobert int
char_type_p(tree type)2178*404b540aSrobert char_type_p (tree type)
2179*404b540aSrobert {
2180*404b540aSrobert   return (same_type_p (type, char_type_node)
2181*404b540aSrobert 	  || same_type_p (type, unsigned_char_type_node)
2182*404b540aSrobert 	  || same_type_p (type, signed_char_type_node)
2183*404b540aSrobert 	  || same_type_p (type, wchar_type_node));
2184*404b540aSrobert }
2185*404b540aSrobert 
2186*404b540aSrobert /* Returns the kind of linkage associated with the indicated DECL.  Th
2187*404b540aSrobert    value returned is as specified by the language standard; it is
2188*404b540aSrobert    independent of implementation details regarding template
2189*404b540aSrobert    instantiation, etc.  For example, it is possible that a declaration
2190*404b540aSrobert    to which this function assigns external linkage would not show up
2191*404b540aSrobert    as a global symbol when you run `nm' on the resulting object file.  */
2192*404b540aSrobert 
2193*404b540aSrobert linkage_kind
decl_linkage(tree decl)2194*404b540aSrobert decl_linkage (tree decl)
2195*404b540aSrobert {
2196*404b540aSrobert   /* This function doesn't attempt to calculate the linkage from first
2197*404b540aSrobert      principles as given in [basic.link].  Instead, it makes use of
2198*404b540aSrobert      the fact that we have already set TREE_PUBLIC appropriately, and
2199*404b540aSrobert      then handles a few special cases.  Ideally, we would calculate
2200*404b540aSrobert      linkage first, and then transform that into a concrete
2201*404b540aSrobert      implementation.  */
2202*404b540aSrobert 
2203*404b540aSrobert   /* Things that don't have names have no linkage.  */
2204*404b540aSrobert   if (!DECL_NAME (decl))
2205*404b540aSrobert     return lk_none;
2206*404b540aSrobert 
2207*404b540aSrobert   /* Things that are TREE_PUBLIC have external linkage.  */
2208*404b540aSrobert   if (TREE_PUBLIC (decl))
2209*404b540aSrobert     return lk_external;
2210*404b540aSrobert 
2211*404b540aSrobert   if (TREE_CODE (decl) == NAMESPACE_DECL)
2212*404b540aSrobert     return lk_external;
2213*404b540aSrobert 
2214*404b540aSrobert   /* Linkage of a CONST_DECL depends on the linkage of the enumeration
2215*404b540aSrobert      type.  */
2216*404b540aSrobert   if (TREE_CODE (decl) == CONST_DECL)
2217*404b540aSrobert     return decl_linkage (TYPE_NAME (TREE_TYPE (decl)));
2218*404b540aSrobert 
2219*404b540aSrobert   /* Some things that are not TREE_PUBLIC have external linkage, too.
2220*404b540aSrobert      For example, on targets that don't have weak symbols, we make all
2221*404b540aSrobert      template instantiations have internal linkage (in the object
2222*404b540aSrobert      file), but the symbols should still be treated as having external
2223*404b540aSrobert      linkage from the point of view of the language.  */
2224*404b540aSrobert   if (TREE_CODE (decl) != TYPE_DECL && DECL_LANG_SPECIFIC (decl)
2225*404b540aSrobert       && DECL_COMDAT (decl))
2226*404b540aSrobert     return lk_external;
2227*404b540aSrobert 
2228*404b540aSrobert   /* Things in local scope do not have linkage, if they don't have
2229*404b540aSrobert      TREE_PUBLIC set.  */
2230*404b540aSrobert   if (decl_function_context (decl))
2231*404b540aSrobert     return lk_none;
2232*404b540aSrobert 
2233*404b540aSrobert   /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
2234*404b540aSrobert      are considered to have external linkage for language purposes.  DECLs
2235*404b540aSrobert      really meant to have internal linkage have DECL_THIS_STATIC set.  */
2236*404b540aSrobert   if (TREE_CODE (decl) == TYPE_DECL
2237*404b540aSrobert       || ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2238*404b540aSrobert 	  && !DECL_THIS_STATIC (decl)))
2239*404b540aSrobert     return lk_external;
2240*404b540aSrobert 
2241*404b540aSrobert   /* Everything else has internal linkage.  */
2242*404b540aSrobert   return lk_internal;
2243*404b540aSrobert }
2244*404b540aSrobert 
2245*404b540aSrobert /* EXP is an expression that we want to pre-evaluate.  Returns (in
2246*404b540aSrobert    *INITP) an expression that will perform the pre-evaluation.  The
2247*404b540aSrobert    value returned by this function is a side-effect free expression
2248*404b540aSrobert    equivalent to the pre-evaluated expression.  Callers must ensure
2249*404b540aSrobert    that *INITP is evaluated before EXP.  */
2250*404b540aSrobert 
2251*404b540aSrobert tree
stabilize_expr(tree exp,tree * initp)2252*404b540aSrobert stabilize_expr (tree exp, tree* initp)
2253*404b540aSrobert {
2254*404b540aSrobert   tree init_expr;
2255*404b540aSrobert 
2256*404b540aSrobert   if (!TREE_SIDE_EFFECTS (exp))
2257*404b540aSrobert     init_expr = NULL_TREE;
2258*404b540aSrobert   else if (!real_lvalue_p (exp)
2259*404b540aSrobert 	   || !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)))
2260*404b540aSrobert     {
2261*404b540aSrobert       init_expr = get_target_expr (exp);
2262*404b540aSrobert       exp = TARGET_EXPR_SLOT (init_expr);
2263*404b540aSrobert     }
2264*404b540aSrobert   else
2265*404b540aSrobert     {
2266*404b540aSrobert       exp = build_unary_op (ADDR_EXPR, exp, 1);
2267*404b540aSrobert       init_expr = get_target_expr (exp);
2268*404b540aSrobert       exp = TARGET_EXPR_SLOT (init_expr);
2269*404b540aSrobert       exp = build_indirect_ref (exp, 0);
2270*404b540aSrobert     }
2271*404b540aSrobert   *initp = init_expr;
2272*404b540aSrobert 
2273*404b540aSrobert   gcc_assert (!TREE_SIDE_EFFECTS (exp));
2274*404b540aSrobert   return exp;
2275*404b540aSrobert }
2276*404b540aSrobert 
2277*404b540aSrobert /* Add NEW, an expression whose value we don't care about, after the
2278*404b540aSrobert    similar expression ORIG.  */
2279*404b540aSrobert 
2280*404b540aSrobert tree
add_stmt_to_compound(tree orig,tree new)2281*404b540aSrobert add_stmt_to_compound (tree orig, tree new)
2282*404b540aSrobert {
2283*404b540aSrobert   if (!new || !TREE_SIDE_EFFECTS (new))
2284*404b540aSrobert     return orig;
2285*404b540aSrobert   if (!orig || !TREE_SIDE_EFFECTS (orig))
2286*404b540aSrobert     return new;
2287*404b540aSrobert   return build2 (COMPOUND_EXPR, void_type_node, orig, new);
2288*404b540aSrobert }
2289*404b540aSrobert 
2290*404b540aSrobert /* Like stabilize_expr, but for a call whose arguments we want to
2291*404b540aSrobert    pre-evaluate.  CALL is modified in place to use the pre-evaluated
2292*404b540aSrobert    arguments, while, upon return, *INITP contains an expression to
2293*404b540aSrobert    compute the arguments.  */
2294*404b540aSrobert 
2295*404b540aSrobert void
stabilize_call(tree call,tree * initp)2296*404b540aSrobert stabilize_call (tree call, tree *initp)
2297*404b540aSrobert {
2298*404b540aSrobert   tree inits = NULL_TREE;
2299*404b540aSrobert   tree t;
2300*404b540aSrobert 
2301*404b540aSrobert   if (call == error_mark_node)
2302*404b540aSrobert     return;
2303*404b540aSrobert 
2304*404b540aSrobert   gcc_assert (TREE_CODE (call) == CALL_EXPR
2305*404b540aSrobert 	      || TREE_CODE (call) == AGGR_INIT_EXPR);
2306*404b540aSrobert 
2307*404b540aSrobert   for (t = TREE_OPERAND (call, 1); t; t = TREE_CHAIN (t))
2308*404b540aSrobert     if (TREE_SIDE_EFFECTS (TREE_VALUE (t)))
2309*404b540aSrobert       {
2310*404b540aSrobert 	tree init;
2311*404b540aSrobert 	TREE_VALUE (t) = stabilize_expr (TREE_VALUE (t), &init);
2312*404b540aSrobert 	inits = add_stmt_to_compound (inits, init);
2313*404b540aSrobert       }
2314*404b540aSrobert 
2315*404b540aSrobert   *initp = inits;
2316*404b540aSrobert }
2317*404b540aSrobert 
2318*404b540aSrobert /* Like stabilize_expr, but for an initialization.
2319*404b540aSrobert 
2320*404b540aSrobert    If the initialization is for an object of class type, this function
2321*404b540aSrobert    takes care not to introduce additional temporaries.
2322*404b540aSrobert 
2323*404b540aSrobert    Returns TRUE iff the expression was successfully pre-evaluated,
2324*404b540aSrobert    i.e., if INIT is now side-effect free, except for, possible, a
2325*404b540aSrobert    single call to a constructor.  */
2326*404b540aSrobert 
2327*404b540aSrobert bool
stabilize_init(tree init,tree * initp)2328*404b540aSrobert stabilize_init (tree init, tree *initp)
2329*404b540aSrobert {
2330*404b540aSrobert   tree t = init;
2331*404b540aSrobert 
2332*404b540aSrobert   *initp = NULL_TREE;
2333*404b540aSrobert 
2334*404b540aSrobert   if (t == error_mark_node)
2335*404b540aSrobert     return true;
2336*404b540aSrobert 
2337*404b540aSrobert   if (TREE_CODE (t) == INIT_EXPR
2338*404b540aSrobert       && TREE_CODE (TREE_OPERAND (t, 1)) != TARGET_EXPR)
2339*404b540aSrobert     {
2340*404b540aSrobert       TREE_OPERAND (t, 1) = stabilize_expr (TREE_OPERAND (t, 1), initp);
2341*404b540aSrobert       return true;
2342*404b540aSrobert     }
2343*404b540aSrobert 
2344*404b540aSrobert   if (TREE_CODE (t) == INIT_EXPR)
2345*404b540aSrobert     t = TREE_OPERAND (t, 1);
2346*404b540aSrobert   if (TREE_CODE (t) == TARGET_EXPR)
2347*404b540aSrobert     t = TARGET_EXPR_INITIAL (t);
2348*404b540aSrobert   if (TREE_CODE (t) == COMPOUND_EXPR)
2349*404b540aSrobert     t = expr_last (t);
2350*404b540aSrobert   if (TREE_CODE (t) == CONSTRUCTOR
2351*404b540aSrobert       && EMPTY_CONSTRUCTOR_P (t))
2352*404b540aSrobert     /* Default-initialization.  */
2353*404b540aSrobert     return true;
2354*404b540aSrobert 
2355*404b540aSrobert   /* If the initializer is a COND_EXPR, we can't preevaluate
2356*404b540aSrobert      anything.  */
2357*404b540aSrobert   if (TREE_CODE (t) == COND_EXPR)
2358*404b540aSrobert     return false;
2359*404b540aSrobert 
2360*404b540aSrobert   if (TREE_CODE (t) == CALL_EXPR
2361*404b540aSrobert       || TREE_CODE (t) == AGGR_INIT_EXPR)
2362*404b540aSrobert     {
2363*404b540aSrobert       stabilize_call (t, initp);
2364*404b540aSrobert       return true;
2365*404b540aSrobert     }
2366*404b540aSrobert 
2367*404b540aSrobert   /* The initialization is being performed via a bitwise copy -- and
2368*404b540aSrobert      the item copied may have side effects.  */
2369*404b540aSrobert   return TREE_SIDE_EFFECTS (init);
2370*404b540aSrobert }
2371*404b540aSrobert 
2372*404b540aSrobert /* Like "fold", but should be used whenever we might be processing the
2373*404b540aSrobert    body of a template.  */
2374*404b540aSrobert 
2375*404b540aSrobert tree
fold_if_not_in_template(tree expr)2376*404b540aSrobert fold_if_not_in_template (tree expr)
2377*404b540aSrobert {
2378*404b540aSrobert   /* In the body of a template, there is never any need to call
2379*404b540aSrobert      "fold".  We will call fold later when actually instantiating the
2380*404b540aSrobert      template.  Integral constant expressions in templates will be
2381*404b540aSrobert      evaluated via fold_non_dependent_expr, as necessary.  */
2382*404b540aSrobert   if (processing_template_decl)
2383*404b540aSrobert     return expr;
2384*404b540aSrobert 
2385*404b540aSrobert   /* Fold C++ front-end specific tree codes.  */
2386*404b540aSrobert   if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
2387*404b540aSrobert     return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
2388*404b540aSrobert 
2389*404b540aSrobert   return fold (expr);
2390*404b540aSrobert }
2391*404b540aSrobert 
2392*404b540aSrobert /* Returns true if a cast to TYPE may appear in an integral constant
2393*404b540aSrobert    expression.  */
2394*404b540aSrobert 
2395*404b540aSrobert bool
cast_valid_in_integral_constant_expression_p(tree type)2396*404b540aSrobert cast_valid_in_integral_constant_expression_p (tree type)
2397*404b540aSrobert {
2398*404b540aSrobert   return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
2399*404b540aSrobert 	  || dependent_type_p (type)
2400*404b540aSrobert 	  || type == error_mark_node);
2401*404b540aSrobert }
2402*404b540aSrobert 
2403*404b540aSrobert 
2404*404b540aSrobert #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
2405*404b540aSrobert /* Complain that some language-specific thing hanging off a tree
2406*404b540aSrobert    node has been accessed improperly.  */
2407*404b540aSrobert 
2408*404b540aSrobert void
lang_check_failed(const char * file,int line,const char * function)2409*404b540aSrobert lang_check_failed (const char* file, int line, const char* function)
2410*404b540aSrobert {
2411*404b540aSrobert   internal_error ("lang_* check: failed in %s, at %s:%d",
2412*404b540aSrobert 		  function, trim_filename (file), line);
2413*404b540aSrobert }
2414*404b540aSrobert #endif /* ENABLE_TREE_CHECKING */
2415*404b540aSrobert 
2416*404b540aSrobert #include "gt-cp-tree.h"
2417