[cfe-commits] r70193 - in /cfe/trunk: clang.xcodeproj/project.pbxproj lib/Frontend/CMakeLists.txt lib/Frontend/PCHWriter.cpp lib/Frontend/PCHWriterDecl.cpp

Chris Lattner sabre at nondot.org
Sun Apr 26 23:16:06 PDT 2009


Author: lattner
Date: Mon Apr 27 01:16:06 2009
New Revision: 70193

URL: http://llvm.org/viewvc/llvm-project?rev=70193&view=rev
Log:
split decl writing out to its own PCHWriterDecl.cpp file.

Added:
    cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
Modified:
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/lib/Frontend/CMakeLists.txt
    cfe/trunk/lib/Frontend/PCHWriter.cpp

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=70193&r1=70192&r2=70193&view=diff

==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Mon Apr 27 01:16:06 2009
@@ -174,6 +174,7 @@
 		DECB6F070F9D93A800F5FBC7 /* InitPreprocessor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DECB6F060F9D93A800F5FBC7 /* InitPreprocessor.cpp */; };
 		DECB77130FA5752300F5FBC7 /* PCHReaderStmt.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DECB77120FA5752300F5FBC7 /* PCHReaderStmt.cpp */; };
 		DECB77790FA579B000F5FBC7 /* PCHReaderDecl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DECB77780FA579B000F5FBC7 /* PCHReaderDecl.cpp */; };
+		DECB77F70FA5850200F5FBC7 /* PCHWriterDecl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DECB77F60FA5850200F5FBC7 /* PCHWriterDecl.cpp */; };
 		DED626C90AE0C065001E80A4 /* TargetInfo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DED626C80AE0C065001E80A4 /* TargetInfo.cpp */; };
 		DED62ABB0AE2EDF1001E80A4 /* Decl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DED62ABA0AE2EDF1001E80A4 /* Decl.cpp */; };
 		DED676D10B6C786700AAD4A3 /* Builtins.def in CopyFiles */ = {isa = PBXBuildFile; fileRef = DED676D00B6C786700AAD4A3 /* Builtins.def */; };
@@ -587,6 +588,7 @@
 		DECB73550FA3EE5A00F5FBC7 /* StmtCXX.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StmtCXX.h; path = clang/AST/StmtCXX.h; sourceTree = "<group>"; };
 		DECB77120FA5752300F5FBC7 /* PCHReaderStmt.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PCHReaderStmt.cpp; path = lib/Frontend/PCHReaderStmt.cpp; sourceTree = "<group>"; };
 		DECB77780FA579B000F5FBC7 /* PCHReaderDecl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PCHReaderDecl.cpp; path = lib/Frontend/PCHReaderDecl.cpp; sourceTree = "<group>"; };
+		DECB77F60FA5850200F5FBC7 /* PCHWriterDecl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PCHWriterDecl.cpp; path = lib/Frontend/PCHWriterDecl.cpp; sourceTree = "<group>"; };
 		DED626C80AE0C065001E80A4 /* TargetInfo.cpp */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; path = TargetInfo.cpp; sourceTree = "<group>"; tabWidth = 2; };
 		DED62ABA0AE2EDF1001E80A4 /* Decl.cpp */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; name = Decl.cpp; path = lib/AST/Decl.cpp; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
 		DED676D00B6C786700AAD4A3 /* Builtins.def */ = {isa = PBXFileReference; fileEncoding = 30; indentWidth = 2; lastKnownFileType = text; name = Builtins.def; path = clang/AST/Builtins.def; sourceTree = "<group>"; tabWidth = 2; };
@@ -815,6 +817,7 @@
 				DECB77780FA579B000F5FBC7 /* PCHReaderDecl.cpp */,
 				DECB77120FA5752300F5FBC7 /* PCHReaderStmt.cpp */,
 				DEF165700F8FB34D0098507F /* PCHWriter.cpp */,
+				DECB77F60FA5850200F5FBC7 /* PCHWriterDecl.cpp */,
 				352246E40F5C6BE000D0D279 /* PlistDiagnostics.cpp */,
 				352246E50F5C6BE000D0D279 /* TextDiagnosticBuffer.cpp */,
 				352246E60F5C6BE000D0D279 /* TextDiagnosticPrinter.cpp */,
@@ -1669,6 +1672,7 @@
 				DECB6F070F9D93A800F5FBC7 /* InitPreprocessor.cpp in Sources */,
 				DECB77130FA5752300F5FBC7 /* PCHReaderStmt.cpp in Sources */,
 				DECB77790FA579B000F5FBC7 /* PCHReaderDecl.cpp in Sources */,
