aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/TemplateClone.h
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-21 05:54:26 +0000
committer_ov4 <ov4@ov4.org>2026-10-21 05:54:26 +0000
commitc56877035d6600b463a9cbf0e339f7548c0b46fd (patch)
tree5b2af13b7f8f097d8de5436a729b39e85427b9f0 /lib/CodeGen/TemplateClone.h
parentdd5bbae055fd34ce2e12ef63f8a261c6b15dc83e (diff)
feat(codegen): instantiate function templates by deduction
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/TemplateClone.h')
-rw-r--r--lib/CodeGen/TemplateClone.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/CodeGen/TemplateClone.h b/lib/CodeGen/TemplateClone.h
new file mode 100644
index 0000000..a0f196f
--- /dev/null
+++ b/lib/CodeGen/TemplateClone.h
@@ -0,0 +1,32 @@
+#pragma once
+#include "cps/TemplateAST.h"
+#include "cps/Identifier.h"
+#include <functional>
+
+namespace cps::detail {
+
+using TemplateBindings = IdentifierMap<std::vector<std::string>>;
+
+// Clones own all children; source ASTs remain reusable for later instances.
+class TemplateClone {
+ const TemplateBindings &Bindings;
+ IdentifierMap<std::vector<std::string>> PackNames;
+ std::function<void(int, const std::string &)> Error;
+ bool Failed = false;
+ void fail(int Line, const std::string &Message);
+ std::vector<std::unique_ptr<ExprAST>> arguments(const std::vector<std::unique_ptr<ExprAST>> &Args);
+ std::vector<std::unique_ptr<StmtAST>> body(const std::vector<std::unique_ptr<StmtAST>> &Body);
+ std::unique_ptr<ExprAST> expression(ExprAST *Expr);
+ std::unique_ptr<StmtAST> statement(StmtAST *Stmt);
+public:
+ TemplateClone(const TemplateBindings &Bindings,
+ std::function<void(int, const std::string &)> Error)
+ : Bindings(Bindings), Error(std::move(Error)) {}
+ std::string type(const std::string &Name);
+ std::unique_ptr<FunctionDefAST> function(const FunctionDefAST *Source,
+ const std::string &Name, const std::string &OwnerClass = {});
+ std::unique_ptr<ClassTypeDeclAST> klass(const ClassTypeDeclAST *Source,
+ const std::string &Name);
+};
+
+} // namespace cps::detail