]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++, contracts: Simplify contracts headers [NFC].
authorIain Sandoe <iain@sandoe.co.uk>
Sat, 30 Aug 2025 11:14:58 +0000 (12:14 +0100)
committerIain Sandoe <iain@sandoe.co.uk>
Tue, 2 Sep 2025 15:01:33 +0000 (16:01 +0100)
We have contracts-related declarations and macros split between contracts.h
and cp-tree.h, and then contracts.h is included in the latter, which means
that it is included in all c++ front end files.

This patch:
 - moves all the contracts-related material to contracts.h.
 - makes some functions that are only used in contracts.cc static.
 - tries to group the external API for contracts into related topics.
 - includes contracts.h in the front end sources that need it.

gcc/cp/ChangeLog:

* constexpr.cc: Include contracts.h
* coroutines.cc: Likewise.
* cp-gimplify.cc: Likewise.
* decl.cc: Likewise.
* decl2.cc: Likewise.
* mangle.cc: Likewise.
* module.cc: Likewise.
* pt.cc: Likewise.
* search.cc: Likewise.
* semantics.cc: Likewise.
* contracts.cc (validate_contract_role, setup_default_contract_role,
add_contract_role, get_concrete_axiom_semantic,
get_default_contract_role): Make static.
* cp-tree.h (make_postcondition_variable, grok_contract,
finish_contract_condition, find_contract, set_decl_contracts,
get_contract_semantic, set_contract_semantic): Move to contracts.h.
* contracts.h (get_contract_role, add_contract_role,
validate_contract_role, setup_default_contract_role,
lookup_concrete_semantic, get_default_contract_role): Remove.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
13 files changed:
gcc/cp/constexpr.cc
gcc/cp/contracts.cc
gcc/cp/contracts.h
gcc/cp/coroutines.cc
gcc/cp/cp-gimplify.cc
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/decl2.cc
gcc/cp/mangle.cc
gcc/cp/module.cc
gcc/cp/pt.cc
gcc/cp/search.cc
gcc/cp/semantics.cc

index 701420ca8ec018b0e13dd1125c58e3c194808a16..c3d05248a9a7ce8d4b1c50cac9ffb51ec4025538 100644 (file)
@@ -41,6 +41,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "intl.h"
 #include "toplev.h"
+#include "contracts.h"
 
 static bool verify_constant (tree, bool, bool *, bool *);
 #define VERIFY_CONSTANT(X)                                             \
index d0cfd2efd552647a873397ce14af0d7d3b1ad4ae..042524dca38208df055027564df8f829b8de8359 100644 (file)
@@ -159,7 +159,7 @@ bool valid_configs[CCS_MAYBE + 1][CCS_MAYBE + 1] = {
   { 0, 1, 0, 0, 1, },
 };
 
-void
+static void
 validate_contract_role (contract_role *role)
 {
   gcc_assert (role);
@@ -171,7 +171,7 @@ validate_contract_role (contract_role *role)
                "the %<default%> semantic");
 }
 
-contract_semantic
+static contract_semantic
 lookup_concrete_semantic (const char *name)
 {
   if (strcmp (name, "ignore") == 0)
@@ -210,7 +210,9 @@ role_name_equal (contract_role *role, const char *name)
   return role_name_equal (role->name, name);
 }
 
-contract_role *
+static void setup_default_contract_role (bool update = true);
+
+static contract_role *
 get_contract_role (const char *name)
 {
   for (int i = 0; i < max_custom_roles; ++i)
@@ -227,12 +229,12 @@ get_contract_role (const char *name)
   return NULL;
 }
 
