r265526 - Revert "[modules] Continue factoring encoding of AST records out of ASTWriter."

Dmitry Polukhin via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 6 03:01:47 PDT 2016


Author: dpolukhin
Date: Wed Apr  6 05:01:46 2016
New Revision: 265526

URL: http://llvm.org/viewvc/llvm-project?rev=265526&view=rev
Log:
Revert "[modules] Continue factoring encoding of AST records out of ASTWriter."

This reverts commit r265518.

Modified:
    cfe/trunk/include/clang/AST/DeclCXX.h
    cfe/trunk/include/clang/Serialization/ASTWriter.h
    cfe/trunk/lib/Serialization/ASTWriter.cpp
    cfe/trunk/lib/Serialization/ASTWriterDecl.cpp
    cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
    cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp

Modified: cfe/trunk/include/clang/AST/DeclCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclCXX.h?rev=265526&r1=265525&r2=265526&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclCXX.h (original)
+++ cfe/trunk/include/clang/AST/DeclCXX.h Wed Apr  6 05:01:46 2016
@@ -1714,7 +1714,6 @@ public:
 
   friend class ASTDeclReader;
   friend class ASTDeclWriter;
-  friend class ASTRecordWriter;
   friend class ASTReader;
   friend class ASTWriter;
 };

Modified: cfe/trunk/include/clang/Serialization/ASTWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTWriter.h?rev=265526&r1=265525&r2=265526&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTWriter.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTWriter.h Wed Apr  6 05:01:46 2016
@@ -90,7 +90,6 @@ public:
 
   friend class ASTDeclWriter;
   friend class ASTStmtWriter;
-  friend class ASTTypeWriter;
   friend class ASTRecordWriter;
 private:
   /// \brief Map that provides the ID numbers of each type within the
@@ -392,6 +391,14 @@ private:
   /// redeclaration chains.
   llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
                                       
+  /// \brief Statements that we've encountered while serializing a
+  /// declaration or type.
+  SmallVector<Stmt *, 16> StmtsToEmit;
+
+  /// \brief Statements collection to use for ASTWriter::AddStmt().
+  /// It will point to StmtsToEmit unless it is overriden.
+  SmallVector<Stmt *, 16> *CollectedStmts;
+
   /// \brief Mapping from SwitchCase statements to IDs.
   llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
 
@@ -548,6 +555,7 @@ private:
 
   void WriteDeclAbbrevs();
   void WriteDecl(ASTContext &Context, Decl *D);
+  void AddFunctionDefinition(const FunctionDecl *FD, RecordDataImpl &Record);
 
   uint64_t WriteASTCore(Sema &SemaRef,
                         StringRef isysroot, const std::string &OutputFile,
@@ -641,6 +649,26 @@ public:
   /// \brief Determine the type ID of an already-emitted type.
   serialization::TypeID getTypeID(QualType T) const;
 
+  /// \brief Emits a reference to a declarator info.
+  void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
+
+  /// \brief Emits a type with source-location information.
+  void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
+
+  /// \brief Emits a template argument location info.
+  void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
+                                  const TemplateArgumentLocInfo &Arg,
+                                  RecordDataImpl &Record);
+
+  /// \brief Emits a template argument location.
+  void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
+                              RecordDataImpl &Record);
+
+  /// \brief Emits an AST template argument list info.
+  void AddASTTemplateArgumentListInfo(
+                          const ASTTemplateArgumentListInfo *ASTTemplArgList,
+                          RecordDataImpl &Record);
+
   /// \brief Find the first local declaration of a given local redeclarable
   /// decl.
   const Decl *getFirstLocalDecl(const Decl *D);
@@ -656,26 +684,59 @@ public:
   /// declaration.
   serialization::DeclID getDeclID(const Decl *D);
 
+  void AddAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record);
+
   /// \brief Emit a declaration name.
   void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
-
+  void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
+                             DeclarationName Name, RecordDataImpl &Record);
+  void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
+                              RecordDataImpl &Record);
   unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
 
+  void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
+
   /// \brief Emit a nested name specifier.
   void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
 
+  /// \brief Emit a nested name specifier with source-location information.
+  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
+                                 RecordDataImpl &Record);
+
+  /// \brief Emit a template name.
+  void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
+
+  /// \brief Emit a template argument.
+  void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
+
   /// \brief Emit a template parameter list.
   void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
                                 RecordDataImpl &Record);
 
+  /// \brief Emit a template argument list.
+  void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
+                                RecordDataImpl &Record);
+
   /// \brief Emit a UnresolvedSet structure.
   void AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record);
 
+  /// \brief Emit a C++ base specifier.
+  void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
+                           RecordDataImpl &Record);
+
   /// \brief Emit the ID for a CXXCtorInitializer array and register the array
   /// for later serialization.
   void AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
                                  RecordDataImpl &Record);
 
+  /// \brief Emit a CXXCtorInitializer array.
+  void AddCXXCtorInitializers(
+                             const CXXCtorInitializer * const *CtorInitializers,
+                             unsigned NumCtorInitializers,
+                             RecordDataImpl &Record);
+
+  void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
+
   /// \brief Add a string to the given record.
   void AddString(StringRef Str, RecordDataImpl &Record);
 
@@ -710,6 +771,22 @@ public:
   /// within the method pool/selector table.
   void SetSelectorOffset(Selector Sel, uint32_t Offset);
 
+  /// \brief Add the given statement or expression to the queue of
+  /// statements to emit.
+  ///
+  /// This routine should be used when emitting types and declarations
+  /// that have expressions as part of their formulation. Once the
+  /// type or declaration has been written, call FlushStmts() to write
+  /// the corresponding statements just after the type or
+  /// declaration.
+  void AddStmt(Stmt *S) {
+      CollectedStmts->push_back(S);
+  }
+
+  /// \brief Flush all of the statements and expressions that have
+  /// been added to the queue via AddStmt().
+  void FlushStmts();
+
   /// \brief Flush all of the C++ base specifier sets that have been added
   /// via \c AddCXXBaseSpecifiersRef().
   void FlushCXXBaseSpecifiers();
@@ -721,6 +798,7 @@ public:
   /// \brief Flush all pending records that are tacked onto the end of
   /// decl and decl update records.
   void FlushPendingAfterDecl() {
+    FlushStmts();
     FlushCXXBaseSpecifiers();
     FlushCXXCtorInitializers();
   }
@@ -793,15 +871,6 @@ class ASTRecordWriter {
   ASTWriter *Writer;
   ASTWriter::RecordDataImpl *Record;
 
-  /// \brief Statements that we've encountered while serializing a
-  /// declaration or type.
-  SmallVector<Stmt *, 16> StmtsToEmit;
-
-  /// \brief Flush all of the statements and expressions that have
-  /// been added to the queue via AddStmt().
-  void FlushStmts();
-  void FlushSubStmts();
-
 public:
   /// Construct a ASTRecordWriter that uses the default encoding scheme.
   ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
@@ -827,37 +896,15 @@ public:
   uint64_t &operator[](size_t N) { return (*Record)[N]; }
   /// @}
 
-  /// \brief Emit the record to the stream, followed by its substatements, and
-  /// return its offset.
+
+  /// \brief Emit the record to the stream, and return its offset.
   // FIXME: Allow record producers to suggest Abbrevs.
   uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
     uint64_t Offset = Writer->Stream.GetCurrentBitNo();
-    Writer->Stream.EmitRecord(Code, *Record, Abbrev);
-    FlushStmts();
+    Writer->Stream.EmitRecord(Code, *Record);
     return Offset;
   }
 
-  /// \brief Emit the record to the stream, preceded by its substatements.
-  uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
-    FlushSubStmts();
-    Writer->Stream.EmitRecord(Code, *Record, Abbrev);
-    return Writer->Stream.GetCurrentBitNo();
-  }
-
-  /// \brief Add the given statement or expression to the queue of
-  /// statements to emit.
-  ///
-  /// This routine should be used when emitting types and declarations
-  /// that have expressions as part of their formulation. Once the
-  /// type or declaration has been written, Emit() will write
-  /// the corresponding statements just after the record.
-  void AddStmt(Stmt *S) {
-    StmtsToEmit.push_back(S);
-  }
-
-  /// \brief Add a definition for the given function to the queue of statements
-  /// to emit.
-  void AddFunctionDefinition(const FunctionDecl *FD);
 
   /// \brief Emit a source location.
   void AddSourceLocation(SourceLocation Loc) {
@@ -911,21 +958,26 @@ public:
   }
 
   /// \brief Emits a reference to a declarator info.
-  void AddTypeSourceInfo(TypeSourceInfo *TInfo);
-
-  /// \brief Emits a type with source-location information.
-  void AddTypeLoc(TypeLoc TL);
+  void AddTypeSourceInfo(TypeSourceInfo *TInfo) {
+    return Writer->AddTypeSourceInfo(TInfo, *Record);
+  }
 
   /// \brief Emits a template argument location info.
   void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
-                                  const TemplateArgumentLocInfo &Arg);
+                                  const TemplateArgumentLocInfo &Arg) {
+    return Writer->AddTemplateArgumentLocInfo(Kind, Arg, *Record);
+  }
 
   /// \brief Emits a template argument location.
-  void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
+  void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
+    return Writer->AddTemplateArgumentLoc(Arg, *Record);
+  }
 
   /// \brief Emits an AST template argument list info.
   void AddASTTemplateArgumentListInfo(
-      const ASTTemplateArgumentListInfo *ASTTemplArgList);
+      const ASTTemplateArgumentListInfo *ASTTemplArgList) {
+    return Writer->AddASTTemplateArgumentListInfo(ASTTemplArgList, *Record);
+  }
 
   /// \brief Emit a reference to a declaration.
   void AddDeclRef(const Decl *D) {
@@ -937,10 +989,17 @@ public:
   }
 
   void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
-                             DeclarationName Name);
-  void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
+                             DeclarationName Name) {
+    return Writer->AddDeclarationNameLoc(DNLoc, Name, *Record);
+  }
 
-  void AddQualifierInfo(const QualifierInfo &Info);
+  void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo) {
+    return Writer->AddDeclarationNameInfo(NameInfo, *Record);
+  }
+
+  void AddQualifierInfo(const QualifierInfo &Info) {
+    return Writer->AddQualifierInfo(Info, *Record);
+  }
 
   /// \brief Emit a nested name specifier.
   void AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
@@ -948,13 +1007,19 @@ public:
   }
 
   /// \brief Emit a nested name specifier with source-location information.
-  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
+  void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
+    return Writer->AddNestedNameSpecifierLoc(NNS, *Record);
+  }
 
   /// \brief Emit a template name.
-  void AddTemplateName(TemplateName Name);
+  void AddTemplateName(TemplateName Name) {
+    return Writer->AddTemplateName(Name, *Record);
+  }
 
   /// \brief Emit a template argument.
-  void AddTemplateArgument(const TemplateArgument &Arg);
+  void AddTemplateArgument(const TemplateArgument &Arg) {
+    return Writer->AddTemplateArgument(Arg, *Record);
+  }
 
   /// \brief Emit a template parameter list.
   void AddTemplateParameterList(const TemplateParameterList *TemplateParams) {
@@ -962,7 +1027,9 @@ public:
   }
 
   /// \brief Emit a template argument list.
-  void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
+  void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs) {
+    return Writer->AddTemplateArgumentList(TemplateArgs, *Record);
+  }
 
   /// \brief Emit a UnresolvedSet structure.
   void AddUnresolvedSet(const ASTUnresolvedSet &Set) {
@@ -970,7 +1037,9 @@ public:
   }
 
   /// \brief Emit a C++ base specifier.
-  void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
+  void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
+    return Writer->AddCXXBaseSpecifier(Base, *Record);
+  }
 
   /// \brief Emit the ID for a CXXCtorInitializer array and register the array
   /// for later serialization.
@@ -980,9 +1049,14 @@ public:
 
   /// \brief Emit a CXXCtorInitializer array.
   void AddCXXCtorInitializers(const CXXCtorInitializer *const *CtorInitializers,
-                              unsigned NumCtorInitializers);
+                              unsigned NumCtorInitializers) {
+    return Writer->AddCXXCtorInitializers(CtorInitializers, NumCtorInitializers,
+                                          *Record);
+  }
 
-  void AddCXXDefinitionData(const CXXRecordDecl *D);
+  void AddCXXDefinitionData(const CXXRecordDecl *D) {
+    return Writer->AddCXXDefinitionData(D, *Record);
+  }
 
   /// \brief Emit a string.
   void AddString(StringRef Str) {
@@ -1000,7 +1074,9 @@ public:
   }
 
   /// \brief Emit a list of attributes.
-  void AddAttributes(ArrayRef<const Attr*> Attrs);
+  void AddAttributes(ArrayRef<const Attr*> Attrs) {
+    return Writer->AddAttributes(Attrs, *Record);
+  }
 };
 
 /// \brief AST and semantic-analysis consumer that generates a

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=265526&r1=265525&r2=265526&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Wed Apr  6 05:01:46 2016
@@ -82,42 +82,19 @@ static StringRef bytes(const SmallVector
 // Type serialization
 //===----------------------------------------------------------------------===//
 
-namespace clang {
+namespace {
   class ASTTypeWriter {
     ASTWriter &Writer;
     ASTRecordWriter Record;
 
+  public:
     /// \brief Type code that corresponds to the record generated.
     TypeCode Code;
     /// \brief Abbreviation to use for the record, if any.
     unsigned AbbrevToUse;
 
-  public:
     ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
-      : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
-
-    uint64_t Emit() {
-      return Record.Emit(Code, AbbrevToUse);
-    }
-
-    void Visit(QualType T) {
-      if (T.hasLocalNonFastQualifiers()) {
-        Qualifiers Qs = T.getLocalQualifiers();
-        Record.AddTypeRef(T.getLocalUnqualifiedType());
-        Record.push_back(Qs.getAsOpaqueValue());
-        Code = TYPE_EXT_QUAL;
-        AbbrevToUse = Writer.TypeExtQualAbbrev;
-      } else {
-        switch (T->getTypeClass()) {
-          // For all of the concrete, non-dependent types, call the
-          // appropriate visitor function.
-#define TYPE(Class, Base) \
-        case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
-#define ABSTRACT_TYPE(Class, Base)
-#include "clang/AST/TypeNodes.def"
-        }
-      }
-    }
+      : Writer(Writer), Record(Writer, Record), Code(TYPE_EXT_QUAL) { }
 
     void VisitArrayType(const ArrayType *T);
     void VisitFunctionType(const FunctionType *T);
@@ -127,7 +104,7 @@ namespace clang {
 #define ABSTRACT_TYPE(Class, Base)
 #include "clang/AST/TypeNodes.def"
   };
-} // end namespace clang
+} // end anonymous namespace
 
 void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
   llvm_unreachable("Built-in types are never serialized");
@@ -197,7 +174,7 @@ void ASTTypeWriter::VisitVariableArrayTy
   VisitArrayType(T);
   Record.AddSourceLocation(T->getLBracketLoc());
   Record.AddSourceLocation(T->getRBracketLoc());
-  Record.AddStmt(T->getSizeExpr());
+  Writer.AddStmt(T->getSizeExpr());
   Code = TYPE_VARIABLE_ARRAY;
 }
 
@@ -232,15 +209,15 @@ void ASTTypeWriter::VisitFunctionNoProto
   Code = TYPE_FUNCTION_NO_PROTO;
 }
 
-static void addExceptionSpec(const FunctionProtoType *T,
-                             ASTRecordWriter &Record) {
+static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
+                             ASTRecordWriter Record) {
   Record.push_back(T->getExceptionSpecType());
   if (T->getExceptionSpecType() == EST_Dynamic) {
     Record.push_back(T->getNumExceptions());
     for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
       Record.AddTypeRef(T->getExceptionType(I));
   } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
-    Record.AddStmt(T->getNoexceptExpr());
+    Writer.AddStmt(T->getNoexceptExpr());
   } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
     Record.AddDeclRef(T->getExceptionSpecDecl());
     Record.AddDeclRef(T->getExceptionSpecTemplate());
@@ -256,7 +233,7 @@ void ASTTypeWriter::VisitFunctionProtoTy
   Record.push_back(T->hasTrailingReturn());
   Record.push_back(T->getTypeQuals());
   Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
-  addExceptionSpec(T, Record);
+  addExceptionSpec(Writer, T, Record);
 
   Record.push_back(T->getNumParams());
   for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
@@ -288,7 +265,7 @@ void ASTTypeWriter::VisitTypedefType(con
 }
 
 void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
-  Record.AddStmt(T->getUnderlyingExpr());
+  Writer.AddStmt(T->getUnderlyingExpr());
   Code = TYPE_TYPEOF_EXPR;
 }
 
@@ -299,7 +276,7 @@ void ASTTypeWriter::VisitTypeOfType(cons
 
 void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
   Record.AddTypeRef(T->getUnderlyingType());
-  Record.AddStmt(T->getUnderlyingExpr());
+  Writer.AddStmt(T->getUnderlyingExpr());
   Code = TYPE_DECLTYPE;
 }
 
@@ -376,7 +353,7 @@ ASTTypeWriter::VisitTemplateSpecializati
 void
 ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
   VisitArrayType(T);
-  Record.AddStmt(T->getSizeExpr());
+  Writer.AddStmt(T->getSizeExpr());
   Record.AddSourceRange(T->getBracketsRange());
   Code = TYPE_DEPENDENT_SIZED_ARRAY;
 }
