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