]>
Commit | Line | Data |
---|---|---|
0090dad2 | 1 | /* Perform the semantic phase of parsing, i.e., the process of |
2 | building tree structure, checking semantic consistency, and | |
3 | building RTL. These routines are used both during actual parsing | |
9031d10b | 4 | and during the instantiation of template functions. |
0090dad2 | 5 | |
f7855282 | 6 | Copyright (C) 1998-2012 Free Software Foundation, Inc. |
0090dad2 | 7 | Written by Mark Mitchell (mmitchell@usa.net) based on code found |
9031d10b | 8 | formerly in parse.y and pt.c. |
0090dad2 | 9 | |
6f0d25a6 | 10 | This file is part of GCC. |
0090dad2 | 11 | |
6f0d25a6 | 12 | GCC is free software; you can redistribute it and/or modify it |
0090dad2 | 13 | under the terms of the GNU General Public License as published by |
aa139c3f | 14 | the Free Software Foundation; either version 3, or (at your option) |
0090dad2 | 15 | any later version. |
9031d10b | 16 | |
6f0d25a6 | 17 | GCC is distributed in the hope that it will be useful, but |
0090dad2 | 18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | General Public License for more details. | |
9031d10b | 21 | |
aa139c3f | 22 | You should have received a copy of the GNU General Public License |
23 | along with GCC; see the file COPYING3. If not see | |
24 | <http://www.gnu.org/licenses/>. */ | |
0090dad2 | 25 | |
26 | #include "config.h" | |
b3ef7553 | 27 | #include "system.h" |
805e22b2 | 28 | #include "coretypes.h" |
29 | #include "tm.h" | |
0090dad2 | 30 | #include "tree.h" |
31 | #include "cp-tree.h" | |
7bedc3a0 | 32 | #include "c-family/c-common.h" |
6c536c4f | 33 | #include "c-family/c-objc.h" |
1e3b023c | 34 | #include "tree-inline.h" |
ac7549c7 | 35 | #include "intl.h" |
d6a718e6 | 36 | #include "toplev.h" |
a3d5bae2 | 37 | #include "flags.h" |
a6260fc7 | 38 | #include "timevar.h" |
4ee9c684 | 39 | #include "diagnostic.h" |
6cb758f0 | 40 | #include "cgraph.h" |
2363ef00 | 41 | #include "tree-iterator.h" |
fc40d314 | 42 | #include "vec.h" |
853b7640 | 43 | #include "target.h" |
75a70cf9 | 44 | #include "gimple.h" |
0f71a633 | 45 | #include "bitmap.h" |
d1455aa3 | 46 | #include "hash-table.h" |
0090dad2 | 47 | |
c5fe651b | 48 | static bool verify_constant (tree, bool, bool *, bool *); |
49 | #define VERIFY_CONSTANT(X) \ | |
50 | do { \ | |
51 | if (verify_constant ((X), allow_non_constant, non_constant_p, overflow_p)) \ | |
52 | return t; \ | |
53 | } while (0) | |
54 | ||
0090dad2 | 55 | /* There routines provide a modular interface to perform many parsing |
56 | operations. They may therefore be used during actual parsing, or | |
57 | during template instantiation, which may be regarded as a | |
c1946583 | 58 | degenerate form of parsing. */ |
0090dad2 | 59 | |
807be5b4 | 60 | static tree maybe_convert_cond (tree); |
4ee9c684 | 61 | static tree finalize_nrv_r (tree *, int *, void *); |
a8b75081 | 62 | static tree capture_decltype (tree); |
90e3d9ba | 63 | |
019cf886 | 64 | |
4f62c42e | 65 | /* Deferred Access Checking Overview |
66 | --------------------------------- | |
67 | ||
68 | Most C++ expressions and declarations require access checking | |
69 | to be performed during parsing. However, in several cases, | |
70 | this has to be treated differently. | |
71 | ||
72 | For member declarations, access checking has to be deferred | |
73 | until more information about the declaration is known. For | |
74 | example: | |
75 | ||
76 | class A { | |
653e5405 | 77 | typedef int X; |
4f62c42e | 78 | public: |
653e5405 | 79 | X f(); |
4f62c42e | 80 | }; |
81 | ||
82 | A::X A::f(); | |
83 | A::X g(); | |
84 | ||
85 | When we are parsing the function return type `A::X', we don't | |
86 | really know if this is allowed until we parse the function name. | |
87 | ||
88 | Furthermore, some contexts require that access checking is | |
89 | never performed at all. These include class heads, and template | |
90 | instantiations. | |
91 | ||
92 | Typical use of access checking functions is described here: | |
9031d10b | 93 | |
4f62c42e | 94 | 1. When we enter a context that requires certain access checking |
95 | mode, the function `push_deferring_access_checks' is called with | |
96 | DEFERRING argument specifying the desired mode. Access checking | |
97 | may be performed immediately (dk_no_deferred), deferred | |
98 | (dk_deferred), or not performed (dk_no_check). | |
99 | ||
100 | 2. When a declaration such as a type, or a variable, is encountered, | |
101 | the function `perform_or_defer_access_check' is called. It | |
f1f41a6c | 102 | maintains a vector of all deferred checks. |
4f62c42e | 103 | |
104 | 3. The global `current_class_type' or `current_function_decl' is then | |
105 | setup by the parser. `enforce_access' relies on these information | |
106 | to check access. | |
107 | ||
108 | 4. Upon exiting the context mentioned in step 1, | |
109 | `perform_deferred_access_checks' is called to check all declaration | |
f1f41a6c | 110 | stored in the vector. `pop_deferring_access_checks' is then |
4f62c42e | 111 | called to restore the previous access checking mode. |
112 | ||
113 | In case of parsing error, we simply call `pop_deferring_access_checks' | |
114 | without `perform_deferred_access_checks'. */ | |
115 | ||
fb1e4f4a | 116 | typedef struct GTY(()) deferred_access { |
f1f41a6c | 117 | /* A vector representing name-lookups for which we have deferred |
fc40d314 | 118 | checking access controls. We cannot check the accessibility of |
119 | names used in a decl-specifier-seq until we know what is being | |
120 | declared because code like: | |
121 | ||
9031d10b | 122 | class A { |
653e5405 | 123 | class B {}; |
124 | B* f(); | |
fc40d314 | 125 | } |
126 | ||
127 | A::B* A::f() { return 0; } | |
128 | ||
3369eb76 | 129 | is valid, even though `A::B' is not generally accessible. */ |
f1f41a6c | 130 | vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks; |
9031d10b | 131 | |
fc40d314 | 132 | /* The current mode of access checks. */ |
133 | enum deferring_kind deferring_access_checks_kind; | |
9031d10b | 134 | |
fc40d314 | 135 | } deferred_access; |
fc40d314 | 136 | |
9b57b06b | 137 | /* Data for deferred access checking. */ |
f1f41a6c | 138 | static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack; |
fc40d314 | 139 | static GTY(()) unsigned deferred_access_no_check; |
9b57b06b | 140 | |
141 | /* Save the current deferred access states and start deferred | |
142 | access checking iff DEFER_P is true. */ | |
143 | ||
2a0d3beb | 144 | void |
145 | push_deferring_access_checks (deferring_kind deferring) | |
9b57b06b | 146 | { |
4cab8273 | 147 | /* For context like template instantiation, access checking |
148 | disabling applies to all nested context. */ | |
fc40d314 | 149 | if (deferred_access_no_check || deferring == dk_no_check) |
150 | deferred_access_no_check++; | |
9b57b06b | 151 | else |
fc40d314 | 152 | { |
e82e4eb5 | 153 | deferred_access e = {NULL, deferring}; |
f1f41a6c | 154 | vec_safe_push (deferred_access_stack, e); |
fc40d314 | 155 | } |
9b57b06b | 156 | } |
157 | ||
158 | /* Resume deferring access checks again after we stopped doing | |
159 | this previously. */ | |
160 | ||
2a0d3beb | 161 | void |
162 | resume_deferring_access_checks (void) | |
9b57b06b | 163 | { |
fc40d314 | 164 | if (!deferred_access_no_check) |
f1f41a6c | 165 | deferred_access_stack->last().deferring_access_checks_kind = dk_deferred; |
9b57b06b | 166 | } |
167 | ||
168 | /* Stop deferring access checks. */ | |
169 | ||
2a0d3beb | 170 | void |
171 | stop_deferring_access_checks (void) | |
9b57b06b | 172 | { |
fc40d314 | 173 | if (!deferred_access_no_check) |
f1f41a6c | 174 | deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred; |
9b57b06b | 175 | } |
176 | ||
177 | /* Discard the current deferred access checks and restore the | |
178 | previous states. */ | |
179 | ||
2a0d3beb | 180 | void |
181 | pop_deferring_access_checks (void) | |
9b57b06b | 182 | { |
fc40d314 | 183 | if (deferred_access_no_check) |
184 | deferred_access_no_check--; | |
185 | else | |
f1f41a6c | 186 | deferred_access_stack->pop (); |
9b57b06b | 187 | } |
188 | ||
9031d10b | 189 | /* Returns a TREE_LIST representing the deferred checks. |
190 | The TREE_PURPOSE of each node is the type through which the | |
9b57b06b | 191 | access occurred; the TREE_VALUE is the declaration named. |
192 | */ | |
193 | ||
f1f41a6c | 194 | vec<deferred_access_check, va_gc> * |
2a0d3beb | 195 | get_deferred_access_checks (void) |
9b57b06b | 196 | { |
fc40d314 | 197 | if (deferred_access_no_check) |
198 | return NULL; | |
199 | else | |
f1f41a6c | 200 | return (deferred_access_stack->last().deferred_access_checks); |
9b57b06b | 201 | } |
202 | ||
203 | /* Take current deferred checks and combine with the | |
204 | previous states if we also defer checks previously. | |
205 | Otherwise perform checks now. */ | |
206 | ||
2a0d3beb | 207 | void |
208 | pop_to_parent_deferring_access_checks (void) | |
9b57b06b | 209 | { |
fc40d314 | 210 | if (deferred_access_no_check) |
211 | deferred_access_no_check--; | |
212 | else | |
213 | { | |
f1f41a6c | 214 | vec<deferred_access_check, va_gc> *checks; |
fc40d314 | 215 | deferred_access *ptr; |
216 | ||
f1f41a6c | 217 | checks = (deferred_access_stack->last ().deferred_access_checks); |
fc40d314 | 218 | |
f1f41a6c | 219 | deferred_access_stack->pop (); |
220 | ptr = &deferred_access_stack->last (); | |
fc40d314 | 221 | if (ptr->deferring_access_checks_kind == dk_no_deferred) |
222 | { | |
223 | /* Check access. */ | |
eb833cbe | 224 | perform_access_checks (checks, tf_warning_or_error); |
fc40d314 | 225 | } |
226 | else | |
227 | { | |
228 | /* Merge with parent. */ | |
3369eb76 | 229 | int i, j; |
230 | deferred_access_check *chk, *probe; | |
9031d10b | 231 | |
f1f41a6c | 232 | FOR_EACH_VEC_SAFE_ELT (checks, i, chk) |
fc40d314 | 233 | { |
f1f41a6c | 234 | FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe) |
3369eb76 | 235 | { |
236 | if (probe->binfo == chk->binfo && | |
237 | probe->decl == chk->decl && | |
238 | probe->diag_decl == chk->diag_decl) | |
239 | goto found; | |
240 | } | |
fc40d314 | 241 | /* Insert into parent's checks. */ |
f1f41a6c | 242 | vec_safe_push (ptr->deferred_access_checks, *chk); |
fc40d314 | 243 | found:; |
244 | } | |
245 | } | |
246 | } | |
9b57b06b | 247 | } |
248 | ||
23010bc8 | 249 | /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node |
250 | is the BINFO indicating the qualifying scope used to access the | |
eb833cbe | 251 | DECL node stored in the TREE_VALUE of the node. If CHECKS is empty |
252 | or we aren't in SFINAE context or all the checks succeed return TRUE, | |
253 | otherwise FALSE. */ | |
23010bc8 | 254 | |
eb833cbe | 255 | bool |
f1f41a6c | 256 | perform_access_checks (vec<deferred_access_check, va_gc> *checks, |
eb833cbe | 257 | tsubst_flags_t complain) |
23010bc8 | 258 | { |
3369eb76 | 259 | int i; |
260 | deferred_access_check *chk; | |
5d56c2e0 | 261 | location_t loc = input_location; |
eb833cbe | 262 | bool ok = true; |
3369eb76 | 263 | |
264 | if (!checks) | |
eb833cbe | 265 | return true; |
3369eb76 | 266 | |
f1f41a6c | 267 | FOR_EACH_VEC_SAFE_ELT (checks, i, chk) |
5d56c2e0 | 268 | { |
269 | input_location = chk->loc; | |
eb833cbe | 270 | ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain); |
5d56c2e0 | 271 | } |
272 | ||
273 | input_location = loc; | |
eb833cbe | 274 | return (complain & tf_error) ? true : ok; |
23010bc8 | 275 | } |
276 | ||
895bc691 | 277 | /* Perform the deferred access checks. |
278 | ||
279 | After performing the checks, we still have to keep the list | |
280 | `deferred_access_stack->deferred_access_checks' since we may want | |
281 | to check access for them again later in a different context. | |
282 | For example: | |
283 | ||
284 | class A { | |
285 | typedef int X; | |
286 | static X a; | |
287 | }; | |
288 | A::X A::a, x; // No error for `A::a', error for `x' | |
289 | ||
290 | We have to perform deferred access of `A::X', first with `A::a', | |
eb833cbe | 291 | next with `x'. Return value like perform_access_checks above. */ |
9b57b06b | 292 | |
eb833cbe | 293 | bool |
294 | perform_deferred_access_checks (tsubst_flags_t complain) | |
9b57b06b | 295 | { |
eb833cbe | 296 | return perform_access_checks (get_deferred_access_checks (), complain); |
9b57b06b | 297 | } |
298 | ||
299 | /* Defer checking the accessibility of DECL, when looked up in | |
eb833cbe | 300 | BINFO. DIAG_DECL is the declaration to use to print diagnostics. |
301 | Return value like perform_access_checks above. */ | |
9b57b06b | 302 | |
eb833cbe | 303 | bool |
304 | perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl, | |
305 | tsubst_flags_t complain) | |
9b57b06b | 306 | { |
3369eb76 | 307 | int i; |
fc40d314 | 308 | deferred_access *ptr; |
3369eb76 | 309 | deferred_access_check *chk; |
3369eb76 | 310 | |
9b57b06b | 311 | |
fc40d314 | 312 | /* Exit if we are in a context that no access checking is performed. |
313 | */ | |
314 | if (deferred_access_no_check) | |
eb833cbe | 315 | return true; |
9031d10b | 316 | |
b4df430b | 317 | gcc_assert (TREE_CODE (binfo) == TREE_BINFO); |
c344b0fe | 318 | |
f1f41a6c | 319 | ptr = &deferred_access_stack->last (); |
9031d10b | 320 | |
9b57b06b | 321 | /* If we are not supposed to defer access checks, just check now. */ |
fc40d314 | 322 | if (ptr->deferring_access_checks_kind == dk_no_deferred) |
9b57b06b | 323 | { |
eb833cbe | 324 | bool ok = enforce_access (binfo, decl, diag_decl, complain); |
325 | return (complain & tf_error) ? true : ok; | |
9b57b06b | 326 | } |
9031d10b | 327 | |
9b57b06b | 328 | /* See if we are already going to perform this check. */ |
f1f41a6c | 329 | FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk) |
3369eb76 | 330 | { |
331 | if (chk->decl == decl && chk->binfo == binfo && | |
332 | chk->diag_decl == diag_decl) | |
333 | { | |
eb833cbe | 334 | return true; |
3369eb76 | 335 | } |
336 | } | |
9b57b06b | 337 | /* If not, record the check. */ |
e82e4eb5 | 338 | deferred_access_check new_access = {binfo, decl, diag_decl, input_location}; |
f1f41a6c | 339 | vec_safe_push (ptr->deferred_access_checks, new_access); |
8d96fd47 | 340 | |
341 | return true; | |
342 | } | |
343 | ||
3160db1d | 344 | /* Returns nonzero if the current statement is a full expression, |
5c3247a9 | 345 | i.e. temporaries created during that statement should be destroyed |
346 | at the end of the statement. */ | |
8036397f | 347 | |
5c3247a9 | 348 | int |
807be5b4 | 349 | stmts_are_full_exprs_p (void) |
5c3247a9 | 350 | { |
a08e60ae | 351 | return current_stmt_tree ()->stmts_are_full_exprs_p; |
352 | } | |
353 | ||
d1725120 | 354 | /* T is a statement. Add it to the statement-tree. This is the C++ |
355 | version. The C/ObjC frontends have a slightly different version of | |
356 | this function. */ | |
357 | ||
358 | tree | |
359 | add_stmt (tree t) | |
360 | { | |
361 | enum tree_code code = TREE_CODE (t); | |
362 | ||
363 | if (EXPR_P (t) && code != LABEL_EXPR) | |
364 | { | |
365 | if (!EXPR_HAS_LOCATION (t)) | |
366 | SET_EXPR_LOCATION (t, input_location); | |
367 | ||
368 | /* When we expand a statement-tree, we must know whether or not the | |
369 | statements are full-expressions. We record that fact here. */ | |
370 | STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p (); | |
371 | } | |
372 | ||
373 | /* Add T to the statement-tree. Non-side-effect statements need to be | |
374 | recorded during statement expressions. */ | |
f1f41a6c | 375 | gcc_checking_assert (!stmt_list_stack->is_empty ()); |
d1725120 | 376 | append_to_statement_list_force (t, &cur_stmt_list); |
377 | ||
378 | return t; | |
379 | } | |
380 | ||
b75409ba | 381 | /* Returns the stmt_tree to which statements are currently being added. */ |
a08e60ae | 382 | |
383 | stmt_tree | |
807be5b4 | 384 | current_stmt_tree (void) |
a08e60ae | 385 | { |
9031d10b | 386 | return (cfun |
387 | ? &cfun->language->base.x_stmt_tree | |
a08e60ae | 388 | : &scope_chain->x_stmt_tree); |
5c3247a9 | 389 | } |
8036397f | 390 | |
73d4090e | 391 | /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */ |
392 | ||
393 | static tree | |
394 | maybe_cleanup_point_expr (tree expr) | |
395 | { | |
396 | if (!processing_template_decl && stmts_are_full_exprs_p ()) | |
acbc760a | 397 | expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr); |
73d4090e | 398 | return expr; |
399 | } | |
400 | ||
acbc760a | 401 | /* Like maybe_cleanup_point_expr except have the type of the new expression be |
c6e1336c | 402 | void so we don't need to create a temporary variable to hold the inner |
403 | expression. The reason why we do this is because the original type might be | |
404 | an aggregate and we cannot create a temporary variable for that type. */ | |
acbc760a | 405 | |
3f1ab65c | 406 | tree |
acbc760a | 407 | maybe_cleanup_point_expr_void (tree expr) |
408 | { | |
409 | if (!processing_template_decl && stmts_are_full_exprs_p ()) | |
410 | expr = fold_build_cleanup_point_expr (void_type_node, expr); | |
411 | return expr; | |
412 | } | |
413 | ||
414 | ||
415 | ||
73d4090e | 416 | /* Create a declaration statement for the declaration given by the DECL. */ |
417 | ||
418 | void | |
7dd37241 | 419 | add_decl_expr (tree decl) |
73d4090e | 420 | { |
e60a6f7b | 421 | tree r = build_stmt (input_location, DECL_EXPR, decl); |
56dfff01 | 422 | if (DECL_INITIAL (decl) |
423 | || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl)))) | |
acbc760a | 424 | r = maybe_cleanup_point_expr_void (r); |
73d4090e | 425 | add_stmt (r); |
426 | } | |
427 | ||
5c3247a9 | 428 | /* Finish a scope. */ |
8036397f | 429 | |
d7e71db9 | 430 | tree |
2363ef00 | 431 | do_poplevel (tree stmt_list) |
8036397f | 432 | { |
2363ef00 | 433 | tree block = NULL; |
8036397f | 434 | |
5c3247a9 | 435 | if (stmts_are_full_exprs_p ()) |
2363ef00 | 436 | block = poplevel (kept_level_p (), 1, 0); |
5c3247a9 | 437 | |
2363ef00 | 438 | stmt_list = pop_stmt_list (stmt_list); |
9031d10b | 439 | |
2363ef00 | 440 | if (!processing_template_decl) |
441 | { | |
e60a6f7b | 442 | stmt_list = c_build_bind_expr (input_location, block, stmt_list); |
2363ef00 | 443 | /* ??? See c_end_compound_stmt re statement expressions. */ |
8036397f | 444 | } |
445 | ||
2363ef00 | 446 | return stmt_list; |
8036397f | 447 | } |
448 | ||
9031d10b | 449 | /* Begin a new scope. */ |
8036397f | 450 | |
2363ef00 | 451 | static tree |
6e9029b4 | 452 | do_pushlevel (scope_kind sk) |
8036397f | 453 | { |
2363ef00 | 454 | tree ret = push_stmt_list (); |
5c3247a9 | 455 | if (stmts_are_full_exprs_p ()) |
2363ef00 | 456 | begin_scope (sk, NULL); |
457 | return ret; | |
458 | } | |
dddab69e | 459 | |
460 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed | |
461 | when the current scope is exited. EH_ONLY is true when this is not | |
462 | meant to apply to normal control flow transfer. */ | |
463 | ||
464 | void | |
465 | push_cleanup (tree decl, tree cleanup, bool eh_only) | |
466 | { | |
e60a6f7b | 467 | tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl); |
dddab69e | 468 | CLEANUP_EH_ONLY (stmt) = eh_only; |
469 | add_stmt (stmt); | |
470 | CLEANUP_BODY (stmt) = push_stmt_list (); | |
471 | } | |
2363ef00 | 472 | |
2b4cffe7 | 473 | /* Begin a conditional that might contain a declaration. When generating |
474 | normal code, we want the declaration to appear before the statement | |
475 | containing the conditional. When generating template code, we want the | |
7dd37241 | 476 | conditional to be rendered as the raw DECL_EXPR. */ |
2363ef00 | 477 | |
478 | static void | |
2b4cffe7 | 479 | begin_cond (tree *cond_p) |
2363ef00 | 480 | { |
2b4cffe7 | 481 | if (processing_template_decl) |
482 | *cond_p = push_stmt_list (); | |
483 | } | |
484 | ||
485 | /* Finish such a conditional. */ | |
486 | ||
487 | static void | |
488 | finish_cond (tree *cond_p, tree expr) | |
489 | { | |
490 | if (processing_template_decl) | |
8036397f | 491 | { |
2b4cffe7 | 492 | tree cond = pop_stmt_list (*cond_p); |
d95d815d | 493 | |
c9e4cdb5 | 494 | if (expr == NULL_TREE) |
495 | /* Empty condition in 'for'. */ | |
496 | gcc_assert (empty_expr_stmt_p (cond)); | |
497 | else if (check_for_bare_parameter_packs (expr)) | |
498 | expr = error_mark_node; | |
499 | else if (!empty_expr_stmt_p (cond)) | |
500 | expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr); | |
8036397f | 501 | } |
2b4cffe7 | 502 | *cond_p = expr; |
8036397f | 503 | } |
504 | ||
2363ef00 | 505 | /* If *COND_P specifies a conditional with a declaration, transform the |
506 | loop such that | |
653e5405 | 507 | while (A x = 42) { } |
508 | for (; A x = 42;) { } | |
2363ef00 | 509 | becomes |
653e5405 | 510 | while (true) { A x = 42; if (!x) break; } |
511 | for (;;) { A x = 42; if (!x) break; } | |
2b4cffe7 | 512 | The statement list for BODY will be empty if the conditional did |
513 | not declare anything. */ | |
9031d10b | 514 | |
2363ef00 | 515 | static void |
2b4cffe7 | 516 | simplify_loop_decl_cond (tree *cond_p, tree body) |
2363ef00 | 517 | { |
2b4cffe7 | 518 | tree cond, if_stmt; |
2363ef00 | 519 | |
2b4cffe7 | 520 | if (!TREE_SIDE_EFFECTS (body)) |
521 | return; | |
2363ef00 | 522 | |
2b4cffe7 | 523 | cond = *cond_p; |
524 | *cond_p = boolean_true_node; | |
9031d10b | 525 | |
2b4cffe7 | 526 | if_stmt = begin_if_stmt (); |
ebd21de4 | 527 | cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error); |
2b4cffe7 | 528 | finish_if_stmt_cond (cond, if_stmt); |
529 | finish_break_stmt (); | |
530 | finish_then_clause (if_stmt); | |
531 | finish_if_stmt (if_stmt); | |
532 | } | |
2363ef00 | 533 | |
8036397f | 534 | /* Finish a goto-statement. */ |
535 | ||
4aa0811f | 536 | tree |
807be5b4 | 537 | finish_goto_stmt (tree destination) |
8036397f | 538 | { |
539 | if (TREE_CODE (destination) == IDENTIFIER_NODE) | |
540 | destination = lookup_label (destination); | |
541 | ||
542 | /* We warn about unused labels with -Wunused. That means we have to | |
543 | mark the used labels as used. */ | |
544 | if (TREE_CODE (destination) == LABEL_DECL) | |
545 | TREE_USED (destination) = 1; | |
0eb00403 | 546 | else |
547 | { | |
95f11b21 | 548 | destination = mark_rvalue_use (destination); |
0eb00403 | 549 | if (!processing_template_decl) |
30f461c5 | 550 | { |
c4698a21 | 551 | destination = cp_convert (ptr_type_node, destination, |
552 | tf_warning_or_error); | |
30f461c5 | 553 | if (error_operand_p (destination)) |
554 | return NULL_TREE; | |
9f071b17 | 555 | destination |
556 | = fold_build_cleanup_point_expr (TREE_TYPE (destination), | |
557 | destination); | |
30f461c5 | 558 | } |
0eb00403 | 559 | } |
9031d10b | 560 | |
8036397f | 561 | check_goto (destination); |
562 | ||
e60a6f7b | 563 | return add_stmt (build_stmt (input_location, GOTO_EXPR, destination)); |
8036397f | 564 | } |
565 | ||
18a4cb16 | 566 | /* COND is the condition-expression for an if, while, etc., |
168f96cc | 567 | statement. Convert it to a boolean value, if appropriate. |
568 | In addition, verify sequence points if -Wsequence-point is enabled. */ | |
18a4cb16 | 569 | |
9140e457 | 570 | static tree |
807be5b4 | 571 | maybe_convert_cond (tree cond) |
18a4cb16 | 572 | { |
573 | /* Empty conditions remain empty. */ | |
574 | if (!cond) | |
575 | return NULL_TREE; | |
576 | ||
577 | /* Wait until we instantiate templates before doing conversion. */ | |
578 | if (processing_template_decl) | |
579 | return cond; | |
580 | ||
168f96cc | 581 | if (warn_sequence_point) |
582 | verify_sequence_points (cond); | |
583 | ||
18a4cb16 | 584 | /* Do the conversion. */ |
585 | cond = convert_from_reference (cond); | |
60a0513e | 586 | |
587 | if (TREE_CODE (cond) == MODIFY_EXPR | |
588 | && !TREE_NO_WARNING (cond) | |
589 | && warn_parentheses) | |
590 | { | |
591 | warning (OPT_Wparentheses, | |
592 | "suggest parentheses around assignment used as truth value"); | |
593 | TREE_NO_WARNING (cond) = 1; | |
594 | } | |
595 | ||
18a4cb16 | 596 | return condition_conversion (cond); |
597 | } | |
598 | ||
3da16ddc | 599 | /* Finish an expression-statement, whose EXPRESSION is as indicated. */ |
7f826305 | 600 | |
4aa0811f | 601 | tree |
807be5b4 | 602 | finish_expr_stmt (tree expr) |
0090dad2 | 603 | { |
4aa0811f | 604 | tree r = NULL_TREE; |
605 | ||
d8bbef5c | 606 | if (expr != NULL_TREE) |
0090dad2 | 607 | { |
942ab15b | 608 | if (!processing_template_decl) |
2569a1be | 609 | { |
610 | if (warn_sequence_point) | |
611 | verify_sequence_points (expr); | |
dab3247a | 612 | expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error); |
2569a1be | 613 | } |
11dfb79c | 614 | else if (!type_dependent_expression_p (expr)) |
dab3247a | 615 | convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT, |
ebd21de4 | 616 | tf_warning_or_error); |
2363ef00 | 617 | |
830a6615 | 618 | if (check_for_bare_parameter_packs (expr)) |
613687b1 | 619 | expr = error_mark_node; |
d95d815d | 620 | |
2363ef00 | 621 | /* Simplification of inner statement expressions, compound exprs, |
1f849cd8 | 622 | etc can result in us already having an EXPR_STMT. */ |
73d4090e | 623 | if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) |
624 | { | |
625 | if (TREE_CODE (expr) != EXPR_STMT) | |
e60a6f7b | 626 | expr = build_stmt (input_location, EXPR_STMT, expr); |
acbc760a | 627 | expr = maybe_cleanup_point_expr_void (expr); |
73d4090e | 628 | } |
629 | ||
2363ef00 | 630 | r = add_stmt (expr); |
8036397f | 631 | } |
9f4bf220 | 632 | |
8036397f | 633 | finish_stmt (); |
019cf886 | 634 | |
4aa0811f | 635 | return r; |
8036397f | 636 | } |
637 | ||
8036397f | 638 | |
0090dad2 | 639 | /* Begin an if-statement. Returns a newly created IF_STMT if |
640 | appropriate. */ | |
641 | ||
642 | tree | |
807be5b4 | 643 | begin_if_stmt (void) |
0090dad2 | 644 | { |
2363ef00 | 645 | tree r, scope; |
16878d88 | 646 | scope = do_pushlevel (sk_cond); |
852787ab | 647 | r = build_stmt (input_location, IF_STMT, NULL_TREE, |
648 | NULL_TREE, NULL_TREE, scope); | |
2b4cffe7 | 649 | begin_cond (&IF_COND (r)); |
0090dad2 | 650 | return r; |
651 | } | |
652 | ||
653 | /* Process the COND of an if-statement, which may be given by | |
654 | IF_STMT. */ | |
655 | ||
9031d10b | 656 | void |
807be5b4 | 657 | finish_if_stmt_cond (tree cond, tree if_stmt) |
0090dad2 | 658 | { |
2b4cffe7 | 659 | finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond)); |
660 | add_stmt (if_stmt); | |
2363ef00 | 661 | THEN_CLAUSE (if_stmt) = push_stmt_list (); |
0090dad2 | 662 | } |
663 | ||
664 | /* Finish the then-clause of an if-statement, which may be given by | |
665 | IF_STMT. */ | |
666 | ||
667 | tree | |
807be5b4 | 668 | finish_then_clause (tree if_stmt) |
0090dad2 | 669 | { |
2363ef00 | 670 | THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt)); |
8036397f | 671 | return if_stmt; |
0090dad2 | 672 | } |
673 | ||
674 | /* Begin the else-clause of an if-statement. */ | |
675 | ||
2363ef00 | 676 | void |
677 | begin_else_clause (tree if_stmt) | |
0090dad2 | 678 | { |
2363ef00 | 679 | ELSE_CLAUSE (if_stmt) = push_stmt_list (); |
0090dad2 | 680 | } |
681 | ||
682 | /* Finish the else-clause of an if-statement, which may be given by | |
683 | IF_STMT. */ | |
684 | ||
685 | void | |
807be5b4 | 686 | finish_else_clause (tree if_stmt) |
0090dad2 | 687 | { |
2363ef00 | 688 | ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt)); |
0090dad2 | 689 | } |
690 | ||
2b686fc5 | 691 | /* Finish an if-statement. */ |
0090dad2 | 692 | |
9031d10b | 693 | void |
2363ef00 | 694 | finish_if_stmt (tree if_stmt) |
0090dad2 | 695 | { |
852787ab | 696 | tree scope = IF_SCOPE (if_stmt); |
697 | IF_SCOPE (if_stmt) = NULL; | |
2363ef00 | 698 | add_stmt (do_poplevel (scope)); |
0090dad2 | 699 | finish_stmt (); |
8036397f | 700 | } |
701 | ||
0090dad2 | 702 | /* Begin a while-statement. Returns a newly created WHILE_STMT if |
703 | appropriate. */ | |
704 | ||
705 | tree | |
807be5b4 | 706 | begin_while_stmt (void) |
0090dad2 | 707 | { |
708 | tree r; | |
e60a6f7b | 709 | r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE); |
a08e60ae | 710 | add_stmt (r); |
2363ef00 | 711 | WHILE_BODY (r) = do_pushlevel (sk_block); |
2b4cffe7 | 712 | begin_cond (&WHILE_COND (r)); |
0090dad2 | 713 | return r; |
714 | } | |
715 | ||
7702ef60 | 716 | /* Process the COND of a while-statement, which may be given by |
0090dad2 | 717 | WHILE_STMT. */ |
718 | ||
9031d10b | 719 | void |
807be5b4 | 720 | finish_while_stmt_cond (tree cond, tree while_stmt) |
0090dad2 | 721 | { |
2b4cffe7 | 722 | finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond)); |
723 | simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt)); | |
0090dad2 | 724 | } |
725 | ||
726 | /* Finish a while-statement, which may be given by WHILE_STMT. */ | |
727 | ||
9031d10b | 728 | void |
807be5b4 | 729 | finish_while_stmt (tree while_stmt) |
0090dad2 | 730 | { |
2363ef00 | 731 | WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt)); |
0090dad2 | 732 | finish_stmt (); |
733 | } | |
734 | ||
735 | /* Begin a do-statement. Returns a newly created DO_STMT if | |
736 | appropriate. */ | |
737 | ||
738 | tree | |
807be5b4 | 739 | begin_do_stmt (void) |
0090dad2 | 740 | { |
e60a6f7b | 741 | tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE); |
a08e60ae | 742 | add_stmt (r); |
2363ef00 | 743 | DO_BODY (r) = push_stmt_list (); |
8036397f | 744 | return r; |
0090dad2 | 745 | } |
746 | ||
747 | /* Finish the body of a do-statement, which may be given by DO_STMT. */ | |
748 | ||
749 | void | |
807be5b4 | 750 | finish_do_body (tree do_stmt) |
0090dad2 | 751 | { |
ffe8fd56 | 752 | tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt)); |
753 | ||
754 | if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body)) | |
755 | body = STATEMENT_LIST_TAIL (body)->stmt; | |
756 | ||
757 | if (IS_EMPTY_STMT (body)) | |
758 | warning (OPT_Wempty_body, | |
759 | "suggest explicit braces around empty body in %<do%> statement"); | |
0090dad2 | 760 | } |
761 | ||
762 | /* Finish a do-statement, which may be given by DO_STMT, and whose | |
763 | COND is as indicated. */ | |
764 | ||
765 | void | |
807be5b4 | 766 | finish_do_stmt (tree cond, tree do_stmt) |
0090dad2 | 767 | { |
18a4cb16 | 768 | cond = maybe_convert_cond (cond); |
8036397f | 769 | DO_COND (do_stmt) = cond; |
770 | finish_stmt (); | |
771 | } | |
18a4cb16 | 772 | |
0090dad2 | 773 | /* Finish a return-statement. The EXPRESSION returned, if any, is as |
774 | indicated. */ | |
775 | ||
4aa0811f | 776 | tree |
807be5b4 | 777 | finish_return_stmt (tree expr) |
0090dad2 | 778 | { |
4aa0811f | 779 | tree r; |
81a3c55b | 780 | bool no_warning; |
4aa0811f | 781 | |
81a3c55b | 782 | expr = check_return_expr (expr, &no_warning); |
8487df40 | 783 | |
784 | if (flag_openmp && !check_omp_return ()) | |
785 | return error_mark_node; | |
8036397f | 786 | if (!processing_template_decl) |
51046b65 | 787 | { |
168f96cc | 788 | if (warn_sequence_point) |
789 | verify_sequence_points (expr); | |
790 | ||
853b7640 | 791 | if (DECL_DESTRUCTOR_P (current_function_decl) |
9031d10b | 792 | || (DECL_CONSTRUCTOR_P (current_function_decl) |
853b7640 | 793 | && targetm.cxx.cdtor_returns_this ())) |
51046b65 | 794 | { |
795 | /* Similarly, all destructors must run destructors for | |
796 | base-classes before returning. So, all returns in a | |
2b686fc5 | 797 | destructor get sent to the DTOR_LABEL; finish_function emits |
51046b65 | 798 | code to return a value there. */ |
853b7640 | 799 | return finish_goto_stmt (cdtor_label); |
51046b65 | 800 | } |
801 | } | |
73d4090e | 802 | |
e60a6f7b | 803 | r = build_stmt (input_location, RETURN_EXPR, expr); |
81a3c55b | 804 | TREE_NO_WARNING (r) |= no_warning; |
acbc760a | 805 | r = maybe_cleanup_point_expr_void (r); |
73d4090e | 806 | r = add_stmt (r); |
8036397f | 807 | finish_stmt (); |
4aa0811f | 808 | |
809 | return r; | |
8036397f | 810 | } |
51046b65 | 811 | |
fa7d5870 | 812 | /* Begin the scope of a for-statement or a range-for-statement. |
813 | Both the returned trees are to be used in a call to | |
814 | begin_for_stmt or begin_range_for_stmt. */ | |
0090dad2 | 815 | |
816 | tree | |
fa7d5870 | 817 | begin_for_scope (tree *init) |
818 | { | |
819 | tree scope = NULL_TREE; | |
820 | if (flag_new_for_scope > 0) | |
821 | scope = do_pushlevel (sk_for); | |
822 | ||
823 | if (processing_template_decl) | |
824 | *init = push_stmt_list (); | |
825 | else | |
826 | *init = NULL_TREE; | |
827 | ||
828 | return scope; | |
829 | } | |
830 | ||
831 | /* Begin a for-statement. Returns a new FOR_STMT. | |
832 | SCOPE and INIT should be the return of begin_for_scope, | |
833 | or both NULL_TREE */ | |
834 | ||
835 | tree | |
836 | begin_for_stmt (tree scope, tree init) | |
0090dad2 | 837 | { |
838 | tree r; | |
839 | ||
e60a6f7b | 840 | r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE, |
e45c9c55 | 841 | NULL_TREE, NULL_TREE, NULL_TREE); |
2363ef00 | 842 | |
fa7d5870 | 843 | if (scope == NULL_TREE) |
844 | { | |
1bf4f2e0 | 845 | gcc_assert (!init || !(flag_new_for_scope > 0)); |
846 | if (!init) | |
847 | scope = begin_for_scope (&init); | |
fa7d5870 | 848 | } |
849 | FOR_INIT_STMT (r) = init; | |
e45c9c55 | 850 | FOR_SCOPE (r) = scope; |
5250373a | 851 | |
0090dad2 | 852 | return r; |
853 | } | |
854 | ||
855 | /* Finish the for-init-statement of a for-statement, which may be | |
856 | given by FOR_STMT. */ | |
857 | ||
858 | void | |
807be5b4 | 859 | finish_for_init_stmt (tree for_stmt) |
0090dad2 | 860 | { |
5250373a | 861 | if (processing_template_decl) |
862 | FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt)); | |
2363ef00 | 863 | add_stmt (for_stmt); |
864 | FOR_BODY (for_stmt) = do_pushlevel (sk_block); | |
2b4cffe7 | 865 | begin_cond (&FOR_COND (for_stmt)); |
0090dad2 | 866 | } |
867 | ||
868 | /* Finish the COND of a for-statement, which may be given by | |
869 | FOR_STMT. */ | |
870 | ||
871 | void | |
807be5b4 | 872 | finish_for_cond (tree cond, tree for_stmt) |
0090dad2 | 873 | { |
2b4cffe7 | 874 | finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond)); |
875 | simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt)); | |
0090dad2 | 876 | } |
877 | ||
878 | /* Finish the increment-EXPRESSION in a for-statement, which may be | |
879 | given by FOR_STMT. */ | |
880 | ||
881 | void | |
807be5b4 | 882 | finish_for_expr (tree expr, tree for_stmt) |
0090dad2 | 883 | { |
73d4090e | 884 | if (!expr) |
885 | return; | |
a9d1411a | 886 | /* If EXPR is an overloaded function, issue an error; there is no |
887 | context available to use to perform overload resolution. */ | |
73d4090e | 888 | if (type_unknown_p (expr)) |
a9d1411a | 889 | { |
890 | cxx_incomplete_type_error (expr, TREE_TYPE (expr)); | |
891 | expr = error_mark_node; | |
892 | } | |
1b660ce9 | 893 | if (!processing_template_decl) |
894 | { | |
895 | if (warn_sequence_point) | |
653e5405 | 896 | verify_sequence_points (expr); |
dab3247a | 897 | expr = convert_to_void (expr, ICV_THIRD_IN_FOR, |
ebd21de4 | 898 | tf_warning_or_error); |
1b660ce9 | 899 | } |
900 | else if (!type_dependent_expression_p (expr)) | |
dab3247a | 901 | convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR, |
ebd21de4 | 902 | tf_warning_or_error); |
acbc760a | 903 | expr = maybe_cleanup_point_expr_void (expr); |
830a6615 | 904 | if (check_for_bare_parameter_packs (expr)) |
613687b1 | 905 | expr = error_mark_node; |
8036397f | 906 | FOR_EXPR (for_stmt) = expr; |
0090dad2 | 907 | } |
908 | ||
909 | /* Finish the body of a for-statement, which may be given by | |
910 | FOR_STMT. The increment-EXPR for the loop must be | |
9dd72ec4 | 911 | provided. |
912 | It can also finish RANGE_FOR_STMT. */ | |
0090dad2 | 913 | |
914 | void | |
807be5b4 | 915 | finish_for_stmt (tree for_stmt) |
0090dad2 | 916 | { |
9dd72ec4 | 917 | if (TREE_CODE (for_stmt) == RANGE_FOR_STMT) |
5d3abe4e | 918 | RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt)); |
9dd72ec4 | 919 | else |
5d3abe4e | 920 | FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt)); |
2363ef00 | 921 | |
0090dad2 | 922 | /* Pop the scope for the body of the loop. */ |
5d3abe4e | 923 | if (flag_new_for_scope > 0) |
2363ef00 | 924 | { |
e45c9c55 | 925 | tree scope; |
926 | tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT | |
927 | ? &RANGE_FOR_SCOPE (for_stmt) | |
928 | : &FOR_SCOPE (for_stmt)); | |
929 | scope = *scope_ptr; | |
930 | *scope_ptr = NULL; | |
2363ef00 | 931 | add_stmt (do_poplevel (scope)); |
932 | } | |
933 | ||
9031d10b | 934 | finish_stmt (); |
0090dad2 | 935 | } |
936 | ||
9dd72ec4 | 937 | /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT. |
fa7d5870 | 938 | SCOPE and INIT should be the return of begin_for_scope, |
939 | or both NULL_TREE . | |
9dd72ec4 | 940 | To finish it call finish_for_stmt(). */ |
941 | ||
942 | tree | |
fa7d5870 | 943 | begin_range_for_stmt (tree scope, tree init) |
9dd72ec4 | 944 | { |
945 | tree r; | |
5d3abe4e | 946 | |
9dd72ec4 | 947 | r = build_stmt (input_location, RANGE_FOR_STMT, |
e45c9c55 | 948 | NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE); |
5d3abe4e | 949 | |
fa7d5870 | 950 | if (scope == NULL_TREE) |
951 | { | |
1bf4f2e0 | 952 | gcc_assert (!init || !(flag_new_for_scope > 0)); |
953 | if (!init) | |
954 | scope = begin_for_scope (&init); | |
fa7d5870 | 955 | } |
956 | ||
957 | /* RANGE_FOR_STMTs do not use nor save the init tree, so we | |
958 | pop it now. */ | |
959 | if (init) | |
960 | pop_stmt_list (init); | |
e45c9c55 | 961 | RANGE_FOR_SCOPE (r) = scope; |
9dd72ec4 | 962 | |
963 | return r; | |
964 | } | |
965 | ||
966 | /* Finish the head of a range-based for statement, which may | |
967 | be given by RANGE_FOR_STMT. DECL must be the declaration | |
968 | and EXPR must be the loop expression. */ | |
969 | ||
970 | void | |
971 | finish_range_for_decl (tree range_for_stmt, tree decl, tree expr) | |
972 | { | |
973 | RANGE_FOR_DECL (range_for_stmt) = decl; | |
974 | RANGE_FOR_EXPR (range_for_stmt) = expr; | |
975 | add_stmt (range_for_stmt); | |
976 | RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block); | |
977 | } | |
978 | ||
0090dad2 | 979 | /* Finish a break-statement. */ |
980 | ||
4aa0811f | 981 | tree |
807be5b4 | 982 | finish_break_stmt (void) |
0090dad2 | 983 | { |
e8653c02 | 984 | /* In switch statements break is sometimes stylistically used after |
985 | a return statement. This can lead to spurious warnings about | |
986 | control reaching the end of a non-void function when it is | |
987 | inlined. Note that we are calling block_may_fallthru with | |
988 | language specific tree nodes; this works because | |
989 | block_may_fallthru returns true when given something it does not | |
990 | understand. */ | |
991 | if (!block_may_fallthru (cur_stmt_list)) | |
992 | return void_zero_node; | |
e60a6f7b | 993 | return add_stmt (build_stmt (input_location, BREAK_STMT)); |
8036397f | 994 | } |
995 | ||
0090dad2 | 996 | /* Finish a continue-statement. */ |
997 | ||
4aa0811f | 998 | tree |
807be5b4 | 999 | finish_continue_stmt (void) |
0090dad2 | 1000 | { |
e60a6f7b | 1001 | return add_stmt (build_stmt (input_location, CONTINUE_STMT)); |
0090dad2 | 1002 | } |
1003 | ||
8036397f | 1004 | /* Begin a switch-statement. Returns a new SWITCH_STMT if |
1005 | appropriate. */ | |
1006 | ||
1007 | tree | |
807be5b4 | 1008 | begin_switch_stmt (void) |
8036397f | 1009 | { |
2363ef00 | 1010 | tree r, scope; |
1011 | ||
16878d88 | 1012 | scope = do_pushlevel (sk_cond); |
1f919c50 | 1013 | r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope); |
1014 | ||
ffc8e524 | 1015 | begin_cond (&SWITCH_STMT_COND (r)); |
2363ef00 | 1016 | |
581d6863 | 1017 | return r; |
0090dad2 | 1018 | } |
1019 | ||
581d6863 | 1020 | /* Finish the cond of a switch-statement. */ |
0090dad2 | 1021 | |
581d6863 | 1022 | void |
807be5b4 | 1023 | finish_switch_cond (tree cond, tree switch_stmt) |
0090dad2 | 1024 | { |
79dc3b8e | 1025 | tree orig_type = NULL; |
8036397f | 1026 | if (!processing_template_decl) |
922fb6c7 | 1027 | { |
8036397f | 1028 | /* Convert the condition to an integer or enumeration type. */ |
35771a9a | 1029 | cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true); |
8036397f | 1030 | if (cond == NULL_TREE) |
922fb6c7 | 1031 | { |
8036397f | 1032 | error ("switch quantity not an integer"); |
1033 | cond = error_mark_node; | |
1034 | } | |
79dc3b8e | 1035 | orig_type = TREE_TYPE (cond); |
8036397f | 1036 | if (cond != error_mark_node) |
1037 | { | |
a681799d | 1038 | /* [stmt.switch] |
1039 | ||
1040 | Integral promotions are performed. */ | |
1041 | cond = perform_integral_promotions (cond); | |
73d4090e | 1042 | cond = maybe_cleanup_point_expr (cond); |
922fb6c7 | 1043 | } |
0090dad2 | 1044 | } |
830a6615 | 1045 | if (check_for_bare_parameter_packs (cond)) |
613687b1 | 1046 | cond = error_mark_node; |
168f96cc | 1047 | else if (!processing_template_decl && warn_sequence_point) |
1048 | verify_sequence_points (cond); | |
1049 | ||
ffc8e524 | 1050 | finish_cond (&SWITCH_STMT_COND (switch_stmt), cond); |
1051 | SWITCH_STMT_TYPE (switch_stmt) = orig_type; | |
2b4cffe7 | 1052 | add_stmt (switch_stmt); |
225ec6aa | 1053 | push_switch (switch_stmt); |
ffc8e524 | 1054 | SWITCH_STMT_BODY (switch_stmt) = push_stmt_list (); |
0090dad2 | 1055 | } |
1056 | ||
1057 | /* Finish the body of a switch-statement, which may be given by | |
1058 | SWITCH_STMT. The COND to switch on is indicated. */ | |
1059 | ||
1060 | void | |
807be5b4 | 1061 | finish_switch_stmt (tree switch_stmt) |
0090dad2 | 1062 | { |
2363ef00 | 1063 | tree scope; |
1064 | ||
ffc8e524 | 1065 | SWITCH_STMT_BODY (switch_stmt) = |
1066 | pop_stmt_list (SWITCH_STMT_BODY (switch_stmt)); | |
9031d10b | 1067 | pop_switch (); |
0090dad2 | 1068 | finish_stmt (); |
2363ef00 | 1069 | |
1f919c50 | 1070 | scope = SWITCH_STMT_SCOPE (switch_stmt); |
1071 | SWITCH_STMT_SCOPE (switch_stmt) = NULL; | |
2363ef00 | 1072 | add_stmt (do_poplevel (scope)); |
0090dad2 | 1073 | } |
1074 | ||
0090dad2 | 1075 | /* Begin a try-block. Returns a newly-created TRY_BLOCK if |
1076 | appropriate. */ | |
1077 | ||
1078 | tree | |
807be5b4 | 1079 | begin_try_block (void) |
0090dad2 | 1080 | { |
e60a6f7b | 1081 | tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE); |
a08e60ae | 1082 | add_stmt (r); |
2363ef00 | 1083 | TRY_STMTS (r) = push_stmt_list (); |
8036397f | 1084 | return r; |
0090dad2 | 1085 | } |
1086 | ||
78f7169a | 1087 | /* Likewise, for a function-try-block. The block returned in |
1088 | *COMPOUND_STMT is an artificial outer scope, containing the | |
1089 | function-try-block. */ | |
2c440a13 | 1090 | |
1091 | tree | |
78f7169a | 1092 | begin_function_try_block (tree *compound_stmt) |
2c440a13 | 1093 | { |
78f7169a | 1094 | tree r; |
1095 | /* This outer scope does not exist in the C++ standard, but we need | |
1096 | a place to put __FUNCTION__ and similar variables. */ | |
1097 | *compound_stmt = begin_compound_stmt (0); | |
1098 | r = begin_try_block (); | |
8036397f | 1099 | FN_TRY_BLOCK_P (r) = 1; |
8036397f | 1100 | return r; |
2c440a13 | 1101 | } |
1102 | ||
0090dad2 | 1103 | /* Finish a try-block, which may be given by TRY_BLOCK. */ |
1104 | ||
1105 | void | |
807be5b4 | 1106 | finish_try_block (tree try_block) |
0090dad2 | 1107 | { |
2363ef00 | 1108 | TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); |
1109 | TRY_HANDLERS (try_block) = push_stmt_list (); | |
0090dad2 | 1110 | } |
1111 | ||
938566e5 | 1112 | /* Finish the body of a cleanup try-block, which may be given by |
1113 | TRY_BLOCK. */ | |
1114 | ||
0a8302dc | 1115 | void |
807be5b4 | 1116 | finish_cleanup_try_block (tree try_block) |
0a8302dc | 1117 | { |
2363ef00 | 1118 | TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); |
0a8302dc | 1119 | } |
1120 | ||
b48733fd | 1121 | /* Finish an implicitly generated try-block, with a cleanup is given |
1122 | by CLEANUP. */ | |
1123 | ||
1124 | void | |
807be5b4 | 1125 | finish_cleanup (tree cleanup, tree try_block) |
b48733fd | 1126 | { |
8036397f | 1127 | TRY_HANDLERS (try_block) = cleanup; |
1128 | CLEANUP_P (try_block) = 1; | |
b48733fd | 1129 | } |
1130 | ||
2c440a13 | 1131 | /* Likewise, for a function-try-block. */ |
1132 | ||
1133 | void | |
807be5b4 | 1134 | finish_function_try_block (tree try_block) |
2c440a13 | 1135 | { |
2363ef00 | 1136 | finish_try_block (try_block); |
1137 | /* FIXME : something queer about CTOR_INITIALIZER somehow following | |
1138 | the try block, but moving it inside. */ | |
e0e489c4 | 1139 | in_function_try_handler = 1; |
2c440a13 | 1140 | } |
1141 | ||
0090dad2 | 1142 | /* Finish a handler-sequence for a try-block, which may be given by |
1143 | TRY_BLOCK. */ | |
1144 | ||
1145 | void | |
807be5b4 | 1146 | finish_handler_sequence (tree try_block) |
0090dad2 | 1147 | { |
2363ef00 | 1148 | TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block)); |
8036397f | 1149 | check_handlers (TRY_HANDLERS (try_block)); |
0090dad2 | 1150 | } |
1151 | ||
78f7169a | 1152 | /* Finish the handler-seq for a function-try-block, given by |
1153 | TRY_BLOCK. COMPOUND_STMT is the outer block created by | |
1154 | begin_function_try_block. */ | |
2c440a13 | 1155 | |
1156 | void | |
78f7169a | 1157 | finish_function_handler_sequence (tree try_block, tree compound_stmt) |
2c440a13 | 1158 | { |
e0e489c4 | 1159 | in_function_try_handler = 0; |
2363ef00 | 1160 | finish_handler_sequence (try_block); |
78f7169a | 1161 | finish_compound_stmt (compound_stmt); |
8036397f | 1162 | } |
1163 | ||
0090dad2 | 1164 | /* Begin a handler. Returns a HANDLER if appropriate. */ |
1165 | ||
1166 | tree | |
807be5b4 | 1167 | begin_handler (void) |
0090dad2 | 1168 | { |
1169 | tree r; | |
2363ef00 | 1170 | |
e60a6f7b | 1171 | r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE); |
a08e60ae | 1172 | add_stmt (r); |
2363ef00 | 1173 | |
6993fb0a | 1174 | /* Create a binding level for the eh_info and the exception object |
1175 | cleanup. */ | |
2363ef00 | 1176 | HANDLER_BODY (r) = do_pushlevel (sk_catch); |
1177 | ||
0090dad2 | 1178 | return r; |
1179 | } | |
1180 | ||
1181 | /* Finish the handler-parameters for a handler, which may be given by | |
e0e489c4 | 1182 | HANDLER. DECL is the declaration for the catch parameter, or NULL |
1183 | if this is a `catch (...)' clause. */ | |
0090dad2 | 1184 | |
6993fb0a | 1185 | void |
807be5b4 | 1186 | finish_handler_parms (tree decl, tree handler) |
e0e489c4 | 1187 | { |
6993fb0a | 1188 | tree type = NULL_TREE; |
e0e489c4 | 1189 | if (processing_template_decl) |
1190 | { | |
1191 | if (decl) | |
1192 | { | |
1193 | decl = pushdecl (decl); | |
1194 | decl = push_template_decl (decl); | |
2363ef00 | 1195 | HANDLER_PARMS (handler) = decl; |
6993fb0a | 1196 | type = TREE_TYPE (decl); |
e0e489c4 | 1197 | } |
1198 | } | |
8036397f | 1199 | else |
6993fb0a | 1200 | type = expand_start_catch_block (decl); |
6993fb0a | 1201 | HANDLER_TYPE (handler) = type; |
d6309987 | 1202 | if (!processing_template_decl && type) |
a6a203ec | 1203 | mark_used (eh_type_info (type)); |
8036397f | 1204 | } |
1205 | ||
1206 | /* Finish a handler, which may be given by HANDLER. The BLOCKs are | |
1207 | the return value from the matching call to finish_handler_parms. */ | |
1208 | ||
1209 | void | |
807be5b4 | 1210 | finish_handler (tree handler) |
8036397f | 1211 | { |
1212 | if (!processing_template_decl) | |
6993fb0a | 1213 | expand_end_catch_block (); |
2363ef00 | 1214 | HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler)); |
8036397f | 1215 | } |
1216 | ||
632f8185 | 1217 | /* Begin a compound statement. FLAGS contains some bits that control the |
6cd5db64 | 1218 | behavior and context. If BCS_NO_SCOPE is set, the compound statement |
632f8185 | 1219 | does not define a scope. If BCS_FN_BODY is set, this is the outermost |
9031d10b | 1220 | block of a function. If BCS_TRY_BLOCK is set, this is the block |
632f8185 | 1221 | created on behalf of a TRY statement. Returns a token to be passed to |
1222 | finish_compound_stmt. */ | |
0090dad2 | 1223 | |
1224 | tree | |
2363ef00 | 1225 | begin_compound_stmt (unsigned int flags) |
0090dad2 | 1226 | { |
2363ef00 | 1227 | tree r; |
019cf886 | 1228 | |
2363ef00 | 1229 | if (flags & BCS_NO_SCOPE) |
1230 | { | |
1231 | r = push_stmt_list (); | |
1232 | STATEMENT_LIST_NO_SCOPE (r) = 1; | |
1233 | ||
1234 | /* Normally, we try hard to keep the BLOCK for a statement-expression. | |
1235 | But, if it's a statement-expression with a scopeless block, there's | |
1236 | nothing to keep, and we don't want to accidentally keep a block | |
9031d10b | 1237 | *inside* the scopeless block. */ |
2363ef00 | 1238 | keep_next_level (false); |
1239 | } | |
b48733fd | 1240 | else |
2363ef00 | 1241 | r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block); |
1242 | ||
632f8185 | 1243 | /* When processing a template, we need to remember where the braces were, |
1244 | so that we can set up identical scopes when instantiating the template | |
1245 | later. BIND_EXPR is a handy candidate for this. | |
1246 | Note that do_poplevel won't create a BIND_EXPR itself here (and thus | |
1247 | result in nested BIND_EXPRs), since we don't build BLOCK nodes when | |
1248 | processing templates. */ | |
1249 | if (processing_template_decl) | |
2363ef00 | 1250 | { |
831d52a2 | 1251 | r = build3 (BIND_EXPR, NULL, NULL, r, NULL); |
632f8185 | 1252 | BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0; |
1253 | BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0; | |
2363ef00 | 1254 | TREE_SIDE_EFFECTS (r) = 1; |
1255 | } | |
0090dad2 | 1256 | |
1257 | return r; | |
1258 | } | |
1259 | ||
632f8185 | 1260 | /* Finish a compound-statement, which is given by STMT. */ |
0090dad2 | 1261 | |
2363ef00 | 1262 | void |
1263 | finish_compound_stmt (tree stmt) | |
0090dad2 | 1264 | { |
632f8185 | 1265 | if (TREE_CODE (stmt) == BIND_EXPR) |
3bb40bd8 | 1266 | { |
1267 | tree body = do_poplevel (BIND_EXPR_BODY (stmt)); | |
1268 | /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special, | |
1269 | discard the BIND_EXPR so it can be merged with the containing | |
1270 | STATEMENT_LIST. */ | |
1271 | if (TREE_CODE (body) == STATEMENT_LIST | |
1272 | && STATEMENT_LIST_HEAD (body) == NULL | |
1273 | && !BIND_EXPR_BODY_BLOCK (stmt) | |
1274 | && !BIND_EXPR_TRY_BLOCK (stmt)) | |
1275 | stmt = body; | |
1276 | else | |
1277 | BIND_EXPR_BODY (stmt) = body; | |
1278 | } | |
2363ef00 | 1279 | else if (STATEMENT_LIST_NO_SCOPE (stmt)) |
1280 | stmt = pop_stmt_list (stmt); | |
68f8f8cc | 1281 | else |
b2341f36 | 1282 | { |
1283 | /* Destroy any ObjC "super" receivers that may have been | |
1284 | created. */ | |
1285 | objc_clear_super_receiver (); | |
1286 | ||
1287 | stmt = do_poplevel (stmt); | |
1288 | } | |
0090dad2 | 1289 | |
2363ef00 | 1290 | /* ??? See c_end_compound_stmt wrt statement expressions. */ |
1291 | add_stmt (stmt); | |
0090dad2 | 1292 | finish_stmt (); |
0090dad2 | 1293 | } |
1294 | ||
4ee9c684 | 1295 | /* Finish an asm-statement, whose components are a STRING, some |
78f55ca8 | 1296 | OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some |
1297 | LABELS. Also note whether the asm-statement should be | |
1298 | considered volatile. */ | |
690a42fb | 1299 | |
4aa0811f | 1300 | tree |
4ee9c684 | 1301 | finish_asm_stmt (int volatile_p, tree string, tree output_operands, |
78f55ca8 | 1302 | tree input_operands, tree clobbers, tree labels) |
8036397f | 1303 | { |
1304 | tree r; | |
bf14e7f5 | 1305 | tree t; |
89552023 | 1306 | int ninputs = list_length (input_operands); |
1307 | int noutputs = list_length (output_operands); | |
bf14e7f5 | 1308 | |
bf14e7f5 | 1309 | if (!processing_template_decl) |
37c0f956 | 1310 | { |
8b0d0af6 | 1311 | const char *constraint; |
1312 | const char **oconstraints; | |
1313 | bool allows_mem, allows_reg, is_inout; | |
1314 | tree operand; | |
37c0f956 | 1315 | int i; |
37c0f956 | 1316 | |
fd70b918 | 1317 | oconstraints = XALLOCAVEC (const char *, noutputs); |
8b0d0af6 | 1318 | |
1319 | string = resolve_asm_operand_names (string, output_operands, | |
78f55ca8 | 1320 | input_operands, labels); |
8b0d0af6 | 1321 | |
1322 | for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i) | |
37c0f956 | 1323 | { |
8b0d0af6 | 1324 | operand = TREE_VALUE (t); |
1325 | ||
1326 | /* ??? Really, this should not be here. Users should be using a | |
1327 | proper lvalue, dammit. But there's a long history of using | |
1328 | casts in the output operands. In cases like longlong.h, this | |
1329 | becomes a primitive form of typechecking -- if the cast can be | |
1330 | removed, then the output operand had a type of the proper width; | |
1331 | otherwise we'll get an error. Gross, but ... */ | |
1332 | STRIP_NOPS (operand); | |
1333 | ||
fbb73d9b | 1334 | operand = mark_lvalue_use (operand); |
1335 | ||
ebd21de4 | 1336 | if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error)) |
8b0d0af6 | 1337 | operand = error_mark_node; |
1338 | ||
074ab442 | 1339 | if (operand != error_mark_node |
89552023 | 1340 | && (TREE_READONLY (operand) |
1341 | || CP_TYPE_CONST_P (TREE_TYPE (operand)) | |
074ab442 | 1342 | /* Functions are not modifiable, even though they are |
1343 | lvalues. */ | |
1344 | || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE | |
1345 | || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE | |
1346 | /* If it's an aggregate and any field is const, then it is | |
1347 | effectively const. */ | |
1348 | || (CLASS_TYPE_P (TREE_TYPE (operand)) | |
1349 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand))))) | |
a1f90215 | 1350 | cxx_readonly_error (operand, lv_asm); |
89552023 | 1351 | |
8b0d0af6 | 1352 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); |
1353 | oconstraints[i] = constraint; | |
1354 | ||
1355 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, | |
1356 | &allows_mem, &allows_reg, &is_inout)) | |
1357 | { | |
1358 | /* If the operand is going to end up in memory, | |
1359 | mark it addressable. */ | |
1360 | if (!allows_reg && !cxx_mark_addressable (operand)) | |
1361 | operand = error_mark_node; | |
1362 | } | |
1363 | else | |
1364 | operand = error_mark_node; | |
1365 | ||
1366 | TREE_VALUE (t) = operand; | |
1367 | } | |
1368 | ||
1369 | for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t)) | |
1370 | { | |
1371 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); | |
4405c1ad | 1372 | operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error); |
8b0d0af6 | 1373 | |
37c0f956 | 1374 | /* If the type of the operand hasn't been determined (e.g., |
1375 | because it involves an overloaded function), then issue | |
1376 | an error message. There's no context available to | |
1377 | resolve the overloading. */ | |
8b0d0af6 | 1378 | if (TREE_TYPE (operand) == unknown_type_node) |
37c0f956 | 1379 | { |
9031d10b | 1380 | error ("type of asm operand %qE could not be determined", |
653e5405 | 1381 | TREE_VALUE (t)); |
8b0d0af6 | 1382 | operand = error_mark_node; |
37c0f956 | 1383 | } |
37c0f956 | 1384 | |
8b0d0af6 | 1385 | if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
1386 | oconstraints, &allows_mem, &allows_reg)) | |
37c0f956 | 1387 | { |
8b0d0af6 | 1388 | /* If the operand is going to end up in memory, |
1389 | mark it addressable. */ | |
c6e95001 | 1390 | if (!allows_reg && allows_mem) |
1391 | { | |
1392 | /* Strip the nops as we allow this case. FIXME, this really | |
1393 | should be rejected or made deprecated. */ | |
1394 | STRIP_NOPS (operand); | |
1395 | if (!cxx_mark_addressable (operand)) | |
1396 | operand = error_mark_node; | |
1397 | } | |
37c0f956 | 1398 | } |
8b0d0af6 | 1399 | else |
1400 | operand = error_mark_node; | |
37c0f956 | 1401 | |
8b0d0af6 | 1402 | TREE_VALUE (t) = operand; |
37c0f956 | 1403 | } |
1404 | } | |
bf14e7f5 | 1405 | |
e60a6f7b | 1406 | r = build_stmt (input_location, ASM_EXPR, string, |
389acd0a | 1407 | output_operands, input_operands, |
78f55ca8 | 1408 | clobbers, labels); |
89552023 | 1409 | ASM_VOLATILE_P (r) = volatile_p || noutputs == 0; |
acbc760a | 1410 | r = maybe_cleanup_point_expr_void (r); |
4aa0811f | 1411 | return add_stmt (r); |
0090dad2 | 1412 | } |
01b3f071 | 1413 | |
e1c8f1c5 | 1414 | /* Finish a label with the indicated NAME. Returns the new label. */ |
edf2a96a | 1415 | |
0a3b29ad | 1416 | tree |
807be5b4 | 1417 | finish_label_stmt (tree name) |
edf2a96a | 1418 | { |
92ddaf90 | 1419 | tree decl = define_label (input_location, name); |
b34664af | 1420 | |
e1c8f1c5 | 1421 | if (decl == error_mark_node) |
b34664af | 1422 | return error_mark_node; |
1423 | ||
e60a6f7b | 1424 | add_stmt (build_stmt (input_location, LABEL_EXPR, decl)); |
e1c8f1c5 | 1425 | |
1426 | return decl; | |
edf2a96a | 1427 | } |
1428 | ||
2d46e540 | 1429 | /* Finish a series of declarations for local labels. G++ allows users |
1430 | to declare "local" labels, i.e., labels with scope. This extension | |
1431 | is useful when writing code involving statement-expressions. */ | |
1432 | ||
1433 | void | |
807be5b4 | 1434 | finish_label_decl (tree name) |
2d46e540 | 1435 | { |
cc961cff | 1436 | if (!at_function_scope_p ()) |
1437 | { | |
1438 | error ("__label__ declarations are only allowed in function scopes"); | |
1439 | return; | |
1440 | } | |
1441 | ||
1442 | add_decl_expr (declare_local_label (name)); | |
2d46e540 | 1443 | } |
1444 | ||
a9bc793b | 1445 | /* When DECL goes out of scope, make sure that CLEANUP is executed. */ |
b48733fd | 1446 | |
9031d10b | 1447 | void |
807be5b4 | 1448 | finish_decl_cleanup (tree decl, tree cleanup) |
b48733fd | 1449 | { |
2363ef00 | 1450 | push_cleanup (decl, cleanup, false); |
8036397f | 1451 | } |
1452 | ||
a9bc793b | 1453 | /* If the current scope exits with an exception, run CLEANUP. */ |
2243fa67 | 1454 | |
a9bc793b | 1455 | void |
807be5b4 | 1456 | finish_eh_cleanup (tree cleanup) |
2243fa67 | 1457 | { |
2363ef00 | 1458 | push_cleanup (NULL, cleanup, true); |
8036397f | 1459 | } |
1460 | ||
6507cda8 | 1461 | /* The MEM_INITS is a list of mem-initializers, in reverse of the |
1462 | order they were written by the user. Each node is as for | |
1463 | emit_mem_initializers. */ | |
5fea3263 | 1464 | |
1465 | void | |
6507cda8 | 1466 | finish_mem_initializers (tree mem_inits) |
5fea3263 | 1467 | { |
6507cda8 | 1468 | /* Reorder the MEM_INITS so that they are in the order they appeared |
1469 | in the source program. */ | |
1470 | mem_inits = nreverse (mem_inits); | |
5fea3263 | 1471 | |
fb75f6dc | 1472 | if (processing_template_decl) |
d95d815d | 1473 | { |
1474 | tree mem; | |
1475 | ||
1476 | for (mem = mem_inits; mem; mem = TREE_CHAIN (mem)) | |
1477 | { | |
1478 | /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the | |
1479 | check for bare parameter packs in the TREE_VALUE, because | |
1480 | any parameter packs in the TREE_VALUE have already been | |
1481 | bound as part of the TREE_PURPOSE. See | |
1482 | make_pack_expansion for more information. */ | |
613687b1 | 1483 | if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION |
830a6615 | 1484 | && check_for_bare_parameter_packs (TREE_VALUE (mem))) |
613687b1 | 1485 | TREE_VALUE (mem) = error_mark_node; |
d95d815d | 1486 | } |
1487 | ||
255b5d15 | 1488 | add_stmt (build_min_nt_loc (UNKNOWN_LOCATION, |
1489 | CTOR_INITIALIZER, mem_inits)); | |
d95d815d | 1490 | } |
7e3e1bb9 | 1491 | else |
6507cda8 | 1492 | emit_mem_initializers (mem_inits); |
019cf886 | 1493 | } |
1494 | ||
01b3f071 | 1495 | /* Finish a parenthesized expression EXPR. */ |
1496 | ||
1497 | tree | |
807be5b4 | 1498 | finish_parenthesized_expr (tree expr) |
01b3f071 | 1499 | { |
ce45a448 | 1500 | if (EXPR_P (expr)) |
aff9e656 | 1501 | /* This inhibits warnings in c_common_truthvalue_conversion. */ |
6cec67d6 | 1502 | TREE_NO_WARNING (expr) = 1; |
01b3f071 | 1503 | |
38ba19fa | 1504 | if (TREE_CODE (expr) == OFFSET_REF |
1505 | || TREE_CODE (expr) == SCOPE_REF) | |
30efa7ed | 1506 | /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be |
1507 | enclosed in parentheses. */ | |
1508 | PTRMEM_OK_P (expr) = 0; | |
9031d10b | 1509 | |
a9aacc0c | 1510 | if (TREE_CODE (expr) == STRING_CST) |
1511 | PAREN_STRING_LITERAL_P (expr) = 1; | |
9031d10b | 1512 | |
01b3f071 | 1513 | return expr; |
1514 | } | |
1515 | ||
0a3b29ad | 1516 | /* Finish a reference to a non-static data member (DECL) that is not |
1517 | preceded by `.' or `->'. */ | |
1518 | ||
1519 | tree | |
26d880e6 | 1520 | finish_non_static_data_member (tree decl, tree object, tree qualifying_scope) |
0a3b29ad | 1521 | { |
b4df430b | 1522 | gcc_assert (TREE_CODE (decl) == FIELD_DECL); |
0a3b29ad | 1523 | |
70269a57 | 1524 | if (!object) |
76418e04 | 1525 | { |
76418e04 | 1526 | tree scope = qualifying_scope; |
1527 | if (scope == NULL_TREE) | |
1528 | scope = context_for_name_lookup (decl); | |
1529 | object = maybe_dummy_object (scope, NULL); | |
1530 | } | |
1531 | ||
6e2ac691 | 1532 | if (object == error_mark_node) |
1533 | return error_mark_node; | |
1534 | ||
70269a57 | 1535 | /* DR 613: Can use non-static data members without an associated |
1536 | object in sizeof/decltype/alignof. */ | |
1537 | if (is_dummy_object (object) && cp_unevaluated_operand == 0 | |
1538 | && (!processing_template_decl || !current_class_ref)) | |
0a3b29ad | 1539 | { |
9031d10b | 1540 | if (current_function_decl |
0a3b29ad | 1541 | && DECL_STATIC_FUNCTION_P (current_function_decl)) |
3cf8b391 | 1542 | error ("invalid use of member %q+D in static member function", decl); |
0a3b29ad | 1543 | else |
3cf8b391 | 1544 | error ("invalid use of non-static data member %q+D", decl); |
0a3b29ad | 1545 | error ("from this location"); |
1546 | ||
1547 | return error_mark_node; | |
1548 | } | |
a8b75081 | 1549 | |
76418e04 | 1550 | if (current_class_ptr) |
1551 | TREE_USED (current_class_ptr) = 1; | |
37009aa5 | 1552 | if (processing_template_decl && !qualifying_scope) |
0a3b29ad | 1553 | { |
26d880e6 | 1554 | tree type = TREE_TYPE (decl); |
0a3b29ad | 1555 | |
26d880e6 | 1556 | if (TREE_CODE (type) == REFERENCE_TYPE) |
9cd0fcd1 | 1557 | /* Quals on the object don't matter. */; |
26d880e6 | 1558 | else |
1559 | { | |
331bc0ad | 1560 | /* Set the cv qualifiers. */ |
76418e04 | 1561 | int quals = (current_class_ref |
1562 | ? cp_type_quals (TREE_TYPE (current_class_ref)) | |
1563 | : TYPE_UNQUALIFIED); | |
9031d10b | 1564 | |
26d880e6 | 1565 | if (DECL_MUTABLE_P (decl)) |
1566 | quals &= ~TYPE_QUAL_CONST; | |
03aa2b89 | 1567 | |
26d880e6 | 1568 | quals |= cp_type_quals (TREE_TYPE (decl)); |
1569 | type = cp_build_qualified_type (type, quals); | |
1570 | } | |
9031d10b | 1571 | |
9cd0fcd1 | 1572 | return (convert_from_reference |
1573 | (build_min (COMPONENT_REF, type, object, decl, NULL_TREE))); | |
26d880e6 | 1574 | } |
a55b2063 | 1575 | /* If PROCESSING_TEMPLATE_DECL is nonzero here, then |
1576 | QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF | |
1577 | for now. */ | |
1578 | else if (processing_template_decl) | |
1579 | return build_qualified_name (TREE_TYPE (decl), | |
1580 | qualifying_scope, | |
a59543d9 | 1581 | decl, |
a55b2063 | 1582 | /*template_p=*/false); |
26d880e6 | 1583 | else |
1584 | { | |
1585 | tree access_type = TREE_TYPE (object); | |
5fd824ed | 1586 | |
579bb663 | 1587 | perform_or_defer_access_check (TYPE_BINFO (access_type), decl, |
eb833cbe | 1588 | decl, tf_warning_or_error); |
0a3b29ad | 1589 | |
1590 | /* If the data member was named `C::M', convert `*this' to `C' | |
1591 | first. */ | |
1592 | if (qualifying_scope) | |
1593 | { | |
1594 | tree binfo = NULL_TREE; | |
1595 | object = build_scoped_ref (object, qualifying_scope, | |
1596 | &binfo); | |
1597 | } | |
1598 | ||
1599 | return build_class_member_access_expr (object, decl, | |
1600 | /*access_path=*/NULL_TREE, | |
ebd21de4 | 1601 | /*preserve_reference=*/false, |
1602 | tf_warning_or_error); | |
0a3b29ad | 1603 | } |
1604 | } | |
1605 | ||
b08c3803 | 1606 | /* If we are currently parsing a template and we encountered a typedef |
1607 | TYPEDEF_DECL that is being accessed though CONTEXT, this function | |
1608 | adds the typedef to a list tied to the current template. | |
3817a00d | 1609 | At template instantiation time, that list is walked and access check |
b08c3803 | 1610 | performed for each typedef. |
1611 | LOCATION is the location of the usage point of TYPEDEF_DECL. */ | |
1612 | ||
1613 | void | |
1614 | add_typedef_to_current_template_for_access_check (tree typedef_decl, | |
1615 | tree context, | |
1616 | location_t location) | |
1617 | { | |
1618 | tree template_info = NULL; | |
1619 | tree cs = current_scope (); | |
1620 | ||
1621 | if (!is_typedef_decl (typedef_decl) | |
1622 | || !context | |
1623 | || !CLASS_TYPE_P (context) | |
1624 | || !cs) | |
1625 | return; | |
1626 | ||
1627 | if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL) | |
1628 | template_info = get_template_info (cs); | |
1629 | ||
1630 | if (template_info | |
1631 | && TI_TEMPLATE (template_info) | |
1632 | && !currently_open_class (context)) | |
1633 | append_type_to_template_for_access_check (cs, typedef_decl, | |
1634 | context, location); | |
1635 | } | |
1636 | ||
ef4534a3 | 1637 | /* DECL was the declaration to which a qualified-id resolved. Issue |
1638 | an error message if it is not accessible. If OBJECT_TYPE is | |
1639 | non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the | |
1640 | type of `*x', or `x', respectively. If the DECL was named as | |
1641 | `A::B' then NESTED_NAME_SPECIFIER is `A'. */ | |
1642 | ||
1643 | void | |
9031d10b | 1644 | check_accessibility_of_qualified_id (tree decl, |
1645 | tree object_type, | |
ef4534a3 | 1646 | tree nested_name_specifier) |
1647 | { | |
1648 | tree scope; | |
1649 | tree qualifying_type = NULL_TREE; | |
3cb98335 | 1650 | |
41771881 | 1651 | /* If we are parsing a template declaration and if decl is a typedef, |
1652 | add it to a list tied to the template. | |
1653 | At template instantiation time, that list will be walked and | |
1654 | access check performed. */ | |
b08c3803 | 1655 | add_typedef_to_current_template_for_access_check (decl, |
1656 | nested_name_specifier | |
1657 | ? nested_name_specifier | |
1658 | : DECL_CONTEXT (decl), | |
1659 | input_location); | |
41771881 | 1660 | |
4a44ba29 | 1661 | /* If we're not checking, return immediately. */ |
3cb98335 | 1662 | if (deferred_access_no_check) |
1663 | return; | |
9031d10b | 1664 | |
ef4534a3 | 1665 | /* Determine the SCOPE of DECL. */ |
1666 | scope = context_for_name_lookup (decl); | |
1667 | /* If the SCOPE is not a type, then DECL is not a member. */ | |
1668 | if (!TYPE_P (scope)) | |
1669 | return; | |
1670 | /* Compute the scope through which DECL is being accessed. */ | |
9031d10b | 1671 | if (object_type |
ef4534a3 | 1672 | /* OBJECT_TYPE might not be a class type; consider: |
1673 | ||
1674 | class A { typedef int I; }; | |
1675 | I *p; | |
1676 | p->A::I::~I(); | |
1677 | ||
653e5405 | 1678 | In this case, we will have "A::I" as the DECL, but "I" as the |
ef4534a3 | 1679 | OBJECT_TYPE. */ |
1680 | && CLASS_TYPE_P (object_type) | |
1681 | && DERIVED_FROM_P (scope, object_type)) | |
1682 | /* If we are processing a `->' or `.' expression, use the type of the | |
1683 | left-hand side. */ | |
1684 | qualifying_type = object_type; | |
1685 | else if (nested_name_specifier) | |
1686 | { | |
1687 | /* If the reference is to a non-static member of the | |
1688 | current class, treat it as if it were referenced through | |
1689 | `this'. */ | |
1690 | if (DECL_NONSTATIC_MEMBER_P (decl) | |
1691 | && current_class_ptr | |
1692 | && DERIVED_FROM_P (scope, current_class_type)) | |
1693 | qualifying_type = current_class_type; | |
1694 | /* Otherwise, use the type indicated by the | |
1695 | nested-name-specifier. */ | |
1696 | else | |
1697 | qualifying_type = nested_name_specifier; | |
1698 | } | |
1699 | else | |
1700 | /* Otherwise, the name must be from the current class or one of | |
1701 | its bases. */ | |
1702 | qualifying_type = currently_open_derived_class (scope); | |
1703 | ||
62740469 | 1704 | if (qualifying_type |
1705 | /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM | |
1706 | or similar in a default argument value. */ | |
1707 | && CLASS_TYPE_P (qualifying_type) | |
1708 | && !dependent_type_p (qualifying_type)) | |
579bb663 | 1709 | perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl, |
eb833cbe | 1710 | decl, tf_warning_or_error); |
ef4534a3 | 1711 | } |
1712 | ||
1713 | /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the | |
1714 | class named to the left of the "::" operator. DONE is true if this | |
1715 | expression is a complete postfix-expression; it is false if this | |
1716 | expression is followed by '->', '[', '(', etc. ADDRESS_P is true | |
fbb01da7 | 1717 | iff this expression is the operand of '&'. TEMPLATE_P is true iff |
1718 | the qualified-id was of the form "A::template B". TEMPLATE_ARG_P | |
1719 | is true iff this qualified name appears as a template argument. */ | |
ef4534a3 | 1720 | |
1721 | tree | |
074ab442 | 1722 | finish_qualified_id_expr (tree qualifying_class, |
1723 | tree expr, | |
fbb01da7 | 1724 | bool done, |
074ab442 | 1725 | bool address_p, |
fbb01da7 | 1726 | bool template_p, |
1727 | bool template_arg_p) | |
ef4534a3 | 1728 | { |
528638c9 | 1729 | gcc_assert (TYPE_P (qualifying_class)); |
1730 | ||
2be3e2ee | 1731 | if (error_operand_p (expr)) |
1732 | return error_mark_node; | |
1733 | ||
0638c434 | 1734 | if (DECL_P (expr) || BASELINK_P (expr)) |
528638c9 | 1735 | mark_used (expr); |
528638c9 | 1736 | |
fbb01da7 | 1737 | if (template_p) |
1738 | check_template_keyword (expr); | |
1739 | ||
ef4534a3 | 1740 | /* If EXPR occurs as the operand of '&', use special handling that |
1741 | permits a pointer-to-member. */ | |
1742 | if (address_p && done) | |
1743 | { | |
1744 | if (TREE_CODE (expr) == SCOPE_REF) | |
1745 | expr = TREE_OPERAND (expr, 1); | |
9031d10b | 1746 | expr = build_offset_ref (qualifying_class, expr, |
1bc16cab | 1747 | /*address_p=*/true); |
ef4534a3 | 1748 | return expr; |
1749 | } | |
1750 | ||
fbb01da7 | 1751 | /* Within the scope of a class, turn references to non-static |
1752 | members into expression of the form "this->...". */ | |
1753 | if (template_arg_p) | |
1754 | /* But, within a template argument, we do not want make the | |
1755 | transformation, as there is no "this" pointer. */ | |
1756 | ; | |
1757 | else if (TREE_CODE (expr) == FIELD_DECL) | |
352aca0e | 1758 | { |
1759 | push_deferring_access_checks (dk_no_check); | |
70269a57 | 1760 | expr = finish_non_static_data_member (expr, NULL_TREE, |
352aca0e | 1761 | qualifying_class); |
1762 | pop_deferring_access_checks (); | |
1763 | } | |
ef4534a3 | 1764 | else if (BASELINK_P (expr) && !processing_template_decl) |
1765 | { | |
f7659496 | 1766 | tree ob; |
ef4534a3 | 1767 | |
1768 | /* See if any of the functions are non-static members. */ | |
eac53a7c | 1769 | /* If so, the expression may be relative to 'this'. */ |
1f07118e | 1770 | if (!shared_member_p (expr) |
f7659496 | 1771 | && (ob = maybe_dummy_object (qualifying_class, NULL), |
1772 | !is_dummy_object (ob))) | |
9031d10b | 1773 | expr = (build_class_member_access_expr |
f7659496 | 1774 | (ob, |
ef4534a3 | 1775 | expr, |
1776 | BASELINK_ACCESS_BINFO (expr), | |
ebd21de4 | 1777 | /*preserve_reference=*/false, |
1778 | tf_warning_or_error)); | |
ef4534a3 | 1779 | else if (done) |
1bc16cab | 1780 | /* The expression is a qualified name whose address is not |
1781 | being taken. */ | |
1782 | expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false); | |
ef4534a3 | 1783 | } |
d0b4bf06 | 1784 | else if (BASELINK_P (expr)) |
1785 | ; | |
1786 | else | |
1787 | { | |
d0b4bf06 | 1788 | /* In a template, return a SCOPE_REF for most qualified-ids |
1789 | so that we can check access at instantiation time. But if | |
1790 | we're looking at a member of the current instantiation, we | |
1791 | know we have access and building up the SCOPE_REF confuses | |
1792 | non-type template argument handling. */ | |
1793 | if (processing_template_decl | |
1794 | && !currently_open_class (qualifying_class)) | |
1795 | expr = build_qualified_name (TREE_TYPE (expr), | |
1796 | qualifying_class, expr, | |
1797 | template_p); | |
dbea931d | 1798 | |
1799 | expr = convert_from_reference (expr); | |
d0b4bf06 | 1800 | } |
ef4534a3 | 1801 | |
1802 | return expr; | |
1803 | } | |
1804 | ||
615d03b4 | 1805 | /* Begin a statement-expression. The value returned must be passed to |
1806 | finish_stmt_expr. */ | |
01b3f071 | 1807 | |
9031d10b | 1808 | tree |
807be5b4 | 1809 | begin_stmt_expr (void) |
01b3f071 | 1810 | { |
2363ef00 | 1811 | return push_stmt_list (); |
8036397f | 1812 | } |
1813 | ||
942ab15b | 1814 | /* Process the final expression of a statement expression. EXPR can be |
d6534f57 | 1815 | NULL, if the final expression is empty. Return a STATEMENT_LIST |
1816 | containing all the statements in the statement-expression, or | |
1817 | ERROR_MARK_NODE if there was an error. */ | |
942ab15b | 1818 | |
1819 | tree | |
2363ef00 | 1820 | finish_stmt_expr_expr (tree expr, tree stmt_expr) |
942ab15b | 1821 | { |
41dbd8dc | 1822 | if (error_operand_p (expr)) |
4100761f | 1823 | { |
1824 | /* The type of the statement-expression is the type of the last | |
1825 | expression. */ | |
1826 | TREE_TYPE (stmt_expr) = error_mark_node; | |
1827 | return error_mark_node; | |
1828 | } | |
9031d10b | 1829 | |
d6534f57 | 1830 | /* If the last statement does not have "void" type, then the value |
074ab442 | 1831 | of the last statement is the value of the entire expression. */ |
942ab15b | 1832 | if (expr) |
1833 | { | |
c3d09d4d | 1834 | tree type = TREE_TYPE (expr); |
1835 | ||
1836 | if (processing_template_decl) | |
1837 | { | |
e60a6f7b | 1838 | expr = build_stmt (input_location, EXPR_STMT, expr); |
c3d09d4d | 1839 | expr = add_stmt (expr); |
1840 | /* Mark the last statement so that we can recognize it as such at | |
1841 | template-instantiation time. */ | |
1842 | EXPR_STMT_STMT_EXPR_RESULT (expr) = 1; | |
1843 | } | |
1844 | else if (VOID_TYPE_P (type)) | |
942ab15b | 1845 | { |
c3d09d4d | 1846 | /* Just treat this like an ordinary statement. */ |
1847 | expr = finish_expr_stmt (expr); | |
1848 | } | |
1849 | else | |
1850 | { | |
1851 | /* It actually has a value we need to deal with. First, force it | |
1852 | to be an rvalue so that we won't need to build up a copy | |
1853 | constructor call later when we try to assign it to something. */ | |
9e505437 | 1854 | expr = force_rvalue (expr, tf_warning_or_error); |
d6534f57 | 1855 | if (error_operand_p (expr)) |
1856 | return error_mark_node; | |
c3d09d4d | 1857 | |
1858 | /* Update for array-to-pointer decay. */ | |
bbdcc797 | 1859 | type = TREE_TYPE (expr); |
c3d09d4d | 1860 | |
1861 | /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a | |
1862 | normal statement, but don't convert to void or actually add | |
1863 | the EXPR_STMT. */ | |
1864 | if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) | |
1865 | expr = maybe_cleanup_point_expr (expr); | |
1866 | add_stmt (expr); | |
d6534f57 | 1867 | } |
c3d09d4d | 1868 | |
d6534f57 | 1869 | /* The type of the statement-expression is the type of the last |
1870 | expression. */ | |
1871 | TREE_TYPE (stmt_expr) = type; | |
942ab15b | 1872 | } |
9031d10b | 1873 | |
d6534f57 | 1874 | return stmt_expr; |
942ab15b | 1875 | } |
1876 | ||
face0cb7 | 1877 | /* Finish a statement-expression. EXPR should be the value returned |
1878 | by the previous begin_stmt_expr. Returns an expression | |
1879 | representing the statement-expression. */ | |
01b3f071 | 1880 | |
9031d10b | 1881 | tree |
2363ef00 | 1882 | finish_stmt_expr (tree stmt_expr, bool has_no_scope) |
01b3f071 | 1883 | { |
d6534f57 | 1884 | tree type; |
1885 | tree result; | |
2363ef00 | 1886 | |
d6534f57 | 1887 | if (error_operand_p (stmt_expr)) |
96f59d23 | 1888 | { |
1889 | pop_stmt_list (stmt_expr); | |
1890 | return error_mark_node; | |
1891 | } | |
2363ef00 | 1892 | |
d6534f57 | 1893 | gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST); |
2363ef00 | 1894 | |
d6534f57 | 1895 | type = TREE_TYPE (stmt_expr); |
1896 | result = pop_stmt_list (stmt_expr); | |
c3d09d4d | 1897 | TREE_TYPE (result) = type; |
76a24869 | 1898 | |
942ab15b | 1899 | if (processing_template_decl) |
2363ef00 | 1900 | { |
1901 | result = build_min (STMT_EXPR, type, result); | |
1902 | TREE_SIDE_EFFECTS (result) = 1; | |
1903 | STMT_EXPR_NO_SCOPE (result) = has_no_scope; | |
1904 | } | |
c3d09d4d | 1905 | else if (CLASS_TYPE_P (type)) |
942ab15b | 1906 | { |
c3d09d4d | 1907 | /* Wrap the statement-expression in a TARGET_EXPR so that the |
1908 | temporary object created by the final expression is destroyed at | |
1909 | the end of the full-expression containing the | |
1910 | statement-expression. */ | |
9e505437 | 1911 | result = force_target_expr (type, result, tf_warning_or_error); |
942ab15b | 1912 | } |
2363ef00 | 1913 | |
01b3f071 | 1914 | return result; |
1915 | } | |
1916 | ||
ffc6c453 | 1917 | /* Returns the expression which provides the value of STMT_EXPR. */ |
1918 | ||
1919 | tree | |
1920 | stmt_expr_value_expr (tree stmt_expr) | |
1921 | { | |
1922 | tree t = STMT_EXPR_STMT (stmt_expr); | |
1923 | ||
1924 | if (TREE_CODE (t) == BIND_EXPR) | |
1925 | t = BIND_EXPR_BODY (t); | |
1926 | ||
e377c26a | 1927 | if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t)) |
ffc6c453 | 1928 | t = STATEMENT_LIST_TAIL (t)->stmt; |
1929 | ||
1930 | if (TREE_CODE (t) == EXPR_STMT) | |
1931 | t = EXPR_STMT_EXPR (t); | |
1932 | ||
1933 | return t; | |
1934 | } | |
1935 | ||
d9655b31 | 1936 | /* Return TRUE iff EXPR_STMT is an empty list of |
1937 | expression statements. */ | |
1938 | ||
1939 | bool | |
1940 | empty_expr_stmt_p (tree expr_stmt) | |
1941 | { | |
1942 | tree body = NULL_TREE; | |
1943 | ||
7a86e244 | 1944 | if (expr_stmt == void_zero_node) |
1945 | return true; | |
1946 | ||
d9655b31 | 1947 | if (expr_stmt) |
1948 | { | |
1949 | if (TREE_CODE (expr_stmt) == EXPR_STMT) | |
1950 | body = EXPR_STMT_EXPR (expr_stmt); | |
1951 | else if (TREE_CODE (expr_stmt) == STATEMENT_LIST) | |
1952 | body = expr_stmt; | |
1953 | } | |
1954 | ||
7a86e244 | 1955 | if (body) |
1956 | { | |
1957 | if (TREE_CODE (body) == STATEMENT_LIST) | |
1958 | return tsi_end_p (tsi_start (body)); | |
1959 | else | |
1960 | return empty_expr_stmt_p (body); | |
1961 | } | |
d9655b31 | 1962 | return false; |
1963 | } | |
1964 | ||
0886adbc | 1965 | /* Perform Koenig lookup. FN is the postfix-expression representing |
b1ea83f4 | 1966 | the function (or functions) to call; ARGS are the arguments to the |
9dd72ec4 | 1967 | call; if INCLUDE_STD then the `std' namespace is automatically |
1968 | considered an associated namespace (used in range-based for loops). | |
1969 | Returns the functions to be considered by overload resolution. */ | |
0886adbc | 1970 | |
1971 | tree | |
f1f41a6c | 1972 | perform_koenig_lookup (tree fn, vec<tree, va_gc> *args, bool include_std, |
8411500a | 1973 | tsubst_flags_t complain) |
0886adbc | 1974 | { |
1975 | tree identifier = NULL_TREE; | |
1976 | tree functions = NULL_TREE; | |
32f71e4f | 1977 | tree tmpl_args = NULL_TREE; |
d69c0c73 | 1978 | bool template_id = false; |
32f71e4f | 1979 | |
1980 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
1981 | { | |
d69c0c73 | 1982 | /* Use a separate flag to handle null args. */ |
1983 | template_id = true; | |
32f71e4f | 1984 | tmpl_args = TREE_OPERAND (fn, 1); |
1985 | fn = TREE_OPERAND (fn, 0); | |
1986 | } | |
0886adbc | 1987 | |
1988 | /* Find the name of the overloaded function. */ | |
1989 | if (TREE_CODE (fn) == IDENTIFIER_NODE) | |
1990 | identifier = fn; | |
1991 | else if (is_overloaded_fn (fn)) | |
1992 | { | |
1993 | functions = fn; | |
1994 | identifier = DECL_NAME (get_first_fn (functions)); | |
1995 | } | |
1996 | else if (DECL_P (fn)) | |
1997 | { | |
1998 | functions = fn; | |
1999 | identifier = DECL_NAME (fn); | |
2000 | } | |
2001 | ||
2002 | /* A call to a namespace-scope function using an unqualified name. | |
2003 | ||
2004 | Do Koenig lookup -- unless any of the arguments are | |
2005 | type-dependent. */ | |
32f71e4f | 2006 | if (!any_type_dependent_arguments_p (args) |
2007 | && !any_dependent_template_arguments_p (tmpl_args)) | |
0886adbc | 2008 | { |
9dd72ec4 | 2009 | fn = lookup_arg_dependent (identifier, functions, args, include_std); |
0886adbc | 2010 | if (!fn) |
8411500a | 2011 | { |
2012 | /* The unqualified name could not be resolved. */ | |
2013 | if (complain) | |
2014 | fn = unqualified_fn_lookup_error (identifier); | |
2015 | else | |
2016 | fn = identifier; | |
2017 | } | |
0886adbc | 2018 | } |
0886adbc | 2019 | |
d69c0c73 | 2020 | if (fn && template_id) |
2021 | fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args); | |
32f71e4f | 2022 | |
0886adbc | 2023 | return fn; |
2024 | } | |
2025 | ||
f352a3fb | 2026 | /* Generate an expression for `FN (ARGS)'. This may change the |
2027 | contents of ARGS. | |
f70cb9e6 | 2028 | |
2029 | If DISALLOW_VIRTUAL is true, the call to FN will be not generated | |
2030 | as a virtual call, even if FN is virtual. (This flag is set when | |
2031 | encountering an expression where the function name is explicitly | |
2032 | qualified. For example a call to `X::f' never generates a virtual | |
2033 | call.) | |
2034 | ||
2035 | Returns code for the call. */ | |
01b3f071 | 2036 | |
9031d10b | 2037 | tree |
f1f41a6c | 2038 | finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual, |
f352a3fb | 2039 | bool koenig_p, tsubst_flags_t complain) |
01b3f071 | 2040 | { |
13795292 | 2041 | tree result; |
2042 | tree orig_fn; | |
f1f41a6c | 2043 | vec<tree, va_gc> *orig_args = NULL; |
13795292 | 2044 | |
f352a3fb | 2045 | if (fn == error_mark_node) |
f70cb9e6 | 2046 | return error_mark_node; |
2047 | ||
1b8d97ae | 2048 | gcc_assert (!TYPE_P (fn)); |
8257afbc | 2049 | |
13795292 | 2050 | orig_fn = fn; |
13795292 | 2051 | |
2052 | if (processing_template_decl) | |
2053 | { | |
daf9237f | 2054 | /* If the call expression is dependent, build a CALL_EXPR node |
2055 | with no type; type_dependent_expression_p recognizes | |
2056 | expressions with no type as being dependent. */ | |
13795292 | 2057 | if (type_dependent_expression_p (fn) |
daf9237f | 2058 | || any_type_dependent_arguments_p (*args) |
51e6e22c | 2059 | /* For a non-static member function that doesn't have an |
2060 | explicit object argument, we need to specifically | |
daf9237f | 2061 | test the type dependency of the "this" pointer because it |
2062 | is not included in *ARGS even though it is considered to | |
2063 | be part of the list of arguments. Note that this is | |
2064 | related to CWG issues 515 and 1005. */ | |
51e6e22c | 2065 | || (TREE_CODE (fn) != COMPONENT_REF |
2066 | && non_static_member_function_p (fn) | |
daf9237f | 2067 | && current_class_ref |
2068 | && type_dependent_expression_p (current_class_ref))) | |
cbce34a5 | 2069 | { |
f352a3fb | 2070 | result = build_nt_call_vec (fn, *args); |
8d631348 | 2071 | SET_EXPR_LOCATION (result, EXPR_LOC_OR_HERE (fn)); |
ba026663 | 2072 | KOENIG_LOOKUP_P (result) = koenig_p; |
2889212c | 2073 | if (cfun) |
2074 | { | |
2075 | do | |
2076 | { | |
2077 | tree fndecl = OVL_CURRENT (fn); | |
2078 | if (TREE_CODE (fndecl) != FUNCTION_DECL | |
2079 | || !TREE_THIS_VOLATILE (fndecl)) | |
2080 | break; | |
2081 | fn = OVL_NEXT (fn); | |
2082 | } | |
2083 | while (fn); | |
2084 | if (!fn) | |
2085 | current_function_returns_abnormally = 1; | |
2086 | } | |
cbce34a5 | 2087 | return result; |
2088 | } | |
f352a3fb | 2089 | orig_args = make_tree_vector_copy (*args); |
13795292 | 2090 | if (!BASELINK_P (fn) |
2091 | && TREE_CODE (fn) != PSEUDO_DTOR_EXPR | |
2092 | && TREE_TYPE (fn) != unknown_type_node) | |
2093 | fn = build_non_dependent_expr (fn); | |
f352a3fb | 2094 | make_args_non_dependent (*args); |
13795292 | 2095 | } |
2096 | ||
4e81a9f3 | 2097 | if (TREE_CODE (fn) == COMPONENT_REF) |
2098 | { | |
2099 | tree member = TREE_OPERAND (fn, 1); | |
2100 | if (BASELINK_P (member)) | |
2101 | { | |
2102 | tree object = TREE_OPERAND (fn, 0); | |
2103 | return build_new_method_call (object, member, | |
2104 | args, NULL_TREE, | |
2105 | (disallow_virtual | |
2106 | ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | |
2107 | : LOOKUP_NORMAL), | |
2108 | /*fn_p=*/NULL, | |
2109 | complain); | |
2110 | } | |
2111 | } | |
2112 | ||
0e5cde0c | 2113 | if (is_overloaded_fn (fn)) |
2114 | fn = baselink_for_fns (fn); | |
0a3b29ad | 2115 | |
13795292 | 2116 | result = NULL_TREE; |
f70cb9e6 | 2117 | if (BASELINK_P (fn)) |
83a31b40 | 2118 | { |
f70cb9e6 | 2119 | tree object; |
2120 | ||
2121 | /* A call to a member function. From [over.call.func]: | |
2122 | ||
2123 | If the keyword this is in scope and refers to the class of | |
2124 | that member function, or a derived class thereof, then the | |
2125 | function call is transformed into a qualified function call | |
2126 | using (*this) as the postfix-expression to the left of the | |
2127 | . operator.... [Otherwise] a contrived object of type T | |
9031d10b | 2128 | becomes the implied object argument. |
f70cb9e6 | 2129 | |
f7659496 | 2130 | In this situation: |
f70cb9e6 | 2131 | |
2132 | struct A { void f(); }; | |
2133 | struct B : public A {}; | |
2134 | struct C : public A { void g() { B::f(); }}; | |
2135 | ||
f7659496 | 2136 | "the class of that member function" refers to `A'. But 11.2 |
2137 | [class.access.base] says that we need to convert 'this' to B* as | |
2138 | part of the access, so we pass 'B' to maybe_dummy_object. */ | |
01b3f071 | 2139 | |
f7659496 | 2140 | object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)), |
2141 | NULL); | |
01b3f071 | 2142 | |
13795292 | 2143 | if (processing_template_decl) |
2144 | { | |
2145 | if (type_dependent_expression_p (object)) | |
f352a3fb | 2146 | { |
2147 | tree ret = build_nt_call_vec (orig_fn, orig_args); | |
2148 | release_tree_vector (orig_args); | |
2149 | return ret; | |
2150 | } | |
13795292 | 2151 | object = build_non_dependent_expr (object); |
2152 | } | |
2153 | ||
2154 | result = build_new_method_call (object, fn, args, NULL_TREE, | |
9031d10b | 2155 | (disallow_virtual |
884a66e4 | 2156 | ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL |
2157 | : LOOKUP_NORMAL), | |
ebd21de4 | 2158 | /*fn_p=*/NULL, |
2159 | complain); | |
f70cb9e6 | 2160 | } |
2161 | else if (is_overloaded_fn (fn)) | |
87daa93b | 2162 | { |
2163 | /* If the function is an overloaded builtin, resolve it. */ | |
2164 | if (TREE_CODE (fn) == FUNCTION_DECL | |
65441f6f | 2165 | && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL |
2166 | || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD)) | |
e60a6f7b | 2167 | result = resolve_overloaded_builtin (input_location, fn, *args); |
87daa93b | 2168 | |
2169 | if (!result) | |
121296ee | 2170 | { |
2171 | if (warn_sizeof_pointer_memaccess | |
f1f41a6c | 2172 | && !vec_safe_is_empty (*args) |
121296ee | 2173 | && !processing_template_decl) |
2174 | { | |
57f872a2 | 2175 | location_t sizeof_arg_loc[3]; |
2176 | tree sizeof_arg[3]; | |
2177 | unsigned int i; | |
2178 | for (i = 0; i < 3; i++) | |
2179 | { | |
2180 | tree t; | |
2181 | ||
2182 | sizeof_arg_loc[i] = UNKNOWN_LOCATION; | |
2183 | sizeof_arg[i] = NULL_TREE; | |
f1f41a6c | 2184 | if (i >= (*args)->length ()) |
57f872a2 | 2185 | continue; |
f1f41a6c | 2186 | t = (**args)[i]; |
57f872a2 | 2187 | if (TREE_CODE (t) != SIZEOF_EXPR) |
2188 | continue; | |
2189 | if (SIZEOF_EXPR_TYPE_P (t)) | |
2190 | sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0)); | |
2191 | else | |
2192 | sizeof_arg[i] = TREE_OPERAND (t, 0); | |
2193 | sizeof_arg_loc[i] = EXPR_LOCATION (t); | |
2194 | } | |
121296ee | 2195 | sizeof_pointer_memaccess_warning |
57f872a2 | 2196 | (sizeof_arg_loc, fn, *args, |
121296ee | 2197 | sizeof_arg, same_type_ignoring_top_level_qualifiers_p); |
2198 | } | |
2199 | ||
2200 | /* A call to a namespace-scope function. */ | |
2201 | result = build_new_function_call (fn, args, koenig_p, complain); | |
2202 | } | |
87daa93b | 2203 | } |
0a3b29ad | 2204 | else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR) |
2205 | { | |
f1f41a6c | 2206 | if (!vec_safe_is_empty (*args)) |
0a3b29ad | 2207 | error ("arguments to destructor are not allowed"); |
2208 | /* Mark the pseudo-destructor call as having side-effects so | |
2209 | that we do not issue warnings about its use. */ | |
2210 | result = build1 (NOP_EXPR, | |
2211 | void_type_node, | |
2212 | TREE_OPERAND (fn, 0)); | |
2213 | TREE_SIDE_EFFECTS (result) = 1; | |
0a3b29ad | 2214 | } |
f70cb9e6 | 2215 | else if (CLASS_TYPE_P (TREE_TYPE (fn))) |
13795292 | 2216 | /* If the "function" is really an object of class type, it might |
2217 | have an overloaded `operator ()'. */ | |
f352a3fb | 2218 | result = build_op_call (fn, args, complain); |
87daa93b | 2219 | |
13795292 | 2220 | if (!result) |
2221 | /* A call where the function is unknown. */ | |
f352a3fb | 2222 | result = cp_build_function_call_vec (fn, args, complain); |
f70cb9e6 | 2223 | |
f5126e46 | 2224 | if (processing_template_decl && result != error_mark_node) |
cbce34a5 | 2225 | { |
f5126e46 | 2226 | if (TREE_CODE (result) == INDIRECT_REF) |
2227 | result = TREE_OPERAND (result, 0); | |
f352a3fb | 2228 | result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args); |
330f36dc | 2229 | SET_EXPR_LOCATION (result, input_location); |
ba026663 | 2230 | KOENIG_LOOKUP_P (result) = koenig_p; |
f352a3fb | 2231 | release_tree_vector (orig_args); |
f5126e46 | 2232 | result = convert_from_reference (result); |
cbce34a5 | 2233 | } |
f352a3fb | 2234 | |
b75fa3a6 | 2235 | if (koenig_p) |
2236 | { | |
2237 | /* Free garbage OVERLOADs from arg-dependent lookup. */ | |
2238 | tree next = NULL_TREE; | |
2239 | for (fn = orig_fn; | |
2240 | fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn); | |
2241 | fn = next) | |
2242 | { | |
2243 | if (processing_template_decl) | |
2244 | /* In a template, we'll re-use them at instantiation time. */ | |
2245 | OVL_ARG_DEPENDENT (fn) = false; | |
2246 | else | |
2247 | { | |
2248 | next = OVL_CHAIN (fn); | |
2249 | ggc_free (fn); | |
2250 | } | |
2251 | } | |
2252 | } | |
2253 | ||
13795292 | 2254 | return result; |
01b3f071 | 2255 | } |
2256 | ||
2257 | /* Finish a call to a postfix increment or decrement or EXPR. (Which | |
2258 | is indicated by CODE, which should be POSTINCREMENT_EXPR or | |
2259 | POSTDECREMENT_EXPR.) */ | |
2260 | ||
9031d10b | 2261 | tree |
807be5b4 | 2262 | finish_increment_expr (tree expr, enum tree_code code) |
01b3f071 | 2263 | { |
ef0b0c72 | 2264 | return build_x_unary_op (input_location, code, expr, tf_warning_or_error); |
01b3f071 | 2265 | } |
2266 | ||
2267 | /* Finish a use of `this'. Returns an expression for `this'. */ | |
2268 | ||
9031d10b | 2269 | tree |
807be5b4 | 2270 | finish_this_expr (void) |
01b3f071 | 2271 | { |
2272 | tree result; | |
2273 | ||
43936711 | 2274 | if (current_class_ptr) |
2275 | { | |
2276 | tree type = TREE_TYPE (current_class_ref); | |
2277 | ||
2278 | /* In a lambda expression, 'this' refers to the captured 'this'. */ | |
2279 | if (LAMBDA_TYPE_P (type)) | |
2280 | result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type)); | |
2281 | else | |
2282 | result = current_class_ptr; | |
2283 | ||
2284 | } | |
01b3f071 | 2285 | else if (current_function_decl |
2286 | && DECL_STATIC_FUNCTION_P (current_function_decl)) | |
2287 | { | |
1e5fcbe2 | 2288 | error ("%<this%> is unavailable for static member functions"); |
01b3f071 | 2289 | result = error_mark_node; |
2290 | } | |
2291 | else | |
2292 | { | |
2293 | if (current_function_decl) | |
1e5fcbe2 | 2294 | error ("invalid use of %<this%> in non-member function"); |
01b3f071 | 2295 | else |
1e5fcbe2 | 2296 | error ("invalid use of %<this%> at top level"); |
01b3f071 | 2297 | result = error_mark_node; |
2298 | } | |
2299 | ||
2300 | return result; | |
2301 | } | |
2302 | ||
0a3b29ad | 2303 | /* Finish a pseudo-destructor expression. If SCOPE is NULL, the |
2304 | expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is | |
2305 | the TYPE for the type given. If SCOPE is non-NULL, the expression | |
2306 | was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */ | |
01b3f071 | 2307 | |
9031d10b | 2308 | tree |
807be5b4 | 2309 | finish_pseudo_destructor_expr (tree object, tree scope, tree destructor) |
01b3f071 | 2310 | { |
a355acc3 | 2311 | if (object == error_mark_node || destructor == error_mark_node) |
0a3b29ad | 2312 | return error_mark_node; |
8318ad7a | 2313 | |
b4df430b | 2314 | gcc_assert (TYPE_P (destructor)); |
01b3f071 | 2315 | |
0a3b29ad | 2316 | if (!processing_template_decl) |
2317 | { | |
2318 | if (scope == error_mark_node) | |
2319 | { | |
2320 | error ("invalid qualifying scope in pseudo-destructor name"); | |
2321 | return error_mark_node; | |
2322 | } | |
d05f521d | 2323 | if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor)) |
2324 | { | |
2325 | error ("qualified type %qT does not match destructor name ~%qT", | |
2326 | scope, destructor); | |
2327 | return error_mark_node; | |
2328 | } | |
2329 | ||
9031d10b | 2330 | |
d45cef9b | 2331 | /* [expr.pseudo] says both: |
2332 | ||
653e5405 | 2333 | The type designated by the pseudo-destructor-name shall be |
d45cef9b | 2334 | the same as the object type. |
2335 | ||
653e5405 | 2336 | and: |
d45cef9b | 2337 | |
653e5405 | 2338 | The cv-unqualified versions of the object type and of the |
d45cef9b | 2339 | type designated by the pseudo-destructor-name shall be the |
2340 | same type. | |
2341 | ||
653e5405 | 2342 | We implement the more generous second sentence, since that is |
2343 | what most other compilers do. */ | |
9031d10b | 2344 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object), |
d45cef9b | 2345 | destructor)) |
0a3b29ad | 2346 | { |
03d6ca9e | 2347 | error ("%qE is not of type %qT", object, destructor); |
0a3b29ad | 2348 | return error_mark_node; |
2349 | } | |
2350 | } | |
01b3f071 | 2351 | |
831d52a2 | 2352 | return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor); |
01b3f071 | 2353 | } |
2354 | ||
d8bbef5c | 2355 | /* Finish an expression of the form CODE EXPR. */ |
2356 | ||
2357 | tree | |
ef0b0c72 | 2358 | finish_unary_op_expr (location_t loc, enum tree_code code, tree expr) |
d8bbef5c | 2359 | { |
ef0b0c72 | 2360 | tree result = build_x_unary_op (loc, code, expr, tf_warning_or_error); |
f170d67f | 2361 | if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr)) |
e60a6f7b | 2362 | overflow_warning (input_location, result); |
f170d67f | 2363 | |
d8bbef5c | 2364 | return result; |
2365 | } | |
2366 | ||
0a3b29ad | 2367 | /* Finish a compound-literal expression. TYPE is the type to which |
f82f1250 | 2368 | the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */ |
0a3b29ad | 2369 | |
2370 | tree | |
95034afb | 2371 | finish_compound_literal (tree type, tree compound_literal, |
2372 | tsubst_flags_t complain) | |
0a3b29ad | 2373 | { |
cc88e3b2 | 2374 | if (type == error_mark_node) |
2375 | return error_mark_node; | |
2376 | ||
604fac7f | 2377 | if (TREE_CODE (type) == REFERENCE_TYPE) |
2378 | { | |
2379 | compound_literal | |
2380 | = finish_compound_literal (TREE_TYPE (type), compound_literal, | |
2381 | complain); | |
2382 | return cp_build_c_cast (type, compound_literal, complain); | |
2383 | } | |
2384 | ||
ac4d57eb | 2385 | if (!TYPE_OBJ_P (type)) |
2386 | { | |
95034afb | 2387 | if (complain & tf_error) |
2388 | error ("compound literal of non-object type %qT", type); | |
ac4d57eb | 2389 | return error_mark_node; |
2390 | } | |
2391 | ||
0a3b29ad | 2392 | if (processing_template_decl) |
0a3b29ad | 2393 | { |
788f3e3e | 2394 | TREE_TYPE (compound_literal) = type; |
2395 | /* Mark the expression as a compound literal. */ | |
2396 | TREE_HAS_CONSTRUCTOR (compound_literal) = 1; | |
2397 | return compound_literal; | |
0a3b29ad | 2398 | } |
2399 | ||
23e81e2e | 2400 | type = complete_type (type); |
f82f1250 | 2401 | |
2402 | if (TYPE_NON_AGGREGATE_CLASS (type)) | |
2403 | { | |
2404 | /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST | |
2405 | everywhere that deals with function arguments would be a pain, so | |
2406 | just wrap it in a TREE_LIST. The parser set a flag so we know | |
2407 | that it came from T{} rather than T({}). */ | |
2408 | CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1; | |
2409 | compound_literal = build_tree_list (NULL_TREE, compound_literal); | |
95034afb | 2410 | return build_functional_cast (type, compound_literal, complain); |
f82f1250 | 2411 | } |
2412 | ||
bc7a08da | 2413 | if (TREE_CODE (type) == ARRAY_TYPE |
2414 | && check_array_initializer (NULL_TREE, type, compound_literal)) | |
2415 | return error_mark_node; | |
b52bd4ae | 2416 | compound_literal = reshape_init (type, compound_literal, complain); |
7e783eb3 | 2417 | if (SCALAR_TYPE_P (type) |
af8f1e3f | 2418 | && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal) |
2419 | && (complain & tf_warning_or_error)) | |
8d49ffe6 | 2420 | check_narrowing (type, compound_literal); |
2e8bc2ba | 2421 | if (TREE_CODE (type) == ARRAY_TYPE |
2422 | && TYPE_DOMAIN (type) == NULL_TREE) | |
2423 | { | |
2424 | cp_complete_array_type_or_error (&type, compound_literal, | |
2425 | false, complain); | |
2426 | if (type == error_mark_node) | |
2427 | return error_mark_node; | |
2428 | } | |
b52bd4ae | 2429 | compound_literal = digest_init (type, compound_literal, complain); |
b3d801b4 | 2430 | if (TREE_CODE (compound_literal) == CONSTRUCTOR) |
2431 | TREE_HAS_CONSTRUCTOR (compound_literal) = true; | |
4f662622 | 2432 | /* Put static/constant array temporaries in static variables, but always |
2433 | represent class temporaries with TARGET_EXPR so we elide copies. */ | |
2434 | if ((!at_function_scope_p () || CP_TYPE_CONST_P (type)) | |
2435 | && TREE_CODE (type) == ARRAY_TYPE | |
3035a6c8 | 2436 | && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
4f662622 | 2437 | && initializer_constant_valid_p (compound_literal, type)) |
2438 | { | |
2439 | tree decl = create_temporary_var (type); | |
2440 | DECL_INITIAL (decl) = compound_literal; | |
2441 | TREE_STATIC (decl) = 1; | |
2442 | if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type)) | |
2443 | { | |
2444 | /* 5.19 says that a constant expression can include an | |
2445 | lvalue-rvalue conversion applied to "a glvalue of literal type | |
2446 | that refers to a non-volatile temporary object initialized | |
2447 | with a constant expression". Rather than try to communicate | |
2448 | that this VAR_DECL is a temporary, just mark it constexpr. */ | |
2449 | DECL_DECLARED_CONSTEXPR_P (decl) = true; | |
2450 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
2451 | TREE_CONSTANT (decl) = true; | |
2452 | } | |
2453 | cp_apply_type_quals_to_decl (cp_type_quals (type), decl); | |
2454 | decl = pushdecl_top_level (decl); | |
2455 | DECL_NAME (decl) = make_anon_name (); | |
2456 | SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl)); | |
2457 | return decl; | |
2458 | } | |
2459 | else | |
9e505437 | 2460 | return get_target_expr_sfinae (compound_literal, complain); |
0a3b29ad | 2461 | } |
2462 | ||
334ec926 | 2463 | /* Return the declaration for the function-name variable indicated by |
2464 | ID. */ | |
2465 | ||
2466 | tree | |
2467 | finish_fname (tree id) | |
2468 | { | |
2469 | tree decl; | |
9031d10b | 2470 | |
e3b80d49 | 2471 | decl = fname_decl (input_location, C_RID_CODE (id), id); |
d31e4592 | 2472 | if (processing_template_decl && current_function_decl) |
c08d51be | 2473 | decl = DECL_NAME (decl); |
334ec926 | 2474 | return decl; |
2475 | } | |
2476 | ||
ce2a6403 | 2477 | /* Finish a translation unit. */ |
d8bbef5c | 2478 | |
9031d10b | 2479 | void |
807be5b4 | 2480 | finish_translation_unit (void) |
d8bbef5c | 2481 | { |
2482 | /* In case there were missing closebraces, | |
2483 | get us back to the global binding level. */ | |
656a7ba0 | 2484 | pop_everything (); |
d8bbef5c | 2485 | while (current_namespace != global_namespace) |
2486 | pop_namespace (); | |
65b7f83f | 2487 | |
47cd6605 | 2488 | /* Do file scope __FUNCTION__ et al. */ |
65b7f83f | 2489 | finish_fname_decls (); |
d8bbef5c | 2490 | } |
2491 | ||
01b3f071 | 2492 | /* Finish a template type parameter, specified as AGGR IDENTIFIER. |
2493 | Returns the parameter. */ | |
2494 | ||
9031d10b | 2495 | tree |
807be5b4 | 2496 | finish_template_type_parm (tree aggr, tree identifier) |
01b3f071 | 2497 | { |
771665d8 | 2498 | if (aggr != class_type_node) |
01b3f071 | 2499 | { |
2b9e3597 | 2500 | permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>"); |
01b3f071 | 2501 | aggr = class_type_node; |
2502 | } | |
2503 | ||
2504 | return build_tree_list (aggr, identifier); | |
2505 | } | |
2506 | ||
2507 | /* Finish a template template parameter, specified as AGGR IDENTIFIER. | |
2508 | Returns the parameter. */ | |
2509 | ||
9031d10b | 2510 | tree |
807be5b4 | 2511 | finish_template_template_parm (tree aggr, tree identifier) |
01b3f071 | 2512 | { |
e60a6f7b | 2513 | tree decl = build_decl (input_location, |
2514 | TYPE_DECL, identifier, NULL_TREE); | |
01b3f071 | 2515 | tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE); |
2516 | DECL_TEMPLATE_PARMS (tmpl) = current_template_parms; | |
2517 | DECL_TEMPLATE_RESULT (tmpl) = decl; | |
ecdd3d84 | 2518 | DECL_ARTIFICIAL (decl) = 1; |
01b3f071 | 2519 | end_template_decl (); |
2520 | ||
b4df430b | 2521 | gcc_assert (DECL_TEMPLATE_PARMS (tmpl)); |
88eba637 | 2522 | |
faab44d2 | 2523 | check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl), |
2524 | /*is_primary=*/true, /*is_partial=*/false, | |
2525 | /*is_friend=*/0); | |
2526 | ||
01b3f071 | 2527 | return finish_template_type_parm (aggr, tmpl); |
2528 | } | |
d8bbef5c | 2529 | |
4c487414 | 2530 | /* ARGUMENT is the default-argument value for a template template |
2531 | parameter. If ARGUMENT is invalid, issue error messages and return | |
2532 | the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */ | |
2533 | ||
2534 | tree | |
2535 | check_template_template_default_arg (tree argument) | |
2536 | { | |
2537 | if (TREE_CODE (argument) != TEMPLATE_DECL | |
2538 | && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM | |
4c487414 | 2539 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) |
2540 | { | |
eae805b4 | 2541 | if (TREE_CODE (argument) == TYPE_DECL) |
d312e151 | 2542 | error ("invalid use of type %qT as a default value for a template " |
2543 | "template-parameter", TREE_TYPE (argument)); | |
eae805b4 | 2544 | else |
2545 | error ("invalid default argument for a template template parameter"); | |
4c487414 | 2546 | return error_mark_node; |
2547 | } | |
2548 | ||
2549 | return argument; | |
2550 | } | |
2551 | ||
d8bbef5c | 2552 | /* Begin a class definition, as indicated by T. */ |
2553 | ||
2554 | tree | |
6935b87a | 2555 | begin_class_definition (tree t) |
d8bbef5c | 2556 | { |
5623f36d | 2557 | if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t))) |
6cd31fa5 | 2558 | return error_mark_node; |
2559 | ||
df9d14a6 | 2560 | if (processing_template_parmlist) |
2561 | { | |
03d6ca9e | 2562 | error ("definition of %q#T inside template parameter list", t); |
df9d14a6 | 2563 | return error_mark_node; |
2564 | } | |
8df5a43d | 2565 | |
2566 | /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733 | |
2567 | are passed the same as decimal scalar types. */ | |
f71a3189 | 2568 | if (TREE_CODE (t) == RECORD_TYPE |
2569 | && !processing_template_decl) | |
8df5a43d | 2570 | { |
f71a3189 | 2571 | tree ns = TYPE_CONTEXT (t); |
2572 | if (ns && TREE_CODE (ns) == NAMESPACE_DECL | |
2573 | && DECL_CONTEXT (ns) == std_node | |
1bd1e44f | 2574 | && DECL_NAME (ns) |
f71a3189 | 2575 | && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal")) |
2576 | { | |
2577 | const char *n = TYPE_NAME_STRING (t); | |
2578 | if ((strcmp (n, "decimal32") == 0) | |
2579 | || (strcmp (n, "decimal64") == 0) | |
2580 | || (strcmp (n, "decimal128") == 0)) | |
2581 | TYPE_TRANSPARENT_AGGR (t) = 1; | |
2582 | } | |
8df5a43d | 2583 | } |
2584 | ||
bf2bb055 | 2585 | /* A non-implicit typename comes from code like: |
2586 | ||
2587 | template <typename T> struct A { | |
653e5405 | 2588 | template <typename U> struct A<T>::B ... |
bf2bb055 | 2589 | |
2590 | This is erroneous. */ | |
2591 | else if (TREE_CODE (t) == TYPENAME_TYPE) | |
2592 | { | |
03d6ca9e | 2593 | error ("invalid definition of qualified type %qT", t); |
bf2bb055 | 2594 | t = error_mark_node; |
2595 | } | |
2596 | ||
95397ff9 | 2597 | if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t)) |
d8bbef5c | 2598 | { |
95397ff9 | 2599 | t = make_class_type (RECORD_TYPE); |
3f3fa556 | 2600 | pushtag (make_anon_name (), t, /*tag_scope=*/ts_current); |
d8bbef5c | 2601 | } |
eae9825c | 2602 | |
fb868a5d | 2603 | if (TYPE_BEING_DEFINED (t)) |
d8bbef5c | 2604 | { |
95397ff9 | 2605 | t = make_class_type (TREE_CODE (t)); |
3f3fa556 | 2606 | pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current); |
d8bbef5c | 2607 | } |
f646a0ac | 2608 | maybe_process_partial_specialization (t); |
f815eb0f | 2609 | pushclass (t); |
d8bbef5c | 2610 | TYPE_BEING_DEFINED (t) = 1; |
4a2849cb | 2611 | |
1940f978 | 2612 | if (flag_pack_struct) |
2613 | { | |
2614 | tree v; | |
2615 | TYPE_PACKED (t) = 1; | |
2616 | /* Even though the type is being defined for the first time | |
2617 | here, there might have been a forward declaration, so there | |
2618 | might be cv-qualified variants of T. */ | |
2619 | for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) | |
2620 | TYPE_PACKED (v) = 1; | |
2621 | } | |
d8bbef5c | 2622 | /* Reset the interface data, at the earliest possible |
2623 | moment, as it might have been set via a class foo; | |
2624 | before. */ | |
83c4eacf | 2625 | if (! TYPE_ANONYMOUS_P (t)) |
2626 | { | |
4507a637 | 2627 | struct c_fileinfo *finfo = get_fileinfo (input_filename); |
62bf98ad | 2628 | CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only; |
83c4eacf | 2629 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X |
62bf98ad | 2630 | (t, finfo->interface_unknown); |
83c4eacf | 2631 | } |
d8bbef5c | 2632 | reset_specialization(); |
9031d10b | 2633 | |
d70cc5b5 | 2634 | /* Make a declaration for this class in its own scope. */ |
2635 | build_self_reference (); | |
2636 | ||
eae9825c | 2637 | return t; |
d8bbef5c | 2638 | } |
2639 | ||
0f2952a1 | 2640 | /* Finish the member declaration given by DECL. */ |
2641 | ||
2642 | void | |
807be5b4 | 2643 | finish_member_declaration (tree decl) |
0f2952a1 | 2644 | { |
2645 | if (decl == error_mark_node || decl == NULL_TREE) | |
2646 | return; | |
2647 | ||
2648 | if (decl == void_type_node) | |
2649 | /* The COMPONENT was a friend, not a member, and so there's | |
2650 | nothing for us to do. */ | |
2651 | return; | |
2652 | ||
2653 | /* We should see only one DECL at a time. */ | |
1767a056 | 2654 | gcc_assert (DECL_CHAIN (decl) == NULL_TREE); |
0f2952a1 | 2655 | |
2656 | /* Set up access control for DECL. */ | |
9031d10b | 2657 | TREE_PRIVATE (decl) |
0f2952a1 | 2658 | = (current_access_specifier == access_private_node); |
9031d10b | 2659 | TREE_PROTECTED (decl) |
0f2952a1 | 2660 | = (current_access_specifier == access_protected_node); |
2661 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
2662 | { | |
b8e0d419 | 2663 | TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl); |
2664 | TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl); | |
0f2952a1 | 2665 | } |
2666 | ||
c28ddc97 | 2667 | /* Mark the DECL as a member of the current class, unless it's |
2668 | a member of an enumeration. */ | |
2669 | if (TREE_CODE (decl) != CONST_DECL) | |
2670 | DECL_CONTEXT (decl) = current_class_type; | |
0f2952a1 | 2671 | |
a4f66688 | 2672 | /* Check for bare parameter packs in the member variable declaration. */ |
4eaf1b43 | 2673 | if (TREE_CODE (decl) == FIELD_DECL) |
613687b1 | 2674 | { |
830a6615 | 2675 | if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
613687b1 | 2676 | TREE_TYPE (decl) = error_mark_node; |
830a6615 | 2677 | if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl))) |
613687b1 | 2678 | DECL_ATTRIBUTES (decl) = NULL_TREE; |
2679 | } | |
a4f66688 | 2680 | |
f0edcca6 | 2681 | /* [dcl.link] |
2682 | ||
2683 | A C language linkage is ignored for the names of class members | |
2684 | and the member function type of class member functions. */ | |
2685 | if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c) | |
4b1984f5 | 2686 | SET_DECL_LANGUAGE (decl, lang_cplusplus); |
f0edcca6 | 2687 | |
0f2952a1 | 2688 | /* Put functions on the TYPE_METHODS list and everything else on the |
2689 | TYPE_FIELDS list. Note that these are built up in reverse order. | |
2690 | We reverse them (to obtain declaration order) in finish_struct. */ | |
9031d10b | 2691 | if (TREE_CODE (decl) == FUNCTION_DECL |
0f2952a1 | 2692 | || DECL_FUNCTION_TEMPLATE_P (decl)) |
2693 | { | |
2694 | /* We also need to add this function to the | |
2695 | CLASSTYPE_METHOD_VEC. */ | |
e36e7923 | 2696 | if (add_method (current_class_type, decl, NULL_TREE)) |
2697 | { | |
1767a056 | 2698 | DECL_CHAIN (decl) = TYPE_METHODS (current_class_type); |
e36e7923 | 2699 | TYPE_METHODS (current_class_type) = decl; |
ca1c7bb3 | 2700 | |
e36e7923 | 2701 | maybe_add_class_template_decl_list (current_class_type, decl, |
2702 | /*friend_p=*/0); | |
2703 | } | |
0f2952a1 | 2704 | } |
ca1c7bb3 | 2705 | /* Enter the DECL into the scope of the class. */ |
807f85cf | 2706 | else if (pushdecl_class_level (decl)) |
0f2952a1 | 2707 | { |
807f85cf | 2708 | if (TREE_CODE (decl) == USING_DECL) |
2709 | { | |
807f85cf | 2710 | /* For now, ignore class-scope USING_DECLS, so that |
2711 | debugging backends do not see them. */ | |
2712 | DECL_IGNORED_P (decl) = 1; | |
2713 | } | |
2714 | ||
0f2952a1 | 2715 | /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields |
2716 | go at the beginning. The reason is that lookup_field_1 | |
2717 | searches the list in order, and we want a field name to | |
2718 | override a type name so that the "struct stat hack" will | |
2719 | work. In particular: | |
2720 | ||
2721 | struct S { enum E { }; int E } s; | |
2722 | s.E = 3; | |
2723 | ||
6c0cc2cd | 2724 | is valid. In addition, the FIELD_DECLs must be maintained in |
0f2952a1 | 2725 | declaration order so that class layout works as expected. |
2726 | However, we don't need that order until class layout, so we | |
2727 | save a little time by putting FIELD_DECLs on in reverse order | |
2728 | here, and then reversing them in finish_struct_1. (We could | |
2729 | also keep a pointer to the correct insertion points in the | |
2730 | list.) */ | |
2731 | ||
2732 | if (TREE_CODE (decl) == TYPE_DECL) | |
9031d10b | 2733 | TYPE_FIELDS (current_class_type) |
0f2952a1 | 2734 | = chainon (TYPE_FIELDS (current_class_type), decl); |
2735 | else | |
2736 | { | |
1767a056 | 2737 | DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type); |
0f2952a1 | 2738 | TYPE_FIELDS (current_class_type) = decl; |
2739 | } | |
1eaf178d | 2740 | |
9031d10b | 2741 | maybe_add_class_template_decl_list (current_class_type, decl, |
ca1c7bb3 | 2742 | /*friend_p=*/0); |
0f2952a1 | 2743 | } |
b11ef6ee | 2744 | |
2745 | if (pch_file) | |
2746 | note_decl_for_pch (decl); | |
2747 | } | |
2748 | ||
2749 | /* DECL has been declared while we are building a PCH file. Perform | |
2750 | actions that we might normally undertake lazily, but which can be | |
2751 | performed now so that they do not have to be performed in | |
2752 | translation units which include the PCH file. */ | |
2753 | ||
2754 | void | |
2755 | note_decl_for_pch (tree decl) | |
2756 | { | |
2757 | gcc_assert (pch_file); | |
2758 | ||
b11ef6ee | 2759 | /* There's a good chance that we'll have to mangle names at some |
2760 | point, even if only for emission in debugging information. */ | |
ec689dc3 | 2761 | if ((TREE_CODE (decl) == VAR_DECL |
2762 | || TREE_CODE (decl) == FUNCTION_DECL) | |
2763 | && !processing_template_decl) | |
b11ef6ee | 2764 | mangle_decl (decl); |
0f2952a1 | 2765 | } |
2766 | ||
aa977dcc | 2767 | /* Finish processing a complete template declaration. The PARMS are |
34197853 | 2768 | the template parameters. */ |
2769 | ||
2770 | void | |
807be5b4 | 2771 | finish_template_decl (tree parms) |
34197853 | 2772 | { |
2773 | if (parms) | |
2774 | end_template_decl (); | |
2775 | else | |
2776 | end_specialization (); | |
2777 | } | |
2778 | ||
9abe66a7 | 2779 | /* Finish processing a template-id (which names a type) of the form |
34197853 | 2780 | NAME < ARGS >. Return the TYPE_DECL for the type named by the |
3160db1d | 2781 | template-id. If ENTERING_SCOPE is nonzero we are about to enter |
34197853 | 2782 | the scope of template-id indicated. */ |
2783 | ||
2784 | tree | |
807be5b4 | 2785 | finish_template_type (tree name, tree args, int entering_scope) |
34197853 | 2786 | { |
370478b1 | 2787 | tree type; |
34197853 | 2788 | |
370478b1 | 2789 | type = lookup_template_class (name, args, |
b992fe70 | 2790 | NULL_TREE, NULL_TREE, entering_scope, |
0fbca5e8 | 2791 | tf_warning_or_error | tf_user); |
370478b1 | 2792 | if (type == error_mark_node) |
2793 | return type; | |
2794 | else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type)) | |
2795 | return TYPE_STUB_DECL (type); | |
2796 | else | |
2797 | return TYPE_NAME (type); | |
34197853 | 2798 | } |
343a4a9c | 2799 | |
69f578cd | 2800 | /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER. |
2801 | Return a TREE_LIST containing the ACCESS_SPECIFIER and the | |
2802 | BASE_CLASS, or NULL_TREE if an error occurred. The | |
4109ca29 | 2803 | ACCESS_SPECIFIER is one of |
57c28194 | 2804 | access_{default,public,protected_private}_node. For a virtual base |
2805 | we set TREE_TYPE. */ | |
69f578cd | 2806 | |
9031d10b | 2807 | tree |
95f3173a | 2808 | finish_base_specifier (tree base, tree access, bool virtual_p) |
69f578cd | 2809 | { |
69f578cd | 2810 | tree result; |
2811 | ||
95f3173a | 2812 | if (base == error_mark_node) |
ca62cc3f | 2813 | { |
2814 | error ("invalid base-class specification"); | |
2815 | result = NULL_TREE; | |
2816 | } | |
95397ff9 | 2817 | else if (! MAYBE_CLASS_TYPE_P (base)) |
2818 | { | |
2819 | error ("%qT is not a class type", base); | |
2820 | result = NULL_TREE; | |
2821 | } | |
69f578cd | 2822 | else |
46a79a8f | 2823 | { |
95f3173a | 2824 | if (cp_type_quals (base) != 0) |
653e5405 | 2825 | { |
e966d1b8 | 2826 | /* DR 484: Can a base-specifier name a cv-qualified |
2827 | class type? */ | |
653e5405 | 2828 | base = TYPE_MAIN_VARIANT (base); |
2829 | } | |
95f3173a | 2830 | result = build_tree_list (access, base); |
57c28194 | 2831 | if (virtual_p) |
2832 | TREE_TYPE (result) = integer_type_node; | |
46a79a8f | 2833 | } |
69f578cd | 2834 | |
2835 | return result; | |
2836 | } | |
0f2952a1 | 2837 | |
0e5cde0c | 2838 | /* If FNS is a member function, a set of member functions, or a |
2839 | template-id referring to one or more member functions, return a | |
2840 | BASELINK for FNS, incorporating the current access context. | |
2841 | Otherwise, return FNS unchanged. */ | |
2842 | ||
2843 | tree | |
2844 | baselink_for_fns (tree fns) | |
2845 | { | |
47744737 | 2846 | tree scope; |
0e5cde0c | 2847 | tree cl; |
2848 | ||
2849 | if (BASELINK_P (fns) | |
0e5cde0c | 2850 | || error_operand_p (fns)) |
2851 | return fns; | |
47744737 | 2852 | |
2853 | scope = ovl_scope (fns); | |
2854 | if (!CLASS_TYPE_P (scope)) | |
0e5cde0c | 2855 | return fns; |
2856 | ||
47744737 | 2857 | cl = currently_open_derived_class (scope); |
0e5cde0c | 2858 | if (!cl) |
47744737 | 2859 | cl = scope; |
0e5cde0c | 2860 | cl = TYPE_BINFO (cl); |
f302e37a | 2861 | return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE); |
0e5cde0c | 2862 | } |
2863 | ||
a8b75081 | 2864 | /* Returns true iff DECL is an automatic variable from a function outside |
2865 | the current one. */ | |
2866 | ||
2867 | static bool | |
2868 | outer_automatic_var_p (tree decl) | |
2869 | { | |
2870 | return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL) | |
2871 | && DECL_FUNCTION_SCOPE_P (decl) | |
2872 | && !TREE_STATIC (decl) | |
2873 | && DECL_CONTEXT (decl) != current_function_decl); | |
2874 | } | |
2875 | ||
0886adbc | 2876 | /* ID_EXPRESSION is a representation of parsed, but unprocessed, |
2877 | id-expression. (See cp_parser_id_expression for details.) SCOPE, | |
2878 | if non-NULL, is the type or namespace used to explicitly qualify | |
2879 | ID_EXPRESSION. DECL is the entity to which that name has been | |
9031d10b | 2880 | resolved. |
0886adbc | 2881 | |
2882 | *CONSTANT_EXPRESSION_P is true if we are presently parsing a | |
2883 | constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will | |
2884 | be set to true if this expression isn't permitted in a | |
2885 | constant-expression, but it is otherwise not set by this function. | |
2886 | *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a | |
2887 | constant-expression, but a non-constant expression is also | |
2888 | permissible. | |
2889 | ||
fbb01da7 | 2890 | DONE is true if this expression is a complete postfix-expression; |
2891 | it is false if this expression is followed by '->', '[', '(', etc. | |
2892 | ADDRESS_P is true iff this expression is the operand of '&'. | |
2893 | TEMPLATE_P is true iff the qualified-id was of the form | |
2894 | "A::template B". TEMPLATE_ARG_P is true iff this qualified name | |
2895 | appears as a template argument. | |
2896 | ||
0886adbc | 2897 | If an error occurs, and it is the kind of error that might cause |
2898 | the parser to abort a tentative parse, *ERROR_MSG is filled in. It | |
2899 | is the caller's responsibility to issue the message. *ERROR_MSG | |
2900 | will be a string with static storage duration, so the caller need | |
2901 | not "free" it. | |
2902 | ||
2903 | Return an expression for the entity, after issuing appropriate | |
2904 | diagnostics. This function is also responsible for transforming a | |
2905 | reference to a non-static member into a COMPONENT_REF that makes | |
9031d10b | 2906 | the use of "this" explicit. |
0886adbc | 2907 | |
2908 | Upon return, *IDK will be filled in appropriately. */ | |
0886adbc | 2909 | tree |
9031d10b | 2910 | finish_id_expression (tree id_expression, |
0886adbc | 2911 | tree decl, |
2912 | tree scope, | |
2913 | cp_id_kind *idk, | |
f47c1747 | 2914 | bool integral_constant_expression_p, |
2915 | bool allow_non_integral_constant_expression_p, | |
2916 | bool *non_integral_constant_expression_p, | |
fbb01da7 | 2917 | bool template_p, |
2918 | bool done, | |
2919 | bool address_p, | |
2920 | bool template_arg_p, | |
ad9ae192 | 2921 | const char **error_msg, |
2922 | location_t location) | |
0886adbc | 2923 | { |
8fc22c4b | 2924 | decl = strip_using_decl (decl); |
2925 | ||
0886adbc | 2926 | /* Initialize the output parameters. */ |
2927 | *idk = CP_ID_KIND_NONE; | |
2928 | *error_msg = NULL; | |
2929 | ||
2930 | if (id_expression == error_mark_node) | |
2931 | return error_mark_node; | |
2932 | /* If we have a template-id, then no further lookup is | |
2933 | required. If the template-id was for a template-class, we | |
2934 | will sometimes have a TYPE_DECL at this point. */ | |
2935 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
c0b419db | 2936 | || TREE_CODE (decl) == TYPE_DECL) |
0886adbc | 2937 | ; |
2938 | /* Look up the name. */ | |
9031d10b | 2939 | else |
0886adbc | 2940 | { |
2941 | if (decl == error_mark_node) | |
2942 | { | |
2943 | /* Name lookup failed. */ | |
9031d10b | 2944 | if (scope |
2945 | && (!TYPE_P (scope) | |
7ef14399 | 2946 | || (!dependent_type_p (scope) |
2947 | && !(TREE_CODE (id_expression) == IDENTIFIER_NODE | |
2948 | && IDENTIFIER_TYPENAME_P (id_expression) | |
2949 | && dependent_type_p (TREE_TYPE (id_expression)))))) | |
0886adbc | 2950 | { |
7ef14399 | 2951 | /* If the qualifying type is non-dependent (and the name |
2952 | does not name a conversion operator to a dependent | |
2953 | type), issue an error. */ | |
ad9ae192 | 2954 | qualified_name_lookup_error (scope, id_expression, decl, location); |
0886adbc | 2955 | return error_mark_node; |
2956 | } | |
2957 | else if (!scope) | |
2958 | { | |
2959 | /* It may be resolved via Koenig lookup. */ | |
2960 | *idk = CP_ID_KIND_UNQUALIFIED; | |
2961 | return id_expression; | |
2962 | } | |
7ef14399 | 2963 | else |
2964 | decl = id_expression; | |
0886adbc | 2965 | } |
2966 | /* If DECL is a variable that would be out of scope under | |
2967 | ANSI/ISO rules, but in scope in the ARM, name lookup | |
2968 | will succeed. Issue a diagnostic here. */ | |
2969 | else | |
2970 | decl = check_for_out_of_scope_variable (decl); | |
2971 | ||
2972 | /* Remember that the name was used in the definition of | |
2973 | the current class so that we can check later to see if | |
2974 | the meaning would have been different after the class | |
2975 | was entirely defined. */ | |
dff917fb | 2976 | if (!scope && decl != error_mark_node |
2977 | && TREE_CODE (id_expression) == IDENTIFIER_NODE) | |
0886adbc | 2978 | maybe_note_name_used_in_class (id_expression, decl); |
f3d3cc67 | 2979 | |
a8b75081 | 2980 | /* Disallow uses of local variables from containing functions, except |
2981 | within lambda-expressions. */ | |
bcc4b4ea | 2982 | if (outer_automatic_var_p (decl) |
a8b75081 | 2983 | /* It's not a use (3.2) if we're in an unevaluated context. */ |
2984 | && !cp_unevaluated_operand) | |
f3d3cc67 | 2985 | { |
a8b75081 | 2986 | tree context = DECL_CONTEXT (decl); |
2987 | tree containing_function = current_function_decl; | |
2988 | tree lambda_stack = NULL_TREE; | |
2989 | tree lambda_expr = NULL_TREE; | |
981e6961 | 2990 | tree initializer = convert_from_reference (decl); |
a8b75081 | 2991 | |
5f9d485a | 2992 | /* Mark it as used now even if the use is ill-formed. */ |
2993 | mark_used (decl); | |
2994 | ||
a8b75081 | 2995 | /* Core issue 696: "[At the July 2009 meeting] the CWG expressed |
2996 | support for an approach in which a reference to a local | |
2997 | [constant] automatic variable in a nested class or lambda body | |
2998 | would enter the expression as an rvalue, which would reduce | |
2999 | the complexity of the problem" | |
3000 | ||
3001 | FIXME update for final resolution of core issue 696. */ | |
d58b8a94 | 3002 | if (decl_constant_var_p (decl)) |
a8b75081 | 3003 | return integral_constant_value (decl); |
3004 | ||
3005 | /* If we are in a lambda function, we can move out until we hit | |
3006 | 1. the context, | |
3007 | 2. a non-lambda function, or | |
3008 | 3. a non-default capturing lambda function. */ | |
3009 | while (context != containing_function | |
3010 | && LAMBDA_FUNCTION_P (containing_function)) | |
3011 | { | |
3012 | lambda_expr = CLASSTYPE_LAMBDA_EXPR | |
3013 | (DECL_CONTEXT (containing_function)); | |
3014 | ||
3015 | if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) | |
3016 | == CPLD_NONE) | |
3017 | break; | |
3018 | ||
3019 | lambda_stack = tree_cons (NULL_TREE, | |
3020 | lambda_expr, | |
3021 | lambda_stack); | |
3022 | ||
3023 | containing_function | |
3024 | = decl_function_context (containing_function); | |
3025 | } | |
3026 | ||
3027 | if (context == containing_function) | |
3028 | { | |
3029 | decl = add_default_capture (lambda_stack, | |
3030 | /*id=*/DECL_NAME (decl), | |
ab9eab4a | 3031 | initializer); |
a8b75081 | 3032 | } |
3033 | else if (lambda_expr) | |
3034 | { | |
3035 | error ("%qD is not captured", decl); | |
3036 | return error_mark_node; | |
3037 | } | |
3038 | else | |
f3d3cc67 | 3039 | { |
3040 | error (TREE_CODE (decl) == VAR_DECL | |
e087c7d8 | 3041 | ? G_("use of local variable with automatic storage from containing function") |
ac7549c7 | 3042 | : G_("use of parameter from containing function")); |
f3d3cc67 | 3043 | error (" %q+#D declared here", decl); |
3044 | return error_mark_node; | |
3045 | } | |
3046 | } | |
1c72f3b7 | 3047 | |
3048 | /* Also disallow uses of function parameters outside the function | |
3049 | body, except inside an unevaluated context (i.e. decltype). */ | |
3050 | if (TREE_CODE (decl) == PARM_DECL | |
3051 | && DECL_CONTEXT (decl) == NULL_TREE | |
3052 | && !cp_unevaluated_operand) | |
3053 | { | |
3054 | error ("use of parameter %qD outside function body", decl); | |
3055 | return error_mark_node; | |
3056 | } | |
0886adbc | 3057 | } |
3058 | ||
3059 | /* If we didn't find anything, or what we found was a type, | |
3060 | then this wasn't really an id-expression. */ | |
3061 | if (TREE_CODE (decl) == TEMPLATE_DECL | |
3062 | && !DECL_FUNCTION_TEMPLATE_P (decl)) | |
3063 | { | |
3064 | *error_msg = "missing template arguments"; | |
3065 | return error_mark_node; | |
3066 | } | |
3067 | else if (TREE_CODE (decl) == TYPE_DECL | |
3068 | || TREE_CODE (decl) == NAMESPACE_DECL) | |
3069 | { | |
3070 | *error_msg = "expected primary-expression"; | |
3071 | return error_mark_node; | |
3072 | } | |
3073 | ||
3074 | /* If the name resolved to a template parameter, there is no | |
7fc97909 | 3075 | need to look it up again later. */ |
3076 | if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl)) | |
3077 | || TREE_CODE (decl) == TEMPLATE_PARM_INDEX) | |
0886adbc | 3078 | { |
729f89ff | 3079 | tree r; |
9031d10b | 3080 | |
0886adbc | 3081 | *idk = CP_ID_KIND_NONE; |
7fc97909 | 3082 | if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX) |
3083 | decl = TEMPLATE_PARM_DECL (decl); | |
729f89ff | 3084 | r = convert_from_reference (DECL_INITIAL (decl)); |
9031d10b | 3085 | |
3086 | if (integral_constant_expression_p | |
4a072583 | 3087 | && !dependent_type_p (TREE_TYPE (decl)) |
729f89ff | 3088 | && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r)))) |
7fc97909 | 3089 | { |
f47c1747 | 3090 | if (!allow_non_integral_constant_expression_p) |
03d6ca9e | 3091 | error ("template parameter %qD of type %qT is not allowed in " |
7fc97909 | 3092 | "an integral constant expression because it is not of " |
3093 | "integral or enumeration type", decl, TREE_TYPE (decl)); | |
f47c1747 | 3094 | *non_integral_constant_expression_p = true; |
7fc97909 | 3095 | } |
729f89ff | 3096 | return r; |
7fc97909 | 3097 | } |
0886adbc | 3098 | else |
3099 | { | |
3100 | bool dependent_p; | |
3101 | ||
3102 | /* If the declaration was explicitly qualified indicate | |
3103 | that. The semantics of `A::f(3)' are different than | |
3104 | `f(3)' if `f' is virtual. */ | |
9031d10b | 3105 | *idk = (scope |
0886adbc | 3106 | ? CP_ID_KIND_QUALIFIED |
3107 | : (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
3108 | ? CP_ID_KIND_TEMPLATE_ID | |
3109 | : CP_ID_KIND_UNQUALIFIED)); | |
3110 | ||
3111 | ||
3112 | /* [temp.dep.expr] | |
3113 | ||
3114 | An id-expression is type-dependent if it contains an | |
3115 | identifier that was declared with a dependent type. | |
3116 | ||
0886adbc | 3117 | The standard is not very specific about an id-expression that |
3118 | names a set of overloaded functions. What if some of them | |
3119 | have dependent types and some of them do not? Presumably, | |
3120 | such a name should be treated as a dependent name. */ | |
3121 | /* Assume the name is not dependent. */ | |
3122 | dependent_p = false; | |
3123 | if (!processing_template_decl) | |
3124 | /* No names are dependent outside a template. */ | |
3125 | ; | |
c28ddc97 | 3126 | else if (TREE_CODE (decl) == CONST_DECL) |
3127 | /* We don't want to treat enumerators as dependent. */ | |
3128 | ; | |
0886adbc | 3129 | /* A template-id where the name of the template was not resolved |
3130 | is definitely dependent. */ | |
3131 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
9031d10b | 3132 | && (TREE_CODE (TREE_OPERAND (decl, 0)) |
0886adbc | 3133 | == IDENTIFIER_NODE)) |
3134 | dependent_p = true; | |
3135 | /* For anything except an overloaded function, just check its | |
3136 | type. */ | |
3137 | else if (!is_overloaded_fn (decl)) | |
9031d10b | 3138 | dependent_p |
0886adbc | 3139 | = dependent_type_p (TREE_TYPE (decl)); |
3140 | /* For a set of overloaded functions, check each of the | |
3141 | functions. */ | |
3142 | else | |
3143 | { | |
3144 | tree fns = decl; | |
3145 | ||
3146 | if (BASELINK_P (fns)) | |
3147 | fns = BASELINK_FUNCTIONS (fns); | |
3148 | ||
3149 | /* For a template-id, check to see if the template | |
3150 | arguments are dependent. */ | |
3151 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) | |
3152 | { | |
3153 | tree args = TREE_OPERAND (fns, 1); | |
3154 | dependent_p = any_dependent_template_arguments_p (args); | |
3155 | /* The functions are those referred to by the | |
3156 | template-id. */ | |
3157 | fns = TREE_OPERAND (fns, 0); | |
3158 | } | |
3159 | ||
3160 | /* If there are no dependent template arguments, go through | |
63eff20d | 3161 | the overloaded functions. */ |
0886adbc | 3162 | while (fns && !dependent_p) |
3163 | { | |
3164 | tree fn = OVL_CURRENT (fns); | |
3165 | ||
3166 | /* Member functions of dependent classes are | |
3167 | dependent. */ | |
3168 | if (TREE_CODE (fn) == FUNCTION_DECL | |
3169 | && type_dependent_expression_p (fn)) | |
3170 | dependent_p = true; | |
3171 | else if (TREE_CODE (fn) == TEMPLATE_DECL | |
3172 | && dependent_template_p (fn)) | |
3173 | dependent_p = true; | |
3174 | ||
3175 | fns = OVL_NEXT (fns); | |
3176 | } | |
3177 | } | |
3178 | ||
3179 | /* If the name was dependent on a template parameter, we will | |
3180 | resolve the name at instantiation time. */ | |
3181 | if (dependent_p) | |
3182 | { | |
3183 | /* Create a SCOPE_REF for qualified names, if the scope is | |
3184 | dependent. */ | |
3185 | if (scope) | |
3186 | { | |
fbb01da7 | 3187 | if (TYPE_P (scope)) |
3188 | { | |
3189 | if (address_p && done) | |
3190 | decl = finish_qualified_id_expr (scope, decl, | |
3191 | done, address_p, | |
3192 | template_p, | |
3193 | template_arg_p); | |
eac53a7c | 3194 | else |
3195 | { | |
3196 | tree type = NULL_TREE; | |
3197 | if (DECL_P (decl) && !dependent_scope_p (scope)) | |
3198 | type = TREE_TYPE (decl); | |
3199 | decl = build_qualified_name (type, | |
3200 | scope, | |
3201 | id_expression, | |
3202 | template_p); | |
3203 | } | |
fbb01da7 | 3204 | } |
3205 | if (TREE_TYPE (decl)) | |
3206 | decl = convert_from_reference (decl); | |
3207 | return decl; | |
0886adbc | 3208 | } |
3209 | /* A TEMPLATE_ID already contains all the information we | |
3210 | need. */ | |
3211 | if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR) | |
3212 | return id_expression; | |
c08d51be | 3213 | *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT; |
2c584053 | 3214 | /* If we found a variable, then name lookup during the |
3215 | instantiation will always resolve to the same VAR_DECL | |
3216 | (or an instantiation thereof). */ | |
29ac02b7 | 3217 | if (TREE_CODE (decl) == VAR_DECL |
3218 | || TREE_CODE (decl) == PARM_DECL) | |
ca0553d6 | 3219 | { |
3220 | mark_used (decl); | |
3221 | return convert_from_reference (decl); | |
3222 | } | |
9a4312bd | 3223 | /* The same is true for FIELD_DECL, but we also need to |
3224 | make sure that the syntax is correct. */ | |
3225 | else if (TREE_CODE (decl) == FIELD_DECL) | |
edb111a3 | 3226 | { |
3227 | /* Since SCOPE is NULL here, this is an unqualified name. | |
3228 | Access checking has been performed during name lookup | |
3229 | already. Turn off checking to avoid duplicate errors. */ | |
3230 | push_deferring_access_checks (dk_no_check); | |
3231 | decl = finish_non_static_data_member | |
70269a57 | 3232 | (decl, NULL_TREE, |
edb111a3 | 3233 | /*qualifying_scope=*/NULL_TREE); |
3234 | pop_deferring_access_checks (); | |
3235 | return decl; | |
3236 | } | |
c08d51be | 3237 | return id_expression; |
0886adbc | 3238 | } |
3239 | ||
135f8bad | 3240 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
03aa2b89 | 3241 | { |
03d6ca9e | 3242 | error ("use of namespace %qD as expression", decl); |
03aa2b89 | 3243 | return error_mark_node; |
3244 | } | |
3245 | else if (DECL_CLASS_TEMPLATE_P (decl)) | |
3246 | { | |
03d6ca9e | 3247 | error ("use of class template %qT as expression", decl); |
03aa2b89 | 3248 | return error_mark_node; |
3249 | } | |
3250 | else if (TREE_CODE (decl) == TREE_LIST) | |
3251 | { | |
3252 | /* Ambiguous reference to base members. */ | |
03d6ca9e | 3253 | error ("request for member %qD is ambiguous in " |
03aa2b89 | 3254 | "multiple inheritance lattice", id_expression); |
3255 | print_candidates (decl); | |
3256 | return error_mark_node; | |
3257 | } | |
135f8bad | 3258 | |
3259 | /* Mark variable-like entities as used. Functions are similarly | |
3260 | marked either below or after overload resolution. */ | |
68204971 | 3261 | if ((TREE_CODE (decl) == VAR_DECL |
3262 | || TREE_CODE (decl) == PARM_DECL | |
3263 | || TREE_CODE (decl) == CONST_DECL | |
3264 | || TREE_CODE (decl) == RESULT_DECL) | |
3265 | && !mark_used (decl)) | |
3266 | return error_mark_node; | |
135f8bad | 3267 | |
d58b8a94 | 3268 | /* Only certain kinds of names are allowed in constant |
c28ddc97 | 3269 | expression. Template parameters have already |
d58b8a94 | 3270 | been handled above. */ |
1d4070b9 | 3271 | if (! error_operand_p (decl) |
3272 | && integral_constant_expression_p | |
d58b8a94 | 3273 | && ! decl_constant_var_p (decl) |
c28ddc97 | 3274 | && TREE_CODE (decl) != CONST_DECL |
d58b8a94 | 3275 | && ! builtin_valid_in_constant_expr_p (decl)) |
3276 | { | |
3277 | if (!allow_non_integral_constant_expression_p) | |
3278 | { | |
3279 | error ("%qD cannot appear in a constant-expression", decl); | |
3280 | return error_mark_node; | |
3281 | } | |
3282 | *non_integral_constant_expression_p = true; | |
3283 | } | |
3284 | ||
462819c8 | 3285 | tree wrap; |
3286 | if (TREE_CODE (decl) == VAR_DECL | |
3287 | && !cp_unevaluated_operand | |
3288 | && DECL_THREAD_LOCAL_P (decl) | |
3289 | && (wrap = get_tls_wrapper_fn (decl))) | |
3290 | { | |
3291 | /* Replace an evaluated use of the thread_local variable with | |
3292 | a call to its wrapper. */ | |
3293 | decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error); | |
3294 | } | |
3295 | else if (scope) | |
135f8bad | 3296 | { |
9031d10b | 3297 | decl = (adjust_result_of_qualified_name_lookup |
09616866 | 3298 | (decl, scope, current_nonlambda_class_type())); |
58cae612 | 3299 | |
3300 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
3301 | mark_used (decl); | |
3302 | ||
d0b4bf06 | 3303 | if (TYPE_P (scope)) |
fbb01da7 | 3304 | decl = finish_qualified_id_expr (scope, |
3305 | decl, | |
3306 | done, | |
3307 | address_p, | |
3308 | template_p, | |
3309 | template_arg_p); | |
729f89ff | 3310 | else |
d0b4bf06 | 3311 | decl = convert_from_reference (decl); |
135f8bad | 3312 | } |
03aa2b89 | 3313 | else if (TREE_CODE (decl) == FIELD_DECL) |
edb111a3 | 3314 | { |
3315 | /* Since SCOPE is NULL here, this is an unqualified name. | |
3316 | Access checking has been performed during name lookup | |
3317 | already. Turn off checking to avoid duplicate errors. */ | |
3318 | push_deferring_access_checks (dk_no_check); | |
70269a57 | 3319 | decl = finish_non_static_data_member (decl, NULL_TREE, |
edb111a3 | 3320 | /*qualifying_scope=*/NULL_TREE); |
3321 | pop_deferring_access_checks (); | |
3322 | } | |
03aa2b89 | 3323 | else if (is_overloaded_fn (decl)) |
3324 | { | |
0e5cde0c | 3325 | tree first_fn; |
0886adbc | 3326 | |
1f07118e | 3327 | first_fn = get_first_fn (decl); |
03aa2b89 | 3328 | if (TREE_CODE (first_fn) == TEMPLATE_DECL) |
3329 | first_fn = DECL_TEMPLATE_RESULT (first_fn); | |
135f8bad | 3330 | |
bfb588d6 | 3331 | if (!really_overloaded_fn (decl) |
3332 | && !mark_used (first_fn)) | |
3333 | return error_mark_node; | |
135f8bad | 3334 | |
fbb01da7 | 3335 | if (!template_arg_p |
3336 | && TREE_CODE (first_fn) == FUNCTION_DECL | |
2c9f7d9e | 3337 | && DECL_FUNCTION_MEMBER_P (first_fn) |
3338 | && !shared_member_p (decl)) | |
03aa2b89 | 3339 | { |
3340 | /* A set of member functions. */ | |
3341 | decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0); | |
fbb01da7 | 3342 | return finish_class_member_access_expr (decl, id_expression, |
ebd21de4 | 3343 | /*template_p=*/false, |
3344 | tf_warning_or_error); | |
03aa2b89 | 3345 | } |
0e5cde0c | 3346 | |
3347 | decl = baselink_for_fns (decl); | |
03aa2b89 | 3348 | } |
3349 | else | |
3350 | { | |
03aa2b89 | 3351 | if (DECL_P (decl) && DECL_NONLOCAL (decl) |
c2d52c0c | 3352 | && DECL_CLASS_SCOPE_P (decl)) |
03aa2b89 | 3353 | { |
c2d52c0c | 3354 | tree context = context_for_name_lookup (decl); |
3355 | if (context != current_class_type) | |
3356 | { | |
3357 | tree path = currently_open_derived_class (context); | |
3358 | perform_or_defer_access_check (TYPE_BINFO (path), | |
eb833cbe | 3359 | decl, decl, |
3360 | tf_warning_or_error); | |
c2d52c0c | 3361 | } |
03aa2b89 | 3362 | } |
9031d10b | 3363 | |
729f89ff | 3364 | decl = convert_from_reference (decl); |
03aa2b89 | 3365 | } |
0886adbc | 3366 | } |
3367 | ||
3368 | if (TREE_DEPRECATED (decl)) | |
45c4e798 | 3369 | warn_deprecated_use (decl, NULL_TREE); |
0886adbc | 3370 | |
3371 | return decl; | |
3372 | } | |
3373 | ||
902b4e01 | 3374 | /* Implement the __typeof keyword: Return the type of EXPR, suitable for |
3375 | use as a type-specifier. */ | |
3376 | ||
63e22e88 | 3377 | tree |
807be5b4 | 3378 | finish_typeof (tree expr) |
63e22e88 | 3379 | { |
1cfa8cdd | 3380 | tree type; |
3381 | ||
7d0f6734 | 3382 | if (type_dependent_expression_p (expr)) |
63e22e88 | 3383 | { |
95397ff9 | 3384 | type = cxx_make_type (TYPEOF_TYPE); |
82bb2115 | 3385 | TYPEOF_TYPE_EXPR (type) = expr; |
34da8800 | 3386 | SET_TYPE_STRUCTURAL_EQUALITY (type); |
63e22e88 | 3387 | |
1cfa8cdd | 3388 | return type; |
63e22e88 | 3389 | } |
3390 | ||
fbb73d9b | 3391 | expr = mark_type_use (expr); |
3392 | ||
66a0bac0 | 3393 | type = unlowered_expr_type (expr); |
1cfa8cdd | 3394 | |
3395 | if (!type || type == unknown_type_node) | |
3396 | { | |
03d6ca9e | 3397 | error ("type of %qE is unknown", expr); |
1cfa8cdd | 3398 | return error_mark_node; |
3399 | } | |
3400 | ||
3401 | return type; | |
63e22e88 | 3402 | } |
019cf886 | 3403 | |
8de5c43e | 3404 | /* Implement the __underlying_type keyword: Return the underlying |
3405 | type of TYPE, suitable for use as a type-specifier. */ | |
3406 | ||
3407 | tree | |
3408 | finish_underlying_type (tree type) | |
3409 | { | |
3410 | tree underlying_type; | |
3411 | ||
3412 | if (processing_template_decl) | |
3413 | { | |
3414 | underlying_type = cxx_make_type (UNDERLYING_TYPE); | |
3415 | UNDERLYING_TYPE_TYPE (underlying_type) = type; | |
3416 | SET_TYPE_STRUCTURAL_EQUALITY (underlying_type); | |
3417 | ||
3418 | return underlying_type; | |
3419 | } | |
3420 | ||
3421 | complete_type (type); | |
3422 | ||
3423 | if (TREE_CODE (type) != ENUMERAL_TYPE) | |
3424 | { | |
8c7d88f7 | 3425 | error ("%qT is not an enumeration type", type); |
8de5c43e | 3426 | return error_mark_node; |
3427 | } | |
3428 | ||
3429 | underlying_type = ENUM_UNDERLYING_TYPE (type); | |
3430 | ||
3431 | /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE | |
3432 | includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information. | |
3433 | See finish_enum_value_list for details. */ | |
3434 | if (!ENUM_FIXED_UNDERLYING_TYPE_P (type)) | |
3435 | underlying_type | |
3436 | = c_common_type_for_mode (TYPE_MODE (underlying_type), | |
3437 | TYPE_UNSIGNED (underlying_type)); | |
3438 | ||
3439 | return underlying_type; | |
3440 | } | |
3441 | ||
e6014a82 | 3442 | /* Implement the __direct_bases keyword: Return the direct base classes |
3443 | of type */ | |
3444 | ||
3445 | tree | |
3446 | calculate_direct_bases (tree type) | |
3447 | { | |
f1f41a6c | 3448 | vec<tree, va_gc> *vector = make_tree_vector(); |
e6014a82 | 3449 | tree bases_vec = NULL_TREE; |
f1f41a6c | 3450 | vec<tree, va_gc> *base_binfos; |
e6014a82 | 3451 | tree binfo; |
3452 | unsigned i; | |
3453 | ||
3454 | complete_type (type); | |
3455 | ||
3456 | if (!NON_UNION_CLASS_TYPE_P (type)) | |
3457 | return make_tree_vec (0); | |
3458 | ||
3459 | base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type)); | |
3460 | ||
3461 | /* Virtual bases are initialized first */ | |
f1f41a6c | 3462 | for (i = 0; base_binfos->iterate (i, &binfo); i++) |
e6014a82 | 3463 | { |
3464 | if (BINFO_VIRTUAL_P (binfo)) | |
3465 | { | |
f1f41a6c | 3466 | vec_safe_push (vector, binfo); |
e6014a82 | 3467 | } |
3468 | } | |
3469 | ||
3470 | /* Now non-virtuals */ | |
f1f41a6c | 3471 | for (i = 0; base_binfos->iterate (i, &binfo); i++) |
e6014a82 | 3472 | { |
3473 | if (!BINFO_VIRTUAL_P (binfo)) | |
3474 | { | |
f1f41a6c | 3475 | vec_safe_push (vector, binfo); |
e6014a82 | 3476 | } |
3477 | } | |
3478 | ||
3479 | ||
f1f41a6c | 3480 | bases_vec = make_tree_vec (vector->length ()); |
e6014a82 | 3481 | |
f1f41a6c | 3482 | for (i = 0; i < vector->length (); ++i) |
e6014a82 | 3483 | { |
f1f41a6c | 3484 | TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]); |
e6014a82 | 3485 | } |
3486 | return bases_vec; | |
3487 | } | |
3488 | ||
3489 | /* Implement the __bases keyword: Return the base classes | |
3490 | of type */ | |
3491 | ||
3492 | /* Find morally non-virtual base classes by walking binfo hierarchy */ | |
3493 | /* Virtual base classes are handled separately in finish_bases */ | |
3494 | ||
3495 | static tree | |
a49c5913 | 3496 | dfs_calculate_bases_pre (tree binfo, void * /*data_*/) |
e6014a82 | 3497 | { |
3498 | /* Don't walk bases of virtual bases */ | |
3499 | return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE; | |
3500 | } | |
3501 | ||
3502 | static tree | |
3503 | dfs_calculate_bases_post (tree binfo, void *data_) | |
3504 | { | |
f1f41a6c | 3505 | vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_); |
e6014a82 | 3506 | if (!BINFO_VIRTUAL_P (binfo)) |
3507 | { | |
f1f41a6c | 3508 | vec_safe_push (*data, BINFO_TYPE (binfo)); |
e6014a82 | 3509 | } |
3510 | return NULL_TREE; | |
3511 | } | |
3512 | ||
3513 | /* Calculates the morally non-virtual base classes of a class */ | |
f1f41a6c | 3514 | static vec<tree, va_gc> * |
e6014a82 | 3515 | calculate_bases_helper (tree type) |
3516 | { | |
f1f41a6c | 3517 | vec<tree, va_gc> *vector = make_tree_vector(); |
e6014a82 | 3518 | |
3519 | /* Now add non-virtual base classes in order of construction */ | |
3520 | dfs_walk_all (TYPE_BINFO (type), | |
3521 | dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector); | |
3522 | return vector; | |
3523 | } | |
3524 | ||
3525 | tree | |
3526 | calculate_bases (tree type) | |
3527 | { | |
f1f41a6c | 3528 | vec<tree, va_gc> *vector = make_tree_vector(); |
e6014a82 | 3529 | tree bases_vec = NULL_TREE; |
3530 | unsigned i; | |
f1f41a6c | 3531 | vec<tree, va_gc> *vbases; |
3532 | vec<tree, va_gc> *nonvbases; | |
e6014a82 | 3533 | tree binfo; |
3534 | ||
3535 | complete_type (type); | |
3536 | ||
3537 | if (!NON_UNION_CLASS_TYPE_P (type)) | |
3538 | return make_tree_vec (0); | |
3539 | ||
3540 | /* First go through virtual base classes */ | |
3541 | for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0; | |
f1f41a6c | 3542 | vec_safe_iterate (vbases, i, &binfo); i++) |
e6014a82 | 3543 | { |
f1f41a6c | 3544 | vec<tree, va_gc> *vbase_bases; |
3545 | vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo)); | |
3546 | vec_safe_splice (vector, vbase_bases); | |
e6014a82 | 3547 | release_tree_vector (vbase_bases); |
3548 | } | |
3549 | ||
3550 | /* Now for the non-virtual bases */ | |
3551 | nonvbases = calculate_bases_helper (type); | |
f1f41a6c | 3552 | vec_safe_splice (vector, nonvbases); |
e6014a82 | 3553 | release_tree_vector (nonvbases); |
3554 | ||
3555 | /* Last element is entire class, so don't copy */ | |
f1f41a6c | 3556 | bases_vec = make_tree_vec (vector->length () - 1); |
e6014a82 | 3557 | |
f1f41a6c | 3558 | for (i = 0; i < vector->length () - 1; ++i) |
e6014a82 | 3559 | { |
f1f41a6c | 3560 | TREE_VEC_ELT (bases_vec, i) = (*vector)[i]; |
e6014a82 | 3561 | } |
3562 | release_tree_vector (vector); | |
3563 | return bases_vec; | |
3564 | } | |
3565 | ||
3566 | tree | |
3567 | finish_bases (tree type, bool direct) | |
3568 | { | |
3569 | tree bases = NULL_TREE; | |
3570 | ||
3571 | if (!processing_template_decl) | |
3572 | { | |
3573 | /* Parameter packs can only be used in templates */ | |
3574 | error ("Parameter pack __bases only valid in template declaration"); | |
3575 | return error_mark_node; | |
3576 | } | |
3577 | ||
3578 | bases = cxx_make_type (BASES); | |
3579 | BASES_TYPE (bases) = type; | |
3580 | BASES_DIRECT (bases) = direct; | |
3581 | SET_TYPE_STRUCTURAL_EQUALITY (bases); | |
3582 | ||
3583 | return bases; | |
3584 | } | |
3585 | ||
bf75f33a | 3586 | /* Perform C++-specific checks for __builtin_offsetof before calling |
3587 | fold_offsetof. */ | |
3588 | ||
3589 | tree | |
3590 | finish_offsetof (tree expr) | |
3591 | { | |
8d7dd437 | 3592 | if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) |
3593 | { | |
3594 | error ("cannot apply %<offsetof%> to destructor %<~%T%>", | |
3595 | TREE_OPERAND (expr, 2)); | |
3596 | return error_mark_node; | |
3597 | } | |
bf75f33a | 3598 | if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE |
3599 | || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE | |
4fdaf896 | 3600 | || TREE_TYPE (expr) == unknown_type_node) |
bf75f33a | 3601 | { |
ede90cc2 | 3602 | if (TREE_CODE (expr) == COMPONENT_REF |
3603 | || TREE_CODE (expr) == COMPOUND_EXPR) | |
86a737e0 | 3604 | expr = TREE_OPERAND (expr, 1); |
3605 | error ("cannot apply %<offsetof%> to member function %qD", expr); | |
bf75f33a | 3606 | return error_mark_node; |
3607 | } | |
8ce416a1 | 3608 | if (REFERENCE_REF_P (expr)) |
f23ce5f0 | 3609 | expr = TREE_OPERAND (expr, 0); |
972f6131 | 3610 | if (TREE_CODE (expr) == COMPONENT_REF) |
3611 | { | |
3612 | tree object = TREE_OPERAND (expr, 0); | |
3613 | if (!complete_type_or_else (TREE_TYPE (object), object)) | |
3614 | return error_mark_node; | |
3615 | } | |
7549df0d | 3616 | return fold_offsetof (expr); |
bf75f33a | 3617 | } |
3618 | ||
b9e35020 | 3619 | /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This |
3620 | function is broken out from the above for the benefit of the tree-ssa | |
3621 | project. */ | |
3622 | ||
3623 | void | |
3624 | simplify_aggr_init_expr (tree *tp) | |
3625 | { | |
3626 | tree aggr_init_expr = *tp; | |
3627 | ||
952cb193 | 3628 | /* Form an appropriate CALL_EXPR. */ |
c2f47e15 | 3629 | tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr); |
3630 | tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr); | |
bbdcc797 | 3631 | tree type = TREE_TYPE (slot); |
b9e35020 | 3632 | |
3633 | tree call_expr; | |
3634 | enum style_t { ctor, arg, pcc } style; | |
805e22b2 | 3635 | |
952cb193 | 3636 | if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr)) |
805e22b2 | 3637 | style = ctor; |
3638 | #ifdef PCC_STATIC_STRUCT_RETURN | |
3639 | else if (1) | |
3640 | style = pcc; | |
3641 | #endif | |
805e22b2 | 3642 | else |
2e3e31d2 | 3643 | { |
3644 | gcc_assert (TREE_ADDRESSABLE (type)); | |
3645 | style = arg; | |
3646 | } | |
805e22b2 | 3647 | |
389dd41b | 3648 | call_expr = build_call_array_loc (input_location, |
3649 | TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), | |
3650 | fn, | |
3651 | aggr_init_expr_nargs (aggr_init_expr), | |
3652 | AGGR_INIT_EXPR_ARGP (aggr_init_expr)); | |
c0f83203 | 3653 | TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr); |
c2f47e15 | 3654 | |
ea523851 | 3655 | if (style == ctor) |
952cb193 | 3656 | { |
ea523851 | 3657 | /* Replace the first argument to the ctor with the address of the |
3658 | slot. */ | |
9b86eec0 | 3659 | cxx_mark_addressable (slot); |
c2f47e15 | 3660 | CALL_EXPR_ARG (call_expr, 0) = |
3661 | build1 (ADDR_EXPR, build_pointer_type (type), slot); | |
952cb193 | 3662 | } |
c2f47e15 | 3663 | else if (style == arg) |
ea523851 | 3664 | { |
3665 | /* Just mark it addressable here, and leave the rest to | |
3666 | expand_call{,_inline}. */ | |
3667 | cxx_mark_addressable (slot); | |
3668 | CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true; | |
5f52d2e2 | 3669 | call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr); |
ea523851 | 3670 | } |
805e22b2 | 3671 | else if (style == pcc) |
952cb193 | 3672 | { |
805e22b2 | 3673 | /* If we're using the non-reentrant PCC calling convention, then we |
3674 | need to copy the returned value out of the static buffer into the | |
3675 | SLOT. */ | |
4cab8273 | 3676 | push_deferring_access_checks (dk_no_check); |
9a812d33 | 3677 | call_expr = build_aggr_init (slot, call_expr, |
ebd21de4 | 3678 | DIRECT_BIND | LOOKUP_ONLYCONVERTING, |
3679 | tf_warning_or_error); | |
4cab8273 | 3680 | pop_deferring_access_checks (); |
79e6e76b | 3681 | call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot); |
952cb193 | 3682 | } |
952cb193 | 3683 | |
a63dcad5 | 3684 | if (AGGR_INIT_ZERO_FIRST (aggr_init_expr)) |
3685 | { | |
3686 | tree init = build_zero_init (type, NULL_TREE, | |
3687 | /*static_storage_p=*/false); | |
3688 | init = build2 (INIT_EXPR, void_type_node, slot, init); | |
3689 | call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr), | |
3690 | init, call_expr); | |
3691 | } | |
3692 | ||
952cb193 | 3693 | *tp = call_expr; |
952cb193 | 3694 | } |
3695 | ||
2b82dde2 | 3696 | /* Emit all thunks to FN that should be emitted when FN is emitted. */ |
3697 | ||
84e10000 | 3698 | void |
807be5b4 | 3699 | emit_associated_thunks (tree fn) |
2b82dde2 | 3700 | { |
3701 | /* When we use vcall offsets, we emit thunks with the virtual | |
3702 | functions to which they thunk. The whole point of vcall offsets | |
3703 | is so that you can know statically the entire set of thunks that | |
3704 | will ever be needed for a given virtual function, thereby | |
3705 | enabling you to output all the thunks with the function itself. */ | |
34e5cced | 3706 | if (DECL_VIRTUAL_P (fn) |
3707 | /* Do not emit thunks for extern template instantiations. */ | |
3708 | && ! DECL_REALLY_EXTERN (fn)) | |
2b82dde2 | 3709 | { |
641985fa | 3710 | tree thunk; |
9031d10b | 3711 | |
1767a056 | 3712 | for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk)) |
805e22b2 | 3713 | { |
6709b660 | 3714 | if (!THUNK_ALIAS (thunk)) |
805e22b2 | 3715 | { |
4880ab99 | 3716 | use_thunk (thunk, /*emit_p=*/1); |
3717 | if (DECL_RESULT_THUNK_P (thunk)) | |
3718 | { | |
3719 | tree probe; | |
9031d10b | 3720 | |
4880ab99 | 3721 | for (probe = DECL_THUNKS (thunk); |
1767a056 | 3722 | probe; probe = DECL_CHAIN (probe)) |
4880ab99 | 3723 | use_thunk (probe, /*emit_p=*/1); |
3724 | } | |
805e22b2 | 3725 | } |
4880ab99 | 3726 | else |
b4df430b | 3727 | gcc_assert (!DECL_THUNKS (thunk)); |
805e22b2 | 3728 | } |
2b82dde2 | 3729 | } |
3730 | } | |
3731 | ||
262c8920 | 3732 | /* Returns true iff FUN is an instantiation of a constexpr function |
3733 | template. */ | |
3734 | ||
3735 | static inline bool | |
3736 | is_instantiation_of_constexpr (tree fun) | |
3737 | { | |
d6ef1dfb | 3738 | return (DECL_TEMPLOID_INSTANTIATION (fun) |
262c8920 | 3739 | && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT |
3740 | (DECL_TI_TEMPLATE (fun)))); | |
3741 | } | |
3742 | ||
019cf886 | 3743 | /* Generate RTL for FN. */ |
3744 | ||
ed772161 | 3745 | bool |
3746 | expand_or_defer_fn_1 (tree fn) | |
6cb758f0 | 3747 | { |
3748 | /* When the parser calls us after finishing the body of a template | |
7f233616 | 3749 | function, we don't really want to expand the body. */ |
3750 | if (processing_template_decl) | |
6cb758f0 | 3751 | { |
3752 | /* Normally, collection only occurs in rest_of_compilation. So, | |
3753 | if we don't collect here, we never collect junk generated | |
3754 | during the processing of templates until we hit a | |
dd63dd90 | 3755 | non-template function. It's not safe to do this inside a |
3756 | nested class, though, as the parser may have local state that | |
3757 | is not a GC root. */ | |
3758 | if (!function_depth) | |
3759 | ggc_collect (); | |
ed772161 | 3760 | return false; |
6cb758f0 | 3761 | } |
3762 | ||
bfec3452 | 3763 | gcc_assert (DECL_SAVED_TREE (fn)); |
75a70cf9 | 3764 | |
6cb758f0 | 3765 | /* If this is a constructor or destructor body, we have to clone |
3766 | it. */ | |
3767 | if (maybe_clone_body (fn)) | |
3768 | { | |
3769 | /* We don't want to process FN again, so pretend we've written | |
3770 | it out, even though we haven't. */ | |
3771 | TREE_ASM_WRITTEN (fn) = 1; | |
262c8920 | 3772 | /* If this is an instantiation of a constexpr function, keep |
3773 | DECL_SAVED_TREE for explain_invalid_constexpr_fn. */ | |
3774 | if (!is_instantiation_of_constexpr (fn)) | |
3775 | DECL_SAVED_TREE (fn) = NULL_TREE; | |
ed772161 | 3776 | return false; |
6cb758f0 | 3777 | } |
3778 | ||
caa6fdce | 3779 | /* We make a decision about linkage for these functions at the end |
3780 | of the compilation. Until that point, we do not want the back | |
3781 | end to output them -- but we do want it to see the bodies of | |
aa255322 | 3782 | these functions so that it can inline them as appropriate. */ |
caa6fdce | 3783 | if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn)) |
3784 | { | |
d9803664 | 3785 | if (DECL_INTERFACE_KNOWN (fn)) |
3786 | /* We've already made a decision as to how this function will | |
3787 | be handled. */; | |
3788 | else if (!at_eof) | |
caa6fdce | 3789 | { |
3790 | DECL_EXTERNAL (fn) = 1; | |
3791 | DECL_NOT_REALLY_EXTERN (fn) = 1; | |
3792 | note_vague_linkage_fn (fn); | |
d9803664 | 3793 | /* A non-template inline function with external linkage will |
3794 | always be COMDAT. As we must eventually determine the | |
3795 | linkage of all functions, and as that causes writes to | |
3796 | the data mapped in from the PCH file, it's advantageous | |
3797 | to mark the functions at this point. */ | |
3798 | if (!DECL_IMPLICIT_INSTANTIATION (fn)) | |
3799 | { | |
3800 | /* This function must have external linkage, as | |
3801 | otherwise DECL_INTERFACE_KNOWN would have been | |
3802 | set. */ | |
3803 | gcc_assert (TREE_PUBLIC (fn)); | |
3804 | comdat_linkage (fn); | |
3805 | DECL_INTERFACE_KNOWN (fn) = 1; | |
3806 | } | |
caa6fdce | 3807 | } |
3808 | else | |
3809 | import_export_decl (fn); | |
aa255322 | 3810 | |
3811 | /* If the user wants us to keep all inline functions, then mark | |
3812 | this function as needed so that finish_file will make sure to | |
aed4eb5d | 3813 | output it later. Similarly, all dllexport'd functions must |
3814 | be emitted; there may be callers in other DLLs. */ | |
0ef106fc | 3815 | if ((flag_keep_inline_functions |
3816 | && DECL_DECLARED_INLINE_P (fn) | |
3817 | && !DECL_REALLY_EXTERN (fn)) | |
0675168d | 3818 | || (flag_keep_inline_dllexport |
3819 | && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))) | |
ab185a23 | 3820 | { |
3821 | mark_needed (fn); | |
3822 | DECL_EXTERNAL (fn) = 0; | |
3823 | } | |
caa6fdce | 3824 | } |
3825 | ||
6cb758f0 | 3826 | /* There's no reason to do any of the work here if we're only doing |
3827 | semantic analysis; this code just generates RTL. */ | |
3828 | if (flag_syntax_only) | |
ed772161 | 3829 | return false; |
3830 | ||
3831 | return true; | |
3832 | } | |
6cb758f0 | 3833 | |
ed772161 | 3834 | void |
3835 | expand_or_defer_fn (tree fn) | |
3836 | { | |
3837 | if (expand_or_defer_fn_1 (fn)) | |
3838 | { | |
3839 | function_depth++; | |
70024a5e | 3840 | |
ed772161 | 3841 | /* Expand or defer, at the whim of the compilation unit manager. */ |
3842 | cgraph_finalize_function (fn, function_depth > 1); | |
28454517 | 3843 | emit_associated_thunks (fn); |
70024a5e | 3844 | |
ed772161 | 3845 | function_depth--; |
3846 | } | |
6cb758f0 | 3847 | } |
3848 | ||
4ee9c684 | 3849 | struct nrv_data |
3850 | { | |
3851 | tree var; | |
3852 | tree result; | |
d1455aa3 | 3853 | hash_table <pointer_hash <tree_node> > visited; |
4ee9c684 | 3854 | }; |
80ac742d | 3855 | |
4ee9c684 | 3856 | /* Helper function for walk_tree, used by finalize_nrv below. */ |
3857 | ||
3858 | static tree | |
3859 | finalize_nrv_r (tree* tp, int* walk_subtrees, void* data) | |
80ac742d | 3860 | { |
4ee9c684 | 3861 | struct nrv_data *dp = (struct nrv_data *)data; |
d1455aa3 | 3862 | tree_node **slot; |
4cfd9030 | 3863 | |
3864 | /* No need to walk into types. There wouldn't be any need to walk into | |
3865 | non-statements, except that we have to consider STMT_EXPRs. */ | |
80ac742d | 3866 | if (TYPE_P (*tp)) |
3867 | *walk_subtrees = 0; | |
4ee9c684 | 3868 | /* Change all returns to just refer to the RESULT_DECL; this is a nop, |
3869 | but differs from using NULL_TREE in that it indicates that we care | |
3870 | about the value of the RESULT_DECL. */ | |
c857cd60 | 3871 | else if (TREE_CODE (*tp) == RETURN_EXPR) |
3872 | TREE_OPERAND (*tp, 0) = dp->result; | |
4ee9c684 | 3873 | /* Change all cleanups for the NRV to only run when an exception is |
3874 | thrown. */ | |
4cfd9030 | 3875 | else if (TREE_CODE (*tp) == CLEANUP_STMT |
4ee9c684 | 3876 | && CLEANUP_DECL (*tp) == dp->var) |
a9bc793b | 3877 | CLEANUP_EH_ONLY (*tp) = 1; |
7dd37241 | 3878 | /* Replace the DECL_EXPR for the NRV with an initialization of the |
4ee9c684 | 3879 | RESULT_DECL, if needed. */ |
7dd37241 | 3880 | else if (TREE_CODE (*tp) == DECL_EXPR |
3881 | && DECL_EXPR_DECL (*tp) == dp->var) | |
4ee9c684 | 3882 | { |
3883 | tree init; | |
3884 | if (DECL_INITIAL (dp->var) | |
3885 | && DECL_INITIAL (dp->var) != error_mark_node) | |
d7daeaeb | 3886 | init = build2 (INIT_EXPR, void_type_node, dp->result, |
3887 | DECL_INITIAL (dp->var)); | |
4ee9c684 | 3888 | else |
e60a6f7b | 3889 | init = build_empty_stmt (EXPR_LOCATION (*tp)); |
d7daeaeb | 3890 | DECL_INITIAL (dp->var) = NULL_TREE; |
1cf1742e | 3891 | SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp)); |
4ee9c684 | 3892 | *tp = init; |
3893 | } | |
3894 | /* And replace all uses of the NRV with the RESULT_DECL. */ | |
3895 | else if (*tp == dp->var) | |
3896 | *tp = dp->result; | |
3897 | ||
3898 | /* Avoid walking into the same tree more than once. Unfortunately, we | |
3899 | can't just use walk_tree_without duplicates because it would only call | |
3900 | us for the first occurrence of dp->var in the function body. */ | |
d1455aa3 | 3901 | slot = dp->visited.find_slot (*tp, INSERT); |
4ee9c684 | 3902 | if (*slot) |
3903 | *walk_subtrees = 0; | |
3904 | else | |
3905 | *slot = *tp; | |
80ac742d | 3906 | |
3907 | /* Keep iterating. */ | |
3908 | return NULL_TREE; | |
3909 | } | |
3910 | ||
4ee9c684 | 3911 | /* Called from finish_function to implement the named return value |
c857cd60 | 3912 | optimization by overriding all the RETURN_EXPRs and pertinent |
4ee9c684 | 3913 | CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the |
3914 | RESULT_DECL for the function. */ | |
6528331d | 3915 | |
90e3d9ba | 3916 | void |
4ee9c684 | 3917 | finalize_nrv (tree *tp, tree var, tree result) |
6528331d | 3918 | { |
4ee9c684 | 3919 | struct nrv_data data; |
3920 | ||
93d0e758 | 3921 | /* Copy name from VAR to RESULT. */ |
4ee9c684 | 3922 | DECL_NAME (result) = DECL_NAME (var); |
4ee9c684 | 3923 | /* Don't forget that we take its address. */ |
3924 | TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var); | |
93d0e758 | 3925 | /* Finally set DECL_VALUE_EXPR to avoid assigning |
3926 | a stack slot at -O0 for the original var and debug info | |
3927 | uses RESULT location for VAR. */ | |
3928 | SET_DECL_VALUE_EXPR (var, result); | |
3929 | DECL_HAS_VALUE_EXPR_P (var) = 1; | |
4ee9c684 | 3930 | |
3931 | data.var = var; | |
3932 | data.result = result; | |
d1455aa3 | 3933 | data.visited.create (37); |
20a8f962 | 3934 | cp_walk_tree (tp, finalize_nrv_r, &data, 0); |
d1455aa3 | 3935 | data.visited.dispose (); |
1f1fa73c | 3936 | } |
8487df40 | 3937 | \f |
fd6481cf | 3938 | /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */ |
3939 | ||
3940 | bool | |
3941 | cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor, | |
3942 | bool need_copy_ctor, bool need_copy_assignment) | |
3943 | { | |
3944 | int save_errorcount = errorcount; | |
3945 | tree info, t; | |
3946 | ||
3947 | /* Always allocate 3 elements for simplicity. These are the | |
3948 | function decls for the ctor, dtor, and assignment op. | |
3949 | This layout is known to the three lang hooks, | |
3950 | cxx_omp_clause_default_init, cxx_omp_clause_copy_init, | |
3951 | and cxx_omp_clause_assign_op. */ | |
3952 | info = make_tree_vec (3); | |
3953 | CP_OMP_CLAUSE_INFO (c) = info; | |
3954 | ||
2ee92e27 | 3955 | if (need_default_ctor || need_copy_ctor) |
fd6481cf | 3956 | { |
3957 | if (need_default_ctor) | |
2ee92e27 | 3958 | t = get_default_ctor (type); |
fd6481cf | 3959 | else |
9b222de3 | 3960 | t = get_copy_ctor (type, tf_warning_or_error); |
2ee92e27 | 3961 | |
3962 | if (t && !trivial_fn_p (t)) | |
3963 | TREE_VEC_ELT (info, 0) = t; | |
fd6481cf | 3964 | } |
3965 | ||
3966 | if ((need_default_ctor || need_copy_ctor) | |
3967 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
9b222de3 | 3968 | TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error); |
fd6481cf | 3969 | |
2ee92e27 | 3970 | if (need_copy_assignment) |
fd6481cf | 3971 | { |
2ee92e27 | 3972 | t = get_copy_assign (type); |
3973 | ||
3974 | if (t && !trivial_fn_p (t)) | |
3975 | TREE_VEC_ELT (info, 2) = t; | |
fd6481cf | 3976 | } |
3977 | ||
3978 | return errorcount != save_errorcount; | |
3979 | } | |
3980 | ||
8487df40 | 3981 | /* For all elements of CLAUSES, validate them vs OpenMP constraints. |
3982 | Remove any elements from the list that are invalid. */ | |
3983 | ||
3984 | tree | |
3985 | finish_omp_clauses (tree clauses) | |
3986 | { | |
3987 | bitmap_head generic_head, firstprivate_head, lastprivate_head; | |
3988 | tree c, t, *pc = &clauses; | |
3989 | const char *name; | |
3990 | ||
3991 | bitmap_obstack_initialize (NULL); | |
3992 | bitmap_initialize (&generic_head, &bitmap_default_obstack); | |
3993 | bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); | |
3994 | bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); | |
3995 | ||
3996 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
3997 | { | |
3998 | bool remove = false; | |
3999 | ||
4000 | switch (OMP_CLAUSE_CODE (c)) | |
4001 | { | |
4002 | case OMP_CLAUSE_SHARED: | |
4003 | name = "shared"; | |
4004 | goto check_dup_generic; | |
4005 | case OMP_CLAUSE_PRIVATE: | |
4006 | name = "private"; | |
4007 | goto check_dup_generic; | |
4008 | case OMP_CLAUSE_REDUCTION: | |
4009 | name = "reduction"; | |
4010 | goto check_dup_generic; | |
4011 | case OMP_CLAUSE_COPYPRIVATE: | |
4012 | name = "copyprivate"; | |
4013 | goto check_dup_generic; | |
4014 | case OMP_CLAUSE_COPYIN: | |
4015 | name = "copyin"; | |
4016 | goto check_dup_generic; | |
4017 | check_dup_generic: | |
4018 | t = OMP_CLAUSE_DECL (c); | |
4019 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
4020 | { | |
4021 | if (processing_template_decl) | |
4022 | break; | |
6c1a068b | 4023 | if (DECL_P (t)) |
4024 | error ("%qD is not a variable in clause %qs", t, name); | |
4025 | else | |
4026 | error ("%qE is not a variable in clause %qs", t, name); | |
8487df40 | 4027 | remove = true; |
4028 | } | |
4029 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
4030 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) | |
4031 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
4032 | { | |
6c1a068b | 4033 | error ("%qD appears more than once in data clauses", t); |
8487df40 | 4034 | remove = true; |
4035 | } | |
4036 | else | |
4037 | bitmap_set_bit (&generic_head, DECL_UID (t)); | |
4038 | break; | |
4039 | ||
4040 | case OMP_CLAUSE_FIRSTPRIVATE: | |
4041 | t = OMP_CLAUSE_DECL (c); | |
4042 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
4043 | { | |
4044 | if (processing_template_decl) | |
4045 | break; | |
fc47df68 | 4046 | if (DECL_P (t)) |
4047 | error ("%qD is not a variable in clause %<firstprivate%>", t); | |
4048 | else | |
4049 | error ("%qE is not a variable in clause %<firstprivate%>", t); | |
8487df40 | 4050 | remove = true; |
4051 | } | |
4052 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
4053 | || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
4054 | { | |
fc47df68 | 4055 | error ("%qD appears more than once in data clauses", t); |
8487df40 | 4056 | remove = true; |
4057 | } | |
4058 | else | |
4059 | bitmap_set_bit (&firstprivate_head, DECL_UID (t)); | |
4060 | break; | |
4061 | ||
4062 | case OMP_CLAUSE_LASTPRIVATE: | |
4063 | t = OMP_CLAUSE_DECL (c); | |
4064 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
4065 | { | |
4066 | if (processing_template_decl) | |
4067 | break; | |
fc47df68 | 4068 | if (DECL_P (t)) |
4069 | error ("%qD is not a variable in clause %<lastprivate%>", t); | |
4070 | else | |
4071 | error ("%qE is not a variable in clause %<lastprivate%>", t); | |
8487df40 | 4072 | remove = true; |
4073 | } | |
4074 | else if (bitmap_bit_p (&generic_head, DECL_UID (t)) | |
4075 | || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) | |
4076 | { | |
fc47df68 | 4077 | error ("%qD appears more than once in data clauses", t); |
8487df40 | 4078 | remove = true; |
4079 | } | |
4080 | else | |
4081 | bitmap_set_bit (&lastprivate_head, DECL_UID (t)); | |
4082 | break; | |
4083 | ||
4084 | case OMP_CLAUSE_IF: | |
4085 | t = OMP_CLAUSE_IF_EXPR (c); | |
4086 | t = maybe_convert_cond (t); | |
4087 | if (t == error_mark_node) | |
4088 | remove = true; | |
a1e95a65 | 4089 | else if (!processing_template_decl) |
4090 | t = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
8487df40 | 4091 | OMP_CLAUSE_IF_EXPR (c) = t; |
4092 | break; | |
4093 | ||
2169f33b | 4094 | case OMP_CLAUSE_FINAL: |
4095 | t = OMP_CLAUSE_FINAL_EXPR (c); | |
4096 | t = maybe_convert_cond (t); | |
4097 | if (t == error_mark_node) | |
4098 | remove = true; | |
a1e95a65 | 4099 | else if (!processing_template_decl) |
4100 | t = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
2169f33b | 4101 | OMP_CLAUSE_FINAL_EXPR (c) = t; |
4102 | break; | |
4103 | ||
8487df40 | 4104 | case OMP_CLAUSE_NUM_THREADS: |
4105 | t = OMP_CLAUSE_NUM_THREADS_EXPR (c); | |
4106 | if (t == error_mark_node) | |
4107 | remove = true; | |
a8cb6ebc | 4108 | else if (!type_dependent_expression_p (t) |
4109 | && !INTEGRAL_TYPE_P (TREE_TYPE (t))) | |
8487df40 | 4110 | { |
4111 | error ("num_threads expression must be integral"); | |
4112 | remove = true; | |
4113 | } | |
80678da6 | 4114 | else |
a1e95a65 | 4115 | { |
4116 | t = mark_rvalue_use (t); | |
4117 | if (!processing_template_decl) | |
4118 | t = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
4119 | OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; | |
4120 | } | |
8487df40 | 4121 | break; |
4122 | ||
4123 | case OMP_CLAUSE_SCHEDULE: | |
4124 | t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c); | |
4125 | if (t == NULL) | |
4126 | ; | |
4127 | else if (t == error_mark_node) | |
4128 | remove = true; | |
a8cb6ebc | 4129 | else if (!type_dependent_expression_p (t) |
4130 | && !INTEGRAL_TYPE_P (TREE_TYPE (t))) | |
8487df40 | 4131 | { |
4132 | error ("schedule chunk size expression must be integral"); | |
4133 | remove = true; | |
4134 | } | |
80678da6 | 4135 | else |
a1e95a65 | 4136 | { |
4137 | t = mark_rvalue_use (t); | |
4138 | if (!processing_template_decl) | |
4139 | t = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
4140 | OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; | |
4141 | } | |
8487df40 | 4142 | break; |
4143 | ||
4144 | case OMP_CLAUSE_NOWAIT: | |
4145 | case OMP_CLAUSE_ORDERED: | |
4146 | case OMP_CLAUSE_DEFAULT: | |
fd6481cf | 4147 | case OMP_CLAUSE_UNTIED: |
4148 | case OMP_CLAUSE_COLLAPSE: | |
2169f33b | 4149 | case OMP_CLAUSE_MERGEABLE: |
8487df40 | 4150 | break; |
4151 | ||
4152 | default: | |
4153 | gcc_unreachable (); | |
4154 | } | |
4155 | ||
4156 | if (remove) | |
4157 | *pc = OMP_CLAUSE_CHAIN (c); | |
4158 | else | |
4159 | pc = &OMP_CLAUSE_CHAIN (c); | |
4160 | } | |
4161 | ||
4162 | for (pc = &clauses, c = clauses; c ; c = *pc) | |
4163 | { | |
bc620c5c | 4164 | enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c); |
8487df40 | 4165 | bool remove = false; |
4166 | bool need_complete_non_reference = false; | |
4167 | bool need_default_ctor = false; | |
4168 | bool need_copy_ctor = false; | |
4169 | bool need_copy_assignment = false; | |
4170 | bool need_implicitly_determined = false; | |
4171 | tree type, inner_type; | |
4172 | ||
4173 | switch (c_kind) | |
4174 | { | |
4175 | case OMP_CLAUSE_SHARED: | |
4176 | name = "shared"; | |
4177 | need_implicitly_determined = true; | |
4178 | break; | |
4179 | case OMP_CLAUSE_PRIVATE: | |
4180 | name = "private"; | |
4181 | need_complete_non_reference = true; | |
4182 | need_default_ctor = true; | |
4183 | need_implicitly_determined = true; | |
4184 | break; | |
4185 | case OMP_CLAUSE_FIRSTPRIVATE: | |
4186 | name = "firstprivate"; | |
4187 | need_complete_non_reference = true; | |
4188 | need_copy_ctor = true; | |
4189 | need_implicitly_determined = true; | |
4190 | break; | |
4191 | case OMP_CLAUSE_LASTPRIVATE: | |
4192 | name = "lastprivate"; | |
4193 | need_complete_non_reference = true; | |
4194 | need_copy_assignment = true; | |
4195 | need_implicitly_determined = true; | |
4196 | break; | |
4197 | case OMP_CLAUSE_REDUCTION: | |
4198 | name = "reduction"; | |
4199 | need_implicitly_determined = true; | |
4200 | break; | |
4201 | case OMP_CLAUSE_COPYPRIVATE: | |
4202 | name = "copyprivate"; | |
4203 | need_copy_assignment = true; | |
4204 | break; | |
4205 | case OMP_CLAUSE_COPYIN: | |
4206 | name = "copyin"; | |
4207 | need_copy_assignment = true; | |
4208 | break; | |
4209 | default: | |
4210 | pc = &OMP_CLAUSE_CHAIN (c); | |
4211 | continue; | |
4212 | } | |
4213 | ||
4214 | t = OMP_CLAUSE_DECL (c); | |
4215 | if (processing_template_decl | |
4216 | && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
4217 | { | |
4218 | pc = &OMP_CLAUSE_CHAIN (c); | |
4219 | continue; | |
4220 | } | |
4221 | ||
4222 | switch (c_kind) | |
4223 | { | |
4224 | case OMP_CLAUSE_LASTPRIVATE: | |
4225 | if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t))) | |
4226 | need_default_ctor = true; | |
4227 | break; | |
4228 | ||
4229 | case OMP_CLAUSE_REDUCTION: | |
4230 | if (AGGREGATE_TYPE_P (TREE_TYPE (t)) | |
4231 | || POINTER_TYPE_P (TREE_TYPE (t))) | |
4232 | { | |
4233 | error ("%qE has invalid type for %<reduction%>", t); | |
4234 | remove = true; | |
4235 | } | |
4236 | else if (FLOAT_TYPE_P (TREE_TYPE (t))) | |
4237 | { | |
4238 | enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); | |
4239 | switch (r_code) | |
4240 | { | |
4241 | case PLUS_EXPR: | |
4242 | case MULT_EXPR: | |
4243 | case MINUS_EXPR: | |
2169f33b | 4244 | case MIN_EXPR: |
4245 | case MAX_EXPR: | |
8487df40 | 4246 | break; |
4247 | default: | |
4248 | error ("%qE has invalid type for %<reduction(%s)%>", | |
4249 | t, operator_name_info[r_code].name); | |
4250 | remove = true; | |
4251 | } | |
4252 | } | |
4253 | break; | |
4254 | ||
4255 | case OMP_CLAUSE_COPYIN: | |
4256 | if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) | |
4257 | { | |
4258 | error ("%qE must be %<threadprivate%> for %<copyin%>", t); | |
4259 | remove = true; | |
4260 | } | |
4261 | break; | |
4262 | ||
4263 | default: | |
4264 | break; | |
4265 | } | |
4266 | ||
58d3add9 | 4267 | if (need_complete_non_reference || need_copy_assignment) |
8487df40 | 4268 | { |
4269 | t = require_complete_type (t); | |
4270 | if (t == error_mark_node) | |
4271 | remove = true; | |
58d3add9 | 4272 | else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE |
4273 | && need_complete_non_reference) | |
8487df40 | 4274 | { |
4275 | error ("%qE has reference type for %qs", t, name); | |
4276 | remove = true; | |
4277 | } | |
4278 | } | |
4279 | if (need_implicitly_determined) | |
4280 | { | |
4281 | const char *share_name = NULL; | |
4282 | ||
4283 | if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) | |
4284 | share_name = "threadprivate"; | |
4285 | else switch (cxx_omp_predetermined_sharing (t)) | |
4286 | { | |
4287 | case OMP_CLAUSE_DEFAULT_UNSPECIFIED: | |
4288 | break; | |
4289 | case OMP_CLAUSE_DEFAULT_SHARED: | |
2169f33b | 4290 | /* const vars may be specified in firstprivate clause. */ |
4291 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE | |
4292 | && cxx_omp_const_qual_no_mutable (t)) | |
4293 | break; | |
8487df40 | 4294 | share_name = "shared"; |
4295 | break; | |
4296 | case OMP_CLAUSE_DEFAULT_PRIVATE: | |
4297 | share_name = "private"; | |
4298 | break; | |
4299 | default: | |
4300 | gcc_unreachable (); | |
4301 | } | |
4302 | if (share_name) | |
4303 | { | |
4304 | error ("%qE is predetermined %qs for %qs", | |
4305 | t, share_name, name); | |
4306 | remove = true; | |
4307 | } | |
4308 | } | |
4309 | ||
4310 | /* We're interested in the base element, not arrays. */ | |
4311 | inner_type = type = TREE_TYPE (t); | |
4312 | while (TREE_CODE (inner_type) == ARRAY_TYPE) | |
4313 | inner_type = TREE_TYPE (inner_type); | |
4314 | ||
3b49899c | 4315 | /* Check for special function availability by building a call to one. |
8487df40 | 4316 | Save the results, because later we won't be in the right context |
4317 | for making these queries. */ | |
4318 | if (CLASS_TYPE_P (inner_type) | |
58d3add9 | 4319 | && COMPLETE_TYPE_P (inner_type) |
abb3caf7 | 4320 | && (need_default_ctor || need_copy_ctor || need_copy_assignment) |
fd6481cf | 4321 | && !type_dependent_expression_p (t) |
4322 | && cxx_omp_create_clause_info (c, inner_type, need_default_ctor, | |
4323 | need_copy_ctor, need_copy_assignment)) | |
4324 | remove = true; | |
8487df40 | 4325 | |
4326 | if (remove) | |
4327 | *pc = OMP_CLAUSE_CHAIN (c); | |
4328 | else | |
4329 | pc = &OMP_CLAUSE_CHAIN (c); | |
4330 | } | |
4331 | ||
4332 | bitmap_obstack_release (NULL); | |
4333 | return clauses; | |
4334 | } | |
4335 | ||
4336 | /* For all variables in the tree_list VARS, mark them as thread local. */ | |
4337 | ||
4338 | void | |
4339 | finish_omp_threadprivate (tree vars) | |
4340 | { | |
4341 | tree t; | |
4342 | ||
4343 | /* Mark every variable in VARS to be assigned thread local storage. */ | |
4344 | for (t = vars; t; t = TREE_CHAIN (t)) | |
4345 | { | |
4346 | tree v = TREE_PURPOSE (t); | |
4347 | ||
927fed67 | 4348 | if (error_operand_p (v)) |
4349 | ; | |
4350 | else if (TREE_CODE (v) != VAR_DECL) | |
4351 | error ("%<threadprivate%> %qD is not file, namespace " | |
4352 | "or block scope variable", v); | |
8487df40 | 4353 | /* If V had already been marked threadprivate, it doesn't matter |
4354 | whether it had been used prior to this point. */ | |
927fed67 | 4355 | else if (TREE_USED (v) |
8487df40 | 4356 | && (DECL_LANG_SPECIFIC (v) == NULL |
4357 | || !CP_DECL_THREADPRIVATE_P (v))) | |
4358 | error ("%qE declared %<threadprivate%> after first use", v); | |
4359 | else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v)) | |
4360 | error ("automatic variable %qE cannot be %<threadprivate%>", v); | |
c7623d7c | 4361 | else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v)))) |
8487df40 | 4362 | error ("%<threadprivate%> %qE has incomplete type", v); |
fd6481cf | 4363 | else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v)) |
4364 | && CP_DECL_CONTEXT (v) != current_class_type) | |
4365 | error ("%<threadprivate%> %qE directive not " | |
4366 | "in %qT definition", v, CP_DECL_CONTEXT (v)); | |
8487df40 | 4367 | else |
4368 | { | |
4369 | /* Allocate a LANG_SPECIFIC structure for V, if needed. */ | |
4370 | if (DECL_LANG_SPECIFIC (v) == NULL) | |
4371 | { | |
4372 | retrofit_lang_decl (v); | |
4373 | ||
4374 | /* Make sure that DECL_DISCRIMINATOR_P continues to be true | |
4375 | after the allocation of the lang_decl structure. */ | |
4376 | if (DECL_DISCRIMINATOR_P (v)) | |
39e70cbf | 4377 | DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1; |
8487df40 | 4378 | } |
4379 | ||
4380 | if (! DECL_THREAD_LOCAL_P (v)) | |
4381 | { | |
4382 | DECL_TLS_MODEL (v) = decl_default_tls_model (v); | |
4383 | /* If rtl has been already set for this var, call | |
4384 | make_decl_rtl once again, so that encode_section_info | |
4385 | has a chance to look at the new decl flags. */ | |
4386 | if (DECL_RTL_SET_P (v)) | |
4387 | make_decl_rtl (v); | |
4388 | } | |
4389 | CP_DECL_THREADPRIVATE_P (v) = 1; | |
4390 | } | |
4391 | } | |
4392 | } | |
4393 | ||
4394 | /* Build an OpenMP structured block. */ | |
4395 | ||
4396 | tree | |
4397 | begin_omp_structured_block (void) | |
4398 | { | |
4399 | return do_pushlevel (sk_omp); | |
4400 | } | |
4401 | ||
4402 | tree | |
4403 | finish_omp_structured_block (tree block) | |
4404 | { | |
4405 | return do_poplevel (block); | |
4406 | } | |
4407 | ||
3b49899c | 4408 | /* Similarly, except force the retention of the BLOCK. */ |
8487df40 | 4409 | |
4410 | tree | |
4411 | begin_omp_parallel (void) | |
4412 | { | |
4413 | keep_next_level (true); | |
4414 | return begin_omp_structured_block (); | |
4415 | } | |
4416 | ||
4417 | tree | |
4418 | finish_omp_parallel (tree clauses, tree body) | |
4419 | { | |
4420 | tree stmt; | |
4421 | ||
4422 | body = finish_omp_structured_block (body); | |
1f1fa73c | 4423 | |
8487df40 | 4424 | stmt = make_node (OMP_PARALLEL); |
4425 | TREE_TYPE (stmt) = void_type_node; | |
4426 | OMP_PARALLEL_CLAUSES (stmt) = clauses; | |
4427 | OMP_PARALLEL_BODY (stmt) = body; | |
41fb2c18 | 4428 | |
8487df40 | 4429 | return add_stmt (stmt); |
4430 | } | |
4431 | ||
fd6481cf | 4432 | tree |
4433 | begin_omp_task (void) | |
4434 | { | |
4435 | keep_next_level (true); | |
4436 | return begin_omp_structured_block (); | |
4437 | } | |
4438 | ||
4439 | tree | |
4440 | finish_omp_task (tree clauses, tree body) | |
4441 | { | |
4442 | tree stmt; | |
4443 | ||
4444 | body = finish_omp_structured_block (body); | |
4445 | ||
4446 | stmt = make_node (OMP_TASK); | |
4447 | TREE_TYPE (stmt) = void_type_node; | |
4448 | OMP_TASK_CLAUSES (stmt) = clauses; | |
4449 | OMP_TASK_BODY (stmt) = body; | |
4450 | ||
4451 | return add_stmt (stmt); | |
4452 | } | |
4453 | ||
4454 | /* Helper function for finish_omp_for. Convert Ith random access iterator | |
4455 | into integral iterator. Return FALSE if successful. */ | |
4456 | ||
4457 | static bool | |
4458 | handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv, | |
4459 | tree condv, tree incrv, tree *body, | |
4460 | tree *pre_body, tree clauses) | |
4461 | { | |
4462 | tree diff, iter_init, iter_incr = NULL, last; | |
4463 | tree incr_var = NULL, orig_pre_body, orig_body, c; | |
4464 | tree decl = TREE_VEC_ELT (declv, i); | |
4465 | tree init = TREE_VEC_ELT (initv, i); | |
4466 | tree cond = TREE_VEC_ELT (condv, i); | |
4467 | tree incr = TREE_VEC_ELT (incrv, i); | |
4468 | tree iter = decl; | |
4469 | location_t elocus = locus; | |
4470 | ||
4471 | if (init && EXPR_HAS_LOCATION (init)) | |
4472 | elocus = EXPR_LOCATION (init); | |
4473 | ||
4474 | switch (TREE_CODE (cond)) | |
4475 | { | |
4476 | case GT_EXPR: | |
4477 | case GE_EXPR: | |
4478 | case LT_EXPR: | |
4479 | case LE_EXPR: | |
4390875c | 4480 | if (TREE_OPERAND (cond, 1) == iter) |
4481 | cond = build2 (swap_tree_comparison (TREE_CODE (cond)), | |
4482 | TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0)); | |
fd6481cf | 4483 | if (TREE_OPERAND (cond, 0) != iter) |
4484 | cond = error_mark_node; | |
4485 | else | |
4486 | { | |
255b5d15 | 4487 | tree tem = build_x_binary_op (EXPR_LOCATION (cond), |
4488 | TREE_CODE (cond), | |
ef0b0c72 | 4489 | iter, ERROR_MARK, |
fd6481cf | 4490 | TREE_OPERAND (cond, 1), ERROR_MARK, |
4491 | NULL, tf_warning_or_error); | |
4492 | if (error_operand_p (tem)) | |
4493 | return true; | |
4494 | } | |
4495 | break; | |
4496 | default: | |
4497 | cond = error_mark_node; | |
4498 | break; | |
4499 | } | |
4500 | if (cond == error_mark_node) | |
4501 | { | |
ccb59bb6 | 4502 | error_at (elocus, "invalid controlling predicate"); |
fd6481cf | 4503 | return true; |
4504 | } | |
255b5d15 | 4505 | diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1), |
fd6481cf | 4506 | ERROR_MARK, iter, ERROR_MARK, NULL, |
4507 | tf_warning_or_error); | |
4508 | if (error_operand_p (diff)) | |
4509 | return true; | |
4510 | if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE) | |
4511 | { | |
ccb59bb6 | 4512 | error_at (elocus, "difference between %qE and %qD does not have integer type", |
4513 | TREE_OPERAND (cond, 1), iter); | |
fd6481cf | 4514 | return true; |
4515 | } | |
4516 | ||
4517 | switch (TREE_CODE (incr)) | |
4518 | { | |
4519 | case PREINCREMENT_EXPR: | |
4520 | case PREDECREMENT_EXPR: | |
4521 | case POSTINCREMENT_EXPR: | |
4522 | case POSTDECREMENT_EXPR: | |
4523 | if (TREE_OPERAND (incr, 0) != iter) | |
4524 | { | |
4525 | incr = error_mark_node; | |
4526 | break; | |
4527 | } | |
255b5d15 | 4528 | iter_incr = build_x_unary_op (EXPR_LOCATION (incr), |
4529 | TREE_CODE (incr), iter, | |
fd6481cf | 4530 | tf_warning_or_error); |
4531 | if (error_operand_p (iter_incr)) | |
4532 | return true; | |
4533 | else if (TREE_CODE (incr) == PREINCREMENT_EXPR | |
4534 | || TREE_CODE (incr) == POSTINCREMENT_EXPR) | |
4535 | incr = integer_one_node; | |
4536 | else | |
4537 | incr = integer_minus_one_node; | |
4538 | break; | |
4539 | case MODIFY_EXPR: | |
4540 | if (TREE_OPERAND (incr, 0) != iter) | |
4541 | incr = error_mark_node; | |
4542 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR | |
4543 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) | |
4544 | { | |
4545 | tree rhs = TREE_OPERAND (incr, 1); | |
4546 | if (TREE_OPERAND (rhs, 0) == iter) | |
4547 | { | |
4548 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1))) | |
4549 | != INTEGER_TYPE) | |
4550 | incr = error_mark_node; | |
4551 | else | |
4552 | { | |
255b5d15 | 4553 | iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs), |
4554 | iter, TREE_CODE (rhs), | |
fd6481cf | 4555 | TREE_OPERAND (rhs, 1), |
4556 | tf_warning_or_error); | |
4557 | if (error_operand_p (iter_incr)) | |
4558 | return true; | |
4559 | incr = TREE_OPERAND (rhs, 1); | |
c4698a21 | 4560 | incr = cp_convert (TREE_TYPE (diff), incr, |
4561 | tf_warning_or_error); | |
fd6481cf | 4562 | if (TREE_CODE (rhs) == MINUS_EXPR) |
4563 | { | |
4564 | incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr); | |
4565 | incr = fold_if_not_in_template (incr); | |
4566 | } | |
4567 | if (TREE_CODE (incr) != INTEGER_CST | |
4568 | && (TREE_CODE (incr) != NOP_EXPR | |
4569 | || (TREE_CODE (TREE_OPERAND (incr, 0)) | |
4570 | != INTEGER_CST))) | |
4571 | iter_incr = NULL; | |
4572 | } | |
4573 | } | |
4574 | else if (TREE_OPERAND (rhs, 1) == iter) | |
4575 | { | |
4576 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE | |
4577 | || TREE_CODE (rhs) != PLUS_EXPR) | |
4578 | incr = error_mark_node; | |
4579 | else | |
4580 | { | |
255b5d15 | 4581 | iter_incr = build_x_binary_op (EXPR_LOCATION (rhs), |
4582 | PLUS_EXPR, | |
fd6481cf | 4583 | TREE_OPERAND (rhs, 0), |
4584 | ERROR_MARK, iter, | |
4585 | ERROR_MARK, NULL, | |
4586 | tf_warning_or_error); | |
4587 | if (error_operand_p (iter_incr)) | |
4588 | return true; | |
255b5d15 | 4589 | iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs), |
4590 | iter, NOP_EXPR, | |
fd6481cf | 4591 | iter_incr, |
4592 | tf_warning_or_error); | |
4593 | if (error_operand_p (iter_incr)) | |
4594 | return true; | |
4595 | incr = TREE_OPERAND (rhs, 0); | |
4596 | iter_incr = NULL; | |
4597 | } | |
4598 | } | |
4599 | else | |
4600 | incr = error_mark_node; | |
4601 | } | |
4602 | else | |
4603 | incr = error_mark_node; | |
4604 | break; | |
4605 | default: | |
4606 | incr = error_mark_node; | |
4607 | break; | |
4608 | } | |
4609 | ||
4610 | if (incr == error_mark_node) | |
4611 | { | |
ccb59bb6 | 4612 | error_at (elocus, "invalid increment expression"); |
fd6481cf | 4613 | return true; |
4614 | } | |
4615 | ||
c4698a21 | 4616 | incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error); |
fd6481cf | 4617 | for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) |
4618 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE | |
4619 | && OMP_CLAUSE_DECL (c) == iter) | |
4620 | break; | |
4621 | ||
4622 | decl = create_temporary_var (TREE_TYPE (diff)); | |
4623 | pushdecl (decl); | |
4624 | add_decl_expr (decl); | |
4625 | last = create_temporary_var (TREE_TYPE (diff)); | |
4626 | pushdecl (last); | |
4627 | add_decl_expr (last); | |
4628 | if (c && iter_incr == NULL) | |
4629 | { | |
4630 | incr_var = create_temporary_var (TREE_TYPE (diff)); | |
4631 | pushdecl (incr_var); | |
4632 | add_decl_expr (incr_var); | |
4633 | } | |
4634 | gcc_assert (stmts_are_full_exprs_p ()); | |
4635 | ||
4636 | orig_pre_body = *pre_body; | |
4637 | *pre_body = push_stmt_list (); | |
4638 | if (orig_pre_body) | |
4639 | add_stmt (orig_pre_body); | |
4640 | if (init != NULL) | |
255b5d15 | 4641 | finish_expr_stmt (build_x_modify_expr (elocus, |
4642 | iter, NOP_EXPR, init, | |
fd6481cf | 4643 | tf_warning_or_error)); |
4644 | init = build_int_cst (TREE_TYPE (diff), 0); | |
4645 | if (c && iter_incr == NULL) | |
4646 | { | |
255b5d15 | 4647 | finish_expr_stmt (build_x_modify_expr (elocus, |
4648 | incr_var, NOP_EXPR, | |
fd6481cf | 4649 | incr, tf_warning_or_error)); |
4650 | incr = incr_var; | |
255b5d15 | 4651 | iter_incr = build_x_modify_expr (elocus, |
4652 | iter, PLUS_EXPR, incr, | |
fd6481cf | 4653 | tf_warning_or_error); |
4654 | } | |
255b5d15 | 4655 | finish_expr_stmt (build_x_modify_expr (elocus, |
4656 | last, NOP_EXPR, init, | |
fd6481cf | 4657 | tf_warning_or_error)); |
4658 | *pre_body = pop_stmt_list (*pre_body); | |
4659 | ||
8e70fb09 | 4660 | cond = cp_build_binary_op (elocus, |
4661 | TREE_CODE (cond), decl, diff, | |
fd6481cf | 4662 | tf_warning_or_error); |
8458f4ca | 4663 | incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR, |
e60a6f7b | 4664 | elocus, incr, NULL_TREE); |
fd6481cf | 4665 | |
4666 | orig_body = *body; | |
4667 | *body = push_stmt_list (); | |
4668 | iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last); | |
255b5d15 | 4669 | iter_init = build_x_modify_expr (elocus, |
4670 | iter, PLUS_EXPR, iter_init, | |
fd6481cf | 4671 | tf_warning_or_error); |
4672 | iter_init = build1 (NOP_EXPR, void_type_node, iter_init); | |
4673 | finish_expr_stmt (iter_init); | |
255b5d15 | 4674 | finish_expr_stmt (build_x_modify_expr (elocus, |
4675 | last, NOP_EXPR, decl, | |
fd6481cf | 4676 | tf_warning_or_error)); |
4677 | add_stmt (orig_body); | |
4678 | *body = pop_stmt_list (*body); | |
4679 | ||
4680 | if (c) | |
4681 | { | |
4682 | OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list (); | |
4683 | finish_expr_stmt (iter_incr); | |
4684 | OMP_CLAUSE_LASTPRIVATE_STMT (c) | |
4685 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c)); | |
4686 | } | |
4687 | ||
4688 | TREE_VEC_ELT (declv, i) = decl; | |
4689 | TREE_VEC_ELT (initv, i) = init; | |
4690 | TREE_VEC_ELT (condv, i) = cond; | |
4691 | TREE_VEC_ELT (incrv, i) = incr; | |
4692 | ||
4693 | return false; | |
4694 | } | |
4695 | ||
8487df40 | 4696 | /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR |
4697 | are directly for their associated operands in the statement. DECL | |
4698 | and INIT are a combo; if DECL is NULL then INIT ought to be a | |
4699 | MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are | |
4700 | optional statements that need to go before the loop into its | |
4701 | sk_omp scope. */ | |
4702 | ||
4703 | tree | |
fd6481cf | 4704 | finish_omp_for (location_t locus, tree declv, tree initv, tree condv, |
4705 | tree incrv, tree body, tree pre_body, tree clauses) | |
4706 | { | |
4707 | tree omp_for = NULL, orig_incr = NULL; | |
4708 | tree decl, init, cond, incr; | |
4709 | location_t elocus; | |
4710 | int i; | |
4711 | ||
4712 | gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv)); | |
4713 | gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv)); | |
4714 | gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv)); | |
4715 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) | |
4716 | { | |
4717 | decl = TREE_VEC_ELT (declv, i); | |
4718 | init = TREE_VEC_ELT (initv, i); | |
4719 | cond = TREE_VEC_ELT (condv, i); | |
4720 | incr = TREE_VEC_ELT (incrv, i); | |
4721 | elocus = locus; | |
4722 | ||
4723 | if (decl == NULL) | |
4724 | { | |
4725 | if (init != NULL) | |
4726 | switch (TREE_CODE (init)) | |
8487df40 | 4727 | { |
fd6481cf | 4728 | case MODIFY_EXPR: |
8487df40 | 4729 | decl = TREE_OPERAND (init, 0); |
fd6481cf | 4730 | init = TREE_OPERAND (init, 1); |
4731 | break; | |
4732 | case MODOP_EXPR: | |
4733 | if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR) | |
4734 | { | |
4735 | decl = TREE_OPERAND (init, 0); | |
4736 | init = TREE_OPERAND (init, 2); | |
4737 | } | |
4738 | break; | |
4739 | default: | |
4740 | break; | |
8487df40 | 4741 | } |
8487df40 | 4742 | |
fd6481cf | 4743 | if (decl == NULL) |
4744 | { | |
ccb59bb6 | 4745 | error_at (locus, |
4746 | "expected iteration declaration or initialization"); | |
fd6481cf | 4747 | return NULL; |
4748 | } | |
8487df40 | 4749 | } |
8487df40 | 4750 | |
fd6481cf | 4751 | if (init && EXPR_HAS_LOCATION (init)) |
4752 | elocus = EXPR_LOCATION (init); | |
8487df40 | 4753 | |
4754 | if (cond == NULL) | |
4755 | { | |
ccb59bb6 | 4756 | error_at (elocus, "missing controlling predicate"); |
8487df40 | 4757 | return NULL; |
4758 | } | |
4759 | ||
4760 | if (incr == NULL) | |
4761 | { | |
ccb59bb6 | 4762 | error_at (elocus, "missing increment expression"); |
8487df40 | 4763 | return NULL; |
4764 | } | |
4765 | ||
fd6481cf | 4766 | TREE_VEC_ELT (declv, i) = decl; |
4767 | TREE_VEC_ELT (initv, i) = init; | |
4768 | } | |
4769 | ||
4770 | if (dependent_omp_for_p (declv, initv, condv, incrv)) | |
4771 | { | |
4772 | tree stmt; | |
4773 | ||
8487df40 | 4774 | stmt = make_node (OMP_FOR); |
4775 | ||
fd6481cf | 4776 | for (i = 0; i < TREE_VEC_LENGTH (declv); i++) |
4777 | { | |
4778 | /* This is really just a place-holder. We'll be decomposing this | |
4779 | again and going through the cp_build_modify_expr path below when | |
4780 | we instantiate the thing. */ | |
4781 | TREE_VEC_ELT (initv, i) | |
4782 | = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i), | |
4783 | TREE_VEC_ELT (initv, i)); | |
4784 | } | |
8487df40 | 4785 | |
4786 | TREE_TYPE (stmt) = void_type_node; | |
fd6481cf | 4787 | OMP_FOR_INIT (stmt) = initv; |
4788 | OMP_FOR_COND (stmt) = condv; | |
4789 | OMP_FOR_INCR (stmt) = incrv; | |
8487df40 | 4790 | OMP_FOR_BODY (stmt) = body; |
4791 | OMP_FOR_PRE_BODY (stmt) = pre_body; | |
fd6481cf | 4792 | OMP_FOR_CLAUSES (stmt) = clauses; |
8487df40 | 4793 | |
4794 | SET_EXPR_LOCATION (stmt, locus); | |
4795 | return add_stmt (stmt); | |
4796 | } | |
4797 | ||
fd6481cf | 4798 | if (processing_template_decl) |
4799 | orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv)); | |
8487df40 | 4800 | |
fd6481cf | 4801 | for (i = 0; i < TREE_VEC_LENGTH (declv); ) |
a46a7e42 | 4802 | { |
fd6481cf | 4803 | decl = TREE_VEC_ELT (declv, i); |
4804 | init = TREE_VEC_ELT (initv, i); | |
4805 | cond = TREE_VEC_ELT (condv, i); | |
4806 | incr = TREE_VEC_ELT (incrv, i); | |
4807 | if (orig_incr) | |
4808 | TREE_VEC_ELT (orig_incr, i) = incr; | |
4809 | elocus = locus; | |
4810 | ||
4811 | if (init && EXPR_HAS_LOCATION (init)) | |
a46a7e42 | 4812 | elocus = EXPR_LOCATION (init); |
a46a7e42 | 4813 | |
fd6481cf | 4814 | if (!DECL_P (decl)) |
4815 | { | |
ccb59bb6 | 4816 | error_at (elocus, "expected iteration declaration or initialization"); |
fd6481cf | 4817 | return NULL; |
4818 | } | |
b25fbb3e | 4819 | |
fd6481cf | 4820 | if (incr && TREE_CODE (incr) == MODOP_EXPR) |
4821 | { | |
4822 | if (orig_incr) | |
4823 | TREE_VEC_ELT (orig_incr, i) = incr; | |
4824 | incr = cp_build_modify_expr (TREE_OPERAND (incr, 0), | |
4825 | TREE_CODE (TREE_OPERAND (incr, 1)), | |
4826 | TREE_OPERAND (incr, 2), | |
4827 | tf_warning_or_error); | |
4828 | } | |
4829 | ||
4830 | if (CLASS_TYPE_P (TREE_TYPE (decl))) | |
4831 | { | |
4832 | if (handle_omp_for_class_iterator (i, locus, declv, initv, condv, | |
4833 | incrv, &body, &pre_body, clauses)) | |
4834 | return NULL; | |
4835 | continue; | |
4836 | } | |
4837 | ||
4838 | if (!INTEGRAL_TYPE_P (TREE_TYPE (decl)) | |
4839 | && TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE) | |
4840 | { | |
ccb59bb6 | 4841 | error_at (elocus, "invalid type for iteration variable %qE", decl); |
fd6481cf | 4842 | return NULL; |
4843 | } | |
b25fbb3e | 4844 | |
962cad33 | 4845 | if (!processing_template_decl) |
bacfcc02 | 4846 | { |
4847 | init = fold_build_cleanup_point_expr (TREE_TYPE (init), init); | |
4848 | init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error); | |
4849 | } | |
4850 | else | |
4851 | init = build2 (MODIFY_EXPR, void_type_node, decl, init); | |
267f2f49 | 4852 | if (cond |
4853 | && TREE_SIDE_EFFECTS (cond) | |
4854 | && COMPARISON_CLASS_P (cond) | |
4855 | && !processing_template_decl) | |
fd6481cf | 4856 | { |
267f2f49 | 4857 | tree t = TREE_OPERAND (cond, 0); |
4858 | if (TREE_SIDE_EFFECTS (t) | |
4859 | && t != decl | |
4860 | && (TREE_CODE (t) != NOP_EXPR | |
4861 | || TREE_OPERAND (t, 0) != decl)) | |
4862 | TREE_OPERAND (cond, 0) | |
4863 | = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
fd6481cf | 4864 | |
267f2f49 | 4865 | t = TREE_OPERAND (cond, 1); |
4866 | if (TREE_SIDE_EFFECTS (t) | |
4867 | && t != decl | |
4868 | && (TREE_CODE (t) != NOP_EXPR | |
4869 | || TREE_OPERAND (t, 0) != decl)) | |
4870 | TREE_OPERAND (cond, 1) | |
fd6481cf | 4871 | = fold_build_cleanup_point_expr (TREE_TYPE (t), t); |
4872 | } | |
4873 | if (decl == error_mark_node || init == error_mark_node) | |
4874 | return NULL; | |
4875 | ||
4876 | TREE_VEC_ELT (declv, i) = decl; | |
4877 | TREE_VEC_ELT (initv, i) = init; | |
4878 | TREE_VEC_ELT (condv, i) = cond; | |
4879 | TREE_VEC_ELT (incrv, i) = incr; | |
4880 | i++; | |
b25fbb3e | 4881 | } |
fd6481cf | 4882 | |
4883 | if (IS_EMPTY_STMT (pre_body)) | |
4884 | pre_body = NULL; | |
4885 | ||
4886 | omp_for = c_finish_omp_for (locus, declv, initv, condv, incrv, | |
4887 | body, pre_body); | |
4888 | ||
4889 | if (omp_for == NULL) | |
4890 | return NULL; | |
4891 | ||
4892 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++) | |
b25fbb3e | 4893 | { |
267f2f49 | 4894 | decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0); |
4895 | incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i); | |
b25fbb3e | 4896 | |
fd6481cf | 4897 | if (TREE_CODE (incr) != MODIFY_EXPR) |
4898 | continue; | |
4899 | ||
4900 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1)) | |
267f2f49 | 4901 | && BINARY_CLASS_P (TREE_OPERAND (incr, 1)) |
4902 | && !processing_template_decl) | |
fd6481cf | 4903 | { |
267f2f49 | 4904 | tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0); |
4905 | if (TREE_SIDE_EFFECTS (t) | |
4906 | && t != decl | |
4907 | && (TREE_CODE (t) != NOP_EXPR | |
4908 | || TREE_OPERAND (t, 0) != decl)) | |
4909 | TREE_OPERAND (TREE_OPERAND (incr, 1), 0) | |
4910 | = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
fd6481cf | 4911 | |
267f2f49 | 4912 | t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1); |
4913 | if (TREE_SIDE_EFFECTS (t) | |
4914 | && t != decl | |
4915 | && (TREE_CODE (t) != NOP_EXPR | |
4916 | || TREE_OPERAND (t, 0) != decl)) | |
4917 | TREE_OPERAND (TREE_OPERAND (incr, 1), 1) | |
4918 | = fold_build_cleanup_point_expr (TREE_TYPE (t), t); | |
fd6481cf | 4919 | } |
4920 | ||
4921 | if (orig_incr) | |
4922 | TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i); | |
b25fbb3e | 4923 | } |
fd6481cf | 4924 | if (omp_for != NULL) |
4925 | OMP_FOR_CLAUSES (omp_for) = clauses; | |
b25fbb3e | 4926 | return omp_for; |
8487df40 | 4927 | } |
4928 | ||
4929 | void | |
2169f33b | 4930 | finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs, |
4931 | tree rhs, tree v, tree lhs1, tree rhs1) | |
8487df40 | 4932 | { |
221e68f2 | 4933 | tree orig_lhs; |
4934 | tree orig_rhs; | |
2169f33b | 4935 | tree orig_v; |
4936 | tree orig_lhs1; | |
4937 | tree orig_rhs1; | |
221e68f2 | 4938 | bool dependent_p; |
6d2e04be | 4939 | tree stmt; |
4940 | ||
221e68f2 | 4941 | orig_lhs = lhs; |
4942 | orig_rhs = rhs; | |
2169f33b | 4943 | orig_v = v; |
4944 | orig_lhs1 = lhs1; | |
4945 | orig_rhs1 = rhs1; | |
221e68f2 | 4946 | dependent_p = false; |
4947 | stmt = NULL_TREE; | |
4948 | ||
4949 | /* Even in a template, we can detect invalid uses of the atomic | |
4950 | pragma if neither LHS nor RHS is type-dependent. */ | |
4951 | if (processing_template_decl) | |
6d2e04be | 4952 | { |
221e68f2 | 4953 | dependent_p = (type_dependent_expression_p (lhs) |
2169f33b | 4954 | || (rhs && type_dependent_expression_p (rhs)) |
4955 | || (v && type_dependent_expression_p (v)) | |
4956 | || (lhs1 && type_dependent_expression_p (lhs1)) | |
4957 | || (rhs1 && type_dependent_expression_p (rhs1))); | |
221e68f2 | 4958 | if (!dependent_p) |
6d2e04be | 4959 | { |
4960 | lhs = build_non_dependent_expr (lhs); | |
2169f33b | 4961 | if (rhs) |
4962 | rhs = build_non_dependent_expr (rhs); | |
4963 | if (v) | |
4964 | v = build_non_dependent_expr (v); | |
4965 | if (lhs1) | |
4966 | lhs1 = build_non_dependent_expr (lhs1); | |
4967 | if (rhs1) | |
4968 | rhs1 = build_non_dependent_expr (rhs1); | |
6d2e04be | 4969 | } |
221e68f2 | 4970 | } |
4971 | if (!dependent_p) | |
4972 | { | |
2169f33b | 4973 | stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs, |
4974 | v, lhs1, rhs1); | |
221e68f2 | 4975 | if (stmt == error_mark_node) |
4976 | return; | |
6d2e04be | 4977 | } |
221e68f2 | 4978 | if (processing_template_decl) |
2169f33b | 4979 | { |
4980 | if (code == OMP_ATOMIC_READ) | |
4981 | { | |
255b5d15 | 4982 | stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs), |
4983 | OMP_ATOMIC_READ, orig_lhs); | |
2169f33b | 4984 | stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt); |
4985 | } | |
4986 | else | |
4987 | { | |
4988 | if (opcode == NOP_EXPR) | |
4989 | stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs); | |
4990 | else | |
4991 | stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs); | |
4992 | if (orig_rhs1) | |
255b5d15 | 4993 | stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1), |
4994 | COMPOUND_EXPR, orig_rhs1, stmt); | |
2169f33b | 4995 | if (code != OMP_ATOMIC) |
4996 | { | |
255b5d15 | 4997 | stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1), |
4998 | code, orig_lhs1, stmt); | |
2169f33b | 4999 | stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt); |
5000 | } | |
5001 | } | |
5002 | stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt); | |
5003 | } | |
221e68f2 | 5004 | add_stmt (stmt); |
8487df40 | 5005 | } |
5006 | ||
5007 | void | |
5008 | finish_omp_barrier (void) | |
5009 | { | |
b9a16870 | 5010 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER); |
f1f41a6c | 5011 | vec<tree, va_gc> *vec = make_tree_vector (); |
f352a3fb | 5012 | tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error); |
5013 | release_tree_vector (vec); | |
8487df40 | 5014 | finish_expr_stmt (stmt); |
5015 | } | |
5016 | ||
5017 | void | |
5018 | finish_omp_flush (void) | |
5019 | { | |
b9a16870 | 5020 | tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE); |
f1f41a6c | 5021 | vec<tree, va_gc> *vec = make_tree_vector (); |
f352a3fb | 5022 | tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error); |
5023 | release_tree_vector (vec); | |
8487df40 | 5024 | finish_expr_stmt (stmt); |
5025 | } | |
5026 | ||
fd6481cf | 5027 | void |
5028 | finish_omp_taskwait (void) | |
8487df40 | 5029 | { |
b9a16870 | 5030 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT); |
f1f41a6c | 5031 | vec<tree, va_gc> *vec = make_tree_vector (); |
f352a3fb | 5032 | tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error); |
5033 | release_tree_vector (vec); | |
fd6481cf | 5034 | finish_expr_stmt (stmt); |
8487df40 | 5035 | } |
2169f33b | 5036 | |
5037 | void | |
5038 | finish_omp_taskyield (void) | |
5039 | { | |
b9a16870 | 5040 | tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD); |
f1f41a6c | 5041 | vec<tree, va_gc> *vec = make_tree_vector (); |
2169f33b | 5042 | tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error); |
5043 | release_tree_vector (vec); | |
5044 | finish_expr_stmt (stmt); | |
5045 | } | |
8487df40 | 5046 | \f |
4c0315d0 | 5047 | /* Begin a __transaction_atomic or __transaction_relaxed statement. |
5048 | If PCOMPOUND is non-null, this is for a function-transaction-block, and we | |
5049 | should create an extra compound stmt. */ | |
5050 | ||
5051 | tree | |
5052 | begin_transaction_stmt (location_t loc, tree *pcompound, int flags) | |
5053 | { | |
5054 | tree r; | |
5055 | ||
5056 | if (pcompound) | |
5057 | *pcompound = begin_compound_stmt (0); | |
5058 | ||
5059 | r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE); | |
5060 | ||
5061 | /* Only add the statement to the function if support enabled. */ | |
5062 | if (flag_tm) | |
5063 | add_stmt (r); | |
5064 | else | |
5065 | error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0 | |
5066 | ? G_("%<__transaction_relaxed%> without " | |
5067 | "transactional memory support enabled") | |
5068 | : G_("%<__transaction_atomic%> without " | |
5069 | "transactional memory support enabled"))); | |
5070 | ||
5071 | TRANSACTION_EXPR_BODY (r) = push_stmt_list (); | |
5072 | return r; | |
5073 | } | |
5074 | ||
5075 | /* End a __transaction_atomic or __transaction_relaxed statement. | |
5076 | If COMPOUND_STMT is non-null, this is for a function-transaction-block, | |
f770bf53 | 5077 | and we should end the compound. If NOEX is non-NULL, we wrap the body in |
5078 | a MUST_NOT_THROW_EXPR with NOEX as condition. */ | |
4c0315d0 | 5079 | |
5080 | void | |
f770bf53 | 5081 | finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex) |
4c0315d0 | 5082 | { |
5083 | TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt)); | |
5084 | TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0; | |
5085 | TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0; | |
5086 | TRANSACTION_EXPR_IS_STMT (stmt) = 1; | |
5087 | ||
f770bf53 | 5088 | /* noexcept specifications are not allowed for function transactions. */ |
5089 | gcc_assert (!(noex && compound_stmt)); | |
5090 | if (noex) | |
5091 | { | |
5092 | tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt), | |
5093 | noex); | |
5094 | SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt))); | |
5095 | TREE_SIDE_EFFECTS (body) = 1; | |
5096 | TRANSACTION_EXPR_BODY (stmt) = body; | |
5097 | } | |
5098 | ||
4c0315d0 | 5099 | if (compound_stmt) |
5100 | finish_compound_stmt (compound_stmt); | |
5101 | finish_stmt (); | |
5102 | } | |
5103 | ||
f770bf53 | 5104 | /* Build a __transaction_atomic or __transaction_relaxed expression. If |
5105 | NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as | |
5106 | condition. */ | |
4c0315d0 | 5107 | |
5108 | tree | |
f770bf53 | 5109 | build_transaction_expr (location_t loc, tree expr, int flags, tree noex) |
4c0315d0 | 5110 | { |
5111 | tree ret; | |
f770bf53 | 5112 | if (noex) |
5113 | { | |
5114 | expr = build_must_not_throw_expr (expr, noex); | |
5115 | SET_EXPR_LOCATION (expr, loc); | |
5116 | TREE_SIDE_EFFECTS (expr) = 1; | |
5117 | } | |
4c0315d0 | 5118 | ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr); |
5119 | if (flags & TM_STMT_ATTR_RELAXED) | |
5120 | TRANSACTION_EXPR_RELAXED (ret) = 1; | |
5121 | SET_EXPR_LOCATION (ret, loc); | |
5122 | return ret; | |
5123 | } | |
5124 | \f | |
41fb2c18 | 5125 | void |
807be5b4 | 5126 | init_cp_semantics (void) |
41fb2c18 | 5127 | { |
41fb2c18 | 5128 | } |
7a05c4b1 | 5129 | \f |
5130 | /* Build a STATIC_ASSERT for a static assertion with the condition | |
5131 | CONDITION and the message text MESSAGE. LOCATION is the location | |
5132 | of the static assertion in the source code. When MEMBER_P, this | |
5133 | static assertion is a member of a class. */ | |
5134 | void | |
5135 | finish_static_assert (tree condition, tree message, location_t location, | |
5136 | bool member_p) | |
5137 | { | |
530f9e00 | 5138 | if (message == NULL_TREE |
5139 | || message == error_mark_node | |
5140 | || condition == NULL_TREE | |
5141 | || condition == error_mark_node) | |
5142 | return; | |
5143 | ||
830a6615 | 5144 | if (check_for_bare_parameter_packs (condition)) |
5145 | condition = error_mark_node; | |
5146 | ||
7a05c4b1 | 5147 | if (type_dependent_expression_p (condition) |
5148 | || value_dependent_expression_p (condition)) | |
5149 | { | |
5150 | /* We're in a template; build a STATIC_ASSERT and put it in | |
5151 | the right place. */ | |
5152 | tree assertion; | |
5153 | ||
5154 | assertion = make_node (STATIC_ASSERT); | |
5155 | STATIC_ASSERT_CONDITION (assertion) = condition; | |
5156 | STATIC_ASSERT_MESSAGE (assertion) = message; | |
5157 | STATIC_ASSERT_SOURCE_LOCATION (assertion) = location; | |
5158 | ||
5159 | if (member_p) | |
5160 | maybe_add_class_template_decl_list (current_class_type, | |
5161 | assertion, | |
5162 | /*friend_p=*/0); | |
5163 | else | |
5164 | add_stmt (assertion); | |
5165 | ||
5166 | return; | |
5167 | } | |
5168 | ||
5169 | /* Fold the expression and convert it to a boolean value. */ | |
5170 | condition = fold_non_dependent_expr (condition); | |
c4698a21 | 5171 | condition = cp_convert (boolean_type_node, condition, tf_warning_or_error); |
ce984e5e | 5172 | condition = maybe_constant_value (condition); |
7a05c4b1 | 5173 | |
5174 | if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition)) | |
5175 | /* Do nothing; the condition is satisfied. */ | |
5176 | ; | |
5177 | else | |
5178 | { | |
5179 | location_t saved_loc = input_location; | |
5180 | ||
5181 | input_location = location; | |
5182 | if (TREE_CODE (condition) == INTEGER_CST | |
5183 | && integer_zerop (condition)) | |
5184 | /* Report the error. */ | |
9503eeb0 | 5185 | error ("static assertion failed: %s", TREE_STRING_POINTER (message)); |
7a05c4b1 | 5186 | else if (condition && condition != error_mark_node) |
ce984e5e | 5187 | { |
5188 | error ("non-constant condition for static assertion"); | |
5189 | cxx_constant_value (condition); | |
5190 | } | |
7a05c4b1 | 5191 | input_location = saved_loc; |
5192 | } | |
5193 | } | |
34da8800 | 5194 | \f |
5195 | /* Implements the C++0x decltype keyword. Returns the type of EXPR, | |
5196 | suitable for use as a type-specifier. | |
5197 | ||
5198 | ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an | |
5199 | id-expression or a class member access, FALSE when it was parsed as | |
5200 | a full expression. */ | |
3986b463 | 5201 | |
34da8800 | 5202 | tree |
c2b6be66 | 5203 | finish_decltype_type (tree expr, bool id_expression_or_member_access_p, |
5204 | tsubst_flags_t complain) | |
34da8800 | 5205 | { |
ca3ea3bd | 5206 | tree type = NULL_TREE; |
34da8800 | 5207 | |
a7905afa | 5208 | if (!expr || error_operand_p (expr)) |
5209 | return error_mark_node; | |
5210 | ||
711178fb | 5211 | if (TYPE_P (expr) |
5212 | || TREE_CODE (expr) == TYPE_DECL | |
5213 | || (TREE_CODE (expr) == BIT_NOT_EXPR | |
5214 | && TYPE_P (TREE_OPERAND (expr, 0)))) | |
5215 | { | |
c2b6be66 | 5216 | if (complain & tf_error) |
5217 | error ("argument to decltype must be an expression"); | |
711178fb | 5218 | return error_mark_node; |
5219 | } | |
5220 | ||
288dc77b | 5221 | /* Depending on the resolution of DR 1172, we may later need to distinguish |
5222 | instantiation-dependent but not type-dependent expressions so that, say, | |
5223 | A<decltype(sizeof(T))>::U doesn't require 'typename'. */ | |
5224 | if (instantiation_dependent_expression_p (expr)) | |
34da8800 | 5225 | { |
95397ff9 | 5226 | type = cxx_make_type (DECLTYPE_TYPE); |
34da8800 | 5227 | DECLTYPE_TYPE_EXPR (type) = expr; |
5228 | DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type) | |
5229 | = id_expression_or_member_access_p; | |
5230 | SET_TYPE_STRUCTURAL_EQUALITY (type); | |
5231 | ||
5232 | return type; | |
5233 | } | |
5234 | ||
5235 | /* The type denoted by decltype(e) is defined as follows: */ | |
5236 | ||
eebd67e7 | 5237 | expr = resolve_nondeduced_context (expr); |
8b25863e | 5238 | |
07bbe61e | 5239 | if (type_unknown_p (expr)) |
5240 | { | |
5241 | if (complain & tf_error) | |
5242 | error ("decltype cannot resolve address of overloaded function"); | |
5243 | return error_mark_node; | |
5244 | } | |
5245 | ||
ad668201 | 5246 | if (invalid_nonstatic_memfn_p (expr, complain)) |
5247 | return error_mark_node; | |
5248 | ||
8b25863e | 5249 | /* To get the size of a static data member declared as an array of |
5250 | unknown bound, we need to instantiate it. */ | |
5251 | if (TREE_CODE (expr) == VAR_DECL | |
5252 | && VAR_HAD_UNKNOWN_BOUND (expr) | |
5253 | && DECL_TEMPLATE_INSTANTIATION (expr)) | |
5254 | instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false); | |
5255 | ||
34da8800 | 5256 | if (id_expression_or_member_access_p) |
5257 | { | |
5258 | /* If e is an id-expression or a class member access (5.2.5 | |
5259 | [expr.ref]), decltype(e) is defined as the type of the entity | |
5260 | named by e. If there is no such entity, or e names a set of | |
5261 | overloaded functions, the program is ill-formed. */ | |
5262 | if (TREE_CODE (expr) == IDENTIFIER_NODE) | |
5263 | expr = lookup_name (expr); | |
5264 | ||
5265 | if (TREE_CODE (expr) == INDIRECT_REF) | |
5266 | /* This can happen when the expression is, e.g., "a.b". Just | |
5267 | look at the underlying operand. */ | |
5268 | expr = TREE_OPERAND (expr, 0); | |
5269 | ||
5270 | if (TREE_CODE (expr) == OFFSET_REF | |
0943ab30 | 5271 | || TREE_CODE (expr) == MEMBER_REF |
5272 | || TREE_CODE (expr) == SCOPE_REF) | |
34da8800 | 5273 | /* We're only interested in the field itself. If it is a |
5274 | BASELINK, we will need to see through it in the next | |
5275 | step. */ | |
5276 | expr = TREE_OPERAND (expr, 1); | |
5277 | ||
a00f651a | 5278 | if (BASELINK_P (expr)) |
07bbe61e | 5279 | /* See through BASELINK nodes to the underlying function. */ |
34da8800 | 5280 | expr = BASELINK_FUNCTIONS (expr); |
5281 | ||
34da8800 | 5282 | switch (TREE_CODE (expr)) |
5283 | { | |
5284 | case FIELD_DECL: | |
557902f7 | 5285 | if (DECL_BIT_FIELD_TYPE (expr)) |
34da8800 | 5286 | { |
5287 | type = DECL_BIT_FIELD_TYPE (expr); | |
5288 | break; | |
5289 | } | |
5290 | /* Fall through for fields that aren't bitfields. */ | |
5291 | ||
5292 | case FUNCTION_DECL: | |
5293 | case VAR_DECL: | |
5294 | case CONST_DECL: | |
5295 | case PARM_DECL: | |
5296 | case RESULT_DECL: | |
6c2d37e1 | 5297 | case TEMPLATE_PARM_INDEX: |
fbb73d9b | 5298 | expr = mark_type_use (expr); |
34da8800 | 5299 | type = TREE_TYPE (expr); |
5300 | break; | |
5301 | ||
5302 | case ERROR_MARK: | |
5303 | type = error_mark_node; | |
5304 | break; | |
5305 | ||
5306 | case COMPONENT_REF: | |
fbb73d9b | 5307 | mark_type_use (expr); |
34da8800 | 5308 | type = is_bitfield_expr_with_lowered_type (expr); |
5309 | if (!type) | |
5310 | type = TREE_TYPE (TREE_OPERAND (expr, 1)); | |
5311 | break; | |
5312 | ||
5313 | case BIT_FIELD_REF: | |
5314 | gcc_unreachable (); | |
5315 | ||
5316 | case INTEGER_CST: | |
44a7454c | 5317 | case PTRMEM_CST: |
34da8800 | 5318 | /* We can get here when the id-expression refers to an |
44a7454c | 5319 | enumerator or non-type template parameter. */ |
34da8800 | 5320 | type = TREE_TYPE (expr); |
5321 | break; | |
5322 | ||
5323 | default: | |
07bbe61e | 5324 | gcc_unreachable (); |
34da8800 | 5325 | return error_mark_node; |
5326 | } | |
5327 | } | |
5328 | else | |
5329 | { | |
dbeb1e25 | 5330 | /* Within a lambda-expression: |
5331 | ||
5332 | Every occurrence of decltype((x)) where x is a possibly | |
5333 | parenthesized id-expression that names an entity of | |
5334 | automatic storage duration is treated as if x were | |
5335 | transformed into an access to a corresponding data member | |
5336 | of the closure type that would have been declared if x | |
5337 | were a use of the denoted entity. */ | |
5338 | if (outer_automatic_var_p (expr) | |
5339 | && current_function_decl | |
5340 | && LAMBDA_FUNCTION_P (current_function_decl)) | |
5341 | type = capture_decltype (expr); | |
5342 | else if (error_operand_p (expr)) | |
5343 | type = error_mark_node; | |
5344 | else if (expr == current_class_ptr) | |
5345 | /* If the expression is just "this", we want the | |
5346 | cv-unqualified pointer for the "this" type. */ | |
5347 | type = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); | |
5348 | else | |
5349 | { | |
5350 | /* Otherwise, where T is the type of e, if e is an lvalue, | |
5351 | decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */ | |
5352 | cp_lvalue_kind clk = lvalue_kind (expr); | |
5353 | type = unlowered_expr_type (expr); | |
5354 | gcc_assert (TREE_CODE (type) != REFERENCE_TYPE); | |
e779f859 | 5355 | |
5356 | /* For vector types, pick a non-opaque variant. */ | |
5357 | if (TREE_CODE (type) == VECTOR_TYPE) | |
5358 | type = strip_typedefs (type); | |
5359 | ||
dbeb1e25 | 5360 | if (clk != clk_none && !(clk & clk_class)) |
5361 | type = cp_build_reference_type (type, (clk & clk_rvalueref)); | |
5362 | } | |
34da8800 | 5363 | } |
5364 | ||
34da8800 | 5365 | return type; |
5366 | } | |
9b57b06b | 5367 | |
0aa5d886 | 5368 | /* Called from trait_expr_value to evaluate either __has_nothrow_assign or |
5369 | __has_nothrow_copy, depending on assign_p. */ | |
481451eb | 5370 | |
5371 | static bool | |
0aa5d886 | 5372 | classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p) |
481451eb | 5373 | { |
0aa5d886 | 5374 | tree fns; |
481451eb | 5375 | |
0aa5d886 | 5376 | if (assign_p) |
5377 | { | |
5378 | int ix; | |
5379 | ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR)); | |
5380 | if (ix < 0) | |
481451eb | 5381 | return false; |
f1f41a6c | 5382 | fns = (*CLASSTYPE_METHOD_VEC (type))[ix]; |
0aa5d886 | 5383 | } |
ab8002de | 5384 | else if (TYPE_HAS_COPY_CTOR (type)) |
0aa5d886 | 5385 | { |
5386 | /* If construction of the copy constructor was postponed, create | |
5387 | it now. */ | |
5388 | if (CLASSTYPE_LAZY_COPY_CTOR (type)) | |
5389 | lazily_declare_fn (sfk_copy_constructor, type); | |
a8b75081 | 5390 | if (CLASSTYPE_LAZY_MOVE_CTOR (type)) |
5391 | lazily_declare_fn (sfk_move_constructor, type); | |
0aa5d886 | 5392 | fns = CLASSTYPE_CONSTRUCTORS (type); |
481451eb | 5393 | } |
5394 | else | |
5395 | return false; | |
5396 | ||
0aa5d886 | 5397 | for (; fns; fns = OVL_NEXT (fns)) |
e88a1fbf | 5398 | { |
5399 | tree fn = OVL_CURRENT (fns); | |
5400 | ||
5401 | if (assign_p) | |
5402 | { | |
5403 | if (copy_fn_p (fn) == 0) | |
5404 | continue; | |
5405 | } | |
5406 | else if (copy_fn_p (fn) <= 0) | |
5407 | continue; | |
5408 | ||
5409 | if (!TYPE_NOTHROW_P (TREE_TYPE (fn))) | |
5410 | return false; | |
5411 | } | |
0aa5d886 | 5412 | |
481451eb | 5413 | return true; |
5414 | } | |
5415 | ||
5416 | /* Actually evaluates the trait. */ | |
5417 | ||
5418 | static bool | |
5419 | trait_expr_value (cp_trait_kind kind, tree type1, tree type2) | |
5420 | { | |
5421 | enum tree_code type_code1; | |
5422 | tree t; | |
5423 | ||
5424 | type_code1 = TREE_CODE (type1); | |
5425 | ||
5426 | switch (kind) | |
5427 | { | |
5428 | case CPTK_HAS_NOTHROW_ASSIGN: | |
c1c67b4f | 5429 | type1 = strip_array_types (type1); |
0aa5d886 | 5430 | return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE |
5431 | && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2) | |
5432 | || (CLASS_TYPE_P (type1) | |
5433 | && classtype_has_nothrow_assign_or_copy_p (type1, | |
5434 | true)))); | |
481451eb | 5435 | |
5436 | case CPTK_HAS_TRIVIAL_ASSIGN: | |
c1c67b4f | 5437 | /* ??? The standard seems to be missing the "or array of such a class |
5438 | type" wording for this trait. */ | |
5439 | type1 = strip_array_types (type1); | |
481451eb | 5440 | return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE |
c1c67b4f | 5441 | && (trivial_type_p (type1) |
481451eb | 5442 | || (CLASS_TYPE_P (type1) |
ab8002de | 5443 | && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1)))); |
481451eb | 5444 | |
5445 | case CPTK_HAS_NOTHROW_CONSTRUCTOR: | |
5446 | type1 = strip_array_types (type1); | |
5447 | return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2) | |
5448 | || (CLASS_TYPE_P (type1) | |
2ee92e27 | 5449 | && (t = locate_ctor (type1)) |
151c036b | 5450 | && TYPE_NOTHROW_P (TREE_TYPE (t)))); |
481451eb | 5451 | |
5452 | case CPTK_HAS_TRIVIAL_CONSTRUCTOR: | |
5453 | type1 = strip_array_types (type1); | |
c1c67b4f | 5454 | return (trivial_type_p (type1) |
481451eb | 5455 | || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1))); |
5456 | ||
5457 | case CPTK_HAS_NOTHROW_COPY: | |
c1c67b4f | 5458 | type1 = strip_array_types (type1); |
481451eb | 5459 | return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2) |
5460 | || (CLASS_TYPE_P (type1) | |
0aa5d886 | 5461 | && classtype_has_nothrow_assign_or_copy_p (type1, false))); |
481451eb | 5462 | |
5463 | case CPTK_HAS_TRIVIAL_COPY: | |
c1c67b4f | 5464 | /* ??? The standard seems to be missing the "or array of such a class |
5465 | type" wording for this trait. */ | |
5466 | type1 = strip_array_types (type1); | |
5467 | return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE | |
ab8002de | 5468 | || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1))); |
481451eb | 5469 | |
5470 | case CPTK_HAS_TRIVIAL_DESTRUCTOR: | |
5471 | type1 = strip_array_types (type1); | |
c1c67b4f | 5472 | return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE |
481451eb | 5473 | || (CLASS_TYPE_P (type1) |
5474 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1))); | |
5475 | ||
5476 | case CPTK_HAS_VIRTUAL_DESTRUCTOR: | |
0f16033e | 5477 | return type_has_virtual_destructor (type1); |
481451eb | 5478 | |
5479 | case CPTK_IS_ABSTRACT: | |
793fb8f3 | 5480 | return (ABSTRACT_CLASS_TYPE_P (type1)); |
481451eb | 5481 | |
5482 | case CPTK_IS_BASE_OF: | |
5483 | return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2) | |
5484 | && DERIVED_FROM_P (type1, type2)); | |
5485 | ||
5486 | case CPTK_IS_CLASS: | |
5487 | return (NON_UNION_CLASS_TYPE_P (type1)); | |
5488 | ||
5489 | case CPTK_IS_CONVERTIBLE_TO: | |
5490 | /* TODO */ | |
5491 | return false; | |
5492 | ||
5493 | case CPTK_IS_EMPTY: | |
5494 | return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1)); | |
5495 | ||
5496 | case CPTK_IS_ENUM: | |
5497 | return (type_code1 == ENUMERAL_TYPE); | |
5498 | ||
aa4313eb | 5499 | case CPTK_IS_FINAL: |
5500 | return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1)); | |
5501 | ||
23407dc9 | 5502 | case CPTK_IS_LITERAL_TYPE: |
5503 | return (literal_type_p (type1)); | |
5504 | ||
481451eb | 5505 | case CPTK_IS_POD: |
5506 | return (pod_type_p (type1)); | |
5507 | ||
5508 | case CPTK_IS_POLYMORPHIC: | |
5509 | return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1)); | |
5510 | ||
c1c67b4f | 5511 | case CPTK_IS_STD_LAYOUT: |
5512 | return (std_layout_type_p (type1)); | |
5513 | ||
5514 | case CPTK_IS_TRIVIAL: | |
5515 | return (trivial_type_p (type1)); | |
5516 | ||
481451eb | 5517 | case CPTK_IS_UNION: |
5518 | return (type_code1 == UNION_TYPE); | |
5519 | ||
5520 | default: | |
5521 | gcc_unreachable (); | |
5522 | return false; | |
5523 | } | |
5524 | } | |
5525 | ||
8215802e | 5526 | /* If TYPE is an array of unknown bound, or (possibly cv-qualified) |
5527 | void, or a complete type, returns it, otherwise NULL_TREE. */ | |
ad0f709b | 5528 | |
8215802e | 5529 | static tree |
ad0f709b | 5530 | check_trait_type (tree type) |
5531 | { | |
e6bc5891 | 5532 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) |
5533 | && COMPLETE_TYPE_P (TREE_TYPE (type))) | |
8215802e | 5534 | return type; |
ad0f709b | 5535 | |
5536 | if (VOID_TYPE_P (type)) | |
8215802e | 5537 | return type; |
ad0f709b | 5538 | |
8215802e | 5539 | return complete_type_or_else (strip_array_types (type), NULL_TREE); |
ad0f709b | 5540 | } |
5541 | ||
481451eb | 5542 | /* Process a trait expression. */ |
5543 | ||
5544 | tree | |
5545 | finish_trait_expr (cp_trait_kind kind, tree type1, tree type2) | |
5546 | { | |
5547 | gcc_assert (kind == CPTK_HAS_NOTHROW_ASSIGN | |
5548 | || kind == CPTK_HAS_NOTHROW_CONSTRUCTOR | |
5549 | || kind == CPTK_HAS_NOTHROW_COPY | |
5550 | || kind == CPTK_HAS_TRIVIAL_ASSIGN | |
5551 | || kind == CPTK_HAS_TRIVIAL_CONSTRUCTOR | |
5552 | || kind == CPTK_HAS_TRIVIAL_COPY | |
5553 | || kind == CPTK_HAS_TRIVIAL_DESTRUCTOR | |
5554 | || kind == CPTK_HAS_VIRTUAL_DESTRUCTOR | |
5555 | || kind == CPTK_IS_ABSTRACT | |
5556 | || kind == CPTK_IS_BASE_OF | |
5557 | || kind == CPTK_IS_CLASS | |
5558 | || kind == CPTK_IS_CONVERTIBLE_TO | |
5559 | || kind == CPTK_IS_EMPTY | |
5560 | || kind == CPTK_IS_ENUM | |
aa4313eb | 5561 | || kind == CPTK_IS_FINAL |
23407dc9 | 5562 | || kind == CPTK_IS_LITERAL_TYPE |
481451eb | 5563 | || kind == CPTK_IS_POD |
5564 | || kind == CPTK_IS_POLYMORPHIC | |
c1c67b4f | 5565 | || kind == CPTK_IS_STD_LAYOUT |
5566 | || kind == CPTK_IS_TRIVIAL | |
481451eb | 5567 | || kind == CPTK_IS_UNION); |
5568 | ||
5569 | if (kind == CPTK_IS_CONVERTIBLE_TO) | |
5570 | { | |
5571 | sorry ("__is_convertible_to"); | |
5572 | return error_mark_node; | |
5573 | } | |
5574 | ||
5575 | if (type1 == error_mark_node | |
5576 | || ((kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO) | |
5577 | && type2 == error_mark_node)) | |
5578 | return error_mark_node; | |
5579 | ||
5580 | if (processing_template_decl) | |
5581 | { | |
5582 | tree trait_expr = make_node (TRAIT_EXPR); | |
5583 | TREE_TYPE (trait_expr) = boolean_type_node; | |
5584 | TRAIT_EXPR_TYPE1 (trait_expr) = type1; | |
5585 | TRAIT_EXPR_TYPE2 (trait_expr) = type2; | |
5586 | TRAIT_EXPR_KIND (trait_expr) = kind; | |
5587 | return trait_expr; | |
5588 | } | |
5589 | ||
ad0f709b | 5590 | switch (kind) |
481451eb | 5591 | { |
ad0f709b | 5592 | case CPTK_HAS_NOTHROW_ASSIGN: |
5593 | case CPTK_HAS_TRIVIAL_ASSIGN: | |
5594 | case CPTK_HAS_NOTHROW_CONSTRUCTOR: | |
5595 | case CPTK_HAS_TRIVIAL_CONSTRUCTOR: | |
5596 | case CPTK_HAS_NOTHROW_COPY: | |
5597 | case CPTK_HAS_TRIVIAL_COPY: | |
5598 | case CPTK_HAS_TRIVIAL_DESTRUCTOR: | |
5599 | case CPTK_HAS_VIRTUAL_DESTRUCTOR: | |
5600 | case CPTK_IS_ABSTRACT: | |
5601 | case CPTK_IS_EMPTY: | |
aa4313eb | 5602 | case CPTK_IS_FINAL: |
23407dc9 | 5603 | case CPTK_IS_LITERAL_TYPE: |
ad0f709b | 5604 | case CPTK_IS_POD: |
5605 | case CPTK_IS_POLYMORPHIC: | |
c1c67b4f | 5606 | case CPTK_IS_STD_LAYOUT: |
5607 | case CPTK_IS_TRIVIAL: | |
ad0f709b | 5608 | if (!check_trait_type (type1)) |
8215802e | 5609 | return error_mark_node; |
ad0f709b | 5610 | break; |
5611 | ||
5612 | case CPTK_IS_BASE_OF: | |
5613 | if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2) | |
5614 | && !same_type_ignoring_top_level_qualifiers_p (type1, type2) | |
8215802e | 5615 | && !complete_type_or_else (type2, NULL_TREE)) |
5616 | /* We already issued an error. */ | |
5617 | return error_mark_node; | |
ad0f709b | 5618 | break; |
5619 | ||
5620 | case CPTK_IS_CLASS: | |
5621 | case CPTK_IS_ENUM: | |
5622 | case CPTK_IS_UNION: | |
5623 | break; | |
5624 | ||
5625 | case CPTK_IS_CONVERTIBLE_TO: | |
5626 | default: | |
5627 | gcc_unreachable (); | |
481451eb | 5628 | } |
5629 | ||
5630 | return (trait_expr_value (kind, type1, type2) | |
5631 | ? boolean_true_node : boolean_false_node); | |
5632 | } | |
5633 | ||
3ae3a17f | 5634 | /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64, |
5635 | which is ignored for C++. */ | |
5636 | ||
5637 | void | |
5638 | set_float_const_decimal64 (void) | |
5639 | { | |
5640 | } | |
5641 | ||
5642 | void | |
5643 | clear_float_const_decimal64 (void) | |
5644 | { | |
5645 | } | |
5646 | ||
5647 | bool | |
5648 | float_const_decimal64_p (void) | |
5649 | { | |
5650 | return 0; | |
5651 | } | |
5652 | ||
c99de541 | 5653 | \f |
17814aca | 5654 | /* Return true if T is a literal type. */ |
5655 | ||
5656 | bool | |
5657 | literal_type_p (tree t) | |
5658 | { | |
e1b036be | 5659 | if (SCALAR_TYPE_P (t) |
bf0cb017 | 5660 | || TREE_CODE (t) == VECTOR_TYPE |
e1b036be | 5661 | || TREE_CODE (t) == REFERENCE_TYPE) |
17814aca | 5662 | return true; |
5663 | if (CLASS_TYPE_P (t)) | |
1330e439 | 5664 | { |
bf8126b6 | 5665 | t = complete_type (t); |
5666 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); | |
5667 | return CLASSTYPE_LITERAL_P (t); | |
1330e439 | 5668 | } |
17814aca | 5669 | if (TREE_CODE (t) == ARRAY_TYPE) |
5670 | return literal_type_p (strip_array_types (t)); | |
5671 | return false; | |
5672 | } | |
5673 | ||
17814aca | 5674 | /* If DECL is a variable declared `constexpr', require its type |
5675 | be literal. Return the DECL if OK, otherwise NULL. */ | |
5676 | ||
5677 | tree | |
5678 | ensure_literal_type_for_constexpr_object (tree decl) | |
5679 | { | |
5680 | tree type = TREE_TYPE (decl); | |
5681 | if (TREE_CODE (decl) == VAR_DECL && DECL_DECLARED_CONSTEXPR_P (decl) | |
1330e439 | 5682 | && !processing_template_decl) |
17814aca | 5683 | { |
bf8126b6 | 5684 | if (CLASS_TYPE_P (type) && !COMPLETE_TYPE_P (complete_type (type))) |
1330e439 | 5685 | /* Don't complain here, we'll complain about incompleteness |
5686 | when we try to initialize the variable. */; | |
5687 | else if (!literal_type_p (type)) | |
5688 | { | |
5689 | error ("the type %qT of constexpr variable %qD is not literal", | |
5690 | type, decl); | |
262c8920 | 5691 | explain_non_literal_class (type); |
1330e439 | 5692 | return NULL; |
5693 | } | |
17814aca | 5694 | } |
5695 | return decl; | |
5696 | } | |
5697 | ||
4905dfb6 | 5698 | /* Representation of entries in the constexpr function definition table. */ |
5699 | ||
5700 | typedef struct GTY(()) constexpr_fundef { | |
5701 | tree decl; | |
5702 | tree body; | |
5703 | } constexpr_fundef; | |
5704 | ||
5705 | /* This table holds all constexpr function definitions seen in | |
5706 | the current translation unit. */ | |
5707 | ||
5708 | static GTY ((param_is (constexpr_fundef))) htab_t constexpr_fundef_table; | |
5709 | ||
4905dfb6 | 5710 | /* Utility function used for managing the constexpr function table. |
5711 | Return true if the entries pointed to by P and Q are for the | |
5712 | same constexpr function. */ | |
5713 | ||
5714 | static inline int | |
5715 | constexpr_fundef_equal (const void *p, const void *q) | |
5716 | { | |
5717 | const constexpr_fundef *lhs = (const constexpr_fundef *) p; | |
5718 | const constexpr_fundef *rhs = (const constexpr_fundef *) q; | |
5719 | return lhs->decl == rhs->decl; | |
5720 | } | |
5721 | ||
5722 | /* Utility function used for managing the constexpr function table. | |
5723 | Return a hash value for the entry pointed to by Q. */ | |
5724 | ||
5725 | static inline hashval_t | |
5726 | constexpr_fundef_hash (const void *p) | |
5727 | { | |
5728 | const constexpr_fundef *fundef = (const constexpr_fundef *) p; | |
5729 | return DECL_UID (fundef->decl); | |
5730 | } | |
5731 | ||
5732 | /* Return a previously saved definition of function FUN. */ | |
5733 | ||
5734 | static constexpr_fundef * | |
5735 | retrieve_constexpr_fundef (tree fun) | |
5736 | { | |
5737 | constexpr_fundef fundef = { NULL, NULL }; | |
5738 | if (constexpr_fundef_table == NULL) | |
5739 | return NULL; | |
5740 | ||
5741 | fundef.decl = fun; | |
5742 | return (constexpr_fundef *) htab_find (constexpr_fundef_table, &fundef); | |
5743 | } | |
5744 | ||
ca63c29a | 5745 | /* Check whether the parameter and return types of FUN are valid for a |
bf8126b6 | 5746 | constexpr function, and complain if COMPLAIN. */ |
ca63c29a | 5747 | |
bf8126b6 | 5748 | static bool |
5749 | is_valid_constexpr_fn (tree fun, bool complain) | |
ca63c29a | 5750 | { |
5751 | tree parm = FUNCTION_FIRST_USER_PARM (fun); | |
5752 | bool ret = true; | |
5753 | for (; parm != NULL; parm = TREE_CHAIN (parm)) | |
bf8126b6 | 5754 | if (!literal_type_p (TREE_TYPE (parm))) |
ca63c29a | 5755 | { |
5756 | ret = false; | |
5757 | if (complain) | |
262c8920 | 5758 | { |
5759 | error ("invalid type for parameter %d of constexpr " | |
5760 | "function %q+#D", DECL_PARM_INDEX (parm), fun); | |
5761 | explain_non_literal_class (TREE_TYPE (parm)); | |
5762 | } | |
ca63c29a | 5763 | } |
5764 | ||
5765 | if (!DECL_CONSTRUCTOR_P (fun)) | |
5766 | { | |
5767 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
bf8126b6 | 5768 | if (!literal_type_p (rettype)) |
ca63c29a | 5769 | { |
5770 | ret = false; | |
5771 | if (complain) | |
262c8920 | 5772 | { |
5773 | error ("invalid return type %qT of constexpr function %q+D", | |
5774 | rettype, fun); | |
5775 | explain_non_literal_class (rettype); | |
5776 | } | |
ca63c29a | 5777 | } |
5778 | ||
bf8126b6 | 5779 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) |
5780 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) | |
ca63c29a | 5781 | { |
bf8126b6 | 5782 | ret = false; |
5783 | if (complain) | |
262c8920 | 5784 | { |
5785 | error ("enclosing class of constexpr non-static member " | |
5786 | "function %q+#D is not a literal type", fun); | |
5787 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
5788 | } | |
ca63c29a | 5789 | } |
5790 | } | |
a76cd2d2 | 5791 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) |
5792 | { | |
5793 | ret = false; | |
5794 | if (complain) | |
5795 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
5796 | } | |
ca63c29a | 5797 | |
5798 | return ret; | |
5799 | } | |
5800 | ||
4905dfb6 | 5801 | /* Subroutine of build_constexpr_constructor_member_initializers. |
5802 | The expression tree T represents a data member initialization | |
5803 | in a (constexpr) constructor definition. Build a pairing of | |
5804 | the data member with its initializer, and prepend that pair | |
5805 | to the existing initialization pair INITS. */ | |
5806 | ||
5807 | static bool | |
f1f41a6c | 5808 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) |
4905dfb6 | 5809 | { |
5810 | tree member, init; | |
5811 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
5812 | t = TREE_OPERAND (t, 0); | |
5813 | if (TREE_CODE (t) == EXPR_STMT) | |
5814 | t = TREE_OPERAND (t, 0); | |
5815 | if (t == error_mark_node) | |
5816 | return false; | |
e39e78dc | 5817 | if (TREE_CODE (t) == STATEMENT_LIST) |
5818 | { | |
5819 | tree_stmt_iterator i; | |
5820 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
5821 | { | |
5822 | if (! build_data_member_initialization (tsi_stmt (i), vec)) | |
5823 | return false; | |
5824 | } | |
5825 | return true; | |
5826 | } | |
4905dfb6 | 5827 | if (TREE_CODE (t) == CLEANUP_STMT) |
60c0253f | 5828 | { |
5829 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
5830 | but we can in a constexpr constructor for a non-literal class. Just | |
5831 | ignore it; either all the initialization will be constant, in which | |
5832 | case the cleanup can't run, or it can't be constexpr. | |
5833 | Still recurse into CLEANUP_BODY. */ | |
e39e78dc | 5834 | return build_data_member_initialization (CLEANUP_BODY (t), vec); |
60c0253f | 5835 | } |
4905dfb6 | 5836 | if (TREE_CODE (t) == CONVERT_EXPR) |
5837 | t = TREE_OPERAND (t, 0); | |
5838 | if (TREE_CODE (t) == INIT_EXPR | |
5839 | || TREE_CODE (t) == MODIFY_EXPR) | |
5840 | { | |
5841 | member = TREE_OPERAND (t, 0); | |
5842 | init = unshare_expr (TREE_OPERAND (t, 1)); | |
5843 | } | |
5844 | else | |
5845 | { | |
4905dfb6 | 5846 | gcc_assert (TREE_CODE (t) == CALL_EXPR); |
5847 | member = CALL_EXPR_ARG (t, 0); | |
e77a1599 | 5848 | /* We don't use build_cplus_new here because it complains about |
5849 | abstract bases. Leaving the call unwrapped means that it has the | |
5850 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
5851 | init = unshare_expr (t); | |
5852 | } | |
5853 | if (TREE_CODE (member) == INDIRECT_REF) | |
5854 | member = TREE_OPERAND (member, 0); | |
5855 | if (TREE_CODE (member) == NOP_EXPR) | |
5856 | { | |
5857 | tree op = member; | |
5858 | STRIP_NOPS (op); | |
5859 | if (TREE_CODE (op) == ADDR_EXPR) | |
4905dfb6 | 5860 | { |
e77a1599 | 5861 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
5862 | (TREE_TYPE (TREE_TYPE (op)), | |
5863 | TREE_TYPE (TREE_TYPE (member)))); | |
5864 | /* Initializing a cv-qualified member; we need to look through | |
5865 | the const_cast. */ | |
5866 | member = op; | |
4905dfb6 | 5867 | } |
211276ee | 5868 | else if (op == current_class_ptr |
5869 | && (same_type_ignoring_top_level_qualifiers_p | |
5870 | (TREE_TYPE (TREE_TYPE (member)), | |
5871 | current_class_type))) | |
5872 | /* Delegating constructor. */ | |
5873 | member = op; | |
4905dfb6 | 5874 | else |
5875 | { | |
2e6c5d72 | 5876 | /* This is an initializer for an empty base; keep it for now so |
5877 | we can check it in cxx_eval_bare_aggregate. */ | |
e77a1599 | 5878 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); |
4905dfb6 | 5879 | } |
4905dfb6 | 5880 | } |
e77a1599 | 5881 | if (TREE_CODE (member) == ADDR_EXPR) |
5882 | member = TREE_OPERAND (member, 0); | |
05d560e7 | 5883 | if (TREE_CODE (member) == COMPONENT_REF |
5884 | /* If we're initializing a member of a subaggregate, it's a vtable | |
5885 | pointer. Leave it as COMPONENT_REF so we remember the path to get | |
5886 | to the vfield. */ | |
5887 | && TREE_CODE (TREE_OPERAND (member, 0)) != COMPONENT_REF) | |
4905dfb6 | 5888 | member = TREE_OPERAND (member, 1); |
5889 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
5890 | return true; | |
5891 | } | |
5892 | ||
fdf548d1 | 5893 | /* Make sure that there are no statements after LAST in the constructor |
5894 | body represented by LIST. */ | |
5895 | ||
5896 | bool | |
5897 | check_constexpr_ctor_body (tree last, tree list) | |
5898 | { | |
5899 | bool ok = true; | |
5900 | if (TREE_CODE (list) == STATEMENT_LIST) | |
5901 | { | |
5902 | tree_stmt_iterator i = tsi_last (list); | |
5903 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
5904 | { | |
5905 | tree t = tsi_stmt (i); | |
5906 | if (t == last) | |
5907 | break; | |
5908 | if (TREE_CODE (t) == BIND_EXPR) | |
5909 | { | |
5910 | if (!check_constexpr_ctor_body (last, BIND_EXPR_BODY (t))) | |
5911 | return false; | |
5912 | else | |
5913 | continue; | |
5914 | } | |
5915 | /* We currently allow typedefs and static_assert. | |
5916 | FIXME allow them in the standard, too. */ | |
5917 | if (TREE_CODE (t) != STATIC_ASSERT) | |
5918 | { | |
5919 | ok = false; | |
5920 | break; | |
5921 | } | |
5922 | } | |
5923 | } | |
5924 | else if (list != last | |
5925 | && TREE_CODE (list) != STATIC_ASSERT) | |
5926 | ok = false; | |
5927 | if (!ok) | |
5928 | { | |
5929 | error ("constexpr constructor does not have empty body"); | |
5930 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; | |
5931 | } | |
5932 | return ok; | |
5933 | } | |
5934 | ||
f1f41a6c | 5935 | /* V is a vector of constructor elements built up for the base and member |
17d06bda | 5936 | initializers of a constructor for TYPE. They need to be in increasing |
5937 | offset order, which they might not be yet if TYPE has a primary base | |
5938 | which is not first in the base-clause. */ | |
5939 | ||
f1f41a6c | 5940 | static vec<constructor_elt, va_gc> * |
5941 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
17d06bda | 5942 | { |
952fcfec | 5943 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); |
5944 | constructor_elt elt; | |
5945 | int i; | |
5946 | ||
5947 | if (pri == NULL_TREE | |
5948 | || pri == BINFO_BASE_BINFO (TYPE_BINFO (type), 0)) | |
f1f41a6c | 5949 | return v; |
17d06bda | 5950 | |
5951 | /* Find the element for the primary base and move it to the beginning of | |
5952 | the vec. */ | |
f1f41a6c | 5953 | vec<constructor_elt, va_gc> &vref = *v; |
952fcfec | 5954 | pri = BINFO_TYPE (pri); |
5955 | for (i = 1; ; ++i) | |
f1f41a6c | 5956 | if (TREE_TYPE (vref[i].index) == pri) |
17d06bda | 5957 | break; |
952fcfec | 5958 | |
f1f41a6c | 5959 | elt = vref[i]; |
952fcfec | 5960 | for (; i > 0; --i) |
f1f41a6c | 5961 | vref[i] = vref[i-1]; |
5962 | vref[0] = elt; | |
5963 | return v; | |
17d06bda | 5964 | } |
5965 | ||
4905dfb6 | 5966 | /* Build compile-time evalable representations of member-initializer list |
5967 | for a constexpr constructor. */ | |
5968 | ||
5969 | static tree | |
5970 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
5971 | { | |
f1f41a6c | 5972 | vec<constructor_elt, va_gc> *vec = NULL; |
4905dfb6 | 5973 | bool ok = true; |
5974 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR | |
5975 | || TREE_CODE (body) == EH_SPEC_BLOCK) | |
5976 | body = TREE_OPERAND (body, 0); | |
238baf6b | 5977 | if (TREE_CODE (body) == STATEMENT_LIST) |
5978 | body = STATEMENT_LIST_HEAD (body)->stmt; | |
5979 | body = BIND_EXPR_BODY (body); | |
4905dfb6 | 5980 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
28393b8e | 5981 | { |
5982 | body = TREE_OPERAND (body, 0); | |
5983 | if (TREE_CODE (body) == EXPR_STMT) | |
5984 | body = TREE_OPERAND (body, 0); | |
5985 | if (TREE_CODE (body) == INIT_EXPR | |
5986 | && (same_type_ignoring_top_level_qualifiers_p | |
5987 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
5988 | current_class_type))) | |
5989 | { | |
5990 | /* Trivial copy. */ | |
5991 | return TREE_OPERAND (body, 1); | |
5992 | } | |
5993 | ok = build_data_member_initialization (body, &vec); | |
5994 | } | |
74ffc957 | 5995 | else if (TREE_CODE (body) == STATEMENT_LIST) |
4905dfb6 | 5996 | { |
5997 | tree_stmt_iterator i; | |
4905dfb6 | 5998 | for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) |
5999 | { | |
6000 | ok = build_data_member_initialization (tsi_stmt (i), &vec); | |
6001 | if (!ok) | |
6002 | break; | |
6003 | } | |
6004 | } | |
a2748d43 | 6005 | else if (TREE_CODE (body) == TRY_BLOCK) |
6006 | { | |
6007 | error ("body of %<constexpr%> constructor cannot be " | |
6008 | "a function-try-block"); | |
6009 | return error_mark_node; | |
6010 | } | |
5770f123 | 6011 | else if (EXPR_P (body)) |
6012 | ok = build_data_member_initialization (body, &vec); | |
74ffc957 | 6013 | else |
6014 | gcc_assert (errorcount > 0); | |
4905dfb6 | 6015 | if (ok) |
211276ee | 6016 | { |
f1f41a6c | 6017 | if (vec_safe_length (vec) > 0) |
211276ee | 6018 | { |
6019 | /* In a delegating constructor, return the target. */ | |
f1f41a6c | 6020 | constructor_elt *ce = &(*vec)[0]; |
211276ee | 6021 | if (ce->index == current_class_ptr) |
6022 | { | |
6023 | body = ce->value; | |
f1f41a6c | 6024 | vec_free (vec); |
211276ee | 6025 | return body; |
6026 | } | |
6027 | } | |
17d06bda | 6028 | vec = sort_constexpr_mem_initializers (type, vec); |
211276ee | 6029 | return build_constructor (type, vec); |
6030 | } | |
4905dfb6 | 6031 | else |
6032 | return error_mark_node; | |
6033 | } | |
6034 | ||
b309fd0a | 6035 | /* Subroutine of register_constexpr_fundef. BODY is the body of a function |
6036 | declared to be constexpr, or a sub-statement thereof. Returns the | |
6037 | return value if suitable, error_mark_node for a statement not allowed in | |
6038 | a constexpr function, or NULL_TREE if no return value was found. */ | |
6039 | ||
6040 | static tree | |
6041 | constexpr_fn_retval (tree body) | |
6042 | { | |
6043 | switch (TREE_CODE (body)) | |
6044 | { | |
6045 | case STATEMENT_LIST: | |
6046 | { | |
6047 | tree_stmt_iterator i; | |
6048 | tree expr = NULL_TREE; | |
6049 | for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) | |
6050 | { | |
6051 | tree s = constexpr_fn_retval (tsi_stmt (i)); | |
6052 | if (s == error_mark_node) | |
6053 | return error_mark_node; | |
6054 | else if (s == NULL_TREE) | |
6055 | /* Keep iterating. */; | |
6056 | else if (expr) | |
6057 | /* Multiple return statements. */ | |
6058 | return error_mark_node; | |
6059 | else | |
6060 | expr = s; | |
6061 | } | |
6062 | return expr; | |
6063 | } | |
6064 | ||
6065 | case RETURN_EXPR: | |
6066 | return unshare_expr (TREE_OPERAND (body, 0)); | |
6067 | ||
6068 | case DECL_EXPR: | |
6069 | if (TREE_CODE (DECL_EXPR_DECL (body)) == USING_DECL) | |
6070 | return NULL_TREE; | |
6071 | return error_mark_node; | |
6072 | ||
97092f3f | 6073 | case CLEANUP_POINT_EXPR: |
6074 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
6075 | ||
b309fd0a | 6076 | case USING_STMT: |
6077 | return NULL_TREE; | |
6078 | ||
6079 | default: | |
6080 | return error_mark_node; | |
6081 | } | |
6082 | } | |
6083 | ||
262c8920 | 6084 | /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
6085 | FUN; do the necessary transformations to turn it into a single expression | |
6086 | that we can store in the hash table. */ | |
4905dfb6 | 6087 | |
262c8920 | 6088 | static tree |
6089 | massage_constexpr_body (tree fun, tree body) | |
4905dfb6 | 6090 | { |
4905dfb6 | 6091 | if (DECL_CONSTRUCTOR_P (fun)) |
6092 | body = build_constexpr_constructor_member_initializers | |
6093 | (DECL_CONTEXT (fun), body); | |
6094 | else | |
6095 | { | |
4905dfb6 | 6096 | if (TREE_CODE (body) == EH_SPEC_BLOCK) |
6097 | body = EH_SPEC_STMTS (body); | |
6098 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
6099 | body = TREE_OPERAND (body, 0); | |
8471bd39 | 6100 | if (TREE_CODE (body) == BIND_EXPR) |
6101 | body = BIND_EXPR_BODY (body); | |
b309fd0a | 6102 | body = constexpr_fn_retval (body); |
262c8920 | 6103 | } |
6104 | return body; | |
6105 | } | |
6106 | ||
bbab4abf | 6107 | /* FUN is a constexpr constructor with massaged body BODY. Return true |
6108 | if some bases/fields are uninitialized, and complain if COMPLAIN. */ | |
6109 | ||
6110 | static bool | |
6111 | cx_check_missing_mem_inits (tree fun, tree body, bool complain) | |
6112 | { | |
6113 | bool bad; | |
6114 | tree field; | |
6115 | unsigned i, nelts; | |
2e5f66c6 | 6116 | tree ctype; |
bbab4abf | 6117 | |
6118 | if (TREE_CODE (body) != CONSTRUCTOR) | |
6119 | return false; | |
6120 | ||
bbab4abf | 6121 | nelts = CONSTRUCTOR_NELTS (body); |
2e5f66c6 | 6122 | ctype = DECL_CONTEXT (fun); |
6123 | field = TYPE_FIELDS (ctype); | |
6124 | ||
6125 | if (TREE_CODE (ctype) == UNION_TYPE) | |
6126 | { | |
6127 | if (nelts == 0 && next_initializable_field (field)) | |
6128 | { | |
6129 | if (complain) | |
6130 | error ("%<constexpr%> constructor for union %qT must " | |
6131 | "initialize exactly one non-static data member", ctype); | |
6132 | return true; | |
6133 | } | |
6134 | return false; | |
6135 | } | |
6136 | ||
6137 | bad = false; | |
bbab4abf | 6138 | for (i = 0; i <= nelts; ++i) |
6139 | { | |
6140 | tree index; | |
6141 | if (i == nelts) | |
6142 | index = NULL_TREE; | |
6143 | else | |
6144 | { | |
6145 | index = CONSTRUCTOR_ELT (body, i)->index; | |
190130f1 | 6146 | /* Skip base and vtable inits. */ |
0a378509 | 6147 | if (TREE_CODE (index) != FIELD_DECL |
6148 | || DECL_ARTIFICIAL (index)) | |
bbab4abf | 6149 | continue; |
6150 | } | |
0a378509 | 6151 | for (; field != index; field = DECL_CHAIN (field)) |
bbab4abf | 6152 | { |
190130f1 | 6153 | tree ftype; |
bbab4abf | 6154 | if (TREE_CODE (field) != FIELD_DECL |
17d06bda | 6155 | || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field)) |
6156 | || DECL_ARTIFICIAL (field)) | |
bbab4abf | 6157 | continue; |
190130f1 | 6158 | ftype = strip_array_types (TREE_TYPE (field)); |
6159 | if (type_has_constexpr_default_constructor (ftype)) | |
6160 | { | |
6161 | /* It's OK to skip a member with a trivial constexpr ctor. | |
6162 | A constexpr ctor that isn't trivial should have been | |
6163 | added in by now. */ | |
208d6e3e | 6164 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) |
6165 | || errorcount != 0); | |
190130f1 | 6166 | continue; |
6167 | } | |
2e5f66c6 | 6168 | if (!complain) |
6169 | return true; | |
bbab4abf | 6170 | error ("uninitialized member %qD in %<constexpr%> constructor", |
6171 | field); | |
6172 | bad = true; | |
6173 | } | |
6174 | if (field == NULL_TREE) | |
6175 | break; | |
6176 | field = DECL_CHAIN (field); | |
6177 | } | |
6178 | ||
6179 | return bad; | |
6180 | } | |
6181 | ||
262c8920 | 6182 | /* We are processing the definition of the constexpr function FUN. |
6183 | Check that its BODY fulfills the propriate requirements and | |
6184 | enter it in the constexpr function definition table. | |
6185 | For constructor BODY is actually the TREE_LIST of the | |
6186 | member-initializer list. */ | |
6187 | ||
6188 | tree | |
6189 | register_constexpr_fundef (tree fun, tree body) | |
6190 | { | |
6191 | constexpr_fundef entry; | |
6192 | constexpr_fundef **slot; | |
6193 | ||
d6ef1dfb | 6194 | if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) |
190130f1 | 6195 | return NULL; |
6196 | ||
262c8920 | 6197 | body = massage_constexpr_body (fun, body); |
6198 | if (body == NULL_TREE || body == error_mark_node) | |
6199 | { | |
d9c249a4 | 6200 | if (!DECL_CONSTRUCTOR_P (fun)) |
6201 | error ("body of constexpr function %qD not a return-statement", fun); | |
262c8920 | 6202 | return NULL; |
4905dfb6 | 6203 | } |
6204 | ||
7c51b530 | 6205 | if (!potential_rvalue_constant_expression (body)) |
4905dfb6 | 6206 | { |
d6ef1dfb | 6207 | if (!DECL_GENERATED_P (fun)) |
7c51b530 | 6208 | require_potential_rvalue_constant_expression (body); |
4905dfb6 | 6209 | return NULL; |
6210 | } | |
e2a3b42d | 6211 | |
bbab4abf | 6212 | if (DECL_CONSTRUCTOR_P (fun) |
d6ef1dfb | 6213 | && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun))) |
bbab4abf | 6214 | return NULL; |
6215 | ||
e2a3b42d | 6216 | /* Create the constexpr function table if necessary. */ |
6217 | if (constexpr_fundef_table == NULL) | |
6218 | constexpr_fundef_table = htab_create_ggc (101, | |
6219 | constexpr_fundef_hash, | |
6220 | constexpr_fundef_equal, | |
6221 | ggc_free); | |
6222 | entry.decl = fun; | |
6223 | entry.body = body; | |
6224 | slot = (constexpr_fundef **) | |
6225 | htab_find_slot (constexpr_fundef_table, &entry, INSERT); | |
6226 | ||
6227 | gcc_assert (*slot == NULL); | |
6228 | *slot = ggc_alloc_constexpr_fundef (); | |
6229 | **slot = entry; | |
6230 | ||
4905dfb6 | 6231 | return fun; |
6232 | } | |
6233 | ||
262c8920 | 6234 | /* FUN is a non-constexpr function called in a context that requires a |
6235 | constant expression. If it comes from a constexpr template, explain why | |
6236 | the instantiation isn't constexpr. */ | |
6237 | ||
6238 | void | |
6239 | explain_invalid_constexpr_fn (tree fun) | |
6240 | { | |
6241 | static struct pointer_set_t *diagnosed; | |
6242 | tree body; | |
6243 | location_t save_loc; | |
af185806 | 6244 | /* Only diagnose defaulted functions or instantiations. */ |
6245 | if (!DECL_DEFAULTED_FN (fun) | |
6246 | && !is_instantiation_of_constexpr (fun)) | |
262c8920 | 6247 | return; |
6248 | if (diagnosed == NULL) | |
6249 | diagnosed = pointer_set_create (); | |
6250 | if (pointer_set_insert (diagnosed, fun) != 0) | |
6251 | /* Already explained. */ | |
6252 | return; | |
6253 | ||
6254 | save_loc = input_location; | |
6255 | input_location = DECL_SOURCE_LOCATION (fun); | |
bbab4abf | 6256 | inform (0, "%q+D is not usable as a constexpr function because:", fun); |
262c8920 | 6257 | /* First check the declaration. */ |
6258 | if (is_valid_constexpr_fn (fun, true)) | |
6259 | { | |
6260 | /* Then if it's OK, the body. */ | |
6261 | if (DECL_DEFAULTED_FN (fun)) | |
6262 | explain_implicit_non_constexpr (fun); | |
6263 | else | |
6264 | { | |
6265 | body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); | |
6266 | require_potential_rvalue_constant_expression (body); | |
bbab4abf | 6267 | if (DECL_CONSTRUCTOR_P (fun)) |
6268 | cx_check_missing_mem_inits (fun, body, true); | |
262c8920 | 6269 | } |
6270 | } | |
6271 | input_location = save_loc; | |
6272 | } | |
6273 | ||
ace3c39f | 6274 | /* Objects of this type represent calls to constexpr functions |
6275 | along with the bindings of parameters to their arguments, for | |
6276 | the purpose of compile time evaluation. */ | |
6277 | ||
6278 | typedef struct GTY(()) constexpr_call { | |
6279 | /* Description of the constexpr function definition. */ | |
6280 | constexpr_fundef *fundef; | |
4cdb0eae | 6281 | /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE |
ace3c39f | 6282 | is a parameter _DECL and the TREE_VALUE is the value of the parameter. |
6283 | Note: This arrangement is made to accomodate the use of | |
6284 | iterative_hash_template_arg (see pt.c). If you change this | |
6285 | representation, also change the hash calculation in | |
6286 | cxx_eval_call_expression. */ | |
6287 | tree bindings; | |
6288 | /* Result of the call. | |
6289 | NULL means the call is being evaluated. | |
6290 | error_mark_node means that the evaluation was erroneous; | |
6291 | otherwise, the actuall value of the call. */ | |
6292 | tree result; | |
6293 | /* The hash of this call; we remember it here to avoid having to | |
6294 | recalculate it when expanding the hash table. */ | |
6295 | hashval_t hash; | |
6296 | } constexpr_call; | |
6297 | ||
6298 | /* A table of all constexpr calls that have been evaluated by the | |
6299 | compiler in this translation unit. */ | |
6300 | ||
6301 | static GTY ((param_is (constexpr_call))) htab_t constexpr_call_table; | |
6302 | ||
6303 | static tree cxx_eval_constant_expression (const constexpr_call *, tree, | |
4326a074 | 6304 | bool, bool, bool *, bool *); |
ace3c39f | 6305 | |
6306 | /* Compute a hash value for a constexpr call representation. */ | |
6307 | ||
6308 | static hashval_t | |
6309 | constexpr_call_hash (const void *p) | |
6310 | { | |
6311 | const constexpr_call *info = (const constexpr_call *) p; | |
6312 | return info->hash; | |
6313 | } | |
6314 | ||
6315 | /* Return 1 if the objects pointed to by P and Q represent calls | |
6316 | to the same constexpr function with the same arguments. | |
6317 | Otherwise, return 0. */ | |
6318 | ||
6319 | static int | |
6320 | constexpr_call_equal (const void *p, const void *q) | |
6321 | { | |
6322 | const constexpr_call *lhs = (const constexpr_call *) p; | |
6323 | const constexpr_call *rhs = (const constexpr_call *) q; | |
6324 | tree lhs_bindings; | |
6325 | tree rhs_bindings; | |
6326 | if (lhs == rhs) | |
6327 | return 1; | |
6328 | if (!constexpr_fundef_equal (lhs->fundef, rhs->fundef)) | |
6329 | return 0; | |
6330 | lhs_bindings = lhs->bindings; | |
6331 | rhs_bindings = rhs->bindings; | |
6332 | while (lhs_bindings != NULL && rhs_bindings != NULL) | |
6333 | { | |
6334 | tree lhs_arg = TREE_VALUE (lhs_bindings); | |
6335 | tree rhs_arg = TREE_VALUE (rhs_bindings); | |
6336 | gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg)); | |
6337 | if (!cp_tree_equal (lhs_arg, rhs_arg)) | |
6338 | return 0; | |
6339 | lhs_bindings = TREE_CHAIN (lhs_bindings); | |
6340 | rhs_bindings = TREE_CHAIN (rhs_bindings); | |
6341 | } | |
6342 | return lhs_bindings == rhs_bindings; | |
6343 | } | |
6344 | ||
6345 | /* Initialize the constexpr call table, if needed. */ | |
6346 | ||
6347 | static void | |
6348 | maybe_initialize_constexpr_call_table (void) | |
6349 | { | |
6350 | if (constexpr_call_table == NULL) | |
6351 | constexpr_call_table = htab_create_ggc (101, | |
6352 | constexpr_call_hash, | |
6353 | constexpr_call_equal, | |
6354 | ggc_free); | |
6355 | } | |
6356 | ||
4905dfb6 | 6357 | /* Return true if T designates the implied `this' parameter. */ |
6358 | ||
6359 | static inline bool | |
6360 | is_this_parameter (tree t) | |
6361 | { | |
6362 | return t == current_class_ptr; | |
6363 | } | |
6364 | ||
6365 | /* We have an expression tree T that represents a call, either CALL_EXPR | |
6366 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
6367 | retrun the _DECL for that function. */ | |
6368 | ||
6369 | static tree | |
6370 | get_function_named_in_call (tree t) | |
6371 | { | |
6372 | tree fun = NULL; | |
6373 | switch (TREE_CODE (t)) | |
6374 | { | |
6375 | case CALL_EXPR: | |
6376 | fun = CALL_EXPR_FN (t); | |
6377 | break; | |
6378 | ||
6379 | case AGGR_INIT_EXPR: | |
6380 | fun = AGGR_INIT_EXPR_FN (t); | |
6381 | break; | |
6382 | ||
6383 | default: | |
6384 | gcc_unreachable(); | |
6385 | break; | |
6386 | } | |
6387 | if (TREE_CODE (fun) == ADDR_EXPR | |
6388 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
6389 | fun = TREE_OPERAND (fun, 0); | |
17814aca | 6390 | return fun; |
6391 | } | |
6392 | ||
4905dfb6 | 6393 | /* We have an expression tree T that represents a call, either CALL_EXPR |
6394 | or AGGR_INIT_EXPR. Return the Nth argument. */ | |
6395 | ||
6396 | static inline tree | |
6397 | get_nth_callarg (tree t, int n) | |
6398 | { | |
6399 | switch (TREE_CODE (t)) | |
6400 | { | |
6401 | case CALL_EXPR: | |
6402 | return CALL_EXPR_ARG (t, n); | |
6403 | ||
6404 | case AGGR_INIT_EXPR: | |
6405 | return AGGR_INIT_EXPR_ARG (t, n); | |
6406 | ||
6407 | default: | |
6408 | gcc_unreachable (); | |
6409 | return NULL; | |
6410 | } | |
6411 | } | |
6412 | ||
ace3c39f | 6413 | /* Look up the binding of the function parameter T in a constexpr |
6414 | function call context CALL. */ | |
6415 | ||
6416 | static tree | |
6417 | lookup_parameter_binding (const constexpr_call *call, tree t) | |
6418 | { | |
6419 | tree b = purpose_member (t, call->bindings); | |
6420 | return TREE_VALUE (b); | |
6421 | } | |
6422 | ||
6423 | /* Attempt to evaluate T which represents a call to a builtin function. | |
6424 | We assume here that all builtin functions evaluate to scalar types | |
6425 | represented by _CST nodes. */ | |
6426 | ||
6427 | static tree | |
6428 | cxx_eval_builtin_function_call (const constexpr_call *call, tree t, | |
6429 | bool allow_non_constant, bool addr, | |
4326a074 | 6430 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6431 | { |
6432 | const int nargs = call_expr_nargs (t); | |
6433 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
6434 | tree new_call; | |
6435 | int i; | |
6436 | for (i = 0; i < nargs; ++i) | |
6437 | { | |
6438 | args[i] = cxx_eval_constant_expression (call, CALL_EXPR_ARG (t, i), | |
6439 | allow_non_constant, addr, | |
4326a074 | 6440 | non_constant_p, overflow_p); |
ace3c39f | 6441 | if (allow_non_constant && *non_constant_p) |
6442 | return t; | |
6443 | } | |
6444 | if (*non_constant_p) | |
6445 | return t; | |
6446 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), | |
6447 | CALL_EXPR_FN (t), nargs, args); | |
c5fe651b | 6448 | new_call = fold (new_call); |
6449 | VERIFY_CONSTANT (new_call); | |
6450 | return new_call; | |
ace3c39f | 6451 | } |
6452 | ||
6453 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
6454 | the type of the value to match. */ | |
6455 | ||
6456 | static tree | |
6457 | adjust_temp_type (tree type, tree temp) | |
6458 | { | |
6459 | if (TREE_TYPE (temp) == type) | |
6460 | return temp; | |
6461 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
6462 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
6463 | return build_constructor (type, CONSTRUCTOR_ELTS (temp)); | |
02e4455f | 6464 | gcc_assert (scalarish_type_p (type)); |
a1f05651 | 6465 | return cp_fold_convert (type, temp); |
ace3c39f | 6466 | } |
6467 | ||
6468 | /* Subroutine of cxx_eval_call_expression. | |
6469 | We are processing a call expression (either CALL_EXPR or | |
6470 | AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate | |
6471 | all arguments and bind their values to correspondings | |
6472 | parameters, making up the NEW_CALL context. */ | |
6473 | ||
6474 | static void | |
6475 | cxx_bind_parameters_in_call (const constexpr_call *old_call, tree t, | |
6476 | constexpr_call *new_call, | |
6477 | bool allow_non_constant, | |
4326a074 | 6478 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6479 | { |
6480 | const int nargs = call_expr_nargs (t); | |
6481 | tree fun = new_call->fundef->decl; | |
6482 | tree parms = DECL_ARGUMENTS (fun); | |
6483 | int i; | |
6484 | for (i = 0; i < nargs; ++i) | |
6485 | { | |
6486 | tree x, arg; | |
6487 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
6488 | /* For member function, the first argument is a pointer to the implied | |
6489 | object. And for an object contruction, don't bind `this' before | |
6490 | it is fully constructed. */ | |
6491 | if (i == 0 && DECL_CONSTRUCTOR_P (fun)) | |
6492 | goto next; | |
6493 | x = get_nth_callarg (t, i); | |
6494 | arg = cxx_eval_constant_expression (old_call, x, allow_non_constant, | |
6495 | TREE_CODE (type) == REFERENCE_TYPE, | |
4326a074 | 6496 | non_constant_p, overflow_p); |
ace3c39f | 6497 | /* Don't VERIFY_CONSTANT here. */ |
6498 | if (*non_constant_p && allow_non_constant) | |
6499 | return; | |
6500 | /* Just discard ellipsis args after checking their constantitude. */ | |
6501 | if (!parms) | |
6502 | continue; | |
a6789aad | 6503 | if (*non_constant_p) |
6504 | /* Don't try to adjust the type of non-constant args. */ | |
6505 | goto next; | |
ace3c39f | 6506 | |
6507 | /* Make sure the binding has the same type as the parm. */ | |
6508 | if (TREE_CODE (type) != REFERENCE_TYPE) | |
6509 | arg = adjust_temp_type (type, arg); | |
6510 | new_call->bindings = tree_cons (parms, arg, new_call->bindings); | |
6511 | next: | |
6512 | parms = TREE_CHAIN (parms); | |
6513 | } | |
6514 | } | |
6515 | ||
0a32318d | 6516 | /* Variables and functions to manage constexpr call expansion context. |
6517 | These do not need to be marked for PCH or GC. */ | |
6518 | ||
54cf6eed | 6519 | /* FIXME remember and print actual constant arguments. */ |
1e094109 | 6520 | static vec<tree> call_stack = vNULL; |
0a32318d | 6521 | static int call_stack_tick; |
6522 | static int last_cx_error_tick; | |
6523 | ||
54cf6eed | 6524 | static bool |
0a32318d | 6525 | push_cx_call_context (tree call) |
6526 | { | |
6527 | ++call_stack_tick; | |
6528 | if (!EXPR_HAS_LOCATION (call)) | |
6529 | SET_EXPR_LOCATION (call, input_location); | |
f1f41a6c | 6530 | call_stack.safe_push (call); |
6531 | if (call_stack.length () > (unsigned) max_constexpr_depth) | |
54cf6eed | 6532 | return false; |
6533 | return true; | |
0a32318d | 6534 | } |
6535 | ||
6536 | static void | |
6537 | pop_cx_call_context (void) | |
6538 | { | |
6539 | ++call_stack_tick; | |
f1f41a6c | 6540 | call_stack.pop (); |
0a32318d | 6541 | } |
6542 | ||
f1f41a6c | 6543 | vec<tree> |
0a32318d | 6544 | cx_error_context (void) |
6545 | { | |
1e094109 | 6546 | vec<tree> r = vNULL; |
0a32318d | 6547 | if (call_stack_tick != last_cx_error_tick |
f1f41a6c | 6548 | && !call_stack.is_empty ()) |
0a32318d | 6549 | r = call_stack; |
6550 | last_cx_error_tick = call_stack_tick; | |
6551 | return r; | |
6552 | } | |
6553 | ||
ace3c39f | 6554 | /* Subroutine of cxx_eval_constant_expression. |
6555 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
6556 | evaluation. */ | |
6557 | ||
6558 | static tree | |
6559 | cxx_eval_call_expression (const constexpr_call *old_call, tree t, | |
6560 | bool allow_non_constant, bool addr, | |
4326a074 | 6561 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6562 | { |
0a32318d | 6563 | location_t loc = EXPR_LOC_OR_HERE (t); |
ace3c39f | 6564 | tree fun = get_function_named_in_call (t); |
6565 | tree result; | |
6566 | constexpr_call new_call = { NULL, NULL, NULL, 0 }; | |
6567 | constexpr_call **slot; | |
54cf6eed | 6568 | constexpr_call *entry; |
6569 | bool depth_ok; | |
6570 | ||
ace3c39f | 6571 | if (TREE_CODE (fun) != FUNCTION_DECL) |
6572 | { | |
6573 | /* Might be a constexpr function pointer. */ | |
6574 | fun = cxx_eval_constant_expression (old_call, fun, allow_non_constant, | |
4326a074 | 6575 | /*addr*/false, non_constant_p, overflow_p); |
ace3c39f | 6576 | if (TREE_CODE (fun) == ADDR_EXPR) |
6577 | fun = TREE_OPERAND (fun, 0); | |
6578 | } | |
6579 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
6580 | { | |
262c8920 | 6581 | if (!allow_non_constant && !*non_constant_p) |
ace3c39f | 6582 | error_at (loc, "expression %qE does not designate a constexpr " |
6583 | "function", fun); | |
6584 | *non_constant_p = true; | |
6585 | return t; | |
6586 | } | |
6587 | if (DECL_CLONED_FUNCTION_P (fun)) | |
6588 | fun = DECL_CLONED_FUNCTION (fun); | |
6589 | if (is_builtin_fn (fun)) | |
6590 | return cxx_eval_builtin_function_call (old_call, t, allow_non_constant, | |
4326a074 | 6591 | addr, non_constant_p, overflow_p); |
ace3c39f | 6592 | if (!DECL_DECLARED_CONSTEXPR_P (fun)) |
6593 | { | |
6594 | if (!allow_non_constant) | |
6595 | { | |
262c8920 | 6596 | error_at (loc, "call to non-constexpr function %qD", fun); |
6597 | explain_invalid_constexpr_fn (fun); | |
ace3c39f | 6598 | } |
6599 | *non_constant_p = true; | |
6600 | return t; | |
6601 | } | |
6602 | ||
29379c7b | 6603 | /* Shortcut trivial copy constructor/op=. */ |
6604 | if (call_expr_nargs (t) == 2 && trivial_fn_p (fun)) | |
3d87e7a2 | 6605 | { |
6606 | tree arg = convert_from_reference (get_nth_callarg (t, 1)); | |
6607 | return cxx_eval_constant_expression (old_call, arg, allow_non_constant, | |
4326a074 | 6608 | addr, non_constant_p, overflow_p); |
3d87e7a2 | 6609 | } |
29379c7b | 6610 | |
ace3c39f | 6611 | /* If in direct recursive call, optimize definition search. */ |
6612 | if (old_call != NULL && old_call->fundef->decl == fun) | |
6613 | new_call.fundef = old_call->fundef; | |
6614 | else | |
6615 | { | |
6616 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
6617 | if (new_call.fundef == NULL || new_call.fundef->body == NULL) | |
6618 | { | |
6619 | if (!allow_non_constant) | |
bbab4abf | 6620 | { |
d9c249a4 | 6621 | if (DECL_INITIAL (fun)) |
bbab4abf | 6622 | { |
6623 | /* The definition of fun was somehow unsuitable. */ | |
6624 | error_at (loc, "%qD called in a constant expression", fun); | |
6625 | explain_invalid_constexpr_fn (fun); | |
6626 | } | |
6627 | else | |
6628 | error_at (loc, "%qD used before its definition", fun); | |
6629 | } | |
ace3c39f | 6630 | *non_constant_p = true; |
6631 | return t; | |
6632 | } | |
6633 | } | |
6634 | cxx_bind_parameters_in_call (old_call, t, &new_call, | |
4326a074 | 6635 | allow_non_constant, non_constant_p, overflow_p); |
ace3c39f | 6636 | if (*non_constant_p) |
6637 | return t; | |
6638 | ||
54cf6eed | 6639 | depth_ok = push_cx_call_context (t); |
0a32318d | 6640 | |
ace3c39f | 6641 | new_call.hash |
6642 | = iterative_hash_template_arg (new_call.bindings, | |
6643 | constexpr_fundef_hash (new_call.fundef)); | |
6644 | ||
6645 | /* If we have seen this call before, we are done. */ | |
6646 | maybe_initialize_constexpr_call_table (); | |
6647 | slot = (constexpr_call **) | |
6648 | htab_find_slot (constexpr_call_table, &new_call, INSERT); | |
54cf6eed | 6649 | entry = *slot; |
6650 | if (entry == NULL) | |
ace3c39f | 6651 | { |
6652 | /* We need to keep a pointer to the entry, not just the slot, as the | |
6653 | slot can move in the call to cxx_eval_builtin_function_call. */ | |
54cf6eed | 6654 | *slot = entry = ggc_alloc_constexpr_call (); |
ace3c39f | 6655 | *entry = new_call; |
54cf6eed | 6656 | } |
6657 | /* Calls which are in progress have their result set to NULL | |
6658 | so that we can detect circular dependencies. */ | |
6659 | else if (entry->result == NULL) | |
6660 | { | |
6661 | if (!allow_non_constant) | |
6662 | error ("call has circular dependency"); | |
6663 | *non_constant_p = true; | |
6664 | entry->result = result = error_mark_node; | |
6665 | } | |
6666 | ||
6667 | if (!depth_ok) | |
6668 | { | |
6669 | if (!allow_non_constant) | |
6670 | error ("constexpr evaluation depth exceeds maximum of %d (use " | |
6671 | "-fconstexpr-depth= to increase the maximum)", | |
6672 | max_constexpr_depth); | |
6673 | *non_constant_p = true; | |
6674 | entry->result = result = error_mark_node; | |
6675 | } | |
6676 | else | |
6677 | { | |
6678 | result = entry->result; | |
944ec1bb | 6679 | if (!result || result == error_mark_node) |
54cf6eed | 6680 | result = (cxx_eval_constant_expression |
6681 | (&new_call, new_call.fundef->body, | |
6682 | allow_non_constant, addr, | |
4326a074 | 6683 | non_constant_p, overflow_p)); |
54cf6eed | 6684 | if (result == error_mark_node) |
6685 | *non_constant_p = true; | |
ace3c39f | 6686 | if (*non_constant_p) |
6687 | entry->result = result = error_mark_node; | |
6688 | else | |
6689 | { | |
6690 | /* If this was a call to initialize an object, set the type of | |
6691 | the CONSTRUCTOR to the type of that object. */ | |
6692 | if (DECL_CONSTRUCTOR_P (fun)) | |
6693 | { | |
6694 | tree ob_arg = get_nth_callarg (t, 0); | |
6695 | STRIP_NOPS (ob_arg); | |
6696 | gcc_assert (TREE_CODE (TREE_TYPE (ob_arg)) == POINTER_TYPE | |
6697 | && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg)))); | |
6698 | result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)), | |
6699 | result); | |
6700 | } | |
6701 | entry->result = result; | |
6702 | } | |
6703 | } | |
6704 | ||
0a32318d | 6705 | pop_cx_call_context (); |
56c73cbc | 6706 | return unshare_expr (result); |
ace3c39f | 6707 | } |
6708 | ||
a1f05651 | 6709 | /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ |
6710 | ||
ace3c39f | 6711 | bool |
6712 | reduced_constant_expression_p (tree t) | |
6713 | { | |
ace3c39f | 6714 | /* FIXME are we calling this too much? */ |
6715 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
6716 | } | |
6717 | ||
6718 | /* Some expressions may have constant operands but are not constant | |
6719 | themselves, such as 1/0. Call this function (or rather, the macro | |
6720 | following it) to check for that condition. | |
6721 | ||
6722 | We only call this in places that require an arithmetic constant, not in | |
6723 | places where we might have a non-constant expression that can be a | |
6724 | component of a constant expression, such as the address of a constexpr | |
6725 | variable that might be dereferenced later. */ | |
6726 | ||
6727 | static bool | |
4326a074 | 6728 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, |
6729 | bool *overflow_p) | |
ace3c39f | 6730 | { |
6731 | if (!*non_constant_p && !reduced_constant_expression_p (t)) | |
4326a074 | 6732 | { |
6733 | if (!allow_non_constant) | |
6734 | error ("%q+E is not a constant expression", t); | |
6735 | *non_constant_p = true; | |
6736 | } | |
6737 | if (TREE_OVERFLOW_P (t)) | |
ace3c39f | 6738 | { |
6739 | if (!allow_non_constant) | |
a1f05651 | 6740 | { |
4326a074 | 6741 | permerror (input_location, "overflow in constant expression"); |
6742 | /* If we're being permissive (and are in an enforcing | |
6743 | context), ignore the overflow. */ | |
6744 | if (flag_permissive) | |
6745 | return *non_constant_p; | |
a1f05651 | 6746 | } |
4326a074 | 6747 | *overflow_p = true; |
ace3c39f | 6748 | } |
6749 | return *non_constant_p; | |
6750 | } | |
ace3c39f | 6751 | |
6752 | /* Subroutine of cxx_eval_constant_expression. | |
6753 | Attempt to reduce the unary expression tree T to a compile time value. | |
6754 | If successful, return the value. Otherwise issue a diagnostic | |
6755 | and return error_mark_node. */ | |
6756 | ||
6757 | static tree | |
6758 | cxx_eval_unary_expression (const constexpr_call *call, tree t, | |
6759 | bool allow_non_constant, bool addr, | |
4326a074 | 6760 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6761 | { |
6762 | tree r; | |
6763 | tree orig_arg = TREE_OPERAND (t, 0); | |
6764 | tree arg = cxx_eval_constant_expression (call, orig_arg, allow_non_constant, | |
4326a074 | 6765 | addr, non_constant_p, overflow_p); |
ace3c39f | 6766 | VERIFY_CONSTANT (arg); |
6767 | if (arg == orig_arg) | |
6768 | return t; | |
6769 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), arg); | |
6770 | VERIFY_CONSTANT (r); | |
6771 | return r; | |
6772 | } | |
6773 | ||
6774 | /* Subroutine of cxx_eval_constant_expression. | |
6775 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
6776 | ||
6777 | static tree | |
6778 | cxx_eval_binary_expression (const constexpr_call *call, tree t, | |
6779 | bool allow_non_constant, bool addr, | |
4326a074 | 6780 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6781 | { |
6782 | tree r; | |
6783 | tree orig_lhs = TREE_OPERAND (t, 0); | |
6784 | tree orig_rhs = TREE_OPERAND (t, 1); | |
6785 | tree lhs, rhs; | |
6786 | lhs = cxx_eval_constant_expression (call, orig_lhs, | |
6787 | allow_non_constant, addr, | |
4326a074 | 6788 | non_constant_p, overflow_p); |
ace3c39f | 6789 | VERIFY_CONSTANT (lhs); |
6790 | rhs = cxx_eval_constant_expression (call, orig_rhs, | |
6791 | allow_non_constant, addr, | |
4326a074 | 6792 | non_constant_p, overflow_p); |
ace3c39f | 6793 | VERIFY_CONSTANT (rhs); |
6794 | if (lhs == orig_lhs && rhs == orig_rhs) | |
6795 | return t; | |
6796 | r = fold_build2 (TREE_CODE (t), TREE_TYPE (t), lhs, rhs); | |
6797 | VERIFY_CONSTANT (r); | |
6798 | return r; | |
6799 | } | |
6800 | ||
6801 | /* Subroutine of cxx_eval_constant_expression. | |
6802 | Attempt to evaluate condition expressions. Dead branches are not | |
6803 | looked into. */ | |
6804 | ||
6805 | static tree | |
6806 | cxx_eval_conditional_expression (const constexpr_call *call, tree t, | |
6807 | bool allow_non_constant, bool addr, | |
4326a074 | 6808 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6809 | { |
6810 | tree val = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0), | |
6811 | allow_non_constant, addr, | |
4326a074 | 6812 | non_constant_p, overflow_p); |
ace3c39f | 6813 | VERIFY_CONSTANT (val); |
251c88b3 | 6814 | /* Don't VERIFY_CONSTANT the other operands. */ |
6815 | if (integer_zerop (val)) | |
6816 | return cxx_eval_constant_expression (call, TREE_OPERAND (t, 2), | |
ace3c39f | 6817 | allow_non_constant, addr, |
4326a074 | 6818 | non_constant_p, overflow_p); |
251c88b3 | 6819 | return cxx_eval_constant_expression (call, TREE_OPERAND (t, 1), |
ace3c39f | 6820 | allow_non_constant, addr, |
4326a074 | 6821 | non_constant_p, overflow_p); |
ace3c39f | 6822 | } |
6823 | ||
6824 | /* Subroutine of cxx_eval_constant_expression. | |
6825 | Attempt to reduce a reference to an array slot. */ | |
6826 | ||
6827 | static tree | |
6828 | cxx_eval_array_reference (const constexpr_call *call, tree t, | |
6829 | bool allow_non_constant, bool addr, | |
4326a074 | 6830 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6831 | { |
6832 | tree oldary = TREE_OPERAND (t, 0); | |
6833 | tree ary = cxx_eval_constant_expression (call, oldary, | |
6834 | allow_non_constant, addr, | |
4326a074 | 6835 | non_constant_p, overflow_p); |
ace3c39f | 6836 | tree index, oldidx; |
6837 | HOST_WIDE_INT i; | |
2349ea85 | 6838 | tree elem_type; |
191115df | 6839 | unsigned len, elem_nchars = 1; |
ace3c39f | 6840 | if (*non_constant_p) |
6841 | return t; | |
6842 | oldidx = TREE_OPERAND (t, 1); | |
6843 | index = cxx_eval_constant_expression (call, oldidx, | |
6844 | allow_non_constant, false, | |
4326a074 | 6845 | non_constant_p, overflow_p); |
ace3c39f | 6846 | VERIFY_CONSTANT (index); |
6847 | if (addr && ary == oldary && index == oldidx) | |
6848 | return t; | |
6849 | else if (addr) | |
6850 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
2349ea85 | 6851 | elem_type = TREE_TYPE (TREE_TYPE (ary)); |
191115df | 6852 | if (TREE_CODE (ary) == CONSTRUCTOR) |
6853 | len = CONSTRUCTOR_NELTS (ary); | |
f68c484f | 6854 | else if (TREE_CODE (ary) == STRING_CST) |
191115df | 6855 | { |
2349ea85 | 6856 | elem_nchars = (TYPE_PRECISION (elem_type) |
191115df | 6857 | / TYPE_PRECISION (char_type_node)); |
6858 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
6859 | } | |
f68c484f | 6860 | else |
6861 | { | |
6862 | /* We can't do anything with other tree codes, so use | |
6863 | VERIFY_CONSTANT to complain and fail. */ | |
6864 | VERIFY_CONSTANT (ary); | |
6865 | gcc_unreachable (); | |
6866 | } | |
ace3c39f | 6867 | if (compare_tree_int (index, len) >= 0) |
6868 | { | |
2349ea85 | 6869 | if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary)))) |
6870 | { | |
6871 | /* If it's within the array bounds but doesn't have an explicit | |
6872 | initializer, it's value-initialized. */ | |
6873 | tree val = build_value_init (elem_type, tf_warning_or_error); | |
6874 | return cxx_eval_constant_expression (call, val, | |
6875 | allow_non_constant, addr, | |
4326a074 | 6876 | non_constant_p, overflow_p); |
2349ea85 | 6877 | } |
6878 | ||
ace3c39f | 6879 | if (!allow_non_constant) |
6880 | error ("array subscript out of bound"); | |
6881 | *non_constant_p = true; | |
6882 | return t; | |
6883 | } | |
6884 | i = tree_low_cst (index, 0); | |
6885 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
f1f41a6c | 6886 | return (*CONSTRUCTOR_ELTS (ary))[i].value; |
191115df | 6887 | else if (elem_nchars == 1) |
ace3c39f | 6888 | return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))), |
6889 | TREE_STRING_POINTER (ary)[i]); | |
191115df | 6890 | else |
6891 | { | |
6892 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary))); | |
6893 | return native_interpret_expr (type, (const unsigned char *) | |
6894 | TREE_STRING_POINTER (ary) | |
6895 | + i * elem_nchars, elem_nchars); | |
6896 | } | |
ace3c39f | 6897 | /* Don't VERIFY_CONSTANT here. */ |
6898 | } | |
6899 | ||
6900 | /* Subroutine of cxx_eval_constant_expression. | |
6901 | Attempt to reduce a field access of a value of class type. */ | |
6902 | ||
6903 | static tree | |
6904 | cxx_eval_component_reference (const constexpr_call *call, tree t, | |
6905 | bool allow_non_constant, bool addr, | |
4326a074 | 6906 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 6907 | { |
6908 | unsigned HOST_WIDE_INT i; | |
6909 | tree field; | |
6910 | tree value; | |
6911 | tree part = TREE_OPERAND (t, 1); | |
6912 | tree orig_whole = TREE_OPERAND (t, 0); | |
6913 | tree whole = cxx_eval_constant_expression (call, orig_whole, | |
6914 | allow_non_constant, addr, | |
4326a074 | 6915 | non_constant_p, overflow_p); |
ace3c39f | 6916 | if (whole == orig_whole) |
6917 | return t; | |
6918 | if (addr) | |
6919 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), | |
6920 | whole, part, NULL_TREE); | |
6921 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
6922 | CONSTRUCTOR. */ | |
6923 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
6924 | { | |
6925 | if (!allow_non_constant) | |
6926 | error ("%qE is not a constant expression", orig_whole); | |
6927 | *non_constant_p = true; | |
6928 | } | |
f602376c | 6929 | if (DECL_MUTABLE_P (part)) |
6930 | { | |
6931 | if (!allow_non_constant) | |
6932 | error ("mutable %qD is not usable in a constant expression", part); | |
6933 | *non_constant_p = true; | |
6934 | } | |
ace3c39f | 6935 | if (*non_constant_p) |
6936 | return t; | |
6937 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
6938 | { | |
6939 | if (field == part) | |
6940 | return value; | |
6941 | } | |
5ece2854 | 6942 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE |
6943 | && CONSTRUCTOR_NELTS (whole) > 0) | |
ace3c39f | 6944 | { |
8e1cdf27 | 6945 | /* DR 1188 says we don't have to deal with this. */ |
ace3c39f | 6946 | if (!allow_non_constant) |
6947 | error ("accessing %qD member instead of initialized %qD member in " | |
6948 | "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index); | |
6949 | *non_constant_p = true; | |
6950 | return t; | |
6951 | } | |
5ece2854 | 6952 | |
6953 | /* If there's no explicit init for this field, it's value-initialized. */ | |
6954 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
6955 | return cxx_eval_constant_expression (call, value, | |
6956 | allow_non_constant, addr, | |
4326a074 | 6957 | non_constant_p, overflow_p); |
ace3c39f | 6958 | } |
6959 | ||
fa704bb1 | 6960 | /* Subroutine of cxx_eval_constant_expression. |
6961 | Attempt to reduce a field access of a value of class type that is | |
6962 | expressed as a BIT_FIELD_REF. */ | |
6963 | ||
6964 | static tree | |
6965 | cxx_eval_bit_field_ref (const constexpr_call *call, tree t, | |
6966 | bool allow_non_constant, bool addr, | |
4326a074 | 6967 | bool *non_constant_p, bool *overflow_p) |
fa704bb1 | 6968 | { |
6969 | tree orig_whole = TREE_OPERAND (t, 0); | |
793c6646 | 6970 | tree retval, fldval, utype, mask; |
6971 | bool fld_seen = false; | |
6972 | HOST_WIDE_INT istart, isize; | |
fa704bb1 | 6973 | tree whole = cxx_eval_constant_expression (call, orig_whole, |
6974 | allow_non_constant, addr, | |
4326a074 | 6975 | non_constant_p, overflow_p); |
fa704bb1 | 6976 | tree start, field, value; |
6977 | unsigned HOST_WIDE_INT i; | |
6978 | ||
6979 | if (whole == orig_whole) | |
6980 | return t; | |
6981 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
6982 | CONSTRUCTOR. */ | |
6983 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
6984 | { | |
6985 | if (!allow_non_constant) | |
6986 | error ("%qE is not a constant expression", orig_whole); | |
6987 | *non_constant_p = true; | |
6988 | } | |
6989 | if (*non_constant_p) | |
6990 | return t; | |
6991 | ||
6992 | start = TREE_OPERAND (t, 2); | |
793c6646 | 6993 | istart = tree_low_cst (start, 0); |
6994 | isize = tree_low_cst (TREE_OPERAND (t, 1), 0); | |
6995 | utype = TREE_TYPE (t); | |
6996 | if (!TYPE_UNSIGNED (utype)) | |
6997 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
6998 | retval = build_int_cst (utype, 0); | |
fa704bb1 | 6999 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
7000 | { | |
793c6646 | 7001 | tree bitpos = bit_position (field); |
7002 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) | |
fa704bb1 | 7003 | return value; |
793c6646 | 7004 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE |
7005 | && TREE_CODE (value) == INTEGER_CST | |
7006 | && host_integerp (bitpos, 0) | |
7007 | && host_integerp (DECL_SIZE (field), 0)) | |
7008 | { | |
7009 | HOST_WIDE_INT bit = tree_low_cst (bitpos, 0); | |
7010 | HOST_WIDE_INT sz = tree_low_cst (DECL_SIZE (field), 0); | |
7011 | HOST_WIDE_INT shift; | |
7012 | if (bit >= istart && bit + sz <= istart + isize) | |
7013 | { | |
7014 | fldval = fold_convert (utype, value); | |
7015 | mask = build_int_cst_type (utype, -1); | |
7016 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
7017 | size_int (TYPE_PRECISION (utype) - sz)); | |
7018 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
7019 | size_int (TYPE_PRECISION (utype) - sz)); | |
7020 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
7021 | shift = bit - istart; | |
7022 | if (BYTES_BIG_ENDIAN) | |
7023 | shift = TYPE_PRECISION (utype) - shift - sz; | |
7024 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
7025 | size_int (shift)); | |
7026 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
7027 | fld_seen = true; | |
7028 | } | |
7029 | } | |
fa704bb1 | 7030 | } |
793c6646 | 7031 | if (fld_seen) |
7032 | return fold_convert (TREE_TYPE (t), retval); | |
7033 | gcc_unreachable (); | |
fa704bb1 | 7034 | return error_mark_node; |
7035 | } | |
7036 | ||
ace3c39f | 7037 | /* Subroutine of cxx_eval_constant_expression. |
7038 | Evaluate a short-circuited logical expression T in the context | |
7039 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
7040 | early return. CONTINUE_VALUE is used here purely for | |
7041 | sanity check purposes. */ | |
7042 | ||
7043 | static tree | |
7044 | cxx_eval_logical_expression (const constexpr_call *call, tree t, | |
7045 | tree bailout_value, tree continue_value, | |
7046 | bool allow_non_constant, bool addr, | |
4326a074 | 7047 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 7048 | { |
7049 | tree r; | |
7050 | tree lhs = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0), | |
7051 | allow_non_constant, addr, | |
4326a074 | 7052 | non_constant_p, overflow_p); |
ace3c39f | 7053 | VERIFY_CONSTANT (lhs); |
d4a724ce | 7054 | if (tree_int_cst_equal (lhs, bailout_value)) |
ace3c39f | 7055 | return lhs; |
d4a724ce | 7056 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); |
ace3c39f | 7057 | r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1), |
4326a074 | 7058 | allow_non_constant, addr, non_constant_p, overflow_p); |
ace3c39f | 7059 | VERIFY_CONSTANT (r); |
7060 | return r; | |
7061 | } | |
7062 | ||
05d560e7 | 7063 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of |
7064 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
7065 | field. Return a pointer to the constructor_elt corresponding to the | |
7066 | initialization of the field. */ | |
7067 | ||
7068 | static constructor_elt * | |
f1f41a6c | 7069 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) |
05d560e7 | 7070 | { |
7071 | tree aggr = TREE_OPERAND (ref, 0); | |
7072 | tree field = TREE_OPERAND (ref, 1); | |
7073 | HOST_WIDE_INT i; | |
7074 | constructor_elt *ce; | |
7075 | ||
7076 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
7077 | ||
7078 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
7079 | { | |
7080 | constructor_elt *base_ce | |
7081 | = base_field_constructor_elt (v, aggr); | |
7082 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
7083 | } | |
7084 | ||
f1f41a6c | 7085 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
05d560e7 | 7086 | if (ce->index == field) |
7087 | return ce; | |
7088 | ||
7089 | gcc_unreachable (); | |
7090 | return NULL; | |
7091 | } | |
7092 | ||
ace3c39f | 7093 | /* Subroutine of cxx_eval_constant_expression. |
7094 | The expression tree T denotes a C-style array or a C-style | |
7095 | aggregate. Reduce it to a constant expression. */ | |
7096 | ||
7097 | static tree | |
7098 | cxx_eval_bare_aggregate (const constexpr_call *call, tree t, | |
7099 | bool allow_non_constant, bool addr, | |
4326a074 | 7100 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 7101 | { |
f1f41a6c | 7102 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
7103 | vec<constructor_elt, va_gc> *n; | |
7104 | vec_alloc (n, vec_safe_length (v)); | |
ace3c39f | 7105 | constructor_elt *ce; |
7106 | HOST_WIDE_INT i; | |
7107 | bool changed = false; | |
ace3c39f | 7108 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); |
f1f41a6c | 7109 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
ace3c39f | 7110 | { |
7111 | tree elt = cxx_eval_constant_expression (call, ce->value, | |
7112 | allow_non_constant, addr, | |
4326a074 | 7113 | non_constant_p, overflow_p); |
ace3c39f | 7114 | /* Don't VERIFY_CONSTANT here. */ |
7115 | if (allow_non_constant && *non_constant_p) | |
7116 | goto fail; | |
7117 | if (elt != ce->value) | |
7118 | changed = true; | |
05d560e7 | 7119 | if (TREE_CODE (ce->index) == COMPONENT_REF) |
ace3c39f | 7120 | { |
05d560e7 | 7121 | /* This is an initialization of a vfield inside a base |
7122 | subaggregate that we already initialized; push this | |
7123 | initialization into the previous initialization. */ | |
7124 | constructor_elt *inner = base_field_constructor_elt (n, ce->index); | |
7125 | inner->value = elt; | |
ace3c39f | 7126 | } |
2e6c5d72 | 7127 | else if (TREE_CODE (ce->index) == NOP_EXPR) |
7128 | { | |
7129 | /* This is an initializer for an empty base; now that we've | |
7130 | checked that it's constant, we can ignore it. */ | |
7131 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce->index)))); | |
7132 | } | |
ace3c39f | 7133 | else |
7134 | CONSTRUCTOR_APPEND_ELT (n, ce->index, elt); | |
7135 | } | |
7136 | if (*non_constant_p || !changed) | |
7137 | { | |
7138 | fail: | |
f1f41a6c | 7139 | vec_free (n); |
ace3c39f | 7140 | return t; |
7141 | } | |
7142 | t = build_constructor (TREE_TYPE (t), n); | |
7143 | TREE_CONSTANT (t) = true; | |
7144 | return t; | |
7145 | } | |
7146 | ||
7147 | /* Subroutine of cxx_eval_constant_expression. | |
7148 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
7149 | initialization of a non-static data member of array type. Reduce it to a | |
7150 | CONSTRUCTOR. | |
7151 | ||
c359c2b2 | 7152 | Note that apart from value-initialization (when VALUE_INIT is true), |
7153 | this is only intended to support value-initialization and the | |
7154 | initializations done by defaulted constructors for classes with | |
7155 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
7156 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
7157 | for the copy/move constructor. */ | |
ace3c39f | 7158 | |
7159 | static tree | |
7160 | cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init, | |
c359c2b2 | 7161 | bool value_init, bool allow_non_constant, bool addr, |
4326a074 | 7162 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 7163 | { |
7164 | tree elttype = TREE_TYPE (atype); | |
7165 | int max = tree_low_cst (array_type_nelts (atype), 0); | |
f1f41a6c | 7166 | vec<constructor_elt, va_gc> *n; |
7167 | vec_alloc (n, max + 1); | |
2f205e67 | 7168 | bool pre_init = false; |
ace3c39f | 7169 | int i; |
7170 | ||
7171 | /* For the default constructor, build up a call to the default | |
7172 | constructor of the element type. We only need to handle class types | |
7173 | here, as for a constructor to be constexpr, all members must be | |
7174 | initialized, which for a defaulted default constructor means they must | |
7175 | be of a class type with a constexpr default constructor. */ | |
2f205e67 | 7176 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
7177 | /* We only do this at the lowest level. */; | |
7178 | else if (value_init) | |
7179 | { | |
7180 | init = build_value_init (elttype, tf_warning_or_error); | |
7181 | init = cxx_eval_constant_expression | |
4326a074 | 7182 | (call, init, allow_non_constant, addr, non_constant_p, overflow_p); |
2f205e67 | 7183 | pre_init = true; |
7184 | } | |
c359c2b2 | 7185 | else if (!init) |
ace3c39f | 7186 | { |
f1f41a6c | 7187 | vec<tree, va_gc> *argvec = make_tree_vector (); |
ace3c39f | 7188 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
7189 | &argvec, elttype, LOOKUP_NORMAL, | |
7190 | tf_warning_or_error); | |
7191 | release_tree_vector (argvec); | |
7192 | init = cxx_eval_constant_expression (call, init, allow_non_constant, | |
4326a074 | 7193 | addr, non_constant_p, overflow_p); |
2f205e67 | 7194 | pre_init = true; |
ace3c39f | 7195 | } |
7196 | ||
7197 | if (*non_constant_p && !allow_non_constant) | |
7198 | goto fail; | |
7199 | ||
7200 | for (i = 0; i <= max; ++i) | |
7201 | { | |
7202 | tree idx = build_int_cst (size_type_node, i); | |
7203 | tree eltinit; | |
7204 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
7205 | { | |
7206 | /* A multidimensional array; recurse. */ | |
33185aa6 | 7207 | if (value_init || init == NULL_TREE) |
c359c2b2 | 7208 | eltinit = NULL_TREE; |
7209 | else | |
7210 | eltinit = cp_build_array_ref (input_location, init, idx, | |
7211 | tf_warning_or_error); | |
7212 | eltinit = cxx_eval_vec_init_1 (call, elttype, eltinit, value_init, | |
ace3c39f | 7213 | allow_non_constant, addr, |
4326a074 | 7214 | non_constant_p, overflow_p); |
ace3c39f | 7215 | } |
2f205e67 | 7216 | else if (pre_init) |
c359c2b2 | 7217 | { |
2f205e67 | 7218 | /* Initializing an element using value or default initialization |
7219 | we just pre-built above. */ | |
7220 | if (i == 0) | |
7221 | eltinit = init; | |
7222 | else | |
7223 | eltinit = unshare_expr (init); | |
ace3c39f | 7224 | } |
7225 | else | |
7226 | { | |
7227 | /* Copying an element. */ | |
f1f41a6c | 7228 | vec<tree, va_gc> *argvec; |
ace3c39f | 7229 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
7230 | (atype, TREE_TYPE (init))); | |
7231 | eltinit = cp_build_array_ref (input_location, init, idx, | |
7232 | tf_warning_or_error); | |
7233 | if (!real_lvalue_p (init)) | |
7234 | eltinit = move (eltinit); | |
7235 | argvec = make_tree_vector (); | |
f1f41a6c | 7236 | argvec->quick_push (eltinit); |
ace3c39f | 7237 | eltinit = (build_special_member_call |
7238 | (NULL_TREE, complete_ctor_identifier, &argvec, | |
7239 | elttype, LOOKUP_NORMAL, tf_warning_or_error)); | |
7240 | release_tree_vector (argvec); | |
7241 | eltinit = cxx_eval_constant_expression | |
4326a074 | 7242 | (call, eltinit, allow_non_constant, addr, non_constant_p, overflow_p); |
ace3c39f | 7243 | } |
7244 | if (*non_constant_p && !allow_non_constant) | |
7245 | goto fail; | |
7246 | CONSTRUCTOR_APPEND_ELT (n, idx, eltinit); | |
7247 | } | |
7248 | ||
7249 | if (!*non_constant_p) | |
7250 | { | |
09426ab6 | 7251 | init = build_constructor (atype, n); |
ace3c39f | 7252 | TREE_CONSTANT (init) = true; |
7253 | return init; | |
7254 | } | |
7255 | ||
7256 | fail: | |
f1f41a6c | 7257 | vec_free (n); |
ace3c39f | 7258 | return init; |
7259 | } | |
7260 | ||
7261 | static tree | |
7262 | cxx_eval_vec_init (const constexpr_call *call, tree t, | |
7263 | bool allow_non_constant, bool addr, | |
4326a074 | 7264 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 7265 | { |
7266 | tree atype = TREE_TYPE (t); | |
7267 | tree init = VEC_INIT_EXPR_INIT (t); | |
c359c2b2 | 7268 | tree r = cxx_eval_vec_init_1 (call, atype, init, |
7269 | VEC_INIT_EXPR_VALUE_INIT (t), | |
4326a074 | 7270 | allow_non_constant, addr, non_constant_p, overflow_p); |
ace3c39f | 7271 | if (*non_constant_p) |
7272 | return t; | |
7273 | else | |
7274 | return r; | |
7275 | } | |
7276 | ||
7277 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to | |
7278 | match. We want to be less strict for simple *& folding; if we have a | |
7279 | non-const temporary that we access through a const pointer, that should | |
7280 | work. We handle this here rather than change fold_indirect_ref_1 | |
7281 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
7282 | don't really make sense outside of constant expression evaluation. Also | |
7283 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
97bf3311 | 7284 | with TBAA in fold_indirect_ref_1. |
7285 | ||
7286 | Try to keep this function synced with fold_indirect_ref_1. */ | |
ace3c39f | 7287 | |
7288 | static tree | |
97bf3311 | 7289 | cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base) |
ace3c39f | 7290 | { |
97bf3311 | 7291 | tree sub, subtype; |
ace3c39f | 7292 | |
ace3c39f | 7293 | sub = op0; |
ace3c39f | 7294 | STRIP_NOPS (sub); |
7295 | subtype = TREE_TYPE (sub); | |
9bf1a5dd | 7296 | if (!POINTER_TYPE_P (subtype)) |
7297 | return NULL_TREE; | |
ace3c39f | 7298 | |
7299 | if (TREE_CODE (sub) == ADDR_EXPR) | |
7300 | { | |
7301 | tree op = TREE_OPERAND (sub, 0); | |
7302 | tree optype = TREE_TYPE (op); | |
7303 | ||
97bf3311 | 7304 | /* *&CONST_DECL -> to the value of the const decl. */ |
7305 | if (TREE_CODE (op) == CONST_DECL) | |
7306 | return DECL_INITIAL (op); | |
7307 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
ace3c39f | 7308 | if (same_type_ignoring_top_level_qualifiers_p (optype, type)) |
97bf3311 | 7309 | { |
7310 | tree fop = fold_read_from_constant_string (op); | |
7311 | if (fop) | |
7312 | return fop; | |
7313 | else | |
7314 | return op; | |
7315 | } | |
7316 | /* *(foo *)&fooarray => fooarray[0] */ | |
7317 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
7318 | && (same_type_ignoring_top_level_qualifiers_p | |
7319 | (type, TREE_TYPE (optype)))) | |
7320 | { | |
7321 | tree type_domain = TYPE_DOMAIN (optype); | |
7322 | tree min_val = size_zero_node; | |
7323 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
7324 | min_val = TYPE_MIN_VALUE (type_domain); | |
7325 | return build4_loc (loc, ARRAY_REF, type, op, min_val, | |
7326 | NULL_TREE, NULL_TREE); | |
7327 | } | |
7328 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
7329 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
7330 | && (same_type_ignoring_top_level_qualifiers_p | |
7331 | (type, TREE_TYPE (optype)))) | |
7332 | return fold_build1_loc (loc, REALPART_EXPR, type, op); | |
7333 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
7334 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
7335 | && (same_type_ignoring_top_level_qualifiers_p | |
7336 | (type, TREE_TYPE (optype)))) | |
7337 | { | |
7338 | tree part_width = TYPE_SIZE (type); | |
7339 | tree index = bitsize_int (0); | |
7340 | return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index); | |
7341 | } | |
ace3c39f | 7342 | /* Also handle conversion to an empty base class, which |
7343 | is represented with a NOP_EXPR. */ | |
97bf3311 | 7344 | else if (is_empty_class (type) |
ace3c39f | 7345 | && CLASS_TYPE_P (optype) |
7346 | && DERIVED_FROM_P (type, optype)) | |
7347 | { | |
97bf3311 | 7348 | *empty_base = true; |
7349 | return op; | |
ace3c39f | 7350 | } |
7351 | /* *(foo *)&struct_with_foo_field => COMPONENT_REF */ | |
7352 | else if (RECORD_OR_UNION_TYPE_P (optype)) | |
7353 | { | |
7354 | tree field = TYPE_FIELDS (optype); | |
7355 | for (; field; field = DECL_CHAIN (field)) | |
7356 | if (TREE_CODE (field) == FIELD_DECL | |
7357 | && integer_zerop (byte_position (field)) | |
7358 | && (same_type_ignoring_top_level_qualifiers_p | |
7359 | (TREE_TYPE (field), type))) | |
7360 | { | |
97bf3311 | 7361 | return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE); |
ace3c39f | 7362 | break; |
7363 | } | |
7364 | } | |
7365 | } | |
7366 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
7367 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
7368 | { | |
7369 | tree op00 = TREE_OPERAND (sub, 0); | |
7370 | tree op01 = TREE_OPERAND (sub, 1); | |
7371 | ||
7372 | STRIP_NOPS (op00); | |
7373 | if (TREE_CODE (op00) == ADDR_EXPR) | |
7374 | { | |
7375 | tree op00type; | |
7376 | op00 = TREE_OPERAND (op00, 0); | |
7377 | op00type = TREE_TYPE (op00); | |
7378 | ||
97bf3311 | 7379 | /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ |
7380 | if (TREE_CODE (op00type) == VECTOR_TYPE | |
7381 | && (same_type_ignoring_top_level_qualifiers_p | |
7382 | (type, TREE_TYPE (op00type)))) | |
7383 | { | |
7384 | HOST_WIDE_INT offset = tree_low_cst (op01, 0); | |
7385 | tree part_width = TYPE_SIZE (type); | |
7386 | unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT; | |
7387 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; | |
7388 | tree index = bitsize_int (indexi); | |
7389 | ||
7390 | if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type)) | |
7391 | return fold_build3_loc (loc, | |
7392 | BIT_FIELD_REF, type, op00, | |
7393 | part_width, index); | |
7394 | ||
7395 | } | |
7396 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ | |
7397 | else if (TREE_CODE (op00type) == COMPLEX_TYPE | |
7398 | && (same_type_ignoring_top_level_qualifiers_p | |
7399 | (type, TREE_TYPE (op00type)))) | |
7400 | { | |
7401 | tree size = TYPE_SIZE_UNIT (type); | |
7402 | if (tree_int_cst_equal (size, op01)) | |
7403 | return fold_build1_loc (loc, IMAGPART_EXPR, type, op00); | |
7404 | } | |
7405 | /* ((foo *)&fooarray)[1] => fooarray[1] */ | |
7406 | else if (TREE_CODE (op00type) == ARRAY_TYPE | |
7407 | && (same_type_ignoring_top_level_qualifiers_p | |
7408 | (type, TREE_TYPE (op00type)))) | |
7409 | { | |
7410 | tree type_domain = TYPE_DOMAIN (op00type); | |
7411 | tree min_val = size_zero_node; | |
7412 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
7413 | min_val = TYPE_MIN_VALUE (type_domain); | |
7414 | op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01, | |
7415 | TYPE_SIZE_UNIT (type)); | |
7416 | op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val); | |
7417 | return build4_loc (loc, ARRAY_REF, type, op00, op01, | |
7418 | NULL_TREE, NULL_TREE); | |
7419 | } | |
ace3c39f | 7420 | /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */ |
97bf3311 | 7421 | else if (RECORD_OR_UNION_TYPE_P (op00type)) |
ace3c39f | 7422 | { |
7423 | tree field = TYPE_FIELDS (op00type); | |
7424 | for (; field; field = DECL_CHAIN (field)) | |
7425 | if (TREE_CODE (field) == FIELD_DECL | |
7426 | && tree_int_cst_equal (byte_position (field), op01) | |
7427 | && (same_type_ignoring_top_level_qualifiers_p | |
7428 | (TREE_TYPE (field), type))) | |
7429 | { | |
97bf3311 | 7430 | return fold_build3 (COMPONENT_REF, type, op00, |
ace3c39f | 7431 | field, NULL_TREE); |
7432 | break; | |
7433 | } | |
7434 | } | |
7435 | } | |
7436 | } | |
97bf3311 | 7437 | /* *(foo *)fooarrptreturn> (*fooarrptr)[0] */ |
7438 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
7439 | && (same_type_ignoring_top_level_qualifiers_p | |
7440 | (type, TREE_TYPE (TREE_TYPE (subtype))))) | |
7441 | { | |
7442 | tree type_domain; | |
7443 | tree min_val = size_zero_node; | |
7444 | sub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL); | |
7445 | if (!sub) | |
7446 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
7447 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
7448 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
7449 | min_val = TYPE_MIN_VALUE (type_domain); | |
7450 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
7451 | NULL_TREE); | |
7452 | } | |
ace3c39f | 7453 | |
97bf3311 | 7454 | return NULL_TREE; |
7455 | } | |
7456 | ||
7457 | static tree | |
7458 | cxx_eval_indirect_ref (const constexpr_call *call, tree t, | |
7459 | bool allow_non_constant, bool addr, | |
4326a074 | 7460 | bool *non_constant_p, bool *overflow_p) |
97bf3311 | 7461 | { |
7462 | tree orig_op0 = TREE_OPERAND (t, 0); | |
7463 | tree op0 = cxx_eval_constant_expression (call, orig_op0, allow_non_constant, | |
4326a074 | 7464 | /*addr*/false, non_constant_p, overflow_p); |
97bf3311 | 7465 | bool empty_base = false; |
7466 | tree r; | |
7467 | ||
7468 | /* Don't VERIFY_CONSTANT here. */ | |
7469 | if (*non_constant_p) | |
7470 | return t; | |
7471 | ||
7472 | r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0, | |
7473 | &empty_base); | |
7474 | ||
7475 | if (r) | |
ace3c39f | 7476 | r = cxx_eval_constant_expression (call, r, allow_non_constant, |
4326a074 | 7477 | addr, non_constant_p, overflow_p); |
97bf3311 | 7478 | else |
ace3c39f | 7479 | { |
97bf3311 | 7480 | tree sub = op0; |
7481 | STRIP_NOPS (sub); | |
0cb494eb | 7482 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR) |
97bf3311 | 7483 | { |
0cb494eb | 7484 | sub = TREE_OPERAND (sub, 0); |
7485 | STRIP_NOPS (sub); | |
7486 | } | |
7487 | if (TREE_CODE (sub) == ADDR_EXPR) | |
7488 | { | |
7489 | /* We couldn't fold to a constant value. Make sure it's not | |
7490 | something we should have been able to fold. */ | |
97bf3311 | 7491 | gcc_assert (!same_type_ignoring_top_level_qualifiers_p |
7492 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); | |
8e1cdf27 | 7493 | /* DR 1188 says we don't have to deal with this. */ |
97bf3311 | 7494 | if (!allow_non_constant) |
7495 | error ("accessing value of %qE through a %qT glvalue in a " | |
7496 | "constant expression", build_fold_indirect_ref (sub), | |
7497 | TREE_TYPE (t)); | |
7498 | *non_constant_p = true; | |
7499 | return t; | |
7500 | } | |
ace3c39f | 7501 | } |
7502 | ||
7503 | /* If we're pulling out the value of an empty base, make sure | |
7504 | that the whole object is constant and then return an empty | |
7505 | CONSTRUCTOR. */ | |
7506 | if (empty_base) | |
7507 | { | |
7508 | VERIFY_CONSTANT (r); | |
7509 | r = build_constructor (TREE_TYPE (t), NULL); | |
7510 | TREE_CONSTANT (r) = true; | |
7511 | } | |
7512 | ||
97bf3311 | 7513 | if (r == NULL_TREE) |
f6f1f701 | 7514 | { |
7515 | if (!addr) | |
7516 | VERIFY_CONSTANT (t); | |
7517 | return t; | |
7518 | } | |
ace3c39f | 7519 | return r; |
7520 | } | |
7521 | ||
b9c67e42 | 7522 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. |
7523 | Shared between potential_constant_expression and | |
7524 | cxx_eval_constant_expression. */ | |
7525 | ||
7526 | static void | |
7527 | non_const_var_error (tree r) | |
7528 | { | |
7529 | tree type = TREE_TYPE (r); | |
7530 | error ("the value of %qD is not usable in a constant " | |
7531 | "expression", r); | |
49147f1a | 7532 | /* Avoid error cascade. */ |
7533 | if (DECL_INITIAL (r) == error_mark_node) | |
7534 | return; | |
b9c67e42 | 7535 | if (DECL_DECLARED_CONSTEXPR_P (r)) |
7536 | inform (DECL_SOURCE_LOCATION (r), | |
7537 | "%qD used in its own initializer", r); | |
7538 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
7539 | { | |
7540 | if (!CP_TYPE_CONST_P (type)) | |
7541 | inform (DECL_SOURCE_LOCATION (r), | |
7542 | "%q#D is not const", r); | |
7543 | else if (CP_TYPE_VOLATILE_P (type)) | |
7544 | inform (DECL_SOURCE_LOCATION (r), | |
7545 | "%q#D is volatile", r); | |
46d73a5c | 7546 | else if (!DECL_INITIAL (r) |
7547 | || !TREE_CONSTANT (DECL_INITIAL (r))) | |
b9c67e42 | 7548 | inform (DECL_SOURCE_LOCATION (r), |
7549 | "%qD was not initialized with a constant " | |
7550 | "expression", r); | |
7551 | else | |
7552 | gcc_unreachable (); | |
7553 | } | |
7554 | else | |
7555 | { | |
7556 | if (cxx_dialect >= cxx0x && !DECL_DECLARED_CONSTEXPR_P (r)) | |
7557 | inform (DECL_SOURCE_LOCATION (r), | |
7558 | "%qD was not declared %<constexpr%>", r); | |
7559 | else | |
7560 | inform (DECL_SOURCE_LOCATION (r), | |
7561 | "%qD does not have integral or enumeration type", | |
7562 | r); | |
7563 | } | |
7564 | } | |
7565 | ||
4cdb0eae | 7566 | /* Evaluate VEC_PERM_EXPR (v1, v2, mask). */ |
7567 | static tree | |
7568 | cxx_eval_vec_perm_expr (const constexpr_call *call, tree t, | |
7569 | bool allow_non_constant, bool addr, | |
4326a074 | 7570 | bool *non_constant_p, bool *overflow_p) |
4cdb0eae | 7571 | { |
7572 | int i; | |
7573 | tree args[3]; | |
7574 | tree val; | |
7575 | tree elttype = TREE_TYPE (t); | |
7576 | ||
7577 | for (i = 0; i < 3; i++) | |
7578 | { | |
7579 | args[i] = cxx_eval_constant_expression (call, TREE_OPERAND (t, i), | |
7580 | allow_non_constant, addr, | |
4326a074 | 7581 | non_constant_p, overflow_p); |
4cdb0eae | 7582 | if (*non_constant_p) |
7583 | goto fail; | |
7584 | } | |
7585 | ||
7586 | gcc_assert (TREE_CODE (TREE_TYPE (args[0])) == VECTOR_TYPE); | |
7587 | gcc_assert (TREE_CODE (TREE_TYPE (args[1])) == VECTOR_TYPE); | |
7588 | gcc_assert (TREE_CODE (TREE_TYPE (args[2])) == VECTOR_TYPE); | |
7589 | ||
7590 | val = fold_ternary_loc (EXPR_LOCATION (t), VEC_PERM_EXPR, elttype, | |
7591 | args[0], args[1], args[2]); | |
7592 | if (val != NULL_TREE) | |
7593 | return val; | |
7594 | ||
7595 | fail: | |
7596 | return t; | |
7597 | } | |
7598 | ||
ace3c39f | 7599 | /* Attempt to reduce the expression T to a constant value. |
7600 | On failure, issue diagnostic and return error_mark_node. */ | |
7601 | /* FIXME unify with c_fully_fold */ | |
7602 | ||
7603 | static tree | |
7604 | cxx_eval_constant_expression (const constexpr_call *call, tree t, | |
7605 | bool allow_non_constant, bool addr, | |
4326a074 | 7606 | bool *non_constant_p, bool *overflow_p) |
ace3c39f | 7607 | { |
7608 | tree r = t; | |
7609 | ||
7610 | if (t == error_mark_node) | |
7611 | { | |
7612 | *non_constant_p = true; | |
7613 | return t; | |
7614 | } | |
7615 | if (CONSTANT_CLASS_P (t)) | |
7616 | { | |
7617 | if (TREE_CODE (t) == PTRMEM_CST) | |
7618 | t = cplus_expand_constant (t); | |
4326a074 | 7619 | else if (TREE_OVERFLOW (t) && (!flag_permissive || allow_non_constant)) |
7620 | *overflow_p = true; | |
ace3c39f | 7621 | return t; |
7622 | } | |
7623 | if (TREE_CODE (t) != NOP_EXPR | |
7624 | && reduced_constant_expression_p (t)) | |
7625 | return fold (t); | |
7626 | ||
7627 | switch (TREE_CODE (t)) | |
7628 | { | |
7629 | case VAR_DECL: | |
7630 | if (addr) | |
7631 | return t; | |
7632 | /* else fall through. */ | |
7633 | case CONST_DECL: | |
7634 | r = integral_constant_value (t); | |
7635 | if (TREE_CODE (r) == TARGET_EXPR | |
7636 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
7637 | r = TARGET_EXPR_INITIAL (r); | |
7638 | if (DECL_P (r)) | |
7639 | { | |
7640 | if (!allow_non_constant) | |
b9c67e42 | 7641 | non_const_var_error (r); |
ace3c39f | 7642 | *non_constant_p = true; |
7643 | } | |
7644 | break; | |
7645 | ||
7646 | case FUNCTION_DECL: | |
07b53544 | 7647 | case TEMPLATE_DECL: |
ace3c39f | 7648 | case LABEL_DECL: |
7649 | return t; | |
7650 | ||
7651 | case PARM_DECL: | |
7652 | if (call && DECL_CONTEXT (t) == call->fundef->decl) | |
bbab4abf | 7653 | { |
7654 | if (DECL_ARTIFICIAL (t) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t))) | |
7655 | { | |
7656 | if (!allow_non_constant) | |
7657 | sorry ("use of the value of the object being constructed " | |
7658 | "in a constant expression"); | |
7659 | *non_constant_p = true; | |
7660 | } | |
7661 | else | |
7662 | r = lookup_parameter_binding (call, t); | |
7663 | } | |
ace3c39f | 7664 | else if (addr) |
7665 | /* Defer in case this is only used for its type. */; | |
7666 | else | |
7667 | { | |
7668 | if (!allow_non_constant) | |
7669 | error ("%qE is not a constant expression", t); | |
7670 | *non_constant_p = true; | |
7671 | } | |
7672 | break; | |
7673 | ||
7674 | case CALL_EXPR: | |
7675 | case AGGR_INIT_EXPR: | |
7676 | r = cxx_eval_call_expression (call, t, allow_non_constant, addr, | |
4326a074 | 7677 | non_constant_p, overflow_p); |
ace3c39f | 7678 | break; |
7679 | ||
7680 | case TARGET_EXPR: | |
d1839b38 | 7681 | if (!literal_type_p (TREE_TYPE (t))) |
e3260d66 | 7682 | { |
7683 | if (!allow_non_constant) | |
262c8920 | 7684 | { |
7685 | error ("temporary of non-literal type %qT in a " | |
7686 | "constant expression", TREE_TYPE (t)); | |
7687 | explain_non_literal_class (TREE_TYPE (t)); | |
7688 | } | |
e3260d66 | 7689 | *non_constant_p = true; |
7690 | break; | |
7691 | } | |
7692 | /* else fall through. */ | |
ace3c39f | 7693 | case INIT_EXPR: |
7694 | /* Pass false for 'addr' because these codes indicate | |
7695 | initialization of a temporary. */ | |
7696 | r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1), | |
7697 | allow_non_constant, false, | |
4326a074 | 7698 | non_constant_p, overflow_p); |
ace3c39f | 7699 | if (!*non_constant_p) |
7700 | /* Adjust the type of the result to the type of the temporary. */ | |
7701 | r = adjust_temp_type (TREE_TYPE (t), r); | |
7702 | break; | |
7703 | ||
7704 | case SCOPE_REF: | |
7705 | r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 1), | |
7706 | allow_non_constant, addr, | |
4326a074 | 7707 | non_constant_p, overflow_p); |
ace3c39f | 7708 | break; |
7709 | ||
7710 | case RETURN_EXPR: | |
7711 | case NON_LVALUE_EXPR: | |
7712 | case TRY_CATCH_EXPR: | |
7713 | case CLEANUP_POINT_EXPR: | |
7714 | case MUST_NOT_THROW_EXPR: | |
7715 | case SAVE_EXPR: | |
7716 | r = cxx_eval_constant_expression (call, TREE_OPERAND (t, 0), | |
7717 | allow_non_constant, addr, | |
4326a074 | 7718 | non_constant_p, overflow_p); |
ace3c39f | 7719 | break; |
7720 | ||
7721 | /* These differ from cxx_eval_unary_expression in that this doesn't | |
7722 | check for a constant operand or result; an address can be | |
7723 | constant without its operand being, and vice versa. */ | |
7724 | case INDIRECT_REF: | |
7725 | r = cxx_eval_indirect_ref (call, t, allow_non_constant, addr, | |
4326a074 | 7726 | non_constant_p, overflow_p); |
ace3c39f | 7727 | break; |
7728 | ||
7729 | case ADDR_EXPR: | |
7730 | { | |
7731 | tree oldop = TREE_OPERAND (t, 0); | |
7732 | tree op = cxx_eval_constant_expression (call, oldop, | |
7733 | allow_non_constant, | |
7734 | /*addr*/true, | |
4326a074 | 7735 | non_constant_p, overflow_p); |
ace3c39f | 7736 | /* Don't VERIFY_CONSTANT here. */ |
7737 | if (*non_constant_p) | |
7738 | return t; | |
7739 | /* This function does more aggressive folding than fold itself. */ | |
7740 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
7741 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7742 | return t; | |
7743 | break; | |
7744 | } | |
7745 | ||
7746 | case REALPART_EXPR: | |
7747 | case IMAGPART_EXPR: | |
7748 | case CONJ_EXPR: | |
7749 | case FIX_TRUNC_EXPR: | |
7750 | case FLOAT_EXPR: | |
7751 | case NEGATE_EXPR: | |
7752 | case ABS_EXPR: | |
7753 | case BIT_NOT_EXPR: | |
7754 | case TRUTH_NOT_EXPR: | |
7755 | case FIXED_CONVERT_EXPR: | |
7756 | r = cxx_eval_unary_expression (call, t, allow_non_constant, addr, | |
4326a074 | 7757 | non_constant_p, overflow_p); |
ace3c39f | 7758 | break; |
7759 | ||
121296ee | 7760 | case SIZEOF_EXPR: |
7761 | if (SIZEOF_EXPR_TYPE_P (t)) | |
7762 | r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)), | |
7763 | SIZEOF_EXPR, false); | |
7764 | else if (TYPE_P (TREE_OPERAND (t, 0))) | |
7765 | r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR, | |
7766 | false); | |
7767 | else | |
7768 | r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR, | |
7769 | false); | |
7770 | if (r == error_mark_node) | |
7771 | r = size_one_node; | |
7772 | VERIFY_CONSTANT (r); | |
7773 | break; | |
7774 | ||
ace3c39f | 7775 | case COMPOUND_EXPR: |
7776 | { | |
7777 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
7778 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
7779 | introduced by build_call_a. */ | |
7780 | tree op0 = TREE_OPERAND (t, 0); | |
7781 | tree op1 = TREE_OPERAND (t, 1); | |
7782 | STRIP_NOPS (op1); | |
7783 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
7784 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
7785 | r = cxx_eval_constant_expression (call, op0, allow_non_constant, | |
4326a074 | 7786 | addr, non_constant_p, overflow_p); |
ace3c39f | 7787 | else |
c93219d0 | 7788 | { |
7789 | /* Check that the LHS is constant and then discard it. */ | |
7790 | cxx_eval_constant_expression (call, op0, allow_non_constant, | |
4326a074 | 7791 | false, non_constant_p, overflow_p); |
fa0582d8 | 7792 | op1 = TREE_OPERAND (t, 1); |
c93219d0 | 7793 | r = cxx_eval_constant_expression (call, op1, allow_non_constant, |
4326a074 | 7794 | addr, non_constant_p, overflow_p); |
c93219d0 | 7795 | } |
ace3c39f | 7796 | } |
7797 | break; | |
7798 | ||
7799 | case POINTER_PLUS_EXPR: | |
7800 | case PLUS_EXPR: | |
7801 | case MINUS_EXPR: | |
7802 | case MULT_EXPR: | |
7803 | case TRUNC_DIV_EXPR: | |
7804 | case CEIL_DIV_EXPR: | |
7805 | case FLOOR_DIV_EXPR: | |
7806 | case ROUND_DIV_EXPR: | |
7807 | case TRUNC_MOD_EXPR: | |
7808 | case CEIL_MOD_EXPR: | |
7809 | case ROUND_MOD_EXPR: | |
7810 | case RDIV_EXPR: | |
7811 | case EXACT_DIV_EXPR: | |
7812 | case MIN_EXPR: | |
7813 | case MAX_EXPR: | |
7814 | case LSHIFT_EXPR: | |
7815 | case RSHIFT_EXPR: | |
7816 | case LROTATE_EXPR: | |
7817 | case RROTATE_EXPR: | |
7818 | case BIT_IOR_EXPR: | |
7819 | case BIT_XOR_EXPR: | |
7820 | case BIT_AND_EXPR: | |
7821 | case TRUTH_XOR_EXPR: | |
7822 | case LT_EXPR: | |
7823 | case LE_EXPR: | |
7824 | case GT_EXPR: | |
7825 | case GE_EXPR: | |
7826 | case EQ_EXPR: | |
7827 | case NE_EXPR: | |
7828 | case UNORDERED_EXPR: | |
7829 | case ORDERED_EXPR: | |
7830 | case UNLT_EXPR: | |
7831 | case UNLE_EXPR: | |
7832 | case UNGT_EXPR: | |
7833 | case UNGE_EXPR: | |
7834 | case UNEQ_EXPR: | |
7835 | case RANGE_EXPR: | |
7836 | case COMPLEX_EXPR: | |
ace3c39f | 7837 | r = cxx_eval_binary_expression (call, t, allow_non_constant, addr, |
4326a074 | 7838 | non_constant_p, overflow_p); |
ace3c39f | 7839 | break; |
7840 | ||
7841 | /* fold can introduce non-IF versions of these; still treat them as | |
7842 | short-circuiting. */ | |
7843 | case TRUTH_AND_EXPR: | |
7844 | case TRUTH_ANDIF_EXPR: | |
7845 | r = cxx_eval_logical_expression (call, t, boolean_false_node, | |
7846 | boolean_true_node, | |
7847 | allow_non_constant, addr, | |
4326a074 | 7848 | non_constant_p, overflow_p); |
ace3c39f | 7849 | break; |
7850 | ||
7851 | case TRUTH_OR_EXPR: | |
7852 | case TRUTH_ORIF_EXPR: | |
7853 | r = cxx_eval_logical_expression (call, t, boolean_true_node, | |
7854 | boolean_false_node, | |
7855 | allow_non_constant, addr, | |
4326a074 | 7856 | non_constant_p, overflow_p); |
ace3c39f | 7857 | break; |
7858 | ||
7859 | case ARRAY_REF: | |
7860 | r = cxx_eval_array_reference (call, t, allow_non_constant, addr, | |
4326a074 | 7861 | non_constant_p, overflow_p); |
ace3c39f | 7862 | break; |
7863 | ||
7864 | case COMPONENT_REF: | |
7865 | r = cxx_eval_component_reference (call, t, allow_non_constant, addr, | |
4326a074 | 7866 | non_constant_p, overflow_p); |
ace3c39f | 7867 | break; |
7868 | ||
fa704bb1 | 7869 | case BIT_FIELD_REF: |
7870 | r = cxx_eval_bit_field_ref (call, t, allow_non_constant, addr, | |
4326a074 | 7871 | non_constant_p, overflow_p); |
fa704bb1 | 7872 | break; |
7873 | ||
ace3c39f | 7874 | case COND_EXPR: |
7875 | case VEC_COND_EXPR: | |
7876 | r = cxx_eval_conditional_expression (call, t, allow_non_constant, addr, | |
4326a074 | 7877 | non_constant_p, overflow_p); |
ace3c39f | 7878 | break; |
7879 | ||
7880 | case CONSTRUCTOR: | |
7881 | r = cxx_eval_bare_aggregate (call, t, allow_non_constant, addr, | |
4326a074 | 7882 | non_constant_p, overflow_p); |
ace3c39f | 7883 | break; |
7884 | ||
7885 | case VEC_INIT_EXPR: | |
7886 | /* We can get this in a defaulted constructor for a class with a | |
7887 | non-static data member of array type. Either the initializer will | |
7888 | be NULL, meaning default-initialization, or it will be an lvalue | |
7889 | or xvalue of the same type, meaning direct-initialization from the | |
7890 | corresponding member. */ | |
7891 | r = cxx_eval_vec_init (call, t, allow_non_constant, addr, | |
4326a074 | 7892 | non_constant_p, overflow_p); |
ace3c39f | 7893 | break; |
7894 | ||
4cdb0eae | 7895 | case VEC_PERM_EXPR: |
7896 | r = cxx_eval_vec_perm_expr (call, t, allow_non_constant, addr, | |
4326a074 | 7897 | non_constant_p, overflow_p); |
4cdb0eae | 7898 | break; |
7899 | ||
ace3c39f | 7900 | case CONVERT_EXPR: |
7901 | case VIEW_CONVERT_EXPR: | |
7902 | case NOP_EXPR: | |
7903 | { | |
7904 | tree oldop = TREE_OPERAND (t, 0); | |
e80c5f2b | 7905 | tree op = cxx_eval_constant_expression (call, oldop, |
7906 | allow_non_constant, addr, | |
4326a074 | 7907 | non_constant_p, overflow_p); |
ace3c39f | 7908 | if (*non_constant_p) |
7909 | return t; | |
7910 | if (op == oldop) | |
7911 | /* We didn't fold at the top so we could check for ptr-int | |
7912 | conversion. */ | |
7913 | return fold (t); | |
e80c5f2b | 7914 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op); |
ca4646eb | 7915 | /* Conversion of an out-of-range value has implementation-defined |
7916 | behavior; the language considers it different from arithmetic | |
7917 | overflow, which is undefined. */ | |
7918 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
7919 | TREE_OVERFLOW (r) = false; | |
ace3c39f | 7920 | } |
7921 | break; | |
7922 | ||
7923 | case EMPTY_CLASS_EXPR: | |
7924 | /* This is good enough for a function argument that might not get | |
7925 | used, and they can't do anything with it, so just return it. */ | |
7926 | return t; | |
7927 | ||
7928 | case LAMBDA_EXPR: | |
ace3c39f | 7929 | case PREINCREMENT_EXPR: |
7930 | case POSTINCREMENT_EXPR: | |
7931 | case PREDECREMENT_EXPR: | |
7932 | case POSTDECREMENT_EXPR: | |
7933 | case NEW_EXPR: | |
7934 | case VEC_NEW_EXPR: | |
7935 | case DELETE_EXPR: | |
7936 | case VEC_DELETE_EXPR: | |
7937 | case THROW_EXPR: | |
7938 | case MODIFY_EXPR: | |
7939 | case MODOP_EXPR: | |
7940 | /* GCC internal stuff. */ | |
7941 | case VA_ARG_EXPR: | |
7942 | case OBJ_TYPE_REF: | |
7943 | case WITH_CLEANUP_EXPR: | |
7944 | case STATEMENT_LIST: | |
7945 | case BIND_EXPR: | |
7946 | case NON_DEPENDENT_EXPR: | |
7947 | case BASELINK: | |
7948 | case EXPR_STMT: | |
b2188d7e | 7949 | case OFFSET_REF: |
ace3c39f | 7950 | if (!allow_non_constant) |
7951 | error_at (EXPR_LOC_OR_HERE (t), | |
7952 | "expression %qE is not a constant-expression", t); | |
7953 | *non_constant_p = true; | |
7954 | break; | |
7955 | ||
7956 | default: | |
7957 | internal_error ("unexpected expression %qE of kind %s", t, | |
7958 | tree_code_name[TREE_CODE (t)]); | |
7959 | *non_constant_p = true; | |
7960 | break; | |
7961 | } | |
7962 | ||
7963 | if (r == error_mark_node) | |
7964 | *non_constant_p = true; | |
7965 | ||
7966 | if (*non_constant_p) | |
7967 | return t; | |
7968 | else | |
7969 | return r; | |
7970 | } | |
7971 | ||
7972 | static tree | |
7973 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant) | |
7974 | { | |
7975 | bool non_constant_p = false; | |
4326a074 | 7976 | bool overflow_p = false; |
ace3c39f | 7977 | tree r = cxx_eval_constant_expression (NULL, t, allow_non_constant, |
4326a074 | 7978 | false, &non_constant_p, &overflow_p); |
ace3c39f | 7979 | |
4326a074 | 7980 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); |
ace3c39f | 7981 | |
f602376c | 7982 | if (TREE_CODE (t) != CONSTRUCTOR |
7983 | && cp_has_mutable_p (TREE_TYPE (t))) | |
7984 | { | |
7985 | /* We allow a mutable type if the original expression was a | |
7986 | CONSTRUCTOR so that we can do aggregate initialization of | |
7987 | constexpr variables. */ | |
7988 | if (!allow_non_constant) | |
7989 | error ("%qT cannot be the type of a complete constant expression " | |
7990 | "because it has mutable sub-objects", TREE_TYPE (t)); | |
7991 | non_constant_p = true; | |
7992 | } | |
7993 | ||
74051031 | 7994 | /* Technically we should check this for all subexpressions, but that |
7995 | runs into problems with our internal representation of pointer | |
7996 | subtraction and the 5.19 rules are still in flux. */ | |
7997 | if (CONVERT_EXPR_CODE_P (TREE_CODE (r)) | |
7998 | && ARITHMETIC_TYPE_P (TREE_TYPE (r)) | |
7999 | && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR) | |
8000 | { | |
8001 | if (!allow_non_constant) | |
8002 | error ("conversion from pointer type %qT " | |
8003 | "to arithmetic type %qT in a constant-expression", | |
8004 | TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r)); | |
8005 | non_constant_p = true; | |
8006 | } | |
8007 | ||
4326a074 | 8008 | if (!non_constant_p && overflow_p) |
8009 | non_constant_p = true; | |
8010 | ||
ace3c39f | 8011 | if (non_constant_p && !allow_non_constant) |
8012 | return error_mark_node; | |
4326a074 | 8013 | else if (non_constant_p && TREE_CONSTANT (r)) |
ace3c39f | 8014 | { |
8015 | /* This isn't actually constant, so unset TREE_CONSTANT. */ | |
4326a074 | 8016 | if (EXPR_P (r)) |
8017 | r = copy_node (r); | |
8018 | else if (TREE_CODE (r) == CONSTRUCTOR) | |
8019 | r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); | |
ace3c39f | 8020 | else |
4326a074 | 8021 | r = build_nop (TREE_TYPE (r), r); |
ace3c39f | 8022 | TREE_CONSTANT (r) = false; |
ace3c39f | 8023 | } |
8024 | else if (non_constant_p || r == t) | |
8025 | return t; | |
4326a074 | 8026 | |
8027 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) | |
ace3c39f | 8028 | { |
8029 | if (TREE_CODE (t) == TARGET_EXPR | |
8030 | && TARGET_EXPR_INITIAL (t) == r) | |
8031 | return t; | |
8032 | else | |
8033 | { | |
8034 | r = get_target_expr (r); | |
8035 | TREE_CONSTANT (r) = true; | |
8036 | return r; | |
8037 | } | |
8038 | } | |
8039 | else | |
8040 | return r; | |
8041 | } | |
8042 | ||
ce984e5e | 8043 | /* Returns true if T is a valid subexpression of a constant expression, |
8044 | even if it isn't itself a constant expression. */ | |
8045 | ||
8046 | bool | |
8047 | is_sub_constant_expr (tree t) | |
8048 | { | |
8049 | bool non_constant_p = false; | |
4326a074 | 8050 | bool overflow_p = false; |
8051 | cxx_eval_constant_expression (NULL, t, true, false, &non_constant_p, | |
8052 | &overflow_p); | |
8053 | return !non_constant_p && !overflow_p; | |
ce984e5e | 8054 | } |
8055 | ||
ace3c39f | 8056 | /* If T represents a constant expression returns its reduced value. |
8057 | Otherwise return error_mark_node. If T is dependent, then | |
8058 | return NULL. */ | |
8059 | ||
8060 | tree | |
8061 | cxx_constant_value (tree t) | |
8062 | { | |
8063 | return cxx_eval_outermost_constant_expr (t, false); | |
8064 | } | |
8065 | ||
8066 | /* If T is a constant expression, returns its reduced value. | |
8067 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
8068 | Otherwise, returns a version of T without TREE_CONSTANT. */ | |
8069 | ||
8070 | tree | |
8071 | maybe_constant_value (tree t) | |
8072 | { | |
8073 | tree r; | |
8074 | ||
8075 | if (type_dependent_expression_p (t) | |
cfa61f84 | 8076 | || type_unknown_p (t) |
8e2135fb | 8077 | || BRACE_ENCLOSED_INITIALIZER_P (t) |
bff898fb | 8078 | || !potential_constant_expression (t) |
ace3c39f | 8079 | || value_dependent_expression_p (t)) |
46d73a5c | 8080 | { |
8081 | if (TREE_OVERFLOW_P (t)) | |
8082 | { | |
8083 | t = build_nop (TREE_TYPE (t), t); | |
8084 | TREE_CONSTANT (t) = false; | |
8085 | } | |
8086 | return t; | |
8087 | } | |
ace3c39f | 8088 | |
8089 | r = cxx_eval_outermost_constant_expr (t, true); | |
8090 | #ifdef ENABLE_CHECKING | |
8091 | /* cp_tree_equal looks through NOPs, so allow them. */ | |
8092 | gcc_assert (r == t | |
8093 | || CONVERT_EXPR_P (t) | |
8094 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
8095 | || !cp_tree_equal (r, t)); | |
8096 | #endif | |
8097 | return r; | |
8098 | } | |
8099 | ||
8100 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather | |
8101 | than wrapped in a TARGET_EXPR. */ | |
8102 | ||
8103 | tree | |
8104 | maybe_constant_init (tree t) | |
8105 | { | |
8106 | t = maybe_constant_value (t); | |
8107 | if (TREE_CODE (t) == TARGET_EXPR) | |
8108 | { | |
8109 | tree init = TARGET_EXPR_INITIAL (t); | |
4326a074 | 8110 | if (TREE_CODE (init) == CONSTRUCTOR) |
ace3c39f | 8111 | t = init; |
8112 | } | |
8113 | return t; | |
8114 | } | |
8115 | ||
bff898fb | 8116 | #if 0 |
8117 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
4905dfb6 | 8118 | /* Return true if the object referred to by REF has automatic or thread |
8119 | local storage. */ | |
17814aca | 8120 | |
4905dfb6 | 8121 | enum { ck_ok, ck_bad, ck_unknown }; |
8122 | static int | |
8123 | check_automatic_or_tls (tree ref) | |
8124 | { | |
8125 | enum machine_mode mode; | |
8126 | HOST_WIDE_INT bitsize, bitpos; | |
8127 | tree offset; | |
8128 | int volatilep = 0, unsignedp = 0; | |
8129 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
8130 | &mode, &unsignedp, &volatilep, false); | |
8131 | duration_kind dk; | |
8132 | ||
8133 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
8134 | and this isn't a constant expression anyway. */ | |
8135 | if (!DECL_P (decl)) | |
8136 | return ck_unknown; | |
8137 | dk = decl_storage_duration (decl); | |
8138 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
8139 | } | |
bff898fb | 8140 | #endif |
4905dfb6 | 8141 | |
bff898fb | 8142 | /* Return true if T denotes a potentially constant expression. Issue |
8143 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
8144 | an lvalue-rvalue conversion is implied. | |
4905dfb6 | 8145 | |
bff898fb | 8146 | C++0x [expr.const] used to say |
4905dfb6 | 8147 | |
8148 | 6 An expression is a potential constant expression if it is | |
8149 | a constant expression where all occurences of function | |
8150 | parameters are replaced by arbitrary constant expressions | |
8151 | of the appropriate type. | |
8152 | ||
8153 | 2 A conditional expression is a constant expression unless it | |
8154 | involves one of the following as a potentially evaluated | |
8155 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
8156 | logical OR (5.15), and conditional (5.16) operations that are | |
8157 | not evaluated are not considered. */ | |
8158 | ||
bff898fb | 8159 | static bool |
8160 | potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) | |
4905dfb6 | 8161 | { |
bff898fb | 8162 | enum { any = false, rval = true }; |
4905dfb6 | 8163 | int i; |
8164 | tree tmp; | |
bff898fb | 8165 | |
4905dfb6 | 8166 | if (t == error_mark_node) |
8167 | return false; | |
bff898fb | 8168 | if (t == NULL_TREE) |
8169 | return true; | |
4905dfb6 | 8170 | if (TREE_THIS_VOLATILE (t)) |
8171 | { | |
8172 | if (flags & tf_error) | |
8173 | error ("expression %qE has side-effects", t); | |
8174 | return false; | |
8175 | } | |
8176 | if (CONSTANT_CLASS_P (t)) | |
4326a074 | 8177 | return true; |
4905dfb6 | 8178 | |
8179 | switch (TREE_CODE (t)) | |
8180 | { | |
8181 | case FUNCTION_DECL: | |
bff898fb | 8182 | case BASELINK: |
d1d697ea | 8183 | case TEMPLATE_DECL: |
bff898fb | 8184 | case OVERLOAD: |
8185 | case TEMPLATE_ID_EXPR: | |
4905dfb6 | 8186 | case LABEL_DECL: |
8187 | case CONST_DECL: | |
bff898fb | 8188 | case SIZEOF_EXPR: |
8189 | case ALIGNOF_EXPR: | |
8190 | case OFFSETOF_EXPR: | |
8191 | case NOEXCEPT_EXPR: | |
8192 | case TEMPLATE_PARM_INDEX: | |
8193 | case TRAIT_EXPR: | |
8194 | case IDENTIFIER_NODE: | |
244db24d | 8195 | case USERDEF_LITERAL: |
27b28dc4 | 8196 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ |
8197 | case FIELD_DECL: | |
4905dfb6 | 8198 | case PARM_DECL: |
22de038c | 8199 | case USING_DECL: |
4905dfb6 | 8200 | return true; |
8201 | ||
8202 | case AGGR_INIT_EXPR: | |
8203 | case CALL_EXPR: | |
8204 | /* -- an invocation of a function other than a constexpr function | |
8205 | or a constexpr constructor. */ | |
8206 | { | |
8207 | tree fun = get_function_named_in_call (t); | |
8208 | const int nargs = call_expr_nargs (t); | |
fe80b3d3 | 8209 | i = 0; |
8210 | ||
8211 | if (is_overloaded_fn (fun)) | |
8212 | { | |
8213 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
8214 | { | |
8215 | if (builtin_valid_in_constant_expr_p (fun)) | |
8216 | return true; | |
8217 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
9cb49131 | 8218 | /* Allow any built-in function; if the expansion |
8219 | isn't constant, we'll deal with that then. */ | |
8220 | && !is_builtin_fn (fun)) | |
fe80b3d3 | 8221 | { |
8222 | if (flags & tf_error) | |
262c8920 | 8223 | { |
8224 | error_at (EXPR_LOC_OR_HERE (t), | |
8225 | "call to non-constexpr function %qD", fun); | |
8226 | explain_invalid_constexpr_fn (fun); | |
8227 | } | |
fe80b3d3 | 8228 | return false; |
8229 | } | |
8230 | /* A call to a non-static member function takes the address | |
8231 | of the object as the first argument. But in a constant | |
8232 | expression the address will be folded away, so look | |
8233 | through it now. */ | |
8234 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
8235 | && !DECL_CONSTRUCTOR_P (fun)) | |
8236 | { | |
8237 | tree x = get_nth_callarg (t, 0); | |
8238 | if (is_this_parameter (x)) | |
c24b1556 | 8239 | { |
8240 | if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x))) | |
8241 | { | |
8242 | if (flags & tf_error) | |
8243 | sorry ("calling a member function of the " | |
8244 | "object being constructed in a constant " | |
8245 | "expression"); | |
8246 | return false; | |
8247 | } | |
8248 | /* Otherwise OK. */; | |
8249 | } | |
fe80b3d3 | 8250 | else if (!potential_constant_expression_1 (x, rval, flags)) |
262c8920 | 8251 | return false; |
fe80b3d3 | 8252 | i = 1; |
8253 | } | |
8254 | } | |
8255 | else | |
8256 | fun = get_first_fn (fun); | |
8257 | /* Skip initial arguments to base constructors. */ | |
8258 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
8259 | i = num_artificial_parms_for (fun); | |
8260 | fun = DECL_ORIGIN (fun); | |
8261 | } | |
4905dfb6 | 8262 | else |
4905dfb6 | 8263 | { |
fe80b3d3 | 8264 | if (potential_constant_expression_1 (fun, rval, flags)) |
8265 | /* Might end up being a constant function pointer. */; | |
8266 | else | |
262c8920 | 8267 | return false; |
4905dfb6 | 8268 | } |
8269 | for (; i < nargs; ++i) | |
8270 | { | |
8271 | tree x = get_nth_callarg (t, i); | |
fe80b3d3 | 8272 | if (!potential_constant_expression_1 (x, rval, flags)) |
262c8920 | 8273 | return false; |
4905dfb6 | 8274 | } |
8275 | return true; | |
8276 | } | |
8277 | ||
8278 | case NON_LVALUE_EXPR: | |
8279 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
8280 | -- an lvalue of integral type that refers to a non-volatile | |
8281 | const variable or static data member initialized with | |
8282 | constant expressions, or | |
8283 | ||
8284 | -- an lvalue of literal type that refers to non-volatile | |
8285 | object defined with constexpr, or that refers to a | |
8286 | sub-object of such an object; */ | |
bff898fb | 8287 | return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, flags); |
4905dfb6 | 8288 | |
8289 | case VAR_DECL: | |
b9c67e42 | 8290 | if (want_rval && !decl_constant_var_p (t) |
8291 | && !dependent_type_p (TREE_TYPE (t))) | |
4905dfb6 | 8292 | { |
8293 | if (flags & tf_error) | |
b9c67e42 | 8294 | non_const_var_error (t); |
4905dfb6 | 8295 | return false; |
8296 | } | |
8297 | return true; | |
8298 | ||
8299 | case NOP_EXPR: | |
8300 | case CONVERT_EXPR: | |
8301 | case VIEW_CONVERT_EXPR: | |
74051031 | 8302 | /* -- a reinterpret_cast. FIXME not implemented, and this rule |
8303 | may change to something more specific to type-punning (DR 1312). */ | |
4905dfb6 | 8304 | { |
8305 | tree from = TREE_OPERAND (t, 0); | |
bff898fb | 8306 | return (potential_constant_expression_1 |
8307 | (from, TREE_CODE (t) != VIEW_CONVERT_EXPR, flags)); | |
4905dfb6 | 8308 | } |
8309 | ||
8310 | case ADDR_EXPR: | |
8311 | /* -- a unary operator & that is applied to an lvalue that | |
8312 | designates an object with thread or automatic storage | |
8313 | duration; */ | |
8314 | t = TREE_OPERAND (t, 0); | |
bff898fb | 8315 | #if 0 |
8316 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
8317 | any checking here, as we might dereference the pointer later. If | |
8318 | we remove this code, also remove check_automatic_or_tls. */ | |
4905dfb6 | 8319 | i = check_automatic_or_tls (t); |
8320 | if (i == ck_ok) | |
8321 | return true; | |
8322 | if (i == ck_bad) | |
8323 | { | |
8324 | if (flags & tf_error) | |
8325 | error ("address-of an object %qE with thread local or " | |
8326 | "automatic storage is not a constant expression", t); | |
8327 | return false; | |
8328 | } | |
bff898fb | 8329 | #endif |
8330 | return potential_constant_expression_1 (t, any, flags); | |
4905dfb6 | 8331 | |
8332 | case COMPONENT_REF: | |
8333 | case BIT_FIELD_REF: | |
bff898fb | 8334 | case ARROW_EXPR: |
8335 | case OFFSET_REF: | |
4905dfb6 | 8336 | /* -- a class member access unless its postfix-expression is |
8337 | of literal type or of pointer to literal type. */ | |
8338 | /* This test would be redundant, as it follows from the | |
8339 | postfix-expression being a potential constant expression. */ | |
bff898fb | 8340 | return potential_constant_expression_1 (TREE_OPERAND (t, 0), |
8341 | want_rval, flags); | |
8342 | ||
8343 | case EXPR_PACK_EXPANSION: | |
8344 | return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t), | |
8345 | want_rval, flags); | |
4905dfb6 | 8346 | |
8347 | case INDIRECT_REF: | |
8348 | { | |
8349 | tree x = TREE_OPERAND (t, 0); | |
8350 | STRIP_NOPS (x); | |
8351 | if (is_this_parameter (x)) | |
7c51b530 | 8352 | { |
56f83c40 | 8353 | if (want_rval && DECL_CONTEXT (x) |
8354 | && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x))) | |
7c51b530 | 8355 | { |
8356 | if (flags & tf_error) | |
7fa940a3 | 8357 | sorry ("use of the value of the object being constructed " |
8358 | "in a constant expression"); | |
7c51b530 | 8359 | return false; |
8360 | } | |
8361 | return true; | |
8362 | } | |
bff898fb | 8363 | return potential_constant_expression_1 (x, rval, flags); |
4905dfb6 | 8364 | } |
8365 | ||
8366 | case LAMBDA_EXPR: | |
8367 | case DYNAMIC_CAST_EXPR: | |
8368 | case PSEUDO_DTOR_EXPR: | |
8369 | case PREINCREMENT_EXPR: | |
8370 | case POSTINCREMENT_EXPR: | |
8371 | case PREDECREMENT_EXPR: | |
8372 | case POSTDECREMENT_EXPR: | |
8373 | case NEW_EXPR: | |
8374 | case VEC_NEW_EXPR: | |
8375 | case DELETE_EXPR: | |
8376 | case VEC_DELETE_EXPR: | |
8377 | case THROW_EXPR: | |
8378 | case MODIFY_EXPR: | |
8379 | case MODOP_EXPR: | |
8380 | /* GCC internal stuff. */ | |
8381 | case VA_ARG_EXPR: | |
8382 | case OBJ_TYPE_REF: | |
8383 | case WITH_CLEANUP_EXPR: | |
8384 | case CLEANUP_POINT_EXPR: | |
8385 | case MUST_NOT_THROW_EXPR: | |
8386 | case TRY_CATCH_EXPR: | |
8387 | case STATEMENT_LIST: | |
bff898fb | 8388 | /* Don't bother trying to define a subset of statement-expressions to |
8389 | be constant-expressions, at least for now. */ | |
8390 | case STMT_EXPR: | |
8391 | case EXPR_STMT: | |
4905dfb6 | 8392 | case BIND_EXPR: |
4c0315d0 | 8393 | case TRANSACTION_EXPR: |
cc3ffbdd | 8394 | case IF_STMT: |
8395 | case DO_STMT: | |
8396 | case FOR_STMT: | |
8397 | case WHILE_STMT: | |
4905dfb6 | 8398 | if (flags & tf_error) |
8399 | error ("expression %qE is not a constant-expression", t); | |
8400 | return false; | |
8401 | ||
8402 | case TYPEID_EXPR: | |
8403 | /* -- a typeid expression whose operand is of polymorphic | |
8404 | class type; */ | |
8405 | { | |
8406 | tree e = TREE_OPERAND (t, 0); | |
bff898fb | 8407 | if (!TYPE_P (e) && !type_dependent_expression_p (e) |
8408 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) | |
4905dfb6 | 8409 | { |
8410 | if (flags & tf_error) | |
8411 | error ("typeid-expression is not a constant expression " | |
8412 | "because %qE is of polymorphic type", e); | |
8413 | return false; | |
8414 | } | |
8415 | return true; | |
8416 | } | |
8417 | ||
8418 | case MINUS_EXPR: | |
8419 | /* -- a subtraction where both operands are pointers. */ | |
8420 | if (TYPE_PTR_P (TREE_OPERAND (t, 0)) | |
8421 | && TYPE_PTR_P (TREE_OPERAND (t, 1))) | |
8422 | { | |
8423 | if (flags & tf_error) | |
8424 | error ("difference of two pointer expressions is not " | |
8425 | "a constant expression"); | |
8426 | return false; | |
8427 | } | |
bff898fb | 8428 | want_rval = true; |
4905dfb6 | 8429 | goto binary; |
8430 | ||
8431 | case LT_EXPR: | |
8432 | case LE_EXPR: | |
8433 | case GT_EXPR: | |
8434 | case GE_EXPR: | |
8435 | case EQ_EXPR: | |
8436 | case NE_EXPR: | |
8437 | /* -- a relational or equality operator where at least | |
8438 | one of the operands is a pointer. */ | |
8439 | if (TYPE_PTR_P (TREE_OPERAND (t, 0)) | |
8440 | || TYPE_PTR_P (TREE_OPERAND (t, 1))) | |
8441 | { | |
8442 | if (flags & tf_error) | |
8443 | error ("pointer comparison expression is not a " | |
8444 | "constant expression"); | |
8445 | return false; | |
8446 | } | |
bff898fb | 8447 | want_rval = true; |
4905dfb6 | 8448 | goto binary; |
8449 | ||
cd1e23f1 | 8450 | case BIT_NOT_EXPR: |
8451 | /* A destructor. */ | |
8452 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
8453 | return true; | |
8454 | /* else fall through. */ | |
8455 | ||
4905dfb6 | 8456 | case REALPART_EXPR: |
8457 | case IMAGPART_EXPR: | |
8458 | case CONJ_EXPR: | |
8459 | case SAVE_EXPR: | |
8460 | case FIX_TRUNC_EXPR: | |
8461 | case FLOAT_EXPR: | |
8462 | case NEGATE_EXPR: | |
8463 | case ABS_EXPR: | |
4905dfb6 | 8464 | case TRUTH_NOT_EXPR: |
4905dfb6 | 8465 | case FIXED_CONVERT_EXPR: |
bff898fb | 8466 | case UNARY_PLUS_EXPR: |
8467 | return potential_constant_expression_1 (TREE_OPERAND (t, 0), rval, | |
8468 | flags); | |
8469 | ||
8470 | case CAST_EXPR: | |
8471 | case CONST_CAST_EXPR: | |
8472 | case STATIC_CAST_EXPR: | |
8473 | case REINTERPRET_CAST_EXPR: | |
91c3ace5 | 8474 | case IMPLICIT_CONV_EXPR: |
bff898fb | 8475 | return (potential_constant_expression_1 |
8476 | (TREE_OPERAND (t, 0), | |
8477 | TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE, flags)); | |
8478 | ||
8479 | case PAREN_EXPR: | |
8480 | case NON_DEPENDENT_EXPR: | |
4905dfb6 | 8481 | /* For convenience. */ |
8482 | case RETURN_EXPR: | |
bff898fb | 8483 | return potential_constant_expression_1 (TREE_OPERAND (t, 0), |
8484 | want_rval, flags); | |
8485 | ||
8486 | case SCOPE_REF: | |
8487 | return potential_constant_expression_1 (TREE_OPERAND (t, 1), | |
8488 | want_rval, flags); | |
4905dfb6 | 8489 | |
4905dfb6 | 8490 | case TARGET_EXPR: |
d1839b38 | 8491 | if (!literal_type_p (TREE_TYPE (t))) |
e3260d66 | 8492 | { |
8493 | if (flags & tf_error) | |
262c8920 | 8494 | { |
8495 | error ("temporary of non-literal type %qT in a " | |
8496 | "constant expression", TREE_TYPE (t)); | |
8497 | explain_non_literal_class (TREE_TYPE (t)); | |
8498 | } | |
e3260d66 | 8499 | return false; |
8500 | } | |
8501 | case INIT_EXPR: | |
bff898fb | 8502 | return potential_constant_expression_1 (TREE_OPERAND (t, 1), |
8503 | rval, flags); | |
4905dfb6 | 8504 | |
8505 | case CONSTRUCTOR: | |
8506 | { | |
f1f41a6c | 8507 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
4905dfb6 | 8508 | constructor_elt *ce; |
f1f41a6c | 8509 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
bff898fb | 8510 | if (!potential_constant_expression_1 (ce->value, want_rval, flags)) |
4905dfb6 | 8511 | return false; |
8512 | return true; | |
8513 | } | |
8514 | ||
8515 | case TREE_LIST: | |
8516 | { | |
8517 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
8518 | || DECL_P (TREE_PURPOSE (t))); | |
bff898fb | 8519 | if (!potential_constant_expression_1 (TREE_VALUE (t), want_rval, |
8520 | flags)) | |
4905dfb6 | 8521 | return false; |
8522 | if (TREE_CHAIN (t) == NULL_TREE) | |
8523 | return true; | |
bff898fb | 8524 | return potential_constant_expression_1 (TREE_CHAIN (t), want_rval, |
8525 | flags); | |
4905dfb6 | 8526 | } |
8527 | ||
8528 | case TRUNC_DIV_EXPR: | |
8529 | case CEIL_DIV_EXPR: | |
8530 | case FLOOR_DIV_EXPR: | |
8531 | case ROUND_DIV_EXPR: | |
8532 | case TRUNC_MOD_EXPR: | |
8533 | case CEIL_MOD_EXPR: | |
8534 | case ROUND_MOD_EXPR: | |
bff898fb | 8535 | { |
8536 | tree denom = TREE_OPERAND (t, 1); | |
8537 | /* We can't call maybe_constant_value on an expression | |
8538 | that hasn't been through fold_non_dependent_expr yet. */ | |
8539 | if (!processing_template_decl) | |
8540 | denom = maybe_constant_value (denom); | |
8541 | if (integer_zerop (denom)) | |
8542 | { | |
8543 | if (flags & tf_error) | |
8544 | error ("division by zero is not a constant-expression"); | |
8545 | return false; | |
8546 | } | |
8547 | else | |
8548 | { | |
8549 | want_rval = true; | |
8550 | goto binary; | |
8551 | } | |
8552 | } | |
4905dfb6 | 8553 | |
8554 | case COMPOUND_EXPR: | |
8555 | { | |
8556 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
8557 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
8558 | introduced by build_call_a. */ | |
8559 | tree op0 = TREE_OPERAND (t, 0); | |
8560 | tree op1 = TREE_OPERAND (t, 1); | |
8561 | STRIP_NOPS (op1); | |
8562 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
8563 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
bff898fb | 8564 | return potential_constant_expression_1 (op0, want_rval, flags); |
4905dfb6 | 8565 | else |
8566 | goto binary; | |
8567 | } | |
8568 | ||
8569 | /* If the first operand is the non-short-circuit constant, look at | |
8570 | the second operand; otherwise we only care about the first one for | |
8571 | potentiality. */ | |
8572 | case TRUTH_AND_EXPR: | |
8573 | case TRUTH_ANDIF_EXPR: | |
8574 | tmp = boolean_true_node; | |
8575 | goto truth; | |
8576 | case TRUTH_OR_EXPR: | |
8577 | case TRUTH_ORIF_EXPR: | |
8578 | tmp = boolean_false_node; | |
8579 | truth: | |
8bd5fdf8 | 8580 | { |
8581 | tree op = TREE_OPERAND (t, 0); | |
8582 | if (!potential_constant_expression_1 (op, rval, flags)) | |
8583 | return false; | |
8584 | if (!processing_template_decl) | |
8585 | op = maybe_constant_value (op); | |
8586 | if (tree_int_cst_equal (op, tmp)) | |
8587 | return potential_constant_expression_1 (TREE_OPERAND (t, 1), rval, flags); | |
8588 | else | |
8589 | return true; | |
8590 | } | |
4905dfb6 | 8591 | |
4905dfb6 | 8592 | case PLUS_EXPR: |
8593 | case MULT_EXPR: | |
8594 | case POINTER_PLUS_EXPR: | |
8595 | case RDIV_EXPR: | |
8596 | case EXACT_DIV_EXPR: | |
8597 | case MIN_EXPR: | |
8598 | case MAX_EXPR: | |
8599 | case LSHIFT_EXPR: | |
8600 | case RSHIFT_EXPR: | |
8601 | case LROTATE_EXPR: | |
8602 | case RROTATE_EXPR: | |
8603 | case BIT_IOR_EXPR: | |
8604 | case BIT_XOR_EXPR: | |
8605 | case BIT_AND_EXPR: | |
2dac7012 | 8606 | case TRUTH_XOR_EXPR: |
414b72cf | 8607 | case UNORDERED_EXPR: |
8608 | case ORDERED_EXPR: | |
4905dfb6 | 8609 | case UNLT_EXPR: |
8610 | case UNLE_EXPR: | |
8611 | case UNGT_EXPR: | |
8612 | case UNGE_EXPR: | |
8613 | case UNEQ_EXPR: | |
8d42956b | 8614 | case LTGT_EXPR: |
4905dfb6 | 8615 | case RANGE_EXPR: |
8616 | case COMPLEX_EXPR: | |
bff898fb | 8617 | want_rval = true; |
8618 | /* Fall through. */ | |
8619 | case ARRAY_REF: | |
8620 | case ARRAY_RANGE_REF: | |
8621 | case MEMBER_REF: | |
8622 | case DOTSTAR_EXPR: | |
4905dfb6 | 8623 | binary: |
8624 | for (i = 0; i < 2; ++i) | |
bff898fb | 8625 | if (!potential_constant_expression_1 (TREE_OPERAND (t, i), |
8626 | want_rval, flags)) | |
4905dfb6 | 8627 | return false; |
8628 | return true; | |
8629 | ||
6e987697 | 8630 | case FMA_EXPR: |
bf0cb017 | 8631 | case VEC_PERM_EXPR: |
6e987697 | 8632 | for (i = 0; i < 3; ++i) |
8633 | if (!potential_constant_expression_1 (TREE_OPERAND (t, i), | |
8634 | true, flags)) | |
8635 | return false; | |
8636 | return true; | |
8637 | ||
4905dfb6 | 8638 | case COND_EXPR: |
8639 | case VEC_COND_EXPR: | |
8640 | /* If the condition is a known constant, we know which of the legs we | |
8641 | care about; otherwise we only require that the condition and | |
8642 | either of the legs be potentially constant. */ | |
8643 | tmp = TREE_OPERAND (t, 0); | |
bff898fb | 8644 | if (!potential_constant_expression_1 (tmp, rval, flags)) |
4905dfb6 | 8645 | return false; |
8bd5fdf8 | 8646 | if (!processing_template_decl) |
8647 | tmp = maybe_constant_value (tmp); | |
8648 | if (integer_zerop (tmp)) | |
bff898fb | 8649 | return potential_constant_expression_1 (TREE_OPERAND (t, 2), |
8650 | want_rval, flags); | |
251c88b3 | 8651 | else if (TREE_CODE (tmp) == INTEGER_CST) |
8652 | return potential_constant_expression_1 (TREE_OPERAND (t, 1), | |
8653 | want_rval, flags); | |
4905dfb6 | 8654 | for (i = 1; i < 3; ++i) |
bff898fb | 8655 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), |
8656 | want_rval, tf_none)) | |
4905dfb6 | 8657 | return true; |
8658 | if (flags & tf_error) | |
8659 | error ("expression %qE is not a constant-expression", t); | |
8660 | return false; | |
8661 | ||
8662 | case VEC_INIT_EXPR: | |
bff898fb | 8663 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) |
8664 | return true; | |
8665 | if (flags & tf_error) | |
d5e10271 | 8666 | { |
8667 | error ("non-constant array initialization"); | |
8668 | diagnose_non_constexpr_vec_init (t); | |
8669 | } | |
bff898fb | 8670 | return false; |
4905dfb6 | 8671 | |
8672 | default: | |
121296ee | 8673 | if (objc_is_property_ref (t)) |
8674 | return false; | |
8675 | ||
98a5f45d | 8676 | sorry ("unexpected AST of kind %s", tree_code_name[TREE_CODE (t)]); |
4905dfb6 | 8677 | gcc_unreachable(); |
8678 | return false; | |
8679 | } | |
8680 | } | |
8681 | ||
bff898fb | 8682 | /* The main entry point to the above. */ |
8683 | ||
8684 | bool | |
8685 | potential_constant_expression (tree t) | |
8686 | { | |
8687 | return potential_constant_expression_1 (t, false, tf_none); | |
8688 | } | |
8689 | ||
cfa61f84 | 8690 | /* As above, but require a constant rvalue. */ |
8691 | ||
8692 | bool | |
8693 | potential_rvalue_constant_expression (tree t) | |
8694 | { | |
8695 | return potential_constant_expression_1 (t, true, tf_none); | |
8696 | } | |
8697 | ||
bff898fb | 8698 | /* Like above, but complain about non-constant expressions. */ |
8699 | ||
8700 | bool | |
8701 | require_potential_constant_expression (tree t) | |
8702 | { | |
8703 | return potential_constant_expression_1 (t, false, tf_warning_or_error); | |
8704 | } | |
cfa61f84 | 8705 | |
8706 | /* Cross product of the above. */ | |
8707 | ||
8708 | bool | |
8709 | require_potential_rvalue_constant_expression (tree t) | |
8710 | { | |
8711 | return potential_constant_expression_1 (t, true, tf_warning_or_error); | |
8712 | } | |
4905dfb6 | 8713 | \f |
a8b75081 | 8714 | /* Constructor for a lambda expression. */ |
8715 | ||
8716 | tree | |
8717 | build_lambda_expr (void) | |
8718 | { | |
8719 | tree lambda = make_node (LAMBDA_EXPR); | |
8720 | LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE; | |
8721 | LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE; | |
8722 | LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE; | |
bcc4b4ea | 8723 | LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL; |
a8b75081 | 8724 | LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE; |
8725 | LAMBDA_EXPR_MUTABLE_P (lambda) = false; | |
8726 | return lambda; | |
8727 | } | |
8728 | ||
8729 | /* Create the closure object for a LAMBDA_EXPR. */ | |
8730 | ||
8731 | tree | |
8732 | build_lambda_object (tree lambda_expr) | |
8733 | { | |
8734 | /* Build aggregate constructor call. | |
8735 | - cp_parser_braced_list | |
8736 | - cp_parser_functional_cast */ | |
f1f41a6c | 8737 | vec<constructor_elt, va_gc> *elts = NULL; |
a8b75081 | 8738 | tree node, expr, type; |
8739 | location_t saved_loc; | |
8740 | ||
8741 | if (processing_template_decl) | |
8742 | return lambda_expr; | |
8743 | ||
8744 | /* Make sure any error messages refer to the lambda-introducer. */ | |
8745 | saved_loc = input_location; | |
8746 | input_location = LAMBDA_EXPR_LOCATION (lambda_expr); | |
8747 | ||
8748 | for (node = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); | |
8749 | node; | |
8750 | node = TREE_CHAIN (node)) | |
8751 | { | |
8752 | tree field = TREE_PURPOSE (node); | |
8753 | tree val = TREE_VALUE (node); | |
8754 | ||
80346df5 | 8755 | if (field == error_mark_node) |
8756 | { | |
8757 | expr = error_mark_node; | |
8758 | goto out; | |
8759 | } | |
8760 | ||
3786e1ba | 8761 | if (DECL_P (val)) |
8762 | mark_used (val); | |
8763 | ||
a8b75081 | 8764 | /* Mere mortals can't copy arrays with aggregate initialization, so |
8765 | do some magic to make it work here. */ | |
8766 | if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE) | |
8767 | val = build_array_copy (val); | |
7da3c25a | 8768 | else if (DECL_NORMAL_CAPTURE_P (field) |
8769 | && TREE_CODE (TREE_TYPE (field)) != REFERENCE_TYPE) | |
8770 | { | |
8771 | /* "the entities that are captured by copy are used to | |
8772 | direct-initialize each corresponding non-static data | |
8773 | member of the resulting closure object." | |
8774 | ||
8775 | There's normally no way to express direct-initialization | |
8776 | from an element of a CONSTRUCTOR, so we build up a special | |
8777 | TARGET_EXPR to bypass the usual copy-initialization. */ | |
9e505437 | 8778 | val = force_rvalue (val, tf_warning_or_error); |
7da3c25a | 8779 | if (TREE_CODE (val) == TARGET_EXPR) |
8780 | TARGET_EXPR_DIRECT_INIT_P (val) = true; | |
8781 | } | |
a8b75081 | 8782 | |
8783 | CONSTRUCTOR_APPEND_ELT (elts, DECL_NAME (field), val); | |
8784 | } | |
8785 | ||
8786 | expr = build_constructor (init_list_type_node, elts); | |
8787 | CONSTRUCTOR_IS_DIRECT_INIT (expr) = 1; | |
8788 | ||
8789 | /* N2927: "[The closure] class type is not an aggregate." | |
8790 | But we briefly treat it as an aggregate to make this simpler. */ | |
35f606ed | 8791 | type = LAMBDA_EXPR_CLOSURE (lambda_expr); |
a8b75081 | 8792 | CLASSTYPE_NON_AGGREGATE (type) = 0; |
95034afb | 8793 | expr = finish_compound_literal (type, expr, tf_warning_or_error); |
a8b75081 | 8794 | CLASSTYPE_NON_AGGREGATE (type) = 1; |
8795 | ||
80346df5 | 8796 | out: |
a8b75081 | 8797 | input_location = saved_loc; |
8798 | return expr; | |
8799 | } | |
8800 | ||
8801 | /* Return an initialized RECORD_TYPE for LAMBDA. | |
8802 | LAMBDA must have its explicit captures already. */ | |
8803 | ||
8804 | tree | |
8805 | begin_lambda_type (tree lambda) | |
8806 | { | |
8807 | tree type; | |
8808 | ||
8809 | { | |
8810 | /* Unique name. This is just like an unnamed class, but we cannot use | |
8811 | make_anon_name because of certain checks against TYPE_ANONYMOUS_P. */ | |
8812 | tree name; | |
8813 | name = make_lambda_name (); | |
8814 | ||
8815 | /* Create the new RECORD_TYPE for this lambda. */ | |
8816 | type = xref_tag (/*tag_code=*/record_type, | |
8817 | name, | |
8818 | /*scope=*/ts_within_enclosing_non_class, | |
8819 | /*template_header_p=*/false); | |
8820 | } | |
8821 | ||
8822 | /* Designate it as a struct so that we can use aggregate initialization. */ | |
8823 | CLASSTYPE_DECLARED_CLASS (type) = false; | |
8824 | ||
3910cc8d | 8825 | /* Cross-reference the expression and the type. */ |
8826 | LAMBDA_EXPR_CLOSURE (lambda) = type; | |
8827 | CLASSTYPE_LAMBDA_EXPR (type) = lambda; | |
8828 | ||
a8b75081 | 8829 | /* Clear base types. */ |
8830 | xref_basetypes (type, /*bases=*/NULL_TREE); | |
8831 | ||
8832 | /* Start the class. */ | |
6935b87a | 8833 | type = begin_class_definition (type); |
69bcfc08 | 8834 | if (type == error_mark_node) |
8835 | return error_mark_node; | |
a8b75081 | 8836 | |
a8b75081 | 8837 | return type; |
8838 | } | |
8839 | ||
8840 | /* Returns the type to use for the return type of the operator() of a | |
8841 | closure class. */ | |
8842 | ||
8843 | tree | |
8844 | lambda_return_type (tree expr) | |
8845 | { | |
86359a65 | 8846 | if (expr == NULL_TREE) |
8847 | return void_type_node; | |
a75d43a4 | 8848 | if (type_unknown_p (expr) |
8849 | || BRACE_ENCLOSED_INITIALIZER_P (expr)) | |
cca512c2 | 8850 | { |
a75d43a4 | 8851 | cxx_incomplete_type_error (expr, TREE_TYPE (expr)); |
cca512c2 | 8852 | return void_type_node; |
8853 | } | |
86359a65 | 8854 | gcc_checking_assert (!type_dependent_expression_p (expr)); |
8855 | return cv_unqualified (type_decays_to (unlowered_expr_type (expr))); | |
a8b75081 | 8856 | } |
8857 | ||
8858 | /* Given a LAMBDA_EXPR or closure type LAMBDA, return the op() of the | |
8859 | closure type. */ | |
8860 | ||
8861 | tree | |
8862 | lambda_function (tree lambda) | |
8863 | { | |
8864 | tree type; | |
8865 | if (TREE_CODE (lambda) == LAMBDA_EXPR) | |
35f606ed | 8866 | type = LAMBDA_EXPR_CLOSURE (lambda); |
a8b75081 | 8867 | else |
8868 | type = lambda; | |
8869 | gcc_assert (LAMBDA_TYPE_P (type)); | |
8870 | /* Don't let debug_tree cause instantiation. */ | |
c0156a98 | 8871 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (type) |
8872 | && !COMPLETE_OR_OPEN_TYPE_P (type)) | |
a8b75081 | 8873 | return NULL_TREE; |
8874 | lambda = lookup_member (type, ansi_opname (CALL_EXPR), | |
2cbaacd9 | 8875 | /*protect=*/0, /*want_type=*/false, |
8876 | tf_warning_or_error); | |
a8b75081 | 8877 | if (lambda) |
8878 | lambda = BASELINK_FUNCTIONS (lambda); | |
8879 | return lambda; | |
8880 | } | |
8881 | ||
8882 | /* Returns the type to use for the FIELD_DECL corresponding to the | |
8883 | capture of EXPR. | |
8884 | The caller should add REFERENCE_TYPE for capture by reference. */ | |
8885 | ||
8886 | tree | |
8887 | lambda_capture_field_type (tree expr) | |
8888 | { | |
8889 | tree type; | |
8890 | if (type_dependent_expression_p (expr)) | |
8891 | { | |
8892 | type = cxx_make_type (DECLTYPE_TYPE); | |
8893 | DECLTYPE_TYPE_EXPR (type) = expr; | |
8894 | DECLTYPE_FOR_LAMBDA_CAPTURE (type) = true; | |
8895 | SET_TYPE_STRUCTURAL_EQUALITY (type); | |
8896 | } | |
8897 | else | |
8898 | type = non_reference (unlowered_expr_type (expr)); | |
8899 | return type; | |
8900 | } | |
8901 | ||
86359a65 | 8902 | /* Insert the deduced return type for an auto function. */ |
a8b75081 | 8903 | |
8904 | void | |
86359a65 | 8905 | apply_deduced_return_type (tree fco, tree return_type) |
a8b75081 | 8906 | { |
a8b75081 | 8907 | tree result; |
8908 | ||
a8b75081 | 8909 | if (return_type == error_mark_node) |
8910 | return; | |
8911 | ||
86359a65 | 8912 | if (LAMBDA_FUNCTION_P (fco)) |
8913 | { | |
8914 | tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type); | |
8915 | LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type; | |
8916 | } | |
8917 | ||
8918 | if (DECL_CONV_FN_P (fco)) | |
8919 | DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type); | |
8920 | ||
93529b28 | 8921 | TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco)); |
a8b75081 | 8922 | |
8923 | result = DECL_RESULT (fco); | |
8924 | if (result == NULL_TREE) | |
8925 | return; | |
86359a65 | 8926 | if (TREE_TYPE (result) == return_type) |
8927 | return; | |
a8b75081 | 8928 | |
8929 | /* We already have a DECL_RESULT from start_preparsed_function. | |
8930 | Now we need to redo the work it and allocate_struct_function | |
8931 | did to reflect the new type. */ | |
a75d43a4 | 8932 | gcc_assert (current_function_decl == fco); |
a8b75081 | 8933 | result = build_decl (input_location, RESULT_DECL, NULL_TREE, |
8934 | TYPE_MAIN_VARIANT (return_type)); | |
8935 | DECL_ARTIFICIAL (result) = 1; | |
8936 | DECL_IGNORED_P (result) = 1; | |
8937 | cp_apply_type_quals_to_decl (cp_type_quals (return_type), | |
8938 | result); | |
8939 | ||
8940 | DECL_RESULT (fco) = result; | |
8941 | ||
86359a65 | 8942 | if (!processing_template_decl) |
a8b75081 | 8943 | { |
86359a65 | 8944 | bool aggr = aggregate_value_p (result, fco); |
a8b75081 | 8945 | #ifdef PCC_STATIC_STRUCT_RETURN |
86359a65 | 8946 | cfun->returns_pcc_struct = aggr; |
a8b75081 | 8947 | #endif |
86359a65 | 8948 | cfun->returns_struct = aggr; |
a8b75081 | 8949 | } |
8950 | ||
8951 | } | |
8952 | ||
8953 | /* DECL is a local variable or parameter from the surrounding scope of a | |
8954 | lambda-expression. Returns the decltype for a use of the capture field | |
8955 | for DECL even if it hasn't been captured yet. */ | |
8956 | ||
8957 | static tree | |
8958 | capture_decltype (tree decl) | |
8959 | { | |
8960 | tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl)); | |
8961 | /* FIXME do lookup instead of list walk? */ | |
8962 | tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam)); | |
8963 | tree type; | |
8964 | ||
8965 | if (cap) | |
8966 | type = TREE_TYPE (TREE_PURPOSE (cap)); | |
8967 | else | |
8968 | switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam)) | |
8969 | { | |
8970 | case CPLD_NONE: | |
8971 | error ("%qD is not captured", decl); | |
8972 | return error_mark_node; | |
8973 | ||
8974 | case CPLD_COPY: | |
8975 | type = TREE_TYPE (decl); | |
8976 | if (TREE_CODE (type) == REFERENCE_TYPE | |
8977 | && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE) | |
8978 | type = TREE_TYPE (type); | |
8979 | break; | |
8980 | ||
8981 | case CPLD_REFERENCE: | |
8982 | type = TREE_TYPE (decl); | |
8983 | if (TREE_CODE (type) != REFERENCE_TYPE) | |
8984 | type = build_reference_type (TREE_TYPE (decl)); | |
8985 | break; | |
8986 | ||
8987 | default: | |
8988 | gcc_unreachable (); | |
8989 | } | |
8990 | ||
8991 | if (TREE_CODE (type) != REFERENCE_TYPE) | |
8992 | { | |
8993 | if (!LAMBDA_EXPR_MUTABLE_P (lam)) | |
ce494fcf | 8994 | type = cp_build_qualified_type (type, (cp_type_quals (type) |
a8b75081 | 8995 | |TYPE_QUAL_CONST)); |
8996 | type = build_reference_type (type); | |
8997 | } | |
8998 | return type; | |
8999 | } | |
9000 | ||
bcc4b4ea | 9001 | /* Returns true iff DECL is a lambda capture proxy variable created by |
9002 | build_capture_proxy. */ | |
9003 | ||
9004 | bool | |
9005 | is_capture_proxy (tree decl) | |
9006 | { | |
9007 | return (TREE_CODE (decl) == VAR_DECL | |
9008 | && DECL_HAS_VALUE_EXPR_P (decl) | |
9009 | && !DECL_ANON_UNION_VAR_P (decl) | |
9010 | && LAMBDA_FUNCTION_P (DECL_CONTEXT (decl))); | |
9011 | } | |
9012 | ||
9013 | /* Returns true iff DECL is a capture proxy for a normal capture | |
9014 | (i.e. without explicit initializer). */ | |
9015 | ||
9016 | bool | |
9017 | is_normal_capture_proxy (tree decl) | |
9018 | { | |
bcc4b4ea | 9019 | if (!is_capture_proxy (decl)) |
9020 | /* It's not a capture proxy. */ | |
9021 | return false; | |
9022 | ||
9023 | /* It is a capture proxy, is it a normal capture? */ | |
f7855282 | 9024 | tree val = DECL_VALUE_EXPR (decl); |
9025 | if (val == error_mark_node) | |
9026 | return true; | |
9027 | ||
bcc4b4ea | 9028 | gcc_assert (TREE_CODE (val) == COMPONENT_REF); |
9029 | val = TREE_OPERAND (val, 1); | |
9030 | return DECL_NORMAL_CAPTURE_P (val); | |
9031 | } | |
9032 | ||
9033 | /* VAR is a capture proxy created by build_capture_proxy; add it to the | |
9034 | current function, which is the operator() for the appropriate lambda. */ | |
9035 | ||
607cc249 | 9036 | void |
bcc4b4ea | 9037 | insert_capture_proxy (tree var) |
9038 | { | |
d0ef83bc | 9039 | cp_binding_level *b; |
bcc4b4ea | 9040 | int skip; |
9041 | tree stmt_list; | |
9042 | ||
9043 | /* Put the capture proxy in the extra body block so that it won't clash | |
9044 | with a later local variable. */ | |
9045 | b = current_binding_level; | |
9046 | for (skip = 0; ; ++skip) | |
9047 | { | |
d0ef83bc | 9048 | cp_binding_level *n = b->level_chain; |
bcc4b4ea | 9049 | if (n->kind == sk_function_parms) |
9050 | break; | |
9051 | b = n; | |
9052 | } | |
9053 | pushdecl_with_scope (var, b, false); | |
9054 | ||
9055 | /* And put a DECL_EXPR in the STATEMENT_LIST for the same block. */ | |
9056 | var = build_stmt (DECL_SOURCE_LOCATION (var), DECL_EXPR, var); | |
f1f41a6c | 9057 | stmt_list = (*stmt_list_stack)[stmt_list_stack->length () - 1 - skip]; |
bcc4b4ea | 9058 | gcc_assert (stmt_list); |
9059 | append_to_statement_list_force (var, &stmt_list); | |
9060 | } | |
9061 | ||
9062 | /* We've just finished processing a lambda; if the containing scope is also | |
9063 | a lambda, insert any capture proxies that were created while processing | |
9064 | the nested lambda. */ | |
9065 | ||
9066 | void | |
9067 | insert_pending_capture_proxies (void) | |
9068 | { | |
9069 | tree lam; | |
f1f41a6c | 9070 | vec<tree, va_gc> *proxies; |
bcc4b4ea | 9071 | unsigned i; |
9072 | ||
9073 | if (!current_function_decl || !LAMBDA_FUNCTION_P (current_function_decl)) | |
9074 | return; | |
9075 | ||
9076 | lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl)); | |
9077 | proxies = LAMBDA_EXPR_PENDING_PROXIES (lam); | |
f1f41a6c | 9078 | for (i = 0; i < vec_safe_length (proxies); ++i) |
bcc4b4ea | 9079 | { |
f1f41a6c | 9080 | tree var = (*proxies)[i]; |
bcc4b4ea | 9081 | insert_capture_proxy (var); |
9082 | } | |
9083 | release_tree_vector (LAMBDA_EXPR_PENDING_PROXIES (lam)); | |
9084 | LAMBDA_EXPR_PENDING_PROXIES (lam) = NULL; | |
9085 | } | |
9086 | ||
b3b681fb | 9087 | /* Given REF, a COMPONENT_REF designating a field in the lambda closure, |
9088 | return the type we want the proxy to have: the type of the field itself, | |
9089 | with added const-qualification if the lambda isn't mutable and the | |
9090 | capture is by value. */ | |
9091 | ||
9092 | tree | |
9093 | lambda_proxy_type (tree ref) | |
9094 | { | |
9095 | tree type; | |
9096 | if (REFERENCE_REF_P (ref)) | |
9097 | ref = TREE_OPERAND (ref, 0); | |
9098 | type = TREE_TYPE (ref); | |
9099 | if (!dependent_type_p (type)) | |
9100 | return type; | |
9101 | type = cxx_make_type (DECLTYPE_TYPE); | |
9102 | DECLTYPE_TYPE_EXPR (type) = ref; | |
9103 | DECLTYPE_FOR_LAMBDA_PROXY (type) = true; | |
9104 | SET_TYPE_STRUCTURAL_EQUALITY (type); | |
9105 | return type; | |
9106 | } | |
9107 | ||
bcc4b4ea | 9108 | /* MEMBER is a capture field in a lambda closure class. Now that we're |
9109 | inside the operator(), build a placeholder var for future lookups and | |
9110 | debugging. */ | |
9111 | ||
9112 | tree | |
9113 | build_capture_proxy (tree member) | |
9114 | { | |
b3b681fb | 9115 | tree var, object, fn, closure, name, lam, type; |
bcc4b4ea | 9116 | |
9117 | closure = DECL_CONTEXT (member); | |
9118 | fn = lambda_function (closure); | |
9119 | lam = CLASSTYPE_LAMBDA_EXPR (closure); | |
9120 | ||
9121 | /* The proxy variable forwards to the capture field. */ | |
9122 | object = build_fold_indirect_ref (DECL_ARGUMENTS (fn)); | |
9123 | object = finish_non_static_data_member (member, object, NULL_TREE); | |
9124 | if (REFERENCE_REF_P (object)) | |
9125 | object = TREE_OPERAND (object, 0); | |
9126 | ||
9127 | /* Remove the __ inserted by add_capture. */ | |
9128 | name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2); | |
9129 | ||
b3b681fb | 9130 | type = lambda_proxy_type (object); |
9131 | var = build_decl (input_location, VAR_DECL, name, type); | |
bcc4b4ea | 9132 | SET_DECL_VALUE_EXPR (var, object); |
9133 | DECL_HAS_VALUE_EXPR_P (var) = 1; | |
9134 | DECL_ARTIFICIAL (var) = 1; | |
9135 | TREE_USED (var) = 1; | |
9136 | DECL_CONTEXT (var) = fn; | |
9137 | ||
9138 | if (name == this_identifier) | |
9139 | { | |
9140 | gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member); | |
9141 | LAMBDA_EXPR_THIS_CAPTURE (lam) = var; | |
9142 | } | |
9143 | ||
9144 | if (fn == current_function_decl) | |
9145 | insert_capture_proxy (var); | |
9146 | else | |
f1f41a6c | 9147 | vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var); |
bcc4b4ea | 9148 | |
9149 | return var; | |
9150 | } | |
9151 | ||
a8b75081 | 9152 | /* From an ID and INITIALIZER, create a capture (by reference if |
9153 | BY_REFERENCE_P is true), add it to the capture-list for LAMBDA, | |
9154 | and return it. */ | |
9155 | ||
9156 | tree | |
7da3c25a | 9157 | add_capture (tree lambda, tree id, tree initializer, bool by_reference_p, |
9158 | bool explicit_init_p) | |
a8b75081 | 9159 | { |
3bd4ccf0 | 9160 | char *buf; |
9161 | tree type, member, name; | |
a8b75081 | 9162 | |
9163 | type = lambda_capture_field_type (initializer); | |
9164 | if (by_reference_p) | |
9165 | { | |
9166 | type = build_reference_type (type); | |
9167 | if (!real_lvalue_p (initializer)) | |
9168 | error ("cannot capture %qE by reference", initializer); | |
9169 | } | |
55d9ad95 | 9170 | else |
9171 | /* Capture by copy requires a complete type. */ | |
9172 | type = complete_type (type); | |
a8b75081 | 9173 | |
3bd4ccf0 | 9174 | /* Add __ to the beginning of the field name so that user code |
9175 | won't find the field with name lookup. We can't just leave the name | |
9176 | unset because template instantiation uses the name to find | |
9177 | instantiated fields. */ | |
9178 | buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3); | |
9179 | buf[1] = buf[0] = '_'; | |
9180 | memcpy (buf + 2, IDENTIFIER_POINTER (id), | |
9181 | IDENTIFIER_LENGTH (id) + 1); | |
9182 | name = get_identifier (buf); | |
9183 | ||
9184 | /* If TREE_TYPE isn't set, we're still in the introducer, so check | |
9185 | for duplicates. */ | |
35f606ed | 9186 | if (!LAMBDA_EXPR_CLOSURE (lambda)) |
3bd4ccf0 | 9187 | { |
9188 | if (IDENTIFIER_MARKED (name)) | |
9189 | { | |
9190 | pedwarn (input_location, 0, | |
9191 | "already captured %qD in lambda expression", id); | |
9192 | return NULL_TREE; | |
9193 | } | |
9194 | IDENTIFIER_MARKED (name) = true; | |
9195 | } | |
9196 | ||
a8b75081 | 9197 | /* Make member variable. */ |
3bd4ccf0 | 9198 | member = build_lang_decl (FIELD_DECL, name, type); |
bcc4b4ea | 9199 | |
7da3c25a | 9200 | if (!explicit_init_p) |
9201 | /* Normal captures are invisible to name lookup but uses are replaced | |
9202 | with references to the capture field; we implement this by only | |
9203 | really making them invisible in unevaluated context; see | |
9204 | qualify_lookup. For now, let's make explicitly initialized captures | |
9205 | always visible. */ | |
9206 | DECL_NORMAL_CAPTURE_P (member) = true; | |
a8b75081 | 9207 | |
3bd4ccf0 | 9208 | if (id == this_identifier) |
9209 | LAMBDA_EXPR_THIS_CAPTURE (lambda) = member; | |
9210 | ||
86b604cf | 9211 | /* Add it to the appropriate closure class if we've started it. */ |
35f606ed | 9212 | if (current_class_type |
9213 | && current_class_type == LAMBDA_EXPR_CLOSURE (lambda)) | |
86b604cf | 9214 | finish_member_declaration (member); |
a8b75081 | 9215 | |
9216 | LAMBDA_EXPR_CAPTURE_LIST (lambda) | |
9217 | = tree_cons (member, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda)); | |
9218 | ||
35f606ed | 9219 | if (LAMBDA_EXPR_CLOSURE (lambda)) |
bcc4b4ea | 9220 | return build_capture_proxy (member); |
9221 | /* For explicit captures we haven't started the function yet, so we wait | |
9222 | and build the proxy from cp_parser_lambda_body. */ | |
9223 | return NULL_TREE; | |
a8b75081 | 9224 | } |
9225 | ||
86b604cf | 9226 | /* Register all the capture members on the list CAPTURES, which is the |
9227 | LAMBDA_EXPR_CAPTURE_LIST for the lambda after the introducer. */ | |
9228 | ||
3bd4ccf0 | 9229 | void |
9230 | register_capture_members (tree captures) | |
86b604cf | 9231 | { |
3bd4ccf0 | 9232 | if (captures == NULL_TREE) |
9233 | return; | |
9234 | ||
9235 | register_capture_members (TREE_CHAIN (captures)); | |
9236 | /* We set this in add_capture to avoid duplicates. */ | |
9237 | IDENTIFIER_MARKED (DECL_NAME (TREE_PURPOSE (captures))) = false; | |
9238 | finish_member_declaration (TREE_PURPOSE (captures)); | |
86b604cf | 9239 | } |
9240 | ||
a8b75081 | 9241 | /* Similar to add_capture, except this works on a stack of nested lambdas. |
9242 | BY_REFERENCE_P in this case is derived from the default capture mode. | |
9243 | Returns the capture for the lambda at the bottom of the stack. */ | |
9244 | ||
9245 | tree | |
9246 | add_default_capture (tree lambda_stack, tree id, tree initializer) | |
9247 | { | |
bcc4b4ea | 9248 | bool this_capture_p = (id == this_identifier); |
a8b75081 | 9249 | |
bcc4b4ea | 9250 | tree var = NULL_TREE; |
a8b75081 | 9251 | |
9252 | tree saved_class_type = current_class_type; | |
9253 | ||
9254 | tree node; | |
9255 | ||
9256 | for (node = lambda_stack; | |
9257 | node; | |
9258 | node = TREE_CHAIN (node)) | |
9259 | { | |
9260 | tree lambda = TREE_VALUE (node); | |
9261 | ||
35f606ed | 9262 | current_class_type = LAMBDA_EXPR_CLOSURE (lambda); |
bcc4b4ea | 9263 | var = add_capture (lambda, |
a8b75081 | 9264 | id, |
9265 | initializer, | |
9266 | /*by_reference_p=*/ | |
9267 | (!this_capture_p | |
9268 | && (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) | |
7da3c25a | 9269 | == CPLD_REFERENCE)), |
9270 | /*explicit_init_p=*/false); | |
bcc4b4ea | 9271 | initializer = convert_from_reference (var); |
a8b75081 | 9272 | } |
9273 | ||
9274 | current_class_type = saved_class_type; | |
9275 | ||
bcc4b4ea | 9276 | return var; |
a8b75081 | 9277 | } |
9278 | ||
9279 | /* Return the capture pertaining to a use of 'this' in LAMBDA, in the form of an | |
9280 | INDIRECT_REF, possibly adding it through default capturing. */ | |
9281 | ||
9282 | tree | |
9283 | lambda_expr_this_capture (tree lambda) | |
9284 | { | |
9285 | tree result; | |
9286 | ||
9287 | tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda); | |
9288 | ||
9289 | /* Try to default capture 'this' if we can. */ | |
9290 | if (!this_capture | |
9291 | && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE) | |
9292 | { | |
35f606ed | 9293 | tree containing_function = TYPE_CONTEXT (LAMBDA_EXPR_CLOSURE (lambda)); |
a8b75081 | 9294 | tree lambda_stack = tree_cons (NULL_TREE, lambda, NULL_TREE); |
fffbaa80 | 9295 | tree init = NULL_TREE; |
a8b75081 | 9296 | |
9297 | /* If we are in a lambda function, we can move out until we hit: | |
9298 | 1. a non-lambda function, | |
9299 | 2. a lambda function capturing 'this', or | |
9300 | 3. a non-default capturing lambda function. */ | |
9301 | while (LAMBDA_FUNCTION_P (containing_function)) | |
9302 | { | |
9303 | tree lambda | |
9304 | = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function)); | |
9305 | ||
fffbaa80 | 9306 | if (LAMBDA_EXPR_THIS_CAPTURE (lambda)) |
9307 | { | |
9308 | /* An outer lambda has already captured 'this'. */ | |
bcc4b4ea | 9309 | init = LAMBDA_EXPR_THIS_CAPTURE (lambda); |
fffbaa80 | 9310 | break; |
9311 | } | |
9312 | ||
9313 | if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_NONE) | |
9314 | /* An outer lambda won't let us capture 'this'. */ | |
9315 | break; | |
a8b75081 | 9316 | |
9317 | lambda_stack = tree_cons (NULL_TREE, | |
9318 | lambda, | |
9319 | lambda_stack); | |
9320 | ||
9321 | containing_function = decl_function_context (containing_function); | |
9322 | } | |
9323 | ||
fffbaa80 | 9324 | if (!init && DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function) |
9325 | && !LAMBDA_FUNCTION_P (containing_function)) | |
9326 | /* First parameter is 'this'. */ | |
9327 | init = DECL_ARGUMENTS (containing_function); | |
a8b75081 | 9328 | |
fffbaa80 | 9329 | if (init) |
9330 | this_capture = add_default_capture (lambda_stack, | |
bcc4b4ea | 9331 | /*id=*/this_identifier, |
fffbaa80 | 9332 | init); |
a8b75081 | 9333 | } |
9334 | ||
9335 | if (!this_capture) | |
9336 | { | |
9337 | error ("%<this%> was not captured for this lambda function"); | |
9338 | result = error_mark_node; | |
9339 | } | |
9340 | else | |
9341 | { | |
9342 | /* To make sure that current_class_ref is for the lambda. */ | |
35f606ed | 9343 | gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)) |
9344 | == LAMBDA_EXPR_CLOSURE (lambda)); | |
a8b75081 | 9345 | |
bcc4b4ea | 9346 | result = this_capture; |
a8b75081 | 9347 | |
9348 | /* If 'this' is captured, each use of 'this' is transformed into an | |
9349 | access to the corresponding unnamed data member of the closure | |
9350 | type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast | |
9351 | ensures that the transformed expression is an rvalue. ] */ | |
9352 | result = rvalue (result); | |
9353 | } | |
9354 | ||
9355 | return result; | |
9356 | } | |
9357 | ||
e97d2125 | 9358 | /* Returns the method basetype of the innermost non-lambda function, or |
9359 | NULL_TREE if none. */ | |
9360 | ||
9361 | tree | |
9362 | nonlambda_method_basetype (void) | |
9363 | { | |
9364 | tree fn, type; | |
9365 | if (!current_class_ref) | |
9366 | return NULL_TREE; | |
9367 | ||
9368 | type = current_class_type; | |
9369 | if (!LAMBDA_TYPE_P (type)) | |
9370 | return type; | |
9371 | ||
9372 | /* Find the nearest enclosing non-lambda function. */ | |
9373 | fn = TYPE_NAME (type); | |
9374 | do | |
9375 | fn = decl_function_context (fn); | |
9376 | while (fn && LAMBDA_FUNCTION_P (fn)); | |
9377 | ||
9378 | if (!fn || !DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) | |
9379 | return NULL_TREE; | |
9380 | ||
9381 | return TYPE_METHOD_BASETYPE (TREE_TYPE (fn)); | |
9382 | } | |
9383 | ||
91a733f9 | 9384 | /* If the closure TYPE has a static op(), also add a conversion to function |
9385 | pointer. */ | |
9386 | ||
9387 | void | |
9388 | maybe_add_lambda_conv_op (tree type) | |
9389 | { | |
9390 | bool nested = (current_function_decl != NULL_TREE); | |
9391 | tree callop = lambda_function (type); | |
9392 | tree rettype, name, fntype, fn, body, compound_stmt; | |
43936711 | 9393 | tree thistype, stattype, statfn, convfn, call, arg; |
f1f41a6c | 9394 | vec<tree, va_gc> *argvec; |
91a733f9 | 9395 | |
43936711 | 9396 | if (LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (type)) != NULL_TREE) |
91a733f9 | 9397 | return; |
9398 | ||
7b9f2d96 | 9399 | if (processing_template_decl) |
9400 | return; | |
9401 | ||
43936711 | 9402 | stattype = build_function_type (TREE_TYPE (TREE_TYPE (callop)), |
9403 | FUNCTION_ARG_CHAIN (callop)); | |
9404 | ||
9405 | /* First build up the conversion op. */ | |
9406 | ||
9407 | rettype = build_pointer_type (stattype); | |
91a733f9 | 9408 | name = mangle_conv_op_name_for_type (rettype); |
43936711 | 9409 | thistype = cp_build_qualified_type (type, TYPE_QUAL_CONST); |
9410 | fntype = build_method_type_directly (thistype, rettype, void_list_node); | |
9411 | fn = convfn = build_lang_decl (FUNCTION_DECL, name, fntype); | |
91a733f9 | 9412 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop); |
9413 | ||
9414 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn | |
9415 | && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT) | |
9416 | DECL_ALIGN (fn) = 2 * BITS_PER_UNIT; | |
9417 | ||
9418 | SET_OVERLOADED_OPERATOR_CODE (fn, TYPE_EXPR); | |
9419 | grokclassfn (type, fn, NO_SPECIAL); | |
9420 | set_linkage_according_to_type (type, fn); | |
9421 | rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof); | |
9422 | DECL_IN_AGGR_P (fn) = 1; | |
9423 | DECL_ARTIFICIAL (fn) = 1; | |
9424 | DECL_NOT_REALLY_EXTERN (fn) = 1; | |
9425 | DECL_DECLARED_INLINE_P (fn) = 1; | |
43936711 | 9426 | DECL_ARGUMENTS (fn) = build_this_parm (fntype, TYPE_QUAL_CONST); |
0e7db593 | 9427 | if (nested) |
9428 | DECL_INTERFACE_KNOWN (fn) = 1; | |
43936711 | 9429 | |
9430 | add_method (type, fn, NULL_TREE); | |
9431 | ||
9432 | /* Generic thunk code fails for varargs; we'll complain in mark_used if | |
9433 | the conversion op is used. */ | |
9434 | if (varargs_function_p (callop)) | |
9435 | { | |
9436 | DECL_DELETED_FN (fn) = 1; | |
9437 | return; | |
9438 | } | |
9439 | ||
9440 | /* Now build up the thunk to be returned. */ | |
9441 | ||
9442 | name = get_identifier ("_FUN"); | |
9443 | fn = statfn = build_lang_decl (FUNCTION_DECL, name, stattype); | |
9444 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (callop); | |
9445 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn | |
9446 | && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT) | |
9447 | DECL_ALIGN (fn) = 2 * BITS_PER_UNIT; | |
9448 | grokclassfn (type, fn, NO_SPECIAL); | |
9449 | set_linkage_according_to_type (type, fn); | |
9450 | rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof); | |
9451 | DECL_IN_AGGR_P (fn) = 1; | |
9452 | DECL_ARTIFICIAL (fn) = 1; | |
9453 | DECL_NOT_REALLY_EXTERN (fn) = 1; | |
9454 | DECL_DECLARED_INLINE_P (fn) = 1; | |
91a733f9 | 9455 | DECL_STATIC_FUNCTION_P (fn) = 1; |
1767a056 | 9456 | DECL_ARGUMENTS (fn) = copy_list (DECL_CHAIN (DECL_ARGUMENTS (callop))); |
9457 | for (arg = DECL_ARGUMENTS (fn); arg; arg = DECL_CHAIN (arg)) | |
43936711 | 9458 | DECL_CONTEXT (arg) = fn; |
0e7db593 | 9459 | if (nested) |
9460 | DECL_INTERFACE_KNOWN (fn) = 1; | |
91a733f9 | 9461 | |
9462 | add_method (type, fn, NULL_TREE); | |
9463 | ||
9464 | if (nested) | |
9465 | push_function_context (); | |
cf797b5e | 9466 | else |
9467 | /* Still increment function_depth so that we don't GC in the | |
9468 | middle of an expression. */ | |
9469 | ++function_depth; | |
43936711 | 9470 | |
9471 | /* Generate the body of the thunk. */ | |
9472 | ||
9473 | start_preparsed_function (statfn, NULL_TREE, | |
9474 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
9475 | if (DECL_ONE_ONLY (statfn)) | |
9476 | { | |
9477 | /* Put the thunk in the same comdat group as the call op. */ | |
cf951b1a | 9478 | symtab_add_to_same_comdat_group |
9479 | ((symtab_node) cgraph_get_create_node (statfn), | |
9480 | (symtab_node) cgraph_get_create_node (callop)); | |
43936711 | 9481 | } |
9482 | body = begin_function_body (); | |
9483 | compound_stmt = begin_compound_stmt (0); | |
9484 | ||
5448d137 | 9485 | arg = build1 (NOP_EXPR, TREE_TYPE (DECL_ARGUMENTS (callop)), |
9486 | null_pointer_node); | |
43936711 | 9487 | argvec = make_tree_vector (); |
f1f41a6c | 9488 | argvec->quick_push (arg); |
1767a056 | 9489 | for (arg = DECL_ARGUMENTS (statfn); arg; arg = DECL_CHAIN (arg)) |
dec88b99 | 9490 | { |
9491 | mark_exp_read (arg); | |
f1f41a6c | 9492 | vec_safe_push (argvec, arg); |
dec88b99 | 9493 | } |
f1f41a6c | 9494 | call = build_call_a (callop, argvec->length (), argvec->address ()); |
43936711 | 9495 | CALL_FROM_THUNK_P (call) = 1; |
2a6e66d9 | 9496 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (call))) |
3d4bed93 | 9497 | call = build_cplus_new (TREE_TYPE (call), call, tf_warning_or_error); |
2a6e66d9 | 9498 | call = convert_from_reference (call); |
43936711 | 9499 | finish_return_stmt (call); |
9500 | ||
9501 | finish_compound_stmt (compound_stmt); | |
9502 | finish_function_body (body); | |
9503 | ||
9504 | expand_or_defer_fn (finish_function (2)); | |
9505 | ||
9506 | /* Generate the body of the conversion op. */ | |
9507 | ||
9508 | start_preparsed_function (convfn, NULL_TREE, | |
91a733f9 | 9509 | SF_PRE_PARSED | SF_INCLASS_INLINE); |
9510 | body = begin_function_body (); | |
9511 | compound_stmt = begin_compound_stmt (0); | |
9512 | ||
4405c1ad | 9513 | finish_return_stmt (decay_conversion (statfn, tf_warning_or_error)); |
91a733f9 | 9514 | |
9515 | finish_compound_stmt (compound_stmt); | |
9516 | finish_function_body (body); | |
9517 | ||
9518 | expand_or_defer_fn (finish_function (2)); | |
43936711 | 9519 | |
91a733f9 | 9520 | if (nested) |
9521 | pop_function_context (); | |
cf797b5e | 9522 | else |
9523 | --function_depth; | |
91a733f9 | 9524 | } |
bcc4b4ea | 9525 | |
9526 | /* Returns true iff VAL is a lambda-related declaration which should | |
9527 | be ignored by unqualified lookup. */ | |
9528 | ||
9529 | bool | |
9530 | is_lambda_ignored_entity (tree val) | |
9531 | { | |
9532 | /* In unevaluated context, look past normal capture proxies. */ | |
9533 | if (cp_unevaluated_operand && is_normal_capture_proxy (val)) | |
9534 | return true; | |
9535 | ||
9536 | /* Always ignore lambda fields, their names are only for debugging. */ | |
9537 | if (TREE_CODE (val) == FIELD_DECL | |
9538 | && CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (val))) | |
9539 | return true; | |
9540 | ||
9541 | /* None of the lookups that use qualify_lookup want the op() from the | |
9542 | lambda; they want the one from the enclosing class. */ | |
9543 | if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val)) | |
9544 | return true; | |
9545 | ||
9546 | return false; | |
9547 | } | |
9548 | ||
9b57b06b | 9549 | #include "gt-cp-semantics.h" |