Skip to content

Commit 69d1f4f

Browse files
Momen-G-Ardaveedvdv-nvidia
authored andcommitted
Bind an NSDMI's implicit this to the constructed object in a constructor init [GH #324]
When a default member initializer that aggregate-initializes another object of the same class was used by a constructor, lower_param_ref searched every aggregate on aggregate_this_stack, including the ones skipped because one of their explicit elements was being lowered, before it tried the "this" parameter of the constructor init: struct Y { unsigned k = 7; unsigned n = Y{2, k}.n; }; struct D : Y { D() {} }; // D().n was 2, not 7. The "this" parameter of the constructor init is now used before the skipped aggregates are searched.
1 parent 8433d66 commit 69d1f4f

18 files changed

Lines changed: 203 additions & 16 deletions

‎src/lower_il.c‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16074,32 +16074,41 @@ Several nested aggregates can have the type of "this", as in:
1607416074

1607516075
where "k" refers to the Y being initialized, not to the temporary
1607616076
initialized by "Y{2, k}". The aggregates for which an explicitly specified
16077-
element is being lowered are therefore skipped.
16077+
element is being lowered are therefore skipped. When no other aggregate
16078+
matches, the "this" parameter of a constructor init comes next, as in:
16079+
16080+
struct D : Y { D() {} };
16081+
16082+
where the default member initializer of Y::n is lowered by the constructor
16083+
of Y, and "k" refers to the object that constructor initializes. Only
16084+
then are the skipped aggregates searched.
1607816085
*/
1607916086
{
1608016087
a_boolean found = FALSE;
1608116088

1608216089
check_assertion(expr->kind == (an_expr_node_kind)enk_param_ref &&
1608316090
expr->variant.param_ref.param_num == 0);
1608416091
if (aggregate_this_stack != NULL) {
16085-
/* We must be lowering an aggregate constant. If no candidate aggregate
16086-
matches, fall back on all the aggregates on the stack. */
16092+
/* We must be lowering an aggregate constant. Look for a candidate
16093+
aggregate. */
16094+
found = replace_param_ref_with_aggregate_this(expr,
16095+
/*candidates_only=*/TRUE);
16096+
} /* if */
16097+
if (!found && ctor_init_this != NULL &&
16098+
identical_types_ignoring_qualifiers(ctor_init_this->type, expr->type)) {
16099+
/* We're lowering a constructor init; use the "this" parameter for
16100+
the function. */
16101+
set_expr_node_kind(expr, enk_variable);
16102+
node_variable(expr) = ctor_init_this;
16103+
found = TRUE;
16104+
} /* if */
16105+
if (!found && aggregate_this_stack != NULL) {
16106+
/* Fall back on all the aggregates on the stack. */
1608716107
found = replace_param_ref_with_aggregate_this(expr,
16088-
/*candidates_only=*/TRUE) ||
16089-
replace_param_ref_with_aggregate_this(expr,
1609016108
/*candidates_only=*/FALSE);
1609116109
} /* if */
1609216110
if (!found) {
16093-
if (ctor_init_this != NULL) {
16094-
/* We're lowering a constructor init; use the "this" parameter for
16095-
the function. */
16096-
check_assertion(identical_types_ignoring_qualifiers(ctor_init_this->type,
16097-
expr->type));
16098-
set_expr_node_kind(expr, (an_expr_node_kind)enk_variable);
16099-
node_variable(expr) = ctor_init_this;
16100-
} else {
16101-
unexpected_condition();
16102-
} /* if */
16111+
unexpected_condition();
1610316112
} /* if */
1610416113
} /* lower_param_ref */
1610516114

‎src/lower_init.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6773,7 +6773,8 @@ dealt with).
67736773
initializer (i.e., it is implicit); an explicitly specified element
67746774
refers to the "this" of the enclosing context, as in:
67756775
struct Y { unsigned k; unsigned n = Y{2, k}.n; };
6776-
where "k" in "Y{2, k}" designates the outer Y, not the temporary.
6776+
where "k" in "Y{2, k}" designates the outer Y, not the temporary;
6777+
the outer Y may also be the object of a constructor init.
67776778
The default member initializers of the members of an anonymous
67786779
union refer to the enclosing class, so an anonymous union does not
67796780
affect the search. */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[GH #324]
2+
Implicit "this" in a default member initializer used by a constructor
3+
4+
The changes for GH #408 did not cover the case where a default member
5+
initializer that contains an aggregate initialization of another object of
6+
the same class is used by a constructor (an implicitly-defined default
7+
constructor, possibly called for a base class or a member). An implicit
8+
use of "this" in the elements of the nested initialization still referred
9+
to the nested object instead of to the object being constructed. For
10+
example:
11+
12+
struct Y { unsigned k = 7; unsigned n = Y{2, k}.n; };
13+
struct D : Y { D() {} };
14+
unsigned n = D().n; // n is now 7 (previously 2).
15+
16+
This is now fixed.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eccp -DTEST_NUMBER=1 --c++14 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eccp -DTEST_NUMBER=2 --c++17 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eccp -DTEST_NUMBER=3 --c++20 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eccp -DTEST_NUMBER=4 --c++20 --g++ --gnu_version=150000 Test_name.c

0 commit comments

Comments
 (0)