LCOV - code coverage report
Current view: top level - gcc/cp - contracts.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.2 % 1327 1276
Test Date: 2026-05-11 19:44:49 Functions: 100.0 % 99 99
Legend: Lines:     hit not hit

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