blob: c7ea8da087ae7492429c67498dba63134738dff7 [file] [log] [blame]
Douglas Gregorc34897d2009-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 Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000024#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorb7064742009-04-27 22:23:34 +000030#include "clang/Basic/Version.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Douglas Gregoreccf0d12009-05-12 01:31:05 +000036#include "llvm/System/Path.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
Chris Lattnerd83ede52009-04-27 06:16:06 +000043
Douglas Gregorc34897d2009-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
53 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor6cc5d192009-04-27 18:38:38 +000054 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregorc34897d2009-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) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 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) {
110 Writer.AddTypeRef(T->getPointeeType(), Record);
111 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
112 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 Gregor1d381132009-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 Gregorc34897d2009-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 Gregor1d381132009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-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 Redl2767d882009-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 Gregorc34897d2009-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 Gregorc72f6c82009-04-16 22:23:12 +0000198 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-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 Carlsson93ab5332009-06-24 19:06:50 +0000207void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
208 Writer.AddStmt(T->getUnderlyingExpr());
209 Code = pch::TYPE_DECLTYPE;
210}
211
Douglas Gregorc34897d2009-04-09 22:27:44 +0000212void PCHTypeWriter::VisitTagType(const TagType *T) {
213 Writer.AddDeclRef(T->getDecl(), Record);
214 assert(!T->isBeingDefined() &&
215 "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
228void
229PCHTypeWriter::VisitTemplateSpecializationType(
230 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000231 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000232 assert(false && "Cannot serialize template specialization types");
233}
234
235void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000236 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000237 assert(false && "Cannot serialize qualified name types");
238}
239
240void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
241 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000242 Record.push_back(T->getNumProtocols());
Steve Naroff83418522009-05-27 16:21:00 +0000243 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
244 E = T->qual_end(); I != E; ++I)
245 Writer.AddDeclRef(*I, Record);
Steve Naroff77763c52009-07-18 15:33:26 +0000246 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000247}
248
Steve Naroffc75c1a82009-06-17 22:40:22 +0000249void
250PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Steve Naroff329ec222009-07-10 23:34:53 +0000251 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000252 Record.push_back(T->getNumProtocols());
Steve Naroffc75c1a82009-06-17 22:40:22 +0000253 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff83418522009-05-27 16:21:00 +0000254 E = T->qual_end(); I != E; ++I)
255 Writer.AddDeclRef(*I, Record);
Steve Naroffc75c1a82009-06-17 22:40:22 +0000256 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000257}
258
Chris Lattner80f83c62009-04-22 05:57:30 +0000259//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000260// PCHWriter Implementation
261//===----------------------------------------------------------------------===//
262
Chris Lattner920673a2009-04-26 22:26:21 +0000263static void EmitBlockID(unsigned ID, const char *Name,
264 llvm::BitstreamWriter &Stream,
265 PCHWriter::RecordData &Record) {
266 Record.clear();
267 Record.push_back(ID);
268 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
269
270 // Emit the block name if present.
271 if (Name == 0 || Name[0] == 0) return;
272 Record.clear();
273 while (*Name)
274 Record.push_back(*Name++);
275 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
276}
277
278static void EmitRecordID(unsigned ID, const char *Name,
279 llvm::BitstreamWriter &Stream,
280 PCHWriter::RecordData &Record) {
281 Record.clear();
282 Record.push_back(ID);
283 while (*Name)
284 Record.push_back(*Name++);
285 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000286}
287
288static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
289 PCHWriter::RecordData &Record) {
290#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
291 RECORD(STMT_STOP);
292 RECORD(STMT_NULL_PTR);
293 RECORD(STMT_NULL);
294 RECORD(STMT_COMPOUND);
295 RECORD(STMT_CASE);
296 RECORD(STMT_DEFAULT);
297 RECORD(STMT_LABEL);
298 RECORD(STMT_IF);
299 RECORD(STMT_SWITCH);
300 RECORD(STMT_WHILE);
301 RECORD(STMT_DO);
302 RECORD(STMT_FOR);
303 RECORD(STMT_GOTO);
304 RECORD(STMT_INDIRECT_GOTO);
305 RECORD(STMT_CONTINUE);
306 RECORD(STMT_BREAK);
307 RECORD(STMT_RETURN);
308 RECORD(STMT_DECL);
309 RECORD(STMT_ASM);
310 RECORD(EXPR_PREDEFINED);
311 RECORD(EXPR_DECL_REF);
312 RECORD(EXPR_INTEGER_LITERAL);
313 RECORD(EXPR_FLOATING_LITERAL);
314 RECORD(EXPR_IMAGINARY_LITERAL);
315 RECORD(EXPR_STRING_LITERAL);
316 RECORD(EXPR_CHARACTER_LITERAL);
317 RECORD(EXPR_PAREN);
318 RECORD(EXPR_UNARY_OPERATOR);
319 RECORD(EXPR_SIZEOF_ALIGN_OF);
320 RECORD(EXPR_ARRAY_SUBSCRIPT);
321 RECORD(EXPR_CALL);
322 RECORD(EXPR_MEMBER);
323 RECORD(EXPR_BINARY_OPERATOR);
324 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
325 RECORD(EXPR_CONDITIONAL_OPERATOR);
326 RECORD(EXPR_IMPLICIT_CAST);
327 RECORD(EXPR_CSTYLE_CAST);
328 RECORD(EXPR_COMPOUND_LITERAL);
329 RECORD(EXPR_EXT_VECTOR_ELEMENT);
330 RECORD(EXPR_INIT_LIST);
331 RECORD(EXPR_DESIGNATED_INIT);
332 RECORD(EXPR_IMPLICIT_VALUE_INIT);
333 RECORD(EXPR_VA_ARG);
334 RECORD(EXPR_ADDR_LABEL);
335 RECORD(EXPR_STMT);
336 RECORD(EXPR_TYPES_COMPATIBLE);
337 RECORD(EXPR_CHOOSE);
338 RECORD(EXPR_GNU_NULL);
339 RECORD(EXPR_SHUFFLE_VECTOR);
340 RECORD(EXPR_BLOCK);
341 RECORD(EXPR_BLOCK_DECL_REF);
342 RECORD(EXPR_OBJC_STRING_LITERAL);
343 RECORD(EXPR_OBJC_ENCODE);
344 RECORD(EXPR_OBJC_SELECTOR_EXPR);
345 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
346 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
347 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
348 RECORD(EXPR_OBJC_KVC_REF_EXPR);
349 RECORD(EXPR_OBJC_MESSAGE_EXPR);
350 RECORD(EXPR_OBJC_SUPER_EXPR);
351 RECORD(STMT_OBJC_FOR_COLLECTION);
352 RECORD(STMT_OBJC_CATCH);
353 RECORD(STMT_OBJC_FINALLY);
354 RECORD(STMT_OBJC_AT_TRY);
355 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
356 RECORD(STMT_OBJC_AT_THROW);
357#undef RECORD
Chris Lattner920673a2009-04-26 22:26:21 +0000358}
359
360void PCHWriter::WriteBlockInfoBlock() {
361 RecordData Record;
362 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
363
Chris Lattner880f3f72009-04-27 00:40:25 +0000364#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner920673a2009-04-26 22:26:21 +0000365#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
366
367 // PCH Top-Level Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000368 BLOCK(PCH_BLOCK);
Zhongxing Xu29b61a52009-06-03 09:23:28 +0000369 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner920673a2009-04-26 22:26:21 +0000370 RECORD(TYPE_OFFSET);
371 RECORD(DECL_OFFSET);
372 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorb7064742009-04-27 22:23:34 +0000373 RECORD(METADATA);
Chris Lattner920673a2009-04-26 22:26:21 +0000374 RECORD(IDENTIFIER_OFFSET);
375 RECORD(IDENTIFIER_TABLE);
376 RECORD(EXTERNAL_DEFINITIONS);
377 RECORD(SPECIAL_TYPES);
378 RECORD(STATISTICS);
379 RECORD(TENTATIVE_DEFINITIONS);
380 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
381 RECORD(SELECTOR_OFFSETS);
382 RECORD(METHOD_POOL);
383 RECORD(PP_COUNTER_VALUE);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000384 RECORD(SOURCE_LOCATION_OFFSETS);
385 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000386 RECORD(STAT_CACHE);
Douglas Gregorb36b20d2009-04-27 20:06:05 +0000387 RECORD(EXT_VECTOR_DECLS);
Douglas Gregora252b232009-07-02 17:08:52 +0000388 RECORD(COMMENT_RANGES);
389
Chris Lattner920673a2009-04-26 22:26:21 +0000390 // SourceManager Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000391 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000392 RECORD(SM_SLOC_FILE_ENTRY);
393 RECORD(SM_SLOC_BUFFER_ENTRY);
394 RECORD(SM_SLOC_BUFFER_BLOB);
395 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
396 RECORD(SM_LINE_TABLE);
397 RECORD(SM_HEADER_FILE_INFO);
398
399 // Preprocessor Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000400 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000401 RECORD(PP_MACRO_OBJECT_LIKE);
402 RECORD(PP_MACRO_FUNCTION_LIKE);
403 RECORD(PP_TOKEN);
404
405 // Types block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000406 BLOCK(TYPES_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000407 RECORD(TYPE_EXT_QUAL);
408 RECORD(TYPE_FIXED_WIDTH_INT);
409 RECORD(TYPE_COMPLEX);
410 RECORD(TYPE_POINTER);
411 RECORD(TYPE_BLOCK_POINTER);
412 RECORD(TYPE_LVALUE_REFERENCE);
413 RECORD(TYPE_RVALUE_REFERENCE);
414 RECORD(TYPE_MEMBER_POINTER);
415 RECORD(TYPE_CONSTANT_ARRAY);
416 RECORD(TYPE_INCOMPLETE_ARRAY);
417 RECORD(TYPE_VARIABLE_ARRAY);
418 RECORD(TYPE_VECTOR);
419 RECORD(TYPE_EXT_VECTOR);
420 RECORD(TYPE_FUNCTION_PROTO);
421 RECORD(TYPE_FUNCTION_NO_PROTO);
422 RECORD(TYPE_TYPEDEF);
423 RECORD(TYPE_TYPEOF_EXPR);
424 RECORD(TYPE_TYPEOF);
425 RECORD(TYPE_RECORD);
426 RECORD(TYPE_ENUM);
427 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffc75c1a82009-06-17 22:40:22 +0000428 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000429 // Statements and Exprs can occur in the Types block.
430 AddStmtsExprs(Stream, Record);
431
Chris Lattner920673a2009-04-26 22:26:21 +0000432 // Decls block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000433 BLOCK(DECLS_BLOCK);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000434 RECORD(DECL_ATTR);
435 RECORD(DECL_TRANSLATION_UNIT);
436 RECORD(DECL_TYPEDEF);
437 RECORD(DECL_ENUM);
438 RECORD(DECL_RECORD);
439 RECORD(DECL_ENUM_CONSTANT);
440 RECORD(DECL_FUNCTION);
441 RECORD(DECL_OBJC_METHOD);
442 RECORD(DECL_OBJC_INTERFACE);
443 RECORD(DECL_OBJC_PROTOCOL);
444 RECORD(DECL_OBJC_IVAR);
445 RECORD(DECL_OBJC_AT_DEFS_FIELD);
446 RECORD(DECL_OBJC_CLASS);
447 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
448 RECORD(DECL_OBJC_CATEGORY);
449 RECORD(DECL_OBJC_CATEGORY_IMPL);
450 RECORD(DECL_OBJC_IMPLEMENTATION);
451 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
452 RECORD(DECL_OBJC_PROPERTY);
453 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +0000454 RECORD(DECL_FIELD);
455 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000456 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +0000457 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000458 RECORD(DECL_ORIGINAL_PARM_VAR);
459 RECORD(DECL_FILE_SCOPE_ASM);
460 RECORD(DECL_BLOCK);
461 RECORD(DECL_CONTEXT_LEXICAL);
462 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000463 // Statements and Exprs can occur in the Decls block.
464 AddStmtsExprs(Stream, Record);
Chris Lattner920673a2009-04-26 22:26:21 +0000465#undef RECORD
466#undef BLOCK
467 Stream.ExitBlock();
468}
469
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000470/// \brief Adjusts the given filename to only write out the portion of the
471/// filename that is not part of the system root directory.
472///
473/// \param Filename the file name to adjust.
474///
475/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
476/// the returned filename will be adjusted by this system root.
477///
478/// \returns either the original filename (if it needs no adjustment) or the
479/// adjusted filename (which points into the @p Filename parameter).
480static const char *
481adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
482 assert(Filename && "No file name to adjust?");
483
484 if (!isysroot)
485 return Filename;
486
487 // Verify that the filename and the system root have the same prefix.
488 unsigned Pos = 0;
489 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
490 if (Filename[Pos] != isysroot[Pos])
491 return Filename; // Prefixes don't match.
492
493 // We hit the end of the filename before we hit the end of the system root.
494 if (!Filename[Pos])
495 return Filename;
496
497 // If the file name has a '/' at the current position, skip over the '/'.
498 // We distinguish sysroot-based includes from absolute includes by the
499 // absence of '/' at the beginning of sysroot-based includes.
500 if (Filename[Pos] == '/')
501 ++Pos;
502
503 return Filename + Pos;
504}
Chris Lattner920673a2009-04-26 22:26:21 +0000505
Douglas Gregorb7064742009-04-27 22:23:34 +0000506/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000507void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorb5887f32009-04-10 21:16:55 +0000508 using namespace llvm;
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000509
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000510 // Metadata
511 const TargetInfo &Target = Context.Target;
512 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
513 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
514 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
515 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
516 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
517 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
518 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
519 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
520 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
521
522 RecordData Record;
523 Record.push_back(pch::METADATA);
524 Record.push_back(pch::VERSION_MAJOR);
525 Record.push_back(pch::VERSION_MINOR);
526 Record.push_back(CLANG_VERSION_MAJOR);
527 Record.push_back(CLANG_VERSION_MINOR);
528 Record.push_back(isysroot != 0);
Daniel Dunbar608b3882009-08-24 09:10:05 +0000529 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000530 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000531
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000532 // Original file name
533 SourceManager &SM = Context.getSourceManager();
534 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
535 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
536 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
537 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
538 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
539
540 llvm::sys::Path MainFilePath(MainFile->getName());
541 std::string MainFileName;
542
543 if (!MainFilePath.isAbsolute()) {
544 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner31bc3042009-08-23 22:45:33 +0000545 P.appendComponent(MainFilePath.str());
546 MainFileName = P.str();
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000547 } else {
Chris Lattner31bc3042009-08-23 22:45:33 +0000548 MainFileName = MainFilePath.str();
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000549 }
550
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000551 const char *MainFileNameStr = MainFileName.c_str();
552 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
553 isysroot);
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000554 RecordData Record;
555 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000556 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000557 }
Douglas Gregorb5887f32009-04-10 21:16:55 +0000558}
559
560/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000561void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
562 RecordData Record;
563 Record.push_back(LangOpts.Trigraphs);
564 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
565 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
566 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
567 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
568 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
569 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
570 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
571 Record.push_back(LangOpts.C99); // C99 Support
572 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
573 Record.push_back(LangOpts.CPlusPlus); // C++ Support
574 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor179cfb12009-04-10 20:39:37 +0000575 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
576
577 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
578 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
579 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
580
581 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor179cfb12009-04-10 20:39:37 +0000582 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
583 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begeman909e06e2009-06-25 23:01:11 +0000584 Record.push_back(LangOpts.AltiVec);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000585 Record.push_back(LangOpts.Exceptions); // Support exception handling.
586
587 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
588 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
589 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
590
Chris Lattnercd4da472009-04-27 07:35:58 +0000591 // Whether static initializers are protected by locks.
592 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000593 Record.push_back(LangOpts.Blocks); // block extension to C
594 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
595 // they are unused.
596 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
597 // (modulo the platform support).
598
599 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
600 // signed integer arithmetic overflows.
601
602 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
603 // may be ripped out at any time.
604
605 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
606 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
607 // defined.
608 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
609 // opposed to __DYNAMIC__).
610 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
611
612 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
613 // used (instead of C99 semantics).
614 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssonf2310142009-05-13 19:49:53 +0000615 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
616 // be enabled.
Eli Friedmand9389be2009-06-05 07:05:05 +0000617 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
618 // unsigned type
Douglas Gregor179cfb12009-04-10 20:39:37 +0000619 Record.push_back(LangOpts.getGCMode());
620 Record.push_back(LangOpts.getVisibilityMode());
621 Record.push_back(LangOpts.InstantiationDepth);
Nate Begeman909e06e2009-06-25 23:01:11 +0000622 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9a0c2a52009-08-22 22:30:33 +0000623 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000624 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000625}
626
Douglas Gregorab1cef72009-04-10 03:52:48 +0000627//===----------------------------------------------------------------------===//
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000628// stat cache Serialization
629//===----------------------------------------------------------------------===//
630
631namespace {
632// Trait used for the on-disk hash table of stat cache results.
633class VISIBILITY_HIDDEN PCHStatCacheTrait {
634public:
635 typedef const char * key_type;
636 typedef key_type key_type_ref;
637
638 typedef std::pair<int, struct stat> data_type;
639 typedef const data_type& data_type_ref;
640
641 static unsigned ComputeHash(const char *path) {
642 return BernsteinHash(path);
643 }
644
645 std::pair<unsigned,unsigned>
646 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
647 data_type_ref Data) {
648 unsigned StrLen = strlen(path);
649 clang::io::Emit16(Out, StrLen);
650 unsigned DataLen = 1; // result value
651 if (Data.first == 0)
652 DataLen += 4 + 4 + 2 + 8 + 8;
653 clang::io::Emit8(Out, DataLen);
654 return std::make_pair(StrLen + 1, DataLen);
655 }
656
657 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
658 Out.write(path, KeyLen);
659 }
660
661 void EmitData(llvm::raw_ostream& Out, key_type_ref,
662 data_type_ref Data, unsigned DataLen) {
663 using namespace clang::io;
664 uint64_t Start = Out.tell(); (void)Start;
665
666 // Result of stat()
667 Emit8(Out, Data.first? 1 : 0);
668
669 if (Data.first == 0) {
670 Emit32(Out, (uint32_t) Data.second.st_ino);
671 Emit32(Out, (uint32_t) Data.second.st_dev);
672 Emit16(Out, (uint16_t) Data.second.st_mode);
673 Emit64(Out, (uint64_t) Data.second.st_mtime);
674 Emit64(Out, (uint64_t) Data.second.st_size);
675 }
676
677 assert(Out.tell() - Start == DataLen && "Wrong data length");
678 }
679};
680} // end anonymous namespace
681
682/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000683void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
684 const char *isysroot) {
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000685 // Build the on-disk hash table containing information about every
686 // stat() call.
687 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
688 unsigned NumStatEntries = 0;
689 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
690 StatEnd = StatCalls.end();
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000691 Stat != StatEnd; ++Stat, ++NumStatEntries) {
692 const char *Filename = Stat->first();
693 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
694 Generator.insert(Filename, Stat->second);
695 }
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000696
697 // Create the on-disk hash table in a buffer.
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000698 llvm::SmallString<4096> StatCacheData;
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000699 uint32_t BucketOffset;
700 {
701 llvm::raw_svector_ostream Out(StatCacheData);
702 // Make sure that no bucket is at offset 0
703 clang::io::Emit32(Out, 0);
704 BucketOffset = Generator.Emit(Out);
705 }
706
707 // Create a blob abbreviation
708 using namespace llvm;
709 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
710 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
714 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
715
716 // Write the stat cache
717 RecordData Record;
718 Record.push_back(pch::STAT_CACHE);
719 Record.push_back(BucketOffset);
720 Record.push_back(NumStatEntries);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000721 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000722}
723
724//===----------------------------------------------------------------------===//
Douglas Gregorab1cef72009-04-10 03:52:48 +0000725// Source Manager Serialization
726//===----------------------------------------------------------------------===//
727
728/// \brief Create an abbreviation for the SLocEntry that refers to a
729/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000730static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000731 using namespace llvm;
732 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
733 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000739 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000740}
741
742/// \brief Create an abbreviation for the SLocEntry that refers to a
743/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000744static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000745 using namespace llvm;
746 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
747 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000753 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000754}
755
756/// \brief Create an abbreviation for the SLocEntry that refers to a
757/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000758static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000759 using namespace llvm;
760 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
761 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000763 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000764}
765
766/// \brief Create an abbreviation for the SLocEntry that refers to an
767/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000768static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000769 using namespace llvm;
770 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
771 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000777 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000778}
779
780/// \brief Writes the block containing the serialized form of the
781/// source manager.
782///
783/// TODO: We should probably use an on-disk hash table (stored in a
784/// blob), indexed based on the file name, so that we only create
785/// entries for files that we actually need. In the common case (no
786/// errors), we probably won't have to create file entries for any of
787/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000788void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000789 const Preprocessor &PP,
790 const char *isysroot) {
Douglas Gregor32e231c2009-04-27 06:38:32 +0000791 RecordData Record;
792
Chris Lattner84b04f12009-04-10 17:16:57 +0000793 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000794 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000795
796 // Abbreviations for the various kinds of source-location entries.
Chris Lattnereb559a62009-04-27 19:03:22 +0000797 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
798 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
799 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
800 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000801
Douglas Gregor635f97f2009-04-13 16:31:14 +0000802 // Write the line table.
803 if (SourceMgr.hasLineTable()) {
804 LineTableInfo &LineTable = SourceMgr.getLineTable();
805
806 // Emit the file names
807 Record.push_back(LineTable.getNumFilenames());
808 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
809 // Emit the file name
810 const char *Filename = LineTable.getFilename(I);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000811 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000812 unsigned FilenameLen = Filename? strlen(Filename) : 0;
813 Record.push_back(FilenameLen);
814 if (FilenameLen)
815 Record.insert(Record.end(), Filename, Filename + FilenameLen);
816 }
817
818 // Emit the line entries
819 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
820 L != LEnd; ++L) {
821 // Emit the file ID
822 Record.push_back(L->first);
823
824 // Emit the line entries
825 Record.push_back(L->second.size());
826 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
827 LEEnd = L->second.end();
828 LE != LEEnd; ++LE) {
829 Record.push_back(LE->FileOffset);
830 Record.push_back(LE->LineNo);
831 Record.push_back(LE->FilenameID);
832 Record.push_back((unsigned)LE->FileKind);
833 Record.push_back(LE->IncludeOffset);
834 }
Douglas Gregor635f97f2009-04-13 16:31:14 +0000835 }
Zhongxing Xu01838482009-05-22 08:38:27 +0000836 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000837 }
838
Douglas Gregor32e231c2009-04-27 06:38:32 +0000839 // Write out entries for all of the header files we know about.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000840 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000841 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000842 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
843 E = HS.header_file_end();
844 I != E; ++I) {
845 Record.push_back(I->isImport);
846 Record.push_back(I->DirInfo);
847 Record.push_back(I->NumIncludes);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000848 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000849 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
850 Record.clear();
851 }
852
Douglas Gregor32e231c2009-04-27 06:38:32 +0000853 // Write out the source location entry table. We skip the first
854 // entry, which is always the same dummy entry.
Chris Lattner93307da2009-04-27 19:01:47 +0000855 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor32e231c2009-04-27 06:38:32 +0000856 RecordData PreloadSLocs;
857 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
858 for (SourceManager::sloc_entry_iterator
859 SLoc = SourceMgr.sloc_entry_begin() + 1,
860 SLocEnd = SourceMgr.sloc_entry_end();
861 SLoc != SLocEnd; ++SLoc) {
862 // Record the offset of this source-location entry.
863 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
864
865 // Figure out which record code to use.
866 unsigned Code;
867 if (SLoc->isFile()) {
868 if (SLoc->getFile().getContentCache()->Entry)
869 Code = pch::SM_SLOC_FILE_ENTRY;
870 else
871 Code = pch::SM_SLOC_BUFFER_ENTRY;
872 } else
873 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
874 Record.clear();
875 Record.push_back(Code);
876
877 Record.push_back(SLoc->getOffset());
878 if (SLoc->isFile()) {
879 const SrcMgr::FileInfo &File = SLoc->getFile();
880 Record.push_back(File.getIncludeLoc().getRawEncoding());
881 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
882 Record.push_back(File.hasLineDirectives());
883
884 const SrcMgr::ContentCache *Content = File.getContentCache();
885 if (Content->Entry) {
886 // The source location entry is a file. The blob associated
887 // with this entry is the file name.
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000888
889 // Turn the file name into an absolute path, if it isn't already.
890 const char *Filename = Content->Entry->getName();
891 llvm::sys::Path FilePath(Filename, strlen(Filename));
892 std::string FilenameStr;
893 if (!FilePath.isAbsolute()) {
894 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner31bc3042009-08-23 22:45:33 +0000895 P.appendComponent(FilePath.str());
896 FilenameStr = P.str();
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000897 Filename = FilenameStr.c_str();
898 }
899
900 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000901 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000902
903 // FIXME: For now, preload all file source locations, so that
904 // we get the appropriate File entries in the reader. This is
905 // a temporary measure.
906 PreloadSLocs.push_back(SLocEntryOffsets.size());
907 } else {
908 // The source location entry is a buffer. The blob associated
909 // with this entry contains the contents of the buffer.
910
911 // We add one to the size so that we capture the trailing NULL
912 // that is required by llvm::MemoryBuffer::getMemBuffer (on
913 // the reader side).
914 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
915 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000916 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
917 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000918 Record.clear();
919 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
920 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000921 llvm::StringRef(Buffer->getBufferStart(),
922 Buffer->getBufferSize() + 1));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000923
924 if (strcmp(Name, "<built-in>") == 0)
925 PreloadSLocs.push_back(SLocEntryOffsets.size());
926 }
927 } else {
928 // The source location entry is an instantiation.
929 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
930 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
931 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
932 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
933
934 // Compute the token length for this macro expansion.
935 unsigned NextOffset = SourceMgr.getNextOffset();
936 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
937 if (++NextSLoc != SLocEnd)
938 NextOffset = NextSLoc->getOffset();
939 Record.push_back(NextOffset - SLoc->getOffset() - 1);
940 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
941 }
942 }
943
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000944 Stream.ExitBlock();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000945
946 if (SLocEntryOffsets.empty())
947 return;
948
949 // Write the source-location offsets table into the PCH block. This
950 // table is used for lazily loading source-location information.
951 using namespace llvm;
952 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
953 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
957 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
958
959 Record.clear();
960 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
961 Record.push_back(SLocEntryOffsets.size());
962 Record.push_back(SourceMgr.getNextOffset());
963 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
964 (const char *)&SLocEntryOffsets.front(),
Chris Lattner93307da2009-04-27 19:01:47 +0000965 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000966
967 // Write the source location entry preloads array, telling the PCH
968 // reader which source locations entries it should load eagerly.
969 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000970}
971
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000972//===----------------------------------------------------------------------===//
973// Preprocessor Serialization
974//===----------------------------------------------------------------------===//
975
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000976/// \brief Writes the block containing the serialized form of the
977/// preprocessor.
978///
Chris Lattner850eabd2009-04-10 18:08:30 +0000979void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +0000980 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000981
Chris Lattner4b21c202009-04-13 01:29:17 +0000982 // If the preprocessor __COUNTER__ value has been bumped, remember it.
983 if (PP.getCounterValue() != 0) {
984 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000985 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +0000986 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000987 }
988
989 // Enter the preprocessor block.
990 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +0000991
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000992 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
993 // FIXME: use diagnostics subsystem for localization etc.
994 if (PP.SawDateOrTime())
995 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
996
Chris Lattner1b094952009-04-10 18:00:12 +0000997 // Loop over all the macro definitions that are live at the end of the file,
998 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +0000999 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1000 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001001 // FIXME: This emits macros in hash table order, we should do it in a stable
1002 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001003 MacroInfo *MI = I->second;
1004
1005 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1006 // been redefined by the header (in which case they are not isBuiltinMacro).
1007 if (MI->isBuiltinMacro())
1008 continue;
1009
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001010 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001011 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001012 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001013 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1014 Record.push_back(MI->isUsed());
1015
1016 unsigned Code;
1017 if (MI->isObjectLike()) {
1018 Code = pch::PP_MACRO_OBJECT_LIKE;
1019 } else {
1020 Code = pch::PP_MACRO_FUNCTION_LIKE;
1021
1022 Record.push_back(MI->isC99Varargs());
1023 Record.push_back(MI->isGNUVarargs());
1024 Record.push_back(MI->getNumArgs());
1025 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1026 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001027 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001028 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001029 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001030 Record.clear();
1031
Chris Lattner850eabd2009-04-10 18:08:30 +00001032 // Emit the tokens array.
1033 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1034 // Note that we know that the preprocessor does not have any annotation
1035 // tokens in it because they are created by the parser, and thus can't be
1036 // in a macro definition.
1037 const Token &Tok = MI->getReplacementToken(TokNo);
1038
1039 Record.push_back(Tok.getLocation().getRawEncoding());
1040 Record.push_back(Tok.getLength());
1041
Chris Lattner850eabd2009-04-10 18:08:30 +00001042 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1043 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001044 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001045
1046 // FIXME: Should translate token kind to a stable encoding.
1047 Record.push_back(Tok.getKind());
1048 // FIXME: Should translate token flags to a stable encoding.
1049 Record.push_back(Tok.getFlags());
1050
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001051 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001052 Record.clear();
1053 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001054 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001055 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001056 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001057}
1058
Douglas Gregora252b232009-07-02 17:08:52 +00001059void PCHWriter::WriteComments(ASTContext &Context) {
1060 using namespace llvm;
1061
1062 if (Context.Comments.empty())
1063 return;
1064
1065 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1066 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1067 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1068 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
1069
1070 RecordData Record;
1071 Record.push_back(pch::COMMENT_RANGES);
1072 Stream.EmitRecordWithBlob(CommentCode, Record,
1073 (const char*)&Context.Comments[0],
1074 Context.Comments.size() * sizeof(SourceRange));
1075}
1076
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001077//===----------------------------------------------------------------------===//
1078// Type Serialization
1079//===----------------------------------------------------------------------===//
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001080
Douglas Gregorc34897d2009-04-09 22:27:44 +00001081/// \brief Write the representation of a type to the PCH stream.
1082void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001083 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001084 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001085 ID = NextTypeID++;
1086
1087 // Record the offset for this type.
1088 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001089 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001090 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1091 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001092 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001093 }
1094
1095 RecordData Record;
1096
1097 // Emit the type's representation.
1098 PCHTypeWriter W(*this, Record);
1099 switch (T->getTypeClass()) {
1100 // For all of the concrete, non-dependent types, call the
1101 // appropriate visitor function.
1102#define TYPE(Class, Base) \
1103 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1104#define ABSTRACT_TYPE(Class, Base)
1105#define DEPENDENT_TYPE(Class, Base)
1106#include "clang/AST/TypeNodes.def"
1107
1108 // For all of the dependent type nodes (which only occur in C++
1109 // templates), produce an error.
1110#define TYPE(Class, Base)
1111#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1112#include "clang/AST/TypeNodes.def"
1113 assert(false && "Cannot serialize dependent type nodes");
1114 break;
1115 }
1116
1117 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001118 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001119
1120 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001121 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001122}
1123
1124/// \brief Write a block containing all of the types.
1125void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001126 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001127 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001128
Douglas Gregore43f0972009-04-26 03:49:13 +00001129 // Emit all of the types that need to be emitted (so far).
1130 while (!TypesToEmit.empty()) {
1131 const Type *T = TypesToEmit.front();
1132 TypesToEmit.pop();
1133 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1134 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001135 }
1136
1137 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001138 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001139}
1140
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001141//===----------------------------------------------------------------------===//
1142// Declaration Serialization
1143//===----------------------------------------------------------------------===//
1144
Douglas Gregorc34897d2009-04-09 22:27:44 +00001145/// \brief Write the block containing all of the declaration IDs
1146/// lexically declared within the given DeclContext.
1147///
1148/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1149/// bistream, or 0 if no block was written.
1150uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1151 DeclContext *DC) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001152 if (DC->decls_empty())
Douglas Gregorc34897d2009-04-09 22:27:44 +00001153 return 0;
1154
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001155 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001156 RecordData Record;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001157 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1158 D != DEnd; ++D)
Douglas Gregorc34897d2009-04-09 22:27:44 +00001159 AddDeclRef(*D, Record);
1160
Douglas Gregoraf136d92009-04-22 22:34:57 +00001161 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001162 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001163 return Offset;
1164}
1165
1166/// \brief Write the block containing all of the declaration IDs
1167/// visible from the given DeclContext.
1168///
1169/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1170/// bistream, or 0 if no block was written.
1171uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1172 DeclContext *DC) {
1173 if (DC->getPrimaryContext() != DC)
1174 return 0;
1175
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001176 // Since there is no name lookup into functions or methods, and we
1177 // perform name lookup for the translation unit via the
1178 // IdentifierInfo chains, don't bother to build a
1179 // visible-declarations table for these entities.
1180 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001181 return 0;
1182
Douglas Gregorc34897d2009-04-09 22:27:44 +00001183 // Force the DeclContext to build a its name-lookup table.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001184 DC->lookup(DeclarationName());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001185
1186 // Serialize the contents of the mapping used for lookup. Note that,
1187 // although we have two very different code paths, the serialized
1188 // representation is the same for both cases: a declaration name,
1189 // followed by a size, followed by references to the visible
1190 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001191 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001192 RecordData Record;
1193 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001194 if (!Map)
1195 return 0;
1196
Douglas Gregorc34897d2009-04-09 22:27:44 +00001197 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1198 D != DEnd; ++D) {
1199 AddDeclarationName(D->first, Record);
1200 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1201 Record.push_back(Result.second - Result.first);
1202 for(; Result.first != Result.second; ++Result.first)
1203 AddDeclRef(*Result.first, Record);
1204 }
1205
1206 if (Record.size() == 0)
1207 return 0;
1208
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001209 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001210 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001211 return Offset;
1212}
1213
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001214//===----------------------------------------------------------------------===//
1215// Global Method Pool and Selector Serialization
1216//===----------------------------------------------------------------------===//
1217
Douglas Gregorff9a6092009-04-20 20:36:09 +00001218namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001219// Trait used for the on-disk hash table used in the method pool.
1220class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1221 PCHWriter &Writer;
1222
1223public:
1224 typedef Selector key_type;
1225 typedef key_type key_type_ref;
1226
1227 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1228 typedef const data_type& data_type_ref;
1229
1230 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1231
1232 static unsigned ComputeHash(Selector Sel) {
1233 unsigned N = Sel.getNumArgs();
1234 if (N == 0)
1235 ++N;
1236 unsigned R = 5381;
1237 for (unsigned I = 0; I != N; ++I)
1238 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1239 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1240 return R;
1241 }
1242
1243 std::pair<unsigned,unsigned>
1244 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1245 data_type_ref Methods) {
1246 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1247 clang::io::Emit16(Out, KeyLen);
1248 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1249 for (const ObjCMethodList *Method = &Methods.first; Method;
1250 Method = Method->Next)
1251 if (Method->Method)
1252 DataLen += 4;
1253 for (const ObjCMethodList *Method = &Methods.second; Method;
1254 Method = Method->Next)
1255 if (Method->Method)
1256 DataLen += 4;
1257 clang::io::Emit16(Out, DataLen);
1258 return std::make_pair(KeyLen, DataLen);
1259 }
1260
Douglas Gregor2d711832009-04-25 17:48:32 +00001261 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1262 uint64_t Start = Out.tell();
1263 assert((Start >> 32) == 0 && "Selector key offset too large");
1264 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001265 unsigned N = Sel.getNumArgs();
1266 clang::io::Emit16(Out, N);
1267 if (N == 0)
1268 N = 1;
1269 for (unsigned I = 0; I != N; ++I)
1270 clang::io::Emit32(Out,
1271 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1272 }
1273
1274 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001275 data_type_ref Methods, unsigned DataLen) {
1276 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001277 unsigned NumInstanceMethods = 0;
1278 for (const ObjCMethodList *Method = &Methods.first; Method;
1279 Method = Method->Next)
1280 if (Method->Method)
1281 ++NumInstanceMethods;
1282
1283 unsigned NumFactoryMethods = 0;
1284 for (const ObjCMethodList *Method = &Methods.second; Method;
1285 Method = Method->Next)
1286 if (Method->Method)
1287 ++NumFactoryMethods;
1288
1289 clang::io::Emit16(Out, NumInstanceMethods);
1290 clang::io::Emit16(Out, NumFactoryMethods);
1291 for (const ObjCMethodList *Method = &Methods.first; Method;
1292 Method = Method->Next)
1293 if (Method->Method)
1294 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001295 for (const ObjCMethodList *Method = &Methods.second; Method;
1296 Method = Method->Next)
1297 if (Method->Method)
1298 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001299
1300 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001301 }
1302};
1303} // end anonymous namespace
1304
1305/// \brief Write the method pool into the PCH file.
1306///
1307/// The method pool contains both instance and factory methods, stored
1308/// in an on-disk hash table indexed by the selector.
1309void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1310 using namespace llvm;
1311
1312 // Create and write out the blob that contains the instance and
1313 // factor method pools.
1314 bool Empty = true;
1315 {
1316 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1317
1318 // Create the on-disk hash table representation. Start by
1319 // iterating through the instance method pool.
1320 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001321 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001322 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1323 Instance = SemaRef.InstanceMethodPool.begin(),
1324 InstanceEnd = SemaRef.InstanceMethodPool.end();
1325 Instance != InstanceEnd; ++Instance) {
1326 // Check whether there is a factory method with the same
1327 // selector.
1328 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1329 = SemaRef.FactoryMethodPool.find(Instance->first);
1330
1331 if (Factory == SemaRef.FactoryMethodPool.end())
1332 Generator.insert(Instance->first,
1333 std::make_pair(Instance->second,
1334 ObjCMethodList()));
1335 else
1336 Generator.insert(Instance->first,
1337 std::make_pair(Instance->second, Factory->second));
1338
Douglas Gregor2d711832009-04-25 17:48:32 +00001339 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001340 Empty = false;
1341 }
1342
1343 // Now iterate through the factory method pool, to pick up any
1344 // selectors that weren't already in the instance method pool.
1345 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1346 Factory = SemaRef.FactoryMethodPool.begin(),
1347 FactoryEnd = SemaRef.FactoryMethodPool.end();
1348 Factory != FactoryEnd; ++Factory) {
1349 // Check whether there is an instance method with the same
1350 // selector. If so, there is no work to do here.
1351 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1352 = SemaRef.InstanceMethodPool.find(Factory->first);
1353
Douglas Gregor2d711832009-04-25 17:48:32 +00001354 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001355 Generator.insert(Factory->first,
1356 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001357 ++NumSelectorsInMethodPool;
1358 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001359
1360 Empty = false;
1361 }
1362
Douglas Gregor2d711832009-04-25 17:48:32 +00001363 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001364 return;
1365
1366 // Create the on-disk hash table in a buffer.
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001367 llvm::SmallString<4096> MethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001368 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001369 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001370 {
1371 PCHMethodPoolTrait Trait(*this);
1372 llvm::raw_svector_ostream Out(MethodPool);
1373 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001374 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001375 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001376
1377 // For every selector that we have seen but which was not
1378 // written into the hash table, write the selector itself and
1379 // record it's offset.
1380 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1381 if (SelectorOffsets[I] == 0)
1382 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001383 }
1384
1385 // Create a blob abbreviation
1386 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1387 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1391 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1392
Douglas Gregor2d711832009-04-25 17:48:32 +00001393 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001394 RecordData Record;
1395 Record.push_back(pch::METHOD_POOL);
1396 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001397 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001398 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor2d711832009-04-25 17:48:32 +00001399
1400 // Create a blob abbreviation for the selector table offsets.
1401 Abbrev = new BitCodeAbbrev();
1402 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1405 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1406
1407 // Write the selector offsets table.
1408 Record.clear();
1409 Record.push_back(pch::SELECTOR_OFFSETS);
1410 Record.push_back(SelectorOffsets.size());
1411 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1412 (const char *)&SelectorOffsets.front(),
1413 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001414 }
1415}
1416
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001417//===----------------------------------------------------------------------===//
1418// Identifier Table Serialization
1419//===----------------------------------------------------------------------===//
1420
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001421namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001422class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1423 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001424 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001425
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001426 /// \brief Determines whether this is an "interesting" identifier
1427 /// that needs a full IdentifierInfo structure written into the hash
1428 /// table.
1429 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1430 return II->isPoisoned() ||
1431 II->isExtensionToken() ||
1432 II->hasMacroDefinition() ||
1433 II->getObjCOrBuiltinID() ||
1434 II->getFETokenInfo<void>();
1435 }
1436
Douglas Gregorff9a6092009-04-20 20:36:09 +00001437public:
1438 typedef const IdentifierInfo* key_type;
1439 typedef key_type key_type_ref;
1440
1441 typedef pch::IdentID data_type;
1442 typedef data_type data_type_ref;
1443
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001444 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1445 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001446
1447 static unsigned ComputeHash(const IdentifierInfo* II) {
1448 return clang::BernsteinHash(II->getName());
1449 }
1450
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001451 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001452 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1453 pch::IdentID ID) {
1454 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001455 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1456 if (isInterestingIdentifier(II)) {
Douglas Gregor67d91172009-04-28 21:32:13 +00001457 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001458 if (II->hasMacroDefinition() &&
1459 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor67d91172009-04-28 21:32:13 +00001460 DataLen += 4;
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001461 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1462 DEnd = IdentifierResolver::end();
1463 D != DEnd; ++D)
1464 DataLen += sizeof(pch::DeclID);
1465 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001466 clang::io::Emit16(Out, DataLen);
Douglas Gregor68619772009-04-28 20:01:51 +00001467 // We emit the key length after the data length so that every
1468 // string is preceded by a 16-bit length. This matches the PTH
1469 // format for storing identifiers.
Douglas Gregor85c4a872009-04-25 21:04:17 +00001470 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001471 return std::make_pair(KeyLen, DataLen);
1472 }
1473
1474 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1475 unsigned KeyLen) {
1476 // Record the location of the key data. This is used when generating
1477 // the mapping from persistent IDs to strings.
1478 Writer.SetIdentifierOffset(II, Out.tell());
1479 Out.write(II->getName(), KeyLen);
1480 }
1481
1482 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1483 pch::IdentID ID, unsigned) {
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001484 if (!isInterestingIdentifier(II)) {
1485 clang::io::Emit32(Out, ID << 1);
1486 return;
1487 }
Douglas Gregor67d91172009-04-28 21:32:13 +00001488
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001489 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001490 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001491 bool hasMacroDefinition =
1492 II->hasMacroDefinition() &&
1493 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor67d91172009-04-28 21:32:13 +00001494 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001495 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001496 Bits = (Bits << 1) | II->isExtensionToken();
1497 Bits = (Bits << 1) | II->isPoisoned();
1498 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregor67d91172009-04-28 21:32:13 +00001499 clang::io::Emit16(Out, Bits);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001500
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001501 if (hasMacroDefinition)
Douglas Gregor67d91172009-04-28 21:32:13 +00001502 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001503
Douglas Gregorc713da92009-04-21 22:25:48 +00001504 // Emit the declaration IDs in reverse order, because the
1505 // IdentifierResolver provides the declarations as they would be
1506 // visible (e.g., the function "stat" would come before the struct
1507 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1508 // adds declarations to the end of the list (so we need to see the
1509 // struct "status" before the function "status").
1510 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1511 IdentifierResolver::end());
1512 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1513 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001514 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001515 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001516 }
1517};
1518} // end anonymous namespace
1519
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001520/// \brief Write the identifier table into the PCH file.
1521///
1522/// The identifier table consists of a blob containing string data
1523/// (the actual identifiers themselves) and a separate "offsets" index
1524/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001525void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001526 using namespace llvm;
1527
1528 // Create and write out the blob that contains the identifier
1529 // strings.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001530 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001531 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1532
Douglas Gregor91137812009-04-28 20:33:11 +00001533 // Look for any identifiers that were named while processing the
1534 // headers, but are otherwise not needed. We add these to the hash
1535 // table to enable checking of the predefines buffer in the case
1536 // where the user adds new macro definitions when building the PCH
1537 // file.
1538 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1539 IDEnd = PP.getIdentifierTable().end();
1540 ID != IDEnd; ++ID)
1541 getIdentifierRef(ID->second);
1542
Douglas Gregorff9a6092009-04-20 20:36:09 +00001543 // Create the on-disk hash table representation.
Douglas Gregor91137812009-04-28 20:33:11 +00001544 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001545 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1546 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1547 ID != IDEnd; ++ID) {
1548 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor68619772009-04-28 20:01:51 +00001549 Generator.insert(ID->first, ID->second);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001550 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001551
Douglas Gregorff9a6092009-04-20 20:36:09 +00001552 // Create the on-disk hash table in a buffer.
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001553 llvm::SmallString<4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001554 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001555 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001556 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001557 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001558 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001559 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00001560 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001561 }
1562
1563 // Create a blob abbreviation
1564 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1565 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001568 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001569
1570 // Write the identifier table
1571 RecordData Record;
1572 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001573 Record.push_back(BucketOffset);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001574 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001575 }
1576
1577 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001578 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1579 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1582 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1583
1584 RecordData Record;
1585 Record.push_back(pch::IDENTIFIER_OFFSET);
1586 Record.push_back(IdentifierOffsets.size());
1587 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1588 (const char *)&IdentifierOffsets.front(),
1589 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001590}
1591
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001592//===----------------------------------------------------------------------===//
1593// General Serialization Routines
1594//===----------------------------------------------------------------------===//
1595
Douglas Gregor1c507882009-04-15 21:30:51 +00001596/// \brief Write a record containing the given attributes.
1597void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1598 RecordData Record;
1599 for (; Attr; Attr = Attr->getNext()) {
1600 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1601 Record.push_back(Attr->isInherited());
1602 switch (Attr->getKind()) {
1603 case Attr::Alias:
1604 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1605 break;
1606
1607 case Attr::Aligned:
1608 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1609 break;
1610
1611 case Attr::AlwaysInline:
1612 break;
1613
1614 case Attr::AnalyzerNoReturn:
1615 break;
1616
1617 case Attr::Annotate:
1618 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1619 break;
1620
1621 case Attr::AsmLabel:
1622 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1623 break;
1624
1625 case Attr::Blocks:
1626 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1627 break;
1628
1629 case Attr::Cleanup:
1630 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1631 break;
1632
1633 case Attr::Const:
1634 break;
1635
1636 case Attr::Constructor:
1637 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1638 break;
1639
1640 case Attr::DLLExport:
1641 case Attr::DLLImport:
1642 case Attr::Deprecated:
1643 break;
1644
1645 case Attr::Destructor:
1646 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1647 break;
1648
1649 case Attr::FastCall:
1650 break;
1651
1652 case Attr::Format: {
1653 const FormatAttr *Format = cast<FormatAttr>(Attr);
1654 AddString(Format->getType(), Record);
1655 Record.push_back(Format->getFormatIdx());
1656 Record.push_back(Format->getFirstArg());
1657 break;
1658 }
1659
Fariborz Jahanian306d7252009-05-20 17:41:43 +00001660 case Attr::FormatArg: {
1661 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1662 Record.push_back(Format->getFormatIdx());
1663 break;
1664 }
1665
Fariborz Jahanian180f3412009-05-13 18:09:35 +00001666 case Attr::Sentinel : {
1667 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1668 Record.push_back(Sentinel->getSentinel());
1669 Record.push_back(Sentinel->getNullPos());
1670 break;
1671 }
1672
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001673 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001674 case Attr::IBOutletKind:
Ryan Flynn31af0912009-08-09 20:07:29 +00001675 case Attr::Malloc:
Mike Stump9c71db12009-08-26 22:31:08 +00001676 case Attr::NoDebug:
Douglas Gregor1c507882009-04-15 21:30:51 +00001677 case Attr::NoReturn:
1678 case Attr::NoThrow:
Mike Stump9c71db12009-08-26 22:31:08 +00001679 case Attr::NoInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001680 break;
1681
1682 case Attr::NonNull: {
1683 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1684 Record.push_back(NonNull->size());
1685 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1686 break;
1687 }
1688
1689 case Attr::ObjCException:
1690 case Attr::ObjCNSObject:
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001691 case Attr::CFReturnsRetained:
1692 case Attr::NSReturnsRetained:
Douglas Gregor1c507882009-04-15 21:30:51 +00001693 case Attr::Overloadable:
1694 break;
1695
Anders Carlssonc915fa72009-08-08 18:23:56 +00001696 case Attr::PragmaPack:
1697 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor1c507882009-04-15 21:30:51 +00001698 break;
1699
Anders Carlssonc915fa72009-08-08 18:23:56 +00001700 case Attr::Packed:
1701 break;
1702
Douglas Gregor1c507882009-04-15 21:30:51 +00001703 case Attr::Pure:
1704 break;
1705
1706 case Attr::Regparm:
1707 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1708 break;
Nate Begeman60702162009-06-26 06:32:41 +00001709
1710 case Attr::ReqdWorkGroupSize:
1711 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1712 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1713 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1714 break;
Douglas Gregor1c507882009-04-15 21:30:51 +00001715
1716 case Attr::Section:
1717 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1718 break;
1719
1720 case Attr::StdCall:
1721 case Attr::TransparentUnion:
1722 case Attr::Unavailable:
1723 case Attr::Unused:
1724 case Attr::Used:
1725 break;
1726
1727 case Attr::Visibility:
1728 // FIXME: stable encoding
1729 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1730 break;
1731
1732 case Attr::WarnUnusedResult:
1733 case Attr::Weak:
1734 case Attr::WeakImport:
1735 break;
1736 }
1737 }
1738
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001739 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001740}
1741
1742void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1743 Record.push_back(Str.size());
1744 Record.insert(Record.end(), Str.begin(), Str.end());
1745}
1746
Douglas Gregorff9a6092009-04-20 20:36:09 +00001747/// \brief Note that the identifier II occurs at the given offset
1748/// within the identifier table.
1749void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001750 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001751}
1752
Douglas Gregor2d711832009-04-25 17:48:32 +00001753/// \brief Note that the selector Sel occurs at the given offset
1754/// within the method pool/selector table.
1755void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1756 unsigned ID = SelectorIDs[Sel];
1757 assert(ID && "Unknown selector");
1758 SelectorOffsets[ID - 1] = Offset;
1759}
1760
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001761PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001762 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00001763 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1764 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001765
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001766void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1767 const char *isysroot) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00001768 using namespace llvm;
1769
Douglas Gregor87887da2009-04-20 15:53:59 +00001770 ASTContext &Context = SemaRef.Context;
1771 Preprocessor &PP = SemaRef.PP;
1772
Douglas Gregorc34897d2009-04-09 22:27:44 +00001773 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001774 Stream.Emit((unsigned)'C', 8);
1775 Stream.Emit((unsigned)'P', 8);
1776 Stream.Emit((unsigned)'C', 8);
1777 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00001778
1779 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001780
1781 // The translation unit is the first declaration we'll emit.
1782 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1783 DeclsToEmit.push(Context.getTranslationUnitDecl());
1784
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001785 // Make sure that we emit IdentifierInfos (and any attached
1786 // declarations) for builtins.
1787 {
1788 IdentifierTable &Table = PP.getIdentifierTable();
1789 llvm::SmallVector<const char *, 32> BuiltinNames;
1790 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1791 Context.getLangOptions().NoBuiltin);
1792 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1793 getIdentifierRef(&Table.get(BuiltinNames[I]));
1794 }
1795
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001796 // Build a record containing all of the tentative definitions in
1797 // this header file. Generally, this record will be empty.
1798 RecordData TentativeDefinitions;
1799 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
1800 TD = SemaRef.TentativeDefinitions.begin(),
1801 TDEnd = SemaRef.TentativeDefinitions.end();
1802 TD != TDEnd; ++TD)
1803 AddDeclRef(TD->second, TentativeDefinitions);
1804
Douglas Gregor062d9482009-04-22 22:18:58 +00001805 // Build a record containing all of the locally-scoped external
1806 // declarations in this header file. Generally, this record will be
1807 // empty.
1808 RecordData LocallyScopedExternalDecls;
1809 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1810 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1811 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1812 TD != TDEnd; ++TD)
1813 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1814
Douglas Gregorb36b20d2009-04-27 20:06:05 +00001815 // Build a record containing all of the ext_vector declarations.
1816 RecordData ExtVectorDecls;
1817 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1818 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1819
Douglas Gregorc34897d2009-04-09 22:27:44 +00001820 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001821 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00001822 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001823 WriteMetadata(Context, isysroot);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001824 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001825 if (StatCalls && !isysroot)
1826 WriteStatCache(*StatCalls, isysroot);
1827 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001828 WritePreprocessor(PP);
Douglas Gregora252b232009-07-02 17:08:52 +00001829 WriteComments(Context);
Steve Naroff77763c52009-07-18 15:33:26 +00001830 // Write the record of special types.
1831 Record.clear();
1832
1833 AddTypeRef(Context.getBuiltinVaListType(), Record);
1834 AddTypeRef(Context.getObjCIdType(), Record);
1835 AddTypeRef(Context.getObjCSelType(), Record);
1836 AddTypeRef(Context.getObjCProtoType(), Record);
1837 AddTypeRef(Context.getObjCClassType(), Record);
1838 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1839 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1840 AddTypeRef(Context.getFILEType(), Record);
Mike Stumped2f9292009-07-28 02:25:19 +00001841 AddTypeRef(Context.getjmp_bufType(), Record);
1842 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregor90107992009-08-21 00:27:50 +00001843 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1844 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroff77763c52009-07-18 15:33:26 +00001845 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Douglas Gregora252b232009-07-02 17:08:52 +00001846
Douglas Gregore43f0972009-04-26 03:49:13 +00001847 // Keep writing types and declarations until all types and
1848 // declarations have been written.
1849 do {
1850 if (!DeclsToEmit.empty())
1851 WriteDeclsBlock(Context);
1852 if (!TypesToEmit.empty())
1853 WriteTypesBlock(Context);
1854 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1855
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001856 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001857 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00001858
1859 // Write the type offsets array
1860 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1861 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1864 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1865 Record.clear();
1866 Record.push_back(pch::TYPE_OFFSET);
1867 Record.push_back(TypeOffsets.size());
1868 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1869 (const char *)&TypeOffsets.front(),
Chris Lattnerea332f32009-04-27 18:24:17 +00001870 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Douglas Gregor24a224c2009-04-25 18:35:21 +00001871
1872 // Write the declaration offsets array
1873 Abbrev = new BitCodeAbbrev();
1874 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1877 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1878 Record.clear();
1879 Record.push_back(pch::DECL_OFFSET);
1880 Record.push_back(DeclOffsets.size());
1881 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1882 (const char *)&DeclOffsets.front(),
Chris Lattnerea332f32009-04-27 18:24:17 +00001883 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregore01ad442009-04-18 05:55:16 +00001884
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001885 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00001886 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001887 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001888
1889 // Write the record containing tentative definitions.
1890 if (!TentativeDefinitions.empty())
1891 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00001892
1893 // Write the record containing locally-scoped external definitions.
1894 if (!LocallyScopedExternalDecls.empty())
1895 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
1896 LocallyScopedExternalDecls);
Douglas Gregorb36b20d2009-04-27 20:06:05 +00001897
1898 // Write the record containing ext_vector type names.
1899 if (!ExtVectorDecls.empty())
1900 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00001901
1902 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001903 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001904 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001905 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001906 Record.push_back(NumLexicalDeclContexts);
1907 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00001908 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001909 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001910}
1911
1912void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1913 Record.push_back(Loc.getRawEncoding());
1914}
1915
1916void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1917 Record.push_back(Value.getBitWidth());
1918 unsigned N = Value.getNumWords();
1919 const uint64_t* Words = Value.getRawData();
1920 for (unsigned I = 0; I != N; ++I)
1921 Record.push_back(Words[I]);
1922}
1923
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001924void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1925 Record.push_back(Value.isUnsigned());
1926 AddAPInt(Value, Record);
1927}
1928
Douglas Gregore2f37202009-04-14 21:55:33 +00001929void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1930 AddAPInt(Value.bitcastToAPInt(), Record);
1931}
1932
Douglas Gregorc34897d2009-04-09 22:27:44 +00001933void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001934 Record.push_back(getIdentifierRef(II));
1935}
1936
1937pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1938 if (II == 0)
1939 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001940
1941 pch::IdentID &ID = IdentifierIDs[II];
1942 if (ID == 0)
1943 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001944 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001945}
1946
Steve Naroff9e84d782009-04-23 10:39:46 +00001947void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1948 if (SelRef.getAsOpaquePtr() == 0) {
1949 Record.push_back(0);
1950 return;
1951 }
1952
1953 pch::SelectorID &SID = SelectorIDs[SelRef];
1954 if (SID == 0) {
1955 SID = SelectorIDs.size();
1956 SelVector.push_back(SelRef);
1957 }
1958 Record.push_back(SID);
1959}
1960
Douglas Gregorc34897d2009-04-09 22:27:44 +00001961void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1962 if (T.isNull()) {
1963 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1964 return;
1965 }
1966
1967 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001968 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001969 switch (BT->getKind()) {
1970 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1971 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1972 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1973 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1974 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1975 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1976 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1977 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00001978 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001979 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1980 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1981 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1982 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1983 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1984 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1985 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00001986 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001987 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1988 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1989 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001990 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001991 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
1992 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001993 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1994 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff7bffd372009-07-15 18:40:39 +00001995 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
1996 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson4a8498c2009-06-26 18:41:36 +00001997 case BuiltinType::UndeducedAuto:
1998 assert(0 && "Should not see undeduced auto here");
1999 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002000 }
2001
2002 Record.push_back((ID << 3) | T.getCVRQualifiers());
2003 return;
2004 }
2005
Douglas Gregorac8f2802009-04-10 17:25:41 +00002006 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002007 if (ID == 0) {
2008 // We haven't seen this type before. Assign it a new ID and put it
2009 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002010 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002011 TypesToEmit.push(T.getTypePtr());
2012 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002013
2014 // Encode the type qualifiers in the type reference.
2015 Record.push_back((ID << 3) | T.getCVRQualifiers());
2016}
2017
2018void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2019 if (D == 0) {
2020 Record.push_back(0);
2021 return;
2022 }
2023
Douglas Gregorac8f2802009-04-10 17:25:41 +00002024 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002025 if (ID == 0) {
2026 // We haven't seen this declaration before. Give it a new ID and
2027 // enqueue it in the list of declarations to emit.
2028 ID = DeclIDs.size();
2029 DeclsToEmit.push(const_cast<Decl *>(D));
2030 }
2031
2032 Record.push_back(ID);
2033}
2034
Douglas Gregorff9a6092009-04-20 20:36:09 +00002035pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2036 if (D == 0)
2037 return 0;
2038
2039 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2040 return DeclIDs[D];
2041}
2042
Douglas Gregorc34897d2009-04-09 22:27:44 +00002043void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnercd4da472009-04-27 07:35:58 +00002044 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002045 Record.push_back(Name.getNameKind());
2046 switch (Name.getNameKind()) {
2047 case DeclarationName::Identifier:
2048 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2049 break;
2050
2051 case DeclarationName::ObjCZeroArgSelector:
2052 case DeclarationName::ObjCOneArgSelector:
2053 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002054 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002055 break;
2056
2057 case DeclarationName::CXXConstructorName:
2058 case DeclarationName::CXXDestructorName:
2059 case DeclarationName::CXXConversionFunctionName:
2060 AddTypeRef(Name.getCXXNameType(), Record);
2061 break;
2062
2063 case DeclarationName::CXXOperatorName:
2064 Record.push_back(Name.getCXXOverloadedOperator());
2065 break;
2066
2067 case DeclarationName::CXXUsingDirective:
2068 // No extra data to emit
2069 break;
2070 }
2071}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002072