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