|| TREE_CODE (TREE_VALUE (TREE_VALUE (attr))) == ASSERTION_STMT);
}
+/* True if ATTR is an assertion. */
+
+bool
+cp_contract_assertion_p (const_tree attr)
+{
+ /* This is only an assertion if it is a valid cxx contract attribute and the
+ statement is an ASSERTION_STMT. */
+ return cxx_contract_attribute_p (attr)
+ && TREE_CODE (CONTRACT_STATEMENT (attr)) == ASSERTION_STMT;
+}
+
/* Remove all c++2a style contract attributes from the DECL_ATTRIBUTEs of the
FUNCTION_DECL FNDECL. */
}
}
+/* Replace the any contract attributes on OVERRIDER with a copy where any
+ references to BASEFN's PARM_DECLs have been rewritten to the corresponding
+ PARM_DECL in OVERRIDER. */
+
+void
+inherit_base_contracts (tree overrider, tree basefn)
+{
+ tree last = NULL_TREE, contract_attrs = NULL_TREE;
+ for (tree a = DECL_CONTRACTS (basefn);
+ a != NULL_TREE;
+ a = CONTRACT_CHAIN (a))
+ {
+ tree c = copy_node (a);
+ TREE_VALUE (c) = build_tree_list (TREE_PURPOSE (TREE_VALUE (c)),
+ copy_node (CONTRACT_STATEMENT (c)));
+
+ tree src = basefn;
+ tree dst = overrider;
+ remap_contract (src, dst, CONTRACT_STATEMENT (c), /*duplicate_p=*/true);
+
+ CONTRACT_COMMENT (CONTRACT_STATEMENT (c)) =
+ copy_node (CONTRACT_COMMENT (CONTRACT_STATEMENT (c)));
+
+ chainon (last, c);
+ last = c;
+ if (!contract_attrs)
+ contract_attrs = c;
+ }
+
+ set_decl_contracts (overrider, contract_attrs);
+}
+
#include "gt-cp-contracts.h"
/* Definitions for C++ contract levels. Implements functionality described in
the N4820 working draft version of contracts, P1290, P1332, and P1429.
- Copyright (C) 2020 Free Software Foundation, Inc.
+ Copyright (C) 2020-2022 Free Software Foundation, Inc.
Contributed by Jeff Chapman II (jchapman@lock3software.com)
This file is part of GCC.
/* 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. */
extern void handle_OPT_fcontract_role_ (const char *);
extern void handle_OPT_fcontract_semantic_ (const char *);
+enum contract_matching_context
+{
+ cmc_declaration,
+ cmc_override
+};
+
+/* True iff the FUNCTION_DECL NODE currently has any contracts. */
+#define DECL_HAS_CONTRACTS_P(NODE) \
+ (DECL_CONTRACTS (NODE) != NULL_TREE)
+
+/* For a FUNCTION_DECL of a guarded function, this points to a list of the pre
+ and post contracts of the first decl of NODE in original order. */
+#define DECL_CONTRACTS(NODE) \
+ (find_contract (DECL_ATTRIBUTES (NODE)))
+
+/* The next contract (if any) after this one in an attribute list. */
+#define CONTRACT_CHAIN(NODE) \
+ (find_contract (TREE_CHAIN (NODE)))
+
+/* The wrapper of the original source location of a list of contracts. */
+#define CONTRACT_SOURCE_LOCATION_WRAPPER(NODE) \
+ (TREE_PURPOSE (TREE_VALUE (NODE)))
+
+/* The original source location of a list of contracts. */
+#define CONTRACT_SOURCE_LOCATION(NODE) \
+ (EXPR_LOCATION (CONTRACT_SOURCE_LOCATION_WRAPPER (NODE)))
+
+/* The actual code _STMT for a contract attribute. */
+#define CONTRACT_STATEMENT(NODE) \
+ (TREE_VALUE (TREE_VALUE (NODE)))
+
+/* For a FUNCTION_DECL of a guarded function, this holds the function decl
+ where pre contract checks are emitted. */
+#define DECL_PRE_FN(NODE) \
+ (get_precondition_function ((NODE)))
+
+/* For a FUNCTION_DECL of a guarded function, this holds the function decl
+ where post contract checks are emitted. */
+#define DECL_POST_FN(NODE) \
+ (get_postcondition_function ((NODE)))
+
+/* For a FUNCTION_DECL of a pre/post function, this points back to the
+ original guarded function. */
+#define DECL_ORIGINAL_FN(NODE) \
+ (DECL_ABSTRACT_ORIGIN (NODE))
+
+/* True iff the FUNCTION_DECL is the pre function for a guarded function. */
+#define DECL_IS_PRE_FN_P(NODE) \
+ (DECL_ORIGINAL_FN (NODE) && DECL_PRE_FN (DECL_ORIGINAL_FN (NODE)) == NODE)
+
+/* True iff the FUNCTION_DECL is the post function for a guarded function. */
+#define DECL_IS_POST_FN_P(NODE) \
+ (DECL_ORIGINAL_FN (NODE) && DECL_POST_FN (DECL_ORIGINAL_FN (NODE)) == NODE)
+
+extern tree invalidate_contract (tree);
+extern tree finish_contract_attribute (tree, tree);
+extern void update_late_contract (tree, tree, tree);
+extern void remove_contract_attributes (tree);
+extern void copy_contract_attributes (tree, tree);
+extern tree splice_out_contracts (tree);
+extern bool check_postcondition_result (tree, tree, location_t);
+extern void rebuild_postconditions (tree);
+extern bool match_contract_conditions (location_t, tree, location_t, tree, contract_matching_context);
+extern void defer_guarded_contract_match (tree, tree, tree);
+extern void match_deferred_contracts (tree);
+extern void remap_contract (tree, tree, tree, bool);
+extern void remap_contracts (tree, tree, tree, bool);
+extern void remap_dummy_this (tree, tree *);
+extern bool contract_active_p (tree);
+extern bool contract_any_active_p (tree);
+extern bool contract_any_deferred_p (tree);
+extern bool all_attributes_are_contracts_p (tree);
+extern void build_contract_function_decls (tree);
+extern void set_contract_functions (tree, tree, tree);
+extern tree start_postcondition_statement ();
+extern void finish_postcondition_statement (tree);
+extern tree build_contract_check (tree);
+extern tree get_postcondition_result_parameter (tree);
+extern tree get_precondition_function (tree);
+extern tree get_postcondition_function (tree);
+extern tree get_contracts_original_fn (tree);
+extern void emit_assertion (tree);
+extern void emit_preconditions (tree);
+extern void emit_postconditions_cleanup (tree);
+extern void maybe_update_postconditions (tree);
+extern void start_function_contracts (tree);
+extern void finish_function_contracts (tree);
+extern tree apply_postcondition_to_return (tree);
+extern void duplicate_contracts (tree, tree);
+extern void inherit_base_contracts (tree, tree);
+
#endif /* ! GCC_CP_CONTRACT_H */
#define DECL_PENDING_INLINE_INFO(NODE) \
(LANG_DECL_FN_CHECK (NODE)->u.pending_inline_info)
-/* 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;
-}
-
-/* True iff the FUNCTION_DECL NODE currently has any contracts. */
-#define DECL_HAS_CONTRACTS_P(NODE) \
- (DECL_CONTRACTS (NODE) != NULL_TREE)
-
-/* For a FUNCTION_DECL of a guarded function, this points to a list of the pre
- and post contracts of the first decl of NODE in original order. */
-#define DECL_CONTRACTS(NODE) \
- (find_contract (DECL_ATTRIBUTES (NODE)))
-
-/* The next contract (if any) after this one in an attribute list. */
-#define CONTRACT_CHAIN(NODE) \
- (find_contract (TREE_CHAIN (NODE)))
-
-/* The wrapper of the original source location of a list of contracts. */
-#define CONTRACT_SOURCE_LOCATION_WRAPPER(NODE) \
- (TREE_PURPOSE (TREE_VALUE (NODE)))
-
-/* The original source location of a list of contracts. */
-#define CONTRACT_SOURCE_LOCATION(NODE) \
- (EXPR_LOCATION (CONTRACT_SOURCE_LOCATION_WRAPPER (NODE)))
-
-/* The actual code _STMT for a contract attribute. */
-#define CONTRACT_STATEMENT(NODE) \
- (TREE_VALUE (TREE_VALUE (NODE)))
-
-/* For a FUNCTION_DECL of a guarded function, this holds the function decl
- where pre contract checks are emitted. */
-#define DECL_PRE_FN(NODE) \
- (get_precondition_function ((NODE)))
-
-/* For a FUNCTION_DECL of a guarded function, this holds the function decl
- where post contract checks are emitted. */
-#define DECL_POST_FN(NODE) \
- (get_postcondition_function ((NODE)))
-
-/* For a FUNCTION_DECL of a pre/post function, this points back to the
- original guarded function. */
-#define DECL_ORIGINAL_FN(NODE) \
- (DECL_ABSTRACT_ORIGIN (NODE))
-
-/* True iff the FUNCTION_DECL is the pre function for a guarded function. */
-#define DECL_IS_PRE_FN_P(NODE) \
- (DECL_ORIGINAL_FN (NODE) && DECL_PRE_FN (DECL_ORIGINAL_FN (NODE)) == NODE)
-
-/* True iff the FUNCTION_DECL is the post function for a guarded function. */
-#define DECL_IS_POST_FN_P(NODE) \
- (DECL_ORIGINAL_FN (NODE) && DECL_POST_FN (DECL_ORIGINAL_FN (NODE)) == NODE)
-
/* Nonzero for TYPE_DECL means that it was written 'using name = type'. */
#define TYPE_DECL_ALIAS_P(NODE) \
DECL_LANG_FLAG_6 (TYPE_DECL_CHECK (NODE))
extern tree build_cleanup (tree);
extern tree build_offset_ref_call_from_tree (tree, vec<tree, va_gc> **,
tsubst_flags_t);
-extern bool cp_tree_defined_p (tree);
extern bool decl_defined_p (tree);
extern bool decl_constant_var_p (tree);
extern bool decl_maybe_constant_var_p (tree);
extern void maybe_show_extern_c_location (void);
extern bool literal_integer_zerop (const_tree);
extern tree attr_chainon (tree, tree);
-extern bool function_declarator_p (const cp_declarator *);
-extern const cp_declarator *find_innermost_function_declarator (const cp_declarator *);
/* in pt.cc */
extern tree canonical_type_parameter (tree);
tsubst_flags_t,
access_failure_info *afi = NULL);
-/* contracts.cc */
-enum contract_matching_context
-{
- cmc_declaration,
- cmc_override
-};
-
-extern void init_contract_processing ();
-extern tree invalidate_contract (tree);
-extern tree make_postcondition_variable (cp_expr);
-extern tree make_postcondition_variable (cp_expr, tree);
-extern bool check_postcondition_result (tree, tree, location_t);
-extern void rebuild_postconditions (tree);
-extern tree grok_contract (tree, tree, tree, cp_expr, location_t);
-extern tree finish_contract_attribute (tree, tree);
-extern void update_late_contract (tree, tree, tree);
-extern tree finish_contract_condition (cp_expr);
-extern void remove_contract_attributes (tree);
-extern void copy_contract_attributes (tree, tree);
-extern tree splice_out_contracts (tree);
-extern bool match_contract_conditions (location_t, tree, location_t, tree, contract_matching_context);
-extern void defer_guarded_contract_match (tree, tree, tree);
-extern void match_deferred_contracts (tree);
-extern void remap_contract (tree, tree, tree, bool);
-extern void remap_contracts (tree, tree, tree, bool);
-extern void remap_dummy_this (tree, tree *);
-extern bool contract_active_p (tree);
-extern bool contract_any_active_p (tree);
-extern bool contract_any_deferred_p (tree);
-extern bool all_attributes_are_contracts_p (tree);
-extern void build_contract_function_decls (tree);
-extern void set_contract_functions (tree, tree, tree);
-extern tree start_postcondition_statement ();
-extern void finish_postcondition_statement (tree);
-extern tree build_contract_check (tree);
-extern tree get_postcondition_result_parameter (tree);
-extern tree get_precondition_function (tree);
-extern tree get_postcondition_function (tree);
-extern tree get_contracts_original_fn (tree);
-
-extern void emit_assertion (tree);
-extern void emit_preconditions (tree);
-extern void emit_postconditions_cleanup (tree);
-extern void maybe_update_postconditions (tree);
-extern void start_function_contracts (tree);
-extern void finish_function_contracts (tree);
-extern tree apply_postcondition_to_return (tree);
-extern void duplicate_contracts (tree, tree);
-
-inline void
-set_decl_contracts (tree decl, tree contract_attrs)
-{
- remove_contract_attributes (decl);
- DECL_ATTRIBUTES (decl) = chainon (DECL_ATTRIBUTES (decl), contract_attrs);
-}
-
/* RAII sentinel to ensures that deferred access checks are popped before
a function returns. */
extern tree coro_get_destroy_function (tree);
extern tree coro_get_ramp_function (tree);
+/* 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);
+}
+
/* Inline bodies. */
inline tree
inner_declarator = declarator->declarator;
/* Check that contracts aren't misapplied. */
- tree contract_attr = find_contract (declarator->std_attributes);
- if (contract_attr && !function_declarator_p (declarator))
- diagnose_misapplied_contracts (contract_attr);
+ if (tree contract_attr = find_contract (declarator->std_attributes))
+ if (declarator->kind != cdk_function
+ || innermost_code != cdk_function)
+ diagnose_misapplied_contracts (contract_attr);
/* We don't want to warn in parameter context because we don't
yet know if the parse will succeed, and this might turn out
}
/* Contract attributes appertain to the declaration. */
- tree *p;
- for (p = &attrs; *p;)
+ for (tree *p = &attrs; *p;)
{
tree l = *p;
if (cxx_contract_attribute_p (l))
collect_ada_namespace (decl, source_file);
}
-/* cp_tree_defined_p helper -- returns TP if TP is undefined. */
-
-static tree
-cp_tree_defined_p_r (tree *tp, int *, void *)
-{
- enum tree_code code = TREE_CODE (*tp);
- if ((code == FUNCTION_DECL || code == VAR_DECL)
- && !decl_defined_p (*tp))
- return *tp;
- return NULL_TREE;
-}
-
-/* Returns true iff there is a definition for all entities referenced in the
- tree TP. */
-
-bool
-cp_tree_defined_p (tree tp)
-{
- tree undefined_t = walk_tree (&tp, cp_tree_defined_p_r, NULL, NULL);
- return !undefined_t;
-}
-
/* Returns true iff there is a definition available for variable or
function DECL. */
tree_node (contract);
}
- /* Write a reference to contracts pre/post functions, if any to avoid
+ /* Write a reference to contracts pre/post functions, if any, to avoid
regenerating them in importers. */
tree_node (DECL_PRE_FN (fn));
tree_node (DECL_POST_FN (fn));
parser->oacc_routine = NULL;
}
}
-
-/* True if ATTR is an assertion. */
-
-bool
-cp_contract_assertion_p (const_tree attr)
-{
- /* This is only an assertion if it is a valid cxx contract attribute and the
- statement is an ASSERTION_STMT. */
- return cxx_contract_attribute_p (attr)
- && TREE_CODE (CONTRACT_STATEMENT (attr)) == ASSERTION_STMT;
-}
\f
/* Decl-specifiers. */
return parameter;
}
-/* Returns a pointer to the function declarator iff DECLARATOR is a
- declaration for a function. Returns NULL otherwise. */
+/* Returns true iff DECLARATOR is a declaration for a function. */
-const cp_declarator *
-find_innermost_function_declarator (const cp_declarator *declarator)
+static bool
+function_declarator_p (const cp_declarator *declarator)
{
while (declarator)
{
if (declarator->kind == cdk_function
&& declarator->declarator->kind == cdk_id)
- return declarator;
+ return true;
if (declarator->kind == cdk_id
|| declarator->kind == cdk_decomp
|| declarator->kind == cdk_error)
- return NULL;
+ return false;
declarator = declarator->declarator;
}
- return NULL;
-}
-
-/* Returns true iff DECLARATOR is a declaration for a function. */
-
-bool
-function_declarator_p (const cp_declarator *declarator)
-{
- return find_innermost_function_declarator (declarator) != NULL;
+ return false;
}
/* The parser. */
cp_parser_ctor_initializer_opt_and_function_body
(parser, /*in_function_try_block=*/false);
- fn = current_function_decl;
-
/* Finish the function. */
fn = finish_function (inline_p);
if (UNPARSED_NOEXCEPT_SPEC_P (spec))
vec_safe_push (unparsed_noexcepts, decl);
- /* If a member function has attributes, they are all deferred. */
+ /* Contracts are deferred. */
for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
- {
- if (cxx_contract_attribute_p (attr))
- {
- vec_safe_push (unparsed_contracts, decl);
- break;
- }
- }
+ if (cxx_contract_attribute_p (attr))
+ {
+ vec_safe_push (unparsed_contracts, decl);
+ break;
+ }
}
/* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
return true;
}
-/* Replace the any contract attributes on OVERRIDER with a copy where any
- references to BASEFN's PARM_DECLs have been rewritten to the corresponding
- PARM_DECL in OVERRIDER. */
-
-static void
-inherit_base_contracts (tree overrider, tree basefn)
-{
- tree last = NULL_TREE, contract_attrs = NULL_TREE;
- for (tree a = DECL_CONTRACTS (basefn);
- a != NULL_TREE;
- a = CONTRACT_CHAIN (a))
- {
- tree c = copy_node (a);
- TREE_VALUE (c) = build_tree_list (TREE_PURPOSE (TREE_VALUE (c)),
- copy_node (CONTRACT_STATEMENT (c)));
-
- tree src = basefn;
- tree dst = overrider;
- remap_contract (src, dst, CONTRACT_STATEMENT (c), /*duplicate_p=*/true);
-
- CONTRACT_COMMENT (CONTRACT_STATEMENT (c)) =
- copy_node (CONTRACT_COMMENT (CONTRACT_STATEMENT (c)));
-
- chainon (last, c);
- last = c;
- if (!contract_attrs)
- contract_attrs = c;
- }
-
- set_decl_contracts (overrider, contract_attrs);
-}
-
/* Check that virtual overrider OVERRIDER is acceptable for base function
BASEFN. Issue diagnostic, and return zero, if unacceptable. */
if (line.length () < 1 && (l != expstart.line && l != expend.line))
continue;
- /* for the first line in the range, only start at expstart.column */
+ /* For the first line in the range, only start at expstart.column */
if (l == expstart.line)
{
if (expstart.column == 0)
line = line.subspan (expstart.column - 1,
line.length() - expstart.column + 1);
}
- /* for the last line, don't go past expstart.column */
+ /* For the last line, don't go past expend.column */
else if (l == expend.line)
{
if (line.length () < (size_t)expend.column)
obstack_grow (&buf_obstack, line.get_buffer (), line.length ());
}
- /* Null terminate and finish the buf obstack. */
+ /* NUL-terminate and finish the buf obstack. */
obstack_1grow (&buf_obstack, 0);
const char *buf = (const char *) obstack_finish (&buf_obstack);