@@ -484,11 +461,12 @@ ASTTypeWriter::VisitPipeType(const PipeT
 namespace {
 
 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
-  ASTRecordWriter &Record;
+  ASTWriter &Writer;
+  ASTRecordWriter Record;
 
 public:
-  TypeLocWriter(ASTRecordWriter &Record)
-    : Record(Record) { }
+  TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
+    : Writer(Writer), Record(Writer, Record) { }
 
 #define ABSTRACT_TYPELOC(CLASS, PARENT)
 #define TYPELOC(CLASS, PARENT) \
@@ -543,7 +521,7 @@ void TypeLocWriter::VisitArrayTypeLoc(Ar
   Record.AddSourceLocation(TL.getRBracketLoc());
   Record.push_back(TL.getSizeExpr() ? 1 : 0);
   if (TL.getSizeExpr())
-    Record.AddStmt(TL.getSizeExpr());
+    Writer.AddStmt(TL.getSizeExpr());
 }
 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
   VisitArrayTypeLoc(TL);
@@ -627,7 +605,7 @@ void TypeLocWriter::VisitAttributedTypeL
   if (TL.hasAttrExprOperand()) {
     Expr *operand = TL.getAttrExprOperand();
     Record.push_back(operand ? 1 : 0);
-    if (operand) Record.AddStmt(operand);
+    if (operand) Writer.AddStmt(operand);
   } else if (TL.hasAttrEnumOperand()) {
     Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
   }
@@ -2765,23 +2743,43 @@ void ASTWriter::WriteType(QualType T) {
 
   assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
 
-  RecordData Record;
-
-  // Emit the type's representation.
-  ASTTypeWriter W(*this, Record);
-  W.Visit(T);
-  uint64_t Offset = W.Emit();
-
   // Record the offset for this type.
   unsigned Index = Idx.getIndex() - FirstTypeID;
   if (TypeOffsets.size() == Index)
-    TypeOffsets.push_back(Offset);
+    TypeOffsets.push_back(Stream.GetCurrentBitNo());
   else if (TypeOffsets.size() < Index) {
     TypeOffsets.resize(Index + 1);
-    TypeOffsets[Index] = Offset;
+    TypeOffsets[Index] = Stream.GetCurrentBitNo();
+  }
+
+  RecordData Record;
+
+  // Emit the type's representation.
+  ASTTypeWriter W(*this, Record);
+  W.AbbrevToUse = 0;
+
+  if (T.hasLocalNonFastQualifiers()) {
+    Qualifiers Qs = T.getLocalQualifiers();
+    AddTypeRef(T.getLocalUnqualifiedType(), Record);
+    Record.push_back(Qs.getAsOpaqueValue());
+    W.Code = TYPE_EXT_QUAL;
+    W.AbbrevToUse = TypeExtQualAbbrev;
   } else {
-    llvm_unreachable("Types emitted in wrong order");
+    switch (T->getTypeClass()) {
+      // For all of the concrete, non-dependent types, call the
+      // appropriate visitor function.
+#define TYPE(Class, Base) \
+    case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
+#define ABSTRACT_TYPE(Class, Base)
+#include "clang/AST/TypeNodes.def"
+    }
   }
+
+  // Emit the serialized record.
+  Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
+
+  // Flush any expressions that were written as part of this type.
+  FlushStmts();
 }
 
 //===----------------------------------------------------------------------===//
@@ -3991,12 +3989,12 @@ void ASTWriter::WriteModuleFileExtension
 //===----------------------------------------------------------------------===//
 
 /// \brief Emit the list of attributes to the specified record.
-void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
-  auto &Record = *this;
+void ASTWriter::AddAttributes(ArrayRef<const Attr *> Attrs,
+                              RecordDataImpl &Record) {
   Record.push_back(Attrs.size());
   for (const auto *A : Attrs) {
     Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
-    Record.AddSourceRange(A->getRange());
+    AddSourceRange(A->getRange(), Record);
 
 #include "clang/Serialization/AttrPCHWrite.inc"
 
@@ -4101,7 +4099,7 @@ ASTWriter::ASTWriter(
       NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
       NextSubmoduleID(FirstSubmoduleID),
       FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
-      NumStatements(0), NumMacros(0),
+      CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
       NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
       NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
       TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
@@ -4685,8 +4683,8 @@ void ASTWriter::WriteDeclUpdatesBlocks(R
         break;
 
       case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
-        Record.AddStmt(const_cast<Expr *>(
-            cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
+        AddStmt(const_cast<Expr*>(
+                  cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
         break;
 
       case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
@@ -4740,6 +4738,7 @@ void ASTWriter::WriteDeclUpdatesBlocks(R
 
       case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
         addExceptionSpec(
+            *this,
             cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
             Record);
         break;
@@ -4776,7 +4775,7 @@ void ASTWriter::WriteDeclUpdatesBlocks(R
       Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
       Record.push_back(Def->isInlined());
       Record.AddSourceLocation(Def->getInnerLocStart());
-      Record.AddFunctionDefinition(Def);
+      AddFunctionDefinition(Def, Record.getRecordData());
     }
 
     OffsetsRecord.push_back(GetDeclRef(D));
@@ -4898,23 +4897,24 @@ void ASTWriter::AddCXXBaseSpecifiersRef(
   Record.push_back(NextCXXBaseSpecifiersID++);
 }
 
-void ASTRecordWriter::AddTemplateArgumentLocInfo(
-    TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
+void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
+                                           const TemplateArgumentLocInfo &Arg,
+                                           RecordDataImpl &Record) {
   switch (Kind) {
   case TemplateArgument::Expression:
     AddStmt(Arg.getAsExpr());
     break;
   case TemplateArgument::Type:
-    AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
+    AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
     break;
   case TemplateArgument::Template:
-    AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
-    AddSourceLocation(Arg.getTemplateNameLoc());
+    AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
+    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
     break;
   case TemplateArgument::TemplateExpansion:
-    AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
-    AddSourceLocation(Arg.getTemplateNameLoc());
-    AddSourceLocation(Arg.getTemplateEllipsisLoc());
+    AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
+    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
+    AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
     break;
   case TemplateArgument::Null:
   case TemplateArgument::Integral:
@@ -4926,32 +4926,35 @@ void ASTRecordWriter::AddTemplateArgumen
   }
 }
 
-void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
-  AddTemplateArgument(Arg.getArgument());
+void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
+                                       RecordDataImpl &Record) {
+  AddTemplateArgument(Arg.getArgument(), Record);
 
   if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
     bool InfoHasSameExpr
       = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
-    Record->push_back(InfoHasSameExpr);
+    Record.push_back(InfoHasSameExpr);
     if (InfoHasSameExpr)
       return; // Avoid storing the same expr twice.
   }
-  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
+  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
+                             Record);
 }
 
-void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
+void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, 
+                                  RecordDataImpl &Record) {
   if (!TInfo) {
-    AddTypeRef(QualType());
+    AddTypeRef(QualType(), Record);
     return;
   }
 
-  AddTypeLoc(TInfo->getTypeLoc());
+  AddTypeLoc(TInfo->getTypeLoc(), Record);
 }
 
-void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
-  AddTypeRef(TL.getType());
+void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
+  AddTypeRef(TL.getType(), Record);
 
-  TypeLocWriter TLW(*this);
+  TypeLocWriter TLW(*this, Record);
   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
     TLW.Visit(TL);
 }
@@ -5141,25 +5144,28 @@ unsigned ASTWriter::getAnonymousDeclarat
   return It->second;
 }
 
-void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
-                                            DeclarationName Name) {
+void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
+                                     DeclarationName Name, RecordDataImpl &Record) {
   switch (Name.getNameKind()) {
   case DeclarationName::CXXConstructorName:
   case DeclarationName::CXXDestructorName:
   case DeclarationName::CXXConversionFunctionName:
-    AddTypeSourceInfo(DNLoc.NamedType.TInfo);
+    AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
     break;
 
   case DeclarationName::CXXOperatorName:
-    AddSourceLocation(SourceLocation::getFromRawEncoding(
-        DNLoc.CXXOperatorName.BeginOpNameLoc));
     AddSourceLocation(
-        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
+       SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
+       Record);
+    AddSourceLocation(
+        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
+        Record);
     break;
 
   case DeclarationName::CXXLiteralOperatorName:
-    AddSourceLocation(SourceLocation::getFromRawEncoding(
-        DNLoc.CXXLiteralOperatorName.OpNameLoc));
+    AddSourceLocation(
+     SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
+     Record);
     break;
 
   case DeclarationName::Identifier:
@@ -5171,18 +5177,19 @@ void ASTRecordWriter::AddDeclarationName
   }
 }
 
-void ASTRecordWriter::AddDeclarationNameInfo(
-    const DeclarationNameInfo &NameInfo) {
-  AddDeclarationName(NameInfo.getName());
-  AddSourceLocation(NameInfo.getLoc());
-  AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
+void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
+                                       RecordDataImpl &Record) {
+  AddDeclarationName(NameInfo.getName(), Record);
+  AddSourceLocation(NameInfo.getLoc(), Record);
+  AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
 }
 
-void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
-  AddNestedNameSpecifierLoc(Info.QualifierLoc);
-  Record->push_back(Info.NumTemplParamLists);
+void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
+                                 RecordDataImpl &Record) {
+  AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
+  Record.push_back(Info.NumTemplParamLists);
   for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
-    AddTemplateParameterList(Info.TemplParamLists[i]);
+    AddTemplateParameterList(Info.TemplParamLists[i], Record);
 }
 
 void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
@@ -5232,7 +5239,8 @@ void ASTWriter::AddNestedNameSpecifier(N
   }
 }
 
-void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
+void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
+                                          RecordDataImpl &Record) {
   // Nested name specifiers usually aren't too long. I think that 8 would
   // typically accommodate the vast majority.
   SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
@@ -5244,136 +5252,137 @@ void ASTRecordWriter::AddNestedNameSpeci
     NNS = NNS.getPrefix();
   }
 
-  Record->push_back(NestedNames.size());
+  Record.push_back(NestedNames.size());
   while(!NestedNames.empty()) {
     NNS = NestedNames.pop_back_val();
     NestedNameSpecifier::SpecifierKind Kind
       = NNS.getNestedNameSpecifier()->getKind();
-    Record->push_back(Kind);
+    Record.push_back(Kind);
     switch (Kind) {
     case NestedNameSpecifier::Identifier:
-      AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
-      AddSourceRange(NNS.getLocalSourceRange());
+      AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
+      AddSourceRange(NNS.getLocalSourceRange(), Record);
       break;
 
     case NestedNameSpecifier::Namespace:
-      AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
-      AddSourceRange(NNS.getLocalSourceRange());
+      AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
+      AddSourceRange(NNS.getLocalSourceRange(), Record);
       break;
 
     case NestedNameSpecifier::NamespaceAlias:
-      AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
-      AddSourceRange(NNS.getLocalSourceRange());
+      AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
+      AddSourceRange(NNS.getLocalSourceRange(), Record);
       break;
 
     case NestedNameSpecifier::TypeSpec:
     case NestedNameSpecifier::TypeSpecWithTemplate:
-      Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
-      AddTypeLoc(NNS.getTypeLoc());
-      AddSourceLocation(NNS.getLocalSourceRange().getEnd());
+      Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
+      AddTypeLoc(NNS.getTypeLoc(), Record);
+      AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
       break;
 
     case NestedNameSpecifier::Global:
-      AddSourceLocation(NNS.getLocalSourceRange().getEnd());
+      AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
       break;
 
     case NestedNameSpecifier::Super:
-      AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
-      AddSourceRange(NNS.getLocalSourceRange());
+      AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
+      AddSourceRange(NNS.getLocalSourceRange(), Record);
       break;
     }
   }
 }
 
-void ASTRecordWriter::AddTemplateName(TemplateName Name) {
+void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
   TemplateName::NameKind Kind = Name.getKind();
-  Record->push_back(Kind);
+  Record.push_back(Kind);
   switch (Kind) {
   case TemplateName::Template:
-    AddDeclRef(Name.getAsTemplateDecl());
+    AddDeclRef(Name.getAsTemplateDecl(), Record);
     break;
 
   case TemplateName::OverloadedTemplate: {
     OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
-    Record->push_back(OvT->size());
+    Record.push_back(OvT->size());
     for (const auto &I : *OvT)
-      AddDeclRef(I);
+      AddDeclRef(I, Record);
     break;
   }
 
   case TemplateName::QualifiedTemplate: {
     QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
-    AddNestedNameSpecifier(QualT->getQualifier());
-    Record->push_back(QualT->hasTemplateKeyword());
-    AddDeclRef(QualT->getTemplateDecl());
+    AddNestedNameSpecifier(QualT->getQualifier(), Record);
+    Record.push_back(QualT->hasTemplateKeyword());
+    AddDeclRef(QualT->getTemplateDecl(), Record);
     break;
   }
 
   case TemplateName::DependentTemplate: {
     DependentTemplateName *DepT = Name.getAsDependentTemplateName();
-    AddNestedNameSpecifier(DepT->getQualifier());
-    Record->push_back(DepT->isIdentifier());
+    AddNestedNameSpecifier(DepT->getQualifier(), Record);
+    Record.push_back(DepT->isIdentifier());
     if (DepT->isIdentifier())
-      AddIdentifierRef(DepT->getIdentifier());
+      AddIdentifierRef(DepT->getIdentifier(), Record);
     else
-      Record->push_back(DepT->getOperator());
+      Record.push_back(DepT->getOperator());
     break;
   }
 
   case TemplateName::SubstTemplateTemplateParm: {
     SubstTemplateTemplateParmStorage *subst
       = Name.getAsSubstTemplateTemplateParm();
-    AddDeclRef(subst->getParameter());
-    AddTemplateName(subst->getReplacement());
+    AddDeclRef(subst->getParameter(), Record);
+    AddTemplateName(subst->getReplacement(), Record);
     break;
   }
       
   case TemplateName::SubstTemplateTemplateParmPack: {
     SubstTemplateTemplateParmPackStorage *SubstPack
       = Name.getAsSubstTemplateTemplateParmPack();
-    AddDeclRef(SubstPack->getParameterPack());
-    AddTemplateArgument(SubstPack->getArgumentPack());
+    AddDeclRef(SubstPack->getParameterPack(), Record);
+    AddTemplateArgument(SubstPack->getArgumentPack(), Record);
     break;
   }
   }
 }
 
-void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
-  Record->push_back(Arg.getKind());
+void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
+                                    RecordDataImpl &Record) {
+  Record.push_back(Arg.getKind());
   switch (Arg.getKind()) {
   case TemplateArgument::Null:
     break;
   case TemplateArgument::Type:
-    AddTypeRef(Arg.getAsType());
+    AddTypeRef(Arg.getAsType(), Record);
     break;
   case TemplateArgument::Declaration:
-    AddDeclRef(Arg.getAsDecl());
-    AddTypeRef(Arg.getParamTypeForDecl());
+    AddDeclRef(Arg.getAsDecl(), Record);
+    AddTypeRef(Arg.getParamTypeForDecl(), Record);
     break;
   case TemplateArgument::NullPtr:
-    AddTypeRef(Arg.getNullPtrType());
+    AddTypeRef(Arg.getNullPtrType(), Record);
     break;
   case TemplateArgument::Integral:
-    AddAPSInt(Arg.getAsIntegral());
-    AddTypeRef(Arg.getIntegralType());
+    AddAPSInt(Arg.getAsIntegral(), Record);
+    AddTypeRef(Arg.getIntegralType(), Record);
     break;
   case TemplateArgument::Template:
-    AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
+    AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
     break;
   case TemplateArgument::TemplateExpansion:
-    AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
+    AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
     if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
-      Record->push_back(*NumExpansions + 1);
+      Record.push_back(*NumExpansions + 1);
     else
-      Record->push_back(0);
+      Record.push_back(0);
     break;
   case TemplateArgument::Expression:
     AddStmt(Arg.getAsExpr());
     break;
   case TemplateArgument::Pack:
-    Record->push_back(Arg.pack_size());
+    Record.push_back(Arg.pack_size());
     for (const auto &P : Arg.pack_elements())
-      AddTemplateArgument(P);
+      AddTemplateArgument(P, Record);
     break;
   }
 }
@@ -5391,23 +5400,25 @@ ASTWriter::AddTemplateParameterList(cons
 }
 
 /// \brief Emit a template argument list.
-void ASTRecordWriter::AddTemplateArgumentList(
-    const TemplateArgumentList *TemplateArgs) {
+void
+ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
+                                   RecordDataImpl &Record) {
   assert(TemplateArgs && "No TemplateArgs!");
-  Record->push_back(TemplateArgs->size());
+  Record.push_back(TemplateArgs->size());
   for (int i=0, e = TemplateArgs->size(); i != e; ++i)
-    AddTemplateArgument(TemplateArgs->get(i));
+    AddTemplateArgument(TemplateArgs->get(i), Record);
 }
 
-void ASTRecordWriter::AddASTTemplateArgumentListInfo(
-    const ASTTemplateArgumentListInfo *ASTTemplArgList) {
+void
+ASTWriter::AddASTTemplateArgumentListInfo
+(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
   assert(ASTTemplArgList && "No ASTTemplArgList!");
-  AddSourceLocation(ASTTemplArgList->LAngleLoc);
-  AddSourceLocation(ASTTemplArgList->RAngleLoc);
-  Record->push_back(ASTTemplArgList->NumTemplateArgs);
+  AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
+  AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
+  Record.push_back(ASTTemplArgList->NumTemplateArgs);
   const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
   for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
-    AddTemplateArgumentLoc(TemplArgs[i]);
+    AddTemplateArgumentLoc(TemplArgs[i], Record);
 }
 
 void
@@ -5420,16 +5431,17 @@ ASTWriter::AddUnresolvedSet(const ASTUnr
   }
 }
 
