[cfe-commits] r106997 - in /cfe/trunk: include/clang/AST/ExprCXX.h lib/Frontend/PCHReaderStmt.cpp lib/Frontend/PCHWriterStmt.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Jun 28 02:31:48 PDT 2010


Author: akirtzidis
Date: Mon Jun 28 04:31:48 2010
New Revision: 106997

URL: http://llvm.org/viewvc/llvm-project?rev=106997&view=rev
Log:
Refactor PCH reading/writing of template arguments passed to expressions.

Modified:
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/lib/Frontend/PCHReaderStmt.cpp
    cfe/trunk/lib/Frontend/PCHWriterStmt.cpp

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=106997&r1=106996&r2=106997&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Mon Jun 28 04:31:48 2010
@@ -1947,20 +1947,6 @@
   /// \brief The location of the member name.
   SourceLocation MemberLoc;
 
-  /// \brief Retrieve the explicit template argument list that followed the
-  /// member template name, if any.
-  ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
-    assert(HasExplicitTemplateArgs);
-    return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
-  }
-
-  /// \brief Retrieve the explicit template argument list that followed the
-  /// member template name, if any.
-  const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
-    return const_cast<CXXDependentScopeMemberExpr *>(this)
-             ->getExplicitTemplateArgumentList();
-  }
-
   CXXDependentScopeMemberExpr(ASTContext &C,
                           Expr *Base, QualType BaseType, bool IsArrow,
                           SourceLocation OperatorLoc,
@@ -2071,6 +2057,20 @@
     return HasExplicitTemplateArgs;
   }
 
+  /// \brief Retrieve the explicit template argument list that followed the
+  /// member template name, if any.
+  ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() {
+    assert(HasExplicitTemplateArgs);
+    return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1);
+  }
+
+  /// \brief Retrieve the explicit template argument list that followed the
+  /// member template name, if any.
+  const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const {
+    return const_cast<CXXDependentScopeMemberExpr *>(this)
+             ->getExplicitTemplateArgumentList();
+  }
+
   /// \brief Copies the template arguments (if present) into the given
   /// structure.
   void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const {

Modified: cfe/trunk/lib/Frontend/PCHReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderStmt.cpp?rev=106997&r1=106996&r2=106997&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderStmt.cpp Mon Jun 28 04:31:48 2010
@@ -19,14 +19,6 @@
 
 namespace {
 
-  class StmtStackExprReader : public PCHReader::ExprReader {
-    llvm::SmallVectorImpl<Stmt *>::iterator StmtI;
-  public:
-    StmtStackExprReader(const llvm::SmallVectorImpl<Stmt *>::iterator &stmtI)
-      : StmtI(stmtI) { }
-    virtual Expr *Read() { return cast_or_null<Expr>(*StmtI++); }
-  };
-
   class PCHStmtReader : public StmtVisitor<PCHStmtReader, unsigned> {
     PCHReader &Reader;
     const PCHReader::RecordData &Record;
@@ -45,6 +37,13 @@
     /// \brief The number of record fields required for the Expr class
     /// itself.
     static const unsigned NumExprFields = NumStmtFields + 3;
+    
+    /// \brief Read and initialize a ExplicitTemplateArgumentList structure.
+    /// \return the number of Exprs that were read.
+    unsigned
+    ReadExplicitTemplateArgumentList(ExplicitTemplateArgumentList &ArgList,
+                                     unsigned NumTemplateArgs,
+                            llvm::SmallVectorImpl<Stmt *>::iterator EndOfExprs);
 
     // Each of the Visit* functions reads in part of the expression
     // from the given record and the current expression stack, then
@@ -156,6 +155,34 @@
   };
 }
 
+unsigned PCHStmtReader::
+ReadExplicitTemplateArgumentList(ExplicitTemplateArgumentList &ArgList,
+                                 unsigned NumTemplateArgs,
+                           llvm::SmallVectorImpl<Stmt *>::iterator EndOfExprs) {
+
+  class StmtStackExprReader : public PCHReader::ExprReader {
+    llvm::SmallVectorImpl<Stmt *>::iterator StmtI;
+  public:
+    StmtStackExprReader(const llvm::SmallVectorImpl<Stmt *>::iterator &stmtI)
+      : StmtI(stmtI) { }
+    virtual Expr *Read() { return cast_or_null<Expr>(*StmtI++); }
+  };
+
+  unsigned NumExprs = Record[Idx++];
+  
+  TemplateArgumentListInfo ArgInfo;
+  ArgInfo.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
+  ArgInfo.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
+  
+  StmtStackExprReader ExprReader(EndOfExprs - NumExprs);
+  for (unsigned i = 0; i != NumTemplateArgs; ++i)
+    ArgInfo.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx,
+                                                       ExprReader));
+  ArgList.initializeFrom(ArgInfo);
+
+  return NumExprs;
+}
+
 unsigned PCHStmtReader::VisitStmt(Stmt *S) {
   assert(Idx == NumStmtFields && "Incorrect statement field count");
   return 0;
@@ -1138,23 +1165,10 @@
   unsigned NumTemplateArgs = Record[Idx++];
   assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() &&
          "Read wrong record during creation ?");
