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); 59090075Sobrien return condition_conversion (cond); 59190075Sobrien} 59290075Sobrien 59350397Sobrien/* Finish an expression-statement, whose EXPRESSION is as indicated. */ 59450397Sobrien 59590075Sobrientree 596132718Skanfinish_expr_stmt (tree expr) 59750397Sobrien{ 59890075Sobrien tree r = NULL_TREE; 59990075Sobrien 60050397Sobrien if (expr != NULL_TREE) 60150397Sobrien { 602132718Skan if (!processing_template_decl) 603169689Skan { 604169689Skan if (warn_sequence_point) 605169689Skan verify_sequence_points (expr); 606169689Skan expr = convert_to_void (expr, "statement"); 607169689Skan } 608132718Skan else if (!type_dependent_expression_p (expr)) 609132718Skan convert_to_void (build_non_dependent_expr (expr), "statement"); 610169689Skan 611169689Skan /* Simplification of inner statement expressions, compound exprs, 612169689Skan etc can result in us already having an EXPR_STMT. */ 613169689Skan if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) 614169689Skan { 615169689Skan if (TREE_CODE (expr) != EXPR_STMT) 616169689Skan expr = build_stmt (EXPR_STMT, expr); 617169689Skan expr = maybe_cleanup_point_expr_void (expr); 618169689Skan } 619169689Skan 620169689Skan r = add_stmt (expr); 62150397Sobrien } 62250397Sobrien 62350397Sobrien finish_stmt (); 62490075Sobrien 62590075Sobrien return r; 62650397Sobrien} 62750397Sobrien 62890075Sobrien 62950397Sobrien/* Begin an if-statement. Returns a newly created IF_STMT if 63050397Sobrien appropriate. */ 63150397Sobrien 63250397Sobrientree 633132718Skanbegin_if_stmt (void) 63450397Sobrien{ 635169689Skan tree r, scope; 636169689Skan scope = do_pushlevel (sk_block); 63790075Sobrien r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE); 638169689Skan TREE_CHAIN (r) = scope; 639169689Skan begin_cond (&IF_COND (r)); 64050397Sobrien return r; 64150397Sobrien} 64250397Sobrien 64350397Sobrien/* Process the COND of an if-statement, which may be given by 64450397Sobrien IF_STMT. */ 64550397Sobrien 646169689Skanvoid 647132718Skanfinish_if_stmt_cond (tree cond, tree if_stmt) 64850397Sobrien{ 649169689Skan finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond)); 650169689Skan add_stmt (if_stmt); 651169689Skan THEN_CLAUSE (if_stmt) = push_stmt_list (); 65250397Sobrien} 65350397Sobrien 65450397Sobrien/* Finish the then-clause of an if-statement, which may be given by 65550397Sobrien IF_STMT. */ 65650397Sobrien 65750397Sobrientree 658132718Skanfinish_then_clause (tree if_stmt) 65950397Sobrien{ 660169689Skan THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt)); 66190075Sobrien return if_stmt; 66250397Sobrien} 66350397Sobrien 66450397Sobrien/* Begin the else-clause of an if-statement. */ 66550397Sobrien 666169689Skanvoid 667169689Skanbegin_else_clause (tree if_stmt) 66850397Sobrien{ 669169689Skan ELSE_CLAUSE (if_stmt) = push_stmt_list (); 67050397Sobrien} 67150397Sobrien 67250397Sobrien/* Finish the else-clause of an if-statement, which may be given by 67350397Sobrien IF_STMT. */ 67450397Sobrien 67550397Sobrienvoid 676132718Skanfinish_else_clause (tree if_stmt) 67750397Sobrien{ 678169689Skan ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt)); 67950397Sobrien} 68050397Sobrien 68190075Sobrien/* Finish an if-statement. */ 68250397Sobrien 683169689Skanvoid 684169689Skanfinish_if_stmt (tree if_stmt) 68550397Sobrien{ 686169689Skan tree scope = TREE_CHAIN (if_stmt); 687169689Skan TREE_CHAIN (if_stmt) = NULL; 688169689Skan add_stmt (do_poplevel (scope)); 689117395Skan finish_stmt (); 690169689Skan empty_body_warning (THEN_CLAUSE (if_stmt), ELSE_CLAUSE (if_stmt)); 69150397Sobrien} 69250397Sobrien 69350397Sobrien/* Begin a while-statement. Returns a newly created WHILE_STMT if 69450397Sobrien appropriate. */ 69550397Sobrien 69650397Sobrientree 697132718Skanbegin_while_stmt (void) 69850397Sobrien{ 69950397Sobrien tree r; 70090075Sobrien r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE); 70190075Sobrien add_stmt (r); 702169689Skan WHILE_BODY (r) = do_pushlevel (sk_block); 703169689Skan begin_cond (&WHILE_COND (r)); 70450397Sobrien return r; 70550397Sobrien} 70650397Sobrien 70790075Sobrien/* Process the COND of a while-statement, which may be given by 70850397Sobrien WHILE_STMT. */ 70950397Sobrien 710169689Skanvoid 711132718Skanfinish_while_stmt_cond (tree cond, tree while_stmt) 71250397Sobrien{ 713169689Skan finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond)); 714169689Skan simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt)); 71550397Sobrien} 71650397Sobrien 71750397Sobrien/* Finish a while-statement, which may be given by WHILE_STMT. */ 71850397Sobrien 719169689Skanvoid 720132718Skanfinish_while_stmt (tree while_stmt) 72150397Sobrien{ 722169689Skan WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt)); 72350397Sobrien finish_stmt (); 72450397Sobrien} 72550397Sobrien 72650397Sobrien/* Begin a do-statement. Returns a newly created DO_STMT if 72750397Sobrien appropriate. */ 72850397Sobrien 72950397Sobrientree 730132718Skanbegin_do_stmt (void) 73150397Sobrien{ 73290075Sobrien tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE); 73390075Sobrien add_stmt (r); 734169689Skan DO_BODY (r) = push_stmt_list (); 73590075Sobrien return r; 73650397Sobrien} 73750397Sobrien 73850397Sobrien/* Finish the body of a do-statement, which may be given by DO_STMT. */ 73950397Sobrien 74050397Sobrienvoid 741132718Skanfinish_do_body (tree do_stmt) 74250397Sobrien{ 743169689Skan DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt)); 74450397Sobrien} 74550397Sobrien 74650397Sobrien/* Finish a do-statement, which may be given by DO_STMT, and whose 74750397Sobrien COND is as indicated. */ 74850397Sobrien 74950397Sobrienvoid 750132718Skanfinish_do_stmt (tree cond, tree do_stmt) 75150397Sobrien{ 75290075Sobrien cond = maybe_convert_cond (cond); 75390075Sobrien DO_COND (do_stmt) = cond; 75450397Sobrien finish_stmt (); 75550397Sobrien} 75650397Sobrien 75750397Sobrien/* Finish a return-statement. The EXPRESSION returned, if any, is as 75850397Sobrien indicated. */ 75950397Sobrien 76090075Sobrientree 761132718Skanfinish_return_stmt (tree expr) 76250397Sobrien{ 76390075Sobrien tree r; 764169689Skan bool no_warning; 76590075Sobrien 766169689Skan expr = check_return_expr (expr, &no_warning); 767169689Skan 768169689Skan if (flag_openmp && !check_omp_return ()) 769169689Skan return error_mark_node; 77090075Sobrien if (!processing_template_decl) 77190075Sobrien { 772169689Skan if (DECL_DESTRUCTOR_P (current_function_decl) 773169689Skan || (DECL_CONSTRUCTOR_P (current_function_decl) 774169689Skan && targetm.cxx.cdtor_returns_this ())) 77590075Sobrien { 77690075Sobrien /* Similarly, all destructors must run destructors for 77790075Sobrien base-classes before returning. So, all returns in a 77890075Sobrien destructor get sent to the DTOR_LABEL; finish_function emits 77990075Sobrien code to return a value there. */ 780169689Skan return finish_goto_stmt (cdtor_label); 78190075Sobrien } 78290075Sobrien } 783169689Skan 784169689Skan r = build_stmt (RETURN_EXPR, expr); 785169689Skan TREE_NO_WARNING (r) |= no_warning; 786169689Skan r = maybe_cleanup_point_expr_void (r); 787169689Skan r = add_stmt (r); 78850397Sobrien finish_stmt (); 78990075Sobrien 79090075Sobrien return r; 79150397Sobrien} 79250397Sobrien 79350397Sobrien/* Begin a for-statement. Returns a new FOR_STMT if appropriate. */ 79450397Sobrien 79550397Sobrientree 796132718Skanbegin_for_stmt (void) 79750397Sobrien{ 79850397Sobrien tree r; 79950397Sobrien 800169689Skan r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE, 80190075Sobrien NULL_TREE, NULL_TREE); 80250397Sobrien 803169689Skan if (flag_new_for_scope > 0) 804169689Skan TREE_CHAIN (r) = do_pushlevel (sk_for); 805169689Skan 806169689Skan if (processing_template_decl) 807169689Skan FOR_INIT_STMT (r) = push_stmt_list (); 808169689Skan 80950397Sobrien return r; 81050397Sobrien} 81150397Sobrien 81250397Sobrien/* Finish the for-init-statement of a for-statement, which may be 81350397Sobrien given by FOR_STMT. */ 81450397Sobrien 81550397Sobrienvoid 816132718Skanfinish_for_init_stmt (tree for_stmt) 81750397Sobrien{ 818169689Skan if (processing_template_decl) 819169689Skan FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt)); 820169689Skan add_stmt (for_stmt); 821169689Skan FOR_BODY (for_stmt) = do_pushlevel (sk_block); 822169689Skan begin_cond (&FOR_COND (for_stmt)); 82350397Sobrien} 82450397Sobrien 82550397Sobrien/* Finish the COND of a for-statement, which may be given by 82650397Sobrien FOR_STMT. */ 82750397Sobrien 82850397Sobrienvoid 829132718Skanfinish_for_cond (tree cond, tree for_stmt) 83050397Sobrien{ 831169689Skan finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond)); 832169689Skan simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt)); 83350397Sobrien} 83450397Sobrien 83550397Sobrien/* Finish the increment-EXPRESSION in a for-statement, which may be 83650397Sobrien given by FOR_STMT. */ 83750397Sobrien 83850397Sobrienvoid 839132718Skanfinish_for_expr (tree expr, tree for_stmt) 84050397Sobrien{ 841169689Skan if (!expr) 842169689Skan return; 843132718Skan /* If EXPR is an overloaded function, issue an error; there is no 844132718Skan context available to use to perform overload resolution. */ 845169689Skan if (type_unknown_p (expr)) 846132718Skan { 847132718Skan cxx_incomplete_type_error (expr, TREE_TYPE (expr)); 848132718Skan expr = error_mark_node; 849132718Skan } 850169689Skan if (!processing_template_decl) 851169689Skan { 852169689Skan if (warn_sequence_point) 853169689Skan verify_sequence_points (expr); 854169689Skan expr = convert_to_void (expr, "3rd expression in for"); 855169689Skan } 856169689Skan else if (!type_dependent_expression_p (expr)) 857169689Skan convert_to_void (build_non_dependent_expr (expr), "3rd expression in for"); 858169689Skan expr = maybe_cleanup_point_expr_void (expr); 85990075Sobrien FOR_EXPR (for_stmt) = expr; 86050397Sobrien} 86150397Sobrien 86250397Sobrien/* Finish the body of a for-statement, which may be given by 86350397Sobrien FOR_STMT. The increment-EXPR for the loop must be 86450397Sobrien provided. */ 86550397Sobrien 86650397Sobrienvoid 867132718Skanfinish_for_stmt (tree for_stmt) 86850397Sobrien{ 869169689Skan FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt)); 870169689Skan 87150397Sobrien /* Pop the scope for the body of the loop. */ 872169689Skan if (flag_new_for_scope > 0) 873169689Skan { 874169689Skan tree scope = TREE_CHAIN (for_stmt); 875169689Skan TREE_CHAIN (for_stmt) = NULL; 876169689Skan add_stmt (do_poplevel (scope)); 877169689Skan } 878169689Skan 879169689Skan finish_stmt (); 88050397Sobrien} 88150397Sobrien 88250397Sobrien/* Finish a break-statement. */ 88350397Sobrien 88490075Sobrientree 885132718Skanfinish_break_stmt (void) 88650397Sobrien{ 887169689Skan return add_stmt (build_stmt (BREAK_STMT)); 88850397Sobrien} 88950397Sobrien 89050397Sobrien/* Finish a continue-statement. */ 89150397Sobrien 89290075Sobrientree 893132718Skanfinish_continue_stmt (void) 89450397Sobrien{ 895169689Skan return add_stmt (build_stmt (CONTINUE_STMT)); 89650397Sobrien} 89750397Sobrien 89890075Sobrien/* Begin a switch-statement. Returns a new SWITCH_STMT if 89990075Sobrien appropriate. */ 90050397Sobrien 90190075Sobrientree 902132718Skanbegin_switch_stmt (void) 90350397Sobrien{ 904169689Skan tree r, scope; 905169689Skan 90696263Sobrien r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE); 907169689Skan 908169689Skan scope = do_pushlevel (sk_block); 909169689Skan TREE_CHAIN (r) = scope; 910169689Skan begin_cond (&SWITCH_STMT_COND (r)); 911169689Skan 91290075Sobrien return r; 91350397Sobrien} 91450397Sobrien 91590075Sobrien/* Finish the cond of a switch-statement. */ 91650397Sobrien 91790075Sobrienvoid 918132718Skanfinish_switch_cond (tree cond, tree switch_stmt) 91950397Sobrien{ 92096263Sobrien tree orig_type = NULL; 92190075Sobrien if (!processing_template_decl) 92250397Sobrien { 92390075Sobrien tree index; 92452284Sobrien 92590075Sobrien /* Convert the condition to an integer or enumeration type. */ 926132718Skan cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true); 92790075Sobrien if (cond == NULL_TREE) 92890075Sobrien { 92990075Sobrien error ("switch quantity not an integer"); 93090075Sobrien cond = error_mark_node; 93190075Sobrien } 93296263Sobrien orig_type = TREE_TYPE (cond); 93390075Sobrien if (cond != error_mark_node) 93490075Sobrien { 935132718Skan /* [stmt.switch] 936132718Skan 937132718Skan Integral promotions are performed. */ 938132718Skan cond = perform_integral_promotions (cond); 939169689Skan cond = maybe_cleanup_point_expr (cond); 94090075Sobrien } 94150397Sobrien 94296263Sobrien if (cond != error_mark_node) 94396263Sobrien { 94496263Sobrien index = get_unwidened (cond, NULL_TREE); 94596263Sobrien /* We can't strip a conversion from a signed type to an unsigned, 94696263Sobrien because if we did, int_fits_type_p would do the wrong thing 94796263Sobrien when checking case values for being in range, 94896263Sobrien and it's too hard to do the right thing. */ 949169689Skan if (TYPE_UNSIGNED (TREE_TYPE (cond)) 950169689Skan == TYPE_UNSIGNED (TREE_TYPE (index))) 95196263Sobrien cond = index; 95296263Sobrien } 95390075Sobrien } 954169689Skan finish_cond (&SWITCH_STMT_COND (switch_stmt), cond); 955169689Skan SWITCH_STMT_TYPE (switch_stmt) = orig_type; 956169689Skan add_stmt (switch_stmt); 95790075Sobrien push_switch (switch_stmt); 958169689Skan SWITCH_STMT_BODY (switch_stmt) = push_stmt_list (); 95950397Sobrien} 96050397Sobrien 96150397Sobrien/* Finish the body of a switch-statement, which may be given by 96250397Sobrien SWITCH_STMT. The COND to switch on is indicated. */ 96350397Sobrien 96450397Sobrienvoid 965132718Skanfinish_switch_stmt (tree switch_stmt) 96650397Sobrien{ 967169689Skan tree scope; 968169689Skan 969169689Skan SWITCH_STMT_BODY (switch_stmt) = 970169689Skan pop_stmt_list (SWITCH_STMT_BODY (switch_stmt)); 971169689Skan pop_switch (); 972117395Skan finish_stmt (); 97350397Sobrien 974169689Skan scope = TREE_CHAIN (switch_stmt); 975169689Skan TREE_CHAIN (switch_stmt) = NULL; 976169689Skan add_stmt (do_poplevel (scope)); 97750397Sobrien} 97850397Sobrien 97950397Sobrien/* Begin a try-block. Returns a newly-created TRY_BLOCK if 98050397Sobrien appropriate. */ 98150397Sobrien 98250397Sobrientree 983132718Skanbegin_try_block (void) 98450397Sobrien{ 98590075Sobrien tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE); 98690075Sobrien add_stmt (r); 987169689Skan TRY_STMTS (r) = push_stmt_list (); 98890075Sobrien return r; 98950397Sobrien} 99050397Sobrien 991169689Skan/* Likewise, for a function-try-block. The block returned in 992169689Skan *COMPOUND_STMT is an artificial outer scope, containing the 993169689Skan function-try-block. */ 99490075Sobrien 99590075Sobrientree 996169689Skanbegin_function_try_block (tree *compound_stmt) 99790075Sobrien{ 998169689Skan tree r; 999169689Skan /* This outer scope does not exist in the C++ standard, but we need 1000169689Skan a place to put __FUNCTION__ and similar variables. */ 1001169689Skan *compound_stmt = begin_compound_stmt (0); 1002169689Skan r = begin_try_block (); 100390075Sobrien FN_TRY_BLOCK_P (r) = 1; 100490075Sobrien return r; 100590075Sobrien} 100690075Sobrien 100750397Sobrien/* Finish a try-block, which may be given by TRY_BLOCK. */ 100850397Sobrien 100950397Sobrienvoid 1010132718Skanfinish_try_block (tree try_block) 101150397Sobrien{ 1012169689Skan TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); 1013169689Skan TRY_HANDLERS (try_block) = push_stmt_list (); 101490075Sobrien} 101590075Sobrien 101690075Sobrien/* Finish the body of a cleanup try-block, which may be given by 101790075Sobrien TRY_BLOCK. */ 101890075Sobrien 101990075Sobrienvoid 1020132718Skanfinish_cleanup_try_block (tree try_block) 102190075Sobrien{ 1022169689Skan TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block)); 102390075Sobrien} 102490075Sobrien 102590075Sobrien/* Finish an implicitly generated try-block, with a cleanup is given 102690075Sobrien by CLEANUP. */ 102790075Sobrien 102890075Sobrienvoid 1029132718Skanfinish_cleanup (tree cleanup, tree try_block) 103090075Sobrien{ 103190075Sobrien TRY_HANDLERS (try_block) = cleanup; 103290075Sobrien CLEANUP_P (try_block) = 1; 103390075Sobrien} 103490075Sobrien 103590075Sobrien/* Likewise, for a function-try-block. */ 103690075Sobrien 103790075Sobrienvoid 1038132718Skanfinish_function_try_block (tree try_block) 103990075Sobrien{ 1040169689Skan finish_try_block (try_block); 1041169689Skan /* FIXME : something queer about CTOR_INITIALIZER somehow following 1042169689Skan the try block, but moving it inside. */ 104390075Sobrien in_function_try_handler = 1; 104450397Sobrien} 104550397Sobrien 104650397Sobrien/* Finish a handler-sequence for a try-block, which may be given by 104750397Sobrien TRY_BLOCK. */ 104850397Sobrien 104950397Sobrienvoid 1050132718Skanfinish_handler_sequence (tree try_block) 105150397Sobrien{ 1052169689Skan TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block)); 105390075Sobrien check_handlers (TRY_HANDLERS (try_block)); 105450397Sobrien} 105550397Sobrien 1056169689Skan/* Finish the handler-seq for a function-try-block, given by 1057169689Skan TRY_BLOCK. COMPOUND_STMT is the outer block created by 1058169689Skan begin_function_try_block. */ 105990075Sobrien 106090075Sobrienvoid 1061169689Skanfinish_function_handler_sequence (tree try_block, tree compound_stmt) 106290075Sobrien{ 106390075Sobrien in_function_try_handler = 0; 1064169689Skan finish_handler_sequence (try_block); 1065169689Skan finish_compound_stmt (compound_stmt); 106690075Sobrien} 106790075Sobrien 106850397Sobrien/* Begin a handler. Returns a HANDLER if appropriate. */ 106950397Sobrien 107050397Sobrientree 1071132718Skanbegin_handler (void) 107250397Sobrien{ 107350397Sobrien tree r; 1074169689Skan 107590075Sobrien r = build_stmt (HANDLER, NULL_TREE, NULL_TREE); 107690075Sobrien add_stmt (r); 1077169689Skan 107890075Sobrien /* Create a binding level for the eh_info and the exception object 107990075Sobrien cleanup. */ 1080169689Skan HANDLER_BODY (r) = do_pushlevel (sk_catch); 1081169689Skan 108250397Sobrien return r; 108350397Sobrien} 108450397Sobrien 108550397Sobrien/* Finish the handler-parameters for a handler, which may be given by 108690075Sobrien HANDLER. DECL is the declaration for the catch parameter, or NULL 108790075Sobrien if this is a `catch (...)' clause. */ 108850397Sobrien 108950397Sobrienvoid 1090132718Skanfinish_handler_parms (tree decl, tree handler) 109150397Sobrien{ 109290075Sobrien tree type = NULL_TREE; 109350397Sobrien if (processing_template_decl) 109490075Sobrien { 109590075Sobrien if (decl) 109690075Sobrien { 109790075Sobrien decl = pushdecl (decl); 109890075Sobrien decl = push_template_decl (decl); 1099169689Skan HANDLER_PARMS (handler) = decl; 110090075Sobrien type = TREE_TYPE (decl); 110190075Sobrien } 110290075Sobrien } 110390075Sobrien else 110490075Sobrien type = expand_start_catch_block (decl); 110590075Sobrien HANDLER_TYPE (handler) = type; 1106132718Skan if (!processing_template_decl && type) 1107132718Skan mark_used (eh_type_info (type)); 110850397Sobrien} 110950397Sobrien 111090075Sobrien/* Finish a handler, which may be given by HANDLER. The BLOCKs are 111190075Sobrien the return value from the matching call to finish_handler_parms. */ 111250397Sobrien 111350397Sobrienvoid 1114132718Skanfinish_handler (tree handler) 111550397Sobrien{ 111690075Sobrien if (!processing_template_decl) 111750397Sobrien expand_end_catch_block (); 1118169689Skan HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler)); 111950397Sobrien} 112050397Sobrien 1121169689Skan/* Begin a compound statement. FLAGS contains some bits that control the 1122169689Skan behavior and context. If BCS_NO_SCOPE is set, the compound statement 1123169689Skan does not define a scope. If BCS_FN_BODY is set, this is the outermost 1124169689Skan block of a function. If BCS_TRY_BLOCK is set, this is the block 1125169689Skan created on behalf of a TRY statement. Returns a token to be passed to 1126169689Skan finish_compound_stmt. */ 112750397Sobrien 112850397Sobrientree 1129169689Skanbegin_compound_stmt (unsigned int flags) 113050397Sobrien{ 1131169689Skan tree r; 113250397Sobrien 1133169689Skan if (flags & BCS_NO_SCOPE) 1134169689Skan { 1135169689Skan r = push_stmt_list (); 1136169689Skan STATEMENT_LIST_NO_SCOPE (r) = 1; 113790075Sobrien 1138169689Skan /* Normally, we try hard to keep the BLOCK for a statement-expression. 1139169689Skan But, if it's a statement-expression with a scopeless block, there's 1140169689Skan nothing to keep, and we don't want to accidentally keep a block 1141169689Skan *inside* the scopeless block. */ 1142169689Skan keep_next_level (false); 1143169689Skan } 1144169689Skan else 1145169689Skan r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block); 114690075Sobrien 1147169689Skan /* When processing a template, we need to remember where the braces were, 1148169689Skan so that we can set up identical scopes when instantiating the template 1149169689Skan later. BIND_EXPR is a handy candidate for this. 1150169689Skan Note that do_poplevel won't create a BIND_EXPR itself here (and thus 1151169689Skan result in nested BIND_EXPRs), since we don't build BLOCK nodes when 1152169689Skan processing templates. */ 1153169689Skan if (processing_template_decl) 1154169689Skan { 1155169689Skan r = build3 (BIND_EXPR, NULL, NULL, r, NULL); 1156169689Skan BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0; 1157169689Skan BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0; 1158169689Skan TREE_SIDE_EFFECTS (r) = 1; 1159169689Skan } 116090075Sobrien 116150397Sobrien return r; 116250397Sobrien} 116350397Sobrien 1164169689Skan/* Finish a compound-statement, which is given by STMT. */ 116550397Sobrien 1166169689Skanvoid 1167169689Skanfinish_compound_stmt (tree stmt) 116850397Sobrien{ 1169169689Skan if (TREE_CODE (stmt) == BIND_EXPR) 1170169689Skan BIND_EXPR_BODY (stmt) = do_poplevel (BIND_EXPR_BODY (stmt)); 1171169689Skan else if (STATEMENT_LIST_NO_SCOPE (stmt)) 1172169689Skan stmt = pop_stmt_list (stmt); 1173132718Skan else 1174169689Skan { 1175169689Skan /* Destroy any ObjC "super" receivers that may have been 1176169689Skan created. */ 1177169689Skan objc_clear_super_receiver (); 117850397Sobrien 1179169689Skan stmt = do_poplevel (stmt); 1180169689Skan } 118150397Sobrien 1182169689Skan /* ??? See c_end_compound_stmt wrt statement expressions. */ 1183169689Skan add_stmt (stmt); 118450397Sobrien finish_stmt (); 118550397Sobrien} 118650397Sobrien 1187169689Skan/* Finish an asm-statement, whose components are a STRING, some 1188169689Skan OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note 1189169689Skan whether the asm-statement should be considered volatile. */ 119050397Sobrien 119190075Sobrientree 1192169689Skanfinish_asm_stmt (int volatile_p, tree string, tree output_operands, 1193169689Skan tree input_operands, tree clobbers) 119450397Sobrien{ 119590075Sobrien tree r; 119690075Sobrien tree t; 1197169689Skan int ninputs = list_length (input_operands); 1198169689Skan int noutputs = list_length (output_operands); 119990075Sobrien 120090075Sobrien if (!processing_template_decl) 120150397Sobrien { 1202169689Skan const char *constraint; 1203169689Skan const char **oconstraints; 1204169689Skan bool allows_mem, allows_reg, is_inout; 1205169689Skan tree operand; 120690075Sobrien int i; 120790075Sobrien 1208169689Skan oconstraints = (const char **) alloca (noutputs * sizeof (char *)); 1209169689Skan 1210169689Skan string = resolve_asm_operand_names (string, output_operands, 1211169689Skan input_operands); 1212169689Skan 1213169689Skan for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i) 121450397Sobrien { 1215169689Skan operand = TREE_VALUE (t); 1216169689Skan 1217169689Skan /* ??? Really, this should not be here. Users should be using a 1218169689Skan proper lvalue, dammit. But there's a long history of using 1219169689Skan casts in the output operands. In cases like longlong.h, this 1220169689Skan becomes a primitive form of typechecking -- if the cast can be 1221169689Skan removed, then the output operand had a type of the proper width; 1222169689Skan otherwise we'll get an error. Gross, but ... */ 1223169689Skan STRIP_NOPS (operand); 1224169689Skan 1225169689Skan if (!lvalue_or_else (operand, lv_asm)) 1226169689Skan operand = error_mark_node; 1227169689Skan 1228169689Skan if (operand != error_mark_node 1229169689Skan && (TREE_READONLY (operand) 1230169689Skan || CP_TYPE_CONST_P (TREE_TYPE (operand)) 1231169689Skan /* Functions are not modifiable, even though they are 1232169689Skan lvalues. */ 1233169689Skan || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE 1234169689Skan || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE 1235169689Skan /* If it's an aggregate and any field is const, then it is 1236169689Skan effectively const. */ 1237169689Skan || (CLASS_TYPE_P (TREE_TYPE (operand)) 1238169689Skan && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand))))) 1239169689Skan readonly_error (operand, "assignment (via 'asm' output)", 0); 1240169689Skan 1241169689Skan constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); 1242169689Skan oconstraints[i] = constraint; 1243169689Skan 1244169689Skan if (parse_output_constraint (&constraint, i, ninputs, noutputs, 1245169689Skan &allows_mem, &allows_reg, &is_inout)) 124690075Sobrien { 1247169689Skan /* If the operand is going to end up in memory, 1248169689Skan mark it addressable. */ 1249169689Skan if (!allows_reg && !cxx_mark_addressable (operand)) 1250169689Skan operand = error_mark_node; 125190075Sobrien } 1252169689Skan else 1253169689Skan operand = error_mark_node; 1254169689Skan 1255169689Skan TREE_VALUE (t) = operand; 125690075Sobrien } 125752284Sobrien 1258169689Skan for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t)) 125990075Sobrien { 126090075Sobrien constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))); 1261169689Skan operand = decay_conversion (TREE_VALUE (t)); 126290075Sobrien 1263169689Skan /* If the type of the operand hasn't been determined (e.g., 1264169689Skan because it involves an overloaded function), then issue 1265169689Skan an error message. There's no context available to 1266169689Skan resolve the overloading. */ 1267169689Skan if (TREE_TYPE (operand) == unknown_type_node) 126890075Sobrien { 1269169689Skan error ("type of asm operand %qE could not be determined", 1270169689Skan TREE_VALUE (t)); 1271169689Skan operand = error_mark_node; 127290075Sobrien } 127390075Sobrien 1274169689Skan if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0, 1275169689Skan oconstraints, &allows_mem, &allows_reg)) 1276169689Skan { 1277169689Skan /* If the operand is going to end up in memory, 1278169689Skan mark it addressable. */ 1279169689Skan if (!allows_reg && allows_mem) 1280169689Skan { 1281169689Skan /* Strip the nops as we allow this case. FIXME, this really 1282169689Skan should be rejected or made deprecated. */ 1283169689Skan STRIP_NOPS (operand); 1284169689Skan if (!cxx_mark_addressable (operand)) 1285169689Skan operand = error_mark_node; 1286169689Skan } 1287169689Skan } 1288169689Skan else 1289169689Skan operand = error_mark_node; 1290169689Skan 1291169689Skan TREE_VALUE (t) = operand; 129250397Sobrien } 129390075Sobrien } 129490075Sobrien 1295169689Skan r = build_stmt (ASM_EXPR, string, 129690075Sobrien output_operands, input_operands, 129790075Sobrien clobbers); 1298169689Skan ASM_VOLATILE_P (r) = volatile_p || noutputs == 0; 1299169689Skan r = maybe_cleanup_point_expr_void (r); 130090075Sobrien return add_stmt (r); 130190075Sobrien} 130290075Sobrien 130390075Sobrien/* Finish a label with the indicated NAME. */ 130490075Sobrien 1305132718Skantree 1306132718Skanfinish_label_stmt (tree name) 130790075Sobrien{ 1308132718Skan tree decl = define_label (input_location, name); 1309169689Skan 1310169689Skan if (decl == error_mark_node) 1311169689Skan return error_mark_node; 1312169689Skan 1313169689Skan return add_stmt (build_stmt (LABEL_EXPR, decl)); 131490075Sobrien} 131590075Sobrien 131690075Sobrien/* Finish a series of declarations for local labels. G++ allows users 131790075Sobrien to declare "local" labels, i.e., labels with scope. This extension 131890075Sobrien is useful when writing code involving statement-expressions. */ 131990075Sobrien 132090075Sobrienvoid 1321132718Skanfinish_label_decl (tree name) 132290075Sobrien{ 1323220150Smm if (!at_function_scope_p ()) 1324220150Smm { 1325220150Smm error ("__label__ declarations are only allowed in function scopes"); 1326220150Smm return; 1327220150Smm } 1328220150Smm 1329220150Smm add_decl_expr (declare_local_label (name)); 133090075Sobrien} 133190075Sobrien 1332117395Skan/* When DECL goes out of scope, make sure that CLEANUP is executed. */ 133390075Sobrien 1334169689Skanvoid 1335132718Skanfinish_decl_cleanup (tree decl, tree cleanup) 133690075Sobrien{ 1337169689Skan push_cleanup (decl, cleanup, false); 133890075Sobrien} 133990075Sobrien 1340117395Skan/* If the current scope exits with an exception, run CLEANUP. */ 134190075Sobrien 1342117395Skanvoid 1343132718Skanfinish_eh_cleanup (tree cleanup) 134490075Sobrien{ 1345169689Skan push_cleanup (NULL, cleanup, true); 134690075Sobrien} 134790075Sobrien 1348117395Skan/* The MEM_INITS is a list of mem-initializers, in reverse of the 1349117395Skan order they were written by the user. Each node is as for 1350117395Skan emit_mem_initializers. */ 135190075Sobrien 1352117395Skanvoid 1353117395Skanfinish_mem_initializers (tree mem_inits) 1354117395Skan{ 1355117395Skan /* Reorder the MEM_INITS so that they are in the order they appeared 1356117395Skan in the source program. */ 1357117395Skan mem_inits = nreverse (mem_inits); 135890075Sobrien 135990075Sobrien if (processing_template_decl) 1360117395Skan add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits)); 136190075Sobrien else 1362117395Skan emit_mem_initializers (mem_inits); 136390075Sobrien} 136490075Sobrien 136550397Sobrien/* Finish a parenthesized expression EXPR. */ 136650397Sobrien 136750397Sobrientree 1368132718Skanfinish_parenthesized_expr (tree expr) 136950397Sobrien{ 1370169689Skan if (EXPR_P (expr)) 1371117395Skan /* This inhibits warnings in c_common_truthvalue_conversion. */ 1372169689Skan TREE_NO_WARNING (expr) = 1; 137350397Sobrien 137490075Sobrien if (TREE_CODE (expr) == OFFSET_REF) 137590075Sobrien /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be 137690075Sobrien enclosed in parentheses. */ 137790075Sobrien PTRMEM_OK_P (expr) = 0; 1378169689Skan 1379169689Skan if (TREE_CODE (expr) == STRING_CST) 1380169689Skan PAREN_STRING_LITERAL_P (expr) = 1; 1381169689Skan 138250397Sobrien return expr; 138350397Sobrien} 138450397Sobrien 1385132718Skan/* Finish a reference to a non-static data member (DECL) that is not 1386132718Skan preceded by `.' or `->'. */ 1387132718Skan 1388132718Skantree 1389132718Skanfinish_non_static_data_member (tree decl, tree object, tree qualifying_scope) 1390132718Skan{ 1391169689Skan gcc_assert (TREE_CODE (decl) == FIELD_DECL); 1392132718Skan 1393132718Skan if (!object) 1394132718Skan { 1395169689Skan if (current_function_decl 1396132718Skan && DECL_STATIC_FUNCTION_P (current_function_decl)) 1397169689Skan error ("invalid use of member %q+D in static member function", decl); 1398132718Skan else 1399169689Skan error ("invalid use of non-static data member %q+D", decl); 1400132718Skan error ("from this location"); 1401132718Skan 1402132718Skan return error_mark_node; 1403132718Skan } 1404132718Skan TREE_USED (current_class_ptr) = 1; 1405132718Skan if (processing_template_decl && !qualifying_scope) 1406132718Skan { 1407132718Skan tree type = TREE_TYPE (decl); 1408132718Skan 1409132718Skan if (TREE_CODE (type) == REFERENCE_TYPE) 1410132718Skan type = TREE_TYPE (type); 1411132718Skan else 1412132718Skan { 1413132718Skan /* Set the cv qualifiers. */ 1414132718Skan int quals = cp_type_quals (TREE_TYPE (current_class_ref)); 1415169689Skan 1416132718Skan if (DECL_MUTABLE_P (decl)) 1417132718Skan quals &= ~TYPE_QUAL_CONST; 1418132718Skan 1419132718Skan quals |= cp_type_quals (TREE_TYPE (decl)); 1420132718Skan type = cp_build_qualified_type (type, quals); 1421132718Skan } 1422169689Skan 1423169689Skan return build_min (COMPONENT_REF, type, object, decl, NULL_TREE); 1424132718Skan } 1425132718Skan else 1426132718Skan { 1427132718Skan tree access_type = TREE_TYPE (object); 1428132718Skan tree lookup_context = context_for_name_lookup (decl); 1429169689Skan 1430132718Skan while (!DERIVED_FROM_P (lookup_context, access_type)) 1431132718Skan { 1432132718Skan access_type = TYPE_CONTEXT (access_type); 1433132718Skan while (access_type && DECL_P (access_type)) 1434132718Skan access_type = DECL_CONTEXT (access_type); 1435132718Skan 1436132718Skan if (!access_type) 1437132718Skan { 1438169689Skan error ("object missing in reference to %q+D", decl); 1439132718Skan error ("from this location"); 1440132718Skan return error_mark_node; 1441132718Skan } 1442132718Skan } 1443132718Skan 1444132718Skan /* If PROCESSING_TEMPLATE_DECL is nonzero here, then 1445132718Skan QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF 1446132718Skan for now. */ 1447132718Skan if (processing_template_decl) 1448169689Skan return build_qualified_name (TREE_TYPE (decl), 1449169689Skan qualifying_scope, 1450169689Skan DECL_NAME (decl), 1451169689Skan /*template_p=*/false); 1452132718Skan 1453169689Skan perform_or_defer_access_check (TYPE_BINFO (access_type), decl, 1454169689Skan decl); 1455132718Skan 1456132718Skan /* If the data member was named `C::M', convert `*this' to `C' 1457132718Skan first. */ 1458132718Skan if (qualifying_scope) 1459132718Skan { 1460132718Skan tree binfo = NULL_TREE; 1461132718Skan object = build_scoped_ref (object, qualifying_scope, 1462132718Skan &binfo); 1463132718Skan } 1464132718Skan 1465132718Skan return build_class_member_access_expr (object, decl, 1466132718Skan /*access_path=*/NULL_TREE, 1467132718Skan /*preserve_reference=*/false); 1468132718Skan } 1469132718Skan} 1470132718Skan 1471132718Skan/* DECL was the declaration to which a qualified-id resolved. Issue 1472132718Skan an error message if it is not accessible. If OBJECT_TYPE is 1473132718Skan non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the 1474132718Skan type of `*x', or `x', respectively. If the DECL was named as 1475132718Skan `A::B' then NESTED_NAME_SPECIFIER is `A'. */ 1476132718Skan 1477132718Skanvoid 1478169689Skancheck_accessibility_of_qualified_id (tree decl, 1479169689Skan tree object_type, 1480132718Skan tree nested_name_specifier) 1481132718Skan{ 1482132718Skan tree scope; 1483132718Skan tree qualifying_type = NULL_TREE; 1484169689Skan 1485169689Skan /* If we're not checking, return immediately. */ 1486169689Skan if (deferred_access_no_check) 1487169689Skan return; 1488169689Skan 1489132718Skan /* Determine the SCOPE of DECL. */ 1490132718Skan scope = context_for_name_lookup (decl); 1491132718Skan /* If the SCOPE is not a type, then DECL is not a member. */ 1492132718Skan if (!TYPE_P (scope)) 1493132718Skan return; 1494132718Skan /* Compute the scope through which DECL is being accessed. */ 1495169689Skan if (object_type 1496132718Skan /* OBJECT_TYPE might not be a class type; consider: 1497132718Skan 1498132718Skan class A { typedef int I; }; 1499132718Skan I *p; 1500132718Skan p->A::I::~I(); 1501132718Skan 1502169689Skan In this case, we will have "A::I" as the DECL, but "I" as the 1503132718Skan OBJECT_TYPE. */ 1504132718Skan && CLASS_TYPE_P (object_type) 1505132718Skan && DERIVED_FROM_P (scope, object_type)) 1506132718Skan /* If we are processing a `->' or `.' expression, use the type of the 1507132718Skan left-hand side. */ 1508132718Skan qualifying_type = object_type; 1509132718Skan else if (nested_name_specifier) 1510132718Skan { 1511132718Skan /* If the reference is to a non-static member of the 1512132718Skan current class, treat it as if it were referenced through 1513132718Skan `this'. */ 1514132718Skan if (DECL_NONSTATIC_MEMBER_P (decl) 1515132718Skan && current_class_ptr 1516132718Skan && DERIVED_FROM_P (scope, current_class_type)) 1517132718Skan qualifying_type = current_class_type; 1518132718Skan /* Otherwise, use the type indicated by the 1519132718Skan nested-name-specifier. */ 1520132718Skan else 1521132718Skan qualifying_type = nested_name_specifier; 1522132718Skan } 1523132718Skan else 1524132718Skan /* Otherwise, the name must be from the current class or one of 1525132718Skan its bases. */ 1526132718Skan qualifying_type = currently_open_derived_class (scope); 1527132718Skan 1528169689Skan if (qualifying_type 1529169689Skan /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM 1530169689Skan or similar in a default argument value. */ 1531169689Skan && CLASS_TYPE_P (qualifying_type) 1532169689Skan && !dependent_type_p (qualifying_type)) 1533169689Skan perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl, 1534169689Skan decl); 1535132718Skan} 1536132718Skan 1537132718Skan/* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the 1538132718Skan class named to the left of the "::" operator. DONE is true if this 1539132718Skan expression is a complete postfix-expression; it is false if this 1540132718Skan expression is followed by '->', '[', '(', etc. ADDRESS_P is true 1541169689Skan iff this expression is the operand of '&'. TEMPLATE_P is true iff 1542169689Skan the qualified-id was of the form "A::template B". TEMPLATE_ARG_P 1543169689Skan is true iff this qualified name appears as a template argument. */ 1544132718Skan 1545132718Skantree 1546169689Skanfinish_qualified_id_expr (tree qualifying_class, 1547169689Skan tree expr, 1548169689Skan bool done, 1549169689Skan bool address_p, 1550169689Skan bool template_p, 1551169689Skan bool template_arg_p) 1552132718Skan{ 1553169689Skan gcc_assert (TYPE_P (qualifying_class)); 1554169689Skan 1555132718Skan if (error_operand_p (expr)) 1556132718Skan return error_mark_node; 1557132718Skan 1558169689Skan if (DECL_P (expr) || BASELINK_P (expr)) 1559169689Skan mark_used (expr); 1560169689Skan 1561169689Skan if (template_p) 1562169689Skan check_template_keyword (expr); 1563169689Skan 1564132718Skan /* If EXPR occurs as the operand of '&', use special handling that 1565132718Skan permits a pointer-to-member. */ 1566132718Skan if (address_p && done) 1567132718Skan { 1568132718Skan if (TREE_CODE (expr) == SCOPE_REF) 1569132718Skan expr = TREE_OPERAND (expr, 1); 1570169689Skan expr = build_offset_ref (qualifying_class, expr, 1571132718Skan /*address_p=*/true); 1572132718Skan return expr; 1573132718Skan } 1574132718Skan 1575169689Skan /* Within the scope of a class, turn references to non-static 1576169689Skan members into expression of the form "this->...". */ 1577169689Skan if (template_arg_p) 1578169689Skan /* But, within a template argument, we do not want make the 1579169689Skan transformation, as there is no "this" pointer. */ 1580169689Skan ; 1581169689Skan else if (TREE_CODE (expr) == FIELD_DECL) 1582132718Skan expr = finish_non_static_data_member (expr, current_class_ref, 1583132718Skan qualifying_class); 1584132718Skan else if (BASELINK_P (expr) && !processing_template_decl) 1585132718Skan { 1586132718Skan tree fns; 1587132718Skan 1588132718Skan /* See if any of the functions are non-static members. */ 1589132718Skan fns = BASELINK_FUNCTIONS (expr); 1590132718Skan if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 1591132718Skan fns = TREE_OPERAND (fns, 0); 1592132718Skan /* If so, the expression may be relative to the current 1593132718Skan class. */ 1594146895Skan if (!shared_member_p (fns) 1595169689Skan && current_class_type 1596132718Skan && DERIVED_FROM_P (qualifying_class, current_class_type)) 1597169689Skan expr = (build_class_member_access_expr 1598132718Skan (maybe_dummy_object (qualifying_class, NULL), 1599132718Skan expr, 1600132718Skan BASELINK_ACCESS_BINFO (expr), 1601132718Skan /*preserve_reference=*/false)); 1602132718Skan else if (done) 1603132718Skan /* The expression is a qualified name whose address is not 1604132718Skan being taken. */ 1605132718Skan expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false); 1606132718Skan } 1607132718Skan 1608132718Skan return expr; 1609132718Skan} 1610132718Skan 161150397Sobrien/* Begin a statement-expression. The value returned must be passed to 161250397Sobrien finish_stmt_expr. */ 161350397Sobrien 1614169689Skantree 1615132718Skanbegin_stmt_expr (void) 161650397Sobrien{ 1617169689Skan return push_stmt_list (); 161850397Sobrien} 161950397Sobrien 1620132718Skan/* Process the final expression of a statement expression. EXPR can be 1621169689Skan NULL, if the final expression is empty. Return a STATEMENT_LIST 1622169689Skan containing all the statements in the statement-expression, or 1623169689Skan ERROR_MARK_NODE if there was an error. */ 162490075Sobrien 162590075Sobrientree 1626169689Skanfinish_stmt_expr_expr (tree expr, tree stmt_expr) 162790075Sobrien{ 1628146895Skan if (error_operand_p (expr)) 1629146895Skan return error_mark_node; 1630169689Skan 1631169689Skan /* If the last statement does not have "void" type, then the value 1632169689Skan of the last statement is the value of the entire expression. */ 1633132718Skan if (expr) 1634132718Skan { 1635169689Skan tree type = TREE_TYPE (expr); 1636169689Skan 1637169689Skan if (processing_template_decl) 1638132718Skan { 1639169689Skan expr = build_stmt (EXPR_STMT, expr); 1640169689Skan expr = add_stmt (expr); 1641169689Skan /* Mark the last statement so that we can recognize it as such at 1642169689Skan template-instantiation time. */ 1643169689Skan EXPR_STMT_STMT_EXPR_RESULT (expr) = 1; 1644169689Skan } 1645169689Skan else if (VOID_TYPE_P (type)) 1646169689Skan { 1647169689Skan /* Just treat this like an ordinary statement. */ 1648169689Skan expr = finish_expr_stmt (expr); 1649169689Skan } 1650169689Skan else 1651169689Skan { 1652169689Skan /* It actually has a value we need to deal with. First, force it 1653169689Skan to be an rvalue so that we won't need to build up a copy 1654169689Skan constructor call later when we try to assign it to something. */ 1655169689Skan expr = force_rvalue (expr); 1656169689Skan if (error_operand_p (expr)) 1657169689Skan return error_mark_node; 165890075Sobrien 1659169689Skan /* Update for array-to-pointer decay. */ 1660169689Skan type = TREE_TYPE (expr); 166190075Sobrien 1662169689Skan /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a 1663169689Skan normal statement, but don't convert to void or actually add 1664169689Skan the EXPR_STMT. */ 1665169689Skan if (TREE_CODE (expr) != CLEANUP_POINT_EXPR) 1666169689Skan expr = maybe_cleanup_point_expr (expr); 1667169689Skan add_stmt (expr); 1668132718Skan } 1669132718Skan 1670169689Skan /* The type of the statement-expression is the type of the last 1671169689Skan expression. */ 1672169689Skan TREE_TYPE (stmt_expr) = type; 1673132718Skan } 167490075Sobrien 1675169689Skan return stmt_expr; 167690075Sobrien} 167790075Sobrien 1678132718Skan/* Finish a statement-expression. EXPR should be the value returned 1679132718Skan by the previous begin_stmt_expr. Returns an expression 1680132718Skan representing the statement-expression. */ 168150397Sobrien 1682169689Skantree 1683169689Skanfinish_stmt_expr (tree stmt_expr, bool has_no_scope) 168450397Sobrien{ 1685169689Skan tree type; 168650397Sobrien tree result; 168750397Sobrien 1688169689Skan if (error_operand_p (stmt_expr)) 1689169689Skan return error_mark_node; 169050397Sobrien 1691169689Skan gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST); 1692169689Skan 1693169689Skan type = TREE_TYPE (stmt_expr); 1694169689Skan result = pop_stmt_list (stmt_expr); 1695169689Skan TREE_TYPE (result) = type; 1696169689Skan 1697132718Skan if (processing_template_decl) 1698132718Skan { 1699169689Skan result = build_min (STMT_EXPR, type, result); 1700169689Skan TREE_SIDE_EFFECTS (result) = 1; 1701169689Skan STMT_EXPR_NO_SCOPE (result) = has_no_scope; 1702132718Skan } 1703169689Skan else if (CLASS_TYPE_P (type)) 1704169689Skan { 1705169689Skan /* Wrap the statement-expression in a TARGET_EXPR so that the 1706169689Skan temporary object created by the final expression is destroyed at 1707169689Skan the end of the full-expression containing the 1708169689Skan statement-expression. */ 1709169689Skan result = force_target_expr (type, result); 1710169689Skan } 1711169689Skan 171250397Sobrien return result; 171350397Sobrien} 171450397Sobrien 1715132718Skan/* Perform Koenig lookup. FN is the postfix-expression representing 1716132718Skan the function (or functions) to call; ARGS are the arguments to the 1717132718Skan call. Returns the functions to be considered by overload 1718132718Skan resolution. */ 1719132718Skan 1720132718Skantree 1721132718Skanperform_koenig_lookup (tree fn, tree args) 1722132718Skan{ 1723132718Skan tree identifier = NULL_TREE; 1724132718Skan tree functions = NULL_TREE; 1725132718Skan 1726132718Skan /* Find the name of the overloaded function. */ 1727132718Skan if (TREE_CODE (fn) == IDENTIFIER_NODE) 1728132718Skan identifier = fn; 1729132718Skan else if (is_overloaded_fn (fn)) 1730132718Skan { 1731132718Skan functions = fn; 1732132718Skan identifier = DECL_NAME (get_first_fn (functions)); 1733132718Skan } 1734132718Skan else if (DECL_P (fn)) 1735132718Skan { 1736132718Skan functions = fn; 1737132718Skan identifier = DECL_NAME (fn); 1738132718Skan } 1739132718Skan 1740132718Skan /* A call to a namespace-scope function using an unqualified name. 1741132718Skan 1742132718Skan Do Koenig lookup -- unless any of the arguments are 1743132718Skan type-dependent. */ 1744132718Skan if (!any_type_dependent_arguments_p (args)) 1745132718Skan { 1746132718Skan fn = lookup_arg_dependent (identifier, functions, args); 1747132718Skan if (!fn) 1748132718Skan /* The unqualified name could not be resolved. */ 1749132718Skan fn = unqualified_fn_lookup_error (identifier); 1750132718Skan } 1751132718Skan 1752132718Skan return fn; 1753132718Skan} 1754132718Skan 1755117395Skan/* Generate an expression for `FN (ARGS)'. 175650397Sobrien 1757117395Skan If DISALLOW_VIRTUAL is true, the call to FN will be not generated 1758117395Skan as a virtual call, even if FN is virtual. (This flag is set when 1759117395Skan encountering an expression where the function name is explicitly 1760117395Skan qualified. For example a call to `X::f' never generates a virtual 1761117395Skan call.) 1762117395Skan 1763117395Skan Returns code for the call. */ 1764117395Skan 1765169689Skantree 1766132718Skanfinish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p) 176750397Sobrien{ 1768132718Skan tree result; 1769132718Skan tree orig_fn; 1770132718Skan tree orig_args; 1771132718Skan 1772117395Skan if (fn == error_mark_node || args == error_mark_node) 1773117395Skan return error_mark_node; 177450397Sobrien 1775117395Skan /* ARGS should be a list of arguments. */ 1776169689Skan gcc_assert (!args || TREE_CODE (args) == TREE_LIST); 1777169689Skan gcc_assert (!TYPE_P (fn)); 1778117395Skan 1779132718Skan orig_fn = fn; 1780132718Skan orig_args = args; 1781132718Skan 1782132718Skan if (processing_template_decl) 1783132718Skan { 1784132718Skan if (type_dependent_expression_p (fn) 1785132718Skan || any_type_dependent_arguments_p (args)) 1786132718Skan { 1787169689Skan result = build_nt (CALL_EXPR, fn, args, NULL_TREE); 1788132718Skan KOENIG_LOOKUP_P (result) = koenig_p; 1789132718Skan return result; 1790132718Skan } 1791132718Skan if (!BASELINK_P (fn) 1792132718Skan && TREE_CODE (fn) != PSEUDO_DTOR_EXPR 1793132718Skan && TREE_TYPE (fn) != unknown_type_node) 1794132718Skan fn = build_non_dependent_expr (fn); 1795132718Skan args = build_non_dependent_args (orig_args); 1796132718Skan } 1797132718Skan 1798169689Skan if (is_overloaded_fn (fn)) 1799169689Skan fn = baselink_for_fns (fn); 1800132718Skan 1801132718Skan result = NULL_TREE; 1802117395Skan if (BASELINK_P (fn)) 180350397Sobrien { 1804117395Skan tree object; 1805117395Skan 1806117395Skan /* A call to a member function. From [over.call.func]: 1807117395Skan 1808117395Skan If the keyword this is in scope and refers to the class of 1809117395Skan that member function, or a derived class thereof, then the 1810117395Skan function call is transformed into a qualified function call 1811117395Skan using (*this) as the postfix-expression to the left of the 1812117395Skan . operator.... [Otherwise] a contrived object of type T 1813169689Skan becomes the implied object argument. 1814117395Skan 1815169689Skan This paragraph is unclear about this situation: 1816117395Skan 1817117395Skan struct A { void f(); }; 1818117395Skan struct B : public A {}; 1819117395Skan struct C : public A { void g() { B::f(); }}; 1820117395Skan 1821117395Skan In particular, for `B::f', this paragraph does not make clear 1822169689Skan whether "the class of that member function" refers to `A' or 1823117395Skan to `B'. We believe it refers to `B'. */ 1824169689Skan if (current_class_type 1825117395Skan && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)), 1826117395Skan current_class_type) 1827117395Skan && current_class_ref) 1828117395Skan object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)), 1829117395Skan NULL); 1830117395Skan else 1831117395Skan { 1832117395Skan tree representative_fn; 1833117395Skan 1834117395Skan representative_fn = BASELINK_FUNCTIONS (fn); 1835117395Skan if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR) 1836117395Skan representative_fn = TREE_OPERAND (representative_fn, 0); 1837117395Skan representative_fn = get_first_fn (representative_fn); 1838117395Skan object = build_dummy_object (DECL_CONTEXT (representative_fn)); 1839117395Skan } 1840117395Skan 1841132718Skan if (processing_template_decl) 1842132718Skan { 1843132718Skan if (type_dependent_expression_p (object)) 1844169689Skan return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE); 1845132718Skan object = build_non_dependent_expr (object); 1846132718Skan } 1847132718Skan 1848132718Skan result = build_new_method_call (object, fn, args, NULL_TREE, 1849169689Skan (disallow_virtual 1850169689Skan ? LOOKUP_NONVIRTUAL : 0), 1851169689Skan /*fn_p=*/NULL); 185250397Sobrien } 1853117395Skan else if (is_overloaded_fn (fn)) 1854169689Skan { 1855169689Skan /* If the function is an overloaded builtin, resolve it. */ 1856169689Skan if (TREE_CODE (fn) == FUNCTION_DECL 1857169689Skan && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL 1858169689Skan || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD)) 1859169689Skan result = resolve_overloaded_builtin (fn, args); 1860169689Skan 1861169689Skan if (!result) 1862169689Skan /* A call to a namespace-scope function. */ 1863169689Skan result = build_new_function_call (fn, args, koenig_p); 1864169689Skan } 1865132718Skan else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR) 1866132718Skan { 1867132718Skan if (args) 1868132718Skan error ("arguments to destructor are not allowed"); 1869132718Skan /* Mark the pseudo-destructor call as having side-effects so 1870132718Skan that we do not issue warnings about its use. */ 1871132718Skan result = build1 (NOP_EXPR, 1872132718Skan void_type_node, 1873132718Skan TREE_OPERAND (fn, 0)); 1874132718Skan TREE_SIDE_EFFECTS (result) = 1; 1875132718Skan } 1876117395Skan else if (CLASS_TYPE_P (TREE_TYPE (fn))) 1877132718Skan /* If the "function" is really an object of class type, it might 1878132718Skan have an overloaded `operator ()'. */ 1879132718Skan result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE, 1880132718Skan /*overloaded_p=*/NULL); 1881169689Skan 1882132718Skan if (!result) 1883132718Skan /* A call where the function is unknown. */ 1884132718Skan result = build_function_call (fn, args); 1885132718Skan 1886132718Skan if (processing_template_decl) 1887117395Skan { 1888169689Skan result = build3 (CALL_EXPR, TREE_TYPE (result), orig_fn, 1889169689Skan orig_args, NULL_TREE); 1890132718Skan KOENIG_LOOKUP_P (result) = koenig_p; 1891117395Skan } 1892132718Skan return result; 189350397Sobrien} 189450397Sobrien 189550397Sobrien/* Finish a call to a postfix increment or decrement or EXPR. (Which 189650397Sobrien is indicated by CODE, which should be POSTINCREMENT_EXPR or 189750397Sobrien POSTDECREMENT_EXPR.) */ 189850397Sobrien 1899169689Skantree 1900132718Skanfinish_increment_expr (tree expr, enum tree_code code) 190150397Sobrien{ 1902169689Skan return build_x_unary_op (code, expr); 190350397Sobrien} 190450397Sobrien 190550397Sobrien/* Finish a use of `this'. Returns an expression for `this'. */ 190650397Sobrien 1907169689Skantree 1908132718Skanfinish_this_expr (void) 190950397Sobrien{ 191050397Sobrien tree result; 191150397Sobrien 191250397Sobrien if (current_class_ptr) 191350397Sobrien { 191450397Sobrien result = current_class_ptr; 191550397Sobrien } 191650397Sobrien else if (current_function_decl 191750397Sobrien && DECL_STATIC_FUNCTION_P (current_function_decl)) 191850397Sobrien { 1919169689Skan error ("%<this%> is unavailable for static member functions"); 192050397Sobrien result = error_mark_node; 192150397Sobrien } 192250397Sobrien else 192350397Sobrien { 192450397Sobrien if (current_function_decl) 1925169689Skan error ("invalid use of %<this%> in non-member function"); 192650397Sobrien else 1927169689Skan error ("invalid use of %<this%> at top level"); 192850397Sobrien result = error_mark_node; 192950397Sobrien } 193050397Sobrien 193150397Sobrien return result; 193250397Sobrien} 193350397Sobrien 1934132718Skan/* Finish a pseudo-destructor expression. If SCOPE is NULL, the 1935132718Skan expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is 1936132718Skan the TYPE for the type given. If SCOPE is non-NULL, the expression 1937132718Skan was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */ 193850397Sobrien 1939169689Skantree 1940132718Skanfinish_pseudo_destructor_expr (tree object, tree scope, tree destructor) 194150397Sobrien{ 1942132718Skan if (destructor == error_mark_node) 1943132718Skan return error_mark_node; 194450397Sobrien 1945169689Skan gcc_assert (TYPE_P (destructor)); 194650397Sobrien 1947132718Skan if (!processing_template_decl) 1948132718Skan { 1949132718Skan if (scope == error_mark_node) 195050397Sobrien { 1951132718Skan error ("invalid qualifying scope in pseudo-destructor name"); 195250397Sobrien return error_mark_node; 195350397Sobrien } 1954169689Skan if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor)) 1955169689Skan { 1956169689Skan error ("qualified type %qT does not match destructor name ~%qT", 1957169689Skan scope, destructor); 1958169689Skan return error_mark_node; 1959169689Skan } 1960169689Skan 1961169689Skan 1962132718Skan /* [expr.pseudo] says both: 196350397Sobrien 1964169689Skan The type designated by the pseudo-destructor-name shall be 1965132718Skan the same as the object type. 196650397Sobrien 1967169689Skan and: 196850397Sobrien 1969169689Skan The cv-unqualified versions of the object type and of the 1970132718Skan type designated by the pseudo-destructor-name shall be the 1971132718Skan same type. 197250397Sobrien 1973169689Skan We implement the more generous second sentence, since that is 1974169689Skan what most other compilers do. */ 1975169689Skan if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object), 1976132718Skan destructor)) 1977132718Skan { 1978169689Skan error ("%qE is not of type %qT", object, destructor); 1979132718Skan return error_mark_node; 1980132718Skan } 1981132718Skan } 198290075Sobrien 1983169689Skan return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor); 198450397Sobrien} 198550397Sobrien 198650397Sobrien/* Finish an expression of the form CODE EXPR. */ 198750397Sobrien 198850397Sobrientree 1989132718Skanfinish_unary_op_expr (enum tree_code code, tree expr) 199050397Sobrien{ 199150397Sobrien tree result = build_x_unary_op (code, expr); 199290075Sobrien /* Inside a template, build_x_unary_op does not fold the 199390075Sobrien expression. So check whether the result is folded before 199490075Sobrien setting TREE_NEGATED_INT. */ 199590075Sobrien if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST 199690075Sobrien && TREE_CODE (result) == INTEGER_CST 1997169689Skan && !TYPE_UNSIGNED (TREE_TYPE (result)) 199890075Sobrien && INT_CST_LT (result, integer_zero_node)) 1999169689Skan { 2000169689Skan /* RESULT may be a cached INTEGER_CST, so we must copy it before 2001169689Skan setting TREE_NEGATED_INT. */ 2002169689Skan result = copy_node (result); 2003169689Skan TREE_NEGATED_INT (result) = 1; 2004169689Skan } 200550397Sobrien overflow_warning (result); 200650397Sobrien return result; 200750397Sobrien} 200850397Sobrien 2009132718Skan/* Finish a compound-literal expression. TYPE is the type to which 2010132718Skan the INITIALIZER_LIST is being cast. */ 201150397Sobrien 201250397Sobrientree 2013169689Skanfinish_compound_literal (tree type, VEC(constructor_elt,gc) *initializer_list) 201450397Sobrien{ 2015169689Skan tree var; 2016132718Skan tree compound_literal; 201750397Sobrien 2018169689Skan if (!TYPE_OBJ_P (type)) 2019169689Skan { 2020169689Skan error ("compound literal of non-object type %qT", type); 2021169689Skan return error_mark_node; 2022169689Skan } 2023169689Skan 2024132718Skan /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */ 2025132718Skan compound_literal = build_constructor (NULL_TREE, initializer_list); 2026132718Skan if (processing_template_decl) 2027132718Skan { 2028169689Skan TREE_TYPE (compound_literal) = type; 2029169689Skan /* Mark the expression as a compound literal. */ 2030169689Skan TREE_HAS_CONSTRUCTOR (compound_literal) = 1; 2031169689Skan return compound_literal; 2032169689Skan } 2033132718Skan 2034169689Skan /* Create a temporary variable to represent the compound literal. */ 2035169689Skan var = create_temporary_var (type); 2036169689Skan if (!current_function_decl) 2037169689Skan { 2038169689Skan /* If this compound-literal appears outside of a function, then 2039169689Skan the corresponding variable has static storage duration, just 2040169689Skan like the variable in whose initializer it appears. */ 2041169689Skan TREE_STATIC (var) = 1; 2042169689Skan /* The variable has internal linkage, since there is no need to 2043169689Skan reference it from another translation unit. */ 2044169689Skan TREE_PUBLIC (var) = 0; 2045169689Skan /* It must have a name, so that the name mangler can mangle it. */ 2046169689Skan DECL_NAME (var) = make_anon_name (); 2047132718Skan } 2048169689Skan /* We must call pushdecl, since the gimplifier complains if the 2049169689Skan variable has not been declared via a BIND_EXPR. */ 2050169689Skan pushdecl (var); 2051169689Skan /* Initialize the variable as we would any other variable with a 2052169689Skan brace-enclosed initializer. */ 2053169689Skan cp_finish_decl (var, compound_literal, 2054169689Skan /*init_const_expr_p=*/false, 2055169689Skan /*asmspec_tree=*/NULL_TREE, 2056169689Skan LOOKUP_ONLYCONVERTING); 2057169689Skan return var; 205850397Sobrien} 205950397Sobrien 2060117395Skan/* Return the declaration for the function-name variable indicated by 2061117395Skan ID. */ 2062117395Skan 2063117395Skantree 2064117395Skanfinish_fname (tree id) 2065117395Skan{ 2066117395Skan tree decl; 2067169689Skan 2068117395Skan decl = fname_decl (C_RID_CODE (id), id); 2069117395Skan if (processing_template_decl) 2070132718Skan decl = DECL_NAME (decl); 2071117395Skan return decl; 2072117395Skan} 2073117395Skan 207450397Sobrien/* Finish a translation unit. */ 207550397Sobrien 2076169689Skanvoid 2077132718Skanfinish_translation_unit (void) 207850397Sobrien{ 207950397Sobrien /* In case there were missing closebraces, 208050397Sobrien get us back to the global binding level. */ 208190075Sobrien pop_everything (); 208250397Sobrien while (current_namespace != global_namespace) 208350397Sobrien pop_namespace (); 208490075Sobrien 2085117395Skan /* Do file scope __FUNCTION__ et al. */ 208690075Sobrien finish_fname_decls (); 208750397Sobrien} 208850397Sobrien 208950397Sobrien/* Finish a template type parameter, specified as AGGR IDENTIFIER. 209050397Sobrien Returns the parameter. */ 209150397Sobrien 2092169689Skantree 2093132718Skanfinish_template_type_parm (tree aggr, tree identifier) 209450397Sobrien{ 209590075Sobrien if (aggr != class_type_node) 209650397Sobrien { 2097169689Skan pedwarn ("template type parameters must use the keyword %<class%> or %<typename%>"); 209850397Sobrien aggr = class_type_node; 209950397Sobrien } 210050397Sobrien 210150397Sobrien return build_tree_list (aggr, identifier); 210250397Sobrien} 210350397Sobrien 210450397Sobrien/* Finish a template template parameter, specified as AGGR IDENTIFIER. 210550397Sobrien Returns the parameter. */ 210650397Sobrien 2107169689Skantree 2108132718Skanfinish_template_template_parm (tree aggr, tree identifier) 210950397Sobrien{ 211050397Sobrien tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE); 211150397Sobrien tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE); 211250397Sobrien DECL_TEMPLATE_PARMS (tmpl) = current_template_parms; 211350397Sobrien DECL_TEMPLATE_RESULT (tmpl) = decl; 211490075Sobrien DECL_ARTIFICIAL (decl) = 1; 211550397Sobrien end_template_decl (); 211650397Sobrien 2117169689Skan gcc_assert (DECL_TEMPLATE_PARMS (tmpl)); 211890075Sobrien 211950397Sobrien return finish_template_type_parm (aggr, tmpl); 212050397Sobrien} 212150397Sobrien 2122117395Skan/* ARGUMENT is the default-argument value for a template template 2123117395Skan parameter. If ARGUMENT is invalid, issue error messages and return 2124117395Skan the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */ 2125117395Skan 2126117395Skantree 2127117395Skancheck_template_template_default_arg (tree argument) 2128117395Skan{ 2129117395Skan if (TREE_CODE (argument) != TEMPLATE_DECL 2130117395Skan && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM 2131117395Skan && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) 2132117395Skan { 2133132718Skan if (TREE_CODE (argument) == TYPE_DECL) 2134169689Skan error ("invalid use of type %qT as a default value for a template " 2135169689Skan "template-parameter", TREE_TYPE (argument)); 2136132718Skan else 2137132718Skan error ("invalid default argument for a template template parameter"); 2138117395Skan return error_mark_node; 2139117395Skan } 2140117395Skan 2141117395Skan return argument; 2142117395Skan} 2143117395Skan 214450397Sobrien/* Begin a class definition, as indicated by T. */ 214550397Sobrien 214650397Sobrientree 2147169689Skanbegin_class_definition (tree t, tree attributes) 214850397Sobrien{ 214990075Sobrien if (t == error_mark_node) 215090075Sobrien return error_mark_node; 215190075Sobrien 215290075Sobrien if (processing_template_parmlist) 215350397Sobrien { 2154169689Skan error ("definition of %q#T inside template parameter list", t); 215590075Sobrien return error_mark_node; 215690075Sobrien } 215790075Sobrien /* A non-implicit typename comes from code like: 215890075Sobrien 215990075Sobrien template <typename T> struct A { 2160169689Skan template <typename U> struct A<T>::B ... 216190075Sobrien 216290075Sobrien This is erroneous. */ 216390075Sobrien else if (TREE_CODE (t) == TYPENAME_TYPE) 216490075Sobrien { 2165169689Skan error ("invalid definition of qualified type %qT", t); 216690075Sobrien t = error_mark_node; 216790075Sobrien } 216890075Sobrien 216990075Sobrien if (t == error_mark_node || ! IS_AGGR_TYPE (t)) 217090075Sobrien { 217190075Sobrien t = make_aggr_type (RECORD_TYPE); 2172169689Skan pushtag (make_anon_name (), t, /*tag_scope=*/ts_current); 217350397Sobrien } 217452284Sobrien 217590075Sobrien /* Update the location of the decl. */ 2176132718Skan DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location; 2177169689Skan 217852284Sobrien if (TYPE_BEING_DEFINED (t)) 217950397Sobrien { 218090075Sobrien t = make_aggr_type (TREE_CODE (t)); 2181169689Skan pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current); 218250397Sobrien } 218352284Sobrien maybe_process_partial_specialization (t); 2184132718Skan pushclass (t); 218550397Sobrien TYPE_BEING_DEFINED (t) = 1; 2186169689Skan 2187169689Skan cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 2188169689Skan 2189132718Skan if (flag_pack_struct) 2190132718Skan { 2191132718Skan tree v; 2192132718Skan TYPE_PACKED (t) = 1; 2193132718Skan /* Even though the type is being defined for the first time 2194132718Skan here, there might have been a forward declaration, so there 2195132718Skan might be cv-qualified variants of T. */ 2196132718Skan for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) 2197132718Skan TYPE_PACKED (v) = 1; 2198132718Skan } 219950397Sobrien /* Reset the interface data, at the earliest possible 220050397Sobrien moment, as it might have been set via a class foo; 220150397Sobrien before. */ 220290075Sobrien if (! TYPE_ANONYMOUS_P (t)) 220350397Sobrien { 2204169689Skan struct c_fileinfo *finfo = get_fileinfo (input_filename); 2205169689Skan CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only; 220690075Sobrien SET_CLASSTYPE_INTERFACE_UNKNOWN_X 2207169689Skan (t, finfo->interface_unknown); 220850397Sobrien } 220950397Sobrien reset_specialization(); 2210169689Skan 221152284Sobrien /* Make a declaration for this class in its own scope. */ 221252284Sobrien build_self_reference (); 221352284Sobrien 221452284Sobrien return t; 221550397Sobrien} 221650397Sobrien 221752284Sobrien/* Finish the member declaration given by DECL. */ 221850397Sobrien 221952284Sobrienvoid 2220132718Skanfinish_member_declaration (tree decl) 222152284Sobrien{ 222252284Sobrien if (decl == error_mark_node || decl == NULL_TREE) 222352284Sobrien return; 222452284Sobrien 222552284Sobrien if (decl == void_type_node) 222652284Sobrien /* The COMPONENT was a friend, not a member, and so there's 222752284Sobrien nothing for us to do. */ 222852284Sobrien return; 222952284Sobrien 223052284Sobrien /* We should see only one DECL at a time. */ 2231169689Skan gcc_assert (TREE_CHAIN (decl) == NULL_TREE); 223252284Sobrien 223352284Sobrien /* Set up access control for DECL. */ 2234169689Skan TREE_PRIVATE (decl) 223552284Sobrien = (current_access_specifier == access_private_node); 2236169689Skan TREE_PROTECTED (decl) 223752284Sobrien = (current_access_specifier == access_protected_node); 223852284Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 223952284Sobrien { 224090075Sobrien TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl); 224190075Sobrien TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl); 224252284Sobrien } 224352284Sobrien 224452284Sobrien /* Mark the DECL as a member of the current class. */ 224590075Sobrien DECL_CONTEXT (decl) = current_class_type; 224652284Sobrien 224790075Sobrien /* [dcl.link] 224890075Sobrien 224990075Sobrien A C language linkage is ignored for the names of class members 225090075Sobrien and the member function type of class member functions. */ 225190075Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c) 225290075Sobrien SET_DECL_LANGUAGE (decl, lang_cplusplus); 225390075Sobrien 225452284Sobrien /* Put functions on the TYPE_METHODS list and everything else on the 225552284Sobrien TYPE_FIELDS list. Note that these are built up in reverse order. 225652284Sobrien We reverse them (to obtain declaration order) in finish_struct. */ 2257169689Skan if (TREE_CODE (decl) == FUNCTION_DECL 225852284Sobrien || DECL_FUNCTION_TEMPLATE_P (decl)) 225952284Sobrien { 226052284Sobrien /* We also need to add this function to the 226152284Sobrien CLASSTYPE_METHOD_VEC. */ 2262169689Skan if (add_method (current_class_type, decl, NULL_TREE)) 2263169689Skan { 2264169689Skan TREE_CHAIN (decl) = TYPE_METHODS (current_class_type); 2265169689Skan TYPE_METHODS (current_class_type) = decl; 226652284Sobrien 2267169689Skan maybe_add_class_template_decl_list (current_class_type, decl, 2268169689Skan /*friend_p=*/0); 2269169689Skan } 227052284Sobrien } 2271117395Skan /* Enter the DECL into the scope of the class. */ 2272169689Skan else if ((TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) 2273132718Skan || pushdecl_class_level (decl)) 227452284Sobrien { 227552284Sobrien /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields 227652284Sobrien go at the beginning. The reason is that lookup_field_1 227752284Sobrien searches the list in order, and we want a field name to 227852284Sobrien override a type name so that the "struct stat hack" will 227952284Sobrien work. In particular: 228052284Sobrien 228152284Sobrien struct S { enum E { }; int E } s; 228252284Sobrien s.E = 3; 228352284Sobrien 2284117395Skan is valid. In addition, the FIELD_DECLs must be maintained in 228552284Sobrien declaration order so that class layout works as expected. 228652284Sobrien However, we don't need that order until class layout, so we 228752284Sobrien save a little time by putting FIELD_DECLs on in reverse order 228852284Sobrien here, and then reversing them in finish_struct_1. (We could 228952284Sobrien also keep a pointer to the correct insertion points in the 229052284Sobrien list.) */ 229152284Sobrien 229252284Sobrien if (TREE_CODE (decl) == TYPE_DECL) 2293169689Skan TYPE_FIELDS (current_class_type) 229452284Sobrien = chainon (TYPE_FIELDS (current_class_type), decl); 229552284Sobrien else 229652284Sobrien { 229752284Sobrien TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type); 229852284Sobrien TYPE_FIELDS (current_class_type) = decl; 229952284Sobrien } 230052284Sobrien 2301169689Skan maybe_add_class_template_decl_list (current_class_type, decl, 2302117395Skan /*friend_p=*/0); 230352284Sobrien } 2304169689Skan 2305169689Skan if (pch_file) 2306169689Skan note_decl_for_pch (decl); 230752284Sobrien} 230852284Sobrien 2309169689Skan/* DECL has been declared while we are building a PCH file. Perform 2310169689Skan actions that we might normally undertake lazily, but which can be 2311169689Skan performed now so that they do not have to be performed in 2312169689Skan translation units which include the PCH file. */ 231350397Sobrien 2314169689Skanvoid 2315169689Skannote_decl_for_pch (tree decl) 231650397Sobrien{ 2317169689Skan gcc_assert (pch_file); 231852284Sobrien 2319169689Skan /* There's a good chance that we'll have to mangle names at some 2320169689Skan point, even if only for emission in debugging information. */ 2321169689Skan if ((TREE_CODE (decl) == VAR_DECL 2322169689Skan || TREE_CODE (decl) == FUNCTION_DECL) 2323169689Skan && !processing_template_decl) 2324169689Skan mangle_decl (decl); 232550397Sobrien} 232652284Sobrien 232790075Sobrien/* Finish processing a complete template declaration. The PARMS are 232852284Sobrien the template parameters. */ 232952284Sobrien 233052284Sobrienvoid 2331132718Skanfinish_template_decl (tree parms) 233252284Sobrien{ 233352284Sobrien if (parms) 233452284Sobrien end_template_decl (); 233552284Sobrien else 233652284Sobrien end_specialization (); 233752284Sobrien} 233852284Sobrien 233990075Sobrien/* Finish processing a template-id (which names a type) of the form 234052284Sobrien NAME < ARGS >. Return the TYPE_DECL for the type named by the 2341117395Skan template-id. If ENTERING_SCOPE is nonzero we are about to enter 234252284Sobrien the scope of template-id indicated. */ 234352284Sobrien 234452284Sobrientree 2345132718Skanfinish_template_type (tree name, tree args, int entering_scope) 234652284Sobrien{ 234752284Sobrien tree decl; 234852284Sobrien 234952284Sobrien decl = lookup_template_class (name, args, 2350132718Skan NULL_TREE, NULL_TREE, entering_scope, 2351169689Skan tf_warning_or_error | tf_user); 235252284Sobrien if (decl != error_mark_node) 235352284Sobrien decl = TYPE_STUB_DECL (decl); 235452284Sobrien 235552284Sobrien return decl; 235652284Sobrien} 235752284Sobrien 235852284Sobrien/* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER. 235952284Sobrien Return a TREE_LIST containing the ACCESS_SPECIFIER and the 236052284Sobrien BASE_CLASS, or NULL_TREE if an error occurred. The 236190075Sobrien ACCESS_SPECIFIER is one of 2362169689Skan access_{default,public,protected_private}_node. For a virtual base 2363169689Skan we set TREE_TYPE. */ 236452284Sobrien 2365169689Skantree 2366132718Skanfinish_base_specifier (tree base, tree access, bool virtual_p) 236752284Sobrien{ 236852284Sobrien tree result; 236952284Sobrien 2370132718Skan if (base == error_mark_node) 2371117395Skan { 2372117395Skan error ("invalid base-class specification"); 2373117395Skan result = NULL_TREE; 2374117395Skan } 2375132718Skan else if (! is_aggr_type (base, 1)) 237690075Sobrien result = NULL_TREE; 237752284Sobrien else 237852284Sobrien { 2379132718Skan if (cp_type_quals (base) != 0) 2380169689Skan { 2381169689Skan error ("base class %qT has cv qualifiers", base); 2382169689Skan base = TYPE_MAIN_VARIANT (base); 2383169689Skan } 2384132718Skan result = build_tree_list (access, base); 2385169689Skan if (virtual_p) 2386169689Skan TREE_TYPE (result) = integer_type_node; 238752284Sobrien } 238852284Sobrien 238952284Sobrien return result; 239052284Sobrien} 239152284Sobrien 2392169689Skan/* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is 2393169689Skan what we found when we tried to do the lookup. */ 239452284Sobrien 239552284Sobrienvoid 2396169689Skanqualified_name_lookup_error (tree scope, tree name, tree decl) 239752284Sobrien{ 2398161651Skan if (scope == error_mark_node) 2399161651Skan ; /* We already complained. */ 2400161651Skan else if (TYPE_P (scope)) 2401132718Skan { 2402132718Skan if (!COMPLETE_TYPE_P (scope)) 2403169689Skan error ("incomplete type %qT used in nested name specifier", scope); 2404169689Skan else if (TREE_CODE (decl) == TREE_LIST) 2405169689Skan { 2406169689Skan error ("reference to %<%T::%D%> is ambiguous", scope, name); 2407169689Skan print_candidates (decl); 2408169689Skan } 2409132718Skan else 2410169689Skan error ("%qD is not a member of %qT", name, scope); 2411132718Skan } 2412132718Skan else if (scope != global_namespace) 2413169689Skan error ("%qD is not a member of %qD", name, scope); 2414132718Skan else 2415169689Skan error ("%<::%D%> has not been declared", name); 2416132718Skan} 2417169689Skan 2418169689Skan/* If FNS is a member function, a set of member functions, or a 2419169689Skan template-id referring to one or more member functions, return a 2420169689Skan BASELINK for FNS, incorporating the current access context. 2421169689Skan Otherwise, return FNS unchanged. */ 2422169689Skan 2423169689Skantree 2424169689Skanbaselink_for_fns (tree fns) 2425169689Skan{ 2426169689Skan tree fn; 2427169689Skan tree cl; 2428169689Skan 2429169689Skan if (BASELINK_P (fns) 2430169689Skan || error_operand_p (fns)) 2431169689Skan return fns; 2432169689Skan 2433169689Skan fn = fns; 2434169689Skan if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 2435169689Skan fn = TREE_OPERAND (fn, 0); 2436169689Skan fn = get_first_fn (fn); 2437169689Skan if (!DECL_FUNCTION_MEMBER_P (fn)) 2438169689Skan return fns; 2439169689Skan 2440169689Skan cl = currently_open_derived_class (DECL_CONTEXT (fn)); 2441169689Skan if (!cl) 2442169689Skan cl = DECL_CONTEXT (fn); 2443169689Skan cl = TYPE_BINFO (cl); 2444169689Skan return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE); 2445169689Skan} 2446169689Skan 2447132718Skan/* ID_EXPRESSION is a representation of parsed, but unprocessed, 2448132718Skan id-expression. (See cp_parser_id_expression for details.) SCOPE, 2449132718Skan if non-NULL, is the type or namespace used to explicitly qualify 2450132718Skan ID_EXPRESSION. DECL is the entity to which that name has been 2451169689Skan resolved. 2452132718Skan 2453132718Skan *CONSTANT_EXPRESSION_P is true if we are presently parsing a 2454132718Skan constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will 2455132718Skan be set to true if this expression isn't permitted in a 2456132718Skan constant-expression, but it is otherwise not set by this function. 2457132718Skan *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a 2458132718Skan constant-expression, but a non-constant expression is also 2459132718Skan permissible. 2460132718Skan 2461169689Skan DONE is true if this expression is a complete postfix-expression; 2462169689Skan it is false if this expression is followed by '->', '[', '(', etc. 2463169689Skan ADDRESS_P is true iff this expression is the operand of '&'. 2464169689Skan TEMPLATE_P is true iff the qualified-id was of the form 2465169689Skan "A::template B". TEMPLATE_ARG_P is true iff this qualified name 2466169689Skan appears as a template argument. 2467169689Skan 2468132718Skan If an error occurs, and it is the kind of error that might cause 2469132718Skan the parser to abort a tentative parse, *ERROR_MSG is filled in. It 2470132718Skan is the caller's responsibility to issue the message. *ERROR_MSG 2471132718Skan will be a string with static storage duration, so the caller need 2472132718Skan not "free" it. 2473132718Skan 2474132718Skan Return an expression for the entity, after issuing appropriate 2475132718Skan diagnostics. This function is also responsible for transforming a 2476132718Skan reference to a non-static member into a COMPONENT_REF that makes 2477169689Skan the use of "this" explicit. 2478132718Skan 2479132718Skan Upon return, *IDK will be filled in appropriately. */ 2480132718Skan 2481132718Skantree 2482169689Skanfinish_id_expression (tree id_expression, 2483132718Skan tree decl, 2484132718Skan tree scope, 2485132718Skan cp_id_kind *idk, 2486132718Skan bool integral_constant_expression_p, 2487132718Skan bool allow_non_integral_constant_expression_p, 2488132718Skan bool *non_integral_constant_expression_p, 2489169689Skan bool template_p, 2490169689Skan bool done, 2491169689Skan bool address_p, 2492169689Skan bool template_arg_p, 2493132718Skan const char **error_msg) 2494132718Skan{ 2495132718Skan /* Initialize the output parameters. */ 2496132718Skan *idk = CP_ID_KIND_NONE; 2497132718Skan *error_msg = NULL; 2498132718Skan 2499132718Skan if (id_expression == error_mark_node) 2500132718Skan return error_mark_node; 2501132718Skan /* If we have a template-id, then no further lookup is 2502132718Skan required. If the template-id was for a template-class, we 2503132718Skan will sometimes have a TYPE_DECL at this point. */ 2504132718Skan else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 2505132718Skan || TREE_CODE (decl) == TYPE_DECL) 2506132718Skan ; 2507132718Skan /* Look up the name. */ 2508169689Skan else 2509132718Skan { 2510132718Skan if (decl == error_mark_node) 2511132718Skan { 2512132718Skan /* Name lookup failed. */ 2513169689Skan if (scope 2514169689Skan && (!TYPE_P (scope) 2515132718Skan || (!dependent_type_p (scope) 2516132718Skan && !(TREE_CODE (id_expression) == IDENTIFIER_NODE 2517132718Skan && IDENTIFIER_TYPENAME_P (id_expression) 2518132718Skan && dependent_type_p (TREE_TYPE (id_expression)))))) 2519132718Skan { 2520132718Skan /* If the qualifying type is non-dependent (and the name 2521132718Skan does not name a conversion operator to a dependent 2522132718Skan type), issue an error. */ 2523169689Skan qualified_name_lookup_error (scope, id_expression, decl); 2524132718Skan return error_mark_node; 2525132718Skan } 2526132718Skan else if (!scope) 2527132718Skan { 2528132718Skan /* It may be resolved via Koenig lookup. */ 2529132718Skan *idk = CP_ID_KIND_UNQUALIFIED; 2530132718Skan return id_expression; 2531132718Skan } 2532132718Skan else 2533132718Skan decl = id_expression; 2534132718Skan } 2535132718Skan /* If DECL is a variable that would be out of scope under 2536132718Skan ANSI/ISO rules, but in scope in the ARM, name lookup 2537132718Skan will succeed. Issue a diagnostic here. */ 2538132718Skan else 2539132718Skan decl = check_for_out_of_scope_variable (decl); 2540132718Skan 2541132718Skan /* Remember that the name was used in the definition of 2542132718Skan the current class so that we can check later to see if 2543132718Skan the meaning would have been different after the class 2544132718Skan was entirely defined. */ 2545132718Skan if (!scope && decl != error_mark_node) 2546132718Skan maybe_note_name_used_in_class (id_expression, decl); 2547161651Skan 2548161651Skan /* Disallow uses of local variables from containing functions. */ 2549161651Skan if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL) 2550161651Skan { 2551161651Skan tree context = decl_function_context (decl); 2552161651Skan if (context != NULL_TREE && context != current_function_decl 2553161651Skan && ! TREE_STATIC (decl)) 2554161651Skan { 2555161651Skan error (TREE_CODE (decl) == VAR_DECL 2556169689Skan ? "use of %<auto%> variable from containing function" 2557161651Skan : "use of parameter from containing function"); 2558169689Skan error (" %q+#D declared here", decl); 2559161651Skan return error_mark_node; 2560161651Skan } 2561161651Skan } 2562132718Skan } 2563132718Skan 2564132718Skan /* If we didn't find anything, or what we found was a type, 2565132718Skan then this wasn't really an id-expression. */ 2566132718Skan if (TREE_CODE (decl) == TEMPLATE_DECL 2567132718Skan && !DECL_FUNCTION_TEMPLATE_P (decl)) 2568132718Skan { 2569132718Skan *error_msg = "missing template arguments"; 2570132718Skan return error_mark_node; 2571132718Skan } 2572132718Skan else if (TREE_CODE (decl) == TYPE_DECL 2573132718Skan || TREE_CODE (decl) == NAMESPACE_DECL) 2574132718Skan { 2575132718Skan *error_msg = "expected primary-expression"; 2576132718Skan return error_mark_node; 2577132718Skan } 2578132718Skan 2579132718Skan /* If the name resolved to a template parameter, there is no 2580132718Skan need to look it up again later. */ 2581132718Skan if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl)) 2582132718Skan || TREE_CODE (decl) == TEMPLATE_PARM_INDEX) 2583132718Skan { 2584169689Skan tree r; 2585169689Skan 2586132718Skan *idk = CP_ID_KIND_NONE; 2587132718Skan if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX) 2588132718Skan decl = TEMPLATE_PARM_DECL (decl); 2589169689Skan r = convert_from_reference (DECL_INITIAL (decl)); 2590169689Skan 2591169689Skan if (integral_constant_expression_p 2592132718Skan && !dependent_type_p (TREE_TYPE (decl)) 2593169689Skan && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r)))) 2594132718Skan { 2595132718Skan if (!allow_non_integral_constant_expression_p) 2596169689Skan error ("template parameter %qD of type %qT is not allowed in " 2597132718Skan "an integral constant expression because it is not of " 2598132718Skan "integral or enumeration type", decl, TREE_TYPE (decl)); 2599132718Skan *non_integral_constant_expression_p = true; 2600132718Skan } 2601169689Skan return r; 2602132718Skan } 2603169689Skan /* Similarly, we resolve enumeration constants to their 2604132718Skan underlying values. */ 2605132718Skan else if (TREE_CODE (decl) == CONST_DECL) 2606132718Skan { 2607132718Skan *idk = CP_ID_KIND_NONE; 2608132718Skan if (!processing_template_decl) 2609169689Skan { 2610169689Skan used_types_insert (TREE_TYPE (decl)); 2611169689Skan return DECL_INITIAL (decl); 2612169689Skan } 2613132718Skan return decl; 2614132718Skan } 2615132718Skan else 2616132718Skan { 2617132718Skan bool dependent_p; 2618132718Skan 2619132718Skan /* If the declaration was explicitly qualified indicate 2620132718Skan that. The semantics of `A::f(3)' are different than 2621132718Skan `f(3)' if `f' is virtual. */ 2622169689Skan *idk = (scope 2623132718Skan ? CP_ID_KIND_QUALIFIED 2624132718Skan : (TREE_CODE (decl) == TEMPLATE_ID_EXPR 2625132718Skan ? CP_ID_KIND_TEMPLATE_ID 2626132718Skan : CP_ID_KIND_UNQUALIFIED)); 2627132718Skan 2628132718Skan 2629132718Skan /* [temp.dep.expr] 2630132718Skan 2631132718Skan An id-expression is type-dependent if it contains an 2632132718Skan identifier that was declared with a dependent type. 2633132718Skan 2634132718Skan The standard is not very specific about an id-expression that 2635132718Skan names a set of overloaded functions. What if some of them 2636132718Skan have dependent types and some of them do not? Presumably, 2637132718Skan such a name should be treated as a dependent name. */ 2638132718Skan /* Assume the name is not dependent. */ 2639132718Skan dependent_p = false; 2640132718Skan if (!processing_template_decl) 2641132718Skan /* No names are dependent outside a template. */ 2642132718Skan ; 2643132718Skan /* A template-id where the name of the template was not resolved 2644132718Skan is definitely dependent. */ 2645132718Skan else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 2646169689Skan && (TREE_CODE (TREE_OPERAND (decl, 0)) 2647132718Skan == IDENTIFIER_NODE)) 2648132718Skan dependent_p = true; 2649132718Skan /* For anything except an overloaded function, just check its 2650132718Skan type. */ 2651132718Skan else if (!is_overloaded_fn (decl)) 2652169689Skan dependent_p 2653132718Skan = dependent_type_p (TREE_TYPE (decl)); 2654132718Skan /* For a set of overloaded functions, check each of the 2655132718Skan functions. */ 2656132718Skan else 2657132718Skan { 2658132718Skan tree fns = decl; 2659132718Skan 2660132718Skan if (BASELINK_P (fns)) 2661132718Skan fns = BASELINK_FUNCTIONS (fns); 2662132718Skan 2663132718Skan /* For a template-id, check to see if the template 2664132718Skan arguments are dependent. */ 2665132718Skan if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 2666132718Skan { 2667132718Skan tree args = TREE_OPERAND (fns, 1); 2668132718Skan dependent_p = any_dependent_template_arguments_p (args); 2669132718Skan /* The functions are those referred to by the 2670132718Skan template-id. */ 2671132718Skan fns = TREE_OPERAND (fns, 0); 2672132718Skan } 2673132718Skan 2674132718Skan /* If there are no dependent template arguments, go through 2675132718Skan the overloaded functions. */ 2676132718Skan while (fns && !dependent_p) 2677132718Skan { 2678132718Skan tree fn = OVL_CURRENT (fns); 2679132718Skan 2680132718Skan /* Member functions of dependent classes are 2681132718Skan dependent. */ 2682132718Skan if (TREE_CODE (fn) == FUNCTION_DECL 2683132718Skan && type_dependent_expression_p (fn)) 2684132718Skan dependent_p = true; 2685132718Skan else if (TREE_CODE (fn) == TEMPLATE_DECL 2686132718Skan && dependent_template_p (fn)) 2687132718Skan dependent_p = true; 2688132718Skan 2689132718Skan fns = OVL_NEXT (fns); 2690132718Skan } 2691132718Skan } 2692132718Skan 2693132718Skan /* If the name was dependent on a template parameter, we will 2694132718Skan resolve the name at instantiation time. */ 2695132718Skan if (dependent_p) 2696132718Skan { 2697132718Skan /* Create a SCOPE_REF for qualified names, if the scope is 2698132718Skan dependent. */ 2699132718Skan if (scope) 2700132718Skan { 2701132718Skan /* Since this name was dependent, the expression isn't 2702132718Skan constant -- yet. No error is issued because it might 2703132718Skan be constant when things are instantiated. */ 2704132718Skan if (integral_constant_expression_p) 2705132718Skan *non_integral_constant_expression_p = true; 2706169689Skan if (TYPE_P (scope)) 2707169689Skan { 2708169689Skan if (address_p && done) 2709169689Skan decl = finish_qualified_id_expr (scope, decl, 2710169689Skan done, address_p, 2711169689Skan template_p, 2712169689Skan template_arg_p); 2713169689Skan else if (dependent_type_p (scope)) 2714169689Skan decl = build_qualified_name (/*type=*/NULL_TREE, 2715169689Skan scope, 2716169689Skan id_expression, 2717169689Skan template_p); 2718169689Skan else if (DECL_P (decl)) 2719169689Skan decl = build_qualified_name (TREE_TYPE (decl), 2720169689Skan scope, 2721169689Skan id_expression, 2722169689Skan template_p); 2723169689Skan } 2724169689Skan if (TREE_TYPE (decl)) 2725169689Skan decl = convert_from_reference (decl); 2726169689Skan return decl; 2727132718Skan } 2728132718Skan /* A TEMPLATE_ID already contains all the information we 2729132718Skan need. */ 2730132718Skan if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR) 2731132718Skan return id_expression; 2732132718Skan *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT; 2733132718Skan /* If we found a variable, then name lookup during the 2734132718Skan instantiation will always resolve to the same VAR_DECL 2735132718Skan (or an instantiation thereof). */ 2736132718Skan if (TREE_CODE (decl) == VAR_DECL 2737132718Skan || TREE_CODE (decl) == PARM_DECL) 2738169689Skan return convert_from_reference (decl); 2739146895Skan /* The same is true for FIELD_DECL, but we also need to 2740146895Skan make sure that the syntax is correct. */ 2741146895Skan else if (TREE_CODE (decl) == FIELD_DECL) 2742146895Skan { 2743146895Skan /* Since SCOPE is NULL here, this is an unqualified name. 2744146895Skan Access checking has been performed during name lookup 2745146895Skan already. Turn off checking to avoid duplicate errors. */ 2746146895Skan push_deferring_access_checks (dk_no_check); 2747146895Skan decl = finish_non_static_data_member 2748146895Skan (decl, current_class_ref, 2749146895Skan /*qualifying_scope=*/NULL_TREE); 2750146895Skan pop_deferring_access_checks (); 2751146895Skan return decl; 2752146895Skan } 2753132718Skan return id_expression; 2754132718Skan } 2755132718Skan 2756132718Skan /* Only certain kinds of names are allowed in constant 2757169689Skan expression. Enumerators and template parameters have already 2758169689Skan been handled above. */ 2759146895Skan if (integral_constant_expression_p 2760169689Skan && ! DECL_INTEGRAL_CONSTANT_VAR_P (decl) 2761169689Skan && ! builtin_valid_in_constant_expr_p (decl)) 2762132718Skan { 2763146895Skan if (!allow_non_integral_constant_expression_p) 2764132718Skan { 2765169689Skan error ("%qD cannot appear in a constant-expression", decl); 2766146895Skan return error_mark_node; 2767132718Skan } 2768146895Skan *non_integral_constant_expression_p = true; 2769132718Skan } 2770169689Skan 2771132718Skan if (TREE_CODE (decl) == NAMESPACE_DECL) 2772132718Skan { 2773169689Skan error ("use of namespace %qD as expression", decl); 2774132718Skan return error_mark_node; 2775132718Skan } 2776132718Skan else if (DECL_CLASS_TEMPLATE_P (decl)) 2777132718Skan { 2778169689Skan error ("use of class template %qT as expression", decl); 2779132718Skan return error_mark_node; 2780132718Skan } 2781132718Skan else if (TREE_CODE (decl) == TREE_LIST) 2782132718Skan { 2783132718Skan /* Ambiguous reference to base members. */ 2784169689Skan error ("request for member %qD is ambiguous in " 2785132718Skan "multiple inheritance lattice", id_expression); 2786132718Skan print_candidates (decl); 2787132718Skan return error_mark_node; 2788132718Skan } 2789132718Skan 2790132718Skan /* Mark variable-like entities as used. Functions are similarly 2791132718Skan marked either below or after overload resolution. */ 2792132718Skan if (TREE_CODE (decl) == VAR_DECL 2793132718Skan || TREE_CODE (decl) == PARM_DECL 2794132718Skan || TREE_CODE (decl) == RESULT_DECL) 2795132718Skan mark_used (decl); 2796132718Skan 2797132718Skan if (scope) 2798132718Skan { 2799169689Skan decl = (adjust_result_of_qualified_name_lookup 2800132718Skan (decl, scope, current_class_type)); 2801132718Skan 2802132718Skan if (TREE_CODE (decl) == FUNCTION_DECL) 2803132718Skan mark_used (decl); 2804132718Skan 2805132718Skan if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl)) 2806169689Skan decl = finish_qualified_id_expr (scope, 2807169689Skan decl, 2808169689Skan done, 2809169689Skan address_p, 2810169689Skan template_p, 2811169689Skan template_arg_p); 2812169689Skan else 2813169689Skan { 2814169689Skan tree r = convert_from_reference (decl); 2815169689Skan 2816169689Skan if (processing_template_decl && TYPE_P (scope)) 2817169689Skan r = build_qualified_name (TREE_TYPE (r), 2818169689Skan scope, decl, 2819169689Skan template_p); 2820169689Skan decl = r; 2821169689Skan } 2822132718Skan } 2823132718Skan else if (TREE_CODE (decl) == FIELD_DECL) 2824146895Skan { 2825146895Skan /* Since SCOPE is NULL here, this is an unqualified name. 2826146895Skan Access checking has been performed during name lookup 2827146895Skan already. Turn off checking to avoid duplicate errors. */ 2828146895Skan push_deferring_access_checks (dk_no_check); 2829146895Skan decl = finish_non_static_data_member (decl, current_class_ref, 2830146895Skan /*qualifying_scope=*/NULL_TREE); 2831146895Skan pop_deferring_access_checks (); 2832146895Skan } 2833132718Skan else if (is_overloaded_fn (decl)) 2834132718Skan { 2835169689Skan tree first_fn; 2836132718Skan 2837169689Skan first_fn = decl; 2838169689Skan if (TREE_CODE (first_fn) == TEMPLATE_ID_EXPR) 2839169689Skan first_fn = TREE_OPERAND (first_fn, 0); 2840169689Skan first_fn = get_first_fn (first_fn); 2841132718Skan if (TREE_CODE (first_fn) == TEMPLATE_DECL) 2842132718Skan first_fn = DECL_TEMPLATE_RESULT (first_fn); 2843132718Skan 2844132718Skan if (!really_overloaded_fn (decl)) 2845132718Skan mark_used (first_fn); 2846132718Skan 2847169689Skan if (!template_arg_p 2848169689Skan && TREE_CODE (first_fn) == FUNCTION_DECL 2849146895Skan && DECL_FUNCTION_MEMBER_P (first_fn) 2850146895Skan && !shared_member_p (decl)) 2851132718Skan { 2852132718Skan /* A set of member functions. */ 2853132718Skan decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0); 2854169689Skan return finish_class_member_access_expr (decl, id_expression, 2855169689Skan /*template_p=*/false); 2856132718Skan } 2857169689Skan 2858169689Skan decl = baselink_for_fns (decl); 2859132718Skan } 2860132718Skan else 2861132718Skan { 2862132718Skan if (DECL_P (decl) && DECL_NONLOCAL (decl) 2863132718Skan && DECL_CLASS_SCOPE_P (decl) 2864132718Skan && DECL_CONTEXT (decl) != current_class_type) 2865132718Skan { 2866132718Skan tree path; 2867169689Skan 2868132718Skan path = currently_open_derived_class (DECL_CONTEXT (decl)); 2869169689Skan perform_or_defer_access_check (TYPE_BINFO (path), decl, decl); 2870132718Skan } 2871169689Skan 2872169689Skan decl = convert_from_reference (decl); 2873132718Skan } 2874132718Skan } 2875132718Skan 2876132718Skan if (TREE_DEPRECATED (decl)) 2877132718Skan warn_deprecated_use (decl); 2878132718Skan 2879132718Skan return decl; 2880132718Skan} 2881132718Skan 288290075Sobrien/* Implement the __typeof keyword: Return the type of EXPR, suitable for 288390075Sobrien use as a type-specifier. */ 288490075Sobrien 288552284Sobrientree 2886132718Skanfinish_typeof (tree expr) 288752284Sobrien{ 2888110611Skan tree type; 2889110611Skan 2890132718Skan if (type_dependent_expression_p (expr)) 289152284Sobrien { 2892110611Skan type = make_aggr_type (TYPEOF_TYPE); 2893169689Skan TYPEOF_TYPE_EXPR (type) = expr; 289452284Sobrien 2895110611Skan return type; 289652284Sobrien } 289752284Sobrien 2898169689Skan type = unlowered_expr_type (expr); 2899110611Skan 2900110611Skan if (!type || type == unknown_type_node) 2901110611Skan { 2902169689Skan error ("type of %qE is unknown", expr); 2903110611Skan return error_mark_node; 2904110611Skan } 2905110611Skan 2906110611Skan return type; 290752284Sobrien} 290890075Sobrien 2909169689Skan/* Perform C++-specific checks for __builtin_offsetof before calling 2910169689Skan fold_offsetof. */ 291190075Sobrien 2912169689Skantree 2913169689Skanfinish_offsetof (tree expr) 291490075Sobrien{ 2915169689Skan if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR) 291690075Sobrien { 2917169689Skan error ("cannot apply %<offsetof%> to destructor %<~%T%>", 2918169689Skan TREE_OPERAND (expr, 2)); 2919169689Skan return error_mark_node; 292090075Sobrien } 2921169689Skan if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE 2922169689Skan || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE 2923169689Skan || TREE_CODE (TREE_TYPE (expr)) == UNKNOWN_TYPE) 2924169689Skan { 2925169689Skan if (TREE_CODE (expr) == COMPONENT_REF 2926169689Skan || TREE_CODE (expr) == COMPOUND_EXPR) 2927169689Skan expr = TREE_OPERAND (expr, 1); 2928169689Skan error ("cannot apply %<offsetof%> to member function %qD", expr); 2929169689Skan return error_mark_node; 2930169689Skan } 2931169689Skan return fold_offsetof (expr, NULL_TREE); 293290075Sobrien} 293390075Sobrien 293490075Sobrien/* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs 2935169689Skan with equivalent CALL_EXPRs. */ 293690075Sobrien 293790075Sobrienstatic tree 2938169689Skansimplify_aggr_init_exprs_r (tree* tp, 2939169689Skan int* walk_subtrees, 2940169689Skan void* data ATTRIBUTE_UNUSED) 294190075Sobrien{ 294290075Sobrien /* We don't need to walk into types; there's nothing in a type that 294390075Sobrien needs simplification. (And, furthermore, there are places we 294490075Sobrien actively don't want to go. For example, we don't want to wander 294590075Sobrien into the default arguments for a FUNCTION_DECL that appears in a 294690075Sobrien CALL_EXPR.) */ 2947132718Skan if (TYPE_P (*tp)) 294890075Sobrien { 294990075Sobrien *walk_subtrees = 0; 295090075Sobrien return NULL_TREE; 295190075Sobrien } 295290075Sobrien /* Only AGGR_INIT_EXPRs are interesting. */ 2953132718Skan else if (TREE_CODE (*tp) != AGGR_INIT_EXPR) 295490075Sobrien return NULL_TREE; 295590075Sobrien 2956132718Skan simplify_aggr_init_expr (tp); 2957132718Skan 2958132718Skan /* Keep iterating. */ 2959132718Skan return NULL_TREE; 2960132718Skan} 2961132718Skan 2962132718Skan/* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This 2963132718Skan function is broken out from the above for the benefit of the tree-ssa 2964132718Skan project. */ 2965132718Skan 2966132718Skanvoid 2967132718Skansimplify_aggr_init_expr (tree *tp) 2968132718Skan{ 2969132718Skan tree aggr_init_expr = *tp; 2970132718Skan 297190075Sobrien /* Form an appropriate CALL_EXPR. */ 2972132718Skan tree fn = TREE_OPERAND (aggr_init_expr, 0); 2973132718Skan tree args = TREE_OPERAND (aggr_init_expr, 1); 2974132718Skan tree slot = TREE_OPERAND (aggr_init_expr, 2); 2975169689Skan tree type = TREE_TYPE (slot); 2976132718Skan 2977132718Skan tree call_expr; 2978132718Skan enum style_t { ctor, arg, pcc } style; 2979132718Skan 298090075Sobrien if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr)) 2981132718Skan style = ctor; 2982132718Skan#ifdef PCC_STATIC_STRUCT_RETURN 2983132718Skan else if (1) 2984132718Skan style = pcc; 2985132718Skan#endif 2986132718Skan else 2987169689Skan { 2988169689Skan gcc_assert (TREE_ADDRESSABLE (type)); 2989169689Skan style = arg; 2990169689Skan } 2991132718Skan 2992169689Skan if (style == ctor) 299390075Sobrien { 2994169689Skan /* Replace the first argument to the ctor with the address of the 2995169689Skan slot. */ 2996132718Skan tree addr; 2997132718Skan 2998169689Skan args = TREE_CHAIN (args); 2999117395Skan cxx_mark_addressable (slot); 3000169689Skan addr = build1 (ADDR_EXPR, build_pointer_type (type), slot); 3001132718Skan args = tree_cons (NULL_TREE, addr, args); 300290075Sobrien } 3003132718Skan 3004169689Skan call_expr = build3 (CALL_EXPR, 3005169689Skan TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), 3006169689Skan fn, args, NULL_TREE); 300790075Sobrien 3008132718Skan if (style == arg) 3009169689Skan { 3010169689Skan /* Just mark it addressable here, and leave the rest to 3011169689Skan expand_call{,_inline}. */ 3012169689Skan cxx_mark_addressable (slot); 3013169689Skan CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true; 3014169689Skan call_expr = build2 (MODIFY_EXPR, TREE_TYPE (call_expr), slot, call_expr); 3015169689Skan } 3016132718Skan else if (style == pcc) 301790075Sobrien { 3018132718Skan /* If we're using the non-reentrant PCC calling convention, then we 3019132718Skan need to copy the returned value out of the static buffer into the 3020132718Skan SLOT. */ 3021132718Skan push_deferring_access_checks (dk_no_check); 302290075Sobrien call_expr = build_aggr_init (slot, call_expr, 302390075Sobrien DIRECT_BIND | LOOKUP_ONLYCONVERTING); 3024132718Skan pop_deferring_access_checks (); 3025169689Skan call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot); 302690075Sobrien } 302790075Sobrien 302890075Sobrien *tp = call_expr; 302990075Sobrien} 303090075Sobrien 303190075Sobrien/* Emit all thunks to FN that should be emitted when FN is emitted. */ 303290075Sobrien 303390075Sobrienstatic void 3034132718Skanemit_associated_thunks (tree fn) 303590075Sobrien{ 303690075Sobrien /* When we use vcall offsets, we emit thunks with the virtual 303790075Sobrien functions to which they thunk. The whole point of vcall offsets 303890075Sobrien is so that you can know statically the entire set of thunks that 303990075Sobrien will ever be needed for a given virtual function, thereby 304090075Sobrien enabling you to output all the thunks with the function itself. */ 304190075Sobrien if (DECL_VIRTUAL_P (fn)) 304290075Sobrien { 3043117395Skan tree thunk; 3044169689Skan 3045117395Skan for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk)) 3046132718Skan { 3047132718Skan if (!THUNK_ALIAS (thunk)) 3048132718Skan { 3049132718Skan use_thunk (thunk, /*emit_p=*/1); 3050132718Skan if (DECL_RESULT_THUNK_P (thunk)) 3051132718Skan { 3052132718Skan tree probe; 3053169689Skan 3054132718Skan for (probe = DECL_THUNKS (thunk); 3055132718Skan probe; probe = TREE_CHAIN (probe)) 3056132718Skan use_thunk (probe, /*emit_p=*/1); 3057132718Skan } 3058132718Skan } 3059132718Skan else 3060169689Skan gcc_assert (!DECL_THUNKS (thunk)); 3061132718Skan } 306290075Sobrien } 306390075Sobrien} 306490075Sobrien 306590075Sobrien/* Generate RTL for FN. */ 306690075Sobrien 306790075Sobrienvoid 3068132718Skanexpand_body (tree fn) 306990075Sobrien{ 3070117395Skan tree saved_function; 3071169689Skan 3072132718Skan /* Compute the appropriate object-file linkage for inline 3073132718Skan functions. */ 3074132718Skan if (DECL_DECLARED_INLINE_P (fn)) 3075132718Skan import_export_decl (fn); 307690075Sobrien 3077132718Skan /* If FN is external, then there's no point in generating RTL for 3078132718Skan it. This situation can arise with an inline function under 3079132718Skan `-fexternal-templates'; we instantiate the function, even though 3080132718Skan we're not planning on emitting it, in case we get a chance to 3081132718Skan inline it. */ 3082132718Skan if (DECL_EXTERNAL (fn)) 3083132718Skan return; 3084132718Skan 3085132718Skan /* ??? When is this needed? */ 3086132718Skan saved_function = current_function_decl; 3087132718Skan 3088132718Skan /* Emit any thunks that should be emitted at the same time as FN. */ 3089132718Skan emit_associated_thunks (fn); 3090132718Skan 3091169689Skan /* This function is only called from cgraph, or recursively from 3092169689Skan emit_associated_thunks. In neither case should we be currently 3093169689Skan generating trees for a function. */ 3094169689Skan gcc_assert (function_depth == 0); 3095132718Skan 3096169689Skan tree_rest_of_compilation (fn); 3097132718Skan 3098132718Skan current_function_decl = saved_function; 3099132718Skan 3100132718Skan if (DECL_CLONED_FUNCTION_P (fn)) 3101132718Skan { 3102132718Skan /* If this is a clone, go through the other clones now and mark 3103169689Skan their parameters used. We have to do that here, as we don't 3104169689Skan know whether any particular clone will be expanded, and 3105169689Skan therefore cannot pick one arbitrarily. */ 3106132718Skan tree probe; 3107132718Skan 3108132718Skan for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn)); 3109132718Skan probe && DECL_CLONED_FUNCTION_P (probe); 3110132718Skan probe = TREE_CHAIN (probe)) 3111132718Skan { 3112132718Skan tree parms; 3113132718Skan 3114132718Skan for (parms = DECL_ARGUMENTS (probe); 3115132718Skan parms; parms = TREE_CHAIN (parms)) 3116132718Skan TREE_USED (parms) = 1; 3117132718Skan } 3118132718Skan } 3119132718Skan} 3120132718Skan 3121132718Skan/* Generate RTL for FN. */ 3122132718Skan 3123132718Skanvoid 3124132718Skanexpand_or_defer_fn (tree fn) 3125132718Skan{ 312690075Sobrien /* When the parser calls us after finishing the body of a template 3127132718Skan function, we don't really want to expand the body. */ 3128132718Skan if (processing_template_decl) 312990075Sobrien { 313090075Sobrien /* Normally, collection only occurs in rest_of_compilation. So, 313190075Sobrien if we don't collect here, we never collect junk generated 313290075Sobrien during the processing of templates until we hit a 3133169689Skan non-template function. It's not safe to do this inside a 3134169689Skan nested class, though, as the parser may have local state that 3135169689Skan is not a GC root. */ 3136169689Skan if (!function_depth) 3137169689Skan ggc_collect (); 313890075Sobrien return; 313990075Sobrien } 314090075Sobrien 314190075Sobrien /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */ 314290075Sobrien walk_tree_without_duplicates (&DECL_SAVED_TREE (fn), 314390075Sobrien simplify_aggr_init_exprs_r, 314490075Sobrien NULL); 314590075Sobrien 314690075Sobrien /* If this is a constructor or destructor body, we have to clone 314790075Sobrien it. */ 314890075Sobrien if (maybe_clone_body (fn)) 314990075Sobrien { 315090075Sobrien /* We don't want to process FN again, so pretend we've written 315190075Sobrien it out, even though we haven't. */ 315290075Sobrien TREE_ASM_WRITTEN (fn) = 1; 315390075Sobrien return; 315490075Sobrien } 315590075Sobrien 3156146895Skan /* If this function is marked with the constructor attribute, add it 3157146895Skan to the list of functions to be called along with constructors 3158146895Skan from static duration objects. */ 3159146895Skan if (DECL_STATIC_CONSTRUCTOR (fn)) 3160146895Skan static_ctors = tree_cons (NULL_TREE, fn, static_ctors); 3161146895Skan 3162146895Skan /* If this function is marked with the destructor attribute, add it 3163146895Skan to the list of functions to be called along with destructors from 3164146895Skan static duration objects. */ 3165146895Skan if (DECL_STATIC_DESTRUCTOR (fn)) 3166146895Skan static_dtors = tree_cons (NULL_TREE, fn, static_dtors); 3167146895Skan 3168169689Skan /* We make a decision about linkage for these functions at the end 3169169689Skan of the compilation. Until that point, we do not want the back 3170169689Skan end to output them -- but we do want it to see the bodies of 3171169689Skan these functions so that it can inline them as appropriate. */ 3172169689Skan if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn)) 3173169689Skan { 3174169689Skan if (DECL_INTERFACE_KNOWN (fn)) 3175169689Skan /* We've already made a decision as to how this function will 3176169689Skan be handled. */; 3177169689Skan else if (!at_eof) 3178169689Skan { 3179169689Skan DECL_EXTERNAL (fn) = 1; 3180169689Skan DECL_NOT_REALLY_EXTERN (fn) = 1; 3181169689Skan note_vague_linkage_fn (fn); 3182169689Skan /* A non-template inline function with external linkage will 3183169689Skan always be COMDAT. As we must eventually determine the 3184169689Skan linkage of all functions, and as that causes writes to 3185169689Skan the data mapped in from the PCH file, it's advantageous 3186169689Skan to mark the functions at this point. */ 3187169689Skan if (!DECL_IMPLICIT_INSTANTIATION (fn)) 3188169689Skan { 3189169689Skan /* This function must have external linkage, as 3190169689Skan otherwise DECL_INTERFACE_KNOWN would have been 3191169689Skan set. */ 3192169689Skan gcc_assert (TREE_PUBLIC (fn)); 3193169689Skan comdat_linkage (fn); 3194169689Skan DECL_INTERFACE_KNOWN (fn) = 1; 3195169689Skan } 3196169689Skan } 3197169689Skan else 3198169689Skan import_export_decl (fn); 3199169689Skan 3200169689Skan /* If the user wants us to keep all inline functions, then mark 3201169689Skan this function as needed so that finish_file will make sure to 3202169689Skan output it later. */ 3203169689Skan if (flag_keep_inline_functions && DECL_DECLARED_INLINE_P (fn)) 3204169689Skan mark_needed (fn); 3205169689Skan } 3206169689Skan 3207169689Skan /* There's no reason to do any of the work here if we're only doing 3208169689Skan semantic analysis; this code just generates RTL. */ 3209169689Skan if (flag_syntax_only) 3210169689Skan return; 3211169689Skan 3212132718Skan function_depth++; 321390075Sobrien 3214132718Skan /* Expand or defer, at the whim of the compilation unit manager. */ 3215132718Skan cgraph_finalize_function (fn, function_depth > 1); 3216117395Skan 3217132718Skan function_depth--; 321890075Sobrien} 321990075Sobrien 3220169689Skanstruct nrv_data 3221169689Skan{ 3222169689Skan tree var; 3223169689Skan tree result; 3224169689Skan htab_t visited; 3225169689Skan}; 322690075Sobrien 3227169689Skan/* Helper function for walk_tree, used by finalize_nrv below. */ 3228169689Skan 3229169689Skanstatic tree 3230169689Skanfinalize_nrv_r (tree* tp, int* walk_subtrees, void* data) 323190075Sobrien{ 3232169689Skan struct nrv_data *dp = (struct nrv_data *)data; 3233169689Skan void **slot; 323490075Sobrien 323590075Sobrien /* No need to walk into types. There wouldn't be any need to walk into 323690075Sobrien non-statements, except that we have to consider STMT_EXPRs. */ 323790075Sobrien if (TYPE_P (*tp)) 323890075Sobrien *walk_subtrees = 0; 3239169689Skan /* Change all returns to just refer to the RESULT_DECL; this is a nop, 3240169689Skan but differs from using NULL_TREE in that it indicates that we care 3241169689Skan about the value of the RESULT_DECL. */ 3242169689Skan else if (TREE_CODE (*tp) == RETURN_EXPR) 3243169689Skan TREE_OPERAND (*tp, 0) = dp->result; 3244169689Skan /* Change all cleanups for the NRV to only run when an exception is 3245169689Skan thrown. */ 324690075Sobrien else if (TREE_CODE (*tp) == CLEANUP_STMT 3247169689Skan && CLEANUP_DECL (*tp) == dp->var) 324896263Sobrien CLEANUP_EH_ONLY (*tp) = 1; 3249169689Skan /* Replace the DECL_EXPR for the NRV with an initialization of the 3250146895Skan RESULT_DECL, if needed. */ 3251169689Skan else if (TREE_CODE (*tp) == DECL_EXPR 3252169689Skan && DECL_EXPR_DECL (*tp) == dp->var) 3253146895Skan { 3254146895Skan tree init; 3255169689Skan if (DECL_INITIAL (dp->var) 3256169689Skan && DECL_INITIAL (dp->var) != error_mark_node) 3257146895Skan { 3258169689Skan init = build2 (INIT_EXPR, void_type_node, dp->result, 3259169689Skan DECL_INITIAL (dp->var)); 3260169689Skan DECL_INITIAL (dp->var) = error_mark_node; 3261146895Skan } 3262146895Skan else 3263169689Skan init = build_empty_stmt (); 3264169689Skan SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp)); 3265146895Skan *tp = init; 3266146895Skan } 3267169689Skan /* And replace all uses of the NRV with the RESULT_DECL. */ 3268169689Skan else if (*tp == dp->var) 3269169689Skan *tp = dp->result; 327090075Sobrien 3271169689Skan /* Avoid walking into the same tree more than once. Unfortunately, we 3272169689Skan can't just use walk_tree_without duplicates because it would only call 3273169689Skan us for the first occurrence of dp->var in the function body. */ 3274169689Skan slot = htab_find_slot (dp->visited, *tp, INSERT); 3275169689Skan if (*slot) 3276169689Skan *walk_subtrees = 0; 3277169689Skan else 3278169689Skan *slot = *tp; 3279169689Skan 328090075Sobrien /* Keep iterating. */ 328190075Sobrien return NULL_TREE; 328290075Sobrien} 328390075Sobrien 3284169689Skan/* Called from finish_function to implement the named return value 3285169689Skan optimization by overriding all the RETURN_EXPRs and pertinent 3286169689Skan CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the 3287169689Skan RESULT_DECL for the function. */ 328890075Sobrien 3289132718Skanvoid 3290169689Skanfinalize_nrv (tree *tp, tree var, tree result) 329190075Sobrien{ 3292169689Skan struct nrv_data data; 3293169689Skan 3294169689Skan /* Copy debugging information from VAR to RESULT. */ 3295169689Skan DECL_NAME (result) = DECL_NAME (var); 3296169689Skan DECL_ARTIFICIAL (result) = DECL_ARTIFICIAL (var); 3297169689Skan DECL_IGNORED_P (result) = DECL_IGNORED_P (var); 3298169689Skan DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var); 3299169689Skan DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var); 3300169689Skan /* Don't forget that we take its address. */ 3301169689Skan TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var); 3302169689Skan 3303169689Skan data.var = var; 3304169689Skan data.result = result; 3305169689Skan data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); 3306169689Skan walk_tree (tp, finalize_nrv_r, &data, 0); 3307169689Skan htab_delete (data.visited); 330890075Sobrien} 3309169689Skan 3310169689Skan/* For all elements of CLAUSES, validate them vs OpenMP constraints. 3311169689Skan Remove any elements from the list that are invalid. */ 331290075Sobrien 3313169689Skantree 3314169689Skanfinish_omp_clauses (tree clauses) 3315169689Skan{ 3316169689Skan bitmap_head generic_head, firstprivate_head, lastprivate_head; 3317169689Skan tree c, t, *pc = &clauses; 3318169689Skan const char *name; 331990075Sobrien 3320169689Skan bitmap_obstack_initialize (NULL); 3321169689Skan bitmap_initialize (&generic_head, &bitmap_default_obstack); 3322169689Skan bitmap_initialize (&firstprivate_head, &bitmap_default_obstack); 3323169689Skan bitmap_initialize (&lastprivate_head, &bitmap_default_obstack); 3324169689Skan 3325169689Skan for (pc = &clauses, c = clauses; c ; c = *pc) 3326169689Skan { 3327169689Skan bool remove = false; 3328169689Skan 3329169689Skan switch (OMP_CLAUSE_CODE (c)) 3330169689Skan { 3331169689Skan case OMP_CLAUSE_SHARED: 3332169689Skan name = "shared"; 3333169689Skan goto check_dup_generic; 3334169689Skan case OMP_CLAUSE_PRIVATE: 3335169689Skan name = "private"; 3336169689Skan goto check_dup_generic; 3337169689Skan case OMP_CLAUSE_REDUCTION: 3338169689Skan name = "reduction"; 3339169689Skan goto check_dup_generic; 3340169689Skan case OMP_CLAUSE_COPYPRIVATE: 3341169689Skan name = "copyprivate"; 3342169689Skan goto check_dup_generic; 3343169689Skan case OMP_CLAUSE_COPYIN: 3344169689Skan name = "copyin"; 3345169689Skan goto check_dup_generic; 3346169689Skan check_dup_generic: 3347169689Skan t = OMP_CLAUSE_DECL (c); 3348169689Skan if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3349169689Skan { 3350169689Skan if (processing_template_decl) 3351169689Skan break; 3352171825Skan if (DECL_P (t)) 3353171825Skan error ("%qD is not a variable in clause %qs", t, name); 3354171825Skan else 3355171825Skan error ("%qE is not a variable in clause %qs", t, name); 3356169689Skan remove = true; 3357169689Skan } 3358169689Skan else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 3359169689Skan || bitmap_bit_p (&firstprivate_head, DECL_UID (t)) 3360169689Skan || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 3361169689Skan { 3362171825Skan error ("%qD appears more than once in data clauses", t); 3363169689Skan remove = true; 3364169689Skan } 3365169689Skan else 3366169689Skan bitmap_set_bit (&generic_head, DECL_UID (t)); 3367169689Skan break; 3368169689Skan 3369169689Skan case OMP_CLAUSE_FIRSTPRIVATE: 3370169689Skan t = OMP_CLAUSE_DECL (c); 3371169689Skan if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3372169689Skan { 3373169689Skan if (processing_template_decl) 3374169689Skan break; 3375169689Skan error ("%qE is not a variable in clause %<firstprivate%>", t); 3376169689Skan remove = true; 3377169689Skan } 3378169689Skan else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 3379169689Skan || bitmap_bit_p (&firstprivate_head, DECL_UID (t))) 3380169689Skan { 3381169689Skan error ("%qE appears more than once in data clauses", t); 3382169689Skan remove = true; 3383169689Skan } 3384169689Skan else 3385169689Skan bitmap_set_bit (&firstprivate_head, DECL_UID (t)); 3386169689Skan break; 3387169689Skan 3388169689Skan case OMP_CLAUSE_LASTPRIVATE: 3389169689Skan t = OMP_CLAUSE_DECL (c); 3390169689Skan if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3391169689Skan { 3392169689Skan if (processing_template_decl) 3393169689Skan break; 3394169689Skan error ("%qE is not a variable in clause %<lastprivate%>", t); 3395169689Skan remove = true; 3396169689Skan } 3397169689Skan else if (bitmap_bit_p (&generic_head, DECL_UID (t)) 3398169689Skan || bitmap_bit_p (&lastprivate_head, DECL_UID (t))) 3399169689Skan { 3400169689Skan error ("%qE appears more than once in data clauses", t); 3401169689Skan remove = true; 3402169689Skan } 3403169689Skan else 3404169689Skan bitmap_set_bit (&lastprivate_head, DECL_UID (t)); 3405169689Skan break; 3406169689Skan 3407169689Skan case OMP_CLAUSE_IF: 3408169689Skan t = OMP_CLAUSE_IF_EXPR (c); 3409169689Skan t = maybe_convert_cond (t); 3410169689Skan if (t == error_mark_node) 3411169689Skan remove = true; 3412169689Skan OMP_CLAUSE_IF_EXPR (c) = t; 3413169689Skan break; 3414169689Skan 3415169689Skan case OMP_CLAUSE_NUM_THREADS: 3416169689Skan t = OMP_CLAUSE_NUM_THREADS_EXPR (c); 3417169689Skan if (t == error_mark_node) 3418169689Skan remove = true; 3419169689Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) 3420169689Skan && !type_dependent_expression_p (t)) 3421169689Skan { 3422169689Skan error ("num_threads expression must be integral"); 3423169689Skan remove = true; 3424169689Skan } 3425169689Skan break; 3426169689Skan 3427169689Skan case OMP_CLAUSE_SCHEDULE: 3428169689Skan t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c); 3429169689Skan if (t == NULL) 3430169689Skan ; 3431169689Skan else if (t == error_mark_node) 3432169689Skan remove = true; 3433169689Skan else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)) 3434169689Skan && !type_dependent_expression_p (t)) 3435169689Skan { 3436169689Skan error ("schedule chunk size expression must be integral"); 3437169689Skan remove = true; 3438169689Skan } 3439169689Skan break; 3440169689Skan 3441169689Skan case OMP_CLAUSE_NOWAIT: 3442169689Skan case OMP_CLAUSE_ORDERED: 3443169689Skan case OMP_CLAUSE_DEFAULT: 3444169689Skan break; 3445169689Skan 3446169689Skan default: 3447169689Skan gcc_unreachable (); 3448169689Skan } 3449169689Skan 3450169689Skan if (remove) 3451169689Skan *pc = OMP_CLAUSE_CHAIN (c); 3452169689Skan else 3453169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3454169689Skan } 3455169689Skan 3456169689Skan for (pc = &clauses, c = clauses; c ; c = *pc) 3457169689Skan { 3458169689Skan enum tree_code c_kind = OMP_CLAUSE_CODE (c); 3459169689Skan bool remove = false; 3460169689Skan bool need_complete_non_reference = false; 3461169689Skan bool need_default_ctor = false; 3462169689Skan bool need_copy_ctor = false; 3463169689Skan bool need_copy_assignment = false; 3464169689Skan bool need_implicitly_determined = false; 3465169689Skan tree type, inner_type; 3466169689Skan 3467169689Skan switch (c_kind) 3468169689Skan { 3469169689Skan case OMP_CLAUSE_SHARED: 3470169689Skan name = "shared"; 3471169689Skan need_implicitly_determined = true; 3472169689Skan break; 3473169689Skan case OMP_CLAUSE_PRIVATE: 3474169689Skan name = "private"; 3475169689Skan need_complete_non_reference = true; 3476169689Skan need_default_ctor = true; 3477169689Skan need_implicitly_determined = true; 3478169689Skan break; 3479169689Skan case OMP_CLAUSE_FIRSTPRIVATE: 3480169689Skan name = "firstprivate"; 3481169689Skan need_complete_non_reference = true; 3482169689Skan need_copy_ctor = true; 3483169689Skan need_implicitly_determined = true; 3484169689Skan break; 3485169689Skan case OMP_CLAUSE_LASTPRIVATE: 3486169689Skan name = "lastprivate"; 3487169689Skan need_complete_non_reference = true; 3488169689Skan need_copy_assignment = true; 3489169689Skan need_implicitly_determined = true; 3490169689Skan break; 3491169689Skan case OMP_CLAUSE_REDUCTION: 3492169689Skan name = "reduction"; 3493169689Skan need_implicitly_determined = true; 3494169689Skan break; 3495169689Skan case OMP_CLAUSE_COPYPRIVATE: 3496169689Skan name = "copyprivate"; 3497169689Skan need_copy_assignment = true; 3498169689Skan break; 3499169689Skan case OMP_CLAUSE_COPYIN: 3500169689Skan name = "copyin"; 3501169689Skan need_copy_assignment = true; 3502169689Skan break; 3503169689Skan default: 3504169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3505169689Skan continue; 3506169689Skan } 3507169689Skan 3508169689Skan t = OMP_CLAUSE_DECL (c); 3509169689Skan if (processing_template_decl 3510169689Skan && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) 3511169689Skan { 3512169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3513169689Skan continue; 3514169689Skan } 3515169689Skan 3516169689Skan switch (c_kind) 3517169689Skan { 3518169689Skan case OMP_CLAUSE_LASTPRIVATE: 3519169689Skan if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t))) 3520169689Skan need_default_ctor = true; 3521169689Skan break; 3522169689Skan 3523169689Skan case OMP_CLAUSE_REDUCTION: 3524169689Skan if (AGGREGATE_TYPE_P (TREE_TYPE (t)) 3525169689Skan || POINTER_TYPE_P (TREE_TYPE (t))) 3526169689Skan { 3527169689Skan error ("%qE has invalid type for %<reduction%>", t); 3528169689Skan remove = true; 3529169689Skan } 3530169689Skan else if (FLOAT_TYPE_P (TREE_TYPE (t))) 3531169689Skan { 3532169689Skan enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c); 3533169689Skan switch (r_code) 3534169689Skan { 3535169689Skan case PLUS_EXPR: 3536169689Skan case MULT_EXPR: 3537169689Skan case MINUS_EXPR: 3538169689Skan break; 3539169689Skan default: 3540169689Skan error ("%qE has invalid type for %<reduction(%s)%>", 3541169689Skan t, operator_name_info[r_code].name); 3542169689Skan remove = true; 3543169689Skan } 3544169689Skan } 3545169689Skan break; 3546169689Skan 3547169689Skan case OMP_CLAUSE_COPYIN: 3548169689Skan if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t)) 3549169689Skan { 3550169689Skan error ("%qE must be %<threadprivate%> for %<copyin%>", t); 3551169689Skan remove = true; 3552169689Skan } 3553169689Skan break; 3554169689Skan 3555169689Skan default: 3556169689Skan break; 3557169689Skan } 3558169689Skan 3559169689Skan if (need_complete_non_reference) 3560169689Skan { 3561169689Skan t = require_complete_type (t); 3562169689Skan if (t == error_mark_node) 3563169689Skan remove = true; 3564169689Skan else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE) 3565169689Skan { 3566169689Skan error ("%qE has reference type for %qs", t, name); 3567169689Skan remove = true; 3568169689Skan } 3569169689Skan } 3570169689Skan if (need_implicitly_determined) 3571169689Skan { 3572169689Skan const char *share_name = NULL; 3573169689Skan 3574169689Skan if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t)) 3575169689Skan share_name = "threadprivate"; 3576169689Skan else switch (cxx_omp_predetermined_sharing (t)) 3577169689Skan { 3578169689Skan case OMP_CLAUSE_DEFAULT_UNSPECIFIED: 3579169689Skan break; 3580169689Skan case OMP_CLAUSE_DEFAULT_SHARED: 3581169689Skan share_name = "shared"; 3582169689Skan break; 3583169689Skan case OMP_CLAUSE_DEFAULT_PRIVATE: 3584169689Skan share_name = "private"; 3585169689Skan break; 3586169689Skan default: 3587169689Skan gcc_unreachable (); 3588169689Skan } 3589169689Skan if (share_name) 3590169689Skan { 3591169689Skan error ("%qE is predetermined %qs for %qs", 3592169689Skan t, share_name, name); 3593169689Skan remove = true; 3594169689Skan } 3595169689Skan } 3596169689Skan 3597169689Skan /* We're interested in the base element, not arrays. */ 3598169689Skan inner_type = type = TREE_TYPE (t); 3599169689Skan while (TREE_CODE (inner_type) == ARRAY_TYPE) 3600169689Skan inner_type = TREE_TYPE (inner_type); 3601169689Skan 3602169689Skan /* Check for special function availability by building a call to one. 3603169689Skan Save the results, because later we won't be in the right context 3604169689Skan for making these queries. */ 3605169689Skan if (CLASS_TYPE_P (inner_type) 3606169689Skan && (need_default_ctor || need_copy_ctor || need_copy_assignment) 3607169689Skan && !type_dependent_expression_p (t)) 3608169689Skan { 3609169689Skan int save_errorcount = errorcount; 3610169689Skan tree info; 3611169689Skan 3612169689Skan /* Always allocate 3 elements for simplicity. These are the 3613169689Skan function decls for the ctor, dtor, and assignment op. 3614169689Skan This layout is known to the three lang hooks, 3615169689Skan cxx_omp_clause_default_init, cxx_omp_clause_copy_init, 3616169689Skan and cxx_omp_clause_assign_op. */ 3617169689Skan info = make_tree_vec (3); 3618169689Skan CP_OMP_CLAUSE_INFO (c) = info; 3619169689Skan 3620169689Skan if (need_default_ctor 3621169689Skan || (need_copy_ctor 3622169689Skan && !TYPE_HAS_TRIVIAL_INIT_REF (inner_type))) 3623169689Skan { 3624169689Skan if (need_default_ctor) 3625169689Skan t = NULL; 3626169689Skan else 3627169689Skan { 3628169689Skan t = build_int_cst (build_pointer_type (inner_type), 0); 3629169689Skan t = build1 (INDIRECT_REF, inner_type, t); 3630169689Skan t = build_tree_list (NULL, t); 3631169689Skan } 3632169689Skan t = build_special_member_call (NULL_TREE, 3633169689Skan complete_ctor_identifier, 3634169689Skan t, inner_type, LOOKUP_NORMAL); 3635169689Skan t = get_callee_fndecl (t); 3636169689Skan TREE_VEC_ELT (info, 0) = t; 3637169689Skan } 3638169689Skan 3639169689Skan if ((need_default_ctor || need_copy_ctor) 3640169689Skan && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_type)) 3641169689Skan { 3642169689Skan t = build_int_cst (build_pointer_type (inner_type), 0); 3643169689Skan t = build1 (INDIRECT_REF, inner_type, t); 3644169689Skan t = build_special_member_call (t, complete_dtor_identifier, 3645169689Skan NULL, inner_type, LOOKUP_NORMAL); 3646169689Skan t = get_callee_fndecl (t); 3647169689Skan TREE_VEC_ELT (info, 1) = t; 3648169689Skan } 3649169689Skan 3650169689Skan if (need_copy_assignment 3651169689Skan && !TYPE_HAS_TRIVIAL_ASSIGN_REF (inner_type)) 3652169689Skan { 3653169689Skan t = build_int_cst (build_pointer_type (inner_type), 0); 3654169689Skan t = build1 (INDIRECT_REF, inner_type, t); 3655169689Skan t = build_special_member_call (t, ansi_assopname (NOP_EXPR), 3656169689Skan build_tree_list (NULL, t), 3657169689Skan inner_type, LOOKUP_NORMAL); 3658169689Skan 3659169689Skan /* We'll have called convert_from_reference on the call, which 3660169689Skan may well have added an indirect_ref. It's unneeded here, 3661169689Skan and in the way, so kill it. */ 3662169689Skan if (TREE_CODE (t) == INDIRECT_REF) 3663169689Skan t = TREE_OPERAND (t, 0); 3664169689Skan 3665169689Skan t = get_callee_fndecl (t); 3666169689Skan TREE_VEC_ELT (info, 2) = t; 3667169689Skan } 3668169689Skan 3669169689Skan if (errorcount != save_errorcount) 3670169689Skan remove = true; 3671169689Skan } 3672169689Skan 3673169689Skan if (remove) 3674169689Skan *pc = OMP_CLAUSE_CHAIN (c); 3675169689Skan else 3676169689Skan pc = &OMP_CLAUSE_CHAIN (c); 3677169689Skan } 3678169689Skan 3679169689Skan bitmap_obstack_release (NULL); 3680169689Skan return clauses; 3681169689Skan} 3682169689Skan 3683169689Skan/* For all variables in the tree_list VARS, mark them as thread local. */ 3684169689Skan 368590075Sobrienvoid 3686169689Skanfinish_omp_threadprivate (tree vars) 3687169689Skan{ 3688169689Skan tree t; 3689169689Skan 3690169689Skan /* Mark every variable in VARS to be assigned thread local storage. */ 3691169689Skan for (t = vars; t; t = TREE_CHAIN (t)) 3692169689Skan { 3693169689Skan tree v = TREE_PURPOSE (t); 3694169689Skan 3695169689Skan /* If V had already been marked threadprivate, it doesn't matter 3696169689Skan whether it had been used prior to this point. */ 3697169689Skan if (TREE_USED (v) 3698169689Skan && (DECL_LANG_SPECIFIC (v) == NULL 3699169689Skan || !CP_DECL_THREADPRIVATE_P (v))) 3700169689Skan error ("%qE declared %<threadprivate%> after first use", v); 3701169689Skan else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v)) 3702169689Skan error ("automatic variable %qE cannot be %<threadprivate%>", v); 3703169689Skan else if (! COMPLETE_TYPE_P (TREE_TYPE (v))) 3704169689Skan error ("%<threadprivate%> %qE has incomplete type", v); 3705169689Skan else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))) 3706169689Skan error ("%<threadprivate%> %qE is not file, namespace " 3707169689Skan "or block scope variable", v); 3708169689Skan else 3709169689Skan { 3710169689Skan /* Allocate a LANG_SPECIFIC structure for V, if needed. */ 3711169689Skan if (DECL_LANG_SPECIFIC (v) == NULL) 3712169689Skan { 3713169689Skan retrofit_lang_decl (v); 3714169689Skan 3715169689Skan /* Make sure that DECL_DISCRIMINATOR_P continues to be true 3716169689Skan after the allocation of the lang_decl structure. */ 3717169689Skan if (DECL_DISCRIMINATOR_P (v)) 3718169689Skan DECL_LANG_SPECIFIC (v)->decl_flags.u2sel = 1; 3719169689Skan } 3720169689Skan 3721169689Skan if (! DECL_THREAD_LOCAL_P (v)) 3722169689Skan { 3723169689Skan DECL_TLS_MODEL (v) = decl_default_tls_model (v); 3724169689Skan /* If rtl has been already set for this var, call 3725169689Skan make_decl_rtl once again, so that encode_section_info 3726169689Skan has a chance to look at the new decl flags. */ 3727169689Skan if (DECL_RTL_SET_P (v)) 3728169689Skan make_decl_rtl (v); 3729169689Skan } 3730169689Skan CP_DECL_THREADPRIVATE_P (v) = 1; 3731169689Skan } 3732169689Skan } 3733169689Skan} 3734169689Skan 3735169689Skan/* Build an OpenMP structured block. */ 3736169689Skan 3737169689Skantree 3738169689Skanbegin_omp_structured_block (void) 3739169689Skan{ 3740169689Skan return do_pushlevel (sk_omp); 3741169689Skan} 3742169689Skan 3743169689Skantree 3744169689Skanfinish_omp_structured_block (tree block) 3745169689Skan{ 3746169689Skan return do_poplevel (block); 3747169689Skan} 3748169689Skan 3749169689Skan/* Similarly, except force the retention of the BLOCK. */ 3750169689Skan 3751169689Skantree 3752169689Skanbegin_omp_parallel (void) 3753169689Skan{ 3754169689Skan keep_next_level (true); 3755169689Skan return begin_omp_structured_block (); 3756169689Skan} 3757169689Skan 3758169689Skantree 3759169689Skanfinish_omp_parallel (tree clauses, tree body) 3760169689Skan{ 3761169689Skan tree stmt; 3762169689Skan 3763169689Skan body = finish_omp_structured_block (body); 3764169689Skan 3765169689Skan stmt = make_node (OMP_PARALLEL); 3766169689Skan TREE_TYPE (stmt) = void_type_node; 3767169689Skan OMP_PARALLEL_CLAUSES (stmt) = clauses; 3768169689Skan OMP_PARALLEL_BODY (stmt) = body; 3769169689Skan 3770169689Skan return add_stmt (stmt); 3771169689Skan} 3772169689Skan 3773169689Skan/* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR 3774169689Skan are directly for their associated operands in the statement. DECL 3775169689Skan and INIT are a combo; if DECL is NULL then INIT ought to be a 3776169689Skan MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are 3777169689Skan optional statements that need to go before the loop into its 3778169689Skan sk_omp scope. */ 3779169689Skan 3780169689Skantree 3781169689Skanfinish_omp_for (location_t locus, tree decl, tree init, tree cond, 3782169689Skan tree incr, tree body, tree pre_body) 3783169689Skan{ 3784169689Skan if (decl == NULL) 3785169689Skan { 3786169689Skan if (init != NULL) 3787169689Skan switch (TREE_CODE (init)) 3788169689Skan { 3789169689Skan case MODIFY_EXPR: 3790169689Skan decl = TREE_OPERAND (init, 0); 3791169689Skan init = TREE_OPERAND (init, 1); 3792169689Skan break; 3793169689Skan case MODOP_EXPR: 3794169689Skan if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR) 3795169689Skan { 3796169689Skan decl = TREE_OPERAND (init, 0); 3797169689Skan init = TREE_OPERAND (init, 2); 3798169689Skan } 3799169689Skan break; 3800169689Skan default: 3801169689Skan break; 3802169689Skan } 3803169689Skan 3804169689Skan if (decl == NULL) 3805169689Skan { 3806169689Skan error ("expected iteration declaration or initialization"); 3807169689Skan return NULL; 3808169689Skan } 3809169689Skan } 3810169689Skan 3811169689Skan if (type_dependent_expression_p (decl) 3812169689Skan || type_dependent_expression_p (init) 3813169689Skan || (cond && type_dependent_expression_p (cond)) 3814169689Skan || (incr && type_dependent_expression_p (incr))) 3815169689Skan { 3816169689Skan tree stmt; 3817169689Skan 3818169689Skan if (cond == NULL) 3819169689Skan { 3820169689Skan error ("%Hmissing controlling predicate", &locus); 3821169689Skan return NULL; 3822169689Skan } 3823169689Skan 3824169689Skan if (incr == NULL) 3825169689Skan { 3826169689Skan error ("%Hmissing increment expression", &locus); 3827169689Skan return NULL; 3828169689Skan } 3829169689Skan 3830169689Skan stmt = make_node (OMP_FOR); 3831169689Skan 3832169689Skan /* This is really just a place-holder. We'll be decomposing this 3833169689Skan again and going through the build_modify_expr path below when 3834169689Skan we instantiate the thing. */ 3835169689Skan init = build2 (MODIFY_EXPR, void_type_node, decl, init); 3836169689Skan 3837169689Skan TREE_TYPE (stmt) = void_type_node; 3838169689Skan OMP_FOR_INIT (stmt) = init; 3839169689Skan OMP_FOR_COND (stmt) = cond; 3840169689Skan OMP_FOR_INCR (stmt) = incr; 3841169689Skan OMP_FOR_BODY (stmt) = body; 3842169689Skan OMP_FOR_PRE_BODY (stmt) = pre_body; 3843169689Skan 3844169689Skan SET_EXPR_LOCATION (stmt, locus); 3845169689Skan return add_stmt (stmt); 3846169689Skan } 3847169689Skan 3848169689Skan if (!DECL_P (decl)) 3849169689Skan { 3850169689Skan error ("expected iteration declaration or initialization"); 3851169689Skan return NULL; 3852169689Skan } 3853169689Skan 3854169689Skan if (pre_body == NULL || IS_EMPTY_STMT (pre_body)) 3855169689Skan pre_body = NULL; 3856169689Skan else if (! processing_template_decl) 3857169689Skan { 3858169689Skan add_stmt (pre_body); 3859169689Skan pre_body = NULL; 3860169689Skan } 3861169689Skan init = build_modify_expr (decl, NOP_EXPR, init); 3862169689Skan return c_finish_omp_for (locus, decl, init, cond, incr, body, pre_body); 3863169689Skan} 3864169689Skan 3865169689Skanvoid 3866169689Skanfinish_omp_atomic (enum tree_code code, tree lhs, tree rhs) 3867169689Skan{ 3868169689Skan tree orig_lhs; 3869169689Skan tree orig_rhs; 3870169689Skan bool dependent_p; 3871169689Skan tree stmt; 3872169689Skan 3873169689Skan orig_lhs = lhs; 3874169689Skan orig_rhs = rhs; 3875169689Skan dependent_p = false; 3876169689Skan stmt = NULL_TREE; 3877169689Skan 3878169689Skan /* Even in a template, we can detect invalid uses of the atomic 3879169689Skan pragma if neither LHS nor RHS is type-dependent. */ 3880169689Skan if (processing_template_decl) 3881169689Skan { 3882169689Skan dependent_p = (type_dependent_expression_p (lhs) 3883169689Skan || type_dependent_expression_p (rhs)); 3884169689Skan if (!dependent_p) 3885169689Skan { 3886169689Skan lhs = build_non_dependent_expr (lhs); 3887169689Skan rhs = build_non_dependent_expr (rhs); 3888169689Skan } 3889169689Skan } 3890169689Skan if (!dependent_p) 3891169689Skan { 3892169689Skan stmt = c_finish_omp_atomic (code, lhs, rhs); 3893169689Skan if (stmt == error_mark_node) 3894169689Skan return; 3895169689Skan } 3896169689Skan if (processing_template_decl) 3897169689Skan { 3898169689Skan stmt = build2 (OMP_ATOMIC, void_type_node, orig_lhs, orig_rhs); 3899169689Skan OMP_ATOMIC_DEPENDENT_P (stmt) = 1; 3900169689Skan OMP_ATOMIC_CODE (stmt) = code; 3901169689Skan } 3902169689Skan add_stmt (stmt); 3903169689Skan} 3904169689Skan 3905169689Skanvoid 3906169689Skanfinish_omp_barrier (void) 3907169689Skan{ 3908169689Skan tree fn = built_in_decls[BUILT_IN_GOMP_BARRIER]; 3909169689Skan tree stmt = finish_call_expr (fn, NULL, false, false); 3910169689Skan finish_expr_stmt (stmt); 3911169689Skan} 3912169689Skan 3913169689Skanvoid 3914169689Skanfinish_omp_flush (void) 3915169689Skan{ 3916169689Skan tree fn = built_in_decls[BUILT_IN_SYNCHRONIZE]; 3917169689Skan tree stmt = finish_call_expr (fn, NULL, false, false); 3918169689Skan finish_expr_stmt (stmt); 3919169689Skan} 3920169689Skan 3921169689Skan/* True if OpenMP sharing attribute of DECL is predetermined. */ 3922169689Skan 3923169689Skanenum omp_clause_default_kind 3924169689Skancxx_omp_predetermined_sharing (tree decl) 3925169689Skan{ 3926169689Skan enum omp_clause_default_kind kind; 3927169689Skan 3928169689Skan kind = c_omp_predetermined_sharing (decl); 3929169689Skan if (kind != OMP_CLAUSE_DEFAULT_UNSPECIFIED) 3930169689Skan return kind; 3931169689Skan 3932169689Skan /* Static data members are predetermined as shared. */ 3933169689Skan if (TREE_STATIC (decl)) 3934169689Skan { 3935169689Skan tree ctx = CP_DECL_CONTEXT (decl); 3936169689Skan if (TYPE_P (ctx) && IS_AGGR_TYPE (ctx)) 3937169689Skan return OMP_CLAUSE_DEFAULT_SHARED; 3938169689Skan } 3939169689Skan 3940169689Skan return OMP_CLAUSE_DEFAULT_UNSPECIFIED; 3941169689Skan} 3942169689Skan 3943169689Skanvoid 3944132718Skaninit_cp_semantics (void) 394590075Sobrien{ 394690075Sobrien} 3947132718Skan 3948132718Skan#include "gt-cp-semantics.h" 3949