-// FIXME: Move this out of the main ASTRecordWriter interface.
-void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
-  Record->push_back(Base.isVirtual());
-  Record->push_back(Base.isBaseOfClass());
-  Record->push_back(Base.getAccessSpecifierAsWritten());
-  Record->push_back(Base.getInheritConstructors());
-  AddTypeSourceInfo(Base.getTypeSourceInfo());
-  AddSourceRange(Base.getSourceRange());
+void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
+                                    RecordDataImpl &Record) {
+  Record.push_back(Base.isVirtual());
+  Record.push_back(Base.isBaseOfClass());
+  Record.push_back(Base.getAccessSpecifierAsWritten());
+  Record.push_back(Base.getInheritConstructors());
+  AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
+  AddSourceRange(Base.getSourceRange(), Record);
   AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() 
-                                          : SourceLocation());
+                                          : SourceLocation(),
+                    Record);
 }
 
 void ASTWriter::FlushCXXBaseSpecifiers() {
@@ -5437,30 +5449,26 @@ void ASTWriter::FlushCXXBaseSpecifiers()
   unsigned N = CXXBaseSpecifiersToWrite.size();
   for (unsigned I = 0; I != N; ++I) {
     Record.clear();
-
-    const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
-                        *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
-
-    // Write the base specifier set.
-    ASTRecordWriter Writer(*this, Record);
-    Writer.push_back(BEnd - B);
-    for (; B != BEnd; ++B)
-      Writer.AddCXXBaseSpecifier(*B);
-    uint64_t Offset = Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
     
     // Record the offset of this base-specifier set.
-    //
-    // FIXME: We don't need an indirect lookup table for these; instead, write
-    // the base specifier record prior to the decl record and store its offset
-    // from the decl record rather than its ID.
     unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
     if (Index == CXXBaseSpecifiersOffsets.size())
-      CXXBaseSpecifiersOffsets.push_back(Offset);
+      CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
     else {
       if (Index > CXXBaseSpecifiersOffsets.size())
         CXXBaseSpecifiersOffsets.resize(Index + 1);
-      CXXBaseSpecifiersOffsets[Index] = Offset;
+      CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
     }
+
+    const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
+                        *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
+    Record.push_back(BEnd - B);
+    for (; B != BEnd; ++B)
+      AddCXXBaseSpecifier(*B, Record);
+    Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
+    
+    // Flush any expressions that were written as part of the base specifiers.
+    FlushStmts();
   }
 
   assert(N == CXXBaseSpecifiersToWrite.size() &&
@@ -5468,40 +5476,40 @@ void ASTWriter::FlushCXXBaseSpecifiers()
   CXXBaseSpecifiersToWrite.clear();
 }
 
-// FIXME: Move this out of the main ASTRecordWriter interface.
-void ASTRecordWriter::AddCXXCtorInitializers(
-    const CXXCtorInitializer *const *CtorInitializers,
-    unsigned NumCtorInitializers) {
-  Record->push_back(NumCtorInitializers);
+void ASTWriter::AddCXXCtorInitializers(
+                             const CXXCtorInitializer * const *CtorInitializers,
+                             unsigned NumCtorInitializers,
+                             RecordDataImpl &Record) {
+  Record.push_back(NumCtorInitializers);
   for (unsigned i=0; i != NumCtorInitializers; ++i) {
     const CXXCtorInitializer *Init = CtorInitializers[i];
 
     if (Init->isBaseInitializer()) {
-      Record->push_back(CTOR_INITIALIZER_BASE);
-      AddTypeSourceInfo(Init->getTypeSourceInfo());
-      Record->push_back(Init->isBaseVirtual());
+      Record.push_back(CTOR_INITIALIZER_BASE);
+      AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
+      Record.push_back(Init->isBaseVirtual());
     } else if (Init->isDelegatingInitializer()) {
-      Record->push_back(CTOR_INITIALIZER_DELEGATING);
-      AddTypeSourceInfo(Init->getTypeSourceInfo());
+      Record.push_back(CTOR_INITIALIZER_DELEGATING);
+      AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
     } else if (Init->isMemberInitializer()){
-      Record->push_back(CTOR_INITIALIZER_MEMBER);
-      AddDeclRef(Init->getMember());
+      Record.push_back(CTOR_INITIALIZER_MEMBER);
+      AddDeclRef(Init->getMember(), Record);
     } else {
-      Record->push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
-      AddDeclRef(Init->getIndirectMember());
+      Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
+      AddDeclRef(Init->getIndirectMember(), Record);
     }
 
-    AddSourceLocation(Init->getMemberLocation());
+    AddSourceLocation(Init->getMemberLocation(), Record);
     AddStmt(Init->getInit());
-    AddSourceLocation(Init->getLParenLoc());
-    AddSourceLocation(Init->getRParenLoc());
-    Record->push_back(Init->isWritten());
+    AddSourceLocation(Init->getLParenLoc(), Record);
+    AddSourceLocation(Init->getRParenLoc(), Record);
+    Record.push_back(Init->isWritten());
     if (Init->isWritten()) {
-      Record->push_back(Init->getSourceOrder());
+      Record.push_back(Init->getSourceOrder());
     } else {
-      Record->push_back(Init->getNumArrayIndices());
+      Record.push_back(Init->getNumArrayIndices());
       for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
-        AddDeclRef(Init->getArrayIndex(i));
+        AddDeclRef(Init->getArrayIndex(i), Record);
     }
   }
 }
@@ -5514,19 +5522,21 @@ void ASTWriter::FlushCXXCtorInitializers
   for (auto &Init : CXXCtorInitializersToWrite) {
     Record.clear();
 
-    ASTRecordWriter Writer(*this, Record);
-    Writer.AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size());
-    uint64_t Offset = Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
-
     // Record the offset of this mem-initializer list.
     unsigned Index = Init.ID - 1;
     if (Index == CXXCtorInitializersOffsets.size())
-      CXXCtorInitializersOffsets.push_back(Offset);
+      CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
     else {
       if (Index > CXXCtorInitializersOffsets.size())
         CXXCtorInitializersOffsets.resize(Index + 1);
-      CXXCtorInitializersOffsets[Index] = Offset;
+      CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
     }
+
+    AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
+    Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
+
+    // Flush any expressions that were written as part of the initializers.
+    FlushStmts();
   }
 
   assert(N == CXXCtorInitializersToWrite.size() &&
@@ -5534,81 +5544,82 @@ void ASTWriter::FlushCXXCtorInitializers
   CXXCtorInitializersToWrite.clear();
 }
 
-void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
+void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
   auto &Data = D->data();
-  Record->push_back(Data.IsLambda);
-  Record->push_back(Data.UserDeclaredConstructor);
-  Record->push_back(Data.UserDeclaredSpecialMembers);
-  Record->push_back(Data.Aggregate);
-  Record->push_back(Data.PlainOldData);
-  Record->push_back(Data.Empty);
-  Record->push_back(Data.Polymorphic);
-  Record->push_back(Data.Abstract);
-  Record->push_back(Data.IsStandardLayout);
-  Record->push_back(Data.HasNoNonEmptyBases);
-  Record->push_back(Data.HasPrivateFields);
-  Record->push_back(Data.HasProtectedFields);
-  Record->push_back(Data.HasPublicFields);
-  Record->push_back(Data.HasMutableFields);
-  Record->push_back(Data.HasVariantMembers);
-  Record->push_back(Data.HasOnlyCMembers);
-  Record->push_back(Data.HasInClassInitializer);
-  Record->push_back(Data.HasUninitializedReferenceMember);
-  Record->push_back(Data.HasUninitializedFields);
-  Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
-  Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
-  Record->push_back(Data.NeedOverloadResolutionForDestructor);
-  Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
-  Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
-  Record->push_back(Data.DefaultedDestructorIsDeleted);
-  Record->push_back(Data.HasTrivialSpecialMembers);
-  Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
-  Record->push_back(Data.HasIrrelevantDestructor);
-  Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
-  Record->push_back(Data.HasDefaultedDefaultConstructor);
-  Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
-  Record->push_back(Data.HasConstexprDefaultConstructor);
-  Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
-  Record->push_back(Data.ComputedVisibleConversions);
-  Record->push_back(Data.UserProvidedDefaultConstructor);
-  Record->push_back(Data.DeclaredSpecialMembers);
-  Record->push_back(Data.ImplicitCopyConstructorHasConstParam);
-  Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
-  Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
-  Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
+  Record.push_back(Data.IsLambda);
+  Record.push_back(Data.UserDeclaredConstructor);
+  Record.push_back(Data.UserDeclaredSpecialMembers);
+  Record.push_back(Data.Aggregate);
+  Record.push_back(Data.PlainOldData);
+  Record.push_back(Data.Empty);
+  Record.push_back(Data.Polymorphic);
+  Record.push_back(Data.Abstract);
+  Record.push_back(Data.IsStandardLayout);
+  Record.push_back(Data.HasNoNonEmptyBases);
+  Record.push_back(Data.HasPrivateFields);
+  Record.push_back(Data.HasProtectedFields);
+  Record.push_back(Data.HasPublicFields);
+  Record.push_back(Data.HasMutableFields);
+  Record.push_back(Data.HasVariantMembers);
+  Record.push_back(Data.HasOnlyCMembers);
+  Record.push_back(Data.HasInClassInitializer);
+  Record.push_back(Data.HasUninitializedReferenceMember);
+  Record.push_back(Data.HasUninitializedFields);
+  Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
+  Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
+  Record.push_back(Data.NeedOverloadResolutionForDestructor);
+  Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
+  Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
+  Record.push_back(Data.DefaultedDestructorIsDeleted);
+  Record.push_back(Data.HasTrivialSpecialMembers);
+  Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
+  Record.push_back(Data.HasIrrelevantDestructor);
+  Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
+  Record.push_back(Data.HasDefaultedDefaultConstructor);
+  Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
+  Record.push_back(Data.HasConstexprDefaultConstructor);
+  Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
+  Record.push_back(Data.ComputedVisibleConversions);
+  Record.push_back(Data.UserProvidedDefaultConstructor);
+  Record.push_back(Data.DeclaredSpecialMembers);
+  Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
+  Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
+  Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
+  Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
   // IsLambda bit is already saved.
 
-  Record->push_back(Data.NumBases);
+  Record.push_back(Data.NumBases);
   if (Data.NumBases > 0)
-    AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases);
+    AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, 
+                            Record);
   
   // FIXME: Make VBases lazily computed when needed to avoid storing them.
-  Record->push_back(Data.NumVBases);
+  Record.push_back(Data.NumVBases);
   if (Data.NumVBases > 0)
-    AddCXXBaseSpecifiersRef(Data.getVBases(),
-                            Data.getVBases() + Data.NumVBases);
+    AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, 
+                            Record);
 
-  AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
-  AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
+  AddUnresolvedSet(Data.Conversions.get(*Context), Record);
+  AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
   // Data.Definition is the owning decl, no need to write it. 
-  AddDeclRef(D->getFirstFriend());
+  AddDeclRef(D->getFirstFriend(), Record);
   
   // Add lambda-specific data.
   if (Data.IsLambda) {
     auto &Lambda = D->getLambdaData();
-    Record->push_back(Lambda.Dependent);
-    Record->push_back(Lambda.IsGenericLambda);
-    Record->push_back(Lambda.CaptureDefault);
-    Record->push_back(Lambda.NumCaptures);
-    Record->push_back(Lambda.NumExplicitCaptures);
-    Record->push_back(Lambda.ManglingNumber);
-    AddDeclRef(Lambda.ContextDecl);
-    AddTypeSourceInfo(Lambda.MethodTyInfo);
+    Record.push_back(Lambda.Dependent);
+    Record.push_back(Lambda.IsGenericLambda);
+    Record.push_back(Lambda.CaptureDefault);
+    Record.push_back(Lambda.NumCaptures);
+    Record.push_back(Lambda.NumExplicitCaptures);
+    Record.push_back(Lambda.ManglingNumber);
+    AddDeclRef(Lambda.ContextDecl, Record);
+    AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
     for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
       const LambdaCapture &Capture = Lambda.Captures[I];
-      AddSourceLocation(Capture.getLocation());
-      Record->push_back(Capture.isImplicit());
-      Record->push_back(Capture.getCaptureKind());
+      AddSourceLocation(Capture.getLocation(), Record);
+      Record.push_back(Capture.isImplicit());
+      Record.push_back(Capture.getCaptureKind());
       switch (Capture.getCaptureKind()) {
       case LCK_StarThis:
       case LCK_This:
@@ -5618,9 +5629,10 @@ void ASTRecordWriter::AddCXXDefinitionDa
       case LCK_ByRef:
         VarDecl *Var =
             Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
-        AddDeclRef(Var);
+        AddDeclRef(Var, Record);
         AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
-                                                    : SourceLocation());
+                                                    : SourceLocation(),
+                          Record);
         break;
       }
     }

Modified: cfe/trunk/lib/Serialization/ASTWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterDecl.cpp?rev=265526&r1=265525&r2=265526&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterDecl.cpp Wed Apr  6 05:01:46 2016
@@ -171,6 +171,17 @@ namespace clang {
       Record.AddSourceLocation(typeParams->getRAngleLoc());
     }
 
+    void AddFunctionDefinition(const FunctionDecl *FD) {
+      assert(FD->doesThisDeclarationHaveABody());
+      if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
+        Record.push_back(CD->NumCtorInitializers);
+        if (CD->NumCtorInitializers)
+          Record.AddCXXCtorInitializersRef(
+              llvm::makeArrayRef(CD->init_begin(), CD->init_end()));
+      }
+      Writer.AddStmt(FD->getBody());
+    }
+
     /// Add to the record the first declaration from each module file that
     /// provides a declaration of D. The intent is to provide a sufficient
     /// set such that reloading this set will load all current redeclarations.
@@ -282,7 +293,7 @@ void ASTDeclWriter::Visit(Decl *D) {
   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     Record.push_back(FD->doesThisDeclarationHaveABody());
     if (FD->doesThisDeclarationHaveABody())
-      Record.AddFunctionDefinition(FD);
+      AddFunctionDefinition(FD);
   }
 }
 
@@ -497,7 +508,7 @@ void ASTDeclWriter::VisitEnumConstantDec
   VisitValueDecl(D);
   Record.push_back(D->getInitExpr()? 1 : 0);
   if (D->getInitExpr())
-    Record.AddStmt(D->getInitExpr());
+    Writer.AddStmt(D->getInitExpr());
   Record.AddAPSInt(D->getInitVal());
 
   Code = serialization::DECL_ENUM_CONSTANT;
@@ -618,7 +629,7 @@ void ASTDeclWriter::VisitObjCMethodDecl(
                       D->getSelfDecl() != nullptr || D->getCmdDecl() != nullptr;
   Record.push_back(HasBodyStuff);
   if (HasBodyStuff) {
-    Record.AddStmt(D->getBody());
+    Writer.AddStmt(D->getBody());
     Record.AddDeclRef(D->getSelfDecl());
     Record.AddDeclRef(D->getCmdDecl());
   }
@@ -835,8 +846,8 @@ void ASTDeclWriter::VisitObjCPropertyImp
   Record.AddDeclRef(D->getPropertyDecl());
   Record.AddDeclRef(D->getPropertyIvarDecl());
   Record.AddSourceLocation(D->getPropertyIvarDeclLoc());
-  Record.AddStmt(D->getGetterCXXConstructor());
-  Record.AddStmt(D->getSetterCXXAssignment());
+  Writer.AddStmt(D->getGetterCXXConstructor());
+  Writer.AddStmt(D->getSetterCXXAssignment());
   Code = serialization::DECL_OBJC_PROPERTY_IMPL;
 }
 
@@ -852,7 +863,7 @@ void ASTDeclWriter::VisitFieldDecl(Field
         QualType(static_cast<Type *>(D->InitStorage.getPointer()), 0));
   } else {
     Record.push_back(D->InitStorage.getInt() + 1);
-    Record.AddStmt(static_cast<Expr *>(D->InitStorage.getPointer()));
+    Writer.AddStmt(static_cast<Expr *>(D->InitStorage.getPointer()));
   }
   if (!D->getDeclName())
     Record.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D));
@@ -911,7 +922,7 @@ void ASTDeclWriter::VisitVarDecl(VarDecl
 
   if (D->getInit()) {
     Record.push_back(!D->isInitKnownICE() ? 1 : (D->isInitICE() ? 3 : 2));
-    Record.AddStmt(D->getInit());
+    Writer.AddStmt(D->getInit());
   } else {
     Record.push_back(0);
   }
@@ -973,7 +984,7 @@ void ASTDeclWriter::VisitParmVarDecl(Par
   Record.push_back(D->hasInheritedDefaultArg());
   Record.push_back(D->hasUninstantiatedDefaultArg());
   if (D->hasUninstantiatedDefaultArg())
-    Record.AddStmt(D->getUninstantiatedDefaultArg());
+    Writer.AddStmt(D->getUninstantiatedDefaultArg());
   Code = serialization::DECL_PARM_VAR;
 
   assert(!D->isARCPseudoStrong()); // can be true of ImplicitParamDecl
@@ -1012,7 +1023,7 @@ void ASTDeclWriter::VisitParmVarDecl(Par
 
 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
   VisitDecl(D);
-  Record.AddStmt(D->getAsmString());
+  Writer.AddStmt(D->getAsmString());
   Record.AddSourceLocation(D->getRParenLoc());
   Code = serialization::DECL_FILE_SCOPE_ASM;
 }
@@ -1024,7 +1035,7 @@ void ASTDeclWriter::VisitEmptyDecl(Empty
 
 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
   VisitDecl(D);
-  Record.AddStmt(D->getBody());
+  Writer.AddStmt(D->getBody());
   Record.AddTypeSourceInfo(D->getSignatureAsWritten());
   Record.push_back(D->param_size());
   for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
@@ -1044,7 +1055,7 @@ void ASTDeclWriter::VisitBlockDecl(Block
     if (capture.hasCopyExpr()) flags |= 4;
     Record.push_back(flags);
 
-    if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());
+    if (capture.hasCopyExpr()) Writer.AddStmt(capture.getCopyExpr());
   }
 
   Code = serialization::DECL_BLOCK;
@@ -1494,7 +1505,7 @@ void ASTDeclWriter::VisitNonTypeTemplate
                           !D->defaultArgumentWasInherited();
     Record.push_back(OwnsDefaultArg);
     if (OwnsDefaultArg)
-      Record.AddStmt(D->getDefaultArgument());
+      Writer.AddStmt(D->getDefaultArgument());
     Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
   }
 }
@@ -1535,9 +1546,9 @@ void ASTDeclWriter::VisitTypeAliasTempla
 
 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
   VisitDecl(D);
-  Record.AddStmt(D->getAssertExpr());
+  Writer.AddStmt(D->getAssertExpr());
   Record.push_back(D->isFailed());
-  Record.AddStmt(D->getMessage());
+  Writer.AddStmt(D->getMessage());
   Record.AddSourceLocation(D->getRParenLoc());
   Code = serialization::DECL_STATIC_ASSERT;
 }
@@ -1650,15 +1661,15 @@ void ASTDeclWriter::VisitOMPThreadPrivat
   Record.push_back(D->varlist_size());
   VisitDecl(D);
   for (auto *I : D->varlists())
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
   Code = serialization::DECL_OMP_THREADPRIVATE;
 }
 
 void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
   VisitValueDecl(D);
   Record.AddSourceLocation(D->getLocStart());
-  Record.AddStmt(D->getCombiner());
-  Record.AddStmt(D->getInitializer());
+  Writer.AddStmt(D->getCombiner());
+  Writer.AddStmt(D->getInitializer());
   Record.AddDeclRef(D->getPrevDeclInScope());
   Code = serialization::DECL_OMP_DECLARE_REDUCTION;
 }
@@ -2136,6 +2147,9 @@ static bool isRequiredDecl(const Decl *D
 }
 
 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
+  // Switch case IDs are per Decl.
+  ClearSwitchCaseIDs();
+
   // Determine the ID for this declaration.
   serialization::DeclID ID;
   assert(!D->isFromASTFile() && "should not be emitting imported decl");
@@ -2194,16 +2208,10 @@ void ASTWriter::WriteDecl(ASTContext &Co
     EagerlyDeserializedDecls.push_back(ID);
 }
 
-void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {
-  // Switch case IDs are per function body.
-  Writer->ClearSwitchCaseIDs();
-
-  assert(FD->doesThisDeclarationHaveABody());
-  if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
-    Record->push_back(CD->getNumCtorInitializers());
-    if (CD->getNumCtorInitializers())
-      AddCXXCtorInitializersRef(
-          llvm::makeArrayRef(CD->init_begin(), CD->init_end()));
-  }
-  AddStmt(FD->getBody());
+void ASTWriter::AddFunctionDefinition(const FunctionDecl *FD,
+                                      RecordDataImpl &Record) {
+  ClearSwitchCaseIDs();
+
+  ASTDeclWriter W(*this, FD->getASTContext(), Record);
+  W.AddFunctionDefinition(FD);
 }

