From 1d2bb133b7a3540aeb81c5f9616cb366aebc919f Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 03:45:06 +0000 Subject: refactor(codegen): share file keyword filename operand emission EOF, OPENFILE, READFILE and CLOSEFILE each rejected a whole-array filename and then emitted it as a C string. Route them through a private emitFileNameOperand helper; diagnostics are unchanged. WRITEFILE keeps its own combined check. Co-Authored-By: Claude Opus 5.5 --- lib/CodeGen/ExpressionGen.cc | 6 +----- lib/CodeGen/StatementGen.cc | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/CodeGen/ExpressionGen.cc b/lib/CodeGen/ExpressionGen.cc index 3377a39..ece055a 100644 --- a/lib/CodeGen/ExpressionGen.cc +++ b/lib/CodeGen/ExpressionGen.cc @@ -225,11 +225,7 @@ Value *CodeGen::emitCall(const std::string &Callee, return Memory->emitRelease(Address, Line); } if (Name == "EOF") { - if (isWholeArrayVar(ArgExprs[0].get())) { - reportError("Cannot use a whole array as the EOF filename"); - return nullptr; - } - Value *FileName = emitCStringExpr(ArgExprs[0].get()); + Value *FileName = emitFileNameOperand(ArgExprs[0].get(), "EOF"); if (!FileName) return nullptr; return Files->emitEof(FileName, Line); } diff --git a/lib/CodeGen/StatementGen.cc b/lib/CodeGen/StatementGen.cc index 40fe394..f4cd661 100644 --- a/lib/CodeGen/StatementGen.cc +++ b/lib/CodeGen/StatementGen.cc @@ -239,11 +239,7 @@ void CodeGen::emitStmtImpl(StmtAST *Stmt) { } if (auto *Open = dynamic_cast(Stmt)) { - if (isWholeArrayVar(Open->getFileName())) { - reportError("Cannot use a whole array as the OPENFILE filename"); - return; - } - Value *Name = emitCStringExpr(Open->getFileName()); + Value *Name = emitFileNameOperand(Open->getFileName(), "OPENFILE"); if (!Name) return; Files->emitOpen(Name, Open->getMode(), Open->getLine()); return; @@ -265,11 +261,7 @@ void CodeGen::emitStmtImpl(StmtAST *Stmt) { Read->getVarName().c_str(), Info->TypeName.c_str()); return; } - if (isWholeArrayVar(Read->getFileName())) { - reportError("Cannot use a whole array as the READFILE filename"); - return; - } - Value *Name = emitCStringExpr(Read->getFileName()); + Value *Name = emitFileNameOperand(Read->getFileName(), "READFILE"); if (!Name) return; Value *LineStr = Files->emitRead(Name, Read->getLine()); if (!LineStr) return; @@ -302,11 +294,7 @@ void CodeGen::emitStmtImpl(StmtAST *Stmt) { } if (auto *Close = dynamic_cast(Stmt)) { - if (isWholeArrayVar(Close->getFileName())) { - reportError("Cannot use a whole array as the CLOSEFILE filename"); - return; - } - Value *Name = emitCStringExpr(Close->getFileName()); + Value *Name = emitFileNameOperand(Close->getFileName(), "CLOSEFILE"); if (!Name) return; Files->emitClose(Name, Close->getLine()); return; @@ -346,3 +334,13 @@ void CodeGen::emitStmtImpl(StmtAST *Stmt) { return; } } + +// File keywords take their filename as a STRING-valued expression; a whole +// array is rejected before evaluation. +Value *CodeGen::emitFileNameOperand(ExprAST *E, const char *Keyword) { + if (isWholeArrayVar(E)) { + reportError("Cannot use a whole array as the %s filename", Keyword); + return nullptr; + } + return emitCStringExpr(E); +} -- cgit v1.3.1-4-g156e