Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/Translation/ForthToMLIR/ForthToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,13 @@ LogicalResult ForthParser::parseWordDefinition() {
return emitError("kernel name '" + kernelName + "' conflicts with word " +
currentToken.text);
}
llvm::SMLoc nameLoc = currentToken.location;
if (auto previous = wordDefs.find(wordName); previous != wordDefs.end()) {
(void)emitError("word '" + wordName + "' is already defined");
sourceMgr.PrintMessage(previous->second, llvm::SourceMgr::DK_Note,
"previous definition is here");
return failure();
}
consume(); // consume word name

// Create function: !forth.stack -> !forth.stack
Expand Down Expand Up @@ -1435,7 +1442,7 @@ LogicalResult ForthParser::parseWordDefinition() {
builder.create<func::ReturnOp>(loc, resultStack);

// Register the word
wordDefs.insert(wordName);
wordDefs[wordName] = nameLoc;

consume(); // consume ';'
return success();
Expand Down
3 changes: 1 addition & 2 deletions lib/Translation/ForthToMLIR/ForthToMLIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/SourceMgr.h"
#include <string>
#include <unordered_set>
#include <vector>

namespace mlir {
Expand Down Expand Up @@ -98,7 +97,7 @@ class ForthParser {
OpBuilder builder;
ForthLexer lexer;
Token currentToken;
std::unordered_set<std::string> wordDefs;
llvm::StringMap<llvm::SMLoc> wordDefs;
std::vector<ParamDecl> paramDecls;
std::vector<SharedDecl> sharedDecls;
llvm::StringMap<Value> paramValues;
Expand Down
9 changes: 9 additions & 0 deletions test/Translation/Forth/word-redefinition-error.forth
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
\ CHECK: :[[@LINE+6]]:3: error: word 'FOO' is already defined
\ CHECK: :[[@LINE+4]]:3: note: previous definition is here
\ CHECK-NOT: redefinition of symbol
\! kernel k
\! param a i64[1]
: foo 1 ;
: foo 2 ;
foo a !
Loading