Eliminate CXXConditionDeclExpr with extreme prejudice.
All statements that involve conditions can now hold on to a separate
condition declaration (a VarDecl), and will use a DeclRefExpr
referring to that VarDecl for the condition expression. ForStmts now
have such a VarDecl (I'd missed those in previous commits).
Also, since this change reworks the Action interface for
if/while/switch/for, use FullExprArg for the full expressions in those
expressions, to ensure that we're emitting
Note that we are (still) not generating the right cleanups for
condition variables in for statements. That will be a follow-on
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89817 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index 202e16e..1071aeb 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -95,7 +95,8 @@
typedef Sema::ExprArg ExprArg;
typedef Sema::MultiExprArg MultiExprArg;
typedef Sema::MultiStmtArg MultiStmtArg;
-
+ typedef Sema::DeclPtrTy DeclPtrTy;
+
/// \brief Initializes a new tree transformer.
TreeTransform(Sema &SemaRef) : SemaRef(SemaRef) { }
@@ -665,17 +666,19 @@
/// By default, performs semantic analysis to build the new statement.
/// Subclasses may override this routine to provide different behavior.
OwningStmtResult RebuildIfStmt(SourceLocation IfLoc, Sema::FullExprArg Cond,
- StmtArg Then, SourceLocation ElseLoc,
- StmtArg Else) {
- return getSema().ActOnIfStmt(IfLoc, Cond, move(Then), ElseLoc, move(Else));
+ VarDecl *CondVar, StmtArg Then,
+ SourceLocation ElseLoc, StmtArg Else) {
+ return getSema().ActOnIfStmt(IfLoc, Cond, DeclPtrTy::make(CondVar),
+ move(Then), ElseLoc, move(Else));
}
/// \brief Start building a new switch statement.
///
/// By default, performs semantic analysis to build the new statement.
/// Subclasses may override this routine to provide different behavior.
- OwningStmtResult RebuildSwitchStmtStart(ExprArg Cond) {
- return getSema().ActOnStartOfSwitchStmt(move(Cond));
+ OwningStmtResult RebuildSwitchStmtStart(Sema::FullExprArg Cond,
+ VarDecl *CondVar) {
+ return getSema().ActOnStartOfSwitchStmt(Cond, DeclPtrTy::make(CondVar));
}
/// \brief Attach the body to the switch statement.
@@ -694,8 +697,10 @@
/// Subclasses may override this routine to provide different behavior.
OwningStmtResult RebuildWhileStmt(SourceLocation WhileLoc,
Sema::FullExprArg Cond,
+ VarDecl *CondVar,
StmtArg Body) {
- return getSema().ActOnWhileStmt(WhileLoc, Cond, move(Body));
+ return getSema().ActOnWhileStmt(WhileLoc, Cond, DeclPtrTy::make(CondVar),
+ move(Body));
}
/// \brief Build a new do-while statement.
@@ -717,10 +722,12 @@
/// Subclasses may override this routine to provide different behavior.
OwningStmtResult RebuildForStmt(SourceLocation ForLoc,
SourceLocation LParenLoc,
- StmtArg Init, ExprArg Cond, ExprArg Inc,
+ StmtArg Init, Sema::FullExprArg Cond,
+ VarDecl *CondVar, Sema::FullExprArg Inc,
SourceLocation RParenLoc, StmtArg Body) {
- return getSema().ActOnForStmt(ForLoc, LParenLoc, move(Init), move(Cond),
- move(Inc), RParenLoc, move(Body));
+ return getSema().ActOnForStmt(ForLoc, LParenLoc, move(Init), Cond,
+ DeclPtrTy::make(CondVar),
+ Inc, RParenLoc, move(Body));
}
/// \brief Build a new goto statement.
@@ -1374,18 +1381,6 @@
0, RParenLoc);
}
- /// \brief Build a new C++ conditional declaration expression.
- ///
- /// By default, performs semantic analysis to build the new expression.
- /// Subclasses may override this routine to provide different behavior.
- OwningExprResult RebuildCXXConditionDeclExpr(SourceLocation StartLoc,
- SourceLocation EqLoc,
- VarDecl *Var) {
- return SemaRef.Owned(new (SemaRef.Context) CXXConditionDeclExpr(StartLoc,
- EqLoc,
- Var));
- }
-
/// \brief Build a new C++ "new" expression.
///
/// By default, performs semantic analysis to build the new expression.
@@ -3071,13 +3066,12 @@
getDerived().TransformDefinition(S->getConditionVariable()));
if (!ConditionVar)
return SemaRef.StmtError();
-
- Cond = getSema().CheckConditionVariable(ConditionVar);
- } else
+ } else {
Cond = getDerived().TransformExpr(S->getCond());
- if (Cond.isInvalid())
- return SemaRef.StmtError();
+ if (Cond.isInvalid())
+ return SemaRef.StmtError();
+ }
Sema::FullExprArg FullCond(getSema().FullExpr(Cond));
@@ -3093,11 +3087,13 @@
if (!getDerived().AlwaysRebuild() &&
FullCond->get() == S->getCond() &&
+ ConditionVar == S->getConditionVariable() &&
Then.get() == S->getThen() &&
Else.get() == S->getElse())
return SemaRef.Owned(S->Retain());
- return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, move(Then),
+ return getDerived().RebuildIfStmt(S->getIfLoc(), FullCond, ConditionVar,
+ move(Then),
S->getElseLoc(), move(Else));
}
@@ -3113,15 +3109,18 @@
getDerived().TransformDefinition(S->getConditionVariable()));
if (!ConditionVar)
return SemaRef.StmtError();
-
- Cond = getSema().CheckConditionVariable(ConditionVar);
- } else
+ } else {
Cond = getDerived().TransformExpr(S->getCond());
- if (Cond.isInvalid())
- return SemaRef.StmtError();
+
+ if (Cond.isInvalid())
+ return SemaRef.StmtError();
+ }
+ Sema::FullExprArg FullCond(getSema().FullExpr(Cond));
+
// Rebuild the switch statement.
- OwningStmtResult Switch = getDerived().RebuildSwitchStmtStart(move(Cond));
+ OwningStmtResult Switch = getDerived().RebuildSwitchStmtStart(FullCond,
+ ConditionVar);
if (Switch.isInvalid())
return SemaRef.StmtError();
@@ -3147,12 +3146,12 @@
getDerived().TransformDefinition(S->getConditionVariable()));
if (!ConditionVar)
return SemaRef.StmtError();
-
- Cond = getSema().CheckConditionVariable(ConditionVar);
- } else
+ } else {
Cond = getDerived().TransformExpr(S->getCond());
- if (Cond.isInvalid())
- return SemaRef.StmtError();
+
+ if (Cond.isInvalid())
+ return SemaRef.StmtError();
+ }
Sema::FullExprArg FullCond(getSema().FullExpr(Cond));
@@ -3163,10 +3162,12 @@
if (!getDerived().AlwaysRebuild() &&
FullCond->get() == S->getCond() &&
+ ConditionVar == S->getConditionVariable() &&
Body.get() == S->getBody())
return SemaRef.Owned(S->Retain());
- return getDerived().RebuildWhileStmt(S->getWhileLoc(), FullCond, move(Body));
+ return getDerived().RebuildWhileStmt(S->getWhileLoc(), FullCond, ConditionVar,
+ move(Body));
}
template<typename Derived>
@@ -3201,9 +3202,20 @@
return SemaRef.StmtError();
// Transform the condition
- OwningExprResult Cond = getDerived().TransformExpr(S->getCond());
- if (Cond.isInvalid())
- return SemaRef.StmtError();
+ OwningExprResult Cond(SemaRef);
+ VarDecl *ConditionVar = 0;
+ if (S->getConditionVariable()) {
+ ConditionVar
+ = cast_or_null<VarDecl>(
+ getDerived().TransformDefinition(S->getConditionVariable()));
+ if (!ConditionVar)
+ return SemaRef.StmtError();
+ } else {
+ Cond = getDerived().TransformExpr(S->getCond());
+
+ if (Cond.isInvalid())
+ return SemaRef.StmtError();
+ }
// Transform the increment
OwningExprResult Inc = getDerived().TransformExpr(S->getInc());
@@ -3223,7 +3235,9 @@
return SemaRef.Owned(S->Retain());
return getDerived().RebuildForStmt(S->getForLoc(), S->getLParenLoc(),
- move(Init), move(Cond), move(Inc),
+ move(Init), getSema().FullExpr(Cond),
+ ConditionVar,
+ getSema().FullExpr(Inc),
S->getRParenLoc(), move(Body));
}
@@ -4418,24 +4432,6 @@
template<typename Derived>
Sema::OwningExprResult
-TreeTransform<Derived>::TransformCXXConditionDeclExpr(CXXConditionDeclExpr *E,
- bool isAddressOfOperand) {
- VarDecl *Var
- = cast_or_null<VarDecl>(getDerived().TransformDefinition(E->getVarDecl()));
- if (!Var)
- return SemaRef.ExprError();
-
- if (!getDerived().AlwaysRebuild() &&
- Var == E->getVarDecl())
- return SemaRef.Owned(E->Retain());
-
- return getDerived().RebuildCXXConditionDeclExpr(E->getStartLoc(),
- /*FIXME:*/E->getStartLoc(),
- Var);
-}
-
-template<typename Derived>
-Sema::OwningExprResult
TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E,
bool isAddressOfOperand) {
// Transform the type that we're allocating