1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00

Fix memory leaks

This commit is contained in:
Ibrahim Numanagić 2022-02-01 12:39:50 -08:00
parent bbdd51cf9e
commit 6b4f29a3f2
5 changed files with 12 additions and 8 deletions

View File

@ -100,7 +100,7 @@ int LinkType::unify(Type *typ, Unification *undo) {
if (undo) {
LOG_TYPECHECK("[unify] {} := {}", id, typ->debugString(true));
// Link current type to typ and ensure that this modification is recorded in undo.
undo->linked.push_back(this);
undo->linked.push_back(getLink());
kind = Link;
seqassert(!typ->getLink() || typ->getLink()->kind != Unbound ||
typ->getLink()->id <= id,
@ -108,7 +108,7 @@ int LinkType::unify(Type *typ, Unification *undo) {
type = typ->follow();
if (auto t = type->getLink())
if (trait && t->kind == Unbound && !t->trait) {
undo->traits.push_back(t.get());
undo->traits.push_back(t->getLink());
t->trait = trait;
}
}
@ -205,7 +205,7 @@ bool LinkType::occurs(Type *typ, Type::Unification *undo) {
if (tl->trait && occurs(tl->trait.get(), undo))
return true;
if (undo && tl->level > level) {
undo->leveled.emplace_back(make_pair(tl.get(), tl->level));
undo->leveled.emplace_back(make_pair(tl, tl->level));
tl->level = level;
}
return false;

View File

@ -39,11 +39,11 @@ struct Type : public codon::SrcObject, public std::enable_shared_from_this<Type>
/// Needed because the unify() is destructive.
struct Unification {
/// List of unbound types that have been changed.
std::vector<LinkType *> linked;
std::vector<std::shared_ptr<LinkType>> linked;
/// List of unbound types whose level has been changed.
std::vector<std::pair<LinkType *, int>> leveled;
std::vector<std::pair<std::shared_ptr<LinkType>, int>> leveled;
/// List of assigned traits.
std::vector<LinkType *> traits;
std::vector<std::shared_ptr<LinkType>> traits;
/// Pointer to a TypecheckVisitor to support realization function types.
TypecheckVisitor *realizator = nullptr;

View File

@ -438,7 +438,7 @@ void TranslateVisitor::transformFunction(types::FuncType *type, FunctionStmt *as
func->setAttribute(std::make_unique<ir::KeyValueAttribute>(attr));
for (int i = 0; i < names.size(); i++)
func->getArgVar(names[i])->setSrcInfo(ast->args[indices[i]].getSrcInfo());
func->setUnmangledName(ctx->cache->reverseIdentifierLookup[type->ast->name]);
// func->setUnmangledName(ctx->cache->reverseIdentifierLookup[type->ast->name]);
if (!ast->attributes.has(Attr::C) && !ast->attributes.has(Attr::Internal)) {
ctx->addBlock();
for (auto i = 0; i < names.size(); i++)
@ -515,7 +515,7 @@ void TranslateVisitor::transformLLVMFunction(types::FuncType *type, FunctionStmt
f->setLLVMBody(join(lines, "\n"));
f->setLLVMDeclarations(declare);
f->setLLVMLiterals(literals);
func->setUnmangledName(ctx->cache->reverseIdentifierLookup[type->ast->name]);
// func->setUnmangledName(ctx->cache->reverseIdentifierLookup[type->ast->name]);
}
} // namespace ast

View File

@ -237,6 +237,7 @@ types::TypePtr TypecheckVisitor::realizeFunc(types::FuncType *type) {
} else {
r->ir = ctx->cache->module->Nr<ir::BodiedFunc>(type->realizedName());
}
r->ir->setUnmangledName(ctx->cache->reverseIdentifierLookup[type->ast->name]);
auto parent = type->funcParent;
if (!ast->attributes.parentClass.empty() &&
@ -284,6 +285,7 @@ types::TypePtr TypecheckVisitor::realizeFunc(types::FuncType *type) {
irType->setAstType(type->getFunc());
r->ir->realize(irType, names);
// LOG("-> {}", *(r->ir));
ctx->cache->functions[type->ast->name].realizations[type->realizedName()] = r;
} else {
ctx->cache->functions[type->ast->name].realizations[oldKey] =

View File

@ -874,6 +874,8 @@ void LLVMVisitor::visit(const InternalFunc *x) {
}
}
if (!result)
internalFuncMatchesIgnoreArgs<RecordType>("__new__", x);
seqassert(result, "internal function {} not found", *x);
B->CreateRet(result);
}