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 --- include/cps/CodeGen.h | 21 ++++++++++++++++----- include/cps/FunctionGen.h | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index ffd589c..99630ec 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -79,12 +79,21 @@ class CodeGen { std::string CurrentClass; const std::vector *findMethod(const std::string &Class, const std::string &Name) const; bool isRegisteredMethod(const std::string &Class, const PrototypeAST *Method) const; + // The type a call or operator result must have where it is used. A cast + // only prefers its type (Exact = false) and otherwise converts the result. + struct ResultContext { const TypeInfo *Type = nullptr; bool Exact = true; }; + std::map ResultContexts; + void expectResult(const ExprAST *E, const TypeInfo *Type, bool Exact = true); + const ResultContext *resultContext(const ExprAST *E) const; const FuncSig *resolveMethod(const std::string &Class, const std::string &Name, - const std::vector> &Args); - llvm::Value *emitMemberCall(MemberCallExprAST *Call); + const std::vector> &Args, + const ResultContext *Context = nullptr); + llvm::Value *emitMemberCall(MemberCallExprAST *Call, + const ResultContext *Context = nullptr); llvm::Value *emitMethodCall(const std::string &Class, llvm::Value *Receiver, const std::string &Name, - const std::vector> &Args, int Line); + const std::vector> &Args, int Line, + const ResultContext *Context = nullptr); bool typeNeedsDestruction(const std::string &TypeName) const; bool emitDestroy(const std::string &TypeName, llvm::Value *Addr, int Line); bool typeNeedsConstruction(const std::string &TypeName) const; @@ -204,10 +213,12 @@ class CodeGen { llvm::Value *emitIntBuiltin(ExprAST *Arg, int Line); llvm::Value *emitCall(const std::string &Callee, const std::vector> &ArgExprs, int Line, - const std::vector *Explicit = nullptr); + const std::vector *Explicit = nullptr, + const ResultContext *Context = nullptr); const FuncSig *resolveCall(const std::string &Name, const std::vector> &Args, - const std::vector *Explicit = nullptr); + const std::vector *Explicit = nullptr, + const ResultContext *Context = nullptr); struct OperatorResolution { const FuncSig *Signature = nullptr; bool Ambiguous = false; diff --git a/include/cps/FunctionGen.h b/include/cps/FunctionGen.h index f5e5ba2..c3c530b 100644 --- a/include/cps/FunctionGen.h +++ b/include/cps/FunctionGen.h @@ -40,6 +40,7 @@ struct FuncSig { std::string Name; std::string LinkName; std::string ParamKey; + std::string ReturnKey; bool IsOverload = false; const PrototypeAST *Prototype = nullptr; std::string ReturnTypeName; -- cgit v1.3.1-4-g156e