[cfe-commits] r71901 - in /cfe/trunk: 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

Douglas Gregor dgregor at apple.com
Fri May 15 15:12:32 PDT 2009


Author: dgregor
Date: Fri May 15 17:12:32 2009
New Revision: 71901

URL: http://llvm.org/viewvc/llvm-project?rev=71901&view=rev
Log:
Template instantiation for "for" loops

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

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=71901&r1=71900&r2=71901&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Fri May 15 17:12:32 2009
@@ -735,14 +735,19 @@
   enum { INIT, COND, INC, BODY, END_EXPR };
   Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
   SourceLocation ForLoc;
+  SourceLocation LParenLoc, RParenLoc;
+
 public:
-  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL) 
+  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL,
+          SourceLocation LP, SourceLocation RP) 
     : Stmt(ForStmtClass) {
     SubExprs[INIT] = Init;
     SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
     SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
     SubExprs[BODY] = Body;
     ForLoc = FL;
+    LParenLoc = LP;
+    RParenLoc = RP;
   }
   
   /// \brief Build an empty for statement.
@@ -765,6 +770,10 @@
 
   SourceLocation getForLoc() const { return ForLoc; }
   void setForLoc(SourceLocation L) { ForLoc = L; }
+  SourceLocation getLParenLoc() const { return LParenLoc; }
+  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
+  SourceLocation getRParenLoc() const { return RParenLoc; }
+  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
 
   virtual SourceRange getSourceRange() const { 
     return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 

Modified: cfe/trunk/lib/Frontend/PCHReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderStmt.cpp?rev=71901&r1=71900&r2=71901&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderStmt.cpp Fri May 15 17:12:32 2009
@@ -217,6 +217,8 @@
   S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
   S->setBody(StmtStack.back());
   S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  S->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
   return 4;
 }
 

Modified: cfe/trunk/lib/Frontend/PCHWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterStmt.cpp?rev=71901&r1=71900&r2=71901&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterStmt.cpp Fri May 15 17:12:32 2009
@@ -204,6 +204,8 @@
   Writer.WriteSubStmt(S->getInc());
   Writer.WriteSubStmt(S->getBody());
   Writer.AddSourceLocation(S->getForLoc(), Record);
+  Writer.AddSourceLocation(S->getLParenLoc(), Record);
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
   Code = pch::STMT_FOR;
 }
 

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=71901&r1=71900&r2=71901&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri May 15 17:12:32 2009
@@ -589,7 +589,7 @@
       }
     }
   }
-  if (Second) {
+  if (Second && !Second->isTypeDependent()) {
     DefaultFunctionArrayConversion(Second);
     QualType SecondType = Second->getType();
 
@@ -605,7 +605,8 @@
   second.release();
   third.release();
   body.release();
-  return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc));
+  return Owned(new (Context) ForStmt(First, Second, Third, Body, ForLoc,
+                                     LParenLoc, RParenLoc));
 }
 
 Action::OwningStmtResult

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateStmt.cpp?rev=71901&r1=71900&r2=71901&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateStmt.cpp Fri May 15 17:12:32 2009
@@ -42,6 +42,7 @@
     OwningStmtResult VisitIfStmt(IfStmt *S);
     OwningStmtResult VisitWhileStmt(WhileStmt *S);
     OwningStmtResult VisitDoStmt(DoStmt *S);
+    OwningStmtResult VisitForStmt(ForStmt *S);
     OwningStmtResult VisitExpr(Expr *E);
     OwningStmtResult VisitLabelStmt(LabelStmt *S);
     OwningStmtResult VisitGotoStmt(GotoStmt *S);
@@ -187,6 +188,32 @@
                              move(Cond));
 }
 
+Sema::OwningStmtResult TemplateStmtInstantiator::VisitForStmt(ForStmt *S) {
+  // Instantiate the initialization statement
+  OwningStmtResult Init = SemaRef.InstantiateStmt(S->getInit(), TemplateArgs);
+  if (Init.isInvalid())
+    return SemaRef.StmtError();
+
+  // Instantiate the condition
+  OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
+  if (Cond.isInvalid())
+    return SemaRef.StmtError();
+
+  // Instantiate the increment
+  OwningExprResult Inc = SemaRef.InstantiateExpr(S->getInc(), TemplateArgs);
+  if (Inc.isInvalid())
+    return SemaRef.StmtError();
+
+  // Instantiate the body
+  OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
+  if (Body.isInvalid())
+    return SemaRef.StmtError();
+
+  return SemaRef.ActOnForStmt(S->getForLoc(), S->getLParenLoc(),
+                              move(Init), move(Cond), move(Inc),
+                              S->getRParenLoc(), move(Body));
+}
+
 Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) {
   Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs);
   if (Result.isInvalid())

Modified: cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp?rev=71901&r1=71900&r2=71901&view=diff

==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-function-1.cpp Fri May 15 17:12:32 2009
@@ -112,3 +112,12 @@
 struct NotConvertibleToBool { };
 template struct Do0<ConvertibleToInt>;
 template struct Do0<NotConvertibleToBool>; // expected-note{{instantiation}}
+
+template<typename T> struct For0 {
+  void f(T f, T l) {
+    for (; f != l; ++f) {
+    }
+  }
+};
+
+template struct For0<int*>;





More information about the cfe-commits mailing list