-contract_role *
+static contract_role *
 add_contract_role (const char *name,
                   contract_semantic des,
                   contract_semantic aus,
                   contract_semantic axs,
-                  bool update)
+                  bool update = true)
 {
   for (int i = 0; i < max_custom_roles; ++i)
     {
@@ -271,7 +273,7 @@ get_concrete_axiom_semantic ()
   return flag_contract_assumption_mode ? CCS_ASSUME : CCS_IGNORE;
 }
 
-void
+static void
 setup_default_contract_role (bool update)
 {
   contract_semantic check = get_concrete_check ();
@@ -491,6 +493,14 @@ handle_OPT_fcontract_semantic_ (const char *arg)
   validate_contract_role (role);
 }
 
+/* Returns the default role.  */
+
+static contract_role *
+get_default_contract_role ()
+{
+  return get_contract_role ("default");
+}
+
 /* Convert a contract CONFIG into a contract_mode.  */
 
 static contract_mode
index 7d955f7881d6d1c50f4e28f5841778e164971e13..ead07d19fb7badf3f73030a824c3bdc71a869463 100644 (file)
@@ -131,38 +131,20 @@ struct contract_mode
   } u;
 };
 
-extern contract_role *get_contract_role        (const char *);
-extern contract_role *add_contract_role        (const char *,
-                                        contract_semantic,
-                                        contract_semantic,
-                                        contract_semantic,
-                                        bool = true);
-extern void validate_contract_role     (contract_role *);
-extern void setup_default_contract_role        (bool = true);
-extern contract_semantic lookup_concrete_semantic (const char *);
-
 /* Map a source level semantic or level name to its value, or invalid.  */
 extern contract_semantic map_contract_semantic (const char *);
 extern contract_level map_contract_level       (const char *);
 
 /* Check if an attribute is a cxx contract attribute.  */
-extern bool cxx_contract_attribute_p (const_tree);
-extern bool cp_contract_assertion_p (const_tree);
-
-/* Returns the default role.  */
-
-inline contract_role *
-get_default_contract_role ()
-{
-  return get_contract_role ("default");
-}
+extern bool cxx_contract_attribute_p           (const_tree);
+extern bool cp_contract_assertion_p            (const_tree);
 
 /* Handle various command line arguments related to semantic mapping.  */
-extern void handle_OPT_fcontract_build_level_ (const char *);
+extern void handle_OPT_fcontract_build_level_  (const char *);
 extern void handle_OPT_fcontract_assumption_mode_ (const char *);
 extern void handle_OPT_fcontract_continuation_mode_ (const char *);
