From a99852084ff6f0491734d508a4dcd2e7d8411814 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 06:04:11 +0000 Subject: feat(classes): warn when method declarations shadow fields Retain parameter source lines for warnings and preserve field access prefix validation when distinguishing fields from methods. Co-Authored-By: Claude Fable 5.1 --- lib/CodeGen/ConstantGen.cc | 12 ++++++++++++ lib/CodeGen/StatementGen.cc | 2 ++ 2 files changed, 14 insertions(+) (limited to 'lib/CodeGen') diff --git a/lib/CodeGen/ConstantGen.cc b/lib/CodeGen/ConstantGen.cc index 12bf4b1..58913e4 100644 --- a/lib/CodeGen/ConstantGen.cc +++ b/lib/CodeGen/ConstantGen.cc @@ -1,4 +1,5 @@ #include "cps/CodeGen.h" +#include using namespace llvm; using namespace cps; @@ -15,6 +16,16 @@ void CodeGen::emitScopedBlock(const std::vector> &Body) ConstantScopes.pop_back(); } +void CodeGen::warnMemberShadow(const std::string &Name, int Line) const { + if (CurrentClass.empty()) return; + const TypeInfo *Class = resolveType(CurrentClass); + const RecordPayload *Record = Class ? Class->asRecord() : nullptr; + if (Record && Record->findField(Name)) { + fprintf(stderr, "warning: Declaration of %s shadows a field of class %s at line %d\n", + Name.c_str(), CurrentClass.c_str(), Line); + } +} + bool CodeGen::prepareSymbolDeclaration(const std::string &Name, bool IsConstant) { if (!CurrentClass.empty() && sameIdentifier(Name, "this")) { reportError("Cannot redeclare this inside a method"); @@ -31,6 +42,7 @@ bool CodeGen::prepareSymbolDeclaration(const std::string &Name, bool IsConstant) } else if (IsConstant) { Scope.Saved.emplace(Name, std::nullopt); } + warnMemberShadow(Name, DiagnosticLine); return true; } diff --git a/lib/CodeGen/StatementGen.cc b/lib/CodeGen/StatementGen.cc index c55aebe..2443877 100644 --- a/lib/CodeGen/StatementGen.cc +++ b/lib/CodeGen/StatementGen.cc @@ -23,6 +23,8 @@ void CodeGen::emitStmt(StmtAST *Stmt) { if (auto *FuncDef = dynamic_cast(Stmt)) { SaveAndRestore ClassContext(CurrentClass, FuncDef->getProto()->getOwnerClass()); + for (const auto &Param : FuncDef->getProto()->getParams()) + warnMemberShadow(Param.Name, Param.Line ? Param.Line : DiagnosticLine); BasicBlock *SavedBlock = Builder->GetInsertBlock(); // Function bodies get a fresh array scope (metadata SSA values belong // to the emitting function), mirroring FunctionGen's Symbols handling. -- cgit v1.3.1-4-g156e