-  if (E->hasExplicitTemplateArgs()) {
-    TemplateArgumentListInfo ArgInfo;
-    ArgInfo.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
-    ArgInfo.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
-
-    NumExprs = Record[Idx++];
-    llvm::SmallVectorImpl<Stmt *>::iterator
-      StartOfExprs = StmtStack.end() - NumExprs;
-    if (isa<UnresolvedMemberExpr>(E))
-      --StartOfExprs; // UnresolvedMemberExpr contains an Expr;
-    StmtStackExprReader ExprReader(StartOfExprs);
-    for (unsigned i = 0; i != NumTemplateArgs; ++i)
-      ArgInfo.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx,
-                                                         ExprReader));
-
-    E->initializeTemplateArgumentsFrom(ArgInfo);
-  }
+  if (E->hasExplicitTemplateArgs())
+    NumExprs
+      = ReadExplicitTemplateArgumentList(*E->getExplicitTemplateArgumentList(),
+                                         NumTemplateArgs, StmtStack.end());
 
   E->setBase(cast_or_null<Expr>(StmtStack.back()));
   E->setBaseType(Reader.GetType(Record[Idx++]));
@@ -1192,21 +1206,11 @@
   assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() &&
          "Read wrong record during creation ?");
   if (E->hasExplicitTemplateArgs()) {
-    TemplateArgumentListInfo ArgInfo;
-    ArgInfo.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
-    ArgInfo.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
-
-    NumExprs = Record[Idx++];
-    llvm::SmallVectorImpl<Stmt *>::iterator
-      StartOfExprs = StmtStack.end() - NumExprs;
+    llvm::SmallVectorImpl<Stmt *>::iterator EndOfExprs = StmtStack.end();
     if (isa<UnresolvedMemberExpr>(E))
-      --StartOfExprs; // UnresolvedMemberExpr contains an Expr;
-    StmtStackExprReader ExprReader(StartOfExprs);
-    for (unsigned i = 0; i != NumTemplateArgs; ++i)
-      ArgInfo.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx,
-                                                         ExprReader));
-
-    E->getExplicitTemplateArgs().initializeFrom(ArgInfo);
+      --EndOfExprs; // UnresolvedMemberExpr contains an Expr.
+    NumExprs = ReadExplicitTemplateArgumentList(E->getExplicitTemplateArgs(),
+                                                NumTemplateArgs, EndOfExprs);
   }
 
   unsigned NumDecls = Record[Idx++];

Modified: cfe/trunk/lib/Frontend/PCHWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterStmt.cpp?rev=106997&r1=106996&r2=106997&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterStmt.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterStmt.cpp Mon Jun 28 04:31:48 2010
@@ -32,6 +32,9 @@
 
     PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
       : Writer(Writer), Record(Record) { }
+    
+    void
+    AddExplicitTemplateArgumentList(const ExplicitTemplateArgumentList &Args);
 
     void VisitStmt(Stmt *S);
     void VisitNullStmt(NullStmt *S);
@@ -142,6 +145,49 @@
   };
 }
 
