blob: 4b8847c09887ec9e42ed5a0d0415cd868a58057e [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000024#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorb7064742009-04-27 22:23:34 +000030#include "clang/Basic/Version.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Douglas Gregoreccf0d12009-05-12 01:31:05 +000036#include "llvm/System/Path.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
Chris Lattnerd83ede52009-04-27 06:16:06 +000043
Douglas Gregorc34897d2009-04-09 22:27:44 +000044namespace {
45 class VISIBILITY_HIDDEN PCHTypeWriter {
46 PCHWriter &Writer;
47 PCHWriter::RecordData &Record;
48
49 public:
50 /// \brief Type code that corresponds to the record generated.
51 pch::TypeCode Code;
52
53 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
Douglas Gregor6cc5d192009-04-27 18:38:38 +000054 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +000055
56 void VisitArrayType(const ArrayType *T);
57 void VisitFunctionType(const FunctionType *T);
58 void VisitTagType(const TagType *T);
59
60#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
61#define ABSTRACT_TYPE(Class, Base)
62#define DEPENDENT_TYPE(Class, Base)
63#include "clang/AST/TypeNodes.def"
64 };
65}
66
67void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
68 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
69 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
70 Record.push_back(T->getAddressSpace());
71 Code = pch::TYPE_EXT_QUAL;
72}
73
74void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
75 assert(false && "Built-in types are never serialized");
76}
77
78void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
79 Record.push_back(T->getWidth());
80 Record.push_back(T->isSigned());
81 Code = pch::TYPE_FIXED_WIDTH_INT;
82}
83
84void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
85 Writer.AddTypeRef(T->getElementType(), Record);
86 Code = pch::TYPE_COMPLEX;
87}
88
89void PCHTypeWriter::VisitPointerType(const PointerType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_POINTER;
92}
93
94void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_BLOCK_POINTER;
97}
98
99void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_LVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Code = pch::TYPE_RVALUE_REFERENCE;
107}
108
109void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
110 Writer.AddTypeRef(T->getPointeeType(), Record);
111 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
112 Code = pch::TYPE_MEMBER_POINTER;
113}
114
115void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
116 Writer.AddTypeRef(T->getElementType(), Record);
117 Record.push_back(T->getSizeModifier()); // FIXME: stable values
118 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
119}
120
121void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
122 VisitArrayType(T);
123 Writer.AddAPInt(T->getSize(), Record);
124 Code = pch::TYPE_CONSTANT_ARRAY;
125}
126
Douglas Gregor1d381132009-07-06 15:59:29 +0000127void PCHTypeWriter
128::VisitConstantArrayWithExprType(const ConstantArrayWithExprType *T) {
129 VisitArrayType(T);
130 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
131 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
132 Writer.AddAPInt(T->getSize(), Record);
133 Writer.AddStmt(T->getSizeExpr());
134 Code = pch::TYPE_CONSTANT_ARRAY_WITH_EXPR;
135}
136
137void PCHTypeWriter
138::VisitConstantArrayWithoutExprType(const ConstantArrayWithoutExprType *T) {
139 VisitArrayType(T);
140 Writer.AddAPInt(T->getSize(), Record);
141 Code = pch::TYPE_CONSTANT_ARRAY_WITHOUT_EXPR;
142}
143
Douglas Gregorc34897d2009-04-09 22:27:44 +0000144void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
145 VisitArrayType(T);
146 Code = pch::TYPE_INCOMPLETE_ARRAY;
147}
148
149void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
150 VisitArrayType(T);
Douglas Gregor1d381132009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000154 Code = pch::TYPE_VARIABLE_ARRAY;
155}
156
157void PCHTypeWriter::VisitVectorType(const VectorType *T) {
158 Writer.AddTypeRef(T->getElementType(), Record);
159 Record.push_back(T->getNumElements());
160 Code = pch::TYPE_VECTOR;
161}
162
163void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
164 VisitVectorType(T);
165 Code = pch::TYPE_EXT_VECTOR;
166}
167
168void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
169 Writer.AddTypeRef(T->getResultType(), Record);
170}
171
172void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
173 VisitFunctionType(T);
174 Code = pch::TYPE_FUNCTION_NO_PROTO;
175}
176
177void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
178 VisitFunctionType(T);
179 Record.push_back(T->getNumArgs());
180 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
181 Writer.AddTypeRef(T->getArgType(I), Record);
182 Record.push_back(T->isVariadic());
183 Record.push_back(T->getTypeQuals());
Sebastian Redl2767d882009-05-27 22:11:52 +0000184 Record.push_back(T->hasExceptionSpec());
185 Record.push_back(T->hasAnyExceptionSpec());
186 Record.push_back(T->getNumExceptions());
187 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
188 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000189 Code = pch::TYPE_FUNCTION_PROTO;
190}
191
192void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
193 Writer.AddDeclRef(T->getDecl(), Record);
194 Code = pch::TYPE_TYPEDEF;
195}
196
197void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000198 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000199 Code = pch::TYPE_TYPEOF_EXPR;
200}
201
202void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
203 Writer.AddTypeRef(T->getUnderlyingType(), Record);
204 Code = pch::TYPE_TYPEOF;
205}
206
Anders Carlsson93ab5332009-06-24 19:06:50 +0000207void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
208 Writer.AddStmt(T->getUnderlyingExpr());
209 Code = pch::TYPE_DECLTYPE;
210}
211
Douglas Gregorc34897d2009-04-09 22:27:44 +0000212void PCHTypeWriter::VisitTagType(const TagType *T) {
213 Writer.AddDeclRef(T->getDecl(), Record);
214 assert(!T->isBeingDefined() &&
215 "Cannot serialize in the middle of a type definition");
216}
217
218void PCHTypeWriter::VisitRecordType(const RecordType *T) {
219 VisitTagType(T);
220 Code = pch::TYPE_RECORD;
221}
222
223void PCHTypeWriter::VisitEnumType(const EnumType *T) {
224 VisitTagType(T);
225 Code = pch::TYPE_ENUM;
226}
227
228void
229PCHTypeWriter::VisitTemplateSpecializationType(
230 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000231 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000232 assert(false && "Cannot serialize template specialization types");
233}
234
235void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000236 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000237 assert(false && "Cannot serialize qualified name types");
238}
239
240void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
241 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000242 Record.push_back(T->getNumProtocols());
Steve Naroff83418522009-05-27 16:21:00 +0000243 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
244 E = T->qual_end(); I != E; ++I)
245 Writer.AddDeclRef(*I, Record);
Steve Naroff77763c52009-07-18 15:33:26 +0000246 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000247}
248
Steve Naroffc75c1a82009-06-17 22:40:22 +0000249void
250PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Steve Naroff329ec222009-07-10 23:34:53 +0000251 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000252 Record.push_back(T->getNumProtocols());
Steve Naroffc75c1a82009-06-17 22:40:22 +0000253 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff83418522009-05-27 16:21:00 +0000254 E = T->qual_end(); I != E; ++I)
255 Writer.AddDeclRef(*I, Record);
Steve Naroffc75c1a82009-06-17 22:40:22 +0000256 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000257}
258
Chris Lattner80f83c62009-04-22 05:57:30 +0000259//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000260// PCHWriter Implementation
261//===----------------------------------------------------------------------===//
262
Chris Lattner920673a2009-04-26 22:26:21 +0000263static void EmitBlockID(unsigned ID, const char *Name,
264 llvm::BitstreamWriter &Stream,
265 PCHWriter::RecordData &Record) {
266 Record.clear();
267 Record.push_back(ID);
268 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
269
270 // Emit the block name if present.
271 if (Name == 0 || Name[0] == 0) return;
272 Record.clear();
273 while (*Name)
274 Record.push_back(*Name++);
275 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
276}
277
278static void EmitRecordID(unsigned ID, const char *Name,
279 llvm::BitstreamWriter &Stream,
280 PCHWriter::RecordData &Record) {
281 Record.clear();
282 Record.push_back(ID);
283 while (*Name)
284 Record.push_back(*Name++);
285 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000286}
287
288static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
289 PCHWriter::RecordData &Record) {
290#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
291 RECORD(STMT_STOP);
292 RECORD(STMT_NULL_PTR);
293 RECORD(STMT_NULL);
294 RECORD(STMT_COMPOUND);
295 RECORD(STMT_CASE);
296 RECORD(STMT_DEFAULT);
297 RECORD(STMT_LABEL);
298 RECORD(STMT_IF);
299 RECORD(STMT_SWITCH);
300 RECORD(STMT_WHILE);
301 RECORD(STMT_DO);
302 RECORD(STMT_FOR);
303 RECORD(STMT_GOTO);
304 RECORD(STMT_INDIRECT_GOTO);
305 RECORD(STMT_CONTINUE);
306 RECORD(STMT_BREAK);
307 RECORD(STMT_RETURN);
308 RECORD(STMT_DECL);
309 RECORD(STMT_ASM);
310 RECORD(EXPR_PREDEFINED);
311 RECORD(EXPR_DECL_REF);
312 RECORD(EXPR_INTEGER_LITERAL);
313 RECORD(EXPR_FLOATING_LITERAL);
314 RECORD(EXPR_IMAGINARY_LITERAL);
315 RECORD(EXPR_STRING_LITERAL);
316 RECORD(EXPR_CHARACTER_LITERAL);
317 RECORD(EXPR_PAREN);
318 RECORD(EXPR_UNARY_OPERATOR);
319 RECORD(EXPR_SIZEOF_ALIGN_OF);
320 RECORD(EXPR_ARRAY_SUBSCRIPT);
321 RECORD(EXPR_CALL);
322 RECORD(EXPR_MEMBER);
323 RECORD(EXPR_BINARY_OPERATOR);
324 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
325 RECORD(EXPR_CONDITIONAL_OPERATOR);
326 RECORD(EXPR_IMPLICIT_CAST);
327 RECORD(EXPR_CSTYLE_CAST);
328 RECORD(EXPR_COMPOUND_LITERAL);
329 RECORD(EXPR_EXT_VECTOR_ELEMENT);
330 RECORD(EXPR_INIT_LIST);
331 RECORD(EXPR_DESIGNATED_INIT);
332 RECORD(EXPR_IMPLICIT_VALUE_INIT);
333 RECORD(EXPR_VA_ARG);
334 RECORD(EXPR_ADDR_LABEL);
335 RECORD(EXPR_STMT);
336 RECORD(EXPR_TYPES_COMPATIBLE);
337 RECORD(EXPR_CHOOSE);
338 RECORD(EXPR_GNU_NULL);
339 RECORD(EXPR_SHUFFLE_VECTOR);
340 RECORD(EXPR_BLOCK);
341 RECORD(EXPR_BLOCK_DECL_REF);
342 RECORD(EXPR_OBJC_STRING_LITERAL);
343 RECORD(EXPR_OBJC_ENCODE);
344 RECORD(EXPR_OBJC_SELECTOR_EXPR);
345 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
346 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
347 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
348 RECORD(EXPR_OBJC_KVC_REF_EXPR);
349 RECORD(EXPR_OBJC_MESSAGE_EXPR);
350 RECORD(EXPR_OBJC_SUPER_EXPR);
351 RECORD(STMT_OBJC_FOR_COLLECTION);
352 RECORD(STMT_OBJC_CATCH);
353 RECORD(STMT_OBJC_FINALLY);
354 RECORD(STMT_OBJC_AT_TRY);
355 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
356 RECORD(STMT_OBJC_AT_THROW);
357#undef RECORD
Chris Lattner920673a2009-04-26 22:26:21 +0000358}
359
360void PCHWriter::WriteBlockInfoBlock() {
361 RecordData Record;
362 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
363
Chris Lattner880f3f72009-04-27 00:40:25 +0000364#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner920673a2009-04-26 22:26:21 +0000365#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
366
367 // PCH Top-Level Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000368 BLOCK(PCH_BLOCK);
Zhongxing Xu29b61a52009-06-03 09:23:28 +0000369 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner920673a2009-04-26 22:26:21 +0000370 RECORD(TYPE_OFFSET);
371 RECORD(DECL_OFFSET);
372 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorb7064742009-04-27 22:23:34 +0000373 RECORD(METADATA);
Chris Lattner920673a2009-04-26 22:26:21 +0000374 RECORD(IDENTIFIER_OFFSET);
375 RECORD(IDENTIFIER_TABLE);
376 RECORD(EXTERNAL_DEFINITIONS);
377 RECORD(SPECIAL_TYPES);
378 RECORD(STATISTICS);
379 RECORD(TENTATIVE_DEFINITIONS);
380 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
381 RECORD(SELECTOR_OFFSETS);
382 RECORD(METHOD_POOL);
383 RECORD(PP_COUNTER_VALUE);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000384 RECORD(SOURCE_LOCATION_OFFSETS);
385 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000386 RECORD(STAT_CACHE);
Douglas Gregorb36b20d2009-04-27 20:06:05 +0000387 RECORD(EXT_VECTOR_DECLS);
Douglas Gregora252b232009-07-02 17:08:52 +0000388 RECORD(COMMENT_RANGES);
389
Chris Lattner920673a2009-04-26 22:26:21 +0000390 // SourceManager Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000391 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000392 RECORD(SM_SLOC_FILE_ENTRY);
393 RECORD(SM_SLOC_BUFFER_ENTRY);
394 RECORD(SM_SLOC_BUFFER_BLOB);
395 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
396 RECORD(SM_LINE_TABLE);
397 RECORD(SM_HEADER_FILE_INFO);
398
399 // Preprocessor Block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000400 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000401 RECORD(PP_MACRO_OBJECT_LIKE);
402 RECORD(PP_MACRO_FUNCTION_LIKE);
403 RECORD(PP_TOKEN);
404
405 // Types block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000406 BLOCK(TYPES_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +0000407 RECORD(TYPE_EXT_QUAL);
408 RECORD(TYPE_FIXED_WIDTH_INT);
409 RECORD(TYPE_COMPLEX);
410 RECORD(TYPE_POINTER);
411 RECORD(TYPE_BLOCK_POINTER);
412 RECORD(TYPE_LVALUE_REFERENCE);
413 RECORD(TYPE_RVALUE_REFERENCE);
414 RECORD(TYPE_MEMBER_POINTER);
415 RECORD(TYPE_CONSTANT_ARRAY);
416 RECORD(TYPE_INCOMPLETE_ARRAY);
417 RECORD(TYPE_VARIABLE_ARRAY);
418 RECORD(TYPE_VECTOR);
419 RECORD(TYPE_EXT_VECTOR);
420 RECORD(TYPE_FUNCTION_PROTO);
421 RECORD(TYPE_FUNCTION_NO_PROTO);
422 RECORD(TYPE_TYPEDEF);
423 RECORD(TYPE_TYPEOF_EXPR);
424 RECORD(TYPE_TYPEOF);
425 RECORD(TYPE_RECORD);
426 RECORD(TYPE_ENUM);
427 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffc75c1a82009-06-17 22:40:22 +0000428 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000429 // Statements and Exprs can occur in the Types block.
430 AddStmtsExprs(Stream, Record);
431
Chris Lattner920673a2009-04-26 22:26:21 +0000432 // Decls block.
Chris Lattner880f3f72009-04-27 00:40:25 +0000433 BLOCK(DECLS_BLOCK);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000434 RECORD(DECL_ATTR);
435 RECORD(DECL_TRANSLATION_UNIT);
436 RECORD(DECL_TYPEDEF);
437 RECORD(DECL_ENUM);
438 RECORD(DECL_RECORD);
439 RECORD(DECL_ENUM_CONSTANT);
440 RECORD(DECL_FUNCTION);
441 RECORD(DECL_OBJC_METHOD);
442 RECORD(DECL_OBJC_INTERFACE);
443 RECORD(DECL_OBJC_PROTOCOL);
444 RECORD(DECL_OBJC_IVAR);
445 RECORD(DECL_OBJC_AT_DEFS_FIELD);
446 RECORD(DECL_OBJC_CLASS);
447 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
448 RECORD(DECL_OBJC_CATEGORY);
449 RECORD(DECL_OBJC_CATEGORY_IMPL);
450 RECORD(DECL_OBJC_IMPLEMENTATION);
451 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
452 RECORD(DECL_OBJC_PROPERTY);
453 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +0000454 RECORD(DECL_FIELD);
455 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000456 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +0000457 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +0000458 RECORD(DECL_ORIGINAL_PARM_VAR);
459 RECORD(DECL_FILE_SCOPE_ASM);
460 RECORD(DECL_BLOCK);
461 RECORD(DECL_CONTEXT_LEXICAL);
462 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000463 // Statements and Exprs can occur in the Decls block.
464 AddStmtsExprs(Stream, Record);
Chris Lattner920673a2009-04-26 22:26:21 +0000465#undef RECORD
466#undef BLOCK
467 Stream.ExitBlock();
468}
469
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000470/// \brief Adjusts the given filename to only write out the portion of the
471/// filename that is not part of the system root directory.
472///
473/// \param Filename the file name to adjust.
474///
475/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
476/// the returned filename will be adjusted by this system root.
477///
478/// \returns either the original filename (if it needs no adjustment) or the
479/// adjusted filename (which points into the @p Filename parameter).
480static const char *
481adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
482 assert(Filename && "No file name to adjust?");
483
484 if (!isysroot)
485 return Filename;
486
487 // Verify that the filename and the system root have the same prefix.
488 unsigned Pos = 0;
489 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
490 if (Filename[Pos] != isysroot[Pos])
491 return Filename; // Prefixes don't match.
492
493 // We hit the end of the filename before we hit the end of the system root.
494 if (!Filename[Pos])
495 return Filename;
496
497 // If the file name has a '/' at the current position, skip over the '/'.
498 // We distinguish sysroot-based includes from absolute includes by the
499 // absence of '/' at the beginning of sysroot-based includes.
500 if (Filename[Pos] == '/')
501 ++Pos;
502
503 return Filename + Pos;
504}
Chris Lattner920673a2009-04-26 22:26:21 +0000505
Douglas Gregorb7064742009-04-27 22:23:34 +0000506/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000507void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorb5887f32009-04-10 21:16:55 +0000508 using namespace llvm;
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000509
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000510 // Metadata
511 const TargetInfo &Target = Context.Target;
512 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
513 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
514 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
515 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
516 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
517 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
518 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
519 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
520 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
521
522 RecordData Record;
523 Record.push_back(pch::METADATA);
524 Record.push_back(pch::VERSION_MAJOR);
525 Record.push_back(pch::VERSION_MINOR);
526 Record.push_back(CLANG_VERSION_MAJOR);
527 Record.push_back(CLANG_VERSION_MINOR);
528 Record.push_back(isysroot != 0);
Daniel Dunbar608b3882009-08-24 09:10:05 +0000529 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000530 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000531
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000532 // Original file name
533 SourceManager &SM = Context.getSourceManager();
534 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
535 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
536 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
537 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
538 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
539
540 llvm::sys::Path MainFilePath(MainFile->getName());
541 std::string MainFileName;
542
543 if (!MainFilePath.isAbsolute()) {
544 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner31bc3042009-08-23 22:45:33 +0000545 P.appendComponent(MainFilePath.str());
546 MainFileName = P.str();
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000547 } else {
Chris Lattner31bc3042009-08-23 22:45:33 +0000548 MainFileName = MainFilePath.str();
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000549 }
550
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000551 const char *MainFileNameStr = MainFileName.c_str();
552 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
553 isysroot);
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000554 RecordData Record;
555 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000556 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregoreccf0d12009-05-12 01:31:05 +0000557 }
Douglas Gregorb5887f32009-04-10 21:16:55 +0000558}
559
560/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000561void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
562 RecordData Record;
563 Record.push_back(LangOpts.Trigraphs);
564 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
565 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
566 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
567 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
568 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
569 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
570 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
571 Record.push_back(LangOpts.C99); // C99 Support
572 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
573 Record.push_back(LangOpts.CPlusPlus); // C++ Support
574 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor179cfb12009-04-10 20:39:37 +0000575 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
576
577 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
578 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
579 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
580
581 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor179cfb12009-04-10 20:39:37 +0000582 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
583 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begeman909e06e2009-06-25 23:01:11 +0000584 Record.push_back(LangOpts.AltiVec);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000585 Record.push_back(LangOpts.Exceptions); // Support exception handling.
586
587 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
588 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
589 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
590
Chris Lattnercd4da472009-04-27 07:35:58 +0000591 // Whether static initializers are protected by locks.
592 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor78a63db2009-09-03 14:36:33 +0000593 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000594 Record.push_back(LangOpts.Blocks); // block extension to C
595 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
596 // they are unused.
597 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
598 // (modulo the platform support).
599
600 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
601 // signed integer arithmetic overflows.
602
603 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
604 // may be ripped out at any time.
605
606 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
607 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
608 // defined.
609 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
610 // opposed to __DYNAMIC__).
611 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
612
613 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
614 // used (instead of C99 semantics).
615 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssonf2310142009-05-13 19:49:53 +0000616 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
617 // be enabled.
Eli Friedmand9389be2009-06-05 07:05:05 +0000618 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
619 // unsigned type
Douglas Gregor179cfb12009-04-10 20:39:37 +0000620 Record.push_back(LangOpts.getGCMode());
621 Record.push_back(LangOpts.getVisibilityMode());
622 Record.push_back(LangOpts.InstantiationDepth);
Nate Begeman909e06e2009-06-25 23:01:11 +0000623 Record.push_back(LangOpts.OpenCL);
Anders Carlsson9a0c2a52009-08-22 22:30:33 +0000624 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000625 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +0000626}
627
Douglas Gregorab1cef72009-04-10 03:52:48 +0000628//===----------------------------------------------------------------------===//
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000629// stat cache Serialization
630//===----------------------------------------------------------------------===//
631
632namespace {
633// Trait used for the on-disk hash table of stat cache results.
634class VISIBILITY_HIDDEN PCHStatCacheTrait {
635public:
636 typedef const char * key_type;
637 typedef key_type key_type_ref;
638
639 typedef std::pair<int, struct stat> data_type;
640 typedef const data_type& data_type_ref;
641
642 static unsigned ComputeHash(const char *path) {
643 return BernsteinHash(path);
644 }
645
646 std::pair<unsigned,unsigned>
647 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
648 data_type_ref Data) {
649 unsigned StrLen = strlen(path);
650 clang::io::Emit16(Out, StrLen);
651 unsigned DataLen = 1; // result value
652 if (Data.first == 0)
653 DataLen += 4 + 4 + 2 + 8 + 8;
654 clang::io::Emit8(Out, DataLen);
655 return std::make_pair(StrLen + 1, DataLen);
656 }
657
658 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
659 Out.write(path, KeyLen);
660 }
661
662 void EmitData(llvm::raw_ostream& Out, key_type_ref,
663 data_type_ref Data, unsigned DataLen) {
664 using namespace clang::io;
665 uint64_t Start = Out.tell(); (void)Start;
666
667 // Result of stat()
668 Emit8(Out, Data.first? 1 : 0);
669
670 if (Data.first == 0) {
671 Emit32(Out, (uint32_t) Data.second.st_ino);
672 Emit32(Out, (uint32_t) Data.second.st_dev);
673 Emit16(Out, (uint16_t) Data.second.st_mode);
674 Emit64(Out, (uint64_t) Data.second.st_mtime);
675 Emit64(Out, (uint64_t) Data.second.st_size);
676 }
677
678 assert(Out.tell() - Start == DataLen && "Wrong data length");
679 }
680};
681} // end anonymous namespace
682
683/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000684void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
685 const char *isysroot) {
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000686 // Build the on-disk hash table containing information about every
687 // stat() call.
688 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
689 unsigned NumStatEntries = 0;
690 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
691 StatEnd = StatCalls.end();
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000692 Stat != StatEnd; ++Stat, ++NumStatEntries) {
693 const char *Filename = Stat->first();
694 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
695 Generator.insert(Filename, Stat->second);
696 }
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000697
698 // Create the on-disk hash table in a buffer.
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000699 llvm::SmallString<4096> StatCacheData;
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000700 uint32_t BucketOffset;
701 {
702 llvm::raw_svector_ostream Out(StatCacheData);
703 // Make sure that no bucket is at offset 0
704 clang::io::Emit32(Out, 0);
705 BucketOffset = Generator.Emit(Out);
706 }
707
708 // Create a blob abbreviation
709 using namespace llvm;
710 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
711 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
715 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
716
717 // Write the stat cache
718 RecordData Record;
719 Record.push_back(pch::STAT_CACHE);
720 Record.push_back(BucketOffset);
721 Record.push_back(NumStatEntries);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000722 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000723}
724
725//===----------------------------------------------------------------------===//
Douglas Gregorab1cef72009-04-10 03:52:48 +0000726// Source Manager Serialization
727//===----------------------------------------------------------------------===//
728
729/// \brief Create an abbreviation for the SLocEntry that refers to a
730/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000731static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000732 using namespace llvm;
733 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
734 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +0000739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000740 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000741}
742
743/// \brief Create an abbreviation for the SLocEntry that refers to a
744/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000745static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000746 using namespace llvm;
747 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
748 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000754 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000755}
756
757/// \brief Create an abbreviation for the SLocEntry that refers to a
758/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000759static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000760 using namespace llvm;
761 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
762 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000764 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000765}
766
767/// \brief Create an abbreviation for the SLocEntry that refers to an
768/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000769static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000770 using namespace llvm;
771 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
772 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +0000777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000778 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000779}
780
781/// \brief Writes the block containing the serialized form of the
782/// source manager.
783///
784/// TODO: We should probably use an on-disk hash table (stored in a
785/// blob), indexed based on the file name, so that we only create
786/// entries for files that we actually need. In the common case (no
787/// errors), we probably won't have to create file entries for any of
788/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000789void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000790 const Preprocessor &PP,
791 const char *isysroot) {
Douglas Gregor32e231c2009-04-27 06:38:32 +0000792 RecordData Record;
793
Chris Lattner84b04f12009-04-10 17:16:57 +0000794 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000795 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000796
797 // Abbreviations for the various kinds of source-location entries.
Chris Lattnereb559a62009-04-27 19:03:22 +0000798 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
799 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
800 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
801 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000802
Douglas Gregor635f97f2009-04-13 16:31:14 +0000803 // Write the line table.
804 if (SourceMgr.hasLineTable()) {
805 LineTableInfo &LineTable = SourceMgr.getLineTable();
806
807 // Emit the file names
808 Record.push_back(LineTable.getNumFilenames());
809 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
810 // Emit the file name
811 const char *Filename = LineTable.getFilename(I);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000812 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000813 unsigned FilenameLen = Filename? strlen(Filename) : 0;
814 Record.push_back(FilenameLen);
815 if (FilenameLen)
816 Record.insert(Record.end(), Filename, Filename + FilenameLen);
817 }
818
819 // Emit the line entries
820 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
821 L != LEnd; ++L) {
822 // Emit the file ID
823 Record.push_back(L->first);
824
825 // Emit the line entries
826 Record.push_back(L->second.size());
827 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
828 LEEnd = L->second.end();
829 LE != LEEnd; ++LE) {
830 Record.push_back(LE->FileOffset);
831 Record.push_back(LE->LineNo);
832 Record.push_back(LE->FilenameID);
833 Record.push_back((unsigned)LE->FileKind);
834 Record.push_back(LE->IncludeOffset);
835 }
Douglas Gregor635f97f2009-04-13 16:31:14 +0000836 }
Zhongxing Xu01838482009-05-22 08:38:27 +0000837 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +0000838 }
839
Douglas Gregor32e231c2009-04-27 06:38:32 +0000840 // Write out entries for all of the header files we know about.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000841 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000842 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000843 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
844 E = HS.header_file_end();
845 I != E; ++I) {
846 Record.push_back(I->isImport);
847 Record.push_back(I->DirInfo);
848 Record.push_back(I->NumIncludes);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000849 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000850 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
851 Record.clear();
852 }
853
Douglas Gregor32e231c2009-04-27 06:38:32 +0000854 // Write out the source location entry table. We skip the first
855 // entry, which is always the same dummy entry.
Chris Lattner93307da2009-04-27 19:01:47 +0000856 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor32e231c2009-04-27 06:38:32 +0000857 RecordData PreloadSLocs;
858 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
859 for (SourceManager::sloc_entry_iterator
860 SLoc = SourceMgr.sloc_entry_begin() + 1,
861 SLocEnd = SourceMgr.sloc_entry_end();
862 SLoc != SLocEnd; ++SLoc) {
863 // Record the offset of this source-location entry.
864 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
865
866 // Figure out which record code to use.
867 unsigned Code;
868 if (SLoc->isFile()) {
869 if (SLoc->getFile().getContentCache()->Entry)
870 Code = pch::SM_SLOC_FILE_ENTRY;
871 else
872 Code = pch::SM_SLOC_BUFFER_ENTRY;
873 } else
874 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
875 Record.clear();
876 Record.push_back(Code);
877
878 Record.push_back(SLoc->getOffset());
879 if (SLoc->isFile()) {
880 const SrcMgr::FileInfo &File = SLoc->getFile();
881 Record.push_back(File.getIncludeLoc().getRawEncoding());
882 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
883 Record.push_back(File.hasLineDirectives());
884
885 const SrcMgr::ContentCache *Content = File.getContentCache();
886 if (Content->Entry) {
887 // The source location entry is a file. The blob associated
888 // with this entry is the file name.
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000889
890 // Turn the file name into an absolute path, if it isn't already.
891 const char *Filename = Content->Entry->getName();
892 llvm::sys::Path FilePath(Filename, strlen(Filename));
893 std::string FilenameStr;
894 if (!FilePath.isAbsolute()) {
895 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner31bc3042009-08-23 22:45:33 +0000896 P.appendComponent(FilePath.str());
897 FilenameStr = P.str();
Douglas Gregor3ee12ae2009-07-07 00:12:59 +0000898 Filename = FilenameStr.c_str();
899 }
900
901 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000902 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor32e231c2009-04-27 06:38:32 +0000903
904 // FIXME: For now, preload all file source locations, so that
905 // we get the appropriate File entries in the reader. This is
906 // a temporary measure.
907 PreloadSLocs.push_back(SLocEntryOffsets.size());
908 } else {
909 // The source location entry is a buffer. The blob associated
910 // with this entry contains the contents of the buffer.
911
912 // We add one to the size so that we capture the trailing NULL
913 // that is required by llvm::MemoryBuffer::getMemBuffer (on
914 // the reader side).
915 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
916 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000917 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
918 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000919 Record.clear();
920 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
921 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar01cc32c2009-08-24 09:31:37 +0000922 llvm::StringRef(Buffer->getBufferStart(),
923 Buffer->getBufferSize() + 1));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000924
925 if (strcmp(Name, "<built-in>") == 0)
926 PreloadSLocs.push_back(SLocEntryOffsets.size());
927 }
928 } else {
929 // The source location entry is an instantiation.
930 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
931 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
932 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
933 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
934
935 // Compute the token length for this macro expansion.
936 unsigned NextOffset = SourceMgr.getNextOffset();
937 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
938 if (++NextSLoc != SLocEnd)
939 NextOffset = NextSLoc->getOffset();
940 Record.push_back(NextOffset - SLoc->getOffset() - 1);
941 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
942 }
943 }
944
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000945 Stream.ExitBlock();
Douglas Gregor32e231c2009-04-27 06:38:32 +0000946
947 if (SLocEntryOffsets.empty())
948 return;
949
950 // Write the source-location offsets table into the PCH block. This
951 // table is used for lazily loading source-location information.
952 using namespace llvm;
953 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
954 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
958 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
959
960 Record.clear();
961 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
962 Record.push_back(SLocEntryOffsets.size());
963 Record.push_back(SourceMgr.getNextOffset());
964 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
965 (const char *)&SLocEntryOffsets.front(),
Chris Lattner93307da2009-04-27 19:01:47 +0000966 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor32e231c2009-04-27 06:38:32 +0000967
968 // Write the source location entry preloads array, telling the PCH
969 // reader which source locations entries it should load eagerly.
970 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000971}
972
Douglas Gregor6cc5d192009-04-27 18:38:38 +0000973//===----------------------------------------------------------------------===//
974// Preprocessor Serialization
975//===----------------------------------------------------------------------===//
976
Chris Lattnerffc05ed2009-04-10 17:15:23 +0000977/// \brief Writes the block containing the serialized form of the
978/// preprocessor.
979///
Chris Lattner850eabd2009-04-10 18:08:30 +0000980void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +0000981 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +0000982
Chris Lattner4b21c202009-04-13 01:29:17 +0000983 // If the preprocessor __COUNTER__ value has been bumped, remember it.
984 if (PP.getCounterValue() != 0) {
985 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000986 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +0000987 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000988 }
989
990 // Enter the preprocessor block.
991 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +0000992
Douglas Gregorf6e1fb22009-04-26 00:07:37 +0000993 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
994 // FIXME: use diagnostics subsystem for localization etc.
995 if (PP.SawDateOrTime())
996 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
997
Chris Lattner1b094952009-04-10 18:00:12 +0000998 // Loop over all the macro definitions that are live at the end of the file,
999 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001000 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1001 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001002 // FIXME: This emits macros in hash table order, we should do it in a stable
1003 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001004 MacroInfo *MI = I->second;
1005
1006 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1007 // been redefined by the header (in which case they are not isBuiltinMacro).
1008 if (MI->isBuiltinMacro())
1009 continue;
1010
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001011 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001012 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001013 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001014 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1015 Record.push_back(MI->isUsed());
1016
1017 unsigned Code;
1018 if (MI->isObjectLike()) {
1019 Code = pch::PP_MACRO_OBJECT_LIKE;
1020 } else {
1021 Code = pch::PP_MACRO_FUNCTION_LIKE;
1022
1023 Record.push_back(MI->isC99Varargs());
1024 Record.push_back(MI->isGNUVarargs());
1025 Record.push_back(MI->getNumArgs());
1026 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1027 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001028 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001029 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001030 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001031 Record.clear();
1032
Chris Lattner850eabd2009-04-10 18:08:30 +00001033 // Emit the tokens array.
1034 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1035 // Note that we know that the preprocessor does not have any annotation
1036 // tokens in it because they are created by the parser, and thus can't be
1037 // in a macro definition.
1038 const Token &Tok = MI->getReplacementToken(TokNo);
1039
1040 Record.push_back(Tok.getLocation().getRawEncoding());
1041 Record.push_back(Tok.getLength());
1042
Chris Lattner850eabd2009-04-10 18:08:30 +00001043 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1044 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001045 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001046
1047 // FIXME: Should translate token kind to a stable encoding.
1048 Record.push_back(Tok.getKind());
1049 // FIXME: Should translate token flags to a stable encoding.
1050 Record.push_back(Tok.getFlags());
1051
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001052 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001053 Record.clear();
1054 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001055 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001056 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001057 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001058}
1059
Douglas Gregora252b232009-07-02 17:08:52 +00001060void PCHWriter::WriteComments(ASTContext &Context) {
1061 using namespace llvm;
1062
1063 if (Context.Comments.empty())
1064 return;
1065
1066 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1067 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1068 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1069 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
1070
1071 RecordData Record;
1072 Record.push_back(pch::COMMENT_RANGES);
1073 Stream.EmitRecordWithBlob(CommentCode, Record,
1074 (const char*)&Context.Comments[0],
1075 Context.Comments.size() * sizeof(SourceRange));
1076}
1077
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001078//===----------------------------------------------------------------------===//
1079// Type Serialization
1080//===----------------------------------------------------------------------===//
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001081
Douglas Gregorc34897d2009-04-09 22:27:44 +00001082/// \brief Write the representation of a type to the PCH stream.
1083void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001084 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001085 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001086 ID = NextTypeID++;
1087
1088 // Record the offset for this type.
1089 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001090 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001091 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1092 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001093 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001094 }
1095
1096 RecordData Record;
1097
1098 // Emit the type's representation.
1099 PCHTypeWriter W(*this, Record);
1100 switch (T->getTypeClass()) {
1101 // For all of the concrete, non-dependent types, call the
1102 // appropriate visitor function.
1103#define TYPE(Class, Base) \
1104 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1105#define ABSTRACT_TYPE(Class, Base)
1106#define DEPENDENT_TYPE(Class, Base)
1107#include "clang/AST/TypeNodes.def"
1108
1109 // For all of the dependent type nodes (which only occur in C++
1110 // templates), produce an error.
1111#define TYPE(Class, Base)
1112#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1113#include "clang/AST/TypeNodes.def"
1114 assert(false && "Cannot serialize dependent type nodes");
1115 break;
1116 }
1117
1118 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001119 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001120
1121 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001122 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001123}
1124
1125/// \brief Write a block containing all of the types.
1126void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001127 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001128 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001129
Douglas Gregore43f0972009-04-26 03:49:13 +00001130 // Emit all of the types that need to be emitted (so far).
1131 while (!TypesToEmit.empty()) {
1132 const Type *T = TypesToEmit.front();
1133 TypesToEmit.pop();
1134 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1135 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001136 }
1137
1138 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001139 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001140}
1141
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001142//===----------------------------------------------------------------------===//
1143// Declaration Serialization
1144//===----------------------------------------------------------------------===//
1145
Douglas Gregorc34897d2009-04-09 22:27:44 +00001146/// \brief Write the block containing all of the declaration IDs
1147/// lexically declared within the given DeclContext.
1148///
1149/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1150/// bistream, or 0 if no block was written.
1151uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1152 DeclContext *DC) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001153 if (DC->decls_empty())
Douglas Gregorc34897d2009-04-09 22:27:44 +00001154 return 0;
1155
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001156 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001157 RecordData Record;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001158 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1159 D != DEnd; ++D)
Douglas Gregorc34897d2009-04-09 22:27:44 +00001160 AddDeclRef(*D, Record);
1161
Douglas Gregoraf136d92009-04-22 22:34:57 +00001162 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001163 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001164 return Offset;
1165}
1166
1167/// \brief Write the block containing all of the declaration IDs
1168/// visible from the given DeclContext.
1169///
1170/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1171/// bistream, or 0 if no block was written.
1172uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1173 DeclContext *DC) {
1174 if (DC->getPrimaryContext() != DC)
1175 return 0;
1176
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001177 // Since there is no name lookup into functions or methods, and we
1178 // perform name lookup for the translation unit via the
1179 // IdentifierInfo chains, don't bother to build a
1180 // visible-declarations table for these entities.
1181 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001182 return 0;
1183
Douglas Gregorc34897d2009-04-09 22:27:44 +00001184 // Force the DeclContext to build a its name-lookup table.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001185 DC->lookup(DeclarationName());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001186
1187 // Serialize the contents of the mapping used for lookup. Note that,
1188 // although we have two very different code paths, the serialized
1189 // representation is the same for both cases: a declaration name,
1190 // followed by a size, followed by references to the visible
1191 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001192 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001193 RecordData Record;
1194 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001195 if (!Map)
1196 return 0;
1197
Douglas Gregorc34897d2009-04-09 22:27:44 +00001198 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1199 D != DEnd; ++D) {
1200 AddDeclarationName(D->first, Record);
1201 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1202 Record.push_back(Result.second - Result.first);
1203 for(; Result.first != Result.second; ++Result.first)
1204 AddDeclRef(*Result.first, Record);
1205 }
1206
1207 if (Record.size() == 0)
1208 return 0;
1209
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001210 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001211 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001212 return Offset;
1213}
1214
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001215//===----------------------------------------------------------------------===//
1216// Global Method Pool and Selector Serialization
1217//===----------------------------------------------------------------------===//
1218
Douglas Gregorff9a6092009-04-20 20:36:09 +00001219namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001220// Trait used for the on-disk hash table used in the method pool.
1221class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1222 PCHWriter &Writer;
1223
1224public:
1225 typedef Selector key_type;
1226 typedef key_type key_type_ref;
1227
1228 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1229 typedef const data_type& data_type_ref;
1230
1231 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1232
1233 static unsigned ComputeHash(Selector Sel) {
1234 unsigned N = Sel.getNumArgs();
1235 if (N == 0)
1236 ++N;
1237 unsigned R = 5381;
1238 for (unsigned I = 0; I != N; ++I)
1239 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1240 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1241 return R;
1242 }
1243
1244 std::pair<unsigned,unsigned>
1245 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1246 data_type_ref Methods) {
1247 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1248 clang::io::Emit16(Out, KeyLen);
1249 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1250 for (const ObjCMethodList *Method = &Methods.first; Method;
1251 Method = Method->Next)
1252 if (Method->Method)
1253 DataLen += 4;
1254 for (const ObjCMethodList *Method = &Methods.second; Method;
1255 Method = Method->Next)
1256 if (Method->Method)
1257 DataLen += 4;
1258 clang::io::Emit16(Out, DataLen);
1259 return std::make_pair(KeyLen, DataLen);
1260 }
1261
Douglas Gregor2d711832009-04-25 17:48:32 +00001262 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1263 uint64_t Start = Out.tell();
1264 assert((Start >> 32) == 0 && "Selector key offset too large");
1265 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001266 unsigned N = Sel.getNumArgs();
1267 clang::io::Emit16(Out, N);
1268 if (N == 0)
1269 N = 1;
1270 for (unsigned I = 0; I != N; ++I)
1271 clang::io::Emit32(Out,
1272 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1273 }
1274
1275 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001276 data_type_ref Methods, unsigned DataLen) {
1277 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001278 unsigned NumInstanceMethods = 0;
1279 for (const ObjCMethodList *Method = &Methods.first; Method;
1280 Method = Method->Next)
1281 if (Method->Method)
1282 ++NumInstanceMethods;
1283
1284 unsigned NumFactoryMethods = 0;
1285 for (const ObjCMethodList *Method = &Methods.second; Method;
1286 Method = Method->Next)
1287 if (Method->Method)
1288 ++NumFactoryMethods;
1289
1290 clang::io::Emit16(Out, NumInstanceMethods);
1291 clang::io::Emit16(Out, NumFactoryMethods);
1292 for (const ObjCMethodList *Method = &Methods.first; Method;
1293 Method = Method->Next)
1294 if (Method->Method)
1295 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001296 for (const ObjCMethodList *Method = &Methods.second; Method;
1297 Method = Method->Next)
1298 if (Method->Method)
1299 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001300
1301 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001302 }
1303};
1304} // end anonymous namespace
1305
1306/// \brief Write the method pool into the PCH file.
1307///
1308/// The method pool contains both instance and factory methods, stored
1309/// in an on-disk hash table indexed by the selector.
1310void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1311 using namespace llvm;
1312
1313 // Create and write out the blob that contains the instance and
1314 // factor method pools.
1315 bool Empty = true;
1316 {
1317 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1318
1319 // Create the on-disk hash table representation. Start by
1320 // iterating through the instance method pool.
1321 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001322 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001323 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1324 Instance = SemaRef.InstanceMethodPool.begin(),
1325 InstanceEnd = SemaRef.InstanceMethodPool.end();
1326 Instance != InstanceEnd; ++Instance) {
1327 // Check whether there is a factory method with the same
1328 // selector.
1329 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1330 = SemaRef.FactoryMethodPool.find(Instance->first);
1331
1332 if (Factory == SemaRef.FactoryMethodPool.end())
1333 Generator.insert(Instance->first,
1334 std::make_pair(Instance->second,
1335 ObjCMethodList()));
1336 else
1337 Generator.insert(Instance->first,
1338 std::make_pair(Instance->second, Factory->second));
1339
Douglas Gregor2d711832009-04-25 17:48:32 +00001340 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001341 Empty = false;
1342 }
1343
1344 // Now iterate through the factory method pool, to pick up any
1345 // selectors that weren't already in the instance method pool.
1346 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1347 Factory = SemaRef.FactoryMethodPool.begin(),
1348 FactoryEnd = SemaRef.FactoryMethodPool.end();
1349 Factory != FactoryEnd; ++Factory) {
1350 // Check whether there is an instance method with the same
1351 // selector. If so, there is no work to do here.
1352 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1353 = SemaRef.InstanceMethodPool.find(Factory->first);
1354
Douglas Gregor2d711832009-04-25 17:48:32 +00001355 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001356 Generator.insert(Factory->first,
1357 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001358 ++NumSelectorsInMethodPool;
1359 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001360
1361 Empty = false;
1362 }
1363
Douglas Gregor2d711832009-04-25 17:48:32 +00001364 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001365 return;
1366
1367 // Create the on-disk hash table in a buffer.
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001368 llvm::SmallString<4096> MethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001369 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001370 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001371 {
1372 PCHMethodPoolTrait Trait(*this);
1373 llvm::raw_svector_ostream Out(MethodPool);
1374 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001375 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001376 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001377
1378 // For every selector that we have seen but which was not
1379 // written into the hash table, write the selector itself and
1380 // record it's offset.
1381 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1382 if (SelectorOffsets[I] == 0)
1383 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001384 }
1385
1386 // Create a blob abbreviation
1387 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1388 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1392 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1393
Douglas Gregor2d711832009-04-25 17:48:32 +00001394 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001395 RecordData Record;
1396 Record.push_back(pch::METHOD_POOL);
1397 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001398 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001399 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor2d711832009-04-25 17:48:32 +00001400
1401 // Create a blob abbreviation for the selector table offsets.
1402 Abbrev = new BitCodeAbbrev();
1403 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1406 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1407
1408 // Write the selector offsets table.
1409 Record.clear();
1410 Record.push_back(pch::SELECTOR_OFFSETS);
1411 Record.push_back(SelectorOffsets.size());
1412 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1413 (const char *)&SelectorOffsets.front(),
1414 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001415 }
1416}
1417
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001418//===----------------------------------------------------------------------===//
1419// Identifier Table Serialization
1420//===----------------------------------------------------------------------===//
1421
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001422namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001423class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1424 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001425 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001426
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001427 /// \brief Determines whether this is an "interesting" identifier
1428 /// that needs a full IdentifierInfo structure written into the hash
1429 /// table.
1430 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1431 return II->isPoisoned() ||
1432 II->isExtensionToken() ||
1433 II->hasMacroDefinition() ||
1434 II->getObjCOrBuiltinID() ||
1435 II->getFETokenInfo<void>();
1436 }
1437
Douglas Gregorff9a6092009-04-20 20:36:09 +00001438public:
1439 typedef const IdentifierInfo* key_type;
1440 typedef key_type key_type_ref;
1441
1442 typedef pch::IdentID data_type;
1443 typedef data_type data_type_ref;
1444
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001445 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1446 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001447
1448 static unsigned ComputeHash(const IdentifierInfo* II) {
1449 return clang::BernsteinHash(II->getName());
1450 }
1451
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001452 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001453 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1454 pch::IdentID ID) {
1455 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001456 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1457 if (isInterestingIdentifier(II)) {
Douglas Gregor67d91172009-04-28 21:32:13 +00001458 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001459 if (II->hasMacroDefinition() &&
1460 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor67d91172009-04-28 21:32:13 +00001461 DataLen += 4;
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001462 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1463 DEnd = IdentifierResolver::end();
1464 D != DEnd; ++D)
1465 DataLen += sizeof(pch::DeclID);
1466 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001467 clang::io::Emit16(Out, DataLen);
Douglas Gregor68619772009-04-28 20:01:51 +00001468 // We emit the key length after the data length so that every
1469 // string is preceded by a 16-bit length. This matches the PTH
1470 // format for storing identifiers.
Douglas Gregor85c4a872009-04-25 21:04:17 +00001471 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001472 return std::make_pair(KeyLen, DataLen);
1473 }
1474
1475 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1476 unsigned KeyLen) {
1477 // Record the location of the key data. This is used when generating
1478 // the mapping from persistent IDs to strings.
1479 Writer.SetIdentifierOffset(II, Out.tell());
1480 Out.write(II->getName(), KeyLen);
1481 }
1482
1483 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1484 pch::IdentID ID, unsigned) {
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001485 if (!isInterestingIdentifier(II)) {
1486 clang::io::Emit32(Out, ID << 1);
1487 return;
1488 }
Douglas Gregor67d91172009-04-28 21:32:13 +00001489
Douglas Gregor2c09dad2009-04-28 21:18:29 +00001490 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001491 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001492 bool hasMacroDefinition =
1493 II->hasMacroDefinition() &&
1494 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor67d91172009-04-28 21:32:13 +00001495 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001496 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001497 Bits = (Bits << 1) | II->isExtensionToken();
1498 Bits = (Bits << 1) | II->isPoisoned();
1499 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregor67d91172009-04-28 21:32:13 +00001500 clang::io::Emit16(Out, Bits);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001501
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001502 if (hasMacroDefinition)
Douglas Gregor67d91172009-04-28 21:32:13 +00001503 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001504
Douglas Gregorc713da92009-04-21 22:25:48 +00001505 // Emit the declaration IDs in reverse order, because the
1506 // IdentifierResolver provides the declarations as they would be
1507 // visible (e.g., the function "stat" would come before the struct
1508 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1509 // adds declarations to the end of the list (so we need to see the
1510 // struct "status" before the function "status").
1511 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1512 IdentifierResolver::end());
1513 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1514 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001515 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001516 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001517 }
1518};
1519} // end anonymous namespace
1520
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001521/// \brief Write the identifier table into the PCH file.
1522///
1523/// The identifier table consists of a blob containing string data
1524/// (the actual identifiers themselves) and a separate "offsets" index
1525/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001526void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001527 using namespace llvm;
1528
1529 // Create and write out the blob that contains the identifier
1530 // strings.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001531 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001532 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1533
Douglas Gregor91137812009-04-28 20:33:11 +00001534 // Look for any identifiers that were named while processing the
1535 // headers, but are otherwise not needed. We add these to the hash
1536 // table to enable checking of the predefines buffer in the case
1537 // where the user adds new macro definitions when building the PCH
1538 // file.
1539 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1540 IDEnd = PP.getIdentifierTable().end();
1541 ID != IDEnd; ++ID)
1542 getIdentifierRef(ID->second);
1543
Douglas Gregorff9a6092009-04-20 20:36:09 +00001544 // Create the on-disk hash table representation.
Douglas Gregor91137812009-04-28 20:33:11 +00001545 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001546 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1547 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1548 ID != IDEnd; ++ID) {
1549 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor68619772009-04-28 20:01:51 +00001550 Generator.insert(ID->first, ID->second);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001551 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001552
Douglas Gregorff9a6092009-04-20 20:36:09 +00001553 // Create the on-disk hash table in a buffer.
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001554 llvm::SmallString<4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001555 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001556 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001557 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001558 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001559 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001560 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00001561 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001562 }
1563
1564 // Create a blob abbreviation
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1566 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001569 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001570
1571 // Write the identifier table
1572 RecordData Record;
1573 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00001574 Record.push_back(BucketOffset);
Daniel Dunbar01cc32c2009-08-24 09:31:37 +00001575 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001576 }
1577
1578 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001579 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1580 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1583 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1584
1585 RecordData Record;
1586 Record.push_back(pch::IDENTIFIER_OFFSET);
1587 Record.push_back(IdentifierOffsets.size());
1588 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1589 (const char *)&IdentifierOffsets.front(),
1590 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001591}
1592
Douglas Gregor6cc5d192009-04-27 18:38:38 +00001593//===----------------------------------------------------------------------===//
1594// General Serialization Routines
1595//===----------------------------------------------------------------------===//
1596
Douglas Gregor1c507882009-04-15 21:30:51 +00001597/// \brief Write a record containing the given attributes.
1598void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1599 RecordData Record;
1600 for (; Attr; Attr = Attr->getNext()) {
1601 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1602 Record.push_back(Attr->isInherited());
1603 switch (Attr->getKind()) {
1604 case Attr::Alias:
1605 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1606 break;
1607
1608 case Attr::Aligned:
1609 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1610 break;
1611
1612 case Attr::AlwaysInline:
1613 break;
1614
1615 case Attr::AnalyzerNoReturn:
1616 break;
1617
1618 case Attr::Annotate:
1619 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1620 break;
1621
1622 case Attr::AsmLabel:
1623 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1624 break;
1625
1626 case Attr::Blocks:
1627 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1628 break;
1629
1630 case Attr::Cleanup:
1631 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1632 break;
1633
1634 case Attr::Const:
1635 break;
1636
1637 case Attr::Constructor:
1638 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1639 break;
1640
1641 case Attr::DLLExport:
1642 case Attr::DLLImport:
1643 case Attr::Deprecated:
1644 break;
1645
1646 case Attr::Destructor:
1647 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1648 break;
1649
1650 case Attr::FastCall:
1651 break;
1652
1653 case Attr::Format: {
1654 const FormatAttr *Format = cast<FormatAttr>(Attr);
1655 AddString(Format->getType(), Record);
1656 Record.push_back(Format->getFormatIdx());
1657 Record.push_back(Format->getFirstArg());
1658 break;
1659 }
1660
Fariborz Jahanian306d7252009-05-20 17:41:43 +00001661 case Attr::FormatArg: {
1662 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1663 Record.push_back(Format->getFormatIdx());
1664 break;
1665 }
1666
Fariborz Jahanian180f3412009-05-13 18:09:35 +00001667 case Attr::Sentinel : {
1668 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1669 Record.push_back(Sentinel->getSentinel());
1670 Record.push_back(Sentinel->getNullPos());
1671 break;
1672 }
1673
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001674 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001675 case Attr::IBOutletKind:
Ryan Flynn31af0912009-08-09 20:07:29 +00001676 case Attr::Malloc:
Mike Stump9c71db12009-08-26 22:31:08 +00001677 case Attr::NoDebug:
Douglas Gregor1c507882009-04-15 21:30:51 +00001678 case Attr::NoReturn:
1679 case Attr::NoThrow:
Mike Stump9c71db12009-08-26 22:31:08 +00001680 case Attr::NoInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001681 break;
1682
1683 case Attr::NonNull: {
1684 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1685 Record.push_back(NonNull->size());
1686 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1687 break;
1688 }
1689
1690 case Attr::ObjCException:
1691 case Attr::ObjCNSObject:
Ted Kremenek13ddd1a2009-05-09 02:44:38 +00001692 case Attr::CFReturnsRetained:
1693 case Attr::NSReturnsRetained:
Douglas Gregor1c507882009-04-15 21:30:51 +00001694 case Attr::Overloadable:
1695 break;
1696
Anders Carlssonc915fa72009-08-08 18:23:56 +00001697 case Attr::PragmaPack:
1698 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor1c507882009-04-15 21:30:51 +00001699 break;
1700
Anders Carlssonc915fa72009-08-08 18:23:56 +00001701 case Attr::Packed:
1702 break;
1703
Douglas Gregor1c507882009-04-15 21:30:51 +00001704 case Attr::Pure:
1705 break;
1706
1707 case Attr::Regparm:
1708 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1709 break;
Nate Begeman60702162009-06-26 06:32:41 +00001710
1711 case Attr::ReqdWorkGroupSize:
1712 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1713 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1714 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1715 break;
Douglas Gregor1c507882009-04-15 21:30:51 +00001716
1717 case Attr::Section:
1718 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1719 break;
1720
1721 case Attr::StdCall:
1722 case Attr::TransparentUnion:
1723 case Attr::Unavailable:
1724 case Attr::Unused:
1725 case Attr::Used:
1726 break;
1727
1728 case Attr::Visibility:
1729 // FIXME: stable encoding
1730 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1731 break;
1732
1733 case Attr::WarnUnusedResult:
1734 case Attr::Weak:
1735 case Attr::WeakImport:
1736 break;
1737 }
1738 }
1739
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001740 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001741}
1742
1743void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1744 Record.push_back(Str.size());
1745 Record.insert(Record.end(), Str.begin(), Str.end());
1746}
1747
Douglas Gregorff9a6092009-04-20 20:36:09 +00001748/// \brief Note that the identifier II occurs at the given offset
1749/// within the identifier table.
1750void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001751 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001752}
1753
Douglas Gregor2d711832009-04-25 17:48:32 +00001754/// \brief Note that the selector Sel occurs at the given offset
1755/// within the method pool/selector table.
1756void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1757 unsigned ID = SelectorIDs[Sel];
1758 assert(ID && "Unknown selector");
1759 SelectorOffsets[ID - 1] = Offset;
1760}
1761
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001762PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001763 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00001764 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1765 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001766
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001767void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1768 const char *isysroot) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00001769 using namespace llvm;
1770
Douglas Gregor87887da2009-04-20 15:53:59 +00001771 ASTContext &Context = SemaRef.Context;
1772 Preprocessor &PP = SemaRef.PP;
1773
Douglas Gregorc34897d2009-04-09 22:27:44 +00001774 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001775 Stream.Emit((unsigned)'C', 8);
1776 Stream.Emit((unsigned)'P', 8);
1777 Stream.Emit((unsigned)'C', 8);
1778 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00001779
1780 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001781
1782 // The translation unit is the first declaration we'll emit.
1783 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1784 DeclsToEmit.push(Context.getTranslationUnitDecl());
1785
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001786 // Make sure that we emit IdentifierInfos (and any attached
1787 // declarations) for builtins.
1788 {
1789 IdentifierTable &Table = PP.getIdentifierTable();
1790 llvm::SmallVector<const char *, 32> BuiltinNames;
1791 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1792 Context.getLangOptions().NoBuiltin);
1793 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1794 getIdentifierRef(&Table.get(BuiltinNames[I]));
1795 }
1796
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001797 // Build a record containing all of the tentative definitions in
1798 // this header file. Generally, this record will be empty.
1799 RecordData TentativeDefinitions;
1800 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
1801 TD = SemaRef.TentativeDefinitions.begin(),
1802 TDEnd = SemaRef.TentativeDefinitions.end();
1803 TD != TDEnd; ++TD)
1804 AddDeclRef(TD->second, TentativeDefinitions);
1805
Douglas Gregor062d9482009-04-22 22:18:58 +00001806 // Build a record containing all of the locally-scoped external
1807 // declarations in this header file. Generally, this record will be
1808 // empty.
1809 RecordData LocallyScopedExternalDecls;
1810 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1811 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1812 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1813 TD != TDEnd; ++TD)
1814 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1815
Douglas Gregorb36b20d2009-04-27 20:06:05 +00001816 // Build a record containing all of the ext_vector declarations.
1817 RecordData ExtVectorDecls;
1818 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1819 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1820
Douglas Gregorc34897d2009-04-09 22:27:44 +00001821 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001822 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00001823 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001824 WriteMetadata(Context, isysroot);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001825 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor3ee12ae2009-07-07 00:12:59 +00001826 if (StatCalls && !isysroot)
1827 WriteStatCache(*StatCalls, isysroot);
1828 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001829 WritePreprocessor(PP);
Douglas Gregora252b232009-07-02 17:08:52 +00001830 WriteComments(Context);
Steve Naroff77763c52009-07-18 15:33:26 +00001831 // Write the record of special types.
1832 Record.clear();
1833
1834 AddTypeRef(Context.getBuiltinVaListType(), Record);
1835 AddTypeRef(Context.getObjCIdType(), Record);
1836 AddTypeRef(Context.getObjCSelType(), Record);
1837 AddTypeRef(Context.getObjCProtoType(), Record);
1838 AddTypeRef(Context.getObjCClassType(), Record);
1839 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1840 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1841 AddTypeRef(Context.getFILEType(), Record);
Mike Stumped2f9292009-07-28 02:25:19 +00001842 AddTypeRef(Context.getjmp_bufType(), Record);
1843 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregor90107992009-08-21 00:27:50 +00001844 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1845 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroff77763c52009-07-18 15:33:26 +00001846 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Douglas Gregora252b232009-07-02 17:08:52 +00001847
Douglas Gregore43f0972009-04-26 03:49:13 +00001848 // Keep writing types and declarations until all types and
1849 // declarations have been written.
1850 do {
1851 if (!DeclsToEmit.empty())
1852 WriteDeclsBlock(Context);
1853 if (!TypesToEmit.empty())
1854 WriteTypesBlock(Context);
1855 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1856
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001857 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001858 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00001859
1860 // Write the type offsets array
1861 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1862 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1865 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1866 Record.clear();
1867 Record.push_back(pch::TYPE_OFFSET);
1868 Record.push_back(TypeOffsets.size());
1869 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1870 (const char *)&TypeOffsets.front(),
Chris Lattnerea332f32009-04-27 18:24:17 +00001871 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Douglas Gregor24a224c2009-04-25 18:35:21 +00001872
1873 // Write the declaration offsets array
1874 Abbrev = new BitCodeAbbrev();
1875 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1878 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1879 Record.clear();
1880 Record.push_back(pch::DECL_OFFSET);
1881 Record.push_back(DeclOffsets.size());
1882 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1883 (const char *)&DeclOffsets.front(),
Chris Lattnerea332f32009-04-27 18:24:17 +00001884 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregore01ad442009-04-18 05:55:16 +00001885
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001886 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00001887 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001888 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001889
1890 // Write the record containing tentative definitions.
1891 if (!TentativeDefinitions.empty())
1892 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00001893
1894 // Write the record containing locally-scoped external definitions.
1895 if (!LocallyScopedExternalDecls.empty())
1896 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
1897 LocallyScopedExternalDecls);
Douglas Gregorb36b20d2009-04-27 20:06:05 +00001898
1899 // Write the record containing ext_vector type names.
1900 if (!ExtVectorDecls.empty())
1901 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00001902
1903 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001904 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001905 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001906 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001907 Record.push_back(NumLexicalDeclContexts);
1908 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00001909 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001910 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001911}
1912
1913void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1914 Record.push_back(Loc.getRawEncoding());
1915}
1916
1917void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1918 Record.push_back(Value.getBitWidth());
1919 unsigned N = Value.getNumWords();
1920 const uint64_t* Words = Value.getRawData();
1921 for (unsigned I = 0; I != N; ++I)
1922 Record.push_back(Words[I]);
1923}
1924
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001925void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1926 Record.push_back(Value.isUnsigned());
1927 AddAPInt(Value, Record);
1928}
1929
Douglas Gregore2f37202009-04-14 21:55:33 +00001930void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1931 AddAPInt(Value.bitcastToAPInt(), Record);
1932}
1933
Douglas Gregorc34897d2009-04-09 22:27:44 +00001934void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001935 Record.push_back(getIdentifierRef(II));
1936}
1937
1938pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1939 if (II == 0)
1940 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001941
1942 pch::IdentID &ID = IdentifierIDs[II];
1943 if (ID == 0)
1944 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001945 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001946}
1947
Steve Naroff9e84d782009-04-23 10:39:46 +00001948void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1949 if (SelRef.getAsOpaquePtr() == 0) {
1950 Record.push_back(0);
1951 return;
1952 }
1953
1954 pch::SelectorID &SID = SelectorIDs[SelRef];
1955 if (SID == 0) {
1956 SID = SelectorIDs.size();
1957 SelVector.push_back(SelRef);
1958 }
1959 Record.push_back(SID);
1960}
1961
Douglas Gregorc34897d2009-04-09 22:27:44 +00001962void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1963 if (T.isNull()) {
1964 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1965 return;
1966 }
1967
1968 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001969 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001970 switch (BT->getKind()) {
1971 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1972 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1973 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1974 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1975 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1976 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1977 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1978 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00001979 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001980 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1981 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1982 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1983 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1984 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1985 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1986 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00001987 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001988 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1989 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1990 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001991 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001992 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
1993 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001994 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1995 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroff7bffd372009-07-15 18:40:39 +00001996 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
1997 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlsson4a8498c2009-06-26 18:41:36 +00001998 case BuiltinType::UndeducedAuto:
1999 assert(0 && "Should not see undeduced auto here");
2000 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002001 }
2002
2003 Record.push_back((ID << 3) | T.getCVRQualifiers());
2004 return;
2005 }
2006
Douglas Gregorac8f2802009-04-10 17:25:41 +00002007 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002008 if (ID == 0) {
2009 // We haven't seen this type before. Assign it a new ID and put it
2010 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002011 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002012 TypesToEmit.push(T.getTypePtr());
2013 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002014
2015 // Encode the type qualifiers in the type reference.
2016 Record.push_back((ID << 3) | T.getCVRQualifiers());
2017}
2018
2019void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2020 if (D == 0) {
2021 Record.push_back(0);
2022 return;
2023 }
2024
Douglas Gregorac8f2802009-04-10 17:25:41 +00002025 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002026 if (ID == 0) {
2027 // We haven't seen this declaration before. Give it a new ID and
2028 // enqueue it in the list of declarations to emit.
2029 ID = DeclIDs.size();
2030 DeclsToEmit.push(const_cast<Decl *>(D));
2031 }
2032
2033 Record.push_back(ID);
2034}
2035
Douglas Gregorff9a6092009-04-20 20:36:09 +00002036pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2037 if (D == 0)
2038 return 0;
2039
2040 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2041 return DeclIDs[D];
2042}
2043
Douglas Gregorc34897d2009-04-09 22:27:44 +00002044void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnercd4da472009-04-27 07:35:58 +00002045 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002046 Record.push_back(Name.getNameKind());
2047 switch (Name.getNameKind()) {
2048 case DeclarationName::Identifier:
2049 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2050 break;
2051
2052 case DeclarationName::ObjCZeroArgSelector:
2053 case DeclarationName::ObjCOneArgSelector:
2054 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002055 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002056 break;
2057
2058 case DeclarationName::CXXConstructorName:
2059 case DeclarationName::CXXDestructorName:
2060 case DeclarationName::CXXConversionFunctionName:
2061 AddTypeRef(Name.getCXXNameType(), Record);
2062 break;
2063
2064 case DeclarationName::CXXOperatorName:
2065 Record.push_back(Name.getCXXOverloadedOperator());
2066 break;
2067
2068 case DeclarationName::CXXUsingDirective:
2069 // No extra data to emit
2070 break;
2071 }
2072}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002073