Skip to content

Commit

Permalink
fix: Fixed not finding functions when called inside methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 24, 2022
1 parent 9c1f6a7 commit 43386b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ void Codegen::visit(FuncDecl *node) {
for (const auto &param: node->getParameters())
paramTypes.push_back(visit(param.second));

auto name = getMangledName(node->getSpan(), node->getName(), paramTypes);
auto name = getMangledName(node->getSpan(), node->getName(), paramTypes, classSymbol != nullptr);
auto linkage = Function::ExternalLinkage;

FunctionType *FT = FunctionType::get(visit(node->getReturnType()), paramTypes, false);
Expand Down Expand Up @@ -1065,8 +1065,8 @@ std::string Codegen::getTypeMangledName(llvm::SMRange span, llvm::Type *type) {
}

// TODO: Change to support private/public and module system
std::string Codegen::getMangledName(llvm::SMRange span, std::string func_name, const std::vector<llvm::Type *> &paramTypes) {
std::string name = classSymbol != nullptr ? classSymbol->getName() + "::" + std::move(func_name) + ":" : "_" + std::move(func_name) + ":";
std::string Codegen::getMangledName(llvm::SMRange span, std::string func_name, const std::vector<llvm::Type *> &paramTypes, bool isMethod) {
std::string name = classSymbol != nullptr && isMethod ? classSymbol->getName() + "::" + std::move(func_name) + ":" : "_" + std::move(func_name) + ":";
bool first = true;

for (auto param_type: paramTypes) {
Expand Down Expand Up @@ -1171,10 +1171,11 @@ llvm::Value *Codegen::genFuncCall(FuncCall *node, const std::vector<llvm::Value
paramTypes.insert(paramTypes.begin(), class_ptr->getType());

classSymbol = class_sym;
name = getMangledName(node->getSpan(), "new", paramTypes);
name = getMangledName(node->getSpan(), "new", paramTypes, true);
} else {
name = getMangledName(node->getSpan(), node->getName(), paramTypes);
name = getMangledName(node->getSpan(), node->getName(), paramTypes, !extra_params.empty());
}

auto symbol = Scope->lookup(name);
// Get function without name mangling in case of extern C functions
symbol = symbol == nullptr ? Scope->lookup(node->getName()) : symbol;
Expand Down
2 changes: 1 addition & 1 deletion src/liblesma/Backend/Codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace lesma {
llvm::Value *Cast(llvm::SMRange span, llvm::Value *val, llvm::Type *type);
llvm::Value *Cast(llvm::SMRange span, llvm::Value *val, llvm::Type *type, bool isStore);
static llvm::Type *GetExtendedType(llvm::Type *left, llvm::Type *right);
std::string getMangledName(llvm::SMRange span, std::string func_name, const std::vector<llvm::Type *> &paramTypes);
std::string getMangledName(llvm::SMRange span, std::string func_name, const std::vector<llvm::Type *> &paramTypes, bool isMethod = false);
std::string getTypeMangledName(llvm::SMRange span, llvm::Type *type);
llvm::Value *genFuncCall(FuncCall *node, const std::vector<llvm::Value *> &extra_params);
static int FindIndexInFields(SymbolType *_struct, const std::string &field);
Expand Down

0 comments on commit 43386b6

Please sign in to comment.