]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Frontend/PCHWriter.cpp
Update clang to r86025.
[FreeBSD/FreeBSD.git] / lib / Frontend / PCHWriter.cpp
1 //===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
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 PCHWriter class, which writes a precompiled header.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Frontend/PCHWriter.h"
15 #include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
16 #include "../Sema/IdentifierResolver.h" // FIXME: move header
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclContextInternals.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/Type.h"
22 #include "clang/AST/TypeLocVisitor.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Lex/HeaderSearch.h"
26 #include "clang/Basic/FileManager.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/Version.h"
32 #include "llvm/ADT/APFloat.h"
33 #include "llvm/ADT/APInt.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/Bitcode/BitstreamWriter.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/System/Path.h"
39 #include <cstdio>
40 using namespace clang;
41
42 //===----------------------------------------------------------------------===//
43 // Type serialization
44 //===----------------------------------------------------------------------===//
45
46 namespace {
47   class VISIBILITY_HIDDEN PCHTypeWriter {
48     PCHWriter &Writer;
49     PCHWriter::RecordData &Record;
50
51   public:
52     /// \brief Type code that corresponds to the record generated.
53     pch::TypeCode Code;
54
55     PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
56       : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
57
58     void VisitArrayType(const ArrayType *T);
59     void VisitFunctionType(const FunctionType *T);
60     void VisitTagType(const TagType *T);
61
62 #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
63 #define ABSTRACT_TYPE(Class, Base)
64 #define DEPENDENT_TYPE(Class, Base)
65 #include "clang/AST/TypeNodes.def"
66   };
67 }
68
69 void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70   assert(false && "Built-in types are never serialized");
71 }
72
73 void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
74   Record.push_back(T->getWidth());
75   Record.push_back(T->isSigned());
76   Code = pch::TYPE_FIXED_WIDTH_INT;
77 }
78
79 void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
80   Writer.AddTypeRef(T->getElementType(), Record);
81   Code = pch::TYPE_COMPLEX;
82 }
83
84 void PCHTypeWriter::VisitPointerType(const PointerType *T) {
85   Writer.AddTypeRef(T->getPointeeType(), Record);
86   Code = pch::TYPE_POINTER;
87 }
88
89 void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
90   Writer.AddTypeRef(T->getPointeeType(), Record);
91   Code = pch::TYPE_BLOCK_POINTER;
92 }
93
94 void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
95   Writer.AddTypeRef(T->getPointeeType(), Record);
96   Code = pch::TYPE_LVALUE_REFERENCE;
97 }
98
99 void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
100   Writer.AddTypeRef(T->getPointeeType(), Record);
101   Code = pch::TYPE_RVALUE_REFERENCE;
102 }
103
104 void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
105   Writer.AddTypeRef(T->getPointeeType(), Record);
106   Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
107   Code = pch::TYPE_MEMBER_POINTER;
108 }
109
110 void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
111   Writer.AddTypeRef(T->getElementType(), Record);
112   Record.push_back(T->getSizeModifier()); // FIXME: stable values
113   Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
114 }
115
116 void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
117   VisitArrayType(T);
118   Writer.AddAPInt(T->getSize(), Record);
119   Code = pch::TYPE_CONSTANT_ARRAY;
120 }
121
122 void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
123   VisitArrayType(T);
124   Code = pch::TYPE_INCOMPLETE_ARRAY;
125 }
126
127 void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
128   VisitArrayType(T);
129   Writer.AddSourceLocation(T->getLBracketLoc(), Record);
130   Writer.AddSourceLocation(T->getRBracketLoc(), Record);
131   Writer.AddStmt(T->getSizeExpr());
132   Code = pch::TYPE_VARIABLE_ARRAY;
133 }
134
135 void PCHTypeWriter::VisitVectorType(const VectorType *T) {
136   Writer.AddTypeRef(T->getElementType(), Record);
137   Record.push_back(T->getNumElements());
138   Code = pch::TYPE_VECTOR;
139 }
140
141 void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
142   VisitVectorType(T);
143   Code = pch::TYPE_EXT_VECTOR;
144 }
145
146 void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
147   Writer.AddTypeRef(T->getResultType(), Record);
148 }
149
150 void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
151   VisitFunctionType(T);
152   Code = pch::TYPE_FUNCTION_NO_PROTO;
153 }
154
155 void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
156   VisitFunctionType(T);
157   Record.push_back(T->getNumArgs());
158   for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
159     Writer.AddTypeRef(T->getArgType(I), Record);
160   Record.push_back(T->isVariadic());
161   Record.push_back(T->getTypeQuals());
162   Record.push_back(T->hasExceptionSpec());
163   Record.push_back(T->hasAnyExceptionSpec());
164   Record.push_back(T->getNumExceptions());
165   for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
166     Writer.AddTypeRef(T->getExceptionType(I), Record);
167   Code = pch::TYPE_FUNCTION_PROTO;
168 }
169
170 void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
171   Writer.AddDeclRef(T->getDecl(), Record);
172   Code = pch::TYPE_TYPEDEF;
173 }
174
175 void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
176   Writer.AddStmt(T->getUnderlyingExpr());
177   Code = pch::TYPE_TYPEOF_EXPR;
178 }
179
180 void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
181   Writer.AddTypeRef(T->getUnderlyingType(), Record);
182   Code = pch::TYPE_TYPEOF;
183 }
184
185 void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
186   Writer.AddStmt(T->getUnderlyingExpr());
187   Code = pch::TYPE_DECLTYPE;
188 }
189
190 void PCHTypeWriter::VisitTagType(const TagType *T) {
191   Writer.AddDeclRef(T->getDecl(), Record);
192   assert(!T->isBeingDefined() &&
193          "Cannot serialize in the middle of a type definition");
194 }
195
196 void PCHTypeWriter::VisitRecordType(const RecordType *T) {
197   VisitTagType(T);
198   Code = pch::TYPE_RECORD;
199 }
200
201 void PCHTypeWriter::VisitEnumType(const EnumType *T) {
202   VisitTagType(T);
203   Code = pch::TYPE_ENUM;
204 }
205
206 void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
207   Writer.AddTypeRef(T->getUnderlyingType(), Record);
208   Record.push_back(T->getTagKind());
209   Code = pch::TYPE_ELABORATED;
210 }
211
212 void
213 PCHTypeWriter::VisitSubstTemplateTypeParmType(
214                                         const SubstTemplateTypeParmType *T) {
215   Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
216   Writer.AddTypeRef(T->getReplacementType(), Record);
217   Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
218 }
219
220 void
221 PCHTypeWriter::VisitTemplateSpecializationType(
222                                        const TemplateSpecializationType *T) {
223   // FIXME: Serialize this type (C++ only)
224   assert(false && "Cannot serialize template specialization types");
225 }
226
227 void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
228   // FIXME: Serialize this type (C++ only)
229   assert(false && "Cannot serialize qualified name types");
230 }
231
232 void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
233   Writer.AddDeclRef(T->getDecl(), Record);
234   Record.push_back(T->getNumProtocols());
235   for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
236        E = T->qual_end(); I != E; ++I)
237     Writer.AddDeclRef(*I, Record);
238   Code = pch::TYPE_OBJC_INTERFACE;
239 }
240
241 void
242 PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
243   Writer.AddTypeRef(T->getPointeeType(), Record);
244   Record.push_back(T->getNumProtocols());
245   for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
246        E = T->qual_end(); I != E; ++I)
247     Writer.AddDeclRef(*I, Record);
248   Code = pch::TYPE_OBJC_OBJECT_POINTER;
249 }
250
251 namespace {
252
253 class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
254   PCHWriter &Writer;
255   PCHWriter::RecordData &Record;
256
257 public:
258   TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
259     : Writer(Writer), Record(Record) { }
260
261 #define ABSTRACT_TYPELOC(CLASS, PARENT)
262 #define TYPELOC(CLASS, PARENT) \
263     void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
264 #include "clang/AST/TypeLocNodes.def"
265
266   void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
267   void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
268 };
269
270 }
271
272 void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
273   // nothing to do
274 }
275 void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
276   Writer.AddSourceLocation(TL.getNameLoc(), Record);
277 }
278 void TypeLocWriter::VisitFixedWidthIntTypeLoc(FixedWidthIntTypeLoc TL) {
279   Writer.AddSourceLocation(TL.getNameLoc(), Record);
280 }
281 void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
282   Writer.AddSourceLocation(TL.getNameLoc(), Record);
283 }
284 void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
285   Writer.AddSourceLocation(TL.getStarLoc(), Record);
286 }
287 void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
288   Writer.AddSourceLocation(TL.getCaretLoc(), Record);
289 }
290 void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
291   Writer.AddSourceLocation(TL.getAmpLoc(), Record);
292 }
293 void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
294   Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
295 }
296 void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
297   Writer.AddSourceLocation(TL.getStarLoc(), Record);
298 }
299 void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
300   Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
301   Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
302   Record.push_back(TL.getSizeExpr() ? 1 : 0);
303   if (TL.getSizeExpr())
304     Writer.AddStmt(TL.getSizeExpr());
305 }
306 void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
307   VisitArrayTypeLoc(TL);
308 }
309 void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
310   VisitArrayTypeLoc(TL);
311 }
312 void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
313   VisitArrayTypeLoc(TL);
314 }
315 void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
316                                             DependentSizedArrayTypeLoc TL) {
317   VisitArrayTypeLoc(TL);
318 }
319 void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
320                                         DependentSizedExtVectorTypeLoc TL) {
321   Writer.AddSourceLocation(TL.getNameLoc(), Record);
322 }
323 void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
324   Writer.AddSourceLocation(TL.getNameLoc(), Record);
325 }
326 void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
327   Writer.AddSourceLocation(TL.getNameLoc(), Record);
328 }
329 void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
330   Writer.AddSourceLocation(TL.getLParenLoc(), Record);
331   Writer.AddSourceLocation(TL.getRParenLoc(), Record);
332   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
333     Writer.AddDeclRef(TL.getArg(i), Record);
334 }
335 void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
336   VisitFunctionTypeLoc(TL);
337 }
338 void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
339   VisitFunctionTypeLoc(TL);
340 }
341 void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
342   Writer.AddSourceLocation(TL.getNameLoc(), Record);
343 }
344 void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
345   Writer.AddSourceLocation(TL.getNameLoc(), Record);
346 }
347 void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
348   Writer.AddSourceLocation(TL.getNameLoc(), Record);
349 }
350 void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
351   Writer.AddSourceLocation(TL.getNameLoc(), Record);
352 }
353 void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
354   Writer.AddSourceLocation(TL.getNameLoc(), Record);
355 }
356 void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
357   Writer.AddSourceLocation(TL.getNameLoc(), Record);
358 }
359 void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
360   Writer.AddSourceLocation(TL.getNameLoc(), Record);
361 }
362 void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
363   Writer.AddSourceLocation(TL.getNameLoc(), Record);
364 }
365 void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
366                                             SubstTemplateTypeParmTypeLoc TL) {
367   Writer.AddSourceLocation(TL.getNameLoc(), Record);
368 }
369 void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
370                                            TemplateSpecializationTypeLoc TL) {
371   Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
372   Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
373   Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
374   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
375     Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record);
376 }
377 void TypeLocWriter::VisitQualifiedNameTypeLoc(QualifiedNameTypeLoc TL) {
378   Writer.AddSourceLocation(TL.getNameLoc(), Record);
379 }
380 void TypeLocWriter::VisitTypenameTypeLoc(TypenameTypeLoc TL) {
381   Writer.AddSourceLocation(TL.getNameLoc(), Record);
382 }
383 void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
384   Writer.AddSourceLocation(TL.getNameLoc(), Record);
385   Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
386   Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
387   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
388     Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
389 }
390 void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
391   Writer.AddSourceLocation(TL.getStarLoc(), Record);
392   Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
393   Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
394   Record.push_back(TL.hasBaseTypeAsWritten());
395   Record.push_back(TL.hasProtocolsAsWritten());
396   if (TL.hasProtocolsAsWritten())
397     for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
398       Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
399 }
400
401 //===----------------------------------------------------------------------===//
402 // PCHWriter Implementation
403 //===----------------------------------------------------------------------===//
404
405 static void EmitBlockID(unsigned ID, const char *Name,
406                         llvm::BitstreamWriter &Stream,
407                         PCHWriter::RecordData &Record) {
408   Record.clear();
409   Record.push_back(ID);
410   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
411
412   // Emit the block name if present.
413   if (Name == 0 || Name[0] == 0) return;
414   Record.clear();
415   while (*Name)
416     Record.push_back(*Name++);
417   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
418 }
419
420 static void EmitRecordID(unsigned ID, const char *Name,
421                          llvm::BitstreamWriter &Stream,
422                          PCHWriter::RecordData &Record) {
423   Record.clear();
424   Record.push_back(ID);
425   while (*Name)
426     Record.push_back(*Name++);
427   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
428 }
429
430 static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
431                           PCHWriter::RecordData &Record) {
432 #define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
433   RECORD(STMT_STOP);
434   RECORD(STMT_NULL_PTR);
435   RECORD(STMT_NULL);
436   RECORD(STMT_COMPOUND);
437   RECORD(STMT_CASE);
438   RECORD(STMT_DEFAULT);
439   RECORD(STMT_LABEL);
440   RECORD(STMT_IF);
441   RECORD(STMT_SWITCH);
442   RECORD(STMT_WHILE);
443   RECORD(STMT_DO);
444   RECORD(STMT_FOR);
445   RECORD(STMT_GOTO);
446   RECORD(STMT_INDIRECT_GOTO);
447   RECORD(STMT_CONTINUE);
448   RECORD(STMT_BREAK);
449   RECORD(STMT_RETURN);
450   RECORD(STMT_DECL);
451   RECORD(STMT_ASM);
452   RECORD(EXPR_PREDEFINED);
453   RECORD(EXPR_DECL_REF);
454   RECORD(EXPR_INTEGER_LITERAL);
455   RECORD(EXPR_FLOATING_LITERAL);
456   RECORD(EXPR_IMAGINARY_LITERAL);
457   RECORD(EXPR_STRING_LITERAL);
458   RECORD(EXPR_CHARACTER_LITERAL);
459   RECORD(EXPR_PAREN);
460   RECORD(EXPR_UNARY_OPERATOR);
461   RECORD(EXPR_SIZEOF_ALIGN_OF);
462   RECORD(EXPR_ARRAY_SUBSCRIPT);
463   RECORD(EXPR_CALL);
464   RECORD(EXPR_MEMBER);
465   RECORD(EXPR_BINARY_OPERATOR);
466   RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
467   RECORD(EXPR_CONDITIONAL_OPERATOR);
468   RECORD(EXPR_IMPLICIT_CAST);
469   RECORD(EXPR_CSTYLE_CAST);
470   RECORD(EXPR_COMPOUND_LITERAL);
471   RECORD(EXPR_EXT_VECTOR_ELEMENT);
472   RECORD(EXPR_INIT_LIST);
473   RECORD(EXPR_DESIGNATED_INIT);
474   RECORD(EXPR_IMPLICIT_VALUE_INIT);
475   RECORD(EXPR_VA_ARG);
476   RECORD(EXPR_ADDR_LABEL);
477   RECORD(EXPR_STMT);
478   RECORD(EXPR_TYPES_COMPATIBLE);
479   RECORD(EXPR_CHOOSE);
480   RECORD(EXPR_GNU_NULL);
481   RECORD(EXPR_SHUFFLE_VECTOR);
482   RECORD(EXPR_BLOCK);
483   RECORD(EXPR_BLOCK_DECL_REF);
484   RECORD(EXPR_OBJC_STRING_LITERAL);
485   RECORD(EXPR_OBJC_ENCODE);
486   RECORD(EXPR_OBJC_SELECTOR_EXPR);
487   RECORD(EXPR_OBJC_PROTOCOL_EXPR);
488   RECORD(EXPR_OBJC_IVAR_REF_EXPR);
489   RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
490   RECORD(EXPR_OBJC_KVC_REF_EXPR);
491   RECORD(EXPR_OBJC_MESSAGE_EXPR);
492   RECORD(EXPR_OBJC_SUPER_EXPR);
493   RECORD(STMT_OBJC_FOR_COLLECTION);
494   RECORD(STMT_OBJC_CATCH);
495   RECORD(STMT_OBJC_FINALLY);
496   RECORD(STMT_OBJC_AT_TRY);
497   RECORD(STMT_OBJC_AT_SYNCHRONIZED);
498   RECORD(STMT_OBJC_AT_THROW);
499 #undef RECORD
500 }
501
502 void PCHWriter::WriteBlockInfoBlock() {
503   RecordData Record;
504   Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
505
506 #define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
507 #define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
508
509   // PCH Top-Level Block.
510   BLOCK(PCH_BLOCK);
511   RECORD(ORIGINAL_FILE_NAME);
512   RECORD(TYPE_OFFSET);
513   RECORD(DECL_OFFSET);
514   RECORD(LANGUAGE_OPTIONS);
515   RECORD(METADATA);
516   RECORD(IDENTIFIER_OFFSET);
517   RECORD(IDENTIFIER_TABLE);
518   RECORD(EXTERNAL_DEFINITIONS);
519   RECORD(SPECIAL_TYPES);
520   RECORD(STATISTICS);
521   RECORD(TENTATIVE_DEFINITIONS);
522   RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
523   RECORD(SELECTOR_OFFSETS);
524   RECORD(METHOD_POOL);
525   RECORD(PP_COUNTER_VALUE);
526   RECORD(SOURCE_LOCATION_OFFSETS);
527   RECORD(SOURCE_LOCATION_PRELOADS);
528   RECORD(STAT_CACHE);
529   RECORD(EXT_VECTOR_DECLS);
530   RECORD(COMMENT_RANGES);
531   RECORD(SVN_BRANCH_REVISION);
532   
533   // SourceManager Block.
534   BLOCK(SOURCE_MANAGER_BLOCK);
535   RECORD(SM_SLOC_FILE_ENTRY);
536   RECORD(SM_SLOC_BUFFER_ENTRY);
537   RECORD(SM_SLOC_BUFFER_BLOB);
538   RECORD(SM_SLOC_INSTANTIATION_ENTRY);
539   RECORD(SM_LINE_TABLE);
540   RECORD(SM_HEADER_FILE_INFO);
541
542   // Preprocessor Block.
543   BLOCK(PREPROCESSOR_BLOCK);
544   RECORD(PP_MACRO_OBJECT_LIKE);
545   RECORD(PP_MACRO_FUNCTION_LIKE);
546   RECORD(PP_TOKEN);
547
548   // Decls and Types block.
549   BLOCK(DECLTYPES_BLOCK);
550   RECORD(TYPE_EXT_QUAL);
551   RECORD(TYPE_FIXED_WIDTH_INT);
552   RECORD(TYPE_COMPLEX);
553   RECORD(TYPE_POINTER);
554   RECORD(TYPE_BLOCK_POINTER);
555   RECORD(TYPE_LVALUE_REFERENCE);
556   RECORD(TYPE_RVALUE_REFERENCE);
557   RECORD(TYPE_MEMBER_POINTER);
558   RECORD(TYPE_CONSTANT_ARRAY);
559   RECORD(TYPE_INCOMPLETE_ARRAY);
560   RECORD(TYPE_VARIABLE_ARRAY);
561   RECORD(TYPE_VECTOR);
562   RECORD(TYPE_EXT_VECTOR);
563   RECORD(TYPE_FUNCTION_PROTO);
564   RECORD(TYPE_FUNCTION_NO_PROTO);
565   RECORD(TYPE_TYPEDEF);
566   RECORD(TYPE_TYPEOF_EXPR);
567   RECORD(TYPE_TYPEOF);
568   RECORD(TYPE_RECORD);
569   RECORD(TYPE_ENUM);
570   RECORD(TYPE_OBJC_INTERFACE);
571   RECORD(TYPE_OBJC_OBJECT_POINTER);
572   RECORD(DECL_ATTR);
573   RECORD(DECL_TRANSLATION_UNIT);
574   RECORD(DECL_TYPEDEF);
575   RECORD(DECL_ENUM);
576   RECORD(DECL_RECORD);
577   RECORD(DECL_ENUM_CONSTANT);
578   RECORD(DECL_FUNCTION);
579   RECORD(DECL_OBJC_METHOD);
580   RECORD(DECL_OBJC_INTERFACE);
581   RECORD(DECL_OBJC_PROTOCOL);
582   RECORD(DECL_OBJC_IVAR);
583   RECORD(DECL_OBJC_AT_DEFS_FIELD);
584   RECORD(DECL_OBJC_CLASS);
585   RECORD(DECL_OBJC_FORWARD_PROTOCOL);
586   RECORD(DECL_OBJC_CATEGORY);
587   RECORD(DECL_OBJC_CATEGORY_IMPL);
588   RECORD(DECL_OBJC_IMPLEMENTATION);
589   RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
590   RECORD(DECL_OBJC_PROPERTY);
591   RECORD(DECL_OBJC_PROPERTY_IMPL);
592   RECORD(DECL_FIELD);
593   RECORD(DECL_VAR);
594   RECORD(DECL_IMPLICIT_PARAM);
595   RECORD(DECL_PARM_VAR);
596   RECORD(DECL_FILE_SCOPE_ASM);
597   RECORD(DECL_BLOCK);
598   RECORD(DECL_CONTEXT_LEXICAL);
599   RECORD(DECL_CONTEXT_VISIBLE);
600   // Statements and Exprs can occur in the Decls and Types block.
601   AddStmtsExprs(Stream, Record);
602 #undef RECORD
603 #undef BLOCK
604   Stream.ExitBlock();
605 }
606
607 /// \brief Adjusts the given filename to only write out the portion of the
608 /// filename that is not part of the system root directory.
609 ///
610 /// \param Filename the file name to adjust.
611 ///
612 /// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
613 /// the returned filename will be adjusted by this system root.
614 ///
615 /// \returns either the original filename (if it needs no adjustment) or the
616 /// adjusted filename (which points into the @p Filename parameter).
617 static const char *
618 adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
619   assert(Filename && "No file name to adjust?");
620
621   if (!isysroot)
622     return Filename;
623
624   // Verify that the filename and the system root have the same prefix.
625   unsigned Pos = 0;
626   for (; Filename[Pos] && isysroot[Pos]; ++Pos)
627     if (Filename[Pos] != isysroot[Pos])
628       return Filename; // Prefixes don't match.
629
630   // We hit the end of the filename before we hit the end of the system root.
631   if (!Filename[Pos])
632     return Filename;
633
634   // If the file name has a '/' at the current position, skip over the '/'.
635   // We distinguish sysroot-based includes from absolute includes by the
636   // absence of '/' at the beginning of sysroot-based includes.
637   if (Filename[Pos] == '/')
638     ++Pos;
639
640   return Filename + Pos;
641 }
642
643 /// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
644 void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
645   using namespace llvm;
646
647   // Metadata
648   const TargetInfo &Target = Context.Target;
649   BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
650   MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
651   MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
652   MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
653   MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
654   MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
655   MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
656   MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
657   unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
658
659   RecordData Record;
660   Record.push_back(pch::METADATA);
661   Record.push_back(pch::VERSION_MAJOR);
662   Record.push_back(pch::VERSION_MINOR);
663   Record.push_back(CLANG_VERSION_MAJOR);
664   Record.push_back(CLANG_VERSION_MINOR);
665   Record.push_back(isysroot != 0);
666   const std::string &TripleStr = Target.getTriple().getTriple();
667   Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
668
669   // Original file name
670   SourceManager &SM = Context.getSourceManager();
671   if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
672     BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
673     FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
674     FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
675     unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
676
677     llvm::sys::Path MainFilePath(MainFile->getName());
678     std::string MainFileName;
679
680     if (!MainFilePath.isAbsolute()) {
681       llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
682       P.appendComponent(MainFilePath.str());
683       MainFileName = P.str();
684     } else {
685       MainFileName = MainFilePath.str();
686     }
687
688     const char *MainFileNameStr = MainFileName.c_str();
689     MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
690                                                       isysroot);
691     RecordData Record;
692     Record.push_back(pch::ORIGINAL_FILE_NAME);
693     Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
694   }
695   
696   // Subversion branch/version information.
697   BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
698   SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
699   SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
700   SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
701   unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
702   Record.clear();
703   Record.push_back(pch::SVN_BRANCH_REVISION);
704   Record.push_back(getClangSubversionRevision());
705   Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
706 }
707
708 /// \brief Write the LangOptions structure.
709 void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
710   RecordData Record;
711   Record.push_back(LangOpts.Trigraphs);
712   Record.push_back(LangOpts.BCPLComment);  // BCPL-style '//' comments.
713   Record.push_back(LangOpts.DollarIdents);  // '$' allowed in identifiers.
714   Record.push_back(LangOpts.AsmPreprocessor);  // Preprocessor in asm mode.
715   Record.push_back(LangOpts.GNUMode);  // True in gnu99 mode false in c99 mode (etc)
716   Record.push_back(LangOpts.ImplicitInt);  // C89 implicit 'int'.
717   Record.push_back(LangOpts.Digraphs);  // C94, C99 and C++
718   Record.push_back(LangOpts.HexFloats);  // C99 Hexadecimal float constants.
719   Record.push_back(LangOpts.C99);  // C99 Support
720   Record.push_back(LangOpts.Microsoft);  // Microsoft extensions.
721   Record.push_back(LangOpts.CPlusPlus);  // C++ Support
722   Record.push_back(LangOpts.CPlusPlus0x);  // C++0x Support
723   Record.push_back(LangOpts.CXXOperatorNames);  // Treat C++ operator names as keywords.
724
725   Record.push_back(LangOpts.ObjC1);  // Objective-C 1 support enabled.
726   Record.push_back(LangOpts.ObjC2);  // Objective-C 2 support enabled.
727   Record.push_back(LangOpts.ObjCNonFragileABI);  // Objective-C modern abi enabled
728
729   Record.push_back(LangOpts.PascalStrings);  // Allow Pascal strings
730   Record.push_back(LangOpts.WritableStrings);  // Allow writable strings
731   Record.push_back(LangOpts.LaxVectorConversions);
732   Record.push_back(LangOpts.AltiVec);
733   Record.push_back(LangOpts.Exceptions);  // Support exception handling.
734
735   Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
736   Record.push_back(LangOpts.Freestanding); // Freestanding implementation
737   Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
738
739   // Whether static initializers are protected by locks.
740   Record.push_back(LangOpts.ThreadsafeStatics);
741   Record.push_back(LangOpts.POSIXThreads);
742   Record.push_back(LangOpts.Blocks); // block extension to C
743   Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
744                                   // they are unused.
745   Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
746                                   // (modulo the platform support).
747
748   Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
749                                   // signed integer arithmetic overflows.
750
751   Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
752                                   // may be ripped out at any time.
753
754   Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
755   Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
756                                   // defined.
757   Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
758                                   // opposed to __DYNAMIC__).
759   Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
760
761   Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
762                                   // used (instead of C99 semantics).
763   Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
764   Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
765                                             // be enabled.
766   Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
767                                            // unsigned type
768   Record.push_back(LangOpts.getGCMode());
769   Record.push_back(LangOpts.getVisibilityMode());
770   Record.push_back(LangOpts.getStackProtectorMode());
771   Record.push_back(LangOpts.InstantiationDepth);
772   Record.push_back(LangOpts.OpenCL);
773   Record.push_back(LangOpts.ElideConstructors);
774   Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
775 }
776
777 //===----------------------------------------------------------------------===//
778 // stat cache Serialization
779 //===----------------------------------------------------------------------===//
780
781 namespace {
782 // Trait used for the on-disk hash table of stat cache results.
783 class VISIBILITY_HIDDEN PCHStatCacheTrait {
784 public:
785   typedef const char * key_type;
786   typedef key_type key_type_ref;
787
788   typedef std::pair<int, struct stat> data_type;
789   typedef const data_type& data_type_ref;
790
791   static unsigned ComputeHash(const char *path) {
792     return llvm::HashString(path);
793   }
794
795   std::pair<unsigned,unsigned>
796     EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
797                       data_type_ref Data) {
798     unsigned StrLen = strlen(path);
799     clang::io::Emit16(Out, StrLen);
800     unsigned DataLen = 1; // result value
801     if (Data.first == 0)
802       DataLen += 4 + 4 + 2 + 8 + 8;
803     clang::io::Emit8(Out, DataLen);
804     return std::make_pair(StrLen + 1, DataLen);
805   }
806
807   void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
808     Out.write(path, KeyLen);
809   }
810
811   void EmitData(llvm::raw_ostream& Out, key_type_ref,
812                 data_type_ref Data, unsigned DataLen) {
813     using namespace clang::io;
814     uint64_t Start = Out.tell(); (void)Start;
815
816     // Result of stat()
817     Emit8(Out, Data.first? 1 : 0);
818
819     if (Data.first == 0) {
820       Emit32(Out, (uint32_t) Data.second.st_ino);
821       Emit32(Out, (uint32_t) Data.second.st_dev);
822       Emit16(Out, (uint16_t) Data.second.st_mode);
823       Emit64(Out, (uint64_t) Data.second.st_mtime);
824       Emit64(Out, (uint64_t) Data.second.st_size);
825     }
826
827     assert(Out.tell() - Start == DataLen && "Wrong data length");
828   }
829 };
830 } // end anonymous namespace
831
832 /// \brief Write the stat() system call cache to the PCH file.
833 void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
834                                const char *isysroot) {
835   // Build the on-disk hash table containing information about every
836   // stat() call.
837   OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
838   unsigned NumStatEntries = 0;
839   for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
840                                 StatEnd = StatCalls.end();
841        Stat != StatEnd; ++Stat, ++NumStatEntries) {
842     const char *Filename = Stat->first();
843     Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
844     Generator.insert(Filename, Stat->second);
845   }
846
847   // Create the on-disk hash table in a buffer.
848   llvm::SmallString<4096> StatCacheData;
849   uint32_t BucketOffset;
850   {
851     llvm::raw_svector_ostream Out(StatCacheData);
852     // Make sure that no bucket is at offset 0
853     clang::io::Emit32(Out, 0);
854     BucketOffset = Generator.Emit(Out);
855   }
856
857   // Create a blob abbreviation
858   using namespace llvm;
859   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
860   Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
861   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
862   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
863   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
864   unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
865
866   // Write the stat cache
867   RecordData Record;
868   Record.push_back(pch::STAT_CACHE);
869   Record.push_back(BucketOffset);
870   Record.push_back(NumStatEntries);
871   Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
872 }
873
874 //===----------------------------------------------------------------------===//
875 // Source Manager Serialization
876 //===----------------------------------------------------------------------===//
877
878 /// \brief Create an abbreviation for the SLocEntry that refers to a
879 /// file.
880 static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
881   using namespace llvm;
882   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
883   Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
884   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
885   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
886   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
887   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
888   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
889   return Stream.EmitAbbrev(Abbrev);
890 }
891
892 /// \brief Create an abbreviation for the SLocEntry that refers to a
893 /// buffer.
894 static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
895   using namespace llvm;
896   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
897   Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
898   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
899   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
900   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
901   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
902   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
903   return Stream.EmitAbbrev(Abbrev);
904 }
905
906 /// \brief Create an abbreviation for the SLocEntry that refers to a
907 /// buffer's blob.
908 static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
909   using namespace llvm;
910   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
911   Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
912   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
913   return Stream.EmitAbbrev(Abbrev);
914 }
915
916 /// \brief Create an abbreviation for the SLocEntry that refers to an
917 /// buffer.
918 static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
919   using namespace llvm;
920   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
921   Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
922   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
923   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
924   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
925   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
926   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
927   return Stream.EmitAbbrev(Abbrev);
928 }
929
930 /// \brief Writes the block containing the serialized form of the
931 /// source manager.
932 ///
933 /// TODO: We should probably use an on-disk hash table (stored in a
934 /// blob), indexed based on the file name, so that we only create
935 /// entries for files that we actually need. In the common case (no
936 /// errors), we probably won't have to create file entries for any of
937 /// the files in the AST.
938 void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
939                                         const Preprocessor &PP,
940                                         const char *isysroot) {
941   RecordData Record;
942
943   // Enter the source manager block.
944   Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
945
946   // Abbreviations for the various kinds of source-location entries.
947   unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
948   unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
949   unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
950   unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
951
952   // Write the line table.
953   if (SourceMgr.hasLineTable()) {
954     LineTableInfo &LineTable = SourceMgr.getLineTable();
955
956     // Emit the file names
957     Record.push_back(LineTable.getNumFilenames());
958     for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
959       // Emit the file name
960       const char *Filename = LineTable.getFilename(I);
961       Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
962       unsigned FilenameLen = Filename? strlen(Filename) : 0;
963       Record.push_back(FilenameLen);
964       if (FilenameLen)
965         Record.insert(Record.end(), Filename, Filename + FilenameLen);
966     }
967
968     // Emit the line entries
969     for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
970          L != LEnd; ++L) {
971       // Emit the file ID
972       Record.push_back(L->first);
973
974       // Emit the line entries
975       Record.push_back(L->second.size());
976       for (std::vector<LineEntry>::iterator LE = L->second.begin(),
977                                          LEEnd = L->second.end();
978            LE != LEEnd; ++LE) {
979         Record.push_back(LE->FileOffset);
980         Record.push_back(LE->LineNo);
981         Record.push_back(LE->FilenameID);
982         Record.push_back((unsigned)LE->FileKind);
983         Record.push_back(LE->IncludeOffset);
984       }
985     }
986     Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
987   }
988
989   // Write out entries for all of the header files we know about.
990   HeaderSearch &HS = PP.getHeaderSearchInfo();
991   Record.clear();
992   for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
993                                           E = HS.header_file_end();
994        I != E; ++I) {
995     Record.push_back(I->isImport);
996     Record.push_back(I->DirInfo);
997     Record.push_back(I->NumIncludes);
998     AddIdentifierRef(I->ControllingMacro, Record);
999     Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1000     Record.clear();
1001   }
1002
1003   // Write out the source location entry table. We skip the first
1004   // entry, which is always the same dummy entry.
1005   std::vector<uint32_t> SLocEntryOffsets;
1006   RecordData PreloadSLocs;
1007   SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
1008   for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
1009     // Get this source location entry.
1010     const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
1011     
1012     // Record the offset of this source-location entry.
1013     SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1014
1015     // Figure out which record code to use.
1016     unsigned Code;
1017     if (SLoc->isFile()) {
1018       if (SLoc->getFile().getContentCache()->Entry)
1019         Code = pch::SM_SLOC_FILE_ENTRY;
1020       else
1021         Code = pch::SM_SLOC_BUFFER_ENTRY;
1022     } else
1023       Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1024     Record.clear();
1025     Record.push_back(Code);
1026
1027     Record.push_back(SLoc->getOffset());
1028     if (SLoc->isFile()) {
1029       const SrcMgr::FileInfo &File = SLoc->getFile();
1030       Record.push_back(File.getIncludeLoc().getRawEncoding());
1031       Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1032       Record.push_back(File.hasLineDirectives());
1033
1034       const SrcMgr::ContentCache *Content = File.getContentCache();
1035       if (Content->Entry) {
1036         // The source location entry is a file. The blob associated
1037         // with this entry is the file name.
1038
1039         // Turn the file name into an absolute path, if it isn't already.
1040         const char *Filename = Content->Entry->getName();
1041         llvm::sys::Path FilePath(Filename, strlen(Filename));
1042         std::string FilenameStr;
1043         if (!FilePath.isAbsolute()) {
1044           llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
1045           P.appendComponent(FilePath.str());
1046           FilenameStr = P.str();
1047           Filename = FilenameStr.c_str();
1048         }
1049
1050         Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1051         Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
1052
1053         // FIXME: For now, preload all file source locations, so that
1054         // we get the appropriate File entries in the reader. This is
1055         // a temporary measure.
1056         PreloadSLocs.push_back(SLocEntryOffsets.size());
1057       } else {
1058         // The source location entry is a buffer. The blob associated
1059         // with this entry contains the contents of the buffer.
1060
1061         // We add one to the size so that we capture the trailing NULL
1062         // that is required by llvm::MemoryBuffer::getMemBuffer (on
1063         // the reader side).
1064         const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1065         const char *Name = Buffer->getBufferIdentifier();
1066         Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1067                                   llvm::StringRef(Name, strlen(Name) + 1));
1068         Record.clear();
1069         Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1070         Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1071                                   llvm::StringRef(Buffer->getBufferStart(),
1072                                                   Buffer->getBufferSize() + 1));
1073
1074         if (strcmp(Name, "<built-in>") == 0)
1075           PreloadSLocs.push_back(SLocEntryOffsets.size());
1076       }
1077     } else {
1078       // The source location entry is an instantiation.
1079       const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1080       Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1081       Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1082       Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1083
1084       // Compute the token length for this macro expansion.
1085       unsigned NextOffset = SourceMgr.getNextOffset();
1086       if (I + 1 != N)
1087         NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
1088       Record.push_back(NextOffset - SLoc->getOffset() - 1);
1089       Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1090     }
1091   }
1092
1093   Stream.ExitBlock();
1094
1095   if (SLocEntryOffsets.empty())
1096     return;
1097
1098   // Write the source-location offsets table into the PCH block. This
1099   // table is used for lazily loading source-location information.
1100   using namespace llvm;
1101   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1102   Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1103   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1104   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1105   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1106   unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1107
1108   Record.clear();
1109   Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1110   Record.push_back(SLocEntryOffsets.size());
1111   Record.push_back(SourceMgr.getNextOffset());
1112   Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1113                             (const char *)&SLocEntryOffsets.front(),
1114                            SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
1115
1116   // Write the source location entry preloads array, telling the PCH
1117   // reader which source locations entries it should load eagerly.
1118   Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
1119 }
1120
1121 //===----------------------------------------------------------------------===//
1122 // Preprocessor Serialization
1123 //===----------------------------------------------------------------------===//
1124
1125 /// \brief Writes the block containing the serialized form of the
1126 /// preprocessor.
1127 ///
1128 void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
1129   RecordData Record;
1130
1131   // If the preprocessor __COUNTER__ value has been bumped, remember it.
1132   if (PP.getCounterValue() != 0) {
1133     Record.push_back(PP.getCounterValue());
1134     Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
1135     Record.clear();
1136   }
1137
1138   // Enter the preprocessor block.
1139   Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
1140
1141   // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1142   // FIXME: use diagnostics subsystem for localization etc.
1143   if (PP.SawDateOrTime())
1144     fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1145
1146   // Loop over all the macro definitions that are live at the end of the file,
1147   // emitting each to the PP section.
1148   // FIXME: Make sure that this sees macros defined in included PCH files.
1149   for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1150        I != E; ++I) {
1151     // FIXME: This emits macros in hash table order, we should do it in a stable
1152     // order so that output is reproducible.
1153     MacroInfo *MI = I->second;
1154
1155     // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1156     // been redefined by the header (in which case they are not isBuiltinMacro).
1157     if (MI->isBuiltinMacro())
1158       continue;
1159
1160     // FIXME: Remove this identifier reference?
1161     AddIdentifierRef(I->first, Record);
1162     MacroOffsets[I->first] = Stream.GetCurrentBitNo();
1163     Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1164     Record.push_back(MI->isUsed());
1165
1166     unsigned Code;
1167     if (MI->isObjectLike()) {
1168       Code = pch::PP_MACRO_OBJECT_LIKE;
1169     } else {
1170       Code = pch::PP_MACRO_FUNCTION_LIKE;
1171
1172       Record.push_back(MI->isC99Varargs());
1173       Record.push_back(MI->isGNUVarargs());
1174       Record.push_back(MI->getNumArgs());
1175       for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1176            I != E; ++I)
1177         AddIdentifierRef(*I, Record);
1178     }
1179     Stream.EmitRecord(Code, Record);
1180     Record.clear();
1181
1182     // Emit the tokens array.
1183     for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1184       // Note that we know that the preprocessor does not have any annotation
1185       // tokens in it because they are created by the parser, and thus can't be
1186       // in a macro definition.
1187       const Token &Tok = MI->getReplacementToken(TokNo);
1188
1189       Record.push_back(Tok.getLocation().getRawEncoding());
1190       Record.push_back(Tok.getLength());
1191
1192       // FIXME: When reading literal tokens, reconstruct the literal pointer if
1193       // it is needed.
1194       AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1195
1196       // FIXME: Should translate token kind to a stable encoding.
1197       Record.push_back(Tok.getKind());
1198       // FIXME: Should translate token flags to a stable encoding.
1199       Record.push_back(Tok.getFlags());
1200
1201       Stream.EmitRecord(pch::PP_TOKEN, Record);
1202       Record.clear();
1203     }
1204     ++NumMacros;
1205   }
1206   Stream.ExitBlock();
1207 }
1208
1209 void PCHWriter::WriteComments(ASTContext &Context) {
1210   using namespace llvm;
1211
1212   if (Context.Comments.empty())
1213     return;
1214
1215   BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1216   CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1217   CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1218   unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
1219
1220   RecordData Record;
1221   Record.push_back(pch::COMMENT_RANGES);
1222   Stream.EmitRecordWithBlob(CommentCode, Record,
1223                             (const char*)&Context.Comments[0],
1224                             Context.Comments.size() * sizeof(SourceRange));
1225 }
1226
1227 //===----------------------------------------------------------------------===//
1228 // Type Serialization
1229 //===----------------------------------------------------------------------===//
1230
1231 /// \brief Write the representation of a type to the PCH stream.
1232 void PCHWriter::WriteType(QualType T) {
1233   pch::TypeID &ID = TypeIDs[T];
1234   if (ID == 0) // we haven't seen this type before.
1235     ID = NextTypeID++;
1236
1237   // Record the offset for this type.
1238   if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
1239     TypeOffsets.push_back(Stream.GetCurrentBitNo());
1240   else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1241     TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
1242     TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
1243   }
1244
1245   RecordData Record;
1246
1247   // Emit the type's representation.
1248   PCHTypeWriter W(*this, Record);
1249
1250   if (T.hasNonFastQualifiers()) {
1251     Qualifiers Qs = T.getQualifiers();
1252     AddTypeRef(T.getUnqualifiedType(), Record);
1253     Record.push_back(Qs.getAsOpaqueValue());
1254     W.Code = pch::TYPE_EXT_QUAL;
1255   } else {
1256     switch (T->getTypeClass()) {
1257       // For all of the concrete, non-dependent types, call the
1258       // appropriate visitor function.
1259 #define TYPE(Class, Base) \
1260       case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1261 #define ABSTRACT_TYPE(Class, Base)
1262 #define DEPENDENT_TYPE(Class, Base)
1263 #include "clang/AST/TypeNodes.def"
1264
1265       // For all of the dependent type nodes (which only occur in C++
1266       // templates), produce an error.
1267 #define TYPE(Class, Base)
1268 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
1269 #include "clang/AST/TypeNodes.def"
1270       assert(false && "Cannot serialize dependent type nodes");
1271       break;
1272     }
1273   }
1274
1275   // Emit the serialized record.
1276   Stream.EmitRecord(W.Code, Record);
1277
1278   // Flush any expressions that were written as part of this type.
1279   FlushStmts();
1280 }
1281
1282 //===----------------------------------------------------------------------===//
1283 // Declaration Serialization
1284 //===----------------------------------------------------------------------===//
1285
1286 /// \brief Write the block containing all of the declaration IDs
1287 /// lexically declared within the given DeclContext.
1288 ///
1289 /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1290 /// bistream, or 0 if no block was written.
1291 uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1292                                                  DeclContext *DC) {
1293   if (DC->decls_empty())
1294     return 0;
1295
1296   uint64_t Offset = Stream.GetCurrentBitNo();
1297   RecordData Record;
1298   for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1299          D != DEnd; ++D)
1300     AddDeclRef(*D, Record);
1301
1302   ++NumLexicalDeclContexts;
1303   Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
1304   return Offset;
1305 }
1306
1307 /// \brief Write the block containing all of the declaration IDs
1308 /// visible from the given DeclContext.
1309 ///
1310 /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1311 /// bistream, or 0 if no block was written.
1312 uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1313                                                  DeclContext *DC) {
1314   if (DC->getPrimaryContext() != DC)
1315     return 0;
1316
1317   // Since there is no name lookup into functions or methods, and we
1318   // perform name lookup for the translation unit via the
1319   // IdentifierInfo chains, don't bother to build a
1320   // visible-declarations table for these entities.
1321   if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
1322     return 0;
1323
1324   // Force the DeclContext to build a its name-lookup table.
1325   DC->lookup(DeclarationName());
1326
1327   // Serialize the contents of the mapping used for lookup. Note that,
1328   // although we have two very different code paths, the serialized
1329   // representation is the same for both cases: a declaration name,
1330   // followed by a size, followed by references to the visible
1331   // declarations that have that name.
1332   uint64_t Offset = Stream.GetCurrentBitNo();
1333   RecordData Record;
1334   StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
1335   if (!Map)
1336     return 0;
1337
1338   for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1339        D != DEnd; ++D) {
1340     AddDeclarationName(D->first, Record);
1341     DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1342     Record.push_back(Result.second - Result.first);
1343     for (; Result.first != Result.second; ++Result.first)
1344       AddDeclRef(*Result.first, Record);
1345   }
1346
1347   if (Record.size() == 0)
1348     return 0;
1349
1350   Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
1351   ++NumVisibleDeclContexts;
1352   return Offset;
1353 }
1354
1355 //===----------------------------------------------------------------------===//
1356 // Global Method Pool and Selector Serialization
1357 //===----------------------------------------------------------------------===//
1358
1359 namespace {
1360 // Trait used for the on-disk hash table used in the method pool.
1361 class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1362   PCHWriter &Writer;
1363
1364 public:
1365   typedef Selector key_type;
1366   typedef key_type key_type_ref;
1367
1368   typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1369   typedef const data_type& data_type_ref;
1370
1371   explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1372
1373   static unsigned ComputeHash(Selector Sel) {
1374     unsigned N = Sel.getNumArgs();
1375     if (N == 0)
1376       ++N;
1377     unsigned R = 5381;
1378     for (unsigned I = 0; I != N; ++I)
1379       if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1380         R = llvm::HashString(II->getName(), R);
1381     return R;
1382   }
1383
1384   std::pair<unsigned,unsigned>
1385     EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1386                       data_type_ref Methods) {
1387     unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1388     clang::io::Emit16(Out, KeyLen);
1389     unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1390     for (const ObjCMethodList *Method = &Methods.first; Method;
1391          Method = Method->Next)
1392       if (Method->Method)
1393         DataLen += 4;
1394     for (const ObjCMethodList *Method = &Methods.second; Method;
1395          Method = Method->Next)
1396       if (Method->Method)
1397         DataLen += 4;
1398     clang::io::Emit16(Out, DataLen);
1399     return std::make_pair(KeyLen, DataLen);
1400   }
1401
1402   void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1403     uint64_t Start = Out.tell();
1404     assert((Start >> 32) == 0 && "Selector key offset too large");
1405     Writer.SetSelectorOffset(Sel, Start);
1406     unsigned N = Sel.getNumArgs();
1407     clang::io::Emit16(Out, N);
1408     if (N == 0)
1409       N = 1;
1410     for (unsigned I = 0; I != N; ++I)
1411       clang::io::Emit32(Out,
1412                     Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1413   }
1414
1415   void EmitData(llvm::raw_ostream& Out, key_type_ref,
1416                 data_type_ref Methods, unsigned DataLen) {
1417     uint64_t Start = Out.tell(); (void)Start;
1418     unsigned NumInstanceMethods = 0;
1419     for (const ObjCMethodList *Method = &Methods.first; Method;
1420          Method = Method->Next)
1421       if (Method->Method)
1422         ++NumInstanceMethods;
1423
1424     unsigned NumFactoryMethods = 0;
1425     for (const ObjCMethodList *Method = &Methods.second; Method;
1426          Method = Method->Next)
1427       if (Method->Method)
1428         ++NumFactoryMethods;
1429
1430     clang::io::Emit16(Out, NumInstanceMethods);
1431     clang::io::Emit16(Out, NumFactoryMethods);
1432     for (const ObjCMethodList *Method = &Methods.first; Method;
1433          Method = Method->Next)
1434       if (Method->Method)
1435         clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
1436     for (const ObjCMethodList *Method = &Methods.second; Method;
1437          Method = Method->Next)
1438       if (Method->Method)
1439         clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
1440
1441     assert(Out.tell() - Start == DataLen && "Data length is wrong");
1442   }
1443 };
1444 } // end anonymous namespace
1445
1446 /// \brief Write the method pool into the PCH file.
1447 ///
1448 /// The method pool contains both instance and factory methods, stored
1449 /// in an on-disk hash table indexed by the selector.
1450 void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1451   using namespace llvm;
1452
1453   // Create and write out the blob that contains the instance and
1454   // factor method pools.
1455   bool Empty = true;
1456   {
1457     OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1458
1459     // Create the on-disk hash table representation. Start by
1460     // iterating through the instance method pool.
1461     PCHMethodPoolTrait::key_type Key;
1462     unsigned NumSelectorsInMethodPool = 0;
1463     for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1464            Instance = SemaRef.InstanceMethodPool.begin(),
1465            InstanceEnd = SemaRef.InstanceMethodPool.end();
1466          Instance != InstanceEnd; ++Instance) {
1467       // Check whether there is a factory method with the same
1468       // selector.
1469       llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1470         = SemaRef.FactoryMethodPool.find(Instance->first);
1471
1472       if (Factory == SemaRef.FactoryMethodPool.end())
1473         Generator.insert(Instance->first,
1474                          std::make_pair(Instance->second,
1475                                         ObjCMethodList()));
1476       else
1477         Generator.insert(Instance->first,
1478                          std::make_pair(Instance->second, Factory->second));
1479
1480       ++NumSelectorsInMethodPool;
1481       Empty = false;
1482     }
1483
1484     // Now iterate through the factory method pool, to pick up any
1485     // selectors that weren't already in the instance method pool.
1486     for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1487            Factory = SemaRef.FactoryMethodPool.begin(),
1488            FactoryEnd = SemaRef.FactoryMethodPool.end();
1489          Factory != FactoryEnd; ++Factory) {
1490       // Check whether there is an instance method with the same
1491       // selector. If so, there is no work to do here.
1492       llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1493         = SemaRef.InstanceMethodPool.find(Factory->first);
1494
1495       if (Instance == SemaRef.InstanceMethodPool.end()) {
1496         Generator.insert(Factory->first,
1497                          std::make_pair(ObjCMethodList(), Factory->second));
1498         ++NumSelectorsInMethodPool;
1499       }
1500
1501       Empty = false;
1502     }
1503
1504     if (Empty && SelectorOffsets.empty())
1505       return;
1506
1507     // Create the on-disk hash table in a buffer.
1508     llvm::SmallString<4096> MethodPool;
1509     uint32_t BucketOffset;
1510     SelectorOffsets.resize(SelVector.size());
1511     {
1512       PCHMethodPoolTrait Trait(*this);
1513       llvm::raw_svector_ostream Out(MethodPool);
1514       // Make sure that no bucket is at offset 0
1515       clang::io::Emit32(Out, 0);
1516       BucketOffset = Generator.Emit(Out, Trait);
1517
1518       // For every selector that we have seen but which was not
1519       // written into the hash table, write the selector itself and
1520       // record it's offset.
1521       for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1522         if (SelectorOffsets[I] == 0)
1523           Trait.EmitKey(Out, SelVector[I], 0);
1524     }
1525
1526     // Create a blob abbreviation
1527     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1528     Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1529     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1530     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1531     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1532     unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1533
1534     // Write the method pool
1535     RecordData Record;
1536     Record.push_back(pch::METHOD_POOL);
1537     Record.push_back(BucketOffset);
1538     Record.push_back(NumSelectorsInMethodPool);
1539     Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
1540
1541     // Create a blob abbreviation for the selector table offsets.
1542     Abbrev = new BitCodeAbbrev();
1543     Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1544     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1545     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1546     unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1547
1548     // Write the selector offsets table.
1549     Record.clear();
1550     Record.push_back(pch::SELECTOR_OFFSETS);
1551     Record.push_back(SelectorOffsets.size());
1552     Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1553                               (const char *)&SelectorOffsets.front(),
1554                               SelectorOffsets.size() * 4);
1555   }
1556 }
1557
1558 //===----------------------------------------------------------------------===//
1559 // Identifier Table Serialization
1560 //===----------------------------------------------------------------------===//
1561
1562 namespace {
1563 class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1564   PCHWriter &Writer;
1565   Preprocessor &PP;
1566
1567   /// \brief Determines whether this is an "interesting" identifier
1568   /// that needs a full IdentifierInfo structure written into the hash
1569   /// table.
1570   static bool isInterestingIdentifier(const IdentifierInfo *II) {
1571     return II->isPoisoned() ||
1572       II->isExtensionToken() ||
1573       II->hasMacroDefinition() ||
1574       II->getObjCOrBuiltinID() ||
1575       II->getFETokenInfo<void>();
1576   }
1577
1578 public:
1579   typedef const IdentifierInfo* key_type;
1580   typedef key_type  key_type_ref;
1581
1582   typedef pch::IdentID data_type;
1583   typedef data_type data_type_ref;
1584
1585   PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1586     : Writer(Writer), PP(PP) { }
1587
1588   static unsigned ComputeHash(const IdentifierInfo* II) {
1589     return llvm::HashString(II->getName());
1590   }
1591
1592   std::pair<unsigned,unsigned>
1593     EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1594                       pch::IdentID ID) {
1595     unsigned KeyLen = II->getLength() + 1;
1596     unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1597     if (isInterestingIdentifier(II)) {
1598       DataLen += 2; // 2 bytes for builtin ID, flags
1599       if (II->hasMacroDefinition() &&
1600           !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1601         DataLen += 4;
1602       for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1603                                      DEnd = IdentifierResolver::end();
1604            D != DEnd; ++D)
1605         DataLen += sizeof(pch::DeclID);
1606     }
1607     clang::io::Emit16(Out, DataLen);
1608     // We emit the key length after the data length so that every
1609     // string is preceded by a 16-bit length. This matches the PTH
1610     // format for storing identifiers.
1611     clang::io::Emit16(Out, KeyLen);
1612     return std::make_pair(KeyLen, DataLen);
1613   }
1614
1615   void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1616                unsigned KeyLen) {
1617     // Record the location of the key data.  This is used when generating
1618     // the mapping from persistent IDs to strings.
1619     Writer.SetIdentifierOffset(II, Out.tell());
1620     Out.write(II->getNameStart(), KeyLen);
1621   }
1622
1623   void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1624                 pch::IdentID ID, unsigned) {
1625     if (!isInterestingIdentifier(II)) {
1626       clang::io::Emit32(Out, ID << 1);
1627       return;
1628     }
1629
1630     clang::io::Emit32(Out, (ID << 1) | 0x01);
1631     uint32_t Bits = 0;
1632     bool hasMacroDefinition =
1633       II->hasMacroDefinition() &&
1634       !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
1635     Bits = (uint32_t)II->getObjCOrBuiltinID();
1636     Bits = (Bits << 1) | hasMacroDefinition;
1637     Bits = (Bits << 1) | II->isExtensionToken();
1638     Bits = (Bits << 1) | II->isPoisoned();
1639     Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1640     clang::io::Emit16(Out, Bits);
1641
1642     if (hasMacroDefinition)
1643       clang::io::Emit32(Out, Writer.getMacroOffset(II));
1644
1645     // Emit the declaration IDs in reverse order, because the
1646     // IdentifierResolver provides the declarations as they would be
1647     // visible (e.g., the function "stat" would come before the struct
1648     // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1649     // adds declarations to the end of the list (so we need to see the
1650     // struct "status" before the function "status").
1651     llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1652                                         IdentifierResolver::end());
1653     for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1654                                                       DEnd = Decls.rend();
1655          D != DEnd; ++D)
1656       clang::io::Emit32(Out, Writer.getDeclID(*D));
1657   }
1658 };
1659 } // end anonymous namespace
1660
1661 /// \brief Write the identifier table into the PCH file.
1662 ///
1663 /// The identifier table consists of a blob containing string data
1664 /// (the actual identifiers themselves) and a separate "offsets" index
1665 /// that maps identifier IDs to locations within the blob.
1666 void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
1667   using namespace llvm;
1668
1669   // Create and write out the blob that contains the identifier
1670   // strings.
1671   {
1672     OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1673
1674     // Look for any identifiers that were named while processing the
1675     // headers, but are otherwise not needed. We add these to the hash
1676     // table to enable checking of the predefines buffer in the case
1677     // where the user adds new macro definitions when building the PCH
1678     // file.
1679     for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1680                                 IDEnd = PP.getIdentifierTable().end();
1681          ID != IDEnd; ++ID)
1682       getIdentifierRef(ID->second);
1683
1684     // Create the on-disk hash table representation.
1685     IdentifierOffsets.resize(IdentifierIDs.size());
1686     for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1687            ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1688          ID != IDEnd; ++ID) {
1689       assert(ID->first && "NULL identifier in identifier table");
1690       Generator.insert(ID->first, ID->second);
1691     }
1692
1693     // Create the on-disk hash table in a buffer.
1694     llvm::SmallString<4096> IdentifierTable;
1695     uint32_t BucketOffset;
1696     {
1697       PCHIdentifierTableTrait Trait(*this, PP);
1698       llvm::raw_svector_ostream Out(IdentifierTable);
1699       // Make sure that no bucket is at offset 0
1700       clang::io::Emit32(Out, 0);
1701       BucketOffset = Generator.Emit(Out, Trait);
1702     }
1703
1704     // Create a blob abbreviation
1705     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1706     Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1707     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1708     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1709     unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
1710
1711     // Write the identifier table
1712     RecordData Record;
1713     Record.push_back(pch::IDENTIFIER_TABLE);
1714     Record.push_back(BucketOffset);
1715     Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
1716   }
1717
1718   // Write the offsets table for identifier IDs.
1719   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1720   Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1721   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1722   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1723   unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1724
1725   RecordData Record;
1726   Record.push_back(pch::IDENTIFIER_OFFSET);
1727   Record.push_back(IdentifierOffsets.size());
1728   Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1729                             (const char *)&IdentifierOffsets.front(),
1730                             IdentifierOffsets.size() * sizeof(uint32_t));
1731 }
1732
1733 //===----------------------------------------------------------------------===//
1734 // General Serialization Routines
1735 //===----------------------------------------------------------------------===//
1736
1737 /// \brief Write a record containing the given attributes.
1738 void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1739   RecordData Record;
1740   for (; Attr; Attr = Attr->getNext()) {
1741     Record.push_back(Attr->getKind()); // FIXME: stable encoding
1742     Record.push_back(Attr->isInherited());
1743     switch (Attr->getKind()) {
1744     case Attr::Alias:
1745       AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1746       break;
1747
1748     case Attr::Aligned:
1749       Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1750       break;
1751
1752     case Attr::AlwaysInline:
1753       break;
1754
1755     case Attr::AnalyzerNoReturn:
1756       break;
1757
1758     case Attr::Annotate:
1759       AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1760       break;
1761
1762     case Attr::AsmLabel:
1763       AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1764       break;
1765
1766     case Attr::Blocks:
1767       Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1768       break;
1769
1770     case Attr::Cleanup:
1771       AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1772       break;
1773
1774     case Attr::Const:
1775       break;
1776
1777     case Attr::Constructor:
1778       Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1779       break;
1780
1781     case Attr::DLLExport:
1782     case Attr::DLLImport:
1783     case Attr::Deprecated:
1784       break;
1785
1786     case Attr::Destructor:
1787       Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1788       break;
1789
1790     case Attr::FastCall:
1791       break;
1792
1793     case Attr::Format: {
1794       const FormatAttr *Format = cast<FormatAttr>(Attr);
1795       AddString(Format->getType(), Record);
1796       Record.push_back(Format->getFormatIdx());
1797       Record.push_back(Format->getFirstArg());
1798       break;
1799     }
1800
1801     case Attr::FormatArg: {
1802       const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1803       Record.push_back(Format->getFormatIdx());
1804       break;
1805     }
1806
1807     case Attr::Sentinel : {
1808       const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1809       Record.push_back(Sentinel->getSentinel());
1810       Record.push_back(Sentinel->getNullPos());
1811       break;
1812     }
1813
1814     case Attr::GNUInline:
1815     case Attr::IBOutletKind:
1816     case Attr::Malloc:
1817     case Attr::NoDebug:
1818     case Attr::NoReturn:
1819     case Attr::NoThrow:
1820     case Attr::NoInline:
1821       break;
1822
1823     case Attr::NonNull: {
1824       const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1825       Record.push_back(NonNull->size());
1826       Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1827       break;
1828     }
1829
1830     case Attr::ObjCException:
1831     case Attr::ObjCNSObject:
1832     case Attr::CFReturnsRetained:
1833     case Attr::NSReturnsRetained:
1834     case Attr::Overloadable:
1835       break;
1836
1837     case Attr::PragmaPack:
1838       Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
1839       break;
1840
1841     case Attr::Packed:
1842       break;
1843
1844     case Attr::Pure:
1845       break;
1846
1847     case Attr::Regparm:
1848       Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1849       break;
1850
1851     case Attr::ReqdWorkGroupSize:
1852       Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1853       Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1854       Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1855       break;
1856
1857     case Attr::Section:
1858       AddString(cast<SectionAttr>(Attr)->getName(), Record);
1859       break;
1860
1861     case Attr::StdCall:
1862     case Attr::TransparentUnion:
1863     case Attr::Unavailable:
1864     case Attr::Unused:
1865     case Attr::Used:
1866       break;
1867
1868     case Attr::Visibility:
1869       // FIXME: stable encoding
1870       Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1871       break;
1872
1873     case Attr::WarnUnusedResult:
1874     case Attr::Weak:
1875     case Attr::WeakImport:
1876       break;
1877     }
1878   }
1879
1880   Stream.EmitRecord(pch::DECL_ATTR, Record);
1881 }
1882
1883 void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1884   Record.push_back(Str.size());
1885   Record.insert(Record.end(), Str.begin(), Str.end());
1886 }
1887
1888 /// \brief Note that the identifier II occurs at the given offset
1889 /// within the identifier table.
1890 void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
1891   IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
1892 }
1893
1894 /// \brief Note that the selector Sel occurs at the given offset
1895 /// within the method pool/selector table.
1896 void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1897   unsigned ID = SelectorIDs[Sel];
1898   assert(ID && "Unknown selector");
1899   SelectorOffsets[ID - 1] = Offset;
1900 }
1901
1902 PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1903   : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
1904     NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1905     NumVisibleDeclContexts(0) { }
1906
1907 void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1908                          const char *isysroot) {
1909   using namespace llvm;
1910
1911   ASTContext &Context = SemaRef.Context;
1912   Preprocessor &PP = SemaRef.PP;
1913
1914   // Emit the file header.
1915   Stream.Emit((unsigned)'C', 8);
1916   Stream.Emit((unsigned)'P', 8);
1917   Stream.Emit((unsigned)'C', 8);
1918   Stream.Emit((unsigned)'H', 8);
1919
1920   WriteBlockInfoBlock();
1921
1922   // The translation unit is the first declaration we'll emit.
1923   DeclIDs[Context.getTranslationUnitDecl()] = 1;
1924   DeclTypesToEmit.push(Context.getTranslationUnitDecl());
1925
1926   // Make sure that we emit IdentifierInfos (and any attached
1927   // declarations) for builtins.
1928   {
1929     IdentifierTable &Table = PP.getIdentifierTable();
1930     llvm::SmallVector<const char *, 32> BuiltinNames;
1931     Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1932                                         Context.getLangOptions().NoBuiltin);
1933     for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1934       getIdentifierRef(&Table.get(BuiltinNames[I]));
1935   }
1936
1937   // Build a record containing all of the tentative definitions in this file, in
1938   // TentativeDefinitionList order.  Generally, this record will be empty for
1939   // headers.
1940   RecordData TentativeDefinitions;
1941   for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1942     VarDecl *VD =
1943       SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1944     if (VD) AddDeclRef(VD, TentativeDefinitions);
1945   }
1946
1947   // Build a record containing all of the locally-scoped external
1948   // declarations in this header file. Generally, this record will be
1949   // empty.
1950   RecordData LocallyScopedExternalDecls;
1951   // FIXME: This is filling in the PCH file in densemap order which is
1952   // nondeterminstic!
1953   for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1954          TD = SemaRef.LocallyScopedExternalDecls.begin(),
1955          TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1956        TD != TDEnd; ++TD)
1957     AddDeclRef(TD->second, LocallyScopedExternalDecls);
1958
1959   // Build a record containing all of the ext_vector declarations.
1960   RecordData ExtVectorDecls;
1961   for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1962     AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1963
1964   // Write the remaining PCH contents.
1965   RecordData Record;
1966   Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
1967   WriteMetadata(Context, isysroot);
1968   WriteLanguageOptions(Context.getLangOptions());
1969   if (StatCalls && !isysroot)
1970     WriteStatCache(*StatCalls, isysroot);
1971   WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
1972   WriteComments(Context);
1973   // Write the record of special types.
1974   Record.clear();
1975
1976   AddTypeRef(Context.getBuiltinVaListType(), Record);
1977   AddTypeRef(Context.getObjCIdType(), Record);
1978   AddTypeRef(Context.getObjCSelType(), Record);
1979   AddTypeRef(Context.getObjCProtoType(), Record);
1980   AddTypeRef(Context.getObjCClassType(), Record);
1981   AddTypeRef(Context.getRawCFConstantStringType(), Record);
1982   AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1983   AddTypeRef(Context.getFILEType(), Record);
1984   AddTypeRef(Context.getjmp_bufType(), Record);
1985   AddTypeRef(Context.getsigjmp_bufType(), Record);
1986   AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1987   AddTypeRef(Context.ObjCClassRedefinitionType, Record);
1988   AddTypeRef(Context.getRawBlockdescriptorType(), Record);
1989   AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
1990   Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1991
1992   // Keep writing types and declarations until all types and
1993   // declarations have been written.
1994   Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
1995   WriteDeclsBlockAbbrevs();
1996   while (!DeclTypesToEmit.empty()) {
1997     DeclOrType DOT = DeclTypesToEmit.front();
1998     DeclTypesToEmit.pop();
1999     if (DOT.isType())
2000       WriteType(DOT.getType());
2001     else
2002       WriteDecl(Context, DOT.getDecl());
2003   }
2004   Stream.ExitBlock();
2005   
2006   WritePreprocessor(PP);
2007   WriteMethodPool(SemaRef);
2008   WriteIdentifierTable(PP);
2009
2010   // Write the type offsets array
2011   BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2012   Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2013   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2014   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2015   unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2016   Record.clear();
2017   Record.push_back(pch::TYPE_OFFSET);
2018   Record.push_back(TypeOffsets.size());
2019   Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2020                             (const char *)&TypeOffsets.front(),
2021                             TypeOffsets.size() * sizeof(TypeOffsets[0]));
2022
2023   // Write the declaration offsets array
2024   Abbrev = new BitCodeAbbrev();
2025   Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2026   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2027   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2028   unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2029   Record.clear();
2030   Record.push_back(pch::DECL_OFFSET);
2031   Record.push_back(DeclOffsets.size());
2032   Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2033                             (const char *)&DeclOffsets.front(),
2034                             DeclOffsets.size() * sizeof(DeclOffsets[0]));
2035
2036   // Write the record containing external, unnamed definitions.
2037   if (!ExternalDefinitions.empty())
2038     Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
2039
2040   // Write the record containing tentative definitions.
2041   if (!TentativeDefinitions.empty())
2042     Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
2043
2044   // Write the record containing locally-scoped external definitions.
2045   if (!LocallyScopedExternalDecls.empty())
2046     Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2047                       LocallyScopedExternalDecls);
2048
2049   // Write the record containing ext_vector type names.
2050   if (!ExtVectorDecls.empty())
2051     Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
2052
2053   // Some simple statistics
2054   Record.clear();
2055   Record.push_back(NumStatements);
2056   Record.push_back(NumMacros);
2057   Record.push_back(NumLexicalDeclContexts);
2058   Record.push_back(NumVisibleDeclContexts);
2059   Stream.EmitRecord(pch::STATISTICS, Record);
2060   Stream.ExitBlock();
2061 }
2062
2063 void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2064   Record.push_back(Loc.getRawEncoding());
2065 }
2066
2067 void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2068   Record.push_back(Value.getBitWidth());
2069   unsigned N = Value.getNumWords();
2070   const uint64_t* Words = Value.getRawData();
2071   for (unsigned I = 0; I != N; ++I)
2072     Record.push_back(Words[I]);
2073 }
2074
2075 void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2076   Record.push_back(Value.isUnsigned());
2077   AddAPInt(Value, Record);
2078 }
2079
2080 void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2081   AddAPInt(Value.bitcastToAPInt(), Record);
2082 }
2083
2084 void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
2085   Record.push_back(getIdentifierRef(II));
2086 }
2087
2088 pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2089   if (II == 0)
2090     return 0;
2091
2092   pch::IdentID &ID = IdentifierIDs[II];
2093   if (ID == 0)
2094     ID = IdentifierIDs.size();
2095   return ID;
2096 }
2097
2098 void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2099   if (SelRef.getAsOpaquePtr() == 0) {
2100     Record.push_back(0);
2101     return;
2102   }
2103
2104   pch::SelectorID &SID = SelectorIDs[SelRef];
2105   if (SID == 0) {
2106     SID = SelectorIDs.size();
2107     SelVector.push_back(SelRef);
2108   }
2109   Record.push_back(SID);
2110 }
2111
2112 void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
2113                                        RecordData &Record) {
2114   switch (Arg.getArgument().getKind()) {
2115   case TemplateArgument::Expression:
2116     AddStmt(Arg.getLocInfo().getAsExpr());
2117     break;
2118   case TemplateArgument::Type:
2119     AddDeclaratorInfo(Arg.getLocInfo().getAsDeclaratorInfo(), Record);
2120     break;
2121   case TemplateArgument::Null:
2122   case TemplateArgument::Integral:
2123   case TemplateArgument::Declaration:
2124   case TemplateArgument::Pack:
2125     break;
2126   }
2127 }
2128
2129 void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2130   if (DInfo == 0) {
2131     AddTypeRef(QualType(), Record);
2132     return;
2133   }
2134
2135   AddTypeRef(DInfo->getType(), Record);
2136   TypeLocWriter TLW(*this, Record);
2137   for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2138     TLW.Visit(TL);  
2139 }
2140
2141 void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2142   if (T.isNull()) {
2143     Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2144     return;
2145   }
2146
2147   unsigned FastQuals = T.getFastQualifiers();
2148   T.removeFastQualifiers();
2149
2150   if (T.hasNonFastQualifiers()) {
2151     pch::TypeID &ID = TypeIDs[T];
2152     if (ID == 0) {
2153       // We haven't seen these qualifiers applied to this type before.
2154       // Assign it a new ID.  This is the only time we enqueue a
2155       // qualified type, and it has no CV qualifiers.
2156       ID = NextTypeID++;
2157       DeclTypesToEmit.push(T);
2158     }
2159     
2160     // Encode the type qualifiers in the type reference.
2161     Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2162     return;
2163   }
2164
2165   assert(!T.hasQualifiers());
2166   
2167   if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
2168     pch::TypeID ID = 0;
2169     switch (BT->getKind()) {
2170     case BuiltinType::Void:       ID = pch::PREDEF_TYPE_VOID_ID;       break;
2171     case BuiltinType::Bool:       ID = pch::PREDEF_TYPE_BOOL_ID;       break;
2172     case BuiltinType::Char_U:     ID = pch::PREDEF_TYPE_CHAR_U_ID;     break;
2173     case BuiltinType::UChar:      ID = pch::PREDEF_TYPE_UCHAR_ID;      break;
2174     case BuiltinType::UShort:     ID = pch::PREDEF_TYPE_USHORT_ID;     break;
2175     case BuiltinType::UInt:       ID = pch::PREDEF_TYPE_UINT_ID;       break;
2176     case BuiltinType::ULong:      ID = pch::PREDEF_TYPE_ULONG_ID;      break;
2177     case BuiltinType::ULongLong:  ID = pch::PREDEF_TYPE_ULONGLONG_ID;  break;
2178     case BuiltinType::UInt128:    ID = pch::PREDEF_TYPE_UINT128_ID;    break;
2179     case BuiltinType::Char_S:     ID = pch::PREDEF_TYPE_CHAR_S_ID;     break;
2180     case BuiltinType::SChar:      ID = pch::PREDEF_TYPE_SCHAR_ID;      break;
2181     case BuiltinType::WChar:      ID = pch::PREDEF_TYPE_WCHAR_ID;      break;
2182     case BuiltinType::Short:      ID = pch::PREDEF_TYPE_SHORT_ID;      break;
2183     case BuiltinType::Int:        ID = pch::PREDEF_TYPE_INT_ID;        break;
2184     case BuiltinType::Long:       ID = pch::PREDEF_TYPE_LONG_ID;       break;
2185     case BuiltinType::LongLong:   ID = pch::PREDEF_TYPE_LONGLONG_ID;   break;
2186     case BuiltinType::Int128:     ID = pch::PREDEF_TYPE_INT128_ID;     break;
2187     case BuiltinType::Float:      ID = pch::PREDEF_TYPE_FLOAT_ID;      break;
2188     case BuiltinType::Double:     ID = pch::PREDEF_TYPE_DOUBLE_ID;     break;
2189     case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2190     case BuiltinType::NullPtr:    ID = pch::PREDEF_TYPE_NULLPTR_ID;    break;
2191     case BuiltinType::Char16:     ID = pch::PREDEF_TYPE_CHAR16_ID;     break;
2192     case BuiltinType::Char32:     ID = pch::PREDEF_TYPE_CHAR32_ID;     break;
2193     case BuiltinType::Overload:   ID = pch::PREDEF_TYPE_OVERLOAD_ID;   break;
2194     case BuiltinType::Dependent:  ID = pch::PREDEF_TYPE_DEPENDENT_ID;  break;
2195     case BuiltinType::ObjCId:     ID = pch::PREDEF_TYPE_OBJC_ID;       break;
2196     case BuiltinType::ObjCClass:  ID = pch::PREDEF_TYPE_OBJC_CLASS;    break;
2197     case BuiltinType::UndeducedAuto:
2198       assert(0 && "Should not see undeduced auto here");
2199       break;
2200     }
2201
2202     Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2203     return;
2204   }
2205
2206   pch::TypeID &ID = TypeIDs[T];
2207   if (ID == 0) {
2208     // We haven't seen this type before. Assign it a new ID and put it
2209     // into the queue of types to emit.
2210     ID = NextTypeID++;
2211     DeclTypesToEmit.push(T);
2212   }
2213
2214   // Encode the type qualifiers in the type reference.
2215   Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2216 }
2217
2218 void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2219   if (D == 0) {
2220     Record.push_back(0);
2221     return;
2222   }
2223
2224   pch::DeclID &ID = DeclIDs[D];
2225   if (ID == 0) {
2226     // We haven't seen this declaration before. Give it a new ID and
2227     // enqueue it in the list of declarations to emit.
2228     ID = DeclIDs.size();
2229     DeclTypesToEmit.push(const_cast<Decl *>(D));
2230   }
2231
2232   Record.push_back(ID);
2233 }
2234
2235 pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2236   if (D == 0)
2237     return 0;
2238
2239   assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2240   return DeclIDs[D];
2241 }
2242
2243 void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2244   // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
2245   Record.push_back(Name.getNameKind());
2246   switch (Name.getNameKind()) {
2247   case DeclarationName::Identifier:
2248     AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2249     break;
2250
2251   case DeclarationName::ObjCZeroArgSelector:
2252   case DeclarationName::ObjCOneArgSelector:
2253   case DeclarationName::ObjCMultiArgSelector:
2254     AddSelectorRef(Name.getObjCSelector(), Record);
2255     break;
2256
2257   case DeclarationName::CXXConstructorName:
2258   case DeclarationName::CXXDestructorName:
2259   case DeclarationName::CXXConversionFunctionName:
2260     AddTypeRef(Name.getCXXNameType(), Record);
2261     break;
2262
2263   case DeclarationName::CXXOperatorName:
2264     Record.push_back(Name.getCXXOverloadedOperator());
2265     break;
2266
2267   case DeclarationName::CXXUsingDirective:
2268     // No extra data to emit
2269     break;
2270   }
2271 }
2272