LCOV - code coverage report
Current view: top level - gcc/cp - contracts.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.5 % 1346 1286
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 98 98
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.5.0-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.