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