]> granicus.if.org Git - clang/commitdiff
Template instantiation for WhileStmt and CXXConditionDeclExpr.
authorDouglas Gregor <dgregor@apple.com>
Fri, 15 May 2009 21:45:53 +0000 (21:45 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 15 May 2009 21:45:53 +0000 (21:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71896 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ExprCXX.h
lib/Sema/SemaStmt.cpp
lib/Sema/SemaTemplateInstantiateExpr.cpp
lib/Sema/SemaTemplateInstantiateStmt.cpp
test/SemaTemplate/instantiate-function-1.cpp

index c34ded26a5752a33b46698dbfdc2aa6cec6c8a55..b6a970e231227a27b1059c468e3b409bf4689034 100644 (file)
@@ -564,7 +564,10 @@ public:
   CXXConditionDeclExpr(SourceLocation startLoc,
                        SourceLocation eqLoc, VarDecl *var)
     : DeclRefExpr(CXXConditionDeclExprClass, var, 
-                  var->getType().getNonReferenceType(), startLoc) {}
+                  var->getType().getNonReferenceType(), startLoc,
+                  var->getType()->isDependentType(),
+                  /*FIXME:integral constant?*/
+                    var->getType()->isDependentType()) {}
 
   virtual void Destroy(ASTContext& Ctx);
 
index a5277a92939cd2d3213808eecbd95c92fdf41a82..652e2a6e0f94523e235c85ad7071635fc1c3f5c5 100644 (file)
@@ -520,17 +520,19 @@ Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprArg Cond, StmtArg Body) {
   Expr *condExpr = Cond.takeAs<Expr>();
   assert(condExpr && "ActOnWhileStmt(): missing expression");
 
-  DefaultFunctionArrayConversion(condExpr);
-  Cond = condExpr;
-  QualType condType = condExpr->getType();
-
-  if (getLangOptions().CPlusPlus) {
-    if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
-      return StmtError();
-  } else if (!condType->isScalarType()) // C99 6.8.5p2
-    return StmtError(Diag(WhileLoc,
-      diag::err_typecheck_statement_requires_scalar)
-      << condType << condExpr->getSourceRange());
+  if (!condExpr->isTypeDependent()) {
+    DefaultFunctionArrayConversion(condExpr);
+    Cond = condExpr;
+    QualType condType = condExpr->getType();
+    
+    if (getLangOptions().CPlusPlus) {
+      if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
+        return StmtError();
+    } else if (!condType->isScalarType()) // C99 6.8.5p2
+      return StmtError(Diag(WhileLoc,
+                            diag::err_typecheck_statement_requires_scalar)
+                       << condType << condExpr->getSourceRange());
+  }
 
   Cond.release();
   return Owned(new (Context) WhileStmt(condExpr, Body.takeAs<Stmt>(), 
index 08c388efea1736912178fbca38d29b6fb0401724..53e42fc5294c72a5d25f86635d681a06f5b6a147 100644 (file)
@@ -42,6 +42,7 @@ namespace {
     OwningExprResult VisitUnaryOperator(UnaryOperator *E);
     OwningExprResult VisitBinaryOperator(BinaryOperator *E);
     OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
+    OwningExprResult VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E);
     OwningExprResult VisitConditionalOperator(ConditionalOperator *E);
     OwningExprResult VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
     OwningExprResult VisitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E);
@@ -262,6 +263,21 @@ TemplateExprInstantiator::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
   return move(Result);  
 }
 
+Sema::OwningExprResult 
+TemplateExprInstantiator::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
+  VarDecl *Var 
+    = cast_or_null<VarDecl>(SemaRef.InstantiateDecl(E->getVarDecl(),
+                                                    SemaRef.CurContext,
+                                                    TemplateArgs));
+  if (!Var)
+    return SemaRef.ExprError();
+
+  return SemaRef.Owned(new (SemaRef.Context) CXXConditionDeclExpr(
+                                                    E->getStartLoc(), 
+                                                    SourceLocation(),
+                                                    Var));
+}
+
 Sema::OwningExprResult
 TemplateExprInstantiator::VisitConditionalOperator(ConditionalOperator *E) {
   Sema::OwningExprResult Cond = Visit(E->getCond());
index 2a3f48a469d1eac163eb7354cd72ded471d3b44a..9a4d75e117ae77651a344a98d1a6a45ef2499c11 100644 (file)
@@ -40,6 +40,7 @@ namespace {
     OwningStmtResult VisitNullStmt(NullStmt *S);
     OwningStmtResult VisitCompoundStmt(CompoundStmt *S);
     OwningStmtResult VisitIfStmt(IfStmt *S);
+    OwningStmtResult VisitWhileStmt(WhileStmt *S);
     OwningStmtResult VisitExpr(Expr *E);
     OwningStmtResult VisitLabelStmt(LabelStmt *S);
     OwningStmtResult VisitGotoStmt(GotoStmt *S);
@@ -156,6 +157,20 @@ Sema::OwningStmtResult TemplateStmtInstantiator::VisitIfStmt(IfStmt *S) {
                              S->getElseLoc(), move(Else));
 }
 
+Sema::OwningStmtResult TemplateStmtInstantiator::VisitWhileStmt(WhileStmt *S) {
+  // Instantiate the condition
+  OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
+  if (Cond.isInvalid())
+    return SemaRef.StmtError();
+
+  // Instantiate the body
+  OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
+  if (Body.isInvalid())
+    return SemaRef.StmtError();
+
+  return SemaRef.ActOnWhileStmt(S->getWhileLoc(), move(Cond), move(Body));
+}
+
 Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) {
   Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs);
   if (Result.isInvalid())
index e7c4af163a2bba44c4b65596d3422a5907b439f4..3b88700c540bded0f549938e6f935c4a457324ea 100644 (file)
@@ -87,3 +87,14 @@ template <typename T> struct X7 {
 };
 
 template struct X7<int>;
+
+template<typename T> struct While0 {
+  void f(T t) {
+    while (t) {
+    }
+
+    while (T t2 = T()) ;
+  }
+};
+
+template struct While0<float>;