+				DECB77F70FA5850200F5FBC7 /* PCHWriterDecl.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Modified: cfe/trunk/lib/Frontend/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CMakeLists.txt?rev=70193&r1=70192&r2=70193&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/CMakeLists.txt (original)
+++ cfe/trunk/lib/Frontend/CMakeLists.txt Mon Apr 27 01:16:06 2009
@@ -11,6 +11,7 @@
   PCHReaderDecl.cpp
   PCHReaderStmt.cpp
   PCHWriter.cpp
+  PCHWriterDecl.cpp
   PlistDiagnostics.cpp
   ManagerRegistry.cpp
   )

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

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Mon Apr 27 01:16:06 2009
@@ -17,7 +17,6 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclContextInternals.h"
-#include "clang/AST/DeclVisitor.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/AST/Type.h"
@@ -40,6 +39,7 @@
 //===----------------------------------------------------------------------===//
 // Type serialization
 //===----------------------------------------------------------------------===//
+
 namespace {
   class VISIBILITY_HIDDEN PCHTypeWriter {
     PCHWriter &Writer;
@@ -229,378 +229,6 @@
   Code = pch::TYPE_OBJC_QUALIFIED_ID;
 }
 
-//===----------------------------------------------------------------------===//
-// Declaration serialization
-//===----------------------------------------------------------------------===//
-namespace {
-  class VISIBILITY_HIDDEN PCHDeclWriter
-    : public DeclVisitor<PCHDeclWriter, void> {
-
-    PCHWriter &Writer;
-    ASTContext &Context;
-    PCHWriter::RecordData &Record;
-
-  public:
-    pch::DeclCode Code;
-
-    PCHDeclWriter(PCHWriter &Writer, ASTContext &Context, 
-                  PCHWriter::RecordData &Record) 
-      : Writer(Writer), Context(Context), Record(Record) { }
-
-    void VisitDecl(Decl *D);
-    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
-    void VisitNamedDecl(NamedDecl *D);
-    void VisitTypeDecl(TypeDecl *D);
-    void VisitTypedefDecl(TypedefDecl *D);
-    void VisitTagDecl(TagDecl *D);
-    void VisitEnumDecl(EnumDecl *D);
-    void VisitRecordDecl(RecordDecl *D);
-    void VisitValueDecl(ValueDecl *D);
-    void VisitEnumConstantDecl(EnumConstantDecl *D);
-    void VisitFunctionDecl(FunctionDecl *D);
-    void VisitFieldDecl(FieldDecl *D);
-    void VisitVarDecl(VarDecl *D);
-    void VisitImplicitParamDecl(ImplicitParamDecl *D);
-    void VisitParmVarDecl(ParmVarDecl *D);
-    void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
-    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
-    void VisitBlockDecl(BlockDecl *D);
-    void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 
-                          uint64_t VisibleOffset);
-    void VisitObjCMethodDecl(ObjCMethodDecl *D);
-    void VisitObjCContainerDecl(ObjCContainerDecl *D);
-    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
-    void VisitObjCIvarDecl(ObjCIvarDecl *D);
-    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
-    void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
-    void VisitObjCClassDecl(ObjCClassDecl *D);
-    void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
-    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
-    void VisitObjCImplDecl(ObjCImplDecl *D);
-    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
-    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
-    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
-    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
-    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
-  };
-}
-
-void PCHDeclWriter::VisitDecl(Decl *D) {
-  Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
-  Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
-  Writer.AddSourceLocation(D->getLocation(), Record);
-  Record.push_back(D->isInvalidDecl());
-  Record.push_back(D->hasAttrs());
-  Record.push_back(D->isImplicit());
-  Record.push_back(D->getAccess());
-}
-
-void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
-  VisitDecl(D);
-  Code = pch::DECL_TRANSLATION_UNIT;
-}
-
-void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
-  VisitDecl(D);
-  Writer.AddDeclarationName(D->getDeclName(), Record);
-}
-
-void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
-  VisitNamedDecl(D);
-  Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
-}
-
-void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
-  VisitTypeDecl(D);
-  Writer.AddTypeRef(D->getUnderlyingType(), Record);
-  Code = pch::DECL_TYPEDEF;
-}
-
-void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
-  VisitTypeDecl(D);
-  Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
-  Record.push_back(D->isDefinition());
-  Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
-}
-
-void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
-  VisitTagDecl(D);
-  Writer.AddTypeRef(D->getIntegerType(), Record);
-  Code = pch::DECL_ENUM;
-}
-
-void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
-  VisitTagDecl(D);
-  Record.push_back(D->hasFlexibleArrayMember());
-  Record.push_back(D->isAnonymousStructOrUnion());
-  Code = pch::DECL_RECORD;
-}
-
-void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
-  VisitNamedDecl(D);
-  Writer.AddTypeRef(D->getType(), Record);
-}
-
-void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
-  VisitValueDecl(D);
-  Record.push_back(D->getInitExpr()? 1 : 0);
-  if (D->getInitExpr())
-    Writer.AddStmt(D->getInitExpr());
-  Writer.AddAPSInt(D->getInitVal(), Record);
-  Code = pch::DECL_ENUM_CONSTANT;
-}
-
-void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
-  VisitValueDecl(D);
-  Record.push_back(D->isThisDeclarationADefinition());
-  if (D->isThisDeclarationADefinition())
-    Writer.AddStmt(D->getBody(Context));
-  Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
-  Record.push_back(D->getStorageClass()); // FIXME: stable encoding
-  Record.push_back(D->isInline());
-  Record.push_back(D->isC99InlineDefinition());
-  Record.push_back(D->isVirtual());
-  Record.push_back(D->isPure());
-  Record.push_back(D->inheritedPrototype());
-  Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
-  Record.push_back(D->isDeleted());
-  Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
-  Record.push_back(D->param_size());
-  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
-       P != PEnd; ++P)
-    Writer.AddDeclRef(*P, Record);
-  Code = pch::DECL_FUNCTION;
-}
-
-void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
-  VisitNamedDecl(D);
-  // FIXME: convert to LazyStmtPtr?
-  // Unlike C/C++, method bodies will never be in header files. 
-  Record.push_back(D->getBody() != 0);
-  if (D->getBody() != 0) {
-    Writer.AddStmt(D->getBody(Context));
-    Writer.AddDeclRef(D->getSelfDecl(), Record);
-    Writer.AddDeclRef(D->getCmdDecl(), Record);
-  }
-  Record.push_back(D->isInstanceMethod());
-  Record.push_back(D->isVariadic());
-  Record.push_back(D->isSynthesized());
-  // FIXME: stable encoding for @required/@optional
-  Record.push_back(D->getImplementationControl()); 
-  // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
-  Record.push_back(D->getObjCDeclQualifier()); 
-  Writer.AddTypeRef(D->getResultType(), Record);
-  Writer.AddSourceLocation(D->getLocEnd(), Record);
-  Record.push_back(D->param_size());
-  for (ObjCMethodDecl::param_iterator P = D->param_begin(), 
-                                   PEnd = D->param_end(); P != PEnd; ++P)
-    Writer.AddDeclRef(*P, Record);
-  Code = pch::DECL_OBJC_METHOD;
-}
-
-void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
-  VisitNamedDecl(D);
-  Writer.AddSourceLocation(D->getAtEndLoc(), Record);
-  // Abstract class (no need to define a stable pch::DECL code).
-}
-
-void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
-  VisitObjCContainerDecl(D);
-  Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
-  Writer.AddDeclRef(D->getSuperClass(), Record);
-  Record.push_back(D->protocol_size());
-  for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), 
-         PEnd = D->protocol_end();
-       P != PEnd; ++P)
-    Writer.AddDeclRef(*P, Record);
-  Record.push_back(D->ivar_size());
-  for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), 
-                                     IEnd = D->ivar_end(); I != IEnd; ++I)
-    Writer.AddDeclRef(*I, Record);
-  Writer.AddDeclRef(D->getCategoryList(), Record);
-  Record.push_back(D->isForwardDecl());
-  Record.push_back(D->isImplicitInterfaceDecl());
-  Writer.AddSourceLocation(D->getClassLoc(), Record);
-  Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
-  Writer.AddSourceLocation(D->getLocEnd(), Record);
-  Code = pch::DECL_OBJC_INTERFACE;
-}
-
-void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
-  VisitFieldDecl(D);
-  // FIXME: stable encoding for @public/@private/@protected/@package
-  Record.push_back(D->getAccessControl()); 
-  Code = pch::DECL_OBJC_IVAR;
-}
-
-void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
-  VisitObjCContainerDecl(D);
-  Record.push_back(D->isForwardDecl());
-  Writer.AddSourceLocation(D->getLocEnd(), Record);
-  Record.push_back(D->protocol_size());
-  for (ObjCProtocolDecl::protocol_iterator 
-       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
-    Writer.AddDeclRef(*I, Record);
-  Code = pch::DECL_OBJC_PROTOCOL;
-}
-
-void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
-  VisitFieldDecl(D);
-  Code = pch::DECL_OBJC_AT_DEFS_FIELD;
-}
-
-void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
-  VisitDecl(D);
-  Record.push_back(D->size());
-  for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
-    Writer.AddDeclRef(*I, Record);
-  Code = pch::DECL_OBJC_CLASS;
-}
-
-void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
-  VisitDecl(D);
-  Record.push_back(D->protocol_size());
-  for (ObjCProtocolDecl::protocol_iterator 
-       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
-    Writer.AddDeclRef(*I, Record);
-  Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
-}
-
-void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
-  VisitObjCContainerDecl(D);
-  Writer.AddDeclRef(D->getClassInterface(), Record);
-  Record.push_back(D->protocol_size());
-  for (ObjCProtocolDecl::protocol_iterator 
-       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
-    Writer.AddDeclRef(*I, Record);
-  Writer.AddDeclRef(D->getNextClassCategory(), Record);
-  Writer.AddSourceLocation(D->getLocEnd(), Record);
-  Code = pch::DECL_OBJC_CATEGORY;
-}
-
-void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
-  VisitNamedDecl(D);
-  Writer.AddDeclRef(D->getClassInterface(), Record);
-  Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
-}
-
-void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
-  VisitNamedDecl(D);
-  Writer.AddTypeRef(D->getType(), Record);
-  // FIXME: stable encoding
-  Record.push_back((unsigned)D->getPropertyAttributes());
-  // FIXME: stable encoding
-  Record.push_back((unsigned)D->getPropertyImplementation());
-  Writer.AddDeclarationName(D->getGetterName(), Record);
-  Writer.AddDeclarationName(D->getSetterName(), Record);
-  Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
-  Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
-  Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
-  Code = pch::DECL_OBJC_PROPERTY;
-}
-
-void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
-  VisitNamedDecl(D);
-  Writer.AddDeclRef(D->getClassInterface(), Record);
-  Writer.AddSourceLocation(D->getLocEnd(), Record);
-  // Abstract class (no need to define a stable pch::DECL code).
-}
-
-void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
-  VisitObjCImplDecl(D);
-  Writer.AddIdentifierRef(D->getIdentifier(), Record);
-  Code = pch::DECL_OBJC_CATEGORY_IMPL;
-}
-
-void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
-  VisitObjCImplDecl(D);
-  Writer.AddDeclRef(D->getSuperClass(), Record);
-  Code = pch::DECL_OBJC_IMPLEMENTATION;
-}
-
-void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
-  VisitDecl(D);
-  Writer.AddSourceLocation(D->getLocStart(), Record);
-  Writer.AddDeclRef(D->getPropertyDecl(), Record);
-  Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
-  Code = pch::DECL_OBJC_PROPERTY_IMPL;
-}
-
-void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
-  VisitValueDecl(D);
-  Record.push_back(D->isMutable());
-  Record.push_back(D->getBitWidth()? 1 : 0);
-  if (D->getBitWidth())
-    Writer.AddStmt(D->getBitWidth());
-  Code = pch::DECL_FIELD;
-}
-
-void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
-  VisitValueDecl(D);
-  Record.push_back(D->getStorageClass()); // FIXME: stable encoding
-  Record.push_back(D->isThreadSpecified());
-  Record.push_back(D->hasCXXDirectInitializer());
-  Record.push_back(D->isDeclaredInCondition());
-  Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
-  Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
-  Record.push_back(D->getInit()? 1 : 0);
-  if (D->getInit())
-    Writer.AddStmt(D->getInit());
-  Code = pch::DECL_VAR;
-}
-
-void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
-  VisitVarDecl(D);
-  Code = pch::DECL_IMPLICIT_PARAM;
-}
-
-void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
-  VisitVarDecl(D);
-  Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
-  // FIXME: emit default argument (C++)
-  // FIXME: why isn't the "default argument" just stored as the initializer
-  // in VarDecl?
-  Code = pch::DECL_PARM_VAR;
-}
-
-void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
-  VisitParmVarDecl(D);
-  Writer.AddTypeRef(D->getOriginalType(), Record);
-  Code = pch::DECL_ORIGINAL_PARM_VAR;
-}
-
-void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
-  VisitDecl(D);
-  Writer.AddStmt(D->getAsmString());
-  Code = pch::DECL_FILE_SCOPE_ASM;
-}
-
-void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
-  VisitDecl(D);
-  Writer.AddStmt(D->getBody());
-  Record.push_back(D->param_size());
-  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
-       P != PEnd; ++P)
-    Writer.AddDeclRef(*P, Record);
-  Code = pch::DECL_BLOCK;
-}
-
-/// \brief Emit the DeclContext part of a declaration context decl.
-///
-/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
-/// block for this declaration context is stored. May be 0 to indicate
-/// that there are no declarations stored within this context.
-///
-/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
-/// block for this declaration context is stored. May be 0 to indicate
-/// that there are no declarations visible from this context. Note
-/// that this value will not be emitted for non-primary declaration
-/// contexts.
-void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 
-                                     uint64_t VisibleOffset) {
-  Record.push_back(LexicalOffset);
-  Record.push_back(VisibleOffset);
-}
 
 //===----------------------------------------------------------------------===//
 // Statement/expression serialization
@@ -2024,78 +1652,6 @@
   return Offset;
 }
 
-/// \brief Write a block containing all of the declarations.
-void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
-  // Enter the declarations block.
-  Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
-
-  // Emit all of the declarations.
-  RecordData Record;
-  PCHDeclWriter W(*this, Context, Record);
-  while (!DeclsToEmit.empty()) {
-    // Pull the next declaration off the queue
-    Decl *D = DeclsToEmit.front();
-    DeclsToEmit.pop();
-
-    // If this declaration is also a DeclContext, write blocks for the
-    // declarations that lexically stored inside its context and those
-    // declarations that are visible from its context. These blocks
-    // are written before the declaration itself so that we can put
-    // their offsets into the record for the declaration.
-    uint64_t LexicalOffset = 0;
-    uint64_t VisibleOffset = 0;
-    DeclContext *DC = dyn_cast<DeclContext>(D);
-    if (DC) {
-      LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
-      VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
-    }
-
-    // Determine the ID for this declaration
-    pch::DeclID ID = DeclIDs[D];
-    if (ID == 0)
-      ID = DeclIDs.size();
-
-    unsigned Index = ID - 1;
-
-    // Record the offset for this declaration
-    if (DeclOffsets.size() == Index)
-      DeclOffsets.push_back(Stream.GetCurrentBitNo());
-    else if (DeclOffsets.size() < Index) {
-      DeclOffsets.resize(Index+1);
-      DeclOffsets[Index] = Stream.GetCurrentBitNo();
-    }
-
-    // Build and emit a record for this declaration
-    Record.clear();
-    W.Code = (pch::DeclCode)0;
-    W.Visit(D);
-    if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
-
-    if (!W.Code) {
-      fprintf(stderr, "Cannot serialize declaration of kind %s\n",
-              D->getDeclKindName());
-      assert(false && "Unhandled declaration kind while generating PCH");
-      exit(-1);
-    }
-    Stream.EmitRecord(W.Code, Record);
-
-    // If the declaration had any attributes, write them now.
-    if (D->hasAttrs())
-      WriteAttributeRecord(D->getAttrs());
-
-    // Flush any expressions that were written as part of this declaration.
-    FlushStmts();
-    
-    // Note external declarations so that we can add them to a record
-    // in the PCH file later.
-    if (isa<FileScopeAsmDecl>(D))
-      ExternalDefinitions.push_back(ID);
-  }
-
-  // Exit the declarations block
-  Stream.ExitBlock();
-}
-
 namespace {
 // Trait used for the on-disk hash table used in the method pool.
 class VISIBILITY_HIDDEN PCHMethodPoolTrait {

Added: cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterDecl.cpp?rev=70193&view=auto

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterDecl.cpp (added)
+++ cfe/trunk/lib/Frontend/PCHWriterDecl.cpp Mon Apr 27 01:16:06 2009
@@ -0,0 +1,468 @@
+//===--- PCHWriterDecl.cpp - Declaration Serialization --------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements serialization for Declarations.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Frontend/PCHWriter.h"
+#include "clang/AST/DeclVisitor.h"
+#include "clang/AST/Expr.h"
+#include "llvm/Bitcode/BitstreamWriter.h"
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Declaration serialization
+//===----------------------------------------------------------------------===//
+
+namespace {
+  class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> {
+
+    PCHWriter &Writer;
+    ASTContext &Context;
+    PCHWriter::RecordData &Record;
+
+  public:
+    pch::DeclCode Code;
+
+    PCHDeclWriter(PCHWriter &Writer, ASTContext &Context, 
+                  PCHWriter::RecordData &Record) 
+      : Writer(Writer), Context(Context), Record(Record) { }
+
+    void VisitDecl(Decl *D);
+    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
+    void VisitNamedDecl(NamedDecl *D);
+    void VisitTypeDecl(TypeDecl *D);
+    void VisitTypedefDecl(TypedefDecl *D);
+    void VisitTagDecl(TagDecl *D);
+    void VisitEnumDecl(EnumDecl *D);
+    void VisitRecordDecl(RecordDecl *D);
+    void VisitValueDecl(ValueDecl *D);
+    void VisitEnumConstantDecl(EnumConstantDecl *D);
+    void VisitFunctionDecl(FunctionDecl *D);
+    void VisitFieldDecl(FieldDecl *D);
+    void VisitVarDecl(VarDecl *D);
+    void VisitImplicitParamDecl(ImplicitParamDecl *D);
+    void VisitParmVarDecl(ParmVarDecl *D);
+    void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
+    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
+    void VisitBlockDecl(BlockDecl *D);
+    void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 
+                          uint64_t VisibleOffset);
+    void VisitObjCMethodDecl(ObjCMethodDecl *D);
+    void VisitObjCContainerDecl(ObjCContainerDecl *D);
+    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
+    void VisitObjCIvarDecl(ObjCIvarDecl *D);
+    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
+    void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
+    void VisitObjCClassDecl(ObjCClassDecl *D);
+    void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
+    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
+    void VisitObjCImplDecl(ObjCImplDecl *D);
+    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
+    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
+    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
+    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
+    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
+  };
+}
+
+void PCHDeclWriter::VisitDecl(Decl *D) {
+  Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
+  Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
+  Writer.AddSourceLocation(D->getLocation(), Record);
+  Record.push_back(D->isInvalidDecl());
+  Record.push_back(D->hasAttrs());
+  Record.push_back(D->isImplicit());
+  Record.push_back(D->getAccess());
+}
+
+void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
+  VisitDecl(D);
+  Code = pch::DECL_TRANSLATION_UNIT;
+}
+
+void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
+  VisitDecl(D);
+  Writer.AddDeclarationName(D->getDeclName(), Record);
+}
+
+void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
+  VisitNamedDecl(D);
+  Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
+}
+
+void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
+  VisitTypeDecl(D);
+  Writer.AddTypeRef(D->getUnderlyingType(), Record);
+  Code = pch::DECL_TYPEDEF;
+}
+
+void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
+  VisitTypeDecl(D);
+  Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
+  Record.push_back(D->isDefinition());
+  Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
+}
+
+void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
+  VisitTagDecl(D);
+  Writer.AddTypeRef(D->getIntegerType(), Record);
+  Code = pch::DECL_ENUM;
+}
+
+void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
+  VisitTagDecl(D);
+  Record.push_back(D->hasFlexibleArrayMember());
+  Record.push_back(D->isAnonymousStructOrUnion());
+  Code = pch::DECL_RECORD;
+}
+
+void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
+  VisitNamedDecl(D);
+  Writer.AddTypeRef(D->getType(), Record);
+}
+
+void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
+  VisitValueDecl(D);
+  Record.push_back(D->getInitExpr()? 1 : 0);
+  if (D->getInitExpr())
+    Writer.AddStmt(D->getInitExpr());
+  Writer.AddAPSInt(D->getInitVal(), Record);
+  Code = pch::DECL_ENUM_CONSTANT;
+}
+
+void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
+  VisitValueDecl(D);
+  Record.push_back(D->isThisDeclarationADefinition());
+  if (D->isThisDeclarationADefinition())
+    Writer.AddStmt(D->getBody(Context));
+  Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
+  Record.push_back(D->getStorageClass()); // FIXME: stable encoding
+  Record.push_back(D->isInline());
+  Record.push_back(D->isC99InlineDefinition());
+  Record.push_back(D->isVirtual());
+  Record.push_back(D->isPure());
+  Record.push_back(D->inheritedPrototype());
+  Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
+  Record.push_back(D->isDeleted());
+  Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
+  Record.push_back(D->param_size());
+  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
+       P != PEnd; ++P)
+    Writer.AddDeclRef(*P, Record);
+  Code = pch::DECL_FUNCTION;
+}
+
+void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
+  VisitNamedDecl(D);
+  // FIXME: convert to LazyStmtPtr?
+  // Unlike C/C++, method bodies will never be in header files. 
+  Record.push_back(D->getBody() != 0);
+  if (D->getBody() != 0) {
+    Writer.AddStmt(D->getBody(Context));
+    Writer.AddDeclRef(D->getSelfDecl(), Record);
+    Writer.AddDeclRef(D->getCmdDecl(), Record);
+  }
+  Record.push_back(D->isInstanceMethod());
+  Record.push_back(D->isVariadic());
+  Record.push_back(D->isSynthesized());
+  // FIXME: stable encoding for @required/@optional
+  Record.push_back(D->getImplementationControl()); 
+  // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
+  Record.push_back(D->getObjCDeclQualifier()); 
+  Writer.AddTypeRef(D->getResultType(), Record);
+  Writer.AddSourceLocation(D->getLocEnd(), Record);
+  Record.push_back(D->param_size());
+  for (ObjCMethodDecl::param_iterator P = D->param_begin(), 
+                                   PEnd = D->param_end(); P != PEnd; ++P)
+    Writer.AddDeclRef(*P, Record);
+  Code = pch::DECL_OBJC_METHOD;
+}
+
+void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
+  VisitNamedDecl(D);
+  Writer.AddSourceLocation(D->getAtEndLoc(), Record);
+  // Abstract class (no need to define a stable pch::DECL code).
+}
+
+void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
+  VisitObjCContainerDecl(D);
+  Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
+  Writer.AddDeclRef(D->getSuperClass(), Record);
+  Record.push_back(D->protocol_size());
+  for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(), 
+         PEnd = D->protocol_end();
+       P != PEnd; ++P)
+    Writer.AddDeclRef(*P, Record);
+  Record.push_back(D->ivar_size());
+  for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(), 
+                                     IEnd = D->ivar_end(); I != IEnd; ++I)
+    Writer.AddDeclRef(*I, Record);
+  Writer.AddDeclRef(D->getCategoryList(), Record);
+  Record.push_back(D->isForwardDecl());
+  Record.push_back(D->isImplicitInterfaceDecl());
+  Writer.AddSourceLocation(D->getClassLoc(), Record);
+  Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
+  Writer.AddSourceLocation(D->getLocEnd(), Record);
+  Code = pch::DECL_OBJC_INTERFACE;
+}
+
+void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
+  VisitFieldDecl(D);
+  // FIXME: stable encoding for @public/@private/@protected/@package
+  Record.push_back(D->getAccessControl()); 
+  Code = pch::DECL_OBJC_IVAR;
+}
+
+void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
+  VisitObjCContainerDecl(D);
+  Record.push_back(D->isForwardDecl());
+  Writer.AddSourceLocation(D->getLocEnd(), Record);
+  Record.push_back(D->protocol_size());
+  for (ObjCProtocolDecl::protocol_iterator 
+       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
+    Writer.AddDeclRef(*I, Record);
+  Code = pch::DECL_OBJC_PROTOCOL;
+}
+
+void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
+  VisitFieldDecl(D);
+  Code = pch::DECL_OBJC_AT_DEFS_FIELD;
+}
+
+void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
+  VisitDecl(D);
+  Record.push_back(D->size());
+  for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
+    Writer.AddDeclRef(*I, Record);
+  Code = pch::DECL_OBJC_CLASS;
+}
+
+void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
+  VisitDecl(D);
+  Record.push_back(D->protocol_size());
+  for (ObjCProtocolDecl::protocol_iterator 
+       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
+    Writer.AddDeclRef(*I, Record);
+  Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
+}
+
+void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
+  VisitObjCContainerDecl(D);
+  Writer.AddDeclRef(D->getClassInterface(), Record);
+  Record.push_back(D->protocol_size());
+  for (ObjCProtocolDecl::protocol_iterator 
+       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
+    Writer.AddDeclRef(*I, Record);
+  Writer.AddDeclRef(D->getNextClassCategory(), Record);
+  Writer.AddSourceLocation(D->getLocEnd(), Record);
+  Code = pch::DECL_OBJC_CATEGORY;
+}
+
+void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
+  VisitNamedDecl(D);
+  Writer.AddDeclRef(D->getClassInterface(), Record);
+  Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
+}
+
+void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
+  VisitNamedDecl(D);
+  Writer.AddTypeRef(D->getType(), Record);
+  // FIXME: stable encoding
+  Record.push_back((unsigned)D->getPropertyAttributes());
+  // FIXME: stable encoding
+  Record.push_back((unsigned)D->getPropertyImplementation());
+  Writer.AddDeclarationName(D->getGetterName(), Record);
+  Writer.AddDeclarationName(D->getSetterName(), Record);
+  Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
+  Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
+  Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
+  Code = pch::DECL_OBJC_PROPERTY;
+}
+
+void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
+  VisitNamedDecl(D);
+  Writer.AddDeclRef(D->getClassInterface(), Record);
+  Writer.AddSourceLocation(D->getLocEnd(), Record);
+  // Abstract class (no need to define a stable pch::DECL code).
+}
+
+void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
+  VisitObjCImplDecl(D);
+  Writer.AddIdentifierRef(D->getIdentifier(), Record);
+  Code = pch::DECL_OBJC_CATEGORY_IMPL;
+}
+
+void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
+  VisitObjCImplDecl(D);
+  Writer.AddDeclRef(D->getSuperClass(), Record);
+  Code = pch::DECL_OBJC_IMPLEMENTATION;
+}
+
+void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
+  VisitDecl(D);
+  Writer.AddSourceLocation(D->getLocStart(), Record);
+  Writer.AddDeclRef(D->getPropertyDecl(), Record);
+  Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
+  Code = pch::DECL_OBJC_PROPERTY_IMPL;
+}
+
+void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
+  VisitValueDecl(D);
+  Record.push_back(D->isMutable());
+  Record.push_back(D->getBitWidth()? 1 : 0);
+  if (D->getBitWidth())
+    Writer.AddStmt(D->getBitWidth());
+  Code = pch::DECL_FIELD;
+}
+
+void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
+  VisitValueDecl(D);
+  Record.push_back(D->getStorageClass()); // FIXME: stable encoding
+  Record.push_back(D->isThreadSpecified());
+  Record.push_back(D->hasCXXDirectInitializer());
+  Record.push_back(D->isDeclaredInCondition());
+  Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
+  Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
+  Record.push_back(D->getInit()? 1 : 0);
+  if (D->getInit())
+    Writer.AddStmt(D->getInit());
+  Code = pch::DECL_VAR;
+}
+
+void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
+  VisitVarDecl(D);
+  Code = pch::DECL_IMPLICIT_PARAM;
+}
+
+void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
+  VisitVarDecl(D);
+  Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
+  // FIXME: emit default argument (C++)
+  // FIXME: why isn't the "default argument" just stored as the initializer
+  // in VarDecl?
+  Code = pch::DECL_PARM_VAR;
+}
+
+void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
+  VisitParmVarDecl(D);
+  Writer.AddTypeRef(D->getOriginalType(), Record);
+  Code = pch::DECL_ORIGINAL_PARM_VAR;
+}
+
+void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
+  VisitDecl(D);
+  Writer.AddStmt(D->getAsmString());
+  Code = pch::DECL_FILE_SCOPE_ASM;
+}
+
+void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
+  VisitDecl(D);
+  Writer.AddStmt(D->getBody());
+  Record.push_back(D->param_size());
+  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
+       P != PEnd; ++P)
+    Writer.AddDeclRef(*P, Record);
+  Code = pch::DECL_BLOCK;
+}
+
+/// \brief Emit the DeclContext part of a declaration context decl.
+///
+/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
+/// block for this declaration context is stored. May be 0 to indicate
+/// that there are no declarations stored within this context.
+///
+/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
+/// block for this declaration context is stored. May be 0 to indicate
+/// that there are no declarations visible from this context. Note
+/// that this value will not be emitted for non-primary declaration
+/// contexts.
+void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset, 
+                                     uint64_t VisibleOffset) {
+  Record.push_back(LexicalOffset);
+  Record.push_back(VisibleOffset);
+}
+
+
+//===----------------------------------------------------------------------===//
+// PCHWriter Implementation
+//===----------------------------------------------------------------------===//
+
+/// \brief Write a block containing all of the declarations.
+void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
+  // Enter the declarations block.
+  Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
+
+  // Emit all of the declarations.
+  RecordData Record;
+  PCHDeclWriter W(*this, Context, Record);
+  while (!DeclsToEmit.empty()) {
+    // Pull the next declaration off the queue
+    Decl *D = DeclsToEmit.front();
+    DeclsToEmit.pop();
+
+    // If this declaration is also a DeclContext, write blocks for the
+    // declarations that lexically stored inside its context and those
+    // declarations that are visible from its context. These blocks
+    // are written before the declaration itself so that we can put
+    // their offsets into the record for the declaration.
+    uint64_t LexicalOffset = 0;
+    uint64_t VisibleOffset = 0;
+    DeclContext *DC = dyn_cast<DeclContext>(D);
+    if (DC) {
+      LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
+      VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
+    }
+
+    // Determine the ID for this declaration
+    pch::DeclID ID = DeclIDs[D];
+    if (ID == 0)
+      ID = DeclIDs.size();
+
+    unsigned Index = ID - 1;
+
+    // Record the offset for this declaration
+    if (DeclOffsets.size() == Index)
+      DeclOffsets.push_back(Stream.GetCurrentBitNo());
+    else if (DeclOffsets.size() < Index) {
+      DeclOffsets.resize(Index+1);
+      DeclOffsets[Index] = Stream.GetCurrentBitNo();
+    }
+
+    // Build and emit a record for this declaration
+    Record.clear();
+    W.Code = (pch::DeclCode)0;
+    W.Visit(D);
+    if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
+
+    if (!W.Code) {
+      fprintf(stderr, "Cannot serialize declaration of kind %s\n",
+              D->getDeclKindName());
+      assert(false && "Unhandled declaration kind while generating PCH");
+      exit(-1);
+    }
+    Stream.EmitRecord(W.Code, Record);
+
+    // If the declaration had any attributes, write them now.
+    if (D->hasAttrs())
+      WriteAttributeRecord(D->getAttrs());
+
+    // Flush any expressions that were written as part of this declaration.
+    FlushStmts();
+    
+    // Note external declarations so that we can add them to a record
+    // in the PCH file later.
+    if (isa<FileScopeAsmDecl>(D))
+      ExternalDefinitions.push_back(ID);
+  }
+
+  // Exit the declarations block
+  Stream.ExitBlock();
+}





More information about the cfe-commits mailing list