Skip to content

Commit

Permalink
fix: fixed importing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed Apr 24, 2023
1 parent 7824a9c commit 6cf7681
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ void Codegen::CompileModule(llvm::SMRange span, const std::string &filepath, boo

// Only import if it's exported
imp_alias = findInImports(name);
if (func_symbol != nullptr && func_symbol->isExported() && (importAll || !imp_alias.empty() || isMethod(name))) {
// TODO: methods should only be imported if they class is in the imports specified
if (func_symbol != nullptr && func_symbol->isExported() && (importAll || !imp_alias.empty() || isMethod(sym.second->getMangledName()))) {
auto symbol = new Value(imp_alias.empty() ? name : std::regex_replace(name, std::regex(name), imp_alias), func_symbol->getType());

if (isJIT) {
Expand All @@ -253,6 +254,7 @@ void Codegen::CompileModule(llvm::SMRange span, const std::string &filepath, boo
symbol->setExported(false);
symbol->setMangledName(sym.second->getMangledName());
}

Scope->insertSymbol(symbol);
}
}
Expand Down Expand Up @@ -576,11 +578,13 @@ void Codegen::visit(const FuncDecl *node) {
std::vector<Field *> fields;
std::vector<lesma::Type *> paramTypes;
std::vector<llvm::Type *> paramLLVMTypes;
bool shouldExport = node->isExported();

if (selfSymbol != nullptr) {
paramTypes.push_back(selfSymbol->getType());
paramLLVMTypes.push_back(selfSymbol->getType()->getLLVMType()->getPointerTo());
fields.push_back(new Field{"self", selfSymbol->getType()});
shouldExport = selfSymbol->isExported();
}

for (auto param: node->getParameters()) {
Expand Down Expand Up @@ -615,7 +619,7 @@ void Codegen::visit(const FuncDecl *node) {
}

auto mangledName = getMangledName(node->getSpan(), node->getName(), paramTypes, selfSymbol != nullptr);
auto linkage = node->isExported() ? Function::ExternalLinkage : Function::PrivateLinkage;
auto linkage = shouldExport ? Function::ExternalLinkage : Function::PrivateLinkage;

node->getReturnType()->accept(*this);

Expand Down Expand Up @@ -871,6 +875,7 @@ void Codegen::visit(const Class *node) {
Scope->insertSymbol(structSymbol);

selfSymbol = new Value(node->getIdentifier(), new Type(TY_PTR, structType->getPointerTo(), type));
selfSymbol->setExported(node->isExported());
auto has_constructor = false;
for (auto func: node->getMethods()) {
func->accept(*this);
Expand Down Expand Up @@ -1492,19 +1497,16 @@ lesma::Value *Codegen::Cast(llvm::SMRange span, lesma::Value *val, lesma::Type *
}

lesma::Value *Codegen::genFuncCall(const FuncCall *node, const std::vector<lesma::Value *> &extra_params = {}) {
std::vector<lesma::Value *> params;
std::vector<lesma::Type *> paramTypes;
std::vector<llvm::Value *> paramsLLVM;

for (auto arg: extra_params) {
params.push_back(arg);
paramTypes.push_back(arg->getType());
paramsLLVM.push_back(arg->getLLVMValue());
}

for (auto arg: node->getArguments()) {
arg->accept(*this);
params.push_back(result);
paramTypes.push_back(result->getType());
paramsLLVM.push_back(result->getLLVMValue());
}
Expand Down
5 changes: 2 additions & 3 deletions src/liblesma/Symbol/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ Value *SymbolTable::lookupFunction(const std::string &name, std::vector<lesma::T
if (!it->second->getType()->is(TY_FUNCTION))
continue;
// Check if the parameter types match
std::vector<Field *> funcParamTypes = it->second->getType()->getFields();
std::vector<llvm::Value *> tmpValues;

bool paramsMatch = true;
std::vector<Field *> funcParamTypes = it->second->getType()->getFields();
size_t numParams = std::max(funcParamTypes.size(), paramTypes.size());

for (size_t i = 0; i < numParams; ++i) {
if (i < funcParamTypes.size() && i < paramTypes.size()) {
if (!funcParamTypes[i]->type->isEqual(paramTypes[i])) {
Expand Down

0 comments on commit 6cf7681

Please sign in to comment.