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

include/clang/AST/Stmt.h
lib/Frontend/PCHReaderStmt.cpp
lib/Frontend/PCHWriterStmt.cpp
lib/Sema/SemaStmt.cpp
lib/Sema/SemaTemplateInstantiateStmt.cpp
test/SemaTemplate/instantiate-function-1.cpp

index 14afcfcd6234f1d647fde3c9eeb09e55082104e1..8f1771016a6fa36ae3f438af3730c91cbd65a0f0 100644 (file)
@@ -687,12 +687,15 @@ class DoStmt : public Stmt {
   enum { COND, BODY, END_EXPR };
   Stmt* SubExprs[END_EXPR];
   SourceLocation DoLoc;
+  SourceLocation WhileLoc;
+
 public:
-  DoStmt(Stmt *body, Expr *cond, SourceLocation DL) 
-    : Stmt(DoStmtClass), DoLoc(DL) {
+  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL
+    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL) {
     SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
     SubExprs[BODY] = body;
     DoLoc = DL;
+    WhileLoc = WL;
   }  
 
   /// \brief Build an empty do-while statement.
@@ -707,6 +710,8 @@ public:
 
   SourceLocation getDoLoc() const { return DoLoc; }
   void setDoLoc(SourceLocation L) { DoLoc = L; }
+  SourceLocation getWhileLoc() const { return WhileLoc; }
+  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
 
   virtual SourceRange getSourceRange() const { 
     return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 
index eb8dab968b76ac4e092747d69e95b7eedb23ba0f..e526f5bd944ec29b25a390024c93cccdc945f707 100644 (file)
@@ -206,6 +206,7 @@ unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
   S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
   S->setBody(StmtStack.back());
   S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
   return 2;
 }
 
index af5d5635877b1e1129fb9f4d2bf5357c0d9924e8..827676ad30bf49ed7337e451cf71d9836df68d4a 100644 (file)
@@ -193,6 +193,7 @@ void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
   Writer.WriteSubStmt(S->getCond());
   Writer.WriteSubStmt(S->getBody());
   Writer.AddSourceLocation(S->getDoLoc(), Record);
+  Writer.AddSourceLocation(S->getWhileLoc(), Record);
   Code = pch::STMT_DO;
 }
 
index 652e2a6e0f94523e235c85ad7071635fc1c3f5c5..8226dd23f9539c9cbe636c997d760c88c889655a 100644 (file)
@@ -545,19 +545,23 @@ Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body,
   Expr *condExpr = Cond.takeAs<Expr>();
   assert(condExpr && "ActOnDoStmt(): 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(DoLoc, 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(DoLoc, 
+                            diag::err_typecheck_statement_requires_scalar)
+                       << condType << condExpr->getSourceRange());
+  }
 
   Cond.release();
-  return Owned(new (Context) DoStmt(Body.takeAs<Stmt>(), condExpr, DoLoc));
+  return Owned(new (Context) DoStmt(Body.takeAs<Stmt>(), condExpr, DoLoc,
+                                    WhileLoc));
 }
 
 Action::OwningStmtResult
index 9a4d75e117ae77651a344a98d1a6a45ef2499c11..c18966a878bd272044fca5cc6843ee96d38ba8f3 100644 (file)
@@ -41,6 +41,7 @@ namespace {
     OwningStmtResult VisitCompoundStmt(CompoundStmt *S);
     OwningStmtResult VisitIfStmt(IfStmt *S);
     OwningStmtResult VisitWhileStmt(WhileStmt *S);
+    OwningStmtResult VisitDoStmt(DoStmt *S);
     OwningStmtResult VisitExpr(Expr *E);
     OwningStmtResult VisitLabelStmt(LabelStmt *S);
     OwningStmtResult VisitGotoStmt(GotoStmt *S);
@@ -171,6 +172,21 @@ Sema::OwningStmtResult TemplateStmtInstantiator::VisitWhileStmt(WhileStmt *S) {
   return SemaRef.ActOnWhileStmt(S->getWhileLoc(), move(Cond), move(Body));
 }
 
+Sema::OwningStmtResult TemplateStmtInstantiator::VisitDoStmt(DoStmt *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.ActOnDoStmt(S->getDoLoc(), move(Body), S->getWhileLoc(),
+                             move(Cond));
+}
+
 Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) {
   Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs);
   if (Result.isInvalid())
index 3b88700c540bded0f549938e6f935c4a457324ea..6da25e5984c1167c5883a64413429e368df62a00 100644 (file)
@@ -98,3 +98,17 @@ template<typename T> struct While0 {
 };
 
 template struct While0<float>;
+
+template<typename T> struct Do0 {
+  void f(T t) {
+    do {
+    } while (t); // expected-error{{not contextually}}
+    
+    do {
+    } while (T t2 = T());
+  }
+};
+
+struct NotConvertibleToBool { };
+template struct Do0<ConvertibleToInt>;
+template struct Do0<NotConvertibleToBool>; // expected-note{{instantiation}}