blob: 5d5e09bc6674796ea57b6c7fc18315edb36d89b6 [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"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000024#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000030#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000036#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000043
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044namespace {
45 class VISIBILITY_HIDDEN PCHTypeWriter {
46 PCHWriter &Writer;
47 PCHWriter::RecordData &Record;
48
49 public:
50 /// \brief Type code that corresponds to the record generated.
51 pch::TypeCode Code;
52
Mike Stump11289f42009-09-09 15:08:12 +000053 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregorc5046832009-04-27 18:38:38 +000054 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055
56 void VisitArrayType(const ArrayType *T);
57 void VisitFunctionType(const FunctionType *T);
58 void VisitTagType(const TagType *T);
59
60#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
61#define ABSTRACT_TYPE(Class, Base)
62#define DEPENDENT_TYPE(Class, Base)
63#include "clang/AST/TypeNodes.def"
64 };
65}
66
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
68 assert(false && "Built-in types are never serialized");
69}
70
71void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
72 Record.push_back(T->getWidth());
73 Record.push_back(T->isSigned());
74 Code = pch::TYPE_FIXED_WIDTH_INT;
75}
76
77void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
78 Writer.AddTypeRef(T->getElementType(), Record);
79 Code = pch::TYPE_COMPLEX;
80}
81
82void PCHTypeWriter::VisitPointerType(const PointerType *T) {
83 Writer.AddTypeRef(T->getPointeeType(), Record);
84 Code = pch::TYPE_POINTER;
85}
86
87void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000088 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089 Code = pch::TYPE_BLOCK_POINTER;
90}
91
92void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
93 Writer.AddTypeRef(T->getPointeeType(), Record);
94 Code = pch::TYPE_LVALUE_REFERENCE;
95}
96
97void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
98 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_RVALUE_REFERENCE;
100}
101
102void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105 Code = pch::TYPE_MEMBER_POINTER;
106}
107
108void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
109 Writer.AddTypeRef(T->getElementType(), Record);
110 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000111 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112}
113
114void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
115 VisitArrayType(T);
116 Writer.AddAPInt(T->getSize(), Record);
117 Code = pch::TYPE_CONSTANT_ARRAY;
118}
119
120void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
121 VisitArrayType(T);
122 Code = pch::TYPE_INCOMPLETE_ARRAY;
123}
124
125void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
126 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000127 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
128 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000129 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130 Code = pch::TYPE_VARIABLE_ARRAY;
131}
132
133void PCHTypeWriter::VisitVectorType(const VectorType *T) {
134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getNumElements());
136 Code = pch::TYPE_VECTOR;
137}
138
139void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
140 VisitVectorType(T);
141 Code = pch::TYPE_EXT_VECTOR;
142}
143
144void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
145 Writer.AddTypeRef(T->getResultType(), Record);
146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000160 Record.push_back(T->hasExceptionSpec());
161 Record.push_back(T->hasAnyExceptionSpec());
162 Record.push_back(T->getNumExceptions());
163 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
164 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 Code = pch::TYPE_FUNCTION_PROTO;
166}
167
168void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
169 Writer.AddDeclRef(T->getDecl(), Record);
170 Code = pch::TYPE_TYPEDEF;
171}
172
173void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000174 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175 Code = pch::TYPE_TYPEOF_EXPR;
176}
177
178void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
179 Writer.AddTypeRef(T->getUnderlyingType(), Record);
180 Code = pch::TYPE_TYPEOF;
181}
182
Anders Carlsson81df7b82009-06-24 19:06:50 +0000183void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
184 Writer.AddStmt(T->getUnderlyingExpr());
185 Code = pch::TYPE_DECLTYPE;
186}
187
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000188void PCHTypeWriter::VisitTagType(const TagType *T) {
189 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000190 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000191 "Cannot serialize in the middle of a type definition");
192}
193
194void PCHTypeWriter::VisitRecordType(const RecordType *T) {
195 VisitTagType(T);
196 Code = pch::TYPE_RECORD;
197}
198
199void PCHTypeWriter::VisitEnumType(const EnumType *T) {
200 VisitTagType(T);
201 Code = pch::TYPE_ENUM;
202}
203
John McCallfcc33b02009-09-05 00:15:47 +0000204void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
205 Writer.AddTypeRef(T->getUnderlyingType(), Record);
206 Record.push_back(T->getTagKind());
207 Code = pch::TYPE_ELABORATED;
208}
209
Mike Stump11289f42009-09-09 15:08:12 +0000210void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000211PCHTypeWriter::VisitTemplateSpecializationType(
212 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000213 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 assert(false && "Cannot serialize template specialization types");
215}
216
217void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000218 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219 assert(false && "Cannot serialize qualified name types");
220}
221
222void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
223 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000225 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
226 E = T->qual_end(); I != E; ++I)
227 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000228 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229}
230
Steve Narofffb4330f2009-06-17 22:40:22 +0000231void
232PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000233 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000235 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000236 E = T->qual_end(); I != E; ++I)
237 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000238 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239}
240
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000241void PCHTypeWriter::VisitObjCProtocolListType(const ObjCProtocolListType *T) {
242 Writer.AddTypeRef(T->getBaseType(), Record);
243 Record.push_back(T->getNumProtocols());
244 for (ObjCProtocolListType::qual_iterator I = T->qual_begin(),
245 E = T->qual_end(); I != E; ++I)
246 Writer.AddDeclRef(*I, Record);
247 Code = pch::TYPE_OBJC_PROTOCOL_LIST;
248}
249
Chris Lattner19cea4e2009-04-22 05:57:30 +0000250//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251// PCHWriter Implementation
252//===----------------------------------------------------------------------===//
253
Chris Lattner28fa4e62009-04-26 22:26:21 +0000254static void EmitBlockID(unsigned ID, const char *Name,
255 llvm::BitstreamWriter &Stream,
256 PCHWriter::RecordData &Record) {
257 Record.clear();
258 Record.push_back(ID);
259 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
260
261 // Emit the block name if present.
262 if (Name == 0 || Name[0] == 0) return;
263 Record.clear();
264 while (*Name)
265 Record.push_back(*Name++);
266 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
267}
268
269static void EmitRecordID(unsigned ID, const char *Name,
270 llvm::BitstreamWriter &Stream,
271 PCHWriter::RecordData &Record) {
272 Record.clear();
273 Record.push_back(ID);
274 while (*Name)
275 Record.push_back(*Name++);
276 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000277}
278
279static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
280 PCHWriter::RecordData &Record) {
281#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
282 RECORD(STMT_STOP);
283 RECORD(STMT_NULL_PTR);
284 RECORD(STMT_NULL);
285 RECORD(STMT_COMPOUND);
286 RECORD(STMT_CASE);
287 RECORD(STMT_DEFAULT);
288 RECORD(STMT_LABEL);
289 RECORD(STMT_IF);
290 RECORD(STMT_SWITCH);
291 RECORD(STMT_WHILE);
292 RECORD(STMT_DO);
293 RECORD(STMT_FOR);
294 RECORD(STMT_GOTO);
295 RECORD(STMT_INDIRECT_GOTO);
296 RECORD(STMT_CONTINUE);
297 RECORD(STMT_BREAK);
298 RECORD(STMT_RETURN);
299 RECORD(STMT_DECL);
300 RECORD(STMT_ASM);
301 RECORD(EXPR_PREDEFINED);
302 RECORD(EXPR_DECL_REF);
303 RECORD(EXPR_INTEGER_LITERAL);
304 RECORD(EXPR_FLOATING_LITERAL);
305 RECORD(EXPR_IMAGINARY_LITERAL);
306 RECORD(EXPR_STRING_LITERAL);
307 RECORD(EXPR_CHARACTER_LITERAL);
308 RECORD(EXPR_PAREN);
309 RECORD(EXPR_UNARY_OPERATOR);
310 RECORD(EXPR_SIZEOF_ALIGN_OF);
311 RECORD(EXPR_ARRAY_SUBSCRIPT);
312 RECORD(EXPR_CALL);
313 RECORD(EXPR_MEMBER);
314 RECORD(EXPR_BINARY_OPERATOR);
315 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
316 RECORD(EXPR_CONDITIONAL_OPERATOR);
317 RECORD(EXPR_IMPLICIT_CAST);
318 RECORD(EXPR_CSTYLE_CAST);
319 RECORD(EXPR_COMPOUND_LITERAL);
320 RECORD(EXPR_EXT_VECTOR_ELEMENT);
321 RECORD(EXPR_INIT_LIST);
322 RECORD(EXPR_DESIGNATED_INIT);
323 RECORD(EXPR_IMPLICIT_VALUE_INIT);
324 RECORD(EXPR_VA_ARG);
325 RECORD(EXPR_ADDR_LABEL);
326 RECORD(EXPR_STMT);
327 RECORD(EXPR_TYPES_COMPATIBLE);
328 RECORD(EXPR_CHOOSE);
329 RECORD(EXPR_GNU_NULL);
330 RECORD(EXPR_SHUFFLE_VECTOR);
331 RECORD(EXPR_BLOCK);
332 RECORD(EXPR_BLOCK_DECL_REF);
333 RECORD(EXPR_OBJC_STRING_LITERAL);
334 RECORD(EXPR_OBJC_ENCODE);
335 RECORD(EXPR_OBJC_SELECTOR_EXPR);
336 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
337 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
338 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
339 RECORD(EXPR_OBJC_KVC_REF_EXPR);
340 RECORD(EXPR_OBJC_MESSAGE_EXPR);
341 RECORD(EXPR_OBJC_SUPER_EXPR);
342 RECORD(STMT_OBJC_FOR_COLLECTION);
343 RECORD(STMT_OBJC_CATCH);
344 RECORD(STMT_OBJC_FINALLY);
345 RECORD(STMT_OBJC_AT_TRY);
346 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
347 RECORD(STMT_OBJC_AT_THROW);
348#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000349}
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner28fa4e62009-04-26 22:26:21 +0000351void PCHWriter::WriteBlockInfoBlock() {
352 RecordData Record;
353 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner64031982009-04-27 00:40:25 +0000355#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000356#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattner28fa4e62009-04-26 22:26:21 +0000358 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000359 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000360 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000361 RECORD(TYPE_OFFSET);
362 RECORD(DECL_OFFSET);
363 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000364 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000365 RECORD(IDENTIFIER_OFFSET);
366 RECORD(IDENTIFIER_TABLE);
367 RECORD(EXTERNAL_DEFINITIONS);
368 RECORD(SPECIAL_TYPES);
369 RECORD(STATISTICS);
370 RECORD(TENTATIVE_DEFINITIONS);
371 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
372 RECORD(SELECTOR_OFFSETS);
373 RECORD(METHOD_POOL);
374 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000375 RECORD(SOURCE_LOCATION_OFFSETS);
376 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000377 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000378 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000379 RECORD(COMMENT_RANGES);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000380 RECORD(SVN_BRANCH_REVISION);
381
Chris Lattner28fa4e62009-04-26 22:26:21 +0000382 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000383 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000384 RECORD(SM_SLOC_FILE_ENTRY);
385 RECORD(SM_SLOC_BUFFER_ENTRY);
386 RECORD(SM_SLOC_BUFFER_BLOB);
387 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
388 RECORD(SM_LINE_TABLE);
389 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattner28fa4e62009-04-26 22:26:21 +0000391 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000392 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000393 RECORD(PP_MACRO_OBJECT_LIKE);
394 RECORD(PP_MACRO_FUNCTION_LIKE);
395 RECORD(PP_TOKEN);
396
397 // Types block.
Chris Lattner64031982009-04-27 00:40:25 +0000398 BLOCK(TYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000399 RECORD(TYPE_EXT_QUAL);
400 RECORD(TYPE_FIXED_WIDTH_INT);
401 RECORD(TYPE_COMPLEX);
402 RECORD(TYPE_POINTER);
403 RECORD(TYPE_BLOCK_POINTER);
404 RECORD(TYPE_LVALUE_REFERENCE);
405 RECORD(TYPE_RVALUE_REFERENCE);
406 RECORD(TYPE_MEMBER_POINTER);
407 RECORD(TYPE_CONSTANT_ARRAY);
408 RECORD(TYPE_INCOMPLETE_ARRAY);
409 RECORD(TYPE_VARIABLE_ARRAY);
410 RECORD(TYPE_VECTOR);
411 RECORD(TYPE_EXT_VECTOR);
412 RECORD(TYPE_FUNCTION_PROTO);
413 RECORD(TYPE_FUNCTION_NO_PROTO);
414 RECORD(TYPE_TYPEDEF);
415 RECORD(TYPE_TYPEOF_EXPR);
416 RECORD(TYPE_TYPEOF);
417 RECORD(TYPE_RECORD);
418 RECORD(TYPE_ENUM);
419 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000420 RECORD(TYPE_OBJC_OBJECT_POINTER);
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000421 RECORD(TYPE_OBJC_PROTOCOL_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000422 // Statements and Exprs can occur in the Types block.
423 AddStmtsExprs(Stream, Record);
424
Chris Lattner28fa4e62009-04-26 22:26:21 +0000425 // Decls block.
Chris Lattner64031982009-04-27 00:40:25 +0000426 BLOCK(DECLS_BLOCK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000427 RECORD(DECL_ATTR);
428 RECORD(DECL_TRANSLATION_UNIT);
429 RECORD(DECL_TYPEDEF);
430 RECORD(DECL_ENUM);
431 RECORD(DECL_RECORD);
432 RECORD(DECL_ENUM_CONSTANT);
433 RECORD(DECL_FUNCTION);
434 RECORD(DECL_OBJC_METHOD);
435 RECORD(DECL_OBJC_INTERFACE);
436 RECORD(DECL_OBJC_PROTOCOL);
437 RECORD(DECL_OBJC_IVAR);
438 RECORD(DECL_OBJC_AT_DEFS_FIELD);
439 RECORD(DECL_OBJC_CLASS);
440 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
441 RECORD(DECL_OBJC_CATEGORY);
442 RECORD(DECL_OBJC_CATEGORY_IMPL);
443 RECORD(DECL_OBJC_IMPLEMENTATION);
444 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
445 RECORD(DECL_OBJC_PROPERTY);
446 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000447 RECORD(DECL_FIELD);
448 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000449 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000450 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000451 RECORD(DECL_ORIGINAL_PARM_VAR);
452 RECORD(DECL_FILE_SCOPE_ASM);
453 RECORD(DECL_BLOCK);
454 RECORD(DECL_CONTEXT_LEXICAL);
455 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000456 // Statements and Exprs can occur in the Decls block.
457 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000458#undef RECORD
459#undef BLOCK
460 Stream.ExitBlock();
461}
462
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000463/// \brief Adjusts the given filename to only write out the portion of the
464/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000465///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000466/// \param Filename the file name to adjust.
467///
468/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
469/// the returned filename will be adjusted by this system root.
470///
471/// \returns either the original filename (if it needs no adjustment) or the
472/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000473static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000474adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
475 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000476
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000477 if (!isysroot)
478 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000480 // Verify that the filename and the system root have the same prefix.
481 unsigned Pos = 0;
482 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
483 if (Filename[Pos] != isysroot[Pos])
484 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000485
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000486 // We hit the end of the filename before we hit the end of the system root.
487 if (!Filename[Pos])
488 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000490 // If the file name has a '/' at the current position, skip over the '/'.
491 // We distinguish sysroot-based includes from absolute includes by the
492 // absence of '/' at the beginning of sysroot-based includes.
493 if (Filename[Pos] == '/')
494 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000495
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000496 return Filename + Pos;
497}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000498
Douglas Gregor7b71e632009-04-27 22:23:34 +0000499/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000500void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000501 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000502
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000503 // Metadata
504 const TargetInfo &Target = Context.Target;
505 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
506 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
507 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
508 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
509 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
510 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
511 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
512 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
513 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000515 RecordData Record;
516 Record.push_back(pch::METADATA);
517 Record.push_back(pch::VERSION_MAJOR);
518 Record.push_back(pch::VERSION_MINOR);
519 Record.push_back(CLANG_VERSION_MAJOR);
520 Record.push_back(CLANG_VERSION_MINOR);
521 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000522 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000523 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregor45fe0362009-05-12 01:31:05 +0000525 // Original file name
526 SourceManager &SM = Context.getSourceManager();
527 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
528 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
529 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
530 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
531 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
532
533 llvm::sys::Path MainFilePath(MainFile->getName());
534 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Douglas Gregor45fe0362009-05-12 01:31:05 +0000536 if (!MainFilePath.isAbsolute()) {
537 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000538 P.appendComponent(MainFilePath.str());
539 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000540 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000541 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000542 }
543
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000544 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000545 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000546 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000547 RecordData Record;
548 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000549 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000550 }
Douglas Gregord54f3a12009-10-05 21:07:28 +0000551
552 // Subversion branch/version information.
553 BitCodeAbbrev *SvnAbbrev = new BitCodeAbbrev();
554 SvnAbbrev->Add(BitCodeAbbrevOp(pch::SVN_BRANCH_REVISION));
555 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // SVN revision
556 SvnAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
557 unsigned SvnAbbrevCode = Stream.EmitAbbrev(SvnAbbrev);
558 Record.clear();
559 Record.push_back(pch::SVN_BRANCH_REVISION);
560 Record.push_back(getClangSubversionRevision());
561 Stream.EmitRecordWithBlob(SvnAbbrevCode, Record, getClangSubversionPath());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000562}
563
564/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000565void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
566 RecordData Record;
567 Record.push_back(LangOpts.Trigraphs);
568 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
569 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
570 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
571 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
572 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
573 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
574 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
575 Record.push_back(LangOpts.C99); // C99 Support
576 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
577 Record.push_back(LangOpts.CPlusPlus); // C++ Support
578 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000579 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000580
Douglas Gregor55abb232009-04-10 20:39:37 +0000581 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
582 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
583 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000584
Douglas Gregor55abb232009-04-10 20:39:37 +0000585 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000586 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
587 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000588 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000589 Record.push_back(LangOpts.Exceptions); // Support exception handling.
590
591 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
592 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
593 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
594
Chris Lattner258172e2009-04-27 07:35:58 +0000595 // Whether static initializers are protected by locks.
596 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000597 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000598 Record.push_back(LangOpts.Blocks); // block extension to C
599 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
600 // they are unused.
601 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
602 // (modulo the platform support).
603
604 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
605 // signed integer arithmetic overflows.
606
607 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
608 // may be ripped out at any time.
609
610 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000611 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000612 // defined.
613 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
614 // opposed to __DYNAMIC__).
615 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
616
617 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
618 // used (instead of C99 semantics).
619 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000620 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
621 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000622 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
623 // unsigned type
Douglas Gregor55abb232009-04-10 20:39:37 +0000624 Record.push_back(LangOpts.getGCMode());
625 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000626 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000627 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000628 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000629 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000630 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000631}
632
Douglas Gregora7f71a92009-04-10 03:52:48 +0000633//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000634// stat cache Serialization
635//===----------------------------------------------------------------------===//
636
637namespace {
638// Trait used for the on-disk hash table of stat cache results.
639class VISIBILITY_HIDDEN PCHStatCacheTrait {
640public:
641 typedef const char * key_type;
642 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000643
Douglas Gregorc5046832009-04-27 18:38:38 +0000644 typedef std::pair<int, struct stat> data_type;
645 typedef const data_type& data_type_ref;
646
647 static unsigned ComputeHash(const char *path) {
648 return BernsteinHash(path);
649 }
Mike Stump11289f42009-09-09 15:08:12 +0000650
651 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000652 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
653 data_type_ref Data) {
654 unsigned StrLen = strlen(path);
655 clang::io::Emit16(Out, StrLen);
656 unsigned DataLen = 1; // result value
657 if (Data.first == 0)
658 DataLen += 4 + 4 + 2 + 8 + 8;
659 clang::io::Emit8(Out, DataLen);
660 return std::make_pair(StrLen + 1, DataLen);
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Douglas Gregorc5046832009-04-27 18:38:38 +0000663 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
664 Out.write(path, KeyLen);
665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Douglas Gregorc5046832009-04-27 18:38:38 +0000667 void EmitData(llvm::raw_ostream& Out, key_type_ref,
668 data_type_ref Data, unsigned DataLen) {
669 using namespace clang::io;
670 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000671
Douglas Gregorc5046832009-04-27 18:38:38 +0000672 // Result of stat()
673 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregorc5046832009-04-27 18:38:38 +0000675 if (Data.first == 0) {
676 Emit32(Out, (uint32_t) Data.second.st_ino);
677 Emit32(Out, (uint32_t) Data.second.st_dev);
678 Emit16(Out, (uint16_t) Data.second.st_mode);
679 Emit64(Out, (uint64_t) Data.second.st_mtime);
680 Emit64(Out, (uint64_t) Data.second.st_size);
681 }
682
683 assert(Out.tell() - Start == DataLen && "Wrong data length");
684 }
685};
686} // end anonymous namespace
687
688/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000689void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
690 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000691 // Build the on-disk hash table containing information about every
692 // stat() call.
693 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
694 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000695 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000696 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000697 Stat != StatEnd; ++Stat, ++NumStatEntries) {
698 const char *Filename = Stat->first();
699 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
700 Generator.insert(Filename, Stat->second);
701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Douglas Gregorc5046832009-04-27 18:38:38 +0000703 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000704 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000705 uint32_t BucketOffset;
706 {
707 llvm::raw_svector_ostream Out(StatCacheData);
708 // Make sure that no bucket is at offset 0
709 clang::io::Emit32(Out, 0);
710 BucketOffset = Generator.Emit(Out);
711 }
712
713 // Create a blob abbreviation
714 using namespace llvm;
715 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
716 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
717 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
720 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
721
722 // Write the stat cache
723 RecordData Record;
724 Record.push_back(pch::STAT_CACHE);
725 Record.push_back(BucketOffset);
726 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000727 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000728}
729
730//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000731// Source Manager Serialization
732//===----------------------------------------------------------------------===//
733
734/// \brief Create an abbreviation for the SLocEntry that refers to a
735/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000736static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000737 using namespace llvm;
738 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
739 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000745 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000746}
747
748/// \brief Create an abbreviation for the SLocEntry that refers to a
749/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000750static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000751 using namespace llvm;
752 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
753 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000759 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000760}
761
762/// \brief Create an abbreviation for the SLocEntry that refers to a
763/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000764static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000765 using namespace llvm;
766 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
767 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000769 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000770}
771
772/// \brief Create an abbreviation for the SLocEntry that refers to an
773/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000774static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000775 using namespace llvm;
776 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
777 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000783 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000784}
785
786/// \brief Writes the block containing the serialized form of the
787/// source manager.
788///
789/// TODO: We should probably use an on-disk hash table (stored in a
790/// blob), indexed based on the file name, so that we only create
791/// entries for files that we actually need. In the common case (no
792/// errors), we probably won't have to create file entries for any of
793/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000794void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000795 const Preprocessor &PP,
796 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000797 RecordData Record;
798
Chris Lattner0910e3b2009-04-10 17:16:57 +0000799 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000800 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000801
802 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000803 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
804 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
805 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
806 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000807
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000808 // Write the line table.
809 if (SourceMgr.hasLineTable()) {
810 LineTableInfo &LineTable = SourceMgr.getLineTable();
811
812 // Emit the file names
813 Record.push_back(LineTable.getNumFilenames());
814 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
815 // Emit the file name
816 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000817 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000818 unsigned FilenameLen = Filename? strlen(Filename) : 0;
819 Record.push_back(FilenameLen);
820 if (FilenameLen)
821 Record.insert(Record.end(), Filename, Filename + FilenameLen);
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000824 // Emit the line entries
825 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
826 L != LEnd; ++L) {
827 // Emit the file ID
828 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000829
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000830 // Emit the line entries
831 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000832 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000833 LEEnd = L->second.end();
834 LE != LEEnd; ++LE) {
835 Record.push_back(LE->FileOffset);
836 Record.push_back(LE->LineNo);
837 Record.push_back(LE->FilenameID);
838 Record.push_back((unsigned)LE->FileKind);
839 Record.push_back(LE->IncludeOffset);
840 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000841 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000842 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000843 }
844
Douglas Gregor258ae542009-04-27 06:38:32 +0000845 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000846 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000847 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000848 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000849 E = HS.header_file_end();
850 I != E; ++I) {
851 Record.push_back(I->isImport);
852 Record.push_back(I->DirInfo);
853 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +0000854 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000855 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
856 Record.clear();
857 }
858
Douglas Gregor258ae542009-04-27 06:38:32 +0000859 // Write out the source location entry table. We skip the first
860 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +0000861 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +0000862 RecordData PreloadSLocs;
863 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Mike Stump11289f42009-09-09 15:08:12 +0000864 for (SourceManager::sloc_entry_iterator
Douglas Gregor258ae542009-04-27 06:38:32 +0000865 SLoc = SourceMgr.sloc_entry_begin() + 1,
866 SLocEnd = SourceMgr.sloc_entry_end();
867 SLoc != SLocEnd; ++SLoc) {
868 // Record the offset of this source-location entry.
869 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
870
871 // Figure out which record code to use.
872 unsigned Code;
873 if (SLoc->isFile()) {
874 if (SLoc->getFile().getContentCache()->Entry)
875 Code = pch::SM_SLOC_FILE_ENTRY;
876 else
877 Code = pch::SM_SLOC_BUFFER_ENTRY;
878 } else
879 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
880 Record.clear();
881 Record.push_back(Code);
882
883 Record.push_back(SLoc->getOffset());
884 if (SLoc->isFile()) {
885 const SrcMgr::FileInfo &File = SLoc->getFile();
886 Record.push_back(File.getIncludeLoc().getRawEncoding());
887 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
888 Record.push_back(File.hasLineDirectives());
889
890 const SrcMgr::ContentCache *Content = File.getContentCache();
891 if (Content->Entry) {
892 // The source location entry is a file. The blob associated
893 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000895 // Turn the file name into an absolute path, if it isn't already.
896 const char *Filename = Content->Entry->getName();
897 llvm::sys::Path FilePath(Filename, strlen(Filename));
898 std::string FilenameStr;
899 if (!FilePath.isAbsolute()) {
900 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000901 P.appendComponent(FilePath.str());
902 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000903 Filename = FilenameStr.c_str();
904 }
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000906 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000907 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +0000908
909 // FIXME: For now, preload all file source locations, so that
910 // we get the appropriate File entries in the reader. This is
911 // a temporary measure.
912 PreloadSLocs.push_back(SLocEntryOffsets.size());
913 } else {
914 // The source location entry is a buffer. The blob associated
915 // with this entry contains the contents of the buffer.
916
917 // We add one to the size so that we capture the trailing NULL
918 // that is required by llvm::MemoryBuffer::getMemBuffer (on
919 // the reader side).
920 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
921 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000922 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
923 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +0000924 Record.clear();
925 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
926 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +0000927 llvm::StringRef(Buffer->getBufferStart(),
928 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +0000929
930 if (strcmp(Name, "<built-in>") == 0)
931 PreloadSLocs.push_back(SLocEntryOffsets.size());
932 }
933 } else {
934 // The source location entry is an instantiation.
935 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
936 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
937 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
938 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
939
940 // Compute the token length for this macro expansion.
941 unsigned NextOffset = SourceMgr.getNextOffset();
942 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
943 if (++NextSLoc != SLocEnd)
944 NextOffset = NextSLoc->getOffset();
945 Record.push_back(NextOffset - SLoc->getOffset() - 1);
946 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
947 }
948 }
949
Douglas Gregor8f45df52009-04-16 22:23:12 +0000950 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +0000951
952 if (SLocEntryOffsets.empty())
953 return;
954
955 // Write the source-location offsets table into the PCH block. This
956 // table is used for lazily loading source-location information.
957 using namespace llvm;
958 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
959 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
963 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000964
Douglas Gregor258ae542009-04-27 06:38:32 +0000965 Record.clear();
966 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
967 Record.push_back(SLocEntryOffsets.size());
968 Record.push_back(SourceMgr.getNextOffset());
969 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +0000970 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +0000971 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +0000972
973 // Write the source location entry preloads array, telling the PCH
974 // reader which source locations entries it should load eagerly.
975 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000976}
977
Douglas Gregorc5046832009-04-27 18:38:38 +0000978//===----------------------------------------------------------------------===//
979// Preprocessor Serialization
980//===----------------------------------------------------------------------===//
981
Chris Lattnereeffaef2009-04-10 17:15:23 +0000982/// \brief Writes the block containing the serialized form of the
983/// preprocessor.
984///
Chris Lattner2199f5b2009-04-10 18:08:30 +0000985void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +0000986 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +0000987
Chris Lattner0af3ba12009-04-13 01:29:17 +0000988 // If the preprocessor __COUNTER__ value has been bumped, remember it.
989 if (PP.getCounterValue() != 0) {
990 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +0000991 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +0000992 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +0000993 }
994
995 // Enter the preprocessor block.
996 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregoreda6a892009-04-26 00:07:37 +0000998 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
999 // FIXME: use diagnostics subsystem for localization etc.
1000 if (PP.SawDateOrTime())
1001 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001003 // Loop over all the macro definitions that are live at the end of the file,
1004 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001005 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1006 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001007 // FIXME: This emits macros in hash table order, we should do it in a stable
1008 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001009 MacroInfo *MI = I->second;
1010
1011 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1012 // been redefined by the header (in which case they are not isBuiltinMacro).
1013 if (MI->isBuiltinMacro())
1014 continue;
1015
Douglas Gregorc3366a52009-04-21 23:56:24 +00001016 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001017 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001018 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001019 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1020 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001021
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001022 unsigned Code;
1023 if (MI->isObjectLike()) {
1024 Code = pch::PP_MACRO_OBJECT_LIKE;
1025 } else {
1026 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001028 Record.push_back(MI->isC99Varargs());
1029 Record.push_back(MI->isGNUVarargs());
1030 Record.push_back(MI->getNumArgs());
1031 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1032 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001033 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001034 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001035 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001036 Record.clear();
1037
Chris Lattner2199f5b2009-04-10 18:08:30 +00001038 // Emit the tokens array.
1039 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1040 // Note that we know that the preprocessor does not have any annotation
1041 // tokens in it because they are created by the parser, and thus can't be
1042 // in a macro definition.
1043 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001044
Chris Lattner2199f5b2009-04-10 18:08:30 +00001045 Record.push_back(Tok.getLocation().getRawEncoding());
1046 Record.push_back(Tok.getLength());
1047
Chris Lattner2199f5b2009-04-10 18:08:30 +00001048 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1049 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001050 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001051
Chris Lattner2199f5b2009-04-10 18:08:30 +00001052 // FIXME: Should translate token kind to a stable encoding.
1053 Record.push_back(Tok.getKind());
1054 // FIXME: Should translate token flags to a stable encoding.
1055 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001056
Douglas Gregor8f45df52009-04-16 22:23:12 +00001057 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001058 Record.clear();
1059 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001060 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001061 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001062 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001063}
1064
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001065void PCHWriter::WriteComments(ASTContext &Context) {
1066 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001068 if (Context.Comments.empty())
1069 return;
Mike Stump11289f42009-09-09 15:08:12 +00001070
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001071 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1072 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1073 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1074 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001075
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001076 RecordData Record;
1077 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001078 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001079 (const char*)&Context.Comments[0],
1080 Context.Comments.size() * sizeof(SourceRange));
1081}
1082
Douglas Gregorc5046832009-04-27 18:38:38 +00001083//===----------------------------------------------------------------------===//
1084// Type Serialization
1085//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001086
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001087/// \brief Write the representation of a type to the PCH stream.
John McCall8ccfcb52009-09-24 19:53:00 +00001088void PCHWriter::WriteType(QualType T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001089 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001090 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001091 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001092
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001093 // Record the offset for this type.
1094 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001095 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001096 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1097 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001098 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001099 }
1100
1101 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001102
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001103 // Emit the type's representation.
1104 PCHTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001105
1106 if (T.hasNonFastQualifiers()) {
1107 Qualifiers Qs = T.getQualifiers();
1108 AddTypeRef(T.getUnqualifiedType(), Record);
1109 Record.push_back(Qs.getAsOpaqueValue());
1110 W.Code = pch::TYPE_EXT_QUAL;
1111 } else {
1112 switch (T->getTypeClass()) {
1113 // For all of the concrete, non-dependent types, call the
1114 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001115#define TYPE(Class, Base) \
John McCall8ccfcb52009-09-24 19:53:00 +00001116 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001117#define ABSTRACT_TYPE(Class, Base)
1118#define DEPENDENT_TYPE(Class, Base)
1119#include "clang/AST/TypeNodes.def"
1120
John McCall8ccfcb52009-09-24 19:53:00 +00001121 // For all of the dependent type nodes (which only occur in C++
1122 // templates), produce an error.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001123#define TYPE(Class, Base)
1124#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1125#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001126 assert(false && "Cannot serialize dependent type nodes");
1127 break;
1128 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001129 }
1130
1131 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001132 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001133
1134 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001135 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001136}
1137
1138/// \brief Write a block containing all of the types.
1139void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner0910e3b2009-04-10 17:16:57 +00001140 // Enter the types block.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001141 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001142
Douglas Gregor1970d882009-04-26 03:49:13 +00001143 // Emit all of the types that need to be emitted (so far).
1144 while (!TypesToEmit.empty()) {
John McCall8ccfcb52009-09-24 19:53:00 +00001145 QualType T = TypesToEmit.front();
Douglas Gregor1970d882009-04-26 03:49:13 +00001146 TypesToEmit.pop();
Douglas Gregor1970d882009-04-26 03:49:13 +00001147 WriteType(T);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001148 }
1149
1150 // Exit the types block
Douglas Gregor8f45df52009-04-16 22:23:12 +00001151 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001152}
1153
Douglas Gregorc5046832009-04-27 18:38:38 +00001154//===----------------------------------------------------------------------===//
1155// Declaration Serialization
1156//===----------------------------------------------------------------------===//
1157
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001158/// \brief Write the block containing all of the declaration IDs
1159/// lexically declared within the given DeclContext.
1160///
1161/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1162/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001163uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001164 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001165 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001166 return 0;
1167
Douglas Gregor8f45df52009-04-16 22:23:12 +00001168 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001169 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001170 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1171 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001172 AddDeclRef(*D, Record);
1173
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001174 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001175 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001176 return Offset;
1177}
1178
1179/// \brief Write the block containing all of the declaration IDs
1180/// visible from the given DeclContext.
1181///
1182/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1183/// bistream, or 0 if no block was written.
1184uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1185 DeclContext *DC) {
1186 if (DC->getPrimaryContext() != DC)
1187 return 0;
1188
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001189 // Since there is no name lookup into functions or methods, and we
1190 // perform name lookup for the translation unit via the
1191 // IdentifierInfo chains, don't bother to build a
1192 // visible-declarations table for these entities.
1193 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001194 return 0;
1195
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001196 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001197 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001198
1199 // Serialize the contents of the mapping used for lookup. Note that,
1200 // although we have two very different code paths, the serialized
1201 // representation is the same for both cases: a declaration name,
1202 // followed by a size, followed by references to the visible
1203 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001204 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001205 RecordData Record;
1206 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001207 if (!Map)
1208 return 0;
1209
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001210 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1211 D != DEnd; ++D) {
1212 AddDeclarationName(D->first, Record);
1213 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1214 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001215 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001216 AddDeclRef(*Result.first, Record);
1217 }
1218
1219 if (Record.size() == 0)
1220 return 0;
1221
Douglas Gregor8f45df52009-04-16 22:23:12 +00001222 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001223 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001224 return Offset;
1225}
1226
Douglas Gregorc5046832009-04-27 18:38:38 +00001227//===----------------------------------------------------------------------===//
1228// Global Method Pool and Selector Serialization
1229//===----------------------------------------------------------------------===//
1230
Douglas Gregore84a9da2009-04-20 20:36:09 +00001231namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001232// Trait used for the on-disk hash table used in the method pool.
1233class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1234 PCHWriter &Writer;
1235
1236public:
1237 typedef Selector key_type;
1238 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregorc78d3462009-04-24 21:10:55 +00001240 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1241 typedef const data_type& data_type_ref;
1242
1243 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001244
Douglas Gregorc78d3462009-04-24 21:10:55 +00001245 static unsigned ComputeHash(Selector Sel) {
1246 unsigned N = Sel.getNumArgs();
1247 if (N == 0)
1248 ++N;
1249 unsigned R = 5381;
1250 for (unsigned I = 0; I != N; ++I)
1251 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1252 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1253 return R;
1254 }
Mike Stump11289f42009-09-09 15:08:12 +00001255
1256 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001257 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1258 data_type_ref Methods) {
1259 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1260 clang::io::Emit16(Out, KeyLen);
1261 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001262 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001263 Method = Method->Next)
1264 if (Method->Method)
1265 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001266 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001267 Method = Method->Next)
1268 if (Method->Method)
1269 DataLen += 4;
1270 clang::io::Emit16(Out, DataLen);
1271 return std::make_pair(KeyLen, DataLen);
1272 }
Mike Stump11289f42009-09-09 15:08:12 +00001273
Douglas Gregor95c13f52009-04-25 17:48:32 +00001274 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001275 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001276 assert((Start >> 32) == 0 && "Selector key offset too large");
1277 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001278 unsigned N = Sel.getNumArgs();
1279 clang::io::Emit16(Out, N);
1280 if (N == 0)
1281 N = 1;
1282 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001283 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001284 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
Douglas Gregorc78d3462009-04-24 21:10:55 +00001287 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001288 data_type_ref Methods, unsigned DataLen) {
1289 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001290 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001291 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001292 Method = Method->Next)
1293 if (Method->Method)
1294 ++NumInstanceMethods;
1295
1296 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001297 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001298 Method = Method->Next)
1299 if (Method->Method)
1300 ++NumFactoryMethods;
1301
1302 clang::io::Emit16(Out, NumInstanceMethods);
1303 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001304 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001305 Method = Method->Next)
1306 if (Method->Method)
1307 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001308 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001309 Method = Method->Next)
1310 if (Method->Method)
1311 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001312
1313 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001314 }
1315};
1316} // end anonymous namespace
1317
1318/// \brief Write the method pool into the PCH file.
1319///
1320/// The method pool contains both instance and factory methods, stored
1321/// in an on-disk hash table indexed by the selector.
1322void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1323 using namespace llvm;
1324
1325 // Create and write out the blob that contains the instance and
1326 // factor method pools.
1327 bool Empty = true;
1328 {
1329 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001330
Douglas Gregorc78d3462009-04-24 21:10:55 +00001331 // Create the on-disk hash table representation. Start by
1332 // iterating through the instance method pool.
1333 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001334 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001335 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001336 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001337 InstanceEnd = SemaRef.InstanceMethodPool.end();
1338 Instance != InstanceEnd; ++Instance) {
1339 // Check whether there is a factory method with the same
1340 // selector.
1341 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1342 = SemaRef.FactoryMethodPool.find(Instance->first);
1343
1344 if (Factory == SemaRef.FactoryMethodPool.end())
1345 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001346 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001347 ObjCMethodList()));
1348 else
1349 Generator.insert(Instance->first,
1350 std::make_pair(Instance->second, Factory->second));
1351
Douglas Gregor95c13f52009-04-25 17:48:32 +00001352 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001353 Empty = false;
1354 }
1355
1356 // Now iterate through the factory method pool, to pick up any
1357 // selectors that weren't already in the instance method pool.
1358 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001359 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001360 FactoryEnd = SemaRef.FactoryMethodPool.end();
1361 Factory != FactoryEnd; ++Factory) {
1362 // Check whether there is an instance method with the same
1363 // selector. If so, there is no work to do here.
1364 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1365 = SemaRef.InstanceMethodPool.find(Factory->first);
1366
Douglas Gregor95c13f52009-04-25 17:48:32 +00001367 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001368 Generator.insert(Factory->first,
1369 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001370 ++NumSelectorsInMethodPool;
1371 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001372
1373 Empty = false;
1374 }
1375
Douglas Gregor95c13f52009-04-25 17:48:32 +00001376 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001377 return;
1378
1379 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001380 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001381 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001382 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001383 {
1384 PCHMethodPoolTrait Trait(*this);
1385 llvm::raw_svector_ostream Out(MethodPool);
1386 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001387 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001388 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001389
1390 // For every selector that we have seen but which was not
1391 // written into the hash table, write the selector itself and
1392 // record it's offset.
1393 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1394 if (SelectorOffsets[I] == 0)
1395 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001396 }
1397
1398 // Create a blob abbreviation
1399 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1400 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1404 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1405
Douglas Gregor95c13f52009-04-25 17:48:32 +00001406 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001407 RecordData Record;
1408 Record.push_back(pch::METHOD_POOL);
1409 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001410 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001411 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001412
1413 // Create a blob abbreviation for the selector table offsets.
1414 Abbrev = new BitCodeAbbrev();
1415 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1418 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1419
1420 // Write the selector offsets table.
1421 Record.clear();
1422 Record.push_back(pch::SELECTOR_OFFSETS);
1423 Record.push_back(SelectorOffsets.size());
1424 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1425 (const char *)&SelectorOffsets.front(),
1426 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001427 }
1428}
1429
Douglas Gregorc5046832009-04-27 18:38:38 +00001430//===----------------------------------------------------------------------===//
1431// Identifier Table Serialization
1432//===----------------------------------------------------------------------===//
1433
Douglas Gregorc78d3462009-04-24 21:10:55 +00001434namespace {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001435class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1436 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001437 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001438
Douglas Gregor1d583f22009-04-28 21:18:29 +00001439 /// \brief Determines whether this is an "interesting" identifier
1440 /// that needs a full IdentifierInfo structure written into the hash
1441 /// table.
1442 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1443 return II->isPoisoned() ||
1444 II->isExtensionToken() ||
1445 II->hasMacroDefinition() ||
1446 II->getObjCOrBuiltinID() ||
1447 II->getFETokenInfo<void>();
1448 }
1449
Douglas Gregore84a9da2009-04-20 20:36:09 +00001450public:
1451 typedef const IdentifierInfo* key_type;
1452 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregore84a9da2009-04-20 20:36:09 +00001454 typedef pch::IdentID data_type;
1455 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001456
1457 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001458 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001459
1460 static unsigned ComputeHash(const IdentifierInfo* II) {
1461 return clang::BernsteinHash(II->getName());
1462 }
Mike Stump11289f42009-09-09 15:08:12 +00001463
1464 std::pair<unsigned,unsigned>
1465 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001466 pch::IdentID ID) {
1467 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001468 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1469 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001470 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001471 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001472 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001473 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001474 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1475 DEnd = IdentifierResolver::end();
1476 D != DEnd; ++D)
1477 DataLen += sizeof(pch::DeclID);
1478 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001479 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001480 // We emit the key length after the data length so that every
1481 // string is preceded by a 16-bit length. This matches the PTH
1482 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001483 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001484 return std::make_pair(KeyLen, DataLen);
1485 }
Mike Stump11289f42009-09-09 15:08:12 +00001486
1487 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001488 unsigned KeyLen) {
1489 // Record the location of the key data. This is used when generating
1490 // the mapping from persistent IDs to strings.
1491 Writer.SetIdentifierOffset(II, Out.tell());
1492 Out.write(II->getName(), KeyLen);
1493 }
Mike Stump11289f42009-09-09 15:08:12 +00001494
1495 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001496 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001497 if (!isInterestingIdentifier(II)) {
1498 clang::io::Emit32(Out, ID << 1);
1499 return;
1500 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001501
Douglas Gregor1d583f22009-04-28 21:18:29 +00001502 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001503 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001504 bool hasMacroDefinition =
1505 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001506 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001507 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001508 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001509 Bits = (Bits << 1) | II->isExtensionToken();
1510 Bits = (Bits << 1) | II->isPoisoned();
1511 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001512 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001513
Douglas Gregorc3366a52009-04-21 23:56:24 +00001514 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001515 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001516
Douglas Gregora868bbd2009-04-21 22:25:48 +00001517 // Emit the declaration IDs in reverse order, because the
1518 // IdentifierResolver provides the declarations as they would be
1519 // visible (e.g., the function "stat" would come before the struct
1520 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1521 // adds declarations to the end of the list (so we need to see the
1522 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001523 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001524 IdentifierResolver::end());
1525 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1526 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001527 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001528 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001529 }
1530};
1531} // end anonymous namespace
1532
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001533/// \brief Write the identifier table into the PCH file.
1534///
1535/// The identifier table consists of a blob containing string data
1536/// (the actual identifiers themselves) and a separate "offsets" index
1537/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001538void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001539 using namespace llvm;
1540
1541 // Create and write out the blob that contains the identifier
1542 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001543 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001544 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregore6648fb2009-04-28 20:33:11 +00001546 // Look for any identifiers that were named while processing the
1547 // headers, but are otherwise not needed. We add these to the hash
1548 // table to enable checking of the predefines buffer in the case
1549 // where the user adds new macro definitions when building the PCH
1550 // file.
1551 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1552 IDEnd = PP.getIdentifierTable().end();
1553 ID != IDEnd; ++ID)
1554 getIdentifierRef(ID->second);
1555
Douglas Gregore84a9da2009-04-20 20:36:09 +00001556 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001557 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001558 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1559 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1560 ID != IDEnd; ++ID) {
1561 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001562 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001563 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001564
Douglas Gregore84a9da2009-04-20 20:36:09 +00001565 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001566 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001567 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001568 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001569 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001570 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001571 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001572 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001573 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001574 }
1575
1576 // Create a blob abbreviation
1577 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1578 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001581 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001582
1583 // Write the identifier table
1584 RecordData Record;
1585 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001586 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001587 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001588 }
1589
1590 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001591 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1592 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1595 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1596
1597 RecordData Record;
1598 Record.push_back(pch::IDENTIFIER_OFFSET);
1599 Record.push_back(IdentifierOffsets.size());
1600 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1601 (const char *)&IdentifierOffsets.front(),
1602 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001603}
1604
Douglas Gregorc5046832009-04-27 18:38:38 +00001605//===----------------------------------------------------------------------===//
1606// General Serialization Routines
1607//===----------------------------------------------------------------------===//
1608
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001609/// \brief Write a record containing the given attributes.
1610void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1611 RecordData Record;
1612 for (; Attr; Attr = Attr->getNext()) {
1613 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1614 Record.push_back(Attr->isInherited());
1615 switch (Attr->getKind()) {
1616 case Attr::Alias:
1617 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1618 break;
1619
1620 case Attr::Aligned:
1621 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1622 break;
1623
1624 case Attr::AlwaysInline:
1625 break;
Mike Stump11289f42009-09-09 15:08:12 +00001626
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001627 case Attr::AnalyzerNoReturn:
1628 break;
1629
1630 case Attr::Annotate:
1631 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1632 break;
1633
1634 case Attr::AsmLabel:
1635 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1636 break;
1637
1638 case Attr::Blocks:
1639 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1640 break;
1641
1642 case Attr::Cleanup:
1643 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1644 break;
1645
1646 case Attr::Const:
1647 break;
1648
1649 case Attr::Constructor:
1650 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1651 break;
1652
1653 case Attr::DLLExport:
1654 case Attr::DLLImport:
1655 case Attr::Deprecated:
1656 break;
1657
1658 case Attr::Destructor:
1659 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1660 break;
1661
1662 case Attr::FastCall:
1663 break;
1664
1665 case Attr::Format: {
1666 const FormatAttr *Format = cast<FormatAttr>(Attr);
1667 AddString(Format->getType(), Record);
1668 Record.push_back(Format->getFormatIdx());
1669 Record.push_back(Format->getFirstArg());
1670 break;
1671 }
1672
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001673 case Attr::FormatArg: {
1674 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1675 Record.push_back(Format->getFormatIdx());
1676 break;
1677 }
1678
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001679 case Attr::Sentinel : {
1680 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1681 Record.push_back(Sentinel->getSentinel());
1682 Record.push_back(Sentinel->getNullPos());
1683 break;
1684 }
Mike Stump11289f42009-09-09 15:08:12 +00001685
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001686 case Attr::GNUInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001687 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001688 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001689 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001690 case Attr::NoReturn:
1691 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001692 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001693 break;
1694
1695 case Attr::NonNull: {
1696 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1697 Record.push_back(NonNull->size());
1698 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1699 break;
1700 }
1701
1702 case Attr::ObjCException:
1703 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001704 case Attr::CFReturnsRetained:
1705 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001706 case Attr::Overloadable:
1707 break;
1708
Anders Carlsson68e0b682009-08-08 18:23:56 +00001709 case Attr::PragmaPack:
1710 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001711 break;
1712
Anders Carlsson68e0b682009-08-08 18:23:56 +00001713 case Attr::Packed:
1714 break;
Mike Stump11289f42009-09-09 15:08:12 +00001715
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001716 case Attr::Pure:
1717 break;
1718
1719 case Attr::Regparm:
1720 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1721 break;
Mike Stump11289f42009-09-09 15:08:12 +00001722
Nate Begemanf2758702009-06-26 06:32:41 +00001723 case Attr::ReqdWorkGroupSize:
1724 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1725 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1726 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1727 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001728
1729 case Attr::Section:
1730 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1731 break;
1732
1733 case Attr::StdCall:
1734 case Attr::TransparentUnion:
1735 case Attr::Unavailable:
1736 case Attr::Unused:
1737 case Attr::Used:
1738 break;
1739
1740 case Attr::Visibility:
1741 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001742 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001743 break;
1744
1745 case Attr::WarnUnusedResult:
1746 case Attr::Weak:
1747 case Attr::WeakImport:
1748 break;
1749 }
1750 }
1751
Douglas Gregor8f45df52009-04-16 22:23:12 +00001752 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001753}
1754
1755void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1756 Record.push_back(Str.size());
1757 Record.insert(Record.end(), Str.begin(), Str.end());
1758}
1759
Douglas Gregore84a9da2009-04-20 20:36:09 +00001760/// \brief Note that the identifier II occurs at the given offset
1761/// within the identifier table.
1762void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001763 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001764}
1765
Douglas Gregor95c13f52009-04-25 17:48:32 +00001766/// \brief Note that the selector Sel occurs at the given offset
1767/// within the method pool/selector table.
1768void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1769 unsigned ID = SelectorIDs[Sel];
1770 assert(ID && "Unknown selector");
1771 SelectorOffsets[ID - 1] = Offset;
1772}
1773
Mike Stump11289f42009-09-09 15:08:12 +00001774PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1775 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001776 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1777 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001778
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001779void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1780 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001781 using namespace llvm;
1782
Douglas Gregor162dd022009-04-20 15:53:59 +00001783 ASTContext &Context = SemaRef.Context;
1784 Preprocessor &PP = SemaRef.PP;
1785
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001786 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001787 Stream.Emit((unsigned)'C', 8);
1788 Stream.Emit((unsigned)'P', 8);
1789 Stream.Emit((unsigned)'C', 8);
1790 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001791
Chris Lattner28fa4e62009-04-26 22:26:21 +00001792 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001793
1794 // The translation unit is the first declaration we'll emit.
1795 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1796 DeclsToEmit.push(Context.getTranslationUnitDecl());
1797
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001798 // Make sure that we emit IdentifierInfos (and any attached
1799 // declarations) for builtins.
1800 {
1801 IdentifierTable &Table = PP.getIdentifierTable();
1802 llvm::SmallVector<const char *, 32> BuiltinNames;
1803 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1804 Context.getLangOptions().NoBuiltin);
1805 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1806 getIdentifierRef(&Table.get(BuiltinNames[I]));
1807 }
1808
Chris Lattner0c797362009-09-08 18:19:27 +00001809 // Build a record containing all of the tentative definitions in this file, in
1810 // TentativeDefinitionList order. Generally, this record will be empty for
1811 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001812 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001813 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1814 VarDecl *VD =
1815 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1816 if (VD) AddDeclRef(VD, TentativeDefinitions);
1817 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001818
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001819 // Build a record containing all of the locally-scoped external
1820 // declarations in this header file. Generally, this record will be
1821 // empty.
1822 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001823 // FIXME: This is filling in the PCH file in densemap order which is
1824 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001825 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001826 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1827 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1828 TD != TDEnd; ++TD)
1829 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1830
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001831 // Build a record containing all of the ext_vector declarations.
1832 RecordData ExtVectorDecls;
1833 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1834 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1835
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001836 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001837 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001838 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001839 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001840 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001841 if (StatCalls && !isysroot)
1842 WriteStatCache(*StatCalls, isysroot);
1843 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Chris Lattnereeffaef2009-04-10 17:15:23 +00001844 WritePreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +00001845 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001846 // Write the record of special types.
1847 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001848
Steve Naroffc277ad12009-07-18 15:33:26 +00001849 AddTypeRef(Context.getBuiltinVaListType(), Record);
1850 AddTypeRef(Context.getObjCIdType(), Record);
1851 AddTypeRef(Context.getObjCSelType(), Record);
1852 AddTypeRef(Context.getObjCProtoType(), Record);
1853 AddTypeRef(Context.getObjCClassType(), Record);
1854 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1855 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1856 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001857 AddTypeRef(Context.getjmp_bufType(), Record);
1858 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001859 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1860 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00001861 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001862
Douglas Gregor1970d882009-04-26 03:49:13 +00001863 // Keep writing types and declarations until all types and
1864 // declarations have been written.
1865 do {
1866 if (!DeclsToEmit.empty())
1867 WriteDeclsBlock(Context);
1868 if (!TypesToEmit.empty())
1869 WriteTypesBlock(Context);
1870 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1871
Douglas Gregorc78d3462009-04-24 21:10:55 +00001872 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001873 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00001874
1875 // Write the type offsets array
1876 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1877 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1880 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1881 Record.clear();
1882 Record.push_back(pch::TYPE_OFFSET);
1883 Record.push_back(TypeOffsets.size());
1884 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001885 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00001886 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00001887
Douglas Gregor745ed142009-04-25 18:35:21 +00001888 // Write the declaration offsets array
1889 Abbrev = new BitCodeAbbrev();
1890 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1893 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1894 Record.clear();
1895 Record.push_back(pch::DECL_OFFSET);
1896 Record.push_back(DeclOffsets.size());
1897 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001898 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00001899 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00001900
Douglas Gregord4df8652009-04-22 22:02:47 +00001901 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001902 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00001903 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00001904
1905 // Write the record containing tentative definitions.
1906 if (!TentativeDefinitions.empty())
1907 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001908
1909 // Write the record containing locally-scoped external definitions.
1910 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00001911 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001912 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001913
1914 // Write the record containing ext_vector type names.
1915 if (!ExtVectorDecls.empty())
1916 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00001917
Douglas Gregor08f01292009-04-17 22:13:46 +00001918 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00001919 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00001920 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001921 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001922 Record.push_back(NumLexicalDeclContexts);
1923 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00001924 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001925 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001926}
1927
1928void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1929 Record.push_back(Loc.getRawEncoding());
1930}
1931
1932void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1933 Record.push_back(Value.getBitWidth());
1934 unsigned N = Value.getNumWords();
1935 const uint64_t* Words = Value.getRawData();
1936 for (unsigned I = 0; I != N; ++I)
1937 Record.push_back(Words[I]);
1938}
1939
Douglas Gregor1daeb692009-04-13 18:14:40 +00001940void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1941 Record.push_back(Value.isUnsigned());
1942 AddAPInt(Value, Record);
1943}
1944
Douglas Gregore0a3a512009-04-14 21:55:33 +00001945void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1946 AddAPInt(Value.bitcastToAPInt(), Record);
1947}
1948
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001949void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001950 Record.push_back(getIdentifierRef(II));
1951}
1952
1953pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1954 if (II == 0)
1955 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001956
1957 pch::IdentID &ID = IdentifierIDs[II];
1958 if (ID == 0)
1959 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001960 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001961}
1962
Steve Naroff2ddea052009-04-23 10:39:46 +00001963void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1964 if (SelRef.getAsOpaquePtr() == 0) {
1965 Record.push_back(0);
1966 return;
1967 }
1968
1969 pch::SelectorID &SID = SelectorIDs[SelRef];
1970 if (SID == 0) {
1971 SID = SelectorIDs.size();
1972 SelVector.push_back(SelRef);
1973 }
1974 Record.push_back(SID);
1975}
1976
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001977void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1978 if (T.isNull()) {
1979 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1980 return;
1981 }
1982
John McCall8ccfcb52009-09-24 19:53:00 +00001983 unsigned FastQuals = T.getFastQualifiers();
1984 T.removeFastQualifiers();
1985
1986 if (T.hasNonFastQualifiers()) {
1987 pch::TypeID &ID = TypeIDs[T];
1988 if (ID == 0) {
1989 // We haven't seen these qualifiers applied to this type before.
1990 // Assign it a new ID. This is the only time we enqueue a
1991 // qualified type, and it has no CV qualifiers.
1992 ID = NextTypeID++;
1993 TypesToEmit.push(T);
1994 }
1995
1996 // Encode the type qualifiers in the type reference.
1997 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
1998 return;
1999 }
2000
2001 assert(!T.hasQualifiers());
2002
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002003 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00002004 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002005 switch (BT->getKind()) {
2006 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2007 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2008 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2009 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2010 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2011 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2012 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2013 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002014 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002015 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2016 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2017 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2018 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2019 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2020 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2021 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00002022 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002023 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2024 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2025 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002026 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002027 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2028 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002029 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2030 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002031 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2032 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002033 case BuiltinType::UndeducedAuto:
2034 assert(0 && "Should not see undeduced auto here");
2035 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002036 }
2037
John McCall8ccfcb52009-09-24 19:53:00 +00002038 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002039 return;
2040 }
2041
John McCall8ccfcb52009-09-24 19:53:00 +00002042 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor1970d882009-04-26 03:49:13 +00002043 if (ID == 0) {
2044 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002045 // into the queue of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002046 ID = NextTypeID++;
John McCall8ccfcb52009-09-24 19:53:00 +00002047 TypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002048 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002049
2050 // Encode the type qualifiers in the type reference.
John McCall8ccfcb52009-09-24 19:53:00 +00002051 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002052}
2053
2054void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2055 if (D == 0) {
2056 Record.push_back(0);
2057 return;
2058 }
2059
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002060 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002061 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002062 // We haven't seen this declaration before. Give it a new ID and
2063 // enqueue it in the list of declarations to emit.
2064 ID = DeclIDs.size();
2065 DeclsToEmit.push(const_cast<Decl *>(D));
2066 }
2067
2068 Record.push_back(ID);
2069}
2070
Douglas Gregore84a9da2009-04-20 20:36:09 +00002071pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2072 if (D == 0)
2073 return 0;
2074
2075 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2076 return DeclIDs[D];
2077}
2078
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002079void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002080 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002081 Record.push_back(Name.getNameKind());
2082 switch (Name.getNameKind()) {
2083 case DeclarationName::Identifier:
2084 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2085 break;
2086
2087 case DeclarationName::ObjCZeroArgSelector:
2088 case DeclarationName::ObjCOneArgSelector:
2089 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002090 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002091 break;
2092
2093 case DeclarationName::CXXConstructorName:
2094 case DeclarationName::CXXDestructorName:
2095 case DeclarationName::CXXConversionFunctionName:
2096 AddTypeRef(Name.getCXXNameType(), Record);
2097 break;
2098
2099 case DeclarationName::CXXOperatorName:
2100 Record.push_back(Name.getCXXOverloadedOperator());
2101 break;
2102
2103 case DeclarationName::CXXUsingDirective:
2104 // No extra data to emit
2105 break;
2106 }
2107}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002108