Modified: cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterStmt.cpp?rev=265526&r1=265525&r2=265526&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterStmt.cpp Wed Apr  6 05:01:46 2016
@@ -31,23 +31,14 @@ namespace clang {
   class ASTStmtWriter : public StmtVisitor<ASTStmtWriter, void> {
     friend class OMPClauseWriter;
     ASTWriter &Writer;
-    ASTRecordWriter Record;
+    ASTWriter::RecordData &Record;
 
+  public:
     serialization::StmtCode Code;
     unsigned AbbrevToUse;
 
-  public:
     ASTStmtWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
-        : Writer(Writer), Record(Writer, Record),
-          Code(serialization::STMT_NULL_PTR), AbbrevToUse(0) {}
-
-    ASTStmtWriter(const ASTStmtWriter&) = delete;
-
-    uint64_t Emit(Stmt *S) {
-      assert(Code != serialization::STMT_NULL_PTR &&
-             "unhandled sub-statement writing AST file");
-      return Record.EmitStmt(Code, AbbrevToUse);
-    }
+      : Writer(Writer), Record(Record) { }
 
     void AddTemplateKWAndArgsInfo(const ASTTemplateKWAndArgsInfo &ArgInfo,
                                   const TemplateArgumentLoc *Args);
@@ -61,11 +52,11 @@ namespace clang {
 
 void ASTStmtWriter::AddTemplateKWAndArgsInfo(
     const ASTTemplateKWAndArgsInfo &ArgInfo, const TemplateArgumentLoc *Args) {
-  Record.AddSourceLocation(ArgInfo.TemplateKWLoc);
-  Record.AddSourceLocation(ArgInfo.LAngleLoc);
-  Record.AddSourceLocation(ArgInfo.RAngleLoc);
+  Writer.AddSourceLocation(ArgInfo.TemplateKWLoc, Record);
+  Writer.AddSourceLocation(ArgInfo.LAngleLoc, Record);
+  Writer.AddSourceLocation(ArgInfo.RAngleLoc, Record);
   for (unsigned i = 0; i != ArgInfo.NumTemplateArgs; ++i)
-    Record.AddTemplateArgumentLoc(Args[i]);
+    Writer.AddTemplateArgumentLoc(Args[i], Record);
 }
 
 void ASTStmtWriter::VisitStmt(Stmt *S) {
@@ -73,7 +64,7 @@ void ASTStmtWriter::VisitStmt(Stmt *S) {
 
 void ASTStmtWriter::VisitNullStmt(NullStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getSemiLoc());
+  Writer.AddSourceLocation(S->getSemiLoc(), Record);
   Record.push_back(S->HasLeadingEmptyMacro);
   Code = serialization::STMT_NULL;
 }
@@ -82,68 +73,68 @@ void ASTStmtWriter::VisitCompoundStmt(Co
   VisitStmt(S);
   Record.push_back(S->size());
   for (auto *CS : S->body())
-    Record.AddStmt(CS);
-  Record.AddSourceLocation(S->getLBracLoc());
-  Record.AddSourceLocation(S->getRBracLoc());
+    Writer.AddStmt(CS);
+  Writer.AddSourceLocation(S->getLBracLoc(), Record);
+  Writer.AddSourceLocation(S->getRBracLoc(), Record);
   Code = serialization::STMT_COMPOUND;
 }
 
 void ASTStmtWriter::VisitSwitchCase(SwitchCase *S) {
   VisitStmt(S);
   Record.push_back(Writer.getSwitchCaseID(S));
-  Record.AddSourceLocation(S->getKeywordLoc());
-  Record.AddSourceLocation(S->getColonLoc());
+  Writer.AddSourceLocation(S->getKeywordLoc(), Record);
+  Writer.AddSourceLocation(S->getColonLoc(), Record);
 }
 
 void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) {
   VisitSwitchCase(S);
-  Record.AddStmt(S->getLHS());
-  Record.AddStmt(S->getRHS());
-  Record.AddStmt(S->getSubStmt());
-  Record.AddSourceLocation(S->getEllipsisLoc());
+  Writer.AddStmt(S->getLHS());
+  Writer.AddStmt(S->getRHS());
+  Writer.AddStmt(S->getSubStmt());
+  Writer.AddSourceLocation(S->getEllipsisLoc(), Record);
   Code = serialization::STMT_CASE;
 }
 
 void ASTStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
   VisitSwitchCase(S);
-  Record.AddStmt(S->getSubStmt());
+  Writer.AddStmt(S->getSubStmt());
   Code = serialization::STMT_DEFAULT;
 }
 
 void ASTStmtWriter::VisitLabelStmt(LabelStmt *S) {
   VisitStmt(S);
-  Record.AddDeclRef(S->getDecl());
-  Record.AddStmt(S->getSubStmt());
-  Record.AddSourceLocation(S->getIdentLoc());
+  Writer.AddDeclRef(S->getDecl(), Record);
+  Writer.AddStmt(S->getSubStmt());
+  Writer.AddSourceLocation(S->getIdentLoc(), Record);
   Code = serialization::STMT_LABEL;
 }
 
 void ASTStmtWriter::VisitAttributedStmt(AttributedStmt *S) {
   VisitStmt(S);
   Record.push_back(S->getAttrs().size());
-  Record.AddAttributes(S->getAttrs());
-  Record.AddStmt(S->getSubStmt());
-  Record.AddSourceLocation(S->getAttrLoc());
+  Writer.AddAttributes(S->getAttrs(), Record);
+  Writer.AddStmt(S->getSubStmt());
+  Writer.AddSourceLocation(S->getAttrLoc(), Record);
   Code = serialization::STMT_ATTRIBUTED;
 }
 
 void ASTStmtWriter::VisitIfStmt(IfStmt *S) {
   VisitStmt(S);
-  Record.AddDeclRef(S->getConditionVariable());
-  Record.AddStmt(S->getCond());
-  Record.AddStmt(S->getThen());
-  Record.AddStmt(S->getElse());
-  Record.AddSourceLocation(S->getIfLoc());
-  Record.AddSourceLocation(S->getElseLoc());
+  Writer.AddDeclRef(S->getConditionVariable(), Record);
+  Writer.AddStmt(S->getCond());
+  Writer.AddStmt(S->getThen());
+  Writer.AddStmt(S->getElse());
+  Writer.AddSourceLocation(S->getIfLoc(), Record);
+  Writer.AddSourceLocation(S->getElseLoc(), Record);
   Code = serialization::STMT_IF;
 }
 
 void ASTStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
   VisitStmt(S);
-  Record.AddDeclRef(S->getConditionVariable());
-  Record.AddStmt(S->getCond());
-  Record.AddStmt(S->getBody());
-  Record.AddSourceLocation(S->getSwitchLoc());
+  Writer.AddDeclRef(S->getConditionVariable(), Record);
+  Writer.AddStmt(S->getCond());
+  Writer.AddStmt(S->getBody());
+  Writer.AddSourceLocation(S->getSwitchLoc(), Record);
   Record.push_back(S->isAllEnumCasesCovered());
   for (SwitchCase *SC = S->getSwitchCaseList(); SC;
        SC = SC->getNextSwitchCase())
@@ -153,79 +144,79 @@ void ASTStmtWriter::VisitSwitchStmt(Swit
 
 void ASTStmtWriter::VisitWhileStmt(WhileStmt *S) {
   VisitStmt(S);
-  Record.AddDeclRef(S->getConditionVariable());
-  Record.AddStmt(S->getCond());
-  Record.AddStmt(S->getBody());
-  Record.AddSourceLocation(S->getWhileLoc());
+  Writer.AddDeclRef(S->getConditionVariable(), Record);
+  Writer.AddStmt(S->getCond());
+  Writer.AddStmt(S->getBody());
+  Writer.AddSourceLocation(S->getWhileLoc(), Record);
   Code = serialization::STMT_WHILE;
 }
 
 void ASTStmtWriter::VisitDoStmt(DoStmt *S) {
   VisitStmt(S);
-  Record.AddStmt(S->getCond());
-  Record.AddStmt(S->getBody());
-  Record.AddSourceLocation(S->getDoLoc());
-  Record.AddSourceLocation(S->getWhileLoc());
-  Record.AddSourceLocation(S->getRParenLoc());
+  Writer.AddStmt(S->getCond());
+  Writer.AddStmt(S->getBody());
+  Writer.AddSourceLocation(S->getDoLoc(), Record);
+  Writer.AddSourceLocation(S->getWhileLoc(), Record);
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
   Code = serialization::STMT_DO;
 }
 
 void ASTStmtWriter::VisitForStmt(ForStmt *S) {
   VisitStmt(S);
-  Record.AddStmt(S->getInit());
-  Record.AddStmt(S->getCond());
-  Record.AddDeclRef(S->getConditionVariable());
-  Record.AddStmt(S->getInc());
-  Record.AddStmt(S->getBody());
-  Record.AddSourceLocation(S->getForLoc());
-  Record.AddSourceLocation(S->getLParenLoc());
-  Record.AddSourceLocation(S->getRParenLoc());
+  Writer.AddStmt(S->getInit());
+  Writer.AddStmt(S->getCond());
+  Writer.AddDeclRef(S->getConditionVariable(), Record);
+  Writer.AddStmt(S->getInc());
+  Writer.AddStmt(S->getBody());
+  Writer.AddSourceLocation(S->getForLoc(), Record);
+  Writer.AddSourceLocation(S->getLParenLoc(), Record);
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
   Code = serialization::STMT_FOR;
 }
 
 void ASTStmtWriter::VisitGotoStmt(GotoStmt *S) {
   VisitStmt(S);
-  Record.AddDeclRef(S->getLabel());
-  Record.AddSourceLocation(S->getGotoLoc());
-  Record.AddSourceLocation(S->getLabelLoc());
+  Writer.AddDeclRef(S->getLabel(), Record);
+  Writer.AddSourceLocation(S->getGotoLoc(), Record);
+  Writer.AddSourceLocation(S->getLabelLoc(), Record);
   Code = serialization::STMT_GOTO;
 }
 
 void ASTStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getGotoLoc());
-  Record.AddSourceLocation(S->getStarLoc());
-  Record.AddStmt(S->getTarget());
+  Writer.AddSourceLocation(S->getGotoLoc(), Record);
+  Writer.AddSourceLocation(S->getStarLoc(), Record);
+  Writer.AddStmt(S->getTarget());
   Code = serialization::STMT_INDIRECT_GOTO;
 }
 
 void ASTStmtWriter::VisitContinueStmt(ContinueStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getContinueLoc());
+  Writer.AddSourceLocation(S->getContinueLoc(), Record);
   Code = serialization::STMT_CONTINUE;
 }
 
 void ASTStmtWriter::VisitBreakStmt(BreakStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getBreakLoc());
+  Writer.AddSourceLocation(S->getBreakLoc(), Record);
   Code = serialization::STMT_BREAK;
 }
 
 void ASTStmtWriter::VisitReturnStmt(ReturnStmt *S) {
   VisitStmt(S);
-  Record.AddStmt(S->getRetValue());
-  Record.AddSourceLocation(S->getReturnLoc());
-  Record.AddDeclRef(S->getNRVOCandidate());
+  Writer.AddStmt(S->getRetValue());
+  Writer.AddSourceLocation(S->getReturnLoc(), Record);
+  Writer.AddDeclRef(S->getNRVOCandidate(), Record);
   Code = serialization::STMT_RETURN;
 }
 
 void ASTStmtWriter::VisitDeclStmt(DeclStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getStartLoc());
-  Record.AddSourceLocation(S->getEndLoc());
+  Writer.AddSourceLocation(S->getStartLoc(), Record);
+  Writer.AddSourceLocation(S->getEndLoc(), Record);
   DeclGroupRef DG = S->getDeclGroup();
   for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
-    Record.AddDeclRef(*D);
+    Writer.AddDeclRef(*D, Record);
   Code = serialization::STMT_DECL;
 }
 
@@ -234,65 +225,64 @@ void ASTStmtWriter::VisitAsmStmt(AsmStmt
   Record.push_back(S->getNumOutputs());
   Record.push_back(S->getNumInputs());
   Record.push_back(S->getNumClobbers());
-  Record.AddSourceLocation(S->getAsmLoc());
+  Writer.AddSourceLocation(S->getAsmLoc(), Record);
   Record.push_back(S->isVolatile());
   Record.push_back(S->isSimple());
 }
 
 void ASTStmtWriter::VisitGCCAsmStmt(GCCAsmStmt *S) {
   VisitAsmStmt(S);
-  Record.AddSourceLocation(S->getRParenLoc());
-  Record.AddStmt(S->getAsmString());
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
+  Writer.AddStmt(S->getAsmString());
 
   // Outputs
   for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {      
-    Record.AddIdentifierRef(S->getOutputIdentifier(I));
-    Record.AddStmt(S->getOutputConstraintLiteral(I));
-    Record.AddStmt(S->getOutputExpr(I));
+    Writer.AddIdentifierRef(S->getOutputIdentifier(I), Record);
+    Writer.AddStmt(S->getOutputConstraintLiteral(I));
+    Writer.AddStmt(S->getOutputExpr(I));
   }
 
   // Inputs
   for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
-    Record.AddIdentifierRef(S->getInputIdentifier(I));
-    Record.AddStmt(S->getInputConstraintLiteral(I));
-    Record.AddStmt(S->getInputExpr(I));
+    Writer.AddIdentifierRef(S->getInputIdentifier(I), Record);
+    Writer.AddStmt(S->getInputConstraintLiteral(I));
+    Writer.AddStmt(S->getInputExpr(I));
   }
 
   // Clobbers
   for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
-    Record.AddStmt(S->getClobberStringLiteral(I));
+    Writer.AddStmt(S->getClobberStringLiteral(I));
 
   Code = serialization::STMT_GCCASM;
 }
 
 void ASTStmtWriter::VisitMSAsmStmt(MSAsmStmt *S) {
   VisitAsmStmt(S);
-  Record.AddSourceLocation(S->getLBraceLoc());
-  Record.AddSourceLocation(S->getEndLoc());
+  Writer.AddSourceLocation(S->getLBraceLoc(), Record);
+  Writer.AddSourceLocation(S->getEndLoc(), Record);
   Record.push_back(S->getNumAsmToks());
-  Record.AddString(S->getAsmString());
+  Writer.AddString(S->getAsmString(), Record);
 
   // Tokens
   for (unsigned I = 0, N = S->getNumAsmToks(); I != N; ++I) {
-    // FIXME: Move this to ASTRecordWriter?
-    Writer.AddToken(S->getAsmToks()[I], Record.getRecordData());
+    Writer.AddToken(S->getAsmToks()[I], Record);
   }
 
   // Clobbers
   for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I) {
-    Record.AddString(S->getClobber(I));
+    Writer.AddString(S->getClobber(I), Record);
   }
 
   // Outputs
   for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {      
-    Record.AddStmt(S->getOutputExpr(I));
-    Record.AddString(S->getOutputConstraint(I));
+    Writer.AddStmt(S->getOutputExpr(I));
+    Writer.AddString(S->getOutputConstraint(I), Record);
   }
 
   // Inputs
   for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
-    Record.AddStmt(S->getInputExpr(I));
-    Record.AddString(S->getInputConstraint(I));
+    Writer.AddStmt(S->getInputExpr(I));
+    Writer.AddString(S->getInputConstraint(I), Record);
   }
 
   Code = serialization::STMT_MSASM;
@@ -324,26 +314,26 @@ void ASTStmtWriter::VisitCapturedStmt(Ca
   Record.push_back(std::distance(S->capture_begin(), S->capture_end()));
 
   // CapturedDecl and captured region kind
-  Record.AddDeclRef(S->getCapturedDecl());
+  Writer.AddDeclRef(S->getCapturedDecl(), Record);
   Record.push_back(S->getCapturedRegionKind());
 
-  Record.AddDeclRef(S->getCapturedRecordDecl());
+  Writer.AddDeclRef(S->getCapturedRecordDecl(), Record);
 
   // Capture inits
   for (auto *I : S->capture_inits())
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
 
   // Body
-  Record.AddStmt(S->getCapturedStmt());
+  Writer.AddStmt(S->getCapturedStmt());
 
   // Captures
   for (const auto &I : S->captures()) {
     if (I.capturesThis() || I.capturesVariableArrayType())
-      Record.AddDeclRef(nullptr);
+      Writer.AddDeclRef(nullptr, Record);
     else
-      Record.AddDeclRef(I.getCapturedVar());
+      Writer.AddDeclRef(I.getCapturedVar(), Record);
     Record.push_back(I.getCaptureKind());
-    Record.AddSourceLocation(I.getLocation());
+    Writer.AddSourceLocation(I.getLocation(), Record);
   }
 
   Code = serialization::STMT_CAPTURED;
@@ -351,7 +341,7 @@ void ASTStmtWriter::VisitCapturedStmt(Ca
 
 void ASTStmtWriter::VisitExpr(Expr *E) {
   VisitStmt(E);
-  Record.AddTypeRef(E->getType());
+  Writer.AddTypeRef(E->getType(), Record);
   Record.push_back(E->isTypeDependent());
   Record.push_back(E->isValueDependent());
   Record.push_back(E->isInstantiationDependent());
@@ -362,9 +352,9 @@ void ASTStmtWriter::VisitExpr(Expr *E) {
 
 void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Record.push_back(E->getIdentType()); // FIXME: stable encoding
-  Record.AddStmt(E->getFunctionName());
+  Writer.AddStmt(E->getFunctionName());
   Code = serialization::EXPR_PREDEFINED;
 }
 
@@ -391,25 +381,25 @@ void ASTStmtWriter::VisitDeclRefExpr(Dec
   }
 
   if (E->hasQualifier())
-    Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
+    Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
 
   if (E->getDecl() != E->getFoundDecl())
-    Record.AddDeclRef(E->getFoundDecl());
+    Writer.AddDeclRef(E->getFoundDecl(), Record);
 
   if (E->hasTemplateKWAndArgsInfo())
     AddTemplateKWAndArgsInfo(*E->getTrailingObjects<ASTTemplateKWAndArgsInfo>(),
                              E->getTrailingObjects<TemplateArgumentLoc>());
 
-  Record.AddDeclRef(E->getDecl());
-  Record.AddSourceLocation(E->getLocation());
-  Record.AddDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName());
+  Writer.AddDeclRef(E->getDecl(), Record);
+  Writer.AddSourceLocation(E->getLocation(), Record);
+  Writer.AddDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName(), Record);
   Code = serialization::EXPR_DECL_REF;
 }
 
 void ASTStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getLocation());
-  Record.AddAPInt(E->getValue());
+  Writer.AddSourceLocation(E->getLocation(), Record);
+  Writer.AddAPInt(E->getValue(), Record);
 
   if (E->getValue().getBitWidth() == 32) {
     AbbrevToUse = Writer.getIntegerLiteralAbbrev();
@@ -422,14 +412,14 @@ void ASTStmtWriter::VisitFloatingLiteral
   VisitExpr(E);
   Record.push_back(E->getRawSemantics());
   Record.push_back(E->isExact());
-  Record.AddAPFloat(E->getValue());
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddAPFloat(E->getValue(), Record);
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Code = serialization::EXPR_FLOATING_LITERAL;
 }
 
 void ASTStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddStmt(E->getSubExpr());
   Code = serialization::EXPR_IMAGINARY_LITERAL;
 }
 
@@ -445,14 +435,14 @@ void ASTStmtWriter::VisitStringLiteral(S
   // the AST file during deserialization.
   Record.append(E->getBytes().begin(), E->getBytes().end());
   for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
-    Record.AddSourceLocation(E->getStrTokenLoc(I));
+    Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
   Code = serialization::EXPR_STRING_LITERAL;
 }
 
 void ASTStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
   VisitExpr(E);
   Record.push_back(E->getValue());
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Record.push_back(E->getKind());
 
   AbbrevToUse = Writer.getCharacterLiteralAbbrev();
@@ -462,9 +452,9 @@ void ASTStmtWriter::VisitCharacterLitera
 
 void ASTStmtWriter::VisitParenExpr(ParenExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getLParen());
-  Record.AddSourceLocation(E->getRParen());
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddSourceLocation(E->getLParen(), Record);
+  Writer.AddSourceLocation(E->getRParen(), Record);
+  Writer.AddStmt(E->getSubExpr());
   Code = serialization::EXPR_PAREN;
 }
 
@@ -472,17 +462,17 @@ void ASTStmtWriter::VisitParenListExpr(P
   VisitExpr(E);
   Record.push_back(E->NumExprs);
   for (unsigned i=0; i != E->NumExprs; ++i)
-    Record.AddStmt(E->Exprs[i]);
-  Record.AddSourceLocation(E->LParenLoc);
-  Record.AddSourceLocation(E->RParenLoc);
+    Writer.AddStmt(E->Exprs[i]);
+  Writer.AddSourceLocation(E->LParenLoc, Record);
+  Writer.AddSourceLocation(E->RParenLoc, Record);
   Code = serialization::EXPR_PAREN_LIST;
 }
 
 void ASTStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddStmt(E->getSubExpr());
   Record.push_back(E->getOpcode()); // FIXME: stable encoding
-  Record.AddSourceLocation(E->getOperatorLoc());
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
   Code = serialization::EXPR_UNARY_OPERATOR;
 }
 
@@ -490,34 +480,34 @@ void ASTStmtWriter::VisitOffsetOfExpr(Of
   VisitExpr(E);
   Record.push_back(E->getNumComponents());
   Record.push_back(E->getNumExpressions());
-  Record.AddSourceLocation(E->getOperatorLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
-  Record.AddTypeSourceInfo(E->getTypeSourceInfo());
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
   for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
     const OffsetOfNode &ON = E->getComponent(I);
     Record.push_back(ON.getKind()); // FIXME: Stable encoding
-    Record.AddSourceLocation(ON.getSourceRange().getBegin());
-    Record.AddSourceLocation(ON.getSourceRange().getEnd());
+    Writer.AddSourceLocation(ON.getSourceRange().getBegin(), Record);
+    Writer.AddSourceLocation(ON.getSourceRange().getEnd(), Record);
     switch (ON.getKind()) {
     case OffsetOfNode::Array:
       Record.push_back(ON.getArrayExprIndex());
       break;
 
     case OffsetOfNode::Field:
-      Record.AddDeclRef(ON.getField());
+      Writer.AddDeclRef(ON.getField(), Record);
       break;
 
     case OffsetOfNode::Identifier:
-      Record.AddIdentifierRef(ON.getFieldName());
+      Writer.AddIdentifierRef(ON.getFieldName(), Record);
       break;
 
     case OffsetOfNode::Base:
-      Record.AddCXXBaseSpecifier(*ON.getBase());
+      Writer.AddCXXBaseSpecifier(*ON.getBase(), Record);
       break;
     }
   }
   for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I)
-    Record.AddStmt(E->getIndexExpr(I));
+    Writer.AddStmt(E->getIndexExpr(I));
   Code = serialization::EXPR_OFFSETOF;
 }
 
