aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-22 03:52:21 +0000
committer_ov4 <ov4@ov4.org>2026-10-22 03:58:26 +0000
commite717664c749cf237479147ddecd70a39fc3426f2 (patch)
tree3db2c715f2b566654d356ccd1e3fd6adbe23c260 /lib
parent5fdea7e2edd534d15eff95efb33336430d7b4872 (diff)
refactor(codegen): share writable variable lookup
emitLValue (variable and designator base) and plain assignment repeated the same lookup, enum-or-unknown diagnostic and requireWritable check. Move it into a private lookupWritableSymbol helper. READFILE keeps its own lookup, which has no enum diagnostic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/CodeGen/Designator.cc41
-rw-r--r--lib/CodeGen/StatementGen.cc12
2 files changed, 22 insertions, 31 deletions
diff --git a/lib/CodeGen/Designator.cc b/lib/CodeGen/Designator.cc
index ed43cbe..4f0e82b 100644
--- a/lib/CodeGen/Designator.cc
+++ b/lib/CodeGen/Designator.cc
@@ -139,22 +139,30 @@ Value *CodeGen::emitMethodCall(const std::string &Class, Value *Receiver,
return FuncGen->emitCall(*Sig, Args);
}
+// Looks up a variable that is about to be written, diagnosing unknown names,
+// enum values and read-only symbols.
+const SymbolInfo *CodeGen::lookupWritableSymbol(const std::string &Name) {
+ const SymbolInfo *Info = getSymbolInfo(Name);
+ if (!Info) {
+ if (Types->lookupEnumConstant(Name)) {
+ reportError("Enum value %s is not an assignable location", Name.c_str());
+ } else {
+ reportError("Unknown variable name %s", Name.c_str());
+ }
+ return nullptr;
+ }
+ if (!requireWritable(*Info, Name)) return nullptr;
+ return Info;
+}
+
// Resolves a designator (or legacy variable/array-access node) to the address
// of its storage plus the type name stored there. Array indexing goes through
// ArrayHandler's checked pipeline; dereferences emit the null-pointer check.
bool CodeGen::emitLValue(ExprAST *E, LValueInfo &Out) {
SaveAndRestore<int> Location(DiagnosticLine, E && E->getLine() ? E->getLine() : DiagnosticLine);
if (auto *Var = dynamic_cast<VariableExprAST*>(E)) {
- const SymbolInfo *Info = getSymbolInfo(Var->getName());
- if (!Info) {
- if (Types->lookupEnumConstant(Var->getName())) {
- reportError("Enum value %s is not an assignable location", Var->getName().c_str());
- } else {
- reportError("Unknown variable name %s", Var->getName().c_str());
- }
- return false;
- }
- if (!requireWritable(*Info, Var->getName())) return false;
+ const SymbolInfo *Info = lookupWritableSymbol(Var->getName());
+ if (!Info) return false;
if (Info->IsArray) {
reportError("Whole array %s is not an assignable location; index it", Var->getName().c_str());
return false;
@@ -175,17 +183,8 @@ bool CodeGen::emitLValue(ExprAST *E, LValueInfo &Out) {
return false;
}
- const SymbolInfo *Info = getSymbolInfo(D->getBaseName());
- if (!Info) {
- if (Types->lookupEnumConstant(D->getBaseName())) {
- reportError("Enum value %s is not an assignable location", D->getBaseName().c_str());
- } else {
- reportError("Unknown variable name %s", D->getBaseName().c_str());
- }
- return false;
- }
-
- if (!requireWritable(*Info, D->getBaseName())) return false;
+ const SymbolInfo *Info = lookupWritableSymbol(D->getBaseName());
+ if (!Info) return false;
const auto &Accesses = D->getAccesses();
Value *Addr = symbolAddress(*Info);
std::string TypeName = Info->TypeName;
diff --git a/lib/CodeGen/StatementGen.cc b/lib/CodeGen/StatementGen.cc
index f4cd661..c1a9490 100644
--- a/lib/CodeGen/StatementGen.cc
+++ b/lib/CodeGen/StatementGen.cc
@@ -152,16 +152,8 @@ void CodeGen::emitStmtImpl(StmtAST *Stmt) {
}
if (auto *Assign = dynamic_cast<AssignStmtAST*>(Stmt)) {
- const SymbolInfo *Info = getSymbolInfo(Assign->getName());
- if (!Info) {
- if (Types->lookupEnumConstant(Assign->getName())) {
- reportError("Enum value %s is not an assignable location", Assign->getName().c_str());
- } else {
- reportError("Unknown variable name %s", Assign->getName().c_str());
- }
- return;
- }
- if (!requireWritable(*Info, Assign->getName())) return;
+ const SymbolInfo *Info = lookupWritableSymbol(Assign->getName());
+ if (!Info) return;
if (Info->IsArray) {
reportError("Cannot assign array %s without indices", Assign->getName().c_str());
return;