Add the FullExprArg wrapper and use it for if statement conditions.

llvm-svn: 71982
This commit is contained in:
Anders Carlsson 2009-05-17 18:26:53 +00:00
parent 23ca0b4869
commit 5262746c28
7 changed files with 57 additions and 13 deletions

View File

@ -102,6 +102,34 @@ public:
typedef ASTMultiPtr<&ActionBase::DeleteStmt> MultiStmtArg;
typedef ASTMultiPtr<&ActionBase::DeleteTemplateParams> MultiTemplateParamsArg;
class FullExprArg {
public:
FullExprArg(const FullExprArg& Other)
: Expr(move(const_cast<FullExprArg&>(Other).Expr)) {}
OwningExprResult release() {
return move(Expr);
}
ExprArg* operator->() {
return &Expr;
}
private:
// FIXME: No need to make the entire Action class a friend when it's just
// Action::FullExpr that needs access to the constructor below.
friend class Action;
explicit FullExprArg(ExprArg expr)
: Expr(move(expr)) {}
ExprArg Expr;
};
FullExprArg FullExpr(ExprArg &Arg) {
return FullExprArg(ActOnFinishFullExpr(move(Arg)));
}
// Utilities for Action implementations to return smart results.
OwningExprResult ExprError() { return OwningExprResult(*this, true); }
@ -461,8 +489,9 @@ public:
return StmtEmpty();
}
virtual OwningStmtResult ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
StmtArg ThenVal, SourceLocation ElseLoc,
virtual OwningStmtResult ActOnIfStmt(SourceLocation IfLoc,
FullExprArg CondVal, StmtArg ThenVal,
SourceLocation ElseLoc,
StmtArg ElseVal) {
return StmtEmpty();
}
@ -1042,6 +1071,13 @@ public:
return ExprEmpty();
}
/// ActOnFinishFullExpr - Called whenever a full expression has been parsed.
/// (C++ [intro.execution]p12).
virtual OwningExprResult ActOnFinishFullExpr(ExprArg Expr) {
return move(Expr);
}
//===---------------------------- C++ Classes ---------------------------===//
/// ActOnBaseSpecifier - Parsed a base specifier
virtual BaseResult ActOnBaseSpecifier(DeclPtrTy classdecl,

View File

@ -141,6 +141,7 @@ public:
typedef Action::ExprArg ExprArg;
typedef Action::MultiStmtArg MultiStmtArg;
typedef Action::FullExprArg FullExprArg;
/// Adorns a ExprResult with Actions to make it an OwningExprResult
OwningExprResult Owned(ExprResult res) {

View File

@ -632,7 +632,7 @@ Parser::OwningStmtResult Parser::ParseIfStatement() {
if (ElseStmt.isInvalid())
ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
return Actions.ActOnIfStmt(IfLoc, move(CondExp), move(ThenStmt),
return Actions.ActOnIfStmt(IfLoc, Actions.FullExpr(CondExp), move(ThenStmt),
ElseLoc, move(ElseStmt));
}

View File

@ -1172,9 +1172,9 @@ public:
IdentifierInfo *II,
SourceLocation ColonLoc,
StmtArg SubStmt);
virtual OwningStmtResult ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
StmtArg ThenVal, SourceLocation ElseLoc,
StmtArg ElseVal);
virtual OwningStmtResult ActOnIfStmt(SourceLocation IfLoc,
FullExprArg CondVal, StmtArg ThenVal,
SourceLocation ElseLoc, StmtArg ElseVal);
virtual OwningStmtResult ActOnStartOfSwitchStmt(ExprArg Cond);
virtual OwningStmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc,
StmtArg Switch, StmtArg Body);

View File

@ -180,17 +180,19 @@ Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
}
Action::OwningStmtResult
Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal,
StmtArg ThenVal, SourceLocation ElseLoc,
StmtArg ElseVal) {
Expr *condExpr = CondVal.takeAs<Expr>();
OwningExprResult CondResult(CondVal.release());
Expr *condExpr = CondResult.takeAs<Expr>();
assert(condExpr && "ActOnIfStmt(): missing expression");
if (!condExpr->isTypeDependent()) {
DefaultFunctionArrayConversion(condExpr);
// Take ownership again until we're past the error checking.
CondVal = condExpr;
CondResult = condExpr;
QualType condType = condExpr->getType();
if (getLangOptions().CPlusPlus) {
@ -213,7 +215,7 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
}
CondVal.release();
CondResult.release();
return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
ElseLoc, ElseVal.takeAs<Stmt>()));
}

View File

@ -25,6 +25,10 @@ namespace {
Sema &SemaRef;
const TemplateArgumentList &TemplateArgs;
Sema::FullExprArg FullExpr(Sema::ExprArg &expr) {
return SemaRef.FullExpr(expr);
}
public:
typedef Sema::OwningExprResult OwningExprResult;
typedef Sema::OwningStmtResult OwningStmtResult;
@ -225,7 +229,7 @@ Sema::OwningStmtResult TemplateStmtInstantiator::VisitIfStmt(IfStmt *S) {
if (Else.isInvalid())
return SemaRef.StmtError();
return SemaRef.ActOnIfStmt(S->getIfLoc(), move(Cond), move(Then),
return SemaRef.ActOnIfStmt(S->getIfLoc(), FullExpr(Cond), move(Then),
S->getElseLoc(), move(Else));
}

View File

@ -296,8 +296,9 @@ namespace {
return StmtEmpty();
}
virtual OwningStmtResult ActOnIfStmt(SourceLocation IfLoc, ExprArg CondVal,
StmtArg ThenVal,SourceLocation ElseLoc,
virtual OwningStmtResult ActOnIfStmt(SourceLocation IfLoc,
FullExprArg CondVal, StmtArg ThenVal,
SourceLocation ElseLoc,
StmtArg ElseVal) {
llvm::cout << __FUNCTION__ << "\n";
return StmtEmpty();