Skip to content

Commit 7b2eb4b

Browse files
committed
c++, contracts: Some arg list fixes
Update the handling for type args and decl arguments which were not quite right in some cases. Signed-off-by: Iain Sandoe <[email protected]>
1 parent 1c0db04 commit 7b2eb4b

File tree

1 file changed

+55
-66
lines changed

1 file changed

+55
-66
lines changed

gcc/cp/contracts.cc

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,61 +1453,20 @@ set_contract_functions (tree fndecl, tree pre, tree post)
14531453
set_postcondition_function (fndecl, post);
14541454
}
14551455

1456-
/* Return a copy of the function decl FNDECL with its own unshared
1457-
PARM_DECL and DECL_ATTRIBUTEs. */
1458-
1459-
static tree
1460-
copy_fn_decl (tree fndecl)
1461-
{
1462-
gcc_checking_assert (!error_operand_p (fndecl));
1463-
1464-
tree decl = copy_decl (fndecl);
1465-
DECL_ATTRIBUTES (decl) = copy_list (DECL_ATTRIBUTES (fndecl));
1466-
1467-
if (DECL_RESULT (fndecl))
1468-
{
1469-
DECL_RESULT (decl) = copy_decl (DECL_RESULT (fndecl));
1470-
DECL_CONTEXT (DECL_RESULT (decl)) = decl;
1471-
}
1472-
1473-
if (!DECL_ARGUMENTS (fndecl))
1474-
return decl;
1475-
1476-
if (VOID_TYPE_P (DECL_ARGUMENTS (fndecl)))
1477-
{
1478-
DECL_ARGUMENTS (decl) = void_list_node;
1479-
return decl;
1480-
}
1481-
1482-
tree last = DECL_ARGUMENTS (decl) = copy_decl (DECL_ARGUMENTS (decl));
1483-
DECL_CONTEXT (last) = decl;
1484-
for (tree p = TREE_CHAIN (DECL_ARGUMENTS (fndecl)); p; p = TREE_CHAIN (p))
1485-
{
1486-
if (VOID_TYPE_P (p))
1487-
{
1488-
TREE_CHAIN (last) = void_list_node;
1489-
break;
1490-
}
1491-
last = TREE_CHAIN (last) = copy_decl (p);
1492-
DECL_CONTEXT (last) = decl;
1493-
}
1494-
return decl;
1495-
}
1496-
14971456
/* Build a declaration for the pre- or postcondition of a guarded FNDECL. */
14981457

14991458
static tree
15001459
build_contract_condition_function (tree fndecl, bool pre)
15011460
{
1502-
if (TREE_TYPE (fndecl) == error_mark_node)
1461+
if (error_operand_p (fndecl))
15031462
return error_mark_node;
1463+
15041464
if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl)
15051465
&& !TYPE_METHOD_BASETYPE (TREE_TYPE (fndecl)))
15061466
return error_mark_node;
15071467

1508-
/* Create and rename the unchecked function and give an internal name. */
1509-
tree fn = copy_fn_decl (fndecl);
1510-
DECL_RESULT (fn) = NULL_TREE;
1468+
/* Start the copy. */
1469+
tree fn = copy_decl (fndecl);
15111470

15121471
/* Don't propagate declaration attributes to the checking function,
15131472
including the original contracts. */
@@ -1525,12 +1484,11 @@ build_contract_condition_function (tree fndecl, bool pre)
15251484
if (DECL_ATTRIBUTES (fn))
15261485
cplus_decl_attributes (&fn, DECL_ATTRIBUTES (fn), 0);
15271486

1487+
/* FIXME will later optimizations delete unused args to prevent extra arg
1488+
passing? do we care? */
15281489
/* Handle the args list. */
15291490
tree arg_types = NULL_TREE;
15301491
tree *last = &arg_types;
1531-
1532-
/* FIXME will later optimizations delete unused args to prevent extra arg
1533-
passing? do we care? */
15341492
tree class_type = NULL_TREE;
15351493
for (tree arg_type = TYPE_ARG_TYPES (TREE_TYPE (fn));
15361494
arg_type && arg_type != void_list_node;
@@ -1546,6 +1504,20 @@ build_contract_condition_function (tree fndecl, bool pre)
15461504
last = &TREE_CHAIN (*last);
15471505
}
15481506

