Template instantiation for "if" statements. Also:

  - Skip semantic analysis of the "if" condition if it is type-dependent.
  - Added the location of the "else" keyword into IfStmt, so that we can
    provide it for type-checking after template instantiation.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71875 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index e200c46..a5277a9 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -184,17 +184,20 @@
 
   assert(condExpr && "ActOnIfStmt(): missing expression");
 
-  DefaultFunctionArrayConversion(condExpr);
-  // Take ownership again until we're past the error checking.
-  CondVal = condExpr;
-  QualType condType = condExpr->getType();
-
-  if (getLangOptions().CPlusPlus) {
-    if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
-      return StmtError();
-  } else if (!condType->isScalarType()) // C99 6.8.4.1p1
-    return StmtError(Diag(IfLoc, diag::err_typecheck_statement_requires_scalar)
-      << condType << condExpr->getSourceRange());
+  if (!condExpr->isTypeDependent()) {
+    DefaultFunctionArrayConversion(condExpr);
+    // Take ownership again until we're past the error checking.
+    CondVal = condExpr;
+    QualType condType = condExpr->getType();
+    
+    if (getLangOptions().CPlusPlus) {
+      if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
+        return StmtError();
+    } else if (!condType->isScalarType()) // C99 6.8.4.1p1
+      return StmtError(Diag(IfLoc, 
+                            diag::err_typecheck_statement_requires_scalar)
+                       << condType << condExpr->getSourceRange());
+  }
 
   Stmt *thenStmt = ThenVal.takeAs<Stmt>();
 
@@ -209,7 +212,7 @@
 
   CondVal.release();
   return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
-                                    ElseVal.takeAs<Stmt>()));
+                                    ElseLoc, ElseVal.takeAs<Stmt>()));
 }
 
 Action::OwningStmtResult
diff --git a/lib/Sema/SemaTemplateInstantiateStmt.cpp b/lib/Sema/SemaTemplateInstantiateStmt.cpp
index cbe4449..957402a 100644
--- a/lib/Sema/SemaTemplateInstantiateStmt.cpp
+++ b/lib/Sema/SemaTemplateInstantiateStmt.cpp
@@ -39,6 +39,7 @@
     OwningStmtResult VisitDeclStmt(DeclStmt *S);
     OwningStmtResult VisitNullStmt(NullStmt *S);
     OwningStmtResult VisitCompoundStmt(CompoundStmt *S);
+    OwningStmtResult VisitIfStmt(IfStmt *S);
     OwningStmtResult VisitExpr(Expr *E);
     OwningStmtResult VisitLabelStmt(LabelStmt *S);
     OwningStmtResult VisitGotoStmt(GotoStmt *S);
@@ -135,6 +136,26 @@
                                               S->getRBracLoc()));
 }
 
+Sema::OwningStmtResult TemplateStmtInstantiator::VisitIfStmt(IfStmt *S) {
+  // Instantiate the condition
+  OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
+  if (Cond.isInvalid())
+    return SemaRef.StmtError();
+
+  // Instantiate the "then" branch.
+  OwningStmtResult Then = SemaRef.InstantiateStmt(S->getThen(), TemplateArgs);
+  if (Then.isInvalid())
+    return SemaRef.StmtError();
+
+  // Instantiate the "else" branch.
+  OwningStmtResult Else = SemaRef.InstantiateStmt(S->getElse(), TemplateArgs);
+  if (Else.isInvalid())
+    return SemaRef.StmtError();
+
+  return SemaRef.ActOnIfStmt(S->getIfLoc(), move(Cond), move(Then),
+                             S->getElseLoc(), move(Else));
+}
+
 Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) {
   Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs);
   if (Result.isInvalid())