blob: ae8a589b60e4526b4ebea743b341e885a602b1ed [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();
530 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record,
531 TripleStr.data(), TripleStr.size());
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000532
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000533 // Original file name
534 SourceManager &SM = Context.getSourceManager();
535 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
536 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
537 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
538 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
539 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
540
541 llvm::sys::Path MainFilePath(MainFile->getName());
542 std::string MainFileName;
543
544 if (!MainFilePath.isAbsolute()) {
545 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner31bc3042009-08-23 22:45:33 +0000546 P.appendComponent(MainFilePath.str());
547 MainFileName = P.str();
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000548 } else {
Chris Lattner31bc3042009-08-23 22:45:33 +0000549 MainFileName = MainFilePath.str();
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000550 }
551
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000552 const char *MainFileNameStr = MainFileName.c_str();
553 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
554 isysroot);
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000555 RecordData Record;
556 Record.push_back(pch::ORIGINAL_FILE_NAME);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000557 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr,
558 strlen(MainFileNameStr));
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000559 }
Douglas Gregorb5887f32009-04-10 21:16:55 +0000560}
561
562/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000563void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
564 RecordData Record;
565 Record.push_back(LangOpts.Trigraphs);
566 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
567 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
568 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
569 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
570 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
571 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
572 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
573 Record.push_back(LangOpts.C99); // C99 Support
574 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
575 Record.push_back(LangOpts.CPlusPlus); // C++ Support
576 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor179cfb12009-04-10 20:39:37 +0000577 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
578
579 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
580 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
581 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
582
583 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor179cfb12009-04-10 20:39:37 +0000584 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
585 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begeman909e06e2009-06-25 23:01:11 +0000586 Record.push_back(LangOpts.AltiVec);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000587 Record.push_back(LangOpts.Exceptions); // Support exception handling.
588
589 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
590 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
591 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
592
Chris Lattnercd4da472009-04-27 07:35:58 +0000593 // Whether static initializers are protected by locks.
594 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000595 Record.push_back(LangOpts.Blocks); // block extension to C
596 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
597 // they are unused.
598 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
599 // (modulo the platform support).
600
601 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
602 // signed integer arithmetic overflows.
603
604 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
605 // may be ripped out at any time.
606
607 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
608 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
609 // defined.
610 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
611 // opposed to __DYNAMIC__).
612 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
613
614 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
615 // used (instead of C99 semantics).
616 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssonf2310142009-05-13 19:49:53 +0000617 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
618 // be enabled.
Eli Friedmand9389be2009-06-05 07:05:05 +0000619 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
620 // unsigned type
Douglas Gregor179cfb12009-04-10 20:39:37 +0000621 Record.push_back(LangOpts.getGCMode());
622 Record.push_back(LangOpts.getVisibilityMode());
623 Record.push_back(LangOpts.InstantiationDepth);
Nate Begeman909e06e2009-06-25 23:01:11 +0000624 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9a0c2a52009-08-22 22:30:33 +0000625 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000626 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000627}
628
Douglas Gregorab1cef72009-04-10 03:52:48 +0000629//===----------------------------------------------------------------------===//
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000630// stat cache Serialization
631//===----------------------------------------------------------------------===//
632
633namespace {
634// Trait used for the on-disk hash table of stat cache results.
635class VISIBILITY_HIDDEN PCHStatCacheTrait {
636public:
637 typedef const char * key_type;
638 typedef key_type key_type_ref;
639
640 typedef std::pair<int, struct stat> data_type;
641 typedef const data_type& data_type_ref;
642
643 static unsigned ComputeHash(const char *path) {
644 return BernsteinHash(path);
645 }
646
647 std::pair<unsigned,unsigned>
648 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
649 data_type_ref Data) {
650 unsigned StrLen = strlen(path);
651 clang::io::Emit16(Out, StrLen);
652 unsigned DataLen = 1; // result value
653 if (Data.first == 0)
654 DataLen += 4 + 4 + 2 + 8 + 8;
655 clang::io::Emit8(Out, DataLen);
656 return std::make_pair(StrLen + 1, DataLen);
657 }
658
659 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
660 Out.write(path, KeyLen);
661 }
662
663 void EmitData(llvm::raw_ostream& Out, key_type_ref,
664 data_type_ref Data, unsigned DataLen) {
665 using namespace clang::io;
666 uint64_t Start = Out.tell(); (void)Start;
667
668 // Result of stat()
669 Emit8(Out, Data.first? 1 : 0);
670
671 if (Data.first == 0) {
672 Emit32(Out, (uint32_t) Data.second.st_ino);
673 Emit32(Out, (uint32_t) Data.second.st_dev);
674 Emit16(Out, (uint16_t) Data.second.st_mode);
675 Emit64(Out, (uint64_t) Data.second.st_mtime);
676 Emit64(Out, (uint64_t) Data.second.st_size);
677 }
678
679 assert(Out.tell() - Start == DataLen && "Wrong data length");
680 }
681};
682} // end anonymous namespace
683
684/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000685void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
686 const char *isysroot) {
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000687 // Build the on-disk hash table containing information about every
688 // stat() call.
689 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
690 unsigned NumStatEntries = 0;
691 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
692 StatEnd = StatCalls.end();
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000693 Stat != StatEnd; ++Stat, ++NumStatEntries) {
694 const char *Filename = Stat->first();
695 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
696 Generator.insert(Filename, Stat->second);
697 }
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000698
699 // Create the on-disk hash table in a buffer.
700 llvm::SmallVector<char, 4096> StatCacheData;
701 uint32_t BucketOffset;
702 {
703 llvm::raw_svector_ostream Out(StatCacheData);
704 // Make sure that no bucket is at offset 0
705 clang::io::Emit32(Out, 0);
706 BucketOffset = Generator.Emit(Out);
707 }
708
709 // Create a blob abbreviation
710 using namespace llvm;
711 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
712 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
715 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
716 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
717
718 // Write the stat cache
719 RecordData Record;
720 Record.push_back(pch::STAT_CACHE);
721 Record.push_back(BucketOffset);
722 Record.push_back(NumStatEntries);
723 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record,
724 &StatCacheData.front(),
725 StatCacheData.size());
726}
727
728//===----------------------------------------------------------------------===//
Douglas Gregorab1cef72009-04-10 03:52:48 +0000729// Source Manager Serialization
730//===----------------------------------------------------------------------===//
731
732/// \brief Create an abbreviation for the SLocEntry that refers to a
733/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000734static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000735 using namespace llvm;
736 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
737 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000743 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000744}
745
746/// \brief Create an abbreviation for the SLocEntry that refers to a
747/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000748static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000749 using namespace llvm;
750 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
751 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000757 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000758}
759
760/// \brief Create an abbreviation for the SLocEntry that refers to a
761/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000762static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000763 using namespace llvm;
764 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
765 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000767 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000768}
769
770/// \brief Create an abbreviation for the SLocEntry that refers to an
771/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000772static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000773 using namespace llvm;
774 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
775 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000781 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000782}
783
784/// \brief Writes the block containing the serialized form of the
785/// source manager.
786///
787/// TODO: We should probably use an on-disk hash table (stored in a
788/// blob), indexed based on the file name, so that we only create
789/// entries for files that we actually need. In the common case (no
790/// errors), we probably won't have to create file entries for any of
791/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000792void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000793 const Preprocessor &PP,
794 const char *isysroot) {
Douglas Gregor32e231c2009-04-27 06:38:32 +0000795 RecordData Record;
796
Chris Lattner84b04f12009-04-10 17:16:57 +0000797 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000798 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000799
800 // Abbreviations for the various kinds of source-location entries.
Chris Lattnereb559a62009-04-27 19:03:22 +0000801 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
802 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
803 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
804 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000805
Douglas Gregor635f97f2009-04-13 16:31:14 +0000806 // Write the line table.
807 if (SourceMgr.hasLineTable()) {
808 LineTableInfo &LineTable = SourceMgr.getLineTable();
809
810 // Emit the file names
811 Record.push_back(LineTable.getNumFilenames());
812 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
813 // Emit the file name
814 const char *Filename = LineTable.getFilename(I);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000815 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000816 unsigned FilenameLen = Filename? strlen(Filename) : 0;
817 Record.push_back(FilenameLen);
818 if (FilenameLen)
819 Record.insert(Record.end(), Filename, Filename + FilenameLen);
820 }
821
822 // Emit the line entries
823 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
824 L != LEnd; ++L) {
825 // Emit the file ID
826 Record.push_back(L->first);
827
828 // Emit the line entries
829 Record.push_back(L->second.size());
830 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
831 LEEnd = L->second.end();
832 LE != LEEnd; ++LE) {
833 Record.push_back(LE->FileOffset);
834 Record.push_back(LE->LineNo);
835 Record.push_back(LE->FilenameID);
836 Record.push_back((unsigned)LE->FileKind);
837 Record.push_back(LE->IncludeOffset);
838 }
Douglas Gregor635f97f2009-04-13 16:31:14 +0000839 }
Zhongxing Xu01838482009-05-22 08:38:27 +0000840 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000841 }
842
Douglas Gregor32e231c2009-04-27 06:38:32 +0000843 // Write out entries for all of the header files we know about.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000844 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000845 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000846 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
847 E = HS.header_file_end();
848 I != E; ++I) {
849 Record.push_back(I->isImport);
850 Record.push_back(I->DirInfo);
851 Record.push_back(I->NumIncludes);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000852 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000853 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
854 Record.clear();
855 }
856
Douglas Gregor32e231c2009-04-27 06:38:32 +0000857 // Write out the source location entry table. We skip the first
858 // entry, which is always the same dummy entry.
Chris Lattner93307da2009-04-27 19:01:47 +0000859 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor32e231c2009-04-27 06:38:32 +0000860 RecordData PreloadSLocs;
861 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
862 for (SourceManager::sloc_entry_iterator
863 SLoc = SourceMgr.sloc_entry_begin() + 1,
864 SLocEnd = SourceMgr.sloc_entry_end();
865 SLoc != SLocEnd; ++SLoc) {
866 // Record the offset of this source-location entry.
867 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
868
869 // Figure out which record code to use.
870 unsigned Code;
871 if (SLoc->isFile()) {
872 if (SLoc->getFile().getContentCache()->Entry)
873 Code = pch::SM_SLOC_FILE_ENTRY;
874 else
875 Code = pch::SM_SLOC_BUFFER_ENTRY;
876 } else
877 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
878 Record.clear();
879 Record.push_back(Code);
880
881 Record.push_back(SLoc->getOffset());
882 if (SLoc->isFile()) {
883 const SrcMgr::FileInfo &File = SLoc->getFile();
884 Record.push_back(File.getIncludeLoc().getRawEncoding());
885 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
886 Record.push_back(File.hasLineDirectives());
887
888 const SrcMgr::ContentCache *Content = File.getContentCache();
889 if (Content->Entry) {
890 // The source location entry is a file. The blob associated
891 // with this entry is the file name.
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000892
893 // Turn the file name into an absolute path, if it isn't already.
894 const char *Filename = Content->Entry->getName();
895 llvm::sys::Path FilePath(Filename, strlen(Filename));
896 std::string FilenameStr;
897 if (!FilePath.isAbsolute()) {
898 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner31bc3042009-08-23 22:45:33 +0000899 P.appendComponent(FilePath.str());
900 FilenameStr = P.str();
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000901 Filename = FilenameStr.c_str();
902 }
903
904 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
905 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename,
906 strlen(Filename));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000907
908 // FIXME: For now, preload all file source locations, so that
909 // we get the appropriate File entries in the reader. This is
910 // a temporary measure.
911 PreloadSLocs.push_back(SLocEntryOffsets.size());
912 } else {
913 // The source location entry is a buffer. The blob associated
914 // with this entry contains the contents of the buffer.
915
916 // We add one to the size so that we capture the trailing NULL
917 // that is required by llvm::MemoryBuffer::getMemBuffer (on
918 // the reader side).
919 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
920 const char *Name = Buffer->getBufferIdentifier();
921 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
922 Record.clear();
923 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
924 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
925 Buffer->getBufferStart(),
926 Buffer->getBufferSize() + 1);
927
928 if (strcmp(Name, "<built-in>") == 0)
929 PreloadSLocs.push_back(SLocEntryOffsets.size());
930 }
931 } else {
932 // The source location entry is an instantiation.
933 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
934 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
935 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
936 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
937
938 // Compute the token length for this macro expansion.
939 unsigned NextOffset = SourceMgr.getNextOffset();
940 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
941 if (++NextSLoc != SLocEnd)
942 NextOffset = NextSLoc->getOffset();
943 Record.push_back(NextOffset - SLoc->getOffset() - 1);
944 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
945 }
946 }
947
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000948 Stream.ExitBlock();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000949
950 if (SLocEntryOffsets.empty())
951 return;
952
953 // Write the source-location offsets table into the PCH block. This
954 // table is used for lazily loading source-location information.
955 using namespace llvm;
956 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
957 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
961 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
962
963 Record.clear();
964 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
965 Record.push_back(SLocEntryOffsets.size());
966 Record.push_back(SourceMgr.getNextOffset());
967 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
968 (const char *)&SLocEntryOffsets.front(),
Chris Lattner93307da2009-04-27 19:01:47 +0000969 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000970
971 // Write the source location entry preloads array, telling the PCH
972 // reader which source locations entries it should load eagerly.
973 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000974}
975
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000976//===----------------------------------------------------------------------===//
977// Preprocessor Serialization
978//===----------------------------------------------------------------------===//
979
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000980/// \brief Writes the block containing the serialized form of the
981/// preprocessor.
982///
Chris Lattner850eabd2009-04-10 18:08:30 +0000983void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +0000984 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000985
Chris Lattner4b21c202009-04-13 01:29:17 +0000986 // If the preprocessor __COUNTER__ value has been bumped, remember it.
987 if (PP.getCounterValue() != 0) {
988 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000989 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +0000990 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000991 }
992
993 // Enter the preprocessor block.
994 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +0000995
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000996 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
997 // FIXME: use diagnostics subsystem for localization etc.
998 if (PP.SawDateOrTime())
999 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1000
Chris Lattner1b094952009-04-10 18:00:12 +00001001 // Loop over all the macro definitions that are live at the end of the file,
1002 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001003 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1004 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001005 // FIXME: This emits macros in hash table order, we should do it in a stable
1006 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001007 MacroInfo *MI = I->second;
1008
1009 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1010 // been redefined by the header (in which case they are not isBuiltinMacro).
1011 if (MI->isBuiltinMacro())
1012 continue;
1013
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001014 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001015 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001016 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001017 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1018 Record.push_back(MI->isUsed());
1019
1020 unsigned Code;
1021 if (MI->isObjectLike()) {
1022 Code = pch::PP_MACRO_OBJECT_LIKE;
1023 } else {
1024 Code = pch::PP_MACRO_FUNCTION_LIKE;
1025
1026 Record.push_back(MI->isC99Varargs());
1027 Record.push_back(MI->isGNUVarargs());
1028 Record.push_back(MI->getNumArgs());
1029 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1030 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001031 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001032 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001033 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001034 Record.clear();
1035
Chris Lattner850eabd2009-04-10 18:08:30 +00001036 // Emit the tokens array.
1037 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1038 // Note that we know that the preprocessor does not have any annotation
1039 // tokens in it because they are created by the parser, and thus can't be
1040 // in a macro definition.
1041 const Token &Tok = MI->getReplacementToken(TokNo);
1042
1043 Record.push_back(Tok.getLocation().getRawEncoding());
1044 Record.push_back(Tok.getLength());
1045
Chris Lattner850eabd2009-04-10 18:08:30 +00001046 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1047 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001048 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001049
1050 // FIXME: Should translate token kind to a stable encoding.
1051 Record.push_back(Tok.getKind());
1052 // FIXME: Should translate token flags to a stable encoding.
1053 Record.push_back(Tok.getFlags());
1054
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001055 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001056 Record.clear();
1057 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001058 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001059 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001060 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001061}
1062
Douglas Gregora252b232009-07-02 17:08:52 +00001063void PCHWriter::WriteComments(ASTContext &Context) {
1064 using namespace llvm;
1065
1066 if (Context.Comments.empty())
1067 return;
1068
1069 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1070 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1071 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1072 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
1073
1074 RecordData Record;
1075 Record.push_back(pch::COMMENT_RANGES);
1076 Stream.EmitRecordWithBlob(CommentCode, Record,
1077 (const char*)&Context.Comments[0],
1078 Context.Comments.size() * sizeof(SourceRange));
1079}
1080
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001081//===----------------------------------------------------------------------===//
1082// Type Serialization
1083//===----------------------------------------------------------------------===//
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001084
Douglas Gregorc34897d2009-04-09 22:27:44 +00001085/// \brief Write the representation of a type to the PCH stream.
1086void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001087 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001088 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001089 ID = NextTypeID++;
1090
1091 // Record the offset for this type.
1092 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001093 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001094 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1095 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001096 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001097 }
1098
1099 RecordData Record;
1100
1101 // Emit the type's representation.
1102 PCHTypeWriter W(*this, Record);
1103 switch (T->getTypeClass()) {
1104 // For all of the concrete, non-dependent types, call the
1105 // appropriate visitor function.
1106#define TYPE(Class, Base) \
1107 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1108#define ABSTRACT_TYPE(Class, Base)
1109#define DEPENDENT_TYPE(Class, Base)
1110#include "clang/AST/TypeNodes.def"
1111
1112 // For all of the dependent type nodes (which only occur in C++
1113 // templates), produce an error.
1114#define TYPE(Class, Base)
1115#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1116#include "clang/AST/TypeNodes.def"
1117 assert(false && "Cannot serialize dependent type nodes");
1118 break;
1119 }
1120
1121 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001122 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001123
1124 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001125 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001126}
1127
1128/// \brief Write a block containing all of the types.
1129void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001130 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001131 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001132
Douglas Gregore43f0972009-04-26 03:49:13 +00001133 // Emit all of the types that need to be emitted (so far).
1134 while (!TypesToEmit.empty()) {
1135 const Type *T = TypesToEmit.front();
1136 TypesToEmit.pop();
1137 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1138 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001139 }
1140
1141 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001142 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001143}
1144
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001145//===----------------------------------------------------------------------===//
1146// Declaration Serialization
1147//===----------------------------------------------------------------------===//
1148
Douglas Gregorc34897d2009-04-09 22:27:44 +00001149/// \brief Write the block containing all of the declaration IDs
1150/// lexically declared within the given DeclContext.
1151///
1152/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1153/// bistream, or 0 if no block was written.
1154uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1155 DeclContext *DC) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001156 if (DC->decls_empty())
Douglas Gregorc34897d2009-04-09 22:27:44 +00001157 return 0;
1158
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001159 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001160 RecordData Record;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001161 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1162 D != DEnd; ++D)
Douglas Gregorc34897d2009-04-09 22:27:44 +00001163 AddDeclRef(*D, Record);
1164
Douglas Gregoraf136d92009-04-22 22:34:57 +00001165 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001166 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001167 return Offset;
1168}
1169
1170/// \brief Write the block containing all of the declaration IDs
1171/// visible from the given DeclContext.
1172///
1173/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1174/// bistream, or 0 if no block was written.
1175uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1176 DeclContext *DC) {
1177 if (DC->getPrimaryContext() != DC)
1178 return 0;
1179
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001180 // Since there is no name lookup into functions or methods, and we
1181 // perform name lookup for the translation unit via the
1182 // IdentifierInfo chains, don't bother to build a
1183 // visible-declarations table for these entities.
1184 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001185 return 0;
1186
Douglas Gregorc34897d2009-04-09 22:27:44 +00001187 // Force the DeclContext to build a its name-lookup table.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001188 DC->lookup(DeclarationName());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001189
1190 // Serialize the contents of the mapping used for lookup. Note that,
1191 // although we have two very different code paths, the serialized
1192 // representation is the same for both cases: a declaration name,
1193 // followed by a size, followed by references to the visible
1194 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001195 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001196 RecordData Record;
1197 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001198 if (!Map)
1199 return 0;
1200
Douglas Gregorc34897d2009-04-09 22:27:44 +00001201 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1202 D != DEnd; ++D) {
1203 AddDeclarationName(D->first, Record);
1204 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1205 Record.push_back(Result.second - Result.first);
1206 for(; Result.first != Result.second; ++Result.first)
1207 AddDeclRef(*Result.first, Record);
1208 }
1209
1210 if (Record.size() == 0)
1211 return 0;
1212
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001213 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001214 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001215 return Offset;
1216}
1217
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001218//===----------------------------------------------------------------------===//
1219// Global Method Pool and Selector Serialization
1220//===----------------------------------------------------------------------===//
1221
Douglas Gregorff9a6092009-04-20 20:36:09 +00001222namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001223// Trait used for the on-disk hash table used in the method pool.
1224class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1225 PCHWriter &Writer;
1226
1227public:
1228 typedef Selector key_type;
1229 typedef key_type key_type_ref;
1230
1231 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1232 typedef const data_type& data_type_ref;
1233
1234 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1235
1236 static unsigned ComputeHash(Selector Sel) {
1237 unsigned N = Sel.getNumArgs();
1238 if (N == 0)
1239 ++N;
1240 unsigned R = 5381;
1241 for (unsigned I = 0; I != N; ++I)
1242 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1243 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1244 return R;
1245 }
1246
1247 std::pair<unsigned,unsigned>
1248 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1249 data_type_ref Methods) {
1250 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1251 clang::io::Emit16(Out, KeyLen);
1252 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1253 for (const ObjCMethodList *Method = &Methods.first; Method;
1254 Method = Method->Next)
1255 if (Method->Method)
1256 DataLen += 4;
1257 for (const ObjCMethodList *Method = &Methods.second; Method;
1258 Method = Method->Next)
1259 if (Method->Method)
1260 DataLen += 4;
1261 clang::io::Emit16(Out, DataLen);
1262 return std::make_pair(KeyLen, DataLen);
1263 }
1264
Douglas Gregor2d711832009-04-25 17:48:32 +00001265 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1266 uint64_t Start = Out.tell();
1267 assert((Start >> 32) == 0 && "Selector key offset too large");
1268 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001269 unsigned N = Sel.getNumArgs();
1270 clang::io::Emit16(Out, N);
1271 if (N == 0)
1272 N = 1;
1273 for (unsigned I = 0; I != N; ++I)
1274 clang::io::Emit32(Out,
1275 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1276 }
1277
1278 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001279 data_type_ref Methods, unsigned DataLen) {
1280 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001281 unsigned NumInstanceMethods = 0;
1282 for (const ObjCMethodList *Method = &Methods.first; Method;
1283 Method = Method->Next)
1284 if (Method->Method)
1285 ++NumInstanceMethods;
1286
1287 unsigned NumFactoryMethods = 0;
1288 for (const ObjCMethodList *Method = &Methods.second; Method;
1289 Method = Method->Next)
1290 if (Method->Method)
1291 ++NumFactoryMethods;
1292
1293 clang::io::Emit16(Out, NumInstanceMethods);
1294 clang::io::Emit16(Out, NumFactoryMethods);
1295 for (const ObjCMethodList *Method = &Methods.first; Method;
1296 Method = Method->Next)
1297 if (Method->Method)
1298 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001299 for (const ObjCMethodList *Method = &Methods.second; Method;
1300 Method = Method->Next)
1301 if (Method->Method)
1302 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001303
1304 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001305 }
1306};
1307} // end anonymous namespace
1308
1309/// \brief Write the method pool into the PCH file.
1310///
1311/// The method pool contains both instance and factory methods, stored
1312/// in an on-disk hash table indexed by the selector.
1313void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1314 using namespace llvm;
1315
1316 // Create and write out the blob that contains the instance and
1317 // factor method pools.
1318 bool Empty = true;
1319 {
1320 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1321
1322 // Create the on-disk hash table representation. Start by
1323 // iterating through the instance method pool.
1324 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001325 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001326 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1327 Instance = SemaRef.InstanceMethodPool.begin(),
1328 InstanceEnd = SemaRef.InstanceMethodPool.end();
1329 Instance != InstanceEnd; ++Instance) {
1330 // Check whether there is a factory method with the same
1331 // selector.
1332 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1333 = SemaRef.FactoryMethodPool.find(Instance->first);
1334
1335 if (Factory == SemaRef.FactoryMethodPool.end())
1336 Generator.insert(Instance->first,
1337 std::make_pair(Instance->second,
1338 ObjCMethodList()));
1339 else
1340 Generator.insert(Instance->first,
1341 std::make_pair(Instance->second, Factory->second));
1342
Douglas Gregor2d711832009-04-25 17:48:32 +00001343 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001344 Empty = false;
1345 }
1346
1347 // Now iterate through the factory method pool, to pick up any
1348 // selectors that weren't already in the instance method pool.
1349 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1350 Factory = SemaRef.FactoryMethodPool.begin(),
1351 FactoryEnd = SemaRef.FactoryMethodPool.end();
1352 Factory != FactoryEnd; ++Factory) {
1353 // Check whether there is an instance method with the same
1354 // selector. If so, there is no work to do here.
1355 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1356 = SemaRef.InstanceMethodPool.find(Factory->first);
1357
Douglas Gregor2d711832009-04-25 17:48:32 +00001358 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001359 Generator.insert(Factory->first,
1360 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001361 ++NumSelectorsInMethodPool;
1362 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001363
1364 Empty = false;
1365 }
1366
Douglas Gregor2d711832009-04-25 17:48:32 +00001367 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001368 return;
1369
1370 // Create the on-disk hash table in a buffer.
1371 llvm::SmallVector<char, 4096> MethodPool;
1372 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001373 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001374 {
1375 PCHMethodPoolTrait Trait(*this);
1376 llvm::raw_svector_ostream Out(MethodPool);
1377 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001378 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001379 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001380
1381 // For every selector that we have seen but which was not
1382 // written into the hash table, write the selector itself and
1383 // record it's offset.
1384 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1385 if (SelectorOffsets[I] == 0)
1386 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001387 }
1388
1389 // Create a blob abbreviation
1390 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1391 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1395 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1396
Douglas Gregor2d711832009-04-25 17:48:32 +00001397 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001398 RecordData Record;
1399 Record.push_back(pch::METHOD_POOL);
1400 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001401 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001402 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1403 &MethodPool.front(),
1404 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00001405
1406 // Create a blob abbreviation for the selector table offsets.
1407 Abbrev = new BitCodeAbbrev();
1408 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1411 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1412
1413 // Write the selector offsets table.
1414 Record.clear();
1415 Record.push_back(pch::SELECTOR_OFFSETS);
1416 Record.push_back(SelectorOffsets.size());
1417 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1418 (const char *)&SelectorOffsets.front(),
1419 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001420 }
1421}
1422
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001423//===----------------------------------------------------------------------===//
1424// Identifier Table Serialization
1425//===----------------------------------------------------------------------===//
1426
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001427namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001428class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1429 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001430 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001431
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001432 /// \brief Determines whether this is an "interesting" identifier
1433 /// that needs a full IdentifierInfo structure written into the hash
1434 /// table.
1435 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1436 return II->isPoisoned() ||
1437 II->isExtensionToken() ||
1438 II->hasMacroDefinition() ||
1439 II->getObjCOrBuiltinID() ||
1440 II->getFETokenInfo<void>();
1441 }
1442
Douglas Gregorff9a6092009-04-20 20:36:09 +00001443public:
1444 typedef const IdentifierInfo* key_type;
1445 typedef key_type key_type_ref;
1446
1447 typedef pch::IdentID data_type;
1448 typedef data_type data_type_ref;
1449
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001450 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1451 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001452
1453 static unsigned ComputeHash(const IdentifierInfo* II) {
1454 return clang::BernsteinHash(II->getName());
1455 }
1456
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001457 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001458 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1459 pch::IdentID ID) {
1460 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001461 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1462 if (isInterestingIdentifier(II)) {
Douglas Gregor67d91172009-04-28 21:32:13 +00001463 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001464 if (II->hasMacroDefinition() &&
1465 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor67d91172009-04-28 21:32:13 +00001466 DataLen += 4;
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001467 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1468 DEnd = IdentifierResolver::end();
1469 D != DEnd; ++D)
1470 DataLen += sizeof(pch::DeclID);
1471 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001472 clang::io::Emit16(Out, DataLen);
Douglas Gregor68619772009-04-28 20:01:51 +00001473 // We emit the key length after the data length so that every
1474 // string is preceded by a 16-bit length. This matches the PTH
1475 // format for storing identifiers.
Douglas Gregor85c4a872009-04-25 21:04:17 +00001476 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001477 return std::make_pair(KeyLen, DataLen);
1478 }
1479
1480 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1481 unsigned KeyLen) {
1482 // Record the location of the key data. This is used when generating
1483 // the mapping from persistent IDs to strings.
1484 Writer.SetIdentifierOffset(II, Out.tell());
1485 Out.write(II->getName(), KeyLen);
1486 }
1487
1488 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1489 pch::IdentID ID, unsigned) {
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001490 if (!isInterestingIdentifier(II)) {
1491 clang::io::Emit32(Out, ID << 1);
1492 return;
1493 }
Douglas Gregor67d91172009-04-28 21:32:13 +00001494
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001495 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001496 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001497 bool hasMacroDefinition =
1498 II->hasMacroDefinition() &&
1499 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor67d91172009-04-28 21:32:13 +00001500 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001501 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001502 Bits = (Bits << 1) | II->isExtensionToken();
1503 Bits = (Bits << 1) | II->isPoisoned();
1504 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregor67d91172009-04-28 21:32:13 +00001505 clang::io::Emit16(Out, Bits);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001506
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001507 if (hasMacroDefinition)
Douglas Gregor67d91172009-04-28 21:32:13 +00001508 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001509
Douglas Gregorc713da92009-04-21 22:25:48 +00001510 // Emit the declaration IDs in reverse order, because the
1511 // IdentifierResolver provides the declarations as they would be
1512 // visible (e.g., the function "stat" would come before the struct
1513 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1514 // adds declarations to the end of the list (so we need to see the
1515 // struct "status" before the function "status").
1516 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1517 IdentifierResolver::end());
1518 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1519 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001520 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001521 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001522 }
1523};
1524} // end anonymous namespace
1525
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001526/// \brief Write the identifier table into the PCH file.
1527///
1528/// The identifier table consists of a blob containing string data
1529/// (the actual identifiers themselves) and a separate "offsets" index
1530/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001531void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001532 using namespace llvm;
1533
1534 // Create and write out the blob that contains the identifier
1535 // strings.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001536 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001537 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1538
Douglas Gregor91137812009-04-28 20:33:11 +00001539 // Look for any identifiers that were named while processing the
1540 // headers, but are otherwise not needed. We add these to the hash
1541 // table to enable checking of the predefines buffer in the case
1542 // where the user adds new macro definitions when building the PCH
1543 // file.
1544 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1545 IDEnd = PP.getIdentifierTable().end();
1546 ID != IDEnd; ++ID)
1547 getIdentifierRef(ID->second);
1548
Douglas Gregorff9a6092009-04-20 20:36:09 +00001549 // Create the on-disk hash table representation.
Douglas Gregor91137812009-04-28 20:33:11 +00001550 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001551 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1552 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1553 ID != IDEnd; ++ID) {
1554 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor68619772009-04-28 20:01:51 +00001555 Generator.insert(ID->first, ID->second);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001556 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001557
Douglas Gregorff9a6092009-04-20 20:36:09 +00001558 // Create the on-disk hash table in a buffer.
1559 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001560 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001561 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001562 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001563 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001564 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001565 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00001566 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001567 }
1568
1569 // Create a blob abbreviation
1570 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1571 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001574 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001575
1576 // Write the identifier table
1577 RecordData Record;
1578 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001579 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001580 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1581 &IdentifierTable.front(),
1582 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001583 }
1584
1585 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001586 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1587 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1590 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1591
1592 RecordData Record;
1593 Record.push_back(pch::IDENTIFIER_OFFSET);
1594 Record.push_back(IdentifierOffsets.size());
1595 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1596 (const char *)&IdentifierOffsets.front(),
1597 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001598}
1599
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001600//===----------------------------------------------------------------------===//
1601// General Serialization Routines
1602//===----------------------------------------------------------------------===//
1603
Douglas Gregor1c507882009-04-15 21:30:51 +00001604/// \brief Write a record containing the given attributes.
1605void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1606 RecordData Record;
1607 for (; Attr; Attr = Attr->getNext()) {
1608 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1609 Record.push_back(Attr->isInherited());
1610 switch (Attr->getKind()) {
1611 case Attr::Alias:
1612 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1613 break;
1614
1615 case Attr::Aligned:
1616 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1617 break;
1618
1619 case Attr::AlwaysInline:
1620 break;
1621
1622 case Attr::AnalyzerNoReturn:
1623 break;
1624
1625 case Attr::Annotate:
1626 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1627 break;
1628
1629 case Attr::AsmLabel:
1630 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1631 break;
1632
1633 case Attr::Blocks:
1634 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1635 break;
1636
1637 case Attr::Cleanup:
1638 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1639 break;
1640
1641 case Attr::Const:
1642 break;
1643
1644 case Attr::Constructor:
1645 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1646 break;
1647
1648 case Attr::DLLExport:
1649 case Attr::DLLImport:
1650 case Attr::Deprecated:
1651 break;
1652
1653 case Attr::Destructor:
1654 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1655 break;
1656
1657 case Attr::FastCall:
1658 break;
1659
1660 case Attr::Format: {
1661 const FormatAttr *Format = cast<FormatAttr>(Attr);
1662 AddString(Format->getType(), Record);
1663 Record.push_back(Format->getFormatIdx());
1664 Record.push_back(Format->getFirstArg());
1665 break;
1666 }
1667
Fariborz Jahanian306d7252009-05-20 17:41:43 +00001668 case Attr::FormatArg: {
1669 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1670 Record.push_back(Format->getFormatIdx());
1671 break;
1672 }
1673
Fariborz Jahanian180f3412009-05-13 18:09:35 +00001674 case Attr::Sentinel : {
1675 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1676 Record.push_back(Sentinel->getSentinel());
1677 Record.push_back(Sentinel->getNullPos());
1678 break;
1679 }
1680
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001681 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001682 case Attr::IBOutletKind:
Ryan Flynn31af0912009-08-09 20:07:29 +00001683 case Attr::Malloc:
Douglas Gregor1c507882009-04-15 21:30:51 +00001684 case Attr::NoReturn:
1685 case Attr::NoThrow:
1686 case Attr::Nodebug:
1687 case Attr::Noinline:
1688 break;
1689
1690 case Attr::NonNull: {
1691 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1692 Record.push_back(NonNull->size());
1693 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1694 break;
1695 }
1696
1697 case Attr::ObjCException:
1698 case Attr::ObjCNSObject:
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001699 case Attr::CFReturnsRetained:
1700 case Attr::NSReturnsRetained:
Douglas Gregor1c507882009-04-15 21:30:51 +00001701 case Attr::Overloadable:
1702 break;
1703
Anders Carlssonc915fa72009-08-08 18:23:56 +00001704 case Attr::PragmaPack:
1705 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor1c507882009-04-15 21:30:51 +00001706 break;
1707
Anders Carlssonc915fa72009-08-08 18:23:56 +00001708 case Attr::Packed:
1709 break;
1710
Douglas Gregor1c507882009-04-15 21:30:51 +00001711 case Attr::Pure:
1712 break;
1713
1714 case Attr::Regparm:
1715 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1716 break;
Nate Begeman60702162009-06-26 06:32:41 +00001717
1718 case Attr::ReqdWorkGroupSize:
1719 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1720 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1721 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1722 break;
Douglas Gregor1c507882009-04-15 21:30:51 +00001723
1724 case Attr::Section:
1725 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1726 break;
1727
1728 case Attr::StdCall:
1729 case Attr::TransparentUnion:
1730 case Attr::Unavailable:
1731 case Attr::Unused:
1732 case Attr::Used:
1733 break;
1734
1735 case Attr::Visibility:
1736 // FIXME: stable encoding
1737 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1738 break;
1739
1740 case Attr::WarnUnusedResult:
1741 case Attr::Weak:
1742 case Attr::WeakImport:
1743 break;
1744 }
1745 }
1746
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001747 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001748}
1749
1750void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1751 Record.push_back(Str.size());
1752 Record.insert(Record.end(), Str.begin(), Str.end());
1753}
1754
Douglas Gregorff9a6092009-04-20 20:36:09 +00001755/// \brief Note that the identifier II occurs at the given offset
1756/// within the identifier table.
1757void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001758 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001759}
1760
Douglas Gregor2d711832009-04-25 17:48:32 +00001761/// \brief Note that the selector Sel occurs at the given offset
1762/// within the method pool/selector table.
1763void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1764 unsigned ID = SelectorIDs[Sel];
1765 assert(ID && "Unknown selector");
1766 SelectorOffsets[ID - 1] = Offset;
1767}
1768
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001769PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001770 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00001771 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1772 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001773
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001774void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1775 const char *isysroot) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00001776 using namespace llvm;
1777
Douglas Gregor87887da2009-04-20 15:53:59 +00001778 ASTContext &Context = SemaRef.Context;
1779 Preprocessor &PP = SemaRef.PP;
1780
Douglas Gregorc34897d2009-04-09 22:27:44 +00001781 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001782 Stream.Emit((unsigned)'C', 8);
1783 Stream.Emit((unsigned)'P', 8);
1784 Stream.Emit((unsigned)'C', 8);
1785 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00001786
1787 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001788
1789 // The translation unit is the first declaration we'll emit.
1790 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1791 DeclsToEmit.push(Context.getTranslationUnitDecl());
1792
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001793 // Make sure that we emit IdentifierInfos (and any attached
1794 // declarations) for builtins.
1795 {
1796 IdentifierTable &Table = PP.getIdentifierTable();
1797 llvm::SmallVector<const char *, 32> BuiltinNames;
1798 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1799 Context.getLangOptions().NoBuiltin);
1800 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1801 getIdentifierRef(&Table.get(BuiltinNames[I]));
1802 }
1803
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001804 // Build a record containing all of the tentative definitions in
1805 // this header file. Generally, this record will be empty.
1806 RecordData TentativeDefinitions;
1807 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
1808 TD = SemaRef.TentativeDefinitions.begin(),
1809 TDEnd = SemaRef.TentativeDefinitions.end();
1810 TD != TDEnd; ++TD)
1811 AddDeclRef(TD->second, TentativeDefinitions);
1812
Douglas Gregor062d9482009-04-22 22:18:58 +00001813 // Build a record containing all of the locally-scoped external
1814 // declarations in this header file. Generally, this record will be
1815 // empty.
1816 RecordData LocallyScopedExternalDecls;
1817 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1818 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1819 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1820 TD != TDEnd; ++TD)
1821 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1822
Douglas Gregorb36b20d2009-04-27 20:06:05 +00001823 // Build a record containing all of the ext_vector declarations.
1824 RecordData ExtVectorDecls;
1825 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1826 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1827
Douglas Gregorc34897d2009-04-09 22:27:44 +00001828 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001829 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00001830 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001831 WriteMetadata(Context, isysroot);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001832 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001833 if (StatCalls && !isysroot)
1834 WriteStatCache(*StatCalls, isysroot);
1835 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001836 WritePreprocessor(PP);
Douglas Gregora252b232009-07-02 17:08:52 +00001837 WriteComments(Context);
Steve Naroff77763c52009-07-18 15:33:26 +00001838 // Write the record of special types.
1839 Record.clear();
1840
1841 AddTypeRef(Context.getBuiltinVaListType(), Record);
1842 AddTypeRef(Context.getObjCIdType(), Record);
1843 AddTypeRef(Context.getObjCSelType(), Record);
1844 AddTypeRef(Context.getObjCProtoType(), Record);
1845 AddTypeRef(Context.getObjCClassType(), Record);
1846 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1847 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1848 AddTypeRef(Context.getFILEType(), Record);
Mike Stumped2f9292009-07-28 02:25:19 +00001849 AddTypeRef(Context.getjmp_bufType(), Record);
1850 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregor90107992009-08-21 00:27:50 +00001851 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1852 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroff77763c52009-07-18 15:33:26 +00001853 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Douglas Gregora252b232009-07-02 17:08:52 +00001854
Douglas Gregore43f0972009-04-26 03:49:13 +00001855 // Keep writing types and declarations until all types and
1856 // declarations have been written.
1857 do {
1858 if (!DeclsToEmit.empty())
1859 WriteDeclsBlock(Context);
1860 if (!TypesToEmit.empty())
1861 WriteTypesBlock(Context);
1862 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1863
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001864 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001865 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00001866
1867 // Write the type offsets array
1868 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1869 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1872 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1873 Record.clear();
1874 Record.push_back(pch::TYPE_OFFSET);
1875 Record.push_back(TypeOffsets.size());
1876 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1877 (const char *)&TypeOffsets.front(),
Chris Lattnerea332f32009-04-27 18:24:17 +00001878 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Douglas Gregor24a224c2009-04-25 18:35:21 +00001879
1880 // Write the declaration offsets array
1881 Abbrev = new BitCodeAbbrev();
1882 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1885 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1886 Record.clear();
1887 Record.push_back(pch::DECL_OFFSET);
1888 Record.push_back(DeclOffsets.size());
1889 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1890 (const char *)&DeclOffsets.front(),
Chris Lattnerea332f32009-04-27 18:24:17 +00001891 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregore01ad442009-04-18 05:55:16 +00001892
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001893 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00001894 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001895 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001896
1897 // Write the record containing tentative definitions.
1898 if (!TentativeDefinitions.empty())
1899 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00001900
1901 // Write the record containing locally-scoped external definitions.
1902 if (!LocallyScopedExternalDecls.empty())
1903 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
1904 LocallyScopedExternalDecls);
Douglas Gregorb36b20d2009-04-27 20:06:05 +00001905
1906 // Write the record containing ext_vector type names.
1907 if (!ExtVectorDecls.empty())
1908 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00001909
1910 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001911 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001912 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001913 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001914 Record.push_back(NumLexicalDeclContexts);
1915 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00001916 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001917 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001918}
1919
1920void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1921 Record.push_back(Loc.getRawEncoding());
1922}
1923
1924void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1925 Record.push_back(Value.getBitWidth());
1926 unsigned N = Value.getNumWords();
1927 const uint64_t* Words = Value.getRawData();
1928 for (unsigned I = 0; I != N; ++I)
1929 Record.push_back(Words[I]);
1930}
1931
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001932void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1933 Record.push_back(Value.isUnsigned());
1934 AddAPInt(Value, Record);
1935}
1936
Douglas Gregore2f37202009-04-14 21:55:33 +00001937void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1938 AddAPInt(Value.bitcastToAPInt(), Record);
1939}
1940
Douglas Gregorc34897d2009-04-09 22:27:44 +00001941void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001942 Record.push_back(getIdentifierRef(II));
1943}
1944
1945pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1946 if (II == 0)
1947 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001948
1949 pch::IdentID &ID = IdentifierIDs[II];
1950 if (ID == 0)
1951 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001952 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001953}
1954
Steve Naroff9e84d782009-04-23 10:39:46 +00001955void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1956 if (SelRef.getAsOpaquePtr() == 0) {
1957 Record.push_back(0);
1958 return;
1959 }
1960
1961 pch::SelectorID &SID = SelectorIDs[SelRef];
1962 if (SID == 0) {
1963 SID = SelectorIDs.size();
1964 SelVector.push_back(SelRef);
1965 }
1966 Record.push_back(SID);
1967}
1968
Douglas Gregorc34897d2009-04-09 22:27:44 +00001969void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1970 if (T.isNull()) {
1971 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1972 return;
1973 }
1974
1975 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001976 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001977 switch (BT->getKind()) {
1978 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1979 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1980 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1981 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1982 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1983 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1984 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1985 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00001986 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001987 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1988 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1989 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1990 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1991 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1992 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1993 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00001994 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001995 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1996 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1997 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001998 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001999 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2000 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002001 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2002 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff7bffd372009-07-15 18:40:39 +00002003 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2004 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson4a8498c2009-06-26 18:41:36 +00002005 case BuiltinType::UndeducedAuto:
2006 assert(0 && "Should not see undeduced auto here");
2007 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002008 }
2009
2010 Record.push_back((ID << 3) | T.getCVRQualifiers());
2011 return;
2012 }
2013
Douglas Gregorac8f2802009-04-10 17:25:41 +00002014 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002015 if (ID == 0) {
2016 // We haven't seen this type before. Assign it a new ID and put it
2017 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002018 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002019 TypesToEmit.push(T.getTypePtr());
2020 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002021
2022 // Encode the type qualifiers in the type reference.
2023 Record.push_back((ID << 3) | T.getCVRQualifiers());
2024}
2025
2026void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2027 if (D == 0) {
2028 Record.push_back(0);
2029 return;
2030 }
2031
Douglas Gregorac8f2802009-04-10 17:25:41 +00002032 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002033 if (ID == 0) {
2034 // We haven't seen this declaration before. Give it a new ID and
2035 // enqueue it in the list of declarations to emit.
2036 ID = DeclIDs.size();
2037 DeclsToEmit.push(const_cast<Decl *>(D));
2038 }
2039
2040 Record.push_back(ID);
2041}
2042
Douglas Gregorff9a6092009-04-20 20:36:09 +00002043pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2044 if (D == 0)
2045 return 0;
2046
2047 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2048 return DeclIDs[D];
2049}
2050
Douglas Gregorc34897d2009-04-09 22:27:44 +00002051void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnercd4da472009-04-27 07:35:58 +00002052 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002053 Record.push_back(Name.getNameKind());
2054 switch (Name.getNameKind()) {
2055 case DeclarationName::Identifier:
2056 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2057 break;
2058
2059 case DeclarationName::ObjCZeroArgSelector:
2060 case DeclarationName::ObjCOneArgSelector:
2061 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002062 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002063 break;
2064
2065 case DeclarationName::CXXConstructorName:
2066 case DeclarationName::CXXDestructorName:
2067 case DeclarationName::CXXConversionFunctionName:
2068 AddTypeRef(Name.getCXXNameType(), Record);
2069 break;
2070
2071 case DeclarationName::CXXOperatorName:
2072 Record.push_back(Name.getCXXOverloadedOperator());
2073 break;
2074
2075 case DeclarationName::CXXUsingDirective:
2076 // No extra data to emit
2077 break;
2078 }
2079}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002080