diff --git a/llvm/examples/Kaleidoscope/Chapter3/toy.cpp b/llvm/examples/Kaleidoscope/Chapter3/toy.cpp index 03563006685ad..f243117e85214 100644 --- a/llvm/examples/Kaleidoscope/Chapter3/toy.cpp +++ b/llvm/examples/Kaleidoscope/Chapter3/toy.cpp @@ -165,6 +165,8 @@ class PrototypeAST { Function *codegen(); const std::string &getName() const { return Name; } + + const std::vector &getArgs() const { return Args; } }; /// FunctionAST - This class represents a function definition itself. @@ -383,7 +385,7 @@ static std::unique_ptr ParseTopLevelExpr() { if (auto E = ParseExpression()) { // Make an anonymous proto. auto Proto = std::make_unique("__anon_expr", - std::vector()); + std::vector()); return std::make_unique(std::move(Proto), std::move(E)); } return nullptr; @@ -484,8 +486,28 @@ Function *FunctionAST::codegen() { // First, check for an existing function from a previous 'extern' declaration. Function *TheFunction = TheModule->getFunction(Proto->getName()); - if (!TheFunction) + if (TheFunction && !TheFunction->empty()) { + fprintf(stderr, "Error: Function '%s' redefined.\n", + TheFunction->getName().str().c_str()); + return nullptr; + } + + if (!TheFunction) { TheFunction = Proto->codegen(); + } else { + if (TheFunction->arg_size() != Proto->getArgs().size()) { + fprintf(stderr, + "Error: Function '%s' redefined with different number of " + "arguments.\n", + TheFunction->getName().str().c_str()); + return nullptr; + } + + size_t ArgIdx = 0ul; + for (auto &FArg : TheFunction->args()) { + FArg.setName(Proto->getArgs()[ArgIdx++]); + } + } if (!TheFunction) return nullptr;