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 |
4 | and during the instantiation of template functions. |
5 | |
6 | Copyright (C) 1998-2017 Free Software Foundation, Inc. |
7 | Written by Mark Mitchell (mmitchell@usa.net) based on code found |
8 | formerly in parse.y and pt.c. |
9 | |
10 | This file is part of GCC. |
11 | |
12 | GCC is free software; you can redistribute it and/or modify it |
13 | under the terms of the GNU General Public License as published by |
14 | the Free Software Foundation; either version 3, or (at your option) |
15 | any later version. |
16 | |
17 | GCC is distributed in the hope that it will be useful, but |
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. |
21 | |
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/>. */ |
25 | |
26 | #include "config.h" |
27 | #include "system.h" |
28 | #include "coretypes.h" |
29 | #include "target.h" |
30 | #include "bitmap.h" |
31 | #include "cp-tree.h" |
32 | #include "stringpool.h" |
33 | #include "cgraph.h" |
34 | #include "stmt.h" |
35 | #include "varasm.h" |
36 | #include "stor-layout.h" |
37 | #include "c-family/c-objc.h" |
38 | #include "tree-inline.h" |
39 | #include "intl.h" |
40 | #include "tree-iterator.h" |
41 | #include "omp-general.h" |
42 | #include "convert.h" |
43 | #include "stringpool.h" |
44 | #include "attribs.h" |
45 | #include "gomp-constants.h" |
46 | #include "predict.h" |
47 | |
48 | /* There routines provide a modular interface to perform many parsing |
49 | operations. They may therefore be used during actual parsing, or |
50 | during template instantiation, which may be regarded as a |
51 | degenerate form of parsing. */ |
52 | |
53 | static tree maybe_convert_cond (tree); |
54 | static tree finalize_nrv_r (tree *, int *, void *); |
55 | static tree capture_decltype (tree); |
56 | |
57 | /* Used for OpenMP non-static data member privatization. */ |
58 | |
59 | static hash_map<tree, tree> *omp_private_member_map; |
60 | static vec<tree> omp_private_member_vec; |
61 | static bool omp_private_member_ignore_next; |
62 | |
63 | |
64 | /* Deferred Access Checking Overview |
65 | --------------------------------- |
66 | |
67 | Most C++ expressions and declarations require access checking |
68 | to be performed during parsing. However, in several cases, |
69 | this has to be treated differently. |
70 | |
71 | For member declarations, access checking has to be deferred |
72 | until more information about the declaration is known. For |
73 | example: |
74 | |
75 | class A { |
76 | typedef int X; |
77 | public: |
78 | X f(); |
79 | }; |
80 | |
81 | A::X A::f(); |
82 | A::X g(); |
83 | |
84 | When we are parsing the function return type `A::X', we don't |
85 | really know if this is allowed until we parse the function name. |
86 | |
87 | Furthermore, some contexts require that access checking is |
88 | never performed at all. These include class heads, and template |
89 | instantiations. |
90 | |
91 | Typical use of access checking functions is described here: |
92 | |
93 | 1. When we enter a context that requires certain access checking |
94 | mode, the function `push_deferring_access_checks' is called with |
95 | DEFERRING argument specifying the desired mode. Access checking |
96 | may be performed immediately (dk_no_deferred), deferred |
97 | (dk_deferred), or not performed (dk_no_check). |
98 | |
99 | 2. When a declaration such as a type, or a variable, is encountered, |
100 | the function `perform_or_defer_access_check' is called. It |
101 | maintains a vector of all deferred checks. |
102 | |
103 | 3. The global `current_class_type' or `current_function_decl' is then |
104 | setup by the parser. `enforce_access' relies on these information |
105 | to check access. |
106 | |
107 | 4. Upon exiting the context mentioned in step 1, |
108 | `perform_deferred_access_checks' is called to check all declaration |
109 | stored in the vector. `pop_deferring_access_checks' is then |
110 | called to restore the previous access checking mode. |
111 | |
112 | In case of parsing error, we simply call `pop_deferring_access_checks' |
113 | without `perform_deferred_access_checks'. */ |
114 | |
115 | struct GTY(()) deferred_access { |
116 | /* A vector representing name-lookups for which we have deferred |
117 | checking access controls. We cannot check the accessibility of |
118 | names used in a decl-specifier-seq until we know what is being |
119 | declared because code like: |
120 | |
121 | class A { |
122 | class B {}; |
123 | B* f(); |
124 | } |
125 | |
126 | A::B* A::f() { return 0; } |
127 | |
128 | is valid, even though `A::B' is not generally accessible. */ |
129 | vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks; |
130 | |
131 | /* The current mode of access checks. */ |
132 | enum deferring_kind deferring_access_checks_kind; |
133 | |
134 | }; |
135 | |
136 | /* Data for deferred access checking. */ |
137 | static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack; |
138 | static GTY(()) unsigned deferred_access_no_check; |
139 | |
140 | /* Save the current deferred access states and start deferred |
141 | access checking iff DEFER_P is true. */ |
142 | |
143 | void |
144 | push_deferring_access_checks (deferring_kind deferring) |
145 | { |
146 | /* For context like template instantiation, access checking |
147 | disabling applies to all nested context. */ |
148 | if (deferred_access_no_check || deferring == dk_no_check) |
149 | deferred_access_no_check++; |
150 | else |
151 | { |
152 | deferred_access e = {NULL, deferring}; |
153 | vec_safe_push (deferred_access_stack, e); |
154 | } |
155 | } |
156 | |
157 | /* Save the current deferred access states and start deferred access |
158 | checking, continuing the set of deferred checks in CHECKS. */ |
159 | |
160 | void |
161 | reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks) |
162 | { |
163 | push_deferring_access_checks (dk_deferred); |
164 | if (!deferred_access_no_check) |
165 | deferred_access_stack->last().deferred_access_checks = checks; |
166 | } |
167 | |
168 | /* Resume deferring access checks again after we stopped doing |
169 | this previously. */ |
170 | |
171 | void |
172 | resume_deferring_access_checks (void) |
173 | { |
174 | if (!deferred_access_no_check) |
175 | deferred_access_stack->last().deferring_access_checks_kind = dk_deferred; |
176 | } |
177 | |
178 | /* Stop deferring access checks. */ |
179 | |
180 | void |
181 | stop_deferring_access_checks (void) |
182 | { |
183 | if (!deferred_access_no_check) |
184 | deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred; |
185 | } |
186 | |
187 | /* Discard the current deferred access checks and restore the |
188 | previous states. */ |
189 | |
190 | void |
191 | pop_deferring_access_checks (void) |
192 | { |
193 | if (deferred_access_no_check) |
194 | deferred_access_no_check--; |
195 | else |
196 | deferred_access_stack->pop (); |
197 | } |
198 | |
199 | /* Returns a TREE_LIST representing the deferred checks. |
200 | The TREE_PURPOSE of each node is the type through which the |
201 | access occurred; the TREE_VALUE is the declaration named. |
202 | */ |
203 | |
204 | vec<deferred_access_check, va_gc> * |
205 | get_deferred_access_checks (void) |
206 | { |
207 | if (deferred_access_no_check) |
208 | return NULL; |
209 | else |
210 | return (deferred_access_stack->last().deferred_access_checks); |
211 | } |
212 | |
213 | /* Take current deferred checks and combine with the |
214 | previous states if we also defer checks previously. |
215 | Otherwise perform checks now. */ |
216 | |
217 | void |
218 | pop_to_parent_deferring_access_checks (void) |
219 | { |
220 | if (deferred_access_no_check) |
221 | deferred_access_no_check--; |
222 | else |
223 | { |
224 | vec<deferred_access_check, va_gc> *checks; |
225 | deferred_access *ptr; |
226 | |
227 | checks = (deferred_access_stack->last ().deferred_access_checks); |
228 | |
229 | deferred_access_stack->pop (); |
230 | ptr = &deferred_access_stack->last (); |
231 | if (ptr->deferring_access_checks_kind == dk_no_deferred) |
232 | { |
233 | /* Check access. */ |
234 | perform_access_checks (checks, tf_warning_or_error); |
235 | } |
236 | else |
237 | { |
238 | /* Merge with parent. */ |
239 | int i, j; |
240 | deferred_access_check *chk, *probe; |
241 | |
242 | FOR_EACH_VEC_SAFE_ELT (checks, i, chk) |
243 | { |
244 | FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe) |
245 | { |
246 | if (probe->binfo == chk->binfo && |
247 | probe->decl == chk->decl && |
248 | probe->diag_decl == chk->diag_decl) |
249 | goto found; |
250 | } |
251 | /* Insert into parent's checks. */ |
252 | vec_safe_push (ptr->deferred_access_checks, *chk); |
253 | found:; |
254 | } |
255 | } |
256 | } |
257 | } |
258 | |
259 | /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node |
260 | is the BINFO indicating the qualifying scope used to access the |
261 | DECL node stored in the TREE_VALUE of the node. If CHECKS is empty |
262 | or we aren't in SFINAE context or all the checks succeed return TRUE, |
263 | otherwise FALSE. */ |
264 | |
265 | bool |
266 | perform_access_checks (vec<deferred_access_check, va_gc> *checks, |
267 | tsubst_flags_t complain) |
268 | { |
269 | int i; |
270 | deferred_access_check *chk; |
271 | location_t loc = input_location; |
272 | bool ok = true; |
273 | |
274 | if (!checks) |
275 | return true; |
276 | |
277 | FOR_EACH_VEC_SAFE_ELT (checks, i, chk) |
278 | { |
279 | input_location = chk->loc; |
280 | ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain); |
281 | } |
282 | |
283 | input_location = loc; |
284 | return (complain & tf_error) ? true : ok; |
285 | } |
286 | |
287 | /* Perform the deferred access checks. |
288 | |
289 | After performing the checks, we still have to keep the list |
290 | `deferred_access_stack->deferred_access_checks' since we may want |
291 | to check access for them again later in a different context. |
292 | For example: |
293 | |
294 | class A { |
295 | typedef int X; |
296 | static X a; |
297 | }; |
298 | A::X A::a, x; // No error for `A::a', error for `x' |
299 | |
300 | We have to perform deferred access of `A::X', first with `A::a', |
301 | next with `x'. Return value like perform_access_checks above. */ |
302 | |
303 | bool |
304 | perform_deferred_access_checks (tsubst_flags_t complain) |
305 | { |
306 | return perform_access_checks (get_deferred_access_checks (), complain); |
307 | } |
308 | |
309 | /* Defer checking the accessibility of DECL, when looked up in |
310 | BINFO. DIAG_DECL is the declaration to use to print diagnostics. |
311 | Return value like perform_access_checks above. |
312 | If non-NULL, report failures to AFI. */ |
313 | |
314 | bool |
315 | perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl, |
316 | tsubst_flags_t complain, |
317 | access_failure_info *afi) |
318 | { |
319 | int i; |
320 | deferred_access *ptr; |
321 | deferred_access_check *chk; |
322 | |
323 | |
324 | /* Exit if we are in a context that no access checking is performed. |
325 | */ |
326 | if (deferred_access_no_check) |
327 | return true; |
328 | |
329 | gcc_assert (TREE_CODE (binfo) == TREE_BINFO); |
330 | |
331 | ptr = &deferred_access_stack->last (); |
332 | |
333 | /* If we are not supposed to defer access checks, just check now. */ |
334 | if (ptr->deferring_access_checks_kind == dk_no_deferred) |
335 | { |
336 | bool ok = enforce_access (binfo, decl, diag_decl, complain, afi); |
337 | return (complain & tf_error) ? true : ok; |
338 | } |
339 | |
340 | /* See if we are already going to perform this check. */ |
341 | FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk) |
342 | { |
343 | if (chk->decl == decl && chk->binfo == binfo && |
344 | chk->diag_decl == diag_decl) |
345 | { |
346 | return true; |
347 | } |
348 | } |
349 | /* If not, record the check. */ |
350 | deferred_access_check new_access = {binfo, decl, diag_decl, input_location}; |
351 | vec_safe_push (ptr->deferred_access_checks, new_access); |
352 | |
353 | return true; |
354 | } |
355 | |
356 | /* Returns nonzero if the current statement is a full expression, |
357 | i.e. temporaries created during that statement should be destroyed |
358 | at the end of the statement. */ |
359 | |
360 | int |
361 | stmts_are_full_exprs_p (void) |
362 | { |
363 | return current_stmt_tree ()->stmts_are_full_exprs_p; |
364 | } |
365 | |
366 | /* T is a statement. Add it to the statement-tree. This is the C++ |
367 | version. The C/ObjC frontends have a slightly different version of |
368 | this function. */ |
369 | |
370 | tree |
371 | add_stmt (tree t) |
372 | { |
373 | enum tree_code code = TREE_CODE (t); |
374 | |
375 | if (EXPR_P (t) && code != LABEL_EXPR) |
376 | { |
377 | if (!EXPR_HAS_LOCATION (t)) |
378 | SET_EXPR_LOCATION (t, input_location); |
379 | |
380 | /* When we expand a statement-tree, we must know whether or not the |
381 | statements are full-expressions. We record that fact here. */ |
382 | STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p (); |
383 | } |
384 | |
385 | if (code == LABEL_EXPR || code == CASE_LABEL_EXPR) |
386 | STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1; |
387 | |
388 | /* Add T to the statement-tree. Non-side-effect statements need to be |
389 | recorded during statement expressions. */ |
390 | gcc_checking_assert (!stmt_list_stack->is_empty ()); |
391 | append_to_statement_list_force (t, &cur_stmt_list); |
392 | |
393 | return t; |
394 | } |
395 | |
396 | /* Returns the stmt_tree to which statements are currently being added. */ |
397 | |
398 | stmt_tree |
399 | current_stmt_tree (void) |
400 | { |
401 | return (cfun |
402 | ? &cfun->language->base.x_stmt_tree |
403 | : &scope_chain->x_stmt_tree); |
404 | } |
405 | |
406 | /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */ |
407 | |
408 | static tree |
409 | maybe_cleanup_point_expr (tree expr) |
410 | { |
411 | if (!processing_template_decl && stmts_are_full_exprs_p ()) |
412 | expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr); |
413 | return expr; |
414 | } |
415 | |
416 | /* Like maybe_cleanup_point_expr except have the type of the new expression be |
417 | void so we don't need to create a temporary variable to hold the inner |
418 | expression. The reason why we do this is because the original type might be |
419 | an aggregate and we cannot create a temporary variable for that type. */ |
420 | |
421 | tree |
422 | maybe_cleanup_point_expr_void (tree expr) |
423 | { |
424 | if (!processing_template_decl && stmts_are_full_exprs_p ()) |
425 | expr = fold_build_cleanup_point_expr (void_type_node, expr); |
426 | return expr; |
427 | } |
428 | |
429 | |
430 | |
431 | /* Create a declaration statement for the declaration given by the DECL. */ |
432 | |
433 | void |
434 | add_decl_expr (tree decl) |
435 | { |
436 | tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl); |
437 | if (DECL_INITIAL (decl) |
438 | || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl)))) |
439 | r = maybe_cleanup_point_expr_void (r); |
440 | add_stmt (r); |
441 | } |
442 | |
443 | /* Finish a scope. */ |
444 | |
445 | tree |
446 | do_poplevel (tree stmt_list) |
447 | { |
448 | tree block = NULL; |
449 | |
450 | if (stmts_are_full_exprs_p ()) |
451 | block = poplevel (kept_level_p (), 1, 0); |
452 | |
453 | stmt_list = pop_stmt_list (stmt_list); |
454 | |
455 | if (!processing_template_decl) |
456 | { |
457 | stmt_list = c_build_bind_expr (input_location, block, stmt_list); |
458 | /* ??? See c_end_compound_stmt re statement expressions. */ |
459 | } |
460 | |
461 | return stmt_list; |
462 | } |
463 | |
464 | /* Begin a new scope. */ |
465 | |
466 | static tree |
467 | do_pushlevel (scope_kind sk) |
468 | { |
469 | tree ret = push_stmt_list (); |
470 | if (stmts_are_full_exprs_p ()) |
471 | begin_scope (sk, NULL); |
472 | return ret; |
473 | } |
474 | |
475 | /* Queue a cleanup. CLEANUP is an expression/statement to be executed |
476 | when the current scope is exited. EH_ONLY is true when this is not |
477 | meant to apply to normal control flow transfer. */ |
478 | |
479 | void |
480 | push_cleanup (tree decl, tree cleanup, bool eh_only) |
481 | { |
482 | tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl); |
483 | CLEANUP_EH_ONLY (stmt) = eh_only; |
484 | add_stmt (stmt); |
485 | CLEANUP_BODY (stmt) = push_stmt_list (); |
486 | } |
487 | |
488 | /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all |
489 | the current loops, represented by 'NULL_TREE' if we've seen a possible |
490 | exit, and 'error_mark_node' if not. This is currently used only to |
491 | suppress the warning about a function with no return statements, and |
492 | therefore we don't bother noting returns as possible exits. We also |
493 | don't bother with gotos. */ |
494 | |
495 | static void |
496 | begin_maybe_infinite_loop (tree cond) |
497 | { |
498 | /* Only track this while parsing a function, not during instantiation. */ |
499 | if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl) |
500 | && !processing_template_decl)) |
501 | return; |
502 | bool maybe_infinite = true; |
503 | if (cond) |
504 | { |
505 | cond = fold_non_dependent_expr (cond); |
506 | maybe_infinite = integer_nonzerop (cond); |
507 | } |
508 | vec_safe_push (cp_function_chain->infinite_loops, |
509 | maybe_infinite ? error_mark_node : NULL_TREE); |
510 | |
511 | } |
512 | |
513 | /* A break is a possible exit for the current loop. */ |
514 | |
515 | void |
516 | break_maybe_infinite_loop (void) |
517 | { |
518 | if (!cfun) |
519 | return; |
520 | cp_function_chain->infinite_loops->last() = NULL_TREE; |
521 | } |
522 | |
523 | /* If we reach the end of the loop without seeing a possible exit, we have |
524 | an infinite loop. */ |
525 | |
526 | static void |
527 | end_maybe_infinite_loop (tree cond) |
528 | { |
529 | if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl) |
530 | && !processing_template_decl)) |
531 | return; |
532 | tree current = cp_function_chain->infinite_loops->pop(); |
533 | if (current != NULL_TREE) |
534 | { |
535 | cond = fold_non_dependent_expr (cond); |
536 | if (integer_nonzerop (cond)) |
537 | current_function_infinite_loop = 1; |
538 | } |
539 | } |
540 | |
541 | |
542 | /* Begin a conditional that might contain a declaration. When generating |
543 | normal code, we want the declaration to appear before the statement |
544 | containing the conditional. When generating template code, we want the |
545 | conditional to be rendered as the raw DECL_EXPR. */ |
546 | |
547 | static void |
548 | begin_cond (tree *cond_p) |
549 | { |
550 | if (processing_template_decl) |
551 | *cond_p = push_stmt_list (); |
552 | } |
553 | |
554 | /* Finish such a conditional. */ |
555 | |
556 | static void |
557 | finish_cond (tree *cond_p, tree expr) |
558 | { |
559 | if (processing_template_decl) |
560 | { |
561 | tree cond = pop_stmt_list (*cond_p); |
562 | |
563 | if (expr == NULL_TREE) |
564 | /* Empty condition in 'for'. */ |
565 | gcc_assert (empty_expr_stmt_p (cond)); |
566 | else if (check_for_bare_parameter_packs (expr)) |
567 | expr = error_mark_node; |
568 | else if (!empty_expr_stmt_p (cond)) |
569 | expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr); |
570 | } |
571 | *cond_p = expr; |
572 | } |
573 | |
574 | /* If *COND_P specifies a conditional with a declaration, transform the |
575 | loop such that |
576 | while (A x = 42) { } |
577 | for (; A x = 42;) { } |
578 | becomes |
579 | while (true) { A x = 42; if (!x) break; } |
580 | for (;;) { A x = 42; if (!x) break; } |
581 | The statement list for BODY will be empty if the conditional did |
582 | not declare anything. */ |
583 | |
584 | static void |
585 | simplify_loop_decl_cond (tree *cond_p, tree body) |
586 | { |
587 | tree cond, if_stmt; |
588 | |
589 | if (!TREE_SIDE_EFFECTS (body)) |
590 | return; |
591 | |
592 | cond = *cond_p; |
593 | *cond_p = boolean_true_node; |
594 | |
595 | if_stmt = begin_if_stmt (); |
596 | cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error); |
597 | finish_if_stmt_cond (cond, if_stmt); |
598 | finish_break_stmt (); |
599 | finish_then_clause (if_stmt); |
600 | finish_if_stmt (if_stmt); |
601 | } |
602 | |
603 | /* Finish a goto-statement. */ |
604 | |
605 | tree |
606 | finish_goto_stmt (tree destination) |
607 | { |
608 | if (identifier_p (destination)) |
609 | destination = lookup_label (destination); |
610 | |
611 | /* We warn about unused labels with -Wunused. That means we have to |
612 | mark the used labels as used. */ |
613 | if (TREE_CODE (destination) == LABEL_DECL) |
614 | TREE_USED (destination) = 1; |
615 | else |
616 | { |
617 | destination = mark_rvalue_use (destination); |
618 | if (!processing_template_decl) |
619 | { |
620 | destination = cp_convert (ptr_type_node, destination, |
621 | tf_warning_or_error); |
622 | if (error_operand_p (destination)) |
623 | return NULL_TREE; |
624 | destination |
625 | = fold_build_cleanup_point_expr (TREE_TYPE (destination), |
626 | destination); |
627 | } |
628 | } |
629 | |
630 | check_goto (destination); |
631 | |
632 | add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN)); |
633 | return add_stmt (build_stmt (input_location, GOTO_EXPR, destination)); |
634 | } |
635 | |
636 | /* COND is the condition-expression for an if, while, etc., |
637 | statement. Convert it to a boolean value, if appropriate. |
638 | In addition, verify sequence points if -Wsequence-point is enabled. */ |
639 | |
640 | static tree |
641 | maybe_convert_cond (tree cond) |
642 | { |
643 | /* Empty conditions remain empty. */ |
644 | if (!cond) |
645 | return NULL_TREE; |
646 | |
647 | /* Wait until we instantiate templates before doing conversion. */ |
648 | if (processing_template_decl) |
649 | return cond; |
650 | |
651 | if (warn_sequence_point) |
652 | verify_sequence_points (cond); |
653 | |
654 | /* Do the conversion. */ |
655 | cond = convert_from_reference (cond); |
656 | |
657 | if (TREE_CODE (cond) == MODIFY_EXPR |
658 | && !TREE_NO_WARNING (cond) |
659 | && warn_parentheses) |
660 | { |
661 | warning_at (EXPR_LOC_OR_LOC (cond, input_location), OPT_Wparentheses, |
662 | "suggest parentheses around assignment used as truth value" ); |
663 | TREE_NO_WARNING (cond) = 1; |
664 | } |
665 | |
666 | return condition_conversion (cond); |
667 | } |
668 | |
669 | /* Finish an expression-statement, whose EXPRESSION is as indicated. */ |
670 | |
671 | tree |
672 | finish_expr_stmt (tree expr) |
673 | { |
674 | tree r = NULL_TREE; |
675 | location_t loc = EXPR_LOCATION (expr); |
676 | |
677 | if (expr != NULL_TREE) |
678 | { |
679 | /* If we ran into a problem, make sure we complained. */ |
680 | gcc_assert (expr != error_mark_node || seen_error ()); |
681 | |
682 | if (!processing_template_decl) |
683 | { |
684 | if (warn_sequence_point) |
685 | verify_sequence_points (expr); |
686 | expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error); |
687 | } |
688 | else if (!type_dependent_expression_p (expr)) |
689 | convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT, |
690 | tf_warning_or_error); |
691 | |
692 | if (check_for_bare_parameter_packs (expr)) |
693 | expr = error_mark_node; |
694 | |
695 | /* Simplification of inner statement expressions, compound exprs, |
696 | etc can result in us already having an EXPR_STMT. */ |
697 | if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) |
698 | { |
699 | if (TREE_CODE (expr) != EXPR_STMT) |
700 | expr = build_stmt (loc, EXPR_STMT, expr); |
701 | expr = maybe_cleanup_point_expr_void (expr); |
702 | } |
703 | |
704 | r = add_stmt (expr); |
705 | } |
706 | |
707 | return r; |
708 | } |
709 | |
710 | |
711 | /* Begin an if-statement. Returns a newly created IF_STMT if |
712 | appropriate. */ |
713 | |
714 | tree |
715 | begin_if_stmt (void) |
716 | { |
717 | tree r, scope; |
718 | scope = do_pushlevel (sk_cond); |
719 | r = build_stmt (input_location, IF_STMT, NULL_TREE, |
720 | NULL_TREE, NULL_TREE, scope); |
721 | current_binding_level->this_entity = r; |
722 | begin_cond (&IF_COND (r)); |
723 | return r; |
724 | } |
725 | |
726 | /* Process the COND of an if-statement, which may be given by |
727 | IF_STMT. */ |
728 | |
729 | tree |
730 | finish_if_stmt_cond (tree cond, tree if_stmt) |
731 | { |
732 | cond = maybe_convert_cond (cond); |
733 | if (IF_STMT_CONSTEXPR_P (if_stmt) |
734 | && require_constant_expression (cond) |
735 | && !value_dependent_expression_p (cond)) |
736 | { |
737 | cond = instantiate_non_dependent_expr (cond); |
738 | cond = cxx_constant_value (cond, NULL_TREE); |
739 | } |
740 | finish_cond (&IF_COND (if_stmt), cond); |
741 | add_stmt (if_stmt); |
742 | THEN_CLAUSE (if_stmt) = push_stmt_list (); |
743 | return cond; |
744 | } |
745 | |
746 | /* Finish the then-clause of an if-statement, which may be given by |
747 | IF_STMT. */ |
748 | |
749 | tree |
750 | finish_then_clause (tree if_stmt) |
751 | { |
752 | THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt)); |
753 | return if_stmt; |
754 | } |
755 | |
756 | /* Begin the else-clause of an if-statement. */ |
757 | |
758 | void |
759 | begin_else_clause (tree if_stmt) |
760 | { |
761 | ELSE_CLAUSE (if_stmt) = push_stmt_list (); |
762 | } |
763 | |
764 | /* Finish the else-clause of an if-statement, which may be given by |
765 | IF_STMT. */ |
766 | |
767 | void |
768 | finish_else_clause (tree if_stmt) |
769 | { |
770 | ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt)); |
771 | } |
772 | |
773 | /* Finish an if-statement. */ |
774 | |
775 | void |
776 | finish_if_stmt (tree if_stmt) |
777 | { |
778 | tree scope = IF_SCOPE (if_stmt); |
779 | IF_SCOPE (if_stmt) = NULL; |
780 | add_stmt (do_poplevel (scope)); |
781 | } |
782 | |
783 | /* Begin a while-statement. Returns a newly created WHILE_STMT if |
784 | appropriate. */ |
785 | |
786 | tree |
787 | begin_while_stmt (void) |
788 | { |
789 | tree r; |
790 | r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE); |
791 | add_stmt (r); |
792 | WHILE_BODY (r) = do_pushlevel (sk_block); |
793 | begin_cond (&WHILE_COND (r)); |
794 | return r; |
795 | } |
796 | |
797 | /* Process the COND of a while-statement, which may be given by |
798 | WHILE_STMT. */ |
799 | |
800 | void |
801 | finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep) |
802 | { |
803 | cond = maybe_convert_cond (cond); |
804 | finish_cond (&WHILE_COND (while_stmt), cond); |
805 | begin_maybe_infinite_loop (cond); |
806 | if (ivdep && cond != error_mark_node) |
807 | WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR, |
808 | TREE_TYPE (WHILE_COND (while_stmt)), |
809 | WHILE_COND (while_stmt), |
810 | build_int_cst (integer_type_node, |
811 | annot_expr_ivdep_kind), |
812 | integer_zero_node); |
813 | simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt)); |
814 | } |
815 | |
816 | /* Finish a while-statement, which may be given by WHILE_STMT. */ |
817 | |
818 | void |
819 | finish_while_stmt (tree while_stmt) |
820 | { |
821 | end_maybe_infinite_loop (boolean_true_node); |
822 | WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt)); |
823 | } |
824 | |
825 | /* Begin a do-statement. Returns a newly created DO_STMT if |
826 | appropriate. */ |
827 | |
828 | tree |
829 | begin_do_stmt (void) |
830 | { |
831 | tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE); |
832 | begin_maybe_infinite_loop (boolean_true_node); |
833 | add_stmt (r); |
834 | DO_BODY (r) = push_stmt_list (); |
835 | return r; |
836 | } |
837 | |
838 | /* Finish the body of a do-statement, which may be given by DO_STMT. */ |
839 | |
840 | void |
841 | finish_do_body (tree do_stmt) |
842 | { |
843 | tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt)); |
844 | |
845 | if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body)) |
846 | body = STATEMENT_LIST_TAIL (body)->stmt; |
847 | |
848 | if (IS_EMPTY_STMT (body)) |
849 | warning (OPT_Wempty_body, |
850 | "suggest explicit braces around empty body in %<do%> statement" ); |
851 | } |
852 | |
853 | /* Finish a do-statement, which may be given by DO_STMT, and whose |
854 | COND is as indicated. */ |
855 | |
856 | void |
857 | finish_do_stmt (tree cond, tree do_stmt, bool ivdep) |
858 | { |
859 | cond = maybe_convert_cond (cond); |
860 | end_maybe_infinite_loop (cond); |
861 | if (ivdep && cond != error_mark_node) |
862 | cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond, |
863 | build_int_cst (integer_type_node, annot_expr_ivdep_kind), |
864 | integer_zero_node); |
865 | DO_COND (do_stmt) = cond; |
866 | } |
867 | |
868 | /* Finish a return-statement. The EXPRESSION returned, if any, is as |
869 | indicated. */ |
870 | |
871 | tree |
872 | finish_return_stmt (tree expr) |
873 | { |
874 | tree r; |
875 | bool no_warning; |
876 | |
877 | expr = check_return_expr (expr, &no_warning); |
878 | |
879 | if (error_operand_p (expr) |
880 | || (flag_openmp && !check_omp_return ())) |
881 | { |
882 | /* Suppress -Wreturn-type for this function. */ |
883 | if (warn_return_type) |
884 | TREE_NO_WARNING (current_function_decl) = true; |
885 | return error_mark_node; |
886 | } |
887 | |
888 | if (!processing_template_decl) |
889 | { |
890 | if (warn_sequence_point) |
891 | verify_sequence_points (expr); |
892 | |
893 | if (DECL_DESTRUCTOR_P (current_function_decl) |
894 | || (DECL_CONSTRUCTOR_P (current_function_decl) |
895 | && targetm.cxx.cdtor_returns_this ())) |
896 | { |
897 | /* Similarly, all destructors must run destructors for |
898 | base-classes before returning. So, all returns in a |
899 | destructor get sent to the DTOR_LABEL; finish_function emits |
900 | code to return a value there. */ |
901 | return finish_goto_stmt (cdtor_label); |
902 | } |
903 | } |
904 | |
905 | r = build_stmt (input_location, RETURN_EXPR, expr); |
906 | TREE_NO_WARNING (r) |= no_warning; |
907 | r = maybe_cleanup_point_expr_void (r); |
908 | r = add_stmt (r); |
909 | |
910 | return r; |
911 | } |
912 | |
913 | /* Begin the scope of a for-statement or a range-for-statement. |
914 | Both the returned trees are to be used in a call to |
915 | begin_for_stmt or begin_range_for_stmt. */ |
916 | |
917 | tree |
918 | begin_for_scope (tree *init) |
919 | { |
920 | tree scope = NULL_TREE; |
921 | if (flag_new_for_scope > 0) |
922 | scope = do_pushlevel (sk_for); |
923 | |
924 | if (processing_template_decl) |
925 | *init = push_stmt_list (); |
926 | else |
927 | *init = NULL_TREE; |
928 | |
929 | return scope; |
930 | } |
931 | |
932 | /* Begin a for-statement. Returns a new FOR_STMT. |
933 | SCOPE and INIT should be the return of begin_for_scope, |
934 | or both NULL_TREE */ |
935 | |
936 | tree |
937 | begin_for_stmt (tree scope, tree init) |
938 | { |
939 | tree r; |
940 | |
941 | r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE, |
942 | NULL_TREE, NULL_TREE, NULL_TREE); |
943 | |
944 | if (scope == NULL_TREE) |
945 | { |
946 | gcc_assert (!init || !(flag_new_for_scope > 0)); |
947 | if (!init) |
948 | scope = begin_for_scope (&init); |
949 | } |
950 | FOR_INIT_STMT (r) = init; |
951 | FOR_SCOPE (r) = scope; |
952 | |
953 | return r; |
954 | } |
955 | |
956 | /* Finish the init-statement of a for-statement, which may be |
957 | given by FOR_STMT. */ |
958 | |
959 | void |
960 | finish_init_stmt (tree for_stmt) |
961 | { |
962 | if (processing_template_decl) |
963 | FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt)); |
964 | add_stmt (for_stmt); |
965 | FOR_BODY (for_stmt) = do_pushlevel (sk_block); |
966 | begin_cond (&FOR_COND (for_stmt)); |
967 | } |
968 | |
969 | /* Finish the COND of a for-statement, which may be given by |
970 | FOR_STMT. */ |
971 | |
972 | void |
973 | finish_for_cond (tree cond, tree for_stmt, bool ivdep) |
974 | { |
975 | cond = maybe_convert_cond (cond); |
976 | finish_cond (&FOR_COND (for_stmt), cond); |
977 | begin_maybe_infinite_loop (cond); |
978 | if (ivdep && cond != error_mark_node) |
979 | FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR, |
980 | TREE_TYPE (FOR_COND (for_stmt)), |
981 | FOR_COND (for_stmt), |
982 | build_int_cst (integer_type_node, |
983 | annot_expr_ivdep_kind), |
984 | integer_zero_node); |
985 | simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt)); |
986 | } |
987 | |
988 | /* Finish the increment-EXPRESSION in a for-statement, which may be |
989 | given by FOR_STMT. */ |
990 | |
991 | void |
992 | finish_for_expr (tree expr, tree for_stmt) |
993 | { |
994 | if (!expr) |
995 | return; |
996 | /* If EXPR is an overloaded function, issue an error; there is no |
997 | context available to use to perform overload resolution. */ |
998 | if (type_unknown_p (expr)) |
999 | { |
1000 | cxx_incomplete_type_error (expr, TREE_TYPE (expr)); |
1001 | expr = error_mark_node; |
1002 | } |
1003 | if (!processing_template_decl) |
1004 | { |
1005 | if (warn_sequence_point) |
1006 | verify_sequence_points (expr); |
1007 | expr = convert_to_void (expr, ICV_THIRD_IN_FOR, |
1008 | tf_warning_or_error); |
1009 | } |
1010 | else if (!type_dependent_expression_p (expr)) |
1011 | convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR, |
1012 | tf_warning_or_error); |
1013 | expr = maybe_cleanup_point_expr_void (expr); |
1014 | if (check_for_bare_parameter_packs (expr)) |
1015 | expr = error_mark_node; |
1016 | FOR_EXPR (for_stmt) = expr; |
1017 | } |
1018 | |
1019 | /* Finish the body of a for-statement, which may be given by |
1020 | FOR_STMT. The increment-EXPR for the loop must be |
1021 | provided. |
1022 | It can also finish RANGE_FOR_STMT. */ |
1023 | |
1024 | void |
1025 | finish_for_stmt (tree for_stmt) |
1026 | { |
1027 | end_maybe_infinite_loop (boolean_true_node); |
1028 | |
1029 | if (TREE_CODE (for_stmt) == RANGE_FOR_STMT) |
1030 | RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt)); |
1031 | else |
1032 | FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt)); |
1033 | |
1034 | /* Pop the scope for the body of the loop. */ |
1035 | if (flag_new_for_scope > 0) |
1036 | { |
1037 | tree scope; |
1038 | tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT |
1039 | ? &RANGE_FOR_SCOPE (for_stmt) |
1040 | : &FOR_SCOPE (for_stmt)); |
1041 | scope = *scope_ptr; |
1042 | *scope_ptr = NULL; |
1043 | add_stmt (do_poplevel (scope)); |
1044 | } |
1045 | } |
1046 | |
1047 | /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT. |
1048 | SCOPE and INIT should be the return of begin_for_scope, |
1049 | or both NULL_TREE . |
1050 | To finish it call finish_for_stmt(). */ |
1051 | |
1052 | tree |
1053 | begin_range_for_stmt (tree scope, tree init) |
1054 | { |
1055 | tree r; |
1056 | |
1057 | begin_maybe_infinite_loop (boolean_false_node); |
1058 | |
1059 | r = build_stmt (input_location, RANGE_FOR_STMT, |
1060 | NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE); |
1061 | |
1062 | if (scope == NULL_TREE) |
1063 | { |
1064 | gcc_assert (!init || !(flag_new_for_scope > 0)); |
1065 | if (!init) |
1066 | scope = begin_for_scope (&init); |
1067 | } |
1068 | |
1069 | /* RANGE_FOR_STMTs do not use nor save the init tree, so we |
1070 | pop it now. */ |
1071 | if (init) |
1072 | pop_stmt_list (init); |
1073 | RANGE_FOR_SCOPE (r) = scope; |
1074 | |
1075 | return r; |
1076 | } |
1077 | |
1078 | /* Finish the head of a range-based for statement, which may |
1079 | be given by RANGE_FOR_STMT. DECL must be the declaration |
1080 | and EXPR must be the loop expression. */ |
1081 | |
1082 | void |
1083 | finish_range_for_decl (tree range_for_stmt, tree decl, tree expr) |
1084 | { |
1085 | RANGE_FOR_DECL (range_for_stmt) = decl; |
1086 | RANGE_FOR_EXPR (range_for_stmt) = expr; |
1087 | add_stmt (range_for_stmt); |
1088 | RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block); |
1089 | } |
1090 | |
1091 | /* Finish a break-statement. */ |
1092 | |
1093 | tree |
1094 | finish_break_stmt (void) |
1095 | { |
1096 | /* In switch statements break is sometimes stylistically used after |
1097 | a return statement. This can lead to spurious warnings about |
1098 | control reaching the end of a non-void function when it is |
1099 | inlined. Note that we are calling block_may_fallthru with |
1100 | language specific tree nodes; this works because |
1101 | block_may_fallthru returns true when given something it does not |
1102 | understand. */ |
1103 | if (!block_may_fallthru (cur_stmt_list)) |
1104 | return void_node; |
1105 | note_break_stmt (); |
1106 | return add_stmt (build_stmt (input_location, BREAK_STMT)); |
1107 | } |
1108 | |
1109 | /* Finish a continue-statement. */ |
1110 | |
1111 | tree |
1112 | finish_continue_stmt (void) |
1113 | { |
1114 | return add_stmt (build_stmt (input_location, CONTINUE_STMT)); |
1115 | } |
1116 | |
1117 | /* Begin a switch-statement. Returns a new SWITCH_STMT if |
1118 | appropriate. */ |
1119 | |
1120 | tree |
1121 | begin_switch_stmt (void) |
1122 | { |
1123 | tree r, scope; |
1124 | |
1125 | scope = do_pushlevel (sk_cond); |
1126 | r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope); |
1127 | |
1128 | begin_cond (&SWITCH_STMT_COND (r)); |
1129 | |
1130 | return r; |
1131 | } |
1132 | |
1133 | /* Finish the cond of a switch-statement. */ |
1134 | |
1135 | void |
1136 | finish_switch_cond (tree cond, tree switch_stmt) |
1137 | { |
1138 | tree orig_type = NULL; |
1139 | |
1140 | if (!processing_template_decl) |
1141 | { |
1142 | /* Convert the condition to an integer or enumeration type. */ |
1143 | cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true); |
1144 | if (cond == NULL_TREE) |
1145 | { |
1146 | error ("switch quantity not an integer" ); |
1147 | cond = error_mark_node; |
1148 | } |
1149 | /* We want unlowered type here to handle enum bit-fields. */ |
1150 | orig_type = unlowered_expr_type (cond); |
1151 | if (TREE_CODE (orig_type) != ENUMERAL_TYPE) |
1152 | orig_type = TREE_TYPE (cond); |
1153 | if (cond != error_mark_node) |
1154 | { |
1155 | /* [stmt.switch] |
1156 | |
1157 | Integral promotions are performed. */ |
1158 | cond = perform_integral_promotions (cond); |
1159 | cond = maybe_cleanup_point_expr (cond); |
1160 | } |
1161 | } |
1162 | if (check_for_bare_parameter_packs (cond)) |
1163 | cond = error_mark_node; |
1164 | else if (!processing_template_decl && warn_sequence_point) |
1165 | verify_sequence_points (cond); |
1166 | |
1167 | finish_cond (&SWITCH_STMT_COND (switch_stmt), cond); |
1168 | SWITCH_STMT_TYPE (switch_stmt) = orig_type; |
1169 | add_stmt (switch_stmt); |
1170 | push_switch (switch_stmt); |
1171 | SWITCH_STMT_BODY (switch_stmt) = push_stmt_list (); |
1172 | } |
1173 | |
1174 | /* Finish the body of a switch-statement, which may be given by |
1175 | SWITCH_STMT. The COND to switch on is indicated. */ |
1176 | |
1177 | void |
1178 | finish_switch_stmt (tree switch_stmt) |
1179 | { |
1180 | tree scope; |
1181 | |
1182 | SWITCH_STMT_BODY (switch_stmt) = |
1183 | pop_stmt_list (SWITCH_STMT_BODY (switch_stmt)); |
1184 | pop_switch (); |
1185 | |
1186 | scope = SWITCH_STMT_SCOPE (switch_stmt); |
1187 | SWITCH_STMT_SCOPE (switch_stmt) = NULL; |
1188 | add_stmt (do_poplevel (scope)); |
1189 | } |
1190 | |
1191 | /* Begin a try-block. Returns a newly-created TRY_BLOCK if |
1192 | appropriate. */ |
1193 | |
1194 | tree |
1195 | begin_try_block (void) |
1196 | { |
1197 | tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE); |
1198 | add_stmt (r); |
1199 | TRY_STMTS (r) = push_stmt_list (); |
1200 | return r; |
1201 | } |
1202 | |
1203 | /* Likewise, for a function-try-block. The block returned in |
1204 | *COMPOUND_STMT is an artificial outer scope, containing the |
1205 | function-try-block. */ |
1206 | |
1207 | tree |
1208 | begin_function_try_block (tree *compound_stmt) |
1209 | { |
1210 | tree r; |
1211 | /* This outer scope does not exist in the C++ standard, but we need |
1212 | a place to put __FUNCTION__ and similar variables. */ |
1213 | *compound_stmt = begin_compound_stmt (0); |
1214 | r = begin_try_block (); |
1215 | FN_TRY_BLOCK_P (r) = 1; |
1216 | return r; |
1217 | } |
1218 | |
1219 | /* Finish a try-block, which may be given by TRY_BLOCK. */ |
1220 | |
1221 | void |
1222 | finish_try_block (tree try_block) |
1223 | { |
1224 | TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); |
1225 | TRY_HANDLERS (try_block) = push_stmt_list (); |
1226 | } |
1227 | |
1228 | /* Finish the body of a cleanup try-block, which may be given by |
1229 | TRY_BLOCK. */ |
1230 | |
1231 | void |
1232 | finish_cleanup_try_block (tree try_block) |
1233 | { |
1234 | TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); |
1235 | } |
1236 | |
1237 | /* Finish an implicitly generated try-block, with a cleanup is given |
1238 | by CLEANUP. */ |
1239 | |
1240 | void |
1241 | finish_cleanup (tree cleanup, tree try_block) |
1242 | { |
1243 | TRY_HANDLERS (try_block) = cleanup; |
1244 | CLEANUP_P (try_block) = 1; |
1245 | } |
1246 | |
1247 | /* Likewise, for a function-try-block. */ |
1248 | |
1249 | void |
1250 | finish_function_try_block (tree try_block) |
1251 | { |
1252 | finish_try_block (try_block); |
1253 | /* FIXME : something queer about CTOR_INITIALIZER somehow following |
1254 | the try block, but moving it inside. */ |
1255 | in_function_try_handler = 1; |
1256 | } |
1257 | |
1258 | /* Finish a handler-sequence for a try-block, which may be given by |
1259 | TRY_BLOCK. */ |
1260 | |
1261 | void |
1262 | finish_handler_sequence (tree try_block) |
1263 | { |
1264 | TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block)); |
1265 | check_handlers (TRY_HANDLERS (try_block)); |
1266 | } |
1267 | |
1268 | /* Finish the handler-seq for a function-try-block, given by |
1269 | TRY_BLOCK. COMPOUND_STMT is the outer block created by |
1270 | begin_function_try_block. */ |
1271 | |
1272 | void |
1273 | finish_function_handler_sequence (tree try_block, tree compound_stmt) |
1274 | { |
1275 | in_function_try_handler = 0; |
1276 | finish_handler_sequence (try_block); |
1277 | finish_compound_stmt (compound_stmt); |
1278 | } |
1279 | |
1280 | /* Begin a handler. Returns a HANDLER if appropriate. */ |
1281 | |
1282 | tree |
1283 | begin_handler (void) |
1284 | { |
1285 | tree r; |
1286 | |
1287 | r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE); |
1288 | add_stmt (r); |
1289 | |
1290 | /* Create a binding level for the eh_info and the exception object |
1291 | cleanup. */ |
1292 | HANDLER_BODY (r) = do_pushlevel (sk_catch); |
1293 | |
1294 | return r; |
1295 | } |
1296 | |
1297 | /* Finish the handler-parameters for a handler, which may be given by |
1298 | HANDLER. DECL is the declaration for the catch parameter, or NULL |
1299 | if this is a `catch (...)' clause. */ |
1300 | |
1301 | void |
1302 | finish_handler_parms (tree decl, tree handler) |
1303 | { |
1304 | tree type = NULL_TREE; |
1305 | if (processing_template_decl) |
1306 | { |
1307 | if (decl) |
1308 | { |
1309 | decl = pushdecl (decl); |
1310 | decl = push_template_decl (decl); |
1311 | HANDLER_PARMS (handler) = decl; |
1312 | type = TREE_TYPE (decl); |
1313 | } |
1314 | } |
1315 | else |
1316 | { |
1317 | type = expand_start_catch_block (decl); |
1318 | if (warn_catch_value |
1319 | && type != NULL_TREE |
1320 | && type != error_mark_node |
1321 | && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE) |
1322 | { |
1323 | tree orig_type = TREE_TYPE (decl); |
1324 | if (CLASS_TYPE_P (orig_type)) |
1325 | { |
1326 | if (TYPE_POLYMORPHIC_P (orig_type)) |
1327 | warning (OPT_Wcatch_value_, |
1328 | "catching polymorphic type %q#T by value" , orig_type); |
1329 | else if (warn_catch_value > 1) |
1330 | warning (OPT_Wcatch_value_, |
1331 | "catching type %q#T by value" , orig_type); |
1332 | } |
1333 | else if (warn_catch_value > 2) |
1334 | warning (OPT_Wcatch_value_, |
1335 | "catching non-reference type %q#T" , orig_type); |
1336 | } |
1337 | } |
1338 | HANDLER_TYPE (handler) = type; |
1339 | } |
1340 | |
1341 | /* Finish a handler, which may be given by HANDLER. The BLOCKs are |
1342 | the return value from the matching call to finish_handler_parms. */ |
1343 | |
1344 | void |
1345 | finish_handler (tree handler) |
1346 | { |
1347 | if (!processing_template_decl) |
1348 | expand_end_catch_block (); |
1349 | HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler)); |
1350 | } |
1351 | |
1352 | /* Begin a compound statement. FLAGS contains some bits that control the |
1353 | behavior and context. If BCS_NO_SCOPE is set, the compound statement |
1354 | does not define a scope. If BCS_FN_BODY is set, this is the outermost |
1355 | block of a function. If BCS_TRY_BLOCK is set, this is the block |
1356 | created on behalf of a TRY statement. Returns a token to be passed to |
1357 | finish_compound_stmt. */ |
1358 | |
1359 | tree |
1360 | begin_compound_stmt (unsigned int flags) |
1361 | { |
1362 | tree r; |
1363 | |
1364 | if (flags & BCS_NO_SCOPE) |
1365 | { |
1366 | r = push_stmt_list (); |
1367 | STATEMENT_LIST_NO_SCOPE (r) = 1; |
1368 | |
1369 | /* Normally, we try hard to keep the BLOCK for a statement-expression. |
1370 | But, if it's a statement-expression with a scopeless block, there's |
1371 | nothing to keep, and we don't want to accidentally keep a block |
1372 | *inside* the scopeless block. */ |
1373 | keep_next_level (false); |
1374 | } |
1375 | else |
1376 | { |
1377 | scope_kind sk = sk_block; |
1378 | if (flags & BCS_TRY_BLOCK) |
1379 | sk = sk_try; |
1380 | else if (flags & BCS_TRANSACTION) |
1381 | sk = sk_transaction; |
1382 | r = do_pushlevel (sk); |
1383 | } |
1384 | |
1385 | /* When processing a template, we need to remember where the braces were, |
1386 | so that we can set up identical scopes when instantiating the template |
1387 | later. BIND_EXPR is a handy candidate for this. |
1388 | Note that do_poplevel won't create a BIND_EXPR itself here (and thus |
1389 | result in nested BIND_EXPRs), since we don't build BLOCK nodes when |
1390 | processing templates. */ |
1391 | if (processing_template_decl) |
1392 | { |
1393 | r = build3 (BIND_EXPR, NULL, NULL, r, NULL); |
1394 | BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0; |
1395 | BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0; |
1396 | TREE_SIDE_EFFECTS (r) = 1; |
1397 | } |
1398 | |
1399 | return r; |
1400 | } |
1401 | |
1402 | /* Finish a compound-statement, which is given by STMT. */ |
1403 | |
1404 | void |
1405 | finish_compound_stmt (tree stmt) |
1406 | { |
1407 | if (TREE_CODE (stmt) == BIND_EXPR) |
1408 | { |
1409 | tree body = do_poplevel (BIND_EXPR_BODY (stmt)); |
1410 | /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special, |
1411 | discard the BIND_EXPR so it can be merged with the containing |
1412 | STATEMENT_LIST. */ |
1413 | if (TREE_CODE (body) == STATEMENT_LIST |
1414 | && STATEMENT_LIST_HEAD (body) == NULL |
1415 | && !BIND_EXPR_BODY_BLOCK (stmt) |
1416 | && !BIND_EXPR_TRY_BLOCK (stmt)) |
1417 | stmt = body; |
1418 | else |
1419 | BIND_EXPR_BODY (stmt) = body; |
1420 | } |
1421 | else if (STATEMENT_LIST_NO_SCOPE (stmt)) |
1422 | stmt = pop_stmt_list (stmt); |
1423 | else |
1424 | { |
1425 | /* Destroy any ObjC "super" receivers that may have been |
1426 | created. */ |
1427 | objc_clear_super_receiver (); |
1428 | |
1429 | stmt = do_poplevel (stmt); |
1430 | } |
1431 | |
1432 | /* ??? See c_end_compound_stmt wrt statement expressions. */ |
1433 | add_stmt (stmt); |
1434 | } |
1435 | |
1436 | /* Finish an asm-statement, whose components are a STRING, some |
1437 | OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some |
1438 | LABELS. Also note whether the asm-statement should be |
1439 | considered volatile. */ |
1440 | |
1441 | tree |
1442 | finish_asm_stmt (int volatile_p, tree string, tree output_operands, |
1443 | tree input_operands, tree clobbers, tree labels) |
1444 | { |
1445 | tree r; |
1446 | tree t; |
1447 | int ninputs = list_length (input_operands); |
1448 | int noutputs = list_length (output_operands); |
1449 | |
1450 | if (!processing_template_decl) |
1451 | { |
1452 | const char *constraint; |
1453 | const char **oconstraints; |
1454 | bool allows_mem, allows_reg, is_inout; |
1455 | tree operand; |
1456 | int i; |
1457 | |
1458 | oconstraints = XALLOCAVEC (const char *, noutputs); |
1459 | |
1460 | string = resolve_asm_operand_names (string, output_operands, |
1461 | input_operands, labels); |
1462 | |
1463 | for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i) |
1464 | { |
1465 | operand = TREE_VALUE (t); |
1466 | |
1467 | /* ??? Really, this should not be here. Users should be using a |
1468 | proper lvalue, dammit. But there's a long history of using |
1469 | casts in the output operands. In cases like longlong.h, this |
1470 | becomes a primitive form of typechecking -- if the cast can be |
1471 | removed, then the output operand had a type of the proper width; |
1472 | otherwise we'll get an error. Gross, but ... */ |
1473 | STRIP_NOPS (operand); |
1474 | |
1475 | operand = mark_lvalue_use (operand); |
1476 | |
1477 | if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error)) |
1478 | operand = error_mark_node; |
1479 | |
1480 | if (operand != error_mark_node |
1481 | && (TREE_READONLY (operand) |
1482 | || CP_TYPE_CONST_P (TREE_TYPE (operand)) |
1483 | /* Functions are not modifiable, even though they are |
1484 | lvalues. */ |
1485 | || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE |
1486 | || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE |
1487 | /* If it's an aggregate and any field is const, then it is |
1488 | effectively const. */ |
1489 | || (CLASS_TYPE_P (TREE_TYPE (operand)) |
1490 | && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand))))) |
1491 | cxx_readonly_error (operand, lv_asm); |
1492 | |
1493 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); |
1494 | oconstraints[i] = constraint; |
1495 | |
1496 | if (parse_output_constraint (&constraint, i, ninputs, noutputs, |
1497 | &allows_mem, &allows_reg, &is_inout)) |
1498 | { |
1499 | /* If the operand is going to end up in memory, |
1500 | mark it addressable. */ |
1501 | if (!allows_reg && !cxx_mark_addressable (operand)) |
1502 | operand = error_mark_node; |
1503 | } |
1504 | else |
1505 | operand = error_mark_node; |
1506 | |
1507 | TREE_VALUE (t) = operand; |
1508 | } |
1509 | |
1510 | for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t)) |
1511 | { |
1512 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); |
1513 | bool constraint_parsed |
1514 | = parse_input_constraint (&constraint, i, ninputs, noutputs, 0, |
1515 | oconstraints, &allows_mem, &allows_reg); |
1516 | /* If the operand is going to end up in memory, don't call |
1517 | decay_conversion. */ |
1518 | if (constraint_parsed && !allows_reg && allows_mem) |
1519 | operand = mark_lvalue_use (TREE_VALUE (t)); |
1520 | else |
1521 | operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error); |
1522 | |
1523 | /* If the type of the operand hasn't been determined (e.g., |
1524 | because it involves an overloaded function), then issue |
1525 | an error message. There's no context available to |
1526 | resolve the overloading. */ |
1527 | if (TREE_TYPE (operand) == unknown_type_node) |
1528 | { |
1529 | error ("type of asm operand %qE could not be determined" , |
1530 | TREE_VALUE (t)); |
1531 | operand = error_mark_node; |
1532 | } |
1533 | |
1534 | if (constraint_parsed) |
1535 | { |
1536 | /* If the operand is going to end up in memory, |
1537 | mark it addressable. */ |
1538 | if (!allows_reg && allows_mem) |
1539 | { |
1540 | /* Strip the nops as we allow this case. FIXME, this really |
1541 | should be rejected or made deprecated. */ |
1542 | STRIP_NOPS (operand); |
1543 | if (!cxx_mark_addressable (operand)) |
1544 | operand = error_mark_node; |
1545 | } |
1546 | else if (!allows_reg && !allows_mem) |
1547 | { |
1548 | /* If constraint allows neither register nor memory, |
1549 | try harder to get a constant. */ |
1550 | tree constop = maybe_constant_value (operand); |
1551 | if (TREE_CONSTANT (constop)) |
1552 | operand = constop; |
1553 | } |
1554 | } |
1555 | else |
1556 | operand = error_mark_node; |
1557 | |
1558 | TREE_VALUE (t) = operand; |
1559 | } |
1560 | } |
1561 | |
1562 | r = build_stmt (input_location, ASM_EXPR, string, |
1563 | output_operands, input_operands, |
1564 | clobbers, labels); |
1565 | ASM_VOLATILE_P (r) = volatile_p || noutputs == 0; |
1566 | r = maybe_cleanup_point_expr_void (r); |
1567 | return add_stmt (r); |
1568 | } |
1569 | |
1570 | /* Finish a label with the indicated NAME. Returns the new label. */ |
1571 | |
1572 | tree |
1573 | finish_label_stmt (tree name) |
1574 | { |
1575 | tree decl = define_label (input_location, name); |
1576 | |
1577 | if (decl == error_mark_node) |
1578 | return error_mark_node; |
1579 | |
1580 | add_stmt (build_stmt (input_location, LABEL_EXPR, decl)); |
1581 | |
1582 | return decl; |
1583 | } |
1584 | |
1585 | /* Finish a series of declarations for local labels. G++ allows users |
1586 | to declare "local" labels, i.e., labels with scope. This extension |
1587 | is useful when writing code involving statement-expressions. */ |
1588 | |
1589 | void |
1590 | finish_label_decl (tree name) |
1591 | { |
1592 | if (!at_function_scope_p ()) |
1593 | { |
1594 | error ("__label__ declarations are only allowed in function scopes" ); |
1595 | return; |
1596 | } |
1597 | |
1598 | add_decl_expr (declare_local_label (name)); |
1599 | } |
1600 | |
1601 | /* When DECL goes out of scope, make sure that CLEANUP is executed. */ |
1602 | |
1603 | void |
1604 | finish_decl_cleanup (tree decl, tree cleanup) |
1605 | { |
1606 | push_cleanup (decl, cleanup, false); |
1607 | } |
1608 | |
1609 | /* If the current scope exits with an exception, run CLEANUP. */ |
1610 | |
1611 | void |
1612 | finish_eh_cleanup (tree cleanup) |
1613 | { |
1614 | push_cleanup (NULL, cleanup, true); |
1615 | } |
1616 | |
1617 | /* The MEM_INITS is a list of mem-initializers, in reverse of the |
1618 | order they were written by the user. Each node is as for |
1619 | emit_mem_initializers. */ |
1620 | |
1621 | void |
1622 | finish_mem_initializers (tree mem_inits) |
1623 | { |
1624 | /* Reorder the MEM_INITS so that they are in the order they appeared |
1625 | in the source program. */ |
1626 | mem_inits = nreverse (mem_inits); |
1627 | |
1628 | if (processing_template_decl) |
1629 | { |
1630 | tree mem; |
1631 | |
1632 | for (mem = mem_inits; mem; mem = TREE_CHAIN (mem)) |
1633 | { |
1634 | /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the |
1635 | check for bare parameter packs in the TREE_VALUE, because |
1636 | any parameter packs in the TREE_VALUE have already been |
1637 | bound as part of the TREE_PURPOSE. See |
1638 | make_pack_expansion for more information. */ |
1639 | if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION |
1640 | && check_for_bare_parameter_packs (TREE_VALUE (mem))) |
1641 | TREE_VALUE (mem) = error_mark_node; |
1642 | } |
1643 | |
1644 | add_stmt (build_min_nt_loc (UNKNOWN_LOCATION, |
1645 | CTOR_INITIALIZER, mem_inits)); |
1646 | } |
1647 | else |
1648 | emit_mem_initializers (mem_inits); |
1649 | } |
1650 | |
1651 | /* Obfuscate EXPR if it looks like an id-expression or member access so |
1652 | that the call to finish_decltype in do_auto_deduction will give the |
1653 | right result. */ |
1654 | |
1655 | tree |
1656 | force_paren_expr (tree expr) |
1657 | { |
1658 | /* This is only needed for decltype(auto) in C++14. */ |
1659 | if (cxx_dialect < cxx14) |
1660 | return expr; |
1661 | |
1662 | /* If we're in unevaluated context, we can't be deducing a |
1663 | return/initializer type, so we don't need to mess with this. */ |
1664 | if (cp_unevaluated_operand) |
1665 | return expr; |
1666 | |
1667 | if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF |
1668 | && TREE_CODE (expr) != SCOPE_REF) |
1669 | return expr; |
1670 | |
1671 | if (TREE_CODE (expr) == COMPONENT_REF |
1672 | || TREE_CODE (expr) == SCOPE_REF) |
1673 | REF_PARENTHESIZED_P (expr) = true; |
1674 | else if (type_dependent_expression_p (expr)) |
1675 | expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr); |
1676 | else if (VAR_P (expr) && DECL_HARD_REGISTER (expr)) |
1677 | /* We can't bind a hard register variable to a reference. */; |
1678 | else |
1679 | { |
1680 | cp_lvalue_kind kind = lvalue_kind (expr); |
1681 | if ((kind & ~clk_class) != clk_none) |
1682 | { |
1683 | tree type = unlowered_expr_type (expr); |
1684 | bool rval = !!(kind & clk_rvalueref); |
1685 | type = cp_build_reference_type (type, rval); |
1686 | /* This inhibits warnings in, eg, cxx_mark_addressable |
1687 | (c++/60955). */ |
1688 | warning_sentinel s (extra_warnings); |
1689 | expr = build_static_cast (type, expr, tf_error); |
1690 | if (expr != error_mark_node) |
1691 | REF_PARENTHESIZED_P (expr) = true; |
1692 | } |
1693 | } |
1694 | |
1695 | return expr; |
1696 | } |
1697 | |
1698 | /* If T is an id-expression obfuscated by force_paren_expr, undo the |
1699 | obfuscation and return the underlying id-expression. Otherwise |
1700 | return T. */ |
1701 | |
1702 | tree |
1703 | maybe_undo_parenthesized_ref (tree t) |
1704 | { |
1705 | if (cxx_dialect >= cxx14 |
1706 | && INDIRECT_REF_P (t) |
1707 | && REF_PARENTHESIZED_P (t)) |
1708 | { |
1709 | t = TREE_OPERAND (t, 0); |
1710 | while (TREE_CODE (t) == NON_LVALUE_EXPR |
1711 | || TREE_CODE (t) == NOP_EXPR) |
1712 | t = TREE_OPERAND (t, 0); |
1713 | |
1714 | gcc_assert (TREE_CODE (t) == ADDR_EXPR |
1715 | || TREE_CODE (t) == STATIC_CAST_EXPR); |
1716 | t = TREE_OPERAND (t, 0); |
1717 | } |
1718 | |
1719 | return t; |
1720 | } |
1721 | |
1722 | /* Finish a parenthesized expression EXPR. */ |
1723 | |
1724 | cp_expr |
1725 | finish_parenthesized_expr (cp_expr expr) |
1726 | { |
1727 | if (EXPR_P (expr)) |
1728 | /* This inhibits warnings in c_common_truthvalue_conversion. */ |
1729 | TREE_NO_WARNING (expr) = 1; |
1730 | |
1731 | if (TREE_CODE (expr) == OFFSET_REF |
1732 | || TREE_CODE (expr) == SCOPE_REF) |
1733 | /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be |
1734 | enclosed in parentheses. */ |
1735 | PTRMEM_OK_P (expr) = 0; |
1736 | |
1737 | if (TREE_CODE (expr) == STRING_CST) |
1738 | PAREN_STRING_LITERAL_P (expr) = 1; |
1739 | |
1740 | expr = cp_expr (force_paren_expr (expr), expr.get_location ()); |
1741 | |
1742 | return expr; |
1743 | } |
1744 | |
1745 | /* Finish a reference to a non-static data member (DECL) that is not |
1746 | preceded by `.' or `->'. */ |
1747 | |
1748 | tree |
1749 | finish_non_static_data_member (tree decl, tree object, tree qualifying_scope) |
1750 | { |
1751 | gcc_assert (TREE_CODE (decl) == FIELD_DECL); |
1752 | bool try_omp_private = !object && omp_private_member_map; |
1753 | tree ret; |
1754 | |
1755 | if (!object) |
1756 | { |
1757 | tree scope = qualifying_scope; |
1758 | if (scope == NULL_TREE) |
1759 | scope = context_for_name_lookup (decl); |
1760 | object = maybe_dummy_object (scope, NULL); |
1761 | } |
1762 | |
1763 | object = maybe_resolve_dummy (object, true); |
1764 | if (object == error_mark_node) |
1765 | return error_mark_node; |
1766 | |
1767 | /* DR 613/850: Can use non-static data members without an associated |
1768 | object in sizeof/decltype/alignof. */ |
1769 | if (is_dummy_object (object) && cp_unevaluated_operand == 0 |
1770 | && (!processing_template_decl || !current_class_ref)) |
1771 | { |
1772 | if (current_function_decl |
1773 | && DECL_STATIC_FUNCTION_P (current_function_decl)) |
1774 | error ("invalid use of member %qD in static member function" , decl); |
1775 | else |
1776 | error ("invalid use of non-static data member %qD" , decl); |
1777 | inform (DECL_SOURCE_LOCATION (decl), "declared here" ); |
1778 | |
1779 | return error_mark_node; |
1780 | } |
1781 | |
1782 | if (current_class_ptr) |
1783 | TREE_USED (current_class_ptr) = 1; |
1784 | if (processing_template_decl && !qualifying_scope) |
1785 | { |
1786 | tree type = TREE_TYPE (decl); |
1787 | |
1788 | if (TREE_CODE (type) == REFERENCE_TYPE) |
1789 | /* Quals on the object don't matter. */; |
1790 | else if (PACK_EXPANSION_P (type)) |
1791 | /* Don't bother trying to represent this. */ |
1792 | type = NULL_TREE; |
1793 | else |
1794 | { |
1795 | /* Set the cv qualifiers. */ |
1796 | int quals = cp_type_quals (TREE_TYPE (object)); |
1797 | |
1798 | if (DECL_MUTABLE_P (decl)) |
1799 | quals &= ~TYPE_QUAL_CONST; |
1800 | |
1801 | quals |= cp_type_quals (TREE_TYPE (decl)); |
1802 | type = cp_build_qualified_type (type, quals); |
1803 | } |
1804 | |
1805 | ret = (convert_from_reference |
1806 | (build_min (COMPONENT_REF, type, object, decl, NULL_TREE))); |
1807 | } |
1808 | /* If PROCESSING_TEMPLATE_DECL is nonzero here, then |
1809 | QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF |
1810 | for now. */ |
1811 | else if (processing_template_decl) |
1812 | ret = build_qualified_name (TREE_TYPE (decl), |
1813 | qualifying_scope, |
1814 | decl, |
1815 | /*template_p=*/false); |
1816 | else |
1817 | { |
1818 | tree access_type = TREE_TYPE (object); |
1819 | |
1820 | perform_or_defer_access_check (TYPE_BINFO (access_type), decl, |
1821 | decl, tf_warning_or_error); |
1822 | |
1823 | /* If the data member was named `C::M', convert `*this' to `C' |
1824 | first. */ |
1825 | if (qualifying_scope) |
1826 | { |
1827 | tree binfo = NULL_TREE; |
1828 | object = build_scoped_ref (object, qualifying_scope, |
1829 | &binfo); |
1830 | } |
1831 | |
1832 | ret = build_class_member_access_expr (object, decl, |
1833 | /*access_path=*/NULL_TREE, |
1834 | /*preserve_reference=*/false, |
1835 | tf_warning_or_error); |
1836 | } |
1837 | if (try_omp_private) |
1838 | { |
1839 | tree *v = omp_private_member_map->get (decl); |
1840 | if (v) |
1841 | ret = convert_from_reference (*v); |
1842 | } |
1843 | return ret; |
1844 | } |
1845 | |
1846 | /* If we are currently parsing a template and we encountered a typedef |
1847 | TYPEDEF_DECL that is being accessed though CONTEXT, this function |
1848 | adds the typedef to a list tied to the current template. |
1849 | At template instantiation time, that list is walked and access check |
1850 | performed for each typedef. |
1851 | LOCATION is the location of the usage point of TYPEDEF_DECL. */ |
1852 | |
1853 | void |
1854 | add_typedef_to_current_template_for_access_check (tree typedef_decl, |
1855 | tree context, |
1856 | location_t location) |
1857 | { |
1858 | tree template_info = NULL; |
1859 | tree cs = current_scope (); |
1860 | |
1861 | if (!is_typedef_decl (typedef_decl) |
1862 | || !context |
1863 | || !CLASS_TYPE_P (context) |
1864 | || !cs) |
1865 | return; |
1866 | |
1867 | if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL) |
1868 | template_info = get_template_info (cs); |
1869 | |
1870 | if (template_info |
1871 | && TI_TEMPLATE (template_info) |
1872 | && !currently_open_class (context)) |
1873 | append_type_to_template_for_access_check (cs, typedef_decl, |
1874 | context, location); |
1875 | } |
1876 | |
1877 | /* DECL was the declaration to which a qualified-id resolved. Issue |
1878 | an error message if it is not accessible. If OBJECT_TYPE is |
1879 | non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the |
1880 | type of `*x', or `x', respectively. If the DECL was named as |
1881 | `A::B' then NESTED_NAME_SPECIFIER is `A'. */ |
1882 | |
1883 | void |
1884 | check_accessibility_of_qualified_id (tree decl, |
1885 | tree object_type, |
1886 | tree nested_name_specifier) |
1887 | { |
1888 | tree scope; |
1889 | tree qualifying_type = NULL_TREE; |
1890 | |
1891 | /* If we are parsing a template declaration and if decl is a typedef, |
1892 | add it to a list tied to the template. |
1893 | At template instantiation time, that list will be walked and |
1894 | access check performed. */ |
1895 | add_typedef_to_current_template_for_access_check (decl, |
1896 | nested_name_specifier |
1897 | ? nested_name_specifier |
1898 | : DECL_CONTEXT (decl), |
1899 | input_location); |
1900 | |
1901 | /* If we're not checking, return immediately. */ |
1902 | if (deferred_access_no_check) |
1903 | return; |
1904 | |
1905 | /* Determine the SCOPE of DECL. */ |
1906 | scope = context_for_name_lookup (decl); |
1907 | /* If the SCOPE is not a type, then DECL is not a member. */ |
1908 | if (!TYPE_P (scope)) |
1909 | return; |
1910 | /* Compute the scope through which DECL is being accessed. */ |
1911 | if (object_type |
1912 | /* OBJECT_TYPE might not be a class type; consider: |
1913 | |
1914 | class A { typedef int I; }; |
1915 | I *p; |
1916 | p->A::I::~I(); |
1917 | |
1918 | In this case, we will have "A::I" as the DECL, but "I" as the |
1919 | OBJECT_TYPE. */ |
1920 | && CLASS_TYPE_P (object_type) |
1921 | && DERIVED_FROM_P (scope, object_type)) |
1922 | /* If we are processing a `->' or `.' expression, use the type of the |
1923 | left-hand side. */ |
1924 | qualifying_type = object_type; |
1925 | else if (nested_name_specifier) |
1926 | { |
1927 | /* If the reference is to a non-static member of the |
1928 | current class, treat it as if it were referenced through |
1929 | `this'. */ |
1930 | tree ct; |
1931 | if (DECL_NONSTATIC_MEMBER_P (decl) |
1932 | && current_class_ptr |
1933 | && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ())) |
1934 | qualifying_type = ct; |
1935 | /* Otherwise, use the type indicated by the |
1936 | nested-name-specifier. */ |
1937 | else |
1938 | qualifying_type = nested_name_specifier; |
1939 | } |
1940 | else |
1941 | /* Otherwise, the name must be from the current class or one of |
1942 | its bases. */ |
1943 | qualifying_type = currently_open_derived_class (scope); |
1944 | |
1945 | if (qualifying_type |
1946 | /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM |
1947 | or similar in a default argument value. */ |
1948 | && CLASS_TYPE_P (qualifying_type) |
1949 | && !dependent_type_p (qualifying_type)) |
1950 | perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl, |
1951 | decl, tf_warning_or_error); |
1952 | } |
1953 | |
1954 | /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the |
1955 | class named to the left of the "::" operator. DONE is true if this |
1956 | expression is a complete postfix-expression; it is false if this |
1957 | expression is followed by '->', '[', '(', etc. ADDRESS_P is true |
1958 | iff this expression is the operand of '&'. TEMPLATE_P is true iff |
1959 | the qualified-id was of the form "A::template B". TEMPLATE_ARG_P |
1960 | is true iff this qualified name appears as a template argument. */ |
1961 | |
1962 | tree |
1963 | finish_qualified_id_expr (tree qualifying_class, |
1964 | tree expr, |
1965 | bool done, |
1966 | bool address_p, |
1967 | bool template_p, |
1968 | bool template_arg_p, |
1969 | tsubst_flags_t complain) |
1970 | { |
1971 | gcc_assert (TYPE_P (qualifying_class)); |
1972 | |
1973 | if (error_operand_p (expr)) |
1974 | return error_mark_node; |
1975 | |
1976 | if ((DECL_P (expr) || BASELINK_P (expr)) |
1977 | && !mark_used (expr, complain)) |
1978 | return error_mark_node; |
1979 | |
1980 | if (template_p) |
1981 | { |
1982 | if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE) |
1983 | /* cp_parser_lookup_name thought we were looking for a type, |
1984 | but we're actually looking for a declaration. */ |
1985 | expr = build_qualified_name (/*type*/NULL_TREE, |
1986 | TYPE_CONTEXT (expr), |
1987 | TYPE_IDENTIFIER (expr), |
1988 | /*template_p*/true); |
1989 | else |
1990 | check_template_keyword (expr); |
1991 | } |
1992 | |
1993 | /* If EXPR occurs as the operand of '&', use special handling that |
1994 | permits a pointer-to-member. */ |
1995 | if (address_p && done) |
1996 | { |
1997 | if (TREE_CODE (expr) == SCOPE_REF) |
1998 | expr = TREE_OPERAND (expr, 1); |
1999 | expr = build_offset_ref (qualifying_class, expr, |
2000 | /*address_p=*/true, complain); |
2001 | return expr; |
2002 | } |
2003 | |
2004 | /* No need to check access within an enum. */ |
2005 | if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE) |
2006 | return expr; |
2007 | |
2008 | /* Within the scope of a class, turn references to non-static |
2009 | members into expression of the form "this->...". */ |
2010 | if (template_arg_p) |
2011 | /* But, within a template argument, we do not want make the |
2012 | transformation, as there is no "this" pointer. */ |
2013 | ; |
2014 | else if (TREE_CODE (expr) == FIELD_DECL) |
2015 | { |
2016 | push_deferring_access_checks (dk_no_check); |
2017 | expr = finish_non_static_data_member (expr, NULL_TREE, |
2018 | qualifying_class); |
2019 | pop_deferring_access_checks (); |
2020 | } |
2021 | else if (BASELINK_P (expr)) |
2022 | { |
2023 | /* See if any of the functions are non-static members. */ |
2024 | /* If so, the expression may be relative to 'this'. */ |
2025 | if (!shared_member_p (expr) |
2026 | && current_class_ptr |
2027 | && DERIVED_FROM_P (qualifying_class, |
2028 | current_nonlambda_class_type ())) |
2029 | expr = (build_class_member_access_expr |
2030 | (maybe_dummy_object (qualifying_class, NULL), |
2031 | expr, |
2032 | BASELINK_ACCESS_BINFO (expr), |
2033 | /*preserve_reference=*/false, |
2034 | complain)); |
2035 | else if (done) |
2036 | /* The expression is a qualified name whose address is not |
2037 | being taken. */ |
2038 | expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false, |
2039 | complain); |
2040 | } |
2041 | else |
2042 | { |
2043 | /* In a template, return a SCOPE_REF for most qualified-ids |
2044 | so that we can check access at instantiation time. But if |
2045 | we're looking at a member of the current instantiation, we |
2046 | know we have access and building up the SCOPE_REF confuses |
2047 | non-type template argument handling. */ |
2048 | if (processing_template_decl |
2049 | && (!currently_open_class (qualifying_class) |
2050 | || TREE_CODE (expr) == BIT_NOT_EXPR)) |
2051 | expr = build_qualified_name (TREE_TYPE (expr), |
2052 | qualifying_class, expr, |
2053 | template_p); |
2054 | |
2055 | expr = convert_from_reference (expr); |
2056 | } |
2057 | |
2058 | return expr; |
2059 | } |
2060 | |
2061 | /* Begin a statement-expression. The value returned must be passed to |
2062 | finish_stmt_expr. */ |
2063 | |
2064 | tree |
2065 | begin_stmt_expr (void) |
2066 | { |
2067 | return push_stmt_list (); |
2068 | } |
2069 | |
2070 | /* Process the final expression of a statement expression. EXPR can be |
2071 | NULL, if the final expression is empty. Return a STATEMENT_LIST |
2072 | containing all the statements in the statement-expression, or |
2073 | ERROR_MARK_NODE if there was an error. */ |
2074 | |
2075 | tree |
2076 | finish_stmt_expr_expr (tree expr, tree stmt_expr) |
2077 | { |
2078 | if (error_operand_p (expr)) |
2079 | { |
2080 | /* The type of the statement-expression is the type of the last |
2081 | expression. */ |
2082 | TREE_TYPE (stmt_expr) = error_mark_node; |
2083 | return error_mark_node; |
2084 | } |
2085 | |
2086 | /* If the last statement does not have "void" type, then the value |
2087 | of the last statement is the value of the entire expression. */ |
2088 | if (expr) |
2089 | { |
2090 | tree type = TREE_TYPE (expr); |
2091 | |
2092 | if (processing_template_decl) |
2093 | { |
2094 | expr = build_stmt (input_location, EXPR_STMT, expr); |
2095 | expr = add_stmt (expr); |
2096 | /* Mark the last statement so that we can recognize it as such at |
2097 | template-instantiation time. */ |
2098 | EXPR_STMT_STMT_EXPR_RESULT (expr) = 1; |
2099 | } |
2100 | else if (VOID_TYPE_P (type)) |
2101 | { |
2102 | /* Just treat this like an ordinary statement. */ |
2103 | expr = finish_expr_stmt (expr); |
2104 | } |
2105 | else |
2106 | { |
2107 | /* It actually has a value we need to deal with. First, force it |
2108 | to be an rvalue so that we won't need to build up a copy |
2109 | constructor call later when we try to assign it to something. */ |
2110 | expr = force_rvalue (expr, tf_warning_or_error); |
2111 | if (error_operand_p (expr)) |
2112 | return error_mark_node; |
2113 | |
2114 | /* Update for array-to-pointer decay. */ |
2115 | type = TREE_TYPE (expr); |
2116 | |
2117 | /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a |
2118 | normal statement, but don't convert to void or actually add |
2119 | the EXPR_STMT. */ |
2120 | if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) |
2121 | expr = maybe_cleanup_point_expr (expr); |
2122 | add_stmt (expr); |
2123 | } |
2124 | |
2125 | /* The type of the statement-expression is the type of the last |
2126 | expression. */ |
2127 | TREE_TYPE (stmt_expr) = type; |
2128 | } |
2129 | |
2130 | return stmt_expr; |
2131 | } |
2132 | |
2133 | /* Finish a statement-expression. EXPR should be the value returned |
2134 | by the previous begin_stmt_expr. Returns an expression |
2135 | representing the statement-expression. */ |
2136 | |
2137 | tree |
2138 | finish_stmt_expr (tree stmt_expr, bool has_no_scope) |
2139 | { |
2140 | tree type; |
2141 | tree result; |
2142 | |
2143 | if (error_operand_p (stmt_expr)) |
2144 | { |
2145 | pop_stmt_list (stmt_expr); |
2146 | return error_mark_node; |
2147 | } |
2148 | |
2149 | gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST); |
2150 | |
2151 | type = TREE_TYPE (stmt_expr); |
2152 | result = pop_stmt_list (stmt_expr); |
2153 | TREE_TYPE (result) = type; |
2154 | |
2155 | if (processing_template_decl) |
2156 | { |
2157 | result = build_min (STMT_EXPR, type, result); |
2158 | TREE_SIDE_EFFECTS (result) = 1; |
2159 | STMT_EXPR_NO_SCOPE (result) = has_no_scope; |
2160 | } |
2161 | else if (CLASS_TYPE_P (type)) |
2162 | { |
2163 | /* Wrap the statement-expression in a TARGET_EXPR so that the |
2164 | temporary object created by the final expression is destroyed at |
2165 | the end of the full-expression containing the |
2166 | statement-expression. */ |
2167 | result = force_target_expr (type, result, tf_warning_or_error); |
2168 | } |
2169 | |
2170 | return result; |
2171 | } |
2172 | |
2173 | /* Returns the expression which provides the value of STMT_EXPR. */ |
2174 | |
2175 | tree |
2176 | stmt_expr_value_expr (tree stmt_expr) |
2177 | { |
2178 | tree t = STMT_EXPR_STMT (stmt_expr); |
2179 | |
2180 | if (TREE_CODE (t) == BIND_EXPR) |
2181 | t = BIND_EXPR_BODY (t); |
2182 | |
2183 | if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t)) |
2184 | t = STATEMENT_LIST_TAIL (t)->stmt; |
2185 | |
2186 | if (TREE_CODE (t) == EXPR_STMT) |
2187 | t = EXPR_STMT_EXPR (t); |
2188 | |
2189 | return t; |
2190 | } |
2191 | |
2192 | /* Return TRUE iff EXPR_STMT is an empty list of |
2193 | expression statements. */ |
2194 | |
2195 | bool |
2196 | empty_expr_stmt_p (tree expr_stmt) |
2197 | { |
2198 | tree body = NULL_TREE; |
2199 | |
2200 | if (expr_stmt == void_node) |
2201 | return true; |
2202 | |
2203 | if (expr_stmt) |
2204 | { |
2205 | if (TREE_CODE (expr_stmt) == EXPR_STMT) |
2206 | body = EXPR_STMT_EXPR (expr_stmt); |
2207 | else if (TREE_CODE (expr_stmt) == STATEMENT_LIST) |
2208 | body = expr_stmt; |
2209 | } |
2210 | |
2211 | if (body) |
2212 | { |
2213 | if (TREE_CODE (body) == STATEMENT_LIST) |
2214 | return tsi_end_p (tsi_start (body)); |
2215 | else |
2216 | return empty_expr_stmt_p (body); |
2217 | } |
2218 | return false; |
2219 | } |
2220 | |
2221 | /* Perform Koenig lookup. FN is the postfix-expression representing |
2222 | the function (or functions) to call; ARGS are the arguments to the |
2223 | call. Returns the functions to be considered by overload resolution. */ |
2224 | |
2225 | cp_expr |
2226 | perform_koenig_lookup (cp_expr fn, vec<tree, va_gc> *args, |
2227 | tsubst_flags_t complain) |
2228 | { |
2229 | tree identifier = NULL_TREE; |
2230 | tree functions = NULL_TREE; |
2231 | tree tmpl_args = NULL_TREE; |
2232 | bool template_id = false; |
2233 | location_t loc = fn.get_location (); |
2234 | |
2235 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) |
2236 | { |
2237 | /* Use a separate flag to handle null args. */ |
2238 | template_id = true; |
2239 | tmpl_args = TREE_OPERAND (fn, 1); |
2240 | fn = TREE_OPERAND (fn, 0); |
2241 | } |
2242 | |
2243 | /* Find the name of the overloaded function. */ |
2244 | if (identifier_p (fn)) |
2245 | identifier = fn; |
2246 | else |
2247 | { |
2248 | functions = fn; |
2249 | identifier = OVL_NAME (functions); |
2250 | } |
2251 | |
2252 | /* A call to a namespace-scope function using an unqualified name. |
2253 | |
2254 | Do Koenig lookup -- unless any of the arguments are |
2255 | type-dependent. */ |
2256 | if (!any_type_dependent_arguments_p (args) |
2257 | && !any_dependent_template_arguments_p (tmpl_args)) |
2258 | { |
2259 | fn = lookup_arg_dependent (identifier, functions, args); |
2260 | if (!fn) |
2261 | { |
2262 | /* The unqualified name could not be resolved. */ |
2263 | if (complain & tf_error) |
2264 | fn = unqualified_fn_lookup_error (cp_expr (identifier, loc)); |
2265 | else |
2266 | fn = identifier; |
2267 | } |
2268 | } |
2269 | |
2270 | if (fn && template_id && fn != error_mark_node) |
2271 | fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args); |
2272 | |
2273 | return fn; |
2274 | } |
2275 | |
2276 | /* Generate an expression for `FN (ARGS)'. This may change the |
2277 | contents of ARGS. |
2278 | |
2279 | If DISALLOW_VIRTUAL is true, the call to FN will be not generated |
2280 | as a virtual call, even if FN is virtual. (This flag is set when |
2281 | encountering an expression where the function name is explicitly |
2282 | qualified. For example a call to `X::f' never generates a virtual |
2283 | call.) |
2284 | |
2285 | Returns code for the call. */ |
2286 | |
2287 | tree |
2288 | finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual, |
2289 | bool koenig_p, tsubst_flags_t complain) |
2290 | { |
2291 | tree result; |
2292 | tree orig_fn; |
2293 | vec<tree, va_gc> *orig_args = NULL; |
2294 | |
2295 | if (fn == error_mark_node) |
2296 | return error_mark_node; |
2297 | |
2298 | gcc_assert (!TYPE_P (fn)); |
2299 | |
2300 | /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo |
2301 | it so that we can tell this is a call to a known function. */ |
2302 | fn = maybe_undo_parenthesized_ref (fn); |
2303 | |
2304 | orig_fn = fn; |
2305 | |
2306 | if (processing_template_decl) |
2307 | { |
2308 | /* If FN is a local extern declaration or set thereof, look them up |
2309 | again at instantiation time. */ |
2310 | if (is_overloaded_fn (fn)) |
2311 | { |
2312 | tree ifn = get_first_fn (fn); |
2313 | if (TREE_CODE (ifn) == FUNCTION_DECL |
2314 | && DECL_LOCAL_FUNCTION_P (ifn)) |
2315 | orig_fn = DECL_NAME (ifn); |
2316 | } |
2317 | |
2318 | /* If the call expression is dependent, build a CALL_EXPR node |
2319 | with no type; type_dependent_expression_p recognizes |
2320 | expressions with no type as being dependent. */ |
2321 | if (type_dependent_expression_p (fn) |
2322 | || any_type_dependent_arguments_p (*args)) |
2323 | { |
2324 | result = build_min_nt_call_vec (orig_fn, *args); |
2325 | SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location)); |
2326 | KOENIG_LOOKUP_P (result) = koenig_p; |
2327 | if (is_overloaded_fn (fn)) |
2328 | { |
2329 | fn = get_fns (fn); |
2330 | lookup_keep (fn, true); |
2331 | } |
2332 | |
2333 | if (cfun) |
2334 | { |
2335 | bool abnormal = true; |
2336 | for (lkp_iterator iter (fn); abnormal && iter; ++iter) |
2337 | { |
2338 | tree fndecl = *iter; |
2339 | if (TREE_CODE (fndecl) != FUNCTION_DECL |
2340 | || !TREE_THIS_VOLATILE (fndecl)) |
2341 | abnormal = false; |
2342 | } |
2343 | /* FIXME: Stop warning about falling off end of non-void |
2344 | function. But this is wrong. Even if we only see |
2345 | no-return fns at this point, we could select a |
2346 | future-defined return fn during instantiation. Or |
2347 | vice-versa. */ |
2348 | if (abnormal) |
2349 | current_function_returns_abnormally = 1; |
2350 | } |
2351 | return result; |
2352 | } |
2353 | orig_args = make_tree_vector_copy (*args); |
2354 | if (!BASELINK_P (fn) |
2355 | && TREE_CODE (fn) != PSEUDO_DTOR_EXPR |
2356 | && TREE_TYPE (fn) != unknown_type_node) |
2357 | fn = build_non_dependent_expr (fn); |
2358 | make_args_non_dependent (*args); |
2359 | } |
2360 | |
2361 | if (TREE_CODE (fn) == COMPONENT_REF) |
2362 | { |
2363 | tree member = TREE_OPERAND (fn, 1); |
2364 | if (BASELINK_P (member)) |
2365 | { |
2366 | tree object = TREE_OPERAND (fn, 0); |
2367 | return build_new_method_call (object, member, |
2368 | args, NULL_TREE, |
2369 | (disallow_virtual |
2370 | ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL |
2371 | : LOOKUP_NORMAL), |
2372 | /*fn_p=*/NULL, |
2373 | complain); |
2374 | } |
2375 | } |
2376 | |
2377 | /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */ |
2378 | if (TREE_CODE (fn) == ADDR_EXPR |
2379 | && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD) |
2380 | fn = TREE_OPERAND (fn, 0); |
2381 | |
2382 | if (is_overloaded_fn (fn)) |
2383 | fn = baselink_for_fns (fn); |
2384 | |
2385 | result = NULL_TREE; |
2386 | if (BASELINK_P (fn)) |
2387 | { |
2388 | tree object; |
2389 | |
2390 | /* A call to a member function. From [over.call.func]: |
2391 | |
2392 | If the keyword this is in scope and refers to the class of |
2393 | that member function, or a derived class thereof, then the |
2394 | function call is transformed into a qualified function call |
2395 | using (*this) as the postfix-expression to the left of the |
2396 | . operator.... [Otherwise] a contrived object of type T |
2397 | becomes the implied object argument. |
2398 | |
2399 | In this situation: |
2400 | |
2401 | struct A { void f(); }; |
2402 | struct B : public A {}; |
2403 | struct C : public A { void g() { B::f(); }}; |
2404 | |
2405 | "the class of that member function" refers to `A'. But 11.2 |
2406 | [class.access.base] says that we need to convert 'this' to B* as |
2407 | part of the access, so we pass 'B' to maybe_dummy_object. */ |
2408 | |
2409 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn))) |
2410 | { |
2411 | /* A constructor call always uses a dummy object. (This constructor |
2412 | call which has the form A::A () is actually invalid and we are |
2413 | going to reject it later in build_new_method_call.) */ |
2414 | object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn))); |
2415 | } |
2416 | else |
2417 | object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)), |
2418 | NULL); |
2419 | |
2420 | result = build_new_method_call (object, fn, args, NULL_TREE, |
2421 | (disallow_virtual |
2422 | ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL |
2423 | : LOOKUP_NORMAL), |
2424 | /*fn_p=*/NULL, |
2425 | complain); |
2426 | } |
2427 | else if (is_overloaded_fn (fn)) |
2428 | { |
2429 | /* If the function is an overloaded builtin, resolve it. */ |
2430 | if (TREE_CODE (fn) == FUNCTION_DECL |
2431 | && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL |
2432 | || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD)) |
2433 | result = resolve_overloaded_builtin (input_location, fn, *args); |
2434 | |
2435 | if (!result) |
2436 | { |
2437 | if (warn_sizeof_pointer_memaccess |
2438 | && (complain & tf_warning) |
2439 | && !vec_safe_is_empty (*args) |
2440 | && !processing_template_decl) |
2441 | { |
2442 | location_t sizeof_arg_loc[3]; |
2443 | tree sizeof_arg[3]; |
2444 | unsigned int i; |
2445 | for (i = 0; i < 3; i++) |
2446 | { |
2447 | tree t; |
2448 | |
2449 | sizeof_arg_loc[i] = UNKNOWN_LOCATION; |
2450 | sizeof_arg[i] = NULL_TREE; |
2451 | if (i >= (*args)->length ()) |
2452 | continue; |
2453 | t = (**args)[i]; |
2454 | if (TREE_CODE (t) != SIZEOF_EXPR) |
2455 | continue; |
2456 | if (SIZEOF_EXPR_TYPE_P (t)) |
2457 | sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0)); |
2458 | else |
2459 | sizeof_arg[i] = TREE_OPERAND (t, 0); |
2460 | sizeof_arg_loc[i] = EXPR_LOCATION (t); |
2461 | } |
2462 | sizeof_pointer_memaccess_warning |
2463 | (sizeof_arg_loc, fn, *args, |
2464 | sizeof_arg, same_type_ignoring_top_level_qualifiers_p); |
2465 | } |
2466 | |
2467 | /* A call to a namespace-scope function. */ |
2468 | result = build_new_function_call (fn, args, complain); |
2469 | } |
2470 | } |
2471 | else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR) |
2472 | { |
2473 | if (!vec_safe_is_empty (*args)) |
2474 | error ("arguments to destructor are not allowed" ); |
2475 | /* Mark the pseudo-destructor call as having side-effects so |
2476 | that we do not issue warnings about its use. */ |
2477 | result = build1 (NOP_EXPR, |
2478 | void_type_node, |
2479 | TREE_OPERAND (fn, 0)); |
2480 | TREE_SIDE_EFFECTS (result) = 1; |
2481 | } |
2482 | else if (CLASS_TYPE_P (TREE_TYPE (fn))) |
2483 | /* If the "function" is really an object of class type, it might |
2484 | have an overloaded `operator ()'. */ |
2485 | result = build_op_call (fn, args, complain); |
2486 | |
2487 | if (!result) |
2488 | /* A call where the function is unknown. */ |
2489 | result = cp_build_function_call_vec (fn, args, complain); |
2490 | |
2491 | if (processing_template_decl && result != error_mark_node) |
2492 | { |
2493 | if (INDIRECT_REF_P (result)) |
2494 | result = TREE_OPERAND (result, 0); |
2495 | result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args); |
2496 | SET_EXPR_LOCATION (result, input_location); |
2497 | KOENIG_LOOKUP_P (result) = koenig_p; |
2498 | release_tree_vector (orig_args); |
2499 | result = convert_from_reference (result); |
2500 | } |
2501 | |
2502 | /* Free or retain OVERLOADs from lookup. */ |
2503 | if (is_overloaded_fn (orig_fn)) |
2504 | lookup_keep (get_fns (orig_fn), processing_template_decl); |
2505 | |
2506 | return result; |
2507 | } |
2508 | |
2509 | /* Finish a call to a postfix increment or decrement or EXPR. (Which |
2510 | is indicated by CODE, which should be POSTINCREMENT_EXPR or |
2511 | POSTDECREMENT_EXPR.) */ |
2512 | |
2513 | cp_expr |
2514 | finish_increment_expr (cp_expr expr, enum tree_code code) |
2515 | { |
2516 | /* input_location holds the location of the trailing operator token. |
2517 | Build a location of the form: |
2518 | expr++ |
2519 | ~~~~^~ |
2520 | with the caret at the operator token, ranging from the start |
2521 | of EXPR to the end of the operator token. */ |
2522 | location_t combined_loc = make_location (input_location, |
2523 | expr.get_start (), |
2524 | get_finish (input_location)); |
2525 | cp_expr result = build_x_unary_op (combined_loc, code, expr, |
2526 | tf_warning_or_error); |
2527 | /* TODO: build_x_unary_op doesn't honor the location, so set it here. */ |
2528 | result.set_location (combined_loc); |
2529 | return result; |
2530 | } |
2531 | |
2532 | /* Finish a use of `this'. Returns an expression for `this'. */ |
2533 | |
2534 | tree |
2535 | finish_this_expr (void) |
2536 | { |
2537 | tree result = NULL_TREE; |
2538 | |
2539 | if (current_class_ptr) |
2540 | { |
2541 | tree type = TREE_TYPE (current_class_ref); |
2542 | |
2543 | /* In a lambda expression, 'this' refers to the captured 'this'. */ |
2544 | if (LAMBDA_TYPE_P (type)) |
2545 | result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true); |
2546 | else |
2547 | result = current_class_ptr; |
2548 | } |
2549 | |
2550 | if (result) |
2551 | /* The keyword 'this' is a prvalue expression. */ |
2552 | return rvalue (result); |
2553 | |
2554 | tree fn = current_nonlambda_function (); |
2555 | if (fn && DECL_STATIC_FUNCTION_P (fn)) |
2556 | error ("%<this%> is unavailable for static member functions" ); |
2557 | else if (fn) |
2558 | error ("invalid use of %<this%> in non-member function" ); |
2559 | else |
2560 | error ("invalid use of %<this%> at top level" ); |
2561 | return error_mark_node; |
2562 | } |
2563 | |
2564 | /* Finish a pseudo-destructor expression. If SCOPE is NULL, the |
2565 | expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is |
2566 | the TYPE for the type given. If SCOPE is non-NULL, the expression |
2567 | was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */ |
2568 | |
2569 | tree |
2570 | finish_pseudo_destructor_expr (tree object, tree scope, tree destructor, |
2571 | location_t loc) |
2572 | { |
2573 | if (object == error_mark_node || destructor == error_mark_node) |
2574 | return error_mark_node; |
2575 | |
2576 | gcc_assert (TYPE_P (destructor)); |
2577 | |
2578 | if (!processing_template_decl) |
2579 | { |
2580 | if (scope == error_mark_node) |
2581 | { |
2582 | error_at (loc, "invalid qualifying scope in pseudo-destructor name" ); |
2583 | return error_mark_node; |
2584 | } |
2585 | if (is_auto (destructor)) |
2586 | destructor = TREE_TYPE (object); |
2587 | if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor)) |
2588 | { |
2589 | error_at (loc, |
2590 | "qualified type %qT does not match destructor name ~%qT" , |
2591 | scope, destructor); |
2592 | return error_mark_node; |
2593 | } |
2594 | |
2595 | |
2596 | /* [expr.pseudo] says both: |
2597 | |
2598 | The type designated by the pseudo-destructor-name shall be |
2599 | the same as the object type. |
2600 | |
2601 | and: |
2602 | |
2603 | The cv-unqualified versions of the object type and of the |
2604 | type designated by the pseudo-destructor-name shall be the |
2605 | same type. |
2606 | |
2607 | We implement the more generous second sentence, since that is |
2608 | what most other compilers do. */ |
2609 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object), |
2610 | destructor)) |
2611 | { |
2612 | error_at (loc, "%qE is not of type %qT" , object, destructor); |
2613 | return error_mark_node; |
2614 | } |
2615 | } |
2616 | |
2617 | return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object, |
2618 | scope, destructor); |
2619 | } |
2620 | |
2621 | /* Finish an expression of the form CODE EXPR. */ |
2622 | |
2623 | cp_expr |
2624 | finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr, |
2625 | tsubst_flags_t complain) |
2626 | { |
2627 | /* Build a location of the form: |
2628 | ++expr |
2629 | ^~~~~~ |
2630 | with the caret at the operator token, ranging from the start |
2631 | of the operator token to the end of EXPR. */ |
2632 | location_t combined_loc = make_location (op_loc, |
2633 | op_loc, expr.get_finish ()); |
2634 | cp_expr result = build_x_unary_op (combined_loc, code, expr, complain); |
2635 | /* TODO: build_x_unary_op doesn't always honor the location. */ |
2636 | result.set_location (combined_loc); |
2637 | |
2638 | tree result_ovl, expr_ovl; |
2639 | |
2640 | if (!(complain & tf_warning)) |
2641 | return result; |
2642 | |
2643 | result_ovl = result; |
2644 | expr_ovl = expr; |
2645 | |
2646 | if (!processing_template_decl) |
2647 | expr_ovl = cp_fully_fold (expr_ovl); |
2648 | |
2649 | if (!CONSTANT_CLASS_P (expr_ovl) |
2650 | || TREE_OVERFLOW_P (expr_ovl)) |
2651 | return result; |
2652 | |
2653 | if (!processing_template_decl) |
2654 | result_ovl = cp_fully_fold (result_ovl); |
2655 | |
2656 | if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl)) |
2657 | overflow_warning (combined_loc, result_ovl); |
2658 | |
2659 | return result; |
2660 | } |
2661 | |
2662 | /* Finish a compound-literal expression or C++11 functional cast with aggregate |
2663 | initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL |
2664 | is being cast. */ |
2665 | |
2666 | tree |
2667 | finish_compound_literal (tree type, tree compound_literal, |
2668 | tsubst_flags_t complain, |
2669 | fcl_t fcl_context) |
2670 | { |
2671 | if (type == error_mark_node) |
2672 | return error_mark_node; |
2673 | |
2674 | if (TREE_CODE (type) == REFERENCE_TYPE) |
2675 | { |
2676 | compound_literal |
2677 | = finish_compound_literal (TREE_TYPE (type), compound_literal, |
2678 | complain, fcl_context); |
2679 | return cp_build_c_cast (type, compound_literal, complain); |
2680 | } |
2681 | |
2682 | if (!TYPE_OBJ_P (type)) |
2683 | { |
2684 | if (complain & tf_error) |
2685 | error ("compound literal of non-object type %qT" , type); |
2686 | return error_mark_node; |
2687 | } |
2688 | |
2689 | if (tree anode = type_uses_auto (type)) |
2690 | if (CLASS_PLACEHOLDER_TEMPLATE (anode)) |
2691 | { |
2692 | type = do_auto_deduction (type, compound_literal, anode, complain, |
2693 | adc_variable_type); |
2694 | if (type == error_mark_node) |
2695 | return error_mark_node; |
2696 | } |
2697 | |
2698 | if (processing_template_decl) |
2699 | { |
2700 | TREE_TYPE (compound_literal) = type; |
2701 | /* Mark the expression as a compound literal. */ |
2702 | TREE_HAS_CONSTRUCTOR (compound_literal) = 1; |
2703 | if (fcl_context == fcl_c99) |
2704 | CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1; |
2705 | return compound_literal; |
2706 | } |
2707 | |
2708 | type = complete_type (type); |
2709 | |
2710 | if (TYPE_NON_AGGREGATE_CLASS (type)) |
2711 | { |
2712 | /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST |
2713 | everywhere that deals with function arguments would be a pain, so |
2714 | just wrap it in a TREE_LIST. The parser set a flag so we know |
2715 | that it came from T{} rather than T({}). */ |
2716 | CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1; |
2717 | compound_literal = build_tree_list (NULL_TREE, compound_literal); |
2718 | return build_functional_cast (type, compound_literal, complain); |
2719 | } |
2720 | |
2721 | if (TREE_CODE (type) == ARRAY_TYPE |
2722 | && check_array_initializer (NULL_TREE, type, compound_literal)) |
2723 | return error_mark_node; |
2724 | compound_literal = reshape_init (type, compound_literal, complain); |
2725 | if (SCALAR_TYPE_P (type) |
2726 | && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal) |
2727 | && !check_narrowing (type, compound_literal, complain)) |
2728 | return error_mark_node; |
2729 | if (TREE_CODE (type) == ARRAY_TYPE |
2730 | && TYPE_DOMAIN (type) == NULL_TREE) |
2731 | { |
2732 | cp_complete_array_type_or_error (&type, compound_literal, |
2733 | false, complain); |
2734 | if (type == error_mark_node) |
2735 | return error_mark_node; |
2736 | } |
2737 | compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL, |
2738 | complain); |
2739 | if (TREE_CODE (compound_literal) == CONSTRUCTOR) |
2740 | { |
2741 | TREE_HAS_CONSTRUCTOR (compound_literal) = true; |
2742 | if (fcl_context == fcl_c99) |
2743 | CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1; |
2744 | } |
2745 | |
2746 | /* Put static/constant array temporaries in static variables. */ |
2747 | /* FIXME all C99 compound literals should be variables rather than C++ |
2748 | temporaries, unless they are used as an aggregate initializer. */ |
2749 | if ((!at_function_scope_p () || CP_TYPE_CONST_P (type)) |
2750 | && fcl_context == fcl_c99 |
2751 | && TREE_CODE (type) == ARRAY_TYPE |
2752 | && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
2753 | && initializer_constant_valid_p (compound_literal, type)) |
2754 | { |
2755 | tree decl = create_temporary_var (type); |
2756 | DECL_INITIAL (decl) = compound_literal; |
2757 | TREE_STATIC (decl) = 1; |
2758 | if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type)) |
2759 | { |
2760 | /* 5.19 says that a constant expression can include an |
2761 | lvalue-rvalue conversion applied to "a glvalue of literal type |
2762 | that refers to a non-volatile temporary object initialized |
2763 | with a constant expression". Rather than try to communicate |
2764 | that this VAR_DECL is a temporary, just mark it constexpr. */ |
2765 | DECL_DECLARED_CONSTEXPR_P (decl) = true; |
2766 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; |
2767 | TREE_CONSTANT (decl) = true; |
2768 | } |
2769 | cp_apply_type_quals_to_decl (cp_type_quals (type), decl); |
2770 | decl = pushdecl_top_level (decl); |
2771 | DECL_NAME (decl) = make_anon_name (); |
2772 | SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl)); |
2773 | /* Make sure the destructor is callable. */ |
2774 | tree clean = cxx_maybe_build_cleanup (decl, complain); |
2775 | if (clean == error_mark_node) |
2776 | return error_mark_node; |
2777 | return decl; |
2778 | } |
2779 | |
2780 | /* Represent other compound literals with TARGET_EXPR so we produce |
2781 | an lvalue, but can elide copies. */ |
2782 | if (!VECTOR_TYPE_P (type)) |
2783 | compound_literal = get_target_expr_sfinae (compound_literal, complain); |
2784 | |
2785 | return compound_literal; |
2786 | } |
2787 | |
2788 | /* Return the declaration for the function-name variable indicated by |
2789 | ID. */ |
2790 | |
2791 | tree |
2792 | finish_fname (tree id) |
2793 | { |
2794 | tree decl; |
2795 | |
2796 | decl = fname_decl (input_location, C_RID_CODE (id), id); |
2797 | if (processing_template_decl && current_function_decl |
2798 | && decl != error_mark_node) |
2799 | decl = DECL_NAME (decl); |
2800 | return decl; |
2801 | } |
2802 | |
2803 | /* Finish a translation unit. */ |
2804 | |
2805 | void |
2806 | finish_translation_unit (void) |
2807 | { |
2808 | /* In case there were missing closebraces, |
2809 | get us back to the global binding level. */ |
2810 | pop_everything (); |
2811 | while (current_namespace != global_namespace) |
2812 | pop_namespace (); |
2813 | |
2814 | /* Do file scope __FUNCTION__ et al. */ |
2815 | finish_fname_decls (); |
2816 | } |
2817 | |
2818 | /* Finish a template type parameter, specified as AGGR IDENTIFIER. |
2819 | Returns the parameter. */ |
2820 | |
2821 | tree |
2822 | finish_template_type_parm (tree aggr, tree identifier) |
2823 | { |
2824 | if (aggr != class_type_node) |
2825 | { |
2826 | permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>" ); |
2827 | aggr = class_type_node; |
2828 | } |
2829 | |
2830 | return build_tree_list (aggr, identifier); |
2831 | } |
2832 | |
2833 | /* Finish a template template parameter, specified as AGGR IDENTIFIER. |
2834 | Returns the parameter. */ |
2835 | |
2836 | tree |
2837 | finish_template_template_parm (tree aggr, tree identifier) |
2838 | { |
2839 | tree decl = build_decl (input_location, |
2840 | TYPE_DECL, identifier, NULL_TREE); |
2841 | |
2842 | tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE); |
2843 | DECL_TEMPLATE_PARMS (tmpl) = current_template_parms; |
2844 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2845 | DECL_ARTIFICIAL (decl) = 1; |
2846 | |
2847 | // Associate the constraints with the underlying declaration, |
2848 | // not the template. |
2849 | tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); |
2850 | tree constr = build_constraints (reqs, NULL_TREE); |
2851 | set_constraints (decl, constr); |
2852 | |
2853 | end_template_decl (); |
2854 | |
2855 | gcc_assert (DECL_TEMPLATE_PARMS (tmpl)); |
2856 | |
2857 | check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl), |
2858 | /*is_primary=*/true, /*is_partial=*/false, |
2859 | /*is_friend=*/0); |
2860 | |
2861 | return finish_template_type_parm (aggr, tmpl); |
2862 | } |
2863 | |
2864 | /* ARGUMENT is the default-argument value for a template template |
2865 | parameter. If ARGUMENT is invalid, issue error messages and return |
2866 | the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */ |
2867 | |
2868 | tree |
2869 | check_template_template_default_arg (tree argument) |
2870 | { |
2871 | if (TREE_CODE (argument) != TEMPLATE_DECL |
2872 | && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM |
2873 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) |
2874 | { |
2875 | if (TREE_CODE (argument) == TYPE_DECL) |
2876 | error ("invalid use of type %qT as a default value for a template " |
2877 | "template-parameter" , TREE_TYPE (argument)); |
2878 | else |
2879 | error ("invalid default argument for a template template parameter" ); |
2880 | return error_mark_node; |
2881 | } |
2882 | |
2883 | return argument; |
2884 | } |
2885 | |
2886 | /* Begin a class definition, as indicated by T. */ |
2887 | |
2888 | tree |
2889 | begin_class_definition (tree t) |
2890 | { |
2891 | if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t))) |
2892 | return error_mark_node; |
2893 | |
2894 | if (processing_template_parmlist) |
2895 | { |
2896 | error ("definition of %q#T inside template parameter list" , t); |
2897 | return error_mark_node; |
2898 | } |
2899 | |
2900 | /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733 |
2901 | are passed the same as decimal scalar types. */ |
2902 | if (TREE_CODE (t) == RECORD_TYPE |
2903 | && !processing_template_decl) |
2904 | { |
2905 | tree ns = TYPE_CONTEXT (t); |
2906 | if (ns && TREE_CODE (ns) == NAMESPACE_DECL |
2907 | && DECL_CONTEXT (ns) == std_node |
2908 | && DECL_NAME (ns) |
2909 | && id_equal (DECL_NAME (ns), "decimal" )) |
2910 | { |
2911 | const char *n = TYPE_NAME_STRING (t); |
2912 | if ((strcmp (n, "decimal32" ) == 0) |
2913 | || (strcmp (n, "decimal64" ) == 0) |
2914 | || (strcmp (n, "decimal128" ) == 0)) |
2915 | TYPE_TRANSPARENT_AGGR (t) = 1; |
2916 | } |
2917 | } |
2918 | |
2919 | /* A non-implicit typename comes from code like: |
2920 | |
2921 | template <typename T> struct A { |
2922 | template <typename U> struct A<T>::B ... |
2923 | |
2924 | This is erroneous. */ |
2925 | else if (TREE_CODE (t) == TYPENAME_TYPE) |
2926 | { |
2927 | error ("invalid definition of qualified type %qT" , t); |
2928 | t = error_mark_node; |
2929 | } |
2930 | |
2931 | if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t)) |
2932 | { |
2933 | t = make_class_type (RECORD_TYPE); |
2934 | pushtag (make_anon_name (), t, /*tag_scope=*/ts_current); |
2935 | } |
2936 | |
2937 | if (TYPE_BEING_DEFINED (t)) |
2938 | { |
2939 | t = make_class_type (TREE_CODE (t)); |
2940 | pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current); |
2941 | } |
2942 | maybe_process_partial_specialization (t); |
2943 | pushclass (t); |
2944 | TYPE_BEING_DEFINED (t) = 1; |
2945 | class_binding_level->defining_class_p = 1; |
2946 | |
2947 | if (flag_pack_struct) |
2948 | { |
2949 | tree v; |
2950 | TYPE_PACKED (t) = 1; |
2951 | /* Even though the type is being defined for the first time |
2952 | here, there might have been a forward declaration, so there |
2953 | might be cv-qualified variants of T. */ |
2954 | for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) |
2955 | TYPE_PACKED (v) = 1; |
2956 | } |
2957 | /* Reset the interface data, at the earliest possible |
2958 | moment, as it might have been set via a class foo; |
2959 | before. */ |
2960 | if (! TYPE_UNNAMED_P (t)) |
2961 | { |
2962 | struct c_fileinfo *finfo = \ |
2963 | get_fileinfo (LOCATION_FILE (input_location)); |
2964 | CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only; |
2965 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X |
2966 | (t, finfo->interface_unknown); |
2967 | } |
2968 | reset_specialization(); |
2969 | |
2970 | /* Make a declaration for this class in its own scope. */ |
2971 | build_self_reference (); |
2972 | |
2973 | return t; |
2974 | } |
2975 | |
2976 | /* Finish the member declaration given by DECL. */ |
2977 | |
2978 | void |
2979 | finish_member_declaration (tree decl) |
2980 | { |
2981 | if (decl == error_mark_node || decl == NULL_TREE) |
2982 | return; |
2983 | |
2984 | if (decl == void_type_node) |
2985 | /* The COMPONENT was a friend, not a member, and so there's |
2986 | nothing for us to do. */ |
2987 | return; |
2988 | |
2989 | /* We should see only one DECL at a time. */ |
2990 | gcc_assert (DECL_CHAIN (decl) == NULL_TREE); |
2991 | |
2992 | /* Don't add decls after definition. */ |
2993 | gcc_assert (TYPE_BEING_DEFINED (current_class_type) |
2994 | /* We can add lambda types when late parsing default |
2995 | arguments. */ |
2996 | || LAMBDA_TYPE_P (TREE_TYPE (decl))); |
2997 | |
2998 | /* Set up access control for DECL. */ |
2999 | TREE_PRIVATE (decl) |
3000 | = (current_access_specifier == access_private_node); |
3001 | TREE_PROTECTED (decl) |
3002 | = (current_access_specifier == access_protected_node); |
3003 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
3004 | { |
3005 | TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl); |
3006 | TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl); |
3007 | } |
3008 | |
3009 | /* Mark the DECL as a member of the current class, unless it's |
3010 | a member of an enumeration. */ |
3011 | if (TREE_CODE (decl) != CONST_DECL) |
3012 | DECL_CONTEXT (decl) = current_class_type; |
3013 | |
3014 | if (TREE_CODE (decl) == USING_DECL) |
3015 | /* For now, ignore class-scope USING_DECLS, so that debugging |
3016 | backends do not see them. */ |
3017 | DECL_IGNORED_P (decl) = 1; |
3018 | |
3019 | /* Check for bare parameter packs in the non-static data member |
3020 | declaration. */ |
3021 | if (TREE_CODE (decl) == FIELD_DECL) |
3022 | { |
3023 | if (check_for_bare_parameter_packs (TREE_TYPE (decl))) |
3024 | TREE_TYPE (decl) = error_mark_node; |
3025 | if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl))) |
3026 | DECL_ATTRIBUTES (decl) = NULL_TREE; |
3027 | } |
3028 | |
3029 | /* [dcl.link] |
3030 | |
3031 | A C language linkage is ignored for the names of class members |
3032 | and the member function type of class member functions. */ |
3033 | if (DECL_LANG_SPECIFIC (decl)) |
3034 | SET_DECL_LANGUAGE (decl, lang_cplusplus); |
3035 | |
3036 | bool add = false; |
3037 | |
3038 | /* Functions and non-functions are added differently. */ |
3039 | if (DECL_DECLARES_FUNCTION_P (decl)) |
3040 | add = add_method (current_class_type, decl, false); |
3041 | /* Enter the DECL into the scope of the class, if the class |
3042 | isn't a closure (whose fields are supposed to be unnamed). */ |
3043 | else if (CLASSTYPE_LAMBDA_EXPR (current_class_type) |
3044 | || pushdecl_class_level (decl)) |
3045 | add = true; |
3046 | |
3047 | if (add) |
3048 | { |
3049 | /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields |
3050 | go at the beginning. The reason is that |
3051 | legacy_nonfn_member_lookup searches the list in order, and we |
3052 | want a field name to override a type name so that the "struct |
3053 | stat hack" will work. In particular: |
3054 | |
3055 | struct S { enum E { }; static const int E = 5; int ary[S::E]; } s; |
3056 | |
3057 | is valid. */ |
3058 | |
3059 | if (TREE_CODE (decl) == TYPE_DECL) |
3060 | TYPE_FIELDS (current_class_type) |
3061 | = chainon (TYPE_FIELDS (current_class_type), decl); |
3062 | else |
3063 | { |
3064 | DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type); |
3065 | TYPE_FIELDS (current_class_type) = decl; |
3066 | } |
3067 | |
3068 | maybe_add_class_template_decl_list (current_class_type, decl, |
3069 | /*friend_p=*/0); |
3070 | } |
3071 | } |
3072 | |
3073 | /* Finish processing a complete template declaration. The PARMS are |
3074 | the template parameters. */ |
3075 | |
3076 | void |
3077 | finish_template_decl (tree parms) |
3078 | { |
3079 | if (parms) |
3080 | end_template_decl (); |
3081 | else |
3082 | end_specialization (); |
3083 | } |
3084 | |
3085 | // Returns the template type of the class scope being entered. If we're |
3086 | // entering a constrained class scope. TYPE is the class template |
3087 | // scope being entered and we may need to match the intended type with |
3088 | // a constrained specialization. For example: |
3089 | // |
3090 | // template<Object T> |
3091 | // struct S { void f(); }; #1 |
3092 | // |
3093 | // template<Object T> |
3094 | // void S<T>::f() { } #2 |
3095 | // |
3096 | // We check, in #2, that S<T> refers precisely to the type declared by |
3097 | // #1 (i.e., that the constraints match). Note that the following should |
3098 | // be an error since there is no specialization of S<T> that is |
3099 | // unconstrained, but this is not diagnosed here. |
3100 | // |
3101 | // template<typename T> |
3102 | // void S<T>::f() { } |
3103 | // |
3104 | // We cannot diagnose this problem here since this function also matches |
3105 | // qualified template names that are not part of a definition. For example: |
3106 | // |
3107 | // template<Integral T, Floating_point U> |
3108 | // typename pair<T, U>::first_type void f(T, U); |
3109 | // |
3110 | // Here, it is unlikely that there is a partial specialization of |
3111 | // pair constrained for for Integral and Floating_point arguments. |
3112 | // |
3113 | // The general rule is: if a constrained specialization with matching |
3114 | // constraints is found return that type. Also note that if TYPE is not a |
3115 | // class-type (e.g. a typename type), then no fixup is needed. |
3116 | |
3117 | static tree |
3118 | fixup_template_type (tree type) |
3119 | { |
3120 | // Find the template parameter list at the a depth appropriate to |
3121 | // the scope we're trying to enter. |
3122 | tree parms = current_template_parms; |
3123 | int depth = template_class_depth (type); |
3124 | for (int n = processing_template_decl; n > depth && parms; --n) |
3125 | parms = TREE_CHAIN (parms); |
3126 | if (!parms) |
3127 | return type; |
3128 | tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms); |
3129 | tree cur_constr = build_constraints (cur_reqs, NULL_TREE); |
3130 | |
3131 | // Search for a specialization whose type and constraints match. |
3132 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
3133 | tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
3134 | while (specs) |
3135 | { |
3136 | tree spec_constr = get_constraints (TREE_VALUE (specs)); |
3137 | |
3138 | // If the type and constraints match a specialization, then we |
3139 | // are entering that type. |
3140 | if (same_type_p (type, TREE_TYPE (specs)) |
3141 | && equivalent_constraints (cur_constr, spec_constr)) |
3142 | return TREE_TYPE (specs); |
3143 | specs = TREE_CHAIN (specs); |
3144 | } |
3145 | |
3146 | // If no specialization matches, then must return the type |
3147 | // previously found. |
3148 | return type; |
3149 | } |
3150 | |
3151 | /* Finish processing a template-id (which names a type) of the form |
3152 | NAME < ARGS >. Return the TYPE_DECL for the type named by the |
3153 | template-id. If ENTERING_SCOPE is nonzero we are about to enter |
3154 | the scope of template-id indicated. */ |
3155 | |
3156 | tree |
3157 | finish_template_type (tree name, tree args, int entering_scope) |
3158 | { |
3159 | tree type; |
3160 | |
3161 | type = lookup_template_class (name, args, |
3162 | NULL_TREE, NULL_TREE, entering_scope, |
3163 | tf_warning_or_error | tf_user); |
3164 | |
3165 | /* If we might be entering the scope of a partial specialization, |
3166 | find the one with the right constraints. */ |
3167 | if (flag_concepts |
3168 | && entering_scope |
3169 | && CLASS_TYPE_P (type) |
3170 | && CLASSTYPE_TEMPLATE_INFO (type) |
3171 | && dependent_type_p (type) |
3172 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) |
3173 | type = fixup_template_type (type); |
3174 | |
3175 | if (type == error_mark_node) |
3176 | return type; |
3177 | else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type)) |
3178 | return TYPE_STUB_DECL (type); |
3179 | else |
3180 | return TYPE_NAME (type); |
3181 | } |
3182 | |
3183 | /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER. |
3184 | Return a TREE_LIST containing the ACCESS_SPECIFIER and the |
3185 | BASE_CLASS, or NULL_TREE if an error occurred. The |
3186 | ACCESS_SPECIFIER is one of |
3187 | access_{default,public,protected_private}_node. For a virtual base |
3188 | we set TREE_TYPE. */ |
3189 | |
3190 | tree |
3191 | finish_base_specifier (tree base, tree access, bool virtual_p) |
3192 | { |
3193 | tree result; |
3194 | |
3195 | if (base == error_mark_node) |
3196 | { |
3197 | error ("invalid base-class specification" ); |
3198 | result = NULL_TREE; |
3199 | } |
3200 | else if (! MAYBE_CLASS_TYPE_P (base)) |
3201 | { |
3202 | error ("%qT is not a class type" , base); |
3203 | result = NULL_TREE; |
3204 | } |
3205 | else |
3206 | { |
3207 | if (cp_type_quals (base) != 0) |
3208 | { |
3209 | /* DR 484: Can a base-specifier name a cv-qualified |
3210 | class type? */ |
3211 | base = TYPE_MAIN_VARIANT (base); |
3212 | } |
3213 | result = build_tree_list (access, base); |
3214 | if (virtual_p) |
3215 | TREE_TYPE (result) = integer_type_node; |
3216 | } |
3217 | |
3218 | return result; |
3219 | } |
3220 | |
3221 | /* If FNS is a member function, a set of member functions, or a |
3222 | template-id referring to one or more member functions, return a |
3223 | BASELINK for FNS, incorporating the current access context. |
3224 | Otherwise, return FNS unchanged. */ |
3225 | |
3226 | tree |
3227 | baselink_for_fns (tree fns) |
3228 | { |
3229 | tree scope; |
3230 | tree cl; |
3231 | |
3232 | if (BASELINK_P (fns) |
3233 | || error_operand_p (fns)) |
3234 | return fns; |
3235 | |
3236 | scope = ovl_scope (fns); |
3237 | if (!CLASS_TYPE_P (scope)) |
3238 | return fns; |
3239 | |
3240 | cl = currently_open_derived_class (scope); |
3241 | if (!cl) |
3242 | cl = scope; |
3243 | cl = TYPE_BINFO (cl); |
3244 | return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE); |
3245 | } |
3246 | |
3247 | /* Returns true iff DECL is a variable from a function outside |
3248 | the current one. */ |
3249 | |
3250 | static bool |
3251 | outer_var_p (tree decl) |
3252 | { |
3253 | return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL) |
3254 | && DECL_FUNCTION_SCOPE_P (decl) |
3255 | /* Don't get confused by temporaries. */ |
3256 | && DECL_NAME (decl) |
3257 | && (DECL_CONTEXT (decl) != current_function_decl |
3258 | || parsing_nsdmi ())); |
3259 | } |
3260 | |
3261 | /* As above, but also checks that DECL is automatic. */ |
3262 | |
3263 | bool |
3264 | outer_automatic_var_p (tree decl) |
3265 | { |
3266 | return (outer_var_p (decl) |
3267 | && !TREE_STATIC (decl)); |
3268 | } |
3269 | |
3270 | /* DECL satisfies outer_automatic_var_p. Possibly complain about it or |
3271 | rewrite it for lambda capture. |
3272 | |
3273 | If ODR_USE is true, we're being called from mark_use, and we complain about |
3274 | use of constant variables. If ODR_USE is false, we're being called for the |
3275 | id-expression, and we do lambda capture. */ |
3276 | |
3277 | tree |
3278 | process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use) |
3279 | { |
3280 | if (cp_unevaluated_operand) |
3281 | /* It's not a use (3.2) if we're in an unevaluated context. */ |
3282 | return decl; |
3283 | if (decl == error_mark_node) |
3284 | return decl; |
3285 | |
3286 | tree context = DECL_CONTEXT (decl); |
3287 | tree containing_function = current_function_decl; |
3288 | tree lambda_stack = NULL_TREE; |
3289 | tree lambda_expr = NULL_TREE; |
3290 | tree initializer = convert_from_reference (decl); |
3291 | |
3292 | /* Mark it as used now even if the use is ill-formed. */ |
3293 | if (!mark_used (decl, complain)) |
3294 | return error_mark_node; |
3295 | |
3296 | if (parsing_nsdmi ()) |
3297 | containing_function = NULL_TREE; |
3298 | |
3299 | if (containing_function && LAMBDA_FUNCTION_P (containing_function)) |
3300 | { |
3301 | /* Check whether we've already built a proxy. */ |
3302 | tree var = decl; |
3303 | while (is_normal_capture_proxy (var)) |
3304 | var = DECL_CAPTURED_VARIABLE (var); |
3305 | tree d = retrieve_local_specialization (var); |
3306 | |
3307 | if (d && d != decl && is_capture_proxy (d)) |
3308 | { |
3309 | if (DECL_CONTEXT (d) == containing_function) |
3310 | /* We already have an inner proxy. */ |
3311 | return d; |
3312 | else |
3313 | /* We need to capture an outer proxy. */ |
3314 | return process_outer_var_ref (d, complain, odr_use); |
3315 | } |
3316 | } |
3317 | |
3318 | /* If we are in a lambda function, we can move out until we hit |
3319 | 1. the context, |
3320 | 2. a non-lambda function, or |
3321 | 3. a non-default capturing lambda function. */ |
3322 | while (context != containing_function |
3323 | /* containing_function can be null with invalid generic lambdas. */ |
3324 | && containing_function |
3325 | && LAMBDA_FUNCTION_P (containing_function)) |
3326 | { |
3327 | tree closure = DECL_CONTEXT (containing_function); |
3328 | lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure); |
3329 | |
3330 | if (TYPE_CLASS_SCOPE_P (closure)) |
3331 | /* A lambda in an NSDMI (c++/64496). */ |
3332 | break; |
3333 | |
3334 | if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) |
3335 | == CPLD_NONE) |
3336 | break; |
3337 | |
3338 | lambda_stack = tree_cons (NULL_TREE, |
3339 | lambda_expr, |
3340 | lambda_stack); |
3341 | |
3342 | containing_function |
3343 | = decl_function_context (containing_function); |
3344 | } |
3345 | |
3346 | /* In a lambda within a template, wait until instantiation |
3347 | time to implicitly capture. */ |
3348 | if (context == containing_function |
3349 | && DECL_TEMPLATE_INFO (containing_function) |
3350 | && uses_template_parms (DECL_TI_ARGS (containing_function))) |
3351 | return decl; |
3352 | |
3353 | if (lambda_expr && VAR_P (decl) |
3354 | && DECL_ANON_UNION_VAR_P (decl)) |
3355 | { |
3356 | if (complain & tf_error) |
3357 | error ("cannot capture member %qD of anonymous union" , decl); |
3358 | return error_mark_node; |
3359 | } |
3360 | /* Do lambda capture when processing the id-expression, not when |
3361 | odr-using a variable. */ |
3362 | if (!odr_use && context == containing_function) |
3363 | { |
3364 | decl = add_default_capture (lambda_stack, |
3365 | /*id=*/DECL_NAME (decl), |
3366 | initializer); |
3367 | } |
3368 | /* Only an odr-use of an outer automatic variable causes an |
3369 | error, and a constant variable can decay to a prvalue |
3370 | constant without odr-use. So don't complain yet. */ |
3371 | else if (!odr_use && decl_constant_var_p (decl)) |
3372 | return decl; |
3373 | else if (lambda_expr) |
3374 | { |
3375 | if (complain & tf_error) |
3376 | { |
3377 | error ("%qD is not captured" , decl); |
3378 | tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr); |
3379 | if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) |
3380 | == CPLD_NONE) |
3381 | inform (location_of (closure), |
3382 | "the lambda has no capture-default" ); |
3383 | else if (TYPE_CLASS_SCOPE_P (closure)) |
3384 | inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot " |
3385 | "capture variables from the enclosing context" , |
3386 | TYPE_CONTEXT (closure)); |
3387 | inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here" , decl); |
3388 | } |
3389 | return error_mark_node; |
3390 | } |
3391 | else |
3392 | { |
3393 | if (complain & tf_error) |
3394 | { |
3395 | error (VAR_P (decl) |
3396 | ? G_("use of local variable with automatic storage from " |
3397 | "containing function" ) |
3398 | : G_("use of parameter from containing function" )); |
3399 | inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here" , decl); |
3400 | } |
3401 | return error_mark_node; |
3402 | } |
3403 | return decl; |
3404 | } |
3405 | |
3406 | /* ID_EXPRESSION is a representation of parsed, but unprocessed, |
3407 | id-expression. (See cp_parser_id_expression for details.) SCOPE, |
3408 | if non-NULL, is the type or namespace used to explicitly qualify |
3409 | ID_EXPRESSION. DECL is the entity to which that name has been |
3410 | resolved. |
3411 | |
3412 | *CONSTANT_EXPRESSION_P is true if we are presently parsing a |
3413 | constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will |
3414 | be set to true if this expression isn't permitted in a |
3415 | constant-expression, but it is otherwise not set by this function. |
3416 | *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a |
3417 | constant-expression, but a non-constant expression is also |
3418 | permissible. |
3419 | |
3420 | DONE is true if this expression is a complete postfix-expression; |
3421 | it is false if this expression is followed by '->', '[', '(', etc. |
3422 | ADDRESS_P is true iff this expression is the operand of '&'. |
3423 | TEMPLATE_P is true iff the qualified-id was of the form |
3424 | "A::template B". TEMPLATE_ARG_P is true iff this qualified name |
3425 | appears as a template argument. |
3426 | |
3427 | If an error occurs, and it is the kind of error that might cause |
3428 | the parser to abort a tentative parse, *ERROR_MSG is filled in. It |
3429 | is the caller's responsibility to issue the message. *ERROR_MSG |
3430 | will be a string with static storage duration, so the caller need |
3431 | not "free" it. |
3432 | |
3433 | Return an expression for the entity, after issuing appropriate |
3434 | diagnostics. This function is also responsible for transforming a |
3435 | reference to a non-static member into a COMPONENT_REF that makes |
3436 | the use of "this" explicit. |
3437 | |
3438 | Upon return, *IDK will be filled in appropriately. */ |
3439 | cp_expr |
3440 | finish_id_expression (tree id_expression, |
3441 | tree decl, |
3442 | tree scope, |
3443 | cp_id_kind *idk, |
3444 | bool integral_constant_expression_p, |
3445 | bool allow_non_integral_constant_expression_p, |
3446 | bool *non_integral_constant_expression_p, |
3447 | bool template_p, |
3448 | bool done, |
3449 | bool address_p, |
3450 | bool template_arg_p, |
3451 | const char **error_msg, |
3452 | location_t location) |
3453 | { |
3454 | decl = strip_using_decl (decl); |
3455 | |
3456 | /* Initialize the output parameters. */ |
3457 | *idk = CP_ID_KIND_NONE; |
3458 | *error_msg = NULL; |
3459 | |
3460 | if (id_expression == error_mark_node) |
3461 | return error_mark_node; |
3462 | /* If we have a template-id, then no further lookup is |
3463 | required. If the template-id was for a template-class, we |
3464 | will sometimes have a TYPE_DECL at this point. */ |
3465 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
3466 | || TREE_CODE (decl) == TYPE_DECL) |
3467 | ; |
3468 | /* Look up the name. */ |
3469 | else |
3470 | { |
3471 | if (decl == error_mark_node) |
3472 | { |
3473 | /* Name lookup failed. */ |
3474 | if (scope |
3475 | && (!TYPE_P (scope) |
3476 | || (!dependent_type_p (scope) |
3477 | && !(identifier_p (id_expression) |
3478 | && IDENTIFIER_CONV_OP_P (id_expression) |
3479 | && dependent_type_p (TREE_TYPE (id_expression)))))) |
3480 | { |
3481 | /* If the qualifying type is non-dependent (and the name |
3482 | does not name a conversion operator to a dependent |
3483 | type), issue an error. */ |
3484 | qualified_name_lookup_error (scope, id_expression, decl, location); |
3485 | return error_mark_node; |
3486 | } |
3487 | else if (!scope) |
3488 | { |
3489 | /* It may be resolved via Koenig lookup. */ |
3490 | *idk = CP_ID_KIND_UNQUALIFIED; |
3491 | return id_expression; |
3492 | } |
3493 | else |
3494 | decl = id_expression; |
3495 | } |
3496 | /* If DECL is a variable that would be out of scope under |
3497 | ANSI/ISO rules, but in scope in the ARM, name lookup |
3498 | will succeed. Issue a diagnostic here. */ |
3499 | else |
3500 | decl = check_for_out_of_scope_variable (decl); |
3501 | |
3502 | /* Remember that the name was used in the definition of |
3503 | the current class so that we can check later to see if |
3504 | the meaning would have been different after the class |
3505 | was entirely defined. */ |
3506 | if (!scope && decl != error_mark_node && identifier_p (id_expression)) |
3507 | maybe_note_name_used_in_class (id_expression, decl); |
3508 | |
3509 | /* A use in unevaluated operand might not be instantiated appropriately |
3510 | if tsubst_copy builds a dummy parm, or if we never instantiate a |
3511 | generic lambda, so mark it now. */ |
3512 | if (processing_template_decl && cp_unevaluated_operand) |
3513 | mark_type_use (decl); |
3514 | |
3515 | /* Disallow uses of local variables from containing functions, except |
3516 | within lambda-expressions. */ |
3517 | if (outer_automatic_var_p (decl)) |
3518 | { |
3519 | decl = process_outer_var_ref (decl, tf_warning_or_error); |
3520 | if (decl == error_mark_node) |
3521 | return error_mark_node; |
3522 | } |
3523 | |
3524 | /* Also disallow uses of function parameters outside the function |
3525 | body, except inside an unevaluated context (i.e. decltype). */ |
3526 | if (TREE_CODE (decl) == PARM_DECL |
3527 | && DECL_CONTEXT (decl) == NULL_TREE |
3528 | && !cp_unevaluated_operand) |
3529 | { |
3530 | *error_msg = G_("use of parameter outside function body" ); |
3531 | return error_mark_node; |
3532 | } |
3533 | } |
3534 | |
3535 | /* If we didn't find anything, or what we found was a type, |
3536 | then this wasn't really an id-expression. */ |
3537 | if (TREE_CODE (decl) == TEMPLATE_DECL |
3538 | && !DECL_FUNCTION_TEMPLATE_P (decl)) |
3539 | { |
3540 | *error_msg = G_("missing template arguments" ); |
3541 | return error_mark_node; |
3542 | } |
3543 | else if (TREE_CODE (decl) == TYPE_DECL |
3544 | || TREE_CODE (decl) == NAMESPACE_DECL) |
3545 | { |
3546 | *error_msg = G_("expected primary-expression" ); |
3547 | return error_mark_node; |
3548 | } |
3549 | |
3550 | /* If the name resolved to a template parameter, there is no |
3551 | need to look it up again later. */ |
3552 | if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl)) |
3553 | || TREE_CODE (decl) == TEMPLATE_PARM_INDEX) |
3554 | { |
3555 | tree r; |
3556 | |
3557 | *idk = CP_ID_KIND_NONE; |
3558 | if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX) |
3559 | decl = TEMPLATE_PARM_DECL (decl); |
3560 | r = convert_from_reference (DECL_INITIAL (decl)); |
3561 | |
3562 | if (integral_constant_expression_p |
3563 | && !dependent_type_p (TREE_TYPE (decl)) |
3564 | && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r)))) |
3565 | { |
3566 | if (!allow_non_integral_constant_expression_p) |
3567 | error ("template parameter %qD of type %qT is not allowed in " |
3568 | "an integral constant expression because it is not of " |
3569 | "integral or enumeration type" , decl, TREE_TYPE (decl)); |
3570 | *non_integral_constant_expression_p = true; |
3571 | } |
3572 | return r; |
3573 | } |
3574 | else |
3575 | { |
3576 | bool dependent_p = type_dependent_expression_p (decl); |
3577 | |
3578 | /* If the declaration was explicitly qualified indicate |
3579 | that. The semantics of `A::f(3)' are different than |
3580 | `f(3)' if `f' is virtual. */ |
3581 | *idk = (scope |
3582 | ? CP_ID_KIND_QUALIFIED |
3583 | : (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
3584 | ? CP_ID_KIND_TEMPLATE_ID |
3585 | : (dependent_p |
3586 | ? CP_ID_KIND_UNQUALIFIED_DEPENDENT |
3587 | : CP_ID_KIND_UNQUALIFIED))); |
3588 | |
3589 | if (dependent_p |
3590 | && DECL_P (decl) |
3591 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl))) |
3592 | /* Dependent type attributes on the decl mean that the TREE_TYPE is |
3593 | wrong, so just return the identifier. */ |
3594 | return id_expression; |
3595 | |
3596 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
3597 | { |
3598 | error ("use of namespace %qD as expression" , decl); |
3599 | return error_mark_node; |
3600 | } |
3601 | else if (DECL_CLASS_TEMPLATE_P (decl)) |
3602 | { |
3603 | error ("use of class template %qT as expression" , decl); |
3604 | return error_mark_node; |
3605 | } |
3606 | else if (TREE_CODE (decl) == TREE_LIST) |
3607 | { |
3608 | /* Ambiguous reference to base members. */ |
3609 | error ("request for member %qD is ambiguous in " |
3610 | "multiple inheritance lattice" , id_expression); |
3611 | print_candidates (decl); |
3612 | return error_mark_node; |
3613 | } |
3614 | |
3615 | /* Mark variable-like entities as used. Functions are similarly |
3616 | marked either below or after overload resolution. */ |
3617 | if ((VAR_P (decl) |
3618 | || TREE_CODE (decl) == PARM_DECL |
3619 | || TREE_CODE (decl) == CONST_DECL |
3620 | || TREE_CODE (decl) == RESULT_DECL) |
3621 | && !mark_used (decl)) |
3622 | return error_mark_node; |
3623 | |
3624 | /* Only certain kinds of names are allowed in constant |
3625 | expression. Template parameters have already |
3626 | been handled above. */ |
3627 | if (! error_operand_p (decl) |
3628 | && !dependent_p |
3629 | && integral_constant_expression_p |
3630 | && ! decl_constant_var_p (decl) |
3631 | && TREE_CODE (decl) != CONST_DECL |
3632 | && ! builtin_valid_in_constant_expr_p (decl)) |
3633 | { |
3634 | if (!allow_non_integral_constant_expression_p) |
3635 | { |
3636 | error ("%qD cannot appear in a constant-expression" , decl); |
3637 | return error_mark_node; |
3638 | } |
3639 | *non_integral_constant_expression_p = true; |
3640 | } |
3641 | |
3642 | tree wrap; |
3643 | if (VAR_P (decl) |
3644 | && !cp_unevaluated_operand |
3645 | && !processing_template_decl |
3646 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)) |
3647 | && CP_DECL_THREAD_LOCAL_P (decl) |
3648 | && (wrap = get_tls_wrapper_fn (decl))) |
3649 | { |
3650 | /* Replace an evaluated use of the thread_local variable with |
3651 | a call to its wrapper. */ |
3652 | decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error); |
3653 | } |
3654 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
3655 | && !dependent_p |
3656 | && variable_template_p (TREE_OPERAND (decl, 0))) |
3657 | { |
3658 | decl = finish_template_variable (decl); |
3659 | mark_used (decl); |
3660 | decl = convert_from_reference (decl); |
3661 | } |
3662 | else if (scope) |
3663 | { |
3664 | if (TREE_CODE (decl) == SCOPE_REF) |
3665 | { |
3666 | gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0))); |
3667 | decl = TREE_OPERAND (decl, 1); |
3668 | } |
3669 | |
3670 | decl = (adjust_result_of_qualified_name_lookup |
3671 | (decl, scope, current_nonlambda_class_type())); |
3672 | |
3673 | if (TREE_CODE (decl) == FUNCTION_DECL) |
3674 | mark_used (decl); |
3675 | |
3676 | if (TYPE_P (scope)) |
3677 | decl = finish_qualified_id_expr (scope, |
3678 | decl, |
3679 | done, |
3680 | address_p, |
3681 | template_p, |
3682 | template_arg_p, |
3683 | tf_warning_or_error); |
3684 | else |
3685 | decl = convert_from_reference (decl); |
3686 | } |
3687 | else if (TREE_CODE (decl) == FIELD_DECL) |
3688 | { |
3689 | /* Since SCOPE is NULL here, this is an unqualified name. |
3690 | Access checking has been performed during name lookup |
3691 | already. Turn off checking to avoid duplicate errors. */ |
3692 | push_deferring_access_checks (dk_no_check); |
3693 | decl = finish_non_static_data_member (decl, NULL_TREE, |
3694 | /*qualifying_scope=*/NULL_TREE); |
3695 | pop_deferring_access_checks (); |
3696 | } |
3697 | else if (is_overloaded_fn (decl)) |
3698 | { |
3699 | tree first_fn = get_first_fn (decl); |
3700 | |
3701 | if (TREE_CODE (first_fn) == TEMPLATE_DECL) |
3702 | first_fn = DECL_TEMPLATE_RESULT (first_fn); |
3703 | |
3704 | /* [basic.def.odr]: "A function whose name appears as a |
3705 | potentially-evaluated expression is odr-used if it is the unique |
3706 | lookup result". |
3707 | |
3708 | But only mark it if it's a complete postfix-expression; in a call, |
3709 | ADL might select a different function, and we'll call mark_used in |
3710 | build_over_call. */ |
3711 | if (done |
3712 | && !really_overloaded_fn (decl) |
3713 | && !mark_used (first_fn)) |
3714 | return error_mark_node; |
3715 | |
3716 | if (!template_arg_p |
3717 | && TREE_CODE (first_fn) == FUNCTION_DECL |
3718 | && DECL_FUNCTION_MEMBER_P (first_fn) |
3719 | && !shared_member_p (decl)) |
3720 | { |
3721 | /* A set of member functions. */ |
3722 | decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0); |
3723 | return finish_class_member_access_expr (decl, id_expression, |
3724 | /*template_p=*/false, |
3725 | tf_warning_or_error); |
3726 | } |
3727 | |
3728 | decl = baselink_for_fns (decl); |
3729 | } |
3730 | else |
3731 | { |
3732 | if (DECL_P (decl) && DECL_NONLOCAL (decl) |
3733 | && DECL_CLASS_SCOPE_P (decl)) |
3734 | { |
3735 | tree context = context_for_name_lookup (decl); |
3736 | if (context != current_class_type) |
3737 | { |
3738 | tree path = currently_open_derived_class (context); |
3739 | perform_or_defer_access_check (TYPE_BINFO (path), |
3740 | decl, decl, |
3741 | tf_warning_or_error); |
3742 | } |
3743 | } |
3744 | |
3745 | decl = convert_from_reference (decl); |
3746 | } |
3747 | } |
3748 | |
3749 | return cp_expr (decl, location); |
3750 | } |
3751 | |
3752 | /* Implement the __typeof keyword: Return the type of EXPR, suitable for |
3753 | use as a type-specifier. */ |
3754 | |
3755 | tree |
3756 | finish_typeof (tree expr) |
3757 | { |
3758 | tree type; |
3759 | |
3760 | if (type_dependent_expression_p (expr)) |
3761 | { |
3762 | type = cxx_make_type (TYPEOF_TYPE); |
3763 | TYPEOF_TYPE_EXPR (type) = expr; |
3764 | SET_TYPE_STRUCTURAL_EQUALITY (type); |
3765 | |
3766 | return type; |
3767 | } |
3768 | |
3769 | expr = mark_type_use (expr); |
3770 | |
3771 | type = unlowered_expr_type (expr); |
3772 | |
3773 | if (!type || type == unknown_type_node) |
3774 | { |
3775 | error ("type of %qE is unknown" , expr); |
3776 | return error_mark_node; |
3777 | } |
3778 | |
3779 | return type; |
3780 | } |
3781 | |
3782 | /* Implement the __underlying_type keyword: Return the underlying |
3783 | type of TYPE, suitable for use as a type-specifier. */ |
3784 | |
3785 | tree |
3786 | finish_underlying_type (tree type) |
3787 | { |
3788 | tree underlying_type; |
3789 | |
3790 | if (processing_template_decl) |
3791 | { |
3792 | underlying_type = cxx_make_type (UNDERLYING_TYPE); |
3793 | UNDERLYING_TYPE_TYPE (underlying_type) = type; |
3794 | SET_TYPE_STRUCTURAL_EQUALITY (underlying_type); |
3795 | |
3796 | return underlying_type; |
3797 | } |
3798 | |
3799 | if (!complete_type_or_else (type, NULL_TREE)) |
3800 | return error_mark_node; |
3801 | |
3802 | if (TREE_CODE (type) != ENUMERAL_TYPE) |
3803 | { |
3804 | error ("%qT is not an enumeration type" , type); |
3805 | return error_mark_node; |
3806 | } |
3807 | |
3808 | underlying_type = ENUM_UNDERLYING_TYPE (type); |
3809 | |
3810 | /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE |
3811 | includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information. |
3812 | See finish_enum_value_list for details. */ |
3813 | if (!ENUM_FIXED_UNDERLYING_TYPE_P (type)) |
3814 | underlying_type |
3815 | = c_common_type_for_mode (TYPE_MODE (underlying_type), |
3816 | TYPE_UNSIGNED (underlying_type)); |
3817 | |
3818 | return underlying_type; |
3819 | } |
3820 | |
3821 | /* Implement the __direct_bases keyword: Return the direct base classes |
3822 | of type */ |
3823 | |
3824 | tree |
3825 | calculate_direct_bases (tree type) |
3826 | { |
3827 | vec<tree, va_gc> *vector = make_tree_vector(); |
3828 | tree bases_vec = NULL_TREE; |
3829 | vec<tree, va_gc> *base_binfos; |
3830 | tree binfo; |
3831 | unsigned i; |
3832 | |
3833 | complete_type (type); |
3834 | |
3835 | if (!NON_UNION_CLASS_TYPE_P (type)) |
3836 | return make_tree_vec (0); |
3837 | |
3838 | base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type)); |
3839 | |
3840 | /* Virtual bases are initialized first */ |
3841 | for (i = 0; base_binfos->iterate (i, &binfo); i++) |
3842 | { |
3843 | if (BINFO_VIRTUAL_P (binfo)) |
3844 | { |
3845 | vec_safe_push (vector, binfo); |
3846 | } |
3847 | } |
3848 | |
3849 | /* Now non-virtuals */ |
3850 | for (i = 0; base_binfos->iterate (i, &binfo); i++) |
3851 | { |
3852 | if (!BINFO_VIRTUAL_P (binfo)) |
3853 | { |
3854 | vec_safe_push (vector, binfo); |
3855 | } |
3856 | } |
3857 | |
3858 | |
3859 | bases_vec = make_tree_vec (vector->length ()); |
3860 | |
3861 | for (i = 0; i < vector->length (); ++i) |
3862 | { |
3863 | TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]); |
3864 | } |
3865 | return bases_vec; |
3866 | } |
3867 | |
3868 | /* Implement the __bases keyword: Return the base classes |
3869 | of type */ |
3870 | |
3871 | /* Find morally non-virtual base classes by walking binfo hierarchy */ |
3872 | /* Virtual base classes are handled separately in finish_bases */ |
3873 | |
3874 | static tree |
3875 | dfs_calculate_bases_pre (tree binfo, void * /*data_*/) |
3876 | { |
3877 | /* Don't walk bases of virtual bases */ |
3878 | return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE; |
3879 | } |
3880 | |
3881 | static tree |
3882 | dfs_calculate_bases_post (tree binfo, void *data_) |
3883 | { |
3884 | vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_); |
3885 | if (!BINFO_VIRTUAL_P (binfo)) |
3886 | { |
3887 | vec_safe_push (*data, BINFO_TYPE (binfo)); |
3888 | } |
3889 | return NULL_TREE; |
3890 | } |
3891 | |
3892 | /* Calculates the morally non-virtual base classes of a class */ |
3893 | static vec<tree, va_gc> * |
3894 | calculate_bases_helper (tree type) |
3895 | { |
3896 | vec<tree, va_gc> *vector = make_tree_vector(); |
3897 | |
3898 | /* Now add non-virtual base classes in order of construction */ |
3899 | if (TYPE_BINFO (type)) |
3900 | dfs_walk_all (TYPE_BINFO (type), |
3901 | dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector); |
3902 | return vector; |
3903 | } |
3904 | |
3905 | tree |
3906 | calculate_bases (tree type) |
3907 | { |
3908 | vec<tree, va_gc> *vector = make_tree_vector(); |
3909 | tree bases_vec = NULL_TREE; |
3910 | unsigned i; |
3911 | vec<tree, va_gc> *vbases; |
3912 | vec<tree, va_gc> *nonvbases; |
3913 | tree binfo; |
3914 | |
3915 | complete_type (type); |
3916 | |
3917 | if (!NON_UNION_CLASS_TYPE_P (type)) |
3918 | return make_tree_vec (0); |
3919 | |
3920 | /* First go through virtual base classes */ |
3921 | for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0; |
3922 | vec_safe_iterate (vbases, i, &binfo); i++) |
3923 | { |
3924 | vec<tree, va_gc> *vbase_bases; |
3925 | vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo)); |
3926 | vec_safe_splice (vector, vbase_bases); |
3927 | release_tree_vector (vbase_bases); |
3928 | } |
3929 | |
3930 | /* Now for the non-virtual bases */ |
3931 | nonvbases = calculate_bases_helper (type); |
3932 | vec_safe_splice (vector, nonvbases); |
3933 | release_tree_vector (nonvbases); |
3934 | |
3935 | /* Note that during error recovery vector->length can even be zero. */ |
3936 | if (vector->length () > 1) |
3937 | { |
3938 | /* Last element is entire class, so don't copy */ |
3939 | bases_vec = make_tree_vec (vector->length() - 1); |
3940 | |
3941 | for (i = 0; i < vector->length () - 1; ++i) |
3942 | TREE_VEC_ELT (bases_vec, i) = (*vector)[i]; |
3943 | } |
3944 | else |
3945 | bases_vec = make_tree_vec (0); |
3946 | |
3947 | release_tree_vector (vector); |
3948 | return bases_vec; |
3949 | } |
3950 | |
3951 | tree |
3952 | finish_bases (tree type, bool direct) |
3953 | { |
3954 | tree bases = NULL_TREE; |
3955 | |
3956 | if (!processing_template_decl) |
3957 | { |
3958 | /* Parameter packs can only be used in templates */ |
3959 | error ("Parameter pack __bases only valid in template declaration" ); |
3960 | return error_mark_node; |
3961 | } |
3962 | |
3963 | bases = cxx_make_type (BASES); |
3964 | BASES_TYPE (bases) = type; |
3965 | BASES_DIRECT (bases) = direct; |
3966 | SET_TYPE_STRUCTURAL_EQUALITY (bases); |
3967 | |
3968 | return bases; |
3969 | } |
3970 | |
3971 | /* Perform C++-specific checks for __builtin_offsetof before calling |
3972 | fold_offsetof. */ |
3973 | |
3974 | tree |
3975 | finish_offsetof (tree object_ptr, tree expr, location_t loc) |
3976 | { |
3977 | /* If we're processing a template, we can't finish the semantics yet. |
3978 | Otherwise we can fold the entire expression now. */ |
3979 | if (processing_template_decl) |
3980 | { |
3981 | expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr); |
3982 | SET_EXPR_LOCATION (expr, loc); |
3983 | return expr; |
3984 | } |
3985 | |
3986 | if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) |
3987 | { |
3988 | error ("cannot apply %<offsetof%> to destructor %<~%T%>" , |
3989 | TREE_OPERAND (expr, 2)); |
3990 | return error_mark_node; |
3991 | } |
3992 | if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE |
3993 | || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE |
3994 | || TREE_TYPE (expr) == unknown_type_node) |
3995 | { |
3996 | if (INDIRECT_REF_P (expr)) |
3997 | error ("second operand of %<offsetof%> is neither a single " |
3998 | "identifier nor a sequence of member accesses and " |
3999 | "array references" ); |
4000 | else |
4001 | { |
4002 | if (TREE_CODE (expr) == COMPONENT_REF |
4003 | || TREE_CODE (expr) == COMPOUND_EXPR) |
4004 | expr = TREE_OPERAND (expr, 1); |
4005 | error ("cannot apply %<offsetof%> to member function %qD" , expr); |
4006 | } |
4007 | return error_mark_node; |
4008 | } |
4009 | if (REFERENCE_REF_P (expr)) |
4010 | expr = TREE_OPERAND (expr, 0); |
4011 | if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr)) |
4012 | return error_mark_node; |
4013 | if (warn_invalid_offsetof |
4014 | && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr))) |
4015 | && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr))) |
4016 | && cp_unevaluated_operand == 0) |
4017 | pedwarn (loc, OPT_Winvalid_offsetof, |
4018 | "offsetof within non-standard-layout type %qT is undefined" , |
4019 | TREE_TYPE (TREE_TYPE (object_ptr))); |
4020 | return fold_offsetof (expr); |
4021 | } |
4022 | |
4023 | /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This |
4024 | function is broken out from the above for the benefit of the tree-ssa |
4025 | project. */ |
4026 | |
4027 | void |
4028 | simplify_aggr_init_expr (tree *tp) |
4029 | { |
4030 | tree aggr_init_expr = *tp; |
4031 | |
4032 | /* Form an appropriate CALL_EXPR. */ |
4033 | tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr); |
4034 | tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr); |
4035 | tree type = TREE_TYPE (slot); |
4036 | |
4037 | tree call_expr; |
4038 | enum style_t { ctor, arg, pcc } style; |
4039 | |
4040 | if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr)) |
4041 | style = ctor; |
4042 | #ifdef PCC_STATIC_STRUCT_RETURN |
4043 | else if (1) |
4044 | style = pcc; |
4045 | #endif |
4046 | else |
4047 | { |
4048 | gcc_assert (TREE_ADDRESSABLE (type)); |
4049 | style = arg; |
4050 | } |
4051 | |
4052 | call_expr = build_call_array_loc (input_location, |
4053 | TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), |
4054 | fn, |
4055 | aggr_init_expr_nargs (aggr_init_expr), |
4056 | AGGR_INIT_EXPR_ARGP (aggr_init_expr)); |
4057 | TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr); |
4058 | CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr); |
4059 | CALL_EXPR_OPERATOR_SYNTAX (call_expr) |
4060 | = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr); |
4061 | CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr); |
4062 | CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr); |
4063 | |
4064 | if (style == ctor) |
4065 | { |
4066 | /* Replace the first argument to the ctor with the address of the |
4067 | slot. */ |
4068 | cxx_mark_addressable (slot); |
4069 | CALL_EXPR_ARG (call_expr, 0) = |
4070 | build1 (ADDR_EXPR, build_pointer_type (type), slot); |
4071 | } |
4072 | else if (style == arg) |
4073 | { |
4074 | /* Just mark it addressable here, and leave the rest to |
4075 | expand_call{,_inline}. */ |
4076 | cxx_mark_addressable (slot); |
4077 | CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true; |
4078 | call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr); |
4079 | } |
4080 | else if (style == pcc) |
4081 | { |
4082 | /* If we're using the non-reentrant PCC calling convention, then we |
4083 | need to copy the returned value out of the static buffer into the |
4084 | SLOT. */ |
4085 | push_deferring_access_checks (dk_no_check); |
4086 | call_expr = build_aggr_init (slot, call_expr, |
4087 | DIRECT_BIND | LOOKUP_ONLYCONVERTING, |
4088 | tf_warning_or_error); |
4089 | pop_deferring_access_checks (); |
4090 | call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot); |
4091 | } |
4092 | |
4093 | if (AGGR_INIT_ZERO_FIRST (aggr_init_expr)) |
4094 | { |
4095 | tree init = build_zero_init (type, NULL_TREE, |
4096 | /*static_storage_p=*/false); |
4097 | init = build2 (INIT_EXPR, void_type_node, slot, init); |
4098 | call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr), |
4099 | init, call_expr); |
4100 | } |
4101 | |
4102 | *tp = call_expr; |
4103 | } |
4104 | |
4105 | /* Emit all thunks to FN that should be emitted when FN is emitted. */ |
4106 | |
4107 | void |
4108 | emit_associated_thunks (tree fn) |
4109 | { |
4110 | /* When we use vcall offsets, we emit thunks with the virtual |
4111 | functions to which they thunk. The whole point of vcall offsets |
4112 | is so that you can know statically the entire set of thunks that |
4113 | will ever be needed for a given virtual function, thereby |
4114 | enabling you to output all the thunks with the function itself. */ |
4115 | if (DECL_VIRTUAL_P (fn) |
4116 | /* Do not emit thunks for extern template instantiations. */ |
4117 | && ! DECL_REALLY_EXTERN (fn)) |
4118 | { |
4119 | tree thunk; |
4120 | |
4121 | for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk)) |
4122 | { |
4123 | if (!THUNK_ALIAS (thunk)) |
4124 | { |
4125 | use_thunk (thunk, /*emit_p=*/1); |
4126 | if (DECL_RESULT_THUNK_P (thunk)) |
4127 | { |
4128 | tree probe; |
4129 | |
4130 | for (probe = DECL_THUNKS (thunk); |
4131 | probe; probe = DECL_CHAIN (probe)) |
4132 | use_thunk (probe, /*emit_p=*/1); |
4133 | } |
4134 | } |
4135 | else |
4136 | gcc_assert (!DECL_THUNKS (thunk)); |
4137 | } |
4138 | } |
4139 | } |
4140 | |
4141 | /* Generate RTL for FN. */ |
4142 | |
4143 | bool |
4144 | expand_or_defer_fn_1 (tree fn) |
4145 | { |
4146 | /* When the parser calls us after finishing the body of a template |
4147 | function, we don't really want to expand the body. */ |
4148 | if (processing_template_decl) |
4149 | { |
4150 | /* Normally, collection only occurs in rest_of_compilation. So, |
4151 | if we don't collect here, we never collect junk generated |
4152 | during the processing of templates until we hit a |
4153 | non-template function. It's not safe to do this inside a |
4154 | nested class, though, as the parser may have local state that |
4155 | is not a GC root. */ |
4156 | if (!function_depth) |
4157 | ggc_collect (); |
4158 | return false; |
4159 | } |
4160 | |
4161 | gcc_assert (DECL_SAVED_TREE (fn)); |
4162 | |
4163 | /* We make a decision about linkage for these functions at the end |
4164 | of the compilation. Until that point, we do not want the back |
4165 | end to output them -- but we do want it to see the bodies of |
4166 | these functions so that it can inline them as appropriate. */ |
4167 | if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn)) |
4168 | { |
4169 | if (DECL_INTERFACE_KNOWN (fn)) |
4170 | /* We've already made a decision as to how this function will |
4171 | be handled. */; |
4172 | else if (!at_eof) |
4173 | tentative_decl_linkage (fn); |
4174 | else |
4175 | import_export_decl (fn); |
4176 | |
4177 | /* If the user wants us to keep all inline functions, then mark |
4178 | this function as needed so that finish_file will make sure to |
4179 | output it later. Similarly, all dllexport'd functions must |
4180 | be emitted; there may be callers in other DLLs. */ |
4181 | if (DECL_DECLARED_INLINE_P (fn) |
4182 | && !DECL_REALLY_EXTERN (fn) |
4183 | && (flag_keep_inline_functions |
4184 | || (flag_keep_inline_dllexport |
4185 | && lookup_attribute ("dllexport" , DECL_ATTRIBUTES (fn))))) |
4186 | { |
4187 | mark_needed (fn); |
4188 | DECL_EXTERNAL (fn) = 0; |
4189 | } |
4190 | } |
4191 | |
4192 | /* If this is a constructor or destructor body, we have to clone |
4193 | it. */ |
4194 | if (maybe_clone_body (fn)) |
4195 | { |
4196 | /* We don't want to process FN again, so pretend we've written |
4197 | it out, even though we haven't. */ |
4198 | TREE_ASM_WRITTEN (fn) = 1; |
4199 | /* If this is a constexpr function, keep DECL_SAVED_TREE. */ |
4200 | if (!DECL_DECLARED_CONSTEXPR_P (fn)) |
4201 | DECL_SAVED_TREE (fn) = NULL_TREE; |
4202 | return false; |
4203 | } |
4204 | |
4205 | /* There's no reason to do any of the work here if we're only doing |
4206 | semantic analysis; this code just generates RTL. */ |
4207 | if (flag_syntax_only) |
4208 | return false; |
4209 | |
4210 | return true; |
4211 | } |
4212 | |
4213 | void |
4214 | expand_or_defer_fn (tree fn) |
4215 | { |
4216 | if (expand_or_defer_fn_1 (fn)) |
4217 | { |
4218 | function_depth++; |
4219 | |
4220 | /* Expand or defer, at the whim of the compilation unit manager. */ |
4221 | cgraph_node::finalize_function (fn, function_depth > 1); |
4222 | emit_associated_thunks (fn); |
4223 | |
4224 | function_depth--; |
4225 | } |
4226 | } |
4227 | |
4228 | struct nrv_data |
4229 | { |
4230 | nrv_data () : visited (37) {} |
4231 | |
4232 | tree var; |
4233 | tree result; |
4234 | hash_table<nofree_ptr_hash <tree_node> > visited; |
4235 | }; |
4236 | |
4237 | /* Helper function for walk_tree, used by finalize_nrv below. */ |
4238 | |
4239 | static tree |
4240 | finalize_nrv_r (tree* tp, int* walk_subtrees, void* data) |
4241 | { |
4242 | struct nrv_data *dp = (struct nrv_data *)data; |
4243 | tree_node **slot; |
4244 | |
4245 | /* No need to walk into types. There wouldn't be any need to walk into |
4246 | non-statements, except that we have to consider STMT_EXPRs. */ |
4247 | if (TYPE_P (*tp)) |
4248 | *walk_subtrees = 0; |
4249 | /* Change all returns to just refer to the RESULT_DECL; this is a nop, |
4250 | but differs from using NULL_TREE in that it indicates that we care |
4251 | about the value of the RESULT_DECL. */ |
4252 | else if (TREE_CODE (*tp) == RETURN_EXPR) |
4253 | TREE_OPERAND (*tp, 0) = dp->result; |
4254 | /* Change all cleanups for the NRV to only run when an exception is |
4255 | thrown. */ |
4256 | else if (TREE_CODE (*tp) == CLEANUP_STMT |
4257 | && CLEANUP_DECL (*tp) == dp->var) |
4258 | CLEANUP_EH_ONLY (*tp) = 1; |
4259 | /* Replace the DECL_EXPR for the NRV with an initialization of the |
4260 | RESULT_DECL, if needed. */ |
4261 | else if (TREE_CODE (*tp) == DECL_EXPR |
4262 | && DECL_EXPR_DECL (*tp) == dp->var) |
4263 | { |
4264 | tree init; |
4265 | if (DECL_INITIAL (dp->var) |
4266 | && DECL_INITIAL (dp->var) != error_mark_node) |
4267 | init = build2 (INIT_EXPR, void_type_node, dp->result, |
4268 | DECL_INITIAL (dp->var)); |
4269 | else |
4270 | init = build_empty_stmt (EXPR_LOCATION (*tp)); |
4271 | DECL_INITIAL (dp->var) = NULL_TREE; |
4272 | SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp)); |
4273 | *tp = init; |
4274 | } |
4275 | /* And replace all uses of the NRV with the RESULT_DECL. */ |
4276 | else if (*tp == dp->var) |
4277 | *tp = dp->result; |
4278 | |
4279 | /* Avoid walking into the same tree more than once. Unfortunately, we |
4280 | can't just use walk_tree_without duplicates because it would only call |
4281 | us for the first occurrence of dp->var in the function body. */ |
4282 | slot = dp->visited.find_slot (*tp, INSERT); |
4283 | if (*slot) |
4284 | *walk_subtrees = 0; |
4285 | else |
4286 | *slot = *tp; |
4287 | |
4288 | /* Keep iterating. */ |
4289 | return NULL_TREE; |
4290 | } |
4291 | |
4292 | /* Called from finish_function to implement the named return value |
4293 | optimization by overriding all the RETURN_EXPRs and pertinent |
4294 | CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the |
4295 | RESULT_DECL for the function. */ |
4296 | |
4297 | void |
4298 | finalize_nrv (tree *tp, tree var, tree result) |
4299 | { |
4300 | struct nrv_data data; |
4301 | |
4302 | /* Copy name from VAR to RESULT. */ |
4303 | DECL_NAME (result) = DECL_NAME (var); |
4304 | /* Don't forget that we take its address. */ |
4305 | TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var); |
4306 | /* Finally set DECL_VALUE_EXPR to avoid assigning |
4307 | a stack slot at -O0 for the original var and debug info |
4308 | uses RESULT location for VAR. */ |
4309 | SET_DECL_VALUE_EXPR (var, result); |
4310 | DECL_HAS_VALUE_EXPR_P (var) = 1; |
4311 | |
4312 | data.var = var; |
4313 | data.result = result; |
4314 | cp_walk_tree (tp, finalize_nrv_r, &data, 0); |
4315 | } |
4316 | |
4317 | /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */ |
4318 | |
4319 | bool |
4320 | cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor, |
4321 | bool need_copy_ctor, bool need_copy_assignment, |
4322 | bool need_dtor) |
4323 | { |
4324 | int save_errorcount = errorcount; |
4325 | tree info, t; |
4326 | |
4327 | /* Always allocate 3 elements for simplicity. These are the |
4328 | function decls for the ctor, dtor, and assignment op. |
4329 | This layout is known to the three lang hooks, |
4330 | cxx_omp_clause_default_init, cxx_omp_clause_copy_init, |
4331 | and cxx_omp_clause_assign_op. */ |
4332 | info = make_tree_vec (3); |
4333 | CP_OMP_CLAUSE_INFO (c) = info; |
4334 | |
4335 | if (need_default_ctor || need_copy_ctor) |
4336 | { |
4337 | if (need_default_ctor) |
4338 | t = get_default_ctor (type); |
4339 | else |
4340 | t = get_copy_ctor (type, tf_warning_or_error); |
4341 | |
4342 | if (t && !trivial_fn_p (t)) |
4343 | TREE_VEC_ELT (info, 0) = t; |
4344 | } |
4345 | |
4346 | if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
4347 | TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error); |
4348 | |
4349 | if (need_copy_assignment) |
4350 | { |
4351 | t = get_copy_assign (type); |
4352 | |
4353 | if (t && !trivial_fn_p (t)) |
4354 | TREE_VEC_ELT (info, 2) = t; |
4355 | } |
4356 | |
4357 | return errorcount != save_errorcount; |
4358 | } |
4359 | |
4360 | /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding |
4361 | FIELD_DECL, otherwise return DECL itself. */ |
4362 | |
4363 | static tree |
4364 | omp_clause_decl_field (tree decl) |
4365 | { |
4366 | if (VAR_P (decl) |
4367 | && DECL_HAS_VALUE_EXPR_P (decl) |
4368 | && DECL_ARTIFICIAL (decl) |
4369 | && DECL_LANG_SPECIFIC (decl) |
4370 | && DECL_OMP_PRIVATIZED_MEMBER (decl)) |
4371 | { |
4372 | tree f = DECL_VALUE_EXPR (decl); |
4373 | if (TREE_CODE (f) == INDIRECT_REF) |
4374 | f = TREE_OPERAND (f, 0); |
4375 | if (TREE_CODE (f) == COMPONENT_REF) |
4376 | { |
4377 | f = TREE_OPERAND (f, 1); |
4378 | gcc_assert (TREE_CODE (f) == FIELD_DECL); |
4379 | return f; |
4380 | } |
4381 | } |
4382 | return NULL_TREE; |
4383 | } |
4384 | |
4385 | /* Adjust DECL if needed for printing using %qE. */ |
4386 | |
4387 | static tree |
4388 | omp_clause_printable_decl (tree decl) |
4389 | { |
4390 | tree t = omp_clause_decl_field (decl); |
4391 | if (t) |
4392 | return t; |
4393 | return decl; |
4394 | } |
4395 | |
4396 | /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER |
4397 | VAR_DECL T that doesn't need a DECL_EXPR added, record it for |
4398 | privatization. */ |
4399 | |
4400 | static void |
4401 | omp_note_field_privatization (tree f, tree t) |
4402 | { |
4403 | if (!omp_private_member_map) |
4404 | omp_private_member_map = new hash_map<tree, tree>; |
4405 | tree &v = omp_private_member_map->get_or_insert (f); |
4406 | if (v == NULL_TREE) |
4407 | { |
4408 | v = t; |
4409 | omp_private_member_vec.safe_push (f); |
4410 | /* Signal that we don't want to create DECL_EXPR for this dummy var. */ |
4411 | omp_private_member_vec.safe_push (integer_zero_node); |
4412 | } |
4413 | } |
4414 | |
4415 | /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER |
4416 | dummy VAR_DECL. */ |
4417 | |
4418 | tree |
4419 | omp_privatize_field (tree t, bool shared) |
4420 | { |
4421 | tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE); |
4422 | if (m == error_mark_node) |
4423 | return error_mark_node; |
4424 | if (!omp_private_member_map && !shared) |
4425 | omp_private_member_map = new hash_map<tree, tree>; |
4426 | if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) |
4427 | { |
4428 | gcc_assert (TREE_CODE (m) == INDIRECT_REF); |
4429 | m = TREE_OPERAND (m, 0); |
4430 | } |
4431 | tree vb = NULL_TREE; |
4432 | tree &v = shared ? vb : omp_private_member_map->get_or_insert (t); |
4433 | if (v == NULL_TREE) |
4434 | { |
4435 | v = create_temporary_var (TREE_TYPE (m)); |
4436 | retrofit_lang_decl (v); |
4437 | DECL_OMP_PRIVATIZED_MEMBER (v) = 1; |
4438 | SET_DECL_VALUE_EXPR (v, m); |
4439 | DECL_HAS_VALUE_EXPR_P (v) = 1; |
4440 | if (!shared) |
4441 | omp_private_member_vec.safe_push (t); |
4442 | } |
4443 | return v; |
4444 | } |
4445 | |
4446 | /* Helper function for handle_omp_array_sections. Called recursively |
4447 | to handle multiple array-section-subscripts. C is the clause, |
4448 | T current expression (initially OMP_CLAUSE_DECL), which is either |
4449 | a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound |
4450 | expression if specified, TREE_VALUE length expression if specified, |
4451 | TREE_CHAIN is what it has been specified after, or some decl. |
4452 | TYPES vector is populated with array section types, MAYBE_ZERO_LEN |
4453 | set to true if any of the array-section-subscript could have length |
4454 | of zero (explicit or implicit), FIRST_NON_ONE is the index of the |
4455 | first array-section-subscript which is known not to have length |
4456 | of one. Given say: |
4457 | map(a[:b][2:1][:c][:2][:d][e:f][2:5]) |
4458 | FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c] |
4459 | all are or may have length of 1, array-section-subscript [:2] is the |
4460 | first one known not to have length 1. For array-section-subscript |
4461 | <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't |
4462 | 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we |
4463 | can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above |
4464 | case though, as some lengths could be zero. */ |
4465 | |
4466 | static tree |
4467 | handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types, |
4468 | bool &maybe_zero_len, unsigned int &first_non_one, |
4469 | enum c_omp_region_type ort) |
4470 | { |
4471 | tree ret, low_bound, length, type; |
4472 | if (TREE_CODE (t) != TREE_LIST) |
4473 | { |
4474 | if (error_operand_p (t)) |
4475 | return error_mark_node; |
4476 | if (REFERENCE_REF_P (t) |
4477 | && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF) |
4478 | t = TREE_OPERAND (t, 0); |
4479 | ret = t; |
4480 | if (TREE_CODE (t) == COMPONENT_REF |
4481 | && ort == C_ORT_OMP |
4482 | && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP |
4483 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO |
4484 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM) |
4485 | && !type_dependent_expression_p (t)) |
4486 | { |
4487 | if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL |
4488 | && DECL_BIT_FIELD (TREE_OPERAND (t, 1))) |
4489 | { |
4490 | error_at (OMP_CLAUSE_LOCATION (c), |
4491 | "bit-field %qE in %qs clause" , |
4492 | t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]); |
4493 | return error_mark_node; |
4494 | } |
4495 | while (TREE_CODE (t) == COMPONENT_REF) |
4496 | { |
4497 | if (TREE_TYPE (TREE_OPERAND (t, 0)) |
4498 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE) |
4499 | { |
4500 | |
---|