]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/tree.c
tree-core.h (struct attribute_spec): Swap affects_type_identity and handler fields.
[thirdparty/gcc.git] / gcc / cp / tree.c
CommitLineData
8d08fdba 1/* Language-dependent node constructors for parse phase of GNU compiler.
cbe34bb5 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
8d08fdba
MS
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
f5adbb8d 5This file is part of GCC.
8d08fdba 6
f5adbb8d 7GCC is free software; you can redistribute it and/or modify
8d08fdba 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
10any later version.
11
f5adbb8d 12GCC is distributed in the hope that it will be useful,
8d08fdba
MS
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
8d08fdba
MS
20
21#include "config.h"
8d052bc7 22#include "system.h"
4977bab6 23#include "coretypes.h"
8d08fdba 24#include "tree.h"
2adfab87
AM
25#include "cp-tree.h"
26#include "gimple-expr.h"
27#include "cgraph.h"
d8a2d370
DN
28#include "stor-layout.h"
29#include "print-tree.h"
30#include "tree-iterator.h"
25af8512 31#include "tree-inline.h"
e58a9aa1 32#include "debug.h"
41990f96 33#include "convert.h"
2fb9a547 34#include "gimplify.h"
577eec56 35#include "stringpool.h"
b71983a5 36#include "attribs.h"
f3ec182d 37#include "flags.h"
12027a89 38
b57b79f7
NN
39static tree bot_manip (tree *, int *, void *);
40static tree bot_replace (tree *, int *, void *);
b57b79f7 41static hashval_t list_hash_pieces (tree, tree, tree);
574cfaa4 42static tree build_target_expr (tree, tree, tsubst_flags_t);
b57b79f7
NN
43static tree count_trees_r (tree *, int *, void *);
44static tree verify_stmt_tree_r (tree *, int *, void *);
a6f86b51 45static tree build_local_temp (tree);
b57b79f7 46
b57b79f7 47static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
7dbb85a7 48static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
91d231cb 49
27b8d0cd 50/* If REF is an lvalue, returns the kind of lvalue that REF is.
df5c89cb 51 Otherwise, returns clk_none. */
8d08fdba 52
4e9ca9b0
JM
53cp_lvalue_kind
54lvalue_kind (const_tree ref)
8ccc31eb 55{
27b8d0cd
MM
56 cp_lvalue_kind op1_lvalue_kind = clk_none;
57 cp_lvalue_kind op2_lvalue_kind = clk_none;
58
8af2fec4
RY
59 /* Expressions of reference type are sometimes wrapped in
60 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
61 representation, not part of the language, so we have to look
62 through them. */
31e292c7 63 if (REFERENCE_REF_P (ref))
4e9ca9b0 64 return lvalue_kind (TREE_OPERAND (ref, 0));
8af2fec4 65
8810610e
JJ
66 if (TREE_TYPE (ref)
67 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
8af2fec4
RY
68 {
69 /* unnamed rvalue references are rvalues */
70 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
71 && TREE_CODE (ref) != PARM_DECL
5a6ccc94 72 && !VAR_P (ref)
b24290fb
JM
73 && TREE_CODE (ref) != COMPONENT_REF
74 /* Functions are always lvalues. */
75 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
df5c89cb 76 return clk_rvalueref;
8af2fec4 77
d732e98f 78 /* lvalue references and named rvalue references are lvalues. */
8af2fec4
RY
79 return clk_ordinary;
80 }
8ccc31eb 81
394fd776 82 if (ref == current_class_ptr)
27b8d0cd 83 return clk_none;
8ccc31eb
MS
84
85 switch (TREE_CODE (ref))
86 {
8f4361eb
AP
87 case SAVE_EXPR:
88 return clk_none;
8ccc31eb 89 /* preincrements and predecrements are valid lvals, provided
e92cc029 90 what they refer to are valid lvals. */
8ccc31eb
MS
91 case PREINCREMENT_EXPR:
92 case PREDECREMENT_EXPR:
c7ae64f2 93 case TRY_CATCH_EXPR:
69851283
MM
94 case REALPART_EXPR:
95 case IMAGPART_EXPR:
4e9ca9b0 96 return lvalue_kind (TREE_OPERAND (ref, 0));
8ccc31eb 97
949bd6c8
JM
98 case MEMBER_REF:
99 case DOTSTAR_EXPR:
100 if (TREE_CODE (ref) == MEMBER_REF)
101 op1_lvalue_kind = clk_ordinary;
102 else
103 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
104 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
105 op1_lvalue_kind = clk_none;
106 return op1_lvalue_kind;
107
27b8d0cd 108 case COMPONENT_REF:
b447b28c
JJ
109 if (BASELINK_P (TREE_OPERAND (ref, 1)))
110 {
111 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
112
113 /* For static member function recurse on the BASELINK, we can get
114 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
115 OVERLOAD, the overload is resolved first if possible through
116 resolve_address_of_overloaded_function. */
117 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
118 return lvalue_kind (TREE_OPERAND (ref, 1));
119 }
4e9ca9b0 120 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
c8b2e872 121 /* Look at the member designator. */
4af9e878 122 if (!op1_lvalue_kind)
0cbd7506 123 ;
4af9e878
JM
124 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
125 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
b7da27c2
JM
126 situations. If we're seeing a COMPONENT_REF, it's a non-static
127 member, so it isn't an lvalue. */
128 op1_lvalue_kind = clk_none;
129 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
130 /* This can be IDENTIFIER_NODE in a template. */;
e0d1297c 131 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
27b8d0cd
MM
132 {
133 /* Clear the ordinary bit. If this object was a class
134 rvalue we want to preserve that information. */
135 op1_lvalue_kind &= ~clk_ordinary;
cd0be382 136 /* The lvalue is for a bitfield. */
27b8d0cd
MM
137 op1_lvalue_kind |= clk_bitfield;
138 }
e0d1297c
NS
139 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
140 op1_lvalue_kind |= clk_packed;
9f63daea 141
27b8d0cd
MM
142 return op1_lvalue_kind;
143
8ccc31eb 144 case STRING_CST:
266b4890 145 case COMPOUND_LITERAL_EXPR:
27b8d0cd 146 return clk_ordinary;
8ccc31eb 147
e58a9aa1 148 case CONST_DECL:
4b8c1a92
JJ
149 /* CONST_DECL without TREE_STATIC are enumeration values and
150 thus not lvalues. With TREE_STATIC they are used by ObjC++
151 in objc_build_string_object and need to be considered as
152 lvalues. */
153 if (! TREE_STATIC (ref))
154 return clk_none;
191816a3 155 /* FALLTHRU */
8ccc31eb 156 case VAR_DECL:
29c90a3c 157 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
70f40fea
JJ
158 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
159
8ccc31eb
MS
160 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
161 && DECL_LANG_SPECIFIC (ref)
162 && DECL_IN_AGGR_P (ref))
27b8d0cd 163 return clk_none;
191816a3 164 /* FALLTHRU */
8ccc31eb 165 case INDIRECT_REF:
e87b4dde 166 case ARROW_EXPR:
8ccc31eb
MS
167 case ARRAY_REF:
168 case PARM_DECL:
169 case RESULT_DECL:
3e605b20 170 case PLACEHOLDER_EXPR:
ea48c8a0 171 return clk_ordinary;
8ccc31eb 172
3ee353e9
JM
173 /* A scope ref in a template, left as SCOPE_REF to support later
174 access checking. */
8ccc31eb 175 case SCOPE_REF:
c5c8755a
JM
176 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
177 {
178 tree op = TREE_OPERAND (ref, 1);
179 if (TREE_CODE (op) == FIELD_DECL)
180 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
181 else
182 return lvalue_kind (op);
183 }
3ee353e9 184
27b8d0cd
MM
185 case MAX_EXPR:
186 case MIN_EXPR:
d211a298
RS
187 /* Disallow <? and >? as lvalues if either argument side-effects. */
188 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
189 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
190 return clk_none;
4e9ca9b0
JM
191 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
192 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
8ccc31eb
MS
193 break;
194
195 case COND_EXPR:
4e9ca9b0 196 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
42924ed7 197 ? TREE_OPERAND (ref, 1)
df5c89cb 198 : TREE_OPERAND (ref, 0));
4e9ca9b0 199 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
27b8d0cd 200 break;
8ccc31eb 201
46ab17db
PP
202 case MODOP_EXPR:
203 /* We expect to see unlowered MODOP_EXPRs only during
204 template processing. */
205 gcc_assert (processing_template_decl);
206 return clk_ordinary;
207
8ccc31eb 208 case MODIFY_EXPR:
e87b4dde 209 case TYPEID_EXPR:
27b8d0cd 210 return clk_ordinary;
8ccc31eb
MS
211
212 case COMPOUND_EXPR:
4e9ca9b0 213 return lvalue_kind (TREE_OPERAND (ref, 1));
69851283
MM
214
215 case TARGET_EXPR:
df5c89cb 216 return clk_class;
69851283 217
356955cf 218 case VA_ARG_EXPR:
df5c89cb 219 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
c0ad5a31
MM
220
221 case CALL_EXPR:
e87b4dde
JM
222 /* We can see calls outside of TARGET_EXPR in templates. */
223 if (CLASS_TYPE_P (TREE_TYPE (ref)))
224 return clk_class;
4e8dca1c 225 return clk_none;
69851283
MM
226
227 case FUNCTION_DECL:
228 /* All functions (except non-static-member functions) are
229 lvalues. */
9f63daea 230 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
27b8d0cd 231 ? clk_none : clk_ordinary);
7f85441b 232
4af9e878
JM
233 case BASELINK:
234 /* We now represent a reference to a single static member function
235 with a BASELINK. */
1e4ae551
MLI
236 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
237 its argument unmodified and we assign it to a const_tree. */
4e9ca9b0 238 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
4af9e878 239
d17811fd 240 case NON_DEPENDENT_EXPR:
92886d3e 241 return lvalue_kind (TREE_OPERAND (ref, 0));
d17811fd 242
7f85441b 243 default:
e87b4dde
JM
244 if (!TREE_TYPE (ref))
245 return clk_none;
d478f1e4
JM
246 if (CLASS_TYPE_P (TREE_TYPE (ref))
247 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
e87b4dde 248 return clk_class;
7f85441b 249 break;
8ccc31eb
MS
250 }
251
27b8d0cd
MM
252 /* If one operand is not an lvalue at all, then this expression is
253 not an lvalue. */
254 if (!op1_lvalue_kind || !op2_lvalue_kind)
255 return clk_none;
256
257 /* Otherwise, it's an lvalue, and it has all the odd properties
258 contributed by either operand. */
259 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
9771799c 260 /* It's not an ordinary lvalue if it involves any other kind. */
27b8d0cd
MM
261 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
262 op1_lvalue_kind &= ~clk_ordinary;
9771799c
JM
263 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
264 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
265 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
266 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
267 op1_lvalue_kind = clk_none;
27b8d0cd 268 return op1_lvalue_kind;
8ccc31eb
MS
269}
270
72b3e203 271/* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
aa6e8ed3
MM
272
273cp_lvalue_kind
4e9ca9b0 274real_lvalue_p (const_tree ref)
aa6e8ed3 275{
4e9ca9b0 276 cp_lvalue_kind kind = lvalue_kind (ref);
df5c89cb
JM
277 if (kind & (clk_rvalueref|clk_class))
278 return clk_none;
279 else
280 return kind;
aa6e8ed3
MM
281}
282
72b3e203 283/* c-common wants us to return bool. */
bb19d4af
JM
284
285bool
72b3e203 286lvalue_p (const_tree t)
bb19d4af
JM
287{
288 return real_lvalue_p (t);
289}
290
72b3e203 291/* This differs from lvalue_p in that xvalues are included. */
69851283 292
1e4ae551 293bool
c3edc633 294glvalue_p (const_tree ref)
df5c89cb 295{
4e9ca9b0 296 cp_lvalue_kind kind = lvalue_kind (ref);
df5c89cb
JM
297 if (kind & clk_class)
298 return false;
299 else
300 return (kind != clk_none);
6c6e776d
MA
301}
302
c3edc633
JM
303/* This differs from glvalue_p in that class prvalues are included. */
304
305bool
bb19d4af 306obvalue_p (const_tree ref)
c3edc633
JM
307{
308 return (lvalue_kind (ref) != clk_none);
309}
310
311/* Returns true if REF is an xvalue (the result of dereferencing an rvalue
312 reference), false otherwise. */
04398fa8
PC
313
314bool
315xvalue_p (const_tree ref)
316{
317 return (lvalue_kind (ref) == clk_rvalueref);
318}
319
47e5d7cc
JM
320/* True if REF is a bit-field. */
321
322bool
323bitfield_p (const_tree ref)
324{
325 return (lvalue_kind (ref) & clk_bitfield);
326}
327
5a0802ea
MP
328/* C++-specific version of stabilize_reference. */
329
330tree
331cp_stabilize_reference (tree ref)
332{
333 switch (TREE_CODE (ref))
334 {
5c9c546b
JM
335 case NON_DEPENDENT_EXPR:
336 /* We aren't actually evaluating this. */
337 return ref;
338
5a0802ea
MP
339 /* We need to treat specially anything stabilize_reference doesn't
340 handle specifically. */
341 case VAR_DECL:
342 case PARM_DECL:
343 case RESULT_DECL:
344 CASE_CONVERT:
345 case FLOAT_EXPR:
346 case FIX_TRUNC_EXPR:
347 case INDIRECT_REF:
348 case COMPONENT_REF:
349 case BIT_FIELD_REF:
350 case ARRAY_REF:
351 case ARRAY_RANGE_REF:
352 case ERROR_MARK:
353 break;
354 default:
355 cp_lvalue_kind kind = lvalue_kind (ref);
356 if ((kind & ~clk_class) != clk_none)
357 {
358 tree type = unlowered_expr_type (ref);
359 bool rval = !!(kind & clk_rvalueref);
360 type = cp_build_reference_type (type, rval);
361 /* This inhibits warnings in, eg, cxx_mark_addressable
362 (c++/60955). */
363 warning_sentinel s (extra_warnings);
364 ref = build_static_cast (type, ref, tf_error);
365 }
366 }
367
368 return stabilize_reference (ref);
369}
370
100d337a
MA
371/* Test whether DECL is a builtin that may appear in a
372 constant-expression. */
373
374bool
58f9752a 375builtin_valid_in_constant_expr_p (const_tree decl)
100d337a 376{
ce209777
JJ
377 if (!(TREE_CODE (decl) == FUNCTION_DECL
378 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL))
cda0a029
JM
379 /* Not a built-in. */
380 return false;
381 switch (DECL_FUNCTION_CODE (decl))
382 {
b25aad5f
MS
383 /* These always have constant results like the corresponding
384 macros/symbol. */
385 case BUILT_IN_FILE:
386 case BUILT_IN_FUNCTION:
387 case BUILT_IN_LINE:
388
44a845ca
MS
389 /* The following built-ins are valid in constant expressions
390 when their arguments are. */
391 case BUILT_IN_ADD_OVERFLOW_P:
392 case BUILT_IN_SUB_OVERFLOW_P:
393 case BUILT_IN_MUL_OVERFLOW_P:
394
cda0a029
JM
395 /* These have constant results even if their operands are
396 non-constant. */
b25aad5f
MS
397 case BUILT_IN_CONSTANT_P:
398 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
cda0a029
JM
399 return true;
400 default:
401 return false;
402 }
100d337a
MA
403}
404
c506ca22
MM
405/* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
406
407static tree
574cfaa4 408build_target_expr (tree decl, tree value, tsubst_flags_t complain)
c506ca22
MM
409{
410 tree t;
30fdd4f2 411 tree type = TREE_TYPE (decl);
04941f76 412
6e085858
JM
413 value = mark_rvalue_use (value);
414
595278be
MM
415 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
416 || TREE_TYPE (decl) == TREE_TYPE (value)
417 /* On ARM ctors return 'this'. */
418 || (TYPE_PTR_P (TREE_TYPE (value))
419 && TREE_CODE (value) == CALL_EXPR)
420 || useless_type_conversion_p (TREE_TYPE (decl),
421 TREE_TYPE (value)));
c506ca22 422
1d7bc790
NS
423 if (complain & tf_no_cleanup)
424 /* The caller is building a new-expr and does not need a cleanup. */
425 t = NULL_TREE;
426 else
427 {
428 t = cxx_maybe_build_cleanup (decl, complain);
429 if (t == error_mark_node)
430 return error_mark_node;
431 }
30fdd4f2 432 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
f107ee33
JM
433 if (EXPR_HAS_LOCATION (value))
434 SET_EXPR_LOCATION (t, EXPR_LOCATION (value));
c506ca22
MM
435 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
436 ignore the TARGET_EXPR. If there really turn out to be no
437 side-effects, then the optimizer should be able to get rid of
438 whatever code is generated anyhow. */
439 TREE_SIDE_EFFECTS (t) = 1;
440
441 return t;
442}
443
a6f86b51
JM
444/* Return an undeclared local temporary of type TYPE for use in building a
445 TARGET_EXPR. */
446
447static tree
448build_local_temp (tree type)
449{
c2255bc4
AH
450 tree slot = build_decl (input_location,
451 VAR_DECL, NULL_TREE, type);
a6f86b51 452 DECL_ARTIFICIAL (slot) = 1;
78e0d62b 453 DECL_IGNORED_P (slot) = 1;
a6f86b51
JM
454 DECL_CONTEXT (slot) = current_function_decl;
455 layout_decl (slot, 0);
456 return slot;
457}
458
5039610b
SL
459/* Set various status flags when building an AGGR_INIT_EXPR object T. */
460
461static void
462process_aggr_init_operands (tree t)
463{
464 bool side_effects;
465
466 side_effects = TREE_SIDE_EFFECTS (t);
467 if (!side_effects)
468 {
469 int i, n;
470 n = TREE_OPERAND_LENGTH (t);
471 for (i = 1; i < n; i++)
472 {
473 tree op = TREE_OPERAND (t, i);
474 if (op && TREE_SIDE_EFFECTS (op))
475 {
476 side_effects = 1;
477 break;
478 }
479 }
480 }
481 TREE_SIDE_EFFECTS (t) = side_effects;
482}
483
484/* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
485 FN, and SLOT. NARGS is the number of call arguments which are specified
486 as a tree array ARGS. */
487
488static tree
489build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
490 tree *args)
491{
492 tree t;
493 int i;
494
495 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
496 TREE_TYPE (t) = return_type;
497 AGGR_INIT_EXPR_FN (t) = fn;
498 AGGR_INIT_EXPR_SLOT (t) = slot;
499 for (i = 0; i < nargs; i++)
500 AGGR_INIT_EXPR_ARG (t, i) = args[i];
501 process_aggr_init_operands (t);
502 return t;
503}
504
505/* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
844ae01d 506 target. TYPE is the type to be initialized.
8d08fdba 507
844ae01d
JM
508 Build an AGGR_INIT_EXPR to represent the initialization. This function
509 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
510 to initialize another object, whereas a TARGET_EXPR can either
511 initialize another object or create its own temporary object, and as a
512 result building up a TARGET_EXPR requires that the type's destructor be
513 callable. */
e92cc029 514
8d08fdba 515tree
094484e7 516build_aggr_init_expr (tree type, tree init)
8d08fdba 517{
e1376b00 518 tree fn;
e8abc66f
MS
519 tree slot;
520 tree rval;
4977bab6 521 int is_ctor;
e8abc66f 522
e4d7d8cb
JM
523 /* Don't build AGGR_INIT_EXPR in a template. */
524 if (processing_template_decl)
525 return init;
526
babaa9df
JM
527 fn = cp_get_callee (init);
528 if (fn == NULL_TREE)
06126ca2 529 return convert (type, init);
c11b6f21 530
4977bab6
ZW
531 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
532 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
533 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
534
e1376b00
MM
535 /* We split the CALL_EXPR into its function and its arguments here.
536 Then, in expand_expr, we put them back together. The reason for
537 this is that this expression might be a default argument
538 expression. In that case, we need a new temporary every time the
539 expression is used. That's what break_out_target_exprs does; it
540 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
541 temporary slot. Then, expand_expr builds up a call-expression
542 using the new slot. */
4977bab6
ZW
543
544 /* If we don't need to use a constructor to create an object of this
545 type, don't mess with AGGR_INIT_EXPR. */
546 if (is_ctor || TREE_ADDRESSABLE (type))
547 {
844ae01d
JM
548 slot = build_local_temp (type);
549
8ba8c375
JM
550 if (TREE_CODE (init) == CALL_EXPR)
551 {
552 rval = build_aggr_init_array (void_type_node, fn, slot,
553 call_expr_nargs (init),
554 CALL_EXPR_ARGP (init));
555 AGGR_INIT_FROM_THUNK_P (rval)
556 = CALL_FROM_THUNK_P (init);
557 }
5039610b 558 else
8ba8c375
JM
559 {
560 rval = build_aggr_init_array (void_type_node, fn, slot,
561 aggr_init_expr_nargs (init),
562 AGGR_INIT_EXPR_ARGP (init));
563 AGGR_INIT_FROM_THUNK_P (rval)
564 = AGGR_INIT_FROM_THUNK_P (init);
565 }
4977bab6
ZW
566 TREE_SIDE_EFFECTS (rval) = 1;
567 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
d8a0d13e 568 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
4eb24e01
JM
569 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
570 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
571 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
4977bab6
ZW
572 }
573 else
574 rval = init;
575
844ae01d
JM
576 return rval;
577}
578
579/* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
580 target. TYPE is the type that this initialization should appear to
581 have.
582
583 Build an encapsulation of the initialization to perform
584 and return it so that it can be processed by language-independent
585 and language-specific expression expanders. */
586
587tree
362115a9 588build_cplus_new (tree type, tree init, tsubst_flags_t complain)
844ae01d 589{
094484e7 590 tree rval = build_aggr_init_expr (type, init);
844ae01d
JM
591 tree slot;
592
57fcd4f4
JM
593 if (!complete_type_or_maybe_complain (type, init, complain))
594 return error_mark_node;
595
0ed4ab44
JM
596 /* Make sure that we're not trying to create an instance of an
597 abstract class. */
598 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
599 return error_mark_node;
600
844ae01d
JM
601 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
602 slot = AGGR_INIT_EXPR_SLOT (rval);
236fd18c
JM
603 else if (TREE_CODE (rval) == CALL_EXPR
604 || TREE_CODE (rval) == CONSTRUCTOR)
844ae01d
JM
605 slot = build_local_temp (type);
606 else
607 return rval;
608
574cfaa4 609 rval = build_target_expr (slot, rval, complain);
a6343f61
PC
610
611 if (rval != error_mark_node)
612 TARGET_EXPR_IMPLICIT_P (rval) = 1;
8d08fdba 613
8d08fdba
MS
614 return rval;
615}
616
262a7d6b
JM
617/* Subroutine of build_vec_init_expr: Build up a single element
618 intialization as a proxy for the full array initialization to get things
619 marked as used and any appropriate diagnostics.
620
621 Since we're deferring building the actual constructor calls until
622 gimplification time, we need to build one now and throw it away so
623 that the relevant constructor gets mark_used before cgraph decides
624 what functions are needed. Here we assume that init is either
625 NULL_TREE, void_type_node (indicating value-initialization), or
626 another array to copy. */
627
628static tree
9c69dcea 629build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
262a7d6b 630{
b73a4704 631 tree inner_type = strip_array_types (type);
9771b263 632 vec<tree, va_gc> *argvec;
262a7d6b 633
b73a4704
JM
634 if (integer_zerop (array_type_nelts_total (type))
635 || !CLASS_TYPE_P (inner_type))
262a7d6b
JM
636 /* No interesting initialization to do. */
637 return integer_zero_node;
638 else if (init == void_type_node)
9c69dcea 639 return build_value_init (inner_type, complain);
262a7d6b 640
b73a4704
JM
641 gcc_assert (init == NULL_TREE
642 || (same_type_ignoring_top_level_qualifiers_p
643 (type, TREE_TYPE (init))));
644
645 argvec = make_tree_vector ();
646 if (init)
262a7d6b 647 {
01290963
JM
648 tree init_type = strip_array_types (TREE_TYPE (init));
649 tree dummy = build_dummy_object (init_type);
72b3e203 650 if (!lvalue_p (init))
262a7d6b 651 dummy = move (dummy);
9771b263 652 argvec->quick_push (dummy);
262a7d6b 653 }
9c69dcea 654 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
262a7d6b 655 &argvec, inner_type, LOOKUP_NORMAL,
9c69dcea
JM
656 complain);
657 release_tree_vector (argvec);
658
20888def
JM
659 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
660 we don't want one here because we aren't creating a temporary. */
661 if (TREE_CODE (init) == TARGET_EXPR)
662 init = TARGET_EXPR_INITIAL (init);
663
9c69dcea 664 return init;
262a7d6b
JM
665}
666
b73a4704
JM
667/* Return a TARGET_EXPR which expresses the initialization of an array to
668 be named later, either default-initialization or copy-initialization
669 from another array of the same type. */
d5f4eddd
JM
670
671tree
9c69dcea 672build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
d5f4eddd 673{
b73a4704 674 tree slot;
4de2f020 675 bool value_init = false;
9c69dcea 676 tree elt_init = build_vec_init_elt (type, init, complain);
534ecb17 677
262a7d6b 678 if (init == void_type_node)
534ecb17 679 {
4de2f020
JM
680 value_init = true;
681 init = NULL_TREE;
682 }
534ecb17 683
b73a4704
JM
684 slot = build_local_temp (type);
685 init = build2 (VEC_INIT_EXPR, type, slot, init);
0a2cdfe6 686 TREE_SIDE_EFFECTS (init) = true;
d5f4eddd 687 SET_EXPR_LOCATION (init, input_location);
4de2f020 688
604b2bfc 689 if (cxx_dialect >= cxx11
262a7d6b
JM
690 && potential_constant_expression (elt_init))
691 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
4de2f020
JM
692 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
693
d5f4eddd
JM
694 return init;
695}
696
262a7d6b
JM
697/* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
698 that requires a constant expression. */
699
700void
701diagnose_non_constexpr_vec_init (tree expr)
702{
703 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
704 tree init, elt_init;
705 if (VEC_INIT_EXPR_VALUE_INIT (expr))
7ac37b96 706 init = void_type_node;
262a7d6b
JM
707 else
708 init = VEC_INIT_EXPR_INIT (expr);
709
9c69dcea 710 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
262a7d6b
JM
711 require_potential_constant_expression (elt_init);
712}
713
534ecb17
JM
714tree
715build_array_copy (tree init)
716{
9c69dcea 717 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
534ecb17
JM
718}
719
ab93b543 720/* Build a TARGET_EXPR using INIT to initialize a new temporary of the
c506ca22 721 indicated TYPE. */
aa36c081
JM
722
723tree
574cfaa4 724build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
aa36c081 725{
50bc768d 726 gcc_assert (!VOID_TYPE_P (type));
59445d74 727
309714d4
JM
728 if (TREE_CODE (init) == TARGET_EXPR
729 || init == error_mark_node)
5062dbd5 730 return init;
d758e847 731 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
7efc22ea 732 && !VOID_TYPE_P (TREE_TYPE (init))
4b5aa881 733 && TREE_CODE (init) != COND_EXPR
662eceda
MM
734 && TREE_CODE (init) != CONSTRUCTOR
735 && TREE_CODE (init) != VA_ARG_EXPR)
7efc22ea
JM
736 /* We need to build up a copy constructor call. A void initializer
737 means we're being called from bot_manip. COND_EXPR is a special
182609b5 738 case because we already have copies on the arms and we don't want
4b5aa881 739 another one here. A CONSTRUCTOR is aggregate initialization, which
662eceda
MM
740 is handled separately. A VA_ARG_EXPR is magic creation of an
741 aggregate; there's no additional work to be done. */
574cfaa4 742 return force_rvalue (init, complain);
5062dbd5 743
574cfaa4 744 return force_target_expr (type, init, complain);
a6f86b51 745}
aa36c081 746
a6f86b51
JM
747/* Like the above function, but without the checking. This function should
748 only be used by code which is deliberately trying to subvert the type
d758e847
JM
749 system, such as call_builtin_trap. Or build_over_call, to avoid
750 infinite recursion. */
a6f86b51
JM
751
752tree
574cfaa4 753force_target_expr (tree type, tree init, tsubst_flags_t complain)
a6f86b51 754{
59445d74
RH
755 tree slot;
756
50bc768d 757 gcc_assert (!VOID_TYPE_P (type));
59445d74
RH
758
759 slot = build_local_temp (type);
574cfaa4 760 return build_target_expr (slot, init, complain);
aa36c081
JM
761}
762
c506ca22
MM
763/* Like build_target_expr_with_type, but use the type of INIT. */
764
765tree
574cfaa4 766get_target_expr_sfinae (tree init, tsubst_flags_t complain)
c506ca22 767{
450a927a 768 if (TREE_CODE (init) == AGGR_INIT_EXPR)
574cfaa4 769 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
991e0156 770 else if (TREE_CODE (init) == VEC_INIT_EXPR)
574cfaa4 771 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
450a927a 772 else
6e085858
JM
773 {
774 init = convert_bitfield_to_declared_type (init);
775 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
776 }
574cfaa4
JM
777}
778
779tree
780get_target_expr (tree init)
781{
782 return get_target_expr_sfinae (init, tf_warning_or_error);
c506ca22
MM
783}
784
e1039697
MM
785/* If EXPR is a bitfield reference, convert it to the declared type of
786 the bitfield, and return the resulting expression. Otherwise,
787 return EXPR itself. */
788
789tree
790convert_bitfield_to_declared_type (tree expr)
791{
792 tree bitfield_type;
793
794 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
795 if (bitfield_type)
cda0a029
JM
796 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
797 expr);
e1039697
MM
798 return expr;
799}
800
5cc53d4e
MM
801/* EXPR is being used in an rvalue context. Return a version of EXPR
802 that is marked as an rvalue. */
803
804tree
805rvalue (tree expr)
806{
41990f96
MM
807 tree type;
808
809 if (error_operand_p (expr))
810 return expr;
811
03a904b5
JJ
812 expr = mark_rvalue_use (expr);
813
41990f96
MM
814 /* [basic.lval]
815
816 Non-class rvalues always have cv-unqualified types. */
817 type = TREE_TYPE (expr);
36c37128
JM
818 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
819 type = cv_unqualified (type);
41990f96 820
b9c6b842
JM
821 /* We need to do this for rvalue refs as well to get the right answer
822 from decltype; see c++/36628. */
c3edc633 823 if (!processing_template_decl && glvalue_p (expr))
41990f96
MM
824 expr = build1 (NON_LVALUE_EXPR, type, expr);
825 else if (type != TREE_TYPE (expr))
826 expr = build_nop (type, expr);
827
5cc53d4e
MM
828 return expr;
829}
830
8d08fdba 831\f
2a22f99c
TS
832struct cplus_array_info
833{
834 tree type;
835 tree domain;
836};
837
ca752f39 838struct cplus_array_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
839{
840 typedef cplus_array_info *compare_type;
841
842 static hashval_t hash (tree t);
843 static bool equal (tree, cplus_array_info *);
844};
845
06d40de8
DG
846/* Hash an ARRAY_TYPE. K is really of type `tree'. */
847
2a22f99c
TS
848hashval_t
849cplus_array_hasher::hash (tree t)
06d40de8
DG
850{
851 hashval_t hash;
06d40de8 852
eb9c434c
JJ
853 hash = TYPE_UID (TREE_TYPE (t));
854 if (TYPE_DOMAIN (t))
855 hash ^= TYPE_UID (TYPE_DOMAIN (t));
06d40de8
DG
856 return hash;
857}
858
06d40de8
DG
859/* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
860 of type `cplus_array_info*'. */
861
2a22f99c
TS
862bool
863cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
06d40de8 864{
714f2304 865 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
06d40de8
DG
866}
867
38e40fcd
JM
868/* Hash table containing dependent array types, which are unsuitable for
869 the language-independent type hash table. */
2a22f99c 870static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
06d40de8 871
33cb682b
JM
872/* Build an ARRAY_TYPE without laying it out. */
873
874static tree
875build_min_array_type (tree elt_type, tree index_type)
876{
877 tree t = cxx_make_type (ARRAY_TYPE);
878 TREE_TYPE (t) = elt_type;
879 TYPE_DOMAIN (t) = index_type;
880 return t;
881}
882
883/* Set TYPE_CANONICAL like build_array_type_1, but using
884 build_cplus_array_type. */
885
886static void
887set_array_type_canon (tree t, tree elt_type, tree index_type)
888{
889 /* Set the canonical type for this new node. */
890 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
891 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
892 SET_TYPE_STRUCTURAL_EQUALITY (t);
893 else if (TYPE_CANONICAL (elt_type) != elt_type
894 || (index_type && TYPE_CANONICAL (index_type) != index_type))
895 TYPE_CANONICAL (t)
896 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
897 index_type
898 ? TYPE_CANONICAL (index_type) : index_type);
899 else
900 TYPE_CANONICAL (t) = t;
901}
902
903/* Like build_array_type, but handle special C++ semantics: an array of a
904 variant element type is a variant of the array of the main variant of
905 the element type. */
06d40de8 906
38e40fcd
JM
907tree
908build_cplus_array_type (tree elt_type, tree index_type)
8d08fdba 909{
8d08fdba
MS
910 tree t;
911
adecb3f4
MM
912 if (elt_type == error_mark_node || index_type == error_mark_node)
913 return error_mark_node;
914
887ab4e5
PP
915 bool dependent = (uses_template_parms (elt_type)
916 || (index_type && uses_template_parms (index_type)));
33cb682b
JM
917
918 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
919 /* Start with an array of the TYPE_MAIN_VARIANT. */
920 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
921 index_type);
922 else if (dependent)
5566b478 923 {
33cb682b
JM
924 /* Since type_hash_canon calls layout_type, we need to use our own
925 hash table. */
06d40de8
DG
926 cplus_array_info cai;
927 hashval_t hash;
714f2304 928
06d40de8 929 if (cplus_array_htab == NULL)
2a22f99c 930 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
06d40de8 931
eb9c434c
JJ
932 hash = TYPE_UID (elt_type);
933 if (index_type)
934 hash ^= TYPE_UID (index_type);
06d40de8
DG
935 cai.type = elt_type;
936 cai.domain = index_type;
937
2a22f99c 938 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
06d40de8 939 if (*e)
714f2304 940 /* We have found the type: we're done. */
06d40de8
DG
941 return (tree) *e;
942 else
943 {
714f2304 944 /* Build a new array type. */
33cb682b 945 t = build_min_array_type (elt_type, index_type);
06d40de8 946
714f2304
DG
947 /* Store it in the hash table. */
948 *e = t;
949
950 /* Set the canonical type for this new node. */
33cb682b 951 set_array_type_canon (t, elt_type, index_type);
06d40de8 952 }
5566b478
MS
953 }
954 else
3ebc22c1 955 {
8a59d466
JJ
956 bool typeless_storage
957 = (elt_type == unsigned_char_type_node
958 || elt_type == signed_char_type_node
959 || elt_type == char_type_node
960 || (TREE_CODE (elt_type) == ENUMERAL_TYPE
961 && TYPE_CONTEXT (elt_type) == std_node
962 && !strcmp ("byte", TYPE_NAME_STRING (elt_type))));
963 t = build_array_type (elt_type, index_type, typeless_storage);
3ebc22c1 964 }
8d08fdba 965
33cb682b 966 /* Now check whether we already have this array variant. */
38e40fcd
JM
967 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
968 {
33cb682b
JM
969 tree m = t;
970 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
024da309
JM
971 if (TREE_TYPE (t) == elt_type
972 && TYPE_NAME (t) == NULL_TREE
973 && TYPE_ATTRIBUTES (t) == NULL_TREE)
33cb682b
JM
974 break;
975 if (!t)
38e40fcd 976 {
33cb682b
JM
977 t = build_min_array_type (elt_type, index_type);
978 set_array_type_canon (t, elt_type, index_type);
00da5e28
JJ
979 if (!dependent)
980 {
981 layout_type (t);
982 /* Make sure sizes are shared with the main variant.
983 layout_type can't be called after setting TYPE_NEXT_VARIANT,
984 as it will overwrite alignment etc. of all variants. */
985 TYPE_SIZE (t) = TYPE_SIZE (m);
986 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
350792ff 987 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
00da5e28 988 }
e78167f2 989
38e40fcd
JM
990 TYPE_MAIN_VARIANT (t) = m;
991 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
992 TYPE_NEXT_VARIANT (m) = t;
993 }
994 }
995
03d31730
PC
996 /* Avoid spurious warnings with VLAs (c++/54583). */
997 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
998 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
999
33cb682b
JM
1000 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1001 place more easily. */
1002 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1003 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1004 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1005 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1006
1007 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1008 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1009 {
1010 /* The element type has been completed since the last time we saw
1011 this array type; update the layout and 'tor flags for any variants
1012 that need it. */
1013 layout_type (t);
1014 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1015 {
1016 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1017 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1018 }
1019 }
1020
8d08fdba
MS
1021 return t;
1022}
e349ee73 1023
09357846
JM
1024/* Return an ARRAY_TYPE with element type ELT and length N. */
1025
1026tree
1027build_array_of_n_type (tree elt, int n)
1028{
1029 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1030}
1031
94a073b2
JM
1032/* True iff T is an N3639 array of runtime bound (VLA). These were
1033 approved for C++14 but then removed. */
0138d6b2
JM
1034
1035bool
1036array_of_runtime_bound_p (tree t)
1037{
1038 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1039 return false;
1040 tree dom = TYPE_DOMAIN (t);
1041 if (!dom)
1042 return false;
1043 tree max = TYPE_MAX_VALUE (dom);
593bcbb8
JM
1044 return (!potential_rvalue_constant_expression (max)
1045 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
0138d6b2
JM
1046}
1047
8af2fec4
RY
1048/* Return a reference type node referring to TO_TYPE. If RVAL is
1049 true, return an rvalue reference type, otherwise return an lvalue
1050 reference type. If a type node exists, reuse it, otherwise create
1051 a new one. */
1052tree
1053cp_build_reference_type (tree to_type, bool rval)
1054{
1055 tree lvalue_ref, t;
70f40fea
JJ
1056
1057 if (TREE_CODE (to_type) == REFERENCE_TYPE)
1058 {
1059 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1060 to_type = TREE_TYPE (to_type);
1061 }
1062
8af2fec4
RY
1063 lvalue_ref = build_reference_type (to_type);
1064 if (!rval)
1065 return lvalue_ref;
1066
1067 /* This code to create rvalue reference types is based on and tied
1068 to the code creating lvalue reference types in the middle-end
1069 functions build_reference_type_for_mode and build_reference_type.
1070
1071 It works by putting the rvalue reference type nodes after the
1072 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1073 they will effectively be ignored by the middle end. */
1074
1075 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1076 if (TYPE_REF_IS_RVALUE (t))
1077 return t;
1078
22521c89 1079 t = build_distinct_type_copy (lvalue_ref);
8af2fec4
RY
1080
1081 TYPE_REF_IS_RVALUE (t) = true;
1082 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1083 TYPE_NEXT_REF_TO (lvalue_ref) = t;
8af2fec4
RY
1084
1085 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1086 SET_TYPE_STRUCTURAL_EQUALITY (t);
1087 else if (TYPE_CANONICAL (to_type) != to_type)
1088 TYPE_CANONICAL (t)
1089 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
1090 else
1091 TYPE_CANONICAL (t) = t;
1092
1093 layout_type (t);
1094
1095 return t;
1096
1097}
1098
d5f4eddd
JM
1099/* Returns EXPR cast to rvalue reference type, like std::move. */
1100
1101tree
1102move (tree expr)
1103{
1104 tree type = TREE_TYPE (expr);
1105 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
1106 type = cp_build_reference_type (type, /*rval*/true);
1107 return build_static_cast (type, expr, tf_warning_or_error);
1108}
1109
9ae165a0
DG
1110/* Used by the C++ front end to build qualified array types. However,
1111 the C version of this function does not properly maintain canonical
1112 types (which are not used in C). */
1113tree
e9e32ee6
JM
1114c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1115 size_t /* orig_qual_indirect */)
9ae165a0
DG
1116{
1117 return cp_build_qualified_type (type, type_quals);
1118}
8af2fec4 1119
8d08fdba 1120\f
adecb3f4
MM
1121/* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1122 arrays correctly. In particular, if TYPE is an array of T's, and
c2ea3a40 1123 TYPE_QUALS is non-empty, returns an array of qualified T's.
9f63daea 1124
a59b92b0 1125 FLAGS determines how to deal with ill-formed qualifications. If
4f2b0fb2
NS
1126 tf_ignore_bad_quals is set, then bad qualifications are dropped
1127 (this is permitted if TYPE was introduced via a typedef or template
1128 type parameter). If bad qualifications are dropped and tf_warning
1129 is set, then a warning is issued for non-const qualifications. If
1130 tf_ignore_bad_quals is not set and tf_error is not set, we
1131 return error_mark_node. Otherwise, we issue an error, and ignore
1132 the qualifications.
1133
1134 Qualification of a reference type is valid when the reference came
1135 via a typedef or template type argument. [dcl.ref] No such
1136 dispensation is provided for qualifying a function type. [dcl.fct]
1137 DR 295 queries this and the proposed resolution brings it into line
34cd5ae7 1138 with qualifying a reference. We implement the DR. We also behave
4f2b0fb2 1139 in a similar manner for restricting non-pointer types. */
9f63daea 1140
f376e137 1141tree
9f63daea 1142cp_build_qualified_type_real (tree type,
0cbd7506
MS
1143 int type_quals,
1144 tsubst_flags_t complain)
f376e137 1145{
2adeacc9 1146 tree result;
4f2b0fb2 1147 int bad_quals = TYPE_UNQUALIFIED;
2adeacc9 1148
e76a2646
MS
1149 if (type == error_mark_node)
1150 return type;
e271912d 1151
89d684bb 1152 if (type_quals == cp_type_quals (type))
e271912d
JM
1153 return type;
1154
4f2b0fb2 1155 if (TREE_CODE (type) == ARRAY_TYPE)
f376e137 1156 {
db3626d1
MM
1157 /* In C++, the qualification really applies to the array element
1158 type. Obtain the appropriately qualified element type. */
1159 tree t;
9f63daea
EC
1160 tree element_type
1161 = cp_build_qualified_type_real (TREE_TYPE (type),
db3626d1
MM
1162 type_quals,
1163 complain);
1164
1165 if (element_type == error_mark_node)
adecb3f4 1166 return error_mark_node;
f376e137 1167
38e40fcd
JM
1168 /* See if we already have an identically qualified type. Tests
1169 should be equivalent to those in check_qualified_type. */
29fae15c 1170 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
ef765996 1171 if (TREE_TYPE (t) == element_type
29fae15c 1172 && TYPE_NAME (t) == TYPE_NAME (type)
38e40fcd
JM
1173 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1174 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1175 TYPE_ATTRIBUTES (type)))
29fae15c 1176 break;
9f63daea 1177
29fae15c 1178 if (!t)
38e40fcd
JM
1179 {
1180 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1181
1182 /* Keep the typedef name. */
1183 if (TYPE_NAME (t) != TYPE_NAME (type))
1184 {
1185 t = build_variant_type_copy (t);
1186 TYPE_NAME (t) = TYPE_NAME (type);
fe37c7af 1187 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
0212e31e 1188 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
38e40fcd
JM
1189 }
1190 }
f376e137 1191
db3626d1 1192 /* Even if we already had this variant, we update
834c6dff 1193 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
9f63daea
EC
1194 they changed since the variant was originally created.
1195
db3626d1
MM
1196 This seems hokey; if there is some way to use a previous
1197 variant *without* coming through here,
1198 TYPE_NEEDS_CONSTRUCTING will never be updated. */
9f63daea 1199 TYPE_NEEDS_CONSTRUCTING (t)
db3626d1 1200 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
9f63daea 1201 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
834c6dff 1202 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
db3626d1 1203 return t;
f376e137 1204 }
9a3c2683
JJ
1205 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1206 {
1207 tree t = PACK_EXPANSION_PATTERN (type);
1208
1209 t = cp_build_qualified_type_real (t, type_quals, complain);
d5f0b3f0 1210 return make_pack_expansion (t, complain);
9a3c2683 1211 }
9f63daea 1212
39a13be5 1213 /* A reference or method type shall not be cv-qualified.
93e1ddcf
JM
1214 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1215 (in CD1) we always ignore extra cv-quals on functions. */
4b011bbf
JM
1216 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1217 && (TREE_CODE (type) == REFERENCE_TYPE
2872152c 1218 || TREE_CODE (type) == FUNCTION_TYPE
4b011bbf
JM
1219 || TREE_CODE (type) == METHOD_TYPE))
1220 {
93e1ddcf
JM
1221 if (TREE_CODE (type) == REFERENCE_TYPE)
1222 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
4b011bbf
JM
1223 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1224 }
9f63daea 1225
2872152c
JM
1226 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1227 if (TREE_CODE (type) == FUNCTION_TYPE)
1228 type_quals |= type_memfn_quals (type);
1229
4b011bbf 1230 /* A restrict-qualified type must be a pointer (or reference)
0d9c0892 1231 to object or incomplete type. */
4b011bbf
JM
1232 if ((type_quals & TYPE_QUAL_RESTRICT)
1233 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1234 && TREE_CODE (type) != TYPENAME_TYPE
1235 && !POINTER_TYPE_P (type))
1236 {
1237 bad_quals |= TYPE_QUAL_RESTRICT;
1238 type_quals &= ~TYPE_QUAL_RESTRICT;
1239 }
1240
93e1ddcf
JM
1241 if (bad_quals == TYPE_UNQUALIFIED
1242 || (complain & tf_ignore_bad_quals))
4b011bbf 1243 /*OK*/;
93e1ddcf 1244 else if (!(complain & tf_error))
4b011bbf 1245 return error_mark_node;
4b011bbf
JM
1246 else
1247 {
93e1ddcf
JM
1248 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1249 error ("%qV qualifiers cannot be applied to %qT",
1250 bad_type, type);
4b011bbf 1251 }
9f63daea 1252
2adeacc9
MM
1253 /* Retrieve (or create) the appropriately qualified variant. */
1254 result = build_qualified_type (type, type_quals);
1255
2eed8e37
BK
1256 /* Preserve exception specs and ref-qualifier since build_qualified_type
1257 doesn't know about them. */
1258 if (TREE_CODE (result) == FUNCTION_TYPE
1259 || TREE_CODE (result) == METHOD_TYPE)
1260 {
1261 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1262 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1263 }
1264
2adeacc9 1265 return result;
f376e137 1266}
53929c47 1267
164247b0
JM
1268/* Return TYPE with const and volatile removed. */
1269
1270tree
1271cv_unqualified (tree type)
1272{
ea8b8aa0
JM
1273 int quals;
1274
1275 if (type == error_mark_node)
1276 return type;
1277
a3360e77 1278 quals = cp_type_quals (type);
164247b0
JM
1279 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1280 return cp_build_qualified_type (type, quals);
1281}
1282
b71983a5
JM
1283/* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1284 from ATTRIBS that affect type identity, and no others. If any are not
1285 applied, set *remove_attributes to true. */
1286
1287static tree
1288apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1289{
1290 tree first_ident = NULL_TREE;
1291 tree new_attribs = NULL_TREE;
1292 tree *p = &new_attribs;
1293
9f7fb685
JM
1294 if (OVERLOAD_TYPE_P (result))
1295 {
1296 /* On classes and enums all attributes are ingrained. */
1297 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1298 return result;
1299 }
1300
b71983a5
JM
1301 for (tree a = attribs; a; a = TREE_CHAIN (a))
1302 {
1303 const attribute_spec *as
1304 = lookup_attribute_spec (get_attribute_name (a));
1305 if (as && as->affects_type_identity)
1306 {
1307 if (!first_ident)
1308 first_ident = a;
1309 else if (first_ident == error_mark_node)
1310 {
1311 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1312 p = &TREE_CHAIN (*p);
1313 }
1314 }
1315 else if (first_ident)
1316 {
1317 for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1318 {
1319 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1320 p = &TREE_CHAIN (*p);
1321 }
1322 first_ident = error_mark_node;
1323 }
1324 }
1325 if (first_ident != error_mark_node)
1326 new_attribs = first_ident;
1327
1328 if (first_ident == attribs)
1329 /* All attributes affected type identity. */;
1330 else
1331 *remove_attributes = true;
1332
1333 return cp_build_type_attribute_variant (result, new_attribs);
1334}
1335
cd41d410
DS
1336/* Builds a qualified variant of T that is not a typedef variant.
1337 E.g. consider the following declarations:
1338 typedef const int ConstInt;
1339 typedef ConstInt* PtrConstInt;
1340 If T is PtrConstInt, this function returns a type representing
1341 const int*.
1342 In other words, if T is a typedef, the function returns the underlying type.
1343 The cv-qualification and attributes of the type returned match the
1344 input type.
1345 They will always be compatible types.
1346 The returned type is built so that all of its subtypes
1347 recursively have their typedefs stripped as well.
1348
1349 This is different from just returning TYPE_CANONICAL (T)
1350 Because of several reasons:
1351 * If T is a type that needs structural equality
1352 its TYPE_CANONICAL (T) will be NULL.
1353 * TYPE_CANONICAL (T) desn't carry type attributes
b71983a5
JM
1354 and loses template parameter names.
1355
1356 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1357 affect type identity, and set the referent to true if any were
1358 stripped. */
53929c47
JM
1359
1360tree
b71983a5 1361strip_typedefs (tree t, bool *remove_attributes)
53929c47 1362{
cd41d410
DS
1363 tree result = NULL, type = NULL, t0 = NULL;
1364
2bd8ca21 1365 if (!t || t == error_mark_node)
cd41d410
DS
1366 return t;
1367
2bd8ca21
JM
1368 if (TREE_CODE (t) == TREE_LIST)
1369 {
1370 bool changed = false;
1371 vec<tree,va_gc> *vec = make_tree_vector ();
8f56fadc 1372 tree r = t;
2bd8ca21
JM
1373 for (; t; t = TREE_CHAIN (t))
1374 {
1375 gcc_assert (!TREE_PURPOSE (t));
b71983a5 1376 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
2bd8ca21
JM
1377 if (elt != TREE_VALUE (t))
1378 changed = true;
1379 vec_safe_push (vec, elt);
1380 }
2bd8ca21
JM
1381 if (changed)
1382 r = build_tree_list_vec (vec);
1383 release_tree_vector (vec);
1384 return r;
1385 }
1386
cd41d410
DS
1387 gcc_assert (TYPE_P (t));
1388
2bd8ca21
JM
1389 if (t == TYPE_CANONICAL (t))
1390 return t;
1391
31cb2db0
JM
1392 if (dependent_alias_template_spec_p (t))
1393 /* DR 1558: However, if the template-id is dependent, subsequent
1394 template argument substitution still applies to the template-id. */
1395 return t;
1396
cd41d410
DS
1397 switch (TREE_CODE (t))
1398 {
1399 case POINTER_TYPE:
b71983a5 1400 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
cd41d410
DS
1401 result = build_pointer_type (type);
1402 break;
1403 case REFERENCE_TYPE:
b71983a5 1404 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
cd41d410
DS
1405 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1406 break;
1407 case OFFSET_TYPE:
b71983a5
JM
1408 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1409 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
cd41d410
DS
1410 result = build_offset_type (t0, type);
1411 break;
1412 case RECORD_TYPE:
1413 if (TYPE_PTRMEMFUNC_P (t))
1414 {
b71983a5 1415 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
cd41d410
DS
1416 result = build_ptrmemfunc_type (t0);
1417 }
1418 break;
1419 case ARRAY_TYPE:
b71983a5
JM
1420 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1421 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
cd41d410
DS
1422 result = build_cplus_array_type (type, t0);
1423 break;
1424 case FUNCTION_TYPE:
1425 case METHOD_TYPE:
1426 {
75a27d35
JJ
1427 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1428 bool changed;
1429
1430 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1431 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1432 can't expect that re-hashing a function type will find a previous
1433 equivalent type, so try to reuse the input type if nothing has
1434 changed. If the type is itself a variant, that will change. */
1435 bool is_variant = typedef_variant_p (t);
1436 if (remove_attributes
1437 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1438 is_variant = true;
1439
1440 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
802561b2
JM
1441 tree canon_spec = (flag_noexcept_type
1442 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1443 : NULL_TREE);
1444 changed = (type != TREE_TYPE (t) || is_variant
1445 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
75a27d35 1446
cd41d410
DS
1447 for (arg_node = TYPE_ARG_TYPES (t);
1448 arg_node;
1449 arg_node = TREE_CHAIN (arg_node))
1450 {
1451 if (arg_node == void_list_node)
1452 break;
b71983a5
JM
1453 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1454 remove_attributes);
cd41d410 1455 gcc_assert (arg_type);
75a27d35
JJ
1456 if (arg_type == TREE_VALUE (arg_node) && !changed)
1457 continue;
1458
1459 if (!changed)
1460 {
1461 changed = true;
1462 for (arg_node2 = TYPE_ARG_TYPES (t);
1463 arg_node2 != arg_node;
1464 arg_node2 = TREE_CHAIN (arg_node2))
1465 arg_types
1466 = tree_cons (TREE_PURPOSE (arg_node2),
1467 TREE_VALUE (arg_node2), arg_types);
1468 }
1469
1470 arg_types
1471 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
cd41d410
DS
1472 }
1473
75a27d35
JJ
1474 if (!changed)
1475 return t;
1476
cd41d410
DS
1477 if (arg_types)
1478 arg_types = nreverse (arg_types);
1479
1480 /* A list of parameters not ending with an ellipsis
1481 must end with void_list_node. */
1482 if (arg_node)
1483 arg_types = chainon (arg_types, void_list_node);
1484
cd41d410
DS
1485 if (TREE_CODE (t) == METHOD_TYPE)
1486 {
1487 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1488 gcc_assert (class_type);
1489 result =
1490 build_method_type_directly (class_type, type,
1491 TREE_CHAIN (arg_types));
f585f69b
JM
1492 result
1493 = build_ref_qualified_type (result, type_memfn_rqual (t));
cd41d410
DS
1494 }
1495 else
2872152c 1496 {
cd41d410
DS
1497 result = build_function_type (type,
1498 arg_types);
2eed8e37
BK
1499 result = apply_memfn_quals (result,
1500 type_memfn_quals (t),
1501 type_memfn_rqual (t));
2872152c 1502 }
3c3905fc 1503
802561b2
JM
1504 if (canon_spec)
1505 result = build_exception_variant (result, canon_spec);
cab421f4
PC
1506 if (TYPE_HAS_LATE_RETURN_TYPE (t))
1507 TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
cd41d410
DS
1508 }
1509 break;
e6c2fc5d 1510 case TYPENAME_TYPE:
5b5d851e 1511 {
cffc4a68 1512 bool changed = false;
5b5d851e 1513 tree fullname = TYPENAME_TYPE_FULLNAME (t);
9ebced77
JJ
1514 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1515 && TREE_OPERAND (fullname, 1))
5b5d851e
JM
1516 {
1517 tree args = TREE_OPERAND (fullname, 1);
1518 tree new_args = copy_node (args);
5b5d851e
JM
1519 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1520 {
1521 tree arg = TREE_VEC_ELT (args, i);
1522 tree strip_arg;
1523 if (TYPE_P (arg))
b71983a5 1524 strip_arg = strip_typedefs (arg, remove_attributes);
5b5d851e 1525 else
b71983a5 1526 strip_arg = strip_typedefs_expr (arg, remove_attributes);
5b5d851e
JM
1527 TREE_VEC_ELT (new_args, i) = strip_arg;
1528 if (strip_arg != arg)
1529 changed = true;
1530 }
1531 if (changed)
7349ed05
JJ
1532 {
1533 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1534 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1535 fullname
1536 = lookup_template_function (TREE_OPERAND (fullname, 0),
1537 new_args);
1538 }
5b5d851e
JM
1539 else
1540 ggc_free (new_args);
1541 }
cffc4a68
JM
1542 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes);
1543 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1544 return t;
1545 tree name = fullname;
1546 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1547 name = TREE_OPERAND (fullname, 0);
1548 /* Use build_typename_type rather than make_typename_type because we
1549 don't want to resolve it here, just strip typedefs. */
1550 result = build_typename_type (ctx, name, fullname, typename_type);
5b5d851e 1551 }
e6c2fc5d 1552 break;
49bb4bbe 1553 case DECLTYPE_TYPE:
b71983a5
JM
1554 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1555 remove_attributes);
49bb4bbe 1556 if (result == DECLTYPE_TYPE_EXPR (t))
8f56fadc 1557 result = NULL_TREE;
49bb4bbe
JM
1558 else
1559 result = (finish_decltype_type
1560 (result,
1561 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1562 tf_none));
1563 break;
be7c73ae
MP
1564 case UNDERLYING_TYPE:
1565 type = strip_typedefs (UNDERLYING_TYPE_TYPE (t), remove_attributes);
1566 result = finish_underlying_type (type);
1567 break;
cd41d410
DS
1568 default:
1569 break;
1570 }
1ad8aeeb 1571
cd41d410 1572 if (!result)
6284a979
JM
1573 {
1574 if (typedef_variant_p (t))
42c729c5
MP
1575 {
1576 /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1577 strip typedefs with attributes. */
1578 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1579 result = strip_typedefs (result);
1580 }
6284a979
JM
1581 else
1582 result = TYPE_MAIN_VARIANT (t);
1583 }
1584 gcc_assert (!typedef_variant_p (result));
423aec8b
MP
1585
1586 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1587 /* If RESULT is complete and T isn't, it's likely the case that T
1588 is a variant of RESULT which hasn't been updated yet. Skip the
1589 attribute handling. */;
1590 else
05322543 1591 {
423aec8b
MP
1592 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1593 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
b71983a5 1594 {
423aec8b
MP
1595 gcc_assert (TYPE_USER_ALIGN (t));
1596 if (remove_attributes)
1597 *remove_attributes = true;
b71983a5 1598 else
423aec8b
MP
1599 {
1600 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1601 result = build_variant_type_copy (result);
1602 else
1603 result = build_aligned_type (result, TYPE_ALIGN (t));
1604 TYPE_USER_ALIGN (result) = true;
1605 }
1606 }
1607
1608 if (TYPE_ATTRIBUTES (t))
1609 {
1610 if (remove_attributes)
1611 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1612 remove_attributes);
1613 else
1614 result = cp_build_type_attribute_variant (result,
1615 TYPE_ATTRIBUTES (t));
b71983a5 1616 }
05322543 1617 }
423aec8b 1618
cd41d410 1619 return cp_build_qualified_type (result, cp_type_quals (t));
53929c47 1620}
cd41d410 1621
49bb4bbe
JM
1622/* Like strip_typedefs above, but works on expressions, so that in
1623
1624 template<class T> struct A
1625 {
1626 typedef T TT;
1627 B<sizeof(TT)> b;
1628 };
1629
1630 sizeof(TT) is replaced by sizeof(T). */
1631
1632tree
b71983a5 1633strip_typedefs_expr (tree t, bool *remove_attributes)
49bb4bbe
JM
1634{
1635 unsigned i,n;
1636 tree r, type, *ops;
1637 enum tree_code code;
1638
1639 if (t == NULL_TREE || t == error_mark_node)
1640 return t;
1641
1642 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1643 return t;
1644
1645 /* Some expressions have type operands, so let's handle types here rather
1646 than check TYPE_P in multiple places below. */
1647 if (TYPE_P (t))
b71983a5 1648 return strip_typedefs (t, remove_attributes);
49bb4bbe
JM
1649
1650 code = TREE_CODE (t);
1651 switch (code)
1652 {
1653 case IDENTIFIER_NODE:
1654 case TEMPLATE_PARM_INDEX:
1655 case OVERLOAD:
1656 case BASELINK:
1657 case ARGUMENT_PACK_SELECT:
1658 return t;
1659
1660 case TRAIT_EXPR:
1661 {
b71983a5
JM
1662 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1663 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
49bb4bbe
JM
1664 if (type1 == TRAIT_EXPR_TYPE1 (t)
1665 && type2 == TRAIT_EXPR_TYPE2 (t))
1666 return t;
1667 r = copy_node (t);
8f56fadc
JM
1668 TRAIT_EXPR_TYPE1 (r) = type1;
1669 TRAIT_EXPR_TYPE2 (r) = type2;
49bb4bbe
JM
1670 return r;
1671 }
1672
1673 case TREE_LIST:
1674 {
9771b263 1675 vec<tree, va_gc> *vec = make_tree_vector ();
49bb4bbe
JM
1676 bool changed = false;
1677 tree it;
1678 for (it = t; it; it = TREE_CHAIN (it))
1679 {
b71983a5 1680 tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
9771b263 1681 vec_safe_push (vec, val);
49bb4bbe
JM
1682 if (val != TREE_VALUE (t))
1683 changed = true;
1684 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1685 }
1686 if (changed)
1687 {
1688 r = NULL_TREE;
9771b263 1689 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
49bb4bbe
JM
1690 r = tree_cons (NULL_TREE, it, r);
1691 }
1692 else
1693 r = t;
1694 release_tree_vector (vec);
1695 return r;
1696 }
1697
1698 case TREE_VEC:
1699 {
1700 bool changed = false;
9771b263 1701 vec<tree, va_gc> *vec = make_tree_vector ();
49bb4bbe 1702 n = TREE_VEC_LENGTH (t);
9771b263 1703 vec_safe_reserve (vec, n);
49bb4bbe
JM
1704 for (i = 0; i < n; ++i)
1705 {
b71983a5
JM
1706 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1707 remove_attributes);
9771b263 1708 vec->quick_push (op);
49bb4bbe
JM
1709 if (op != TREE_VEC_ELT (t, i))
1710 changed = true;
1711 }
1712 if (changed)
1713 {
1714 r = copy_node (t);
1715 for (i = 0; i < n; ++i)
9771b263 1716 TREE_VEC_ELT (r, i) = (*vec)[i];
7349ed05
JJ
1717 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1718 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
49bb4bbe
JM
1719 }
1720 else
1721 r = t;
1722 release_tree_vector (vec);
1723 return r;
1724 }
1725
1726 case CONSTRUCTOR:
1727 {
1728 bool changed = false;
9771b263
DN
1729 vec<constructor_elt, va_gc> *vec
1730 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
49bb4bbe 1731 n = CONSTRUCTOR_NELTS (t);
b71983a5 1732 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
49bb4bbe
JM
1733 for (i = 0; i < n; ++i)
1734 {
9771b263 1735 constructor_elt *e = &(*vec)[i];
b71983a5 1736 tree op = strip_typedefs_expr (e->value, remove_attributes);
49bb4bbe
JM
1737 if (op != e->value)
1738 {
1739 changed = true;
1740 e->value = op;
1741 }
b71983a5
JM
1742 gcc_checking_assert
1743 (e->index == strip_typedefs_expr (e->index, remove_attributes));
49bb4bbe
JM
1744 }
1745
1746 if (!changed && type == TREE_TYPE (t))
1747 {
9771b263 1748 vec_free (vec);
49bb4bbe
JM
1749 return t;
1750 }
1751 else
1752 {
1753 r = copy_node (t);
1754 TREE_TYPE (r) = type;
1755 CONSTRUCTOR_ELTS (r) = vec;
1756 return r;
1757 }
1758 }
1759
1760 case LAMBDA_EXPR:
8e519a8b
JM
1761 error ("lambda-expression in a constant expression");
1762 return error_mark_node;
49bb4bbe
JM
1763
1764 default:
1765 break;
1766 }
1767
1768 gcc_assert (EXPR_P (t));
1769
1770 n = TREE_OPERAND_LENGTH (t);
1771 ops = XALLOCAVEC (tree, n);
1772 type = TREE_TYPE (t);
1773
1774 switch (code)
1775 {
1776 CASE_CONVERT:
1777 case IMPLICIT_CONV_EXPR:
1778 case DYNAMIC_CAST_EXPR:
1779 case STATIC_CAST_EXPR:
1780 case CONST_CAST_EXPR:
1781 case REINTERPRET_CAST_EXPR:
1782 case CAST_EXPR:
1783 case NEW_EXPR:
b71983a5 1784 type = strip_typedefs (type, remove_attributes);
49bb4bbe
JM
1785 /* fallthrough */
1786
1787 default:
1788 for (i = 0; i < n; ++i)
b71983a5 1789 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
49bb4bbe
JM
1790 break;
1791 }
1792
1793 /* If nothing changed, return t. */
1794 for (i = 0; i < n; ++i)
1795 if (ops[i] != TREE_OPERAND (t, i))
1796 break;
1797 if (i == n && type == TREE_TYPE (t))
1798 return t;
1799
1800 r = copy_node (t);
1801 TREE_TYPE (r) = type;
1802 for (i = 0; i < n; ++i)
1803 TREE_OPERAND (r, i) = ops[i];
1804 return r;
1805}
1806
48b45647
NS
1807/* Makes a copy of BINFO and TYPE, which is to be inherited into a
1808 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1809 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1810 VIRT indicates whether TYPE is inherited virtually or not.
1811 IGO_PREV points at the previous binfo of the inheritance graph
1812 order chain. The newly copied binfo's TREE_CHAIN forms this
1813 ordering.
1814
1815 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1816 correct order. That is in the order the bases themselves should be
1817 constructed in.
dbbf88d1
NS
1818
1819 The BINFO_INHERITANCE of a virtual base class points to the binfo
48b45647
NS
1820 of the most derived type. ??? We could probably change this so that
1821 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1822 remove a field. They currently can only differ for primary virtual
1823 virtual bases. */
dbbf88d1
NS
1824
1825tree
48b45647 1826copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
9a71c18b 1827{
48b45647 1828 tree new_binfo;
9a71c18b 1829
48b45647
NS
1830 if (virt)
1831 {
1832 /* See if we've already made this virtual base. */
1833 new_binfo = binfo_for_vbase (type, t);
1834 if (new_binfo)
1835 return new_binfo;
1836 }
9f63daea 1837
fa743e8c 1838 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
48b45647 1839 BINFO_TYPE (new_binfo) = type;
9a71c18b 1840
48b45647
NS
1841 /* Chain it into the inheritance graph. */
1842 TREE_CHAIN (*igo_prev) = new_binfo;
1843 *igo_prev = new_binfo;
9f63daea 1844
05262294 1845 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
dfbcd65a 1846 {
fa743e8c
NS
1847 int ix;
1848 tree base_binfo;
9f63daea 1849
539ed333 1850 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
9f63daea 1851
48b45647
NS
1852 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1853 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
9f63daea 1854
fa743e8c
NS
1855 /* We do not need to copy the accesses, as they are read only. */
1856 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
9f63daea 1857
48b45647 1858 /* Recursively copy base binfos of BINFO. */
fa743e8c 1859 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
dbbf88d1 1860 {
48b45647 1861 tree new_base_binfo;
48b45647
NS
1862 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1863 t, igo_prev,
1864 BINFO_VIRTUAL_P (base_binfo));
9f63daea 1865
48b45647
NS
1866 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1867 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
fa743e8c 1868 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
dbbf88d1 1869 }
9a71c18b 1870 }
48b45647
NS
1871 else
1872 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
9f63daea 1873
48b45647
NS
1874 if (virt)
1875 {
1876 /* Push it onto the list after any virtual bases it contains
1877 will have been pushed. */
9771b263 1878 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
48b45647
NS
1879 BINFO_VIRTUAL_P (new_binfo) = 1;
1880 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1881 }
9f63daea 1882
48b45647 1883 return new_binfo;
9a71c18b 1884}
8d08fdba
MS
1885\f
1886/* Hashing of lists so that we don't make duplicates.
1887 The entry point is `list_hash_canon'. */
1888
9f63daea 1889struct list_proxy
9ccb25d5
MM
1890{
1891 tree purpose;
1892 tree value;
1893 tree chain;
1894};
1895
ca752f39 1896struct list_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
1897{
1898 typedef list_proxy *compare_type;
1899
1900 static hashval_t hash (tree);
1901 static bool equal (tree, list_proxy *);
1902};
1903
1904/* Now here is the hash table. When recording a list, it is added
1905 to the slot whose index is the hash code mod the table size.
1906 Note that the hash table is used for several kinds of lists.
1907 While all these live in the same table, they are completely independent,
1908 and the hash code is computed differently for each of these. */
1909
1910static GTY (()) hash_table<list_hasher> *list_hash_table;
1911
9ccb25d5
MM
1912/* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1913 for a node we are thinking about adding). */
1914
2a22f99c
TS
1915bool
1916list_hasher::equal (tree t, list_proxy *proxy)
9ccb25d5 1917{
9ccb25d5
MM
1918 return (TREE_VALUE (t) == proxy->value
1919 && TREE_PURPOSE (t) == proxy->purpose
1920 && TREE_CHAIN (t) == proxy->chain);
1921}
8d08fdba
MS
1922
1923/* Compute a hash code for a list (chain of TREE_LIST nodes
1924 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1925 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1926
9ccb25d5 1927static hashval_t
b57b79f7 1928list_hash_pieces (tree purpose, tree value, tree chain)
8d08fdba 1929{
9ccb25d5 1930 hashval_t hashcode = 0;
9f63daea 1931
37c46b43 1932 if (chain)
fd917e0d 1933 hashcode += TREE_HASH (chain);
9f63daea 1934
37c46b43 1935 if (value)
fd917e0d 1936 hashcode += TREE_HASH (value);
8d08fdba
MS
1937 else
1938 hashcode += 1007;
37c46b43 1939 if (purpose)
fd917e0d 1940 hashcode += TREE_HASH (purpose);
8d08fdba
MS
1941 else
1942 hashcode += 1009;
1943 return hashcode;
1944}
1945
9ccb25d5 1946/* Hash an already existing TREE_LIST. */
8d08fdba 1947
2a22f99c
TS
1948hashval_t
1949list_hasher::hash (tree t)
8d08fdba 1950{
9f63daea
EC
1951 return list_hash_pieces (TREE_PURPOSE (t),
1952 TREE_VALUE (t),
9ccb25d5 1953 TREE_CHAIN (t));
8d08fdba
MS
1954}
1955
51632249
JM
1956/* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1957 object for an identical list if one already exists. Otherwise, build a
1958 new one, and record it as the canonical object. */
8d08fdba 1959
8d08fdba 1960tree
b57b79f7 1961hash_tree_cons (tree purpose, tree value, tree chain)
8d08fdba 1962{
a703fb38 1963 int hashcode = 0;
2a22f99c 1964 tree *slot;
9ccb25d5
MM
1965 struct list_proxy proxy;
1966
1967 /* Hash the list node. */
1968 hashcode = list_hash_pieces (purpose, value, chain);
1969 /* Create a proxy for the TREE_LIST we would like to create. We
1970 don't actually create it so as to avoid creating garbage. */
1971 proxy.purpose = purpose;
1972 proxy.value = value;
1973 proxy.chain = chain;
1974 /* See if it is already in the table. */
2a22f99c 1975 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
9ccb25d5
MM
1976 /* If not, create a new node. */
1977 if (!*slot)
fad205ff 1978 *slot = tree_cons (purpose, value, chain);
67f5655f 1979 return (tree) *slot;
8d08fdba
MS
1980}
1981
1982/* Constructor for hashed lists. */
e92cc029 1983
8d08fdba 1984tree
b57b79f7 1985hash_tree_chain (tree value, tree chain)
8d08fdba 1986{
51632249 1987 return hash_tree_cons (NULL_TREE, value, chain);
8d08fdba 1988}
8d08fdba 1989\f
8d08fdba 1990void
b57b79f7 1991debug_binfo (tree elem)
8d08fdba 1992{
fed3cef0 1993 HOST_WIDE_INT n;
8d08fdba
MS
1994 tree virtuals;
1995
90ff44cf
KG
1996 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1997 "\nvtable type:\n",
1998 TYPE_NAME_STRING (BINFO_TYPE (elem)),
fed3cef0 1999 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
8d08fdba
MS
2000 debug_tree (BINFO_TYPE (elem));
2001 if (BINFO_VTABLE (elem))
fed3cef0 2002 fprintf (stderr, "vtable decl \"%s\"\n",
c35cce41 2003 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
8d08fdba
MS
2004 else
2005 fprintf (stderr, "no vtable decl yet\n");
2006 fprintf (stderr, "virtuals:\n");
da3d4dfa 2007 virtuals = BINFO_VIRTUALS (elem);
1f84ec23 2008 n = 0;
f30432d7 2009
8d08fdba
MS
2010 while (virtuals)
2011 {
83f2ccf4 2012 tree fndecl = TREE_VALUE (virtuals);
71e89f27 2013 fprintf (stderr, "%s [%ld =? %ld]\n",
8d08fdba 2014 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
71e89f27 2015 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
f30432d7 2016 ++n;
8d08fdba 2017 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
2018 }
2019}
2020
02ed62dd
MM
2021/* Build a representation for the qualified name SCOPE::NAME. TYPE is
2022 the type of the result expression, if known, or NULL_TREE if the
2023 resulting expression is type-dependent. If TEMPLATE_P is true,
2024 NAME is known to be a template because the user explicitly used the
3db45ab5 2025 "template" keyword after the "::".
02ed62dd
MM
2026
2027 All SCOPE_REFs should be built by use of this function. */
2028
2029tree
2030build_qualified_name (tree type, tree scope, tree name, bool template_p)
2031{
2032 tree t;
36569397
MM
2033 if (type == error_mark_node
2034 || scope == error_mark_node
2035 || name == error_mark_node)
2036 return error_mark_node;
88b811bd 2037 gcc_assert (TREE_CODE (name) != SCOPE_REF);
02ed62dd
MM
2038 t = build2 (SCOPE_REF, type, scope, name);
2039 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
d816a3ba 2040 PTRMEM_OK_P (t) = true;
7097b3ac
JM
2041 if (type)
2042 t = convert_from_reference (t);
02ed62dd
MM
2043 return t;
2044}
2045
2eed8e37
BK
2046/* Like check_qualified_type, but also check ref-qualifier and exception
2047 specification. */
2048
2049static bool
2050cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2051 cp_ref_qualifier rqual, tree raises)
2052{
1906d6b4
JM
2053 return (TYPE_QUALS (cand) == type_quals
2054 && check_base_type (cand, base)
2eed8e37
BK
2055 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2056 ce_exact)
2057 && type_memfn_rqual (cand) == rqual);
2058}
2059
2060/* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2061
2062tree
2063build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2064{
2065 tree t;
2066
2067 if (rqual == type_memfn_rqual (type))
2068 return type;
2069
2070 int type_quals = TYPE_QUALS (type);
2071 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2072 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2073 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
2074 return t;
2075
2076 t = build_variant_type_copy (type);
2077 switch (rqual)
2078 {
2079 case REF_QUAL_RVALUE:
2080 FUNCTION_RVALUE_QUALIFIED (t) = 1;
39bde5ea
JM
2081 FUNCTION_REF_QUALIFIED (t) = 1;
2082 break;
2eed8e37 2083 case REF_QUAL_LVALUE:
39bde5ea 2084 FUNCTION_RVALUE_QUALIFIED (t) = 0;
2eed8e37
BK
2085 FUNCTION_REF_QUALIFIED (t) = 1;
2086 break;
2087 default:
2088 FUNCTION_REF_QUALIFIED (t) = 0;
2089 break;
2090 }
2091
2092 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2093 /* Propagate structural equality. */
2094 SET_TYPE_STRUCTURAL_EQUALITY (t);
2095 else if (TYPE_CANONICAL (type) != type)
2096 /* Build the underlying canonical type, since it is different
2097 from TYPE. */
2098 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
2099 rqual);
2100 else
2101 /* T is its own canonical type. */
2102 TYPE_CANONICAL (t) = t;
2103
2104 return t;
2105}
2106
e09ae857
NS
2107/* Cache of free ovl nodes. Uses OVL_FUNCTION for chaining. */
2108static GTY((deletable)) tree ovl_cache;
2109
2110/* Make a raw overload node containing FN. */
2111
2112tree
2113ovl_make (tree fn, tree next)
2114{
2115 tree result = ovl_cache;
2116
2117 if (result)
2118 {
2119 ovl_cache = OVL_FUNCTION (result);
2120 /* Zap the flags. */
2121 memset (result, 0, sizeof (tree_base));
2122 TREE_SET_CODE (result, OVERLOAD);
2123 }
2124 else
2125 result = make_node (OVERLOAD);
2126
2127 if (TREE_CODE (fn) == OVERLOAD)
2128 OVL_NESTED_P (result) = true;
2129
2130 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2131 ? unknown_type_node : TREE_TYPE (fn));
2132 OVL_FUNCTION (result) = fn;
2133 OVL_CHAIN (result) = next;
2134 return result;
2135}
2136
1bf07cc3
NS
2137static tree
2138ovl_copy (tree ovl)
2139{
2140 tree result = ovl_cache;
2141
2142 if (result)
2143 {
2144 ovl_cache = OVL_FUNCTION (result);
2145 /* Zap the flags. */
2146 memset (result, 0, sizeof (tree_base));
2147 TREE_SET_CODE (result, OVERLOAD);
2148 }
2149 else
2150 result = make_node (OVERLOAD);
2151
724e517a 2152 gcc_checking_assert (!OVL_NESTED_P (ovl) && OVL_USED_P (ovl));
1bf07cc3
NS
2153 TREE_TYPE (result) = TREE_TYPE (ovl);
2154 OVL_FUNCTION (result) = OVL_FUNCTION (ovl);
2155 OVL_CHAIN (result) = OVL_CHAIN (ovl);
ef4c5e78
NS
2156 OVL_HIDDEN_P (result) = OVL_HIDDEN_P (ovl);
2157 OVL_USING_P (result) = OVL_USING_P (ovl);
724e517a 2158 OVL_LOOKUP_P (result) = OVL_LOOKUP_P (ovl);
1bf07cc3
NS
2159
2160 return result;
2161}
2162
36f4bc9c 2163/* Add FN to the (potentially NULL) overload set OVL. USING_P is
32196b87
NS
2164 true, if FN is via a using declaration. We also pay attention to
2165 DECL_HIDDEN. Overloads are ordered as hidden, using, regular. */
36f4bc9c
NS
2166
2167tree
2168ovl_insert (tree fn, tree maybe_ovl, bool using_p)
2169{
1bf07cc3 2170 bool copying = false; /* Checking use only. */
ef4c5e78
NS
2171 bool hidden_p = DECL_HIDDEN_P (fn);
2172 int weight = (hidden_p << 1) | (using_p << 0);
36f4bc9c
NS
2173
2174 tree result = NULL_TREE;
2175 tree insert_after = NULL_TREE;
2176
2177 /* Find insertion point. */
2178 while (maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
ef4c5e78
NS
2179 && (weight < ((OVL_HIDDEN_P (maybe_ovl) << 1)
2180 | (OVL_USING_P (maybe_ovl) << 0))))
36f4bc9c 2181 {
1bf07cc3 2182 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl)
a5cfa13a 2183 && (!copying || OVL_USED_P (maybe_ovl)));
1bf07cc3
NS
2184 if (OVL_USED_P (maybe_ovl))
2185 {
2186 copying = true;
2187 maybe_ovl = ovl_copy (maybe_ovl);
2188 if (insert_after)
2189 OVL_CHAIN (insert_after) = maybe_ovl;
2190 }
36f4bc9c
NS
2191 if (!result)
2192 result = maybe_ovl;
2193 insert_after = maybe_ovl;
2194 maybe_ovl = OVL_CHAIN (maybe_ovl);
2195 }
2196
2197 tree trail = fn;
ef4c5e78 2198 if (maybe_ovl || using_p || hidden_p || TREE_CODE (fn) == TEMPLATE_DECL)
36f4bc9c
NS
2199 {
2200 trail = ovl_make (fn, maybe_ovl);
ef4c5e78
NS
2201 if (hidden_p)
2202 OVL_HIDDEN_P (trail) = true;
36f4bc9c 2203 if (using_p)
2063b07f 2204 OVL_USING_P (trail) = true;
36f4bc9c
NS
2205 }
2206
2207 if (insert_after)
2208 {
1bf07cc3 2209 OVL_CHAIN (insert_after) = trail;
36f4bc9c
NS
2210 TREE_TYPE (insert_after) = unknown_type_node;
2211 }
2212 else
2213 result = trail;
2214
2215 return result;
2216}
2217
c0edbb32
NS
2218/* Skip any hidden names at the beginning of OVL. */
2219
2220tree
2221ovl_skip_hidden (tree ovl)
2222{
2223 for (;
2224 ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl);
2225 ovl = OVL_CHAIN (ovl))
2226 gcc_checking_assert (DECL_HIDDEN_P (OVL_FUNCTION (ovl)));
2227
2228 if (ovl && TREE_CODE (ovl) != OVERLOAD && DECL_HIDDEN_P (ovl))
2229 {
2230 /* Any hidden functions should have been wrapped in an
2231 overload, but injected friend classes will not. */
2232 gcc_checking_assert (!DECL_DECLARES_FUNCTION_P (ovl));
2233 ovl = NULL_TREE;
2234 }
2235
2236 return ovl;
2237}
2238
ef4c5e78
NS
2239/* NODE is an OVL_HIDDEN_P node which is now revealed. */
2240
2241tree
2242ovl_iterator::reveal_node (tree overload, tree node)
2243{
2244 /* We cannot have returned NODE as part of a lookup overload, so it
2245 cannot be USED. */
2246 gcc_checking_assert (!OVL_USED_P (node));
2247
2248 OVL_HIDDEN_P (node) = false;
2249 if (tree chain = OVL_CHAIN (node))
2250 if (TREE_CODE (chain) == OVERLOAD
2251 && (OVL_USING_P (chain) || OVL_HIDDEN_P (chain)))
2252 {
2253 /* The node needs moving, and the simplest way is to remove it
2254 and reinsert. */
2255 overload = remove_node (overload, node);
2256 overload = ovl_insert (OVL_FUNCTION (node), overload);
2257 }
2258 return overload;
2259}
2260
1bf07cc3
NS
2261/* NODE is on the overloads of OVL. Remove it. If a predecessor is
2262 OVL_USED_P we must copy OVL nodes, because those are immutable.
2263 The removed node is unaltered and may continue to be iterated
2264 from (i.e. it is safe to remove a node from an overload one is
2265 currently iterating over). */
36f4bc9c
NS
2266
2267tree
2268ovl_iterator::remove_node (tree overload, tree node)
2269{
1bf07cc3
NS
2270 bool copying = false; /* Checking use only. */
2271
36f4bc9c
NS
2272 tree *slot = &overload;
2273 while (*slot != node)
1bf07cc3
NS
2274 {
2275 tree probe = *slot;
2276 gcc_checking_assert (!OVL_LOOKUP_P (probe)
a5cfa13a 2277 && (!copying || OVL_USED_P (probe)));
1bf07cc3
NS
2278 if (OVL_USED_P (probe))
2279 {
2280 copying = true;
2281 probe = ovl_copy (probe);
2282 *slot = probe;
2283 }
2284
2285 slot = &OVL_CHAIN (probe);
2286 }
36f4bc9c
NS
2287
2288 /* Stitch out NODE. We don't have to worry about now making a
2289 singleton overload (and consequently maybe setting its type),
2290 because all uses of this function will be followed by inserting a
2291 new node that must follow the place we've cut this out from. */
6f2f4050
NS
2292 if (TREE_CODE (node) != OVERLOAD)
2293 /* Cloned inherited ctors don't mark themselves as via_using. */
2294 *slot = NULL_TREE;
2295 else
2296 *slot = OVL_CHAIN (node);
36f4bc9c
NS
2297
2298 return overload;
2299}
2300
32196b87
NS
2301/* Mark or unmark a lookup set. */
2302
2303void
2304lookup_mark (tree ovl, bool val)
2305{
3d7ff728 2306 for (lkp_iterator iter (ovl); iter; ++iter)
32196b87 2307 {
3d7ff728
NS
2308 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2309 LOOKUP_SEEN_P (*iter) = val;
32196b87
NS
2310 }
2311}
2312
31ab89c1 2313/* Add a set of new FNS into a lookup. */
e09ae857
NS
2314
2315tree
31ab89c1 2316lookup_add (tree fns, tree lookup)
e09ae857 2317{
31ab89c1 2318 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
e09ae857 2319 {
31ab89c1 2320 lookup = ovl_make (fns, lookup);
e09ae857
NS
2321 OVL_LOOKUP_P (lookup) = true;
2322 }
2323 else
31ab89c1 2324 lookup = fns;
e09ae857
NS
2325
2326 return lookup;
2327}
2328
3d7ff728
NS
2329/* FNS is a new overload set, add them to LOOKUP, if they are not
2330 already present there. */
32196b87
NS
2331
2332tree
3d7ff728 2333lookup_maybe_add (tree fns, tree lookup, bool deduping)
32196b87 2334{
3d7ff728
NS
2335 if (deduping)
2336 for (tree next, probe = fns; probe; probe = next)
2337 {
2338 tree fn = probe;
2339 next = NULL_TREE;
32196b87 2340
3d7ff728 2341 if (TREE_CODE (probe) == OVERLOAD)
32196b87 2342 {
3d7ff728
NS
2343 fn = OVL_FUNCTION (probe);
2344 next = OVL_CHAIN (probe);
32196b87 2345 }
32196b87 2346
3d7ff728
NS
2347 if (!LOOKUP_SEEN_P (fn))
2348 LOOKUP_SEEN_P (fn) = true;
2349 else
2350 {
2351 /* This function was already seen. Insert all the
2352 predecessors onto the lookup. */
2353 for (; fns != probe; fns = OVL_CHAIN (fns))
2354 {
2355 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2356 /* Propagate OVL_USING, but OVL_HIDDEN doesn't matter. */
2357 if (OVL_USING_P (fns))
2358 OVL_USING_P (lookup) = true;
2359 }
32196b87 2360
3d7ff728
NS
2361 /* And now skip this function. */
2362 fns = next;
2363 }
2364 }
32196b87 2365
3d7ff728
NS
2366 if (fns)
2367 /* We ended in a set of new functions. Add them all in one go. */
2368 lookup = lookup_add (fns, lookup);
2369
2370 return lookup;
32196b87
NS
2371}
2372
724e517a
NS
2373/* Regular overload OVL is part of a kept lookup. Mark the nodes on
2374 it as immutable. */
2375
2376static void
2377ovl_used (tree ovl)
2378{
2379 for (;
2380 ovl && TREE_CODE (ovl) == OVERLOAD
2381 && !OVL_USED_P (ovl);
2382 ovl = OVL_CHAIN (ovl))
2383 {
2384 gcc_checking_assert (!OVL_LOOKUP_P (ovl));
2385 OVL_USED_P (ovl) = true;
2386 }
2387}
2388
1bf07cc3
NS
2389/* If KEEP is true, preserve the contents of a lookup so that it is
2390 available for a later instantiation. Otherwise release the LOOKUP
2391 nodes for reuse. */
2392
2393void
2394lookup_keep (tree lookup, bool keep)
2395{
2396 for (;
2397 lookup && TREE_CODE (lookup) == OVERLOAD
2398 && OVL_LOOKUP_P (lookup) && !OVL_USED_P (lookup);
2399 lookup = OVL_CHAIN (lookup))
2400 if (keep)
724e517a
NS
2401 {
2402 OVL_USED_P (lookup) = true;
2403 ovl_used (OVL_FUNCTION (lookup));
2404 }
1bf07cc3
NS
2405 else
2406 {
2407 OVL_FUNCTION (lookup) = ovl_cache;
2408 ovl_cache = lookup;
2409 }
724e517a
NS
2410
2411 if (keep)
2412 ovl_used (lookup);
1bf07cc3
NS
2413}
2414
3b426391 2415/* Returns nonzero if X is an expression for a (possibly overloaded)
eff3a276
MM
2416 function. If "f" is a function or function template, "f", "c->f",
2417 "c.f", "C::f", and "f<int>" will all be considered possibly
2418 overloaded functions. Returns 2 if the function is actually
b9704fc5 2419 overloaded, i.e., if it is impossible to know the type of the
eff3a276
MM
2420 function without performing overload resolution. */
2421
8d08fdba 2422int
b57b79f7 2423is_overloaded_fn (tree x)
8d08fdba 2424{
4bb0968f 2425 /* A baselink is also considered an overloaded function. */
ccbe00a4
JM
2426 if (TREE_CODE (x) == OFFSET_REF
2427 || TREE_CODE (x) == COMPONENT_REF)
05e0b2f4 2428 x = TREE_OPERAND (x, 1);
d48b9bbe 2429 x = MAYBE_BASELINK_FUNCTIONS (x);
d095e03c
JM
2430 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2431 x = TREE_OPERAND (x, 0);
d48b9bbe
NS
2432
2433 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2434 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
eff3a276 2435 return 2;
d48b9bbe
NS
2436
2437 return (TREE_CODE (x) == FUNCTION_DECL
2438 || TREE_CODE (x) == OVERLOAD);
8d08fdba
MS
2439}
2440
f7d605ac
JM
2441/* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2442 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2443 NULL_TREE. */
2444
4b6aaa99 2445tree
f7d605ac
JM
2446dependent_name (tree x)
2447{
9dc6f476 2448 if (identifier_p (x))
f7d605ac 2449 return x;
d48b9bbe
NS
2450 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2451 x = TREE_OPERAND (x, 0);
2452 if (TREE_CODE (x) == OVERLOAD || TREE_CODE (x) == FUNCTION_DECL)
2453 return OVL_NAME (x);
f7d605ac
JM
2454 return NULL_TREE;
2455}
2456
eff3a276
MM
2457/* Returns true iff X is an expression for an overloaded function
2458 whose type cannot be known without performing overload
2459 resolution. */
2460
2461bool
b57b79f7 2462really_overloaded_fn (tree x)
9f63daea 2463{
eff3a276 2464 return is_overloaded_fn (x) == 2;
8926095f
MS
2465}
2466
1f0ed17c
NS
2467/* Get the overload set FROM refers to. */
2468
8d08fdba 2469tree
294e855f 2470get_fns (tree from)
8d08fdba 2471{
c6002625 2472 /* A baselink is also considered an overloaded function. */
7e361ae6
JM
2473 if (TREE_CODE (from) == OFFSET_REF
2474 || TREE_CODE (from) == COMPONENT_REF)
ccbe00a4 2475 from = TREE_OPERAND (from, 1);
4bb0968f 2476 if (BASELINK_P (from))
da15dae6 2477 from = BASELINK_FUNCTIONS (from);
d095e03c
JM
2478 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2479 from = TREE_OPERAND (from, 0);
1f0ed17c
NS
2480 gcc_assert (TREE_CODE (from) == OVERLOAD
2481 || TREE_CODE (from) == FUNCTION_DECL);
294e855f
JM
2482 return from;
2483}
2484
1f0ed17c
NS
2485/* Return the first function of the overload set FROM refers to. */
2486
294e855f
JM
2487tree
2488get_first_fn (tree from)
2489{
848bf88d 2490 return OVL_FIRST (get_fns (from));
2c73f9f5 2491}
8d08fdba 2492
aef3a6b2
JM
2493/* Return the scope where the overloaded functions OVL were found. */
2494
2495tree
2496ovl_scope (tree ovl)
2497{
2498 if (TREE_CODE (ovl) == OFFSET_REF
2499 || TREE_CODE (ovl) == COMPONENT_REF)
2500 ovl = TREE_OPERAND (ovl, 1);
2501 if (TREE_CODE (ovl) == BASELINK)
2502 return BINFO_TYPE (BASELINK_BINFO (ovl));
2503 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2504 ovl = TREE_OPERAND (ovl, 0);
2505 /* Skip using-declarations. */
6f2f4050
NS
2506 lkp_iterator iter (ovl);
2507 do
2508 ovl = *iter;
2509 while (iter.using_p () && ++iter);
2510
2511 return CP_DECL_CONTEXT (ovl);
aef3a6b2 2512}
8d08fdba
MS
2513\f
2514#define PRINT_RING_SIZE 4
2515
f41c4af3
JM
2516static const char *
2517cxx_printable_name_internal (tree decl, int v, bool translate)
8d08fdba 2518{
1bde0042 2519 static unsigned int uid_ring[PRINT_RING_SIZE];
8d08fdba 2520 static char *print_ring[PRINT_RING_SIZE];
f41c4af3 2521 static bool trans_ring[PRINT_RING_SIZE];
8d08fdba
MS
2522 static int ring_counter;
2523 int i;
2524
2525 /* Only cache functions. */
2ba25f50
MS
2526 if (v < 2
2527 || TREE_CODE (decl) != FUNCTION_DECL
8d08fdba 2528 || DECL_LANG_SPECIFIC (decl) == 0)
f41c4af3 2529 return lang_decl_name (decl, v, translate);
8d08fdba
MS
2530
2531 /* See if this print name is lying around. */
2532 for (i = 0; i < PRINT_RING_SIZE; i++)
f41c4af3 2533 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
8d08fdba
MS
2534 /* yes, so return it. */
2535 return print_ring[i];
2536
2537 if (++ring_counter == PRINT_RING_SIZE)
2538 ring_counter = 0;
2539
2540 if (current_function_decl != NULL_TREE)
2541 {
8fa6fa79
JM
2542 /* There may be both translated and untranslated versions of the
2543 name cached. */
2544 for (i = 0; i < 2; i++)
2545 {
2546 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2547 ring_counter += 1;
2548 if (ring_counter == PRINT_RING_SIZE)
2549 ring_counter = 0;
2550 }
1bde0042 2551 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
8d08fdba
MS
2552 }
2553
04695783 2554 free (print_ring[ring_counter]);
8d08fdba 2555
f41c4af3 2556 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1bde0042 2557 uid_ring[ring_counter] = DECL_UID (decl);
f41c4af3 2558 trans_ring[ring_counter] = translate;
8d08fdba
MS
2559 return print_ring[ring_counter];
2560}
f41c4af3
JM
2561
2562const char *
2563cxx_printable_name (tree decl, int v)
2564{
2565 return cxx_printable_name_internal (decl, v, false);
2566}
2567
2568const char *
2569cxx_printable_name_translate (tree decl, int v)
2570{
2571 return cxx_printable_name_internal (decl, v, true);
2572}
8d08fdba 2573\f
51dc6603
JM
2574/* Return the canonical version of exception-specification RAISES for a C++17
2575 function type, for use in type comparison and building TYPE_CANONICAL. */
2576
2577tree
2578canonical_eh_spec (tree raises)
2579{
2580 if (raises == NULL_TREE)
2581 return raises;
2582 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2583 || uses_template_parms (raises)
2584 || uses_template_parms (TREE_PURPOSE (raises)))
2585 /* Keep a dependent or deferred exception specification. */
2586 return raises;
2587 else if (nothrow_spec_p (raises))
2588 /* throw() -> noexcept. */
2589 return noexcept_true_spec;
2590 else
2591 /* For C++17 type matching, anything else -> nothing. */
2592 return NULL_TREE;
2593}
2594
f30432d7 2595/* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
8d08fdba 2596 listed in RAISES. */
e92cc029 2597
8d08fdba 2598tree
b57b79f7 2599build_exception_variant (tree type, tree raises)
8d08fdba 2600{
3a55fb4c
JM
2601 tree v;
2602 int type_quals;
8d08fdba 2603
3a55fb4c
JM
2604 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2605 return type;
2606
2607 type_quals = TYPE_QUALS (type);
2eed8e37 2608 cp_ref_qualifier rqual = type_memfn_rqual (type);
3a55fb4c 2609 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2eed8e37 2610 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
4cc1d462 2611 return v;
8d08fdba
MS
2612
2613 /* Need to build a new variant. */
8dd16ecc 2614 v = build_variant_type_copy (type);
8d08fdba 2615 TYPE_RAISES_EXCEPTIONS (v) = raises;
51dc6603
JM
2616
2617 if (!flag_noexcept_type)
2618 /* The exception-specification is not part of the canonical type. */
2619 return v;
2620
2621 /* Canonicalize the exception specification. */
2622 tree cr = canonical_eh_spec (raises);
2623
2624 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2625 /* Propagate structural equality. */
2626 SET_TYPE_STRUCTURAL_EQUALITY (v);
2627 else if (TYPE_CANONICAL (type) != type || cr != raises)
2628 /* Build the underlying canonical type, since it is different
2629 from TYPE. */
2630 TYPE_CANONICAL (v) = build_exception_variant (TYPE_CANONICAL (type), cr);
2631 else
2632 /* T is its own canonical type. */
2633 TYPE_CANONICAL (v) = v;
2634
8d08fdba
MS
2635 return v;
2636}
2637
dac65501
KL
2638/* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2639 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1899c3a4 2640 arguments. */
73b0fce8
KL
2641
2642tree
b57b79f7 2643bind_template_template_parm (tree t, tree newargs)
73b0fce8 2644{
1899c3a4 2645 tree decl = TYPE_NAME (t);
6b9b6b15
JM
2646 tree t2;
2647
9e1e64ec 2648 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
c2255bc4
AH
2649 decl = build_decl (input_location,
2650 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1899c3a4 2651
dac65501
KL
2652 /* These nodes have to be created to reflect new TYPE_DECL and template
2653 arguments. */
2654 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2655 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2656 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
aa373032 2657 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
6b9b6b15 2658
1899c3a4
KL
2659 TREE_TYPE (decl) = t2;
2660 TYPE_NAME (t2) = decl;
2661 TYPE_STUB_DECL (t2) = decl;
dac65501 2662 TYPE_SIZE (t2) = 0;
06d40de8 2663 SET_TYPE_STRUCTURAL_EQUALITY (t2);
73b0fce8 2664
73b0fce8
KL
2665 return t2;
2666}
2667
bf3428d0 2668/* Called from count_trees via walk_tree. */
297a5329
JM
2669
2670static tree
44de5aeb 2671count_trees_r (tree *tp, int *walk_subtrees, void *data)
297a5329 2672{
44de5aeb
RK
2673 ++*((int *) data);
2674
2675 if (TYPE_P (*tp))
2676 *walk_subtrees = 0;
2677
297a5329
JM
2678 return NULL_TREE;
2679}
2680
2681/* Debugging function for measuring the rough complexity of a tree
2682 representation. */
2683
2684int
b57b79f7 2685count_trees (tree t)
297a5329 2686{
bf3428d0 2687 int n_trees = 0;
14588106 2688 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
297a5329 2689 return n_trees;
9f63daea 2690}
297a5329 2691
b2244c65
MM
2692/* Called from verify_stmt_tree via walk_tree. */
2693
2694static tree
12308bc6 2695verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
b2244c65
MM
2696{
2697 tree t = *tp;
8d67ee55
RS
2698 hash_table<nofree_ptr_hash <tree_node> > *statements
2699 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
703c8606 2700 tree_node **slot;
b2244c65 2701
009ed910 2702 if (!STATEMENT_CODE_P (TREE_CODE (t)))
b2244c65
MM
2703 return NULL_TREE;
2704
2705 /* If this statement is already present in the hash table, then
2706 there is a circularity in the statement tree. */
703c8606 2707 gcc_assert (!statements->find (t));
9f63daea 2708
703c8606 2709 slot = statements->find_slot (t, INSERT);
b2244c65
MM
2710 *slot = t;
2711
2712 return NULL_TREE;
2713}
2714
2715/* Debugging function to check that the statement T has not been
2716 corrupted. For now, this function simply checks that T contains no
2717 circularities. */
2718
2719void
b57b79f7 2720verify_stmt_tree (tree t)
b2244c65 2721{
8d67ee55 2722 hash_table<nofree_ptr_hash <tree_node> > statements (37);
14588106 2723 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
b2244c65
MM
2724}
2725
50a6dbd7 2726/* Check if the type T depends on a type with no linkage and if so, return
4684cd27 2727 it. If RELAXED_P then do not consider a class type declared within
ecc607fc 2728 a vague-linkage function to have no linkage. */
50a6dbd7
JM
2729
2730tree
4684cd27 2731no_linkage_check (tree t, bool relaxed_p)
50a6dbd7 2732{
caf43ca4
MM
2733 tree r;
2734
2adeacc9
MM
2735 /* There's no point in checking linkage on template functions; we
2736 can't know their complete types. */
2737 if (processing_template_decl)
2738 return NULL_TREE;
2739
caf43ca4
MM
2740 switch (TREE_CODE (t))
2741 {
2742 case RECORD_TYPE:
2743 if (TYPE_PTRMEMFUNC_P (t))
2744 goto ptrmem;
e6d92cec 2745 /* Lambda types that don't have mangling scope have no linkage. We
c7335680 2746 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
e6d92cec
JM
2747 when we get here from pushtag none of the lambda information is
2748 set up yet, so we want to assume that the lambda has linkage and
2749 fix it up later if not. */
2750 if (CLASSTYPE_LAMBDA_EXPR (t)
c7335680 2751 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
e6d92cec
JM
2752 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2753 return t;
caf43ca4
MM
2754 /* Fall through. */
2755 case UNION_TYPE:
2756 if (!CLASS_TYPE_P (t))
2757 return NULL_TREE;
2758 /* Fall through. */
2759 case ENUMERAL_TYPE:
6a7b9203 2760 /* Only treat unnamed types as having no linkage if they're at
2f59d9e0 2761 namespace scope. This is core issue 966. */
6a7b9203 2762 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
caf43ca4 2763 return t;
ecc607fc 2764
e6d92cec 2765 for (r = CP_TYPE_CONTEXT (t); ; )
ecc607fc 2766 {
e6d92cec
JM
2767 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2768 have linkage, or we might just be in an anonymous namespace.
2769 If we're in a TREE_PUBLIC class, we have linkage. */
2770 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2771 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2772 else if (TREE_CODE (r) == FUNCTION_DECL)
2773 {
d6dcdbd5 2774 if (!relaxed_p || !vague_linkage_p (r))
e6d92cec
JM
2775 return t;
2776 else
2777 r = CP_DECL_CONTEXT (r);
2778 }
ecc607fc 2779 else
e6d92cec 2780 break;
ecc607fc
JM
2781 }
2782
caf43ca4
MM
2783 return NULL_TREE;
2784
2785 case ARRAY_TYPE:
2786 case POINTER_TYPE:
2787 case REFERENCE_TYPE:
404c2aea 2788 case VECTOR_TYPE:
4684cd27 2789 return no_linkage_check (TREE_TYPE (t), relaxed_p);
caf43ca4
MM
2790
2791 case OFFSET_TYPE:
2792 ptrmem:
4684cd27
MM
2793 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2794 relaxed_p);
caf43ca4
MM
2795 if (r)
2796 return r;
4684cd27 2797 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
caf43ca4
MM
2798
2799 case METHOD_TYPE:
caf43ca4
MM
2800 case FUNCTION_TYPE:
2801 {
d88511ae
JM
2802 tree parm = TYPE_ARG_TYPES (t);
2803 if (TREE_CODE (t) == METHOD_TYPE)
2804 /* The 'this' pointer isn't interesting; a method has the same
2805 linkage (or lack thereof) as its enclosing class. */
2806 parm = TREE_CHAIN (parm);
2807 for (;
9f63daea 2808 parm && parm != void_list_node;
caf43ca4
MM
2809 parm = TREE_CHAIN (parm))
2810 {
4684cd27 2811 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
caf43ca4
MM
2812 if (r)
2813 return r;
2814 }
4684cd27 2815 return no_linkage_check (TREE_TYPE (t), relaxed_p);
caf43ca4
MM
2816 }
2817
2818 default:
2819 return NULL_TREE;
2820 }
50a6dbd7
JM
2821}
2822
5566b478 2823extern int depth_reached;
5566b478 2824
8d08fdba 2825void
b57b79f7 2826cxx_print_statistics (void)
8d08fdba 2827{
8d08fdba 2828 print_class_statistics ();
7dcfe861 2829 print_template_statistics ();
7aa6d18a
SB
2830 if (GATHER_STATISTICS)
2831 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2832 depth_reached);
8d08fdba
MS
2833}
2834
e92cc029
MS
2835/* Return, as an INTEGER_CST node, the number of elements for TYPE
2836 (which is an ARRAY_TYPE). This counts only elements of the top
2837 array. */
8d08fdba
MS
2838
2839tree
b57b79f7 2840array_type_nelts_top (tree type)
8d08fdba 2841{
db3927fb
AH
2842 return fold_build2_loc (input_location,
2843 PLUS_EXPR, sizetype,
7866705a 2844 array_type_nelts (type),
701e903a 2845 size_one_node);
8d08fdba
MS
2846}
2847
e92cc029
MS
2848/* Return, as an INTEGER_CST node, the number of elements for TYPE
2849 (which is an ARRAY_TYPE). This one is a recursive count of all
2850 ARRAY_TYPEs that are clumped together. */
8d08fdba
MS
2851
2852tree
b57b79f7 2853array_type_nelts_total (tree type)
8d08fdba
MS
2854{
2855 tree sz = array_type_nelts_top (type);
2856 type = TREE_TYPE (type);
2857 while (TREE_CODE (type) == ARRAY_TYPE)
2858 {
2859 tree n = array_type_nelts_top (type);
db3927fb
AH
2860 sz = fold_build2_loc (input_location,
2861 MULT_EXPR, sizetype, sz, n);
8d08fdba
MS
2862 type = TREE_TYPE (type);
2863 }
2864 return sz;
2865}
878cd289 2866
b3ab27f3
MM
2867/* Called from break_out_target_exprs via mapcar. */
2868
2869static tree
b57b79f7 2870bot_manip (tree* tp, int* walk_subtrees, void* data)
878cd289 2871{
8dfaeb63
MM
2872 splay_tree target_remap = ((splay_tree) data);
2873 tree t = *tp;
2874
edb7c512 2875 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
8dfaeb63 2876 {
a4d25b44
JM
2877 /* There can't be any TARGET_EXPRs or their slot variables below this
2878 point. But we must make a copy, in case subsequent processing
2879 alters any part of it. For example, during gimplification a cast
2880 of the form (T) &X::f (where "f" is a member function) will lead
2881 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
8dfaeb63 2882 *walk_subtrees = 0;
a4d25b44 2883 *tp = unshare_expr (t);
8dfaeb63
MM
2884 return NULL_TREE;
2885 }
495d26d6 2886 if (TREE_CODE (t) == TARGET_EXPR)
73aad9b9 2887 {
b3ab27f3
MM
2888 tree u;
2889
02531345 2890 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
875bcfdb
JM
2891 {
2892 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2893 tf_warning_or_error);
2894 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2895 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2896 }
9f63daea 2897 else
574cfaa4
JM
2898 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2899 tf_warning_or_error);
b3ab27f3 2900
e08cc018
JM
2901 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2902 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2903 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2904
b3ab27f3 2905 /* Map the old variable to the new one. */
9f63daea
EC
2906 splay_tree_insert (target_remap,
2907 (splay_tree_key) TREE_OPERAND (t, 0),
b3ab27f3 2908 (splay_tree_value) TREE_OPERAND (u, 0));
8dfaeb63 2909
7efc22ea
JM
2910 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2911
8dfaeb63
MM
2912 /* Replace the old expression with the new version. */
2913 *tp = u;
2914 /* We don't have to go below this point; the recursive call to
2915 break_out_target_exprs will have handled anything below this
2916 point. */
2917 *walk_subtrees = 0;
2918 return NULL_TREE;
73aad9b9 2919 }
2ed4c029
JM
2920 if (TREE_CODE (*tp) == SAVE_EXPR)
2921 {
2922 t = *tp;
2923 splay_tree_node n = splay_tree_lookup (target_remap,
2924 (splay_tree_key) t);
2925 if (n)
2926 {
2927 *tp = (tree)n->value;
2928 *walk_subtrees = 0;
2929 }
2930 else
2931 {
2932 copy_tree_r (tp, walk_subtrees, NULL);
2933 splay_tree_insert (target_remap,
2934 (splay_tree_key)t,
2935 (splay_tree_value)*tp);
2936 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2937 splay_tree_insert (target_remap,
2938 (splay_tree_key)*tp,
2939 (splay_tree_value)*tp);
2940 }
2941 return NULL_TREE;
2942 }
73aad9b9 2943
8dfaeb63 2944 /* Make a copy of this node. */
5507a6c3
JM
2945 t = copy_tree_r (tp, walk_subtrees, NULL);
2946 if (TREE_CODE (*tp) == CALL_EXPR)
cfb1582c
JM
2947 {
2948 set_flags_from_callee (*tp);
2949
2950 /* builtin_LINE and builtin_FILE get the location where the default
2951 argument is expanded, not where the call was written. */
2952 tree callee = get_callee_fndecl (*tp);
ce209777 2953 if (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
cfb1582c
JM
2954 switch (DECL_FUNCTION_CODE (callee))
2955 {
2956 case BUILT_IN_FILE:
2957 case BUILT_IN_LINE:
2958 SET_EXPR_LOCATION (*tp, input_location);
083e891e
MP
2959 default:
2960 break;
cfb1582c
JM
2961 }
2962 }
5507a6c3 2963 return t;
878cd289 2964}
9f63daea 2965
8dfaeb63
MM
2966/* Replace all remapped VAR_DECLs in T with their new equivalents.
2967 DATA is really a splay-tree mapping old variables to new
2968 variables. */
b3ab27f3
MM
2969
2970static tree
12308bc6 2971bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
b3ab27f3 2972{
8dfaeb63
MM
2973 splay_tree target_remap = ((splay_tree) data);
2974
5a6ccc94 2975 if (VAR_P (*t))
b3ab27f3
MM
2976 {
2977 splay_tree_node n = splay_tree_lookup (target_remap,
2978 (splay_tree_key) *t);
2979 if (n)
2980 *t = (tree) n->value;
2981 }
382346e5 2982 else if (TREE_CODE (*t) == PARM_DECL
22c6ea00
JM
2983 && DECL_NAME (*t) == this_identifier
2984 && !DECL_CONTEXT (*t))
382346e5
JM
2985 {
2986 /* In an NSDMI we need to replace the 'this' parameter we used for
2987 parsing with the real one for this function. */
2988 *t = current_class_ptr;
2989 }
c65b0607
JM
2990 else if (TREE_CODE (*t) == CONVERT_EXPR
2991 && CONVERT_EXPR_VBASE_PATH (*t))
2992 {
2993 /* In an NSDMI build_base_path defers building conversions to virtual
2994 bases, and we handle it here. */
2995 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
9771b263 2996 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
c65b0607 2997 int i; tree binfo;
9771b263 2998 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
c65b0607
JM
2999 if (BINFO_TYPE (binfo) == basetype)
3000 break;
3001 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
3002 tf_warning_or_error);
3003 }
b3ab27f3
MM
3004
3005 return NULL_TREE;
3006}
9f63daea 3007
8dfaeb63
MM
3008/* When we parse a default argument expression, we may create
3009 temporary variables via TARGET_EXPRs. When we actually use the
a4d25b44
JM
3010 default-argument expression, we make a copy of the expression
3011 and replace the temporaries with appropriate local versions. */
e92cc029 3012
878cd289 3013tree
b57b79f7 3014break_out_target_exprs (tree t)
878cd289 3015{
8dfaeb63
MM
3016 static int target_remap_count;
3017 static splay_tree target_remap;
3018
b3ab27f3 3019 if (!target_remap_count++)
9f63daea
EC
3020 target_remap = splay_tree_new (splay_tree_compare_pointers,
3021 /*splay_tree_delete_key_fn=*/NULL,
b3ab27f3 3022 /*splay_tree_delete_value_fn=*/NULL);
14588106
RG
3023 cp_walk_tree (&t, bot_manip, target_remap, NULL);
3024 cp_walk_tree (&t, bot_replace, target_remap, NULL);
b3ab27f3
MM
3025
3026 if (!--target_remap_count)
3027 {
3028 splay_tree_delete (target_remap);
3029 target_remap = NULL;
3030 }
3031
3032 return t;
878cd289 3033}
f30432d7 3034
3e605b20
JM
3035/* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3036 which we expect to have type TYPE. */
3037
3038tree
3039build_ctor_subob_ref (tree index, tree type, tree obj)
3040{
3041 if (index == NULL_TREE)
3042 /* Can't refer to a particular member of a vector. */
3043 obj = NULL_TREE;
3044 else if (TREE_CODE (index) == INTEGER_CST)
3045 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3046 else
3047 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3048 /*reference*/false, tf_none);
3049 if (obj)
05dd97db
MS
3050 {
3051 tree objtype = TREE_TYPE (obj);
3052 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3053 {
3054 /* When the destination object refers to a flexible array member
3055 verify that it matches the type of the source object except
3b96b93a
MS
3056 for its domain and qualifiers. */
3057 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3058 TYPE_MAIN_VARIANT (objtype),
3059 COMPARE_REDECLARATION));
05dd97db
MS
3060 }
3061 else
3062 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3063 }
3064
3e605b20
JM
3065 return obj;
3066}
3067
817a77e4
JM
3068struct replace_placeholders_t
3069{
3070 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3071 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
2dc589be 3072 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
817a77e4
JM
3073};
3074
3e605b20
JM
3075/* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3076 build up subexpressions as we go deeper. */
3077
3e605b20
JM
3078static tree
3079replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3080{
817a77e4
JM
3081 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3082 tree obj = d->obj;
3e605b20
JM
3083
3084 if (TREE_CONSTANT (*t))
3085 {
3086 *walk_subtrees = false;
3087 return NULL_TREE;
3088 }
3089
3090 switch (TREE_CODE (*t))
3091 {
3092 case PLACEHOLDER_EXPR:
6a9263f7
JM
3093 {
3094 tree x = obj;
2dc589be
JJ
3095 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3096 TREE_TYPE (x));
6a9263f7
JM
3097 x = TREE_OPERAND (x, 0))
3098 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
3099 *t = x;
3100 *walk_subtrees = false;
817a77e4 3101 d->seen = true;
6a9263f7 3102 }
3e605b20
JM
3103 break;
3104
3105 case CONSTRUCTOR:
3106 {
3107 constructor_elt *ce;
3108 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3109 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3110 {
3111 tree *valp = &ce->value;
3112 tree type = TREE_TYPE (*valp);
3113 tree subob = obj;
3114
3115 if (TREE_CODE (*valp) == CONSTRUCTOR
3116 && AGGREGATE_TYPE_P (type))
3117 {
7599760d
JM
3118 /* If we're looking at the initializer for OBJ, then build
3119 a sub-object reference. If we're looking at an
3120 initializer for another object, just pass OBJ down. */
3121 if (same_type_ignoring_top_level_qualifiers_p
3122 (TREE_TYPE (*t), TREE_TYPE (obj)))
3123 subob = build_ctor_subob_ref (ce->index, type, obj);
3e605b20
JM
3124 if (TREE_CODE (*valp) == TARGET_EXPR)
3125 valp = &TARGET_EXPR_INITIAL (*valp);
3126 }
817a77e4 3127 d->obj = subob;
2dc589be 3128 cp_walk_tree (valp, replace_placeholders_r, data_, d->pset);
817a77e4 3129 d->obj = obj;
3e605b20
JM
3130 }
3131 *walk_subtrees = false;
3132 break;
3133 }
3134
3135 default:
3136 break;
3137 }
3138
3139 return NULL_TREE;
3140}
3141
2166aeb3
MP
3142/* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3143 a PLACEHOLDER_EXPR has been encountered. */
3144
3e605b20 3145tree
817a77e4 3146replace_placeholders (tree exp, tree obj, bool *seen_p)
3e605b20 3147{
2166aeb3
MP
3148 /* This is only relevant for C++14. */
3149 if (cxx_dialect < cxx14)
3150 return exp;
3151
3152 /* If the object isn't a (member of a) class, do nothing. */
3153 tree op0 = obj;
3154 while (TREE_CODE (op0) == COMPONENT_REF)
3155 op0 = TREE_OPERAND (op0, 0);
3156 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3157 return exp;
3158
3e605b20 3159 tree *tp = &exp;
2dc589be
JJ
3160 hash_set<tree> pset;
3161 replace_placeholders_t data = { obj, false, &pset };
3e605b20
JM
3162 if (TREE_CODE (exp) == TARGET_EXPR)
3163 tp = &TARGET_EXPR_INITIAL (exp);
2dc589be 3164 cp_walk_tree (tp, replace_placeholders_r, &data, &pset);
817a77e4
JM
3165 if (seen_p)
3166 *seen_p = data.seen;
3e605b20
JM
3167 return exp;
3168}
3169
8e1daa34
NS
3170/* Similar to `build_nt', but for template definitions of dependent
3171 expressions */
5566b478
MS
3172
3173tree
f330f599 3174build_min_nt_loc (location_t loc, enum tree_code code, ...)
5566b478 3175{
926ce8bd
KH
3176 tree t;
3177 int length;
3178 int i;
e34d07f2 3179 va_list p;
5566b478 3180
5039610b
SL
3181 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3182
e34d07f2 3183 va_start (p, code);
5566b478 3184
5566b478 3185 t = make_node (code);
f330f599 3186 SET_EXPR_LOCATION (t, loc);
8d5e6e25 3187 length = TREE_CODE_LENGTH (code);
5566b478
MS
3188
3189 for (i = 0; i < length; i++)
3190 {
3191 tree x = va_arg (p, tree);
2a1e9fdd 3192 TREE_OPERAND (t, i) = x;
58dec010
NS
3193 if (x && TREE_CODE (x) == OVERLOAD)
3194 lookup_keep (x, true);
5566b478
MS
3195 }
3196
e34d07f2 3197 va_end (p);
5566b478
MS
3198 return t;
3199}
3200
8e1daa34 3201/* Similar to `build', but for template definitions. */
5566b478
MS
3202
3203tree
e34d07f2 3204build_min (enum tree_code code, tree tt, ...)
5566b478 3205{
926ce8bd
KH
3206 tree t;
3207 int length;
3208 int i;
e34d07f2 3209 va_list p;
5566b478 3210
5039610b
SL
3211 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3212
e34d07f2 3213 va_start (p, tt);
5566b478 3214
5566b478 3215 t = make_node (code);
8d5e6e25 3216 length = TREE_CODE_LENGTH (code);
2a1e9fdd 3217 TREE_TYPE (t) = tt;
5566b478
MS
3218
3219 for (i = 0; i < length; i++)
3220 {
3221 tree x = va_arg (p, tree);
2a1e9fdd 3222 TREE_OPERAND (t, i) = x;
58dec010
NS
3223 if (x)
3224 {
3225 if (!TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3226 TREE_SIDE_EFFECTS (t) = 1;
3227 if (TREE_CODE (x) == OVERLOAD)
3228 lookup_keep (x, true);
3229 }
5566b478
MS
3230 }
3231
e34d07f2 3232 va_end (p);
ea9e71de
NS
3233
3234 if (code == CAST_EXPR)
3235 /* The single operand is a TREE_LIST, which we have to check. */
3236 for (tree v = TREE_OPERAND (t, 0); v; v = TREE_CHAIN (v))
3237 if (TREE_CODE (TREE_VALUE (v)) == OVERLOAD)
3238 lookup_keep (TREE_VALUE (v), true);
3239
5566b478
MS
3240 return t;
3241}
3242
8e1daa34
NS
3243/* Similar to `build', but for template definitions of non-dependent
3244 expressions. NON_DEP is the non-dependent expression that has been
3245 built. */
3246
3247tree
3248build_min_non_dep (enum tree_code code, tree non_dep, ...)
3249{
926ce8bd
KH
3250 tree t;
3251 int length;
3252 int i;
8e1daa34
NS
3253 va_list p;
3254
5039610b
SL
3255 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3256
8e1daa34
NS
3257 va_start (p, non_dep);
3258
e87b4dde
JM
3259 if (REFERENCE_REF_P (non_dep))
3260 non_dep = TREE_OPERAND (non_dep, 0);
3261
8e1daa34
NS
3262 t = make_node (code);
3263 length = TREE_CODE_LENGTH (code);
6a2cc46b 3264 TREE_TYPE (t) = unlowered_expr_type (non_dep);
8e1daa34
NS
3265 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3266
3267 for (i = 0; i < length; i++)
3268 {
3269 tree x = va_arg (p, tree);
3270 TREE_OPERAND (t, i) = x;
58dec010
NS
3271 if (x && TREE_CODE (x) == OVERLOAD)
3272 lookup_keep (x, true);
8e1daa34
NS
3273 }
3274
3275 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
3276 /* This should not be considered a COMPOUND_EXPR, because it
04c06002 3277 resolves to an overload. */
8e1daa34 3278 COMPOUND_EXPR_OVERLOADED (t) = 1;
9f63daea 3279
8e1daa34 3280 va_end (p);
e87b4dde 3281 return convert_from_reference (t);
8e1daa34
NS
3282}
3283
58dec010
NS
3284/* Similar to build_min_nt, but call expressions */
3285
3286tree
3287build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3288{
3289 tree ret, t;
3290 unsigned int ix;
3291
3292 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3293 CALL_EXPR_FN (ret) = fn;
3294 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3295 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3296 {
3297 CALL_EXPR_ARG (ret, ix) = t;
3298 if (TREE_CODE (t) == OVERLOAD)
3299 lookup_keep (t, true);
3300 }
3301 return ret;
3302}
3303
3304/* Similar to `build_min_nt_call_vec', but for template definitions of
3fcb9d1b
NF
3305 non-dependent expressions. NON_DEP is the non-dependent expression
3306 that has been built. */
5039610b
SL
3307
3308tree
9771b263 3309build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
5039610b 3310{
58dec010 3311 tree t = build_min_nt_call_vec (fn, argvec);
e87b4dde
JM
3312 if (REFERENCE_REF_P (non_dep))
3313 non_dep = TREE_OPERAND (non_dep, 0);
5039610b
SL
3314 TREE_TYPE (t) = TREE_TYPE (non_dep);
3315 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
e87b4dde 3316 return convert_from_reference (t);
5039610b
SL
3317}
3318
fcb9363e
PP
3319/* Similar to build_min_non_dep, but for expressions that have been resolved to
3320 a call to an operator overload. OP is the operator that has been
3321 overloaded. NON_DEP is the non-dependent expression that's been built,
3322 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3323 the overload that NON_DEP is calling. */
3324
3325tree
3326build_min_non_dep_op_overload (enum tree_code op,
3327 tree non_dep,
3328 tree overload, ...)
3329{
3330 va_list p;
3331 int nargs, expected_nargs;
3332 tree fn, call;
3333 vec<tree, va_gc> *args;
3334
4eb24e01 3335 non_dep = extract_call_expr (non_dep);
fcb9363e
PP
3336
3337 nargs = call_expr_nargs (non_dep);
3338
3339 expected_nargs = cp_tree_code_length (op);
e8b0383c
JJ
3340 if ((op == POSTINCREMENT_EXPR
3341 || op == POSTDECREMENT_EXPR)
3342 /* With -fpermissive non_dep could be operator++(). */
3343 && (!flag_permissive || nargs != expected_nargs))
fcb9363e
PP
3344 expected_nargs += 1;
3345 gcc_assert (nargs == expected_nargs);
3346
3347 args = make_tree_vector ();
3348 va_start (p, overload);
3349
3350 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3351 {
3352 fn = overload;
3353 for (int i = 0; i < nargs; i++)
3354 {
3355 tree arg = va_arg (p, tree);
3356 vec_safe_push (args, arg);
3357 }
3358 }
3359 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3360 {
3361 tree object = va_arg (p, tree);
3362 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3363 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3364 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3365 object, method, NULL_TREE);
3366 for (int i = 1; i < nargs; i++)
3367 {
3368 tree arg = va_arg (p, tree);
3369 vec_safe_push (args, arg);
3370 }
3371 }
3372 else
3373 gcc_unreachable ();
3374
3375 va_end (p);
3376 call = build_min_non_dep_call_vec (non_dep, fn, args);
3377 release_tree_vector (args);
3378
4eb24e01 3379 tree call_expr = extract_call_expr (call);
aa2500e9 3380 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
4eb24e01
JM
3381 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3382 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3383 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
aa2500e9 3384
fcb9363e
PP
3385 return call;
3386}
3387
af63ba4b
JM
3388/* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3389
3390vec<tree, va_gc> *
3391vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3392{
3393 unsigned len = vec_safe_length (old_vec);
3394 gcc_assert (idx <= len);
3395
3396 vec<tree, va_gc> *new_vec = NULL;
3397 vec_alloc (new_vec, len + 1);
3398
3399 unsigned i;
3400 for (i = 0; i < len; ++i)
3401 {
3402 if (i == idx)
3403 new_vec->quick_push (elt);
3404 new_vec->quick_push ((*old_vec)[i]);
3405 }
3406 if (i == idx)
3407 new_vec->quick_push (elt);
3408
3409 return new_vec;
3410}
3411
5566b478 3412tree
b57b79f7 3413get_type_decl (tree t)
5566b478 3414{
5566b478
MS
3415 if (TREE_CODE (t) == TYPE_DECL)
3416 return t;
2f939d94 3417 if (TYPE_P (t))
5566b478 3418 return TYPE_STUB_DECL (t);
315fb5db
NS
3419 gcc_assert (t == error_mark_node);
3420 return t;
5566b478
MS
3421}
3422
700466c2
JM
3423/* Returns the namespace that contains DECL, whether directly or
3424 indirectly. */
3425
3426tree
b57b79f7 3427decl_namespace_context (tree decl)
700466c2
JM
3428{
3429 while (1)
3430 {
3431 if (TREE_CODE (decl) == NAMESPACE_DECL)
3432 return decl;
3433 else if (TYPE_P (decl))
3434 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3435 else
3436 decl = CP_DECL_CONTEXT (decl);
3437 }
3438}
3439
b9e75696
JM
3440/* Returns true if decl is within an anonymous namespace, however deeply
3441 nested, or false otherwise. */
3442
3443bool
58f9752a 3444decl_anon_ns_mem_p (const_tree decl)
b9e75696 3445{
d48b9bbe 3446 while (TREE_CODE (decl) != NAMESPACE_DECL)
b9e75696 3447 {
d48b9bbe
NS
3448 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3449 if (TYPE_P (decl))
3450 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3451
3452 decl = CP_DECL_CONTEXT (decl);
b9e75696 3453 }
d48b9bbe 3454 return !TREE_PUBLIC (decl);
b9e75696
JM
3455}
3456
c873934c
JM
3457/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3458 CALL_EXPRS. Return whether they are equivalent. */
3459
3460static bool
3461called_fns_equal (tree t1, tree t2)
3462{
3463 /* Core 1321: dependent names are equivalent even if the overload sets
3464 are different. But do compare explicit template arguments. */
3465 tree name1 = dependent_name (t1);
3466 tree name2 = dependent_name (t2);
3467 if (name1 || name2)
3468 {
3469 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3470
3471 if (name1 != name2)
3472 return false;
3473
3474 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3475 targs1 = TREE_OPERAND (t1, 1);
3476 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3477 targs2 = TREE_OPERAND (t2, 1);
3478 return cp_tree_equal (targs1, targs2);
3479 }
3480 else
3481 return cp_tree_equal (t1, t2);
3482}
3483
67d743fe 3484/* Return truthvalue of whether T1 is the same tree structure as T2.
c8a209ca 3485 Return 1 if they are the same. Return 0 if they are different. */
67d743fe 3486
c8a209ca 3487bool
b57b79f7 3488cp_tree_equal (tree t1, tree t2)
67d743fe 3489{
926ce8bd 3490 enum tree_code code1, code2;
67d743fe
MS
3491
3492 if (t1 == t2)
c8a209ca
NS
3493 return true;
3494 if (!t1 || !t2)
3495 return false;
3496
ef796bef
JM
3497 code1 = TREE_CODE (t1);
3498 code2 = TREE_CODE (t2);
9f63daea 3499
67d743fe 3500 if (code1 != code2)
c8a209ca 3501 return false;
67d743fe 3502
6296cf8e
JM
3503 if (CONSTANT_CLASS_P (t1)
3504 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3505 return false;
3506
67d743fe
MS
3507 switch (code1)
3508 {
632f2871
RS
3509 case VOID_CST:
3510 /* There's only a single VOID_CST node, so we should never reach
3511 here. */
3512 gcc_unreachable ();
3513
67d743fe 3514 case INTEGER_CST:
807e902e 3515 return tree_int_cst_equal (t1, t2);
67d743fe
MS
3516
3517 case REAL_CST:
624d31fe 3518 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
67d743fe
MS
3519
3520 case STRING_CST:
3521 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
da61dec9 3522 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
c8a209ca 3523 TREE_STRING_LENGTH (t1));
67d743fe 3524
d05739f8
JM
3525 case FIXED_CST:
3526 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3527 TREE_FIXED_CST (t2));
3528
2a2193e0
SM
3529 case COMPLEX_CST:
3530 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3531 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3532
4eab75dd
MG
3533 case VECTOR_CST:
3534 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3535
67d743fe 3536 case CONSTRUCTOR:
7dd4bdf5
MM
3537 /* We need to do this when determining whether or not two
3538 non-type pointer to member function template arguments
3539 are the same. */
31d06664
JM
3540 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3541 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
c8a209ca 3542 return false;
31d06664
JM
3543 {
3544 tree field, value;
3545 unsigned int i;
3546 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3547 {
3548 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3549 if (!cp_tree_equal (field, elt2->index)
3550 || !cp_tree_equal (value, elt2->value))
3551 return false;
3552 }
3553 }
3554 return true;
7dd4bdf5
MM
3555
3556 case TREE_LIST:
c8a209ca
NS
3557 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3558 return false;
3559 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3560 return false;
7dd4bdf5 3561 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
67d743fe
MS
3562
3563 case SAVE_EXPR:
3564 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3565
3566 case CALL_EXPR:
5039610b
SL
3567 {
3568 tree arg1, arg2;
3569 call_expr_arg_iterator iter1, iter2;
c873934c 3570 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
5039610b
SL
3571 return false;
3572 for (arg1 = first_call_expr_arg (t1, &iter1),
3573 arg2 = first_call_expr_arg (t2, &iter2);
3574 arg1 && arg2;
3575 arg1 = next_call_expr_arg (&iter1),
3576 arg2 = next_call_expr_arg (&iter2))
3577 if (!cp_tree_equal (arg1, arg2))
3578 return false;
96b4a0b5
JM
3579 if (arg1 || arg2)
3580 return false;
3581 return true;
5039610b 3582 }
67d743fe 3583
c8a209ca
NS
3584 case TARGET_EXPR:
3585 {
3586 tree o1 = TREE_OPERAND (t1, 0);
3587 tree o2 = TREE_OPERAND (t2, 0);
9f63daea 3588
c8a209ca
NS
3589 /* Special case: if either target is an unallocated VAR_DECL,
3590 it means that it's going to be unified with whatever the
3591 TARGET_EXPR is really supposed to initialize, so treat it
3592 as being equivalent to anything. */
5a6ccc94 3593 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
c8a209ca
NS
3594 && !DECL_RTL_SET_P (o1))
3595 /*Nop*/;
5a6ccc94 3596 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
c8a209ca
NS
3597 && !DECL_RTL_SET_P (o2))
3598 /*Nop*/;
3599 else if (!cp_tree_equal (o1, o2))
3600 return false;
9f63daea 3601
c8a209ca
NS
3602 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3603 }
9f63daea 3604
67d743fe 3605 case PARM_DECL:
a77f94e2 3606 /* For comparing uses of parameters in late-specified return types
e7dc5734
JM
3607 with an out-of-class definition of the function, but can also come
3608 up for expressions that involve 'this' in a member function
3609 template. */
25976b7f 3610
971e17ff 3611 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
25976b7f
JM
3612 /* When comparing hash table entries, only an exact match is
3613 good enough; we don't want to replace 'this' with the
971e17ff
AS
3614 version from another function. But be more flexible
3615 with local parameters in a requires-expression. */
25976b7f
JM
3616 return false;
3617
e7dc5734
JM
3618 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3619 {
3620 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3621 return false;
971e17ff
AS
3622 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3623 return false;
e7dc5734
JM
3624 if (DECL_ARTIFICIAL (t1)
3625 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3626 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3627 return true;
3628 }
3629 return false;
a77f94e2
JM
3630
3631 case VAR_DECL:
67d743fe 3632 case CONST_DECL:
9b2770f2 3633 case FIELD_DECL:
67d743fe 3634 case FUNCTION_DECL:
c8a209ca
NS
3635 case TEMPLATE_DECL:
3636 case IDENTIFIER_NODE:
47c0c7d7 3637 case SSA_NAME:
c8a209ca 3638 return false;
67d743fe 3639
17a27b4f
MM
3640 case BASELINK:
3641 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3642 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4643a68e 3643 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
17a27b4f
MM
3644 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3645 BASELINK_FUNCTIONS (t2)));
3646
f84b4be9 3647 case TEMPLATE_PARM_INDEX:
31758337
NS
3648 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3649 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
9524f710
LE
3650 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3651 == TEMPLATE_PARM_PARAMETER_PACK (t2))
31758337
NS
3652 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3653 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
67d743fe 3654
bf12d54d 3655 case TEMPLATE_ID_EXPR:
c873934c
JM
3656 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3657 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3658
971e17ff
AS
3659 case CONSTRAINT_INFO:
3660 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3661 CI_ASSOCIATED_CONSTRAINTS (t2));
3662
f078dc7d
AS
3663 case CHECK_CONSTR:
3664 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3665 && comp_template_args (CHECK_CONSTR_ARGS (t1),
3666 CHECK_CONSTR_ARGS (t2)));
3667
c873934c 3668 case TREE_VEC:
bf12d54d
NS
3669 {
3670 unsigned ix;
c873934c 3671 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
bf12d54d 3672 return false;
c873934c
JM
3673 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3674 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3675 TREE_VEC_ELT (t2, ix)))
bf12d54d 3676 return false;
bf12d54d
NS
3677 return true;
3678 }
9f63daea 3679
67d743fe 3680 case SIZEOF_EXPR:
abff8e06 3681 case ALIGNOF_EXPR:
c8a209ca
NS
3682 {
3683 tree o1 = TREE_OPERAND (t1, 0);
3684 tree o2 = TREE_OPERAND (t2, 0);
9f63daea 3685
ba29e5c2
JJ
3686 if (code1 == SIZEOF_EXPR)
3687 {
3688 if (SIZEOF_EXPR_TYPE_P (t1))
3689 o1 = TREE_TYPE (o1);
3690 if (SIZEOF_EXPR_TYPE_P (t2))
3691 o2 = TREE_TYPE (o2);
3692 }
c8a209ca
NS
3693 if (TREE_CODE (o1) != TREE_CODE (o2))
3694 return false;
3695 if (TYPE_P (o1))
3696 return same_type_p (o1, o2);
3697 else
3698 return cp_tree_equal (o1, o2);
3699 }
9f63daea 3700
6f9f76e3
SM
3701 case MODOP_EXPR:
3702 {
3703 tree t1_op1, t2_op1;
3704
3705 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3706 return false;
3707
3708 t1_op1 = TREE_OPERAND (t1, 1);
3709 t2_op1 = TREE_OPERAND (t2, 1);
3710 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3711 return false;
3712
3713 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3714 }
3715
61a127b3
MM
3716 case PTRMEM_CST:
3717 /* Two pointer-to-members are the same if they point to the same
3718 field or function in the same class. */
c8a209ca
NS
3719 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3720 return false;
3721
3722 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
61a127b3 3723
943e3ede 3724 case OVERLOAD:
a736411a
NS
3725 {
3726 /* Two overloads. Must be exactly the same set of decls. */
3727 lkp_iterator first (t1);
3728 lkp_iterator second (t2);
3729
3730 for (; first && second; ++first, ++second)
3731 if (*first != *second)
3732 return false;
3733 return !(first || second);
3734 }
943e3ede 3735
ea798d0f
PC
3736 case TRAIT_EXPR:
3737 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3738 return false;
3739 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
cf5986df 3740 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
ea798d0f 3741
ab73eba8
JM
3742 case CAST_EXPR:
3743 case STATIC_CAST_EXPR:
3744 case REINTERPRET_CAST_EXPR:
3745 case CONST_CAST_EXPR:
3746 case DYNAMIC_CAST_EXPR:
a4474a38 3747 case IMPLICIT_CONV_EXPR:
ab73eba8 3748 case NEW_EXPR:
ef796bef
JM
3749 CASE_CONVERT:
3750 case NON_LVALUE_EXPR:
3751 case VIEW_CONVERT_EXPR:
ab73eba8
JM
3752 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3753 return false;
3754 /* Now compare operands as usual. */
3755 break;
3756
10261728
JM
3757 case DEFERRED_NOEXCEPT:
3758 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3759 DEFERRED_NOEXCEPT_PATTERN (t2))
3760 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3761 DEFERRED_NOEXCEPT_ARGS (t2)));
3762 break;
3763
7f85441b
KG
3764 default:
3765 break;
67d743fe
MS
3766 }
3767
3768 switch (TREE_CODE_CLASS (code1))
3769 {
6615c446
JO
3770 case tcc_unary:
3771 case tcc_binary:
3772 case tcc_comparison:
3773 case tcc_expression:
5039610b 3774 case tcc_vl_exp:
6615c446
JO
3775 case tcc_reference:
3776 case tcc_statement:
aa1826e2 3777 {
5039610b
SL
3778 int i, n;
3779
d26e5986 3780 n = cp_tree_operand_length (t1);
5039610b
SL
3781 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3782 && n != TREE_OPERAND_LENGTH (t2))
3783 return false;
9f63daea 3784
5039610b 3785 for (i = 0; i < n; ++i)
c8a209ca
NS
3786 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3787 return false;
9f63daea 3788
c8a209ca 3789 return true;
aa1826e2 3790 }
9f63daea 3791
6615c446 3792 case tcc_type:
c8a209ca 3793 return same_type_p (t1, t2);
6615c446
JO
3794 default:
3795 gcc_unreachable ();
67d743fe 3796 }
6615c446 3797 /* We can get here with --disable-checking. */
c8a209ca 3798 return false;
67d743fe 3799}
73aad9b9 3800
d11ad92e
MS
3801/* The type of ARG when used as an lvalue. */
3802
3803tree
b57b79f7 3804lvalue_type (tree arg)
d11ad92e 3805{
2c73f9f5 3806 tree type = TREE_TYPE (arg);
8cd4c175 3807 return type;
d11ad92e
MS
3808}
3809
3810/* The type of ARG for printing error messages; denote lvalues with
3811 reference types. */
3812
3813tree
b57b79f7 3814error_type (tree arg)
d11ad92e
MS
3815{
3816 tree type = TREE_TYPE (arg);
9f63daea 3817
d11ad92e
MS
3818 if (TREE_CODE (type) == ARRAY_TYPE)
3819 ;
08476342
NS
3820 else if (TREE_CODE (type) == ERROR_MARK)
3821 ;
72b3e203 3822 else if (lvalue_p (arg))
d11ad92e 3823 type = build_reference_type (lvalue_type (arg));
9e1e64ec 3824 else if (MAYBE_CLASS_TYPE_P (type))
d11ad92e
MS
3825 type = lvalue_type (arg);
3826
3827 return type;
3828}
eb66be0e
MS
3829
3830/* Does FUNCTION use a variable-length argument list? */
3831
3832int
58f9752a 3833varargs_function_p (const_tree function)
eb66be0e 3834{
f38958e8 3835 return stdarg_p (TREE_TYPE (function));
eb66be0e 3836}
f94ae2f5
JM
3837
3838/* Returns 1 if decl is a member of a class. */
3839
3840int
58f9752a 3841member_p (const_tree decl)
f94ae2f5 3842{
58f9752a 3843 const_tree const ctx = DECL_CONTEXT (decl);
2f939d94 3844 return (ctx && TYPE_P (ctx));
f94ae2f5 3845}
51924768
JM
3846
3847/* Create a placeholder for member access where we don't actually have an
3848 object that the access is against. */
3849
3850tree
b57b79f7 3851build_dummy_object (tree type)
51924768 3852{
32f3d032 3853 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
04757a2a 3854 return cp_build_fold_indirect_ref (decl);
51924768
JM
3855}
3856
3857/* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3858 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3859 binfo path from current_class_type to TYPE, or 0. */
3860
3861tree
b57b79f7 3862maybe_dummy_object (tree type, tree* binfop)
51924768
JM
3863{
3864 tree decl, context;
2db1ab2d 3865 tree binfo;
a6846853 3866 tree current = current_nonlambda_class_type ();
9f63daea 3867
a6846853 3868 if (current
22854930
PC
3869 && (binfo = lookup_base (current, type, ba_any, NULL,
3870 tf_warning_or_error)))
a6846853 3871 context = current;
51924768
JM
3872 else
3873 {
3874 /* Reference from a nested class member function. */
3875 context = type;
2db1ab2d 3876 binfo = TYPE_BINFO (type);
51924768
JM
3877 }
3878
2db1ab2d
NS
3879 if (binfop)
3880 *binfop = binfo;
9f63daea 3881
41d04a8d
JM
3882 if (current_class_ref
3883 /* current_class_ref might not correspond to current_class_type if
3884 we're in tsubst_default_argument or a lambda-declarator; in either
3885 case, we want to use current_class_ref if it matches CONTEXT. */
3886 && (same_type_ignoring_top_level_qualifiers_p
3887 (TREE_TYPE (current_class_ref), context)))
51924768
JM
3888 decl = current_class_ref;
3889 else
3890 decl = build_dummy_object (context);
3891
3892 return decl;
3893}
3894
3895/* Returns 1 if OB is a placeholder object, or a pointer to one. */
3896
3897int
58f9752a 3898is_dummy_object (const_tree ob)
51924768 3899{
591cb3cf 3900 if (INDIRECT_REF_P (ob))
51924768 3901 ob = TREE_OPERAND (ob, 0);
32f3d032 3902 return (TREE_CODE (ob) == CONVERT_EXPR
632f2871 3903 && TREE_OPERAND (ob, 0) == void_node);
51924768 3904}
5524676d 3905
c32097d8
JM
3906/* Returns 1 iff type T is something we want to treat as a scalar type for
3907 the purpose of deciding whether it is trivial/POD/standard-layout. */
3908
11f35925 3909bool
c32097d8
JM
3910scalarish_type_p (const_tree t)
3911{
3912 if (t == error_mark_node)
3913 return 1;
3914
b55b02ea 3915 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
c32097d8
JM
3916}
3917
3918/* Returns true iff T requires non-trivial default initialization. */
3919
3920bool
3921type_has_nontrivial_default_init (const_tree t)
3922{
3923 t = strip_array_types (CONST_CAST_TREE (t));
3924
3925 if (CLASS_TYPE_P (t))
3926 return TYPE_HAS_COMPLEX_DFLT (t);
3927 else
3928 return 0;
3929}
3930
f3ec182d
JM
3931/* Track classes with only deleted copy/move constructors so that we can warn
3932 if they are used in call/return by value. */
3933
3934static GTY(()) hash_set<tree>* deleted_copy_types;
3935static void
3936remember_deleted_copy (const_tree t)
3937{
3938 if (!deleted_copy_types)
3939 deleted_copy_types = hash_set<tree>::create_ggc(37);
3940 deleted_copy_types->add (CONST_CAST_TREE (t));
3941}
3942void
3943maybe_warn_parm_abi (tree t, location_t loc)
3944{
3945 if (!deleted_copy_types
3946 || !deleted_copy_types->contains (t))
3947 return;
3948
3949 warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
3950 "-fabi-version=12 (GCC 8)", t);
3951 static bool explained = false;
3952 if (!explained)
3953 {
3954 inform (loc, " because all of its copy and move constructors "
3955 "are deleted");
3956 explained = true;
3957 }
3958}
3959
d758e847
JM
3960/* Returns true iff copying an object of type T (including via move
3961 constructor) is non-trivial. That is, T has no non-trivial copy
f3ec182d
JM
3962 constructors and no non-trivial move constructors, and not all copy/move
3963 constructors are deleted. This function implements the ABI notion of
3964 non-trivial copy, which has diverged from the one in the standard. */
c32097d8
JM
3965
3966bool
f3ec182d 3967type_has_nontrivial_copy_init (const_tree type)
c32097d8 3968{
f3ec182d 3969 tree t = strip_array_types (CONST_CAST_TREE (type));
c32097d8
JM
3970
3971 if (CLASS_TYPE_P (t))
d758e847
JM
3972 {
3973 gcc_assert (COMPLETE_TYPE_P (t));
f3ec182d
JM
3974
3975 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
3976 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
3977 /* Nontrivial. */
3978 return true;
3979
3980 if (cxx_dialect < cxx11)
3981 /* No deleted functions before C++11. */
3982 return false;
3983
3984 /* Before ABI v12 we did a bitwise copy of types with only deleted
3985 copy/move constructors. */
3986 if (!abi_version_at_least (12)
3987 && !(warn_abi && abi_version_crosses (12)))
3988 return false;
3989
3990 bool saw_copy = false;
3991 bool saw_non_deleted = false;
3992
3993 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
3994 saw_copy = saw_non_deleted = true;
3995 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
3996 {
3997 saw_copy = true;
4a18c066 3998 if (classtype_has_move_assign_or_move_ctor_p (t, true))
f3ec182d
JM
3999 /* [class.copy]/8 If the class definition declares a move
4000 constructor or move assignment operator, the implicitly declared
4001 copy constructor is defined as deleted.... */;
4002 else
4003 /* Any other reason the implicitly-declared function would be
4004 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4005 set. */
4006 saw_non_deleted = true;
4007 }
4008
527b7b19 4009 if (!saw_non_deleted)
a736411a 4010 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
f3ec182d 4011 {
a736411a 4012 tree fn = *iter;
f3ec182d
JM
4013 if (copy_fn_p (fn))
4014 {
4015 saw_copy = true;
4016 if (!DECL_DELETED_FN (fn))
4017 {
4018 /* Not deleted, therefore trivial. */
4019 saw_non_deleted = true;
4020 break;
4021 }
4022 }
4023 }
4024
4025 gcc_assert (saw_copy);
4026
4027 if (saw_copy && !saw_non_deleted)
4028 {
4029 if (warn_abi && abi_version_crosses (12))
4030 remember_deleted_copy (t);
4031 if (abi_version_at_least (12))
4032 return true;
4033 }
4034
4035 return false;
d758e847 4036 }
c32097d8
JM
4037 else
4038 return 0;
4039}
4040
46408846
JM
4041/* Returns 1 iff type T is a trivially copyable type, as defined in
4042 [basic.types] and [class]. */
c32097d8
JM
4043
4044bool
46408846 4045trivially_copyable_p (const_tree t)
c32097d8
JM
4046{
4047 t = strip_array_types (CONST_CAST_TREE (t));
4048
4049 if (CLASS_TYPE_P (t))
d758e847
JM
4050 return ((!TYPE_HAS_COPY_CTOR (t)
4051 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4052 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4053 && (!TYPE_HAS_COPY_ASSIGN (t)
4054 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4055 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
334738b4 4056 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
c32097d8 4057 else
75bda2e8 4058 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
c32097d8
JM
4059}
4060
46408846
JM
4061/* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4062 [class]. */
4063
4064bool
4065trivial_type_p (const_tree t)
4066{
4067 t = strip_array_types (CONST_CAST_TREE (t));
4068
4069 if (CLASS_TYPE_P (t))
4070 return (TYPE_HAS_TRIVIAL_DFLT (t)
4071 && trivially_copyable_p (t));
4072 else
4073 return scalarish_type_p (t);
4074}
4075
5524676d
JM
4076/* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4077
c32097d8 4078bool
58f9752a 4079pod_type_p (const_tree t)
5524676d 4080{
4e9b57fa 4081 /* This CONST_CAST is okay because strip_array_types returns its
75547801 4082 argument unmodified and we assign it to a const_tree. */
b1d5455a 4083 t = strip_array_types (CONST_CAST_TREE(t));
5524676d 4084
cc72bbaa
JM
4085 if (!CLASS_TYPE_P (t))
4086 return scalarish_type_p (t);
4087 else if (cxx_dialect > cxx98)
c32097d8
JM
4088 /* [class]/10: A POD struct is a class that is both a trivial class and a
4089 standard-layout class, and has no non-static data members of type
4090 non-POD struct, non-POD union (or array of such types).
4091
4092 We don't need to check individual members because if a member is
4093 non-std-layout or non-trivial, the class will be too. */
4094 return (std_layout_type_p (t) && trivial_type_p (t));
4095 else
cc72bbaa
JM
4096 /* The C++98 definition of POD is different. */
4097 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
c32097d8
JM
4098}
4099
4100/* Returns true iff T is POD for the purpose of layout, as defined in the
4101 C++ ABI. */
4102
4103bool
4104layout_pod_type_p (const_tree t)
4105{
4106 t = strip_array_types (CONST_CAST_TREE (t));
4107
4108 if (CLASS_TYPE_P (t))
4109 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4110 else
4111 return scalarish_type_p (t);
4112}
4113
4114/* Returns true iff T is a standard-layout type, as defined in
4115 [basic.types]. */
4116
4117bool
4118std_layout_type_p (const_tree t)
4119{
4120 t = strip_array_types (CONST_CAST_TREE (t));
4121
4122 if (CLASS_TYPE_P (t))
4123 return !CLASSTYPE_NON_STD_LAYOUT (t);
4124 else
4125 return scalarish_type_p (t);
5524676d 4126}
e5dc5fb2 4127
342cfb3e
JJ
4128static bool record_has_unique_obj_representations (const_tree, const_tree);
4129
4130/* Returns true iff T satisfies std::has_unique_object_representations<T>,
4131 as defined in [meta.unary.prop]. */
4132
4133bool
4134type_has_unique_obj_representations (const_tree t)
4135{
4136 bool ret;
4137
4138 t = strip_array_types (CONST_CAST_TREE (t));
4139
4140 if (!trivially_copyable_p (t))
4141 return false;
4142
4143 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4144 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4145
4146 switch (TREE_CODE (t))
4147 {
4148 case INTEGER_TYPE:
4149 case POINTER_TYPE:
4150 case REFERENCE_TYPE:
4151 /* If some backend has any paddings in these types, we should add
4152 a target hook for this and handle it there. */
4153 return true;
4154
4155 case BOOLEAN_TYPE:
4156 /* For bool values other than 0 and 1 should only appear with
4157 undefined behavior. */
4158 return true;
4159
4160 case ENUMERAL_TYPE:
4161 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4162
4163 case REAL_TYPE:
4164 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4165 when storing long double values, so for that we have to return false.
4166 Other kinds of floating point values are questionable due to +.0/-.0
4167 and NaNs, let's play safe for now. */
4168 return false;
4169
4170 case FIXED_POINT_TYPE:
4171 return false;
4172
4173 case OFFSET_TYPE:
4174 return true;
4175
4176 case COMPLEX_TYPE:
4177 case VECTOR_TYPE:
4178 return type_has_unique_obj_representations (TREE_TYPE (t));
4179
4180 case RECORD_TYPE:
4181 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4182 if (CLASS_TYPE_P (t))
4183 {
4184 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4185 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4186 }
4187 return ret;
4188
4189 case UNION_TYPE:
4190 ret = true;
4191 bool any_fields;
4192 any_fields = false;
4193 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4194 if (TREE_CODE (field) == FIELD_DECL)
4195 {
4196 any_fields = true;
4197 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4198 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4199 {
4200 ret = false;
4201 break;
4202 }
4203 }
4204 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4205 ret = false;
4206 if (CLASS_TYPE_P (t))
4207 {
4208 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4209 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4210 }
4211 return ret;
4212
4213 case NULLPTR_TYPE:
4214 return false;
4215
4216 case ERROR_MARK:
4217 return false;
4218
4219 default:
4220 gcc_unreachable ();
4221 }
4222}
4223
4224/* Helper function for type_has_unique_obj_representations. */
4225
4226static bool
4227record_has_unique_obj_representations (const_tree t, const_tree sz)
4228{
4229 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4230 if (TREE_CODE (field) != FIELD_DECL)
4231 ;
4232 /* For bases, can't use type_has_unique_obj_representations here, as in
4233 struct S { int i : 24; S (); };
4234 struct T : public S { int j : 8; T (); };
4235 S doesn't have unique obj representations, but T does. */
4236 else if (DECL_FIELD_IS_BASE (field))
4237 {
4238 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4239 DECL_SIZE (field)))
4240 return false;
4241 }
4242 else if (DECL_C_BIT_FIELD (field))
4243 {
4244 tree btype = DECL_BIT_FIELD_TYPE (field);
4245 if (!type_has_unique_obj_representations (btype))
4246 return false;
4247 }
4248 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4249 return false;
4250
4251 offset_int cur = 0;
4252 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4253 if (TREE_CODE (field) == FIELD_DECL)
4254 {
4255 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4256 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4257 fld = fld * BITS_PER_UNIT + bitpos;
4258 if (cur != fld)
4259 return false;
4260 if (DECL_SIZE (field))
4261 {
4262 offset_int size = wi::to_offset (DECL_SIZE (field));
4263 cur += size;
4264 }
4265 }
4266 if (cur != wi::to_offset (sz))
4267 return false;
4268
4269 return true;
4270}
4271
39ef6592
LC
4272/* Nonzero iff type T is a class template implicit specialization. */
4273
4274bool
ac7d7749 4275class_tmpl_impl_spec_p (const_tree t)
39ef6592
LC
4276{
4277 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4278}
4279
94e6e4c4
AO
4280/* Returns 1 iff zero initialization of type T means actually storing
4281 zeros in it. */
4282
4283int
58f9752a 4284zero_init_p (const_tree t)
94e6e4c4 4285{
4e9b57fa 4286 /* This CONST_CAST is okay because strip_array_types returns its
75547801 4287 argument unmodified and we assign it to a const_tree. */
b1d5455a 4288 t = strip_array_types (CONST_CAST_TREE(t));
94e6e4c4 4289
17bbb839
MM
4290 if (t == error_mark_node)
4291 return 1;
4292
94e6e4c4 4293 /* NULL pointers to data members are initialized with -1. */
66b1156a 4294 if (TYPE_PTRDATAMEM_P (t))
94e6e4c4
AO
4295 return 0;
4296
4297 /* Classes that contain types that can't be zero-initialized, cannot
4298 be zero-initialized themselves. */
4299 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4300 return 0;
4301
4302 return 1;
4303}
4304
b632761d
JM
4305/* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4306 warn_unused_result attribute. */
4307
4308static tree
4309handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4310 int /*flags*/, bool *no_add_attrs)
4311{
4312 if (TREE_CODE (*node) == FUNCTION_DECL)
4313 {
4314 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4315 warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
4316 "return type", name, *node);
4317 }
4318 else if (OVERLOAD_TYPE_P (*node))
4319 /* OK */;
4320 else
4321 {
4322 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4323 "functions or to class or enumeration types", name);
4324 *no_add_attrs = true;
4325 }
4326 return NULL_TREE;
4327}
4328
91d231cb 4329/* Table of valid C++ attributes. */
349ae713 4330const struct attribute_spec cxx_attribute_table[] =
e5dc5fb2 4331{
4849deb1
JJ
4332 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4333 affects_type_identity, handler, exclude } */
4334 { "init_priority", 1, 1, true, false, false, false,
4335 handle_init_priority_attribute, NULL },
4336 { "abi_tag", 1, -1, false, false, false, true,
4337 handle_abi_tag_attribute, NULL },
4338 { NULL, 0, 0, false, false, false, false, NULL, NULL }
91d231cb
JM
4339};
4340
d067e05f
JM
4341/* Table of C++ standard attributes. */
4342const struct attribute_spec std_attribute_table[] =
4343{
4849deb1
JJ
4344 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4345 affects_type_identity, handler, exclude } */
4346 { "maybe_unused", 0, 0, false, false, false, false,
4347 handle_unused_attribute, NULL },
4348 { "nodiscard", 0, 0, false, false, false, false,
4349 handle_nodiscard_attribute, NULL },
4350 { NULL, 0, 0, false, false, false, false, NULL, NULL }
d067e05f
JM
4351};
4352
91d231cb
JM
4353/* Handle an "init_priority" attribute; arguments as in
4354 struct attribute_spec.handler. */
4355static tree
9f63daea 4356handle_init_priority_attribute (tree* node,
0cbd7506
MS
4357 tree name,
4358 tree args,
12308bc6 4359 int /*flags*/,
0cbd7506 4360 bool* no_add_attrs)
91d231cb
JM
4361{
4362 tree initp_expr = TREE_VALUE (args);
4363 tree decl = *node;
4364 tree type = TREE_TYPE (decl);
4365 int pri;
4366
4367 STRIP_NOPS (initp_expr);
5d77fb19 4368 initp_expr = default_conversion (initp_expr);
cda0a029
JM
4369 if (initp_expr)
4370 initp_expr = maybe_constant_value (initp_expr);
9f63daea 4371
91d231cb
JM
4372 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
4373 {
4374 error ("requested init_priority is not an integer constant");
cda0a029 4375 cxx_constant_value (initp_expr);
91d231cb
JM
4376 *no_add_attrs = true;
4377 return NULL_TREE;
4378 }
e5dc5fb2 4379
91d231cb 4380 pri = TREE_INT_CST_LOW (initp_expr);
9f63daea 4381
91d231cb
JM
4382 type = strip_array_types (type);
4383
4384 if (decl == NULL_TREE
5a6ccc94 4385 || !VAR_P (decl)
91d231cb
JM
4386 || !TREE_STATIC (decl)
4387 || DECL_EXTERNAL (decl)
4388 || (TREE_CODE (type) != RECORD_TYPE
4389 && TREE_CODE (type) != UNION_TYPE)
4390 /* Static objects in functions are initialized the
4391 first time control passes through that
4392 function. This is not precise enough to pin down an
c6002625 4393 init_priority value, so don't allow it. */
9f63daea 4394 || current_function_decl)
91d231cb 4395 {
a82e1a7d 4396 error ("can only use %qE attribute on file-scope definitions "
0cbd7506 4397 "of objects of class type", name);
91d231cb
JM
4398 *no_add_attrs = true;
4399 return NULL_TREE;
4400 }
e5dc5fb2 4401
91d231cb
JM
4402 if (pri > MAX_INIT_PRIORITY || pri <= 0)
4403 {
4404 error ("requested init_priority is out of range");
4405 *no_add_attrs = true;
4406 return NULL_TREE;
4407 }
e5dc5fb2 4408
91d231cb
JM
4409 /* Check for init_priorities that are reserved for
4410 language and runtime support implementations.*/
4411 if (pri <= MAX_RESERVED_INIT_PRIORITY)
4412 {
9f63daea 4413 warning
d4ee4d25 4414 (0, "requested init_priority is reserved for internal use");
e5dc5fb2
JM
4415 }
4416
91d231cb
JM
4417 if (SUPPORTS_INIT_PRIORITY)
4418 {
820cc88f
DB
4419 SET_DECL_INIT_PRIORITY (decl, pri);
4420 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
91d231cb
JM
4421 return NULL_TREE;
4422 }
4423 else
4424 {
a82e1a7d 4425 error ("%qE attribute is not supported on this platform", name);
91d231cb
JM
4426 *no_add_attrs = true;
4427 return NULL_TREE;
4428 }
e5dc5fb2 4429}
87533b37 4430
7dbb85a7
JM
4431/* DECL is being redeclared; the old declaration had the abi tags in OLD,
4432 and the new one has the tags in NEW_. Give an error if there are tags
4433 in NEW_ that weren't in OLD. */
4434
4435bool
4436check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
4437{
4438 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
4439 old = TREE_VALUE (old);
4440 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
4441 new_ = TREE_VALUE (new_);
4442 bool err = false;
4443 for (const_tree t = new_; t; t = TREE_CHAIN (t))
4444 {
4445 tree str = TREE_VALUE (t);
4446 for (const_tree in = old; in; in = TREE_CHAIN (in))
4447 {
4448 tree ostr = TREE_VALUE (in);
4449 if (cp_tree_equal (str, ostr))
4450 goto found;
4451 }
0f2c4a8f 4452 error ("redeclaration of %qD adds abi tag %qE", decl, str);
7dbb85a7
JM
4453 err = true;
4454 found:;
4455 }
4456 if (err)
4457 {
4458 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
4459 return false;
4460 }
4461 return true;
4462}
4463
7cb73573
JM
4464/* The abi_tag attribute with the name NAME was given ARGS. If they are
4465 ill-formed, give an error and return false; otherwise, return true. */
7dbb85a7 4466
7cb73573
JM
4467bool
4468check_abi_tag_args (tree args, tree name)
7dbb85a7 4469{
7cb73573
JM
4470 if (!args)
4471 {
4472 error ("the %qE attribute requires arguments", name);
4473 return false;
4474 }
4c5cf0b2
JM
4475 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
4476 {
4477 tree elt = TREE_VALUE (arg);
4478 if (TREE_CODE (elt) != STRING_CST
4479 || (!same_type_ignoring_top_level_qualifiers_p
4480 (strip_array_types (TREE_TYPE (elt)),
4481 char_type_node)))
4482 {
4483 error ("arguments to the %qE attribute must be narrow string "
4484 "literals", name);
7cb73573 4485 return false;
4c5cf0b2
JM
4486 }
4487 const char *begin = TREE_STRING_POINTER (elt);
4488 const char *end = begin + TREE_STRING_LENGTH (elt);
4489 for (const char *p = begin; p != end; ++p)
4490 {
4491 char c = *p;
4492 if (p == begin)
4493 {
4494 if (!ISALPHA (c) && c != '_')
4495 {
4496 error ("arguments to the %qE attribute must contain valid "
4497 "identifiers", name);
4498 inform (input_location, "%<%c%> is not a valid first "
4499 "character for an identifier", c);
7cb73573 4500 return false;
4c5cf0b2
JM
4501 }
4502 }
4503 else if (p == end - 1)
4504 gcc_assert (c == 0);
4505 else
4506 {
4507 if (!ISALNUM (c) && c != '_')
4508 {
4509 error ("arguments to the %qE attribute must contain valid "
4510 "identifiers", name);
4511 inform (input_location, "%<%c%> is not a valid character "
4512 "in an identifier", c);
7cb73573 4513 return false;
4c5cf0b2
JM
4514 }
4515 }
4516 }
4517 }
7cb73573
JM
4518 return true;
4519}
4520
4521/* Handle an "abi_tag" attribute; arguments as in
4522 struct attribute_spec.handler. */
4523
4524static tree
4525handle_abi_tag_attribute (tree* node, tree name, tree args,
4526 int flags, bool* no_add_attrs)
4527{
4528 if (!check_abi_tag_args (args, name))
4529 goto fail;
4c5cf0b2 4530
7dbb85a7
JM
4531 if (TYPE_P (*node))
4532 {
73243d63 4533 if (!OVERLOAD_TYPE_P (*node))
7dbb85a7
JM
4534 {
4535 error ("%qE attribute applied to non-class, non-enum type %qT",
4536 name, *node);
4537 goto fail;
4538 }
4539 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
4540 {
4541 error ("%qE attribute applied to %qT after its definition",
4542 name, *node);
4543 goto fail;
4544 }
7b3bc1f3
MP
4545 else if (CLASS_TYPE_P (*node)
4546 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
2982147e
JM
4547 {
4548 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4549 "template instantiation %qT", name, *node);
4550 goto fail;
4551 }
7b3bc1f3
MP
4552 else if (CLASS_TYPE_P (*node)
4553 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
2982147e
JM
4554 {
4555 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4556 "template specialization %qT", name, *node);
4557 goto fail;
4558 }
7dbb85a7
JM
4559
4560 tree attributes = TYPE_ATTRIBUTES (*node);
4561 tree decl = TYPE_NAME (*node);
4562
4563 /* Make sure all declarations have the same abi tags. */
4564 if (DECL_SOURCE_LOCATION (decl) != input_location)
4565 {
4566 if (!check_abi_tag_redeclaration (decl,
4567 lookup_attribute ("abi_tag",
4568 attributes),
4569 args))
4570 goto fail;
4571 }
4572 }
4573 else
4574 {
56a6f1d3 4575 if (!VAR_OR_FUNCTION_DECL_P (*node))
7dbb85a7 4576 {
7cb73573
JM
4577 error ("%qE attribute applied to non-function, non-variable %qD",
4578 name, *node);
7dbb85a7
JM
4579 goto fail;
4580 }
4581 else if (DECL_LANGUAGE (*node) == lang_c)
4582 {
7cb73573 4583 error ("%qE attribute applied to extern \"C\" declaration %qD",
7dbb85a7
JM
4584 name, *node);
4585 goto fail;
4586 }
4587 }
4588
4589 return NULL_TREE;
4590
4591 fail:
4592 *no_add_attrs = true;
4593 return NULL_TREE;
4594}
4595
87533b37
MM
4596/* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
4597 thing pointed to by the constant. */
4598
4599tree
b57b79f7 4600make_ptrmem_cst (tree type, tree member)
87533b37
MM
4601{
4602 tree ptrmem_cst = make_node (PTRMEM_CST);
87533b37
MM
4603 TREE_TYPE (ptrmem_cst) = type;
4604 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
4605 return ptrmem_cst;
4606}
4607
e9525111 4608/* Build a variant of TYPE that has the indicated ATTRIBUTES. May
51035976 4609 return an existing type if an appropriate type already exists. */
e9525111
MM
4610
4611tree
4612cp_build_type_attribute_variant (tree type, tree attributes)
4613{
4614 tree new_type;
4615
4616 new_type = build_type_attribute_variant (type, attributes);
3a55fb4c
JM
4617 if (TREE_CODE (new_type) == FUNCTION_TYPE
4618 || TREE_CODE (new_type) == METHOD_TYPE)
2eed8e37
BK
4619 {
4620 new_type = build_exception_variant (new_type,
4621 TYPE_RAISES_EXCEPTIONS (type));
4622 new_type = build_ref_qualified_type (new_type,
4623 type_memfn_rqual (type));
4624 }
8e30dcf3
JM
4625
4626 /* Making a new main variant of a class type is broken. */
4627 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
4628
e9525111
MM
4629 return new_type;
4630}
4631
2dff8956 4632/* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
1906d6b4 4633 Called only after doing all language independent checks. */
2dff8956
JJ
4634
4635bool
4636cxx_type_hash_eq (const_tree typea, const_tree typeb)
4637{
220e83ca
KT
4638 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
4639 || TREE_CODE (typea) == METHOD_TYPE);
2dff8956 4640
1906d6b4
JM
4641 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
4642 return false;
2dff8956 4643 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3a55fb4c 4644 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
2dff8956
JJ
4645}
4646
27c825c5
JM
4647/* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
4648 C++, these are the exception-specifier and ref-qualifier. */
4649
4650tree
4651cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
4652{
4653 tree type = CONST_CAST_TREE (typea);
4654 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4655 {
4656 type = build_exception_variant (type, TYPE_RAISES_EXCEPTIONS (typeb));
4657 type = build_ref_qualified_type (type, type_memfn_rqual (typeb));
4658 }
4659 return type;
4660}
4661
25af8512 4662/* Apply FUNC to all language-specific sub-trees of TP in a pre-order
350fae66 4663 traversal. Called from walk_tree. */
25af8512 4664
9f63daea 4665tree
350fae66 4666cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
6e2830c3 4667 void *data, hash_set<tree> *pset)
25af8512
AO
4668{
4669 enum tree_code code = TREE_CODE (*tp);
4670 tree result;
9f63daea 4671
25af8512
AO
4672#define WALK_SUBTREE(NODE) \
4673 do \
4674 { \
14588106 4675 result = cp_walk_tree (&(NODE), func, data, pset); \
6de9cd9a 4676 if (result) goto out; \
25af8512
AO
4677 } \
4678 while (0)
4679
4680 /* Not one of the easy cases. We must explicitly go through the
4681 children. */
6de9cd9a 4682 result = NULL_TREE;
25af8512
AO
4683 switch (code)
4684 {
4685 case DEFAULT_ARG:
4686 case TEMPLATE_TEMPLATE_PARM:
4687 case BOUND_TEMPLATE_TEMPLATE_PARM:
b8c6534b 4688 case UNBOUND_CLASS_TEMPLATE:
25af8512
AO
4689 case TEMPLATE_PARM_INDEX:
4690 case TEMPLATE_TYPE_PARM:
4691 case TYPENAME_TYPE:
4692 case TYPEOF_TYPE:
a0d260fc 4693 case UNDERLYING_TYPE:
da1d7781 4694 /* None of these have subtrees other than those already walked
0cbd7506 4695 above. */
25af8512
AO
4696 *walk_subtrees_p = 0;
4697 break;
4698
5d80a306 4699 case BASELINK:
88b811bd
JM
4700 if (BASELINK_QUALIFIED_P (*tp))
4701 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
5d80a306
DG
4702 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
4703 *walk_subtrees_p = 0;
4704 break;
4705
25af8512
AO
4706 case PTRMEM_CST:
4707 WALK_SUBTREE (TREE_TYPE (*tp));
4708 *walk_subtrees_p = 0;
4709 break;
4710
4711 case TREE_LIST:
5dae1114 4712 WALK_SUBTREE (TREE_PURPOSE (*tp));
25af8512
AO
4713 break;
4714
4715 case OVERLOAD:
4716 WALK_SUBTREE (OVL_FUNCTION (*tp));
4717 WALK_SUBTREE (OVL_CHAIN (*tp));
4718 *walk_subtrees_p = 0;
4439d02f
DG
4719 break;
4720
4721 case USING_DECL:
4722 WALK_SUBTREE (DECL_NAME (*tp));
4723 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
4724 WALK_SUBTREE (USING_DECL_DECLS (*tp));
4725 *walk_subtrees_p = 0;
25af8512
AO
4726 break;
4727
4728 case RECORD_TYPE:
4729 if (TYPE_PTRMEMFUNC_P (*tp))
35abb8ed 4730 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
25af8512
AO
4731 break;
4732
5d80a306
DG
4733 case TYPE_ARGUMENT_PACK:
4734 case NONTYPE_ARGUMENT_PACK:
4735 {
4736 tree args = ARGUMENT_PACK_ARGS (*tp);
4737 int i, len = TREE_VEC_LENGTH (args);
4738 for (i = 0; i < len; i++)
4739 WALK_SUBTREE (TREE_VEC_ELT (args, i));
4740 }
4741 break;
4742
4743 case TYPE_PACK_EXPANSION:
4744 WALK_SUBTREE (TREE_TYPE (*tp));
c67dd256 4745 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5d80a306
DG
4746 *walk_subtrees_p = 0;
4747 break;
4748
4749 case EXPR_PACK_EXPANSION:
4750 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
c67dd256 4751 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5d80a306
DG
4752 *walk_subtrees_p = 0;
4753 break;
4754
4755 case CAST_EXPR:
a7cbc517
JJ
4756 case REINTERPRET_CAST_EXPR:
4757 case STATIC_CAST_EXPR:
4758 case CONST_CAST_EXPR:
4759 case DYNAMIC_CAST_EXPR:
a4474a38 4760 case IMPLICIT_CONV_EXPR:
5d80a306
DG
4761 if (TREE_TYPE (*tp))
4762 WALK_SUBTREE (TREE_TYPE (*tp));
4763
4764 {
4765 int i;
4766 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4767 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4768 }
4769 *walk_subtrees_p = 0;
4770 break;
4771
cb68ec50
PC
4772 case TRAIT_EXPR:
4773 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4774 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4775 *walk_subtrees_p = 0;
4776 break;
4777
3ad6a8e1
DG
4778 case DECLTYPE_TYPE:
4779 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4780 *walk_subtrees_p = 0;
4781 break;
4782
971e17ff
AS
4783 case REQUIRES_EXPR:
4784 // Only recurse through the nested expression. Do not
4785 // walk the parameter list. Doing so causes false
4786 // positives in the pack expansion checker since the
4787 // requires parameters are introduced as pack expansions.
4788 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4789 *walk_subtrees_p = 0;
4790 break;
3ad6a8e1 4791
e43ebb12
JJ
4792 case DECL_EXPR:
4793 /* User variables should be mentioned in BIND_EXPR_VARS
4794 and their initializers and sizes walked when walking
4795 the containing BIND_EXPR. Compiler temporaries are
4796 handled here. */
4797 if (VAR_P (TREE_OPERAND (*tp, 0))
4798 && DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
4799 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))
4800 {
4801 tree decl = TREE_OPERAND (*tp, 0);
4802 WALK_SUBTREE (DECL_INITIAL (decl));
4803 WALK_SUBTREE (DECL_SIZE (decl));
4804 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
4805 }
4806 break;
4807
25af8512 4808 default:
350fae66 4809 return NULL_TREE;
25af8512
AO
4810 }
4811
4812 /* We didn't find what we were looking for. */
6de9cd9a 4813 out:
6de9cd9a 4814 return result;
25af8512
AO
4815
4816#undef WALK_SUBTREE
4817}
4818
b655f214
MM
4819/* Like save_expr, but for C++. */
4820
4821tree
4822cp_save_expr (tree expr)
4823{
4824 /* There is no reason to create a SAVE_EXPR within a template; if
4825 needed, we can create the SAVE_EXPR when instantiating the
4826 template. Furthermore, the middle-end cannot handle C++-specific
4827 tree codes. */
4828 if (processing_template_decl)
4829 return expr;
4830 return save_expr (expr);
4831}
4832
87e3dbc9
MM
4833/* Initialize tree.c. */
4834
0a818f84 4835void
b57b79f7 4836init_tree (void)
0a818f84 4837{
2a22f99c 4838 list_hash_table = hash_table<list_hasher>::create_ggc (61);
d067e05f 4839 register_scoped_attributes (std_attribute_table, NULL);
0a818f84
GRK
4840}
4841
872f37f9 4842/* Returns the kind of special function that DECL (a FUNCTION_DECL)
50ad9642
MM
4843 is. Note that sfk_none is zero, so this function can be used as a
4844 predicate to test whether or not DECL is a special function. */
872f37f9
MM
4845
4846special_function_kind
58f9752a 4847special_function_p (const_tree decl)
872f37f9
MM
4848{
4849 /* Rather than doing all this stuff with magic names, we should
4850 probably have a field of type `special_function_kind' in
4851 DECL_LANG_SPECIFIC. */
31f7f784 4852 if (DECL_INHERITED_CTOR (decl))
85b5d65a 4853 return sfk_inheriting_constructor;
872f37f9
MM
4854 if (DECL_COPY_CONSTRUCTOR_P (decl))
4855 return sfk_copy_constructor;
d5f4eddd
JM
4856 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4857 return sfk_move_constructor;
872f37f9
MM
4858 if (DECL_CONSTRUCTOR_P (decl))
4859 return sfk_constructor;
137073d3
NS
4860 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
4861 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
ac177431
JM
4862 {
4863 if (copy_fn_p (decl))
4864 return sfk_copy_assignment;
4865 if (move_fn_p (decl))
4866 return sfk_move_assignment;
4867 }
872f37f9
MM
4868 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4869 return sfk_destructor;
4870 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4871 return sfk_complete_destructor;
4872 if (DECL_BASE_DESTRUCTOR_P (decl))
4873 return sfk_base_destructor;
4874 if (DECL_DELETING_DESTRUCTOR_P (decl))
4875 return sfk_deleting_destructor;
4876 if (DECL_CONV_FN_P (decl))
4877 return sfk_conversion;
a56c0ac0
JM
4878 if (deduction_guide_p (decl))
4879 return sfk_deduction_guide;
872f37f9
MM
4880
4881 return sfk_none;
4882}
7b019c19 4883
838dfd8a 4884/* Returns nonzero if TYPE is a character type, including wchar_t. */
7b019c19
MM
4885
4886int
b57b79f7 4887char_type_p (tree type)
7b019c19
MM
4888{
4889 return (same_type_p (type, char_type_node)
4890 || same_type_p (type, unsigned_char_type_node)
4891 || same_type_p (type, signed_char_type_node)
b6baa67d
KVH
4892 || same_type_p (type, char16_type_node)
4893 || same_type_p (type, char32_type_node)
7b019c19
MM
4894 || same_type_p (type, wchar_type_node));
4895}
ad50e811
MM
4896
4897/* Returns the kind of linkage associated with the indicated DECL. Th
4898 value returned is as specified by the language standard; it is
4899 independent of implementation details regarding template
4900 instantiation, etc. For example, it is possible that a declaration
4901 to which this function assigns external linkage would not show up
4902 as a global symbol when you run `nm' on the resulting object file. */
4903
4904linkage_kind
b57b79f7 4905decl_linkage (tree decl)
ad50e811
MM
4906{
4907 /* This function doesn't attempt to calculate the linkage from first
4908 principles as given in [basic.link]. Instead, it makes use of
4909 the fact that we have already set TREE_PUBLIC appropriately, and
4910 then handles a few special cases. Ideally, we would calculate
4911 linkage first, and then transform that into a concrete
4912 implementation. */
4913
4914 /* Things that don't have names have no linkage. */
4915 if (!DECL_NAME (decl))
4916 return lk_none;
4917
c02cdc25
TT
4918 /* Fields have no linkage. */
4919 if (TREE_CODE (decl) == FIELD_DECL)
4920 return lk_none;
4921
ad50e811
MM
4922 /* Things that are TREE_PUBLIC have external linkage. */
4923 if (TREE_PUBLIC (decl))
4924 return lk_external;
3db45ab5 4925
effdaefe
JM
4926 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
4927 check one of the "clones" for the real linkage. */
4928 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
4929 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
4930 && DECL_CHAIN (decl)
4931 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
4932 return decl_linkage (DECL_CHAIN (decl));
4933
b70f0f48
JM
4934 if (TREE_CODE (decl) == NAMESPACE_DECL)
4935 return lk_external;
4936
3db45ab5 4937 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3f774254
DB
4938 type. */
4939 if (TREE_CODE (decl) == CONST_DECL)
8d0d1915 4940 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
ad50e811 4941
ad50e811
MM
4942 /* Things in local scope do not have linkage, if they don't have
4943 TREE_PUBLIC set. */
4944 if (decl_function_context (decl))
4945 return lk_none;
4946
b70f0f48 4947 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
d5d0ed2d
JM
4948 are considered to have external linkage for language purposes, as do
4949 template instantiations on targets without weak symbols. DECLs really
4950 meant to have internal linkage have DECL_THIS_STATIC set. */
ce41114b 4951 if (TREE_CODE (decl) == TYPE_DECL)
b70f0f48 4952 return lk_external;
cb6da767 4953 if (VAR_OR_FUNCTION_DECL_P (decl))
ce41114b
JJ
4954 {
4955 if (!DECL_THIS_STATIC (decl))
4956 return lk_external;
4957
4958 /* Static data members and static member functions from classes
4959 in anonymous namespace also don't have TREE_PUBLIC set. */
4960 if (DECL_CLASS_CONTEXT (decl))
4961 return lk_external;
4962 }
b70f0f48 4963
ad50e811
MM
4964 /* Everything else has internal linkage. */
4965 return lk_internal;
4966}
b95ca513
JM
4967
4968/* Returns the storage duration of the object or reference associated with
4969 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
4970
4971duration_kind
4972decl_storage_duration (tree decl)
4973{
4974 if (TREE_CODE (decl) == PARM_DECL)
4975 return dk_auto;
4976 if (TREE_CODE (decl) == FUNCTION_DECL)
4977 return dk_static;
5a6ccc94 4978 gcc_assert (VAR_P (decl));
b95ca513
JM
4979 if (!TREE_STATIC (decl)
4980 && !DECL_EXTERNAL (decl))
4981 return dk_auto;
3048c0c7 4982 if (CP_DECL_THREAD_LOCAL_P (decl))
b95ca513
JM
4983 return dk_thread;
4984 return dk_static;
4985}
6f30f1f1 4986\f
9beafc83
MM
4987/* EXP is an expression that we want to pre-evaluate. Returns (in
4988 *INITP) an expression that will perform the pre-evaluation. The
4989 value returned by this function is a side-effect free expression
4990 equivalent to the pre-evaluated expression. Callers must ensure
4991 that *INITP is evaluated before EXP. */
6f30f1f1
JM
4992
4993tree
b57b79f7 4994stabilize_expr (tree exp, tree* initp)
6f30f1f1
JM
4995{
4996 tree init_expr;
4997
4998 if (!TREE_SIDE_EFFECTS (exp))
9beafc83 4999 init_expr = NULL_TREE;
982058cb
PC
5000 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5001 {
989e6706 5002 init_expr = exp;
632f2871 5003 exp = void_node;
982058cb 5004 }
e3edeff4
JM
5005 /* There are no expressions with REFERENCE_TYPE, but there can be call
5006 arguments with such a type; just treat it as a pointer. */
5007 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
fa07d25b 5008 || SCALAR_TYPE_P (TREE_TYPE (exp))
c3edc633 5009 || !glvalue_p (exp))
6f30f1f1
JM
5010 {
5011 init_expr = get_target_expr (exp);
5012 exp = TARGET_EXPR_SLOT (init_expr);
fc2bfea1
JM
5013 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5014 exp = move (exp);
5015 else
5016 exp = rvalue (exp);
6f30f1f1
JM
5017 }
5018 else
5019 {
72b3e203 5020 bool xval = !lvalue_p (exp);
93c0e0bb 5021 exp = cp_build_addr_expr (exp, tf_warning_or_error);
6f30f1f1
JM
5022 init_expr = get_target_expr (exp);
5023 exp = TARGET_EXPR_SLOT (init_expr);
04757a2a 5024 exp = cp_build_fold_indirect_ref (exp);
883fff6c
JM
5025 if (xval)
5026 exp = move (exp);
6f30f1f1 5027 }
6f30f1f1 5028 *initp = init_expr;
9beafc83
MM
5029
5030 gcc_assert (!TREE_SIDE_EFFECTS (exp));
6f30f1f1
JM
5031 return exp;
5032}
6de9cd9a 5033
be93747e 5034/* Add NEW_EXPR, an expression whose value we don't care about, after the
40aac948
JM
5035 similar expression ORIG. */
5036
5037tree
be93747e 5038add_stmt_to_compound (tree orig, tree new_expr)
40aac948 5039{
be93747e 5040 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
40aac948
JM
5041 return orig;
5042 if (!orig || !TREE_SIDE_EFFECTS (orig))
be93747e
KG
5043 return new_expr;
5044 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
40aac948
JM
5045}
5046
9beafc83
MM
5047/* Like stabilize_expr, but for a call whose arguments we want to
5048 pre-evaluate. CALL is modified in place to use the pre-evaluated
5049 arguments, while, upon return, *INITP contains an expression to
5050 compute the arguments. */
6de9cd9a
DN
5051
5052void
5053stabilize_call (tree call, tree *initp)
5054{
5055 tree inits = NULL_TREE;
5039610b
SL
5056 int i;
5057 int nargs = call_expr_nargs (call);
6de9cd9a 5058
28267cfc
JJ
5059 if (call == error_mark_node || processing_template_decl)
5060 {
5061 *initp = NULL_TREE;
5062 return;
5063 }
6de9cd9a 5064
5039610b 5065 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6de9cd9a 5066
5039610b
SL
5067 for (i = 0; i < nargs; i++)
5068 {
5069 tree init;
5070 CALL_EXPR_ARG (call, i) =
5071 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5072 inits = add_stmt_to_compound (inits, init);
5073 }
5074
5075 *initp = inits;
5076}
5077
5078/* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5079 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5080 arguments, while, upon return, *INITP contains an expression to
5081 compute the arguments. */
5082
81bd268c 5083static void
5039610b
SL
5084stabilize_aggr_init (tree call, tree *initp)
5085{
5086 tree inits = NULL_TREE;
5087 int i;
5088 int nargs = aggr_init_expr_nargs (call);
5089
5090 if (call == error_mark_node)
5091 return;
5092
5093 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5094
5095 for (i = 0; i < nargs; i++)
5096 {
5097 tree init;
5098 AGGR_INIT_EXPR_ARG (call, i) =
5099 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5100 inits = add_stmt_to_compound (inits, init);
5101 }
6de9cd9a
DN
5102
5103 *initp = inits;
5104}
5105
9beafc83
MM
5106/* Like stabilize_expr, but for an initialization.
5107
5108 If the initialization is for an object of class type, this function
5109 takes care not to introduce additional temporaries.
5110
5111 Returns TRUE iff the expression was successfully pre-evaluated,
66edf32a 5112 i.e., if INIT is now side-effect free, except for, possibly, a
9beafc83 5113 single call to a constructor. */
6de9cd9a
DN
5114
5115bool
5116stabilize_init (tree init, tree *initp)
5117{
5118 tree t = init;
5119
9beafc83
MM
5120 *initp = NULL_TREE;
5121
28267cfc 5122 if (t == error_mark_node || processing_template_decl)
6de9cd9a
DN
5123 return true;
5124
9beafc83
MM
5125 if (TREE_CODE (t) == INIT_EXPR)
5126 t = TREE_OPERAND (t, 1);
5127 if (TREE_CODE (t) == TARGET_EXPR)
5128 t = TARGET_EXPR_INITIAL (t);
66edf32a
JM
5129
5130 /* If the RHS can be stabilized without breaking copy elision, stabilize
5131 it. We specifically don't stabilize class prvalues here because that
5132 would mean an extra copy, but they might be stabilized below. */
5133 if (TREE_CODE (init) == INIT_EXPR
5134 && TREE_CODE (t) != CONSTRUCTOR
5135 && TREE_CODE (t) != AGGR_INIT_EXPR
5136 && (SCALAR_TYPE_P (TREE_TYPE (t))
c3edc633 5137 || glvalue_p (t)))
66edf32a
JM
5138 {
5139 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5140 return true;
5141 }
5142
5143 if (TREE_CODE (t) == COMPOUND_EXPR
5144 && TREE_CODE (init) == INIT_EXPR)
5145 {
5146 tree last = expr_last (t);
5147 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5148 if (!TREE_SIDE_EFFECTS (last))
5149 {
5150 *initp = t;
5151 TREE_OPERAND (init, 1) = last;
5152 return true;
5153 }
5154 }
5155
b9d6b015
JM
5156 if (TREE_CODE (t) == CONSTRUCTOR)
5157 {
5158 /* Aggregate initialization: stabilize each of the field
5159 initializers. */
5160 unsigned i;
0c59fd2f 5161 constructor_elt *ce;
b9d6b015 5162 bool good = true;
9771b263
DN
5163 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5164 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
0c59fd2f
JM
5165 {
5166 tree type = TREE_TYPE (ce->value);
5167 tree subinit;
5168 if (TREE_CODE (type) == REFERENCE_TYPE
5169 || SCALAR_TYPE_P (type))
5170 ce->value = stabilize_expr (ce->value, &subinit);
5171 else if (!stabilize_init (ce->value, &subinit))
5172 good = false;
5173 *initp = add_stmt_to_compound (*initp, subinit);
5174 }
b9d6b015
JM
5175 return good;
5176 }
9beafc83 5177
5039610b 5178 if (TREE_CODE (t) == CALL_EXPR)
9beafc83
MM
5179 {
5180 stabilize_call (t, initp);
5181 return true;
6de9cd9a
DN
5182 }
5183
5039610b
SL
5184 if (TREE_CODE (t) == AGGR_INIT_EXPR)
5185 {
5186 stabilize_aggr_init (t, initp);
5187 return true;
5188 }
5189
9beafc83
MM
5190 /* The initialization is being performed via a bitwise copy -- and
5191 the item copied may have side effects. */
4bbbcbf6 5192 return !TREE_SIDE_EFFECTS (init);
6de9cd9a
DN
5193}
5194
015c2c66
MM
5195/* Returns true if a cast to TYPE may appear in an integral constant
5196 expression. */
5197
5198bool
5199cast_valid_in_integral_constant_expression_p (tree type)
5200{
5201 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
604b2bfc 5202 || cxx_dialect >= cxx11
015c2c66
MM
5203 || dependent_type_p (type)
5204 || type == error_mark_node);
5205}
5206
4537ec0c
DN
5207/* Return true if we need to fix linkage information of DECL. */
5208
5209static bool
5210cp_fix_function_decl_p (tree decl)
5211{
5212 /* Skip if DECL is not externally visible. */
5213 if (!TREE_PUBLIC (decl))
5214 return false;
5215
5216 /* We need to fix DECL if it a appears to be exported but with no
5217 function body. Thunks do not have CFGs and we may need to
5218 handle them specially later. */
5219 if (!gimple_has_body_p (decl)
5220 && !DECL_THUNK_P (decl)
5221 && !DECL_EXTERNAL (decl))
87501227 5222 {
d52f5295 5223 struct cgraph_node *node = cgraph_node::get (decl);
87501227
JJ
5224
5225 /* Don't fix same_body aliases. Although they don't have their own
5226 CFG, they share it with what they alias to. */
67348ccc
DM
5227 if (!node || !node->alias
5228 || !vec_safe_length (node->ref_list.references))
87501227
JJ
5229 return true;
5230 }
4537ec0c
DN
5231
5232 return false;
5233}
5234
5235/* Clean the C++ specific parts of the tree T. */
5236
5237void
5238cp_free_lang_data (tree t)
5239{
5240 if (TREE_CODE (t) == METHOD_TYPE
5241 || TREE_CODE (t) == FUNCTION_TYPE)
5242 {
5243 /* Default args are not interesting anymore. */
5244 tree argtypes = TYPE_ARG_TYPES (t);
5245 while (argtypes)
5246 {
5247 TREE_PURPOSE (argtypes) = 0;
5248 argtypes = TREE_CHAIN (argtypes);
5249 }
5250 }
5251 else if (TREE_CODE (t) == FUNCTION_DECL
5252 && cp_fix_function_decl_p (t))
5253 {
5254 /* If T is used in this translation unit at all, the definition
5255 must exist somewhere else since we have decided to not emit it
5256 in this TU. So make it an external reference. */
5257 DECL_EXTERNAL (t) = 1;
5258 TREE_STATIC (t) = 0;
5259 }
b4ca4f9e 5260 if (TREE_CODE (t) == NAMESPACE_DECL)
44e00a7a
NS
5261 /* We do not need the leftover chaining of namespaces from the
5262 binding level. */
5263 DECL_CHAIN (t) = NULL_TREE;
4537ec0c
DN
5264}
5265
bffad7f1
SB
5266/* Stub for c-common. Please keep in sync with c-decl.c.
5267 FIXME: If address space support is target specific, then this
5268 should be a C target hook. But currently this is not possible,
5269 because this function is called via REGISTER_TARGET_PRAGMAS. */
5270void
12308bc6 5271c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
bffad7f1
SB
5272{
5273}
5274
d26e5986
NF
5275/* Return the number of operands in T that we care about for things like
5276 mangling. */
5277
5278int
5279cp_tree_operand_length (const_tree t)
5280{
5281 enum tree_code code = TREE_CODE (t);
5282
5fdfa03e
PP
5283 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5284 return VL_EXP_OPERAND_LENGTH (t);
d26e5986 5285
5fdfa03e 5286 return cp_tree_code_length (code);
d26e5986 5287}
30b07d03 5288
fcb9363e
PP
5289/* Like cp_tree_operand_length, but takes a tree_code CODE. */
5290
5291int
5292cp_tree_code_length (enum tree_code code)
5293{
5294 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5295
5296 switch (code)
5297 {
5298 case PREINCREMENT_EXPR:
5299 case PREDECREMENT_EXPR:
5300 case POSTINCREMENT_EXPR:
5301 case POSTDECREMENT_EXPR:
5302 return 1;
5303
5304 case ARRAY_REF:
5305 return 2;
5306
5307 case EXPR_PACK_EXPANSION:
5308 return 1;
5309
5310 default:
5311 return TREE_CODE_LENGTH (code);
5312 }
5313}
5314
30b07d03
PC
5315/* Implement -Wzero_as_null_pointer_constant. Return true if the
5316 conditions for the warning hold, false otherwise. */
5317bool
5318maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
5319{
5320 if (c_inhibit_evaluation_warnings == 0
5321 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
5322 {
5323 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
5324 "zero as null pointer constant");
5325 return true;
5326 }
5327 return false;
5328}
e2500fed
GK
5329\f
5330#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5331/* Complain that some language-specific thing hanging off a tree
5332 node has been accessed improperly. */
5333
5334void
b57b79f7 5335lang_check_failed (const char* file, int line, const char* function)
e2500fed
GK
5336{
5337 internal_error ("lang_* check: failed in %s, at %s:%d",
5338 function, trim_filename (file), line);
5339}
5340#endif /* ENABLE_TREE_CHECKING */
5341
5342#include "gt-cp-tree.h"