blob: d181013a37e526b52f4c0f73996a215775ab9ebb [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);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000356 RECORD(SOURCE_LOCATION_OFFSETS);
357 RECORD(SOURCE_LOCATION_PRELOADS);
358
Chris Lattner920673a2009-04-26 22:26:21 +0000359 // SourceManager Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000360 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000361 RECORD(SM_SLOC_FILE_ENTRY);
362 RECORD(SM_SLOC_BUFFER_ENTRY);
363 RECORD(SM_SLOC_BUFFER_BLOB);
364 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
365 RECORD(SM_LINE_TABLE);
366 RECORD(SM_HEADER_FILE_INFO);
367
368 // Preprocessor Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000369 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000370 RECORD(PP_MACRO_OBJECT_LIKE);
371 RECORD(PP_MACRO_FUNCTION_LIKE);
372 RECORD(PP_TOKEN);
373
374 // Types block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000375 BLOCK(TYPES_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000376 RECORD(TYPE_EXT_QUAL);
377 RECORD(TYPE_FIXED_WIDTH_INT);
378 RECORD(TYPE_COMPLEX);
379 RECORD(TYPE_POINTER);
380 RECORD(TYPE_BLOCK_POINTER);
381 RECORD(TYPE_LVALUE_REFERENCE);
382 RECORD(TYPE_RVALUE_REFERENCE);
383 RECORD(TYPE_MEMBER_POINTER);
384 RECORD(TYPE_CONSTANT_ARRAY);
385 RECORD(TYPE_INCOMPLETE_ARRAY);
386 RECORD(TYPE_VARIABLE_ARRAY);
387 RECORD(TYPE_VECTOR);
388 RECORD(TYPE_EXT_VECTOR);
389 RECORD(TYPE_FUNCTION_PROTO);
390 RECORD(TYPE_FUNCTION_NO_PROTO);
391 RECORD(TYPE_TYPEDEF);
392 RECORD(TYPE_TYPEOF_EXPR);
393 RECORD(TYPE_TYPEOF);
394 RECORD(TYPE_RECORD);
395 RECORD(TYPE_ENUM);
396 RECORD(TYPE_OBJC_INTERFACE);
397 RECORD(TYPE_OBJC_QUALIFIED_INTERFACE);
398 RECORD(TYPE_OBJC_QUALIFIED_ID);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000399 // Statements and Exprs can occur in the Types block.
400 AddStmtsExprs(Stream, Record);
401
Chris Lattner920673a2009-04-26 22:26:21 +0000402 // Decls block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000403 BLOCK(DECLS_BLOCK);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000404 RECORD(DECL_ATTR);
405 RECORD(DECL_TRANSLATION_UNIT);
406 RECORD(DECL_TYPEDEF);
407 RECORD(DECL_ENUM);
408 RECORD(DECL_RECORD);
409 RECORD(DECL_ENUM_CONSTANT);
410 RECORD(DECL_FUNCTION);
411 RECORD(DECL_OBJC_METHOD);
412 RECORD(DECL_OBJC_INTERFACE);
413 RECORD(DECL_OBJC_PROTOCOL);
414 RECORD(DECL_OBJC_IVAR);
415 RECORD(DECL_OBJC_AT_DEFS_FIELD);
416 RECORD(DECL_OBJC_CLASS);
417 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
418 RECORD(DECL_OBJC_CATEGORY);
419 RECORD(DECL_OBJC_CATEGORY_IMPL);
420 RECORD(DECL_OBJC_IMPLEMENTATION);
421 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
422 RECORD(DECL_OBJC_PROPERTY);
423 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +0000424 RECORD(DECL_FIELD);
425 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000426 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +0000427 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000428 RECORD(DECL_ORIGINAL_PARM_VAR);
429 RECORD(DECL_FILE_SCOPE_ASM);
430 RECORD(DECL_BLOCK);
431 RECORD(DECL_CONTEXT_LEXICAL);
432 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000433 // Statements and Exprs can occur in the Decls block.
434 AddStmtsExprs(Stream, Record);
Chris Lattner920673a2009-04-26 22:26:21 +0000435#undef RECORD
436#undef BLOCK
437 Stream.ExitBlock();
438}
439
440
Douglas Gregorb5887f32009-04-10 21:16:55 +0000441/// \brief Write the target triple (e.g., i686-apple-darwin9).
442void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
443 using namespace llvm;
444 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
445 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000447 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +0000448
449 RecordData Record;
450 Record.push_back(pch::TARGET_TRIPLE);
451 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000452 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +0000453}
454
455/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000456void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
457 RecordData Record;
458 Record.push_back(LangOpts.Trigraphs);
459 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
460 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
461 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
462 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
463 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
464 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
465 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
466 Record.push_back(LangOpts.C99); // C99 Support
467 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
468 Record.push_back(LangOpts.CPlusPlus); // C++ Support
469 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
470 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
471 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
472
473 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
474 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
475 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
476
477 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
478 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
479 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
480 Record.push_back(LangOpts.LaxVectorConversions);
481 Record.push_back(LangOpts.Exceptions); // Support exception handling.
482
483 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
484 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
485 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
486
487 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
488 // by locks.
489 Record.push_back(LangOpts.Blocks); // block extension to C
490 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
491 // they are unused.
492 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
493 // (modulo the platform support).
494
495 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
496 // signed integer arithmetic overflows.
497
498 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
499 // may be ripped out at any time.
500
501 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
502 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
503 // defined.
504 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
505 // opposed to __DYNAMIC__).
506 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
507
508 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
509 // used (instead of C99 semantics).
510 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
511 Record.push_back(LangOpts.getGCMode());
512 Record.push_back(LangOpts.getVisibilityMode());
513 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000514 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000515}
516
Douglas Gregorab1cef72009-04-10 03:52:48 +0000517//===----------------------------------------------------------------------===//
518// Source Manager Serialization
519//===----------------------------------------------------------------------===//
520
521/// \brief Create an abbreviation for the SLocEntry that refers to a
522/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000523static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000524 using namespace llvm;
525 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
526 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000532 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000533}
534
535/// \brief Create an abbreviation for the SLocEntry that refers to a
536/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000537static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000538 using namespace llvm;
539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
540 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000546 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000547}
548
549/// \brief Create an abbreviation for the SLocEntry that refers to a
550/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000551static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000552 using namespace llvm;
553 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
554 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000556 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000557}
558
559/// \brief Create an abbreviation for the SLocEntry that refers to an
560/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000561static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000562 using namespace llvm;
563 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
564 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000570 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000571}
572
573/// \brief Writes the block containing the serialized form of the
574/// source manager.
575///
576/// TODO: We should probably use an on-disk hash table (stored in a
577/// blob), indexed based on the file name, so that we only create
578/// entries for files that we actually need. In the common case (no
579/// errors), we probably won't have to create file entries for any of
580/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000581void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
582 const Preprocessor &PP) {
Douglas Gregor32e231c2009-04-27 06:38:32 +0000583 RecordData Record;
584
Chris Lattner84b04f12009-04-10 17:16:57 +0000585 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000586 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000587
588 // Abbreviations for the various kinds of source-location entries.
Douglas Gregor32e231c2009-04-27 06:38:32 +0000589 int SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
590 int SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
591 int SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
592 int SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000593
Douglas Gregor635f97f2009-04-13 16:31:14 +0000594 // Write the line table.
595 if (SourceMgr.hasLineTable()) {
596 LineTableInfo &LineTable = SourceMgr.getLineTable();
597
598 // Emit the file names
599 Record.push_back(LineTable.getNumFilenames());
600 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
601 // Emit the file name
602 const char *Filename = LineTable.getFilename(I);
603 unsigned FilenameLen = Filename? strlen(Filename) : 0;
604 Record.push_back(FilenameLen);
605 if (FilenameLen)
606 Record.insert(Record.end(), Filename, Filename + FilenameLen);
607 }
608
609 // Emit the line entries
610 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
611 L != LEnd; ++L) {
612 // Emit the file ID
613 Record.push_back(L->first);
614
615 // Emit the line entries
616 Record.push_back(L->second.size());
617 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
618 LEEnd = L->second.end();
619 LE != LEEnd; ++LE) {
620 Record.push_back(LE->FileOffset);
621 Record.push_back(LE->LineNo);
622 Record.push_back(LE->FilenameID);
623 Record.push_back((unsigned)LE->FileKind);
624 Record.push_back(LE->IncludeOffset);
625 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000626 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000627 }
628 }
629
Douglas Gregor32e231c2009-04-27 06:38:32 +0000630 // Write out entries for all of the header files we know about.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000631 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000632 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000633 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
634 E = HS.header_file_end();
635 I != E; ++I) {
636 Record.push_back(I->isImport);
637 Record.push_back(I->DirInfo);
638 Record.push_back(I->NumIncludes);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000639 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000640 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
641 Record.clear();
642 }
643
Douglas Gregor32e231c2009-04-27 06:38:32 +0000644 // Write out the source location entry table. We skip the first
645 // entry, which is always the same dummy entry.
646 std::vector<uint64_t> SLocEntryOffsets;
647 RecordData PreloadSLocs;
648 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
649 for (SourceManager::sloc_entry_iterator
650 SLoc = SourceMgr.sloc_entry_begin() + 1,
651 SLocEnd = SourceMgr.sloc_entry_end();
652 SLoc != SLocEnd; ++SLoc) {
653 // Record the offset of this source-location entry.
654 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
655
656 // Figure out which record code to use.
657 unsigned Code;
658 if (SLoc->isFile()) {
659 if (SLoc->getFile().getContentCache()->Entry)
660 Code = pch::SM_SLOC_FILE_ENTRY;
661 else
662 Code = pch::SM_SLOC_BUFFER_ENTRY;
663 } else
664 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
665 Record.clear();
666 Record.push_back(Code);
667
668 Record.push_back(SLoc->getOffset());
669 if (SLoc->isFile()) {
670 const SrcMgr::FileInfo &File = SLoc->getFile();
671 Record.push_back(File.getIncludeLoc().getRawEncoding());
672 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
673 Record.push_back(File.hasLineDirectives());
674
675 const SrcMgr::ContentCache *Content = File.getContentCache();
676 if (Content->Entry) {
677 // The source location entry is a file. The blob associated
678 // with this entry is the file name.
679 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
680 Content->Entry->getName(),
681 strlen(Content->Entry->getName()));
682
683 // FIXME: For now, preload all file source locations, so that
684 // we get the appropriate File entries in the reader. This is
685 // a temporary measure.
686 PreloadSLocs.push_back(SLocEntryOffsets.size());
687 } else {
688 // The source location entry is a buffer. The blob associated
689 // with this entry contains the contents of the buffer.
690
691 // We add one to the size so that we capture the trailing NULL
692 // that is required by llvm::MemoryBuffer::getMemBuffer (on
693 // the reader side).
694 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
695 const char *Name = Buffer->getBufferIdentifier();
696 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
697 Record.clear();
698 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
699 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
700 Buffer->getBufferStart(),
701 Buffer->getBufferSize() + 1);
702
703 if (strcmp(Name, "<built-in>") == 0)
704 PreloadSLocs.push_back(SLocEntryOffsets.size());
705 }
706 } else {
707 // The source location entry is an instantiation.
708 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
709 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
710 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
711 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
712
713 // Compute the token length for this macro expansion.
714 unsigned NextOffset = SourceMgr.getNextOffset();
715 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
716 if (++NextSLoc != SLocEnd)
717 NextOffset = NextSLoc->getOffset();
718 Record.push_back(NextOffset - SLoc->getOffset() - 1);
719 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
720 }
721 }
722
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000723 Stream.ExitBlock();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000724
725 if (SLocEntryOffsets.empty())
726 return;
727
728 // Write the source-location offsets table into the PCH block. This
729 // table is used for lazily loading source-location information.
730 using namespace llvm;
731 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
732 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
736 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
737
738 Record.clear();
739 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
740 Record.push_back(SLocEntryOffsets.size());
741 Record.push_back(SourceMgr.getNextOffset());
742 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
743 (const char *)&SLocEntryOffsets.front(),
744 SLocEntryOffsets.size() * 8);
745
746 // Write the source location entry preloads array, telling the PCH
747 // reader which source locations entries it should load eagerly.
748 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000749}
750
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000751/// \brief Writes the block containing the serialized form of the
752/// preprocessor.
753///
Chris Lattner850eabd2009-04-10 18:08:30 +0000754void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +0000755 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000756
Chris Lattner4b21c202009-04-13 01:29:17 +0000757 // If the preprocessor __COUNTER__ value has been bumped, remember it.
758 if (PP.getCounterValue() != 0) {
759 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000760 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +0000761 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000762 }
763
764 // Enter the preprocessor block.
765 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +0000766
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000767 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
768 // FIXME: use diagnostics subsystem for localization etc.
769 if (PP.SawDateOrTime())
770 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
771
Chris Lattner1b094952009-04-10 18:00:12 +0000772 // Loop over all the macro definitions that are live at the end of the file,
773 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +0000774 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
775 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000776 // FIXME: This emits macros in hash table order, we should do it in a stable
777 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +0000778 MacroInfo *MI = I->second;
779
780 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
781 // been redefined by the header (in which case they are not isBuiltinMacro).
782 if (MI->isBuiltinMacro())
783 continue;
784
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000785 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +0000786 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000787 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +0000788 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
789 Record.push_back(MI->isUsed());
790
791 unsigned Code;
792 if (MI->isObjectLike()) {
793 Code = pch::PP_MACRO_OBJECT_LIKE;
794 } else {
795 Code = pch::PP_MACRO_FUNCTION_LIKE;
796
797 Record.push_back(MI->isC99Varargs());
798 Record.push_back(MI->isGNUVarargs());
799 Record.push_back(MI->getNumArgs());
800 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
801 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +0000802 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000803 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000804 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +0000805 Record.clear();
806
Chris Lattner850eabd2009-04-10 18:08:30 +0000807 // Emit the tokens array.
808 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
809 // Note that we know that the preprocessor does not have any annotation
810 // tokens in it because they are created by the parser, and thus can't be
811 // in a macro definition.
812 const Token &Tok = MI->getReplacementToken(TokNo);
813
814 Record.push_back(Tok.getLocation().getRawEncoding());
815 Record.push_back(Tok.getLength());
816
Chris Lattner850eabd2009-04-10 18:08:30 +0000817 // FIXME: When reading literal tokens, reconstruct the literal pointer if
818 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +0000819 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +0000820
821 // FIXME: Should translate token kind to a stable encoding.
822 Record.push_back(Tok.getKind());
823 // FIXME: Should translate token flags to a stable encoding.
824 Record.push_back(Tok.getFlags());
825
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000826 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +0000827 Record.clear();
828 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +0000829 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +0000830 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000831 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000832}
833
834
Douglas Gregorc34897d2009-04-09 22:27:44 +0000835/// \brief Write the representation of a type to the PCH stream.
836void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000837 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +0000838 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +0000839 ID = NextTypeID++;
840
841 // Record the offset for this type.
842 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000843 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000844 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
845 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000846 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000847 }
848
849 RecordData Record;
850
851 // Emit the type's representation.
852 PCHTypeWriter W(*this, Record);
853 switch (T->getTypeClass()) {
854 // For all of the concrete, non-dependent types, call the
855 // appropriate visitor function.
856#define TYPE(Class, Base) \
857 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
858#define ABSTRACT_TYPE(Class, Base)
859#define DEPENDENT_TYPE(Class, Base)
860#include "clang/AST/TypeNodes.def"
861
862 // For all of the dependent type nodes (which only occur in C++
863 // templates), produce an error.
864#define TYPE(Class, Base)
865#define DEPENDENT_TYPE(Class, Base) case Type::Class:
866#include "clang/AST/TypeNodes.def"
867 assert(false && "Cannot serialize dependent type nodes");
868 break;
869 }
870
871 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000872 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000873
874 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000875 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000876}
877
878/// \brief Write a block containing all of the types.
879void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +0000880 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000881 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000882
Douglas Gregore43f0972009-04-26 03:49:13 +0000883 // Emit all of the types that need to be emitted (so far).
884 while (!TypesToEmit.empty()) {
885 const Type *T = TypesToEmit.front();
886 TypesToEmit.pop();
887 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
888 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000889 }
890
891 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000892 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000893}
894
895/// \brief Write the block containing all of the declaration IDs
896/// lexically declared within the given DeclContext.
897///
898/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
899/// bistream, or 0 if no block was written.
900uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
901 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000902 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +0000903 return 0;
904
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000905 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000906 RecordData Record;
907 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
908 DEnd = DC->decls_end(Context);
909 D != DEnd; ++D)
910 AddDeclRef(*D, Record);
911
Douglas Gregoraf136d92009-04-22 22:34:57 +0000912 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000913 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000914 return Offset;
915}
916
917/// \brief Write the block containing all of the declaration IDs
918/// visible from the given DeclContext.
919///
920/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
921/// bistream, or 0 if no block was written.
922uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
923 DeclContext *DC) {
924 if (DC->getPrimaryContext() != DC)
925 return 0;
926
Douglas Gregor35ca85e2009-04-21 22:32:33 +0000927 // Since there is no name lookup into functions or methods, and we
928 // perform name lookup for the translation unit via the
929 // IdentifierInfo chains, don't bother to build a
930 // visible-declarations table for these entities.
931 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +0000932 return 0;
933
Douglas Gregorc34897d2009-04-09 22:27:44 +0000934 // Force the DeclContext to build a its name-lookup table.
935 DC->lookup(Context, DeclarationName());
936
937 // Serialize the contents of the mapping used for lookup. Note that,
938 // although we have two very different code paths, the serialized
939 // representation is the same for both cases: a declaration name,
940 // followed by a size, followed by references to the visible
941 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000942 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000943 RecordData Record;
944 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000945 if (!Map)
946 return 0;
947
Douglas Gregorc34897d2009-04-09 22:27:44 +0000948 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
949 D != DEnd; ++D) {
950 AddDeclarationName(D->first, Record);
951 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
952 Record.push_back(Result.second - Result.first);
953 for(; Result.first != Result.second; ++Result.first)
954 AddDeclRef(*Result.first, Record);
955 }
956
957 if (Record.size() == 0)
958 return 0;
959
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000960 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +0000961 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000962 return Offset;
963}
964
Douglas Gregorff9a6092009-04-20 20:36:09 +0000965namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +0000966// Trait used for the on-disk hash table used in the method pool.
967class VISIBILITY_HIDDEN PCHMethodPoolTrait {
968 PCHWriter &Writer;
969
970public:
971 typedef Selector key_type;
972 typedef key_type key_type_ref;
973
974 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
975 typedef const data_type& data_type_ref;
976
977 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
978
979 static unsigned ComputeHash(Selector Sel) {
980 unsigned N = Sel.getNumArgs();
981 if (N == 0)
982 ++N;
983 unsigned R = 5381;
984 for (unsigned I = 0; I != N; ++I)
985 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
986 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
987 return R;
988 }
989
990 std::pair<unsigned,unsigned>
991 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
992 data_type_ref Methods) {
993 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
994 clang::io::Emit16(Out, KeyLen);
995 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
996 for (const ObjCMethodList *Method = &Methods.first; Method;
997 Method = Method->Next)
998 if (Method->Method)
999 DataLen += 4;
1000 for (const ObjCMethodList *Method = &Methods.second; Method;
1001 Method = Method->Next)
1002 if (Method->Method)
1003 DataLen += 4;
1004 clang::io::Emit16(Out, DataLen);
1005 return std::make_pair(KeyLen, DataLen);
1006 }
1007
Douglas Gregor2d711832009-04-25 17:48:32 +00001008 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1009 uint64_t Start = Out.tell();
1010 assert((Start >> 32) == 0 && "Selector key offset too large");
1011 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001012 unsigned N = Sel.getNumArgs();
1013 clang::io::Emit16(Out, N);
1014 if (N == 0)
1015 N = 1;
1016 for (unsigned I = 0; I != N; ++I)
1017 clang::io::Emit32(Out,
1018 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1019 }
1020
1021 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001022 data_type_ref Methods, unsigned DataLen) {
1023 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001024 unsigned NumInstanceMethods = 0;
1025 for (const ObjCMethodList *Method = &Methods.first; Method;
1026 Method = Method->Next)
1027 if (Method->Method)
1028 ++NumInstanceMethods;
1029
1030 unsigned NumFactoryMethods = 0;
1031 for (const ObjCMethodList *Method = &Methods.second; Method;
1032 Method = Method->Next)
1033 if (Method->Method)
1034 ++NumFactoryMethods;
1035
1036 clang::io::Emit16(Out, NumInstanceMethods);
1037 clang::io::Emit16(Out, NumFactoryMethods);
1038 for (const ObjCMethodList *Method = &Methods.first; Method;
1039 Method = Method->Next)
1040 if (Method->Method)
1041 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001042 for (const ObjCMethodList *Method = &Methods.second; Method;
1043 Method = Method->Next)
1044 if (Method->Method)
1045 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001046
1047 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001048 }
1049};
1050} // end anonymous namespace
1051
1052/// \brief Write the method pool into the PCH file.
1053///
1054/// The method pool contains both instance and factory methods, stored
1055/// in an on-disk hash table indexed by the selector.
1056void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1057 using namespace llvm;
1058
1059 // Create and write out the blob that contains the instance and
1060 // factor method pools.
1061 bool Empty = true;
1062 {
1063 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1064
1065 // Create the on-disk hash table representation. Start by
1066 // iterating through the instance method pool.
1067 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001068 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001069 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1070 Instance = SemaRef.InstanceMethodPool.begin(),
1071 InstanceEnd = SemaRef.InstanceMethodPool.end();
1072 Instance != InstanceEnd; ++Instance) {
1073 // Check whether there is a factory method with the same
1074 // selector.
1075 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1076 = SemaRef.FactoryMethodPool.find(Instance->first);
1077
1078 if (Factory == SemaRef.FactoryMethodPool.end())
1079 Generator.insert(Instance->first,
1080 std::make_pair(Instance->second,
1081 ObjCMethodList()));
1082 else
1083 Generator.insert(Instance->first,
1084 std::make_pair(Instance->second, Factory->second));
1085
Douglas Gregor2d711832009-04-25 17:48:32 +00001086 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001087 Empty = false;
1088 }
1089
1090 // Now iterate through the factory method pool, to pick up any
1091 // selectors that weren't already in the instance method pool.
1092 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1093 Factory = SemaRef.FactoryMethodPool.begin(),
1094 FactoryEnd = SemaRef.FactoryMethodPool.end();
1095 Factory != FactoryEnd; ++Factory) {
1096 // Check whether there is an instance method with the same
1097 // selector. If so, there is no work to do here.
1098 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1099 = SemaRef.InstanceMethodPool.find(Factory->first);
1100
Douglas Gregor2d711832009-04-25 17:48:32 +00001101 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001102 Generator.insert(Factory->first,
1103 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001104 ++NumSelectorsInMethodPool;
1105 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001106
1107 Empty = false;
1108 }
1109
Douglas Gregor2d711832009-04-25 17:48:32 +00001110 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001111 return;
1112
1113 // Create the on-disk hash table in a buffer.
1114 llvm::SmallVector<char, 4096> MethodPool;
1115 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001116 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001117 {
1118 PCHMethodPoolTrait Trait(*this);
1119 llvm::raw_svector_ostream Out(MethodPool);
1120 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001121 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001122 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001123
1124 // For every selector that we have seen but which was not
1125 // written into the hash table, write the selector itself and
1126 // record it's offset.
1127 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1128 if (SelectorOffsets[I] == 0)
1129 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001130 }
1131
1132 // Create a blob abbreviation
1133 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1134 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1138 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1139
Douglas Gregor2d711832009-04-25 17:48:32 +00001140 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001141 RecordData Record;
1142 Record.push_back(pch::METHOD_POOL);
1143 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001144 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001145 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1146 &MethodPool.front(),
1147 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00001148
1149 // Create a blob abbreviation for the selector table offsets.
1150 Abbrev = new BitCodeAbbrev();
1151 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1152 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1154 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1155
1156 // Write the selector offsets table.
1157 Record.clear();
1158 Record.push_back(pch::SELECTOR_OFFSETS);
1159 Record.push_back(SelectorOffsets.size());
1160 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1161 (const char *)&SelectorOffsets.front(),
1162 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001163 }
1164}
1165
1166namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001167class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1168 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001169 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001170
1171public:
1172 typedef const IdentifierInfo* key_type;
1173 typedef key_type key_type_ref;
1174
1175 typedef pch::IdentID data_type;
1176 typedef data_type data_type_ref;
1177
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001178 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1179 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001180
1181 static unsigned ComputeHash(const IdentifierInfo* II) {
1182 return clang::BernsteinHash(II->getName());
1183 }
1184
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001185 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001186 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1187 pch::IdentID ID) {
1188 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00001189 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1190 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001191 if (II->hasMacroDefinition() &&
1192 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1193 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001194 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1195 DEnd = IdentifierResolver::end();
1196 D != DEnd; ++D)
1197 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001198 // We emit the key length after the data length so that the
1199 // "uninteresting" identifiers following the identifier hash table
1200 // structure will have the same (key length, key characters)
1201 // layout as the keys in the hash table. This also matches the
1202 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00001203 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001204 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001205 return std::make_pair(KeyLen, DataLen);
1206 }
1207
1208 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1209 unsigned KeyLen) {
1210 // Record the location of the key data. This is used when generating
1211 // the mapping from persistent IDs to strings.
1212 Writer.SetIdentifierOffset(II, Out.tell());
1213 Out.write(II->getName(), KeyLen);
1214 }
1215
1216 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1217 pch::IdentID ID, unsigned) {
1218 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001219 bool hasMacroDefinition =
1220 II->hasMacroDefinition() &&
1221 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001222 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001223 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1224 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001225 Bits = (Bits << 1) | II->isExtensionToken();
1226 Bits = (Bits << 1) | II->isPoisoned();
1227 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1228 clang::io::Emit32(Out, Bits);
1229 clang::io::Emit32(Out, ID);
1230
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001231 if (hasMacroDefinition)
1232 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1233
Douglas Gregorc713da92009-04-21 22:25:48 +00001234 // Emit the declaration IDs in reverse order, because the
1235 // IdentifierResolver provides the declarations as they would be
1236 // visible (e.g., the function "stat" would come before the struct
1237 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1238 // adds declarations to the end of the list (so we need to see the
1239 // struct "status" before the function "status").
1240 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1241 IdentifierResolver::end());
1242 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1243 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001244 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001245 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001246 }
1247};
1248} // end anonymous namespace
1249
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001250/// \brief Write the identifier table into the PCH file.
1251///
1252/// The identifier table consists of a blob containing string data
1253/// (the actual identifiers themselves) and a separate "offsets" index
1254/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001255void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001256 using namespace llvm;
1257
1258 // Create and write out the blob that contains the identifier
1259 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001260 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001261 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001262 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1263
Douglas Gregor85c4a872009-04-25 21:04:17 +00001264 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
1265
Douglas Gregorff9a6092009-04-20 20:36:09 +00001266 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001267 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1268 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1269 ID != IDEnd; ++ID) {
1270 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00001271
1272 // Classify each identifier as either "interesting" or "not
1273 // interesting". Interesting identifiers are those that have
1274 // additional information that needs to be read from the PCH
1275 // file, e.g., a built-in ID, declaration chain, or macro
1276 // definition. These identifiers are placed into the hash table
1277 // so that they can be found when looked up in the user program.
1278 // All other identifiers are "uninteresting", which means that
1279 // the IdentifierInfo built by default has all of the
1280 // information we care about. Such identifiers are placed after
1281 // the hash table.
1282 const IdentifierInfo *II = ID->first;
1283 if (II->isPoisoned() ||
1284 II->isExtensionToken() ||
1285 II->hasMacroDefinition() ||
1286 II->getObjCOrBuiltinID() ||
1287 II->getFETokenInfo<void>())
1288 Generator.insert(ID->first, ID->second);
1289 else
1290 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001291 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001292
Douglas Gregorff9a6092009-04-20 20:36:09 +00001293 // Create the on-disk hash table in a buffer.
1294 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001295 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001296 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001297 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001298 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001299 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001300 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00001301 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001302
1303 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
1304 const IdentifierInfo *II = UninterestingIdentifiers[I];
1305 unsigned N = II->getLength() + 1;
1306 clang::io::Emit16(Out, N);
1307 SetIdentifierOffset(II, Out.tell());
1308 Out.write(II->getName(), N);
1309 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001310 }
1311
1312 // Create a blob abbreviation
1313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1314 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001317 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001318
1319 // Write the identifier table
1320 RecordData Record;
1321 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001322 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001323 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
1324 &IdentifierTable.front(),
1325 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001326 }
1327
1328 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001329 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1330 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1333 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1334
1335 RecordData Record;
1336 Record.push_back(pch::IDENTIFIER_OFFSET);
1337 Record.push_back(IdentifierOffsets.size());
1338 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1339 (const char *)&IdentifierOffsets.front(),
1340 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001341}
1342
Douglas Gregor1c507882009-04-15 21:30:51 +00001343/// \brief Write a record containing the given attributes.
1344void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1345 RecordData Record;
1346 for (; Attr; Attr = Attr->getNext()) {
1347 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1348 Record.push_back(Attr->isInherited());
1349 switch (Attr->getKind()) {
1350 case Attr::Alias:
1351 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1352 break;
1353
1354 case Attr::Aligned:
1355 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1356 break;
1357
1358 case Attr::AlwaysInline:
1359 break;
1360
1361 case Attr::AnalyzerNoReturn:
1362 break;
1363
1364 case Attr::Annotate:
1365 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1366 break;
1367
1368 case Attr::AsmLabel:
1369 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1370 break;
1371
1372 case Attr::Blocks:
1373 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1374 break;
1375
1376 case Attr::Cleanup:
1377 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1378 break;
1379
1380 case Attr::Const:
1381 break;
1382
1383 case Attr::Constructor:
1384 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1385 break;
1386
1387 case Attr::DLLExport:
1388 case Attr::DLLImport:
1389 case Attr::Deprecated:
1390 break;
1391
1392 case Attr::Destructor:
1393 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1394 break;
1395
1396 case Attr::FastCall:
1397 break;
1398
1399 case Attr::Format: {
1400 const FormatAttr *Format = cast<FormatAttr>(Attr);
1401 AddString(Format->getType(), Record);
1402 Record.push_back(Format->getFormatIdx());
1403 Record.push_back(Format->getFirstArg());
1404 break;
1405 }
1406
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001407 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001408 case Attr::IBOutletKind:
1409 case Attr::NoReturn:
1410 case Attr::NoThrow:
1411 case Attr::Nodebug:
1412 case Attr::Noinline:
1413 break;
1414
1415 case Attr::NonNull: {
1416 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1417 Record.push_back(NonNull->size());
1418 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1419 break;
1420 }
1421
1422 case Attr::ObjCException:
1423 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00001424 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00001425 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00001426 case Attr::Overloadable:
1427 break;
1428
1429 case Attr::Packed:
1430 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1431 break;
1432
1433 case Attr::Pure:
1434 break;
1435
1436 case Attr::Regparm:
1437 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1438 break;
1439
1440 case Attr::Section:
1441 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1442 break;
1443
1444 case Attr::StdCall:
1445 case Attr::TransparentUnion:
1446 case Attr::Unavailable:
1447 case Attr::Unused:
1448 case Attr::Used:
1449 break;
1450
1451 case Attr::Visibility:
1452 // FIXME: stable encoding
1453 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1454 break;
1455
1456 case Attr::WarnUnusedResult:
1457 case Attr::Weak:
1458 case Attr::WeakImport:
1459 break;
1460 }
1461 }
1462
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001463 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001464}
1465
1466void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1467 Record.push_back(Str.size());
1468 Record.insert(Record.end(), Str.begin(), Str.end());
1469}
1470
Douglas Gregorff9a6092009-04-20 20:36:09 +00001471/// \brief Note that the identifier II occurs at the given offset
1472/// within the identifier table.
1473void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001474 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001475}
1476
Douglas Gregor2d711832009-04-25 17:48:32 +00001477/// \brief Note that the selector Sel occurs at the given offset
1478/// within the method pool/selector table.
1479void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1480 unsigned ID = SelectorIDs[Sel];
1481 assert(ID && "Unknown selector");
1482 SelectorOffsets[ID - 1] = Offset;
1483}
1484
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001485PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001486 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00001487 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1488 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001489
Douglas Gregor87887da2009-04-20 15:53:59 +00001490void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00001491 using namespace llvm;
1492
Douglas Gregor87887da2009-04-20 15:53:59 +00001493 ASTContext &Context = SemaRef.Context;
1494 Preprocessor &PP = SemaRef.PP;
1495
Douglas Gregorc34897d2009-04-09 22:27:44 +00001496 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001497 Stream.Emit((unsigned)'C', 8);
1498 Stream.Emit((unsigned)'P', 8);
1499 Stream.Emit((unsigned)'C', 8);
1500 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00001501
1502 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001503
1504 // The translation unit is the first declaration we'll emit.
1505 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1506 DeclsToEmit.push(Context.getTranslationUnitDecl());
1507
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001508 // Make sure that we emit IdentifierInfos (and any attached
1509 // declarations) for builtins.
1510 {
1511 IdentifierTable &Table = PP.getIdentifierTable();
1512 llvm::SmallVector<const char *, 32> BuiltinNames;
1513 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1514 Context.getLangOptions().NoBuiltin);
1515 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1516 getIdentifierRef(&Table.get(BuiltinNames[I]));
1517 }
1518
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001519 // Build a record containing all of the tentative definitions in
1520 // this header file. Generally, this record will be empty.
1521 RecordData TentativeDefinitions;
1522 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
1523 TD = SemaRef.TentativeDefinitions.begin(),
1524 TDEnd = SemaRef.TentativeDefinitions.end();
1525 TD != TDEnd; ++TD)
1526 AddDeclRef(TD->second, TentativeDefinitions);
1527
Douglas Gregor062d9482009-04-22 22:18:58 +00001528 // Build a record containing all of the locally-scoped external
1529 // declarations in this header file. Generally, this record will be
1530 // empty.
1531 RecordData LocallyScopedExternalDecls;
1532 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1533 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1534 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1535 TD != TDEnd; ++TD)
1536 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1537
Douglas Gregorc34897d2009-04-09 22:27:44 +00001538 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001539 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00001540 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001541 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001542 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001543 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001544 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00001545
1546 // Keep writing types and declarations until all types and
1547 // declarations have been written.
1548 do {
1549 if (!DeclsToEmit.empty())
1550 WriteDeclsBlock(Context);
1551 if (!TypesToEmit.empty())
1552 WriteTypesBlock(Context);
1553 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1554
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001555 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001556 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00001557
1558 // Write the type offsets array
1559 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1560 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1563 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1564 Record.clear();
1565 Record.push_back(pch::TYPE_OFFSET);
1566 Record.push_back(TypeOffsets.size());
1567 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1568 (const char *)&TypeOffsets.front(),
1569 TypeOffsets.size() * sizeof(uint64_t));
1570
1571 // Write the declaration offsets array
1572 Abbrev = new BitCodeAbbrev();
1573 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1576 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1577 Record.clear();
1578 Record.push_back(pch::DECL_OFFSET);
1579 Record.push_back(DeclOffsets.size());
1580 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1581 (const char *)&DeclOffsets.front(),
1582 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00001583
1584 // Write the record of special types.
1585 Record.clear();
1586 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00001587 AddTypeRef(Context.getObjCIdType(), Record);
1588 AddTypeRef(Context.getObjCSelType(), Record);
1589 AddTypeRef(Context.getObjCProtoType(), Record);
1590 AddTypeRef(Context.getObjCClassType(), Record);
1591 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1592 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00001593 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1594
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001595 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00001596 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001597 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001598
1599 // Write the record containing tentative definitions.
1600 if (!TentativeDefinitions.empty())
1601 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00001602
1603 // Write the record containing locally-scoped external definitions.
1604 if (!LocallyScopedExternalDecls.empty())
1605 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
1606 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00001607
1608 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001609 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001610 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001611 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001612 Record.push_back(NumLexicalDeclContexts);
1613 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00001614 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001615 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001616}
1617
1618void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1619 Record.push_back(Loc.getRawEncoding());
1620}
1621
1622void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1623 Record.push_back(Value.getBitWidth());
1624 unsigned N = Value.getNumWords();
1625 const uint64_t* Words = Value.getRawData();
1626 for (unsigned I = 0; I != N; ++I)
1627 Record.push_back(Words[I]);
1628}
1629
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001630void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1631 Record.push_back(Value.isUnsigned());
1632 AddAPInt(Value, Record);
1633}
1634
Douglas Gregore2f37202009-04-14 21:55:33 +00001635void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1636 AddAPInt(Value.bitcastToAPInt(), Record);
1637}
1638
Douglas Gregorc34897d2009-04-09 22:27:44 +00001639void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001640 Record.push_back(getIdentifierRef(II));
1641}
1642
1643pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1644 if (II == 0)
1645 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001646
1647 pch::IdentID &ID = IdentifierIDs[II];
1648 if (ID == 0)
1649 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001650 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001651}
1652
Steve Naroff9e84d782009-04-23 10:39:46 +00001653void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1654 if (SelRef.getAsOpaquePtr() == 0) {
1655 Record.push_back(0);
1656 return;
1657 }
1658
1659 pch::SelectorID &SID = SelectorIDs[SelRef];
1660 if (SID == 0) {
1661 SID = SelectorIDs.size();
1662 SelVector.push_back(SelRef);
1663 }
1664 Record.push_back(SID);
1665}
1666
Douglas Gregorc34897d2009-04-09 22:27:44 +00001667void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1668 if (T.isNull()) {
1669 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1670 return;
1671 }
1672
1673 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001674 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001675 switch (BT->getKind()) {
1676 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1677 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1678 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1679 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1680 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1681 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1682 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1683 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1684 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1685 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1686 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1687 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1688 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1689 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1690 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1691 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1692 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1693 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1694 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1695 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1696 }
1697
1698 Record.push_back((ID << 3) | T.getCVRQualifiers());
1699 return;
1700 }
1701
Douglas Gregorac8f2802009-04-10 17:25:41 +00001702 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00001703 if (ID == 0) {
1704 // We haven't seen this type before. Assign it a new ID and put it
1705 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001706 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00001707 TypesToEmit.push(T.getTypePtr());
1708 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001709
1710 // Encode the type qualifiers in the type reference.
1711 Record.push_back((ID << 3) | T.getCVRQualifiers());
1712}
1713
1714void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1715 if (D == 0) {
1716 Record.push_back(0);
1717 return;
1718 }
1719
Douglas Gregorac8f2802009-04-10 17:25:41 +00001720 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001721 if (ID == 0) {
1722 // We haven't seen this declaration before. Give it a new ID and
1723 // enqueue it in the list of declarations to emit.
1724 ID = DeclIDs.size();
1725 DeclsToEmit.push(const_cast<Decl *>(D));
1726 }
1727
1728 Record.push_back(ID);
1729}
1730
Douglas Gregorff9a6092009-04-20 20:36:09 +00001731pch::DeclID PCHWriter::getDeclID(const Decl *D) {
1732 if (D == 0)
1733 return 0;
1734
1735 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
1736 return DeclIDs[D];
1737}
1738
Douglas Gregorc34897d2009-04-09 22:27:44 +00001739void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1740 Record.push_back(Name.getNameKind());
1741 switch (Name.getNameKind()) {
1742 case DeclarationName::Identifier:
1743 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1744 break;
1745
1746 case DeclarationName::ObjCZeroArgSelector:
1747 case DeclarationName::ObjCOneArgSelector:
1748 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00001749 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001750 break;
1751
1752 case DeclarationName::CXXConstructorName:
1753 case DeclarationName::CXXDestructorName:
1754 case DeclarationName::CXXConversionFunctionName:
1755 AddTypeRef(Name.getCXXNameType(), Record);
1756 break;
1757
1758 case DeclarationName::CXXOperatorName:
1759 Record.push_back(Name.getCXXOverloadedOperator());
1760 break;
1761
1762 case DeclarationName::CXXUsingDirective:
1763 // No extra data to emit
1764 break;
1765 }
1766}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001767