ASTWriter.cpp revision 4ea884b429445aa6f1af5bc6e51d0b65a4043e24
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"
150eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidis#include "ASTCommon.h"
16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/IdentifierResolver.h"
182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ASTContext.h"
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/Decl.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/DeclContextInternals.h"
212a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/AST/DeclTemplate.h"
220b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor#include "clang/AST/Expr.h"
237a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall#include "clang/AST/ExprCXX.h"
242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/Type.h"
25a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#include "clang/AST/TypeLocVisitor.h"
266ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redl#include "clang/Serialization/ASTReader.h"
277c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "clang/Lex/MacroInfo.h"
286a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor#include "clang/Lex/PreprocessingRecord.h"
297c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "clang/Lex/Preprocessor.h"
3083d63c78810556d26b62ac4cbae2eda6cdd2570cSteve Naroff#include "clang/Lex/HeaderSearch.h"
3114f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "clang/Basic/FileManager.h"
323251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor#include "clang/Basic/OnDiskHashTable.h"
3314f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "clang/Basic/SourceManager.h"
34bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor#include "clang/Basic/SourceManagerInternals.h"
352bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor#include "clang/Basic/TargetInfo.h"
36ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor#include "clang/Basic/Version.h"
3717fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor#include "llvm/ADT/APFloat.h"
3817fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor#include "llvm/ADT/APInt.h"
392596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar#include "llvm/ADT/StringExtras.h"
402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "llvm/Bitcode/BitstreamWriter.h"
4114f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "llvm/Support/MemoryBuffer.h"
42b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor#include "llvm/System/Path.h"
433c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner#include <cstdio>
442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorusing namespace clang;
458538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlusing namespace clang::serialization;
462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
47ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redltemplate <typename T, typename Allocator>
48ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian RedlT *data(std::vector<T, Allocator> &v) {
49ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  return v.empty() ? 0 : &v.front();
50ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl}
51ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redltemplate <typename T, typename Allocator>
52ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redlconst T *data(const std::vector<T, Allocator> &v) {
53ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  return v.empty() ? 0 : &v.front();
54ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl}
55ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// Type serialization
582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
5912b1c7615d4f9a2edc544be499f895f16ac100edChris Lattner
602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregornamespace {
613397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  class ASTTypeWriter {
62a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl    ASTWriter &Writer;
63a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl    ASTWriter::RecordData &Record;
642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  public:
662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// \brief Type code that corresponds to the record generated.
678538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    TypeCode Code;
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
693397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
708538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    void VisitArrayType(const ArrayType *T);
732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    void VisitFunctionType(const FunctionType *T);
742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    void VisitTagType(const TagType *T);
752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define ABSTRACT_TYPE(Class, Base)
782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/TypeNodes.def"
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  };
802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
823397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  assert(false && "Built-in types are never serialized");
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
863397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitComplexType(const ComplexType *T) {
872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getElementType(), Record);
888538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_COMPLEX;
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
913397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitPointerType(const PointerType *T) {
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getPointeeType(), Record);
938538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_POINTER;
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
963397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(T->getPointeeType(), Record);
988538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_BLOCK_POINTER;
992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1013397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
1022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getPointeeType(), Record);
1038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_LVALUE_REFERENCE;
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1063397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
1072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getPointeeType(), Record);
1088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_RVALUE_REFERENCE;
1092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1113397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
1121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(T->getPointeeType(), Record);
1131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
1148538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_MEMBER_POINTER;
1152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1173397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitArrayType(const ArrayType *T) {
1182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getElementType(), Record);
1192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getSizeModifier()); // FIXME: stable values
1200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
1212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1233397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitArrayType(T);
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddAPInt(T->getSize(), Record);
1268538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_CONSTANT_ARRAY;
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1293397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitArrayType(T);
1318538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_INCOMPLETE_ARRAY;
1322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1343397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitArrayType(T);
1367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Writer.AddSourceLocation(T->getLBracketLoc(), Record);
1377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Writer.AddSourceLocation(T->getRBracketLoc(), Record);
138c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Writer.AddStmt(T->getSizeExpr());
1398538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_VARIABLE_ARRAY;
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1423397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitVectorType(const VectorType *T) {
1432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getElementType(), Record);
1442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getNumElements());
145788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  Record.push_back(T->getAltiVecSpecific());
1468538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_VECTOR;
1472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1493397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
1502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitVectorType(T);
1518538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_EXT_VECTOR;
1522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1543397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
1552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getResultType(), Record);
156264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo C = T->getExtInfo();
157264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  Record.push_back(C.getNoReturn());
158425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  Record.push_back(C.getRegParm());
159ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  // FIXME: need to stabilize encoding of calling convention...
160264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  Record.push_back(C.getCC());
1612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1633397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
1642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitFunctionType(T);
1658538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_FUNCTION_NO_PROTO;
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1683397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
1692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitFunctionType(T);
1702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getNumArgs());
1712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
1722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Writer.AddTypeRef(T->getArgType(I), Record);
1732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->isVariadic());
1742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getTypeQuals());
175465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  Record.push_back(T->hasExceptionSpec());
176465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  Record.push_back(T->hasAnyExceptionSpec());
177465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  Record.push_back(T->getNumExceptions());
178465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
179465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    Writer.AddTypeRef(T->getExceptionType(I), Record);
1808538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_FUNCTION_PROTO;
1812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1833397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
184ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Writer.AddDeclRef(T->getDecl(), Record);
1858538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_UNRESOLVED_USING;
186ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
187ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1883397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
1892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddDeclRef(T->getDecl(), Record);
1909763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
1919763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
1928538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TYPEDEF;
1932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1953397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
196c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Writer.AddStmt(T->getUnderlyingExpr());
1978538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TYPEOF_EXPR;
1982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2003397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
2012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddTypeRef(T->getUnderlyingType(), Record);
2028538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TYPEOF;
2032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2053397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
206395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Writer.AddStmt(T->getUnderlyingExpr());
2078538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DECLTYPE;
208395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
209395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2103397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitTagType(const TagType *T) {
211be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  Record.push_back(T->isDependentType());
2122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Writer.AddDeclRef(T->getDecl(), Record);
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!T->isBeingDefined() &&
2142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor         "Cannot serialize in the middle of a type definition");
2152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2173397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitRecordType(const RecordType *T) {
2182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitTagType(T);
2198538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_RECORD;
2202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2223397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitEnumType(const EnumType *T) {
2232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  VisitTagType(T);
2248538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_ENUM;
2252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2283397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitSubstTemplateTypeParmType(
22949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                        const SubstTemplateTypeParmType *T) {
23049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
23149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  Writer.AddTypeRef(T->getReplacementType(), Record);
2328538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
23349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
23449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
23549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallvoid
2363397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitTemplateSpecializationType(
2372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                       const TemplateSpecializationType *T) {
238be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  Record.push_back(T->isDependentType());
23990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Writer.AddTemplateName(T->getTemplateName(), Record);
24090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->getNumArgs());
24190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
24290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis         ArgI != ArgE; ++ArgI)
24390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Writer.AddTemplateArgument(*ArgI, Record);
2449763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
2459763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                : T->getCanonicalTypeInternal(),
2469763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                    Record);
2478538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TEMPLATE_SPECIALIZATION;
24890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
24990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
25090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2513397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
252ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis  VisitArrayType(T);
253ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis  Writer.AddStmt(T->getSizeExpr());
254ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis  Writer.AddSourceRange(T->getBracketsRange(), Record);
2558538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DEPENDENT_SIZED_ARRAY;
25690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
25790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
25890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2593397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentSizedExtVectorType(
26090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis                                        const DependentSizedExtVectorType *T) {
26190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  // FIXME: Serialize this type (C++ only)
26290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  assert(false && "Cannot serialize dependent sized extended vector types");
26390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
26490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
26590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2663397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
26790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->getDepth());
26890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->getIndex());
26990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(T->isParameterPack());
27090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Writer.AddIdentifierRef(T->getName(), Record);
2718538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_TEMPLATE_TYPE_PARM;
27290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
27390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
27490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2753397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
2768dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  Record.push_back(T->getKeyword());
2778dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
2788dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  Writer.AddIdentifierRef(T->getIdentifier(), Record);
279f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
280f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis                                                : T->getCanonicalTypeInternal(),
281f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis                    Record);
2828538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DEPENDENT_NAME;
28390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
28490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
28590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisvoid
2863397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitDependentTemplateSpecializationType(
28790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis                                const DependentTemplateSpecializationType *T) {
2883acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Record.push_back(T->getKeyword());
2893acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
2903acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddIdentifierRef(T->getIdentifier(), Record);
2913acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Record.push_back(T->getNumArgs());
2923acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  for (DependentTemplateSpecializationType::iterator
2933acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis         I = T->begin(), E = T->end(); I != E; ++I)
2943acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis    Writer.AddTemplateArgument(*I, Record);
2958538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
2962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2983397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
299465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  Record.push_back(T->getKeyword());
3003acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
3013acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis  Writer.AddTypeRef(T->getNamedType(), Record);
3028538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_ELABORATED;
3032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3053397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
3063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  Writer.AddDeclRef(T->getDecl(), Record);
30731f17ecbef57b5679c017c375db330546b7b5145John McCall  Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
3088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_INJECTED_CLASS_NAME;
3093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
3103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
3113397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
312deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Writer.AddDeclRef(T->getDecl(), Record);
3138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_OBJC_INTERFACE;
314c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
315c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
3163397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
317c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Writer.AddTypeRef(T->getBaseType(), Record);
3182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(T->getNumProtocols());
319c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator I = T->qual_begin(),
320446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff       E = T->qual_end(); I != E; ++I)
321446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff    Writer.AddDeclRef(*I, Record);
3228538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_OBJC_OBJECT;
3232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
325d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroffvoid
3263397c5570369f19b2d6c52e898f708d75ceede1fSebastian RedlASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Writer.AddTypeRef(T->getPointeeType(), Record);
3288538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Code = TYPE_OBJC_OBJECT_POINTER;
3292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
3302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
331a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallnamespace {
332a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
333a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallclass TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
334a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter &Writer;
335a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter::RecordData &Record;
336a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
337a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCallpublic:
338a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
339a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    : Writer(Writer), Record(Record) { }
340a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
34151bd803fbdade51d674598ed45da3d54190a656cJohn McCall#define ABSTRACT_TYPELOC(CLASS, PARENT)
342a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#define TYPELOC(CLASS, PARENT) \
34351bd803fbdade51d674598ed45da3d54190a656cJohn McCall    void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
344a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#include "clang/AST/TypeLocNodes.def"
345a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
34651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
34751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
348a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall};
349a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
350a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
351a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
35251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
35351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  // nothing to do
35451bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
35551bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
356ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
357ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  if (TL.needsExtraLocalData()) {
358ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.getWrittenTypeSpec());
359ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.getWrittenSignSpec());
360ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.getWrittenWidthSpec());
361ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    Record.push_back(TL.hasModeAttr());
362ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  }
36351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
36451bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
36551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
36651bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
36751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
36851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getStarLoc(), Record);
36951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
37051bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
37151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getCaretLoc(), Record);
37251bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
37351bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
37451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getAmpLoc(), Record);
37551bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
37651bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
37751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
37851bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
37951bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
38051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getStarLoc(), Record);
38151bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
38251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
38351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
38451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
38551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Record.push_back(TL.getSizeExpr() ? 1 : 0);
38651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  if (TL.getSizeExpr())
38751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    Writer.AddStmt(TL.getSizeExpr());
38851bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
38951bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
39051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
39151bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
39251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
39351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
39451bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
39551bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
39651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
39751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
39851bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitDependentSizedArrayTypeLoc(
39951bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                            DependentSizedArrayTypeLoc TL) {
40051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitArrayTypeLoc(TL);
40151bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
40251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
40351bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                        DependentSizedExtVectorTypeLoc TL) {
40451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
40551bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
40651bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
40751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
40851bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
40951bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
41051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
41151bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
41251bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
41351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
41451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
41551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
41651bd803fbdade51d674598ed45da3d54190a656cJohn McCall    Writer.AddDeclRef(TL.getArg(i), Record);
41751bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
41851bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
41951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitFunctionTypeLoc(TL);
42051bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
42151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
42251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  VisitFunctionTypeLoc(TL);
42351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
424ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
425ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
426ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
42751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
42851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
42951bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
43051bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
431cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
432cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
433cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
43451bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
43551bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
436cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
437cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
438cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
439cfb708c354e2f30ccc5cba9d644650f408a1ec3eJohn McCall  Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
44051bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
44151bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
44251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
443a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
44451bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
44551bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
446a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
44751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
44851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
449a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
45051bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
45151bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
452a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
45349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallvoid TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
45449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                            SubstTemplateTypeParmTypeLoc TL) {
45549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
45649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
45751bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitTemplateSpecializationTypeLoc(
45851bd803fbdade51d674598ed45da3d54190a656cJohn McCall                                           TemplateSpecializationTypeLoc TL) {
459833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
460833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
461833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
462833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
46344f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
46444f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                      TL.getArgLoc(i).getLocInfo(), Record);
465a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
466465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnaravoid TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
467e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
468e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceRange(TL.getQualifierRange(), Record);
469a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
4703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallvoid TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
4713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
4723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
4734714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregorvoid TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
474e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
475e4da7a034a2fcf4b14d0bcc28d05de0878159061Abramo Bagnara  Writer.AddSourceRange(TL.getQualifierRange(), Record);
47651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
477a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
47833500955d731c73717af52088b7fc0e7a85681e7John McCallvoid TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
47933500955d731c73717af52088b7fc0e7a85681e7John McCall       DependentTemplateSpecializationTypeLoc TL) {
48033500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
48133500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceRange(TL.getQualifierRange(), Record);
48233500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
48333500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
48433500955d731c73717af52088b7fc0e7a85681e7John McCall  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
48533500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
48644f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
48744f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                      TL.getArgLoc(I).getLocInfo(), Record);
48833500955d731c73717af52088b7fc0e7a85681e7John McCall}
48951bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
49051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getNameLoc(), Record);
491c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
492c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallvoid TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
493c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Record.push_back(TL.hasBaseTypeAsWritten());
49454e14c4db764c0636160d26c5bbf491637c83a76John McCall  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
49554e14c4db764c0636160d26c5bbf491637c83a76John McCall  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
49654e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
49754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
498a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
49951bd803fbdade51d674598ed45da3d54190a656cJohn McCallvoid TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
50051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  Writer.AddSourceLocation(TL.getStarLoc(), Record);
501a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
502a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
5034dcf151a555ff51e4d643e8e6eeb80f121d11d1bChris Lattner//===----------------------------------------------------------------------===//
504a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl// ASTWriter Implementation
5052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
5062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
507b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattnerstatic void EmitBlockID(unsigned ID, const char *Name,
508b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner                        llvm::BitstreamWriter &Stream,
509a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl                        ASTWriter::RecordData &Record) {
510b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.clear();
511b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.push_back(ID);
512b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
513b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
514b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  // Emit the block name if present.
515b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  if (Name == 0 || Name[0] == 0) return;
516b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.clear();
517b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  while (*Name)
518b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner    Record.push_back(*Name++);
519b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
520b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner}
521b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
522b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattnerstatic void EmitRecordID(unsigned ID, const char *Name,
523b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner                         llvm::BitstreamWriter &Stream,
524a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl                         ASTWriter::RecordData &Record) {
525b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.clear();
526b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Record.push_back(ID);
527b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  while (*Name)
528b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner    Record.push_back(*Name++);
529b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
5300558df2da807646e65d4fa290f4e92114af1a746Chris Lattner}
5310558df2da807646e65d4fa290f4e92114af1a746Chris Lattner
5320558df2da807646e65d4fa290f4e92114af1a746Chris Lattnerstatic void AddStmtsExprs(llvm::BitstreamWriter &Stream,
533a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl                          ASTWriter::RecordData &Record) {
5348538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
5350558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_STOP);
5360558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_NULL_PTR);
5370558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_NULL);
5380558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_COMPOUND);
5390558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_CASE);
5400558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_DEFAULT);
5410558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_LABEL);
5420558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_IF);
5430558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_SWITCH);
5440558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_WHILE);
5450558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_DO);
5460558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_FOR);
5470558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_GOTO);
5480558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_INDIRECT_GOTO);
5490558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_CONTINUE);
5500558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_BREAK);
5510558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_RETURN);
5520558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_DECL);
5530558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_ASM);
5540558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_PREDEFINED);
5550558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_DECL_REF);
5560558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_INTEGER_LITERAL);
5570558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_FLOATING_LITERAL);
5580558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_IMAGINARY_LITERAL);
5590558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_STRING_LITERAL);
5600558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CHARACTER_LITERAL);
5610558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_PAREN);
5620558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_UNARY_OPERATOR);
5630558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_SIZEOF_ALIGN_OF);
5640558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_ARRAY_SUBSCRIPT);
5650558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CALL);
5660558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_MEMBER);
5670558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_BINARY_OPERATOR);
5680558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
5690558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CONDITIONAL_OPERATOR);
5700558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_IMPLICIT_CAST);
5710558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CSTYLE_CAST);
5720558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_COMPOUND_LITERAL);
5730558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_EXT_VECTOR_ELEMENT);
5740558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_INIT_LIST);
5750558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_DESIGNATED_INIT);
5760558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_IMPLICIT_VALUE_INIT);
5770558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_VA_ARG);
5780558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_ADDR_LABEL);
5790558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_STMT);
5800558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_TYPES_COMPATIBLE);
5810558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_CHOOSE);
5820558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_GNU_NULL);
5830558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_SHUFFLE_VECTOR);
5840558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_BLOCK);
5850558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_BLOCK_DECL_REF);
5860558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_STRING_LITERAL);
5870558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_ENCODE);
5880558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_SELECTOR_EXPR);
5890558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_PROTOCOL_EXPR);
5900558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_IVAR_REF_EXPR);
5910558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
5920558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_KVC_REF_EXPR);
5930558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_MESSAGE_EXPR);
5940558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(EXPR_OBJC_SUPER_EXPR);
5950558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_FOR_COLLECTION);
5960558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_CATCH);
5970558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_FINALLY);
5980558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_AT_TRY);
5990558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_AT_SYNCHRONIZED);
6000558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  RECORD(STMT_OBJC_AT_THROW);
601eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_OPERATOR_CALL);
602eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_CONSTRUCT);
603eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_STATIC_CAST);
604eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_DYNAMIC_CAST);
605eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_REINTERPRET_CAST);
606eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_CONST_CAST);
607eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_FUNCTIONAL_CAST);
608eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_BOOL_LITERAL);
609eb7f96141f754150a92433286fa385910a22f494Sam Weinig  RECORD(EXPR_CXX_NULL_PTR_LITERAL);
6100558df2da807646e65d4fa290f4e92114af1a746Chris Lattner#undef RECORD
611b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner}
6121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
613a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteBlockInfoBlock() {
614b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RecordData Record;
615b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6178538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
6188538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6203397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // AST Top-Level Block.
621f29f0a28c4d9599b389bbb6d186e14af753dc5a3Sebastian Redl  BLOCK(AST_BLOCK);
62251e774d42269e3b22d746184c0b9076fc13b32e6Zhongxing Xu  RECORD(ORIGINAL_FILE_NAME);
623b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_OFFSET);
624b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_OFFSET);
625b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(LANGUAGE_OPTIONS);
626ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor  RECORD(METADATA);
627b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(IDENTIFIER_OFFSET);
628b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(IDENTIFIER_TABLE);
629b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(EXTERNAL_DEFINITIONS);
630b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SPECIAL_TYPES);
631b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(STATISTICS);
632b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TENTATIVE_DEFINITIONS);
63349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  RECORD(UNUSED_FILESCOPED_DECLS);
634b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
635b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SELECTOR_OFFSETS);
636b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(METHOD_POOL);
637b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_COUNTER_VALUE);
6387f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RECORD(SOURCE_LOCATION_OFFSETS);
6397f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RECORD(SOURCE_LOCATION_PRELOADS);
6404fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  RECORD(STAT_CACHE);
641b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  RECORD(EXT_VECTOR_DECLS);
6425b4ec636637c9d876102240127cc0dca9280e83aTed Kremenek  RECORD(VERSION_CONTROL_BRANCH_REVISION);
6436a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  RECORD(MACRO_DEFINITION_OFFSETS);
644a93e3b5bde9f0a7b59215f19f176f7d69881b81cSebastian Redl  RECORD(CHAINED_METADATA);
645320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  RECORD(REFERENCED_SELECTOR_POOL);
6466a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
647b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  // SourceManager Block.
6482f4efd10c805cb779618c1a22a35eb07b5043c4eChris Lattner  BLOCK(SOURCE_MANAGER_BLOCK);
649b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_FILE_ENTRY);
650b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_BUFFER_ENTRY);
651b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_BUFFER_BLOB);
652b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_SLOC_INSTANTIATION_ENTRY);
653b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(SM_LINE_TABLE);
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  // Preprocessor Block.
6562f4efd10c805cb779618c1a22a35eb07b5043c4eChris Lattner  BLOCK(PREPROCESSOR_BLOCK);
657b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_MACRO_OBJECT_LIKE);
658b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_MACRO_FUNCTION_LIKE);
659b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(PP_TOKEN);
6606a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  RECORD(PP_MACRO_INSTANTIATION);
6616a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  RECORD(PP_MACRO_DEFINITION);
6626a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
66361d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  // Decls and Types block.
66461d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  BLOCK(DECLTYPES_BLOCK);
665b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_EXT_QUAL);
666b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_COMPLEX);
667b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_POINTER);
668b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_BLOCK_POINTER);
669b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_LVALUE_REFERENCE);
670b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_RVALUE_REFERENCE);
671b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_MEMBER_POINTER);
672b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_CONSTANT_ARRAY);
673b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_INCOMPLETE_ARRAY);
674b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_VARIABLE_ARRAY);
675b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_VECTOR);
676b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_EXT_VECTOR);
677b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_FUNCTION_PROTO);
678b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_FUNCTION_NO_PROTO);
679b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_TYPEDEF);
680b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_TYPEOF_EXPR);
681b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_TYPEOF);
682b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_RECORD);
683b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_ENUM);
684b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(TYPE_OBJC_INTERFACE);
685a53d2cbe37e4be0d95b9d3e09f74eafae31fc940John McCall  RECORD(TYPE_OBJC_OBJECT);
686d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  RECORD(TYPE_OBJC_OBJECT_POINTER);
6870ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_ATTR);
6880ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_TRANSLATION_UNIT);
6890ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_TYPEDEF);
6900ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_ENUM);
6910ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_RECORD);
6920ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_ENUM_CONSTANT);
6930ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_FUNCTION);
6940ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_METHOD);
6950ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_INTERFACE);
6960ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_PROTOCOL);
6970ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_IVAR);
6980ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_AT_DEFS_FIELD);
6990ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_CLASS);
7000ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_FORWARD_PROTOCOL);
7010ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_CATEGORY);
7020ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_CATEGORY_IMPL);
7030ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_IMPLEMENTATION);
7040ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
7050ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_PROPERTY);
7060ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_OBJC_PROPERTY_IMPL);
707b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_FIELD);
708b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_VAR);
7090ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_IMPLICIT_PARAM);
710b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  RECORD(DECL_PARM_VAR);
7110ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_FILE_SCOPE_ASM);
7120ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_BLOCK);
7130ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_CONTEXT_LEXICAL);
7140ff8cda4442cff571aba1be91dd16f64a0bf16aaChris Lattner  RECORD(DECL_CONTEXT_VISIBLE);
71561d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  // Statements and Exprs can occur in the Decls and Types block.
7160558df2da807646e65d4fa290f4e92114af1a746Chris Lattner  AddStmtsExprs(Stream, Record);
717b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner#undef RECORD
718b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner#undef BLOCK
719b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  Stream.ExitBlock();
720b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner}
721b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
722e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \brief Adjusts the given filename to only write out the portion of the
723e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// filename that is not part of the system root directory.
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
725e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \param Filename the file name to adjust.
726e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor///
727e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
728e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// the returned filename will be adjusted by this system root.
729e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor///
730e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// \returns either the original filename (if it needs no adjustment) or the
731e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor/// adjusted filename (which points into the @p Filename parameter).
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic const char *
733e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas GregoradjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
734e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  assert(Filename && "No file name to adjust?");
7351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
736e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (!isysroot)
737e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    return Filename;
7381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
739e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // Verify that the filename and the system root have the same prefix.
740e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  unsigned Pos = 0;
741e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  for (; Filename[Pos] && isysroot[Pos]; ++Pos)
742e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    if (Filename[Pos] != isysroot[Pos])
743e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor      return Filename; // Prefixes don't match.
7441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
745e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // We hit the end of the filename before we hit the end of the system root.
746e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (!Filename[Pos])
747e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    return Filename;
7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
749e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // If the file name has a '/' at the current position, skip over the '/'.
750e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // We distinguish sysroot-based includes from absolute includes by the
751e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // absence of '/' at the beginning of sysroot-based includes.
752e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (Filename[Pos] == '/')
753e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    ++Pos;
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
755e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  return Filename + Pos;
756e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor}
757b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner
7583397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
759a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
7602bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor  using namespace llvm;
761b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor
762e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  // Metadata
763e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  const TargetInfo &Target = Context.Target;
764e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
76577f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(
7668538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                    Chain ? CHAINED_METADATA : METADATA));
7673397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
7683397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
769e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
770e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
771e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
77277f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  // Target triple or chained PCH name
77377f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
774e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
7751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
776e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  RecordData Record;
7778538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(Chain ? CHAINED_METADATA : METADATA);
7788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(VERSION_MAJOR);
7798538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(VERSION_MINOR);
780e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  Record.push_back(CLANG_VERSION_MAJOR);
781e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  Record.push_back(CLANG_VERSION_MINOR);
782e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  Record.push_back(isysroot != 0);
78377f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  // FIXME: This writes the absolute path for chained headers.
78477f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
78577f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl  Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
787b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  // Original file name
788b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  SourceManager &SM = Context.getSourceManager();
789b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
790b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
7918538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
792b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
793b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
794b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor
795b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    llvm::sys::Path MainFilePath(MainFile->getName());
7961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
79711a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam    MainFilePath.makeAbsolute();
798b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor
799aba54a95e9d5e4dc9056abec6bb70ea777c4a7bcKovarththanan Rajaratnam    const char *MainFileNameStr = MainFilePath.c_str();
8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
801e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor                                                      isysroot);
802b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor    RecordData Record;
8038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(ORIGINAL_FILE_NAME);
804ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar    Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
805b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor  }
80611a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
807f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  // Repository branch/version information.
808f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
8098538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
810f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
811f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
812445e23e9b909ec8e21303c7dd82c90b72fc09ac4Douglas Gregor  Record.clear();
8138538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
814f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek  Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
815f7a96a39958b3f919f26764777eec948b43d74bcTed Kremenek                            getClangFullRepositoryVersion());
8162bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor}
8172bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor
8182bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor/// \brief Write the LangOptions structure.
819a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
8200a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  RecordData Record;
8210a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Trigraphs);
8220a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.BCPLComment);  // BCPL-style '//' comments.
8230a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.DollarIdents);  // '$' allowed in identifiers.
8240a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.AsmPreprocessor);  // Preprocessor in asm mode.
8250a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.GNUMode);  // True in gnu99 mode false in c99 mode (etc)
826eb5d7b752651283de5abfcc2f91df7227582a08dChandler Carruth  Record.push_back(LangOpts.GNUKeywords);  // Allow GNU-extension keywords
8270a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.ImplicitInt);  // C89 implicit 'int'.
8280a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Digraphs);  // C94, C99 and C++
8290a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.HexFloats);  // C99 Hexadecimal float constants.
8300a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.C99);  // C99 Support
8310a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Microsoft);  // Microsoft extensions.
8320a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.CPlusPlus);  // C++ Support
8330a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.CPlusPlus0x);  // C++0x Support
8340a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.CXXOperatorNames);  // Treat C++ operator names as keywords.
8351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8360a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.ObjC1);  // Objective-C 1 support enabled.
8370a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.ObjC2);  // Objective-C 2 support enabled.
83811a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam  Record.push_back(LangOpts.ObjCNonFragileABI);  // Objective-C
839412e798941ca64e2e6b084323915fa9aa5f6bdf3Fariborz Jahanian                                                 // modern abi enabled.
84011a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam  Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
841412e798941ca64e2e6b084323915fa9aa5f6bdf3Fariborz Jahanian                                                 // modern abi enabled.
8424c9d8d0eca5ca635d9a30222f690db9140e98325Fariborz Jahanian  Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
8431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8440a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.PascalStrings);  // Allow Pascal strings
8450a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.WritableStrings);  // Allow writable strings
8460a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.LaxVectorConversions);
847b9e7e63ae2098bc02e79c032df0a3124d09a4b4eNate Begeman  Record.push_back(LangOpts.AltiVec);
8480a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Exceptions);  // Support exception handling.
84973482884560be041d86eccbd7dd5a6918677393bDaniel Dunbar  Record.push_back(LangOpts.SjLjExceptions);
8500a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
8510a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
8520a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Freestanding); // Freestanding implementation
8530a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
8540a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
855ea5ce4705df0743093925585d8edc80e0d8fe3ffChris Lattner  // Whether static initializers are protected by locks.
856ea5ce4705df0743093925585d8edc80e0d8fe3ffChris Lattner  Record.push_back(LangOpts.ThreadsafeStatics);
857972d954bd216c86a961bb7f81c53af85de17c2f0Douglas Gregor  Record.push_back(LangOpts.POSIXThreads);
8580a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Blocks); // block extension to C
8590a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
8600a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // they are unused.
8610a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
8620a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // (modulo the platform support).
8630a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
864a4d71455f0d418e16cc0c5c5aa55a3bad3494aeeChris Lattner  Record.push_back(LangOpts.getSignedOverflowBehavior());
865a4d71455f0d418e16cc0c5c5aa55a3bad3494aeeChris Lattner  Record.push_back(LangOpts.HeinousExtensions);
8660a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
8670a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
8690a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // defined.
8700a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
8710a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // opposed to __DYNAMIC__).
8720a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
8730a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
8740a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
8750a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor                                  // used (instead of C99 semantics).
8760a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
877a33d9b4ebf732a5da6d56fd7319ff6c020789b1cAnders Carlsson  Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
878a33d9b4ebf732a5da6d56fd7319ff6c020789b1cAnders Carlsson                                            // be enabled.
87915b91764d08e886391c865c4a444d7b51141c284Eli Friedman  Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
88015b91764d08e886391c865c4a444d7b51141c284Eli Friedman                                           // unsigned type
881a6fda124bf380479529d6a80b84b62cacd3cb707John Thompson  Record.push_back(LangOpts.ShortWChar);  // force wchar_t to be unsigned short
8820a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.getGCMode());
8830a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.getVisibilityMode());
884ab8e281b32a3d3b9b18257d26844362bf806ecdcDaniel Dunbar  Record.push_back(LangOpts.getStackProtectorMode());
8850a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor  Record.push_back(LangOpts.InstantiationDepth);
886b9e7e63ae2098bc02e79c032df0a3124d09a4b4eNate Begeman  Record.push_back(LangOpts.OpenCL);
8879c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  Record.push_back(LangOpts.CatchUndefined);
88892f5822df6a0d7571df44b5d279ed4f017fbb0e6Anders Carlsson  Record.push_back(LangOpts.ElideConstructors);
889a0068fc64351db9c47916566e3b85ab733cd8d6dDouglas Gregor  Record.push_back(LangOpts.SpellChecking);
8908538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
8910a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor}
8920a0428e96c6f1e8bef7a481a9eb69a6f6df38951Douglas Gregor
89314f79002e58556798e86168c63e48d533287eda5Douglas Gregor//===----------------------------------------------------------------------===//
8944fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// stat cache Serialization
8954fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
8964fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
8974fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregornamespace {
8984fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Trait used for the on-disk hash table of stat cache results.
8993397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlclass ASTStatCacheTrait {
9004fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregorpublic:
9014fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  typedef const char * key_type;
9024fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  typedef key_type key_type_ref;
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9044fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  typedef std::pair<int, struct stat> data_type;
9054fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  typedef const data_type& data_type_ref;
9064fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
9074fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  static unsigned ComputeHash(const char *path) {
9082596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar    return llvm::HashString(path);
9094fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
9101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<unsigned,unsigned>
9124fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
9134fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor                      data_type_ref Data) {
9144fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    unsigned StrLen = strlen(path);
9154fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    clang::io::Emit16(Out, StrLen);
9164fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    unsigned DataLen = 1; // result value
9174fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    if (Data.first == 0)
9184fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor      DataLen += 4 + 4 + 2 + 8 + 8;
9194fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    clang::io::Emit8(Out, DataLen);
9204fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    return std::make_pair(StrLen + 1, DataLen);
9214fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9234fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
9244fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    Out.write(path, KeyLen);
9254fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
9261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9274fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  void EmitData(llvm::raw_ostream& Out, key_type_ref,
9284fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor                data_type_ref Data, unsigned DataLen) {
9294fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    using namespace clang::io;
9304fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    uint64_t Start = Out.tell(); (void)Start;
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9324fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    // Result of stat()
9334fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    Emit8(Out, Data.first? 1 : 0);
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9354fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    if (Data.first == 0) {
9364fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor      Emit32(Out, (uint32_t) Data.second.st_ino);
9374fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor      Emit32(Out, (uint32_t) Data.second.st_dev);
9384fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor      Emit16(Out, (uint16_t) Data.second.st_mode);
9394fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor      Emit64(Out, (uint64_t) Data.second.st_mtime);
9404fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor      Emit64(Out, (uint64_t) Data.second.st_size);
9414fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    }
9424fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
9434fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    assert(Out.tell() - Start == DataLen && "Wrong data length");
9444fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
9454fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor};
9464fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor} // end anonymous namespace
9474fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
9483397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the stat() system call cache to the AST file.
949a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
9504fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Build the on-disk hash table containing information about every
9514fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // stat() call.
9523397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
9534fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  unsigned NumStatEntries = 0;
9541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
9554fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor                                StatEnd = StatCalls.end();
956e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor       Stat != StatEnd; ++Stat, ++NumStatEntries) {
957e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    const char *Filename = Stat->first();
958e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor    Generator.insert(Filename, Stat->second);
959e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  }
9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9614fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Create the on-disk hash table in a buffer.
9621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::SmallString<4096> StatCacheData;
9634fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  uint32_t BucketOffset;
9644fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  {
9654fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    llvm::raw_svector_ostream Out(StatCacheData);
9664fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    // Make sure that no bucket is at offset 0
9674fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    clang::io::Emit32(Out, 0);
9684fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    BucketOffset = Generator.Emit(Out);
9694fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  }
9704fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
9714fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Create a blob abbreviation
9724fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  using namespace llvm;
9734fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
9748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
9754fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
9764fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
9774fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
9784fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
9794fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
9804fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  // Write the stat cache
9814fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  RecordData Record;
9828538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(STAT_CACHE);
9834fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Record.push_back(BucketOffset);
9844fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor  Record.push_back(NumStatEntries);
985ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar  Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
9864fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor}
9874fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
9884fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
98914f79002e58556798e86168c63e48d533287eda5Douglas Gregor// Source Manager Serialization
99014f79002e58556798e86168c63e48d533287eda5Douglas Gregor//===----------------------------------------------------------------------===//
99114f79002e58556798e86168c63e48d533287eda5Douglas Gregor
99214f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to a
99314f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// file.
994c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
99514f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
99614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
9978538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
99814f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
99914f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
100014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
100114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
10022d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor  // FileEntry fields.
10032d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
10042d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
100512fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor  // HeaderFileInfo fields.
100612fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
100712fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
100812fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
100912fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
101014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1011c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
101214f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
101314f79002e58556798e86168c63e48d533287eda5Douglas Gregor
101414f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to a
101514f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// buffer.
1016c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
101714f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
101814f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
10198538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
102014f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
102114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
102214f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
102314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
102414f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1025c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
102614f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
102714f79002e58556798e86168c63e48d533287eda5Douglas Gregor
102814f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to a
102914f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// buffer's blob.
1030c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
103114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
103214f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
10338538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
103414f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1035c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
103614f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
103714f79002e58556798e86168c63e48d533287eda5Douglas Gregor
103814f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Create an abbreviation for the SLocEntry that refers to an
103914f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// buffer.
1040c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregorstatic unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
104114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  using namespace llvm;
104214f79002e58556798e86168c63e48d533287eda5Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
10438538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
104414f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
104514f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
104614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
104714f79002e58556798e86168c63e48d533287eda5Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1048f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1049c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  return Stream.EmitAbbrev(Abbrev);
105014f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
105114f79002e58556798e86168c63e48d533287eda5Douglas Gregor
105214f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// \brief Writes the block containing the serialized form of the
105314f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// source manager.
105414f79002e58556798e86168c63e48d533287eda5Douglas Gregor///
105514f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// TODO: We should probably use an on-disk hash table (stored in a
105614f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// blob), indexed based on the file name, so that we only create
105714f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// entries for files that we actually need. In the common case (no
105814f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// errors), we probably won't have to create file entries for any of
105914f79002e58556798e86168c63e48d533287eda5Douglas Gregor/// the files in the AST.
1060a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1061e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor                                        const Preprocessor &PP,
1062e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor                                        const char *isysroot) {
10637f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RecordData Record;
10647f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
1065f04ad69fed38d26fc0d6f7d6fd0a4631ddfbc7feChris Lattner  // Enter the source manager block.
10668538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
106714f79002e58556798e86168c63e48d533287eda5Douglas Gregor
106814f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // Abbreviations for the various kinds of source-location entries.
1069828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1070828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1071828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1072828e18cd80319c67b9b9776d1ed5411161d9f0bfChris Lattner  unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
10737f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
10747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  // Write the line table.
10757f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  if (SourceMgr.hasLineTable()) {
10767f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    LineTableInfo &LineTable = SourceMgr.getLineTable();
10777f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
10787f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    // Emit the file names
10797f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    Record.push_back(LineTable.getNumFilenames());
10807f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
10817f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      // Emit the file name
10827f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      const char *Filename = LineTable.getFilename(I);
1083e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor      Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
10847f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      unsigned FilenameLen = Filename? strlen(Filename) : 0;
10857f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      Record.push_back(FilenameLen);
10867f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      if (FilenameLen)
10877f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.insert(Record.end(), Filename, Filename + FilenameLen);
10887f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    }
10891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10907f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    // Emit the line entries
10917f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
10927f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor         L != LEnd; ++L) {
10937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      // Emit the file ID
10947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      Record.push_back(L->first);
10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10967f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      // Emit the line entries
10977f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      Record.push_back(L->second.size());
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (std::vector<LineEntry>::iterator LE = L->second.begin(),
10997f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor                                         LEEnd = L->second.end();
11007f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor           LE != LEEnd; ++LE) {
11017f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->FileOffset);
11027f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->LineNo);
11037f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->FilenameID);
11047f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back((unsigned)LE->FileKind);
11057f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        Record.push_back(LE->IncludeOffset);
11067f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor      }
11077f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    }
11088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(SM_LINE_TABLE, Record);
11097f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  }
11107f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
111114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // Write out the source location entry table. We skip the first
111214f79002e58556798e86168c63e48d533287eda5Douglas Gregor  // entry, which is always the same dummy entry.
1113090d9b53e32bb30d9e74de895bb59b409bd49e00Chris Lattner  std::vector<uint32_t> SLocEntryOffsets;
11147f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  RecordData PreloadSLocs;
11150fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl  unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
11160fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl  SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
11170fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl  for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
11180fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl       I != N; ++I) {
1119bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    // Get this source location entry.
1120bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
112111a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
11227f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    // Record the offset of this source-location entry.
11237f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
11247f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
112514f79002e58556798e86168c63e48d533287eda5Douglas Gregor    // Figure out which record code to use.
112614f79002e58556798e86168c63e48d533287eda5Douglas Gregor    unsigned Code;
112714f79002e58556798e86168c63e48d533287eda5Douglas Gregor    if (SLoc->isFile()) {
112814f79002e58556798e86168c63e48d533287eda5Douglas Gregor      if (SLoc->getFile().getContentCache()->Entry)
11298538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Code = SM_SLOC_FILE_ENTRY;
113014f79002e58556798e86168c63e48d533287eda5Douglas Gregor      else
11318538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Code = SM_SLOC_BUFFER_ENTRY;
113214f79002e58556798e86168c63e48d533287eda5Douglas Gregor    } else
11338538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Code = SM_SLOC_INSTANTIATION_ENTRY;
11347f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    Record.clear();
113514f79002e58556798e86168c63e48d533287eda5Douglas Gregor    Record.push_back(Code);
113614f79002e58556798e86168c63e48d533287eda5Douglas Gregor
113714f79002e58556798e86168c63e48d533287eda5Douglas Gregor    Record.push_back(SLoc->getOffset());
113814f79002e58556798e86168c63e48d533287eda5Douglas Gregor    if (SLoc->isFile()) {
113914f79002e58556798e86168c63e48d533287eda5Douglas Gregor      const SrcMgr::FileInfo &File = SLoc->getFile();
114014f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(File.getIncludeLoc().getRawEncoding());
114114f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1142bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor      Record.push_back(File.hasLineDirectives());
114314f79002e58556798e86168c63e48d533287eda5Douglas Gregor
114414f79002e58556798e86168c63e48d533287eda5Douglas Gregor      const SrcMgr::ContentCache *Content = File.getContentCache();
114514f79002e58556798e86168c63e48d533287eda5Douglas Gregor      if (Content->Entry) {
114614f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // The source location entry is a file. The blob associated
114714f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // with this entry is the file name.
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11492d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor        // Emit size/modification time for this file.
11502d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor        Record.push_back(Content->Entry->getSize());
11512d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor        Record.push_back(Content->Entry->getModificationTime());
11522d52be56ff595341be3c6cec337af6763804ce66Douglas Gregor
115312fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        // Emit header-search information associated with this file.
115412fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        HeaderFileInfo HFI;
115512fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        HeaderSearch &HS = PP.getHeaderSearchInfo();
115612fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        if (Content->Entry->getUID() < HS.header_file_size())
115712fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor          HFI = HS.header_file_begin()[Content->Entry->getUID()];
115812fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        Record.push_back(HFI.isImport);
115912fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        Record.push_back(HFI.DirInfo);
116012fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        Record.push_back(HFI.NumIncludes);
116112fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor        AddIdentifierRef(HFI.ControllingMacro, Record);
116212fab31aa5868b1a6b52246b5a87daa48a338fe2Douglas Gregor
1163e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        // Turn the file name into an absolute path, if it isn't already.
1164e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        const char *Filename = Content->Entry->getName();
1165e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        llvm::sys::Path FilePath(Filename, strlen(Filename));
116611a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam        FilePath.makeAbsolute();
1167aba54a95e9d5e4dc9056abec6bb70ea777c4a7bcKovarththanan Rajaratnam        Filename = FilePath.c_str();
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1169e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor        Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1170ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar        Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
11717f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
11727f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        // FIXME: For now, preload all file source locations, so that
11737f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        // we get the appropriate File entries in the reader. This is
11747f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        // a temporary measure.
11750fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl        PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
117614f79002e58556798e86168c63e48d533287eda5Douglas Gregor      } else {
117714f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // The source location entry is a buffer. The blob associated
117814f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // with this entry contains the contents of the buffer.
117914f79002e58556798e86168c63e48d533287eda5Douglas Gregor
118014f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // We add one to the size so that we capture the trailing NULL
118114f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // that is required by llvm::MemoryBuffer::getMemBuffer (on
118214f79002e58556798e86168c63e48d533287eda5Douglas Gregor        // the reader side).
118336c35ba0aca641e60e5dbee8efbc620c08b9bd61Douglas Gregor        const llvm::MemoryBuffer *Buffer
1184e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner          = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
118514f79002e58556798e86168c63e48d533287eda5Douglas Gregor        const char *Name = Buffer->getBufferIdentifier();
1186ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar        Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1187ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar                                  llvm::StringRef(Name, strlen(Name) + 1));
118814f79002e58556798e86168c63e48d533287eda5Douglas Gregor        Record.clear();
11898538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Record.push_back(SM_SLOC_BUFFER_BLOB);
1190c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor        Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1191ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar                                  llvm::StringRef(Buffer->getBufferStart(),
1192ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar                                                  Buffer->getBufferSize() + 1));
11937f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
11947f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor        if (strcmp(Name, "<built-in>") == 0)
11950fa7d0b15ea2a224bfe43ac745d411f915da87ddSebastian Redl          PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
119614f79002e58556798e86168c63e48d533287eda5Douglas Gregor      }
119714f79002e58556798e86168c63e48d533287eda5Douglas Gregor    } else {
119814f79002e58556798e86168c63e48d533287eda5Douglas Gregor      // The source location entry is an instantiation.
119914f79002e58556798e86168c63e48d533287eda5Douglas Gregor      const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
120014f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(Inst.getSpellingLoc().getRawEncoding());
120114f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
120214f79002e58556798e86168c63e48d533287eda5Douglas Gregor      Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
120314f79002e58556798e86168c63e48d533287eda5Douglas Gregor
1204f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor      // Compute the token length for this macro expansion.
1205f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor      unsigned NextOffset = SourceMgr.getNextOffset();
1206bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor      if (I + 1 != N)
1207bdfe48ac80573e026595af91e541474dbf02565fDouglas Gregor        NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
1208f60e9918690fcf02974bc1ebecd42c99d561855eDouglas Gregor      Record.push_back(NextOffset - SLoc->getOffset() - 1);
1209c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor      Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
121014f79002e58556798e86168c63e48d533287eda5Douglas Gregor    }
121114f79002e58556798e86168c63e48d533287eda5Douglas Gregor  }
121214f79002e58556798e86168c63e48d533287eda5Douglas Gregor
12137f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Stream.ExitBlock();
1214bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
12157f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  if (SLocEntryOffsets.empty())
12167f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor    return;
12172eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor
12183397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Write the source-location offsets table into the AST block. This
12197f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  // table is used for lazily loading source-location information.
12207f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  using namespace llvm;
12217f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
12228538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
12237f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
12247f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
12257f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
12267f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12287f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Record.clear();
12298538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(SOURCE_LOCATION_OFFSETS);
12307f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Record.push_back(SLocEntryOffsets.size());
12317f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Record.push_back(SourceMgr.getNextOffset());
12327f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1233ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(SLocEntryOffsets),
1234090d9b53e32bb30d9e74de895bb59b409bd49e00Chris Lattner                           SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
12357f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor
12363397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Write the source location entry preloads array, telling the AST
12377f94b0b0c6791013d2f72ced9b4bedd3b23673a6Douglas Gregor  // reader which source locations entries it should load eagerly.
12388538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
123914f79002e58556798e86168c63e48d533287eda5Douglas Gregor}
124014f79002e58556798e86168c63e48d533287eda5Douglas Gregor
12414fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
12424fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Preprocessor Serialization
12434fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
12444fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
12450b1fb988012da21d996c43e36867787a7a07b889Chris Lattner/// \brief Writes the block containing the serialized form of the
12460b1fb988012da21d996c43e36867787a7a07b889Chris Lattner/// preprocessor.
12470b1fb988012da21d996c43e36867787a7a07b889Chris Lattner///
1248a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WritePreprocessor(const Preprocessor &PP) {
12497c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  RecordData Record;
12507c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
1251c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  // If the preprocessor __COUNTER__ value has been bumped, remember it.
1252c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  if (PP.getCounterValue() != 0) {
1253c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner    Record.push_back(PP.getCounterValue());
12548538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(PP_COUNTER_VALUE, Record);
1255c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner    Record.clear();
12562eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  }
12572eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor
12582eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  // Enter the preprocessor block.
12598538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2);
12601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12613397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
12622eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  // FIXME: use diagnostics subsystem for localization etc.
12632eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor  if (PP.SawDateOrTime())
12642eafc1b56347f772729e082e6bac824b0ef1b585Douglas Gregor    fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12667c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  // Loop over all the macro definitions that are live at the end of the file,
12677c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  // emitting each to the PP section.
12686a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
12697c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
12707c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner       I != E; ++I) {
127142d42b5b84603032e57add333b5b44e0ef99bd9eChris Lattner    // FIXME: This emits macros in hash table order, we should do it in a stable
127242d42b5b84603032e57add333b5b44e0ef99bd9eChris Lattner    // order so that output is reproducible.
12737c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    MacroInfo *MI = I->second;
12747c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
12753397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    // Don't emit builtin macros like __LINE__ to the AST file unless they have
12767c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    // been redefined by the header (in which case they are not isBuiltinMacro).
12773397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    // Also skip macros from a AST file if we're chaining.
12783c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl    if (MI->isBuiltinMacro() || (Chain && MI->isFromAST()))
12797c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      continue;
12807c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
12817356a31327be9b3c3434a0c88746028980da5684Chris Lattner    AddIdentifierRef(I->first, Record);
128237e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    MacroOffsets[I->first] = Stream.GetCurrentBitNo();
12837c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    Record.push_back(MI->getDefinitionLoc().getRawEncoding());
12847c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    Record.push_back(MI->isUsed());
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12867c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    unsigned Code;
12877c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    if (MI->isObjectLike()) {
12888538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Code = PP_MACRO_OBJECT_LIKE;
12897c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    } else {
12908538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Code = PP_MACRO_FUNCTION_LIKE;
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12927c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      Record.push_back(MI->isC99Varargs());
12937c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      Record.push_back(MI->isGNUVarargs());
12947c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      Record.push_back(MI->getNumArgs());
12957c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner      for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
12967c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner           I != E; ++I)
12977356a31327be9b3c3434a0c88746028980da5684Chris Lattner        AddIdentifierRef(*I, Record);
12987c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    }
12996a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13006a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    // If we have a detailed preprocessing record, record the macro definition
13016a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    // ID that corresponds to this macro.
13026a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    if (PPRec)
13036a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
13046a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
1305c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    Stream.EmitRecord(Code, Record);
13067c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    Record.clear();
1307f04ad69fed38d26fc0d6f7d6fd0a4631ddfbc7feChris Lattner
1308df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner    // Emit the tokens array.
1309df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner    for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1310df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // Note that we know that the preprocessor does not have any annotation
1311df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // tokens in it because they are created by the parser, and thus can't be
1312df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // in a macro definition.
1313df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      const Token &Tok = MI->getReplacementToken(TokNo);
13141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1315df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getLocation().getRawEncoding());
1316df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getLength());
1317df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner
1318df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // FIXME: When reading literal tokens, reconstruct the literal pointer if
1319df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // it is needed.
13207356a31327be9b3c3434a0c88746028980da5684Chris Lattner      AddIdentifierRef(Tok.getIdentifierInfo(), Record);
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1322df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // FIXME: Should translate token kind to a stable encoding.
1323df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getKind());
1324df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      // FIXME: Should translate token flags to a stable encoding.
1325df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.push_back(Tok.getFlags());
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13278538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      Stream.EmitRecord(PP_TOKEN, Record);
1328df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner      Record.clear();
1329df961c28f84666051ad59d2da1f44023f6366d02Chris Lattner    }
133037e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    ++NumMacros;
13317c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  }
13326a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13336a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  // If the preprocessor has a preprocessing record, emit it.
13346a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  unsigned NumPreprocessingRecords = 0;
13356a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (PPRec) {
13366a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
13376a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor         E != EEnd; ++E) {
13386a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      Record.clear();
13396a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13406a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
13416a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        Record.push_back(NumPreprocessingRecords++);
13426a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddSourceLocation(MI->getSourceRange().getBegin(), Record);
13436a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddSourceLocation(MI->getSourceRange().getEnd(), Record);
13446a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddIdentifierRef(MI->getName(), Record);
13456a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        Record.push_back(getMacroDefinitionID(MI->getDefinition()));
13468538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
13476a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        continue;
13486a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      }
13496a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13506a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
13516a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        // Record this macro definition's location.
13528538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        IdentID ID = getMacroDefinitionID(MD);
13536a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        if (ID != MacroDefinitionOffsets.size()) {
13546a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor          if (ID > MacroDefinitionOffsets.size())
13556a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor            MacroDefinitionOffsets.resize(ID + 1);
13566a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13576a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor          MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
13586a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        } else
13596a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor          MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
13606a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13616a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        Record.push_back(NumPreprocessingRecords++);
13626a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        Record.push_back(ID);
13636a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddSourceLocation(MD->getSourceRange().getBegin(), Record);
13646a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddSourceLocation(MD->getSourceRange().getEnd(), Record);
13656a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddIdentifierRef(MD->getName(), Record);
13666a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        AddSourceLocation(MD->getLocation(), Record);
13678538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
13686a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor        continue;
13696a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor      }
13706a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    }
13716a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  }
13726a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
1373c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.ExitBlock();
13746a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13756a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  // Write the offsets table for the preprocessing record.
13766a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (NumPreprocessingRecords > 0) {
13776a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    // Write the offsets table for identifier IDs.
13786a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    using namespace llvm;
13796a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
13808538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
13816a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
13826a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
13836a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
13846a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
13856a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
13866a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Record.clear();
13878538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(MACRO_DEFINITION_OFFSETS);
13886a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Record.push_back(NumPreprocessingRecords);
13896a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Record.push_back(MacroDefinitionOffsets.size());
13906a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1391ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                              (const char *)data(MacroDefinitionOffsets),
13926a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor                              MacroDefinitionOffsets.size() * sizeof(uint32_t));
13936a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  }
13940b1fb988012da21d996c43e36867787a7a07b889Chris Lattner}
13950b1fb988012da21d996c43e36867787a7a07b889Chris Lattner
13964fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
13974fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Type Serialization
13984fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
13990b1fb988012da21d996c43e36867787a7a07b889Chris Lattner
14003397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the representation of a type to the AST stream.
1401a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteType(QualType T) {
140201b81c4d074bba9c18372d521405dfe32fc4f552Argyrios Kyrtzidis  TypeIdx &Idx = TypeIdxs[T];
1403c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis  if (Idx.getIndex() == 0) // we haven't seen this type before.
1404c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis    Idx = TypeIdx(NextTypeID++);
14051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Record the offset for this type.
1407c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis  unsigned Index = Idx.getIndex() - FirstTypeID;
1408681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  if (TypeOffsets.size() == Index)
1409c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    TypeOffsets.push_back(Stream.GetCurrentBitNo());
1410681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  else if (TypeOffsets.size() < Index) {
1411681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    TypeOffsets.resize(Index + 1);
1412681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    TypeOffsets[Index] = Stream.GetCurrentBitNo();
14132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
14142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
14152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  RecordData Record;
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Emit the type's representation.
14183397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  ASTTypeWriter W(*this, Record);
14190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1420a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (T.hasLocalNonFastQualifiers()) {
1421a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    Qualifiers Qs = T.getLocalQualifiers();
1422a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    AddTypeRef(T.getLocalUnqualifiedType(), Record);
14230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Record.push_back(Qs.getAsOpaqueValue());
14248538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    W.Code = TYPE_EXT_QUAL;
14250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  } else {
14260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    switch (T->getTypeClass()) {
14270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      // For all of the concrete, non-dependent types, call the
14280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      // appropriate visitor function.
14292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define TYPE(Class, Base) \
1430b7166334d897e1e4e6a5b428fe2d0ec752ef187fMike Stump    case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
14312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define ABSTRACT_TYPE(Class, Base)
14322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/TypeNodes.def"
14330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
14342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
14352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
14362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Emit the serialized record.
1437c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.EmitRecord(W.Code, Record);
14380b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor
14390b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor  // Flush any expressions that were written as part of this type.
1440c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  FlushStmts();
14412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
14422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
14434fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
14444fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Declaration Serialization
14454fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
14464fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
14472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \brief Write the block containing all of the declaration IDs
14482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// lexically declared within the given DeclContext.
14492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor///
14502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
14512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// bistream, or 0 if no block was written.
1452a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redluint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
14532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                                 DeclContext *DC) {
145417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  if (DC->decls_empty())
14552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return 0;
14562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1457c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  uint64_t Offset = Stream.GetCurrentBitNo();
14582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  RecordData Record;
14598538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(DECL_CONTEXT_LEXICAL);
14608538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  llvm::SmallVector<DeclID, 64> Decls;
146117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
146217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         D != DEnd; ++D)
1463681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    Decls.push_back(GetDeclRef(*D));
14642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
14652512308525ff328aa992da0b5ee14a488d2ea93aDouglas Gregor  ++NumLexicalDeclContexts;
1466681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
14678538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl     reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(DeclID));
14682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return Offset;
14692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
14702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1471a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteTypeDeclOffsets() {
14721476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  using namespace llvm;
14731476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  RecordData Record;
14741476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
14751476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // Write the type offsets array
14761476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
14778538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
14781476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
14791476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
14801476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
14811476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.clear();
14828538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(TYPE_OFFSET);
14831476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.push_back(TypeOffsets.size());
14841476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1485ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(TypeOffsets),
14861476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl                            TypeOffsets.size() * sizeof(TypeOffsets[0]));
14871476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
14881476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // Write the declaration offsets array
14891476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev = new BitCodeAbbrev();
14908538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
14911476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
14921476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
14931476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
14941476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.clear();
14958538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(DECL_OFFSET);
14961476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Record.push_back(DeclOffsets.size());
14971476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1498ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(DeclOffsets),
14991476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl                            DeclOffsets.size() * sizeof(DeclOffsets[0]));
15001476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl}
15011476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
15024fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
15034fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Global Method Pool and Selector Serialization
15044fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
15054fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
15063251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregornamespace {
1507f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor// Trait used for the on-disk hash table used in the method pool.
15083397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlclass ASTMethodPoolTrait {
1509a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter &Writer;
1510f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1511f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregorpublic:
1512f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  typedef Selector key_type;
1513f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  typedef key_type key_type_ref;
15141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15155d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  struct data_type {
15168538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    SelectorID ID;
15175d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    ObjCMethodList Instance, Factory;
15185d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  };
1519f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  typedef const data_type& data_type_ref;
1520f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
15213397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1523f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  static unsigned ComputeHash(Selector Sel) {
15240eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidis    return serialization::ComputeHash(Sel);
1525f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<unsigned,unsigned>
1528f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1529f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor                      data_type_ref Methods) {
1530f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1531f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, KeyLen);
15325d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
15335d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1534f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1535f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1536f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        DataLen += 4;
15375d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1538f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1539f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1540f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        DataLen += 4;
1541f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, DataLen);
1542f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    return std::make_pair(KeyLen, DataLen);
1543f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
154583941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor  void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
15461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    uint64_t Start = Out.tell();
154783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    assert((Start >> 32) == 0 && "Selector key offset too large");
154883941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Writer.SetSelectorOffset(Sel, Start);
1549f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned N = Sel.getNumArgs();
1550f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, N);
1551f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    if (N == 0)
1552f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      N = 1;
1553f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    for (unsigned I = 0; I != N; ++I)
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      clang::io::Emit32(Out,
1555f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor                    Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1556f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
15571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1558f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  void EmitData(llvm::raw_ostream& Out, key_type_ref,
1559a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor                data_type_ref Methods, unsigned DataLen) {
1560a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor    uint64_t Start = Out.tell(); (void)Start;
15615d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    clang::io::Emit32(Out, Methods.ID);
1562f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned NumInstanceMethods = 0;
15635d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1564f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1565f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1566f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        ++NumInstanceMethods;
1567f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1568f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned NumFactoryMethods = 0;
15695d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1570f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1571f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1572f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        ++NumFactoryMethods;
1573f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1574f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, NumInstanceMethods);
1575f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    clang::io::Emit16(Out, NumFactoryMethods);
15765d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1577f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1578f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1579f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
15805d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1581f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor         Method = Method->Next)
1582f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      if (Method->Method)
1583f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
1584a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor
1585a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor    assert(Out.tell() - Start == DataLen && "Data length is wrong");
1586f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
1587f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor};
1588f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor} // end anonymous namespace
1589f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1590059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl/// \brief Write ObjC data: selectors and the method pool.
1591f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor///
1592f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor/// The method pool contains both instance and factory methods, stored
1593059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl/// in an on-disk hash table indexed by the selector. The hash table also
1594059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl/// contains an empty entry for every other selector known to Sema.
1595a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteSelectors(Sema &SemaRef) {
1596f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  using namespace llvm;
1597f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1598059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl  // Do we have to do anything at all?
15995d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
1600059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl    return;
1601e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  unsigned NumTableEntries = 0;
1602059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl  // Create and write out the blob that contains selectors and the method pool.
1603f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  {
16043397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
16055d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    ASTMethodPoolTrait Trait(*this);
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1607059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl    // Create the on-disk hash table representation. We walk through every
1608059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl    // selector we've seen and look it up in the method pool.
1609e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
16108538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    for (llvm::DenseMap<Selector, SelectorID>::iterator
16115d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl             I = SelectorIDs.begin(), E = SelectorIDs.end();
16125d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl         I != E; ++I) {
16135d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      Selector S = I->first;
1614059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl      Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
16153397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      ASTMethodPoolTrait::data_type Data = {
16165d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        I->second,
16175d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        ObjCMethodList(),
16185d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        ObjCMethodList()
16195d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      };
16205d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      if (F != SemaRef.MethodPool.end()) {
16215d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        Data.Instance = F->second.first;
16225d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl        Data.Factory = F->second.second;
16235d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl      }
16243397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      // Only write this selector if it's not in an existing AST or something
1625e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl      // changed.
1626e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl      if (Chain && I->second < FirstSelectorID) {
1627e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        // Selector already exists. Did it change?
1628e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        bool changed = false;
1629e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1630e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl             M = M->Next) {
1631e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl          if (M->Method->getPCHLevel() == 0)
1632e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl            changed = true;
1633e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        }
1634e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1635e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl             M = M->Next) {
1636e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl          if (M->Method->getPCHLevel() == 0)
1637e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl            changed = true;
1638e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        }
1639e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl        if (!changed)
1640e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl          continue;
1641fa78dec572259aca763457b435744f79d822c5d4Sebastian Redl      } else if (Data.Instance.Method || Data.Factory.Method) {
1642fa78dec572259aca763457b435744f79d822c5d4Sebastian Redl        // A new method pool entry.
1643fa78dec572259aca763457b435744f79d822c5d4Sebastian Redl        ++NumTableEntries;
1644e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl      }
16455d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Generator.insert(S, Data, Trait);
1646f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    }
1647f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1648f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    // Create the on-disk hash table in a buffer.
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SmallString<4096> MethodPool;
1650f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    uint32_t BucketOffset;
1651f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    {
16523397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      ASTMethodPoolTrait Trait(*this);
1653f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      llvm::raw_svector_ostream Out(MethodPool);
1654f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      // Make sure that no bucket is at offset 0
1655a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor      clang::io::Emit32(Out, 0);
1656f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      BucketOffset = Generator.Emit(Out, Trait);
1657f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    }
1658f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
1659f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    // Create a blob abbreviation
1660f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
16618538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
1662f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
166383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1664f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1665f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1666f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
166783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    // Write the method pool
1668f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    RecordData Record;
16698538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(METHOD_POOL);
1670f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor    Record.push_back(BucketOffset);
1671e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    Record.push_back(NumTableEntries);
1672ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar    Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
167383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor
167483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    // Create a blob abbreviation for the selector table offsets.
167583941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev = new BitCodeAbbrev();
16768538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
167783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
167883941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
167983941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
168083941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor
168183941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    // Write the selector offsets table.
168283941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Record.clear();
16838538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(SELECTOR_OFFSETS);
168483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Record.push_back(SelectorOffsets.size());
168583941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor    Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1686ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                              (const char *)data(SelectorOffsets),
168783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor                              SelectorOffsets.size() * 4);
1688f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor  }
1689f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor}
1690f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor
16913397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the selectors referenced in @selector expression into AST file.
1692a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
1693320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  using namespace llvm;
1694320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  if (SemaRef.ReferencedSelectors.empty())
1695320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    return;
1696725cd9686a0f5bb6c994cb3e43f58b63567c6860Sebastian Redl
1697320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  RecordData Record;
1698725cd9686a0f5bb6c994cb3e43f58b63567c6860Sebastian Redl
16993397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Note: this writes out all references even for a dependent AST. But it is
1700a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  // very tricky to fix, and given that @selector shouldn't really appear in
1701a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  // headers, probably not worth it. It's not a correctness issue.
1702320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  for (DenseMap<Selector, SourceLocation>::iterator S =
1703320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian       SemaRef.ReferencedSelectors.begin(),
1704320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian       E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1705320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    Selector Sel = (*S).first;
1706320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    SourceLocation Loc = (*S).second;
1707320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    AddSelectorRef(Sel, Record);
1708320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian    AddSourceLocation(Loc, Record);
1709320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  }
17108538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
1711320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian}
1712320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian
17134fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
17144fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// Identifier Table Serialization
17154fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
17164fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
1717f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregornamespace {
17183397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlclass ASTIdentifierTableTrait {
1719a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  ASTWriter &Writer;
172037e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor  Preprocessor &PP;
17213251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
1722a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  /// \brief Determines whether this is an "interesting" identifier
1723a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  /// that needs a full IdentifierInfo structure written into the hash
1724a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  /// table.
1725a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  static bool isInterestingIdentifier(const IdentifierInfo *II) {
1726a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    return II->isPoisoned() ||
1727a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->isExtensionToken() ||
1728a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->hasMacroDefinition() ||
1729a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->getObjCOrBuiltinID() ||
1730a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      II->getFETokenInfo<void>();
1731a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor  }
1732a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor
17333251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregorpublic:
17343251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  typedef const IdentifierInfo* key_type;
17353251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  typedef key_type  key_type_ref;
17361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17378538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  typedef IdentID data_type;
17383251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  typedef data_type data_type_ref;
17391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17403397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
174137e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    : Writer(Writer), PP(PP) { }
17423251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
17433251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  static unsigned ComputeHash(const IdentifierInfo* II) {
17442596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar    return llvm::HashString(II->getName());
17453251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
17461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<unsigned,unsigned>
17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
17498538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                      IdentID ID) {
1750e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar    unsigned KeyLen = II->getLength() + 1;
1751a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1752a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    if (isInterestingIdentifier(II)) {
17535998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor      DataLen += 2; // 2 bytes for builtin ID, flags
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (II->hasMacroDefinition() &&
1755a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor          !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
17565998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor        DataLen += 4;
1757a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1758a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor                                     DEnd = IdentifierResolver::end();
1759a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor           D != DEnd; ++D)
17608538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl        DataLen += sizeof(DeclID);
1761a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    }
1762668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    clang::io::Emit16(Out, DataLen);
176302fc75169afe785c114e795ec4d24edfb4073c42Douglas Gregor    // We emit the key length after the data length so that every
176402fc75169afe785c114e795ec4d24edfb4073c42Douglas Gregor    // string is preceded by a 16-bit length. This matches the PTH
176502fc75169afe785c114e795ec4d24edfb4073c42Douglas Gregor    // format for storing identifiers.
1766d6595a40fe12e3d5ffe5ce48987b379d547439a4Douglas Gregor    clang::io::Emit16(Out, KeyLen);
17673251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    return std::make_pair(KeyLen, DataLen);
17683251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
17691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
17713251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor               unsigned KeyLen) {
17723251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    // Record the location of the key data.  This is used when generating
17733251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    // the mapping from persistent IDs to strings.
17743251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    Writer.SetIdentifierOffset(II, Out.tell());
1775e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar    Out.write(II->getNameStart(), KeyLen);
17763251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
17771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
17798538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                IdentID ID, unsigned) {
1780a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    if (!isInterestingIdentifier(II)) {
1781a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      clang::io::Emit32(Out, ID << 1);
1782a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor      return;
1783a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    }
17845998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor
1785a92193ebd9840e5ce4de1b09e49f1b024c0f5c2fDouglas Gregor    clang::io::Emit32(Out, (ID << 1) | 0x01);
17863251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    uint32_t Bits = 0;
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    bool hasMacroDefinition =
17881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      II->hasMacroDefinition() &&
178937e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor      !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
17905998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor    Bits = (uint32_t)II->getObjCOrBuiltinID();
1791b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1792b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1793b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(II->isPoisoned());
1794646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
1795b0b84385f0cb0ea4036579f5f384f1c19b917c7eDaniel Dunbar    Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
17965998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor    clang::io::Emit16(Out, Bits);
17973251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
179837e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor    if (hasMacroDefinition)
17995998da5c29dae3f654cb65738ff237203e2222c8Douglas Gregor      clang::io::Emit32(Out, Writer.getMacroOffset(II));
180037e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor
1801668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // Emit the declaration IDs in reverse order, because the
1802668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // IdentifierResolver provides the declarations as they would be
1803668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // visible (e.g., the function "stat" would come before the struct
1804668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1805668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // adds declarations to the end of the list (so we need to see the
1806668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    // struct "status" before the function "status").
1807f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    // Only emit declarations that aren't from a chained PCH, though.
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1809668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor                                        IdentifierResolver::end());
1810668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1811668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor                                                      DEnd = Decls.rend();
18123251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor         D != DEnd; ++D)
1813d8c5abb096a5f6babb3709180fe304be5462bcc1Sebastian Redl      clang::io::Emit32(Out, Writer.getDeclID(*D));
18143251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  }
18153251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor};
18163251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor} // end anonymous namespace
18173251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
18183397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl/// \brief Write the identifier table into the AST file.
1819afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor///
1820afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor/// The identifier table consists of a blob containing string data
1821afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor/// (the actual identifiers themselves) and a separate "offsets" index
1822afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor/// that maps identifier IDs to locations within the blob.
1823a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
1824afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  using namespace llvm;
1825afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
1826afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  // Create and write out the blob that contains the identifier
1827afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  // strings.
1828afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  {
18293397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
18305d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    ASTIdentifierTableTrait Trait(*this, PP);
18311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183292b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // Look for any identifiers that were named while processing the
183392b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // headers, but are otherwise not needed. We add these to the hash
183492b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // table to enable checking of the predefines buffer in the case
18353397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    // where the user adds new macro definitions when building the AST
183692b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    // file.
183792b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor    for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
183892b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor                                IDEnd = PP.getIdentifierTable().end();
183992b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor         ID != IDEnd; ++ID)
184092b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor      getIdentifierRef(ID->second);
184192b059ea944adaa3e00bb53d63a09868a4752547Douglas Gregor
1842f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    // Create the on-disk hash table representation. We only store offsets
1843f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    // for identifiers that appear here for the first time.
1844f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    IdentifierOffsets.resize(NextIdentID - FirstIdentID);
18458538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
1846afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor           ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1847afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor         ID != IDEnd; ++ID) {
1848afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor      assert(ID->first && "NULL identifier in identifier table");
18493c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl      if (!Chain || !ID->first->isFromAST())
18505d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis        Generator.insert(ID->first, ID->second, Trait);
18513251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    }
1852afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
18533251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    // Create the on-disk hash table in a buffer.
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::SmallString<4096> IdentifierTable;
1855668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    uint32_t BucketOffset;
18563251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    {
18573397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      ASTIdentifierTableTrait Trait(*this, PP);
18583251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor      llvm::raw_svector_ostream Out(IdentifierTable);
1859f0aaf7a59729a4ae0146e3464ee987745be95829Douglas Gregor      // Make sure that no bucket is at offset 0
1860a67e58c8fa03c2f1aa7609bf5a436d1adba75eefDouglas Gregor      clang::io::Emit32(Out, 0);
1861668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor      BucketOffset = Generator.Emit(Out, Trait);
1862afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    }
1863afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
1864afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    // Create a blob abbreviation
1865afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
18668538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
1867668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
18683251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1869c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
1870afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
1871afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    // Write the identifier table
1872afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor    RecordData Record;
18738538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Record.push_back(IDENTIFIER_TABLE);
1874668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    Record.push_back(BucketOffset);
1875ec312a1f0557b1d27f3eb6cf49acbf7e72696422Daniel Dunbar    Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
1876afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  }
1877afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
1878afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  // Write the offsets table for identifier IDs.
18792b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
18808538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
18812b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
18822b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
18832b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
18842b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor
18852b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  RecordData Record;
18868538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(IDENTIFIER_OFFSET);
18872b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Record.push_back(IdentifierOffsets.size());
18882b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor  Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1889ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl                            (const char *)data(IdentifierOffsets),
18902b3a5a83ea30bde7fa8f9d8e9a0cb12623759bfbDouglas Gregor                            IdentifierOffsets.size() * sizeof(uint32_t));
1891afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor}
1892afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
18934fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
18945d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis// DeclContext's Name Lookup Table Serialization
18955d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
18965d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
18975d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidisnamespace {
18985d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis// Trait used for the on-disk hash table used in the method pool.
18995d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidisclass ASTDeclContextNameLookupTrait {
19005d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  ASTWriter &Writer;
19015d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19025d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidispublic:
19035d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef DeclarationName key_type;
19045d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef key_type key_type_ref;
19055d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19065d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef DeclContext::lookup_result data_type;
19075d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  typedef const data_type& data_type_ref;
19085d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19095d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
19105d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19115d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  unsigned ComputeHash(DeclarationName Name) {
19125d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    llvm::FoldingSetNodeID ID;
19135d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    ID.AddInteger(Name.getNameKind());
19145d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19155d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    switch (Name.getNameKind()) {
19165d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::Identifier:
19175d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddString(Name.getAsIdentifierInfo()->getName());
19185d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19195d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCZeroArgSelector:
19205d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCOneArgSelector:
19215d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCMultiArgSelector:
19225d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
19235d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19245d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConstructorName:
19255d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXDestructorName:
19265d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConversionFunctionName:
19275d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
19285d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19295d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXOperatorName:
19305d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddInteger(Name.getCXXOverloadedOperator());
19315d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19325d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXLiteralOperatorName:
19335d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      ID.AddString(Name.getCXXLiteralIdentifier()->getName());
19345d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXUsingDirective:
19355d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19365d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    }
19375d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19385d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    return ID.ComputeHash();
19395d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
19405d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19415d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  std::pair<unsigned,unsigned>
19425d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
19435d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis                      data_type_ref Lookup) {
19445d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    unsigned KeyLen = 1;
19455d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    switch (Name.getNameKind()) {
19465d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::Identifier:
19475d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCZeroArgSelector:
19485d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCOneArgSelector:
19495d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCMultiArgSelector:
19505d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConstructorName:
19515d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXDestructorName:
19525d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConversionFunctionName:
19535d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXLiteralOperatorName:
19545d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      KeyLen += 4;
19555d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19565d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXOperatorName:
19575d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      KeyLen += 1;
19585d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19595d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXUsingDirective:
19605d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19615d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    }
19625d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    clang::io::Emit16(Out, KeyLen);
19635d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19645d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    // 2 bytes for num of decls and 4 for each DeclID.
19655d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
19665d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    clang::io::Emit16(Out, DataLen);
19675d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19685d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    return std::make_pair(KeyLen, DataLen);
19695d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
19705d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19715d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
19725d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    using namespace clang::io;
19735d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
19745d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
19755d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    Emit8(Out, Name.getNameKind());
19765d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    switch (Name.getNameKind()) {
19775d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::Identifier:
19785d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
19795d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19805d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCZeroArgSelector:
19815d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCOneArgSelector:
19825d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::ObjCMultiArgSelector:
19835d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
19845d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19855d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConstructorName:
19865d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXDestructorName:
19875d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXConversionFunctionName:
19885d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
19895d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19905d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXOperatorName:
19915d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
19925d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit8(Out, Name.getCXXOverloadedOperator());
19935d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19945d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXLiteralOperatorName:
19955d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
19965d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19975d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    case DeclarationName::CXXUsingDirective:
19985d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      break;
19995d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    }
20005d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
20015d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
20025d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  void EmitData(llvm::raw_ostream& Out, key_type_ref,
20035d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis                data_type Lookup, unsigned DataLen) {
20045d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    uint64_t Start = Out.tell(); (void)Start;
20055d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    clang::io::Emit16(Out, Lookup.second - Lookup.first);
20065d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    for (; Lookup.first != Lookup.second; ++Lookup.first)
20075d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis      clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
20085d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
20095d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis    assert(Out.tell() - Start == DataLen && "Data length is wrong");
20105d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  }
20115d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis};
20125d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis} // end anonymous namespace
20135d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis
2014074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis/// \brief Write the block containing all of the declaration IDs
2015074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis/// visible from the given DeclContext.
2016074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis///
2017074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
20181d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// bitstream, or 0 if no block was written.
2019074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidisuint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2020074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis                                                 DeclContext *DC) {
2021074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DC->getPrimaryContext() != DC)
2022074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2023074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2024074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Since there is no name lookup into functions or methods, don't bother to
2025074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // build a visible-declarations table for these entities.
2026074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DC->isFunctionOrMethod())
2027074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2028074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2029074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // If not in C++, we perform name lookup for the translation unit via the
2030074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // IdentifierInfo chains, don't bother to build a visible-declarations table.
2031074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // FIXME: In C++ we need the visible declarations in order to "see" the
2032074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // friend declarations, is there a way to do this without writing the table ?
2033074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2034074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2035074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2036074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Force the DeclContext to build a its name-lookup table.
2037a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis  if (DC->hasExternalVisibleStorage())
2038a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis    DC->MaterializeVisibleDeclsFromExternalStorage();
2039a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis  else
2040a60786b46eaa4766bb57fb3ca4e0191b3f73e42aArgyrios Kyrtzidis    DC->lookup(DeclarationName());
2041074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2042074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Serialize the contents of the mapping used for lookup. Note that,
2043074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // although we have two very different code paths, the serialized
2044074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // representation is the same for both cases: a declaration name,
2045074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // followed by a size, followed by references to the visible
2046074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // declarations that have that name.
2047074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  uint64_t Offset = Stream.GetCurrentBitNo();
2048074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2049074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  if (!Map || Map->empty())
2050074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    return 0;
2051074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2052074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2053074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  ASTDeclContextNameLookupTrait Trait(*this);
2054074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2055074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Create the on-disk hash table representation.
2056074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2057074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis       D != DEnd; ++D) {
2058074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    DeclarationName Name = D->first;
2059074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    DeclContext::lookup_result Result = D->second.getLookupResult();
2060074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    Generator.insert(Name, Result, Trait);
2061074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  }
2062074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2063074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Create the on-disk hash table in a buffer.
2064074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  llvm::SmallString<4096> LookupTable;
2065074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  uint32_t BucketOffset;
2066074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  {
2067074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    llvm::raw_svector_ostream Out(LookupTable);
2068074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    // Make sure that no bucket is at offset 0
2069074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    clang::io::Emit32(Out, 0);
2070074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis    BucketOffset = Generator.Emit(Out, Trait);
2071074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  }
2072074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2073074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  // Write the lookup table
2074074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  RecordData Record;
2075074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Record.push_back(DECL_CONTEXT_VISIBLE);
2076074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Record.push_back(BucketOffset);
2077074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2078074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis                            LookupTable.str());
2079074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
2080074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2081074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  ++NumVisibleDeclContexts;
2082074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis  return Offset;
2083074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis}
2084074dcc8ef8c5df7a155c85648e8eae786bee6cabArgyrios Kyrtzidis
20851d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// \brief Write an UPDATE_VISIBLE block for the given context.
20861d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl///
20871d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
20881d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// DeclContext in a dependent AST file. As such, they only exist for the TU
20891d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl/// (in C++) and for namespaces.
20901d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redlvoid ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
20911d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  assert((DC->isTranslationUnit() || DC->isNamespace()) &&
20921d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl         "Only TU and namespaces should have visible decl updates.");
20931d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
20941d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Make the context build its lookup table, but don't make it load external
20951d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // decls.
20961d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  DC->lookup(DeclarationName());
20971d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
20981d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
20991d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  if (!Map || Map->empty())
21001d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    return;
21011d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
21021d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
21031d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  ASTDeclContextNameLookupTrait Trait(*this);
21041d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
21051d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Create the hash table.
21061d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
21071d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl       D != DEnd; ++D) {
21081d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    DeclarationName Name = D->first;
21091d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    DeclContext::lookup_result Result = D->second.getLookupResult();
21105967d6228f183a5fa384f2f1918df679ed2d8666Sebastian Redl    // For any name that appears in this table, the results are complete, i.e.
21115967d6228f183a5fa384f2f1918df679ed2d8666Sebastian Redl    // they overwrite results from previous PCHs. Merging is always a mess.
21125967d6228f183a5fa384f2f1918df679ed2d8666Sebastian Redl    Generator.insert(Name, Result, Trait);
21131d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  }
21141d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
21151d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Create the on-disk hash table in a buffer.
21161d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  llvm::SmallString<4096> LookupTable;
21171d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  uint32_t BucketOffset;
21181d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  {
21191d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    llvm::raw_svector_ostream Out(LookupTable);
21201d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    // Make sure that no bucket is at offset 0
21211d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    clang::io::Emit32(Out, 0);
21221d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    BucketOffset = Generator.Emit(Out, Trait);
21231d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  }
21241d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
21251d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Write the lookup table
21261d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  RecordData Record;
21271d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Record.push_back(UPDATE_VISIBLE);
21281d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Record.push_back(getDeclID(cast<Decl>(DC)));
21291d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Record.push_back(BucketOffset);
21301d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
21311d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl}
21321d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
21334153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
21344153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl/// that have new specializations in the current AST file.
21354153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redlvoid ASTWriter::WriteAdditionalTemplateSpecializations() {
21364153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl  RecordData Record;
21374153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl  for (AdditionalTemplateSpecializationsMap::iterator
21384153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl           I = AdditionalTemplateSpecializations.begin(),
21394153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl           E = AdditionalTemplateSpecializations.end();
21404153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl       I != E; ++I) {
21414153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl    Record.clear();
21424153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl    Record.push_back(I->first);
21434153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl    Record.insert(Record.end(), I->second.begin(), I->second.end());
21444153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl    Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
21454153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl  }
21464153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl}
21474153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl
21485d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
21494fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor// General Serialization Routines
21504fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor//===----------------------------------------------------------------------===//
21514fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor
215268a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor/// \brief Write a record containing the given attributes.
2153a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
215468a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  RecordData Record;
2155cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt  for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2156cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    const Attr * A = *i;
2157cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2158cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    AddSourceLocation(A->getLocation(), Record);
2159cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt    Record.push_back(A->isInherited());
216068a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
2161cf807c4dfdb23e8fa3f400e0b24ef5b79db7a530Sean Hunt#include "clang/Serialization/AttrPCHWrite.inc"
21625b160927672440076aa53c31d84149f70fd8d40eFariborz Jahanian
216368a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  }
216468a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
21658538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(DECL_ATTR, Record);
216668a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor}
216768a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
21681d6107c30194f10cb2c634e9d8f8740d660b1644Benjamin Kramervoid ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) {
216968a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  Record.push_back(Str.size());
217068a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor  Record.insert(Record.end(), Str.begin(), Str.end());
217168a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor}
217268a2eb0cc76267ba0615992fb5e0977853c397b2Douglas Gregor
21733251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor/// \brief Note that the identifier II occurs at the given offset
21743251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor/// within the identifier table.
2175a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
21768538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  IdentID ID = IdentifierIDs[II];
21773397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Only store offsets new to this AST file. Other identifier names are looked
2178f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  // up earlier in the chain and thus don't need an offset.
2179f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  if (ID >= FirstIdentID)
2180f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    IdentifierOffsets[ID - FirstIdentID] = Offset;
21813251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor}
21823251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
218383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor/// \brief Note that the selector Sel occurs at the given offset
218483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor/// within the method pool/selector table.
2185a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
218683941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor  unsigned ID = SelectorIDs[Sel];
218783941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor  assert(ID && "Unknown selector");
2188e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  // Don't record offsets for selectors that are also available in a different
2189e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  // file.
2190e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  if (ID < FirstSelectorID)
2191e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    return;
2192e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  SelectorOffsets[ID - FirstSelectorID] = Offset;
219383941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor}
219483941df2745d69c05acee3174c7a265c206f70d9Douglas Gregor
2195a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
2196e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
21978538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
2198e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2199e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
2200e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2201e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    NumVisibleDeclContexts(0) {
220230c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl}
22032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2204a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
220530c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl                         const char *isysroot) {
22062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Emit the file header.
2207c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'C', 8);
2208c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'P', 8);
2209c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'C', 8);
2210c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.Emit((unsigned)'H', 8);
22111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2212b145b1e9de866e79fb386e4a074dc0b41853acf3Chris Lattner  WriteBlockInfoBlock();
22132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
22141dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  if (Chain)
2215a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl    WriteASTChain(SemaRef, StatCalls, isysroot);
22161dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  else
2217a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl    WriteASTCore(SemaRef, StatCalls, isysroot);
22181dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl}
22191dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2220a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
22211dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl                             const char *isysroot) {
22221dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  using namespace llvm;
22231dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
22241dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  ASTContext &Context = SemaRef.Context;
22251dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Preprocessor &PP = SemaRef.PP;
22261dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
22272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // The translation unit is the first declaration we'll emit.
22282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  DeclIDs[Context.getTranslationUnitDecl()] = 1;
2229f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  ++NextDeclID;
223061d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  DeclTypesToEmit.push(Context.getTranslationUnitDecl());
22312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
22322deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  // Make sure that we emit IdentifierInfos (and any attached
22332deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  // declarations) for builtins.
22342deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  {
22352deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    IdentifierTable &Table = PP.getIdentifierTable();
22362deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    llvm::SmallVector<const char *, 32> BuiltinNames;
22372deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
22382deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor                                        Context.getLangOptions().NoBuiltin);
22392deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
22402deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor      getIdentifierRef(&Table.get(BuiltinNames[I]));
22412deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  }
22422deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor
224363d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // Build a record containing all of the tentative definitions in this file, in
2244e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  // TentativeDefinitions order.  Generally, this record will be empty for
224563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // headers.
22464c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  RecordData TentativeDefinitions;
2247e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl  for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2248e9d12b6c50c1e9b05443db099e21026c5991a93bSebastian Redl    AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
224963d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  }
22504c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor
225149b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Build a record containing all of the file scoped decls in this file.
225249b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  RecordData UnusedFileScopedDecls;
225349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
225449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis    AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
22554056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
225672b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  RecordData WeakUndeclaredIdentifiers;
225772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
225872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    WeakUndeclaredIdentifiers.push_back(
225972b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis                                      SemaRef.WeakUndeclaredIdentifiers.size());
226072b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
226172b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis         I = SemaRef.WeakUndeclaredIdentifiers.begin(),
226272b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis         E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
226372b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
226472b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
226572b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
226672b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis      WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
226772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis    }
226872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  }
226911a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
227014c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // Build a record containing all of the locally-scoped external
227114c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // declarations in this header file. Generally, this record will be
227214c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // empty.
227314c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  RecordData LocallyScopedExternalDecls;
22743397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // FIXME: This is filling in the AST file in densemap order which is
227563d65f873fdfcb04b216ea9c648d1df5992aed1cChris Lattner  // nondeterminstic!
22761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
227714c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor         TD = SemaRef.LocallyScopedExternalDecls.begin(),
227814c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor         TDEnd = SemaRef.LocallyScopedExternalDecls.end();
227914c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor       TD != TDEnd; ++TD)
228014c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor    AddDeclRef(TD->second, LocallyScopedExternalDecls);
228114c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor
2282b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  // Build a record containing all of the ext_vector declarations.
2283b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  RecordData ExtVectorDecls;
2284b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2285b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor    AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2286b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor
2287d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Build a record containing all of the VTable uses information.
2288d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  RecordData VTableUses;
2289be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis  if (!SemaRef.VTableUses.empty()) {
2290be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis    VTableUses.push_back(SemaRef.VTableUses.size());
2291be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2292be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2293be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2294be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2295be4ebcdaa03a84d915b37f96d241b74eece67c92Argyrios Kyrtzidis    }
2296d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  }
2297d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
2298d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Build a record containing all of dynamic classes declarations.
2299d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  RecordData DynamicClasses;
2300d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2301d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis    AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2302d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
23030e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // Build a record containing all of pending implicit instantiations.
230462c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  RecordData PendingInstantiations;
23050e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  for (std::deque<Sema::PendingImplicitInstantiation>::iterator
230662c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         I = SemaRef.PendingInstantiations.begin(),
230762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
230862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    AddDeclRef(I->first, PendingInstantiations);
230962c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    AddSourceLocation(I->second, PendingInstantiations);
23100e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  }
23110e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
23120e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis         "There are local ones at end of translation unit!");
23130e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis
231476c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  // Build a record containing some declaration references.
231576c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  RecordData SemaDeclRefs;
231676c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
231776c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
231876c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
231976c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  }
232076c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis
23213397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // Write the remaining AST contents.
2322ad1de006ea080b540e480efc6b86c2e201dbf1ecDouglas Gregor  RecordData Record;
23238538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(AST_BLOCK_ID, 5);
232430c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl  WriteMetadata(Context, isysroot);
23251dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  WriteLanguageOptions(Context.getLangOptions());
2326e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  if (StatCalls && !isysroot)
2327dd41ed59cf7aefabd40bf766d8fcc7ebd759c8e5Douglas Gregor    WriteStatCache(*StatCalls);
2328e650c8c3bca2f58cad8ffa8aab63126d26e890cdDouglas Gregor  WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
2329c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  // Write the record of special types.
2330c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  Record.clear();
23311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2332c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getBuiltinVaListType(), Record);
2333c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCIdType(), Record);
2334c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCSelType(), Record);
2335c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCProtoType(), Record);
2336c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getObjCClassType(), Record);
2337c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getRawCFConstantStringType(), Record);
2338c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2339c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  AddTypeRef(Context.getFILEType(), Record);
2340782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  AddTypeRef(Context.getjmp_bufType(), Record);
2341782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  AddTypeRef(Context.getsigjmp_bufType(), Record);
2342d1571acc700b652a52c766e36a6c688d9bf6f3a1Douglas Gregor  AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2343d1571acc700b652a52c766e36a6c688d9bf6f3a1Douglas Gregor  AddTypeRef(Context.ObjCClassRedefinitionType, Record);
2344adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  AddTypeRef(Context.getRawBlockdescriptorType(), Record);
2345083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
23462bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  AddTypeRef(Context.ObjCSelRedefinitionType, Record);
23472bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  AddTypeRef(Context.getRawNSConstantStringType(), Record);
2348006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis  Record.push_back(Context.isInt128Installed());
23498538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(SPECIAL_TYPES, Record);
23501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2351366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor  // Keep writing types and declarations until all types and
2352366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor  // declarations have been written.
23538538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
235461d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  WriteDeclsBlockAbbrevs();
235561d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  while (!DeclTypesToEmit.empty()) {
235661d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclOrType DOT = DeclTypesToEmit.front();
235761d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclTypesToEmit.pop();
235861d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    if (DOT.isType())
235961d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor      WriteType(DOT.getType());
236061d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    else
236161d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor      WriteDecl(Context, DOT.getDecl());
236261d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  }
236361d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor  Stream.ExitBlock();
236411a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
2365813a97b3eee957eac3ac1fb111b8892fb9afd0d4Douglas Gregor  WritePreprocessor(PP);
2366059612dd3429cef2d61f11950f3d93a40182bf69Sebastian Redl  WriteSelectors(SemaRef);
2367320198303df7c16950d83ae79c3f702b84badcf7Fariborz Jahanian  WriteReferencedSelectorsPool(SemaRef);
236837e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor  WriteIdentifierTable(PP);
23698f5dc7fe4d42cea78fa92d1638f753cf65b54cb5Douglas Gregor
23701476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  WriteTypeDeclOffsets();
2371ad1de006ea080b540e480efc6b86c2e201dbf1ecDouglas Gregor
23724c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  // Write the record containing external, unnamed definitions.
2373fdd0172ca1b3c837f8c2b37d69cc2085234e09faDouglas Gregor  if (!ExternalDefinitions.empty())
23748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
23754c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor
23764c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  // Write the record containing tentative definitions.
23774c0e86b392c5fb0cb771551fc877edb6979be69cDouglas Gregor  if (!TentativeDefinitions.empty())
23788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
237914c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor
238049b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Write the record containing unused file scoped decls.
238149b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  if (!UnusedFileScopedDecls.empty())
23828538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
238311a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam
238472b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  // Write the record containing weak undeclared identifiers.
238572b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis  if (!WeakUndeclaredIdentifiers.empty())
23868538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
238772b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis                      WeakUndeclaredIdentifiers);
238872b90571b1783b17c3f2204cec5ca440edc38beeArgyrios Kyrtzidis
238914c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  // Write the record containing locally-scoped external definitions.
239014c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor  if (!LocallyScopedExternalDecls.empty())
23918538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
239214c22f20c077cecd68581952a0c227f8c180be03Douglas Gregor                      LocallyScopedExternalDecls);
2393b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor
2394b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  // Write the record containing ext_vector type names.
2395b81c17092039f39be60e9656a37cffbdf2e2c783Douglas Gregor  if (!ExtVectorDecls.empty())
23968538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
23971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2398d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Write the record containing VTable uses information.
2399d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  if (!VTableUses.empty())
24008538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(VTABLE_USES, VTableUses);
2401d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
2402d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  // Write the record containing dynamic classes declarations.
2403d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis  if (!DynamicClasses.empty())
24048538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
2405d455add086f1dfa16ae87dc310e49493bbc2b0a6Argyrios Kyrtzidis
24060e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis  // Write the record containing pending implicit instantiations.
240762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  if (!PendingInstantiations.empty())
240862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
24090e0363866792b309d70e9c8e92b4c239773af89cArgyrios Kyrtzidis
241076c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  // Write the record containing declaration references of Sema.
241176c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis  if (!SemaDeclRefs.empty())
24128538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
241376c38d385447b7acdff2d7e6b13fa8580e7174a7Argyrios Kyrtzidis
24143e1af84bb0092a1aafb49deaa4ab6664c9a9071bDouglas Gregor  // Some simple statistics
2415ad1de006ea080b540e480efc6b86c2e201dbf1ecDouglas Gregor  Record.clear();
24163e1af84bb0092a1aafb49deaa4ab6664c9a9071bDouglas Gregor  Record.push_back(NumStatements);
241737e2684abfe38207fdb90620da062bb18c23f29aDouglas Gregor  Record.push_back(NumMacros);
24182512308525ff328aa992da0b5ee14a488d2ea93aDouglas Gregor  Record.push_back(NumLexicalDeclContexts);
24192512308525ff328aa992da0b5ee14a488d2ea93aDouglas Gregor  Record.push_back(NumVisibleDeclContexts);
24208538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(STATISTICS, Record);
2421c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Stream.ExitBlock();
24222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
24232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2424a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
242530c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl                              const char *isysroot) {
24261dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  using namespace llvm;
24271dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2428ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  FirstDeclID += Chain->getTotalNumDecls();
2429ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  FirstTypeID += Chain->getTotalNumTypes();
2430ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  FirstIdentID += Chain->getTotalNumIdentifiers();
2431e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  FirstSelectorID += Chain->getTotalNumSelectors();
2432ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  NextDeclID = FirstDeclID;
2433ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  NextTypeID = FirstTypeID;
2434ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  NextIdentID = FirstIdentID;
2435e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  NextSelectorID = FirstSelectorID;
2436ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl
24371dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  ASTContext &Context = SemaRef.Context;
24381dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Preprocessor &PP = SemaRef.PP;
24391476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl
24401dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  RecordData Record;
24418538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(AST_BLOCK_ID, 5);
244230c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl  WriteMetadata(Context, isysroot);
24431476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  if (StatCalls && !isysroot)
24441476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl    WriteStatCache(*StatCalls);
24451476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // FIXME: Source manager block should only write new stuff, which could be
24461476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  // done by tracking the largest ID in the chain
24471476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
24481dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
24491dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  // The special types are in the chained PCH.
24501dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
24511dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  // We don't start with the translation unit, but with its decls that
24523c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  // don't come from the chained PCH.
24531dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
24548538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  llvm::SmallVector<DeclID, 64> NewGlobalDecls;
2455681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2456681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl                                  E = TU->noload_decls_end();
24571dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl       I != E; ++I) {
2458d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl    if ((*I)->getPCHLevel() == 0)
2459d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl      NewGlobalDecls.push_back(GetDeclRef(*I));
24600b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    else if ((*I)->isChangedSinceDeserialization())
24610b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl      (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
24621dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  }
2463681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  // We also need to write a lexical updates block for the TU.
2464d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
24658538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
2466d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2467d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2468d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  Record.clear();
24698538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Record.push_back(TU_UPDATE_LEXICAL);
2470d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl  Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2471d692af71226d2fb537d86c670af96114ddd485c3Sebastian Redl                          reinterpret_cast<const char*>(NewGlobalDecls.data()),
24728538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                          NewGlobalDecls.size() * sizeof(DeclID));
24731d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // And in C++, a visible updates block for the TU.
24741d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  if (Context.getLangOptions().CPlusPlus) {
24751d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    Abv = new llvm::BitCodeAbbrev();
24761d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
24771d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
24781d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
24791d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
24801d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
24811d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    WriteDeclContextVisibleUpdate(TU);
24821d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  }
24831dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2484083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Build a record containing all of the new tentative definitions in this
2485083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // file, in TentativeDefinitions order.
2486083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  RecordData TentativeDefinitions;
2487083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2488083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl    if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2489083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl      AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2490083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2491083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
249249b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Build a record containing all of the file scoped decls in this file.
249349b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  RecordData UnusedFileScopedDecls;
249449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
249549b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis    if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
249649b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis      AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
2497083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2498083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
24994056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // We write the entire table, overwriting the tables from the chain.
25004056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData WeakUndeclaredIdentifiers;
25014056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
25024056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    WeakUndeclaredIdentifiers.push_back(
25034056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl                                      SemaRef.WeakUndeclaredIdentifiers.size());
25044056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
25054056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl         I = SemaRef.WeakUndeclaredIdentifiers.begin(),
25064056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl         E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
25074056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
25084056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
25094056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
25104056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
25114056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    }
25124056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
25134056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
2514083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Build a record containing all of the locally-scoped external
2515083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // declarations in this header file. Generally, this record will be
2516083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // empty.
2517083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  RecordData LocallyScopedExternalDecls;
25183397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // FIXME: This is filling in the AST file in densemap order which is
2519083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // nondeterminstic!
2520083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2521083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl         TD = SemaRef.LocallyScopedExternalDecls.begin(),
2522083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl         TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2523083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl       TD != TDEnd; ++TD) {
2524083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl    if (TD->second->getPCHLevel() == 0)
2525083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl      AddDeclRef(TD->second, LocallyScopedExternalDecls);
2526083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2527083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
2528083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Build a record containing all of the ext_vector declarations.
2529083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  RecordData ExtVectorDecls;
2530083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2531083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl    if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2532083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl      AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2533083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  }
2534083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
25354056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing all of the VTable uses information.
25364056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // We write everything here, because it's too hard to determine whether
25374056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // a use is new to this part.
25384056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData VTableUses;
25394056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!SemaRef.VTableUses.empty()) {
25404056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    VTableUses.push_back(SemaRef.VTableUses.size());
25414056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
25424056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
25434056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
25444056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
25454056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    }
25464056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
25474056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
25484056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing all of dynamic classes declarations.
25494056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData DynamicClasses;
25504056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
25514056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
25524056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl      AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
25534056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
25544056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing all of pending implicit instantiations.
255562c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  RecordData PendingInstantiations;
25564056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  for (std::deque<Sema::PendingImplicitInstantiation>::iterator
255762c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         I = SemaRef.PendingInstantiations.begin(),
255862c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth         N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
25594056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    if (I->first->getPCHLevel() == 0) {
256062c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      AddDeclRef(I->first, PendingInstantiations);
256162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth      AddSourceLocation(I->second, PendingInstantiations);
25624056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    }
25634056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
25644056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
25654056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl         "There are local ones at end of translation unit!");
25664056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
25674056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Build a record containing some declaration references.
25684056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // It's not worth the effort to avoid duplication here.
25694056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  RecordData SemaDeclRefs;
25704056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
25714056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
25724056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
25734056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  }
25744056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
25758538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
25761dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  WriteDeclsBlockAbbrevs();
25771dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  while (!DeclTypesToEmit.empty()) {
25781dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    DeclOrType DOT = DeclTypesToEmit.front();
25791dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    DeclTypesToEmit.pop();
25801dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    if (DOT.isType())
25811dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl      WriteType(DOT.getType());
25821dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl    else
25831dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl      WriteDecl(Context, DOT.getDecl());
25841dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  }
25851dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Stream.ExitBlock();
25861dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2587083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  WritePreprocessor(PP);
2588a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  WriteSelectors(SemaRef);
2589a68340fd55e177a5849cb3adbf66aedce1f6e91bSebastian Redl  WriteReferencedSelectorsPool(SemaRef);
2590f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  WriteIdentifierTable(PP);
25911476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  WriteTypeDeclOffsets();
2592083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
2593a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  /// Build a record containing first declarations from a chained PCH and the
25943397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  /// most recent declarations in this AST that they point to.
2595a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  RecordData FirstLatestDeclIDs;
2596a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  for (FirstLatestDeclMap::iterator
2597a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis        I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2598a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis    assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2599a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis           "Expected first & second to be in different PCHs");
2600a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis    AddDeclRef(I->first, FirstLatestDeclIDs);
2601a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis    AddDeclRef(I->second, FirstLatestDeclIDs);
2602a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  }
2603a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis  if (!FirstLatestDeclIDs.empty())
26048538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
2605a865005c74019184e04f7fcdd4d61c31c095a4ffArgyrios Kyrtzidis
2606083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing external, unnamed definitions.
2607083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!ExternalDefinitions.empty())
26088538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
2609083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
2610083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing tentative definitions.
2611083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!TentativeDefinitions.empty())
26128538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
2613083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
261449b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  // Write the record containing unused file scoped decls.
261549b96d1a382ae9f31456166f1a734d3f7f30b992Argyrios Kyrtzidis  if (!UnusedFileScopedDecls.empty())
26168538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
2617083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
26184056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing weak undeclared identifiers.
26194056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!WeakUndeclaredIdentifiers.empty())
26208538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
26214056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl                      WeakUndeclaredIdentifiers);
26224056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
2623083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing locally-scoped external definitions.
2624083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!LocallyScopedExternalDecls.empty())
26258538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
2626083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl                      LocallyScopedExternalDecls);
2627083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
2628083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  // Write the record containing ext_vector type names.
2629083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  if (!ExtVectorDecls.empty())
26308538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
2631083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
26324056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing VTable uses information.
26334056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!VTableUses.empty())
26348538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(VTABLE_USES, VTableUses);
26354056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
26364056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing dynamic classes declarations.
26374056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!DynamicClasses.empty())
26388538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
26394056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
26404056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing pending implicit instantiations.
264162c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth  if (!PendingInstantiations.empty())
264262c78d54bee499dd87f768f48b21c9b5ec15e516Chandler Carruth    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
26434056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl
26444056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  // Write the record containing declaration references of Sema.
26454056680e3c154ef84874410eba55323f9b07aad6Sebastian Redl  if (!SemaDeclRefs.empty())
26468538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
2647083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl
26481d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  // Write the updates to C++ namespaces.
26491d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl  for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
26501d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl           I = UpdatedNamespaces.begin(),
26511d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl           E = UpdatedNamespaces.end();
26521d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl         I != E; ++I)
26531d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl    WriteDeclContextVisibleUpdate(*I);
26541d1e42b17da6a53391d50b08068ecde04311e368Sebastian Redl
26554153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl  // Write the updates to C++ template specialization lists.
26564153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl  if (!AdditionalTemplateSpecializations.empty())
26574153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl    WriteAdditionalTemplateSpecializations();
26584153a060f4cd03e9db1349328a158e9d898a2610Sebastian Redl
2659083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.clear();
2660083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumStatements);
2661083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumMacros);
2662083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumLexicalDeclContexts);
2663083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  Record.push_back(NumVisibleDeclContexts);
26640b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  WriteDeclUpdateBlock();
26658538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(STATISTICS, Record);
26661dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl  Stream.ExitBlock();
26671dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl}
26681dc13a15e789a174e5e5855efe27036dd7a9d252Sebastian Redl
2669a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::WriteDeclUpdateBlock() {
26700b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  if (ReplacedDecls.empty())
26710b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    return;
26720b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl
26730b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  RecordData Record;
26748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
26750b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl           I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
26760b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    Record.push_back(I->first);
26770b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    Record.push_back(I->second);
26780b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  }
26798538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  Stream.EmitRecord(DECL_REPLACEMENTS, Record);
26800b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl}
26810b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl
2682a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
26832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(Loc.getRawEncoding());
26842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
26852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2686a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
26876ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  AddSourceLocation(Range.getBegin(), Record);
26886ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  AddSourceLocation(Range.getEnd(), Record);
26896ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner}
26906ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
2691a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
26922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(Value.getBitWidth());
26934ea884b429445aa6f1af5bc6e51d0b65a4043e24Benjamin Kramer  const uint64_t *Words = Value.getRawData();
26944ea884b429445aa6f1af5bc6e51d0b65a4043e24Benjamin Kramer  Record.append(Words, Words + Value.getNumWords());
26952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
26962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2697a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
26980a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor  Record.push_back(Value.isUnsigned());
26990a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor  AddAPInt(Value, Record);
27000a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor}
27010a2b45e5885b6b8477b167042c0f6cd1d99a1f13Douglas Gregor
2702a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
270317fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor  AddAPInt(Value.bitcastToAPInt(), Record);
270417fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor}
270517fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor
2706a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
27072deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  Record.push_back(getIdentifierRef(II));
27082deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor}
27092deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor
27108538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlIdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
27112deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  if (II == 0)
27122deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    return 0;
2713afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor
27148538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  IdentID &ID = IdentifierIDs[II];
2715afaf308b779cd8e8fc8c42601b9f383423c15c2dDouglas Gregor  if (ID == 0)
2716f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    ID = NextIdentID++;
27172deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  return ID;
27182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
27192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
27208538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlIdentID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
27216a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (MD == 0)
27226a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    return 0;
27236a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
27248538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  IdentID &ID = MacroDefinitions[MD];
27256a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  if (ID == 0)
27266a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor    ID = MacroDefinitions.size();
27276a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  return ID;
27286a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor}
27296a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor
2730a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
27315d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  Record.push_back(getSelectorRef(SelRef));
27325d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl}
27335d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl
27348538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlSelectorID ASTWriter::getSelectorRef(Selector Sel) {
27355d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  if (Sel.getAsOpaquePtr() == 0) {
27365d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl    return 0;
273790cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  }
273890cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff
27398538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  SelectorID &SID = SelectorIDs[Sel];
2740e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  if (SID == 0 && Chain) {
2741e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    // This might trigger a ReadSelector callback, which will set the ID for
2742e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    // this selector.
2743e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    Chain->LoadSelector(Sel);
2744e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl  }
274590cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  if (SID == 0) {
2746e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl    SID = NextSelectorID++;
274790cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  }
27485d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  return SID;
274990cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff}
275090cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff
2751a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2752d2598368876cfe40bc8465540033bc5b5e58d8afChris Lattner  AddDeclRef(Temp->getDestructor(), Record);
2753d2598368876cfe40bc8465540033bc5b5e58d8afChris Lattner}
2754d2598368876cfe40bc8465540033bc5b5e58d8afChris Lattner
2755a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
275644f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                           const TemplateArgumentLocInfo &Arg,
275744f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                           RecordData &Record) {
275844f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis  switch (Kind) {
2759833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Expression:
276044f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    AddStmt(Arg.getAsExpr());
2761833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    break;
2762833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Type:
276344f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis    AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
2764833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    break;
2765788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
276617cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    AddSourceRange(Arg.getTemplateQualifierRange(), Record);
276717cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
2768788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    break;
2769833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Null:
2770833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Integral:
2771833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Declaration:
2772833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  case TemplateArgument::Pack:
2773833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    break;
2774833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
2775833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2776833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2777a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
277844f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                                       RecordData &Record) {
277944f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis  AddTemplateArgument(Arg.getArgument(), Record);
278017cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis
278117cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis  if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
278217cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    bool InfoHasSameExpr
278317cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis      = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
278417cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    Record.push_back(InfoHasSameExpr);
278517cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis    if (InfoHasSameExpr)
278617cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis      return; // Avoid storing the same expr twice.
278717cfdeda476aa8899f0ccedd9cb9cdb76e89b6b4Argyrios Kyrtzidis  }
278844f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
278944f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis                             Record);
279044f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis}
279144f8c37e378f716e8cbb600e3800f437cf58f9e5Argyrios Kyrtzidis
2792a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2793a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  if (TInfo == 0) {
2794a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    AddTypeRef(QualType(), Record);
2795a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    return;
2796a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  }
2797a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
2798a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  AddTypeRef(TInfo->getType(), Record);
2799a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  TypeLocWriter TLW(*this, Record);
2800a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
280111a18f115e8974ef24e8d5bb549ed3289871efa4Kovarththanan Rajaratnam    TLW.Visit(TL);
2802a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall}
2803a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
2804a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
28057fb35182f43392cea4517c203bbabb22364a19fcArgyrios Kyrtzidis  Record.push_back(GetOrCreateTypeID(T));
28067fb35182f43392cea4517c203bbabb22364a19fcArgyrios Kyrtzidis}
28077fb35182f43392cea4517c203bbabb22364a19fcArgyrios Kyrtzidis
28087fb35182f43392cea4517c203bbabb22364a19fcArgyrios KyrtzidisTypeID ASTWriter::GetOrCreateTypeID(QualType T) {
2809eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis  return MakeTypeID(T,
2810eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis              std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2811eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis}
28122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28135d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios KyrtzidisTypeID ASTWriter::getTypeID(QualType T) const {
2814eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis  return MakeTypeID(T,
2815eb3f04e60fa17dba27344ed89fd1b9134bb6839cArgyrios Kyrtzidis              std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
281626fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis}
281726fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis
281826fca90786af17f97e1a5ecc310d4bf39b831595Argyrios KyrtzidisTypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
281926fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  if (T.isNull())
282026fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis    return TypeIdx();
282126fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  assert(!T.getLocalFastQualifiers());
282226fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis
282301b81c4d074bba9c18372d521405dfe32fc4f552Argyrios Kyrtzidis  TypeIdx &Idx = TypeIdxs[T];
2824c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis  if (Idx.getIndex() == 0) {
2825366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor    // We haven't seen this type before. Assign it a new ID and put it
28260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // into the queue of types to emit.
2827c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidis    Idx = TypeIdx(NextTypeID++);
282861d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclTypesToEmit.push(T);
2829366809a4e9340b3458b471e2294a75a9f09ea304Douglas Gregor  }
283026fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  return Idx;
283126fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis}
28322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28335d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios KyrtzidisTypeIdx ASTWriter::getTypeIdx(QualType T) const {
283426fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  if (T.isNull())
283526fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis    return TypeIdx();
283626fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis  assert(!T.getLocalFastQualifiers());
283726fca90786af17f97e1a5ecc310d4bf39b831595Argyrios Kyrtzidis
28385d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  TypeIdxMap::const_iterator I = TypeIdxs.find(T);
28395d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  assert(I != TypeIdxs.end() && "Type not emitted!");
28405d26768e2661faa7ce0b55ffff4be8b3969fbbf5Argyrios Kyrtzidis  return I->second;
28412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
28422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2843a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2844681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  Record.push_back(GetDeclRef(D));
2845681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl}
2846681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl
28478538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlDeclID ASTWriter::GetDeclRef(const Decl *D) {
28482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (D == 0) {
2849681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl    return 0;
28502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
28512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28528538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  DeclID &ID = DeclIDs[D];
28531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ID == 0) {
28542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // We haven't seen this declaration before. Give it a new ID and
28552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // enqueue it in the list of declarations to emit.
2856f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl    ID = NextDeclID++;
285761d60ee6aa0a5ded0ddcf48679673b37506a1895Douglas Gregor    DeclTypesToEmit.push(const_cast<Decl *>(D));
28580b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl  } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
28590b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    // We don't add it to the replacement collection here, because we don't
28600b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    // have the offset yet.
28610b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    DeclTypesToEmit.push(const_cast<Decl *>(D));
28620b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    // Reset the flag, so that we don't add this decl multiple times.
28630b17c61e8f143901ce11b4a6e5129ac63aaeee04Sebastian Redl    const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
28642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
28652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2866681d7237e1014bf64dd5ead6bf74ae55cdd19e61Sebastian Redl  return ID;
28672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
28682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28698538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian RedlDeclID ASTWriter::getDeclID(const Decl *D) {
28703251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  if (D == 0)
28713251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    return 0;
28723251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
28733251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
28743251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  return DeclIDs[D];
28753251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor}
28763251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
2877a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2878ea5ce4705df0743093925585d8edc80e0d8fe3ffChris Lattner  // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
28792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Record.push_back(Name.getNameKind());
28802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  switch (Name.getNameKind()) {
28812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::Identifier:
28822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
28832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
28842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::ObjCZeroArgSelector:
28862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::ObjCOneArgSelector:
28872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::ObjCMultiArgSelector:
288890cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff    AddSelectorRef(Name.getObjCSelector(), Record);
28892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
28902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXConstructorName:
28922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXDestructorName:
28932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXConversionFunctionName:
28942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    AddTypeRef(Name.getCXXNameType(), Record);
28952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
28962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
28972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXOperatorName:
28982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    Record.push_back(Name.getCXXOverloadedOperator());
28992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
29002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
29013e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt  case DeclarationName::CXXLiteralOperatorName:
29023e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
29033e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    break;
29043e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt
29052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  case DeclarationName::CXXUsingDirective:
29062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // No extra data to emit
29072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    break;
29082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
29092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
29106ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
2911a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
29126ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner                                       RecordData &Record) {
29136ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  // Nested name specifiers usually aren't too long. I think that 8 would
29146ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  // typically accomodate the vast majority.
29156ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
29166ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
29176ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  // Push each of the NNS's onto a stack for serialization in reverse order.
29186ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  while (NNS) {
29196ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NestedNames.push_back(NNS);
29206ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NNS = NNS->getPrefix();
29216ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  }
29226ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
29236ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  Record.push_back(NestedNames.size());
29246ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  while(!NestedNames.empty()) {
29256ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NNS = NestedNames.pop_back_val();
29266ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
29276ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    Record.push_back(Kind);
29286ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    switch (Kind) {
29296ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::Identifier:
29306ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      AddIdentifierRef(NNS->getAsIdentifier(), Record);
29316ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
29326ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
29336ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::Namespace:
29346ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      AddDeclRef(NNS->getAsNamespace(), Record);
29356ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
29366ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
29376ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::TypeSpec:
29386ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::TypeSpecWithTemplate:
29396ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      AddTypeRef(QualType(NNS->getAsType(), 0), Record);
29406ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
29416ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
29426ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner
29436ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    case NestedNameSpecifier::Global:
29446ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      // Don't need to write an associated value.
29456ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner      break;
29466ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner    }
29476ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner  }
29486ad9ac097918fbdeb443ea7b99d4db9e49b28534Chris Lattner}
294990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
2950a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
295190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  TemplateName::NameKind Kind = Name.getKind();
295290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(Kind);
295390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  switch (Kind) {
295490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::Template:
295590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddDeclRef(Name.getAsTemplateDecl(), Record);
295690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
295790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
295890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::OverloadedTemplate: {
295990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
296090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(OvT->size());
296190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
296290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis           I != E; ++I)
296390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      AddDeclRef(*I, Record);
296490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
296590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
296690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
296790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::QualifiedTemplate: {
296890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
296990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddNestedNameSpecifier(QualT->getQualifier(), Record);
297090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(QualT->hasTemplateKeyword());
297190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddDeclRef(QualT->getTemplateDecl(), Record);
297290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
297390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
297490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
297590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateName::DependentTemplate: {
297690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    DependentTemplateName *DepT = Name.getAsDependentTemplateName();
297790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddNestedNameSpecifier(DepT->getQualifier(), Record);
297890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(DepT->isIdentifier());
297990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    if (DepT->isIdentifier())
298090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      AddIdentifierRef(DepT->getIdentifier(), Record);
298190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    else
298290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      Record.push_back(DepT->getOperator());
298390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
298490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
298590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
298690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
298790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
2988a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
298990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis                                    RecordData &Record) {
299090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  Record.push_back(Arg.getKind());
299190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  switch (Arg.getKind()) {
299290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Null:
299390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
299490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Type:
299590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddTypeRef(Arg.getAsType(), Record);
299690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
299790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Declaration:
299890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddDeclRef(Arg.getAsDecl(), Record);
299990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
300090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Integral:
300190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddAPSInt(*Arg.getAsIntegral(), Record);
300290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddTypeRef(Arg.getIntegralType(), Record);
300390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
300490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Template:
300590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddTemplateName(Arg.getAsTemplate(), Record);
300690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
300790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Expression:
300890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    AddStmt(Arg.getAsExpr());
300990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
301090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  case TemplateArgument::Pack:
301190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    Record.push_back(Arg.pack_size());
301290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
301390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis           I != E; ++I)
301490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis      AddTemplateArgument(*I, Record);
301590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    break;
301690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
301790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
3018dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis
3019dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidisvoid
3020a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
3021dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis                                    RecordData &Record) {
3022dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  assert(TemplateParams && "No TemplateParams!");
3023dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3024dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3025dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3026dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  Record.push_back(TemplateParams->size());
3027dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  for (TemplateParameterList::const_iterator
3028dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis         P = TemplateParams->begin(), PEnd = TemplateParams->end();
3029dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis         P != PEnd; ++P)
3030dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis    AddDeclRef(*P, Record);
3031dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis}
3032dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis
3033dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis/// \brief Emit a template argument list.
3034dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidisvoid
3035a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
3036dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis                                   RecordData &Record) {
3037dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  assert(TemplateArgs && "No TemplateArgs!");
3038dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  Record.push_back(TemplateArgs->flat_size());
3039dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis  for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3040dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis    AddTemplateArgument(TemplateArgs->get(i), Record);
3041dd41c14bfd7686b556de2acf6952e21a4f80b7aaArgyrios Kyrtzidis}
304237ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis
304337ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis
304437ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidisvoid
3045a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian RedlASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
304637ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  Record.push_back(Set.size());
304737ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  for (UnresolvedSetImpl::const_iterator
304837ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis         I = Set.begin(), E = Set.end(); I != E; ++I) {
304937ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis    AddDeclRef(I.getDecl(), Record);
305037ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis    Record.push_back(I.getAccess());
305137ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  }
305237ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis}
30530745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis
3054a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
30550745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis                                    RecordData &Record) {
30560745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  Record.push_back(Base.isVirtual());
30570745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  Record.push_back(Base.isBaseOfClass());
30580745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  Record.push_back(Base.getAccessSpecifierAsWritten());
30595606220447c7901ba8d80147ddab893bb7949dd5Nick Lewycky  AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
30600745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis  AddSourceRange(Base.getSourceRange(), Record);
30610745d0a648b75bd304045309276c70a755adaafbArgyrios Kyrtzidis}
306230c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl
3063a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redlvoid ASTWriter::AddCXXBaseOrMemberInitializers(
30648e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis                        const CXXBaseOrMemberInitializer * const *BaseOrMembers,
30658e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis                        unsigned NumBaseOrMembers, RecordData &Record) {
30668e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis  Record.push_back(NumBaseOrMembers);
30678e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis  for (unsigned i=0; i != NumBaseOrMembers; ++i) {
30688e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
30698e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis
30708e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    Record.push_back(Init->isBaseInitializer());
30718e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    if (Init->isBaseInitializer()) {
30728e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
30738e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      Record.push_back(Init->isBaseVirtual());
30748e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    } else {
30758e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      AddDeclRef(Init->getMember(), Record);
30768e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    }
30778e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddSourceLocation(Init->getMemberLocation(), Record);
30788e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddStmt(Init->getInit());
30798e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddDeclRef(Init->getAnonUnionMember(), Record);
30808e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddSourceLocation(Init->getLParenLoc(), Record);
30818e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    AddSourceLocation(Init->getRParenLoc(), Record);
30828e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    Record.push_back(Init->isWritten());
30838e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    if (Init->isWritten()) {
30848e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      Record.push_back(Init->getSourceOrder());
30858e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    } else {
30868e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      Record.push_back(Init->getNumArrayIndices());
30878e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis      for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
30888e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis        AddDeclRef(Init->getArrayIndex(i), Record);
30898e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis    }
30908e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis  }
30918e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis}
30928e706f4b8da141612861e127610141b8d17a9667Argyrios Kyrtzidis
3093c43b54cbc10654ed59de797898042e1a05265246Sebastian Redlvoid ASTWriter::SetReader(ASTReader *Reader) {
3094ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  assert(Reader && "Cannot remove chain");
3095ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  assert(FirstDeclID == NextDeclID &&
3096ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl         FirstTypeID == NextTypeID &&
3097ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl         FirstIdentID == NextIdentID &&
3098e58aa890e8de55bb3146e6ea9fbbba3a58ce30c6Sebastian Redl         FirstSelectorID == NextSelectorID &&
3099ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl         "Setting chain after writing has started.");
3100ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  Chain = Reader;
3101ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl}
3102ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl
31038538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
3104f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl  IdentifierIDs[II] = ID;
3105f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl}
3106f2f0f03d08c6143137a79a8edffc7d41823bc3c7Sebastian Redl
3107c8e5d51f51e46c6f7717761537c6609ef9daf57cArgyrios Kyrtzidisvoid ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
310801b81c4d074bba9c18372d521405dfe32fc4f552Argyrios Kyrtzidis  TypeIdxs[T] = Idx;
310930c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl}
311030c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl
31118538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid ASTWriter::DeclRead(DeclID ID, const Decl *D) {
31121476ed40ef6ef144937821da888c7e4d9ea0acd7Sebastian Redl  DeclIDs[D] = ID;
311330c514c225342844700ed4640ec6d90ddf0e12b2Sebastian Redl}
31145d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl
31158538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid ASTWriter::SelectorRead(SelectorID ID, Selector S) {
31165d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl  SelectorIDs[S] = ID;
31175d05007b7a7883159154e3f65f338a2542d53913Sebastian Redl}
3118