OSDN Git Service

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