+/// \brief Return the number of Exprs contained in the given TemplateArgument.
+static unsigned NumExprsContainedIn(const TemplateArgument &Arg) {
+  switch (Arg.getKind()) {
+  default: break;
+  case TemplateArgument::Expression:
+    return 1;
+  case TemplateArgument::Pack: {
+    unsigned Count = 0;
+    for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
+           I != E; ++I)
+      Count += NumExprsContainedIn(*I);
+    return Count;
+  }
+  }
+  
+  return 0;
+}
+
+/// \brief Return the number of Exprs contained in the given
+/// ExplicitTemplateArgumentList.
+static unsigned NumExprsContainedIn(const ExplicitTemplateArgumentList &Args) {
+  unsigned Count = 0;
+  for (unsigned i=0; i != Args.NumTemplateArgs; ++i) {
+    const TemplateArgumentLoc &ArgLoc = Args.getTemplateArgs()[i];
+    const TemplateArgument &TemplA = ArgLoc.getArgument();
+    Count += NumExprsContainedIn(TemplA);
+    if (TemplA.getKind() == TemplateArgument::Expression &&
+        TemplA.getAsExpr() != ArgLoc.getLocInfo().getAsExpr())
+      ++Count; // 1 in TemplateArgumentLocInfo.
+  }
+  
+  return Count;
+}
+
+void PCHStmtWriter::
+AddExplicitTemplateArgumentList(const ExplicitTemplateArgumentList &Args) {
+  Record.push_back(NumExprsContainedIn(Args));
+  Writer.AddSourceLocation(Args.LAngleLoc, Record);
+  Writer.AddSourceLocation(Args.RAngleLoc, Record);
+  for (unsigned i=0; i != Args.NumTemplateArgs; ++i)
+    Writer.AddTemplateArgumentLoc(Args.getTemplateArgs()[i], Record);
+}
+
 void PCHStmtWriter::VisitStmt(Stmt *S) {
 }
 
@@ -1025,54 +1071,19 @@
   Code = pch::EXPR_CXX_EXPR_WITH_TEMPORARIES;
 }
 
-/// \brief Return the number of Exprs contained in the given TemplateArgument.
-static unsigned NumExprsContainedIn(const TemplateArgument Arg) {
-  switch (Arg.getKind()) {
-  default: break;
-  case TemplateArgument::Expression:
-    return 1;
-  case TemplateArgument::Pack: {
-    unsigned Count = 0;
-    for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
-           I != E; ++I)
-      Count += NumExprsContainedIn(*I);
-    return Count;
-  }
-  }
-  
-  return 0;
-}
-
-/// \brief Return the number of Exprs contained in the given
-/// TemplateArgumentLoc.
-static
-unsigned NumExprsContainedIn(const TemplateArgumentLoc *Args,unsigned NumArgs) {
-  unsigned Count = 0;
-  for (unsigned i=0; i != NumArgs; ++i) {
-    const TemplateArgument &TemplA = Args[i].getArgument();
-    Count += NumExprsContainedIn(TemplA);
-    if (TemplA.getKind() == TemplateArgument::Expression &&
-        TemplA.getAsExpr() != Args[i].getLocInfo().getAsExpr())
-      ++Count; // 1 in TemplateArgumentLocInfo.
-  }
-  
-  return Count;
-}
-
 void
 PCHStmtWriter::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){
   VisitExpr(E);
+  
+  // Don't emit anything here, NumTemplateArgs must be emitted first.
 
   if (E->hasExplicitTemplateArgs()) {
-    assert(E->getNumTemplateArgs() &&
+    const ExplicitTemplateArgumentList &Args
+      = *E->getExplicitTemplateArgumentList();
+    assert(Args.NumTemplateArgs &&
            "Num of template args was zero! PCH reading will mess up!");
-    Record.push_back(E->getNumTemplateArgs());
-    Writer.AddSourceLocation(E->getLAngleLoc(), Record);
-    Writer.AddSourceLocation(E->getRAngleLoc(), Record);
-    Record.push_back(NumExprsContainedIn(E->getTemplateArgs(),
-                                         E->getNumTemplateArgs()));
-    for (int i=0, e = E->getNumTemplateArgs(); i != e; ++i)
-      Writer.AddTemplateArgumentLoc(E->getTemplateArgs()[i], Record);
+    Record.push_back(Args.NumTemplateArgs);
+    AddExplicitTemplateArgumentList(Args);
   } else {
     Record.push_back(0);
   }
@@ -1108,18 +1119,15 @@
 
 void PCHStmtWriter::VisitOverloadExpr(OverloadExpr *E) {
   VisitExpr(E);
+  
+  // Don't emit anything here, NumTemplateArgs must be emitted first.
 
   if (E->hasExplicitTemplateArgs()) {
     const ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs();
     assert(Args.NumTemplateArgs &&
            "Num of template args was zero! PCH reading will mess up!");
     Record.push_back(Args.NumTemplateArgs);
-    Writer.AddSourceLocation(Args.LAngleLoc, Record);
-    Writer.AddSourceLocation(Args.RAngleLoc, Record);
-    Record.push_back(NumExprsContainedIn(Args.getTemplateArgs(),
-                                         Args.NumTemplateArgs));
-    for (unsigned i=0; i != Args.NumTemplateArgs; ++i)
-      Writer.AddTemplateArgumentLoc(Args.getTemplateArgs()[i], Record);
+    AddExplicitTemplateArgumentList(Args);
   } else {
     Record.push_back(0);
   }





More information about the cfe-commits mailing list