150397Sobrien/* Perform the semantic phase of parsing, i.e., the process of 250397Sobrien building tree structure, checking semantic consistency, and 350397Sobrien building RTL. These routines are used both during actual parsing 4169689Skan and during the instantiation of template functions. 550397Sobrien 6169689Skan Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 7169689Skan Free Software Foundation, Inc. 850397Sobrien Written by Mark Mitchell (mmitchell@usa.net) based on code found 9169689Skan formerly in parse.y and pt.c. 1050397Sobrien 11132718Skan This file is part of GCC. 1250397Sobrien 13132718Skan GCC is free software; you can redistribute it and/or modify it 1450397Sobrien under the terms of the GNU General Public License as published by 1550397Sobrien the Free Software Foundation; either version 2, or (at your option) 1650397Sobrien any later version. 17169689Skan 18132718Skan GCC is distributed in the hope that it will be useful, but 1950397Sobrien WITHOUT ANY WARRANTY; without even the implied warranty of 2050397Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 2150397Sobrien General Public License for more details. 22169689Skan 2350397Sobrien You should have received a copy of the GNU General Public License 24132718Skan along with GCC; see the file COPYING. If not, write to the Free 25169689Skan Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 26169689Skan 02110-1301, USA. */ 2750397Sobrien 2850397Sobrien#include "config.h" 2950397Sobrien#include "system.h" 30132718Skan#include "coretypes.h" 31132718Skan#include "tm.h" 3250397Sobrien#include "tree.h" 3350397Sobrien#include "cp-tree.h" 34169689Skan#include "c-common.h" 3590075Sobrien#include "tree-inline.h" 36169689Skan#include "tree-mudflap.h" 3750397Sobrien#include "except.h" 3850397Sobrien#include "toplev.h" 3990075Sobrien#include "flags.h" 4090075Sobrien#include "rtl.h" 4190075Sobrien#include "expr.h" 4290075Sobrien#include "output.h" 4390075Sobrien#include "timevar.h" 4490075Sobrien#include "debug.h" 45169689Skan#include "diagnostic.h" 46132718Skan#include "cgraph.h" 47169689Skan#include "tree-iterator.h" 48169689Skan#include "vec.h" 49169689Skan#include "target.h" 5050397Sobrien 5150397Sobrien/* There routines provide a modular interface to perform many parsing 5250397Sobrien operations. They may therefore be used during actual parsing, or 5350397Sobrien during template instantiation, which may be regarded as a 54169689Skan degenerate form of parsing. */ 5550397Sobrien 56132718Skanstatic tree maybe_convert_cond (tree); 57132718Skanstatic tree simplify_aggr_init_exprs_r (tree *, int *, void *); 58132718Skanstatic void emit_associated_thunks (tree); 59169689Skanstatic tree finalize_nrv_r (tree *, int *, void *); 6050397Sobrien 61132718Skan 62132718Skan/* Deferred Access Checking Overview 63132718Skan --------------------------------- 64132718Skan 65132718Skan Most C++ expressions and declarations require access checking 66132718Skan to be performed during parsing. However, in several cases, 67132718Skan this has to be treated differently. 68132718Skan 69132718Skan For member declarations, access checking has to be deferred 70132718Skan until more information about the declaration is known. For 71132718Skan example: 72132718Skan 73132718Skan class A { 74169689Skan typedef int X; 75132718Skan public: 76169689Skan X f(); 77132718Skan }; 78132718Skan 79132718Skan A::X A::f(); 80132718Skan A::X g(); 81132718Skan 82132718Skan When we are parsing the function return type `A::X', we don't 83132718Skan really know if this is allowed until we parse the function name. 84132718Skan 85132718Skan Furthermore, some contexts require that access checking is 86132718Skan never performed at all. These include class heads, and template 87132718Skan instantiations. 88132718Skan 89132718Skan Typical use of access checking functions is described here: 90169689Skan 91132718Skan 1. When we enter a context that requires certain access checking 92132718Skan mode, the function `push_deferring_access_checks' is called with 93132718Skan DEFERRING argument specifying the desired mode. Access checking 94132718Skan may be performed immediately (dk_no_deferred), deferred 95132718Skan (dk_deferred), or not performed (dk_no_check). 96132718Skan 97132718Skan 2. When a declaration such as a type, or a variable, is encountered, 98132718Skan the function `perform_or_defer_access_check' is called. It 99169689Skan maintains a VEC of all deferred checks. 100132718Skan 101132718Skan 3. The global `current_class_type' or `current_function_decl' is then 102132718Skan setup by the parser. `enforce_access' relies on these information 103132718Skan to check access. 104132718Skan 105132718Skan 4. Upon exiting the context mentioned in step 1, 106132718Skan `perform_deferred_access_checks' is called to check all declaration 107169689Skan stored in the VEC. `pop_deferring_access_checks' is then 108132718Skan called to restore the previous access checking mode. 109132718Skan 110132718Skan In case of parsing error, we simply call `pop_deferring_access_checks' 111132718Skan without `perform_deferred_access_checks'. */ 112132718Skan 113169689Skantypedef struct deferred_access GTY(()) 114169689Skan{ 115169689Skan /* A VEC representing name-lookups for which we have deferred 116169689Skan checking access controls. We cannot check the accessibility of 117169689Skan names used in a decl-specifier-seq until we know what is being 118169689Skan declared because code like: 119169689Skan 120169689Skan class A { 121169689Skan class B {}; 122169689Skan B* f(); 123169689Skan } 124169689Skan 125169689Skan A::B* A::f() { return 0; } 126169689Skan 127169689Skan is valid, even though `A::B' is not generally accessible. */ 128169689Skan VEC (deferred_access_check,gc)* GTY(()) deferred_access_checks; 129169689Skan 130169689Skan /* The current mode of access checks. */ 131169689Skan enum deferring_kind deferring_access_checks_kind; 132169689Skan 133169689Skan} deferred_access; 134169689SkanDEF_VEC_O (deferred_access); 135169689SkanDEF_VEC_ALLOC_O (deferred_access,gc); 136169689Skan 137132718Skan/* Data for deferred access checking. */ 138169689Skanstatic GTY(()) VEC(deferred_access,gc) *deferred_access_stack; 139169689Skanstatic GTY(()) unsigned deferred_access_no_check; 140132718Skan 141132718Skan/* Save the current deferred access states and start deferred 142132718Skan access checking iff DEFER_P is true. */ 143132718Skan 144132718Skanvoid 145132718Skanpush_deferring_access_checks (deferring_kind deferring) 146132718Skan{ 147132718Skan /* For context like template instantiation, access checking 148132718Skan disabling applies to all nested context. */ 149169689Skan if (deferred_access_no_check || deferring == dk_no_check) 150169689Skan deferred_access_no_check++; 151169689Skan else 152169689Skan { 153169689Skan deferred_access *ptr; 154132718Skan 155169689Skan ptr = VEC_safe_push (deferred_access, gc, deferred_access_stack, NULL); 156169689Skan ptr->deferred_access_checks = NULL; 157169689Skan ptr->deferring_access_checks_kind = deferring; 158132718Skan } 159132718Skan} 160132718Skan 161132718Skan/* Resume deferring access checks again after we stopped doing 162132718Skan this previously. */ 163132718Skan 164132718Skanvoid 165132718Skanresume_deferring_access_checks (void) 166132718Skan{ 167169689Skan if (!deferred_access_no_check) 168169689Skan VEC_last (deferred_access, deferred_access_stack) 169169689Skan ->deferring_access_checks_kind = dk_deferred; 170132718Skan} 171132718Skan 172132718Skan/* Stop deferring access checks. */ 173132718Skan 174132718Skanvoid 175132718Skanstop_deferring_access_checks (void) 176132718Skan{ 177169689Skan if (!deferred_access_no_check) 178169689Skan VEC_last (deferred_access, deferred_access_stack) 179169689Skan ->deferring_access_checks_kind = dk_no_deferred; 180132718Skan} 181132718Skan 182132718Skan/* Discard the current deferred access checks and restore the 183132718Skan previous states. */ 184132718Skan 185132718Skanvoid 186132718Skanpop_deferring_access_checks (void) 187132718Skan{ 188169689Skan if (deferred_access_no_check) 189169689Skan deferred_access_no_check--; 190169689Skan else 191169689Skan VEC_pop (deferred_access, deferred_access_stack); 192132718Skan} 193132718Skan 194169689Skan/* Returns a TREE_LIST representing the deferred checks. 195169689Skan The TREE_PURPOSE of each node is the type through which the 196132718Skan access occurred; the TREE_VALUE is the declaration named. 197132718Skan */ 198132718Skan 199169689SkanVEC (deferred_access_check,gc)* 200132718Skanget_deferred_access_checks (void) 201132718Skan{ 202169689Skan if (deferred_access_no_check) 203169689Skan return NULL; 204169689Skan else 205169689Skan return (VEC_last (deferred_access, deferred_access_stack) 206169689Skan ->deferred_access_checks); 207132718Skan} 208132718Skan 209132718Skan/* Take current deferred checks and combine with the 210132718Skan previous states if we also defer checks previously. 211132718Skan Otherwise perform checks now. */ 212132718Skan 213132718Skanvoid 214132718Skanpop_to_parent_deferring_access_checks (void) 215132718Skan{ 216169689Skan if (deferred_access_no_check) 217169689Skan deferred_access_no_check--; 218169689Skan else 219169689Skan { 220169689Skan VEC (deferred_access_check,gc) *checks; 221169689Skan deferred_access *ptr; 222132718Skan 223169689Skan checks = (VEC_last (deferred_access, deferred_access_stack) 224169689Skan ->deferred_access_checks); 225132718Skan 226169689Skan VEC_pop (deferred_access, deferred_access_stack); 227169689Skan ptr = VEC_last (deferred_access, deferred_access_stack); 228169689Skan if (ptr->deferring_access_checks_kind == dk_no_deferred) 229169689Skan { 230169689Skan /* Check access. */ 231169689Skan perform_access_checks (checks); 232169689Skan } 233169689Skan else 234169689Skan { 235169689Skan /* Merge with parent. */ 236169689Skan int i, j; 237169689Skan deferred_access_check *chk, *probe; 238132718Skan 239169689Skan for (i = 0 ; 240169689Skan VEC_iterate (deferred_access_check, checks, i, chk) ; 241169689Skan ++i) 242169689Skan { 243169689Skan for (j = 0 ; 244169689Skan VEC_iterate (deferred_access_check, 245169689Skan ptr->deferred_access_checks, j, probe) ; 246169689Skan ++j) 247169689Skan { 248169689Skan if (probe->binfo == chk->binfo && 249169689Skan probe->decl == chk->decl && 250169689Skan probe->diag_decl == chk->diag_decl) 251169689Skan goto found; 252169689Skan } 253169689Skan /* Insert into parent's checks. */ 254169689Skan VEC_safe_push (deferred_access_check, gc, 255169689Skan ptr->deferred_access_checks, chk); 256169689Skan found:; 257169689Skan } 258169689Skan } 259169689Skan } 260132718Skan} 261132718Skan 262169689Skan/* Perform the access checks in CHECKS. The TREE_PURPOSE of each node 263169689Skan is the BINFO indicating the qualifying scope used to access the 264169689Skan DECL node stored in the TREE_VALUE of the node. */ 265169689Skan 266169689Skanvoid 267169689Skanperform_access_checks (VEC (deferred_access_check,gc)* checks) 268169689Skan{ 269169689Skan int i; 270169689Skan deferred_access_check *chk; 271169689Skan 272169689Skan if (!checks) 273169689Skan return; 274169689Skan 275169689Skan for (i = 0 ; VEC_iterate (deferred_access_check, checks, i, chk) ; ++i) 276169689Skan enforce_access (chk->binfo, chk->decl, chk->diag_decl); 277169689Skan} 278169689Skan 279132718Skan/* Perform the deferred access checks. 280132718Skan 281132718Skan After performing the checks, we still have to keep the list 282132718Skan `deferred_access_stack->deferred_access_checks' since we may want 283132718Skan to check access for them again later in a different context. 284132718Skan For example: 285132718Skan 286132718Skan class A { 287132718Skan typedef int X; 288132718Skan static X a; 289132718Skan }; 290132718Skan A::X A::a, x; // No error for `A::a', error for `x' 291132718Skan 292132718Skan We have to perform deferred access of `A::X', first with `A::a', 293132718Skan next with `x'. */ 294132718Skan 295132718Skanvoid 296132718Skanperform_deferred_access_checks (void) 297132718Skan{ 298169689Skan perform_access_checks (get_deferred_access_checks ()); 299132718Skan} 300132718Skan 301132718Skan/* Defer checking the accessibility of DECL, when looked up in 302169689Skan BINFO. DIAG_DECL is the declaration to use to print diagnostics. */ 303132718Skan 304132718Skanvoid 305169689Skanperform_or_defer_access_check (tree binfo, tree decl, tree diag_decl) 306132718Skan{ 307169689Skan int i; 308169689Skan deferred_access *ptr; 309169689Skan deferred_access_check *chk; 310169689Skan deferred_access_check *new_access; 311132718Skan 312169689Skan 313169689Skan /* Exit if we are in a context that no access checking is performed. 314169689Skan */ 315169689Skan if (deferred_access_no_check) 316169689Skan return; 317169689Skan 318169689Skan gcc_assert (TREE_CODE (binfo) == TREE_BINFO); 319169689Skan 320169689Skan ptr = VEC_last (deferred_access, deferred_access_stack); 321169689Skan 322132718Skan /* If we are not supposed to defer access checks, just check now. */ 323169689Skan if (ptr->deferring_access_checks_kind == dk_no_deferred) 324132718Skan { 325169689Skan enforce_access (binfo, decl, diag_decl); 326132718Skan return; 327132718Skan } 328132718Skan 329132718Skan /* See if we are already going to perform this check. */ 330169689Skan for (i = 0 ; 331169689Skan VEC_iterate (deferred_access_check, 332169689Skan ptr->deferred_access_checks, i, chk) ; 333169689Skan ++i) 334169689Skan { 335169689Skan if (chk->decl == decl && chk->binfo == binfo && 336169689Skan chk->diag_decl == diag_decl) 337169689Skan { 338169689Skan return; 339169689Skan } 340169689Skan } 341132718Skan /* If not, record the check. */ 342169689Skan new_access = 343169689Skan VEC_safe_push (deferred_access_check, gc, 344169689Skan ptr->deferred_access_checks, 0); 345169689Skan new_access->binfo = binfo; 346169689Skan new_access->decl = decl; 347169689Skan new_access->diag_decl = diag_decl; 348132718Skan} 349132718Skan 350117395Skan/* Returns nonzero if the current statement is a full expression, 35190075Sobrien i.e. temporaries created during that statement should be destroyed 35290075Sobrien at the end of the statement. */ 35350397Sobrien 35490075Sobrienint 355132718Skanstmts_are_full_exprs_p (void) 35690075Sobrien{ 35790075Sobrien return current_stmt_tree ()->stmts_are_full_exprs_p; 35890075Sobrien} 35950397Sobrien 360169689Skan/* T is a statement. Add it to the statement-tree. This is the C++ 361169689Skan version. The C/ObjC frontends have a slightly different version of 362169689Skan this function. */ 363169689Skan 364169689Skantree 365169689Skanadd_stmt (tree t) 366169689Skan{ 367169689Skan enum tree_code code = TREE_CODE (t); 368169689Skan 369169689Skan if (EXPR_P (t) && code != LABEL_EXPR) 370169689Skan { 371169689Skan if (!EXPR_HAS_LOCATION (t)) 372169689Skan SET_EXPR_LOCATION (t, input_location); 373169689Skan 374169689Skan /* When we expand a statement-tree, we must know whether or not the 375169689Skan statements are full-expressions. We record that fact here. */ 376169689Skan STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p (); 377169689Skan } 378169689Skan 379169689Skan /* Add T to the statement-tree. Non-side-effect statements need to be 380169689Skan recorded during statement expressions. */ 381169689Skan append_to_statement_list_force (t, &cur_stmt_list); 382169689Skan 383169689Skan return t; 384169689Skan} 385169689Skan 38690075Sobrien/* Returns the stmt_tree (if any) to which statements are currently 38790075Sobrien being added. If there is no active statement-tree, NULL is 38890075Sobrien returned. */ 38990075Sobrien 39090075Sobrienstmt_tree 391132718Skancurrent_stmt_tree (void) 39290075Sobrien{ 393169689Skan return (cfun 394169689Skan ? &cfun->language->base.x_stmt_tree 39590075Sobrien : &scope_chain->x_stmt_tree); 39690075Sobrien} 39790075Sobrien 398169689Skan/* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */ 399169689Skan 400169689Skanstatic tree 401169689Skanmaybe_cleanup_point_expr (tree expr) 402169689Skan{ 403169689Skan if (!processing_template_decl && stmts_are_full_exprs_p ()) 404169689Skan expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr); 405169689Skan return expr; 406169689Skan} 407169689Skan 408169689Skan/* Like maybe_cleanup_point_expr except have the type of the new expression be 409169689Skan void so we don't need to create a temporary variable to hold the inner 410169689Skan expression. The reason why we do this is because the original type might be 411169689Skan an aggregate and we cannot create a temporary variable for that type. */ 412169689Skan 413169689Skanstatic tree 414169689Skanmaybe_cleanup_point_expr_void (tree expr) 415169689Skan{ 416169689Skan if (!processing_template_decl && stmts_are_full_exprs_p ()) 417169689Skan expr = fold_build_cleanup_point_expr (void_type_node, expr); 418169689Skan return expr; 419169689Skan} 420169689Skan 421169689Skan 422169689Skan 423169689Skan/* Create a declaration statement for the declaration given by the DECL. */ 424169689Skan 425169689Skanvoid 426169689Skanadd_decl_expr (tree decl) 427169689Skan{ 428169689Skan tree r = build_stmt (DECL_EXPR, decl); 429169689Skan if (DECL_INITIAL (decl) 430169689Skan || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl)))) 431169689Skan r = maybe_cleanup_point_expr_void (r); 432169689Skan add_stmt (r); 433169689Skan} 434169689Skan 43590075Sobrien/* Nonzero if TYPE is an anonymous union or struct type. We have to use a 43690075Sobrien flag for this because "A union for which objects or pointers are 43790075Sobrien declared is not an anonymous union" [class.union]. */ 43890075Sobrien 43990075Sobrienint 440132718Skananon_aggr_type_p (tree node) 44190075Sobrien{ 442117395Skan return ANON_AGGR_TYPE_P (node); 44390075Sobrien} 44490075Sobrien 44590075Sobrien/* Finish a scope. */ 44690075Sobrien 44790075Sobrientree 448169689Skando_poplevel (tree stmt_list) 44990075Sobrien{ 450169689Skan tree block = NULL; 45190075Sobrien 45290075Sobrien if (stmts_are_full_exprs_p ()) 453169689Skan block = poplevel (kept_level_p (), 1, 0); 454169689Skan 455169689Skan stmt_list = pop_stmt_list (stmt_list); 456169689Skan 457169689Skan if (!processing_template_decl) 45890075Sobrien { 459169689Skan stmt_list = c_build_bind_expr (block, stmt_list); 460169689Skan /* ??? See c_end_compound_stmt re statement expressions. */ 46190075Sobrien } 46290075Sobrien 463169689Skan return stmt_list; 46490075Sobrien} 46590075Sobrien 466169689Skan/* Begin a new scope. */ 46790075Sobrien 468169689Skanstatic tree 469132718Skando_pushlevel (scope_kind sk) 47090075Sobrien{ 471169689Skan tree ret = push_stmt_list (); 47290075Sobrien if (stmts_are_full_exprs_p ()) 473169689Skan begin_scope (sk, NULL); 474169689Skan return ret; 475169689Skan} 476169689Skan 477169689Skan/* Queue a cleanup. CLEANUP is an expression/statement to be executed 478169689Skan when the current scope is exited. EH_ONLY is true when this is not 479169689Skan meant to apply to normal control flow transfer. */ 480169689Skan 481169689Skanvoid 482169689Skanpush_cleanup (tree decl, tree cleanup, bool eh_only) 483169689Skan{ 484169689Skan tree stmt = build_stmt (CLEANUP_STMT, NULL, cleanup, decl); 485169689Skan CLEANUP_EH_ONLY (stmt) = eh_only; 486169689Skan add_stmt (stmt); 487169689Skan CLEANUP_BODY (stmt) = push_stmt_list (); 488169689Skan} 489169689Skan 490169689Skan/* Begin a conditional that might contain a declaration. When generating 491169689Skan normal code, we want the declaration to appear before the statement 492169689Skan containing the conditional. When generating template code, we want the 493169689Skan conditional to be rendered as the raw DECL_EXPR. */ 494169689Skan 495169689Skanstatic void 496169689Skanbegin_cond (tree *cond_p) 497169689Skan{ 498169689Skan if (processing_template_decl) 499169689Skan *cond_p = push_stmt_list (); 500169689Skan} 501169689Skan 502169689Skan/* Finish such a conditional. */ 503169689Skan 504169689Skanstatic void 505169689Skanfinish_cond (tree *cond_p, tree expr) 506169689Skan{ 507169689Skan if (processing_template_decl) 50890075Sobrien { 509169689Skan tree cond = pop_stmt_list (*cond_p); 510169689Skan if (TREE_CODE (cond) == DECL_EXPR) 511169689Skan expr = cond; 51290075Sobrien } 513169689Skan *cond_p = expr; 51490075Sobrien} 51590075Sobrien 516169689Skan/* If *COND_P specifies a conditional with a declaration, transform the 517169689Skan loop such that 518169689Skan while (A x = 42) { } 519169689Skan for (; A x = 42;) { } 520169689Skan becomes 521169689Skan while (true) { A x = 42; if (!x) break; } 522169689Skan for (;;) { A x = 42; if (!x) break; } 523169689Skan The statement list for BODY will be empty if the conditional did 524169689Skan not declare anything. */ 525169689Skan 526169689Skanstatic void 527169689Skansimplify_loop_decl_cond (tree *cond_p, tree body) 528169689Skan{ 529169689Skan tree cond, if_stmt; 530169689Skan 531169689Skan if (!TREE_SIDE_EFFECTS (body)) 532169689Skan return; 533169689Skan 534169689Skan cond = *cond_p; 535169689Skan *cond_p = boolean_true_node; 536169689Skan 537169689Skan if_stmt = begin_if_stmt (); 538169689Skan cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0); 539169689Skan finish_if_stmt_cond (cond, if_stmt); 540169689Skan finish_break_stmt (); 541169689Skan finish_then_clause (if_stmt); 542169689Skan finish_if_stmt (if_stmt); 543169689Skan} 544169689Skan 54590075Sobrien/* Finish a goto-statement. */ 54690075Sobrien 54790075Sobrientree 548132718Skanfinish_goto_stmt (tree destination) 54990075Sobrien{ 55090075Sobrien if (TREE_CODE (destination) == IDENTIFIER_NODE) 55190075Sobrien destination = lookup_label (destination); 55290075Sobrien 55390075Sobrien /* We warn about unused labels with -Wunused. That means we have to 55490075Sobrien mark the used labels as used. */ 55590075Sobrien if (TREE_CODE (destination) == LABEL_DECL) 55690075Sobrien TREE_USED (destination) = 1; 557132718Skan else 558132718Skan { 559132718Skan /* The DESTINATION is being used as an rvalue. */ 560132718Skan if (!processing_template_decl) 561132718Skan destination = decay_conversion (destination); 562132718Skan /* We don't inline calls to functions with computed gotos. 563132718Skan Those functions are typically up to some funny business, 564132718Skan and may be depending on the labels being at particular 565132718Skan addresses, or some such. */ 566132718Skan DECL_UNINLINABLE (current_function_decl) = 1; 567132718Skan } 568169689Skan 56990075Sobrien check_goto (destination); 57090075Sobrien 571169689Skan return add_stmt (build_stmt (GOTO_EXPR, destination)); 57290075Sobrien} 57390075Sobrien 57490075Sobrien/* COND is the condition-expression for an if, while, etc., 57590075Sobrien statement. Convert it to a boolean value, if appropriate. */ 57690075Sobrien 577132718Skanstatic tree 578132718Skanmaybe_convert_cond (tree cond) 57990075Sobrien{ 58090075Sobrien /* Empty conditions remain empty. */ 58190075Sobrien if (!cond) 58290075Sobrien return NULL_TREE; 58390075Sobrien 58490075Sobrien /* Wait until we instantiate templates before doing conversion. */ 58590075Sobrien if (processing_template_decl) 58690075Sobrien return cond; 58790075Sobrien 58890075Sobrien /* Do the conversion. */ 58990075Sobrien cond = convert_from_reference (cond); 590259268Spfg 591259268Spfg if (TREE_CODE (cond) == MODIFY_EXPR 592259268Spfg && !TREE_NO_WARNING (cond) 593259268Spfg && warn_parentheses) 594259268Spfg { 595259268Spfg warning (OPT_Wparentheses, 596259268Spfg "suggest parentheses around assignment used as truth value"); 597259268Spfg TREE_NO_WARNING (cond) = 1; 598259268Spfg } 599259268Spfg 60090075Sobrien return condition_conversion (cond); 60190075Sobrien} 60290075Sobrien 60350397Sobrien/* Finish an expression-statement, whose EXPRESSION is as indicated. */ 60450397Sobrien 60590075Sobrientree 606132718Skanfinish_expr_stmt (tree expr) 60750397Sobrien{ 60890075Sobrien tree r = NULL_TREE; 60990075Sobrien 61050397Sobrien if (expr != NULL_TREE) 61150397Sobrien { 612132718Skan if (!processing_template_decl) 613169689Skan { 614169689Skan if (warn_sequence_point) 615169689Skan verify_sequence_points (expr); 616169689Skan expr = convert_to_void (expr, "statement"); 617169689Skan } 618132718Skan else if (!type_dependent_expression_p (expr)) 619132718Skan convert_to_void (build_non_dependent_expr (expr), "statement"); 620169689Skan 621169689Skan /* Simplification of inner statement expressions, compound exprs, 622169689Skan etc can result in us already having an EXPR_STMT. */ 623169689Skan if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) 624169689Skan { 625169689Skan if (TREE_CODE (expr) != EXPR_STMT) 626169689Skan expr = build_stmt (EXPR_STMT, expr); 627169689Skan expr = maybe_cleanup_point_expr_void (expr); 628169689Skan } 629169689Skan 630169689Skan r = add_stmt (expr); 63150397Sobrien } 63250397Sobrien 63350397Sobrien finish_stmt (); 63490075Sobrien 63590075Sobrien return r; 63650397Sobrien} 63750397Sobrien 63890075Sobrien 63950397Sobrien/* Begin an if-statement. Returns a newly created IF_STMT if 64050397Sobrien appropriate. */ 64150397Sobrien 64250397Sobrientree 643132718Skanbegin_if_stmt (void) 64450397Sobrien{ 645169689Skan tree r, scope; 646169689Skan scope = do_pushlevel (sk_block); 64790075Sobrien r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE); 648169689Skan TREE_CHAIN (r) = scope; 649169689Skan begin_cond (&IF_COND (r)); 65050397Sobrien return r; 65150397Sobrien} 65250397Sobrien 65350397Sobrien/* Process the COND of an if-statement, which may be given by 65450397Sobrien IF_STMT. */ 65550397Sobrien 656169689Skanvoid 657132718Skanfinish_if_stmt_cond (tree cond, tree if_stmt) 65850397Sobrien{ 659169689Skan finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond)); 660169689Skan add_stmt (if_stmt); 661169689Skan THEN_CLAUSE (if_stmt) = push_stmt_list (); 66250397Sobrien} 66350397Sobrien 66450397Sobrien/* Finish the then-clause of an if-statement, which may be given by 66550397Sobrien IF_STMT. */ 66650397Sobrien 66750397Sobrientree 668132718Skanfinish_then_clause (tree if_stmt) 66950397Sobrien{ 670169689Skan THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt)); 67190075Sobrien return if_stmt; 67250397Sobrien} 67350397Sobrien 67450397Sobrien/* Begin the else-clause of an if-statement. */ 67550397Sobrien 676169689Skanvoid 677169689Skanbegin_else_clause (tree if_stmt) 67850397Sobrien{ 679169689Skan ELSE_CLAUSE (if_stmt) = push_stmt_list (); 68050397Sobrien} 68150397Sobrien 68250397Sobrien/* Finish the else-clause of an if-statement, which may be given by 68350397Sobrien IF_STMT. */ 68450397Sobrien 68550397Sobrienvoid 686132718Skanfinish_else_clause (tree if_stmt) 68750397Sobrien{ 688169689Skan ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt)); 68950397Sobrien} 69050397Sobrien 69190075Sobrien/* Finish an if-statement. */ 69250397Sobrien 693169689Skanvoid 694169689Skanfinish_if_stmt (tree if_stmt) 69550397Sobrien{ 696169689Skan tree scope = TREE_CHAIN (if_stmt); 697169689Skan TREE_CHAIN (if_stmt) = NULL; 698169689Skan add_stmt (do_poplevel (scope)); 699117395Skan finish_stmt (); 700169689Skan empty_body_warning (THEN_CLAUSE (if_stmt), ELSE_CLAUSE (if_stmt)); 70150397Sobrien} 70250397Sobrien 70350397Sobrien/* Begin a while-statement. Returns a newly created WHILE_STMT if 70450397Sobrien appropriate. */ 70550397Sobrien 70650397Sobrientree 707260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 708260918Spfgbegin_while_stmt (tree attribs) 709260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 71050397Sobrien{ 71150397Sobrien tree r; 712260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 713260918Spfg r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE, attribs); 714260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 71590075Sobrien add_stmt (r); 716169689Skan WHILE_BODY (r) = do_pushlevel (sk_block); 717169689Skan begin_cond (&WHILE_COND (r)); 71850397Sobrien return r; 71950397Sobrien} 72050397Sobrien 72190075Sobrien/* Process the COND of a while-statement, which may be given by 72250397Sobrien WHILE_STMT. */ 72350397Sobrien 724169689Skanvoid 725132718Skanfinish_while_stmt_cond (tree cond, tree while_stmt) 72650397Sobrien{ 727169689Skan finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond)); 728169689Skan simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt)); 72950397Sobrien} 73050397Sobrien 73150397Sobrien/* Finish a while-statement, which may be given by WHILE_STMT. */ 73250397Sobrien 733169689Skanvoid 734132718Skanfinish_while_stmt (tree while_stmt) 73550397Sobrien{ 736169689Skan WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt)); 73750397Sobrien finish_stmt (); 73850397Sobrien} 73950397Sobrien 74050397Sobrien/* Begin a do-statement. Returns a newly created DO_STMT if 74150397Sobrien appropriate. */ 74250397Sobrien 74350397Sobrientree 744260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 745260918Spfgbegin_do_stmt (tree attribs) 746260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 74750397Sobrien{ 748260918Spfg /* APPLE LOCAL radar 4445586 */ 749260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 750260918Spfg tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE, attribs, NULL_TREE); 751260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 75290075Sobrien add_stmt (r); 753169689Skan DO_BODY (r) = push_stmt_list (); 75490075Sobrien return r; 75550397Sobrien} 75650397Sobrien 75750397Sobrien/* Finish the body of a do-statement, which may be given by DO_STMT. */ 75850397Sobrien 75950397Sobrienvoid 760132718Skanfinish_do_body (tree do_stmt) 76150397Sobrien{ 762169689Skan DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt)); 76350397Sobrien} 76450397Sobrien 76550397Sobrien/* Finish a do-statement, which may be given by DO_STMT, and whose 76650397Sobrien COND is as indicated. */ 76750397Sobrien 76850397Sobrienvoid 769132718Skanfinish_do_stmt (tree cond, tree do_stmt) 77050397Sobrien{ 77190075Sobrien cond = maybe_convert_cond (cond); 77290075Sobrien DO_COND (do_stmt) = cond; 77350397Sobrien finish_stmt (); 77450397Sobrien} 77550397Sobrien 77650397Sobrien/* Finish a return-statement. The EXPRESSION returned, if any, is as 77750397Sobrien indicated. */ 77850397Sobrien 77990075Sobrientree 780132718Skanfinish_return_stmt (tree expr) 78150397Sobrien{ 78290075Sobrien tree r; 783169689Skan bool no_warning; 78490075Sobrien 785169689Skan expr = check_return_expr (expr, &no_warning); 786169689Skan 787169689Skan if (flag_openmp && !check_omp_return ()) 788169689Skan return error_mark_node; 78990075Sobrien if (!processing_template_decl) 79090075Sobrien { 791169689Skan if (DECL_DESTRUCTOR_P (current_function_decl) 792169689Skan || (DECL_CONSTRUCTOR_P (current_function_decl) 793169689Skan && targetm.cxx.cdtor_returns_this ())) 79490075Sobrien { 79590075Sobrien /* Similarly, all destructors must run destructors for 79690075Sobrien base-classes before returning. So, all returns in a 79790075Sobrien destructor get sent to the DTOR_LABEL; finish_function emits 79890075Sobrien code to return a value there. */ 799169689Skan return finish_goto_stmt (cdtor_label); 80090075Sobrien } 80190075Sobrien } 802169689Skan 803169689Skan r = build_stmt (RETURN_EXPR, expr); 804169689Skan TREE_NO_WARNING (r) |= no_warning; 805169689Skan r = maybe_cleanup_point_expr_void (r); 806169689Skan r = add_stmt (r); 80750397Sobrien finish_stmt (); 80890075Sobrien 80990075Sobrien return r; 81050397Sobrien} 81150397Sobrien 81250397Sobrien/* Begin a for-statement. Returns a new FOR_STMT if appropriate. */ 81350397Sobrien 81450397Sobrientree 815260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 816260918Spfgbegin_for_stmt (tree attribs) 817260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 81850397Sobrien{ 81950397Sobrien tree r; 82050397Sobrien 821169689Skan r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE, 822260918Spfg/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 823260918Spfg NULL_TREE, NULL_TREE, attribs); 82450397Sobrien 825260918Spfg/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 826260918Spfg 827169689Skan if (flag_new_for_scope > 0) 828169689Skan TREE_CHAIN (r) = do_pushlevel (sk_for); 829169689Skan 830169689Skan if (processing_template_decl) 831169689Skan FOR_INIT_STMT (r) = push_stmt_list (); 832169689Skan 83350397Sobrien return r; 83450397Sobrien} 83550397Sobrien 83650397Sobrien/* Finish the for-init-statement of a for-statement, which may be 83750397Sobrien given by FOR_STMT. */ 83850397Sobrien 83950397Sobrienvoid 840132718Skanfinish_for_init_stmt (tree for_stmt) 84150397Sobrien{ 842169689Skan if (processing_template_decl) 843169689Skan FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt)); 844169689Skan add_stmt (for_stmt); 845169689Skan FOR_BODY (for_stmt) = do_pushlevel (sk_block); 846169689Skan begin_cond (&FOR_COND (for_stmt)); 84750397Sobrien} 84850397Sobrien 84950397Sobrien/* Finish the COND of a for-statement, which may be given by 85050397Sobrien FOR_STMT. */ 85150397Sobrien 85250397Sobrienvoid 853132718Skanfinish_for_cond (tree cond, tree for_stmt) 85450397Sobrien{ 855169689Skan finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond)); 856169689Skan simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt)); 85750397Sobrien} 85850397Sobrien 85950397Sobrien/* Finish the increment-EXPRESSION in a for-statement, which may be 86050397Sobrien given by FOR_STMT. */ 86150397Sobrien 86250397Sobrienvoid 863132718Skanfinish_for_expr (tree expr, tree for_stmt) 86450397Sobrien{ 865169689Skan if (!expr) 866169689Skan return; 867132718Skan /* If EXPR is an overloaded function, issue an error; there is no 868132718Skan context available to use to perform overload resolution. */ 869169689Skan if (type_unknown_p (expr)) 870132718Skan { 871132718Skan cxx_incomplete_type_error (expr, TREE_TYPE (expr)); 872132718Skan expr = error_mark_node; 873132718Skan } 874169689Skan if (!processing_template_decl) 875169689Skan { 876169689Skan if (warn_sequence_point) 877169689Skan verify_sequence_points (expr); 878169689Skan expr = convert_to_void (expr, "3rd expression in for"); 879169689Skan } 880169689Skan else if (!type_dependent_expression_p (expr)) 881169689Skan convert_to_void (build_non_dependent_expr (expr), "3rd expression in for"); 882169689Skan expr = maybe_cleanup_point_expr_void (expr); 88390075Sobrien FOR_EXPR (for_stmt) = expr; 88450397Sobrien} 88550397Sobrien 88650397Sobrien/* Finish the body of a for-statement, which may be given by 88750397Sobrien FOR_STMT. The increment-EXPR for the loop must be 88850397Sobrien provided. */ 88950397Sobrien 89050397Sobrienvoid 891132718Skanfinish_for_stmt (tree for_stmt) 89250397Sobrien{ 893169689Skan FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt)); 894169689Skan 89550397Sobrien /* Pop the scope for the body of the loop. */ 896169689Skan if (flag_new_for_scope > 0) 897169689Skan { 898169689Skan tree scope = TREE_CHAIN (for_stmt); 899169689Skan TREE_CHAIN (for_stmt) = NULL; 900169689Skan add_stmt (do_poplevel (scope)); 901169689Skan } 902169689Skan 903169689Skan finish_stmt (); 90450397Sobrien} 90550397Sobrien 90650397Sobrien/* Finish a break-statement. */ 90750397Sobrien 90890075Sobrientree 909132718Skanfinish_break_stmt (void) 91050397Sobrien{ 911169689Skan return add_stmt (build_stmt (BREAK_STMT)); 91250397Sobrien} 91350397Sobrien 91450397Sobrien/* Finish a continue-statement. */ 91550397Sobrien 91690075Sobrientree 917132718Skanfinish_continue_stmt (void) 91850397Sobrien{ 919169689Skan return add_stmt (build_stmt (CONTINUE_STMT)); 92050397Sobrien} 92150397Sobrien 92290075Sobrien/* Begin a switch-statement. Returns a new SWITCH_STMT if 92390075Sobrien appropriate. */ 92450397Sobrien 92590075Sobrientree 926132718Skanbegin_switch_stmt (void) 92750397Sobrien{ 928169689Skan tree r, scope; 929169689Skan 93096263Sobrien r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE); 931169689Skan 932169689Skan scope = do_pushlevel (sk_block); 933169689Skan TREE_CHAIN (r) = scope; 934169689Skan begin_cond (&SWITCH_STMT_COND (r)); 935169689Skan 93690075Sobrien return r; 93750397Sobrien} 93850397Sobrien 93990075Sobrien/* Finish the cond of a switch-statement. */ 94050397Sobrien 94190075Sobrienvoid 942132718Skanfinish_switch_cond (tree cond, tree switch_stmt) 94350397Sobrien{ 94496263Sobrien tree orig_type = NULL; 94590075Sobrien if (!processing_template_decl) 94650397Sobrien { 94790075Sobrien tree index; 94852284Sobrien 94990075Sobrien /* Convert the condition to an integer or enumeration type. */ 950132718Skan cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true); 95190075Sobrien if (cond == NULL_TREE) 95290075Sobrien { 95390075Sobrien error ("switch quantity not an integer"); 95490075Sobrien cond = error_mark_node; 95590075Sobrien } 95696263Sobrien orig_type = TREE_TYPE (cond); 95790075Sobrien if (cond != error_mark_node) 95890075Sobrien { 959132718Skan /* [stmt.switch] 960132718Skan 961132718Skan Integral promotions are performed. */ 962132718Skan cond = perform_integral_promotions (cond); 963169689Skan cond = maybe_cleanup_point_expr (cond); 96490075Sobrien } 96550397Sobrien 96696263Sobrien if (cond != error_mark_node) 96796263Sobrien { 96896263Sobrien index = get_unwidened (cond, NULL_TREE); 96996263Sobrien /* We can't strip a conversion from a signed type to an unsigned, 97096263Sobrien because if we did, int_fits_type_p would do the wrong thing 97196263Sobrien when checking case values for being in range, 97296263Sobrien and it's too hard to do the right thing. */ 973169689Skan if (TYPE_UNSIGNED (TREE_TYPE (cond)) 974169689Skan == TYPE_UNSIGNED (TREE_TYPE (index))) 97596263Sobrien cond = index; 97696263Sobrien } 97790075Sobrien } 978169689Skan finish_cond (&SWITCH_STMT_COND (switch_stmt), cond); 979169689Skan SWITCH_STMT_TYPE (switch_stmt) = orig_type; 980169689Skan add_stmt (switch_stmt); 98190075Sobrien push_switch (switch_stmt); 982169689Skan SWITCH_STMT_BODY (switch_stmt) = push_stmt_list (); 98350397Sobrien} 98450397Sobrien 98550397Sobrien/* Finish the body of a switch-statement, which may be given by 98650397Sobrien SWITCH_STMT. The COND to switch on is indicated. */ 98750397Sobrien 98850397Sobrienvoid 989132718Skanfinish_switch_stmt (tree switch_stmt) 99050397Sobrien{ 991169689Skan tree scope; 992169689Skan 993169689Skan SWITCH_STMT_BODY (switch_stmt) = 994169689Skan pop_stmt_list (SWITCH_STMT_BODY (switch_stmt)); 995169689Skan pop_switch (); 996117395Skan finish_stmt (); 99750397Sobrien 998169689Skan scope = TREE_CHAIN (switch_stmt); 999169689Skan TREE_CHAIN (switch_stmt) = NULL; 1000169689Skan add_stmt (do_poplevel (scope)); 100150397Sobrien} 100250397Sobrien 100350397Sobrien/* Begin a try-block. Returns a newly-created TRY_BLOCK if 100450397Sobrien appropriate. */ 100550397Sobrien 100650397Sobrientree 1007132718Skanbegin_try_block (void) 100850397Sobrien{ 100990075Sobrien tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE); 101090075Sobrien add_stmt (r); 1011169689Skan TRY_STMTS (r) = push_stmt_list (); 101290075Sobrien return r; 101350397Sobrien} 101450397Sobrien 1015169689Skan/* Likewise, for a function-try-block. The block returned in 1016169689Skan *COMPOUND_STMT is an artificial outer scope, containing the 1017169689Skan function-try-block. */ 101890075Sobrien 101990075Sobrientree 1020169689Skanbegin_function_try_block (tree *compound_stmt) 102190075Sobrien{ 1022169689Skan tree r; 1023169689Skan /* This outer scope does not exist in the C++ standard, but we need 1024169689Skan a place to put __FUNCTION__ and similar variables. */ 1025169689Skan *compound_stmt = begin_compound_stmt (0); 1026169689Skan r = begin_try_block (); 102790075Sobrien FN_TRY_BLOCK_P (r) = 1; 102890075Sobrien return r; 102990075Sobrien} 103090075Sobrien 103150397Sobrien/* Finish a try-block, which may be given by TRY_BLOCK. */ 103250397Sobrien 103350397Sobrienvoid 1034132718Skanfinish_try_block (tree try_block) 103550397Sobrien{ 1036169689Skan TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); 1037169689Skan TRY_HANDLERS (try_block) = push_stmt_list (); 103890075Sobrien} 103990075Sobrien 104090075Sobrien/* Finish the body of a cleanup try-block, which may be given by 104190075Sobrien TRY_BLOCK. */ 104290075Sobrien 104390075Sobrienvoid 1044132718Skanfinish_cleanup_try_block (tree try_block) 104590075Sobrien{ 1046169689Skan TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); 104790075Sobrien} 104890075Sobrien 104990075Sobrien/* Finish an implicitly generated try-block, with a cleanup is given 105090075Sobrien by CLEANUP. */ 105190075Sobrien 105290075Sobrienvoid 1053132718Skanfinish_cleanup (tree cleanup, tree try_block) 105490075Sobrien{ 105590075Sobrien TRY_HANDLERS (try_block) = cleanup; 105690075Sobrien CLEANUP_P (try_block) = 1; 105790075Sobrien} 105890075Sobrien 105990075Sobrien/* Likewise, for a function-try-block. */ 106090075Sobrien 106190075Sobrienvoid 1062132718Skanfinish_function_try_block (tree try_block) 106390075Sobrien{ 1064169689Skan finish_try_block (try_block); 1065169689Skan /* FIXME : something queer about CTOR_INITIALIZER somehow following 1066169689Skan the try block, but moving it inside. */ 106790075Sobrien in_function_try_handler = 1; 106850397Sobrien} 106950397Sobrien 107050397Sobrien/* Finish a handler-sequence for a try-block, which may be given by 107150397Sobrien TRY_BLOCK. */ 107250397Sobrien 107350397Sobrienvoid 1074132718Skanfinish_handler_sequence (tree try_block) 107550397Sobrien{ 1076169689Skan TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block)); 107790075Sobrien check_handlers (TRY_HANDLERS (try_block)); 107850397Sobrien} 107950397Sobrien 1080169689Skan/* Finish the handler-seq for a function-try-block, given by 1081169689Skan TRY_BLOCK. COMPOUND_STMT is the outer block created by 1082169689Skan begin_function_try_block. */ 108390075Sobrien 108490075Sobrienvoid 1085169689Skanfinish_function_handler_sequence (tree try_block, tree compound_stmt) 108690075Sobrien{ 108790075Sobrien in_function_try_handler = 0; 1088169689Skan finish_handler_sequence (try_block); 1089169689Skan finish_compound_stmt (compound_stmt); 109090075Sobrien} 109190075Sobrien 109250397Sobrien/* Begin a handler. Returns a HANDLER if appropriate. */ 109350397Sobrien 109450397Sobrientree 1095132718Skanbegin_handler (void) 109650397Sobrien{ 109750397Sobrien tree r; 1098169689Skan 109990075Sobrien r = build_stmt (HANDLER, NULL_TREE, NULL_TREE); 110090075Sobrien add_stmt (r); 1101169689Skan 110290075Sobrien /* Create a binding level for the eh_info and the exception object 110390075Sobrien cleanup. */ 1104169689Skan HANDLER_BODY (r) = do_pushlevel (sk_catch); 1105169689Skan 110650397Sobrien return r; 110750397Sobrien} 110850397Sobrien 110950397Sobrien/* Finish the handler-parameters for a handler, which may be given by 111090075Sobrien HANDLER. DECL is the declaration for the catch parameter, or NULL 111190075Sobrien if this is a `catch (...)' clause. */ 111250397Sobrien 111350397Sobrienvoid 1114132718Skanfinish_handler_parms (tree decl, tree handler) 111550397Sobrien{ 111690075Sobrien tree type = NULL_TREE; 111750397Sobrien if (processing_template_decl) 111890075Sobrien { 111990075Sobrien if (decl) 112090075Sobrien { 112190075Sobrien decl = pushdecl (decl); 112290075Sobrien decl = push_template_decl (decl); 1123169689Skan HANDLER_PARMS (handler) = decl; 112490075Sobrien type = TREE_TYPE (decl); 112590075Sobrien } 112690075Sobrien } 112790075Sobrien else 112890075Sobrien type = expand_start_catch_block (decl); 112990075Sobrien HANDLER_TYPE (handler) = type; 1130132718Skan if (!processing_template_decl && type) 1131132718Skan mark_used (eh_type_info (type)); 113250397Sobrien} 113350397Sobrien 113490075Sobrien/* Finish a handler, which may be given by HANDLER. The BLOCKs are 113590075Sobrien the return value from the matching call to finish_handler_parms. */ 113650397Sobrien 113750397Sobrienvoid 1138132718Skanfinish_handler (tree handler) 113950397Sobrien{ 114090075Sobrien if (!processing_template_decl) 114150397Sobrien expand_end_catch_block (); 1142169689Skan HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler)); 114350397Sobrien} 114450397Sobrien 1145169689Skan/* Begin a compound statement. FLAGS contains some bits that control the 1146169689Skan behavior and context. If BCS_NO_SCOPE is set, the compound statement 1147169689Skan does not define a scope. If BCS_FN_BODY is set, this is the outermost 1148169689Skan block of a function. If BCS_TRY_BLOCK is set, this is the block 1149169689Skan created on behalf of a TRY statement. Returns a token to be passed to 1150169689Skan finish_compound_stmt. */ 115150397Sobrien 115250397Sobrientree 1153169689Skanbegin_compound_stmt (unsigned int flags) 115450397Sobrien{ 1155169689Skan tree r; 115650397Sobrien 1157169689Skan if (flags & BCS_NO_SCOPE) 1158169689Skan { 1159169689Skan r = push_stmt_list (); 1160169689Skan STATEMENT_LIST_NO_SCOPE (r) = 1; 116190075Sobrien 1162169689Skan /* Normally, we try hard to keep the BLOCK for a statement-expression. 1163169689Skan But, if it's a statement-expression with a scopeless block, there's 1164169689Skan nothing to keep, and we don't want to accidentally keep a block 1165169689Skan *inside* the scopeless block. */ 1166169689Skan keep_next_level (false); 1167169689Skan } 1168169689Skan else 1169169689Skan r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block); 117090075Sobrien 1171169689Skan /* When processing a template, we need to remember where the braces were, 1172169689Skan so that we can set up identical scopes when instantiating the template 1173169689Skan later. BIND_EXPR is a handy candidate for this. 1174169689Skan Note that do_poplevel won't create a BIND_EXPR itself here (and thus 1175169689Skan result in nested BIND_EXPRs), since we don't build BLOCK nodes when 1176169689Skan processing templates. */ 1177169689Skan if (processing_template_decl) 1178169689Skan { 1179169689Skan r = build3 (BIND_EXPR, NULL, NULL, r, NULL); 1180169689Skan BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0; 1181169689Skan BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0; 1182169689Skan TREE_SIDE_EFFECTS (r) = 1; 1183169689Skan } 118490075Sobrien 118550397Sobrien return r; 118650397Sobrien} 118750397Sobrien 1188169689Skan/* Finish a compound-statement, which is given by STMT. */ 118950397Sobrien 1190169689Skanvoid 1191169689Skanfinish_compound_stmt (tree stmt) 119250397Sobrien{ 1193169689Skan if (TREE_CODE (stmt) == BIND_EXPR) 1194169689Skan BIND_EXPR_BODY (stmt) = do_poplevel (BIND_EXPR_BODY (stmt)); 1195169689Skan else if (STATEMENT_LIST_NO_SCOPE (stmt)) 1196169689Skan stmt = pop_stmt_list (stmt); 1197132718Skan else 1198169689Skan { 1199169689Skan /* Destroy any ObjC "super" receivers that may have been 1200169689Skan created. */ 1201169689Skan objc_clear_super_receiver (); 120250397Sobrien 1203169689Skan stmt = do_poplevel (stmt); 1204169689Skan } 120550397Sobrien 1206169689Skan /* ??? See c_end_compound_stmt wrt statement expressions. */ 1207169689Skan add_stmt (stmt); 120850397Sobrien finish_stmt (); 120950397Sobrien} 121050397Sobrien 1211169689Skan/* Finish an asm-statement, whose components are a STRING, some 1212169689Skan OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note 1213169689Skan whether the asm-statement should be considered volatile. */ 121450397Sobrien 121590075Sobrientree 1216169689Skanfinish_asm_stmt (int volatile_p, tree string, tree output_operands, 1217169689Skan tree input_operands, tree clobbers) 121850397Sobrien{ 121990075Sobrien tree r; 122090075Sobrien tree t; 1221169689Skan int ninputs = list_length (input_operands); 1222169689Skan int noutputs = list_length (output_operands); 122390075Sobrien 122490075Sobrien if (!processing_template_decl) 122550397Sobrien { 1226169689Skan const char *constraint; 1227169689Skan const char **oconstraints; 1228169689Skan bool allows_mem, allows_reg, is_inout; 1229169689Skan tree operand; 123090075Sobrien int i; 123190075Sobrien 1232169689Skan oconstraints = (const char **) alloca (noutputs * sizeof (char *)); 1233169689Skan 1234169689Skan string = resolve_asm_operand_names (string, output_operands, 1235169689Skan input_operands); 1236169689Skan 1237169689Skan for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i) 123850397Sobrien { 1239169689Skan operand = TREE_VALUE (t); 1240169689Skan 1241169689Skan /* ??? Really, this should not be here. Users should be using a 1242169689Skan proper lvalue, dammit. But there's a long history of using 1243169689Skan casts in the output operands. In cases like longlong.h, this 1244169689Skan becomes a primitive form of typechecking -- if the cast can be 1245169689Skan removed, then the output operand had a type of the proper width; 1246169689Skan otherwise we'll get an error. Gross, but ... */ 1247169689Skan STRIP_NOPS (operand); 1248169689Skan 1249169689Skan if (!lvalue_or_else (operand, lv_asm)) 1250169689Skan operand = error_mark_node; 1251169689Skan 1252169689Skan if (operand != error_mark_node 1253169689Skan && (TREE_READONLY (operand) 1254169689Skan || CP_TYPE_CONST_P (TREE_TYPE (operand)) 1255169689Skan /* Functions are not modifiable, even though they are 1256169689Skan lvalues. */ 1257169689Skan || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE 1258169689Skan || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE 1259169689Skan /* If it's an aggregate and any field is const, then it is 1260169689Skan effectively const. */ 1261169689Skan || (CLASS_TYPE_P (TREE_TYPE (operand)) 1262169689Skan && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand))))) 1263169689Skan readonly_error (operand, "assignment (via 'asm' output)", 0); 1264169689Skan 1265169689Skan constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); 1266169689Skan oconstraints[i] = constraint; 1267169689Skan 1268169689Skan if (parse_output_constraint (&constraint, i, ninputs, noutputs, 1269169689Skan &allows_mem, &allows_reg, &is_inout)) 127090075Sobrien { 1271169689Skan /* If the operand is going to end up in memory, 1272169689Skan mark it addressable. */ 1273169689Skan if (!allows_reg && !cxx_mark_addressable (operand)) 1274169689Skan operand = error_mark_node; 127590075Sobrien } 1276169689Skan else 1277169689Skan operand = error_mark_node; 1278169689Skan 1279169689Skan TREE_VALUE (t) = operand; 128090075Sobrien } 128152284Sobrien 1282169689Skan for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t)) 128390075Sobrien { 128490075Sobrien constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); 1285169689Skan operand = decay_conversion (TREE_VALUE (t)); 128690075Sobrien 1287169689Skan /* If the type of the operand hasn't been determined (e.g., 1288169689Skan because it involves an overloaded function), then issue 1289169689Skan an error message. There's no context available to 1290169689Skan resolve the overloading. */ 1291169689Skan if (TREE_TYPE (operand) == unknown_type_node) 129290075Sobrien { 1293169689Skan error ("type of asm operand %qE could not be determined", 1294169689Skan TREE_VALUE (t)); 1295169689Skan operand = error_mark_node; 129690075Sobrien } 129790075Sobrien 1298169689Skan if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, 1299169689Skan oconstraints, &allows_mem, &allows_reg)) 1300169689Skan { 1301169689Skan /* If the operand is going to end up in memory, 1302169689Skan mark it addressable. */ 1303169689Skan if (!allows_reg && allows_mem) 1304169689Skan { 1305169689Skan /* Strip the nops as we allow this case. FIXME, this really 1306169689Skan should be rejected or made deprecated. */ 1307169689Skan STRIP_NOPS (operand); 1308169689Skan if (!cxx_mark_addressable (operand)) 1309169689Skan operand = error_mark_node; 1310169689Skan } 1311169689Skan } 1312169689Skan else 1313169689Skan operand = error_mark_node; 1314169689Skan 1315169689Skan TREE_VALUE (t) = operand; 131650397Sobrien } 131790075Sobrien } 131890075Sobrien 1319169689Skan r = build_stmt (ASM_EXPR, string, 132090075Sobrien output_operands, input_operands, 132190075Sobrien clobbers); 1322169689Skan ASM_VOLATILE_P (r) = volatile_p || noutputs == 0; 1323169689Skan r = maybe_cleanup_point_expr_void (r); 132490075Sobrien return add_stmt (r); 132590075Sobrien} 132690075Sobrien 132790075Sobrien/* Finish a label with the indicated NAME. */ 132890075Sobrien 1329132718Skantree 1330132718Skanfinish_label_stmt (tree name) 133190075Sobrien{ 1332132718Skan tree decl = define_label (input_location, name); 1333169689Skan 1334169689Skan if (decl == error_mark_node) 1335169689Skan return error_mark_node; 1336169689Skan 1337169689Skan return add_stmt (build_stmt (LABEL_EXPR, decl)); 133890075Sobrien} 133990075Sobrien 134090075Sobrien/* Finish a series of declarations for local labels. G++ allows users 134190075Sobrien to declare "local" labels, i.e., labels with scope. This extension 134290075Sobrien is useful when writing code involving statement-expressions. */ 134390075Sobrien 134490075Sobrienvoid 1345132718Skanfinish_label_decl (tree name) 134690075Sobrien{ 1347220150Smm if (!at_function_scope_p ()) 1348220150Smm { 1349220150Smm error ("__label__ declarations are only allowed in function scopes"); 1350220150Smm return; 1351220150Smm } 1352220150Smm 1353220150Smm add_decl_expr (declare_local_label (name)); 135490075Sobrien} 135590075Sobrien 1356117395Skan/* When DECL goes out of scope, make sure that CLEANUP is executed. */ 135790075Sobrien 1358169689Skanvoid 1359132718Skanfinish_decl_cleanup (tree decl, tree cleanup) 136090075Sobrien{ 1361169689Skan push_cleanup (decl, cleanup, false); 136290075Sobrien} 136390075Sobrien 1364117395Skan/* If the current scope exits with an exception, run CLEANUP. */ 136590075Sobrien 1366117395Skanvoid 1367132718Skanfinish_eh_cleanup (tree cleanup) 136890075Sobrien{ 1369169689Skan push_cleanup (NULL, cleanup, true); 137090075Sobrien} 137190075Sobrien 1372117395Skan/* The MEM_INITS is a list of mem-initializers, in reverse of the 1373117395Skan order they were written by the user. Each node is as for 1374117395Skan emit_mem_initializers. */ 137590075Sobrien 1376117395Skanvoid 1377117395Skanfinish_mem_initializers (tree mem_inits) 1378117395Skan{ 1379117395Skan /* Reorder the MEM_INITS so that they are in the order they appeared 1380117395Skan in the source program. */ 1381117395Skan mem_inits = nreverse (mem_inits); 138290075Sobrien 138390075Sobrien if (processing_template_decl) 1384117395Skan add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits)); 138590075Sobrien else 1386117395Skan emit_mem_initializers (mem_inits); 138790075Sobrien} 138890075Sobrien 138950397Sobrien/* Finish a parenthesized expression EXPR. */ 139050397Sobrien 139150397Sobrientree 1392132718Skanfinish_parenthesized_expr (tree expr) 139350397Sobrien{ 1394169689Skan if (EXPR_P (expr)) 1395117395Skan /* This inhibits warnings in c_common_truthvalue_conversion. */ 1396169689Skan TREE_NO_WARNING (expr) = 1; 139750397Sobrien 139890075Sobrien if (TREE_CODE (expr) == OFFSET_REF) 139990075Sobrien /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be 140090075Sobrien enclosed in parentheses. */ 140190075Sobrien PTRMEM_OK_P (expr) = 0; 1402169689Skan 1403169689Skan if (TREE_CODE (expr) == STRING_CST) 1404169689Skan PAREN_STRING_LITERAL_P (expr) = 1; 1405169689Skan 140650397Sobrien return expr; 140750397Sobrien} 140850397Sobrien 1409132718Skan/* Finish a reference to a non-static data member (DECL) that is not 1410132718Skan preceded by `.' or `->'. */ 1411132718Skan 1412132718Skantree 1413132718Skanfinish_non_static_data_member (tree decl, tree object, tree qualifying_scope) 1414132718Skan{ 1415169689Skan gcc_assert (TREE_CODE (decl) == FIELD_DECL); 1416132718Skan 1417132718Skan if (!object) 1418132718Skan { 1419169689Skan if (current_function_decl 1420132718Skan && DECL_STATIC_FUNCTION_P (current_function_decl)) 1421169689Skan error ("invalid use of member %q+D in static member function", decl); 1422132718Skan else 1423169689Skan error ("invalid use of non-static data member %q+D", decl); 1424132718Skan error ("from this location"); 1425132718Skan 1426132718Skan return error_mark_node; 1427132718Skan } 1428132718Skan TREE_USED (current_class_ptr) = 1; 1429132718Skan if (processing_template_decl && !qualifying_scope) 1430132718Skan { 1431132718Skan tree type = TREE_TYPE (decl); 1432132718Skan 1433132718Skan if (TREE_CODE (type) == REFERENCE_TYPE) 1434132718Skan type = TREE_TYPE (type); 1435132718Skan else 1436132718Skan { 1437132718Skan /* Set the cv qualifiers. */ 1438132718Skan int quals = cp_type_quals (TREE_TYPE (current_class_ref)); 1439169689Skan 1440132718Skan if (DECL_MUTABLE_P (decl)) 1441132718Skan quals &= ~TYPE_QUAL_CONST; 1442132718Skan 1443132718Skan quals |= cp_type_quals (TREE_TYPE (decl)); 1444132718Skan type = cp_build_qualified_type (type, quals); 1445132718Skan } 1446169689Skan 1447169689Skan return build_min (COMPONENT_REF, type, object, decl, NULL_TREE); 1448132718Skan } 1449132718Skan else 1450132718Skan { 1451132718Skan tree access_type = TREE_TYPE (object); 1452132718Skan tree lookup_context = context_for_name_lookup (decl); 1453169689Skan 1454132718Skan while (!DERIVED_FROM_P (lookup_context, access_type)) 1455132718Skan { 1456132718Skan access_type = TYPE_CONTEXT (access_type); 1457132718Skan while (access_type && DECL_P (access_type)) 1458132718Skan access_type = DECL_CONTEXT (access_type); 1459132718Skan 1460132718Skan if (!access_type) 1461132718Skan { 1462169689Skan error ("object missing in reference to %q+D", decl); 1463132718Skan error ("from this location"); 1464132718Skan return error_mark_node; 1465132718Skan } 1466132718Skan } 1467132718Skan 1468132718Skan /* If PROCESSING_TEMPLATE_DECL is nonzero here, then 1469132718Skan QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF 1470132718Skan for now. */ 1471132718Skan if (processing_template_decl) 1472169689Skan return build_qualified_name (TREE_TYPE (decl), 1473169689Skan qualifying_scope, 1474169689Skan DECL_NAME (decl), 1475169689Skan /*template_p=*/false); 1476132718Skan 1477169689Skan perform_or_defer_access_check (TYPE_BINFO (access_type), decl, 1478169689Skan decl); 1479132718Skan 1480132718Skan /* If the data member was named `C::M', convert `*this' to `C' 1481132718Skan first. */ 1482132718Skan if (qualifying_scope) 1483132718Skan { 1484132718Skan tree binfo = NULL_TREE; 1485132718Skan object = build_scoped_ref (object, qualifying_scope, 1486132718Skan &binfo); 1487132718Skan } 1488132718Skan 1489132718Skan return build_class_member_access_expr (object, decl, 1490132718Skan /*access_path=*/NULL_TREE, 1491132718Skan /*preserve_reference=*/false); 1492132718Skan } 1493132718Skan} 1494132718Skan 1495132718Skan/* DECL was the declaration to which a qualified-id resolved. Issue 1496132718Skan an error message if it is not accessible. If OBJECT_TYPE is 1497132718Skan non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the 1498132718Skan type of `*x', or `x', respectively. If the DECL was named as 1499132718Skan `A::B' then NESTED_NAME_SPECIFIER is `A'. */ 1500132718Skan 1501132718Skanvoid 1502169689Skancheck_accessibility_of_qualified_id (tree decl, 1503169689Skan tree object_type, 1504132718Skan tree nested_name_specifier) 1505132718Skan{ 1506132718Skan tree scope; 1507132718Skan tree qualifying_type = NULL_TREE; 1508169689Skan 1509169689Skan /* If we're not checking, return immediately. */ 1510169689Skan if (deferred_access_no_check) 1511169689Skan return; 1512169689Skan 1513132718Skan /* Determine the SCOPE of DECL. */ 1514132718Skan scope = context_for_name_lookup (decl); 1515132718Skan /* If the SCOPE is not a type, then DECL is not a member. */ 1516132718Skan if (!TYPE_P (scope)) 1517132718Skan return; 1518132718Skan /* Compute the scope through which DECL is being accessed. */ 1519169689Skan if (object_type 1520132718Skan /* OBJECT_TYPE might not be a class type; consider: 1521132718Skan 1522132718Skan class A { typedef int I; }; 1523132718Skan I *p; 1524132718Skan p->A::I::~I(); 1525132718Skan 1526169689Skan In this case, we will have "A::I" as the DECL, but "I" as the 1527132718Skan OBJECT_TYPE. */ 1528132718Skan && CLASS_TYPE_P (object_type) 1529132718Skan && DERIVED_FROM_P (scope, object_type)) 1530132718Skan /* If we are processing a `->' or `.' expression, use the type of the 1531132718Skan left-hand side. */ 1532132718Skan qualifying_type = object_type; 1533132718Skan else if (nested_name_specifier) 1534132718Skan { 1535132718Skan /* If the reference is to a non-static member of the 1536132718Skan current class, treat it as if it were referenced through 1537132718Skan `this'. */ 1538132718Skan if (DECL_NONSTATIC_MEMBER_P (decl) 1539132718Skan && current_class_ptr 1540132718Skan && DERIVED_FROM_P (scope, current_class_type)) 1541132718Skan qualifying_type = current_class_type; 1542132718Skan /* Otherwise, use the type indicated by the 1543132718Skan nested-name-specifier. */ 1544132718Skan else 1545132718Skan qualifying_type = nested_name_specifier; 1546132718Skan } 1547132718Skan else 1548132718Skan /* Otherwise, the name must be from the current class or one of 1549132718Skan its bases. */ 1550132718Skan qualifying_type = currently_open_derived_class (scope); 1551132718Skan 1552169689Skan if (qualifying_type 1553169689Skan /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM 1554169689Skan or similar in a default argument value. */ 1555169689Skan && CLASS_TYPE_P (qualifying_type) 1556169689Skan && !dependent_type_p (qualifying_type)) 1557169689Skan perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl, 1558169689Skan decl); 1559132718Skan} 1560132718Skan 1561132718Skan/* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the 1562132718Skan class named to the left of the "::" operator. DONE is true if this 1563132718Skan expression is a complete postfix-expression; it is false if this 1564132718Skan expression is followed by '->', '[', '(', etc. ADDRESS_P is true 1565169689Skan iff this expression is the operand of '&'. TEMPLATE_P is true iff 1566169689Skan the qualified-id was of the form "A::template B". TEMPLATE_ARG_P 1567169689Skan is true iff this qualified name appears as a template argument. */ 1568132718Skan 1569132718Skantree 1570169689Skanfinish_qualified_id_expr (tree qualifying_class, 1571169689Skan tree expr, 1572169689Skan bool done, 1573169689Skan bool address_p, 1574169689Skan bool template_p, 1575169689Skan bool template_arg_p) 1576132718Skan{ 1577169689Skan gcc_assert (TYPE_P (qualifying_class)); 1578169689Skan 1579132718Skan if (error_operand_p (expr)) 1580132718Skan return error_mark_node; 1581132718Skan 1582169689Skan if (DECL_P (expr) || BASELINK_P (expr)) 1583169689Skan mark_used (expr); 1584169689Skan 1585169689Skan if (template_p) 1586169689Skan check_template_keyword (expr); 1587169689Skan 1588132718Skan /* If EXPR occurs as the operand of '&', use special handling that 1589132718Skan permits a pointer-to-member. */ 1590132718Skan if (address_p && done) 1591132718Skan { 1592132718Skan if (TREE_CODE (expr) == SCOPE_REF) 1593132718Skan expr = TREE_OPERAND (expr, 1); 1594169689Skan expr = build_offset_ref (qualifying_class, expr, 1595132718Skan /*address_p=*/true); 1596132718Skan return expr; 1597132718Skan } 1598132718Skan 1599169689Skan /* Within the scope of a class, turn references to non-static 1600169689Skan members into expression of the form "this->...". */ 1601169689Skan if (template_arg_p) 1602169689Skan /* But, within a template argument, we do not want make the 1603169689Skan transformation, as there is no "this" pointer. */ 1604169689Skan ; 1605169689Skan else if (TREE_CODE (expr) == FIELD_DECL) 1606132718Skan expr = finish_non_static_data_member (expr, current_class_ref, 1607132718Skan qualifying_class); 1608132718Skan else if (BASELINK_P (expr) && !processing_template_decl) 1609132718Skan { 1610132718Skan tree fns; 1611132718Skan 1612132718Skan /* See if any of the functions are non-static members. */ 1613132718Skan fns = BASELINK_FUNCTIONS (expr); 1614132718Skan if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 1615132718Skan fns = TREE_OPERAND (fns, 0); 1616132718Skan /* If so, the expression may be relative to the current 1617132718Skan class. */ 1618146895Skan if (!shared_member_p (fns) 1619169689Skan && current_class_type 1620132718Skan && DERIVED_FROM_P (qualifying_class, current_class_type)) 1621169689Skan expr = (build_class_member_access_expr 1622132718Skan (maybe_dummy_object (qualifying_class, NULL), 1623132718Skan expr, 1624132718Skan BASELINK_ACCESS_BINFO (expr), 1625132718Skan /*preserve_reference=*/false)); 1626132718Skan else if (done) 1627132718Skan /* The expression is a qualified name whose address is not 1628132718Skan being taken. */ 1629132718Skan expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false); 1630132718Skan } 1631132718Skan 1632132718Skan return expr; 1633132718Skan} 1634132718Skan 163550397Sobrien/* Begin a statement-expression. The value returned must be passed to 163650397Sobrien finish_stmt_expr. */ 163750397Sobrien 1638169689Skantree 1639132718Skanbegin_stmt_expr (void) 164050397Sobrien{ 1641169689Skan return push_stmt_list (); 164250397Sobrien} 164350397Sobrien 1644132718Skan/* Process the final expression of a statement expression. EXPR can be 1645169689Skan NULL, if the final expression is empty. Return a STATEMENT_LIST 1646169689Skan containing all the statements in the statement-expression, or 1647169689Skan ERROR_MARK_NODE if there was an error. */ 164890075Sobrien 164990075Sobrientree 1650169689Skanfinish_stmt_expr_expr (tree expr, tree stmt_expr) 165190075Sobrien{ 1652146895Skan if (error_operand_p (expr)) 1653146895Skan return error_mark_node; 1654169689Skan 1655169689Skan /* If the last statement does not have "void" type, then the value 1656169689Skan of the last statement is the value of the entire expression. */ 1657132718Skan if (expr) 1658132718Skan { 1659169689Skan tree type = TREE_TYPE (expr); 1660169689Skan 1661169689Skan if (processing_template_decl) 1662132718Skan { 1663169689Skan expr = build_stmt (EXPR_STMT, expr); 1664169689Skan expr = add_stmt (expr); 1665169689Skan /* Mark the last statement so that we can recognize it as such at 1666169689Skan template-instantiation time. */ 1667169689Skan EXPR_STMT_STMT_EXPR_RESULT (expr) = 1; 1668169689Skan } 1669169689Skan else if (VOID_TYPE_P (type)) 1670169689Skan { 1671169689Skan /* Just treat this like an ordinary statement. */ 1672169689Skan expr = finish_expr_stmt (expr); 1673169689Skan } 1674169689Skan else 1675169689Skan { 1676169689Skan /* It actually has a value we need to deal with. First, force it 1677169689Skan to be an rvalue so that we won't need to build up a copy 1678169689Skan constructor call later when we try to assign it to something. */ 1679169689Skan expr = force_rvalue (expr); 1680169689Skan if (error_operand_p (expr)) 1681169689Skan return error_mark_node; 168290075Sobrien 1683169689Skan /* Update for array-to-pointer decay. */ 1684169689Skan type = TREE_TYPE (expr); 168590075Sobrien 1686169689Skan /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a 1687169689Skan normal statement, but don't convert to void or actually add 1688169689Skan the EXPR_STMT. */ 1689169689Skan if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) 1690169689Skan expr = maybe_cleanup_point_expr (expr); 1691169689Skan add_stmt (expr); 1692132718Skan } 1693132718Skan 1694169689Skan /* The type of the statement-expression is the type of the last 1695169689Skan expression. */ 1696169689Skan TREE_TYPE (stmt_expr) = type; 1697132718Skan } 169890075Sobrien 1699169689Skan return stmt_expr; 170090075Sobrien} 170190075Sobrien 1702132718Skan/* Finish a statement-expression. EXPR should be the value returned 1703132718Skan by the previous begin_stmt_expr. Returns an expression 1704132718Skan representing the statement-expression. */ 170550397Sobrien 1706169689Skantree 1707169689Skanfinish_stmt_expr (tree stmt_expr, bool has_no_scope) 170850397Sobrien{ 1709169689Skan tree type; 171050397Sobrien tree result; 171150397Sobrien 1712169689Skan if (error_operand_p (stmt_expr)) 1713169689Skan return error_mark_node; 171450397Sobrien 1715169689Skan gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST); 1716169689Skan 1717169689Skan type = TREE_TYPE (stmt_expr); 1718169689Skan result = pop_stmt_list (stmt_expr); 1719169689Skan TREE_TYPE (result) = type; 1720169689Skan 1721132718Skan if (processing_template_decl) 1722132718Skan { 1723169689Skan result = build_min (STMT_EXPR, type, result); 1724169689Skan TREE_SIDE_EFFECTS (result) = 1; 1725169689Skan STMT_EXPR_NO_SCOPE (result) = has_no_scope; 1726132718Skan } 1727169689Skan else if (CLASS_TYPE_P (type)) 1728169689Skan { 1729169689Skan /* Wrap the statement-expression in a TARGET_EXPR so that the 1730169689Skan temporary object created by the final expression is destroyed at 1731169689Skan the end of the full-expression containing the 1732169689Skan statement-expression. */ 1733169689Skan result = force_target_expr (type, result); 1734169689Skan } 1735169689Skan 173650397Sobrien return result; 173750397Sobrien} 173850397Sobrien 1739132718Skan/* Perform Koenig lookup. FN is the postfix-expression representing 1740132718Skan the function (or functions) to call; ARGS are the arguments to the 1741132718Skan call. Returns the functions to be considered by overload 1742132718Skan resolution. */ 1743132718Skan 1744132718Skantree 1745132718Skanperform_koenig_lookup (tree fn, tree args) 1746132718Skan{ 1747132718Skan tree identifier = NULL_TREE; 1748132718Skan tree functions = NULL_TREE; 1749132718Skan 1750132718Skan /* Find the name of the overloaded function. */ 1751132718Skan if (TREE_CODE (fn) == IDENTIFIER_NODE) 1752132718Skan identifier = fn; 1753132718Skan else if (is_overloaded_fn (fn)) 1754132718Skan { 1755132718Skan functions = fn; 1756132718Skan identifier = DECL_NAME (get_first_fn (functions)); 1757132718Skan } 1758132718Skan else if (DECL_P (fn)) 1759132718Skan { 1760132718Skan functions = fn; 1761132718Skan identifier = DECL_NAME (fn); 1762132718Skan } 1763132718Skan 1764132718Skan /* A call to a namespace-scope function using an unqualified name. 1765132718Skan 1766132718Skan Do Koenig lookup -- unless any of the arguments are 1767132718Skan type-dependent. */ 1768132718Skan if (!any_type_dependent_arguments_p (args)) 1769132718Skan { 1770132718Skan fn = lookup_arg_dependent (identifier, functions, args); 1771132718Skan if (!fn) 1772132718Skan /* The unqualified name could not be resolved. */ 1773132718Skan fn = unqualified_fn_lookup_error (identifier); 1774132718Skan } 1775132718Skan 1776132718Skan return fn; 1777132718Skan} 1778132718Skan 1779117395Skan/* Generate an expression for `FN (ARGS)'. 178050397Sobrien 1781117395Skan If DISALLOW_VIRTUAL is true, the call to FN will be not generated 1782117395Skan as a virtual call, even if FN is virtual. (This flag is set when 1783117395Skan encountering an expression where the function name is explicitly 1784117395Skan qualified. For example a call to `X::f' never generates a virtual 1785117395Skan call.) 1786117395Skan 1787117395Skan Returns code for the call. */ 1788117395Skan 1789169689Skantree 1790132718Skanfinish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p) 179150397Sobrien{ 1792132718Skan tree result; 1793132718Skan tree orig_fn; 1794132718Skan tree orig_args; 1795132718Skan 1796117395Skan if (fn == error_mark_node || args == error_mark_node) 1797117395Skan return error_mark_node; 179850397Sobrien 1799117395Skan /* ARGS should be a list of arguments. */ 1800169689Skan gcc_assert (!args || TREE_CODE (args) == TREE_LIST); 1801169689Skan gcc_assert (!TYPE_P (fn)); 1802117395Skan 1803132718Skan orig_fn = fn; 1804132718Skan orig_args = args; 1805132718Skan 1806132718Skan if (processing_template_decl) 1807132718Skan { 1808132718Skan if (type_dependent_expression_p (fn) 1809132718Skan || any_type_dependent_arguments_p (args)) 1810132718Skan { 1811169689Skan result = build_nt (CALL_EXPR, fn, args, NULL_TREE); 1812132718Skan KOENIG_LOOKUP_P (result) = koenig_p; 1813132718Skan return result; 1814132718Skan } 1815132718Skan if (!BASELINK_P (fn) 1816132718Skan && TREE_CODE (fn) != PSEUDO_DTOR_EXPR 1817132718Skan && TREE_TYPE (fn) != unknown_type_node) 1818132718Skan fn = build_non_dependent_expr (fn); 1819132718Skan args = build_non_dependent_args (orig_args); 1820132718Skan } 1821132718Skan 1822169689Skan if (is_overloaded_fn (fn)) 1823169689Skan fn = baselink_for_fns (fn); 1824132718Skan 1825132718Skan result = NULL_TREE; 1826117395Skan if (BASELINK_P (fn)) 182750397Sobrien { 1828117395Skan tree object; 1829117395Skan 1830117395Skan /* A call to a member function. From [over.call.func]: 1831117395Skan 1832117395Skan If the keyword this is in scope and refers to the class of 1833117395Skan that member function, or a derived class thereof, then the 1834117395Skan function call is transformed into a qualified function call 1835117395Skan using (*this) as the postfix-expression to the left of the 1836117395Skan . operator.... [Otherwise] a contrived object of type T 1837169689Skan becomes the implied object argument. 1838117395Skan 1839169689Skan This paragraph is unclear about this situation: 1840117395Skan 1841117395Skan struct A { void f(); }; 1842117395Skan struct B : public A {}; 1843117395Skan struct C : public A { void g() { B::f(); }}; 1844117395Skan 1845117395Skan In particular, for `B::f', this paragraph does not make clear 1846169689Skan whether "the class of that member function" refers to `A' or 1847117395Skan to `B'. We believe it refers to `B'. */ 1848169689Skan if (current_class_type 1849117395Skan && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)), 1850117395Skan current_class_type) 1851117395Skan && current_class_ref) 1852117395Skan object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)), 1853117395Skan NULL); 1854117395Skan else 1855117395Skan { 1856117395Skan tree representative_fn; 1857117395Skan 1858117395Skan representative_fn = BASELINK_FUNCTIONS (fn); 1859117395Skan if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR) 1860117395Skan representative_fn = TREE_OPERAND (representative_fn, 0); 1861117395Skan representative_fn = get_first_fn (representative_fn); 1862117395Skan object = build_dummy_object (DECL_CONTEXT (representative_fn)); 1863117395Skan } 1864117395Skan 1865132718Skan if (processing_template_decl) 1866132718Skan { 1867132718Skan if (type_dependent_expression_p (object)) 1868169689Skan return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE); 1869132718Skan object = build_non_dependent_expr (object); 1870132718Skan } 1871132718Skan 1872132718Skan result = build_new_method_call (object, fn, args, NULL_TREE, 1873169689Skan (disallow_virtual 1874169689Skan ? LOOKUP_NONVIRTUAL : 0), 1875169689Skan /*fn_p=*/NULL); 187650397Sobrien } 1877117395Skan else if (is_overloaded_fn (fn)) 1878169689Skan { 1879169689Skan /* If the function is an overloaded builtin, resolve it. */ 1880169689Skan if (TREE_CODE (fn) == FUNCTION_DECL 1881169689Skan && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL 1882169689Skan || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD)) 1883169689Skan result = resolve_overloaded_builtin (fn, args); 1884169689Skan 1885169689Skan if (!result) 1886169689Skan /* A call to a namespace-scope function. */ 1887169689Skan result = build_new_function_call (fn, args, koenig_p); 1888169689Skan } 1889132718Skan else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR) 1890132718Skan { 1891132718Skan if (args) 1892132718Skan error ("arguments to destructor are not allowed"); 1893132718Skan /* Mark the pseudo-destructor call as having side-effects so 1894132718Skan that we do not issue warnings about its use. */ 1895132718Skan result = build1 (NOP_EXPR, 1896132718Skan void_type_node, 1897132718Skan TREE_OPERAND (fn, 0)); 1898132718Skan TREE_SIDE_EFFECTS (result) = 1; 1899132718Skan } 1900117395Skan else if (CLASS_TYPE_P (TREE_TYPE (fn))) 1901132718Skan /* If the "function" is really an object of class type, it might 1902132718Skan have an overloaded `operator ()'. */ 1903132718Skan result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE, 1904132718Skan /*overloaded_p=*/NULL); 1905169689Skan 1906132718Skan if (!result) 1907132718Skan /* A call where the function is unknown. */ 1908132718Skan result = build_function_call (fn, args); 1909132718Skan 1910132718Skan if (processing_template_decl) 1911117395Skan { 1912169689Skan result = build3 (CALL_EXPR, TREE_TYPE (result), orig_fn, 1913169689Skan orig_args, NULL_TREE); 1914132718Skan KOENIG_LOOKUP_P (result) = koenig_p; 1915117395Skan } 1916132718Skan return result; 191750397Sobrien} 191850397Sobrien 191950397Sobrien/* Finish a call to a postfix increment or decrement or EXPR. (Which 192050397Sobrien is indicated by CODE, which should be POSTINCREMENT_EXPR or 192150397Sobrien POSTDECREMENT_EXPR.) */ 192250397Sobrien 1923169689Skantree 1924132718Skanfinish_increment_expr (tree expr, enum tree_code code) 192550397Sobrien{ 1926169689Skan return build_x_unary_op (code, expr); 192750397Sobrien} 192850397Sobrien 192950397Sobrien/* Finish a use of `this'. Returns an expression for `this'. */ 193050397Sobrien 1931169689Skantree 1932132718Skanfinish_this_expr (void) 193350397Sobrien{ 193450397Sobrien tree result; 193550397Sobrien 193650397Sobrien if (current_class_ptr) 193750397Sobrien { 193850397Sobrien result = current_class_ptr; 193950397Sobrien } 194050397Sobrien else if (current_function_decl 194150397Sobrien && DECL_STATIC_FUNCTION_P (current_function_decl)) 194250397Sobrien { 1943169689Skan error ("%<this%> is unavailable for static member functions"); 194450397Sobrien result = error_mark_node; 194550397Sobrien } 1946261188Spfg /* APPLE LOCAL begin radar 6275956 */ 1947261188Spfg else if (cur_block && current_function_decl 1948261188Spfg && BLOCK_SYNTHESIZED_FUNC (current_function_decl)) 1949261188Spfg { 1950261188Spfg result = lookup_name (this_identifier); 1951261188Spfg if (!result) 1952261188Spfg { 1953261188Spfg error ("invalid use of %<this%> in a block"); 1954261188Spfg result = error_mark_node; 1955261188Spfg } 1956261188Spfg } 1957261188Spfg /* APPLE LOCAL end radar 6275956 */ 195850397Sobrien else 195950397Sobrien { 196050397Sobrien if (current_function_decl) 1961169689Skan error ("invalid use of %<this%> in non-member function"); 196250397Sobrien else 1963169689Skan error ("invalid use of %<this%> at top level"); 196450397Sobrien result = error_mark_node; 196550397Sobrien } 196650397Sobrien 196750397Sobrien return result; 196850397Sobrien} 196950397Sobrien 1970132718Skan/* Finish a pseudo-destructor expression. If SCOPE is NULL, the 1971132718Skan expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is 1972132718Skan the TYPE for the type given. If SCOPE is non-NULL, the expression 1973132718Skan was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */ 197450397Sobrien 1975169689Skantree 1976132718Skanfinish_pseudo_destructor_expr (tree object, tree scope, tree destructor) 197750397Sobrien{ 1978132718Skan if (destructor == error_mark_node) 1979132718Skan return error_mark_node; 198050397Sobrien 1981169689Skan gcc_assert (TYPE_P (destructor)); 198250397Sobrien 1983132718Skan if (!processing_template_decl) 1984132718Skan { 1985132718Skan if (scope == error_mark_node) 198650397Sobrien { 1987132718Skan error ("invalid qualifying scope in pseudo-destructor name"); 198850397Sobrien return error_mark_node; 198950397Sobrien } 1990169689Skan if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor)) 1991169689Skan { 1992169689Skan error ("qualified type %qT does not match destructor name ~%qT", 1993169689Skan scope, destructor); 1994169689Skan return error_mark_node; 1995169689Skan } 1996169689Skan 1997169689Skan 1998132718Skan /* [expr.pseudo] says both: 199950397Sobrien 2000169689Skan The type designated by the pseudo-destructor-name shall be 2001132718Skan the same as the object type. 200250397Sobrien 2003169689Skan and: 200450397Sobrien 2005169689Skan The cv-unqualified versions of the object type and of the 2006132718Skan type designated by the pseudo-destructor-name shall be the 2007132718Skan same type. 200850397Sobrien 2009169689Skan We implement the more generous second sentence, since that is 2010169689Skan what most other compilers do. */ 2011169689Skan if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object), 2012132718Skan destructor)) 2013132718Skan { 2014169689Skan error ("%qE is not of type %qT", object, destructor); 2015132718Skan return error_mark_node; 2016132718Skan } 2017132718Skan } 201890075Sobrien 2019169689Skan return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor); 202050397Sobrien} 202150397Sobrien 202250397Sobrien/* Finish an expression of the form CODE EXPR. */ 202350397Sobrien 202450397Sobrientree 2025132718Skanfinish_unary_op_expr (enum tree_code code, tree expr) 202650397Sobrien{ 202750397Sobrien tree result = build_x_unary_op (code, expr); 202890075Sobrien /* Inside a template, build_x_unary_op does not fold the 202990075Sobrien expression. So check whether the result is folded before 203090075Sobrien setting TREE_NEGATED_INT. */ 203190075Sobrien if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST 203290075Sobrien && TREE_CODE (result) == INTEGER_CST 2033169689Skan && !TYPE_UNSIGNED (TREE_TYPE (result)) 203490075Sobrien && INT_CST_LT (result, integer_zero_node)) 2035169689Skan { 2036169689Skan /* RESULT may be a cached INTEGER_CST, so we must copy it before 2037169689Skan setting TREE_NEGATED_INT. */ 2038169689Skan result = copy_node (result); 2039169689Skan TREE_NEGATED_INT (result) = 1; 2040169689Skan } 2041259584Spfg if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr)) 2042259584Spfg overflow_warning (result); 2043259584Spfg 204450397Sobrien return result; 204550397Sobrien} 204650397Sobrien 2047132718Skan/* Finish a compound-literal expression. TYPE is the type to which 2048132718Skan the INITIALIZER_LIST is being cast. */ 204950397Sobrien 205050397Sobrientree 2051169689Skanfinish_compound_literal (tree type, VEC(constructor_elt,gc) *initializer_list) 205250397Sobrien{ 2053169689Skan tree var; 2054132718Skan tree compound_literal; 205550397Sobrien 2056169689Skan if (!TYPE_OBJ_P (type)) 2057169689Skan { 2058169689Skan error ("compound literal of non-object type %qT", type); 2059169689Skan return error_mark_node; 2060169689Skan } 2061169689Skan 2062132718Skan /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */ 2063132718Skan compound_literal = build_constructor (NULL_TREE, initializer_list); 2064132718Skan if (processing_template_decl) 2065132718Skan { 2066169689Skan TREE_TYPE (compound_literal) = type; 2067169689Skan /* Mark the expression as a compound literal. */ 2068169689Skan TREE_HAS_CONSTRUCTOR (compound_literal) = 1; 2069169689Skan return compound_literal; 2070169689Skan } 2071132718Skan 2072169689Skan /* Create a temporary variable to represent the compound literal. */ 2073169689Skan var = create_temporary_var (type); 2074169689Skan if (!current_function_decl) 2075169689Skan { 2076169689Skan /* If this compound-literal appears outside of a function, then 2077169689Skan the corresponding variable has static storage duration, just 2078169689Skan like the variable in whose initializer it appears. */ 2079169689Skan TREE_STATIC (var) = 1; 2080169689Skan /* The variable has internal linkage, since there is no need to 2081169689Skan reference it from another translation unit. */ 2082169689Skan TREE_PUBLIC (var) = 0; 2083169689Skan /* It must have a name, so that the name mangler can mangle it. */ 2084169689Skan DECL_NAME (var) = make_anon_name (); 2085132718Skan } 2086169689Skan /* We must call pushdecl, since the gimplifier complains if the 2087169689Skan variable has not been declared via a BIND_EXPR. */ 2088169689Skan pushdecl (var); 2089169689Skan /* Initialize the variable as we would any other variable with a 2090169689Skan brace-enclosed initializer. */ 2091169689Skan cp_finish_decl (var, compound_literal, 2092169689Skan /*init_const_expr_p=*/false, 2093169689Skan /*asmspec_tree=*/NULL_TREE, 2094169689Skan LOOKUP_ONLYCONVERTING); 2095169689Skan return var; 209650397Sobrien} 209750397Sobrien 2098117395Skan/* Return the declaration for the function-name variable indicated by 2099117395Skan ID. */ 2100117395Skan 2101117395Skantree 2102117395Skanfinish_fname (tree id) 2103117395Skan{ 2104117395Skan tree decl; 2105169689Skan 2106117395Skan decl = fname_decl (C_RID_CODE (id), id); 2107117395Skan if (processing_template_decl) 2108132718Skan decl = DECL_NAME (decl); 2109117395Skan return decl; 2110117395Skan} 2111117395Skan 211250397Sobrien/* Finish a translation unit. */ 211350397Sobrien 2114169689Skanvoid 2115132718Skanfinish_translation_unit (void) 211650397Sobrien{ 211750397Sobrien /* In case there were missing closebraces, 211850397Sobrien get us back to the global binding level. */ 211990075Sobrien pop_everything (); 212050397Sobrien while (current_namespace != global_namespace) 212150397Sobrien pop_namespace (); 212290075Sobrien 2123117395Skan /* Do file scope __FUNCTION__ et al. */ 212490075Sobrien finish_fname_decls (); 212550397Sobrien} 212650397Sobrien 212750397Sobrien/* Finish a template type parameter, specified as AGGR IDENTIFIER. 212850397Sobrien Returns the parameter. */ 212950397Sobrien 2130169689Skantree 2131132718Skanfinish_template_type_parm (tree aggr, tree identifier) 213250397Sobrien{ 213390075Sobrien if (aggr != class_type_node) 213450397Sobrien { 2135169689Skan pedwarn ("template type parameters must use the keyword %<class%> or %<typename%>"); 213650397Sobrien aggr = class_type_node; 213750397Sobrien } 213850397Sobrien 213950397Sobrien return build_tree_list (aggr, identifier); 214050397Sobrien} 214150397Sobrien 214250397Sobrien/* Finish a template template parameter, specified as AGGR IDENTIFIER. 214350397Sobrien Returns the parameter. */ 214450397Sobrien 2145169689Skantree 2146132718Skanfinish_template_template_parm (tree aggr, tree identifier) 214750397Sobrien{ 214850397Sobrien tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE); 214950397Sobrien tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE); 215050397Sobrien DECL_TEMPLATE_PARMS (tmpl) = current_template_parms; 215150397Sobrien DECL_TEMPLATE_RESULT (tmpl) = decl; 215290075Sobrien DECL_ARTIFICIAL (decl) = 1; 215350397Sobrien end_template_decl (); 215450397Sobrien 2155169689Skan gcc_assert (DECL_TEMPLATE_PARMS (tmpl)); 215690075Sobrien 215750397Sobrien return finish_template_type_parm (aggr, tmpl); 215850397Sobrien} 215950397Sobrien 2160117395Skan/* ARGUMENT is the default-argument value for a template template 2161117395Skan parameter. If ARGUMENT is invalid, issue error messages and return 2162117395Skan the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */ 2163117395Skan 2164117395Skantree 2165117395Skancheck_template_template_default_arg (tree argument) 2166117395Skan{ 2167117395Skan if (TREE_CODE (argument) != TEMPLATE_DECL 2168117395Skan && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM 2169117395Skan && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) 2170117395Skan { 2171132718Skan if (TREE_CODE (argument) == TYPE_DECL) 2172169689Skan error ("invalid use of type %qT as a default value for a template " 2173169689Skan "template-parameter", TREE_TYPE (argument)); 2174132718Skan else 2175132718Skan error ("invalid default argument for a template template parameter"); 2176117395Skan return error_mark_node; 2177117395Skan } 2178117395Skan 2179117395Skan return argument; 2180117395Skan} 2181117395Skan 218250397Sobrien/* Begin a class definition, as indicated by T. */ 218350397Sobrien 218450397Sobrientree 2185169689Skanbegin_class_definition (tree t, tree attributes) 218650397Sobrien{ 218790075Sobrien if (t == error_mark_node) 218890075Sobrien return error_mark_node; 218990075Sobrien 219090075Sobrien if (processing_template_parmlist) 219150397Sobrien { 2192169689Skan error ("definition of %q#T inside template parameter list", t); 219390075Sobrien return error_mark_node; 219490075Sobrien } 219590075Sobrien /* A non-implicit typename comes from code like: 219690075Sobrien 219790075Sobrien template <typename T> struct A { 2198169689Skan template <typename U> struct A<T>::B ... 219990075Sobrien 220090075Sobrien This is erroneous. */ 220190075Sobrien else if (TREE_CODE (t) == TYPENAME_TYPE) 220290075Sobrien { 2203169689Skan error ("invalid definition of qualified type %qT", t); 220490075Sobrien t = error_mark_node; 220590075Sobrien } 220690075Sobrien 220790075Sobrien if (t == error_mark_node || ! IS_AGGR_TYPE (t)) 220890075Sobrien { 220990075Sobrien t = make_aggr_type (RECORD_TYPE); 2210169689Skan pushtag (make_anon_name (), t, /*tag_scope=*/ts_current); 221150397Sobrien } 221252284Sobrien 221390075Sobrien /* Update the location of the decl. */ 2214132718Skan DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location; 2215169689Skan 221652284Sobrien if (TYPE_BEING_DEFINED (t)) 221750397Sobrien { 221890075Sobrien t = make_aggr_type (TREE_CODE (t)); 2219169689Skan pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current); 222050397Sobrien } 222152284Sobrien maybe_process_partial_specialization (t); 2222132718Skan pushclass (t); 222350397Sobrien TYPE_BEING_DEFINED (t) = 1; 2224169689Skan 2225169689Skan cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 2226169689Skan 2227132718Skan if (flag_pack_struct) 2228132718Skan { 2229132718Skan tree v; 2230132718Skan TYPE_PACKED (t) = 1; 2231132718Skan /* Even though the type is being defined for the first time 2232132718Skan here, there might have been a forward declaration, so there 2233132718Skan might be cv-qualified variants of T. */ 2234132718Skan for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) 2235132718Skan TYPE_PACKED (v) = 1; 2236132718Skan } 223750397Sobrien /* Reset the interface data, at the earliest possible 223850397Sobrien moment, as it might have been set via a class foo; 223950397Sobrien before. */ 224090075Sobrien if (! TYPE_ANONYMOUS_P (t)) 224150397Sobrien { 2242169689Skan struct c_fileinfo *finfo = get_fileinfo (input_filename); 2243169689Skan CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only; 224490075Sobrien SET_CLASSTYPE_INTERFACE_UNKNOWN_X 2245169689Skan (t, finfo->interface_unknown); 224650397Sobrien } 224750397Sobrien reset_specialization(); 2248169689Skan 224952284Sobrien /* Make a declaration for this class in its own scope. */ 225052284Sobrien build_self_reference (); 225152284Sobrien 225252284Sobrien return t; 225350397Sobrien} 225450397Sobrien 225552284Sobrien/* Finish the member declaration given by DECL. */ 225650397Sobrien 225752284Sobrienvoid 2258132718Skanfinish_member_declaration (tree decl) 225952284Sobrien{ 226052284Sobrien if (decl == error_mark_node || decl == NULL_TREE) 226152284Sobrien return; 226252284Sobrien 226352284Sobrien if (decl == void_type_node) 226452284Sobrien /* The COMPONENT was a friend, not a member, and so there's 226552284Sobrien nothing for us to do. */ 226652284Sobrien return; 226752284Sobrien 226852284Sobrien /* We should see only one DECL at a time. */ 2269169689Skan gcc_assert (TREE_CHAIN (decl) == NULL_TREE); 227052284Sobrien 227152284Sobrien /* Set up access control for DECL. */ 2272169689Skan TREE_PRIVATE (decl) 227352284Sobrien = (current_access_specifier == access_private_node); 2274169689Skan TREE_PROTECTED (decl) 227552284Sobrien = (current_access_specifier == access_protected_node); 227652284Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 227752284Sobrien { 227890075Sobrien TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl); 227990075Sobrien TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl); 228052284Sobrien } 228152284Sobrien 228252284Sobrien /* Mark the DECL as a member of the current class. */ 228390075Sobrien DECL_CONTEXT (decl) = current_class_type; 228452284Sobrien 228590075Sobrien /* [dcl.link] 228690075Sobrien 228790075Sobrien A C language linkage is ignored for the names of class members 228890075Sobrien and the member function type of class member functions. */ 228990075Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c) 229090075Sobrien SET_DECL_LANGUAGE (decl, lang_cplusplus); 229190075Sobrien 229252284Sobrien /* Put functions on the TYPE_METHODS list and everything else on the 229352284Sobrien TYPE_FIELDS list. Note that these are built up in reverse order. 229452284Sobrien We reverse them (to obtain declaration order) in finish_struct. */ 2295169689Skan if (TREE_CODE (decl) == FUNCTION_DECL 229652284Sobrien || DECL_FUNCTION_TEMPLATE_P (decl)) 229752284Sobrien { 229852284Sobrien /* We also need to add this function to the 229952284Sobrien CLASSTYPE_METHOD_VEC. */ 2300169689Skan if (add_method (current_class_type, decl, NULL_TREE)) 2301169689Skan { 2302169689Skan TREE_CHAIN (decl) = TYPE_METHODS (current_class_type); 2303169689Skan TYPE_METHODS (current_class_type) = decl; 230452284Sobrien 2305169689Skan maybe_add_class_template_decl_list (current_class_type, decl, 2306169689Skan /*friend_p=*/0); 2307169689Skan } 230852284Sobrien } 2309117395Skan /* Enter the DECL into the scope of the class. */ 2310169689Skan else if ((TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) 2311132718Skan || pushdecl_class_level (decl)) 231252284Sobrien { 231352284Sobrien /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields 231452284Sobrien go at the beginning. The reason is that lookup_field_1 231552284Sobrien searches the list in order, and we want a field name to 231652284Sobrien override a type name so that the "struct stat hack" will 231752284Sobrien work. In particular: 231852284Sobrien 231952284Sobrien struct S { enum E { }; int E } s; 232052284Sobrien s.E = 3; 232152284Sobrien 2322117395Skan is valid. In addition, the FIELD_DECLs must be maintained in 232352284Sobrien declaration order so that class layout works as expected. 232452284Sobrien However, we don't need that order until class layout, so we 232552284Sobrien save a little time by putting FIELD_DECLs on in reverse order 232652284Sobrien here, and then reversing them in finish_struct_1. (We could 232752284Sobrien also keep a pointer to the correct insertion points in the 232852284Sobrien list.) */ 232952284Sobrien 233052284Sobrien if (TREE_CODE (decl) == TYPE_DECL) 2331169689Skan TYPE_FIELDS (current_class_type) 233252284Sobrien = chainon (TYPE_FIELDS (current_class_type), decl); 233352284Sobrien else 233452284Sobrien { 233552284Sobrien TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type); 233652284Sobrien TYPE_FIELDS (current_class_type) = decl; 233752284Sobrien } 233852284Sobrien 2339169689Skan maybe_add_class_template_decl_list (current_class_type, decl, 2340117395Skan /*friend_p=*/0); 234152284Sobrien } 2342169689Skan 2343169689Skan if (pch_file) 2344169689Skan note_decl_for_pch (decl); 234552284Sobrien} 234652284Sobrien 2347169689Skan/* DECL has been declared while we are building a PCH file. Perform 2348169689Skan actions that we might normally undertake lazily, but which can be 2349169689Skan performed now so that they do not have to be performed in 2350169689Skan translation units which include the PCH file. */ 235150397Sobrien 2352169689Skanvoid 2353169689Skannote_decl_for_pch (tree decl) 235450397Sobrien{ 2355169689Skan gcc_assert (pch_file); 235652284Sobrien 2357169689Skan /* There's a good chance that we'll have to mangle names at some 2358169689Skan point, even if only for emission in debugging information. */ 2359169689Skan if ((TREE_CODE (decl) == VAR_DECL 2360169689Skan || TREE_CODE (decl) == FUNCTION_DECL) 2361169689Skan && !processing_template_decl) 2362169689Skan mangle_decl (decl); 236350397Sobrien} 236452284Sobrien 236590075Sobrien/* Finish processing a complete template declaration. The PARMS are 236652284Sobrien the template parameters. */ 236752284Sobrien 236852284Sobrienvoid 2369132718Skanfinish_template_decl (tree parms) 237052284Sobrien{ 237152284Sobrien if (parms) 237252284Sobrien end_template_decl (); 237352284Sobrien else 237452284Sobrien end_specialization (); 237552284Sobrien} 237652284Sobrien 237790075Sobrien/* Finish processing a template-id (which names a type) of the form 237852284Sobrien NAME < ARGS >. Return the TYPE_DECL for the type named by the 2379117395Skan template-id. If ENTERING_SCOPE is nonzero we are about to enter 238052284Sobrien the scope of template-id indicated. */ 238152284Sobrien 238252284Sobrientree 2383132718Skanfinish_template_type (tree name, tree args, int entering_scope) 238452284Sobrien{ 238552284Sobrien tree decl; 238652284Sobrien 238752284Sobrien decl = lookup_template_class (name, args, 2388132718Skan NULL_TREE, NULL_TREE, entering_scope, 2389169689Skan tf_warning_or_error | tf_user); 239052284Sobrien if (decl != error_mark_node) 239152284Sobrien decl = TYPE_STUB_DECL (decl); 239252284Sobrien 239352284Sobrien return decl; 239452284Sobrien} 239552284Sobrien 239652284Sobrien/* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER. 239752284Sobrien Return a TREE_LIST containing the ACCESS_SPECIFIER and the 239852284Sobrien BASE_CLASS, or NULL_TREE if an error occurred. The 239990075Sobrien ACCESS_SPECIFIER is one of 2400169689Skan access_{default,public,protected_private}_node. For a virtual base 2401169689Skan we set TREE_TYPE. */ 240252284Sobrien 2403169689Skantree 2404132718Skanfinish_base_specifier (tree base, tree access, bool virtual_p) 240552284Sobrien{ 240652284Sobrien tree result; 240752284Sobrien 2408132718Skan if (base == error_mark_node) 2409117395Skan { 2410117395Skan error ("invalid base-class specification"); 2411117395Skan result = NULL_TREE; 2412117395Skan } 2413132718Skan else if (! is_aggr_type (base, 1)) 241490075Sobrien result = NULL_TREE; 241552284Sobrien else 241652284Sobrien { 2417132718Skan if (cp_type_quals (base) != 0) 2418169689Skan { 2419169689Skan error ("base class %qT has cv qualifiers", base); 2420169689Skan base = TYPE_MAIN_VARIANT (base); 2421169689Skan } 2422132718Skan result = build_tree_list (access, base); 2423169689Skan if (virtual_p) 2424169689Skan TREE_TYPE (result) = integer_type_node; 242552284Sobrien } 242652284Sobrien 242752284Sobrien return result; 242852284Sobrien} 242952284Sobrien 2430169689Skan/* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is 2431169689Skan what we found when we tried to do the lookup. */ 243252284Sobrien 243352284Sobrienvoid 2434169689Skanqualified_name_lookup_error (tree scope, tree name, tree decl) 243552284Sobrien{ 2436161651Skan if (scope == error_mark_node) 2437161651Skan ; /* We already complained. */ 2438161651Skan else if (TYPE_P (scope)) 2439132718Skan { 2440132718Skan if (!COMPLETE_TYPE_P (scope)) 2441169689Skan error ("incomplete type %qT used in nested name specifier", scope); 2442169689Skan else if (TREE_CODE (decl) == TREE_LIST) 2443169689Skan { 2444169689Skan error ("reference to %<%T::%D%> is ambiguous", scope, name); 2445169689Skan print_candidates (decl); 2446169689Skan } 2447132718Skan else 2448169689Skan error ("%qD is not a member of %qT", name, scope); 2449132718Skan } 2450132718Skan else if (scope != global_namespace) 2451169689Skan error ("%qD is not a member of %qD", name, scope); 2452132718Skan else 2453169689Skan error ("%<::%D%> has not been declared", name); 2454132718Skan} 2455169689Skan 2456169689Skan/* If FNS is a member function, a set of member functions, or a 2457169689Skan template-id referring to one or more member functions, return a 2458169689Skan BASELINK for FNS, incorporating the current access context. 2459169689Skan Otherwise, return FNS unchanged. */ 2460169689Skan 2461169689Skantree 2462169689Skanbaselink_for_fns (tree fns) 2463169689Skan{ 2464169689Skan tree fn; 2465169689Skan tree cl; 2466169689Skan 2467169689Skan if (BASELINK_P (fns) 2468169689Skan || error_operand_p (fns)) 2469169689Skan return fns; 2470169689Skan 2471169689Skan fn = fns; 2472169689Skan if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 2473169689Skan fn = TREE_OPERAND (fn, 0); 2474169689Skan fn = get_first_fn (fn); 2475169689Skan if (!DECL_FUNCTION_MEMBER_P (fn)) 2476169689Skan return fns; 2477169689Skan 2478169689Skan cl = currently_open_derived_class (DECL_CONTEXT (fn)); 2479169689Skan if (!cl) 2480169689Skan cl = DECL_CONTEXT (fn); 2481169689Skan cl = TYPE_BINFO (cl); 2482169689Skan return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE); 2483169689Skan} 2484169689Skan 2485261188Spfg/* APPLE LOCAL begin blocks 6040305 */ 2486261188Spfgstatic bool 2487261188Spfgblock_var_ok_for_context (tree context) 2488261188Spfg{ 2489261188Spfg /* FIXME - local classes inside blocks, templates, etc */ 2490261188Spfg struct block_sema_info *b = cur_block; 2491261188Spfg tree decl = current_function_decl; 2492261188Spfg 2493261188Spfg /* If in a block helper, only variables from the context of the helper 2494261188Spfg are ok. */ 2495261188Spfg while (b && b->helper_func_decl == decl) 2496261188Spfg { 2497261188Spfg if (context == DECL_CONTEXT (decl)) 2498261188Spfg return true; 2499261188Spfg decl = DECL_CONTEXT (decl); 2500261188Spfg b = b->prev_block_info; 2501261188Spfg } 2502261188Spfg 2503261188Spfg return false; 2504261188Spfg} 2505261188Spfg 2506261188Spfg/* APPLE LOCAL begin radar 6545782 */ 2507261188Spfg/** This routine does all the work on use of variables in a block. */ 2508261188Spfgstatic tree get_final_block_variable (tree name, tree var) { 2509261188Spfg tree decl = var; 2510261188Spfg 2511261188Spfg if (cur_block 2512261188Spfg && (TREE_CODE (decl) == VAR_DECL 2513261188Spfg || TREE_CODE (decl) == PARM_DECL) 2514261188Spfg && !lookup_name_in_block (name, &decl)) 2515261188Spfg { 2516261188Spfg bool gdecl; 2517261188Spfg /* We are referencing a variable inside a block whose 2518261188Spfg declaration is outside. */ 2519261188Spfg gcc_assert (decl && 2520261188Spfg (TREE_CODE (decl) == VAR_DECL 2521261188Spfg || TREE_CODE (decl) == PARM_DECL)); 2522261188Spfg gdecl = (TREE_CODE (decl) == VAR_DECL && 2523261188Spfg (DECL_EXTERNAL (decl) || TREE_STATIC (decl))); 2524261188Spfg /* Treat all 'global' variables as 'byref' by default. */ 2525261188Spfg if (gdecl 2526261188Spfg || (TREE_CODE (decl) == VAR_DECL && COPYABLE_BYREF_LOCAL_VAR (decl))) 2527261188Spfg { 2528261188Spfg /* byref globals are directly accessed. */ 2529261188Spfg /* APPLE LOCAL begin radar 7760213 */ 2530261188Spfg if (!gdecl) { 2531261188Spfg if (HasByrefArray(TREE_TYPE (decl))) 2532261188Spfg error ("cannot access __block variable of array type inside block"); 2533261188Spfg /* build a decl for the byref variable. */ 2534261188Spfg decl = build_block_byref_decl (name, decl, decl); 2535261188Spfg } 2536261188Spfg /* APPLE LOCAL end radar 7760213 */ 2537261188Spfg else 2538261188Spfg add_block_global_byref_list (decl); 2539261188Spfg } 2540261188Spfg else 2541261188Spfg { 2542261188Spfg /* 'byref' globals are never copied-in. So, do not add 2543261188Spfg them to the copied-in list. */ 2544261188Spfg if (!in_block_global_byref_list (decl)) { 2545261188Spfg /* APPLE LOCAL begin radar 7721728 */ 2546261188Spfg if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) 2547261188Spfg error ("cannot access copied-in variable of array type inside block"); 2548261188Spfg /* APPLE LOCAL end radar 7721728 */ 2549261188Spfg /* build a new decl node. set its type to 'const' type 2550261188Spfg of the old decl. */ 2551261188Spfg decl = build_block_ref_decl (name, decl); 2552261188Spfg } 2553261188Spfg } 2554261188Spfg } 2555261188Spfg return decl; 2556261188Spfg} 2557261188Spfg/* APPLE LOCAL end radar 6545782 */ 2558261188Spfg 2559261188Spfg/* APPLE LOCAL end blocks 6040305 */ 2560261188Spfg 2561132718Skan/* ID_EXPRESSION is a representation of parsed, but unprocessed, 2562132718Skan id-expression. (See cp_parser_id_expression for details.) SCOPE, 2563132718Skan if non-NULL, is the type or namespace used to explicitly qualify 2564132718Skan ID_EXPRESSION. DECL is the entity to which that name has been 2565169689Skan resolved. 2566132718Skan 2567132718Skan *CONSTANT_EXPRESSION_P is true if we are presently parsing a 2568132718Skan constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will 2569132718Skan be set to true if this expression isn't permitted in a 2570132718Skan constant-expression, but it is otherwise not set by this function. 2571132718Skan *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a 2572132718Skan constant-expression, but a non-constant expression is also 2573132718Skan permissible. 2574132718Skan 2575169689Skan DONE is true if this expression is a complete postfix-expression; 2576169689Skan it is false if this expression is followed by '->', '[', '(', etc. 2577169689Skan ADDRESS_P is true iff this expression is the operand of '&'. 2578169689Skan TEMPLATE_P is true iff the qualified-id was of the form 2579169689Skan "A::template B". TEMPLATE_ARG_P is true iff this qualified name 2580169689Skan appears as a template argument. 2581169689Skan 2582132718Skan If an error occurs, and it is the kind of error that might cause 2583132718Skan the parser to abort a tentative parse, *ERROR_MSG is filled in. It 2584132718Skan is the caller's responsibility to issue the message. *ERROR_MSG 2585132718Skan will be a string with static storage duration, so the caller need 2586132718Skan not "free" it. 2587132718Skan 2588132718Skan Return an expression for the entity, after issuing appropriate 2589132718Skan diagnostics. This function is also responsible for transforming a 2590132718Skan reference to a non-static member into a COMPONENT_REF that makes 2591169689Skan the use of "this" explicit. 2592132718Skan 2593132718Skan Upon return, *IDK will be filled in appropriately. */ 2594132718Skan 2595132718Skantree 2596169689Skanfinish_id_expression (tree id_expression, 2597132718Skan tree decl, 2598132718Skan tree scope, 2599132718Skan cp_id_kind *idk, 2600132718Skan bool integral_constant_expression_p, 2601132718Skan bool allow_non_integral_constant_expression_p, 2602132718Skan bool *non_integral_constant_expression_p, 2603169689Skan bool template_p, 2604169689Skan bool done, 2605169689Skan bool address_p, 2606169689Skan bool template_arg_p, 2607132718Skan const char **error_msg) 2608132718Skan{ 2609132718Skan /* Initialize the output parameters. */ 2610132718Skan *idk = CP_ID_KIND_NONE; 2611132718Skan *error_msg = NULL; 2612132718Skan 2613132718Skan if (id_expression == error_mark_node) 2614132718Skan return error_mark_node; 2615132718Skan /* If we have a template-id, then no further lookup is 2616132718Skan required. If the template-id was for a template-class, we 2617132718Skan will sometimes have a TYPE_DECL at this point. */ 2618132718Skan else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 2619132718Skan || TREE_CODE (decl) == TYPE_DECL) 2620132718Skan ; 2621132718Skan /* Look up the name. */ 2622169689Skan else 2623132718Skan { 2624132718Skan if (decl == error_mark_node) 2625132718Skan { 2626132718Skan /* Name lookup failed. */ 2627261188Spfg 2628169689Skan if (scope 2629169689Skan && (!TYPE_P (scope) 2630132718Skan || (!dependent_type_p (scope) 2631132718Skan && !(TREE_CODE (id_expression) == IDENTIFIER_NODE 2632132718Skan && IDENTIFIER_TYPENAME_P (id_expression) 2633132718Skan && dependent_type_p (TREE_TYPE (id_expression)))))) 2634132718Skan { 2635132718Skan /* If the qualifying type is non-dependent (and the name 2636132718Skan does not name a conversion operator to a dependent 2637132718Skan type), issue an error. */ 2638169689Skan qualified_name_lookup_error (scope, id_expression, decl); 2639132718Skan return error_mark_node; 2640132718Skan } 2641132718Skan else if (!scope) 2642132718Skan { 2643132718Skan /* It may be resolved via Koenig lookup. */ 2644132718Skan *idk = CP_ID_KIND_UNQUALIFIED; 2645132718Skan return id_expression; 2646132718Skan } 2647132718Skan else 2648132718Skan decl = id_expression; 2649132718Skan } 2650132718Skan /* If DECL is a variable that would be out of scope under 2651132718Skan ANSI/ISO rules, but in scope in the ARM, name lookup 2652132718Skan will succeed. Issue a diagnostic here. */ 2653132718Skan else 2654132718Skan decl = check_for_out_of_scope_variable (decl); 2655132718Skan 2656132718Skan /* Remember that the name was used in the definition of 2657132718Skan the current class so that we can check later to see if 2658132718Skan the meaning would have been different after the class 2659132718Skan was entirely defined. */ 2660132718Skan if (!scope && decl != error_mark_node) 2661132718Skan maybe_note_name_used_in_class (id_expression, decl); 2662161651Skan 2663161651Skan /* Disallow uses of local variables from containing functions. */ 2664161651Skan if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL) 2665161651Skan { 2666161651Skan tree context = decl_function_context (decl); 2667161651Skan if (context != NULL_TREE && context != current_function_decl 2668261188Spfg /* APPLE LOCAL begin blocks 6040305 */ 2669261188Spfg && ! TREE_STATIC (decl) 2670261188Spfg && !block_var_ok_for_context (context)) 2671261188Spfg /* APPLE LOCAL end blocks 6040305 */ 2672161651Skan { 2673161651Skan error (TREE_CODE (decl) == VAR_DECL 2674169689Skan ? "use of %<auto%> variable from containing function" 2675161651Skan : "use of parameter from containing function"); 2676169689Skan error (" %q+#D declared here", decl); 2677161651Skan return error_mark_node; 2678161651Skan } 2679161651Skan } 2680132718Skan } 2681132718Skan 2682132718Skan /* If we didn't find anything, or what we found was a type, 2683132718Skan then this wasn't really an id-expression. */ 2684132718Skan if (TREE_CODE (decl) == TEMPLATE_DECL 2685132718Skan && !DECL_FUNCTION_TEMPLATE_P (decl)) 2686132718Skan { 2687132718Skan *error_msg = "missing template arguments"; 2688132718Skan return error_mark_node; 2689132718Skan } 2690132718Skan else if (TREE_CODE (decl) == TYPE_DECL 2691132718Skan || TREE_CODE (decl) == NAMESPACE_DECL) 2692132718Skan { 2693132718Skan *error_msg = "expected primary-expression"; 2694132718Skan return error_mark_node; 2695132718Skan } 2696132718Skan 2697132718Skan /* If the name resolved to a template parameter, there is no 2698132718Skan need to look it up again later. */ 2699132718Skan if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl)) 2700132718Skan || TREE_CODE (decl) == TEMPLATE_PARM_INDEX) 2701132718Skan { 2702169689Skan tree r; 2703169689Skan 2704132718Skan *idk = CP_ID_KIND_NONE; 2705132718Skan if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX) 2706132718Skan decl = TEMPLATE_PARM_DECL (decl); 2707169689Skan r = convert_from_reference (DECL_INITIAL (decl)); 2708169689Skan 2709169689Skan if (integral_constant_expression_p 2710132718Skan && !dependent_type_p (TREE_TYPE (decl)) 2711169689Skan && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r)))) 2712132718Skan { 2713132718Skan if (!allow_non_integral_constant_expression_p) 2714169689Skan error ("template parameter %qD of type %qT is not allowed in " 2715132718Skan "an integral constant expression because it is not of " 2716132718Skan "integral or enumeration type", decl, TREE_TYPE (decl)); 2717132718Skan *non_integral_constant_expression_p = true; 2718132718Skan } 2719169689Skan return r; 2720132718Skan } 2721169689Skan /* Similarly, we resolve enumeration constants to their 2722132718Skan underlying values. */ 2723132718Skan else if (TREE_CODE (decl) == CONST_DECL) 2724132718Skan { 2725132718Skan *idk = CP_ID_KIND_NONE; 2726132718Skan if (!processing_template_decl) 2727169689Skan { 2728169689Skan used_types_insert (TREE_TYPE (decl)); 2729169689Skan return DECL_INITIAL (decl); 2730169689Skan } 2731132718Skan return decl; 2732132718Skan } 2733132718Skan else 2734132718Skan { 2735132718Skan bool dependent_p; 2736132718Skan 2737132718Skan /* If the declaration was explicitly qualified indicate 2738132718Skan that. The semantics of `A::f(3)' are different than 2739132718Skan `f(3)' if `f' is virtual. */ 2740169689Skan *idk = (scope 2741132718Skan ? CP_ID_KIND_QUALIFIED 2742132718Skan : (TREE_CODE (decl) == TEMPLATE_ID_EXPR 2743132718Skan ? CP_ID_KIND_TEMPLATE_ID 2744132718Skan : CP_ID_KIND_UNQUALIFIED)); 2745132718Skan 2746132718Skan 2747132718Skan /* [temp.dep.expr] 2748132718Skan 2749132718Skan An id-expression is type-dependent if it contains an 2750132718Skan identifier that was declared with a dependent type. 2751132718Skan 2752132718Skan The standard is not very specific about an id-expression that 2753132718Skan names a set of overloaded functions. What if some of them 2754132718Skan have dependent types and some of them do not? Presumably, 2755132718Skan such a name should be treated as a dependent name. */ 2756132718Skan /* Assume the name is not dependent. */ 2757132718Skan dependent_p = false; 2758132718Skan if (!processing_template_decl) 2759132718Skan /* No names are dependent outside a template. */ 2760132718Skan ; 2761132718Skan /* A template-id where the name of the template was not resolved 2762132718Skan is definitely dependent. */ 2763132718Skan else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 2764169689Skan && (TREE_CODE (TREE_OPERAND (decl, 0)) 2765132718Skan == IDENTIFIER_NODE)) 2766132718Skan dependent_p = true; 2767132718Skan /* For anything except an overloaded function, just check its 2768132718Skan type. */ 2769132718Skan else if (!is_overloaded_fn (decl)) 2770169689Skan dependent_p 2771132718Skan = dependent_type_p (TREE_TYPE (decl)); 2772132718Skan /* For a set of overloaded functions, check each of the 2773132718Skan functions. */ 2774132718Skan else 2775132718Skan { 2776132718Skan tree fns = decl; 2777132718Skan 2778132718Skan if (BASELINK_P (fns)) 2779132718Skan fns = BASELINK_FUNCTIONS (fns); 2780132718Skan 2781132718Skan /* For a template-id, check to see if the template 2782132718Skan arguments are dependent. */ 2783132718Skan if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 2784132718Skan { 2785132718Skan tree args = TREE_OPERAND (fns, 1); 2786132718Skan dependent_p = any_dependent_template_arguments_p (args); 2787132718Skan /* The functions are those referred to by the 2788132718Skan template-id. */ 2789132718Skan fns = TREE_OPERAND (fns, 0); 2790132718Skan } 2791132718Skan 2792132718Skan /* If there are no dependent template arguments, go through 2793132718Skan the overloaded functions. */ 2794132718Skan while (fns && !dependent_p) 2795132718Skan { 2796132718Skan tree fn = OVL_CURRENT (fns); 2797132718Skan 2798132718Skan /* Member functions of dependent classes are 2799132718Skan dependent. */ 2800132718Skan if (TREE_CODE (fn) == FUNCTION_DECL 2801132718Skan && type_dependent_expression_p (fn)) 2802132718Skan dependent_p = true; 2803132718Skan else if (TREE_CODE (fn) == TEMPLATE_DECL 2804132718Skan && dependent_template_p (fn)) 2805132718Skan dependent_p = true; 2806132718Skan 2807132718Skan fns = OVL_NEXT (fns); 2808132718Skan } 2809132718Skan } 2810132718Skan 2811132718Skan /* If the name was dependent on a template parameter, we will 2812132718Skan resolve the name at instantiation time. */ 2813132718Skan if (dependent_p) 2814132718Skan { 2815132718Skan /* Create a SCOPE_REF for qualified names, if the scope is 2816132718Skan dependent. */ 2817132718Skan if (scope) 2818132718Skan { 2819132718Skan /* Since this name was dependent, the expression isn't 2820132718Skan constant -- yet. No error is issued because it might 2821132718Skan be constant when things are instantiated. */ 2822132718Skan if (integral_constant_expression_p) 2823132718Skan *non_integral_constant_expression_p = true; 2824169689Skan if (TYPE_P (scope)) 2825169689Skan { 2826169689Skan if (address_p && done) 2827169689Skan decl = finish_qualified_id_expr (scope, decl, 2828169689Skan done, address_p, 2829169689Skan template_p, 2830169689Skan template_arg_p); 2831169689Skan else if (dependent_type_p (scope)) 2832169689Skan decl = build_qualified_name (/*type=*/NULL_TREE, 2833169689Skan scope, 2834169689Skan id_expression, 2835169689Skan template_p); 2836169689Skan else if (DECL_P (decl)) 2837169689Skan decl = build_qualified_name (TREE_TYPE (decl), 2838169689Skan scope, 2839169689Skan id_expression, 2840169689Skan template_p); 2841169689Skan } 2842169689Skan if (TREE_TYPE (decl)) 2843169689Skan decl = convert_from_reference (decl); 2844169689Skan return decl; 2845132718Skan } 2846132718Skan /* A TEMPLATE_ID already contains all the information we 2847132718Skan need. */ 2848132718Skan if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR) 2849132718Skan return id_expression; 2850132718Skan *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT; 2851132718Skan /* If we found a variable, then name lookup during the 2852132718Skan instantiation will always resolve to the same VAR_DECL 2853132718Skan (or an instantiation thereof). */ 2854132718Skan if (TREE_CODE (decl) == VAR_DECL 2855132718Skan || TREE_CODE (decl) == PARM_DECL) 2856169689Skan return convert_from_reference (decl); 2857146895Skan /* The same is true for FIELD_DECL, but we also need to 2858146895Skan make sure that the syntax is correct. */ 2859146895Skan else if (TREE_CODE (decl) == FIELD_DECL) 2860146895Skan { 2861146895Skan /* Since SCOPE is NULL here, this is an unqualified name. 2862146895Skan Access checking has been performed during name lookup 2863146895Skan already. Turn off checking to avoid duplicate errors. */ 2864146895Skan push_deferring_access_checks (dk_no_check); 2865146895Skan decl = finish_non_static_data_member 2866146895Skan (decl, current_class_ref, 2867146895Skan /*qualifying_scope=*/NULL_TREE); 2868146895Skan pop_deferring_access_checks (); 2869146895Skan return decl; 2870146895Skan } 2871132718Skan return id_expression; 2872132718Skan } 2873132718Skan 2874132718Skan /* Only certain kinds of names are allowed in constant 2875169689Skan expression. Enumerators and template parameters have already 2876169689Skan been handled above. */ 2877146895Skan if (integral_constant_expression_p 2878169689Skan && ! DECL_INTEGRAL_CONSTANT_VAR_P (decl) 2879169689Skan && ! builtin_valid_in_constant_expr_p (decl)) 2880132718Skan { 2881146895Skan if (!allow_non_integral_constant_expression_p) 2882132718Skan { 2883169689Skan error ("%qD cannot appear in a constant-expression", decl); 2884146895Skan return error_mark_node; 2885132718Skan } 2886146895Skan *non_integral_constant_expression_p = true; 2887132718Skan } 2888169689Skan 2889132718Skan if (TREE_CODE (decl) == NAMESPACE_DECL) 2890132718Skan { 2891169689Skan error ("use of namespace %qD as expression", decl); 2892132718Skan return error_mark_node; 2893132718Skan } 2894132718Skan else if (DECL_CLASS_TEMPLATE_P (decl)) 2895132718Skan { 2896169689Skan error ("use of class template %qT as expression", decl); 2897132718Skan return error_mark_node; 2898132718Skan } 2899132718Skan else if (TREE_CODE (decl) == TREE_LIST) 2900132718Skan { 2901132718Skan /* Ambiguous reference to base members. */ 2902169689Skan error ("request for member %qD is ambiguous in " 2903132718Skan "multiple inheritance lattice", id_expression); 2904132718Skan print_candidates (decl); 2905132718Skan return error_mark_node; 2906132718Skan } 2907132718Skan 2908132718Skan /* Mark variable-like entities as used. Functions are similarly 2909132718Skan marked either below or after overload resolution. */ 2910132718Skan if (TREE_CODE (decl) == VAR_DECL 2911132718Skan || TREE_CODE (decl) == PARM_DECL 2912132718Skan || TREE_CODE (decl) == RESULT_DECL) 2913132718Skan mark_used (decl); 2914132718Skan 2915132718Skan if (scope) 2916132718Skan { 2917169689Skan decl = (adjust_result_of_qualified_name_lookup 2918132718Skan (decl, scope, current_class_type)); 2919132718Skan 2920132718Skan if (TREE_CODE (decl) == FUNCTION_DECL) 2921132718Skan mark_used (decl); 2922132718Skan 2923132718Skan if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl)) 2924169689Skan decl = finish_qualified_id_expr (scope, 2925169689Skan decl, 2926169689Skan done, 2927169689Skan address_p, 2928169689Skan template_p, 2929169689Skan template_arg_p); 2930169689Skan else 2931169689Skan { 2932169689Skan tree r = convert_from_reference (decl); 2933169689Skan 2934169689Skan if (processing_template_decl && TYPE_P (scope)) 2935169689Skan r = build_qualified_name (TREE_TYPE (r), 2936169689Skan scope, decl, 2937169689Skan template_p); 2938169689Skan decl = r; 2939169689Skan } 2940132718Skan } 2941132718Skan else if (TREE_CODE (decl) == FIELD_DECL) 2942146895Skan { 2943146895Skan /* Since SCOPE is NULL here, this is an unqualified name. 2944146895Skan Access checking has been performed during name lookup 2945146895Skan already. Turn off checking to avoid duplicate errors. */ 2946146895Skan push_deferring_access_checks (dk_no_check); 2947261188Spfg /* APPLE LOCAL begin radar 6169580 */ 2948261188Spfg if (cur_block) 2949261188Spfg { 2950261188Spfg tree exp; 2951261188Spfg tree this_copiedin_var = lookup_name (this_identifier); 2952261188Spfg gcc_assert (!current_class_ref); 2953261188Spfg gcc_assert (this_copiedin_var); 2954261188Spfg exp = build_x_arrow (this_copiedin_var); 2955261188Spfg decl = build_class_member_access_expr (exp, decl, TREE_TYPE (exp), 2956261188Spfg /*preserve_reference=*/false); 2957261188Spfg } 2958261188Spfg else 2959261188Spfg decl = finish_non_static_data_member (decl, current_class_ref, 2960261188Spfg /*qualifying_scope=*/NULL_TREE); 2961261188Spfg /* APPLE LOCAL end radar 6169580 */ 2962146895Skan pop_deferring_access_checks (); 2963146895Skan } 2964132718Skan else if (is_overloaded_fn (decl)) 2965132718Skan { 2966169689Skan tree first_fn; 2967132718Skan 2968169689Skan first_fn = decl; 2969169689Skan if (TREE_CODE (first_fn) == TEMPLATE_ID_EXPR) 2970169689Skan first_fn = TREE_OPERAND (first_fn, 0); 2971169689Skan first_fn = get_first_fn (first_fn); 2972132718Skan if (TREE_CODE (first_fn) == TEMPLATE_DECL) 2973132718Skan first_fn = DECL_TEMPLATE_RESULT (first_fn); 2974132718Skan 2975132718Skan if (!really_overloaded_fn (decl)) 2976132718Skan mark_used (first_fn); 2977132718Skan 2978169689Skan if (!template_arg_p 2979169689Skan && TREE_CODE (first_fn) == FUNCTION_DECL 2980146895Skan && DECL_FUNCTION_MEMBER_P (first_fn) 2981146895Skan && !shared_member_p (decl)) 2982132718Skan { 2983132718Skan /* A set of member functions. */ 2984132718Skan decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0); 2985169689Skan return finish_class_member_access_expr (decl, id_expression, 2986169689Skan /*template_p=*/false); 2987132718Skan } 2988169689Skan 2989169689Skan decl = baselink_for_fns (decl); 2990132718Skan } 2991132718Skan else 2992132718Skan { 2993132718Skan if (DECL_P (decl) && DECL_NONLOCAL (decl) 2994132718Skan && DECL_CLASS_SCOPE_P (decl) 2995132718Skan && DECL_CONTEXT (decl) != current_class_type) 2996132718Skan { 2997132718Skan tree path; 2998169689Skan 2999132718Skan path = currently_open_derived_class (DECL_CONTEXT (decl)); 3000169689Skan perform_or_defer_access_check (TYPE_BINFO (path), decl, decl); 3001132718Skan } 3002261188Spfg /* APPLE LOCAL radar 6545782 */ 3003261188Spfg decl = get_final_block_variable (id_expression, decl); 3004169689Skan decl = convert_from_reference (decl); 3005132718Skan } 3006132718Skan } 3007132718Skan 3008132718Skan if (TREE_DEPRECATED (decl)) 3009132718Skan warn_deprecated_use (decl); 3010132718Skan 3011261188Spfg /* APPLE LOCAL begin blocks 6040305 (cd) */ 3012261188Spfg if (TREE_CODE (decl) == VAR_DECL) 3013261188Spfg { 3014261188Spfg if (BLOCK_DECL_BYREF (decl)) 3015261188Spfg { 3016261188Spfg tree orig_decl = decl; 3017261188Spfg decl = build_indirect_ref (decl, "unary *"); 3018261188Spfg if (COPYABLE_BYREF_LOCAL_VAR (orig_decl)) 3019261188Spfg { 3020261188Spfg /* What we have is an expression which is of type 3021261188Spfg struct __Block_byref_X. Must get to the value of the variable 3022261188Spfg embedded in this structure. It is at: 3023261188Spfg __Block_byref_X.__forwarding->x */ 3024261188Spfg decl = build_byref_local_var_access (decl, 3025261188Spfg DECL_NAME (orig_decl)); 3026261188Spfg } 3027261188Spfg } 3028261188Spfg else 3029261188Spfg if (COPYABLE_BYREF_LOCAL_VAR (decl)) 3030261188Spfg decl = build_byref_local_var_access (decl, 3031261188Spfg DECL_NAME (decl)); 3032261188Spfg } 3033261188Spfg /* APPLE LOCAL end blocks 6040305 (cd) */ 3034261188Spfg 3035132718Skan return decl; 3036132718Skan} 3037132718Skan 303890075Sobrien/* Implement the __typeof keyword: Return the type of EXPR, suitable for 303990075Sobrien use as a type-specifier. */ 304090075Sobrien 304152284Sobrientree 3042132718Skanfinish_typeof (tree expr) 304352284Sobrien{ 3044110611Skan tree type; 3045110611Skan 3046132718Skan if (type_dependent_expression_p (expr)) 304752284Sobrien { 3048110611Skan type = make_aggr_type (TYPEOF_TYPE); 3049169689Skan TYPEOF_TYPE_EXPR (type) = expr; 305052284Sobrien 3051110611Skan return type; 305252284Sobrien } 305352284Sobrien 3054169689Skan type = unlowered_expr_type (expr); 3055110611Skan 3056110611Skan if (!type || type == unknown_type_node) 3057110611Skan { 3058169689Skan error ("type of %qE is unknown", expr); 3059110611Skan return error_mark_node; 3060110611Skan } 3061110611Skan 3062110611Skan return type; 306352284Sobrien} 306490075Sobrien 3065169689Skan/* Perform C++-specific checks for __builtin_offsetof before calling 3066169689Skan fold_offsetof. */ 306790075Sobrien 3068169689Skantree 3069169689Skanfinish_offsetof (tree expr) 307090075Sobrien{ 3071169689Skan if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) 307290075Sobrien { 3073169689Skan error ("cannot apply %<offsetof%> to destructor %<~%T%>", 3074169689Skan TREE_OPERAND (expr, 2)); 3075169689Skan return error_mark_node; 307690075Sobrien } 3077169689Skan if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE 3078169689Skan || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE 3079169689Skan || TREE_CODE (TREE_TYPE (expr)) == UNKNOWN_TYPE) 3080169689Skan { 3081169689Skan if (TREE_CODE (expr) == COMPONENT_REF 3082169689Skan || TREE_CODE (expr) == COMPOUND_EXPR) 3083169689Skan expr = TREE_OPERAND (expr, 1); 3084169689Skan error ("cannot apply %<offsetof%> to member function %qD", expr); 3085169689Skan return error_mark_node; 3086169689Skan } 3087169689Skan return fold_offsetof (expr, NULL_TREE); 308890075Sobrien} 308990075Sobrien 309090075Sobrien/* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs 3091169689Skan with equivalent CALL_EXPRs. */ 309290075Sobrien 309390075Sobrienstatic tree 3094169689Skansimplify_aggr_init_exprs_r (tree* tp, 3095169689Skan int* walk_subtrees, 3096169689Skan void* data ATTRIBUTE_UNUSED) 309790075Sobrien{ 309890075Sobrien /* We don't need to walk into types; there's nothing in a type that 309990075Sobrien needs simplification. (And, furthermore, there are places we 310090075Sobrien actively don't want to go. For example, we don't want to wander 310190075Sobrien into the default arguments for a FUNCTION_DECL that appears in a 310290075Sobrien CALL_EXPR.) */ 3103132718Skan if (TYPE_P (*tp)) 310490075Sobrien { 310590075Sobrien *walk_subtrees = 0; 310690075Sobrien return NULL_TREE; 310790075Sobrien } 310890075Sobrien /* Only AGGR_INIT_EXPRs are interesting. */ 3109132718Skan else if (TREE_CODE (*tp) != AGGR_INIT_EXPR) 311090075Sobrien return NULL_TREE; 311190075Sobrien 3112132718Skan simplify_aggr_init_expr (tp); 3113132718Skan 3114132718Skan /* Keep iterating. */ 3115132718Skan return NULL_TREE; 3116132718Skan} 3117132718Skan 3118132718Skan/* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This 3119132718Skan function is broken out from the above for the benefit of the tree-ssa 3120132718Skan project. */ 3121132718Skan 3122132718Skanvoid 3123132718Skansimplify_aggr_init_expr (tree *tp) 3124132718Skan{ 3125132718Skan tree aggr_init_expr = *tp; 3126132718Skan 312790075Sobrien /* Form an appropriate CALL_EXPR. */ 3128132718Skan tree fn = TREE_OPERAND (aggr_init_expr, 0); 3129132718Skan tree args = TREE_OPERAND (aggr_init_expr, 1); 3130132718Skan tree slot = TREE_OPERAND (aggr_init_expr, 2); 3131169689Skan tree type = TREE_TYPE (slot); 3132132718Skan 3133132718Skan tree call_expr; 3134132718Skan enum style_t { ctor, arg, pcc } style; 3135132718Skan 313690075Sobrien if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr)) 3137132718Skan style = ctor; 3138132718Skan#ifdef PCC_STATIC_STRUCT_RETURN 3139132718Skan else if (1) 3140132718Skan style = pcc; 3141132718Skan#endif 3142132718Skan else 3143169689Skan { 3144169689Skan gcc_assert (TREE_ADDRESSABLE (type)); 3145169689Skan style = arg; 3146169689Skan } 3147132718Skan 3148169689Skan if (style == ctor) 314990075Sobrien { 3150169689Skan /* Replace the first argument to the ctor with the address of the 3151169689Skan slot. */ 3152132718Skan tree addr; 3153132718Skan 3154169689Skan args = TREE_CHAIN (args); 3155117395Skan cxx_mark_addressable (slot); 3156169689Skan addr = build1 (ADDR_EXPR, build_pointer_type (type), slot); 3157132718Skan args = tree_cons (NULL_TREE, addr, args); 315890075Sobrien } 3159132718Skan 3160169689Skan call_expr = build3 (CALL_EXPR, 3161169689Skan TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), 3162169689Skan fn, args, NULL_TREE); 316390075Sobrien 3164132718Skan if (style == arg) 3165169689Skan { 3166169689Skan /* Just mark it addressable here, and leave the rest to 3167169689Skan expand_call{,_inline}. */ 3168169689Skan cxx_mark_addressable (slot); 3169169689Skan CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true; 3170169689Skan call_expr = build2 (MODIFY_EXPR, TREE_TYPE (call_expr), slot, call_expr); 3171169689Skan } 3172132718Skan else if (style == pcc) 317390075Sobrien { 3174132718Skan /* If we're using the non-reentrant PCC calling convention, then we 3175132718Skan need to copy the returned value out of the static buffer into the 3176132718Skan SLOT. */ 3177132718Skan push_deferring_access_checks (dk_no_check); 317890075Sobrien call_expr = build_aggr_init (slot, call_expr, 317990075Sobrien DIRECT_BIND | LOOKUP_ONLYCONVERTING); 3180132718Skan pop_deferring_access_checks (); 3181169689Skan call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot); 318290075Sobrien } 318390075Sobrien 318490075Sobrien *tp = call_expr; 318590075Sobrien} 318690075Sobrien 318790075Sobrien/* Emit all thunks to FN that should be emitted when FN is emitted. */ 318890075Sobrien 318990075Sobrienstatic void 3190132718Skanemit_associated_thunks (tree fn) 319190075Sobrien{ 319290075Sobrien /* When we use vcall offsets, we emit thunks with the virtual 319390075Sobrien functions to which they thunk. The whole point of vcall offsets 319490075Sobrien is so that you can know statically the entire set of thunks that 319590075Sobrien will ever be needed for a given virtual function, thereby 319690075Sobrien enabling you to output all the thunks with the function itself. */ 319790075Sobrien if (DECL_VIRTUAL_P (fn)) 319890075Sobrien { 3199117395Skan tree thunk; 3200169689Skan 3201117395Skan for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk)) 3202132718Skan { 3203132718Skan if (!THUNK_ALIAS (thunk)) 3204132718Skan { 3205132718Skan use_thunk (thunk, /*emit_p=*/1); 3206132718Skan if (DECL_RESULT_THUNK_P (thunk)) 3207132718Skan { 3208132718Skan tree probe; 3209169689Skan 3210132718Skan for (probe = DECL_THUNKS (thunk); 3211132718Skan probe; probe = TREE_CHAIN (probe)) 3212132718Skan use_thunk (probe, /*emit_p=*/1); 3213132718Skan } 3214132718Skan } 3215132718Skan else 3216169689Skan gcc_assert (!DECL_THUNKS (thunk)); 3217132718Skan } 321890075Sobrien } 321990075Sobrien} 322090075Sobrien 322190075Sobrien/* Generate RTL for FN. */ 322290075Sobrien 322390075Sobrienvoid 3224132718Skanexpand_body (tree fn) 322590075Sobrien{ 3226117395Skan tree saved_function; 3227169689Skan 3228132718Skan /* Compute the appropriate object-file linkage for inline 3229132718Skan functions. */ 3230132718Skan if (DECL_DECLARED_INLINE_P (fn)) 3231132718Skan import_export_decl (fn); 323290075Sobrien 3233132718Skan /* If FN is external, then there's no point in generating RTL for 3234132718Skan it. This situation can arise with an inline function under 3235132718Skan `-fexternal-templates'; we instantiate the function, even though 3236132718Skan we're not planning on emitting it, in case we get a chance to 3237132718Skan inline it. */ 3238132718Skan if (DECL_EXTERNAL (fn)) 3239132718Skan return; 3240132718Skan 3241132718Skan /* ??? When is this needed? */ 3242132718Skan saved_function = current_function_decl; 3243132718Skan 3244132718Skan /* Emit any thunks that should be emitted at the same time as FN. */ 3245132718Skan emit_associated_thunks (fn); 3246132718Skan 3247169689Skan /* This function is only called from cgraph, or recursively from 3248169689Skan emit_associated_thunks. In neither case should we be currently 3249169689Skan generating trees for a function. */ 3250169689Skan gcc_assert (function_depth == 0); 3251132718Skan 3252169689Skan tree_rest_of_compilation (fn); 3253132718Skan 3254132718Skan current_function_decl = saved_function; 3255132718Skan 3256132718Skan if (DECL_CLONED_FUNCTION_P (fn)) 3257132718Skan { 3258132718Skan /* If this is a clone, go through the other clones now and mark 3259169689Skan their parameters used. We have to do that here, as we don't 3260169689Skan know whether any particular clone will be expanded, and 3261169689Skan therefore cannot pick one arbitrarily. */ 3262132718Skan tree probe; 3263132718Skan 3264132718Skan for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn)); 3265132718Skan probe && DECL_CLONED_FUNCTION_P (probe); 3266132718Skan probe = TREE_CHAIN (probe)) 3267132718Skan { 3268132718Skan tree parms; 3269132718Skan 3270132718Skan for (parms = DECL_ARGUMENTS (probe); 3271132718Skan parms; parms = TREE_CHAIN (parms)) 3272132718Skan TREE_USED (parms) = 1; 3273132718Skan } 3274132718Skan } 3275132718Skan} 3276132718Skan 3277132718Skan/* Generate RTL for FN. */ 3278132718Skan 3279132718Skanvoid 3280132718Skanexpand_or_defer_fn (tree fn) 3281132718Skan{ 328290075Sobrien /* When the parser calls us after finishing the body of a template 3283132718Skan function, we don't really want to expand the body. */ 3284132718Skan if (processing_template_decl) 328590075Sobrien { 328690075Sobrien /* Normally, collection only occurs in rest_of_compilation. So, 328790075Sobrien if we don't collect here, we never collect junk generated 328890075Sobrien during the processing of templates until we hit a 3289169689Skan non-template function. It's not safe to do this inside a 3290169689Skan nested class, though, as the parser may have local state that 3291169689Skan is not a GC root. */ 3292169689Skan if (!function_depth) 3293169689Skan ggc_collect (); 329490075Sobrien return; 329590075Sobrien } 329690075Sobrien 329790075Sobrien /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */ 329890075Sobrien walk_tree_without_duplicates (&DECL_SAVED_TREE (fn), 329990075Sobrien simplify_aggr_init_exprs_r, 330090075Sobrien NULL); 330190075Sobrien 330290075Sobrien /* If this is a constructor or destructor body, we have to clone 330390075Sobrien it. */ 330490075Sobrien if (maybe_clone_body (fn)) 330590075Sobrien { 3306261188Spfg /* APPLE LOCAL begin radar 6305545 */ 3307261188Spfg /* Must lower the nested functions which could be, among other 3308261188Spfg things, block helper functions. */ 3309261188Spfg lower_if_nested_functions (fn); 3310261188Spfg /* APPLE LOCAL end radar 6305545 */ 331190075Sobrien /* We don't want to process FN again, so pretend we've written 331290075Sobrien it out, even though we haven't. */ 331390075Sobrien TREE_ASM_WRITTEN (fn) = 1; 331490075Sobrien return; 331590075Sobrien } 331690075Sobrien 3317146895Skan /* If this function is marked with the constructor attribute, add it 3318146895Skan to the list of functions to be called along with constructors 3319146895Skan from static duration objects. */ 3320146895Skan if (DECL_STATIC_CONSTRUCTOR (fn)) 3321146895Skan static_ctors = tree_cons (NULL_TREE, fn, static_ctors); 3322146895Skan 3323146895Skan /* If this function is marked with the destructor attribute, add it 3324146895Skan to the list of functions to be called along with destructors from 3325146895Skan static duration objects. */ 3326146895Skan if (DECL_STATIC_DESTRUCTOR (fn)) 3327146895Skan static_dtors = tree_cons (NULL_TREE, fn, static_dtors); 3328146895Skan 3329169689Skan /* We make a decision about linkage for these functions at the end 3330169689Skan of the compilation. Until that point, we do not want the back 3331169689Skan end to output them -- but we do want it to see the bodies of 3332169689Skan these functions so that it can inline them as appropriate. */ 3333169689Skan if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn)) 3334169689Skan { 3335169689Skan if (DECL_INTERFACE_KNOWN (fn)) 3336169689Skan /* We've already made a decision as to how this function will 3337169689Skan be handled. */; 3338169689Skan else if (!at_eof) 3339169689Skan { 3340169689Skan DECL_EXTERNAL (fn) = 1; 3341169689Skan DECL_NOT_REALLY_EXTERN (fn) = 1; 3342169689Skan note_vague_linkage_fn (fn); 3343169689Skan /* A non-template inline function with external linkage will 3344169689Skan always be COMDAT. As we must eventually determine the 3345169689Skan linkage of all functions, and as that causes writes to 3346169689Skan the data mapped in from the PCH file, it's advantageous 3347169689Skan to mark the functions at this point. */ 3348169689Skan if (!DECL_IMPLICIT_INSTANTIATION (fn)) 3349169689Skan { 3350169689Skan /* This function must have external linkage, as 3351169689Skan otherwise DECL_INTERFACE_KNOWN would have been 3352169689Skan set. */ 3353169689Skan gcc_assert (TREE_PUBLIC (fn)); 3354169689Skan comdat_linkage (fn); 3355169689Skan DECL_INTERFACE_KNOWN (fn) = 1; 3356169689Skan } 3357169689Skan } 3358169689Skan else 3359169689Skan import_export_decl (fn); 3360169689Skan 3361169689Skan /* If the user wants us to keep all inline functions, then mark 3362169689Skan this function as needed so that finish_file will make sure to 3363169689Skan output it later. */ 3364169689Skan if (flag_keep_inline_functions && DECL_DECLARED_INLINE_P (fn)) 3365169689Skan mark_needed (fn); 3366169689Skan } 3367169689Skan 3368169689Skan /* There's no reason to do any of the work here if we're only doing 3369169689Skan semantic analysis; this code just generates RTL. */ 3370169689Skan if (flag_syntax_only) 3371169689Skan return; 3372169689Skan 3373132718Skan function_depth++; 337490075Sobrien 3375132718Skan /* Expand or defer, at the whim of the compilation unit manager. */ 3376132718Skan cgraph_finalize_function (fn, function_depth > 1); 3377117395Skan 3378132718Skan function_depth--; 337990075Sobrien} 338090075Sobrien 3381169689Skanstruct nrv_data 3382169689Skan{ 3383169689Skan tree var; 3384169689Skan tree result; 3385169689Skan htab_t visited; 3386169689Skan}; 338790075Sobrien 3388169689Skan/* Helper function for walk_tree, used by finalize_nrv below. */ 3389169689Skan 3390169689Skanstatic tree 3391169689Skanfinalize_nrv_r (tree* tp, int* walk_subtrees, void* data) 339290075Sobrien{ 3393169689Skan struct nrv_data *dp = (struct nrv_data *)data; 3394169689Skan void **slot; 339590075Sobrien 339690075Sobrien /* No need to walk into types. There wouldn't be any need to walk into 339790075Sobrien non-statements, except that we have to consider STMT_EXPRs. */ 339890075Sobrien if (TYPE_P (*tp)) 339990075Sobrien *walk_subtrees = 0; 3400169689Skan /* Change all returns to just refer to the RESULT_DECL; this is a nop, 3401169689Skan but differs from using NULL_TREE in that it indicates that we care 3402169689Skan about the value of the RESULT_DECL. */ 3403169689Skan else if (TREE_CODE (*tp) == RETURN_EXPR) 3404169689Skan TREE_OPERAND (*tp, 0) = dp->result; 3405169689Skan /* Change all cleanups for the NRV to only run when an exception is 3406169689Skan thrown. */ 340790075Sobrien else if (TREE_CODE (*tp) == CLEANUP_STMT 3408169689Skan && CLEANUP_DECL (*tp) == dp->var) 340996263Sobrien CLEANUP_EH_ONLY (*tp) = 1; 3410169689Skan /* Replace the DECL_EXPR for the NRV with an initialization of the 3411146895Skan RESULT_DECL, if needed. */ 3412169689Skan else if (TREE_CODE (*tp) == DECL_EXPR 3413169689Skan && DECL_EXPR_DECL (*tp) == dp->var) 3414146895Skan { 3415146895Skan tree init; 3416169689Skan if (DECL_INITIAL (dp->var) 3417169689Skan && DECL_INITIAL (dp->var) != error_mark_node) 3418146895Skan { 3419169689Skan init = build2 (INIT_EXPR, void_type_node, dp->result, 3420169689Skan DECL_INITIAL (dp->var)); 3421169689Skan DECL_INITIAL (dp->var) = error_mark_node; 3422146895Skan } 3423146895Skan else 3424169689Skan init = build_empty_stmt (); 3425169689Skan SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp)); 3426146895Skan *tp = init; 3427146895Skan } 3428169689Skan /* And replace all uses of the NRV with the RESULT_DECL. */ 3429169689Skan else if (*tp == dp->var) 3430169689Skan *tp = dp->result; 343190075Sobrien 3432169689Skan /* Avoid walking into the same tree more than once. Unfortunately, we 3433169689Skan can't just use walk_tree_without duplicates because it would only call 3434169689Skan us for the first occurrence of dp->var in the function body. */ 3435169689Skan slot = htab_find_slot (dp->visited, *tp, INSERT); 3436169689Skan if (*slot) 3437169689Skan *walk_subtrees = 0; 3438169689Skan else 3439169689Skan *slot = *tp; 3440169689Skan 344190075Sobrien /* Keep iterating. */ 344290075Sobrien return NULL_TREE; 344390075Sobrien} 344490075Sobrien 3445169689Skan/* Called from finish_function to implement the named return value 3446169689Skan optimization by overriding all the RETURN_EXPRs and pertinent 3447169689Skan CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the 3448169689Skan RESULT_DECL for the function. */ 344990075Sobrien 3450132718Skanvoid 3451169689Skanfinalize_nrv (tree *tp, tree var, tree result) 345290075Sobrien{ 3453169689Skan struct nrv_data data; 3454169689Skan 3455169689Skan /* Copy debugging information from VAR to RESULT. */ 3456169689Skan DECL_NAME (result) = DECL_NAME (var); 3457169689Skan DECL_ARTIFICIAL (result) = DECL_ARTIFICIAL (var); 3458169689Skan DECL_IGNORED_P (result) = DECL_IGNORED_P (var); 3459169689Skan DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var); 3460169689Skan DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var); 3461169689Skan /* Don't forget that we take its address. */ 3462169689Skan TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var); 3463169689Skan 3464169689Skan data.var = var; 3465169689Skan data.result = result; 3466169689Skan data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 3467169689Skan walk_tree (tp, finalize_nrv_r, &data, 0); 3468169689Skan htab_delete (data.visited); 346990075Sobrien} 3470169689Skan 3471169689Skan/* For all elements of CLAUSES, validate them vs OpenMP constraints. 3472169689Skan Remove any elements from the list that are invalid. */ 347390075Sobrien 3474169689Skantree 3475169689Skanfinish_omp_clauses (tree clauses) 3476169689Skan{ 3477169689Skan bitmap_head generic_head, firstprivate_head, lastprivate_head; 3478169689Skan tree c, t, *pc = &clauses; 3479169689Skan const char *name; 348090075Sobrien 3481169689Skan bitmap_obstack_initialize (NULL); 3482169689Skan bitmap_initialize (&generic_head, &bitmap_default_obstack); 3483169689Skan bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); 3484169689Skan bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); 3485169689Skan 3486169689Skan for (pc = &clauses, c = clauses; c ; c = *pc) 3487169689Skan { 3488169689Skan bool remove = false; 3489169689Skan 3490169689Skan switch (OMP_CLAUSE_CODE (c)) 3491169689Skan { 3492169689Skan case OMP_CLAUSE_SHARED: 3493169689Skan name = "shared"; 3494169689Skan goto check_dup_generic; 3495169689Skan case OMP_CLAUSE_PRIVATE: 3496169689Skan name = "private"; 3497169689Skan goto check_dup_generic; 3498169689Skan case OMP_CLAUSE_REDUCTION: 3499169689Skan name = "reduction"; 3500169689Skan goto check_dup_generic; 3501169689Skan case OMP_CLAUSE_COPYPRIVATE: 3502169689Skan name = "copyprivate"; 3503169689Skan goto check_dup_generic; 3504169689Skan case OMP_CLAUSE_COPYIN: 3505169689Skan name = "copyin"; 3506169689Skan goto check_dup_generic; 3507169689Skan check_dup_generic: 3508169689Skan t = OMP_CLAUSE_DECL (c); 3509169689Skan if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3510169689Skan { 3511169689Skan if (processing_template_decl) 3512169689Skan break; 3513171825Skan if (DECL_P (t)) 3514171825Skan error ("%qD is not a variable in clause %qs", t, name); 3515171825Skan else 3516171825Skan error ("%qE is not a variable in clause %qs", t, name); 3517169689Skan remove = true; 3518169689Skan } 3519169689Skan else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 3520169689Skan || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 3521169689Skan || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 3522169689Skan { 3523171825Skan error ("%qD appears more than once in data clauses", t); 3524169689Skan remove = true; 3525169689Skan } 3526169689Skan else 3527169689Skan bitmap_set_bit (&generic_head, DECL_UID (t)); 3528169689Skan break; 3529169689Skan 3530169689Skan case OMP_CLAUSE_FIRSTPRIVATE: 3531169689Skan t = OMP_CLAUSE_DECL (c); 3532169689Skan if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3533169689Skan { 3534169689Skan if (processing_template_decl) 3535169689Skan break; 3536169689Skan error ("%qE is not a variable in clause %<firstprivate%>", t); 3537169689Skan remove = true; 3538169689Skan } 3539169689Skan else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 3540169689Skan || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) 3541169689Skan { 3542169689Skan error ("%qE appears more than once in data clauses", t); 3543169689Skan remove = true; 3544169689Skan } 3545169689Skan else 3546169689Skan bitmap_set_bit (&firstprivate_head, DECL_UID (t)); 3547169689Skan break; 3548169689Skan 3549169689Skan case OMP_CLAUSE_LASTPRIVATE: 3550169689Skan t = OMP_CLAUSE_DECL (c); 3551169689Skan if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3552169689Skan { 3553169689Skan if (processing_template_decl) 3554169689Skan break; 3555169689Skan error ("%qE is not a variable in clause %<lastprivate%>", t); 3556169689Skan remove = true; 3557169689Skan } 3558169689Skan else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 3559169689Skan || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 3560169689Skan { 3561169689Skan error ("%qE appears more than once in data clauses", t); 3562169689Skan remove = true; 3563169689Skan } 3564169689Skan else 3565169689Skan bitmap_set_bit (&lastprivate_head, DECL_UID (t)); 3566169689Skan break; 3567169689Skan 3568169689Skan case OMP_CLAUSE_IF: 3569169689Skan t = OMP_CLAUSE_IF_EXPR (c); 3570169689Skan t = maybe_convert_cond (t); 3571169689Skan if (t == error_mark_node) 3572169689Skan remove = true; 3573169689Skan OMP_CLAUSE_IF_EXPR (c) = t; 3574169689Skan break; 3575169689Skan 3576169689Skan case OMP_CLAUSE_NUM_THREADS: 3577169689Skan t = OMP_CLAUSE_NUM_THREADS_EXPR (c); 3578169689Skan if (t == error_mark_node) 3579169689Skan remove = true; 3580169689Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) 3581169689Skan && !type_dependent_expression_p (t)) 3582169689Skan { 3583169689Skan error ("num_threads expression must be integral"); 3584169689Skan remove = true; 3585169689Skan } 3586169689Skan break; 3587169689Skan 3588169689Skan case OMP_CLAUSE_SCHEDULE: 3589169689Skan t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c); 3590169689Skan if (t == NULL) 3591169689Skan ; 3592169689Skan else if (t == error_mark_node) 3593169689Skan remove = true; 3594169689Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) 3595169689Skan && !type_dependent_expression_p (t)) 3596169689Skan { 3597169689Skan error ("schedule chunk size expression must be integral"); 3598169689Skan remove = true; 3599169689Skan } 3600169689Skan break; 3601169689Skan 3602169689Skan case OMP_CLAUSE_NOWAIT: 3603169689Skan case OMP_CLAUSE_ORDERED: 3604169689Skan case OMP_CLAUSE_DEFAULT: 3605169689Skan break; 3606169689Skan 3607169689Skan default: 3608169689Skan gcc_unreachable (); 3609169689Skan } 3610169689Skan 3611169689Skan if (remove) 3612169689Skan *pc = OMP_CLAUSE_CHAIN (c); 3613169689Skan else 3614169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3615169689Skan } 3616169689Skan 3617169689Skan for (pc = &clauses, c = clauses; c ; c = *pc) 3618169689Skan { 3619169689Skan enum tree_code c_kind = OMP_CLAUSE_CODE (c); 3620169689Skan bool remove = false; 3621169689Skan bool need_complete_non_reference = false; 3622169689Skan bool need_default_ctor = false; 3623169689Skan bool need_copy_ctor = false; 3624169689Skan bool need_copy_assignment = false; 3625169689Skan bool need_implicitly_determined = false; 3626169689Skan tree type, inner_type; 3627169689Skan 3628169689Skan switch (c_kind) 3629169689Skan { 3630169689Skan case OMP_CLAUSE_SHARED: 3631169689Skan name = "shared"; 3632169689Skan need_implicitly_determined = true; 3633169689Skan break; 3634169689Skan case OMP_CLAUSE_PRIVATE: 3635169689Skan name = "private"; 3636169689Skan need_complete_non_reference = true; 3637169689Skan need_default_ctor = true; 3638169689Skan need_implicitly_determined = true; 3639169689Skan break; 3640169689Skan case OMP_CLAUSE_FIRSTPRIVATE: 3641169689Skan name = "firstprivate"; 3642169689Skan need_complete_non_reference = true; 3643169689Skan need_copy_ctor = true; 3644169689Skan need_implicitly_determined = true; 3645169689Skan break; 3646169689Skan case OMP_CLAUSE_LASTPRIVATE: 3647169689Skan name = "lastprivate"; 3648169689Skan need_complete_non_reference = true; 3649169689Skan need_copy_assignment = true; 3650169689Skan need_implicitly_determined = true; 3651169689Skan break; 3652169689Skan case OMP_CLAUSE_REDUCTION: 3653169689Skan name = "reduction"; 3654169689Skan need_implicitly_determined = true; 3655169689Skan break; 3656169689Skan case OMP_CLAUSE_COPYPRIVATE: 3657169689Skan name = "copyprivate"; 3658169689Skan need_copy_assignment = true; 3659169689Skan break; 3660169689Skan case OMP_CLAUSE_COPYIN: 3661169689Skan name = "copyin"; 3662169689Skan need_copy_assignment = true; 3663169689Skan break; 3664169689Skan default: 3665169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3666169689Skan continue; 3667169689Skan } 3668169689Skan 3669169689Skan t = OMP_CLAUSE_DECL (c); 3670169689Skan if (processing_template_decl 3671169689Skan && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3672169689Skan { 3673169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3674169689Skan continue; 3675169689Skan } 3676169689Skan 3677169689Skan switch (c_kind) 3678169689Skan { 3679169689Skan case OMP_CLAUSE_LASTPRIVATE: 3680169689Skan if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t))) 3681169689Skan need_default_ctor = true; 3682169689Skan break; 3683169689Skan 3684169689Skan case OMP_CLAUSE_REDUCTION: 3685169689Skan if (AGGREGATE_TYPE_P (TREE_TYPE (t)) 3686169689Skan || POINTER_TYPE_P (TREE_TYPE (t))) 3687169689Skan { 3688169689Skan error ("%qE has invalid type for %<reduction%>", t); 3689169689Skan remove = true; 3690169689Skan } 3691169689Skan else if (FLOAT_TYPE_P (TREE_TYPE (t))) 3692169689Skan { 3693169689Skan enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); 3694169689Skan switch (r_code) 3695169689Skan { 3696169689Skan case PLUS_EXPR: 3697169689Skan case MULT_EXPR: 3698169689Skan case MINUS_EXPR: 3699169689Skan break; 3700169689Skan default: 3701169689Skan error ("%qE has invalid type for %<reduction(%s)%>", 3702169689Skan t, operator_name_info[r_code].name); 3703169689Skan remove = true; 3704169689Skan } 3705169689Skan } 3706169689Skan break; 3707169689Skan 3708169689Skan case OMP_CLAUSE_COPYIN: 3709169689Skan if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) 3710169689Skan { 3711169689Skan error ("%qE must be %<threadprivate%> for %<copyin%>", t); 3712169689Skan remove = true; 3713169689Skan } 3714169689Skan break; 3715169689Skan 3716169689Skan default: 3717169689Skan break; 3718169689Skan } 3719169689Skan 3720169689Skan if (need_complete_non_reference) 3721169689Skan { 3722169689Skan t = require_complete_type (t); 3723169689Skan if (t == error_mark_node) 3724169689Skan remove = true; 3725169689Skan else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) 3726169689Skan { 3727169689Skan error ("%qE has reference type for %qs", t, name); 3728169689Skan remove = true; 3729169689Skan } 3730169689Skan } 3731169689Skan if (need_implicitly_determined) 3732169689Skan { 3733169689Skan const char *share_name = NULL; 3734169689Skan 3735169689Skan if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) 3736169689Skan share_name = "threadprivate"; 3737169689Skan else switch (cxx_omp_predetermined_sharing (t)) 3738169689Skan { 3739169689Skan case OMP_CLAUSE_DEFAULT_UNSPECIFIED: 3740169689Skan break; 3741169689Skan case OMP_CLAUSE_DEFAULT_SHARED: 3742169689Skan share_name = "shared"; 3743169689Skan break; 3744169689Skan case OMP_CLAUSE_DEFAULT_PRIVATE: 3745169689Skan share_name = "private"; 3746169689Skan break; 3747169689Skan default: 3748169689Skan gcc_unreachable (); 3749169689Skan } 3750169689Skan if (share_name) 3751169689Skan { 3752169689Skan error ("%qE is predetermined %qs for %qs", 3753169689Skan t, share_name, name); 3754169689Skan remove = true; 3755169689Skan } 3756169689Skan } 3757169689Skan 3758169689Skan /* We're interested in the base element, not arrays. */ 3759169689Skan inner_type = type = TREE_TYPE (t); 3760169689Skan while (TREE_CODE (inner_type) == ARRAY_TYPE) 3761169689Skan inner_type = TREE_TYPE (inner_type); 3762169689Skan 3763169689Skan /* Check for special function availability by building a call to one. 3764169689Skan Save the results, because later we won't be in the right context 3765169689Skan for making these queries. */ 3766169689Skan if (CLASS_TYPE_P (inner_type) 3767169689Skan && (need_default_ctor || need_copy_ctor || need_copy_assignment) 3768169689Skan && !type_dependent_expression_p (t)) 3769169689Skan { 3770169689Skan int save_errorcount = errorcount; 3771169689Skan tree info; 3772169689Skan 3773169689Skan /* Always allocate 3 elements for simplicity. These are the 3774169689Skan function decls for the ctor, dtor, and assignment op. 3775169689Skan This layout is known to the three lang hooks, 3776169689Skan cxx_omp_clause_default_init, cxx_omp_clause_copy_init, 3777169689Skan and cxx_omp_clause_assign_op. */ 3778169689Skan info = make_tree_vec (3); 3779169689Skan CP_OMP_CLAUSE_INFO (c) = info; 3780169689Skan 3781169689Skan if (need_default_ctor 3782169689Skan || (need_copy_ctor 3783169689Skan && !TYPE_HAS_TRIVIAL_INIT_REF (inner_type))) 3784169689Skan { 3785169689Skan if (need_default_ctor) 3786169689Skan t = NULL; 3787169689Skan else 3788169689Skan { 3789169689Skan t = build_int_cst (build_pointer_type (inner_type), 0); 3790169689Skan t = build1 (INDIRECT_REF, inner_type, t); 3791169689Skan t = build_tree_list (NULL, t); 3792169689Skan } 3793169689Skan t = build_special_member_call (NULL_TREE, 3794169689Skan complete_ctor_identifier, 3795169689Skan t, inner_type, LOOKUP_NORMAL); 3796169689Skan t = get_callee_fndecl (t); 3797169689Skan TREE_VEC_ELT (info, 0) = t; 3798169689Skan } 3799169689Skan 3800169689Skan if ((need_default_ctor || need_copy_ctor) 3801169689Skan && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_type)) 3802169689Skan { 3803169689Skan t = build_int_cst (build_pointer_type (inner_type), 0); 3804169689Skan t = build1 (INDIRECT_REF, inner_type, t); 3805169689Skan t = build_special_member_call (t, complete_dtor_identifier, 3806169689Skan NULL, inner_type, LOOKUP_NORMAL); 3807169689Skan t = get_callee_fndecl (t); 3808169689Skan TREE_VEC_ELT (info, 1) = t; 3809169689Skan } 3810169689Skan 3811169689Skan if (need_copy_assignment 3812169689Skan && !TYPE_HAS_TRIVIAL_ASSIGN_REF (inner_type)) 3813169689Skan { 3814169689Skan t = build_int_cst (build_pointer_type (inner_type), 0); 3815169689Skan t = build1 (INDIRECT_REF, inner_type, t); 3816169689Skan t = build_special_member_call (t, ansi_assopname (NOP_EXPR), 3817169689Skan build_tree_list (NULL, t), 3818169689Skan inner_type, LOOKUP_NORMAL); 3819169689Skan 3820169689Skan /* We'll have called convert_from_reference on the call, which 3821169689Skan may well have added an indirect_ref. It's unneeded here, 3822169689Skan and in the way, so kill it. */ 3823169689Skan if (TREE_CODE (t) == INDIRECT_REF) 3824169689Skan t = TREE_OPERAND (t, 0); 3825169689Skan 3826169689Skan t = get_callee_fndecl (t); 3827169689Skan TREE_VEC_ELT (info, 2) = t; 3828169689Skan } 3829169689Skan 3830169689Skan if (errorcount != save_errorcount) 3831169689Skan remove = true; 3832169689Skan } 3833169689Skan 3834169689Skan if (remove) 3835169689Skan *pc = OMP_CLAUSE_CHAIN (c); 3836169689Skan else 3837169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3838169689Skan } 3839169689Skan 3840169689Skan bitmap_obstack_release (NULL); 3841169689Skan return clauses; 3842169689Skan} 3843169689Skan 3844169689Skan/* For all variables in the tree_list VARS, mark them as thread local. */ 3845169689Skan 384690075Sobrienvoid 3847169689Skanfinish_omp_threadprivate (tree vars) 3848169689Skan{ 3849169689Skan tree t; 3850169689Skan 3851169689Skan /* Mark every variable in VARS to be assigned thread local storage. */ 3852169689Skan for (t = vars; t; t = TREE_CHAIN (t)) 3853169689Skan { 3854169689Skan tree v = TREE_PURPOSE (t); 3855169689Skan 3856169689Skan /* If V had already been marked threadprivate, it doesn't matter 3857169689Skan whether it had been used prior to this point. */ 3858169689Skan if (TREE_USED (v) 3859169689Skan && (DECL_LANG_SPECIFIC (v) == NULL 3860169689Skan || !CP_DECL_THREADPRIVATE_P (v))) 3861169689Skan error ("%qE declared %<threadprivate%> after first use", v); 3862169689Skan else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v)) 3863169689Skan error ("automatic variable %qE cannot be %<threadprivate%>", v); 3864169689Skan else if (! COMPLETE_TYPE_P (TREE_TYPE (v))) 3865169689Skan error ("%<threadprivate%> %qE has incomplete type", v); 3866169689Skan else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))) 3867169689Skan error ("%<threadprivate%> %qE is not file, namespace " 3868169689Skan "or block scope variable", v); 3869169689Skan else 3870169689Skan { 3871169689Skan /* Allocate a LANG_SPECIFIC structure for V, if needed. */ 3872169689Skan if (DECL_LANG_SPECIFIC (v) == NULL) 3873169689Skan { 3874169689Skan retrofit_lang_decl (v); 3875169689Skan 3876169689Skan /* Make sure that DECL_DISCRIMINATOR_P continues to be true 3877169689Skan after the allocation of the lang_decl structure. */ 3878169689Skan if (DECL_DISCRIMINATOR_P (v)) 3879169689Skan DECL_LANG_SPECIFIC (v)->decl_flags.u2sel = 1; 3880169689Skan } 3881169689Skan 3882169689Skan if (! DECL_THREAD_LOCAL_P (v)) 3883169689Skan { 3884169689Skan DECL_TLS_MODEL (v) = decl_default_tls_model (v); 3885169689Skan /* If rtl has been already set for this var, call 3886169689Skan make_decl_rtl once again, so that encode_section_info 3887169689Skan has a chance to look at the new decl flags. */ 3888169689Skan if (DECL_RTL_SET_P (v)) 3889169689Skan make_decl_rtl (v); 3890169689Skan } 3891169689Skan CP_DECL_THREADPRIVATE_P (v) = 1; 3892169689Skan } 3893169689Skan } 3894169689Skan} 3895169689Skan 3896169689Skan/* Build an OpenMP structured block. */ 3897169689Skan 3898169689Skantree 3899169689Skanbegin_omp_structured_block (void) 3900169689Skan{ 3901169689Skan return do_pushlevel (sk_omp); 3902169689Skan} 3903169689Skan 3904169689Skantree 3905169689Skanfinish_omp_structured_block (tree block) 3906169689Skan{ 3907169689Skan return do_poplevel (block); 3908169689Skan} 3909169689Skan 3910169689Skan/* Similarly, except force the retention of the BLOCK. */ 3911169689Skan 3912169689Skantree 3913169689Skanbegin_omp_parallel (void) 3914169689Skan{ 3915169689Skan keep_next_level (true); 3916169689Skan return begin_omp_structured_block (); 3917169689Skan} 3918169689Skan 3919169689Skantree 3920169689Skanfinish_omp_parallel (tree clauses, tree body) 3921169689Skan{ 3922169689Skan tree stmt; 3923169689Skan 3924169689Skan body = finish_omp_structured_block (body); 3925169689Skan 3926169689Skan stmt = make_node (OMP_PARALLEL); 3927169689Skan TREE_TYPE (stmt) = void_type_node; 3928169689Skan OMP_PARALLEL_CLAUSES (stmt) = clauses; 3929169689Skan OMP_PARALLEL_BODY (stmt) = body; 3930169689Skan 3931169689Skan return add_stmt (stmt); 3932169689Skan} 3933169689Skan 3934169689Skan/* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR 3935169689Skan are directly for their associated operands in the statement. DECL 3936169689Skan and INIT are a combo; if DECL is NULL then INIT ought to be a 3937169689Skan MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are 3938169689Skan optional statements that need to go before the loop into its 3939169689Skan sk_omp scope. */ 3940169689Skan 3941169689Skantree 3942169689Skanfinish_omp_for (location_t locus, tree decl, tree init, tree cond, 3943169689Skan tree incr, tree body, tree pre_body) 3944169689Skan{ 3945169689Skan if (decl == NULL) 3946169689Skan { 3947169689Skan if (init != NULL) 3948169689Skan switch (TREE_CODE (init)) 3949169689Skan { 3950169689Skan case MODIFY_EXPR: 3951169689Skan decl = TREE_OPERAND (init, 0); 3952169689Skan init = TREE_OPERAND (init, 1); 3953169689Skan break; 3954169689Skan case MODOP_EXPR: 3955169689Skan if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR) 3956169689Skan { 3957169689Skan decl = TREE_OPERAND (init, 0); 3958169689Skan init = TREE_OPERAND (init, 2); 3959169689Skan } 3960169689Skan break; 3961169689Skan default: 3962169689Skan break; 3963169689Skan } 3964169689Skan 3965169689Skan if (decl == NULL) 3966169689Skan { 3967169689Skan error ("expected iteration declaration or initialization"); 3968169689Skan return NULL; 3969169689Skan } 3970169689Skan } 3971169689Skan 3972169689Skan if (type_dependent_expression_p (decl) 3973169689Skan || type_dependent_expression_p (init) 3974169689Skan || (cond && type_dependent_expression_p (cond)) 3975169689Skan || (incr && type_dependent_expression_p (incr))) 3976169689Skan { 3977169689Skan tree stmt; 3978169689Skan 3979169689Skan if (cond == NULL) 3980169689Skan { 3981169689Skan error ("%Hmissing controlling predicate", &locus); 3982169689Skan return NULL; 3983169689Skan } 3984169689Skan 3985169689Skan if (incr == NULL) 3986169689Skan { 3987169689Skan error ("%Hmissing increment expression", &locus); 3988169689Skan return NULL; 3989169689Skan } 3990169689Skan 3991169689Skan stmt = make_node (OMP_FOR); 3992169689Skan 3993169689Skan /* This is really just a place-holder. We'll be decomposing this 3994169689Skan again and going through the build_modify_expr path below when 3995169689Skan we instantiate the thing. */ 3996169689Skan init = build2 (MODIFY_EXPR, void_type_node, decl, init); 3997169689Skan 3998169689Skan TREE_TYPE (stmt) = void_type_node; 3999169689Skan OMP_FOR_INIT (stmt) = init; 4000169689Skan OMP_FOR_COND (stmt) = cond; 4001169689Skan OMP_FOR_INCR (stmt) = incr; 4002169689Skan OMP_FOR_BODY (stmt) = body; 4003169689Skan OMP_FOR_PRE_BODY (stmt) = pre_body; 4004169689Skan 4005169689Skan SET_EXPR_LOCATION (stmt, locus); 4006169689Skan return add_stmt (stmt); 4007169689Skan } 4008169689Skan 4009169689Skan if (!DECL_P (decl)) 4010169689Skan { 4011169689Skan error ("expected iteration declaration or initialization"); 4012169689Skan return NULL; 4013169689Skan } 4014169689Skan 4015169689Skan if (pre_body == NULL || IS_EMPTY_STMT (pre_body)) 4016169689Skan pre_body = NULL; 4017169689Skan else if (! processing_template_decl) 4018169689Skan { 4019169689Skan add_stmt (pre_body); 4020169689Skan pre_body = NULL; 4021169689Skan } 4022169689Skan init = build_modify_expr (decl, NOP_EXPR, init); 4023169689Skan return c_finish_omp_for (locus, decl, init, cond, incr, body, pre_body); 4024169689Skan} 4025169689Skan 4026169689Skanvoid 4027169689Skanfinish_omp_atomic (enum tree_code code, tree lhs, tree rhs) 4028169689Skan{ 4029169689Skan tree orig_lhs; 4030169689Skan tree orig_rhs; 4031169689Skan bool dependent_p; 4032169689Skan tree stmt; 4033169689Skan 4034169689Skan orig_lhs = lhs; 4035169689Skan orig_rhs = rhs; 4036169689Skan dependent_p = false; 4037169689Skan stmt = NULL_TREE; 4038169689Skan 4039169689Skan /* Even in a template, we can detect invalid uses of the atomic 4040169689Skan pragma if neither LHS nor RHS is type-dependent. */ 4041169689Skan if (processing_template_decl) 4042169689Skan { 4043169689Skan dependent_p = (type_dependent_expression_p (lhs) 4044169689Skan || type_dependent_expression_p (rhs)); 4045169689Skan if (!dependent_p) 4046169689Skan { 4047169689Skan lhs = build_non_dependent_expr (lhs); 4048169689Skan rhs = build_non_dependent_expr (rhs); 4049169689Skan } 4050169689Skan } 4051169689Skan if (!dependent_p) 4052169689Skan { 4053169689Skan stmt = c_finish_omp_atomic (code, lhs, rhs); 4054169689Skan if (stmt == error_mark_node) 4055169689Skan return; 4056169689Skan } 4057169689Skan if (processing_template_decl) 4058169689Skan { 4059169689Skan stmt = build2 (OMP_ATOMIC, void_type_node, orig_lhs, orig_rhs); 4060169689Skan OMP_ATOMIC_DEPENDENT_P (stmt) = 1; 4061169689Skan OMP_ATOMIC_CODE (stmt) = code; 4062169689Skan } 4063169689Skan add_stmt (stmt); 4064169689Skan} 4065169689Skan 4066169689Skanvoid 4067169689Skanfinish_omp_barrier (void) 4068169689Skan{ 4069169689Skan tree fn = built_in_decls[BUILT_IN_GOMP_BARRIER]; 4070169689Skan tree stmt = finish_call_expr (fn, NULL, false, false); 4071169689Skan finish_expr_stmt (stmt); 4072169689Skan} 4073169689Skan 4074169689Skanvoid 4075169689Skanfinish_omp_flush (void) 4076169689Skan{ 4077169689Skan tree fn = built_in_decls[BUILT_IN_SYNCHRONIZE]; 4078169689Skan tree stmt = finish_call_expr (fn, NULL, false, false); 4079169689Skan finish_expr_stmt (stmt); 4080169689Skan} 4081169689Skan 4082169689Skan/* True if OpenMP sharing attribute of DECL is predetermined. */ 4083169689Skan 4084169689Skanenum omp_clause_default_kind 4085169689Skancxx_omp_predetermined_sharing (tree decl) 4086169689Skan{ 4087169689Skan enum omp_clause_default_kind kind; 4088169689Skan 4089169689Skan kind = c_omp_predetermined_sharing (decl); 4090169689Skan if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED) 4091169689Skan return kind; 4092169689Skan 4093169689Skan /* Static data members are predetermined as shared. */ 4094169689Skan if (TREE_STATIC (decl)) 4095169689Skan { 4096169689Skan tree ctx = CP_DECL_CONTEXT (decl); 4097169689Skan if (TYPE_P (ctx) && IS_AGGR_TYPE (ctx)) 4098169689Skan return OMP_CLAUSE_DEFAULT_SHARED; 4099169689Skan } 4100169689Skan 4101169689Skan return OMP_CLAUSE_DEFAULT_UNSPECIFIED; 4102169689Skan} 4103261188Spfg 4104261188Spfg/* APPLE LOCAL begin blocks 6040305 (ch) */ 4105261188Spfgtree 4106261188Spfgbegin_block (void) 4107261188Spfg{ 4108261188Spfg struct block_sema_info *csi; 4109261188Spfg tree block; 4110261188Spfg /* push_scope (); */ 4111261188Spfg current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4112261188Spfg#if 0 4113261188Spfg block = do_pushlevel (sk_block); 4114261188Spfg#else 4115261188Spfg block = NULL_TREE; 4116261188Spfg#endif 4117261188Spfg csi = (struct block_sema_info*)xcalloc (1, sizeof (struct block_sema_info)); 4118261188Spfg csi->prev_block_info = cur_block; 4119261188Spfg cur_block = csi; 4120261188Spfg return block; 4121261188Spfg} 4122261188Spfg 4123261188Spfgstruct block_sema_info * 4124261188Spfgfinish_block (tree block) 4125261188Spfg{ 4126261188Spfg struct block_sema_info *csi = cur_block; 4127261188Spfg cur_block = cur_block->prev_block_info; 4128261188Spfg /* pop_scope (); */ 4129261188Spfg#if 0 4130261188Spfg if (block) 4131261188Spfg do_poplevel (block); 4132261188Spfg#else 4133261188Spfg block = 0; 4134261188Spfg#endif 4135261188Spfg return csi; 4136261188Spfg} 4137261188Spfg/* APPLE LOCAL end blocks 6040305 (ch) */ 4138261188Spfg 4139169689Skan 4140169689Skanvoid 4141132718Skaninit_cp_semantics (void) 414290075Sobrien{ 414390075Sobrien} 4144132718Skan 4145132718Skan#include "gt-cp-semantics.h" 4146