1507+
/* Copy the function parameters, if present. Disable warnings for them. */
1508+
DECL_ARGUMENTS (fn) = NULL_TREE;
1509+
if (DECL_ARGUMENTS (fndecl))
1510+
{
1511+
tree *last_a = &DECL_ARGUMENTS (fn);
1512+
for (tree p = DECL_ARGUMENTS (fndecl); p; p = TREE_CHAIN (p))
1513+
{
1514+
*last_a = copy_decl (p);
1515+
suppress_warning (*last_a);
1516+
DECL_CONTEXT (*last_a) = fn;
1517+
last_a = &TREE_CHAIN (*last_a);
1518+
}
1519+
}
1520+
15491521
tree orig_fn_value_type = TREE_TYPE (TREE_TYPE (fn));
15501522
if (!pre && !VOID_TYPE_P (orig_fn_value_type))
15511523
{
@@ -1555,19 +1527,23 @@ build_contract_condition_function (tree fndecl, bool pre)
15551527
tree parm = build_lang_decl (PARM_DECL, name, orig_fn_value_type);
15561528
DECL_CONTEXT (parm) = fn;
15571529
DECL_ARTIFICIAL (parm) = true;
1530+
suppress_warning (parm);
15581531
DECL_ARGUMENTS (fn) = chainon (DECL_ARGUMENTS (fn), parm);
15591532
*last = build_tree_list (NULL_TREE, orig_fn_value_type);
15601533
last = &TREE_CHAIN (*last);
15611534
}
1535+
15621536
*last = void_list_node;
1563-
/* The handlers are void fns. */
1564-
TREE_TYPE (fn) = build_function_type (void_type_node, arg_types);
15651537

1566-
/* Disable warnings for all the parameters */
1567-
for (tree p = DECL_ARGUMENTS (fn); p && p!=void_list_node;
1568-
p = TREE_CHAIN (p))
1569-
suppress_warning (p);
1538+
/* The handlers are void fns. */
1539+
tree resdecl = build_decl (DECL_SOURCE_LOCATION (fndecl), RESULT_DECL,
1540+
0, void_type_node);
1541+
DECL_CONTEXT (resdecl) = fn;
1542+
DECL_ARTIFICIAL (resdecl) = true;
1543+
DECL_IGNORED_P (resdecl) = true;
1544+
DECL_RESULT (fn) = resdecl;
15701545

1546+
TREE_TYPE (fn) = build_function_type (void_type_node, arg_types);
15711547
if (DECL_IOBJ_MEMBER_FUNCTION_P (fndecl))
15721548
TREE_TYPE (fn) = build_method_type (class_type, TREE_TYPE (fn));
15731549

@@ -1898,9 +1874,22 @@ build_contract_wrapper_function (tree fndecl, bool is_cvh,
18981874

18991875
location_t loc = DECL_SOURCE_LOCATION (fndecl);
19001876

1901-
tree wrapper_return_type = TREE_TYPE (TREE_TYPE (fndecl));
1902-
tree wrapper_args = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1877+
/* Handle the arg types list. */
1878+
tree wrapper_args = NULL_TREE;
1879+
tree *last = &wrapper_args;
1880+
for (tree arg_type = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); arg_type;
1881+
arg_type = TREE_CHAIN (arg_type))
1882+
{
1883+
if (arg_type == void_list_node)
1884+
{
1885+
*last = void_list_node;
1886+
break;
1887+
}
1888+
*last = build_tree_list (TREE_PURPOSE (arg_type), TREE_VALUE (arg_type));
1889+
last = &TREE_CHAIN (*last);
1890+
}
19031891

1892+
tree wrapper_return_type = copy_node (TREE_TYPE (TREE_TYPE (fndecl)));
19041893
tree wrapper_type = build_function_type (wrapper_return_type, wrapper_args);
19051894

19061895
/* Contract violation wrapper function is always noexcept. Otherwise,
@@ -1930,19 +1919,19 @@ build_contract_wrapper_function (tree fndecl, bool is_cvh,
19301919
DECL_IGNORED_P (resdecl) = true;
19311920
DECL_RESULT (wrapdecl) = resdecl;
19321921

1933-
/* Copy the function parameters. */
1934-
tree last = DECL_ARGUMENTS (wrapdecl) = copy_decl (DECL_ARGUMENTS (fndecl));
1935-
DECL_CONTEXT (last) = wrapdecl;
1936-
for (tree p = TREE_CHAIN (DECL_ARGUMENTS (fndecl)); p; p = TREE_CHAIN (p))
1922+
/* Copy the function parameters, if present. Suppress (e.g. unused)
1923+
warnings on them. */
1924+
DECL_ARGUMENTS (wrapdecl) = NULL_TREE;
1925+
if (tree p = DECL_ARGUMENTS (fndecl))
19371926
{
1938-
if (VOID_TYPE_P (p))
1927+
tree *last_a = &DECL_ARGUMENTS (wrapdecl);
1928+
for (; p; p = TREE_CHAIN (p))
19391929
{
1940-
TREE_CHAIN (last) = void_list_node;
1941-
break;
1930+
*last_a = copy_decl (p);
1931+
suppress_warning (*last_a);
1932+
DECL_CONTEXT (*last_a) = wrapdecl;
1933+
last_a = &TREE_CHAIN (*last_a);
19421934
}
1943-
suppress_warning (p);
1944-
last = TREE_CHAIN (last) = copy_decl (p);
1945-
DECL_CONTEXT (last) = wrapdecl;
19461935
}
19471936

19481937
/* Copy selected attributes from the original function. */

0 commit comments

Comments
 (0)