blob: d02a61896486a5020ecc889cdb4140658df1b0e3 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamWriter.h"
35#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
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 Gregorc72f6c82009-04-16 22:23:12 +0000133 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000134 Code = pch::TYPE_VARIABLE_ARRAY;
135}
136
137void PCHTypeWriter::VisitVectorType(const VectorType *T) {
138 Writer.AddTypeRef(T->getElementType(), Record);
139 Record.push_back(T->getNumElements());
140 Code = pch::TYPE_VECTOR;
141}
142
143void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
144 VisitVectorType(T);
145 Code = pch::TYPE_EXT_VECTOR;
146}
147
148void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
149 Writer.AddTypeRef(T->getResultType(), Record);
150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
164 Code = pch::TYPE_FUNCTION_PROTO;
165}
166
167void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
168 Writer.AddDeclRef(T->getDecl(), Record);
169 Code = pch::TYPE_TYPEDEF;
170}
171
172void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000173 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000174 Code = pch::TYPE_TYPEOF_EXPR;
175}
176
177void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
178 Writer.AddTypeRef(T->getUnderlyingType(), Record);
179 Code = pch::TYPE_TYPEOF;
180}
181
182void PCHTypeWriter::VisitTagType(const TagType *T) {
183 Writer.AddDeclRef(T->getDecl(), Record);
184 assert(!T->isBeingDefined() &&
185 "Cannot serialize in the middle of a type definition");
186}
187
188void PCHTypeWriter::VisitRecordType(const RecordType *T) {
189 VisitTagType(T);
190 Code = pch::TYPE_RECORD;
191}
192
193void PCHTypeWriter::VisitEnumType(const EnumType *T) {
194 VisitTagType(T);
195 Code = pch::TYPE_ENUM;
196}
197
198void
199PCHTypeWriter::VisitTemplateSpecializationType(
200 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000201 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000202 assert(false && "Cannot serialize template specialization types");
203}
204
205void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000206 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000207 assert(false && "Cannot serialize qualified name types");
208}
209
210void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
211 Writer.AddDeclRef(T->getDecl(), Record);
212 Code = pch::TYPE_OBJC_INTERFACE;
213}
214
215void
216PCHTypeWriter::VisitObjCQualifiedInterfaceType(
217 const ObjCQualifiedInterfaceType *T) {
218 VisitObjCInterfaceType(T);
219 Record.push_back(T->getNumProtocols());
220 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
221 Writer.AddDeclRef(T->getProtocol(I), Record);
222 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
223}
224
225void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
226 Record.push_back(T->getNumProtocols());
227 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
228 Writer.AddDeclRef(T->getProtocols(I), Record);
229 Code = pch::TYPE_OBJC_QUALIFIED_ID;
230}
231
Douglas Gregorc34897d2009-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 Gregore3241e92009-04-18 00:02:19 +0000240 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000241 PCHWriter::RecordData &Record;
242
243 public:
244 pch::DeclCode Code;
245
Douglas Gregore3241e92009-04-18 00:02:19 +0000246 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
247 PCHWriter::RecordData &Record)
248 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-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 Gregor47f1b2c2009-04-13 18:14:40 +0000255 void VisitTagDecl(TagDecl *D);
256 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000257 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000258 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000259 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000260 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000261 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000262 void VisitVarDecl(VarDecl *D);
Douglas Gregorce066712009-04-26 22:20:50 +0000263 void VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000264 void VisitParmVarDecl(ParmVarDecl *D);
265 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000266 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
267 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000268 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
269 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000270 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000271 void VisitObjCContainerDecl(ObjCContainerDecl *D);
272 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
273 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000274 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
275 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
276 void VisitObjCClassDecl(ObjCClassDecl *D);
277 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
278 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
279 void VisitObjCImplDecl(ObjCImplDecl *D);
280 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
281 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
282 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
283 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
284 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000285 };
286}
287
288void PCHDeclWriter::VisitDecl(Decl *D) {
289 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
290 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
291 Writer.AddSourceLocation(D->getLocation(), Record);
292 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000293 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000294 Record.push_back(D->isImplicit());
295 Record.push_back(D->getAccess());
296}
297
298void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
299 VisitDecl(D);
300 Code = pch::DECL_TRANSLATION_UNIT;
301}
302
303void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
304 VisitDecl(D);
305 Writer.AddDeclarationName(D->getDeclName(), Record);
306}
307
308void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
309 VisitNamedDecl(D);
310 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
311}
312
313void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
314 VisitTypeDecl(D);
315 Writer.AddTypeRef(D->getUnderlyingType(), Record);
316 Code = pch::DECL_TYPEDEF;
317}
318
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000319void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
320 VisitTypeDecl(D);
321 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
322 Record.push_back(D->isDefinition());
323 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
324}
325
326void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
327 VisitTagDecl(D);
328 Writer.AddTypeRef(D->getIntegerType(), Record);
329 Code = pch::DECL_ENUM;
330}
331
Douglas Gregor982365e2009-04-13 21:20:57 +0000332void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
333 VisitTagDecl(D);
334 Record.push_back(D->hasFlexibleArrayMember());
335 Record.push_back(D->isAnonymousStructOrUnion());
336 Code = pch::DECL_RECORD;
337}
338
Douglas Gregorc34897d2009-04-09 22:27:44 +0000339void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
340 VisitNamedDecl(D);
341 Writer.AddTypeRef(D->getType(), Record);
342}
343
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000344void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
345 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000346 Record.push_back(D->getInitExpr()? 1 : 0);
347 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000348 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000349 Writer.AddAPSInt(D->getInitVal(), Record);
350 Code = pch::DECL_ENUM_CONSTANT;
351}
352
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000353void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
354 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000355 Record.push_back(D->isThisDeclarationADefinition());
356 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000357 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000358 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
359 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
360 Record.push_back(D->isInline());
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000361 Record.push_back(D->isC99InlineDefinition());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000362 Record.push_back(D->isVirtual());
363 Record.push_back(D->isPure());
364 Record.push_back(D->inheritedPrototype());
365 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
366 Record.push_back(D->isDeleted());
367 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
368 Record.push_back(D->param_size());
369 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
370 P != PEnd; ++P)
371 Writer.AddDeclRef(*P, Record);
372 Code = pch::DECL_FUNCTION;
373}
374
Steve Naroff79ea0e02009-04-20 15:06:07 +0000375void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
376 VisitNamedDecl(D);
377 // FIXME: convert to LazyStmtPtr?
378 // Unlike C/C++, method bodies will never be in header files.
379 Record.push_back(D->getBody() != 0);
380 if (D->getBody() != 0) {
381 Writer.AddStmt(D->getBody(Context));
382 Writer.AddDeclRef(D->getSelfDecl(), Record);
383 Writer.AddDeclRef(D->getCmdDecl(), Record);
384 }
385 Record.push_back(D->isInstanceMethod());
386 Record.push_back(D->isVariadic());
387 Record.push_back(D->isSynthesized());
388 // FIXME: stable encoding for @required/@optional
389 Record.push_back(D->getImplementationControl());
390 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
391 Record.push_back(D->getObjCDeclQualifier());
392 Writer.AddTypeRef(D->getResultType(), Record);
393 Writer.AddSourceLocation(D->getLocEnd(), Record);
394 Record.push_back(D->param_size());
395 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
396 PEnd = D->param_end(); P != PEnd; ++P)
397 Writer.AddDeclRef(*P, Record);
398 Code = pch::DECL_OBJC_METHOD;
399}
400
Steve Naroff7333b492009-04-20 20:09:33 +0000401void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
402 VisitNamedDecl(D);
403 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
404 // Abstract class (no need to define a stable pch::DECL code).
405}
406
407void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
408 VisitObjCContainerDecl(D);
409 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
410 Writer.AddDeclRef(D->getSuperClass(), Record);
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000411 Record.push_back(D->protocol_size());
412 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
413 PEnd = D->protocol_end();
414 P != PEnd; ++P)
415 Writer.AddDeclRef(*P, Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000416 Record.push_back(D->ivar_size());
417 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
418 IEnd = D->ivar_end(); I != IEnd; ++I)
419 Writer.AddDeclRef(*I, Record);
Douglas Gregorae660c72009-04-23 22:34:55 +0000420 Writer.AddDeclRef(D->getCategoryList(), Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000421 Record.push_back(D->isForwardDecl());
422 Record.push_back(D->isImplicitInterfaceDecl());
423 Writer.AddSourceLocation(D->getClassLoc(), Record);
424 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
425 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000426 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000427}
428
429void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
430 VisitFieldDecl(D);
431 // FIXME: stable encoding for @public/@private/@protected/@package
432 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000433 Code = pch::DECL_OBJC_IVAR;
434}
435
436void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
437 VisitObjCContainerDecl(D);
438 Record.push_back(D->isForwardDecl());
439 Writer.AddSourceLocation(D->getLocEnd(), Record);
440 Record.push_back(D->protocol_size());
441 for (ObjCProtocolDecl::protocol_iterator
442 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
443 Writer.AddDeclRef(*I, Record);
444 Code = pch::DECL_OBJC_PROTOCOL;
445}
446
447void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
448 VisitFieldDecl(D);
449 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
450}
451
452void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
453 VisitDecl(D);
454 Record.push_back(D->size());
455 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
456 Writer.AddDeclRef(*I, Record);
457 Code = pch::DECL_OBJC_CLASS;
458}
459
460void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
461 VisitDecl(D);
462 Record.push_back(D->protocol_size());
463 for (ObjCProtocolDecl::protocol_iterator
464 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
465 Writer.AddDeclRef(*I, Record);
466 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
467}
468
469void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
470 VisitObjCContainerDecl(D);
471 Writer.AddDeclRef(D->getClassInterface(), Record);
472 Record.push_back(D->protocol_size());
473 for (ObjCProtocolDecl::protocol_iterator
474 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
475 Writer.AddDeclRef(*I, Record);
476 Writer.AddDeclRef(D->getNextClassCategory(), Record);
477 Writer.AddSourceLocation(D->getLocEnd(), Record);
478 Code = pch::DECL_OBJC_CATEGORY;
479}
480
481void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
482 VisitNamedDecl(D);
483 Writer.AddDeclRef(D->getClassInterface(), Record);
484 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
485}
486
487void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
488 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000489 Writer.AddTypeRef(D->getType(), Record);
490 // FIXME: stable encoding
491 Record.push_back((unsigned)D->getPropertyAttributes());
492 // FIXME: stable encoding
493 Record.push_back((unsigned)D->getPropertyImplementation());
494 Writer.AddDeclarationName(D->getGetterName(), Record);
495 Writer.AddDeclarationName(D->getSetterName(), Record);
496 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
497 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
498 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000499 Code = pch::DECL_OBJC_PROPERTY;
500}
501
502void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000503 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000504 Writer.AddDeclRef(D->getClassInterface(), Record);
505 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000506 // Abstract class (no need to define a stable pch::DECL code).
507}
508
509void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
510 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000511 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000512 Code = pch::DECL_OBJC_CATEGORY_IMPL;
513}
514
515void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
516 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000517 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000518 Code = pch::DECL_OBJC_IMPLEMENTATION;
519}
520
521void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
522 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000523 Writer.AddSourceLocation(D->getLocStart(), Record);
524 Writer.AddDeclRef(D->getPropertyDecl(), Record);
525 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000526 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000527}
528
Douglas Gregor982365e2009-04-13 21:20:57 +0000529void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
530 VisitValueDecl(D);
531 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000532 Record.push_back(D->getBitWidth()? 1 : 0);
533 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000534 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000535 Code = pch::DECL_FIELD;
536}
537
Douglas Gregorc34897d2009-04-09 22:27:44 +0000538void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
539 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000540 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000541 Record.push_back(D->isThreadSpecified());
542 Record.push_back(D->hasCXXDirectInitializer());
543 Record.push_back(D->isDeclaredInCondition());
544 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
545 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000546 Record.push_back(D->getInit()? 1 : 0);
547 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000548 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000549 Code = pch::DECL_VAR;
550}
551
Douglas Gregorce066712009-04-26 22:20:50 +0000552void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
553 VisitVarDecl(D);
554 Code = pch::DECL_IMPLICIT_PARAM;
555}
556
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000557void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
558 VisitVarDecl(D);
559 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000560 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000561 // FIXME: why isn't the "default argument" just stored as the initializer
562 // in VarDecl?
563 Code = pch::DECL_PARM_VAR;
564}
565
566void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
567 VisitParmVarDecl(D);
568 Writer.AddTypeRef(D->getOriginalType(), Record);
569 Code = pch::DECL_ORIGINAL_PARM_VAR;
570}
571
Douglas Gregor2a491792009-04-13 22:49:25 +0000572void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
573 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000574 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000575 Code = pch::DECL_FILE_SCOPE_ASM;
576}
577
578void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
579 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000580 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000581 Record.push_back(D->param_size());
582 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
583 P != PEnd; ++P)
584 Writer.AddDeclRef(*P, Record);
585 Code = pch::DECL_BLOCK;
586}
587
Douglas Gregorc34897d2009-04-09 22:27:44 +0000588/// \brief Emit the DeclContext part of a declaration context decl.
589///
590/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
591/// block for this declaration context is stored. May be 0 to indicate
592/// that there are no declarations stored within this context.
593///
594/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
595/// block for this declaration context is stored. May be 0 to indicate
596/// that there are no declarations visible from this context. Note
597/// that this value will not be emitted for non-primary declaration
598/// contexts.
599void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
600 uint64_t VisibleOffset) {
601 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000602 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000603}
604
605//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000606// Statement/expression serialization
607//===----------------------------------------------------------------------===//
608namespace {
609 class VISIBILITY_HIDDEN PCHStmtWriter
610 : public StmtVisitor<PCHStmtWriter, void> {
611
612 PCHWriter &Writer;
613 PCHWriter::RecordData &Record;
614
615 public:
616 pch::StmtCode Code;
617
618 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
619 : Writer(Writer), Record(Record) { }
620
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000621 void VisitStmt(Stmt *S);
622 void VisitNullStmt(NullStmt *S);
623 void VisitCompoundStmt(CompoundStmt *S);
624 void VisitSwitchCase(SwitchCase *S);
625 void VisitCaseStmt(CaseStmt *S);
626 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000627 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000628 void VisitIfStmt(IfStmt *S);
629 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000630 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000631 void VisitDoStmt(DoStmt *S);
632 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000633 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000634 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000635 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000636 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000637 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000638 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000639 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000640 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000641 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000642 void VisitDeclRefExpr(DeclRefExpr *E);
643 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000644 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000645 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000646 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000647 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000648 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000649 void VisitUnaryOperator(UnaryOperator *E);
650 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000651 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000652 void VisitCallExpr(CallExpr *E);
653 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000654 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000655 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000656 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
657 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000658 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000659 void VisitExplicitCastExpr(ExplicitCastExpr *E);
660 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000661 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000662 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000663 void VisitInitListExpr(InitListExpr *E);
664 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
665 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000666 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000667 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000668 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000669 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
670 void VisitChooseExpr(ChooseExpr *E);
671 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000672 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000673 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000674 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000675
Steve Naroff79762bd2009-04-26 18:52:16 +0000676 // Objective-C Expressions
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000677 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000678 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000679 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
680 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000681 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
682 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
683 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000684 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000685 void VisitObjCSuperExpr(ObjCSuperExpr *E);
Steve Naroff79762bd2009-04-26 18:52:16 +0000686
687 // Objective-C Statements
688 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
Douglas Gregorce066712009-04-26 22:20:50 +0000689 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *);
690 void VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *);
Steve Naroff79762bd2009-04-26 18:52:16 +0000691 void VisitObjCAtTryStmt(ObjCAtTryStmt *);
692 void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
693 void VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000694 };
695}
696
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000697void PCHStmtWriter::VisitStmt(Stmt *S) {
698}
699
700void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
701 VisitStmt(S);
702 Writer.AddSourceLocation(S->getSemiLoc(), Record);
703 Code = pch::STMT_NULL;
704}
705
706void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
707 VisitStmt(S);
708 Record.push_back(S->size());
709 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
710 CS != CSEnd; ++CS)
711 Writer.WriteSubStmt(*CS);
712 Writer.AddSourceLocation(S->getLBracLoc(), Record);
713 Writer.AddSourceLocation(S->getRBracLoc(), Record);
714 Code = pch::STMT_COMPOUND;
715}
716
717void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
718 VisitStmt(S);
719 Record.push_back(Writer.RecordSwitchCaseID(S));
720}
721
722void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
723 VisitSwitchCase(S);
724 Writer.WriteSubStmt(S->getLHS());
725 Writer.WriteSubStmt(S->getRHS());
726 Writer.WriteSubStmt(S->getSubStmt());
727 Writer.AddSourceLocation(S->getCaseLoc(), Record);
728 Code = pch::STMT_CASE;
729}
730
731void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
732 VisitSwitchCase(S);
733 Writer.WriteSubStmt(S->getSubStmt());
734 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
735 Code = pch::STMT_DEFAULT;
736}
737
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000738void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
739 VisitStmt(S);
740 Writer.AddIdentifierRef(S->getID(), Record);
741 Writer.WriteSubStmt(S->getSubStmt());
742 Writer.AddSourceLocation(S->getIdentLoc(), Record);
743 Record.push_back(Writer.GetLabelID(S));
744 Code = pch::STMT_LABEL;
745}
746
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000747void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
748 VisitStmt(S);
749 Writer.WriteSubStmt(S->getCond());
750 Writer.WriteSubStmt(S->getThen());
751 Writer.WriteSubStmt(S->getElse());
752 Writer.AddSourceLocation(S->getIfLoc(), Record);
753 Code = pch::STMT_IF;
754}
755
756void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
757 VisitStmt(S);
758 Writer.WriteSubStmt(S->getCond());
759 Writer.WriteSubStmt(S->getBody());
760 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
761 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
762 SC = SC->getNextSwitchCase())
763 Record.push_back(Writer.getSwitchCaseID(SC));
764 Code = pch::STMT_SWITCH;
765}
766
Douglas Gregora6b503f2009-04-17 00:16:09 +0000767void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
768 VisitStmt(S);
769 Writer.WriteSubStmt(S->getCond());
770 Writer.WriteSubStmt(S->getBody());
771 Writer.AddSourceLocation(S->getWhileLoc(), Record);
772 Code = pch::STMT_WHILE;
773}
774
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000775void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
776 VisitStmt(S);
777 Writer.WriteSubStmt(S->getCond());
778 Writer.WriteSubStmt(S->getBody());
779 Writer.AddSourceLocation(S->getDoLoc(), Record);
780 Code = pch::STMT_DO;
781}
782
783void PCHStmtWriter::VisitForStmt(ForStmt *S) {
784 VisitStmt(S);
785 Writer.WriteSubStmt(S->getInit());
786 Writer.WriteSubStmt(S->getCond());
787 Writer.WriteSubStmt(S->getInc());
788 Writer.WriteSubStmt(S->getBody());
789 Writer.AddSourceLocation(S->getForLoc(), Record);
790 Code = pch::STMT_FOR;
791}
792
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000793void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
794 VisitStmt(S);
795 Record.push_back(Writer.GetLabelID(S->getLabel()));
796 Writer.AddSourceLocation(S->getGotoLoc(), Record);
797 Writer.AddSourceLocation(S->getLabelLoc(), Record);
798 Code = pch::STMT_GOTO;
799}
800
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000801void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
802 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000803 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000804 Writer.WriteSubStmt(S->getTarget());
805 Code = pch::STMT_INDIRECT_GOTO;
806}
807
Douglas Gregora6b503f2009-04-17 00:16:09 +0000808void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
809 VisitStmt(S);
810 Writer.AddSourceLocation(S->getContinueLoc(), Record);
811 Code = pch::STMT_CONTINUE;
812}
813
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000814void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
815 VisitStmt(S);
816 Writer.AddSourceLocation(S->getBreakLoc(), Record);
817 Code = pch::STMT_BREAK;
818}
819
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000820void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
821 VisitStmt(S);
822 Writer.WriteSubStmt(S->getRetValue());
823 Writer.AddSourceLocation(S->getReturnLoc(), Record);
824 Code = pch::STMT_RETURN;
825}
826
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000827void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
828 VisitStmt(S);
829 Writer.AddSourceLocation(S->getStartLoc(), Record);
830 Writer.AddSourceLocation(S->getEndLoc(), Record);
831 DeclGroupRef DG = S->getDeclGroup();
832 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
833 Writer.AddDeclRef(*D, Record);
834 Code = pch::STMT_DECL;
835}
836
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000837void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
838 VisitStmt(S);
839 Record.push_back(S->getNumOutputs());
840 Record.push_back(S->getNumInputs());
841 Record.push_back(S->getNumClobbers());
842 Writer.AddSourceLocation(S->getAsmLoc(), Record);
843 Writer.AddSourceLocation(S->getRParenLoc(), Record);
844 Record.push_back(S->isVolatile());
845 Record.push_back(S->isSimple());
846 Writer.WriteSubStmt(S->getAsmString());
847
848 // Outputs
849 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
850 Writer.AddString(S->getOutputName(I), Record);
851 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
852 Writer.WriteSubStmt(S->getOutputExpr(I));
853 }
854
855 // Inputs
856 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
857 Writer.AddString(S->getInputName(I), Record);
858 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
859 Writer.WriteSubStmt(S->getInputExpr(I));
860 }
861
862 // Clobbers
863 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
864 Writer.WriteSubStmt(S->getClobber(I));
865
866 Code = pch::STMT_ASM;
867}
868
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000869void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000870 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000871 Writer.AddTypeRef(E->getType(), Record);
872 Record.push_back(E->isTypeDependent());
873 Record.push_back(E->isValueDependent());
874}
875
Douglas Gregore2f37202009-04-14 21:55:33 +0000876void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
877 VisitExpr(E);
878 Writer.AddSourceLocation(E->getLocation(), Record);
879 Record.push_back(E->getIdentType()); // FIXME: stable encoding
880 Code = pch::EXPR_PREDEFINED;
881}
882
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000883void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
884 VisitExpr(E);
885 Writer.AddDeclRef(E->getDecl(), Record);
886 Writer.AddSourceLocation(E->getLocation(), Record);
887 Code = pch::EXPR_DECL_REF;
888}
889
890void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
891 VisitExpr(E);
892 Writer.AddSourceLocation(E->getLocation(), Record);
893 Writer.AddAPInt(E->getValue(), Record);
894 Code = pch::EXPR_INTEGER_LITERAL;
895}
896
Douglas Gregore2f37202009-04-14 21:55:33 +0000897void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
898 VisitExpr(E);
899 Writer.AddAPFloat(E->getValue(), Record);
900 Record.push_back(E->isExact());
901 Writer.AddSourceLocation(E->getLocation(), Record);
902 Code = pch::EXPR_FLOATING_LITERAL;
903}
904
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000905void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
906 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000907 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000908 Code = pch::EXPR_IMAGINARY_LITERAL;
909}
910
Douglas Gregor596e0932009-04-15 16:35:07 +0000911void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
912 VisitExpr(E);
913 Record.push_back(E->getByteLength());
914 Record.push_back(E->getNumConcatenated());
915 Record.push_back(E->isWide());
916 // FIXME: String data should be stored as a blob at the end of the
917 // StringLiteral. However, we can't do so now because we have no
918 // provision for coping with abbreviations when we're jumping around
919 // the PCH file during deserialization.
920 Record.insert(Record.end(),
921 E->getStrData(), E->getStrData() + E->getByteLength());
922 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
923 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
924 Code = pch::EXPR_STRING_LITERAL;
925}
926
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000927void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
928 VisitExpr(E);
929 Record.push_back(E->getValue());
930 Writer.AddSourceLocation(E->getLoc(), Record);
931 Record.push_back(E->isWide());
932 Code = pch::EXPR_CHARACTER_LITERAL;
933}
934
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000935void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
936 VisitExpr(E);
937 Writer.AddSourceLocation(E->getLParen(), Record);
938 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000939 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000940 Code = pch::EXPR_PAREN;
941}
942
Douglas Gregor12d74052009-04-15 15:58:59 +0000943void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
944 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000945 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000946 Record.push_back(E->getOpcode()); // FIXME: stable encoding
947 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
948 Code = pch::EXPR_UNARY_OPERATOR;
949}
950
951void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
952 VisitExpr(E);
953 Record.push_back(E->isSizeOf());
954 if (E->isArgumentType())
955 Writer.AddTypeRef(E->getArgumentType(), Record);
956 else {
957 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000958 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000959 }
960 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
961 Writer.AddSourceLocation(E->getRParenLoc(), Record);
962 Code = pch::EXPR_SIZEOF_ALIGN_OF;
963}
964
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000965void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
966 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000967 Writer.WriteSubStmt(E->getLHS());
968 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000969 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
970 Code = pch::EXPR_ARRAY_SUBSCRIPT;
971}
972
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000973void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
974 VisitExpr(E);
975 Record.push_back(E->getNumArgs());
976 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000977 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000978 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
979 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000980 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000981 Code = pch::EXPR_CALL;
982}
983
984void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
985 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000986 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000987 Writer.AddDeclRef(E->getMemberDecl(), Record);
988 Writer.AddSourceLocation(E->getMemberLoc(), Record);
989 Record.push_back(E->isArrow());
990 Code = pch::EXPR_MEMBER;
991}
992
Douglas Gregora151ba42009-04-14 23:32:43 +0000993void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
994 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000995 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000996}
997
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000998void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
999 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001000 Writer.WriteSubStmt(E->getLHS());
1001 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001002 Record.push_back(E->getOpcode()); // FIXME: stable encoding
1003 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1004 Code = pch::EXPR_BINARY_OPERATOR;
1005}
1006
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001007void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
1008 VisitBinaryOperator(E);
1009 Writer.AddTypeRef(E->getComputationLHSType(), Record);
1010 Writer.AddTypeRef(E->getComputationResultType(), Record);
1011 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
1012}
1013
1014void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
1015 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001016 Writer.WriteSubStmt(E->getCond());
1017 Writer.WriteSubStmt(E->getLHS());
1018 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001019 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1020}
1021
Douglas Gregora151ba42009-04-14 23:32:43 +00001022void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1023 VisitCastExpr(E);
1024 Record.push_back(E->isLvalueCast());
1025 Code = pch::EXPR_IMPLICIT_CAST;
1026}
1027
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001028void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1029 VisitCastExpr(E);
1030 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1031}
1032
1033void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1034 VisitExplicitCastExpr(E);
1035 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1036 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1037 Code = pch::EXPR_CSTYLE_CAST;
1038}
1039
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001040void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1041 VisitExpr(E);
1042 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001043 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001044 Record.push_back(E->isFileScope());
1045 Code = pch::EXPR_COMPOUND_LITERAL;
1046}
1047
Douglas Gregorec0b8292009-04-15 23:02:49 +00001048void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1049 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001050 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001051 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1052 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1053 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1054}
1055
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001056void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1057 VisitExpr(E);
1058 Record.push_back(E->getNumInits());
1059 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001060 Writer.WriteSubStmt(E->getInit(I));
1061 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001062 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1063 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1064 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1065 Record.push_back(E->hadArrayRangeDesignator());
1066 Code = pch::EXPR_INIT_LIST;
1067}
1068
1069void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1070 VisitExpr(E);
1071 Record.push_back(E->getNumSubExprs());
1072 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001073 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001074 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1075 Record.push_back(E->usesGNUSyntax());
1076 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1077 DEnd = E->designators_end();
1078 D != DEnd; ++D) {
1079 if (D->isFieldDesignator()) {
1080 if (FieldDecl *Field = D->getField()) {
1081 Record.push_back(pch::DESIG_FIELD_DECL);
1082 Writer.AddDeclRef(Field, Record);
1083 } else {
1084 Record.push_back(pch::DESIG_FIELD_NAME);
1085 Writer.AddIdentifierRef(D->getFieldName(), Record);
1086 }
1087 Writer.AddSourceLocation(D->getDotLoc(), Record);
1088 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1089 } else if (D->isArrayDesignator()) {
1090 Record.push_back(pch::DESIG_ARRAY);
1091 Record.push_back(D->getFirstExprIndex());
1092 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1093 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1094 } else {
1095 assert(D->isArrayRangeDesignator() && "Unknown designator");
1096 Record.push_back(pch::DESIG_ARRAY_RANGE);
1097 Record.push_back(D->getFirstExprIndex());
1098 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1099 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1100 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1101 }
1102 }
1103 Code = pch::EXPR_DESIGNATED_INIT;
1104}
1105
1106void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1107 VisitExpr(E);
1108 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1109}
1110
Douglas Gregorec0b8292009-04-15 23:02:49 +00001111void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1112 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001113 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001114 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1115 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1116 Code = pch::EXPR_VA_ARG;
1117}
1118
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001119void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1120 VisitExpr(E);
1121 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1122 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1123 Record.push_back(Writer.GetLabelID(E->getLabel()));
1124 Code = pch::EXPR_ADDR_LABEL;
1125}
1126
Douglas Gregoreca12f62009-04-17 19:05:30 +00001127void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1128 VisitExpr(E);
1129 Writer.WriteSubStmt(E->getSubStmt());
1130 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1131 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1132 Code = pch::EXPR_STMT;
1133}
1134
Douglas Gregor209d4622009-04-15 23:33:31 +00001135void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1136 VisitExpr(E);
1137 Writer.AddTypeRef(E->getArgType1(), Record);
1138 Writer.AddTypeRef(E->getArgType2(), Record);
1139 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1140 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1141 Code = pch::EXPR_TYPES_COMPATIBLE;
1142}
1143
1144void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1145 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001146 Writer.WriteSubStmt(E->getCond());
1147 Writer.WriteSubStmt(E->getLHS());
1148 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001149 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1150 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1151 Code = pch::EXPR_CHOOSE;
1152}
1153
1154void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1155 VisitExpr(E);
1156 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1157 Code = pch::EXPR_GNU_NULL;
1158}
1159
Douglas Gregor725e94b2009-04-16 00:01:45 +00001160void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1161 VisitExpr(E);
1162 Record.push_back(E->getNumSubExprs());
1163 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001164 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001165 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1166 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1167 Code = pch::EXPR_SHUFFLE_VECTOR;
1168}
1169
Douglas Gregore246b742009-04-17 19:21:43 +00001170void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1171 VisitExpr(E);
1172 Writer.AddDeclRef(E->getBlockDecl(), Record);
1173 Record.push_back(E->hasBlockDeclRefExprs());
1174 Code = pch::EXPR_BLOCK;
1175}
1176
Douglas Gregor725e94b2009-04-16 00:01:45 +00001177void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1178 VisitExpr(E);
1179 Writer.AddDeclRef(E->getDecl(), Record);
1180 Writer.AddSourceLocation(E->getLocation(), Record);
1181 Record.push_back(E->isByRef());
1182 Code = pch::EXPR_BLOCK_DECL_REF;
1183}
1184
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001185//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001186// Objective-C Expressions and Statements.
1187//===----------------------------------------------------------------------===//
1188
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001189void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1190 VisitExpr(E);
1191 Writer.WriteSubStmt(E->getString());
1192 Writer.AddSourceLocation(E->getAtLoc(), Record);
1193 Code = pch::EXPR_OBJC_STRING_LITERAL;
1194}
1195
Chris Lattner80f83c62009-04-22 05:57:30 +00001196void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1197 VisitExpr(E);
1198 Writer.AddTypeRef(E->getEncodedType(), Record);
1199 Writer.AddSourceLocation(E->getAtLoc(), Record);
1200 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1201 Code = pch::EXPR_OBJC_ENCODE;
1202}
1203
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001204void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1205 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001206 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001207 Writer.AddSourceLocation(E->getAtLoc(), Record);
1208 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1209 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1210}
1211
1212void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1213 VisitExpr(E);
1214 Writer.AddDeclRef(E->getProtocol(), Record);
1215 Writer.AddSourceLocation(E->getAtLoc(), Record);
1216 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1217 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1218}
1219
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001220void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1221 VisitExpr(E);
1222 Writer.AddDeclRef(E->getDecl(), Record);
1223 Writer.AddSourceLocation(E->getLocation(), Record);
1224 Writer.WriteSubStmt(E->getBase());
1225 Record.push_back(E->isArrow());
1226 Record.push_back(E->isFreeIvar());
Steve Naroffa323e972009-04-26 14:11:39 +00001227 Code = pch::EXPR_OBJC_IVAR_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001228}
1229
1230void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1231 VisitExpr(E);
1232 Writer.AddDeclRef(E->getProperty(), Record);
1233 Writer.AddSourceLocation(E->getLocation(), Record);
1234 Writer.WriteSubStmt(E->getBase());
Steve Naroffa323e972009-04-26 14:11:39 +00001235 Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001236}
1237
1238void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1239 VisitExpr(E);
1240 Writer.AddDeclRef(E->getGetterMethod(), Record);
1241 Writer.AddDeclRef(E->getSetterMethod(), Record);
1242
1243 // NOTE: ClassProp and Base are mutually exclusive.
1244 Writer.AddDeclRef(E->getClassProp(), Record);
1245 Writer.WriteSubStmt(E->getBase());
1246 Writer.AddSourceLocation(E->getLocation(), Record);
1247 Writer.AddSourceLocation(E->getClassLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001248 Code = pch::EXPR_OBJC_KVC_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001249}
1250
Steve Narofffb3e4022009-04-25 14:04:28 +00001251void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1252 VisitExpr(E);
1253 Record.push_back(E->getNumArgs());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001254 Writer.AddSourceLocation(E->getLeftLoc(), Record);
1255 Writer.AddSourceLocation(E->getRightLoc(), Record);
Steve Narofffb3e4022009-04-25 14:04:28 +00001256 Writer.AddSelectorRef(E->getSelector(), Record);
1257 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
Steve Narofffb3e4022009-04-25 14:04:28 +00001258 Writer.WriteSubStmt(E->getReceiver());
Douglas Gregorce066712009-04-26 22:20:50 +00001259
1260 if (!E->getReceiver()) {
1261 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
1262 Writer.AddDeclRef(CI.first, Record);
1263 Writer.AddIdentifierRef(CI.second, Record);
1264 }
1265
Steve Narofffb3e4022009-04-25 14:04:28 +00001266 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1267 Arg != ArgEnd; ++Arg)
1268 Writer.WriteSubStmt(*Arg);
1269 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1270}
1271
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001272void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1273 VisitExpr(E);
1274 Writer.AddSourceLocation(E->getLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001275 Code = pch::EXPR_OBJC_SUPER_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001276}
1277
Steve Naroff79762bd2009-04-26 18:52:16 +00001278void PCHStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1279 VisitStmt(S);
1280 Writer.WriteSubStmt(S->getElement());
1281 Writer.WriteSubStmt(S->getCollection());
1282 Writer.WriteSubStmt(S->getBody());
1283 Writer.AddSourceLocation(S->getForLoc(), Record);
1284 Writer.AddSourceLocation(S->getRParenLoc(), Record);
1285 Code = pch::STMT_OBJC_FOR_COLLECTION;
1286}
1287
Douglas Gregorce066712009-04-26 22:20:50 +00001288void PCHStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +00001289 Writer.WriteSubStmt(S->getCatchBody());
1290 Writer.WriteSubStmt(S->getNextCatchStmt());
1291 Writer.AddDeclRef(S->getCatchParamDecl(), Record);
1292 Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
1293 Writer.AddSourceLocation(S->getRParenLoc(), Record);
1294 Code = pch::STMT_OBJC_CATCH;
1295}
1296
Douglas Gregorce066712009-04-26 22:20:50 +00001297void PCHStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +00001298 Writer.WriteSubStmt(S->getFinallyBody());
1299 Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
1300 Code = pch::STMT_OBJC_FINALLY;
1301}
1302
1303void PCHStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1304 Writer.WriteSubStmt(S->getTryBody());
1305 Writer.WriteSubStmt(S->getCatchStmts());
1306 Writer.WriteSubStmt(S->getFinallyStmt());
1307 Writer.AddSourceLocation(S->getAtTryLoc(), Record);
1308 Code = pch::STMT_OBJC_AT_TRY;
1309}
1310
1311void PCHStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1312 Writer.WriteSubStmt(S->getSynchExpr());
1313 Writer.WriteSubStmt(S->getSynchBody());
1314 Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
1315 Code = pch::STMT_OBJC_AT_SYNCHRONIZED;
1316}
1317
1318void PCHStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1319 Writer.WriteSubStmt(S->getThrowExpr());
1320 Writer.AddSourceLocation(S->getThrowLoc(), Record);
1321 Code = pch::STMT_OBJC_AT_THROW;
1322}
Chris Lattner80f83c62009-04-22 05:57:30 +00001323
1324//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001325// PCHWriter Implementation
1326//===----------------------------------------------------------------------===//
1327
Chris Lattner920673a2009-04-26 22:26:21 +00001328static void EmitBlockID(unsigned ID, const char *Name,
1329 llvm::BitstreamWriter &Stream,
1330 PCHWriter::RecordData &Record) {
1331 Record.clear();
1332 Record.push_back(ID);
1333 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
1334
1335 // Emit the block name if present.
1336 if (Name == 0 || Name[0] == 0) return;
1337 Record.clear();
1338 while (*Name)
1339 Record.push_back(*Name++);
1340 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
1341}
1342
1343static void EmitRecordID(unsigned ID, const char *Name,
1344 llvm::BitstreamWriter &Stream,
1345 PCHWriter::RecordData &Record) {
1346 Record.clear();
1347 Record.push_back(ID);
1348 while (*Name)
1349 Record.push_back(*Name++);
1350 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
1351
1352}
1353
1354void PCHWriter::WriteBlockInfoBlock() {
1355 RecordData Record;
1356 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
1357
1358#define BLOCK(X) EmitBlockID(pch::X, #X, Stream, Record)
1359#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
1360
1361 // PCH Top-Level Block.
1362 BLOCK(PCH_BLOCK_ID);
1363 RECORD(TYPE_OFFSET);
1364 RECORD(DECL_OFFSET);
1365 RECORD(LANGUAGE_OPTIONS);
1366 RECORD(TARGET_TRIPLE);
1367 RECORD(IDENTIFIER_OFFSET);
1368 RECORD(IDENTIFIER_TABLE);
1369 RECORD(EXTERNAL_DEFINITIONS);
1370 RECORD(SPECIAL_TYPES);
1371 RECORD(STATISTICS);
1372 RECORD(TENTATIVE_DEFINITIONS);
1373 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
1374 RECORD(SELECTOR_OFFSETS);
1375 RECORD(METHOD_POOL);
1376 RECORD(PP_COUNTER_VALUE);
1377
1378 // SourceManager Block.
1379 BLOCK(SOURCE_MANAGER_BLOCK_ID);
1380 RECORD(SM_SLOC_FILE_ENTRY);
1381 RECORD(SM_SLOC_BUFFER_ENTRY);
1382 RECORD(SM_SLOC_BUFFER_BLOB);
1383 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
1384 RECORD(SM_LINE_TABLE);
1385 RECORD(SM_HEADER_FILE_INFO);
1386
1387 // Preprocessor Block.
1388 BLOCK(PREPROCESSOR_BLOCK_ID);
1389 RECORD(PP_MACRO_OBJECT_LIKE);
1390 RECORD(PP_MACRO_FUNCTION_LIKE);
1391 RECORD(PP_TOKEN);
1392
1393 // Types block.
1394 BLOCK(TYPES_BLOCK_ID);
1395 RECORD(TYPE_EXT_QUAL);
1396 RECORD(TYPE_FIXED_WIDTH_INT);
1397 RECORD(TYPE_COMPLEX);
1398 RECORD(TYPE_POINTER);
1399 RECORD(TYPE_BLOCK_POINTER);
1400 RECORD(TYPE_LVALUE_REFERENCE);
1401 RECORD(TYPE_RVALUE_REFERENCE);
1402 RECORD(TYPE_MEMBER_POINTER);
1403 RECORD(TYPE_CONSTANT_ARRAY);
1404 RECORD(TYPE_INCOMPLETE_ARRAY);
1405 RECORD(TYPE_VARIABLE_ARRAY);
1406 RECORD(TYPE_VECTOR);
1407 RECORD(TYPE_EXT_VECTOR);
1408 RECORD(TYPE_FUNCTION_PROTO);
1409 RECORD(TYPE_FUNCTION_NO_PROTO);
1410 RECORD(TYPE_TYPEDEF);
1411 RECORD(TYPE_TYPEOF_EXPR);
1412 RECORD(TYPE_TYPEOF);
1413 RECORD(TYPE_RECORD);
1414 RECORD(TYPE_ENUM);
1415 RECORD(TYPE_OBJC_INTERFACE);
1416 RECORD(TYPE_OBJC_QUALIFIED_INTERFACE);
1417 RECORD(TYPE_OBJC_QUALIFIED_ID);
1418
1419 // Decls block.
1420 BLOCK(DECLS_BLOCK_ID);
1421 // ... many others
1422 RECORD(DECL_FIELD);
1423 RECORD(DECL_VAR);
1424 RECORD(DECL_PARM_VAR);
1425
1426
1427#undef RECORD
1428#undef BLOCK
1429 Stream.ExitBlock();
1430}
1431
1432
Douglas Gregorb5887f32009-04-10 21:16:55 +00001433/// \brief Write the target triple (e.g., i686-apple-darwin9).
1434void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1435 using namespace llvm;
1436 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1437 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001439 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001440
1441 RecordData Record;
1442 Record.push_back(pch::TARGET_TRIPLE);
1443 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001444 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001445}
1446
1447/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001448void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1449 RecordData Record;
1450 Record.push_back(LangOpts.Trigraphs);
1451 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1452 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1453 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1454 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1455 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1456 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1457 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1458 Record.push_back(LangOpts.C99); // C99 Support
1459 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1460 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1461 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1462 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1463 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1464
1465 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1466 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1467 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1468
1469 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1470 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1471 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1472 Record.push_back(LangOpts.LaxVectorConversions);
1473 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1474
1475 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1476 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1477 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1478
1479 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1480 // by locks.
1481 Record.push_back(LangOpts.Blocks); // block extension to C
1482 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1483 // they are unused.
1484 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1485 // (modulo the platform support).
1486
1487 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1488 // signed integer arithmetic overflows.
1489
1490 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1491 // may be ripped out at any time.
1492
1493 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1494 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1495 // defined.
1496 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1497 // opposed to __DYNAMIC__).
1498 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1499
1500 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1501 // used (instead of C99 semantics).
1502 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1503 Record.push_back(LangOpts.getGCMode());
1504 Record.push_back(LangOpts.getVisibilityMode());
1505 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001506 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001507}
1508
Douglas Gregorab1cef72009-04-10 03:52:48 +00001509//===----------------------------------------------------------------------===//
1510// Source Manager Serialization
1511//===----------------------------------------------------------------------===//
1512
1513/// \brief Create an abbreviation for the SLocEntry that refers to a
1514/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001515static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001516 using namespace llvm;
1517 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1518 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1521 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001524 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001525}
1526
1527/// \brief Create an abbreviation for the SLocEntry that refers to a
1528/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001529static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001530 using namespace llvm;
1531 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1532 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001538 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001539}
1540
1541/// \brief Create an abbreviation for the SLocEntry that refers to a
1542/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001543static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001544 using namespace llvm;
1545 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1546 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001548 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001549}
1550
1551/// \brief Create an abbreviation for the SLocEntry that refers to an
1552/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001553static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001554 using namespace llvm;
1555 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1556 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001562 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001563}
1564
1565/// \brief Writes the block containing the serialized form of the
1566/// source manager.
1567///
1568/// TODO: We should probably use an on-disk hash table (stored in a
1569/// blob), indexed based on the file name, so that we only create
1570/// entries for files that we actually need. In the common case (no
1571/// errors), we probably won't have to create file entries for any of
1572/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001573void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1574 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001575 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001576 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001577
1578 // Abbreviations for the various kinds of source-location entries.
1579 int SLocFileAbbrv = -1;
1580 int SLocBufferAbbrv = -1;
1581 int SLocBufferBlobAbbrv = -1;
1582 int SLocInstantiationAbbrv = -1;
1583
1584 // Write out the source location entry table. We skip the first
1585 // entry, which is always the same dummy entry.
1586 RecordData Record;
1587 for (SourceManager::sloc_entry_iterator
1588 SLoc = SourceMgr.sloc_entry_begin() + 1,
1589 SLocEnd = SourceMgr.sloc_entry_end();
1590 SLoc != SLocEnd; ++SLoc) {
1591 // Figure out which record code to use.
1592 unsigned Code;
1593 if (SLoc->isFile()) {
1594 if (SLoc->getFile().getContentCache()->Entry)
1595 Code = pch::SM_SLOC_FILE_ENTRY;
1596 else
1597 Code = pch::SM_SLOC_BUFFER_ENTRY;
1598 } else
1599 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1600 Record.push_back(Code);
1601
1602 Record.push_back(SLoc->getOffset());
1603 if (SLoc->isFile()) {
1604 const SrcMgr::FileInfo &File = SLoc->getFile();
1605 Record.push_back(File.getIncludeLoc().getRawEncoding());
1606 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001607 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001608
1609 const SrcMgr::ContentCache *Content = File.getContentCache();
1610 if (Content->Entry) {
1611 // The source location entry is a file. The blob associated
1612 // with this entry is the file name.
1613 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001614 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1615 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001616 Content->Entry->getName(),
1617 strlen(Content->Entry->getName()));
1618 } else {
1619 // The source location entry is a buffer. The blob associated
1620 // with this entry contains the contents of the buffer.
1621 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001622 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1623 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001624 }
1625
1626 // We add one to the size so that we capture the trailing NULL
1627 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1628 // the reader side).
1629 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1630 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001631 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001632 Record.clear();
1633 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001634 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001635 Buffer->getBufferStart(),
1636 Buffer->getBufferSize() + 1);
1637 }
1638 } else {
1639 // The source location entry is an instantiation.
1640 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1641 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1642 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1643 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1644
Douglas Gregor364e5802009-04-15 18:05:10 +00001645 // Compute the token length for this macro expansion.
1646 unsigned NextOffset = SourceMgr.getNextOffset();
1647 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1648 if (++NextSLoc != SLocEnd)
1649 NextOffset = NextSLoc->getOffset();
1650 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1651
Douglas Gregorab1cef72009-04-10 03:52:48 +00001652 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001653 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1654 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001655 }
1656
1657 Record.clear();
1658 }
1659
Douglas Gregor635f97f2009-04-13 16:31:14 +00001660 // Write the line table.
1661 if (SourceMgr.hasLineTable()) {
1662 LineTableInfo &LineTable = SourceMgr.getLineTable();
1663
1664 // Emit the file names
1665 Record.push_back(LineTable.getNumFilenames());
1666 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1667 // Emit the file name
1668 const char *Filename = LineTable.getFilename(I);
1669 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1670 Record.push_back(FilenameLen);
1671 if (FilenameLen)
1672 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1673 }
1674
1675 // Emit the line entries
1676 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1677 L != LEnd; ++L) {
1678 // Emit the file ID
1679 Record.push_back(L->first);
1680
1681 // Emit the line entries
1682 Record.push_back(L->second.size());
1683 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1684 LEEnd = L->second.end();
1685 LE != LEEnd; ++LE) {
1686 Record.push_back(LE->FileOffset);
1687 Record.push_back(LE->LineNo);
1688 Record.push_back(LE->FilenameID);
1689 Record.push_back((unsigned)LE->FileKind);
1690 Record.push_back(LE->IncludeOffset);
1691 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001692 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001693 }
1694 }
1695
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001696 // Loop over all the header files.
1697 HeaderSearch &HS = PP.getHeaderSearchInfo();
1698 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1699 E = HS.header_file_end();
1700 I != E; ++I) {
1701 Record.push_back(I->isImport);
1702 Record.push_back(I->DirInfo);
1703 Record.push_back(I->NumIncludes);
1704 if (I->ControllingMacro)
1705 AddIdentifierRef(I->ControllingMacro, Record);
1706 else
1707 Record.push_back(0);
1708 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1709 Record.clear();
1710 }
1711
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001712 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001713}
1714
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001715/// \brief Writes the block containing the serialized form of the
1716/// preprocessor.
1717///
Chris Lattner850eabd2009-04-10 18:08:30 +00001718void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001719 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001720
Chris Lattner4b21c202009-04-13 01:29:17 +00001721 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1722 if (PP.getCounterValue() != 0) {
1723 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001724 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001725 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001726 }
1727
1728 // Enter the preprocessor block.
1729 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001730
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001731 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1732 // FIXME: use diagnostics subsystem for localization etc.
1733 if (PP.SawDateOrTime())
1734 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1735
Chris Lattner1b094952009-04-10 18:00:12 +00001736 // Loop over all the macro definitions that are live at the end of the file,
1737 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001738 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1739 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001740 // FIXME: This emits macros in hash table order, we should do it in a stable
1741 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001742 MacroInfo *MI = I->second;
1743
1744 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1745 // been redefined by the header (in which case they are not isBuiltinMacro).
1746 if (MI->isBuiltinMacro())
1747 continue;
1748
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001749 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001750 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001751 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001752 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1753 Record.push_back(MI->isUsed());
1754
1755 unsigned Code;
1756 if (MI->isObjectLike()) {
1757 Code = pch::PP_MACRO_OBJECT_LIKE;
1758 } else {
1759 Code = pch::PP_MACRO_FUNCTION_LIKE;
1760
1761 Record.push_back(MI->isC99Varargs());
1762 Record.push_back(MI->isGNUVarargs());
1763 Record.push_back(MI->getNumArgs());
1764 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1765 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001766 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001767 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001768 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001769 Record.clear();
1770
Chris Lattner850eabd2009-04-10 18:08:30 +00001771 // Emit the tokens array.
1772 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1773 // Note that we know that the preprocessor does not have any annotation
1774 // tokens in it because they are created by the parser, and thus can't be
1775 // in a macro definition.
1776 const Token &Tok = MI->getReplacementToken(TokNo);
1777
1778 Record.push_back(Tok.getLocation().getRawEncoding());
1779 Record.push_back(Tok.getLength());
1780
Chris Lattner850eabd2009-04-10 18:08:30 +00001781 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1782 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001783 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001784
1785 // FIXME: Should translate token kind to a stable encoding.
1786 Record.push_back(Tok.getKind());
1787 // FIXME: Should translate token flags to a stable encoding.
1788 Record.push_back(Tok.getFlags());
1789
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001790 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001791 Record.clear();
1792 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001793 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001794 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001795 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001796}
1797
1798
Douglas Gregorc34897d2009-04-09 22:27:44 +00001799/// \brief Write the representation of a type to the PCH stream.
1800void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001801 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001802 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001803 ID = NextTypeID++;
1804
1805 // Record the offset for this type.
1806 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001807 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001808 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1809 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001810 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001811 }
1812
1813 RecordData Record;
1814
1815 // Emit the type's representation.
1816 PCHTypeWriter W(*this, Record);
1817 switch (T->getTypeClass()) {
1818 // For all of the concrete, non-dependent types, call the
1819 // appropriate visitor function.
1820#define TYPE(Class, Base) \
1821 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1822#define ABSTRACT_TYPE(Class, Base)
1823#define DEPENDENT_TYPE(Class, Base)
1824#include "clang/AST/TypeNodes.def"
1825
1826 // For all of the dependent type nodes (which only occur in C++
1827 // templates), produce an error.
1828#define TYPE(Class, Base)
1829#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1830#include "clang/AST/TypeNodes.def"
1831 assert(false && "Cannot serialize dependent type nodes");
1832 break;
1833 }
1834
1835 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001836 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001837
1838 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001839 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001840}
1841
1842/// \brief Write a block containing all of the types.
1843void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001844 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001845 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001846
Douglas Gregore43f0972009-04-26 03:49:13 +00001847 // Emit all of the types that need to be emitted (so far).
1848 while (!TypesToEmit.empty()) {
1849 const Type *T = TypesToEmit.front();
1850 TypesToEmit.pop();
1851 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1852 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001853 }
1854
1855 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001856 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001857}
1858
1859/// \brief Write the block containing all of the declaration IDs
1860/// lexically declared within the given DeclContext.
1861///
1862/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1863/// bistream, or 0 if no block was written.
1864uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1865 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001866 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001867 return 0;
1868
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001869 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001870 RecordData Record;
1871 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1872 DEnd = DC->decls_end(Context);
1873 D != DEnd; ++D)
1874 AddDeclRef(*D, Record);
1875
Douglas Gregoraf136d92009-04-22 22:34:57 +00001876 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001877 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001878 return Offset;
1879}
1880
1881/// \brief Write the block containing all of the declaration IDs
1882/// visible from the given DeclContext.
1883///
1884/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1885/// bistream, or 0 if no block was written.
1886uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1887 DeclContext *DC) {
1888 if (DC->getPrimaryContext() != DC)
1889 return 0;
1890
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001891 // Since there is no name lookup into functions or methods, and we
1892 // perform name lookup for the translation unit via the
1893 // IdentifierInfo chains, don't bother to build a
1894 // visible-declarations table for these entities.
1895 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001896 return 0;
1897
Douglas Gregorc34897d2009-04-09 22:27:44 +00001898 // Force the DeclContext to build a its name-lookup table.
1899 DC->lookup(Context, DeclarationName());
1900
1901 // Serialize the contents of the mapping used for lookup. Note that,
1902 // although we have two very different code paths, the serialized
1903 // representation is the same for both cases: a declaration name,
1904 // followed by a size, followed by references to the visible
1905 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001906 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001907 RecordData Record;
1908 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001909 if (!Map)
1910 return 0;
1911
Douglas Gregorc34897d2009-04-09 22:27:44 +00001912 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1913 D != DEnd; ++D) {
1914 AddDeclarationName(D->first, Record);
1915 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1916 Record.push_back(Result.second - Result.first);
1917 for(; Result.first != Result.second; ++Result.first)
1918 AddDeclRef(*Result.first, Record);
1919 }
1920
1921 if (Record.size() == 0)
1922 return 0;
1923
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001924 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001925 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001926 return Offset;
1927}
1928
1929/// \brief Write a block containing all of the declarations.
1930void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001931 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001932 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001933
1934 // Emit all of the declarations.
1935 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001936 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001937 while (!DeclsToEmit.empty()) {
1938 // Pull the next declaration off the queue
1939 Decl *D = DeclsToEmit.front();
1940 DeclsToEmit.pop();
1941
1942 // If this declaration is also a DeclContext, write blocks for the
1943 // declarations that lexically stored inside its context and those
1944 // declarations that are visible from its context. These blocks
1945 // are written before the declaration itself so that we can put
1946 // their offsets into the record for the declaration.
1947 uint64_t LexicalOffset = 0;
1948 uint64_t VisibleOffset = 0;
1949 DeclContext *DC = dyn_cast<DeclContext>(D);
1950 if (DC) {
1951 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1952 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1953 }
1954
1955 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001956 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001957 if (ID == 0)
1958 ID = DeclIDs.size();
1959
1960 unsigned Index = ID - 1;
1961
1962 // Record the offset for this declaration
1963 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001964 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001965 else if (DeclOffsets.size() < Index) {
1966 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001967 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001968 }
1969
1970 // Build and emit a record for this declaration
1971 Record.clear();
1972 W.Code = (pch::DeclCode)0;
1973 W.Visit(D);
1974 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001975
1976 if (!W.Code) {
1977 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1978 D->getDeclKindName());
1979 assert(false && "Unhandled declaration kind while generating PCH");
1980 exit(-1);
1981 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001982 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001983
Douglas Gregor1c507882009-04-15 21:30:51 +00001984 // If the declaration had any attributes, write them now.
1985 if (D->hasAttrs())
1986 WriteAttributeRecord(D->getAttrs());
1987
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001988 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001989 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001990
Douglas Gregor631f6c62009-04-14 00:24:19 +00001991 // Note external declarations so that we can add them to a record
1992 // in the PCH file later.
1993 if (isa<FileScopeAsmDecl>(D))
1994 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001995 }
1996
1997 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001998 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001999}
2000
Douglas Gregorff9a6092009-04-20 20:36:09 +00002001namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002002// Trait used for the on-disk hash table used in the method pool.
2003class VISIBILITY_HIDDEN PCHMethodPoolTrait {
2004 PCHWriter &Writer;
2005
2006public:
2007 typedef Selector key_type;
2008 typedef key_type key_type_ref;
2009
2010 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
2011 typedef const data_type& data_type_ref;
2012
2013 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
2014
2015 static unsigned ComputeHash(Selector Sel) {
2016 unsigned N = Sel.getNumArgs();
2017 if (N == 0)
2018 ++N;
2019 unsigned R = 5381;
2020 for (unsigned I = 0; I != N; ++I)
2021 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
2022 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
2023 return R;
2024 }
2025
2026 std::pair<unsigned,unsigned>
2027 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2028 data_type_ref Methods) {
2029 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2030 clang::io::Emit16(Out, KeyLen);
2031 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
2032 for (const ObjCMethodList *Method = &Methods.first; Method;
2033 Method = Method->Next)
2034 if (Method->Method)
2035 DataLen += 4;
2036 for (const ObjCMethodList *Method = &Methods.second; Method;
2037 Method = Method->Next)
2038 if (Method->Method)
2039 DataLen += 4;
2040 clang::io::Emit16(Out, DataLen);
2041 return std::make_pair(KeyLen, DataLen);
2042 }
2043
Douglas Gregor2d711832009-04-25 17:48:32 +00002044 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
2045 uint64_t Start = Out.tell();
2046 assert((Start >> 32) == 0 && "Selector key offset too large");
2047 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002048 unsigned N = Sel.getNumArgs();
2049 clang::io::Emit16(Out, N);
2050 if (N == 0)
2051 N = 1;
2052 for (unsigned I = 0; I != N; ++I)
2053 clang::io::Emit32(Out,
2054 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2055 }
2056
2057 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00002058 data_type_ref Methods, unsigned DataLen) {
2059 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002060 unsigned NumInstanceMethods = 0;
2061 for (const ObjCMethodList *Method = &Methods.first; Method;
2062 Method = Method->Next)
2063 if (Method->Method)
2064 ++NumInstanceMethods;
2065
2066 unsigned NumFactoryMethods = 0;
2067 for (const ObjCMethodList *Method = &Methods.second; Method;
2068 Method = Method->Next)
2069 if (Method->Method)
2070 ++NumFactoryMethods;
2071
2072 clang::io::Emit16(Out, NumInstanceMethods);
2073 clang::io::Emit16(Out, NumFactoryMethods);
2074 for (const ObjCMethodList *Method = &Methods.first; Method;
2075 Method = Method->Next)
2076 if (Method->Method)
2077 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002078 for (const ObjCMethodList *Method = &Methods.second; Method;
2079 Method = Method->Next)
2080 if (Method->Method)
2081 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00002082
2083 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002084 }
2085};
2086} // end anonymous namespace
2087
2088/// \brief Write the method pool into the PCH file.
2089///
2090/// The method pool contains both instance and factory methods, stored
2091/// in an on-disk hash table indexed by the selector.
2092void PCHWriter::WriteMethodPool(Sema &SemaRef) {
2093 using namespace llvm;
2094
2095 // Create and write out the blob that contains the instance and
2096 // factor method pools.
2097 bool Empty = true;
2098 {
2099 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
2100
2101 // Create the on-disk hash table representation. Start by
2102 // iterating through the instance method pool.
2103 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00002104 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002105 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2106 Instance = SemaRef.InstanceMethodPool.begin(),
2107 InstanceEnd = SemaRef.InstanceMethodPool.end();
2108 Instance != InstanceEnd; ++Instance) {
2109 // Check whether there is a factory method with the same
2110 // selector.
2111 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
2112 = SemaRef.FactoryMethodPool.find(Instance->first);
2113
2114 if (Factory == SemaRef.FactoryMethodPool.end())
2115 Generator.insert(Instance->first,
2116 std::make_pair(Instance->second,
2117 ObjCMethodList()));
2118 else
2119 Generator.insert(Instance->first,
2120 std::make_pair(Instance->second, Factory->second));
2121
Douglas Gregor2d711832009-04-25 17:48:32 +00002122 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002123 Empty = false;
2124 }
2125
2126 // Now iterate through the factory method pool, to pick up any
2127 // selectors that weren't already in the instance method pool.
2128 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2129 Factory = SemaRef.FactoryMethodPool.begin(),
2130 FactoryEnd = SemaRef.FactoryMethodPool.end();
2131 Factory != FactoryEnd; ++Factory) {
2132 // Check whether there is an instance method with the same
2133 // selector. If so, there is no work to do here.
2134 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
2135 = SemaRef.InstanceMethodPool.find(Factory->first);
2136
Douglas Gregor2d711832009-04-25 17:48:32 +00002137 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002138 Generator.insert(Factory->first,
2139 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00002140 ++NumSelectorsInMethodPool;
2141 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002142
2143 Empty = false;
2144 }
2145
Douglas Gregor2d711832009-04-25 17:48:32 +00002146 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002147 return;
2148
2149 // Create the on-disk hash table in a buffer.
2150 llvm::SmallVector<char, 4096> MethodPool;
2151 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00002152 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002153 {
2154 PCHMethodPoolTrait Trait(*this);
2155 llvm::raw_svector_ostream Out(MethodPool);
2156 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002157 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002158 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00002159
2160 // For every selector that we have seen but which was not
2161 // written into the hash table, write the selector itself and
2162 // record it's offset.
2163 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
2164 if (SelectorOffsets[I] == 0)
2165 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002166 }
2167
2168 // Create a blob abbreviation
2169 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2170 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2174 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2175
Douglas Gregor2d711832009-04-25 17:48:32 +00002176 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002177 RecordData Record;
2178 Record.push_back(pch::METHOD_POOL);
2179 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002180 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002181 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2182 &MethodPool.front(),
2183 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002184
2185 // Create a blob abbreviation for the selector table offsets.
2186 Abbrev = new BitCodeAbbrev();
2187 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2190 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2191
2192 // Write the selector offsets table.
2193 Record.clear();
2194 Record.push_back(pch::SELECTOR_OFFSETS);
2195 Record.push_back(SelectorOffsets.size());
2196 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2197 (const char *)&SelectorOffsets.front(),
2198 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002199 }
2200}
2201
2202namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002203class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2204 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002205 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002206
2207public:
2208 typedef const IdentifierInfo* key_type;
2209 typedef key_type key_type_ref;
2210
2211 typedef pch::IdentID data_type;
2212 typedef data_type data_type_ref;
2213
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002214 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2215 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002216
2217 static unsigned ComputeHash(const IdentifierInfo* II) {
2218 return clang::BernsteinHash(II->getName());
2219 }
2220
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002221 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002222 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2223 pch::IdentID ID) {
2224 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002225 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2226 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002227 if (II->hasMacroDefinition() &&
2228 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2229 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002230 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2231 DEnd = IdentifierResolver::end();
2232 D != DEnd; ++D)
2233 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002234 // We emit the key length after the data length so that the
2235 // "uninteresting" identifiers following the identifier hash table
2236 // structure will have the same (key length, key characters)
2237 // layout as the keys in the hash table. This also matches the
2238 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002239 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002240 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002241 return std::make_pair(KeyLen, DataLen);
2242 }
2243
2244 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2245 unsigned KeyLen) {
2246 // Record the location of the key data. This is used when generating
2247 // the mapping from persistent IDs to strings.
2248 Writer.SetIdentifierOffset(II, Out.tell());
2249 Out.write(II->getName(), KeyLen);
2250 }
2251
2252 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2253 pch::IdentID ID, unsigned) {
2254 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002255 bool hasMacroDefinition =
2256 II->hasMacroDefinition() &&
2257 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002258 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002259 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2260 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002261 Bits = (Bits << 1) | II->isExtensionToken();
2262 Bits = (Bits << 1) | II->isPoisoned();
2263 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2264 clang::io::Emit32(Out, Bits);
2265 clang::io::Emit32(Out, ID);
2266
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002267 if (hasMacroDefinition)
2268 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2269
Douglas Gregorc713da92009-04-21 22:25:48 +00002270 // Emit the declaration IDs in reverse order, because the
2271 // IdentifierResolver provides the declarations as they would be
2272 // visible (e.g., the function "stat" would come before the struct
2273 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2274 // adds declarations to the end of the list (so we need to see the
2275 // struct "status" before the function "status").
2276 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2277 IdentifierResolver::end());
2278 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2279 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002280 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002281 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002282 }
2283};
2284} // end anonymous namespace
2285
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002286/// \brief Write the identifier table into the PCH file.
2287///
2288/// The identifier table consists of a blob containing string data
2289/// (the actual identifiers themselves) and a separate "offsets" index
2290/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002291void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002292 using namespace llvm;
2293
2294 // Create and write out the blob that contains the identifier
2295 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002296 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002297 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002298 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2299
Douglas Gregor85c4a872009-04-25 21:04:17 +00002300 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2301
Douglas Gregorff9a6092009-04-20 20:36:09 +00002302 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002303 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2304 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2305 ID != IDEnd; ++ID) {
2306 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002307
2308 // Classify each identifier as either "interesting" or "not
2309 // interesting". Interesting identifiers are those that have
2310 // additional information that needs to be read from the PCH
2311 // file, e.g., a built-in ID, declaration chain, or macro
2312 // definition. These identifiers are placed into the hash table
2313 // so that they can be found when looked up in the user program.
2314 // All other identifiers are "uninteresting", which means that
2315 // the IdentifierInfo built by default has all of the
2316 // information we care about. Such identifiers are placed after
2317 // the hash table.
2318 const IdentifierInfo *II = ID->first;
2319 if (II->isPoisoned() ||
2320 II->isExtensionToken() ||
2321 II->hasMacroDefinition() ||
2322 II->getObjCOrBuiltinID() ||
2323 II->getFETokenInfo<void>())
2324 Generator.insert(ID->first, ID->second);
2325 else
2326 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002327 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002328
Douglas Gregorff9a6092009-04-20 20:36:09 +00002329 // Create the on-disk hash table in a buffer.
2330 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002331 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002332 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002333 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002334 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002335 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002336 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002337 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002338
2339 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2340 const IdentifierInfo *II = UninterestingIdentifiers[I];
2341 unsigned N = II->getLength() + 1;
2342 clang::io::Emit16(Out, N);
2343 SetIdentifierOffset(II, Out.tell());
2344 Out.write(II->getName(), N);
2345 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002346 }
2347
2348 // Create a blob abbreviation
2349 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2350 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002353 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002354
2355 // Write the identifier table
2356 RecordData Record;
2357 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002358 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002359 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2360 &IdentifierTable.front(),
2361 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002362 }
2363
2364 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002365 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2366 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2369 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2370
2371 RecordData Record;
2372 Record.push_back(pch::IDENTIFIER_OFFSET);
2373 Record.push_back(IdentifierOffsets.size());
2374 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2375 (const char *)&IdentifierOffsets.front(),
2376 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002377}
2378
Douglas Gregor1c507882009-04-15 21:30:51 +00002379/// \brief Write a record containing the given attributes.
2380void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2381 RecordData Record;
2382 for (; Attr; Attr = Attr->getNext()) {
2383 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2384 Record.push_back(Attr->isInherited());
2385 switch (Attr->getKind()) {
2386 case Attr::Alias:
2387 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2388 break;
2389
2390 case Attr::Aligned:
2391 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2392 break;
2393
2394 case Attr::AlwaysInline:
2395 break;
2396
2397 case Attr::AnalyzerNoReturn:
2398 break;
2399
2400 case Attr::Annotate:
2401 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2402 break;
2403
2404 case Attr::AsmLabel:
2405 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2406 break;
2407
2408 case Attr::Blocks:
2409 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2410 break;
2411
2412 case Attr::Cleanup:
2413 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2414 break;
2415
2416 case Attr::Const:
2417 break;
2418
2419 case Attr::Constructor:
2420 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2421 break;
2422
2423 case Attr::DLLExport:
2424 case Attr::DLLImport:
2425 case Attr::Deprecated:
2426 break;
2427
2428 case Attr::Destructor:
2429 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2430 break;
2431
2432 case Attr::FastCall:
2433 break;
2434
2435 case Attr::Format: {
2436 const FormatAttr *Format = cast<FormatAttr>(Attr);
2437 AddString(Format->getType(), Record);
2438 Record.push_back(Format->getFormatIdx());
2439 Record.push_back(Format->getFirstArg());
2440 break;
2441 }
2442
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002443 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002444 case Attr::IBOutletKind:
2445 case Attr::NoReturn:
2446 case Attr::NoThrow:
2447 case Attr::Nodebug:
2448 case Attr::Noinline:
2449 break;
2450
2451 case Attr::NonNull: {
2452 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2453 Record.push_back(NonNull->size());
2454 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2455 break;
2456 }
2457
2458 case Attr::ObjCException:
2459 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002460 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002461 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002462 case Attr::Overloadable:
2463 break;
2464
2465 case Attr::Packed:
2466 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2467 break;
2468
2469 case Attr::Pure:
2470 break;
2471
2472 case Attr::Regparm:
2473 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2474 break;
2475
2476 case Attr::Section:
2477 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2478 break;
2479
2480 case Attr::StdCall:
2481 case Attr::TransparentUnion:
2482 case Attr::Unavailable:
2483 case Attr::Unused:
2484 case Attr::Used:
2485 break;
2486
2487 case Attr::Visibility:
2488 // FIXME: stable encoding
2489 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2490 break;
2491
2492 case Attr::WarnUnusedResult:
2493 case Attr::Weak:
2494 case Attr::WeakImport:
2495 break;
2496 }
2497 }
2498
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002499 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002500}
2501
2502void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2503 Record.push_back(Str.size());
2504 Record.insert(Record.end(), Str.begin(), Str.end());
2505}
2506
Douglas Gregorff9a6092009-04-20 20:36:09 +00002507/// \brief Note that the identifier II occurs at the given offset
2508/// within the identifier table.
2509void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002510 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002511}
2512
Douglas Gregor2d711832009-04-25 17:48:32 +00002513/// \brief Note that the selector Sel occurs at the given offset
2514/// within the method pool/selector table.
2515void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2516 unsigned ID = SelectorIDs[Sel];
2517 assert(ID && "Unknown selector");
2518 SelectorOffsets[ID - 1] = Offset;
2519}
2520
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002521PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002522 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002523 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2524 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002525
Douglas Gregor87887da2009-04-20 15:53:59 +00002526void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002527 using namespace llvm;
2528
Douglas Gregor87887da2009-04-20 15:53:59 +00002529 ASTContext &Context = SemaRef.Context;
2530 Preprocessor &PP = SemaRef.PP;
2531
Douglas Gregorc34897d2009-04-09 22:27:44 +00002532 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002533 Stream.Emit((unsigned)'C', 8);
2534 Stream.Emit((unsigned)'P', 8);
2535 Stream.Emit((unsigned)'C', 8);
2536 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00002537
2538 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002539
2540 // The translation unit is the first declaration we'll emit.
2541 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2542 DeclsToEmit.push(Context.getTranslationUnitDecl());
2543
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002544 // Make sure that we emit IdentifierInfos (and any attached
2545 // declarations) for builtins.
2546 {
2547 IdentifierTable &Table = PP.getIdentifierTable();
2548 llvm::SmallVector<const char *, 32> BuiltinNames;
2549 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2550 Context.getLangOptions().NoBuiltin);
2551 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2552 getIdentifierRef(&Table.get(BuiltinNames[I]));
2553 }
2554
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002555 // Build a record containing all of the tentative definitions in
2556 // this header file. Generally, this record will be empty.
2557 RecordData TentativeDefinitions;
2558 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2559 TD = SemaRef.TentativeDefinitions.begin(),
2560 TDEnd = SemaRef.TentativeDefinitions.end();
2561 TD != TDEnd; ++TD)
2562 AddDeclRef(TD->second, TentativeDefinitions);
2563
Douglas Gregor062d9482009-04-22 22:18:58 +00002564 // Build a record containing all of the locally-scoped external
2565 // declarations in this header file. Generally, this record will be
2566 // empty.
2567 RecordData LocallyScopedExternalDecls;
2568 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2569 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2570 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2571 TD != TDEnd; ++TD)
2572 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2573
Douglas Gregorc34897d2009-04-09 22:27:44 +00002574 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002575 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002576 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002577 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002578 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002579 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002580 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002581
2582 // Keep writing types and declarations until all types and
2583 // declarations have been written.
2584 do {
2585 if (!DeclsToEmit.empty())
2586 WriteDeclsBlock(Context);
2587 if (!TypesToEmit.empty())
2588 WriteTypesBlock(Context);
2589 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2590
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002591 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002592 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002593
2594 // Write the type offsets array
2595 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2596 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2599 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2600 Record.clear();
2601 Record.push_back(pch::TYPE_OFFSET);
2602 Record.push_back(TypeOffsets.size());
2603 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2604 (const char *)&TypeOffsets.front(),
2605 TypeOffsets.size() * sizeof(uint64_t));
2606
2607 // Write the declaration offsets array
2608 Abbrev = new BitCodeAbbrev();
2609 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2612 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2613 Record.clear();
2614 Record.push_back(pch::DECL_OFFSET);
2615 Record.push_back(DeclOffsets.size());
2616 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2617 (const char *)&DeclOffsets.front(),
2618 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002619
2620 // Write the record of special types.
2621 Record.clear();
2622 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002623 AddTypeRef(Context.getObjCIdType(), Record);
2624 AddTypeRef(Context.getObjCSelType(), Record);
2625 AddTypeRef(Context.getObjCProtoType(), Record);
2626 AddTypeRef(Context.getObjCClassType(), Record);
2627 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2628 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002629 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2630
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002631 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002632 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002633 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002634
2635 // Write the record containing tentative definitions.
2636 if (!TentativeDefinitions.empty())
2637 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002638
2639 // Write the record containing locally-scoped external definitions.
2640 if (!LocallyScopedExternalDecls.empty())
2641 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2642 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002643
2644 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002645 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002646 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002647 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002648 Record.push_back(NumLexicalDeclContexts);
2649 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002650 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002651 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002652}
2653
2654void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2655 Record.push_back(Loc.getRawEncoding());
2656}
2657
2658void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2659 Record.push_back(Value.getBitWidth());
2660 unsigned N = Value.getNumWords();
2661 const uint64_t* Words = Value.getRawData();
2662 for (unsigned I = 0; I != N; ++I)
2663 Record.push_back(Words[I]);
2664}
2665
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002666void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2667 Record.push_back(Value.isUnsigned());
2668 AddAPInt(Value, Record);
2669}
2670
Douglas Gregore2f37202009-04-14 21:55:33 +00002671void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2672 AddAPInt(Value.bitcastToAPInt(), Record);
2673}
2674
Douglas Gregorc34897d2009-04-09 22:27:44 +00002675void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002676 Record.push_back(getIdentifierRef(II));
2677}
2678
2679pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2680 if (II == 0)
2681 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002682
2683 pch::IdentID &ID = IdentifierIDs[II];
2684 if (ID == 0)
2685 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002686 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002687}
2688
Steve Naroff9e84d782009-04-23 10:39:46 +00002689void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2690 if (SelRef.getAsOpaquePtr() == 0) {
2691 Record.push_back(0);
2692 return;
2693 }
2694
2695 pch::SelectorID &SID = SelectorIDs[SelRef];
2696 if (SID == 0) {
2697 SID = SelectorIDs.size();
2698 SelVector.push_back(SelRef);
2699 }
2700 Record.push_back(SID);
2701}
2702
Douglas Gregorc34897d2009-04-09 22:27:44 +00002703void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2704 if (T.isNull()) {
2705 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2706 return;
2707 }
2708
2709 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002710 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002711 switch (BT->getKind()) {
2712 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2713 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2714 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2715 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2716 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2717 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2718 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2719 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2720 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2721 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2722 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2723 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2724 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2725 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2726 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2727 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2728 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2729 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2730 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2731 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2732 }
2733
2734 Record.push_back((ID << 3) | T.getCVRQualifiers());
2735 return;
2736 }
2737
Douglas Gregorac8f2802009-04-10 17:25:41 +00002738 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002739 if (ID == 0) {
2740 // We haven't seen this type before. Assign it a new ID and put it
2741 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002742 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002743 TypesToEmit.push(T.getTypePtr());
2744 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002745
2746 // Encode the type qualifiers in the type reference.
2747 Record.push_back((ID << 3) | T.getCVRQualifiers());
2748}
2749
2750void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2751 if (D == 0) {
2752 Record.push_back(0);
2753 return;
2754 }
2755
Douglas Gregorac8f2802009-04-10 17:25:41 +00002756 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002757 if (ID == 0) {
2758 // We haven't seen this declaration before. Give it a new ID and
2759 // enqueue it in the list of declarations to emit.
2760 ID = DeclIDs.size();
2761 DeclsToEmit.push(const_cast<Decl *>(D));
2762 }
2763
2764 Record.push_back(ID);
2765}
2766
Douglas Gregorff9a6092009-04-20 20:36:09 +00002767pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2768 if (D == 0)
2769 return 0;
2770
2771 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2772 return DeclIDs[D];
2773}
2774
Douglas Gregorc34897d2009-04-09 22:27:44 +00002775void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2776 Record.push_back(Name.getNameKind());
2777 switch (Name.getNameKind()) {
2778 case DeclarationName::Identifier:
2779 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2780 break;
2781
2782 case DeclarationName::ObjCZeroArgSelector:
2783 case DeclarationName::ObjCOneArgSelector:
2784 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002785 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002786 break;
2787
2788 case DeclarationName::CXXConstructorName:
2789 case DeclarationName::CXXDestructorName:
2790 case DeclarationName::CXXConversionFunctionName:
2791 AddTypeRef(Name.getCXXNameType(), Record);
2792 break;
2793
2794 case DeclarationName::CXXOperatorName:
2795 Record.push_back(Name.getCXXOverloadedOperator());
2796 break;
2797
2798 case DeclarationName::CXXUsingDirective:
2799 // No extra data to emit
2800 break;
2801 }
2802}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002803
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002804/// \brief Write the given substatement or subexpression to the
2805/// bitstream.
2806void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002807 RecordData Record;
2808 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002809 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002810
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002811 if (!S) {
2812 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002813 return;
2814 }
2815
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002816 Writer.Code = pch::STMT_NULL_PTR;
2817 Writer.Visit(S);
2818 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002819 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002820 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002821}
2822
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002823/// \brief Flush all of the statements that have been added to the
2824/// queue via AddStmt().
2825void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002826 RecordData Record;
2827 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002828
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002829 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002830 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002831 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002832
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002833 if (!S) {
2834 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002835 continue;
2836 }
2837
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002838 Writer.Code = pch::STMT_NULL_PTR;
2839 Writer.Visit(S);
2840 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002841 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002842 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002843
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002844 assert(N == StmtsToEmit.size() &&
2845 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002846
2847 // Note that we are at the end of a full expression. Any
2848 // expression records that follow this one are part of a different
2849 // expression.
2850 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002851 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002852 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002853
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002854 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002855 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002856}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002857
2858unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2859 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2860 "SwitchCase recorded twice");
2861 unsigned NextID = SwitchCaseIDs.size();
2862 SwitchCaseIDs[S] = NextID;
2863 return NextID;
2864}
2865
2866unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2867 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2868 "SwitchCase hasn't been seen yet");
2869 return SwitchCaseIDs[S];
2870}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002871
2872/// \brief Retrieve the ID for the given label statement, which may
2873/// or may not have been emitted yet.
2874unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2875 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2876 if (Pos != LabelIDs.end())
2877 return Pos->second;
2878
2879 unsigned NextID = LabelIDs.size();
2880 LabelIDs[S] = NextID;
2881 return NextID;
2882}