From b28384fed1ce638e8b00b72dba974e9fd7e55eb4 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 28 Oct 2026 13:36:16 +0000 Subject: feat(overload): support default return types Co-Authored-By: Claude Opus 5.5 --- lib/CodeGen/FunctionGen.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'lib/CodeGen/FunctionGen.cc') diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index c162b46..a0c520d 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -113,9 +113,30 @@ const FuncSig *FunctionGen::getSignatureByLink(const std::string &Name) const { return &Signatures.at(It->second.first)[It->second.second]; } +// Defaults resolve only return-type ambiguity within one parameter signature. +const FuncSig *FunctionGen::selectDefaultReturn(const std::vector &Candidates) { + const FuncSig *Selected = nullptr; + for (const FuncSig *Sig : Candidates) { + if (!sameIdentifier(Sig->Name, Candidates.front()->Name) || + !sameIdentifier(Sig->ParamKey, Candidates.front()->ParamKey)) return nullptr; + if (Sig->IsDefaultReturn) { + if (Selected) return nullptr; + Selected = Sig; + } + } + return Selected; +} + // Copy-constructor, operator, MAIN and name-collision rules for a // FUNCTION/PROCEDURE prototype; reports the first violation. bool FunctionGen::validatePrototype(PrototypeAST *Proto) { + if (Proto->isDefaultReturn()) { + const TypeInfo *Return = Types.resolve(Proto->getReturnType()); + if (!Proto->isOverload() || (Return && Return->isVoid())) { + reportError("DEFAULT requires a non-VOID OVERLOAD FUNCTION"); + return false; + } + } if (!Proto->getOwnerClass().empty() && sameIdentifier(Proto->getName(), "NEW") && Proto->getParams().size() == 1) { const auto &P = Proto->getParams()[0]; @@ -192,6 +213,7 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { Sig.OwnerUnit = CurrentUnit; Sig.Name = Proto->getLinkName(); Sig.IsOverload = Proto->isOverload(); + Sig.IsDefaultReturn = Proto->isDefaultReturn(); Sig.Prototype = Proto; Sig.ReturnTypeName = Proto->getReturnType(); @@ -246,6 +268,11 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { Proto->getName().c_str(), Proto->getName().c_str()); return nullptr; } + if (sameIdentifier(Existing.ParamKey, Sig.ParamKey) && + Existing.IsDefaultReturn && Sig.IsDefaultReturn) { + reportError("Duplicate DEFAULT return for %s(%s)", Sig.Name.c_str(), Sig.ParamKey.c_str()); + return nullptr; + } if (sameIdentifier(Existing.ParamKey, Sig.ParamKey) && sameIdentifier(Existing.ReturnKey, Sig.ReturnKey)) { if (Existing.OwnerUnit != CurrentUnit) -- cgit v1.3.1-4-g156e