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