From: Sebastian Redl Date: Fri, 16 Jan 2009 23:28:06 +0000 (+0000) Subject: Convert some more statement actions to smart pointers. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f05b1520d6f175acbfc3913489f4dfa842875ec4;p=clang Convert some more statement actions to smart pointers. Fix a type error; parser wanted to pass the third part of a for-statement as a statement; should be expression. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62380 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/PrintParserCallbacks.cpp b/Driver/PrintParserCallbacks.cpp index 7999da01d9..c04dbdc3ac 100644 --- a/Driver/PrintParserCallbacks.cpp +++ b/Driver/PrintParserCallbacks.cpp @@ -312,29 +312,31 @@ namespace { return StmtEmpty(); } - virtual StmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, - StmtTy *Body) { + virtual OwningStmtResult ActOnWhileStmt(SourceLocation WhileLoc, + ExprArg Cond, StmtArg Body) { llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } - virtual StmtResult ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, - SourceLocation WhileLoc, ExprTy *Cond) { + virtual OwningStmtResult ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, + SourceLocation WhileLoc, ExprArg Cond){ llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } - virtual StmtResult ActOnForStmt(SourceLocation ForLoc, - SourceLocation LParenLoc, - StmtTy *First, ExprTy *Second, ExprTy *Third, - SourceLocation RParenLoc, StmtTy *Body) { + virtual OwningStmtResult ActOnForStmt(SourceLocation ForLoc, + SourceLocation LParenLoc, + StmtArg First, ExprArg Second, + ExprArg Third, SourceLocation RParenLoc, + StmtArg Body) { llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } - virtual StmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, - SourceLocation LParenLoc, - StmtTy *First, ExprTy *Second, - SourceLocation RParenLoc, StmtTy *Body) { + virtual OwningStmtResult ActOnObjCForCollectionStmt( + SourceLocation ForColLoc, + SourceLocation LParenLoc, + StmtArg First, ExprArg Second, + SourceLocation RParenLoc, StmtArg Body) { llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } virtual StmtResult ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 009858959e..d0891bb350 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -416,25 +416,26 @@ public: return StmtEmpty(); } - virtual StmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, - StmtTy *Body) { - return 0; + virtual OwningStmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, + StmtArg Body) { + return StmtEmpty(); } - virtual StmtResult ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, - SourceLocation WhileLoc, ExprTy *Cond) { - return 0; + virtual OwningStmtResult ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, + SourceLocation WhileLoc, ExprArg Cond) { + return StmtEmpty(); } - virtual StmtResult ActOnForStmt(SourceLocation ForLoc, - SourceLocation LParenLoc, - StmtTy *First, ExprTy *Second, ExprTy *Third, - SourceLocation RParenLoc, StmtTy *Body) { - return 0; + virtual OwningStmtResult ActOnForStmt(SourceLocation ForLoc, + SourceLocation LParenLoc, + StmtArg First, ExprArg Second, + ExprArg Third, SourceLocation RParenLoc, + StmtArg Body) { + return StmtEmpty(); } - virtual StmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, - SourceLocation LParenLoc, - StmtTy *First, ExprTy *Second, - SourceLocation RParenLoc, StmtTy *Body) { - return 0; + virtual OwningStmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, + SourceLocation LParenLoc, + StmtArg First, ExprArg Second, + SourceLocation RParenLoc, StmtArg Body) { + return StmtEmpty(); } virtual StmtResult ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index 153aeb824e..1bc78583ed 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -722,7 +722,8 @@ Parser::OwningStmtResult Parser::ParseWhileStatement() { if (Cond.isInvalid() || Body.isInvalid()) return StmtError(); - return Owned(Actions.ActOnWhileStmt(WhileLoc, Cond.release(),Body.release())); + return Actions.ActOnWhileStmt(WhileLoc, move_convert(Cond), + move_convert(Body)); } /// ParseDoStatement @@ -786,8 +787,8 @@ Parser::OwningStmtResult Parser::ParseDoStatement() { if (Cond.isInvalid() || Body.isInvalid()) return StmtError(); - return Owned(Actions.ActOnDoStmt(DoLoc, Body.release(), WhileLoc, - Cond.release())); + return Actions.ActOnDoStmt(DoLoc, move_convert(Body), WhileLoc, + move_convert(Cond)); } /// ParseForStatement @@ -843,8 +844,8 @@ Parser::OwningStmtResult Parser::ParseForStatement() { OwningExprResult Value(Actions); bool ForEach = false; - OwningStmtResult FirstPart(Actions), ThirdPart(Actions); - OwningExprResult SecondPart(Actions); + OwningStmtResult FirstPart(Actions); + OwningExprResult SecondPart(Actions), ThirdPart(Actions); // Parse the first part of the for specifier. if (Tok.is(tok::semi)) { // for (; @@ -903,11 +904,7 @@ Parser::OwningStmtResult Parser::ParseForStatement() { if (Tok.is(tok::r_paren)) { // for (...;...;) // no third part. } else { - Value = ParseExpression(); - if (!Value.isInvalid()) { - // Turn the expression into a stmt. - ThirdPart = Actions.ActOnExprStmt(move_convert(Value)); - } + ThirdPart = ParseExpression(); } } // Match the ')'. @@ -940,14 +937,14 @@ Parser::OwningStmtResult Parser::ParseForStatement() { return StmtError(); if (!ForEach) - return Owned(Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.release(), - SecondPart.release(), ThirdPart.release(), - RParenLoc, Body.release())); + return Actions.ActOnForStmt(ForLoc, LParenLoc, move_convert(FirstPart), + move_convert(SecondPart), move_convert(ThirdPart), + RParenLoc, move_convert(Body)); else - return Owned(Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, - FirstPart.release(), - SecondPart.release(), - RParenLoc, Body.release())); + return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, + move_convert(FirstPart), + move_convert(SecondPart), + RParenLoc, move_convert(Body)); } /// ParseGotoStatement diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 668cf5b7c4..be9da6bd80 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -873,19 +873,20 @@ public: virtual OwningStmtResult ActOnStartOfSwitchStmt(ExprArg Cond); virtual OwningStmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, StmtArg Body); - virtual StmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, - StmtTy *Body); - virtual StmtResult ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, - SourceLocation WhileLoc, ExprTy *Cond); - - virtual StmtResult ActOnForStmt(SourceLocation ForLoc, - SourceLocation LParenLoc, - StmtTy *First, ExprTy *Second, ExprTy *Third, - SourceLocation RParenLoc, StmtTy *Body); - virtual StmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, - SourceLocation LParenLoc, - StmtTy *First, ExprTy *Second, - SourceLocation RParenLoc, StmtTy *Body); + virtual OwningStmtResult ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, + StmtArg Body); + virtual OwningStmtResult ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, + SourceLocation WhileLoc, ExprArg Cond); + + virtual OwningStmtResult ActOnForStmt(SourceLocation ForLoc, + SourceLocation LParenLoc, + StmtArg First, ExprArg Second, + ExprArg Third, SourceLocation RParenLoc, + StmtArg Body); + virtual OwningStmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, + SourceLocation LParenLoc, + StmtArg First, ExprArg Second, + SourceLocation RParenLoc, StmtArg Body); virtual StmtResult ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 34f9f65cd7..7a41f4d976 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -532,52 +532,57 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, return Owned(SS); } -Action::StmtResult -Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { - Expr *condExpr = (Expr *)Cond; +Action::OwningStmtResult +Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) { + Expr *condExpr = (Expr *)Cond.release(); assert(condExpr && "ActOnWhileStmt(): missing expression"); - + DefaultFunctionArrayConversion(condExpr); + Cond = condExpr; QualType condType = condExpr->getType(); - + if (getLangOptions().CPlusPlus) { if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 - return true; + return StmtError(); } else if (!condType->isScalarType()) // C99 6.8.5p2 - return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar) - << condType << condExpr->getSourceRange(); + return StmtError(Diag(WhileLoc, + diag::err_typecheck_statement_requires_scalar) + << condType << condExpr->getSourceRange()); - return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); + Cond.release(); + return Owned(new WhileStmt(condExpr, (Stmt*)Body.release(), WhileLoc)); } -Action::StmtResult -Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body, - SourceLocation WhileLoc, ExprTy *Cond) { - Expr *condExpr = (Expr *)Cond; +Action::OwningStmtResult +Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, + SourceLocation WhileLoc, ExprArg Cond) { + Expr *condExpr = (Expr *)Cond.release(); assert(condExpr && "ActOnDoStmt(): missing expression"); - + DefaultFunctionArrayConversion(condExpr); + Cond = condExpr; QualType condType = condExpr->getType(); - + if (getLangOptions().CPlusPlus) { if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 - return true; + return StmtError(); } else if (!condType->isScalarType()) // C99 6.8.5p2 - return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar) - << condType << condExpr->getSourceRange(); + return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar) + << condType << condExpr->getSourceRange()); - return new DoStmt((Stmt*)Body, condExpr, DoLoc); + Cond.release(); + return Owned(new DoStmt((Stmt*)Body.release(), condExpr, DoLoc)); } -Action::StmtResult -Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, - StmtTy *first, ExprTy *second, ExprTy *third, - SourceLocation RParenLoc, StmtTy *body) { - Stmt *First = static_cast(first); - Expr *Second = static_cast(second); - Expr *Third = static_cast(third); - Stmt *Body = static_cast(body); - +Action::OwningStmtResult +Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, + StmtArg first, ExprArg second, ExprArg third, + SourceLocation RParenLoc, StmtArg body) { + Stmt *First = static_cast(first.get()); + Expr *Second = static_cast(second.get()); + Expr *Third = static_cast(third.get()); + Stmt *Body = static_cast(body.get()); + if (!getLangOptions().CPlusPlus) { if (DeclStmt *DS = dyn_cast_or_null(First)) { // C99 6.8.5p3: The declaration part of a 'for' statement shall only @@ -597,32 +602,37 @@ Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, if (Second) { DefaultFunctionArrayConversion(Second); QualType SecondType = Second->getType(); - + if (getLangOptions().CPlusPlus) { if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4 - return true; + return StmtError(); } else if (!SecondType->isScalarType()) // C99 6.8.5p2 - return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar) - << SecondType << Second->getSourceRange(); - } - return new ForStmt(First, Second, Third, Body, ForLoc); + return StmtError(Diag(ForLoc, + diag::err_typecheck_statement_requires_scalar) + << SecondType << Second->getSourceRange()); + } + first.release(); + second.release(); + third.release(); + body.release(); + return Owned(new ForStmt(First, Second, Third, Body, ForLoc)); } -Action::StmtResult -Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, - SourceLocation LParenLoc, - StmtTy *first, ExprTy *second, - SourceLocation RParenLoc, StmtTy *body) { - Stmt *First = static_cast(first); - Expr *Second = static_cast(second); - Stmt *Body = static_cast(body); +Action::OwningStmtResult +Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, + SourceLocation LParenLoc, + StmtArg first, ExprArg second, + SourceLocation RParenLoc, StmtArg body) { + Stmt *First = static_cast(first.get()); + Expr *Second = static_cast(second.get()); + Stmt *Body = static_cast(body.get()); if (First) { QualType FirstType; if (DeclStmt *DS = dyn_cast(First)) { if (!DS->hasSolitaryDecl()) - return Diag((*DS->decl_begin())->getLocation(), - diag::err_toomany_element_decls); - + return StmtError(Diag((*DS->decl_begin())->getLocation(), + diag::err_toomany_element_decls)); + ScopedDecl *D = DS->getSolitaryDecl(); FirstType = cast(D)->getType(); // C99 6.8.5p3: The declaration part of a 'for' statement shall only @@ -630,15 +640,17 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, // 'register'. VarDecl *VD = cast(D); if (VD->isBlockVarDecl() && !VD->hasLocalStorage()) - return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for); + return StmtError(Diag(VD->getLocation(), + diag::err_non_variable_decl_in_for)); } else { Expr::isLvalueResult lval = cast(First)->isLvalue(Context); - + if (lval != Expr::LV_Valid) - return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue) - << First->getSourceRange(); + return StmtError(Diag(First->getLocStart(), + diag::err_selector_element_not_lvalue) + << First->getSourceRange()); - FirstType = static_cast(first)->getType(); + FirstType = static_cast(First)->getType(); } if (!Context.isObjCObjectPointerType(FirstType)) Diag(ForLoc, diag::err_selector_element_type) @@ -651,7 +663,11 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, Diag(ForLoc, diag::err_collection_expr_type) << SecondType << Second->getSourceRange(); } - return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc); + first.release(); + second.release(); + body.release(); + return Owned(new ObjCForCollectionStmt(First, Second, Body, + ForLoc, RParenLoc)); } Action::StmtResult