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