blob: 86a52fcf52c83d30ad18b7d2bcceca2be07c2c83 [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregore7785042009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregor3251ceb2009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000024#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000030#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000036#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000043
Douglas Gregor2cf26342009-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 Gregor4fed3f42009-04-27 18:38:38 +000054 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-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 Gregor7e7eb3d2009-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 Gregor2cf26342009-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 Gregor7e7eb3d2009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-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 Redl465226e2009-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 Gregor2cf26342009-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 Gregorc9490c02009-04-16 22:23:12 +0000198 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-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 Carlsson395b4752009-06-24 19:06:50 +0000207void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) {
208 Writer.AddStmt(T->getUnderlyingExpr());
209 Code = pch::TYPE_DECLTYPE;
210}
211
Douglas Gregor2cf26342009-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
John McCall7da24312009-09-05 00:15:47 +0000228void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
230 Record.push_back(T->getTagKind());
231 Code = pch::TYPE_ELABORATED;
232}
233
Douglas Gregor2cf26342009-04-09 22:27:44 +0000234void
235PCHTypeWriter::VisitTemplateSpecializationType(
236 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000237 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000238 assert(false && "Cannot serialize template specialization types");
239}
240
241void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000242 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243 assert(false && "Cannot serialize qualified name types");
244}
245
246void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
247 Writer.AddDeclRef(T->getDecl(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 Record.push_back(T->getNumProtocols());
Steve Naroff446ee4e2009-05-27 16:21:00 +0000249 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
250 E = T->qual_end(); I != E; ++I)
251 Writer.AddDeclRef(*I, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000252 Code = pch::TYPE_OBJC_INTERFACE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253}
254
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000255void
256PCHTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Steve Naroff14108da2009-07-10 23:34:53 +0000257 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 Record.push_back(T->getNumProtocols());
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000259 for (ObjCInterfaceType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000260 E = T->qual_end(); I != E; ++I)
261 Writer.AddDeclRef(*I, Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000262 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263}
264
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000265//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +0000266// PCHWriter Implementation
267//===----------------------------------------------------------------------===//
268
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000269static void EmitBlockID(unsigned ID, const char *Name,
270 llvm::BitstreamWriter &Stream,
271 PCHWriter::RecordData &Record) {
272 Record.clear();
273 Record.push_back(ID);
274 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
275
276 // Emit the block name if present.
277 if (Name == 0 || Name[0] == 0) return;
278 Record.clear();
279 while (*Name)
280 Record.push_back(*Name++);
281 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
282}
283
284static void EmitRecordID(unsigned ID, const char *Name,
285 llvm::BitstreamWriter &Stream,
286 PCHWriter::RecordData &Record) {
287 Record.clear();
288 Record.push_back(ID);
289 while (*Name)
290 Record.push_back(*Name++);
291 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000292}
293
294static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
295 PCHWriter::RecordData &Record) {
296#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
297 RECORD(STMT_STOP);
298 RECORD(STMT_NULL_PTR);
299 RECORD(STMT_NULL);
300 RECORD(STMT_COMPOUND);
301 RECORD(STMT_CASE);
302 RECORD(STMT_DEFAULT);
303 RECORD(STMT_LABEL);
304 RECORD(STMT_IF);
305 RECORD(STMT_SWITCH);
306 RECORD(STMT_WHILE);
307 RECORD(STMT_DO);
308 RECORD(STMT_FOR);
309 RECORD(STMT_GOTO);
310 RECORD(STMT_INDIRECT_GOTO);
311 RECORD(STMT_CONTINUE);
312 RECORD(STMT_BREAK);
313 RECORD(STMT_RETURN);
314 RECORD(STMT_DECL);
315 RECORD(STMT_ASM);
316 RECORD(EXPR_PREDEFINED);
317 RECORD(EXPR_DECL_REF);
318 RECORD(EXPR_INTEGER_LITERAL);
319 RECORD(EXPR_FLOATING_LITERAL);
320 RECORD(EXPR_IMAGINARY_LITERAL);
321 RECORD(EXPR_STRING_LITERAL);
322 RECORD(EXPR_CHARACTER_LITERAL);
323 RECORD(EXPR_PAREN);
324 RECORD(EXPR_UNARY_OPERATOR);
325 RECORD(EXPR_SIZEOF_ALIGN_OF);
326 RECORD(EXPR_ARRAY_SUBSCRIPT);
327 RECORD(EXPR_CALL);
328 RECORD(EXPR_MEMBER);
329 RECORD(EXPR_BINARY_OPERATOR);
330 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
331 RECORD(EXPR_CONDITIONAL_OPERATOR);
332 RECORD(EXPR_IMPLICIT_CAST);
333 RECORD(EXPR_CSTYLE_CAST);
334 RECORD(EXPR_COMPOUND_LITERAL);
335 RECORD(EXPR_EXT_VECTOR_ELEMENT);
336 RECORD(EXPR_INIT_LIST);
337 RECORD(EXPR_DESIGNATED_INIT);
338 RECORD(EXPR_IMPLICIT_VALUE_INIT);
339 RECORD(EXPR_VA_ARG);
340 RECORD(EXPR_ADDR_LABEL);
341 RECORD(EXPR_STMT);
342 RECORD(EXPR_TYPES_COMPATIBLE);
343 RECORD(EXPR_CHOOSE);
344 RECORD(EXPR_GNU_NULL);
345 RECORD(EXPR_SHUFFLE_VECTOR);
346 RECORD(EXPR_BLOCK);
347 RECORD(EXPR_BLOCK_DECL_REF);
348 RECORD(EXPR_OBJC_STRING_LITERAL);
349 RECORD(EXPR_OBJC_ENCODE);
350 RECORD(EXPR_OBJC_SELECTOR_EXPR);
351 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
352 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
353 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
354 RECORD(EXPR_OBJC_KVC_REF_EXPR);
355 RECORD(EXPR_OBJC_MESSAGE_EXPR);
356 RECORD(EXPR_OBJC_SUPER_EXPR);
357 RECORD(STMT_OBJC_FOR_COLLECTION);
358 RECORD(STMT_OBJC_CATCH);
359 RECORD(STMT_OBJC_FINALLY);
360 RECORD(STMT_OBJC_AT_TRY);
361 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
362 RECORD(STMT_OBJC_AT_THROW);
363#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000364}
365
366void PCHWriter::WriteBlockInfoBlock() {
367 RecordData Record;
368 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
369
Chris Lattner2f4efd12009-04-27 00:40:25 +0000370#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000371#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
372
373 // PCH Top-Level Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000374 BLOCK(PCH_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000375 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000376 RECORD(TYPE_OFFSET);
377 RECORD(DECL_OFFSET);
378 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000379 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000380 RECORD(IDENTIFIER_OFFSET);
381 RECORD(IDENTIFIER_TABLE);
382 RECORD(EXTERNAL_DEFINITIONS);
383 RECORD(SPECIAL_TYPES);
384 RECORD(STATISTICS);
385 RECORD(TENTATIVE_DEFINITIONS);
386 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
387 RECORD(SELECTOR_OFFSETS);
388 RECORD(METHOD_POOL);
389 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000390 RECORD(SOURCE_LOCATION_OFFSETS);
391 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000392 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000393 RECORD(EXT_VECTOR_DECLS);
Douglas Gregor2e222532009-07-02 17:08:52 +0000394 RECORD(COMMENT_RANGES);
395
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000396 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000397 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000398 RECORD(SM_SLOC_FILE_ENTRY);
399 RECORD(SM_SLOC_BUFFER_ENTRY);
400 RECORD(SM_SLOC_BUFFER_BLOB);
401 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
402 RECORD(SM_LINE_TABLE);
403 RECORD(SM_HEADER_FILE_INFO);
404
405 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000406 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000407 RECORD(PP_MACRO_OBJECT_LIKE);
408 RECORD(PP_MACRO_FUNCTION_LIKE);
409 RECORD(PP_TOKEN);
410
411 // Types block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000412 BLOCK(TYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000413 RECORD(TYPE_EXT_QUAL);
414 RECORD(TYPE_FIXED_WIDTH_INT);
415 RECORD(TYPE_COMPLEX);
416 RECORD(TYPE_POINTER);
417 RECORD(TYPE_BLOCK_POINTER);
418 RECORD(TYPE_LVALUE_REFERENCE);
419 RECORD(TYPE_RVALUE_REFERENCE);
420 RECORD(TYPE_MEMBER_POINTER);
421 RECORD(TYPE_CONSTANT_ARRAY);
422 RECORD(TYPE_INCOMPLETE_ARRAY);
423 RECORD(TYPE_VARIABLE_ARRAY);
424 RECORD(TYPE_VECTOR);
425 RECORD(TYPE_EXT_VECTOR);
426 RECORD(TYPE_FUNCTION_PROTO);
427 RECORD(TYPE_FUNCTION_NO_PROTO);
428 RECORD(TYPE_TYPEDEF);
429 RECORD(TYPE_TYPEOF_EXPR);
430 RECORD(TYPE_TYPEOF);
431 RECORD(TYPE_RECORD);
432 RECORD(TYPE_ENUM);
433 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000434 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0558df22009-04-27 00:49:53 +0000435 // Statements and Exprs can occur in the Types block.
436 AddStmtsExprs(Stream, Record);
437
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000438 // Decls block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000439 BLOCK(DECLS_BLOCK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000440 RECORD(DECL_ATTR);
441 RECORD(DECL_TRANSLATION_UNIT);
442 RECORD(DECL_TYPEDEF);
443 RECORD(DECL_ENUM);
444 RECORD(DECL_RECORD);
445 RECORD(DECL_ENUM_CONSTANT);
446 RECORD(DECL_FUNCTION);
447 RECORD(DECL_OBJC_METHOD);
448 RECORD(DECL_OBJC_INTERFACE);
449 RECORD(DECL_OBJC_PROTOCOL);
450 RECORD(DECL_OBJC_IVAR);
451 RECORD(DECL_OBJC_AT_DEFS_FIELD);
452 RECORD(DECL_OBJC_CLASS);
453 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
454 RECORD(DECL_OBJC_CATEGORY);
455 RECORD(DECL_OBJC_CATEGORY_IMPL);
456 RECORD(DECL_OBJC_IMPLEMENTATION);
457 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
458 RECORD(DECL_OBJC_PROPERTY);
459 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000460 RECORD(DECL_FIELD);
461 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000462 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000463 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000464 RECORD(DECL_ORIGINAL_PARM_VAR);
465 RECORD(DECL_FILE_SCOPE_ASM);
466 RECORD(DECL_BLOCK);
467 RECORD(DECL_CONTEXT_LEXICAL);
468 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattner0558df22009-04-27 00:49:53 +0000469 // Statements and Exprs can occur in the Decls block.
470 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000471#undef RECORD
472#undef BLOCK
473 Stream.ExitBlock();
474}
475
Douglas Gregore650c8c2009-07-07 00:12:59 +0000476/// \brief Adjusts the given filename to only write out the portion of the
477/// filename that is not part of the system root directory.
478///
479/// \param Filename the file name to adjust.
480///
481/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
482/// the returned filename will be adjusted by this system root.
483///
484/// \returns either the original filename (if it needs no adjustment) or the
485/// adjusted filename (which points into the @p Filename parameter).
486static const char *
487adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
488 assert(Filename && "No file name to adjust?");
489
490 if (!isysroot)
491 return Filename;
492
493 // Verify that the filename and the system root have the same prefix.
494 unsigned Pos = 0;
495 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
496 if (Filename[Pos] != isysroot[Pos])
497 return Filename; // Prefixes don't match.
498
499 // We hit the end of the filename before we hit the end of the system root.
500 if (!Filename[Pos])
501 return Filename;
502
503 // If the file name has a '/' at the current position, skip over the '/'.
504 // We distinguish sysroot-based includes from absolute includes by the
505 // absence of '/' at the beginning of sysroot-based includes.
506 if (Filename[Pos] == '/')
507 ++Pos;
508
509 return Filename + Pos;
510}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000511
Douglas Gregorab41e632009-04-27 22:23:34 +0000512/// \brief Write the PCH metadata (e.g., i686-apple-darwin9).
Douglas Gregore650c8c2009-07-07 00:12:59 +0000513void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000514 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000515
Douglas Gregore650c8c2009-07-07 00:12:59 +0000516 // Metadata
517 const TargetInfo &Target = Context.Target;
518 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
519 MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA));
520 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major
521 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor
522 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
523 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
524 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
525 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
526 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
527
528 RecordData Record;
529 Record.push_back(pch::METADATA);
530 Record.push_back(pch::VERSION_MAJOR);
531 Record.push_back(pch::VERSION_MINOR);
532 Record.push_back(CLANG_VERSION_MAJOR);
533 Record.push_back(CLANG_VERSION_MINOR);
534 Record.push_back(isysroot != 0);
Daniel Dunbar1752ee42009-08-24 09:10:05 +0000535 const std::string &TripleStr = Target.getTriple().getTriple();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000536 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000537
Douglas Gregorb64c1932009-05-12 01:31:05 +0000538 // Original file name
539 SourceManager &SM = Context.getSourceManager();
540 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
541 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
542 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
543 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
544 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
545
546 llvm::sys::Path MainFilePath(MainFile->getName());
547 std::string MainFileName;
548
549 if (!MainFilePath.isAbsolute()) {
550 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000551 P.appendComponent(MainFilePath.str());
552 MainFileName = P.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000553 } else {
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000554 MainFileName = MainFilePath.str();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000555 }
556
Douglas Gregore650c8c2009-07-07 00:12:59 +0000557 const char *MainFileNameStr = MainFileName.c_str();
558 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
559 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000560 RecordData Record;
561 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000562 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000563 }
Douglas Gregor2bec0412009-04-10 21:16:55 +0000564}
565
566/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000567void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
568 RecordData Record;
569 Record.push_back(LangOpts.Trigraphs);
570 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
571 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
572 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
573 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
574 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
575 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
576 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
577 Record.push_back(LangOpts.C99); // C99 Support
578 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
579 Record.push_back(LangOpts.CPlusPlus); // C++ Support
580 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000581 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
582
583 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
584 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
585 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
586
587 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000588 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
589 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000590 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000591 Record.push_back(LangOpts.Exceptions); // Support exception handling.
592
593 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
594 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
595 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
596
Chris Lattnerea5ce472009-04-27 07:35:58 +0000597 // Whether static initializers are protected by locks.
598 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000599 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000600 Record.push_back(LangOpts.Blocks); // block extension to C
601 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
602 // they are unused.
603 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
604 // (modulo the platform support).
605
606 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
607 // signed integer arithmetic overflows.
608
609 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
610 // may be ripped out at any time.
611
612 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
613 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
614 // defined.
615 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
616 // opposed to __DYNAMIC__).
617 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
618
619 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
620 // used (instead of C99 semantics).
621 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000622 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
623 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000624 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
625 // unsigned type
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000626 Record.push_back(LangOpts.getGCMode());
627 Record.push_back(LangOpts.getVisibilityMode());
628 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000629 Record.push_back(LangOpts.OpenCL);
Anders Carlsson92f58222009-08-22 22:30:33 +0000630 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000631 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000632}
633
Douglas Gregor14f79002009-04-10 03:52:48 +0000634//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000635// stat cache Serialization
636//===----------------------------------------------------------------------===//
637
638namespace {
639// Trait used for the on-disk hash table of stat cache results.
640class VISIBILITY_HIDDEN PCHStatCacheTrait {
641public:
642 typedef const char * key_type;
643 typedef key_type key_type_ref;
644
645 typedef std::pair<int, struct stat> data_type;
646 typedef const data_type& data_type_ref;
647
648 static unsigned ComputeHash(const char *path) {
649 return BernsteinHash(path);
650 }
651
652 std::pair<unsigned,unsigned>
653 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
654 data_type_ref Data) {
655 unsigned StrLen = strlen(path);
656 clang::io::Emit16(Out, StrLen);
657 unsigned DataLen = 1; // result value
658 if (Data.first == 0)
659 DataLen += 4 + 4 + 2 + 8 + 8;
660 clang::io::Emit8(Out, DataLen);
661 return std::make_pair(StrLen + 1, DataLen);
662 }
663
664 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
665 Out.write(path, KeyLen);
666 }
667
668 void EmitData(llvm::raw_ostream& Out, key_type_ref,
669 data_type_ref Data, unsigned DataLen) {
670 using namespace clang::io;
671 uint64_t Start = Out.tell(); (void)Start;
672
673 // Result of stat()
674 Emit8(Out, Data.first? 1 : 0);
675
676 if (Data.first == 0) {
677 Emit32(Out, (uint32_t) Data.second.st_ino);
678 Emit32(Out, (uint32_t) Data.second.st_dev);
679 Emit16(Out, (uint16_t) Data.second.st_mode);
680 Emit64(Out, (uint64_t) Data.second.st_mtime);
681 Emit64(Out, (uint64_t) Data.second.st_size);
682 }
683
684 assert(Out.tell() - Start == DataLen && "Wrong data length");
685 }
686};
687} // end anonymous namespace
688
689/// \brief Write the stat() system call cache to the PCH file.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000690void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls,
691 const char *isysroot) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000692 // Build the on-disk hash table containing information about every
693 // stat() call.
694 OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator;
695 unsigned NumStatEntries = 0;
696 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
697 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000698 Stat != StatEnd; ++Stat, ++NumStatEntries) {
699 const char *Filename = Stat->first();
700 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
701 Generator.insert(Filename, Stat->second);
702 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000703
704 // Create the on-disk hash table in a buffer.
Daniel Dunbarec312a12009-08-24 09:31:37 +0000705 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000706 uint32_t BucketOffset;
707 {
708 llvm::raw_svector_ostream Out(StatCacheData);
709 // Make sure that no bucket is at offset 0
710 clang::io::Emit32(Out, 0);
711 BucketOffset = Generator.Emit(Out);
712 }
713
714 // Create a blob abbreviation
715 using namespace llvm;
716 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
717 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
721 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
722
723 // Write the stat cache
724 RecordData Record;
725 Record.push_back(pch::STAT_CACHE);
726 Record.push_back(BucketOffset);
727 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000728 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000729}
730
731//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000732// Source Manager Serialization
733//===----------------------------------------------------------------------===//
734
735/// \brief Create an abbreviation for the SLocEntry that refers to a
736/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000737static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000738 using namespace llvm;
739 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
740 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +0000745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +0000746 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000747}
748
749/// \brief Create an abbreviation for the SLocEntry that refers to a
750/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000751static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000752 using namespace llvm;
753 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
754 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000760 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000761}
762
763/// \brief Create an abbreviation for the SLocEntry that refers to a
764/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000765static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000766 using namespace llvm;
767 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
768 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +0000770 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000771}
772
773/// \brief Create an abbreviation for the SLocEntry that refers to an
774/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000775static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000776 using namespace llvm;
777 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
778 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
780 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +0000783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +0000784 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +0000785}
786
787/// \brief Writes the block containing the serialized form of the
788/// source manager.
789///
790/// TODO: We should probably use an on-disk hash table (stored in a
791/// blob), indexed based on the file name, so that we only create
792/// entries for files that we actually need. In the common case (no
793/// errors), we probably won't have to create file entries for any of
794/// the files in the AST.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000795void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000796 const Preprocessor &PP,
797 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000798 RecordData Record;
799
Chris Lattnerf04ad692009-04-10 17:16:57 +0000800 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000801 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +0000802
803 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +0000804 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
805 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
806 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
807 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +0000808
Douglas Gregorbd945002009-04-13 16:31:14 +0000809 // Write the line table.
810 if (SourceMgr.hasLineTable()) {
811 LineTableInfo &LineTable = SourceMgr.getLineTable();
812
813 // Emit the file names
814 Record.push_back(LineTable.getNumFilenames());
815 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
816 // Emit the file name
817 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000818 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +0000819 unsigned FilenameLen = Filename? strlen(Filename) : 0;
820 Record.push_back(FilenameLen);
821 if (FilenameLen)
822 Record.insert(Record.end(), Filename, Filename + FilenameLen);
823 }
824
825 // Emit the line entries
826 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
827 L != LEnd; ++L) {
828 // Emit the file ID
829 Record.push_back(L->first);
830
831 // Emit the line entries
832 Record.push_back(L->second.size());
833 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
834 LEEnd = L->second.end();
835 LE != LEEnd; ++LE) {
836 Record.push_back(LE->FileOffset);
837 Record.push_back(LE->LineNo);
838 Record.push_back(LE->FilenameID);
839 Record.push_back((unsigned)LE->FileKind);
840 Record.push_back(LE->IncludeOffset);
841 }
Douglas Gregorbd945002009-04-13 16:31:14 +0000842 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +0000843 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +0000844 }
845
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000846 // Write out entries for all of the header files we know about.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000847 HeaderSearch &HS = PP.getHeaderSearchInfo();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000848 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000849 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
850 E = HS.header_file_end();
851 I != E; ++I) {
852 Record.push_back(I->isImport);
853 Record.push_back(I->DirInfo);
854 Record.push_back(I->NumIncludes);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000855 AddIdentifierRef(I->ControllingMacro, Record);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000856 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
857 Record.clear();
858 }
859
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000860 // Write out the source location entry table. We skip the first
861 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +0000862 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000863 RecordData PreloadSLocs;
864 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1);
865 for (SourceManager::sloc_entry_iterator
866 SLoc = SourceMgr.sloc_entry_begin() + 1,
867 SLocEnd = SourceMgr.sloc_entry_end();
868 SLoc != SLocEnd; ++SLoc) {
869 // Record the offset of this source-location entry.
870 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
871
872 // Figure out which record code to use.
873 unsigned Code;
874 if (SLoc->isFile()) {
875 if (SLoc->getFile().getContentCache()->Entry)
876 Code = pch::SM_SLOC_FILE_ENTRY;
877 else
878 Code = pch::SM_SLOC_BUFFER_ENTRY;
879 } else
880 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
881 Record.clear();
882 Record.push_back(Code);
883
884 Record.push_back(SLoc->getOffset());
885 if (SLoc->isFile()) {
886 const SrcMgr::FileInfo &File = SLoc->getFile();
887 Record.push_back(File.getIncludeLoc().getRawEncoding());
888 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
889 Record.push_back(File.hasLineDirectives());
890
891 const SrcMgr::ContentCache *Content = File.getContentCache();
892 if (Content->Entry) {
893 // The source location entry is a file. The blob associated
894 // with this entry is the file name.
Douglas Gregore650c8c2009-07-07 00:12:59 +0000895
896 // Turn the file name into an absolute path, if it isn't already.
897 const char *Filename = Content->Entry->getName();
898 llvm::sys::Path FilePath(Filename, strlen(Filename));
899 std::string FilenameStr;
900 if (!FilePath.isAbsolute()) {
901 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000902 P.appendComponent(FilePath.str());
903 FilenameStr = P.str();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000904 Filename = FilenameStr.c_str();
905 }
906
907 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000908 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000909
910 // FIXME: For now, preload all file source locations, so that
911 // we get the appropriate File entries in the reader. This is
912 // a temporary measure.
913 PreloadSLocs.push_back(SLocEntryOffsets.size());
914 } else {
915 // The source location entry is a buffer. The blob associated
916 // with this entry contains the contents of the buffer.
917
918 // We add one to the size so that we capture the trailing NULL
919 // that is required by llvm::MemoryBuffer::getMemBuffer (on
920 // the reader side).
921 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
922 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +0000923 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
924 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000925 Record.clear();
926 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
927 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +0000928 llvm::StringRef(Buffer->getBufferStart(),
929 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000930
931 if (strcmp(Name, "<built-in>") == 0)
932 PreloadSLocs.push_back(SLocEntryOffsets.size());
933 }
934 } else {
935 // The source location entry is an instantiation.
936 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
937 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
938 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
939 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
940
941 // Compute the token length for this macro expansion.
942 unsigned NextOffset = SourceMgr.getNextOffset();
943 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
944 if (++NextSLoc != SLocEnd)
945 NextOffset = NextSLoc->getOffset();
946 Record.push_back(NextOffset - SLoc->getOffset() - 1);
947 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
948 }
949 }
950
Douglas Gregorc9490c02009-04-16 22:23:12 +0000951 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000952
953 if (SLocEntryOffsets.empty())
954 return;
955
956 // Write the source-location offsets table into the PCH block. This
957 // table is used for lazily loading source-location information.
958 using namespace llvm;
959 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
960 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
964 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
965
966 Record.clear();
967 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
968 Record.push_back(SLocEntryOffsets.size());
969 Record.push_back(SourceMgr.getNextOffset());
970 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
971 (const char *)&SLocEntryOffsets.front(),
Chris Lattner090d9b52009-04-27 19:01:47 +0000972 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000973
974 // Write the source location entry preloads array, telling the PCH
975 // reader which source locations entries it should load eagerly.
976 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +0000977}
978
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000979//===----------------------------------------------------------------------===//
980// Preprocessor Serialization
981//===----------------------------------------------------------------------===//
982
Chris Lattner0b1fb982009-04-10 17:15:23 +0000983/// \brief Writes the block containing the serialized form of the
984/// preprocessor.
985///
Chris Lattnerdf961c22009-04-10 18:08:30 +0000986void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +0000987 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +0000988
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000989 // If the preprocessor __COUNTER__ value has been bumped, remember it.
990 if (PP.getCounterValue() != 0) {
991 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +0000992 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000993 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000994 }
995
996 // Enter the preprocessor block.
997 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000998
Douglas Gregor2eafc1b2009-04-26 00:07:37 +0000999 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1000 // FIXME: use diagnostics subsystem for localization etc.
1001 if (PP.SawDateOrTime())
1002 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1003
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001004 // Loop over all the macro definitions that are live at the end of the file,
1005 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001006 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1007 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001008 // FIXME: This emits macros in hash table order, we should do it in a stable
1009 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001010 MacroInfo *MI = I->second;
1011
1012 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1013 // been redefined by the header (in which case they are not isBuiltinMacro).
1014 if (MI->isBuiltinMacro())
1015 continue;
1016
Douglas Gregor37e26842009-04-21 23:56:24 +00001017 // FIXME: Remove this identifier reference?
Chris Lattner7356a312009-04-11 21:15:38 +00001018 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001019 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001020 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1021 Record.push_back(MI->isUsed());
1022
1023 unsigned Code;
1024 if (MI->isObjectLike()) {
1025 Code = pch::PP_MACRO_OBJECT_LIKE;
1026 } else {
1027 Code = pch::PP_MACRO_FUNCTION_LIKE;
1028
1029 Record.push_back(MI->isC99Varargs());
1030 Record.push_back(MI->isGNUVarargs());
1031 Record.push_back(MI->getNumArgs());
1032 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1033 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001034 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001035 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001036 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001037 Record.clear();
1038
Chris Lattnerdf961c22009-04-10 18:08:30 +00001039 // Emit the tokens array.
1040 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1041 // Note that we know that the preprocessor does not have any annotation
1042 // tokens in it because they are created by the parser, and thus can't be
1043 // in a macro definition.
1044 const Token &Tok = MI->getReplacementToken(TokNo);
1045
1046 Record.push_back(Tok.getLocation().getRawEncoding());
1047 Record.push_back(Tok.getLength());
1048
Chris Lattnerdf961c22009-04-10 18:08:30 +00001049 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1050 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001051 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001052
1053 // FIXME: Should translate token kind to a stable encoding.
1054 Record.push_back(Tok.getKind());
1055 // FIXME: Should translate token flags to a stable encoding.
1056 Record.push_back(Tok.getFlags());
1057
Douglas Gregorc9490c02009-04-16 22:23:12 +00001058 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001059 Record.clear();
1060 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001061 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001062 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001063 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001064}
1065
Douglas Gregor2e222532009-07-02 17:08:52 +00001066void PCHWriter::WriteComments(ASTContext &Context) {
1067 using namespace llvm;
1068
1069 if (Context.Comments.empty())
1070 return;
1071
1072 BitCodeAbbrev *CommentAbbrev = new BitCodeAbbrev();
1073 CommentAbbrev->Add(BitCodeAbbrevOp(pch::COMMENT_RANGES));
1074 CommentAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1075 unsigned CommentCode = Stream.EmitAbbrev(CommentAbbrev);
1076
1077 RecordData Record;
1078 Record.push_back(pch::COMMENT_RANGES);
1079 Stream.EmitRecordWithBlob(CommentCode, Record,
1080 (const char*)&Context.Comments[0],
1081 Context.Comments.size() * sizeof(SourceRange));
1082}
1083
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001084//===----------------------------------------------------------------------===//
1085// Type Serialization
1086//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001087
Douglas Gregor2cf26342009-04-09 22:27:44 +00001088/// \brief Write the representation of a type to the PCH stream.
1089void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001090 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001091 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001092 ID = NextTypeID++;
1093
1094 // Record the offset for this type.
1095 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001096 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001097 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1098 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001099 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001100 }
1101
1102 RecordData Record;
1103
1104 // Emit the type's representation.
1105 PCHTypeWriter W(*this, Record);
1106 switch (T->getTypeClass()) {
1107 // For all of the concrete, non-dependent types, call the
1108 // appropriate visitor function.
1109#define TYPE(Class, Base) \
1110 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1111#define ABSTRACT_TYPE(Class, Base)
1112#define DEPENDENT_TYPE(Class, Base)
1113#include "clang/AST/TypeNodes.def"
1114
1115 // For all of the dependent type nodes (which only occur in C++
1116 // templates), produce an error.
1117#define TYPE(Class, Base)
1118#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1119#include "clang/AST/TypeNodes.def"
1120 assert(false && "Cannot serialize dependent type nodes");
1121 break;
1122 }
1123
1124 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001125 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001126
1127 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001128 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001129}
1130
1131/// \brief Write a block containing all of the types.
1132void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001133 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001134 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001135
Douglas Gregor366809a2009-04-26 03:49:13 +00001136 // Emit all of the types that need to be emitted (so far).
1137 while (!TypesToEmit.empty()) {
1138 const Type *T = TypesToEmit.front();
1139 TypesToEmit.pop();
1140 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1141 WriteType(T);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001142 }
1143
1144 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001145 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001146}
1147
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001148//===----------------------------------------------------------------------===//
1149// Declaration Serialization
1150//===----------------------------------------------------------------------===//
1151
Douglas Gregor2cf26342009-04-09 22:27:44 +00001152/// \brief Write the block containing all of the declaration IDs
1153/// lexically declared within the given DeclContext.
1154///
1155/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1156/// bistream, or 0 if no block was written.
1157uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1158 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001159 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001160 return 0;
1161
Douglas Gregorc9490c02009-04-16 22:23:12 +00001162 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001163 RecordData Record;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001164 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1165 D != DEnd; ++D)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001166 AddDeclRef(*D, Record);
1167
Douglas Gregor25123082009-04-22 22:34:57 +00001168 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001169 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001170 return Offset;
1171}
1172
1173/// \brief Write the block containing all of the declaration IDs
1174/// visible from the given DeclContext.
1175///
1176/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1177/// bistream, or 0 if no block was written.
1178uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1179 DeclContext *DC) {
1180 if (DC->getPrimaryContext() != DC)
1181 return 0;
1182
Douglas Gregoraff22df2009-04-21 22:32:33 +00001183 // Since there is no name lookup into functions or methods, and we
1184 // perform name lookup for the translation unit via the
1185 // IdentifierInfo chains, don't bother to build a
1186 // visible-declarations table for these entities.
1187 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001188 return 0;
1189
Douglas Gregor2cf26342009-04-09 22:27:44 +00001190 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001191 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001192
1193 // Serialize the contents of the mapping used for lookup. Note that,
1194 // although we have two very different code paths, the serialized
1195 // representation is the same for both cases: a declaration name,
1196 // followed by a size, followed by references to the visible
1197 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001198 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001199 RecordData Record;
1200 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001201 if (!Map)
1202 return 0;
1203
Douglas Gregor2cf26342009-04-09 22:27:44 +00001204 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1205 D != DEnd; ++D) {
1206 AddDeclarationName(D->first, Record);
1207 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1208 Record.push_back(Result.second - Result.first);
1209 for(; Result.first != Result.second; ++Result.first)
1210 AddDeclRef(*Result.first, Record);
1211 }
1212
1213 if (Record.size() == 0)
1214 return 0;
1215
Douglas Gregorc9490c02009-04-16 22:23:12 +00001216 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001217 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001218 return Offset;
1219}
1220
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001221//===----------------------------------------------------------------------===//
1222// Global Method Pool and Selector Serialization
1223//===----------------------------------------------------------------------===//
1224
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001225namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001226// Trait used for the on-disk hash table used in the method pool.
1227class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1228 PCHWriter &Writer;
1229
1230public:
1231 typedef Selector key_type;
1232 typedef key_type key_type_ref;
1233
1234 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1235 typedef const data_type& data_type_ref;
1236
1237 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1238
1239 static unsigned ComputeHash(Selector Sel) {
1240 unsigned N = Sel.getNumArgs();
1241 if (N == 0)
1242 ++N;
1243 unsigned R = 5381;
1244 for (unsigned I = 0; I != N; ++I)
1245 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1246 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1247 return R;
1248 }
1249
1250 std::pair<unsigned,unsigned>
1251 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1252 data_type_ref Methods) {
1253 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1254 clang::io::Emit16(Out, KeyLen);
1255 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1256 for (const ObjCMethodList *Method = &Methods.first; Method;
1257 Method = Method->Next)
1258 if (Method->Method)
1259 DataLen += 4;
1260 for (const ObjCMethodList *Method = &Methods.second; Method;
1261 Method = Method->Next)
1262 if (Method->Method)
1263 DataLen += 4;
1264 clang::io::Emit16(Out, DataLen);
1265 return std::make_pair(KeyLen, DataLen);
1266 }
1267
Douglas Gregor83941df2009-04-25 17:48:32 +00001268 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1269 uint64_t Start = Out.tell();
1270 assert((Start >> 32) == 0 && "Selector key offset too large");
1271 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001272 unsigned N = Sel.getNumArgs();
1273 clang::io::Emit16(Out, N);
1274 if (N == 0)
1275 N = 1;
1276 for (unsigned I = 0; I != N; ++I)
1277 clang::io::Emit32(Out,
1278 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1279 }
1280
1281 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001282 data_type_ref Methods, unsigned DataLen) {
1283 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001284 unsigned NumInstanceMethods = 0;
1285 for (const ObjCMethodList *Method = &Methods.first; Method;
1286 Method = Method->Next)
1287 if (Method->Method)
1288 ++NumInstanceMethods;
1289
1290 unsigned NumFactoryMethods = 0;
1291 for (const ObjCMethodList *Method = &Methods.second; Method;
1292 Method = Method->Next)
1293 if (Method->Method)
1294 ++NumFactoryMethods;
1295
1296 clang::io::Emit16(Out, NumInstanceMethods);
1297 clang::io::Emit16(Out, NumFactoryMethods);
1298 for (const ObjCMethodList *Method = &Methods.first; Method;
1299 Method = Method->Next)
1300 if (Method->Method)
1301 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001302 for (const ObjCMethodList *Method = &Methods.second; Method;
1303 Method = Method->Next)
1304 if (Method->Method)
1305 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001306
1307 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001308 }
1309};
1310} // end anonymous namespace
1311
1312/// \brief Write the method pool into the PCH file.
1313///
1314/// The method pool contains both instance and factory methods, stored
1315/// in an on-disk hash table indexed by the selector.
1316void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1317 using namespace llvm;
1318
1319 // Create and write out the blob that contains the instance and
1320 // factor method pools.
1321 bool Empty = true;
1322 {
1323 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1324
1325 // Create the on-disk hash table representation. Start by
1326 // iterating through the instance method pool.
1327 PCHMethodPoolTrait::key_type Key;
Douglas Gregor83941df2009-04-25 17:48:32 +00001328 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001329 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1330 Instance = SemaRef.InstanceMethodPool.begin(),
1331 InstanceEnd = SemaRef.InstanceMethodPool.end();
1332 Instance != InstanceEnd; ++Instance) {
1333 // Check whether there is a factory method with the same
1334 // selector.
1335 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1336 = SemaRef.FactoryMethodPool.find(Instance->first);
1337
1338 if (Factory == SemaRef.FactoryMethodPool.end())
1339 Generator.insert(Instance->first,
1340 std::make_pair(Instance->second,
1341 ObjCMethodList()));
1342 else
1343 Generator.insert(Instance->first,
1344 std::make_pair(Instance->second, Factory->second));
1345
Douglas Gregor83941df2009-04-25 17:48:32 +00001346 ++NumSelectorsInMethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001347 Empty = false;
1348 }
1349
1350 // Now iterate through the factory method pool, to pick up any
1351 // selectors that weren't already in the instance method pool.
1352 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1353 Factory = SemaRef.FactoryMethodPool.begin(),
1354 FactoryEnd = SemaRef.FactoryMethodPool.end();
1355 Factory != FactoryEnd; ++Factory) {
1356 // Check whether there is an instance method with the same
1357 // selector. If so, there is no work to do here.
1358 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1359 = SemaRef.InstanceMethodPool.find(Factory->first);
1360
Douglas Gregor83941df2009-04-25 17:48:32 +00001361 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001362 Generator.insert(Factory->first,
1363 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor83941df2009-04-25 17:48:32 +00001364 ++NumSelectorsInMethodPool;
1365 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001366
1367 Empty = false;
1368 }
1369
Douglas Gregor83941df2009-04-25 17:48:32 +00001370 if (Empty && SelectorOffsets.empty())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001371 return;
1372
1373 // Create the on-disk hash table in a buffer.
Daniel Dunbarec312a12009-08-24 09:31:37 +00001374 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001375 uint32_t BucketOffset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001376 SelectorOffsets.resize(SelVector.size());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001377 {
1378 PCHMethodPoolTrait Trait(*this);
1379 llvm::raw_svector_ostream Out(MethodPool);
1380 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001381 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001382 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor83941df2009-04-25 17:48:32 +00001383
1384 // For every selector that we have seen but which was not
1385 // written into the hash table, write the selector itself and
1386 // record it's offset.
1387 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1388 if (SelectorOffsets[I] == 0)
1389 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001390 }
1391
1392 // Create a blob abbreviation
1393 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1394 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1398 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1399
Douglas Gregor83941df2009-04-25 17:48:32 +00001400 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001401 RecordData Record;
1402 Record.push_back(pch::METHOD_POOL);
1403 Record.push_back(BucketOffset);
Douglas Gregor83941df2009-04-25 17:48:32 +00001404 Record.push_back(NumSelectorsInMethodPool);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001405 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001406
1407 // Create a blob abbreviation for the selector table offsets.
1408 Abbrev = new BitCodeAbbrev();
1409 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1412 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1413
1414 // Write the selector offsets table.
1415 Record.clear();
1416 Record.push_back(pch::SELECTOR_OFFSETS);
1417 Record.push_back(SelectorOffsets.size());
1418 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1419 (const char *)&SelectorOffsets.front(),
1420 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001421 }
1422}
1423
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001424//===----------------------------------------------------------------------===//
1425// Identifier Table Serialization
1426//===----------------------------------------------------------------------===//
1427
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001428namespace {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001429class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1430 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001431 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001432
Douglas Gregora92193e2009-04-28 21:18:29 +00001433 /// \brief Determines whether this is an "interesting" identifier
1434 /// that needs a full IdentifierInfo structure written into the hash
1435 /// table.
1436 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1437 return II->isPoisoned() ||
1438 II->isExtensionToken() ||
1439 II->hasMacroDefinition() ||
1440 II->getObjCOrBuiltinID() ||
1441 II->getFETokenInfo<void>();
1442 }
1443
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001444public:
1445 typedef const IdentifierInfo* key_type;
1446 typedef key_type key_type_ref;
1447
1448 typedef pch::IdentID data_type;
1449 typedef data_type data_type_ref;
1450
Douglas Gregor37e26842009-04-21 23:56:24 +00001451 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1452 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001453
1454 static unsigned ComputeHash(const IdentifierInfo* II) {
1455 return clang::BernsteinHash(II->getName());
1456 }
1457
Douglas Gregor37e26842009-04-21 23:56:24 +00001458 std::pair<unsigned,unsigned>
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001459 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1460 pch::IdentID ID) {
1461 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001462 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1463 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001464 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregora92193e2009-04-28 21:18:29 +00001465 if (II->hasMacroDefinition() &&
1466 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001467 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001468 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1469 DEnd = IdentifierResolver::end();
1470 D != DEnd; ++D)
1471 DataLen += sizeof(pch::DeclID);
1472 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001473 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001474 // We emit the key length after the data length so that every
1475 // string is preceded by a 16-bit length. This matches the PTH
1476 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001477 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001478 return std::make_pair(KeyLen, DataLen);
1479 }
1480
1481 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1482 unsigned KeyLen) {
1483 // Record the location of the key data. This is used when generating
1484 // the mapping from persistent IDs to strings.
1485 Writer.SetIdentifierOffset(II, Out.tell());
1486 Out.write(II->getName(), KeyLen);
1487 }
1488
1489 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1490 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001491 if (!isInterestingIdentifier(II)) {
1492 clang::io::Emit32(Out, ID << 1);
1493 return;
1494 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001495
Douglas Gregora92193e2009-04-28 21:18:29 +00001496 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001497 uint32_t Bits = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001498 bool hasMacroDefinition =
1499 II->hasMacroDefinition() &&
1500 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001501 Bits = (uint32_t)II->getObjCOrBuiltinID();
Douglas Gregor2deaea32009-04-22 18:49:13 +00001502 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001503 Bits = (Bits << 1) | II->isExtensionToken();
1504 Bits = (Bits << 1) | II->isPoisoned();
1505 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
Douglas Gregor5998da52009-04-28 21:32:13 +00001506 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001507
Douglas Gregor37e26842009-04-21 23:56:24 +00001508 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001509 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001510
Douglas Gregor668c1a42009-04-21 22:25:48 +00001511 // Emit the declaration IDs in reverse order, because the
1512 // IdentifierResolver provides the declarations as they would be
1513 // visible (e.g., the function "stat" would come before the struct
1514 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1515 // adds declarations to the end of the list (so we need to see the
1516 // struct "status" before the function "status").
1517 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1518 IdentifierResolver::end());
1519 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1520 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001521 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00001522 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001523 }
1524};
1525} // end anonymous namespace
1526
Douglas Gregorafaf3082009-04-11 00:14:32 +00001527/// \brief Write the identifier table into the PCH file.
1528///
1529/// The identifier table consists of a blob containing string data
1530/// (the actual identifiers themselves) and a separate "offsets" index
1531/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00001532void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001533 using namespace llvm;
1534
1535 // Create and write out the blob that contains the identifier
1536 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001537 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001538 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1539
Douglas Gregor92b059e2009-04-28 20:33:11 +00001540 // Look for any identifiers that were named while processing the
1541 // headers, but are otherwise not needed. We add these to the hash
1542 // table to enable checking of the predefines buffer in the case
1543 // where the user adds new macro definitions when building the PCH
1544 // file.
1545 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1546 IDEnd = PP.getIdentifierTable().end();
1547 ID != IDEnd; ++ID)
1548 getIdentifierRef(ID->second);
1549
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001550 // Create the on-disk hash table representation.
Douglas Gregor92b059e2009-04-28 20:33:11 +00001551 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001552 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1553 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1554 ID != IDEnd; ++ID) {
1555 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor02fc7512009-04-28 20:01:51 +00001556 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001557 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001558
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001559 // Create the on-disk hash table in a buffer.
Daniel Dunbarec312a12009-08-24 09:31:37 +00001560 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001561 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001562 {
Douglas Gregor37e26842009-04-21 23:56:24 +00001563 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001564 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001565 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001566 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001567 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001568 }
1569
1570 // Create a blob abbreviation
1571 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1572 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001575 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001576
1577 // Write the identifier table
1578 RecordData Record;
1579 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001580 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001581 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001582 }
1583
1584 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001585 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1586 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1589 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1590
1591 RecordData Record;
1592 Record.push_back(pch::IDENTIFIER_OFFSET);
1593 Record.push_back(IdentifierOffsets.size());
1594 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
1595 (const char *)&IdentifierOffsets.front(),
1596 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001597}
1598
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001599//===----------------------------------------------------------------------===//
1600// General Serialization Routines
1601//===----------------------------------------------------------------------===//
1602
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001603/// \brief Write a record containing the given attributes.
1604void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1605 RecordData Record;
1606 for (; Attr; Attr = Attr->getNext()) {
1607 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1608 Record.push_back(Attr->isInherited());
1609 switch (Attr->getKind()) {
1610 case Attr::Alias:
1611 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1612 break;
1613
1614 case Attr::Aligned:
1615 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1616 break;
1617
1618 case Attr::AlwaysInline:
1619 break;
1620
1621 case Attr::AnalyzerNoReturn:
1622 break;
1623
1624 case Attr::Annotate:
1625 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1626 break;
1627
1628 case Attr::AsmLabel:
1629 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1630 break;
1631
1632 case Attr::Blocks:
1633 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1634 break;
1635
1636 case Attr::Cleanup:
1637 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1638 break;
1639
1640 case Attr::Const:
1641 break;
1642
1643 case Attr::Constructor:
1644 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1645 break;
1646
1647 case Attr::DLLExport:
1648 case Attr::DLLImport:
1649 case Attr::Deprecated:
1650 break;
1651
1652 case Attr::Destructor:
1653 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1654 break;
1655
1656 case Attr::FastCall:
1657 break;
1658
1659 case Attr::Format: {
1660 const FormatAttr *Format = cast<FormatAttr>(Attr);
1661 AddString(Format->getType(), Record);
1662 Record.push_back(Format->getFormatIdx());
1663 Record.push_back(Format->getFirstArg());
1664 break;
1665 }
1666
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001667 case Attr::FormatArg: {
1668 const FormatArgAttr *Format = cast<FormatArgAttr>(Attr);
1669 Record.push_back(Format->getFormatIdx());
1670 break;
1671 }
1672
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001673 case Attr::Sentinel : {
1674 const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr);
1675 Record.push_back(Sentinel->getSentinel());
1676 Record.push_back(Sentinel->getNullPos());
1677 break;
1678 }
1679
Chris Lattnercf2a7212009-04-20 19:12:28 +00001680 case Attr::GNUInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001681 case Attr::IBOutletKind:
Ryan Flynn76168e22009-08-09 20:07:29 +00001682 case Attr::Malloc:
Mike Stump1feade82009-08-26 22:31:08 +00001683 case Attr::NoDebug:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001684 case Attr::NoReturn:
1685 case Attr::NoThrow:
Mike Stump1feade82009-08-26 22:31:08 +00001686 case Attr::NoInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001687 break;
1688
1689 case Attr::NonNull: {
1690 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1691 Record.push_back(NonNull->size());
1692 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1693 break;
1694 }
1695
1696 case Attr::ObjCException:
1697 case Attr::ObjCNSObject:
Ted Kremenekb71368d2009-05-09 02:44:38 +00001698 case Attr::CFReturnsRetained:
1699 case Attr::NSReturnsRetained:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001700 case Attr::Overloadable:
1701 break;
1702
Anders Carlssona860e752009-08-08 18:23:56 +00001703 case Attr::PragmaPack:
1704 Record.push_back(cast<PragmaPackAttr>(Attr)->getAlignment());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001705 break;
1706
Anders Carlssona860e752009-08-08 18:23:56 +00001707 case Attr::Packed:
1708 break;
1709
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001710 case Attr::Pure:
1711 break;
1712
1713 case Attr::Regparm:
1714 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1715 break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001716
1717 case Attr::ReqdWorkGroupSize:
1718 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim());
1719 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim());
1720 Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim());
1721 break;
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001722
1723 case Attr::Section:
1724 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1725 break;
1726
1727 case Attr::StdCall:
1728 case Attr::TransparentUnion:
1729 case Attr::Unavailable:
1730 case Attr::Unused:
1731 case Attr::Used:
1732 break;
1733
1734 case Attr::Visibility:
1735 // FIXME: stable encoding
1736 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1737 break;
1738
1739 case Attr::WarnUnusedResult:
1740 case Attr::Weak:
1741 case Attr::WeakImport:
1742 break;
1743 }
1744 }
1745
Douglas Gregorc9490c02009-04-16 22:23:12 +00001746 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001747}
1748
1749void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1750 Record.push_back(Str.size());
1751 Record.insert(Record.end(), Str.begin(), Str.end());
1752}
1753
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001754/// \brief Note that the identifier II occurs at the given offset
1755/// within the identifier table.
1756void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001757 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001758}
1759
Douglas Gregor83941df2009-04-25 17:48:32 +00001760/// \brief Note that the selector Sel occurs at the given offset
1761/// within the method pool/selector table.
1762void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
1763 unsigned ID = SelectorIDs[Sel];
1764 assert(ID && "Unknown selector");
1765 SelectorOffsets[ID - 1] = Offset;
1766}
1767
Douglas Gregorc9490c02009-04-16 22:23:12 +00001768PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor37e26842009-04-21 23:56:24 +00001769 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00001770 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
1771 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001772
Douglas Gregore650c8c2009-07-07 00:12:59 +00001773void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
1774 const char *isysroot) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001775 using namespace llvm;
1776
Douglas Gregore7785042009-04-20 15:53:59 +00001777 ASTContext &Context = SemaRef.Context;
1778 Preprocessor &PP = SemaRef.PP;
1779
Douglas Gregor2cf26342009-04-09 22:27:44 +00001780 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001781 Stream.Emit((unsigned)'C', 8);
1782 Stream.Emit((unsigned)'P', 8);
1783 Stream.Emit((unsigned)'C', 8);
1784 Stream.Emit((unsigned)'H', 8);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001785
1786 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001787
1788 // The translation unit is the first declaration we'll emit.
1789 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1790 DeclsToEmit.push(Context.getTranslationUnitDecl());
1791
Douglas Gregor2deaea32009-04-22 18:49:13 +00001792 // Make sure that we emit IdentifierInfos (and any attached
1793 // declarations) for builtins.
1794 {
1795 IdentifierTable &Table = PP.getIdentifierTable();
1796 llvm::SmallVector<const char *, 32> BuiltinNames;
1797 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
1798 Context.getLangOptions().NoBuiltin);
1799 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
1800 getIdentifierRef(&Table.get(BuiltinNames[I]));
1801 }
1802
Chris Lattner63d65f82009-09-08 18:19:27 +00001803 // Build a record containing all of the tentative definitions in this file, in
1804 // TentativeDefinitionList order. Generally, this record will be empty for
1805 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001806 RecordData TentativeDefinitions;
Chris Lattner63d65f82009-09-08 18:19:27 +00001807 for (unsigned i = 0, e = SemaRef.TentativeDefinitionList.size(); i != e; ++i){
1808 VarDecl *VD =
1809 SemaRef.TentativeDefinitions.lookup(SemaRef.TentativeDefinitionList[i]);
1810 if (VD) AddDeclRef(VD, TentativeDefinitions);
1811 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001812
Douglas Gregor14c22f22009-04-22 22:18:58 +00001813 // Build a record containing all of the locally-scoped external
1814 // declarations in this header file. Generally, this record will be
1815 // empty.
1816 RecordData LocallyScopedExternalDecls;
Chris Lattner63d65f82009-09-08 18:19:27 +00001817 // FIXME: This is filling in the PCH file in densemap order which is
1818 // nondeterminstic!
Douglas Gregor14c22f22009-04-22 22:18:58 +00001819 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
1820 TD = SemaRef.LocallyScopedExternalDecls.begin(),
1821 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
1822 TD != TDEnd; ++TD)
1823 AddDeclRef(TD->second, LocallyScopedExternalDecls);
1824
Douglas Gregorb81c1702009-04-27 20:06:05 +00001825 // Build a record containing all of the ext_vector declarations.
1826 RecordData ExtVectorDecls;
1827 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
1828 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
1829
Douglas Gregor2cf26342009-04-09 22:27:44 +00001830 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00001831 RecordData Record;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001832 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001833 WriteMetadata(Context, isysroot);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001834 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001835 if (StatCalls && !isysroot)
1836 WriteStatCache(*StatCalls, isysroot);
1837 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Chris Lattner0b1fb982009-04-10 17:15:23 +00001838 WritePreprocessor(PP);
Douglas Gregor2e222532009-07-02 17:08:52 +00001839 WriteComments(Context);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001840 // Write the record of special types.
1841 Record.clear();
1842
1843 AddTypeRef(Context.getBuiltinVaListType(), Record);
1844 AddTypeRef(Context.getObjCIdType(), Record);
1845 AddTypeRef(Context.getObjCSelType(), Record);
1846 AddTypeRef(Context.getObjCProtoType(), Record);
1847 AddTypeRef(Context.getObjCClassType(), Record);
1848 AddTypeRef(Context.getRawCFConstantStringType(), Record);
1849 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
1850 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00001851 AddTypeRef(Context.getjmp_bufType(), Record);
1852 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00001853 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
1854 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001855 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Douglas Gregor2e222532009-07-02 17:08:52 +00001856
Douglas Gregor366809a2009-04-26 03:49:13 +00001857 // Keep writing types and declarations until all types and
1858 // declarations have been written.
1859 do {
1860 if (!DeclsToEmit.empty())
1861 WriteDeclsBlock(Context);
1862 if (!TypesToEmit.empty())
1863 WriteTypesBlock(Context);
1864 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
1865
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001866 WriteMethodPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00001867 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001868
1869 // Write the type offsets array
1870 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1871 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1873 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1874 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1875 Record.clear();
1876 Record.push_back(pch::TYPE_OFFSET);
1877 Record.push_back(TypeOffsets.size());
1878 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
1879 (const char *)&TypeOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00001880 TypeOffsets.size() * sizeof(TypeOffsets[0]));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001881
1882 // Write the declaration offsets array
1883 Abbrev = new BitCodeAbbrev();
1884 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1887 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1888 Record.clear();
1889 Record.push_back(pch::DECL_OFFSET);
1890 Record.push_back(DeclOffsets.size());
1891 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
1892 (const char *)&DeclOffsets.front(),
Chris Lattnerc732f5a2009-04-27 18:24:17 +00001893 DeclOffsets.size() * sizeof(DeclOffsets[0]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001894
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001895 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00001896 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00001897 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001898
1899 // Write the record containing tentative definitions.
1900 if (!TentativeDefinitions.empty())
1901 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00001902
1903 // Write the record containing locally-scoped external definitions.
1904 if (!LocallyScopedExternalDecls.empty())
1905 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
1906 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00001907
1908 // Write the record containing ext_vector type names.
1909 if (!ExtVectorDecls.empty())
1910 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001911
1912 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00001913 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00001914 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00001915 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00001916 Record.push_back(NumLexicalDeclContexts);
1917 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00001918 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001919 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001920}
1921
1922void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1923 Record.push_back(Loc.getRawEncoding());
1924}
1925
1926void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1927 Record.push_back(Value.getBitWidth());
1928 unsigned N = Value.getNumWords();
1929 const uint64_t* Words = Value.getRawData();
1930 for (unsigned I = 0; I != N; ++I)
1931 Record.push_back(Words[I]);
1932}
1933
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001934void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1935 Record.push_back(Value.isUnsigned());
1936 AddAPInt(Value, Record);
1937}
1938
Douglas Gregor17fc2232009-04-14 21:55:33 +00001939void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1940 AddAPInt(Value.bitcastToAPInt(), Record);
1941}
1942
Douglas Gregor2cf26342009-04-09 22:27:44 +00001943void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00001944 Record.push_back(getIdentifierRef(II));
1945}
1946
1947pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
1948 if (II == 0)
1949 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001950
1951 pch::IdentID &ID = IdentifierIDs[II];
1952 if (ID == 0)
1953 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00001954 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001955}
1956
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001957void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
1958 if (SelRef.getAsOpaquePtr() == 0) {
1959 Record.push_back(0);
1960 return;
1961 }
1962
1963 pch::SelectorID &SID = SelectorIDs[SelRef];
1964 if (SID == 0) {
1965 SID = SelectorIDs.size();
1966 SelVector.push_back(SelRef);
1967 }
1968 Record.push_back(SID);
1969}
1970
Douglas Gregor2cf26342009-04-09 22:27:44 +00001971void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1972 if (T.isNull()) {
1973 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1974 return;
1975 }
1976
1977 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001978 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001979 switch (BT->getKind()) {
1980 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1981 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1982 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1983 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1984 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1985 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1986 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1987 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001988 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001989 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1990 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1991 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1992 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1993 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1994 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1995 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00001996 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001997 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1998 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1999 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002000 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002001 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2002 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002003 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2004 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002005 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2006 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002007 case BuiltinType::UndeducedAuto:
2008 assert(0 && "Should not see undeduced auto here");
2009 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002010 }
2011
2012 Record.push_back((ID << 3) | T.getCVRQualifiers());
2013 return;
2014 }
2015
Douglas Gregor8038d512009-04-10 17:25:41 +00002016 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor366809a2009-04-26 03:49:13 +00002017 if (ID == 0) {
2018 // We haven't seen this type before. Assign it a new ID and put it
2019 // into the queu of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002020 ID = NextTypeID++;
Douglas Gregor366809a2009-04-26 03:49:13 +00002021 TypesToEmit.push(T.getTypePtr());
2022 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002023
2024 // Encode the type qualifiers in the type reference.
2025 Record.push_back((ID << 3) | T.getCVRQualifiers());
2026}
2027
2028void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2029 if (D == 0) {
2030 Record.push_back(0);
2031 return;
2032 }
2033
Douglas Gregor8038d512009-04-10 17:25:41 +00002034 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002035 if (ID == 0) {
2036 // We haven't seen this declaration before. Give it a new ID and
2037 // enqueue it in the list of declarations to emit.
2038 ID = DeclIDs.size();
2039 DeclsToEmit.push(const_cast<Decl *>(D));
2040 }
2041
2042 Record.push_back(ID);
2043}
2044
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002045pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2046 if (D == 0)
2047 return 0;
2048
2049 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2050 return DeclIDs[D];
2051}
2052
Douglas Gregor2cf26342009-04-09 22:27:44 +00002053void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002054 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002055 Record.push_back(Name.getNameKind());
2056 switch (Name.getNameKind()) {
2057 case DeclarationName::Identifier:
2058 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2059 break;
2060
2061 case DeclarationName::ObjCZeroArgSelector:
2062 case DeclarationName::ObjCOneArgSelector:
2063 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002064 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002065 break;
2066
2067 case DeclarationName::CXXConstructorName:
2068 case DeclarationName::CXXDestructorName:
2069 case DeclarationName::CXXConversionFunctionName:
2070 AddTypeRef(Name.getCXXNameType(), Record);
2071 break;
2072
2073 case DeclarationName::CXXOperatorName:
2074 Record.push_back(Name.getCXXOverloadedOperator());
2075 break;
2076
2077 case DeclarationName::CXXUsingDirective:
2078 // No extra data to emit
2079 break;
2080 }
2081}
Douglas Gregor0b748912009-04-14 21:18:50 +00002082