@@ -525,42 +515,42 @@ void ASTStmtWriter::VisitUnaryExprOrType
   VisitExpr(E);
   Record.push_back(E->getKind());
   if (E->isArgumentType())
-    Record.AddTypeSourceInfo(E->getArgumentTypeInfo());
+    Writer.AddTypeSourceInfo(E->getArgumentTypeInfo(), Record);
   else {
     Record.push_back(0);
-    Record.AddStmt(E->getArgumentExpr());
+    Writer.AddStmt(E->getArgumentExpr());
   }
-  Record.AddSourceLocation(E->getOperatorLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_SIZEOF_ALIGN_OF;
 }
 
 void ASTStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getLHS());
-  Record.AddStmt(E->getRHS());
-  Record.AddSourceLocation(E->getRBracketLoc());
+  Writer.AddStmt(E->getLHS());
+  Writer.AddStmt(E->getRHS());
+  Writer.AddSourceLocation(E->getRBracketLoc(), Record);
   Code = serialization::EXPR_ARRAY_SUBSCRIPT;
 }
 
 void ASTStmtWriter::VisitOMPArraySectionExpr(OMPArraySectionExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getBase());
-  Record.AddStmt(E->getLowerBound());
-  Record.AddStmt(E->getLength());
-  Record.AddSourceLocation(E->getColonLoc());
-  Record.AddSourceLocation(E->getRBracketLoc());
+  Writer.AddStmt(E->getBase());
+  Writer.AddStmt(E->getLowerBound());
+  Writer.AddStmt(E->getLength());
+  Writer.AddSourceLocation(E->getColonLoc(), Record);
+  Writer.AddSourceLocation(E->getRBracketLoc(), Record);
   Code = serialization::EXPR_OMP_ARRAY_SECTION;
 }
 
 void ASTStmtWriter::VisitCallExpr(CallExpr *E) {
   VisitExpr(E);
   Record.push_back(E->getNumArgs());
-  Record.AddSourceLocation(E->getRParenLoc());
-  Record.AddStmt(E->getCallee());
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
+  Writer.AddStmt(E->getCallee());
   for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
        Arg != ArgEnd; ++Arg)
-    Record.AddStmt(*Arg);
+    Writer.AddStmt(*Arg);
   Code = serialization::EXPR_CALL;
 }
 
@@ -569,43 +559,43 @@ void ASTStmtWriter::VisitMemberExpr(Memb
 
   Record.push_back(E->hasQualifier());
   if (E->hasQualifier())
-    Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
+    Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
 
   Record.push_back(E->HasTemplateKWAndArgsInfo);
   if (E->HasTemplateKWAndArgsInfo) {
-    Record.AddSourceLocation(E->getTemplateKeywordLoc());
+    Writer.AddSourceLocation(E->getTemplateKeywordLoc(), Record);
     unsigned NumTemplateArgs = E->getNumTemplateArgs();
     Record.push_back(NumTemplateArgs);
-    Record.AddSourceLocation(E->getLAngleLoc());
-    Record.AddSourceLocation(E->getRAngleLoc());
+    Writer.AddSourceLocation(E->getLAngleLoc(), Record);
+    Writer.AddSourceLocation(E->getRAngleLoc(), Record);
     for (unsigned i=0; i != NumTemplateArgs; ++i)
-      Record.AddTemplateArgumentLoc(E->getTemplateArgs()[i]);
+      Writer.AddTemplateArgumentLoc(E->getTemplateArgs()[i], Record);
   }
 
   Record.push_back(E->hadMultipleCandidates());
 
   DeclAccessPair FoundDecl = E->getFoundDecl();
-  Record.AddDeclRef(FoundDecl.getDecl());
+  Writer.AddDeclRef(FoundDecl.getDecl(), Record);
   Record.push_back(FoundDecl.getAccess());
 
-  Record.AddTypeRef(E->getType());
+  Writer.AddTypeRef(E->getType(), Record);
   Record.push_back(E->getValueKind());
   Record.push_back(E->getObjectKind());
-  Record.AddStmt(E->getBase());
-  Record.AddDeclRef(E->getMemberDecl());
-  Record.AddSourceLocation(E->getMemberLoc());
+  Writer.AddStmt(E->getBase());
+  Writer.AddDeclRef(E->getMemberDecl(), Record);
+  Writer.AddSourceLocation(E->getMemberLoc(), Record);
   Record.push_back(E->isArrow());
-  Record.AddSourceLocation(E->getOperatorLoc());
-  Record.AddDeclarationNameLoc(E->MemberDNLoc,
-                               E->getMemberDecl()->getDeclName());
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
+  Writer.AddDeclarationNameLoc(E->MemberDNLoc,
+                               E->getMemberDecl()->getDeclName(), Record);
   Code = serialization::EXPR_MEMBER;
 }
 
 void ASTStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getBase());
-  Record.AddSourceLocation(E->getIsaMemberLoc());
-  Record.AddSourceLocation(E->getOpLoc());
+  Writer.AddStmt(E->getBase());
+  Writer.AddSourceLocation(E->getIsaMemberLoc(), Record);
+  Writer.AddSourceLocation(E->getOpLoc(), Record);
   Record.push_back(E->isArrow());
   Code = serialization::EXPR_OBJC_ISA;
 }
@@ -613,15 +603,15 @@ void ASTStmtWriter::VisitObjCIsaExpr(Obj
 void ASTStmtWriter::
 VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddStmt(E->getSubExpr());
   Record.push_back(E->shouldCopy());
   Code = serialization::EXPR_OBJC_INDIRECT_COPY_RESTORE;
 }
 
 void ASTStmtWriter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
   VisitExplicitCastExpr(E);
-  Record.AddSourceLocation(E->getLParenLoc());
-  Record.AddSourceLocation(E->getBridgeKeywordLoc());
+  Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddSourceLocation(E->getBridgeKeywordLoc(), Record);
   Record.push_back(E->getBridgeKind()); // FIXME: Stable encoding
   Code = serialization::EXPR_OBJC_BRIDGED_CAST;
 }
@@ -629,51 +619,51 @@ void ASTStmtWriter::VisitObjCBridgedCast
 void ASTStmtWriter::VisitCastExpr(CastExpr *E) {
   VisitExpr(E);
   Record.push_back(E->path_size());
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddStmt(E->getSubExpr());
   Record.push_back(E->getCastKind()); // FIXME: stable encoding
 
   for (CastExpr::path_iterator
          PI = E->path_begin(), PE = E->path_end(); PI != PE; ++PI)
-    Record.AddCXXBaseSpecifier(**PI);
+    Writer.AddCXXBaseSpecifier(**PI, Record);
 }
 
 void ASTStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getLHS());
-  Record.AddStmt(E->getRHS());
+  Writer.AddStmt(E->getLHS());
+  Writer.AddStmt(E->getRHS());
   Record.push_back(E->getOpcode()); // FIXME: stable encoding
-  Record.AddSourceLocation(E->getOperatorLoc());
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
   Record.push_back(E->isFPContractable());
   Code = serialization::EXPR_BINARY_OPERATOR;
 }
 
 void ASTStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
   VisitBinaryOperator(E);
-  Record.AddTypeRef(E->getComputationLHSType());
-  Record.AddTypeRef(E->getComputationResultType());
+  Writer.AddTypeRef(E->getComputationLHSType(), Record);
+  Writer.AddTypeRef(E->getComputationResultType(), Record);
   Code = serialization::EXPR_COMPOUND_ASSIGN_OPERATOR;
 }
 
 void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getCond());
-  Record.AddStmt(E->getLHS());
-  Record.AddStmt(E->getRHS());
-  Record.AddSourceLocation(E->getQuestionLoc());
-  Record.AddSourceLocation(E->getColonLoc());
+  Writer.AddStmt(E->getCond());
+  Writer.AddStmt(E->getLHS());
+  Writer.AddStmt(E->getRHS());
+  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
+  Writer.AddSourceLocation(E->getColonLoc(), Record);
   Code = serialization::EXPR_CONDITIONAL_OPERATOR;
 }
 
 void
 ASTStmtWriter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getOpaqueValue());
-  Record.AddStmt(E->getCommon());
-  Record.AddStmt(E->getCond());
-  Record.AddStmt(E->getTrueExpr());
-  Record.AddStmt(E->getFalseExpr());
-  Record.AddSourceLocation(E->getQuestionLoc());
-  Record.AddSourceLocation(E->getColonLoc());
+  Writer.AddStmt(E->getOpaqueValue());
+  Writer.AddStmt(E->getCommon());
+  Writer.AddStmt(E->getCond());
+  Writer.AddStmt(E->getTrueExpr());
+  Writer.AddStmt(E->getFalseExpr());
+  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
+  Writer.AddSourceLocation(E->getColonLoc(), Record);
   Code = serialization::EXPR_BINARY_CONDITIONAL_OPERATOR;
 }
 
@@ -688,30 +678,30 @@ void ASTStmtWriter::VisitImplicitCastExp
 
 void ASTStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
   VisitCastExpr(E);
-  Record.AddTypeSourceInfo(E->getTypeInfoAsWritten());
+  Writer.AddTypeSourceInfo(E->getTypeInfoAsWritten(), Record);
 }
 
 void ASTStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
   VisitExplicitCastExpr(E);
-  Record.AddSourceLocation(E->getLParenLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_CSTYLE_CAST;
 }
 
 void ASTStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getLParenLoc());
-  Record.AddTypeSourceInfo(E->getTypeSourceInfo());
-  Record.AddStmt(E->getInitializer());
+  Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
+  Writer.AddStmt(E->getInitializer());
   Record.push_back(E->isFileScope());
   Code = serialization::EXPR_COMPOUND_LITERAL;
 }
 
 void ASTStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getBase());
-  Record.AddIdentifierRef(&E->getAccessor());
-  Record.AddSourceLocation(E->getAccessorLoc());
+  Writer.AddStmt(E->getBase());
+  Writer.AddIdentifierRef(&E->getAccessor(), Record);
+  Writer.AddSourceLocation(E->getAccessorLoc(), Record);
   Code = serialization::EXPR_EXT_VECTOR_ELEMENT;
 }
 
@@ -719,15 +709,15 @@ void ASTStmtWriter::VisitInitListExpr(In
   VisitExpr(E);
   // NOTE: only add the (possibly null) syntactic form.
   // No need to serialize the isSemanticForm flag and the semantic form.
-  Record.AddStmt(E->getSyntacticForm());
-  Record.AddSourceLocation(E->getLBraceLoc());
-  Record.AddSourceLocation(E->getRBraceLoc());
+  Writer.AddStmt(E->getSyntacticForm());
+  Writer.AddSourceLocation(E->getLBraceLoc(), Record);
+  Writer.AddSourceLocation(E->getRBraceLoc(), Record);
   bool isArrayFiller = E->ArrayFillerOrUnionFieldInit.is<Expr*>();
   Record.push_back(isArrayFiller);
   if (isArrayFiller)
-    Record.AddStmt(E->getArrayFiller());
+    Writer.AddStmt(E->getArrayFiller());
   else
-    Record.AddDeclRef(E->getInitializedFieldInUnion());
+    Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
   Record.push_back(E->hadArrayRangeDesignator());
   Record.push_back(E->getNumInits());
   if (isArrayFiller) {
@@ -735,10 +725,10 @@ void ASTStmtWriter::VisitInitListExpr(In
     // Replace them by 0 to indicate that the filler goes in that place.
     Expr *filler = E->getArrayFiller();
     for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
-      Record.AddStmt(E->getInit(I) != filler ? E->getInit(I) : nullptr);
+      Writer.AddStmt(E->getInit(I) != filler ? E->getInit(I) : nullptr);
   } else {
     for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
-      Record.AddStmt(E->getInit(I));
+      Writer.AddStmt(E->getInit(I));
   }
   Code = serialization::EXPR_INIT_LIST;
 }
@@ -747,8 +737,8 @@ void ASTStmtWriter::VisitDesignatedInitE
   VisitExpr(E);
   Record.push_back(E->getNumSubExprs());
   for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
-    Record.AddStmt(E->getSubExpr(I));
-  Record.AddSourceLocation(E->getEqualOrColonLoc());
+    Writer.AddStmt(E->getSubExpr(I));
+  Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
   Record.push_back(E->usesGNUSyntax());
   for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
                                              DEnd = E->designators_end();
@@ -756,25 +746,25 @@ void ASTStmtWriter::VisitDesignatedInitE
     if (D->isFieldDesignator()) {
       if (FieldDecl *Field = D->getField()) {
         Record.push_back(serialization::DESIG_FIELD_DECL);
-        Record.AddDeclRef(Field);
+        Writer.AddDeclRef(Field, Record);
       } else {
         Record.push_back(serialization::DESIG_FIELD_NAME);
-        Record.AddIdentifierRef(D->getFieldName());
+        Writer.AddIdentifierRef(D->getFieldName(), Record);
       }
-      Record.AddSourceLocation(D->getDotLoc());
-      Record.AddSourceLocation(D->getFieldLoc());
+      Writer.AddSourceLocation(D->getDotLoc(), Record);
+      Writer.AddSourceLocation(D->getFieldLoc(), Record);
     } else if (D->isArrayDesignator()) {
       Record.push_back(serialization::DESIG_ARRAY);
       Record.push_back(D->getFirstExprIndex());
-      Record.AddSourceLocation(D->getLBracketLoc());
-      Record.AddSourceLocation(D->getRBracketLoc());
+      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
+      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
     } else {
       assert(D->isArrayRangeDesignator() && "Unknown designator");
       Record.push_back(serialization::DESIG_ARRAY_RANGE);
       Record.push_back(D->getFirstExprIndex());
-      Record.AddSourceLocation(D->getLBracketLoc());
-      Record.AddSourceLocation(D->getEllipsisLoc());
-      Record.AddSourceLocation(D->getRBracketLoc());
+      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
+      Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
+      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
     }
   }
   Code = serialization::EXPR_DESIGNATED_INIT;
@@ -782,8 +772,8 @@ void ASTStmtWriter::VisitDesignatedInitE
 
 void ASTStmtWriter::VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getBase());
-  Record.AddStmt(E->getUpdater());
+  Writer.AddStmt(E->getBase());
+  Writer.AddStmt(E->getUpdater());
   Code = serialization::EXPR_DESIGNATED_INIT_UPDATE;
 }
 
@@ -799,44 +789,44 @@ void ASTStmtWriter::VisitImplicitValueIn
 
 void ASTStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubExpr());
-  Record.AddTypeSourceInfo(E->getWrittenTypeInfo());
-  Record.AddSourceLocation(E->getBuiltinLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddStmt(E->getSubExpr());
+  Writer.AddTypeSourceInfo(E->getWrittenTypeInfo(), Record);
+  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Record.push_back(E->isMicrosoftABI());
   Code = serialization::EXPR_VA_ARG;
 }
 
 void ASTStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getAmpAmpLoc());
-  Record.AddSourceLocation(E->getLabelLoc());
-  Record.AddDeclRef(E->getLabel());
+  Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
+  Writer.AddSourceLocation(E->getLabelLoc(), Record);
+  Writer.AddDeclRef(E->getLabel(), Record);
   Code = serialization::EXPR_ADDR_LABEL;
 }
 
 void ASTStmtWriter::VisitStmtExpr(StmtExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubStmt());
-  Record.AddSourceLocation(E->getLParenLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddStmt(E->getSubStmt());
+  Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_STMT;
 }
 
 void ASTStmtWriter::VisitChooseExpr(ChooseExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getCond());
-  Record.AddStmt(E->getLHS());
-  Record.AddStmt(E->getRHS());
-  Record.AddSourceLocation(E->getBuiltinLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddStmt(E->getCond());
+  Writer.AddStmt(E->getLHS());
+  Writer.AddStmt(E->getRHS());
+  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Record.push_back(E->isConditionDependent() ? false : E->isConditionTrue());
   Code = serialization::EXPR_CHOOSE;
 }
 
 void ASTStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getTokenLocation());
+  Writer.AddSourceLocation(E->getTokenLocation(), Record);
   Code = serialization::EXPR_GNU_NULL;
 }
 
@@ -844,24 +834,24 @@ void ASTStmtWriter::VisitShuffleVectorEx
   VisitExpr(E);
   Record.push_back(E->getNumSubExprs());
   for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
-    Record.AddStmt(E->getExpr(I));
-  Record.AddSourceLocation(E->getBuiltinLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+    Writer.AddStmt(E->getExpr(I));
+  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_SHUFFLE_VECTOR;
 }
 
 void ASTStmtWriter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getBuiltinLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
-  Record.AddTypeSourceInfo(E->getTypeSourceInfo());
-  Record.AddStmt(E->getSrcExpr());
+  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
+  Writer.AddStmt(E->getSrcExpr());
   Code = serialization::EXPR_CONVERT_VECTOR;
 }
 
 void ASTStmtWriter::VisitBlockExpr(BlockExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getBlockDecl());
+  Writer.AddDeclRef(E->getBlockDecl(), Record);
   Code = serialization::EXPR_BLOCK;
 }
 
@@ -869,16 +859,16 @@ void ASTStmtWriter::VisitGenericSelectio
   VisitExpr(E);
   Record.push_back(E->getNumAssocs());
 
-  Record.AddStmt(E->getControllingExpr());
+  Writer.AddStmt(E->getControllingExpr());
   for (unsigned I = 0, N = E->getNumAssocs(); I != N; ++I) {
-    Record.AddTypeSourceInfo(E->getAssocTypeSourceInfo(I));
-    Record.AddStmt(E->getAssocExpr(I));
+    Writer.AddTypeSourceInfo(E->getAssocTypeSourceInfo(I), Record);
+    Writer.AddStmt(E->getAssocExpr(I));
   }
   Record.push_back(E->isResultDependent() ? -1U : E->getResultIndex());
 
-  Record.AddSourceLocation(E->getGenericLoc());
-  Record.AddSourceLocation(E->getDefaultLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddSourceLocation(E->getGenericLoc(), Record);
+  Writer.AddSourceLocation(E->getDefaultLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_GENERIC_SELECTION;
 }
 
@@ -892,10 +882,10 @@ void ASTStmtWriter::VisitPseudoObjectExp
   result = (result == PseudoObjectExpr::NoResult ? 0 : result + 1);
   Record.push_back(result);
 
-  Record.AddStmt(E->getSyntacticForm());
+  Writer.AddStmt(E->getSyntacticForm());
   for (PseudoObjectExpr::semantics_iterator
          i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
-    Record.AddStmt(*i);
+    Writer.AddStmt(*i);
   }
   Code = serialization::EXPR_PSEUDO_OBJECT;
 }
@@ -904,9 +894,9 @@ void ASTStmtWriter::VisitAtomicExpr(Atom
   VisitExpr(E);
   Record.push_back(E->getOp());
   for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
-    Record.AddStmt(E->getSubExprs()[I]);
-  Record.AddSourceLocation(E->getBuiltinLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+    Writer.AddStmt(E->getSubExprs()[I]);
+  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_ATOMIC;
 }
 
@@ -916,16 +906,16 @@ void ASTStmtWriter::VisitAtomicExpr(Atom
 
 void ASTStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getString());
-  Record.AddSourceLocation(E->getAtLoc());
+  Writer.AddStmt(E->getString());
+  Writer.AddSourceLocation(E->getAtLoc(), Record);
   Code = serialization::EXPR_OBJC_STRING_LITERAL;
 }
 
 void ASTStmtWriter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubExpr());
-  Record.AddDeclRef(E->getBoxingMethod());
-  Record.AddSourceRange(E->getSourceRange());
+  Writer.AddStmt(E->getSubExpr());
+  Writer.AddDeclRef(E->getBoxingMethod(), Record);
+  Writer.AddSourceRange(E->getSourceRange(), Record);
   Code = serialization::EXPR_OBJC_BOXED_EXPRESSION;
 }
 
