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