From 0900667401415a0914b8a1510543c7c7eaf32e53 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 24 Oct 2026 18:01:36 +0000 Subject: feat(overload): select overloads by return type as well as parameters Co-Authored-By: Claude Opus 5.5 --- lib/CodeGen/FunctionGen.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen/FunctionGen.cc') diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index c233205..2e927f9 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -229,6 +229,10 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { } Type *RetType = getLLVMType(Proto->getReturnType()); + const TypeInfo *ReturnInfo = Types.resolve(Proto->getReturnType()); + Sig.ReturnKey = ReturnInfo + ? (ReturnInfo->isC() ? "C:" + ReturnInfo->CIdentity : ReturnInfo->Name) + : Proto->getReturnType(); FunctionType *FT = FunctionType::get(RetType, ArgTypes, false); auto &Overloads = Signatures[Sig.Name]; for (const FuncSig &Existing : Overloads) { @@ -241,7 +245,8 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { Proto->getName().c_str(), Proto->getName().c_str()); return nullptr; } - if (sameIdentifier(Existing.ParamKey, Sig.ParamKey)) { + if (sameIdentifier(Existing.ParamKey, Sig.ParamKey) && + sameIdentifier(Existing.ReturnKey, Sig.ReturnKey)) { if (Existing.OwnerUnit != CurrentUnit) reportError("Duplicate definition %s in %s and %s", Sig.Name.c_str(), UnitPaths[Existing.OwnerUnit].c_str(), UnitPaths[CurrentUnit].c_str()); @@ -250,6 +255,12 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { } } Sig.LinkName = Sig.Name + (Sig.IsOverload ? "(" + Sig.ParamKey + ")" : ""); + // Only the return type distinguishes non-void overloads that share a + // parameter list, so it enters their link name. PROCEDURE, NEW and + // OPERATOR<- keep the established names. + if (Sig.IsOverload && ReturnInfo && !ReturnInfo->isVoid() && + !sameIdentifier(Proto->getName(), "NEW") && Proto->getOperator() != tok_assign) + Sig.LinkName += ":" + Sig.ReturnKey; Function *F = Function::Create(FT, Proto->getOwnerClass().empty() && sameIdentifier(Proto->getName(), "MAIN") ? Function::InternalLinkage : Function::ExternalLinkage, -- cgit v1.3.1-4-g156e