Skip to content

Commit

Permalink
Fix context of some nested aggregates
Browse files Browse the repository at this point in the history
The context for instances of aggregates *not* nested in the current
function, but some parent. Fixes ldc-developers#2960.
  • Loading branch information
kinke committed Jan 19, 2019
1 parent f5a5324 commit 14c716a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
14 changes: 2 additions & 12 deletions gen/nested.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,8 @@ LLValue *DtoNestedContext(Loc &loc, Dsymbol *sym) {
return llvm::ConstantPointerNull::get(getVoidPtrType());
}

FuncDeclaration *frameToPass = nullptr;
if (AggregateDeclaration *ad = sym->isAggregateDeclaration()) {
// If sym is a nested struct or a nested class, pass the frame
// of the function where sym is declared.
frameToPass = ad->toParent()->isFuncDeclaration();
} else if (FuncDeclaration *symfd = sym->isFuncDeclaration()) {
// If sym is a nested function, and its parent context is different
// than the one we got, adjust it.
frameToPass = getParentFunc(symfd);
}

if (frameToPass) {
// The symbol may need a parent context of the current function.
if (FuncDeclaration *frameToPass = getParentFunc(sym)) {
IF_LOG Logger::println("Parent frame is from %s", frameToPass->toChars());
FuncDeclaration *ctxfd = irFunc.decl;
IF_LOG Logger::println("Current function is %s", ctxfd->toChars());
Expand Down
36 changes: 36 additions & 0 deletions tests/codegen/nested_gh2960.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %ldc -run %s

template listDir(alias handler)
{
struct NestedStruct
{
void callHandler() { handler(); }
}

class NestedClass
{
void callHandler() { handler(); }
}

void nestedFunc() { handler(); }

void listDir()
{
int a = 123;
void foo() { assert(a == 123); }

// pass local listDir() frame as context
foo();

// pass parent context for sibling symbols:
NestedStruct().callHandler();
(new NestedClass).callHandler();
nestedFunc();
}
}

void main()
{
int magic = 0xDEADBEEF;
listDir!(() { assert(magic == 0xDEADBEEF); })();
}

0 comments on commit 14c716a

Please sign in to comment.