Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traverse full chain of nested aggregates when resolving a nested variable #3558

Merged
merged 1 commit into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions gen/nested.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ DValue *DtoNestedVariable(Loc &loc, Type *astype, VarDeclaration *vd,
if (currentCtx) {
Logger::println("Using own nested context of current function");
ctx = currentCtx;
} else if (irfunc->decl->isMember2()) {
} else if (AggregateDeclaration *ad = irfunc->decl->isMember2()) {
Logger::println(
"Current function is member of nested class, loading vthis");

AggregateDeclaration *cd = irfunc->decl->isMember2();
LLValue *val = irfunc->thisArg;
if (cd->isClassDeclaration()) {
val = DtoLoad(val);
LLValue *val =
ad->isClassDeclaration() ? DtoLoad(irfunc->thisArg) : irfunc->thisArg;
for (; ad; ad = ad->toParent2()->isAggregateDeclaration()) {
assert(ad->vthis);
val = DtoLoad(DtoGEP(val, 0, getVthisIdx(ad), ".vthis"));
}
ctx = DtoLoad(DtoGEP(val, 0, getVthisIdx(cd), ".vthis"));
ctx = val;
skipDIDeclaration = true;
} else {
Logger::println("Regular nested function, using context arg");
Expand Down Expand Up @@ -455,17 +455,13 @@ void DtoCreateNestedContext(FuncGenState &funcGen) {
LLValue *src = irFunc.nestArg;
if (!src) {
assert(irFunc.thisArg);
assert(fd->isMember2());
LLValue *thisval = DtoLoad(irFunc.thisArg);
AggregateDeclaration *cd = fd->isMember2();
assert(cd);
assert(cd->vthis);
AggregateDeclaration *ad = fd->isMember2();
assert(ad);
assert(ad->vthis);
LLValue *thisptr =
ad->isClassDeclaration() ? DtoLoad(irFunc.thisArg) : irFunc.thisArg;
IF_LOG Logger::println("Indexing to 'this'");
if (cd->isStructDeclaration()) {
src = DtoExtractValue(thisval, getVthisIdx(cd), ".vthis");
} else {
src = DtoLoad(DtoGEP(thisval, 0, getVthisIdx(cd), ".vthis"));
}
src = DtoLoad(DtoGEP(thisptr, 0, getVthisIdx(ad), ".vthis"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated but seemed really ugly - loading the whole struct, just to extract the vthis part, instead of loading the vthis part only.

}
if (depth > 1) {
src = DtoBitCast(src, getVoidPtrType());
Expand Down
51 changes: 51 additions & 0 deletions tests/codegen/nested_gh3556.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// https://github.com/ldc-developers/ldc/issues/3556
// RUN: %ldc -run %s

class C {
int counter = 1;

void test1() {
assert(counter == 1);
++counter;
}

void run1() {
class C2 {
int counter2 = 11;

class C3 {
void run3() {
test1();
test2();
++counter;
++counter2;
}
}

void test2() {
assert(counter == 2);
++counter;
assert(counter2 == 11);
++counter2;
}

void run2() {
auto c3 = new C3;
c3.run3();
++counter;
++counter2;
}
}

auto c2 = new C2;
c2.run2();
assert(c2.counter2 == 14);
++counter;
}
}

void main() {
auto c = new C;
c.run1();
assert(c.counter == 6);
}