xref: /minix3/external/bsd/llvm/dist/clang/lib/Serialization/ASTWriter.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file defines the ASTWriter class, which writes AST files.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Serialization/ASTWriter.h"
15f4a2713aSLionel Sambuc #include "ASTCommon.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclContextInternals.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclFriend.h"
20*0a6a1f1dSLionel Sambuc #include "clang/AST/DeclLookups.h"
21f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
22f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
23f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
24f4a2713aSLionel Sambuc #include "clang/AST/Type.h"
25f4a2713aSLionel Sambuc #include "clang/AST/TypeLocVisitor.h"
26*0a6a1f1dSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
27f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
28f4a2713aSLionel Sambuc #include "clang/Basic/FileSystemStatCache.h"
29f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
30f4a2713aSLionel Sambuc #include "clang/Basic/SourceManagerInternals.h"
31f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
32f4a2713aSLionel Sambuc #include "clang/Basic/TargetOptions.h"
33f4a2713aSLionel Sambuc #include "clang/Basic/Version.h"
34f4a2713aSLionel Sambuc #include "clang/Basic/VersionTuple.h"
35f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
36f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearchOptions.h"
37f4a2713aSLionel Sambuc #include "clang/Lex/MacroInfo.h"
38f4a2713aSLionel Sambuc #include "clang/Lex/PreprocessingRecord.h"
39f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
40f4a2713aSLionel Sambuc #include "clang/Lex/PreprocessorOptions.h"
41f4a2713aSLionel Sambuc #include "clang/Sema/IdentifierResolver.h"
42f4a2713aSLionel Sambuc #include "clang/Sema/Sema.h"
43f4a2713aSLionel Sambuc #include "clang/Serialization/ASTReader.h"
44f4a2713aSLionel Sambuc #include "llvm/ADT/APFloat.h"
45f4a2713aSLionel Sambuc #include "llvm/ADT/APInt.h"
46f4a2713aSLionel Sambuc #include "llvm/ADT/Hashing.h"
47f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
48f4a2713aSLionel Sambuc #include "llvm/Bitcode/BitstreamWriter.h"
49*0a6a1f1dSLionel Sambuc #include "llvm/Support/EndianStream.h"
50f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
51f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
52*0a6a1f1dSLionel Sambuc #include "llvm/Support/OnDiskHashTable.h"
53f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
54*0a6a1f1dSLionel Sambuc #include "llvm/Support/Process.h"
55f4a2713aSLionel Sambuc #include <algorithm>
56f4a2713aSLionel Sambuc #include <cstdio>
57f4a2713aSLionel Sambuc #include <string.h>
58f4a2713aSLionel Sambuc #include <utility>
59f4a2713aSLionel Sambuc using namespace clang;
60f4a2713aSLionel Sambuc using namespace clang::serialization;
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc template <typename T, typename Allocator>
data(const std::vector<T,Allocator> & v)63f4a2713aSLionel Sambuc static StringRef data(const std::vector<T, Allocator> &v) {
64f4a2713aSLionel Sambuc   if (v.empty()) return StringRef();
65f4a2713aSLionel Sambuc   return StringRef(reinterpret_cast<const char*>(&v[0]),
66f4a2713aSLionel Sambuc                          sizeof(T) * v.size());
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc template <typename T>
data(const SmallVectorImpl<T> & v)70f4a2713aSLionel Sambuc static StringRef data(const SmallVectorImpl<T> &v) {
71f4a2713aSLionel Sambuc   return StringRef(reinterpret_cast<const char*>(v.data()),
72f4a2713aSLionel Sambuc                          sizeof(T) * v.size());
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
76f4a2713aSLionel Sambuc // Type serialization
77f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc namespace {
80f4a2713aSLionel Sambuc   class ASTTypeWriter {
81f4a2713aSLionel Sambuc     ASTWriter &Writer;
82f4a2713aSLionel Sambuc     ASTWriter::RecordDataImpl &Record;
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc   public:
85f4a2713aSLionel Sambuc     /// \brief Type code that corresponds to the record generated.
86f4a2713aSLionel Sambuc     TypeCode Code;
87*0a6a1f1dSLionel Sambuc     /// \brief Abbreviation to use for the record, if any.
88*0a6a1f1dSLionel Sambuc     unsigned AbbrevToUse;
89f4a2713aSLionel Sambuc 
ASTTypeWriter(ASTWriter & Writer,ASTWriter::RecordDataImpl & Record)90f4a2713aSLionel Sambuc     ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
91f4a2713aSLionel Sambuc       : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc     void VisitArrayType(const ArrayType *T);
94f4a2713aSLionel Sambuc     void VisitFunctionType(const FunctionType *T);
95f4a2713aSLionel Sambuc     void VisitTagType(const TagType *T);
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
98f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
99f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
100f4a2713aSLionel Sambuc   };
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc 
VisitBuiltinType(const BuiltinType * T)103f4a2713aSLionel Sambuc void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
104f4a2713aSLionel Sambuc   llvm_unreachable("Built-in types are never serialized");
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc 
VisitComplexType(const ComplexType * T)107f4a2713aSLionel Sambuc void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
108f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getElementType(), Record);
109f4a2713aSLionel Sambuc   Code = TYPE_COMPLEX;
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc 
VisitPointerType(const PointerType * T)112f4a2713aSLionel Sambuc void ASTTypeWriter::VisitPointerType(const PointerType *T) {
113f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPointeeType(), Record);
114f4a2713aSLionel Sambuc   Code = TYPE_POINTER;
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc 
VisitDecayedType(const DecayedType * T)117f4a2713aSLionel Sambuc void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
118f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getOriginalType(), Record);
119f4a2713aSLionel Sambuc   Code = TYPE_DECAYED;
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc 
VisitAdjustedType(const AdjustedType * T)122*0a6a1f1dSLionel Sambuc void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
123*0a6a1f1dSLionel Sambuc   Writer.AddTypeRef(T->getOriginalType(), Record);
124*0a6a1f1dSLionel Sambuc   Writer.AddTypeRef(T->getAdjustedType(), Record);
125*0a6a1f1dSLionel Sambuc   Code = TYPE_ADJUSTED;
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc 
VisitBlockPointerType(const BlockPointerType * T)128f4a2713aSLionel Sambuc void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
129f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPointeeType(), Record);
130f4a2713aSLionel Sambuc   Code = TYPE_BLOCK_POINTER;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc 
VisitLValueReferenceType(const LValueReferenceType * T)133f4a2713aSLionel Sambuc void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
134f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
135f4a2713aSLionel Sambuc   Record.push_back(T->isSpelledAsLValue());
136f4a2713aSLionel Sambuc   Code = TYPE_LVALUE_REFERENCE;
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc 
VisitRValueReferenceType(const RValueReferenceType * T)139f4a2713aSLionel Sambuc void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
140f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
141f4a2713aSLionel Sambuc   Code = TYPE_RVALUE_REFERENCE;
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc 
VisitMemberPointerType(const MemberPointerType * T)144f4a2713aSLionel Sambuc void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
145f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPointeeType(), Record);
146f4a2713aSLionel Sambuc   Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
147f4a2713aSLionel Sambuc   Code = TYPE_MEMBER_POINTER;
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc 
VisitArrayType(const ArrayType * T)150f4a2713aSLionel Sambuc void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
151f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getElementType(), Record);
152f4a2713aSLionel Sambuc   Record.push_back(T->getSizeModifier()); // FIXME: stable values
153f4a2713aSLionel Sambuc   Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc 
VisitConstantArrayType(const ConstantArrayType * T)156f4a2713aSLionel Sambuc void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
157f4a2713aSLionel Sambuc   VisitArrayType(T);
158f4a2713aSLionel Sambuc   Writer.AddAPInt(T->getSize(), Record);
159f4a2713aSLionel Sambuc   Code = TYPE_CONSTANT_ARRAY;
160f4a2713aSLionel Sambuc }
161f4a2713aSLionel Sambuc 
VisitIncompleteArrayType(const IncompleteArrayType * T)162f4a2713aSLionel Sambuc void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
163f4a2713aSLionel Sambuc   VisitArrayType(T);
164f4a2713aSLionel Sambuc   Code = TYPE_INCOMPLETE_ARRAY;
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc 
VisitVariableArrayType(const VariableArrayType * T)167f4a2713aSLionel Sambuc void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
168f4a2713aSLionel Sambuc   VisitArrayType(T);
169f4a2713aSLionel Sambuc   Writer.AddSourceLocation(T->getLBracketLoc(), Record);
170f4a2713aSLionel Sambuc   Writer.AddSourceLocation(T->getRBracketLoc(), Record);
171f4a2713aSLionel Sambuc   Writer.AddStmt(T->getSizeExpr());
172f4a2713aSLionel Sambuc   Code = TYPE_VARIABLE_ARRAY;
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc 
VisitVectorType(const VectorType * T)175f4a2713aSLionel Sambuc void ASTTypeWriter::VisitVectorType(const VectorType *T) {
176f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getElementType(), Record);
177f4a2713aSLionel Sambuc   Record.push_back(T->getNumElements());
178f4a2713aSLionel Sambuc   Record.push_back(T->getVectorKind());
179f4a2713aSLionel Sambuc   Code = TYPE_VECTOR;
180f4a2713aSLionel Sambuc }
181f4a2713aSLionel Sambuc 
VisitExtVectorType(const ExtVectorType * T)182f4a2713aSLionel Sambuc void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
183f4a2713aSLionel Sambuc   VisitVectorType(T);
184f4a2713aSLionel Sambuc   Code = TYPE_EXT_VECTOR;
185f4a2713aSLionel Sambuc }
186f4a2713aSLionel Sambuc 
VisitFunctionType(const FunctionType * T)187f4a2713aSLionel Sambuc void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
188*0a6a1f1dSLionel Sambuc   Writer.AddTypeRef(T->getReturnType(), Record);
189f4a2713aSLionel Sambuc   FunctionType::ExtInfo C = T->getExtInfo();
190f4a2713aSLionel Sambuc   Record.push_back(C.getNoReturn());
191f4a2713aSLionel Sambuc   Record.push_back(C.getHasRegParm());
192f4a2713aSLionel Sambuc   Record.push_back(C.getRegParm());
193f4a2713aSLionel Sambuc   // FIXME: need to stabilize encoding of calling convention...
194f4a2713aSLionel Sambuc   Record.push_back(C.getCC());
195f4a2713aSLionel Sambuc   Record.push_back(C.getProducesResult());
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc   if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
198*0a6a1f1dSLionel Sambuc     AbbrevToUse = 0;
199f4a2713aSLionel Sambuc }
200f4a2713aSLionel Sambuc 
VisitFunctionNoProtoType(const FunctionNoProtoType * T)201f4a2713aSLionel Sambuc void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
202f4a2713aSLionel Sambuc   VisitFunctionType(T);
203f4a2713aSLionel Sambuc   Code = TYPE_FUNCTION_NO_PROTO;
204f4a2713aSLionel Sambuc }
205f4a2713aSLionel Sambuc 
addExceptionSpec(ASTWriter & Writer,const FunctionProtoType * T,ASTWriter::RecordDataImpl & Record)206*0a6a1f1dSLionel Sambuc static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
207*0a6a1f1dSLionel Sambuc                              ASTWriter::RecordDataImpl &Record) {
208f4a2713aSLionel Sambuc   Record.push_back(T->getExceptionSpecType());
209f4a2713aSLionel Sambuc   if (T->getExceptionSpecType() == EST_Dynamic) {
210f4a2713aSLionel Sambuc     Record.push_back(T->getNumExceptions());
211f4a2713aSLionel Sambuc     for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
212f4a2713aSLionel Sambuc       Writer.AddTypeRef(T->getExceptionType(I), Record);
213f4a2713aSLionel Sambuc   } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
214f4a2713aSLionel Sambuc     Writer.AddStmt(T->getNoexceptExpr());
215f4a2713aSLionel Sambuc   } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
216f4a2713aSLionel Sambuc     Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
217f4a2713aSLionel Sambuc     Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
218f4a2713aSLionel Sambuc   } else if (T->getExceptionSpecType() == EST_Unevaluated) {
219f4a2713aSLionel Sambuc     Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
220f4a2713aSLionel Sambuc   }
221*0a6a1f1dSLionel Sambuc }
222*0a6a1f1dSLionel Sambuc 
VisitFunctionProtoType(const FunctionProtoType * T)223*0a6a1f1dSLionel Sambuc void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
224*0a6a1f1dSLionel Sambuc   VisitFunctionType(T);
225*0a6a1f1dSLionel Sambuc 
226*0a6a1f1dSLionel Sambuc   Record.push_back(T->isVariadic());
227*0a6a1f1dSLionel Sambuc   Record.push_back(T->hasTrailingReturn());
228*0a6a1f1dSLionel Sambuc   Record.push_back(T->getTypeQuals());
229*0a6a1f1dSLionel Sambuc   Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
230*0a6a1f1dSLionel Sambuc   addExceptionSpec(Writer, T, Record);
231*0a6a1f1dSLionel Sambuc 
232*0a6a1f1dSLionel Sambuc   Record.push_back(T->getNumParams());
233*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
234*0a6a1f1dSLionel Sambuc     Writer.AddTypeRef(T->getParamType(I), Record);
235*0a6a1f1dSLionel Sambuc 
236*0a6a1f1dSLionel Sambuc   if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
237*0a6a1f1dSLionel Sambuc       T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
238*0a6a1f1dSLionel Sambuc     AbbrevToUse = 0;
239*0a6a1f1dSLionel Sambuc 
240f4a2713aSLionel Sambuc   Code = TYPE_FUNCTION_PROTO;
241f4a2713aSLionel Sambuc }
242f4a2713aSLionel Sambuc 
VisitUnresolvedUsingType(const UnresolvedUsingType * T)243f4a2713aSLionel Sambuc void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
244f4a2713aSLionel Sambuc   Writer.AddDeclRef(T->getDecl(), Record);
245f4a2713aSLionel Sambuc   Code = TYPE_UNRESOLVED_USING;
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc 
VisitTypedefType(const TypedefType * T)248f4a2713aSLionel Sambuc void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
249f4a2713aSLionel Sambuc   Writer.AddDeclRef(T->getDecl(), Record);
250f4a2713aSLionel Sambuc   assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
251f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
252f4a2713aSLionel Sambuc   Code = TYPE_TYPEDEF;
253f4a2713aSLionel Sambuc }
254f4a2713aSLionel Sambuc 
VisitTypeOfExprType(const TypeOfExprType * T)255f4a2713aSLionel Sambuc void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
256f4a2713aSLionel Sambuc   Writer.AddStmt(T->getUnderlyingExpr());
257f4a2713aSLionel Sambuc   Code = TYPE_TYPEOF_EXPR;
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc 
VisitTypeOfType(const TypeOfType * T)260f4a2713aSLionel Sambuc void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
261f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getUnderlyingType(), Record);
262f4a2713aSLionel Sambuc   Code = TYPE_TYPEOF;
263f4a2713aSLionel Sambuc }
264f4a2713aSLionel Sambuc 
VisitDecltypeType(const DecltypeType * T)265f4a2713aSLionel Sambuc void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
266f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getUnderlyingType(), Record);
267f4a2713aSLionel Sambuc   Writer.AddStmt(T->getUnderlyingExpr());
268f4a2713aSLionel Sambuc   Code = TYPE_DECLTYPE;
269f4a2713aSLionel Sambuc }
270f4a2713aSLionel Sambuc 
VisitUnaryTransformType(const UnaryTransformType * T)271f4a2713aSLionel Sambuc void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
272f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getBaseType(), Record);
273f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getUnderlyingType(), Record);
274f4a2713aSLionel Sambuc   Record.push_back(T->getUTTKind());
275f4a2713aSLionel Sambuc   Code = TYPE_UNARY_TRANSFORM;
276f4a2713aSLionel Sambuc }
277f4a2713aSLionel Sambuc 
VisitAutoType(const AutoType * T)278f4a2713aSLionel Sambuc void ASTTypeWriter::VisitAutoType(const AutoType *T) {
279f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getDeducedType(), Record);
280f4a2713aSLionel Sambuc   Record.push_back(T->isDecltypeAuto());
281f4a2713aSLionel Sambuc   if (T->getDeducedType().isNull())
282f4a2713aSLionel Sambuc     Record.push_back(T->isDependentType());
283f4a2713aSLionel Sambuc   Code = TYPE_AUTO;
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc 
VisitTagType(const TagType * T)286f4a2713aSLionel Sambuc void ASTTypeWriter::VisitTagType(const TagType *T) {
287f4a2713aSLionel Sambuc   Record.push_back(T->isDependentType());
288f4a2713aSLionel Sambuc   Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
289f4a2713aSLionel Sambuc   assert(!T->isBeingDefined() &&
290f4a2713aSLionel Sambuc          "Cannot serialize in the middle of a type definition");
291f4a2713aSLionel Sambuc }
292f4a2713aSLionel Sambuc 
VisitRecordType(const RecordType * T)293f4a2713aSLionel Sambuc void ASTTypeWriter::VisitRecordType(const RecordType *T) {
294f4a2713aSLionel Sambuc   VisitTagType(T);
295f4a2713aSLionel Sambuc   Code = TYPE_RECORD;
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc 
VisitEnumType(const EnumType * T)298f4a2713aSLionel Sambuc void ASTTypeWriter::VisitEnumType(const EnumType *T) {
299f4a2713aSLionel Sambuc   VisitTagType(T);
300f4a2713aSLionel Sambuc   Code = TYPE_ENUM;
301f4a2713aSLionel Sambuc }
302f4a2713aSLionel Sambuc 
VisitAttributedType(const AttributedType * T)303f4a2713aSLionel Sambuc void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
304f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getModifiedType(), Record);
305f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getEquivalentType(), Record);
306f4a2713aSLionel Sambuc   Record.push_back(T->getAttrKind());
307f4a2713aSLionel Sambuc   Code = TYPE_ATTRIBUTED;
308f4a2713aSLionel Sambuc }
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc void
VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType * T)311f4a2713aSLionel Sambuc ASTTypeWriter::VisitSubstTemplateTypeParmType(
312f4a2713aSLionel Sambuc                                         const SubstTemplateTypeParmType *T) {
313f4a2713aSLionel Sambuc   Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
314f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getReplacementType(), Record);
315f4a2713aSLionel Sambuc   Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
316f4a2713aSLionel Sambuc }
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc void
VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType * T)319f4a2713aSLionel Sambuc ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
320f4a2713aSLionel Sambuc                                       const SubstTemplateTypeParmPackType *T) {
321f4a2713aSLionel Sambuc   Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
322f4a2713aSLionel Sambuc   Writer.AddTemplateArgument(T->getArgumentPack(), Record);
323f4a2713aSLionel Sambuc   Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
324f4a2713aSLionel Sambuc }
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc void
VisitTemplateSpecializationType(const TemplateSpecializationType * T)327f4a2713aSLionel Sambuc ASTTypeWriter::VisitTemplateSpecializationType(
328f4a2713aSLionel Sambuc                                        const TemplateSpecializationType *T) {
329f4a2713aSLionel Sambuc   Record.push_back(T->isDependentType());
330f4a2713aSLionel Sambuc   Writer.AddTemplateName(T->getTemplateName(), Record);
331f4a2713aSLionel Sambuc   Record.push_back(T->getNumArgs());
332f4a2713aSLionel Sambuc   for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
333f4a2713aSLionel Sambuc          ArgI != ArgE; ++ArgI)
334f4a2713aSLionel Sambuc     Writer.AddTemplateArgument(*ArgI, Record);
335f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
336f4a2713aSLionel Sambuc                     T->isCanonicalUnqualified() ? QualType()
337f4a2713aSLionel Sambuc                                                 : T->getCanonicalTypeInternal(),
338f4a2713aSLionel Sambuc                     Record);
339f4a2713aSLionel Sambuc   Code = TYPE_TEMPLATE_SPECIALIZATION;
340f4a2713aSLionel Sambuc }
341f4a2713aSLionel Sambuc 
342f4a2713aSLionel Sambuc void
VisitDependentSizedArrayType(const DependentSizedArrayType * T)343f4a2713aSLionel Sambuc ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
344f4a2713aSLionel Sambuc   VisitArrayType(T);
345f4a2713aSLionel Sambuc   Writer.AddStmt(T->getSizeExpr());
346f4a2713aSLionel Sambuc   Writer.AddSourceRange(T->getBracketsRange(), Record);
347f4a2713aSLionel Sambuc   Code = TYPE_DEPENDENT_SIZED_ARRAY;
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc 
350f4a2713aSLionel Sambuc void
VisitDependentSizedExtVectorType(const DependentSizedExtVectorType * T)351f4a2713aSLionel Sambuc ASTTypeWriter::VisitDependentSizedExtVectorType(
352f4a2713aSLionel Sambuc                                         const DependentSizedExtVectorType *T) {
353f4a2713aSLionel Sambuc   // FIXME: Serialize this type (C++ only)
354f4a2713aSLionel Sambuc   llvm_unreachable("Cannot serialize dependent sized extended vector types");
355f4a2713aSLionel Sambuc }
356f4a2713aSLionel Sambuc 
357f4a2713aSLionel Sambuc void
VisitTemplateTypeParmType(const TemplateTypeParmType * T)358f4a2713aSLionel Sambuc ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
359f4a2713aSLionel Sambuc   Record.push_back(T->getDepth());
360f4a2713aSLionel Sambuc   Record.push_back(T->getIndex());
361f4a2713aSLionel Sambuc   Record.push_back(T->isParameterPack());
362f4a2713aSLionel Sambuc   Writer.AddDeclRef(T->getDecl(), Record);
363f4a2713aSLionel Sambuc   Code = TYPE_TEMPLATE_TYPE_PARM;
364f4a2713aSLionel Sambuc }
365f4a2713aSLionel Sambuc 
366f4a2713aSLionel Sambuc void
VisitDependentNameType(const DependentNameType * T)367f4a2713aSLionel Sambuc ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
368f4a2713aSLionel Sambuc   Record.push_back(T->getKeyword());
369f4a2713aSLionel Sambuc   Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
370f4a2713aSLionel Sambuc   Writer.AddIdentifierRef(T->getIdentifier(), Record);
371f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
372f4a2713aSLionel Sambuc                                                 : T->getCanonicalTypeInternal(),
373f4a2713aSLionel Sambuc                     Record);
374f4a2713aSLionel Sambuc   Code = TYPE_DEPENDENT_NAME;
375f4a2713aSLionel Sambuc }
376f4a2713aSLionel Sambuc 
377f4a2713aSLionel Sambuc void
VisitDependentTemplateSpecializationType(const DependentTemplateSpecializationType * T)378f4a2713aSLionel Sambuc ASTTypeWriter::VisitDependentTemplateSpecializationType(
379f4a2713aSLionel Sambuc                                 const DependentTemplateSpecializationType *T) {
380f4a2713aSLionel Sambuc   Record.push_back(T->getKeyword());
381f4a2713aSLionel Sambuc   Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
382f4a2713aSLionel Sambuc   Writer.AddIdentifierRef(T->getIdentifier(), Record);
383f4a2713aSLionel Sambuc   Record.push_back(T->getNumArgs());
384f4a2713aSLionel Sambuc   for (DependentTemplateSpecializationType::iterator
385f4a2713aSLionel Sambuc          I = T->begin(), E = T->end(); I != E; ++I)
386f4a2713aSLionel Sambuc     Writer.AddTemplateArgument(*I, Record);
387f4a2713aSLionel Sambuc   Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc 
VisitPackExpansionType(const PackExpansionType * T)390f4a2713aSLionel Sambuc void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
391f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPattern(), Record);
392f4a2713aSLionel Sambuc   if (Optional<unsigned> NumExpansions = T->getNumExpansions())
393f4a2713aSLionel Sambuc     Record.push_back(*NumExpansions + 1);
394f4a2713aSLionel Sambuc   else
395f4a2713aSLionel Sambuc     Record.push_back(0);
396f4a2713aSLionel Sambuc   Code = TYPE_PACK_EXPANSION;
397f4a2713aSLionel Sambuc }
398f4a2713aSLionel Sambuc 
VisitParenType(const ParenType * T)399f4a2713aSLionel Sambuc void ASTTypeWriter::VisitParenType(const ParenType *T) {
400f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getInnerType(), Record);
401f4a2713aSLionel Sambuc   Code = TYPE_PAREN;
402f4a2713aSLionel Sambuc }
403f4a2713aSLionel Sambuc 
VisitElaboratedType(const ElaboratedType * T)404f4a2713aSLionel Sambuc void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
405f4a2713aSLionel Sambuc   Record.push_back(T->getKeyword());
406f4a2713aSLionel Sambuc   Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
407f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getNamedType(), Record);
408f4a2713aSLionel Sambuc   Code = TYPE_ELABORATED;
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc 
VisitInjectedClassNameType(const InjectedClassNameType * T)411f4a2713aSLionel Sambuc void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
412f4a2713aSLionel Sambuc   Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
413f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
414f4a2713aSLionel Sambuc   Code = TYPE_INJECTED_CLASS_NAME;
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc 
VisitObjCInterfaceType(const ObjCInterfaceType * T)417f4a2713aSLionel Sambuc void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
418f4a2713aSLionel Sambuc   Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
419f4a2713aSLionel Sambuc   Code = TYPE_OBJC_INTERFACE;
420f4a2713aSLionel Sambuc }
421f4a2713aSLionel Sambuc 
VisitObjCObjectType(const ObjCObjectType * T)422f4a2713aSLionel Sambuc void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
423f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getBaseType(), Record);
424f4a2713aSLionel Sambuc   Record.push_back(T->getNumProtocols());
425*0a6a1f1dSLionel Sambuc   for (const auto *I : T->quals())
426*0a6a1f1dSLionel Sambuc     Writer.AddDeclRef(I, Record);
427f4a2713aSLionel Sambuc   Code = TYPE_OBJC_OBJECT;
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc 
430f4a2713aSLionel Sambuc void
VisitObjCObjectPointerType(const ObjCObjectPointerType * T)431f4a2713aSLionel Sambuc ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
432f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getPointeeType(), Record);
433f4a2713aSLionel Sambuc   Code = TYPE_OBJC_OBJECT_POINTER;
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc void
VisitAtomicType(const AtomicType * T)437f4a2713aSLionel Sambuc ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
438f4a2713aSLionel Sambuc   Writer.AddTypeRef(T->getValueType(), Record);
439f4a2713aSLionel Sambuc   Code = TYPE_ATOMIC;
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc 
442f4a2713aSLionel Sambuc namespace {
443f4a2713aSLionel Sambuc 
444f4a2713aSLionel Sambuc class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
445f4a2713aSLionel Sambuc   ASTWriter &Writer;
446f4a2713aSLionel Sambuc   ASTWriter::RecordDataImpl &Record;
447f4a2713aSLionel Sambuc 
448f4a2713aSLionel Sambuc public:
TypeLocWriter(ASTWriter & Writer,ASTWriter::RecordDataImpl & Record)449f4a2713aSLionel Sambuc   TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
450f4a2713aSLionel Sambuc     : Writer(Writer), Record(Record) { }
451f4a2713aSLionel Sambuc 
452f4a2713aSLionel Sambuc #define ABSTRACT_TYPELOC(CLASS, PARENT)
453f4a2713aSLionel Sambuc #define TYPELOC(CLASS, PARENT) \
454f4a2713aSLionel Sambuc     void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
455f4a2713aSLionel Sambuc #include "clang/AST/TypeLocNodes.def"
456f4a2713aSLionel Sambuc 
457f4a2713aSLionel Sambuc   void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
458f4a2713aSLionel Sambuc   void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
459f4a2713aSLionel Sambuc };
460f4a2713aSLionel Sambuc 
461f4a2713aSLionel Sambuc }
462f4a2713aSLionel Sambuc 
VisitQualifiedTypeLoc(QualifiedTypeLoc TL)463f4a2713aSLionel Sambuc void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
464f4a2713aSLionel Sambuc   // nothing to do
465f4a2713aSLionel Sambuc }
VisitBuiltinTypeLoc(BuiltinTypeLoc TL)466f4a2713aSLionel Sambuc void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
467f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
468f4a2713aSLionel Sambuc   if (TL.needsExtraLocalData()) {
469f4a2713aSLionel Sambuc     Record.push_back(TL.getWrittenTypeSpec());
470f4a2713aSLionel Sambuc     Record.push_back(TL.getWrittenSignSpec());
471f4a2713aSLionel Sambuc     Record.push_back(TL.getWrittenWidthSpec());
472f4a2713aSLionel Sambuc     Record.push_back(TL.hasModeAttr());
473f4a2713aSLionel Sambuc   }
474f4a2713aSLionel Sambuc }
VisitComplexTypeLoc(ComplexTypeLoc TL)475f4a2713aSLionel Sambuc void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
476f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
477f4a2713aSLionel Sambuc }
VisitPointerTypeLoc(PointerTypeLoc TL)478f4a2713aSLionel Sambuc void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
479f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getStarLoc(), Record);
480f4a2713aSLionel Sambuc }
VisitDecayedTypeLoc(DecayedTypeLoc TL)481f4a2713aSLionel Sambuc void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
482f4a2713aSLionel Sambuc   // nothing to do
483f4a2713aSLionel Sambuc }
VisitAdjustedTypeLoc(AdjustedTypeLoc TL)484*0a6a1f1dSLionel Sambuc void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
485*0a6a1f1dSLionel Sambuc   // nothing to do
486*0a6a1f1dSLionel Sambuc }
VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL)487f4a2713aSLionel Sambuc void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
488f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getCaretLoc(), Record);
489f4a2713aSLionel Sambuc }
VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL)490f4a2713aSLionel Sambuc void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
491f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getAmpLoc(), Record);
492f4a2713aSLionel Sambuc }
VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL)493f4a2713aSLionel Sambuc void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
494f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
495f4a2713aSLionel Sambuc }
VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL)496f4a2713aSLionel Sambuc void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
497f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getStarLoc(), Record);
498f4a2713aSLionel Sambuc   Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
499f4a2713aSLionel Sambuc }
VisitArrayTypeLoc(ArrayTypeLoc TL)500f4a2713aSLionel Sambuc void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
501f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
502f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
503f4a2713aSLionel Sambuc   Record.push_back(TL.getSizeExpr() ? 1 : 0);
504f4a2713aSLionel Sambuc   if (TL.getSizeExpr())
505f4a2713aSLionel Sambuc     Writer.AddStmt(TL.getSizeExpr());
506f4a2713aSLionel Sambuc }
VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL)507f4a2713aSLionel Sambuc void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
508f4a2713aSLionel Sambuc   VisitArrayTypeLoc(TL);
509f4a2713aSLionel Sambuc }
VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL)510f4a2713aSLionel Sambuc void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
511f4a2713aSLionel Sambuc   VisitArrayTypeLoc(TL);
512f4a2713aSLionel Sambuc }
VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL)513f4a2713aSLionel Sambuc void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
514f4a2713aSLionel Sambuc   VisitArrayTypeLoc(TL);
515f4a2713aSLionel Sambuc }
VisitDependentSizedArrayTypeLoc(DependentSizedArrayTypeLoc TL)516f4a2713aSLionel Sambuc void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
517f4a2713aSLionel Sambuc                                             DependentSizedArrayTypeLoc TL) {
518f4a2713aSLionel Sambuc   VisitArrayTypeLoc(TL);
519f4a2713aSLionel Sambuc }
VisitDependentSizedExtVectorTypeLoc(DependentSizedExtVectorTypeLoc TL)520f4a2713aSLionel Sambuc void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
521f4a2713aSLionel Sambuc                                         DependentSizedExtVectorTypeLoc TL) {
522f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
523f4a2713aSLionel Sambuc }
VisitVectorTypeLoc(VectorTypeLoc TL)524f4a2713aSLionel Sambuc void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
525f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
526f4a2713aSLionel Sambuc }
VisitExtVectorTypeLoc(ExtVectorTypeLoc TL)527f4a2713aSLionel Sambuc void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
528f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
529f4a2713aSLionel Sambuc }
VisitFunctionTypeLoc(FunctionTypeLoc TL)530f4a2713aSLionel Sambuc void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
531f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
532f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
534f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
535*0a6a1f1dSLionel Sambuc   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
536*0a6a1f1dSLionel Sambuc     Writer.AddDeclRef(TL.getParam(i), Record);
537f4a2713aSLionel Sambuc }
VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL)538f4a2713aSLionel Sambuc void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
539f4a2713aSLionel Sambuc   VisitFunctionTypeLoc(TL);
540f4a2713aSLionel Sambuc }
VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL)541f4a2713aSLionel Sambuc void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
542f4a2713aSLionel Sambuc   VisitFunctionTypeLoc(TL);
543f4a2713aSLionel Sambuc }
VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL)544f4a2713aSLionel Sambuc void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
545f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
546f4a2713aSLionel Sambuc }
VisitTypedefTypeLoc(TypedefTypeLoc TL)547f4a2713aSLionel Sambuc void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
548f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
549f4a2713aSLionel Sambuc }
VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL)550f4a2713aSLionel Sambuc void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
551f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
552f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
554f4a2713aSLionel Sambuc }
VisitTypeOfTypeLoc(TypeOfTypeLoc TL)555f4a2713aSLionel Sambuc void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
556f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
557f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
558f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
559f4a2713aSLionel Sambuc   Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
560f4a2713aSLionel Sambuc }
VisitDecltypeTypeLoc(DecltypeTypeLoc TL)561f4a2713aSLionel Sambuc void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
562f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
563f4a2713aSLionel Sambuc }
VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL)564f4a2713aSLionel Sambuc void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
565f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getKWLoc(), Record);
566f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568f4a2713aSLionel Sambuc   Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
569f4a2713aSLionel Sambuc }
VisitAutoTypeLoc(AutoTypeLoc TL)570f4a2713aSLionel Sambuc void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
571f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
572f4a2713aSLionel Sambuc }
VisitRecordTypeLoc(RecordTypeLoc TL)573f4a2713aSLionel Sambuc void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
574f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
575f4a2713aSLionel Sambuc }
VisitEnumTypeLoc(EnumTypeLoc TL)576f4a2713aSLionel Sambuc void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
577f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
578f4a2713aSLionel Sambuc }
VisitAttributedTypeLoc(AttributedTypeLoc TL)579f4a2713aSLionel Sambuc void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
580f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
581f4a2713aSLionel Sambuc   if (TL.hasAttrOperand()) {
582f4a2713aSLionel Sambuc     SourceRange range = TL.getAttrOperandParensRange();
583f4a2713aSLionel Sambuc     Writer.AddSourceLocation(range.getBegin(), Record);
584f4a2713aSLionel Sambuc     Writer.AddSourceLocation(range.getEnd(), Record);
585f4a2713aSLionel Sambuc   }
586f4a2713aSLionel Sambuc   if (TL.hasAttrExprOperand()) {
587f4a2713aSLionel Sambuc     Expr *operand = TL.getAttrExprOperand();
588f4a2713aSLionel Sambuc     Record.push_back(operand ? 1 : 0);
589f4a2713aSLionel Sambuc     if (operand) Writer.AddStmt(operand);
590f4a2713aSLionel Sambuc   } else if (TL.hasAttrEnumOperand()) {
591f4a2713aSLionel Sambuc     Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
592f4a2713aSLionel Sambuc   }
593f4a2713aSLionel Sambuc }
VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL)594f4a2713aSLionel Sambuc void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
595f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
596f4a2713aSLionel Sambuc }
VisitSubstTemplateTypeParmTypeLoc(SubstTemplateTypeParmTypeLoc TL)597f4a2713aSLionel Sambuc void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
598f4a2713aSLionel Sambuc                                             SubstTemplateTypeParmTypeLoc TL) {
599f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
600f4a2713aSLionel Sambuc }
VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL)601f4a2713aSLionel Sambuc void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
602f4a2713aSLionel Sambuc                                           SubstTemplateTypeParmPackTypeLoc TL) {
603f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
604f4a2713aSLionel Sambuc }
VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL)605f4a2713aSLionel Sambuc void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
606f4a2713aSLionel Sambuc                                            TemplateSpecializationTypeLoc TL) {
607f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
608f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
609f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611f4a2713aSLionel Sambuc   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
612f4a2713aSLionel Sambuc     Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
613f4a2713aSLionel Sambuc                                       TL.getArgLoc(i).getLocInfo(), Record);
614f4a2713aSLionel Sambuc }
VisitParenTypeLoc(ParenTypeLoc TL)615f4a2713aSLionel Sambuc void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
616f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
617f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
618f4a2713aSLionel Sambuc }
VisitElaboratedTypeLoc(ElaboratedTypeLoc TL)619f4a2713aSLionel Sambuc void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
620f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
621f4a2713aSLionel Sambuc   Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
622f4a2713aSLionel Sambuc }
VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL)623f4a2713aSLionel Sambuc void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
624f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
625f4a2713aSLionel Sambuc }
VisitDependentNameTypeLoc(DependentNameTypeLoc TL)626f4a2713aSLionel Sambuc void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
627f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
628f4a2713aSLionel Sambuc   Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
629f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
630f4a2713aSLionel Sambuc }
VisitDependentTemplateSpecializationTypeLoc(DependentTemplateSpecializationTypeLoc TL)631f4a2713aSLionel Sambuc void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
632f4a2713aSLionel Sambuc        DependentTemplateSpecializationTypeLoc TL) {
633f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
634f4a2713aSLionel Sambuc   Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
635f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
636f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
637f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639f4a2713aSLionel Sambuc   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
640f4a2713aSLionel Sambuc     Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
641f4a2713aSLionel Sambuc                                       TL.getArgLoc(I).getLocInfo(), Record);
642f4a2713aSLionel Sambuc }
VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL)643f4a2713aSLionel Sambuc void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
644f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
645f4a2713aSLionel Sambuc }
VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL)646f4a2713aSLionel Sambuc void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
647f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getNameLoc(), Record);
648f4a2713aSLionel Sambuc }
VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL)649f4a2713aSLionel Sambuc void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
650f4a2713aSLionel Sambuc   Record.push_back(TL.hasBaseTypeAsWritten());
651f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
652f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
653f4a2713aSLionel Sambuc   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654f4a2713aSLionel Sambuc     Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
655f4a2713aSLionel Sambuc }
VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL)656f4a2713aSLionel Sambuc void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
657f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getStarLoc(), Record);
658f4a2713aSLionel Sambuc }
VisitAtomicTypeLoc(AtomicTypeLoc TL)659f4a2713aSLionel Sambuc void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
660f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getKWLoc(), Record);
661f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
662f4a2713aSLionel Sambuc   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
663f4a2713aSLionel Sambuc }
664f4a2713aSLionel Sambuc 
WriteTypeAbbrevs()665*0a6a1f1dSLionel Sambuc void ASTWriter::WriteTypeAbbrevs() {
666*0a6a1f1dSLionel Sambuc   using namespace llvm;
667*0a6a1f1dSLionel Sambuc 
668*0a6a1f1dSLionel Sambuc   BitCodeAbbrev *Abv;
669*0a6a1f1dSLionel Sambuc 
670*0a6a1f1dSLionel Sambuc   // Abbreviation for TYPE_EXT_QUAL
671*0a6a1f1dSLionel Sambuc   Abv = new BitCodeAbbrev();
672*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
673*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Type
674*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3));   // Quals
675*0a6a1f1dSLionel Sambuc   TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
676*0a6a1f1dSLionel Sambuc 
677*0a6a1f1dSLionel Sambuc   // Abbreviation for TYPE_FUNCTION_PROTO
678*0a6a1f1dSLionel Sambuc   Abv = new BitCodeAbbrev();
679*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
680*0a6a1f1dSLionel Sambuc   // FunctionType
681*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // ReturnType
682*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
683*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // HasRegParm
684*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // RegParm
685*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
686*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // ProducesResult
687*0a6a1f1dSLionel Sambuc   // FunctionProtoType
688*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // IsVariadic
689*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // HasTrailingReturn
690*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // TypeQuals
691*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(0));                         // RefQualifier
692*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(EST_None));                  // ExceptionSpec
693*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // NumParams
694*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
695*0a6a1f1dSLionel Sambuc   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Params
696*0a6a1f1dSLionel Sambuc   TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
697*0a6a1f1dSLionel Sambuc }
698*0a6a1f1dSLionel Sambuc 
699f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
700f4a2713aSLionel Sambuc // ASTWriter Implementation
701f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
702f4a2713aSLionel Sambuc 
EmitBlockID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)703f4a2713aSLionel Sambuc static void EmitBlockID(unsigned ID, const char *Name,
704f4a2713aSLionel Sambuc                         llvm::BitstreamWriter &Stream,
705f4a2713aSLionel Sambuc                         ASTWriter::RecordDataImpl &Record) {
706f4a2713aSLionel Sambuc   Record.clear();
707f4a2713aSLionel Sambuc   Record.push_back(ID);
708f4a2713aSLionel Sambuc   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
709f4a2713aSLionel Sambuc 
710f4a2713aSLionel Sambuc   // Emit the block name if present.
711*0a6a1f1dSLionel Sambuc   if (!Name || Name[0] == 0)
712*0a6a1f1dSLionel Sambuc     return;
713f4a2713aSLionel Sambuc   Record.clear();
714f4a2713aSLionel Sambuc   while (*Name)
715f4a2713aSLionel Sambuc     Record.push_back(*Name++);
716f4a2713aSLionel Sambuc   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
717f4a2713aSLionel Sambuc }
718f4a2713aSLionel Sambuc 
EmitRecordID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)719f4a2713aSLionel Sambuc static void EmitRecordID(unsigned ID, const char *Name,
720f4a2713aSLionel Sambuc                          llvm::BitstreamWriter &Stream,
721f4a2713aSLionel Sambuc                          ASTWriter::RecordDataImpl &Record) {
722f4a2713aSLionel Sambuc   Record.clear();
723f4a2713aSLionel Sambuc   Record.push_back(ID);
724f4a2713aSLionel Sambuc   while (*Name)
725f4a2713aSLionel Sambuc     Record.push_back(*Name++);
726f4a2713aSLionel Sambuc   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
727f4a2713aSLionel Sambuc }
728f4a2713aSLionel Sambuc 
AddStmtsExprs(llvm::BitstreamWriter & Stream,ASTWriter::RecordDataImpl & Record)729f4a2713aSLionel Sambuc static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
730f4a2713aSLionel Sambuc                           ASTWriter::RecordDataImpl &Record) {
731f4a2713aSLionel Sambuc #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
732f4a2713aSLionel Sambuc   RECORD(STMT_STOP);
733f4a2713aSLionel Sambuc   RECORD(STMT_NULL_PTR);
734*0a6a1f1dSLionel Sambuc   RECORD(STMT_REF_PTR);
735f4a2713aSLionel Sambuc   RECORD(STMT_NULL);
736f4a2713aSLionel Sambuc   RECORD(STMT_COMPOUND);
737f4a2713aSLionel Sambuc   RECORD(STMT_CASE);
738f4a2713aSLionel Sambuc   RECORD(STMT_DEFAULT);
739f4a2713aSLionel Sambuc   RECORD(STMT_LABEL);
740f4a2713aSLionel Sambuc   RECORD(STMT_ATTRIBUTED);
741f4a2713aSLionel Sambuc   RECORD(STMT_IF);
742f4a2713aSLionel Sambuc   RECORD(STMT_SWITCH);
743f4a2713aSLionel Sambuc   RECORD(STMT_WHILE);
744f4a2713aSLionel Sambuc   RECORD(STMT_DO);
745f4a2713aSLionel Sambuc   RECORD(STMT_FOR);
746f4a2713aSLionel Sambuc   RECORD(STMT_GOTO);
747f4a2713aSLionel Sambuc   RECORD(STMT_INDIRECT_GOTO);
748f4a2713aSLionel Sambuc   RECORD(STMT_CONTINUE);
749f4a2713aSLionel Sambuc   RECORD(STMT_BREAK);
750f4a2713aSLionel Sambuc   RECORD(STMT_RETURN);
751f4a2713aSLionel Sambuc   RECORD(STMT_DECL);
752f4a2713aSLionel Sambuc   RECORD(STMT_GCCASM);
753f4a2713aSLionel Sambuc   RECORD(STMT_MSASM);
754f4a2713aSLionel Sambuc   RECORD(EXPR_PREDEFINED);
755f4a2713aSLionel Sambuc   RECORD(EXPR_DECL_REF);
756f4a2713aSLionel Sambuc   RECORD(EXPR_INTEGER_LITERAL);
757f4a2713aSLionel Sambuc   RECORD(EXPR_FLOATING_LITERAL);
758f4a2713aSLionel Sambuc   RECORD(EXPR_IMAGINARY_LITERAL);
759f4a2713aSLionel Sambuc   RECORD(EXPR_STRING_LITERAL);
760f4a2713aSLionel Sambuc   RECORD(EXPR_CHARACTER_LITERAL);
761f4a2713aSLionel Sambuc   RECORD(EXPR_PAREN);
762*0a6a1f1dSLionel Sambuc   RECORD(EXPR_PAREN_LIST);
763f4a2713aSLionel Sambuc   RECORD(EXPR_UNARY_OPERATOR);
764f4a2713aSLionel Sambuc   RECORD(EXPR_SIZEOF_ALIGN_OF);
765f4a2713aSLionel Sambuc   RECORD(EXPR_ARRAY_SUBSCRIPT);
766f4a2713aSLionel Sambuc   RECORD(EXPR_CALL);
767f4a2713aSLionel Sambuc   RECORD(EXPR_MEMBER);
768f4a2713aSLionel Sambuc   RECORD(EXPR_BINARY_OPERATOR);
769f4a2713aSLionel Sambuc   RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
770f4a2713aSLionel Sambuc   RECORD(EXPR_CONDITIONAL_OPERATOR);
771f4a2713aSLionel Sambuc   RECORD(EXPR_IMPLICIT_CAST);
772f4a2713aSLionel Sambuc   RECORD(EXPR_CSTYLE_CAST);
773f4a2713aSLionel Sambuc   RECORD(EXPR_COMPOUND_LITERAL);
774f4a2713aSLionel Sambuc   RECORD(EXPR_EXT_VECTOR_ELEMENT);
775f4a2713aSLionel Sambuc   RECORD(EXPR_INIT_LIST);
776f4a2713aSLionel Sambuc   RECORD(EXPR_DESIGNATED_INIT);
777f4a2713aSLionel Sambuc   RECORD(EXPR_IMPLICIT_VALUE_INIT);
778f4a2713aSLionel Sambuc   RECORD(EXPR_VA_ARG);
779f4a2713aSLionel Sambuc   RECORD(EXPR_ADDR_LABEL);
780f4a2713aSLionel Sambuc   RECORD(EXPR_STMT);
781f4a2713aSLionel Sambuc   RECORD(EXPR_CHOOSE);
782f4a2713aSLionel Sambuc   RECORD(EXPR_GNU_NULL);
783f4a2713aSLionel Sambuc   RECORD(EXPR_SHUFFLE_VECTOR);
784f4a2713aSLionel Sambuc   RECORD(EXPR_BLOCK);
785f4a2713aSLionel Sambuc   RECORD(EXPR_GENERIC_SELECTION);
786f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_STRING_LITERAL);
787f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_BOXED_EXPRESSION);
788f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_ARRAY_LITERAL);
789f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
790f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_ENCODE);
791f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_SELECTOR_EXPR);
792f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_PROTOCOL_EXPR);
793f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_IVAR_REF_EXPR);
794f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
795f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_KVC_REF_EXPR);
796f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_MESSAGE_EXPR);
797f4a2713aSLionel Sambuc   RECORD(STMT_OBJC_FOR_COLLECTION);
798f4a2713aSLionel Sambuc   RECORD(STMT_OBJC_CATCH);
799f4a2713aSLionel Sambuc   RECORD(STMT_OBJC_FINALLY);
800f4a2713aSLionel Sambuc   RECORD(STMT_OBJC_AT_TRY);
801f4a2713aSLionel Sambuc   RECORD(STMT_OBJC_AT_SYNCHRONIZED);
802f4a2713aSLionel Sambuc   RECORD(STMT_OBJC_AT_THROW);
803f4a2713aSLionel Sambuc   RECORD(EXPR_OBJC_BOOL_LITERAL);
804*0a6a1f1dSLionel Sambuc   RECORD(STMT_CXX_CATCH);
805*0a6a1f1dSLionel Sambuc   RECORD(STMT_CXX_TRY);
806*0a6a1f1dSLionel Sambuc   RECORD(STMT_CXX_FOR_RANGE);
807f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_OPERATOR_CALL);
808*0a6a1f1dSLionel Sambuc   RECORD(EXPR_CXX_MEMBER_CALL);
809f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_CONSTRUCT);
810*0a6a1f1dSLionel Sambuc   RECORD(EXPR_CXX_TEMPORARY_OBJECT);
811f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_STATIC_CAST);
812f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_DYNAMIC_CAST);
813f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_REINTERPRET_CAST);
814f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_CONST_CAST);
815f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_FUNCTIONAL_CAST);
816f4a2713aSLionel Sambuc   RECORD(EXPR_USER_DEFINED_LITERAL);
817f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
818f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_BOOL_LITERAL);
819f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_NULL_PTR_LITERAL);
820f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_TYPEID_EXPR);
821f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_TYPEID_TYPE);
822f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_THIS);
823f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_THROW);
824f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_DEFAULT_ARG);
825*0a6a1f1dSLionel Sambuc   RECORD(EXPR_CXX_DEFAULT_INIT);
826f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_BIND_TEMPORARY);
827f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
828f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_NEW);
829f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_DELETE);
830f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
831f4a2713aSLionel Sambuc   RECORD(EXPR_EXPR_WITH_CLEANUPS);
832f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
833f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
834f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
835f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
836f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
837*0a6a1f1dSLionel Sambuc   RECORD(EXPR_CXX_EXPRESSION_TRAIT);
838f4a2713aSLionel Sambuc   RECORD(EXPR_CXX_NOEXCEPT);
839f4a2713aSLionel Sambuc   RECORD(EXPR_OPAQUE_VALUE);
840*0a6a1f1dSLionel Sambuc   RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
841*0a6a1f1dSLionel Sambuc   RECORD(EXPR_TYPE_TRAIT);
842*0a6a1f1dSLionel Sambuc   RECORD(EXPR_ARRAY_TYPE_TRAIT);
843f4a2713aSLionel Sambuc   RECORD(EXPR_PACK_EXPANSION);
844f4a2713aSLionel Sambuc   RECORD(EXPR_SIZEOF_PACK);
845*0a6a1f1dSLionel Sambuc   RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
846f4a2713aSLionel Sambuc   RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
847*0a6a1f1dSLionel Sambuc   RECORD(EXPR_FUNCTION_PARM_PACK);
848*0a6a1f1dSLionel Sambuc   RECORD(EXPR_MATERIALIZE_TEMPORARY);
849f4a2713aSLionel Sambuc   RECORD(EXPR_CUDA_KERNEL_CALL);
850*0a6a1f1dSLionel Sambuc   RECORD(EXPR_CXX_UUIDOF_EXPR);
851*0a6a1f1dSLionel Sambuc   RECORD(EXPR_CXX_UUIDOF_TYPE);
852*0a6a1f1dSLionel Sambuc   RECORD(EXPR_LAMBDA);
853f4a2713aSLionel Sambuc #undef RECORD
854f4a2713aSLionel Sambuc }
855f4a2713aSLionel Sambuc 
WriteBlockInfoBlock()856f4a2713aSLionel Sambuc void ASTWriter::WriteBlockInfoBlock() {
857f4a2713aSLionel Sambuc   RecordData Record;
858f4a2713aSLionel Sambuc   Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
859f4a2713aSLionel Sambuc 
860f4a2713aSLionel Sambuc #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
861f4a2713aSLionel Sambuc #define RECORD(X) EmitRecordID(X, #X, Stream, Record)
862f4a2713aSLionel Sambuc 
863f4a2713aSLionel Sambuc   // Control Block.
864f4a2713aSLionel Sambuc   BLOCK(CONTROL_BLOCK);
865f4a2713aSLionel Sambuc   RECORD(METADATA);
866*0a6a1f1dSLionel Sambuc   RECORD(SIGNATURE);
867*0a6a1f1dSLionel Sambuc   RECORD(MODULE_NAME);
868*0a6a1f1dSLionel Sambuc   RECORD(MODULE_MAP_FILE);
869f4a2713aSLionel Sambuc   RECORD(IMPORTS);
870f4a2713aSLionel Sambuc   RECORD(LANGUAGE_OPTIONS);
871f4a2713aSLionel Sambuc   RECORD(TARGET_OPTIONS);
872f4a2713aSLionel Sambuc   RECORD(ORIGINAL_FILE);
873f4a2713aSLionel Sambuc   RECORD(ORIGINAL_PCH_DIR);
874f4a2713aSLionel Sambuc   RECORD(ORIGINAL_FILE_ID);
875f4a2713aSLionel Sambuc   RECORD(INPUT_FILE_OFFSETS);
876f4a2713aSLionel Sambuc   RECORD(DIAGNOSTIC_OPTIONS);
877f4a2713aSLionel Sambuc   RECORD(FILE_SYSTEM_OPTIONS);
878f4a2713aSLionel Sambuc   RECORD(HEADER_SEARCH_OPTIONS);
879f4a2713aSLionel Sambuc   RECORD(PREPROCESSOR_OPTIONS);
880f4a2713aSLionel Sambuc 
881f4a2713aSLionel Sambuc   BLOCK(INPUT_FILES_BLOCK);
882f4a2713aSLionel Sambuc   RECORD(INPUT_FILE);
883f4a2713aSLionel Sambuc 
884f4a2713aSLionel Sambuc   // AST Top-Level Block.
885f4a2713aSLionel Sambuc   BLOCK(AST_BLOCK);
886f4a2713aSLionel Sambuc   RECORD(TYPE_OFFSET);
887f4a2713aSLionel Sambuc   RECORD(DECL_OFFSET);
888f4a2713aSLionel Sambuc   RECORD(IDENTIFIER_OFFSET);
889f4a2713aSLionel Sambuc   RECORD(IDENTIFIER_TABLE);
890*0a6a1f1dSLionel Sambuc   RECORD(EAGERLY_DESERIALIZED_DECLS);
891f4a2713aSLionel Sambuc   RECORD(SPECIAL_TYPES);
892f4a2713aSLionel Sambuc   RECORD(STATISTICS);
893f4a2713aSLionel Sambuc   RECORD(TENTATIVE_DEFINITIONS);
894f4a2713aSLionel Sambuc   RECORD(UNUSED_FILESCOPED_DECLS);
895f4a2713aSLionel Sambuc   RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
896f4a2713aSLionel Sambuc   RECORD(SELECTOR_OFFSETS);
897f4a2713aSLionel Sambuc   RECORD(METHOD_POOL);
898f4a2713aSLionel Sambuc   RECORD(PP_COUNTER_VALUE);
899f4a2713aSLionel Sambuc   RECORD(SOURCE_LOCATION_OFFSETS);
900f4a2713aSLionel Sambuc   RECORD(SOURCE_LOCATION_PRELOADS);
901f4a2713aSLionel Sambuc   RECORD(EXT_VECTOR_DECLS);
902f4a2713aSLionel Sambuc   RECORD(PPD_ENTITIES_OFFSETS);
903f4a2713aSLionel Sambuc   RECORD(REFERENCED_SELECTOR_POOL);
904f4a2713aSLionel Sambuc   RECORD(TU_UPDATE_LEXICAL);
905f4a2713aSLionel Sambuc   RECORD(LOCAL_REDECLARATIONS_MAP);
906f4a2713aSLionel Sambuc   RECORD(SEMA_DECL_REFS);
907f4a2713aSLionel Sambuc   RECORD(WEAK_UNDECLARED_IDENTIFIERS);
908f4a2713aSLionel Sambuc   RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
909f4a2713aSLionel Sambuc   RECORD(DECL_REPLACEMENTS);
910f4a2713aSLionel Sambuc   RECORD(UPDATE_VISIBLE);
911f4a2713aSLionel Sambuc   RECORD(DECL_UPDATE_OFFSETS);
912f4a2713aSLionel Sambuc   RECORD(DECL_UPDATES);
913f4a2713aSLionel Sambuc   RECORD(CXX_BASE_SPECIFIER_OFFSETS);
914f4a2713aSLionel Sambuc   RECORD(DIAG_PRAGMA_MAPPINGS);
915f4a2713aSLionel Sambuc   RECORD(CUDA_SPECIAL_DECL_REFS);
916f4a2713aSLionel Sambuc   RECORD(HEADER_SEARCH_TABLE);
917f4a2713aSLionel Sambuc   RECORD(FP_PRAGMA_OPTIONS);
918f4a2713aSLionel Sambuc   RECORD(OPENCL_EXTENSIONS);
919f4a2713aSLionel Sambuc   RECORD(DELEGATING_CTORS);
920f4a2713aSLionel Sambuc   RECORD(KNOWN_NAMESPACES);
921f4a2713aSLionel Sambuc   RECORD(UNDEFINED_BUT_USED);
922f4a2713aSLionel Sambuc   RECORD(MODULE_OFFSET_MAP);
923f4a2713aSLionel Sambuc   RECORD(SOURCE_MANAGER_LINE_TABLE);
924f4a2713aSLionel Sambuc   RECORD(OBJC_CATEGORIES_MAP);
925f4a2713aSLionel Sambuc   RECORD(FILE_SORTED_DECLS);
926f4a2713aSLionel Sambuc   RECORD(IMPORTED_MODULES);
927f4a2713aSLionel Sambuc   RECORD(MERGED_DECLARATIONS);
928f4a2713aSLionel Sambuc   RECORD(LOCAL_REDECLARATIONS);
929f4a2713aSLionel Sambuc   RECORD(OBJC_CATEGORIES);
930f4a2713aSLionel Sambuc   RECORD(MACRO_OFFSET);
931f4a2713aSLionel Sambuc   RECORD(MACRO_TABLE);
932f4a2713aSLionel Sambuc   RECORD(LATE_PARSED_TEMPLATE);
933*0a6a1f1dSLionel Sambuc   RECORD(OPTIMIZE_PRAGMA_OPTIONS);
934f4a2713aSLionel Sambuc 
935f4a2713aSLionel Sambuc   // SourceManager Block.
936f4a2713aSLionel Sambuc   BLOCK(SOURCE_MANAGER_BLOCK);
937f4a2713aSLionel Sambuc   RECORD(SM_SLOC_FILE_ENTRY);
938f4a2713aSLionel Sambuc   RECORD(SM_SLOC_BUFFER_ENTRY);
939f4a2713aSLionel Sambuc   RECORD(SM_SLOC_BUFFER_BLOB);
940f4a2713aSLionel Sambuc   RECORD(SM_SLOC_EXPANSION_ENTRY);
941f4a2713aSLionel Sambuc 
942f4a2713aSLionel Sambuc   // Preprocessor Block.
943f4a2713aSLionel Sambuc   BLOCK(PREPROCESSOR_BLOCK);
944f4a2713aSLionel Sambuc   RECORD(PP_MACRO_OBJECT_LIKE);
945f4a2713aSLionel Sambuc   RECORD(PP_MACRO_FUNCTION_LIKE);
946f4a2713aSLionel Sambuc   RECORD(PP_TOKEN);
947f4a2713aSLionel Sambuc 
948f4a2713aSLionel Sambuc   // Decls and Types block.
949f4a2713aSLionel Sambuc   BLOCK(DECLTYPES_BLOCK);
950f4a2713aSLionel Sambuc   RECORD(TYPE_EXT_QUAL);
951f4a2713aSLionel Sambuc   RECORD(TYPE_COMPLEX);
952f4a2713aSLionel Sambuc   RECORD(TYPE_POINTER);
953f4a2713aSLionel Sambuc   RECORD(TYPE_BLOCK_POINTER);
954f4a2713aSLionel Sambuc   RECORD(TYPE_LVALUE_REFERENCE);
955f4a2713aSLionel Sambuc   RECORD(TYPE_RVALUE_REFERENCE);
956f4a2713aSLionel Sambuc   RECORD(TYPE_MEMBER_POINTER);
957f4a2713aSLionel Sambuc   RECORD(TYPE_CONSTANT_ARRAY);
958f4a2713aSLionel Sambuc   RECORD(TYPE_INCOMPLETE_ARRAY);
959f4a2713aSLionel Sambuc   RECORD(TYPE_VARIABLE_ARRAY);
960f4a2713aSLionel Sambuc   RECORD(TYPE_VECTOR);
961f4a2713aSLionel Sambuc   RECORD(TYPE_EXT_VECTOR);
962f4a2713aSLionel Sambuc   RECORD(TYPE_FUNCTION_NO_PROTO);
963*0a6a1f1dSLionel Sambuc   RECORD(TYPE_FUNCTION_PROTO);
964f4a2713aSLionel Sambuc   RECORD(TYPE_TYPEDEF);
965f4a2713aSLionel Sambuc   RECORD(TYPE_TYPEOF_EXPR);
966f4a2713aSLionel Sambuc   RECORD(TYPE_TYPEOF);
967f4a2713aSLionel Sambuc   RECORD(TYPE_RECORD);
968f4a2713aSLionel Sambuc   RECORD(TYPE_ENUM);
969f4a2713aSLionel Sambuc   RECORD(TYPE_OBJC_INTERFACE);
970f4a2713aSLionel Sambuc   RECORD(TYPE_OBJC_OBJECT_POINTER);
971f4a2713aSLionel Sambuc   RECORD(TYPE_DECLTYPE);
972f4a2713aSLionel Sambuc   RECORD(TYPE_ELABORATED);
973f4a2713aSLionel Sambuc   RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
974f4a2713aSLionel Sambuc   RECORD(TYPE_UNRESOLVED_USING);
975f4a2713aSLionel Sambuc   RECORD(TYPE_INJECTED_CLASS_NAME);
976f4a2713aSLionel Sambuc   RECORD(TYPE_OBJC_OBJECT);
977f4a2713aSLionel Sambuc   RECORD(TYPE_TEMPLATE_TYPE_PARM);
978f4a2713aSLionel Sambuc   RECORD(TYPE_TEMPLATE_SPECIALIZATION);
979f4a2713aSLionel Sambuc   RECORD(TYPE_DEPENDENT_NAME);
980f4a2713aSLionel Sambuc   RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
981f4a2713aSLionel Sambuc   RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
982f4a2713aSLionel Sambuc   RECORD(TYPE_PAREN);
983f4a2713aSLionel Sambuc   RECORD(TYPE_PACK_EXPANSION);
984f4a2713aSLionel Sambuc   RECORD(TYPE_ATTRIBUTED);
985f4a2713aSLionel Sambuc   RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
986*0a6a1f1dSLionel Sambuc   RECORD(TYPE_AUTO);
987*0a6a1f1dSLionel Sambuc   RECORD(TYPE_UNARY_TRANSFORM);
988f4a2713aSLionel Sambuc   RECORD(TYPE_ATOMIC);
989*0a6a1f1dSLionel Sambuc   RECORD(TYPE_DECAYED);
990*0a6a1f1dSLionel Sambuc   RECORD(TYPE_ADJUSTED);
991f4a2713aSLionel Sambuc   RECORD(DECL_TYPEDEF);
992*0a6a1f1dSLionel Sambuc   RECORD(DECL_TYPEALIAS);
993f4a2713aSLionel Sambuc   RECORD(DECL_ENUM);
994f4a2713aSLionel Sambuc   RECORD(DECL_RECORD);
995f4a2713aSLionel Sambuc   RECORD(DECL_ENUM_CONSTANT);
996f4a2713aSLionel Sambuc   RECORD(DECL_FUNCTION);
997f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_METHOD);
998f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_INTERFACE);
999f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_PROTOCOL);
1000f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_IVAR);
1001f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_AT_DEFS_FIELD);
1002f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_CATEGORY);
1003f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_CATEGORY_IMPL);
1004f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_IMPLEMENTATION);
1005f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1006f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_PROPERTY);
1007f4a2713aSLionel Sambuc   RECORD(DECL_OBJC_PROPERTY_IMPL);
1008f4a2713aSLionel Sambuc   RECORD(DECL_FIELD);
1009f4a2713aSLionel Sambuc   RECORD(DECL_MS_PROPERTY);
1010f4a2713aSLionel Sambuc   RECORD(DECL_VAR);
1011f4a2713aSLionel Sambuc   RECORD(DECL_IMPLICIT_PARAM);
1012f4a2713aSLionel Sambuc   RECORD(DECL_PARM_VAR);
1013f4a2713aSLionel Sambuc   RECORD(DECL_FILE_SCOPE_ASM);
1014f4a2713aSLionel Sambuc   RECORD(DECL_BLOCK);
1015f4a2713aSLionel Sambuc   RECORD(DECL_CONTEXT_LEXICAL);
1016f4a2713aSLionel Sambuc   RECORD(DECL_CONTEXT_VISIBLE);
1017f4a2713aSLionel Sambuc   RECORD(DECL_NAMESPACE);
1018f4a2713aSLionel Sambuc   RECORD(DECL_NAMESPACE_ALIAS);
1019f4a2713aSLionel Sambuc   RECORD(DECL_USING);
1020f4a2713aSLionel Sambuc   RECORD(DECL_USING_SHADOW);
1021f4a2713aSLionel Sambuc   RECORD(DECL_USING_DIRECTIVE);
1022f4a2713aSLionel Sambuc   RECORD(DECL_UNRESOLVED_USING_VALUE);
1023f4a2713aSLionel Sambuc   RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1024f4a2713aSLionel Sambuc   RECORD(DECL_LINKAGE_SPEC);
1025f4a2713aSLionel Sambuc   RECORD(DECL_CXX_RECORD);
1026f4a2713aSLionel Sambuc   RECORD(DECL_CXX_METHOD);
1027f4a2713aSLionel Sambuc   RECORD(DECL_CXX_CONSTRUCTOR);
1028f4a2713aSLionel Sambuc   RECORD(DECL_CXX_DESTRUCTOR);
1029f4a2713aSLionel Sambuc   RECORD(DECL_CXX_CONVERSION);
1030f4a2713aSLionel Sambuc   RECORD(DECL_ACCESS_SPEC);
1031f4a2713aSLionel Sambuc   RECORD(DECL_FRIEND);
1032f4a2713aSLionel Sambuc   RECORD(DECL_FRIEND_TEMPLATE);
1033f4a2713aSLionel Sambuc   RECORD(DECL_CLASS_TEMPLATE);
1034f4a2713aSLionel Sambuc   RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1035f4a2713aSLionel Sambuc   RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1036f4a2713aSLionel Sambuc   RECORD(DECL_VAR_TEMPLATE);
1037f4a2713aSLionel Sambuc   RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1038f4a2713aSLionel Sambuc   RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1039f4a2713aSLionel Sambuc   RECORD(DECL_FUNCTION_TEMPLATE);
1040f4a2713aSLionel Sambuc   RECORD(DECL_TEMPLATE_TYPE_PARM);
1041f4a2713aSLionel Sambuc   RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1042f4a2713aSLionel Sambuc   RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1043f4a2713aSLionel Sambuc   RECORD(DECL_STATIC_ASSERT);
1044f4a2713aSLionel Sambuc   RECORD(DECL_CXX_BASE_SPECIFIERS);
1045f4a2713aSLionel Sambuc   RECORD(DECL_INDIRECTFIELD);
1046f4a2713aSLionel Sambuc   RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1047f4a2713aSLionel Sambuc 
1048f4a2713aSLionel Sambuc   // Statements and Exprs can occur in the Decls and Types block.
1049f4a2713aSLionel Sambuc   AddStmtsExprs(Stream, Record);
1050f4a2713aSLionel Sambuc 
1051f4a2713aSLionel Sambuc   BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1052f4a2713aSLionel Sambuc   RECORD(PPD_MACRO_EXPANSION);
1053f4a2713aSLionel Sambuc   RECORD(PPD_MACRO_DEFINITION);
1054f4a2713aSLionel Sambuc   RECORD(PPD_INCLUSION_DIRECTIVE);
1055f4a2713aSLionel Sambuc 
1056f4a2713aSLionel Sambuc #undef RECORD
1057f4a2713aSLionel Sambuc #undef BLOCK
1058f4a2713aSLionel Sambuc   Stream.ExitBlock();
1059f4a2713aSLionel Sambuc }
1060f4a2713aSLionel Sambuc 
1061*0a6a1f1dSLionel Sambuc /// \brief Prepares a path for being written to an AST file by converting it
1062*0a6a1f1dSLionel Sambuc /// to an absolute path and removing nested './'s.
1063*0a6a1f1dSLionel Sambuc ///
1064*0a6a1f1dSLionel Sambuc /// \return \c true if the path was changed.
cleanPathForOutput(FileManager & FileMgr,SmallVectorImpl<char> & Path)1065*0a6a1f1dSLionel Sambuc bool cleanPathForOutput(FileManager &FileMgr, SmallVectorImpl<char> &Path) {
1066*0a6a1f1dSLionel Sambuc   bool Changed = false;
1067*0a6a1f1dSLionel Sambuc 
1068*0a6a1f1dSLionel Sambuc   if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
1069*0a6a1f1dSLionel Sambuc     llvm::sys::fs::make_absolute(Path);
1070*0a6a1f1dSLionel Sambuc     Changed = true;
1071*0a6a1f1dSLionel Sambuc   }
1072*0a6a1f1dSLionel Sambuc 
1073*0a6a1f1dSLionel Sambuc   return Changed | FileMgr.removeDotPaths(Path);
1074*0a6a1f1dSLionel Sambuc }
1075*0a6a1f1dSLionel Sambuc 
1076f4a2713aSLionel Sambuc /// \brief Adjusts the given filename to only write out the portion of the
1077f4a2713aSLionel Sambuc /// filename that is not part of the system root directory.
1078f4a2713aSLionel Sambuc ///
1079f4a2713aSLionel Sambuc /// \param Filename the file name to adjust.
1080f4a2713aSLionel Sambuc ///
1081*0a6a1f1dSLionel Sambuc /// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1082*0a6a1f1dSLionel Sambuc /// the returned filename will be adjusted by this root directory.
1083f4a2713aSLionel Sambuc ///
1084f4a2713aSLionel Sambuc /// \returns either the original filename (if it needs no adjustment) or the
1085f4a2713aSLionel Sambuc /// adjusted filename (which points into the @p Filename parameter).
1086f4a2713aSLionel Sambuc static const char *
adjustFilenameForRelocatableAST(const char * Filename,StringRef BaseDir)1087*0a6a1f1dSLionel Sambuc adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1088f4a2713aSLionel Sambuc   assert(Filename && "No file name to adjust?");
1089f4a2713aSLionel Sambuc 
1090*0a6a1f1dSLionel Sambuc   if (BaseDir.empty())
1091f4a2713aSLionel Sambuc     return Filename;
1092f4a2713aSLionel Sambuc 
1093f4a2713aSLionel Sambuc   // Verify that the filename and the system root have the same prefix.
1094f4a2713aSLionel Sambuc   unsigned Pos = 0;
1095*0a6a1f1dSLionel Sambuc   for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1096*0a6a1f1dSLionel Sambuc     if (Filename[Pos] != BaseDir[Pos])
1097f4a2713aSLionel Sambuc       return Filename; // Prefixes don't match.
1098f4a2713aSLionel Sambuc 
1099f4a2713aSLionel Sambuc   // We hit the end of the filename before we hit the end of the system root.
1100f4a2713aSLionel Sambuc   if (!Filename[Pos])
1101f4a2713aSLionel Sambuc     return Filename;
1102f4a2713aSLionel Sambuc 
1103*0a6a1f1dSLionel Sambuc   // If there's not a path separator at the end of the base directory nor
1104*0a6a1f1dSLionel Sambuc   // immediately after it, then this isn't within the base directory.
1105*0a6a1f1dSLionel Sambuc   if (!llvm::sys::path::is_separator(Filename[Pos])) {
1106*0a6a1f1dSLionel Sambuc     if (!llvm::sys::path::is_separator(BaseDir.back()))
1107*0a6a1f1dSLionel Sambuc       return Filename;
1108*0a6a1f1dSLionel Sambuc   } else {
1109f4a2713aSLionel Sambuc     // If the file name has a '/' at the current position, skip over the '/'.
1110*0a6a1f1dSLionel Sambuc     // We distinguish relative paths from absolute paths by the
1111*0a6a1f1dSLionel Sambuc     // absence of '/' at the beginning of relative paths.
1112*0a6a1f1dSLionel Sambuc     //
1113*0a6a1f1dSLionel Sambuc     // FIXME: This is wrong. We distinguish them by asking if the path is
1114*0a6a1f1dSLionel Sambuc     // absolute, which isn't the same thing. And there might be multiple '/'s
1115*0a6a1f1dSLionel Sambuc     // in a row. Use a better mechanism to indicate whether we have emitted an
1116*0a6a1f1dSLionel Sambuc     // absolute or relative path.
1117f4a2713aSLionel Sambuc     ++Pos;
1118*0a6a1f1dSLionel Sambuc   }
1119f4a2713aSLionel Sambuc 
1120f4a2713aSLionel Sambuc   return Filename + Pos;
1121f4a2713aSLionel Sambuc }
1122f4a2713aSLionel Sambuc 
getSignature()1123*0a6a1f1dSLionel Sambuc static ASTFileSignature getSignature() {
1124*0a6a1f1dSLionel Sambuc   while (1) {
1125*0a6a1f1dSLionel Sambuc     if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1126*0a6a1f1dSLionel Sambuc       return S;
1127*0a6a1f1dSLionel Sambuc     // Rely on GetRandomNumber to eventually return non-zero...
1128*0a6a1f1dSLionel Sambuc   }
1129*0a6a1f1dSLionel Sambuc }
1130*0a6a1f1dSLionel Sambuc 
1131f4a2713aSLionel Sambuc /// \brief Write the control block.
WriteControlBlock(Preprocessor & PP,ASTContext & Context,StringRef isysroot,const std::string & OutputFile)1132f4a2713aSLionel Sambuc void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1133f4a2713aSLionel Sambuc                                   StringRef isysroot,
1134f4a2713aSLionel Sambuc                                   const std::string &OutputFile) {
1135f4a2713aSLionel Sambuc   using namespace llvm;
1136f4a2713aSLionel Sambuc   Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1137f4a2713aSLionel Sambuc   RecordData Record;
1138f4a2713aSLionel Sambuc 
1139f4a2713aSLionel Sambuc   // Metadata
1140f4a2713aSLionel Sambuc   BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1141f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1142f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1143f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1144f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1145f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1146f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1147f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1148f4a2713aSLionel Sambuc   MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1149f4a2713aSLionel Sambuc   unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1150f4a2713aSLionel Sambuc   Record.push_back(METADATA);
1151f4a2713aSLionel Sambuc   Record.push_back(VERSION_MAJOR);
1152f4a2713aSLionel Sambuc   Record.push_back(VERSION_MINOR);
1153f4a2713aSLionel Sambuc   Record.push_back(CLANG_VERSION_MAJOR);
1154f4a2713aSLionel Sambuc   Record.push_back(CLANG_VERSION_MINOR);
1155*0a6a1f1dSLionel Sambuc   assert((!WritingModule || isysroot.empty()) &&
1156*0a6a1f1dSLionel Sambuc          "writing module as a relocatable PCH?");
1157f4a2713aSLionel Sambuc   Record.push_back(!isysroot.empty());
1158f4a2713aSLionel Sambuc   Record.push_back(ASTHasCompilerErrors);
1159f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1160f4a2713aSLionel Sambuc                             getClangFullRepositoryVersion());
1161f4a2713aSLionel Sambuc 
1162*0a6a1f1dSLionel Sambuc   // Signature
1163*0a6a1f1dSLionel Sambuc   Record.clear();
1164*0a6a1f1dSLionel Sambuc   Record.push_back(getSignature());
1165*0a6a1f1dSLionel Sambuc   Stream.EmitRecord(SIGNATURE, Record);
1166*0a6a1f1dSLionel Sambuc 
1167*0a6a1f1dSLionel Sambuc   if (WritingModule) {
1168*0a6a1f1dSLionel Sambuc     // Module name
1169*0a6a1f1dSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1170*0a6a1f1dSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1171*0a6a1f1dSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1172*0a6a1f1dSLionel Sambuc     unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1173*0a6a1f1dSLionel Sambuc     RecordData Record;
1174*0a6a1f1dSLionel Sambuc     Record.push_back(MODULE_NAME);
1175*0a6a1f1dSLionel Sambuc     Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1176*0a6a1f1dSLionel Sambuc   }
1177*0a6a1f1dSLionel Sambuc 
1178*0a6a1f1dSLionel Sambuc   if (WritingModule && WritingModule->Directory) {
1179*0a6a1f1dSLionel Sambuc     // Module directory.
1180*0a6a1f1dSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1181*0a6a1f1dSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1182*0a6a1f1dSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1183*0a6a1f1dSLionel Sambuc     unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1184*0a6a1f1dSLionel Sambuc     RecordData Record;
1185*0a6a1f1dSLionel Sambuc     Record.push_back(MODULE_DIRECTORY);
1186*0a6a1f1dSLionel Sambuc 
1187*0a6a1f1dSLionel Sambuc     SmallString<128> BaseDir(WritingModule->Directory->getName());
1188*0a6a1f1dSLionel Sambuc     cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1189*0a6a1f1dSLionel Sambuc     Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1190*0a6a1f1dSLionel Sambuc 
1191*0a6a1f1dSLionel Sambuc     // Write out all other paths relative to the base directory if possible.
1192*0a6a1f1dSLionel Sambuc     BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1193*0a6a1f1dSLionel Sambuc   } else if (!isysroot.empty()) {
1194*0a6a1f1dSLionel Sambuc     // Write out paths relative to the sysroot if possible.
1195*0a6a1f1dSLionel Sambuc     BaseDirectory = isysroot;
1196*0a6a1f1dSLionel Sambuc   }
1197*0a6a1f1dSLionel Sambuc 
1198*0a6a1f1dSLionel Sambuc   // Module map file
1199*0a6a1f1dSLionel Sambuc   if (WritingModule) {
1200*0a6a1f1dSLionel Sambuc     Record.clear();
1201*0a6a1f1dSLionel Sambuc 
1202*0a6a1f1dSLionel Sambuc     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1203*0a6a1f1dSLionel Sambuc 
1204*0a6a1f1dSLionel Sambuc     // Primary module map file.
1205*0a6a1f1dSLionel Sambuc     AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
1206*0a6a1f1dSLionel Sambuc 
1207*0a6a1f1dSLionel Sambuc     // Additional module map files.
1208*0a6a1f1dSLionel Sambuc     if (auto *AdditionalModMaps =
1209*0a6a1f1dSLionel Sambuc             Map.getAdditionalModuleMapFiles(WritingModule)) {
1210*0a6a1f1dSLionel Sambuc       Record.push_back(AdditionalModMaps->size());
1211*0a6a1f1dSLionel Sambuc       for (const FileEntry *F : *AdditionalModMaps)
1212*0a6a1f1dSLionel Sambuc         AddPath(F->getName(), Record);
1213*0a6a1f1dSLionel Sambuc     } else {
1214*0a6a1f1dSLionel Sambuc       Record.push_back(0);
1215*0a6a1f1dSLionel Sambuc     }
1216*0a6a1f1dSLionel Sambuc 
1217*0a6a1f1dSLionel Sambuc     Stream.EmitRecord(MODULE_MAP_FILE, Record);
1218*0a6a1f1dSLionel Sambuc   }
1219*0a6a1f1dSLionel Sambuc 
1220f4a2713aSLionel Sambuc   // Imports
1221f4a2713aSLionel Sambuc   if (Chain) {
1222f4a2713aSLionel Sambuc     serialization::ModuleManager &Mgr = Chain->getModuleManager();
1223f4a2713aSLionel Sambuc     Record.clear();
1224f4a2713aSLionel Sambuc 
1225f4a2713aSLionel Sambuc     for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1226f4a2713aSLionel Sambuc          M != MEnd; ++M) {
1227f4a2713aSLionel Sambuc       // Skip modules that weren't directly imported.
1228f4a2713aSLionel Sambuc       if (!(*M)->isDirectlyImported())
1229f4a2713aSLionel Sambuc         continue;
1230f4a2713aSLionel Sambuc 
1231f4a2713aSLionel Sambuc       Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1232f4a2713aSLionel Sambuc       AddSourceLocation((*M)->ImportLoc, Record);
1233f4a2713aSLionel Sambuc       Record.push_back((*M)->File->getSize());
1234f4a2713aSLionel Sambuc       Record.push_back((*M)->File->getModificationTime());
1235*0a6a1f1dSLionel Sambuc       Record.push_back((*M)->Signature);
1236*0a6a1f1dSLionel Sambuc       AddPath((*M)->FileName, Record);
1237f4a2713aSLionel Sambuc     }
1238f4a2713aSLionel Sambuc     Stream.EmitRecord(IMPORTS, Record);
1239f4a2713aSLionel Sambuc   }
1240f4a2713aSLionel Sambuc 
1241f4a2713aSLionel Sambuc   // Language options.
1242f4a2713aSLionel Sambuc   Record.clear();
1243f4a2713aSLionel Sambuc   const LangOptions &LangOpts = Context.getLangOpts();
1244f4a2713aSLionel Sambuc #define LANGOPT(Name, Bits, Default, Description) \
1245f4a2713aSLionel Sambuc   Record.push_back(LangOpts.Name);
1246f4a2713aSLionel Sambuc #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1247f4a2713aSLionel Sambuc   Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1248f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.def"
1249*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID)                                                    \
1250*0a6a1f1dSLionel Sambuc   Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1251f4a2713aSLionel Sambuc #include "clang/Basic/Sanitizers.def"
1252f4a2713aSLionel Sambuc 
1253f4a2713aSLionel Sambuc   Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1254f4a2713aSLionel Sambuc   AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1255f4a2713aSLionel Sambuc 
1256f4a2713aSLionel Sambuc   Record.push_back(LangOpts.CurrentModule.size());
1257f4a2713aSLionel Sambuc   Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1258f4a2713aSLionel Sambuc 
1259f4a2713aSLionel Sambuc   // Comment options.
1260f4a2713aSLionel Sambuc   Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1261f4a2713aSLionel Sambuc   for (CommentOptions::BlockCommandNamesTy::const_iterator
1262f4a2713aSLionel Sambuc            I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1263f4a2713aSLionel Sambuc            IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1264f4a2713aSLionel Sambuc        I != IEnd; ++I) {
1265f4a2713aSLionel Sambuc     AddString(*I, Record);
1266f4a2713aSLionel Sambuc   }
1267f4a2713aSLionel Sambuc   Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1268f4a2713aSLionel Sambuc 
1269f4a2713aSLionel Sambuc   Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1270f4a2713aSLionel Sambuc 
1271f4a2713aSLionel Sambuc   // Target options.
1272f4a2713aSLionel Sambuc   Record.clear();
1273f4a2713aSLionel Sambuc   const TargetInfo &Target = Context.getTargetInfo();
1274f4a2713aSLionel Sambuc   const TargetOptions &TargetOpts = Target.getTargetOpts();
1275f4a2713aSLionel Sambuc   AddString(TargetOpts.Triple, Record);
1276f4a2713aSLionel Sambuc   AddString(TargetOpts.CPU, Record);
1277f4a2713aSLionel Sambuc   AddString(TargetOpts.ABI, Record);
1278f4a2713aSLionel Sambuc   Record.push_back(TargetOpts.FeaturesAsWritten.size());
1279f4a2713aSLionel Sambuc   for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1280f4a2713aSLionel Sambuc     AddString(TargetOpts.FeaturesAsWritten[I], Record);
1281f4a2713aSLionel Sambuc   }
1282f4a2713aSLionel Sambuc   Record.push_back(TargetOpts.Features.size());
1283f4a2713aSLionel Sambuc   for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1284f4a2713aSLionel Sambuc     AddString(TargetOpts.Features[I], Record);
1285f4a2713aSLionel Sambuc   }
1286f4a2713aSLionel Sambuc   Stream.EmitRecord(TARGET_OPTIONS, Record);
1287f4a2713aSLionel Sambuc 
1288f4a2713aSLionel Sambuc   // Diagnostic options.
1289f4a2713aSLionel Sambuc   Record.clear();
1290f4a2713aSLionel Sambuc   const DiagnosticOptions &DiagOpts
1291f4a2713aSLionel Sambuc     = Context.getDiagnostics().getDiagnosticOptions();
1292f4a2713aSLionel Sambuc #define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1293f4a2713aSLionel Sambuc #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1294f4a2713aSLionel Sambuc   Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1295f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.def"
1296f4a2713aSLionel Sambuc   Record.push_back(DiagOpts.Warnings.size());
1297f4a2713aSLionel Sambuc   for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1298f4a2713aSLionel Sambuc     AddString(DiagOpts.Warnings[I], Record);
1299*0a6a1f1dSLionel Sambuc   Record.push_back(DiagOpts.Remarks.size());
1300*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1301*0a6a1f1dSLionel Sambuc     AddString(DiagOpts.Remarks[I], Record);
1302f4a2713aSLionel Sambuc   // Note: we don't serialize the log or serialization file names, because they
1303f4a2713aSLionel Sambuc   // are generally transient files and will almost always be overridden.
1304f4a2713aSLionel Sambuc   Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1305f4a2713aSLionel Sambuc 
1306f4a2713aSLionel Sambuc   // File system options.
1307f4a2713aSLionel Sambuc   Record.clear();
1308f4a2713aSLionel Sambuc   const FileSystemOptions &FSOpts
1309f4a2713aSLionel Sambuc     = Context.getSourceManager().getFileManager().getFileSystemOptions();
1310f4a2713aSLionel Sambuc   AddString(FSOpts.WorkingDir, Record);
1311f4a2713aSLionel Sambuc   Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1312f4a2713aSLionel Sambuc 
1313f4a2713aSLionel Sambuc   // Header search options.
1314f4a2713aSLionel Sambuc   Record.clear();
1315f4a2713aSLionel Sambuc   const HeaderSearchOptions &HSOpts
1316f4a2713aSLionel Sambuc     = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1317f4a2713aSLionel Sambuc   AddString(HSOpts.Sysroot, Record);
1318f4a2713aSLionel Sambuc 
1319f4a2713aSLionel Sambuc   // Include entries.
1320f4a2713aSLionel Sambuc   Record.push_back(HSOpts.UserEntries.size());
1321f4a2713aSLionel Sambuc   for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1322f4a2713aSLionel Sambuc     const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1323f4a2713aSLionel Sambuc     AddString(Entry.Path, Record);
1324f4a2713aSLionel Sambuc     Record.push_back(static_cast<unsigned>(Entry.Group));
1325f4a2713aSLionel Sambuc     Record.push_back(Entry.IsFramework);
1326f4a2713aSLionel Sambuc     Record.push_back(Entry.IgnoreSysRoot);
1327f4a2713aSLionel Sambuc   }
1328f4a2713aSLionel Sambuc 
1329f4a2713aSLionel Sambuc   // System header prefixes.
1330f4a2713aSLionel Sambuc   Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1331f4a2713aSLionel Sambuc   for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1332f4a2713aSLionel Sambuc     AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1333f4a2713aSLionel Sambuc     Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1334f4a2713aSLionel Sambuc   }
1335f4a2713aSLionel Sambuc 
1336f4a2713aSLionel Sambuc   AddString(HSOpts.ResourceDir, Record);
1337f4a2713aSLionel Sambuc   AddString(HSOpts.ModuleCachePath, Record);
1338*0a6a1f1dSLionel Sambuc   AddString(HSOpts.ModuleUserBuildPath, Record);
1339f4a2713aSLionel Sambuc   Record.push_back(HSOpts.DisableModuleHash);
1340f4a2713aSLionel Sambuc   Record.push_back(HSOpts.UseBuiltinIncludes);
1341f4a2713aSLionel Sambuc   Record.push_back(HSOpts.UseStandardSystemIncludes);
1342f4a2713aSLionel Sambuc   Record.push_back(HSOpts.UseStandardCXXIncludes);
1343f4a2713aSLionel Sambuc   Record.push_back(HSOpts.UseLibcxx);
1344f4a2713aSLionel Sambuc   Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1345f4a2713aSLionel Sambuc 
1346f4a2713aSLionel Sambuc   // Preprocessor options.
1347f4a2713aSLionel Sambuc   Record.clear();
1348f4a2713aSLionel Sambuc   const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1349f4a2713aSLionel Sambuc 
1350f4a2713aSLionel Sambuc   // Macro definitions.
1351f4a2713aSLionel Sambuc   Record.push_back(PPOpts.Macros.size());
1352f4a2713aSLionel Sambuc   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1353f4a2713aSLionel Sambuc     AddString(PPOpts.Macros[I].first, Record);
1354f4a2713aSLionel Sambuc     Record.push_back(PPOpts.Macros[I].second);
1355f4a2713aSLionel Sambuc   }
1356f4a2713aSLionel Sambuc 
1357f4a2713aSLionel Sambuc   // Includes
1358f4a2713aSLionel Sambuc   Record.push_back(PPOpts.Includes.size());
1359f4a2713aSLionel Sambuc   for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1360f4a2713aSLionel Sambuc     AddString(PPOpts.Includes[I], Record);
1361f4a2713aSLionel Sambuc 
1362f4a2713aSLionel Sambuc   // Macro includes
1363f4a2713aSLionel Sambuc   Record.push_back(PPOpts.MacroIncludes.size());
1364f4a2713aSLionel Sambuc   for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1365f4a2713aSLionel Sambuc     AddString(PPOpts.MacroIncludes[I], Record);
1366f4a2713aSLionel Sambuc 
1367f4a2713aSLionel Sambuc   Record.push_back(PPOpts.UsePredefines);
1368f4a2713aSLionel Sambuc   // Detailed record is important since it is used for the module cache hash.
1369f4a2713aSLionel Sambuc   Record.push_back(PPOpts.DetailedRecord);
1370f4a2713aSLionel Sambuc   AddString(PPOpts.ImplicitPCHInclude, Record);
1371f4a2713aSLionel Sambuc   AddString(PPOpts.ImplicitPTHInclude, Record);
1372f4a2713aSLionel Sambuc   Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1373f4a2713aSLionel Sambuc   Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1374f4a2713aSLionel Sambuc 
1375f4a2713aSLionel Sambuc   // Original file name and file ID
1376f4a2713aSLionel Sambuc   SourceManager &SM = Context.getSourceManager();
1377f4a2713aSLionel Sambuc   if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1378f4a2713aSLionel Sambuc     BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
1379f4a2713aSLionel Sambuc     FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1380f4a2713aSLionel Sambuc     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1381f4a2713aSLionel Sambuc     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1382f4a2713aSLionel Sambuc     unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1383f4a2713aSLionel Sambuc 
1384f4a2713aSLionel Sambuc     Record.clear();
1385f4a2713aSLionel Sambuc     Record.push_back(ORIGINAL_FILE);
1386f4a2713aSLionel Sambuc     Record.push_back(SM.getMainFileID().getOpaqueValue());
1387*0a6a1f1dSLionel Sambuc     EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1388f4a2713aSLionel Sambuc   }
1389f4a2713aSLionel Sambuc 
1390f4a2713aSLionel Sambuc   Record.clear();
1391f4a2713aSLionel Sambuc   Record.push_back(SM.getMainFileID().getOpaqueValue());
1392f4a2713aSLionel Sambuc   Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1393f4a2713aSLionel Sambuc 
1394f4a2713aSLionel Sambuc   // Original PCH directory
1395f4a2713aSLionel Sambuc   if (!OutputFile.empty() && OutputFile != "-") {
1396f4a2713aSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1397f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1398f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1399f4a2713aSLionel Sambuc     unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1400f4a2713aSLionel Sambuc 
1401f4a2713aSLionel Sambuc     SmallString<128> OutputPath(OutputFile);
1402f4a2713aSLionel Sambuc 
1403f4a2713aSLionel Sambuc     llvm::sys::fs::make_absolute(OutputPath);
1404f4a2713aSLionel Sambuc     StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1405f4a2713aSLionel Sambuc 
1406f4a2713aSLionel Sambuc     RecordData Record;
1407f4a2713aSLionel Sambuc     Record.push_back(ORIGINAL_PCH_DIR);
1408f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1409f4a2713aSLionel Sambuc   }
1410f4a2713aSLionel Sambuc 
1411f4a2713aSLionel Sambuc   WriteInputFiles(Context.SourceMgr,
1412f4a2713aSLionel Sambuc                   PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1413f4a2713aSLionel Sambuc                   PP.getLangOpts().Modules);
1414f4a2713aSLionel Sambuc   Stream.ExitBlock();
1415f4a2713aSLionel Sambuc }
1416f4a2713aSLionel Sambuc 
1417f4a2713aSLionel Sambuc namespace  {
1418f4a2713aSLionel Sambuc   /// \brief An input file.
1419f4a2713aSLionel Sambuc   struct InputFileEntry {
1420f4a2713aSLionel Sambuc     const FileEntry *File;
1421f4a2713aSLionel Sambuc     bool IsSystemFile;
1422f4a2713aSLionel Sambuc     bool BufferOverridden;
1423f4a2713aSLionel Sambuc   };
1424f4a2713aSLionel Sambuc }
1425f4a2713aSLionel Sambuc 
WriteInputFiles(SourceManager & SourceMgr,HeaderSearchOptions & HSOpts,bool Modules)1426f4a2713aSLionel Sambuc void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1427f4a2713aSLionel Sambuc                                 HeaderSearchOptions &HSOpts,
1428f4a2713aSLionel Sambuc                                 bool Modules) {
1429f4a2713aSLionel Sambuc   using namespace llvm;
1430f4a2713aSLionel Sambuc   Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1431f4a2713aSLionel Sambuc   RecordData Record;
1432f4a2713aSLionel Sambuc 
1433f4a2713aSLionel Sambuc   // Create input-file abbreviation.
1434f4a2713aSLionel Sambuc   BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1435f4a2713aSLionel Sambuc   IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1436f4a2713aSLionel Sambuc   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1437f4a2713aSLionel Sambuc   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1438f4a2713aSLionel Sambuc   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1439f4a2713aSLionel Sambuc   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1440f4a2713aSLionel Sambuc   IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1441f4a2713aSLionel Sambuc   unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1442f4a2713aSLionel Sambuc 
1443f4a2713aSLionel Sambuc   // Get all ContentCache objects for files, sorted by whether the file is a
1444f4a2713aSLionel Sambuc   // system one or not. System files go at the back, users files at the front.
1445f4a2713aSLionel Sambuc   std::deque<InputFileEntry> SortedFiles;
1446f4a2713aSLionel Sambuc   for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1447f4a2713aSLionel Sambuc     // Get this source location entry.
1448f4a2713aSLionel Sambuc     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1449f4a2713aSLionel Sambuc     assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1450f4a2713aSLionel Sambuc 
1451f4a2713aSLionel Sambuc     // We only care about file entries that were not overridden.
1452f4a2713aSLionel Sambuc     if (!SLoc->isFile())
1453f4a2713aSLionel Sambuc       continue;
1454f4a2713aSLionel Sambuc     const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1455f4a2713aSLionel Sambuc     if (!Cache->OrigEntry)
1456f4a2713aSLionel Sambuc       continue;
1457f4a2713aSLionel Sambuc 
1458f4a2713aSLionel Sambuc     InputFileEntry Entry;
1459f4a2713aSLionel Sambuc     Entry.File = Cache->OrigEntry;
1460f4a2713aSLionel Sambuc     Entry.IsSystemFile = Cache->IsSystemFile;
1461f4a2713aSLionel Sambuc     Entry.BufferOverridden = Cache->BufferOverridden;
1462f4a2713aSLionel Sambuc     if (Cache->IsSystemFile)
1463f4a2713aSLionel Sambuc       SortedFiles.push_back(Entry);
1464f4a2713aSLionel Sambuc     else
1465f4a2713aSLionel Sambuc       SortedFiles.push_front(Entry);
1466f4a2713aSLionel Sambuc   }
1467f4a2713aSLionel Sambuc 
1468f4a2713aSLionel Sambuc   unsigned UserFilesNum = 0;
1469f4a2713aSLionel Sambuc   // Write out all of the input files.
1470f4a2713aSLionel Sambuc   std::vector<uint32_t> InputFileOffsets;
1471f4a2713aSLionel Sambuc   for (std::deque<InputFileEntry>::iterator
1472f4a2713aSLionel Sambuc          I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
1473f4a2713aSLionel Sambuc     const InputFileEntry &Entry = *I;
1474f4a2713aSLionel Sambuc 
1475f4a2713aSLionel Sambuc     uint32_t &InputFileID = InputFileIDs[Entry.File];
1476f4a2713aSLionel Sambuc     if (InputFileID != 0)
1477f4a2713aSLionel Sambuc       continue; // already recorded this file.
1478f4a2713aSLionel Sambuc 
1479f4a2713aSLionel Sambuc     // Record this entry's offset.
1480f4a2713aSLionel Sambuc     InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1481f4a2713aSLionel Sambuc 
1482f4a2713aSLionel Sambuc     InputFileID = InputFileOffsets.size();
1483f4a2713aSLionel Sambuc 
1484f4a2713aSLionel Sambuc     if (!Entry.IsSystemFile)
1485f4a2713aSLionel Sambuc       ++UserFilesNum;
1486f4a2713aSLionel Sambuc 
1487f4a2713aSLionel Sambuc     Record.clear();
1488f4a2713aSLionel Sambuc     Record.push_back(INPUT_FILE);
1489f4a2713aSLionel Sambuc     Record.push_back(InputFileOffsets.size());
1490f4a2713aSLionel Sambuc 
1491f4a2713aSLionel Sambuc     // Emit size/modification time for this file.
1492f4a2713aSLionel Sambuc     Record.push_back(Entry.File->getSize());
1493f4a2713aSLionel Sambuc     Record.push_back(Entry.File->getModificationTime());
1494f4a2713aSLionel Sambuc 
1495f4a2713aSLionel Sambuc     // Whether this file was overridden.
1496f4a2713aSLionel Sambuc     Record.push_back(Entry.BufferOverridden);
1497f4a2713aSLionel Sambuc 
1498*0a6a1f1dSLionel Sambuc     EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1499f4a2713aSLionel Sambuc   }
1500f4a2713aSLionel Sambuc 
1501f4a2713aSLionel Sambuc   Stream.ExitBlock();
1502f4a2713aSLionel Sambuc 
1503f4a2713aSLionel Sambuc   // Create input file offsets abbreviation.
1504f4a2713aSLionel Sambuc   BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1505f4a2713aSLionel Sambuc   OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1506f4a2713aSLionel Sambuc   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1507f4a2713aSLionel Sambuc   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1508f4a2713aSLionel Sambuc                                                                 //   input files
1509f4a2713aSLionel Sambuc   OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));   // Array
1510f4a2713aSLionel Sambuc   unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1511f4a2713aSLionel Sambuc 
1512f4a2713aSLionel Sambuc   // Write input file offsets.
1513f4a2713aSLionel Sambuc   Record.clear();
1514f4a2713aSLionel Sambuc   Record.push_back(INPUT_FILE_OFFSETS);
1515f4a2713aSLionel Sambuc   Record.push_back(InputFileOffsets.size());
1516f4a2713aSLionel Sambuc   Record.push_back(UserFilesNum);
1517f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
1518f4a2713aSLionel Sambuc }
1519f4a2713aSLionel Sambuc 
1520f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1521f4a2713aSLionel Sambuc // Source Manager Serialization
1522f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1523f4a2713aSLionel Sambuc 
1524f4a2713aSLionel Sambuc /// \brief Create an abbreviation for the SLocEntry that refers to a
1525f4a2713aSLionel Sambuc /// file.
CreateSLocFileAbbrev(llvm::BitstreamWriter & Stream)1526f4a2713aSLionel Sambuc static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1527f4a2713aSLionel Sambuc   using namespace llvm;
1528f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1529f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1530f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1531f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1532f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1533f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1534f4a2713aSLionel Sambuc   // FileEntry fields.
1535f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1536f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1537f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1538f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1539f4a2713aSLionel Sambuc   return Stream.EmitAbbrev(Abbrev);
1540f4a2713aSLionel Sambuc }
1541f4a2713aSLionel Sambuc 
1542f4a2713aSLionel Sambuc /// \brief Create an abbreviation for the SLocEntry that refers to a
1543f4a2713aSLionel Sambuc /// buffer.
CreateSLocBufferAbbrev(llvm::BitstreamWriter & Stream)1544f4a2713aSLionel Sambuc static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1545f4a2713aSLionel Sambuc   using namespace llvm;
1546f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1547f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1548f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1549f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1550f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1551f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1552f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1553f4a2713aSLionel Sambuc   return Stream.EmitAbbrev(Abbrev);
1554f4a2713aSLionel Sambuc }
1555f4a2713aSLionel Sambuc 
1556f4a2713aSLionel Sambuc /// \brief Create an abbreviation for the SLocEntry that refers to a
1557f4a2713aSLionel Sambuc /// buffer's blob.
CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter & Stream)1558f4a2713aSLionel Sambuc static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
1559f4a2713aSLionel Sambuc   using namespace llvm;
1560f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1561f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
1562f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1563f4a2713aSLionel Sambuc   return Stream.EmitAbbrev(Abbrev);
1564f4a2713aSLionel Sambuc }
1565f4a2713aSLionel Sambuc 
1566f4a2713aSLionel Sambuc /// \brief Create an abbreviation for the SLocEntry that refers to a macro
1567f4a2713aSLionel Sambuc /// expansion.
CreateSLocExpansionAbbrev(llvm::BitstreamWriter & Stream)1568f4a2713aSLionel Sambuc static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1569f4a2713aSLionel Sambuc   using namespace llvm;
1570f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1571f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1572f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1573f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1574f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1575f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1576f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1577f4a2713aSLionel Sambuc   return Stream.EmitAbbrev(Abbrev);
1578f4a2713aSLionel Sambuc }
1579f4a2713aSLionel Sambuc 
1580f4a2713aSLionel Sambuc namespace {
1581f4a2713aSLionel Sambuc   // Trait used for the on-disk hash table of header search information.
1582f4a2713aSLionel Sambuc   class HeaderFileInfoTrait {
1583f4a2713aSLionel Sambuc     ASTWriter &Writer;
1584f4a2713aSLionel Sambuc     const HeaderSearch &HS;
1585f4a2713aSLionel Sambuc 
1586f4a2713aSLionel Sambuc     // Keep track of the framework names we've used during serialization.
1587f4a2713aSLionel Sambuc     SmallVector<char, 128> FrameworkStringData;
1588f4a2713aSLionel Sambuc     llvm::StringMap<unsigned> FrameworkNameOffset;
1589f4a2713aSLionel Sambuc 
1590f4a2713aSLionel Sambuc   public:
HeaderFileInfoTrait(ASTWriter & Writer,const HeaderSearch & HS)1591f4a2713aSLionel Sambuc     HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1592f4a2713aSLionel Sambuc       : Writer(Writer), HS(HS) { }
1593f4a2713aSLionel Sambuc 
1594f4a2713aSLionel Sambuc     struct key_type {
1595f4a2713aSLionel Sambuc       const FileEntry *FE;
1596f4a2713aSLionel Sambuc       const char *Filename;
1597f4a2713aSLionel Sambuc     };
1598f4a2713aSLionel Sambuc     typedef const key_type &key_type_ref;
1599f4a2713aSLionel Sambuc 
1600f4a2713aSLionel Sambuc     typedef HeaderFileInfo data_type;
1601f4a2713aSLionel Sambuc     typedef const data_type &data_type_ref;
1602*0a6a1f1dSLionel Sambuc     typedef unsigned hash_value_type;
1603*0a6a1f1dSLionel Sambuc     typedef unsigned offset_type;
1604f4a2713aSLionel Sambuc 
ComputeHash(key_type_ref key)1605*0a6a1f1dSLionel Sambuc     static hash_value_type ComputeHash(key_type_ref key) {
1606f4a2713aSLionel Sambuc       // The hash is based only on size/time of the file, so that the reader can
1607f4a2713aSLionel Sambuc       // match even when symlinking or excess path elements ("foo/../", "../")
1608f4a2713aSLionel Sambuc       // change the form of the name. However, complete path is still the key.
1609*0a6a1f1dSLionel Sambuc       //
1610*0a6a1f1dSLionel Sambuc       // FIXME: Using the mtime here will cause problems for explicit module
1611*0a6a1f1dSLionel Sambuc       // imports.
1612f4a2713aSLionel Sambuc       return llvm::hash_combine(key.FE->getSize(),
1613f4a2713aSLionel Sambuc                                 key.FE->getModificationTime());
1614f4a2713aSLionel Sambuc     }
1615f4a2713aSLionel Sambuc 
1616f4a2713aSLionel Sambuc     std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,key_type_ref key,data_type_ref Data)1617f4a2713aSLionel Sambuc     EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1618*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
1619*0a6a1f1dSLionel Sambuc       endian::Writer<little> Writer(Out);
1620f4a2713aSLionel Sambuc       unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1621*0a6a1f1dSLionel Sambuc       Writer.write<uint16_t>(KeyLen);
1622f4a2713aSLionel Sambuc       unsigned DataLen = 1 + 2 + 4 + 4;
1623f4a2713aSLionel Sambuc       if (Data.isModuleHeader)
1624f4a2713aSLionel Sambuc         DataLen += 4;
1625*0a6a1f1dSLionel Sambuc       Writer.write<uint8_t>(DataLen);
1626f4a2713aSLionel Sambuc       return std::make_pair(KeyLen, DataLen);
1627f4a2713aSLionel Sambuc     }
1628f4a2713aSLionel Sambuc 
EmitKey(raw_ostream & Out,key_type_ref key,unsigned KeyLen)1629f4a2713aSLionel Sambuc     void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1630*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
1631*0a6a1f1dSLionel Sambuc       endian::Writer<little> LE(Out);
1632*0a6a1f1dSLionel Sambuc       LE.write<uint64_t>(key.FE->getSize());
1633f4a2713aSLionel Sambuc       KeyLen -= 8;
1634*0a6a1f1dSLionel Sambuc       LE.write<uint64_t>(key.FE->getModificationTime());
1635f4a2713aSLionel Sambuc       KeyLen -= 8;
1636f4a2713aSLionel Sambuc       Out.write(key.Filename, KeyLen);
1637f4a2713aSLionel Sambuc     }
1638f4a2713aSLionel Sambuc 
EmitData(raw_ostream & Out,key_type_ref key,data_type_ref Data,unsigned DataLen)1639f4a2713aSLionel Sambuc     void EmitData(raw_ostream &Out, key_type_ref key,
1640f4a2713aSLionel Sambuc                   data_type_ref Data, unsigned DataLen) {
1641*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
1642*0a6a1f1dSLionel Sambuc       endian::Writer<little> LE(Out);
1643f4a2713aSLionel Sambuc       uint64_t Start = Out.tell(); (void)Start;
1644f4a2713aSLionel Sambuc 
1645f4a2713aSLionel Sambuc       unsigned char Flags = (Data.HeaderRole << 6)
1646f4a2713aSLionel Sambuc                           | (Data.isImport << 5)
1647f4a2713aSLionel Sambuc                           | (Data.isPragmaOnce << 4)
1648f4a2713aSLionel Sambuc                           | (Data.DirInfo << 2)
1649f4a2713aSLionel Sambuc                           | (Data.Resolved << 1)
1650f4a2713aSLionel Sambuc                           | Data.IndexHeaderMapHeader;
1651*0a6a1f1dSLionel Sambuc       LE.write<uint8_t>(Flags);
1652*0a6a1f1dSLionel Sambuc       LE.write<uint16_t>(Data.NumIncludes);
1653f4a2713aSLionel Sambuc 
1654f4a2713aSLionel Sambuc       if (!Data.ControllingMacro)
1655*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(Data.ControllingMacroID);
1656f4a2713aSLionel Sambuc       else
1657*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
1658f4a2713aSLionel Sambuc 
1659f4a2713aSLionel Sambuc       unsigned Offset = 0;
1660f4a2713aSLionel Sambuc       if (!Data.Framework.empty()) {
1661f4a2713aSLionel Sambuc         // If this header refers into a framework, save the framework name.
1662f4a2713aSLionel Sambuc         llvm::StringMap<unsigned>::iterator Pos
1663f4a2713aSLionel Sambuc           = FrameworkNameOffset.find(Data.Framework);
1664f4a2713aSLionel Sambuc         if (Pos == FrameworkNameOffset.end()) {
1665f4a2713aSLionel Sambuc           Offset = FrameworkStringData.size() + 1;
1666f4a2713aSLionel Sambuc           FrameworkStringData.append(Data.Framework.begin(),
1667f4a2713aSLionel Sambuc                                      Data.Framework.end());
1668f4a2713aSLionel Sambuc           FrameworkStringData.push_back(0);
1669f4a2713aSLionel Sambuc 
1670f4a2713aSLionel Sambuc           FrameworkNameOffset[Data.Framework] = Offset;
1671f4a2713aSLionel Sambuc         } else
1672f4a2713aSLionel Sambuc           Offset = Pos->second;
1673f4a2713aSLionel Sambuc       }
1674*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Offset);
1675f4a2713aSLionel Sambuc 
1676f4a2713aSLionel Sambuc       if (Data.isModuleHeader) {
1677f4a2713aSLionel Sambuc         Module *Mod = HS.findModuleForHeader(key.FE).getModule();
1678*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
1679f4a2713aSLionel Sambuc       }
1680f4a2713aSLionel Sambuc 
1681f4a2713aSLionel Sambuc       assert(Out.tell() - Start == DataLen && "Wrong data length");
1682f4a2713aSLionel Sambuc     }
1683f4a2713aSLionel Sambuc 
strings_begin() const1684f4a2713aSLionel Sambuc     const char *strings_begin() const { return FrameworkStringData.begin(); }
strings_end() const1685f4a2713aSLionel Sambuc     const char *strings_end() const { return FrameworkStringData.end(); }
1686f4a2713aSLionel Sambuc   };
1687f4a2713aSLionel Sambuc } // end anonymous namespace
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc /// \brief Write the header search block for the list of files that
1690f4a2713aSLionel Sambuc ///
1691f4a2713aSLionel Sambuc /// \param HS The header search structure to save.
WriteHeaderSearch(const HeaderSearch & HS)1692*0a6a1f1dSLionel Sambuc void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1693f4a2713aSLionel Sambuc   SmallVector<const FileEntry *, 16> FilesByUID;
1694f4a2713aSLionel Sambuc   HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1695f4a2713aSLionel Sambuc 
1696f4a2713aSLionel Sambuc   if (FilesByUID.size() > HS.header_file_size())
1697f4a2713aSLionel Sambuc     FilesByUID.resize(HS.header_file_size());
1698f4a2713aSLionel Sambuc 
1699f4a2713aSLionel Sambuc   HeaderFileInfoTrait GeneratorTrait(*this, HS);
1700*0a6a1f1dSLionel Sambuc   llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1701f4a2713aSLionel Sambuc   SmallVector<const char *, 4> SavedStrings;
1702f4a2713aSLionel Sambuc   unsigned NumHeaderSearchEntries = 0;
1703f4a2713aSLionel Sambuc   for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1704f4a2713aSLionel Sambuc     const FileEntry *File = FilesByUID[UID];
1705f4a2713aSLionel Sambuc     if (!File)
1706f4a2713aSLionel Sambuc       continue;
1707f4a2713aSLionel Sambuc 
1708f4a2713aSLionel Sambuc     // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1709f4a2713aSLionel Sambuc     // from the external source if it was not provided already.
1710*0a6a1f1dSLionel Sambuc     HeaderFileInfo HFI;
1711*0a6a1f1dSLionel Sambuc     if (!HS.tryGetFileInfo(File, HFI) ||
1712*0a6a1f1dSLionel Sambuc         (HFI.External && Chain) ||
1713*0a6a1f1dSLionel Sambuc         (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
1714f4a2713aSLionel Sambuc       continue;
1715f4a2713aSLionel Sambuc 
1716*0a6a1f1dSLionel Sambuc     // Massage the file path into an appropriate form.
1717f4a2713aSLionel Sambuc     const char *Filename = File->getName();
1718*0a6a1f1dSLionel Sambuc     SmallString<128> FilenameTmp(Filename);
1719*0a6a1f1dSLionel Sambuc     if (PreparePathForOutput(FilenameTmp)) {
1720f4a2713aSLionel Sambuc       // If we performed any translation on the file name at all, we need to
1721f4a2713aSLionel Sambuc       // save this string, since the generator will refer to it later.
1722*0a6a1f1dSLionel Sambuc       Filename = strdup(FilenameTmp.c_str());
1723f4a2713aSLionel Sambuc       SavedStrings.push_back(Filename);
1724f4a2713aSLionel Sambuc     }
1725f4a2713aSLionel Sambuc 
1726f4a2713aSLionel Sambuc     HeaderFileInfoTrait::key_type key = { File, Filename };
1727f4a2713aSLionel Sambuc     Generator.insert(key, HFI, GeneratorTrait);
1728f4a2713aSLionel Sambuc     ++NumHeaderSearchEntries;
1729f4a2713aSLionel Sambuc   }
1730f4a2713aSLionel Sambuc 
1731f4a2713aSLionel Sambuc   // Create the on-disk hash table in a buffer.
1732f4a2713aSLionel Sambuc   SmallString<4096> TableData;
1733f4a2713aSLionel Sambuc   uint32_t BucketOffset;
1734f4a2713aSLionel Sambuc   {
1735*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
1736f4a2713aSLionel Sambuc     llvm::raw_svector_ostream Out(TableData);
1737f4a2713aSLionel Sambuc     // Make sure that no bucket is at offset 0
1738*0a6a1f1dSLionel Sambuc     endian::Writer<little>(Out).write<uint32_t>(0);
1739f4a2713aSLionel Sambuc     BucketOffset = Generator.Emit(Out, GeneratorTrait);
1740f4a2713aSLionel Sambuc   }
1741f4a2713aSLionel Sambuc 
1742f4a2713aSLionel Sambuc   // Create a blob abbreviation
1743f4a2713aSLionel Sambuc   using namespace llvm;
1744f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1745f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1746f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1747f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1748f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1749f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1750f4a2713aSLionel Sambuc   unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1751f4a2713aSLionel Sambuc 
1752f4a2713aSLionel Sambuc   // Write the header search table
1753f4a2713aSLionel Sambuc   RecordData Record;
1754f4a2713aSLionel Sambuc   Record.push_back(HEADER_SEARCH_TABLE);
1755f4a2713aSLionel Sambuc   Record.push_back(BucketOffset);
1756f4a2713aSLionel Sambuc   Record.push_back(NumHeaderSearchEntries);
1757f4a2713aSLionel Sambuc   Record.push_back(TableData.size());
1758f4a2713aSLionel Sambuc   TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
1759f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1760f4a2713aSLionel Sambuc 
1761f4a2713aSLionel Sambuc   // Free all of the strings we had to duplicate.
1762f4a2713aSLionel Sambuc   for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1763f4a2713aSLionel Sambuc     free(const_cast<char *>(SavedStrings[I]));
1764f4a2713aSLionel Sambuc }
1765f4a2713aSLionel Sambuc 
1766f4a2713aSLionel Sambuc /// \brief Writes the block containing the serialized form of the
1767f4a2713aSLionel Sambuc /// source manager.
1768f4a2713aSLionel Sambuc ///
1769f4a2713aSLionel Sambuc /// TODO: We should probably use an on-disk hash table (stored in a
1770f4a2713aSLionel Sambuc /// blob), indexed based on the file name, so that we only create
1771f4a2713aSLionel Sambuc /// entries for files that we actually need. In the common case (no
1772f4a2713aSLionel Sambuc /// errors), we probably won't have to create file entries for any of
1773f4a2713aSLionel Sambuc /// the files in the AST.
WriteSourceManagerBlock(SourceManager & SourceMgr,const Preprocessor & PP)1774f4a2713aSLionel Sambuc void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1775*0a6a1f1dSLionel Sambuc                                         const Preprocessor &PP) {
1776f4a2713aSLionel Sambuc   RecordData Record;
1777f4a2713aSLionel Sambuc 
1778f4a2713aSLionel Sambuc   // Enter the source manager block.
1779f4a2713aSLionel Sambuc   Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
1780f4a2713aSLionel Sambuc 
1781f4a2713aSLionel Sambuc   // Abbreviations for the various kinds of source-location entries.
1782f4a2713aSLionel Sambuc   unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1783f4a2713aSLionel Sambuc   unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1784f4a2713aSLionel Sambuc   unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1785f4a2713aSLionel Sambuc   unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
1786f4a2713aSLionel Sambuc 
1787f4a2713aSLionel Sambuc   // Write out the source location entry table. We skip the first
1788f4a2713aSLionel Sambuc   // entry, which is always the same dummy entry.
1789f4a2713aSLionel Sambuc   std::vector<uint32_t> SLocEntryOffsets;
1790f4a2713aSLionel Sambuc   RecordData PreloadSLocs;
1791f4a2713aSLionel Sambuc   SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1792f4a2713aSLionel Sambuc   for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
1793f4a2713aSLionel Sambuc        I != N; ++I) {
1794f4a2713aSLionel Sambuc     // Get this source location entry.
1795f4a2713aSLionel Sambuc     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1796f4a2713aSLionel Sambuc     FileID FID = FileID::get(I);
1797f4a2713aSLionel Sambuc     assert(&SourceMgr.getSLocEntry(FID) == SLoc);
1798f4a2713aSLionel Sambuc 
1799f4a2713aSLionel Sambuc     // Record the offset of this source-location entry.
1800f4a2713aSLionel Sambuc     SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1801f4a2713aSLionel Sambuc 
1802f4a2713aSLionel Sambuc     // Figure out which record code to use.
1803f4a2713aSLionel Sambuc     unsigned Code;
1804f4a2713aSLionel Sambuc     if (SLoc->isFile()) {
1805f4a2713aSLionel Sambuc       const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1806f4a2713aSLionel Sambuc       if (Cache->OrigEntry) {
1807f4a2713aSLionel Sambuc         Code = SM_SLOC_FILE_ENTRY;
1808f4a2713aSLionel Sambuc       } else
1809f4a2713aSLionel Sambuc         Code = SM_SLOC_BUFFER_ENTRY;
1810f4a2713aSLionel Sambuc     } else
1811f4a2713aSLionel Sambuc       Code = SM_SLOC_EXPANSION_ENTRY;
1812f4a2713aSLionel Sambuc     Record.clear();
1813f4a2713aSLionel Sambuc     Record.push_back(Code);
1814f4a2713aSLionel Sambuc 
1815f4a2713aSLionel Sambuc     // Starting offset of this entry within this module, so skip the dummy.
1816f4a2713aSLionel Sambuc     Record.push_back(SLoc->getOffset() - 2);
1817f4a2713aSLionel Sambuc     if (SLoc->isFile()) {
1818f4a2713aSLionel Sambuc       const SrcMgr::FileInfo &File = SLoc->getFile();
1819f4a2713aSLionel Sambuc       Record.push_back(File.getIncludeLoc().getRawEncoding());
1820f4a2713aSLionel Sambuc       Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1821f4a2713aSLionel Sambuc       Record.push_back(File.hasLineDirectives());
1822f4a2713aSLionel Sambuc 
1823f4a2713aSLionel Sambuc       const SrcMgr::ContentCache *Content = File.getContentCache();
1824f4a2713aSLionel Sambuc       if (Content->OrigEntry) {
1825f4a2713aSLionel Sambuc         assert(Content->OrigEntry == Content->ContentsEntry &&
1826f4a2713aSLionel Sambuc                "Writing to AST an overridden file is not supported");
1827f4a2713aSLionel Sambuc 
1828f4a2713aSLionel Sambuc         // The source location entry is a file. Emit input file ID.
1829f4a2713aSLionel Sambuc         assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1830f4a2713aSLionel Sambuc         Record.push_back(InputFileIDs[Content->OrigEntry]);
1831f4a2713aSLionel Sambuc 
1832f4a2713aSLionel Sambuc         Record.push_back(File.NumCreatedFIDs);
1833f4a2713aSLionel Sambuc 
1834f4a2713aSLionel Sambuc         FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
1835f4a2713aSLionel Sambuc         if (FDI != FileDeclIDs.end()) {
1836f4a2713aSLionel Sambuc           Record.push_back(FDI->second->FirstDeclIndex);
1837f4a2713aSLionel Sambuc           Record.push_back(FDI->second->DeclIDs.size());
1838f4a2713aSLionel Sambuc         } else {
1839f4a2713aSLionel Sambuc           Record.push_back(0);
1840f4a2713aSLionel Sambuc           Record.push_back(0);
1841f4a2713aSLionel Sambuc         }
1842f4a2713aSLionel Sambuc 
1843f4a2713aSLionel Sambuc         Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
1844f4a2713aSLionel Sambuc 
1845f4a2713aSLionel Sambuc         if (Content->BufferOverridden) {
1846f4a2713aSLionel Sambuc           Record.clear();
1847f4a2713aSLionel Sambuc           Record.push_back(SM_SLOC_BUFFER_BLOB);
1848f4a2713aSLionel Sambuc           const llvm::MemoryBuffer *Buffer
1849f4a2713aSLionel Sambuc             = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1850f4a2713aSLionel Sambuc           Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1851f4a2713aSLionel Sambuc                                     StringRef(Buffer->getBufferStart(),
1852f4a2713aSLionel Sambuc                                               Buffer->getBufferSize() + 1));
1853f4a2713aSLionel Sambuc         }
1854f4a2713aSLionel Sambuc       } else {
1855f4a2713aSLionel Sambuc         // The source location entry is a buffer. The blob associated
1856f4a2713aSLionel Sambuc         // with this entry contains the contents of the buffer.
1857f4a2713aSLionel Sambuc 
1858f4a2713aSLionel Sambuc         // We add one to the size so that we capture the trailing NULL
1859f4a2713aSLionel Sambuc         // that is required by llvm::MemoryBuffer::getMemBuffer (on
1860f4a2713aSLionel Sambuc         // the reader side).
1861f4a2713aSLionel Sambuc         const llvm::MemoryBuffer *Buffer
1862f4a2713aSLionel Sambuc           = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1863f4a2713aSLionel Sambuc         const char *Name = Buffer->getBufferIdentifier();
1864f4a2713aSLionel Sambuc         Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1865f4a2713aSLionel Sambuc                                   StringRef(Name, strlen(Name) + 1));
1866f4a2713aSLionel Sambuc         Record.clear();
1867f4a2713aSLionel Sambuc         Record.push_back(SM_SLOC_BUFFER_BLOB);
1868f4a2713aSLionel Sambuc         Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1869f4a2713aSLionel Sambuc                                   StringRef(Buffer->getBufferStart(),
1870f4a2713aSLionel Sambuc                                                   Buffer->getBufferSize() + 1));
1871f4a2713aSLionel Sambuc 
1872f4a2713aSLionel Sambuc         if (strcmp(Name, "<built-in>") == 0) {
1873f4a2713aSLionel Sambuc           PreloadSLocs.push_back(SLocEntryOffsets.size());
1874f4a2713aSLionel Sambuc         }
1875f4a2713aSLionel Sambuc       }
1876f4a2713aSLionel Sambuc     } else {
1877f4a2713aSLionel Sambuc       // The source location entry is a macro expansion.
1878f4a2713aSLionel Sambuc       const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
1879f4a2713aSLionel Sambuc       Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1880f4a2713aSLionel Sambuc       Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
1881f4a2713aSLionel Sambuc       Record.push_back(Expansion.isMacroArgExpansion() ? 0
1882f4a2713aSLionel Sambuc                              : Expansion.getExpansionLocEnd().getRawEncoding());
1883f4a2713aSLionel Sambuc 
1884f4a2713aSLionel Sambuc       // Compute the token length for this macro expansion.
1885f4a2713aSLionel Sambuc       unsigned NextOffset = SourceMgr.getNextLocalOffset();
1886f4a2713aSLionel Sambuc       if (I + 1 != N)
1887f4a2713aSLionel Sambuc         NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
1888f4a2713aSLionel Sambuc       Record.push_back(NextOffset - SLoc->getOffset() - 1);
1889f4a2713aSLionel Sambuc       Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
1890f4a2713aSLionel Sambuc     }
1891f4a2713aSLionel Sambuc   }
1892f4a2713aSLionel Sambuc 
1893f4a2713aSLionel Sambuc   Stream.ExitBlock();
1894f4a2713aSLionel Sambuc 
1895f4a2713aSLionel Sambuc   if (SLocEntryOffsets.empty())
1896f4a2713aSLionel Sambuc     return;
1897f4a2713aSLionel Sambuc 
1898f4a2713aSLionel Sambuc   // Write the source-location offsets table into the AST block. This
1899f4a2713aSLionel Sambuc   // table is used for lazily loading source-location information.
1900f4a2713aSLionel Sambuc   using namespace llvm;
1901f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1902f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
1903f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1904f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
1905f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1906f4a2713aSLionel Sambuc   unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1907f4a2713aSLionel Sambuc 
1908f4a2713aSLionel Sambuc   Record.clear();
1909f4a2713aSLionel Sambuc   Record.push_back(SOURCE_LOCATION_OFFSETS);
1910f4a2713aSLionel Sambuc   Record.push_back(SLocEntryOffsets.size());
1911f4a2713aSLionel Sambuc   Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
1912f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
1913f4a2713aSLionel Sambuc 
1914f4a2713aSLionel Sambuc   // Write the source location entry preloads array, telling the AST
1915f4a2713aSLionel Sambuc   // reader which source locations entries it should load eagerly.
1916f4a2713aSLionel Sambuc   Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
1917f4a2713aSLionel Sambuc 
1918f4a2713aSLionel Sambuc   // Write the line table. It depends on remapping working, so it must come
1919f4a2713aSLionel Sambuc   // after the source location offsets.
1920f4a2713aSLionel Sambuc   if (SourceMgr.hasLineTable()) {
1921f4a2713aSLionel Sambuc     LineTableInfo &LineTable = SourceMgr.getLineTable();
1922f4a2713aSLionel Sambuc 
1923f4a2713aSLionel Sambuc     Record.clear();
1924*0a6a1f1dSLionel Sambuc     // Emit the file names.
1925f4a2713aSLionel Sambuc     Record.push_back(LineTable.getNumFilenames());
1926*0a6a1f1dSLionel Sambuc     for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1927*0a6a1f1dSLionel Sambuc       AddPath(LineTable.getFilename(I), Record);
1928f4a2713aSLionel Sambuc 
1929f4a2713aSLionel Sambuc     // Emit the line entries
1930f4a2713aSLionel Sambuc     for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1931f4a2713aSLionel Sambuc          L != LEnd; ++L) {
1932f4a2713aSLionel Sambuc       // Only emit entries for local files.
1933f4a2713aSLionel Sambuc       if (L->first.ID < 0)
1934f4a2713aSLionel Sambuc         continue;
1935f4a2713aSLionel Sambuc 
1936f4a2713aSLionel Sambuc       // Emit the file ID
1937f4a2713aSLionel Sambuc       Record.push_back(L->first.ID);
1938f4a2713aSLionel Sambuc 
1939f4a2713aSLionel Sambuc       // Emit the line entries
1940f4a2713aSLionel Sambuc       Record.push_back(L->second.size());
1941f4a2713aSLionel Sambuc       for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1942f4a2713aSLionel Sambuc                                          LEEnd = L->second.end();
1943f4a2713aSLionel Sambuc            LE != LEEnd; ++LE) {
1944f4a2713aSLionel Sambuc         Record.push_back(LE->FileOffset);
1945f4a2713aSLionel Sambuc         Record.push_back(LE->LineNo);
1946f4a2713aSLionel Sambuc         Record.push_back(LE->FilenameID);
1947f4a2713aSLionel Sambuc         Record.push_back((unsigned)LE->FileKind);
1948f4a2713aSLionel Sambuc         Record.push_back(LE->IncludeOffset);
1949f4a2713aSLionel Sambuc       }
1950f4a2713aSLionel Sambuc     }
1951f4a2713aSLionel Sambuc     Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1952f4a2713aSLionel Sambuc   }
1953f4a2713aSLionel Sambuc }
1954f4a2713aSLionel Sambuc 
1955f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1956f4a2713aSLionel Sambuc // Preprocessor Serialization
1957f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1958f4a2713aSLionel Sambuc 
1959f4a2713aSLionel Sambuc namespace {
1960f4a2713aSLionel Sambuc class ASTMacroTableTrait {
1961f4a2713aSLionel Sambuc public:
1962f4a2713aSLionel Sambuc   typedef IdentID key_type;
1963f4a2713aSLionel Sambuc   typedef key_type key_type_ref;
1964f4a2713aSLionel Sambuc 
1965f4a2713aSLionel Sambuc   struct Data {
1966f4a2713aSLionel Sambuc     uint32_t MacroDirectivesOffset;
1967f4a2713aSLionel Sambuc   };
1968f4a2713aSLionel Sambuc 
1969f4a2713aSLionel Sambuc   typedef Data data_type;
1970f4a2713aSLionel Sambuc   typedef const data_type &data_type_ref;
1971*0a6a1f1dSLionel Sambuc   typedef unsigned hash_value_type;
1972*0a6a1f1dSLionel Sambuc   typedef unsigned offset_type;
1973f4a2713aSLionel Sambuc 
ComputeHash(IdentID IdID)1974*0a6a1f1dSLionel Sambuc   static hash_value_type ComputeHash(IdentID IdID) {
1975f4a2713aSLionel Sambuc     return llvm::hash_value(IdID);
1976f4a2713aSLionel Sambuc   }
1977f4a2713aSLionel Sambuc 
1978f4a2713aSLionel Sambuc   std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,key_type_ref Key,data_type_ref Data)1979f4a2713aSLionel Sambuc   static EmitKeyDataLength(raw_ostream& Out,
1980f4a2713aSLionel Sambuc                            key_type_ref Key, data_type_ref Data) {
1981f4a2713aSLionel Sambuc     unsigned KeyLen = 4; // IdentID.
1982f4a2713aSLionel Sambuc     unsigned DataLen = 4; // MacroDirectivesOffset.
1983f4a2713aSLionel Sambuc     return std::make_pair(KeyLen, DataLen);
1984f4a2713aSLionel Sambuc   }
1985f4a2713aSLionel Sambuc 
EmitKey(raw_ostream & Out,key_type_ref Key,unsigned KeyLen)1986f4a2713aSLionel Sambuc   static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1987*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
1988*0a6a1f1dSLionel Sambuc     endian::Writer<little>(Out).write<uint32_t>(Key);
1989f4a2713aSLionel Sambuc   }
1990f4a2713aSLionel Sambuc 
EmitData(raw_ostream & Out,key_type_ref Key,data_type_ref Data,unsigned)1991f4a2713aSLionel Sambuc   static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1992f4a2713aSLionel Sambuc                        unsigned) {
1993*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
1994*0a6a1f1dSLionel Sambuc     endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
1995f4a2713aSLionel Sambuc   }
1996f4a2713aSLionel Sambuc };
1997f4a2713aSLionel Sambuc } // end anonymous namespace
1998f4a2713aSLionel Sambuc 
compareMacroDirectives(const std::pair<const IdentifierInfo *,MacroDirective * > * X,const std::pair<const IdentifierInfo *,MacroDirective * > * Y)1999f4a2713aSLionel Sambuc static int compareMacroDirectives(
2000f4a2713aSLionel Sambuc     const std::pair<const IdentifierInfo *, MacroDirective *> *X,
2001f4a2713aSLionel Sambuc     const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
2002f4a2713aSLionel Sambuc   return X->first->getName().compare(Y->first->getName());
2003f4a2713aSLionel Sambuc }
2004f4a2713aSLionel Sambuc 
shouldIgnoreMacro(MacroDirective * MD,bool IsModule,const Preprocessor & PP)2005f4a2713aSLionel Sambuc static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2006f4a2713aSLionel Sambuc                               const Preprocessor &PP) {
2007f4a2713aSLionel Sambuc   if (MacroInfo *MI = MD->getMacroInfo())
2008f4a2713aSLionel Sambuc     if (MI->isBuiltinMacro())
2009f4a2713aSLionel Sambuc       return true;
2010f4a2713aSLionel Sambuc 
2011f4a2713aSLionel Sambuc   if (IsModule) {
2012*0a6a1f1dSLionel Sambuc     // Re-export any imported directives.
2013*0a6a1f1dSLionel Sambuc     if (MD->isImported())
2014*0a6a1f1dSLionel Sambuc       return false;
2015*0a6a1f1dSLionel Sambuc 
2016f4a2713aSLionel Sambuc     SourceLocation Loc = MD->getLocation();
2017f4a2713aSLionel Sambuc     if (Loc.isInvalid())
2018f4a2713aSLionel Sambuc       return true;
2019f4a2713aSLionel Sambuc     if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2020f4a2713aSLionel Sambuc       return true;
2021f4a2713aSLionel Sambuc   }
2022f4a2713aSLionel Sambuc 
2023f4a2713aSLionel Sambuc   return false;
2024f4a2713aSLionel Sambuc }
2025f4a2713aSLionel Sambuc 
2026f4a2713aSLionel Sambuc /// \brief Writes the block containing the serialized form of the
2027f4a2713aSLionel Sambuc /// preprocessor.
2028f4a2713aSLionel Sambuc ///
WritePreprocessor(const Preprocessor & PP,bool IsModule)2029f4a2713aSLionel Sambuc void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2030f4a2713aSLionel Sambuc   PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2031f4a2713aSLionel Sambuc   if (PPRec)
2032f4a2713aSLionel Sambuc     WritePreprocessorDetail(*PPRec);
2033f4a2713aSLionel Sambuc 
2034f4a2713aSLionel Sambuc   RecordData Record;
2035f4a2713aSLionel Sambuc 
2036f4a2713aSLionel Sambuc   // If the preprocessor __COUNTER__ value has been bumped, remember it.
2037f4a2713aSLionel Sambuc   if (PP.getCounterValue() != 0) {
2038f4a2713aSLionel Sambuc     Record.push_back(PP.getCounterValue());
2039f4a2713aSLionel Sambuc     Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2040f4a2713aSLionel Sambuc     Record.clear();
2041f4a2713aSLionel Sambuc   }
2042f4a2713aSLionel Sambuc 
2043f4a2713aSLionel Sambuc   // Enter the preprocessor block.
2044f4a2713aSLionel Sambuc   Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2045f4a2713aSLionel Sambuc 
2046f4a2713aSLionel Sambuc   // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2047f4a2713aSLionel Sambuc   // FIXME: use diagnostics subsystem for localization etc.
2048f4a2713aSLionel Sambuc   if (PP.SawDateOrTime())
2049f4a2713aSLionel Sambuc     fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
2050f4a2713aSLionel Sambuc 
2051f4a2713aSLionel Sambuc 
2052f4a2713aSLionel Sambuc   // Loop over all the macro directives that are live at the end of the file,
2053f4a2713aSLionel Sambuc   // emitting each to the PP section.
2054f4a2713aSLionel Sambuc 
2055f4a2713aSLionel Sambuc   // Construct the list of macro directives that need to be serialized.
2056f4a2713aSLionel Sambuc   SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
2057f4a2713aSLionel Sambuc     MacroDirectives;
2058f4a2713aSLionel Sambuc   for (Preprocessor::macro_iterator
2059f4a2713aSLionel Sambuc          I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2060f4a2713aSLionel Sambuc          E = PP.macro_end(/*IncludeExternalMacros=*/false);
2061f4a2713aSLionel Sambuc        I != E; ++I) {
2062f4a2713aSLionel Sambuc     MacroDirectives.push_back(std::make_pair(I->first, I->second));
2063f4a2713aSLionel Sambuc   }
2064f4a2713aSLionel Sambuc 
2065f4a2713aSLionel Sambuc   // Sort the set of macro definitions that need to be serialized by the
2066f4a2713aSLionel Sambuc   // name of the macro, to provide a stable ordering.
2067f4a2713aSLionel Sambuc   llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2068f4a2713aSLionel Sambuc                        &compareMacroDirectives);
2069f4a2713aSLionel Sambuc 
2070*0a6a1f1dSLionel Sambuc   llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
2071f4a2713aSLionel Sambuc 
2072f4a2713aSLionel Sambuc   // Emit the macro directives as a list and associate the offset with the
2073f4a2713aSLionel Sambuc   // identifier they belong to.
2074f4a2713aSLionel Sambuc   for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2075f4a2713aSLionel Sambuc     const IdentifierInfo *Name = MacroDirectives[I].first;
2076f4a2713aSLionel Sambuc     uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2077f4a2713aSLionel Sambuc     MacroDirective *MD = MacroDirectives[I].second;
2078f4a2713aSLionel Sambuc 
2079f4a2713aSLionel Sambuc     // If the macro or identifier need no updates, don't write the macro history
2080f4a2713aSLionel Sambuc     // for this one.
2081f4a2713aSLionel Sambuc     // FIXME: Chain the macro history instead of re-writing it.
2082f4a2713aSLionel Sambuc     if (MD->isFromPCH() &&
2083f4a2713aSLionel Sambuc         Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2084f4a2713aSLionel Sambuc       continue;
2085f4a2713aSLionel Sambuc 
2086f4a2713aSLionel Sambuc     // Emit the macro directives in reverse source order.
2087f4a2713aSLionel Sambuc     for (; MD; MD = MD->getPrevious()) {
2088f4a2713aSLionel Sambuc       if (shouldIgnoreMacro(MD, IsModule, PP))
2089f4a2713aSLionel Sambuc         continue;
2090f4a2713aSLionel Sambuc 
2091f4a2713aSLionel Sambuc       AddSourceLocation(MD->getLocation(), Record);
2092f4a2713aSLionel Sambuc       Record.push_back(MD->getKind());
2093*0a6a1f1dSLionel Sambuc       if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2094f4a2713aSLionel Sambuc         MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2095f4a2713aSLionel Sambuc         Record.push_back(InfoID);
2096*0a6a1f1dSLionel Sambuc         Record.push_back(DefMD->getOwningModuleID());
2097f4a2713aSLionel Sambuc         Record.push_back(DefMD->isAmbiguous());
2098*0a6a1f1dSLionel Sambuc       } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2099*0a6a1f1dSLionel Sambuc         Record.push_back(UndefMD->getOwningModuleID());
2100*0a6a1f1dSLionel Sambuc       } else {
2101*0a6a1f1dSLionel Sambuc         auto *VisMD = cast<VisibilityMacroDirective>(MD);
2102f4a2713aSLionel Sambuc         Record.push_back(VisMD->isPublic());
2103f4a2713aSLionel Sambuc       }
2104*0a6a1f1dSLionel Sambuc 
2105*0a6a1f1dSLionel Sambuc       if (MD->isImported()) {
2106*0a6a1f1dSLionel Sambuc         auto Overrides = MD->getOverriddenModules();
2107*0a6a1f1dSLionel Sambuc         Record.push_back(Overrides.size());
2108*0a6a1f1dSLionel Sambuc         for (auto Override : Overrides)
2109*0a6a1f1dSLionel Sambuc           Record.push_back(Override);
2110*0a6a1f1dSLionel Sambuc       }
2111f4a2713aSLionel Sambuc     }
2112f4a2713aSLionel Sambuc     if (Record.empty())
2113f4a2713aSLionel Sambuc       continue;
2114f4a2713aSLionel Sambuc 
2115f4a2713aSLionel Sambuc     Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2116f4a2713aSLionel Sambuc     Record.clear();
2117f4a2713aSLionel Sambuc 
2118f4a2713aSLionel Sambuc     IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2119f4a2713aSLionel Sambuc 
2120f4a2713aSLionel Sambuc     IdentID NameID = getIdentifierRef(Name);
2121f4a2713aSLionel Sambuc     ASTMacroTableTrait::Data data;
2122f4a2713aSLionel Sambuc     data.MacroDirectivesOffset = MacroDirectiveOffset;
2123f4a2713aSLionel Sambuc     Generator.insert(NameID, data);
2124f4a2713aSLionel Sambuc   }
2125f4a2713aSLionel Sambuc 
2126f4a2713aSLionel Sambuc   /// \brief Offsets of each of the macros into the bitstream, indexed by
2127f4a2713aSLionel Sambuc   /// the local macro ID
2128f4a2713aSLionel Sambuc   ///
2129f4a2713aSLionel Sambuc   /// For each identifier that is associated with a macro, this map
2130f4a2713aSLionel Sambuc   /// provides the offset into the bitstream where that macro is
2131f4a2713aSLionel Sambuc   /// defined.
2132f4a2713aSLionel Sambuc   std::vector<uint32_t> MacroOffsets;
2133f4a2713aSLionel Sambuc 
2134f4a2713aSLionel Sambuc   for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2135f4a2713aSLionel Sambuc     const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2136f4a2713aSLionel Sambuc     MacroInfo *MI = MacroInfosToEmit[I].MI;
2137f4a2713aSLionel Sambuc     MacroID ID = MacroInfosToEmit[I].ID;
2138f4a2713aSLionel Sambuc 
2139f4a2713aSLionel Sambuc     if (ID < FirstMacroID) {
2140f4a2713aSLionel Sambuc       assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2141f4a2713aSLionel Sambuc       continue;
2142f4a2713aSLionel Sambuc     }
2143f4a2713aSLionel Sambuc 
2144f4a2713aSLionel Sambuc     // Record the local offset of this macro.
2145f4a2713aSLionel Sambuc     unsigned Index = ID - FirstMacroID;
2146f4a2713aSLionel Sambuc     if (Index == MacroOffsets.size())
2147f4a2713aSLionel Sambuc       MacroOffsets.push_back(Stream.GetCurrentBitNo());
2148f4a2713aSLionel Sambuc     else {
2149f4a2713aSLionel Sambuc       if (Index > MacroOffsets.size())
2150f4a2713aSLionel Sambuc         MacroOffsets.resize(Index + 1);
2151f4a2713aSLionel Sambuc 
2152f4a2713aSLionel Sambuc       MacroOffsets[Index] = Stream.GetCurrentBitNo();
2153f4a2713aSLionel Sambuc     }
2154f4a2713aSLionel Sambuc 
2155f4a2713aSLionel Sambuc     AddIdentifierRef(Name, Record);
2156f4a2713aSLionel Sambuc     Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2157f4a2713aSLionel Sambuc     AddSourceLocation(MI->getDefinitionLoc(), Record);
2158f4a2713aSLionel Sambuc     AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2159f4a2713aSLionel Sambuc     Record.push_back(MI->isUsed());
2160*0a6a1f1dSLionel Sambuc     Record.push_back(MI->isUsedForHeaderGuard());
2161f4a2713aSLionel Sambuc     unsigned Code;
2162f4a2713aSLionel Sambuc     if (MI->isObjectLike()) {
2163f4a2713aSLionel Sambuc       Code = PP_MACRO_OBJECT_LIKE;
2164f4a2713aSLionel Sambuc     } else {
2165f4a2713aSLionel Sambuc       Code = PP_MACRO_FUNCTION_LIKE;
2166f4a2713aSLionel Sambuc 
2167f4a2713aSLionel Sambuc       Record.push_back(MI->isC99Varargs());
2168f4a2713aSLionel Sambuc       Record.push_back(MI->isGNUVarargs());
2169f4a2713aSLionel Sambuc       Record.push_back(MI->hasCommaPasting());
2170f4a2713aSLionel Sambuc       Record.push_back(MI->getNumArgs());
2171f4a2713aSLionel Sambuc       for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2172f4a2713aSLionel Sambuc            I != E; ++I)
2173f4a2713aSLionel Sambuc         AddIdentifierRef(*I, Record);
2174f4a2713aSLionel Sambuc     }
2175f4a2713aSLionel Sambuc 
2176f4a2713aSLionel Sambuc     // If we have a detailed preprocessing record, record the macro definition
2177f4a2713aSLionel Sambuc     // ID that corresponds to this macro.
2178f4a2713aSLionel Sambuc     if (PPRec)
2179f4a2713aSLionel Sambuc       Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2180f4a2713aSLionel Sambuc 
2181f4a2713aSLionel Sambuc     Stream.EmitRecord(Code, Record);
2182f4a2713aSLionel Sambuc     Record.clear();
2183f4a2713aSLionel Sambuc 
2184f4a2713aSLionel Sambuc     // Emit the tokens array.
2185f4a2713aSLionel Sambuc     for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2186f4a2713aSLionel Sambuc       // Note that we know that the preprocessor does not have any annotation
2187f4a2713aSLionel Sambuc       // tokens in it because they are created by the parser, and thus can't
2188f4a2713aSLionel Sambuc       // be in a macro definition.
2189f4a2713aSLionel Sambuc       const Token &Tok = MI->getReplacementToken(TokNo);
2190f4a2713aSLionel Sambuc       AddToken(Tok, Record);
2191f4a2713aSLionel Sambuc       Stream.EmitRecord(PP_TOKEN, Record);
2192f4a2713aSLionel Sambuc       Record.clear();
2193f4a2713aSLionel Sambuc     }
2194f4a2713aSLionel Sambuc     ++NumMacros;
2195f4a2713aSLionel Sambuc   }
2196f4a2713aSLionel Sambuc 
2197f4a2713aSLionel Sambuc   Stream.ExitBlock();
2198f4a2713aSLionel Sambuc 
2199f4a2713aSLionel Sambuc   // Create the on-disk hash table in a buffer.
2200f4a2713aSLionel Sambuc   SmallString<4096> MacroTable;
2201f4a2713aSLionel Sambuc   uint32_t BucketOffset;
2202f4a2713aSLionel Sambuc   {
2203*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
2204f4a2713aSLionel Sambuc     llvm::raw_svector_ostream Out(MacroTable);
2205f4a2713aSLionel Sambuc     // Make sure that no bucket is at offset 0
2206*0a6a1f1dSLionel Sambuc     endian::Writer<little>(Out).write<uint32_t>(0);
2207f4a2713aSLionel Sambuc     BucketOffset = Generator.Emit(Out);
2208f4a2713aSLionel Sambuc   }
2209f4a2713aSLionel Sambuc 
2210f4a2713aSLionel Sambuc   // Write the macro table
2211f4a2713aSLionel Sambuc   using namespace llvm;
2212f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2213f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2214f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2215f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2216f4a2713aSLionel Sambuc   unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2217f4a2713aSLionel Sambuc 
2218f4a2713aSLionel Sambuc   Record.push_back(MACRO_TABLE);
2219f4a2713aSLionel Sambuc   Record.push_back(BucketOffset);
2220f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2221f4a2713aSLionel Sambuc   Record.clear();
2222f4a2713aSLionel Sambuc 
2223f4a2713aSLionel Sambuc   // Write the offsets table for macro IDs.
2224f4a2713aSLionel Sambuc   using namespace llvm;
2225f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2226f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2227f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2228f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2229f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2230f4a2713aSLionel Sambuc 
2231f4a2713aSLionel Sambuc   unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2232f4a2713aSLionel Sambuc   Record.clear();
2233f4a2713aSLionel Sambuc   Record.push_back(MACRO_OFFSET);
2234f4a2713aSLionel Sambuc   Record.push_back(MacroOffsets.size());
2235f4a2713aSLionel Sambuc   Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2236f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2237f4a2713aSLionel Sambuc                             data(MacroOffsets));
2238f4a2713aSLionel Sambuc }
2239f4a2713aSLionel Sambuc 
WritePreprocessorDetail(PreprocessingRecord & PPRec)2240f4a2713aSLionel Sambuc void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
2241f4a2713aSLionel Sambuc   if (PPRec.local_begin() == PPRec.local_end())
2242f4a2713aSLionel Sambuc     return;
2243f4a2713aSLionel Sambuc 
2244f4a2713aSLionel Sambuc   SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2245f4a2713aSLionel Sambuc 
2246f4a2713aSLionel Sambuc   // Enter the preprocessor block.
2247f4a2713aSLionel Sambuc   Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2248f4a2713aSLionel Sambuc 
2249f4a2713aSLionel Sambuc   // If the preprocessor has a preprocessing record, emit it.
2250f4a2713aSLionel Sambuc   unsigned NumPreprocessingRecords = 0;
2251f4a2713aSLionel Sambuc   using namespace llvm;
2252f4a2713aSLionel Sambuc 
2253f4a2713aSLionel Sambuc   // Set up the abbreviation for
2254f4a2713aSLionel Sambuc   unsigned InclusionAbbrev = 0;
2255f4a2713aSLionel Sambuc   {
2256f4a2713aSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2257f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2258f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2259f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2260f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2261f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2262f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2263f4a2713aSLionel Sambuc     InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2264f4a2713aSLionel Sambuc   }
2265f4a2713aSLionel Sambuc 
2266f4a2713aSLionel Sambuc   unsigned FirstPreprocessorEntityID
2267f4a2713aSLionel Sambuc     = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2268f4a2713aSLionel Sambuc     + NUM_PREDEF_PP_ENTITY_IDS;
2269f4a2713aSLionel Sambuc   unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2270f4a2713aSLionel Sambuc   RecordData Record;
2271f4a2713aSLionel Sambuc   for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2272f4a2713aSLionel Sambuc                                   EEnd = PPRec.local_end();
2273f4a2713aSLionel Sambuc        E != EEnd;
2274f4a2713aSLionel Sambuc        (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2275f4a2713aSLionel Sambuc     Record.clear();
2276f4a2713aSLionel Sambuc 
2277f4a2713aSLionel Sambuc     PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2278f4a2713aSLionel Sambuc                                                      Stream.GetCurrentBitNo()));
2279f4a2713aSLionel Sambuc 
2280f4a2713aSLionel Sambuc     if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
2281f4a2713aSLionel Sambuc       // Record this macro definition's ID.
2282f4a2713aSLionel Sambuc       MacroDefinitions[MD] = NextPreprocessorEntityID;
2283f4a2713aSLionel Sambuc 
2284f4a2713aSLionel Sambuc       AddIdentifierRef(MD->getName(), Record);
2285f4a2713aSLionel Sambuc       Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2286f4a2713aSLionel Sambuc       continue;
2287f4a2713aSLionel Sambuc     }
2288f4a2713aSLionel Sambuc 
2289f4a2713aSLionel Sambuc     if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
2290f4a2713aSLionel Sambuc       Record.push_back(ME->isBuiltinMacro());
2291f4a2713aSLionel Sambuc       if (ME->isBuiltinMacro())
2292f4a2713aSLionel Sambuc         AddIdentifierRef(ME->getName(), Record);
2293f4a2713aSLionel Sambuc       else
2294f4a2713aSLionel Sambuc         Record.push_back(MacroDefinitions[ME->getDefinition()]);
2295f4a2713aSLionel Sambuc       Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2296f4a2713aSLionel Sambuc       continue;
2297f4a2713aSLionel Sambuc     }
2298f4a2713aSLionel Sambuc 
2299f4a2713aSLionel Sambuc     if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2300f4a2713aSLionel Sambuc       Record.push_back(PPD_INCLUSION_DIRECTIVE);
2301f4a2713aSLionel Sambuc       Record.push_back(ID->getFileName().size());
2302f4a2713aSLionel Sambuc       Record.push_back(ID->wasInQuotes());
2303f4a2713aSLionel Sambuc       Record.push_back(static_cast<unsigned>(ID->getKind()));
2304f4a2713aSLionel Sambuc       Record.push_back(ID->importedModule());
2305f4a2713aSLionel Sambuc       SmallString<64> Buffer;
2306f4a2713aSLionel Sambuc       Buffer += ID->getFileName();
2307f4a2713aSLionel Sambuc       // Check that the FileEntry is not null because it was not resolved and
2308f4a2713aSLionel Sambuc       // we create a PCH even with compiler errors.
2309f4a2713aSLionel Sambuc       if (ID->getFile())
2310f4a2713aSLionel Sambuc         Buffer += ID->getFile()->getName();
2311f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2312f4a2713aSLionel Sambuc       continue;
2313f4a2713aSLionel Sambuc     }
2314f4a2713aSLionel Sambuc 
2315f4a2713aSLionel Sambuc     llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2316f4a2713aSLionel Sambuc   }
2317f4a2713aSLionel Sambuc   Stream.ExitBlock();
2318f4a2713aSLionel Sambuc 
2319f4a2713aSLionel Sambuc   // Write the offsets table for the preprocessing record.
2320f4a2713aSLionel Sambuc   if (NumPreprocessingRecords > 0) {
2321f4a2713aSLionel Sambuc     assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2322f4a2713aSLionel Sambuc 
2323f4a2713aSLionel Sambuc     // Write the offsets table for identifier IDs.
2324f4a2713aSLionel Sambuc     using namespace llvm;
2325f4a2713aSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2326f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2327f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2328f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2329f4a2713aSLionel Sambuc     unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2330f4a2713aSLionel Sambuc 
2331f4a2713aSLionel Sambuc     Record.clear();
2332f4a2713aSLionel Sambuc     Record.push_back(PPD_ENTITIES_OFFSETS);
2333f4a2713aSLionel Sambuc     Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
2334f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2335f4a2713aSLionel Sambuc                               data(PreprocessedEntityOffsets));
2336f4a2713aSLionel Sambuc   }
2337f4a2713aSLionel Sambuc }
2338f4a2713aSLionel Sambuc 
getSubmoduleID(Module * Mod)2339f4a2713aSLionel Sambuc unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2340f4a2713aSLionel Sambuc   llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2341f4a2713aSLionel Sambuc   if (Known != SubmoduleIDs.end())
2342f4a2713aSLionel Sambuc     return Known->second;
2343f4a2713aSLionel Sambuc 
2344f4a2713aSLionel Sambuc   return SubmoduleIDs[Mod] = NextSubmoduleID++;
2345f4a2713aSLionel Sambuc }
2346f4a2713aSLionel Sambuc 
getExistingSubmoduleID(Module * Mod) const2347f4a2713aSLionel Sambuc unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2348f4a2713aSLionel Sambuc   if (!Mod)
2349f4a2713aSLionel Sambuc     return 0;
2350f4a2713aSLionel Sambuc 
2351f4a2713aSLionel Sambuc   llvm::DenseMap<Module *, unsigned>::const_iterator
2352f4a2713aSLionel Sambuc     Known = SubmoduleIDs.find(Mod);
2353f4a2713aSLionel Sambuc   if (Known != SubmoduleIDs.end())
2354f4a2713aSLionel Sambuc     return Known->second;
2355f4a2713aSLionel Sambuc 
2356f4a2713aSLionel Sambuc   return 0;
2357f4a2713aSLionel Sambuc }
2358f4a2713aSLionel Sambuc 
2359f4a2713aSLionel Sambuc /// \brief Compute the number of modules within the given tree (including the
2360f4a2713aSLionel Sambuc /// given module).
getNumberOfModules(Module * Mod)2361f4a2713aSLionel Sambuc static unsigned getNumberOfModules(Module *Mod) {
2362f4a2713aSLionel Sambuc   unsigned ChildModules = 0;
2363f4a2713aSLionel Sambuc   for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2364f4a2713aSLionel Sambuc                                SubEnd = Mod->submodule_end();
2365f4a2713aSLionel Sambuc        Sub != SubEnd; ++Sub)
2366f4a2713aSLionel Sambuc     ChildModules += getNumberOfModules(*Sub);
2367f4a2713aSLionel Sambuc 
2368f4a2713aSLionel Sambuc   return ChildModules + 1;
2369f4a2713aSLionel Sambuc }
2370f4a2713aSLionel Sambuc 
WriteSubmodules(Module * WritingModule)2371f4a2713aSLionel Sambuc void ASTWriter::WriteSubmodules(Module *WritingModule) {
2372f4a2713aSLionel Sambuc   // Determine the dependencies of our module and each of it's submodules.
2373f4a2713aSLionel Sambuc   // FIXME: This feels like it belongs somewhere else, but there are no
2374f4a2713aSLionel Sambuc   // other consumers of this information.
2375f4a2713aSLionel Sambuc   SourceManager &SrcMgr = PP->getSourceManager();
2376f4a2713aSLionel Sambuc   ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2377*0a6a1f1dSLionel Sambuc   for (const auto *I : Context->local_imports()) {
2378f4a2713aSLionel Sambuc     if (Module *ImportedFrom
2379f4a2713aSLionel Sambuc           = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2380f4a2713aSLionel Sambuc                                                          SrcMgr))) {
2381f4a2713aSLionel Sambuc       ImportedFrom->Imports.push_back(I->getImportedModule());
2382f4a2713aSLionel Sambuc     }
2383f4a2713aSLionel Sambuc   }
2384f4a2713aSLionel Sambuc 
2385f4a2713aSLionel Sambuc   // Enter the submodule description block.
2386*0a6a1f1dSLionel Sambuc   Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2387f4a2713aSLionel Sambuc 
2388f4a2713aSLionel Sambuc   // Write the abbreviations needed for the submodules block.
2389f4a2713aSLionel Sambuc   using namespace llvm;
2390f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2391f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2392f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2393f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2394f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2395f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2396f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2397*0a6a1f1dSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2398f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2399f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2400f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2401f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2402f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2403f4a2713aSLionel Sambuc   unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2404f4a2713aSLionel Sambuc 
2405f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2406f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2407f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2408f4a2713aSLionel Sambuc   unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2409f4a2713aSLionel Sambuc 
2410f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2411f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2412f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2413f4a2713aSLionel Sambuc   unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2414f4a2713aSLionel Sambuc 
2415f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2416f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2417f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2418f4a2713aSLionel Sambuc   unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2419f4a2713aSLionel Sambuc 
2420f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2421f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2422f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2423f4a2713aSLionel Sambuc   unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2424f4a2713aSLionel Sambuc 
2425f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2426f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2427f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2428f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Feature
2429f4a2713aSLionel Sambuc   unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2430f4a2713aSLionel Sambuc 
2431f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2432f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2433f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2434f4a2713aSLionel Sambuc   unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2435f4a2713aSLionel Sambuc 
2436f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2437*0a6a1f1dSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2438*0a6a1f1dSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2439*0a6a1f1dSLionel Sambuc   unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2440*0a6a1f1dSLionel Sambuc 
2441*0a6a1f1dSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2442f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2443f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2444f4a2713aSLionel Sambuc   unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2445f4a2713aSLionel Sambuc 
2446f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2447*0a6a1f1dSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2448*0a6a1f1dSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2449*0a6a1f1dSLionel Sambuc   unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2450*0a6a1f1dSLionel Sambuc 
2451*0a6a1f1dSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2452f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2453f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2454f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Name
2455f4a2713aSLionel Sambuc   unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2456f4a2713aSLionel Sambuc 
2457f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2458f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2459f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2460f4a2713aSLionel Sambuc   unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2461f4a2713aSLionel Sambuc 
2462f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2463f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2464f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));  // Other module
2465f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Message
2466f4a2713aSLionel Sambuc   unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2467f4a2713aSLionel Sambuc 
2468f4a2713aSLionel Sambuc   // Write the submodule metadata block.
2469f4a2713aSLionel Sambuc   RecordData Record;
2470f4a2713aSLionel Sambuc   Record.push_back(getNumberOfModules(WritingModule));
2471f4a2713aSLionel Sambuc   Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2472f4a2713aSLionel Sambuc   Stream.EmitRecord(SUBMODULE_METADATA, Record);
2473f4a2713aSLionel Sambuc 
2474f4a2713aSLionel Sambuc   // Write all of the submodules.
2475f4a2713aSLionel Sambuc   std::queue<Module *> Q;
2476f4a2713aSLionel Sambuc   Q.push(WritingModule);
2477f4a2713aSLionel Sambuc   while (!Q.empty()) {
2478f4a2713aSLionel Sambuc     Module *Mod = Q.front();
2479f4a2713aSLionel Sambuc     Q.pop();
2480f4a2713aSLionel Sambuc     unsigned ID = getSubmoduleID(Mod);
2481f4a2713aSLionel Sambuc 
2482f4a2713aSLionel Sambuc     // Emit the definition of the block.
2483f4a2713aSLionel Sambuc     Record.clear();
2484f4a2713aSLionel Sambuc     Record.push_back(SUBMODULE_DEFINITION);
2485f4a2713aSLionel Sambuc     Record.push_back(ID);
2486f4a2713aSLionel Sambuc     if (Mod->Parent) {
2487f4a2713aSLionel Sambuc       assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2488f4a2713aSLionel Sambuc       Record.push_back(SubmoduleIDs[Mod->Parent]);
2489f4a2713aSLionel Sambuc     } else {
2490f4a2713aSLionel Sambuc       Record.push_back(0);
2491f4a2713aSLionel Sambuc     }
2492f4a2713aSLionel Sambuc     Record.push_back(Mod->IsFramework);
2493f4a2713aSLionel Sambuc     Record.push_back(Mod->IsExplicit);
2494f4a2713aSLionel Sambuc     Record.push_back(Mod->IsSystem);
2495*0a6a1f1dSLionel Sambuc     Record.push_back(Mod->IsExternC);
2496f4a2713aSLionel Sambuc     Record.push_back(Mod->InferSubmodules);
2497f4a2713aSLionel Sambuc     Record.push_back(Mod->InferExplicitSubmodules);
2498f4a2713aSLionel Sambuc     Record.push_back(Mod->InferExportWildcard);
2499f4a2713aSLionel Sambuc     Record.push_back(Mod->ConfigMacrosExhaustive);
2500f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2501f4a2713aSLionel Sambuc 
2502f4a2713aSLionel Sambuc     // Emit the requirements.
2503f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
2504f4a2713aSLionel Sambuc       Record.clear();
2505f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_REQUIRES);
2506f4a2713aSLionel Sambuc       Record.push_back(Mod->Requirements[I].second);
2507f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2508f4a2713aSLionel Sambuc                                 Mod->Requirements[I].first);
2509f4a2713aSLionel Sambuc     }
2510f4a2713aSLionel Sambuc 
2511f4a2713aSLionel Sambuc     // Emit the umbrella header, if there is one.
2512f4a2713aSLionel Sambuc     if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
2513f4a2713aSLionel Sambuc       Record.clear();
2514f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_UMBRELLA_HEADER);
2515f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2516f4a2713aSLionel Sambuc                                 UmbrellaHeader->getName());
2517f4a2713aSLionel Sambuc     } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2518f4a2713aSLionel Sambuc       Record.clear();
2519f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_UMBRELLA_DIR);
2520f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2521f4a2713aSLionel Sambuc                                 UmbrellaDir->getName());
2522f4a2713aSLionel Sambuc     }
2523f4a2713aSLionel Sambuc 
2524f4a2713aSLionel Sambuc     // Emit the headers.
2525*0a6a1f1dSLionel Sambuc     struct {
2526*0a6a1f1dSLionel Sambuc       unsigned RecordKind;
2527*0a6a1f1dSLionel Sambuc       unsigned Abbrev;
2528*0a6a1f1dSLionel Sambuc       Module::HeaderKind HeaderKind;
2529*0a6a1f1dSLionel Sambuc     } HeaderLists[] = {
2530*0a6a1f1dSLionel Sambuc       {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2531*0a6a1f1dSLionel Sambuc       {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2532*0a6a1f1dSLionel Sambuc       {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2533*0a6a1f1dSLionel Sambuc       {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2534*0a6a1f1dSLionel Sambuc         Module::HK_PrivateTextual},
2535*0a6a1f1dSLionel Sambuc       {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2536*0a6a1f1dSLionel Sambuc     };
2537*0a6a1f1dSLionel Sambuc     for (auto &HL : HeaderLists) {
2538f4a2713aSLionel Sambuc       Record.clear();
2539*0a6a1f1dSLionel Sambuc       Record.push_back(HL.RecordKind);
2540*0a6a1f1dSLionel Sambuc       for (auto &H : Mod->Headers[HL.HeaderKind])
2541*0a6a1f1dSLionel Sambuc         Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2542f4a2713aSLionel Sambuc     }
2543*0a6a1f1dSLionel Sambuc 
2544*0a6a1f1dSLionel Sambuc     // Emit the top headers.
2545*0a6a1f1dSLionel Sambuc     {
2546*0a6a1f1dSLionel Sambuc       auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2547f4a2713aSLionel Sambuc       Record.clear();
2548f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_TOPHEADER);
2549*0a6a1f1dSLionel Sambuc       for (auto *H : TopHeaders)
2550*0a6a1f1dSLionel Sambuc         Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
2551f4a2713aSLionel Sambuc     }
2552f4a2713aSLionel Sambuc 
2553f4a2713aSLionel Sambuc     // Emit the imports.
2554f4a2713aSLionel Sambuc     if (!Mod->Imports.empty()) {
2555f4a2713aSLionel Sambuc       Record.clear();
2556f4a2713aSLionel Sambuc       for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2557f4a2713aSLionel Sambuc         unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
2558f4a2713aSLionel Sambuc         assert(ImportedID && "Unknown submodule!");
2559f4a2713aSLionel Sambuc         Record.push_back(ImportedID);
2560f4a2713aSLionel Sambuc       }
2561f4a2713aSLionel Sambuc       Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2562f4a2713aSLionel Sambuc     }
2563f4a2713aSLionel Sambuc 
2564f4a2713aSLionel Sambuc     // Emit the exports.
2565f4a2713aSLionel Sambuc     if (!Mod->Exports.empty()) {
2566f4a2713aSLionel Sambuc       Record.clear();
2567f4a2713aSLionel Sambuc       for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2568f4a2713aSLionel Sambuc         if (Module *Exported = Mod->Exports[I].getPointer()) {
2569f4a2713aSLionel Sambuc           unsigned ExportedID = SubmoduleIDs[Exported];
2570f4a2713aSLionel Sambuc           assert(ExportedID > 0 && "Unknown submodule ID?");
2571f4a2713aSLionel Sambuc           Record.push_back(ExportedID);
2572f4a2713aSLionel Sambuc         } else {
2573f4a2713aSLionel Sambuc           Record.push_back(0);
2574f4a2713aSLionel Sambuc         }
2575f4a2713aSLionel Sambuc 
2576f4a2713aSLionel Sambuc         Record.push_back(Mod->Exports[I].getInt());
2577f4a2713aSLionel Sambuc       }
2578f4a2713aSLionel Sambuc       Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2579f4a2713aSLionel Sambuc     }
2580f4a2713aSLionel Sambuc 
2581f4a2713aSLionel Sambuc     //FIXME: How do we emit the 'use'd modules?  They may not be submodules.
2582f4a2713aSLionel Sambuc     // Might be unnecessary as use declarations are only used to build the
2583f4a2713aSLionel Sambuc     // module itself.
2584f4a2713aSLionel Sambuc 
2585f4a2713aSLionel Sambuc     // Emit the link libraries.
2586f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2587f4a2713aSLionel Sambuc       Record.clear();
2588f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_LINK_LIBRARY);
2589f4a2713aSLionel Sambuc       Record.push_back(Mod->LinkLibraries[I].IsFramework);
2590f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2591f4a2713aSLionel Sambuc                                 Mod->LinkLibraries[I].Library);
2592f4a2713aSLionel Sambuc     }
2593f4a2713aSLionel Sambuc 
2594f4a2713aSLionel Sambuc     // Emit the conflicts.
2595f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2596f4a2713aSLionel Sambuc       Record.clear();
2597f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_CONFLICT);
2598f4a2713aSLionel Sambuc       unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2599f4a2713aSLionel Sambuc       assert(OtherID && "Unknown submodule!");
2600f4a2713aSLionel Sambuc       Record.push_back(OtherID);
2601f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2602f4a2713aSLionel Sambuc                                 Mod->Conflicts[I].Message);
2603f4a2713aSLionel Sambuc     }
2604f4a2713aSLionel Sambuc 
2605f4a2713aSLionel Sambuc     // Emit the configuration macros.
2606f4a2713aSLionel Sambuc     for (unsigned I = 0, N =  Mod->ConfigMacros.size(); I != N; ++I) {
2607f4a2713aSLionel Sambuc       Record.clear();
2608f4a2713aSLionel Sambuc       Record.push_back(SUBMODULE_CONFIG_MACRO);
2609f4a2713aSLionel Sambuc       Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2610f4a2713aSLionel Sambuc                                 Mod->ConfigMacros[I]);
2611f4a2713aSLionel Sambuc     }
2612f4a2713aSLionel Sambuc 
2613f4a2713aSLionel Sambuc     // Queue up the submodules of this module.
2614f4a2713aSLionel Sambuc     for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2615f4a2713aSLionel Sambuc                                  SubEnd = Mod->submodule_end();
2616f4a2713aSLionel Sambuc          Sub != SubEnd; ++Sub)
2617f4a2713aSLionel Sambuc       Q.push(*Sub);
2618f4a2713aSLionel Sambuc   }
2619f4a2713aSLionel Sambuc 
2620f4a2713aSLionel Sambuc   Stream.ExitBlock();
2621f4a2713aSLionel Sambuc 
2622f4a2713aSLionel Sambuc   assert((NextSubmoduleID - FirstSubmoduleID
2623f4a2713aSLionel Sambuc             == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
2624f4a2713aSLionel Sambuc }
2625f4a2713aSLionel Sambuc 
2626f4a2713aSLionel Sambuc serialization::SubmoduleID
inferSubmoduleIDFromLocation(SourceLocation Loc)2627f4a2713aSLionel Sambuc ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
2628f4a2713aSLionel Sambuc   if (Loc.isInvalid() || !WritingModule)
2629f4a2713aSLionel Sambuc     return 0; // No submodule
2630f4a2713aSLionel Sambuc 
2631f4a2713aSLionel Sambuc   // Find the module that owns this location.
2632f4a2713aSLionel Sambuc   ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2633f4a2713aSLionel Sambuc   Module *OwningMod
2634f4a2713aSLionel Sambuc     = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
2635f4a2713aSLionel Sambuc   if (!OwningMod)
2636f4a2713aSLionel Sambuc     return 0;
2637f4a2713aSLionel Sambuc 
2638f4a2713aSLionel Sambuc   // Check whether this submodule is part of our own module.
2639f4a2713aSLionel Sambuc   if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
2640f4a2713aSLionel Sambuc     return 0;
2641f4a2713aSLionel Sambuc 
2642f4a2713aSLionel Sambuc   return getSubmoduleID(OwningMod);
2643f4a2713aSLionel Sambuc }
2644f4a2713aSLionel Sambuc 
WritePragmaDiagnosticMappings(const DiagnosticsEngine & Diag,bool isModule)2645f4a2713aSLionel Sambuc void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2646f4a2713aSLionel Sambuc                                               bool isModule) {
2647f4a2713aSLionel Sambuc   // Make sure set diagnostic pragmas don't affect the translation unit that
2648f4a2713aSLionel Sambuc   // imports the module.
2649f4a2713aSLionel Sambuc   // FIXME: Make diagnostic pragma sections work properly with modules.
2650f4a2713aSLionel Sambuc   if (isModule)
2651f4a2713aSLionel Sambuc     return;
2652f4a2713aSLionel Sambuc 
2653f4a2713aSLionel Sambuc   llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2654f4a2713aSLionel Sambuc       DiagStateIDMap;
2655f4a2713aSLionel Sambuc   unsigned CurrID = 0;
2656f4a2713aSLionel Sambuc   DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
2657f4a2713aSLionel Sambuc   RecordData Record;
2658f4a2713aSLionel Sambuc   for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
2659f4a2713aSLionel Sambuc          I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2660f4a2713aSLionel Sambuc          I != E; ++I) {
2661f4a2713aSLionel Sambuc     const DiagnosticsEngine::DiagStatePoint &point = *I;
2662f4a2713aSLionel Sambuc     if (point.Loc.isInvalid())
2663f4a2713aSLionel Sambuc       continue;
2664f4a2713aSLionel Sambuc 
2665f4a2713aSLionel Sambuc     Record.push_back(point.Loc.getRawEncoding());
2666f4a2713aSLionel Sambuc     unsigned &DiagStateID = DiagStateIDMap[point.State];
2667f4a2713aSLionel Sambuc     Record.push_back(DiagStateID);
2668f4a2713aSLionel Sambuc 
2669f4a2713aSLionel Sambuc     if (DiagStateID == 0) {
2670f4a2713aSLionel Sambuc       DiagStateID = ++CurrID;
2671f4a2713aSLionel Sambuc       for (DiagnosticsEngine::DiagState::const_iterator
2672f4a2713aSLionel Sambuc              I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2673f4a2713aSLionel Sambuc         if (I->second.isPragma()) {
2674f4a2713aSLionel Sambuc           Record.push_back(I->first);
2675*0a6a1f1dSLionel Sambuc           Record.push_back((unsigned)I->second.getSeverity());
2676f4a2713aSLionel Sambuc         }
2677f4a2713aSLionel Sambuc       }
2678f4a2713aSLionel Sambuc       Record.push_back(-1); // mark the end of the diag/map pairs for this
2679f4a2713aSLionel Sambuc                             // location.
2680f4a2713aSLionel Sambuc     }
2681f4a2713aSLionel Sambuc   }
2682f4a2713aSLionel Sambuc 
2683f4a2713aSLionel Sambuc   if (!Record.empty())
2684f4a2713aSLionel Sambuc     Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
2685f4a2713aSLionel Sambuc }
2686f4a2713aSLionel Sambuc 
WriteCXXBaseSpecifiersOffsets()2687f4a2713aSLionel Sambuc void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2688f4a2713aSLionel Sambuc   if (CXXBaseSpecifiersOffsets.empty())
2689f4a2713aSLionel Sambuc     return;
2690f4a2713aSLionel Sambuc 
2691f4a2713aSLionel Sambuc   RecordData Record;
2692f4a2713aSLionel Sambuc 
2693f4a2713aSLionel Sambuc   // Create a blob abbreviation for the C++ base specifiers offsets.
2694f4a2713aSLionel Sambuc   using namespace llvm;
2695f4a2713aSLionel Sambuc 
2696f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2697f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2698f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2699f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2700f4a2713aSLionel Sambuc   unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2701f4a2713aSLionel Sambuc 
2702f4a2713aSLionel Sambuc   // Write the base specifier offsets table.
2703f4a2713aSLionel Sambuc   Record.clear();
2704f4a2713aSLionel Sambuc   Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2705f4a2713aSLionel Sambuc   Record.push_back(CXXBaseSpecifiersOffsets.size());
2706f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2707f4a2713aSLionel Sambuc                             data(CXXBaseSpecifiersOffsets));
2708f4a2713aSLionel Sambuc }
2709f4a2713aSLionel Sambuc 
2710f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2711f4a2713aSLionel Sambuc // Type Serialization
2712f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2713f4a2713aSLionel Sambuc 
2714f4a2713aSLionel Sambuc /// \brief Write the representation of a type to the AST stream.
WriteType(QualType T)2715f4a2713aSLionel Sambuc void ASTWriter::WriteType(QualType T) {
2716f4a2713aSLionel Sambuc   TypeIdx &Idx = TypeIdxs[T];
2717f4a2713aSLionel Sambuc   if (Idx.getIndex() == 0) // we haven't seen this type before.
2718f4a2713aSLionel Sambuc     Idx = TypeIdx(NextTypeID++);
2719f4a2713aSLionel Sambuc 
2720f4a2713aSLionel Sambuc   assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
2721f4a2713aSLionel Sambuc 
2722f4a2713aSLionel Sambuc   // Record the offset for this type.
2723f4a2713aSLionel Sambuc   unsigned Index = Idx.getIndex() - FirstTypeID;
2724f4a2713aSLionel Sambuc   if (TypeOffsets.size() == Index)
2725f4a2713aSLionel Sambuc     TypeOffsets.push_back(Stream.GetCurrentBitNo());
2726f4a2713aSLionel Sambuc   else if (TypeOffsets.size() < Index) {
2727f4a2713aSLionel Sambuc     TypeOffsets.resize(Index + 1);
2728f4a2713aSLionel Sambuc     TypeOffsets[Index] = Stream.GetCurrentBitNo();
2729f4a2713aSLionel Sambuc   }
2730f4a2713aSLionel Sambuc 
2731f4a2713aSLionel Sambuc   RecordData Record;
2732f4a2713aSLionel Sambuc 
2733f4a2713aSLionel Sambuc   // Emit the type's representation.
2734f4a2713aSLionel Sambuc   ASTTypeWriter W(*this, Record);
2735*0a6a1f1dSLionel Sambuc   W.AbbrevToUse = 0;
2736f4a2713aSLionel Sambuc 
2737f4a2713aSLionel Sambuc   if (T.hasLocalNonFastQualifiers()) {
2738f4a2713aSLionel Sambuc     Qualifiers Qs = T.getLocalQualifiers();
2739f4a2713aSLionel Sambuc     AddTypeRef(T.getLocalUnqualifiedType(), Record);
2740f4a2713aSLionel Sambuc     Record.push_back(Qs.getAsOpaqueValue());
2741f4a2713aSLionel Sambuc     W.Code = TYPE_EXT_QUAL;
2742*0a6a1f1dSLionel Sambuc     W.AbbrevToUse = TypeExtQualAbbrev;
2743f4a2713aSLionel Sambuc   } else {
2744f4a2713aSLionel Sambuc     switch (T->getTypeClass()) {
2745f4a2713aSLionel Sambuc       // For all of the concrete, non-dependent types, call the
2746f4a2713aSLionel Sambuc       // appropriate visitor function.
2747f4a2713aSLionel Sambuc #define TYPE(Class, Base) \
2748f4a2713aSLionel Sambuc     case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
2749f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
2750f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
2751f4a2713aSLionel Sambuc     }
2752f4a2713aSLionel Sambuc   }
2753f4a2713aSLionel Sambuc 
2754f4a2713aSLionel Sambuc   // Emit the serialized record.
2755*0a6a1f1dSLionel Sambuc   Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
2756f4a2713aSLionel Sambuc 
2757f4a2713aSLionel Sambuc   // Flush any expressions that were written as part of this type.
2758f4a2713aSLionel Sambuc   FlushStmts();
2759f4a2713aSLionel Sambuc }
2760f4a2713aSLionel Sambuc 
2761f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2762f4a2713aSLionel Sambuc // Declaration Serialization
2763f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2764f4a2713aSLionel Sambuc 
2765f4a2713aSLionel Sambuc /// \brief Write the block containing all of the declaration IDs
2766f4a2713aSLionel Sambuc /// lexically declared within the given DeclContext.
2767f4a2713aSLionel Sambuc ///
2768f4a2713aSLionel Sambuc /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2769f4a2713aSLionel Sambuc /// bistream, or 0 if no block was written.
WriteDeclContextLexicalBlock(ASTContext & Context,DeclContext * DC)2770f4a2713aSLionel Sambuc uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
2771f4a2713aSLionel Sambuc                                                  DeclContext *DC) {
2772f4a2713aSLionel Sambuc   if (DC->decls_empty())
2773f4a2713aSLionel Sambuc     return 0;
2774f4a2713aSLionel Sambuc 
2775f4a2713aSLionel Sambuc   uint64_t Offset = Stream.GetCurrentBitNo();
2776f4a2713aSLionel Sambuc   RecordData Record;
2777f4a2713aSLionel Sambuc   Record.push_back(DECL_CONTEXT_LEXICAL);
2778f4a2713aSLionel Sambuc   SmallVector<KindDeclIDPair, 64> Decls;
2779*0a6a1f1dSLionel Sambuc   for (const auto *D : DC->decls())
2780*0a6a1f1dSLionel Sambuc     Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
2781f4a2713aSLionel Sambuc 
2782f4a2713aSLionel Sambuc   ++NumLexicalDeclContexts;
2783f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
2784f4a2713aSLionel Sambuc   return Offset;
2785f4a2713aSLionel Sambuc }
2786f4a2713aSLionel Sambuc 
WriteTypeDeclOffsets()2787f4a2713aSLionel Sambuc void ASTWriter::WriteTypeDeclOffsets() {
2788f4a2713aSLionel Sambuc   using namespace llvm;
2789f4a2713aSLionel Sambuc   RecordData Record;
2790f4a2713aSLionel Sambuc 
2791f4a2713aSLionel Sambuc   // Write the type offsets array
2792f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2793f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
2794f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2795f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
2796f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2797f4a2713aSLionel Sambuc   unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2798f4a2713aSLionel Sambuc   Record.clear();
2799f4a2713aSLionel Sambuc   Record.push_back(TYPE_OFFSET);
2800f4a2713aSLionel Sambuc   Record.push_back(TypeOffsets.size());
2801f4a2713aSLionel Sambuc   Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
2802f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
2803f4a2713aSLionel Sambuc 
2804f4a2713aSLionel Sambuc   // Write the declaration offsets array
2805f4a2713aSLionel Sambuc   Abbrev = new BitCodeAbbrev();
2806f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
2807f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2808f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
2809f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2810f4a2713aSLionel Sambuc   unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2811f4a2713aSLionel Sambuc   Record.clear();
2812f4a2713aSLionel Sambuc   Record.push_back(DECL_OFFSET);
2813f4a2713aSLionel Sambuc   Record.push_back(DeclOffsets.size());
2814f4a2713aSLionel Sambuc   Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
2815f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
2816f4a2713aSLionel Sambuc }
2817f4a2713aSLionel Sambuc 
WriteFileDeclIDsMap()2818f4a2713aSLionel Sambuc void ASTWriter::WriteFileDeclIDsMap() {
2819f4a2713aSLionel Sambuc   using namespace llvm;
2820f4a2713aSLionel Sambuc   RecordData Record;
2821f4a2713aSLionel Sambuc 
2822f4a2713aSLionel Sambuc   // Join the vectors of DeclIDs from all files.
2823f4a2713aSLionel Sambuc   SmallVector<DeclID, 256> FileSortedIDs;
2824f4a2713aSLionel Sambuc   for (FileDeclIDsTy::iterator
2825f4a2713aSLionel Sambuc          FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2826f4a2713aSLionel Sambuc     DeclIDInFileInfo &Info = *FI->second;
2827f4a2713aSLionel Sambuc     Info.FirstDeclIndex = FileSortedIDs.size();
2828f4a2713aSLionel Sambuc     for (LocDeclIDsTy::iterator
2829f4a2713aSLionel Sambuc            DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2830f4a2713aSLionel Sambuc       FileSortedIDs.push_back(DI->second);
2831f4a2713aSLionel Sambuc   }
2832f4a2713aSLionel Sambuc 
2833f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2834f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2835f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2836f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2837f4a2713aSLionel Sambuc   unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2838f4a2713aSLionel Sambuc   Record.push_back(FILE_SORTED_DECLS);
2839f4a2713aSLionel Sambuc   Record.push_back(FileSortedIDs.size());
2840f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2841f4a2713aSLionel Sambuc }
2842f4a2713aSLionel Sambuc 
WriteComments()2843f4a2713aSLionel Sambuc void ASTWriter::WriteComments() {
2844f4a2713aSLionel Sambuc   Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
2845f4a2713aSLionel Sambuc   ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
2846f4a2713aSLionel Sambuc   RecordData Record;
2847f4a2713aSLionel Sambuc   for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2848f4a2713aSLionel Sambuc                                         E = RawComments.end();
2849f4a2713aSLionel Sambuc        I != E; ++I) {
2850f4a2713aSLionel Sambuc     Record.clear();
2851f4a2713aSLionel Sambuc     AddSourceRange((*I)->getSourceRange(), Record);
2852f4a2713aSLionel Sambuc     Record.push_back((*I)->getKind());
2853f4a2713aSLionel Sambuc     Record.push_back((*I)->isTrailingComment());
2854f4a2713aSLionel Sambuc     Record.push_back((*I)->isAlmostTrailingComment());
2855f4a2713aSLionel Sambuc     Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2856f4a2713aSLionel Sambuc   }
2857f4a2713aSLionel Sambuc   Stream.ExitBlock();
2858f4a2713aSLionel Sambuc }
2859f4a2713aSLionel Sambuc 
2860f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2861f4a2713aSLionel Sambuc // Global Method Pool and Selector Serialization
2862f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2863f4a2713aSLionel Sambuc 
2864f4a2713aSLionel Sambuc namespace {
2865f4a2713aSLionel Sambuc // Trait used for the on-disk hash table used in the method pool.
2866f4a2713aSLionel Sambuc class ASTMethodPoolTrait {
2867f4a2713aSLionel Sambuc   ASTWriter &Writer;
2868f4a2713aSLionel Sambuc 
2869f4a2713aSLionel Sambuc public:
2870f4a2713aSLionel Sambuc   typedef Selector key_type;
2871f4a2713aSLionel Sambuc   typedef key_type key_type_ref;
2872f4a2713aSLionel Sambuc 
2873f4a2713aSLionel Sambuc   struct data_type {
2874f4a2713aSLionel Sambuc     SelectorID ID;
2875f4a2713aSLionel Sambuc     ObjCMethodList Instance, Factory;
2876f4a2713aSLionel Sambuc   };
2877f4a2713aSLionel Sambuc   typedef const data_type& data_type_ref;
2878f4a2713aSLionel Sambuc 
2879*0a6a1f1dSLionel Sambuc   typedef unsigned hash_value_type;
2880*0a6a1f1dSLionel Sambuc   typedef unsigned offset_type;
2881*0a6a1f1dSLionel Sambuc 
ASTMethodPoolTrait(ASTWriter & Writer)2882f4a2713aSLionel Sambuc   explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
2883f4a2713aSLionel Sambuc 
ComputeHash(Selector Sel)2884*0a6a1f1dSLionel Sambuc   static hash_value_type ComputeHash(Selector Sel) {
2885f4a2713aSLionel Sambuc     return serialization::ComputeHash(Sel);
2886f4a2713aSLionel Sambuc   }
2887f4a2713aSLionel Sambuc 
2888f4a2713aSLionel Sambuc   std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,Selector Sel,data_type_ref Methods)2889f4a2713aSLionel Sambuc     EmitKeyDataLength(raw_ostream& Out, Selector Sel,
2890f4a2713aSLionel Sambuc                       data_type_ref Methods) {
2891*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
2892*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
2893f4a2713aSLionel Sambuc     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2894*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(KeyLen);
2895f4a2713aSLionel Sambuc     unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2896f4a2713aSLionel Sambuc     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2897f4a2713aSLionel Sambuc          Method = Method->getNext())
2898*0a6a1f1dSLionel Sambuc       if (Method->getMethod())
2899f4a2713aSLionel Sambuc         DataLen += 4;
2900f4a2713aSLionel Sambuc     for (const ObjCMethodList *Method = &Methods.Factory; Method;
2901f4a2713aSLionel Sambuc          Method = Method->getNext())
2902*0a6a1f1dSLionel Sambuc       if (Method->getMethod())
2903f4a2713aSLionel Sambuc         DataLen += 4;
2904*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(DataLen);
2905f4a2713aSLionel Sambuc     return std::make_pair(KeyLen, DataLen);
2906f4a2713aSLionel Sambuc   }
2907f4a2713aSLionel Sambuc 
EmitKey(raw_ostream & Out,Selector Sel,unsigned)2908f4a2713aSLionel Sambuc   void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
2909*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
2910*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
2911f4a2713aSLionel Sambuc     uint64_t Start = Out.tell();
2912f4a2713aSLionel Sambuc     assert((Start >> 32) == 0 && "Selector key offset too large");
2913f4a2713aSLionel Sambuc     Writer.SetSelectorOffset(Sel, Start);
2914f4a2713aSLionel Sambuc     unsigned N = Sel.getNumArgs();
2915*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(N);
2916f4a2713aSLionel Sambuc     if (N == 0)
2917f4a2713aSLionel Sambuc       N = 1;
2918f4a2713aSLionel Sambuc     for (unsigned I = 0; I != N; ++I)
2919*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(
2920f4a2713aSLionel Sambuc           Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2921f4a2713aSLionel Sambuc   }
2922f4a2713aSLionel Sambuc 
EmitData(raw_ostream & Out,key_type_ref,data_type_ref Methods,unsigned DataLen)2923f4a2713aSLionel Sambuc   void EmitData(raw_ostream& Out, key_type_ref,
2924f4a2713aSLionel Sambuc                 data_type_ref Methods, unsigned DataLen) {
2925*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
2926*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
2927f4a2713aSLionel Sambuc     uint64_t Start = Out.tell(); (void)Start;
2928*0a6a1f1dSLionel Sambuc     LE.write<uint32_t>(Methods.ID);
2929f4a2713aSLionel Sambuc     unsigned NumInstanceMethods = 0;
2930f4a2713aSLionel Sambuc     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2931f4a2713aSLionel Sambuc          Method = Method->getNext())
2932*0a6a1f1dSLionel Sambuc       if (Method->getMethod())
2933f4a2713aSLionel Sambuc         ++NumInstanceMethods;
2934f4a2713aSLionel Sambuc 
2935f4a2713aSLionel Sambuc     unsigned NumFactoryMethods = 0;
2936f4a2713aSLionel Sambuc     for (const ObjCMethodList *Method = &Methods.Factory; Method;
2937f4a2713aSLionel Sambuc          Method = Method->getNext())
2938*0a6a1f1dSLionel Sambuc       if (Method->getMethod())
2939f4a2713aSLionel Sambuc         ++NumFactoryMethods;
2940f4a2713aSLionel Sambuc 
2941f4a2713aSLionel Sambuc     unsigned InstanceBits = Methods.Instance.getBits();
2942f4a2713aSLionel Sambuc     assert(InstanceBits < 4);
2943*0a6a1f1dSLionel Sambuc     unsigned InstanceHasMoreThanOneDeclBit =
2944*0a6a1f1dSLionel Sambuc         Methods.Instance.hasMoreThanOneDecl();
2945*0a6a1f1dSLionel Sambuc     unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2946*0a6a1f1dSLionel Sambuc                                 (InstanceHasMoreThanOneDeclBit << 2) |
2947*0a6a1f1dSLionel Sambuc                                 InstanceBits;
2948f4a2713aSLionel Sambuc     unsigned FactoryBits = Methods.Factory.getBits();
2949f4a2713aSLionel Sambuc     assert(FactoryBits < 4);
2950*0a6a1f1dSLionel Sambuc     unsigned FactoryHasMoreThanOneDeclBit =
2951*0a6a1f1dSLionel Sambuc         Methods.Factory.hasMoreThanOneDecl();
2952*0a6a1f1dSLionel Sambuc     unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2953*0a6a1f1dSLionel Sambuc                                (FactoryHasMoreThanOneDeclBit << 2) |
2954*0a6a1f1dSLionel Sambuc                                FactoryBits;
2955*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(FullInstanceBits);
2956*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(FullFactoryBits);
2957f4a2713aSLionel Sambuc     for (const ObjCMethodList *Method = &Methods.Instance; Method;
2958f4a2713aSLionel Sambuc          Method = Method->getNext())
2959*0a6a1f1dSLionel Sambuc       if (Method->getMethod())
2960*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
2961f4a2713aSLionel Sambuc     for (const ObjCMethodList *Method = &Methods.Factory; Method;
2962f4a2713aSLionel Sambuc          Method = Method->getNext())
2963*0a6a1f1dSLionel Sambuc       if (Method->getMethod())
2964*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
2965f4a2713aSLionel Sambuc 
2966f4a2713aSLionel Sambuc     assert(Out.tell() - Start == DataLen && "Data length is wrong");
2967f4a2713aSLionel Sambuc   }
2968f4a2713aSLionel Sambuc };
2969f4a2713aSLionel Sambuc } // end anonymous namespace
2970f4a2713aSLionel Sambuc 
2971f4a2713aSLionel Sambuc /// \brief Write ObjC data: selectors and the method pool.
2972f4a2713aSLionel Sambuc ///
2973f4a2713aSLionel Sambuc /// The method pool contains both instance and factory methods, stored
2974f4a2713aSLionel Sambuc /// in an on-disk hash table indexed by the selector. The hash table also
2975f4a2713aSLionel Sambuc /// contains an empty entry for every other selector known to Sema.
WriteSelectors(Sema & SemaRef)2976f4a2713aSLionel Sambuc void ASTWriter::WriteSelectors(Sema &SemaRef) {
2977f4a2713aSLionel Sambuc   using namespace llvm;
2978f4a2713aSLionel Sambuc 
2979f4a2713aSLionel Sambuc   // Do we have to do anything at all?
2980f4a2713aSLionel Sambuc   if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
2981f4a2713aSLionel Sambuc     return;
2982f4a2713aSLionel Sambuc   unsigned NumTableEntries = 0;
2983f4a2713aSLionel Sambuc   // Create and write out the blob that contains selectors and the method pool.
2984f4a2713aSLionel Sambuc   {
2985*0a6a1f1dSLionel Sambuc     llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
2986f4a2713aSLionel Sambuc     ASTMethodPoolTrait Trait(*this);
2987f4a2713aSLionel Sambuc 
2988f4a2713aSLionel Sambuc     // Create the on-disk hash table representation. We walk through every
2989f4a2713aSLionel Sambuc     // selector we've seen and look it up in the method pool.
2990f4a2713aSLionel Sambuc     SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
2991f4a2713aSLionel Sambuc     for (llvm::DenseMap<Selector, SelectorID>::iterator
2992f4a2713aSLionel Sambuc              I = SelectorIDs.begin(), E = SelectorIDs.end();
2993f4a2713aSLionel Sambuc          I != E; ++I) {
2994f4a2713aSLionel Sambuc       Selector S = I->first;
2995f4a2713aSLionel Sambuc       Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
2996f4a2713aSLionel Sambuc       ASTMethodPoolTrait::data_type Data = {
2997f4a2713aSLionel Sambuc         I->second,
2998f4a2713aSLionel Sambuc         ObjCMethodList(),
2999f4a2713aSLionel Sambuc         ObjCMethodList()
3000f4a2713aSLionel Sambuc       };
3001f4a2713aSLionel Sambuc       if (F != SemaRef.MethodPool.end()) {
3002f4a2713aSLionel Sambuc         Data.Instance = F->second.first;
3003f4a2713aSLionel Sambuc         Data.Factory = F->second.second;
3004f4a2713aSLionel Sambuc       }
3005f4a2713aSLionel Sambuc       // Only write this selector if it's not in an existing AST or something
3006f4a2713aSLionel Sambuc       // changed.
3007f4a2713aSLionel Sambuc       if (Chain && I->second < FirstSelectorID) {
3008f4a2713aSLionel Sambuc         // Selector already exists. Did it change?
3009f4a2713aSLionel Sambuc         bool changed = false;
3010*0a6a1f1dSLionel Sambuc         for (ObjCMethodList *M = &Data.Instance;
3011*0a6a1f1dSLionel Sambuc              !changed && M && M->getMethod(); M = M->getNext()) {
3012*0a6a1f1dSLionel Sambuc           if (!M->getMethod()->isFromASTFile())
3013f4a2713aSLionel Sambuc             changed = true;
3014f4a2713aSLionel Sambuc         }
3015*0a6a1f1dSLionel Sambuc         for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
3016f4a2713aSLionel Sambuc              M = M->getNext()) {
3017*0a6a1f1dSLionel Sambuc           if (!M->getMethod()->isFromASTFile())
3018f4a2713aSLionel Sambuc             changed = true;
3019f4a2713aSLionel Sambuc         }
3020f4a2713aSLionel Sambuc         if (!changed)
3021f4a2713aSLionel Sambuc           continue;
3022*0a6a1f1dSLionel Sambuc       } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3023f4a2713aSLionel Sambuc         // A new method pool entry.
3024f4a2713aSLionel Sambuc         ++NumTableEntries;
3025f4a2713aSLionel Sambuc       }
3026f4a2713aSLionel Sambuc       Generator.insert(S, Data, Trait);
3027f4a2713aSLionel Sambuc     }
3028f4a2713aSLionel Sambuc 
3029f4a2713aSLionel Sambuc     // Create the on-disk hash table in a buffer.
3030f4a2713aSLionel Sambuc     SmallString<4096> MethodPool;
3031f4a2713aSLionel Sambuc     uint32_t BucketOffset;
3032f4a2713aSLionel Sambuc     {
3033*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
3034f4a2713aSLionel Sambuc       ASTMethodPoolTrait Trait(*this);
3035f4a2713aSLionel Sambuc       llvm::raw_svector_ostream Out(MethodPool);
3036f4a2713aSLionel Sambuc       // Make sure that no bucket is at offset 0
3037*0a6a1f1dSLionel Sambuc       endian::Writer<little>(Out).write<uint32_t>(0);
3038f4a2713aSLionel Sambuc       BucketOffset = Generator.Emit(Out, Trait);
3039f4a2713aSLionel Sambuc     }
3040f4a2713aSLionel Sambuc 
3041f4a2713aSLionel Sambuc     // Create a blob abbreviation
3042f4a2713aSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3043f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3044f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3045f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3046f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3047f4a2713aSLionel Sambuc     unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3048f4a2713aSLionel Sambuc 
3049f4a2713aSLionel Sambuc     // Write the method pool
3050f4a2713aSLionel Sambuc     RecordData Record;
3051f4a2713aSLionel Sambuc     Record.push_back(METHOD_POOL);
3052f4a2713aSLionel Sambuc     Record.push_back(BucketOffset);
3053f4a2713aSLionel Sambuc     Record.push_back(NumTableEntries);
3054f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
3055f4a2713aSLionel Sambuc 
3056f4a2713aSLionel Sambuc     // Create a blob abbreviation for the selector table offsets.
3057f4a2713aSLionel Sambuc     Abbrev = new BitCodeAbbrev();
3058f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3059f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3060f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3061f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3062f4a2713aSLionel Sambuc     unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3063f4a2713aSLionel Sambuc 
3064f4a2713aSLionel Sambuc     // Write the selector offsets table.
3065f4a2713aSLionel Sambuc     Record.clear();
3066f4a2713aSLionel Sambuc     Record.push_back(SELECTOR_OFFSETS);
3067f4a2713aSLionel Sambuc     Record.push_back(SelectorOffsets.size());
3068f4a2713aSLionel Sambuc     Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
3069f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3070f4a2713aSLionel Sambuc                               data(SelectorOffsets));
3071f4a2713aSLionel Sambuc   }
3072f4a2713aSLionel Sambuc }
3073f4a2713aSLionel Sambuc 
3074f4a2713aSLionel Sambuc /// \brief Write the selectors referenced in @selector expression into AST file.
WriteReferencedSelectorsPool(Sema & SemaRef)3075f4a2713aSLionel Sambuc void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3076f4a2713aSLionel Sambuc   using namespace llvm;
3077f4a2713aSLionel Sambuc   if (SemaRef.ReferencedSelectors.empty())
3078f4a2713aSLionel Sambuc     return;
3079f4a2713aSLionel Sambuc 
3080f4a2713aSLionel Sambuc   RecordData Record;
3081f4a2713aSLionel Sambuc 
3082f4a2713aSLionel Sambuc   // Note: this writes out all references even for a dependent AST. But it is
3083f4a2713aSLionel Sambuc   // very tricky to fix, and given that @selector shouldn't really appear in
3084f4a2713aSLionel Sambuc   // headers, probably not worth it. It's not a correctness issue.
3085f4a2713aSLionel Sambuc   for (DenseMap<Selector, SourceLocation>::iterator S =
3086f4a2713aSLionel Sambuc        SemaRef.ReferencedSelectors.begin(),
3087f4a2713aSLionel Sambuc        E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3088f4a2713aSLionel Sambuc     Selector Sel = (*S).first;
3089f4a2713aSLionel Sambuc     SourceLocation Loc = (*S).second;
3090f4a2713aSLionel Sambuc     AddSelectorRef(Sel, Record);
3091f4a2713aSLionel Sambuc     AddSourceLocation(Loc, Record);
3092f4a2713aSLionel Sambuc   }
3093f4a2713aSLionel Sambuc   Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
3094f4a2713aSLionel Sambuc }
3095f4a2713aSLionel Sambuc 
3096f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3097f4a2713aSLionel Sambuc // Identifier Table Serialization
3098f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3099f4a2713aSLionel Sambuc 
3100f4a2713aSLionel Sambuc namespace {
3101f4a2713aSLionel Sambuc class ASTIdentifierTableTrait {
3102f4a2713aSLionel Sambuc   ASTWriter &Writer;
3103f4a2713aSLionel Sambuc   Preprocessor &PP;
3104f4a2713aSLionel Sambuc   IdentifierResolver &IdResolver;
3105f4a2713aSLionel Sambuc   bool IsModule;
3106f4a2713aSLionel Sambuc 
3107f4a2713aSLionel Sambuc   /// \brief Determines whether this is an "interesting" identifier
3108f4a2713aSLionel Sambuc   /// that needs a full IdentifierInfo structure written into the hash
3109f4a2713aSLionel Sambuc   /// table.
isInterestingIdentifier(IdentifierInfo * II,MacroDirective * & Macro)3110f4a2713aSLionel Sambuc   bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
3111f4a2713aSLionel Sambuc     if (II->isPoisoned() ||
3112f4a2713aSLionel Sambuc         II->isExtensionToken() ||
3113f4a2713aSLionel Sambuc         II->getObjCOrBuiltinID() ||
3114f4a2713aSLionel Sambuc         II->hasRevertedTokenIDToIdentifier() ||
3115f4a2713aSLionel Sambuc         II->getFETokenInfo<void>())
3116f4a2713aSLionel Sambuc       return true;
3117f4a2713aSLionel Sambuc 
3118f4a2713aSLionel Sambuc     return hadMacroDefinition(II, Macro);
3119f4a2713aSLionel Sambuc   }
3120f4a2713aSLionel Sambuc 
hadMacroDefinition(IdentifierInfo * II,MacroDirective * & Macro)3121f4a2713aSLionel Sambuc   bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
3122f4a2713aSLionel Sambuc     if (!II->hadMacroDefinition())
3123f4a2713aSLionel Sambuc       return false;
3124f4a2713aSLionel Sambuc 
3125f4a2713aSLionel Sambuc     if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3126f4a2713aSLionel Sambuc       if (!IsModule)
3127f4a2713aSLionel Sambuc         return !shouldIgnoreMacro(Macro, IsModule, PP);
3128*0a6a1f1dSLionel Sambuc 
3129*0a6a1f1dSLionel Sambuc       MacroState State;
3130*0a6a1f1dSLionel Sambuc       if (getFirstPublicSubmoduleMacro(Macro, State))
3131f4a2713aSLionel Sambuc         return true;
3132f4a2713aSLionel Sambuc     }
3133f4a2713aSLionel Sambuc 
3134f4a2713aSLionel Sambuc     return false;
3135f4a2713aSLionel Sambuc   }
3136f4a2713aSLionel Sambuc 
3137*0a6a1f1dSLionel Sambuc   enum class SubmoduleMacroState {
3138*0a6a1f1dSLionel Sambuc     /// We've seen nothing about this macro.
3139*0a6a1f1dSLionel Sambuc     None,
3140*0a6a1f1dSLionel Sambuc     /// We've seen a public visibility directive.
3141*0a6a1f1dSLionel Sambuc     Public,
3142*0a6a1f1dSLionel Sambuc     /// We've either exported a macro for this module or found that the
3143*0a6a1f1dSLionel Sambuc     /// module's definition of this macro is private.
3144*0a6a1f1dSLionel Sambuc     Done
3145*0a6a1f1dSLionel Sambuc   };
3146*0a6a1f1dSLionel Sambuc   typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
3147*0a6a1f1dSLionel Sambuc 
3148*0a6a1f1dSLionel Sambuc   MacroDirective *
getFirstPublicSubmoduleMacro(MacroDirective * MD,MacroState & State)3149*0a6a1f1dSLionel Sambuc   getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3150*0a6a1f1dSLionel Sambuc     if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3151*0a6a1f1dSLionel Sambuc       return NextMD;
3152*0a6a1f1dSLionel Sambuc     return nullptr;
3153f4a2713aSLionel Sambuc   }
3154f4a2713aSLionel Sambuc 
3155*0a6a1f1dSLionel Sambuc   MacroDirective *
getNextPublicSubmoduleMacro(MacroDirective * MD,MacroState & State)3156*0a6a1f1dSLionel Sambuc   getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3157*0a6a1f1dSLionel Sambuc     if (MacroDirective *NextMD =
3158*0a6a1f1dSLionel Sambuc             getPublicSubmoduleMacro(MD->getPrevious(), State))
3159*0a6a1f1dSLionel Sambuc       return NextMD;
3160*0a6a1f1dSLionel Sambuc     return nullptr;
3161f4a2713aSLionel Sambuc   }
3162f4a2713aSLionel Sambuc 
3163*0a6a1f1dSLionel Sambuc   /// \brief Traverses the macro directives history and returns the next
3164*0a6a1f1dSLionel Sambuc   /// public macro definition or undefinition that has not been found so far.
3165*0a6a1f1dSLionel Sambuc   ///
3166*0a6a1f1dSLionel Sambuc   /// A macro that is defined in submodule A and undefined in submodule B
3167f4a2713aSLionel Sambuc   /// will still be considered as defined/exported from submodule A.
getPublicSubmoduleMacro(MacroDirective * MD,MacroState & State)3168*0a6a1f1dSLionel Sambuc   MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
3169*0a6a1f1dSLionel Sambuc                                           MacroState &State) {
3170f4a2713aSLionel Sambuc     if (!MD)
3171*0a6a1f1dSLionel Sambuc       return nullptr;
3172f4a2713aSLionel Sambuc 
3173*0a6a1f1dSLionel Sambuc     Optional<bool> IsPublic;
3174f4a2713aSLionel Sambuc     for (; MD; MD = MD->getPrevious()) {
3175*0a6a1f1dSLionel Sambuc       // Once we hit an ignored macro, we're done: the rest of the chain
3176*0a6a1f1dSLionel Sambuc       // will all be ignored macros.
3177*0a6a1f1dSLionel Sambuc       if (shouldIgnoreMacro(MD, IsModule, PP))
3178*0a6a1f1dSLionel Sambuc         break;
3179*0a6a1f1dSLionel Sambuc 
3180*0a6a1f1dSLionel Sambuc       // If this macro was imported, re-export it.
3181*0a6a1f1dSLionel Sambuc       if (MD->isImported())
3182*0a6a1f1dSLionel Sambuc         return MD;
3183*0a6a1f1dSLionel Sambuc 
3184*0a6a1f1dSLionel Sambuc       SubmoduleID ModID = getSubmoduleID(MD);
3185*0a6a1f1dSLionel Sambuc       auto &S = State[ModID];
3186*0a6a1f1dSLionel Sambuc       assert(ModID && "found macro in no submodule");
3187*0a6a1f1dSLionel Sambuc 
3188*0a6a1f1dSLionel Sambuc       if (S == SubmoduleMacroState::Done)
3189f4a2713aSLionel Sambuc         continue;
3190f4a2713aSLionel Sambuc 
3191*0a6a1f1dSLionel Sambuc       if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3192*0a6a1f1dSLionel Sambuc         // The latest visibility directive for a name in a submodule affects all
3193*0a6a1f1dSLionel Sambuc         // the directives that come before it.
3194*0a6a1f1dSLionel Sambuc         if (S == SubmoduleMacroState::None)
3195*0a6a1f1dSLionel Sambuc           S = VisMD->isPublic() ? SubmoduleMacroState::Public
3196*0a6a1f1dSLionel Sambuc                                 : SubmoduleMacroState::Done;
3197*0a6a1f1dSLionel Sambuc       } else {
3198*0a6a1f1dSLionel Sambuc         S = SubmoduleMacroState::Done;
3199*0a6a1f1dSLionel Sambuc         return MD;
3200f4a2713aSLionel Sambuc       }
3201f4a2713aSLionel Sambuc     }
3202f4a2713aSLionel Sambuc 
3203*0a6a1f1dSLionel Sambuc     return nullptr;
3204f4a2713aSLionel Sambuc   }
3205f4a2713aSLionel Sambuc 
3206*0a6a1f1dSLionel Sambuc   ArrayRef<SubmoduleID>
getOverriddenSubmodules(MacroDirective * MD,SmallVectorImpl<SubmoduleID> & ScratchSpace)3207*0a6a1f1dSLionel Sambuc   getOverriddenSubmodules(MacroDirective *MD,
3208*0a6a1f1dSLionel Sambuc                           SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3209*0a6a1f1dSLionel Sambuc     assert(!isa<VisibilityMacroDirective>(MD) &&
3210*0a6a1f1dSLionel Sambuc            "only #define and #undef can override");
3211*0a6a1f1dSLionel Sambuc     if (MD->isImported())
3212*0a6a1f1dSLionel Sambuc       return MD->getOverriddenModules();
3213*0a6a1f1dSLionel Sambuc 
3214*0a6a1f1dSLionel Sambuc     ScratchSpace.clear();
3215*0a6a1f1dSLionel Sambuc     SubmoduleID ModID = getSubmoduleID(MD);
3216*0a6a1f1dSLionel Sambuc     for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3217*0a6a1f1dSLionel Sambuc       if (shouldIgnoreMacro(MD, IsModule, PP))
3218*0a6a1f1dSLionel Sambuc         break;
3219*0a6a1f1dSLionel Sambuc 
3220*0a6a1f1dSLionel Sambuc       // If this is a definition from a submodule import, that submodule's
3221*0a6a1f1dSLionel Sambuc       // definition is overridden by the definition or undefinition that we
3222*0a6a1f1dSLionel Sambuc       // started with.
3223*0a6a1f1dSLionel Sambuc       if (MD->isImported()) {
3224*0a6a1f1dSLionel Sambuc         if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3225*0a6a1f1dSLionel Sambuc           SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3226*0a6a1f1dSLionel Sambuc           assert(DefModuleID && "imported macro has no owning module");
3227*0a6a1f1dSLionel Sambuc           ScratchSpace.push_back(DefModuleID);
3228*0a6a1f1dSLionel Sambuc         } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3229*0a6a1f1dSLionel Sambuc           // If we override a #undef, we override anything that #undef overrides.
3230*0a6a1f1dSLionel Sambuc           // We don't need to override it, since an active #undef doesn't affect
3231*0a6a1f1dSLionel Sambuc           // the meaning of a macro.
3232*0a6a1f1dSLionel Sambuc           auto Overrides = UndefMD->getOverriddenModules();
3233*0a6a1f1dSLionel Sambuc           ScratchSpace.insert(ScratchSpace.end(),
3234*0a6a1f1dSLionel Sambuc                               Overrides.begin(), Overrides.end());
3235*0a6a1f1dSLionel Sambuc         }
3236f4a2713aSLionel Sambuc       }
3237f4a2713aSLionel Sambuc 
3238*0a6a1f1dSLionel Sambuc       // Stop once we leave the original macro's submodule.
3239*0a6a1f1dSLionel Sambuc       //
3240*0a6a1f1dSLionel Sambuc       // Either this submodule #included another submodule of the same
3241*0a6a1f1dSLionel Sambuc       // module or it just happened to be built after the other module.
3242*0a6a1f1dSLionel Sambuc       // In the former case, we override the submodule's macro.
3243*0a6a1f1dSLionel Sambuc       //
3244*0a6a1f1dSLionel Sambuc       // FIXME: In the latter case, we shouldn't do so, but we can't tell
3245*0a6a1f1dSLionel Sambuc       // these cases apart.
3246*0a6a1f1dSLionel Sambuc       //
3247*0a6a1f1dSLionel Sambuc       // FIXME: We can leave this submodule and re-enter it if it #includes a
3248*0a6a1f1dSLionel Sambuc       // header within a different submodule of the same module. In such cases
3249*0a6a1f1dSLionel Sambuc       // the overrides list will be incomplete.
3250*0a6a1f1dSLionel Sambuc       SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3251*0a6a1f1dSLionel Sambuc       if (DirectiveModuleID != ModID) {
3252*0a6a1f1dSLionel Sambuc         if (DirectiveModuleID && !MD->isImported())
3253*0a6a1f1dSLionel Sambuc           ScratchSpace.push_back(DirectiveModuleID);
3254*0a6a1f1dSLionel Sambuc         break;
3255*0a6a1f1dSLionel Sambuc       }
3256*0a6a1f1dSLionel Sambuc     }
3257*0a6a1f1dSLionel Sambuc 
3258*0a6a1f1dSLionel Sambuc     std::sort(ScratchSpace.begin(), ScratchSpace.end());
3259*0a6a1f1dSLionel Sambuc     ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3260*0a6a1f1dSLionel Sambuc                        ScratchSpace.end());
3261*0a6a1f1dSLionel Sambuc     return ScratchSpace;
3262f4a2713aSLionel Sambuc   }
3263f4a2713aSLionel Sambuc 
getSubmoduleID(MacroDirective * MD)3264f4a2713aSLionel Sambuc   SubmoduleID getSubmoduleID(MacroDirective *MD) {
3265f4a2713aSLionel Sambuc     return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
3266f4a2713aSLionel Sambuc   }
3267f4a2713aSLionel Sambuc 
3268f4a2713aSLionel Sambuc public:
3269f4a2713aSLionel Sambuc   typedef IdentifierInfo* key_type;
3270f4a2713aSLionel Sambuc   typedef key_type  key_type_ref;
3271f4a2713aSLionel Sambuc 
3272f4a2713aSLionel Sambuc   typedef IdentID data_type;
3273f4a2713aSLionel Sambuc   typedef data_type data_type_ref;
3274f4a2713aSLionel Sambuc 
3275*0a6a1f1dSLionel Sambuc   typedef unsigned hash_value_type;
3276*0a6a1f1dSLionel Sambuc   typedef unsigned offset_type;
3277*0a6a1f1dSLionel Sambuc 
ASTIdentifierTableTrait(ASTWriter & Writer,Preprocessor & PP,IdentifierResolver & IdResolver,bool IsModule)3278f4a2713aSLionel Sambuc   ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3279f4a2713aSLionel Sambuc                           IdentifierResolver &IdResolver, bool IsModule)
3280f4a2713aSLionel Sambuc     : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
3281f4a2713aSLionel Sambuc 
ComputeHash(const IdentifierInfo * II)3282*0a6a1f1dSLionel Sambuc   static hash_value_type ComputeHash(const IdentifierInfo* II) {
3283f4a2713aSLionel Sambuc     return llvm::HashString(II->getName());
3284f4a2713aSLionel Sambuc   }
3285f4a2713aSLionel Sambuc 
3286f4a2713aSLionel Sambuc   std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,IdentifierInfo * II,IdentID ID)3287f4a2713aSLionel Sambuc   EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3288f4a2713aSLionel Sambuc     unsigned KeyLen = II->getLength() + 1;
3289f4a2713aSLionel Sambuc     unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3290*0a6a1f1dSLionel Sambuc     MacroDirective *Macro = nullptr;
3291f4a2713aSLionel Sambuc     if (isInterestingIdentifier(II, Macro)) {
3292f4a2713aSLionel Sambuc       DataLen += 2; // 2 bytes for builtin ID
3293f4a2713aSLionel Sambuc       DataLen += 2; // 2 bytes for flags
3294f4a2713aSLionel Sambuc       if (hadMacroDefinition(II, Macro)) {
3295f4a2713aSLionel Sambuc         DataLen += 4; // MacroDirectives offset.
3296f4a2713aSLionel Sambuc         if (IsModule) {
3297*0a6a1f1dSLionel Sambuc           MacroState State;
3298*0a6a1f1dSLionel Sambuc           SmallVector<SubmoduleID, 16> Scratch;
3299*0a6a1f1dSLionel Sambuc           for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3300*0a6a1f1dSLionel Sambuc                MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
3301*0a6a1f1dSLionel Sambuc             DataLen += 4; // MacroInfo ID or ModuleID.
3302*0a6a1f1dSLionel Sambuc             if (unsigned NumOverrides =
3303*0a6a1f1dSLionel Sambuc                     getOverriddenSubmodules(MD, Scratch).size())
3304*0a6a1f1dSLionel Sambuc               DataLen += 4 * (1 + NumOverrides);
3305f4a2713aSLionel Sambuc           }
3306*0a6a1f1dSLionel Sambuc           DataLen += 4; // 0 terminator.
3307f4a2713aSLionel Sambuc         }
3308f4a2713aSLionel Sambuc       }
3309f4a2713aSLionel Sambuc 
3310f4a2713aSLionel Sambuc       for (IdentifierResolver::iterator D = IdResolver.begin(II),
3311f4a2713aSLionel Sambuc                                      DEnd = IdResolver.end();
3312f4a2713aSLionel Sambuc            D != DEnd; ++D)
3313*0a6a1f1dSLionel Sambuc         DataLen += 4;
3314f4a2713aSLionel Sambuc     }
3315*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
3316*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
3317*0a6a1f1dSLionel Sambuc 
3318*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(DataLen);
3319f4a2713aSLionel Sambuc     // We emit the key length after the data length so that every
3320f4a2713aSLionel Sambuc     // string is preceded by a 16-bit length. This matches the PTH
3321f4a2713aSLionel Sambuc     // format for storing identifiers.
3322*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(KeyLen);
3323f4a2713aSLionel Sambuc     return std::make_pair(KeyLen, DataLen);
3324f4a2713aSLionel Sambuc   }
3325f4a2713aSLionel Sambuc 
EmitKey(raw_ostream & Out,const IdentifierInfo * II,unsigned KeyLen)3326f4a2713aSLionel Sambuc   void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3327f4a2713aSLionel Sambuc                unsigned KeyLen) {
3328f4a2713aSLionel Sambuc     // Record the location of the key data.  This is used when generating
3329f4a2713aSLionel Sambuc     // the mapping from persistent IDs to strings.
3330f4a2713aSLionel Sambuc     Writer.SetIdentifierOffset(II, Out.tell());
3331f4a2713aSLionel Sambuc     Out.write(II->getNameStart(), KeyLen);
3332f4a2713aSLionel Sambuc   }
3333f4a2713aSLionel Sambuc 
emitMacroOverrides(raw_ostream & Out,ArrayRef<SubmoduleID> Overridden)3334*0a6a1f1dSLionel Sambuc   static void emitMacroOverrides(raw_ostream &Out,
3335*0a6a1f1dSLionel Sambuc                                  ArrayRef<SubmoduleID> Overridden) {
3336*0a6a1f1dSLionel Sambuc     if (!Overridden.empty()) {
3337*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
3338*0a6a1f1dSLionel Sambuc       endian::Writer<little> LE(Out);
3339*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Overridden.size() | 0x80000000U);
3340*0a6a1f1dSLionel Sambuc       for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3341*0a6a1f1dSLionel Sambuc         assert(Overridden[I] && "zero module ID for override");
3342*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(Overridden[I]);
3343*0a6a1f1dSLionel Sambuc       }
3344*0a6a1f1dSLionel Sambuc     }
3345*0a6a1f1dSLionel Sambuc   }
3346*0a6a1f1dSLionel Sambuc 
EmitData(raw_ostream & Out,IdentifierInfo * II,IdentID ID,unsigned)3347f4a2713aSLionel Sambuc   void EmitData(raw_ostream& Out, IdentifierInfo* II,
3348f4a2713aSLionel Sambuc                 IdentID ID, unsigned) {
3349*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
3350*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
3351*0a6a1f1dSLionel Sambuc     MacroDirective *Macro = nullptr;
3352f4a2713aSLionel Sambuc     if (!isInterestingIdentifier(II, Macro)) {
3353*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(ID << 1);
3354f4a2713aSLionel Sambuc       return;
3355f4a2713aSLionel Sambuc     }
3356f4a2713aSLionel Sambuc 
3357*0a6a1f1dSLionel Sambuc     LE.write<uint32_t>((ID << 1) | 0x01);
3358f4a2713aSLionel Sambuc     uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3359f4a2713aSLionel Sambuc     assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3360*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(Bits);
3361f4a2713aSLionel Sambuc     Bits = 0;
3362f4a2713aSLionel Sambuc     bool HadMacroDefinition = hadMacroDefinition(II, Macro);
3363f4a2713aSLionel Sambuc     Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3364f4a2713aSLionel Sambuc     Bits = (Bits << 1) | unsigned(IsModule);
3365f4a2713aSLionel Sambuc     Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3366f4a2713aSLionel Sambuc     Bits = (Bits << 1) | unsigned(II->isPoisoned());
3367f4a2713aSLionel Sambuc     Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3368f4a2713aSLionel Sambuc     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3369*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(Bits);
3370f4a2713aSLionel Sambuc 
3371f4a2713aSLionel Sambuc     if (HadMacroDefinition) {
3372*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
3373f4a2713aSLionel Sambuc       if (IsModule) {
3374f4a2713aSLionel Sambuc         // Write the IDs of macros coming from different submodules.
3375*0a6a1f1dSLionel Sambuc         MacroState State;
3376*0a6a1f1dSLionel Sambuc         SmallVector<SubmoduleID, 16> Scratch;
3377*0a6a1f1dSLionel Sambuc         for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3378*0a6a1f1dSLionel Sambuc              MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
3379*0a6a1f1dSLionel Sambuc           if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3380*0a6a1f1dSLionel Sambuc             // FIXME: If this macro directive was created by #pragma pop_macros,
3381*0a6a1f1dSLionel Sambuc             // or if it was created implicitly by resolving conflicting macros,
3382*0a6a1f1dSLionel Sambuc             // it may be for a different submodule from the one in the MacroInfo
3383*0a6a1f1dSLionel Sambuc             // object. If so, we should write out its owning ModuleID.
3384f4a2713aSLionel Sambuc             MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
3385f4a2713aSLionel Sambuc             assert(InfoID);
3386*0a6a1f1dSLionel Sambuc             LE.write<uint32_t>(InfoID << 1);
3387*0a6a1f1dSLionel Sambuc           } else {
3388*0a6a1f1dSLionel Sambuc             auto *UndefMD = cast<UndefMacroDirective>(MD);
3389*0a6a1f1dSLionel Sambuc             SubmoduleID Mod = UndefMD->isImported()
3390*0a6a1f1dSLionel Sambuc                                   ? UndefMD->getOwningModuleID()
3391*0a6a1f1dSLionel Sambuc                                   : getSubmoduleID(UndefMD);
3392*0a6a1f1dSLionel Sambuc             LE.write<uint32_t>((Mod << 1) | 1);
3393f4a2713aSLionel Sambuc           }
3394*0a6a1f1dSLionel Sambuc           emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
3395*0a6a1f1dSLionel Sambuc         }
3396*0a6a1f1dSLionel Sambuc         LE.write<uint32_t>(0xdeadbeef);
3397f4a2713aSLionel Sambuc       }
3398f4a2713aSLionel Sambuc     }
3399f4a2713aSLionel Sambuc 
3400f4a2713aSLionel Sambuc     // Emit the declaration IDs in reverse order, because the
3401f4a2713aSLionel Sambuc     // IdentifierResolver provides the declarations as they would be
3402f4a2713aSLionel Sambuc     // visible (e.g., the function "stat" would come before the struct
3403f4a2713aSLionel Sambuc     // "stat"), but the ASTReader adds declarations to the end of the list
3404f4a2713aSLionel Sambuc     // (so we need to see the struct "status" before the function "status").
3405f4a2713aSLionel Sambuc     // Only emit declarations that aren't from a chained PCH, though.
3406f4a2713aSLionel Sambuc     SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3407f4a2713aSLionel Sambuc                                   IdResolver.end());
3408f4a2713aSLionel Sambuc     for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
3409f4a2713aSLionel Sambuc                                                 DEnd = Decls.rend();
3410f4a2713aSLionel Sambuc          D != DEnd; ++D)
3411*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
3412f4a2713aSLionel Sambuc   }
3413f4a2713aSLionel Sambuc 
3414f4a2713aSLionel Sambuc   /// \brief Returns the most recent local decl or the given decl if there are
3415f4a2713aSLionel Sambuc   /// no local ones. The given decl is assumed to be the most recent one.
getMostRecentLocalDecl(Decl * Orig)3416f4a2713aSLionel Sambuc   Decl *getMostRecentLocalDecl(Decl *Orig) {
3417f4a2713aSLionel Sambuc     // The only way a "from AST file" decl would be more recent from a local one
3418f4a2713aSLionel Sambuc     // is if it came from a module.
3419f4a2713aSLionel Sambuc     if (!PP.getLangOpts().Modules)
3420f4a2713aSLionel Sambuc       return Orig;
3421f4a2713aSLionel Sambuc 
3422f4a2713aSLionel Sambuc     // Look for a local in the decl chain.
3423f4a2713aSLionel Sambuc     for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3424f4a2713aSLionel Sambuc       if (!D->isFromASTFile())
3425f4a2713aSLionel Sambuc         return D;
3426f4a2713aSLionel Sambuc       // If we come up a decl from a (chained-)PCH stop since we won't find a
3427f4a2713aSLionel Sambuc       // local one.
3428f4a2713aSLionel Sambuc       if (D->getOwningModuleID() == 0)
3429f4a2713aSLionel Sambuc         break;
3430f4a2713aSLionel Sambuc     }
3431f4a2713aSLionel Sambuc 
3432f4a2713aSLionel Sambuc     return Orig;
3433f4a2713aSLionel Sambuc   }
3434f4a2713aSLionel Sambuc };
3435f4a2713aSLionel Sambuc } // end anonymous namespace
3436f4a2713aSLionel Sambuc 
3437f4a2713aSLionel Sambuc /// \brief Write the identifier table into the AST file.
3438f4a2713aSLionel Sambuc ///
3439f4a2713aSLionel Sambuc /// The identifier table consists of a blob containing string data
3440f4a2713aSLionel Sambuc /// (the actual identifiers themselves) and a separate "offsets" index
3441f4a2713aSLionel Sambuc /// that maps identifier IDs to locations within the blob.
WriteIdentifierTable(Preprocessor & PP,IdentifierResolver & IdResolver,bool IsModule)3442f4a2713aSLionel Sambuc void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3443f4a2713aSLionel Sambuc                                      IdentifierResolver &IdResolver,
3444f4a2713aSLionel Sambuc                                      bool IsModule) {
3445f4a2713aSLionel Sambuc   using namespace llvm;
3446f4a2713aSLionel Sambuc 
3447f4a2713aSLionel Sambuc   // Create and write out the blob that contains the identifier
3448f4a2713aSLionel Sambuc   // strings.
3449f4a2713aSLionel Sambuc   {
3450*0a6a1f1dSLionel Sambuc     llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3451f4a2713aSLionel Sambuc     ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
3452f4a2713aSLionel Sambuc 
3453f4a2713aSLionel Sambuc     // Look for any identifiers that were named while processing the
3454f4a2713aSLionel Sambuc     // headers, but are otherwise not needed. We add these to the hash
3455f4a2713aSLionel Sambuc     // table to enable checking of the predefines buffer in the case
3456f4a2713aSLionel Sambuc     // where the user adds new macro definitions when building the AST
3457f4a2713aSLionel Sambuc     // file.
3458f4a2713aSLionel Sambuc     for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3459f4a2713aSLionel Sambuc                                 IDEnd = PP.getIdentifierTable().end();
3460f4a2713aSLionel Sambuc          ID != IDEnd; ++ID)
3461f4a2713aSLionel Sambuc       getIdentifierRef(ID->second);
3462f4a2713aSLionel Sambuc 
3463f4a2713aSLionel Sambuc     // Create the on-disk hash table representation. We only store offsets
3464f4a2713aSLionel Sambuc     // for identifiers that appear here for the first time.
3465f4a2713aSLionel Sambuc     IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3466f4a2713aSLionel Sambuc     for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
3467f4a2713aSLionel Sambuc            ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3468f4a2713aSLionel Sambuc          ID != IDEnd; ++ID) {
3469f4a2713aSLionel Sambuc       assert(ID->first && "NULL identifier in identifier table");
3470f4a2713aSLionel Sambuc       if (!Chain || !ID->first->isFromAST() ||
3471f4a2713aSLionel Sambuc           ID->first->hasChangedSinceDeserialization())
3472f4a2713aSLionel Sambuc         Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
3473f4a2713aSLionel Sambuc                          Trait);
3474f4a2713aSLionel Sambuc     }
3475f4a2713aSLionel Sambuc 
3476f4a2713aSLionel Sambuc     // Create the on-disk hash table in a buffer.
3477f4a2713aSLionel Sambuc     SmallString<4096> IdentifierTable;
3478f4a2713aSLionel Sambuc     uint32_t BucketOffset;
3479f4a2713aSLionel Sambuc     {
3480*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
3481f4a2713aSLionel Sambuc       ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
3482f4a2713aSLionel Sambuc       llvm::raw_svector_ostream Out(IdentifierTable);
3483f4a2713aSLionel Sambuc       // Make sure that no bucket is at offset 0
3484*0a6a1f1dSLionel Sambuc       endian::Writer<little>(Out).write<uint32_t>(0);
3485f4a2713aSLionel Sambuc       BucketOffset = Generator.Emit(Out, Trait);
3486f4a2713aSLionel Sambuc     }
3487f4a2713aSLionel Sambuc 
3488f4a2713aSLionel Sambuc     // Create a blob abbreviation
3489f4a2713aSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3490f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3491f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3492f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3493f4a2713aSLionel Sambuc     unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
3494f4a2713aSLionel Sambuc 
3495f4a2713aSLionel Sambuc     // Write the identifier table
3496f4a2713aSLionel Sambuc     RecordData Record;
3497f4a2713aSLionel Sambuc     Record.push_back(IDENTIFIER_TABLE);
3498f4a2713aSLionel Sambuc     Record.push_back(BucketOffset);
3499f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
3500f4a2713aSLionel Sambuc   }
3501f4a2713aSLionel Sambuc 
3502f4a2713aSLionel Sambuc   // Write the offsets table for identifier IDs.
3503f4a2713aSLionel Sambuc   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3504f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3505f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3506f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3507f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3508f4a2713aSLionel Sambuc   unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3509f4a2713aSLionel Sambuc 
3510f4a2713aSLionel Sambuc #ifndef NDEBUG
3511f4a2713aSLionel Sambuc   for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3512f4a2713aSLionel Sambuc     assert(IdentifierOffsets[I] && "Missing identifier offset?");
3513f4a2713aSLionel Sambuc #endif
3514f4a2713aSLionel Sambuc 
3515f4a2713aSLionel Sambuc   RecordData Record;
3516f4a2713aSLionel Sambuc   Record.push_back(IDENTIFIER_OFFSET);
3517f4a2713aSLionel Sambuc   Record.push_back(IdentifierOffsets.size());
3518f4a2713aSLionel Sambuc   Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
3519f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3520f4a2713aSLionel Sambuc                             data(IdentifierOffsets));
3521f4a2713aSLionel Sambuc }
3522f4a2713aSLionel Sambuc 
3523f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3524f4a2713aSLionel Sambuc // DeclContext's Name Lookup Table Serialization
3525f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3526f4a2713aSLionel Sambuc 
3527*0a6a1f1dSLionel Sambuc /// Determine the declaration that should be put into the name lookup table to
3528*0a6a1f1dSLionel Sambuc /// represent the given declaration in this module. This is usually D itself,
3529*0a6a1f1dSLionel Sambuc /// but if D was imported and merged into a local declaration, we want the most
3530*0a6a1f1dSLionel Sambuc /// recent local declaration instead. The chosen declaration will be the most
3531*0a6a1f1dSLionel Sambuc /// recent declaration in any module that imports this one.
getDeclForLocalLookup(NamedDecl * D)3532*0a6a1f1dSLionel Sambuc static NamedDecl *getDeclForLocalLookup(NamedDecl *D) {
3533*0a6a1f1dSLionel Sambuc   if (!D->isFromASTFile())
3534*0a6a1f1dSLionel Sambuc     return D;
3535*0a6a1f1dSLionel Sambuc 
3536*0a6a1f1dSLionel Sambuc   if (Decl *Redecl = D->getPreviousDecl()) {
3537*0a6a1f1dSLionel Sambuc     // For Redeclarable decls, a prior declaration might be local.
3538*0a6a1f1dSLionel Sambuc     for (; Redecl; Redecl = Redecl->getPreviousDecl())
3539*0a6a1f1dSLionel Sambuc       if (!Redecl->isFromASTFile())
3540*0a6a1f1dSLionel Sambuc         return cast<NamedDecl>(Redecl);
3541*0a6a1f1dSLionel Sambuc   } else if (Decl *First = D->getCanonicalDecl()) {
3542*0a6a1f1dSLionel Sambuc     // For Mergeable decls, the first decl might be local.
3543*0a6a1f1dSLionel Sambuc     if (!First->isFromASTFile())
3544*0a6a1f1dSLionel Sambuc       return cast<NamedDecl>(First);
3545*0a6a1f1dSLionel Sambuc   }
3546*0a6a1f1dSLionel Sambuc 
3547*0a6a1f1dSLionel Sambuc   // All declarations are imported. Our most recent declaration will also be
3548*0a6a1f1dSLionel Sambuc   // the most recent one in anyone who imports us.
3549*0a6a1f1dSLionel Sambuc   return D;
3550*0a6a1f1dSLionel Sambuc }
3551*0a6a1f1dSLionel Sambuc 
3552f4a2713aSLionel Sambuc namespace {
3553f4a2713aSLionel Sambuc // Trait used for the on-disk hash table used in the method pool.
3554f4a2713aSLionel Sambuc class ASTDeclContextNameLookupTrait {
3555f4a2713aSLionel Sambuc   ASTWriter &Writer;
3556f4a2713aSLionel Sambuc 
3557f4a2713aSLionel Sambuc public:
3558f4a2713aSLionel Sambuc   typedef DeclarationName key_type;
3559f4a2713aSLionel Sambuc   typedef key_type key_type_ref;
3560f4a2713aSLionel Sambuc 
3561f4a2713aSLionel Sambuc   typedef DeclContext::lookup_result data_type;
3562f4a2713aSLionel Sambuc   typedef const data_type& data_type_ref;
3563f4a2713aSLionel Sambuc 
3564*0a6a1f1dSLionel Sambuc   typedef unsigned hash_value_type;
3565*0a6a1f1dSLionel Sambuc   typedef unsigned offset_type;
3566*0a6a1f1dSLionel Sambuc 
ASTDeclContextNameLookupTrait(ASTWriter & Writer)3567f4a2713aSLionel Sambuc   explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3568f4a2713aSLionel Sambuc 
ComputeHash(DeclarationName Name)3569*0a6a1f1dSLionel Sambuc   hash_value_type ComputeHash(DeclarationName Name) {
3570f4a2713aSLionel Sambuc     llvm::FoldingSetNodeID ID;
3571f4a2713aSLionel Sambuc     ID.AddInteger(Name.getNameKind());
3572f4a2713aSLionel Sambuc 
3573f4a2713aSLionel Sambuc     switch (Name.getNameKind()) {
3574f4a2713aSLionel Sambuc     case DeclarationName::Identifier:
3575f4a2713aSLionel Sambuc       ID.AddString(Name.getAsIdentifierInfo()->getName());
3576f4a2713aSLionel Sambuc       break;
3577f4a2713aSLionel Sambuc     case DeclarationName::ObjCZeroArgSelector:
3578f4a2713aSLionel Sambuc     case DeclarationName::ObjCOneArgSelector:
3579f4a2713aSLionel Sambuc     case DeclarationName::ObjCMultiArgSelector:
3580f4a2713aSLionel Sambuc       ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3581f4a2713aSLionel Sambuc       break;
3582f4a2713aSLionel Sambuc     case DeclarationName::CXXConstructorName:
3583f4a2713aSLionel Sambuc     case DeclarationName::CXXDestructorName:
3584f4a2713aSLionel Sambuc     case DeclarationName::CXXConversionFunctionName:
3585f4a2713aSLionel Sambuc       break;
3586f4a2713aSLionel Sambuc     case DeclarationName::CXXOperatorName:
3587f4a2713aSLionel Sambuc       ID.AddInteger(Name.getCXXOverloadedOperator());
3588f4a2713aSLionel Sambuc       break;
3589f4a2713aSLionel Sambuc     case DeclarationName::CXXLiteralOperatorName:
3590f4a2713aSLionel Sambuc       ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3591f4a2713aSLionel Sambuc     case DeclarationName::CXXUsingDirective:
3592f4a2713aSLionel Sambuc       break;
3593f4a2713aSLionel Sambuc     }
3594f4a2713aSLionel Sambuc 
3595f4a2713aSLionel Sambuc     return ID.ComputeHash();
3596f4a2713aSLionel Sambuc   }
3597f4a2713aSLionel Sambuc 
3598f4a2713aSLionel Sambuc   std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,DeclarationName Name,data_type_ref Lookup)3599f4a2713aSLionel Sambuc     EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
3600f4a2713aSLionel Sambuc                       data_type_ref Lookup) {
3601*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
3602*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
3603f4a2713aSLionel Sambuc     unsigned KeyLen = 1;
3604f4a2713aSLionel Sambuc     switch (Name.getNameKind()) {
3605f4a2713aSLionel Sambuc     case DeclarationName::Identifier:
3606f4a2713aSLionel Sambuc     case DeclarationName::ObjCZeroArgSelector:
3607f4a2713aSLionel Sambuc     case DeclarationName::ObjCOneArgSelector:
3608f4a2713aSLionel Sambuc     case DeclarationName::ObjCMultiArgSelector:
3609f4a2713aSLionel Sambuc     case DeclarationName::CXXLiteralOperatorName:
3610f4a2713aSLionel Sambuc       KeyLen += 4;
3611f4a2713aSLionel Sambuc       break;
3612f4a2713aSLionel Sambuc     case DeclarationName::CXXOperatorName:
3613f4a2713aSLionel Sambuc       KeyLen += 1;
3614f4a2713aSLionel Sambuc       break;
3615f4a2713aSLionel Sambuc     case DeclarationName::CXXConstructorName:
3616f4a2713aSLionel Sambuc     case DeclarationName::CXXDestructorName:
3617f4a2713aSLionel Sambuc     case DeclarationName::CXXConversionFunctionName:
3618f4a2713aSLionel Sambuc     case DeclarationName::CXXUsingDirective:
3619f4a2713aSLionel Sambuc       break;
3620f4a2713aSLionel Sambuc     }
3621*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(KeyLen);
3622f4a2713aSLionel Sambuc 
3623f4a2713aSLionel Sambuc     // 2 bytes for num of decls and 4 for each DeclID.
3624f4a2713aSLionel Sambuc     unsigned DataLen = 2 + 4 * Lookup.size();
3625*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(DataLen);
3626f4a2713aSLionel Sambuc 
3627f4a2713aSLionel Sambuc     return std::make_pair(KeyLen, DataLen);
3628f4a2713aSLionel Sambuc   }
3629f4a2713aSLionel Sambuc 
EmitKey(raw_ostream & Out,DeclarationName Name,unsigned)3630f4a2713aSLionel Sambuc   void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
3631*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
3632*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
3633*0a6a1f1dSLionel Sambuc     LE.write<uint8_t>(Name.getNameKind());
3634f4a2713aSLionel Sambuc     switch (Name.getNameKind()) {
3635f4a2713aSLionel Sambuc     case DeclarationName::Identifier:
3636*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
3637f4a2713aSLionel Sambuc       return;
3638f4a2713aSLionel Sambuc     case DeclarationName::ObjCZeroArgSelector:
3639f4a2713aSLionel Sambuc     case DeclarationName::ObjCOneArgSelector:
3640f4a2713aSLionel Sambuc     case DeclarationName::ObjCMultiArgSelector:
3641*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
3642f4a2713aSLionel Sambuc       return;
3643f4a2713aSLionel Sambuc     case DeclarationName::CXXOperatorName:
3644f4a2713aSLionel Sambuc       assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3645f4a2713aSLionel Sambuc              "Invalid operator?");
3646*0a6a1f1dSLionel Sambuc       LE.write<uint8_t>(Name.getCXXOverloadedOperator());
3647f4a2713aSLionel Sambuc       return;
3648f4a2713aSLionel Sambuc     case DeclarationName::CXXLiteralOperatorName:
3649*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
3650f4a2713aSLionel Sambuc       return;
3651f4a2713aSLionel Sambuc     case DeclarationName::CXXConstructorName:
3652f4a2713aSLionel Sambuc     case DeclarationName::CXXDestructorName:
3653f4a2713aSLionel Sambuc     case DeclarationName::CXXConversionFunctionName:
3654f4a2713aSLionel Sambuc     case DeclarationName::CXXUsingDirective:
3655f4a2713aSLionel Sambuc       return;
3656f4a2713aSLionel Sambuc     }
3657f4a2713aSLionel Sambuc 
3658f4a2713aSLionel Sambuc     llvm_unreachable("Invalid name kind?");
3659f4a2713aSLionel Sambuc   }
3660f4a2713aSLionel Sambuc 
EmitData(raw_ostream & Out,key_type_ref,data_type Lookup,unsigned DataLen)3661f4a2713aSLionel Sambuc   void EmitData(raw_ostream& Out, key_type_ref,
3662f4a2713aSLionel Sambuc                 data_type Lookup, unsigned DataLen) {
3663*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
3664*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
3665f4a2713aSLionel Sambuc     uint64_t Start = Out.tell(); (void)Start;
3666*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(Lookup.size());
3667f4a2713aSLionel Sambuc     for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3668f4a2713aSLionel Sambuc          I != E; ++I)
3669*0a6a1f1dSLionel Sambuc       LE.write<uint32_t>(Writer.GetDeclRef(getDeclForLocalLookup(*I)));
3670f4a2713aSLionel Sambuc 
3671f4a2713aSLionel Sambuc     assert(Out.tell() - Start == DataLen && "Data length is wrong");
3672f4a2713aSLionel Sambuc   }
3673f4a2713aSLionel Sambuc };
3674f4a2713aSLionel Sambuc } // end anonymous namespace
3675f4a2713aSLionel Sambuc 
3676*0a6a1f1dSLionel Sambuc template<typename Visitor>
visitLocalLookupResults(const DeclContext * ConstDC,bool NeedToReconcileExternalVisibleStorage,Visitor AddLookupResult)3677*0a6a1f1dSLionel Sambuc static void visitLocalLookupResults(const DeclContext *ConstDC,
3678*0a6a1f1dSLionel Sambuc                                     bool NeedToReconcileExternalVisibleStorage,
3679*0a6a1f1dSLionel Sambuc                                     Visitor AddLookupResult) {
3680*0a6a1f1dSLionel Sambuc   // FIXME: We need to build the lookups table, which is logically const.
3681*0a6a1f1dSLionel Sambuc   DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3682*0a6a1f1dSLionel Sambuc   assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3683*0a6a1f1dSLionel Sambuc 
3684*0a6a1f1dSLionel Sambuc   SmallVector<DeclarationName, 16> ExternalNames;
3685*0a6a1f1dSLionel Sambuc   for (auto &Lookup : *DC->buildLookup()) {
3686*0a6a1f1dSLionel Sambuc     if (Lookup.second.hasExternalDecls() ||
3687*0a6a1f1dSLionel Sambuc         NeedToReconcileExternalVisibleStorage) {
3688*0a6a1f1dSLionel Sambuc       // We don't know for sure what declarations are found by this name,
3689*0a6a1f1dSLionel Sambuc       // because the external source might have a different set from the set
3690*0a6a1f1dSLionel Sambuc       // that are in the lookup map, and we can't update it now without
3691*0a6a1f1dSLionel Sambuc       // risking invalidating our lookup iterator. So add it to a queue to
3692*0a6a1f1dSLionel Sambuc       // deal with later.
3693*0a6a1f1dSLionel Sambuc       ExternalNames.push_back(Lookup.first);
3694*0a6a1f1dSLionel Sambuc       continue;
3695*0a6a1f1dSLionel Sambuc     }
3696*0a6a1f1dSLionel Sambuc 
3697*0a6a1f1dSLionel Sambuc     AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3698*0a6a1f1dSLionel Sambuc   }
3699*0a6a1f1dSLionel Sambuc 
3700*0a6a1f1dSLionel Sambuc   // Add the names we needed to defer. Note, this shouldn't add any new decls
3701*0a6a1f1dSLionel Sambuc   // to the list we need to serialize: any new declarations we find here should
3702*0a6a1f1dSLionel Sambuc   // be imported from an external source.
3703*0a6a1f1dSLionel Sambuc   // FIXME: What if the external source isn't an ASTReader?
3704*0a6a1f1dSLionel Sambuc   for (const auto &Name : ExternalNames)
3705*0a6a1f1dSLionel Sambuc     AddLookupResult(Name, DC->lookup(Name));
3706*0a6a1f1dSLionel Sambuc }
3707*0a6a1f1dSLionel Sambuc 
AddUpdatedDeclContext(const DeclContext * DC)3708*0a6a1f1dSLionel Sambuc void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3709*0a6a1f1dSLionel Sambuc   if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
3710*0a6a1f1dSLionel Sambuc     // Ensure we emit all the visible declarations.
3711*0a6a1f1dSLionel Sambuc     visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3712*0a6a1f1dSLionel Sambuc                             [&](DeclarationName Name,
3713*0a6a1f1dSLionel Sambuc                                 DeclContext::lookup_const_result Result) {
3714*0a6a1f1dSLionel Sambuc       for (auto *Decl : Result)
3715*0a6a1f1dSLionel Sambuc         GetDeclRef(getDeclForLocalLookup(Decl));
3716*0a6a1f1dSLionel Sambuc     });
3717*0a6a1f1dSLionel Sambuc   }
3718*0a6a1f1dSLionel Sambuc }
3719*0a6a1f1dSLionel Sambuc 
3720*0a6a1f1dSLionel Sambuc uint32_t
GenerateNameLookupTable(const DeclContext * DC,llvm::SmallVectorImpl<char> & LookupTable)3721*0a6a1f1dSLionel Sambuc ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3722*0a6a1f1dSLionel Sambuc                                    llvm::SmallVectorImpl<char> &LookupTable) {
3723*0a6a1f1dSLionel Sambuc   assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3724*0a6a1f1dSLionel Sambuc 
3725*0a6a1f1dSLionel Sambuc   llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3726*0a6a1f1dSLionel Sambuc       Generator;
3727*0a6a1f1dSLionel Sambuc   ASTDeclContextNameLookupTrait Trait(*this);
3728*0a6a1f1dSLionel Sambuc 
3729*0a6a1f1dSLionel Sambuc   // Create the on-disk hash table representation.
3730*0a6a1f1dSLionel Sambuc   DeclarationName ConstructorName;
3731*0a6a1f1dSLionel Sambuc   DeclarationName ConversionName;
3732*0a6a1f1dSLionel Sambuc   SmallVector<NamedDecl *, 8> ConstructorDecls;
3733*0a6a1f1dSLionel Sambuc   SmallVector<NamedDecl *, 4> ConversionDecls;
3734*0a6a1f1dSLionel Sambuc 
3735*0a6a1f1dSLionel Sambuc   visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3736*0a6a1f1dSLionel Sambuc                           [&](DeclarationName Name,
3737*0a6a1f1dSLionel Sambuc                               DeclContext::lookup_result Result) {
3738*0a6a1f1dSLionel Sambuc     if (Result.empty())
3739*0a6a1f1dSLionel Sambuc       return;
3740*0a6a1f1dSLionel Sambuc 
3741*0a6a1f1dSLionel Sambuc     // Different DeclarationName values of certain kinds are mapped to
3742*0a6a1f1dSLionel Sambuc     // identical serialized keys, because we don't want to use type
3743*0a6a1f1dSLionel Sambuc     // identifiers in the keys (since type ids are local to the module).
3744*0a6a1f1dSLionel Sambuc     switch (Name.getNameKind()) {
3745*0a6a1f1dSLionel Sambuc     case DeclarationName::CXXConstructorName:
3746*0a6a1f1dSLionel Sambuc       // There may be different CXXConstructorName DeclarationName values
3747*0a6a1f1dSLionel Sambuc       // in a DeclContext because a UsingDecl that inherits constructors
3748*0a6a1f1dSLionel Sambuc       // has the DeclarationName of the inherited constructors.
3749*0a6a1f1dSLionel Sambuc       if (!ConstructorName)
3750*0a6a1f1dSLionel Sambuc         ConstructorName = Name;
3751*0a6a1f1dSLionel Sambuc       ConstructorDecls.append(Result.begin(), Result.end());
3752*0a6a1f1dSLionel Sambuc       return;
3753*0a6a1f1dSLionel Sambuc 
3754*0a6a1f1dSLionel Sambuc     case DeclarationName::CXXConversionFunctionName:
3755*0a6a1f1dSLionel Sambuc       if (!ConversionName)
3756*0a6a1f1dSLionel Sambuc         ConversionName = Name;
3757*0a6a1f1dSLionel Sambuc       ConversionDecls.append(Result.begin(), Result.end());
3758*0a6a1f1dSLionel Sambuc       return;
3759*0a6a1f1dSLionel Sambuc 
3760*0a6a1f1dSLionel Sambuc     default:
3761*0a6a1f1dSLionel Sambuc       break;
3762*0a6a1f1dSLionel Sambuc     }
3763*0a6a1f1dSLionel Sambuc 
3764*0a6a1f1dSLionel Sambuc     Generator.insert(Name, Result, Trait);
3765*0a6a1f1dSLionel Sambuc   });
3766*0a6a1f1dSLionel Sambuc 
3767*0a6a1f1dSLionel Sambuc   // Add the constructors.
3768*0a6a1f1dSLionel Sambuc   if (!ConstructorDecls.empty()) {
3769*0a6a1f1dSLionel Sambuc     Generator.insert(ConstructorName,
3770*0a6a1f1dSLionel Sambuc                      DeclContext::lookup_result(ConstructorDecls.begin(),
3771*0a6a1f1dSLionel Sambuc                                                 ConstructorDecls.end()),
3772*0a6a1f1dSLionel Sambuc                      Trait);
3773*0a6a1f1dSLionel Sambuc   }
3774*0a6a1f1dSLionel Sambuc 
3775*0a6a1f1dSLionel Sambuc   // Add the conversion functions.
3776*0a6a1f1dSLionel Sambuc   if (!ConversionDecls.empty()) {
3777*0a6a1f1dSLionel Sambuc     Generator.insert(ConversionName,
3778*0a6a1f1dSLionel Sambuc                      DeclContext::lookup_result(ConversionDecls.begin(),
3779*0a6a1f1dSLionel Sambuc                                                 ConversionDecls.end()),
3780*0a6a1f1dSLionel Sambuc                      Trait);
3781*0a6a1f1dSLionel Sambuc   }
3782*0a6a1f1dSLionel Sambuc 
3783*0a6a1f1dSLionel Sambuc   // Create the on-disk hash table in a buffer.
3784*0a6a1f1dSLionel Sambuc   llvm::raw_svector_ostream Out(LookupTable);
3785*0a6a1f1dSLionel Sambuc   // Make sure that no bucket is at offset 0
3786*0a6a1f1dSLionel Sambuc   using namespace llvm::support;
3787*0a6a1f1dSLionel Sambuc   endian::Writer<little>(Out).write<uint32_t>(0);
3788*0a6a1f1dSLionel Sambuc   return Generator.Emit(Out, Trait);
3789*0a6a1f1dSLionel Sambuc }
3790*0a6a1f1dSLionel Sambuc 
3791f4a2713aSLionel Sambuc /// \brief Write the block containing all of the declaration IDs
3792f4a2713aSLionel Sambuc /// visible from the given DeclContext.
3793f4a2713aSLionel Sambuc ///
3794f4a2713aSLionel Sambuc /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
3795f4a2713aSLionel Sambuc /// bitstream, or 0 if no block was written.
WriteDeclContextVisibleBlock(ASTContext & Context,DeclContext * DC)3796f4a2713aSLionel Sambuc uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3797f4a2713aSLionel Sambuc                                                  DeclContext *DC) {
3798f4a2713aSLionel Sambuc   if (DC->getPrimaryContext() != DC)
3799f4a2713aSLionel Sambuc     return 0;
3800f4a2713aSLionel Sambuc 
3801f4a2713aSLionel Sambuc   // Since there is no name lookup into functions or methods, don't bother to
3802f4a2713aSLionel Sambuc   // build a visible-declarations table for these entities.
3803f4a2713aSLionel Sambuc   if (DC->isFunctionOrMethod())
3804f4a2713aSLionel Sambuc     return 0;
3805f4a2713aSLionel Sambuc 
3806f4a2713aSLionel Sambuc   // If not in C++, we perform name lookup for the translation unit via the
3807f4a2713aSLionel Sambuc   // IdentifierInfo chains, don't bother to build a visible-declarations table.
3808f4a2713aSLionel Sambuc   if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
3809f4a2713aSLionel Sambuc     return 0;
3810f4a2713aSLionel Sambuc 
3811f4a2713aSLionel Sambuc   // Serialize the contents of the mapping used for lookup. Note that,
3812f4a2713aSLionel Sambuc   // although we have two very different code paths, the serialized
3813f4a2713aSLionel Sambuc   // representation is the same for both cases: a declaration name,
3814f4a2713aSLionel Sambuc   // followed by a size, followed by references to the visible
3815f4a2713aSLionel Sambuc   // declarations that have that name.
3816f4a2713aSLionel Sambuc   uint64_t Offset = Stream.GetCurrentBitNo();
3817f4a2713aSLionel Sambuc   StoredDeclsMap *Map = DC->buildLookup();
3818f4a2713aSLionel Sambuc   if (!Map || Map->empty())
3819f4a2713aSLionel Sambuc     return 0;
3820f4a2713aSLionel Sambuc 
3821f4a2713aSLionel Sambuc   // Create the on-disk hash table in a buffer.
3822f4a2713aSLionel Sambuc   SmallString<4096> LookupTable;
3823*0a6a1f1dSLionel Sambuc   uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
3824f4a2713aSLionel Sambuc 
3825f4a2713aSLionel Sambuc   // Write the lookup table
3826f4a2713aSLionel Sambuc   RecordData Record;
3827f4a2713aSLionel Sambuc   Record.push_back(DECL_CONTEXT_VISIBLE);
3828f4a2713aSLionel Sambuc   Record.push_back(BucketOffset);
3829f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3830f4a2713aSLionel Sambuc                             LookupTable.str());
3831f4a2713aSLionel Sambuc   ++NumVisibleDeclContexts;
3832f4a2713aSLionel Sambuc   return Offset;
3833f4a2713aSLionel Sambuc }
3834f4a2713aSLionel Sambuc 
3835f4a2713aSLionel Sambuc /// \brief Write an UPDATE_VISIBLE block for the given context.
3836f4a2713aSLionel Sambuc ///
3837f4a2713aSLionel Sambuc /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3838f4a2713aSLionel Sambuc /// DeclContext in a dependent AST file. As such, they only exist for the TU
3839f4a2713aSLionel Sambuc /// (in C++), for namespaces, and for classes with forward-declared unscoped
3840f4a2713aSLionel Sambuc /// enumeration members (in C++11).
WriteDeclContextVisibleUpdate(const DeclContext * DC)3841f4a2713aSLionel Sambuc void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
3842*0a6a1f1dSLionel Sambuc   StoredDeclsMap *Map = DC->getLookupPtr();
3843f4a2713aSLionel Sambuc   if (!Map || Map->empty())
3844f4a2713aSLionel Sambuc     return;
3845f4a2713aSLionel Sambuc 
3846f4a2713aSLionel Sambuc   // Create the on-disk hash table in a buffer.
3847f4a2713aSLionel Sambuc   SmallString<4096> LookupTable;
3848*0a6a1f1dSLionel Sambuc   uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
3849f4a2713aSLionel Sambuc 
3850f4a2713aSLionel Sambuc   // Write the lookup table
3851f4a2713aSLionel Sambuc   RecordData Record;
3852f4a2713aSLionel Sambuc   Record.push_back(UPDATE_VISIBLE);
3853f4a2713aSLionel Sambuc   Record.push_back(getDeclID(cast<Decl>(DC)));
3854f4a2713aSLionel Sambuc   Record.push_back(BucketOffset);
3855f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3856f4a2713aSLionel Sambuc }
3857f4a2713aSLionel Sambuc 
3858f4a2713aSLionel Sambuc /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
WriteFPPragmaOptions(const FPOptions & Opts)3859f4a2713aSLionel Sambuc void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3860f4a2713aSLionel Sambuc   RecordData Record;
3861f4a2713aSLionel Sambuc   Record.push_back(Opts.fp_contract);
3862f4a2713aSLionel Sambuc   Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3863f4a2713aSLionel Sambuc }
3864f4a2713aSLionel Sambuc 
3865f4a2713aSLionel Sambuc /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
WriteOpenCLExtensions(Sema & SemaRef)3866f4a2713aSLionel Sambuc void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
3867f4a2713aSLionel Sambuc   if (!SemaRef.Context.getLangOpts().OpenCL)
3868f4a2713aSLionel Sambuc     return;
3869f4a2713aSLionel Sambuc 
3870f4a2713aSLionel Sambuc   const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3871f4a2713aSLionel Sambuc   RecordData Record;
3872f4a2713aSLionel Sambuc #define OPENCLEXT(nm)  Record.push_back(Opts.nm);
3873f4a2713aSLionel Sambuc #include "clang/Basic/OpenCLExtensions.def"
3874f4a2713aSLionel Sambuc   Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3875f4a2713aSLionel Sambuc }
3876f4a2713aSLionel Sambuc 
WriteRedeclarations()3877f4a2713aSLionel Sambuc void ASTWriter::WriteRedeclarations() {
3878f4a2713aSLionel Sambuc   RecordData LocalRedeclChains;
3879f4a2713aSLionel Sambuc   SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3880f4a2713aSLionel Sambuc 
3881f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3882f4a2713aSLionel Sambuc     Decl *First = Redeclarations[I];
3883f4a2713aSLionel Sambuc     assert(First->isFirstDecl() && "Not the first declaration?");
3884f4a2713aSLionel Sambuc 
3885f4a2713aSLionel Sambuc     Decl *MostRecent = First->getMostRecentDecl();
3886f4a2713aSLionel Sambuc 
3887f4a2713aSLionel Sambuc     // If we only have a single declaration, there is no point in storing
3888f4a2713aSLionel Sambuc     // a redeclaration chain.
3889f4a2713aSLionel Sambuc     if (First == MostRecent)
3890f4a2713aSLionel Sambuc       continue;
3891f4a2713aSLionel Sambuc 
3892f4a2713aSLionel Sambuc     unsigned Offset = LocalRedeclChains.size();
3893f4a2713aSLionel Sambuc     unsigned Size = 0;
3894f4a2713aSLionel Sambuc     LocalRedeclChains.push_back(0); // Placeholder for the size.
3895f4a2713aSLionel Sambuc 
3896f4a2713aSLionel Sambuc     // Collect the set of local redeclarations of this declaration.
3897f4a2713aSLionel Sambuc     for (Decl *Prev = MostRecent; Prev != First;
3898f4a2713aSLionel Sambuc          Prev = Prev->getPreviousDecl()) {
3899f4a2713aSLionel Sambuc       if (!Prev->isFromASTFile()) {
3900f4a2713aSLionel Sambuc         AddDeclRef(Prev, LocalRedeclChains);
3901f4a2713aSLionel Sambuc         ++Size;
3902f4a2713aSLionel Sambuc       }
3903f4a2713aSLionel Sambuc     }
3904f4a2713aSLionel Sambuc 
3905f4a2713aSLionel Sambuc     if (!First->isFromASTFile() && Chain) {
3906f4a2713aSLionel Sambuc       Decl *FirstFromAST = MostRecent;
3907f4a2713aSLionel Sambuc       for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3908f4a2713aSLionel Sambuc         if (Prev->isFromASTFile())
3909f4a2713aSLionel Sambuc           FirstFromAST = Prev;
3910f4a2713aSLionel Sambuc       }
3911f4a2713aSLionel Sambuc 
3912*0a6a1f1dSLionel Sambuc       // FIXME: Do we need to do this for the first declaration from each
3913*0a6a1f1dSLionel Sambuc       // redeclaration chain that was merged into this one?
3914f4a2713aSLionel Sambuc       Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3915f4a2713aSLionel Sambuc     }
3916f4a2713aSLionel Sambuc 
3917f4a2713aSLionel Sambuc     LocalRedeclChains[Offset] = Size;
3918f4a2713aSLionel Sambuc 
3919f4a2713aSLionel Sambuc     // Reverse the set of local redeclarations, so that we store them in
3920f4a2713aSLionel Sambuc     // order (since we found them in reverse order).
3921f4a2713aSLionel Sambuc     std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3922f4a2713aSLionel Sambuc 
3923f4a2713aSLionel Sambuc     // Add the mapping from the first ID from the AST to the set of local
3924f4a2713aSLionel Sambuc     // declarations.
3925f4a2713aSLionel Sambuc     LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3926f4a2713aSLionel Sambuc     LocalRedeclsMap.push_back(Info);
3927f4a2713aSLionel Sambuc 
3928f4a2713aSLionel Sambuc     assert(N == Redeclarations.size() &&
3929f4a2713aSLionel Sambuc            "Deserialized a declaration we shouldn't have");
3930f4a2713aSLionel Sambuc   }
3931f4a2713aSLionel Sambuc 
3932f4a2713aSLionel Sambuc   if (LocalRedeclChains.empty())
3933f4a2713aSLionel Sambuc     return;
3934f4a2713aSLionel Sambuc 
3935f4a2713aSLionel Sambuc   // Sort the local redeclarations map by the first declaration ID,
3936f4a2713aSLionel Sambuc   // since the reader will be performing binary searches on this information.
3937f4a2713aSLionel Sambuc   llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3938f4a2713aSLionel Sambuc 
3939f4a2713aSLionel Sambuc   // Emit the local redeclarations map.
3940f4a2713aSLionel Sambuc   using namespace llvm;
3941f4a2713aSLionel Sambuc   llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3942f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3943f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3944f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3945f4a2713aSLionel Sambuc   unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3946f4a2713aSLionel Sambuc 
3947f4a2713aSLionel Sambuc   RecordData Record;
3948f4a2713aSLionel Sambuc   Record.push_back(LOCAL_REDECLARATIONS_MAP);
3949f4a2713aSLionel Sambuc   Record.push_back(LocalRedeclsMap.size());
3950f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(AbbrevID, Record,
3951f4a2713aSLionel Sambuc     reinterpret_cast<char*>(LocalRedeclsMap.data()),
3952f4a2713aSLionel Sambuc     LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3953f4a2713aSLionel Sambuc 
3954f4a2713aSLionel Sambuc   // Emit the redeclaration chains.
3955f4a2713aSLionel Sambuc   Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3956f4a2713aSLionel Sambuc }
3957f4a2713aSLionel Sambuc 
WriteObjCCategories()3958f4a2713aSLionel Sambuc void ASTWriter::WriteObjCCategories() {
3959f4a2713aSLionel Sambuc   SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3960f4a2713aSLionel Sambuc   RecordData Categories;
3961f4a2713aSLionel Sambuc 
3962f4a2713aSLionel Sambuc   for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3963f4a2713aSLionel Sambuc     unsigned Size = 0;
3964f4a2713aSLionel Sambuc     unsigned StartIndex = Categories.size();
3965f4a2713aSLionel Sambuc 
3966f4a2713aSLionel Sambuc     ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3967f4a2713aSLionel Sambuc 
3968f4a2713aSLionel Sambuc     // Allocate space for the size.
3969f4a2713aSLionel Sambuc     Categories.push_back(0);
3970f4a2713aSLionel Sambuc 
3971f4a2713aSLionel Sambuc     // Add the categories.
3972f4a2713aSLionel Sambuc     for (ObjCInterfaceDecl::known_categories_iterator
3973f4a2713aSLionel Sambuc            Cat = Class->known_categories_begin(),
3974f4a2713aSLionel Sambuc            CatEnd = Class->known_categories_end();
3975f4a2713aSLionel Sambuc          Cat != CatEnd; ++Cat, ++Size) {
3976f4a2713aSLionel Sambuc       assert(getDeclID(*Cat) != 0 && "Bogus category");
3977f4a2713aSLionel Sambuc       AddDeclRef(*Cat, Categories);
3978f4a2713aSLionel Sambuc     }
3979f4a2713aSLionel Sambuc 
3980f4a2713aSLionel Sambuc     // Update the size.
3981f4a2713aSLionel Sambuc     Categories[StartIndex] = Size;
3982f4a2713aSLionel Sambuc 
3983f4a2713aSLionel Sambuc     // Record this interface -> category map.
3984f4a2713aSLionel Sambuc     ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3985f4a2713aSLionel Sambuc     CategoriesMap.push_back(CatInfo);
3986f4a2713aSLionel Sambuc   }
3987f4a2713aSLionel Sambuc 
3988f4a2713aSLionel Sambuc   // Sort the categories map by the definition ID, since the reader will be
3989f4a2713aSLionel Sambuc   // performing binary searches on this information.
3990f4a2713aSLionel Sambuc   llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3991f4a2713aSLionel Sambuc 
3992f4a2713aSLionel Sambuc   // Emit the categories map.
3993f4a2713aSLionel Sambuc   using namespace llvm;
3994f4a2713aSLionel Sambuc   llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3995f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3996f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3997f4a2713aSLionel Sambuc   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3998f4a2713aSLionel Sambuc   unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3999f4a2713aSLionel Sambuc 
4000f4a2713aSLionel Sambuc   RecordData Record;
4001f4a2713aSLionel Sambuc   Record.push_back(OBJC_CATEGORIES_MAP);
4002f4a2713aSLionel Sambuc   Record.push_back(CategoriesMap.size());
4003f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(AbbrevID, Record,
4004f4a2713aSLionel Sambuc                             reinterpret_cast<char*>(CategoriesMap.data()),
4005f4a2713aSLionel Sambuc                             CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
4006f4a2713aSLionel Sambuc 
4007f4a2713aSLionel Sambuc   // Emit the category lists.
4008f4a2713aSLionel Sambuc   Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4009f4a2713aSLionel Sambuc }
4010f4a2713aSLionel Sambuc 
WriteMergedDecls()4011f4a2713aSLionel Sambuc void ASTWriter::WriteMergedDecls() {
4012f4a2713aSLionel Sambuc   if (!Chain || Chain->MergedDecls.empty())
4013f4a2713aSLionel Sambuc     return;
4014f4a2713aSLionel Sambuc 
4015f4a2713aSLionel Sambuc   RecordData Record;
4016f4a2713aSLionel Sambuc   for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
4017f4a2713aSLionel Sambuc                                         IEnd = Chain->MergedDecls.end();
4018f4a2713aSLionel Sambuc        I != IEnd; ++I) {
4019f4a2713aSLionel Sambuc     DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
4020*0a6a1f1dSLionel Sambuc                                               : GetDeclRef(I->first);
4021f4a2713aSLionel Sambuc     assert(CanonID && "Merged declaration not known?");
4022f4a2713aSLionel Sambuc 
4023f4a2713aSLionel Sambuc     Record.push_back(CanonID);
4024f4a2713aSLionel Sambuc     Record.push_back(I->second.size());
4025f4a2713aSLionel Sambuc     Record.append(I->second.begin(), I->second.end());
4026f4a2713aSLionel Sambuc   }
4027f4a2713aSLionel Sambuc   Stream.EmitRecord(MERGED_DECLARATIONS, Record);
4028f4a2713aSLionel Sambuc }
4029f4a2713aSLionel Sambuc 
WriteLateParsedTemplates(Sema & SemaRef)4030f4a2713aSLionel Sambuc void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4031f4a2713aSLionel Sambuc   Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4032f4a2713aSLionel Sambuc 
4033f4a2713aSLionel Sambuc   if (LPTMap.empty())
4034f4a2713aSLionel Sambuc     return;
4035f4a2713aSLionel Sambuc 
4036f4a2713aSLionel Sambuc   RecordData Record;
4037f4a2713aSLionel Sambuc   for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4038f4a2713aSLionel Sambuc                                               ItEnd = LPTMap.end();
4039f4a2713aSLionel Sambuc        It != ItEnd; ++It) {
4040f4a2713aSLionel Sambuc     LateParsedTemplate *LPT = It->second;
4041f4a2713aSLionel Sambuc     AddDeclRef(It->first, Record);
4042f4a2713aSLionel Sambuc     AddDeclRef(LPT->D, Record);
4043f4a2713aSLionel Sambuc     Record.push_back(LPT->Toks.size());
4044f4a2713aSLionel Sambuc 
4045f4a2713aSLionel Sambuc     for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4046f4a2713aSLionel Sambuc                                 TokEnd = LPT->Toks.end();
4047f4a2713aSLionel Sambuc          TokIt != TokEnd; ++TokIt) {
4048f4a2713aSLionel Sambuc       AddToken(*TokIt, Record);
4049f4a2713aSLionel Sambuc     }
4050f4a2713aSLionel Sambuc   }
4051f4a2713aSLionel Sambuc   Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4052f4a2713aSLionel Sambuc }
4053f4a2713aSLionel Sambuc 
4054*0a6a1f1dSLionel Sambuc /// \brief Write the state of 'pragma clang optimize' at the end of the module.
WriteOptimizePragmaOptions(Sema & SemaRef)4055*0a6a1f1dSLionel Sambuc void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4056*0a6a1f1dSLionel Sambuc   RecordData Record;
4057*0a6a1f1dSLionel Sambuc   SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4058*0a6a1f1dSLionel Sambuc   AddSourceLocation(PragmaLoc, Record);
4059*0a6a1f1dSLionel Sambuc   Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4060*0a6a1f1dSLionel Sambuc }
4061*0a6a1f1dSLionel Sambuc 
4062f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4063f4a2713aSLionel Sambuc // General Serialization Routines
4064f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4065f4a2713aSLionel Sambuc 
4066f4a2713aSLionel Sambuc /// \brief Write a record containing the given attributes.
WriteAttributes(ArrayRef<const Attr * > Attrs,RecordDataImpl & Record)4067f4a2713aSLionel Sambuc void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4068f4a2713aSLionel Sambuc                                 RecordDataImpl &Record) {
4069f4a2713aSLionel Sambuc   Record.push_back(Attrs.size());
4070f4a2713aSLionel Sambuc   for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4071f4a2713aSLionel Sambuc                                         e = Attrs.end(); i != e; ++i){
4072f4a2713aSLionel Sambuc     const Attr *A = *i;
4073f4a2713aSLionel Sambuc     Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
4074f4a2713aSLionel Sambuc     AddSourceRange(A->getRange(), Record);
4075f4a2713aSLionel Sambuc 
4076f4a2713aSLionel Sambuc #include "clang/Serialization/AttrPCHWrite.inc"
4077f4a2713aSLionel Sambuc 
4078f4a2713aSLionel Sambuc   }
4079f4a2713aSLionel Sambuc }
4080f4a2713aSLionel Sambuc 
AddToken(const Token & Tok,RecordDataImpl & Record)4081f4a2713aSLionel Sambuc void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4082f4a2713aSLionel Sambuc   AddSourceLocation(Tok.getLocation(), Record);
4083f4a2713aSLionel Sambuc   Record.push_back(Tok.getLength());
4084f4a2713aSLionel Sambuc 
4085f4a2713aSLionel Sambuc   // FIXME: When reading literal tokens, reconstruct the literal pointer
4086f4a2713aSLionel Sambuc   // if it is needed.
4087f4a2713aSLionel Sambuc   AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4088f4a2713aSLionel Sambuc   // FIXME: Should translate token kind to a stable encoding.
4089f4a2713aSLionel Sambuc   Record.push_back(Tok.getKind());
4090f4a2713aSLionel Sambuc   // FIXME: Should translate token flags to a stable encoding.
4091f4a2713aSLionel Sambuc   Record.push_back(Tok.getFlags());
4092f4a2713aSLionel Sambuc }
4093f4a2713aSLionel Sambuc 
AddString(StringRef Str,RecordDataImpl & Record)4094f4a2713aSLionel Sambuc void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
4095f4a2713aSLionel Sambuc   Record.push_back(Str.size());
4096f4a2713aSLionel Sambuc   Record.insert(Record.end(), Str.begin(), Str.end());
4097f4a2713aSLionel Sambuc }
4098f4a2713aSLionel Sambuc 
PreparePathForOutput(SmallVectorImpl<char> & Path)4099*0a6a1f1dSLionel Sambuc bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4100*0a6a1f1dSLionel Sambuc   assert(Context && "should have context when outputting path");
4101*0a6a1f1dSLionel Sambuc 
4102*0a6a1f1dSLionel Sambuc   bool Changed =
4103*0a6a1f1dSLionel Sambuc       cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4104*0a6a1f1dSLionel Sambuc 
4105*0a6a1f1dSLionel Sambuc   // Remove a prefix to make the path relative, if relevant.
4106*0a6a1f1dSLionel Sambuc   const char *PathBegin = Path.data();
4107*0a6a1f1dSLionel Sambuc   const char *PathPtr =
4108*0a6a1f1dSLionel Sambuc       adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4109*0a6a1f1dSLionel Sambuc   if (PathPtr != PathBegin) {
4110*0a6a1f1dSLionel Sambuc     Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4111*0a6a1f1dSLionel Sambuc     Changed = true;
4112*0a6a1f1dSLionel Sambuc   }
4113*0a6a1f1dSLionel Sambuc 
4114*0a6a1f1dSLionel Sambuc   return Changed;
4115*0a6a1f1dSLionel Sambuc }
4116*0a6a1f1dSLionel Sambuc 
AddPath(StringRef Path,RecordDataImpl & Record)4117*0a6a1f1dSLionel Sambuc void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4118*0a6a1f1dSLionel Sambuc   SmallString<128> FilePath(Path);
4119*0a6a1f1dSLionel Sambuc   PreparePathForOutput(FilePath);
4120*0a6a1f1dSLionel Sambuc   AddString(FilePath, Record);
4121*0a6a1f1dSLionel Sambuc }
4122*0a6a1f1dSLionel Sambuc 
EmitRecordWithPath(unsigned Abbrev,RecordDataImpl & Record,StringRef Path)4123*0a6a1f1dSLionel Sambuc void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4124*0a6a1f1dSLionel Sambuc                                    StringRef Path) {
4125*0a6a1f1dSLionel Sambuc   SmallString<128> FilePath(Path);
4126*0a6a1f1dSLionel Sambuc   PreparePathForOutput(FilePath);
4127*0a6a1f1dSLionel Sambuc   Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4128*0a6a1f1dSLionel Sambuc }
4129*0a6a1f1dSLionel Sambuc 
AddVersionTuple(const VersionTuple & Version,RecordDataImpl & Record)4130f4a2713aSLionel Sambuc void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4131f4a2713aSLionel Sambuc                                 RecordDataImpl &Record) {
4132f4a2713aSLionel Sambuc   Record.push_back(Version.getMajor());
4133f4a2713aSLionel Sambuc   if (Optional<unsigned> Minor = Version.getMinor())
4134f4a2713aSLionel Sambuc     Record.push_back(*Minor + 1);
4135f4a2713aSLionel Sambuc   else
4136f4a2713aSLionel Sambuc     Record.push_back(0);
4137f4a2713aSLionel Sambuc   if (Optional<unsigned> Subminor = Version.getSubminor())
4138f4a2713aSLionel Sambuc     Record.push_back(*Subminor + 1);
4139f4a2713aSLionel Sambuc   else
4140f4a2713aSLionel Sambuc     Record.push_back(0);
4141f4a2713aSLionel Sambuc }
4142f4a2713aSLionel Sambuc 
4143f4a2713aSLionel Sambuc /// \brief Note that the identifier II occurs at the given offset
4144f4a2713aSLionel Sambuc /// within the identifier table.
SetIdentifierOffset(const IdentifierInfo * II,uint32_t Offset)4145f4a2713aSLionel Sambuc void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4146f4a2713aSLionel Sambuc   IdentID ID = IdentifierIDs[II];
4147f4a2713aSLionel Sambuc   // Only store offsets new to this AST file. Other identifier names are looked
4148f4a2713aSLionel Sambuc   // up earlier in the chain and thus don't need an offset.
4149f4a2713aSLionel Sambuc   if (ID >= FirstIdentID)
4150f4a2713aSLionel Sambuc     IdentifierOffsets[ID - FirstIdentID] = Offset;
4151f4a2713aSLionel Sambuc }
4152f4a2713aSLionel Sambuc 
4153f4a2713aSLionel Sambuc /// \brief Note that the selector Sel occurs at the given offset
4154f4a2713aSLionel Sambuc /// within the method pool/selector table.
SetSelectorOffset(Selector Sel,uint32_t Offset)4155f4a2713aSLionel Sambuc void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4156f4a2713aSLionel Sambuc   unsigned ID = SelectorIDs[Sel];
4157f4a2713aSLionel Sambuc   assert(ID && "Unknown selector");
4158f4a2713aSLionel Sambuc   // Don't record offsets for selectors that are also available in a different
4159f4a2713aSLionel Sambuc   // file.
4160f4a2713aSLionel Sambuc   if (ID < FirstSelectorID)
4161f4a2713aSLionel Sambuc     return;
4162f4a2713aSLionel Sambuc   SelectorOffsets[ID - FirstSelectorID] = Offset;
4163f4a2713aSLionel Sambuc }
4164f4a2713aSLionel Sambuc 
ASTWriter(llvm::BitstreamWriter & Stream)4165f4a2713aSLionel Sambuc ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
4166*0a6a1f1dSLionel Sambuc     : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4167*0a6a1f1dSLionel Sambuc       WritingModule(nullptr), WritingAST(false),
4168*0a6a1f1dSLionel Sambuc       DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4169f4a2713aSLionel Sambuc       FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4170f4a2713aSLionel Sambuc       FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4171f4a2713aSLionel Sambuc       FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4172f4a2713aSLionel Sambuc       FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4173f4a2713aSLionel Sambuc       FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4174f4a2713aSLionel Sambuc       NextSubmoduleID(FirstSubmoduleID),
4175f4a2713aSLionel Sambuc       FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4176*0a6a1f1dSLionel Sambuc       CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4177*0a6a1f1dSLionel Sambuc       NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4178*0a6a1f1dSLionel Sambuc       NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4179*0a6a1f1dSLionel Sambuc       TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4180*0a6a1f1dSLionel Sambuc       DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
4181*0a6a1f1dSLionel Sambuc       UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
4182*0a6a1f1dSLionel Sambuc       DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
4183*0a6a1f1dSLionel Sambuc       DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4184*0a6a1f1dSLionel Sambuc       CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4185*0a6a1f1dSLionel Sambuc       ExprImplicitCastAbbrev(0) {}
4186f4a2713aSLionel Sambuc 
~ASTWriter()4187f4a2713aSLionel Sambuc ASTWriter::~ASTWriter() {
4188*0a6a1f1dSLionel Sambuc   llvm::DeleteContainerSeconds(FileDeclIDs);
4189f4a2713aSLionel Sambuc }
4190f4a2713aSLionel Sambuc 
WriteAST(Sema & SemaRef,const std::string & OutputFile,Module * WritingModule,StringRef isysroot,bool hasErrors)4191f4a2713aSLionel Sambuc void ASTWriter::WriteAST(Sema &SemaRef,
4192f4a2713aSLionel Sambuc                          const std::string &OutputFile,
4193f4a2713aSLionel Sambuc                          Module *WritingModule, StringRef isysroot,
4194f4a2713aSLionel Sambuc                          bool hasErrors) {
4195f4a2713aSLionel Sambuc   WritingAST = true;
4196f4a2713aSLionel Sambuc 
4197f4a2713aSLionel Sambuc   ASTHasCompilerErrors = hasErrors;
4198f4a2713aSLionel Sambuc 
4199f4a2713aSLionel Sambuc   // Emit the file header.
4200f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'C', 8);
4201f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'P', 8);
4202f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'C', 8);
4203f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'H', 8);
4204f4a2713aSLionel Sambuc 
4205f4a2713aSLionel Sambuc   WriteBlockInfoBlock();
4206f4a2713aSLionel Sambuc 
4207f4a2713aSLionel Sambuc   Context = &SemaRef.Context;
4208f4a2713aSLionel Sambuc   PP = &SemaRef.PP;
4209f4a2713aSLionel Sambuc   this->WritingModule = WritingModule;
4210f4a2713aSLionel Sambuc   WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
4211*0a6a1f1dSLionel Sambuc   Context = nullptr;
4212*0a6a1f1dSLionel Sambuc   PP = nullptr;
4213*0a6a1f1dSLionel Sambuc   this->WritingModule = nullptr;
4214*0a6a1f1dSLionel Sambuc   this->BaseDirectory.clear();
4215f4a2713aSLionel Sambuc 
4216f4a2713aSLionel Sambuc   WritingAST = false;
4217f4a2713aSLionel Sambuc }
4218f4a2713aSLionel Sambuc 
4219f4a2713aSLionel Sambuc template<typename Vector>
AddLazyVectorDecls(ASTWriter & Writer,Vector & Vec,ASTWriter::RecordData & Record)4220f4a2713aSLionel Sambuc static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4221f4a2713aSLionel Sambuc                                ASTWriter::RecordData &Record) {
4222*0a6a1f1dSLionel Sambuc   for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4223f4a2713aSLionel Sambuc        I != E; ++I) {
4224f4a2713aSLionel Sambuc     Writer.AddDeclRef(*I, Record);
4225f4a2713aSLionel Sambuc   }
4226f4a2713aSLionel Sambuc }
4227f4a2713aSLionel Sambuc 
WriteASTCore(Sema & SemaRef,StringRef isysroot,const std::string & OutputFile,Module * WritingModule)4228f4a2713aSLionel Sambuc void ASTWriter::WriteASTCore(Sema &SemaRef,
4229f4a2713aSLionel Sambuc                              StringRef isysroot,
4230f4a2713aSLionel Sambuc                              const std::string &OutputFile,
4231f4a2713aSLionel Sambuc                              Module *WritingModule) {
4232f4a2713aSLionel Sambuc   using namespace llvm;
4233f4a2713aSLionel Sambuc 
4234*0a6a1f1dSLionel Sambuc   bool isModule = WritingModule != nullptr;
4235f4a2713aSLionel Sambuc 
4236f4a2713aSLionel Sambuc   // Make sure that the AST reader knows to finalize itself.
4237f4a2713aSLionel Sambuc   if (Chain)
4238f4a2713aSLionel Sambuc     Chain->finalizeForWriting();
4239f4a2713aSLionel Sambuc 
4240f4a2713aSLionel Sambuc   ASTContext &Context = SemaRef.Context;
4241f4a2713aSLionel Sambuc   Preprocessor &PP = SemaRef.PP;
4242f4a2713aSLionel Sambuc 
4243f4a2713aSLionel Sambuc   // Set up predefined declaration IDs.
4244f4a2713aSLionel Sambuc   DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
4245f4a2713aSLionel Sambuc   if (Context.ObjCIdDecl)
4246f4a2713aSLionel Sambuc     DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
4247f4a2713aSLionel Sambuc   if (Context.ObjCSelDecl)
4248f4a2713aSLionel Sambuc     DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
4249f4a2713aSLionel Sambuc   if (Context.ObjCClassDecl)
4250f4a2713aSLionel Sambuc     DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
4251f4a2713aSLionel Sambuc   if (Context.ObjCProtocolClassDecl)
4252f4a2713aSLionel Sambuc     DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
4253f4a2713aSLionel Sambuc   if (Context.Int128Decl)
4254f4a2713aSLionel Sambuc     DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4255f4a2713aSLionel Sambuc   if (Context.UInt128Decl)
4256f4a2713aSLionel Sambuc     DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
4257f4a2713aSLionel Sambuc   if (Context.ObjCInstanceTypeDecl)
4258f4a2713aSLionel Sambuc     DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
4259f4a2713aSLionel Sambuc   if (Context.BuiltinVaListDecl)
4260f4a2713aSLionel Sambuc     DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4261f4a2713aSLionel Sambuc 
4262f4a2713aSLionel Sambuc   if (!Chain) {
4263f4a2713aSLionel Sambuc     // Make sure that we emit IdentifierInfos (and any attached
4264f4a2713aSLionel Sambuc     // declarations) for builtins. We don't need to do this when we're
4265f4a2713aSLionel Sambuc     // emitting chained PCH files, because all of the builtins will be
4266f4a2713aSLionel Sambuc     // in the original PCH file.
4267f4a2713aSLionel Sambuc     // FIXME: Modules won't like this at all.
4268f4a2713aSLionel Sambuc     IdentifierTable &Table = PP.getIdentifierTable();
4269f4a2713aSLionel Sambuc     SmallVector<const char *, 32> BuiltinNames;
4270f4a2713aSLionel Sambuc     if (!Context.getLangOpts().NoBuiltin) {
4271f4a2713aSLionel Sambuc       Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4272f4a2713aSLionel Sambuc     }
4273f4a2713aSLionel Sambuc     for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4274f4a2713aSLionel Sambuc       getIdentifierRef(&Table.get(BuiltinNames[I]));
4275f4a2713aSLionel Sambuc   }
4276f4a2713aSLionel Sambuc 
4277f4a2713aSLionel Sambuc   // If there are any out-of-date identifiers, bring them up to date.
4278f4a2713aSLionel Sambuc   if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
4279f4a2713aSLionel Sambuc     // Find out-of-date identifiers.
4280f4a2713aSLionel Sambuc     SmallVector<IdentifierInfo *, 4> OutOfDate;
4281f4a2713aSLionel Sambuc     for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4282f4a2713aSLionel Sambuc                                 IDEnd = PP.getIdentifierTable().end();
4283f4a2713aSLionel Sambuc          ID != IDEnd; ++ID) {
4284f4a2713aSLionel Sambuc       if (ID->second->isOutOfDate())
4285f4a2713aSLionel Sambuc         OutOfDate.push_back(ID->second);
4286f4a2713aSLionel Sambuc     }
4287f4a2713aSLionel Sambuc 
4288f4a2713aSLionel Sambuc     // Update the out-of-date identifiers.
4289f4a2713aSLionel Sambuc     for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4290f4a2713aSLionel Sambuc       ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4291f4a2713aSLionel Sambuc     }
4292f4a2713aSLionel Sambuc   }
4293f4a2713aSLionel Sambuc 
4294*0a6a1f1dSLionel Sambuc   // If we saw any DeclContext updates before we started writing the AST file,
4295*0a6a1f1dSLionel Sambuc   // make sure all visible decls in those DeclContexts are written out.
4296*0a6a1f1dSLionel Sambuc   if (!UpdatedDeclContexts.empty()) {
4297*0a6a1f1dSLionel Sambuc     auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4298*0a6a1f1dSLionel Sambuc     UpdatedDeclContexts.clear();
4299*0a6a1f1dSLionel Sambuc     for (auto *DC : OldUpdatedDeclContexts)
4300*0a6a1f1dSLionel Sambuc       AddUpdatedDeclContext(DC);
4301*0a6a1f1dSLionel Sambuc   }
4302*0a6a1f1dSLionel Sambuc 
4303f4a2713aSLionel Sambuc   // Build a record containing all of the tentative definitions in this file, in
4304f4a2713aSLionel Sambuc   // TentativeDefinitions order.  Generally, this record will be empty for
4305f4a2713aSLionel Sambuc   // headers.
4306f4a2713aSLionel Sambuc   RecordData TentativeDefinitions;
4307f4a2713aSLionel Sambuc   AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4308f4a2713aSLionel Sambuc 
4309f4a2713aSLionel Sambuc   // Build a record containing all of the file scoped decls in this file.
4310f4a2713aSLionel Sambuc   RecordData UnusedFileScopedDecls;
4311f4a2713aSLionel Sambuc   if (!isModule)
4312f4a2713aSLionel Sambuc     AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4313f4a2713aSLionel Sambuc                        UnusedFileScopedDecls);
4314f4a2713aSLionel Sambuc 
4315f4a2713aSLionel Sambuc   // Build a record containing all of the delegating constructors we still need
4316f4a2713aSLionel Sambuc   // to resolve.
4317f4a2713aSLionel Sambuc   RecordData DelegatingCtorDecls;
4318f4a2713aSLionel Sambuc   if (!isModule)
4319f4a2713aSLionel Sambuc     AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4320f4a2713aSLionel Sambuc 
4321f4a2713aSLionel Sambuc   // Write the set of weak, undeclared identifiers. We always write the
4322f4a2713aSLionel Sambuc   // entire table, since later PCH files in a PCH chain are only interested in
4323f4a2713aSLionel Sambuc   // the results at the end of the chain.
4324f4a2713aSLionel Sambuc   RecordData WeakUndeclaredIdentifiers;
4325f4a2713aSLionel Sambuc   if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
4326f4a2713aSLionel Sambuc     for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
4327f4a2713aSLionel Sambuc          I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4328f4a2713aSLionel Sambuc          E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4329f4a2713aSLionel Sambuc       AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4330f4a2713aSLionel Sambuc       AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4331f4a2713aSLionel Sambuc       AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4332f4a2713aSLionel Sambuc       WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4333f4a2713aSLionel Sambuc     }
4334f4a2713aSLionel Sambuc   }
4335f4a2713aSLionel Sambuc 
4336f4a2713aSLionel Sambuc   // Build a record containing all of the locally-scoped extern "C"
4337f4a2713aSLionel Sambuc   // declarations in this header file. Generally, this record will be
4338f4a2713aSLionel Sambuc   // empty.
4339f4a2713aSLionel Sambuc   RecordData LocallyScopedExternCDecls;
4340f4a2713aSLionel Sambuc   // FIXME: This is filling in the AST file in densemap order which is
4341f4a2713aSLionel Sambuc   // nondeterminstic!
4342f4a2713aSLionel Sambuc   for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
4343f4a2713aSLionel Sambuc          TD = SemaRef.LocallyScopedExternCDecls.begin(),
4344f4a2713aSLionel Sambuc          TDEnd = SemaRef.LocallyScopedExternCDecls.end();
4345f4a2713aSLionel Sambuc        TD != TDEnd; ++TD) {
4346f4a2713aSLionel Sambuc     if (!TD->second->isFromASTFile())
4347f4a2713aSLionel Sambuc       AddDeclRef(TD->second, LocallyScopedExternCDecls);
4348f4a2713aSLionel Sambuc   }
4349f4a2713aSLionel Sambuc 
4350f4a2713aSLionel Sambuc   // Build a record containing all of the ext_vector declarations.
4351f4a2713aSLionel Sambuc   RecordData ExtVectorDecls;
4352f4a2713aSLionel Sambuc   AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4353f4a2713aSLionel Sambuc 
4354f4a2713aSLionel Sambuc   // Build a record containing all of the VTable uses information.
4355f4a2713aSLionel Sambuc   RecordData VTableUses;
4356f4a2713aSLionel Sambuc   if (!SemaRef.VTableUses.empty()) {
4357f4a2713aSLionel Sambuc     for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4358f4a2713aSLionel Sambuc       AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4359f4a2713aSLionel Sambuc       AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4360f4a2713aSLionel Sambuc       VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4361f4a2713aSLionel Sambuc     }
4362f4a2713aSLionel Sambuc   }
4363f4a2713aSLionel Sambuc 
4364*0a6a1f1dSLionel Sambuc   // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4365*0a6a1f1dSLionel Sambuc   RecordData UnusedLocalTypedefNameCandidates;
4366*0a6a1f1dSLionel Sambuc   for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4367*0a6a1f1dSLionel Sambuc     AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4368*0a6a1f1dSLionel Sambuc 
4369f4a2713aSLionel Sambuc   // Build a record containing all of dynamic classes declarations.
4370f4a2713aSLionel Sambuc   RecordData DynamicClasses;
4371f4a2713aSLionel Sambuc   AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
4372f4a2713aSLionel Sambuc 
4373f4a2713aSLionel Sambuc   // Build a record containing all of pending implicit instantiations.
4374f4a2713aSLionel Sambuc   RecordData PendingInstantiations;
4375f4a2713aSLionel Sambuc   for (std::deque<Sema::PendingImplicitInstantiation>::iterator
4376f4a2713aSLionel Sambuc          I = SemaRef.PendingInstantiations.begin(),
4377f4a2713aSLionel Sambuc          N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4378f4a2713aSLionel Sambuc     AddDeclRef(I->first, PendingInstantiations);
4379f4a2713aSLionel Sambuc     AddSourceLocation(I->second, PendingInstantiations);
4380f4a2713aSLionel Sambuc   }
4381f4a2713aSLionel Sambuc   assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4382f4a2713aSLionel Sambuc          "There are local ones at end of translation unit!");
4383f4a2713aSLionel Sambuc 
4384f4a2713aSLionel Sambuc   // Build a record containing some declaration references.
4385f4a2713aSLionel Sambuc   RecordData SemaDeclRefs;
4386f4a2713aSLionel Sambuc   if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4387f4a2713aSLionel Sambuc     AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4388f4a2713aSLionel Sambuc     AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4389f4a2713aSLionel Sambuc   }
4390f4a2713aSLionel Sambuc 
4391f4a2713aSLionel Sambuc   RecordData CUDASpecialDeclRefs;
4392f4a2713aSLionel Sambuc   if (Context.getcudaConfigureCallDecl()) {
4393f4a2713aSLionel Sambuc     AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4394f4a2713aSLionel Sambuc   }
4395f4a2713aSLionel Sambuc 
4396f4a2713aSLionel Sambuc   // Build a record containing all of the known namespaces.
4397f4a2713aSLionel Sambuc   RecordData KnownNamespaces;
4398f4a2713aSLionel Sambuc   for (llvm::MapVector<NamespaceDecl*, bool>::iterator
4399f4a2713aSLionel Sambuc             I = SemaRef.KnownNamespaces.begin(),
4400f4a2713aSLionel Sambuc          IEnd = SemaRef.KnownNamespaces.end();
4401f4a2713aSLionel Sambuc        I != IEnd; ++I) {
4402f4a2713aSLionel Sambuc     if (!I->second)
4403f4a2713aSLionel Sambuc       AddDeclRef(I->first, KnownNamespaces);
4404f4a2713aSLionel Sambuc   }
4405f4a2713aSLionel Sambuc 
4406f4a2713aSLionel Sambuc   // Build a record of all used, undefined objects that require definitions.
4407f4a2713aSLionel Sambuc   RecordData UndefinedButUsed;
4408f4a2713aSLionel Sambuc 
4409f4a2713aSLionel Sambuc   SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4410f4a2713aSLionel Sambuc   SemaRef.getUndefinedButUsed(Undefined);
4411f4a2713aSLionel Sambuc   for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4412f4a2713aSLionel Sambuc          I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
4413f4a2713aSLionel Sambuc     AddDeclRef(I->first, UndefinedButUsed);
4414f4a2713aSLionel Sambuc     AddSourceLocation(I->second, UndefinedButUsed);
4415f4a2713aSLionel Sambuc   }
4416f4a2713aSLionel Sambuc 
4417f4a2713aSLionel Sambuc   // Write the control block
4418f4a2713aSLionel Sambuc   WriteControlBlock(PP, Context, isysroot, OutputFile);
4419f4a2713aSLionel Sambuc 
4420f4a2713aSLionel Sambuc   // Write the remaining AST contents.
4421f4a2713aSLionel Sambuc   RecordData Record;
4422f4a2713aSLionel Sambuc   Stream.EnterSubblock(AST_BLOCK_ID, 5);
4423f4a2713aSLionel Sambuc 
4424f4a2713aSLionel Sambuc   // This is so that older clang versions, before the introduction
4425f4a2713aSLionel Sambuc   // of the control block, can read and reject the newer PCH format.
4426f4a2713aSLionel Sambuc   Record.clear();
4427f4a2713aSLionel Sambuc   Record.push_back(VERSION_MAJOR);
4428f4a2713aSLionel Sambuc   Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4429f4a2713aSLionel Sambuc 
4430f4a2713aSLionel Sambuc   // Create a lexical update block containing all of the declarations in the
4431f4a2713aSLionel Sambuc   // translation unit that do not come from other AST files.
4432f4a2713aSLionel Sambuc   const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4433f4a2713aSLionel Sambuc   SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4434*0a6a1f1dSLionel Sambuc   for (const auto *I : TU->noload_decls()) {
4435*0a6a1f1dSLionel Sambuc     if (!I->isFromASTFile())
4436*0a6a1f1dSLionel Sambuc       NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
4437f4a2713aSLionel Sambuc   }
4438f4a2713aSLionel Sambuc 
4439f4a2713aSLionel Sambuc   llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4440f4a2713aSLionel Sambuc   Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4441f4a2713aSLionel Sambuc   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4442f4a2713aSLionel Sambuc   unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4443f4a2713aSLionel Sambuc   Record.clear();
4444f4a2713aSLionel Sambuc   Record.push_back(TU_UPDATE_LEXICAL);
4445f4a2713aSLionel Sambuc   Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4446f4a2713aSLionel Sambuc                             data(NewGlobalDecls));
4447f4a2713aSLionel Sambuc 
4448f4a2713aSLionel Sambuc   // And a visible updates block for the translation unit.
4449f4a2713aSLionel Sambuc   Abv = new llvm::BitCodeAbbrev();
4450f4a2713aSLionel Sambuc   Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4451f4a2713aSLionel Sambuc   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4452f4a2713aSLionel Sambuc   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4453f4a2713aSLionel Sambuc   Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4454f4a2713aSLionel Sambuc   UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4455f4a2713aSLionel Sambuc   WriteDeclContextVisibleUpdate(TU);
4456f4a2713aSLionel Sambuc 
4457f4a2713aSLionel Sambuc   // If the translation unit has an anonymous namespace, and we don't already
4458f4a2713aSLionel Sambuc   // have an update block for it, write it as an update block.
4459*0a6a1f1dSLionel Sambuc   // FIXME: Why do we not do this if there's already an update block?
4460f4a2713aSLionel Sambuc   if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4461f4a2713aSLionel Sambuc     ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4462*0a6a1f1dSLionel Sambuc     if (Record.empty())
4463*0a6a1f1dSLionel Sambuc       Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4464f4a2713aSLionel Sambuc   }
4465*0a6a1f1dSLionel Sambuc 
4466*0a6a1f1dSLionel Sambuc   // Add update records for all mangling numbers and static local numbers.
4467*0a6a1f1dSLionel Sambuc   // These aren't really update records, but this is a convenient way of
4468*0a6a1f1dSLionel Sambuc   // tagging this rare extra data onto the declarations.
4469*0a6a1f1dSLionel Sambuc   for (const auto &Number : Context.MangleNumbers)
4470*0a6a1f1dSLionel Sambuc     if (!Number.first->isFromASTFile())
4471*0a6a1f1dSLionel Sambuc       DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4472*0a6a1f1dSLionel Sambuc                                                      Number.second));
4473*0a6a1f1dSLionel Sambuc   for (const auto &Number : Context.StaticLocalNumbers)
4474*0a6a1f1dSLionel Sambuc     if (!Number.first->isFromASTFile())
4475*0a6a1f1dSLionel Sambuc       DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4476*0a6a1f1dSLionel Sambuc                                                      Number.second));
4477f4a2713aSLionel Sambuc 
4478f4a2713aSLionel Sambuc   // Make sure visible decls, added to DeclContexts previously loaded from
4479f4a2713aSLionel Sambuc   // an AST file, are registered for serialization.
4480f4a2713aSLionel Sambuc   for (SmallVectorImpl<const Decl *>::iterator
4481f4a2713aSLionel Sambuc          I = UpdatingVisibleDecls.begin(),
4482f4a2713aSLionel Sambuc          E = UpdatingVisibleDecls.end(); I != E; ++I) {
4483f4a2713aSLionel Sambuc     GetDeclRef(*I);
4484f4a2713aSLionel Sambuc   }
4485f4a2713aSLionel Sambuc 
4486f4a2713aSLionel Sambuc   // Make sure all decls associated with an identifier are registered for
4487f4a2713aSLionel Sambuc   // serialization.
4488f4a2713aSLionel Sambuc   for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4489f4a2713aSLionel Sambuc                               IDEnd = PP.getIdentifierTable().end();
4490f4a2713aSLionel Sambuc        ID != IDEnd; ++ID) {
4491f4a2713aSLionel Sambuc     const IdentifierInfo *II = ID->second;
4492f4a2713aSLionel Sambuc     if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4493f4a2713aSLionel Sambuc       for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4494f4a2713aSLionel Sambuc                                      DEnd = SemaRef.IdResolver.end();
4495f4a2713aSLionel Sambuc            D != DEnd; ++D) {
4496f4a2713aSLionel Sambuc         GetDeclRef(*D);
4497f4a2713aSLionel Sambuc       }
4498f4a2713aSLionel Sambuc     }
4499f4a2713aSLionel Sambuc   }
4500f4a2713aSLionel Sambuc 
4501f4a2713aSLionel Sambuc   // Form the record of special types.
4502f4a2713aSLionel Sambuc   RecordData SpecialTypes;
4503f4a2713aSLionel Sambuc   AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4504f4a2713aSLionel Sambuc   AddTypeRef(Context.getFILEType(), SpecialTypes);
4505f4a2713aSLionel Sambuc   AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4506f4a2713aSLionel Sambuc   AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4507f4a2713aSLionel Sambuc   AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4508f4a2713aSLionel Sambuc   AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
4509f4a2713aSLionel Sambuc   AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
4510f4a2713aSLionel Sambuc   AddTypeRef(Context.getucontext_tType(), SpecialTypes);
4511f4a2713aSLionel Sambuc 
4512f4a2713aSLionel Sambuc   if (Chain) {
4513f4a2713aSLionel Sambuc     // Write the mapping information describing our module dependencies and how
4514f4a2713aSLionel Sambuc     // each of those modules were mapped into our own offset/ID space, so that
4515f4a2713aSLionel Sambuc     // the reader can build the appropriate mapping to its own offset/ID space.
4516f4a2713aSLionel Sambuc     // The map consists solely of a blob with the following format:
4517f4a2713aSLionel Sambuc     // *(module-name-len:i16 module-name:len*i8
4518f4a2713aSLionel Sambuc     //   source-location-offset:i32
4519f4a2713aSLionel Sambuc     //   identifier-id:i32
4520f4a2713aSLionel Sambuc     //   preprocessed-entity-id:i32
4521f4a2713aSLionel Sambuc     //   macro-definition-id:i32
4522f4a2713aSLionel Sambuc     //   submodule-id:i32
4523f4a2713aSLionel Sambuc     //   selector-id:i32
4524f4a2713aSLionel Sambuc     //   declaration-id:i32
4525f4a2713aSLionel Sambuc     //   c++-base-specifiers-id:i32
4526f4a2713aSLionel Sambuc     //   type-id:i32)
4527f4a2713aSLionel Sambuc     //
4528f4a2713aSLionel Sambuc     llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4529f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4530f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4531f4a2713aSLionel Sambuc     unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
4532f4a2713aSLionel Sambuc     SmallString<2048> Buffer;
4533f4a2713aSLionel Sambuc     {
4534f4a2713aSLionel Sambuc       llvm::raw_svector_ostream Out(Buffer);
4535*0a6a1f1dSLionel Sambuc       for (ModuleFile *M : Chain->ModuleMgr) {
4536*0a6a1f1dSLionel Sambuc         using namespace llvm::support;
4537*0a6a1f1dSLionel Sambuc         endian::Writer<little> LE(Out);
4538*0a6a1f1dSLionel Sambuc         StringRef FileName = M->FileName;
4539*0a6a1f1dSLionel Sambuc         LE.write<uint16_t>(FileName.size());
4540f4a2713aSLionel Sambuc         Out.write(FileName.data(), FileName.size());
4541*0a6a1f1dSLionel Sambuc 
4542*0a6a1f1dSLionel Sambuc         // Note: if a base ID was uint max, it would not be possible to load
4543*0a6a1f1dSLionel Sambuc         // another module after it or have more than one entity inside it.
4544*0a6a1f1dSLionel Sambuc         uint32_t None = std::numeric_limits<uint32_t>::max();
4545*0a6a1f1dSLionel Sambuc 
4546*0a6a1f1dSLionel Sambuc         auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4547*0a6a1f1dSLionel Sambuc           assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4548*0a6a1f1dSLionel Sambuc           if (ShouldWrite)
4549*0a6a1f1dSLionel Sambuc             LE.write<uint32_t>(BaseID);
4550*0a6a1f1dSLionel Sambuc           else
4551*0a6a1f1dSLionel Sambuc             LE.write<uint32_t>(None);
4552*0a6a1f1dSLionel Sambuc         };
4553*0a6a1f1dSLionel Sambuc 
4554*0a6a1f1dSLionel Sambuc         // These values should be unique within a chain, since they will be read
4555*0a6a1f1dSLionel Sambuc         // as keys into ContinuousRangeMaps.
4556*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4557*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4558*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4559*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BasePreprocessedEntityID,
4560*0a6a1f1dSLionel Sambuc                           M->NumPreprocessedEntities);
4561*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4562*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4563*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4564*0a6a1f1dSLionel Sambuc         writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
4565f4a2713aSLionel Sambuc       }
4566f4a2713aSLionel Sambuc     }
4567f4a2713aSLionel Sambuc     Record.clear();
4568f4a2713aSLionel Sambuc     Record.push_back(MODULE_OFFSET_MAP);
4569f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4570f4a2713aSLionel Sambuc                               Buffer.data(), Buffer.size());
4571f4a2713aSLionel Sambuc   }
4572*0a6a1f1dSLionel Sambuc 
4573*0a6a1f1dSLionel Sambuc   RecordData DeclUpdatesOffsetsRecord;
4574*0a6a1f1dSLionel Sambuc 
4575*0a6a1f1dSLionel Sambuc   // Keep writing types, declarations, and declaration update records
4576*0a6a1f1dSLionel Sambuc   // until we've emitted all of them.
4577*0a6a1f1dSLionel Sambuc   Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4578*0a6a1f1dSLionel Sambuc   WriteTypeAbbrevs();
4579*0a6a1f1dSLionel Sambuc   WriteDeclAbbrevs();
4580*0a6a1f1dSLionel Sambuc   for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4581*0a6a1f1dSLionel Sambuc                                   E = DeclsToRewrite.end();
4582*0a6a1f1dSLionel Sambuc        I != E; ++I)
4583*0a6a1f1dSLionel Sambuc     DeclTypesToEmit.push(const_cast<Decl*>(*I));
4584*0a6a1f1dSLionel Sambuc   do {
4585*0a6a1f1dSLionel Sambuc     WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4586*0a6a1f1dSLionel Sambuc     while (!DeclTypesToEmit.empty()) {
4587*0a6a1f1dSLionel Sambuc       DeclOrType DOT = DeclTypesToEmit.front();
4588*0a6a1f1dSLionel Sambuc       DeclTypesToEmit.pop();
4589*0a6a1f1dSLionel Sambuc       if (DOT.isType())
4590*0a6a1f1dSLionel Sambuc         WriteType(DOT.getType());
4591*0a6a1f1dSLionel Sambuc       else
4592*0a6a1f1dSLionel Sambuc         WriteDecl(Context, DOT.getDecl());
4593*0a6a1f1dSLionel Sambuc     }
4594*0a6a1f1dSLionel Sambuc   } while (!DeclUpdates.empty());
4595*0a6a1f1dSLionel Sambuc   Stream.ExitBlock();
4596*0a6a1f1dSLionel Sambuc 
4597*0a6a1f1dSLionel Sambuc   DoneWritingDeclsAndTypes = true;
4598*0a6a1f1dSLionel Sambuc 
4599*0a6a1f1dSLionel Sambuc   // These things can only be done once we've written out decls and types.
4600*0a6a1f1dSLionel Sambuc   WriteTypeDeclOffsets();
4601*0a6a1f1dSLionel Sambuc   if (!DeclUpdatesOffsetsRecord.empty())
4602*0a6a1f1dSLionel Sambuc     Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4603*0a6a1f1dSLionel Sambuc   WriteCXXBaseSpecifiersOffsets();
4604*0a6a1f1dSLionel Sambuc   WriteFileDeclIDsMap();
4605*0a6a1f1dSLionel Sambuc   WriteSourceManagerBlock(Context.getSourceManager(), PP);
4606*0a6a1f1dSLionel Sambuc 
4607*0a6a1f1dSLionel Sambuc   WriteComments();
4608f4a2713aSLionel Sambuc   WritePreprocessor(PP, isModule);
4609*0a6a1f1dSLionel Sambuc   WriteHeaderSearch(PP.getHeaderSearchInfo());
4610f4a2713aSLionel Sambuc   WriteSelectors(SemaRef);
4611f4a2713aSLionel Sambuc   WriteReferencedSelectorsPool(SemaRef);
4612f4a2713aSLionel Sambuc   WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
4613f4a2713aSLionel Sambuc   WriteFPPragmaOptions(SemaRef.getFPOptions());
4614f4a2713aSLionel Sambuc   WriteOpenCLExtensions(SemaRef);
4615f4a2713aSLionel Sambuc   WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
4616f4a2713aSLionel Sambuc 
4617f4a2713aSLionel Sambuc   // If we're emitting a module, write out the submodule information.
4618f4a2713aSLionel Sambuc   if (WritingModule)
4619f4a2713aSLionel Sambuc     WriteSubmodules(WritingModule);
4620f4a2713aSLionel Sambuc 
4621f4a2713aSLionel Sambuc   Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4622f4a2713aSLionel Sambuc 
4623f4a2713aSLionel Sambuc   // Write the record containing external, unnamed definitions.
4624*0a6a1f1dSLionel Sambuc   if (!EagerlyDeserializedDecls.empty())
4625*0a6a1f1dSLionel Sambuc     Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
4626f4a2713aSLionel Sambuc 
4627f4a2713aSLionel Sambuc   // Write the record containing tentative definitions.
4628f4a2713aSLionel Sambuc   if (!TentativeDefinitions.empty())
4629f4a2713aSLionel Sambuc     Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
4630f4a2713aSLionel Sambuc 
4631f4a2713aSLionel Sambuc   // Write the record containing unused file scoped decls.
4632f4a2713aSLionel Sambuc   if (!UnusedFileScopedDecls.empty())
4633f4a2713aSLionel Sambuc     Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
4634f4a2713aSLionel Sambuc 
4635f4a2713aSLionel Sambuc   // Write the record containing weak undeclared identifiers.
4636f4a2713aSLionel Sambuc   if (!WeakUndeclaredIdentifiers.empty())
4637f4a2713aSLionel Sambuc     Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
4638f4a2713aSLionel Sambuc                       WeakUndeclaredIdentifiers);
4639f4a2713aSLionel Sambuc 
4640f4a2713aSLionel Sambuc   // Write the record containing locally-scoped extern "C" definitions.
4641f4a2713aSLionel Sambuc   if (!LocallyScopedExternCDecls.empty())
4642f4a2713aSLionel Sambuc     Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4643f4a2713aSLionel Sambuc                       LocallyScopedExternCDecls);
4644f4a2713aSLionel Sambuc 
4645f4a2713aSLionel Sambuc   // Write the record containing ext_vector type names.
4646f4a2713aSLionel Sambuc   if (!ExtVectorDecls.empty())
4647f4a2713aSLionel Sambuc     Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
4648f4a2713aSLionel Sambuc 
4649f4a2713aSLionel Sambuc   // Write the record containing VTable uses information.
4650f4a2713aSLionel Sambuc   if (!VTableUses.empty())
4651f4a2713aSLionel Sambuc     Stream.EmitRecord(VTABLE_USES, VTableUses);
4652f4a2713aSLionel Sambuc 
4653f4a2713aSLionel Sambuc   // Write the record containing dynamic classes declarations.
4654f4a2713aSLionel Sambuc   if (!DynamicClasses.empty())
4655f4a2713aSLionel Sambuc     Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
4656f4a2713aSLionel Sambuc 
4657*0a6a1f1dSLionel Sambuc   // Write the record containing potentially unused local typedefs.
4658*0a6a1f1dSLionel Sambuc   if (!UnusedLocalTypedefNameCandidates.empty())
4659*0a6a1f1dSLionel Sambuc     Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4660*0a6a1f1dSLionel Sambuc                       UnusedLocalTypedefNameCandidates);
4661*0a6a1f1dSLionel Sambuc 
4662f4a2713aSLionel Sambuc   // Write the record containing pending implicit instantiations.
4663f4a2713aSLionel Sambuc   if (!PendingInstantiations.empty())
4664f4a2713aSLionel Sambuc     Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
4665f4a2713aSLionel Sambuc 
4666f4a2713aSLionel Sambuc   // Write the record containing declaration references of Sema.
4667f4a2713aSLionel Sambuc   if (!SemaDeclRefs.empty())
4668f4a2713aSLionel Sambuc     Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
4669f4a2713aSLionel Sambuc 
4670f4a2713aSLionel Sambuc   // Write the record containing CUDA-specific declaration references.
4671f4a2713aSLionel Sambuc   if (!CUDASpecialDeclRefs.empty())
4672f4a2713aSLionel Sambuc     Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
4673f4a2713aSLionel Sambuc 
4674f4a2713aSLionel Sambuc   // Write the delegating constructors.
4675f4a2713aSLionel Sambuc   if (!DelegatingCtorDecls.empty())
4676f4a2713aSLionel Sambuc     Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
4677f4a2713aSLionel Sambuc 
4678f4a2713aSLionel Sambuc   // Write the known namespaces.
4679f4a2713aSLionel Sambuc   if (!KnownNamespaces.empty())
4680f4a2713aSLionel Sambuc     Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
4681f4a2713aSLionel Sambuc 
4682f4a2713aSLionel Sambuc   // Write the undefined internal functions and variables, and inline functions.
4683f4a2713aSLionel Sambuc   if (!UndefinedButUsed.empty())
4684f4a2713aSLionel Sambuc     Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
4685f4a2713aSLionel Sambuc 
4686f4a2713aSLionel Sambuc   // Write the visible updates to DeclContexts.
4687*0a6a1f1dSLionel Sambuc   for (auto *DC : UpdatedDeclContexts)
4688*0a6a1f1dSLionel Sambuc     WriteDeclContextVisibleUpdate(DC);
4689f4a2713aSLionel Sambuc 
4690f4a2713aSLionel Sambuc   if (!WritingModule) {
4691f4a2713aSLionel Sambuc     // Write the submodules that were imported, if any.
4692*0a6a1f1dSLionel Sambuc     struct ModuleInfo {
4693*0a6a1f1dSLionel Sambuc       uint64_t ID;
4694*0a6a1f1dSLionel Sambuc       Module *M;
4695*0a6a1f1dSLionel Sambuc       ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4696*0a6a1f1dSLionel Sambuc     };
4697*0a6a1f1dSLionel Sambuc     llvm::SmallVector<ModuleInfo, 64> Imports;
4698*0a6a1f1dSLionel Sambuc     for (const auto *I : Context.local_imports()) {
4699f4a2713aSLionel Sambuc       assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4700*0a6a1f1dSLionel Sambuc       Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4701*0a6a1f1dSLionel Sambuc                          I->getImportedModule()));
4702f4a2713aSLionel Sambuc     }
4703f4a2713aSLionel Sambuc 
4704*0a6a1f1dSLionel Sambuc     if (!Imports.empty()) {
4705*0a6a1f1dSLionel Sambuc       auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4706*0a6a1f1dSLionel Sambuc         return A.ID < B.ID;
4707*0a6a1f1dSLionel Sambuc       };
4708*0a6a1f1dSLionel Sambuc       auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4709*0a6a1f1dSLionel Sambuc         return A.ID == B.ID;
4710*0a6a1f1dSLionel Sambuc       };
4711*0a6a1f1dSLionel Sambuc 
4712*0a6a1f1dSLionel Sambuc       // Sort and deduplicate module IDs.
4713*0a6a1f1dSLionel Sambuc       std::sort(Imports.begin(), Imports.end(), Cmp);
4714*0a6a1f1dSLionel Sambuc       Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
4715*0a6a1f1dSLionel Sambuc                     Imports.end());
4716*0a6a1f1dSLionel Sambuc 
4717*0a6a1f1dSLionel Sambuc       RecordData ImportedModules;
4718*0a6a1f1dSLionel Sambuc       for (const auto &Import : Imports) {
4719*0a6a1f1dSLionel Sambuc         ImportedModules.push_back(Import.ID);
4720*0a6a1f1dSLionel Sambuc         // FIXME: If the module has macros imported then later has declarations
4721*0a6a1f1dSLionel Sambuc         // imported, this location won't be the right one as a location for the
4722*0a6a1f1dSLionel Sambuc         // declaration imports.
4723*0a6a1f1dSLionel Sambuc         AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4724*0a6a1f1dSLionel Sambuc       }
4725f4a2713aSLionel Sambuc 
4726f4a2713aSLionel Sambuc       Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4727f4a2713aSLionel Sambuc     }
4728f4a2713aSLionel Sambuc   }
4729f4a2713aSLionel Sambuc 
4730f4a2713aSLionel Sambuc   WriteDeclReplacementsBlock();
4731f4a2713aSLionel Sambuc   WriteRedeclarations();
4732f4a2713aSLionel Sambuc   WriteMergedDecls();
4733f4a2713aSLionel Sambuc   WriteObjCCategories();
4734f4a2713aSLionel Sambuc   WriteLateParsedTemplates(SemaRef);
4735*0a6a1f1dSLionel Sambuc   if(!WritingModule)
4736*0a6a1f1dSLionel Sambuc     WriteOptimizePragmaOptions(SemaRef);
4737f4a2713aSLionel Sambuc 
4738f4a2713aSLionel Sambuc   // Some simple statistics
4739f4a2713aSLionel Sambuc   Record.clear();
4740f4a2713aSLionel Sambuc   Record.push_back(NumStatements);
4741f4a2713aSLionel Sambuc   Record.push_back(NumMacros);
4742f4a2713aSLionel Sambuc   Record.push_back(NumLexicalDeclContexts);
4743f4a2713aSLionel Sambuc   Record.push_back(NumVisibleDeclContexts);
4744f4a2713aSLionel Sambuc   Stream.EmitRecord(STATISTICS, Record);
4745f4a2713aSLionel Sambuc   Stream.ExitBlock();
4746f4a2713aSLionel Sambuc }
4747f4a2713aSLionel Sambuc 
WriteDeclUpdatesBlocks(RecordDataImpl & OffsetsRecord)4748*0a6a1f1dSLionel Sambuc void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
4749f4a2713aSLionel Sambuc   if (DeclUpdates.empty())
4750f4a2713aSLionel Sambuc     return;
4751f4a2713aSLionel Sambuc 
4752*0a6a1f1dSLionel Sambuc   DeclUpdateMap LocalUpdates;
4753*0a6a1f1dSLionel Sambuc   LocalUpdates.swap(DeclUpdates);
4754f4a2713aSLionel Sambuc 
4755*0a6a1f1dSLionel Sambuc   for (auto &DeclUpdate : LocalUpdates) {
4756*0a6a1f1dSLionel Sambuc     const Decl *D = DeclUpdate.first;
4757f4a2713aSLionel Sambuc     if (isRewritten(D))
4758f4a2713aSLionel Sambuc       continue; // The decl will be written completely,no need to store updates.
4759f4a2713aSLionel Sambuc 
4760*0a6a1f1dSLionel Sambuc     bool HasUpdatedBody = false;
4761*0a6a1f1dSLionel Sambuc     RecordData Record;
4762*0a6a1f1dSLionel Sambuc     for (auto &Update : DeclUpdate.second) {
4763*0a6a1f1dSLionel Sambuc       DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4764*0a6a1f1dSLionel Sambuc 
4765*0a6a1f1dSLionel Sambuc       Record.push_back(Kind);
4766*0a6a1f1dSLionel Sambuc       switch (Kind) {
4767*0a6a1f1dSLionel Sambuc       case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4768*0a6a1f1dSLionel Sambuc       case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4769*0a6a1f1dSLionel Sambuc       case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4770*0a6a1f1dSLionel Sambuc         assert(Update.getDecl() && "no decl to add?");
4771*0a6a1f1dSLionel Sambuc         Record.push_back(GetDeclRef(Update.getDecl()));
4772*0a6a1f1dSLionel Sambuc         break;
4773*0a6a1f1dSLionel Sambuc 
4774*0a6a1f1dSLionel Sambuc       case UPD_CXX_ADDED_FUNCTION_DEFINITION:
4775*0a6a1f1dSLionel Sambuc         // An updated body is emitted last, so that the reader doesn't need
4776*0a6a1f1dSLionel Sambuc         // to skip over the lazy body to reach statements for other records.
4777*0a6a1f1dSLionel Sambuc         Record.pop_back();
4778*0a6a1f1dSLionel Sambuc         HasUpdatedBody = true;
4779*0a6a1f1dSLionel Sambuc         break;
4780*0a6a1f1dSLionel Sambuc 
4781*0a6a1f1dSLionel Sambuc       case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4782*0a6a1f1dSLionel Sambuc         AddSourceLocation(Update.getLoc(), Record);
4783*0a6a1f1dSLionel Sambuc         break;
4784*0a6a1f1dSLionel Sambuc 
4785*0a6a1f1dSLionel Sambuc       case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4786*0a6a1f1dSLionel Sambuc         auto *RD = cast<CXXRecordDecl>(D);
4787*0a6a1f1dSLionel Sambuc         AddUpdatedDeclContext(RD->getPrimaryContext());
4788*0a6a1f1dSLionel Sambuc         AddCXXDefinitionData(RD, Record);
4789*0a6a1f1dSLionel Sambuc         Record.push_back(WriteDeclContextLexicalBlock(
4790*0a6a1f1dSLionel Sambuc             *Context, const_cast<CXXRecordDecl *>(RD)));
4791*0a6a1f1dSLionel Sambuc 
4792*0a6a1f1dSLionel Sambuc         // This state is sometimes updated by template instantiation, when we
4793*0a6a1f1dSLionel Sambuc         // switch from the specialization referring to the template declaration
4794*0a6a1f1dSLionel Sambuc         // to it referring to the template definition.
4795*0a6a1f1dSLionel Sambuc         if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4796*0a6a1f1dSLionel Sambuc           Record.push_back(MSInfo->getTemplateSpecializationKind());
4797*0a6a1f1dSLionel Sambuc           AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4798*0a6a1f1dSLionel Sambuc         } else {
4799*0a6a1f1dSLionel Sambuc           auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4800*0a6a1f1dSLionel Sambuc           Record.push_back(Spec->getTemplateSpecializationKind());
4801*0a6a1f1dSLionel Sambuc           AddSourceLocation(Spec->getPointOfInstantiation(), Record);
4802*0a6a1f1dSLionel Sambuc 
4803*0a6a1f1dSLionel Sambuc           // The instantiation might have been resolved to a partial
4804*0a6a1f1dSLionel Sambuc           // specialization. If so, record which one.
4805*0a6a1f1dSLionel Sambuc           auto From = Spec->getInstantiatedFrom();
4806*0a6a1f1dSLionel Sambuc           if (auto PartialSpec =
4807*0a6a1f1dSLionel Sambuc                 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4808*0a6a1f1dSLionel Sambuc             Record.push_back(true);
4809*0a6a1f1dSLionel Sambuc             AddDeclRef(PartialSpec, Record);
4810*0a6a1f1dSLionel Sambuc             AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4811*0a6a1f1dSLionel Sambuc                                     Record);
4812*0a6a1f1dSLionel Sambuc           } else {
4813*0a6a1f1dSLionel Sambuc             Record.push_back(false);
4814*0a6a1f1dSLionel Sambuc           }
4815*0a6a1f1dSLionel Sambuc         }
4816*0a6a1f1dSLionel Sambuc         Record.push_back(RD->getTagKind());
4817*0a6a1f1dSLionel Sambuc         AddSourceLocation(RD->getLocation(), Record);
4818*0a6a1f1dSLionel Sambuc         AddSourceLocation(RD->getLocStart(), Record);
4819*0a6a1f1dSLionel Sambuc         AddSourceLocation(RD->getRBraceLoc(), Record);
4820*0a6a1f1dSLionel Sambuc 
4821*0a6a1f1dSLionel Sambuc         // Instantiation may change attributes; write them all out afresh.
4822*0a6a1f1dSLionel Sambuc         Record.push_back(D->hasAttrs());
4823*0a6a1f1dSLionel Sambuc         if (Record.back())
4824*0a6a1f1dSLionel Sambuc           WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4825*0a6a1f1dSLionel Sambuc                                              D->getAttrs().size()), Record);
4826*0a6a1f1dSLionel Sambuc 
4827*0a6a1f1dSLionel Sambuc         // FIXME: Ensure we don't get here for explicit instantiations.
4828*0a6a1f1dSLionel Sambuc         break;
4829*0a6a1f1dSLionel Sambuc       }
4830*0a6a1f1dSLionel Sambuc 
4831*0a6a1f1dSLionel Sambuc       case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4832*0a6a1f1dSLionel Sambuc         addExceptionSpec(
4833*0a6a1f1dSLionel Sambuc             *this,
4834*0a6a1f1dSLionel Sambuc             cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4835*0a6a1f1dSLionel Sambuc             Record);
4836*0a6a1f1dSLionel Sambuc         break;
4837*0a6a1f1dSLionel Sambuc 
4838*0a6a1f1dSLionel Sambuc       case UPD_CXX_DEDUCED_RETURN_TYPE:
4839*0a6a1f1dSLionel Sambuc         Record.push_back(GetOrCreateTypeID(Update.getType()));
4840*0a6a1f1dSLionel Sambuc         break;
4841*0a6a1f1dSLionel Sambuc 
4842*0a6a1f1dSLionel Sambuc       case UPD_DECL_MARKED_USED:
4843*0a6a1f1dSLionel Sambuc         break;
4844*0a6a1f1dSLionel Sambuc 
4845*0a6a1f1dSLionel Sambuc       case UPD_MANGLING_NUMBER:
4846*0a6a1f1dSLionel Sambuc       case UPD_STATIC_LOCAL_NUMBER:
4847*0a6a1f1dSLionel Sambuc         Record.push_back(Update.getNumber());
4848*0a6a1f1dSLionel Sambuc         break;
4849*0a6a1f1dSLionel Sambuc       case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4850*0a6a1f1dSLionel Sambuc         AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4851*0a6a1f1dSLionel Sambuc                        Record);
4852*0a6a1f1dSLionel Sambuc         break;
4853*0a6a1f1dSLionel Sambuc       }
4854*0a6a1f1dSLionel Sambuc     }
4855*0a6a1f1dSLionel Sambuc 
4856*0a6a1f1dSLionel Sambuc     if (HasUpdatedBody) {
4857*0a6a1f1dSLionel Sambuc       const FunctionDecl *Def = cast<FunctionDecl>(D);
4858*0a6a1f1dSLionel Sambuc       Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
4859*0a6a1f1dSLionel Sambuc       Record.push_back(Def->isInlined());
4860*0a6a1f1dSLionel Sambuc       AddSourceLocation(Def->getInnerLocStart(), Record);
4861*0a6a1f1dSLionel Sambuc       AddFunctionDefinition(Def, Record);
4862*0a6a1f1dSLionel Sambuc       if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4863*0a6a1f1dSLionel Sambuc         Record.push_back(GetDeclRef(DD->getOperatorDelete()));
4864*0a6a1f1dSLionel Sambuc     }
4865f4a2713aSLionel Sambuc 
4866f4a2713aSLionel Sambuc     OffsetsRecord.push_back(GetDeclRef(D));
4867*0a6a1f1dSLionel Sambuc     OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4868*0a6a1f1dSLionel Sambuc 
4869*0a6a1f1dSLionel Sambuc     Stream.EmitRecord(DECL_UPDATES, Record);
4870*0a6a1f1dSLionel Sambuc 
4871*0a6a1f1dSLionel Sambuc     // Flush any statements that were written as part of this update record.
4872*0a6a1f1dSLionel Sambuc     FlushStmts();
4873*0a6a1f1dSLionel Sambuc 
4874*0a6a1f1dSLionel Sambuc     // Flush C++ base specifiers, if there are any.
4875*0a6a1f1dSLionel Sambuc     FlushCXXBaseSpecifiers();
4876f4a2713aSLionel Sambuc   }
4877f4a2713aSLionel Sambuc }
4878f4a2713aSLionel Sambuc 
WriteDeclReplacementsBlock()4879f4a2713aSLionel Sambuc void ASTWriter::WriteDeclReplacementsBlock() {
4880f4a2713aSLionel Sambuc   if (ReplacedDecls.empty())
4881f4a2713aSLionel Sambuc     return;
4882f4a2713aSLionel Sambuc 
4883f4a2713aSLionel Sambuc   RecordData Record;
4884f4a2713aSLionel Sambuc   for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4885f4a2713aSLionel Sambuc          I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
4886f4a2713aSLionel Sambuc     Record.push_back(I->ID);
4887f4a2713aSLionel Sambuc     Record.push_back(I->Offset);
4888f4a2713aSLionel Sambuc     Record.push_back(I->Loc);
4889f4a2713aSLionel Sambuc   }
4890f4a2713aSLionel Sambuc   Stream.EmitRecord(DECL_REPLACEMENTS, Record);
4891f4a2713aSLionel Sambuc }
4892f4a2713aSLionel Sambuc 
AddSourceLocation(SourceLocation Loc,RecordDataImpl & Record)4893f4a2713aSLionel Sambuc void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
4894f4a2713aSLionel Sambuc   Record.push_back(Loc.getRawEncoding());
4895f4a2713aSLionel Sambuc }
4896f4a2713aSLionel Sambuc 
AddSourceRange(SourceRange Range,RecordDataImpl & Record)4897f4a2713aSLionel Sambuc void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
4898f4a2713aSLionel Sambuc   AddSourceLocation(Range.getBegin(), Record);
4899f4a2713aSLionel Sambuc   AddSourceLocation(Range.getEnd(), Record);
4900f4a2713aSLionel Sambuc }
4901f4a2713aSLionel Sambuc 
AddAPInt(const llvm::APInt & Value,RecordDataImpl & Record)4902f4a2713aSLionel Sambuc void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
4903f4a2713aSLionel Sambuc   Record.push_back(Value.getBitWidth());
4904f4a2713aSLionel Sambuc   const uint64_t *Words = Value.getRawData();
4905f4a2713aSLionel Sambuc   Record.append(Words, Words + Value.getNumWords());
4906f4a2713aSLionel Sambuc }
4907f4a2713aSLionel Sambuc 
AddAPSInt(const llvm::APSInt & Value,RecordDataImpl & Record)4908f4a2713aSLionel Sambuc void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
4909f4a2713aSLionel Sambuc   Record.push_back(Value.isUnsigned());
4910f4a2713aSLionel Sambuc   AddAPInt(Value, Record);
4911f4a2713aSLionel Sambuc }
4912f4a2713aSLionel Sambuc 
AddAPFloat(const llvm::APFloat & Value,RecordDataImpl & Record)4913f4a2713aSLionel Sambuc void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
4914f4a2713aSLionel Sambuc   AddAPInt(Value.bitcastToAPInt(), Record);
4915f4a2713aSLionel Sambuc }
4916f4a2713aSLionel Sambuc 
AddIdentifierRef(const IdentifierInfo * II,RecordDataImpl & Record)4917f4a2713aSLionel Sambuc void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
4918f4a2713aSLionel Sambuc   Record.push_back(getIdentifierRef(II));
4919f4a2713aSLionel Sambuc }
4920f4a2713aSLionel Sambuc 
getIdentifierRef(const IdentifierInfo * II)4921f4a2713aSLionel Sambuc IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
4922*0a6a1f1dSLionel Sambuc   if (!II)
4923f4a2713aSLionel Sambuc     return 0;
4924f4a2713aSLionel Sambuc 
4925f4a2713aSLionel Sambuc   IdentID &ID = IdentifierIDs[II];
4926f4a2713aSLionel Sambuc   if (ID == 0)
4927f4a2713aSLionel Sambuc     ID = NextIdentID++;
4928f4a2713aSLionel Sambuc   return ID;
4929f4a2713aSLionel Sambuc }
4930f4a2713aSLionel Sambuc 
getMacroRef(MacroInfo * MI,const IdentifierInfo * Name)4931f4a2713aSLionel Sambuc MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
4932f4a2713aSLionel Sambuc   // Don't emit builtin macros like __LINE__ to the AST file unless they
4933f4a2713aSLionel Sambuc   // have been redefined by the header (in which case they are not
4934f4a2713aSLionel Sambuc   // isBuiltinMacro).
4935*0a6a1f1dSLionel Sambuc   if (!MI || MI->isBuiltinMacro())
4936f4a2713aSLionel Sambuc     return 0;
4937f4a2713aSLionel Sambuc 
4938f4a2713aSLionel Sambuc   MacroID &ID = MacroIDs[MI];
4939f4a2713aSLionel Sambuc   if (ID == 0) {
4940f4a2713aSLionel Sambuc     ID = NextMacroID++;
4941f4a2713aSLionel Sambuc     MacroInfoToEmitData Info = { Name, MI, ID };
4942f4a2713aSLionel Sambuc     MacroInfosToEmit.push_back(Info);
4943f4a2713aSLionel Sambuc   }
4944f4a2713aSLionel Sambuc   return ID;
4945f4a2713aSLionel Sambuc }
4946f4a2713aSLionel Sambuc 
getMacroID(MacroInfo * MI)4947f4a2713aSLionel Sambuc MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4948*0a6a1f1dSLionel Sambuc   if (!MI || MI->isBuiltinMacro())
4949f4a2713aSLionel Sambuc     return 0;
4950f4a2713aSLionel Sambuc 
4951f4a2713aSLionel Sambuc   assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4952f4a2713aSLionel Sambuc   return MacroIDs[MI];
4953f4a2713aSLionel Sambuc }
4954f4a2713aSLionel Sambuc 
getMacroDirectivesOffset(const IdentifierInfo * Name)4955f4a2713aSLionel Sambuc uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4956f4a2713aSLionel Sambuc   assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4957f4a2713aSLionel Sambuc   return IdentMacroDirectivesOffsetMap[Name];
4958f4a2713aSLionel Sambuc }
4959f4a2713aSLionel Sambuc 
AddSelectorRef(const Selector SelRef,RecordDataImpl & Record)4960f4a2713aSLionel Sambuc void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
4961f4a2713aSLionel Sambuc   Record.push_back(getSelectorRef(SelRef));
4962f4a2713aSLionel Sambuc }
4963f4a2713aSLionel Sambuc 
getSelectorRef(Selector Sel)4964f4a2713aSLionel Sambuc SelectorID ASTWriter::getSelectorRef(Selector Sel) {
4965*0a6a1f1dSLionel Sambuc   if (Sel.getAsOpaquePtr() == nullptr) {
4966f4a2713aSLionel Sambuc     return 0;
4967f4a2713aSLionel Sambuc   }
4968f4a2713aSLionel Sambuc 
4969f4a2713aSLionel Sambuc   SelectorID SID = SelectorIDs[Sel];
4970f4a2713aSLionel Sambuc   if (SID == 0 && Chain) {
4971f4a2713aSLionel Sambuc     // This might trigger a ReadSelector callback, which will set the ID for
4972f4a2713aSLionel Sambuc     // this selector.
4973f4a2713aSLionel Sambuc     Chain->LoadSelector(Sel);
4974f4a2713aSLionel Sambuc     SID = SelectorIDs[Sel];
4975f4a2713aSLionel Sambuc   }
4976f4a2713aSLionel Sambuc   if (SID == 0) {
4977f4a2713aSLionel Sambuc     SID = NextSelectorID++;
4978f4a2713aSLionel Sambuc     SelectorIDs[Sel] = SID;
4979f4a2713aSLionel Sambuc   }
4980f4a2713aSLionel Sambuc   return SID;
4981f4a2713aSLionel Sambuc }
4982f4a2713aSLionel Sambuc 
AddCXXTemporary(const CXXTemporary * Temp,RecordDataImpl & Record)4983f4a2713aSLionel Sambuc void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
4984f4a2713aSLionel Sambuc   AddDeclRef(Temp->getDestructor(), Record);
4985f4a2713aSLionel Sambuc }
4986f4a2713aSLionel Sambuc 
AddCXXBaseSpecifiersRef(CXXBaseSpecifier const * Bases,CXXBaseSpecifier const * BasesEnd,RecordDataImpl & Record)4987f4a2713aSLionel Sambuc void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4988f4a2713aSLionel Sambuc                                       CXXBaseSpecifier const *BasesEnd,
4989f4a2713aSLionel Sambuc                                         RecordDataImpl &Record) {
4990f4a2713aSLionel Sambuc   assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4991f4a2713aSLionel Sambuc   CXXBaseSpecifiersToWrite.push_back(
4992f4a2713aSLionel Sambuc                                 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4993f4a2713aSLionel Sambuc                                                         Bases, BasesEnd));
4994f4a2713aSLionel Sambuc   Record.push_back(NextCXXBaseSpecifiersID++);
4995f4a2713aSLionel Sambuc }
4996f4a2713aSLionel Sambuc 
AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,const TemplateArgumentLocInfo & Arg,RecordDataImpl & Record)4997f4a2713aSLionel Sambuc void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
4998f4a2713aSLionel Sambuc                                            const TemplateArgumentLocInfo &Arg,
4999f4a2713aSLionel Sambuc                                            RecordDataImpl &Record) {
5000f4a2713aSLionel Sambuc   switch (Kind) {
5001f4a2713aSLionel Sambuc   case TemplateArgument::Expression:
5002f4a2713aSLionel Sambuc     AddStmt(Arg.getAsExpr());
5003f4a2713aSLionel Sambuc     break;
5004f4a2713aSLionel Sambuc   case TemplateArgument::Type:
5005f4a2713aSLionel Sambuc     AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
5006f4a2713aSLionel Sambuc     break;
5007f4a2713aSLionel Sambuc   case TemplateArgument::Template:
5008f4a2713aSLionel Sambuc     AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
5009f4a2713aSLionel Sambuc     AddSourceLocation(Arg.getTemplateNameLoc(), Record);
5010f4a2713aSLionel Sambuc     break;
5011f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
5012f4a2713aSLionel Sambuc     AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
5013f4a2713aSLionel Sambuc     AddSourceLocation(Arg.getTemplateNameLoc(), Record);
5014f4a2713aSLionel Sambuc     AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
5015f4a2713aSLionel Sambuc     break;
5016f4a2713aSLionel Sambuc   case TemplateArgument::Null:
5017f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
5018f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
5019f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
5020f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
5021f4a2713aSLionel Sambuc     // FIXME: Is this right?
5022f4a2713aSLionel Sambuc     break;
5023f4a2713aSLionel Sambuc   }
5024f4a2713aSLionel Sambuc }
5025f4a2713aSLionel Sambuc 
AddTemplateArgumentLoc(const TemplateArgumentLoc & Arg,RecordDataImpl & Record)5026f4a2713aSLionel Sambuc void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
5027f4a2713aSLionel Sambuc                                        RecordDataImpl &Record) {
5028f4a2713aSLionel Sambuc   AddTemplateArgument(Arg.getArgument(), Record);
5029f4a2713aSLionel Sambuc 
5030f4a2713aSLionel Sambuc   if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5031f4a2713aSLionel Sambuc     bool InfoHasSameExpr
5032f4a2713aSLionel Sambuc       = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5033f4a2713aSLionel Sambuc     Record.push_back(InfoHasSameExpr);
5034f4a2713aSLionel Sambuc     if (InfoHasSameExpr)
5035f4a2713aSLionel Sambuc       return; // Avoid storing the same expr twice.
5036f4a2713aSLionel Sambuc   }
5037f4a2713aSLionel Sambuc   AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
5038f4a2713aSLionel Sambuc                              Record);
5039f4a2713aSLionel Sambuc }
5040f4a2713aSLionel Sambuc 
AddTypeSourceInfo(TypeSourceInfo * TInfo,RecordDataImpl & Record)5041f4a2713aSLionel Sambuc void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
5042f4a2713aSLionel Sambuc                                   RecordDataImpl &Record) {
5043*0a6a1f1dSLionel Sambuc   if (!TInfo) {
5044f4a2713aSLionel Sambuc     AddTypeRef(QualType(), Record);
5045f4a2713aSLionel Sambuc     return;
5046f4a2713aSLionel Sambuc   }
5047f4a2713aSLionel Sambuc 
5048f4a2713aSLionel Sambuc   AddTypeLoc(TInfo->getTypeLoc(), Record);
5049f4a2713aSLionel Sambuc }
5050f4a2713aSLionel Sambuc 
AddTypeLoc(TypeLoc TL,RecordDataImpl & Record)5051f4a2713aSLionel Sambuc void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
5052f4a2713aSLionel Sambuc   AddTypeRef(TL.getType(), Record);
5053f4a2713aSLionel Sambuc 
5054f4a2713aSLionel Sambuc   TypeLocWriter TLW(*this, Record);
5055f4a2713aSLionel Sambuc   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
5056f4a2713aSLionel Sambuc     TLW.Visit(TL);
5057f4a2713aSLionel Sambuc }
5058f4a2713aSLionel Sambuc 
AddTypeRef(QualType T,RecordDataImpl & Record)5059f4a2713aSLionel Sambuc void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
5060f4a2713aSLionel Sambuc   Record.push_back(GetOrCreateTypeID(T));
5061f4a2713aSLionel Sambuc }
5062f4a2713aSLionel Sambuc 
GetOrCreateTypeID(QualType T)5063f4a2713aSLionel Sambuc TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
5064f4a2713aSLionel Sambuc   assert(Context);
5065f4a2713aSLionel Sambuc   return MakeTypeID(*Context, T,
5066f4a2713aSLionel Sambuc               std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
5067f4a2713aSLionel Sambuc }
5068f4a2713aSLionel Sambuc 
getTypeID(QualType T) const5069f4a2713aSLionel Sambuc TypeID ASTWriter::getTypeID(QualType T) const {
5070f4a2713aSLionel Sambuc   assert(Context);
5071f4a2713aSLionel Sambuc   return MakeTypeID(*Context, T,
5072f4a2713aSLionel Sambuc               std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
5073f4a2713aSLionel Sambuc }
5074f4a2713aSLionel Sambuc 
GetOrCreateTypeIdx(QualType T)5075f4a2713aSLionel Sambuc TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5076f4a2713aSLionel Sambuc   if (T.isNull())
5077f4a2713aSLionel Sambuc     return TypeIdx();
5078f4a2713aSLionel Sambuc   assert(!T.getLocalFastQualifiers());
5079f4a2713aSLionel Sambuc 
5080f4a2713aSLionel Sambuc   TypeIdx &Idx = TypeIdxs[T];
5081f4a2713aSLionel Sambuc   if (Idx.getIndex() == 0) {
5082f4a2713aSLionel Sambuc     if (DoneWritingDeclsAndTypes) {
5083f4a2713aSLionel Sambuc       assert(0 && "New type seen after serializing all the types to emit!");
5084f4a2713aSLionel Sambuc       return TypeIdx();
5085f4a2713aSLionel Sambuc     }
5086f4a2713aSLionel Sambuc 
5087f4a2713aSLionel Sambuc     // We haven't seen this type before. Assign it a new ID and put it
5088f4a2713aSLionel Sambuc     // into the queue of types to emit.
5089f4a2713aSLionel Sambuc     Idx = TypeIdx(NextTypeID++);
5090f4a2713aSLionel Sambuc     DeclTypesToEmit.push(T);
5091f4a2713aSLionel Sambuc   }
5092f4a2713aSLionel Sambuc   return Idx;
5093f4a2713aSLionel Sambuc }
5094f4a2713aSLionel Sambuc 
getTypeIdx(QualType T) const5095f4a2713aSLionel Sambuc TypeIdx ASTWriter::getTypeIdx(QualType T) const {
5096f4a2713aSLionel Sambuc   if (T.isNull())
5097f4a2713aSLionel Sambuc     return TypeIdx();
5098f4a2713aSLionel Sambuc   assert(!T.getLocalFastQualifiers());
5099f4a2713aSLionel Sambuc 
5100f4a2713aSLionel Sambuc   TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5101f4a2713aSLionel Sambuc   assert(I != TypeIdxs.end() && "Type not emitted!");
5102f4a2713aSLionel Sambuc   return I->second;
5103f4a2713aSLionel Sambuc }
5104f4a2713aSLionel Sambuc 
AddDeclRef(const Decl * D,RecordDataImpl & Record)5105f4a2713aSLionel Sambuc void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
5106f4a2713aSLionel Sambuc   Record.push_back(GetDeclRef(D));
5107f4a2713aSLionel Sambuc }
5108f4a2713aSLionel Sambuc 
GetDeclRef(const Decl * D)5109f4a2713aSLionel Sambuc DeclID ASTWriter::GetDeclRef(const Decl *D) {
5110f4a2713aSLionel Sambuc   assert(WritingAST && "Cannot request a declaration ID before AST writing");
5111f4a2713aSLionel Sambuc 
5112*0a6a1f1dSLionel Sambuc   if (!D) {
5113f4a2713aSLionel Sambuc     return 0;
5114f4a2713aSLionel Sambuc   }
5115f4a2713aSLionel Sambuc 
5116f4a2713aSLionel Sambuc   // If D comes from an AST file, its declaration ID is already known and
5117f4a2713aSLionel Sambuc   // fixed.
5118f4a2713aSLionel Sambuc   if (D->isFromASTFile())
5119f4a2713aSLionel Sambuc     return D->getGlobalID();
5120f4a2713aSLionel Sambuc 
5121f4a2713aSLionel Sambuc   assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
5122f4a2713aSLionel Sambuc   DeclID &ID = DeclIDs[D];
5123f4a2713aSLionel Sambuc   if (ID == 0) {
5124f4a2713aSLionel Sambuc     if (DoneWritingDeclsAndTypes) {
5125f4a2713aSLionel Sambuc       assert(0 && "New decl seen after serializing all the decls to emit!");
5126f4a2713aSLionel Sambuc       return 0;
5127f4a2713aSLionel Sambuc     }
5128f4a2713aSLionel Sambuc 
5129f4a2713aSLionel Sambuc     // We haven't seen this declaration before. Give it a new ID and
5130f4a2713aSLionel Sambuc     // enqueue it in the list of declarations to emit.
5131f4a2713aSLionel Sambuc     ID = NextDeclID++;
5132f4a2713aSLionel Sambuc     DeclTypesToEmit.push(const_cast<Decl *>(D));
5133f4a2713aSLionel Sambuc   }
5134f4a2713aSLionel Sambuc 
5135f4a2713aSLionel Sambuc   return ID;
5136f4a2713aSLionel Sambuc }
5137f4a2713aSLionel Sambuc 
getDeclID(const Decl * D)5138f4a2713aSLionel Sambuc DeclID ASTWriter::getDeclID(const Decl *D) {
5139*0a6a1f1dSLionel Sambuc   if (!D)
5140f4a2713aSLionel Sambuc     return 0;
5141f4a2713aSLionel Sambuc 
5142f4a2713aSLionel Sambuc   // If D comes from an AST file, its declaration ID is already known and
5143f4a2713aSLionel Sambuc   // fixed.
5144f4a2713aSLionel Sambuc   if (D->isFromASTFile())
5145f4a2713aSLionel Sambuc     return D->getGlobalID();
5146f4a2713aSLionel Sambuc 
5147f4a2713aSLionel Sambuc   assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5148f4a2713aSLionel Sambuc   return DeclIDs[D];
5149f4a2713aSLionel Sambuc }
5150f4a2713aSLionel Sambuc 
associateDeclWithFile(const Decl * D,DeclID ID)5151f4a2713aSLionel Sambuc void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
5152f4a2713aSLionel Sambuc   assert(ID);
5153f4a2713aSLionel Sambuc   assert(D);
5154f4a2713aSLionel Sambuc 
5155f4a2713aSLionel Sambuc   SourceLocation Loc = D->getLocation();
5156f4a2713aSLionel Sambuc   if (Loc.isInvalid())
5157f4a2713aSLionel Sambuc     return;
5158f4a2713aSLionel Sambuc 
5159f4a2713aSLionel Sambuc   // We only keep track of the file-level declarations of each file.
5160f4a2713aSLionel Sambuc   if (!D->getLexicalDeclContext()->isFileContext())
5161f4a2713aSLionel Sambuc     return;
5162f4a2713aSLionel Sambuc   // FIXME: ParmVarDecls that are part of a function type of a parameter of
5163f4a2713aSLionel Sambuc   // a function/objc method, should not have TU as lexical context.
5164f4a2713aSLionel Sambuc   if (isa<ParmVarDecl>(D))
5165f4a2713aSLionel Sambuc     return;
5166f4a2713aSLionel Sambuc 
5167f4a2713aSLionel Sambuc   SourceManager &SM = Context->getSourceManager();
5168f4a2713aSLionel Sambuc   SourceLocation FileLoc = SM.getFileLoc(Loc);
5169f4a2713aSLionel Sambuc   assert(SM.isLocalSourceLocation(FileLoc));
5170f4a2713aSLionel Sambuc   FileID FID;
5171f4a2713aSLionel Sambuc   unsigned Offset;
5172*0a6a1f1dSLionel Sambuc   std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5173f4a2713aSLionel Sambuc   if (FID.isInvalid())
5174f4a2713aSLionel Sambuc     return;
5175f4a2713aSLionel Sambuc   assert(SM.getSLocEntry(FID).isFile());
5176f4a2713aSLionel Sambuc 
5177f4a2713aSLionel Sambuc   DeclIDInFileInfo *&Info = FileDeclIDs[FID];
5178f4a2713aSLionel Sambuc   if (!Info)
5179f4a2713aSLionel Sambuc     Info = new DeclIDInFileInfo();
5180f4a2713aSLionel Sambuc 
5181f4a2713aSLionel Sambuc   std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5182f4a2713aSLionel Sambuc   LocDeclIDsTy &Decls = Info->DeclIDs;
5183f4a2713aSLionel Sambuc 
5184f4a2713aSLionel Sambuc   if (Decls.empty() || Decls.back().first <= Offset) {
5185f4a2713aSLionel Sambuc     Decls.push_back(LocDecl);
5186f4a2713aSLionel Sambuc     return;
5187f4a2713aSLionel Sambuc   }
5188f4a2713aSLionel Sambuc 
5189f4a2713aSLionel Sambuc   LocDeclIDsTy::iterator I =
5190f4a2713aSLionel Sambuc       std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
5191f4a2713aSLionel Sambuc 
5192f4a2713aSLionel Sambuc   Decls.insert(I, LocDecl);
5193f4a2713aSLionel Sambuc }
5194f4a2713aSLionel Sambuc 
AddDeclarationName(DeclarationName Name,RecordDataImpl & Record)5195f4a2713aSLionel Sambuc void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
5196f4a2713aSLionel Sambuc   // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
5197f4a2713aSLionel Sambuc   Record.push_back(Name.getNameKind());
5198f4a2713aSLionel Sambuc   switch (Name.getNameKind()) {
5199f4a2713aSLionel Sambuc   case DeclarationName::Identifier:
5200f4a2713aSLionel Sambuc     AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5201f4a2713aSLionel Sambuc     break;
5202f4a2713aSLionel Sambuc 
5203f4a2713aSLionel Sambuc   case DeclarationName::ObjCZeroArgSelector:
5204f4a2713aSLionel Sambuc   case DeclarationName::ObjCOneArgSelector:
5205f4a2713aSLionel Sambuc   case DeclarationName::ObjCMultiArgSelector:
5206f4a2713aSLionel Sambuc     AddSelectorRef(Name.getObjCSelector(), Record);
5207f4a2713aSLionel Sambuc     break;
5208f4a2713aSLionel Sambuc 
5209f4a2713aSLionel Sambuc   case DeclarationName::CXXConstructorName:
5210f4a2713aSLionel Sambuc   case DeclarationName::CXXDestructorName:
5211f4a2713aSLionel Sambuc   case DeclarationName::CXXConversionFunctionName:
5212f4a2713aSLionel Sambuc     AddTypeRef(Name.getCXXNameType(), Record);
5213f4a2713aSLionel Sambuc     break;
5214f4a2713aSLionel Sambuc 
5215f4a2713aSLionel Sambuc   case DeclarationName::CXXOperatorName:
5216f4a2713aSLionel Sambuc     Record.push_back(Name.getCXXOverloadedOperator());
5217f4a2713aSLionel Sambuc     break;
5218f4a2713aSLionel Sambuc 
5219f4a2713aSLionel Sambuc   case DeclarationName::CXXLiteralOperatorName:
5220f4a2713aSLionel Sambuc     AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5221f4a2713aSLionel Sambuc     break;
5222f4a2713aSLionel Sambuc 
5223f4a2713aSLionel Sambuc   case DeclarationName::CXXUsingDirective:
5224f4a2713aSLionel Sambuc     // No extra data to emit
5225f4a2713aSLionel Sambuc     break;
5226f4a2713aSLionel Sambuc   }
5227f4a2713aSLionel Sambuc }
5228f4a2713aSLionel Sambuc 
getAnonymousDeclarationNumber(const NamedDecl * D)5229*0a6a1f1dSLionel Sambuc unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5230*0a6a1f1dSLionel Sambuc   assert(needsAnonymousDeclarationNumber(D) &&
5231*0a6a1f1dSLionel Sambuc          "expected an anonymous declaration");
5232*0a6a1f1dSLionel Sambuc 
5233*0a6a1f1dSLionel Sambuc   // Number the anonymous declarations within this context, if we've not
5234*0a6a1f1dSLionel Sambuc   // already done so.
5235*0a6a1f1dSLionel Sambuc   auto It = AnonymousDeclarationNumbers.find(D);
5236*0a6a1f1dSLionel Sambuc   if (It == AnonymousDeclarationNumbers.end()) {
5237*0a6a1f1dSLionel Sambuc     unsigned Index = 0;
5238*0a6a1f1dSLionel Sambuc     for (Decl *LexicalD : D->getLexicalDeclContext()->decls()) {
5239*0a6a1f1dSLionel Sambuc       auto *ND = dyn_cast<NamedDecl>(LexicalD);
5240*0a6a1f1dSLionel Sambuc       if (!ND || !needsAnonymousDeclarationNumber(ND))
5241*0a6a1f1dSLionel Sambuc         continue;
5242*0a6a1f1dSLionel Sambuc       AnonymousDeclarationNumbers[ND] = Index++;
5243*0a6a1f1dSLionel Sambuc     }
5244*0a6a1f1dSLionel Sambuc 
5245*0a6a1f1dSLionel Sambuc     It = AnonymousDeclarationNumbers.find(D);
5246*0a6a1f1dSLionel Sambuc     assert(It != AnonymousDeclarationNumbers.end() &&
5247*0a6a1f1dSLionel Sambuc            "declaration not found within its lexical context");
5248*0a6a1f1dSLionel Sambuc   }
5249*0a6a1f1dSLionel Sambuc 
5250*0a6a1f1dSLionel Sambuc   return It->second;
5251*0a6a1f1dSLionel Sambuc }
5252*0a6a1f1dSLionel Sambuc 
AddDeclarationNameLoc(const DeclarationNameLoc & DNLoc,DeclarationName Name,RecordDataImpl & Record)5253f4a2713aSLionel Sambuc void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5254f4a2713aSLionel Sambuc                                      DeclarationName Name, RecordDataImpl &Record) {
5255f4a2713aSLionel Sambuc   switch (Name.getNameKind()) {
5256f4a2713aSLionel Sambuc   case DeclarationName::CXXConstructorName:
5257f4a2713aSLionel Sambuc   case DeclarationName::CXXDestructorName:
5258f4a2713aSLionel Sambuc   case DeclarationName::CXXConversionFunctionName:
5259f4a2713aSLionel Sambuc     AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5260f4a2713aSLionel Sambuc     break;
5261f4a2713aSLionel Sambuc 
5262f4a2713aSLionel Sambuc   case DeclarationName::CXXOperatorName:
5263f4a2713aSLionel Sambuc     AddSourceLocation(
5264f4a2713aSLionel Sambuc        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5265f4a2713aSLionel Sambuc        Record);
5266f4a2713aSLionel Sambuc     AddSourceLocation(
5267f4a2713aSLionel Sambuc         SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5268f4a2713aSLionel Sambuc         Record);
5269f4a2713aSLionel Sambuc     break;
5270f4a2713aSLionel Sambuc 
5271f4a2713aSLionel Sambuc   case DeclarationName::CXXLiteralOperatorName:
5272f4a2713aSLionel Sambuc     AddSourceLocation(
5273f4a2713aSLionel Sambuc      SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5274f4a2713aSLionel Sambuc      Record);
5275f4a2713aSLionel Sambuc     break;
5276f4a2713aSLionel Sambuc 
5277f4a2713aSLionel Sambuc   case DeclarationName::Identifier:
5278f4a2713aSLionel Sambuc   case DeclarationName::ObjCZeroArgSelector:
5279f4a2713aSLionel Sambuc   case DeclarationName::ObjCOneArgSelector:
5280f4a2713aSLionel Sambuc   case DeclarationName::ObjCMultiArgSelector:
5281f4a2713aSLionel Sambuc   case DeclarationName::CXXUsingDirective:
5282f4a2713aSLionel Sambuc     break;
5283f4a2713aSLionel Sambuc   }
5284f4a2713aSLionel Sambuc }
5285f4a2713aSLionel Sambuc 
AddDeclarationNameInfo(const DeclarationNameInfo & NameInfo,RecordDataImpl & Record)5286f4a2713aSLionel Sambuc void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
5287f4a2713aSLionel Sambuc                                        RecordDataImpl &Record) {
5288f4a2713aSLionel Sambuc   AddDeclarationName(NameInfo.getName(), Record);
5289f4a2713aSLionel Sambuc   AddSourceLocation(NameInfo.getLoc(), Record);
5290f4a2713aSLionel Sambuc   AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5291f4a2713aSLionel Sambuc }
5292f4a2713aSLionel Sambuc 
AddQualifierInfo(const QualifierInfo & Info,RecordDataImpl & Record)5293f4a2713aSLionel Sambuc void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
5294f4a2713aSLionel Sambuc                                  RecordDataImpl &Record) {
5295f4a2713aSLionel Sambuc   AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
5296f4a2713aSLionel Sambuc   Record.push_back(Info.NumTemplParamLists);
5297f4a2713aSLionel Sambuc   for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5298f4a2713aSLionel Sambuc     AddTemplateParameterList(Info.TemplParamLists[i], Record);
5299f4a2713aSLionel Sambuc }
5300f4a2713aSLionel Sambuc 
AddNestedNameSpecifier(NestedNameSpecifier * NNS,RecordDataImpl & Record)5301f4a2713aSLionel Sambuc void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
5302f4a2713aSLionel Sambuc                                        RecordDataImpl &Record) {
5303f4a2713aSLionel Sambuc   // Nested name specifiers usually aren't too long. I think that 8 would
5304f4a2713aSLionel Sambuc   // typically accommodate the vast majority.
5305f4a2713aSLionel Sambuc   SmallVector<NestedNameSpecifier *, 8> NestedNames;
5306f4a2713aSLionel Sambuc 
5307f4a2713aSLionel Sambuc   // Push each of the NNS's onto a stack for serialization in reverse order.
5308f4a2713aSLionel Sambuc   while (NNS) {
5309f4a2713aSLionel Sambuc     NestedNames.push_back(NNS);
5310f4a2713aSLionel Sambuc     NNS = NNS->getPrefix();
5311f4a2713aSLionel Sambuc   }
5312f4a2713aSLionel Sambuc 
5313f4a2713aSLionel Sambuc   Record.push_back(NestedNames.size());
5314f4a2713aSLionel Sambuc   while(!NestedNames.empty()) {
5315f4a2713aSLionel Sambuc     NNS = NestedNames.pop_back_val();
5316f4a2713aSLionel Sambuc     NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5317f4a2713aSLionel Sambuc     Record.push_back(Kind);
5318f4a2713aSLionel Sambuc     switch (Kind) {
5319f4a2713aSLionel Sambuc     case NestedNameSpecifier::Identifier:
5320f4a2713aSLionel Sambuc       AddIdentifierRef(NNS->getAsIdentifier(), Record);
5321f4a2713aSLionel Sambuc       break;
5322f4a2713aSLionel Sambuc 
5323f4a2713aSLionel Sambuc     case NestedNameSpecifier::Namespace:
5324f4a2713aSLionel Sambuc       AddDeclRef(NNS->getAsNamespace(), Record);
5325f4a2713aSLionel Sambuc       break;
5326f4a2713aSLionel Sambuc 
5327f4a2713aSLionel Sambuc     case NestedNameSpecifier::NamespaceAlias:
5328f4a2713aSLionel Sambuc       AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5329f4a2713aSLionel Sambuc       break;
5330f4a2713aSLionel Sambuc 
5331f4a2713aSLionel Sambuc     case NestedNameSpecifier::TypeSpec:
5332f4a2713aSLionel Sambuc     case NestedNameSpecifier::TypeSpecWithTemplate:
5333f4a2713aSLionel Sambuc       AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5334f4a2713aSLionel Sambuc       Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5335f4a2713aSLionel Sambuc       break;
5336f4a2713aSLionel Sambuc 
5337f4a2713aSLionel Sambuc     case NestedNameSpecifier::Global:
5338f4a2713aSLionel Sambuc       // Don't need to write an associated value.
5339f4a2713aSLionel Sambuc       break;
5340*0a6a1f1dSLionel Sambuc 
5341*0a6a1f1dSLionel Sambuc     case NestedNameSpecifier::Super:
5342*0a6a1f1dSLionel Sambuc       AddDeclRef(NNS->getAsRecordDecl(), Record);
5343*0a6a1f1dSLionel Sambuc       break;
5344f4a2713aSLionel Sambuc     }
5345f4a2713aSLionel Sambuc   }
5346f4a2713aSLionel Sambuc }
5347f4a2713aSLionel Sambuc 
AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,RecordDataImpl & Record)5348f4a2713aSLionel Sambuc void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5349f4a2713aSLionel Sambuc                                           RecordDataImpl &Record) {
5350f4a2713aSLionel Sambuc   // Nested name specifiers usually aren't too long. I think that 8 would
5351f4a2713aSLionel Sambuc   // typically accommodate the vast majority.
5352f4a2713aSLionel Sambuc   SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5353f4a2713aSLionel Sambuc 
5354f4a2713aSLionel Sambuc   // Push each of the nested-name-specifiers's onto a stack for
5355f4a2713aSLionel Sambuc   // serialization in reverse order.
5356f4a2713aSLionel Sambuc   while (NNS) {
5357f4a2713aSLionel Sambuc     NestedNames.push_back(NNS);
5358f4a2713aSLionel Sambuc     NNS = NNS.getPrefix();
5359f4a2713aSLionel Sambuc   }
5360f4a2713aSLionel Sambuc 
5361f4a2713aSLionel Sambuc   Record.push_back(NestedNames.size());
5362f4a2713aSLionel Sambuc   while(!NestedNames.empty()) {
5363f4a2713aSLionel Sambuc     NNS = NestedNames.pop_back_val();
5364f4a2713aSLionel Sambuc     NestedNameSpecifier::SpecifierKind Kind
5365f4a2713aSLionel Sambuc       = NNS.getNestedNameSpecifier()->getKind();
5366f4a2713aSLionel Sambuc     Record.push_back(Kind);
5367f4a2713aSLionel Sambuc     switch (Kind) {
5368f4a2713aSLionel Sambuc     case NestedNameSpecifier::Identifier:
5369f4a2713aSLionel Sambuc       AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5370f4a2713aSLionel Sambuc       AddSourceRange(NNS.getLocalSourceRange(), Record);
5371f4a2713aSLionel Sambuc       break;
5372f4a2713aSLionel Sambuc 
5373f4a2713aSLionel Sambuc     case NestedNameSpecifier::Namespace:
5374f4a2713aSLionel Sambuc       AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5375f4a2713aSLionel Sambuc       AddSourceRange(NNS.getLocalSourceRange(), Record);
5376f4a2713aSLionel Sambuc       break;
5377f4a2713aSLionel Sambuc 
5378f4a2713aSLionel Sambuc     case NestedNameSpecifier::NamespaceAlias:
5379f4a2713aSLionel Sambuc       AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5380f4a2713aSLionel Sambuc       AddSourceRange(NNS.getLocalSourceRange(), Record);
5381f4a2713aSLionel Sambuc       break;
5382f4a2713aSLionel Sambuc 
5383f4a2713aSLionel Sambuc     case NestedNameSpecifier::TypeSpec:
5384f4a2713aSLionel Sambuc     case NestedNameSpecifier::TypeSpecWithTemplate:
5385f4a2713aSLionel Sambuc       Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5386f4a2713aSLionel Sambuc       AddTypeLoc(NNS.getTypeLoc(), Record);
5387f4a2713aSLionel Sambuc       AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5388f4a2713aSLionel Sambuc       break;
5389f4a2713aSLionel Sambuc 
5390f4a2713aSLionel Sambuc     case NestedNameSpecifier::Global:
5391f4a2713aSLionel Sambuc       AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5392f4a2713aSLionel Sambuc       break;
5393*0a6a1f1dSLionel Sambuc 
5394*0a6a1f1dSLionel Sambuc     case NestedNameSpecifier::Super:
5395*0a6a1f1dSLionel Sambuc       AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5396*0a6a1f1dSLionel Sambuc       AddSourceRange(NNS.getLocalSourceRange(), Record);
5397*0a6a1f1dSLionel Sambuc       break;
5398f4a2713aSLionel Sambuc     }
5399f4a2713aSLionel Sambuc   }
5400f4a2713aSLionel Sambuc }
5401f4a2713aSLionel Sambuc 
AddTemplateName(TemplateName Name,RecordDataImpl & Record)5402f4a2713aSLionel Sambuc void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
5403f4a2713aSLionel Sambuc   TemplateName::NameKind Kind = Name.getKind();
5404f4a2713aSLionel Sambuc   Record.push_back(Kind);
5405f4a2713aSLionel Sambuc   switch (Kind) {
5406f4a2713aSLionel Sambuc   case TemplateName::Template:
5407f4a2713aSLionel Sambuc     AddDeclRef(Name.getAsTemplateDecl(), Record);
5408f4a2713aSLionel Sambuc     break;
5409f4a2713aSLionel Sambuc 
5410f4a2713aSLionel Sambuc   case TemplateName::OverloadedTemplate: {
5411f4a2713aSLionel Sambuc     OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5412f4a2713aSLionel Sambuc     Record.push_back(OvT->size());
5413f4a2713aSLionel Sambuc     for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5414f4a2713aSLionel Sambuc            I != E; ++I)
5415f4a2713aSLionel Sambuc       AddDeclRef(*I, Record);
5416f4a2713aSLionel Sambuc     break;
5417f4a2713aSLionel Sambuc   }
5418f4a2713aSLionel Sambuc 
5419f4a2713aSLionel Sambuc   case TemplateName::QualifiedTemplate: {
5420f4a2713aSLionel Sambuc     QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5421f4a2713aSLionel Sambuc     AddNestedNameSpecifier(QualT->getQualifier(), Record);
5422f4a2713aSLionel Sambuc     Record.push_back(QualT->hasTemplateKeyword());
5423f4a2713aSLionel Sambuc     AddDeclRef(QualT->getTemplateDecl(), Record);
5424f4a2713aSLionel Sambuc     break;
5425f4a2713aSLionel Sambuc   }
5426f4a2713aSLionel Sambuc 
5427f4a2713aSLionel Sambuc   case TemplateName::DependentTemplate: {
5428f4a2713aSLionel Sambuc     DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5429f4a2713aSLionel Sambuc     AddNestedNameSpecifier(DepT->getQualifier(), Record);
5430f4a2713aSLionel Sambuc     Record.push_back(DepT->isIdentifier());
5431f4a2713aSLionel Sambuc     if (DepT->isIdentifier())
5432f4a2713aSLionel Sambuc       AddIdentifierRef(DepT->getIdentifier(), Record);
5433f4a2713aSLionel Sambuc     else
5434f4a2713aSLionel Sambuc       Record.push_back(DepT->getOperator());
5435f4a2713aSLionel Sambuc     break;
5436f4a2713aSLionel Sambuc   }
5437f4a2713aSLionel Sambuc 
5438f4a2713aSLionel Sambuc   case TemplateName::SubstTemplateTemplateParm: {
5439f4a2713aSLionel Sambuc     SubstTemplateTemplateParmStorage *subst
5440f4a2713aSLionel Sambuc       = Name.getAsSubstTemplateTemplateParm();
5441f4a2713aSLionel Sambuc     AddDeclRef(subst->getParameter(), Record);
5442f4a2713aSLionel Sambuc     AddTemplateName(subst->getReplacement(), Record);
5443f4a2713aSLionel Sambuc     break;
5444f4a2713aSLionel Sambuc   }
5445f4a2713aSLionel Sambuc 
5446f4a2713aSLionel Sambuc   case TemplateName::SubstTemplateTemplateParmPack: {
5447f4a2713aSLionel Sambuc     SubstTemplateTemplateParmPackStorage *SubstPack
5448f4a2713aSLionel Sambuc       = Name.getAsSubstTemplateTemplateParmPack();
5449f4a2713aSLionel Sambuc     AddDeclRef(SubstPack->getParameterPack(), Record);
5450f4a2713aSLionel Sambuc     AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5451f4a2713aSLionel Sambuc     break;
5452f4a2713aSLionel Sambuc   }
5453f4a2713aSLionel Sambuc   }
5454f4a2713aSLionel Sambuc }
5455f4a2713aSLionel Sambuc 
AddTemplateArgument(const TemplateArgument & Arg,RecordDataImpl & Record)5456f4a2713aSLionel Sambuc void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
5457f4a2713aSLionel Sambuc                                     RecordDataImpl &Record) {
5458f4a2713aSLionel Sambuc   Record.push_back(Arg.getKind());
5459f4a2713aSLionel Sambuc   switch (Arg.getKind()) {
5460f4a2713aSLionel Sambuc   case TemplateArgument::Null:
5461f4a2713aSLionel Sambuc     break;
5462f4a2713aSLionel Sambuc   case TemplateArgument::Type:
5463f4a2713aSLionel Sambuc     AddTypeRef(Arg.getAsType(), Record);
5464f4a2713aSLionel Sambuc     break;
5465f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
5466f4a2713aSLionel Sambuc     AddDeclRef(Arg.getAsDecl(), Record);
5467*0a6a1f1dSLionel Sambuc     AddTypeRef(Arg.getParamTypeForDecl(), Record);
5468f4a2713aSLionel Sambuc     break;
5469f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
5470f4a2713aSLionel Sambuc     AddTypeRef(Arg.getNullPtrType(), Record);
5471f4a2713aSLionel Sambuc     break;
5472f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
5473f4a2713aSLionel Sambuc     AddAPSInt(Arg.getAsIntegral(), Record);
5474f4a2713aSLionel Sambuc     AddTypeRef(Arg.getIntegralType(), Record);
5475f4a2713aSLionel Sambuc     break;
5476f4a2713aSLionel Sambuc   case TemplateArgument::Template:
5477f4a2713aSLionel Sambuc     AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5478f4a2713aSLionel Sambuc     break;
5479f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
5480f4a2713aSLionel Sambuc     AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5481f4a2713aSLionel Sambuc     if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
5482f4a2713aSLionel Sambuc       Record.push_back(*NumExpansions + 1);
5483f4a2713aSLionel Sambuc     else
5484f4a2713aSLionel Sambuc       Record.push_back(0);
5485f4a2713aSLionel Sambuc     break;
5486f4a2713aSLionel Sambuc   case TemplateArgument::Expression:
5487f4a2713aSLionel Sambuc     AddStmt(Arg.getAsExpr());
5488f4a2713aSLionel Sambuc     break;
5489f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
5490f4a2713aSLionel Sambuc     Record.push_back(Arg.pack_size());
5491*0a6a1f1dSLionel Sambuc     for (const auto &P : Arg.pack_elements())
5492*0a6a1f1dSLionel Sambuc       AddTemplateArgument(P, Record);
5493f4a2713aSLionel Sambuc     break;
5494f4a2713aSLionel Sambuc   }
5495f4a2713aSLionel Sambuc }
5496f4a2713aSLionel Sambuc 
5497f4a2713aSLionel Sambuc void
AddTemplateParameterList(const TemplateParameterList * TemplateParams,RecordDataImpl & Record)5498f4a2713aSLionel Sambuc ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
5499f4a2713aSLionel Sambuc                                     RecordDataImpl &Record) {
5500f4a2713aSLionel Sambuc   assert(TemplateParams && "No TemplateParams!");
5501f4a2713aSLionel Sambuc   AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5502f4a2713aSLionel Sambuc   AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5503f4a2713aSLionel Sambuc   AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5504f4a2713aSLionel Sambuc   Record.push_back(TemplateParams->size());
5505f4a2713aSLionel Sambuc   for (TemplateParameterList::const_iterator
5506f4a2713aSLionel Sambuc          P = TemplateParams->begin(), PEnd = TemplateParams->end();
5507f4a2713aSLionel Sambuc          P != PEnd; ++P)
5508f4a2713aSLionel Sambuc     AddDeclRef(*P, Record);
5509f4a2713aSLionel Sambuc }
5510f4a2713aSLionel Sambuc 
5511f4a2713aSLionel Sambuc /// \brief Emit a template argument list.
5512f4a2713aSLionel Sambuc void
AddTemplateArgumentList(const TemplateArgumentList * TemplateArgs,RecordDataImpl & Record)5513f4a2713aSLionel Sambuc ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
5514f4a2713aSLionel Sambuc                                    RecordDataImpl &Record) {
5515f4a2713aSLionel Sambuc   assert(TemplateArgs && "No TemplateArgs!");
5516f4a2713aSLionel Sambuc   Record.push_back(TemplateArgs->size());
5517f4a2713aSLionel Sambuc   for (int i=0, e = TemplateArgs->size(); i != e; ++i)
5518f4a2713aSLionel Sambuc     AddTemplateArgument(TemplateArgs->get(i), Record);
5519f4a2713aSLionel Sambuc }
5520f4a2713aSLionel Sambuc 
5521f4a2713aSLionel Sambuc void
AddASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo * ASTTemplArgList,RecordDataImpl & Record)5522f4a2713aSLionel Sambuc ASTWriter::AddASTTemplateArgumentListInfo
5523f4a2713aSLionel Sambuc (const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5524f4a2713aSLionel Sambuc   assert(ASTTemplArgList && "No ASTTemplArgList!");
5525f4a2713aSLionel Sambuc   AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5526f4a2713aSLionel Sambuc   AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5527f4a2713aSLionel Sambuc   Record.push_back(ASTTemplArgList->NumTemplateArgs);
5528f4a2713aSLionel Sambuc   const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5529f4a2713aSLionel Sambuc   for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5530f4a2713aSLionel Sambuc     AddTemplateArgumentLoc(TemplArgs[i], Record);
5531f4a2713aSLionel Sambuc }
5532f4a2713aSLionel Sambuc 
5533f4a2713aSLionel Sambuc void
AddUnresolvedSet(const ASTUnresolvedSet & Set,RecordDataImpl & Record)5534f4a2713aSLionel Sambuc ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
5535f4a2713aSLionel Sambuc   Record.push_back(Set.size());
5536f4a2713aSLionel Sambuc   for (ASTUnresolvedSet::const_iterator
5537f4a2713aSLionel Sambuc          I = Set.begin(), E = Set.end(); I != E; ++I) {
5538f4a2713aSLionel Sambuc     AddDeclRef(I.getDecl(), Record);
5539f4a2713aSLionel Sambuc     Record.push_back(I.getAccess());
5540f4a2713aSLionel Sambuc   }
5541f4a2713aSLionel Sambuc }
5542f4a2713aSLionel Sambuc 
AddCXXBaseSpecifier(const CXXBaseSpecifier & Base,RecordDataImpl & Record)5543f4a2713aSLionel Sambuc void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
5544f4a2713aSLionel Sambuc                                     RecordDataImpl &Record) {
5545f4a2713aSLionel Sambuc   Record.push_back(Base.isVirtual());
5546f4a2713aSLionel Sambuc   Record.push_back(Base.isBaseOfClass());
5547f4a2713aSLionel Sambuc   Record.push_back(Base.getAccessSpecifierAsWritten());
5548f4a2713aSLionel Sambuc   Record.push_back(Base.getInheritConstructors());
5549f4a2713aSLionel Sambuc   AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
5550f4a2713aSLionel Sambuc   AddSourceRange(Base.getSourceRange(), Record);
5551f4a2713aSLionel Sambuc   AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5552f4a2713aSLionel Sambuc                                           : SourceLocation(),
5553f4a2713aSLionel Sambuc                     Record);
5554f4a2713aSLionel Sambuc }
5555f4a2713aSLionel Sambuc 
FlushCXXBaseSpecifiers()5556f4a2713aSLionel Sambuc void ASTWriter::FlushCXXBaseSpecifiers() {
5557f4a2713aSLionel Sambuc   RecordData Record;
5558f4a2713aSLionel Sambuc   for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5559f4a2713aSLionel Sambuc     Record.clear();
5560f4a2713aSLionel Sambuc 
5561f4a2713aSLionel Sambuc     // Record the offset of this base-specifier set.
5562f4a2713aSLionel Sambuc     unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
5563f4a2713aSLionel Sambuc     if (Index == CXXBaseSpecifiersOffsets.size())
5564f4a2713aSLionel Sambuc       CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5565f4a2713aSLionel Sambuc     else {
5566f4a2713aSLionel Sambuc       if (Index > CXXBaseSpecifiersOffsets.size())
5567f4a2713aSLionel Sambuc         CXXBaseSpecifiersOffsets.resize(Index + 1);
5568f4a2713aSLionel Sambuc       CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5569f4a2713aSLionel Sambuc     }
5570f4a2713aSLionel Sambuc 
5571f4a2713aSLionel Sambuc     const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5572f4a2713aSLionel Sambuc                         *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5573f4a2713aSLionel Sambuc     Record.push_back(BEnd - B);
5574f4a2713aSLionel Sambuc     for (; B != BEnd; ++B)
5575f4a2713aSLionel Sambuc       AddCXXBaseSpecifier(*B, Record);
5576f4a2713aSLionel Sambuc     Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
5577f4a2713aSLionel Sambuc 
5578f4a2713aSLionel Sambuc     // Flush any expressions that were written as part of the base specifiers.
5579f4a2713aSLionel Sambuc     FlushStmts();
5580f4a2713aSLionel Sambuc   }
5581f4a2713aSLionel Sambuc 
5582f4a2713aSLionel Sambuc   CXXBaseSpecifiersToWrite.clear();
5583f4a2713aSLionel Sambuc }
5584f4a2713aSLionel Sambuc 
AddCXXCtorInitializers(const CXXCtorInitializer * const * CtorInitializers,unsigned NumCtorInitializers,RecordDataImpl & Record)5585f4a2713aSLionel Sambuc void ASTWriter::AddCXXCtorInitializers(
5586f4a2713aSLionel Sambuc                              const CXXCtorInitializer * const *CtorInitializers,
5587f4a2713aSLionel Sambuc                              unsigned NumCtorInitializers,
5588f4a2713aSLionel Sambuc                              RecordDataImpl &Record) {
5589f4a2713aSLionel Sambuc   Record.push_back(NumCtorInitializers);
5590f4a2713aSLionel Sambuc   for (unsigned i=0; i != NumCtorInitializers; ++i) {
5591f4a2713aSLionel Sambuc     const CXXCtorInitializer *Init = CtorInitializers[i];
5592f4a2713aSLionel Sambuc 
5593f4a2713aSLionel Sambuc     if (Init->isBaseInitializer()) {
5594f4a2713aSLionel Sambuc       Record.push_back(CTOR_INITIALIZER_BASE);
5595f4a2713aSLionel Sambuc       AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
5596f4a2713aSLionel Sambuc       Record.push_back(Init->isBaseVirtual());
5597f4a2713aSLionel Sambuc     } else if (Init->isDelegatingInitializer()) {
5598f4a2713aSLionel Sambuc       Record.push_back(CTOR_INITIALIZER_DELEGATING);
5599f4a2713aSLionel Sambuc       AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
5600f4a2713aSLionel Sambuc     } else if (Init->isMemberInitializer()){
5601f4a2713aSLionel Sambuc       Record.push_back(CTOR_INITIALIZER_MEMBER);
5602f4a2713aSLionel Sambuc       AddDeclRef(Init->getMember(), Record);
5603f4a2713aSLionel Sambuc     } else {
5604f4a2713aSLionel Sambuc       Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5605f4a2713aSLionel Sambuc       AddDeclRef(Init->getIndirectMember(), Record);
5606f4a2713aSLionel Sambuc     }
5607f4a2713aSLionel Sambuc 
5608f4a2713aSLionel Sambuc     AddSourceLocation(Init->getMemberLocation(), Record);
5609f4a2713aSLionel Sambuc     AddStmt(Init->getInit());
5610f4a2713aSLionel Sambuc     AddSourceLocation(Init->getLParenLoc(), Record);
5611f4a2713aSLionel Sambuc     AddSourceLocation(Init->getRParenLoc(), Record);
5612f4a2713aSLionel Sambuc     Record.push_back(Init->isWritten());
5613f4a2713aSLionel Sambuc     if (Init->isWritten()) {
5614f4a2713aSLionel Sambuc       Record.push_back(Init->getSourceOrder());
5615f4a2713aSLionel Sambuc     } else {
5616f4a2713aSLionel Sambuc       Record.push_back(Init->getNumArrayIndices());
5617f4a2713aSLionel Sambuc       for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5618f4a2713aSLionel Sambuc         AddDeclRef(Init->getArrayIndex(i), Record);
5619f4a2713aSLionel Sambuc     }
5620f4a2713aSLionel Sambuc   }
5621f4a2713aSLionel Sambuc }
5622f4a2713aSLionel Sambuc 
AddCXXDefinitionData(const CXXRecordDecl * D,RecordDataImpl & Record)5623f4a2713aSLionel Sambuc void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5624*0a6a1f1dSLionel Sambuc   auto &Data = D->data();
5625f4a2713aSLionel Sambuc   Record.push_back(Data.IsLambda);
5626f4a2713aSLionel Sambuc   Record.push_back(Data.UserDeclaredConstructor);
5627f4a2713aSLionel Sambuc   Record.push_back(Data.UserDeclaredSpecialMembers);
5628f4a2713aSLionel Sambuc   Record.push_back(Data.Aggregate);
5629f4a2713aSLionel Sambuc   Record.push_back(Data.PlainOldData);
5630f4a2713aSLionel Sambuc   Record.push_back(Data.Empty);
5631f4a2713aSLionel Sambuc   Record.push_back(Data.Polymorphic);
5632f4a2713aSLionel Sambuc   Record.push_back(Data.Abstract);
5633f4a2713aSLionel Sambuc   Record.push_back(Data.IsStandardLayout);
5634f4a2713aSLionel Sambuc   Record.push_back(Data.HasNoNonEmptyBases);
5635f4a2713aSLionel Sambuc   Record.push_back(Data.HasPrivateFields);
5636f4a2713aSLionel Sambuc   Record.push_back(Data.HasProtectedFields);
5637f4a2713aSLionel Sambuc   Record.push_back(Data.HasPublicFields);
5638f4a2713aSLionel Sambuc   Record.push_back(Data.HasMutableFields);
5639*0a6a1f1dSLionel Sambuc   Record.push_back(Data.HasVariantMembers);
5640f4a2713aSLionel Sambuc   Record.push_back(Data.HasOnlyCMembers);
5641f4a2713aSLionel Sambuc   Record.push_back(Data.HasInClassInitializer);
5642f4a2713aSLionel Sambuc   Record.push_back(Data.HasUninitializedReferenceMember);
5643f4a2713aSLionel Sambuc   Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5644f4a2713aSLionel Sambuc   Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5645f4a2713aSLionel Sambuc   Record.push_back(Data.NeedOverloadResolutionForDestructor);
5646f4a2713aSLionel Sambuc   Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5647f4a2713aSLionel Sambuc   Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5648f4a2713aSLionel Sambuc   Record.push_back(Data.DefaultedDestructorIsDeleted);
5649f4a2713aSLionel Sambuc   Record.push_back(Data.HasTrivialSpecialMembers);
5650*0a6a1f1dSLionel Sambuc   Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
5651f4a2713aSLionel Sambuc   Record.push_back(Data.HasIrrelevantDestructor);
5652f4a2713aSLionel Sambuc   Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
5653f4a2713aSLionel Sambuc   Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5654f4a2713aSLionel Sambuc   Record.push_back(Data.HasConstexprDefaultConstructor);
5655f4a2713aSLionel Sambuc   Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
5656f4a2713aSLionel Sambuc   Record.push_back(Data.ComputedVisibleConversions);
5657f4a2713aSLionel Sambuc   Record.push_back(Data.UserProvidedDefaultConstructor);
5658f4a2713aSLionel Sambuc   Record.push_back(Data.DeclaredSpecialMembers);
5659f4a2713aSLionel Sambuc   Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5660f4a2713aSLionel Sambuc   Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5661f4a2713aSLionel Sambuc   Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5662f4a2713aSLionel Sambuc   Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
5663f4a2713aSLionel Sambuc   // IsLambda bit is already saved.
5664f4a2713aSLionel Sambuc 
5665f4a2713aSLionel Sambuc   Record.push_back(Data.NumBases);
5666f4a2713aSLionel Sambuc   if (Data.NumBases > 0)
5667f4a2713aSLionel Sambuc     AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5668f4a2713aSLionel Sambuc                             Record);
5669f4a2713aSLionel Sambuc 
5670f4a2713aSLionel Sambuc   // FIXME: Make VBases lazily computed when needed to avoid storing them.
5671f4a2713aSLionel Sambuc   Record.push_back(Data.NumVBases);
5672f4a2713aSLionel Sambuc   if (Data.NumVBases > 0)
5673f4a2713aSLionel Sambuc     AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5674f4a2713aSLionel Sambuc                             Record);
5675f4a2713aSLionel Sambuc 
5676f4a2713aSLionel Sambuc   AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5677f4a2713aSLionel Sambuc   AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
5678f4a2713aSLionel Sambuc   // Data.Definition is the owning decl, no need to write it.
5679f4a2713aSLionel Sambuc   AddDeclRef(D->getFirstFriend(), Record);
5680f4a2713aSLionel Sambuc 
5681f4a2713aSLionel Sambuc   // Add lambda-specific data.
5682f4a2713aSLionel Sambuc   if (Data.IsLambda) {
5683*0a6a1f1dSLionel Sambuc     auto &Lambda = D->getLambdaData();
5684f4a2713aSLionel Sambuc     Record.push_back(Lambda.Dependent);
5685f4a2713aSLionel Sambuc     Record.push_back(Lambda.IsGenericLambda);
5686f4a2713aSLionel Sambuc     Record.push_back(Lambda.CaptureDefault);
5687f4a2713aSLionel Sambuc     Record.push_back(Lambda.NumCaptures);
5688f4a2713aSLionel Sambuc     Record.push_back(Lambda.NumExplicitCaptures);
5689f4a2713aSLionel Sambuc     Record.push_back(Lambda.ManglingNumber);
5690f4a2713aSLionel Sambuc     AddDeclRef(Lambda.ContextDecl, Record);
5691f4a2713aSLionel Sambuc     AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
5692f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5693*0a6a1f1dSLionel Sambuc       const LambdaCapture &Capture = Lambda.Captures[I];
5694f4a2713aSLionel Sambuc       AddSourceLocation(Capture.getLocation(), Record);
5695f4a2713aSLionel Sambuc       Record.push_back(Capture.isImplicit());
5696f4a2713aSLionel Sambuc       Record.push_back(Capture.getCaptureKind());
5697f4a2713aSLionel Sambuc       switch (Capture.getCaptureKind()) {
5698f4a2713aSLionel Sambuc       case LCK_This:
5699*0a6a1f1dSLionel Sambuc       case LCK_VLAType:
5700f4a2713aSLionel Sambuc         break;
5701f4a2713aSLionel Sambuc       case LCK_ByCopy:
5702f4a2713aSLionel Sambuc       case LCK_ByRef:
5703f4a2713aSLionel Sambuc         VarDecl *Var =
5704*0a6a1f1dSLionel Sambuc             Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
5705f4a2713aSLionel Sambuc         AddDeclRef(Var, Record);
5706f4a2713aSLionel Sambuc         AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5707f4a2713aSLionel Sambuc                                                     : SourceLocation(),
5708f4a2713aSLionel Sambuc                           Record);
5709f4a2713aSLionel Sambuc         break;
5710f4a2713aSLionel Sambuc       }
5711f4a2713aSLionel Sambuc     }
5712f4a2713aSLionel Sambuc   }
5713f4a2713aSLionel Sambuc }
5714f4a2713aSLionel Sambuc 
ReaderInitialized(ASTReader * Reader)5715f4a2713aSLionel Sambuc void ASTWriter::ReaderInitialized(ASTReader *Reader) {
5716f4a2713aSLionel Sambuc   assert(Reader && "Cannot remove chain");
5717f4a2713aSLionel Sambuc   assert((!Chain || Chain == Reader) && "Cannot replace chain");
5718f4a2713aSLionel Sambuc   assert(FirstDeclID == NextDeclID &&
5719f4a2713aSLionel Sambuc          FirstTypeID == NextTypeID &&
5720f4a2713aSLionel Sambuc          FirstIdentID == NextIdentID &&
5721f4a2713aSLionel Sambuc          FirstMacroID == NextMacroID &&
5722f4a2713aSLionel Sambuc          FirstSubmoduleID == NextSubmoduleID &&
5723f4a2713aSLionel Sambuc          FirstSelectorID == NextSelectorID &&
5724f4a2713aSLionel Sambuc          "Setting chain after writing has started.");
5725f4a2713aSLionel Sambuc 
5726f4a2713aSLionel Sambuc   Chain = Reader;
5727f4a2713aSLionel Sambuc 
5728f4a2713aSLionel Sambuc   FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5729f4a2713aSLionel Sambuc   FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5730f4a2713aSLionel Sambuc   FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
5731f4a2713aSLionel Sambuc   FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
5732f4a2713aSLionel Sambuc   FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
5733f4a2713aSLionel Sambuc   FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
5734f4a2713aSLionel Sambuc   NextDeclID = FirstDeclID;
5735f4a2713aSLionel Sambuc   NextTypeID = FirstTypeID;
5736f4a2713aSLionel Sambuc   NextIdentID = FirstIdentID;
5737f4a2713aSLionel Sambuc   NextMacroID = FirstMacroID;
5738f4a2713aSLionel Sambuc   NextSelectorID = FirstSelectorID;
5739f4a2713aSLionel Sambuc   NextSubmoduleID = FirstSubmoduleID;
5740f4a2713aSLionel Sambuc }
5741f4a2713aSLionel Sambuc 
IdentifierRead(IdentID ID,IdentifierInfo * II)5742f4a2713aSLionel Sambuc void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
5743f4a2713aSLionel Sambuc   // Always keep the highest ID. See \p TypeRead() for more information.
5744f4a2713aSLionel Sambuc   IdentID &StoredID = IdentifierIDs[II];
5745f4a2713aSLionel Sambuc   if (ID > StoredID)
5746f4a2713aSLionel Sambuc     StoredID = ID;
5747f4a2713aSLionel Sambuc }
5748f4a2713aSLionel Sambuc 
MacroRead(serialization::MacroID ID,MacroInfo * MI)5749f4a2713aSLionel Sambuc void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
5750f4a2713aSLionel Sambuc   // Always keep the highest ID. See \p TypeRead() for more information.
5751f4a2713aSLionel Sambuc   MacroID &StoredID = MacroIDs[MI];
5752f4a2713aSLionel Sambuc   if (ID > StoredID)
5753f4a2713aSLionel Sambuc     StoredID = ID;
5754f4a2713aSLionel Sambuc }
5755f4a2713aSLionel Sambuc 
TypeRead(TypeIdx Idx,QualType T)5756f4a2713aSLionel Sambuc void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
5757f4a2713aSLionel Sambuc   // Always take the highest-numbered type index. This copes with an interesting
5758f4a2713aSLionel Sambuc   // case for chained AST writing where we schedule writing the type and then,
5759f4a2713aSLionel Sambuc   // later, deserialize the type from another AST. In this case, we want to
5760f4a2713aSLionel Sambuc   // keep the higher-numbered entry so that we can properly write it out to
5761f4a2713aSLionel Sambuc   // the AST file.
5762f4a2713aSLionel Sambuc   TypeIdx &StoredIdx = TypeIdxs[T];
5763f4a2713aSLionel Sambuc   if (Idx.getIndex() >= StoredIdx.getIndex())
5764f4a2713aSLionel Sambuc     StoredIdx = Idx;
5765f4a2713aSLionel Sambuc }
5766f4a2713aSLionel Sambuc 
SelectorRead(SelectorID ID,Selector S)5767f4a2713aSLionel Sambuc void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
5768f4a2713aSLionel Sambuc   // Always keep the highest ID. See \p TypeRead() for more information.
5769f4a2713aSLionel Sambuc   SelectorID &StoredID = SelectorIDs[S];
5770f4a2713aSLionel Sambuc   if (ID > StoredID)
5771f4a2713aSLionel Sambuc     StoredID = ID;
5772f4a2713aSLionel Sambuc }
5773f4a2713aSLionel Sambuc 
MacroDefinitionRead(serialization::PreprocessedEntityID ID,MacroDefinition * MD)5774f4a2713aSLionel Sambuc void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
5775f4a2713aSLionel Sambuc                                     MacroDefinition *MD) {
5776f4a2713aSLionel Sambuc   assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
5777f4a2713aSLionel Sambuc   MacroDefinitions[MD] = ID;
5778f4a2713aSLionel Sambuc }
5779f4a2713aSLionel Sambuc 
ModuleRead(serialization::SubmoduleID ID,Module * Mod)5780f4a2713aSLionel Sambuc void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5781f4a2713aSLionel Sambuc   assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5782f4a2713aSLionel Sambuc   SubmoduleIDs[Mod] = ID;
5783f4a2713aSLionel Sambuc }
5784f4a2713aSLionel Sambuc 
CompletedTagDefinition(const TagDecl * D)5785f4a2713aSLionel Sambuc void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
5786f4a2713aSLionel Sambuc   assert(D->isCompleteDefinition());
5787f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5788f4a2713aSLionel Sambuc   if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5789f4a2713aSLionel Sambuc     // We are interested when a PCH decl is modified.
5790f4a2713aSLionel Sambuc     if (RD->isFromASTFile()) {
5791f4a2713aSLionel Sambuc       // A forward reference was mutated into a definition. Rewrite it.
5792f4a2713aSLionel Sambuc       // FIXME: This happens during template instantiation, should we
5793f4a2713aSLionel Sambuc       // have created a new definition decl instead ?
5794*0a6a1f1dSLionel Sambuc       assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5795*0a6a1f1dSLionel Sambuc              "completed a tag from another module but not by instantiation?");
5796*0a6a1f1dSLionel Sambuc       DeclUpdates[RD].push_back(
5797*0a6a1f1dSLionel Sambuc           DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
5798f4a2713aSLionel Sambuc     }
5799f4a2713aSLionel Sambuc   }
5800f4a2713aSLionel Sambuc }
5801f4a2713aSLionel Sambuc 
AddedVisibleDecl(const DeclContext * DC,const Decl * D)5802f4a2713aSLionel Sambuc void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5803f4a2713aSLionel Sambuc   // TU and namespaces are handled elsewhere.
5804f4a2713aSLionel Sambuc   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5805f4a2713aSLionel Sambuc     return;
5806f4a2713aSLionel Sambuc 
5807f4a2713aSLionel Sambuc   if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
5808f4a2713aSLionel Sambuc     return; // Not a source decl added to a DeclContext from PCH.
5809f4a2713aSLionel Sambuc 
5810f4a2713aSLionel Sambuc   assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
5811*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5812f4a2713aSLionel Sambuc   AddUpdatedDeclContext(DC);
5813f4a2713aSLionel Sambuc   UpdatingVisibleDecls.push_back(D);
5814f4a2713aSLionel Sambuc }
5815f4a2713aSLionel Sambuc 
AddedCXXImplicitMember(const CXXRecordDecl * RD,const Decl * D)5816f4a2713aSLionel Sambuc void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5817f4a2713aSLionel Sambuc   assert(D->isImplicit());
5818f4a2713aSLionel Sambuc   if (!(!D->isFromASTFile() && RD->isFromASTFile()))
5819f4a2713aSLionel Sambuc     return; // Not a source member added to a class from PCH.
5820f4a2713aSLionel Sambuc   if (!isa<CXXMethodDecl>(D))
5821f4a2713aSLionel Sambuc     return; // We are interested in lazily declared implicit methods.
5822f4a2713aSLionel Sambuc 
5823f4a2713aSLionel Sambuc   // A decl coming from PCH was modified.
5824f4a2713aSLionel Sambuc   assert(RD->isCompleteDefinition());
5825*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5826*0a6a1f1dSLionel Sambuc   DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
5827f4a2713aSLionel Sambuc }
5828f4a2713aSLionel Sambuc 
AddedCXXTemplateSpecialization(const ClassTemplateDecl * TD,const ClassTemplateSpecializationDecl * D)5829f4a2713aSLionel Sambuc void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5830f4a2713aSLionel Sambuc                                      const ClassTemplateSpecializationDecl *D) {
5831f4a2713aSLionel Sambuc   // The specializations set is kept in the canonical template.
5832f4a2713aSLionel Sambuc   TD = TD->getCanonicalDecl();
5833f4a2713aSLionel Sambuc   if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5834f4a2713aSLionel Sambuc     return; // Not a source specialization added to a template from PCH.
5835f4a2713aSLionel Sambuc 
5836*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5837*0a6a1f1dSLionel Sambuc   DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5838*0a6a1f1dSLionel Sambuc                                        D));
5839f4a2713aSLionel Sambuc }
5840f4a2713aSLionel Sambuc 
AddedCXXTemplateSpecialization(const VarTemplateDecl * TD,const VarTemplateSpecializationDecl * D)5841f4a2713aSLionel Sambuc void ASTWriter::AddedCXXTemplateSpecialization(
5842f4a2713aSLionel Sambuc     const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5843f4a2713aSLionel Sambuc   // The specializations set is kept in the canonical template.
5844f4a2713aSLionel Sambuc   TD = TD->getCanonicalDecl();
5845f4a2713aSLionel Sambuc   if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5846f4a2713aSLionel Sambuc     return; // Not a source specialization added to a template from PCH.
5847f4a2713aSLionel Sambuc 
5848*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5849*0a6a1f1dSLionel Sambuc   DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5850*0a6a1f1dSLionel Sambuc                                        D));
5851f4a2713aSLionel Sambuc }
5852f4a2713aSLionel Sambuc 
AddedCXXTemplateSpecialization(const FunctionTemplateDecl * TD,const FunctionDecl * D)5853f4a2713aSLionel Sambuc void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5854f4a2713aSLionel Sambuc                                                const FunctionDecl *D) {
5855f4a2713aSLionel Sambuc   // The specializations set is kept in the canonical template.
5856f4a2713aSLionel Sambuc   TD = TD->getCanonicalDecl();
5857f4a2713aSLionel Sambuc   if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5858f4a2713aSLionel Sambuc     return; // Not a source specialization added to a template from PCH.
5859f4a2713aSLionel Sambuc 
5860*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5861*0a6a1f1dSLionel Sambuc   DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5862*0a6a1f1dSLionel Sambuc                                        D));
5863*0a6a1f1dSLionel Sambuc }
5864*0a6a1f1dSLionel Sambuc 
ResolvedExceptionSpec(const FunctionDecl * FD)5865*0a6a1f1dSLionel Sambuc void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5866*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5867*0a6a1f1dSLionel Sambuc   FD = FD->getCanonicalDecl();
5868*0a6a1f1dSLionel Sambuc   if (!FD->isFromASTFile())
5869*0a6a1f1dSLionel Sambuc     return; // Not a function declared in PCH and defined outside.
5870*0a6a1f1dSLionel Sambuc 
5871*0a6a1f1dSLionel Sambuc   DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5872f4a2713aSLionel Sambuc }
5873f4a2713aSLionel Sambuc 
DeducedReturnType(const FunctionDecl * FD,QualType ReturnType)5874f4a2713aSLionel Sambuc void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5875f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5876f4a2713aSLionel Sambuc   FD = FD->getCanonicalDecl();
5877f4a2713aSLionel Sambuc   if (!FD->isFromASTFile())
5878f4a2713aSLionel Sambuc     return; // Not a function declared in PCH and defined outside.
5879f4a2713aSLionel Sambuc 
5880*0a6a1f1dSLionel Sambuc   DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5881f4a2713aSLionel Sambuc }
5882f4a2713aSLionel Sambuc 
CompletedImplicitDefinition(const FunctionDecl * D)5883f4a2713aSLionel Sambuc void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
5884f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5885f4a2713aSLionel Sambuc   if (!D->isFromASTFile())
5886f4a2713aSLionel Sambuc     return; // Declaration not imported from PCH.
5887f4a2713aSLionel Sambuc 
5888*0a6a1f1dSLionel Sambuc   // Implicit function decl from a PCH was defined.
5889*0a6a1f1dSLionel Sambuc   DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
5890*0a6a1f1dSLionel Sambuc }
5891*0a6a1f1dSLionel Sambuc 
FunctionDefinitionInstantiated(const FunctionDecl * D)5892*0a6a1f1dSLionel Sambuc void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5893*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5894*0a6a1f1dSLionel Sambuc   if (!D->isFromASTFile())
5895*0a6a1f1dSLionel Sambuc     return;
5896*0a6a1f1dSLionel Sambuc 
5897*0a6a1f1dSLionel Sambuc   DeclUpdates[D].push_back(
5898*0a6a1f1dSLionel Sambuc       DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
5899f4a2713aSLionel Sambuc }
5900f4a2713aSLionel Sambuc 
StaticDataMemberInstantiated(const VarDecl * D)5901f4a2713aSLionel Sambuc void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
5902f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5903f4a2713aSLionel Sambuc   if (!D->isFromASTFile())
5904f4a2713aSLionel Sambuc     return;
5905f4a2713aSLionel Sambuc 
5906f4a2713aSLionel Sambuc   // Since the actual instantiation is delayed, this really means that we need
5907f4a2713aSLionel Sambuc   // to update the instantiation location.
5908*0a6a1f1dSLionel Sambuc   DeclUpdates[D].push_back(
5909*0a6a1f1dSLionel Sambuc       DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5910*0a6a1f1dSLionel Sambuc        D->getMemberSpecializationInfo()->getPointOfInstantiation()));
5911f4a2713aSLionel Sambuc }
5912f4a2713aSLionel Sambuc 
AddedObjCCategoryToInterface(const ObjCCategoryDecl * CatD,const ObjCInterfaceDecl * IFD)5913f4a2713aSLionel Sambuc void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5914f4a2713aSLionel Sambuc                                              const ObjCInterfaceDecl *IFD) {
5915f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5916f4a2713aSLionel Sambuc   if (!IFD->isFromASTFile())
5917f4a2713aSLionel Sambuc     return; // Declaration not imported from PCH.
5918f4a2713aSLionel Sambuc 
5919f4a2713aSLionel Sambuc   assert(IFD->getDefinition() && "Category on a class without a definition?");
5920f4a2713aSLionel Sambuc   ObjCClassesWithCategories.insert(
5921f4a2713aSLionel Sambuc     const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
5922f4a2713aSLionel Sambuc }
5923f4a2713aSLionel Sambuc 
5924f4a2713aSLionel Sambuc 
AddedObjCPropertyInClassExtension(const ObjCPropertyDecl * Prop,const ObjCPropertyDecl * OrigProp,const ObjCCategoryDecl * ClassExt)5925f4a2713aSLionel Sambuc void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5926f4a2713aSLionel Sambuc                                           const ObjCPropertyDecl *OrigProp,
5927f4a2713aSLionel Sambuc                                           const ObjCCategoryDecl *ClassExt) {
5928f4a2713aSLionel Sambuc   const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5929f4a2713aSLionel Sambuc   if (!D)
5930f4a2713aSLionel Sambuc     return;
5931f4a2713aSLionel Sambuc 
5932f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5933f4a2713aSLionel Sambuc   if (!D->isFromASTFile())
5934f4a2713aSLionel Sambuc     return; // Declaration not imported from PCH.
5935f4a2713aSLionel Sambuc 
5936f4a2713aSLionel Sambuc   RewriteDecl(D);
5937f4a2713aSLionel Sambuc }
5938f4a2713aSLionel Sambuc 
DeclarationMarkedUsed(const Decl * D)5939f4a2713aSLionel Sambuc void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5940f4a2713aSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5941f4a2713aSLionel Sambuc   if (!D->isFromASTFile())
5942f4a2713aSLionel Sambuc     return;
5943f4a2713aSLionel Sambuc 
5944*0a6a1f1dSLionel Sambuc   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
5945*0a6a1f1dSLionel Sambuc }
5946*0a6a1f1dSLionel Sambuc 
DeclarationMarkedOpenMPThreadPrivate(const Decl * D)5947*0a6a1f1dSLionel Sambuc void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5948*0a6a1f1dSLionel Sambuc   assert(!WritingAST && "Already writing the AST!");
5949*0a6a1f1dSLionel Sambuc   if (!D->isFromASTFile())
5950*0a6a1f1dSLionel Sambuc     return;
5951*0a6a1f1dSLionel Sambuc 
5952*0a6a1f1dSLionel Sambuc   DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5953f4a2713aSLionel Sambuc }
5954