blob: a6ee441c869b23e2b34512a6e72a1a8775147c47 [file] [log] [blame]
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001//===--- 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"
Douglas Gregor162dd022009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Mike Stump11289f42009-09-09 15:08:12 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregoref84c4b2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000031#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamWriter.h"
35#include "llvm/Support/Compiler.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000037#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000038#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000039using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// Type serialization
43//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000044
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045namespace {
46 class VISIBILITY_HIDDEN PCHTypeWriter {
47 PCHWriter &Writer;
48 PCHWriter::RecordData &Record;
49
50 public:
51 /// \brief Type code that corresponds to the record generated.
52 pch::TypeCode Code;
53
Mike Stump11289f42009-09-09 15:08:12 +000054 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000055 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
57 void VisitArrayType(const ArrayType *T);
58 void VisitFunctionType(const FunctionType *T);
59 void VisitTagType(const TagType *T);
60
61#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
62#define ABSTRACT_TYPE(Class, Base)
63#define DEPENDENT_TYPE(Class, Base)
64#include "clang/AST/TypeNodes.def"
65 };
66}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
69 assert(false && "Built-in types are never serialized");
70}
71
72void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
73 Record.push_back(T->getWidth());
74 Record.push_back(T->isSigned());
75 Code = pch::TYPE_FIXED_WIDTH_INT;
76}
77
78void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
79 Writer.AddTypeRef(T->getElementType(), Record);
80 Code = pch::TYPE_COMPLEX;
81}
82
83void PCHTypeWriter::VisitPointerType(const PointerType *T) {
84 Writer.AddTypeRef(T->getPointeeType(), Record);
85 Code = pch::TYPE_POINTER;
86}
87
88void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000089 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090 Code = pch::TYPE_BLOCK_POINTER;
91}
92
93void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_LVALUE_REFERENCE;
96}
97
98void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_RVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Code = pch::TYPE_MEMBER_POINTER;
107}
108
109void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
110 Writer.AddTypeRef(T->getElementType(), Record);
111 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000112 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
115void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
116 VisitArrayType(T);
117 Writer.AddAPInt(T->getSize(), Record);
118 Code = pch::TYPE_CONSTANT_ARRAY;
119}
120
121void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
122 VisitArrayType(T);
123 Code = pch::TYPE_INCOMPLETE_ARRAY;
124}
125
126void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
127 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000128 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
129 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000130 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 Code = pch::TYPE_VARIABLE_ARRAY;
132}
133
134void PCHTypeWriter::VisitVectorType(const VectorType *T) {
135 Writer.AddTypeRef(T->getElementType(), Record);
136 Record.push_back(T->getNumElements());
137 Code = pch::TYPE_VECTOR;
138}
139
140void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
141 VisitVectorType(T);
142 Code = pch::TYPE_EXT_VECTOR;
143}
144
145void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
146 Writer.AddTypeRef(T->getResultType(), Record);
147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000161 Record.push_back(T->hasExceptionSpec());
162 Record.push_back(T->hasAnyExceptionSpec());
163 Record.push_back(T->getNumExceptions());
164 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
165 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 Code = pch::TYPE_FUNCTION_PROTO;
167}
168
169void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
170 Writer.AddDeclRef(T->getDecl(), Record);
171 Code = pch::TYPE_TYPEDEF;
172}
173
174void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000175 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 Code = pch::TYPE_TYPEOF_EXPR;
177}
178
179void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
180 Writer.AddTypeRef(T->getUnderlyingType(), Record);
181 Code = pch::TYPE_TYPEOF;
182}
183
Anders Carlsson81df7b82009-06-24 19:06:50 +0000184void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
185 Writer.AddStmt(T->getUnderlyingExpr());
186 Code = pch::TYPE_DECLTYPE;
187}
188
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189void PCHTypeWriter::VisitTagType(const TagType *T) {
190 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000191 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192 "Cannot serialize in the middle of a type definition");
193}
194
195void PCHTypeWriter::VisitRecordType(const RecordType *T) {
196 VisitTagType(T);
197 Code = pch::TYPE_RECORD;
198}
199
200void PCHTypeWriter::VisitEnumType(const EnumType *T) {
201 VisitTagType(T);
202 Code = pch::TYPE_ENUM;
203}
204
John McCallfcc33b02009-09-05 00:15:47 +0000205void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
206 Writer.AddTypeRef(T->getUnderlyingType(), Record);
207 Record.push_back(T->getTagKind());
208 Code = pch::TYPE_ELABORATED;
209}
210
Mike Stump11289f42009-09-09 15:08:12 +0000211void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212PCHTypeWriter::VisitTemplateSpecializationType(
213 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000214 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215 assert(false && "Cannot serialize template specialization types");
216}
217
218void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000219 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220 assert(false && "Cannot serialize qualified name types");
221}
222
223void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
224 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000226 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
227 E = T->qual_end(); I != E; ++I)
228 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000229 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230}
231
Steve Narofffb4330f2009-06-17 22:40:22 +0000232void
233PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000234 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000236 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000237 E = T->qual_end(); I != E; ++I)
238 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000239 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240}
241
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000242void PCHTypeWriter::VisitObjCProtocolListType(const ObjCProtocolListType *T) {
243 Writer.AddTypeRef(T->getBaseType(), Record);
244 Record.push_back(T->getNumProtocols());
245 for (ObjCProtocolListType::qual_iterator I = T->qual_begin(),
246 E = T->qual_end(); I != E; ++I)
247 Writer.AddDeclRef(*I, Record);
248 Code = pch::TYPE_OBJC_PROTOCOL_LIST;
249}
250
John McCall8f115c62009-10-16 21:56:05 +0000251namespace {
252
253class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
254 PCHWriter &Writer;
255 PCHWriter::RecordData &Record;
256
257public:
258 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
259 : Writer(Writer), Record(Record) { }
260
261#define ABSTRACT_TYPELOC(CLASS)
262#define TYPELOC(CLASS, PARENT) \
263 void Visit##CLASS(CLASS TyLoc);
264#include "clang/AST/TypeLocNodes.def"
265
266 void VisitTypeLoc(TypeLoc TyLoc) {
267 assert(0 && "A type loc wrapper was not handled!");
268 }
269};
270
271}
272
273void TypeLocWriter::VisitQualifiedLoc(QualifiedLoc TyLoc) {
274 // nothing to do here
275}
276void TypeLocWriter::VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) {
277 Writer.AddSourceLocation(TyLoc.getStartLoc(), Record);
278}
279void TypeLocWriter::VisitTypedefLoc(TypedefLoc TyLoc) {
280 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
281}
282void TypeLocWriter::VisitObjCInterfaceLoc(ObjCInterfaceLoc TyLoc) {
283 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
284}
285void TypeLocWriter::VisitObjCProtocolListLoc(ObjCProtocolListLoc TyLoc) {
286 Writer.AddSourceLocation(TyLoc.getLAngleLoc(), Record);
287 Writer.AddSourceLocation(TyLoc.getRAngleLoc(), Record);
288 for (unsigned i = 0, e = TyLoc.getNumProtocols(); i != e; ++i)
289 Writer.AddSourceLocation(TyLoc.getProtocolLoc(i), Record);
290}
291void TypeLocWriter::VisitPointerLoc(PointerLoc TyLoc) {
292 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
293}
294void TypeLocWriter::VisitBlockPointerLoc(BlockPointerLoc TyLoc) {
295 Writer.AddSourceLocation(TyLoc.getCaretLoc(), Record);
296}
297void TypeLocWriter::VisitMemberPointerLoc(MemberPointerLoc TyLoc) {
298 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
299}
300void TypeLocWriter::VisitReferenceLoc(ReferenceLoc TyLoc) {
301 Writer.AddSourceLocation(TyLoc.getAmpLoc(), Record);
302}
303void TypeLocWriter::VisitFunctionLoc(FunctionLoc TyLoc) {
304 Writer.AddSourceLocation(TyLoc.getLParenLoc(), Record);
305 Writer.AddSourceLocation(TyLoc.getRParenLoc(), Record);
306 for (unsigned i = 0, e = TyLoc.getNumArgs(); i != e; ++i)
307 Writer.AddDeclRef(TyLoc.getArg(i), Record);
308}
309void TypeLocWriter::VisitArrayLoc(ArrayLoc TyLoc) {
310 Writer.AddSourceLocation(TyLoc.getLBracketLoc(), Record);
311 Writer.AddSourceLocation(TyLoc.getRBracketLoc(), Record);
312 Record.push_back(TyLoc.getSizeExpr() ? 1 : 0);
313 if (TyLoc.getSizeExpr())
314 Writer.AddStmt(TyLoc.getSizeExpr());
315}
316
Chris Lattner19cea4e2009-04-22 05:57:30 +0000317//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000318// PCHWriter Implementation
319//===----------------------------------------------------------------------===//
320
Chris Lattner28fa4e62009-04-26 22:26:21 +0000321static void EmitBlockID(unsigned ID, const char *Name,
322 llvm::BitstreamWriter &Stream,
323 PCHWriter::RecordData &Record) {
324 Record.clear();
325 Record.push_back(ID);
326 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
327
328 // Emit the block name if present.
329 if (Name == 0 || Name[0] == 0) return;
330 Record.clear();
331 while (*Name)
332 Record.push_back(*Name++);
333 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
334}
335
336static void EmitRecordID(unsigned ID, const char *Name,
337 llvm::BitstreamWriter &Stream,
338 PCHWriter::RecordData &Record) {
339 Record.clear();
340 Record.push_back(ID);
341 while (*Name)
342 Record.push_back(*Name++);
343 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000344}
345
346static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
347 PCHWriter::RecordData &Record) {
348#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
349 RECORD(STMT_STOP);
350 RECORD(STMT_NULL_PTR);
351 RECORD(STMT_NULL);
352 RECORD(STMT_COMPOUND);
353 RECORD(STMT_CASE);
354 RECORD(STMT_DEFAULT);
355 RECORD(STMT_LABEL);
356 RECORD(STMT_IF);
357 RECORD(STMT_SWITCH);
358 RECORD(STMT_WHILE);
359 RECORD(STMT_DO);
360 RECORD(STMT_FOR);
361 RECORD(STMT_GOTO);
362 RECORD(STMT_INDIRECT_GOTO);
363 RECORD(STMT_CONTINUE);
364 RECORD(STMT_BREAK);
365 RECORD(STMT_RETURN);
366 RECORD(STMT_DECL);
367 RECORD(STMT_ASM);
368 RECORD(EXPR_PREDEFINED);
369 RECORD(EXPR_DECL_REF);
370 RECORD(EXPR_INTEGER_LITERAL);
371 RECORD(EXPR_FLOATING_LITERAL);
372 RECORD(EXPR_IMAGINARY_LITERAL);
373 RECORD(EXPR_STRING_LITERAL);
374 RECORD(EXPR_CHARACTER_LITERAL);
375 RECORD(EXPR_PAREN);
376 RECORD(EXPR_UNARY_OPERATOR);
377 RECORD(EXPR_SIZEOF_ALIGN_OF);
378 RECORD(EXPR_ARRAY_SUBSCRIPT);
379 RECORD(EXPR_CALL);
380 RECORD(EXPR_MEMBER);
381 RECORD(EXPR_BINARY_OPERATOR);
382 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
383 RECORD(EXPR_CONDITIONAL_OPERATOR);
384 RECORD(EXPR_IMPLICIT_CAST);
385 RECORD(EXPR_CSTYLE_CAST);
386 RECORD(EXPR_COMPOUND_LITERAL);
387 RECORD(EXPR_EXT_VECTOR_ELEMENT);
388 RECORD(EXPR_INIT_LIST);
389 RECORD(EXPR_DESIGNATED_INIT);
390 RECORD(EXPR_IMPLICIT_VALUE_INIT);
391 RECORD(EXPR_VA_ARG);
392 RECORD(EXPR_ADDR_LABEL);
393 RECORD(EXPR_STMT);
394 RECORD(EXPR_TYPES_COMPATIBLE);
395 RECORD(EXPR_CHOOSE);
396 RECORD(EXPR_GNU_NULL);
397 RECORD(EXPR_SHUFFLE_VECTOR);
398 RECORD(EXPR_BLOCK);
399 RECORD(EXPR_BLOCK_DECL_REF);
400 RECORD(EXPR_OBJC_STRING_LITERAL);
401 RECORD(EXPR_OBJC_ENCODE);
402 RECORD(EXPR_OBJC_SELECTOR_EXPR);
403 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
404 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
405 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
406 RECORD(EXPR_OBJC_KVC_REF_EXPR);
407 RECORD(EXPR_OBJC_MESSAGE_EXPR);
408 RECORD(EXPR_OBJC_SUPER_EXPR);
409 RECORD(STMT_OBJC_FOR_COLLECTION);
410 RECORD(STMT_OBJC_CATCH);
411 RECORD(STMT_OBJC_FINALLY);
412 RECORD(STMT_OBJC_AT_TRY);
413 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
414 RECORD(STMT_OBJC_AT_THROW);
415#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000416}
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner28fa4e62009-04-26 22:26:21 +0000418void PCHWriter::WriteBlockInfoBlock() {
419 RecordData Record;
420 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattner64031982009-04-27 00:40:25 +0000422#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000423#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000424
Chris Lattner28fa4e62009-04-26 22:26:21 +0000425 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000426 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000427 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000428 RECORD(TYPE_OFFSET);
429 RECORD(DECL_OFFSET);
430 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000431 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000432 RECORD(IDENTIFIER_OFFSET);
433 RECORD(IDENTIFIER_TABLE);
434 RECORD(EXTERNAL_DEFINITIONS);
435 RECORD(SPECIAL_TYPES);
436 RECORD(STATISTICS);
437 RECORD(TENTATIVE_DEFINITIONS);
438 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
439 RECORD(SELECTOR_OFFSETS);
440 RECORD(METHOD_POOL);
441 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000442 RECORD(SOURCE_LOCATION_OFFSETS);
443 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000444 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000445 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000446 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000447 RECORD(SVN_BRANCH_REVISION);
448
Chris Lattner28fa4e62009-04-26 22:26:21 +0000449 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000450 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000451 RECORD(SM_SLOC_FILE_ENTRY);
452 RECORD(SM_SLOC_BUFFER_ENTRY);
453 RECORD(SM_SLOC_BUFFER_BLOB);
454 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
455 RECORD(SM_LINE_TABLE);
456 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattner28fa4e62009-04-26 22:26:21 +0000458 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000459 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000460 RECORD(PP_MACRO_OBJECT_LIKE);
461 RECORD(PP_MACRO_FUNCTION_LIKE);
462 RECORD(PP_TOKEN);
463
Douglas Gregor12bfa382009-10-17 00:13:19 +0000464 // Decls and Types block.
465 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000466 RECORD(TYPE_EXT_QUAL);
467 RECORD(TYPE_FIXED_WIDTH_INT);
468 RECORD(TYPE_COMPLEX);
469 RECORD(TYPE_POINTER);
470 RECORD(TYPE_BLOCK_POINTER);
471 RECORD(TYPE_LVALUE_REFERENCE);
472 RECORD(TYPE_RVALUE_REFERENCE);
473 RECORD(TYPE_MEMBER_POINTER);
474 RECORD(TYPE_CONSTANT_ARRAY);
475 RECORD(TYPE_INCOMPLETE_ARRAY);
476 RECORD(TYPE_VARIABLE_ARRAY);
477 RECORD(TYPE_VECTOR);
478 RECORD(TYPE_EXT_VECTOR);
479 RECORD(TYPE_FUNCTION_PROTO);
480 RECORD(TYPE_FUNCTION_NO_PROTO);
481 RECORD(TYPE_TYPEDEF);
482 RECORD(TYPE_TYPEOF_EXPR);
483 RECORD(TYPE_TYPEOF);
484 RECORD(TYPE_RECORD);
485 RECORD(TYPE_ENUM);
486 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000487 RECORD(TYPE_OBJC_OBJECT_POINTER);
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000488 RECORD(TYPE_OBJC_PROTOCOL_LIST);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000489 RECORD(DECL_ATTR);
490 RECORD(DECL_TRANSLATION_UNIT);
491 RECORD(DECL_TYPEDEF);
492 RECORD(DECL_ENUM);
493 RECORD(DECL_RECORD);
494 RECORD(DECL_ENUM_CONSTANT);
495 RECORD(DECL_FUNCTION);
496 RECORD(DECL_OBJC_METHOD);
497 RECORD(DECL_OBJC_INTERFACE);
498 RECORD(DECL_OBJC_PROTOCOL);
499 RECORD(DECL_OBJC_IVAR);
500 RECORD(DECL_OBJC_AT_DEFS_FIELD);
501 RECORD(DECL_OBJC_CLASS);
502 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
503 RECORD(DECL_OBJC_CATEGORY);
504 RECORD(DECL_OBJC_CATEGORY_IMPL);
505 RECORD(DECL_OBJC_IMPLEMENTATION);
506 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
507 RECORD(DECL_OBJC_PROPERTY);
508 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000509 RECORD(DECL_FIELD);
510 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000511 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000512 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000513 RECORD(DECL_ORIGINAL_PARM_VAR);
514 RECORD(DECL_FILE_SCOPE_ASM);
515 RECORD(DECL_BLOCK);
516 RECORD(DECL_CONTEXT_LEXICAL);
517 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000518 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000519 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000520#undef RECORD
521#undef BLOCK
522 Stream.ExitBlock();
523}
524
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000525/// \brief Adjusts the given filename to only write out the portion of the
526/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000527///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000528/// \param Filename the file name to adjust.
529///
530/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
531/// the returned filename will be adjusted by this system root.
532///
533/// \returns either the original filename (if it needs no adjustment) or the
534/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000535static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000536adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
537 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000538
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000539 if (!isysroot)
540 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000542 // Verify that the filename and the system root have the same prefix.
543 unsigned Pos = 0;
544 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
545 if (Filename[Pos] != isysroot[Pos])
546 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000547
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000548 // We hit the end of the filename before we hit the end of the system root.
549 if (!Filename[Pos])
550 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000552 // If the file name has a '/' at the current position, skip over the '/'.
553 // We distinguish sysroot-based includes from absolute includes by the
554 // absence of '/' at the beginning of sysroot-based includes.
555 if (Filename[Pos] == '/')
556 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000557
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000558 return Filename + Pos;
559}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000560
Douglas Gregor7b71e632009-04-27 22:23:34 +0000561/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000562void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000563 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000564
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000565 // Metadata
566 const TargetInfo &Target = Context.Target;
567 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
568 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
569 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
570 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
571 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
572 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
573 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
574 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
575 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000577 RecordData Record;
578 Record.push_back(pch::METADATA);
579 Record.push_back(pch::VERSION_MAJOR);
580 Record.push_back(pch::VERSION_MINOR);
581 Record.push_back(CLANG_VERSION_MAJOR);
582 Record.push_back(CLANG_VERSION_MINOR);
583 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000584 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000585 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000586
Douglas Gregor45fe0362009-05-12 01:31:05 +0000587 // Original file name
588 SourceManager &SM = Context.getSourceManager();
589 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
590 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
591 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
592 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
593 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
594
595 llvm::sys::Path MainFilePath(MainFile->getName());
596 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Douglas Gregor45fe0362009-05-12 01:31:05 +0000598 if (!MainFilePath.isAbsolute()) {
599 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000600 P.appendComponent(MainFilePath.str());
601 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000602 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000603 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000604 }
605
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000606 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000607 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000608 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000609 RecordData Record;
610 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000611 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000612 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000613
614 // Subversion branch/version information.
615 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
616 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
617 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
618 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
619 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
620 Record.clear();
621 Record.push_back(pch::SVN_BRANCH_REVISION);
622 Record.push_back(getClangSubversionRevision());
623 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000624}
625
626/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000627void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
628 RecordData Record;
629 Record.push_back(LangOpts.Trigraphs);
630 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
631 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
632 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
633 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
634 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
635 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
636 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
637 Record.push_back(LangOpts.C99); // C99 Support
638 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
639 Record.push_back(LangOpts.CPlusPlus); // C++ Support
640 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000641 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000642
Douglas Gregor55abb232009-04-10 20:39:37 +0000643 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
644 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
645 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregor55abb232009-04-10 20:39:37 +0000647 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000648 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
649 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000650 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000651 Record.push_back(LangOpts.Exceptions); // Support exception handling.
652
653 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
654 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
655 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
656
Chris Lattner258172e2009-04-27 07:35:58 +0000657 // Whether static initializers are protected by locks.
658 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000659 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000660 Record.push_back(LangOpts.Blocks); // block extension to C
661 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
662 // they are unused.
663 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
664 // (modulo the platform support).
665
666 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
667 // signed integer arithmetic overflows.
668
669 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
670 // may be ripped out at any time.
671
672 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000673 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000674 // defined.
675 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
676 // opposed to __DYNAMIC__).
677 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
678
679 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
680 // used (instead of C99 semantics).
681 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000682 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
683 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000684 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
685 // unsigned type
Douglas Gregor55abb232009-04-10 20:39:37 +0000686 Record.push_back(LangOpts.getGCMode());
687 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000688 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000689 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000690 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000691 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000692 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000693}
694
Douglas Gregora7f71a92009-04-10 03:52:48 +0000695//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000696// stat cache Serialization
697//===----------------------------------------------------------------------===//
698
699namespace {
700// Trait used for the on-disk hash table of stat cache results.
701class VISIBILITY_HIDDEN PCHStatCacheTrait {
702public:
703 typedef const char * key_type;
704 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Douglas Gregorc5046832009-04-27 18:38:38 +0000706 typedef std::pair<int, struct stat> data_type;
707 typedef const data_type& data_type_ref;
708
709 static unsigned ComputeHash(const char *path) {
710 return BernsteinHash(path);
711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
713 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000714 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
715 data_type_ref Data) {
716 unsigned StrLen = strlen(path);
717 clang::io::Emit16(Out, StrLen);
718 unsigned DataLen = 1; // result value
719 if (Data.first == 0)
720 DataLen += 4 + 4 + 2 + 8 + 8;
721 clang::io::Emit8(Out, DataLen);
722 return std::make_pair(StrLen + 1, DataLen);
723 }
Mike Stump11289f42009-09-09 15:08:12 +0000724
Douglas Gregorc5046832009-04-27 18:38:38 +0000725 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
726 Out.write(path, KeyLen);
727 }
Mike Stump11289f42009-09-09 15:08:12 +0000728
Douglas Gregorc5046832009-04-27 18:38:38 +0000729 void EmitData(llvm::raw_ostream& Out, key_type_ref,
730 data_type_ref Data, unsigned DataLen) {
731 using namespace clang::io;
732 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000733
Douglas Gregorc5046832009-04-27 18:38:38 +0000734 // Result of stat()
735 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregorc5046832009-04-27 18:38:38 +0000737 if (Data.first == 0) {
738 Emit32(Out, (uint32_t) Data.second.st_ino);
739 Emit32(Out, (uint32_t) Data.second.st_dev);
740 Emit16(Out, (uint16_t) Data.second.st_mode);
741 Emit64(Out, (uint64_t) Data.second.st_mtime);
742 Emit64(Out, (uint64_t) Data.second.st_size);
743 }
744
745 assert(Out.tell() - Start == DataLen && "Wrong data length");
746 }
747};
748} // end anonymous namespace
749
750/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000751void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
752 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000753 // Build the on-disk hash table containing information about every
754 // stat() call.
755 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
756 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000757 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000758 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000759 Stat != StatEnd; ++Stat, ++NumStatEntries) {
760 const char *Filename = Stat->first();
761 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
762 Generator.insert(Filename, Stat->second);
763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Douglas Gregorc5046832009-04-27 18:38:38 +0000765 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000766 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000767 uint32_t BucketOffset;
768 {
769 llvm::raw_svector_ostream Out(StatCacheData);
770 // Make sure that no bucket is at offset 0
771 clang::io::Emit32(Out, 0);
772 BucketOffset = Generator.Emit(Out);
773 }
774
775 // Create a blob abbreviation
776 using namespace llvm;
777 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
778 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
782 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
783
784 // Write the stat cache
785 RecordData Record;
786 Record.push_back(pch::STAT_CACHE);
787 Record.push_back(BucketOffset);
788 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000789 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000790}
791
792//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000793// Source Manager Serialization
794//===----------------------------------------------------------------------===//
795
796/// \brief Create an abbreviation for the SLocEntry that refers to a
797/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000798static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000799 using namespace llvm;
800 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
801 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
803 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000807 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000808}
809
810/// \brief Create an abbreviation for the SLocEntry that refers to a
811/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000812static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000813 using namespace llvm;
814 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
815 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
819 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000821 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000822}
823
824/// \brief Create an abbreviation for the SLocEntry that refers to a
825/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000826static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000827 using namespace llvm;
828 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
829 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000831 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000832}
833
834/// \brief Create an abbreviation for the SLocEntry that refers to an
835/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000836static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000837 using namespace llvm;
838 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
839 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000845 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000846}
847
848/// \brief Writes the block containing the serialized form of the
849/// source manager.
850///
851/// TODO: We should probably use an on-disk hash table (stored in a
852/// blob), indexed based on the file name, so that we only create
853/// entries for files that we actually need. In the common case (no
854/// errors), we probably won't have to create file entries for any of
855/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000856void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000857 const Preprocessor &PP,
858 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000859 RecordData Record;
860
Chris Lattner0910e3b2009-04-10 17:16:57 +0000861 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000862 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000863
864 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000865 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
866 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
867 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
868 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000869
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000870 // Write the line table.
871 if (SourceMgr.hasLineTable()) {
872 LineTableInfo &LineTable = SourceMgr.getLineTable();
873
874 // Emit the file names
875 Record.push_back(LineTable.getNumFilenames());
876 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
877 // Emit the file name
878 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000879 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000880 unsigned FilenameLen = Filename? strlen(Filename) : 0;
881 Record.push_back(FilenameLen);
882 if (FilenameLen)
883 Record.insert(Record.end(), Filename, Filename + FilenameLen);
884 }
Mike Stump11289f42009-09-09 15:08:12 +0000885
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000886 // Emit the line entries
887 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
888 L != LEnd; ++L) {
889 // Emit the file ID
890 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000892 // Emit the line entries
893 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000894 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000895 LEEnd = L->second.end();
896 LE != LEEnd; ++LE) {
897 Record.push_back(LE->FileOffset);
898 Record.push_back(LE->LineNo);
899 Record.push_back(LE->FilenameID);
900 Record.push_back((unsigned)LE->FileKind);
901 Record.push_back(LE->IncludeOffset);
902 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000903 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000904 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000905 }
906
Douglas Gregor258ae542009-04-27 06:38:32 +0000907 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000908 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000909 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000910 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000911 E = HS.header_file_end();
912 I != E; ++I) {
913 Record.push_back(I->isImport);
914 Record.push_back(I->DirInfo);
915 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +0000916 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000917 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
918 Record.clear();
919 }
920
Douglas Gregor258ae542009-04-27 06:38:32 +0000921 // Write out the source location entry table. We skip the first
922 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +0000923 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +0000924 RecordData PreloadSLocs;
925 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Douglas Gregor8655e882009-10-16 22:46:09 +0000926 for (unsigned I = 1, N = SourceMgr.sloc_entry_size(); I != N; ++I) {
927 // Get this source location entry.
928 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
929
Douglas Gregor258ae542009-04-27 06:38:32 +0000930 // Record the offset of this source-location entry.
931 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
932
933 // Figure out which record code to use.
934 unsigned Code;
935 if (SLoc->isFile()) {
936 if (SLoc->getFile().getContentCache()->Entry)
937 Code = pch::SM_SLOC_FILE_ENTRY;
938 else
939 Code = pch::SM_SLOC_BUFFER_ENTRY;
940 } else
941 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
942 Record.clear();
943 Record.push_back(Code);
944
945 Record.push_back(SLoc->getOffset());
946 if (SLoc->isFile()) {
947 const SrcMgr::FileInfo &File = SLoc->getFile();
948 Record.push_back(File.getIncludeLoc().getRawEncoding());
949 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
950 Record.push_back(File.hasLineDirectives());
951
952 const SrcMgr::ContentCache *Content = File.getContentCache();
953 if (Content->Entry) {
954 // The source location entry is a file. The blob associated
955 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +0000956
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 // Turn the file name into an absolute path, if it isn't already.
958 const char *Filename = Content->Entry->getName();
959 llvm::sys::Path FilePath(Filename, strlen(Filename));
960 std::string FilenameStr;
961 if (!FilePath.isAbsolute()) {
962 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000963 P.appendComponent(FilePath.str());
964 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965 Filename = FilenameStr.c_str();
966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000968 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000969 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +0000970
971 // FIXME: For now, preload all file source locations, so that
972 // we get the appropriate File entries in the reader. This is
973 // a temporary measure.
974 PreloadSLocs.push_back(SLocEntryOffsets.size());
975 } else {
976 // The source location entry is a buffer. The blob associated
977 // with this entry contains the contents of the buffer.
978
979 // We add one to the size so that we capture the trailing NULL
980 // that is required by llvm::MemoryBuffer::getMemBuffer (on
981 // the reader side).
982 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
983 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000984 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
985 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +0000986 Record.clear();
987 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
988 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +0000989 llvm::StringRef(Buffer->getBufferStart(),
990 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +0000991
992 if (strcmp(Name, "<built-in>") == 0)
993 PreloadSLocs.push_back(SLocEntryOffsets.size());
994 }
995 } else {
996 // The source location entry is an instantiation.
997 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
998 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
999 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1000 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1001
1002 // Compute the token length for this macro expansion.
1003 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001004 if (I + 1 != N)
1005 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001006 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1007 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1008 }
1009 }
1010
Douglas Gregor8f45df52009-04-16 22:23:12 +00001011 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001012
1013 if (SLocEntryOffsets.empty())
1014 return;
1015
1016 // Write the source-location offsets table into the PCH block. This
1017 // table is used for lazily loading source-location information.
1018 using namespace llvm;
1019 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1020 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1024 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001025
Douglas Gregor258ae542009-04-27 06:38:32 +00001026 Record.clear();
1027 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1028 Record.push_back(SLocEntryOffsets.size());
1029 Record.push_back(SourceMgr.getNextOffset());
1030 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001031 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +00001032 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001033
1034 // Write the source location entry preloads array, telling the PCH
1035 // reader which source locations entries it should load eagerly.
1036 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001037}
1038
Douglas Gregorc5046832009-04-27 18:38:38 +00001039//===----------------------------------------------------------------------===//
1040// Preprocessor Serialization
1041//===----------------------------------------------------------------------===//
1042
Chris Lattnereeffaef2009-04-10 17:15:23 +00001043/// \brief Writes the block containing the serialized form of the
1044/// preprocessor.
1045///
Chris Lattner2199f5b2009-04-10 18:08:30 +00001046void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001047 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001048
Chris Lattner0af3ba12009-04-13 01:29:17 +00001049 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1050 if (PP.getCounterValue() != 0) {
1051 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +00001052 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001053 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001054 }
1055
1056 // Enter the preprocessor block.
1057 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001058
Douglas Gregoreda6a892009-04-26 00:07:37 +00001059 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1060 // FIXME: use diagnostics subsystem for localization etc.
1061 if (PP.SawDateOrTime())
1062 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001063
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001064 // Loop over all the macro definitions that are live at the end of the file,
1065 // emitting each to the PP section.
Douglas Gregor45053152009-10-17 17:25:45 +00001066 // FIXME: Make sure that this sees macros defined in included PCH files.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001067 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1068 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001069 // FIXME: This emits macros in hash table order, we should do it in a stable
1070 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001071 MacroInfo *MI = I->second;
1072
1073 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1074 // been redefined by the header (in which case they are not isBuiltinMacro).
1075 if (MI->isBuiltinMacro())
1076 continue;
1077
Douglas Gregorc3366a52009-04-21 23:56:24 +00001078 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001079 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001080 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001081 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1082 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001084 unsigned Code;
1085 if (MI->isObjectLike()) {
1086 Code = pch::PP_MACRO_OBJECT_LIKE;
1087 } else {
1088 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001090 Record.push_back(MI->isC99Varargs());
1091 Record.push_back(MI->isGNUVarargs());
1092 Record.push_back(MI->getNumArgs());
1093 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1094 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001095 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001096 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001097 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001098 Record.clear();
1099
Chris Lattner2199f5b2009-04-10 18:08:30 +00001100 // Emit the tokens array.
1101 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1102 // Note that we know that the preprocessor does not have any annotation
1103 // tokens in it because they are created by the parser, and thus can't be
1104 // in a macro definition.
1105 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001106
Chris Lattner2199f5b2009-04-10 18:08:30 +00001107 Record.push_back(Tok.getLocation().getRawEncoding());
1108 Record.push_back(Tok.getLength());
1109
Chris Lattner2199f5b2009-04-10 18:08:30 +00001110 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1111 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001112 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattner2199f5b2009-04-10 18:08:30 +00001114 // FIXME: Should translate token kind to a stable encoding.
1115 Record.push_back(Tok.getKind());
1116 // FIXME: Should translate token flags to a stable encoding.
1117 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001118
Douglas Gregor8f45df52009-04-16 22:23:12 +00001119 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001120 Record.clear();
1121 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001122 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001123 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001124 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001125}
1126
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001127void PCHWriter::WriteComments(ASTContext &Context) {
1128 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001129
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001130 if (Context.Comments.empty())
1131 return;
Mike Stump11289f42009-09-09 15:08:12 +00001132
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001133 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1134 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1135 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1136 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001137
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001138 RecordData Record;
1139 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001140 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001141 (const char*)&Context.Comments[0],
1142 Context.Comments.size() * sizeof(SourceRange));
1143}
1144
Douglas Gregorc5046832009-04-27 18:38:38 +00001145//===----------------------------------------------------------------------===//
1146// Type Serialization
1147//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001148
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001149/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001150void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001151 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001152 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001153 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001155 // Record the offset for this type.
1156 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001157 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001158 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1159 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001160 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001161 }
1162
1163 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001164
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001165 // Emit the type's representation.
1166 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001167
1168 if (T.hasNonFastQualifiers()) {
1169 Qualifiers Qs = T.getQualifiers();
1170 AddTypeRef(T.getUnqualifiedType(), Record);
1171 Record.push_back(Qs.getAsOpaqueValue());
1172 W.Code = pch::TYPE_EXT_QUAL;
1173 } else {
1174 switch (T->getTypeClass()) {
1175 // For all of the concrete, non-dependent types, call the
1176 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001177#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001178 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001179#define ABSTRACT_TYPE(Class, Base)
1180#define DEPENDENT_TYPE(Class, Base)
1181#include "clang/AST/TypeNodes.def"
1182
John McCall8ccfcb52009-09-24 19:53:00 +00001183 // For all of the dependent type nodes (which only occur in C++
1184 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001185#define TYPE(Class, Base)
1186#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1187#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001188 assert(false && "Cannot serialize dependent type nodes");
1189 break;
1190 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001191 }
1192
1193 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001194 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001195
1196 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001197 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001198}
1199
Douglas Gregorc5046832009-04-27 18:38:38 +00001200//===----------------------------------------------------------------------===//
1201// Declaration Serialization
1202//===----------------------------------------------------------------------===//
1203
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001204/// \brief Write the block containing all of the declaration IDs
1205/// lexically declared within the given DeclContext.
1206///
1207/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1208/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001209uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001210 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001211 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001212 return 0;
1213
Douglas Gregor8f45df52009-04-16 22:23:12 +00001214 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001215 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001216 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1217 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001218 AddDeclRef(*D, Record);
1219
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001220 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001221 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001222 return Offset;
1223}
1224
1225/// \brief Write the block containing all of the declaration IDs
1226/// visible from the given DeclContext.
1227///
1228/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1229/// bistream, or 0 if no block was written.
1230uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1231 DeclContext *DC) {
1232 if (DC->getPrimaryContext() != DC)
1233 return 0;
1234
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001235 // Since there is no name lookup into functions or methods, and we
1236 // perform name lookup for the translation unit via the
1237 // IdentifierInfo chains, don't bother to build a
1238 // visible-declarations table for these entities.
1239 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001240 return 0;
1241
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001242 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001243 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001244
1245 // Serialize the contents of the mapping used for lookup. Note that,
1246 // although we have two very different code paths, the serialized
1247 // representation is the same for both cases: a declaration name,
1248 // followed by a size, followed by references to the visible
1249 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001250 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001251 RecordData Record;
1252 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001253 if (!Map)
1254 return 0;
1255
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001256 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1257 D != DEnd; ++D) {
1258 AddDeclarationName(D->first, Record);
1259 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1260 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001261 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001262 AddDeclRef(*Result.first, Record);
1263 }
1264
1265 if (Record.size() == 0)
1266 return 0;
1267
Douglas Gregor8f45df52009-04-16 22:23:12 +00001268 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001269 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001270 return Offset;
1271}
1272
Douglas Gregorc5046832009-04-27 18:38:38 +00001273//===----------------------------------------------------------------------===//
1274// Global Method Pool and Selector Serialization
1275//===----------------------------------------------------------------------===//
1276
Douglas Gregore84a9da2009-04-20 20:36:09 +00001277namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001278// Trait used for the on-disk hash table used in the method pool.
1279class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1280 PCHWriter &Writer;
1281
1282public:
1283 typedef Selector key_type;
1284 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001285
Douglas Gregorc78d3462009-04-24 21:10:55 +00001286 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1287 typedef const data_type& data_type_ref;
1288
1289 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregorc78d3462009-04-24 21:10:55 +00001291 static unsigned ComputeHash(Selector Sel) {
1292 unsigned N = Sel.getNumArgs();
1293 if (N == 0)
1294 ++N;
1295 unsigned R = 5381;
1296 for (unsigned I = 0; I != N; ++I)
1297 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1298 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1299 return R;
1300 }
Mike Stump11289f42009-09-09 15:08:12 +00001301
1302 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001303 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1304 data_type_ref Methods) {
1305 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1306 clang::io::Emit16(Out, KeyLen);
1307 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001308 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001309 Method = Method->Next)
1310 if (Method->Method)
1311 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001312 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001313 Method = Method->Next)
1314 if (Method->Method)
1315 DataLen += 4;
1316 clang::io::Emit16(Out, DataLen);
1317 return std::make_pair(KeyLen, DataLen);
1318 }
Mike Stump11289f42009-09-09 15:08:12 +00001319
Douglas Gregor95c13f52009-04-25 17:48:32 +00001320 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001321 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001322 assert((Start >> 32) == 0 && "Selector key offset too large");
1323 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001324 unsigned N = Sel.getNumArgs();
1325 clang::io::Emit16(Out, N);
1326 if (N == 0)
1327 N = 1;
1328 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001329 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001330 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1331 }
Mike Stump11289f42009-09-09 15:08:12 +00001332
Douglas Gregorc78d3462009-04-24 21:10:55 +00001333 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001334 data_type_ref Methods, unsigned DataLen) {
1335 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001336 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001337 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001338 Method = Method->Next)
1339 if (Method->Method)
1340 ++NumInstanceMethods;
1341
1342 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001343 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001344 Method = Method->Next)
1345 if (Method->Method)
1346 ++NumFactoryMethods;
1347
1348 clang::io::Emit16(Out, NumInstanceMethods);
1349 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001350 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001351 Method = Method->Next)
1352 if (Method->Method)
1353 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001354 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001355 Method = Method->Next)
1356 if (Method->Method)
1357 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001358
1359 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001360 }
1361};
1362} // end anonymous namespace
1363
1364/// \brief Write the method pool into the PCH file.
1365///
1366/// The method pool contains both instance and factory methods, stored
1367/// in an on-disk hash table indexed by the selector.
1368void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1369 using namespace llvm;
1370
1371 // Create and write out the blob that contains the instance and
1372 // factor method pools.
1373 bool Empty = true;
1374 {
1375 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001376
Douglas Gregorc78d3462009-04-24 21:10:55 +00001377 // Create the on-disk hash table representation. Start by
1378 // iterating through the instance method pool.
1379 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001380 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001381 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001382 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001383 InstanceEnd = SemaRef.InstanceMethodPool.end();
1384 Instance != InstanceEnd; ++Instance) {
1385 // Check whether there is a factory method with the same
1386 // selector.
1387 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1388 = SemaRef.FactoryMethodPool.find(Instance->first);
1389
1390 if (Factory == SemaRef.FactoryMethodPool.end())
1391 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001392 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001393 ObjCMethodList()));
1394 else
1395 Generator.insert(Instance->first,
1396 std::make_pair(Instance->second, Factory->second));
1397
Douglas Gregor95c13f52009-04-25 17:48:32 +00001398 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001399 Empty = false;
1400 }
1401
1402 // Now iterate through the factory method pool, to pick up any
1403 // selectors that weren't already in the instance method pool.
1404 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001405 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001406 FactoryEnd = SemaRef.FactoryMethodPool.end();
1407 Factory != FactoryEnd; ++Factory) {
1408 // Check whether there is an instance method with the same
1409 // selector. If so, there is no work to do here.
1410 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1411 = SemaRef.InstanceMethodPool.find(Factory->first);
1412
Douglas Gregor95c13f52009-04-25 17:48:32 +00001413 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001414 Generator.insert(Factory->first,
1415 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001416 ++NumSelectorsInMethodPool;
1417 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001418
1419 Empty = false;
1420 }
1421
Douglas Gregor95c13f52009-04-25 17:48:32 +00001422 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001423 return;
1424
1425 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001426 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001427 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001428 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001429 {
1430 PCHMethodPoolTrait Trait(*this);
1431 llvm::raw_svector_ostream Out(MethodPool);
1432 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001433 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001434 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001435
1436 // For every selector that we have seen but which was not
1437 // written into the hash table, write the selector itself and
1438 // record it's offset.
1439 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1440 if (SelectorOffsets[I] == 0)
1441 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001442 }
1443
1444 // Create a blob abbreviation
1445 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1446 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1450 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1451
Douglas Gregor95c13f52009-04-25 17:48:32 +00001452 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001453 RecordData Record;
1454 Record.push_back(pch::METHOD_POOL);
1455 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001456 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001457 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001458
1459 // Create a blob abbreviation for the selector table offsets.
1460 Abbrev = new BitCodeAbbrev();
1461 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1463 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1464 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1465
1466 // Write the selector offsets table.
1467 Record.clear();
1468 Record.push_back(pch::SELECTOR_OFFSETS);
1469 Record.push_back(SelectorOffsets.size());
1470 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1471 (const char *)&SelectorOffsets.front(),
1472 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001473 }
1474}
1475
Douglas Gregorc5046832009-04-27 18:38:38 +00001476//===----------------------------------------------------------------------===//
1477// Identifier Table Serialization
1478//===----------------------------------------------------------------------===//
1479
Douglas Gregorc78d3462009-04-24 21:10:55 +00001480namespace {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001481class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1482 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001483 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001484
Douglas Gregor1d583f22009-04-28 21:18:29 +00001485 /// \brief Determines whether this is an "interesting" identifier
1486 /// that needs a full IdentifierInfo structure written into the hash
1487 /// table.
1488 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1489 return II->isPoisoned() ||
1490 II->isExtensionToken() ||
1491 II->hasMacroDefinition() ||
1492 II->getObjCOrBuiltinID() ||
1493 II->getFETokenInfo<void>();
1494 }
1495
Douglas Gregore84a9da2009-04-20 20:36:09 +00001496public:
1497 typedef const IdentifierInfo* key_type;
1498 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001499
Douglas Gregore84a9da2009-04-20 20:36:09 +00001500 typedef pch::IdentID data_type;
1501 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001502
1503 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001504 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001505
1506 static unsigned ComputeHash(const IdentifierInfo* II) {
1507 return clang::BernsteinHash(II->getName());
1508 }
Mike Stump11289f42009-09-09 15:08:12 +00001509
1510 std::pair<unsigned,unsigned>
1511 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001512 pch::IdentID ID) {
1513 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001514 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1515 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001516 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001517 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001518 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001519 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001520 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1521 DEnd = IdentifierResolver::end();
1522 D != DEnd; ++D)
1523 DataLen += sizeof(pch::DeclID);
1524 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001525 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001526 // We emit the key length after the data length so that every
1527 // string is preceded by a 16-bit length. This matches the PTH
1528 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001529 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001530 return std::make_pair(KeyLen, DataLen);
1531 }
Mike Stump11289f42009-09-09 15:08:12 +00001532
1533 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001534 unsigned KeyLen) {
1535 // Record the location of the key data. This is used when generating
1536 // the mapping from persistent IDs to strings.
1537 Writer.SetIdentifierOffset(II, Out.tell());
1538 Out.write(II->getName(), KeyLen);
1539 }
Mike Stump11289f42009-09-09 15:08:12 +00001540
1541 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001542 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001543 if (!isInterestingIdentifier(II)) {
1544 clang::io::Emit32(Out, ID << 1);
1545 return;
1546 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001547
Douglas Gregor1d583f22009-04-28 21:18:29 +00001548 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001549 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001550 bool hasMacroDefinition =
1551 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001552 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001553 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001554 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001555 Bits = (Bits << 1) | II->isExtensionToken();
1556 Bits = (Bits << 1) | II->isPoisoned();
1557 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001558 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001559
Douglas Gregorc3366a52009-04-21 23:56:24 +00001560 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001561 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001562
Douglas Gregora868bbd2009-04-21 22:25:48 +00001563 // Emit the declaration IDs in reverse order, because the
1564 // IdentifierResolver provides the declarations as they would be
1565 // visible (e.g., the function "stat" would come before the struct
1566 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1567 // adds declarations to the end of the list (so we need to see the
1568 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001569 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001570 IdentifierResolver::end());
1571 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1572 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001573 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001574 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001575 }
1576};
1577} // end anonymous namespace
1578
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001579/// \brief Write the identifier table into the PCH file.
1580///
1581/// The identifier table consists of a blob containing string data
1582/// (the actual identifiers themselves) and a separate "offsets" index
1583/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001584void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001585 using namespace llvm;
1586
1587 // Create and write out the blob that contains the identifier
1588 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001589 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001590 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001591
Douglas Gregore6648fb2009-04-28 20:33:11 +00001592 // Look for any identifiers that were named while processing the
1593 // headers, but are otherwise not needed. We add these to the hash
1594 // table to enable checking of the predefines buffer in the case
1595 // where the user adds new macro definitions when building the PCH
1596 // file.
1597 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1598 IDEnd = PP.getIdentifierTable().end();
1599 ID != IDEnd; ++ID)
1600 getIdentifierRef(ID->second);
1601
Douglas Gregore84a9da2009-04-20 20:36:09 +00001602 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001603 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001604 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1605 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1606 ID != IDEnd; ++ID) {
1607 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001608 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001609 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001610
Douglas Gregore84a9da2009-04-20 20:36:09 +00001611 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001612 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001613 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001614 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001615 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001616 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001617 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001618 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001619 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001620 }
1621
1622 // Create a blob abbreviation
1623 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1624 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001627 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001628
1629 // Write the identifier table
1630 RecordData Record;
1631 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001632 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001633 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001634 }
1635
1636 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001637 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1638 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1641 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1642
1643 RecordData Record;
1644 Record.push_back(pch::IDENTIFIER_OFFSET);
1645 Record.push_back(IdentifierOffsets.size());
1646 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1647 (const char *)&IdentifierOffsets.front(),
1648 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001649}
1650
Douglas Gregorc5046832009-04-27 18:38:38 +00001651//===----------------------------------------------------------------------===//
1652// General Serialization Routines
1653//===----------------------------------------------------------------------===//
1654
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001655/// \brief Write a record containing the given attributes.
1656void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1657 RecordData Record;
1658 for (; Attr; Attr = Attr->getNext()) {
1659 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1660 Record.push_back(Attr->isInherited());
1661 switch (Attr->getKind()) {
1662 case Attr::Alias:
1663 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1664 break;
1665
1666 case Attr::Aligned:
1667 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1668 break;
1669
1670 case Attr::AlwaysInline:
1671 break;
Mike Stump11289f42009-09-09 15:08:12 +00001672
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001673 case Attr::AnalyzerNoReturn:
1674 break;
1675
1676 case Attr::Annotate:
1677 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1678 break;
1679
1680 case Attr::AsmLabel:
1681 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1682 break;
1683
1684 case Attr::Blocks:
1685 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1686 break;
1687
1688 case Attr::Cleanup:
1689 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1690 break;
1691
1692 case Attr::Const:
1693 break;
1694
1695 case Attr::Constructor:
1696 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1697 break;
1698
1699 case Attr::DLLExport:
1700 case Attr::DLLImport:
1701 case Attr::Deprecated:
1702 break;
1703
1704 case Attr::Destructor:
1705 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1706 break;
1707
1708 case Attr::FastCall:
1709 break;
1710
1711 case Attr::Format: {
1712 const FormatAttr *Format = cast<FormatAttr>(Attr);
1713 AddString(Format->getType(), Record);
1714 Record.push_back(Format->getFormatIdx());
1715 Record.push_back(Format->getFirstArg());
1716 break;
1717 }
1718
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001719 case Attr::FormatArg: {
1720 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1721 Record.push_back(Format->getFormatIdx());
1722 break;
1723 }
1724
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001725 case Attr::Sentinel : {
1726 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1727 Record.push_back(Sentinel->getSentinel());
1728 Record.push_back(Sentinel->getNullPos());
1729 break;
1730 }
Mike Stump11289f42009-09-09 15:08:12 +00001731
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001732 case Attr::GNUInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001733 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001734 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001735 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001736 case Attr::NoReturn:
1737 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001738 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001739 break;
1740
1741 case Attr::NonNull: {
1742 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1743 Record.push_back(NonNull->size());
1744 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1745 break;
1746 }
1747
1748 case Attr::ObjCException:
1749 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001750 case Attr::CFReturnsRetained:
1751 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001752 case Attr::Overloadable:
1753 break;
1754
Anders Carlsson68e0b682009-08-08 18:23:56 +00001755 case Attr::PragmaPack:
1756 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001757 break;
1758
Anders Carlsson68e0b682009-08-08 18:23:56 +00001759 case Attr::Packed:
1760 break;
Mike Stump11289f42009-09-09 15:08:12 +00001761
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001762 case Attr::Pure:
1763 break;
1764
1765 case Attr::Regparm:
1766 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1767 break;
Mike Stump11289f42009-09-09 15:08:12 +00001768
Nate Begemanf2758702009-06-26 06:32:41 +00001769 case Attr::ReqdWorkGroupSize:
1770 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1771 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1772 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1773 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001774
1775 case Attr::Section:
1776 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1777 break;
1778
1779 case Attr::StdCall:
1780 case Attr::TransparentUnion:
1781 case Attr::Unavailable:
1782 case Attr::Unused:
1783 case Attr::Used:
1784 break;
1785
1786 case Attr::Visibility:
1787 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001788 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001789 break;
1790
1791 case Attr::WarnUnusedResult:
1792 case Attr::Weak:
1793 case Attr::WeakImport:
1794 break;
1795 }
1796 }
1797
Douglas Gregor8f45df52009-04-16 22:23:12 +00001798 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001799}
1800
1801void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1802 Record.push_back(Str.size());
1803 Record.insert(Record.end(), Str.begin(), Str.end());
1804}
1805
Douglas Gregore84a9da2009-04-20 20:36:09 +00001806/// \brief Note that the identifier II occurs at the given offset
1807/// within the identifier table.
1808void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001809 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001810}
1811
Douglas Gregor95c13f52009-04-25 17:48:32 +00001812/// \brief Note that the selector Sel occurs at the given offset
1813/// within the method pool/selector table.
1814void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1815 unsigned ID = SelectorIDs[Sel];
1816 assert(ID && "Unknown selector");
1817 SelectorOffsets[ID - 1] = Offset;
1818}
1819
Mike Stump11289f42009-09-09 15:08:12 +00001820PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1821 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001822 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1823 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001824
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001825void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1826 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001827 using namespace llvm;
1828
Douglas Gregor162dd022009-04-20 15:53:59 +00001829 ASTContext &Context = SemaRef.Context;
1830 Preprocessor &PP = SemaRef.PP;
1831
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001832 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001833 Stream.Emit((unsigned)'C', 8);
1834 Stream.Emit((unsigned)'P', 8);
1835 Stream.Emit((unsigned)'C', 8);
1836 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001837
Chris Lattner28fa4e62009-04-26 22:26:21 +00001838 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001839
1840 // The translation unit is the first declaration we'll emit.
1841 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Douglas Gregor12bfa382009-10-17 00:13:19 +00001842 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001843
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001844 // Make sure that we emit IdentifierInfos (and any attached
1845 // declarations) for builtins.
1846 {
1847 IdentifierTable &Table = PP.getIdentifierTable();
1848 llvm::SmallVector<const char *, 32> BuiltinNames;
1849 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1850 Context.getLangOptions().NoBuiltin);
1851 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1852 getIdentifierRef(&Table.get(BuiltinNames[I]));
1853 }
1854
Chris Lattner0c797362009-09-08 18:19:27 +00001855 // Build a record containing all of the tentative definitions in this file, in
1856 // TentativeDefinitionList order. Generally, this record will be empty for
1857 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001858 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001859 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1860 VarDecl *VD =
1861 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1862 if (VD) AddDeclRef(VD, TentativeDefinitions);
1863 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001864
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001865 // Build a record containing all of the locally-scoped external
1866 // declarations in this header file. Generally, this record will be
1867 // empty.
1868 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001869 // FIXME: This is filling in the PCH file in densemap order which is
1870 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001871 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001872 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1873 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1874 TD != TDEnd; ++TD)
1875 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1876
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001877 // Build a record containing all of the ext_vector declarations.
1878 RecordData ExtVectorDecls;
1879 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1880 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1881
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001882 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001883 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001884 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001885 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001886 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001887 if (StatCalls && !isysroot)
1888 WriteStatCache(*StatCalls, isysroot);
1889 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Mike Stump11289f42009-09-09 15:08:12 +00001890 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001891 // Write the record of special types.
1892 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001893
Steve Naroffc277ad12009-07-18 15:33:26 +00001894 AddTypeRef(Context.getBuiltinVaListType(), Record);
1895 AddTypeRef(Context.getObjCIdType(), Record);
1896 AddTypeRef(Context.getObjCSelType(), Record);
1897 AddTypeRef(Context.getObjCProtoType(), Record);
1898 AddTypeRef(Context.getObjCClassType(), Record);
1899 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1900 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1901 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001902 AddTypeRef(Context.getjmp_bufType(), Record);
1903 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001904 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1905 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00001906 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001907
Douglas Gregor1970d882009-04-26 03:49:13 +00001908 // Keep writing types and declarations until all types and
1909 // declarations have been written.
Douglas Gregor12bfa382009-10-17 00:13:19 +00001910 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
1911 WriteDeclsBlockAbbrevs();
1912 while (!DeclTypesToEmit.empty()) {
1913 DeclOrType DOT = DeclTypesToEmit.front();
1914 DeclTypesToEmit.pop();
1915 if (DOT.isType())
1916 WriteType(DOT.getType());
1917 else
1918 WriteDecl(Context, DOT.getDecl());
1919 }
1920 Stream.ExitBlock();
1921
Douglas Gregor45053152009-10-17 17:25:45 +00001922 WritePreprocessor(PP);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001923 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001924 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00001925
1926 // Write the type offsets array
1927 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1928 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1929 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1931 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1932 Record.clear();
1933 Record.push_back(pch::TYPE_OFFSET);
1934 Record.push_back(TypeOffsets.size());
1935 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001936 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00001937 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00001938
Douglas Gregor745ed142009-04-25 18:35:21 +00001939 // Write the declaration offsets array
1940 Abbrev = new BitCodeAbbrev();
1941 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1944 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1945 Record.clear();
1946 Record.push_back(pch::DECL_OFFSET);
1947 Record.push_back(DeclOffsets.size());
1948 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001949 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00001950 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00001951
Douglas Gregord4df8652009-04-22 22:02:47 +00001952 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001953 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00001954 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00001955
1956 // Write the record containing tentative definitions.
1957 if (!TentativeDefinitions.empty())
1958 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001959
1960 // Write the record containing locally-scoped external definitions.
1961 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00001962 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001963 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001964
1965 // Write the record containing ext_vector type names.
1966 if (!ExtVectorDecls.empty())
1967 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00001968
Douglas Gregor08f01292009-04-17 22:13:46 +00001969 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00001970 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00001971 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001972 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001973 Record.push_back(NumLexicalDeclContexts);
1974 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00001975 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001976 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001977}
1978
1979void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1980 Record.push_back(Loc.getRawEncoding());
1981}
1982
1983void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1984 Record.push_back(Value.getBitWidth());
1985 unsigned N = Value.getNumWords();
1986 const uint64_t* Words = Value.getRawData();
1987 for (unsigned I = 0; I != N; ++I)
1988 Record.push_back(Words[I]);
1989}
1990
Douglas Gregor1daeb692009-04-13 18:14:40 +00001991void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1992 Record.push_back(Value.isUnsigned());
1993 AddAPInt(Value, Record);
1994}
1995
Douglas Gregore0a3a512009-04-14 21:55:33 +00001996void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1997 AddAPInt(Value.bitcastToAPInt(), Record);
1998}
1999
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002000void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002001 Record.push_back(getIdentifierRef(II));
2002}
2003
2004pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2005 if (II == 0)
2006 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002007
2008 pch::IdentID &ID = IdentifierIDs[II];
2009 if (ID == 0)
2010 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002011 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002012}
2013
Steve Naroff2ddea052009-04-23 10:39:46 +00002014void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2015 if (SelRef.getAsOpaquePtr() == 0) {
2016 Record.push_back(0);
2017 return;
2018 }
2019
2020 pch::SelectorID &SID = SelectorIDs[SelRef];
2021 if (SID == 0) {
2022 SID = SelectorIDs.size();
2023 SelVector.push_back(SelRef);
2024 }
2025 Record.push_back(SID);
2026}
2027
John McCall8f115c62009-10-16 21:56:05 +00002028void PCHWriter::AddDeclaratorInfo(DeclaratorInfo *DInfo, RecordData &Record) {
2029 if (DInfo == 0) {
2030 AddTypeRef(QualType(), Record);
2031 return;
2032 }
2033
2034 AddTypeRef(DInfo->getTypeLoc().getSourceType(), Record);
2035 TypeLocWriter TLW(*this, Record);
2036 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
2037 TLW.Visit(TL);
2038}
2039
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002040void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2041 if (T.isNull()) {
2042 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2043 return;
2044 }
2045
John McCall8ccfcb52009-09-24 19:53:00 +00002046 unsigned FastQuals = T.getFastQualifiers();
2047 T.removeFastQualifiers();
2048
2049 if (T.hasNonFastQualifiers()) {
2050 pch::TypeID &ID = TypeIDs[T];
2051 if (ID == 0) {
2052 // We haven't seen these qualifiers applied to this type before.
2053 // Assign it a new ID. This is the only time we enqueue a
2054 // qualified type, and it has no CV qualifiers.
2055 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002056 DeclTypesToEmit.push(T);
John McCall8ccfcb52009-09-24 19:53:00 +00002057 }
2058
2059 // Encode the type qualifiers in the type reference.
2060 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2061 return;
2062 }
2063
2064 assert(!T.hasQualifiers());
2065
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002066 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002067 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002068 switch (BT->getKind()) {
2069 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2070 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2071 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2072 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2073 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2074 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2075 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2076 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002077 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002078 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2079 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2080 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2081 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2082 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2083 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2084 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002085 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002086 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2087 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2088 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002089 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002090 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2091 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002092 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2093 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002094 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2095 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002096 case BuiltinType::UndeducedAuto:
2097 assert(0 && "Should not see undeduced auto here");
2098 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002099 }
2100
John McCall8ccfcb52009-09-24 19:53:00 +00002101 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002102 return;
2103 }
2104
John McCall8ccfcb52009-09-24 19:53:00 +00002105 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002106 if (ID == 0) {
2107 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002108 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002109 ID = NextTypeID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002110 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002111 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002112
2113 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002114 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002115}
2116
2117void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2118 if (D == 0) {
2119 Record.push_back(0);
2120 return;
2121 }
2122
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002123 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002124 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002125 // We haven't seen this declaration before. Give it a new ID and
2126 // enqueue it in the list of declarations to emit.
2127 ID = DeclIDs.size();
Douglas Gregor12bfa382009-10-17 00:13:19 +00002128 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002129 }
2130
2131 Record.push_back(ID);
2132}
2133
Douglas Gregore84a9da2009-04-20 20:36:09 +00002134pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2135 if (D == 0)
2136 return 0;
2137
2138 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2139 return DeclIDs[D];
2140}
2141
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002142void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002143 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002144 Record.push_back(Name.getNameKind());
2145 switch (Name.getNameKind()) {
2146 case DeclarationName::Identifier:
2147 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2148 break;
2149
2150 case DeclarationName::ObjCZeroArgSelector:
2151 case DeclarationName::ObjCOneArgSelector:
2152 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002153 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002154 break;
2155
2156 case DeclarationName::CXXConstructorName:
2157 case DeclarationName::CXXDestructorName:
2158 case DeclarationName::CXXConversionFunctionName:
2159 AddTypeRef(Name.getCXXNameType(), Record);
2160 break;
2161
2162 case DeclarationName::CXXOperatorName:
2163 Record.push_back(Name.getCXXOverloadedOperator());
2164 break;
2165
2166 case DeclarationName::CXXUsingDirective:
2167 // No extra data to emit
2168 break;
2169 }
2170}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002171