ASTWriter.cpp revision 8e3df4d0864f0a966c20088ca1a29c3398b7639d
14ee2ad04344446e610172a0e73949212923014dfSebastian Redl//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//                     The LLVM Compiler Infrastructure
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// This file is distributed under the University of Illinois Open Source
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// License. See LICENSE.TXT for details.
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
10a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl//  This file defines the ASTWriter class, which writes AST files.
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
147faa2ec03a7ef120ac165bb45b6c70a8b20c9f1cSebastian Redl#include "clang/Serialization/ASTWriter.h"
1589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor#include "clang/Serialization/ASTSerializationListener.h"
160eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidis#include "ASTCommon.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
18e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/IdentifierResolver.h"
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ASTContext.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/Decl.h"
212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/DeclContextInternals.h"
222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/AST/DeclTemplate.h"
2389eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis#include "clang/AST/DeclFriend.h"
240b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor#include "clang/AST/Expr.h"
257a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall#include "clang/AST/ExprCXX.h"
262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/Type.h"
27a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#include "clang/AST/TypeLocVisitor.h"
286ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redl#include "clang/Serialization/ASTReader.h"
297c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "clang/Lex/MacroInfo.h"
306a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor#include "clang/Lex/PreprocessingRecord.h"
317c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "clang/Lex/Preprocessor.h"
3283d63c78810556d26b62ac4cbae2eda6cdd2570cSteve Naroff#include "clang/Lex/HeaderSearch.h"
3314f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "clang/Basic/FileManager.h"
3410e286aa8d39fb51a21412850265d9dae74613eeChris Lattner#include "clang/Basic/FileSystemStatCache.h"
353251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor#include "clang/Basic/OnDiskHashTable.h"
3614f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "clang/Basic/SourceManager.h"
37bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor#include "clang/Basic/SourceManagerInternals.h"
382bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor#include "clang/Basic/TargetInfo.h"
39ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor#include "clang/Basic/Version.h"
4017fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor#include "llvm/ADT/APFloat.h"
4117fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor#include "llvm/ADT/APInt.h"
422596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar#include "llvm/ADT/StringExtras.h"
432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "llvm/Bitcode/BitstreamWriter.h"
44fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer#include "llvm/Support/FileSystem.h"
4514f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "llvm/Support/MemoryBuffer.h"
4603013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
473c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner#include <cstdio>
48cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor#include <string.h>
492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorusing namespace clang;
508538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlusing namespace clang::serialization;
512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
52ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redltemplate <typename T, typename Allocator>
53ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian RedlT *data(std::vector<T, Allocator> &v) {
54ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  return v.empty() ? 0 : &v.front();
55ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl}
56ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redltemplate <typename T, typename Allocator>
57ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redlconst T *data(const std::vector<T, Allocator> &v) {
58ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  return v.empty() ? 0 : &v.front();
59ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl}
60ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// Type serialization
632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
6412b1c7615d4f9a2edc544be499f895f16ac100edChris Lattner
652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregornamespace {
663397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  class ASTTypeWriter {
67a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl    ASTWriter &Writer;
6889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis    ASTWriter::RecordDataImpl &Record;
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  public:
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// \brief Type code that corresponds to the record generated.
728538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    TypeCode Code;
732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis    ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
758538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    void VisitArrayType(const ArrayType *T);
782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    void VisitFunctionType(const FunctionType *T);
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    void VisitTagType(const TagType *T);
802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define ABSTRACT_TYPE(Class, Base)
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/TypeNodes.def"
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  };
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
873397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(false && "Built-in types are never serialized");
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
913397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitComplexType(const ComplexType *T) {
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getElementType(), Record);
938538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_COMPLEX;
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
963397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitPointerType(const PointerType *T) {
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getPointeeType(), Record);
988538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_POINTER;
992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1013397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(T->getPointeeType(), Record);
1038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_BLOCK_POINTER;
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1063397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
1072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getPointeeType(), Record);
1088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_LVALUE_REFERENCE;
1092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1113397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
1122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getPointeeType(), Record);
1138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_RVALUE_REFERENCE;
1142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1163397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(T->getPointeeType(), Record);
1181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
1198538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_MEMBER_POINTER;
1202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1223397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitArrayType(const ArrayType *T) {
1232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getElementType(), Record);
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getSizeModifier()); // FIXME: stable values
1250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1283397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitArrayType(T);
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddAPInt(T->getSize(), Record);
1318538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_CONSTANT_ARRAY;
1322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1343397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitArrayType(T);
1368538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_INCOMPLETE_ARRAY;
1372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1393397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitArrayType(T);
1417e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Writer.AddSourceLocation(T->getLBracketLoc(), Record);
1427e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Writer.AddSourceLocation(T->getRBracketLoc(), Record);
143c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Writer.AddStmt(T->getSizeExpr());
1448538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_VARIABLE_ARRAY;
1452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1473397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitVectorType(const VectorType *T) {
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getElementType(), Record);
1492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getNumElements());
150e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  Record.push_back(T->getVectorKind());
1518538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_VECTOR;
1522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1543397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
1552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitVectorType(T);
1568538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_EXT_VECTOR;
1572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1593397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
1602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getResultType(), Record);
161264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo C = T->getExtInfo();
162264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  Record.push_back(C.getNoReturn());
163425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  Record.push_back(C.getRegParm());
164ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  // FIXME: need to stabilize encoding of calling convention...
165264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  Record.push_back(C.getCC());
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1683397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
1692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitFunctionType(T);
1708538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_FUNCTION_NO_PROTO;
1712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1733397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
1742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitFunctionType(T);
1752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getNumArgs());
1762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
1772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Writer.AddTypeRef(T->getArgType(I), Record);
1782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->isVariadic());
1792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getTypeQuals());
180c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor  Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
181465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  Record.push_back(T->hasExceptionSpec());
182465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  Record.push_back(T->hasAnyExceptionSpec());
183465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  Record.push_back(T->getNumExceptions());
184465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
185465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    Writer.AddTypeRef(T->getExceptionType(I), Record);
1868538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_FUNCTION_PROTO;
1872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1893397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
190ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Writer.AddDeclRef(T->getDecl(), Record);
1918538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_UNRESOLVED_USING;
192ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
193ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1943397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
1952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddDeclRef(T->getDecl(), Record);
1969763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
1979763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
1988538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TYPEDEF;
1992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2013397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
202c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Writer.AddStmt(T->getUnderlyingExpr());
2038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TYPEOF_EXPR;
2042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2063397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
2072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getUnderlyingType(), Record);
2088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TYPEOF;
2092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2113397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
212395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Writer.AddStmt(T->getUnderlyingExpr());
2138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DECLTYPE;
214395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
215395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2163397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTagType(const TagType *T) {
217be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  Record.push_back(T->isDependentType());
2182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddDeclRef(T->getDecl(), Record);
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!T->isBeingDefined() &&
2202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor         "Cannot serialize in the middle of a type definition");
2212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2233397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitRecordType(const RecordType *T) {
2242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitTagType(T);
2258538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_RECORD;
2262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2283397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitEnumType(const EnumType *T) {
2292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitTagType(T);
2308538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_ENUM;
2312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2339d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallvoid ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
2349d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Writer.AddTypeRef(T->getModifiedType(), Record);
2359d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Writer.AddTypeRef(T->getEquivalentType(), Record);
2369d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Record.push_back(T->getAttrKind());
2379d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Code = TYPE_ATTRIBUTED;
2389d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
2399d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2413397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitSubstTemplateTypeParmType(
24249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                        const SubstTemplateTypeParmType *T) {
24349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
24449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  Writer.AddTypeRef(T->getReplacementType(), Record);
2458538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
24649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
24749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
24849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallvoid
249c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorASTTypeWriter::VisitSubstTemplateTypeParmPackType(
250c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                      const SubstTemplateTypeParmPackType *T) {
251c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
252c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Writer.AddTemplateArgument(T->getArgumentPack(), Record);
253c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
254c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
255c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
256c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid
2573397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitTemplateSpecializationType(
2582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                       const TemplateSpecializationType *T) {
259be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  Record.push_back(T->isDependentType());
26090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Writer.AddTemplateName(T->getTemplateName(), Record);
26190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->getNumArgs());
26290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
26390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis         ArgI != ArgE; ++ArgI)
26490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Writer.AddTemplateArgument(*ArgI, Record);
2659763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
2669763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                : T->getCanonicalTypeInternal(),
2679763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                    Record);
2688538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TEMPLATE_SPECIALIZATION;
26990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
27090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
27190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2723397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
273ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis  VisitArrayType(T);
274ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis  Writer.AddStmt(T->getSizeExpr());
275ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis  Writer.AddSourceRange(T->getBracketsRange(), Record);
2768538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DEPENDENT_SIZED_ARRAY;
27790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
27890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
27990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2803397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentSizedExtVectorType(
28190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis                                        const DependentSizedExtVectorType *T) {
28290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  // FIXME: Serialize this type (C++ only)
28390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  assert(false && "Cannot serialize dependent sized extended vector types");
28490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
28590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
28690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2873397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
28890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->getDepth());
28990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->getIndex());
29090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->isParameterPack());
29190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Writer.AddIdentifierRef(T->getName(), Record);
2928538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TEMPLATE_TYPE_PARM;
29390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
29490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
29590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2963397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
2978dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  Record.push_back(T->getKeyword());
2988dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
2998dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  Writer.AddIdentifierRef(T->getIdentifier(), Record);
300f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
301f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis                                                : T->getCanonicalTypeInternal(),
302f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis                    Record);
3038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DEPENDENT_NAME;
30490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
30590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
30690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
3073397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentTemplateSpecializationType(
30890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis                                const DependentTemplateSpecializationType *T) {
3093acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Record.push_back(T->getKeyword());
3103acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
3113acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddIdentifierRef(T->getIdentifier(), Record);
3123acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Record.push_back(T->getNumArgs());
3133acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  for (DependentTemplateSpecializationType::iterator
3143acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis         I = T->begin(), E = T->end(); I != E; ++I)
3153acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis    Writer.AddTemplateArgument(*I, Record);
3168538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
3172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3197536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregorvoid ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
3207536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Writer.AddTypeRef(T->getPattern(), Record);
321cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
322cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Record.push_back(*NumExpansions + 1);
323cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  else
324cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Record.push_back(0);
3257536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Code = TYPE_PACK_EXPANSION;
3267536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
3277536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
328075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnaravoid ASTTypeWriter::VisitParenType(const ParenType *T) {
329075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Writer.AddTypeRef(T->getInnerType(), Record);
330075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Code = TYPE_PAREN;
331075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
332075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3333397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
334465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  Record.push_back(T->getKeyword());
3353acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
3363acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddTypeRef(T->getNamedType(), Record);
3378538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_ELABORATED;
3382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3403397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
3413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  Writer.AddDeclRef(T->getDecl(), Record);
34231f17ecbef57b5679c017c375db330546b7b5145John McCall  Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
3438538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_INJECTED_CLASS_NAME;
3443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
3453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
3463397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
347deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Writer.AddDeclRef(T->getDecl(), Record);
3488538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_OBJC_INTERFACE;
349c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
350c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
3513397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
352c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Writer.AddTypeRef(T->getBaseType(), Record);
3532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getNumProtocols());
354c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator I = T->qual_begin(),
355446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff       E = T->qual_end(); I != E; ++I)
356446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff    Writer.AddDeclRef(*I, Record);
3578538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_OBJC_OBJECT;
3582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
360d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroffvoid
3613397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
3621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(T->getPointeeType(), Record);
3638538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_OBJC_OBJECT_POINTER;
3642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
366a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallnamespace {
367a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
368a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallclass TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
369a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter &Writer;
37089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  ASTWriter::RecordDataImpl &Record;
371a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
372a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallpublic:
37389eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
374a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    : Writer(Writer), Record(Record) { }
375a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
37651bd803fbdade51d674598ed45da3d54190a656cJohn McCall#define ABSTRACT_TYPELOC(CLASS, PARENT)
377a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#define TYPELOC(CLASS, PARENT) \
37851bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
379a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#include "clang/AST/TypeLocNodes.def"
380a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
38151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
38251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
383a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall};
384a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
385a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
386a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
38751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
38851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // nothing to do
38951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
39051bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
391ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
392ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  if (TL.needsExtraLocalData()) {
393ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.getWrittenTypeSpec());
394ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.getWrittenSignSpec());
395ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.getWrittenWidthSpec());
396ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.hasModeAttr());
397ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  }
39851bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
39951bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
40051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
40151bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
40251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
40351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getStarLoc(), Record);
40451bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
40551bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
40651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getCaretLoc(), Record);
40751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
40851bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
40951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getAmpLoc(), Record);
41051bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
41151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
41251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
41351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
41451bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
41551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getStarLoc(), Record);
41651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
41751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
41851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
41951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
42051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Record.push_back(TL.getSizeExpr() ? 1 : 0);
42151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  if (TL.getSizeExpr())
42251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    Writer.AddStmt(TL.getSizeExpr());
42351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
42451bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
42551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
42651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
42751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
42851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
42951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
43051bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
43151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
43251bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
43351bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitDependentSizedArrayTypeLoc(
43451bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                            DependentSizedArrayTypeLoc TL) {
43551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
43651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
43751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
43851bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                        DependentSizedExtVectorTypeLoc TL) {
43951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
44051bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
44151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
44251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
44351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
44451bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
44551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
44651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
44751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
44851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
44951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
450dab60ad68a3a98d687305941a3852e793705f945Douglas Gregor  Record.push_back(TL.getTrailingReturn());
45151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
45251bd803fbdade51d674598ed45da3d54190a656cJohn McCall    Writer.AddDeclRef(TL.getArg(i), Record);
45351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
45451bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
45551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitFunctionTypeLoc(TL);
45651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
45751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
45851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitFunctionTypeLoc(TL);
45951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
460ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
461ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
462ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
46351bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
46451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
46551bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
46651bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
467cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
468cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
469cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
47051bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
47151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
472cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
473cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
474cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
475cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
47651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
47751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
47851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
479a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
48051bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
48151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
482a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
48351bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
48451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
485a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
4869d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallvoid TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4879d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
4889d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (TL.hasAttrOperand()) {
4899d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    SourceRange range = TL.getAttrOperandParensRange();
4909d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    Writer.AddSourceLocation(range.getBegin(), Record);
4919d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    Writer.AddSourceLocation(range.getEnd(), Record);
4929d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  }
4939d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (TL.hasAttrExprOperand()) {
4949d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    Expr *operand = TL.getAttrExprOperand();
4959d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    Record.push_back(operand ? 1 : 0);
4969d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    if (operand) Writer.AddStmt(operand);
4979d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  } else if (TL.hasAttrEnumOperand()) {
4989d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
4999d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  }
5009d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
50151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
50251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
503a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
50449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallvoid TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
50549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                            SubstTemplateTypeParmTypeLoc TL) {
50649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
50749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
508c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
509c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          SubstTemplateTypeParmPackTypeLoc TL) {
510c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Writer.AddSourceLocation(TL.getNameLoc(), Record);
511c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
51251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTemplateSpecializationTypeLoc(
51351bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                           TemplateSpecializationTypeLoc TL) {
514833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
515833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
516833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
517833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
51844f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
51944f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                      TL.getArgLoc(i).getLocInfo(), Record);
520a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
521075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnaravoid TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
522075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
525465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnaravoid TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
526e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
527e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceRange(TL.getQualifierRange(), Record);
528a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
5293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallvoid TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
5313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
5324714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregorvoid TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
533e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
534e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceRange(TL.getQualifierRange(), Record);
53551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
536a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
53733500955d731c73717af52088b7fc0e7a85681e7John McCallvoid TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
53833500955d731c73717af52088b7fc0e7a85681e7John McCall       DependentTemplateSpecializationTypeLoc TL) {
53933500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
54033500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceRange(TL.getQualifierRange(), Record);
54133500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
54233500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
54333500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
54433500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
54544f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
54644f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                      TL.getArgLoc(I).getLocInfo(), Record);
54733500955d731c73717af52088b7fc0e7a85681e7John McCall}
5487536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregorvoid TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5497536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
5507536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
55151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
55251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
553c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
554c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallvoid TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
555c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Record.push_back(TL.hasBaseTypeAsWritten());
55654e14c4db764c0636160d26c5bbf491637c83a76John McCall  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
55754e14c4db764c0636160d26c5bbf491637c83a76John McCall  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
55854e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
55954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
560a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
56151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
56251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getStarLoc(), Record);
563a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
564a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
5654dcf151a555ff51e4d643e8e6eeb80f121d11d1bChris Lattner//===----------------------------------------------------------------------===//
566a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl// ASTWriter Implementation
5672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
5682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
569b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattnerstatic void EmitBlockID(unsigned ID, const char *Name,
570b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner                        llvm::BitstreamWriter &Stream,
57189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                        ASTWriter::RecordDataImpl &Record) {
572b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.clear();
573b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.push_back(ID);
574b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
575b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
576b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  // Emit the block name if present.
577b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  if (Name == 0 || Name[0] == 0) return;
578b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.clear();
579b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  while (*Name)
580b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner    Record.push_back(*Name++);
581b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
582b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner}
583b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
584b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattnerstatic void EmitRecordID(unsigned ID, const char *Name,
585b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner                         llvm::BitstreamWriter &Stream,
58689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                         ASTWriter::RecordDataImpl &Record) {
587b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.clear();
588b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.push_back(ID);
589b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  while (*Name)
590b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner    Record.push_back(*Name++);
591b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
5920558df2da807646e65d4fa290f4e92114af1a746Chris Lattner}
5930558df2da807646e65d4fa290f4e92114af1a746Chris Lattner
5940558df2da807646e65d4fa290f4e92114af1a746Chris Lattnerstatic void AddStmtsExprs(llvm::BitstreamWriter &Stream,
59589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                          ASTWriter::RecordDataImpl &Record) {
5968538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
5970558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_STOP);
5980558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_NULL_PTR);
5990558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_NULL);
6000558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_COMPOUND);
6010558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_CASE);
6020558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_DEFAULT);
6030558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_LABEL);
6040558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_IF);
6050558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_SWITCH);
6060558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_WHILE);
6070558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_DO);
6080558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_FOR);
6090558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_GOTO);
6100558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_INDIRECT_GOTO);
6110558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_CONTINUE);
6120558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_BREAK);
6130558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_RETURN);
6140558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_DECL);
6150558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_ASM);
6160558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_PREDEFINED);
6170558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_DECL_REF);
6180558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_INTEGER_LITERAL);
6190558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_FLOATING_LITERAL);
6200558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_IMAGINARY_LITERAL);
6210558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_STRING_LITERAL);
6220558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CHARACTER_LITERAL);
6230558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_PAREN);
6240558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_UNARY_OPERATOR);
6250558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_SIZEOF_ALIGN_OF);
6260558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_ARRAY_SUBSCRIPT);
6270558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CALL);
6280558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_MEMBER);
6290558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_BINARY_OPERATOR);
6300558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
6310558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CONDITIONAL_OPERATOR);
6320558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_IMPLICIT_CAST);
6330558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CSTYLE_CAST);
6340558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_COMPOUND_LITERAL);
6350558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_EXT_VECTOR_ELEMENT);
6360558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_INIT_LIST);
6370558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_DESIGNATED_INIT);
6380558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_IMPLICIT_VALUE_INIT);
6390558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_VA_ARG);
6400558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_ADDR_LABEL);
6410558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_STMT);
6420558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CHOOSE);
6430558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_GNU_NULL);
6440558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_SHUFFLE_VECTOR);
6450558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_BLOCK);
6460558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_BLOCK_DECL_REF);
6470558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_STRING_LITERAL);
6480558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_ENCODE);
6490558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_SELECTOR_EXPR);
6500558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_PROTOCOL_EXPR);
6510558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_IVAR_REF_EXPR);
6520558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
6530558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_KVC_REF_EXPR);
6540558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_MESSAGE_EXPR);
6550558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_FOR_COLLECTION);
6560558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_CATCH);
6570558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_FINALLY);
6580558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_AT_TRY);
6590558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_AT_SYNCHRONIZED);
6600558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_AT_THROW);
661eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_OPERATOR_CALL);
662eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_CONSTRUCT);
663eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_STATIC_CAST);
664eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_DYNAMIC_CAST);
665eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_REINTERPRET_CAST);
666eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_CONST_CAST);
667eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_FUNCTIONAL_CAST);
668eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_BOOL_LITERAL);
669eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_NULL_PTR_LITERAL);
670b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_TYPEID_EXPR);
671b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_TYPEID_TYPE);
672b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_UUIDOF_EXPR);
673b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_UUIDOF_TYPE);
674b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_THIS);
675b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_THROW);
676b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_DEFAULT_ARG);
677b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_BIND_TEMPORARY);
678b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
679b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_NEW);
680b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_DELETE);
681b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
682b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_EXPR_WITH_CLEANUPS);
683b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
684b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
685b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
686b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
687b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
688b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
689b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_CXX_NOEXCEPT);
690b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_OPAQUE_VALUE);
691b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_BINARY_TYPE_TRAIT);
692b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_PACK_EXPANSION);
693b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_SIZEOF_PACK);
694b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
695e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne  RECORD(EXPR_CUDA_KERNEL_CALL);
6960558df2da807646e65d4fa290f4e92114af1a746Chris Lattner#undef RECORD
697b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner}
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
699a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteBlockInfoBlock() {
700b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RecordData Record;
701b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
7048538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
7051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7063397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // AST Top-Level Block.
707f29f0a28c4d9599b389bbb6d186e14af753dc5a3Sebastian Redl  BLOCK(AST_BLOCK);
70851e774d42269e3b22d746184c0b9076fc13b32e6Zhongxing Xu  RECORD(ORIGINAL_FILE_NAME);
709b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_OFFSET);
710b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_OFFSET);
711b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(LANGUAGE_OPTIONS);
712ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor  RECORD(METADATA);
713b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(IDENTIFIER_OFFSET);
714b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(IDENTIFIER_TABLE);
715b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(EXTERNAL_DEFINITIONS);
716b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SPECIAL_TYPES);
717b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(STATISTICS);
718b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TENTATIVE_DEFINITIONS);
71949b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  RECORD(UNUSED_FILESCOPED_DECLS);
720b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
721b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SELECTOR_OFFSETS);
722b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(METHOD_POOL);
723b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_COUNTER_VALUE);
7247f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RECORD(SOURCE_LOCATION_OFFSETS);
7257f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RECORD(SOURCE_LOCATION_PRELOADS);
7264fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  RECORD(STAT_CACHE);
727b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  RECORD(EXT_VECTOR_DECLS);
7285b4ec636637c9d876102240127cc0dca9280e83aTed Kremenek  RECORD(VERSION_CONTROL_BRANCH_REVISION);
7296a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  RECORD(MACRO_DEFINITION_OFFSETS);
730a93e3b5bde9f0a7b59215f19f176f7d69881b81cSebastian Redl  RECORD(CHAINED_METADATA);
731320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  RECORD(REFERENCED_SELECTOR_POOL);
732b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TU_UPDATE_LEXICAL);
733b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(REDECLS_UPDATE_LATEST);
734b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(SEMA_DECL_REFS);
735b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(WEAK_UNDECLARED_IDENTIFIERS);
736b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
737b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_REPLACEMENTS);
738b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(UPDATE_VISIBLE);
739b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_UPDATE_OFFSETS);
740b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_UPDATES);
741b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(CXX_BASE_SPECIFIER_OFFSETS);
742b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DIAG_PRAGMA_MAPPINGS);
743cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  RECORD(HEADER_SEARCH_TABLE);
744cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
745b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  // SourceManager Block.
7462f4efd10c805cb779618c1a22a35eb07b5043c4eChris Lattner  BLOCK(SOURCE_MANAGER_BLOCK);
747b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_FILE_ENTRY);
748b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_BUFFER_ENTRY);
749b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_BUFFER_BLOB);
750b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_INSTANTIATION_ENTRY);
751b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_LINE_TABLE);
7521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
753b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  // Preprocessor Block.
7542f4efd10c805cb779618c1a22a35eb07b5043c4eChris Lattner  BLOCK(PREPROCESSOR_BLOCK);
755b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_MACRO_OBJECT_LIKE);
756b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_MACRO_FUNCTION_LIKE);
757b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_TOKEN);
758b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor
75961d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  // Decls and Types block.
76061d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  BLOCK(DECLTYPES_BLOCK);
761b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_EXT_QUAL);
762b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_COMPLEX);
763b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_POINTER);
764b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_BLOCK_POINTER);
765b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_LVALUE_REFERENCE);
766b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_RVALUE_REFERENCE);
767b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_MEMBER_POINTER);
768b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_CONSTANT_ARRAY);
769b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_INCOMPLETE_ARRAY);
770b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_VARIABLE_ARRAY);
771b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_VECTOR);
772b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_EXT_VECTOR);
773b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_FUNCTION_PROTO);
774b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_FUNCTION_NO_PROTO);
775b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_TYPEDEF);
776b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_TYPEOF_EXPR);
777b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_TYPEOF);
778b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_RECORD);
779b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_ENUM);
780b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_OBJC_INTERFACE);
781a53d2cbe37e4be0d95b9d3e09f74eafae31fc940John McCall  RECORD(TYPE_OBJC_OBJECT);
782d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  RECORD(TYPE_OBJC_OBJECT_POINTER);
783b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_DECLTYPE);
784b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_ELABORATED);
785b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
786b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_UNRESOLVED_USING);
787b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_INJECTED_CLASS_NAME);
788b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_OBJC_OBJECT);
789b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_TEMPLATE_TYPE_PARM);
790b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_TEMPLATE_SPECIALIZATION);
791b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_DEPENDENT_NAME);
792b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
793b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
794b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_PAREN);
795b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_PACK_EXPANSION);
796b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_ATTRIBUTED);
797b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
7980ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_TRANSLATION_UNIT);
7990ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_TYPEDEF);
8000ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_ENUM);
8010ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_RECORD);
8020ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_ENUM_CONSTANT);
8030ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_FUNCTION);
8040ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_METHOD);
8050ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_INTERFACE);
8060ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_PROTOCOL);
8070ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_IVAR);
8080ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_AT_DEFS_FIELD);
8090ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_CLASS);
8100ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_FORWARD_PROTOCOL);
8110ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_CATEGORY);
8120ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_CATEGORY_IMPL);
8130ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_IMPLEMENTATION);
8140ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
8150ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_PROPERTY);
8160ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_PROPERTY_IMPL);
817b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_FIELD);
818b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_VAR);
8190ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_IMPLICIT_PARAM);
820b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_PARM_VAR);
8210ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_FILE_SCOPE_ASM);
8220ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_BLOCK);
8230ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_CONTEXT_LEXICAL);
8240ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_CONTEXT_VISIBLE);
825b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_NAMESPACE);
826b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_NAMESPACE_ALIAS);
827b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_USING);
828b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_USING_SHADOW);
829b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_USING_DIRECTIVE);
830b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_UNRESOLVED_USING_VALUE);
831b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_UNRESOLVED_USING_TYPENAME);
832b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_LINKAGE_SPEC);
833b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CXX_RECORD);
834b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CXX_METHOD);
835b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CXX_CONSTRUCTOR);
836b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CXX_DESTRUCTOR);
837b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CXX_CONVERSION);
838b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_ACCESS_SPEC);
839b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_FRIEND);
840b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_FRIEND_TEMPLATE);
841b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CLASS_TEMPLATE);
842b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
843b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
844b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_FUNCTION_TEMPLATE);
845b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_TEMPLATE_TYPE_PARM);
846b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
847b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
848b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_STATIC_ASSERT);
849b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_CXX_BASE_SPECIFIERS);
850b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_INDIRECTFIELD);
851b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor  RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
852b6c2b3f658df5525b9c0af606b64d304e7416c2fDouglas Gregor
8534800a5c79023271408af49797e09be32aca93232Douglas Gregor  BLOCK(PREPROCESSOR_DETAIL_BLOCK);
8544800a5c79023271408af49797e09be32aca93232Douglas Gregor  RECORD(PPD_MACRO_INSTANTIATION);
8554800a5c79023271408af49797e09be32aca93232Douglas Gregor  RECORD(PPD_MACRO_DEFINITION);
8564800a5c79023271408af49797e09be32aca93232Douglas Gregor  RECORD(PPD_INCLUSION_DIRECTIVE);
8574800a5c79023271408af49797e09be32aca93232Douglas Gregor
85861d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  // Statements and Exprs can occur in the Decls and Types block.
8590558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  AddStmtsExprs(Stream, Record);
860b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner#undef RECORD
861b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner#undef BLOCK
862b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.ExitBlock();
863b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner}
864b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
865e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \brief Adjusts the given filename to only write out the portion of the
866e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// filename that is not part of the system root directory.
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
868e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \param Filename the file name to adjust.
869e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor///
870e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
871e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// the returned filename will be adjusted by this system root.
872e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor///
873e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \returns either the original filename (if it needs no adjustment) or the
874e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// adjusted filename (which points into the @p Filename parameter).
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic const char *
876e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas GregoradjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
877e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  assert(Filename && "No file name to adjust?");
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
879e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (!isysroot)
880e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    return Filename;
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
882e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // Verify that the filename and the system root have the same prefix.
883e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  unsigned Pos = 0;
884e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  for (; Filename[Pos] && isysroot[Pos]; ++Pos)
885e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    if (Filename[Pos] != isysroot[Pos])
886e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor      return Filename; // Prefixes don't match.
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
888e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // We hit the end of the filename before we hit the end of the system root.
889e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (!Filename[Pos])
890e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    return Filename;
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
892e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // If the file name has a '/' at the current position, skip over the '/'.
893e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // We distinguish sysroot-based includes from absolute includes by the
894e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // absence of '/' at the beginning of sysroot-based includes.
895e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (Filename[Pos] == '/')
896e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    ++Pos;
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
898e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  return Filename + Pos;
899e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor}
900b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
9013397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
9028e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidisvoid ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
9038e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis                              const std::string &OutputFile) {
9042bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor  using namespace llvm;
905b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor
906e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // Metadata
907e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  const TargetInfo &Target = Context.Target;
908e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
90977f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(
9108538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                    Chain ? CHAINED_METADATA : METADATA));
9113397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
9123397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
913e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
914e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
915e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
91677f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  // Target triple or chained PCH name
91777f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
918e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
920e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  RecordData Record;
9218538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(Chain ? CHAINED_METADATA : METADATA);
9228538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(VERSION_MAJOR);
9238538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(VERSION_MINOR);
924e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  Record.push_back(CLANG_VERSION_MAJOR);
925e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  Record.push_back(CLANG_VERSION_MINOR);
926e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  Record.push_back(isysroot != 0);
92777f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  // FIXME: This writes the absolute path for chained headers.
92877f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
92977f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
931b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  // Original file name
932b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  SourceManager &SM = Context.getSourceManager();
933b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
934b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
9358538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
936b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
937b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
938b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor
939fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer    llvm::SmallString<128> MainFilePath(MainFile->getName());
9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
941fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer    llvm::sys::fs::make_absolute(MainFilePath);
942b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor
943aba54a95e9d5e4dc9056abec6bb70ea777c4a7bcKovarththanan Rajaratnam    const char *MainFileNameStr = MainFilePath.c_str();
9441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
945e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor                                                      isysroot);
946b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    RecordData Record;
9478538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(ORIGINAL_FILE_NAME);
948ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar    Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
949b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  }
95011a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
9518e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis  // Original PCH directory
9528e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis  if (!OutputFile.empty() && OutputFile != "-") {
9538e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
9548e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
9558e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
9568e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
9578e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis
9588e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    llvm::SmallString<128> OutputPath(OutputFile);
9598e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis
9608e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    llvm::sys::fs::make_absolute(OutputPath);
9618e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    StringRef origDir = llvm::sys::path::parent_path(OutputPath);
9628e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis
9638e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    RecordData Record;
9648e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    Record.push_back(ORIGINAL_PCH_DIR);
9658e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
9668e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis  }
9678e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis
968f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  // Repository branch/version information.
969f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
9708538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
971f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
972f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
973445e23e9b909ec8e21303c7dd82c90b72fc09ac4Douglas Gregor  Record.clear();
9748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
975f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
976f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek                            getClangFullRepositoryVersion());
9772bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor}
9782bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor
9792bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor/// \brief Write the LangOptions structure.
980a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
9810a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  RecordData Record;
9820a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Trigraphs);
9830a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.BCPLComment);  // BCPL-style '//' comments.
9840a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.DollarIdents);  // '$' allowed in identifiers.
9850a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.AsmPreprocessor);  // Preprocessor in asm mode.
9860a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.GNUMode);  // True in gnu99 mode false in c99 mode (etc)
987eb5d7b752651283de5abfcc2f91df7227582a08dChandler Carruth  Record.push_back(LangOpts.GNUKeywords);  // Allow GNU-extension keywords
9880a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.ImplicitInt);  // C89 implicit 'int'.
9890a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Digraphs);  // C94, C99 and C++
9900a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.HexFloats);  // C99 Hexadecimal float constants.
9910a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.C99);  // C99 Support
9920a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Microsoft);  // Microsoft extensions.
993dae4ac4fe56ec60fc93b3e3c9f51e79e682084b9Michael J. Spencer  // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
994dae4ac4fe56ec60fc93b3e3c9f51e79e682084b9Michael J. Spencer  // already saved elsewhere.
9950a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.CPlusPlus);  // C++ Support
9960a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.CPlusPlus0x);  // C++0x Support
9970a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.CXXOperatorNames);  // Treat C++ operator names as keywords.
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9990a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.ObjC1);  // Objective-C 1 support enabled.
10000a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.ObjC2);  // Objective-C 2 support enabled.
100111a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam  Record.push_back(LangOpts.ObjCNonFragileABI);  // Objective-C
1002412e798941ca64e2e6b084323915fa9aa5f6bdf3Fariborz Jahanian                                                 // modern abi enabled.
100311a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam  Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
1004412e798941ca64e2e6b084323915fa9aa5f6bdf3Fariborz Jahanian                                                 // modern abi enabled.
1005f84109ee6aeffb09366bd70c8593ce1b7818b1adFariborz Jahanian  Record.push_back(LangOpts.AppleKext);          // Apple's kernel extensions ABI
1006c32647d111e516593b9ba242cad7b8ff4016c155Ted Kremenek  Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1007c32647d111e516593b9ba242cad7b8ff4016c155Ted Kremenek                                                      // properties enabled.
10084c9d8d0eca5ca635d9a30222f690db9140e98325Fariborz Jahanian  Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10100a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.PascalStrings);  // Allow Pascal strings
10110a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.WritableStrings);  // Allow writable strings
10120a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.LaxVectorConversions);
1013b9e7e63ae2098bc02e79c032df0a3124d09a4b4eNate Begeman  Record.push_back(LangOpts.AltiVec);
10140a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Exceptions);  // Support exception handling.
101573482884560be041d86eccbd7dd5a6918677393bDaniel Dunbar  Record.push_back(LangOpts.SjLjExceptions);
10160a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
10176f75550e40a9177db9979fc130ce3ece026b137dDouglas Gregor  Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
10180a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
10190a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Freestanding); // Freestanding implementation
10200a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
10210a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
1022ea5ce4705df0743093925585d8edc80e0d8fe3ffChris Lattner  // Whether static initializers are protected by locks.
1023ea5ce4705df0743093925585d8edc80e0d8fe3ffChris Lattner  Record.push_back(LangOpts.ThreadsafeStatics);
1024972d954bd216c86a961bb7f81c53af85de17c2f0Douglas Gregor  Record.push_back(LangOpts.POSIXThreads);
10250a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Blocks); // block extension to C
10260a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
10270a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // they are unused.
10280a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
10290a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // (modulo the platform support).
10300a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
1031a4d71455f0d418e16cc0c5c5aa55a3bad3494aeeChris Lattner  Record.push_back(LangOpts.getSignedOverflowBehavior());
1032a4d71455f0d418e16cc0c5c5aa55a3bad3494aeeChris Lattner  Record.push_back(LangOpts.HeinousExtensions);
10330a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
10340a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
10351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
10360a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // defined.
10370a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
10380a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // opposed to __DYNAMIC__).
10390a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
10400a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
10410a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
10420a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // used (instead of C99 semantics).
10430a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1044a33d9b4ebf732a5da6d56fd7319ff6c020789b1cAnders Carlsson  Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1045a33d9b4ebf732a5da6d56fd7319ff6c020789b1cAnders Carlsson                                            // be enabled.
104615b91764d08e886391c865c4a444d7b51141c284Eli Friedman  Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
104715b91764d08e886391c865c4a444d7b51141c284Eli Friedman                                           // unsigned type
1048a6fda124bf380479529d6a80b84b62cacd3cb707John Thompson  Record.push_back(LangOpts.ShortWChar);  // force wchar_t to be unsigned short
1049b1bdced5967c54d029cd12907e8939c78decf14cArgyrios Kyrtzidis  Record.push_back(LangOpts.ShortEnums);  // Should the enum type be equivalent
1050b1bdced5967c54d029cd12907e8939c78decf14cArgyrios Kyrtzidis                                          // to the smallest integer type with
1051b1bdced5967c54d029cd12907e8939c78decf14cArgyrios Kyrtzidis                                          // enough room.
10520a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.getGCMode());
10530a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.getVisibilityMode());
1054ab8e281b32a3d3b9b18257d26844362bf806ecdcDaniel Dunbar  Record.push_back(LangOpts.getStackProtectorMode());
10550a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.InstantiationDepth);
1056b9e7e63ae2098bc02e79c032df0a3124d09a4b4eNate Begeman  Record.push_back(LangOpts.OpenCL);
105708a53269b1f8241e2e6797e845abbe640fb18253Peter Collingbourne  Record.push_back(LangOpts.CUDA);
10589c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  Record.push_back(LangOpts.CatchUndefined);
105992f5822df6a0d7571df44b5d279ed4f017fbb0e6Anders Carlsson  Record.push_back(LangOpts.ElideConstructors);
1060a0068fc64351db9c47916566e3b85ab733cd8d6dDouglas Gregor  Record.push_back(LangOpts.SpellChecking);
10618538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
10620a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor}
10630a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
106414f79002e58556798e86168c63e48d533287eda5Douglas Gregor//===----------------------------------------------------------------------===//
10654fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// stat cache Serialization
10664fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
10674fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
10684fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregornamespace {
10694fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Trait used for the on-disk hash table of stat cache results.
10703397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlclass ASTStatCacheTrait {
10714fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregorpublic:
10724fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  typedef const char * key_type;
10734fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  typedef key_type key_type_ref;
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107574e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner  typedef struct stat data_type;
107674e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner  typedef const data_type &data_type_ref;
10774fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
10784fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  static unsigned ComputeHash(const char *path) {
10792596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar    return llvm::HashString(path);
10804fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<unsigned,unsigned>
10834fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
10844fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor                      data_type_ref Data) {
10854fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    unsigned StrLen = strlen(path);
10864fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    clang::io::Emit16(Out, StrLen);
108774e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner    unsigned DataLen = 4 + 4 + 2 + 8 + 8;
10884fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    clang::io::Emit8(Out, DataLen);
10894fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    return std::make_pair(StrLen + 1, DataLen);
10904fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10924fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
10934fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    Out.write(path, KeyLen);
10944fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
109674e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner  void EmitData(llvm::raw_ostream &Out, key_type_ref,
10974fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor                data_type_ref Data, unsigned DataLen) {
10984fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    using namespace clang::io;
10994fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    uint64_t Start = Out.tell(); (void)Start;
11001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
110174e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner    Emit32(Out, (uint32_t) Data.st_ino);
110274e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner    Emit32(Out, (uint32_t) Data.st_dev);
110374e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner    Emit16(Out, (uint16_t) Data.st_mode);
110474e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner    Emit64(Out, (uint64_t) Data.st_mtime);
110574e976ba4b0d407bb798ea26476f618e256fc8c7Chris Lattner    Emit64(Out, (uint64_t) Data.st_size);
11064fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
11074fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    assert(Out.tell() - Start == DataLen && "Wrong data length");
11084fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
11094fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor};
11104fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor} // end anonymous namespace
11114fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
11123397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the stat() system call cache to the AST file.
1113a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
11144fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Build the on-disk hash table containing information about every
11154fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // stat() call.
11163397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
11174fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  unsigned NumStatEntries = 0;
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
11194fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor                                StatEnd = StatCalls.end();
1120e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor       Stat != StatEnd; ++Stat, ++NumStatEntries) {
1121e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    const char *Filename = Stat->first();
1122e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    Generator.insert(Filename, Stat->second);
1123e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  }
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11254fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Create the on-disk hash table in a buffer.
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::SmallString<4096> StatCacheData;
11274fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  uint32_t BucketOffset;
11284fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  {
11294fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    llvm::raw_svector_ostream Out(StatCacheData);
11304fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    // Make sure that no bucket is at offset 0
11314fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    clang::io::Emit32(Out, 0);
11324fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    BucketOffset = Generator.Emit(Out);
11334fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
11344fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
11354fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Create a blob abbreviation
11364fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  using namespace llvm;
11374fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
11388538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
11394fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
11404fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
11414fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
11424fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
11434fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
11444fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Write the stat cache
11454fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  RecordData Record;
11468538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(STAT_CACHE);
11474fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Record.push_back(BucketOffset);
11484fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Record.push_back(NumStatEntries);
1149ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar  Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
11504fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor}
11514fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
11524fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
115314f79002e58556798e86168c63e48d533287eda5Douglas Gregor// Source Manager Serialization
115414f79002e58556798e86168c63e48d533287eda5Douglas Gregor//===----------------------------------------------------------------------===//
115514f79002e58556798e86168c63e48d533287eda5Douglas Gregor
115614f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to a
115714f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// file.
1158c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
115914f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
116014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
11618538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
116214f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
116314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
116414f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
116514f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
11662d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor  // FileEntry fields.
11672d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
11682d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
116914f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1170c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
117114f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
117214f79002e58556798e86168c63e48d533287eda5Douglas Gregor
117314f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to a
117414f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// buffer.
1175c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
117614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
117714f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
11788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
117914f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
118014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
118114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
118214f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
118314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1184c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
118514f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
118614f79002e58556798e86168c63e48d533287eda5Douglas Gregor
118714f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to a
118814f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// buffer's blob.
1189c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
119014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
119114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
11928538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
119314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1194c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
119514f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
119614f79002e58556798e86168c63e48d533287eda5Douglas Gregor
119714f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to an
119814f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// buffer.
1199c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
120014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
120114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
12028538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
120314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
120414f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
120514f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
120614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1207f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1208c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
120914f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
121014f79002e58556798e86168c63e48d533287eda5Douglas Gregor
1211cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregornamespace {
1212cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  // Trait used for the on-disk hash table of header search information.
1213cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  class HeaderFileInfoTrait {
1214cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    ASTWriter &Writer;
1215cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    HeaderSearch &HS;
1216cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1217cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  public:
1218cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1219cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      : Writer(Writer), HS(HS) { }
1220cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1221cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    typedef const char *key_type;
1222cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    typedef key_type key_type_ref;
1223cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1224cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    typedef HeaderFileInfo data_type;
1225cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    typedef const data_type &data_type_ref;
1226cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1227cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    static unsigned ComputeHash(const char *path) {
1228cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      // The hash is based only on the filename portion of the key, so that the
1229cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      // reader can match based on filenames when symlinking or excess path
1230cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      // elements ("foo/../", "../") change the form of the name. However,
1231cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      // complete path is still the key.
1232cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      return llvm::HashString(llvm::sys::path::filename(path));
1233cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    }
1234cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1235cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    std::pair<unsigned,unsigned>
1236cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1237cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor                      data_type_ref Data) {
1238cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      unsigned StrLen = strlen(path);
1239cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      clang::io::Emit16(Out, StrLen);
1240cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      unsigned DataLen = 1 + 2 + 4;
1241cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      clang::io::Emit8(Out, DataLen);
1242cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      return std::make_pair(StrLen + 1, DataLen);
1243cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    }
1244cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1245cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1246cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      Out.write(path, KeyLen);
1247cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    }
1248cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1249cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    void EmitData(llvm::raw_ostream &Out, key_type_ref,
1250cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor                  data_type_ref Data, unsigned DataLen) {
1251cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      using namespace clang::io;
1252cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      uint64_t Start = Out.tell(); (void)Start;
1253cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1254cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      unsigned char Flags = (Data.isImport << 3)
1255cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor                          | (Data.DirInfo << 1)
1256cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor                          | Data.Resolved;
1257cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      Emit8(Out, (uint8_t)Flags);
1258cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      Emit16(Out, (uint16_t) Data.NumIncludes);
1259cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1260cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      if (!Data.ControllingMacro)
1261cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor        Emit32(Out, (uint32_t)Data.ControllingMacroID);
1262cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      else
1263cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor        Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1264cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      assert(Out.tell() - Start == DataLen && "Wrong data length");
1265cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    }
1266cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  };
1267cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor} // end anonymous namespace
1268cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1269cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor/// \brief Write the header search block for the list of files that
1270cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor///
1271cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor/// \param HS The header search structure to save.
1272cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor///
1273cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor/// \param Chain Whether we're creating a chained AST file.
1274cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregorvoid ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1275cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1276cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1277cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1278cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  if (FilesByUID.size() > HS.header_file_size())
1279cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    FilesByUID.resize(HS.header_file_size());
1280cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1281cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  HeaderFileInfoTrait GeneratorTrait(*this, HS);
1282cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1283cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  llvm::SmallVector<const char *, 4> SavedStrings;
1284cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  unsigned NumHeaderSearchEntries = 0;
1285cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1286cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    const FileEntry *File = FilesByUID[UID];
1287cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    if (!File)
1288cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      continue;
1289cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1290cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1291cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    if (HFI.External && Chain)
1292cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      continue;
1293cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1294cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    // Turn the file name into an absolute path, if it isn't already.
1295cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    const char *Filename = File->getName();
1296cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1297cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1298cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    // If we performed any translation on the file name at all, we need to
1299cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    // save this string, since the generator will refer to it later.
1300cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    if (Filename != File->getName()) {
1301cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      Filename = strdup(Filename);
1302cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor      SavedStrings.push_back(Filename);
1303cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    }
1304cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1305cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    Generator.insert(Filename, HFI, GeneratorTrait);
1306cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    ++NumHeaderSearchEntries;
1307cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  }
1308cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1309cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  // Create the on-disk hash table in a buffer.
1310cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  llvm::SmallString<4096> TableData;
1311cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  uint32_t BucketOffset;
1312cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  {
1313cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    llvm::raw_svector_ostream Out(TableData);
1314cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    // Make sure that no bucket is at offset 0
1315cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    clang::io::Emit32(Out, 0);
1316cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    BucketOffset = Generator.Emit(Out, GeneratorTrait);
1317cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  }
1318cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1319cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  // Create a blob abbreviation
1320cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  using namespace llvm;
1321cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1322cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1323cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1324cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1325cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1326cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1327cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1328cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  // Write the stat cache
1329cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  RecordData Record;
1330cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Record.push_back(HEADER_SEARCH_TABLE);
1331cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Record.push_back(BucketOffset);
1332cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Record.push_back(NumHeaderSearchEntries);
1333cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1334cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
1335cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  // Free all of the strings we had to duplicate.
1336cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1337cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor    free((void*)SavedStrings[I]);
1338cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor}
1339cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor
134014f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Writes the block containing the serialized form of the
134114f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// source manager.
134214f79002e58556798e86168c63e48d533287eda5Douglas Gregor///
134314f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// TODO: We should probably use an on-disk hash table (stored in a
134414f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// blob), indexed based on the file name, so that we only create
134514f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// entries for files that we actually need. In the common case (no
134614f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// errors), we probably won't have to create file entries for any of
134714f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// the files in the AST.
1348a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1349e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor                                        const Preprocessor &PP,
1350e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor                                        const char *isysroot) {
13517f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RecordData Record;
13527f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1353f04ad69fed38d26fc0d6f7d6fd0a4631ddfbc7feChris Lattner  // Enter the source manager block.
13548538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
135514f79002e58556798e86168c63e48d533287eda5Douglas Gregor
135614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // Abbreviations for the various kinds of source-location entries.
1357828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1358828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1359828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1360828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
13617f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
13627f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  // Write the line table.
13637f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  if (SourceMgr.hasLineTable()) {
13647f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    LineTableInfo &LineTable = SourceMgr.getLineTable();
13657f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
13667f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    // Emit the file names
13677f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    Record.push_back(LineTable.getNumFilenames());
13687f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
13697f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      // Emit the file name
13707f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      const char *Filename = LineTable.getFilename(I);
1371e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor      Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
13727f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      unsigned FilenameLen = Filename? strlen(Filename) : 0;
13737f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      Record.push_back(FilenameLen);
13747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      if (FilenameLen)
13757f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.insert(Record.end(), Filename, Filename + FilenameLen);
13767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    }
13771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    // Emit the line entries
13797f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
13807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor         L != LEnd; ++L) {
13817f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      // Emit the file ID
13827f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      Record.push_back(L->first);
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13847f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      // Emit the line entries
13857f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      Record.push_back(L->second.size());
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (std::vector<LineEntry>::iterator LE = L->second.begin(),
13877f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                         LEEnd = L->second.end();
13887f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor           LE != LEEnd; ++LE) {
13897f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->FileOffset);
13907f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->LineNo);
13917f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->FilenameID);
13927f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back((unsigned)LE->FileKind);
13937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->IncludeOffset);
13947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      }
13957f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    }
13968538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(SM_LINE_TABLE, Record);
13977f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  }
13987f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
139914f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // Write out the source location entry table. We skip the first
140014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // entry, which is always the same dummy entry.
1401090d9b53e32bb30d9e74de895bb59b409bd49e00Chris Lattner  std::vector<uint32_t> SLocEntryOffsets;
14027f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RecordData PreloadSLocs;
14030fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl  unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
14040fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl  SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
14050fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl  for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
14060fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl       I != N; ++I) {
1407bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    // Get this source location entry.
1408bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
140911a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
14107f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    // Record the offset of this source-location entry.
14117f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
14127f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
141314f79002e58556798e86168c63e48d533287eda5Douglas Gregor    // Figure out which record code to use.
141414f79002e58556798e86168c63e48d533287eda5Douglas Gregor    unsigned Code;
141514f79002e58556798e86168c63e48d533287eda5Douglas Gregor    if (SLoc->isFile()) {
141614f79002e58556798e86168c63e48d533287eda5Douglas Gregor      if (SLoc->getFile().getContentCache()->Entry)
14178538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Code = SM_SLOC_FILE_ENTRY;
141814f79002e58556798e86168c63e48d533287eda5Douglas Gregor      else
14198538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Code = SM_SLOC_BUFFER_ENTRY;
142014f79002e58556798e86168c63e48d533287eda5Douglas Gregor    } else
14218538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Code = SM_SLOC_INSTANTIATION_ENTRY;
14227f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    Record.clear();
142314f79002e58556798e86168c63e48d533287eda5Douglas Gregor    Record.push_back(Code);
142414f79002e58556798e86168c63e48d533287eda5Douglas Gregor
142514f79002e58556798e86168c63e48d533287eda5Douglas Gregor    Record.push_back(SLoc->getOffset());
142614f79002e58556798e86168c63e48d533287eda5Douglas Gregor    if (SLoc->isFile()) {
142714f79002e58556798e86168c63e48d533287eda5Douglas Gregor      const SrcMgr::FileInfo &File = SLoc->getFile();
142814f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(File.getIncludeLoc().getRawEncoding());
142914f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1430bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor      Record.push_back(File.hasLineDirectives());
143114f79002e58556798e86168c63e48d533287eda5Douglas Gregor
143214f79002e58556798e86168c63e48d533287eda5Douglas Gregor      const SrcMgr::ContentCache *Content = File.getContentCache();
143314f79002e58556798e86168c63e48d533287eda5Douglas Gregor      if (Content->Entry) {
143414f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // The source location entry is a file. The blob associated
143514f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // with this entry is the file name.
14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14372d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor        // Emit size/modification time for this file.
14382d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor        Record.push_back(Content->Entry->getSize());
14392d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor        Record.push_back(Content->Entry->getModificationTime());
14402d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor
1441e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        // Turn the file name into an absolute path, if it isn't already.
1442e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        const char *Filename = Content->Entry->getName();
1443fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer        llvm::SmallString<128> FilePath(Filename);
1444fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer        llvm::sys::fs::make_absolute(FilePath);
1445aba54a95e9d5e4dc9056abec6bb70ea777c4a7bcKovarththanan Rajaratnam        Filename = FilePath.c_str();
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1447e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1448ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar        Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
144914f79002e58556798e86168c63e48d533287eda5Douglas Gregor      } else {
145014f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // The source location entry is a buffer. The blob associated
145114f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // with this entry contains the contents of the buffer.
145214f79002e58556798e86168c63e48d533287eda5Douglas Gregor
145314f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // We add one to the size so that we capture the trailing NULL
145414f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // that is required by llvm::MemoryBuffer::getMemBuffer (on
145514f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // the reader side).
145636c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor        const llvm::MemoryBuffer *Buffer
1457e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner          = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
145814f79002e58556798e86168c63e48d533287eda5Douglas Gregor        const char *Name = Buffer->getBufferIdentifier();
1459ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar        Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1460ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar                                  llvm::StringRef(Name, strlen(Name) + 1));
146114f79002e58556798e86168c63e48d533287eda5Douglas Gregor        Record.clear();
14628538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Record.push_back(SM_SLOC_BUFFER_BLOB);
1463c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor        Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1464ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar                                  llvm::StringRef(Buffer->getBufferStart(),
1465ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar                                                  Buffer->getBufferSize() + 1));
14667f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
14677f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        if (strcmp(Name, "<built-in>") == 0)
14680fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl          PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
146914f79002e58556798e86168c63e48d533287eda5Douglas Gregor      }
147014f79002e58556798e86168c63e48d533287eda5Douglas Gregor    } else {
147114f79002e58556798e86168c63e48d533287eda5Douglas Gregor      // The source location entry is an instantiation.
147214f79002e58556798e86168c63e48d533287eda5Douglas Gregor      const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
147314f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(Inst.getSpellingLoc().getRawEncoding());
147414f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
147514f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
147614f79002e58556798e86168c63e48d533287eda5Douglas Gregor
1477f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor      // Compute the token length for this macro expansion.
1478f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor      unsigned NextOffset = SourceMgr.getNextOffset();
1479bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor      if (I + 1 != N)
1480bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor        NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
1481f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor      Record.push_back(NextOffset - SLoc->getOffset() - 1);
1482c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor      Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
148314f79002e58556798e86168c63e48d533287eda5Douglas Gregor    }
148414f79002e58556798e86168c63e48d533287eda5Douglas Gregor  }
148514f79002e58556798e86168c63e48d533287eda5Douglas Gregor
14867f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Stream.ExitBlock();
1487bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
14887f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  if (SLocEntryOffsets.empty())
14897f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return;
14902eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor
14913397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Write the source-location offsets table into the AST block. This
14927f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  // table is used for lazily loading source-location information.
14937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  using namespace llvm;
14947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
14958538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
14967f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
14977f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
14987f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
14997f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Record.clear();
15028538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(SOURCE_LOCATION_OFFSETS);
15037f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Record.push_back(SLocEntryOffsets.size());
15048db9faed418ea743f55c31e1ccecadbb23bd55b7Sebastian Redl  unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
15058db9faed418ea743f55c31e1ccecadbb23bd55b7Sebastian Redl  Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
15067f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1507ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(SLocEntryOffsets),
1508090d9b53e32bb30d9e74de895bb59b409bd49e00Chris Lattner                           SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
15097f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
15103397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Write the source location entry preloads array, telling the AST
15117f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  // reader which source locations entries it should load eagerly.
15128538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
151314f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
151414f79002e58556798e86168c63e48d533287eda5Douglas Gregor
15154fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
15164fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Preprocessor Serialization
15174fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
15184fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
15199c7361006b8cd95333958150dd0c803261807b68Douglas Gregorstatic int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
15209c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  const std::pair<const IdentifierInfo *, MacroInfo *> &X =
15219c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
15229c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
15239c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
15249c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  return X.first->getName().compare(Y.first->getName());
15259c7361006b8cd95333958150dd0c803261807b68Douglas Gregor}
15269c7361006b8cd95333958150dd0c803261807b68Douglas Gregor
15270b1fb988012da21d996c43e36867787a7a07b889Chris Lattner/// \brief Writes the block containing the serialized form of the
15280b1fb988012da21d996c43e36867787a7a07b889Chris Lattner/// preprocessor.
15290b1fb988012da21d996c43e36867787a7a07b889Chris Lattner///
1530a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WritePreprocessor(const Preprocessor &PP) {
15317c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  RecordData Record;
15327c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
1533c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  // If the preprocessor __COUNTER__ value has been bumped, remember it.
1534c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  if (PP.getCounterValue() != 0) {
1535c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner    Record.push_back(PP.getCounterValue());
15368538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(PP_COUNTER_VALUE, Record);
1537c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner    Record.clear();
15382eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  }
15392eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor
15402eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  // Enter the preprocessor block.
1541ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15433397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
15442eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  // FIXME: use diagnostics subsystem for localization etc.
15452eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  if (PP.SawDateOrTime())
15462eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor    fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1548ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor
15497c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  // Loop over all the macro definitions that are live at the end of the file,
15507c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  // emitting each to the PP section.
15516a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
155220249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
15539c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  // Construct the list of macro definitions that need to be serialized.
15549c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
15559c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    MacrosToEmit;
15569c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
1557040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor  for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1558040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor                                    E = PP.macro_end(Chain == 0);
15597c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner       I != E; ++I) {
15609c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    MacroDefinitionsSeen.insert(I->first);
15619c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    MacrosToEmit.push_back(std::make_pair(I->first, I->second));
15629c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  }
15639c7361006b8cd95333958150dd0c803261807b68Douglas Gregor
15649c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  // Sort the set of macro definitions that need to be serialized by the
15659c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  // name of the macro, to provide a stable ordering.
15669c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
15679c7361006b8cd95333958150dd0c803261807b68Douglas Gregor                       &compareMacroDefinitions);
15689c7361006b8cd95333958150dd0c803261807b68Douglas Gregor
1569040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor  // Resolve any identifiers that defined macros at the time they were
1570040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor  // deserialized, adding them to the list of macros to emit (if appropriate).
1571040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor  for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1572040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor    IdentifierInfo *Name
1573040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor      = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1574040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor    if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1575040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor      MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1576040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor  }
1577040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor
15789c7361006b8cd95333958150dd0c803261807b68Douglas Gregor  for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
15799c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    const IdentifierInfo *Name = MacrosToEmit[I].first;
15809c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    MacroInfo *MI = MacrosToEmit[I].second;
1581040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor    if (!MI)
1582040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor      continue;
1583040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor
15843397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    // Don't emit builtin macros like __LINE__ to the AST file unless they have
15857c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    // been redefined by the header (in which case they are not isBuiltinMacro).
15863397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    // Also skip macros from a AST file if we're chaining.
1587ee9b0ba29094eb177a285e726ab96e979e5b9c61Douglas Gregor
1588ee9b0ba29094eb177a285e726ab96e979e5b9c61Douglas Gregor    // FIXME: There is a (probably minor) optimization we could do here, if
1589ee9b0ba29094eb177a285e726ab96e979e5b9c61Douglas Gregor    // the macro comes from the original PCH but the identifier comes from a
1590ee9b0ba29094eb177a285e726ab96e979e5b9c61Douglas Gregor    // chained PCH, by storing the offset into the original PCH rather than
1591ee9b0ba29094eb177a285e726ab96e979e5b9c61Douglas Gregor    // writing the macro definition a second time.
159220249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer    if (MI->isBuiltinMacro() ||
15939c7361006b8cd95333958150dd0c803261807b68Douglas Gregor        (Chain && Name->isFromAST() && MI->isFromAST()))
15947c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      continue;
15957c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
15969c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    AddIdentifierRef(Name, Record);
15979c7361006b8cd95333958150dd0c803261807b68Douglas Gregor    MacroOffsets[Name] = Stream.GetCurrentBitNo();
15987c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    Record.push_back(MI->getDefinitionLoc().getRawEncoding());
15997c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    Record.push_back(MI->isUsed());
16001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16017c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    unsigned Code;
16027c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    if (MI->isObjectLike()) {
16038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Code = PP_MACRO_OBJECT_LIKE;
16047c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    } else {
16058538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Code = PP_MACRO_FUNCTION_LIKE;
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16077c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      Record.push_back(MI->isC99Varargs());
16087c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      Record.push_back(MI->isGNUVarargs());
16097c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      Record.push_back(MI->getNumArgs());
16107c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
16117c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner           I != E; ++I)
16127356a31327be9b3c3434a0c88746028980da5684Chris Lattner        AddIdentifierRef(*I, Record);
16137c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    }
161420249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
16156a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    // If we have a detailed preprocessing record, record the macro definition
16166a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    // ID that corresponds to this macro.
16176a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    if (PPRec)
16186a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
161920249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
1620c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    Stream.EmitRecord(Code, Record);
16217c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    Record.clear();
1622f04ad69fed38d26fc0d6f7d6fd0a4631ddfbc7feChris Lattner
1623df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner    // Emit the tokens array.
1624df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner    for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1625df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // Note that we know that the preprocessor does not have any annotation
1626df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // tokens in it because they are created by the parser, and thus can't be
1627df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // in a macro definition.
1628df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      const Token &Tok = MI->getReplacementToken(TokNo);
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1630df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getLocation().getRawEncoding());
1631df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getLength());
1632df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner
1633df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // FIXME: When reading literal tokens, reconstruct the literal pointer if
1634df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // it is needed.
16357356a31327be9b3c3434a0c88746028980da5684Chris Lattner      AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1636df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // FIXME: Should translate token kind to a stable encoding.
1637df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getKind());
1638df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // FIXME: Should translate token flags to a stable encoding.
1639df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getFlags());
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16418538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Stream.EmitRecord(PP_TOKEN, Record);
1642df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.clear();
1643df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner    }
164437e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    ++NumMacros;
16457c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  }
16464800a5c79023271408af49797e09be32aca93232Douglas Gregor  Stream.ExitBlock();
16474800a5c79023271408af49797e09be32aca93232Douglas Gregor
16484800a5c79023271408af49797e09be32aca93232Douglas Gregor  if (PPRec)
16494800a5c79023271408af49797e09be32aca93232Douglas Gregor    WritePreprocessorDetail(*PPRec);
16504800a5c79023271408af49797e09be32aca93232Douglas Gregor}
16514800a5c79023271408af49797e09be32aca93232Douglas Gregor
16524800a5c79023271408af49797e09be32aca93232Douglas Gregorvoid ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
16534800a5c79023271408af49797e09be32aca93232Douglas Gregor  if (PPRec.begin(Chain) == PPRec.end(Chain))
16544800a5c79023271408af49797e09be32aca93232Douglas Gregor    return;
16554800a5c79023271408af49797e09be32aca93232Douglas Gregor
16564800a5c79023271408af49797e09be32aca93232Douglas Gregor  // Enter the preprocessor block.
16574800a5c79023271408af49797e09be32aca93232Douglas Gregor  Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
165820249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
16596a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  // If the preprocessor has a preprocessing record, emit it.
16606a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  unsigned NumPreprocessingRecords = 0;
16614800a5c79023271408af49797e09be32aca93232Douglas Gregor  using namespace llvm;
16624800a5c79023271408af49797e09be32aca93232Douglas Gregor
16634800a5c79023271408af49797e09be32aca93232Douglas Gregor  // Set up the abbreviation for
16644800a5c79023271408af49797e09be32aca93232Douglas Gregor  unsigned InclusionAbbrev = 0;
16654800a5c79023271408af49797e09be32aca93232Douglas Gregor  {
16664800a5c79023271408af49797e09be32aca93232Douglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
16674800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
16684800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
16694800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
16704800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
16714800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
16724800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
16734800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
16744800a5c79023271408af49797e09be32aca93232Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
16754800a5c79023271408af49797e09be32aca93232Douglas Gregor    InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
16764800a5c79023271408af49797e09be32aca93232Douglas Gregor  }
16774800a5c79023271408af49797e09be32aca93232Douglas Gregor
16784800a5c79023271408af49797e09be32aca93232Douglas Gregor  unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
16794800a5c79023271408af49797e09be32aca93232Douglas Gregor  RecordData Record;
16804800a5c79023271408af49797e09be32aca93232Douglas Gregor  for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
16814800a5c79023271408af49797e09be32aca93232Douglas Gregor                                  EEnd = PPRec.end(Chain);
16824800a5c79023271408af49797e09be32aca93232Douglas Gregor       E != EEnd; ++E) {
16834800a5c79023271408af49797e09be32aca93232Douglas Gregor    Record.clear();
168420249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
16854800a5c79023271408af49797e09be32aca93232Douglas Gregor    if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
16864800a5c79023271408af49797e09be32aca93232Douglas Gregor      // Record this macro definition's location.
16874800a5c79023271408af49797e09be32aca93232Douglas Gregor      MacroID ID = getMacroDefinitionID(MD);
16884800a5c79023271408af49797e09be32aca93232Douglas Gregor
16894800a5c79023271408af49797e09be32aca93232Douglas Gregor      // Don't write the macro definition if it is from another AST file.
16904800a5c79023271408af49797e09be32aca93232Douglas Gregor      if (ID < FirstMacroID)
16916a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        continue;
16924800a5c79023271408af49797e09be32aca93232Douglas Gregor
169389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor      // Notify the serialization listener that we're serializing this entity.
169489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor      if (SerializationListener)
169589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor        SerializationListener->SerializedPreprocessedEntity(*E,
16964800a5c79023271408af49797e09be32aca93232Douglas Gregor                                                    Stream.GetCurrentBitNo());
169789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
16984800a5c79023271408af49797e09be32aca93232Douglas Gregor      unsigned Position = ID - FirstMacroID;
16994800a5c79023271408af49797e09be32aca93232Douglas Gregor      if (Position != MacroDefinitionOffsets.size()) {
17004800a5c79023271408af49797e09be32aca93232Douglas Gregor        if (Position > MacroDefinitionOffsets.size())
17014800a5c79023271408af49797e09be32aca93232Douglas Gregor          MacroDefinitionOffsets.resize(Position + 1);
17024800a5c79023271408af49797e09be32aca93232Douglas Gregor
17034800a5c79023271408af49797e09be32aca93232Douglas Gregor        MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
17044800a5c79023271408af49797e09be32aca93232Douglas Gregor      } else
17054800a5c79023271408af49797e09be32aca93232Douglas Gregor        MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
170689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
17074800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(IndexBase + NumPreprocessingRecords++);
17084800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(ID);
17094800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(MD->getSourceRange().getBegin(), Record);
17104800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(MD->getSourceRange().getEnd(), Record);
17114800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddIdentifierRef(MD->getName(), Record);
17124800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(MD->getLocation(), Record);
17134800a5c79023271408af49797e09be32aca93232Douglas Gregor      Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
17144800a5c79023271408af49797e09be32aca93232Douglas Gregor      continue;
17154800a5c79023271408af49797e09be32aca93232Douglas Gregor    }
17164800a5c79023271408af49797e09be32aca93232Douglas Gregor
17174800a5c79023271408af49797e09be32aca93232Douglas Gregor    // Notify the serialization listener that we're serializing this entity.
17184800a5c79023271408af49797e09be32aca93232Douglas Gregor    if (SerializationListener)
17194800a5c79023271408af49797e09be32aca93232Douglas Gregor      SerializationListener->SerializedPreprocessedEntity(*E,
17204800a5c79023271408af49797e09be32aca93232Douglas Gregor                                                    Stream.GetCurrentBitNo());
17214800a5c79023271408af49797e09be32aca93232Douglas Gregor
17224800a5c79023271408af49797e09be32aca93232Douglas Gregor    if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
17234800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(IndexBase + NumPreprocessingRecords++);
17244800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(MI->getSourceRange().getBegin(), Record);
17254800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(MI->getSourceRange().getEnd(), Record);
17264800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddIdentifierRef(MI->getName(), Record);
17274800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(getMacroDefinitionID(MI->getDefinition()));
17284800a5c79023271408af49797e09be32aca93232Douglas Gregor      Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
17294800a5c79023271408af49797e09be32aca93232Douglas Gregor      continue;
17306a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    }
173120249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
17324800a5c79023271408af49797e09be32aca93232Douglas Gregor    if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
17334800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(PPD_INCLUSION_DIRECTIVE);
17344800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(IndexBase + NumPreprocessingRecords++);
17354800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(ID->getSourceRange().getBegin(), Record);
17364800a5c79023271408af49797e09be32aca93232Douglas Gregor      AddSourceLocation(ID->getSourceRange().getEnd(), Record);
17374800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(ID->getFileName().size());
17384800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(ID->wasInQuotes());
17394800a5c79023271408af49797e09be32aca93232Douglas Gregor      Record.push_back(static_cast<unsigned>(ID->getKind()));
17404800a5c79023271408af49797e09be32aca93232Douglas Gregor      llvm::SmallString<64> Buffer;
17414800a5c79023271408af49797e09be32aca93232Douglas Gregor      Buffer += ID->getFileName();
17424800a5c79023271408af49797e09be32aca93232Douglas Gregor      Buffer += ID->getFile()->getName();
17434800a5c79023271408af49797e09be32aca93232Douglas Gregor      Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
17444800a5c79023271408af49797e09be32aca93232Douglas Gregor      continue;
17454800a5c79023271408af49797e09be32aca93232Douglas Gregor    }
17464800a5c79023271408af49797e09be32aca93232Douglas Gregor
17474800a5c79023271408af49797e09be32aca93232Douglas Gregor    llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
17484800a5c79023271408af49797e09be32aca93232Douglas Gregor  }
1749c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.ExitBlock();
175020249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
17516a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  // Write the offsets table for the preprocessing record.
17526a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (NumPreprocessingRecords > 0) {
17536a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    // Write the offsets table for identifier IDs.
17546a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    using namespace llvm;
17556a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
17568538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
17576a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
17586a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
17596a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
17606a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
176120249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
17626a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Record.clear();
17638538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(MACRO_DEFINITION_OFFSETS);
17646a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Record.push_back(NumPreprocessingRecords);
17656a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Record.push_back(MacroDefinitionOffsets.size());
17666a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1767ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                              (const char *)data(MacroDefinitionOffsets),
17686a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor                              MacroDefinitionOffsets.size() * sizeof(uint32_t));
17696a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  }
17700b1fb988012da21d996c43e36867787a7a07b889Chris Lattner}
17710b1fb988012da21d996c43e36867787a7a07b889Chris Lattner
17723efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidisvoid ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
1773f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  RecordData Record;
17743efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  for (Diagnostic::DiagStatePointsTy::const_iterator
17753efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis         I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
17763efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis         I != E; ++I) {
17773efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    const Diagnostic::DiagStatePoint &point = *I;
17783efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    if (point.Loc.isInvalid())
17793efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      continue;
17803efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis
17813efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    Record.push_back(point.Loc.getRawEncoding());
17823efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    for (Diagnostic::DiagState::iterator
17833efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis           I = point.State->begin(), E = point.State->end(); I != E; ++I) {
17843efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      unsigned diag = I->first, map = I->second;
17853efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      if (map & 0x10) { // mapping from a diagnostic pragma.
17863efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis        Record.push_back(diag);
17873efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis        Record.push_back(map & 0x7);
17883efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis      }
1789f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis    }
17903efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    Record.push_back(-1); // mark the end of the diag/map pairs for this
17913efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis                          // location.
1792f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  }
1793f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis
179460f7684881f9f6bc779e91212aea89d9afc723ddArgyrios Kyrtzidis  if (!Record.empty())
17953efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
1796f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis}
1797f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis
17984fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
17994fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Type Serialization
18004fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
18010b1fb988012da21d996c43e36867787a7a07b889Chris Lattner
18023397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the representation of a type to the AST stream.
1803a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteType(QualType T) {
180401b81c4d074bba9c18372d521405dfe32fc4f552Argyrios Kyrtzidis  TypeIdx &Idx = TypeIdxs[T];
1805c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis  if (Idx.getIndex() == 0) // we haven't seen this type before.
1806c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis    Idx = TypeIdx(NextTypeID++);
18071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
180897475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
180955f48de5f6b10b50f4d88a9f7235f4532f799550Douglas Gregor
18102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Record the offset for this type.
1811c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis  unsigned Index = Idx.getIndex() - FirstTypeID;
1812681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  if (TypeOffsets.size() == Index)
1813c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    TypeOffsets.push_back(Stream.GetCurrentBitNo());
1814681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  else if (TypeOffsets.size() < Index) {
1815681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    TypeOffsets.resize(Index + 1);
1816681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    TypeOffsets[Index] = Stream.GetCurrentBitNo();
18172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
18182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
18192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  RecordData Record;
18201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Emit the type's representation.
18223397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  ASTTypeWriter W(*this, Record);
18230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1824a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (T.hasLocalNonFastQualifiers()) {
1825a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    Qualifiers Qs = T.getLocalQualifiers();
1826a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    AddTypeRef(T.getLocalUnqualifiedType(), Record);
18270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Record.push_back(Qs.getAsOpaqueValue());
18288538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    W.Code = TYPE_EXT_QUAL;
18290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  } else {
18300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    switch (T->getTypeClass()) {
18310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      // For all of the concrete, non-dependent types, call the
18320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      // appropriate visitor function.
18332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define TYPE(Class, Base) \
1834b7166334d897e1e4e6a5b428fe2d0ec752ef187fMike Stump    case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
18352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define ABSTRACT_TYPE(Class, Base)
18362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/TypeNodes.def"
18370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
18382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
18392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
18402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Emit the serialized record.
1841c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.EmitRecord(W.Code, Record);
18420b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor
18430b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor  // Flush any expressions that were written as part of this type.
1844c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  FlushStmts();
18452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
18462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
18474fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
18484fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Declaration Serialization
18494fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
18504fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
18512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Write the block containing all of the declaration IDs
18522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// lexically declared within the given DeclContext.
18532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor///
18542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
18552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// bistream, or 0 if no block was written.
1856a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redluint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
18572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                 DeclContext *DC) {
185817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  if (DC->decls_empty())
18592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return 0;
18602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1861c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  uint64_t Offset = Stream.GetCurrentBitNo();
18622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  RecordData Record;
18638538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(DECL_CONTEXT_LEXICAL);
1864eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<KindDeclIDPair, 64> Decls;
186517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
186617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         D != DEnd; ++D)
1867eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis    Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
18682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
18692512308525ff328aa992da0b5ee14a488d2ea93aDouglas Gregor  ++NumLexicalDeclContexts;
1870681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
1871eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis                            reinterpret_cast<char*>(Decls.data()),
1872eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis                            Decls.size() * sizeof(KindDeclIDPair));
18732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return Offset;
18742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
18752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1876a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteTypeDeclOffsets() {
18771476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  using namespace llvm;
18781476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  RecordData Record;
18791476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
18801476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // Write the type offsets array
18811476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
18828538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
18831476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
18841476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
18851476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
18861476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.clear();
18878538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(TYPE_OFFSET);
18881476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.push_back(TypeOffsets.size());
18891476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1890ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(TypeOffsets),
18911476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl                            TypeOffsets.size() * sizeof(TypeOffsets[0]));
18921476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
18931476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // Write the declaration offsets array
18941476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev = new BitCodeAbbrev();
18958538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
18961476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
18971476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
18981476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
18991476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.clear();
19008538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(DECL_OFFSET);
19011476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.push_back(DeclOffsets.size());
19021476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1903ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(DeclOffsets),
19041476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl                            DeclOffsets.size() * sizeof(DeclOffsets[0]));
19051476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl}
19061476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
19074fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
19084fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Global Method Pool and Selector Serialization
19094fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
19104fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
19113251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregornamespace {
1912f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor// Trait used for the on-disk hash table used in the method pool.
19133397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlclass ASTMethodPoolTrait {
1914a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter &Writer;
1915f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1916f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregorpublic:
1917f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  typedef Selector key_type;
1918f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  typedef key_type key_type_ref;
19191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19205d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  struct data_type {
19218538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    SelectorID ID;
19225d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    ObjCMethodList Instance, Factory;
19235d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  };
1924f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  typedef const data_type& data_type_ref;
1925f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
19263397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1928f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  static unsigned ComputeHash(Selector Sel) {
19290eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidis    return serialization::ComputeHash(Sel);
1930f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<unsigned,unsigned>
1933f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1934f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor                      data_type_ref Methods) {
1935f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1936f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, KeyLen);
19375d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
19385d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1939f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1940f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1941f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        DataLen += 4;
19425d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1943f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1944f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1945f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        DataLen += 4;
1946f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, DataLen);
1947f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    return std::make_pair(KeyLen, DataLen);
1948f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
19491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
195083941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor  void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
19511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    uint64_t Start = Out.tell();
195283941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    assert((Start >> 32) == 0 && "Selector key offset too large");
195383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Writer.SetSelectorOffset(Sel, Start);
1954f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned N = Sel.getNumArgs();
1955f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, N);
1956f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    if (N == 0)
1957f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      N = 1;
1958f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    for (unsigned I = 0; I != N; ++I)
19591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      clang::io::Emit32(Out,
1960f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor                    Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1961f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
19621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1963f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  void EmitData(llvm::raw_ostream& Out, key_type_ref,
1964a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor                data_type_ref Methods, unsigned DataLen) {
1965a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor    uint64_t Start = Out.tell(); (void)Start;
19665d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    clang::io::Emit32(Out, Methods.ID);
1967f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned NumInstanceMethods = 0;
19685d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1969f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1970f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1971f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        ++NumInstanceMethods;
1972f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1973f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned NumFactoryMethods = 0;
19745d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1975f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1976f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1977f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        ++NumFactoryMethods;
1978f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1979f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, NumInstanceMethods);
1980f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, NumFactoryMethods);
19815d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1982f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1983f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1984f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
19855d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1986f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1987f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1988f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
1989a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor
1990a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor    assert(Out.tell() - Start == DataLen && "Data length is wrong");
1991f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
1992f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor};
1993f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor} // end anonymous namespace
1994f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1995059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl/// \brief Write ObjC data: selectors and the method pool.
1996f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor///
1997f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor/// The method pool contains both instance and factory methods, stored
1998059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl/// in an on-disk hash table indexed by the selector. The hash table also
1999059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl/// contains an empty entry for every other selector known to Sema.
2000a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteSelectors(Sema &SemaRef) {
2001f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  using namespace llvm;
2002f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
2003059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl  // Do we have to do anything at all?
20045d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
2005059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl    return;
2006e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  unsigned NumTableEntries = 0;
2007059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl  // Create and write out the blob that contains selectors and the method pool.
2008f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  {
20093397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
20105d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    ASTMethodPoolTrait Trait(*this);
20111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2012059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl    // Create the on-disk hash table representation. We walk through every
2013059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl    // selector we've seen and look it up in the method pool.
2014e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
20158538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    for (llvm::DenseMap<Selector, SelectorID>::iterator
20165d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl             I = SelectorIDs.begin(), E = SelectorIDs.end();
20175d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl         I != E; ++I) {
20185d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      Selector S = I->first;
2019059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl      Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
20203397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      ASTMethodPoolTrait::data_type Data = {
20215d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        I->second,
20225d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        ObjCMethodList(),
20235d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        ObjCMethodList()
20245d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      };
20255d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      if (F != SemaRef.MethodPool.end()) {
20265d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        Data.Instance = F->second.first;
20275d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        Data.Factory = F->second.second;
20285d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      }
20293397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      // Only write this selector if it's not in an existing AST or something
2030e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl      // changed.
2031e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl      if (Chain && I->second < FirstSelectorID) {
2032e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        // Selector already exists. Did it change?
2033e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        bool changed = false;
2034e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2035e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl             M = M->Next) {
2036e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl          if (M->Method->getPCHLevel() == 0)
2037e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl            changed = true;
2038e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        }
2039e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2040e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl             M = M->Next) {
2041e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl          if (M->Method->getPCHLevel() == 0)
2042e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl            changed = true;
2043e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        }
2044e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        if (!changed)
2045e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl          continue;
2046fa78dec572259aca763457b435744f79d822c5d4Sebastian Redl      } else if (Data.Instance.Method || Data.Factory.Method) {
2047fa78dec572259aca763457b435744f79d822c5d4Sebastian Redl        // A new method pool entry.
2048fa78dec572259aca763457b435744f79d822c5d4Sebastian Redl        ++NumTableEntries;
2049e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl      }
20505d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Generator.insert(S, Data, Trait);
2051f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    }
2052f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
2053f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    // Create the on-disk hash table in a buffer.
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SmallString<4096> MethodPool;
2055f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    uint32_t BucketOffset;
2056f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    {
20573397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      ASTMethodPoolTrait Trait(*this);
2058f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      llvm::raw_svector_ostream Out(MethodPool);
2059f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      // Make sure that no bucket is at offset 0
2060a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor      clang::io::Emit32(Out, 0);
2061f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      BucketOffset = Generator.Emit(Out, Trait);
2062f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    }
2063f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
2064f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    // Create a blob abbreviation
2065f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
20668538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
2067f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
206883941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2069f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2070f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2071f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
207283941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    // Write the method pool
2073f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    RecordData Record;
20748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(METHOD_POOL);
2075f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    Record.push_back(BucketOffset);
2076e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    Record.push_back(NumTableEntries);
2077ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar    Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
207883941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor
207983941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    // Create a blob abbreviation for the selector table offsets.
208083941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev = new BitCodeAbbrev();
20818538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
20827c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
208383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
208483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
208583941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor
208683941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    // Write the selector offsets table.
208783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Record.clear();
20888538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(SELECTOR_OFFSETS);
208983941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Record.push_back(SelectorOffsets.size());
209083941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2091ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                              (const char *)data(SelectorOffsets),
209283941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor                              SelectorOffsets.size() * 4);
2093f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
2094f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor}
2095f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
20963397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the selectors referenced in @selector expression into AST file.
2097a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
2098320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  using namespace llvm;
2099320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  if (SemaRef.ReferencedSelectors.empty())
2100320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    return;
2101725cd9686a0f5bb6c994cb3e43f58b63567c6860Sebastian Redl
2102320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  RecordData Record;
2103725cd9686a0f5bb6c994cb3e43f58b63567c6860Sebastian Redl
21043397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Note: this writes out all references even for a dependent AST. But it is
2105a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  // very tricky to fix, and given that @selector shouldn't really appear in
2106a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  // headers, probably not worth it. It's not a correctness issue.
2107320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  for (DenseMap<Selector, SourceLocation>::iterator S =
2108320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian       SemaRef.ReferencedSelectors.begin(),
2109320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian       E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2110320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    Selector Sel = (*S).first;
2111320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    SourceLocation Loc = (*S).second;
2112320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    AddSelectorRef(Sel, Record);
2113320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    AddSourceLocation(Loc, Record);
2114320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  }
21158538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
2116320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian}
2117320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian
21184fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
21194fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Identifier Table Serialization
21204fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
21214fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
2122f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregornamespace {
21233397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlclass ASTIdentifierTableTrait {
2124a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter &Writer;
212537e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor  Preprocessor &PP;
21263251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
2127a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  /// \brief Determines whether this is an "interesting" identifier
2128a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  /// that needs a full IdentifierInfo structure written into the hash
2129a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  /// table.
2130a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  static bool isInterestingIdentifier(const IdentifierInfo *II) {
2131a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    return II->isPoisoned() ||
2132a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->isExtensionToken() ||
2133a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->hasMacroDefinition() ||
2134a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->getObjCOrBuiltinID() ||
2135a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->getFETokenInfo<void>();
2136a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  }
2137a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor
21383251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregorpublic:
21393251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  typedef const IdentifierInfo* key_type;
21403251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  typedef key_type  key_type_ref;
21411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21428538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  typedef IdentID data_type;
21433251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  typedef data_type data_type_ref;
21441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21453397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
214637e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    : Writer(Writer), PP(PP) { }
21473251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
21483251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  static unsigned ComputeHash(const IdentifierInfo* II) {
21492596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar    return llvm::HashString(II->getName());
21503251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
21511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<unsigned,unsigned>
21531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
21548538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                      IdentID ID) {
2155e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar    unsigned KeyLen = II->getLength() + 1;
2156a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2157a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    if (isInterestingIdentifier(II)) {
21585998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor      DataLen += 2; // 2 bytes for builtin ID, flags
21591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (II->hasMacroDefinition() &&
2160a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor          !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
21615998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor        DataLen += 4;
2162a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2163a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor                                     DEnd = IdentifierResolver::end();
2164a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor           D != DEnd; ++D)
21658538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        DataLen += sizeof(DeclID);
2166a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    }
2167668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    clang::io::Emit16(Out, DataLen);
216802fc75169afe785c114e795ec4d24edfb4073c42Douglas Gregor    // We emit the key length after the data length so that every
216902fc75169afe785c114e795ec4d24edfb4073c42Douglas Gregor    // string is preceded by a 16-bit length. This matches the PTH
217002fc75169afe785c114e795ec4d24edfb4073c42Douglas Gregor    // format for storing identifiers.
2171d6595a40fe12e3d5ffe5ce48987b379d547439a4Douglas Gregor    clang::io::Emit16(Out, KeyLen);
21723251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    return std::make_pair(KeyLen, DataLen);
21733251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
21763251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor               unsigned KeyLen) {
21773251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    // Record the location of the key data.  This is used when generating
21783251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    // the mapping from persistent IDs to strings.
21793251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    Writer.SetIdentifierOffset(II, Out.tell());
2180e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar    Out.write(II->getNameStart(), KeyLen);
21813251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
21821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
21848538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                IdentID ID, unsigned) {
2185a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    if (!isInterestingIdentifier(II)) {
2186a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      clang::io::Emit32(Out, ID << 1);
2187a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      return;
2188a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    }
21895998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor
2190a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    clang::io::Emit32(Out, (ID << 1) | 0x01);
21913251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    uint32_t Bits = 0;
21921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    bool hasMacroDefinition =
21931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      II->hasMacroDefinition() &&
219437e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor      !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
21955998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor    Bits = (uint32_t)II->getObjCOrBuiltinID();
2196b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2197b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2198b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(II->isPoisoned());
2199646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
2200b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
22015998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor    clang::io::Emit16(Out, Bits);
22023251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
220337e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    if (hasMacroDefinition)
22045998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor      clang::io::Emit32(Out, Writer.getMacroOffset(II));
220537e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor
2206668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // Emit the declaration IDs in reverse order, because the
2207668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // IdentifierResolver provides the declarations as they would be
2208668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // visible (e.g., the function "stat" would come before the struct
2209668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2210668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // adds declarations to the end of the list (so we need to see the
2211668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // struct "status" before the function "status").
2212f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    // Only emit declarations that aren't from a chained PCH, though.
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2214668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor                                        IdentifierResolver::end());
2215668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2216668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor                                                      DEnd = Decls.rend();
22173251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor         D != DEnd; ++D)
2218d8c5abb096a5f6babb3709180fe304be5462bcc1Sebastian Redl      clang::io::Emit32(Out, Writer.getDeclID(*D));
22193251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
22203251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor};
22213251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor} // end anonymous namespace
22223251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
22233397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the identifier table into the AST file.
2224afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor///
2225afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor/// The identifier table consists of a blob containing string data
2226afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor/// (the actual identifiers themselves) and a separate "offsets" index
2227afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor/// that maps identifier IDs to locations within the blob.
2228a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
2229afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  using namespace llvm;
2230afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
2231afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  // Create and write out the blob that contains the identifier
2232afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  // strings.
2233afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  {
22343397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
22355d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    ASTIdentifierTableTrait Trait(*this, PP);
22361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
223792b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // Look for any identifiers that were named while processing the
223892b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // headers, but are otherwise not needed. We add these to the hash
223992b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // table to enable checking of the predefines buffer in the case
22403397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    // where the user adds new macro definitions when building the AST
224192b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // file.
224292b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
224392b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor                                IDEnd = PP.getIdentifierTable().end();
224492b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor         ID != IDEnd; ++ID)
224592b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor      getIdentifierRef(ID->second);
224692b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor
2247f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    // Create the on-disk hash table representation. We only store offsets
2248f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    // for identifiers that appear here for the first time.
2249f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    IdentifierOffsets.resize(NextIdentID - FirstIdentID);
22508538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
2251afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor           ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2252afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor         ID != IDEnd; ++ID) {
2253afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor      assert(ID->first && "NULL identifier in identifier table");
22543c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl      if (!Chain || !ID->first->isFromAST())
22555d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis        Generator.insert(ID->first, ID->second, Trait);
22563251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    }
2257afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
22583251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    // Create the on-disk hash table in a buffer.
22591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SmallString<4096> IdentifierTable;
2260668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    uint32_t BucketOffset;
22613251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    {
22623397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      ASTIdentifierTableTrait Trait(*this, PP);
22633251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor      llvm::raw_svector_ostream Out(IdentifierTable);
2264f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      // Make sure that no bucket is at offset 0
2265a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor      clang::io::Emit32(Out, 0);
2266668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor      BucketOffset = Generator.Emit(Out, Trait);
2267afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    }
2268afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
2269afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    // Create a blob abbreviation
2270afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
22718538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
2272668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
22733251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2274c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
2275afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
2276afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    // Write the identifier table
2277afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    RecordData Record;
22788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(IDENTIFIER_TABLE);
2279668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    Record.push_back(BucketOffset);
2280ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar    Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
2281afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  }
2282afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
2283afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  // Write the offsets table for identifier IDs.
22842b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
22858538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
22862b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
22872b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
22882b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
22892b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor
22902b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  RecordData Record;
22918538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(IDENTIFIER_OFFSET);
22922b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Record.push_back(IdentifierOffsets.size());
22932b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2294ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(IdentifierOffsets),
22952b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor                            IdentifierOffsets.size() * sizeof(uint32_t));
2296afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor}
2297afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
22984fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
22995d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis// DeclContext's Name Lookup Table Serialization
23005d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
23015d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23025d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidisnamespace {
23035d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis// Trait used for the on-disk hash table used in the method pool.
23045d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidisclass ASTDeclContextNameLookupTrait {
23055d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  ASTWriter &Writer;
23065d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23075d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidispublic:
23085d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef DeclarationName key_type;
23095d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef key_type key_type_ref;
23105d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23115d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef DeclContext::lookup_result data_type;
23125d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef const data_type& data_type_ref;
23135d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23145d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
23155d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23165d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  unsigned ComputeHash(DeclarationName Name) {
23175d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    llvm::FoldingSetNodeID ID;
23185d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    ID.AddInteger(Name.getNameKind());
23195d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23205d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    switch (Name.getNameKind()) {
23215d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::Identifier:
23225d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddString(Name.getAsIdentifierInfo()->getName());
23235d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23245d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCZeroArgSelector:
23255d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCOneArgSelector:
23265d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCMultiArgSelector:
23275d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
23285d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23295d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConstructorName:
23305d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXDestructorName:
23315d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConversionFunctionName:
23325d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
23335d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23345d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXOperatorName:
23355d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddInteger(Name.getCXXOverloadedOperator());
23365d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23375d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXLiteralOperatorName:
23385d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddString(Name.getCXXLiteralIdentifier()->getName());
23395d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXUsingDirective:
23405d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23415d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    }
23425d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23435d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    return ID.ComputeHash();
23445d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
23455d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23465d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  std::pair<unsigned,unsigned>
23475d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
23485d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis                      data_type_ref Lookup) {
23495d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    unsigned KeyLen = 1;
23505d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    switch (Name.getNameKind()) {
23515d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::Identifier:
23525d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCZeroArgSelector:
23535d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCOneArgSelector:
23545d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCMultiArgSelector:
23555d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConstructorName:
23565d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXDestructorName:
23575d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConversionFunctionName:
23585d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXLiteralOperatorName:
23595d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      KeyLen += 4;
23605d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23615d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXOperatorName:
23625d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      KeyLen += 1;
23635d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23645d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXUsingDirective:
23655d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23665d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    }
23675d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    clang::io::Emit16(Out, KeyLen);
23685d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23695d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    // 2 bytes for num of decls and 4 for each DeclID.
23705d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
23715d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    clang::io::Emit16(Out, DataLen);
23725d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23735d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    return std::make_pair(KeyLen, DataLen);
23745d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
23755d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23765d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
23775d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    using namespace clang::io;
23785d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
23795d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
23805d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    Emit8(Out, Name.getNameKind());
23815d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    switch (Name.getNameKind()) {
23825d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::Identifier:
23835d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
23845d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23855d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCZeroArgSelector:
23865d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCOneArgSelector:
23875d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCMultiArgSelector:
23885d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
23895d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23905d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConstructorName:
23915d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXDestructorName:
23925d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConversionFunctionName:
23935d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
23945d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23955d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXOperatorName:
23965d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
23975d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit8(Out, Name.getCXXOverloadedOperator());
23985d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
23995d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXLiteralOperatorName:
24005d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
24015d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
24025d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXUsingDirective:
24035d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
24045d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    }
24055d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
24065d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
24075d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  void EmitData(llvm::raw_ostream& Out, key_type_ref,
24085d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis                data_type Lookup, unsigned DataLen) {
24095d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    uint64_t Start = Out.tell(); (void)Start;
24105d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    clang::io::Emit16(Out, Lookup.second - Lookup.first);
24115d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    for (; Lookup.first != Lookup.second; ++Lookup.first)
24125d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
24135d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
24145d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    assert(Out.tell() - Start == DataLen && "Data length is wrong");
24155d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
24165d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis};
24175d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis} // end anonymous namespace
24185d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
2419074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis/// \brief Write the block containing all of the declaration IDs
2420074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis/// visible from the given DeclContext.
2421074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis///
2422074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
24231d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// bitstream, or 0 if no block was written.
2424074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidisuint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2425074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis                                                 DeclContext *DC) {
2426074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DC->getPrimaryContext() != DC)
2427074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2428074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2429074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Since there is no name lookup into functions or methods, don't bother to
2430074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // build a visible-declarations table for these entities.
2431074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DC->isFunctionOrMethod())
2432074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2433074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2434074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If not in C++, we perform name lookup for the translation unit via the
2435074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // IdentifierInfo chains, don't bother to build a visible-declarations table.
2436074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // FIXME: In C++ we need the visible declarations in order to "see" the
2437074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // friend declarations, is there a way to do this without writing the table ?
2438074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2439074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2440074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2441074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Force the DeclContext to build a its name-lookup table.
2442a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis  if (DC->hasExternalVisibleStorage())
2443a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis    DC->MaterializeVisibleDeclsFromExternalStorage();
2444a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis  else
2445a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis    DC->lookup(DeclarationName());
2446074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2447074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Serialize the contents of the mapping used for lookup. Note that,
2448074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // although we have two very different code paths, the serialized
2449074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // representation is the same for both cases: a declaration name,
2450074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // followed by a size, followed by references to the visible
2451074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // declarations that have that name.
2452074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  uint64_t Offset = Stream.GetCurrentBitNo();
2453074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2454074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (!Map || Map->empty())
2455074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2456074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2457074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2458074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  ASTDeclContextNameLookupTrait Trait(*this);
2459074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2460074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Create the on-disk hash table representation.
2461074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2462074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis       D != DEnd; ++D) {
2463074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    DeclarationName Name = D->first;
2464074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    DeclContext::lookup_result Result = D->second.getLookupResult();
2465074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    Generator.insert(Name, Result, Trait);
2466074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  }
2467074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2468074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Create the on-disk hash table in a buffer.
2469074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  llvm::SmallString<4096> LookupTable;
2470074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  uint32_t BucketOffset;
2471074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  {
2472074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    llvm::raw_svector_ostream Out(LookupTable);
2473074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    // Make sure that no bucket is at offset 0
2474074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    clang::io::Emit32(Out, 0);
2475074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    BucketOffset = Generator.Emit(Out, Trait);
2476074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  }
2477074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2478074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Write the lookup table
2479074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  RecordData Record;
2480074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Record.push_back(DECL_CONTEXT_VISIBLE);
2481074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Record.push_back(BucketOffset);
2482074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2483074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis                            LookupTable.str());
2484074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2485074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2486074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  ++NumVisibleDeclContexts;
2487074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return Offset;
2488074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis}
2489074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
24901d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// \brief Write an UPDATE_VISIBLE block for the given context.
24911d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl///
24921d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
24931d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// DeclContext in a dependent AST file. As such, they only exist for the TU
24941d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// (in C++) and for namespaces.
24951d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redlvoid ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
24961d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
24971d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  if (!Map || Map->empty())
24981d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    return;
24991d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
25001d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
25011d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  ASTDeclContextNameLookupTrait Trait(*this);
25021d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
25031d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Create the hash table.
25041d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
25051d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl       D != DEnd; ++D) {
25061d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    DeclarationName Name = D->first;
25071d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    DeclContext::lookup_result Result = D->second.getLookupResult();
25085967d6228f183a5fa384f2f1918df679ed2d8666Sebastian Redl    // For any name that appears in this table, the results are complete, i.e.
25095967d6228f183a5fa384f2f1918df679ed2d8666Sebastian Redl    // they overwrite results from previous PCHs. Merging is always a mess.
25105967d6228f183a5fa384f2f1918df679ed2d8666Sebastian Redl    Generator.insert(Name, Result, Trait);
25111d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  }
25121d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
25131d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Create the on-disk hash table in a buffer.
25141d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  llvm::SmallString<4096> LookupTable;
25151d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  uint32_t BucketOffset;
25161d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  {
25171d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    llvm::raw_svector_ostream Out(LookupTable);
25181d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    // Make sure that no bucket is at offset 0
25191d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    clang::io::Emit32(Out, 0);
25201d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    BucketOffset = Generator.Emit(Out, Trait);
25211d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  }
25221d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
25231d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Write the lookup table
25241d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  RecordData Record;
25251d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Record.push_back(UPDATE_VISIBLE);
25261d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Record.push_back(getDeclID(cast<Decl>(DC)));
25271d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Record.push_back(BucketOffset);
25281d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
25291d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl}
25301d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
25315d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
25324fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// General Serialization Routines
25334fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
25344fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
253568a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor/// \brief Write a record containing the given attributes.
253689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
25374eb9fc0449ddbd5239ddc3ae6b6e52880f47dcf7Argyrios Kyrtzidis  Record.push_back(Attrs.size());
2538cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2539cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr * A = *i;
2540cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2541cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    AddSourceLocation(A->getLocation(), Record);
254268a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
2543cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt#include "clang/Serialization/AttrPCHWrite.inc"
25445b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
254568a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  }
254668a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor}
254768a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
254889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
254968a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  Record.push_back(Str.size());
255068a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  Record.insert(Record.end(), Str.begin(), Str.end());
255168a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor}
255268a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
25533251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor/// \brief Note that the identifier II occurs at the given offset
25543251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor/// within the identifier table.
2555a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
25568538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  IdentID ID = IdentifierIDs[II];
25573397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Only store offsets new to this AST file. Other identifier names are looked
2558f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  // up earlier in the chain and thus don't need an offset.
2559f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  if (ID >= FirstIdentID)
2560f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    IdentifierOffsets[ID - FirstIdentID] = Offset;
25613251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor}
25623251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
256383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor/// \brief Note that the selector Sel occurs at the given offset
256483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor/// within the method pool/selector table.
2565a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
256683941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor  unsigned ID = SelectorIDs[Sel];
256783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor  assert(ID && "Unknown selector");
2568e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  // Don't record offsets for selectors that are also available in a different
2569e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  // file.
2570e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  if (ID < FirstSelectorID)
2571e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    return;
2572e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  SelectorOffsets[ID - FirstSelectorID] = Offset;
257383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor}
257483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor
2575a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
257689d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor  : Stream(Stream), Chain(0), SerializationListener(0),
257789d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor    FirstDeclID(1), NextDeclID(FirstDeclID),
25788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
2579e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
258077424bc3794f569816dba52e423b57486a98e5baDouglas Gregor    NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
258177424bc3794f569816dba52e423b57486a98e5baDouglas Gregor    CollectedStmts(&StmtsToEmit),
2582e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
25837c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
25847c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    NextCXXBaseSpecifiersID(1)
25857c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor{
258630c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl}
25872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2588a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
25898e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis                         const std::string &OutputFile,
259030c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl                         const char *isysroot) {
25912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Emit the file header.
2592c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'C', 8);
2593c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'P', 8);
2594c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'C', 8);
2595c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'H', 8);
25961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2597b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  WriteBlockInfoBlock();
25982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
25991dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  if (Chain)
2600a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl    WriteASTChain(SemaRef, StatCalls, isysroot);
26011dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  else
26028e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis    WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
26031dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl}
26041dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2605a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
26068e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis                             const char *isysroot,
26078e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis                             const std::string &OutputFile) {
26081dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  using namespace llvm;
26091dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
26101dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  ASTContext &Context = SemaRef.Context;
26111dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Preprocessor &PP = SemaRef.PP;
26121dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
26132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // The translation unit is the first declaration we'll emit.
26142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DeclIDs[Context.getTranslationUnitDecl()] = 1;
2615f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  ++NextDeclID;
261661d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  DeclTypesToEmit.push(Context.getTranslationUnitDecl());
26172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
26182deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  // Make sure that we emit IdentifierInfos (and any attached
26192deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  // declarations) for builtins.
26202deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  {
26212deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    IdentifierTable &Table = PP.getIdentifierTable();
26222deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    llvm::SmallVector<const char *, 32> BuiltinNames;
26232deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
26242deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor                                        Context.getLangOptions().NoBuiltin);
26252deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
26262deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor      getIdentifierRef(&Table.get(BuiltinNames[I]));
26272deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  }
26282deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor
262963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Build a record containing all of the tentative definitions in this file, in
2630e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // TentativeDefinitions order.  Generally, this record will be empty for
263163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // headers.
26324c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  RecordData TentativeDefinitions;
2633e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2634e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
263563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  }
26364c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor
263749b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Build a record containing all of the file scoped decls in this file.
263849b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  RecordData UnusedFileScopedDecls;
263949b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
264049b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis    AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
26414056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
264272b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  RecordData WeakUndeclaredIdentifiers;
264372b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
264472b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    WeakUndeclaredIdentifiers.push_back(
264572b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis                                      SemaRef.WeakUndeclaredIdentifiers.size());
264672b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
264772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis         I = SemaRef.WeakUndeclaredIdentifiers.begin(),
264872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis         E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
264972b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
265072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
265172b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
265272b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
265372b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    }
265472b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  }
265511a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
265614c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // Build a record containing all of the locally-scoped external
265714c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // declarations in this header file. Generally, this record will be
265814c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // empty.
265914c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  RecordData LocallyScopedExternalDecls;
26603397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // FIXME: This is filling in the AST file in densemap order which is
266163d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // nondeterminstic!
26621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
266314c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor         TD = SemaRef.LocallyScopedExternalDecls.begin(),
266414c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor         TDEnd = SemaRef.LocallyScopedExternalDecls.end();
266514c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor       TD != TDEnd; ++TD)
266614c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor    AddDeclRef(TD->second, LocallyScopedExternalDecls);
266714c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor
2668b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  // Build a record containing all of the ext_vector declarations.
2669b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  RecordData ExtVectorDecls;
2670b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2671b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor    AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2672b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor
2673d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Build a record containing all of the VTable uses information.
2674d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  RecordData VTableUses;
2675be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis  if (!SemaRef.VTableUses.empty()) {
2676be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis    VTableUses.push_back(SemaRef.VTableUses.size());
2677be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2678be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2679be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2680be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2681be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis    }
2682d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  }
2683d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
2684d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Build a record containing all of dynamic classes declarations.
2685d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  RecordData DynamicClasses;
2686d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2687d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis    AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2688d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
26890e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // Build a record containing all of pending implicit instantiations.
269062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  RecordData PendingInstantiations;
26910e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  for (std::deque<Sema::PendingImplicitInstantiation>::iterator
269262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         I = SemaRef.PendingInstantiations.begin(),
269362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
269462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    AddDeclRef(I->first, PendingInstantiations);
269562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    AddSourceLocation(I->second, PendingInstantiations);
26960e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  }
26970e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
26980e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis         "There are local ones at end of translation unit!");
26990e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis
270076c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  // Build a record containing some declaration references.
270176c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  RecordData SemaDeclRefs;
270276c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
270376c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
270476c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
270576c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  }
270676c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis
270714b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne  RecordData CUDASpecialDeclRefs;
270814b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne  if (Context.getcudaConfigureCallDecl()) {
270914b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne    AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
271014b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne  }
271114b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne
27123397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Write the remaining AST contents.
2713ad1de006ea080b540e480efc6b86c2e201dbf1ecDouglas Gregor  RecordData Record;
27148538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(AST_BLOCK_ID, 5);
27158e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis  WriteMetadata(Context, isysroot, OutputFile);
27161dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  WriteLanguageOptions(Context.getLangOptions());
2717e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (StatCalls && !isysroot)
2718dd41ed59cf7aefabd40bf766d8fcc7ebd759c8e5Douglas Gregor    WriteStatCache(*StatCalls);
2719e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
2720c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  // Write the record of special types.
2721c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  Record.clear();
27221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2723c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getBuiltinVaListType(), Record);
2724c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCIdType(), Record);
2725c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCSelType(), Record);
2726c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCProtoType(), Record);
2727c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCClassType(), Record);
2728c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getRawCFConstantStringType(), Record);
2729c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2730c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getFILEType(), Record);
2731782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  AddTypeRef(Context.getjmp_bufType(), Record);
2732782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  AddTypeRef(Context.getsigjmp_bufType(), Record);
2733d1571acc700b652a52c766e36a6c688d9bf6f3a1Douglas Gregor  AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2734d1571acc700b652a52c766e36a6c688d9bf6f3a1Douglas Gregor  AddTypeRef(Context.ObjCClassRedefinitionType, Record);
2735adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  AddTypeRef(Context.getRawBlockdescriptorType(), Record);
2736083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
27372bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  AddTypeRef(Context.ObjCSelRedefinitionType, Record);
27382bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  AddTypeRef(Context.getRawNSConstantStringType(), Record);
2739006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis  Record.push_back(Context.isInt128Installed());
27408538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(SPECIAL_TYPES, Record);
27411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2742366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor  // Keep writing types and declarations until all types and
2743366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor  // declarations have been written.
27448538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
274561d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  WriteDeclsBlockAbbrevs();
274661d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  while (!DeclTypesToEmit.empty()) {
274761d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclOrType DOT = DeclTypesToEmit.front();
274861d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclTypesToEmit.pop();
274961d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    if (DOT.isType())
275061d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor      WriteType(DOT.getType());
275161d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    else
275261d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor      WriteDecl(Context, DOT.getDecl());
275361d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  }
275461d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  Stream.ExitBlock();
275511a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
2756813a97b3eee957eac3ac1fb111b8892fb9afd0d4Douglas Gregor  WritePreprocessor(PP);
2757cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
2758059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl  WriteSelectors(SemaRef);
2759320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  WriteReferencedSelectorsPool(SemaRef);
276037e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor  WriteIdentifierTable(PP);
27618f5dc7fe4d42cea78fa92d1638f753cf65b54cb5Douglas Gregor
27621476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  WriteTypeDeclOffsets();
27633efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  WritePragmaDiagnosticMappings(Context.getDiagnostics());
2764ad1de006ea080b540e480efc6b86c2e201dbf1ecDouglas Gregor
27657c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  // Write the C++ base-specifier set offsets.
27667c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  if (!CXXBaseSpecifiersOffsets.empty()) {
27677c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    // Create a blob abbreviation for the C++ base specifiers offsets.
27687c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    using namespace llvm;
27697c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
27707c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
27717c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
27727c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
27737c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
27747c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
27757c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
27767c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    // Write the selector offsets table.
27777c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Record.clear();
27787c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
27797c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Record.push_back(CXXBaseSpecifiersOffsets.size());
27807c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
27817c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                              (const char *)CXXBaseSpecifiersOffsets.data(),
27827c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                            CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
27837c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  }
27847c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
27854c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  // Write the record containing external, unnamed definitions.
2786fdd0172ca1b3c837f8c2b37d69cc2085234e09faDouglas Gregor  if (!ExternalDefinitions.empty())
27878538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
27884c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor
27894c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  // Write the record containing tentative definitions.
27904c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  if (!TentativeDefinitions.empty())
27918538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
279214c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor
279349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Write the record containing unused file scoped decls.
279449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  if (!UnusedFileScopedDecls.empty())
27958538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
279611a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
279772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  // Write the record containing weak undeclared identifiers.
279872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  if (!WeakUndeclaredIdentifiers.empty())
27998538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
280072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis                      WeakUndeclaredIdentifiers);
280172b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
280214c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // Write the record containing locally-scoped external definitions.
280314c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  if (!LocallyScopedExternalDecls.empty())
28048538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
280514c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor                      LocallyScopedExternalDecls);
2806b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor
2807b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  // Write the record containing ext_vector type names.
2808b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  if (!ExtVectorDecls.empty())
28098538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
28101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2811d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Write the record containing VTable uses information.
2812d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  if (!VTableUses.empty())
28138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(VTABLE_USES, VTableUses);
2814d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
2815d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Write the record containing dynamic classes declarations.
2816d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  if (!DynamicClasses.empty())
28178538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
2818d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
28190e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // Write the record containing pending implicit instantiations.
282062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  if (!PendingInstantiations.empty())
282162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
28220e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis
282376c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  // Write the record containing declaration references of Sema.
282476c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  if (!SemaDeclRefs.empty())
28258538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
282676c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis
282714b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne  // Write the record containing CUDA-specific declaration references.
282814b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne  if (!CUDASpecialDeclRefs.empty())
282914b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne    Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
283014b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne
28313e1af84bb0092a1aafb49deaa4ab6664c9a9071bDouglas Gregor  // Some simple statistics
2832ad1de006ea080b540e480efc6b86c2e201dbf1ecDouglas Gregor  Record.clear();
28333e1af84bb0092a1aafb49deaa4ab6664c9a9071bDouglas Gregor  Record.push_back(NumStatements);
283437e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor  Record.push_back(NumMacros);
28352512308525ff328aa992da0b5ee14a488d2ea93aDouglas Gregor  Record.push_back(NumLexicalDeclContexts);
28362512308525ff328aa992da0b5ee14a488d2ea93aDouglas Gregor  Record.push_back(NumVisibleDeclContexts);
28378538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(STATISTICS, Record);
2838c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.ExitBlock();
28392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
28402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2841a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
284230c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl                              const char *isysroot) {
28431dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  using namespace llvm;
28441dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
28451dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  ASTContext &Context = SemaRef.Context;
28461dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Preprocessor &PP = SemaRef.PP;
28471476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
28481dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  RecordData Record;
28498538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(AST_BLOCK_ID, 5);
28508e3df4d0864f0a966c20088ca1a29c3398b7639dArgyrios Kyrtzidis  WriteMetadata(Context, isysroot, "");
28511476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  if (StatCalls && !isysroot)
28521476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl    WriteStatCache(*StatCalls);
28531476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // FIXME: Source manager block should only write new stuff, which could be
28541476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // done by tracking the largest ID in the chain
28551476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
28561dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
28571dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  // The special types are in the chained PCH.
28581dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
28591dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  // We don't start with the translation unit, but with its decls that
28603c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  // don't come from the chained PCH.
28611dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
2862eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis  llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
2863681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2864681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl                                  E = TU->noload_decls_end();
28651dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl       I != E; ++I) {
2866d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl    if ((*I)->getPCHLevel() == 0)
2867eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis      NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
28680b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    else if ((*I)->isChangedSinceDeserialization())
28690b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl      (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
28701dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  }
2871681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  // We also need to write a lexical updates block for the TU.
2872d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
28738538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
2874d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2875d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2876d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  Record.clear();
28778538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(TU_UPDATE_LEXICAL);
2878d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2879d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl                          reinterpret_cast<const char*>(NewGlobalDecls.data()),
2880eb5e9986e577b1e2bff3cca5973a2494fb593fbbArgyrios Kyrtzidis                          NewGlobalDecls.size() * sizeof(KindDeclIDPair));
2881100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  // And a visible updates block for the DeclContexts.
2882100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  Abv = new llvm::BitCodeAbbrev();
2883100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2884100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2885100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2886100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2887100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2888100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  WriteDeclContextVisibleUpdate(TU);
28891dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2890083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Build a record containing all of the new tentative definitions in this
2891083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // file, in TentativeDefinitions order.
2892083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  RecordData TentativeDefinitions;
2893083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2894083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl    if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2895083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl      AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2896083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2897083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
289849b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Build a record containing all of the file scoped decls in this file.
289949b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  RecordData UnusedFileScopedDecls;
290049b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
290149b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis    if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
290249b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis      AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
2903083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2904083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
29054056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // We write the entire table, overwriting the tables from the chain.
29064056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData WeakUndeclaredIdentifiers;
29074056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
29084056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    WeakUndeclaredIdentifiers.push_back(
29094056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl                                      SemaRef.WeakUndeclaredIdentifiers.size());
29104056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
29114056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl         I = SemaRef.WeakUndeclaredIdentifiers.begin(),
29124056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl         E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
29134056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
29144056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
29154056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
29164056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
29174056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    }
29184056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
29194056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
2920083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Build a record containing all of the locally-scoped external
2921083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // declarations in this header file. Generally, this record will be
2922083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // empty.
2923083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  RecordData LocallyScopedExternalDecls;
29243397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // FIXME: This is filling in the AST file in densemap order which is
2925083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // nondeterminstic!
2926083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2927083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl         TD = SemaRef.LocallyScopedExternalDecls.begin(),
2928083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl         TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2929083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl       TD != TDEnd; ++TD) {
2930083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl    if (TD->second->getPCHLevel() == 0)
2931083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl      AddDeclRef(TD->second, LocallyScopedExternalDecls);
2932083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2933083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
2934083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Build a record containing all of the ext_vector declarations.
2935083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  RecordData ExtVectorDecls;
2936083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2937083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl    if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2938083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl      AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2939083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2940083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
29414056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing all of the VTable uses information.
29424056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // We write everything here, because it's too hard to determine whether
29434056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // a use is new to this part.
29444056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData VTableUses;
29454056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!SemaRef.VTableUses.empty()) {
29464056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    VTableUses.push_back(SemaRef.VTableUses.size());
29474056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
29484056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
29494056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
29504056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
29514056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    }
29524056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
29534056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
29544056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing all of dynamic classes declarations.
29554056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData DynamicClasses;
29564056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
29574056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
29584056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
29594056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
29604056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing all of pending implicit instantiations.
296162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  RecordData PendingInstantiations;
29624056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  for (std::deque<Sema::PendingImplicitInstantiation>::iterator
296362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         I = SemaRef.PendingInstantiations.begin(),
296462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
29654056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    if (I->first->getPCHLevel() == 0) {
296662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      AddDeclRef(I->first, PendingInstantiations);
296762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      AddSourceLocation(I->second, PendingInstantiations);
29684056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    }
29694056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
29704056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
29714056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl         "There are local ones at end of translation unit!");
29724056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
29734056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing some declaration references.
29744056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // It's not worth the effort to avoid duplication here.
29754056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData SemaDeclRefs;
29764056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
29774056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
29784056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
29794056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
29804056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
29818538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
29821dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  WriteDeclsBlockAbbrevs();
2983d3d0755a4220d3fa6c125eb3f8ce29fc1424eac3Argyrios Kyrtzidis  for (DeclsToRewriteTy::iterator
2984d3d0755a4220d3fa6c125eb3f8ce29fc1424eac3Argyrios Kyrtzidis         I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2985d3d0755a4220d3fa6c125eb3f8ce29fc1424eac3Argyrios Kyrtzidis    DeclTypesToEmit.push(const_cast<Decl*>(*I));
29861dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  while (!DeclTypesToEmit.empty()) {
29871dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    DeclOrType DOT = DeclTypesToEmit.front();
29881dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    DeclTypesToEmit.pop();
29891dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    if (DOT.isType())
29901dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl      WriteType(DOT.getType());
29911dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    else
29921dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl      WriteDecl(Context, DOT.getDecl());
29931dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  }
29941dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Stream.ExitBlock();
29951dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2996083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  WritePreprocessor(PP);
2997a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  WriteSelectors(SemaRef);
2998a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  WriteReferencedSelectorsPool(SemaRef);
2999f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  WriteIdentifierTable(PP);
30001476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  WriteTypeDeclOffsets();
3001f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  // FIXME: For chained PCH only write the new mappings (we currently
3002f41d3be39980d40849850d3fb90403623cc8459eArgyrios Kyrtzidis  // write all of them again).
30033efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  WritePragmaDiagnosticMappings(Context.getDiagnostics());
3004083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
3005a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  /// Build a record containing first declarations from a chained PCH and the
30063397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  /// most recent declarations in this AST that they point to.
3007a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  RecordData FirstLatestDeclIDs;
3008a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  for (FirstLatestDeclMap::iterator
3009a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis        I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3010a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis    assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3011a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis           "Expected first & second to be in different PCHs");
3012a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis    AddDeclRef(I->first, FirstLatestDeclIDs);
3013a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis    AddDeclRef(I->second, FirstLatestDeclIDs);
3014a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  }
3015a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  if (!FirstLatestDeclIDs.empty())
30168538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3017a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis
3018083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing external, unnamed definitions.
3019083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!ExternalDefinitions.empty())
30208538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
3021083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
3022083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing tentative definitions.
3023083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!TentativeDefinitions.empty())
30248538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
3025083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
302649b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Write the record containing unused file scoped decls.
302749b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  if (!UnusedFileScopedDecls.empty())
30288538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
3029083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
30304056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing weak undeclared identifiers.
30314056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!WeakUndeclaredIdentifiers.empty())
30328538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
30334056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl                      WeakUndeclaredIdentifiers);
30344056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
3035083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing locally-scoped external definitions.
3036083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!LocallyScopedExternalDecls.empty())
30378538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
3038083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl                      LocallyScopedExternalDecls);
3039083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
3040083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing ext_vector type names.
3041083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!ExtVectorDecls.empty())
30428538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
3043083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
30444056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing VTable uses information.
30454056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!VTableUses.empty())
30468538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(VTABLE_USES, VTableUses);
30474056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
30484056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing dynamic classes declarations.
30494056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!DynamicClasses.empty())
30508538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
30514056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
30524056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing pending implicit instantiations.
305362c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  if (!PendingInstantiations.empty())
305462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
30554056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
30564056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing declaration references of Sema.
30574056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!SemaDeclRefs.empty())
30588538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
3059083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
3060100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  // Write the updates to DeclContexts.
3061100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3062100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis           I = UpdatedDeclContexts.begin(),
3063100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis           E = UpdatedDeclContexts.end();
30641d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl         I != E; ++I)
30651d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    WriteDeclContextVisibleUpdate(*I);
30661d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
3067aacdd02e5865aa410c1418d7ef77f445b5bb5cbaArgyrios Kyrtzidis  WriteDeclUpdatesBlocks();
30687b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
3069083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.clear();
3070083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumStatements);
3071083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumMacros);
3072083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumLexicalDeclContexts);
3073083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumVisibleDeclContexts);
3074aacdd02e5865aa410c1418d7ef77f445b5bb5cbaArgyrios Kyrtzidis  WriteDeclReplacementsBlock();
30758538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(STATISTICS, Record);
30761dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Stream.ExitBlock();
30771dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl}
30781dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
3079aacdd02e5865aa410c1418d7ef77f445b5bb5cbaArgyrios Kyrtzidisvoid ASTWriter::WriteDeclUpdatesBlocks() {
30807b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  if (DeclUpdates.empty())
30817b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    return;
30827b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
30837b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  RecordData OffsetsRecord;
30847b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
30857b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  for (DeclUpdateMap::iterator
30867b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis         I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
30877b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    const Decl *D = I->first;
30887b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    UpdateRecord &URec = I->second;
30897b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
3090ba901b507fc36408fe6f8478e8ac90b554f1d230Argyrios Kyrtzidis    if (DeclsToRewrite.count(D))
3091ba901b507fc36408fe6f8478e8ac90b554f1d230Argyrios Kyrtzidis      continue; // The decl will be written completely,no need to store updates.
3092ba901b507fc36408fe6f8478e8ac90b554f1d230Argyrios Kyrtzidis
30937b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    uint64_t Offset = Stream.GetCurrentBitNo();
30947b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    Stream.EmitRecord(DECL_UPDATES, URec);
30957b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
30967b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    OffsetsRecord.push_back(GetDeclRef(D));
30977b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis    OffsetsRecord.push_back(Offset);
30987b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  }
30997b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  Stream.ExitBlock();
31007b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
31017b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis}
31027b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
3103aacdd02e5865aa410c1418d7ef77f445b5bb5cbaArgyrios Kyrtzidisvoid ASTWriter::WriteDeclReplacementsBlock() {
31040b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  if (ReplacedDecls.empty())
31050b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    return;
31060b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl
31070b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  RecordData Record;
31088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
31090b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl           I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
31100b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    Record.push_back(I->first);
31110b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    Record.push_back(I->second);
31120b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  }
31138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(DECL_REPLACEMENTS, Record);
31140b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl}
31150b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl
311689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
31172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(Loc.getRawEncoding());
31182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
31192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
312089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
31216ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  AddSourceLocation(Range.getBegin(), Record);
31226ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  AddSourceLocation(Range.getEnd(), Record);
31236ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner}
31246ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
312589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
31262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(Value.getBitWidth());
31274ea884b429445aa6f1af5bc6e51d0b65a4043e24Benjamin Kramer  const uint64_t *Words = Value.getRawData();
31284ea884b429445aa6f1af5bc6e51d0b65a4043e24Benjamin Kramer  Record.append(Words, Words + Value.getNumWords());
31292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
31302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
313189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
31320a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor  Record.push_back(Value.isUnsigned());
31330a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor  AddAPInt(Value, Record);
31340a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor}
31350a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor
313689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
313717fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor  AddAPInt(Value.bitcastToAPInt(), Record);
313817fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor}
313917fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor
314089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
31412deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  Record.push_back(getIdentifierRef(II));
31422deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor}
31432deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor
31448538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlIdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
31452deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  if (II == 0)
31462deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    return 0;
3147afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
31488538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  IdentID &ID = IdentifierIDs[II];
3149afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  if (ID == 0)
3150f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    ID = NextIdentID++;
31512deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  return ID;
31522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
31532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3154f73c93fea0d4b447585bc7459499ba6b822e045cSebastian RedlMacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
31556a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (MD == 0)
31566a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    return 0;
3157f73c93fea0d4b447585bc7459499ba6b822e045cSebastian Redl
3158f73c93fea0d4b447585bc7459499ba6b822e045cSebastian Redl  MacroID &ID = MacroDefinitions[MD];
31596a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (ID == 0)
316077424bc3794f569816dba52e423b57486a98e5baDouglas Gregor    ID = NextMacroID++;
31616a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  return ID;
31626a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor}
31636a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
316489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
31655d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  Record.push_back(getSelectorRef(SelRef));
31665d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl}
31675d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl
31688538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlSelectorID ASTWriter::getSelectorRef(Selector Sel) {
31695d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  if (Sel.getAsOpaquePtr() == 0) {
31705d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    return 0;
317190cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  }
317290cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff
31738538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  SelectorID &SID = SelectorIDs[Sel];
3174e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  if (SID == 0 && Chain) {
3175e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    // This might trigger a ReadSelector callback, which will set the ID for
3176e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    // this selector.
3177e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    Chain->LoadSelector(Sel);
3178e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  }
317990cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  if (SID == 0) {
3180e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    SID = NextSelectorID++;
318190cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  }
31825d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  return SID;
318390cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff}
318490cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff
318589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
3186d2598368876cfe40bc8465540033bc5b5e58d8afChris Lattner  AddDeclRef(Temp->getDestructor(), Record);
3187d2598368876cfe40bc8465540033bc5b5e58d8afChris Lattner}
3188d2598368876cfe40bc8465540033bc5b5e58d8afChris Lattner
31897c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregorvoid ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
31907c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                                      CXXBaseSpecifier const *BasesEnd,
31917c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                                        RecordDataImpl &Record) {
31927c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
31937c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  CXXBaseSpecifiersToWrite.push_back(
31947c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                                QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
31957c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                                                        Bases, BasesEnd));
31967c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  Record.push_back(NextCXXBaseSpecifiersID++);
31977c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor}
31987c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
3199a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
320044f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                           const TemplateArgumentLocInfo &Arg,
320189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                           RecordDataImpl &Record) {
320244f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis  switch (Kind) {
3203833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Expression:
320444f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    AddStmt(Arg.getAsExpr());
3205833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    break;
3206833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Type:
320744f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
3208833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    break;
3209788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
321017cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    AddSourceRange(Arg.getTemplateQualifierRange(), Record);
321117cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
3212a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    break;
3213a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
3214a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3215a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
3216ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor    AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
3217788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    break;
3218833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Null:
3219833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Integral:
3220833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Declaration:
3221833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Pack:
3222833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    break;
3223833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
3224833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
3225833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3226a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
322789eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                       RecordDataImpl &Record) {
322844f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis  AddTemplateArgument(Arg.getArgument(), Record);
322917cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis
323017cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis  if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
323117cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    bool InfoHasSameExpr
323217cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis      = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
323317cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    Record.push_back(InfoHasSameExpr);
323417cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    if (InfoHasSameExpr)
323517cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis      return; // Avoid storing the same expr twice.
323617cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis  }
323744f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
323844f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                             Record);
323944f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis}
324044f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis
324189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
3242a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo == 0) {
3243a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    AddTypeRef(QualType(), Record);
3244a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    return;
3245a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  }
3246a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
3247a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  AddTypeRef(TInfo->getType(), Record);
3248a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  TypeLocWriter TLW(*this, Record);
3249a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
325011a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam    TLW.Visit(TL);
3251a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
3252a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
325389eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
32547fb35182f43392cea4517c203bbabb22364a19fcArgyrios Kyrtzidis  Record.push_back(GetOrCreateTypeID(T));
32557fb35182f43392cea4517c203bbabb22364a19fcArgyrios Kyrtzidis}
32567fb35182f43392cea4517c203bbabb22364a19fcArgyrios Kyrtzidis
32577fb35182f43392cea4517c203bbabb22364a19fcArgyrios KyrtzidisTypeID ASTWriter::GetOrCreateTypeID(QualType T) {
3258eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis  return MakeTypeID(T,
3259eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis              std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3260eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis}
32612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
32625d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios KyrtzidisTypeID ASTWriter::getTypeID(QualType T) const {
3263eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis  return MakeTypeID(T,
3264eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis              std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
326526fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis}
326626fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis
326726fca90786af17f97e1a5ecc310d4bf39b831595Argyrios KyrtzidisTypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
326826fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  if (T.isNull())
326926fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis    return TypeIdx();
327026fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  assert(!T.getLocalFastQualifiers());
327126fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis
327201b81c4d074bba9c18372d521405dfe32fc4f552Argyrios Kyrtzidis  TypeIdx &Idx = TypeIdxs[T];
3273c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis  if (Idx.getIndex() == 0) {
3274366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor    // We haven't seen this type before. Assign it a new ID and put it
32750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // into the queue of types to emit.
3276c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis    Idx = TypeIdx(NextTypeID++);
327761d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclTypesToEmit.push(T);
3278366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor  }
327926fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  return Idx;
328026fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis}
32812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
32825d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios KyrtzidisTypeIdx ASTWriter::getTypeIdx(QualType T) const {
328326fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  if (T.isNull())
328426fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis    return TypeIdx();
328526fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  assert(!T.getLocalFastQualifiers());
328626fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis
32875d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  TypeIdxMap::const_iterator I = TypeIdxs.find(T);
32885d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  assert(I != TypeIdxs.end() && "Type not emitted!");
32895d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  return I->second;
32902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
32912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
32927b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidisvoid ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
3293681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  Record.push_back(GetDeclRef(D));
3294681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl}
3295681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
32968538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlDeclID ASTWriter::GetDeclRef(const Decl *D) {
32972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (D == 0) {
3298681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    return 0;
32992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
330097475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
33018538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  DeclID &ID = DeclIDs[D];
33021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ID == 0) {
33032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // We haven't seen this declaration before. Give it a new ID and
33042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // enqueue it in the list of declarations to emit.
3305f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    ID = NextDeclID++;
330661d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclTypesToEmit.push(const_cast<Decl *>(D));
33070b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
33080b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    // We don't add it to the replacement collection here, because we don't
33090b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    // have the offset yet.
33100b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    DeclTypesToEmit.push(const_cast<Decl *>(D));
33110b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    // Reset the flag, so that we don't add this decl multiple times.
33120b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
33132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
33142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3315681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  return ID;
33162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
33172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
33188538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlDeclID ASTWriter::getDeclID(const Decl *D) {
33193251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  if (D == 0)
33203251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    return 0;
33213251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
33223251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
33233251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  return DeclIDs[D];
33243251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor}
33253251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
332689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
3327ea5ce4705df0743093925585d8edc80e0d8fe3ffChris Lattner  // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
33282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(Name.getNameKind());
33292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  switch (Name.getNameKind()) {
33302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::Identifier:
33312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
33322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
33332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
33342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::ObjCZeroArgSelector:
33352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::ObjCOneArgSelector:
33362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::ObjCMultiArgSelector:
333790cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff    AddSelectorRef(Name.getObjCSelector(), Record);
33382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
33392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
33402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXConstructorName:
33412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXDestructorName:
33422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXConversionFunctionName:
33432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    AddTypeRef(Name.getCXXNameType(), Record);
33442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
33452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
33462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXOperatorName:
33472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Record.push_back(Name.getCXXOverloadedOperator());
33482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
33492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
33503e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt  case DeclarationName::CXXLiteralOperatorName:
33513e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
33523e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    break;
33533e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt
33542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXUsingDirective:
33552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // No extra data to emit
33562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
33572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
33582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
33596ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
33604045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidisvoid ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
336189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                     DeclarationName Name, RecordDataImpl &Record) {
33624045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  switch (Name.getNameKind()) {
33634045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::CXXConstructorName:
33644045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::CXXDestructorName:
33654045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::CXXConversionFunctionName:
33664045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
33674045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    break;
33684045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis
33694045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::CXXOperatorName:
33704045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    AddSourceLocation(
33714045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis       SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
33724045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis       Record);
33734045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    AddSourceLocation(
33744045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
33754045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis        Record);
33764045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    break;
33774045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis
33784045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::CXXLiteralOperatorName:
33794045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    AddSourceLocation(
33804045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis     SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
33814045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis     Record);
33824045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    break;
33834045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis
33844045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::Identifier:
33854045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::ObjCZeroArgSelector:
33864045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::ObjCOneArgSelector:
33874045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::ObjCMultiArgSelector:
33884045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  case DeclarationName::CXXUsingDirective:
33894045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    break;
33904045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  }
33914045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis}
33924045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis
33934045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidisvoid ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
339489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                       RecordDataImpl &Record) {
33954045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  AddDeclarationName(NameInfo.getName(), Record);
33964045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  AddSourceLocation(NameInfo.getLoc(), Record);
33974045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
33984045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis}
33994045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis
34004045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidisvoid ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
340189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                 RecordDataImpl &Record) {
34024045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  AddNestedNameSpecifier(Info.NNS, Record);
34034045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  AddSourceRange(Info.NNSRange, Record);
34044045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  Record.push_back(Info.NumTemplParamLists);
34054045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis  for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
34064045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis    AddTemplateParameterList(Info.TemplParamLists[i], Record);
34074045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis}
34084045107b7384fd68eed5e3e2f06fc2a47e7be0a6Argyrios Kyrtzidis
3409a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
341089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                       RecordDataImpl &Record) {
34116ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  // Nested name specifiers usually aren't too long. I think that 8 would
34126ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  // typically accomodate the vast majority.
34136ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
34146ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
34156ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  // Push each of the NNS's onto a stack for serialization in reverse order.
34166ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  while (NNS) {
34176ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NestedNames.push_back(NNS);
34186ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NNS = NNS->getPrefix();
34196ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  }
34206ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
34216ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  Record.push_back(NestedNames.size());
34226ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  while(!NestedNames.empty()) {
34236ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NNS = NestedNames.pop_back_val();
34246ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
34256ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    Record.push_back(Kind);
34266ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    switch (Kind) {
34276ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::Identifier:
34286ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      AddIdentifierRef(NNS->getAsIdentifier(), Record);
34296ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
34306ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
34316ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::Namespace:
34326ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      AddDeclRef(NNS->getAsNamespace(), Record);
34336ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
34346ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
34356ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::TypeSpec:
34366ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::TypeSpecWithTemplate:
34376ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      AddTypeRef(QualType(NNS->getAsType(), 0), Record);
34386ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
34396ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
34406ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
34416ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::Global:
34426ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      // Don't need to write an associated value.
34436ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
34446ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    }
34456ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  }
34466ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner}
344790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
344889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
344920249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer  TemplateName::NameKind Kind = Name.getKind();
345090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(Kind);
345190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  switch (Kind) {
345290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::Template:
345390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddDeclRef(Name.getAsTemplateDecl(), Record);
345490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
345590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
345690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::OverloadedTemplate: {
345790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
345890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(OvT->size());
345990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
346090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis           I != E; ++I)
346190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      AddDeclRef(*I, Record);
346290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
346390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
346420249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
346590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::QualifiedTemplate: {
346690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
346790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddNestedNameSpecifier(QualT->getQualifier(), Record);
346890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(QualT->hasTemplateKeyword());
346990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddDeclRef(QualT->getTemplateDecl(), Record);
347090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
347190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
347220249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer
347390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::DependentTemplate: {
347490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    DependentTemplateName *DepT = Name.getAsDependentTemplateName();
347590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddNestedNameSpecifier(DepT->getQualifier(), Record);
347690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(DepT->isIdentifier());
347790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    if (DepT->isIdentifier())
347890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      AddIdentifierRef(DepT->getIdentifier(), Record);
347990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    else
348090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      Record.push_back(DepT->getOperator());
348190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
348290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
34831aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
34841aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  case TemplateName::SubstTemplateTemplateParmPack: {
34851aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPackStorage *SubstPack
34861aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = Name.getAsSubstTemplateTemplateParmPack();
34871aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    AddDeclRef(SubstPack->getParameterPack(), Record);
34881aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    AddTemplateArgument(SubstPack->getArgumentPack(), Record);
34891aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    break;
34901aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
349190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
349290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
349390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
349420249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencervoid ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
349589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                    RecordDataImpl &Record) {
349690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(Arg.getKind());
349790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  switch (Arg.getKind()) {
349890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Null:
349990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
350090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Type:
350190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddTypeRef(Arg.getAsType(), Record);
350290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
350390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Declaration:
350490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddDeclRef(Arg.getAsDecl(), Record);
350590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
350690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Integral:
350790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddAPSInt(*Arg.getAsIntegral(), Record);
350890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddTypeRef(Arg.getIntegralType(), Record);
350990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
351090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Template:
35112be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
35122be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    break;
3513a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
3514a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
35152be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
35162be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor      Record.push_back(*NumExpansions + 1);
35172be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor    else
35182be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor      Record.push_back(0);
351990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
352090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Expression:
352190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddStmt(Arg.getAsExpr());
352290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
352390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Pack:
352490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(Arg.pack_size());
352590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
352690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis           I != E; ++I)
352790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      AddTemplateArgument(*I, Record);
352890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
352990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
353090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
3531dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis
3532dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidisvoid
3533a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
353489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                    RecordDataImpl &Record) {
3535dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  assert(TemplateParams && "No TemplateParams!");
3536dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3537dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3538dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3539dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  Record.push_back(TemplateParams->size());
3540dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  for (TemplateParameterList::const_iterator
3541dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis         P = TemplateParams->begin(), PEnd = TemplateParams->end();
3542dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis         P != PEnd; ++P)
3543dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis    AddDeclRef(*P, Record);
3544dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis}
3545dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis
3546dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis/// \brief Emit a template argument list.
3547dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidisvoid
3548a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
354989eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                   RecordDataImpl &Record) {
3550dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  assert(TemplateArgs && "No TemplateArgs!");
3551910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  Record.push_back(TemplateArgs->size());
3552910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  for (int i=0, e = TemplateArgs->size(); i != e; ++i)
3553dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis    AddTemplateArgument(TemplateArgs->get(i), Record);
3554dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis}
355537ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis
355637ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis
355737ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidisvoid
355889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios KyrtzidisASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
355937ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  Record.push_back(Set.size());
356037ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  for (UnresolvedSetImpl::const_iterator
356137ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis         I = Set.begin(), E = Set.end(); I != E; ++I) {
356237ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis    AddDeclRef(I.getDecl(), Record);
356337ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis    Record.push_back(I.getAccess());
356437ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  }
356537ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis}
35660745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis
3567a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
356889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                    RecordDataImpl &Record) {
35690745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  Record.push_back(Base.isVirtual());
35700745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  Record.push_back(Base.isBaseOfClass());
35710745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  Record.push_back(Base.getAccessSpecifierAsWritten());
3572f677ea3cc9598d9952ad7ffab5fb322ba4c5be31Sebastian Redl  Record.push_back(Base.getInheritConstructors());
35735606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
35740745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  AddSourceRange(Base.getSourceRange(), Record);
3575f90b27ad077c3339b62befc892382845339f9490Douglas Gregor  AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3576f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                                          : SourceLocation(),
3577f90b27ad077c3339b62befc892382845339f9490Douglas Gregor                    Record);
35780745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis}
357930c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl
35807c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregorvoid ASTWriter::FlushCXXBaseSpecifiers() {
35817c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  RecordData Record;
35827c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
35837c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Record.clear();
35847c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
35857c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    // Record the offset of this base-specifier set.
35867c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
35877c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    if (Index == CXXBaseSpecifiersOffsets.size())
35887c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor      CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
35897c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    else {
35907c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor      if (Index > CXXBaseSpecifiersOffsets.size())
35917c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor        CXXBaseSpecifiersOffsets.resize(Index + 1);
35927c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor      CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
35937c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    }
35947c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
35957c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
35967c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                        *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
35977c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Record.push_back(BEnd - B);
35987c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    for (; B != BEnd; ++B)
35997c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor      AddCXXBaseSpecifier(*B, Record);
36007c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
3601acec34bda4a765fd05ed10e5aa83bc93dac072c1Douglas Gregor
3602acec34bda4a765fd05ed10e5aa83bc93dac072c1Douglas Gregor    // Flush any expressions that were written as part of the base specifiers.
3603acec34bda4a765fd05ed10e5aa83bc93dac072c1Douglas Gregor    FlushStmts();
36047c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  }
36057c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
36067c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  CXXBaseSpecifiersToWrite.clear();
36077c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor}
36087c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
3609cbb67480094b3bcb5b715acd827cbad55e2a204cSean Huntvoid ASTWriter::AddCXXCtorInitializers(
3610cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt                             const CXXCtorInitializer * const *CtorInitializers,
3611cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt                             unsigned NumCtorInitializers,
3612cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt                             RecordDataImpl &Record) {
3613cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  Record.push_back(NumCtorInitializers);
3614cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt  for (unsigned i=0; i != NumCtorInitializers; ++i) {
3615cbb67480094b3bcb5b715acd827cbad55e2a204cSean Hunt    const CXXCtorInitializer *Init = CtorInitializers[i];
36168e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis
36178e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    Record.push_back(Init->isBaseInitializer());
36188e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    if (Init->isBaseInitializer()) {
36198e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
36208e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      Record.push_back(Init->isBaseVirtual());
36218e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    } else {
362200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      Record.push_back(Init->isIndirectMemberInitializer());
362300eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      if (Init->isIndirectMemberInitializer())
362400eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet        AddDeclRef(Init->getIndirectMember(), Record);
362500eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet      else
362600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet        AddDeclRef(Init->getMember(), Record);
36278e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    }
362800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet
36298e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddSourceLocation(Init->getMemberLocation(), Record);
36308e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddStmt(Init->getInit());
36318e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddSourceLocation(Init->getLParenLoc(), Record);
36328e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddSourceLocation(Init->getRParenLoc(), Record);
36338e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    Record.push_back(Init->isWritten());
36348e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    if (Init->isWritten()) {
36358e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      Record.push_back(Init->getSourceOrder());
36368e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    } else {
36378e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      Record.push_back(Init->getNumArrayIndices());
36388e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
36398e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis        AddDeclRef(Init->getArrayIndex(i), Record);
36408e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    }
36418e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis  }
36428e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis}
36438e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis
364489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidisvoid ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
364589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  assert(D->DefinitionData);
364689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
364789eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.UserDeclaredConstructor);
364889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.UserDeclaredCopyConstructor);
364989eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.UserDeclaredCopyAssignment);
365089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.UserDeclaredDestructor);
365189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.Aggregate);
365289eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.PlainOldData);
365389eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.Empty);
365489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.Polymorphic);
365589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.Abstract);
365689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.HasTrivialConstructor);
365789eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.HasTrivialCopyConstructor);
365889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.HasTrivialCopyAssignment);
365989eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.HasTrivialDestructor);
366089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.ComputedVisibleConversions);
366189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.DeclaredDefaultConstructor);
366289eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.DeclaredCopyConstructor);
366389eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.DeclaredCopyAssignment);
366489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.DeclaredDestructor);
366589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis
366689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.NumBases);
36677c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  if (Data.NumBases > 0)
36687c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
36697c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                            Record);
36707c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor
367189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  // FIXME: Make VBases lazily computed when needed to avoid storing them.
367289eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  Record.push_back(Data.NumVBases);
36737c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  if (Data.NumVBases > 0)
36747c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor    AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
36757c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor                            Record);
367689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis
367789eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  AddUnresolvedSet(Data.Conversions, Record);
367889eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  AddUnresolvedSet(Data.VisibleConversions, Record);
367989eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  // Data.Definition is the owning decl, no need to write it.
368089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  AddDeclRef(Data.FirstFriend, Record);
368189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis}
368289eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis
36837b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidisvoid ASTWriter::ReaderInitialized(ASTReader *Reader) {
3684ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  assert(Reader && "Cannot remove chain");
36857b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  assert(!Chain && "Cannot replace chain");
3686ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  assert(FirstDeclID == NextDeclID &&
3687ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl         FirstTypeID == NextTypeID &&
3688ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl         FirstIdentID == NextIdentID &&
3689e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl         FirstSelectorID == NextSelectorID &&
369077424bc3794f569816dba52e423b57486a98e5baDouglas Gregor         FirstMacroID == NextMacroID &&
36917c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor         FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
3692ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl         "Setting chain after writing has started.");
3693ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  Chain = Reader;
36947b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
36957b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  FirstDeclID += Chain->getTotalNumDecls();
36967b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  FirstTypeID += Chain->getTotalNumTypes();
36977b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  FirstIdentID += Chain->getTotalNumIdentifiers();
36987b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  FirstSelectorID += Chain->getTotalNumSelectors();
36997b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  FirstMacroID += Chain->getTotalNumMacroDefinitions();
37007c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
37017b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  NextDeclID = FirstDeclID;
37027b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  NextTypeID = FirstTypeID;
37037b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  NextIdentID = FirstIdentID;
37047b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  NextSelectorID = FirstSelectorID;
37057b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis  NextMacroID = FirstMacroID;
37067c789c1a3f77f24032aa0bed2afacdb9e094e952Douglas Gregor  NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
3707ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl}
3708ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl
37098538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
3710f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  IdentifierIDs[II] = ID;
3711040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor  if (II->hasMacroDefinition())
3712040a804f852c45c8b8c371ce1d421fd4cd84b6fbDouglas Gregor    DeserializedMacroNames.push_back(II);
3713f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl}
3714f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl
3715c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidisvoid ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
371697475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  // Always take the highest-numbered type index. This copes with an interesting
371797475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  // case for chained AST writing where we schedule writing the type and then,
371820249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencer  // later, deserialize the type from another AST. In this case, we want to
371997475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  // keep the higher-numbered entry so that we can properly write it out to
372097475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  // the AST file.
372197475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  TypeIdx &StoredIdx = TypeIdxs[T];
372297475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor  if (Idx.getIndex() >= StoredIdx.getIndex())
372397475834207bf5abb5b58534f783c9b71d4b9df1Douglas Gregor    StoredIdx = Idx;
372430c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl}
372530c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl
37268538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid ASTWriter::DeclRead(DeclID ID, const Decl *D) {
37271476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  DeclIDs[D] = ID;
372830c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl}
37295d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl
37308538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid ASTWriter::SelectorRead(SelectorID ID, Selector S) {
37315d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  SelectorIDs[S] = ID;
37325d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl}
373377424bc3794f569816dba52e423b57486a98e5baDouglas Gregor
373420249a1af2e462dcafdd6a350f1c7967b264ff25Michael J. Spencervoid ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
373577424bc3794f569816dba52e423b57486a98e5baDouglas Gregor                                    MacroDefinition *MD) {
373677424bc3794f569816dba52e423b57486a98e5baDouglas Gregor  MacroDefinitions[MD] = ID;
373777424bc3794f569816dba52e423b57486a98e5baDouglas Gregor}
3738565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
3739565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidisvoid ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3740565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  assert(D->isDefinition());
3741565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3742565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    // We are interested when a PCH decl is modified.
3743565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    if (RD->getPCHLevel() > 0) {
3744565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      // A forward reference was mutated into a definition. Rewrite it.
3745565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      // FIXME: This happens during template instantiation, should we
3746565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      // have created a new definition decl instead ?
3747d3d0755a4220d3fa6c125eb3f8ce29fc1424eac3Argyrios Kyrtzidis      RewriteDecl(RD);
3748565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    }
3749565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
3750565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    for (CXXRecordDecl::redecl_iterator
3751565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis           I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3752565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3753565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      if (Redecl == RD)
3754565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis        continue;
3755565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis
3756565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      // We are interested when a PCH decl is modified.
3757565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      if (Redecl->getPCHLevel() > 0) {
3758565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis        UpdateRecord &Record = DeclUpdates[Redecl];
3759565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis        Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3760565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis        assert(Redecl->DefinitionData);
3761565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis        assert(Redecl->DefinitionData->Definition == D);
3762565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis        AddDeclRef(D, Record); // the DefinitionDecl
3763565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis      }
3764565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis    }
3765565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis  }
3766565bf30bf5607b9740d288d8d9c45cf38ea75298Argyrios Kyrtzidis}
3767100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidisvoid ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3768100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  // TU and namespaces are handled elsewhere.
3769100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3770100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis    return;
3771100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis
3772100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3773100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis    return; // Not a source decl added to a DeclContext from PCH.
3774100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis
3775100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis  AddUpdatedDeclContext(DC);
3776100050bf643bcfe2d06bdcef491e387171249260Argyrios Kyrtzidis}
3777b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis
3778b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidisvoid ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3779b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  assert(D->isImplicit());
3780b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3781b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis    return; // Not a source member added to a class from PCH.
3782b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  if (!isa<CXXMethodDecl>(D))
3783b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis    return; // We are interested in lazily declared implicit methods.
3784b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis
3785b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  // A decl coming from PCH was modified.
3786b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  assert(RD->isDefinition());
3787b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  UpdateRecord &Record = DeclUpdates[RD];
3788b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3789b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis  AddDeclRef(D, Record);
3790b6cc0e1a789c3f33e3f4b1ee768e679a9beab941Argyrios Kyrtzidis}
3791bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis
3792bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidisvoid ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3793bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis                                     const ClassTemplateSpecializationDecl *D) {
37940f04f69acd0a86ff78961933c13fe5aeda94e1b6Argyrios Kyrtzidis  // The specializations set is kept in the canonical template.
37950f04f69acd0a86ff78961933c13fe5aeda94e1b6Argyrios Kyrtzidis  TD = TD->getCanonicalDecl();
3796bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis  if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3797bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis    return; // Not a source specialization added to a template from PCH.
3798bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis
3799bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis  UpdateRecord &Record = DeclUpdates[TD];
3800bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis  Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3801bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis  AddDeclRef(D, Record);
3802bef1a7b9c175d37e4a727e6ce68bd05232fa6970Argyrios Kyrtzidis}
380389d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor
380489d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas GregorASTSerializationListener::~ASTSerializationListener() { }
3805