-extern void handle_OPT_fcontract_role_ (const char *);
-extern void handle_OPT_fcontract_semantic_ (const char *);
+extern void handle_OPT_fcontract_role_         (const char *);
+extern void handle_OPT_fcontract_semantic_     (const char *);
 
 enum contract_matching_context
 {
@@ -277,29 +259,79 @@ enum contract_matching_context
 #define DECL_IS_POST_FN_P(NODE) \
   (DECL_ABSTRACT_ORIGIN (NODE) && DECL_POST_FN (DECL_ABSTRACT_ORIGIN (NODE)) == NODE)
 
+/* contracts.cc */
+extern void emit_assertion                     (tree);
+
 extern void remove_contract_attributes         (tree);
+extern bool all_attributes_are_contracts_p     (tree);
+extern tree finish_contract_attribute          (tree, tree);
 extern void copy_contract_attributes           (tree, tree);
+extern bool diagnose_misapplied_contracts      (tree);
 extern void remap_contracts                    (tree, tree, tree, bool);
+extern tree splice_out_contracts               (tree);
+extern void inherit_base_contracts             (tree, tree);
+
+extern tree make_postcondition_variable                (cp_expr);
+extern tree make_postcondition_variable                (cp_expr, tree);
 extern void maybe_update_postconditions                (tree);
 extern void rebuild_postconditions             (tree);
 extern bool check_postcondition_result         (tree, tree, location_t);
-extern tree get_precondition_function          (tree);
-extern tree get_postcondition_function         (tree);
+
+extern tree grok_contract                      (tree, tree, tree, cp_expr,
+                                                location_t);
+extern tree finish_contract_condition          (cp_expr);
+extern void update_late_contract               (tree, tree, tree);
+extern tree invalidate_contract                        (tree);
 extern void duplicate_contracts                        (tree, tree);
+
 extern void match_deferred_contracts           (tree);
 extern void defer_guarded_contract_match       (tree, tree, tree);
-extern bool diagnose_misapplied_contracts      (tree);
-extern tree finish_contract_attribute          (tree, tree);
-extern tree invalidate_contract                        (tree);
-extern void update_late_contract               (tree, tree, tree);
-extern tree splice_out_contracts               (tree);
-extern bool all_attributes_are_contracts_p     (tree);
-extern void inherit_base_contracts             (tree, tree);
+
+extern tree get_precondition_function          (tree);
+extern tree get_postcondition_function         (tree);
 extern void start_function_contracts           (tree);
 extern void maybe_apply_function_contracts     (tree);
 extern void finish_function_contracts          (tree);
 extern void set_contract_functions             (tree, tree, tree);
+
 extern tree build_contract_check               (tree);
-extern void emit_assertion                     (tree);
+
+/* Return the first contract in ATTRS, or NULL_TREE if there are none.  */
+
+inline tree
+find_contract (tree attrs)
+{
+  while (attrs && !cxx_contract_attribute_p (attrs))
+    attrs = TREE_CHAIN (attrs);
+  return attrs;
+}
+
+inline void
+set_decl_contracts (tree decl, tree contract_attrs)
+{
+  remove_contract_attributes (decl);
+  DECL_ATTRIBUTES (decl) = chainon (DECL_ATTRIBUTES (decl), contract_attrs);
+}
+
+/* Returns the computed semantic of the node.  */
+
+inline contract_semantic
+get_contract_semantic (const_tree t)
+{
+  return (contract_semantic) (TREE_LANG_FLAG_3 (CONTRACT_CHECK (t))
+      | (TREE_LANG_FLAG_2 (t) << 1)
+      | (TREE_LANG_FLAG_0 ((t)) << 2));
+}
+
+/* Sets the computed semantic of the node.  */
+
+inline void
+set_contract_semantic (tree t, contract_semantic semantic)
+{
+  TREE_LANG_FLAG_3 (CONTRACT_CHECK (t)) = semantic & 0x01;
+  TREE_LANG_FLAG_2 (t) = (semantic & 0x02) >> 1;
+  TREE_LANG_FLAG_0 (t) = (semantic & 0x04) >> 2;
+}
+
 
 #endif /* ! GCC_CP_CONTRACT_H */
index 690e5100707287a02de2b46c95f3ff746a08d080..af1c4bcb9c9dc1887821cbc36c88b4c20c6112ef 100644 (file)
@@ -33,6 +33,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gcc-rich-location.h"
 #include "hash-map.h"
 #include "coroutines.h"
+#include "contracts.h"
 
 /* ================= Debug. ================= */
 
index 4ff8f36a9fb6d32c4c9da3fba2d54cb8dd60ddef..39da4ff236ab10a9923e60c5cf44d50ee489be2c 100644 (file)
@@ -43,6 +43,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "omp-general.h"
 #include "opts.h"
 #include "gcc-urlifier.h"
+#include "contracts.h"
 
 /* Keep track of forward references to immediate-escalating functions in
    case they become consteval.  This vector contains ADDR_EXPRs and
index 55e8e073627222e4f1e3402f09f9ab239a1afb8c..8520ca05556c8f01e18c573f7699b8d97a768c67 100644 (file)
@@ -25,7 +25,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "hard-reg-set.h"
 #include "function.h"
 #include "tristate.h"
-#include "contracts.h"
 
 /* In order for the format checking to accept the C++ front end
    diagnostic framework extensions, you must include this file before
@@ -9110,50 +9109,6 @@ extern tree coro_get_ramp_function               (tree);
 
 extern tree co_await_get_resume_call           (tree await_expr);
 
-
-/* contracts.cc */
-extern tree make_postcondition_variable                (cp_expr);
-extern tree make_postcondition_variable                (cp_expr, tree);
-extern tree grok_contract                      (tree, tree, tree, cp_expr, location_t);
-extern tree finish_contract_condition          (cp_expr);
-
-/* Return the first contract in ATTRS, or NULL_TREE if there are none.  */
-
-inline tree
-find_contract (tree attrs)
-{
-  while (attrs && !cxx_contract_attribute_p (attrs))
-    attrs = TREE_CHAIN (attrs);
-  return attrs;
-}
-
-inline void
-set_decl_contracts (tree decl, tree contract_attrs)
-{
-  remove_contract_attributes (decl);
-  DECL_ATTRIBUTES (decl) = chainon (DECL_ATTRIBUTES (decl), contract_attrs);
-}
-
-/* Returns the computed semantic of the node.  */
-
-inline contract_semantic
-get_contract_semantic (const_tree t)
-{
-  return (contract_semantic) (TREE_LANG_FLAG_3 (CONTRACT_CHECK (t))
-      | (TREE_LANG_FLAG_2 (t) << 1)
-      | (TREE_LANG_FLAG_0 ((t)) << 2));
-}
-
-/* Sets the computed semantic of the node.  */
-
-inline void
-set_contract_semantic (tree t, contract_semantic semantic)
-{
-  TREE_LANG_FLAG_3 (CONTRACT_CHECK (t)) = semantic & 0x01;
-  TREE_LANG_FLAG_2 (t) = (semantic & 0x02) >> 1;
-  TREE_LANG_FLAG_0 (t) = (semantic & 0x04) >> 2;
-}
-
 /* Inline bodies.  */
 
 inline tree
index 4b1a335910b15d295d04efb9c572660630deca29..f088d09f51ebc2c2fa6812fa934b725986ea382e 100644 (file)
@@ -60,6 +60,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "opts.h"
 #include "langhooks-def.h"  /* For lhd_simulate_record_decl  */
 #include "coroutines.h"
+#include "contracts.h"
 #include "gcc-urlifier.h"
 #include "diagnostic-highlight-colors.h"
 #include "pretty-print-markup.h"
index c6c9dfc7723291d3a9041d0b2ebf71dc9d0927e9..6499be1d33b621229ba4bca19d8d02a873d825f6 100644 (file)
@@ -52,6 +52,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "omp-general.h"
 #include "tree-inline.h"
 #include "escaped_string.h"
+#include "contracts.h"
 
 /* Id for dumping the raw trees.  */
 int raw_dump_id;
index f48cb22a3c0401cd99e33b45ae9788efbed63db8..80be40d2dce39db361435809a77d8875ea6160bf 100644 (file)
@@ -55,6 +55,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "stor-layout.h"
 #include "flags.h"
 #include "attribs.h"
+#include "contracts.h"
 
 /* Debugging support.  */
 
index 9412f78ecd9cff1e363d3cde2eaf3ba1b04e820d..0404eae6ce3652bbd46690833f4d529fca7929ed 100644 (file)
@@ -232,6 +232,7 @@ Classes used:
 #include "attribs.h"
 #include "intl.h"
 #include "langhooks.h"
+#include "contracts.h"
 /* This TU doesn't need or want to see the networking.  */
 #define CODY_NETWORKING 0
 #include "mapper-client.h"
index 9b79267d16b3a10c826acc2dea3bbab0e4b4bd61..65de1cff0e64a852f7c41b8e66caf3116c226011 100644 (file)
@@ -49,6 +49,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "builtins.h"
 #include "omp-general.h"
 #include "pretty-print-markup.h"
+#include "contracts.h"
 
 /* The type of functions taking a tree, and some additional data, and
    returning an int.  */
index cea9f7c689ba5e4c5341d28fed7d5de4c8d8d982..77b5b1da527ea10d9305ec6f93292b009ab2f47d 100644 (file)
@@ -31,6 +31,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "stringpool.h"
 #include "attribs.h"
 #include "tree-inline.h"
+#include "contracts.h"
 
 static int is_subobject_of_p (tree, tree);
 static tree dfs_lookup_base (tree, void *);
index 58e6f9461942446e618085428b033dd0a4218585..1937ace70b36847b22ffab135f0a90ef39b4a41e 100644 (file)
@@ -46,6 +46,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "predict.h"
 #include "memmodel.h"
 #include "gimplify.h"
+#include "contracts.h"
 
 /* There routines provide a modular interface to perform many parsing
    operations.  They may therefore be used during actual parsing, or