@@ -933,9 +923,9 @@ void ASTStmtWriter::VisitObjCArrayLitera
   VisitExpr(E);
   Record.push_back(E->getNumElements());
   for (unsigned i = 0; i < E->getNumElements(); i++)
-    Record.AddStmt(E->getElement(i));
-  Record.AddDeclRef(E->getArrayWithObjectsMethod());
-  Record.AddSourceRange(E->getSourceRange());
+    Writer.AddStmt(E->getElement(i));
+  Writer.AddDeclRef(E->getArrayWithObjectsMethod(), Record);
+  Writer.AddSourceRange(E->getSourceRange(), Record);
   Code = serialization::EXPR_OBJC_ARRAY_LITERAL;
 }
 
@@ -945,10 +935,10 @@ void ASTStmtWriter::VisitObjCDictionaryL
   Record.push_back(E->HasPackExpansions);
   for (unsigned i = 0; i < E->getNumElements(); i++) {
     ObjCDictionaryElement Element = E->getKeyValueElement(i);
-    Record.AddStmt(Element.Key);
-    Record.AddStmt(Element.Value);
+    Writer.AddStmt(Element.Key);
+    Writer.AddStmt(Element.Value);
     if (E->HasPackExpansions) {
-      Record.AddSourceLocation(Element.EllipsisLoc);
+      Writer.AddSourceLocation(Element.EllipsisLoc, Record);
       unsigned NumExpansions = 0;
       if (Element.NumExpansions)
         NumExpansions = *Element.NumExpansions + 1;
@@ -956,42 +946,42 @@ void ASTStmtWriter::VisitObjCDictionaryL
     }
   }
     
-  Record.AddDeclRef(E->getDictWithObjectsMethod());
-  Record.AddSourceRange(E->getSourceRange());
+  Writer.AddDeclRef(E->getDictWithObjectsMethod(), Record);
+  Writer.AddSourceRange(E->getSourceRange(), Record);
   Code = serialization::EXPR_OBJC_DICTIONARY_LITERAL;
 }
 
 void ASTStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
   VisitExpr(E);
-  Record.AddTypeSourceInfo(E->getEncodedTypeSourceInfo());
-  Record.AddSourceLocation(E->getAtLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddTypeSourceInfo(E->getEncodedTypeSourceInfo(), Record);
+  Writer.AddSourceLocation(E->getAtLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_OBJC_ENCODE;
 }
 
 void ASTStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
   VisitExpr(E);
-  Record.AddSelectorRef(E->getSelector());
-  Record.AddSourceLocation(E->getAtLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddSelectorRef(E->getSelector(), Record);
+  Writer.AddSourceLocation(E->getAtLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_OBJC_SELECTOR_EXPR;
 }
 
 void ASTStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getProtocol());
-  Record.AddSourceLocation(E->getAtLoc());
-  Record.AddSourceLocation(E->ProtoLoc);
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddDeclRef(E->getProtocol(), Record);
+  Writer.AddSourceLocation(E->getAtLoc(), Record);
+  Writer.AddSourceLocation(E->ProtoLoc, Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_OBJC_PROTOCOL_EXPR;
 }
 
 void ASTStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getDecl());
-  Record.AddSourceLocation(E->getLocation());
-  Record.AddSourceLocation(E->getOpLoc());
-  Record.AddStmt(E->getBase());
+  Writer.AddDeclRef(E->getDecl(), Record);
+  Writer.AddSourceLocation(E->getLocation(), Record);
+  Writer.AddSourceLocation(E->getOpLoc(), Record);
+  Writer.AddStmt(E->getBase());
   Record.push_back(E->isArrow());
   Record.push_back(E->isFreeIvar());
   Code = serialization::EXPR_OBJC_IVAR_REF_EXPR;
@@ -1002,22 +992,22 @@ void ASTStmtWriter::VisitObjCPropertyRef
   Record.push_back(E->SetterAndMethodRefFlags.getInt());
   Record.push_back(E->isImplicitProperty());
   if (E->isImplicitProperty()) {
-    Record.AddDeclRef(E->getImplicitPropertyGetter());
-    Record.AddDeclRef(E->getImplicitPropertySetter());
+    Writer.AddDeclRef(E->getImplicitPropertyGetter(), Record);
+    Writer.AddDeclRef(E->getImplicitPropertySetter(), Record);
   } else {
-    Record.AddDeclRef(E->getExplicitProperty());
+    Writer.AddDeclRef(E->getExplicitProperty(), Record);
   }
-  Record.AddSourceLocation(E->getLocation());
-  Record.AddSourceLocation(E->getReceiverLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
+  Writer.AddSourceLocation(E->getReceiverLocation(), Record);
   if (E->isObjectReceiver()) {
     Record.push_back(0);
-    Record.AddStmt(E->getBase());
+    Writer.AddStmt(E->getBase());
   } else if (E->isSuperReceiver()) {
     Record.push_back(1);
-    Record.AddTypeRef(E->getSuperReceiverType());
+    Writer.AddTypeRef(E->getSuperReceiverType(), Record);
   } else {
     Record.push_back(2);
-    Record.AddDeclRef(E->getClassReceiver());
+    Writer.AddDeclRef(E->getClassReceiver(), Record);
   }
   
   Code = serialization::EXPR_OBJC_PROPERTY_REF_EXPR;
@@ -1025,11 +1015,11 @@ void ASTStmtWriter::VisitObjCPropertyRef
 
 void ASTStmtWriter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getRBracket());
-  Record.AddStmt(E->getBaseExpr());
-  Record.AddStmt(E->getKeyExpr());
-  Record.AddDeclRef(E->getAtIndexMethodDecl());
-  Record.AddDeclRef(E->setAtIndexMethodDecl());
+  Writer.AddSourceLocation(E->getRBracket(), Record);
+  Writer.AddStmt(E->getBaseExpr());
+  Writer.AddStmt(E->getKeyExpr());
+  Writer.AddDeclRef(E->getAtIndexMethodDecl(), Record);
+  Writer.AddDeclRef(E->setAtIndexMethodDecl(), Record);
   
   Code = serialization::EXPR_OBJC_SUBSCRIPT_REF_EXPR;
 }
@@ -1044,101 +1034,101 @@ void ASTStmtWriter::VisitObjCMessageExpr
   Record.push_back((unsigned)E->getReceiverKind()); // FIXME: stable encoding
   switch (E->getReceiverKind()) {
   case ObjCMessageExpr::Instance:
-    Record.AddStmt(E->getInstanceReceiver());
+    Writer.AddStmt(E->getInstanceReceiver());
     break;
 
   case ObjCMessageExpr::Class:
-    Record.AddTypeSourceInfo(E->getClassReceiverTypeInfo());
+    Writer.AddTypeSourceInfo(E->getClassReceiverTypeInfo(), Record);
     break;
 
   case ObjCMessageExpr::SuperClass:
   case ObjCMessageExpr::SuperInstance:
-    Record.AddTypeRef(E->getSuperType());
-    Record.AddSourceLocation(E->getSuperLoc());
+    Writer.AddTypeRef(E->getSuperType(), Record);
+    Writer.AddSourceLocation(E->getSuperLoc(), Record);
     break;
   }
 
   if (E->getMethodDecl()) {
     Record.push_back(1);
-    Record.AddDeclRef(E->getMethodDecl());
+    Writer.AddDeclRef(E->getMethodDecl(), Record);
   } else {
     Record.push_back(0);
-    Record.AddSelectorRef(E->getSelector());    
+    Writer.AddSelectorRef(E->getSelector(), Record);    
   }
     
-  Record.AddSourceLocation(E->getLeftLoc());
-  Record.AddSourceLocation(E->getRightLoc());
+  Writer.AddSourceLocation(E->getLeftLoc(), Record);
+  Writer.AddSourceLocation(E->getRightLoc(), Record);
 
   for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
        Arg != ArgEnd; ++Arg)
-    Record.AddStmt(*Arg);
+    Writer.AddStmt(*Arg);
 
   SourceLocation *Locs = E->getStoredSelLocs();
   for (unsigned i = 0, e = E->getNumStoredSelLocs(); i != e; ++i)
-    Record.AddSourceLocation(Locs[i]);
+    Writer.AddSourceLocation(Locs[i], Record);
 
   Code = serialization::EXPR_OBJC_MESSAGE_EXPR;
 }
 
 void ASTStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
   VisitStmt(S);
-  Record.AddStmt(S->getElement());
-  Record.AddStmt(S->getCollection());
-  Record.AddStmt(S->getBody());
-  Record.AddSourceLocation(S->getForLoc());
-  Record.AddSourceLocation(S->getRParenLoc());
+  Writer.AddStmt(S->getElement());
+  Writer.AddStmt(S->getCollection());
+  Writer.AddStmt(S->getBody());
+  Writer.AddSourceLocation(S->getForLoc(), Record);
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
   Code = serialization::STMT_OBJC_FOR_COLLECTION;
 }
 
 void ASTStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
-  Record.AddStmt(S->getCatchBody());
-  Record.AddDeclRef(S->getCatchParamDecl());
-  Record.AddSourceLocation(S->getAtCatchLoc());
-  Record.AddSourceLocation(S->getRParenLoc());
+  Writer.AddStmt(S->getCatchBody());
+  Writer.AddDeclRef(S->getCatchParamDecl(), Record);
+  Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
   Code = serialization::STMT_OBJC_CATCH;
 }
 
 void ASTStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
-  Record.AddStmt(S->getFinallyBody());
-  Record.AddSourceLocation(S->getAtFinallyLoc());
+  Writer.AddStmt(S->getFinallyBody());
+  Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
   Code = serialization::STMT_OBJC_FINALLY;
 }
 
 void ASTStmtWriter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
-  Record.AddStmt(S->getSubStmt());
-  Record.AddSourceLocation(S->getAtLoc());
+  Writer.AddStmt(S->getSubStmt());
+  Writer.AddSourceLocation(S->getAtLoc(), Record);
   Code = serialization::STMT_OBJC_AUTORELEASE_POOL;
 }
 
 void ASTStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
   Record.push_back(S->getNumCatchStmts());
   Record.push_back(S->getFinallyStmt() != nullptr);
-  Record.AddStmt(S->getTryBody());
+  Writer.AddStmt(S->getTryBody());
   for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I)
-    Record.AddStmt(S->getCatchStmt(I));
+    Writer.AddStmt(S->getCatchStmt(I));
   if (S->getFinallyStmt())
-    Record.AddStmt(S->getFinallyStmt());
-  Record.AddSourceLocation(S->getAtTryLoc());
+    Writer.AddStmt(S->getFinallyStmt());
+  Writer.AddSourceLocation(S->getAtTryLoc(), Record);
   Code = serialization::STMT_OBJC_AT_TRY;
 }
 
 void ASTStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
-  Record.AddStmt(S->getSynchExpr());
-  Record.AddStmt(S->getSynchBody());
-  Record.AddSourceLocation(S->getAtSynchronizedLoc());
+  Writer.AddStmt(S->getSynchExpr());
+  Writer.AddStmt(S->getSynchBody());
+  Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
   Code = serialization::STMT_OBJC_AT_SYNCHRONIZED;
 }
 
 void ASTStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
-  Record.AddStmt(S->getThrowExpr());
-  Record.AddSourceLocation(S->getThrowLoc());
+  Writer.AddStmt(S->getThrowExpr());
+  Writer.AddSourceLocation(S->getThrowLoc(), Record);
   Code = serialization::STMT_OBJC_AT_THROW;
 }
 
 void ASTStmtWriter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) {
   VisitExpr(E);
   Record.push_back(E->getValue());
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Code = serialization::EXPR_OBJC_BOOL_LITERAL;
 }
 
@@ -1148,52 +1138,52 @@ void ASTStmtWriter::VisitObjCBoolLiteral
 
 void ASTStmtWriter::VisitCXXCatchStmt(CXXCatchStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getCatchLoc());
-  Record.AddDeclRef(S->getExceptionDecl());
-  Record.AddStmt(S->getHandlerBlock());
+  Writer.AddSourceLocation(S->getCatchLoc(), Record);
+  Writer.AddDeclRef(S->getExceptionDecl(), Record);
+  Writer.AddStmt(S->getHandlerBlock());
   Code = serialization::STMT_CXX_CATCH;
 }
 
 void ASTStmtWriter::VisitCXXTryStmt(CXXTryStmt *S) {
   VisitStmt(S);
   Record.push_back(S->getNumHandlers());
-  Record.AddSourceLocation(S->getTryLoc());
-  Record.AddStmt(S->getTryBlock());
+  Writer.AddSourceLocation(S->getTryLoc(), Record);
+  Writer.AddStmt(S->getTryBlock());
   for (unsigned i = 0, e = S->getNumHandlers(); i != e; ++i)
-    Record.AddStmt(S->getHandler(i));
+    Writer.AddStmt(S->getHandler(i));
   Code = serialization::STMT_CXX_TRY;
 }
 
 void ASTStmtWriter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getForLoc());
-  Record.AddSourceLocation(S->getCoawaitLoc());
-  Record.AddSourceLocation(S->getColonLoc());
-  Record.AddSourceLocation(S->getRParenLoc());
-  Record.AddStmt(S->getRangeStmt());
-  Record.AddStmt(S->getBeginStmt());
-  Record.AddStmt(S->getEndStmt());
-  Record.AddStmt(S->getCond());
-  Record.AddStmt(S->getInc());
-  Record.AddStmt(S->getLoopVarStmt());
-  Record.AddStmt(S->getBody());
+  Writer.AddSourceLocation(S->getForLoc(), Record);
+  Writer.AddSourceLocation(S->getCoawaitLoc(), Record);
+  Writer.AddSourceLocation(S->getColonLoc(), Record);
+  Writer.AddSourceLocation(S->getRParenLoc(), Record);
+  Writer.AddStmt(S->getRangeStmt());
+  Writer.AddStmt(S->getBeginStmt());
+  Writer.AddStmt(S->getEndStmt());
+  Writer.AddStmt(S->getCond());
+  Writer.AddStmt(S->getInc());
+  Writer.AddStmt(S->getLoopVarStmt());
+  Writer.AddStmt(S->getBody());
   Code = serialization::STMT_CXX_FOR_RANGE;
 }
 
 void ASTStmtWriter::VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getKeywordLoc());
+  Writer.AddSourceLocation(S->getKeywordLoc(), Record);
   Record.push_back(S->isIfExists());
-  Record.AddNestedNameSpecifierLoc(S->getQualifierLoc());
-  Record.AddDeclarationNameInfo(S->getNameInfo());
-  Record.AddStmt(S->getSubStmt());
+  Writer.AddNestedNameSpecifierLoc(S->getQualifierLoc(), Record);
+  Writer.AddDeclarationNameInfo(S->getNameInfo(), Record);
+  Writer.AddStmt(S->getSubStmt());
   Code = serialization::STMT_MS_DEPENDENT_EXISTS;
 }
 
 void ASTStmtWriter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
   VisitCallExpr(E);
   Record.push_back(E->getOperator());
-  Record.AddSourceRange(E->Range);
+  Writer.AddSourceRange(E->Range, Record);
   Record.push_back(E->isFPContractable());
   Code = serialization::EXPR_CXX_OPERATOR_CALL;
 }
@@ -1207,22 +1197,22 @@ void ASTStmtWriter::VisitCXXConstructExp
   VisitExpr(E);
   Record.push_back(E->getNumArgs());
   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
-    Record.AddStmt(E->getArg(I));
-  Record.AddDeclRef(E->getConstructor());
-  Record.AddSourceLocation(E->getLocation());
+    Writer.AddStmt(E->getArg(I));
+  Writer.AddDeclRef(E->getConstructor(), Record);
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Record.push_back(E->isElidable());
   Record.push_back(E->hadMultipleCandidates());
   Record.push_back(E->isListInitialization());
   Record.push_back(E->isStdInitListInitialization());
   Record.push_back(E->requiresZeroInitialization());
   Record.push_back(E->getConstructionKind()); // FIXME: stable encoding
-  Record.AddSourceRange(E->getParenOrBraceRange());
+  Writer.AddSourceRange(E->getParenOrBraceRange(), Record);
   Code = serialization::EXPR_CXX_CONSTRUCT;
 }
 
 void ASTStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
   VisitCXXConstructExpr(E);
-  Record.AddTypeSourceInfo(E->getTypeSourceInfo());
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
   Code = serialization::EXPR_CXX_TEMPORARY_OBJECT;
 }
 
@@ -1233,18 +1223,18 @@ void ASTStmtWriter::VisitLambdaExpr(Lamb
   if (E->HasArrayIndexVars)
     NumArrayIndexVars = E->getArrayIndexStarts()[E->NumCaptures];
   Record.push_back(NumArrayIndexVars);
-  Record.AddSourceRange(E->IntroducerRange);
+  Writer.AddSourceRange(E->IntroducerRange, Record);
   Record.push_back(E->CaptureDefault); // FIXME: stable encoding
-  Record.AddSourceLocation(E->CaptureDefaultLoc);
+  Writer.AddSourceLocation(E->CaptureDefaultLoc, Record);
   Record.push_back(E->ExplicitParams);
   Record.push_back(E->ExplicitResultType);
-  Record.AddSourceLocation(E->ClosingBrace);
+  Writer.AddSourceLocation(E->ClosingBrace, Record);
   
   // Add capture initializers.
   for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
                                       CEnd = E->capture_init_end();
        C != CEnd; ++C) {
-    Record.AddStmt(*C);
+    Writer.AddStmt(*C);
   }
   
   // Add array index variables, if any.
@@ -1253,7 +1243,7 @@ void ASTStmtWriter::VisitLambdaExpr(Lamb
                   E->getArrayIndexStarts() + E->NumCaptures + 1);
     VarDecl **ArrayIndexVars = E->getArrayIndexVars();
     for (unsigned I = 0; I != NumArrayIndexVars; ++I)
-      Record.AddDeclRef(ArrayIndexVars[I]);
+      Writer.AddDeclRef(ArrayIndexVars[I], Record);
   }
   
   Code = serialization::EXPR_LAMBDA;
@@ -1261,14 +1251,15 @@ void ASTStmtWriter::VisitLambdaExpr(Lamb
 
 void ASTStmtWriter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddStmt(E->getSubExpr());
   Code = serialization::EXPR_CXX_STD_INITIALIZER_LIST;
 }
 
 void ASTStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
   VisitExplicitCastExpr(E);
-  Record.AddSourceRange(SourceRange(E->getOperatorLoc(), E->getRParenLoc()));
-  Record.AddSourceRange(E->getAngleBrackets());
+  Writer.AddSourceRange(SourceRange(E->getOperatorLoc(), E->getRParenLoc()),
+                        Record);
+  Writer.AddSourceRange(E->getAngleBrackets(), Record);
 }
 
 void ASTStmtWriter::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) {
@@ -1293,82 +1284,82 @@ void ASTStmtWriter::VisitCXXConstCastExp
 
 void ASTStmtWriter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) {
   VisitExplicitCastExpr(E);
-  Record.AddSourceLocation(E->getLParenLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_CXX_FUNCTIONAL_CAST;
 }
 
 void ASTStmtWriter::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
   VisitCallExpr(E);
-  Record.AddSourceLocation(E->UDSuffixLoc);
+  Writer.AddSourceLocation(E->UDSuffixLoc, Record);
   Code = serialization::EXPR_USER_DEFINED_LITERAL;
 }
 
 void ASTStmtWriter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
   VisitExpr(E);
   Record.push_back(E->getValue());
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Code = serialization::EXPR_CXX_BOOL_LITERAL;
 }
 
 void ASTStmtWriter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Code = serialization::EXPR_CXX_NULL_PTR_LITERAL;
 }
 
 void ASTStmtWriter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
   VisitExpr(E);
