blob: 421c345e78040c03ae281c0320422338b569efff [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"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamWriter.h"
35#include "llvm/Support/Compiler.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.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//===----------------------------------------------------------------------===//
43namespace {
44 class VISIBILITY_HIDDEN PCHTypeWriter {
45 PCHWriter &Writer;
46 PCHWriter::RecordData &Record;
47
48 public:
49 /// \brief Type code that corresponds to the record generated.
50 pch::TypeCode Code;
51
52 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
53 : Writer(Writer), Record(Record) { }
54
55 void VisitArrayType(const ArrayType *T);
56 void VisitFunctionType(const FunctionType *T);
57 void VisitTagType(const TagType *T);
58
59#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
60#define ABSTRACT_TYPE(Class, Base)
61#define DEPENDENT_TYPE(Class, Base)
62#include "clang/AST/TypeNodes.def"
63 };
64}
65
66void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
67 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
68 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
69 Record.push_back(T->getAddressSpace());
70 Code = pch::TYPE_EXT_QUAL;
71}
72
73void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
74 assert(false && "Built-in types are never serialized");
75}
76
77void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
78 Record.push_back(T->getWidth());
79 Record.push_back(T->isSigned());
80 Code = pch::TYPE_FIXED_WIDTH_INT;
81}
82
83void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
84 Writer.AddTypeRef(T->getElementType(), Record);
85 Code = pch::TYPE_COMPLEX;
86}
87
88void PCHTypeWriter::VisitPointerType(const PointerType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_POINTER;
91}
92
93void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_BLOCK_POINTER;
96}
97
98void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_LVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Code = pch::TYPE_RVALUE_REFERENCE;
106}
107
108void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
109 Writer.AddTypeRef(T->getPointeeType(), Record);
110 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
111 Code = pch::TYPE_MEMBER_POINTER;
112}
113
114void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
115 Writer.AddTypeRef(T->getElementType(), Record);
116 Record.push_back(T->getSizeModifier()); // FIXME: stable values
117 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
118}
119
120void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
121 VisitArrayType(T);
122 Writer.AddAPInt(T->getSize(), Record);
123 Code = pch::TYPE_CONSTANT_ARRAY;
124}
125
126void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
127 VisitArrayType(T);
128 Code = pch::TYPE_INCOMPLETE_ARRAY;
129}
130
131void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
132 VisitArrayType(T);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000133 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Code = pch::TYPE_VARIABLE_ARRAY;
135}
136
137void PCHTypeWriter::VisitVectorType(const VectorType *T) {
138 Writer.AddTypeRef(T->getElementType(), Record);
139 Record.push_back(T->getNumElements());
140 Code = pch::TYPE_VECTOR;
141}
142
143void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
144 VisitVectorType(T);
145 Code = pch::TYPE_EXT_VECTOR;
146}
147
148void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
149 Writer.AddTypeRef(T->getResultType(), Record);
150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
164 Code = pch::TYPE_FUNCTION_PROTO;
165}
166
167void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
168 Writer.AddDeclRef(T->getDecl(), Record);
169 Code = pch::TYPE_TYPEDEF;
170}
171
172void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000173 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174 Code = pch::TYPE_TYPEOF_EXPR;
175}
176
177void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
178 Writer.AddTypeRef(T->getUnderlyingType(), Record);
179 Code = pch::TYPE_TYPEOF;
180}
181
182void PCHTypeWriter::VisitTagType(const TagType *T) {
183 Writer.AddDeclRef(T->getDecl(), Record);
184 assert(!T->isBeingDefined() &&
185 "Cannot serialize in the middle of a type definition");
186}
187
188void PCHTypeWriter::VisitRecordType(const RecordType *T) {
189 VisitTagType(T);
190 Code = pch::TYPE_RECORD;
191}
192
193void PCHTypeWriter::VisitEnumType(const EnumType *T) {
194 VisitTagType(T);
195 Code = pch::TYPE_ENUM;
196}
197
198void
199PCHTypeWriter::VisitTemplateSpecializationType(
200 const TemplateSpecializationType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000201 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000202 assert(false && "Cannot serialize template specialization types");
203}
204
205void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000206 // FIXME: Serialize this type (C++ only)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207 assert(false && "Cannot serialize qualified name types");
208}
209
210void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
211 Writer.AddDeclRef(T->getDecl(), Record);
212 Code = pch::TYPE_OBJC_INTERFACE;
213}
214
215void
216PCHTypeWriter::VisitObjCQualifiedInterfaceType(
217 const ObjCQualifiedInterfaceType *T) {
218 VisitObjCInterfaceType(T);
219 Record.push_back(T->getNumProtocols());
220 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
221 Writer.AddDeclRef(T->getProtocol(I), Record);
222 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
223}
224
225void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
226 Record.push_back(T->getNumProtocols());
227 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
228 Writer.AddDeclRef(T->getProtocols(I), Record);
229 Code = pch::TYPE_OBJC_QUALIFIED_ID;
230}
231
Douglas Gregor2cf26342009-04-09 22:27:44 +0000232//===----------------------------------------------------------------------===//
233// Declaration serialization
234//===----------------------------------------------------------------------===//
235namespace {
236 class VISIBILITY_HIDDEN PCHDeclWriter
237 : public DeclVisitor<PCHDeclWriter, void> {
238
239 PCHWriter &Writer;
Douglas Gregor72971342009-04-18 00:02:19 +0000240 ASTContext &Context;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241 PCHWriter::RecordData &Record;
242
243 public:
244 pch::DeclCode Code;
245
Douglas Gregor72971342009-04-18 00:02:19 +0000246 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
247 PCHWriter::RecordData &Record)
248 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249
250 void VisitDecl(Decl *D);
251 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
252 void VisitNamedDecl(NamedDecl *D);
253 void VisitTypeDecl(TypeDecl *D);
254 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000255 void VisitTagDecl(TagDecl *D);
256 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000257 void VisitRecordDecl(RecordDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 void VisitValueDecl(ValueDecl *D);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000259 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000260 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor8c700062009-04-13 21:20:57 +0000261 void VisitFieldDecl(FieldDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262 void VisitVarDecl(VarDecl *D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000263 void VisitParmVarDecl(ParmVarDecl *D);
264 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor1028bc62009-04-13 22:49:25 +0000265 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
266 void VisitBlockDecl(BlockDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
268 uint64_t VisibleOffset);
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000269 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +0000270 void VisitObjCContainerDecl(ObjCContainerDecl *D);
271 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
272 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff30833f82009-04-21 15:12:33 +0000273 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
274 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
275 void VisitObjCClassDecl(ObjCClassDecl *D);
276 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
277 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
278 void VisitObjCImplDecl(ObjCImplDecl *D);
279 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
280 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
281 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
282 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
283 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000284 };
285}
286
287void PCHDeclWriter::VisitDecl(Decl *D) {
288 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
289 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
290 Writer.AddSourceLocation(D->getLocation(), Record);
291 Record.push_back(D->isInvalidDecl());
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000292 Record.push_back(D->hasAttrs());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000293 Record.push_back(D->isImplicit());
294 Record.push_back(D->getAccess());
295}
296
297void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
298 VisitDecl(D);
299 Code = pch::DECL_TRANSLATION_UNIT;
300}
301
302void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
303 VisitDecl(D);
304 Writer.AddDeclarationName(D->getDeclName(), Record);
305}
306
307void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
308 VisitNamedDecl(D);
309 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
310}
311
312void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
313 VisitTypeDecl(D);
314 Writer.AddTypeRef(D->getUnderlyingType(), Record);
315 Code = pch::DECL_TYPEDEF;
316}
317
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000318void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
319 VisitTypeDecl(D);
320 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
321 Record.push_back(D->isDefinition());
322 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
323}
324
325void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
326 VisitTagDecl(D);
327 Writer.AddTypeRef(D->getIntegerType(), Record);
328 Code = pch::DECL_ENUM;
329}
330
Douglas Gregor8c700062009-04-13 21:20:57 +0000331void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
332 VisitTagDecl(D);
333 Record.push_back(D->hasFlexibleArrayMember());
334 Record.push_back(D->isAnonymousStructOrUnion());
335 Code = pch::DECL_RECORD;
336}
337
Douglas Gregor2cf26342009-04-09 22:27:44 +0000338void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
339 VisitNamedDecl(D);
340 Writer.AddTypeRef(D->getType(), Record);
341}
342
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000343void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
344 VisitValueDecl(D);
Douglas Gregor0b748912009-04-14 21:18:50 +0000345 Record.push_back(D->getInitExpr()? 1 : 0);
346 if (D->getInitExpr())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000347 Writer.AddStmt(D->getInitExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000348 Writer.AddAPSInt(D->getInitVal(), Record);
349 Code = pch::DECL_ENUM_CONSTANT;
350}
351
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000352void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
353 VisitValueDecl(D);
Douglas Gregor025452f2009-04-17 00:04:06 +0000354 Record.push_back(D->isThisDeclarationADefinition());
355 if (D->isThisDeclarationADefinition())
Douglas Gregor72971342009-04-18 00:02:19 +0000356 Writer.AddStmt(D->getBody(Context));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000357 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
358 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
359 Record.push_back(D->isInline());
Douglas Gregorb3efa982009-04-23 18:22:55 +0000360 Record.push_back(D->isC99InlineDefinition());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000361 Record.push_back(D->isVirtual());
362 Record.push_back(D->isPure());
363 Record.push_back(D->inheritedPrototype());
364 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
365 Record.push_back(D->isDeleted());
366 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
367 Record.push_back(D->param_size());
368 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
369 P != PEnd; ++P)
370 Writer.AddDeclRef(*P, Record);
371 Code = pch::DECL_FUNCTION;
372}
373
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000374void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
375 VisitNamedDecl(D);
376 // FIXME: convert to LazyStmtPtr?
377 // Unlike C/C++, method bodies will never be in header files.
378 Record.push_back(D->getBody() != 0);
379 if (D->getBody() != 0) {
380 Writer.AddStmt(D->getBody(Context));
381 Writer.AddDeclRef(D->getSelfDecl(), Record);
382 Writer.AddDeclRef(D->getCmdDecl(), Record);
383 }
384 Record.push_back(D->isInstanceMethod());
385 Record.push_back(D->isVariadic());
386 Record.push_back(D->isSynthesized());
387 // FIXME: stable encoding for @required/@optional
388 Record.push_back(D->getImplementationControl());
389 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
390 Record.push_back(D->getObjCDeclQualifier());
391 Writer.AddTypeRef(D->getResultType(), Record);
392 Writer.AddSourceLocation(D->getLocEnd(), Record);
393 Record.push_back(D->param_size());
394 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
395 PEnd = D->param_end(); P != PEnd; ++P)
396 Writer.AddDeclRef(*P, Record);
397 Code = pch::DECL_OBJC_METHOD;
398}
399
Steve Naroff33feeb02009-04-20 20:09:33 +0000400void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
401 VisitNamedDecl(D);
402 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
403 // Abstract class (no need to define a stable pch::DECL code).
404}
405
406void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
407 VisitObjCContainerDecl(D);
408 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
409 Writer.AddDeclRef(D->getSuperClass(), Record);
Douglas Gregor291be392009-04-23 03:59:07 +0000410 Record.push_back(D->protocol_size());
411 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
412 PEnd = D->protocol_end();
413 P != PEnd; ++P)
414 Writer.AddDeclRef(*P, Record);
Steve Naroff33feeb02009-04-20 20:09:33 +0000415 Record.push_back(D->ivar_size());
416 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
417 IEnd = D->ivar_end(); I != IEnd; ++I)
418 Writer.AddDeclRef(*I, Record);
Douglas Gregor133f4822009-04-23 22:34:55 +0000419 Writer.AddDeclRef(D->getCategoryList(), Record);
Steve Naroff33feeb02009-04-20 20:09:33 +0000420 Record.push_back(D->isForwardDecl());
421 Record.push_back(D->isImplicitInterfaceDecl());
422 Writer.AddSourceLocation(D->getClassLoc(), Record);
423 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
424 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000425 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff33feeb02009-04-20 20:09:33 +0000426}
427
428void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
429 VisitFieldDecl(D);
430 // FIXME: stable encoding for @public/@private/@protected/@package
431 Record.push_back(D->getAccessControl());
Steve Naroff30833f82009-04-21 15:12:33 +0000432 Code = pch::DECL_OBJC_IVAR;
433}
434
435void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
436 VisitObjCContainerDecl(D);
437 Record.push_back(D->isForwardDecl());
438 Writer.AddSourceLocation(D->getLocEnd(), Record);
439 Record.push_back(D->protocol_size());
440 for (ObjCProtocolDecl::protocol_iterator
441 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
442 Writer.AddDeclRef(*I, Record);
443 Code = pch::DECL_OBJC_PROTOCOL;
444}
445
446void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
447 VisitFieldDecl(D);
448 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
449}
450
451void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
452 VisitDecl(D);
453 Record.push_back(D->size());
454 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
455 Writer.AddDeclRef(*I, Record);
456 Code = pch::DECL_OBJC_CLASS;
457}
458
459void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
460 VisitDecl(D);
461 Record.push_back(D->protocol_size());
462 for (ObjCProtocolDecl::protocol_iterator
463 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
464 Writer.AddDeclRef(*I, Record);
465 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
466}
467
468void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
469 VisitObjCContainerDecl(D);
470 Writer.AddDeclRef(D->getClassInterface(), Record);
471 Record.push_back(D->protocol_size());
472 for (ObjCProtocolDecl::protocol_iterator
473 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
474 Writer.AddDeclRef(*I, Record);
475 Writer.AddDeclRef(D->getNextClassCategory(), Record);
476 Writer.AddSourceLocation(D->getLocEnd(), Record);
477 Code = pch::DECL_OBJC_CATEGORY;
478}
479
480void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
481 VisitNamedDecl(D);
482 Writer.AddDeclRef(D->getClassInterface(), Record);
483 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
484}
485
486void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
487 VisitNamedDecl(D);
Douglas Gregor70e5a142009-04-22 23:20:34 +0000488 Writer.AddTypeRef(D->getType(), Record);
489 // FIXME: stable encoding
490 Record.push_back((unsigned)D->getPropertyAttributes());
491 // FIXME: stable encoding
492 Record.push_back((unsigned)D->getPropertyImplementation());
493 Writer.AddDeclarationName(D->getGetterName(), Record);
494 Writer.AddDeclarationName(D->getSetterName(), Record);
495 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
496 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
497 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000498 Code = pch::DECL_OBJC_PROPERTY;
499}
500
501void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000502 VisitNamedDecl(D);
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000503 Writer.AddDeclRef(D->getClassInterface(), Record);
504 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000505 // Abstract class (no need to define a stable pch::DECL code).
506}
507
508void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
509 VisitObjCImplDecl(D);
Douglas Gregor10b0e1f2009-04-23 02:53:57 +0000510 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000511 Code = pch::DECL_OBJC_CATEGORY_IMPL;
512}
513
514void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
515 VisitObjCImplDecl(D);
Douglas Gregor8f36aba2009-04-23 03:23:08 +0000516 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000517 Code = pch::DECL_OBJC_IMPLEMENTATION;
518}
519
520void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
521 VisitDecl(D);
Douglas Gregor8818c4f2009-04-23 03:43:53 +0000522 Writer.AddSourceLocation(D->getLocStart(), Record);
523 Writer.AddDeclRef(D->getPropertyDecl(), Record);
524 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff30833f82009-04-21 15:12:33 +0000525 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff33feeb02009-04-20 20:09:33 +0000526}
527
Douglas Gregor8c700062009-04-13 21:20:57 +0000528void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
529 VisitValueDecl(D);
530 Record.push_back(D->isMutable());
Douglas Gregor0b748912009-04-14 21:18:50 +0000531 Record.push_back(D->getBitWidth()? 1 : 0);
532 if (D->getBitWidth())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000533 Writer.AddStmt(D->getBitWidth());
Douglas Gregor8c700062009-04-13 21:20:57 +0000534 Code = pch::DECL_FIELD;
535}
536
Douglas Gregor2cf26342009-04-09 22:27:44 +0000537void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
538 VisitValueDecl(D);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000539 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor2cf26342009-04-09 22:27:44 +0000540 Record.push_back(D->isThreadSpecified());
541 Record.push_back(D->hasCXXDirectInitializer());
542 Record.push_back(D->isDeclaredInCondition());
543 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
544 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregor0b748912009-04-14 21:18:50 +0000545 Record.push_back(D->getInit()? 1 : 0);
546 if (D->getInit())
Douglas Gregorc9490c02009-04-16 22:23:12 +0000547 Writer.AddStmt(D->getInit());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000548 Code = pch::DECL_VAR;
549}
550
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000551void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
552 VisitVarDecl(D);
553 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000554 // FIXME: emit default argument (C++)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000555 // FIXME: why isn't the "default argument" just stored as the initializer
556 // in VarDecl?
557 Code = pch::DECL_PARM_VAR;
558}
559
560void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
561 VisitParmVarDecl(D);
562 Writer.AddTypeRef(D->getOriginalType(), Record);
563 Code = pch::DECL_ORIGINAL_PARM_VAR;
564}
565
Douglas Gregor1028bc62009-04-13 22:49:25 +0000566void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
567 VisitDecl(D);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000568 Writer.AddStmt(D->getAsmString());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000569 Code = pch::DECL_FILE_SCOPE_ASM;
570}
571
572void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
573 VisitDecl(D);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000574 Writer.AddStmt(D->getBody());
Douglas Gregor1028bc62009-04-13 22:49:25 +0000575 Record.push_back(D->param_size());
576 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
577 P != PEnd; ++P)
578 Writer.AddDeclRef(*P, Record);
579 Code = pch::DECL_BLOCK;
580}
581
Douglas Gregor2cf26342009-04-09 22:27:44 +0000582/// \brief Emit the DeclContext part of a declaration context decl.
583///
584/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
585/// block for this declaration context is stored. May be 0 to indicate
586/// that there are no declarations stored within this context.
587///
588/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
589/// block for this declaration context is stored. May be 0 to indicate
590/// that there are no declarations visible from this context. Note
591/// that this value will not be emitted for non-primary declaration
592/// contexts.
593void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
594 uint64_t VisibleOffset) {
595 Record.push_back(LexicalOffset);
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000596 Record.push_back(VisibleOffset);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000597}
598
599//===----------------------------------------------------------------------===//
Douglas Gregor0b748912009-04-14 21:18:50 +0000600// Statement/expression serialization
601//===----------------------------------------------------------------------===//
602namespace {
603 class VISIBILITY_HIDDEN PCHStmtWriter
604 : public StmtVisitor<PCHStmtWriter, void> {
605
606 PCHWriter &Writer;
607 PCHWriter::RecordData &Record;
608
609 public:
610 pch::StmtCode Code;
611
612 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
613 : Writer(Writer), Record(Record) { }
614
Douglas Gregor025452f2009-04-17 00:04:06 +0000615 void VisitStmt(Stmt *S);
616 void VisitNullStmt(NullStmt *S);
617 void VisitCompoundStmt(CompoundStmt *S);
618 void VisitSwitchCase(SwitchCase *S);
619 void VisitCaseStmt(CaseStmt *S);
620 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000621 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000622 void VisitIfStmt(IfStmt *S);
623 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000624 void VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000625 void VisitDoStmt(DoStmt *S);
626 void VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000627 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000628 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000629 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000630 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000631 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000632 void VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000633 void VisitAsmStmt(AsmStmt *S);
Douglas Gregor0b748912009-04-14 21:18:50 +0000634 void VisitExpr(Expr *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000635 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000636 void VisitDeclRefExpr(DeclRefExpr *E);
637 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor17fc2232009-04-14 21:55:33 +0000638 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000639 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000640 void VisitStringLiteral(StringLiteral *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000641 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000642 void VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000643 void VisitUnaryOperator(UnaryOperator *E);
644 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000645 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000646 void VisitCallExpr(CallExpr *E);
647 void VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000648 void VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000649 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000650 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
651 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000652 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000653 void VisitExplicitCastExpr(ExplicitCastExpr *E);
654 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000655 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000656 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000657 void VisitInitListExpr(InitListExpr *E);
658 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
659 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000660 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000661 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000662 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000663 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
664 void VisitChooseExpr(ChooseExpr *E);
665 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000666 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000667 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000668 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000669
670 // Objective-C
Chris Lattner3a57a372009-04-22 06:29:42 +0000671 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000672 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000673 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
674 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000675 };
676}
677
Douglas Gregor025452f2009-04-17 00:04:06 +0000678void PCHStmtWriter::VisitStmt(Stmt *S) {
679}
680
681void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
682 VisitStmt(S);
683 Writer.AddSourceLocation(S->getSemiLoc(), Record);
684 Code = pch::STMT_NULL;
685}
686
687void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
688 VisitStmt(S);
689 Record.push_back(S->size());
690 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
691 CS != CSEnd; ++CS)
692 Writer.WriteSubStmt(*CS);
693 Writer.AddSourceLocation(S->getLBracLoc(), Record);
694 Writer.AddSourceLocation(S->getRBracLoc(), Record);
695 Code = pch::STMT_COMPOUND;
696}
697
698void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
699 VisitStmt(S);
700 Record.push_back(Writer.RecordSwitchCaseID(S));
701}
702
703void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
704 VisitSwitchCase(S);
705 Writer.WriteSubStmt(S->getLHS());
706 Writer.WriteSubStmt(S->getRHS());
707 Writer.WriteSubStmt(S->getSubStmt());
708 Writer.AddSourceLocation(S->getCaseLoc(), Record);
709 Code = pch::STMT_CASE;
710}
711
712void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
713 VisitSwitchCase(S);
714 Writer.WriteSubStmt(S->getSubStmt());
715 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
716 Code = pch::STMT_DEFAULT;
717}
718
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000719void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
720 VisitStmt(S);
721 Writer.AddIdentifierRef(S->getID(), Record);
722 Writer.WriteSubStmt(S->getSubStmt());
723 Writer.AddSourceLocation(S->getIdentLoc(), Record);
724 Record.push_back(Writer.GetLabelID(S));
725 Code = pch::STMT_LABEL;
726}
727
Douglas Gregor025452f2009-04-17 00:04:06 +0000728void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
729 VisitStmt(S);
730 Writer.WriteSubStmt(S->getCond());
731 Writer.WriteSubStmt(S->getThen());
732 Writer.WriteSubStmt(S->getElse());
733 Writer.AddSourceLocation(S->getIfLoc(), Record);
734 Code = pch::STMT_IF;
735}
736
737void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
738 VisitStmt(S);
739 Writer.WriteSubStmt(S->getCond());
740 Writer.WriteSubStmt(S->getBody());
741 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
742 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
743 SC = SC->getNextSwitchCase())
744 Record.push_back(Writer.getSwitchCaseID(SC));
745 Code = pch::STMT_SWITCH;
746}
747
Douglas Gregord921cf92009-04-17 00:16:09 +0000748void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
749 VisitStmt(S);
750 Writer.WriteSubStmt(S->getCond());
751 Writer.WriteSubStmt(S->getBody());
752 Writer.AddSourceLocation(S->getWhileLoc(), Record);
753 Code = pch::STMT_WHILE;
754}
755
Douglas Gregor67d82492009-04-17 00:29:51 +0000756void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
757 VisitStmt(S);
758 Writer.WriteSubStmt(S->getCond());
759 Writer.WriteSubStmt(S->getBody());
760 Writer.AddSourceLocation(S->getDoLoc(), Record);
761 Code = pch::STMT_DO;
762}
763
764void PCHStmtWriter::VisitForStmt(ForStmt *S) {
765 VisitStmt(S);
766 Writer.WriteSubStmt(S->getInit());
767 Writer.WriteSubStmt(S->getCond());
768 Writer.WriteSubStmt(S->getInc());
769 Writer.WriteSubStmt(S->getBody());
770 Writer.AddSourceLocation(S->getForLoc(), Record);
771 Code = pch::STMT_FOR;
772}
773
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000774void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
775 VisitStmt(S);
776 Record.push_back(Writer.GetLabelID(S->getLabel()));
777 Writer.AddSourceLocation(S->getGotoLoc(), Record);
778 Writer.AddSourceLocation(S->getLabelLoc(), Record);
779 Code = pch::STMT_GOTO;
780}
781
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000782void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
783 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000784 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000785 Writer.WriteSubStmt(S->getTarget());
786 Code = pch::STMT_INDIRECT_GOTO;
787}
788
Douglas Gregord921cf92009-04-17 00:16:09 +0000789void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
790 VisitStmt(S);
791 Writer.AddSourceLocation(S->getContinueLoc(), Record);
792 Code = pch::STMT_CONTINUE;
793}
794
Douglas Gregor025452f2009-04-17 00:04:06 +0000795void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
796 VisitStmt(S);
797 Writer.AddSourceLocation(S->getBreakLoc(), Record);
798 Code = pch::STMT_BREAK;
799}
800
Douglas Gregor0de9d882009-04-17 16:34:57 +0000801void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
802 VisitStmt(S);
803 Writer.WriteSubStmt(S->getRetValue());
804 Writer.AddSourceLocation(S->getReturnLoc(), Record);
805 Code = pch::STMT_RETURN;
806}
807
Douglas Gregor84f21702009-04-17 16:55:36 +0000808void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
809 VisitStmt(S);
810 Writer.AddSourceLocation(S->getStartLoc(), Record);
811 Writer.AddSourceLocation(S->getEndLoc(), Record);
812 DeclGroupRef DG = S->getDeclGroup();
813 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
814 Writer.AddDeclRef(*D, Record);
815 Code = pch::STMT_DECL;
816}
817
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000818void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
819 VisitStmt(S);
820 Record.push_back(S->getNumOutputs());
821 Record.push_back(S->getNumInputs());
822 Record.push_back(S->getNumClobbers());
823 Writer.AddSourceLocation(S->getAsmLoc(), Record);
824 Writer.AddSourceLocation(S->getRParenLoc(), Record);
825 Record.push_back(S->isVolatile());
826 Record.push_back(S->isSimple());
827 Writer.WriteSubStmt(S->getAsmString());
828
829 // Outputs
830 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
831 Writer.AddString(S->getOutputName(I), Record);
832 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
833 Writer.WriteSubStmt(S->getOutputExpr(I));
834 }
835
836 // Inputs
837 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
838 Writer.AddString(S->getInputName(I), Record);
839 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
840 Writer.WriteSubStmt(S->getInputExpr(I));
841 }
842
843 // Clobbers
844 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
845 Writer.WriteSubStmt(S->getClobber(I));
846
847 Code = pch::STMT_ASM;
848}
849
Douglas Gregor0b748912009-04-14 21:18:50 +0000850void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000851 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000852 Writer.AddTypeRef(E->getType(), Record);
853 Record.push_back(E->isTypeDependent());
854 Record.push_back(E->isValueDependent());
855}
856
Douglas Gregor17fc2232009-04-14 21:55:33 +0000857void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
858 VisitExpr(E);
859 Writer.AddSourceLocation(E->getLocation(), Record);
860 Record.push_back(E->getIdentType()); // FIXME: stable encoding
861 Code = pch::EXPR_PREDEFINED;
862}
863
Douglas Gregor0b748912009-04-14 21:18:50 +0000864void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
865 VisitExpr(E);
866 Writer.AddDeclRef(E->getDecl(), Record);
867 Writer.AddSourceLocation(E->getLocation(), Record);
868 Code = pch::EXPR_DECL_REF;
869}
870
871void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
872 VisitExpr(E);
873 Writer.AddSourceLocation(E->getLocation(), Record);
874 Writer.AddAPInt(E->getValue(), Record);
875 Code = pch::EXPR_INTEGER_LITERAL;
876}
877
Douglas Gregor17fc2232009-04-14 21:55:33 +0000878void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
879 VisitExpr(E);
880 Writer.AddAPFloat(E->getValue(), Record);
881 Record.push_back(E->isExact());
882 Writer.AddSourceLocation(E->getLocation(), Record);
883 Code = pch::EXPR_FLOATING_LITERAL;
884}
885
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000886void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
887 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000888 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000889 Code = pch::EXPR_IMAGINARY_LITERAL;
890}
891
Douglas Gregor673ecd62009-04-15 16:35:07 +0000892void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
893 VisitExpr(E);
894 Record.push_back(E->getByteLength());
895 Record.push_back(E->getNumConcatenated());
896 Record.push_back(E->isWide());
897 // FIXME: String data should be stored as a blob at the end of the
898 // StringLiteral. However, we can't do so now because we have no
899 // provision for coping with abbreviations when we're jumping around
900 // the PCH file during deserialization.
901 Record.insert(Record.end(),
902 E->getStrData(), E->getStrData() + E->getByteLength());
903 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
904 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
905 Code = pch::EXPR_STRING_LITERAL;
906}
907
Douglas Gregor0b748912009-04-14 21:18:50 +0000908void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
909 VisitExpr(E);
910 Record.push_back(E->getValue());
911 Writer.AddSourceLocation(E->getLoc(), Record);
912 Record.push_back(E->isWide());
913 Code = pch::EXPR_CHARACTER_LITERAL;
914}
915
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000916void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
917 VisitExpr(E);
918 Writer.AddSourceLocation(E->getLParen(), Record);
919 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000920 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000921 Code = pch::EXPR_PAREN;
922}
923
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000924void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
925 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000926 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000927 Record.push_back(E->getOpcode()); // FIXME: stable encoding
928 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
929 Code = pch::EXPR_UNARY_OPERATOR;
930}
931
932void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
933 VisitExpr(E);
934 Record.push_back(E->isSizeOf());
935 if (E->isArgumentType())
936 Writer.AddTypeRef(E->getArgumentType(), Record);
937 else {
938 Record.push_back(0);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000939 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000940 }
941 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
942 Writer.AddSourceLocation(E->getRParenLoc(), Record);
943 Code = pch::EXPR_SIZEOF_ALIGN_OF;
944}
945
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000946void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
947 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000948 Writer.WriteSubStmt(E->getLHS());
949 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000950 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
951 Code = pch::EXPR_ARRAY_SUBSCRIPT;
952}
953
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000954void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
955 VisitExpr(E);
956 Record.push_back(E->getNumArgs());
957 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000958 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000959 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
960 Arg != ArgEnd; ++Arg)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000961 Writer.WriteSubStmt(*Arg);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000962 Code = pch::EXPR_CALL;
963}
964
965void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
966 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000967 Writer.WriteSubStmt(E->getBase());
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000968 Writer.AddDeclRef(E->getMemberDecl(), Record);
969 Writer.AddSourceLocation(E->getMemberLoc(), Record);
970 Record.push_back(E->isArrow());
971 Code = pch::EXPR_MEMBER;
972}
973
Douglas Gregor087fd532009-04-14 23:32:43 +0000974void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
975 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000976 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor087fd532009-04-14 23:32:43 +0000977}
978
Douglas Gregordb600c32009-04-15 00:25:59 +0000979void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
980 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000981 Writer.WriteSubStmt(E->getLHS());
982 Writer.WriteSubStmt(E->getRHS());
Douglas Gregordb600c32009-04-15 00:25:59 +0000983 Record.push_back(E->getOpcode()); // FIXME: stable encoding
984 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
985 Code = pch::EXPR_BINARY_OPERATOR;
986}
987
Douglas Gregorad90e962009-04-15 22:40:36 +0000988void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
989 VisitBinaryOperator(E);
990 Writer.AddTypeRef(E->getComputationLHSType(), Record);
991 Writer.AddTypeRef(E->getComputationResultType(), Record);
992 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
993}
994
995void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
996 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000997 Writer.WriteSubStmt(E->getCond());
998 Writer.WriteSubStmt(E->getLHS());
999 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorad90e962009-04-15 22:40:36 +00001000 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1001}
1002
Douglas Gregor087fd532009-04-14 23:32:43 +00001003void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1004 VisitCastExpr(E);
1005 Record.push_back(E->isLvalueCast());
1006 Code = pch::EXPR_IMPLICIT_CAST;
1007}
1008
Douglas Gregordb600c32009-04-15 00:25:59 +00001009void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1010 VisitCastExpr(E);
1011 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1012}
1013
1014void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1015 VisitExplicitCastExpr(E);
1016 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1017 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1018 Code = pch::EXPR_CSTYLE_CAST;
1019}
1020
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001021void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1022 VisitExpr(E);
1023 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001024 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorba6d7e72009-04-16 02:33:48 +00001025 Record.push_back(E->isFileScope());
1026 Code = pch::EXPR_COMPOUND_LITERAL;
1027}
1028
Douglas Gregord3c98a02009-04-15 23:02:49 +00001029void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1030 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001031 Writer.WriteSubStmt(E->getBase());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001032 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1033 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1034 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1035}
1036
Douglas Gregord077d752009-04-16 00:55:48 +00001037void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1038 VisitExpr(E);
1039 Record.push_back(E->getNumInits());
1040 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001041 Writer.WriteSubStmt(E->getInit(I));
1042 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregord077d752009-04-16 00:55:48 +00001043 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1044 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1045 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1046 Record.push_back(E->hadArrayRangeDesignator());
1047 Code = pch::EXPR_INIT_LIST;
1048}
1049
1050void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1051 VisitExpr(E);
1052 Record.push_back(E->getNumSubExprs());
1053 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001054 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregord077d752009-04-16 00:55:48 +00001055 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1056 Record.push_back(E->usesGNUSyntax());
1057 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1058 DEnd = E->designators_end();
1059 D != DEnd; ++D) {
1060 if (D->isFieldDesignator()) {
1061 if (FieldDecl *Field = D->getField()) {
1062 Record.push_back(pch::DESIG_FIELD_DECL);
1063 Writer.AddDeclRef(Field, Record);
1064 } else {
1065 Record.push_back(pch::DESIG_FIELD_NAME);
1066 Writer.AddIdentifierRef(D->getFieldName(), Record);
1067 }
1068 Writer.AddSourceLocation(D->getDotLoc(), Record);
1069 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1070 } else if (D->isArrayDesignator()) {
1071 Record.push_back(pch::DESIG_ARRAY);
1072 Record.push_back(D->getFirstExprIndex());
1073 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1074 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1075 } else {
1076 assert(D->isArrayRangeDesignator() && "Unknown designator");
1077 Record.push_back(pch::DESIG_ARRAY_RANGE);
1078 Record.push_back(D->getFirstExprIndex());
1079 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1080 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1081 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1082 }
1083 }
1084 Code = pch::EXPR_DESIGNATED_INIT;
1085}
1086
1087void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1088 VisitExpr(E);
1089 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1090}
1091
Douglas Gregord3c98a02009-04-15 23:02:49 +00001092void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1093 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001094 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregord3c98a02009-04-15 23:02:49 +00001095 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1096 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1097 Code = pch::EXPR_VA_ARG;
1098}
1099
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00001100void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1101 VisitExpr(E);
1102 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1103 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1104 Record.push_back(Writer.GetLabelID(E->getLabel()));
1105 Code = pch::EXPR_ADDR_LABEL;
1106}
1107
Douglas Gregor6a2dd552009-04-17 19:05:30 +00001108void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1109 VisitExpr(E);
1110 Writer.WriteSubStmt(E->getSubStmt());
1111 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1112 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1113 Code = pch::EXPR_STMT;
1114}
1115
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001116void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1117 VisitExpr(E);
1118 Writer.AddTypeRef(E->getArgType1(), Record);
1119 Writer.AddTypeRef(E->getArgType2(), Record);
1120 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1121 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1122 Code = pch::EXPR_TYPES_COMPATIBLE;
1123}
1124
1125void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1126 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001127 Writer.WriteSubStmt(E->getCond());
1128 Writer.WriteSubStmt(E->getLHS());
1129 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001130 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1131 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1132 Code = pch::EXPR_CHOOSE;
1133}
1134
1135void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1136 VisitExpr(E);
1137 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1138 Code = pch::EXPR_GNU_NULL;
1139}
1140
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001141void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1142 VisitExpr(E);
1143 Record.push_back(E->getNumSubExprs());
1144 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001145 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001146 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1147 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1148 Code = pch::EXPR_SHUFFLE_VECTOR;
1149}
1150
Douglas Gregor84af7c22009-04-17 19:21:43 +00001151void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1152 VisitExpr(E);
1153 Writer.AddDeclRef(E->getBlockDecl(), Record);
1154 Record.push_back(E->hasBlockDeclRefExprs());
1155 Code = pch::EXPR_BLOCK;
1156}
1157
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001158void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1159 VisitExpr(E);
1160 Writer.AddDeclRef(E->getDecl(), Record);
1161 Writer.AddSourceLocation(E->getLocation(), Record);
1162 Record.push_back(E->isByRef());
1163 Code = pch::EXPR_BLOCK_DECL_REF;
1164}
1165
Douglas Gregor0b748912009-04-14 21:18:50 +00001166//===----------------------------------------------------------------------===//
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001167// Objective-C Expressions and Statements.
1168//===----------------------------------------------------------------------===//
1169
Chris Lattner3a57a372009-04-22 06:29:42 +00001170void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1171 VisitExpr(E);
1172 Writer.WriteSubStmt(E->getString());
1173 Writer.AddSourceLocation(E->getAtLoc(), Record);
1174 Code = pch::EXPR_OBJC_STRING_LITERAL;
1175}
1176
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001177void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1178 VisitExpr(E);
1179 Writer.AddTypeRef(E->getEncodedType(), Record);
1180 Writer.AddSourceLocation(E->getAtLoc(), Record);
1181 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1182 Code = pch::EXPR_OBJC_ENCODE;
1183}
1184
Chris Lattner3a57a372009-04-22 06:29:42 +00001185void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1186 VisitExpr(E);
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001187 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattner3a57a372009-04-22 06:29:42 +00001188 Writer.AddSourceLocation(E->getAtLoc(), Record);
1189 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1190 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1191}
1192
1193void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1194 VisitExpr(E);
1195 Writer.AddDeclRef(E->getProtocol(), Record);
1196 Writer.AddSourceLocation(E->getAtLoc(), Record);
1197 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1198 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1199}
1200
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001201
1202//===----------------------------------------------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00001203// PCHWriter Implementation
1204//===----------------------------------------------------------------------===//
1205
Douglas Gregor2bec0412009-04-10 21:16:55 +00001206/// \brief Write the target triple (e.g., i686-apple-darwin9).
1207void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1208 using namespace llvm;
1209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1210 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2bec0412009-04-10 21:16:55 +00001213
1214 RecordData Record;
1215 Record.push_back(pch::TARGET_TRIPLE);
1216 const char *Triple = Target.getTargetTriple();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001217 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregor2bec0412009-04-10 21:16:55 +00001218}
1219
1220/// \brief Write the LangOptions structure.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001221void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1222 RecordData Record;
1223 Record.push_back(LangOpts.Trigraphs);
1224 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1225 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1226 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1227 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1228 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1229 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1230 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1231 Record.push_back(LangOpts.C99); // C99 Support
1232 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1233 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1234 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1235 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1236 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1237
1238 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1239 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1240 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1241
1242 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1243 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1244 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1245 Record.push_back(LangOpts.LaxVectorConversions);
1246 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1247
1248 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1249 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1250 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1251
1252 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1253 // by locks.
1254 Record.push_back(LangOpts.Blocks); // block extension to C
1255 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1256 // they are unused.
1257 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1258 // (modulo the platform support).
1259
1260 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1261 // signed integer arithmetic overflows.
1262
1263 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1264 // may be ripped out at any time.
1265
1266 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1267 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1268 // defined.
1269 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1270 // opposed to __DYNAMIC__).
1271 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1272
1273 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1274 // used (instead of C99 semantics).
1275 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1276 Record.push_back(LangOpts.getGCMode());
1277 Record.push_back(LangOpts.getVisibilityMode());
1278 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001279 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001280}
1281
Douglas Gregor14f79002009-04-10 03:52:48 +00001282//===----------------------------------------------------------------------===//
1283// Source Manager Serialization
1284//===----------------------------------------------------------------------===//
1285
1286/// \brief Create an abbreviation for the SLocEntry that refers to a
1287/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001288static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001289 using namespace llvm;
1290 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1291 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor14f79002009-04-10 03:52:48 +00001296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001297 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001298}
1299
1300/// \brief Create an abbreviation for the SLocEntry that refers to a
1301/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001302static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001303 using namespace llvm;
1304 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1305 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001311 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001312}
1313
1314/// \brief Create an abbreviation for the SLocEntry that refers to a
1315/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001316static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001317 using namespace llvm;
1318 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1319 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001321 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001322}
1323
1324/// \brief Create an abbreviation for the SLocEntry that refers to an
1325/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001326static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001327 using namespace llvm;
1328 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1329 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001335 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001336}
1337
1338/// \brief Writes the block containing the serialized form of the
1339/// source manager.
1340///
1341/// TODO: We should probably use an on-disk hash table (stored in a
1342/// blob), indexed based on the file name, so that we only create
1343/// entries for files that we actually need. In the common case (no
1344/// errors), we probably won't have to create file entries for any of
1345/// the files in the AST.
1346void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001347 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001348 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001349
1350 // Abbreviations for the various kinds of source-location entries.
1351 int SLocFileAbbrv = -1;
1352 int SLocBufferAbbrv = -1;
1353 int SLocBufferBlobAbbrv = -1;
1354 int SLocInstantiationAbbrv = -1;
1355
1356 // Write out the source location entry table. We skip the first
1357 // entry, which is always the same dummy entry.
1358 RecordData Record;
1359 for (SourceManager::sloc_entry_iterator
1360 SLoc = SourceMgr.sloc_entry_begin() + 1,
1361 SLocEnd = SourceMgr.sloc_entry_end();
1362 SLoc != SLocEnd; ++SLoc) {
1363 // Figure out which record code to use.
1364 unsigned Code;
1365 if (SLoc->isFile()) {
1366 if (SLoc->getFile().getContentCache()->Entry)
1367 Code = pch::SM_SLOC_FILE_ENTRY;
1368 else
1369 Code = pch::SM_SLOC_BUFFER_ENTRY;
1370 } else
1371 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1372 Record.push_back(Code);
1373
1374 Record.push_back(SLoc->getOffset());
1375 if (SLoc->isFile()) {
1376 const SrcMgr::FileInfo &File = SLoc->getFile();
1377 Record.push_back(File.getIncludeLoc().getRawEncoding());
1378 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregorbd945002009-04-13 16:31:14 +00001379 Record.push_back(File.hasLineDirectives());
Douglas Gregor14f79002009-04-10 03:52:48 +00001380
1381 const SrcMgr::ContentCache *Content = File.getContentCache();
1382 if (Content->Entry) {
1383 // The source location entry is a file. The blob associated
1384 // with this entry is the file name.
1385 if (SLocFileAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001386 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1387 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001388 Content->Entry->getName(),
1389 strlen(Content->Entry->getName()));
1390 } else {
1391 // The source location entry is a buffer. The blob associated
1392 // with this entry contains the contents of the buffer.
1393 if (SLocBufferAbbrv == -1) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00001394 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1395 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 }
1397
1398 // We add one to the size so that we capture the trailing NULL
1399 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1400 // the reader side).
1401 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1402 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc9490c02009-04-16 22:23:12 +00001403 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregor14f79002009-04-10 03:52:48 +00001404 Record.clear();
1405 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001406 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregor14f79002009-04-10 03:52:48 +00001407 Buffer->getBufferStart(),
1408 Buffer->getBufferSize() + 1);
1409 }
1410 } else {
1411 // The source location entry is an instantiation.
1412 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1413 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1414 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1415 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1416
Douglas Gregorf60e9912009-04-15 18:05:10 +00001417 // Compute the token length for this macro expansion.
1418 unsigned NextOffset = SourceMgr.getNextOffset();
1419 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1420 if (++NextSLoc != SLocEnd)
1421 NextOffset = NextSLoc->getOffset();
1422 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1423
Douglas Gregor14f79002009-04-10 03:52:48 +00001424 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001425 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1426 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregor14f79002009-04-10 03:52:48 +00001427 }
1428
1429 Record.clear();
1430 }
1431
Douglas Gregorbd945002009-04-13 16:31:14 +00001432 // Write the line table.
1433 if (SourceMgr.hasLineTable()) {
1434 LineTableInfo &LineTable = SourceMgr.getLineTable();
1435
1436 // Emit the file names
1437 Record.push_back(LineTable.getNumFilenames());
1438 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1439 // Emit the file name
1440 const char *Filename = LineTable.getFilename(I);
1441 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1442 Record.push_back(FilenameLen);
1443 if (FilenameLen)
1444 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1445 }
1446
1447 // Emit the line entries
1448 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1449 L != LEnd; ++L) {
1450 // Emit the file ID
1451 Record.push_back(L->first);
1452
1453 // Emit the line entries
1454 Record.push_back(L->second.size());
1455 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1456 LEEnd = L->second.end();
1457 LE != LEEnd; ++LE) {
1458 Record.push_back(LE->FileOffset);
1459 Record.push_back(LE->LineNo);
1460 Record.push_back(LE->FilenameID);
1461 Record.push_back((unsigned)LE->FileKind);
1462 Record.push_back(LE->IncludeOffset);
1463 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001464 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001465 }
1466 }
1467
Douglas Gregorc9490c02009-04-16 22:23:12 +00001468 Stream.ExitBlock();
Douglas Gregor14f79002009-04-10 03:52:48 +00001469}
1470
Chris Lattner0b1fb982009-04-10 17:15:23 +00001471/// \brief Writes the block containing the serialized form of the
1472/// preprocessor.
1473///
Chris Lattnerdf961c22009-04-10 18:08:30 +00001474void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001475 // Enter the preprocessor block.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001476 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattnerf04ad692009-04-10 17:16:57 +00001477
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001478 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1479 // FIXME: use diagnostics subsystem for localization etc.
1480 if (PP.SawDateOrTime())
1481 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattnerf04ad692009-04-10 17:16:57 +00001482
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001483 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001484
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001485 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1486 if (PP.getCounterValue() != 0) {
1487 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001488 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001489 Record.clear();
1490 }
1491
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001492 // Loop over all the macro definitions that are live at the end of the file,
1493 // emitting each to the PP section.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001494 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1495 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001496 // FIXME: This emits macros in hash table order, we should do it in a stable
1497 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001498 MacroInfo *MI = I->second;
1499
1500 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1501 // been redefined by the header (in which case they are not isBuiltinMacro).
1502 if (MI->isBuiltinMacro())
1503 continue;
1504
Douglas Gregor37e26842009-04-21 23:56:24 +00001505 // FIXME: Remove this identifier reference?
Chris Lattner7356a312009-04-11 21:15:38 +00001506 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001507 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001508 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1509 Record.push_back(MI->isUsed());
1510
1511 unsigned Code;
1512 if (MI->isObjectLike()) {
1513 Code = pch::PP_MACRO_OBJECT_LIKE;
1514 } else {
1515 Code = pch::PP_MACRO_FUNCTION_LIKE;
1516
1517 Record.push_back(MI->isC99Varargs());
1518 Record.push_back(MI->isGNUVarargs());
1519 Record.push_back(MI->getNumArgs());
1520 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1521 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001522 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001523 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001524 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001525 Record.clear();
1526
Chris Lattnerdf961c22009-04-10 18:08:30 +00001527 // Emit the tokens array.
1528 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1529 // Note that we know that the preprocessor does not have any annotation
1530 // tokens in it because they are created by the parser, and thus can't be
1531 // in a macro definition.
1532 const Token &Tok = MI->getReplacementToken(TokNo);
1533
1534 Record.push_back(Tok.getLocation().getRawEncoding());
1535 Record.push_back(Tok.getLength());
1536
Chris Lattnerdf961c22009-04-10 18:08:30 +00001537 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1538 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001539 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001540
1541 // FIXME: Should translate token kind to a stable encoding.
1542 Record.push_back(Tok.getKind());
1543 // FIXME: Should translate token flags to a stable encoding.
1544 Record.push_back(Tok.getFlags());
1545
Douglas Gregorc9490c02009-04-16 22:23:12 +00001546 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001547 Record.clear();
1548 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001549 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001550 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001551
1552 // Loop over all the header files.
1553 HeaderSearch &HS = PP.getHeaderSearchInfo();
1554 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1555 E = HS.header_file_end();
1556 I != E; ++I) {
1557 Record.push_back((*I).isImport);
1558 Record.push_back((*I).DirInfo);
1559 Record.push_back((*I).NumIncludes);
1560 if ((*I).ControllingMacro)
1561 AddIdentifierRef((*I).ControllingMacro, Record);
1562 else
1563 Record.push_back(0);
1564 Stream.EmitRecord(pch::PP_HEADER_FILE_INFO, Record);
1565 Record.clear();
1566 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001567 Stream.ExitBlock();
Chris Lattner0b1fb982009-04-10 17:15:23 +00001568}
1569
1570
Douglas Gregor2cf26342009-04-09 22:27:44 +00001571/// \brief Write the representation of a type to the PCH stream.
1572void PCHWriter::WriteType(const Type *T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001573 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001574 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001575 ID = NextTypeID++;
1576
1577 // Record the offset for this type.
1578 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001579 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001580 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1581 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001582 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001583 }
1584
1585 RecordData Record;
1586
1587 // Emit the type's representation.
1588 PCHTypeWriter W(*this, Record);
1589 switch (T->getTypeClass()) {
1590 // For all of the concrete, non-dependent types, call the
1591 // appropriate visitor function.
1592#define TYPE(Class, Base) \
1593 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1594#define ABSTRACT_TYPE(Class, Base)
1595#define DEPENDENT_TYPE(Class, Base)
1596#include "clang/AST/TypeNodes.def"
1597
1598 // For all of the dependent type nodes (which only occur in C++
1599 // templates), produce an error.
1600#define TYPE(Class, Base)
1601#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1602#include "clang/AST/TypeNodes.def"
1603 assert(false && "Cannot serialize dependent type nodes");
1604 break;
1605 }
1606
1607 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001608 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001609
1610 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001611 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001612}
1613
1614/// \brief Write a block containing all of the types.
1615void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001616 // Enter the types block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001617 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001618
1619 // Emit all of the types in the ASTContext
1620 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1621 TEnd = Context.getTypes().end();
1622 T != TEnd; ++T) {
1623 // Builtin types are never serialized.
1624 if (isa<BuiltinType>(*T))
1625 continue;
1626
1627 WriteType(*T);
1628 }
1629
1630 // Exit the types block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001631 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001632}
1633
1634/// \brief Write the block containing all of the declaration IDs
1635/// lexically declared within the given DeclContext.
1636///
1637/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1638/// bistream, or 0 if no block was written.
1639uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1640 DeclContext *DC) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001641 if (DC->decls_empty(Context))
Douglas Gregor2cf26342009-04-09 22:27:44 +00001642 return 0;
1643
Douglas Gregorc9490c02009-04-16 22:23:12 +00001644 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001645 RecordData Record;
1646 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1647 DEnd = DC->decls_end(Context);
1648 D != DEnd; ++D)
1649 AddDeclRef(*D, Record);
1650
Douglas Gregor25123082009-04-22 22:34:57 +00001651 ++NumLexicalDeclContexts;
Douglas Gregorc9490c02009-04-16 22:23:12 +00001652 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001653 return Offset;
1654}
1655
1656/// \brief Write the block containing all of the declaration IDs
1657/// visible from the given DeclContext.
1658///
1659/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1660/// bistream, or 0 if no block was written.
1661uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1662 DeclContext *DC) {
1663 if (DC->getPrimaryContext() != DC)
1664 return 0;
1665
Douglas Gregoraff22df2009-04-21 22:32:33 +00001666 // Since there is no name lookup into functions or methods, and we
1667 // perform name lookup for the translation unit via the
1668 // IdentifierInfo chains, don't bother to build a
1669 // visible-declarations table for these entities.
1670 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor58f06992009-04-18 15:49:20 +00001671 return 0;
1672
Douglas Gregor2cf26342009-04-09 22:27:44 +00001673 // Force the DeclContext to build a its name-lookup table.
1674 DC->lookup(Context, DeclarationName());
1675
1676 // Serialize the contents of the mapping used for lookup. Note that,
1677 // although we have two very different code paths, the serialized
1678 // representation is the same for both cases: a declaration name,
1679 // followed by a size, followed by references to the visible
1680 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001681 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001682 RecordData Record;
1683 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001684 if (!Map)
1685 return 0;
1686
Douglas Gregor2cf26342009-04-09 22:27:44 +00001687 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1688 D != DEnd; ++D) {
1689 AddDeclarationName(D->first, Record);
1690 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1691 Record.push_back(Result.second - Result.first);
1692 for(; Result.first != Result.second; ++Result.first)
1693 AddDeclRef(*Result.first, Record);
1694 }
1695
1696 if (Record.size() == 0)
1697 return 0;
1698
Douglas Gregorc9490c02009-04-16 22:23:12 +00001699 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001700 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001701 return Offset;
1702}
1703
1704/// \brief Write a block containing all of the declarations.
1705void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattnerf04ad692009-04-10 17:16:57 +00001706 // Enter the declarations block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001707 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001708
1709 // Emit all of the declarations.
1710 RecordData Record;
Douglas Gregor72971342009-04-18 00:02:19 +00001711 PCHDeclWriter W(*this, Context, Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001712 while (!DeclsToEmit.empty()) {
1713 // Pull the next declaration off the queue
1714 Decl *D = DeclsToEmit.front();
1715 DeclsToEmit.pop();
1716
1717 // If this declaration is also a DeclContext, write blocks for the
1718 // declarations that lexically stored inside its context and those
1719 // declarations that are visible from its context. These blocks
1720 // are written before the declaration itself so that we can put
1721 // their offsets into the record for the declaration.
1722 uint64_t LexicalOffset = 0;
1723 uint64_t VisibleOffset = 0;
1724 DeclContext *DC = dyn_cast<DeclContext>(D);
1725 if (DC) {
1726 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1727 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1728 }
1729
1730 // Determine the ID for this declaration
Douglas Gregor8038d512009-04-10 17:25:41 +00001731 pch::DeclID ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001732 if (ID == 0)
1733 ID = DeclIDs.size();
1734
1735 unsigned Index = ID - 1;
1736
1737 // Record the offset for this declaration
1738 if (DeclOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001739 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001740 else if (DeclOffsets.size() < Index) {
1741 DeclOffsets.resize(Index+1);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001742 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001743 }
1744
1745 // Build and emit a record for this declaration
1746 Record.clear();
1747 W.Code = (pch::DeclCode)0;
1748 W.Visit(D);
1749 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor70e5a142009-04-22 23:20:34 +00001750
1751 if (!W.Code) {
1752 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1753 D->getDeclKindName());
1754 assert(false && "Unhandled declaration kind while generating PCH");
1755 exit(-1);
1756 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001757 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfdd01722009-04-14 00:24:19 +00001758
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001759 // If the declaration had any attributes, write them now.
1760 if (D->hasAttrs())
1761 WriteAttributeRecord(D->getAttrs());
1762
Douglas Gregor0b748912009-04-14 21:18:50 +00001763 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001764 FlushStmts();
Douglas Gregor0b748912009-04-14 21:18:50 +00001765
Douglas Gregorfdd01722009-04-14 00:24:19 +00001766 // Note external declarations so that we can add them to a record
1767 // in the PCH file later.
1768 if (isa<FileScopeAsmDecl>(D))
1769 ExternalDefinitions.push_back(ID);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001770 }
1771
1772 // Exit the declarations block
Douglas Gregorc9490c02009-04-16 22:23:12 +00001773 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001774}
1775
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001776namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001777// Trait used for the on-disk hash table used in the method pool.
1778class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1779 PCHWriter &Writer;
1780
1781public:
1782 typedef Selector key_type;
1783 typedef key_type key_type_ref;
1784
1785 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1786 typedef const data_type& data_type_ref;
1787
1788 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1789
1790 static unsigned ComputeHash(Selector Sel) {
1791 unsigned N = Sel.getNumArgs();
1792 if (N == 0)
1793 ++N;
1794 unsigned R = 5381;
1795 for (unsigned I = 0; I != N; ++I)
1796 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1797 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1798 return R;
1799 }
1800
1801 std::pair<unsigned,unsigned>
1802 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1803 data_type_ref Methods) {
1804 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1805 clang::io::Emit16(Out, KeyLen);
1806 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1807 for (const ObjCMethodList *Method = &Methods.first; Method;
1808 Method = Method->Next)
1809 if (Method->Method)
1810 DataLen += 4;
1811 for (const ObjCMethodList *Method = &Methods.second; Method;
1812 Method = Method->Next)
1813 if (Method->Method)
1814 DataLen += 4;
1815 clang::io::Emit16(Out, DataLen);
1816 return std::make_pair(KeyLen, DataLen);
1817 }
1818
Douglas Gregora67e58c2009-04-24 21:49:02 +00001819 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned KeyLen) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001820 // FIXME: Keep track of the location of the key data (the
1821 // selector), so we can fold the selector table's storage into
1822 // this hash table.
Douglas Gregora67e58c2009-04-24 21:49:02 +00001823 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001824 unsigned N = Sel.getNumArgs();
1825 clang::io::Emit16(Out, N);
1826 if (N == 0)
1827 N = 1;
1828 for (unsigned I = 0; I != N; ++I)
1829 clang::io::Emit32(Out,
1830 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001831
1832 assert(Out.tell() - Start == KeyLen && "Key length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001833 }
1834
1835 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001836 data_type_ref Methods, unsigned DataLen) {
1837 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001838 unsigned NumInstanceMethods = 0;
1839 for (const ObjCMethodList *Method = &Methods.first; Method;
1840 Method = Method->Next)
1841 if (Method->Method)
1842 ++NumInstanceMethods;
1843
1844 unsigned NumFactoryMethods = 0;
1845 for (const ObjCMethodList *Method = &Methods.second; Method;
1846 Method = Method->Next)
1847 if (Method->Method)
1848 ++NumFactoryMethods;
1849
1850 clang::io::Emit16(Out, NumInstanceMethods);
1851 clang::io::Emit16(Out, NumFactoryMethods);
1852 for (const ObjCMethodList *Method = &Methods.first; Method;
1853 Method = Method->Next)
1854 if (Method->Method)
1855 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001856 for (const ObjCMethodList *Method = &Methods.second; Method;
1857 Method = Method->Next)
1858 if (Method->Method)
1859 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001860
1861 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001862 }
1863};
1864} // end anonymous namespace
1865
1866/// \brief Write the method pool into the PCH file.
1867///
1868/// The method pool contains both instance and factory methods, stored
1869/// in an on-disk hash table indexed by the selector.
1870void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1871 using namespace llvm;
1872
1873 // Create and write out the blob that contains the instance and
1874 // factor method pools.
1875 bool Empty = true;
1876 {
1877 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1878
1879 // Create the on-disk hash table representation. Start by
1880 // iterating through the instance method pool.
1881 PCHMethodPoolTrait::key_type Key;
1882 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1883 Instance = SemaRef.InstanceMethodPool.begin(),
1884 InstanceEnd = SemaRef.InstanceMethodPool.end();
1885 Instance != InstanceEnd; ++Instance) {
1886 // Check whether there is a factory method with the same
1887 // selector.
1888 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1889 = SemaRef.FactoryMethodPool.find(Instance->first);
1890
1891 if (Factory == SemaRef.FactoryMethodPool.end())
1892 Generator.insert(Instance->first,
1893 std::make_pair(Instance->second,
1894 ObjCMethodList()));
1895 else
1896 Generator.insert(Instance->first,
1897 std::make_pair(Instance->second, Factory->second));
1898
1899 Empty = false;
1900 }
1901
1902 // Now iterate through the factory method pool, to pick up any
1903 // selectors that weren't already in the instance method pool.
1904 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1905 Factory = SemaRef.FactoryMethodPool.begin(),
1906 FactoryEnd = SemaRef.FactoryMethodPool.end();
1907 Factory != FactoryEnd; ++Factory) {
1908 // Check whether there is an instance method with the same
1909 // selector. If so, there is no work to do here.
1910 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1911 = SemaRef.InstanceMethodPool.find(Factory->first);
1912
1913 if (Instance == SemaRef.InstanceMethodPool.end())
1914 Generator.insert(Factory->first,
1915 std::make_pair(ObjCMethodList(), Factory->second));
1916
1917 Empty = false;
1918 }
1919
1920 if (Empty)
1921 return;
1922
1923 // Create the on-disk hash table in a buffer.
1924 llvm::SmallVector<char, 4096> MethodPool;
1925 uint32_t BucketOffset;
1926 {
1927 PCHMethodPoolTrait Trait(*this);
1928 llvm::raw_svector_ostream Out(MethodPool);
1929 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001930 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001931 BucketOffset = Generator.Emit(Out, Trait);
1932 }
1933
1934 // Create a blob abbreviation
1935 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1936 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1939 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1940
1941 // Write the identifier table
1942 RecordData Record;
1943 Record.push_back(pch::METHOD_POOL);
1944 Record.push_back(BucketOffset);
1945 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1946 &MethodPool.front(),
1947 MethodPool.size());
1948 }
1949}
1950
1951namespace {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001952class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1953 PCHWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001954 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001955
1956public:
1957 typedef const IdentifierInfo* key_type;
1958 typedef key_type key_type_ref;
1959
1960 typedef pch::IdentID data_type;
1961 typedef data_type data_type_ref;
1962
Douglas Gregor37e26842009-04-21 23:56:24 +00001963 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1964 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001965
1966 static unsigned ComputeHash(const IdentifierInfo* II) {
1967 return clang::BernsteinHash(II->getName());
1968 }
1969
Douglas Gregor37e26842009-04-21 23:56:24 +00001970 std::pair<unsigned,unsigned>
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001971 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1972 pch::IdentID ID) {
1973 unsigned KeyLen = strlen(II->getName()) + 1;
1974 clang::io::Emit16(Out, KeyLen);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001975 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1976 // 4 bytes for the persistent ID
Douglas Gregor37e26842009-04-21 23:56:24 +00001977 if (II->hasMacroDefinition() &&
1978 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1979 DataLen += 8;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001980 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1981 DEnd = IdentifierResolver::end();
1982 D != DEnd; ++D)
1983 DataLen += sizeof(pch::DeclID);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001984 clang::io::Emit16(Out, DataLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001985 return std::make_pair(KeyLen, DataLen);
1986 }
1987
1988 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1989 unsigned KeyLen) {
1990 // Record the location of the key data. This is used when generating
1991 // the mapping from persistent IDs to strings.
1992 Writer.SetIdentifierOffset(II, Out.tell());
1993 Out.write(II->getName(), KeyLen);
1994 }
1995
1996 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1997 pch::IdentID ID, unsigned) {
1998 uint32_t Bits = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001999 bool hasMacroDefinition =
2000 II->hasMacroDefinition() &&
2001 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002002 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002003 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2004 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002005 Bits = (Bits << 1) | II->isExtensionToken();
2006 Bits = (Bits << 1) | II->isPoisoned();
2007 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2008 clang::io::Emit32(Out, Bits);
2009 clang::io::Emit32(Out, ID);
2010
Douglas Gregor37e26842009-04-21 23:56:24 +00002011 if (hasMacroDefinition)
2012 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2013
Douglas Gregor668c1a42009-04-21 22:25:48 +00002014 // Emit the declaration IDs in reverse order, because the
2015 // IdentifierResolver provides the declarations as they would be
2016 // visible (e.g., the function "stat" would come before the struct
2017 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2018 // adds declarations to the end of the list (so we need to see the
2019 // struct "status" before the function "status").
2020 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2021 IdentifierResolver::end());
2022 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2023 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002024 D != DEnd; ++D)
Douglas Gregor668c1a42009-04-21 22:25:48 +00002025 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002026 }
2027};
2028} // end anonymous namespace
2029
Douglas Gregorafaf3082009-04-11 00:14:32 +00002030/// \brief Write the identifier table into the PCH file.
2031///
2032/// The identifier table consists of a blob containing string data
2033/// (the actual identifiers themselves) and a separate "offsets" index
2034/// that maps identifier IDs to locations within the blob.
Douglas Gregor37e26842009-04-21 23:56:24 +00002035void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002036 using namespace llvm;
2037
2038 // Create and write out the blob that contains the identifier
2039 // strings.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002040 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002041 {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002042 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2043
2044 // Create the on-disk hash table representation.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002045 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2046 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2047 ID != IDEnd; ++ID) {
2048 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002049 Generator.insert(ID->first, ID->second);
2050 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002051
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002052 // Create the on-disk hash table in a buffer.
2053 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002054 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002055 {
Douglas Gregor37e26842009-04-21 23:56:24 +00002056 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002057 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002058 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002059 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002060 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002061 }
2062
2063 // Create a blob abbreviation
2064 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2065 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002068 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002069
2070 // Write the identifier table
2071 RecordData Record;
2072 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002073 Record.push_back(BucketOffset);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002074 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2075 &IdentifierTable.front(),
2076 IdentifierTable.size());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002077 }
2078
2079 // Write the offsets table for identifier IDs.
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002080 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentifierOffsets);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002081}
2082
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002083void PCHWriter::WriteSelectorTable() {
Douglas Gregor6e378de2009-04-23 23:18:26 +00002084 Stream.EnterSubblock(pch::SELECTOR_BLOCK_ID, 2);
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002085 RecordData Record;
2086 Record.push_back(pch::SELECTOR_TABLE);
2087 Record.push_back(SelectorIDs.size());
2088
2089 // Create the on-disk representation.
2090 for (unsigned selIdx = 0; selIdx < SelVector.size(); selIdx++) {
2091 assert(SelVector[selIdx].getAsOpaquePtr() && "NULL Selector found");
2092 Record.push_back(SelVector[selIdx].getNumArgs());
2093 if (SelVector[selIdx].getNumArgs())
2094 for (unsigned i = 0; i < SelVector[selIdx].getNumArgs(); i++)
2095 AddIdentifierRef(SelVector[selIdx].getIdentifierInfoForSlot(i), Record);
2096 else
2097 AddIdentifierRef(SelVector[selIdx].getIdentifierInfoForSlot(0), Record);
2098 }
2099 Stream.EmitRecord(pch::SELECTOR_TABLE, Record);
2100 Stream.ExitBlock();
2101}
2102
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002103/// \brief Write a record containing the given attributes.
2104void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2105 RecordData Record;
2106 for (; Attr; Attr = Attr->getNext()) {
2107 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2108 Record.push_back(Attr->isInherited());
2109 switch (Attr->getKind()) {
2110 case Attr::Alias:
2111 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2112 break;
2113
2114 case Attr::Aligned:
2115 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2116 break;
2117
2118 case Attr::AlwaysInline:
2119 break;
2120
2121 case Attr::AnalyzerNoReturn:
2122 break;
2123
2124 case Attr::Annotate:
2125 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2126 break;
2127
2128 case Attr::AsmLabel:
2129 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2130 break;
2131
2132 case Attr::Blocks:
2133 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2134 break;
2135
2136 case Attr::Cleanup:
2137 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2138 break;
2139
2140 case Attr::Const:
2141 break;
2142
2143 case Attr::Constructor:
2144 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2145 break;
2146
2147 case Attr::DLLExport:
2148 case Attr::DLLImport:
2149 case Attr::Deprecated:
2150 break;
2151
2152 case Attr::Destructor:
2153 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2154 break;
2155
2156 case Attr::FastCall:
2157 break;
2158
2159 case Attr::Format: {
2160 const FormatAttr *Format = cast<FormatAttr>(Attr);
2161 AddString(Format->getType(), Record);
2162 Record.push_back(Format->getFormatIdx());
2163 Record.push_back(Format->getFirstArg());
2164 break;
2165 }
2166
Chris Lattnercf2a7212009-04-20 19:12:28 +00002167 case Attr::GNUInline:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002168 case Attr::IBOutletKind:
2169 case Attr::NoReturn:
2170 case Attr::NoThrow:
2171 case Attr::Nodebug:
2172 case Attr::Noinline:
2173 break;
2174
2175 case Attr::NonNull: {
2176 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2177 Record.push_back(NonNull->size());
2178 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2179 break;
2180 }
2181
2182 case Attr::ObjCException:
2183 case Attr::ObjCNSObject:
Ted Kremenekde9a81b2009-04-25 00:17:17 +00002184 case Attr::ObjCOwnershipRetain:
Ted Kremenek0fc169e2009-04-24 23:09:54 +00002185 case Attr::ObjCOwnershipReturns:
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002186 case Attr::Overloadable:
2187 break;
2188
2189 case Attr::Packed:
2190 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2191 break;
2192
2193 case Attr::Pure:
2194 break;
2195
2196 case Attr::Regparm:
2197 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2198 break;
2199
2200 case Attr::Section:
2201 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2202 break;
2203
2204 case Attr::StdCall:
2205 case Attr::TransparentUnion:
2206 case Attr::Unavailable:
2207 case Attr::Unused:
2208 case Attr::Used:
2209 break;
2210
2211 case Attr::Visibility:
2212 // FIXME: stable encoding
2213 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2214 break;
2215
2216 case Attr::WarnUnusedResult:
2217 case Attr::Weak:
2218 case Attr::WeakImport:
2219 break;
2220 }
2221 }
2222
Douglas Gregorc9490c02009-04-16 22:23:12 +00002223 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002224}
2225
2226void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2227 Record.push_back(Str.size());
2228 Record.insert(Record.end(), Str.begin(), Str.end());
2229}
2230
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002231/// \brief Note that the identifier II occurs at the given offset
2232/// within the identifier table.
2233void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
2234 IdentifierOffsets[IdentifierIDs[II] - 1] = (Offset << 1) | 0x01;
2235}
2236
Douglas Gregorc9490c02009-04-16 22:23:12 +00002237PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor37e26842009-04-21 23:56:24 +00002238 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregor25123082009-04-22 22:34:57 +00002239 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2240 NumVisibleDeclContexts(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002241
Douglas Gregore7785042009-04-20 15:53:59 +00002242void PCHWriter::WritePCH(Sema &SemaRef) {
2243 ASTContext &Context = SemaRef.Context;
2244 Preprocessor &PP = SemaRef.PP;
2245
Douglas Gregor2cf26342009-04-09 22:27:44 +00002246 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002247 Stream.Emit((unsigned)'C', 8);
2248 Stream.Emit((unsigned)'P', 8);
2249 Stream.Emit((unsigned)'C', 8);
2250 Stream.Emit((unsigned)'H', 8);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002251
2252 // The translation unit is the first declaration we'll emit.
2253 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2254 DeclsToEmit.push(Context.getTranslationUnitDecl());
2255
Douglas Gregor2deaea32009-04-22 18:49:13 +00002256 // Make sure that we emit IdentifierInfos (and any attached
2257 // declarations) for builtins.
2258 {
2259 IdentifierTable &Table = PP.getIdentifierTable();
2260 llvm::SmallVector<const char *, 32> BuiltinNames;
2261 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2262 Context.getLangOptions().NoBuiltin);
2263 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2264 getIdentifierRef(&Table.get(BuiltinNames[I]));
2265 }
2266
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002267 // Build a record containing all of the tentative definitions in
2268 // this header file. Generally, this record will be empty.
2269 RecordData TentativeDefinitions;
2270 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2271 TD = SemaRef.TentativeDefinitions.begin(),
2272 TDEnd = SemaRef.TentativeDefinitions.end();
2273 TD != TDEnd; ++TD)
2274 AddDeclRef(TD->second, TentativeDefinitions);
2275
Douglas Gregor14c22f22009-04-22 22:18:58 +00002276 // Build a record containing all of the locally-scoped external
2277 // declarations in this header file. Generally, this record will be
2278 // empty.
2279 RecordData LocallyScopedExternalDecls;
2280 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2281 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2282 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2283 TD != TDEnd; ++TD)
2284 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2285
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286 // Write the remaining PCH contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002287 RecordData Record;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002288 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregor2bec0412009-04-10 21:16:55 +00002289 WriteTargetTriple(Context.Target);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002290 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor14f79002009-04-10 03:52:48 +00002291 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattner0b1fb982009-04-10 17:15:23 +00002292 WritePreprocessor(PP);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002293 WriteTypesBlock(Context);
2294 WriteDeclsBlock(Context);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002295 WriteMethodPool(SemaRef);
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002296 WriteSelectorTable();
Douglas Gregor37e26842009-04-21 23:56:24 +00002297 WriteIdentifierTable(PP);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002298 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
2299 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregorad1de002009-04-18 05:55:16 +00002300
2301 // Write the record of special types.
2302 Record.clear();
2303 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregor319ac892009-04-23 22:29:11 +00002304 AddTypeRef(Context.getObjCIdType(), Record);
2305 AddTypeRef(Context.getObjCSelType(), Record);
2306 AddTypeRef(Context.getObjCProtoType(), Record);
2307 AddTypeRef(Context.getObjCClassType(), Record);
2308 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2309 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregorad1de002009-04-18 05:55:16 +00002310 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2311
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002312 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002313 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002314 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002315
2316 // Write the record containing tentative definitions.
2317 if (!TentativeDefinitions.empty())
2318 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002319
2320 // Write the record containing locally-scoped external definitions.
2321 if (!LocallyScopedExternalDecls.empty())
2322 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2323 LocallyScopedExternalDecls);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002324
2325 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002326 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002327 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002328 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002329 Record.push_back(NumLexicalDeclContexts);
2330 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002331 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002332 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002333}
2334
2335void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2336 Record.push_back(Loc.getRawEncoding());
2337}
2338
2339void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2340 Record.push_back(Value.getBitWidth());
2341 unsigned N = Value.getNumWords();
2342 const uint64_t* Words = Value.getRawData();
2343 for (unsigned I = 0; I != N; ++I)
2344 Record.push_back(Words[I]);
2345}
2346
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002347void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2348 Record.push_back(Value.isUnsigned());
2349 AddAPInt(Value, Record);
2350}
2351
Douglas Gregor17fc2232009-04-14 21:55:33 +00002352void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2353 AddAPInt(Value.bitcastToAPInt(), Record);
2354}
2355
Douglas Gregor2cf26342009-04-09 22:27:44 +00002356void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002357 Record.push_back(getIdentifierRef(II));
2358}
2359
2360pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2361 if (II == 0)
2362 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002363
2364 pch::IdentID &ID = IdentifierIDs[II];
2365 if (ID == 0)
2366 ID = IdentifierIDs.size();
Douglas Gregor2deaea32009-04-22 18:49:13 +00002367 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002368}
2369
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002370void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2371 if (SelRef.getAsOpaquePtr() == 0) {
2372 Record.push_back(0);
2373 return;
2374 }
2375
2376 pch::SelectorID &SID = SelectorIDs[SelRef];
2377 if (SID == 0) {
2378 SID = SelectorIDs.size();
2379 SelVector.push_back(SelRef);
2380 }
2381 Record.push_back(SID);
2382}
2383
Douglas Gregor2cf26342009-04-09 22:27:44 +00002384void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2385 if (T.isNull()) {
2386 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2387 return;
2388 }
2389
2390 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002391 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002392 switch (BT->getKind()) {
2393 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2394 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2395 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2396 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2397 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2398 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2399 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2400 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2401 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2402 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2403 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2404 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2405 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2406 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2407 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2408 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2409 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2410 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2411 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2412 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2413 }
2414
2415 Record.push_back((ID << 3) | T.getCVRQualifiers());
2416 return;
2417 }
2418
Douglas Gregor8038d512009-04-10 17:25:41 +00002419 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002420 if (ID == 0) // we haven't seen this type before
2421 ID = NextTypeID++;
2422
2423 // Encode the type qualifiers in the type reference.
2424 Record.push_back((ID << 3) | T.getCVRQualifiers());
2425}
2426
2427void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2428 if (D == 0) {
2429 Record.push_back(0);
2430 return;
2431 }
2432
Douglas Gregor8038d512009-04-10 17:25:41 +00002433 pch::DeclID &ID = DeclIDs[D];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002434 if (ID == 0) {
2435 // We haven't seen this declaration before. Give it a new ID and
2436 // enqueue it in the list of declarations to emit.
2437 ID = DeclIDs.size();
2438 DeclsToEmit.push(const_cast<Decl *>(D));
2439 }
2440
2441 Record.push_back(ID);
2442}
2443
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002444pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2445 if (D == 0)
2446 return 0;
2447
2448 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2449 return DeclIDs[D];
2450}
2451
Douglas Gregor2cf26342009-04-09 22:27:44 +00002452void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2453 Record.push_back(Name.getNameKind());
2454 switch (Name.getNameKind()) {
2455 case DeclarationName::Identifier:
2456 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2457 break;
2458
2459 case DeclarationName::ObjCZeroArgSelector:
2460 case DeclarationName::ObjCOneArgSelector:
2461 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002462 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463 break;
2464
2465 case DeclarationName::CXXConstructorName:
2466 case DeclarationName::CXXDestructorName:
2467 case DeclarationName::CXXConversionFunctionName:
2468 AddTypeRef(Name.getCXXNameType(), Record);
2469 break;
2470
2471 case DeclarationName::CXXOperatorName:
2472 Record.push_back(Name.getCXXOverloadedOperator());
2473 break;
2474
2475 case DeclarationName::CXXUsingDirective:
2476 // No extra data to emit
2477 break;
2478 }
2479}
Douglas Gregor0b748912009-04-14 21:18:50 +00002480
Douglas Gregorc9490c02009-04-16 22:23:12 +00002481/// \brief Write the given substatement or subexpression to the
2482/// bitstream.
2483void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregor087fd532009-04-14 23:32:43 +00002484 RecordData Record;
2485 PCHStmtWriter Writer(*this, Record);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002486 ++NumStatements;
Douglas Gregor087fd532009-04-14 23:32:43 +00002487
Douglas Gregorc9490c02009-04-16 22:23:12 +00002488 if (!S) {
2489 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002490 return;
2491 }
2492
Douglas Gregorc9490c02009-04-16 22:23:12 +00002493 Writer.Code = pch::STMT_NULL_PTR;
2494 Writer.Visit(S);
2495 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor087fd532009-04-14 23:32:43 +00002496 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002497 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002498}
2499
Douglas Gregorc9490c02009-04-16 22:23:12 +00002500/// \brief Flush all of the statements that have been added to the
2501/// queue via AddStmt().
2502void PCHWriter::FlushStmts() {
Douglas Gregor0b748912009-04-14 21:18:50 +00002503 RecordData Record;
2504 PCHStmtWriter Writer(*this, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002505
Douglas Gregorc9490c02009-04-16 22:23:12 +00002506 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor3e1af842009-04-17 22:13:46 +00002507 ++NumStatements;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002508 Stmt *S = StmtsToEmit[I];
Douglas Gregor087fd532009-04-14 23:32:43 +00002509
Douglas Gregorc9490c02009-04-16 22:23:12 +00002510 if (!S) {
2511 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002512 continue;
2513 }
2514
Douglas Gregorc9490c02009-04-16 22:23:12 +00002515 Writer.Code = pch::STMT_NULL_PTR;
2516 Writer.Visit(S);
2517 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregor0b748912009-04-14 21:18:50 +00002518 "Unhandled expression writing PCH file");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002519 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregor087fd532009-04-14 23:32:43 +00002520
Douglas Gregorc9490c02009-04-16 22:23:12 +00002521 assert(N == StmtsToEmit.size() &&
2522 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregor087fd532009-04-14 23:32:43 +00002523
2524 // Note that we are at the end of a full expression. Any
2525 // expression records that follow this one are part of a different
2526 // expression.
2527 Record.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002528 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002529 }
Douglas Gregor087fd532009-04-14 23:32:43 +00002530
Douglas Gregorc9490c02009-04-16 22:23:12 +00002531 StmtsToEmit.clear();
Douglas Gregor0de9d882009-04-17 16:34:57 +00002532 SwitchCaseIDs.clear();
Douglas Gregor0b748912009-04-14 21:18:50 +00002533}
Douglas Gregor025452f2009-04-17 00:04:06 +00002534
2535unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2536 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2537 "SwitchCase recorded twice");
2538 unsigned NextID = SwitchCaseIDs.size();
2539 SwitchCaseIDs[S] = NextID;
2540 return NextID;
2541}
2542
2543unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2544 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2545 "SwitchCase hasn't been seen yet");
2546 return SwitchCaseIDs[S];
2547}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002548
2549/// \brief Retrieve the ID for the given label statement, which may
2550/// or may not have been emitted yet.
2551unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2552 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2553 if (Pos != LabelIDs.end())
2554 return Pos->second;
2555
2556 unsigned NextID = LabelIDs.size();
2557 LabelIDs[S] = NextID;
2558 return NextID;
2559}