blob: 9f564cac4f22612d552c31b65043503e8121e445 [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);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001421 RECORD(DECL_ATTR);
1422 RECORD(DECL_TRANSLATION_UNIT);
1423 RECORD(DECL_TYPEDEF);
1424 RECORD(DECL_ENUM);
1425 RECORD(DECL_RECORD);
1426 RECORD(DECL_ENUM_CONSTANT);
1427 RECORD(DECL_FUNCTION);
1428 RECORD(DECL_OBJC_METHOD);
1429 RECORD(DECL_OBJC_INTERFACE);
1430 RECORD(DECL_OBJC_PROTOCOL);
1431 RECORD(DECL_OBJC_IVAR);
1432 RECORD(DECL_OBJC_AT_DEFS_FIELD);
1433 RECORD(DECL_OBJC_CLASS);
1434 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
1435 RECORD(DECL_OBJC_CATEGORY);
1436 RECORD(DECL_OBJC_CATEGORY_IMPL);
1437 RECORD(DECL_OBJC_IMPLEMENTATION);
1438 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1439 RECORD(DECL_OBJC_PROPERTY);
1440 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +00001441 RECORD(DECL_FIELD);
1442 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001443 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +00001444 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001445 RECORD(DECL_ORIGINAL_PARM_VAR);
1446 RECORD(DECL_FILE_SCOPE_ASM);
1447 RECORD(DECL_BLOCK);
1448 RECORD(DECL_CONTEXT_LEXICAL);
1449 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattner920673a2009-04-26 22:26:21 +00001450
1451#undef RECORD
1452#undef BLOCK
1453 Stream.ExitBlock();
1454}
1455
1456
Douglas Gregorb5887f32009-04-10 21:16:55 +00001457/// \brief Write the target triple (e.g., i686-apple-darwin9).
1458void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1459 using namespace llvm;
1460 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1461 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001463 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001464
1465 RecordData Record;
1466 Record.push_back(pch::TARGET_TRIPLE);
1467 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001468 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001469}
1470
1471/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001472void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1473 RecordData Record;
1474 Record.push_back(LangOpts.Trigraphs);
1475 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1476 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1477 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1478 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1479 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1480 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1481 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1482 Record.push_back(LangOpts.C99); // C99 Support
1483 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1484 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1485 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1486 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1487 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1488
1489 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1490 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1491 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1492
1493 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1494 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1495 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1496 Record.push_back(LangOpts.LaxVectorConversions);
1497 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1498
1499 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1500 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1501 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1502
1503 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1504 // by locks.
1505 Record.push_back(LangOpts.Blocks); // block extension to C
1506 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1507 // they are unused.
1508 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1509 // (modulo the platform support).
1510
1511 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1512 // signed integer arithmetic overflows.
1513
1514 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1515 // may be ripped out at any time.
1516
1517 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1518 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1519 // defined.
1520 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1521 // opposed to __DYNAMIC__).
1522 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1523
1524 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1525 // used (instead of C99 semantics).
1526 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1527 Record.push_back(LangOpts.getGCMode());
1528 Record.push_back(LangOpts.getVisibilityMode());
1529 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001530 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001531}
1532
Douglas Gregorab1cef72009-04-10 03:52:48 +00001533//===----------------------------------------------------------------------===//
1534// Source Manager Serialization
1535//===----------------------------------------------------------------------===//
1536
1537/// \brief Create an abbreviation for the SLocEntry that refers to a
1538/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001539static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001540 using namespace llvm;
1541 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1542 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
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 a
1552/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001553static unsigned CreateSLocBufferAbbrev(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_BUFFER_ENTRY));
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001562 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001563}
1564
1565/// \brief Create an abbreviation for the SLocEntry that refers to a
1566/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001567static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001568 using namespace llvm;
1569 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1570 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001572 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001573}
1574
1575/// \brief Create an abbreviation for the SLocEntry that refers to an
1576/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001577static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001578 using namespace llvm;
1579 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1580 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001586 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001587}
1588
1589/// \brief Writes the block containing the serialized form of the
1590/// source manager.
1591///
1592/// TODO: We should probably use an on-disk hash table (stored in a
1593/// blob), indexed based on the file name, so that we only create
1594/// entries for files that we actually need. In the common case (no
1595/// errors), we probably won't have to create file entries for any of
1596/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001597void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1598 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001599 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001600 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001601
1602 // Abbreviations for the various kinds of source-location entries.
1603 int SLocFileAbbrv = -1;
1604 int SLocBufferAbbrv = -1;
1605 int SLocBufferBlobAbbrv = -1;
1606 int SLocInstantiationAbbrv = -1;
1607
1608 // Write out the source location entry table. We skip the first
1609 // entry, which is always the same dummy entry.
1610 RecordData Record;
1611 for (SourceManager::sloc_entry_iterator
1612 SLoc = SourceMgr.sloc_entry_begin() + 1,
1613 SLocEnd = SourceMgr.sloc_entry_end();
1614 SLoc != SLocEnd; ++SLoc) {
1615 // Figure out which record code to use.
1616 unsigned Code;
1617 if (SLoc->isFile()) {
1618 if (SLoc->getFile().getContentCache()->Entry)
1619 Code = pch::SM_SLOC_FILE_ENTRY;
1620 else
1621 Code = pch::SM_SLOC_BUFFER_ENTRY;
1622 } else
1623 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1624 Record.push_back(Code);
1625
1626 Record.push_back(SLoc->getOffset());
1627 if (SLoc->isFile()) {
1628 const SrcMgr::FileInfo &File = SLoc->getFile();
1629 Record.push_back(File.getIncludeLoc().getRawEncoding());
1630 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001631 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001632
1633 const SrcMgr::ContentCache *Content = File.getContentCache();
1634 if (Content->Entry) {
1635 // The source location entry is a file. The blob associated
1636 // with this entry is the file name.
1637 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001638 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1639 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001640 Content->Entry->getName(),
1641 strlen(Content->Entry->getName()));
1642 } else {
1643 // The source location entry is a buffer. The blob associated
1644 // with this entry contains the contents of the buffer.
1645 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001646 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1647 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001648 }
1649
1650 // We add one to the size so that we capture the trailing NULL
1651 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1652 // the reader side).
1653 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1654 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001655 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001656 Record.clear();
1657 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001658 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001659 Buffer->getBufferStart(),
1660 Buffer->getBufferSize() + 1);
1661 }
1662 } else {
1663 // The source location entry is an instantiation.
1664 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1665 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1666 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1667 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1668
Douglas Gregor364e5802009-04-15 18:05:10 +00001669 // Compute the token length for this macro expansion.
1670 unsigned NextOffset = SourceMgr.getNextOffset();
1671 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1672 if (++NextSLoc != SLocEnd)
1673 NextOffset = NextSLoc->getOffset();
1674 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1675
Douglas Gregorab1cef72009-04-10 03:52:48 +00001676 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001677 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1678 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001679 }
1680
1681 Record.clear();
1682 }
1683
Douglas Gregor635f97f2009-04-13 16:31:14 +00001684 // Write the line table.
1685 if (SourceMgr.hasLineTable()) {
1686 LineTableInfo &LineTable = SourceMgr.getLineTable();
1687
1688 // Emit the file names
1689 Record.push_back(LineTable.getNumFilenames());
1690 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1691 // Emit the file name
1692 const char *Filename = LineTable.getFilename(I);
1693 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1694 Record.push_back(FilenameLen);
1695 if (FilenameLen)
1696 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1697 }
1698
1699 // Emit the line entries
1700 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1701 L != LEnd; ++L) {
1702 // Emit the file ID
1703 Record.push_back(L->first);
1704
1705 // Emit the line entries
1706 Record.push_back(L->second.size());
1707 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1708 LEEnd = L->second.end();
1709 LE != LEEnd; ++LE) {
1710 Record.push_back(LE->FileOffset);
1711 Record.push_back(LE->LineNo);
1712 Record.push_back(LE->FilenameID);
1713 Record.push_back((unsigned)LE->FileKind);
1714 Record.push_back(LE->IncludeOffset);
1715 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001716 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001717 }
1718 }
1719
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001720 // Loop over all the header files.
1721 HeaderSearch &HS = PP.getHeaderSearchInfo();
1722 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1723 E = HS.header_file_end();
1724 I != E; ++I) {
1725 Record.push_back(I->isImport);
1726 Record.push_back(I->DirInfo);
1727 Record.push_back(I->NumIncludes);
1728 if (I->ControllingMacro)
1729 AddIdentifierRef(I->ControllingMacro, Record);
1730 else
1731 Record.push_back(0);
1732 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1733 Record.clear();
1734 }
1735
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001736 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001737}
1738
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001739/// \brief Writes the block containing the serialized form of the
1740/// preprocessor.
1741///
Chris Lattner850eabd2009-04-10 18:08:30 +00001742void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001743 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001744
Chris Lattner4b21c202009-04-13 01:29:17 +00001745 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1746 if (PP.getCounterValue() != 0) {
1747 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001748 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001749 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001750 }
1751
1752 // Enter the preprocessor block.
1753 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001754
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001755 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1756 // FIXME: use diagnostics subsystem for localization etc.
1757 if (PP.SawDateOrTime())
1758 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1759
Chris Lattner1b094952009-04-10 18:00:12 +00001760 // Loop over all the macro definitions that are live at the end of the file,
1761 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001762 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1763 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001764 // FIXME: This emits macros in hash table order, we should do it in a stable
1765 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001766 MacroInfo *MI = I->second;
1767
1768 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1769 // been redefined by the header (in which case they are not isBuiltinMacro).
1770 if (MI->isBuiltinMacro())
1771 continue;
1772
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001773 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001774 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001775 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001776 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1777 Record.push_back(MI->isUsed());
1778
1779 unsigned Code;
1780 if (MI->isObjectLike()) {
1781 Code = pch::PP_MACRO_OBJECT_LIKE;
1782 } else {
1783 Code = pch::PP_MACRO_FUNCTION_LIKE;
1784
1785 Record.push_back(MI->isC99Varargs());
1786 Record.push_back(MI->isGNUVarargs());
1787 Record.push_back(MI->getNumArgs());
1788 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1789 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001790 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001791 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001792 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001793 Record.clear();
1794
Chris Lattner850eabd2009-04-10 18:08:30 +00001795 // Emit the tokens array.
1796 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1797 // Note that we know that the preprocessor does not have any annotation
1798 // tokens in it because they are created by the parser, and thus can't be
1799 // in a macro definition.
1800 const Token &Tok = MI->getReplacementToken(TokNo);
1801
1802 Record.push_back(Tok.getLocation().getRawEncoding());
1803 Record.push_back(Tok.getLength());
1804
Chris Lattner850eabd2009-04-10 18:08:30 +00001805 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1806 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001807 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001808
1809 // FIXME: Should translate token kind to a stable encoding.
1810 Record.push_back(Tok.getKind());
1811 // FIXME: Should translate token flags to a stable encoding.
1812 Record.push_back(Tok.getFlags());
1813
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001814 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001815 Record.clear();
1816 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001817 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001818 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001819 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001820}
1821
1822
Douglas Gregorc34897d2009-04-09 22:27:44 +00001823/// \brief Write the representation of a type to the PCH stream.
1824void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001825 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001826 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001827 ID = NextTypeID++;
1828
1829 // Record the offset for this type.
1830 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001831 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001832 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1833 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001834 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001835 }
1836
1837 RecordData Record;
1838
1839 // Emit the type's representation.
1840 PCHTypeWriter W(*this, Record);
1841 switch (T->getTypeClass()) {
1842 // For all of the concrete, non-dependent types, call the
1843 // appropriate visitor function.
1844#define TYPE(Class, Base) \
1845 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1846#define ABSTRACT_TYPE(Class, Base)
1847#define DEPENDENT_TYPE(Class, Base)
1848#include "clang/AST/TypeNodes.def"
1849
1850 // For all of the dependent type nodes (which only occur in C++
1851 // templates), produce an error.
1852#define TYPE(Class, Base)
1853#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1854#include "clang/AST/TypeNodes.def"
1855 assert(false && "Cannot serialize dependent type nodes");
1856 break;
1857 }
1858
1859 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001860 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001861
1862 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001863 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001864}
1865
1866/// \brief Write a block containing all of the types.
1867void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001868 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001869 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001870
Douglas Gregore43f0972009-04-26 03:49:13 +00001871 // Emit all of the types that need to be emitted (so far).
1872 while (!TypesToEmit.empty()) {
1873 const Type *T = TypesToEmit.front();
1874 TypesToEmit.pop();
1875 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1876 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001877 }
1878
1879 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001880 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001881}
1882
1883/// \brief Write the block containing all of the declaration IDs
1884/// lexically declared within the given DeclContext.
1885///
1886/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1887/// bistream, or 0 if no block was written.
1888uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1889 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001890 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001891 return 0;
1892
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001893 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001894 RecordData Record;
1895 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1896 DEnd = DC->decls_end(Context);
1897 D != DEnd; ++D)
1898 AddDeclRef(*D, Record);
1899
Douglas Gregoraf136d92009-04-22 22:34:57 +00001900 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001901 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001902 return Offset;
1903}
1904
1905/// \brief Write the block containing all of the declaration IDs
1906/// visible from the given DeclContext.
1907///
1908/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1909/// bistream, or 0 if no block was written.
1910uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1911 DeclContext *DC) {
1912 if (DC->getPrimaryContext() != DC)
1913 return 0;
1914
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001915 // Since there is no name lookup into functions or methods, and we
1916 // perform name lookup for the translation unit via the
1917 // IdentifierInfo chains, don't bother to build a
1918 // visible-declarations table for these entities.
1919 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001920 return 0;
1921
Douglas Gregorc34897d2009-04-09 22:27:44 +00001922 // Force the DeclContext to build a its name-lookup table.
1923 DC->lookup(Context, DeclarationName());
1924
1925 // Serialize the contents of the mapping used for lookup. Note that,
1926 // although we have two very different code paths, the serialized
1927 // representation is the same for both cases: a declaration name,
1928 // followed by a size, followed by references to the visible
1929 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001930 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001931 RecordData Record;
1932 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001933 if (!Map)
1934 return 0;
1935
Douglas Gregorc34897d2009-04-09 22:27:44 +00001936 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1937 D != DEnd; ++D) {
1938 AddDeclarationName(D->first, Record);
1939 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1940 Record.push_back(Result.second - Result.first);
1941 for(; Result.first != Result.second; ++Result.first)
1942 AddDeclRef(*Result.first, Record);
1943 }
1944
1945 if (Record.size() == 0)
1946 return 0;
1947
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001948 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001949 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001950 return Offset;
1951}
1952
1953/// \brief Write a block containing all of the declarations.
1954void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001955 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001956 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001957
1958 // Emit all of the declarations.
1959 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001960 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001961 while (!DeclsToEmit.empty()) {
1962 // Pull the next declaration off the queue
1963 Decl *D = DeclsToEmit.front();
1964 DeclsToEmit.pop();
1965
1966 // If this declaration is also a DeclContext, write blocks for the
1967 // declarations that lexically stored inside its context and those
1968 // declarations that are visible from its context. These blocks
1969 // are written before the declaration itself so that we can put
1970 // their offsets into the record for the declaration.
1971 uint64_t LexicalOffset = 0;
1972 uint64_t VisibleOffset = 0;
1973 DeclContext *DC = dyn_cast<DeclContext>(D);
1974 if (DC) {
1975 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1976 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1977 }
1978
1979 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001980 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001981 if (ID == 0)
1982 ID = DeclIDs.size();
1983
1984 unsigned Index = ID - 1;
1985
1986 // Record the offset for this declaration
1987 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001988 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001989 else if (DeclOffsets.size() < Index) {
1990 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001991 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001992 }
1993
1994 // Build and emit a record for this declaration
1995 Record.clear();
1996 W.Code = (pch::DeclCode)0;
1997 W.Visit(D);
1998 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001999
2000 if (!W.Code) {
2001 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
2002 D->getDeclKindName());
2003 assert(false && "Unhandled declaration kind while generating PCH");
2004 exit(-1);
2005 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002006 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00002007
Douglas Gregor1c507882009-04-15 21:30:51 +00002008 // If the declaration had any attributes, write them now.
2009 if (D->hasAttrs())
2010 WriteAttributeRecord(D->getAttrs());
2011
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002012 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002013 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002014
Douglas Gregor631f6c62009-04-14 00:24:19 +00002015 // Note external declarations so that we can add them to a record
2016 // in the PCH file later.
2017 if (isa<FileScopeAsmDecl>(D))
2018 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002019 }
2020
2021 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002022 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002023}
2024
Douglas Gregorff9a6092009-04-20 20:36:09 +00002025namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002026// Trait used for the on-disk hash table used in the method pool.
2027class VISIBILITY_HIDDEN PCHMethodPoolTrait {
2028 PCHWriter &Writer;
2029
2030public:
2031 typedef Selector key_type;
2032 typedef key_type key_type_ref;
2033
2034 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
2035 typedef const data_type& data_type_ref;
2036
2037 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
2038
2039 static unsigned ComputeHash(Selector Sel) {
2040 unsigned N = Sel.getNumArgs();
2041 if (N == 0)
2042 ++N;
2043 unsigned R = 5381;
2044 for (unsigned I = 0; I != N; ++I)
2045 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
2046 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
2047 return R;
2048 }
2049
2050 std::pair<unsigned,unsigned>
2051 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2052 data_type_ref Methods) {
2053 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2054 clang::io::Emit16(Out, KeyLen);
2055 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
2056 for (const ObjCMethodList *Method = &Methods.first; Method;
2057 Method = Method->Next)
2058 if (Method->Method)
2059 DataLen += 4;
2060 for (const ObjCMethodList *Method = &Methods.second; Method;
2061 Method = Method->Next)
2062 if (Method->Method)
2063 DataLen += 4;
2064 clang::io::Emit16(Out, DataLen);
2065 return std::make_pair(KeyLen, DataLen);
2066 }
2067
Douglas Gregor2d711832009-04-25 17:48:32 +00002068 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
2069 uint64_t Start = Out.tell();
2070 assert((Start >> 32) == 0 && "Selector key offset too large");
2071 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002072 unsigned N = Sel.getNumArgs();
2073 clang::io::Emit16(Out, N);
2074 if (N == 0)
2075 N = 1;
2076 for (unsigned I = 0; I != N; ++I)
2077 clang::io::Emit32(Out,
2078 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2079 }
2080
2081 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00002082 data_type_ref Methods, unsigned DataLen) {
2083 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002084 unsigned NumInstanceMethods = 0;
2085 for (const ObjCMethodList *Method = &Methods.first; Method;
2086 Method = Method->Next)
2087 if (Method->Method)
2088 ++NumInstanceMethods;
2089
2090 unsigned NumFactoryMethods = 0;
2091 for (const ObjCMethodList *Method = &Methods.second; Method;
2092 Method = Method->Next)
2093 if (Method->Method)
2094 ++NumFactoryMethods;
2095
2096 clang::io::Emit16(Out, NumInstanceMethods);
2097 clang::io::Emit16(Out, NumFactoryMethods);
2098 for (const ObjCMethodList *Method = &Methods.first; Method;
2099 Method = Method->Next)
2100 if (Method->Method)
2101 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002102 for (const ObjCMethodList *Method = &Methods.second; Method;
2103 Method = Method->Next)
2104 if (Method->Method)
2105 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00002106
2107 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002108 }
2109};
2110} // end anonymous namespace
2111
2112/// \brief Write the method pool into the PCH file.
2113///
2114/// The method pool contains both instance and factory methods, stored
2115/// in an on-disk hash table indexed by the selector.
2116void PCHWriter::WriteMethodPool(Sema &SemaRef) {
2117 using namespace llvm;
2118
2119 // Create and write out the blob that contains the instance and
2120 // factor method pools.
2121 bool Empty = true;
2122 {
2123 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
2124
2125 // Create the on-disk hash table representation. Start by
2126 // iterating through the instance method pool.
2127 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00002128 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002129 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2130 Instance = SemaRef.InstanceMethodPool.begin(),
2131 InstanceEnd = SemaRef.InstanceMethodPool.end();
2132 Instance != InstanceEnd; ++Instance) {
2133 // Check whether there is a factory method with the same
2134 // selector.
2135 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
2136 = SemaRef.FactoryMethodPool.find(Instance->first);
2137
2138 if (Factory == SemaRef.FactoryMethodPool.end())
2139 Generator.insert(Instance->first,
2140 std::make_pair(Instance->second,
2141 ObjCMethodList()));
2142 else
2143 Generator.insert(Instance->first,
2144 std::make_pair(Instance->second, Factory->second));
2145
Douglas Gregor2d711832009-04-25 17:48:32 +00002146 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002147 Empty = false;
2148 }
2149
2150 // Now iterate through the factory method pool, to pick up any
2151 // selectors that weren't already in the instance method pool.
2152 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2153 Factory = SemaRef.FactoryMethodPool.begin(),
2154 FactoryEnd = SemaRef.FactoryMethodPool.end();
2155 Factory != FactoryEnd; ++Factory) {
2156 // Check whether there is an instance method with the same
2157 // selector. If so, there is no work to do here.
2158 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
2159 = SemaRef.InstanceMethodPool.find(Factory->first);
2160
Douglas Gregor2d711832009-04-25 17:48:32 +00002161 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002162 Generator.insert(Factory->first,
2163 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00002164 ++NumSelectorsInMethodPool;
2165 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002166
2167 Empty = false;
2168 }
2169
Douglas Gregor2d711832009-04-25 17:48:32 +00002170 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002171 return;
2172
2173 // Create the on-disk hash table in a buffer.
2174 llvm::SmallVector<char, 4096> MethodPool;
2175 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00002176 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002177 {
2178 PCHMethodPoolTrait Trait(*this);
2179 llvm::raw_svector_ostream Out(MethodPool);
2180 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002181 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002182 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00002183
2184 // For every selector that we have seen but which was not
2185 // written into the hash table, write the selector itself and
2186 // record it's offset.
2187 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
2188 if (SelectorOffsets[I] == 0)
2189 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002190 }
2191
2192 // Create a blob abbreviation
2193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2194 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2198 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2199
Douglas Gregor2d711832009-04-25 17:48:32 +00002200 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002201 RecordData Record;
2202 Record.push_back(pch::METHOD_POOL);
2203 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002204 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002205 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2206 &MethodPool.front(),
2207 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002208
2209 // Create a blob abbreviation for the selector table offsets.
2210 Abbrev = new BitCodeAbbrev();
2211 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2214 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2215
2216 // Write the selector offsets table.
2217 Record.clear();
2218 Record.push_back(pch::SELECTOR_OFFSETS);
2219 Record.push_back(SelectorOffsets.size());
2220 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2221 (const char *)&SelectorOffsets.front(),
2222 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002223 }
2224}
2225
2226namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002227class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2228 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002229 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002230
2231public:
2232 typedef const IdentifierInfo* key_type;
2233 typedef key_type key_type_ref;
2234
2235 typedef pch::IdentID data_type;
2236 typedef data_type data_type_ref;
2237
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002238 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2239 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002240
2241 static unsigned ComputeHash(const IdentifierInfo* II) {
2242 return clang::BernsteinHash(II->getName());
2243 }
2244
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002245 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002246 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2247 pch::IdentID ID) {
2248 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002249 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2250 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002251 if (II->hasMacroDefinition() &&
2252 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2253 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002254 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2255 DEnd = IdentifierResolver::end();
2256 D != DEnd; ++D)
2257 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002258 // We emit the key length after the data length so that the
2259 // "uninteresting" identifiers following the identifier hash table
2260 // structure will have the same (key length, key characters)
2261 // layout as the keys in the hash table. This also matches the
2262 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002263 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002264 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002265 return std::make_pair(KeyLen, DataLen);
2266 }
2267
2268 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2269 unsigned KeyLen) {
2270 // Record the location of the key data. This is used when generating
2271 // the mapping from persistent IDs to strings.
2272 Writer.SetIdentifierOffset(II, Out.tell());
2273 Out.write(II->getName(), KeyLen);
2274 }
2275
2276 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2277 pch::IdentID ID, unsigned) {
2278 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002279 bool hasMacroDefinition =
2280 II->hasMacroDefinition() &&
2281 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002282 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002283 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2284 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002285 Bits = (Bits << 1) | II->isExtensionToken();
2286 Bits = (Bits << 1) | II->isPoisoned();
2287 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2288 clang::io::Emit32(Out, Bits);
2289 clang::io::Emit32(Out, ID);
2290
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002291 if (hasMacroDefinition)
2292 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2293
Douglas Gregorc713da92009-04-21 22:25:48 +00002294 // Emit the declaration IDs in reverse order, because the
2295 // IdentifierResolver provides the declarations as they would be
2296 // visible (e.g., the function "stat" would come before the struct
2297 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2298 // adds declarations to the end of the list (so we need to see the
2299 // struct "status" before the function "status").
2300 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2301 IdentifierResolver::end());
2302 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2303 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002304 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002305 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002306 }
2307};
2308} // end anonymous namespace
2309
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002310/// \brief Write the identifier table into the PCH file.
2311///
2312/// The identifier table consists of a blob containing string data
2313/// (the actual identifiers themselves) and a separate "offsets" index
2314/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002315void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002316 using namespace llvm;
2317
2318 // Create and write out the blob that contains the identifier
2319 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002320 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002321 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002322 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2323
Douglas Gregor85c4a872009-04-25 21:04:17 +00002324 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2325
Douglas Gregorff9a6092009-04-20 20:36:09 +00002326 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002327 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2328 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2329 ID != IDEnd; ++ID) {
2330 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002331
2332 // Classify each identifier as either "interesting" or "not
2333 // interesting". Interesting identifiers are those that have
2334 // additional information that needs to be read from the PCH
2335 // file, e.g., a built-in ID, declaration chain, or macro
2336 // definition. These identifiers are placed into the hash table
2337 // so that they can be found when looked up in the user program.
2338 // All other identifiers are "uninteresting", which means that
2339 // the IdentifierInfo built by default has all of the
2340 // information we care about. Such identifiers are placed after
2341 // the hash table.
2342 const IdentifierInfo *II = ID->first;
2343 if (II->isPoisoned() ||
2344 II->isExtensionToken() ||
2345 II->hasMacroDefinition() ||
2346 II->getObjCOrBuiltinID() ||
2347 II->getFETokenInfo<void>())
2348 Generator.insert(ID->first, ID->second);
2349 else
2350 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002351 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002352
Douglas Gregorff9a6092009-04-20 20:36:09 +00002353 // Create the on-disk hash table in a buffer.
2354 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002355 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002356 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002357 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002358 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002359 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002360 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002361 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002362
2363 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2364 const IdentifierInfo *II = UninterestingIdentifiers[I];
2365 unsigned N = II->getLength() + 1;
2366 clang::io::Emit16(Out, N);
2367 SetIdentifierOffset(II, Out.tell());
2368 Out.write(II->getName(), N);
2369 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002370 }
2371
2372 // Create a blob abbreviation
2373 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2374 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002377 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002378
2379 // Write the identifier table
2380 RecordData Record;
2381 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002382 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002383 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2384 &IdentifierTable.front(),
2385 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002386 }
2387
2388 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002389 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2390 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2393 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2394
2395 RecordData Record;
2396 Record.push_back(pch::IDENTIFIER_OFFSET);
2397 Record.push_back(IdentifierOffsets.size());
2398 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2399 (const char *)&IdentifierOffsets.front(),
2400 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002401}
2402
Douglas Gregor1c507882009-04-15 21:30:51 +00002403/// \brief Write a record containing the given attributes.
2404void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2405 RecordData Record;
2406 for (; Attr; Attr = Attr->getNext()) {
2407 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2408 Record.push_back(Attr->isInherited());
2409 switch (Attr->getKind()) {
2410 case Attr::Alias:
2411 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2412 break;
2413
2414 case Attr::Aligned:
2415 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2416 break;
2417
2418 case Attr::AlwaysInline:
2419 break;
2420
2421 case Attr::AnalyzerNoReturn:
2422 break;
2423
2424 case Attr::Annotate:
2425 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2426 break;
2427
2428 case Attr::AsmLabel:
2429 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2430 break;
2431
2432 case Attr::Blocks:
2433 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2434 break;
2435
2436 case Attr::Cleanup:
2437 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2438 break;
2439
2440 case Attr::Const:
2441 break;
2442
2443 case Attr::Constructor:
2444 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2445 break;
2446
2447 case Attr::DLLExport:
2448 case Attr::DLLImport:
2449 case Attr::Deprecated:
2450 break;
2451
2452 case Attr::Destructor:
2453 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2454 break;
2455
2456 case Attr::FastCall:
2457 break;
2458
2459 case Attr::Format: {
2460 const FormatAttr *Format = cast<FormatAttr>(Attr);
2461 AddString(Format->getType(), Record);
2462 Record.push_back(Format->getFormatIdx());
2463 Record.push_back(Format->getFirstArg());
2464 break;
2465 }
2466
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002467 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002468 case Attr::IBOutletKind:
2469 case Attr::NoReturn:
2470 case Attr::NoThrow:
2471 case Attr::Nodebug:
2472 case Attr::Noinline:
2473 break;
2474
2475 case Attr::NonNull: {
2476 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2477 Record.push_back(NonNull->size());
2478 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2479 break;
2480 }
2481
2482 case Attr::ObjCException:
2483 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002484 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002485 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002486 case Attr::Overloadable:
2487 break;
2488
2489 case Attr::Packed:
2490 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2491 break;
2492
2493 case Attr::Pure:
2494 break;
2495
2496 case Attr::Regparm:
2497 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2498 break;
2499
2500 case Attr::Section:
2501 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2502 break;
2503
2504 case Attr::StdCall:
2505 case Attr::TransparentUnion:
2506 case Attr::Unavailable:
2507 case Attr::Unused:
2508 case Attr::Used:
2509 break;
2510
2511 case Attr::Visibility:
2512 // FIXME: stable encoding
2513 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2514 break;
2515
2516 case Attr::WarnUnusedResult:
2517 case Attr::Weak:
2518 case Attr::WeakImport:
2519 break;
2520 }
2521 }
2522
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002523 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002524}
2525
2526void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2527 Record.push_back(Str.size());
2528 Record.insert(Record.end(), Str.begin(), Str.end());
2529}
2530
Douglas Gregorff9a6092009-04-20 20:36:09 +00002531/// \brief Note that the identifier II occurs at the given offset
2532/// within the identifier table.
2533void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002534 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002535}
2536
Douglas Gregor2d711832009-04-25 17:48:32 +00002537/// \brief Note that the selector Sel occurs at the given offset
2538/// within the method pool/selector table.
2539void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2540 unsigned ID = SelectorIDs[Sel];
2541 assert(ID && "Unknown selector");
2542 SelectorOffsets[ID - 1] = Offset;
2543}
2544
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002545PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002546 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002547 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2548 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002549
Douglas Gregor87887da2009-04-20 15:53:59 +00002550void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002551 using namespace llvm;
2552
Douglas Gregor87887da2009-04-20 15:53:59 +00002553 ASTContext &Context = SemaRef.Context;
2554 Preprocessor &PP = SemaRef.PP;
2555
Douglas Gregorc34897d2009-04-09 22:27:44 +00002556 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002557 Stream.Emit((unsigned)'C', 8);
2558 Stream.Emit((unsigned)'P', 8);
2559 Stream.Emit((unsigned)'C', 8);
2560 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00002561
2562 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002563
2564 // The translation unit is the first declaration we'll emit.
2565 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2566 DeclsToEmit.push(Context.getTranslationUnitDecl());
2567
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002568 // Make sure that we emit IdentifierInfos (and any attached
2569 // declarations) for builtins.
2570 {
2571 IdentifierTable &Table = PP.getIdentifierTable();
2572 llvm::SmallVector<const char *, 32> BuiltinNames;
2573 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2574 Context.getLangOptions().NoBuiltin);
2575 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2576 getIdentifierRef(&Table.get(BuiltinNames[I]));
2577 }
2578
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002579 // Build a record containing all of the tentative definitions in
2580 // this header file. Generally, this record will be empty.
2581 RecordData TentativeDefinitions;
2582 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2583 TD = SemaRef.TentativeDefinitions.begin(),
2584 TDEnd = SemaRef.TentativeDefinitions.end();
2585 TD != TDEnd; ++TD)
2586 AddDeclRef(TD->second, TentativeDefinitions);
2587
Douglas Gregor062d9482009-04-22 22:18:58 +00002588 // Build a record containing all of the locally-scoped external
2589 // declarations in this header file. Generally, this record will be
2590 // empty.
2591 RecordData LocallyScopedExternalDecls;
2592 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2593 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2594 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2595 TD != TDEnd; ++TD)
2596 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2597
Douglas Gregorc34897d2009-04-09 22:27:44 +00002598 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002599 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002600 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002601 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002602 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002603 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002604 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002605
2606 // Keep writing types and declarations until all types and
2607 // declarations have been written.
2608 do {
2609 if (!DeclsToEmit.empty())
2610 WriteDeclsBlock(Context);
2611 if (!TypesToEmit.empty())
2612 WriteTypesBlock(Context);
2613 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2614
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002615 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002616 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002617
2618 // Write the type offsets array
2619 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2620 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2623 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2624 Record.clear();
2625 Record.push_back(pch::TYPE_OFFSET);
2626 Record.push_back(TypeOffsets.size());
2627 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2628 (const char *)&TypeOffsets.front(),
2629 TypeOffsets.size() * sizeof(uint64_t));
2630
2631 // Write the declaration offsets array
2632 Abbrev = new BitCodeAbbrev();
2633 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2636 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2637 Record.clear();
2638 Record.push_back(pch::DECL_OFFSET);
2639 Record.push_back(DeclOffsets.size());
2640 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2641 (const char *)&DeclOffsets.front(),
2642 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002643
2644 // Write the record of special types.
2645 Record.clear();
2646 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002647 AddTypeRef(Context.getObjCIdType(), Record);
2648 AddTypeRef(Context.getObjCSelType(), Record);
2649 AddTypeRef(Context.getObjCProtoType(), Record);
2650 AddTypeRef(Context.getObjCClassType(), Record);
2651 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2652 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002653 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2654
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002655 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002656 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002657 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002658
2659 // Write the record containing tentative definitions.
2660 if (!TentativeDefinitions.empty())
2661 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002662
2663 // Write the record containing locally-scoped external definitions.
2664 if (!LocallyScopedExternalDecls.empty())
2665 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2666 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002667
2668 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002669 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002670 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002671 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002672 Record.push_back(NumLexicalDeclContexts);
2673 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002674 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002675 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002676}
2677
2678void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2679 Record.push_back(Loc.getRawEncoding());
2680}
2681
2682void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2683 Record.push_back(Value.getBitWidth());
2684 unsigned N = Value.getNumWords();
2685 const uint64_t* Words = Value.getRawData();
2686 for (unsigned I = 0; I != N; ++I)
2687 Record.push_back(Words[I]);
2688}
2689
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002690void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2691 Record.push_back(Value.isUnsigned());
2692 AddAPInt(Value, Record);
2693}
2694
Douglas Gregore2f37202009-04-14 21:55:33 +00002695void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2696 AddAPInt(Value.bitcastToAPInt(), Record);
2697}
2698
Douglas Gregorc34897d2009-04-09 22:27:44 +00002699void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002700 Record.push_back(getIdentifierRef(II));
2701}
2702
2703pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2704 if (II == 0)
2705 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002706
2707 pch::IdentID &ID = IdentifierIDs[II];
2708 if (ID == 0)
2709 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002710 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002711}
2712
Steve Naroff9e84d782009-04-23 10:39:46 +00002713void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2714 if (SelRef.getAsOpaquePtr() == 0) {
2715 Record.push_back(0);
2716 return;
2717 }
2718
2719 pch::SelectorID &SID = SelectorIDs[SelRef];
2720 if (SID == 0) {
2721 SID = SelectorIDs.size();
2722 SelVector.push_back(SelRef);
2723 }
2724 Record.push_back(SID);
2725}
2726
Douglas Gregorc34897d2009-04-09 22:27:44 +00002727void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2728 if (T.isNull()) {
2729 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2730 return;
2731 }
2732
2733 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002734 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002735 switch (BT->getKind()) {
2736 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2737 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2738 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2739 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2740 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2741 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2742 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2743 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2744 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2745 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2746 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2747 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2748 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2749 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2750 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2751 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2752 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2753 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2754 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2755 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2756 }
2757
2758 Record.push_back((ID << 3) | T.getCVRQualifiers());
2759 return;
2760 }
2761
Douglas Gregorac8f2802009-04-10 17:25:41 +00002762 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002763 if (ID == 0) {
2764 // We haven't seen this type before. Assign it a new ID and put it
2765 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002766 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002767 TypesToEmit.push(T.getTypePtr());
2768 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002769
2770 // Encode the type qualifiers in the type reference.
2771 Record.push_back((ID << 3) | T.getCVRQualifiers());
2772}
2773
2774void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2775 if (D == 0) {
2776 Record.push_back(0);
2777 return;
2778 }
2779
Douglas Gregorac8f2802009-04-10 17:25:41 +00002780 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002781 if (ID == 0) {
2782 // We haven't seen this declaration before. Give it a new ID and
2783 // enqueue it in the list of declarations to emit.
2784 ID = DeclIDs.size();
2785 DeclsToEmit.push(const_cast<Decl *>(D));
2786 }
2787
2788 Record.push_back(ID);
2789}
2790
Douglas Gregorff9a6092009-04-20 20:36:09 +00002791pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2792 if (D == 0)
2793 return 0;
2794
2795 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2796 return DeclIDs[D];
2797}
2798
Douglas Gregorc34897d2009-04-09 22:27:44 +00002799void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2800 Record.push_back(Name.getNameKind());
2801 switch (Name.getNameKind()) {
2802 case DeclarationName::Identifier:
2803 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2804 break;
2805
2806 case DeclarationName::ObjCZeroArgSelector:
2807 case DeclarationName::ObjCOneArgSelector:
2808 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002809 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002810 break;
2811
2812 case DeclarationName::CXXConstructorName:
2813 case DeclarationName::CXXDestructorName:
2814 case DeclarationName::CXXConversionFunctionName:
2815 AddTypeRef(Name.getCXXNameType(), Record);
2816 break;
2817
2818 case DeclarationName::CXXOperatorName:
2819 Record.push_back(Name.getCXXOverloadedOperator());
2820 break;
2821
2822 case DeclarationName::CXXUsingDirective:
2823 // No extra data to emit
2824 break;
2825 }
2826}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002827
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002828/// \brief Write the given substatement or subexpression to the
2829/// bitstream.
2830void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002831 RecordData Record;
2832 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002833 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002834
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002835 if (!S) {
2836 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002837 return;
2838 }
2839
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002840 Writer.Code = pch::STMT_NULL_PTR;
2841 Writer.Visit(S);
2842 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002843 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002844 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002845}
2846
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002847/// \brief Flush all of the statements that have been added to the
2848/// queue via AddStmt().
2849void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002850 RecordData Record;
2851 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002852
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002853 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002854 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002855 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002856
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002857 if (!S) {
2858 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002859 continue;
2860 }
2861
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002862 Writer.Code = pch::STMT_NULL_PTR;
2863 Writer.Visit(S);
2864 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002865 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002866 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002867
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002868 assert(N == StmtsToEmit.size() &&
2869 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002870
2871 // Note that we are at the end of a full expression. Any
2872 // expression records that follow this one are part of a different
2873 // expression.
2874 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002875 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002876 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002877
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002878 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002879 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002880}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002881
2882unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2883 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2884 "SwitchCase recorded twice");
2885 unsigned NextID = SwitchCaseIDs.size();
2886 SwitchCaseIDs[S] = NextID;
2887 return NextID;
2888}
2889
2890unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2891 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2892 "SwitchCase hasn't been seen yet");
2893 return SwitchCaseIDs[S];
2894}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002895
2896/// \brief Retrieve the ID for the given label statement, which may
2897/// or may not have been emitted yet.
2898unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2899 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2900 if (Pos != LabelIDs.end())
2901 return Pos->second;
2902
2903 unsigned NextID = LabelIDs.size();
2904 LabelIDs[S] = NextID;
2905 return NextID;
2906}