ASTWriter.cpp revision 20249a1af2e462dcafdd6a350f1c7967b264ff25
1f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
2f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//
3f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//                     The LLVM Compiler Infrastructure
4f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//
5f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org// This file is distributed under the University of Illinois Open Source
6f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org// License. See LICENSE.TXT for details.
7f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//
8f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//===----------------------------------------------------------------------===//
9f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//
10f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//  This file defines the ASTWriter class, which writes AST files.
11f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//
12f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//===----------------------------------------------------------------------===//
13f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
14f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Serialization/ASTWriter.h"
15f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "ASTCommon.h"
16f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Sema/Sema.h"
17f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Sema/IdentifierResolver.h"
18f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/ASTContext.h"
19f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/Decl.h"
20f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/DeclContextInternals.h"
21f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/DeclTemplate.h"
22f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/Expr.h"
23f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/ExprCXX.h"
24f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/Type.h"
25f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/TypeLocVisitor.h"
26f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Serialization/ASTReader.h"
27f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Lex/MacroInfo.h"
28f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Lex/PreprocessingRecord.h"
29f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Lex/Preprocessor.h"
30f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Lex/HeaderSearch.h"
31f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Basic/FileManager.h"
32f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Basic/OnDiskHashTable.h"
33f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Basic/SourceManager.h"
34f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Basic/SourceManagerInternals.h"
35f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Basic/TargetInfo.h"
36f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/Basic/Version.h"
37f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "llvm/ADT/APFloat.h"
38f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "llvm/ADT/APInt.h"
39f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "llvm/ADT/StringExtras.h"
40f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "llvm/Bitcode/BitstreamWriter.h"
41f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "llvm/Support/MemoryBuffer.h"
42f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "llvm/System/Path.h"
43f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include <cstdio>
44f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgusing namespace clang;
45f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgusing namespace clang::serialization;
46f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
47f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgtemplate <typename T, typename Allocator>
48f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgT *data(std::vector<T, Allocator> &v) {
49f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  return v.empty() ? 0 : &v.front();
50f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
51f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgtemplate <typename T, typename Allocator>
52f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgconst T *data(const std::vector<T, Allocator> &v) {
53f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  return v.empty() ? 0 : &v.front();
54f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
55f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
56f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//===----------------------------------------------------------------------===//
57f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org// Type serialization
58f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org//===----------------------------------------------------------------------===//
59f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
60f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgnamespace {
61f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  class ASTTypeWriter {
62f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    ASTWriter &Writer;
63f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    ASTWriter::RecordData &Record;
64f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
65f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  public:
66f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    /// \brief Type code that corresponds to the record generated.
67f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    TypeCode Code;
68f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
69f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
70f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org      : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
71f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
72f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    void VisitArrayType(const ArrayType *T);
73f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    void VisitFunctionType(const FunctionType *T);
74f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    void VisitTagType(const TagType *T);
75f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
76f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
77f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#define ABSTRACT_TYPE(Class, Base)
78f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/TypeNodes.def"
79f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  };
80f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
81f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
82f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
83f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  assert(false && "Built-in types are never serialized");
84f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
85f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
86f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitComplexType(const ComplexType *T) {
87f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getElementType(), Record);
88f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_COMPLEX;
89f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
90f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
91f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitPointerType(const PointerType *T) {
92f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getPointeeType(), Record);
93f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_POINTER;
94f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
95f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
96f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
97f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getPointeeType(), Record);
98f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_BLOCK_POINTER;
99f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
100f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
101f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
102f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getPointeeType(), Record);
103f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_LVALUE_REFERENCE;
104f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
105f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
106f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
107f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getPointeeType(), Record);
108f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_RVALUE_REFERENCE;
109f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
110f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
111f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
112f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getPointeeType(), Record);
113f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
114f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_MEMBER_POINTER;
115f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
116f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
117f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitArrayType(const ArrayType *T) {
118f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getElementType(), Record);
119f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getSizeModifier()); // FIXME: stable values
120f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
121f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
122f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
123f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
124f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitArrayType(T);
125f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddAPInt(T->getSize(), Record);
126f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_CONSTANT_ARRAY;
127f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
128f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
129f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
130f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitArrayType(T);
131f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_INCOMPLETE_ARRAY;
132f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
133f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
134f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
135f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitArrayType(T);
136f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddSourceLocation(T->getLBracketLoc(), Record);
137f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddSourceLocation(T->getRBracketLoc(), Record);
138f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddStmt(T->getSizeExpr());
139f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_VARIABLE_ARRAY;
140f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
141f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
142f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitVectorType(const VectorType *T) {
143f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getElementType(), Record);
144f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getNumElements());
145f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getAltiVecSpecific());
146f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_VECTOR;
147f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
148f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
149f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
150f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitVectorType(T);
151f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_EXT_VECTOR;
152f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
153f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
154f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
155f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getResultType(), Record);
156f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  FunctionType::ExtInfo C = T->getExtInfo();
157f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(C.getNoReturn());
158f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(C.getRegParm());
159f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  // FIXME: need to stabilize encoding of calling convention...
160f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(C.getCC());
161f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
162f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
163f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
164f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitFunctionType(T);
165f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_FUNCTION_NO_PROTO;
166f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
167f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
168f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
169f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitFunctionType(T);
170f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getNumArgs());
171f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
172f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Writer.AddTypeRef(T->getArgType(I), Record);
173f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->isVariadic());
174f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getTypeQuals());
175f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->hasExceptionSpec());
176f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->hasAnyExceptionSpec());
177f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getNumExceptions());
178f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
179f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Writer.AddTypeRef(T->getExceptionType(I), Record);
180f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_FUNCTION_PROTO;
181f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
182f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
183f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
184f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddDeclRef(T->getDecl(), Record);
185f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_UNRESOLVED_USING;
186f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
187f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
188f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
189f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddDeclRef(T->getDecl(), Record);
190f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
191f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
192f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_TYPEDEF;
193f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
194f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
195f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
196f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddStmt(T->getUnderlyingExpr());
197f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_TYPEOF_EXPR;
198f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
199f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
200f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
201f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getUnderlyingType(), Record);
202f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_TYPEOF;
203f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
204f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
205f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
206f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddStmt(T->getUnderlyingExpr());
207f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_DECLTYPE;
208f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
209f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
210f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitTagType(const TagType *T) {
211f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->isDependentType());
212f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddDeclRef(T->getDecl(), Record);
213f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  assert(!T->isBeingDefined() &&
214f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org         "Cannot serialize in the middle of a type definition");
215f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
216f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
217f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitRecordType(const RecordType *T) {
218f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitTagType(T);
219f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_RECORD;
220f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
221f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
222f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitEnumType(const EnumType *T) {
223f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitTagType(T);
224f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_ENUM;
225f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
226f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
227f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
228f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitSubstTemplateTypeParmType(
229f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                                        const SubstTemplateTypeParmType *T) {
230f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
231f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getReplacementType(), Record);
232f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
233f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
234f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
235f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
236f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitTemplateSpecializationType(
237f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                                       const TemplateSpecializationType *T) {
238f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->isDependentType());
239f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTemplateName(T->getTemplateName(), Record);
240f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getNumArgs());
241f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
242f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org         ArgI != ArgE; ++ArgI)
243f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Writer.AddTemplateArgument(*ArgI, Record);
244f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
245f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                                                : T->getCanonicalTypeInternal(),
246f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                    Record);
247f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_TEMPLATE_SPECIALIZATION;
248f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
249f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
250f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
251f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
252f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  VisitArrayType(T);
253f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddStmt(T->getSizeExpr());
254f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddSourceRange(T->getBracketsRange(), Record);
255f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_DEPENDENT_SIZED_ARRAY;
256f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
257f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
258f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
259f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitDependentSizedExtVectorType(
260f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                                        const DependentSizedExtVectorType *T) {
261f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  // FIXME: Serialize this type (C++ only)
262f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  assert(false && "Cannot serialize dependent sized extended vector types");
263f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
264f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
265f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
266f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
267f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getDepth());
268f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getIndex());
269f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->isParameterPack());
270f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddIdentifierRef(T->getName(), Record);
271f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_TEMPLATE_TYPE_PARM;
272f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
273f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
274f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
275f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
276f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getKeyword());
277f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
278f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddIdentifierRef(T->getIdentifier(), Record);
279f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
280f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                                                : T->getCanonicalTypeInternal(),
281f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                    Record);
282f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_DEPENDENT_NAME;
283f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
284f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
285f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
286f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitDependentTemplateSpecializationType(
287f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org                                const DependentTemplateSpecializationType *T) {
288f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getKeyword());
289f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
290f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddIdentifierRef(T->getIdentifier(), Record);
291f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getNumArgs());
292f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  for (DependentTemplateSpecializationType::iterator
293f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org         I = T->begin(), E = T->end(); I != E; ++I)
294f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Writer.AddTemplateArgument(*I, Record);
295f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
296f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
297f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
298f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
299f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getKeyword());
300f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
301f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getNamedType(), Record);
302f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_ELABORATED;
303f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
304f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
305f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
306f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddDeclRef(T->getDecl(), Record);
307f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
308f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_INJECTED_CLASS_NAME;
309f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
310f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
311f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
312f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddDeclRef(T->getDecl(), Record);
313f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_OBJC_INTERFACE;
314f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
315f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
316f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
317f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getBaseType(), Record);
318f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Record.push_back(T->getNumProtocols());
319f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  for (ObjCObjectType::qual_iterator I = T->qual_begin(),
320f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org       E = T->qual_end(); I != E; ++I)
321f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Writer.AddDeclRef(*I, Record);
322f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_OBJC_OBJECT;
323f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
324f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
325f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid
326f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
327f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddTypeRef(T->getPointeeType(), Record);
328f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Code = TYPE_OBJC_OBJECT_POINTER;
329f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
330f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
331f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgnamespace {
332f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
333f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgclass TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
334f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  ASTWriter &Writer;
335f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  ASTWriter::RecordData &Record;
336f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
337f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgpublic:
338f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
339f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    : Writer(Writer), Record(Record) { }
340f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
341f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#define ABSTRACT_TYPELOC(CLASS, PARENT)
342f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#define TYPELOC(CLASS, PARENT) \
343f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
344f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org#include "clang/AST/TypeLocNodes.def"
345f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
346f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
347f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
348f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org};
349f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
350f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
351f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org
352f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
353f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  // nothing to do
354f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
355f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
356f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
357f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  if (TL.needsExtraLocalData()) {
358f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Record.push_back(TL.getWrittenTypeSpec());
359f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Record.push_back(TL.getWrittenSignSpec());
360f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Record.push_back(TL.getWrittenWidthSpec());
361f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org    Record.push_back(TL.hasModeAttr());
362f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  }
363f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
364f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
365f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddSourceLocation(TL.getNameLoc(), Record);
366f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org}
367f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.orgvoid TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
368f0c4f33a4aa0760ba0e12a254b69d996442c9c5hbono@chromium.org  Writer.AddSourceLocation(TL.getStarLoc(), Record);
369}
370void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
371  Writer.AddSourceLocation(TL.getCaretLoc(), Record);
372}
373void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
374  Writer.AddSourceLocation(TL.getAmpLoc(), Record);
375}
376void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
377  Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
378}
379void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
380  Writer.AddSourceLocation(TL.getStarLoc(), Record);
381}
382void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
383  Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
384  Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
385  Record.push_back(TL.getSizeExpr() ? 1 : 0);
386  if (TL.getSizeExpr())
387    Writer.AddStmt(TL.getSizeExpr());
388}
389void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
390  VisitArrayTypeLoc(TL);
391}
392void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
393  VisitArrayTypeLoc(TL);
394}
395void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
396  VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
399                                            DependentSizedArrayTypeLoc TL) {
400  VisitArrayTypeLoc(TL);
401}
402void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
403                                        DependentSizedExtVectorTypeLoc TL) {
404  Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
406void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
407  Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
410  Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
413  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
414  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
415  Record.push_back(TL.getTrailingReturn());
416  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
417    Writer.AddDeclRef(TL.getArg(i), Record);
418}
419void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
420  VisitFunctionTypeLoc(TL);
421}
422void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
423  VisitFunctionTypeLoc(TL);
424}
425void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
426  Writer.AddSourceLocation(TL.getNameLoc(), Record);
427}
428void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
429  Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
431void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
432  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
433  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
434  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
435}
436void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
437  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
438  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
439  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
440  Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
441}
442void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
443  Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
446  Writer.AddSourceLocation(TL.getNameLoc(), Record);
447}
448void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
449  Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
451void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
452  Writer.AddSourceLocation(TL.getNameLoc(), Record);
453}
454void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
455                                            SubstTemplateTypeParmTypeLoc TL) {
456  Writer.AddSourceLocation(TL.getNameLoc(), Record);
457}
458void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
459                                           TemplateSpecializationTypeLoc TL) {
460  Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
461  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
462  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
463  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
464    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
465                                      TL.getArgLoc(i).getLocInfo(), Record);
466}
467void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
468  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
469  Writer.AddSourceRange(TL.getQualifierRange(), Record);
470}
471void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
472  Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
474void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
475  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
476  Writer.AddSourceRange(TL.getQualifierRange(), Record);
477  Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
479void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
480       DependentTemplateSpecializationTypeLoc TL) {
481  Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
482  Writer.AddSourceRange(TL.getQualifierRange(), Record);
483  Writer.AddSourceLocation(TL.getNameLoc(), Record);
484  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
485  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
486  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
487    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
488                                      TL.getArgLoc(I).getLocInfo(), Record);
489}
490void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
491  Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
494  Record.push_back(TL.hasBaseTypeAsWritten());
495  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
496  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
497  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
498    Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
499}
500void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
501  Writer.AddSourceLocation(TL.getStarLoc(), Record);
502}
503
504//===----------------------------------------------------------------------===//
505// ASTWriter Implementation
506//===----------------------------------------------------------------------===//
507
508static void EmitBlockID(unsigned ID, const char *Name,
509                        llvm::BitstreamWriter &Stream,
510                        ASTWriter::RecordData &Record) {
511  Record.clear();
512  Record.push_back(ID);
513  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
514
515  // Emit the block name if present.
516  if (Name == 0 || Name[0] == 0) return;
517  Record.clear();
518  while (*Name)
519    Record.push_back(*Name++);
520  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
521}
522
523static void EmitRecordID(unsigned ID, const char *Name,
524                         llvm::BitstreamWriter &Stream,
525                         ASTWriter::RecordData &Record) {
526  Record.clear();
527  Record.push_back(ID);
528  while (*Name)
529    Record.push_back(*Name++);
530  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
531}
532
533static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
534                          ASTWriter::RecordData &Record) {
535#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
536  RECORD(STMT_STOP);
537  RECORD(STMT_NULL_PTR);
538  RECORD(STMT_NULL);
539  RECORD(STMT_COMPOUND);
540  RECORD(STMT_CASE);
541  RECORD(STMT_DEFAULT);
542  RECORD(STMT_LABEL);
543  RECORD(STMT_IF);
544  RECORD(STMT_SWITCH);
545  RECORD(STMT_WHILE);
546  RECORD(STMT_DO);
547  RECORD(STMT_FOR);
548  RECORD(STMT_GOTO);
549  RECORD(STMT_INDIRECT_GOTO);
550  RECORD(STMT_CONTINUE);
551  RECORD(STMT_BREAK);
552  RECORD(STMT_RETURN);
553  RECORD(STMT_DECL);
554  RECORD(STMT_ASM);
555  RECORD(EXPR_PREDEFINED);
556  RECORD(EXPR_DECL_REF);
557  RECORD(EXPR_INTEGER_LITERAL);
558  RECORD(EXPR_FLOATING_LITERAL);
559  RECORD(EXPR_IMAGINARY_LITERAL);
560  RECORD(EXPR_STRING_LITERAL);
561  RECORD(EXPR_CHARACTER_LITERAL);
562  RECORD(EXPR_PAREN);
563  RECORD(EXPR_UNARY_OPERATOR);
564  RECORD(EXPR_SIZEOF_ALIGN_OF);
565  RECORD(EXPR_ARRAY_SUBSCRIPT);
566  RECORD(EXPR_CALL);
567  RECORD(EXPR_MEMBER);
568  RECORD(EXPR_BINARY_OPERATOR);
569  RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
570  RECORD(EXPR_CONDITIONAL_OPERATOR);
571  RECORD(EXPR_IMPLICIT_CAST);
572  RECORD(EXPR_CSTYLE_CAST);
573  RECORD(EXPR_COMPOUND_LITERAL);
574  RECORD(EXPR_EXT_VECTOR_ELEMENT);
575  RECORD(EXPR_INIT_LIST);
576  RECORD(EXPR_DESIGNATED_INIT);
577  RECORD(EXPR_IMPLICIT_VALUE_INIT);
578  RECORD(EXPR_VA_ARG);
579  RECORD(EXPR_ADDR_LABEL);
580  RECORD(EXPR_STMT);
581  RECORD(EXPR_TYPES_COMPATIBLE);
582  RECORD(EXPR_CHOOSE);
583  RECORD(EXPR_GNU_NULL);
584  RECORD(EXPR_SHUFFLE_VECTOR);
585  RECORD(EXPR_BLOCK);
586  RECORD(EXPR_BLOCK_DECL_REF);
587  RECORD(EXPR_OBJC_STRING_LITERAL);
588  RECORD(EXPR_OBJC_ENCODE);
589  RECORD(EXPR_OBJC_SELECTOR_EXPR);
590  RECORD(EXPR_OBJC_PROTOCOL_EXPR);
591  RECORD(EXPR_OBJC_IVAR_REF_EXPR);
592  RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
593  RECORD(EXPR_OBJC_KVC_REF_EXPR);
594  RECORD(EXPR_OBJC_MESSAGE_EXPR);
595  RECORD(STMT_OBJC_FOR_COLLECTION);
596  RECORD(STMT_OBJC_CATCH);
597  RECORD(STMT_OBJC_FINALLY);
598  RECORD(STMT_OBJC_AT_TRY);
599  RECORD(STMT_OBJC_AT_SYNCHRONIZED);
600  RECORD(STMT_OBJC_AT_THROW);
601  RECORD(EXPR_CXX_OPERATOR_CALL);
602  RECORD(EXPR_CXX_CONSTRUCT);
603  RECORD(EXPR_CXX_STATIC_CAST);
604  RECORD(EXPR_CXX_DYNAMIC_CAST);
605  RECORD(EXPR_CXX_REINTERPRET_CAST);
606  RECORD(EXPR_CXX_CONST_CAST);
607  RECORD(EXPR_CXX_FUNCTIONAL_CAST);
608  RECORD(EXPR_CXX_BOOL_LITERAL);
609  RECORD(EXPR_CXX_NULL_PTR_LITERAL);
610#undef RECORD
611}
612
613void ASTWriter::WriteBlockInfoBlock() {
614  RecordData Record;
615  Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
616
617#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
618#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
619
620  // AST Top-Level Block.
621  BLOCK(AST_BLOCK);
622  RECORD(ORIGINAL_FILE_NAME);
623  RECORD(TYPE_OFFSET);
624  RECORD(DECL_OFFSET);
625  RECORD(LANGUAGE_OPTIONS);
626  RECORD(METADATA);
627  RECORD(IDENTIFIER_OFFSET);
628  RECORD(IDENTIFIER_TABLE);
629  RECORD(EXTERNAL_DEFINITIONS);
630  RECORD(SPECIAL_TYPES);
631  RECORD(STATISTICS);
632  RECORD(TENTATIVE_DEFINITIONS);
633  RECORD(UNUSED_FILESCOPED_DECLS);
634  RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
635  RECORD(SELECTOR_OFFSETS);
636  RECORD(METHOD_POOL);
637  RECORD(PP_COUNTER_VALUE);
638  RECORD(SOURCE_LOCATION_OFFSETS);
639  RECORD(SOURCE_LOCATION_PRELOADS);
640  RECORD(STAT_CACHE);
641  RECORD(EXT_VECTOR_DECLS);
642  RECORD(VERSION_CONTROL_BRANCH_REVISION);
643  RECORD(MACRO_DEFINITION_OFFSETS);
644  RECORD(CHAINED_METADATA);
645  RECORD(REFERENCED_SELECTOR_POOL);
646
647  // SourceManager Block.
648  BLOCK(SOURCE_MANAGER_BLOCK);
649  RECORD(SM_SLOC_FILE_ENTRY);
650  RECORD(SM_SLOC_BUFFER_ENTRY);
651  RECORD(SM_SLOC_BUFFER_BLOB);
652  RECORD(SM_SLOC_INSTANTIATION_ENTRY);
653  RECORD(SM_LINE_TABLE);
654
655  // Preprocessor Block.
656  BLOCK(PREPROCESSOR_BLOCK);
657  RECORD(PP_MACRO_OBJECT_LIKE);
658  RECORD(PP_MACRO_FUNCTION_LIKE);
659  RECORD(PP_TOKEN);
660  RECORD(PP_MACRO_INSTANTIATION);
661  RECORD(PP_MACRO_DEFINITION);
662
663  // Decls and Types block.
664  BLOCK(DECLTYPES_BLOCK);
665  RECORD(TYPE_EXT_QUAL);
666  RECORD(TYPE_COMPLEX);
667  RECORD(TYPE_POINTER);
668  RECORD(TYPE_BLOCK_POINTER);
669  RECORD(TYPE_LVALUE_REFERENCE);
670  RECORD(TYPE_RVALUE_REFERENCE);
671  RECORD(TYPE_MEMBER_POINTER);
672  RECORD(TYPE_CONSTANT_ARRAY);
673  RECORD(TYPE_INCOMPLETE_ARRAY);
674  RECORD(TYPE_VARIABLE_ARRAY);
675  RECORD(TYPE_VECTOR);
676  RECORD(TYPE_EXT_VECTOR);
677  RECORD(TYPE_FUNCTION_PROTO);
678  RECORD(TYPE_FUNCTION_NO_PROTO);
679  RECORD(TYPE_TYPEDEF);
680  RECORD(TYPE_TYPEOF_EXPR);
681  RECORD(TYPE_TYPEOF);
682  RECORD(TYPE_RECORD);
683  RECORD(TYPE_ENUM);
684  RECORD(TYPE_OBJC_INTERFACE);
685  RECORD(TYPE_OBJC_OBJECT);
686  RECORD(TYPE_OBJC_OBJECT_POINTER);
687  RECORD(DECL_TRANSLATION_UNIT);
688  RECORD(DECL_TYPEDEF);
689  RECORD(DECL_ENUM);
690  RECORD(DECL_RECORD);
691  RECORD(DECL_ENUM_CONSTANT);
692  RECORD(DECL_FUNCTION);
693  RECORD(DECL_OBJC_METHOD);
694  RECORD(DECL_OBJC_INTERFACE);
695  RECORD(DECL_OBJC_PROTOCOL);
696  RECORD(DECL_OBJC_IVAR);
697  RECORD(DECL_OBJC_AT_DEFS_FIELD);
698  RECORD(DECL_OBJC_CLASS);
699  RECORD(DECL_OBJC_FORWARD_PROTOCOL);
700  RECORD(DECL_OBJC_CATEGORY);
701  RECORD(DECL_OBJC_CATEGORY_IMPL);
702  RECORD(DECL_OBJC_IMPLEMENTATION);
703  RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
704  RECORD(DECL_OBJC_PROPERTY);
705  RECORD(DECL_OBJC_PROPERTY_IMPL);
706  RECORD(DECL_FIELD);
707  RECORD(DECL_VAR);
708  RECORD(DECL_IMPLICIT_PARAM);
709  RECORD(DECL_PARM_VAR);
710  RECORD(DECL_FILE_SCOPE_ASM);
711  RECORD(DECL_BLOCK);
712  RECORD(DECL_CONTEXT_LEXICAL);
713  RECORD(DECL_CONTEXT_VISIBLE);
714  // Statements and Exprs can occur in the Decls and Types block.
715  AddStmtsExprs(Stream, Record);
716#undef RECORD
717#undef BLOCK
718  Stream.ExitBlock();
719}
720
721/// \brief Adjusts the given filename to only write out the portion of the
722/// filename that is not part of the system root directory.
723///
724/// \param Filename the file name to adjust.
725///
726/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
727/// the returned filename will be adjusted by this system root.
728///
729/// \returns either the original filename (if it needs no adjustment) or the
730/// adjusted filename (which points into the @p Filename parameter).
731static const char *
732adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
733  assert(Filename && "No file name to adjust?");
734
735  if (!isysroot)
736    return Filename;
737
738  // Verify that the filename and the system root have the same prefix.
739  unsigned Pos = 0;
740  for (; Filename[Pos] && isysroot[Pos]; ++Pos)
741    if (Filename[Pos] != isysroot[Pos])
742      return Filename; // Prefixes don't match.
743
744  // We hit the end of the filename before we hit the end of the system root.
745  if (!Filename[Pos])
746    return Filename;
747
748  // If the file name has a '/' at the current position, skip over the '/'.
749  // We distinguish sysroot-based includes from absolute includes by the
750  // absence of '/' at the beginning of sysroot-based includes.
751  if (Filename[Pos] == '/')
752    ++Pos;
753
754  return Filename + Pos;
755}
756
757/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
758void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
759  using namespace llvm;
760
761  // Metadata
762  const TargetInfo &Target = Context.Target;
763  BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
764  MetaAbbrev->Add(BitCodeAbbrevOp(
765                    Chain ? CHAINED_METADATA : METADATA));
766  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
767  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
768  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
769  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
770  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
771  // Target triple or chained PCH name
772  MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
773  unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
774
775  RecordData Record;
776  Record.push_back(Chain ? CHAINED_METADATA : METADATA);
777  Record.push_back(VERSION_MAJOR);
778  Record.push_back(VERSION_MINOR);
779  Record.push_back(CLANG_VERSION_MAJOR);
780  Record.push_back(CLANG_VERSION_MINOR);
781  Record.push_back(isysroot != 0);
782  // FIXME: This writes the absolute path for chained headers.
783  const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
784  Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
785
786  // Original file name
787  SourceManager &SM = Context.getSourceManager();
788  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
789    BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
790    FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
791    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
792    unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
793
794    llvm::sys::Path MainFilePath(MainFile->getName());
795
796    MainFilePath.makeAbsolute();
797
798    const char *MainFileNameStr = MainFilePath.c_str();
799    MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
800                                                      isysroot);
801    RecordData Record;
802    Record.push_back(ORIGINAL_FILE_NAME);
803    Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
804  }
805
806  // Repository branch/version information.
807  BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
808  RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
809  RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
810  unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
811  Record.clear();
812  Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
813  Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
814                            getClangFullRepositoryVersion());
815}
816
817/// \brief Write the LangOptions structure.
818void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
819  RecordData Record;
820  Record.push_back(LangOpts.Trigraphs);
821  Record.push_back(LangOpts.BCPLComment);  // BCPL-style '//' comments.
822  Record.push_back(LangOpts.DollarIdents);  // '$' allowed in identifiers.
823  Record.push_back(LangOpts.AsmPreprocessor);  // Preprocessor in asm mode.
824  Record.push_back(LangOpts.GNUMode);  // True in gnu99 mode false in c99 mode (etc)
825  Record.push_back(LangOpts.GNUKeywords);  // Allow GNU-extension keywords
826  Record.push_back(LangOpts.ImplicitInt);  // C89 implicit 'int'.
827  Record.push_back(LangOpts.Digraphs);  // C94, C99 and C++
828  Record.push_back(LangOpts.HexFloats);  // C99 Hexadecimal float constants.
829  Record.push_back(LangOpts.C99);  // C99 Support
830  Record.push_back(LangOpts.Microsoft);  // Microsoft extensions.
831  Record.push_back(LangOpts.CPlusPlus);  // C++ Support
832  Record.push_back(LangOpts.CPlusPlus0x);  // C++0x Support
833  Record.push_back(LangOpts.CXXOperatorNames);  // Treat C++ operator names as keywords.
834
835  Record.push_back(LangOpts.ObjC1);  // Objective-C 1 support enabled.
836  Record.push_back(LangOpts.ObjC2);  // Objective-C 2 support enabled.
837  Record.push_back(LangOpts.ObjCNonFragileABI);  // Objective-C
838                                                 // modern abi enabled.
839  Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
840                                                 // modern abi enabled.
841  Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
842
843  Record.push_back(LangOpts.PascalStrings);  // Allow Pascal strings
844  Record.push_back(LangOpts.WritableStrings);  // Allow writable strings
845  Record.push_back(LangOpts.LaxVectorConversions);
846  Record.push_back(LangOpts.AltiVec);
847  Record.push_back(LangOpts.Exceptions);  // Support exception handling.
848  Record.push_back(LangOpts.SjLjExceptions);
849
850  Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
851  Record.push_back(LangOpts.Freestanding); // Freestanding implementation
852  Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
853
854  // Whether static initializers are protected by locks.
855  Record.push_back(LangOpts.ThreadsafeStatics);
856  Record.push_back(LangOpts.POSIXThreads);
857  Record.push_back(LangOpts.Blocks); // block extension to C
858  Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
859                                  // they are unused.
860  Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
861                                  // (modulo the platform support).
862
863  Record.push_back(LangOpts.getSignedOverflowBehavior());
864  Record.push_back(LangOpts.HeinousExtensions);
865
866  Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
867  Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
868                                  // defined.
869  Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
870                                  // opposed to __DYNAMIC__).
871  Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
872
873  Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
874                                  // used (instead of C99 semantics).
875  Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
876  Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
877                                            // be enabled.
878  Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
879                                           // unsigned type
880  Record.push_back(LangOpts.ShortWChar);  // force wchar_t to be unsigned short
881  Record.push_back(LangOpts.getGCMode());
882  Record.push_back(LangOpts.getVisibilityMode());
883  Record.push_back(LangOpts.getStackProtectorMode());
884  Record.push_back(LangOpts.InstantiationDepth);
885  Record.push_back(LangOpts.OpenCL);
886  Record.push_back(LangOpts.CatchUndefined);
887  Record.push_back(LangOpts.ElideConstructors);
888  Record.push_back(LangOpts.SpellChecking);
889  Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
890}
891
892//===----------------------------------------------------------------------===//
893// stat cache Serialization
894//===----------------------------------------------------------------------===//
895
896namespace {
897// Trait used for the on-disk hash table of stat cache results.
898class ASTStatCacheTrait {
899public:
900  typedef const char * key_type;
901  typedef key_type key_type_ref;
902
903  typedef std::pair<int, struct stat> data_type;
904  typedef const data_type& data_type_ref;
905
906  static unsigned ComputeHash(const char *path) {
907    return llvm::HashString(path);
908  }
909
910  std::pair<unsigned,unsigned>
911    EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
912                      data_type_ref Data) {
913    unsigned StrLen = strlen(path);
914    clang::io::Emit16(Out, StrLen);
915    unsigned DataLen = 1; // result value
916    if (Data.first == 0)
917      DataLen += 4 + 4 + 2 + 8 + 8;
918    clang::io::Emit8(Out, DataLen);
919    return std::make_pair(StrLen + 1, DataLen);
920  }
921
922  void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
923    Out.write(path, KeyLen);
924  }
925
926  void EmitData(llvm::raw_ostream& Out, key_type_ref,
927                data_type_ref Data, unsigned DataLen) {
928    using namespace clang::io;
929    uint64_t Start = Out.tell(); (void)Start;
930
931    // Result of stat()
932    Emit8(Out, Data.first? 1 : 0);
933
934    if (Data.first == 0) {
935      Emit32(Out, (uint32_t) Data.second.st_ino);
936      Emit32(Out, (uint32_t) Data.second.st_dev);
937      Emit16(Out, (uint16_t) Data.second.st_mode);
938      Emit64(Out, (uint64_t) Data.second.st_mtime);
939      Emit64(Out, (uint64_t) Data.second.st_size);
940    }
941
942    assert(Out.tell() - Start == DataLen && "Wrong data length");
943  }
944};
945} // end anonymous namespace
946
947/// \brief Write the stat() system call cache to the AST file.
948void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
949  // Build the on-disk hash table containing information about every
950  // stat() call.
951  OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
952  unsigned NumStatEntries = 0;
953  for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
954                                StatEnd = StatCalls.end();
955       Stat != StatEnd; ++Stat, ++NumStatEntries) {
956    const char *Filename = Stat->first();
957    Generator.insert(Filename, Stat->second);
958  }
959
960  // Create the on-disk hash table in a buffer.
961  llvm::SmallString<4096> StatCacheData;
962  uint32_t BucketOffset;
963  {
964    llvm::raw_svector_ostream Out(StatCacheData);
965    // Make sure that no bucket is at offset 0
966    clang::io::Emit32(Out, 0);
967    BucketOffset = Generator.Emit(Out);
968  }
969
970  // Create a blob abbreviation
971  using namespace llvm;
972  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
973  Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
974  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
975  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
976  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
977  unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
978
979  // Write the stat cache
980  RecordData Record;
981  Record.push_back(STAT_CACHE);
982  Record.push_back(BucketOffset);
983  Record.push_back(NumStatEntries);
984  Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
985}
986
987//===----------------------------------------------------------------------===//
988// Source Manager Serialization
989//===----------------------------------------------------------------------===//
990
991/// \brief Create an abbreviation for the SLocEntry that refers to a
992/// file.
993static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
994  using namespace llvm;
995  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
996  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
997  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
998  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
999  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1000  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1001  // FileEntry fields.
1002  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1003  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1004  // HeaderFileInfo fields.
1005  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1006  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1007  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1008  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
1009  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1010  return Stream.EmitAbbrev(Abbrev);
1011}
1012
1013/// \brief Create an abbreviation for the SLocEntry that refers to a
1014/// buffer.
1015static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1016  using namespace llvm;
1017  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1018  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1019  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1020  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1021  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1022  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1023  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1024  return Stream.EmitAbbrev(Abbrev);
1025}
1026
1027/// \brief Create an abbreviation for the SLocEntry that refers to a
1028/// buffer's blob.
1029static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
1030  using namespace llvm;
1031  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1032  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
1033  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1034  return Stream.EmitAbbrev(Abbrev);
1035}
1036
1037/// \brief Create an abbreviation for the SLocEntry that refers to an
1038/// buffer.
1039static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
1040  using namespace llvm;
1041  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1042  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
1043  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1044  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1045  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1046  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1047  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1048  return Stream.EmitAbbrev(Abbrev);
1049}
1050
1051/// \brief Writes the block containing the serialized form of the
1052/// source manager.
1053///
1054/// TODO: We should probably use an on-disk hash table (stored in a
1055/// blob), indexed based on the file name, so that we only create
1056/// entries for files that we actually need. In the common case (no
1057/// errors), we probably won't have to create file entries for any of
1058/// the files in the AST.
1059void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1060                                        const Preprocessor &PP,
1061                                        const char *isysroot) {
1062  RecordData Record;
1063
1064  // Enter the source manager block.
1065  Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
1066
1067  // Abbreviations for the various kinds of source-location entries.
1068  unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1069  unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1070  unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1071  unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1072
1073  // Write the line table.
1074  if (SourceMgr.hasLineTable()) {
1075    LineTableInfo &LineTable = SourceMgr.getLineTable();
1076
1077    // Emit the file names
1078    Record.push_back(LineTable.getNumFilenames());
1079    for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1080      // Emit the file name
1081      const char *Filename = LineTable.getFilename(I);
1082      Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1083      unsigned FilenameLen = Filename? strlen(Filename) : 0;
1084      Record.push_back(FilenameLen);
1085      if (FilenameLen)
1086        Record.insert(Record.end(), Filename, Filename + FilenameLen);
1087    }
1088
1089    // Emit the line entries
1090    for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1091         L != LEnd; ++L) {
1092      // Emit the file ID
1093      Record.push_back(L->first);
1094
1095      // Emit the line entries
1096      Record.push_back(L->second.size());
1097      for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1098                                         LEEnd = L->second.end();
1099           LE != LEEnd; ++LE) {
1100        Record.push_back(LE->FileOffset);
1101        Record.push_back(LE->LineNo);
1102        Record.push_back(LE->FilenameID);
1103        Record.push_back((unsigned)LE->FileKind);
1104        Record.push_back(LE->IncludeOffset);
1105      }
1106    }
1107    Stream.EmitRecord(SM_LINE_TABLE, Record);
1108  }
1109
1110  // Write out the source location entry table. We skip the first
1111  // entry, which is always the same dummy entry.
1112  std::vector<uint32_t> SLocEntryOffsets;
1113  RecordData PreloadSLocs;
1114  unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1115  SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1116  for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1117       I != N; ++I) {
1118    // Get this source location entry.
1119    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1120
1121    // Record the offset of this source-location entry.
1122    SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1123
1124    // Figure out which record code to use.
1125    unsigned Code;
1126    if (SLoc->isFile()) {
1127      if (SLoc->getFile().getContentCache()->Entry)
1128        Code = SM_SLOC_FILE_ENTRY;
1129      else
1130        Code = SM_SLOC_BUFFER_ENTRY;
1131    } else
1132      Code = SM_SLOC_INSTANTIATION_ENTRY;
1133    Record.clear();
1134    Record.push_back(Code);
1135
1136    Record.push_back(SLoc->getOffset());
1137    if (SLoc->isFile()) {
1138      const SrcMgr::FileInfo &File = SLoc->getFile();
1139      Record.push_back(File.getIncludeLoc().getRawEncoding());
1140      Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1141      Record.push_back(File.hasLineDirectives());
1142
1143      const SrcMgr::ContentCache *Content = File.getContentCache();
1144      if (Content->Entry) {
1145        // The source location entry is a file. The blob associated
1146        // with this entry is the file name.
1147
1148        // Emit size/modification time for this file.
1149        Record.push_back(Content->Entry->getSize());
1150        Record.push_back(Content->Entry->getModificationTime());
1151
1152        // Emit header-search information associated with this file.
1153        HeaderFileInfo HFI;
1154        HeaderSearch &HS = PP.getHeaderSearchInfo();
1155        if (Content->Entry->getUID() < HS.header_file_size())
1156          HFI = HS.header_file_begin()[Content->Entry->getUID()];
1157        Record.push_back(HFI.isImport);
1158        Record.push_back(HFI.DirInfo);
1159        Record.push_back(HFI.NumIncludes);
1160        AddIdentifierRef(HFI.ControllingMacro, Record);
1161
1162        // Turn the file name into an absolute path, if it isn't already.
1163        const char *Filename = Content->Entry->getName();
1164        llvm::sys::Path FilePath(Filename, strlen(Filename));
1165        FilePath.makeAbsolute();
1166        Filename = FilePath.c_str();
1167
1168        Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1169        Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
1170
1171        // FIXME: For now, preload all file source locations, so that
1172        // we get the appropriate File entries in the reader. This is
1173        // a temporary measure.
1174        PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
1175      } else {
1176        // The source location entry is a buffer. The blob associated
1177        // with this entry contains the contents of the buffer.
1178
1179        // We add one to the size so that we capture the trailing NULL
1180        // that is required by llvm::MemoryBuffer::getMemBuffer (on
1181        // the reader side).
1182        const llvm::MemoryBuffer *Buffer
1183          = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1184        const char *Name = Buffer->getBufferIdentifier();
1185        Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1186                                  llvm::StringRef(Name, strlen(Name) + 1));
1187        Record.clear();
1188        Record.push_back(SM_SLOC_BUFFER_BLOB);
1189        Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1190                                  llvm::StringRef(Buffer->getBufferStart(),
1191                                                  Buffer->getBufferSize() + 1));
1192
1193        if (strcmp(Name, "<built-in>") == 0)
1194          PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
1195      }
1196    } else {
1197      // The source location entry is an instantiation.
1198      const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1199      Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1200      Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1201      Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1202
1203      // Compute the token length for this macro expansion.
1204      unsigned NextOffset = SourceMgr.getNextOffset();
1205      if (I + 1 != N)
1206        NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
1207      Record.push_back(NextOffset - SLoc->getOffset() - 1);
1208      Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1209    }
1210  }
1211
1212  Stream.ExitBlock();
1213
1214  if (SLocEntryOffsets.empty())
1215    return;
1216
1217  // Write the source-location offsets table into the AST block. This
1218  // table is used for lazily loading source-location information.
1219  using namespace llvm;
1220  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1221  Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
1222  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1223  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1224  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1225  unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1226
1227  Record.clear();
1228  Record.push_back(SOURCE_LOCATION_OFFSETS);
1229  Record.push_back(SLocEntryOffsets.size());
1230  unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1231  Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
1232  Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1233                            (const char *)data(SLocEntryOffsets),
1234                           SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
1235
1236  // Write the source location entry preloads array, telling the AST
1237  // reader which source locations entries it should load eagerly.
1238  Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
1239}
1240
1241//===----------------------------------------------------------------------===//
1242// Preprocessor Serialization
1243//===----------------------------------------------------------------------===//
1244
1245/// \brief Writes the block containing the serialized form of the
1246/// preprocessor.
1247///
1248void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
1249  RecordData Record;
1250
1251  // If the preprocessor __COUNTER__ value has been bumped, remember it.
1252  if (PP.getCounterValue() != 0) {
1253    Record.push_back(PP.getCounterValue());
1254    Stream.EmitRecord(PP_COUNTER_VALUE, Record);
1255    Record.clear();
1256  }
1257
1258  // Enter the preprocessor block.
1259  Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
1260
1261  // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
1262  // FIXME: use diagnostics subsystem for localization etc.
1263  if (PP.SawDateOrTime())
1264    fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1265
1266
1267  // Loop over all the macro definitions that are live at the end of the file,
1268  // emitting each to the PP section.
1269  PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1270  unsigned InclusionAbbrev = 0;
1271  if (PPRec) {
1272    using namespace llvm;
1273    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1274    Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1275    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1276    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1277    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1278    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1279    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1280    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1281    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1282    InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1283  }
1284
1285  for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1286       I != E; ++I) {
1287    // FIXME: This emits macros in hash table order, we should do it in a stable
1288    // order so that output is reproducible.
1289    MacroInfo *MI = I->second;
1290
1291    // Don't emit builtin macros like __LINE__ to the AST file unless they have
1292    // been redefined by the header (in which case they are not isBuiltinMacro).
1293    // Also skip macros from a AST file if we're chaining.
1294
1295    // FIXME: There is a (probably minor) optimization we could do here, if
1296    // the macro comes from the original PCH but the identifier comes from a
1297    // chained PCH, by storing the offset into the original PCH rather than
1298    // writing the macro definition a second time.
1299    if (MI->isBuiltinMacro() ||
1300        (Chain && I->first->isFromAST() && MI->isFromAST()))
1301      continue;
1302
1303    AddIdentifierRef(I->first, Record);
1304    MacroOffsets[I->first] = Stream.GetCurrentBitNo();
1305    Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1306    Record.push_back(MI->isUsed());
1307
1308    unsigned Code;
1309    if (MI->isObjectLike()) {
1310      Code = PP_MACRO_OBJECT_LIKE;
1311    } else {
1312      Code = PP_MACRO_FUNCTION_LIKE;
1313
1314      Record.push_back(MI->isC99Varargs());
1315      Record.push_back(MI->isGNUVarargs());
1316      Record.push_back(MI->getNumArgs());
1317      for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1318           I != E; ++I)
1319        AddIdentifierRef(*I, Record);
1320    }
1321
1322    // If we have a detailed preprocessing record, record the macro definition
1323    // ID that corresponds to this macro.
1324    if (PPRec)
1325      Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1326
1327    Stream.EmitRecord(Code, Record);
1328    Record.clear();
1329
1330    // Emit the tokens array.
1331    for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1332      // Note that we know that the preprocessor does not have any annotation
1333      // tokens in it because they are created by the parser, and thus can't be
1334      // in a macro definition.
1335      const Token &Tok = MI->getReplacementToken(TokNo);
1336
1337      Record.push_back(Tok.getLocation().getRawEncoding());
1338      Record.push_back(Tok.getLength());
1339
1340      // FIXME: When reading literal tokens, reconstruct the literal pointer if
1341      // it is needed.
1342      AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1343
1344      // FIXME: Should translate token kind to a stable encoding.
1345      Record.push_back(Tok.getKind());
1346      // FIXME: Should translate token flags to a stable encoding.
1347      Record.push_back(Tok.getFlags());
1348
1349      Stream.EmitRecord(PP_TOKEN, Record);
1350      Record.clear();
1351    }
1352    ++NumMacros;
1353  }
1354
1355  // If the preprocessor has a preprocessing record, emit it.
1356  unsigned NumPreprocessingRecords = 0;
1357  if (PPRec) {
1358    unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
1359    for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1360                                       EEnd = PPRec->end(Chain);
1361         E != EEnd; ++E) {
1362      Record.clear();
1363
1364      if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1365        Record.push_back(IndexBase + NumPreprocessingRecords++);
1366        AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1367        AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1368        AddIdentifierRef(MI->getName(), Record);
1369        Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1370        Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1371        continue;
1372      }
1373
1374      if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1375        // Record this macro definition's location.
1376        MacroID ID = getMacroDefinitionID(MD);
1377
1378        // Don't write the macro definition if it is from another AST file.
1379        if (ID < FirstMacroID)
1380          continue;
1381
1382        unsigned Position = ID - FirstMacroID;
1383        if (Position != MacroDefinitionOffsets.size()) {
1384          if (Position > MacroDefinitionOffsets.size())
1385            MacroDefinitionOffsets.resize(Position + 1);
1386
1387          MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1388        } else
1389          MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1390
1391        Record.push_back(IndexBase + NumPreprocessingRecords++);
1392        Record.push_back(ID);
1393        AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1394        AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1395        AddIdentifierRef(MD->getName(), Record);
1396        AddSourceLocation(MD->getLocation(), Record);
1397        Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
1398        continue;
1399      }
1400
1401      if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1402        Record.push_back(PP_INCLUSION_DIRECTIVE);
1403        Record.push_back(IndexBase + NumPreprocessingRecords++);
1404        AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1405        AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1406        Record.push_back(ID->getFileName().size());
1407        Record.push_back(ID->wasInQuotes());
1408        Record.push_back(static_cast<unsigned>(ID->getKind()));
1409        llvm::SmallString<64> Buffer;
1410        Buffer += ID->getFileName();
1411        Buffer += ID->getFile()->getName();
1412        Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1413        continue;
1414      }
1415    }
1416  }
1417
1418  Stream.ExitBlock();
1419
1420  // Write the offsets table for the preprocessing record.
1421  if (NumPreprocessingRecords > 0) {
1422    // Write the offsets table for identifier IDs.
1423    using namespace llvm;
1424    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1425    Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
1426    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1427    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1428    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1429    unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1430
1431    Record.clear();
1432    Record.push_back(MACRO_DEFINITION_OFFSETS);
1433    Record.push_back(NumPreprocessingRecords);
1434    Record.push_back(MacroDefinitionOffsets.size());
1435    Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
1436                              (const char *)data(MacroDefinitionOffsets),
1437                              MacroDefinitionOffsets.size() * sizeof(uint32_t));
1438  }
1439}
1440
1441//===----------------------------------------------------------------------===//
1442// Type Serialization
1443//===----------------------------------------------------------------------===//
1444
1445/// \brief Write the representation of a type to the AST stream.
1446void ASTWriter::WriteType(QualType T) {
1447  TypeIdx &Idx = TypeIdxs[T];
1448  if (Idx.getIndex() == 0) // we haven't seen this type before.
1449    Idx = TypeIdx(NextTypeID++);
1450
1451  assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
1452
1453  // Record the offset for this type.
1454  unsigned Index = Idx.getIndex() - FirstTypeID;
1455  if (TypeOffsets.size() == Index)
1456    TypeOffsets.push_back(Stream.GetCurrentBitNo());
1457  else if (TypeOffsets.size() < Index) {
1458    TypeOffsets.resize(Index + 1);
1459    TypeOffsets[Index] = Stream.GetCurrentBitNo();
1460  }
1461
1462  RecordData Record;
1463
1464  // Emit the type's representation.
1465  ASTTypeWriter W(*this, Record);
1466
1467  if (T.hasLocalNonFastQualifiers()) {
1468    Qualifiers Qs = T.getLocalQualifiers();
1469    AddTypeRef(T.getLocalUnqualifiedType(), Record);
1470    Record.push_back(Qs.getAsOpaqueValue());
1471    W.Code = TYPE_EXT_QUAL;
1472  } else {
1473    switch (T->getTypeClass()) {
1474      // For all of the concrete, non-dependent types, call the
1475      // appropriate visitor function.
1476#define TYPE(Class, Base) \
1477    case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1478#define ABSTRACT_TYPE(Class, Base)
1479#include "clang/AST/TypeNodes.def"
1480    }
1481  }
1482
1483  // Emit the serialized record.
1484  Stream.EmitRecord(W.Code, Record);
1485
1486  // Flush any expressions that were written as part of this type.
1487  FlushStmts();
1488}
1489
1490//===----------------------------------------------------------------------===//
1491// Declaration Serialization
1492//===----------------------------------------------------------------------===//
1493
1494/// \brief Write the block containing all of the declaration IDs
1495/// lexically declared within the given DeclContext.
1496///
1497/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1498/// bistream, or 0 if no block was written.
1499uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1500                                                 DeclContext *DC) {
1501  if (DC->decls_empty())
1502    return 0;
1503
1504  uint64_t Offset = Stream.GetCurrentBitNo();
1505  RecordData Record;
1506  Record.push_back(DECL_CONTEXT_LEXICAL);
1507  llvm::SmallVector<KindDeclIDPair, 64> Decls;
1508  for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1509         D != DEnd; ++D)
1510    Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
1511
1512  ++NumLexicalDeclContexts;
1513  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
1514                            reinterpret_cast<char*>(Decls.data()),
1515                            Decls.size() * sizeof(KindDeclIDPair));
1516  return Offset;
1517}
1518
1519void ASTWriter::WriteTypeDeclOffsets() {
1520  using namespace llvm;
1521  RecordData Record;
1522
1523  // Write the type offsets array
1524  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1525  Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
1526  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1527  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1528  unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1529  Record.clear();
1530  Record.push_back(TYPE_OFFSET);
1531  Record.push_back(TypeOffsets.size());
1532  Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1533                            (const char *)data(TypeOffsets),
1534                            TypeOffsets.size() * sizeof(TypeOffsets[0]));
1535
1536  // Write the declaration offsets array
1537  Abbrev = new BitCodeAbbrev();
1538  Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
1539  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1540  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1541  unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1542  Record.clear();
1543  Record.push_back(DECL_OFFSET);
1544  Record.push_back(DeclOffsets.size());
1545  Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1546                            (const char *)data(DeclOffsets),
1547                            DeclOffsets.size() * sizeof(DeclOffsets[0]));
1548}
1549
1550//===----------------------------------------------------------------------===//
1551// Global Method Pool and Selector Serialization
1552//===----------------------------------------------------------------------===//
1553
1554namespace {
1555// Trait used for the on-disk hash table used in the method pool.
1556class ASTMethodPoolTrait {
1557  ASTWriter &Writer;
1558
1559public:
1560  typedef Selector key_type;
1561  typedef key_type key_type_ref;
1562
1563  struct data_type {
1564    SelectorID ID;
1565    ObjCMethodList Instance, Factory;
1566  };
1567  typedef const data_type& data_type_ref;
1568
1569  explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
1570
1571  static unsigned ComputeHash(Selector Sel) {
1572    return serialization::ComputeHash(Sel);
1573  }
1574
1575  std::pair<unsigned,unsigned>
1576    EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1577                      data_type_ref Methods) {
1578    unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1579    clang::io::Emit16(Out, KeyLen);
1580    unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1581    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1582         Method = Method->Next)
1583      if (Method->Method)
1584        DataLen += 4;
1585    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1586         Method = Method->Next)
1587      if (Method->Method)
1588        DataLen += 4;
1589    clang::io::Emit16(Out, DataLen);
1590    return std::make_pair(KeyLen, DataLen);
1591  }
1592
1593  void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1594    uint64_t Start = Out.tell();
1595    assert((Start >> 32) == 0 && "Selector key offset too large");
1596    Writer.SetSelectorOffset(Sel, Start);
1597    unsigned N = Sel.getNumArgs();
1598    clang::io::Emit16(Out, N);
1599    if (N == 0)
1600      N = 1;
1601    for (unsigned I = 0; I != N; ++I)
1602      clang::io::Emit32(Out,
1603                    Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1604  }
1605
1606  void EmitData(llvm::raw_ostream& Out, key_type_ref,
1607                data_type_ref Methods, unsigned DataLen) {
1608    uint64_t Start = Out.tell(); (void)Start;
1609    clang::io::Emit32(Out, Methods.ID);
1610    unsigned NumInstanceMethods = 0;
1611    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1612         Method = Method->Next)
1613      if (Method->Method)
1614        ++NumInstanceMethods;
1615
1616    unsigned NumFactoryMethods = 0;
1617    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1618         Method = Method->Next)
1619      if (Method->Method)
1620        ++NumFactoryMethods;
1621
1622    clang::io::Emit16(Out, NumInstanceMethods);
1623    clang::io::Emit16(Out, NumFactoryMethods);
1624    for (const ObjCMethodList *Method = &Methods.Instance; Method;
1625         Method = Method->Next)
1626      if (Method->Method)
1627        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
1628    for (const ObjCMethodList *Method = &Methods.Factory; Method;
1629         Method = Method->Next)
1630      if (Method->Method)
1631        clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
1632
1633    assert(Out.tell() - Start == DataLen && "Data length is wrong");
1634  }
1635};
1636} // end anonymous namespace
1637
1638/// \brief Write ObjC data: selectors and the method pool.
1639///
1640/// The method pool contains both instance and factory methods, stored
1641/// in an on-disk hash table indexed by the selector. The hash table also
1642/// contains an empty entry for every other selector known to Sema.
1643void ASTWriter::WriteSelectors(Sema &SemaRef) {
1644  using namespace llvm;
1645
1646  // Do we have to do anything at all?
1647  if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
1648    return;
1649  unsigned NumTableEntries = 0;
1650  // Create and write out the blob that contains selectors and the method pool.
1651  {
1652    OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
1653    ASTMethodPoolTrait Trait(*this);
1654
1655    // Create the on-disk hash table representation. We walk through every
1656    // selector we've seen and look it up in the method pool.
1657    SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
1658    for (llvm::DenseMap<Selector, SelectorID>::iterator
1659             I = SelectorIDs.begin(), E = SelectorIDs.end();
1660         I != E; ++I) {
1661      Selector S = I->first;
1662      Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
1663      ASTMethodPoolTrait::data_type Data = {
1664        I->second,
1665        ObjCMethodList(),
1666        ObjCMethodList()
1667      };
1668      if (F != SemaRef.MethodPool.end()) {
1669        Data.Instance = F->second.first;
1670        Data.Factory = F->second.second;
1671      }
1672      // Only write this selector if it's not in an existing AST or something
1673      // changed.
1674      if (Chain && I->second < FirstSelectorID) {
1675        // Selector already exists. Did it change?
1676        bool changed = false;
1677        for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1678             M = M->Next) {
1679          if (M->Method->getPCHLevel() == 0)
1680            changed = true;
1681        }
1682        for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1683             M = M->Next) {
1684          if (M->Method->getPCHLevel() == 0)
1685            changed = true;
1686        }
1687        if (!changed)
1688          continue;
1689      } else if (Data.Instance.Method || Data.Factory.Method) {
1690        // A new method pool entry.
1691        ++NumTableEntries;
1692      }
1693      Generator.insert(S, Data, Trait);
1694    }
1695
1696    // Create the on-disk hash table in a buffer.
1697    llvm::SmallString<4096> MethodPool;
1698    uint32_t BucketOffset;
1699    {
1700      ASTMethodPoolTrait Trait(*this);
1701      llvm::raw_svector_ostream Out(MethodPool);
1702      // Make sure that no bucket is at offset 0
1703      clang::io::Emit32(Out, 0);
1704      BucketOffset = Generator.Emit(Out, Trait);
1705    }
1706
1707    // Create a blob abbreviation
1708    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1709    Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
1710    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1711    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1712    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1713    unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1714
1715    // Write the method pool
1716    RecordData Record;
1717    Record.push_back(METHOD_POOL);
1718    Record.push_back(BucketOffset);
1719    Record.push_back(NumTableEntries);
1720    Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
1721
1722    // Create a blob abbreviation for the selector table offsets.
1723    Abbrev = new BitCodeAbbrev();
1724    Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
1725    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1726    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1727    unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1728
1729    // Write the selector offsets table.
1730    Record.clear();
1731    Record.push_back(SELECTOR_OFFSETS);
1732    Record.push_back(SelectorOffsets.size());
1733    Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1734                              (const char *)data(SelectorOffsets),
1735                              SelectorOffsets.size() * 4);
1736  }
1737}
1738
1739/// \brief Write the selectors referenced in @selector expression into AST file.
1740void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
1741  using namespace llvm;
1742  if (SemaRef.ReferencedSelectors.empty())
1743    return;
1744
1745  RecordData Record;
1746
1747  // Note: this writes out all references even for a dependent AST. But it is
1748  // very tricky to fix, and given that @selector shouldn't really appear in
1749  // headers, probably not worth it. It's not a correctness issue.
1750  for (DenseMap<Selector, SourceLocation>::iterator S =
1751       SemaRef.ReferencedSelectors.begin(),
1752       E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1753    Selector Sel = (*S).first;
1754    SourceLocation Loc = (*S).second;
1755    AddSelectorRef(Sel, Record);
1756    AddSourceLocation(Loc, Record);
1757  }
1758  Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
1759}
1760
1761//===----------------------------------------------------------------------===//
1762// Identifier Table Serialization
1763//===----------------------------------------------------------------------===//
1764
1765namespace {
1766class ASTIdentifierTableTrait {
1767  ASTWriter &Writer;
1768  Preprocessor &PP;
1769
1770  /// \brief Determines whether this is an "interesting" identifier
1771  /// that needs a full IdentifierInfo structure written into the hash
1772  /// table.
1773  static bool isInterestingIdentifier(const IdentifierInfo *II) {
1774    return II->isPoisoned() ||
1775      II->isExtensionToken() ||
1776      II->hasMacroDefinition() ||
1777      II->getObjCOrBuiltinID() ||
1778      II->getFETokenInfo<void>();
1779  }
1780
1781public:
1782  typedef const IdentifierInfo* key_type;
1783  typedef key_type  key_type_ref;
1784
1785  typedef IdentID data_type;
1786  typedef data_type data_type_ref;
1787
1788  ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
1789    : Writer(Writer), PP(PP) { }
1790
1791  static unsigned ComputeHash(const IdentifierInfo* II) {
1792    return llvm::HashString(II->getName());
1793  }
1794
1795  std::pair<unsigned,unsigned>
1796    EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1797                      IdentID ID) {
1798    unsigned KeyLen = II->getLength() + 1;
1799    unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1800    if (isInterestingIdentifier(II)) {
1801      DataLen += 2; // 2 bytes for builtin ID, flags
1802      if (II->hasMacroDefinition() &&
1803          !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1804        DataLen += 4;
1805      for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1806                                     DEnd = IdentifierResolver::end();
1807           D != DEnd; ++D)
1808        DataLen += sizeof(DeclID);
1809    }
1810    clang::io::Emit16(Out, DataLen);
1811    // We emit the key length after the data length so that every
1812    // string is preceded by a 16-bit length. This matches the PTH
1813    // format for storing identifiers.
1814    clang::io::Emit16(Out, KeyLen);
1815    return std::make_pair(KeyLen, DataLen);
1816  }
1817
1818  void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1819               unsigned KeyLen) {
1820    // Record the location of the key data.  This is used when generating
1821    // the mapping from persistent IDs to strings.
1822    Writer.SetIdentifierOffset(II, Out.tell());
1823    Out.write(II->getNameStart(), KeyLen);
1824  }
1825
1826  void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1827                IdentID ID, unsigned) {
1828    if (!isInterestingIdentifier(II)) {
1829      clang::io::Emit32(Out, ID << 1);
1830      return;
1831    }
1832
1833    clang::io::Emit32(Out, (ID << 1) | 0x01);
1834    uint32_t Bits = 0;
1835    bool hasMacroDefinition =
1836      II->hasMacroDefinition() &&
1837      !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
1838    Bits = (uint32_t)II->getObjCOrBuiltinID();
1839    Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1840    Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1841    Bits = (Bits << 1) | unsigned(II->isPoisoned());
1842    Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
1843    Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
1844    clang::io::Emit16(Out, Bits);
1845
1846    if (hasMacroDefinition)
1847      clang::io::Emit32(Out, Writer.getMacroOffset(II));
1848
1849    // Emit the declaration IDs in reverse order, because the
1850    // IdentifierResolver provides the declarations as they would be
1851    // visible (e.g., the function "stat" would come before the struct
1852    // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1853    // adds declarations to the end of the list (so we need to see the
1854    // struct "status" before the function "status").
1855    // Only emit declarations that aren't from a chained PCH, though.
1856    llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1857                                        IdentifierResolver::end());
1858    for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1859                                                      DEnd = Decls.rend();
1860         D != DEnd; ++D)
1861      clang::io::Emit32(Out, Writer.getDeclID(*D));
1862  }
1863};
1864} // end anonymous namespace
1865
1866/// \brief Write the identifier table into the AST file.
1867///
1868/// The identifier table consists of a blob containing string data
1869/// (the actual identifiers themselves) and a separate "offsets" index
1870/// that maps identifier IDs to locations within the blob.
1871void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
1872  using namespace llvm;
1873
1874  // Create and write out the blob that contains the identifier
1875  // strings.
1876  {
1877    OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
1878    ASTIdentifierTableTrait Trait(*this, PP);
1879
1880    // Look for any identifiers that were named while processing the
1881    // headers, but are otherwise not needed. We add these to the hash
1882    // table to enable checking of the predefines buffer in the case
1883    // where the user adds new macro definitions when building the AST
1884    // file.
1885    for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1886                                IDEnd = PP.getIdentifierTable().end();
1887         ID != IDEnd; ++ID)
1888      getIdentifierRef(ID->second);
1889
1890    // Create the on-disk hash table representation. We only store offsets
1891    // for identifiers that appear here for the first time.
1892    IdentifierOffsets.resize(NextIdentID - FirstIdentID);
1893    for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
1894           ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1895         ID != IDEnd; ++ID) {
1896      assert(ID->first && "NULL identifier in identifier table");
1897      if (!Chain || !ID->first->isFromAST())
1898        Generator.insert(ID->first, ID->second, Trait);
1899    }
1900
1901    // Create the on-disk hash table in a buffer.
1902    llvm::SmallString<4096> IdentifierTable;
1903    uint32_t BucketOffset;
1904    {
1905      ASTIdentifierTableTrait Trait(*this, PP);
1906      llvm::raw_svector_ostream Out(IdentifierTable);
1907      // Make sure that no bucket is at offset 0
1908      clang::io::Emit32(Out, 0);
1909      BucketOffset = Generator.Emit(Out, Trait);
1910    }
1911
1912    // Create a blob abbreviation
1913    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1914    Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
1915    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1916    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1917    unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
1918
1919    // Write the identifier table
1920    RecordData Record;
1921    Record.push_back(IDENTIFIER_TABLE);
1922    Record.push_back(BucketOffset);
1923    Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
1924  }
1925
1926  // Write the offsets table for identifier IDs.
1927  BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1928  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
1929  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1930  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1931  unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1932
1933  RecordData Record;
1934  Record.push_back(IDENTIFIER_OFFSET);
1935  Record.push_back(IdentifierOffsets.size());
1936  Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1937                            (const char *)data(IdentifierOffsets),
1938                            IdentifierOffsets.size() * sizeof(uint32_t));
1939}
1940
1941//===----------------------------------------------------------------------===//
1942// DeclContext's Name Lookup Table Serialization
1943//===----------------------------------------------------------------------===//
1944
1945namespace {
1946// Trait used for the on-disk hash table used in the method pool.
1947class ASTDeclContextNameLookupTrait {
1948  ASTWriter &Writer;
1949
1950public:
1951  typedef DeclarationName key_type;
1952  typedef key_type key_type_ref;
1953
1954  typedef DeclContext::lookup_result data_type;
1955  typedef const data_type& data_type_ref;
1956
1957  explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1958
1959  unsigned ComputeHash(DeclarationName Name) {
1960    llvm::FoldingSetNodeID ID;
1961    ID.AddInteger(Name.getNameKind());
1962
1963    switch (Name.getNameKind()) {
1964    case DeclarationName::Identifier:
1965      ID.AddString(Name.getAsIdentifierInfo()->getName());
1966      break;
1967    case DeclarationName::ObjCZeroArgSelector:
1968    case DeclarationName::ObjCOneArgSelector:
1969    case DeclarationName::ObjCMultiArgSelector:
1970      ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1971      break;
1972    case DeclarationName::CXXConstructorName:
1973    case DeclarationName::CXXDestructorName:
1974    case DeclarationName::CXXConversionFunctionName:
1975      ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1976      break;
1977    case DeclarationName::CXXOperatorName:
1978      ID.AddInteger(Name.getCXXOverloadedOperator());
1979      break;
1980    case DeclarationName::CXXLiteralOperatorName:
1981      ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1982    case DeclarationName::CXXUsingDirective:
1983      break;
1984    }
1985
1986    return ID.ComputeHash();
1987  }
1988
1989  std::pair<unsigned,unsigned>
1990    EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1991                      data_type_ref Lookup) {
1992    unsigned KeyLen = 1;
1993    switch (Name.getNameKind()) {
1994    case DeclarationName::Identifier:
1995    case DeclarationName::ObjCZeroArgSelector:
1996    case DeclarationName::ObjCOneArgSelector:
1997    case DeclarationName::ObjCMultiArgSelector:
1998    case DeclarationName::CXXConstructorName:
1999    case DeclarationName::CXXDestructorName:
2000    case DeclarationName::CXXConversionFunctionName:
2001    case DeclarationName::CXXLiteralOperatorName:
2002      KeyLen += 4;
2003      break;
2004    case DeclarationName::CXXOperatorName:
2005      KeyLen += 1;
2006      break;
2007    case DeclarationName::CXXUsingDirective:
2008      break;
2009    }
2010    clang::io::Emit16(Out, KeyLen);
2011
2012    // 2 bytes for num of decls and 4 for each DeclID.
2013    unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2014    clang::io::Emit16(Out, DataLen);
2015
2016    return std::make_pair(KeyLen, DataLen);
2017  }
2018
2019  void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2020    using namespace clang::io;
2021
2022    assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2023    Emit8(Out, Name.getNameKind());
2024    switch (Name.getNameKind()) {
2025    case DeclarationName::Identifier:
2026      Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2027      break;
2028    case DeclarationName::ObjCZeroArgSelector:
2029    case DeclarationName::ObjCOneArgSelector:
2030    case DeclarationName::ObjCMultiArgSelector:
2031      Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2032      break;
2033    case DeclarationName::CXXConstructorName:
2034    case DeclarationName::CXXDestructorName:
2035    case DeclarationName::CXXConversionFunctionName:
2036      Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2037      break;
2038    case DeclarationName::CXXOperatorName:
2039      assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2040      Emit8(Out, Name.getCXXOverloadedOperator());
2041      break;
2042    case DeclarationName::CXXLiteralOperatorName:
2043      Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2044      break;
2045    case DeclarationName::CXXUsingDirective:
2046      break;
2047    }
2048  }
2049
2050  void EmitData(llvm::raw_ostream& Out, key_type_ref,
2051                data_type Lookup, unsigned DataLen) {
2052    uint64_t Start = Out.tell(); (void)Start;
2053    clang::io::Emit16(Out, Lookup.second - Lookup.first);
2054    for (; Lookup.first != Lookup.second; ++Lookup.first)
2055      clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2056
2057    assert(Out.tell() - Start == DataLen && "Data length is wrong");
2058  }
2059};
2060} // end anonymous namespace
2061
2062/// \brief Write the block containing all of the declaration IDs
2063/// visible from the given DeclContext.
2064///
2065/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
2066/// bitstream, or 0 if no block was written.
2067uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2068                                                 DeclContext *DC) {
2069  if (DC->getPrimaryContext() != DC)
2070    return 0;
2071
2072  // Since there is no name lookup into functions or methods, don't bother to
2073  // build a visible-declarations table for these entities.
2074  if (DC->isFunctionOrMethod())
2075    return 0;
2076
2077  // If not in C++, we perform name lookup for the translation unit via the
2078  // IdentifierInfo chains, don't bother to build a visible-declarations table.
2079  // FIXME: In C++ we need the visible declarations in order to "see" the
2080  // friend declarations, is there a way to do this without writing the table ?
2081  if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2082    return 0;
2083
2084  // Force the DeclContext to build a its name-lookup table.
2085  if (DC->hasExternalVisibleStorage())
2086    DC->MaterializeVisibleDeclsFromExternalStorage();
2087  else
2088    DC->lookup(DeclarationName());
2089
2090  // Serialize the contents of the mapping used for lookup. Note that,
2091  // although we have two very different code paths, the serialized
2092  // representation is the same for both cases: a declaration name,
2093  // followed by a size, followed by references to the visible
2094  // declarations that have that name.
2095  uint64_t Offset = Stream.GetCurrentBitNo();
2096  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2097  if (!Map || Map->empty())
2098    return 0;
2099
2100  OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2101  ASTDeclContextNameLookupTrait Trait(*this);
2102
2103  // Create the on-disk hash table representation.
2104  for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2105       D != DEnd; ++D) {
2106    DeclarationName Name = D->first;
2107    DeclContext::lookup_result Result = D->second.getLookupResult();
2108    Generator.insert(Name, Result, Trait);
2109  }
2110
2111  // Create the on-disk hash table in a buffer.
2112  llvm::SmallString<4096> LookupTable;
2113  uint32_t BucketOffset;
2114  {
2115    llvm::raw_svector_ostream Out(LookupTable);
2116    // Make sure that no bucket is at offset 0
2117    clang::io::Emit32(Out, 0);
2118    BucketOffset = Generator.Emit(Out, Trait);
2119  }
2120
2121  // Write the lookup table
2122  RecordData Record;
2123  Record.push_back(DECL_CONTEXT_VISIBLE);
2124  Record.push_back(BucketOffset);
2125  Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2126                            LookupTable.str());
2127
2128  Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2129  ++NumVisibleDeclContexts;
2130  return Offset;
2131}
2132
2133/// \brief Write an UPDATE_VISIBLE block for the given context.
2134///
2135/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2136/// DeclContext in a dependent AST file. As such, they only exist for the TU
2137/// (in C++) and for namespaces.
2138void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
2139  assert((DC->isTranslationUnit() || DC->isNamespace()) &&
2140         "Only TU and namespaces should have visible decl updates.");
2141
2142  // Make the context build its lookup table, but don't make it load external
2143  // decls.
2144  DC->lookup(DeclarationName());
2145
2146  StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2147  if (!Map || Map->empty())
2148    return;
2149
2150  OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2151  ASTDeclContextNameLookupTrait Trait(*this);
2152
2153  // Create the hash table.
2154  for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2155       D != DEnd; ++D) {
2156    DeclarationName Name = D->first;
2157    DeclContext::lookup_result Result = D->second.getLookupResult();
2158    // For any name that appears in this table, the results are complete, i.e.
2159    // they overwrite results from previous PCHs. Merging is always a mess.
2160    Generator.insert(Name, Result, Trait);
2161  }
2162
2163  // Create the on-disk hash table in a buffer.
2164  llvm::SmallString<4096> LookupTable;
2165  uint32_t BucketOffset;
2166  {
2167    llvm::raw_svector_ostream Out(LookupTable);
2168    // Make sure that no bucket is at offset 0
2169    clang::io::Emit32(Out, 0);
2170    BucketOffset = Generator.Emit(Out, Trait);
2171  }
2172
2173  // Write the lookup table
2174  RecordData Record;
2175  Record.push_back(UPDATE_VISIBLE);
2176  Record.push_back(getDeclID(cast<Decl>(DC)));
2177  Record.push_back(BucketOffset);
2178  Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2179}
2180
2181/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
2182/// that have new specializations in the current AST file.
2183void ASTWriter::WriteAdditionalTemplateSpecializations() {
2184  RecordData Record;
2185  for (AdditionalTemplateSpecializationsMap::iterator
2186           I = AdditionalTemplateSpecializations.begin(),
2187           E = AdditionalTemplateSpecializations.end();
2188       I != E; ++I) {
2189    Record.clear();
2190    Record.push_back(I->first);
2191    Record.insert(Record.end(), I->second.begin(), I->second.end());
2192    Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
2193  }
2194}
2195
2196//===----------------------------------------------------------------------===//
2197// General Serialization Routines
2198//===----------------------------------------------------------------------===//
2199
2200/// \brief Write a record containing the given attributes.
2201void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordData &Record) {
2202  Record.push_back(Attrs.size());
2203  for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2204    const Attr * A = *i;
2205    Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2206    AddSourceLocation(A->getLocation(), Record);
2207    Record.push_back(A->isInherited());
2208
2209#include "clang/Serialization/AttrPCHWrite.inc"
2210
2211  }
2212}
2213
2214void ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) {
2215  Record.push_back(Str.size());
2216  Record.insert(Record.end(), Str.begin(), Str.end());
2217}
2218
2219/// \brief Note that the identifier II occurs at the given offset
2220/// within the identifier table.
2221void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2222  IdentID ID = IdentifierIDs[II];
2223  // Only store offsets new to this AST file. Other identifier names are looked
2224  // up earlier in the chain and thus don't need an offset.
2225  if (ID >= FirstIdentID)
2226    IdentifierOffsets[ID - FirstIdentID] = Offset;
2227}
2228
2229/// \brief Note that the selector Sel occurs at the given offset
2230/// within the method pool/selector table.
2231void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2232  unsigned ID = SelectorIDs[Sel];
2233  assert(ID && "Unknown selector");
2234  // Don't record offsets for selectors that are also available in a different
2235  // file.
2236  if (ID < FirstSelectorID)
2237    return;
2238  SelectorOffsets[ID - FirstSelectorID] = Offset;
2239}
2240
2241ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
2242  : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
2243    FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
2244    FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2245    NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2246    CollectedStmts(&StmtsToEmit),
2247    NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2248    NumVisibleDeclContexts(0) {
2249}
2250
2251void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2252                         const char *isysroot) {
2253  // Emit the file header.
2254  Stream.Emit((unsigned)'C', 8);
2255  Stream.Emit((unsigned)'P', 8);
2256  Stream.Emit((unsigned)'C', 8);
2257  Stream.Emit((unsigned)'H', 8);
2258
2259  WriteBlockInfoBlock();
2260
2261  if (Chain)
2262    WriteASTChain(SemaRef, StatCalls, isysroot);
2263  else
2264    WriteASTCore(SemaRef, StatCalls, isysroot);
2265}
2266
2267void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2268                             const char *isysroot) {
2269  using namespace llvm;
2270
2271  ASTContext &Context = SemaRef.Context;
2272  Preprocessor &PP = SemaRef.PP;
2273
2274  // The translation unit is the first declaration we'll emit.
2275  DeclIDs[Context.getTranslationUnitDecl()] = 1;
2276  ++NextDeclID;
2277  DeclTypesToEmit.push(Context.getTranslationUnitDecl());
2278
2279  // Make sure that we emit IdentifierInfos (and any attached
2280  // declarations) for builtins.
2281  {
2282    IdentifierTable &Table = PP.getIdentifierTable();
2283    llvm::SmallVector<const char *, 32> BuiltinNames;
2284    Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2285                                        Context.getLangOptions().NoBuiltin);
2286    for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2287      getIdentifierRef(&Table.get(BuiltinNames[I]));
2288  }
2289
2290  // Build a record containing all of the tentative definitions in this file, in
2291  // TentativeDefinitions order.  Generally, this record will be empty for
2292  // headers.
2293  RecordData TentativeDefinitions;
2294  for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2295    AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2296  }
2297
2298  // Build a record containing all of the file scoped decls in this file.
2299  RecordData UnusedFileScopedDecls;
2300  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2301    AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
2302
2303  RecordData WeakUndeclaredIdentifiers;
2304  if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2305    WeakUndeclaredIdentifiers.push_back(
2306                                      SemaRef.WeakUndeclaredIdentifiers.size());
2307    for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2308         I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2309         E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2310      AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2311      AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2312      AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2313      WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2314    }
2315  }
2316
2317  // Build a record containing all of the locally-scoped external
2318  // declarations in this header file. Generally, this record will be
2319  // empty.
2320  RecordData LocallyScopedExternalDecls;
2321  // FIXME: This is filling in the AST file in densemap order which is
2322  // nondeterminstic!
2323  for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2324         TD = SemaRef.LocallyScopedExternalDecls.begin(),
2325         TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2326       TD != TDEnd; ++TD)
2327    AddDeclRef(TD->second, LocallyScopedExternalDecls);
2328
2329  // Build a record containing all of the ext_vector declarations.
2330  RecordData ExtVectorDecls;
2331  for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2332    AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2333
2334  // Build a record containing all of the VTable uses information.
2335  RecordData VTableUses;
2336  if (!SemaRef.VTableUses.empty()) {
2337    VTableUses.push_back(SemaRef.VTableUses.size());
2338    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2339      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2340      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2341      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2342    }
2343  }
2344
2345  // Build a record containing all of dynamic classes declarations.
2346  RecordData DynamicClasses;
2347  for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2348    AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2349
2350  // Build a record containing all of pending implicit instantiations.
2351  RecordData PendingInstantiations;
2352  for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2353         I = SemaRef.PendingInstantiations.begin(),
2354         N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2355    AddDeclRef(I->first, PendingInstantiations);
2356    AddSourceLocation(I->second, PendingInstantiations);
2357  }
2358  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2359         "There are local ones at end of translation unit!");
2360
2361  // Build a record containing some declaration references.
2362  RecordData SemaDeclRefs;
2363  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2364    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2365    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2366  }
2367
2368  // Write the remaining AST contents.
2369  RecordData Record;
2370  Stream.EnterSubblock(AST_BLOCK_ID, 5);
2371  WriteMetadata(Context, isysroot);
2372  WriteLanguageOptions(Context.getLangOptions());
2373  if (StatCalls && !isysroot)
2374    WriteStatCache(*StatCalls);
2375  WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
2376  // Write the record of special types.
2377  Record.clear();
2378
2379  AddTypeRef(Context.getBuiltinVaListType(), Record);
2380  AddTypeRef(Context.getObjCIdType(), Record);
2381  AddTypeRef(Context.getObjCSelType(), Record);
2382  AddTypeRef(Context.getObjCProtoType(), Record);
2383  AddTypeRef(Context.getObjCClassType(), Record);
2384  AddTypeRef(Context.getRawCFConstantStringType(), Record);
2385  AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2386  AddTypeRef(Context.getFILEType(), Record);
2387  AddTypeRef(Context.getjmp_bufType(), Record);
2388  AddTypeRef(Context.getsigjmp_bufType(), Record);
2389  AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2390  AddTypeRef(Context.ObjCClassRedefinitionType, Record);
2391  AddTypeRef(Context.getRawBlockdescriptorType(), Record);
2392  AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
2393  AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2394  AddTypeRef(Context.getRawNSConstantStringType(), Record);
2395  Record.push_back(Context.isInt128Installed());
2396  Stream.EmitRecord(SPECIAL_TYPES, Record);
2397
2398  // Keep writing types and declarations until all types and
2399  // declarations have been written.
2400  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
2401  WriteDeclsBlockAbbrevs();
2402  while (!DeclTypesToEmit.empty()) {
2403    DeclOrType DOT = DeclTypesToEmit.front();
2404    DeclTypesToEmit.pop();
2405    if (DOT.isType())
2406      WriteType(DOT.getType());
2407    else
2408      WriteDecl(Context, DOT.getDecl());
2409  }
2410  Stream.ExitBlock();
2411
2412  WritePreprocessor(PP);
2413  WriteSelectors(SemaRef);
2414  WriteReferencedSelectorsPool(SemaRef);
2415  WriteIdentifierTable(PP);
2416
2417  WriteTypeDeclOffsets();
2418
2419  // Write the record containing external, unnamed definitions.
2420  if (!ExternalDefinitions.empty())
2421    Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
2422
2423  // Write the record containing tentative definitions.
2424  if (!TentativeDefinitions.empty())
2425    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
2426
2427  // Write the record containing unused file scoped decls.
2428  if (!UnusedFileScopedDecls.empty())
2429    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
2430
2431  // Write the record containing weak undeclared identifiers.
2432  if (!WeakUndeclaredIdentifiers.empty())
2433    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
2434                      WeakUndeclaredIdentifiers);
2435
2436  // Write the record containing locally-scoped external definitions.
2437  if (!LocallyScopedExternalDecls.empty())
2438    Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
2439                      LocallyScopedExternalDecls);
2440
2441  // Write the record containing ext_vector type names.
2442  if (!ExtVectorDecls.empty())
2443    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
2444
2445  // Write the record containing VTable uses information.
2446  if (!VTableUses.empty())
2447    Stream.EmitRecord(VTABLE_USES, VTableUses);
2448
2449  // Write the record containing dynamic classes declarations.
2450  if (!DynamicClasses.empty())
2451    Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
2452
2453  // Write the record containing pending implicit instantiations.
2454  if (!PendingInstantiations.empty())
2455    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
2456
2457  // Write the record containing declaration references of Sema.
2458  if (!SemaDeclRefs.empty())
2459    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
2460
2461  // Some simple statistics
2462  Record.clear();
2463  Record.push_back(NumStatements);
2464  Record.push_back(NumMacros);
2465  Record.push_back(NumLexicalDeclContexts);
2466  Record.push_back(NumVisibleDeclContexts);
2467  Stream.EmitRecord(STATISTICS, Record);
2468  Stream.ExitBlock();
2469}
2470
2471void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
2472                              const char *isysroot) {
2473  using namespace llvm;
2474
2475  FirstDeclID += Chain->getTotalNumDecls();
2476  FirstTypeID += Chain->getTotalNumTypes();
2477  FirstIdentID += Chain->getTotalNumIdentifiers();
2478  FirstSelectorID += Chain->getTotalNumSelectors();
2479  FirstMacroID += Chain->getTotalNumMacroDefinitions();
2480  NextDeclID = FirstDeclID;
2481  NextTypeID = FirstTypeID;
2482  NextIdentID = FirstIdentID;
2483  NextSelectorID = FirstSelectorID;
2484  NextMacroID = FirstMacroID;
2485
2486  ASTContext &Context = SemaRef.Context;
2487  Preprocessor &PP = SemaRef.PP;
2488
2489  RecordData Record;
2490  Stream.EnterSubblock(AST_BLOCK_ID, 5);
2491  WriteMetadata(Context, isysroot);
2492  if (StatCalls && !isysroot)
2493    WriteStatCache(*StatCalls);
2494  // FIXME: Source manager block should only write new stuff, which could be
2495  // done by tracking the largest ID in the chain
2496  WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
2497
2498  // The special types are in the chained PCH.
2499
2500  // We don't start with the translation unit, but with its decls that
2501  // don't come from the chained PCH.
2502  const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
2503  llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
2504  for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2505                                  E = TU->noload_decls_end();
2506       I != E; ++I) {
2507    if ((*I)->getPCHLevel() == 0)
2508      NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
2509    else if ((*I)->isChangedSinceDeserialization())
2510      (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
2511  }
2512  // We also need to write a lexical updates block for the TU.
2513  llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
2514  Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
2515  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2516  unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2517  Record.clear();
2518  Record.push_back(TU_UPDATE_LEXICAL);
2519  Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2520                          reinterpret_cast<const char*>(NewGlobalDecls.data()),
2521                          NewGlobalDecls.size() * sizeof(KindDeclIDPair));
2522  // And in C++, a visible updates block for the TU.
2523  if (Context.getLangOptions().CPlusPlus) {
2524    Abv = new llvm::BitCodeAbbrev();
2525    Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2526    Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2527    Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2528    Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2529    UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2530    WriteDeclContextVisibleUpdate(TU);
2531  }
2532
2533  // Build a record containing all of the new tentative definitions in this
2534  // file, in TentativeDefinitions order.
2535  RecordData TentativeDefinitions;
2536  for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2537    if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2538      AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2539  }
2540
2541  // Build a record containing all of the file scoped decls in this file.
2542  RecordData UnusedFileScopedDecls;
2543  for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2544    if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2545      AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
2546  }
2547
2548  // We write the entire table, overwriting the tables from the chain.
2549  RecordData WeakUndeclaredIdentifiers;
2550  if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2551    WeakUndeclaredIdentifiers.push_back(
2552                                      SemaRef.WeakUndeclaredIdentifiers.size());
2553    for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2554         I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2555         E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2556      AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2557      AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2558      AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2559      WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2560    }
2561  }
2562
2563  // Build a record containing all of the locally-scoped external
2564  // declarations in this header file. Generally, this record will be
2565  // empty.
2566  RecordData LocallyScopedExternalDecls;
2567  // FIXME: This is filling in the AST file in densemap order which is
2568  // nondeterminstic!
2569  for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2570         TD = SemaRef.LocallyScopedExternalDecls.begin(),
2571         TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2572       TD != TDEnd; ++TD) {
2573    if (TD->second->getPCHLevel() == 0)
2574      AddDeclRef(TD->second, LocallyScopedExternalDecls);
2575  }
2576
2577  // Build a record containing all of the ext_vector declarations.
2578  RecordData ExtVectorDecls;
2579  for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2580    if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2581      AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2582  }
2583
2584  // Build a record containing all of the VTable uses information.
2585  // We write everything here, because it's too hard to determine whether
2586  // a use is new to this part.
2587  RecordData VTableUses;
2588  if (!SemaRef.VTableUses.empty()) {
2589    VTableUses.push_back(SemaRef.VTableUses.size());
2590    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2591      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2592      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2593      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2594    }
2595  }
2596
2597  // Build a record containing all of dynamic classes declarations.
2598  RecordData DynamicClasses;
2599  for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2600    if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2601      AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2602
2603  // Build a record containing all of pending implicit instantiations.
2604  RecordData PendingInstantiations;
2605  for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2606         I = SemaRef.PendingInstantiations.begin(),
2607         N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2608    if (I->first->getPCHLevel() == 0) {
2609      AddDeclRef(I->first, PendingInstantiations);
2610      AddSourceLocation(I->second, PendingInstantiations);
2611    }
2612  }
2613  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2614         "There are local ones at end of translation unit!");
2615
2616  // Build a record containing some declaration references.
2617  // It's not worth the effort to avoid duplication here.
2618  RecordData SemaDeclRefs;
2619  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2620    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2621    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2622  }
2623
2624  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
2625  WriteDeclsBlockAbbrevs();
2626  while (!DeclTypesToEmit.empty()) {
2627    DeclOrType DOT = DeclTypesToEmit.front();
2628    DeclTypesToEmit.pop();
2629    if (DOT.isType())
2630      WriteType(DOT.getType());
2631    else
2632      WriteDecl(Context, DOT.getDecl());
2633  }
2634  Stream.ExitBlock();
2635
2636  WritePreprocessor(PP);
2637  WriteSelectors(SemaRef);
2638  WriteReferencedSelectorsPool(SemaRef);
2639  WriteIdentifierTable(PP);
2640  WriteTypeDeclOffsets();
2641
2642  /// Build a record containing first declarations from a chained PCH and the
2643  /// most recent declarations in this AST that they point to.
2644  RecordData FirstLatestDeclIDs;
2645  for (FirstLatestDeclMap::iterator
2646        I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2647    assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2648           "Expected first & second to be in different PCHs");
2649    AddDeclRef(I->first, FirstLatestDeclIDs);
2650    AddDeclRef(I->second, FirstLatestDeclIDs);
2651  }
2652  if (!FirstLatestDeclIDs.empty())
2653    Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
2654
2655  // Write the record containing external, unnamed definitions.
2656  if (!ExternalDefinitions.empty())
2657    Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
2658
2659  // Write the record containing tentative definitions.
2660  if (!TentativeDefinitions.empty())
2661    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
2662
2663  // Write the record containing unused file scoped decls.
2664  if (!UnusedFileScopedDecls.empty())
2665    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
2666
2667  // Write the record containing weak undeclared identifiers.
2668  if (!WeakUndeclaredIdentifiers.empty())
2669    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
2670                      WeakUndeclaredIdentifiers);
2671
2672  // Write the record containing locally-scoped external definitions.
2673  if (!LocallyScopedExternalDecls.empty())
2674    Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
2675                      LocallyScopedExternalDecls);
2676
2677  // Write the record containing ext_vector type names.
2678  if (!ExtVectorDecls.empty())
2679    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
2680
2681  // Write the record containing VTable uses information.
2682  if (!VTableUses.empty())
2683    Stream.EmitRecord(VTABLE_USES, VTableUses);
2684
2685  // Write the record containing dynamic classes declarations.
2686  if (!DynamicClasses.empty())
2687    Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
2688
2689  // Write the record containing pending implicit instantiations.
2690  if (!PendingInstantiations.empty())
2691    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
2692
2693  // Write the record containing declaration references of Sema.
2694  if (!SemaDeclRefs.empty())
2695    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
2696
2697  // Write the updates to C++ namespaces.
2698  for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
2699           I = UpdatedNamespaces.begin(),
2700           E = UpdatedNamespaces.end();
2701         I != E; ++I)
2702    WriteDeclContextVisibleUpdate(*I);
2703
2704  // Write the updates to C++ template specialization lists.
2705  if (!AdditionalTemplateSpecializations.empty())
2706    WriteAdditionalTemplateSpecializations();
2707
2708  Record.clear();
2709  Record.push_back(NumStatements);
2710  Record.push_back(NumMacros);
2711  Record.push_back(NumLexicalDeclContexts);
2712  Record.push_back(NumVisibleDeclContexts);
2713  WriteDeclUpdateBlock();
2714  Stream.EmitRecord(STATISTICS, Record);
2715  Stream.ExitBlock();
2716}
2717
2718void ASTWriter::WriteDeclUpdateBlock() {
2719  if (ReplacedDecls.empty())
2720    return;
2721
2722  RecordData Record;
2723  for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
2724           I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2725    Record.push_back(I->first);
2726    Record.push_back(I->second);
2727  }
2728  Stream.EmitRecord(DECL_REPLACEMENTS, Record);
2729}
2730
2731void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2732  Record.push_back(Loc.getRawEncoding());
2733}
2734
2735void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
2736  AddSourceLocation(Range.getBegin(), Record);
2737  AddSourceLocation(Range.getEnd(), Record);
2738}
2739
2740void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2741  Record.push_back(Value.getBitWidth());
2742  const uint64_t *Words = Value.getRawData();
2743  Record.append(Words, Words + Value.getNumWords());
2744}
2745
2746void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2747  Record.push_back(Value.isUnsigned());
2748  AddAPInt(Value, Record);
2749}
2750
2751void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2752  AddAPInt(Value.bitcastToAPInt(), Record);
2753}
2754
2755void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
2756  Record.push_back(getIdentifierRef(II));
2757}
2758
2759IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
2760  if (II == 0)
2761    return 0;
2762
2763  IdentID &ID = IdentifierIDs[II];
2764  if (ID == 0)
2765    ID = NextIdentID++;
2766  return ID;
2767}
2768
2769MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
2770  if (MD == 0)
2771    return 0;
2772
2773  MacroID &ID = MacroDefinitions[MD];
2774  if (ID == 0)
2775    ID = NextMacroID++;
2776  return ID;
2777}
2778
2779void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2780  Record.push_back(getSelectorRef(SelRef));
2781}
2782
2783SelectorID ASTWriter::getSelectorRef(Selector Sel) {
2784  if (Sel.getAsOpaquePtr() == 0) {
2785    return 0;
2786  }
2787
2788  SelectorID &SID = SelectorIDs[Sel];
2789  if (SID == 0 && Chain) {
2790    // This might trigger a ReadSelector callback, which will set the ID for
2791    // this selector.
2792    Chain->LoadSelector(Sel);
2793  }
2794  if (SID == 0) {
2795    SID = NextSelectorID++;
2796  }
2797  return SID;
2798}
2799
2800void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
2801  AddDeclRef(Temp->getDestructor(), Record);
2802}
2803
2804void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
2805                                           const TemplateArgumentLocInfo &Arg,
2806                                           RecordData &Record) {
2807  switch (Kind) {
2808  case TemplateArgument::Expression:
2809    AddStmt(Arg.getAsExpr());
2810    break;
2811  case TemplateArgument::Type:
2812    AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
2813    break;
2814  case TemplateArgument::Template:
2815    AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2816    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
2817    break;
2818  case TemplateArgument::Null:
2819  case TemplateArgument::Integral:
2820  case TemplateArgument::Declaration:
2821  case TemplateArgument::Pack:
2822    break;
2823  }
2824}
2825
2826void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2827                                       RecordData &Record) {
2828  AddTemplateArgument(Arg.getArgument(), Record);
2829
2830  if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2831    bool InfoHasSameExpr
2832      = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2833    Record.push_back(InfoHasSameExpr);
2834    if (InfoHasSameExpr)
2835      return; // Avoid storing the same expr twice.
2836  }
2837  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2838                             Record);
2839}
2840
2841void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
2842  if (TInfo == 0) {
2843    AddTypeRef(QualType(), Record);
2844    return;
2845  }
2846
2847  AddTypeRef(TInfo->getType(), Record);
2848  TypeLocWriter TLW(*this, Record);
2849  for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2850    TLW.Visit(TL);
2851}
2852
2853void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
2854  Record.push_back(GetOrCreateTypeID(T));
2855}
2856
2857TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
2858  return MakeTypeID(T,
2859              std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2860}
2861
2862TypeID ASTWriter::getTypeID(QualType T) const {
2863  return MakeTypeID(T,
2864              std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
2865}
2866
2867TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2868  if (T.isNull())
2869    return TypeIdx();
2870  assert(!T.getLocalFastQualifiers());
2871
2872  TypeIdx &Idx = TypeIdxs[T];
2873  if (Idx.getIndex() == 0) {
2874    // We haven't seen this type before. Assign it a new ID and put it
2875    // into the queue of types to emit.
2876    Idx = TypeIdx(NextTypeID++);
2877    DeclTypesToEmit.push(T);
2878  }
2879  return Idx;
2880}
2881
2882TypeIdx ASTWriter::getTypeIdx(QualType T) const {
2883  if (T.isNull())
2884    return TypeIdx();
2885  assert(!T.getLocalFastQualifiers());
2886
2887  TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2888  assert(I != TypeIdxs.end() && "Type not emitted!");
2889  return I->second;
2890}
2891
2892void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2893  Record.push_back(GetDeclRef(D));
2894}
2895
2896DeclID ASTWriter::GetDeclRef(const Decl *D) {
2897  if (D == 0) {
2898    return 0;
2899  }
2900  assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
2901  DeclID &ID = DeclIDs[D];
2902  if (ID == 0) {
2903    // We haven't seen this declaration before. Give it a new ID and
2904    // enqueue it in the list of declarations to emit.
2905    ID = NextDeclID++;
2906    DeclTypesToEmit.push(const_cast<Decl *>(D));
2907  } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2908    // We don't add it to the replacement collection here, because we don't
2909    // have the offset yet.
2910    DeclTypesToEmit.push(const_cast<Decl *>(D));
2911    // Reset the flag, so that we don't add this decl multiple times.
2912    const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
2913  }
2914
2915  return ID;
2916}
2917
2918DeclID ASTWriter::getDeclID(const Decl *D) {
2919  if (D == 0)
2920    return 0;
2921
2922  assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2923  return DeclIDs[D];
2924}
2925
2926void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2927  // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
2928  Record.push_back(Name.getNameKind());
2929  switch (Name.getNameKind()) {
2930  case DeclarationName::Identifier:
2931    AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2932    break;
2933
2934  case DeclarationName::ObjCZeroArgSelector:
2935  case DeclarationName::ObjCOneArgSelector:
2936  case DeclarationName::ObjCMultiArgSelector:
2937    AddSelectorRef(Name.getObjCSelector(), Record);
2938    break;
2939
2940  case DeclarationName::CXXConstructorName:
2941  case DeclarationName::CXXDestructorName:
2942  case DeclarationName::CXXConversionFunctionName:
2943    AddTypeRef(Name.getCXXNameType(), Record);
2944    break;
2945
2946  case DeclarationName::CXXOperatorName:
2947    Record.push_back(Name.getCXXOverloadedOperator());
2948    break;
2949
2950  case DeclarationName::CXXLiteralOperatorName:
2951    AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2952    break;
2953
2954  case DeclarationName::CXXUsingDirective:
2955    // No extra data to emit
2956    break;
2957  }
2958}
2959
2960void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
2961                                     DeclarationName Name, RecordData &Record) {
2962  switch (Name.getNameKind()) {
2963  case DeclarationName::CXXConstructorName:
2964  case DeclarationName::CXXDestructorName:
2965  case DeclarationName::CXXConversionFunctionName:
2966    AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
2967    break;
2968
2969  case DeclarationName::CXXOperatorName:
2970    AddSourceLocation(
2971       SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
2972       Record);
2973    AddSourceLocation(
2974        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
2975        Record);
2976    break;
2977
2978  case DeclarationName::CXXLiteralOperatorName:
2979    AddSourceLocation(
2980     SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
2981     Record);
2982    break;
2983
2984  case DeclarationName::Identifier:
2985  case DeclarationName::ObjCZeroArgSelector:
2986  case DeclarationName::ObjCOneArgSelector:
2987  case DeclarationName::ObjCMultiArgSelector:
2988  case DeclarationName::CXXUsingDirective:
2989    break;
2990  }
2991}
2992
2993void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2994                                       RecordData &Record) {
2995  AddDeclarationName(NameInfo.getName(), Record);
2996  AddSourceLocation(NameInfo.getLoc(), Record);
2997  AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
2998}
2999
3000void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
3001                                 RecordData &Record) {
3002  AddNestedNameSpecifier(Info.NNS, Record);
3003  AddSourceRange(Info.NNSRange, Record);
3004  Record.push_back(Info.NumTemplParamLists);
3005  for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3006    AddTemplateParameterList(Info.TemplParamLists[i], Record);
3007}
3008
3009void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
3010                                       RecordData &Record) {
3011  // Nested name specifiers usually aren't too long. I think that 8 would
3012  // typically accomodate the vast majority.
3013  llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3014
3015  // Push each of the NNS's onto a stack for serialization in reverse order.
3016  while (NNS) {
3017    NestedNames.push_back(NNS);
3018    NNS = NNS->getPrefix();
3019  }
3020
3021  Record.push_back(NestedNames.size());
3022  while(!NestedNames.empty()) {
3023    NNS = NestedNames.pop_back_val();
3024    NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3025    Record.push_back(Kind);
3026    switch (Kind) {
3027    case NestedNameSpecifier::Identifier:
3028      AddIdentifierRef(NNS->getAsIdentifier(), Record);
3029      break;
3030
3031    case NestedNameSpecifier::Namespace:
3032      AddDeclRef(NNS->getAsNamespace(), Record);
3033      break;
3034
3035    case NestedNameSpecifier::TypeSpec:
3036    case NestedNameSpecifier::TypeSpecWithTemplate:
3037      AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3038      Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3039      break;
3040
3041    case NestedNameSpecifier::Global:
3042      // Don't need to write an associated value.
3043      break;
3044    }
3045  }
3046}
3047
3048void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
3049  TemplateName::NameKind Kind = Name.getKind();
3050  Record.push_back(Kind);
3051  switch (Kind) {
3052  case TemplateName::Template:
3053    AddDeclRef(Name.getAsTemplateDecl(), Record);
3054    break;
3055
3056  case TemplateName::OverloadedTemplate: {
3057    OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3058    Record.push_back(OvT->size());
3059    for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3060           I != E; ++I)
3061      AddDeclRef(*I, Record);
3062    break;
3063  }
3064
3065  case TemplateName::QualifiedTemplate: {
3066    QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3067    AddNestedNameSpecifier(QualT->getQualifier(), Record);
3068    Record.push_back(QualT->hasTemplateKeyword());
3069    AddDeclRef(QualT->getTemplateDecl(), Record);
3070    break;
3071  }
3072
3073  case TemplateName::DependentTemplate: {
3074    DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3075    AddNestedNameSpecifier(DepT->getQualifier(), Record);
3076    Record.push_back(DepT->isIdentifier());
3077    if (DepT->isIdentifier())
3078      AddIdentifierRef(DepT->getIdentifier(), Record);
3079    else
3080      Record.push_back(DepT->getOperator());
3081    break;
3082  }
3083  }
3084}
3085
3086void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
3087                                    RecordData &Record) {
3088  Record.push_back(Arg.getKind());
3089  switch (Arg.getKind()) {
3090  case TemplateArgument::Null:
3091    break;
3092  case TemplateArgument::Type:
3093    AddTypeRef(Arg.getAsType(), Record);
3094    break;
3095  case TemplateArgument::Declaration:
3096    AddDeclRef(Arg.getAsDecl(), Record);
3097    break;
3098  case TemplateArgument::Integral:
3099    AddAPSInt(*Arg.getAsIntegral(), Record);
3100    AddTypeRef(Arg.getIntegralType(), Record);
3101    break;
3102  case TemplateArgument::Template:
3103    AddTemplateName(Arg.getAsTemplate(), Record);
3104    break;
3105  case TemplateArgument::Expression:
3106    AddStmt(Arg.getAsExpr());
3107    break;
3108  case TemplateArgument::Pack:
3109    Record.push_back(Arg.pack_size());
3110    for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3111           I != E; ++I)
3112      AddTemplateArgument(*I, Record);
3113    break;
3114  }
3115}
3116
3117void
3118ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
3119                                    RecordData &Record) {
3120  assert(TemplateParams && "No TemplateParams!");
3121  AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3122  AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3123  AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3124  Record.push_back(TemplateParams->size());
3125  for (TemplateParameterList::const_iterator
3126         P = TemplateParams->begin(), PEnd = TemplateParams->end();
3127         P != PEnd; ++P)
3128    AddDeclRef(*P, Record);
3129}
3130
3131/// \brief Emit a template argument list.
3132void
3133ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
3134                                   RecordData &Record) {
3135  assert(TemplateArgs && "No TemplateArgs!");
3136  Record.push_back(TemplateArgs->flat_size());
3137  for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3138    AddTemplateArgument(TemplateArgs->get(i), Record);
3139}
3140
3141
3142void
3143ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
3144  Record.push_back(Set.size());
3145  for (UnresolvedSetImpl::const_iterator
3146         I = Set.begin(), E = Set.end(); I != E; ++I) {
3147    AddDeclRef(I.getDecl(), Record);
3148    Record.push_back(I.getAccess());
3149  }
3150}
3151
3152void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
3153                                    RecordData &Record) {
3154  Record.push_back(Base.isVirtual());
3155  Record.push_back(Base.isBaseOfClass());
3156  Record.push_back(Base.getAccessSpecifierAsWritten());
3157  AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
3158  AddSourceRange(Base.getSourceRange(), Record);
3159}
3160
3161void ASTWriter::AddCXXBaseOrMemberInitializers(
3162                        const CXXBaseOrMemberInitializer * const *BaseOrMembers,
3163                        unsigned NumBaseOrMembers, RecordData &Record) {
3164  Record.push_back(NumBaseOrMembers);
3165  for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3166    const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3167
3168    Record.push_back(Init->isBaseInitializer());
3169    if (Init->isBaseInitializer()) {
3170      AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3171      Record.push_back(Init->isBaseVirtual());
3172    } else {
3173      AddDeclRef(Init->getMember(), Record);
3174    }
3175    AddSourceLocation(Init->getMemberLocation(), Record);
3176    AddStmt(Init->getInit());
3177    AddDeclRef(Init->getAnonUnionMember(), Record);
3178    AddSourceLocation(Init->getLParenLoc(), Record);
3179    AddSourceLocation(Init->getRParenLoc(), Record);
3180    Record.push_back(Init->isWritten());
3181    if (Init->isWritten()) {
3182      Record.push_back(Init->getSourceOrder());
3183    } else {
3184      Record.push_back(Init->getNumArrayIndices());
3185      for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3186        AddDeclRef(Init->getArrayIndex(i), Record);
3187    }
3188  }
3189}
3190
3191void ASTWriter::SetReader(ASTReader *Reader) {
3192  assert(Reader && "Cannot remove chain");
3193  assert(FirstDeclID == NextDeclID &&
3194         FirstTypeID == NextTypeID &&
3195         FirstIdentID == NextIdentID &&
3196         FirstSelectorID == NextSelectorID &&
3197         FirstMacroID == NextMacroID &&
3198         "Setting chain after writing has started.");
3199  Chain = Reader;
3200}
3201
3202void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
3203  IdentifierIDs[II] = ID;
3204}
3205
3206void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
3207  // Always take the highest-numbered type index. This copes with an interesting
3208  // case for chained AST writing where we schedule writing the type and then,
3209  // later, deserialize the type from another AST. In this case, we want to
3210  // keep the higher-numbered entry so that we can properly write it out to
3211  // the AST file.
3212  TypeIdx &StoredIdx = TypeIdxs[T];
3213  if (Idx.getIndex() >= StoredIdx.getIndex())
3214    StoredIdx = Idx;
3215}
3216
3217void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
3218  DeclIDs[D] = ID;
3219}
3220
3221void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
3222  SelectorIDs[S] = ID;
3223}
3224
3225void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
3226                                    MacroDefinition *MD) {
3227  MacroDefinitions[MD] = ID;
3228}
3229