Skip to content

Commit

Permalink
IRGen: fix generation of duplicate global variables
Browse files Browse the repository at this point in the history
Unfortunately I couldn't find a test case to reproduce this…
  • Loading branch information
emlai committed Oct 23, 2017
1 parent 22b5d87 commit 9423998
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/irgen/irgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ llvm::Value* IRGenerator::findValue(llvm::StringRef name, const Decl* decl) {

switch (decl->getKind()) {
case DeclKind::VarDecl:
codegenVarDecl(*llvm::cast<VarDecl>(decl));
return globalScope().getLocalValues().find(llvm::cast<VarDecl>(decl)->getName())->second;
return codegenVarDecl(*llvm::cast<VarDecl>(decl));

case DeclKind::FieldDecl:
return codegenMemberAccess(findValue("this", nullptr),
Expand Down Expand Up @@ -656,8 +655,11 @@ llvm::StructType* IRGenerator::codegenTypeDecl(const TypeDecl& decl) {
return structType;
}

void IRGenerator::codegenVarDecl(const VarDecl& decl) {
if (globalScope().getLocalValues().find(decl.getName()) != globalScope().getLocalValues().end()) return;
llvm::Value* IRGenerator::codegenVarDecl(const VarDecl& decl) {
if (auto* value = module.getGlobalVariable(decl.getName(), true)) return value;

auto it = globalScope().getLocalValues().find(decl.getName());
if (it != globalScope().getLocalValues().end()) return it->second;

llvm::Value* value = decl.getInitializer() ? codegenExpr(*decl.getInitializer()) : nullptr;

Expand All @@ -669,6 +671,7 @@ void IRGenerator::codegenVarDecl(const VarDecl& decl) {
}

globalScope().addLocalValue(decl.getName(), value);
return value;
}

void IRGenerator::codegenDecl(const Decl& decl) {
Expand Down
2 changes: 1 addition & 1 deletion src/irgen/irgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class IRGenerator {
void codegenFunctionDecl(const FunctionDecl& decl);
void codegenInitDecl(const InitDecl& decl);
llvm::StructType* codegenTypeDecl(const TypeDecl& decl);
void codegenVarDecl(const VarDecl& decl);
llvm::Value* codegenVarDecl(const VarDecl& decl);

llvm::Value* getFunctionForCall(const CallExpr& call);
llvm::Function* getFunctionProto(const FunctionDecl& decl, Type receiverType = nullptr,
Expand Down

0 comments on commit 9423998

Please sign in to comment.