[cfe-commits] r67902 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/TranslationUnit.h lib/AST/ASTContext.cpp lib/AST/TranslationUnit.cpp tools/clang-cc/ASTConsumers.cpp

Chris Lattner sabre at nondot.org
Fri Mar 27 20:45:22 PDT 2009


Author: lattner
Date: Fri Mar 27 22:45:20 2009
New Revision: 67902

URL: http://llvm.org/viewvc/llvm-project?rev=67902&view=rev
Log:
move serialization logic from TranslationUnit to ASTContext.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/TranslationUnit.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/TranslationUnit.cpp
    cfe/trunk/tools/clang-cc/ASTConsumers.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Mar 27 22:45:20 2009
@@ -32,6 +32,7 @@
 }
 
 namespace clang {
+  class FileManager;
   class ASTRecordLayout;
   class Expr;
   class IdentifierTable;
@@ -146,6 +147,7 @@
   DeclarationNameTable DeclarationNames;
 
   SourceManager& getSourceManager() { return SourceMgr; }
+  const SourceManager& getSourceManager() const { return SourceMgr; }
   void *Allocate(unsigned Size, unsigned Align = 8) {
     return FreeMemory ? MallocAlloc.Allocate(Size, Align) :
                         BumpAlloc.Allocate(Size, Align);
@@ -674,6 +676,10 @@
   //                    Serialization
   //===--------------------------------------------------------------------===//
 
+  void EmitAll(llvm::Serializer& S) const;
+  static ASTContext* CreateAll(llvm::Deserializer &D,
+                               FileManager &FMgr);  
+
   void Emit(llvm::Serializer& S) const;
   static ASTContext* Create(llvm::Deserializer& D);  
   

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

==============================================================================
--- cfe/trunk/include/clang/AST/TranslationUnit.h (original)
+++ cfe/trunk/include/clang/AST/TranslationUnit.h Fri Mar 27 22:45:20 2009
@@ -44,10 +44,7 @@
   ~TranslationUnit();
 
   const std::string& getSourceFile() const;
-  
-  /// Emit - Emit the translation unit to an arbitray bitcode stream.
-  void Emit(llvm::Serializer& S) const;
-  
+ 
   /// Create - Reconsititute a translation unit from a bitcode stream.
   static TranslationUnit* Create(llvm::Deserializer& D, FileManager& FMgr);
   

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=67902&r1=67901&r2=67902&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Mar 27 22:45:20 2009
@@ -17,12 +17,12 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/RecordLayout.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Bitcode/Serialize.h"
 #include "llvm/Bitcode/Deserialize.h"
 #include "llvm/Support/MathExtras.h"
-
 using namespace clang;
 
 enum FloatingRank {
@@ -3080,6 +3080,47 @@
 //                         Serialization Support
 //===----------------------------------------------------------------------===//
 
+enum {
+  BasicMetadataBlock = 1,
+  ASTContextBlock = 2,
+  DeclsBlock = 3
+};
+
+void ASTContext::EmitAll(llvm::Serializer &S) const {
+  // ===---------------------------------------------------===/
+  //      Serialize the "Translation Unit" metadata.
+  // ===---------------------------------------------------===/
+  
+  // Emit ASTContext.
+  S.EnterBlock(ASTContextBlock);  
+  S.EmitOwnedPtr(this);  
+  S.ExitBlock();      // exit "ASTContextBlock"
+  
+  S.EnterBlock(BasicMetadataBlock);
+  
+  // Block for SourceManager and Target.  Allows easy skipping
+  // around to the block for the Selectors during deserialization.
+  S.EnterBlock();
+  
+  // Emit the SourceManager.
+  S.Emit(getSourceManager());
+  
+  // Emit the Target.
+  S.EmitPtr(&Target);
+  S.EmitCStr(Target.getTargetTriple());
+  
+  S.ExitBlock(); // exit "SourceManager and Target Block"
+  
+  // Emit the Selectors.
+  S.Emit(Selectors);
+  
+  // Emit the Identifier Table.
+  S.Emit(Idents);
+  
+  S.ExitBlock(); // exit "BasicMetadataBlock"
+}
+
+
 /// Emit - Serialize an ASTContext object to Bitcode.
 void ASTContext::Emit(llvm::Serializer& S) const {
   S.Emit(LangOpts);
@@ -3101,6 +3142,51 @@
   // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
 }
 
+ASTContext* ASTContext::CreateAll(llvm::Deserializer &Dezr,
+                                  FileManager &FMgr) {
+  // ===---------------------------------------------------===/
+  //      Deserialize the "Translation Unit" metadata.
+  // ===---------------------------------------------------===/
+  
+  // Skip to the BasicMetaDataBlock.  First jump to ASTContextBlock
+  // (which will appear earlier) and record its location.
+  
+  bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
+  assert (FoundBlock);
+  
+  llvm::Deserializer::Location ASTContextBlockLoc =
+  Dezr.getCurrentBlockLocation();
+  
+  FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
+  assert (FoundBlock);
+  
+  // Read the SourceManager.
+  SourceManager::CreateAndRegister(Dezr, FMgr);
+  
+  { // Read the TargetInfo.
+    llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
+    char* triple = Dezr.ReadCStr(NULL,0,true);
+    Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
+    delete [] triple;
+  }
+  
+  // For Selectors, we must read the identifier table first because the
+  //  SelectorTable depends on the identifiers being already deserialized.
+  llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
+  Dezr.SkipBlock();
+  
+  // Read the identifier table.
+  IdentifierTable::CreateAndRegister(Dezr);
+  
+  // Now jump back and read the selectors.
+  Dezr.JumpTo(SelectorBlkLoc);
+  SelectorTable::CreateAndRegister(Dezr);
+  
+  // Now jump back to ASTContextBlock and read the ASTContext.
+  Dezr.JumpTo(ASTContextBlockLoc);
+  return Dezr.ReadOwnedPtr<ASTContext>();
+}
+
 ASTContext* ASTContext::Create(llvm::Deserializer& D) {
   
   // Read the language options.

Modified: cfe/trunk/lib/AST/TranslationUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/TranslationUnit.cpp?rev=67902&r1=67901&r2=67902&view=diff

==============================================================================
--- cfe/trunk/lib/AST/TranslationUnit.cpp (original)
+++ cfe/trunk/lib/AST/TranslationUnit.cpp Fri Mar 27 22:45:20 2009
@@ -26,9 +26,6 @@
 
 using namespace clang;
 
-enum { BasicMetadataBlock = 1,
-       ASTContextBlock = 2,
-       DeclsBlock = 3 };
 
 TranslationUnit::~TranslationUnit() {
   if (OwnsMetaData && Context) {
@@ -79,7 +76,7 @@
     llvm::Serializer Sezr(Stream);  
     
     // Emit the translation unit.
-    TU.Emit(Sezr);
+    TU.getContext().EmitAll(Sezr);
   }
   
   return true;
@@ -118,40 +115,6 @@
   return false;  
 }
 
-void TranslationUnit::Emit(llvm::Serializer& Sezr) const {
-  // ===---------------------------------------------------===/
-  //      Serialize the "Translation Unit" metadata.
-  // ===---------------------------------------------------===/
-
-  // Emit ASTContext.
-  Sezr.EnterBlock(ASTContextBlock);  
-  Sezr.EmitOwnedPtr(Context);  
-  Sezr.ExitBlock();      // exit "ASTContextBlock"
-  
-  Sezr.EnterBlock(BasicMetadataBlock);
-  
-  // Block for SourceManager and Target.  Allows easy skipping
-  // around to the block for the Selectors during deserialization.
-  Sezr.EnterBlock();
-    
-  // Emit the SourceManager.
-  Sezr.Emit(Context->getSourceManager());
-    
-  // Emit the Target.
-  Sezr.EmitPtr(&Context->Target);
-  Sezr.EmitCStr(Context->Target.getTargetTriple());
-  
-  Sezr.ExitBlock(); // exit "SourceManager and Target Block"
-  
-  // Emit the Selectors.
-  Sezr.Emit(Context->Selectors);
-  
-  // Emit the Identifier Table.
-  Sezr.Emit(Context->Idents);
-  
-  Sezr.ExitBlock(); // exit "BasicMetadataBlock"
-}
-
 TranslationUnit*
 clang::ReadASTBitcodeBuffer(llvm::MemoryBuffer& MBuffer, FileManager& FMgr) {
 
@@ -202,48 +165,7 @@
   // Create the translation unit object.
   TranslationUnit* TU = new TranslationUnit();
   
-  // ===---------------------------------------------------===/
-  //      Deserialize the "Translation Unit" metadata.
-  // ===---------------------------------------------------===/
-  
-  // Skip to the BasicMetaDataBlock.  First jump to ASTContextBlock
-  // (which will appear earlier) and record its location.
-  
-  bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
-  assert (FoundBlock);
-  
-  llvm::Deserializer::Location ASTContextBlockLoc =
-  Dezr.getCurrentBlockLocation();
-  
-  FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
-  assert (FoundBlock);
-  
-  // Read the SourceManager.
-  SourceManager::CreateAndRegister(Dezr,FMgr);
-    
-  { // Read the TargetInfo.
-    llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
-    char* triple = Dezr.ReadCStr(NULL,0,true);
-    Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
-    delete [] triple;
-  }
-  
-  // For Selectors, we must read the identifier table first because the
-  //  SelectorTable depends on the identifiers being already deserialized.
-  llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
-  Dezr.SkipBlock();
-  
-  // Read the identifier table.
-  IdentifierTable::CreateAndRegister(Dezr);
-  
-  // Now jump back and read the selectors.
-  Dezr.JumpTo(SelectorBlkLoc);
-  SelectorTable::CreateAndRegister(Dezr);
-  
-  // Now jump back to ASTContextBlock and read the ASTContext.
-  Dezr.JumpTo(ASTContextBlockLoc);
-  TU->Context = Dezr.ReadOwnedPtr<ASTContext>();
+  TU->Context = ASTContext.CreateAll(Dezr, FmMgr);
   
   return TU;
 }
-

Modified: cfe/trunk/tools/clang-cc/ASTConsumers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/ASTConsumers.cpp?rev=67902&r1=67901&r2=67902&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/ASTConsumers.cpp (original)
+++ cfe/trunk/tools/clang-cc/ASTConsumers.cpp Fri Mar 27 22:45:20 2009
@@ -659,8 +659,7 @@
   DeclContextPrinter() : Out(llvm::errs()) {}
 
   void HandleTranslationUnit(TranslationUnit& TU) {
-    TranslationUnitDecl* TUD = TU.getContext().getTranslationUnitDecl();
-    PrintDeclContext(TUD, 4);
+    PrintDeclContext(TU.getContext().getTranslationUnitDecl(), 4);
   }
 
   void PrintDeclContext(const DeclContext* DC, unsigned Indentation);





More information about the cfe-commits mailing list