blob: c8b64659994bba631f2fb27468902ebd5ec6cc21 [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"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.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"
Chris Lattner64b65f82009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000037using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Type serialization
41//===----------------------------------------------------------------------===//
Chris Lattnerd83ede52009-04-27 06:16:06 +000042
Douglas Gregorc34897d2009-04-09 22:27:44 +000043namespace {
44 class VISIBILITY_HIDDEN PCHTypeWriter {
45 PCHWriter &Writer;
46 PCHWriter::RecordData &Record;
47
48 public:
49 /// \brief Type code that corresponds to the record generated.
50 pch::TypeCode Code;
51
52 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
53 : Writer(Writer), Record(Record) { }
54
55 void VisitArrayType(const ArrayType *T);
56 void VisitFunctionType(const FunctionType *T);
57 void VisitTagType(const TagType *T);
58
59#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
60#define ABSTRACT_TYPE(Class, Base)
61#define DEPENDENT_TYPE(Class, Base)
62#include "clang/AST/TypeNodes.def"
63 };
64}
65
66void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
67 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
68 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
69 Record.push_back(T->getAddressSpace());
70 Code = pch::TYPE_EXT_QUAL;
71}
72
73void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
74 assert(false && "Built-in types are never serialized");
75}
76
77void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
78 Record.push_back(T->getWidth());
79 Record.push_back(T->isSigned());
80 Code = pch::TYPE_FIXED_WIDTH_INT;
81}
82
83void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
84 Writer.AddTypeRef(T->getElementType(), Record);
85 Code = pch::TYPE_COMPLEX;
86}
87
88void PCHTypeWriter::VisitPointerType(const PointerType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_POINTER;
91}
92
93void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_BLOCK_POINTER;
96}
97
98void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_LVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Code = pch::TYPE_RVALUE_REFERENCE;
106}
107
108void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
109 Writer.AddTypeRef(T->getPointeeType(), Record);
110 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
111 Code = pch::TYPE_MEMBER_POINTER;
112}
113
114void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
115 Writer.AddTypeRef(T->getElementType(), Record);
116 Record.push_back(T->getSizeModifier()); // FIXME: stable values
117 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
118}
119
120void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
121 VisitArrayType(T);
122 Writer.AddAPInt(T->getSize(), Record);
123 Code = pch::TYPE_CONSTANT_ARRAY;
124}
125
126void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
127 VisitArrayType(T);
128 Code = pch::TYPE_INCOMPLETE_ARRAY;
129}
130
131void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
132 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000133 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000134 Code = pch::TYPE_VARIABLE_ARRAY;
135}
136
137void PCHTypeWriter::VisitVectorType(const VectorType *T) {
138 Writer.AddTypeRef(T->getElementType(), Record);
139 Record.push_back(T->getNumElements());
140 Code = pch::TYPE_VECTOR;
141}
142
143void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
144 VisitVectorType(T);
145 Code = pch::TYPE_EXT_VECTOR;
146}
147
148void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
149 Writer.AddTypeRef(T->getResultType(), Record);
150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
164 Code = pch::TYPE_FUNCTION_PROTO;
165}
166
167void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
168 Writer.AddDeclRef(T->getDecl(), Record);
169 Code = pch::TYPE_TYPEDEF;
170}
171
172void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000173 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000174 Code = pch::TYPE_TYPEOF_EXPR;
175}
176
177void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
178 Writer.AddTypeRef(T->getUnderlyingType(), Record);
179 Code = pch::TYPE_TYPEOF;
180}
181
182void PCHTypeWriter::VisitTagType(const TagType *T) {
183 Writer.AddDeclRef(T->getDecl(), Record);
184 assert(!T->isBeingDefined() &&
185 "Cannot serialize in the middle of a type definition");
186}
187
188void PCHTypeWriter::VisitRecordType(const RecordType *T) {
189 VisitTagType(T);
190 Code = pch::TYPE_RECORD;
191}
192
193void PCHTypeWriter::VisitEnumType(const EnumType *T) {
194 VisitTagType(T);
195 Code = pch::TYPE_ENUM;
196}
197
198void
199PCHTypeWriter::VisitTemplateSpecializationType(
200 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000201 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000202 assert(false && "Cannot serialize template specialization types");
203}
204
205void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000206 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000207 assert(false && "Cannot serialize qualified name types");
208}
209
210void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
211 Writer.AddDeclRef(T->getDecl(), Record);
212 Code = pch::TYPE_OBJC_INTERFACE;
213}
214
215void
216PCHTypeWriter::VisitObjCQualifiedInterfaceType(
217 const ObjCQualifiedInterfaceType *T) {
218 VisitObjCInterfaceType(T);
219 Record.push_back(T->getNumProtocols());
220 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
221 Writer.AddDeclRef(T->getProtocol(I), Record);
222 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
223}
224
225void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
226 Record.push_back(T->getNumProtocols());
227 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
228 Writer.AddDeclRef(T->getProtocols(I), Record);
229 Code = pch::TYPE_OBJC_QUALIFIED_ID;
230}
231
Chris Lattner80f83c62009-04-22 05:57:30 +0000232//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000233// PCHWriter Implementation
234//===----------------------------------------------------------------------===//
235
Chris Lattner920673a2009-04-26 22:26:21 +0000236static void EmitBlockID(unsigned ID, const char *Name,
237 llvm::BitstreamWriter &Stream,
238 PCHWriter::RecordData &Record) {
239 Record.clear();
240 Record.push_back(ID);
241 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
242
243 // Emit the block name if present.
244 if (Name == 0 || Name[0] == 0) return;
245 Record.clear();
246 while (*Name)
247 Record.push_back(*Name++);
248 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
249}
250
251static void EmitRecordID(unsigned ID, const char *Name,
252 llvm::BitstreamWriter &Stream,
253 PCHWriter::RecordData &Record) {
254 Record.clear();
255 Record.push_back(ID);
256 while (*Name)
257 Record.push_back(*Name++);
258 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000259}
260
261static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
262 PCHWriter::RecordData &Record) {
263#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
264 RECORD(STMT_STOP);
265 RECORD(STMT_NULL_PTR);
266 RECORD(STMT_NULL);
267 RECORD(STMT_COMPOUND);
268 RECORD(STMT_CASE);
269 RECORD(STMT_DEFAULT);
270 RECORD(STMT_LABEL);
271 RECORD(STMT_IF);
272 RECORD(STMT_SWITCH);
273 RECORD(STMT_WHILE);
274 RECORD(STMT_DO);
275 RECORD(STMT_FOR);
276 RECORD(STMT_GOTO);
277 RECORD(STMT_INDIRECT_GOTO);
278 RECORD(STMT_CONTINUE);
279 RECORD(STMT_BREAK);
280 RECORD(STMT_RETURN);
281 RECORD(STMT_DECL);
282 RECORD(STMT_ASM);
283 RECORD(EXPR_PREDEFINED);
284 RECORD(EXPR_DECL_REF);
285 RECORD(EXPR_INTEGER_LITERAL);
286 RECORD(EXPR_FLOATING_LITERAL);
287 RECORD(EXPR_IMAGINARY_LITERAL);
288 RECORD(EXPR_STRING_LITERAL);
289 RECORD(EXPR_CHARACTER_LITERAL);
290 RECORD(EXPR_PAREN);
291 RECORD(EXPR_UNARY_OPERATOR);
292 RECORD(EXPR_SIZEOF_ALIGN_OF);
293 RECORD(EXPR_ARRAY_SUBSCRIPT);
294 RECORD(EXPR_CALL);
295 RECORD(EXPR_MEMBER);
296 RECORD(EXPR_BINARY_OPERATOR);
297 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
298 RECORD(EXPR_CONDITIONAL_OPERATOR);
299 RECORD(EXPR_IMPLICIT_CAST);
300 RECORD(EXPR_CSTYLE_CAST);
301 RECORD(EXPR_COMPOUND_LITERAL);
302 RECORD(EXPR_EXT_VECTOR_ELEMENT);
303 RECORD(EXPR_INIT_LIST);
304 RECORD(EXPR_DESIGNATED_INIT);
305 RECORD(EXPR_IMPLICIT_VALUE_INIT);
306 RECORD(EXPR_VA_ARG);
307 RECORD(EXPR_ADDR_LABEL);
308 RECORD(EXPR_STMT);
309 RECORD(EXPR_TYPES_COMPATIBLE);
310 RECORD(EXPR_CHOOSE);
311 RECORD(EXPR_GNU_NULL);
312 RECORD(EXPR_SHUFFLE_VECTOR);
313 RECORD(EXPR_BLOCK);
314 RECORD(EXPR_BLOCK_DECL_REF);
315 RECORD(EXPR_OBJC_STRING_LITERAL);
316 RECORD(EXPR_OBJC_ENCODE);
317 RECORD(EXPR_OBJC_SELECTOR_EXPR);
318 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
319 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
320 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
321 RECORD(EXPR_OBJC_KVC_REF_EXPR);
322 RECORD(EXPR_OBJC_MESSAGE_EXPR);
323 RECORD(EXPR_OBJC_SUPER_EXPR);
324 RECORD(STMT_OBJC_FOR_COLLECTION);
325 RECORD(STMT_OBJC_CATCH);
326 RECORD(STMT_OBJC_FINALLY);
327 RECORD(STMT_OBJC_AT_TRY);
328 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
329 RECORD(STMT_OBJC_AT_THROW);
330#undef RECORD
Chris Lattner920673a2009-04-26 22:26:21 +0000331}
332
333void PCHWriter::WriteBlockInfoBlock() {
334 RecordData Record;
335 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
336
Chris Lattner880f3f72009-04-27 00:40:25 +0000337#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner920673a2009-04-26 22:26:21 +0000338#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
339
340 // PCH Top-Level Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000341 BLOCK(PCH_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000342 RECORD(TYPE_OFFSET);
343 RECORD(DECL_OFFSET);
344 RECORD(LANGUAGE_OPTIONS);
345 RECORD(TARGET_TRIPLE);
346 RECORD(IDENTIFIER_OFFSET);
347 RECORD(IDENTIFIER_TABLE);
348 RECORD(EXTERNAL_DEFINITIONS);
349 RECORD(SPECIAL_TYPES);
350 RECORD(STATISTICS);
351 RECORD(TENTATIVE_DEFINITIONS);
352 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
353 RECORD(SELECTOR_OFFSETS);
354 RECORD(METHOD_POOL);
355 RECORD(PP_COUNTER_VALUE);
356
357 // SourceManager Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000358 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000359 RECORD(SM_SLOC_FILE_ENTRY);
360 RECORD(SM_SLOC_BUFFER_ENTRY);
361 RECORD(SM_SLOC_BUFFER_BLOB);
362 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
363 RECORD(SM_LINE_TABLE);
364 RECORD(SM_HEADER_FILE_INFO);
365
366 // Preprocessor Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000367 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000368 RECORD(PP_MACRO_OBJECT_LIKE);
369 RECORD(PP_MACRO_FUNCTION_LIKE);
370 RECORD(PP_TOKEN);
371
372 // Types block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000373 BLOCK(TYPES_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000374 RECORD(TYPE_EXT_QUAL);
375 RECORD(TYPE_FIXED_WIDTH_INT);
376 RECORD(TYPE_COMPLEX);
377 RECORD(TYPE_POINTER);
378 RECORD(TYPE_BLOCK_POINTER);
379 RECORD(TYPE_LVALUE_REFERENCE);
380 RECORD(TYPE_RVALUE_REFERENCE);
381 RECORD(TYPE_MEMBER_POINTER);
382 RECORD(TYPE_CONSTANT_ARRAY);
383 RECORD(TYPE_INCOMPLETE_ARRAY);
384 RECORD(TYPE_VARIABLE_ARRAY);
385 RECORD(TYPE_VECTOR);
386 RECORD(TYPE_EXT_VECTOR);
387 RECORD(TYPE_FUNCTION_PROTO);
388 RECORD(TYPE_FUNCTION_NO_PROTO);
389 RECORD(TYPE_TYPEDEF);
390 RECORD(TYPE_TYPEOF_EXPR);
391 RECORD(TYPE_TYPEOF);
392 RECORD(TYPE_RECORD);
393 RECORD(TYPE_ENUM);
394 RECORD(TYPE_OBJC_INTERFACE);
395 RECORD(TYPE_OBJC_QUALIFIED_INTERFACE);
396 RECORD(TYPE_OBJC_QUALIFIED_ID);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000397 // Statements and Exprs can occur in the Types block.
398 AddStmtsExprs(Stream, Record);
399
Chris Lattner920673a2009-04-26 22:26:21 +0000400 // Decls block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000401 BLOCK(DECLS_BLOCK);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000402 RECORD(DECL_ATTR);
403 RECORD(DECL_TRANSLATION_UNIT);
404 RECORD(DECL_TYPEDEF);
405 RECORD(DECL_ENUM);
406 RECORD(DECL_RECORD);
407 RECORD(DECL_ENUM_CONSTANT);
408 RECORD(DECL_FUNCTION);
409 RECORD(DECL_OBJC_METHOD);
410 RECORD(DECL_OBJC_INTERFACE);
411 RECORD(DECL_OBJC_PROTOCOL);
412 RECORD(DECL_OBJC_IVAR);
413 RECORD(DECL_OBJC_AT_DEFS_FIELD);
414 RECORD(DECL_OBJC_CLASS);
415 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
416 RECORD(DECL_OBJC_CATEGORY);
417 RECORD(DECL_OBJC_CATEGORY_IMPL);
418 RECORD(DECL_OBJC_IMPLEMENTATION);
419 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
420 RECORD(DECL_OBJC_PROPERTY);
421 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +0000422 RECORD(DECL_FIELD);
423 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000424 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +0000425 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000426 RECORD(DECL_ORIGINAL_PARM_VAR);
427 RECORD(DECL_FILE_SCOPE_ASM);
428 RECORD(DECL_BLOCK);
429 RECORD(DECL_CONTEXT_LEXICAL);
430 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000431 // Statements and Exprs can occur in the Decls block.
432 AddStmtsExprs(Stream, Record);
Chris Lattner920673a2009-04-26 22:26:21 +0000433#undef RECORD
434#undef BLOCK
435 Stream.ExitBlock();
436}
437
438
Douglas Gregorb5887f32009-04-10 21:16:55 +0000439/// \brief Write the target triple (e.g., i686-apple-darwin9).
440void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
441 using namespace llvm;
442 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
443 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000445 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000446
447 RecordData Record;
448 Record.push_back(pch::TARGET_TRIPLE);
449 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000450 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000451}
452
453/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000454void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
455 RecordData Record;
456 Record.push_back(LangOpts.Trigraphs);
457 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
458 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
459 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
460 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
461 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
462 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
463 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
464 Record.push_back(LangOpts.C99); // C99 Support
465 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
466 Record.push_back(LangOpts.CPlusPlus); // C++ Support
467 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
468 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
469 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
470
471 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
472 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
473 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
474
475 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
476 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
477 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
478 Record.push_back(LangOpts.LaxVectorConversions);
479 Record.push_back(LangOpts.Exceptions); // Support exception handling.
480
481 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
482 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
483 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
484
485 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
486 // by locks.
487 Record.push_back(LangOpts.Blocks); // block extension to C
488 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
489 // they are unused.
490 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
491 // (modulo the platform support).
492
493 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
494 // signed integer arithmetic overflows.
495
496 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
497 // may be ripped out at any time.
498
499 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
500 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
501 // defined.
502 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
503 // opposed to __DYNAMIC__).
504 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
505
506 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
507 // used (instead of C99 semantics).
508 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
509 Record.push_back(LangOpts.getGCMode());
510 Record.push_back(LangOpts.getVisibilityMode());
511 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000512 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000513}
514
Douglas Gregorab1cef72009-04-10 03:52:48 +0000515//===----------------------------------------------------------------------===//
516// Source Manager Serialization
517//===----------------------------------------------------------------------===//
518
519/// \brief Create an abbreviation for the SLocEntry that refers to a
520/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000521static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000522 using namespace llvm;
523 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
524 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
526 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000530 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000531}
532
533/// \brief Create an abbreviation for the SLocEntry that refers to a
534/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000535static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000536 using namespace llvm;
537 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
538 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000544 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000545}
546
547/// \brief Create an abbreviation for the SLocEntry that refers to a
548/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000549static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000550 using namespace llvm;
551 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
552 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000554 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000555}
556
557/// \brief Create an abbreviation for the SLocEntry that refers to an
558/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000559static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000560 using namespace llvm;
561 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
562 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000568 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000569}
570
571/// \brief Writes the block containing the serialized form of the
572/// source manager.
573///
574/// TODO: We should probably use an on-disk hash table (stored in a
575/// blob), indexed based on the file name, so that we only create
576/// entries for files that we actually need. In the common case (no
577/// errors), we probably won't have to create file entries for any of
578/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000579void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
580 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000581 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000582 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000583
584 // Abbreviations for the various kinds of source-location entries.
585 int SLocFileAbbrv = -1;
586 int SLocBufferAbbrv = -1;
587 int SLocBufferBlobAbbrv = -1;
588 int SLocInstantiationAbbrv = -1;
589
590 // Write out the source location entry table. We skip the first
591 // entry, which is always the same dummy entry.
592 RecordData Record;
593 for (SourceManager::sloc_entry_iterator
594 SLoc = SourceMgr.sloc_entry_begin() + 1,
595 SLocEnd = SourceMgr.sloc_entry_end();
596 SLoc != SLocEnd; ++SLoc) {
597 // Figure out which record code to use.
598 unsigned Code;
599 if (SLoc->isFile()) {
600 if (SLoc->getFile().getContentCache()->Entry)
601 Code = pch::SM_SLOC_FILE_ENTRY;
602 else
603 Code = pch::SM_SLOC_BUFFER_ENTRY;
604 } else
605 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
606 Record.push_back(Code);
607
608 Record.push_back(SLoc->getOffset());
609 if (SLoc->isFile()) {
610 const SrcMgr::FileInfo &File = SLoc->getFile();
611 Record.push_back(File.getIncludeLoc().getRawEncoding());
612 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +0000613 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +0000614
615 const SrcMgr::ContentCache *Content = File.getContentCache();
616 if (Content->Entry) {
617 // The source location entry is a file. The blob associated
618 // with this entry is the file name.
619 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000620 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
621 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +0000622 Content->Entry->getName(),
623 strlen(Content->Entry->getName()));
624 } else {
625 // The source location entry is a buffer. The blob associated
626 // with this entry contains the contents of the buffer.
627 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000628 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
629 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000630 }
631
632 // We add one to the size so that we capture the trailing NULL
633 // that is required by llvm::MemoryBuffer::getMemBuffer (on
634 // the reader side).
635 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
636 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000637 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000638 Record.clear();
639 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000640 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +0000641 Buffer->getBufferStart(),
642 Buffer->getBufferSize() + 1);
643 }
644 } else {
645 // The source location entry is an instantiation.
646 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
647 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
648 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
649 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
650
Douglas Gregor364e5802009-04-15 18:05:10 +0000651 // Compute the token length for this macro expansion.
652 unsigned NextOffset = SourceMgr.getNextOffset();
653 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
654 if (++NextSLoc != SLocEnd)
655 NextOffset = NextSLoc->getOffset();
656 Record.push_back(NextOffset - SLoc->getOffset() - 1);
657
Douglas Gregorab1cef72009-04-10 03:52:48 +0000658 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000659 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
660 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000661 }
662
663 Record.clear();
664 }
665
Douglas Gregor635f97f2009-04-13 16:31:14 +0000666 // Write the line table.
667 if (SourceMgr.hasLineTable()) {
668 LineTableInfo &LineTable = SourceMgr.getLineTable();
669
670 // Emit the file names
671 Record.push_back(LineTable.getNumFilenames());
672 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
673 // Emit the file name
674 const char *Filename = LineTable.getFilename(I);
675 unsigned FilenameLen = Filename? strlen(Filename) : 0;
676 Record.push_back(FilenameLen);
677 if (FilenameLen)
678 Record.insert(Record.end(), Filename, Filename + FilenameLen);
679 }
680
681 // Emit the line entries
682 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
683 L != LEnd; ++L) {
684 // Emit the file ID
685 Record.push_back(L->first);
686
687 // Emit the line entries
688 Record.push_back(L->second.size());
689 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
690 LEEnd = L->second.end();
691 LE != LEEnd; ++LE) {
692 Record.push_back(LE->FileOffset);
693 Record.push_back(LE->LineNo);
694 Record.push_back(LE->FilenameID);
695 Record.push_back((unsigned)LE->FileKind);
696 Record.push_back(LE->IncludeOffset);
697 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000698 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000699 }
700 }
701
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000702 // Loop over all the header files.
703 HeaderSearch &HS = PP.getHeaderSearchInfo();
704 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
705 E = HS.header_file_end();
706 I != E; ++I) {
707 Record.push_back(I->isImport);
708 Record.push_back(I->DirInfo);
709 Record.push_back(I->NumIncludes);
710 if (I->ControllingMacro)
711 AddIdentifierRef(I->ControllingMacro, Record);
712 else
713 Record.push_back(0);
714 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
715 Record.clear();
716 }
717
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000718 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000719}
720
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000721/// \brief Writes the block containing the serialized form of the
722/// preprocessor.
723///
Chris Lattner850eabd2009-04-10 18:08:30 +0000724void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +0000725 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000726
Chris Lattner4b21c202009-04-13 01:29:17 +0000727 // If the preprocessor __COUNTER__ value has been bumped, remember it.
728 if (PP.getCounterValue() != 0) {
729 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000730 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +0000731 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000732 }
733
734 // Enter the preprocessor block.
735 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +0000736
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000737 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
738 // FIXME: use diagnostics subsystem for localization etc.
739 if (PP.SawDateOrTime())
740 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
741
Chris Lattner1b094952009-04-10 18:00:12 +0000742 // Loop over all the macro definitions that are live at the end of the file,
743 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +0000744 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
745 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000746 // FIXME: This emits macros in hash table order, we should do it in a stable
747 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +0000748 MacroInfo *MI = I->second;
749
750 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
751 // been redefined by the header (in which case they are not isBuiltinMacro).
752 if (MI->isBuiltinMacro())
753 continue;
754
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000755 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +0000756 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000757 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +0000758 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
759 Record.push_back(MI->isUsed());
760
761 unsigned Code;
762 if (MI->isObjectLike()) {
763 Code = pch::PP_MACRO_OBJECT_LIKE;
764 } else {
765 Code = pch::PP_MACRO_FUNCTION_LIKE;
766
767 Record.push_back(MI->isC99Varargs());
768 Record.push_back(MI->isGNUVarargs());
769 Record.push_back(MI->getNumArgs());
770 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
771 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +0000772 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000773 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000774 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000775 Record.clear();
776
Chris Lattner850eabd2009-04-10 18:08:30 +0000777 // Emit the tokens array.
778 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
779 // Note that we know that the preprocessor does not have any annotation
780 // tokens in it because they are created by the parser, and thus can't be
781 // in a macro definition.
782 const Token &Tok = MI->getReplacementToken(TokNo);
783
784 Record.push_back(Tok.getLocation().getRawEncoding());
785 Record.push_back(Tok.getLength());
786
Chris Lattner850eabd2009-04-10 18:08:30 +0000787 // FIXME: When reading literal tokens, reconstruct the literal pointer if
788 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +0000789 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +0000790
791 // FIXME: Should translate token kind to a stable encoding.
792 Record.push_back(Tok.getKind());
793 // FIXME: Should translate token flags to a stable encoding.
794 Record.push_back(Tok.getFlags());
795
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000796 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +0000797 Record.clear();
798 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000799 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +0000800 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000801 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000802}
803
804
Douglas Gregorc34897d2009-04-09 22:27:44 +0000805/// \brief Write the representation of a type to the PCH stream.
806void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000807 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +0000808 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +0000809 ID = NextTypeID++;
810
811 // Record the offset for this type.
812 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000813 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000814 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
815 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000816 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000817 }
818
819 RecordData Record;
820
821 // Emit the type's representation.
822 PCHTypeWriter W(*this, Record);
823 switch (T->getTypeClass()) {
824 // For all of the concrete, non-dependent types, call the
825 // appropriate visitor function.
826#define TYPE(Class, Base) \
827 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
828#define ABSTRACT_TYPE(Class, Base)
829#define DEPENDENT_TYPE(Class, Base)
830#include "clang/AST/TypeNodes.def"
831
832 // For all of the dependent type nodes (which only occur in C++
833 // templates), produce an error.
834#define TYPE(Class, Base)
835#define DEPENDENT_TYPE(Class, Base) case Type::Class:
836#include "clang/AST/TypeNodes.def"
837 assert(false && "Cannot serialize dependent type nodes");
838 break;
839 }
840
841 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000842 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000843
844 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000845 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000846}
847
848/// \brief Write a block containing all of the types.
849void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000850 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000851 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000852
Douglas Gregore43f0972009-04-26 03:49:13 +0000853 // Emit all of the types that need to be emitted (so far).
854 while (!TypesToEmit.empty()) {
855 const Type *T = TypesToEmit.front();
856 TypesToEmit.pop();
857 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
858 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000859 }
860
861 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000862 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000863}
864
865/// \brief Write the block containing all of the declaration IDs
866/// lexically declared within the given DeclContext.
867///
868/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
869/// bistream, or 0 if no block was written.
870uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
871 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000872 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +0000873 return 0;
874
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000875 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000876 RecordData Record;
877 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
878 DEnd = DC->decls_end(Context);
879 D != DEnd; ++D)
880 AddDeclRef(*D, Record);
881
Douglas Gregoraf136d92009-04-22 22:34:57 +0000882 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000883 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000884 return Offset;
885}
886
887/// \brief Write the block containing all of the declaration IDs
888/// visible from the given DeclContext.
889///
890/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
891/// bistream, or 0 if no block was written.
892uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
893 DeclContext *DC) {
894 if (DC->getPrimaryContext() != DC)
895 return 0;
896
Douglas Gregor35ca85e2009-04-21 22:32:33 +0000897 // Since there is no name lookup into functions or methods, and we
898 // perform name lookup for the translation unit via the
899 // IdentifierInfo chains, don't bother to build a
900 // visible-declarations table for these entities.
901 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +0000902 return 0;
903
Douglas Gregorc34897d2009-04-09 22:27:44 +0000904 // Force the DeclContext to build a its name-lookup table.
905 DC->lookup(Context, DeclarationName());
906
907 // Serialize the contents of the mapping used for lookup. Note that,
908 // although we have two very different code paths, the serialized
909 // representation is the same for both cases: a declaration name,
910 // followed by a size, followed by references to the visible
911 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000912 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000913 RecordData Record;
914 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000915 if (!Map)
916 return 0;
917
Douglas Gregorc34897d2009-04-09 22:27:44 +0000918 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
919 D != DEnd; ++D) {
920 AddDeclarationName(D->first, Record);
921 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
922 Record.push_back(Result.second - Result.first);
923 for(; Result.first != Result.second; ++Result.first)
924 AddDeclRef(*Result.first, Record);
925 }
926
927 if (Record.size() == 0)
928 return 0;
929
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000930 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +0000931 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000932 return Offset;
933}
934
Douglas Gregorff9a6092009-04-20 20:36:09 +0000935namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000936// Trait used for the on-disk hash table used in the method pool.
937class VISIBILITY_HIDDEN PCHMethodPoolTrait {
938 PCHWriter &Writer;
939
940public:
941 typedef Selector key_type;
942 typedef key_type key_type_ref;
943
944 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
945 typedef const data_type& data_type_ref;
946
947 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
948
949 static unsigned ComputeHash(Selector Sel) {
950 unsigned N = Sel.getNumArgs();
951 if (N == 0)
952 ++N;
953 unsigned R = 5381;
954 for (unsigned I = 0; I != N; ++I)
955 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
956 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
957 return R;
958 }
959
960 std::pair<unsigned,unsigned>
961 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
962 data_type_ref Methods) {
963 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
964 clang::io::Emit16(Out, KeyLen);
965 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
966 for (const ObjCMethodList *Method = &Methods.first; Method;
967 Method = Method->Next)
968 if (Method->Method)
969 DataLen += 4;
970 for (const ObjCMethodList *Method = &Methods.second; Method;
971 Method = Method->Next)
972 if (Method->Method)
973 DataLen += 4;
974 clang::io::Emit16(Out, DataLen);
975 return std::make_pair(KeyLen, DataLen);
976 }
977
Douglas Gregor2d711832009-04-25 17:48:32 +0000978 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
979 uint64_t Start = Out.tell();
980 assert((Start >> 32) == 0 && "Selector key offset too large");
981 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000982 unsigned N = Sel.getNumArgs();
983 clang::io::Emit16(Out, N);
984 if (N == 0)
985 N = 1;
986 for (unsigned I = 0; I != N; ++I)
987 clang::io::Emit32(Out,
988 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
989 }
990
991 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +0000992 data_type_ref Methods, unsigned DataLen) {
993 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000994 unsigned NumInstanceMethods = 0;
995 for (const ObjCMethodList *Method = &Methods.first; Method;
996 Method = Method->Next)
997 if (Method->Method)
998 ++NumInstanceMethods;
999
1000 unsigned NumFactoryMethods = 0;
1001 for (const ObjCMethodList *Method = &Methods.second; Method;
1002 Method = Method->Next)
1003 if (Method->Method)
1004 ++NumFactoryMethods;
1005
1006 clang::io::Emit16(Out, NumInstanceMethods);
1007 clang::io::Emit16(Out, NumFactoryMethods);
1008 for (const ObjCMethodList *Method = &Methods.first; Method;
1009 Method = Method->Next)
1010 if (Method->Method)
1011 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001012 for (const ObjCMethodList *Method = &Methods.second; Method;
1013 Method = Method->Next)
1014 if (Method->Method)
1015 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001016
1017 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001018 }
1019};
1020} // end anonymous namespace
1021
1022/// \brief Write the method pool into the PCH file.
1023///
1024/// The method pool contains both instance and factory methods, stored
1025/// in an on-disk hash table indexed by the selector.
1026void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1027 using namespace llvm;
1028
1029 // Create and write out the blob that contains the instance and
1030 // factor method pools.
1031 bool Empty = true;
1032 {
1033 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1034
1035 // Create the on-disk hash table representation. Start by
1036 // iterating through the instance method pool.
1037 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001038 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001039 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1040 Instance = SemaRef.InstanceMethodPool.begin(),
1041 InstanceEnd = SemaRef.InstanceMethodPool.end();
1042 Instance != InstanceEnd; ++Instance) {
1043 // Check whether there is a factory method with the same
1044 // selector.
1045 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1046 = SemaRef.FactoryMethodPool.find(Instance->first);
1047
1048 if (Factory == SemaRef.FactoryMethodPool.end())
1049 Generator.insert(Instance->first,
1050 std::make_pair(Instance->second,
1051 ObjCMethodList()));
1052 else
1053 Generator.insert(Instance->first,
1054 std::make_pair(Instance->second, Factory->second));
1055
Douglas Gregor2d711832009-04-25 17:48:32 +00001056 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001057 Empty = false;
1058 }
1059
1060 // Now iterate through the factory method pool, to pick up any
1061 // selectors that weren't already in the instance method pool.
1062 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1063 Factory = SemaRef.FactoryMethodPool.begin(),
1064 FactoryEnd = SemaRef.FactoryMethodPool.end();
1065 Factory != FactoryEnd; ++Factory) {
1066 // Check whether there is an instance method with the same
1067 // selector. If so, there is no work to do here.
1068 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1069 = SemaRef.InstanceMethodPool.find(Factory->first);
1070
Douglas Gregor2d711832009-04-25 17:48:32 +00001071 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001072 Generator.insert(Factory->first,
1073 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001074 ++NumSelectorsInMethodPool;
1075 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001076
1077 Empty = false;
1078 }
1079
Douglas Gregor2d711832009-04-25 17:48:32 +00001080 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001081 return;
1082
1083 // Create the on-disk hash table in a buffer.
1084 llvm::SmallVector<char, 4096> MethodPool;
1085 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001086 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001087 {
1088 PCHMethodPoolTrait Trait(*this);
1089 llvm::raw_svector_ostream Out(MethodPool);
1090 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001091 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001092 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001093
1094 // For every selector that we have seen but which was not
1095 // written into the hash table, write the selector itself and
1096 // record it's offset.
1097 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1098 if (SelectorOffsets[I] == 0)
1099 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001100 }
1101
1102 // Create a blob abbreviation
1103 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1104 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1108 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1109
Douglas Gregor2d711832009-04-25 17:48:32 +00001110 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001111 RecordData Record;
1112 Record.push_back(pch::METHOD_POOL);
1113 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001114 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001115 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1116 &MethodPool.front(),
1117 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00001118
1119 // Create a blob abbreviation for the selector table offsets.
1120 Abbrev = new BitCodeAbbrev();
1121 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1124 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1125
1126 // Write the selector offsets table.
1127 Record.clear();
1128 Record.push_back(pch::SELECTOR_OFFSETS);
1129 Record.push_back(SelectorOffsets.size());
1130 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1131 (const char *)&SelectorOffsets.front(),
1132 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001133 }
1134}
1135
1136namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001137class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1138 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001139 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001140
1141public:
1142 typedef const IdentifierInfo* key_type;
1143 typedef key_type key_type_ref;
1144
1145 typedef pch::IdentID data_type;
1146 typedef data_type data_type_ref;
1147
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001148 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1149 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001150
1151 static unsigned ComputeHash(const IdentifierInfo* II) {
1152 return clang::BernsteinHash(II->getName());
1153 }
1154
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001155 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001156 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1157 pch::IdentID ID) {
1158 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00001159 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1160 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001161 if (II->hasMacroDefinition() &&
1162 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1163 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001164 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1165 DEnd = IdentifierResolver::end();
1166 D != DEnd; ++D)
1167 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001168 // We emit the key length after the data length so that the
1169 // "uninteresting" identifiers following the identifier hash table
1170 // structure will have the same (key length, key characters)
1171 // layout as the keys in the hash table. This also matches the
1172 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00001173 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001174 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001175 return std::make_pair(KeyLen, DataLen);
1176 }
1177
1178 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1179 unsigned KeyLen) {
1180 // Record the location of the key data. This is used when generating
1181 // the mapping from persistent IDs to strings.
1182 Writer.SetIdentifierOffset(II, Out.tell());
1183 Out.write(II->getName(), KeyLen);
1184 }
1185
1186 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1187 pch::IdentID ID, unsigned) {
1188 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001189 bool hasMacroDefinition =
1190 II->hasMacroDefinition() &&
1191 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001192 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001193 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1194 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001195 Bits = (Bits << 1) | II->isExtensionToken();
1196 Bits = (Bits << 1) | II->isPoisoned();
1197 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1198 clang::io::Emit32(Out, Bits);
1199 clang::io::Emit32(Out, ID);
1200
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001201 if (hasMacroDefinition)
1202 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1203
Douglas Gregorc713da92009-04-21 22:25:48 +00001204 // Emit the declaration IDs in reverse order, because the
1205 // IdentifierResolver provides the declarations as they would be
1206 // visible (e.g., the function "stat" would come before the struct
1207 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1208 // adds declarations to the end of the list (so we need to see the
1209 // struct "status" before the function "status").
1210 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1211 IdentifierResolver::end());
1212 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1213 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001214 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001215 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001216 }
1217};
1218} // end anonymous namespace
1219
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001220/// \brief Write the identifier table into the PCH file.
1221///
1222/// The identifier table consists of a blob containing string data
1223/// (the actual identifiers themselves) and a separate "offsets" index
1224/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001225void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001226 using namespace llvm;
1227
1228 // Create and write out the blob that contains the identifier
1229 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001230 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001231 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001232 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1233
Douglas Gregor85c4a872009-04-25 21:04:17 +00001234 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
1235
Douglas Gregorff9a6092009-04-20 20:36:09 +00001236 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001237 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1238 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1239 ID != IDEnd; ++ID) {
1240 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00001241
1242 // Classify each identifier as either "interesting" or "not
1243 // interesting". Interesting identifiers are those that have
1244 // additional information that needs to be read from the PCH
1245 // file, e.g., a built-in ID, declaration chain, or macro
1246 // definition. These identifiers are placed into the hash table
1247 // so that they can be found when looked up in the user program.
1248 // All other identifiers are "uninteresting", which means that
1249 // the IdentifierInfo built by default has all of the
1250 // information we care about. Such identifiers are placed after
1251 // the hash table.
1252 const IdentifierInfo *II = ID->first;
1253 if (II->isPoisoned() ||
1254 II->isExtensionToken() ||
1255 II->hasMacroDefinition() ||
1256 II->getObjCOrBuiltinID() ||
1257 II->getFETokenInfo<void>())
1258 Generator.insert(ID->first, ID->second);
1259 else
1260 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001261 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001262
Douglas Gregorff9a6092009-04-20 20:36:09 +00001263 // Create the on-disk hash table in a buffer.
1264 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001265 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001266 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001267 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001268 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001269 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001270 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00001271 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001272
1273 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
1274 const IdentifierInfo *II = UninterestingIdentifiers[I];
1275 unsigned N = II->getLength() + 1;
1276 clang::io::Emit16(Out, N);
1277 SetIdentifierOffset(II, Out.tell());
1278 Out.write(II->getName(), N);
1279 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001280 }
1281
1282 // Create a blob abbreviation
1283 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1284 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001287 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001288
1289 // Write the identifier table
1290 RecordData Record;
1291 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001292 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001293 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1294 &IdentifierTable.front(),
1295 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001296 }
1297
1298 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001299 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1300 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1303 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1304
1305 RecordData Record;
1306 Record.push_back(pch::IDENTIFIER_OFFSET);
1307 Record.push_back(IdentifierOffsets.size());
1308 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1309 (const char *)&IdentifierOffsets.front(),
1310 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001311}
1312
Douglas Gregor1c507882009-04-15 21:30:51 +00001313/// \brief Write a record containing the given attributes.
1314void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1315 RecordData Record;
1316 for (; Attr; Attr = Attr->getNext()) {
1317 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1318 Record.push_back(Attr->isInherited());
1319 switch (Attr->getKind()) {
1320 case Attr::Alias:
1321 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1322 break;
1323
1324 case Attr::Aligned:
1325 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1326 break;
1327
1328 case Attr::AlwaysInline:
1329 break;
1330
1331 case Attr::AnalyzerNoReturn:
1332 break;
1333
1334 case Attr::Annotate:
1335 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1336 break;
1337
1338 case Attr::AsmLabel:
1339 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1340 break;
1341
1342 case Attr::Blocks:
1343 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1344 break;
1345
1346 case Attr::Cleanup:
1347 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1348 break;
1349
1350 case Attr::Const:
1351 break;
1352
1353 case Attr::Constructor:
1354 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1355 break;
1356
1357 case Attr::DLLExport:
1358 case Attr::DLLImport:
1359 case Attr::Deprecated:
1360 break;
1361
1362 case Attr::Destructor:
1363 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1364 break;
1365
1366 case Attr::FastCall:
1367 break;
1368
1369 case Attr::Format: {
1370 const FormatAttr *Format = cast<FormatAttr>(Attr);
1371 AddString(Format->getType(), Record);
1372 Record.push_back(Format->getFormatIdx());
1373 Record.push_back(Format->getFirstArg());
1374 break;
1375 }
1376
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001377 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001378 case Attr::IBOutletKind:
1379 case Attr::NoReturn:
1380 case Attr::NoThrow:
1381 case Attr::Nodebug:
1382 case Attr::Noinline:
1383 break;
1384
1385 case Attr::NonNull: {
1386 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1387 Record.push_back(NonNull->size());
1388 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1389 break;
1390 }
1391
1392 case Attr::ObjCException:
1393 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00001394 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00001395 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00001396 case Attr::Overloadable:
1397 break;
1398
1399 case Attr::Packed:
1400 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1401 break;
1402
1403 case Attr::Pure:
1404 break;
1405
1406 case Attr::Regparm:
1407 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1408 break;
1409
1410 case Attr::Section:
1411 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1412 break;
1413
1414 case Attr::StdCall:
1415 case Attr::TransparentUnion:
1416 case Attr::Unavailable:
1417 case Attr::Unused:
1418 case Attr::Used:
1419 break;
1420
1421 case Attr::Visibility:
1422 // FIXME: stable encoding
1423 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1424 break;
1425
1426 case Attr::WarnUnusedResult:
1427 case Attr::Weak:
1428 case Attr::WeakImport:
1429 break;
1430 }
1431 }
1432
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001433 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001434}
1435
1436void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1437 Record.push_back(Str.size());
1438 Record.insert(Record.end(), Str.begin(), Str.end());
1439}
1440
Douglas Gregorff9a6092009-04-20 20:36:09 +00001441/// \brief Note that the identifier II occurs at the given offset
1442/// within the identifier table.
1443void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001444 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001445}
1446
Douglas Gregor2d711832009-04-25 17:48:32 +00001447/// \brief Note that the selector Sel occurs at the given offset
1448/// within the method pool/selector table.
1449void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1450 unsigned ID = SelectorIDs[Sel];
1451 assert(ID && "Unknown selector");
1452 SelectorOffsets[ID - 1] = Offset;
1453}
1454
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001455PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001456 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00001457 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1458 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001459
Douglas Gregor87887da2009-04-20 15:53:59 +00001460void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00001461 using namespace llvm;
1462
Douglas Gregor87887da2009-04-20 15:53:59 +00001463 ASTContext &Context = SemaRef.Context;
1464 Preprocessor &PP = SemaRef.PP;
1465
Douglas Gregorc34897d2009-04-09 22:27:44 +00001466 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001467 Stream.Emit((unsigned)'C', 8);
1468 Stream.Emit((unsigned)'P', 8);
1469 Stream.Emit((unsigned)'C', 8);
1470 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00001471
1472 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001473
1474 // The translation unit is the first declaration we'll emit.
1475 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1476 DeclsToEmit.push(Context.getTranslationUnitDecl());
1477
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001478 // Make sure that we emit IdentifierInfos (and any attached
1479 // declarations) for builtins.
1480 {
1481 IdentifierTable &Table = PP.getIdentifierTable();
1482 llvm::SmallVector<const char *, 32> BuiltinNames;
1483 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1484 Context.getLangOptions().NoBuiltin);
1485 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1486 getIdentifierRef(&Table.get(BuiltinNames[I]));
1487 }
1488
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001489 // Build a record containing all of the tentative definitions in
1490 // this header file. Generally, this record will be empty.
1491 RecordData TentativeDefinitions;
1492 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
1493 TD = SemaRef.TentativeDefinitions.begin(),
1494 TDEnd = SemaRef.TentativeDefinitions.end();
1495 TD != TDEnd; ++TD)
1496 AddDeclRef(TD->second, TentativeDefinitions);
1497
Douglas Gregor062d9482009-04-22 22:18:58 +00001498 // Build a record containing all of the locally-scoped external
1499 // declarations in this header file. Generally, this record will be
1500 // empty.
1501 RecordData LocallyScopedExternalDecls;
1502 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1503 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1504 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1505 TD != TDEnd; ++TD)
1506 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1507
Douglas Gregorc34897d2009-04-09 22:27:44 +00001508 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001509 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00001510 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001511 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001512 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001513 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001514 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00001515
1516 // Keep writing types and declarations until all types and
1517 // declarations have been written.
1518 do {
1519 if (!DeclsToEmit.empty())
1520 WriteDeclsBlock(Context);
1521 if (!TypesToEmit.empty())
1522 WriteTypesBlock(Context);
1523 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1524
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001525 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001526 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00001527
1528 // Write the type offsets array
1529 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1530 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1533 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1534 Record.clear();
1535 Record.push_back(pch::TYPE_OFFSET);
1536 Record.push_back(TypeOffsets.size());
1537 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1538 (const char *)&TypeOffsets.front(),
1539 TypeOffsets.size() * sizeof(uint64_t));
1540
1541 // Write the declaration offsets array
1542 Abbrev = new BitCodeAbbrev();
1543 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1546 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1547 Record.clear();
1548 Record.push_back(pch::DECL_OFFSET);
1549 Record.push_back(DeclOffsets.size());
1550 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1551 (const char *)&DeclOffsets.front(),
1552 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00001553
1554 // Write the record of special types.
1555 Record.clear();
1556 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00001557 AddTypeRef(Context.getObjCIdType(), Record);
1558 AddTypeRef(Context.getObjCSelType(), Record);
1559 AddTypeRef(Context.getObjCProtoType(), Record);
1560 AddTypeRef(Context.getObjCClassType(), Record);
1561 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1562 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00001563 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1564
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001565 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00001566 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001567 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001568
1569 // Write the record containing tentative definitions.
1570 if (!TentativeDefinitions.empty())
1571 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00001572
1573 // Write the record containing locally-scoped external definitions.
1574 if (!LocallyScopedExternalDecls.empty())
1575 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
1576 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00001577
1578 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001579 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001580 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001581 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001582 Record.push_back(NumLexicalDeclContexts);
1583 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00001584 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001585 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001586}
1587
1588void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1589 Record.push_back(Loc.getRawEncoding());
1590}
1591
1592void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1593 Record.push_back(Value.getBitWidth());
1594 unsigned N = Value.getNumWords();
1595 const uint64_t* Words = Value.getRawData();
1596 for (unsigned I = 0; I != N; ++I)
1597 Record.push_back(Words[I]);
1598}
1599
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001600void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1601 Record.push_back(Value.isUnsigned());
1602 AddAPInt(Value, Record);
1603}
1604
Douglas Gregore2f37202009-04-14 21:55:33 +00001605void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1606 AddAPInt(Value.bitcastToAPInt(), Record);
1607}
1608
Douglas Gregorc34897d2009-04-09 22:27:44 +00001609void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001610 Record.push_back(getIdentifierRef(II));
1611}
1612
1613pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1614 if (II == 0)
1615 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001616
1617 pch::IdentID &ID = IdentifierIDs[II];
1618 if (ID == 0)
1619 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001620 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001621}
1622
Steve Naroff9e84d782009-04-23 10:39:46 +00001623void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1624 if (SelRef.getAsOpaquePtr() == 0) {
1625 Record.push_back(0);
1626 return;
1627 }
1628
1629 pch::SelectorID &SID = SelectorIDs[SelRef];
1630 if (SID == 0) {
1631 SID = SelectorIDs.size();
1632 SelVector.push_back(SelRef);
1633 }
1634 Record.push_back(SID);
1635}
1636
Douglas Gregorc34897d2009-04-09 22:27:44 +00001637void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1638 if (T.isNull()) {
1639 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1640 return;
1641 }
1642
1643 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001644 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001645 switch (BT->getKind()) {
1646 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1647 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1648 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1649 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1650 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1651 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1652 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1653 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1654 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1655 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1656 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1657 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1658 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1659 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1660 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1661 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1662 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1663 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1664 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1665 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1666 }
1667
1668 Record.push_back((ID << 3) | T.getCVRQualifiers());
1669 return;
1670 }
1671
Douglas Gregorac8f2802009-04-10 17:25:41 +00001672 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00001673 if (ID == 0) {
1674 // We haven't seen this type before. Assign it a new ID and put it
1675 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001676 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00001677 TypesToEmit.push(T.getTypePtr());
1678 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001679
1680 // Encode the type qualifiers in the type reference.
1681 Record.push_back((ID << 3) | T.getCVRQualifiers());
1682}
1683
1684void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1685 if (D == 0) {
1686 Record.push_back(0);
1687 return;
1688 }
1689
Douglas Gregorac8f2802009-04-10 17:25:41 +00001690 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001691 if (ID == 0) {
1692 // We haven't seen this declaration before. Give it a new ID and
1693 // enqueue it in the list of declarations to emit.
1694 ID = DeclIDs.size();
1695 DeclsToEmit.push(const_cast<Decl *>(D));
1696 }
1697
1698 Record.push_back(ID);
1699}
1700
Douglas Gregorff9a6092009-04-20 20:36:09 +00001701pch::DeclID PCHWriter::getDeclID(const Decl *D) {
1702 if (D == 0)
1703 return 0;
1704
1705 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
1706 return DeclIDs[D];
1707}
1708
Douglas Gregorc34897d2009-04-09 22:27:44 +00001709void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1710 Record.push_back(Name.getNameKind());
1711 switch (Name.getNameKind()) {
1712 case DeclarationName::Identifier:
1713 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1714 break;
1715
1716 case DeclarationName::ObjCZeroArgSelector:
1717 case DeclarationName::ObjCOneArgSelector:
1718 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00001719 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001720 break;
1721
1722 case DeclarationName::CXXConstructorName:
1723 case DeclarationName::CXXDestructorName:
1724 case DeclarationName::CXXConversionFunctionName:
1725 AddTypeRef(Name.getCXXNameType(), Record);
1726 break;
1727
1728 case DeclarationName::CXXOperatorName:
1729 Record.push_back(Name.getCXXOverloadedOperator());
1730 break;
1731
1732 case DeclarationName::CXXUsingDirective:
1733 // No extra data to emit
1734 break;
1735 }
1736}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001737