<div dir="ltr">> <span style="font-size:13px">It does look like this adds a long line or two though - do you mind </span><span style="font-size:13px">clang-format'ing the patch?</span><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">Clang-formatting the tutorials is a good idea. I've applied this patch in r245322 - I'll follow up with a clang-format patch next.</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">- Lang.</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 17, 2015 at 10:03 PM, Justin Bogner <span dir="ltr"><<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Lang Hames via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> writes:<br>
> Hi All,<br>
><br>
> Attached is a patch to partially C++11'ify the Kaleidoscope tutorials. There<br>
> are a few LLVM coding convention fixes and range-based for loops in here, but<br>
> the bulk of the patch is concerned with switching raw pointers to unique_ptr.<br>
><br>
> This is a significant change from the original Kaleidoscope tutorials which<br>
> deliberately avoided any memory management so as not to overcomplicate the<br>
> examples. I thought it was time to revisit this idea, since C++11 has removed<br>
> a lot of the complication of C++ memory management, and there's a benefit to<br>
> having the tutorial code more closely resemble the rest of the LLVM codebase. <br>
><br>
> Any thoughts/feedback on this direction?<br>
<br>
This is great - it isn't really harder to understand and it's that much<br>
closer to what the code you'll actually want to write after reading the<br>
tutorial. LGTM.<br>
<br>
It does look like this adds a long line or two though - do you mind<br>
clang-format'ing the patch? Actually, a lot of this code predates our<br>
formatting rules and you're touching half of the lines anyway, so it's<br>
probably reasonable to just clang-format the whole thing in a prep patch<br>
before this one if you feel like it.<br>
<br>
> I see this as a first step towards fully modernizing the Kaleidoscope<br>
> tutorials. My next step will be to update Chapter 4 onwards to bring them up<br>
> to speed with the recent JIT API changes - I've already started work on a<br>
> follow-up patch for this. If anybody else is interested in helping me bring<br>
> these tutorials up to speed let me know - assistance will very welcome.<br>
><br>
> - Lang.<br>
><br>
> Index: docs/tutorial/LangImpl1.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl1.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl1.rst (working copy)<br>
> @@ -25,7 +25,7 @@<br>
> about teaching compiler techniques and LLVM specifically, *not* about<br>
> teaching modern and sane software engineering principles. In practice,<br>
> this means that we'll take a number of shortcuts to simplify the<br>
> -exposition. For example, the code leaks memory, uses global variables<br>
> +exposition. For example, the code uses global variables<br>
> all over the place, doesn't use nice design patterns like<br>
> `visitors <<a href="http://en.wikipedia.org/wiki/Visitor_pattern" rel="noreferrer" target="_blank">http://en.wikipedia.org/wiki/Visitor_pattern</a>>`_, etc... but<br>
> it is very simple. If you dig in and use the code as a basis for future<br>
> Index: docs/tutorial/LangImpl2.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl2.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl2.rst (working copy)<br>
> @@ -45,7 +45,7 @@<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> };<br>
><br>
> The code above shows the definition of the base ExprAST class and one<br>
> @@ -66,16 +66,17 @@<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(char op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : Op(op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> @@ -83,8 +84,9 @@<br>
> std::string Callee;<br>
> std::vector<ExprAST*> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> };<br>
><br>
> This is all (intentionally) rather straight-forward: variables capture<br>
> @@ -110,17 +112,18 @@<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args)<br>
> - : Name(name), Args(args) {}<br>
> + PrototypeAST(const std::string &name, std::vector<std::string> Args)<br>
> + : Name(name), Args(std::move(Args)) {}<br>
> };<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body)<br>
> - : Proto(proto), Body(body) {}<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
> };<br>
><br>
> In Kaleidoscope, functions are typed with just a count of their<br>
> @@ -142,9 +145,10 @@<br>
><br>
> .. code-block:: c++<br>
><br>
> - ExprAST *X = new VariableExprAST("x");<br>
> - ExprAST *Y = new VariableExprAST("y");<br>
> - ExprAST *Result = new BinaryExprAST('+', X, Y);<br>
> + auto LHS = llvm::make_unique<VariableExprAST>("x");<br>
> + auto RHS = llvm::make_unique<VariableExprAST>("y");<br>
> + auto Result = std::make_unique<BinaryExprAST>('+', std::move(LHS),<br>
> + std::move(RHS));<br>
><br>
> In order to do this, we'll start by defining some basic helper routines:<br>
><br>
> @@ -190,10 +194,10 @@<br>
> .. code-block:: c++<br>
><br>
> /// numberexpr ::= number<br>
> - static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> + static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> This routine is very simple: it expects to be called when the current<br>
> @@ -211,10 +215,10 @@<br>
> .. code-block:: c++<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> - static ExprAST *ParseParenExpr() {<br>
> + static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> - if (!V) return 0;<br>
> + auto V = ParseExpression();<br>
> + if (!V) return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -250,22 +254,22 @@<br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> - static ExprAST *ParseIdentifierExpr() {<br>
> + static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST*> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg) return 0;<br>
> - Args.push_back(Arg);<br>
> + auto Arg = ParseExpression();<br>
> + if (!Arg) return nullptr;<br>
> + Args.push_back(std::move(Arg));<br>
><br>
> if (CurTok == ')') break;<br>
><br>
> @@ -278,7 +282,7 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> This routine follows the same style as the other routines. (It expects<br>
> @@ -303,7 +307,7 @@<br>
> /// ::= identifierexpr<br>
> /// ::= numberexpr<br>
> /// ::= parenexpr<br>
> - static ExprAST *ParsePrimary() {<br>
> + static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default: return Error("unknown token when expecting an expression");<br>
> case tok_identifier: return ParseIdentifierExpr();<br>
> @@ -390,11 +394,11 @@<br>
> /// expression<br>
> /// ::= primary binoprhs<br>
> ///<br>
> - static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParsePrimary();<br>
> - if (!LHS) return 0;<br>
> + static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParsePrimary();<br>
> + if (!LHS) return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> ``ParseBinOpRHS`` is the function that parses the sequence of pairs for<br>
> @@ -416,7 +420,8 @@<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' primary)*<br>
> - static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> + static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,<br>
> + std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -440,8 +445,8 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the primary expression after the binary operator.<br>
> - ExprAST *RHS = ParsePrimary();<br>
> - if (!RHS) return 0;<br>
> + auto RHS = ParsePrimary();<br>
> + if (!RHS) return nullptr;<br>
><br>
> As such, this code eats (and remembers) the binary operator and then<br>
> parses the primary expression that follows. This builds up the whole<br>
> @@ -474,7 +479,8 @@<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> } // loop around to the top of the while loop.<br>
> }<br>
><br>
> @@ -498,11 +504,12 @@<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec+1, RHS);<br>
> + RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));<br>
> if (RHS == 0) return 0;<br>
> }<br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> } // loop around to the top of the while loop.<br>
> }<br>
><br>
> @@ -541,7 +548,7 @@<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> - static PrototypeAST *ParsePrototype() {<br>
> + static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> if (CurTok != tok_identifier)<br>
> return ErrorP("Expected function name in prototype");<br>
><br>
> @@ -561,7 +568,7 @@<br>
> // success.<br>
> getNextToken(); // eat ')'.<br>
><br>
> - return new PrototypeAST(FnName, ArgNames);<br>
> + return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));<br>
> }<br>
><br>
> Given this, a function definition is very simple, just a prototype plus<br>
> @@ -570,14 +577,14 @@<br>
> .. code-block:: c++<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> - static FunctionAST *ParseDefinition() {<br>
> + static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0) return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto) return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> In addition, we support 'extern' to declare functions like 'sin' and<br>
> @@ -587,7 +594,7 @@<br>
> .. code-block:: c++<br>
><br>
> /// external ::= 'extern' prototype<br>
> - static PrototypeAST *ParseExtern() {<br>
> + static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -599,13 +606,13 @@<br>
> .. code-block:: c++<br>
><br>
> /// toplevelexpr ::= expression<br>
> - static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> + static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> Now that we have all the pieces, let's build a little driver that will<br>
> Index: docs/tutorial/LangImpl3.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl3.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl3.rst (working copy)<br>
> @@ -42,7 +42,7 @@<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> virtual Value *Codegen();<br>
> };<br>
> ...<br>
> Index: docs/tutorial/LangImpl4.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl4.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl4.rst (working copy)<br>
> @@ -260,12 +260,12 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> - LF->dump(); // Dump the function for exposition purposes.<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> + FnIR->dump(); // Dump the function for exposition purposes.<br>
><br>
> // JIT the function, returning a function pointer.<br>
> - void *FPtr = TheExecutionEngine->getPointerToFunction(LF);<br>
> + void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);<br>
><br>
> // Cast it to the right type (takes no arguments, returns a double) so we<br>
> // can call it as a native function.<br>
> Index: docs/tutorial/LangImpl5.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl5.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl5.rst (working copy)<br>
> @@ -90,10 +90,11 @@<br>
><br>
> /// IfExprAST - Expression class for if/then/else.<br>
> class IfExprAST : public ExprAST {<br>
> - ExprAST *Cond, *Then, *Else;<br>
> + std::unique<ExprAST> Cond, Then, Else;<br>
> public:<br>
> - IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)<br>
> - : Cond(cond), Then(then), Else(_else) {}<br>
> + IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,<br>
> + std::unique_ptr<ExprAST> Else)<br>
> + : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}<br>
> virtual Value *Codegen();<br>
> };<br>
><br>
> @@ -109,36 +110,37 @@<br>
> .. code-block:: c++<br>
><br>
> /// ifexpr ::= 'if' expression 'then' expression 'else' expression<br>
> - static ExprAST *ParseIfExpr() {<br>
> + static std::unique_ptr<ExprAST> ParseIfExpr() {<br>
> getNextToken(); // eat the if.<br>
><br>
> // condition.<br>
> - ExprAST *Cond = ParseExpression();<br>
> - if (!Cond) return 0;<br>
> + auto Cond = ParseExpression();<br>
> + if (!Cond) return nullptr;<br>
><br>
> if (CurTok != tok_then)<br>
> return Error("expected then");<br>
> getNextToken(); // eat the then<br>
><br>
> - ExprAST *Then = ParseExpression();<br>
> - if (Then == 0) return 0;<br>
> + auto Then = ParseExpression();<br>
> + if (Then) return nullptr;<br>
><br>
> if (CurTok != tok_else)<br>
> return Error("expected else");<br>
><br>
> getNextToken();<br>
><br>
> - ExprAST *Else = ParseExpression();<br>
> - if (!Else) return 0;<br>
> + auto Else = ParseExpression();<br>
> + if (!Else) return nullptr;<br>
><br>
> - return new IfExprAST(Cond, Then, Else);<br>
> + return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),<br>
> + std::move(Else));<br>
> }<br>
><br>
> Next we hook it up as a primary expression:<br>
><br>
> .. code-block:: c++<br>
><br>
> - static ExprAST *ParsePrimary() {<br>
> + static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default: return Error("unknown token when expecting an expression");<br>
> case tok_identifier: return ParseIdentifierExpr();<br>
> @@ -269,7 +271,7 @@<br>
><br>
> Value *IfExprAST::Codegen() {<br>
> Value *CondV = Cond->Codegen();<br>
> - if (CondV == 0) return 0;<br>
> + if (!CondV) return nullptr;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> CondV = Builder.CreateFCmpONE(CondV,<br>
> @@ -464,11 +466,13 @@<br>
> /// ForExprAST - Expression class for for/in.<br>
> class ForExprAST : public ExprAST {<br>
> std::string VarName;<br>
> - ExprAST *Start, *End, *Step, *Body;<br>
> + std::unique_ptr<ExprAST> Start, End, Step, Body;<br>
> public:<br>
> - ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,<br>
> - ExprAST *step, ExprAST *body)<br>
> - : VarName(varname), Start(start), End(end), Step(step), Body(body) {}<br>
> + ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,<br>
> + std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarName(VarName), Start(std::move(Start)), End(std::move(End)),<br>
> + Step(std::move(Step)), Body(std::move(Body)) {}<br>
> virtual Value *Codegen();<br>
> };<br>
><br>
> @@ -483,7 +487,7 @@<br>
> .. code-block:: c++<br>
><br>
> /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression<br>
> - static ExprAST *ParseForExpr() {<br>
> + static std::unique_ptr<ExprAST> ParseForExpr() {<br>
> getNextToken(); // eat the for.<br>
><br>
> if (CurTok != tok_identifier)<br>
> @@ -497,31 +501,33 @@<br>
> getNextToken(); // eat '='.<br>
><br>
><br>
> - ExprAST *Start = ParseExpression();<br>
> - if (Start == 0) return 0;<br>
> + auto Start = ParseExpression();<br>
> + if (!Start) return nullptr;<br>
> if (CurTok != ',')<br>
> return Error("expected ',' after for start value");<br>
> getNextToken();<br>
><br>
> - ExprAST *End = ParseExpression();<br>
> - if (End == 0) return 0;<br>
> + auto End = ParseExpression();<br>
> + if (!End) return nullptr;<br>
><br>
> // The step value is optional.<br>
> - ExprAST *Step = 0;<br>
> + std::unique_ptr<ExprAST> Step;<br>
> if (CurTok == ',') {<br>
> getNextToken();<br>
> Step = ParseExpression();<br>
> - if (Step == 0) return 0;<br>
> + if (!Step) return nullptr;<br>
> }<br>
><br>
> if (CurTok != tok_in)<br>
> return Error("expected 'in' after for");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0) return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body) return nullptr;<br>
><br>
> - return new ForExprAST(IdName, Start, End, Step, Body);<br>
> + return llvm::make_unique<ForExprAST>(IdName, std::move(Start),<br>
> + std::move(End), std::move(Step),<br>
> + std::move(Body));<br>
> }<br>
><br>
> LLVM IR for the 'for' Loop<br>
> Index: docs/tutorial/LangImpl6.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl6.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl6.rst (working copy)<br>
> @@ -129,15 +129,16 @@<br>
> class PrototypeAST {<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> - bool isOperator;<br>
> + bool IsOperator;<br>
> unsigned Precedence; // Precedence if a binary op.<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args,<br>
> - bool isoperator = false, unsigned prec = 0)<br>
> - : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}<br>
> + PrototypeAST(const std::string &name, std::vector<std::string> Args,<br>
> + bool IsOperator = false, unsigned Prec = 0)<br>
> + : Name(name), Args(std::move(Args)), IsOperator(IsOperator),<br>
> + Precedence(Prec) {}<br>
><br>
> - bool isUnaryOp() const { return isOperator && Args.size() == 1; }<br>
> - bool isBinaryOp() const { return isOperator && Args.size() == 2; }<br>
> + bool isUnaryOp() const { return IsOperator && Args.size() == 1; }<br>
> + bool isBinaryOp() const { return IsOperator && Args.size() == 2; }<br>
><br>
> char getOperatorName() const {<br>
> assert(isUnaryOp() || isBinaryOp());<br>
> @@ -161,7 +162,7 @@<br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> /// ::= binary LETTER number? (id, id)<br>
> - static PrototypeAST *ParsePrototype() {<br>
> + static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> std::string FnName;<br>
><br>
> unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.<br>
> @@ -210,7 +211,8 @@<br>
> if (Kind && ArgNames.size() != Kind)<br>
> return ErrorP("Invalid number of operands for operator");<br>
><br>
> - return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);<br>
> + return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames),<br>
> + Kind != 0, BinaryPrecedence);<br>
> }<br>
><br>
> This is all fairly straightforward parsing code, and we have already<br>
> @@ -305,10 +307,10 @@<br>
> /// UnaryExprAST - Expression class for a unary operator.<br>
> class UnaryExprAST : public ExprAST {<br>
> char Opcode;<br>
> - ExprAST *Operand;<br>
> + std::unique_ptr<ExprAST> Operand;<br>
> public:<br>
> - UnaryExprAST(char opcode, ExprAST *operand)<br>
> - : Opcode(opcode), Operand(operand) {}<br>
> + UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)<br>
> + : Opcode(Opcode), Operand(std::move(Operand)) {}<br>
> virtual Value *Codegen();<br>
> };<br>
><br>
> @@ -322,7 +324,7 @@<br>
> /// unary<br>
> /// ::= primary<br>
> /// ::= '!' unary<br>
> - static ExprAST *ParseUnary() {<br>
> + static std::unique_ptr<ExprAST> ParseUnary() {<br>
> // If the current token is not an operator, it must be a primary expr.<br>
> if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')<br>
> return ParsePrimary();<br>
> @@ -330,9 +332,9 @@<br>
> // If this is a unary operator, read it.<br>
> int Opc = CurTok;<br>
> getNextToken();<br>
> - if (ExprAST *Operand = ParseUnary())<br>
> - return new UnaryExprAST(Opc, Operand);<br>
> - return 0;<br>
> + if (auto Operand = ParseUnary())<br>
> + return llvm::unique_ptr<UnaryExprAST>(Opc, std::move(Operand));<br>
> + return nullptr;<br>
> }<br>
><br>
> The grammar we add is pretty straightforward here. If we see a unary<br>
> @@ -350,21 +352,22 @@<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' unary)*<br>
> - static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> + static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,<br>
> + std::unique_ptr<ExprAST> LHS) {<br>
> ...<br>
> // Parse the unary expression after the binary operator.<br>
> - ExprAST *RHS = ParseUnary();<br>
> - if (!RHS) return 0;<br>
> + auto RHS = ParseUnary();<br>
> + if (!RHS) return nullptr;<br>
> ...<br>
> }<br>
> /// expression<br>
> /// ::= unary binoprhs<br>
> ///<br>
> - static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParseUnary();<br>
> - if (!LHS) return 0;<br>
> + static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParseUnary();<br>
> + if (!LHS) return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> With these two simple changes, we are now able to parse unary operators<br>
> @@ -378,7 +381,7 @@<br>
> /// ::= id '(' id* ')'<br>
> /// ::= binary LETTER number? (id, id)<br>
> /// ::= unary LETTER (id)<br>
> - static PrototypeAST *ParsePrototype() {<br>
> + static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> std::string FnName;<br>
><br>
> unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.<br>
> Index: docs/tutorial/LangImpl7.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl7.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl7.rst (working copy)<br>
> @@ -573,7 +573,7 @@<br>
> // Special case '=' because we don't want to emit the LHS as an expression.<br>
> if (Op == '=') {<br>
> // Assignment requires the LHS to be an identifier.<br>
> - VariableExprAST *LHSE = dynamic_cast<VariableExprAST*>(LHS);<br>
> + VariableExprAST *LHSE = dynamic_cast<VariableExprAST*>(LHS.get());<br>
> if (!LHSE)<br>
> return ErrorV("destination of '=' must be a variable");<br>
><br>
> @@ -663,12 +663,12 @@<br>
><br>
> /// VarExprAST - Expression class for var/in<br>
> class VarExprAST : public ExprAST {<br>
> - std::vector<std::pair<std::string, ExprAST*> > VarNames;<br>
> - ExprAST *Body;<br>
> + std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,<br>
> - ExprAST *body)<br>
> - : VarNames(varnames), Body(body) {}<br>
> + VarExprAST(std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,<br>
> + std::unique_ptr<ExprAST> body)<br>
> + : VarNames(std::move(VarNames)), Body(std::move(Body)) {}<br>
><br>
> virtual Value *Codegen();<br>
> };<br>
> @@ -690,7 +690,7 @@<br>
> /// ::= ifexpr<br>
> /// ::= forexpr<br>
> /// ::= varexpr<br>
> - static ExprAST *ParsePrimary() {<br>
> + static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default: return Error("unknown token when expecting an expression");<br>
> case tok_identifier: return ParseIdentifierExpr();<br>
> @@ -708,10 +708,10 @@<br>
><br>
> /// varexpr ::= 'var' identifier ('=' expression)?<br>
> // (',' identifier ('=' expression)?)* 'in' expression<br>
> - static ExprAST *ParseVarExpr() {<br>
> + static std::unique_ptr<ExprAST> ParseVarExpr() {<br>
> getNextToken(); // eat the var.<br>
><br>
> - std::vector<std::pair<std::string, ExprAST*> > VarNames;<br>
> + std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;<br>
><br>
> // At least one variable name is required.<br>
> if (CurTok != tok_identifier)<br>
> @@ -727,15 +727,15 @@<br>
> getNextToken(); // eat identifier.<br>
><br>
> // Read the optional initializer.<br>
> - ExprAST *Init = 0;<br>
> + std::unique_ptr<ExprAST> Init;<br>
> if (CurTok == '=') {<br>
> getNextToken(); // eat the '='.<br>
><br>
> Init = ParseExpression();<br>
> - if (Init == 0) return 0;<br>
> + if (!Init) return nullptr;<br>
> }<br>
><br>
> - VarNames.push_back(std::make_pair(Name, Init));<br>
> + VarNames.push_back(std::make_pair(Name, std::move(Init)));<br>
><br>
> // End of var list, exit loop.<br>
> if (CurTok != ',') break;<br>
> @@ -755,10 +755,11 @@<br>
> return Error("expected 'in' keyword after 'var'");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0) return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body) return nullptr;<br>
><br>
> - return new VarExprAST(VarNames, Body);<br>
> + return llvm::make_unique<VarExprAST>(std::move(VarNames),<br>
> + std::move(Body));<br>
> }<br>
><br>
> Now that we can parse and represent the code, we need to support<br>
> @@ -774,7 +775,7 @@<br>
> // Register all variables and emit their initializer.<br>
> for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {<br>
> const std::string &VarName = VarNames[i].first;<br>
> - ExprAST *Init = VarNames[i].second;<br>
> + ExprAST *Init = VarNames[i].second.get();<br>
><br>
> Basically it loops over all the variables, installing them one at a<br>
> time. For each variable we put into the symbol table, we remember the<br>
> Index: docs/tutorial/LangImpl8.rst<br>
> ===================================================================<br>
> --- docs/tutorial/LangImpl8.rst (revision 245236)<br>
> +++ docs/tutorial/LangImpl8.rst (working copy)<br>
> @@ -75,8 +75,8 @@<br>
><br>
> .. code-block:: udiff<br>
><br>
> - - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - + PrototypeAST *Proto = new PrototypeAST("main", std::vector<std::string>());<br>
> + - auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());<br>
> + + auto Proto = llvm::make_unique<PrototypeAST>("main", std::vector<std::string>());<br>
><br>
> just with the simple change of giving it a name.<br>
><br>
> @@ -108,12 +108,12 @@<br>
> @@ -1108,17 +1108,8 @@ static void HandleExtern() {<br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + - if (auto *FnIR = FnAST->Codegen()) {<br>
> - // We're just doing this to make sure it executes.<br>
> - TheExecutionEngine->finalizeObject();<br>
> - // JIT the function, returning a function pointer.<br>
> - - void *FPtr = TheExecutionEngine->getPointerToFunction(LF);<br>
> + - void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);<br>
> -<br>
> - // Cast it to the right type (takes no arguments, returns a double) so we<br>
> - // can call it as a native function.<br>
> @@ -318,7 +318,8 @@<br>
><br>
> .. code-block:: c++<br>
><br>
> - LHS = new BinaryExprAST(BinLoc, BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinLoc, BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
><br>
> giving us locations for each of our expressions and variables.<br>
><br>
> Index: examples/Kaleidoscope/Chapter2/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter2/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter2/toy.cpp (working copy)<br>
> @@ -1,6 +1,6 @@<br>
> +#include "llvm/ADT/STLExtras.h"<br>
> #include <cctype><br>
> #include <cstdio><br>
> -#include <cstdlib><br>
> #include <map><br>
> #include <string><br>
> #include <vector><br>
> @@ -85,29 +85,31 @@<br>
> /// NumberExprAST - Expression class for numeric literals like "1.0".<br>
> class NumberExprAST : public ExprAST {<br>
> public:<br>
> - NumberExprAST(double val) {}<br>
> + NumberExprAST(double Val) {}<br>
> };<br>
><br>
> /// VariableExprAST - Expression class for referencing a variable, like "a".<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) {}<br>
> + BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS) {}<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST*> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> };<br>
><br>
> /// PrototypeAST - This class represents the "prototype" for a function,<br>
> @@ -117,15 +119,16 @@<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args)<br>
> - : Name(name), Args(args) {}<br>
> + PrototypeAST(const std::string &Name, std::vector<std::string> Args)<br>
> + : Name(Name), Args(std::move(Args)) {}<br>
><br>
> };<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body) {}<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto,<br>
> + std::unique_ptr<ExprAST> Body) {}<br>
> };<br>
> } // end anonymous namespace<br>
><br>
> @@ -157,30 +160,37 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}<br>
> -PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> + fprintf(stderr, "Error: %s\n", Str);<br>
> + return nullptr;<br>
> +}<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> + Error(Str);<br>
> + return nullptr;<br>
> +}<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST*> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg) return 0;<br>
> - Args.push_back(Arg);<br>
> + if (auto Arg = ParseExpression())<br>
> + Args.push_back(std::move(Arg));<br>
> + else<br>
> + return nullptr;<br>
><br>
> if (CurTok == ')') break;<br>
><br>
> @@ -193,21 +203,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> - if (!V) return 0;<br>
> + auto V = ParseExpression();<br>
> + if (!V)<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -219,7 +230,7 @@<br>
> /// ::= identifierexpr<br>
> /// ::= numberexpr<br>
> /// ::= parenexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default: return Error("unknown token when expecting an expression");<br>
> case tok_identifier: return ParseIdentifierExpr();<br>
> @@ -230,7 +241,8 @@<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' primary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> +static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,<br>
> + std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -245,35 +257,36 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the primary expression after the binary operator.<br>
> - ExprAST *RHS = ParsePrimary();<br>
> - if (!RHS) return 0;<br>
> + auto RHS = ParsePrimary();<br>
> + if (!RHS) return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec+1, RHS);<br>
> - if (RHS == 0) return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));<br>
> + if (!RHS) return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= primary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParsePrimary();<br>
> - if (!LHS) return 0;<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParsePrimary();<br>
> + if (!LHS) return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> if (CurTok != tok_identifier)<br>
> return ErrorP("Expected function name in prototype");<br>
><br>
> @@ -292,32 +305,34 @@<br>
> // success.<br>
> getNextToken(); // eat ')'.<br>
><br>
> - return new PrototypeAST(FnName, ArgNames);<br>
> + return llvm::make_unique<PrototypeAST>(std::move(FnName),<br>
> + std::move(ArgNames));<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0) return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto) return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("",<br>
> + std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> Index: examples/Kaleidoscope/Chapter3/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter3/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter3/toy.cpp (working copy)<br>
> @@ -1,3 +1,4 @@<br>
> +#include "llvm/ADT/STLExtras.h"<br>
> #include "llvm/IR/Verifier.h"<br>
> #include "llvm/IR/DerivedTypes.h"<br>
> #include "llvm/IR/IRBuilder.h"<br>
> @@ -92,7 +93,7 @@<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> @@ -100,27 +101,29 @@<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST*> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> @@ -131,19 +134,20 @@<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args)<br>
> - : Name(name), Args(args) {}<br>
> + PrototypeAST(const std::string &Name, std::vector<std::string> Args)<br>
> + : Name(Name), Args(std::move(Args)) {}<br>
><br>
> Function *Codegen();<br>
> };<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body)<br>
> - : Proto(proto), Body(body) {}<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
><br>
> Function *Codegen();<br>
> };<br>
> @@ -177,31 +181,41 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}<br>
> -PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }<br>
> -FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> + fprintf(stderr, "Error: %s\n", Str);<br>
> + return nullptr;<br>
> +}<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> + Error(Str);<br>
> + return nullptr;<br>
> +}<br>
> +std::unique_ptr<FunctionAST> ErrorF(const char *Str) {<br>
> + Error(Str);<br>
> + return nullptr;<br>
> +}<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST*> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg) return 0;<br>
> - Args.push_back(Arg);<br>
> + if (auto Arg = ParseExpression())<br>
> + Args.push_back(std::move(Arg));<br>
> + else<br>
> + return nullptr;<br>
><br>
> if (CurTok == ')') break;<br>
><br>
> @@ -214,21 +228,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> - if (!V) return 0;<br>
> + auto V = ParseExpression();<br>
> + if (!V)<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -240,7 +255,7 @@<br>
> /// ::= identifierexpr<br>
> /// ::= numberexpr<br>
> /// ::= parenexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default: return Error("unknown token when expecting an expression");<br>
> case tok_identifier: return ParseIdentifierExpr();<br>
> @@ -251,7 +266,8 @@<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' primary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> +static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,<br>
> + std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -266,35 +282,36 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the primary expression after the binary operator.<br>
> - ExprAST *RHS = ParsePrimary();<br>
> - if (!RHS) return 0;<br>
> + auto RHS = ParsePrimary();<br>
> + if (!RHS) return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec+1, RHS);<br>
> - if (RHS == 0) return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));<br>
> + if (!RHS) return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= primary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParsePrimary();<br>
> - if (!LHS) return 0;<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParsePrimary();<br>
> + if (!LHS) return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> if (CurTok != tok_identifier)<br>
> return ErrorP("Expected function name in prototype");<br>
><br>
> @@ -313,32 +330,34 @@<br>
> // success.<br>
> getNextToken(); // eat ')'.<br>
><br>
> - return new PrototypeAST(FnName, ArgNames);<br>
> + return llvm::make_unique<PrototypeAST>(std::move(FnName),<br>
> + std::move(ArgNames));<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0) return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto) return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("",<br>
> + std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -351,7 +370,7 @@<br>
> static IRBuilder<> Builder(getGlobalContext());<br>
> static std::map<std::string, Value*> NamedValues;<br>
><br>
> -Value *ErrorV(const char *Str) { Error(Str); return 0; }<br>
> +Value *ErrorV(const char *Str) { Error(Str); return nullptr; }<br>
><br>
> Value *NumberExprAST::Codegen() {<br>
> return ConstantFP::get(getGlobalContext(), APFloat(Val));<br>
> @@ -366,7 +385,7 @@<br>
> Value *BinaryExprAST::Codegen() {<br>
> Value *L = LHS->Codegen();<br>
> Value *R = RHS->Codegen();<br>
> - if (L == 0 || R == 0) return 0;<br>
> + if (!L || !R) return nullptr;<br>
><br>
> switch (Op) {<br>
> case '+': return Builder.CreateFAdd(L, R, "addtmp");<br>
> @@ -384,7 +403,7 @@<br>
> Value *CallExprAST::Codegen() {<br>
> // Look up the name in the global module table.<br>
> Function *CalleeF = TheModule->getFunction(Callee);<br>
> - if (CalleeF == 0)<br>
> + if (!CalleeF)<br>
> return ErrorV("Unknown function referenced");<br>
><br>
> // If argument mismatch error.<br>
> @@ -394,7 +413,7 @@<br>
> std::vector<Value*> ArgsV;<br>
> for (unsigned i = 0, e = Args.size(); i != e; ++i) {<br>
> ArgsV.push_back(Args[i]->Codegen());<br>
> - if (ArgsV.back() == 0) return 0;<br>
> + if (!ArgsV.back()) return nullptr;<br>
> }<br>
><br>
> return Builder.CreateCall(CalleeF, ArgsV, "calltmp");<br>
> @@ -407,7 +426,8 @@<br>
> FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),<br>
> Doubles, false);<br>
><br>
> - Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);<br>
> + Function *F = Function::Create(FT, Function::ExternalLinkage, Name,<br>
> + TheModule);<br>
><br>
> // If F conflicted, there was already something named 'Name'. If it has a<br>
> // body, don't allow redefinition or reextern.<br>
> @@ -419,13 +439,13 @@<br>
> // If F already has a body, reject this.<br>
> if (!F->empty()) {<br>
> ErrorF("redefinition of function");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If F took a different number of args, reject.<br>
> if (F->arg_size() != Args.size()) {<br>
> ErrorF("redefinition of function with different # args");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> }<br>
><br>
> @@ -446,8 +466,8 @@<br>
> NamedValues.clear();<br>
><br>
> Function *TheFunction = Proto->Codegen();<br>
> - if (TheFunction == 0)<br>
> - return 0;<br>
> + if (!TheFunction)<br>
> + return nullptr;<br>
><br>
> // Create a new basic block to start insertion into.<br>
> BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);<br>
> @@ -465,7 +485,7 @@<br>
><br>
> // Error reading body, remove function.<br>
> TheFunction->eraseFromParent();<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> //===----------------------------------------------------------------------===//<br>
> @@ -473,10 +493,10 @@<br>
> //===----------------------------------------------------------------------===//<br>
><br>
> static void HandleDefinition() {<br>
> - if (FunctionAST *F = ParseDefinition()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseDefinition()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> fprintf(stderr, "Read function definition:");<br>
> - LF->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -485,10 +505,10 @@<br>
> }<br>
><br>
> static void HandleExtern() {<br>
> - if (PrototypeAST *P = ParseExtern()) {<br>
> - if (Function *F = P->Codegen()) {<br>
> + if (auto ProtoAST = ParseExtern()) {<br>
> + if (auto *FnIR = ProtoAST->Codegen()) {<br>
> fprintf(stderr, "Read extern: ");<br>
> - F->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -498,10 +518,10 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> fprintf(stderr, "Read top-level expression:");<br>
> - LF->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -553,7 +573,8 @@<br>
> getNextToken();<br>
><br>
> // Make the module, which holds all the code.<br>
> - TheModule = new Module("my cool jit", Context);<br>
> + std::unique_ptr<Module> Owner = llvm::make_unique<Module>("my cool jit", Context);<br>
> + TheModule = Owner.get();<br>
><br>
> // Run the main "interpreter loop" now.<br>
> MainLoop();<br>
> Index: examples/Kaleidoscope/Chapter4/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter4/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter4/toy.cpp (working copy)<br>
> @@ -1,3 +1,4 @@<br>
> +#include "llvm/ADT/STLExtras.h"<br>
> #include "llvm/Analysis/BasicAliasAnalysis.h"<br>
> #include "llvm/Analysis/Passes.h"<br>
> #include "llvm/ExecutionEngine/ExecutionEngine.h"<br>
> @@ -105,40 +106,38 @@<br>
> /// NumberExprAST - Expression class for numeric literals like "1.0".<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> -<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// VariableExprAST - Expression class for referencing a variable, like "a".<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> -<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> -<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST *> Args;<br>
> -<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> @@ -148,22 +147,20 @@<br>
> class PrototypeAST {<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> -<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args)<br>
> - : Name(name), Args(args) {}<br>
> -<br>
> + PrototypeAST(const std::string &name, std::vector<std::string> Args)<br>
> + : Name(name), Args(std::move(Args)) {}<br>
> Function *Codegen();<br>
> };<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}<br>
> -<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
> Function *Codegen();<br>
> };<br>
> } // end anonymous namespace<br>
> @@ -195,41 +192,41 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) {<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> fprintf(stderr, "Error: %s\n", Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -PrototypeAST *ErrorP(const char *Str) {<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -FunctionAST *ErrorF(const char *Str) {<br>
> +std::unique_ptr<FunctionAST> ErrorF(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST *> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg)<br>
> - return 0;<br>
> - Args.push_back(Arg);<br>
> + if (auto Arg = ParseExpression())<br>
> + Args.push_back(std::move(Arg));<br>
> + else<br>
> + return nullptr;<br>
><br>
> if (CurTok == ')')<br>
> break;<br>
> @@ -243,22 +240,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> + auto V = ParseExpression();<br>
> if (!V)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -270,7 +267,7 @@<br>
> /// ::= identifierexpr<br>
> /// ::= numberexpr<br>
> /// ::= parenexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default:<br>
> return Error("unknown token when expecting an expression");<br>
> @@ -285,7 +282,8 @@<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' primary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> +static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,<br>
> + std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -300,38 +298,39 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the primary expression after the binary operator.<br>
> - ExprAST *RHS = ParsePrimary();<br>
> + auto RHS = ParsePrimary();<br>
> if (!RHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec + 1, RHS);<br>
> - if (RHS == 0)<br>
> - return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));<br>
> + if (!RHS)<br>
> + return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= primary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParsePrimary();<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParsePrimary();<br>
> if (!LHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> if (CurTok != tok_identifier)<br>
> return ErrorP("Expected function name in prototype");<br>
><br>
> @@ -350,33 +349,34 @@<br>
> // success.<br>
> getNextToken(); // eat ')'.<br>
><br>
> - return new PrototypeAST(FnName, ArgNames);<br>
> + return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0)<br>
> - return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto)<br>
> + return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("",<br>
> + std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -505,7 +505,7 @@<br>
> Function *PF = OpenModule->getFunction(FnName);<br>
> if (PF && !PF->empty()) {<br>
> ErrorF("redefinition of function across modules");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If we don't have a prototype yet, create one.<br>
> @@ -626,7 +626,7 @@<br>
><br>
> Value *ErrorV(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> Value *NumberExprAST::Codegen() {<br>
> @@ -642,8 +642,8 @@<br>
> Value *BinaryExprAST::Codegen() {<br>
> Value *L = LHS->Codegen();<br>
> Value *R = RHS->Codegen();<br>
> - if (L == 0 || R == 0)<br>
> - return 0;<br>
> + if (!L || !R)<br>
> + return nullptr;<br>
><br>
> switch (Op) {<br>
> case '+':<br>
> @@ -665,7 +665,7 @@<br>
> Value *CallExprAST::Codegen() {<br>
> // Look up the name in the global module table.<br>
> Function *CalleeF = JITHelper->getFunction(Callee);<br>
> - if (CalleeF == 0)<br>
> + if (!CalleeF)<br>
> return ErrorV("Unknown function referenced");<br>
><br>
> // If argument mismatch error.<br>
> @@ -675,8 +675,8 @@<br>
> std::vector<Value *> ArgsV;<br>
> for (unsigned i = 0, e = Args.size(); i != e; ++i) {<br>
> ArgsV.push_back(Args[i]->Codegen());<br>
> - if (ArgsV.back() == 0)<br>
> - return 0;<br>
> + if (!ArgsV.back())<br>
> + return nullptr;<br>
> }<br>
><br>
> return Builder.CreateCall(CalleeF, ArgsV, "calltmp");<br>
> @@ -704,13 +704,13 @@<br>
> // If F already has a body, reject this.<br>
> if (!F->empty()) {<br>
> ErrorF("redefinition of function");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If F took a different number of args, reject.<br>
> if (F->arg_size() != Args.size()) {<br>
> ErrorF("redefinition of function with different # args");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> }<br>
><br>
> @@ -731,8 +731,8 @@<br>
> NamedValues.clear();<br>
><br>
> Function *TheFunction = Proto->Codegen();<br>
> - if (TheFunction == 0)<br>
> - return 0;<br>
> + if (!TheFunction)<br>
> + return nullptr;<br>
><br>
> // Create a new basic block to start insertion into.<br>
> BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);<br>
> @@ -750,7 +750,7 @@<br>
><br>
> // Error reading body, remove function.<br>
> TheFunction->eraseFromParent();<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> //===----------------------------------------------------------------------===//<br>
> @@ -758,10 +758,10 @@<br>
> //===----------------------------------------------------------------------===//<br>
><br>
> static void HandleDefinition() {<br>
> - if (FunctionAST *F = ParseDefinition()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseDefinition()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> fprintf(stderr, "Read function definition:");<br>
> - LF->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -770,10 +770,10 @@<br>
> }<br>
><br>
> static void HandleExtern() {<br>
> - if (PrototypeAST *P = ParseExtern()) {<br>
> - if (Function *F = P->Codegen()) {<br>
> + if (auto ProtoAST = ParseExtern()) {<br>
> + if (auto *FnIR = ProtoAST->Codegen()) {<br>
> fprintf(stderr, "Read extern: ");<br>
> - F->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -783,10 +783,10 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> // JIT the function, returning a function pointer.<br>
> - void *FPtr = JITHelper->getPointerToFunction(LF);<br>
> + void *FPtr = JITHelper->getPointerToFunction(FnIR);<br>
><br>
> // Cast it to the right type (takes no arguments, returns a double) so we<br>
> // can call it as a native function.<br>
> Index: examples/Kaleidoscope/Chapter5/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter5/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter5/toy.cpp (working copy)<br>
> @@ -123,62 +123,61 @@<br>
> /// NumberExprAST - Expression class for numeric literals like "1.0".<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> -<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// VariableExprAST - Expression class for referencing a variable, like "a".<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> -<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> -<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST *> Args;<br>
> -<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// IfExprAST - Expression class for if/then/else.<br>
> class IfExprAST : public ExprAST {<br>
> - ExprAST *Cond, *Then, *Else;<br>
> -<br>
> + std::unique_ptr<ExprAST> Cond, Then, Else;<br>
> public:<br>
> - IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)<br>
> - : Cond(cond), Then(then), Else(_else) {}<br>
> + IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,<br>
> + std::unique_ptr<ExprAST> Else)<br>
> + : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// ForExprAST - Expression class for for/in.<br>
> class ForExprAST : public ExprAST {<br>
> std::string VarName;<br>
> - ExprAST *Start, *End, *Step, *Body;<br>
> -<br>
> + std::unique_ptr<ExprAST> Start, End, Step, Body;<br>
> public:<br>
> - ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,<br>
> - ExprAST *step, ExprAST *body)<br>
> - : VarName(varname), Start(start), End(end), Step(step), Body(body) {}<br>
> + ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,<br>
> + std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarName(VarName), Start(std::move(Start)), End(std::move(End)),<br>
> + Step(std::move(Step)), Body(std::move(Body)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> @@ -188,22 +187,20 @@<br>
> class PrototypeAST {<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> -<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args)<br>
> - : Name(name), Args(args) {}<br>
> -<br>
> + PrototypeAST(const std::string &name, std::vector<std::string> Args)<br>
> + : Name(name), Args(std::move(Args)) {}<br>
> Function *Codegen();<br>
> };<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}<br>
> -<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
> Function *Codegen();<br>
> };<br>
> } // end anonymous namespace<br>
> @@ -235,41 +232,41 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) {<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> fprintf(stderr, "Error: %s\n", Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -PrototypeAST *ErrorP(const char *Str) {<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -FunctionAST *ErrorF(const char *Str) {<br>
> +std::unique_ptr<FunctionAST> ErrorF(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST *> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg)<br>
> - return 0;<br>
> - Args.push_back(Arg);<br>
> + if (auto Arg = ParseExpression())<br>
> + Args.push_back(std::move(Arg));<br>
> + else<br>
> + return nullptr;<br>
><br>
> if (CurTok == ')')<br>
> break;<br>
> @@ -283,22 +280,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> + auto V = ParseExpression();<br>
> if (!V)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -307,36 +304,37 @@<br>
> }<br>
><br>
> /// ifexpr ::= 'if' expression 'then' expression 'else' expression<br>
> -static ExprAST *ParseIfExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIfExpr() {<br>
> getNextToken(); // eat the if.<br>
><br>
> // condition.<br>
> - ExprAST *Cond = ParseExpression();<br>
> + auto Cond = ParseExpression();<br>
> if (!Cond)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_then)<br>
> return Error("expected then");<br>
> getNextToken(); // eat the then<br>
><br>
> - ExprAST *Then = ParseExpression();<br>
> - if (Then == 0)<br>
> - return 0;<br>
> + auto Then = ParseExpression();<br>
> + if (!Then)<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_else)<br>
> return Error("expected else");<br>
><br>
> getNextToken();<br>
><br>
> - ExprAST *Else = ParseExpression();<br>
> + auto Else = ParseExpression();<br>
> if (!Else)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return new IfExprAST(Cond, Then, Else);<br>
> + return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),<br>
> + std::move(Else));<br>
> }<br>
><br>
> /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression<br>
> -static ExprAST *ParseForExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseForExpr() {<br>
> getNextToken(); // eat the for.<br>
><br>
> if (CurTok != tok_identifier)<br>
> @@ -349,35 +347,36 @@<br>
> return Error("expected '=' after for");<br>
> getNextToken(); // eat '='.<br>
><br>
> - ExprAST *Start = ParseExpression();<br>
> - if (Start == 0)<br>
> - return 0;<br>
> + auto Start = ParseExpression();<br>
> + if (!Start)<br>
> + return nullptr;<br>
> if (CurTok != ',')<br>
> return Error("expected ',' after for start value");<br>
> getNextToken();<br>
><br>
> - ExprAST *End = ParseExpression();<br>
> - if (End == 0)<br>
> - return 0;<br>
> + auto End = ParseExpression();<br>
> + if (!End)<br>
> + return nullptr;<br>
><br>
> // The step value is optional.<br>
> - ExprAST *Step = 0;<br>
> + std::unique_ptr<ExprAST> Step;<br>
> if (CurTok == ',') {<br>
> getNextToken();<br>
> Step = ParseExpression();<br>
> - if (Step == 0)<br>
> - return 0;<br>
> + if (!Step)<br>
> + return nullptr;<br>
> }<br>
><br>
> if (CurTok != tok_in)<br>
> return Error("expected 'in' after for");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0)<br>
> - return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body)<br>
> + return nullptr;<br>
><br>
> - return new ForExprAST(IdName, Start, End, Step, Body);<br>
> + return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),<br>
> + std::move(Step), std::move(Body));<br>
> }<br>
><br>
> /// primary<br>
> @@ -386,7 +385,7 @@<br>
> /// ::= parenexpr<br>
> /// ::= ifexpr<br>
> /// ::= forexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default:<br>
> return Error("unknown token when expecting an expression");<br>
> @@ -405,7 +404,8 @@<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' primary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> +static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,<br>
> + std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -420,38 +420,39 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the primary expression after the binary operator.<br>
> - ExprAST *RHS = ParsePrimary();<br>
> + auto RHS = ParsePrimary();<br>
> if (!RHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec + 1, RHS);<br>
> - if (RHS == 0)<br>
> - return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));<br>
> + if (!RHS)<br>
> + return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= primary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParsePrimary();<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParsePrimary();<br>
> if (!LHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> if (CurTok != tok_identifier)<br>
> return ErrorP("Expected function name in prototype");<br>
><br>
> @@ -470,33 +471,34 @@<br>
> // success.<br>
> getNextToken(); // eat ')'.<br>
><br>
> - return new PrototypeAST(FnName, ArgNames);<br>
> + return llvm::make_unique<PrototypeAST>(FnName, std::move(ArgNames));<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0)<br>
> - return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto)<br>
> + return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("",<br>
> + std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -512,7 +514,7 @@<br>
><br>
> Value *ErrorV(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> Value *NumberExprAST::Codegen() {<br>
> @@ -528,8 +530,8 @@<br>
> Value *BinaryExprAST::Codegen() {<br>
> Value *L = LHS->Codegen();<br>
> Value *R = RHS->Codegen();<br>
> - if (L == 0 || R == 0)<br>
> - return 0;<br>
> + if (!L || !R)<br>
> + return nullptr;<br>
><br>
> switch (Op) {<br>
> case '+':<br>
> @@ -551,7 +553,7 @@<br>
> Value *CallExprAST::Codegen() {<br>
> // Look up the name in the global module table.<br>
> Function *CalleeF = TheModule->getFunction(Callee);<br>
> - if (CalleeF == 0)<br>
> + if (!CalleeF)<br>
> return ErrorV("Unknown function referenced");<br>
><br>
> // If argument mismatch error.<br>
> @@ -561,8 +563,8 @@<br>
> std::vector<Value *> ArgsV;<br>
> for (unsigned i = 0, e = Args.size(); i != e; ++i) {<br>
> ArgsV.push_back(Args[i]->Codegen());<br>
> - if (ArgsV.back() == 0)<br>
> - return 0;<br>
> + if (!ArgsV.back())<br>
> + return nullptr;<br>
> }<br>
><br>
> return Builder.CreateCall(CalleeF, ArgsV, "calltmp");<br>
> @@ -570,8 +572,8 @@<br>
><br>
> Value *IfExprAST::Codegen() {<br>
> Value *CondV = Cond->Codegen();<br>
> - if (CondV == 0)<br>
> - return 0;<br>
> + if (!CondV)<br>
> + return nullptr;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> CondV = Builder.CreateFCmpONE(<br>
> @@ -592,8 +594,8 @@<br>
> Builder.SetInsertPoint(ThenBB);<br>
><br>
> Value *ThenV = Then->Codegen();<br>
> - if (ThenV == 0)<br>
> - return 0;<br>
> + if (!ThenV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Then' can change the current block, update ThenBB for the PHI.<br>
> @@ -604,8 +606,8 @@<br>
> Builder.SetInsertPoint(ElseBB);<br>
><br>
> Value *ElseV = Else->Codegen();<br>
> - if (ElseV == 0)<br>
> - return 0;<br>
> + if (!ElseV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Else' can change the current block, update ElseBB for the PHI.<br>
> @@ -641,8 +643,8 @@<br>
><br>
> // Emit the start code first, without 'variable' in scope.<br>
> Value *StartVal = Start->Codegen();<br>
> - if (StartVal == 0)<br>
> - return 0;<br>
> + if (!StartVal)<br>
> + return nullptr;<br>
><br>
> // Make the new basic block for the loop header, inserting after current<br>
> // block.<br>
> @@ -670,15 +672,15 @@<br>
> // Emit the body of the loop. This, like any other expr, can change the<br>
> // current BB. Note that we ignore the value computed by the body, but don't<br>
> // allow an error.<br>
> - if (Body->Codegen() == 0)<br>
> - return 0;<br>
> + if (!Body->Codegen())<br>
> + return nullptr;<br>
><br>
> // Emit the step value.<br>
> Value *StepVal;<br>
> if (Step) {<br>
> StepVal = Step->Codegen();<br>
> - if (StepVal == 0)<br>
> - return 0;<br>
> + if (!StepVal)<br>
> + return nullptr;<br>
> } else {<br>
> // If not specified, use 1.0.<br>
> StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));<br>
> @@ -688,7 +690,7 @@<br>
><br>
> // Compute the end condition.<br>
> Value *EndCond = End->Codegen();<br>
> - if (EndCond == 0)<br>
> + if (!EndCond)<br>
> return EndCond;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> @@ -739,13 +741,13 @@<br>
> // If F already has a body, reject this.<br>
> if (!F->empty()) {<br>
> ErrorF("redefinition of function");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If F took a different number of args, reject.<br>
> if (F->arg_size() != Args.size()) {<br>
> ErrorF("redefinition of function with different # args");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> }<br>
><br>
> @@ -766,8 +768,8 @@<br>
> NamedValues.clear();<br>
><br>
> Function *TheFunction = Proto->Codegen();<br>
> - if (TheFunction == 0)<br>
> - return 0;<br>
> + if (!TheFunction)<br>
> + return nullptr;<br>
><br>
> // Create a new basic block to start insertion into.<br>
> BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);<br>
> @@ -788,7 +790,7 @@<br>
><br>
> // Error reading body, remove function.<br>
> TheFunction->eraseFromParent();<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> //===----------------------------------------------------------------------===//<br>
> @@ -798,10 +800,10 @@<br>
> static ExecutionEngine *TheExecutionEngine;<br>
><br>
> static void HandleDefinition() {<br>
> - if (FunctionAST *F = ParseDefinition()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseDefinition()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> fprintf(stderr, "Read function definition:");<br>
> - LF->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -810,10 +812,10 @@<br>
> }<br>
><br>
> static void HandleExtern() {<br>
> - if (PrototypeAST *P = ParseExtern()) {<br>
> - if (Function *F = P->Codegen()) {<br>
> + if (auto ProtoAST = ParseExtern()) {<br>
> + if (auto *FnIR = ProtoAST->Codegen()) {<br>
> fprintf(stderr, "Read extern: ");<br>
> - F->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -823,11 +825,11 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> TheExecutionEngine->finalizeObject();<br>
> // JIT the function, returning a function pointer.<br>
> - void *FPtr = TheExecutionEngine->getPointerToFunction(LF);<br>
> + void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);<br>
><br>
> // Cast it to the right type (takes no arguments, returns a double) so we<br>
> // can call it as a native function.<br>
> Index: examples/Kaleidoscope/Chapter6/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter6/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter6/toy.cpp (working copy)<br>
> @@ -131,73 +131,72 @@<br>
> /// NumberExprAST - Expression class for numeric literals like "1.0".<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> -<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// VariableExprAST - Expression class for referencing a variable, like "a".<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> -<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// UnaryExprAST - Expression class for a unary operator.<br>
> class UnaryExprAST : public ExprAST {<br>
> char Opcode;<br>
> - ExprAST *Operand;<br>
> + std::unique_ptr<ExprAST> Operand;<br>
><br>
> public:<br>
> - UnaryExprAST(char opcode, ExprAST *operand)<br>
> - : Opcode(opcode), Operand(operand) {}<br>
> + UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)<br>
> + : Opcode(Opcode), Operand(std::move(Operand)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> -<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST *> Args;<br>
> -<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// IfExprAST - Expression class for if/then/else.<br>
> class IfExprAST : public ExprAST {<br>
> - ExprAST *Cond, *Then, *Else;<br>
> -<br>
> + std::unique_ptr<ExprAST> Cond, Then, Else;<br>
> public:<br>
> - IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)<br>
> - : Cond(cond), Then(then), Else(_else) {}<br>
> + IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,<br>
> + std::unique_ptr<ExprAST> Else)<br>
> + : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// ForExprAST - Expression class for for/in.<br>
> class ForExprAST : public ExprAST {<br>
> std::string VarName;<br>
> - ExprAST *Start, *End, *Step, *Body;<br>
> -<br>
> + std::unique_ptr<ExprAST> Start, End, Step, Body;<br>
> public:<br>
> - ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,<br>
> - ExprAST *step, ExprAST *body)<br>
> - : VarName(varname), Start(start), End(end), Step(step), Body(body) {}<br>
> + ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,<br>
> + std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarName(VarName), Start(std::move(Start)), End(std::move(End)),<br>
> + Step(std::move(Step)), Body(std::move(Body)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> @@ -207,15 +206,16 @@<br>
> class PrototypeAST {<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> - bool isOperator;<br>
> + bool IsOperator;<br>
> unsigned Precedence; // Precedence if a binary op.<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args,<br>
> - bool isoperator = false, unsigned prec = 0)<br>
> - : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}<br>
> + PrototypeAST(const std::string &Name, std::vector<std::string> Args,<br>
> + bool IsOperator = false, unsigned Prec = 0)<br>
> + : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),<br>
> + Precedence(Prec) {}<br>
><br>
> - bool isUnaryOp() const { return isOperator && Args.size() == 1; }<br>
> - bool isBinaryOp() const { return isOperator && Args.size() == 2; }<br>
> + bool isUnaryOp() const { return IsOperator && Args.size() == 1; }<br>
> + bool isBinaryOp() const { return IsOperator && Args.size() == 2; }<br>
><br>
> char getOperatorName() const {<br>
> assert(isUnaryOp() || isBinaryOp());<br>
> @@ -229,12 +229,11 @@<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}<br>
> -<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
> Function *Codegen();<br>
> };<br>
> } // end anonymous namespace<br>
> @@ -266,41 +265,41 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) {<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> fprintf(stderr, "Error: %s\n", Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -PrototypeAST *ErrorP(const char *Str) {<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -FunctionAST *ErrorF(const char *Str) {<br>
> +std::unique_ptr<FunctionAST> ErrorF(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST *> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg)<br>
> - return 0;<br>
> - Args.push_back(Arg);<br>
> + if (auto Arg = ParseExpression())<br>
> + Args.push_back(std::move(Arg));<br>
> + else<br>
> + return nullptr;<br>
><br>
> if (CurTok == ')')<br>
> break;<br>
> @@ -314,22 +313,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> + auto V = ParseExpression();<br>
> if (!V)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -338,36 +337,37 @@<br>
> }<br>
><br>
> /// ifexpr ::= 'if' expression 'then' expression 'else' expression<br>
> -static ExprAST *ParseIfExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIfExpr() {<br>
> getNextToken(); // eat the if.<br>
><br>
> // condition.<br>
> - ExprAST *Cond = ParseExpression();<br>
> + auto Cond = ParseExpression();<br>
> if (!Cond)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_then)<br>
> return Error("expected then");<br>
> getNextToken(); // eat the then<br>
><br>
> - ExprAST *Then = ParseExpression();<br>
> - if (Then == 0)<br>
> - return 0;<br>
> + auto Then = ParseExpression();<br>
> + if (!Then)<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_else)<br>
> return Error("expected else");<br>
><br>
> getNextToken();<br>
><br>
> - ExprAST *Else = ParseExpression();<br>
> + auto Else = ParseExpression();<br>
> if (!Else)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return new IfExprAST(Cond, Then, Else);<br>
> + return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),<br>
> + std::move(Else));<br>
> }<br>
><br>
> /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression<br>
> -static ExprAST *ParseForExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseForExpr() {<br>
> getNextToken(); // eat the for.<br>
><br>
> if (CurTok != tok_identifier)<br>
> @@ -380,35 +380,36 @@<br>
> return Error("expected '=' after for");<br>
> getNextToken(); // eat '='.<br>
><br>
> - ExprAST *Start = ParseExpression();<br>
> - if (Start == 0)<br>
> - return 0;<br>
> + auto Start = ParseExpression();<br>
> + if (!Start)<br>
> + return nullptr;<br>
> if (CurTok != ',')<br>
> return Error("expected ',' after for start value");<br>
> getNextToken();<br>
><br>
> - ExprAST *End = ParseExpression();<br>
> - if (End == 0)<br>
> - return 0;<br>
> + auto End = ParseExpression();<br>
> + if (!End)<br>
> + return nullptr;<br>
><br>
> // The step value is optional.<br>
> - ExprAST *Step = 0;<br>
> + std::unique_ptr<ExprAST> Step;<br>
> if (CurTok == ',') {<br>
> getNextToken();<br>
> Step = ParseExpression();<br>
> - if (Step == 0)<br>
> - return 0;<br>
> + if (!Step)<br>
> + return nullptr;<br>
> }<br>
><br>
> if (CurTok != tok_in)<br>
> return Error("expected 'in' after for");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0)<br>
> - return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body)<br>
> + return nullptr;<br>
><br>
> - return new ForExprAST(IdName, Start, End, Step, Body);<br>
> + return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),<br>
> + std::move(Step), std::move(Body));<br>
> }<br>
><br>
> /// primary<br>
> @@ -417,7 +418,7 @@<br>
> /// ::= parenexpr<br>
> /// ::= ifexpr<br>
> /// ::= forexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default:<br>
> return Error("unknown token when expecting an expression");<br>
> @@ -437,7 +438,7 @@<br>
> /// unary<br>
> /// ::= primary<br>
> /// ::= '!' unary<br>
> -static ExprAST *ParseUnary() {<br>
> +static std::unique_ptr<ExprAST> ParseUnary() {<br>
> // If the current token is not an operator, it must be a primary expr.<br>
> if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')<br>
> return ParsePrimary();<br>
> @@ -445,14 +446,14 @@<br>
> // If this is a unary operator, read it.<br>
> int Opc = CurTok;<br>
> getNextToken();<br>
> - if (ExprAST *Operand = ParseUnary())<br>
> - return new UnaryExprAST(Opc, Operand);<br>
> - return 0;<br>
> + if (auto Operand = ParseUnary())<br>
> + return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' unary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> + static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -467,40 +468,40 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the unary expression after the binary operator.<br>
> - ExprAST *RHS = ParseUnary();<br>
> + auto RHS = ParseUnary();<br>
> if (!RHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec + 1, RHS);<br>
> - if (RHS == 0)<br>
> - return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));<br>
> + if (!RHS)<br>
> + return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= unary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParseUnary();<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParseUnary();<br>
> if (!LHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> /// ::= binary LETTER number? (id, id)<br>
> /// ::= unary LETTER (id)<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> std::string FnName;<br>
><br>
> unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.<br>
> @@ -558,33 +559,34 @@<br>
> if (Kind && ArgNames.size() != Kind)<br>
> return ErrorP("Invalid number of operands for operator");<br>
><br>
> - return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);<br>
> + return llvm::make_unique<PrototypeAST>(FnName, ArgNames, Kind != 0,<br>
> + BinaryPrecedence);<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0)<br>
> - return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto)<br>
> + return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -600,7 +602,7 @@<br>
><br>
> Value *ErrorV(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> Value *NumberExprAST::Codegen() {<br>
> @@ -615,11 +617,11 @@<br>
><br>
> Value *UnaryExprAST::Codegen() {<br>
> Value *OperandV = Operand->Codegen();<br>
> - if (OperandV == 0)<br>
> - return 0;<br>
> + if (!OperandV)<br>
> + return nullptr;<br>
><br>
> Function *F = TheModule->getFunction(std::string("unary") + Opcode);<br>
> - if (F == 0)<br>
> + if (!F)<br>
> return ErrorV("Unknown unary operator");<br>
><br>
> return Builder.CreateCall(F, OperandV, "unop");<br>
> @@ -628,8 +630,8 @@<br>
> Value *BinaryExprAST::Codegen() {<br>
> Value *L = LHS->Codegen();<br>
> Value *R = RHS->Codegen();<br>
> - if (L == 0 || R == 0)<br>
> - return 0;<br>
> + if (!L || !R)<br>
> + return nullptr;<br>
><br>
> switch (Op) {<br>
> case '+':<br>
> @@ -659,7 +661,7 @@<br>
> Value *CallExprAST::Codegen() {<br>
> // Look up the name in the global module table.<br>
> Function *CalleeF = TheModule->getFunction(Callee);<br>
> - if (CalleeF == 0)<br>
> + if (!CalleeF)<br>
> return ErrorV("Unknown function referenced");<br>
><br>
> // If argument mismatch error.<br>
> @@ -669,8 +671,8 @@<br>
> std::vector<Value *> ArgsV;<br>
> for (unsigned i = 0, e = Args.size(); i != e; ++i) {<br>
> ArgsV.push_back(Args[i]->Codegen());<br>
> - if (ArgsV.back() == 0)<br>
> - return 0;<br>
> + if (!ArgsV.back())<br>
> + return nullptr;<br>
> }<br>
><br>
> return Builder.CreateCall(CalleeF, ArgsV, "calltmp");<br>
> @@ -678,8 +680,8 @@<br>
><br>
> Value *IfExprAST::Codegen() {<br>
> Value *CondV = Cond->Codegen();<br>
> - if (CondV == 0)<br>
> - return 0;<br>
> + if (!CondV)<br>
> + return nullptr;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> CondV = Builder.CreateFCmpONE(<br>
> @@ -700,8 +702,8 @@<br>
> Builder.SetInsertPoint(ThenBB);<br>
><br>
> Value *ThenV = Then->Codegen();<br>
> - if (ThenV == 0)<br>
> - return 0;<br>
> + if (!ThenV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Then' can change the current block, update ThenBB for the PHI.<br>
> @@ -712,8 +714,8 @@<br>
> Builder.SetInsertPoint(ElseBB);<br>
><br>
> Value *ElseV = Else->Codegen();<br>
> - if (ElseV == 0)<br>
> - return 0;<br>
> + if (!ElseV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Else' can change the current block, update ElseBB for the PHI.<br>
> @@ -749,8 +751,8 @@<br>
><br>
> // Emit the start code first, without 'variable' in scope.<br>
> Value *StartVal = Start->Codegen();<br>
> - if (StartVal == 0)<br>
> - return 0;<br>
> + if (!StartVal)<br>
> + return nullptr;<br>
><br>
> // Make the new basic block for the loop header, inserting after current<br>
> // block.<br>
> @@ -778,15 +780,15 @@<br>
> // Emit the body of the loop. This, like any other expr, can change the<br>
> // current BB. Note that we ignore the value computed by the body, but don't<br>
> // allow an error.<br>
> - if (Body->Codegen() == 0)<br>
> - return 0;<br>
> + if (!Body->Codegen())<br>
> + return nullptr;<br>
><br>
> // Emit the step value.<br>
> Value *StepVal;<br>
> if (Step) {<br>
> StepVal = Step->Codegen();<br>
> - if (StepVal == 0)<br>
> - return 0;<br>
> + if (!StepVal)<br>
> + return nullptr;<br>
> } else {<br>
> // If not specified, use 1.0.<br>
> StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));<br>
> @@ -796,7 +798,7 @@<br>
><br>
> // Compute the end condition.<br>
> Value *EndCond = End->Codegen();<br>
> - if (EndCond == 0)<br>
> + if (!EndCond)<br>
> return EndCond;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> @@ -847,13 +849,13 @@<br>
> // If F already has a body, reject this.<br>
> if (!F->empty()) {<br>
> ErrorF("redefinition of function");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If F took a different number of args, reject.<br>
> if (F->arg_size() != Args.size()) {<br>
> ErrorF("redefinition of function with different # args");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> }<br>
><br>
> @@ -874,8 +876,8 @@<br>
> NamedValues.clear();<br>
><br>
> Function *TheFunction = Proto->Codegen();<br>
> - if (TheFunction == 0)<br>
> - return 0;<br>
> + if (!TheFunction)<br>
> + return nullptr;<br>
><br>
> // If this is an operator, install it.<br>
> if (Proto->isBinaryOp())<br>
> @@ -903,7 +905,7 @@<br>
><br>
> if (Proto->isBinaryOp())<br>
> BinopPrecedence.erase(Proto->getOperatorName());<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> //===----------------------------------------------------------------------===//<br>
> @@ -913,10 +915,10 @@<br>
> static ExecutionEngine *TheExecutionEngine;<br>
><br>
> static void HandleDefinition() {<br>
> - if (FunctionAST *F = ParseDefinition()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseDefinition()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> fprintf(stderr, "Read function definition:");<br>
> - LF->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -925,10 +927,10 @@<br>
> }<br>
><br>
> static void HandleExtern() {<br>
> - if (PrototypeAST *P = ParseExtern()) {<br>
> - if (Function *F = P->Codegen()) {<br>
> + if (auto ProtoAST = ParseExtern()) {<br>
> + if (auto *FnIR = ProtoAST->Codegen()) {<br>
> fprintf(stderr, "Read extern: ");<br>
> - F->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -938,11 +940,11 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> TheExecutionEngine->finalizeObject();<br>
> // JIT the function, returning a function pointer.<br>
> - void *FPtr = TheExecutionEngine->getPointerToFunction(LF);<br>
> + void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);<br>
><br>
> // Cast it to the right type (takes no arguments, returns a double) so we<br>
> // can call it as a native function.<br>
> Index: examples/Kaleidoscope/Chapter7/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter7/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter7/toy.cpp (working copy)<br>
> @@ -136,18 +136,16 @@<br>
> /// NumberExprAST - Expression class for numeric literals like "1.0".<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> -<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// VariableExprAST - Expression class for referencing a variable, like "a".<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> -<br>
> public:<br>
> - VariableExprAST(const std::string &name) : Name(name) {}<br>
> + VariableExprAST(const std::string &Name) : Name(Name) {}<br>
> const std::string &getName() const { return Name; }<br>
> Value *Codegen() override;<br>
> };<br>
> @@ -155,68 +153,66 @@<br>
> /// UnaryExprAST - Expression class for a unary operator.<br>
> class UnaryExprAST : public ExprAST {<br>
> char Opcode;<br>
> - ExprAST *Operand;<br>
> -<br>
> + std::unique_ptr<ExprAST> Operand;<br>
> public:<br>
> - UnaryExprAST(char opcode, ExprAST *operand)<br>
> - : Opcode(opcode), Operand(operand) {}<br>
> + UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)<br>
> + : Opcode(Opcode), Operand(std::move(Operand)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> -<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST *> Args;<br>
> -<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)<br>
> - : Callee(callee), Args(args) {}<br>
> + CallExprAST(const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : Callee(Callee), Args(std::move(Args)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// IfExprAST - Expression class for if/then/else.<br>
> class IfExprAST : public ExprAST {<br>
> - ExprAST *Cond, *Then, *Else;<br>
> -<br>
> + std::unique_ptr<ExprAST> Cond, Then, Else;<br>
> public:<br>
> - IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)<br>
> - : Cond(cond), Then(then), Else(_else) {}<br>
> + IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,<br>
> + std::unique_ptr<ExprAST> Else)<br>
> + : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// ForExprAST - Expression class for for/in.<br>
> class ForExprAST : public ExprAST {<br>
> std::string VarName;<br>
> - ExprAST *Start, *End, *Step, *Body;<br>
> -<br>
> + std::unique_ptr<ExprAST> Start, End, Step, Body;<br>
> public:<br>
> - ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,<br>
> - ExprAST *step, ExprAST *body)<br>
> - : VarName(varname), Start(start), End(end), Step(step), Body(body) {}<br>
> + ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,<br>
> + std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarName(VarName), Start(std::move(Start)), End(std::move(End)),<br>
> + Step(std::move(Step)), Body(std::move(Body)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> /// VarExprAST - Expression class for var/in<br>
> class VarExprAST : public ExprAST {<br>
> - std::vector<std::pair<std::string, ExprAST *> > VarNames;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - VarExprAST(const std::vector<std::pair<std::string, ExprAST *> > &varnames,<br>
> - ExprAST *body)<br>
> - : VarNames(varnames), Body(body) {}<br>
> -<br>
> + VarExprAST(std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarNames(std::move(VarNames)), Body(std::move(Body)) {}<br>
> Value *Codegen() override;<br>
> };<br>
><br>
> @@ -225,15 +221,16 @@<br>
> class PrototypeAST {<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> - bool isOperator;<br>
> + bool IsOperator;<br>
> unsigned Precedence; // Precedence if a binary op.<br>
> public:<br>
> - PrototypeAST(const std::string &name, const std::vector<std::string> &args,<br>
> - bool isoperator = false, unsigned prec = 0)<br>
> - : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}<br>
> + PrototypeAST(const std::string &Name, std::vector<std::string> Args,<br>
> + bool IsOperator = false, unsigned Prec = 0)<br>
> + : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),<br>
> + Precedence(Prec) {}<br>
><br>
> - bool isUnaryOp() const { return isOperator && Args.size() == 1; }<br>
> - bool isBinaryOp() const { return isOperator && Args.size() == 2; }<br>
> + bool isUnaryOp() const { return IsOperator && Args.size() == 1; }<br>
> + bool isBinaryOp() const { return IsOperator && Args.size() == 2; }<br>
><br>
> char getOperatorName() const {<br>
> assert(isUnaryOp() || isBinaryOp());<br>
> @@ -249,12 +246,11 @@<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}<br>
> -<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
> Function *Codegen();<br>
> };<br>
> } // end anonymous namespace<br>
> @@ -286,41 +282,41 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) {<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> fprintf(stderr, "Error: %s\n", Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -PrototypeAST *ErrorP(const char *Str) {<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -FunctionAST *ErrorF(const char *Str) {<br>
> +std::unique_ptr<FunctionAST> ErrorF(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(IdName);<br>
> + return llvm::make_unique<VariableExprAST>(IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST *> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> - if (!Arg)<br>
> - return 0;<br>
> - Args.push_back(Arg);<br>
> + if (auto Arg = ParseExpression())<br>
> + Args.push_back(std::move(Arg));<br>
> + else<br>
> + return nullptr;<br>
><br>
> if (CurTok == ')')<br>
> break;<br>
> @@ -334,22 +330,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> + auto V = ParseExpression();<br>
> if (!V)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -358,36 +354,37 @@<br>
> }<br>
><br>
> /// ifexpr ::= 'if' expression 'then' expression 'else' expression<br>
> -static ExprAST *ParseIfExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIfExpr() {<br>
> getNextToken(); // eat the if.<br>
><br>
> // condition.<br>
> - ExprAST *Cond = ParseExpression();<br>
> + auto Cond = ParseExpression();<br>
> if (!Cond)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_then)<br>
> return Error("expected then");<br>
> getNextToken(); // eat the then<br>
><br>
> - ExprAST *Then = ParseExpression();<br>
> - if (Then == 0)<br>
> - return 0;<br>
> + auto Then = ParseExpression();<br>
> + if (!Then)<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_else)<br>
> return Error("expected else");<br>
><br>
> getNextToken();<br>
><br>
> - ExprAST *Else = ParseExpression();<br>
> + auto Else = ParseExpression();<br>
> if (!Else)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return new IfExprAST(Cond, Then, Else);<br>
> + return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),<br>
> + std::move(Else));<br>
> }<br>
><br>
> /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression<br>
> -static ExprAST *ParseForExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseForExpr() {<br>
> getNextToken(); // eat the for.<br>
><br>
> if (CurTok != tok_identifier)<br>
> @@ -400,43 +397,44 @@<br>
> return Error("expected '=' after for");<br>
> getNextToken(); // eat '='.<br>
><br>
> - ExprAST *Start = ParseExpression();<br>
> - if (Start == 0)<br>
> - return 0;<br>
> + auto Start = ParseExpression();<br>
> + if (!Start)<br>
> + return nullptr;<br>
> if (CurTok != ',')<br>
> return Error("expected ',' after for start value");<br>
> getNextToken();<br>
><br>
> - ExprAST *End = ParseExpression();<br>
> - if (End == 0)<br>
> - return 0;<br>
> + auto End = ParseExpression();<br>
> + if (!End)<br>
> + return nullptr;<br>
><br>
> // The step value is optional.<br>
> - ExprAST *Step = 0;<br>
> + std::unique_ptr<ExprAST> Step;<br>
> if (CurTok == ',') {<br>
> getNextToken();<br>
> Step = ParseExpression();<br>
> - if (Step == 0)<br>
> - return 0;<br>
> + if (!Step)<br>
> + return nullptr;<br>
> }<br>
><br>
> if (CurTok != tok_in)<br>
> return Error("expected 'in' after for");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0)<br>
> - return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body)<br>
> + return nullptr;<br>
><br>
> - return new ForExprAST(IdName, Start, End, Step, Body);<br>
> + return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),<br>
> + std::move(Step), std::move(Body));<br>
> }<br>
><br>
> /// varexpr ::= 'var' identifier ('=' expression)?<br>
> // (',' identifier ('=' expression)?)* 'in' expression<br>
> -static ExprAST *ParseVarExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseVarExpr() {<br>
> getNextToken(); // eat the var.<br>
><br>
> - std::vector<std::pair<std::string, ExprAST *> > VarNames;<br>
> + std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;<br>
><br>
> // At least one variable name is required.<br>
> if (CurTok != tok_identifier)<br>
> @@ -447,16 +445,16 @@<br>
> getNextToken(); // eat identifier.<br>
><br>
> // Read the optional initializer.<br>
> - ExprAST *Init = 0;<br>
> + std::unique_ptr<ExprAST> Init = nullptr;<br>
> if (CurTok == '=') {<br>
> getNextToken(); // eat the '='.<br>
><br>
> Init = ParseExpression();<br>
> - if (Init == 0)<br>
> - return 0;<br>
> + if (!Init)<br>
> + return nullptr;<br>
> }<br>
><br>
> - VarNames.push_back(std::make_pair(Name, Init));<br>
> + VarNames.push_back(std::make_pair(Name, std::move(Init)));<br>
><br>
> // End of var list, exit loop.<br>
> if (CurTok != ',')<br>
> @@ -472,11 +470,11 @@<br>
> return Error("expected 'in' keyword after 'var'");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0)<br>
> - return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body)<br>
> + return nullptr;<br>
><br>
> - return new VarExprAST(VarNames, Body);<br>
> + return llvm::make_unique<VarExprAST>(std::move(VarNames), std::move(Body));<br>
> }<br>
><br>
> /// primary<br>
> @@ -486,7 +484,7 @@<br>
> /// ::= ifexpr<br>
> /// ::= forexpr<br>
> /// ::= varexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default:<br>
> return Error("unknown token when expecting an expression");<br>
> @@ -508,7 +506,7 @@<br>
> /// unary<br>
> /// ::= primary<br>
> /// ::= '!' unary<br>
> -static ExprAST *ParseUnary() {<br>
> +static std::unique_ptr<ExprAST> ParseUnary() {<br>
> // If the current token is not an operator, it must be a primary expr.<br>
> if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')<br>
> return ParsePrimary();<br>
> @@ -516,14 +514,14 @@<br>
> // If this is a unary operator, read it.<br>
> int Opc = CurTok;<br>
> getNextToken();<br>
> - if (ExprAST *Operand = ParseUnary())<br>
> - return new UnaryExprAST(Opc, Operand);<br>
> - return 0;<br>
> + if (auto Operand = ParseUnary())<br>
> + return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' unary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> + static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -538,40 +536,40 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the unary expression after the binary operator.<br>
> - ExprAST *RHS = ParseUnary();<br>
> + auto RHS = ParseUnary();<br>
> if (!RHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec + 1, RHS);<br>
> - if (RHS == 0)<br>
> - return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));<br>
> + if (!RHS)<br>
> + return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= unary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParseUnary();<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParseUnary();<br>
> if (!LHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> /// ::= binary LETTER number? (id, id)<br>
> /// ::= unary LETTER (id)<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> std::string FnName;<br>
><br>
> unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.<br>
> @@ -629,33 +627,34 @@<br>
> if (Kind && ArgNames.size() != Kind)<br>
> return ErrorP("Invalid number of operands for operator");<br>
><br>
> - return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);<br>
> + return llvm::make_unique<PrototypeAST>(FnName, ArgNames, Kind != 0,<br>
> + BinaryPrecedence);<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0)<br>
> - return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto)<br>
> + return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -671,7 +670,7 @@<br>
><br>
> Value *ErrorV(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of<br>
> @@ -691,7 +690,7 @@<br>
> Value *VariableExprAST::Codegen() {<br>
> // Look this variable up in the function.<br>
> Value *V = NamedValues[Name];<br>
> - if (V == 0)<br>
> + if (!V)<br>
> return ErrorV("Unknown variable name");<br>
><br>
> // Load the value.<br>
> @@ -700,11 +699,11 @@<br>
><br>
> Value *UnaryExprAST::Codegen() {<br>
> Value *OperandV = Operand->Codegen();<br>
> - if (OperandV == 0)<br>
> - return 0;<br>
> + if (!OperandV)<br>
> + return nullptr;<br>
><br>
> Function *F = TheModule->getFunction(std::string("unary") + Opcode);<br>
> - if (F == 0)<br>
> + if (!F)<br>
> return ErrorV("Unknown unary operator");<br>
><br>
> return Builder.CreateCall(F, OperandV, "unop");<br>
> @@ -717,17 +716,17 @@<br>
> // This assume we're building without RTTI because LLVM builds that way by<br>
> // default. If you build LLVM with RTTI this can be changed to a<br>
> // dynamic_cast for automatic error checking.<br>
> - VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS);<br>
> + VariableExprAST *LHSE = static_cast<VariableExprAST*>(LHS.get());<br>
> if (!LHSE)<br>
> return ErrorV("destination of '=' must be a variable");<br>
> // Codegen the RHS.<br>
> Value *Val = RHS->Codegen();<br>
> - if (Val == 0)<br>
> - return 0;<br>
> + if (!Val)<br>
> + return nullptr;<br>
><br>
> // Look up the name.<br>
> Value *Variable = NamedValues[LHSE->getName()];<br>
> - if (Variable == 0)<br>
> + if (!Variable)<br>
> return ErrorV("Unknown variable name");<br>
><br>
> Builder.CreateStore(Val, Variable);<br>
> @@ -736,8 +735,8 @@<br>
><br>
> Value *L = LHS->Codegen();<br>
> Value *R = RHS->Codegen();<br>
> - if (L == 0 || R == 0)<br>
> - return 0;<br>
> + if (!L || !R)<br>
> + return nullptr;<br>
><br>
> switch (Op) {<br>
> case '+':<br>
> @@ -767,7 +766,7 @@<br>
> Value *CallExprAST::Codegen() {<br>
> // Look up the name in the global module table.<br>
> Function *CalleeF = TheModule->getFunction(Callee);<br>
> - if (CalleeF == 0)<br>
> + if (!CalleeF)<br>
> return ErrorV("Unknown function referenced");<br>
><br>
> // If argument mismatch error.<br>
> @@ -777,8 +776,8 @@<br>
> std::vector<Value *> ArgsV;<br>
> for (unsigned i = 0, e = Args.size(); i != e; ++i) {<br>
> ArgsV.push_back(Args[i]->Codegen());<br>
> - if (ArgsV.back() == 0)<br>
> - return 0;<br>
> + if (!ArgsV.back())<br>
> + return nullptr;<br>
> }<br>
><br>
> return Builder.CreateCall(CalleeF, ArgsV, "calltmp");<br>
> @@ -786,8 +785,8 @@<br>
><br>
> Value *IfExprAST::Codegen() {<br>
> Value *CondV = Cond->Codegen();<br>
> - if (CondV == 0)<br>
> - return 0;<br>
> + if (!CondV)<br>
> + return nullptr;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> CondV = Builder.CreateFCmpONE(<br>
> @@ -808,8 +807,8 @@<br>
> Builder.SetInsertPoint(ThenBB);<br>
><br>
> Value *ThenV = Then->Codegen();<br>
> - if (ThenV == 0)<br>
> - return 0;<br>
> + if (!ThenV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Then' can change the current block, update ThenBB for the PHI.<br>
> @@ -820,8 +819,8 @@<br>
> Builder.SetInsertPoint(ElseBB);<br>
><br>
> Value *ElseV = Else->Codegen();<br>
> - if (ElseV == 0)<br>
> - return 0;<br>
> + if (!ElseV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Else' can change the current block, update ElseBB for the PHI.<br>
> @@ -866,8 +865,8 @@<br>
><br>
> // Emit the start code first, without 'variable' in scope.<br>
> Value *StartVal = Start->Codegen();<br>
> - if (StartVal == 0)<br>
> - return 0;<br>
> + if (!StartVal)<br>
> + return nullptr;<br>
><br>
> // Store the value into the alloca.<br>
> Builder.CreateStore(StartVal, Alloca);<br>
> @@ -891,15 +890,15 @@<br>
> // Emit the body of the loop. This, like any other expr, can change the<br>
> // current BB. Note that we ignore the value computed by the body, but don't<br>
> // allow an error.<br>
> - if (Body->Codegen() == 0)<br>
> - return 0;<br>
> + if (!Body->Codegen())<br>
> + return nullptr;<br>
><br>
> // Emit the step value.<br>
> Value *StepVal;<br>
> if (Step) {<br>
> StepVal = Step->Codegen();<br>
> - if (StepVal == 0)<br>
> - return 0;<br>
> + if (!StepVal)<br>
> + return nullptr;<br>
> } else {<br>
> // If not specified, use 1.0.<br>
> StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));<br>
> @@ -907,7 +906,7 @@<br>
><br>
> // Compute the end condition.<br>
> Value *EndCond = End->Codegen();<br>
> - if (EndCond == 0)<br>
> + if (!EndCond)<br>
> return EndCond;<br>
><br>
> // Reload, increment, and restore the alloca. This handles the case where<br>
> @@ -948,7 +947,7 @@<br>
> // Register all variables and emit their initializer.<br>
> for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {<br>
> const std::string &VarName = VarNames[i].first;<br>
> - ExprAST *Init = VarNames[i].second;<br>
> + ExprAST *Init = VarNames[i].second.get();<br>
><br>
> // Emit the initializer before adding the variable to scope, this prevents<br>
> // the initializer from referencing the variable itself, and permits stuff<br>
> @@ -958,8 +957,8 @@<br>
> Value *InitVal;<br>
> if (Init) {<br>
> InitVal = Init->Codegen();<br>
> - if (InitVal == 0)<br>
> - return 0;<br>
> + if (!InitVal)<br>
> + return nullptr;<br>
> } else { // If not specified, use 0.0.<br>
> InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));<br>
> }<br>
> @@ -977,8 +976,8 @@<br>
><br>
> // Codegen the body, now that all vars are in scope.<br>
> Value *BodyVal = Body->Codegen();<br>
> - if (BodyVal == 0)<br>
> - return 0;<br>
> + if (!BodyVal)<br>
> + return nullptr;<br>
><br>
> // Pop all our variables from scope.<br>
> for (unsigned i = 0, e = VarNames.size(); i != e; ++i)<br>
> @@ -1008,13 +1007,13 @@<br>
> // If F already has a body, reject this.<br>
> if (!F->empty()) {<br>
> ErrorF("redefinition of function");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If F took a different number of args, reject.<br>
> if (F->arg_size() != Args.size()) {<br>
> ErrorF("redefinition of function with different # args");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> }<br>
><br>
> @@ -1047,8 +1046,8 @@<br>
> NamedValues.clear();<br>
><br>
> Function *TheFunction = Proto->Codegen();<br>
> - if (TheFunction == 0)<br>
> - return 0;<br>
> + if (!TheFunction)<br>
> + return nullptr;<br>
><br>
> // If this is an operator, install it.<br>
> if (Proto->isBinaryOp())<br>
> @@ -1079,7 +1078,7 @@<br>
><br>
> if (Proto->isBinaryOp())<br>
> BinopPrecedence.erase(Proto->getOperatorName());<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> //===----------------------------------------------------------------------===//<br>
> @@ -1089,10 +1088,10 @@<br>
> static ExecutionEngine *TheExecutionEngine;<br>
><br>
> static void HandleDefinition() {<br>
> - if (FunctionAST *F = ParseDefinition()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseDefinition()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> fprintf(stderr, "Read function definition:");<br>
> - LF->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -1101,10 +1100,10 @@<br>
> }<br>
><br>
> static void HandleExtern() {<br>
> - if (PrototypeAST *P = ParseExtern()) {<br>
> - if (Function *F = P->Codegen()) {<br>
> + if (auto ProtoAST = ParseExtern()) {<br>
> + if (auto *FnIR = ProtoAST->Codegen()) {<br>
> fprintf(stderr, "Read extern: ");<br>
> - F->dump();<br>
> + FnIR->dump();<br>
> }<br>
> } else {<br>
> // Skip token for error recovery.<br>
> @@ -1114,11 +1113,11 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (Function *LF = F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (auto *FnIR = FnAST->Codegen()) {<br>
> TheExecutionEngine->finalizeObject();<br>
> // JIT the function, returning a function pointer.<br>
> - void *FPtr = TheExecutionEngine->getPointerToFunction(LF);<br>
> + void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);<br>
><br>
> // Cast it to the right type (takes no arguments, returns a double) so we<br>
> // can call it as a native function.<br>
> Index: examples/Kaleidoscope/Chapter8/toy.cpp<br>
> ===================================================================<br>
> --- examples/Kaleidoscope/Chapter8/toy.cpp (revision 245236)<br>
> +++ examples/Kaleidoscope/Chapter8/toy.cpp (working copy)<br>
> @@ -203,7 +203,6 @@<br>
> /// ExprAST - Base class for all expression nodes.<br>
> class ExprAST {<br>
> SourceLocation Loc;<br>
> -<br>
> public:<br>
> int getLine() const { return Loc.Line; }<br>
> int getCol() const { return Loc.Col; }<br>
> @@ -218,9 +217,8 @@<br>
> /// NumberExprAST - Expression class for numeric literals like "1.0".<br>
> class NumberExprAST : public ExprAST {<br>
> double Val;<br>
> -<br>
> public:<br>
> - NumberExprAST(double val) : Val(val) {}<br>
> + NumberExprAST(double Val) : Val(Val) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> return ExprAST::dump(out << Val, ind);<br>
> }<br>
> @@ -230,10 +228,9 @@<br>
> /// VariableExprAST - Expression class for referencing a variable, like "a".<br>
> class VariableExprAST : public ExprAST {<br>
> std::string Name;<br>
> -<br>
> public:<br>
> - VariableExprAST(SourceLocation Loc, const std::string &name)<br>
> - : ExprAST(Loc), Name(name) {}<br>
> + VariableExprAST(SourceLocation Loc, const std::string &Name)<br>
> + : ExprAST(Loc), Name(Name) {}<br>
> const std::string &getName() const { return Name; }<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> return ExprAST::dump(out << Name, ind);<br>
> @@ -244,11 +241,10 @@<br>
> /// UnaryExprAST - Expression class for a unary operator.<br>
> class UnaryExprAST : public ExprAST {<br>
> char Opcode;<br>
> - ExprAST *Operand;<br>
> -<br>
> + std::unique_ptr<ExprAST> Operand;<br>
> public:<br>
> - UnaryExprAST(char opcode, ExprAST *operand)<br>
> - : Opcode(opcode), Operand(operand) {}<br>
> + UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)<br>
> + : Opcode(Opcode), Operand(std::move(Operand)) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> ExprAST::dump(out << "unary" << Opcode, ind);<br>
> Operand->dump(out, ind + 1);<br>
> @@ -260,11 +256,11 @@<br>
> /// BinaryExprAST - Expression class for a binary operator.<br>
> class BinaryExprAST : public ExprAST {<br>
> char Op;<br>
> - ExprAST *LHS, *RHS;<br>
> -<br>
> + std::unique_ptr<ExprAST> LHS, RHS;<br>
> public:<br>
> - BinaryExprAST(SourceLocation Loc, char op, ExprAST *lhs, ExprAST *rhs)<br>
> - : ExprAST(Loc), Op(op), LHS(lhs), RHS(rhs) {}<br>
> + BinaryExprAST(SourceLocation Loc, char Op, std::unique_ptr<ExprAST> LHS,<br>
> + std::unique_ptr<ExprAST> RHS)<br>
> + : ExprAST(Loc), Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> ExprAST::dump(out << "binary" << Op, ind);<br>
> LHS->dump(indent(out, ind) << "LHS:", ind + 1);<br>
> @@ -277,15 +273,14 @@<br>
> /// CallExprAST - Expression class for function calls.<br>
> class CallExprAST : public ExprAST {<br>
> std::string Callee;<br>
> - std::vector<ExprAST *> Args;<br>
> -<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> public:<br>
> - CallExprAST(SourceLocation Loc, const std::string &callee,<br>
> - std::vector<ExprAST *> &args)<br>
> - : ExprAST(Loc), Callee(callee), Args(args) {}<br>
> + CallExprAST(SourceLocation Loc, const std::string &Callee,<br>
> + std::vector<std::unique_ptr<ExprAST>> Args)<br>
> + : ExprAST(Loc), Callee(Callee), Args(std::move(Args)) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> ExprAST::dump(out << "call " << Callee, ind);<br>
> - for (ExprAST *Arg : Args)<br>
> + for (const auto &Arg : Args)<br>
> Arg->dump(indent(out, ind + 1), ind + 1);<br>
> return out;<br>
> }<br>
> @@ -294,11 +289,11 @@<br>
><br>
> /// IfExprAST - Expression class for if/then/else.<br>
> class IfExprAST : public ExprAST {<br>
> - ExprAST *Cond, *Then, *Else;<br>
> -<br>
> + std::unique_ptr<ExprAST> Cond, Then, Else;<br>
> public:<br>
> - IfExprAST(SourceLocation Loc, ExprAST *cond, ExprAST *then, ExprAST *_else)<br>
> - : ExprAST(Loc), Cond(cond), Then(then), Else(_else) {}<br>
> + IfExprAST(SourceLocation Loc, std::unique_ptr<ExprAST> Cond,<br>
> + std::unique_ptr<ExprAST> Then, std::unique_ptr<ExprAST> Else)<br>
> + : ExprAST(Loc), Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> ExprAST::dump(out << "if", ind);<br>
> Cond->dump(indent(out, ind) << "Cond:", ind + 1);<br>
> @@ -312,12 +307,13 @@<br>
> /// ForExprAST - Expression class for for/in.<br>
> class ForExprAST : public ExprAST {<br>
> std::string VarName;<br>
> - ExprAST *Start, *End, *Step, *Body;<br>
> -<br>
> + std::unique_ptr<ExprAST> Start, End, Step, Body;<br>
> public:<br>
> - ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,<br>
> - ExprAST *step, ExprAST *body)<br>
> - : VarName(varname), Start(start), End(end), Step(step), Body(body) {}<br>
> + ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,<br>
> + std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarName(VarName), Start(std::move(Start)), End(std::move(End)),<br>
> + Step(std::move(Step)), Body(std::move(Body)) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> ExprAST::dump(out << "for", ind);<br>
> Start->dump(indent(out, ind) << "Cond:", ind + 1);<br>
> @@ -331,14 +327,12 @@<br>
><br>
> /// VarExprAST - Expression class for var/in<br>
> class VarExprAST : public ExprAST {<br>
> - std::vector<std::pair<std::string, ExprAST *> > VarNames;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - VarExprAST(const std::vector<std::pair<std::string, ExprAST *> > &varnames,<br>
> - ExprAST *body)<br>
> - : VarNames(varnames), Body(body) {}<br>
> -<br>
> + VarExprAST(std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : VarNames(std::move(VarNames)), Body(std::move(Body)) {}<br>
> std::ostream &dump(std::ostream &out, int ind) override {<br>
> ExprAST::dump(out << "var", ind);<br>
> for (const auto &NamedVar : VarNames)<br>
> @@ -354,19 +348,18 @@<br>
> class PrototypeAST {<br>
> std::string Name;<br>
> std::vector<std::string> Args;<br>
> - bool isOperator;<br>
> + bool IsOperator;<br>
> unsigned Precedence; // Precedence if a binary op.<br>
> int Line;<br>
> -<br>
> public:<br>
> - PrototypeAST(SourceLocation Loc, const std::string &name,<br>
> - const std::vector<std::string> &args, bool isoperator = false,<br>
> - unsigned prec = 0)<br>
> - : Name(name), Args(args), isOperator(isoperator), Precedence(prec),<br>
> - Line(Loc.Line) {}<br>
> + PrototypeAST(SourceLocation Loc, const std::string &Name,<br>
> + std::vector<std::string> Args, bool IsOperator = false,<br>
> + unsigned Prec = 0)<br>
> + : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),<br>
> + Precedence(Prec), Line(Loc.Line) {}<br>
><br>
> - bool isUnaryOp() const { return isOperator && Args.size() == 1; }<br>
> - bool isBinaryOp() const { return isOperator && Args.size() == 2; }<br>
> + bool isUnaryOp() const { return IsOperator && Args.size() == 1; }<br>
> + bool isBinaryOp() const { return IsOperator && Args.size() == 2; }<br>
><br>
> char getOperatorName() const {<br>
> assert(isUnaryOp() || isBinaryOp());<br>
> @@ -383,11 +376,12 @@<br>
><br>
> /// FunctionAST - This class represents a function definition itself.<br>
> class FunctionAST {<br>
> - PrototypeAST *Proto;<br>
> - ExprAST *Body;<br>
> -<br>
> + std::unique_ptr<PrototypeAST> Proto;<br>
> + std::unique_ptr<ExprAST> Body;<br>
> public:<br>
> - FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}<br>
> + FunctionAST(std::unique_ptr<PrototypeAST> Proto,<br>
> + std::unique_ptr<ExprAST> Body)<br>
> + : Proto(std::move(Proto)), Body(std::move(Body)) {}<br>
><br>
> std::ostream &dump(std::ostream &out, int ind) {<br>
> indent(out, ind) << "FunctionAST\n";<br>
> @@ -427,25 +421,25 @@<br>
> }<br>
><br>
> /// Error* - These are little helper functions for error handling.<br>
> -ExprAST *Error(const char *Str) {<br>
> +std::unique_ptr<ExprAST> Error(const char *Str) {<br>
> fprintf(stderr, "Error: %s\n", Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -PrototypeAST *ErrorP(const char *Str) {<br>
> +std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> -FunctionAST *ErrorF(const char *Str) {<br>
> +std::unique_ptr<FunctionAST> ErrorF(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> -static ExprAST *ParseExpression();<br>
> +static std::unique_ptr<ExprAST> ParseExpression();<br>
><br>
> /// identifierexpr<br>
> /// ::= identifier<br>
> /// ::= identifier '(' expression* ')'<br>
> -static ExprAST *ParseIdentifierExpr() {<br>
> + static std::unique_ptr<ExprAST> ParseIdentifierExpr() {<br>
> std::string IdName = IdentifierStr;<br>
><br>
> SourceLocation LitLoc = CurLoc;<br>
> @@ -453,17 +447,17 @@<br>
> getNextToken(); // eat identifier.<br>
><br>
> if (CurTok != '(') // Simple variable ref.<br>
> - return new VariableExprAST(LitLoc, IdName);<br>
> + return llvm::make_unique<VariableExprAST>(LitLoc, IdName);<br>
><br>
> // Call.<br>
> getNextToken(); // eat (<br>
> - std::vector<ExprAST *> Args;<br>
> + std::vector<std::unique_ptr<ExprAST>> Args;<br>
> if (CurTok != ')') {<br>
> while (1) {<br>
> - ExprAST *Arg = ParseExpression();<br>
> + auto Arg = ParseExpression();<br>
> if (!Arg)<br>
> - return 0;<br>
> - Args.push_back(Arg);<br>
> + return nullptr;<br>
> + Args.push_back(std::move(Arg));<br>
><br>
> if (CurTok == ')')<br>
> break;<br>
> @@ -477,22 +471,22 @@<br>
> // Eat the ')'.<br>
> getNextToken();<br>
><br>
> - return new CallExprAST(LitLoc, IdName, Args);<br>
> + return llvm::make_unique<CallExprAST>(LitLoc, IdName, std::move(Args));<br>
> }<br>
><br>
> /// numberexpr ::= number<br>
> -static ExprAST *ParseNumberExpr() {<br>
> - ExprAST *Result = new NumberExprAST(NumVal);<br>
> +static std::unique_ptr<ExprAST> ParseNumberExpr() {<br>
> + auto Result = llvm::make_unique<NumberExprAST>(NumVal);<br>
> getNextToken(); // consume the number<br>
> - return Result;<br>
> + return std::move(Result);<br>
> }<br>
><br>
> /// parenexpr ::= '(' expression ')'<br>
> -static ExprAST *ParseParenExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseParenExpr() {<br>
> getNextToken(); // eat (.<br>
> - ExprAST *V = ParseExpression();<br>
> + auto V = ParseExpression();<br>
> if (!V)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != ')')<br>
> return Error("expected ')'");<br>
> @@ -501,38 +495,39 @@<br>
> }<br>
><br>
> /// ifexpr ::= 'if' expression 'then' expression 'else' expression<br>
> -static ExprAST *ParseIfExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseIfExpr() {<br>
> SourceLocation IfLoc = CurLoc;<br>
><br>
> getNextToken(); // eat the if.<br>
><br>
> // condition.<br>
> - ExprAST *Cond = ParseExpression();<br>
> + auto Cond = ParseExpression();<br>
> if (!Cond)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_then)<br>
> return Error("expected then");<br>
> getNextToken(); // eat the then<br>
><br>
> - ExprAST *Then = ParseExpression();<br>
> - if (Then == 0)<br>
> - return 0;<br>
> + auto Then = ParseExpression();<br>
> + if (!Then)<br>
> + return nullptr;<br>
><br>
> if (CurTok != tok_else)<br>
> return Error("expected else");<br>
><br>
> getNextToken();<br>
><br>
> - ExprAST *Else = ParseExpression();<br>
> + auto Else = ParseExpression();<br>
> if (!Else)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return new IfExprAST(IfLoc, Cond, Then, Else);<br>
> + return llvm::make_unique<IfExprAST>(IfLoc, std::move(Cond), std::move(Then),<br>
> + std::move(Else));<br>
> }<br>
><br>
> /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression<br>
> -static ExprAST *ParseForExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseForExpr() {<br>
> getNextToken(); // eat the for.<br>
><br>
> if (CurTok != tok_identifier)<br>
> @@ -545,43 +540,44 @@<br>
> return Error("expected '=' after for");<br>
> getNextToken(); // eat '='.<br>
><br>
> - ExprAST *Start = ParseExpression();<br>
> - if (Start == 0)<br>
> - return 0;<br>
> + auto Start = ParseExpression();<br>
> + if (!Start)<br>
> + return nullptr;<br>
> if (CurTok != ',')<br>
> return Error("expected ',' after for start value");<br>
> getNextToken();<br>
><br>
> - ExprAST *End = ParseExpression();<br>
> - if (End == 0)<br>
> - return 0;<br>
> + auto End = ParseExpression();<br>
> + if (!End)<br>
> + return nullptr;<br>
><br>
> // The step value is optional.<br>
> - ExprAST *Step = 0;<br>
> + std::unique_ptr<ExprAST> Step;<br>
> if (CurTok == ',') {<br>
> getNextToken();<br>
> Step = ParseExpression();<br>
> - if (Step == 0)<br>
> - return 0;<br>
> + if (!Step)<br>
> + return nullptr;<br>
> }<br>
><br>
> if (CurTok != tok_in)<br>
> return Error("expected 'in' after for");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0)<br>
> - return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body)<br>
> + return nullptr;<br>
><br>
> - return new ForExprAST(IdName, Start, End, Step, Body);<br>
> + return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),<br>
> + std::move(Step), std::move(Body));<br>
> }<br>
><br>
> /// varexpr ::= 'var' identifier ('=' expression)?<br>
> // (',' identifier ('=' expression)?)* 'in' expression<br>
> -static ExprAST *ParseVarExpr() {<br>
> +static std::unique_ptr<ExprAST> ParseVarExpr() {<br>
> getNextToken(); // eat the var.<br>
><br>
> - std::vector<std::pair<std::string, ExprAST *> > VarNames;<br>
> + std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;<br>
><br>
> // At least one variable name is required.<br>
> if (CurTok != tok_identifier)<br>
> @@ -592,16 +588,16 @@<br>
> getNextToken(); // eat identifier.<br>
><br>
> // Read the optional initializer.<br>
> - ExprAST *Init = 0;<br>
> + std::unique_ptr<ExprAST> Init = nullptr;<br>
> if (CurTok == '=') {<br>
> getNextToken(); // eat the '='.<br>
><br>
> Init = ParseExpression();<br>
> - if (Init == 0)<br>
> - return 0;<br>
> + if (!Init)<br>
> + return nullptr;<br>
> }<br>
><br>
> - VarNames.push_back(std::make_pair(Name, Init));<br>
> + VarNames.push_back(std::make_pair(Name, std::move(Init)));<br>
><br>
> // End of var list, exit loop.<br>
> if (CurTok != ',')<br>
> @@ -617,11 +613,11 @@<br>
> return Error("expected 'in' keyword after 'var'");<br>
> getNextToken(); // eat 'in'.<br>
><br>
> - ExprAST *Body = ParseExpression();<br>
> - if (Body == 0)<br>
> - return 0;<br>
> + auto Body = ParseExpression();<br>
> + if (!Body)<br>
> + return nullptr;<br>
><br>
> - return new VarExprAST(VarNames, Body);<br>
> + return llvm::make_unique<VarExprAST>(std::move(VarNames), std::move(Body));<br>
> }<br>
><br>
> /// primary<br>
> @@ -631,7 +627,7 @@<br>
> /// ::= ifexpr<br>
> /// ::= forexpr<br>
> /// ::= varexpr<br>
> -static ExprAST *ParsePrimary() {<br>
> +static std::unique_ptr<ExprAST> ParsePrimary() {<br>
> switch (CurTok) {<br>
> default:<br>
> return Error("unknown token when expecting an expression");<br>
> @@ -653,7 +649,7 @@<br>
> /// unary<br>
> /// ::= primary<br>
> /// ::= '!' unary<br>
> -static ExprAST *ParseUnary() {<br>
> +static std::unique_ptr<ExprAST> ParseUnary() {<br>
> // If the current token is not an operator, it must be a primary expr.<br>
> if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')<br>
> return ParsePrimary();<br>
> @@ -661,14 +657,14 @@<br>
> // If this is a unary operator, read it.<br>
> int Opc = CurTok;<br>
> getNextToken();<br>
> - if (ExprAST *Operand = ParseUnary())<br>
> - return new UnaryExprAST(Opc, Operand);<br>
> - return 0;<br>
> + if (auto Operand = ParseUnary())<br>
> + return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// binoprhs<br>
> /// ::= ('+' unary)*<br>
> -static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {<br>
> +static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) {<br>
> // If this is a binop, find its precedence.<br>
> while (1) {<br>
> int TokPrec = GetTokPrecedence();<br>
> @@ -684,40 +680,41 @@<br>
> getNextToken(); // eat binop<br>
><br>
> // Parse the unary expression after the binary operator.<br>
> - ExprAST *RHS = ParseUnary();<br>
> + auto RHS = ParseUnary();<br>
> if (!RHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> // If BinOp binds less tightly with RHS than the operator after RHS, let<br>
> // the pending operator take RHS as its LHS.<br>
> int NextPrec = GetTokPrecedence();<br>
> if (TokPrec < NextPrec) {<br>
> - RHS = ParseBinOpRHS(TokPrec + 1, RHS);<br>
> - if (RHS == 0)<br>
> - return 0;<br>
> + RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));<br>
> + if (!RHS)<br>
> + return nullptr;<br>
> }<br>
><br>
> // Merge LHS/RHS.<br>
> - LHS = new BinaryExprAST(BinLoc, BinOp, LHS, RHS);<br>
> + LHS = llvm::make_unique<BinaryExprAST>(BinLoc, BinOp, std::move(LHS),<br>
> + std::move(RHS));<br>
> }<br>
> }<br>
><br>
> /// expression<br>
> /// ::= unary binoprhs<br>
> ///<br>
> -static ExprAST *ParseExpression() {<br>
> - ExprAST *LHS = ParseUnary();<br>
> +static std::unique_ptr<ExprAST> ParseExpression() {<br>
> + auto LHS = ParseUnary();<br>
> if (!LHS)<br>
> - return 0;<br>
> + return nullptr;<br>
><br>
> - return ParseBinOpRHS(0, LHS);<br>
> + return ParseBinOpRHS(0, std::move(LHS));<br>
> }<br>
><br>
> /// prototype<br>
> /// ::= id '(' id* ')'<br>
> /// ::= binary LETTER number? (id, id)<br>
> /// ::= unary LETTER (id)<br>
> -static PrototypeAST *ParsePrototype() {<br>
> +static std::unique_ptr<PrototypeAST> ParsePrototype() {<br>
> std::string FnName;<br>
><br>
> SourceLocation FnLoc = CurLoc;<br>
> @@ -777,35 +774,36 @@<br>
> if (Kind && ArgNames.size() != Kind)<br>
> return ErrorP("Invalid number of operands for operator");<br>
><br>
> - return new PrototypeAST(FnLoc, FnName, ArgNames, Kind != 0, BinaryPrecedence);<br>
> + return llvm::make_unique<PrototypeAST>(FnLoc, FnName, ArgNames, Kind != 0,<br>
> + BinaryPrecedence);<br>
> }<br>
><br>
> /// definition ::= 'def' prototype expression<br>
> -static FunctionAST *ParseDefinition() {<br>
> +static std::unique_ptr<FunctionAST> ParseDefinition() {<br>
> getNextToken(); // eat def.<br>
> - PrototypeAST *Proto = ParsePrototype();<br>
> - if (Proto == 0)<br>
> - return 0;<br>
> + auto Proto = ParsePrototype();<br>
> + if (!Proto)<br>
> + return nullptr;<br>
><br>
> - if (ExprAST *E = ParseExpression())<br>
> - return new FunctionAST(Proto, E);<br>
> - return 0;<br>
> + if (auto E = ParseExpression())<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> + return nullptr;<br>
> }<br>
><br>
> /// toplevelexpr ::= expression<br>
> -static FunctionAST *ParseTopLevelExpr() {<br>
> +static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {<br>
> SourceLocation FnLoc = CurLoc;<br>
> - if (ExprAST *E = ParseExpression()) {<br>
> + if (auto E = ParseExpression()) {<br>
> // Make an anonymous proto.<br>
> - PrototypeAST *Proto =<br>
> - new PrototypeAST(FnLoc, "main", std::vector<std::string>());<br>
> - return new FunctionAST(Proto, E);<br>
> + auto Proto =<br>
> + llvm::make_unique<PrototypeAST>(FnLoc, "main", std::vector<std::string>());<br>
> + return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));<br>
> }<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// external ::= 'extern' prototype<br>
> -static PrototypeAST *ParseExtern() {<br>
> +static std::unique_ptr<PrototypeAST> ParseExtern() {<br>
> getNextToken(); // eat extern.<br>
> return ParsePrototype();<br>
> }<br>
> @@ -860,7 +858,7 @@<br>
><br>
> Value *ErrorV(const char *Str) {<br>
> Error(Str);<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of<br>
> @@ -881,7 +879,7 @@<br>
> Value *VariableExprAST::Codegen() {<br>
> // Look this variable up in the function.<br>
> Value *V = NamedValues[Name];<br>
> - if (V == 0)<br>
> + if (!V)<br>
> return ErrorV("Unknown variable name");<br>
><br>
> KSDbgInfo.emitLocation(this);<br>
> @@ -891,11 +889,11 @@<br>
><br>
> Value *UnaryExprAST::Codegen() {<br>
> Value *OperandV = Operand->Codegen();<br>
> - if (OperandV == 0)<br>
> - return 0;<br>
> + if (!OperandV)<br>
> + return nullptr;<br>
><br>
> Function *F = TheModule->getFunction(std::string("unary") + Opcode);<br>
> - if (F == 0)<br>
> + if (!F)<br>
> return ErrorV("Unknown unary operator");<br>
><br>
> KSDbgInfo.emitLocation(this);<br>
> @@ -911,17 +909,17 @@<br>
> // This assume we're building without RTTI because LLVM builds that way by<br>
> // default. If you build LLVM with RTTI this can be changed to a<br>
> // dynamic_cast for automatic error checking.<br>
> - VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS);<br>
> + VariableExprAST *LHSE = static_cast<VariableExprAST*>(LHS.get());<br>
> if (!LHSE)<br>
> return ErrorV("destination of '=' must be a variable");<br>
> // Codegen the RHS.<br>
> Value *Val = RHS->Codegen();<br>
> - if (Val == 0)<br>
> - return 0;<br>
> + if (!Val)<br>
> + return nullptr;<br>
><br>
> // Look up the name.<br>
> Value *Variable = NamedValues[LHSE->getName()];<br>
> - if (Variable == 0)<br>
> + if (!Variable)<br>
> return ErrorV("Unknown variable name");<br>
><br>
> Builder.CreateStore(Val, Variable);<br>
> @@ -930,8 +928,8 @@<br>
><br>
> Value *L = LHS->Codegen();<br>
> Value *R = RHS->Codegen();<br>
> - if (L == 0 || R == 0)<br>
> - return 0;<br>
> + if (!L || !R)<br>
> + return nullptr;<br>
><br>
> switch (Op) {<br>
> case '+':<br>
> @@ -963,7 +961,7 @@<br>
><br>
> // Look up the name in the global module table.<br>
> Function *CalleeF = TheModule->getFunction(Callee);<br>
> - if (CalleeF == 0)<br>
> + if (!CalleeF)<br>
> return ErrorV("Unknown function referenced");<br>
><br>
> // If argument mismatch error.<br>
> @@ -973,8 +971,8 @@<br>
> std::vector<Value *> ArgsV;<br>
> for (unsigned i = 0, e = Args.size(); i != e; ++i) {<br>
> ArgsV.push_back(Args[i]->Codegen());<br>
> - if (ArgsV.back() == 0)<br>
> - return 0;<br>
> + if (!ArgsV.back())<br>
> + return nullptr;<br>
> }<br>
><br>
> return Builder.CreateCall(CalleeF, ArgsV, "calltmp");<br>
> @@ -984,8 +982,8 @@<br>
> KSDbgInfo.emitLocation(this);<br>
><br>
> Value *CondV = Cond->Codegen();<br>
> - if (CondV == 0)<br>
> - return 0;<br>
> + if (!CondV)<br>
> + return nullptr;<br>
><br>
> // Convert condition to a bool by comparing equal to 0.0.<br>
> CondV = Builder.CreateFCmpONE(<br>
> @@ -1006,8 +1004,8 @@<br>
> Builder.SetInsertPoint(ThenBB);<br>
><br>
> Value *ThenV = Then->Codegen();<br>
> - if (ThenV == 0)<br>
> - return 0;<br>
> + if (!ThenV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Then' can change the current block, update ThenBB for the PHI.<br>
> @@ -1018,8 +1016,8 @@<br>
> Builder.SetInsertPoint(ElseBB);<br>
><br>
> Value *ElseV = Else->Codegen();<br>
> - if (ElseV == 0)<br>
> - return 0;<br>
> + if (!ElseV)<br>
> + return nullptr;<br>
><br>
> Builder.CreateBr(MergeBB);<br>
> // Codegen of 'Else' can change the current block, update ElseBB for the PHI.<br>
> @@ -1066,8 +1064,8 @@<br>
><br>
> // Emit the start code first, without 'variable' in scope.<br>
> Value *StartVal = Start->Codegen();<br>
> - if (StartVal == 0)<br>
> - return 0;<br>
> + if (!StartVal)<br>
> + return nullptr;<br>
><br>
> // Store the value into the alloca.<br>
> Builder.CreateStore(StartVal, Alloca);<br>
> @@ -1091,15 +1089,15 @@<br>
> // Emit the body of the loop. This, like any other expr, can change the<br>
> // current BB. Note that we ignore the value computed by the body, but don't<br>
> // allow an error.<br>
> - if (Body->Codegen() == 0)<br>
> - return 0;<br>
> + if (!Body->Codegen())<br>
> + return nullptr;<br>
><br>
> // Emit the step value.<br>
> Value *StepVal;<br>
> if (Step) {<br>
> StepVal = Step->Codegen();<br>
> - if (StepVal == 0)<br>
> - return 0;<br>
> + if (!StepVal)<br>
> + return nullptr;<br>
> } else {<br>
> // If not specified, use 1.0.<br>
> StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));<br>
> @@ -1107,7 +1105,7 @@<br>
><br>
> // Compute the end condition.<br>
> Value *EndCond = End->Codegen();<br>
> - if (EndCond == 0)<br>
> + if (!EndCond)<br>
> return EndCond;<br>
><br>
> // Reload, increment, and restore the alloca. This handles the case where<br>
> @@ -1148,7 +1146,7 @@<br>
> // Register all variables and emit their initializer.<br>
> for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {<br>
> const std::string &VarName = VarNames[i].first;<br>
> - ExprAST *Init = VarNames[i].second;<br>
> + ExprAST *Init = VarNames[i].second.get();<br>
><br>
> // Emit the initializer before adding the variable to scope, this prevents<br>
> // the initializer from referencing the variable itself, and permits stuff<br>
> @@ -1158,8 +1156,8 @@<br>
> Value *InitVal;<br>
> if (Init) {<br>
> InitVal = Init->Codegen();<br>
> - if (InitVal == 0)<br>
> - return 0;<br>
> + if (!InitVal)<br>
> + return nullptr;<br>
> } else { // If not specified, use 0.0.<br>
> InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));<br>
> }<br>
> @@ -1179,8 +1177,8 @@<br>
><br>
> // Codegen the body, now that all vars are in scope.<br>
> Value *BodyVal = Body->Codegen();<br>
> - if (BodyVal == 0)<br>
> - return 0;<br>
> + if (!BodyVal)<br>
> + return nullptr;<br>
><br>
> // Pop all our variables from scope.<br>
> for (unsigned i = 0, e = VarNames.size(); i != e; ++i)<br>
> @@ -1210,13 +1208,13 @@<br>
> // If F already has a body, reject this.<br>
> if (!F->empty()) {<br>
> ErrorF("redefinition of function");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> // If F took a different number of args, reject.<br>
> if (F->arg_size() != Args.size()) {<br>
> ErrorF("redefinition of function with different # args");<br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
> }<br>
><br>
> @@ -1272,11 +1270,11 @@<br>
> NamedValues.clear();<br>
><br>
> Function *TheFunction = Proto->Codegen();<br>
> - if (TheFunction == 0)<br>
> - return 0;<br>
> + if (!TheFunction)<br>
> + return nullptr;<br>
><br>
> // Push the current scope.<br>
> - KSDbgInfo.LexicalBlocks.push_back(KSDbgInfo.FnScopeMap[Proto]);<br>
> + KSDbgInfo.LexicalBlocks.push_back(KSDbgInfo.FnScopeMap[Proto.get()]);<br>
><br>
> // Unset the location for the prologue emission (leading instructions with no<br>
> // location in a function are considered part of the prologue and the debugger<br>
> @@ -1294,7 +1292,7 @@<br>
> // Add all arguments to the symbol table and create their allocas.<br>
> Proto->CreateArgumentAllocas(TheFunction);<br>
><br>
> - KSDbgInfo.emitLocation(Body);<br>
> + KSDbgInfo.emitLocation(Body.get());<br>
><br>
> if (Value *RetVal = Body->Codegen()) {<br>
> // Finish off the function.<br>
> @@ -1322,7 +1320,7 @@<br>
> // unconditionally.<br>
> KSDbgInfo.LexicalBlocks.pop_back();<br>
><br>
> - return 0;<br>
> + return nullptr;<br>
> }<br>
><br>
> //===----------------------------------------------------------------------===//<br>
> @@ -1332,8 +1330,8 @@<br>
> static ExecutionEngine *TheExecutionEngine;<br>
><br>
> static void HandleDefinition() {<br>
> - if (FunctionAST *F = ParseDefinition()) {<br>
> - if (!F->Codegen()) {<br>
> + if (auto FnAST = ParseDefinition()) {<br>
> + if (!FnAST->Codegen()) {<br>
> fprintf(stderr, "Error reading function definition:");<br>
> }<br>
> } else {<br>
> @@ -1343,8 +1341,8 @@<br>
> }<br>
><br>
> static void HandleExtern() {<br>
> - if (PrototypeAST *P = ParseExtern()) {<br>
> - if (!P->Codegen()) {<br>
> + if (auto ProtoAST = ParseExtern()) {<br>
> + if (!ProtoAST->Codegen()) {<br>
> fprintf(stderr, "Error reading extern");<br>
> }<br>
> } else {<br>
> @@ -1355,8 +1353,8 @@<br>
><br>
> static void HandleTopLevelExpression() {<br>
> // Evaluate a top-level expression into an anonymous function.<br>
> - if (FunctionAST *F = ParseTopLevelExpr()) {<br>
> - if (!F->Codegen()) {<br>
> + if (auto FnAST = ParseTopLevelExpr()) {<br>
> + if (!FnAST->Codegen()) {<br>
> fprintf(stderr, "Error generating code for top level expr");<br>
> }<br>
> } else {<br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>