From e1f41ed9fafaf7eb4f2da6878a3fabcf2f7c021e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 09:13:32 +0000 Subject: feat(driver): compile multiple source files with INCLUDE visibility Reject builds without an entry program and duplicate cross-file definitions. Preserve source order for each unit's own constants and import visible exports. Co-Authored-By: Claude Opus 5.5 --- include/cps/CodeGen.h | 13 ++++++++++++- include/cps/FunctionGen.h | 5 +++++ include/cps/SourceUnit.h | 13 +++++++++++++ include/cps/TypeSystem.h | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 include/cps/SourceUnit.h (limited to 'include') diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index 9a0b2ef..6e0fab1 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -5,6 +5,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "cps/AST.h" +#include "cps/SourceUnit.h" #include "cps/RuntimeCheck.h" #include "cps/FunctionGen.h" #include "cps/TypeSystem.h" @@ -32,6 +33,14 @@ class CodeGen { std::unique_ptr TheModule; std::unique_ptr> Builder; int DiagnosticLine = 0; + const std::vector *Units = nullptr; + size_t CurrentUnit = 0; + size_t MainUnit = 0; + IdentifierMap TypeOwners; + std::vector> Exports; + bool isVisible(size_t Owner) const; + void selectUnit(size_t Unit); + void importSymbols(); IdentifierMap Symbols; IdentifierMap> ClassMethods; std::string CurrentClass; @@ -123,7 +132,7 @@ class CodeGen { void emitCInput(llvm::Value *Addr, const TypeInfo *Info); // TYPE-system registration (Designator.cc) and operators (TypeCheckGen.cc). - void runTypePrePass(const std::vector> &Statements); + void runTypePrePass(const std::vector> &Statements, int Phase); llvm::Value *emitUserKindBinaryOp(int Op, llvm::Value *L, const TypeInfo *LT, llvm::Value *R, const TypeInfo *RT, int Line); @@ -161,6 +170,8 @@ public: CodeGen(); ~CodeGen(); void compile(const std::vector> &Statements); + void compile(const std::vector &Sources); + size_t getMainUnit() const { return MainUnit; } void print(llvm::raw_ostream &Output); const llvm::Module &getModule() const { return *TheModule; } bool hadError() const { return HadError; } diff --git a/include/cps/FunctionGen.h b/include/cps/FunctionGen.h index 777ab7d..eb4a839 100644 --- a/include/cps/FunctionGen.h +++ b/include/cps/FunctionGen.h @@ -38,6 +38,7 @@ struct FuncSig { std::string Name; std::string ReturnTypeName; std::vector Params; + size_t OwnerUnit = 0; }; class FunctionGen { @@ -63,6 +64,8 @@ private: void reportError(const char *Fmt, ...); IdentifierMap Signatures; + size_t CurrentUnit = 0; + std::function Visible; ArrayParamBinder BindArrayParam; std::function RegisterCopy; @@ -86,6 +89,8 @@ public: RegisterCopy = std::move(Registrar); } + void setCurrentUnit(size_t Unit) { CurrentUnit = Unit; } + void setVisibility(std::function Predicate) { Visible = std::move(Predicate); } const FuncSig *getSignature(const std::string &Name) const; llvm::Function *emitPrototype(PrototypeAST *Proto); diff --git a/include/cps/SourceUnit.h b/include/cps/SourceUnit.h new file mode 100644 index 0000000..db9ad30 --- /dev/null +++ b/include/cps/SourceUnit.h @@ -0,0 +1,13 @@ +#pragma once +#include "cps/AST.h" +#include +#include + +namespace cps { +// The caller owns the parsed statements for the duration of compilation. +struct SourceUnit { + std::string Path; + const std::vector> &Statements; + std::vector Visible; +}; +} diff --git a/include/cps/TypeSystem.h b/include/cps/TypeSystem.h index d358794..d699067 100644 --- a/include/cps/TypeSystem.h +++ b/include/cps/TypeSystem.h @@ -5,6 +5,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Type.h" #include +#include #include #include #include @@ -118,6 +119,7 @@ class TypeSystem { IdentifierMap Types; IdentifierMap EnumConstants; + std::function Visible; void registerBuiltins(); const TypeInfo *registerType(const std::string &TypeName, llvm::Type *LLVMType, @@ -126,6 +128,7 @@ class TypeSystem { public: explicit TypeSystem(llvm::LLVMContext &Ctx); + void setVisibility(std::function Predicate) { Visible = std::move(Predicate); } const TypeInfo *resolve(const std::string &TypeName) const; // Two-pass user-type registration (pass A then pass B, both in source -- cgit v1.3.1-4-g156e