LCOV - code coverage report
Current view: top level - gcc/cp - contracts.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.5 % 1318 1272
Test Date: 2026-02-28 14:20:25 Functions: 100.0 % 99 99
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* C++ contracts.
       2              : 
       3              :    Copyright (C) 2020-2026 Free Software Foundation, Inc.
       4              :    Originally by Jeff Chapman II (jchapman@lock3software.com) for proposed
       5              :    C++20 contracts.
       6              :    Rewritten for C++26 contracts by:
       7              :      Nina Ranns (dinka.ranns@googlemail.com)
       8              :      Iain Sandoe (iain@sandoe.co.uk)
       9              :      Ville Voutilainen (ville.voutilainen@gmail.com).
      10              : 
      11              : This file is part of GCC.
      12              : 
      13              : GCC is free software; you can redistribute it and/or modify
      14              : it under the terms of the GNU General Public License as published by
      15              : the Free Software Foundation; either version 3, or (at your option)
      16              : any later version.
      17              : 
      18              : GCC is distributed in the hope that it will be useful,
      19              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      20              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21              : GNU General Public License for more details.
      22              : 
      23              : You should have received a copy of the GNU General Public License
      24              : along with GCC; see the file COPYING3.  If not see
      25              : <http://www.gnu.org/licenses/>.  */
      26              : 
      27              : #include "config.h"
      28              : #include "system.h"
      29              : #include "coretypes.h"
      30              : #include "cp-tree.h"
      31              : #include "stringpool.h"
      32              : #include "diagnostic.h"
      33              : #include "options.h"
      34              : #include "contracts.h"
      35              : #include "tree.h"
      36              : #include "tree-inline.h"
      37              : #include "attribs.h"
      38              : #include "tree-iterator.h"
      39              : #include "print-tree.h"
      40              : #include "stor-layout.h"
      41              : #include "intl.h"
      42              : #include "cgraph.h"
      43              : #include "opts.h"
      44              : #include "output.h"
      45              : 
      46              : /*  Design notes.
      47              : 
      48              :   There are three phases:
      49              :     1. Parsing and semantic checks.
      50              :        Most of the code for this is in the parser, with helpers provided here.
      51              :     2. Emitting contract assertion AST nodes into function bodies.
      52              :        This is initiated from "finish_function ()"
      53              :     3. Lowering the contract assertion AST nodes to control flow, constant
      54              :        data and calls to the violation handler.
      55              :        This is initiated from "cp_genericize ()".
      56              : 
      57              :   The organisation of the code in this file is intended to follow those three
      58              :   phases where possible.
      59              : 
      60              :   Contract Assertion State
      61              :   ========================
      62              : 
      63              :   contract_assert () does not require any special handling and can be
      64              :   represented directly by AST inserted in the function body.
      65              : 
      66              :   'pre' and 'post' function contract specifiers require most of the special
      67              :   handling, since they must be tracked across re-declarations of functions and
      68              :   there are contraints on how such specifiers may change in these cases.
      69              : 
      70              :   The contracts specification identifies a "first declaration" of any given
      71              :   function - which is the first encountered when parsing a given TU.
      72              :   Subsequent re-declarations may not add or change the function contract
      73              :   specifiers from any introduced on this first declaration.  It is, however,
      74              :   permitted to omit specifiers on re-declarations.
      75              : 
      76              :   Since the implementation of GCC's (re-)declarations is a destructive merge
      77              :   we need to keep some state on the side to determine whether the re-declaration
      78              :   rules are met.  In this current design we have chosen not to add another tree
      79              :   to each function decl but, instead, keep a map from function decl to contract
      80              :   specifier state.  In this state we record the 'first declaration' specifiers
      81              :   which are used to validate re-declaration(s) and to report the initial state
      82              :   in diagnostics.
      83              : 
      84              :   We need (for example) to compare
      85              :     pre ( x > 2 ) equal to
      86              :     pre ( z > 2 ) when x and z refer to the same function parameter in a
      87              :     re-declaration.
      88              : 
      89              :   The mechanism used to determine if two contracts are the same is to compare
      90              :   the folded trees.  This makes use of current compiler machinery, rather than
      91              :   constructing some new AST comparison scheme.  However, it does introduce an
      92              :   additional complexity in that we need to defer such comparison until parsing
      93              :   is complete - and function contract specifiers in class declarations must be
      94              :   deferred parses, since it is also permitted for specifiers to refer to class
      95              :   members.
      96              : 
      97              :   When we encounter a definition, the parameter names in a function decl are
      98              :   re-written to match those of the definition (thus the expected names will
      99              :   appear in debug information etc).  At this point, we also need to re-map
     100              :   any function parameter names that appear in function contract specifiers
     101              :   to agree with those of the definition - although we intend to keep the
     102              :   'first declaration' record consistent for diagnostics.
     103              : 
     104              :   Since we shared some code from the C++2a contracts implementation, pre and
     105              :   post specifiers are represented by chains of attributes, where the payload
     106              :   of the attribute is an AST node.  However during the parse, these are not
     107              :   inserted into the function bodies, but kept in the decl-keyed state described
     108              :   above.  A future improvement planned here is to store the specifiers using a
     109              :   tree vec instead of the attribute list.
     110              : 
     111              :   Emitting contract AST
     112              :   =====================
     113              : 
     114              :   When we reach `finish_function ()` and therefore are committed to potentially
     115              :   emitting code for an instance, we build a new variant of the function body
     116              :   with the pre-condition AST inserted before the user's function body, and the
     117              :   post condition AST (if any) linked into the function return.
     118              : 
     119              :   Lowering the contract assertion AST
     120              :   ===================================
     121              : 
     122              :   In all cases (pre, post, contract_assert) the AST node is lowered to control
     123              :   flow and (potentially) calls to the violation handler and/or termination.
     124              :   This is done during `cp_genericize ()`.  In the current implementation, the
     125              :   decision on the control flow is made on the basis of the setting of a command-
     126              :   line flag that determines a TU-wide contract evaluation semantic, which has
     127              :   the following initial set of behaviours:
     128              : 
     129              :     'ignore'        : contract assertion AST is lowered to 'nothing',
     130              :                       i.e. omitted.
     131              :     'enforce'       : contract assertion AST is lowered to a check, if this
     132              :                       fails a violation handler is called, followed by
     133              :                       std::terminate().
     134              :     'quick_enforce' : contract assertion AST is lowered to a check, if this
     135              :                       fails, std::terminate () is called.
     136              :     'observe'       : contract assertion AST is lowered to a check, if this
     137              :                       fails, a violation handler is called, the code then
     138              :                       continues.
     139              : 
     140              :   In each case, the "check" might be a simple 'if' (when it is determined that
     141              :   the assertion condition does not throw) or the condition evaluation will be
     142              :   wrapped in a try-catch block that treats any exception thrown when evaluating
     143              :   the check as equivalent to a failed check.  It is noted in the violation data
     144              :   object whether a check failed because of an exception raised in evaluation.
     145              : 
     146              :   At present, a simple (but potentially space-inefficient) scheme is used to
     147              :   store constant data objects that represent the read-only data for the
     148              :   violation.  The exact form of this is subject to revision as it represents
     149              :   ABI that must be agreed between implementations (as of this point, that
     150              :   discussion is not yet concluded).  */
     151              : 
     152              : /* Contract matching.  */
     153              : 
     154              : bool comparing_contracts;
     155              : 
     156              : /* True if the contract is valid.  */
     157              : 
     158              : static bool
     159           80 : contract_valid_p (tree contract)
     160              : {
     161           80 :   return CONTRACT_CONDITION (contract) != error_mark_node;
     162              : }
     163              : 
     164              : /* True if the contract specifier is valid.  */
     165              : 
     166              : static bool
     167           80 : contract_specifier_valid_p (tree contract)
     168              : {
     169           80 :   return contract_valid_p (TREE_VALUE (TREE_VALUE (contract)));
     170              : }
     171              : 
     172              : /* Compare the contract conditions of OLD_CONTRACT and NEW_CONTRACT.
     173              :    Returns false if the conditions are equivalent, and true otherwise.  */
     174              : 
     175              : static bool
     176           40 : mismatched_contracts_p (tree old_contract, tree new_contract)
     177              : {
     178              :   /* Different kinds of contracts do not match.  */
     179           40 :   if (TREE_CODE (old_contract) != TREE_CODE (new_contract))
     180              :     {
     181            0 :       auto_diagnostic_group d;
     182            0 :       error_at (EXPR_LOCATION (new_contract),
     183              :                 "mismatched contract specifier in declaration");
     184            0 :       inform (EXPR_LOCATION (old_contract), "previous contract here");
     185            0 :       return true;
     186            0 :     }
     187              : 
     188              :   /* A deferred contract tentatively matches.  */
     189           40 :   if (CONTRACT_CONDITION_DEFERRED_P (new_contract))
     190              :     return false;
     191              : 
     192              :   /* Compare the conditions of the contracts.  */
     193           40 :   tree t1 = cp_fully_fold_init (CONTRACT_CONDITION (old_contract));
     194           40 :   tree t2 = cp_fully_fold_init (CONTRACT_CONDITION (new_contract));
     195              : 
     196              :   /* Compare the contracts. */
     197              : 
     198           40 :   bool saved_comparing_contracts = comparing_contracts;
     199           40 :   comparing_contracts = true;
     200           40 :   bool matching_p = cp_tree_equal (t1, t2);
     201           40 :   comparing_contracts = saved_comparing_contracts;
     202              : 
     203           40 :   if (!matching_p)
     204              :     {
     205           10 :       auto_diagnostic_group d;
     206           10 :       error_at (EXPR_LOCATION (CONTRACT_CONDITION (new_contract)),
     207              :                 "mismatched contract condition in declaration");
     208           10 :       inform (EXPR_LOCATION (CONTRACT_CONDITION (old_contract)),
     209              :               "previous contract here");
     210           10 :       return true;
     211           10 :     }
     212              : 
     213              :   return false;
     214              : }
     215              : 
     216              : /* Compare the contract specifiers of OLDDECL and NEWDECL. Returns true
     217              :    if the contracts match, and false if they differ.  */
     218              : 
     219              : static bool
     220           38 : match_contract_specifiers (location_t oldloc, tree old_contracts,
     221              :                            location_t newloc, tree new_contracts)
     222              : {
     223              :   /* Contracts only match if they are both specified.  */
     224           38 :   if (!old_contracts || !new_contracts)
     225              :     return true;
     226              : 
     227              :   /* Compare each contract in turn.  */
     228           68 :   while (old_contracts && new_contracts)
     229              :     {
     230              :       /* If either contract is ill-formed, skip the rest of the comparison,
     231              :          since we've already diagnosed an error.  */
     232           40 :       if (!contract_specifier_valid_p (new_contracts)
     233           40 :           || !contract_specifier_valid_p (old_contracts))
     234              :         return false;
     235              : 
     236           80 :       if (mismatched_contracts_p (CONTRACT_STATEMENT (old_contracts),
     237           40 :                                   CONTRACT_STATEMENT (new_contracts)))
     238              :         return false;
     239           30 :       old_contracts = TREE_CHAIN (old_contracts);
     240           30 :       new_contracts = TREE_CHAIN (new_contracts);
     241              :     }
     242              : 
     243              :   /* If we didn't compare all specifiers, the contracts don't match.  */
     244           28 :   if (old_contracts || new_contracts)
     245              :     {
     246            4 :       auto_diagnostic_group d;
     247            4 :       error_at (newloc,
     248              :                 "declaration has a different number of contracts than "
     249              :                 "previously declared");
     250            4 :       inform (oldloc,
     251              :               new_contracts
     252              :               ? "previous declaration with fewer contracts here"
     253              :               : "previous declaration with more contracts here");
     254            4 :       return false;
     255            4 :     }
     256              : 
     257              :   return true;
     258              : }
     259              : 
     260              : /* Return true if CONTRACT is checked or assumed under the current build
     261              :    configuration. */
     262              : 
     263              : static bool
     264         1990 : contract_active_p (tree contract)
     265              : {
     266          703 :   return get_evaluation_semantic (contract) != CES_IGNORE;
     267              : }
     268              : 
     269              : /* True if FNDECL has any checked or assumed contracts whose TREE_CODE is
     270              :    C.  */
     271              : 
     272              : static bool
     273      2236050 : has_active_contract_condition (tree fndecl, tree_code c)
     274              : {
     275      2236050 :   tree as = get_fn_contract_specifiers (fndecl);
     276      2236824 :   for (; as != NULL_TREE; as = TREE_CHAIN (as))
     277              :     {
     278         1477 :       tree contract = TREE_VALUE (TREE_VALUE (as));
     279         2180 :       if (TREE_CODE (contract) == c && contract_active_p (contract))
     280              :         return true;
     281              :     }
     282              :   return false;
     283              : }
     284              : 
     285              : /* True if FNDECL has any checked or assumed preconditions.  */
     286              : 
     287              : static bool
     288          585 : has_active_preconditions (tree fndecl)
     289              : {
     290            0 :   return has_active_contract_condition (fndecl, PRECONDITION_STMT);
     291              : }
     292              : 
     293              : /* True if FNDECL has any checked or assumed postconditions.  */
     294              : 
     295              : static bool
     296      2235465 : has_active_postconditions (tree fndecl)
     297              : {
     298            0 :   return has_active_contract_condition (fndecl, POSTCONDITION_STMT);
     299              : }
     300              : 
     301              : /* Return true if any contract in the CONTRACT list is checked or assumed
     302              :    under the current build configuration.  */
     303              : 
     304              : static bool
     305       296137 : contract_any_active_p (tree fndecl)
     306              : {
     307       296137 :   tree as = get_fn_contract_specifiers (fndecl);
     308       592282 :   for (; as; as = TREE_CHAIN (as))
     309         1287 :     if (contract_active_p (TREE_VALUE (TREE_VALUE (as))))
     310              :       return true;
     311              :   return false;
     312              : }
     313              : 
     314              : /* Return true if any contract in CONTRACTS is not yet parsed.  */
     315              : 
     316              : bool
     317          871 : contract_any_deferred_p (tree contracts)
     318              : {
     319         1675 :   for (; contracts; contracts = TREE_CHAIN (contracts))
     320         1088 :     if (CONTRACT_CONDITION_DEFERRED_P (CONTRACT_STATEMENT (contracts)))
     321              :       return true;
     322              :   return false;
     323              : }
     324              : 
     325              : /* Returns true if function decl FNDECL has contracts and we need to
     326              :    process them for the purposes of either building caller or definition
     327              :    contract checks.
     328              :    This function does not take into account whether caller or definition
     329              :    side checking is enabled. Those checks will be done from the calling
     330              :    function which will be able to determine whether it is doing caller
     331              :    or definition contract handling.  */
     332              : 
     333              : static bool
     334    699288888 : handle_contracts_p (tree fndecl)
     335              : {
     336    699288888 :   return (flag_contracts
     337       394431 :           && !processing_template_decl
     338       296185 :           && (CONTRACT_HELPER (fndecl) == ldf_contract_none)
     339    699585025 :           && contract_any_active_p (fndecl));
     340              : }
     341              : 
     342              : /* For use with the tree inliner. This preserves non-mapped local variables,
     343              :    such as postcondition result variables, during remapping.  */
     344              : 
     345              : static tree
     346          468 : retain_decl (tree decl, copy_body_data *)
     347              : {
     348          468 :   return decl;
     349              : }
     350              : 
     351              : /* Lookup a name in std::, or inject it.  */
     352              : 
     353              : static tree
     354          104 : lookup_std_type (tree name_id)
     355              : {
     356          104 :   tree res_type = lookup_qualified_name
     357          104 :     (std_node, name_id, LOOK_want::TYPE | LOOK_want::HIDDEN_FRIEND);
     358              : 
     359          104 :   if (TREE_CODE (res_type) == TYPE_DECL)
     360           22 :     res_type = TREE_TYPE (res_type);
     361              :   else
     362              :     {
     363           82 :       push_nested_namespace (std_node);
     364           82 :       res_type = make_class_type (RECORD_TYPE);
     365           82 :       create_implicit_typedef (name_id, res_type);
     366           82 :       DECL_SOURCE_LOCATION (TYPE_NAME (res_type)) = BUILTINS_LOCATION;
     367           82 :       DECL_CONTEXT (TYPE_NAME (res_type)) = current_namespace;
     368           82 :       pushdecl_namespace_level (TYPE_NAME (res_type), /*hidden*/true);
     369           82 :       pop_nested_namespace (std_node);
     370              :     }
     371          104 :   return res_type;
     372              : }
     373              : 
     374              : /* Get constract_assertion_kind of the specified contract. Used when building
     375              :   contract_violation object.  */
     376              : 
     377              : static contract_assertion_kind
     378          608 : get_contract_assertion_kind (tree contract)
     379              : {
     380          608 :   if (CONTRACT_ASSERTION_KIND (contract))
     381              :     {
     382          608 :       tree s = CONTRACT_ASSERTION_KIND (contract);
     383          608 :       tree i = (TREE_CODE (s) == INTEGER_CST) ? s
     384            0 :                                               : DECL_INITIAL (STRIP_NOPS (s));
     385          608 :       gcc_checking_assert (!type_dependent_expression_p (s) && i);
     386          608 :       return (contract_assertion_kind) tree_to_uhwi (i);
     387              :     }
     388              : 
     389            0 :   switch (TREE_CODE (contract))
     390              :   {
     391              :     case ASSERTION_STMT:        return CAK_ASSERT;
     392              :     case PRECONDITION_STMT:     return CAK_PRE;
     393              :     case POSTCONDITION_STMT:    return CAK_POST;
     394            0 :     default: break;
     395              :   }
     396              : 
     397            0 :   gcc_unreachable ();
     398              : }
     399              : 
     400              : /* Get contract_evaluation_semantic of the specified contract.  */
     401              : 
     402              : contract_evaluation_semantic
     403         3173 : get_evaluation_semantic (const_tree contract)
     404              : {
     405         3173 :   if (CONTRACT_EVALUATION_SEMANTIC (contract))
     406              :     {
     407         3173 :       tree s = CONTRACT_EVALUATION_SEMANTIC (contract);
     408         3173 :       tree i = (TREE_CODE (s) == INTEGER_CST) ? s
     409            0 :                                               : DECL_INITIAL (STRIP_NOPS (s));
     410         3173 :       gcc_checking_assert (!type_dependent_expression_p (s) && i);
     411         3173 :       switch (contract_evaluation_semantic ev =
     412         3173 :               (contract_evaluation_semantic) tree_to_uhwi (i))
     413              :         {
     414              :         /* This needs to be kept in step with any added semantics.  */
     415         3173 :         case CES_IGNORE:
     416         3173 :         case CES_OBSERVE:
     417         3173 :         case CES_ENFORCE:
     418         3173 :         case CES_QUICK:
     419         3173 :           return ev;
     420              :         default:
     421              :           break;
     422              :         }
     423              :     }
     424              : 
     425            0 :   gcc_unreachable ();
     426              : }
     427              : 
     428              : /* Get location of the last contract in the CONTRACTS tree chain.  */
     429              : 
     430              : static location_t
     431          881 : get_contract_end_loc (tree contracts)
     432              : {
     433          881 :   tree last = NULL_TREE;
     434         2364 :   for (tree a = contracts; a; a = TREE_CHAIN (a))
     435         1483 :     last = a;
     436          881 :   gcc_checking_assert (last);
     437          881 :   last = CONTRACT_STATEMENT (last);
     438          881 :   return EXPR_LOCATION (last);
     439              : }
     440              : 
     441              : struct GTY(()) contract_decl
     442              : {
     443              :   tree contract_specifiers;
     444              :   location_t note_loc;
     445              : };
     446              : 
     447              : static GTY(()) hash_map<tree, contract_decl> *contract_decl_map;
     448              : 
     449              : /* Converts a contract condition to bool and ensures it has a location.  */
     450              : 
     451              : tree
     452         1370 : finish_contract_condition (cp_expr condition)
     453              : {
     454         1370 :   if (!condition || error_operand_p (condition))
     455              :     return condition;
     456              : 
     457              :   /* Ensure we have the condition location saved in case we later need to
     458              :      emit a conversion error during template instantiation and wouldn't
     459              :      otherwise have it.  This differs from maybe_wrap_with_location in that
     460              :      it allows wrappers on EXCEPTIONAL_CLASS_P which includes CONSTRUCTORs.  */
     461         1360 :   if (!CAN_HAVE_LOCATION_P (condition)
     462           68 :       && condition.get_location () != UNKNOWN_LOCATION)
     463              :     {
     464           68 :       tree_code code
     465           68 :         = (((CONSTANT_CLASS_P (condition) && TREE_CODE (condition) != STRING_CST)
     466            0 :             || (TREE_CODE (condition) == CONST_DECL && !TREE_STATIC (condition)))
     467           68 :           ? NON_LVALUE_EXPR : VIEW_CONVERT_EXPR);
     468           68 :       condition = build1_loc (condition.get_location (), code,
     469           68 :                               TREE_TYPE (condition), condition);
     470           68 :       EXPR_LOCATION_WRAPPER_P (condition) = true;
     471              :     }
     472              : 
     473         1360 :   if (type_dependent_expression_p (condition))
     474              :     return condition;
     475              : 
     476         1013 :   return condition_conversion (condition);
     477              : }
     478              : 
     479              : /* Wrap the DECL into VIEW_CONVERT_EXPR representing const qualified version
     480              :    of the declaration.  */
     481              : 
     482              : tree
     483         1637 : view_as_const (tree decl)
     484              : {
     485         1637 :   if (!contract_const_wrapper_p (decl))
     486              :     {
     487         1637 :       tree ctype = TREE_TYPE (decl);
     488         1637 :       location_t loc =
     489         1637 :           EXPR_P (decl) ? EXPR_LOCATION (decl) : DECL_SOURCE_LOCATION (decl);
     490         1637 :       ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
     491              :                                                | TYPE_QUAL_CONST));
     492         1637 :       decl = build1 (VIEW_CONVERT_EXPR, ctype, decl);
     493         1637 :       SET_EXPR_LOCATION (decl, loc);
     494              :       /* Mark the VCE as contract const wrapper.  */
     495         1637 :       CONST_WRAPPER_P (decl) = true;
     496              :     }
     497         1637 :   return decl;
     498              : }
     499              : 
     500              : /* Constify access to DECL from within the contract condition.  */
     501              : 
     502              : tree
     503         1670 : constify_contract_access (tree decl)
     504              : {
     505              :   /* We check if we have a variable, a parameter, a variable of reference type,
     506              :    * or a parameter of reference type
     507              :    */
     508         1670 :   if (!TREE_READONLY (decl)
     509         1670 :       && (VAR_P (decl)
     510         1398 :           || (TREE_CODE (decl) == PARM_DECL)
     511          462 :           || (REFERENCE_REF_P (decl)
     512           90 :               && (VAR_P (TREE_OPERAND (decl, 0))
     513           87 :                   || (TREE_CODE (TREE_OPERAND (decl, 0)) == PARM_DECL)
     514            5 :                   || (TREE_CODE (TREE_OPERAND (decl, 0))
     515              :                       == TEMPLATE_PARM_INDEX)))))
     516         1101 :     decl = view_as_const (decl);
     517              : 
     518         1670 :   return decl;
     519              : }
     520              : 
     521              : /* Indicate that PARM_DECL DECL is ODR used in a postcondition.  */
     522              : 
     523              : static void
     524          532 : set_parm_used_in_post (tree decl, bool constify = true)
     525              : {
     526          532 :   gcc_checking_assert (TREE_CODE (decl) == PARM_DECL);
     527          532 :   DECL_LANG_FLAG_4 (decl) = constify;
     528          532 : }
     529              : 
     530              : /* Test if PARM_DECL is ODR used in a postcondition.  */
     531              : 
     532              : static bool
     533          483 : parm_used_in_post_p (const_tree decl)
     534              : {
     535              :   /* Check if this parameter is odr used within a function's postcondition  */
     536          483 :   return ((TREE_CODE (decl) == PARM_DECL) && DECL_LANG_FLAG_4 (decl));
     537              : }
     538              : 
     539              : /* If declaration DECL is a PARM_DECL and it appears in a postcondition, then
     540              :    check that it is not a non-const by-value param. LOCATION is where the
     541              :    expression was found and is used for diagnostic purposes.  */
     542              : 
     543              : void
     544    699533398 : check_param_in_postcondition (tree decl, location_t location)
     545              : {
     546    699533398 :   if (processing_postcondition
     547         1063 :       && TREE_CODE (decl) == PARM_DECL
     548              :       /* TREE_CODE (decl) == PARM_DECL only holds for non-reference
     549              :          parameters.  */
     550          670 :       && !cp_unevaluated_operand
     551              :       /* Return value parameter has DECL_ARTIFICIAL flag set. The flag
     552              :          presence of the flag should be sufficient to distinguish the
     553              :          return value parameter in this context.  */
     554    699533924 :       && !(DECL_ARTIFICIAL (decl)))
     555              :     {
     556          370 :       set_parm_used_in_post (decl);
     557              : 
     558          370 :       if (!dependent_type_p (TREE_TYPE (decl))
     559          370 :           && !CP_TYPE_CONST_P (TREE_TYPE (decl)))
     560              :         {
     561           76 :           error_at (location,
     562              :                     "a value parameter used in a postcondition must be const");
     563           76 :           inform (DECL_SOURCE_LOCATION (decl), "parameter declared here");
     564              :         }
     565              :     }
     566    699533398 : }
     567              : 
     568              : /* Check if parameters used in postconditions are const qualified on
     569              :    a redeclaration that does not specify contracts or on an instantiation
     570              :    of a function template.  */
     571              : 
     572              : void
     573    149546080 : check_postconditions_in_redecl (tree olddecl, tree newdecl)
     574              : {
     575    149546080 :   tree contract_spec = get_fn_contract_specifiers (olddecl);
     576    149546080 :   if (!contract_spec)
     577              :     return;
     578              : 
     579          284 :   tree t1 = FUNCTION_FIRST_USER_PARM (olddecl);
     580          284 :   tree t2 = FUNCTION_FIRST_USER_PARM (newdecl);
     581              : 
     582         1051 :   for (; t1 && t1 != void_list_node;
     583          483 :   t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
     584              :     {
     585          483 :       if (parm_used_in_post_p (t1))
     586              :         {
     587          162 :           set_parm_used_in_post (t2);
     588          162 :           if (!dependent_type_p (TREE_TYPE (t2))
     589          126 :               && !CP_TYPE_CONST_P (TREE_TYPE (t2))
     590          224 :               && !TREE_READONLY (t2))
     591              :             {
     592           62 :               error_at (DECL_SOURCE_LOCATION (t2),
     593              :               "value parameter %qE used in a postcondition must be const", t2);
     594           62 :               inform (DECL_SOURCE_LOCATION (olddecl),
     595              :               "previous declaration here");
     596              :             }
     597              :         }
     598              :     }
     599              : }
     600              : 
     601              : /* Map from FUNCTION_DECL to a FUNCTION_DECL for either the PRE_FN or POST_FN.
     602              :    These are used to parse contract conditions and are called inside the body
     603              :    of the guarded function.  */
     604              : static GTY(()) hash_map<tree, tree> *decl_pre_fn;
     605              : static GTY(()) hash_map<tree, tree> *decl_post_fn;
     606              : 
     607              : /* Given a pre or post function decl (for an outlined check function) return
     608              :    the decl for the function for which the outlined checks are being
     609              :    performed.  */
     610              : static GTY(()) hash_map<tree, tree> *orig_from_outlined;
     611              : 
     612              : /* Makes PRE the precondition function for FNDECL.  */
     613              : 
     614              : static void
     615            8 : set_precondition_function (tree fndecl, tree pre)
     616              : {
     617            8 :   gcc_assert (pre);
     618            8 :   hash_map_maybe_create<hm_ggc> (decl_pre_fn);
     619            8 :   gcc_checking_assert (!decl_pre_fn->get (fndecl));
     620            8 :   decl_pre_fn->put (fndecl, pre);
     621              : 
     622            8 :   hash_map_maybe_create<hm_ggc> (orig_from_outlined);
     623            8 :   gcc_checking_assert (!orig_from_outlined->get (pre));
     624            8 :   orig_from_outlined->put (pre, fndecl);
     625            8 : }
     626              : 
     627              : /* Makes POST the postcondition function for FNDECL.  */
     628              : 
     629              : static void
     630           26 : set_postcondition_function (tree fndecl, tree post)
     631              : {
     632           26 :   gcc_checking_assert (post);
     633           26 :   hash_map_maybe_create<hm_ggc> (decl_post_fn);
     634           26 :   gcc_checking_assert (!decl_post_fn->get (fndecl));
     635           26 :   decl_post_fn->put (fndecl, post);
     636              : 
     637           26 :   hash_map_maybe_create<hm_ggc> (orig_from_outlined);
     638           26 :   gcc_checking_assert (!orig_from_outlined->get (post));
     639           26 :   orig_from_outlined->put (post, fndecl);
     640           26 : }
     641              : 
     642              : /* For a given pre or post condition function, find the checked function.  */
     643              : tree
     644           18 : get_orig_for_outlined (tree fndecl)
     645              : {
     646           18 :   gcc_checking_assert (fndecl);
     647           18 :   tree *result = hash_map_safe_get (orig_from_outlined, fndecl);
     648           18 :   return result ? *result : NULL_TREE ;
     649              : }
     650              : 
     651              : /* For a given function OLD_FN set suitable names for NEW_FN (which is an
     652              :    outlined contract check) usually by appending '.pre' or '.post'.
     653              : 
     654              :    For functions with special meaning names (i.e. main and cdtors) we need to
     655              :    make special provisions and therefore handle all the contracts function
     656              :    name changes here, rather than requiring a separate update to mangle.cc.
     657              : 
     658              :    PRE specifies if we need an identifier for a pre or post contract check.  */
     659              : 
     660              : static void
     661           56 : contracts_fixup_names (tree new_fn, tree old_fn, bool pre, bool wrapper)
     662              : {
     663           56 :   bool cdtor = DECL_CXX_CONSTRUCTOR_P (old_fn)
     664           56 :                || DECL_CXX_DESTRUCTOR_P (old_fn);
     665           56 :   const char *fname = IDENTIFIER_POINTER (DECL_NAME (old_fn));
     666           82 :   const char *append = wrapper ? "contract_wrapper"
     667           34 :                                : (pre ? "pre" : "post");
     668           56 :   size_t len = strlen (fname);
     669              :   /* Cdtor names have a space at the end.  We need to remove that space
     670              :      when forming the new identifier.  */
     671           56 :   char *nn = xasprintf ("%.*s%s%s",
     672            0 :                         cdtor ? (int)len-1 : int(len),
     673              :                         fname,
     674              :                         JOIN_STR,
     675              :                         append);
     676           56 :   DECL_NAME (new_fn) = get_identifier (nn);
     677           56 :   free (nn);
     678              : 
     679              :   /* Now do the mangled version.  */
     680           56 :   fname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (old_fn));
     681           56 :   nn = xasprintf ("%s%s%s", fname, JOIN_STR, append);
     682           56 :   SET_DECL_ASSEMBLER_NAME (new_fn, get_identifier (nn));
     683           56 :   free (nn);
     684           56 : }
     685              : 
     686              : /* Build a declaration for the pre- or postcondition of a guarded FNDECL.  */
     687              : 
     688              : static tree
     689           34 : build_contract_condition_function (tree fndecl, bool pre)
     690              : {
     691           34 :   if (error_operand_p (fndecl))
     692            0 :     return error_mark_node;
     693              : 
     694              :   /* Start the copy.  */
     695           34 :   tree fn = copy_decl (fndecl);
     696              : 
     697              :   /* Don't propagate declaration attributes to the checking function,
     698              :      including the original contracts.  */
     699           34 :   DECL_ATTRIBUTES (fn) = NULL_TREE;
     700              : 
     701              :   /* If requested, disable optimisation of checking functions; this can, in
     702              :      some cases, prevent UB from eliding the checks themselves.  */
     703           34 :   if (flag_contract_disable_optimized_checks)
     704            0 :     DECL_ATTRIBUTES (fn)
     705            0 :       = tree_cons (get_identifier ("optimize"),
     706              :                    build_tree_list (NULL_TREE, build_string (3, "-O0")),
     707              :                    NULL_TREE);
     708              : 
     709              :   /* Now parse and add any internal representation of these attrs to the
     710              :      decl.  */
     711           34 :   if (DECL_ATTRIBUTES (fn))
     712            0 :     cplus_decl_attributes (&fn, DECL_ATTRIBUTES (fn), 0);
     713              : 
     714              :   /* A possible later optimization may delete unused args to prevent extra arg
     715              :      passing.  */
     716              :   /* Handle the args list.  */
     717           34 :   tree arg_types = NULL_TREE;
     718           34 :   tree *last = &arg_types;
     719           34 :   for (tree arg_type = TYPE_ARG_TYPES (TREE_TYPE (fn));
     720           89 :       arg_type && arg_type != void_list_node;
     721           55 :       arg_type = TREE_CHAIN (arg_type))
     722              :     {
     723           55 :       if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
     724           55 :           && TYPE_ARG_TYPES (TREE_TYPE (fn)) == arg_type)
     725           22 :       continue;
     726           33 :       *last = build_tree_list (TREE_PURPOSE (arg_type), TREE_VALUE (arg_type));
     727           33 :       last = &TREE_CHAIN (*last);
     728              :     }
     729              : 
     730              :   /* Copy the function parameters, if present.  Disable warnings for them.  */
     731           34 :   DECL_ARGUMENTS (fn) = NULL_TREE;
     732           34 :   if (DECL_ARGUMENTS (fndecl))
     733              :     {
     734           31 :       tree *last_a = &DECL_ARGUMENTS (fn);
     735           86 :       for (tree p = DECL_ARGUMENTS (fndecl); p; p = TREE_CHAIN (p))
     736              :         {
     737           55 :           *last_a = copy_decl (p);
     738           55 :           suppress_warning (*last_a);
     739           55 :           DECL_CONTEXT (*last_a) = fn;
     740           55 :           last_a = &TREE_CHAIN (*last_a);
     741              :         }
     742              :     }
     743              : 
     744           34 :   tree orig_fn_value_type = TREE_TYPE (TREE_TYPE (fn));
     745           34 :   if (!pre && !VOID_TYPE_P (orig_fn_value_type))
     746              :     {
     747              :       /* For post contracts that deal with a non-void function, append a
     748              :          parameter to pass the return value.  */
     749           20 :       tree name = get_identifier ("__r");
     750           20 :       tree parm = build_lang_decl (PARM_DECL, name, orig_fn_value_type);
     751           20 :       DECL_CONTEXT (parm) = fn;
     752           20 :       DECL_ARTIFICIAL (parm) = true;
     753           20 :       suppress_warning (parm);
     754           20 :       DECL_ARGUMENTS (fn) = chainon (DECL_ARGUMENTS (fn), parm);
     755           20 :       *last = build_tree_list (NULL_TREE, orig_fn_value_type);
     756           20 :       last = &TREE_CHAIN (*last);
     757              :     }
     758              : 
     759           34 :   *last = void_list_node;
     760              : 
     761           34 :   tree adjusted_type = NULL_TREE;
     762              : 
     763              :   /* The handlers are void fns.  */
     764           34 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl))
     765           22 :     adjusted_type = build_method_type_directly (DECL_CONTEXT (fndecl),
     766              :                                                 void_type_node,
     767              :                                                 arg_types);
     768              :   else
     769           12 :     adjusted_type = build_function_type (void_type_node, arg_types);
     770              : 
     771              :   /* If the original function is noexcept, build a noexcept function.  */
     772           34 :   if (flag_exceptions && type_noexcept_p (TREE_TYPE (fndecl)))
     773            4 :     adjusted_type = build_exception_variant (adjusted_type, noexcept_true_spec);
     774              : 
     775           34 :   TREE_TYPE (fn) = adjusted_type;
     776           34 :   DECL_RESULT (fn) = NULL_TREE; /* Let the start function code fill it in.  */
     777              : 
     778              :   /* The contract check functions are never a cdtor, nor virtual.  */
     779           34 :   DECL_CXX_DESTRUCTOR_P (fn) = DECL_CXX_CONSTRUCTOR_P (fn) = 0;
     780           34 :   DECL_VIRTUAL_P (fn) = false;
     781              : 
     782              :   /* Append .pre / .post to a usable name for the original function.  */
     783           34 :   contracts_fixup_names (fn, fndecl, pre, /*wrapper*/false);
     784              : 
     785           34 :   DECL_INITIAL (fn) = NULL_TREE;
     786           60 :   CONTRACT_HELPER (fn) = pre ? ldf_contract_pre : ldf_contract_post;
     787              :   /* We might have a pre/post for a wrapper.  */
     788           34 :   DECL_CONTRACT_WRAPPER (fn) = DECL_CONTRACT_WRAPPER (fndecl);
     789              : 
     790              :   /* Make these functions internal if we can, i.e. if the guarded function is
     791              :      not vague linkage, or if we can put them in a comdat group with the
     792              :      guarded function.  */
     793           34 :   if (!DECL_WEAK (fndecl) || HAVE_COMDAT_GROUP)
     794              :     {
     795           34 :       TREE_PUBLIC (fn) = false;
     796           34 :       DECL_EXTERNAL (fn) = false;
     797           34 :       DECL_WEAK (fn) = false;
     798           34 :       DECL_COMDAT (fn) = false;
     799              : 
     800              :       /* We may not have set the comdat group on the guarded function yet.
     801              :          If we haven't, we'll add this to the same group in comdat_linkage
     802              :          later.  Otherwise, add it to the same comdat group now.  */
     803           34 :       if (DECL_ONE_ONLY (fndecl))
     804              :         {
     805            0 :           symtab_node *n = symtab_node::get (fndecl);
     806            0 :           cgraph_node::get_create (fn)->add_to_same_comdat_group (n);
     807              :         }
     808              : 
     809              :     }
     810              : 
     811           34 :   DECL_INTERFACE_KNOWN (fn) = true;
     812           34 :   DECL_ARTIFICIAL (fn) = true;
     813           34 :   suppress_warning (fn);
     814              : 
     815           34 :   return fn;
     816              : }
     817              : 
     818              : /* Build the precondition checking function for FNDECL.  */
     819              : 
     820              : static tree
     821           15 : build_precondition_function (tree fndecl)
     822              : {
     823           15 :   if (!has_active_preconditions (fndecl))
     824              :     return NULL_TREE;
     825              : 
     826            8 :   return build_contract_condition_function (fndecl, /*pre=*/true);
     827              : }
     828              : 
     829              : /* Build the postcondition checking function for FNDECL.  If the return
     830              :    type is undeduced, don't build the function yet.  We do that in
     831              :    apply_deduced_return_type.  */
     832              : 
     833              : static tree
     834           33 : build_postcondition_function (tree fndecl)
     835              : {
     836           33 :   if (!has_active_postconditions (fndecl))
     837              :     return NULL_TREE;
     838              : 
     839           26 :   tree type = TREE_TYPE (TREE_TYPE (fndecl));
     840           26 :   if (is_auto (type))
     841              :     return NULL_TREE;
     842              : 
     843           26 :   return build_contract_condition_function (fndecl, /*pre=*/false);
     844              : }
     845              : 
     846              : /* If we're outlining the contract, build the functions to do the
     847              :    precondition and postcondition checks, and associate them with
     848              :    the function decl FNDECL.
     849              :  */
     850              : 
     851              : static void
     852           15 : build_contract_function_decls (tree fndecl)
     853              : {
     854              :   /* Build the pre/post functions (or not).  */
     855           15 :   if (!get_precondition_function (fndecl))
     856           15 :     if (tree pre = build_precondition_function (fndecl))
     857            8 :       set_precondition_function (fndecl, pre);
     858              : 
     859           15 :   if (!get_postcondition_function (fndecl))
     860           15 :     if (tree post = build_postcondition_function (fndecl))
     861            8 :       set_postcondition_function (fndecl, post);
     862           15 : }
     863              : 
     864              : /* Map from FUNCTION_DECL to a FUNCTION_DECL for contract wrapper.  */
     865              : 
     866              : static GTY(()) hash_map<tree, tree> *decl_wrapper_fn = nullptr;
     867              : 
     868              : /* Map from the function decl of a wrapper to the function that it wraps.  */
     869              : 
     870              : static GTY(()) hash_map<tree, tree> *decl_for_wrapper = nullptr;
     871              : 
     872              : /* Makes wrapper the precondition function for FNDECL.  */
     873              : 
     874              : static void
     875           22 : set_contract_wrapper_function (tree fndecl, tree wrapper)
     876              : {
     877           22 :   gcc_checking_assert (wrapper && fndecl);
     878           22 :   hash_map_maybe_create<hm_ggc> (decl_wrapper_fn);
     879           22 :   gcc_checking_assert (decl_wrapper_fn && !decl_wrapper_fn->get (fndecl));
     880           22 :   decl_wrapper_fn->put (fndecl, wrapper);
     881              : 
     882              :   /* We need to know the wrapped function when composing the diagnostic.  */
     883           22 :   hash_map_maybe_create<hm_ggc> (decl_for_wrapper);
     884           22 :   gcc_checking_assert (decl_for_wrapper && !decl_for_wrapper->get (wrapper));
     885           22 :   decl_for_wrapper->put (wrapper, fndecl);
     886           22 : }
     887              : 
     888              : /* Returns the wrapper function decl for FNDECL, or null if not set.  */
     889              : 
     890              : static tree
     891           22 : get_contract_wrapper_function (tree fndecl)
     892              : {
     893           22 :   gcc_checking_assert (fndecl);
     894           22 :   tree *result = hash_map_safe_get (decl_wrapper_fn, fndecl);
     895           11 :   return result ? *result : NULL_TREE;
     896              : }
     897              : 
     898              : /* Given a wrapper function WRAPPER, find the original function decl.  */
     899              : 
     900              : static tree
     901           34 : get_orig_func_for_wrapper (tree wrapper)
     902              : {
     903           34 :   gcc_checking_assert (wrapper);
     904           34 :   tree *result = hash_map_safe_get (decl_for_wrapper, wrapper);
     905           34 :   return result ? *result : NULL_TREE;
     906              : }
     907              : 
     908              : /* Build a declaration for the contract wrapper of a caller FNDECL.
     909              :    We're making a caller side contract check wrapper. For caller side contract
     910              :    checks, postconditions are only checked if check_post is true.
     911              :    Defer the attachment of the contracts to this function until the callee
     912              :    is non-dependent, or we get cases where the conditions can be non-dependent
     913              :    but still need tsubst-ing.  */
     914              : 
     915              : static tree
     916           22 : build_contract_wrapper_function (tree fndecl)
     917              : {
     918           22 :   if (error_operand_p (fndecl))
     919            0 :     return error_mark_node;
     920              : 
     921              :   /* We should not be trying to build wrappers for templates or functions that
     922              :      are still dependent.  */
     923           22 :   gcc_checking_assert (!processing_template_decl
     924              :                        && !TYPE_DEPENDENT_P (TREE_TYPE (fndecl)));
     925              : 
     926           22 :   location_t loc = DECL_SOURCE_LOCATION (fndecl);
     927              : 
     928              :   /* Fill in the names later.  */
     929           22 :   tree wrapdecl
     930           22 :     = build_lang_decl_loc (loc, FUNCTION_DECL, NULL_TREE, TREE_TYPE (fndecl));
     931              : 
     932              :   /* Put the wrapper in the same context as the callee.  */
     933           22 :   DECL_CONTEXT (wrapdecl) = DECL_CONTEXT (fndecl);
     934              : 
     935              :   /* This declaration is a contract wrapper function.  */
     936           22 :   DECL_CONTRACT_WRAPPER (wrapdecl) = true;
     937              : 
     938           22 :   contracts_fixup_names (wrapdecl, fndecl, /*pre*/false, /*wrapper*/true);
     939              : 
     940           22 :   DECL_SOURCE_LOCATION (wrapdecl) = loc;
     941              :   /* The declaration was implicitly generated by the compiler.  */
     942           22 :   DECL_ARTIFICIAL (wrapdecl) = true;
     943              :   /* Declaration, no definition yet.  */
     944           22 :   DECL_INITIAL (wrapdecl) = NULL_TREE;
     945              : 
     946              :   /* Let the start function code fill in the result decl.  */
     947           22 :   DECL_RESULT (wrapdecl) = NULL_TREE;
     948              : 
     949              :   /* Copy the function parameters, if present.  Suppress (e.g. unused)
     950              :      warnings on them.  */
     951           22 :   DECL_ARGUMENTS (wrapdecl) = NULL_TREE;
     952           22 :   if (tree p = DECL_ARGUMENTS (fndecl))
     953              :     {
     954           22 :       tree *last_a = &DECL_ARGUMENTS (wrapdecl);
     955           70 :       for (; p; p = TREE_CHAIN (p))
     956              :         {
     957           48 :           *last_a = copy_decl (p);
     958           48 :           suppress_warning (*last_a);
     959           48 :           DECL_CONTEXT (*last_a) = wrapdecl;
     960           48 :           last_a = &TREE_CHAIN (*last_a);
     961              :         }
     962              :     }
     963              : 
     964              :   /* Copy selected attributes from the original function.  */
     965           22 :   TREE_USED (wrapdecl) = TREE_USED (fndecl);
     966              : 
     967              :   /* Copy any alignment added.  */
     968           22 :   if (DECL_ALIGN (fndecl))
     969           22 :     SET_DECL_ALIGN (wrapdecl, DECL_ALIGN (fndecl));
     970           22 :   DECL_USER_ALIGN (wrapdecl) = DECL_USER_ALIGN (fndecl);
     971              : 
     972              :   /* Make this function internal.  */
     973           22 :   TREE_PUBLIC (wrapdecl) = false;
     974           22 :   DECL_EXTERNAL (wrapdecl) = false;
     975           22 :   DECL_WEAK (wrapdecl) = false;
     976              : 
     977              :   /* We know this is an internal function.  */
     978           22 :   DECL_INTERFACE_KNOWN (wrapdecl) = true;
     979           22 :   return wrapdecl;
     980              : }
     981              : 
     982              : static tree
     983           22 : get_or_create_contract_wrapper_function (tree fndecl)
     984              : {
     985           22 :   tree wrapdecl = get_contract_wrapper_function (fndecl);
     986           22 :   if (!wrapdecl)
     987              :     {
     988           22 :       wrapdecl = build_contract_wrapper_function (fndecl);
     989           22 :       set_contract_wrapper_function (fndecl, wrapdecl);
     990              :     }
     991           22 :   return wrapdecl;
     992              : }
     993              : 
     994              : void
     995    166164470 : start_function_contracts (tree fndecl)
     996              : {
     997    166164470 :   if (error_operand_p (fndecl))
     998              :     return;
     999              : 
    1000    166164470 :   if (!handle_contracts_p (fndecl))
    1001              :     return;
    1002              : 
    1003              :   /* If this is not a client side check and definition side checks are
    1004              :      disabled, do nothing.  */
    1005          354 :   if (!flag_contracts_definition_check
    1006          354 :       && !DECL_CONTRACT_WRAPPER (fndecl))
    1007              :     return;
    1008              : 
    1009              :   /* Check that the postcondition result name, if any, does not shadow a
    1010              :      function parameter.  */
    1011          906 :   for (tree ca = get_fn_contract_specifiers (fndecl); ca; ca = TREE_CHAIN (ca))
    1012          553 :     if (POSTCONDITION_P (CONTRACT_STATEMENT (ca)))
    1013          245 :       if (tree id = POSTCONDITION_IDENTIFIER (CONTRACT_STATEMENT (ca)))
    1014              :         {
    1015          113 :           if (id == error_mark_node)
    1016              :             {
    1017            2 :               CONTRACT_CONDITION (CONTRACT_STATEMENT (ca)) = error_mark_node;
    1018            2 :               continue;
    1019              :             }
    1020          111 :           tree r_name = tree_strip_any_location_wrapper (id);
    1021          111 :           if (TREE_CODE (id) == PARM_DECL)
    1022          111 :             r_name = DECL_NAME (id);
    1023          111 :           gcc_checking_assert (r_name && TREE_CODE (r_name) == IDENTIFIER_NODE);
    1024          111 :           tree seen = lookup_name (r_name);
    1025          111 :           if (seen
    1026            2 :               && TREE_CODE (seen) == PARM_DECL
    1027          113 :               && DECL_CONTEXT (seen) == fndecl)
    1028              :             {
    1029            2 :                 auto_diagnostic_group d;
    1030            2 :                 location_t id_l = location_wrapper_p (id)
    1031            2 :                                   ? EXPR_LOCATION (id)
    1032            2 :                                   : DECL_SOURCE_LOCATION (id);
    1033            2 :                 location_t co_l = EXPR_LOCATION (CONTRACT_STATEMENT (ca));
    1034            2 :                 if (id_l != UNKNOWN_LOCATION)
    1035            2 :                   co_l = make_location (id_l, co_l, co_l);
    1036            2 :                 error_at (co_l, "contract postcondition result name shadows a"
    1037              :                           " function parameter");
    1038            2 :                 inform (DECL_SOURCE_LOCATION (seen),
    1039              :                         "parameter declared here");
    1040            2 :                 POSTCONDITION_IDENTIFIER (CONTRACT_STATEMENT (ca))
    1041            2 :                   = error_mark_node;
    1042            2 :                 CONTRACT_CONDITION (CONTRACT_STATEMENT (ca)) = error_mark_node;
    1043            2 :             }
    1044              :         }
    1045              : 
    1046              :   /* If we are expanding contract assertions inline then no need to declare
    1047              :      the outline function decls.  */
    1048          353 :   if (!flag_contract_checks_outlined)
    1049              :     return;
    1050              : 
    1051              :   /* Contracts may have just been added without a chance to parse them, though
    1052              :      we still need the PRE_FN available to generate a call to it.  */
    1053              :   /* Do we already have declarations generated ? */
    1054           15 :   if (!DECL_PRE_FN (fndecl) && !DECL_POST_FN (fndecl))
    1055           15 :     build_contract_function_decls (fndecl);
    1056              : }
    1057              : 
    1058              : void
    1059      2234862 : maybe_update_postconditions (tree fndecl)
    1060              : {
    1061              :   /* Update any postconditions and the postcondition checking function
    1062              :      as needed.  If there are postconditions, we'll use those to rewrite
    1063              :      return statements to check postconditions.  */
    1064      2234862 :   if (has_active_postconditions (fndecl))
    1065              :     {
    1066           18 :       rebuild_postconditions (fndecl);
    1067           18 :       tree post = build_postcondition_function (fndecl);
    1068           18 :       set_postcondition_function (fndecl, post);
    1069              :     }
    1070      2234862 : }
    1071              : 
    1072              : /* Build and return an argument list containing all the parameters of the
    1073              :    (presumably guarded) function decl FNDECL.  This can be used to forward
    1074              :    all of FNDECL arguments to a function taking the same list of arguments
    1075              :    -- namely the unchecked form of FNDECL.
    1076              : 
    1077              :    We use CALL_FROM_THUNK_P instead of forward_parm for forwarding
    1078              :    semantics.  */
    1079              : 
    1080              : static vec<tree, va_gc> *
    1081           38 : build_arg_list (tree fndecl)
    1082              : {
    1083           38 :   vec<tree, va_gc> *args = make_tree_vector ();
    1084          109 :   for (tree t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
    1085           71 :     vec_safe_push (args, t);
    1086           38 :   return args;
    1087              : }
    1088              : 
    1089              : /* Build and return a thunk like call to FUNC from CALLER using the supplied
    1090              :    arguments.  The call is like a thunk call in the fact that we do not
    1091              :    want to create additional copies of the arguments.  We can not simply reuse
    1092              :    the thunk machinery as it does more than we want.  More specifically, we
    1093              :    don't want to mark the calling function as `DECL_THUNK_P` for this
    1094              :    particular purpose, we only want the special treatment for the parameters
    1095              :    of the call we are about to generate.  We temporarily mark the calling
    1096              :    function as DECL_THUNK_P so build_call_a does the right thing.  */
    1097              : 
    1098              : static tree
    1099           38 : build_thunk_like_call (tree func, int n, tree *argarray)
    1100              : {
    1101           38 :   bool old_decl_thunk_p = DECL_THUNK_P (current_function_decl);
    1102           38 :   LANG_DECL_FN_CHECK (current_function_decl)->thunk_p  = true;
    1103              : 
    1104           38 :   tree call = build_call_a (func, n, argarray);
    1105              : 
    1106              :   /* Revert the `DECL_THUNK_P` flag.  */
    1107           38 :   LANG_DECL_FN_CHECK (current_function_decl)->thunk_p = old_decl_thunk_p;
    1108              : 
    1109              :   /* Mark the call as a thunk call to allow for correct gimplification
    1110              :    of the arguments.  */
    1111           38 :   CALL_FROM_THUNK_P (call) = true;
    1112              : 
    1113           38 :   return call;
    1114              : }
    1115              : 
    1116              : /* If we have a precondition function and it's valid, call it.  */
    1117              : 
    1118              : static void
    1119            8 : add_pre_condition_fn_call (tree fndecl)
    1120              : {
    1121              :   /* If we're starting a guarded function with valid contracts, we need to
    1122              :      insert a call to the pre function.  */
    1123            8 :   gcc_checking_assert (DECL_PRE_FN (fndecl)
    1124              :                        && DECL_PRE_FN (fndecl) != error_mark_node);
    1125              : 
    1126            8 :   releasing_vec args = build_arg_list (fndecl);
    1127            8 :   tree call = build_thunk_like_call (DECL_PRE_FN (fndecl),
    1128            8 :                                      args->length (), args->address ());
    1129              : 
    1130            8 :   finish_expr_stmt (call);
    1131            8 : }
    1132              : 
    1133              : /* Returns the parameter corresponding to the return value of a guarded
    1134              :    function FNDECL.  Returns NULL_TREE if FNDECL has no postconditions or
    1135              :    is void.  */
    1136              : 
    1137              : static tree
    1138            8 : get_postcondition_result_parameter (tree fndecl)
    1139              : {
    1140            8 :   if (!fndecl || fndecl == error_mark_node)
    1141              :     return NULL_TREE;
    1142              : 
    1143            8 :   if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl))))
    1144              :     return NULL_TREE;
    1145              : 
    1146            2 :   tree post = DECL_POST_FN (fndecl);
    1147            2 :   if (!post || post == error_mark_node)
    1148              :     return NULL_TREE;
    1149              : 
    1150              :   /* The last param is the return value.  */
    1151            2 :   return tree_last (DECL_ARGUMENTS (post));
    1152              : }
    1153              : 
    1154              : /* Build and add a call to the post-condition checking function, when that
    1155              :    is in use.  */
    1156              : 
    1157              : static void
    1158            8 : add_post_condition_fn_call (tree fndecl)
    1159              : {
    1160            8 :   gcc_checking_assert (DECL_POST_FN (fndecl)
    1161              :                        && DECL_POST_FN (fndecl) != error_mark_node);
    1162              : 
    1163            8 :   releasing_vec args = build_arg_list (fndecl);
    1164            8 :   if (get_postcondition_result_parameter (fndecl))
    1165            2 :     vec_safe_push (args, DECL_RESULT (fndecl));
    1166            8 :   tree call = build_thunk_like_call (DECL_POST_FN (fndecl),
    1167            8 :                                      args->length (), args->address ());
    1168            8 :   finish_expr_stmt (call);
    1169            8 : }
    1170              : 
    1171              : /* Copy (possibly a sub-set of) contracts from CONTRACTS on FNDECL.  */
    1172              : 
    1173              : static tree
    1174          390 : copy_contracts_list (tree contracts, tree fndecl,
    1175              :                      contract_match_kind remap_kind = cmk_all)
    1176              : {
    1177          390 :   tree last = NULL_TREE, new_contracts = NULL_TREE;
    1178         1065 :   for (; contracts; contracts = TREE_CHAIN (contracts))
    1179              :     {
    1180          815 :       if ((remap_kind == cmk_pre
    1181          365 :            && (TREE_CODE (CONTRACT_STATEMENT (contracts))
    1182              :                == POSTCONDITION_STMT))
    1183          974 :           || (remap_kind == cmk_post
    1184          310 :               && (TREE_CODE (CONTRACT_STATEMENT (contracts))
    1185              :                   == PRECONDITION_STMT)))
    1186          140 :         continue;
    1187              : 
    1188          535 :       tree c = copy_node (contracts);
    1189         1070 :       TREE_VALUE (c) = build_tree_list (TREE_PURPOSE (TREE_VALUE (c)),
    1190          535 :                                         copy_node (CONTRACT_STATEMENT (c)));
    1191              : 
    1192          535 :       copy_body_data id;
    1193          535 :       hash_map<tree, tree> decl_map;
    1194              : 
    1195          535 :       memset (&id, 0, sizeof (id));
    1196              : 
    1197          535 :       id.src_fn = fndecl;
    1198          535 :       id.dst_fn = fndecl;
    1199          535 :       id.src_cfun = DECL_STRUCT_FUNCTION (fndecl);
    1200          535 :       id.decl_map = &decl_map;
    1201              : 
    1202          535 :       id.copy_decl = retain_decl;
    1203              : 
    1204          535 :       id.transform_call_graph_edges = CB_CGE_DUPLICATE;
    1205          535 :       id.transform_new_cfg = false;
    1206          535 :       id.transform_return_to_modify = false;
    1207          535 :       id.transform_parameter = true;
    1208              : 
    1209              :       /* Make sure not to unshare trees behind the front-end's back
    1210              :          since front-end specific mechanisms may rely on sharing.  */
    1211          535 :       id.regimplify = false;
    1212          535 :       id.do_not_unshare = true;
    1213          535 :       id.do_not_fold = true;
    1214              : 
    1215              :       /* We're not inside any EH region.  */
    1216          535 :       id.eh_lp_nr = 0;
    1217          535 :       walk_tree (&CONTRACT_CONDITION (CONTRACT_STATEMENT (c)),
    1218              :                                       copy_tree_body_r, &id, NULL);
    1219              : 
    1220              : 
    1221         1070 :       CONTRACT_COMMENT (CONTRACT_STATEMENT (c))
    1222          535 :         = copy_node (CONTRACT_COMMENT (CONTRACT_STATEMENT (c)));
    1223              : 
    1224          535 :       chainon (last, c);
    1225          535 :       last = c;
    1226          535 :       if (!new_contracts)
    1227          390 :         new_contracts = c;
    1228          535 :     }
    1229          390 :   return new_contracts;
    1230              : }
    1231              : 
    1232              : /* Returns a copy of FNDECL contracts. This is used when emiting a contract.
    1233              :  If we were to emit the original contract tree, any folding of the contract
    1234              :  condition would affect the original contract too. The original contract
    1235              :  tree needs to be preserved in case it is used to apply to a different
    1236              :  function (for inheritance or wrapping reasons). */
    1237              : 
    1238              : static tree
    1239          390 : copy_contracts (tree fndecl, contract_match_kind remap_kind = cmk_all)
    1240              : {
    1241          390 :   tree contracts = get_fn_contract_specifiers (fndecl);
    1242          390 :   return copy_contracts_list (contracts, fndecl, remap_kind);
    1243              : }
    1244              : 
    1245              : /* Add the contract statement CONTRACT to the current block if valid.  */
    1246              : 
    1247              : static bool
    1248          553 : emit_contract_statement (tree contract)
    1249              : {
    1250              :   /* Only add valid contracts.  */
    1251          553 :   if (contract == error_mark_node
    1252          553 :       || CONTRACT_CONDITION (contract) == error_mark_node)
    1253              :     return false;
    1254              : 
    1255          545 :   if (get_evaluation_semantic (contract) == CES_INVALID)
    1256              :     return false;
    1257              : 
    1258          545 :   add_stmt (contract);
    1259          545 :   return true;
    1260              : }
    1261              : 
    1262              : /* Generate the statement for the given contract by adding the contract
    1263              :    statement to the current block. Returns the next contract in the chain.  */
    1264              : 
    1265              : static tree
    1266          535 : emit_contract (tree contract)
    1267              : {
    1268          535 :   gcc_assert (TREE_CODE (contract) == TREE_LIST);
    1269              : 
    1270          535 :   emit_contract_statement (CONTRACT_STATEMENT (contract));
    1271              : 
    1272          535 :   return TREE_CHAIN (contract);
    1273              : }
    1274              : 
    1275              : /* Add a call or a direct evaluation of the pre checks.  */
    1276              : 
    1277              : static void
    1278          240 : apply_preconditions (tree fndecl)
    1279              : {
    1280          240 :   if (flag_contract_checks_outlined)
    1281            8 :     add_pre_condition_fn_call (fndecl);
    1282              :   else
    1283              :   {
    1284          232 :     tree contract_copy = copy_contracts (fndecl, cmk_pre);
    1285          763 :     for (; contract_copy; contract_copy = TREE_CHAIN (contract_copy))
    1286          299 :       emit_contract (contract_copy);
    1287              :   }
    1288          240 : }
    1289              : 
    1290              : /* Add a call or a direct evaluation of the post checks.  */
    1291              : 
    1292              : static void
    1293          166 : apply_postconditions (tree fndecl)
    1294              : {
    1295          166 :   if (flag_contract_checks_outlined)
    1296            8 :     add_post_condition_fn_call (fndecl);
    1297              :   else
    1298              :     {
    1299          158 :       tree contract_copy = copy_contracts (fndecl, cmk_post);
    1300          552 :       for (; contract_copy; contract_copy = TREE_CHAIN (contract_copy))
    1301          236 :         emit_contract (contract_copy);
    1302              :     }
    1303          166 : }
    1304              : 
    1305              : /* Add contract handling to the function in FNDECL.
    1306              : 
    1307              :    When we have only pre-conditions, this simply prepends a call (or a direct
    1308              :    evaluation, for cdtors) to the existing function body.
    1309              : 
    1310              :    When we have post conditions we build a try-finally block.
    1311              :    If the function might throw then the handler in the try-finally is an
    1312              :    EH_ELSE expression, where the post condition check is applied to the
    1313              :    non-exceptional path, and an empty statement is added to the EH path.  If
    1314              :    the function has a non-throwing eh spec, then the handler is simply the
    1315              :    post-condition checker.  */
    1316              : 
    1317              : void
    1318    148595904 : maybe_apply_function_contracts (tree fndecl)
    1319              : {
    1320    148595904 :   if (!handle_contracts_p (fndecl))
    1321              :     /* We did nothing and the original function body statement list will be
    1322              :        popped by our caller.  */
    1323              :     return;
    1324              : 
    1325              :   /* If this is not a client side check and definition side checks are
    1326              :      disabled, do nothing.  */
    1327          354 :   if (!flag_contracts_definition_check
    1328          354 :       && !DECL_CONTRACT_WRAPPER (fndecl))
    1329              :     return;
    1330              : 
    1331          353 :   bool do_pre = has_active_preconditions (fndecl);
    1332          353 :   bool do_post = has_active_postconditions (fndecl);
    1333              :   /* We should not have reached here with nothing to do... */
    1334          353 :   gcc_checking_assert (do_pre || do_post);
    1335              : 
    1336              :   /* If the function is noexcept, the user's written body will be wrapped in a
    1337              :      MUST_NOT_THROW expression.  In that case we leave the MUST_NOT_THROW in
    1338              :      place and do our replacement inside it.  */
    1339          353 :   tree fnbody;
    1340          353 :   if (TYPE_NOEXCEPT_P (TREE_TYPE (fndecl)))
    1341              :     {
    1342           23 :       tree m_n_t_expr = expr_first (DECL_SAVED_TREE (fndecl));
    1343           23 :       gcc_checking_assert (TREE_CODE (m_n_t_expr) == MUST_NOT_THROW_EXPR);
    1344           23 :       fnbody = TREE_OPERAND (m_n_t_expr, 0);
    1345           23 :       TREE_OPERAND (m_n_t_expr, 0) = push_stmt_list ();
    1346              :     }
    1347              :   else
    1348              :     {
    1349          330 :       fnbody = DECL_SAVED_TREE (fndecl);
    1350          330 :       DECL_SAVED_TREE (fndecl) = push_stmt_list ();
    1351              :     }
    1352              : 
    1353              :   /* Now add the pre and post conditions to the existing function body.
    1354              :      This copies the approach used for function try blocks.  */
    1355          353 :   tree compound_stmt = begin_compound_stmt (0);
    1356          353 :   current_binding_level->artificial = true;
    1357              : 
    1358              :   /* Do not add locations for the synthesised code.  */
    1359          353 :   location_t loc = UNKNOWN_LOCATION;
    1360              : 
    1361              :   /* For other cases, we call a function to process the check.  */
    1362              : 
    1363              :   /* If we have a pre, but not a post, then just emit that and we are done.  */
    1364          353 :   if (!do_post)
    1365              :     {
    1366          187 :       apply_preconditions (fndecl);
    1367          187 :       add_stmt (fnbody);
    1368          187 :       finish_compound_stmt (compound_stmt);
    1369          187 :       return;
    1370              :     }
    1371              : 
    1372          166 :   if (do_pre)
    1373              :     /* Add a precondition call, if we have one. */
    1374           53 :     apply_preconditions (fndecl);
    1375          166 :   tree try_fin = build_stmt (loc, TRY_FINALLY_EXPR, fnbody, NULL_TREE);
    1376          166 :   add_stmt (try_fin);
    1377          166 :   TREE_OPERAND (try_fin, 1) = push_stmt_list ();
    1378              :   /* If we have exceptions, and a function that might throw, then add
    1379              :      an EH_ELSE clause that allows the exception to propagate upwards
    1380              :      without encountering the post-condition checks.  */
    1381          166 :   if (flag_exceptions && !type_noexcept_p (TREE_TYPE (fndecl)))
    1382              :     {
    1383          159 :       tree eh_else = build_stmt (loc, EH_ELSE_EXPR, NULL_TREE, NULL_TREE);
    1384          159 :       add_stmt (eh_else);
    1385          159 :       TREE_OPERAND (eh_else, 0) = push_stmt_list ();
    1386          159 :       apply_postconditions (fndecl);
    1387          159 :       TREE_OPERAND (eh_else, 0) = pop_stmt_list (TREE_OPERAND (eh_else, 0));
    1388          159 :       TREE_OPERAND (eh_else, 1) = void_node;
    1389              :     }
    1390              :   else
    1391            7 :     apply_postconditions (fndecl);
    1392          166 :   TREE_OPERAND (try_fin, 1) = pop_stmt_list (TREE_OPERAND (try_fin, 1));
    1393          166 :   finish_compound_stmt (compound_stmt);
    1394              :   /* The DECL_SAVED_TREE stmt list will be popped by our caller.  */
    1395              : }
    1396              : 
    1397              : /* Rewrite the condition of contract in place, so that references to SRC's
    1398              :    parameters are updated to refer to DST's parameters. The postcondition
    1399              :    result variable is left unchanged.
    1400              : 
    1401              :    When declarations are merged, we sometimes need to update contracts to
    1402              :    refer to new parameters.
    1403              : 
    1404              :    If DUPLICATE_P is true, this is called by duplicate_decls to rewrite
    1405              :    contracts in terms of a new set of parameters.  This also preserves the
    1406              :    references to postcondition results, which are not replaced during
    1407              :    merging.  */
    1408              : 
    1409              : static void
    1410          238 : remap_contract (tree src, tree dst, tree contract, bool duplicate_p)
    1411              : {
    1412          238 :   copy_body_data id;
    1413          238 :   hash_map<tree, tree> decl_map;
    1414              : 
    1415          238 :   memset (&id, 0, sizeof (id));
    1416          238 :   id.src_fn = src;
    1417          238 :   id.dst_fn = dst;
    1418          238 :   id.src_cfun = DECL_STRUCT_FUNCTION (src);
    1419          238 :   id.decl_map = &decl_map;
    1420              : 
    1421              :   /* If we're merging contracts, don't copy local variables.  */
    1422          238 :   id.copy_decl = duplicate_p ? retain_decl : copy_decl_no_change;
    1423              : 
    1424          238 :   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
    1425          238 :   id.transform_new_cfg = false;
    1426          238 :   id.transform_return_to_modify = false;
    1427          238 :   id.transform_parameter = true;
    1428              : 
    1429              :   /* Make sure not to unshare trees behind the front-end's back
    1430              :      since front-end specific mechanisms may rely on sharing.  */
    1431          238 :   id.regimplify = false;
    1432          238 :   id.do_not_unshare = true;
    1433          238 :   id.do_not_fold = true;
    1434              : 
    1435              :   /* We're not inside any EH region.  */
    1436          238 :   id.eh_lp_nr = 0;
    1437              : 
    1438          238 :   bool do_remap = false;
    1439              : 
    1440              :   /* Insert parameter remappings.  */
    1441          238 :   gcc_checking_assert (TREE_CODE (src) == FUNCTION_DECL);
    1442          238 :   gcc_checking_assert (TREE_CODE (dst) == FUNCTION_DECL);
    1443              : 
    1444          238 :   int src_num_artificial_args = num_artificial_parms_for (src);
    1445          238 :   int dst_num_artificial_args = num_artificial_parms_for (dst);
    1446              : 
    1447          238 :   for (tree sp = DECL_ARGUMENTS (src), dp = DECL_ARGUMENTS (dst);
    1448          704 :        sp || dp;
    1449          466 :        sp = DECL_CHAIN (sp), dp = DECL_CHAIN (dp))
    1450              :     {
    1451          469 :       if (!sp && dp
    1452            3 :           && TREE_CODE (contract) == POSTCONDITION_STMT
    1453          472 :           && DECL_CHAIN (dp) == NULL_TREE)
    1454              :         {
    1455            3 :           gcc_assert (!duplicate_p);
    1456            3 :           if (tree result = POSTCONDITION_IDENTIFIER (contract))
    1457              :             {
    1458            3 :               gcc_assert (DECL_P (result));
    1459            3 :               insert_decl_map (&id, result, dp);
    1460            3 :               do_remap = true;
    1461              :             }
    1462              :           break;
    1463              :         }
    1464          466 :       gcc_assert (sp && dp);
    1465              : 
    1466          466 :       if (sp == dp)
    1467          201 :         continue;
    1468              : 
    1469          265 :       insert_decl_map (&id, sp, dp);
    1470          265 :       do_remap = true;
    1471              : 
    1472              :       /* First artificial arg is *this. We want to remap that.  However, we
    1473              :          want to skip _in_charge param and __vtt_parm.  Do so now.  */
    1474          265 :       if (src_num_artificial_args > 0)
    1475              :         {
    1476           80 :           while (--src_num_artificial_args,src_num_artificial_args > 0)
    1477            0 :             sp = DECL_CHAIN (sp);
    1478              :         }
    1479          265 :       if (dst_num_artificial_args > 0)
    1480              :         {
    1481           80 :           while (--dst_num_artificial_args,dst_num_artificial_args > 0)
    1482            0 :             dp = DECL_CHAIN (dp);
    1483              :         }
    1484              :     }
    1485              : 
    1486          238 :   if (!do_remap)
    1487          106 :     return;
    1488              : 
    1489          132 :   walk_tree (&CONTRACT_CONDITION (contract), copy_tree_body_r, &id, NULL);
    1490          238 : }
    1491              : 
    1492              : /* Returns a copy of SOURCE contracts where any references to SOURCE's
    1493              :    PARM_DECLs have been rewritten to the corresponding PARM_DECL in DEST.  */
    1494              : 
    1495              : tree
    1496          162 : copy_and_remap_contracts (tree dest, tree source,
    1497              :                           contract_match_kind remap_kind)
    1498              : {
    1499          162 :   tree last = NULL_TREE, contracts_copy= NULL_TREE;
    1500          162 :   tree contracts = get_fn_contract_specifiers (source);
    1501          384 :   for (; contracts; contracts = TREE_CHAIN (contracts))
    1502              :     {
    1503          224 :       if ((remap_kind == cmk_pre
    1504            5 :            && (TREE_CODE (CONTRACT_STATEMENT (contracts))
    1505              :                == POSTCONDITION_STMT))
    1506          225 :           || (remap_kind == cmk_post
    1507            0 :               && (TREE_CODE (CONTRACT_STATEMENT (contracts))
    1508              :                   == PRECONDITION_STMT)))
    1509            2 :         continue;
    1510              : 
    1511              :       /* The first part is copying of the legacy attribute layout - eventually
    1512              :          this will go away.  */
    1513          220 :       tree c = copy_node (contracts);
    1514          440 :       TREE_VALUE (c) = build_tree_list (TREE_PURPOSE (TREE_VALUE (c)),
    1515          220 :                                         copy_node (CONTRACT_STATEMENT (c)));
    1516              :       /* This is the copied contract statement.  */
    1517          220 :       tree stmt = CONTRACT_STATEMENT (c);
    1518              : 
    1519              :       /* If we have an erroneous postcondition identifier, we also mark the
    1520              :          condition as invalid so only need to check that.  */
    1521          220 :       if (CONTRACT_CONDITION (stmt) != error_mark_node)
    1522          220 :         remap_contract (source, dest, stmt, /*duplicate_p=*/true);
    1523              : 
    1524          220 :       if (TREE_CODE (stmt) == POSTCONDITION_STMT)
    1525              :         {
    1526              :           /* If we have a postcondition return value placeholder, then
    1527              :              ensure the copied one has the correct context.  */
    1528           77 :           tree var = POSTCONDITION_IDENTIFIER (stmt);
    1529           77 :           if (var && var != error_mark_node)
    1530           20 :             DECL_CONTEXT (var) = dest;
    1531              :         }
    1532              : 
    1533          220 :       if (CONTRACT_COMMENT (stmt) != error_mark_node)
    1534          220 :         CONTRACT_COMMENT (stmt) = copy_node (CONTRACT_COMMENT (stmt));
    1535              : 
    1536          220 :       chainon (last, c);
    1537          220 :       last = c;
    1538          220 :       if (!contracts_copy)
    1539          162 :         contracts_copy = c;
    1540              :     }
    1541              : 
    1542          162 :   return contracts_copy;
    1543              : }
    1544              : 
    1545              : /* Set the (maybe) parsed contract specifier LIST for DECL.  */
    1546              : 
    1547              : void
    1548         1072 : set_fn_contract_specifiers (tree decl, tree list)
    1549              : {
    1550         1072 :   if (!decl || error_operand_p (decl))
    1551            0 :     return;
    1552              : 
    1553         1072 :   bool existed = false;
    1554         1072 :   contract_decl& rd
    1555         1072 :     = hash_map_safe_get_or_insert<hm_ggc> (contract_decl_map, decl, &existed);
    1556         1072 :   if (!existed)
    1557              :     {
    1558              :       /* This is the first time we encountered this decl, save the location
    1559              :          for error messages.  This will ensure all error messages refer to the
    1560              :          contracts used for the function.  */
    1561          833 :       location_t decl_loc = DECL_SOURCE_LOCATION (decl);
    1562          833 :       location_t cont_end = decl_loc;
    1563          833 :       if (list)
    1564          833 :         cont_end = get_contract_end_loc (list);
    1565          833 :       rd.note_loc = make_location (decl_loc, decl_loc, cont_end);
    1566              :     }
    1567         1072 :   rd.contract_specifiers = list;
    1568              : }
    1569              : 
    1570              : /* Update the entry for DECL in the map of contract specifiers with the
    1571              :   contracts in LIST. */
    1572              : 
    1573              : void
    1574          272 : update_fn_contract_specifiers (tree decl, tree list)
    1575              : {
    1576          272 :   if (!decl || error_operand_p (decl))
    1577            0 :     return;
    1578              : 
    1579          272 :   bool existed = false;
    1580          272 :   contract_decl& rd
    1581          272 :     = hash_map_safe_get_or_insert<hm_ggc> (contract_decl_map, decl, &existed);
    1582          272 :   gcc_checking_assert (existed);
    1583              : 
    1584              :   /* We should only get here when we parse deferred contracts.  */
    1585          272 :   gcc_checking_assert (!contract_any_deferred_p (list));
    1586              : 
    1587          272 :   rd.contract_specifiers = list;
    1588              : }
    1589              : 
    1590              : /* When a decl is about to be removed, then we need to release its content and
    1591              :    then take it out of the map.  */
    1592              : 
    1593              : void
    1594         5215 : remove_decl_with_fn_contracts_specifiers (tree decl)
    1595              : {
    1596         5350 :   if (contract_decl *p = hash_map_safe_get (contract_decl_map, decl))
    1597              :     {
    1598          109 :       p->contract_specifiers = NULL_TREE;
    1599          109 :       contract_decl_map->remove (decl);
    1600              :     }
    1601         5215 : }
    1602              : 
    1603              : /* If this function has contract specifiers, then remove them, but leave the
    1604              :    function registered.  */
    1605              : 
    1606              : void
    1607         2345 : remove_fn_contract_specifiers (tree decl)
    1608              : {
    1609         2375 :   if (contract_decl *p = hash_map_safe_get (contract_decl_map, decl))
    1610              :     {
    1611           30 :       p->contract_specifiers = NULL_TREE;
    1612              :     }
    1613         2345 : }
    1614              : 
    1615              : /* Get the contract specifier list for this DECL if there is one.  */
    1616              : 
    1617              : tree
    1618    466410074 : get_fn_contract_specifiers (tree decl)
    1619              : {
    1620    466658967 :   if (contract_decl *p = hash_map_safe_get (contract_decl_map, decl))
    1621         5946 :     return p->contract_specifiers;
    1622              :   return NULL_TREE;
    1623              : }
    1624              : 
    1625              : /* A subroutine of duplicate_decls. Diagnose issues in the redeclaration of
    1626              :    guarded functions.  */
    1627              : 
    1628              : void
    1629     18635012 : check_redecl_contract (tree newdecl, tree olddecl)
    1630              : {
    1631     18635012 :   if (!flag_contracts)
    1632              :     return;
    1633              : 
    1634         7178 :   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
    1635         2407 :     newdecl = DECL_TEMPLATE_RESULT (newdecl);
    1636         7178 :   if (TREE_CODE (olddecl) == TEMPLATE_DECL)
    1637         2407 :     olddecl = DECL_TEMPLATE_RESULT (olddecl);
    1638              : 
    1639         7178 :   tree new_contracts = get_fn_contract_specifiers (newdecl);
    1640         7178 :   tree old_contracts = get_fn_contract_specifiers (olddecl);
    1641              : 
    1642         7178 :   if (!old_contracts && !new_contracts)
    1643              :     return;
    1644              : 
    1645              :   /* We should always be comparing with the 'first' declaration which should
    1646              :    have been recorded already (if it has contract specifiers).  However
    1647              :    if the new decl is trying to add contracts, that is an error and we do
    1648              :    not want to create a map entry yet.  */
    1649          131 :   contract_decl *rdp = hash_map_safe_get (contract_decl_map, olddecl);
    1650          131 :   gcc_checking_assert(rdp || !old_contracts);
    1651              : 
    1652          131 :   location_t new_loc = DECL_SOURCE_LOCATION (newdecl);
    1653          131 :   if (new_contracts && !old_contracts)
    1654              :     {
    1655           10 :       auto_diagnostic_group d;
    1656              :       /* If a re-declaration has contracts, they must be the same as those
    1657              :        that appear on the first declaration seen (they cannot be added).  */
    1658           10 :       location_t cont_end = get_contract_end_loc (new_contracts);
    1659           10 :       cont_end = make_location (new_loc, new_loc, cont_end);
    1660           10 :       error_at (cont_end, "declaration adds contracts to %q#D", olddecl);
    1661           10 :       inform (DECL_SOURCE_LOCATION (olddecl), "first declared here");
    1662           10 :       return;
    1663           10 :     }
    1664              : 
    1665          121 :   if (old_contracts && !new_contracts)
    1666              :     /* We allow re-declarations to omit contracts declared on the initial decl.
    1667              :        In fact, this is required if the conditions contain lambdas.  Check if
    1668              :        all the parameters are correctly const qualified. */
    1669           79 :     check_postconditions_in_redecl (olddecl, newdecl);
    1670           42 :   else if (old_contracts && new_contracts
    1671           42 :            && !contract_any_deferred_p (old_contracts)
    1672           38 :            && contract_any_deferred_p (new_contracts)
    1673           42 :            && DECL_UNIQUE_FRIEND_P (newdecl))
    1674              :     {
    1675              :       /* Put the defered contracts on the olddecl so we parse it when
    1676              :          we can.  */
    1677            0 :       set_fn_contract_specifiers (olddecl, old_contracts);
    1678              :     }
    1679           42 :   else if (contract_any_deferred_p (old_contracts)
    1680           42 :            || contract_any_deferred_p (new_contracts))
    1681              :     {
    1682              :       /* TODO: ignore these and figure out how to process them later.  */
    1683              :       /* Note that a friend declaration has deferred contracts, but the
    1684              :          declaration of the same function outside the class definition
    1685              :          doesn't.  */
    1686              :     }
    1687              :   else
    1688              :     {
    1689           38 :       gcc_checking_assert (old_contracts);
    1690           38 :       location_t cont_end = get_contract_end_loc (new_contracts);
    1691           38 :       cont_end = make_location (new_loc, new_loc, cont_end);
    1692              :       /* We have two sets - they should match or we issue a diagnostic.  */
    1693           38 :       match_contract_specifiers (rdp->note_loc, old_contracts,
    1694              :                                  cont_end, new_contracts);
    1695              :     }
    1696              : 
    1697              :   return;
    1698              : }
    1699              : 
    1700              : /* Update the contracts of DEST to match the argument names from contracts
    1701              :   of SRC. When we merge two declarations in duplicate_decls, we preserve the
    1702              :   arguments from the new declaration, if the new declaration is a
    1703              :   definition. We need to update the contracts accordingly.  */
    1704              : 
    1705              : void
    1706     11593278 : update_contract_arguments (tree srcdecl, tree destdecl)
    1707              : {
    1708     11593278 :   tree src_contracts = get_fn_contract_specifiers (srcdecl);
    1709     11593278 :   tree dest_contracts = get_fn_contract_specifiers (destdecl);
    1710              : 
    1711     11593278 :   if (!src_contracts && !dest_contracts)
    1712              :     return;
    1713              : 
    1714              :   /* Check if src even has contracts. It is possible that a redeclaration
    1715              :     does not have contracts. Is this is the case, first apply contracts
    1716              :     to src.  */
    1717           85 :   if (!src_contracts)
    1718              :     {
    1719           59 :       if (contract_any_deferred_p (dest_contracts))
    1720              :         {
    1721            0 :           set_fn_contract_specifiers (srcdecl, dest_contracts);
    1722              :           /* Nothing more to do here.  */
    1723            0 :           return;
    1724              :         }
    1725              :       else
    1726           59 :         set_fn_contract_specifiers
    1727           59 :           (srcdecl, copy_and_remap_contracts (srcdecl, destdecl));
    1728              :     }
    1729              : 
    1730              :   /* For deferred contracts, we currently copy the tokens from the redeclaration
    1731              :     onto the decl that will be preserved. This is not ideal because the
    1732              :     redeclaration may have erroneous contracts.
    1733              :     For non deferred contracts we currently do copy and remap, which is doing
    1734              :     more than we need.  */
    1735           85 :   if (contract_any_deferred_p (src_contracts))
    1736            4 :     set_fn_contract_specifiers (destdecl, src_contracts);
    1737              :   else
    1738              :     {
    1739              :       /* Temporarily rename the arguments to get the right mapping.  */
    1740           81 :       tree tmp_arguments = DECL_ARGUMENTS (destdecl);
    1741           81 :       DECL_ARGUMENTS (destdecl) = DECL_ARGUMENTS (srcdecl);
    1742           81 :       set_fn_contract_specifiers (destdecl,
    1743              :                                   copy_and_remap_contracts (destdecl, srcdecl));
    1744           81 :       DECL_ARGUMENTS (destdecl) = tmp_arguments;
    1745              :     }
    1746              : }
    1747              : 
    1748              : /* Checks if a contract check wrapper is needed for fndecl.  */
    1749              : 
    1750              : static bool
    1751          217 : should_contract_wrap_call (bool do_pre, bool do_post)
    1752              : {
    1753              :   /* Only if the target function actually has any contracts.  */
    1754            0 :   if (!do_pre && !do_post)
    1755              :     return false;
    1756              : 
    1757              : 
    1758          217 :   return ((flag_contract_client_check > 1)
    1759          217 :           || ((flag_contract_client_check > 0)
    1760              :               && do_pre));
    1761              : }
    1762              : 
    1763              : /* Possibly replace call with a call to a wrapper function which
    1764              :    will do the contracts check required around a CALL to FNDECL.  */
    1765              : 
    1766              : tree
    1767    225770260 : maybe_contract_wrap_call (tree fndecl, tree call)
    1768              : {
    1769              :   /* We can be called from build_cxx_call without a known callee.  */
    1770    225770260 :   if (!fndecl)
    1771              :     return call;
    1772              : 
    1773    218364170 :   if (error_operand_p (fndecl) || !call || call == error_mark_node)
    1774            0 :     return error_mark_node;
    1775              : 
    1776    218364170 :   if (!handle_contracts_p (fndecl))
    1777              :     return call;
    1778              : 
    1779          217 :   bool do_pre = has_active_preconditions (fndecl);
    1780          217 :   bool do_post = has_active_postconditions (fndecl);
    1781              : 
    1782              :   /* Check if we need a wrapper.  */
    1783    225770458 :   if (!should_contract_wrap_call (do_pre, do_post))
    1784              :     return call;
    1785              : 
    1786              :   /* Build the declaration of the wrapper, if we need to.  */
    1787           22 :   tree wrapdecl = get_or_create_contract_wrapper_function (fndecl);
    1788              : 
    1789           22 :   unsigned nargs = call_expr_nargs (call);
    1790           22 :   vec<tree, va_gc> *argwrap;
    1791           22 :   vec_alloc (argwrap, nargs);
    1792              : 
    1793           22 :   tree arg;
    1794           22 :   call_expr_arg_iterator iter;
    1795           92 :   FOR_EACH_CALL_EXPR_ARG (arg, iter, call)
    1796           48 :     argwrap->quick_push (arg);
    1797              : 
    1798           22 :   tree wrapcall = build_call_expr_loc_vec (DECL_SOURCE_LOCATION (wrapdecl),
    1799              :                                            wrapdecl, argwrap);
    1800              : 
    1801           22 :   return wrapcall;
    1802              : }
    1803              : 
    1804              : /* Map traversal callback to define a wrapper function.
    1805              :    This generates code for client-side contract check wrappers and the
    1806              :    noexcept wrapper around the contract violation handler.  */
    1807              : 
    1808              : bool
    1809           48 : define_contract_wrapper_func (const tree& fndecl, const tree& wrapdecl, void*)
    1810              : {
    1811              :   /* If we already built this function on a previous pass, then do nothing.  */
    1812           48 :   if (DECL_INITIAL (wrapdecl) && DECL_INITIAL (wrapdecl) != error_mark_node)
    1813              :     return true;
    1814              : 
    1815           22 :   gcc_checking_assert (!DECL_HAS_CONTRACTS_P (wrapdecl));
    1816              :   /* We check postconditions if postcondition checks are enabled for clients.
    1817              :     We should not get here unless there are some checks to make.  */
    1818           22 :   bool check_post = flag_contract_client_check > 1;
    1819              :   /* For wrappers on CDTORs we need to refer to the original contracts,
    1820              :      when the wrapper is around a clone.  */
    1821           44 :   set_fn_contract_specifiers ( wrapdecl,
    1822           22 :                       copy_and_remap_contracts (wrapdecl, DECL_ORIGIN (fndecl),
    1823              :                                                 check_post? cmk_all : cmk_pre));
    1824              : 
    1825           22 :   start_preparsed_function (wrapdecl, /*DECL_ATTRIBUTES*/NULL_TREE,
    1826              :                             SF_DEFAULT | SF_PRE_PARSED);
    1827           22 :   tree body = begin_function_body ();
    1828           22 :   tree compound_stmt = begin_compound_stmt (BCS_FN_BODY);
    1829              : 
    1830           22 :   vec<tree, va_gc> * args = build_arg_list (wrapdecl);
    1831              : 
    1832              :   /* We do not support contracts on virtual functions yet.  */
    1833           22 :   gcc_checking_assert (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
    1834              :                        || !DECL_VIRTUAL_P (fndecl));
    1835              : 
    1836           22 :   tree call = build_thunk_like_call (fndecl, args->length (), args->address ());
    1837              : 
    1838           22 :   finish_return_stmt (call);
    1839              : 
    1840           22 :   finish_compound_stmt (compound_stmt);
    1841           22 :   finish_function_body (body);
    1842           22 :   expand_or_defer_fn (finish_function (/*inline_p=*/false));
    1843           22 :   return true;
    1844              : }
    1845              : 
    1846              : /* If any wrapper functions have been declared, emit their definition.
    1847              :    This might be called multiple times, as we instantiate functions. When
    1848              :    the processing here adds more wrappers, then flag to the caller that
    1849              :    possible additional instantiations should be considered.
    1850              :    Once instantiations are complete, this will be called with done == true.  */
    1851              : 
    1852              : bool
    1853          312 : emit_contract_wrapper_func (bool done)
    1854              : {
    1855          312 :   if (!decl_wrapper_fn || decl_wrapper_fn->is_empty ())
    1856              :     return false;
    1857           26 :   size_t start_elements = decl_wrapper_fn->elements ();
    1858           74 :   decl_wrapper_fn->traverse<void *, define_contract_wrapper_func>(NULL);
    1859           26 :   bool more = decl_wrapper_fn->elements () > start_elements;
    1860           26 :   if (done)
    1861           11 :     decl_wrapper_fn->empty ();
    1862           11 :   gcc_checking_assert (!done || !more);
    1863              :   return more;
    1864              : }
    1865              : 
    1866              : /* Mark most of a contract as being invalid.  */
    1867              : 
    1868              : tree
    1869           12 : invalidate_contract (tree contract)
    1870              : {
    1871           12 :   if (TREE_CODE (contract) == POSTCONDITION_STMT
    1872           12 :       && POSTCONDITION_IDENTIFIER (contract))
    1873           12 :     POSTCONDITION_IDENTIFIER (contract) = error_mark_node;
    1874           12 :   CONTRACT_CONDITION (contract) = error_mark_node;
    1875           12 :   CONTRACT_COMMENT (contract) = error_mark_node;
    1876           12 :   return contract;
    1877              : }
    1878              : 
    1879              : /* Returns an invented parameter declaration of the form 'TYPE ID' for the
    1880              :    purpose of parsing the postcondition.
    1881              : 
    1882              :    We use a PARM_DECL instead of a VAR_DECL so that tsubst forces a lookup
    1883              :    in local specializations when we instantiate these things later.  */
    1884              : 
    1885              : tree
    1886          131 : make_postcondition_variable (cp_expr id, tree type)
    1887              : {
    1888          131 :   if (id == error_mark_node)
    1889              :     return id;
    1890          131 :   gcc_checking_assert (scope_chain && scope_chain->bindings
    1891              :                        && scope_chain->bindings->kind == sk_contract);
    1892              : 
    1893          131 :   tree decl = build_lang_decl (PARM_DECL, id, type);
    1894          131 :   DECL_ARTIFICIAL (decl) = true;
    1895          131 :   DECL_SOURCE_LOCATION (decl) = id.get_location ();
    1896          131 :   return pushdecl (decl);
    1897              : }
    1898              : 
    1899              : /* As above, except that the type is unknown.  */
    1900              : 
    1901              : tree
    1902           79 : make_postcondition_variable (cp_expr id)
    1903              : {
    1904           79 :   return make_postcondition_variable (id, make_auto ());
    1905              : }
    1906              : 
    1907              : /* Check that the TYPE is valid for a named postcondition variable on
    1908              :    function decl FNDECL. Emit a diagnostic if it is not.  Returns TRUE if
    1909              :    the result is OK and false otherwise.  */
    1910              : 
    1911              : bool
    1912          205 : check_postcondition_result (tree fndecl, tree type, location_t loc)
    1913              : {
    1914              :   /* Do not be confused by targetm.cxx.cdtor_return_this ();
    1915              :      conceptually, cdtors have no return value.  */
    1916          205 :   if (VOID_TYPE_P (type)
    1917          390 :       || DECL_CONSTRUCTOR_P (fndecl)
    1918          400 :       || DECL_DESTRUCTOR_P (fndecl))
    1919              :     {
    1920           30 :       error_at (loc,
    1921           20 :                 DECL_CONSTRUCTOR_P (fndecl)
    1922              :                 ? G_("constructor does not return a value to test")
    1923            8 :                 : DECL_DESTRUCTOR_P (fndecl)
    1924            8 :                 ? G_("destructor does not return a value to test")
    1925              :                 : G_("function does not return a value to test"));
    1926           10 :       return false;
    1927              :     }
    1928              : 
    1929              :   return true;
    1930              : }
    1931              : 
    1932              : /* Instantiate each postcondition with the return type to finalize the
    1933              :    contract specifiers on a function decl FNDECL.  */
    1934              : 
    1935              : void
    1936         1062 : rebuild_postconditions (tree fndecl)
    1937              : {
    1938         1062 :   if (!fndecl || fndecl == error_mark_node)
    1939              :     return;
    1940              : 
    1941         1062 :   tree type = TREE_TYPE (TREE_TYPE (fndecl));
    1942              : 
    1943              :   /* If the return type is undeduced, defer until later.  */
    1944         1062 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
    1945              :     return;
    1946              : 
    1947         1004 :   tree contract_spec = get_fn_contract_specifiers (fndecl);
    1948         2573 :   for (; contract_spec ; contract_spec = TREE_CHAIN (contract_spec))
    1949              :     {
    1950         1909 :       tree contract = TREE_VALUE (TREE_VALUE (contract_spec));
    1951         1909 :       if (TREE_CODE (contract) != POSTCONDITION_STMT)
    1952         1454 :         continue;
    1953         1241 :       tree condition = CONTRACT_CONDITION (contract);
    1954         1241 :       if (!condition || condition == error_mark_node)
    1955            2 :         continue;
    1956              : 
    1957              :       /* If any conditions are deferred, they're all deferred.  Note that
    1958              :          we don't have to instantiate postconditions in that case because
    1959              :          the type is available through the declaration.  */
    1960         1239 :       if (TREE_CODE (condition) == DEFERRED_PARSE)
    1961          340 :         return;
    1962              : 
    1963          899 :       tree oldvar = POSTCONDITION_IDENTIFIER (contract);
    1964          899 :       if (!oldvar)
    1965          780 :         continue;
    1966              : 
    1967          119 :       gcc_checking_assert (!DECL_CONTEXT (oldvar)
    1968              :                            || DECL_CONTEXT (oldvar) == fndecl);
    1969          119 :       DECL_CONTEXT (oldvar) = fndecl;
    1970              : 
    1971              :       /* Check the postcondition variable.  */
    1972          119 :       location_t loc = DECL_SOURCE_LOCATION (oldvar);
    1973          119 :       if (!check_postcondition_result (fndecl, type, loc))
    1974              :         {
    1975            4 :           invalidate_contract (contract);
    1976            4 :           continue;
    1977              :         }
    1978              : 
    1979              :       /* "Instantiate" the result variable using the known type.  */
    1980          115 :       tree newvar = copy_node (oldvar);
    1981          115 :       TREE_TYPE (newvar) = type;
    1982              : 
    1983              :       /* Make parameters and result available for substitution.  */
    1984          115 :       local_specialization_stack stack (lss_copy);
    1985          289 :       for (tree t = DECL_ARGUMENTS (fndecl); t != NULL_TREE; t = TREE_CHAIN (t))
    1986          174 :         register_local_identity (t);
    1987          115 :       register_local_specialization (newvar, oldvar);
    1988              : 
    1989          115 :       begin_scope (sk_contract, fndecl);
    1990          115 :       bool old_pc = processing_postcondition;
    1991          115 :       processing_postcondition = true;
    1992              : 
    1993          115 :       condition = tsubst_expr (condition, make_tree_vec (0),
    1994              :                                tf_warning_or_error, fndecl);
    1995              : 
    1996              :       /* Update the contract condition and result.  */
    1997          115 :       POSTCONDITION_IDENTIFIER (contract) = newvar;
    1998          115 :       CONTRACT_CONDITION (contract) = finish_contract_condition (condition);
    1999          115 :       processing_postcondition = old_pc;
    2000          115 :       gcc_checking_assert (scope_chain && scope_chain->bindings
    2001              :                            && scope_chain->bindings->kind == sk_contract);
    2002          115 :       pop_bindings_and_leave_scope ();
    2003          115 :     }
    2004              : }
    2005              : 
    2006              : /* Make a string of the contract condition, if it is available.  */
    2007              : 
    2008              : static tree
    2009          996 : build_comment (cp_expr condition)
    2010              : {
    2011              :   /* Try to get the actual source text for the condition; if that fails pretty
    2012              :      print the resulting tree.  */
    2013          996 :   char *str = get_source_text_between (global_dc->get_file_cache (),
    2014              :                                        condition.get_start (),
    2015              :                                        condition.get_finish ());
    2016          996 :   if (!str)
    2017              :     {
    2018            1 :       const char *str = expr_to_string (condition);
    2019            1 :       return build_string_literal (strlen (str) + 1, str);
    2020              :     }
    2021              : 
    2022          995 :   tree t = build_string_literal (strlen (str) + 1, str);
    2023          995 :   free (str);
    2024          995 :   return t;
    2025              : }
    2026              : 
    2027              : /* Build a contract statement.  */
    2028              : 
    2029              : tree
    2030         1023 : grok_contract (tree contract_spec, tree mode, tree result, cp_expr condition,
    2031              :                location_t loc)
    2032              : {
    2033         1023 :   if (condition == error_mark_node)
    2034              :     return error_mark_node;
    2035              : 
    2036         1006 :   tree_code code;
    2037         1006 :   contract_assertion_kind kind = CAK_INVALID;
    2038         1006 :   if (id_equal (contract_spec, "contract_assert"))
    2039              :     {
    2040              :       code = ASSERTION_STMT;
    2041              :       kind = CAK_ASSERT;
    2042              :     }
    2043          929 :   else if (id_equal (contract_spec, "pre"))
    2044              :     {
    2045              :       code = PRECONDITION_STMT;
    2046              :       kind = CAK_PRE;
    2047              :     }
    2048          523 :   else if (id_equal (contract_spec,"post"))
    2049              :     {
    2050              :       code = POSTCONDITION_STMT;
    2051              :       kind = CAK_POST;
    2052              :     }
    2053              :   else
    2054            0 :     gcc_unreachable ();
    2055              : 
    2056              :   /* Build the contract. The condition is added later.  In the case that
    2057              :      the contract is deferred, result an plain identifier, not a result
    2058              :      variable.  */
    2059          523 :   tree contract;
    2060          523 :   if (code != POSTCONDITION_STMT)
    2061          483 :     contract = build5_loc (loc, code, void_type_node, mode,
    2062              :                            NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
    2063              :   else
    2064              :     {
    2065          523 :       contract = build_nt (code, mode, NULL_TREE, NULL_TREE,
    2066              :                            NULL_TREE, NULL_TREE, result);
    2067          523 :       TREE_TYPE (contract) = void_type_node;
    2068          523 :       SET_EXPR_LOCATION (contract, loc);
    2069              :     }
    2070              : 
    2071              :   /* Determine the assertion kind.  */
    2072         1006 :   CONTRACT_ASSERTION_KIND (contract) = build_int_cst (uint16_type_node, kind);
    2073              : 
    2074              :   /* Determine the evaluation semantic.  This is now an override, so that if
    2075              :      not set we will get the default (currently enforce).  */
    2076         1006 :   CONTRACT_EVALUATION_SEMANTIC (contract)
    2077         2012 :     = build_int_cst (uint16_type_node, (uint16_t)
    2078         1006 :                      flag_contract_evaluation_semantic);
    2079              : 
    2080              :   /* If the contract is deferred, don't do anything with the condition.  */
    2081         1006 :   if (TREE_CODE (condition) == DEFERRED_PARSE)
    2082              :     {
    2083          509 :       CONTRACT_CONDITION (contract) = condition;
    2084          509 :       return contract;
    2085              :     }
    2086              : 
    2087              :   /* Generate the comment from the original condition.  */
    2088          497 :   CONTRACT_COMMENT (contract) = build_comment (condition);
    2089              : 
    2090              :   /* The condition is converted to bool.  */
    2091          497 :   condition = finish_contract_condition (condition);
    2092              : 
    2093          497 :   if (condition == error_mark_node)
    2094              :     return error_mark_node;
    2095              : 
    2096          495 :   CONTRACT_CONDITION (contract) = condition;
    2097              : 
    2098          495 :   return contract;
    2099              : }
    2100              : 
    2101              : /* Build the contract specifier where IDENTIFIER is one of 'pre',
    2102              :    'post' or 'assert' and CONTRACT is the underlying statement.  */
    2103              : 
    2104              : tree
    2105          928 : finish_contract_specifier (tree identifier, tree contract)
    2106              : {
    2107          928 :   if (contract == error_mark_node)
    2108              :     return error_mark_node;
    2109              : 
    2110          928 :   tree contract_spec = build_tree_list (build_tree_list (NULL_TREE, identifier),
    2111              :                                         build_tree_list (NULL_TREE, contract));
    2112              : 
    2113              :   /* Mark the contract as dependent if the condition is dependent.  */
    2114          928 :   tree condition = CONTRACT_CONDITION (contract);
    2115          928 :   if (TREE_CODE (condition) != DEFERRED_PARSE
    2116          928 :       && value_dependent_expression_p (condition))
    2117           87 :     ATTR_IS_DEPENDENT (contract_spec) = true;
    2118              : 
    2119              :   return contract_spec;
    2120              : }
    2121              : 
    2122              : /* Update condition of a late-parsed contract and postcondition variable,
    2123              :    if any.  */
    2124              : 
    2125              : void
    2126          499 : update_late_contract (tree contract, tree result, cp_expr condition)
    2127              : {
    2128          499 :   if (TREE_CODE (contract) == POSTCONDITION_STMT)
    2129          335 :     POSTCONDITION_IDENTIFIER (contract) = result;
    2130              : 
    2131              :   /* Generate the comment from the original condition.  */
    2132          499 :   CONTRACT_COMMENT (contract) = build_comment (condition);
    2133              : 
    2134              :   /* The condition is converted to bool.  */
    2135          499 :   condition = finish_contract_condition (condition);
    2136          499 :   CONTRACT_CONDITION (contract) = condition;
    2137          499 : }
    2138              : 
    2139              : /* Returns the precondition funtion for FNDECL, or null if not set.  */
    2140              : 
    2141              : tree
    2142      1249326 : get_precondition_function (tree fndecl)
    2143              : {
    2144      1249326 :   gcc_checking_assert (fndecl);
    2145      1249326 :   tree *result = hash_map_safe_get (decl_pre_fn, fndecl);
    2146           53 :   return result ? *result : NULL_TREE;
    2147              : }
    2148              : 
    2149              : /* Returns the postcondition funtion for FNDECL, or null if not set.  */
    2150              : 
    2151              : tree
    2152      1249328 : get_postcondition_function (tree fndecl)
    2153              : {
    2154      1249328 :   gcc_checking_assert (fndecl);
    2155      1249328 :   tree *result = hash_map_safe_get (decl_post_fn, fndecl);
    2156           40 :   return result ? *result : NULL_TREE;
    2157              : }
    2158              : 
    2159              : /* Set the PRE and POST functions for FNDECL.  Note that PRE and POST can
    2160              :    be null in this case.  If so the functions are not recorded.  Used by the
    2161              :    modules code.  */
    2162              : 
    2163              : void
    2164       415421 : set_contract_functions (tree fndecl, tree pre, tree post)
    2165              : {
    2166       415421 :   if (pre)
    2167            0 :     set_precondition_function (fndecl, pre);
    2168              : 
    2169       415421 :   if (post)
    2170            0 :     set_postcondition_function (fndecl, post);
    2171       415421 : }
    2172              : 
    2173              : 
    2174              : /* We're compiling the pre/postcondition function CONDFN; remap any FN
    2175              :    contracts that match CODE and emit them.  */
    2176              : 
    2177              : static void
    2178           16 : remap_and_emit_conditions (tree fn, tree condfn, tree_code code)
    2179              : {
    2180           16 :   gcc_assert (code == PRECONDITION_STMT || code == POSTCONDITION_STMT);
    2181           16 :   tree contract_spec = get_fn_contract_specifiers (fn);
    2182           38 :   for (; contract_spec; contract_spec = TREE_CHAIN (contract_spec))
    2183              :     {
    2184           22 :       tree contract = CONTRACT_STATEMENT (contract_spec);
    2185           22 :       if (TREE_CODE (contract) == code)
    2186              :         {
    2187           18 :           contract = copy_node (contract);
    2188           18 :           if (CONTRACT_CONDITION (contract) != error_mark_node)
    2189           18 :             remap_contract (fn, condfn, contract, /*duplicate_p=*/false);
    2190           18 :           emit_contract_statement (contract);
    2191              :         }
    2192              :     }
    2193           16 : }
    2194              : 
    2195              : /* Finish up the pre & post function definitions for a guarded FNDECL,
    2196              :    and compile those functions all the way to assembler language output.  */
    2197              : 
    2198              : void
    2199    166164434 : finish_function_outlined_contracts (tree fndecl)
    2200              : {
    2201              :   /* If the guarded func is either already decided to be ill-formed or is
    2202              :      not yet complete return early.  */
    2203    166164434 :   if (error_operand_p (fndecl)
    2204    166164434 :       || !DECL_INITIAL (fndecl)
    2205    332328868 :       || DECL_INITIAL (fndecl) == error_mark_node)
    2206              :     return;
    2207              : 
    2208              :   /* If there are no contracts here, or we're building them in-line then we
    2209              :      do not need to build the outlined functions.  */
    2210    166164344 :   if (!handle_contracts_p (fndecl)
    2211    166164344 :       || !flag_contract_checks_outlined)
    2212              :     return;
    2213              : 
    2214              :   /* If this is not a client side check and definition side checks are
    2215              :      disabled, do nothing.  */
    2216           15 :   if (!flag_contracts_definition_check
    2217           15 :       && !DECL_CONTRACT_WRAPPER (fndecl))
    2218              :     return;
    2219              : 
    2220              :   /* If either the pre or post functions are bad, don't bother emitting
    2221              :      any contracts.  The program is already ill-formed.  */
    2222           15 :   tree pre = DECL_PRE_FN (fndecl);
    2223           15 :   tree post = DECL_POST_FN (fndecl);
    2224           15 :   if (pre == error_mark_node || post == error_mark_node)
    2225              :     return;
    2226              : 
    2227              :   /* We are generating code, deferred parses should be complete.  */
    2228           15 :   tree contract_spec = get_fn_contract_specifiers (fndecl);
    2229           15 :   gcc_checking_assert (!contract_any_deferred_p (contract_spec));
    2230              : 
    2231           15 :   int flags = SF_DEFAULT | SF_PRE_PARSED;
    2232              : 
    2233           15 :   if (pre && !DECL_INITIAL (pre))
    2234              :     {
    2235            8 :       DECL_PENDING_INLINE_P (pre) = false;
    2236            8 :       start_preparsed_function (pre, DECL_ATTRIBUTES (pre), flags);
    2237            8 :       remap_and_emit_conditions (fndecl, pre, PRECONDITION_STMT);
    2238            8 :       finish_return_stmt (NULL_TREE);
    2239            8 :       pre = finish_function (false);
    2240            8 :       expand_or_defer_fn (pre);
    2241              :     }
    2242              : 
    2243           15 :   if (post && !DECL_INITIAL (post))
    2244              :     {
    2245            8 :       DECL_PENDING_INLINE_P (post) = false;
    2246            8 :       start_preparsed_function (post, DECL_ATTRIBUTES (post), flags);
    2247            8 :       remap_and_emit_conditions (fndecl, post, POSTCONDITION_STMT);
    2248            8 :       gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (post))));
    2249            8 :       finish_return_stmt (NULL_TREE);
    2250            8 :       post = finish_function (false);
    2251            8 :       expand_or_defer_fn (post);
    2252              :     }
    2253              : }
    2254              : 
    2255              : /* ===== Code generation ===== */
    2256              : 
    2257              : /* Insert a BUILT_IN_OBSERVABLE_CHECKPOINT epoch marker.  */
    2258              : 
    2259              : static void
    2260          280 : emit_builtin_observable_checkpoint ()
    2261              : {
    2262          280 :   tree fn = builtin_decl_explicit (BUILT_IN_OBSERVABLE_CHKPT);
    2263          280 :   releasing_vec vec;
    2264          280 :   fn = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
    2265          280 :   finish_expr_stmt (fn);
    2266          280 : }
    2267              : 
    2268              : /* Shared code between TU-local wrappers for the violation handler.  */
    2269              : 
    2270              : static tree
    2271          208 : declare_one_violation_handler_wrapper (tree fn_name, tree fn_type,
    2272              :                                        tree p1_type, tree p2_type)
    2273              : {
    2274          208 :   location_t loc = BUILTINS_LOCATION;
    2275          208 :   tree fn_decl = build_lang_decl_loc (loc, FUNCTION_DECL, fn_name, fn_type);
    2276          208 :   DECL_CONTEXT (fn_decl) = FROB_CONTEXT (global_namespace);
    2277          208 :   DECL_ARTIFICIAL (fn_decl) = true;
    2278          208 :   DECL_INITIAL (fn_decl) = error_mark_node;
    2279              :   /* Let the start function code fill in the result decl.  */
    2280          208 :   DECL_RESULT (fn_decl) = NULL_TREE;
    2281              :   /* Two args violation ref, dynamic info.  */
    2282          208 :   tree parms = cp_build_parm_decl (fn_decl, NULL_TREE, p1_type);
    2283          208 :   TREE_USED (parms) = true;
    2284          208 :   DECL_READ_P (parms) = true;
    2285          208 :   tree p2 = cp_build_parm_decl (fn_decl, NULL_TREE, p2_type);
    2286          208 :   TREE_USED (p2) = true;
    2287          208 :   DECL_READ_P (p2) = true;
    2288          208 :   DECL_CHAIN (parms) = p2;
    2289          208 :   DECL_ARGUMENTS (fn_decl) = parms;
    2290              :   /* Make this function internal.  */
    2291          208 :   TREE_PUBLIC (fn_decl) = false;
    2292          208 :   DECL_EXTERNAL (fn_decl) = false;
    2293          208 :   DECL_WEAK (fn_decl) = false;
    2294          208 :   return fn_decl;
    2295              : }
    2296              : 
    2297              : static GTY(()) tree tu_has_violation = NULL_TREE;
    2298              : static GTY(()) tree tu_has_violation_exception = NULL_TREE;
    2299              : 
    2300              : static void
    2301          608 : declare_violation_handler_wrappers ()
    2302              : {
    2303          608 :   if (tu_has_violation && tu_has_violation_exception)
    2304          608 :     return;
    2305              : 
    2306          104 :   iloc_sentinel ils (input_location);
    2307          104 :   input_location = BUILTINS_LOCATION;
    2308          104 :   tree v_obj_type = builtin_contract_violation_type;
    2309          104 :   v_obj_type = cp_build_qualified_type (v_obj_type, TYPE_QUAL_CONST);
    2310          104 :   v_obj_type = cp_build_reference_type (v_obj_type, /*rval*/false);
    2311          104 :   tree fn_type = build_function_type_list (void_type_node, v_obj_type,
    2312              :                                            uint16_type_node, NULL_TREE);
    2313          104 :   tree fn_name = get_identifier ("__tu_has_violation_exception");
    2314          104 :   tu_has_violation_exception
    2315          104 :     = declare_one_violation_handler_wrapper (fn_name, fn_type, v_obj_type,
    2316              :                                              uint16_type_node);
    2317          104 :   fn_name = get_identifier ("__tu_has_violation");
    2318          104 :   tu_has_violation
    2319          104 :     = declare_one_violation_handler_wrapper (fn_name, fn_type, v_obj_type,
    2320              :                                              uint16_type_node);
    2321          104 : }
    2322              : 
    2323              : static GTY(()) tree tu_terminate_wrapper = NULL_TREE;
    2324              : 
    2325              : /* Declare a noipa wrapper around the call to std::terminate */
    2326              : 
    2327              : static tree
    2328          609 : declare_terminate_wrapper ()
    2329              : {
    2330          609 :   if (tu_terminate_wrapper)
    2331              :     return tu_terminate_wrapper;
    2332              : 
    2333          105 :   iloc_sentinel ils (input_location);
    2334          105 :   input_location = BUILTINS_LOCATION;
    2335              : 
    2336          105 :   tree fn_type = build_function_type_list (void_type_node, NULL_TREE);
    2337          105 :   if (!TREE_NOTHROW (terminate_fn))
    2338            0 :     fn_type = build_exception_variant (fn_type, noexcept_true_spec);
    2339          105 :   tree fn_name = get_identifier ("__tu_terminate_wrapper");
    2340              : 
    2341          105 :   tu_terminate_wrapper
    2342          105 :     = build_lang_decl_loc (input_location, FUNCTION_DECL, fn_name, fn_type);
    2343          105 :   DECL_CONTEXT (tu_terminate_wrapper) = FROB_CONTEXT(global_namespace);
    2344          105 :   DECL_ARTIFICIAL (tu_terminate_wrapper) = true;
    2345          105 :   DECL_INITIAL (tu_terminate_wrapper) = error_mark_node;
    2346              :   /* Let the start function code fill in the result decl.  */
    2347          105 :   DECL_RESULT (tu_terminate_wrapper) = NULL_TREE;
    2348              : 
    2349              :   /* Make this function internal.  */
    2350          105 :   TREE_PUBLIC (tu_terminate_wrapper) = false;
    2351          105 :   DECL_EXTERNAL (tu_terminate_wrapper) = false;
    2352          105 :   DECL_WEAK (tu_terminate_wrapper) = false;
    2353              : 
    2354          105 :   DECL_ATTRIBUTES (tu_terminate_wrapper)
    2355          105 :     = tree_cons (get_identifier ("noipa"), NULL, NULL_TREE);
    2356          105 :   cplus_decl_attributes (&tu_terminate_wrapper,
    2357          105 :                          DECL_ATTRIBUTES (tu_terminate_wrapper), 0);
    2358          105 :   return tu_terminate_wrapper;
    2359          105 : }
    2360              : 
    2361              : /* Define a noipa wrapper around the call to std::terminate */
    2362              : 
    2363              : static void
    2364          105 : build_terminate_wrapper ()
    2365              : {
    2366              :   /* We should not be trying to build this if we never used it.  */
    2367          105 :   gcc_checking_assert (tu_terminate_wrapper);
    2368              : 
    2369          105 :   start_preparsed_function (tu_terminate_wrapper,
    2370          105 :                             DECL_ATTRIBUTES(tu_terminate_wrapper),
    2371              :                             SF_DEFAULT | SF_PRE_PARSED);
    2372          105 :   tree body = begin_function_body ();
    2373          105 :   tree compound_stmt = begin_compound_stmt (BCS_FN_BODY);
    2374          105 :   finish_expr_stmt (build_call_a (terminate_fn, 0, nullptr));
    2375          105 :   finish_return_stmt (NULL_TREE);
    2376          105 :   finish_compound_stmt (compound_stmt);
    2377          105 :   finish_function_body (body);
    2378          105 :   tu_terminate_wrapper = finish_function (false);
    2379          105 :   expand_or_defer_fn (tu_terminate_wrapper);
    2380          105 : }
    2381              : 
    2382              : /* Lookup a name in std::contracts, or inject it.  */
    2383              : 
    2384              : static tree
    2385           84 : lookup_std_contracts_type (tree name_id)
    2386              : {
    2387           84 :   tree id_ns = get_identifier ("contracts");
    2388           84 :   tree ns = lookup_qualified_name (std_node, id_ns);
    2389              : 
    2390           84 :   tree res_type = error_mark_node;
    2391           84 :   if (TREE_CODE (ns) == NAMESPACE_DECL)
    2392            2 :     res_type = lookup_qualified_name
    2393            2 :       (ns, name_id, LOOK_want::TYPE | LOOK_want::HIDDEN_FRIEND);
    2394              : 
    2395           84 :   if (TREE_CODE (res_type) == TYPE_DECL)
    2396            2 :     res_type = TREE_TYPE (res_type);
    2397              :   else
    2398              :     {
    2399           82 :       push_nested_namespace (std_node);
    2400           82 :       push_namespace (id_ns, /*inline*/false);
    2401           82 :       res_type = make_class_type (RECORD_TYPE);
    2402           82 :       create_implicit_typedef (name_id, res_type);
    2403           82 :       DECL_SOURCE_LOCATION (TYPE_NAME (res_type)) = BUILTINS_LOCATION;
    2404           82 :       DECL_CONTEXT (TYPE_NAME (res_type)) = current_namespace;
    2405           82 :       pushdecl_namespace_level (TYPE_NAME (res_type), /*hidden*/true);
    2406           82 :       pop_namespace ();
    2407           82 :       pop_nested_namespace (std_node);
    2408              :     }
    2409           84 :   return res_type;
    2410              : }
    2411              : 
    2412              : /* Return handle_contract_violation (), declaring it if needed.  */
    2413              : 
    2414              : static tree
    2415          208 : declare_handle_contract_violation ()
    2416              : {
    2417              :   /* We may need to declare new types, ensure they are not considered
    2418              :      attached to a named module.  */
    2419          208 :   auto module_kind_override = make_temp_override
    2420          208 :     (module_kind, module_kind & ~(MK_PURVIEW | MK_ATTACH | MK_EXPORTING));
    2421          208 :   tree fnname = get_identifier ("handle_contract_violation");
    2422          208 :   tree viol_name = get_identifier ("contract_violation");
    2423          208 :   tree l = lookup_qualified_name (global_namespace, fnname,
    2424              :                                   LOOK_want::HIDDEN_FRIEND);
    2425          500 :   for (tree f: lkp_range (l))
    2426          208 :     if (TREE_CODE (f) == FUNCTION_DECL)
    2427              :         {
    2428          124 :           tree parms = TYPE_ARG_TYPES (TREE_TYPE (f));
    2429          124 :           if (remaining_arguments (parms) != 1)
    2430            0 :             continue;
    2431          124 :           tree parmtype = non_reference (TREE_VALUE (parms));
    2432          124 :           if (CLASS_TYPE_P (parmtype)
    2433          248 :               && TYPE_IDENTIFIER (parmtype) == viol_name)
    2434          124 :             return f;
    2435              :         }
    2436              : 
    2437           84 :   tree violation = lookup_std_contracts_type (viol_name);
    2438           84 :   tree fntype = NULL_TREE;
    2439           84 :   tree v_obj_ref = cp_build_qualified_type (violation, TYPE_QUAL_CONST);
    2440           84 :   v_obj_ref = cp_build_reference_type (v_obj_ref, /*rval*/false);
    2441           84 :   fntype = build_function_type_list (void_type_node, v_obj_ref, NULL_TREE);
    2442              : 
    2443           84 :   push_nested_namespace (global_namespace);
    2444           84 :   tree fndecl
    2445           84 :     = build_cp_library_fn_ptr ("handle_contract_violation", fntype, ECF_COLD);
    2446           84 :   pushdecl_namespace_level (fndecl, /*hiding*/true);
    2447           84 :   pop_nested_namespace (global_namespace);
    2448              : 
    2449              :   /* Build the parameter(s).  */
    2450           84 :   tree parms = cp_build_parm_decl (fndecl, NULL_TREE, v_obj_ref);
    2451           84 :   TREE_USED (parms) = true;
    2452           84 :   DECL_READ_P (parms) = true;
    2453           84 :   DECL_ARGUMENTS (fndecl) = parms;
    2454           84 :   return fndecl;
    2455          208 : }
    2456              : 
    2457              : /* Build the call to handle_contract_violation for VIOLATION.  */
    2458              : 
    2459              : static void
    2460          208 : build_contract_handler_call (tree violation)
    2461              : {
    2462          208 :   tree violation_fn = declare_handle_contract_violation ();
    2463          208 :   tree call = build_call_n (violation_fn, 1, violation);
    2464          208 :   finish_expr_stmt (call);
    2465          208 : }
    2466              : 
    2467              : /* If we have emitted any contracts in this TU that will call a violation
    2468              :    handler, then emit the wrappers for the handler.  */
    2469              : 
    2470              : void
    2471          130 : maybe_emit_violation_handler_wrappers ()
    2472              : {
    2473              :   /* We might need the terminate wrapper, even if we do not use the violation
    2474              :      handler wrappers.  */
    2475          130 :   if (tu_terminate_wrapper && flag_contracts_conservative_ipa)
    2476          105 :     build_terminate_wrapper ();
    2477              : 
    2478          130 :   if (!tu_has_violation && !tu_has_violation_exception)
    2479              :     return;
    2480              : 
    2481          104 :   tree terminate_wrapper = terminate_fn;
    2482          104 :   if (flag_contracts_conservative_ipa)
    2483          104 :     terminate_wrapper = tu_terminate_wrapper;
    2484              : 
    2485              :   /* tu_has_violation */
    2486          104 :   start_preparsed_function (tu_has_violation, NULL_TREE,
    2487              :                             SF_DEFAULT | SF_PRE_PARSED);
    2488          104 :   tree body = begin_function_body ();
    2489          104 :   tree compound_stmt = begin_compound_stmt (BCS_FN_BODY);
    2490          104 :   tree v = DECL_ARGUMENTS (tu_has_violation);
    2491          104 :   tree semantic = DECL_CHAIN (v);
    2492              : 
    2493              :   /* We are going to call the handler.  */
    2494          104 :   build_contract_handler_call (v);
    2495              : 
    2496          104 :   tree if_observe = begin_if_stmt ();
    2497              :   /* if (observe) return; */
    2498          104 :   tree cond = build2 (EQ_EXPR, uint16_type_node, semantic,
    2499              :                       build_int_cst (uint16_type_node, (uint16_t)CES_OBSERVE));
    2500          104 :   finish_if_stmt_cond (cond, if_observe);
    2501          104 :   emit_builtin_observable_checkpoint ();
    2502          104 :   finish_then_clause (if_observe);
    2503          104 :   begin_else_clause (if_observe);
    2504              :   /* else terminate.  */
    2505          104 :   finish_expr_stmt (build_call_a (terminate_wrapper, 0, nullptr));
    2506          104 :   finish_else_clause (if_observe);
    2507          104 :   finish_if_stmt (if_observe);
    2508          104 :   finish_return_stmt (NULL_TREE);
    2509              : 
    2510          104 :   finish_compound_stmt (compound_stmt);
    2511          104 :   finish_function_body (body);
    2512          104 :   tu_has_violation = finish_function (false);
    2513          104 :   expand_or_defer_fn (tu_has_violation);
    2514              : 
    2515              :   /* tu_has_violation_exception */
    2516          104 :   start_preparsed_function (tu_has_violation_exception, NULL_TREE,
    2517              :                             SF_DEFAULT | SF_PRE_PARSED);
    2518          104 :   body = begin_function_body ();
    2519          104 :   compound_stmt = begin_compound_stmt (BCS_FN_BODY);
    2520          104 :   v = DECL_ARGUMENTS (tu_has_violation_exception);
    2521          104 :   semantic = DECL_CHAIN (v);
    2522          104 :   location_t loc = DECL_SOURCE_LOCATION (tu_has_violation_exception);
    2523              : 
    2524          104 :   tree a_type = strip_top_quals (non_reference (TREE_TYPE (v)));
    2525          104 :   tree v2 = build_decl (loc, VAR_DECL, NULL_TREE, a_type);
    2526          104 :   DECL_SOURCE_LOCATION (v2) = loc;
    2527          104 :   DECL_CONTEXT (v2) = current_function_decl;
    2528          104 :   DECL_ARTIFICIAL (v2) = true;
    2529          104 :   layout_decl (v2, 0);
    2530          104 :   v2 = pushdecl (v2);
    2531          104 :   add_decl_expr (v2);
    2532          104 :   tree r = cp_build_init_expr (v2, convert_from_reference (v));
    2533          104 :   finish_expr_stmt (r);
    2534          104 :   tree memb = lookup_member (a_type, get_identifier ("_M_detection_mode"),
    2535              :                      /*protect=*/1, /*want_type=*/0, tf_warning_or_error);
    2536          104 :   r = build_class_member_access_expr (v2, memb, NULL_TREE, false,
    2537              :                                       tf_warning_or_error);
    2538          104 :   r = cp_build_modify_expr
    2539          104 :    (loc, r, NOP_EXPR,
    2540              :     build_int_cst (uint16_type_node, (uint16_t)CDM_EVAL_EXCEPTION),
    2541              :     tf_warning_or_error);
    2542          104 :   finish_expr_stmt (r);
    2543              :   /* We are going to call the handler.  */
    2544          104 :   build_contract_handler_call (v);
    2545              : 
    2546          104 :   if_observe = begin_if_stmt ();
    2547              :   /* if (observe) return; */
    2548          104 :   cond = build2 (EQ_EXPR, uint16_type_node, semantic,
    2549              :                  build_int_cst (uint16_type_node, (uint16_t)CES_OBSERVE));
    2550          104 :   finish_if_stmt_cond (cond, if_observe);
    2551          104 :   emit_builtin_observable_checkpoint ();
    2552          104 :   finish_then_clause (if_observe);
    2553          104 :   begin_else_clause (if_observe);
    2554              :   /* else terminate.  */
    2555          104 :   finish_expr_stmt (build_call_a (terminate_wrapper, 0, nullptr));
    2556          104 :   finish_else_clause (if_observe);
    2557          104 :   finish_if_stmt (if_observe);
    2558          104 :   finish_return_stmt (NULL_TREE);
    2559          104 :   finish_compound_stmt (compound_stmt);
    2560          104 :   finish_function_body (body);
    2561          104 :   tu_has_violation_exception = finish_function (false);
    2562          104 :   expand_or_defer_fn (tu_has_violation_exception);
    2563              : }
    2564              : 
    2565              : /* Build a layout-compatible internal version of contract_violation type.  */
    2566              : 
    2567              : static tree
    2568          134 : get_contract_violation_fields ()
    2569              : {
    2570          134 :   tree fields = NULL_TREE;
    2571              :   /* Must match <contracts>:
    2572              :   class contract_violation {
    2573              :     uint16_t _M_version;
    2574              :     assertion_kind _M_assertion_kind;
    2575              :     evaluation_semantic _M_evaluation_semantic;
    2576              :     detection_mode _M_detection_mode;
    2577              :     const char* _M_comment;
    2578              :     void *_M_src_loc_ptr;
    2579              :     __vendor_ext* _M_ext;
    2580              :   };
    2581              :     If this changes, also update the initializer in
    2582              :     build_contract_violation.  */
    2583          134 :   const tree types[] = { uint16_type_node,
    2584              :                          uint16_type_node,
    2585              :                          uint16_type_node,
    2586              :                          uint16_type_node,
    2587          134 :                          const_string_type_node,
    2588          134 :                          ptr_type_node,
    2589              :                          ptr_type_node
    2590          134 :                         };
    2591          134 :  const char *names[] = { "_M_version",
    2592              :                          "_M_assertion_kind",
    2593              :                          "_M_evaluation_semantic",
    2594              :                          "_M_detection_mode",
    2595              :                          "_M_comment",
    2596              :                          "_M_src_loc_ptr",
    2597              :                          "_M_ext",
    2598              :                         };
    2599          134 :   unsigned n = 0;
    2600         1072 :   for (tree type : types)
    2601              :     {
    2602              :       /* finish_builtin_struct wants fields chained in reverse.  */
    2603          938 :       tree next = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    2604          938 :                                   get_identifier(names[n++]), type);
    2605          938 :       DECL_CHAIN (next) = fields;
    2606          938 :       fields = next;
    2607              :     }
    2608          134 :  return fields;
    2609              : }
    2610              : 
    2611              : /* Build a type to represent contract violation objects.  */
    2612              : 
    2613              : static tree
    2614          134 : init_builtin_contract_violation_type ()
    2615              : {
    2616          134 :   if (builtin_contract_violation_type)
    2617              :     return builtin_contract_violation_type;
    2618              : 
    2619          134 :   tree fields = get_contract_violation_fields ();
    2620              : 
    2621          134 :   iloc_sentinel ils (input_location);
    2622          134 :   input_location = BUILTINS_LOCATION;
    2623          134 :   builtin_contract_violation_type = make_class_type (RECORD_TYPE);
    2624          134 :   finish_builtin_struct (builtin_contract_violation_type,
    2625              :                          "__builtin_contract_violation_type", fields, NULL_TREE);
    2626          268 :   CLASSTYPE_AS_BASE (builtin_contract_violation_type)
    2627          134 :     = builtin_contract_violation_type;
    2628          134 :   DECL_CONTEXT (TYPE_NAME (builtin_contract_violation_type))
    2629          134 :     = FROB_CONTEXT (global_namespace);
    2630          134 :   CLASSTYPE_LITERAL_P (builtin_contract_violation_type) = true;
    2631          134 :   CLASSTYPE_LAZY_COPY_CTOR (builtin_contract_violation_type) = true;
    2632          134 :   xref_basetypes (builtin_contract_violation_type, /*bases=*/NULL_TREE);
    2633          134 :   DECL_CONTEXT (TYPE_NAME (builtin_contract_violation_type))
    2634          134 :     = FROB_CONTEXT (global_namespace);
    2635          134 :   DECL_ARTIFICIAL (TYPE_NAME (builtin_contract_violation_type)) = true;
    2636          134 :   TYPE_ARTIFICIAL (builtin_contract_violation_type) = true;
    2637          134 :   builtin_contract_violation_type
    2638          134 :     = cp_build_qualified_type (builtin_contract_violation_type,
    2639              :                                TYPE_QUAL_CONST);
    2640          134 :   return builtin_contract_violation_type;
    2641          134 : }
    2642              : 
    2643              : /* Early initialisation of types and functions we will use.  */
    2644              : void
    2645          134 : init_contracts ()
    2646              : {
    2647          134 :   init_terminate_fn ();
    2648          134 :   init_builtin_contract_violation_type ();
    2649          134 : }
    2650              : 
    2651              : static GTY(()) tree contracts_source_location_impl_type;
    2652              : 
    2653              : /* Build a layout-compatible internal version of source location __impl
    2654              :    type.  */
    2655              : 
    2656              : static tree
    2657          104 : get_contracts_source_location_impl_type (tree context = NULL_TREE)
    2658              : {
    2659          104 :   if (contracts_source_location_impl_type)
    2660              :      return contracts_source_location_impl_type;
    2661              : 
    2662              :   /* First see if we have a declaration that we can use.  */
    2663          104 :   tree contracts_source_location_type
    2664          104 :     = lookup_std_type (get_identifier ("source_location"));
    2665              : 
    2666          104 :   if (contracts_source_location_type
    2667          104 :       && contracts_source_location_type != error_mark_node
    2668          208 :       && TYPE_FIELDS (contracts_source_location_type))
    2669              :     {
    2670           22 :       contracts_source_location_impl_type = get_source_location_impl_type ();
    2671           22 :       return contracts_source_location_impl_type;
    2672              :     }
    2673              : 
    2674              :   /* We do not, so build the __impl layout equivalent type, which must
    2675              :      match <source_location>:
    2676              :      struct __impl
    2677              :       {
    2678              :           const char* _M_file_name;
    2679              :           const char* _M_function_name;
    2680              :           unsigned _M_line;
    2681              :           unsigned _M_column;
    2682              :       }; */
    2683           82 :   const tree types[] = { const_string_type_node,
    2684              :                         const_string_type_node,
    2685           82 :                         uint_least32_type_node,
    2686           82 :                         uint_least32_type_node };
    2687              : 
    2688           82 :  const char *names[] = { "_M_file_name",
    2689              :                          "_M_function_name",
    2690              :                          "_M_line",
    2691              :                          "_M_column",
    2692              :                         };
    2693           82 :   tree fields = NULL_TREE;
    2694           82 :   unsigned n = 0;
    2695          410 :   for (tree type : types)
    2696              :   {
    2697              :     /* finish_builtin_struct wants fields chained in reverse.  */
    2698          328 :     tree next = build_decl (BUILTINS_LOCATION, FIELD_DECL,
    2699          328 :                             get_identifier (names[n++]), type);
    2700          328 :     DECL_CHAIN (next) = fields;
    2701          328 :     fields = next;
    2702              :   }
    2703              : 
    2704           82 :   iloc_sentinel ils (input_location);
    2705           82 :   input_location = BUILTINS_LOCATION;
    2706           82 :   contracts_source_location_impl_type = cxx_make_type (RECORD_TYPE);
    2707           82 :   finish_builtin_struct (contracts_source_location_impl_type,
    2708              :                          "__impl", fields, NULL_TREE);
    2709           82 :   DECL_CONTEXT (TYPE_NAME (contracts_source_location_impl_type)) = context;
    2710           82 :   DECL_ARTIFICIAL (TYPE_NAME (contracts_source_location_impl_type)) = true;
    2711           82 :   TYPE_ARTIFICIAL (contracts_source_location_impl_type) = true;
    2712           82 :   contracts_source_location_impl_type
    2713           82 :     = cp_build_qualified_type (contracts_source_location_impl_type,
    2714              :                                TYPE_QUAL_CONST);
    2715              : 
    2716           82 :   return contracts_source_location_impl_type;
    2717           82 : }
    2718              : 
    2719              : static tree
    2720          608 : get_src_loc_impl_ptr (location_t loc)
    2721              : {
    2722          608 :   if (!contracts_source_location_impl_type)
    2723          104 :     get_contracts_source_location_impl_type ();
    2724              : 
    2725          608 :   tree fndecl = current_function_decl;
    2726              :   /* We might be an outlined function.  */
    2727          608 :   if (DECL_IS_PRE_FN_P (fndecl) || DECL_IS_POST_FN_P (fndecl))
    2728           18 :     fndecl = get_orig_for_outlined (fndecl);
    2729              :   /* We might be a wrapper.  */
    2730          608 :   if (DECL_IS_WRAPPER_FN_P (fndecl))
    2731           34 :     fndecl = get_orig_func_for_wrapper (fndecl);
    2732              : 
    2733          608 :   gcc_checking_assert (fndecl);
    2734          608 :   tree impl__
    2735          608 :     = build_source_location_impl (loc, fndecl,
    2736              :                                   contracts_source_location_impl_type);
    2737          608 :   tree p = build_pointer_type (contracts_source_location_impl_type);
    2738          608 :   return build_fold_addr_expr_with_type_loc (loc, impl__, p);
    2739              : }
    2740              : 
    2741              : /* Build a contract_violation layout compatible object. */
    2742              : 
    2743              : /* Constructor.  At present, this should always be constant. */
    2744              : 
    2745              : static tree
    2746          608 : build_contract_violation_ctor (tree contract)
    2747              : {
    2748          608 :   bool can_be_const = true;
    2749          608 :   uint16_t version = 1;
    2750              :   /* Default CDM_PREDICATE_FALSE. */
    2751          608 :   uint16_t detection_mode = CDM_PREDICATE_FALSE;
    2752              : 
    2753          608 :   tree assertion_kind = CONTRACT_ASSERTION_KIND (contract);
    2754          608 :   if (!assertion_kind || really_constant_p (assertion_kind))
    2755              :     {
    2756          608 :       contract_assertion_kind kind = get_contract_assertion_kind (contract);
    2757          608 :       assertion_kind = build_int_cst (uint16_type_node, kind);
    2758              :     }
    2759              :   else
    2760              :     can_be_const = false;
    2761              : 
    2762          608 :   tree eval_semantic = CONTRACT_EVALUATION_SEMANTIC (contract);
    2763          608 :   gcc_checking_assert (eval_semantic);
    2764          608 :   if (!really_constant_p (eval_semantic))
    2765            0 :     can_be_const = false;
    2766              : 
    2767          608 :   tree comment = CONTRACT_COMMENT (contract);
    2768          608 :   if (comment && !really_constant_p (comment))
    2769              :     can_be_const = false;
    2770              : 
    2771          608 :   tree std_src_loc_impl_ptr = CONTRACT_STD_SOURCE_LOC (contract);
    2772          608 :   if (std_src_loc_impl_ptr)
    2773              :     {
    2774            0 :       std_src_loc_impl_ptr = convert_from_reference (std_src_loc_impl_ptr);
    2775            0 :       if (!really_constant_p (std_src_loc_impl_ptr))
    2776            0 :         can_be_const = false;
    2777              :     }
    2778              :   else
    2779          608 :     std_src_loc_impl_ptr = get_src_loc_impl_ptr (EXPR_LOCATION (contract));
    2780              : 
    2781              :   /* Must match the type layout in builtin_contract_violation_type.  */
    2782          608 :   tree f0 = next_aggregate_field (TYPE_FIELDS (builtin_contract_violation_type));
    2783          608 :   tree f1 = next_aggregate_field (DECL_CHAIN (f0));
    2784          608 :   tree f2 = next_aggregate_field (DECL_CHAIN (f1));
    2785          608 :   tree f3 = next_aggregate_field (DECL_CHAIN (f2));
    2786          608 :   tree f4 = next_aggregate_field (DECL_CHAIN (f3));
    2787          608 :   tree f5 = next_aggregate_field (DECL_CHAIN (f4));
    2788          608 :   tree f6 = next_aggregate_field (DECL_CHAIN (f5));
    2789          608 :   tree ctor = build_constructor_va
    2790          608 :     (builtin_contract_violation_type, 7,
    2791          608 :      f0, build_int_cst (uint16_type_node, version),
    2792              :      f1, assertion_kind,
    2793              :      f2, eval_semantic,
    2794          608 :      f3, build_int_cst (uint16_type_node, detection_mode),
    2795              :      f4, comment,
    2796              :      f5, std_src_loc_impl_ptr,
    2797              :      f6, build_zero_cst (nullptr_type_node)); // __vendor_ext
    2798              : 
    2799          608 :   TREE_READONLY (ctor) = true;
    2800          608 :   if (can_be_const)
    2801          608 :     TREE_CONSTANT (ctor) = true;
    2802              : 
    2803          608 :   return ctor;
    2804              : }
    2805              : 
    2806              : /* Build a named TU-local constant of TYPE.  */
    2807              : 
    2808              : static tree
    2809          608 : contracts_tu_local_named_var (location_t loc, const char *name, tree type)
    2810              : {
    2811          608 :   tree var_ = build_decl (loc, VAR_DECL, NULL, type);
    2812          608 :   DECL_NAME (var_) = generate_internal_label (name);
    2813          608 :   TREE_PUBLIC (var_) = false;
    2814          608 :   DECL_EXTERNAL (var_) = false;
    2815          608 :   TREE_STATIC (var_) = true;
    2816              :   /* Compiler-generated.  */
    2817          608 :   DECL_ARTIFICIAL (var_) = true;
    2818          608 :   TREE_CONSTANT (var_) = true;
    2819          608 :   layout_decl (var_, 0);
    2820          608 :   return var_;
    2821              : }
    2822              : 
    2823              : /* Create a read-only violation object.  */
    2824              : 
    2825              : static tree
    2826          608 : build_contract_violation_constant (tree ctor, tree contract)
    2827              : {
    2828          608 :   tree viol_ = contracts_tu_local_named_var
    2829          608 :     (EXPR_LOCATION (contract), "Lcontract_violation",
    2830              :      builtin_contract_violation_type);
    2831              : 
    2832          608 :   TREE_CONSTANT (viol_) = true;
    2833          608 :   DECL_INITIAL (viol_) = ctor;
    2834          608 :   varpool_node::finalize_decl (viol_);
    2835              : 
    2836          608 :   return viol_;
    2837              : }
    2838              : 
    2839              : /* Helper to replace references to dummy this parameters with references to
    2840              :    the first argument of the FUNCTION_DECL DATA.  */
    2841              : 
    2842              : static tree
    2843         2913 : remap_dummy_this_1 (tree *tp, int *, void *data)
    2844              : {
    2845         2913 :   if (!is_this_parameter (*tp))
    2846              :     return NULL_TREE;
    2847           30 :   tree fn = (tree)data;
    2848           30 :   *tp = DECL_ARGUMENTS (fn);
    2849           30 :   return NULL_TREE;
    2850              : }
    2851              : 
    2852              : /* Replace all references to dummy this parameters in EXPR with references to
    2853              :    the first argument of the FUNCTION_DECL FNDECL.  */
    2854              : 
    2855              : static void
    2856          609 : remap_dummy_this (tree fndecl, tree *expr)
    2857              : {
    2858            0 :   walk_tree (expr, remap_dummy_this_1, fndecl, NULL);
    2859            0 : }
    2860              : 
    2861              : /* Replace uses of user's placeholder var with the actual return value.  */
    2862              : 
    2863              : struct replace_tree
    2864              : {
    2865              :   tree from, to;
    2866              : };
    2867              : 
    2868              : static tree
    2869         1179 : remap_retval_1 (tree *here, int *do_subtree, void *d)
    2870              : {
    2871         1179 :   replace_tree *data = (replace_tree *) d;
    2872              : 
    2873         1179 :   if (*here == data->from)
    2874              :     {
    2875           58 :       *here = data->to;
    2876           58 :       *do_subtree = 0;
    2877              :     }
    2878              :   else
    2879         1121 :     *do_subtree = 1;
    2880         1179 :   return NULL_TREE;
    2881              : }
    2882              : 
    2883              : static void
    2884          232 : remap_retval (tree fndecl, tree contract)
    2885              : {
    2886          232 :   struct replace_tree data;
    2887          232 :   data.from = POSTCONDITION_IDENTIFIER (contract);
    2888          232 :   gcc_checking_assert (DECL_RESULT (fndecl));
    2889          232 :   data.to = DECL_RESULT (fndecl);
    2890          232 :   walk_tree (&CONTRACT_CONDITION (contract), remap_retval_1, &data, NULL);
    2891          232 : }
    2892              : 
    2893              : 
    2894              : /* Genericize a CONTRACT tree, but do not attach it to the current context,
    2895              :    the caller is responsible for that.
    2896              :    This is called during genericization.  */
    2897              : 
    2898              : tree
    2899          610 : build_contract_check (tree contract)
    2900              : {
    2901          610 :   contract_evaluation_semantic semantic = get_evaluation_semantic (contract);
    2902          610 :   bool quick = false;
    2903          610 :   bool calls_handler = false;
    2904          610 :   switch (semantic)
    2905              :     {
    2906            1 :     case CES_IGNORE:
    2907            1 :       return void_node;
    2908              :     case CES_ENFORCE:
    2909              :     case CES_OBSERVE:
    2910              :       calls_handler = true;
    2911              :       break;
    2912            1 :     case CES_QUICK:
    2913            1 :       quick = true;
    2914            1 :       break;
    2915            0 :     default:
    2916            0 :       gcc_unreachable ();
    2917              :     }
    2918              : 
    2919          609 :   location_t loc = EXPR_LOCATION (contract);
    2920              : 
    2921          609 :   remap_dummy_this (current_function_decl, &CONTRACT_CONDITION (contract));
    2922          609 :   tree condition = CONTRACT_CONDITION (contract);
    2923          609 :   if (condition == error_mark_node)
    2924              :     return NULL_TREE;
    2925              : 
    2926          609 :   if (!flag_contract_checks_outlined && POSTCONDITION_P (contract))
    2927              :     {
    2928          232 :       remap_retval (current_function_decl, contract);
    2929          232 :       condition = CONTRACT_CONDITION (contract);
    2930          232 :       if (condition == error_mark_node)
    2931              :         return NULL_TREE;
    2932              :     }
    2933              : 
    2934          609 :   tree terminate_wrapper = terminate_fn;
    2935          609 :   if (flag_contracts_conservative_ipa)
    2936          609 :     terminate_wrapper = declare_terminate_wrapper ();
    2937          609 :   if (calls_handler)
    2938          608 :     declare_violation_handler_wrappers ();
    2939              : 
    2940          609 :   bool check_might_throw = (flag_exceptions
    2941          609 :                             && !expr_noexcept_p (condition, tf_none));
    2942              : 
    2943              :   /* Build a statement expression to hold a contract check, with the check
    2944              :      potentially wrapped in a try-catch expr.  */
    2945          609 :   tree cc_bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
    2946          609 :   BIND_EXPR_BODY (cc_bind) = push_stmt_list ();
    2947              : 
    2948          609 :   if (TREE_CODE (contract) == ASSERTION_STMT)
    2949           72 :     emit_builtin_observable_checkpoint ();
    2950          609 :   tree cond = build_x_unary_op (loc, TRUTH_NOT_EXPR, condition, NULL_TREE,
    2951              :                                 tf_warning_or_error);
    2952          609 :   tree violation;
    2953          609 :   bool viol_is_var = false;
    2954          609 :   if (quick)
    2955              :     /* We will not be calling a handler.  */
    2956            1 :     violation = build_zero_cst (nullptr_type_node);
    2957              :   else
    2958              :     {
    2959              :       /* Build a violation object, with the contract settings.  */
    2960          608 :       tree ctor = build_contract_violation_ctor (contract);
    2961          608 :       gcc_checking_assert (TREE_CONSTANT (ctor));
    2962          608 :       violation = build_contract_violation_constant (ctor, contract);
    2963          608 :       violation = build_address (violation);
    2964              :     }
    2965              : 
    2966          609 :   tree s_const = build_int_cst (uint16_type_node, semantic);
    2967              :   /* So now do we need a try-catch?  */
    2968          609 :   if (check_might_throw)
    2969              :     {
    2970              :       /* This will hold the computed condition.  */
    2971          118 :       tree check_failed = build_decl (loc, VAR_DECL, NULL, boolean_type_node);
    2972          118 :       DECL_ARTIFICIAL (check_failed) = true;
    2973          118 :       DECL_IGNORED_P (check_failed) = true;
    2974          118 :       DECL_CONTEXT (check_failed) = current_function_decl;
    2975          118 :       layout_decl (check_failed, 0);
    2976          118 :       add_decl_expr (check_failed);
    2977          118 :       DECL_CHAIN (check_failed) = BIND_EXPR_VARS (cc_bind);
    2978          118 :       BIND_EXPR_VARS (cc_bind) = check_failed;
    2979          118 :       tree check_try = begin_try_block ();
    2980          118 :       finish_expr_stmt (cp_build_init_expr (check_failed, cond));
    2981          118 :       finish_try_block (check_try);
    2982              : 
    2983          118 :       tree handler = begin_handler ();
    2984          118 :       finish_handler_parms (NULL_TREE, handler); /* catch (...) */
    2985          118 :       if (quick)
    2986            0 :         finish_expr_stmt (build_call_a (terminate_wrapper, 0, nullptr));
    2987              :       else
    2988              :         {
    2989          118 :           if (viol_is_var)
    2990              :             {
    2991              :               /* We can update the detection mode here.  */
    2992              :               tree memb
    2993              :                 = lookup_member (builtin_contract_violation_type,
    2994              :                                  get_identifier ("_M_detection_mode"),
    2995              :                                  1, 0, tf_warning_or_error);
    2996              :               tree r = cp_build_indirect_ref (loc, violation, RO_UNARY_STAR,
    2997              :                                               tf_warning_or_error);
    2998              :               r = build_class_member_access_expr (r, memb, NULL_TREE, false,
    2999              :                                                   tf_warning_or_error);
    3000              :               r = cp_build_modify_expr
    3001              :                 (loc, r, NOP_EXPR,
    3002              :                  build_int_cst (uint16_type_node, (uint16_t)CDM_EVAL_EXCEPTION),
    3003              :                  tf_warning_or_error);
    3004              :               finish_expr_stmt (r);
    3005              :               finish_expr_stmt (build_call_n (tu_has_violation, 2,
    3006              :                                               violation, s_const));
    3007              :             }
    3008              :           else
    3009              :             /* We need to make a copy of the violation object to update.  */
    3010          118 :             finish_expr_stmt (build_call_n (tu_has_violation_exception, 2,
    3011              :                                             violation, s_const));
    3012              :           /* If we reach here, we have handled the exception thrown and do not
    3013              :              need further action.  */
    3014          118 :           tree e = cp_build_modify_expr (loc, check_failed, NOP_EXPR,
    3015              :                                          boolean_false_node,
    3016              :                                          tf_warning_or_error);
    3017          118 :           finish_expr_stmt (e);
    3018              :         }
    3019          118 :       finish_handler (handler);
    3020          118 :       finish_handler_sequence (check_try);
    3021          118 :       cond = check_failed;
    3022          118 :       BIND_EXPR_VARS (cc_bind) = nreverse (BIND_EXPR_VARS (cc_bind));
    3023              :     }
    3024              : 
    3025          609 :   tree do_check = begin_if_stmt ();
    3026          609 :   finish_if_stmt_cond (cond, do_check);
    3027          609 :   if (quick)
    3028            1 :     finish_expr_stmt (build_call_a (terminate_wrapper, 0, nullptr));
    3029              :   else
    3030          608 :     finish_expr_stmt (build_call_n (tu_has_violation, 2, violation, s_const));
    3031          609 :   finish_then_clause (do_check);
    3032          609 :   finish_if_stmt (do_check);
    3033              : 
    3034          609 :   BIND_EXPR_BODY (cc_bind) = pop_stmt_list (BIND_EXPR_BODY (cc_bind));
    3035          609 :   return cc_bind;
    3036              : }
    3037              : 
    3038              : #include "gt-cp-contracts.h"
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.