diff options
| author | David Blaikie <dblaikie@gmail.com> | 2015-11-03 16:23:21 +0000 |
|---|---|---|
| committer | David Blaikie <dblaikie@gmail.com> | 2015-11-03 16:23:21 +0000 |
| commit | 96a9d8c8e5c083ca8150ec21ca98c4511b93849d (patch) | |
| tree | 510645129b44af2b492469a26f06f3db11f1291c /llvm/examples | |
| parent | 77f16f369b527f5ccd2c114885694435e161eb30 (diff) | |
| download | bcm5719-llvm-96a9d8c8e5c083ca8150ec21ca98c4511b93849d.tar.gz bcm5719-llvm-96a9d8c8e5c083ca8150ec21ca98c4511b93849d.zip | |
Kaleidoscope-ch2: Remove the dependence on LLVM by cloning make_unique into this project
llvm-svn: 251936
Diffstat (limited to 'llvm/examples')
| -rw-r--r-- | llvm/examples/Kaleidoscope/Chapter2/toy.cpp | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/llvm/examples/Kaleidoscope/Chapter2/toy.cpp b/llvm/examples/Kaleidoscope/Chapter2/toy.cpp index bcba554b246..69f35996129 100644 --- a/llvm/examples/Kaleidoscope/Chapter2/toy.cpp +++ b/llvm/examples/Kaleidoscope/Chapter2/toy.cpp @@ -1,10 +1,22 @@ -#include "llvm/ADT/STLExtras.h" #include <cctype> #include <cstdio> #include <map> +#include <memory> #include <string> #include <vector> +namespace helper { +// Cloning make_unique here until it's standard in C++14. +// Using a namespace to avoid conflicting with MSVC's std::make_unique (which +// ADL can sometimes find in unqualified calls). +template <class T, class... Args> +static + typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type + make_unique(Args &&... args) { + return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); +} +} + //===----------------------------------------------------------------------===// // Lexer //===----------------------------------------------------------------------===// @@ -189,7 +201,7 @@ static std::unique_ptr<ExprAST> ParseExpression(); /// numberexpr ::= number static std::unique_ptr<ExprAST> ParseNumberExpr() { - auto Result = llvm::make_unique<NumberExprAST>(NumVal); + auto Result = helper::make_unique<NumberExprAST>(NumVal); getNextToken(); // consume the number return std::move(Result); } @@ -216,7 +228,7 @@ static std::unique_ptr<ExprAST> ParseIdentifierExpr() { getNextToken(); // eat identifier. if (CurTok != '(') // Simple variable ref. - return llvm::make_unique<VariableExprAST>(IdName); + return helper::make_unique<VariableExprAST>(IdName); // Call. getNextToken(); // eat ( @@ -240,7 +252,7 @@ static std::unique_ptr<ExprAST> ParseIdentifierExpr() { // Eat the ')'. getNextToken(); - return llvm::make_unique<CallExprAST>(IdName, std::move(Args)); + return helper::make_unique<CallExprAST>(IdName, std::move(Args)); } /// primary @@ -292,8 +304,8 @@ static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, } // Merge LHS/RHS. - LHS = - llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); + LHS = helper::make_unique<BinaryExprAST>(BinOp, std::move(LHS), + std::move(RHS)); } } @@ -329,7 +341,7 @@ static std::unique_ptr<PrototypeAST> ParsePrototype() { // success. getNextToken(); // eat ')'. - return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames)); + return helper::make_unique<PrototypeAST>(FnName, std::move(ArgNames)); } /// definition ::= 'def' prototype expression @@ -340,7 +352,7 @@ static std::unique_ptr<FunctionAST> ParseDefinition() { return nullptr; if (auto E = ParseExpression()) - return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); + return helper::make_unique<FunctionAST>(std::move(Proto), std::move(E)); return nullptr; } @@ -348,9 +360,9 @@ static std::unique_ptr<FunctionAST> ParseDefinition() { static std::unique_ptr<FunctionAST> ParseTopLevelExpr() { if (auto E = ParseExpression()) { // Make an anonymous proto. - auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr", - std::vector<std::string>()); - return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); + auto Proto = helper::make_unique<PrototypeAST>("__anon_expr", + std::vector<std::string>()); + return helper::make_unique<FunctionAST>(std::move(Proto), std::move(E)); } return nullptr; } |