-  Record.AddSourceRange(E->getSourceRange());
+  Writer.AddSourceRange(E->getSourceRange(), Record);
   if (E->isTypeOperand()) {
-    Record.AddTypeSourceInfo(E->getTypeOperandSourceInfo());
+    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
     Code = serialization::EXPR_CXX_TYPEID_TYPE;
   } else {
-    Record.AddStmt(E->getExprOperand());
+    Writer.AddStmt(E->getExprOperand());
     Code = serialization::EXPR_CXX_TYPEID_EXPR;
   }
 }
 
 void ASTStmtWriter::VisitCXXThisExpr(CXXThisExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Record.push_back(E->isImplicit());
   Code = serialization::EXPR_CXX_THIS;
 }
 
 void ASTStmtWriter::VisitCXXThrowExpr(CXXThrowExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getThrowLoc());
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddSourceLocation(E->getThrowLoc(), Record);
+  Writer.AddStmt(E->getSubExpr());
   Record.push_back(E->isThrownVariableInScope());
   Code = serialization::EXPR_CXX_THROW;
 }
 
 void ASTStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getParam());
-  Record.AddSourceLocation(E->getUsedLocation());
+  Writer.AddDeclRef(E->getParam(), Record);
+  Writer.AddSourceLocation(E->getUsedLocation(), Record);
   Code = serialization::EXPR_CXX_DEFAULT_ARG;
 }
 
 void ASTStmtWriter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getField());
-  Record.AddSourceLocation(E->getExprLoc());
+  Writer.AddDeclRef(E->getField(), Record);
+  Writer.AddSourceLocation(E->getExprLoc(), Record);
   Code = serialization::EXPR_CXX_DEFAULT_INIT;
 }
 
 void ASTStmtWriter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
   VisitExpr(E);
-  Record.AddCXXTemporary(E->getTemporary());
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddCXXTemporary(E->getTemporary(), Record);
+  Writer.AddStmt(E->getSubExpr());
   Code = serialization::EXPR_CXX_BIND_TEMPORARY;
 }
 
 void ASTStmtWriter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
   VisitExpr(E);
-  Record.AddTypeSourceInfo(E->getTypeSourceInfo());
-  Record.AddSourceLocation(E->getRParenLoc());
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_CXX_SCALAR_VALUE_INIT;
 }
 
@@ -1379,15 +1370,15 @@ void ASTStmtWriter::VisitCXXNewExpr(CXXN
   Record.push_back(E->doesUsualArrayDeleteWantSize());
   Record.push_back(E->getNumPlacementArgs());
   Record.push_back(E->StoredInitializationStyle);
-  Record.AddDeclRef(E->getOperatorNew());
-  Record.AddDeclRef(E->getOperatorDelete());
-  Record.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo());
-  Record.AddSourceRange(E->getTypeIdParens());
-  Record.AddSourceRange(E->getSourceRange());
-  Record.AddSourceRange(E->getDirectInitRange());
+  Writer.AddDeclRef(E->getOperatorNew(), Record);
+  Writer.AddDeclRef(E->getOperatorDelete(), Record);
+  Writer.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo(), Record);
+  Writer.AddSourceRange(E->getTypeIdParens(), Record);
+  Writer.AddSourceRange(E->getSourceRange(), Record);
+  Writer.AddSourceRange(E->getDirectInitRange(), Record);
   for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end();
        I != e; ++I)
-    Record.AddStmt(*I);
+    Writer.AddStmt(*I);
 
   Code = serialization::EXPR_CXX_NEW;
 }
@@ -1398,9 +1389,9 @@ void ASTStmtWriter::VisitCXXDeleteExpr(C
   Record.push_back(E->isArrayForm());
   Record.push_back(E->isArrayFormAsWritten());
   Record.push_back(E->doesUsualArrayDeleteWantSize());
-  Record.AddDeclRef(E->getOperatorDelete());
-  Record.AddStmt(E->getArgument());
-  Record.AddSourceLocation(E->getSourceRange().getBegin());
+  Writer.AddDeclRef(E->getOperatorDelete(), Record);
+  Writer.AddStmt(E->getArgument());
+  Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record);
   
   Code = serialization::EXPR_CXX_DELETE;
 }
@@ -1408,20 +1399,20 @@ void ASTStmtWriter::VisitCXXDeleteExpr(C
 void ASTStmtWriter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
   VisitExpr(E);
 
-  Record.AddStmt(E->getBase());
+  Writer.AddStmt(E->getBase());
   Record.push_back(E->isArrow());
-  Record.AddSourceLocation(E->getOperatorLoc());
-  Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
-  Record.AddTypeSourceInfo(E->getScopeTypeInfo());
-  Record.AddSourceLocation(E->getColonColonLoc());
-  Record.AddSourceLocation(E->getTildeLoc());
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
+  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
+  Writer.AddTypeSourceInfo(E->getScopeTypeInfo(), Record);
+  Writer.AddSourceLocation(E->getColonColonLoc(), Record);
+  Writer.AddSourceLocation(E->getTildeLoc(), Record);
 
   // PseudoDestructorTypeStorage.
-  Record.AddIdentifierRef(E->getDestroyedTypeIdentifier());
+  Writer.AddIdentifierRef(E->getDestroyedTypeIdentifier(), Record);
   if (E->getDestroyedTypeIdentifier())
-    Record.AddSourceLocation(E->getDestroyedTypeLoc());
+    Writer.AddSourceLocation(E->getDestroyedTypeLoc(), Record);
   else
-    Record.AddTypeSourceInfo(E->getDestroyedTypeInfo());
+    Writer.AddTypeSourceInfo(E->getDestroyedTypeInfo(), Record);
 
   Code = serialization::EXPR_CXX_PSEUDO_DESTRUCTOR;
 }
@@ -1430,9 +1421,9 @@ void ASTStmtWriter::VisitExprWithCleanup
   VisitExpr(E);
   Record.push_back(E->getNumObjects());
   for (unsigned i = 0, e = E->getNumObjects(); i != e; ++i)
-    Record.AddDeclRef(E->getObject(i));
+    Writer.AddDeclRef(E->getObject(i), Record);
   
-  Record.AddStmt(E->getSubExpr());
+  Writer.AddStmt(E->getSubExpr());
   Code = serialization::EXPR_EXPR_WITH_CLEANUPS;
 }
 
@@ -1453,15 +1444,15 @@ ASTStmtWriter::VisitCXXDependentScopeMem
   }
 
   if (!E->isImplicitAccess())
-    Record.AddStmt(E->getBase());
+    Writer.AddStmt(E->getBase());
   else
-    Record.AddStmt(nullptr);
-  Record.AddTypeRef(E->getBaseType());
+    Writer.AddStmt(nullptr);
+  Writer.AddTypeRef(E->getBaseType(), Record);
   Record.push_back(E->isArrow());
-  Record.AddSourceLocation(E->getOperatorLoc());
-  Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
-  Record.AddDeclRef(E->getFirstQualifierFoundInScope());
-  Record.AddDeclarationNameInfo(E->MemberNameInfo);
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
+  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
+  Writer.AddDeclRef(E->getFirstQualifierFoundInScope(), Record);
+  Writer.AddDeclarationNameInfo(E->MemberNameInfo, Record);
   Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_MEMBER;
 }
 
@@ -1481,8 +1472,8 @@ ASTStmtWriter::VisitDependentScopeDeclRe
                              E->getTrailingObjects<TemplateArgumentLoc>());
   }
 
-  Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
-  Record.AddDeclarationNameInfo(E->NameInfo);
+  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
+  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
   Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF;
 }
 
@@ -1492,10 +1483,10 @@ ASTStmtWriter::VisitCXXUnresolvedConstru
   Record.push_back(E->arg_size());
   for (CXXUnresolvedConstructExpr::arg_iterator
          ArgI = E->arg_begin(), ArgE = E->arg_end(); ArgI != ArgE; ++ArgI)
-    Record.AddStmt(*ArgI);
-  Record.AddTypeSourceInfo(E->getTypeSourceInfo());
-  Record.AddSourceLocation(E->getLParenLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
+    Writer.AddStmt(*ArgI);
+  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
+  Writer.AddSourceLocation(E->getLParenLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
   Code = serialization::EXPR_CXX_UNRESOLVED_CONSTRUCT;
 }
 
@@ -1516,21 +1507,21 @@ void ASTStmtWriter::VisitOverloadExpr(Ov
   Record.push_back(E->getNumDecls());
   for (OverloadExpr::decls_iterator
          OvI = E->decls_begin(), OvE = E->decls_end(); OvI != OvE; ++OvI) {
-    Record.AddDeclRef(OvI.getDecl());
+    Writer.AddDeclRef(OvI.getDecl(), Record);
     Record.push_back(OvI.getAccess());
   }
 
-  Record.AddDeclarationNameInfo(E->NameInfo);
-  Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
+  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
+  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
 }
 
 void ASTStmtWriter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
   VisitOverloadExpr(E);
   Record.push_back(E->isArrow());
   Record.push_back(E->hasUnresolvedUsing());
-  Record.AddStmt(!E->isImplicitAccess() ? E->getBase() : nullptr);
-  Record.AddTypeRef(E->getBaseType());
-  Record.AddSourceLocation(E->getOperatorLoc());
+  Writer.AddStmt(!E->isImplicitAccess() ? E->getBase() : nullptr);
+  Writer.AddTypeRef(E->getBaseType(), Record);
+  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
   Code = serialization::EXPR_CXX_UNRESOLVED_MEMBER;
 }
 
@@ -1538,7 +1529,7 @@ void ASTStmtWriter::VisitUnresolvedLooku
   VisitOverloadExpr(E);
   Record.push_back(E->requiresADL());
   Record.push_back(E->isOverloaded());
-  Record.AddDeclRef(E->getNamingClass());
+  Writer.AddDeclRef(E->getNamingClass(), Record);
   Code = serialization::EXPR_CXX_UNRESOLVED_LOOKUP;
 }
 
@@ -1547,9 +1538,9 @@ void ASTStmtWriter::VisitTypeTraitExpr(T
   Record.push_back(E->TypeTraitExprBits.NumArgs);
   Record.push_back(E->TypeTraitExprBits.Kind); // FIXME: Stable encoding
   Record.push_back(E->TypeTraitExprBits.Value);
-  Record.AddSourceRange(E->getSourceRange());
+  Writer.AddSourceRange(E->getSourceRange(), Record);
   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
-    Record.AddTypeSourceInfo(E->getArg(I));
+    Writer.AddTypeSourceInfo(E->getArg(I), Record);
   Code = serialization::EXPR_TYPE_TRAIT;
 }
 
@@ -1557,8 +1548,8 @@ void ASTStmtWriter::VisitArrayTypeTraitE
   VisitExpr(E);
   Record.push_back(E->getTrait());
   Record.push_back(E->getValue());
-  Record.AddSourceRange(E->getSourceRange());
-  Record.AddTypeSourceInfo(E->getQueriedTypeSourceInfo());
+  Writer.AddSourceRange(E->getSourceRange(), Record);
+  Writer.AddTypeSourceInfo(E->getQueriedTypeSourceInfo(), Record);
   Code = serialization::EXPR_ARRAY_TYPE_TRAIT;
 }
 
@@ -1566,24 +1557,24 @@ void ASTStmtWriter::VisitExpressionTrait
   VisitExpr(E);
   Record.push_back(E->getTrait());
   Record.push_back(E->getValue());
-  Record.AddSourceRange(E->getSourceRange());
-  Record.AddStmt(E->getQueriedExpression());
+  Writer.AddSourceRange(E->getSourceRange(), Record);
+  Writer.AddStmt(E->getQueriedExpression());
   Code = serialization::EXPR_CXX_EXPRESSION_TRAIT;
 }
 
 void ASTStmtWriter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
   VisitExpr(E);
   Record.push_back(E->getValue());
-  Record.AddSourceRange(E->getSourceRange());
-  Record.AddStmt(E->getOperand());
+  Writer.AddSourceRange(E->getSourceRange(), Record);
+  Writer.AddStmt(E->getOperand());
   Code = serialization::EXPR_CXX_NOEXCEPT;
 }
 
 void ASTStmtWriter::VisitPackExpansionExpr(PackExpansionExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getEllipsisLoc());
+  Writer.AddSourceLocation(E->getEllipsisLoc(), Record);
   Record.push_back(E->NumExpansions);
-  Record.AddStmt(E->getPattern());
+  Writer.AddStmt(E->getPattern());
   Code = serialization::EXPR_PACK_EXPANSION;
 }
 
@@ -1591,13 +1582,13 @@ void ASTStmtWriter::VisitSizeOfPackExpr(
   VisitExpr(E);
   Record.push_back(E->isPartiallySubstituted() ? E->getPartialArguments().size()
                                                : 0);
-  Record.AddSourceLocation(E->OperatorLoc);
-  Record.AddSourceLocation(E->PackLoc);
-  Record.AddSourceLocation(E->RParenLoc);
-  Record.AddDeclRef(E->Pack);
+  Writer.AddSourceLocation(E->OperatorLoc, Record);
+  Writer.AddSourceLocation(E->PackLoc, Record);
+  Writer.AddSourceLocation(E->RParenLoc, Record);
+  Writer.AddDeclRef(E->Pack, Record);
   if (E->isPartiallySubstituted()) {
     for (const auto &TA : E->getPartialArguments())
-      Record.AddTemplateArgument(TA);
+      Writer.AddTemplateArgument(TA, Record);
   } else if (!E->isValueDependent()) {
     Record.push_back(E->getPackLength());
   }
@@ -1607,55 +1598,55 @@ void ASTStmtWriter::VisitSizeOfPackExpr(
 void ASTStmtWriter::VisitSubstNonTypeTemplateParmExpr(
                                               SubstNonTypeTemplateParmExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getParameter());
-  Record.AddSourceLocation(E->getNameLoc());
-  Record.AddStmt(E->getReplacement());
+  Writer.AddDeclRef(E->getParameter(), Record);
+  Writer.AddSourceLocation(E->getNameLoc(), Record);
+  Writer.AddStmt(E->getReplacement());
   Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM;
 }
 
 void ASTStmtWriter::VisitSubstNonTypeTemplateParmPackExpr(
                                           SubstNonTypeTemplateParmPackExpr *E) {
   VisitExpr(E);
-  Record.AddDeclRef(E->getParameterPack());
-  Record.AddTemplateArgument(E->getArgumentPack());
-  Record.AddSourceLocation(E->getParameterPackLocation());
+  Writer.AddDeclRef(E->getParameterPack(), Record);
+  Writer.AddTemplateArgument(E->getArgumentPack(), Record);
+  Writer.AddSourceLocation(E->getParameterPackLocation(), Record);
   Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK;
 }
 
 void ASTStmtWriter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
   VisitExpr(E);
   Record.push_back(E->getNumExpansions());
-  Record.AddDeclRef(E->getParameterPack());
-  Record.AddSourceLocation(E->getParameterPackLocation());
+  Writer.AddDeclRef(E->getParameterPack(), Record);
+  Writer.AddSourceLocation(E->getParameterPackLocation(), Record);
   for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end();
        I != End; ++I)
-    Record.AddDeclRef(*I);
+    Writer.AddDeclRef(*I, Record);
   Code = serialization::EXPR_FUNCTION_PARM_PACK;
 }
 
 void ASTStmtWriter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getTemporary());
-  Record.AddDeclRef(E->getExtendingDecl());
+  Writer.AddStmt(E->getTemporary());
+  Writer.AddDeclRef(E->getExtendingDecl(), Record);
   Record.push_back(E->getManglingNumber());
   Code = serialization::EXPR_MATERIALIZE_TEMPORARY;
 }
 
 void ASTStmtWriter::VisitCXXFoldExpr(CXXFoldExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->LParenLoc);
-  Record.AddSourceLocation(E->EllipsisLoc);
-  Record.AddSourceLocation(E->RParenLoc);
-  Record.AddStmt(E->SubExprs[0]);
-  Record.AddStmt(E->SubExprs[1]);
+  Writer.AddSourceLocation(E->LParenLoc, Record);
+  Writer.AddSourceLocation(E->EllipsisLoc, Record);
+  Writer.AddSourceLocation(E->RParenLoc, Record);
+  Writer.AddStmt(E->SubExprs[0]);
+  Writer.AddStmt(E->SubExprs[1]);
   Record.push_back(E->Opcode);
   Code = serialization::EXPR_CXX_FOLD;
 }
 
 void ASTStmtWriter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getSourceExpr());
-  Record.AddSourceLocation(E->getLocation());
+  Writer.AddStmt(E->getSourceExpr());
+  Writer.AddSourceLocation(E->getLocation(), Record);
   Code = serialization::EXPR_OPAQUE_VALUE;
 }
 
@@ -1671,7 +1662,7 @@ void ASTStmtWriter::VisitTypoExpr(TypoEx
 
 void ASTStmtWriter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
   VisitCallExpr(E);
-  Record.AddStmt(E->getConfig());
+  Writer.AddStmt(E->getConfig());
   Code = serialization::EXPR_CUDA_KERNEL_CALL;
 }
 
@@ -1680,9 +1671,9 @@ void ASTStmtWriter::VisitCUDAKernelCallE
 //===----------------------------------------------------------------------===//
 void ASTStmtWriter::VisitAsTypeExpr(AsTypeExpr *E) {
   VisitExpr(E);
-  Record.AddSourceLocation(E->getBuiltinLoc());
-  Record.AddSourceLocation(E->getRParenLoc());
-  Record.AddStmt(E->getSrcExpr());
+  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
+  Writer.AddSourceLocation(E->getRParenLoc(), Record);
+  Writer.AddStmt(E->getSrcExpr());
   Code = serialization::EXPR_ASTYPE;
 }
 
@@ -1692,61 +1683,61 @@ void ASTStmtWriter::VisitAsTypeExpr(AsTy
 void ASTStmtWriter::VisitMSPropertyRefExpr(MSPropertyRefExpr *E) {
   VisitExpr(E);
   Record.push_back(E->isArrow());
-  Record.AddStmt(E->getBaseExpr());
-  Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
-  Record.AddSourceLocation(E->getMemberLoc());
-  Record.AddDeclRef(E->getPropertyDecl());
+  Writer.AddStmt(E->getBaseExpr());
+  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
+  Writer.AddSourceLocation(E->getMemberLoc(), Record);
+  Writer.AddDeclRef(E->getPropertyDecl(), Record);
   Code = serialization::EXPR_CXX_PROPERTY_REF_EXPR;
 }
 
 void ASTStmtWriter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *E) {
   VisitExpr(E);
-  Record.AddStmt(E->getBase());
-  Record.AddStmt(E->getIdx());
-  Record.AddSourceLocation(E->getRBracketLoc());
+  Writer.AddStmt(E->getBase());
+  Writer.AddStmt(E->getIdx());
+  Writer.AddSourceLocation(E->getRBracketLoc(), Record);
   Code = serialization::EXPR_CXX_PROPERTY_SUBSCRIPT_EXPR;
 }
 
 void ASTStmtWriter::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
   VisitExpr(E);
-  Record.AddSourceRange(E->getSourceRange());
-  Record.AddString(E->getUuidStr());
+  Writer.AddSourceRange(E->getSourceRange(), Record);
+  Writer.AddString(E->getUuidStr(), Record);
   if (E->isTypeOperand()) {
-    Record.AddTypeSourceInfo(E->getTypeOperandSourceInfo());
+    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
     Code = serialization::EXPR_CXX_UUIDOF_TYPE;
   } else {
-    Record.AddStmt(E->getExprOperand());
+    Writer.AddStmt(E->getExprOperand());
     Code = serialization::EXPR_CXX_UUIDOF_EXPR;
   }
 }
 
 void ASTStmtWriter::VisitSEHExceptStmt(SEHExceptStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getExceptLoc());
