From a44f1246dbe24fc813a212a5ef3f5811b1d6402a Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 17:42:08 +0000 Subject: feat: whole arrays (and records) as FUNCTION/PROCEDURE parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Parameter syntax lands end-to-end: ARRAY OF T (rank 1, bounds travel with the argument) and ARRAY[l:u,...] OF T (any rank, literal bounds as a checked contract). Element type and rank must match exactly. - Flattened ABI: each rank-R array parameter becomes a data pointer plus kBoundArgsPerDim i64 bounds per dimension. The callee rebuilds ArrayMetadata from those bounds (ArrayHandler::bindArrayParameter via a binder callback injected by CodeGen, so FunctionGen stays ignorant of the array subsystem); the checked-indexing pipeline is reused unchanged. - BYVAL (the CIE default) copies the whole buffer at callee entry (malloc+memcpy, never freed — consistent with the string policy) so callee writes stay invisible; BYREF binds the caller's buffer. - Bounded-parameter contract: compile-time comparison when the caller's bounds are constants, else a runtime [Fatal] check (RuntimeCheck::emitArrayArgBoundsCheck). - marshalCallArgs gains the array branch and a call-site line number (CallStmtAST now carries one); whole-array-to-scalar keeps a clear error, FUNCTION RETURNS ARRAY is a dedicated parse error. - Verified: BYREF fill/sort-style mutation, BYVAL copy invisibility, 2-D bounded params, ARRAY OF with field writes through the parameter, bounds-contract mismatches at compile time and run time. Co-Authored-By: Claude Fable 5 --- lib/CodeGen/CodeGen.cc | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'lib/CodeGen/CodeGen.cc') diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index 62a6f1e..64e1b9f 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -90,6 +90,15 @@ CodeGen::CodeGen() { StrConvHandler = std::make_unique(*TheContext, *Builder, *TheModule); Files = std::make_unique(*TheContext, *Builder, *TheModule, *RuntimeChecker); + // Array parameters are bound into ArrayHandler through this inversion so + // FunctionGen never sees the array subsystem. + FuncGen->setArrayParamBinder( + [this](const ParamDecl &P, Value *DataPtr, + const std::vector &LBs, const std::vector &UBs) { + Arrays->bindArrayParameter(P.Name, P.TypeName, DataPtr, LBs, UBs, + /*MakeCopy=*/P.Mode == PassMode::ByVal, *this); + }); + SetupExternalFunctions(); } @@ -714,7 +723,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { } std::vector Args; - if (!marshalCallArgs(Name, Call->getArgs(), Args)) return nullptr; + if (!marshalCallArgs(Name, Call->getArgs(), Call->getLine(), Args)) return nullptr; return FuncGen->emitCallExpr(Call, Args); } @@ -740,6 +749,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { // BYVAL parameters are evaluated and coerced to the declared parameter type. bool CodeGen::marshalCallArgs(const std::string &Callee, const std::vector> &ArgExprs, + int Line, std::vector &Out) { const FuncSig *Sig = FuncGen->getSignature(Callee); if (!Sig) { @@ -752,17 +762,26 @@ bool CodeGen::marshalCallArgs(const std::string &Callee, } for (size_t i = 0; i < ArgExprs.size(); ++i) { - const std::string &TypeName = Sig->Params[i].first; - bool IsByRef = Sig->Params[i].second; + const ParamSig &P = Sig->Params[i]; + const std::string &TypeName = P.TypeName; + bool IsByRef = P.IsByRef; ExprAST *ArgExpr = ArgExprs[i].get(); - // Parameters can never be arrays, so a whole-array argument is always - // a user error (and would smash the data pointer if let through). + if (P.IsArray) { + if (!Arrays->emitArrayArgument(ArgExpr, P.TypeName, P.Rank, P.DeclaredBounds, + Callee, Line, *this, Out)) { + return false; + } + continue; + } + + // A whole-array argument against a scalar parameter is always a user + // error (and would smash the data pointer if let through). if (auto *Var = dynamic_cast(ArgExpr)) { const SymbolInfo *Info = getSymbolInfo(Var->getName()); if (Info && Info->IsArray) { - reportError("Cannot pass whole array %s to %s; pass an element like %s[i]", - Var->getName().c_str(), Callee.c_str(), Var->getName().c_str()); + reportError("Cannot pass whole array %s to %s; the parameter is not an array (declare it ARRAY OF %s)", + Var->getName().c_str(), Callee.c_str(), TypeName.c_str()); return false; } } @@ -959,7 +978,7 @@ void CodeGen::emitStmt(StmtAST *Stmt) { if (auto *Call = dynamic_cast(Stmt)) { std::vector Args; - if (!marshalCallArgs(Call->getCallee(), Call->getArgs(), Args)) return; + if (!marshalCallArgs(Call->getCallee(), Call->getArgs(), Call->getLine(), Args)) return; FuncGen->emitCallStmt(Call, Args); return; } -- cgit v1.3.1-4-g156e