diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-17 06:04:11 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-17 06:04:11 +0000 |
| commit | a99852084ff6f0491734d508a4dcd2e7d8411814 (patch) | |
| tree | fc849f47b5d657c2cfe1d4b0648725ff2a308299 /lib | |
| parent | 3425b95cb654f8e1adc98365a4b370aa1fff8b4d (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/CodeGen/ConstantGen.cc | 12 | ||||
| -rw-r--r-- | lib/CodeGen/StatementGen.cc | 2 | ||||
| -rw-r--r-- | lib/Parser/ParserFunction.cc | 1 | ||||
| -rw-r--r-- | lib/Parser/ParserType.cc | 4 |
4 files changed, 18 insertions, 1 deletions
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 <cstdio> using namespace llvm; using namespace cps; @@ -15,6 +16,16 @@ void CodeGen::emitScopedBlock(const std::vector<std::unique_ptr<StmtAST>> &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<FunctionDefAST*>(Stmt)) { SaveAndRestore<std::string> 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. diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc index 1aef756..ad187c8 100644 --- a/lib/Parser/ParserFunction.cc +++ b/lib/Parser/ParserFunction.cc @@ -51,6 +51,7 @@ std::optional<std::vector<ParamDecl>> Parser::ParsePrototypeArgs(bool AllowByRef return std::nullopt; } ParamDecl P; + P.Line = Lex.getLine(); P.Name = Lex.IdentifierStr; P.Mode = Current; getNextToken(); diff --git a/lib/Parser/ParserType.cc b/lib/Parser/ParserType.cc index 0d664d0..dab71a1 100644 --- a/lib/Parser/ParserType.cc +++ b/lib/Parser/ParserType.cc @@ -124,7 +124,9 @@ std::optional<std::vector<RecordFieldDecl>> Parser::ParseRecordBody( if (AtField()) { // Class fields default to public, like record fields. bool IsPublic = CurTok != tok_private; - if (IsClass && (CurTok == tok_public || CurTok == tok_private)) getNextToken(); + if (IsClass && (CurTok == tok_public || CurTok == tok_private) && + (Lex.peekToken() == tok_function || Lex.peekToken() == tok_procedure)) + getNextToken(); if (IsClass && (CurTok == tok_function || CurTok == tok_procedure)) { auto Method = CurTok == tok_function ? ParseFunction(Name, IsPublic) : ParseProcedure(Name, IsPublic); |