-  Record.AddStmt(S->getFilterExpr());
-  Record.AddStmt(S->getBlock());
+  Writer.AddSourceLocation(S->getExceptLoc(), Record);
+  Writer.AddStmt(S->getFilterExpr());
+  Writer.AddStmt(S->getBlock());
   Code = serialization::STMT_SEH_EXCEPT;
 }
 
 void ASTStmtWriter::VisitSEHFinallyStmt(SEHFinallyStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getFinallyLoc());
-  Record.AddStmt(S->getBlock());
+  Writer.AddSourceLocation(S->getFinallyLoc(), Record);
+  Writer.AddStmt(S->getBlock());
   Code = serialization::STMT_SEH_FINALLY;
 }
 
 void ASTStmtWriter::VisitSEHTryStmt(SEHTryStmt *S) {
   VisitStmt(S);
   Record.push_back(S->getIsCXXTry());
-  Record.AddSourceLocation(S->getTryLoc());
-  Record.AddStmt(S->getTryBlock());
-  Record.AddStmt(S->getHandler());
+  Writer.AddSourceLocation(S->getTryLoc(), Record);
+  Writer.AddStmt(S->getTryBlock());
+  Writer.AddStmt(S->getHandler());
   Code = serialization::STMT_SEH_TRY;
 }
 
 void ASTStmtWriter::VisitSEHLeaveStmt(SEHLeaveStmt *S) {
   VisitStmt(S);
-  Record.AddSourceLocation(S->getLeaveLoc());
+  Writer.AddSourceLocation(S->getLeaveLoc(), Record);
   Code = serialization::STMT_SEH_LEAVE;
 }
 
@@ -1756,9 +1747,11 @@ void ASTStmtWriter::VisitSEHLeaveStmt(SE
 
 namespace clang {
 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
-  ASTRecordWriter &Record;
+  ASTStmtWriter *Writer;
+  ASTWriter::RecordData &Record;
 public:
-  OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
+  OMPClauseWriter(ASTStmtWriter *W, ASTWriter::RecordData &Record)
+    : Writer(W), Record(Record) { }
 #define OPENMP_CLAUSE(Name, Class)    \
   void Visit##Class(Class *S);
 #include "clang/Basic/OpenMPKinds.def"
@@ -1771,62 +1764,62 @@ public:
 void OMPClauseWriter::writeClause(OMPClause *C) {
   Record.push_back(C->getClauseKind());
   Visit(C);
-  Record.AddSourceLocation(C->getLocStart());
-  Record.AddSourceLocation(C->getLocEnd());
+  Writer->Writer.AddSourceLocation(C->getLocStart(), Record);
+  Writer->Writer.AddSourceLocation(C->getLocEnd(), Record);
 }
 
 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
-  Record.AddStmt(C->getPreInitStmt());
+  Writer->Writer.AddStmt(C->getPreInitStmt());
 }
 
 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
   VisitOMPClauseWithPreInit(C);
-  Record.AddStmt(C->getPostUpdateExpr());
+  Writer->Writer.AddStmt(C->getPostUpdateExpr());
 }
 
 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
   Record.push_back(C->getNameModifier());
-  Record.AddSourceLocation(C->getNameModifierLoc());
-  Record.AddSourceLocation(C->getColonLoc());
-  Record.AddStmt(C->getCondition());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getNameModifierLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getColonLoc(), Record);
+  Writer->Writer.AddStmt(C->getCondition());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
-  Record.AddStmt(C->getCondition());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getCondition());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
-  Record.AddStmt(C->getNumThreads());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getNumThreads());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
-  Record.AddStmt(C->getSafelen());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getSafelen());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
-  Record.AddStmt(C->getSimdlen());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getSimdlen());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
-  Record.AddStmt(C->getNumForLoops());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getNumForLoops());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
   Record.push_back(C->getDefaultKind());
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getDefaultKindKwLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getDefaultKindKwLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
   Record.push_back(C->getProcBindKind());
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getProcBindKindKwLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getProcBindKindKwLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
@@ -1834,17 +1827,17 @@ void OMPClauseWriter::VisitOMPScheduleCl
   Record.push_back(C->getScheduleKind());
   Record.push_back(C->getFirstScheduleModifier());
   Record.push_back(C->getSecondScheduleModifier());
-  Record.AddStmt(C->getChunkSize());
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
-  Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
-  Record.AddSourceLocation(C->getScheduleKindLoc());
-  Record.AddSourceLocation(C->getCommaLoc());
+  Writer->Writer.AddStmt(C->getChunkSize());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getFirstScheduleModifierLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getSecondScheduleModifierLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getScheduleKindLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getCommaLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
-  Record.AddStmt(C->getNumForLoops());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getNumForLoops());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
@@ -1871,225 +1864,225 @@ void OMPClauseWriter::VisitOMPNogroupCla
 
 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->private_copies()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
 }
 
 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
   Record.push_back(C->varlist_size());
   VisitOMPClauseWithPreInit(C);
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->private_copies()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->inits()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
 }
 
 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
   Record.push_back(C->varlist_size());
   VisitOMPClauseWithPostUpdate(C);
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   for (auto *E : C->private_copies())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->source_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->destination_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->assignment_ops())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
 }
 
 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
 }
 
 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
   Record.push_back(C->varlist_size());
   VisitOMPClauseWithPostUpdate(C);
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getColonLoc());
-  Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
-  Record.AddDeclarationNameInfo(C->getNameInfo());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getColonLoc(), Record);
+  Writer->Writer.AddNestedNameSpecifierLoc(C->getQualifierLoc(), Record);
+  Writer->Writer.AddDeclarationNameInfo(C->getNameInfo(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   for (auto *VE : C->privates())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   for (auto *E : C->lhs_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->rhs_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->reduction_ops())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
 }
 
 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
   Record.push_back(C->varlist_size());
   VisitOMPClauseWithPostUpdate(C);
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getColonLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getColonLoc(), Record);
   Record.push_back(C->getModifier());
-  Record.AddSourceLocation(C->getModifierLoc());
+  Writer->Writer.AddSourceLocation(C->getModifierLoc(), Record);
   for (auto *VE : C->varlists()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->privates()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->inits()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->updates()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
   for (auto *VE : C->finals()) {
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   }
-  Record.AddStmt(C->getStep());
-  Record.AddStmt(C->getCalcStep());
+  Writer->Writer.AddStmt(C->getStep());
+  Writer->Writer.AddStmt(C->getCalcStep());
 }
 
 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getColonLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getColonLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
-  Record.AddStmt(C->getAlignment());
+    Writer->Writer.AddStmt(VE);
+  Writer->Writer.AddStmt(C->getAlignment());
 }
 
 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   for (auto *E : C->source_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->destination_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->assignment_ops())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
 }
 
 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
   for (auto *E : C->source_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->destination_exprs())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
   for (auto *E : C->assignment_ops())
-    Record.AddStmt(E);
+    Writer->Writer.AddStmt(E);
 }
 
 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
 }
 
 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   Record.push_back(C->getDependencyKind());
-  Record.AddSourceLocation(C->getDependencyLoc());
-  Record.AddSourceLocation(C->getColonLoc());
+  Writer->Writer.AddSourceLocation(C->getDependencyLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getColonLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
 }
 
 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
-  Record.AddStmt(C->getDevice());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getDevice());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
   Record.push_back(C->varlist_size());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
   Record.push_back(C->getMapTypeModifier());
   Record.push_back(C->getMapType());
-  Record.AddSourceLocation(C->getMapLoc());
-  Record.AddSourceLocation(C->getColonLoc());
+  Writer->Writer.AddSourceLocation(C->getMapLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getColonLoc(), Record);
   for (auto *VE : C->varlists())
-    Record.AddStmt(VE);
+    Writer->Writer.AddStmt(VE);
 }
 
 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
-  Record.AddStmt(C->getNumTeams());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getNumTeams());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
-  Record.AddStmt(C->getThreadLimit());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getThreadLimit());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
-  Record.AddStmt(C->getPriority());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getPriority());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
-  Record.AddStmt(C->getGrainsize());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getGrainsize());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
-  Record.AddStmt(C->getNumTasks());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getNumTasks());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
-  Record.AddStmt(C->getHint());
-  Record.AddSourceLocation(C->getLParenLoc());
+  Writer->Writer.AddStmt(C->getHint());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
   VisitOMPClauseWithPreInit(C);
   Record.push_back(C->getDistScheduleKind());
-  Record.AddStmt(C->getChunkSize());
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getDistScheduleKindLoc());
-  Record.AddSourceLocation(C->getCommaLoc());
+  Writer->Writer.AddStmt(C->getChunkSize());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getDistScheduleKindLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getCommaLoc(), Record);
 }
 
 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
   Record.push_back(C->getDefaultmapKind());
   Record.push_back(C->getDefaultmapModifier());
-  Record.AddSourceLocation(C->getLParenLoc());
-  Record.AddSourceLocation(C->getDefaultmapModifierLoc());
-  Record.AddSourceLocation(C->getDefaultmapKindLoc());
+  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getDefaultmapModifierLoc(), Record);
+  Writer->Writer.AddSourceLocation(C->getDefaultmapKindLoc(), Record);
 }
 
 //===----------------------------------------------------------------------===//
 // OpenMP Directives.
 //===----------------------------------------------------------------------===//
 void ASTStmtWriter::VisitOMPExecutableDirective(OMPExecutableDirective *E) {
-  Record.AddSourceLocation(E->getLocStart());
-  Record.AddSourceLocation(E->getLocEnd());
-  OMPClauseWriter ClauseWriter(Record);
+  Writer.AddSourceLocation(E->getLocStart(), Record);
+  Writer.AddSourceLocation(E->getLocEnd(), Record);
+  OMPClauseWriter ClauseWriter(this, Record);
   for (unsigned i = 0; i < E->getNumClauses(); ++i) {
     ClauseWriter.writeClause(E->getClause(i));
   }
   if (E->hasAssociatedStmt())
-    Record.AddStmt(E->getAssociatedStmt());
+    Writer.AddStmt(E->getAssociatedStmt());
 }
 
 void ASTStmtWriter::VisitOMPLoopDirective(OMPLoopDirective *D) {
@@ -2097,39 +2090,39 @@ void ASTStmtWriter::VisitOMPLoopDirectiv
   Record.push_back(D->getNumClauses());
   Record.push_back(D->getCollapsedNumber());
   VisitOMPExecutableDirective(D);
-  Record.AddStmt(D->getIterationVariable());
-  Record.AddStmt(D->getLastIteration());
-  Record.AddStmt(D->getCalcLastIteration());
-  Record.AddStmt(D->getPreCond());
-  Record.AddStmt(D->getCond());
-  Record.AddStmt(D->getInit());
-  Record.AddStmt(D->getInc());
-  Record.AddStmt(D->getPreInits());
+  Writer.AddStmt(D->getIterationVariable());
+  Writer.AddStmt(D->getLastIteration());
+  Writer.AddStmt(D->getCalcLastIteration());
+  Writer.AddStmt(D->getPreCond());
+  Writer.AddStmt(D->getCond());
+  Writer.AddStmt(D->getInit());
+  Writer.AddStmt(D->getInc());
+  Writer.AddStmt(D->getPreInits());
   if (isOpenMPWorksharingDirective(D->getDirectiveKind()) ||
       isOpenMPTaskLoopDirective(D->getDirectiveKind()) ||
       isOpenMPDistributeDirective(D->getDirectiveKind())) {
-    Record.AddStmt(D->getIsLastIterVariable());
-    Record.AddStmt(D->getLowerBoundVariable());
-    Record.AddStmt(D->getUpperBoundVariable());
-    Record.AddStmt(D->getStrideVariable());
-    Record.AddStmt(D->getEnsureUpperBound());
-    Record.AddStmt(D->getNextLowerBound());
-    Record.AddStmt(D->getNextUpperBound());
+    Writer.AddStmt(D->getIsLastIterVariable());
+    Writer.AddStmt(D->getLowerBoundVariable());
+    Writer.AddStmt(D->getUpperBoundVariable());
+    Writer.AddStmt(D->getStrideVariable());
+    Writer.AddStmt(D->getEnsureUpperBound());
+    Writer.AddStmt(D->getNextLowerBound());
+    Writer.AddStmt(D->getNextUpperBound());
   }
   for (auto I : D->counters()) {
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
   }
   for (auto I : D->private_counters()) {
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
   }
   for (auto I : D->inits()) {
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
   }
   for (auto I : D->updates()) {
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
   }
   for (auto I : D->finals()) {
-    Record.AddStmt(I);
+    Writer.AddStmt(I);
   }
 }
 
@@ -2189,7 +2182,7 @@ void ASTStmtWriter::VisitOMPCriticalDire
   VisitStmt(D);
   Record.push_back(D->getNumClauses());
   VisitOMPExecutableDirective(D);
-  Record.AddDeclarationNameInfo(D->getDirectiveName());
+  Writer.AddDeclarationNameInfo(D->getDirectiveName(), Record);
   Code = serialization::STMT_OMP_CRITICAL_DIRECTIVE;
 }
 
@@ -2226,10 +2219,10 @@ void ASTStmtWriter::VisitOMPAtomicDirect
   VisitStmt(D);
   Record.push_back(D->getNumClauses());
   VisitOMPExecutableDirective(D);
-  Record.AddStmt(D->getX());
-  Record.AddStmt(D->getV());
-  Record.AddStmt(D->getExpr());
-  Record.AddStmt(D->getUpdateExpr());
+  Writer.AddStmt(D->getX());
+  Writer.AddStmt(D->getV());
+  Writer.AddStmt(D->getExpr());
+  Writer.AddStmt(D->getUpdateExpr());
   Record.push_back(D->isXLHSInRHSPart() ? 1 : 0);
   Record.push_back(D->isPostfixUpdate() ? 1 : 0);
   Code = serialization::STMT_OMP_ATOMIC_DIRECTIVE;
@@ -2418,44 +2411,61 @@ void ASTWriter::WriteSubStmt(Stmt *S,
   ParentStmtInserterRAII ParentStmtInserter(S, ParentStmts);
 #endif
 
+  // Redirect ASTWriter::AddStmt to collect sub-stmts.
+  SmallVector<Stmt *, 16> SubStmts;
+  CollectedStmts = &SubStmts;
+
+  Writer.Code = serialization::STMT_NULL_PTR;
+  Writer.AbbrevToUse = 0;
   Writer.Visit(S);
   
-  SubStmtEntries[S] = Writer.Emit(S);
+#ifndef NDEBUG
+  if (Writer.Code == serialization::STMT_NULL_PTR) {
+    SourceManager &SrcMgr
+      = DeclIDs.begin()->first->getASTContext().getSourceManager();
+    S->dump(SrcMgr);
+    llvm_unreachable("Unhandled sub-statement writing AST file");
+  }
+#endif
+
+  // Revert ASTWriter::AddStmt.
+  CollectedStmts = &StmtsToEmit;
+
+  // Write the sub-stmts in reverse order, last to first. When reading them back
+  // we will read them in correct order by "pop"ing them from the Stmts stack.
+  // This simplifies reading and allows to store a variable number of sub-stmts
+  // without knowing it in advance.
+  while (!SubStmts.empty())
+    WriteSubStmt(SubStmts.pop_back_val(), SubStmtEntries, ParentStmts);
+  
+  Stream.EmitRecord(Writer.Code, Record, Writer.AbbrevToUse);
+ 
+  SubStmtEntries[S] = Stream.GetCurrentBitNo();
 }
 
 /// \brief Flush all of the statements that have been added to the
 /// queue via AddStmt().
-void ASTRecordWriter::FlushStmts() {
+void ASTWriter::FlushStmts() {
+  RecordData Record;
+
   // We expect to be the only consumer of the two temporary statement maps,
   // assert that they are empty.
-  assert(Writer->SubStmtEntries.empty() && "unexpected entries in sub-stmt map");
-  assert(Writer->ParentStmts.empty() && "unexpected entries in parent stmt map");
+  assert(SubStmtEntries.empty() && "unexpected entries in sub-stmt map");
+  assert(ParentStmts.empty() && "unexpected entries in parent stmt map");
 
   for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
-    Writer->WriteSubStmt(StmtsToEmit[I], Writer->SubStmtEntries, Writer->ParentStmts);
+    WriteSubStmt(StmtsToEmit[I], SubStmtEntries, ParentStmts);
     
-    assert(N == StmtsToEmit.size() && "record modified while being written!");
+    assert(N == StmtsToEmit.size() &&
+           "Substatement written via AddStmt rather than WriteSubStmt!");
 
     // Note that we are at the end of a full expression. Any
     // expression records that follow this one are part of a different
     // expression.
-    Writer->Stream.EmitRecord(serialization::STMT_STOP, ArrayRef<uint32_t>());
-
-    Writer->SubStmtEntries.clear();
-    Writer->ParentStmts.clear();
-  }
+    Stream.EmitRecord(serialization::STMT_STOP, Record);
 
-  StmtsToEmit.clear();
-}
-
-void ASTRecordWriter::FlushSubStmts() {
-  // For a nested statement, write out the substatements in reverse order (so
-  // that a simple stack machine can be used when loading), and don't emit a
-  // STMT_STOP after each one.
-  for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
-    Writer->WriteSubStmt(StmtsToEmit[N - I - 1],
-                         Writer->SubStmtEntries, Writer->ParentStmts);
-    assert(N == StmtsToEmit.size() && "record modified while being written!");
+    SubStmtEntries.clear();
+    ParentStmts.clear();
   }
 
   StmtsToEmit.clear();

Modified: cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp?rev=265526&r1=265525&r2=265526&view=diff
==============================================================================
--- cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp (original)
+++ cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp Wed Apr  6 05:01:46 2016
@@ -87,13 +87,16 @@ static std::string ReadPCHRecord(StringR
 
 // Assumes that the way to get the value is SA->getname()
 static std::string WritePCHRecord(StringRef type, StringRef name) {
-  return "Record." + StringSwitch<std::string>(type)
-    .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
-    .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
+  return StringSwitch<std::string>(type)
+    .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
+                        ", Record);\n")
+    .Case("TypeSourceInfo *",
+          "AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
     .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
-    .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
-    .Case("StringRef", "AddString(" + std::string(name) + ");\n")
-    .Default("push_back(" + std::string(name) + ");\n");
+    .Case("IdentifierInfo *", 
+          "AddIdentifierRef(" + std::string(name) + ", Record);\n")
+    .Case("StringRef", "AddString(" + std::string(name) + ", Record);\n")
+    .Default("Record.push_back(" + std::string(name) + ");\n");
 }
 
 // Normalize attribute name by removing leading and trailing
@@ -368,7 +371,7 @@ namespace {
       OS << getLowerName();
     }
     void writePCHWrite(raw_ostream &OS) const override {
-      OS << "    Record.AddString(SA->get" << getUpperName() << "());\n";
+      OS << "    AddString(SA->get" << getUpperName() << "(), Record);\n";
     }
     void writeValue(raw_ostream &OS) const override {
       OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
@@ -484,10 +487,10 @@ namespace {
     void writePCHWrite(raw_ostream &OS) const override {
       OS << "    Record.push_back(SA->is" << getUpperName() << "Expr());\n";
       OS << "    if (SA->is" << getUpperName() << "Expr())\n";
-      OS << "      Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
+      OS << "      AddStmt(SA->get" << getUpperName() << "Expr());\n";
       OS << "    else\n";
-      OS << "      Record.AddTypeSourceInfo(SA->get" << getUpperName()
-         << "Type());\n";
+      OS << "      AddTypeSourceInfo(SA->get" << getUpperName()
+         << "Type(), Record);\n";
     }
     void writeValue(raw_ostream &OS) const override {
       OS << "\";\n";
@@ -884,7 +887,7 @@ namespace {
       OS << getLowerName();
     }
     void writePCHWrite(raw_ostream &OS) const override {
-      OS << "    Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
+      OS << "    AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
     }
     void writeValue(raw_ostream &OS) const override {
       OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";




More information about the cfe-commits mailing list