semantics.c revision 52284
1139743Simp/* Perform the semantic phase of parsing, i.e., the process of 239212Sgibbs building tree structure, checking semantic consistency, and 339212Sgibbs building RTL. These routines are used both during actual parsing 439212Sgibbs and during the instantiation of template functions. 539212Sgibbs 639212Sgibbs Copyright (C) 1998, 1999 Free Software Foundation, Inc. 739212Sgibbs Written by Mark Mitchell (mmitchell@usa.net) based on code found 839212Sgibbs formerly in parse.y and pt.c. 939212Sgibbs 1039212Sgibbs This file is part of GNU CC. 1139212Sgibbs 1239212Sgibbs GNU CC is free software; you can redistribute it and/or modify it 1339212Sgibbs under the terms of the GNU General Public License as published by 1439212Sgibbs the Free Software Foundation; either version 2, or (at your option) 1539212Sgibbs any later version. 1639212Sgibbs 1739212Sgibbs GNU CC is distributed in the hope that it will be useful, but 1839212Sgibbs WITHOUT ANY WARRANTY; without even the implied warranty of 1939212Sgibbs MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 2039212Sgibbs General Public License for more details. 2139212Sgibbs 2239212Sgibbs You should have received a copy of the GNU General Public License 2339212Sgibbs along with GNU CC; see the file COPYING. If not, write to the Free 2439212Sgibbs Software Foundation, 59 Temple Place - Suite 330, Boston, MA 2539212Sgibbs 02111-1307, USA. */ 2639212Sgibbs 2739212Sgibbs#include "config.h" 2850477Speter#include "system.h" 2939212Sgibbs#include "tree.h" 3039212Sgibbs#include "cp-tree.h" 3139212Sgibbs#include "except.h" 3239212Sgibbs#include "lex.h" 3339212Sgibbs#include "toplev.h" 3439212Sgibbs 3539212Sgibbs/* There routines provide a modular interface to perform many parsing 3647412Sgibbs operations. They may therefore be used during actual parsing, or 37114216Skan during template instantiation, which may be regarded as a 3855206Speter degenerate form of parsing. Since the current g++ parser is 3939212Sgibbs lacking in several respects, and will be reimplemented, we are 4039212Sgibbs attempting to move most code that is not directly related to 4139212Sgibbs parsing into this file; that will make implementing the new parser 4239212Sgibbs much easier since it will be able to make use of these routines. */ 43195534Sscottl 4439212Sgibbs/* When parsing a template, LAST_TREE contains the last statement 4539212Sgibbs parsed. These are chained together through the TREE_CHAIN field, 4639212Sgibbs but often need to be re-organized since the parse is performed 4739212Sgibbs bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of 4839212Sgibbs STMT. */ 4939212Sgibbs 5039212Sgibbs#define RECHAIN_STMTS(stmt, substmt, last) \ 5139212Sgibbs do { \ 5239212Sgibbs substmt = last; \ 5339212Sgibbs TREE_CHAIN (stmt) = NULL_TREE; \ 5439212Sgibbs last_tree = stmt; \ 5539212Sgibbs } while (0) 5639212Sgibbs 5739212Sgibbs#define RECHAIN_STMTS_FROM_LAST(stmt, substmt) \ 5839212Sgibbs RECHAIN_STMTS (stmt, substmt, last_tree) 5939212Sgibbs 6039212Sgibbs#define RECHAIN_STMTS_FROM_CHAIN(stmt, substmt) \ 6139212Sgibbs RECHAIN_STMTS (stmt, substmt, TREE_CHAIN (stmt)) 6263455Sgibbs 6363455Sgibbs/* Finish an expression-statement, whose EXPRESSION is as indicated. */ 6463455Sgibbs 6563455Sgibbsvoid 66251986Sscottlfinish_expr_stmt (expr) 6739212Sgibbs tree expr; 68216088Sken{ 6939212Sgibbs if (expr != NULL_TREE) 7039212Sgibbs { 7139212Sgibbs if (!processing_template_decl) 7239212Sgibbs { 73251874Sscottl emit_line_note (input_filename, lineno); 74251986Sscottl /* Do default conversion if safe and possibly important, 75251986Sscottl in case within ({...}). */ 76251986Sscottl if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE 77251986Sscottl && lvalue_p (expr)) 78251874Sscottl || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE) 7939212Sgibbs expr = default_conversion (expr); 8039212Sgibbs } 8139212Sgibbs 8239212Sgibbs cplus_expand_expr_stmt (expr); 8339212Sgibbs clear_momentary (); 8439212Sgibbs } 8539212Sgibbs 8639212Sgibbs finish_stmt (); 8739212Sgibbs} 8839212Sgibbs 8939212Sgibbs/* Begin an if-statement. Returns a newly created IF_STMT if 9039212Sgibbs appropriate. */ 9139212Sgibbs 9239212Sgibbstree 9339212Sgibbsbegin_if_stmt () 94251986Sscottl{ 95251986Sscottl tree r; 96251986Sscottl 97251986Sscottl if (processing_template_decl) 98251986Sscottl { 9939212Sgibbs r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE); 10058111Sn_hibma add_tree (r); 10139212Sgibbs } 10239212Sgibbs else 10339212Sgibbs r = NULL_TREE; 10456145Smjacob 10556145Smjacob do_pushlevel (); 10656145Smjacob 107251897Sscottl return r; 10839212Sgibbs} 10956145Smjacob 11056145Smjacob/* Process the COND of an if-statement, which may be given by 11156145Smjacob IF_STMT. */ 11256145Smjacob 11339212Sgibbsvoid 11439212Sgibbsfinish_if_stmt_cond (cond, if_stmt) 11539212Sgibbs tree cond; 11639212Sgibbs tree if_stmt; 11746581Sken{ 11846581Sken if (processing_template_decl) 11946581Sken { 12046581Sken if (last_tree != if_stmt) 12146581Sken RECHAIN_STMTS_FROM_LAST (if_stmt, IF_COND (if_stmt)); 12246581Sken else 12349925Sgibbs IF_COND (if_stmt) = copy_to_permanent (cond); 12449925Sgibbs } 12539212Sgibbs else 12646581Sken { 12746581Sken emit_line_note (input_filename, lineno); 12849925Sgibbs expand_start_cond (condition_conversion (cond), 0); 12946581Sken } 13046581Sken} 13146581Sken 13246581Sken/* Finish the then-clause of an if-statement, which may be given by 13346581Sken IF_STMT. */ 13446581Sken 13546581Skentree 13646581Skenfinish_then_clause (if_stmt) 137203108Smav tree if_stmt; 13846581Sken{ 13946581Sken if (processing_template_decl) 14046581Sken { 14146581Sken RECHAIN_STMTS_FROM_CHAIN (if_stmt, 14246581Sken THEN_CLAUSE (if_stmt)); 14346581Sken last_tree = if_stmt; 14446581Sken return if_stmt; 14546581Sken } 14646581Sken else 14746581Sken return NULL_TREE; 14846581Sken} 14947412Sgibbs 15047412Sgibbs/* Begin the else-clause of an if-statement. */ 15147412Sgibbs 15247412Sgibbsvoid 153223081Sgibbsbegin_else_clause () 154223081Sgibbs{ 15539212Sgibbs if (!processing_template_decl) 15646581Sken expand_start_else (); 15746581Sken} 15846581Sken 15946581Sken/* Finish the else-clause of an if-statement, which may be given by 16049925Sgibbs IF_STMT. */ 16146581Sken 16246581Skenvoid 16346581Skenfinish_else_clause (if_stmt) 16446581Sken tree if_stmt; 16546581Sken{ 16646581Sken if (processing_template_decl) 16746581Sken RECHAIN_STMTS_FROM_CHAIN (if_stmt, ELSE_CLAUSE (if_stmt)); 16846581Sken} 16939212Sgibbs 17039212Sgibbs/* Finsh an if-statement. */ 17139212Sgibbs 17246581Skenvoid 17346581Skenfinish_if_stmt () 17439212Sgibbs{ 17539212Sgibbs if (!processing_template_decl) 17639212Sgibbs expand_end_cond (); 17746581Sken 17846581Sken do_poplevel (); 17939212Sgibbs finish_stmt (); 18039212Sgibbs} 18139212Sgibbs 18239212Sgibbs/* Begin a while-statement. Returns a newly created WHILE_STMT if 183195534Sscottl appropriate. */ 184195534Sscottl 18539212Sgibbstree 186196008Smjacobbegin_while_stmt () 187196008Smjacob{ 188196008Smjacob tree r; 189196008Smjacob 190196008Smjacob if (processing_template_decl) 191196008Smjacob { 192196008Smjacob r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE); 193196008Smjacob add_tree (r); 194196008Smjacob } 195208582Smjacob else 196216088Sken { 197216088Sken emit_nop (); 198216088Sken emit_line_note (input_filename, lineno); 199208582Smjacob expand_start_loop (1); 200208582Smjacob r = NULL_TREE; 201208582Smjacob } 202208582Smjacob 20339212Sgibbs do_pushlevel (); 20446581Sken 20546581Sken return r; 20674840Sken} 20746581Sken 20839212Sgibbs/* Process the COND of an if-statement, which may be given by 20939212Sgibbs WHILE_STMT. */ 21046581Sken 21146581Skenvoid 21249925Sgibbsfinish_while_stmt_cond (cond, while_stmt) 21346581Sken tree cond; 21446581Sken tree while_stmt; 21546581Sken{ 21649925Sgibbs if (processing_template_decl) 21746581Sken { 21846581Sken if (last_tree != while_stmt) 219196008Smjacob RECHAIN_STMTS_FROM_LAST (while_stmt, 220196008Smjacob WHILE_COND (while_stmt)); 221196008Smjacob else 222196008Smjacob TREE_OPERAND (while_stmt, 0) = copy_to_permanent (cond); 22346581Sken } 224196008Smjacob else 22546581Sken { 22639212Sgibbs emit_line_note (input_filename, lineno); 22739212Sgibbs expand_exit_loop_if_false (0, condition_conversion (cond)); 22846581Sken } 22939212Sgibbs 23039212Sgibbs /* If COND wasn't a declaration, clear out the 23146581Sken block we made for it and start a new one here so the 23246581Sken optimization in expand_end_loop will work. */ 23346581Sken if (getdecls () == NULL_TREE) 23446581Sken { 23549925Sgibbs do_poplevel (); 23646581Sken do_pushlevel (); 23746581Sken } 23844499Sgibbs} 23949925Sgibbs 24049925Sgibbs/* Finish a while-statement, which may be given by WHILE_STMT. */ 24149925Sgibbs 24249925Sgibbsvoid 24374840Skenfinish_while_stmt (while_stmt) 24474840Sken tree while_stmt; 24574840Sken{ 24674840Sken do_poplevel (); 24774840Sken 24874840Sken if (processing_template_decl) 24974840Sken RECHAIN_STMTS_FROM_CHAIN (while_stmt, WHILE_BODY (while_stmt)); 250195534Sscottl else 251236778Smav expand_end_loop (); 25274840Sken finish_stmt (); 25374840Sken} 25474840Sken 25574840Sken/* Begin a do-statement. Returns a newly created DO_STMT if 25674840Sken appropriate. */ 25774840Sken 25874840Skentree 25974840Skenbegin_do_stmt () 26074840Sken{ 26174840Sken if (processing_template_decl) 262154593Smjacob { 263154593Smjacob tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE); 264195534Sscottl add_tree (r); 265196352Smav return r; 26674840Sken } 26774840Sken else 268220644Smav { 269220644Smav emit_nop (); 270220644Smav emit_line_note (input_filename, lineno); 271220644Smav expand_start_loop_continue_elsewhere (1); 272220644Smav return NULL_TREE; 273220644Smav } 274220644Smav} 275220644Smav 276154593Smjacob/* Finish the body of a do-statement, which may be given by DO_STMT. */ 277154593Smjacob 278154593Smjacobvoid 279154593Smjacobfinish_do_body (do_stmt) 28074840Sken tree do_stmt; 28139212Sgibbs{ 28260938Sjake if (processing_template_decl) 28360938Sjake RECHAIN_STMTS_FROM_CHAIN (do_stmt, DO_BODY (do_stmt)); 28460938Sjake else 28560938Sjake expand_loop_continue_here (); 28639212Sgibbs} 28739212Sgibbs 28839212Sgibbs/* Finish a do-statement, which may be given by DO_STMT, and whose 28939212Sgibbs COND is as indicated. */ 29039212Sgibbs 291168752Sscottlvoid 29239212Sgibbsfinish_do_stmt (cond, do_stmt) 29339212Sgibbs tree cond; 29439212Sgibbs tree do_stmt; 29539212Sgibbs{ 29639212Sgibbs if (processing_template_decl) 29739212Sgibbs DO_COND (do_stmt) = copy_to_permanent (cond); 29839212Sgibbs else 29939212Sgibbs { 30039212Sgibbs emit_line_note (input_filename, lineno); 30139212Sgibbs expand_exit_loop_if_false (0, condition_conversion (cond)); 30239212Sgibbs expand_end_loop (); 30339212Sgibbs } 30439212Sgibbs 305130200Sscottl clear_momentary (); 30639212Sgibbs finish_stmt (); 30739212Sgibbs} 308130200Sscottl 30939212Sgibbs/* Finish a return-statement. The EXPRESSION returned, if any, is as 31039212Sgibbs indicated. */ 311130200Sscottl 31239212Sgibbsvoid 313130200Sscottlfinish_return_stmt (expr) 314130200Sscottl tree expr; 31539212Sgibbs{ 31639212Sgibbs emit_line_note (input_filename, lineno); 31739212Sgibbs c_expand_return (expr); 318130200Sscottl finish_stmt (); 31939212Sgibbs} 32039212Sgibbs 32139212Sgibbs/* Begin a for-statement. Returns a new FOR_STMT if appropriate. */ 322168752Sscottl 323168752Sscottltree 324168752Sscottlbegin_for_stmt () 325168752Sscottl{ 326168752Sscottl tree r; 327130200Sscottl 32839212Sgibbs if (processing_template_decl) 32939212Sgibbs { 33039212Sgibbs r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE, 33139212Sgibbs NULL_TREE, NULL_TREE); 33239212Sgibbs add_tree (r); 333195534Sscottl } 33456145Smjacob else 335195534Sscottl r = NULL_TREE; 33639212Sgibbs 337199178Smav if (flag_new_for_scope > 0) 33839212Sgibbs { 33939212Sgibbs do_pushlevel (); 34039212Sgibbs note_level_for_for (); 34147412Sgibbs } 34247412Sgibbs 34347412Sgibbs return r; 34447412Sgibbs} 34547412Sgibbs 34647412Sgibbs/* Finish the for-init-statement of a for-statement, which may be 34747412Sgibbs given by FOR_STMT. */ 34847412Sgibbs 34947412Sgibbsvoid 35047412Sgibbsfinish_for_init_stmt (for_stmt) 35147412Sgibbs tree for_stmt; 35247434Sgibbs{ 35347434Sgibbs if (processing_template_decl) 35447434Sgibbs { 35547434Sgibbs if (last_tree != for_stmt) 35647434Sgibbs RECHAIN_STMTS_FROM_CHAIN (for_stmt, FOR_INIT_STMT (for_stmt)); 35747412Sgibbs } 35847412Sgibbs else 35939212Sgibbs { 36039212Sgibbs emit_nop (); 36139212Sgibbs emit_line_note (input_filename, lineno); 36239212Sgibbs expand_start_loop_continue_elsewhere (1); 36339212Sgibbs } 36439212Sgibbs 36539212Sgibbs do_pushlevel (); 36639212Sgibbs} 36739212Sgibbs 36839212Sgibbs/* Finish the COND of a for-statement, which may be given by 36939212Sgibbs FOR_STMT. */ 37039212Sgibbs 37139212Sgibbsvoid 37239212Sgibbsfinish_for_cond (cond, for_stmt) 37339212Sgibbs tree cond; 37439212Sgibbs tree for_stmt; 37539212Sgibbs{ 37639212Sgibbs if (processing_template_decl) 37739212Sgibbs { 37839212Sgibbs if (last_tree != for_stmt) 37939212Sgibbs RECHAIN_STMTS_FROM_LAST (for_stmt, FOR_COND (for_stmt)); 38039212Sgibbs else 38139212Sgibbs FOR_COND (for_stmt) = copy_to_permanent (cond); 38239212Sgibbs } 38339212Sgibbs else 38439212Sgibbs { 38539212Sgibbs emit_line_note (input_filename, lineno); 38639212Sgibbs if (cond) 38739212Sgibbs expand_exit_loop_if_false (0, condition_conversion (cond)); 38839212Sgibbs } 38939212Sgibbs 39039212Sgibbs /* If the cond wasn't a declaration, clear out the 39139212Sgibbs block we made for it and start a new one here so the 39239212Sgibbs optimization in expand_end_loop will work. */ 39339212Sgibbs if (getdecls () == NULL_TREE) 39439212Sgibbs { 39539212Sgibbs do_poplevel (); 39639212Sgibbs do_pushlevel (); 39739212Sgibbs } 39839212Sgibbs} 39939212Sgibbs 40039212Sgibbs/* Finish the increment-EXPRESSION in a for-statement, which may be 401223081Sgibbs given by FOR_STMT. */ 40239212Sgibbs 40339212Sgibbsvoid 40439212Sgibbsfinish_for_expr (expr, for_stmt) 405223081Sgibbs tree expr; 406223081Sgibbs tree for_stmt; 407223081Sgibbs{ 408223081Sgibbs if (processing_template_decl) 409223081Sgibbs FOR_EXPR (for_stmt) = expr; 41039212Sgibbs 411223081Sgibbs /* Don't let the tree nodes for EXPR be discarded 412223081Sgibbs by clear_momentary during the parsing of the next stmt. */ 413223081Sgibbs push_momentary (); 414223081Sgibbs} 415223081Sgibbs 416223081Sgibbs/* Finish the body of a for-statement, which may be given by 417223081Sgibbs FOR_STMT. The increment-EXPR for the loop must be 418223081Sgibbs provided. */ 41939212Sgibbs 42039212Sgibbsvoid 42139212Sgibbsfinish_for_stmt (expr, for_stmt) 42239212Sgibbs tree expr; 42339212Sgibbs tree for_stmt; 42439212Sgibbs{ 42539212Sgibbs /* Pop the scope for the body of the loop. */ 42639212Sgibbs do_poplevel (); 42739212Sgibbs 42839212Sgibbs if (processing_template_decl) 42939212Sgibbs RECHAIN_STMTS_FROM_CHAIN (for_stmt, FOR_BODY (for_stmt)); 43039212Sgibbs else 43139212Sgibbs { 43239212Sgibbs emit_line_note (input_filename, lineno); 43339212Sgibbs expand_loop_continue_here (); 43439212Sgibbs if (expr) 43539212Sgibbs cplus_expand_expr_stmt (expr); 43639212Sgibbs expand_end_loop (); 43739212Sgibbs } 43839212Sgibbs 43939212Sgibbs pop_momentary (); 44039212Sgibbs 44139212Sgibbs if (flag_new_for_scope > 0) 44239212Sgibbs do_poplevel (); 44339212Sgibbs 44439212Sgibbs finish_stmt (); 44539212Sgibbs} 44639212Sgibbs 44739212Sgibbs/* Finish a break-statement. */ 44839212Sgibbs 44939212Sgibbsvoid 45039212Sgibbsfinish_break_stmt () 45139212Sgibbs{ 45239212Sgibbs emit_line_note (input_filename, lineno); 45339212Sgibbs if (processing_template_decl) 45439212Sgibbs add_tree (build_min_nt (BREAK_STMT)); 45539212Sgibbs else if ( ! expand_exit_something ()) 45639212Sgibbs cp_error ("break statement not within loop or switch"); 45739212Sgibbs} 45839212Sgibbs 45939212Sgibbs/* Finish a continue-statement. */ 46039212Sgibbs 46139212Sgibbsvoid 46239212Sgibbsfinish_continue_stmt () 46346581Sken{ 46446581Sken emit_line_note (input_filename, lineno); 46546581Sken if (processing_template_decl) 46646581Sken add_tree (build_min_nt (CONTINUE_STMT)); 46746581Sken else if (! expand_continue_loop (0)) 46839212Sgibbs cp_error ("continue statement not within a loop"); 46939212Sgibbs} 47039212Sgibbs 47139212Sgibbs/* Begin a switch-statement. */ 472195534Sscottl 47339212Sgibbsvoid 474195534Sscottlbegin_switch_stmt () 47546581Sken{ 47639212Sgibbs do_pushlevel (); 47739212Sgibbs} 47839212Sgibbs 47939212Sgibbs/* Finish the cond of a switch-statement. Returns a new 48039212Sgibbs SWITCH_STMT if appropriate. */ 48139212Sgibbs 48239212Sgibbstree 48339212Sgibbsfinish_switch_cond (cond) 48439212Sgibbs tree cond; 48539212Sgibbs{ 48639212Sgibbs tree r; 48739212Sgibbs 48839212Sgibbs if (processing_template_decl) 48939212Sgibbs { 49039212Sgibbs r = build_min_nt (SWITCH_STMT, cond, NULL_TREE); 49139212Sgibbs add_tree (r); 49239212Sgibbs } 49339212Sgibbs else if (cond != error_mark_node) 49439212Sgibbs { 49539212Sgibbs emit_line_note (input_filename, lineno); 49639212Sgibbs c_expand_start_case (cond); 49739212Sgibbs r = NULL_TREE; 49839212Sgibbs } 49939212Sgibbs else 50039212Sgibbs { 50139212Sgibbs /* The code is in error, but we don't want expand_end_case to 50239212Sgibbs crash. */ 50339212Sgibbs c_expand_start_case (boolean_false_node); 50439212Sgibbs r = NULL_TREE; 50539212Sgibbs } 50639212Sgibbs 50739212Sgibbs push_switch (); 50839212Sgibbs 50939212Sgibbs /* Don't let the tree nodes for COND be discarded by 51039212Sgibbs clear_momentary during the parsing of the next stmt. */ 51139212Sgibbs push_momentary (); 51239212Sgibbs 51339212Sgibbs return r; 51439212Sgibbs} 51539212Sgibbs 51639212Sgibbs/* Finish the body of a switch-statement, which may be given by 51739212Sgibbs SWITCH_STMT. The COND to switch on is indicated. */ 51839212Sgibbs 51939212Sgibbsvoid 52039212Sgibbsfinish_switch_stmt (cond, switch_stmt) 52139212Sgibbs tree cond; 52239212Sgibbs tree switch_stmt; 52339212Sgibbs{ 52439212Sgibbs if (processing_template_decl) 52539212Sgibbs RECHAIN_STMTS_FROM_CHAIN (switch_stmt, SWITCH_BODY (switch_stmt)); 52639212Sgibbs else 52739212Sgibbs expand_end_case (cond); 52839212Sgibbs pop_momentary (); 52939212Sgibbs pop_switch (); 53039212Sgibbs do_poplevel (); 53139212Sgibbs finish_stmt (); 53239212Sgibbs} 53339212Sgibbs 53439212Sgibbs/* Finish a case-label. */ 53539212Sgibbs 53639212Sgibbsvoid 53739212Sgibbsfinish_case_label (low_value, high_value) 53839212Sgibbs tree low_value; 53939212Sgibbs tree high_value; 54039212Sgibbs{ 54139212Sgibbs do_case (low_value, high_value); 54239212Sgibbs} 54339212Sgibbs 54439212Sgibbs 54539212Sgibbs/* Finish a goto-statement. */ 54639212Sgibbs 54739212Sgibbsvoid 54839212Sgibbsfinish_goto_stmt (destination) 549251874Sscottl tree destination; 55039212Sgibbs{ 55139212Sgibbs if (processing_template_decl) 55239212Sgibbs add_tree (build_min_nt (GOTO_STMT, destination)); 55339212Sgibbs else 55439212Sgibbs { 55539212Sgibbs emit_line_note (input_filename, lineno); 55639212Sgibbs 557195534Sscottl if (TREE_CODE (destination) == IDENTIFIER_NODE) 55839212Sgibbs { 55953258Sken tree decl = lookup_label (destination); 56039212Sgibbs TREE_USED (decl) = 1; 56139212Sgibbs expand_goto (decl); 56239212Sgibbs } 56339212Sgibbs else 56439212Sgibbs expand_computed_goto (destination); 56539212Sgibbs } 56639212Sgibbs} 56739212Sgibbs 56853258Sken/* Begin a try-block. Returns a newly-created TRY_BLOCK if 56939212Sgibbs appropriate. */ 57039212Sgibbs 57139212Sgibbstree 57239212Sgibbsbegin_try_block () 57339212Sgibbs{ 57441644Sgibbs if (processing_template_decl) 575118105Snjl { 576159311Smjacob tree r = build_min_nt (TRY_BLOCK, NULL_TREE, 577251897Sscottl NULL_TREE); 578251897Sscottl add_tree (r); 579254938Sken return r; 58039212Sgibbs } 58139212Sgibbs else 58239212Sgibbs { 58374840Sken emit_line_note (input_filename, lineno); 58474840Sken expand_start_try_stmts (); 58574840Sken return NULL_TREE; 586196008Smjacob } 58777708Smjacob} 58877708Smjacob 58977708Smjacob/* Finish a try-block, which may be given by TRY_BLOCK. */ 59077708Smjacob 59177708Smjacobvoid 59277708Smjacobfinish_try_block (try_block) 593196008Smjacob tree try_block; 594154593Smjacob{ 595154593Smjacob if (processing_template_decl) 596154593Smjacob RECHAIN_STMTS_FROM_LAST (try_block, TRY_STMTS (try_block)); 59777708Smjacob else 59874840Sken { 59939212Sgibbs expand_start_all_catch (); 60039212Sgibbs } 60139212Sgibbs} 60239212Sgibbs 60339212Sgibbs/* Finish a handler-sequence for a try-block, which may be given by 60439212Sgibbs TRY_BLOCK. */ 60539212Sgibbs 60639212Sgibbsvoid 60739212Sgibbsfinish_handler_sequence (try_block) 60839212Sgibbs tree try_block; 60939212Sgibbs{ 61039212Sgibbs if (processing_template_decl) 61139212Sgibbs RECHAIN_STMTS_FROM_CHAIN (try_block, TRY_HANDLERS (try_block)); 61239212Sgibbs else 61339212Sgibbs { 61439212Sgibbs expand_end_all_catch (); 61539212Sgibbs } 61639212Sgibbs} 61739212Sgibbs 61846581Sken/* Begin a handler. Returns a HANDLER if appropriate. */ 61974840Sken 62074840Skentree 62174840Skenbegin_handler () 62274840Sken{ 62374840Sken tree r; 62474840Sken 62577708Smjacob if (processing_template_decl) 626154593Smjacob { 62777708Smjacob r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE); 62874840Sken add_tree (r); 629195534Sscottl } 630210471Smav else 631210471Smav r = NULL_TREE; 632210471Smav 633210471Smav do_pushlevel (); 63439212Sgibbs 63539212Sgibbs return r; 63647412Sgibbs} 63747412Sgibbs 63847412Sgibbs/* Finish the handler-parameters for a handler, which may be given by 63947412Sgibbs HANDLER. */ 64047412Sgibbs 64147412Sgibbsvoid 642216088Skenfinish_handler_parms (handler) 643216088Sken tree handler; 644216088Sken{ 645216088Sken if (processing_template_decl) 646216088Sken RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_PARMS (handler)); 647216088Sken} 648216088Sken 649216088Sken/* Finish a handler, which may be given by HANDLER. */ 650216088Sken 651216088Skenvoid 652216088Skenfinish_handler (handler) 653216088Sken tree handler; 654216088Sken{ 655216088Sken if (processing_template_decl) 656216088Sken RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_BODY (handler)); 657216088Sken else 658216088Sken expand_end_catch_block (); 659216088Sken 660216088Sken do_poplevel (); 661216088Sken} 662216088Sken 663216088Sken/* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the 664216088Sken compound-statement does not define a scope. Returns a new 665216088Sken COMPOUND_STMT if appropriate. */ 666216088Sken 667216088Skentree 66839212Sgibbsbegin_compound_stmt (has_no_scope) 66939212Sgibbs int has_no_scope; 67039212Sgibbs{ 67139212Sgibbs tree r; 67239212Sgibbs 67339212Sgibbs if (processing_template_decl) 67439212Sgibbs { 67539212Sgibbs r = build_min_nt (COMPOUND_STMT, NULL_TREE); 67639212Sgibbs add_tree (r); 67739212Sgibbs if (has_no_scope) 67839212Sgibbs COMPOUND_STMT_NO_SCOPE (r) = 1; 67939212Sgibbs } 68039212Sgibbs else 68139212Sgibbs r = NULL_TREE; 68239212Sgibbs 68339212Sgibbs if (!has_no_scope) 68439212Sgibbs do_pushlevel (); 68539212Sgibbs 68639212Sgibbs return r; 68739212Sgibbs} 68839212Sgibbs 68939212Sgibbs 69039212Sgibbs/* Finish a compound-statement, which may be given by COMPOUND_STMT. 69139212Sgibbs If HAS_NO_SCOPE is non-zero, the compound statement does not define 69239212Sgibbs a scope. */ 69339212Sgibbs 69439212Sgibbstree 69539212Sgibbsfinish_compound_stmt (has_no_scope, compound_stmt) 69639212Sgibbs int has_no_scope; 69739212Sgibbs tree compound_stmt; 69839212Sgibbs{ 69939212Sgibbs tree r; 70039212Sgibbs 70139212Sgibbs if (!has_no_scope) 70239212Sgibbs r = do_poplevel (); 70339212Sgibbs else 70439212Sgibbs r = NULL_TREE; 70540417Sgibbs 70640417Sgibbs if (processing_template_decl) 70740417Sgibbs RECHAIN_STMTS_FROM_CHAIN (compound_stmt, 70840417Sgibbs COMPOUND_BODY (compound_stmt)); 70940417Sgibbs 71040417Sgibbs finish_stmt (); 71149925Sgibbs 71249925Sgibbs return r; 71339212Sgibbs} 71439212Sgibbs 715195534Sscottl/* Finish an asm-statement, whose components are a CV_QUALIFIER, a 716195534Sscottl STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some 717195534Sscottl CLOBBERS. */ 718195534Sscottl 719195534Sscottlvoid 720195534Sscottlfinish_asm_stmt (cv_qualifier, string, output_operands, 721195534Sscottl input_operands, clobbers) 722195534Sscottl tree cv_qualifier; 723195534Sscottl tree string; 724195534Sscottl tree output_operands; 725195534Sscottl tree input_operands; 726195534Sscottl tree clobbers; 727195534Sscottl{ 728195534Sscottl if (TREE_CHAIN (string)) 729195534Sscottl string = combine_strings (string); 730195534Sscottl 731195534Sscottl if (processing_template_decl) 732195534Sscottl { 733195534Sscottl tree r = build_min_nt (ASM_STMT, cv_qualifier, string, 734195534Sscottl output_operands, input_operands, 735195534Sscottl clobbers); 736195534Sscottl add_tree (r); 73739212Sgibbs } 73839212Sgibbs else 73939212Sgibbs { 74039212Sgibbs emit_line_note (input_filename, lineno); 74139212Sgibbs if (output_operands != NULL_TREE || input_operands != NULL_TREE 74256145Smjacob || clobbers != NULL_TREE) 74355328Smjacob { 74455328Smjacob tree t; 74556145Smjacob 74639212Sgibbs if (cv_qualifier != NULL_TREE 74739212Sgibbs && cv_qualifier != ridpointers[(int) RID_VOLATILE]) 74839212Sgibbs cp_warning ("%s qualifier ignored on asm", 74939212Sgibbs IDENTIFIER_POINTER (cv_qualifier)); 75039212Sgibbs 75139212Sgibbs for (t = input_operands; t; t = TREE_CHAIN (t)) 75239212Sgibbs TREE_VALUE (t) = decay_conversion (TREE_VALUE (t)); 75339212Sgibbs 75439212Sgibbs c_expand_asm_operands (string, output_operands, 75539212Sgibbs input_operands, 75639212Sgibbs clobbers, 757203108Smav cv_qualifier 75839212Sgibbs == ridpointers[(int) RID_VOLATILE], 75939212Sgibbs input_filename, lineno); 76039212Sgibbs } 76139212Sgibbs else 76239212Sgibbs { 76339212Sgibbs /* Don't warn about redundant specification of 'volatile' here. */ 76439212Sgibbs if (cv_qualifier != NULL_TREE 765249152Smav && cv_qualifier != ridpointers[(int) RID_VOLATILE]) 766223081Sgibbs cp_warning ("%s qualifier ignored on asm", 767196008Smjacob IDENTIFIER_POINTER (cv_qualifier)); 76839212Sgibbs expand_asm (string); 76939212Sgibbs } 77039212Sgibbs 77139212Sgibbs finish_stmt (); 77239212Sgibbs } 77339212Sgibbs} 77439212Sgibbs 77539212Sgibbs/* Finish a parenthesized expression EXPR. */ 77639212Sgibbs 77739212Sgibbstree 77839212Sgibbsfinish_parenthesized_expr (expr) 77939212Sgibbs tree expr; 78039212Sgibbs{ 78139212Sgibbs if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr)))) 78239212Sgibbs /* This inhibits warnings in truthvalue_conversion. */ 78339212Sgibbs C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK); 784196008Smjacob 785196008Smjacob return expr; 786196008Smjacob} 787196008Smjacob 788196008Smjacob/* Begin a statement-expression. The value returned must be passed to 789196008Smjacob finish_stmt_expr. */ 790196008Smjacob 791196008Smjacobtree 792196008Smjacobbegin_stmt_expr () 793196008Smjacob{ 794196008Smjacob keep_next_level (); 795196008Smjacob /* If we're processing_template_decl, then the upcoming compound 796196008Smjacob statement will be chained onto the tree structure, starting at 797196008Smjacob last_tree. We return last_tree so that we can later unhook the 798196008Smjacob compound statement. */ 799196008Smjacob return processing_template_decl ? last_tree : expand_start_stmt_expr(); 800196008Smjacob} 801196008Smjacob 802196008Smjacob/* Finish a statement-expression. RTL_EXPR should be the value 803196008Smjacob returned by the previous begin_stmt_expr; EXPR is the 80439212Sgibbs statement-expression. Returns an expression representing the 80539212Sgibbs statement-expression. */ 80639212Sgibbs 80739212Sgibbstree 80839212Sgibbsfinish_stmt_expr (rtl_expr, expr) 80939212Sgibbs tree rtl_expr; 81039212Sgibbs tree expr; 81139212Sgibbs{ 81239212Sgibbs tree result; 81339212Sgibbs 81439212Sgibbs if (!processing_template_decl) 81539212Sgibbs { 81639212Sgibbs rtl_expr = expand_end_stmt_expr (rtl_expr); 81739212Sgibbs /* The statements have side effects, so the group does. */ 81839212Sgibbs TREE_SIDE_EFFECTS (rtl_expr) = 1; 81939212Sgibbs } 82039212Sgibbs 82139212Sgibbs if (TREE_CODE (expr) == BLOCK) 82239212Sgibbs { 82339212Sgibbs /* Make a BIND_EXPR for the BLOCK already made. */ 82439212Sgibbs if (processing_template_decl) 82539212Sgibbs result = build_min_nt (BIND_EXPR, NULL_TREE, last_tree, 82639212Sgibbs NULL_TREE); 82739212Sgibbs else 82839212Sgibbs result = build (BIND_EXPR, TREE_TYPE (rtl_expr), 82939212Sgibbs NULL_TREE, rtl_expr, expr); 83039212Sgibbs 83139212Sgibbs /* Remove the block from the tree at this point. 83239212Sgibbs It gets put back at the proper place 83339212Sgibbs when the BIND_EXPR is expanded. */ 83439212Sgibbs delete_block (expr); 83539212Sgibbs } 83639212Sgibbs else 83739212Sgibbs result = expr; 83839212Sgibbs 83939212Sgibbs if (processing_template_decl) 84039212Sgibbs { 84139212Sgibbs /* Remove the compound statement from the tree structure; it is 84274840Sken now saved in the BIND_EXPR. */ 84374840Sken last_tree = rtl_expr; 84474840Sken TREE_CHAIN (last_tree) = NULL_TREE; 84574840Sken } 84674840Sken 84774840Sken return result; 84874840Sken} 84939212Sgibbs 85074840Sken/* Finish a call to FN with ARGS. Returns a representation of the 85139212Sgibbs call. */ 85274840Sken 85374840Skentree 85474840Skenfinish_call_expr (fn, args, koenig) 855236786Smav tree fn; 856236786Smav tree args; 857236786Smav int koenig; 858236786Smav{ 859236786Smav tree result; 860236786Smav 861236786Smav if (koenig) 862236786Smav { 86374840Sken if (TREE_CODE (fn) == BIT_NOT_EXPR) 86474840Sken fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0)); 86574840Sken else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR) 86674840Sken fn = do_identifier (fn, 2, args); 86774840Sken } 86874840Sken result = build_x_function_call (fn, args, current_class_ref); 86974840Sken 87074840Sken if (TREE_CODE (result) == CALL_EXPR 87174840Sken && (! TREE_TYPE (result) 87274840Sken || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE)) 87339212Sgibbs result = require_complete_type (result); 87439212Sgibbs 87539212Sgibbs return result; 87674840Sken} 87739212Sgibbs 87839212Sgibbs/* Finish a call to a postfix increment or decrement or EXPR. (Which 87978709Smjacob is indicated by CODE, which should be POSTINCREMENT_EXPR or 88078709Smjacob POSTDECREMENT_EXPR.) */ 88178709Smjacob 88278709Smjacobtree 88378709Smjacobfinish_increment_expr (expr, code) 88478709Smjacob tree expr; 88578709Smjacob enum tree_code code; 88678709Smjacob{ 88778709Smjacob /* If we get an OFFSET_REF, turn it into what it really means (e.g., 88878709Smjacob a COMPONENT_REF). This way if we've got, say, a reference to a 88978709Smjacob static member that's being operated on, we don't end up trying to 89078709Smjacob find a member operator for the class it's in. */ 891154593Smjacob 892154593Smjacob if (TREE_CODE (expr) == OFFSET_REF) 893154593Smjacob expr = resolve_offset_ref (expr); 894154593Smjacob return build_x_unary_op (code, expr); 895154593Smjacob} 896154593Smjacob 897236786Smav/* Finish a use of `this'. Returns an expression for `this'. */ 898198708Smav 899198708Smavtree 900199747Smavfinish_this_expr () 901203376Smav{ 902251081Smarius tree result; 903199821Smav 904198708Smav if (current_class_ptr) 905203376Smav { 906251081Smarius#ifdef WARNING_ABOUT_CCD 907251081Smarius TREE_USED (current_class_ptr) = 1; 908251081Smarius#endif 909251081Smarius result = current_class_ptr; 910198708Smav } 911198708Smav else if (current_function_decl 912195534Sscottl && DECL_STATIC_FUNCTION_P (current_function_decl)) 913195534Sscottl { 914199747Smav error ("`this' is unavailable for static member functions"); 915199747Smav result = error_mark_node; 916199747Smav } 917199747Smav else 918199747Smav { 919203376Smav if (current_function_decl) 920207499Smav error ("invalid use of `this' in non-member function"); 921199821Smav else 922199747Smav error ("invalid use of `this' at top level"); 923199821Smav result = error_mark_node; 924195534Sscottl } 925199747Smav 926203376Smav return result; 927207499Smav} 928207499Smav 929207499Smav/* Finish a member function call using OBJECT and ARGS as arguments to 930207499Smav FN. Returns an expression for the call. */ 931207499Smav 932220602Smavtree 933207499Smavfinish_object_call_expr (fn, object, args) 934207499Smav tree fn; 935207499Smav tree object; 936195534Sscottl tree args; 937154593Smjacob{ 93874840Sken#if 0 93974840Sken /* This is a future direction of this code, but because 94074840Sken build_x_function_call cannot always undo what is done in 94174840Sken build_component_ref entirely yet, we cannot do this. */ 94274840Sken 94374840Sken tree real_fn = build_component_ref (object, fn, NULL_TREE, 1); 94474840Sken return finish_call_expr (real_fn, args); 94574840Sken#else 94674840Sken if (TREE_CODE (fn) == TYPE_DECL) 94774840Sken { 948236786Smav if (processing_template_decl) 94974840Sken /* This can happen on code like: 95074840Sken 95174840Sken class X; 95274840Sken template <class T> void f(T t) { 95374840Sken t.X(); 95478709Smjacob } 955154593Smjacob 956236786Smav We just grab the underlying IDENTIFIER. */ 957195534Sscottl fn = DECL_NAME (fn); 95874840Sken else 95974840Sken { 96074840Sken cp_error ("calling type `%T' like a method", fn); 96174840Sken return error_mark_node; 96239212Sgibbs } 96339212Sgibbs } 96439212Sgibbs 96539212Sgibbs return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL); 96639212Sgibbs#endif 96739212Sgibbs} 96839212Sgibbs 969114261Sken/* Finish a qualified member function call using OBJECT and ARGS as 970114261Sken arguments to FN. Returns an expressino for the call. */ 97139212Sgibbs 97239212Sgibbstree 97339212Sgibbsfinish_qualified_object_call_expr (fn, object, args) 97439212Sgibbs tree fn; 97539212Sgibbs tree object; 976196008Smjacob tree args; 977196008Smjacob{ 978196008Smjacob if (IS_SIGNATURE (TREE_OPERAND (fn, 0))) 979196008Smjacob { 980196008Smjacob warning ("signature name in scope resolution ignored"); 981196008Smjacob return finish_object_call_expr (TREE_OPERAND (fn, 1), object, args); 982196008Smjacob } 983196008Smjacob else 984196008Smjacob return build_scoped_method_call (object, TREE_OPERAND (fn, 0), 985196008Smjacob TREE_OPERAND (fn, 1), args); 986196008Smjacob} 987196008Smjacob 988196008Smjacob/* Finish a pseudo-destructor call expression of OBJECT, with SCOPE 989196008Smjacob being the scope, if any, of DESTRUCTOR. Returns an expression for 990196008Smjacob the call. */ 991196008Smjacob 992196008Smjacobtree 993196008Smjacobfinish_pseudo_destructor_call_expr (object, scope, destructor) 994196008Smjacob tree object; 995196008Smjacob tree scope; 996196008Smjacob tree destructor; 997196008Smjacob{ 998196008Smjacob if (scope && scope != destructor) 999196008Smjacob cp_error ("destructor specifier `%T::~%T()' must have matching names", 1000196008Smjacob scope, destructor); 1001196008Smjacob 1002196008Smjacob if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor)) 1003196008Smjacob && (TREE_CODE (TREE_TYPE (object)) != 1004196008Smjacob TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor))))) 1005196008Smjacob cp_error ("`%E' is not of type `%T'", object, destructor); 1006196008Smjacob 1007196008Smjacob return cp_convert (void_type_node, object); 1008196008Smjacob} 1009196008Smjacob 1010196008Smjacob/* Finish a call to a globally qualified member function FN using 1011196008Smjacob ARGS. Returns an expression for the call. */ 1012196008Smjacob 1013196008Smjacobtree 1014196008Smjacobfinish_qualified_call_expr (fn, args) 1015196008Smjacob tree fn; 1016196008Smjacob tree args; 1017196008Smjacob{ 1018196008Smjacob if (processing_template_decl) 1019196008Smjacob return build_min_nt (CALL_EXPR, copy_to_permanent (fn), args, 102039212Sgibbs NULL_TREE); 102139212Sgibbs else 102239212Sgibbs return build_member_call (TREE_OPERAND (fn, 0), 102339212Sgibbs TREE_OPERAND (fn, 1), 102439212Sgibbs args); 102539212Sgibbs} 102639212Sgibbs 102739212Sgibbs/* Finish an expression taking the address of LABEL. Returns an 102839212Sgibbs expression for the address. */ 102939212Sgibbs 103039212Sgibbstree 103139212Sgibbsfinish_label_address_expr (label) 103239212Sgibbs tree label; 103339212Sgibbs{ 103439212Sgibbs tree result; 103539212Sgibbs 103639212Sgibbs label = lookup_label (label); 103739212Sgibbs if (label == NULL_TREE) 103839212Sgibbs result = null_pointer_node; 103939212Sgibbs else 104039212Sgibbs { 104139212Sgibbs TREE_USED (label) = 1; 104239212Sgibbs result = build1 (ADDR_EXPR, ptr_type_node, label); 104339212Sgibbs TREE_CONSTANT (result) = 1; 1044196008Smjacob } 104539212Sgibbs 104639212Sgibbs return result; 104739212Sgibbs} 104856145Smjacob 104939212Sgibbs/* Finish an expression of the form CODE EXPR. */ 105039212Sgibbs 105139212Sgibbstree 105239212Sgibbsfinish_unary_op_expr (code, expr) 105339212Sgibbs enum tree_code code; 105439212Sgibbs tree expr; 105539212Sgibbs{ 105639212Sgibbs tree result = build_x_unary_op (code, expr); 105739212Sgibbs if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST) 105839212Sgibbs TREE_NEGATED_INT (result) = 1; 1059196008Smjacob overflow_warning (result); 1060196008Smjacob return result; 1061196008Smjacob} 1062196008Smjacob 1063196008Smjacob/* Finish an id-expression. */ 1064196008Smjacob 1065196008Smjacobtree 1066196008Smjacobfinish_id_expr (expr) 1067196008Smjacob tree expr; 1068196008Smjacob{ 1069196008Smjacob if (TREE_CODE (expr) == IDENTIFIER_NODE) 1070196008Smjacob expr = do_identifier (expr, 1, NULL_TREE); 1071196008Smjacob 1072196008Smjacob return expr; 1073196008Smjacob} 1074196008Smjacob 107539212Sgibbs/* Begin a new-placement. */ 107639212Sgibbs 107739212Sgibbsint 107839212Sgibbsbegin_new_placement () 107939212Sgibbs{ 108039212Sgibbs /* The arguments to a placement new might be passed to a 108139212Sgibbs deallocation function, in the event that the allocation throws an 108239212Sgibbs exception. Since we don't expand exception handlers until the 108339212Sgibbs end of a function, we must make sure the arguments stay around 108439212Sgibbs that long. */ 108539212Sgibbs return suspend_momentary (); 108639212Sgibbs} 108739212Sgibbs 108853258Sken/* Finish a new-placement. The ARGS are the placement arguments. The 108939212Sgibbs COOKIE is the value returned by the previous call to 109039212Sgibbs begin_new_placement. */ 109139212Sgibbs 109239212Sgibbstree 109339212Sgibbsfinish_new_placement (args, cookie) 109439212Sgibbs tree args; 109539212Sgibbs int cookie; 109639212Sgibbs{ 109739212Sgibbs resume_momentary (cookie); 109839212Sgibbs return args; 109939212Sgibbs} 110039212Sgibbs 110139212Sgibbs/* Begin a function defniition declared with DECL_SPECS and 110239212Sgibbs DECLARATOR. Returns non-zero if the function-declaration is 110339212Sgibbs legal. */ 110439212Sgibbs 110539212Sgibbsint 110639212Sgibbsbegin_function_definition (decl_specs, declarator) 110739212Sgibbs tree decl_specs; 110839212Sgibbs tree declarator; 110939212Sgibbs{ 111039212Sgibbs tree specs; 111139212Sgibbs tree attrs; 111239212Sgibbs split_specs_attrs (decl_specs, &specs, &attrs); 111339212Sgibbs if (!start_function (specs, declarator, attrs, 0)) 111439212Sgibbs return 0; 111539212Sgibbs 111639212Sgibbs reinit_parse_for_function (); 111739212Sgibbs /* The things we're about to see are not directly qualified by any 111839212Sgibbs template headers we've seen thus far. */ 111939212Sgibbs reset_specialization (); 112039212Sgibbs 112139212Sgibbs return 1; 112239212Sgibbs} 112339212Sgibbs 112439212Sgibbs/* Begin a constructor declarator of the form `SCOPE::NAME'. Returns 112539212Sgibbs a SCOPE_REF. */ 112639212Sgibbs 112739212Sgibbstree 112839212Sgibbsbegin_constructor_declarator (scope, name) 112939212Sgibbs tree scope; 1130223081Sgibbs tree name; 1131216088Sken{ 1132216088Sken tree result = build_parse_node (SCOPE_REF, scope, name); 1133223081Sgibbs enter_scope_of (result); 1134216088Sken return result; 1135223081Sgibbs} 1136216088Sken 1137216088Sken/* Finish an init-declarator. Returns a DECL. */ 1138223081Sgibbs 1139216088Skentree 1140216088Skenfinish_declarator (declarator, declspecs, attributes, 1141223081Sgibbs prefix_attributes, initialized) 1142223081Sgibbs tree declarator; 1143223081Sgibbs tree declspecs; 1144247111Smav tree attributes; 1145216088Sken tree prefix_attributes; 1146216088Sken int initialized; 1147216088Sken{ 1148216088Sken return start_decl (declarator, declspecs, initialized, attributes, 1149216088Sken prefix_attributes); 1150216088Sken} 1151216088Sken 115239212Sgibbs/* Finish a translation unit. */ 115339212Sgibbs 115439212Sgibbsvoid 115539212Sgibbsfinish_translation_unit () 115639212Sgibbs{ 115739212Sgibbs /* In case there were missing closebraces, 115839212Sgibbs get us back to the global binding level. */ 115939212Sgibbs while (! toplevel_bindings_p ()) 116039212Sgibbs poplevel (0, 0, 0); 116139212Sgibbs while (current_namespace != global_namespace) 116239212Sgibbs pop_namespace (); 116339212Sgibbs finish_file (); 116439212Sgibbs} 116539212Sgibbs 116647412Sgibbs/* Finish a template type parameter, specified as AGGR IDENTIFIER. 116747412Sgibbs Returns the parameter. */ 116839212Sgibbs 116939212Sgibbstree 117039212Sgibbsfinish_template_type_parm (aggr, identifier) 1171196008Smjacob tree aggr; 117239212Sgibbs tree identifier; 117339212Sgibbs{ 117439212Sgibbs if (aggr == signature_type_node) 117539212Sgibbs sorry ("signature as template type parameter"); 117639212Sgibbs else if (aggr != class_type_node) 117739212Sgibbs { 117839212Sgibbs pedwarn ("template type parameters must use the keyword `class' or `typename'"); 117939212Sgibbs aggr = class_type_node; 118039212Sgibbs } 1181196008Smjacob 1182196008Smjacob return build_tree_list (aggr, identifier); 118339212Sgibbs} 118439212Sgibbs 1185216088Sken/* Finish a template template parameter, specified as AGGR IDENTIFIER. 118639212Sgibbs Returns the parameter. */ 118739212Sgibbs 1188195534Sscottltree 1189223081Sgibbsfinish_template_template_parm (aggr, identifier) 119039212Sgibbs tree aggr; 119139212Sgibbs tree identifier; 119239212Sgibbs{ 119339212Sgibbs tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE); 119439212Sgibbs tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE); 119539212Sgibbs DECL_TEMPLATE_PARMS (tmpl) = current_template_parms; 119639212Sgibbs DECL_TEMPLATE_RESULT (tmpl) = decl; 119739212Sgibbs SET_DECL_ARTIFICIAL (decl); 119839212Sgibbs end_template_decl (); 119939212Sgibbs 120039212Sgibbs return finish_template_type_parm (aggr, tmpl); 120139212Sgibbs} 120239212Sgibbs 120339212Sgibbs/* Finish a parameter list, indicated by PARMS. If ELLIPSIS is 120439212Sgibbs non-zero, the parameter list was terminated by a `...'. */ 120539212Sgibbs 120639212Sgibbstree 1207195534Sscottlfinish_parmlist (parms, ellipsis) 120839212Sgibbs tree parms; 1209195534Sscottl int ellipsis; 1210195534Sscottl{ 1211195534Sscottl if (!ellipsis) 1212195534Sscottl chainon (parms, void_list_node); 1213195534Sscottl /* We mark the PARMS as a parmlist so that declarator processing can 1214195534Sscottl disambiguate certain constructs. */ 1215195534Sscottl if (parms != NULL_TREE) 1216216088Sken TREE_PARMLIST (parms) = 1; 1217216088Sken 1218216088Sken return parms; 1219216088Sken} 1220216088Sken 1221216088Sken/* Begin a class definition, as indicated by T. */ 1222216088Sken 122339212Sgibbstree 122439212Sgibbsbegin_class_definition (t) 122539212Sgibbs tree t; 122639212Sgibbs{ 122739212Sgibbs push_obstacks_nochange (); 122839212Sgibbs end_temporary_allocation (); 122939212Sgibbs 123039212Sgibbs if (t == error_mark_node 123139212Sgibbs || ! IS_AGGR_TYPE (t)) 123239212Sgibbs { 123339212Sgibbs t = make_lang_type (RECORD_TYPE); 123439212Sgibbs pushtag (make_anon_name (), t, 0); 123539212Sgibbs } 123639212Sgibbs 123739212Sgibbs /* In a definition of a member class template, we will get here with an 123839212Sgibbs implicit typename, a TYPENAME_TYPE with a type. */ 123939212Sgibbs if (TREE_CODE (t) == TYPENAME_TYPE) 124039212Sgibbs t = TREE_TYPE (t); 124139212Sgibbs 124239212Sgibbs /* If we generated a partial instantiation of this type, but now 124339212Sgibbs we're seeing a real definition, we're actually looking at a 124439212Sgibbs partial specialization. Consider: 124539212Sgibbs 124639212Sgibbs template <class T, class U> 124739212Sgibbs struct Y {}; 124839212Sgibbs 124939212Sgibbs template <class T> 125039212Sgibbs struct X {}; 125139212Sgibbs 125239212Sgibbs template <class T, class U> 125339212Sgibbs void f() 125439212Sgibbs { 125539212Sgibbs typename X<Y<T, U> >::A a; 125639212Sgibbs } 125739212Sgibbs 125839212Sgibbs template <class T, class U> 125939212Sgibbs struct X<Y<T, U> > 126039212Sgibbs { 126139212Sgibbs }; 1262195534Sscottl 1263195534Sscottl We have to undo the effects of the previous partial 1264195534Sscottl instantiation. */ 1265195534Sscottl if (PARTIAL_INSTANTIATION_P (t)) 1266195534Sscottl { 1267195534Sscottl if (!pedantic) 1268195534Sscottl { 1269195534Sscottl /* Unfortunately, when we're not in pedantic mode, we 1270195534Sscottl attempt to actually fill in some of the fields of the 1271195534Sscottl partial instantiation, in order to support the implicit 1272195534Sscottl typename extension. Clear those fields now, in 1273195534Sscottl preparation for the definition here. The fields cleared 1274195534Sscottl here must match those set in instantiate_class_template. 1275195534Sscottl Look for a comment mentioning begin_class_definition 1276195534Sscottl there. */ 1277195534Sscottl TYPE_BINFO_BASETYPES (t) = NULL_TREE; 1278195534Sscottl TYPE_FIELDS (t) = NULL_TREE; 1279216088Sken TYPE_METHODS (t) = NULL_TREE; 1280216088Sken CLASSTYPE_TAGS (t) = NULL_TREE; 1281216088Sken TYPE_SIZE (t) = NULL_TREE; 1282216088Sken } 1283216088Sken 1284216088Sken /* This isn't a partial instantiation any more. */ 1285216088Sken PARTIAL_INSTANTIATION_P (t) = 0; 1286216088Sken } 1287216088Sken /* If this type was already complete, and we see another definition, 1288216088Sken that's an error. */ 1289216088Sken else if (TYPE_SIZE (t)) 1290216088Sken duplicate_tag_error (t); 1291216088Sken 1292216088Sken if (TYPE_BEING_DEFINED (t)) 1293216088Sken { 1294216088Sken t = make_lang_type (TREE_CODE (t)); 1295216088Sken pushtag (TYPE_IDENTIFIER (t), t, 0); 1296216088Sken } 1297216088Sken maybe_process_partial_specialization (t); 1298216088Sken pushclass (t, 1); 1299216088Sken TYPE_BEING_DEFINED (t) = 1; 1300216088Sken /* Reset the interface data, at the earliest possible 1301216088Sken moment, as it might have been set via a class foo; 1302216088Sken before. */ 1303216088Sken /* Don't change signatures. */ 1304216088Sken if (! IS_SIGNATURE (t)) 1305255542Smav { 1306255542Smav int needs_writing; 1307255542Smav tree name = TYPE_IDENTIFIER (t); 1308255542Smav 1309255542Smav if (! ANON_AGGRNAME_P (name)) 1310255542Smav { 1311255542Smav CLASSTYPE_INTERFACE_ONLY (t) = interface_only; 1312255542Smav SET_CLASSTYPE_INTERFACE_UNKNOWN_X 1313255542Smav (t, interface_unknown); 1314255542Smav } 1315255542Smav 1316255542Smav /* Record how to set the access of this class's 1317255542Smav virtual functions. If write_virtuals == 3, then 1318116350Snjl inline virtuals are ``extern inline''. */ 1319116350Snjl if (write_virtuals == 3) 132039212Sgibbs needs_writing = ! CLASSTYPE_INTERFACE_ONLY (t) 132139212Sgibbs && CLASSTYPE_INTERFACE_KNOWN (t); 132239212Sgibbs else 1323 needs_writing = 1; 1324 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = needs_writing; 1325 } 1326#if 0 1327 tmp = TYPE_IDENTIFIER ($<ttype>0); 1328 if (tmp && IDENTIFIER_TEMPLATE (tmp)) 1329 overload_template_name (tmp, 1); 1330#endif 1331 reset_specialization(); 1332 1333 /* In case this is a local class within a template 1334 function, we save the current tree structure so 1335 that we can get it back later. */ 1336 begin_tree (); 1337 1338 /* Make a declaration for this class in its own scope. */ 1339 build_self_reference (); 1340 1341 return t; 1342} 1343 1344/* Finish the member declaration given by DECL. */ 1345 1346void 1347finish_member_declaration (decl) 1348 tree decl; 1349{ 1350 if (decl == error_mark_node || decl == NULL_TREE) 1351 return; 1352 1353 if (decl == void_type_node) 1354 /* The COMPONENT was a friend, not a member, and so there's 1355 nothing for us to do. */ 1356 return; 1357 1358 /* We should see only one DECL at a time. */ 1359 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0); 1360 1361 /* Set up access control for DECL. */ 1362 TREE_PRIVATE (decl) 1363 = (current_access_specifier == access_private_node); 1364 TREE_PROTECTED (decl) 1365 = (current_access_specifier == access_protected_node); 1366 if (TREE_CODE (decl) == TEMPLATE_DECL) 1367 { 1368 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl); 1369 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl); 1370 } 1371 1372 /* Mark the DECL as a member of the current class. */ 1373 if (TREE_CODE (decl) == FUNCTION_DECL 1374 || DECL_FUNCTION_TEMPLATE_P (decl)) 1375 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in 1376 finish_struct. Presumably it is already set as the function is 1377 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */ 1378 DECL_CLASS_CONTEXT (decl) = current_class_type; 1379 else 1380 DECL_CONTEXT (decl) = current_class_type; 1381 1382 /* Put functions on the TYPE_METHODS list and everything else on the 1383 TYPE_FIELDS list. Note that these are built up in reverse order. 1384 We reverse them (to obtain declaration order) in finish_struct. */ 1385 if (TREE_CODE (decl) == FUNCTION_DECL 1386 || DECL_FUNCTION_TEMPLATE_P (decl)) 1387 { 1388 /* We also need to add this function to the 1389 CLASSTYPE_METHOD_VEC. */ 1390 add_method (current_class_type, 0, decl); 1391 1392 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type); 1393 TYPE_METHODS (current_class_type) = decl; 1394 } 1395 else 1396 { 1397 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields 1398 go at the beginning. The reason is that lookup_field_1 1399 searches the list in order, and we want a field name to 1400 override a type name so that the "struct stat hack" will 1401 work. In particular: 1402 1403 struct S { enum E { }; int E } s; 1404 s.E = 3; 1405 1406 is legal. In addition, the FIELD_DECLs must be maintained in 1407 declaration order so that class layout works as expected. 1408 However, we don't need that order until class layout, so we 1409 save a little time by putting FIELD_DECLs on in reverse order 1410 here, and then reversing them in finish_struct_1. (We could 1411 also keep a pointer to the correct insertion points in the 1412 list.) */ 1413 1414 if (TREE_CODE (decl) == TYPE_DECL) 1415 TYPE_FIELDS (current_class_type) 1416 = chainon (TYPE_FIELDS (current_class_type), decl); 1417 else 1418 { 1419 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type); 1420 TYPE_FIELDS (current_class_type) = decl; 1421 } 1422 1423 /* Enter the DECL into the scope of the class. */ 1424 if (TREE_CODE (decl) != USING_DECL) 1425 pushdecl_class_level (decl); 1426 } 1427} 1428 1429/* Finish a class definition T with the indicate ATTRIBUTES. If SEMI, 1430 the definition is immediately followed by a semicolon. Returns the 1431 type. */ 1432 1433tree 1434finish_class_definition (t, attributes, semi, pop_scope_p) 1435 tree t; 1436 tree attributes; 1437 int semi; 1438 int pop_scope_p; 1439{ 1440 /* finish_struct nukes this anyway; if finish_exception does too, 1441 then it can go. */ 1442 if (semi) 1443 note_got_semicolon (t); 1444 1445 /* If we got any attributes in class_head, xref_tag will stick them in 1446 TREE_TYPE of the type. Grab them now. */ 1447 attributes = chainon (TREE_TYPE (t), attributes); 1448 TREE_TYPE (t) = NULL_TREE; 1449 1450 if (TREE_CODE (t) == ENUMERAL_TYPE) 1451 ; 1452 else 1453 { 1454 t = finish_struct (t, attributes, semi); 1455 if (semi) 1456 note_got_semicolon (t); 1457 } 1458 1459 pop_obstacks (); 1460 1461 if (! semi) 1462 check_for_missing_semicolon (t); 1463 if (pop_scope_p) 1464 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t))); 1465 if (current_scope () == current_function_decl) 1466 do_pending_defargs (); 1467 1468 return t; 1469} 1470 1471/* Finish processing the default argument expressions cached during 1472 the processing of a class definition. */ 1473 1474void 1475begin_inline_definitions () 1476{ 1477 if (pending_inlines 1478 && current_scope () == current_function_decl) 1479 do_pending_inlines (); 1480} 1481 1482/* Finish processing the inline function definitions cached during the 1483 processing of a class definition. */ 1484 1485void 1486finish_inline_definitions () 1487{ 1488 if (current_class_type == NULL_TREE) 1489 clear_inline_text_obstack (); 1490 1491 /* Undo the begin_tree in begin_class_definition. */ 1492 end_tree (); 1493} 1494 1495/* Finish processing the declaration of a member class template 1496 TYPES whose template parameters are given by PARMS. */ 1497 1498tree 1499finish_member_class_template (types) 1500 tree types; 1501{ 1502 tree t; 1503 1504 /* If there are declared, but undefined, partial specializations 1505 mixed in with the typespecs they will not yet have passed through 1506 maybe_process_partial_specialization, so we do that here. */ 1507 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t)) 1508 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t)))) 1509 maybe_process_partial_specialization (TREE_VALUE (t)); 1510 1511 note_list_got_semicolon (types); 1512 grok_x_components (types); 1513 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type) 1514 /* The component was in fact a friend declaration. We avoid 1515 finish_member_template_decl performing certain checks by 1516 unsetting TYPES. */ 1517 types = NULL_TREE; 1518 1519 finish_member_template_decl (types); 1520 1521 /* As with other component type declarations, we do 1522 not store the new DECL on the list of 1523 component_decls. */ 1524 return NULL_TREE; 1525} 1526 1527/* Finish processsing a complete template declaration. The PARMS are 1528 the template parameters. */ 1529 1530void 1531finish_template_decl (parms) 1532 tree parms; 1533{ 1534 if (parms) 1535 end_template_decl (); 1536 else 1537 end_specialization (); 1538} 1539 1540/* Finish processing a a template-id (which names a type) of the form 1541 NAME < ARGS >. Return the TYPE_DECL for the type named by the 1542 template-id. If ENTERING_SCOPE is non-zero we are about to enter 1543 the scope of template-id indicated. */ 1544 1545tree 1546finish_template_type (name, args, entering_scope) 1547 tree name; 1548 tree args; 1549 int entering_scope; 1550{ 1551 tree decl; 1552 1553 decl = lookup_template_class (name, args, 1554 NULL_TREE, NULL_TREE, entering_scope); 1555 if (decl != error_mark_node) 1556 decl = TYPE_STUB_DECL (decl); 1557 1558 return decl; 1559} 1560 1561/* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a 1562 namespace scope or a class scope. */ 1563 1564void 1565enter_scope_of (sr) 1566 tree sr; 1567{ 1568 tree scope = TREE_OPERAND (sr, 0); 1569 1570 if (TREE_CODE (scope) == NAMESPACE_DECL) 1571 { 1572 push_decl_namespace (scope); 1573 TREE_COMPLEXITY (sr) = -1; 1574 } 1575 else if (scope != current_class_type) 1576 { 1577 if (TREE_CODE (scope) == TYPENAME_TYPE) 1578 { 1579 /* In a declarator for a template class member, the scope will 1580 get here as an implicit typename, a TYPENAME_TYPE with a type. */ 1581 scope = TREE_TYPE (scope); 1582 TREE_OPERAND (sr, 0) = scope; 1583 } 1584 push_nested_class (scope, 3); 1585 TREE_COMPLEXITY (sr) = current_class_depth; 1586 } 1587} 1588 1589/* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER. 1590 Return a TREE_LIST containing the ACCESS_SPECIFIER and the 1591 BASE_CLASS, or NULL_TREE if an error occurred. The 1592 ACCESSS_SPECIFIER is one of 1593 access_{default,public,protected_private}[_virtual]_node.*/ 1594 1595tree 1596finish_base_specifier (access_specifier, base_class, 1597 current_aggr_is_signature) 1598 tree access_specifier; 1599 tree base_class; 1600 int current_aggr_is_signature; 1601{ 1602 tree type; 1603 tree result; 1604 1605 if (base_class == NULL_TREE) 1606 { 1607 error ("invalid base class"); 1608 type = error_mark_node; 1609 } 1610 else 1611 type = TREE_TYPE (base_class); 1612 if (current_aggr_is_signature && access_specifier) 1613 error ("access and source specifiers not allowed in signature"); 1614 if (! is_aggr_type (type, 1)) 1615 result = NULL_TREE; 1616 else if (current_aggr_is_signature 1617 && (! type) && (! IS_SIGNATURE (type))) 1618 { 1619 error ("class name not allowed as base signature"); 1620 result = NULL_TREE; 1621 } 1622 else if (current_aggr_is_signature) 1623 { 1624 sorry ("signature inheritance, base type `%s' ignored", 1625 IDENTIFIER_POINTER (access_specifier)); 1626 result = build_tree_list (access_public_node, type); 1627 } 1628 else if (type && IS_SIGNATURE (type)) 1629 { 1630 error ("signature name not allowed as base class"); 1631 result = NULL_TREE; 1632 } 1633 else 1634 result = build_tree_list (access_specifier, type); 1635 1636 return result; 1637} 1638 1639/* Called when multiple declarators are processed. If that is not 1640 premitted in this context, an error is issued. */ 1641 1642void 1643check_multiple_declarators () 1644{ 1645 /* [temp] 1646 1647 In a template-declaration, explicit specialization, or explicit 1648 instantiation the init-declarator-list in the declaration shall 1649 contain at most one declarator. 1650 1651 We don't just use PROCESSING_TEMPLATE_DECL for the first 1652 condition since that would disallow the perfectly legal code, 1653 like `template <class T> struct S { int i, j; };'. */ 1654 tree scope = current_scope (); 1655 1656 if (scope && TREE_CODE (scope) == FUNCTION_DECL) 1657 /* It's OK to write `template <class T> void f() { int i, j;}'. */ 1658 return; 1659 1660 if (PROCESSING_REAL_TEMPLATE_DECL_P () 1661 || processing_explicit_instantiation 1662 || processing_specialization) 1663 cp_error ("multiple declarators in template declaration"); 1664} 1665 1666tree 1667finish_typeof (expr) 1668 tree expr; 1669{ 1670 if (processing_template_decl) 1671 { 1672 tree t; 1673 1674 push_obstacks_nochange (); 1675 end_temporary_allocation (); 1676 1677 t = make_lang_type (TYPEOF_TYPE); 1678 TYPE_FIELDS (t) = expr; 1679 1680 pop_obstacks (); 1681 1682 return t; 1683 } 1684 1685 return TREE_TYPE (expr); 1686} 1687