blob: 531c6f7818452c144c672f7824843b012642d430 [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
67void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
68 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
69 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
70 Record.push_back(T->getAddressSpace());
71 Code = pch::TYPE_EXT_QUAL;
72}
73
74void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
75 assert(false && "Built-in types are never serialized");
76}
77
78void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
79 Record.push_back(T->getWidth());
80 Record.push_back(T->isSigned());
81 Code = pch::TYPE_FIXED_WIDTH_INT;
82}
83
84void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
85 Writer.AddTypeRef(T->getElementType(), Record);
86 Code = pch::TYPE_COMPLEX;
87}
88
89void PCHTypeWriter::VisitPointerType(const PointerType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_POINTER;
92}
93
94void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000095 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096 Code = pch::TYPE_BLOCK_POINTER;
97}
98
99void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_LVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Code = pch::TYPE_RVALUE_REFERENCE;
107}
108
109void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
111 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112 Code = pch::TYPE_MEMBER_POINTER;
113}
114
115void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
116 Writer.AddTypeRef(T->getElementType(), Record);
117 Record.push_back(T->getSizeModifier()); // FIXME: stable values
118 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
119}
120
121void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
122 VisitArrayType(T);
123 Writer.AddAPInt(T->getSize(), Record);
124 Code = pch::TYPE_CONSTANT_ARRAY;
125}
126
Douglas Gregor04318252009-07-06 15:59:29 +0000127void PCHTypeWriter
128::VisitConstantArrayWithExprType(const ConstantArrayWithExprType *T) {
129 VisitArrayType(T);
130 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
131 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
132 Writer.AddAPInt(T->getSize(), Record);
133 Writer.AddStmt(T->getSizeExpr());
134 Code = pch::TYPE_CONSTANT_ARRAY_WITH_EXPR;
135}
136
137void PCHTypeWriter
138::VisitConstantArrayWithoutExprType(const ConstantArrayWithoutExprType *T) {
139 VisitArrayType(T);
140 Writer.AddAPInt(T->getSize(), Record);
141 Code = pch::TYPE_CONSTANT_ARRAY_WITHOUT_EXPR;
142}
143
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
145 VisitArrayType(T);
146 Code = pch::TYPE_INCOMPLETE_ARRAY;
147}
148
149void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
150 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 Code = pch::TYPE_VARIABLE_ARRAY;
155}
156
157void PCHTypeWriter::VisitVectorType(const VectorType *T) {
158 Writer.AddTypeRef(T->getElementType(), Record);
159 Record.push_back(T->getNumElements());
160 Code = pch::TYPE_VECTOR;
161}
162
163void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
164 VisitVectorType(T);
165 Code = pch::TYPE_EXT_VECTOR;
166}
167
168void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
169 Writer.AddTypeRef(T->getResultType(), Record);
170}
171
172void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
173 VisitFunctionType(T);
174 Code = pch::TYPE_FUNCTION_NO_PROTO;
175}
176
177void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
178 VisitFunctionType(T);
179 Record.push_back(T->getNumArgs());
180 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
181 Writer.AddTypeRef(T->getArgType(I), Record);
182 Record.push_back(T->isVariadic());
183 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000184 Record.push_back(T->hasExceptionSpec());
185 Record.push_back(T->hasAnyExceptionSpec());
186 Record.push_back(T->getNumExceptions());
187 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
188 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189 Code = pch::TYPE_FUNCTION_PROTO;
190}
191
192void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
193 Writer.AddDeclRef(T->getDecl(), Record);
194 Code = pch::TYPE_TYPEDEF;
195}
196
197void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000198 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199 Code = pch::TYPE_TYPEOF_EXPR;
200}
201
202void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
203 Writer.AddTypeRef(T->getUnderlyingType(), Record);
204 Code = pch::TYPE_TYPEOF;
205}
206
Anders Carlsson81df7b82009-06-24 19:06:50 +0000207void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
208 Writer.AddStmt(T->getUnderlyingExpr());
209 Code = pch::TYPE_DECLTYPE;
210}
211
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212void PCHTypeWriter::VisitTagType(const TagType *T) {
213 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000214 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215 "Cannot serialize in the middle of a type definition");
216}
217
218void PCHTypeWriter::VisitRecordType(const RecordType *T) {
219 VisitTagType(T);
220 Code = pch::TYPE_RECORD;
221}
222
223void PCHTypeWriter::VisitEnumType(const EnumType *T) {
224 VisitTagType(T);
225 Code = pch::TYPE_ENUM;
226}
227
John McCallfcc33b02009-09-05 00:15:47 +0000228void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
230 Record.push_back(T->getTagKind());
231 Code = pch::TYPE_ELABORATED;
232}
233
Mike Stump11289f42009-09-09 15:08:12 +0000234void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235PCHTypeWriter::VisitTemplateSpecializationType(
236 const TemplateSpecializationType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000237 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000238 assert(false && "Cannot serialize template specialization types");
239}
240
241void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregore95304a2009-04-15 18:43:11 +0000242 // FIXME: Serialize this type (C++ only)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243 assert(false && "Cannot serialize qualified name types");
244}
245
246void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
247 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248 Record.push_back(T->getNumProtocols());
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000249 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
250 E = T->qual_end(); I != E; ++I)
251 Writer.AddDeclRef(*I, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +0000252 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253}
254
Steve Narofffb4330f2009-06-17 22:40:22 +0000255void
256PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000257 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258 Record.push_back(T->getNumProtocols());
Steve Narofffb4330f2009-06-17 22:40:22 +0000259 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000260 E = T->qual_end(); I != E; ++I)
261 Writer.AddDeclRef(*I, Record);
Steve Narofffb4330f2009-06-17 22:40:22 +0000262 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Chris Lattner19cea4e2009-04-22 05:57:30 +0000265//===----------------------------------------------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000266// PCHWriter Implementation
267//===----------------------------------------------------------------------===//
268
Chris Lattner28fa4e62009-04-26 22:26:21 +0000269static void EmitBlockID(unsigned ID, const char *Name,
270 llvm::BitstreamWriter &Stream,
271 PCHWriter::RecordData &Record) {
272 Record.clear();
273 Record.push_back(ID);
274 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
275
276 // Emit the block name if present.
277 if (Name == 0 || Name[0] == 0) return;
278 Record.clear();
279 while (*Name)
280 Record.push_back(*Name++);
281 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
282}
283
284static void EmitRecordID(unsigned ID, const char *Name,
285 llvm::BitstreamWriter &Stream,
286 PCHWriter::RecordData &Record) {
287 Record.clear();
288 Record.push_back(ID);
289 while (*Name)
290 Record.push_back(*Name++);
291 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000292}
293
294static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
295 PCHWriter::RecordData &Record) {
296#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
297 RECORD(STMT_STOP);
298 RECORD(STMT_NULL_PTR);
299 RECORD(STMT_NULL);
300 RECORD(STMT_COMPOUND);
301 RECORD(STMT_CASE);
302 RECORD(STMT_DEFAULT);
303 RECORD(STMT_LABEL);
304 RECORD(STMT_IF);
305 RECORD(STMT_SWITCH);
306 RECORD(STMT_WHILE);
307 RECORD(STMT_DO);
308 RECORD(STMT_FOR);
309 RECORD(STMT_GOTO);
310 RECORD(STMT_INDIRECT_GOTO);
311 RECORD(STMT_CONTINUE);
312 RECORD(STMT_BREAK);
313 RECORD(STMT_RETURN);
314 RECORD(STMT_DECL);
315 RECORD(STMT_ASM);
316 RECORD(EXPR_PREDEFINED);
317 RECORD(EXPR_DECL_REF);
318 RECORD(EXPR_INTEGER_LITERAL);
319 RECORD(EXPR_FLOATING_LITERAL);
320 RECORD(EXPR_IMAGINARY_LITERAL);
321 RECORD(EXPR_STRING_LITERAL);
322 RECORD(EXPR_CHARACTER_LITERAL);
323 RECORD(EXPR_PAREN);
324 RECORD(EXPR_UNARY_OPERATOR);
325 RECORD(EXPR_SIZEOF_ALIGN_OF);
326 RECORD(EXPR_ARRAY_SUBSCRIPT);
327 RECORD(EXPR_CALL);
328 RECORD(EXPR_MEMBER);
329 RECORD(EXPR_BINARY_OPERATOR);
330 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
331 RECORD(EXPR_CONDITIONAL_OPERATOR);
332 RECORD(EXPR_IMPLICIT_CAST);
333 RECORD(EXPR_CSTYLE_CAST);
334 RECORD(EXPR_COMPOUND_LITERAL);
335 RECORD(EXPR_EXT_VECTOR_ELEMENT);
336 RECORD(EXPR_INIT_LIST);
337 RECORD(EXPR_DESIGNATED_INIT);
338 RECORD(EXPR_IMPLICIT_VALUE_INIT);
339 RECORD(EXPR_VA_ARG);
340 RECORD(EXPR_ADDR_LABEL);
341 RECORD(EXPR_STMT);
342 RECORD(EXPR_TYPES_COMPATIBLE);
343 RECORD(EXPR_CHOOSE);
344 RECORD(EXPR_GNU_NULL);
345 RECORD(EXPR_SHUFFLE_VECTOR);
346 RECORD(EXPR_BLOCK);
347 RECORD(EXPR_BLOCK_DECL_REF);
348 RECORD(EXPR_OBJC_STRING_LITERAL);
349 RECORD(EXPR_OBJC_ENCODE);
350 RECORD(EXPR_OBJC_SELECTOR_EXPR);
351 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
352 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
353 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
354 RECORD(EXPR_OBJC_KVC_REF_EXPR);
355 RECORD(EXPR_OBJC_MESSAGE_EXPR);
356 RECORD(EXPR_OBJC_SUPER_EXPR);
357 RECORD(STMT_OBJC_FOR_COLLECTION);
358 RECORD(STMT_OBJC_CATCH);
359 RECORD(STMT_OBJC_FINALLY);
360 RECORD(STMT_OBJC_AT_TRY);
361 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
362 RECORD(STMT_OBJC_AT_THROW);
363#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000364}
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner28fa4e62009-04-26 22:26:21 +0000366void PCHWriter::WriteBlockInfoBlock() {
367 RecordData Record;
368 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000369
Chris Lattner64031982009-04-27 00:40:25 +0000370#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner28fa4e62009-04-26 22:26:21 +0000371#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattner28fa4e62009-04-26 22:26:21 +0000373 // PCH Top-Level Block.
Chris Lattner64031982009-04-27 00:40:25 +0000374 BLOCK(PCH_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000375 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000376 RECORD(TYPE_OFFSET);
377 RECORD(DECL_OFFSET);
378 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000379 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000380 RECORD(IDENTIFIER_OFFSET);
381 RECORD(IDENTIFIER_TABLE);
382 RECORD(EXTERNAL_DEFINITIONS);
383 RECORD(SPECIAL_TYPES);
384 RECORD(STATISTICS);
385 RECORD(TENTATIVE_DEFINITIONS);
386 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
387 RECORD(SELECTOR_OFFSETS);
388 RECORD(METHOD_POOL);
389 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000390 RECORD(SOURCE_LOCATION_OFFSETS);
391 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000392 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000393 RECORD(EXT_VECTOR_DECLS);
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000394 RECORD(COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattner28fa4e62009-04-26 22:26:21 +0000396 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000397 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000398 RECORD(SM_SLOC_FILE_ENTRY);
399 RECORD(SM_SLOC_BUFFER_ENTRY);
400 RECORD(SM_SLOC_BUFFER_BLOB);
401 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
402 RECORD(SM_LINE_TABLE);
403 RECORD(SM_HEADER_FILE_INFO);
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattner28fa4e62009-04-26 22:26:21 +0000405 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000406 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000407 RECORD(PP_MACRO_OBJECT_LIKE);
408 RECORD(PP_MACRO_FUNCTION_LIKE);
409 RECORD(PP_TOKEN);
410
411 // Types block.
Chris Lattner64031982009-04-27 00:40:25 +0000412 BLOCK(TYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000413 RECORD(TYPE_EXT_QUAL);
414 RECORD(TYPE_FIXED_WIDTH_INT);
415 RECORD(TYPE_COMPLEX);
416 RECORD(TYPE_POINTER);
417 RECORD(TYPE_BLOCK_POINTER);
418 RECORD(TYPE_LVALUE_REFERENCE);
419 RECORD(TYPE_RVALUE_REFERENCE);
420 RECORD(TYPE_MEMBER_POINTER);
421 RECORD(TYPE_CONSTANT_ARRAY);
422 RECORD(TYPE_INCOMPLETE_ARRAY);
423 RECORD(TYPE_VARIABLE_ARRAY);
424 RECORD(TYPE_VECTOR);
425 RECORD(TYPE_EXT_VECTOR);
426 RECORD(TYPE_FUNCTION_PROTO);
427 RECORD(TYPE_FUNCTION_NO_PROTO);
428 RECORD(TYPE_TYPEDEF);
429 RECORD(TYPE_TYPEOF_EXPR);
430 RECORD(TYPE_TYPEOF);
431 RECORD(TYPE_RECORD);
432 RECORD(TYPE_ENUM);
433 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000434 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000435 // Statements and Exprs can occur in the Types block.
436 AddStmtsExprs(Stream, Record);
437
Chris Lattner28fa4e62009-04-26 22:26:21 +0000438 // Decls block.
Chris Lattner64031982009-04-27 00:40:25 +0000439 BLOCK(DECLS_BLOCK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000440 RECORD(DECL_ATTR);
441 RECORD(DECL_TRANSLATION_UNIT);
442 RECORD(DECL_TYPEDEF);
443 RECORD(DECL_ENUM);
444 RECORD(DECL_RECORD);
445 RECORD(DECL_ENUM_CONSTANT);
446 RECORD(DECL_FUNCTION);
447 RECORD(DECL_OBJC_METHOD);
448 RECORD(DECL_OBJC_INTERFACE);
449 RECORD(DECL_OBJC_PROTOCOL);
450 RECORD(DECL_OBJC_IVAR);
451 RECORD(DECL_OBJC_AT_DEFS_FIELD);
452 RECORD(DECL_OBJC_CLASS);
453 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
454 RECORD(DECL_OBJC_CATEGORY);
455 RECORD(DECL_OBJC_CATEGORY_IMPL);
456 RECORD(DECL_OBJC_IMPLEMENTATION);
457 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
458 RECORD(DECL_OBJC_PROPERTY);
459 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000460 RECORD(DECL_FIELD);
461 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000462 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000463 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000464 RECORD(DECL_ORIGINAL_PARM_VAR);
465 RECORD(DECL_FILE_SCOPE_ASM);
466 RECORD(DECL_BLOCK);
467 RECORD(DECL_CONTEXT_LEXICAL);
468 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000469 // Statements and Exprs can occur in the Decls block.
470 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000471#undef RECORD
472#undef BLOCK
473 Stream.ExitBlock();
474}
475
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000476/// \brief Adjusts the given filename to only write out the portion of the
477/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000478///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000479/// \param Filename the file name to adjust.
480///
481/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
482/// the returned filename will be adjusted by this system root.
483///
484/// \returns either the original filename (if it needs no adjustment) or the
485/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000486static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000487adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
488 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000490 if (!isysroot)
491 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000493 // Verify that the filename and the system root have the same prefix.
494 unsigned Pos = 0;
495 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
496 if (Filename[Pos] != isysroot[Pos])
497 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000499 // We hit the end of the filename before we hit the end of the system root.
500 if (!Filename[Pos])
501 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000502
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000503 // If the file name has a '/' at the current position, skip over the '/'.
504 // We distinguish sysroot-based includes from absolute includes by the
505 // absence of '/' at the beginning of sysroot-based includes.
506 if (Filename[Pos] == '/')
507 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000508
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000509 return Filename + Pos;
510}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000511
Douglas Gregor7b71e632009-04-27 22:23:34 +0000512/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000513void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000514 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000515
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000516 // Metadata
517 const TargetInfo &Target = Context.Target;
518 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
519 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
520 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
521 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
522 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
523 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
524 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
525 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
526 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000528 RecordData Record;
529 Record.push_back(pch::METADATA);
530 Record.push_back(pch::VERSION_MAJOR);
531 Record.push_back(pch::VERSION_MINOR);
532 Record.push_back(CLANG_VERSION_MAJOR);
533 Record.push_back(CLANG_VERSION_MINOR);
534 Record.push_back(isysroot != 0);
Daniel Dunbar40165182009-08-24 09:10:05 +0000535 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000536 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Mike Stump11289f42009-09-09 15:08:12 +0000537
Douglas Gregor45fe0362009-05-12 01:31:05 +0000538 // Original file name
539 SourceManager &SM = Context.getSourceManager();
540 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
541 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
542 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
543 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
544 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
545
546 llvm::sys::Path MainFilePath(MainFile->getName());
547 std::string MainFileName;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregor45fe0362009-05-12 01:31:05 +0000549 if (!MainFilePath.isAbsolute()) {
550 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000551 P.appendComponent(MainFilePath.str());
552 MainFileName = P.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000553 } else {
Chris Lattner3441b4f2009-08-23 22:45:33 +0000554 MainFileName = MainFilePath.str();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000555 }
556
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000557 const char *MainFileNameStr = MainFileName.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000558 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000559 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000560 RecordData Record;
561 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000562 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000563 }
Douglas Gregorbfbde532009-04-10 21:16:55 +0000564}
565
566/// \brief Write the LangOptions structure.
Douglas Gregor55abb232009-04-10 20:39:37 +0000567void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
568 RecordData Record;
569 Record.push_back(LangOpts.Trigraphs);
570 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
571 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
572 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
573 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
574 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
575 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
576 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
577 Record.push_back(LangOpts.C99); // C99 Support
578 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
579 Record.push_back(LangOpts.CPlusPlus); // C++ Support
580 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000581 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000582
Douglas Gregor55abb232009-04-10 20:39:37 +0000583 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
584 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
585 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
Mike Stump11289f42009-09-09 15:08:12 +0000586
Douglas Gregor55abb232009-04-10 20:39:37 +0000587 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000588 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
589 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000590 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000591 Record.push_back(LangOpts.Exceptions); // Support exception handling.
592
593 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
594 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
595 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
596
Chris Lattner258172e2009-04-27 07:35:58 +0000597 // Whether static initializers are protected by locks.
598 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000599 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000600 Record.push_back(LangOpts.Blocks); // block extension to C
601 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
602 // they are unused.
603 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
604 // (modulo the platform support).
605
606 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
607 // signed integer arithmetic overflows.
608
609 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
610 // may be ripped out at any time.
611
612 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000613 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000614 // defined.
615 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
616 // opposed to __DYNAMIC__).
617 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
618
619 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
620 // used (instead of C99 semantics).
621 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000622 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
623 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000624 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
625 // unsigned type
Douglas Gregor55abb232009-04-10 20:39:37 +0000626 Record.push_back(LangOpts.getGCMode());
627 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000628 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000629 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000630 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000631 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000632 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000633}
634
Douglas Gregora7f71a92009-04-10 03:52:48 +0000635//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000636// stat cache Serialization
637//===----------------------------------------------------------------------===//
638
639namespace {
640// Trait used for the on-disk hash table of stat cache results.
641class VISIBILITY_HIDDEN PCHStatCacheTrait {
642public:
643 typedef const char * key_type;
644 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000645
Douglas Gregorc5046832009-04-27 18:38:38 +0000646 typedef std::pair<int, struct stat> data_type;
647 typedef const data_type& data_type_ref;
648
649 static unsigned ComputeHash(const char *path) {
650 return BernsteinHash(path);
651 }
Mike Stump11289f42009-09-09 15:08:12 +0000652
653 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000654 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
655 data_type_ref Data) {
656 unsigned StrLen = strlen(path);
657 clang::io::Emit16(Out, StrLen);
658 unsigned DataLen = 1; // result value
659 if (Data.first == 0)
660 DataLen += 4 + 4 + 2 + 8 + 8;
661 clang::io::Emit8(Out, DataLen);
662 return std::make_pair(StrLen + 1, DataLen);
663 }
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregorc5046832009-04-27 18:38:38 +0000665 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
666 Out.write(path, KeyLen);
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregorc5046832009-04-27 18:38:38 +0000669 void EmitData(llvm::raw_ostream& Out, key_type_ref,
670 data_type_ref Data, unsigned DataLen) {
671 using namespace clang::io;
672 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Douglas Gregorc5046832009-04-27 18:38:38 +0000674 // Result of stat()
675 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregorc5046832009-04-27 18:38:38 +0000677 if (Data.first == 0) {
678 Emit32(Out, (uint32_t) Data.second.st_ino);
679 Emit32(Out, (uint32_t) Data.second.st_dev);
680 Emit16(Out, (uint16_t) Data.second.st_mode);
681 Emit64(Out, (uint64_t) Data.second.st_mtime);
682 Emit64(Out, (uint64_t) Data.second.st_size);
683 }
684
685 assert(Out.tell() - Start == DataLen && "Wrong data length");
686 }
687};
688} // end anonymous namespace
689
690/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000691void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
692 const char *isysroot) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000693 // Build the on-disk hash table containing information about every
694 // stat() call.
695 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
696 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000697 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000698 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000699 Stat != StatEnd; ++Stat, ++NumStatEntries) {
700 const char *Filename = Stat->first();
701 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
702 Generator.insert(Filename, Stat->second);
703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Douglas Gregorc5046832009-04-27 18:38:38 +0000705 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000706 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000707 uint32_t BucketOffset;
708 {
709 llvm::raw_svector_ostream Out(StatCacheData);
710 // Make sure that no bucket is at offset 0
711 clang::io::Emit32(Out, 0);
712 BucketOffset = Generator.Emit(Out);
713 }
714
715 // Create a blob abbreviation
716 using namespace llvm;
717 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
718 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
721 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
722 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
723
724 // Write the stat cache
725 RecordData Record;
726 Record.push_back(pch::STAT_CACHE);
727 Record.push_back(BucketOffset);
728 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000729 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000730}
731
732//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000733// Source Manager Serialization
734//===----------------------------------------------------------------------===//
735
736/// \brief Create an abbreviation for the SLocEntry that refers to a
737/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000738static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000739 using namespace llvm;
740 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
741 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregora7f71a92009-04-10 03:52:48 +0000746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +0000747 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000748}
749
750/// \brief Create an abbreviation for the SLocEntry that refers to a
751/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000752static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000753 using namespace llvm;
754 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
755 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
760 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000761 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000762}
763
764/// \brief Create an abbreviation for the SLocEntry that refers to a
765/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000766static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000767 using namespace llvm;
768 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
769 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +0000771 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000772}
773
774/// \brief Create an abbreviation for the SLocEntry that refers to an
775/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000776static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000777 using namespace llvm;
778 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
779 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +0000784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +0000785 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000786}
787
788/// \brief Writes the block containing the serialized form of the
789/// source manager.
790///
791/// TODO: We should probably use an on-disk hash table (stored in a
792/// blob), indexed based on the file name, so that we only create
793/// entries for files that we actually need. In the common case (no
794/// errors), we probably won't have to create file entries for any of
795/// the files in the AST.
Douglas Gregoreda6a892009-04-26 00:07:37 +0000796void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000797 const Preprocessor &PP,
798 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000799 RecordData Record;
800
Chris Lattner0910e3b2009-04-10 17:16:57 +0000801 // Enter the source manager block.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000802 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000803
804 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +0000805 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
806 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
807 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
808 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000809
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000810 // Write the line table.
811 if (SourceMgr.hasLineTable()) {
812 LineTableInfo &LineTable = SourceMgr.getLineTable();
813
814 // Emit the file names
815 Record.push_back(LineTable.getNumFilenames());
816 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
817 // Emit the file name
818 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000819 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000820 unsigned FilenameLen = Filename? strlen(Filename) : 0;
821 Record.push_back(FilenameLen);
822 if (FilenameLen)
823 Record.insert(Record.end(), Filename, Filename + FilenameLen);
824 }
Mike Stump11289f42009-09-09 15:08:12 +0000825
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000826 // Emit the line entries
827 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
828 L != LEnd; ++L) {
829 // Emit the file ID
830 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +0000831
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000832 // Emit the line entries
833 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +0000834 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000835 LEEnd = L->second.end();
836 LE != LEEnd; ++LE) {
837 Record.push_back(LE->FileOffset);
838 Record.push_back(LE->LineNo);
839 Record.push_back(LE->FilenameID);
840 Record.push_back((unsigned)LE->FileKind);
841 Record.push_back(LE->IncludeOffset);
842 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000843 }
Zhongxing Xu5a187dd2009-05-22 08:38:27 +0000844 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000845 }
846
Douglas Gregor258ae542009-04-27 06:38:32 +0000847 // Write out entries for all of the header files we know about.
Mike Stump11289f42009-09-09 15:08:12 +0000848 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor258ae542009-04-27 06:38:32 +0000849 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000850 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
Douglas Gregoreda6a892009-04-26 00:07:37 +0000851 E = HS.header_file_end();
852 I != E; ++I) {
853 Record.push_back(I->isImport);
854 Record.push_back(I->DirInfo);
855 Record.push_back(I->NumIncludes);
Douglas Gregor258ae542009-04-27 06:38:32 +0000856 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +0000857 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
858 Record.clear();
859 }
860
Douglas Gregor258ae542009-04-27 06:38:32 +0000861 // Write out the source location entry table. We skip the first
862 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +0000863 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +0000864 RecordData PreloadSLocs;
865 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
Mike Stump11289f42009-09-09 15:08:12 +0000866 for (SourceManager::sloc_entry_iterator
Douglas Gregor258ae542009-04-27 06:38:32 +0000867 SLoc = SourceMgr.sloc_entry_begin() + 1,
868 SLocEnd = SourceMgr.sloc_entry_end();
869 SLoc != SLocEnd; ++SLoc) {
870 // Record the offset of this source-location entry.
871 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
872
873 // Figure out which record code to use.
874 unsigned Code;
875 if (SLoc->isFile()) {
876 if (SLoc->getFile().getContentCache()->Entry)
877 Code = pch::SM_SLOC_FILE_ENTRY;
878 else
879 Code = pch::SM_SLOC_BUFFER_ENTRY;
880 } else
881 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
882 Record.clear();
883 Record.push_back(Code);
884
885 Record.push_back(SLoc->getOffset());
886 if (SLoc->isFile()) {
887 const SrcMgr::FileInfo &File = SLoc->getFile();
888 Record.push_back(File.getIncludeLoc().getRawEncoding());
889 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
890 Record.push_back(File.hasLineDirectives());
891
892 const SrcMgr::ContentCache *Content = File.getContentCache();
893 if (Content->Entry) {
894 // The source location entry is a file. The blob associated
895 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +0000896
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000897 // Turn the file name into an absolute path, if it isn't already.
898 const char *Filename = Content->Entry->getName();
899 llvm::sys::Path FilePath(Filename, strlen(Filename));
900 std::string FilenameStr;
901 if (!FilePath.isAbsolute()) {
902 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000903 P.appendComponent(FilePath.str());
904 FilenameStr = P.str();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000905 Filename = FilenameStr.c_str();
906 }
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000908 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000909 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +0000910
911 // FIXME: For now, preload all file source locations, so that
912 // we get the appropriate File entries in the reader. This is
913 // a temporary measure.
914 PreloadSLocs.push_back(SLocEntryOffsets.size());
915 } else {
916 // The source location entry is a buffer. The blob associated
917 // with this entry contains the contents of the buffer.
918
919 // We add one to the size so that we capture the trailing NULL
920 // that is required by llvm::MemoryBuffer::getMemBuffer (on
921 // the reader side).
922 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
923 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +0000924 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
925 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +0000926 Record.clear();
927 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
928 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +0000929 llvm::StringRef(Buffer->getBufferStart(),
930 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +0000931
932 if (strcmp(Name, "<built-in>") == 0)
933 PreloadSLocs.push_back(SLocEntryOffsets.size());
934 }
935 } else {
936 // The source location entry is an instantiation.
937 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
938 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
939 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
940 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
941
942 // Compute the token length for this macro expansion.
943 unsigned NextOffset = SourceMgr.getNextOffset();
944 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
945 if (++NextSLoc != SLocEnd)
946 NextOffset = NextSLoc->getOffset();
947 Record.push_back(NextOffset - SLoc->getOffset() - 1);
948 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
949 }
950 }
951
Douglas Gregor8f45df52009-04-16 22:23:12 +0000952 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +0000953
954 if (SLocEntryOffsets.empty())
955 return;
956
957 // Write the source-location offsets table into the PCH block. This
958 // table is used for lazily loading source-location information.
959 using namespace llvm;
960 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
961 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
965 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Douglas Gregor258ae542009-04-27 06:38:32 +0000967 Record.clear();
968 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
969 Record.push_back(SLocEntryOffsets.size());
970 Record.push_back(SourceMgr.getNextOffset());
971 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +0000972 (const char *)&SLocEntryOffsets.front(),
Chris Lattner12d61d32009-04-27 19:01:47 +0000973 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +0000974
975 // Write the source location entry preloads array, telling the PCH
976 // reader which source locations entries it should load eagerly.
977 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +0000978}
979
Douglas Gregorc5046832009-04-27 18:38:38 +0000980//===----------------------------------------------------------------------===//
981// Preprocessor Serialization
982//===----------------------------------------------------------------------===//
983
Chris Lattnereeffaef2009-04-10 17:15:23 +0000984/// \brief Writes the block containing the serialized form of the
985/// preprocessor.
986///
Chris Lattner2199f5b2009-04-10 18:08:30 +0000987void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +0000988 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +0000989
Chris Lattner0af3ba12009-04-13 01:29:17 +0000990 // If the preprocessor __COUNTER__ value has been bumped, remember it.
991 if (PP.getCounterValue() != 0) {
992 Record.push_back(PP.getCounterValue());
Douglas Gregor8f45df52009-04-16 22:23:12 +0000993 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +0000994 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +0000995 }
996
997 // Enter the preprocessor block.
998 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregoreda6a892009-04-26 00:07:37 +00001000 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1001 // FIXME: use diagnostics subsystem for localization etc.
1002 if (PP.SawDateOrTime())
1003 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001004
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001005 // Loop over all the macro definitions that are live at the end of the file,
1006 // emitting each to the PP section.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001007 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1008 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001009 // FIXME: This emits macros in hash table order, we should do it in a stable
1010 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001011 MacroInfo *MI = I->second;
1012
1013 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1014 // been redefined by the header (in which case they are not isBuiltinMacro).
1015 if (MI->isBuiltinMacro())
1016 continue;
1017
Douglas Gregorc3366a52009-04-21 23:56:24 +00001018 // FIXME: Remove this identifier reference?
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001019 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001020 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001021 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1022 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001024 unsigned Code;
1025 if (MI->isObjectLike()) {
1026 Code = pch::PP_MACRO_OBJECT_LIKE;
1027 } else {
1028 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001030 Record.push_back(MI->isC99Varargs());
1031 Record.push_back(MI->isGNUVarargs());
1032 Record.push_back(MI->getNumArgs());
1033 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1034 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001035 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001036 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001037 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001038 Record.clear();
1039
Chris Lattner2199f5b2009-04-10 18:08:30 +00001040 // Emit the tokens array.
1041 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1042 // Note that we know that the preprocessor does not have any annotation
1043 // tokens in it because they are created by the parser, and thus can't be
1044 // in a macro definition.
1045 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001046
Chris Lattner2199f5b2009-04-10 18:08:30 +00001047 Record.push_back(Tok.getLocation().getRawEncoding());
1048 Record.push_back(Tok.getLength());
1049
Chris Lattner2199f5b2009-04-10 18:08:30 +00001050 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1051 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001052 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattner2199f5b2009-04-10 18:08:30 +00001054 // FIXME: Should translate token kind to a stable encoding.
1055 Record.push_back(Tok.getKind());
1056 // FIXME: Should translate token flags to a stable encoding.
1057 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001058
Douglas Gregor8f45df52009-04-16 22:23:12 +00001059 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001060 Record.clear();
1061 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001062 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001063 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001064 Stream.ExitBlock();
Chris Lattnereeffaef2009-04-10 17:15:23 +00001065}
1066
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001067void PCHWriter::WriteComments(ASTContext &Context) {
1068 using namespace llvm;
Mike Stump11289f42009-09-09 15:08:12 +00001069
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001070 if (Context.Comments.empty())
1071 return;
Mike Stump11289f42009-09-09 15:08:12 +00001072
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001073 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1074 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1075 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1076 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001077
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001078 RecordData Record;
1079 Record.push_back(pch::COMMENT_RANGES);
Mike Stump11289f42009-09-09 15:08:12 +00001080 Stream.EmitRecordWithBlob(CommentCode, Record,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001081 (const char*)&Context.Comments[0],
1082 Context.Comments.size() * sizeof(SourceRange));
1083}
1084
Douglas Gregorc5046832009-04-27 18:38:38 +00001085//===----------------------------------------------------------------------===//
1086// Type Serialization
1087//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001088
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001089/// \brief Write the representation of a type to the PCH stream.
1090void PCHWriter::WriteType(const Type *T) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001091 pch::TypeID &ID = TypeIDs[T];
Chris Lattner0910e3b2009-04-10 17:16:57 +00001092 if (ID == 0) // we haven't seen this type before.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001093 ID = NextTypeID++;
Mike Stump11289f42009-09-09 15:08:12 +00001094
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001095 // Record the offset for this type.
1096 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001097 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001098 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1099 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001100 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001101 }
1102
1103 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001105 // Emit the type's representation.
1106 PCHTypeWriter W(*this, Record);
1107 switch (T->getTypeClass()) {
1108 // For all of the concrete, non-dependent types, call the
1109 // appropriate visitor function.
1110#define TYPE(Class, Base) \
1111 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1112#define ABSTRACT_TYPE(Class, Base)
1113#define DEPENDENT_TYPE(Class, Base)
1114#include "clang/AST/TypeNodes.def"
1115
1116 // For all of the dependent type nodes (which only occur in C++
1117 // templates), produce an error.
1118#define TYPE(Class, Base)
1119#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1120#include "clang/AST/TypeNodes.def"
1121 assert(false && "Cannot serialize dependent type nodes");
1122 break;
1123 }
1124
1125 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001126 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001127
1128 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001129 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001130}
1131
1132/// \brief Write a block containing all of the types.
1133void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner0910e3b2009-04-10 17:16:57 +00001134 // Enter the types block.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001135 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001136
Douglas Gregor1970d882009-04-26 03:49:13 +00001137 // Emit all of the types that need to be emitted (so far).
1138 while (!TypesToEmit.empty()) {
1139 const Type *T = TypesToEmit.front();
1140 TypesToEmit.pop();
1141 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1142 WriteType(T);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001143 }
1144
1145 // Exit the types block
Douglas Gregor8f45df52009-04-16 22:23:12 +00001146 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001147}
1148
Douglas Gregorc5046832009-04-27 18:38:38 +00001149//===----------------------------------------------------------------------===//
1150// Declaration Serialization
1151//===----------------------------------------------------------------------===//
1152
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001153/// \brief Write the block containing all of the declaration IDs
1154/// lexically declared within the given DeclContext.
1155///
1156/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1157/// bistream, or 0 if no block was written.
Mike Stump11289f42009-09-09 15:08:12 +00001158uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001159 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001160 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001161 return 0;
1162
Douglas Gregor8f45df52009-04-16 22:23:12 +00001163 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001164 RecordData Record;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001165 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1166 D != DEnd; ++D)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001167 AddDeclRef(*D, Record);
1168
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001169 ++NumLexicalDeclContexts;
Douglas Gregor8f45df52009-04-16 22:23:12 +00001170 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001171 return Offset;
1172}
1173
1174/// \brief Write the block containing all of the declaration IDs
1175/// visible from the given DeclContext.
1176///
1177/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1178/// bistream, or 0 if no block was written.
1179uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1180 DeclContext *DC) {
1181 if (DC->getPrimaryContext() != DC)
1182 return 0;
1183
Douglas Gregorb475a5c2009-04-21 22:32:33 +00001184 // Since there is no name lookup into functions or methods, and we
1185 // perform name lookup for the translation unit via the
1186 // IdentifierInfo chains, don't bother to build a
1187 // visible-declarations table for these entities.
1188 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor13d190f2009-04-18 15:49:20 +00001189 return 0;
1190
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001191 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001192 DC->lookup(DeclarationName());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001193
1194 // Serialize the contents of the mapping used for lookup. Note that,
1195 // although we have two very different code paths, the serialized
1196 // representation is the same for both cases: a declaration name,
1197 // followed by a size, followed by references to the visible
1198 // declarations that have that name.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001199 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001200 RecordData Record;
1201 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor183671e2009-04-13 21:20:57 +00001202 if (!Map)
1203 return 0;
1204
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001205 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1206 D != DEnd; ++D) {
1207 AddDeclarationName(D->first, Record);
1208 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1209 Record.push_back(Result.second - Result.first);
Mike Stump11289f42009-09-09 15:08:12 +00001210 for (; Result.first != Result.second; ++Result.first)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001211 AddDeclRef(*Result.first, Record);
1212 }
1213
1214 if (Record.size() == 0)
1215 return 0;
1216
Douglas Gregor8f45df52009-04-16 22:23:12 +00001217 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001218 ++NumVisibleDeclContexts;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001219 return Offset;
1220}
1221
Douglas Gregorc5046832009-04-27 18:38:38 +00001222//===----------------------------------------------------------------------===//
1223// Global Method Pool and Selector Serialization
1224//===----------------------------------------------------------------------===//
1225
Douglas Gregore84a9da2009-04-20 20:36:09 +00001226namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001227// Trait used for the on-disk hash table used in the method pool.
1228class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1229 PCHWriter &Writer;
1230
1231public:
1232 typedef Selector key_type;
1233 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001234
Douglas Gregorc78d3462009-04-24 21:10:55 +00001235 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1236 typedef const data_type& data_type_ref;
1237
1238 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregorc78d3462009-04-24 21:10:55 +00001240 static unsigned ComputeHash(Selector Sel) {
1241 unsigned N = Sel.getNumArgs();
1242 if (N == 0)
1243 ++N;
1244 unsigned R = 5381;
1245 for (unsigned I = 0; I != N; ++I)
1246 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1247 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1248 return R;
1249 }
Mike Stump11289f42009-09-09 15:08:12 +00001250
1251 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001252 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1253 data_type_ref Methods) {
1254 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1255 clang::io::Emit16(Out, KeyLen);
1256 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
Mike Stump11289f42009-09-09 15:08:12 +00001257 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001258 Method = Method->Next)
1259 if (Method->Method)
1260 DataLen += 4;
Mike Stump11289f42009-09-09 15:08:12 +00001261 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001262 Method = Method->Next)
1263 if (Method->Method)
1264 DataLen += 4;
1265 clang::io::Emit16(Out, DataLen);
1266 return std::make_pair(KeyLen, DataLen);
1267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
Douglas Gregor95c13f52009-04-25 17:48:32 +00001269 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001270 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001271 assert((Start >> 32) == 0 && "Selector key offset too large");
1272 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001273 unsigned N = Sel.getNumArgs();
1274 clang::io::Emit16(Out, N);
1275 if (N == 0)
1276 N = 1;
1277 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001278 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001279 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregorc78d3462009-04-24 21:10:55 +00001282 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001283 data_type_ref Methods, unsigned DataLen) {
1284 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001285 unsigned NumInstanceMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001286 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001287 Method = Method->Next)
1288 if (Method->Method)
1289 ++NumInstanceMethods;
1290
1291 unsigned NumFactoryMethods = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001292 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001293 Method = Method->Next)
1294 if (Method->Method)
1295 ++NumFactoryMethods;
1296
1297 clang::io::Emit16(Out, NumInstanceMethods);
1298 clang::io::Emit16(Out, NumFactoryMethods);
Mike Stump11289f42009-09-09 15:08:12 +00001299 for (const ObjCMethodList *Method = &Methods.first; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001300 Method = Method->Next)
1301 if (Method->Method)
1302 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Mike Stump11289f42009-09-09 15:08:12 +00001303 for (const ObjCMethodList *Method = &Methods.second; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001304 Method = Method->Next)
1305 if (Method->Method)
1306 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001307
1308 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001309 }
1310};
1311} // end anonymous namespace
1312
1313/// \brief Write the method pool into the PCH file.
1314///
1315/// The method pool contains both instance and factory methods, stored
1316/// in an on-disk hash table indexed by the selector.
1317void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1318 using namespace llvm;
1319
1320 // Create and write out the blob that contains the instance and
1321 // factor method pools.
1322 bool Empty = true;
1323 {
1324 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001325
Douglas Gregorc78d3462009-04-24 21:10:55 +00001326 // Create the on-disk hash table representation. Start by
1327 // iterating through the instance method pool.
1328 PCHMethodPoolTrait::key_type Key;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001329 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001330 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001331 Instance = SemaRef.InstanceMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001332 InstanceEnd = SemaRef.InstanceMethodPool.end();
1333 Instance != InstanceEnd; ++Instance) {
1334 // Check whether there is a factory method with the same
1335 // selector.
1336 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1337 = SemaRef.FactoryMethodPool.find(Instance->first);
1338
1339 if (Factory == SemaRef.FactoryMethodPool.end())
1340 Generator.insert(Instance->first,
Mike Stump11289f42009-09-09 15:08:12 +00001341 std::make_pair(Instance->second,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001342 ObjCMethodList()));
1343 else
1344 Generator.insert(Instance->first,
1345 std::make_pair(Instance->second, Factory->second));
1346
Douglas Gregor95c13f52009-04-25 17:48:32 +00001347 ++NumSelectorsInMethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001348 Empty = false;
1349 }
1350
1351 // Now iterate through the factory method pool, to pick up any
1352 // selectors that weren't already in the instance method pool.
1353 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
Mike Stump11289f42009-09-09 15:08:12 +00001354 Factory = SemaRef.FactoryMethodPool.begin(),
Douglas Gregorc78d3462009-04-24 21:10:55 +00001355 FactoryEnd = SemaRef.FactoryMethodPool.end();
1356 Factory != FactoryEnd; ++Factory) {
1357 // Check whether there is an instance method with the same
1358 // selector. If so, there is no work to do here.
1359 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1360 = SemaRef.InstanceMethodPool.find(Factory->first);
1361
Douglas Gregor95c13f52009-04-25 17:48:32 +00001362 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001363 Generator.insert(Factory->first,
1364 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001365 ++NumSelectorsInMethodPool;
1366 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00001367
1368 Empty = false;
1369 }
1370
Douglas Gregor95c13f52009-04-25 17:48:32 +00001371 if (Empty && SelectorOffsets.empty())
Douglas Gregorc78d3462009-04-24 21:10:55 +00001372 return;
1373
1374 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001375 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001376 uint32_t BucketOffset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00001377 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc78d3462009-04-24 21:10:55 +00001378 {
1379 PCHMethodPoolTrait Trait(*this);
1380 llvm::raw_svector_ostream Out(MethodPool);
1381 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001382 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001383 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001384
1385 // For every selector that we have seen but which was not
1386 // written into the hash table, write the selector itself and
1387 // record it's offset.
1388 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1389 if (SelectorOffsets[I] == 0)
1390 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001391 }
1392
1393 // Create a blob abbreviation
1394 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1395 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1399 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1400
Douglas Gregor95c13f52009-04-25 17:48:32 +00001401 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001402 RecordData Record;
1403 Record.push_back(pch::METHOD_POOL);
1404 Record.push_back(BucketOffset);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001405 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001406 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001407
1408 // Create a blob abbreviation for the selector table offsets.
1409 Abbrev = new BitCodeAbbrev();
1410 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1413 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1414
1415 // Write the selector offsets table.
1416 Record.clear();
1417 Record.push_back(pch::SELECTOR_OFFSETS);
1418 Record.push_back(SelectorOffsets.size());
1419 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1420 (const char *)&SelectorOffsets.front(),
1421 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001422 }
1423}
1424
Douglas Gregorc5046832009-04-27 18:38:38 +00001425//===----------------------------------------------------------------------===//
1426// Identifier Table Serialization
1427//===----------------------------------------------------------------------===//
1428
Douglas Gregorc78d3462009-04-24 21:10:55 +00001429namespace {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001430class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1431 PCHWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001432 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001433
Douglas Gregor1d583f22009-04-28 21:18:29 +00001434 /// \brief Determines whether this is an "interesting" identifier
1435 /// that needs a full IdentifierInfo structure written into the hash
1436 /// table.
1437 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1438 return II->isPoisoned() ||
1439 II->isExtensionToken() ||
1440 II->hasMacroDefinition() ||
1441 II->getObjCOrBuiltinID() ||
1442 II->getFETokenInfo<void>();
1443 }
1444
Douglas Gregore84a9da2009-04-20 20:36:09 +00001445public:
1446 typedef const IdentifierInfo* key_type;
1447 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001448
Douglas Gregore84a9da2009-04-20 20:36:09 +00001449 typedef pch::IdentID data_type;
1450 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001451
1452 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001453 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001454
1455 static unsigned ComputeHash(const IdentifierInfo* II) {
1456 return clang::BernsteinHash(II->getName());
1457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
1459 std::pair<unsigned,unsigned>
1460 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001461 pch::IdentID ID) {
1462 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001463 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1464 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001465 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001466 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001467 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001468 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001469 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1470 DEnd = IdentifierResolver::end();
1471 D != DEnd; ++D)
1472 DataLen += sizeof(pch::DeclID);
1473 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001474 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001475 // We emit the key length after the data length so that every
1476 // string is preceded by a 16-bit length. This matches the PTH
1477 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001478 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001479 return std::make_pair(KeyLen, DataLen);
1480 }
Mike Stump11289f42009-09-09 15:08:12 +00001481
1482 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001483 unsigned KeyLen) {
1484 // Record the location of the key data. This is used when generating
1485 // the mapping from persistent IDs to strings.
1486 Writer.SetIdentifierOffset(II, Out.tell());
1487 Out.write(II->getName(), KeyLen);
1488 }
Mike Stump11289f42009-09-09 15:08:12 +00001489
1490 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001491 pch::IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001492 if (!isInterestingIdentifier(II)) {
1493 clang::io::Emit32(Out, ID << 1);
1494 return;
1495 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001496
Douglas Gregor1d583f22009-04-28 21:18:29 +00001497 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001498 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001499 bool hasMacroDefinition =
1500 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001501 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001502 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001503 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001504 Bits = (Bits << 1) | II->isExtensionToken();
1505 Bits = (Bits << 1) | II->isPoisoned();
1506 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregorb9256522009-04-28 21:32:13 +00001507 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001508
Douglas Gregorc3366a52009-04-21 23:56:24 +00001509 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001510 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001511
Douglas Gregora868bbd2009-04-21 22:25:48 +00001512 // Emit the declaration IDs in reverse order, because the
1513 // IdentifierResolver provides the declarations as they would be
1514 // visible (e.g., the function "stat" would come before the struct
1515 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1516 // adds declarations to the end of the list (so we need to see the
1517 // struct "status" before the function "status").
Mike Stump11289f42009-09-09 15:08:12 +00001518 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001519 IdentifierResolver::end());
1520 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1521 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001522 D != DEnd; ++D)
Douglas Gregora868bbd2009-04-21 22:25:48 +00001523 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001524 }
1525};
1526} // end anonymous namespace
1527
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001528/// \brief Write the identifier table into the PCH file.
1529///
1530/// The identifier table consists of a blob containing string data
1531/// (the actual identifiers themselves) and a separate "offsets" index
1532/// that maps identifier IDs to locations within the blob.
Douglas Gregorc3366a52009-04-21 23:56:24 +00001533void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001534 using namespace llvm;
1535
1536 // Create and write out the blob that contains the identifier
1537 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001538 {
Douglas Gregore84a9da2009-04-20 20:36:09 +00001539 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
Mike Stump11289f42009-09-09 15:08:12 +00001540
Douglas Gregore6648fb2009-04-28 20:33:11 +00001541 // Look for any identifiers that were named while processing the
1542 // headers, but are otherwise not needed. We add these to the hash
1543 // table to enable checking of the predefines buffer in the case
1544 // where the user adds new macro definitions when building the PCH
1545 // file.
1546 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1547 IDEnd = PP.getIdentifierTable().end();
1548 ID != IDEnd; ++ID)
1549 getIdentifierRef(ID->second);
1550
Douglas Gregore84a9da2009-04-20 20:36:09 +00001551 // Create the on-disk hash table representation.
Douglas Gregore6648fb2009-04-28 20:33:11 +00001552 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001553 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1554 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1555 ID != IDEnd; ++ID) {
1556 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregorab4df582009-04-28 20:01:51 +00001557 Generator.insert(ID->first, ID->second);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001558 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001559
Douglas Gregore84a9da2009-04-20 20:36:09 +00001560 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001561 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001562 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001563 {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001564 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001565 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001566 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001567 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001568 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001569 }
1570
1571 // Create a blob abbreviation
1572 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1573 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001576 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001577
1578 // Write the identifier table
1579 RecordData Record;
1580 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001581 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001582 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001583 }
1584
1585 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001586 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1587 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1590 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1591
1592 RecordData Record;
1593 Record.push_back(pch::IDENTIFIER_OFFSET);
1594 Record.push_back(IdentifierOffsets.size());
1595 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1596 (const char *)&IdentifierOffsets.front(),
1597 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001598}
1599
Douglas Gregorc5046832009-04-27 18:38:38 +00001600//===----------------------------------------------------------------------===//
1601// General Serialization Routines
1602//===----------------------------------------------------------------------===//
1603
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001604/// \brief Write a record containing the given attributes.
1605void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1606 RecordData Record;
1607 for (; Attr; Attr = Attr->getNext()) {
1608 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1609 Record.push_back(Attr->isInherited());
1610 switch (Attr->getKind()) {
1611 case Attr::Alias:
1612 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1613 break;
1614
1615 case Attr::Aligned:
1616 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1617 break;
1618
1619 case Attr::AlwaysInline:
1620 break;
Mike Stump11289f42009-09-09 15:08:12 +00001621
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001622 case Attr::AnalyzerNoReturn:
1623 break;
1624
1625 case Attr::Annotate:
1626 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1627 break;
1628
1629 case Attr::AsmLabel:
1630 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1631 break;
1632
1633 case Attr::Blocks:
1634 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1635 break;
1636
1637 case Attr::Cleanup:
1638 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1639 break;
1640
1641 case Attr::Const:
1642 break;
1643
1644 case Attr::Constructor:
1645 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1646 break;
1647
1648 case Attr::DLLExport:
1649 case Attr::DLLImport:
1650 case Attr::Deprecated:
1651 break;
1652
1653 case Attr::Destructor:
1654 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1655 break;
1656
1657 case Attr::FastCall:
1658 break;
1659
1660 case Attr::Format: {
1661 const FormatAttr *Format = cast<FormatAttr>(Attr);
1662 AddString(Format->getType(), Record);
1663 Record.push_back(Format->getFormatIdx());
1664 Record.push_back(Format->getFirstArg());
1665 break;
1666 }
1667
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001668 case Attr::FormatArg: {
1669 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1670 Record.push_back(Format->getFormatIdx());
1671 break;
1672 }
1673
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001674 case Attr::Sentinel : {
1675 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1676 Record.push_back(Sentinel->getSentinel());
1677 Record.push_back(Sentinel->getNullPos());
1678 break;
1679 }
Mike Stump11289f42009-09-09 15:08:12 +00001680
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001681 case Attr::GNUInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001682 case Attr::IBOutletKind:
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001683 case Attr::Malloc:
Mike Stump3722f582009-08-26 22:31:08 +00001684 case Attr::NoDebug:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001685 case Attr::NoReturn:
1686 case Attr::NoThrow:
Mike Stump3722f582009-08-26 22:31:08 +00001687 case Attr::NoInline:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001688 break;
1689
1690 case Attr::NonNull: {
1691 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1692 Record.push_back(NonNull->size());
1693 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1694 break;
1695 }
1696
1697 case Attr::ObjCException:
1698 case Attr::ObjCNSObject:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001699 case Attr::CFReturnsRetained:
1700 case Attr::NSReturnsRetained:
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001701 case Attr::Overloadable:
1702 break;
1703
Anders Carlsson68e0b682009-08-08 18:23:56 +00001704 case Attr::PragmaPack:
1705 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001706 break;
1707
Anders Carlsson68e0b682009-08-08 18:23:56 +00001708 case Attr::Packed:
1709 break;
Mike Stump11289f42009-09-09 15:08:12 +00001710
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001711 case Attr::Pure:
1712 break;
1713
1714 case Attr::Regparm:
1715 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1716 break;
Mike Stump11289f42009-09-09 15:08:12 +00001717
Nate Begemanf2758702009-06-26 06:32:41 +00001718 case Attr::ReqdWorkGroupSize:
1719 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1720 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1721 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1722 break;
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001723
1724 case Attr::Section:
1725 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1726 break;
1727
1728 case Attr::StdCall:
1729 case Attr::TransparentUnion:
1730 case Attr::Unavailable:
1731 case Attr::Unused:
1732 case Attr::Used:
1733 break;
1734
1735 case Attr::Visibility:
1736 // FIXME: stable encoding
Mike Stump11289f42009-09-09 15:08:12 +00001737 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001738 break;
1739
1740 case Attr::WarnUnusedResult:
1741 case Attr::Weak:
1742 case Attr::WeakImport:
1743 break;
1744 }
1745 }
1746
Douglas Gregor8f45df52009-04-16 22:23:12 +00001747 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00001748}
1749
1750void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1751 Record.push_back(Str.size());
1752 Record.insert(Record.end(), Str.begin(), Str.end());
1753}
1754
Douglas Gregore84a9da2009-04-20 20:36:09 +00001755/// \brief Note that the identifier II occurs at the given offset
1756/// within the identifier table.
1757void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor0e149972009-04-25 19:10:14 +00001758 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001759}
1760
Douglas Gregor95c13f52009-04-25 17:48:32 +00001761/// \brief Note that the selector Sel occurs at the given offset
1762/// within the method pool/selector table.
1763void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1764 unsigned ID = SelectorIDs[Sel];
1765 assert(ID && "Unknown selector");
1766 SelectorOffsets[ID - 1] = Offset;
1767}
1768
Mike Stump11289f42009-09-09 15:08:12 +00001769PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1770 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001771 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1772 NumVisibleDeclContexts(0) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001773
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001774void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1775 const char *isysroot) {
Douglas Gregor745ed142009-04-25 18:35:21 +00001776 using namespace llvm;
1777
Douglas Gregor162dd022009-04-20 15:53:59 +00001778 ASTContext &Context = SemaRef.Context;
1779 Preprocessor &PP = SemaRef.PP;
1780
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001781 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001782 Stream.Emit((unsigned)'C', 8);
1783 Stream.Emit((unsigned)'P', 8);
1784 Stream.Emit((unsigned)'C', 8);
1785 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Chris Lattner28fa4e62009-04-26 22:26:21 +00001787 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001788
1789 // The translation unit is the first declaration we'll emit.
1790 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1791 DeclsToEmit.push(Context.getTranslationUnitDecl());
1792
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001793 // Make sure that we emit IdentifierInfos (and any attached
1794 // declarations) for builtins.
1795 {
1796 IdentifierTable &Table = PP.getIdentifierTable();
1797 llvm::SmallVector<const char *, 32> BuiltinNames;
1798 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1799 Context.getLangOptions().NoBuiltin);
1800 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1801 getIdentifierRef(&Table.get(BuiltinNames[I]));
1802 }
1803
Chris Lattner0c797362009-09-08 18:19:27 +00001804 // Build a record containing all of the tentative definitions in this file, in
1805 // TentativeDefinitionList order. Generally, this record will be empty for
1806 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00001807 RecordData TentativeDefinitions;
Chris Lattner0c797362009-09-08 18:19:27 +00001808 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1809 VarDecl *VD =
1810 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1811 if (VD) AddDeclRef(VD, TentativeDefinitions);
1812 }
Douglas Gregord4df8652009-04-22 22:02:47 +00001813
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001814 // Build a record containing all of the locally-scoped external
1815 // declarations in this header file. Generally, this record will be
1816 // empty.
1817 RecordData LocallyScopedExternalDecls;
Chris Lattner0c797362009-09-08 18:19:27 +00001818 // FIXME: This is filling in the PCH file in densemap order which is
1819 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00001820 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001821 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1822 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1823 TD != TDEnd; ++TD)
1824 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1825
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001826 // Build a record containing all of the ext_vector declarations.
1827 RecordData ExtVectorDecls;
1828 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1829 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1830
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001831 // Write the remaining PCH contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00001832 RecordData Record;
Douglas Gregor745ed142009-04-25 18:35:21 +00001833 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001834 WriteMetadata(Context, isysroot);
Douglas Gregor55abb232009-04-10 20:39:37 +00001835 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001836 if (StatCalls && !isysroot)
1837 WriteStatCache(*StatCalls, isysroot);
1838 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Chris Lattnereeffaef2009-04-10 17:15:23 +00001839 WritePreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +00001840 WriteComments(Context);
Steve Naroffc277ad12009-07-18 15:33:26 +00001841 // Write the record of special types.
1842 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001843
Steve Naroffc277ad12009-07-18 15:33:26 +00001844 AddTypeRef(Context.getBuiltinVaListType(), Record);
1845 AddTypeRef(Context.getObjCIdType(), Record);
1846 AddTypeRef(Context.getObjCSelType(), Record);
1847 AddTypeRef(Context.getObjCProtoType(), Record);
1848 AddTypeRef(Context.getObjCClassType(), Record);
1849 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1850 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1851 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00001852 AddTypeRef(Context.getjmp_bufType(), Record);
1853 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00001854 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1855 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroffc277ad12009-07-18 15:33:26 +00001856 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001857
Douglas Gregor1970d882009-04-26 03:49:13 +00001858 // Keep writing types and declarations until all types and
1859 // declarations have been written.
1860 do {
1861 if (!DeclsToEmit.empty())
1862 WriteDeclsBlock(Context);
1863 if (!TypesToEmit.empty())
1864 WriteTypesBlock(Context);
1865 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1866
Douglas Gregorc78d3462009-04-24 21:10:55 +00001867 WriteMethodPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001868 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00001869
1870 // Write the type offsets array
1871 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1872 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1875 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1876 Record.clear();
1877 Record.push_back(pch::TYPE_OFFSET);
1878 Record.push_back(TypeOffsets.size());
1879 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001880 (const char *)&TypeOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00001881 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Mike Stump11289f42009-09-09 15:08:12 +00001882
Douglas Gregor745ed142009-04-25 18:35:21 +00001883 // Write the declaration offsets array
1884 Abbrev = new BitCodeAbbrev();
1885 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1888 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1889 Record.clear();
1890 Record.push_back(pch::DECL_OFFSET);
1891 Record.push_back(DeclOffsets.size());
1892 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Mike Stump11289f42009-09-09 15:08:12 +00001893 (const char *)&DeclOffsets.front(),
Chris Lattnereeb05692009-04-27 18:24:17 +00001894 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00001895
Douglas Gregord4df8652009-04-22 22:02:47 +00001896 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00001897 if (!ExternalDefinitions.empty())
Douglas Gregor8f45df52009-04-16 22:23:12 +00001898 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00001899
1900 // Write the record containing tentative definitions.
1901 if (!TentativeDefinitions.empty())
1902 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001903
1904 // Write the record containing locally-scoped external definitions.
1905 if (!LocallyScopedExternalDecls.empty())
Mike Stump11289f42009-09-09 15:08:12 +00001906 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00001907 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001908
1909 // Write the record containing ext_vector type names.
1910 if (!ExtVectorDecls.empty())
1911 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00001912
Douglas Gregor08f01292009-04-17 22:13:46 +00001913 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00001914 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00001915 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001916 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001917 Record.push_back(NumLexicalDeclContexts);
1918 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor08f01292009-04-17 22:13:46 +00001919 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00001920 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001921}
1922
1923void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1924 Record.push_back(Loc.getRawEncoding());
1925}
1926
1927void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1928 Record.push_back(Value.getBitWidth());
1929 unsigned N = Value.getNumWords();
1930 const uint64_t* Words = Value.getRawData();
1931 for (unsigned I = 0; I != N; ++I)
1932 Record.push_back(Words[I]);
1933}
1934
Douglas Gregor1daeb692009-04-13 18:14:40 +00001935void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1936 Record.push_back(Value.isUnsigned());
1937 AddAPInt(Value, Record);
1938}
1939
Douglas Gregore0a3a512009-04-14 21:55:33 +00001940void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1941 AddAPInt(Value.bitcastToAPInt(), Record);
1942}
1943
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001944void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001945 Record.push_back(getIdentifierRef(II));
1946}
1947
1948pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1949 if (II == 0)
1950 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001951
1952 pch::IdentID &ID = IdentifierIDs[II];
1953 if (ID == 0)
1954 ID = IdentifierIDs.size();
Douglas Gregor4621c6a2009-04-22 18:49:13 +00001955 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001956}
1957
Steve Naroff2ddea052009-04-23 10:39:46 +00001958void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1959 if (SelRef.getAsOpaquePtr() == 0) {
1960 Record.push_back(0);
1961 return;
1962 }
1963
1964 pch::SelectorID &SID = SelectorIDs[SelRef];
1965 if (SID == 0) {
1966 SID = SelectorIDs.size();
1967 SelVector.push_back(SelRef);
1968 }
1969 Record.push_back(SID);
1970}
1971
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001972void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1973 if (T.isNull()) {
1974 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1975 return;
1976 }
1977
1978 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001979 pch::TypeID ID = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001980 switch (BT->getKind()) {
1981 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1982 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1983 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1984 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1985 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1986 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1987 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1988 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00001989 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001990 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1991 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1992 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1993 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1994 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1995 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1996 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattnerf122cef2009-04-30 02:43:43 +00001997 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001998 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1999 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2000 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl576fd422009-05-10 18:38:11 +00002001 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002002 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2003 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002004 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2005 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff1329fa02009-07-15 18:40:39 +00002006 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2007 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson082acde2009-06-26 18:41:36 +00002008 case BuiltinType::UndeducedAuto:
2009 assert(0 && "Should not see undeduced auto here");
2010 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002011 }
2012
2013 Record.push_back((ID << 3) | T.getCVRQualifiers());
2014 return;
2015 }
2016
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002017 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor1970d882009-04-26 03:49:13 +00002018 if (ID == 0) {
2019 // We haven't seen this type before. Assign it a new ID and put it
2020 // into the queu of types to emit.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002021 ID = NextTypeID++;
Douglas Gregor1970d882009-04-26 03:49:13 +00002022 TypesToEmit.push(T.getTypePtr());
2023 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002024
2025 // Encode the type qualifiers in the type reference.
2026 Record.push_back((ID << 3) | T.getCVRQualifiers());
2027}
2028
2029void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2030 if (D == 0) {
2031 Record.push_back(0);
2032 return;
2033 }
2034
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002035 pch::DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002036 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002037 // We haven't seen this declaration before. Give it a new ID and
2038 // enqueue it in the list of declarations to emit.
2039 ID = DeclIDs.size();
2040 DeclsToEmit.push(const_cast<Decl *>(D));
2041 }
2042
2043 Record.push_back(ID);
2044}
2045
Douglas Gregore84a9da2009-04-20 20:36:09 +00002046pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2047 if (D == 0)
2048 return 0;
2049
2050 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2051 return DeclIDs[D];
2052}
2053
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002054void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002055 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002056 Record.push_back(Name.getNameKind());
2057 switch (Name.getNameKind()) {
2058 case DeclarationName::Identifier:
2059 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2060 break;
2061
2062 case DeclarationName::ObjCZeroArgSelector:
2063 case DeclarationName::ObjCOneArgSelector:
2064 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002065 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002066 break;
2067
2068 case DeclarationName::CXXConstructorName:
2069 case DeclarationName::CXXDestructorName:
2070 case DeclarationName::CXXConversionFunctionName:
2071 AddTypeRef(Name.getCXXNameType(), Record);
2072 break;
2073
2074 case DeclarationName::CXXOperatorName:
2075 Record.push_back(Name.getCXXOverloadedOperator());
2076 break;
2077
2078 case DeclarationName::CXXUsingDirective:
2079 // No extra data to emit
2080 break;
2081 }
2082}
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002083