Overhauled serialization of statements.  We no longer use specialized SerializeTrait<> classes,
but methods in Stmt (like other objects we are serializing).  Full serialization of all statements
is not yet complete.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43793 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/StmtSerialization.cpp b/AST/StmtSerialization.cpp
index 5972c8c..95897fd 100644
--- a/AST/StmtSerialization.cpp
+++ b/AST/StmtSerialization.cpp
@@ -12,86 +12,128 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/AST/Stmt.h"
-#include "clang/AST/StmtVisitor.h"
+#include "clang/AST/Expr.h"
 #include "llvm/Bitcode/Serialize.h"
 #include "llvm/Bitcode/Deserialize.h"
 
-using llvm::Serializer;
-using llvm::Deserializer;
-using llvm::SerializeTrait;
-
 using namespace clang;
 
+void Stmt::Emit(llvm::Serializer& S) const {
+  S.EmitInt(getStmtClass());
+  directEmit(S);
+}  
 
-namespace {
-class StmtSerializer : public StmtVisitor<StmtSerializer> {
-  Serializer& S;
-public:  
-  StmtSerializer(Serializer& S) : S(S) {}
-
-  void VisitDeclStmt(DeclStmt* Stmt);
-  void VisitNullStmt(NullStmt* Stmt);
-  void VisitCompoundStmt(CompoundStmt* Stmt);
+Stmt* Stmt::Materialize(llvm::Deserializer& D) {
+  StmtClass SC = static_cast<StmtClass>(D.ReadInt());
   
-  void VisitStmt(Stmt*) { assert("Not Implemented yet"); }  
-};
-} // end anonymous namespace
-
-
-void SerializeTrait<Stmt>::Emit(Serializer& S, const Stmt& stmt) {
-  S.EmitInt(stmt.getStmtClass());
-  
-  StmtSerializer SS(S);
-  SS.Visit(const_cast<Stmt*>(&stmt));
+  switch (SC) {
+    default:  
+      assert (false && "Not implemented.");
+      return NULL;
+      
+    case CompoundStmtClass:
+      return CompoundStmt::directMaterialize(D);
+      
+    case ReturnStmtClass:
+      return ReturnStmt::directMaterialize(D);
+      
+    case BinaryOperatorClass:
+      return BinaryOperator::directMaterialize(D);
+      
+    case DeclRefExprClass:
+      return DeclRefExpr::directMaterialize(D);
+      
+    case IntegerLiteralClass:
+      return IntegerLiteral::directMaterialize(D);
+  }
 }
 
-void StmtSerializer::VisitDeclStmt(DeclStmt* Stmt) {
-  // FIXME
- // S.EmitOwnedPtr(Stmt->getDecl());  
-}
 
-void StmtSerializer::VisitNullStmt(NullStmt* Stmt) {
-  S.Emit(Stmt->getSemiLoc());
-}
 
-void StmtSerializer::VisitCompoundStmt(CompoundStmt* Stmt) {
-  S.Emit(Stmt->getLBracLoc());
-  S.Emit(Stmt->getRBracLoc());
-
-  CompoundStmt::body_iterator I=Stmt->body_begin(), E=Stmt->body_end();
-
-  S.EmitInt(E-I);  
+void CompoundStmt::directEmit(llvm::Serializer& S) const {
+  S.Emit(LBracLoc);
+  S.Emit(RBracLoc);
+  S.Emit(Body.size());
   
-  for ( ; I != E; ++I )
+  for (const_body_iterator I=body_begin(), E=body_end(); I!=E; ++I)
     S.EmitOwnedPtr(*I);
 }
 
-Stmt* SerializeTrait<Stmt>::Materialize(Deserializer& D) {
-  unsigned sClass = D.ReadInt();
+CompoundStmt* CompoundStmt::directMaterialize(llvm::Deserializer& D) {
+  SourceLocation LB = SourceLocation::ReadVal(D);
+  SourceLocation RB = SourceLocation::ReadVal(D);
+  unsigned size = D.ReadInt();
   
-  switch (sClass) {
-    default:
-      assert(false && "No matching statement class.");
-      return NULL;
-    
-    case Stmt::DeclStmtClass:
-      return NULL; // FIXME
-//      return new DeclStmt(D.ReadOwnedPtr<ScopedDecl>());
-
-    case Stmt::NullStmtClass:
-      return new NullStmt(SourceLocation::ReadVal(D));
-    
-    case Stmt::CompoundStmtClass: {
-      SourceLocation LBracLoc = SourceLocation::ReadVal(D);
-      SourceLocation RBracLoc = SourceLocation::ReadVal(D);
-      unsigned NumStmts = D.ReadInt();
-      llvm::SmallVector<Stmt*, 16> Body;
-      
-      for (unsigned i = 0 ; i <  NumStmts; ++i)
-        Body.push_back(D.ReadOwnedPtr<Stmt>());  
-      
-      return new CompoundStmt(&Body[0],NumStmts,LBracLoc,RBracLoc);
-    }
-  }
+  CompoundStmt* stmt = new CompoundStmt(NULL,0,LB,RB);
+  
+  stmt->Body.reserve(size);
+  
+  for (unsigned i = 0; i < size; ++i)
+    stmt->Body.push_back(D.ReadOwnedPtr<Stmt>());
+  
+  return stmt;
 }
+
+
+void ReturnStmt::directEmit(llvm::Serializer& S) const {
+  S.Emit(RetLoc);
+  S.EmitOwnedPtr(RetExpr);
+}
+
+ReturnStmt* ReturnStmt::directMaterialize(llvm::Deserializer& D) {
+  SourceLocation RetLoc = SourceLocation::ReadVal(D);
+  Expr* RetExpr = D.ReadOwnedPtr<Expr>();  
+  return new ReturnStmt(RetLoc,RetExpr);
+}
+
+
+void BinaryOperator::directEmit(llvm::Serializer& S) const {
+  S.EmitInt(Opc);
+  S.Emit(OpLoc);;
+  S.Emit(getType());
+  S.EmitOwnedPtr(getLHS());
+  S.EmitOwnedPtr(getRHS());
+}
+
+BinaryOperator* BinaryOperator::directMaterialize(llvm::Deserializer& D) {
+  Opcode Opc = static_cast<Opcode>(D.ReadInt());
+  SourceLocation OpLoc = SourceLocation::ReadVal(D);
+  QualType Result = QualType::ReadVal(D);
+  Expr* LHS = D.ReadOwnedPtr<Expr>();
+  Expr* RHS = D.ReadOwnedPtr<Expr>();
+  return new BinaryOperator(LHS,RHS,Opc,Result,OpLoc);
+}
+
+void DeclRefExpr::directEmit(llvm::Serializer& S) const {
+  S.Emit(Loc);
+  S.Emit(getType());
+  S.EmitPtr(getDecl());
+}
+
+DeclRefExpr* DeclRefExpr::directMaterialize(llvm::Deserializer& D) {
+  SourceLocation Loc = SourceLocation::ReadVal(D);
+  QualType T = QualType::ReadVal(D);
+  DeclRefExpr* dr = new DeclRefExpr(NULL,T,Loc);
+  D.ReadPtr(dr->D,false);  
+  return dr;
+}
+
+void IntegerLiteral::directEmit(llvm::Serializer& S) const {
+  S.Emit(Loc);
+  S.Emit(getType());
+  S.Emit(getValue());
+}
+
+IntegerLiteral* IntegerLiteral::directMaterialize(llvm::Deserializer& D) {
+  SourceLocation Loc = SourceLocation::ReadVal(D);
+  QualType T = QualType::ReadVal(D);
+  
+  // Create a dummy APInt because it is more efficient to deserialize
+  // it in place with the deserialized IntegerLiteral. (fewer copies)
+  llvm::APInt temp;  
+  IntegerLiteral* expr = new IntegerLiteral(temp,T,Loc);
+  D.Read(expr->Value);
+  
+  return expr;
+}
+