blob: 41e3fb7fb591679a6472e643a9c69c53aee5b2fc [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
Douglas Gregorb5887f32009-04-10 21:16:55 +00001328/// \brief Write the target triple (e.g., i686-apple-darwin9).
1329void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1330 using namespace llvm;
1331 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1332 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001334 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001335
1336 RecordData Record;
1337 Record.push_back(pch::TARGET_TRIPLE);
1338 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001339 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001340}
1341
1342/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001343void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1344 RecordData Record;
1345 Record.push_back(LangOpts.Trigraphs);
1346 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1347 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1348 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1349 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1350 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1351 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1352 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1353 Record.push_back(LangOpts.C99); // C99 Support
1354 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1355 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1356 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1357 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1358 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1359
1360 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1361 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1362 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1363
1364 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1365 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1366 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1367 Record.push_back(LangOpts.LaxVectorConversions);
1368 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1369
1370 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1371 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1372 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1373
1374 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1375 // by locks.
1376 Record.push_back(LangOpts.Blocks); // block extension to C
1377 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1378 // they are unused.
1379 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1380 // (modulo the platform support).
1381
1382 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1383 // signed integer arithmetic overflows.
1384
1385 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1386 // may be ripped out at any time.
1387
1388 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1389 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1390 // defined.
1391 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1392 // opposed to __DYNAMIC__).
1393 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1394
1395 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1396 // used (instead of C99 semantics).
1397 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1398 Record.push_back(LangOpts.getGCMode());
1399 Record.push_back(LangOpts.getVisibilityMode());
1400 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001401 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001402}
1403
Douglas Gregorab1cef72009-04-10 03:52:48 +00001404//===----------------------------------------------------------------------===//
1405// Source Manager Serialization
1406//===----------------------------------------------------------------------===//
1407
1408/// \brief Create an abbreviation for the SLocEntry that refers to a
1409/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001410static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001411 using namespace llvm;
1412 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1413 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001419 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001420}
1421
1422/// \brief Create an abbreviation for the SLocEntry that refers to a
1423/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001424static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001425 using namespace llvm;
1426 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1427 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001433 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001434}
1435
1436/// \brief Create an abbreviation for the SLocEntry that refers to a
1437/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001438static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001439 using namespace llvm;
1440 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1441 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001443 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001444}
1445
1446/// \brief Create an abbreviation for the SLocEntry that refers to an
1447/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001448static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001449 using namespace llvm;
1450 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1451 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001457 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001458}
1459
1460/// \brief Writes the block containing the serialized form of the
1461/// source manager.
1462///
1463/// TODO: We should probably use an on-disk hash table (stored in a
1464/// blob), indexed based on the file name, so that we only create
1465/// entries for files that we actually need. In the common case (no
1466/// errors), we probably won't have to create file entries for any of
1467/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001468void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1469 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001470 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001471 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001472
1473 // Abbreviations for the various kinds of source-location entries.
1474 int SLocFileAbbrv = -1;
1475 int SLocBufferAbbrv = -1;
1476 int SLocBufferBlobAbbrv = -1;
1477 int SLocInstantiationAbbrv = -1;
1478
1479 // Write out the source location entry table. We skip the first
1480 // entry, which is always the same dummy entry.
1481 RecordData Record;
1482 for (SourceManager::sloc_entry_iterator
1483 SLoc = SourceMgr.sloc_entry_begin() + 1,
1484 SLocEnd = SourceMgr.sloc_entry_end();
1485 SLoc != SLocEnd; ++SLoc) {
1486 // Figure out which record code to use.
1487 unsigned Code;
1488 if (SLoc->isFile()) {
1489 if (SLoc->getFile().getContentCache()->Entry)
1490 Code = pch::SM_SLOC_FILE_ENTRY;
1491 else
1492 Code = pch::SM_SLOC_BUFFER_ENTRY;
1493 } else
1494 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1495 Record.push_back(Code);
1496
1497 Record.push_back(SLoc->getOffset());
1498 if (SLoc->isFile()) {
1499 const SrcMgr::FileInfo &File = SLoc->getFile();
1500 Record.push_back(File.getIncludeLoc().getRawEncoding());
1501 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001502 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001503
1504 const SrcMgr::ContentCache *Content = File.getContentCache();
1505 if (Content->Entry) {
1506 // The source location entry is a file. The blob associated
1507 // with this entry is the file name.
1508 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001509 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1510 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001511 Content->Entry->getName(),
1512 strlen(Content->Entry->getName()));
1513 } else {
1514 // The source location entry is a buffer. The blob associated
1515 // with this entry contains the contents of the buffer.
1516 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001517 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1518 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001519 }
1520
1521 // We add one to the size so that we capture the trailing NULL
1522 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1523 // the reader side).
1524 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1525 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001526 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001527 Record.clear();
1528 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001529 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001530 Buffer->getBufferStart(),
1531 Buffer->getBufferSize() + 1);
1532 }
1533 } else {
1534 // The source location entry is an instantiation.
1535 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1536 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1537 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1538 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1539
Douglas Gregor364e5802009-04-15 18:05:10 +00001540 // Compute the token length for this macro expansion.
1541 unsigned NextOffset = SourceMgr.getNextOffset();
1542 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1543 if (++NextSLoc != SLocEnd)
1544 NextOffset = NextSLoc->getOffset();
1545 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1546
Douglas Gregorab1cef72009-04-10 03:52:48 +00001547 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001548 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1549 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001550 }
1551
1552 Record.clear();
1553 }
1554
Douglas Gregor635f97f2009-04-13 16:31:14 +00001555 // Write the line table.
1556 if (SourceMgr.hasLineTable()) {
1557 LineTableInfo &LineTable = SourceMgr.getLineTable();
1558
1559 // Emit the file names
1560 Record.push_back(LineTable.getNumFilenames());
1561 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1562 // Emit the file name
1563 const char *Filename = LineTable.getFilename(I);
1564 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1565 Record.push_back(FilenameLen);
1566 if (FilenameLen)
1567 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1568 }
1569
1570 // Emit the line entries
1571 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1572 L != LEnd; ++L) {
1573 // Emit the file ID
1574 Record.push_back(L->first);
1575
1576 // Emit the line entries
1577 Record.push_back(L->second.size());
1578 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1579 LEEnd = L->second.end();
1580 LE != LEEnd; ++LE) {
1581 Record.push_back(LE->FileOffset);
1582 Record.push_back(LE->LineNo);
1583 Record.push_back(LE->FilenameID);
1584 Record.push_back((unsigned)LE->FileKind);
1585 Record.push_back(LE->IncludeOffset);
1586 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001587 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001588 }
1589 }
1590
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001591 // Loop over all the header files.
1592 HeaderSearch &HS = PP.getHeaderSearchInfo();
1593 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1594 E = HS.header_file_end();
1595 I != E; ++I) {
1596 Record.push_back(I->isImport);
1597 Record.push_back(I->DirInfo);
1598 Record.push_back(I->NumIncludes);
1599 if (I->ControllingMacro)
1600 AddIdentifierRef(I->ControllingMacro, Record);
1601 else
1602 Record.push_back(0);
1603 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1604 Record.clear();
1605 }
1606
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001607 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001608}
1609
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001610/// \brief Writes the block containing the serialized form of the
1611/// preprocessor.
1612///
Chris Lattner850eabd2009-04-10 18:08:30 +00001613void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001614 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001615
Chris Lattner4b21c202009-04-13 01:29:17 +00001616 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1617 if (PP.getCounterValue() != 0) {
1618 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001619 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001620 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001621 }
1622
1623 // Enter the preprocessor block.
1624 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001625
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001626 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1627 // FIXME: use diagnostics subsystem for localization etc.
1628 if (PP.SawDateOrTime())
1629 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1630
Chris Lattner1b094952009-04-10 18:00:12 +00001631 // Loop over all the macro definitions that are live at the end of the file,
1632 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001633 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1634 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001635 // FIXME: This emits macros in hash table order, we should do it in a stable
1636 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001637 MacroInfo *MI = I->second;
1638
1639 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1640 // been redefined by the header (in which case they are not isBuiltinMacro).
1641 if (MI->isBuiltinMacro())
1642 continue;
1643
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001644 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001645 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001646 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001647 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1648 Record.push_back(MI->isUsed());
1649
1650 unsigned Code;
1651 if (MI->isObjectLike()) {
1652 Code = pch::PP_MACRO_OBJECT_LIKE;
1653 } else {
1654 Code = pch::PP_MACRO_FUNCTION_LIKE;
1655
1656 Record.push_back(MI->isC99Varargs());
1657 Record.push_back(MI->isGNUVarargs());
1658 Record.push_back(MI->getNumArgs());
1659 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1660 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001661 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001662 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001663 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001664 Record.clear();
1665
Chris Lattner850eabd2009-04-10 18:08:30 +00001666 // Emit the tokens array.
1667 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1668 // Note that we know that the preprocessor does not have any annotation
1669 // tokens in it because they are created by the parser, and thus can't be
1670 // in a macro definition.
1671 const Token &Tok = MI->getReplacementToken(TokNo);
1672
1673 Record.push_back(Tok.getLocation().getRawEncoding());
1674 Record.push_back(Tok.getLength());
1675
Chris Lattner850eabd2009-04-10 18:08:30 +00001676 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1677 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001678 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001679
1680 // FIXME: Should translate token kind to a stable encoding.
1681 Record.push_back(Tok.getKind());
1682 // FIXME: Should translate token flags to a stable encoding.
1683 Record.push_back(Tok.getFlags());
1684
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001685 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001686 Record.clear();
1687 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001688 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001689 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001690 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001691}
1692
1693
Douglas Gregorc34897d2009-04-09 22:27:44 +00001694/// \brief Write the representation of a type to the PCH stream.
1695void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001696 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001697 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001698 ID = NextTypeID++;
1699
1700 // Record the offset for this type.
1701 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001702 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001703 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1704 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001705 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001706 }
1707
1708 RecordData Record;
1709
1710 // Emit the type's representation.
1711 PCHTypeWriter W(*this, Record);
1712 switch (T->getTypeClass()) {
1713 // For all of the concrete, non-dependent types, call the
1714 // appropriate visitor function.
1715#define TYPE(Class, Base) \
1716 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1717#define ABSTRACT_TYPE(Class, Base)
1718#define DEPENDENT_TYPE(Class, Base)
1719#include "clang/AST/TypeNodes.def"
1720
1721 // For all of the dependent type nodes (which only occur in C++
1722 // templates), produce an error.
1723#define TYPE(Class, Base)
1724#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1725#include "clang/AST/TypeNodes.def"
1726 assert(false && "Cannot serialize dependent type nodes");
1727 break;
1728 }
1729
1730 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001731 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001732
1733 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001734 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001735}
1736
1737/// \brief Write a block containing all of the types.
1738void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001739 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001740 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001741
Douglas Gregore43f0972009-04-26 03:49:13 +00001742 // Emit all of the types that need to be emitted (so far).
1743 while (!TypesToEmit.empty()) {
1744 const Type *T = TypesToEmit.front();
1745 TypesToEmit.pop();
1746 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1747 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001748 }
1749
1750 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001751 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001752}
1753
1754/// \brief Write the block containing all of the declaration IDs
1755/// lexically declared within the given DeclContext.
1756///
1757/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1758/// bistream, or 0 if no block was written.
1759uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1760 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001761 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001762 return 0;
1763
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001764 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001765 RecordData Record;
1766 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1767 DEnd = DC->decls_end(Context);
1768 D != DEnd; ++D)
1769 AddDeclRef(*D, Record);
1770
Douglas Gregoraf136d92009-04-22 22:34:57 +00001771 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001772 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001773 return Offset;
1774}
1775
1776/// \brief Write the block containing all of the declaration IDs
1777/// visible from the given DeclContext.
1778///
1779/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1780/// bistream, or 0 if no block was written.
1781uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1782 DeclContext *DC) {
1783 if (DC->getPrimaryContext() != DC)
1784 return 0;
1785
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001786 // Since there is no name lookup into functions or methods, and we
1787 // perform name lookup for the translation unit via the
1788 // IdentifierInfo chains, don't bother to build a
1789 // visible-declarations table for these entities.
1790 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001791 return 0;
1792
Douglas Gregorc34897d2009-04-09 22:27:44 +00001793 // Force the DeclContext to build a its name-lookup table.
1794 DC->lookup(Context, DeclarationName());
1795
1796 // Serialize the contents of the mapping used for lookup. Note that,
1797 // although we have two very different code paths, the serialized
1798 // representation is the same for both cases: a declaration name,
1799 // followed by a size, followed by references to the visible
1800 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001801 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001802 RecordData Record;
1803 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001804 if (!Map)
1805 return 0;
1806
Douglas Gregorc34897d2009-04-09 22:27:44 +00001807 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1808 D != DEnd; ++D) {
1809 AddDeclarationName(D->first, Record);
1810 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1811 Record.push_back(Result.second - Result.first);
1812 for(; Result.first != Result.second; ++Result.first)
1813 AddDeclRef(*Result.first, Record);
1814 }
1815
1816 if (Record.size() == 0)
1817 return 0;
1818
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001819 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001820 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001821 return Offset;
1822}
1823
1824/// \brief Write a block containing all of the declarations.
1825void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001826 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001827 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001828
1829 // Emit all of the declarations.
1830 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001831 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001832 while (!DeclsToEmit.empty()) {
1833 // Pull the next declaration off the queue
1834 Decl *D = DeclsToEmit.front();
1835 DeclsToEmit.pop();
1836
1837 // If this declaration is also a DeclContext, write blocks for the
1838 // declarations that lexically stored inside its context and those
1839 // declarations that are visible from its context. These blocks
1840 // are written before the declaration itself so that we can put
1841 // their offsets into the record for the declaration.
1842 uint64_t LexicalOffset = 0;
1843 uint64_t VisibleOffset = 0;
1844 DeclContext *DC = dyn_cast<DeclContext>(D);
1845 if (DC) {
1846 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1847 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1848 }
1849
1850 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001851 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001852 if (ID == 0)
1853 ID = DeclIDs.size();
1854
1855 unsigned Index = ID - 1;
1856
1857 // Record the offset for this declaration
1858 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001859 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001860 else if (DeclOffsets.size() < Index) {
1861 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001862 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001863 }
1864
1865 // Build and emit a record for this declaration
1866 Record.clear();
1867 W.Code = (pch::DeclCode)0;
1868 W.Visit(D);
1869 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001870
1871 if (!W.Code) {
1872 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1873 D->getDeclKindName());
1874 assert(false && "Unhandled declaration kind while generating PCH");
1875 exit(-1);
1876 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001877 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001878
Douglas Gregor1c507882009-04-15 21:30:51 +00001879 // If the declaration had any attributes, write them now.
1880 if (D->hasAttrs())
1881 WriteAttributeRecord(D->getAttrs());
1882
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001883 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001884 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001885
Douglas Gregor631f6c62009-04-14 00:24:19 +00001886 // Note external declarations so that we can add them to a record
1887 // in the PCH file later.
1888 if (isa<FileScopeAsmDecl>(D))
1889 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001890 }
1891
1892 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001893 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001894}
1895
Douglas Gregorff9a6092009-04-20 20:36:09 +00001896namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001897// Trait used for the on-disk hash table used in the method pool.
1898class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1899 PCHWriter &Writer;
1900
1901public:
1902 typedef Selector key_type;
1903 typedef key_type key_type_ref;
1904
1905 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1906 typedef const data_type& data_type_ref;
1907
1908 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1909
1910 static unsigned ComputeHash(Selector Sel) {
1911 unsigned N = Sel.getNumArgs();
1912 if (N == 0)
1913 ++N;
1914 unsigned R = 5381;
1915 for (unsigned I = 0; I != N; ++I)
1916 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1917 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1918 return R;
1919 }
1920
1921 std::pair<unsigned,unsigned>
1922 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1923 data_type_ref Methods) {
1924 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1925 clang::io::Emit16(Out, KeyLen);
1926 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1927 for (const ObjCMethodList *Method = &Methods.first; Method;
1928 Method = Method->Next)
1929 if (Method->Method)
1930 DataLen += 4;
1931 for (const ObjCMethodList *Method = &Methods.second; Method;
1932 Method = Method->Next)
1933 if (Method->Method)
1934 DataLen += 4;
1935 clang::io::Emit16(Out, DataLen);
1936 return std::make_pair(KeyLen, DataLen);
1937 }
1938
Douglas Gregor2d711832009-04-25 17:48:32 +00001939 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1940 uint64_t Start = Out.tell();
1941 assert((Start >> 32) == 0 && "Selector key offset too large");
1942 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001943 unsigned N = Sel.getNumArgs();
1944 clang::io::Emit16(Out, N);
1945 if (N == 0)
1946 N = 1;
1947 for (unsigned I = 0; I != N; ++I)
1948 clang::io::Emit32(Out,
1949 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1950 }
1951
1952 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001953 data_type_ref Methods, unsigned DataLen) {
1954 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001955 unsigned NumInstanceMethods = 0;
1956 for (const ObjCMethodList *Method = &Methods.first; Method;
1957 Method = Method->Next)
1958 if (Method->Method)
1959 ++NumInstanceMethods;
1960
1961 unsigned NumFactoryMethods = 0;
1962 for (const ObjCMethodList *Method = &Methods.second; Method;
1963 Method = Method->Next)
1964 if (Method->Method)
1965 ++NumFactoryMethods;
1966
1967 clang::io::Emit16(Out, NumInstanceMethods);
1968 clang::io::Emit16(Out, NumFactoryMethods);
1969 for (const ObjCMethodList *Method = &Methods.first; Method;
1970 Method = Method->Next)
1971 if (Method->Method)
1972 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001973 for (const ObjCMethodList *Method = &Methods.second; Method;
1974 Method = Method->Next)
1975 if (Method->Method)
1976 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001977
1978 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001979 }
1980};
1981} // end anonymous namespace
1982
1983/// \brief Write the method pool into the PCH file.
1984///
1985/// The method pool contains both instance and factory methods, stored
1986/// in an on-disk hash table indexed by the selector.
1987void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1988 using namespace llvm;
1989
1990 // Create and write out the blob that contains the instance and
1991 // factor method pools.
1992 bool Empty = true;
1993 {
1994 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1995
1996 // Create the on-disk hash table representation. Start by
1997 // iterating through the instance method pool.
1998 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001999 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002000 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2001 Instance = SemaRef.InstanceMethodPool.begin(),
2002 InstanceEnd = SemaRef.InstanceMethodPool.end();
2003 Instance != InstanceEnd; ++Instance) {
2004 // Check whether there is a factory method with the same
2005 // selector.
2006 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
2007 = SemaRef.FactoryMethodPool.find(Instance->first);
2008
2009 if (Factory == SemaRef.FactoryMethodPool.end())
2010 Generator.insert(Instance->first,
2011 std::make_pair(Instance->second,
2012 ObjCMethodList()));
2013 else
2014 Generator.insert(Instance->first,
2015 std::make_pair(Instance->second, Factory->second));
2016
Douglas Gregor2d711832009-04-25 17:48:32 +00002017 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002018 Empty = false;
2019 }
2020
2021 // Now iterate through the factory method pool, to pick up any
2022 // selectors that weren't already in the instance method pool.
2023 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2024 Factory = SemaRef.FactoryMethodPool.begin(),
2025 FactoryEnd = SemaRef.FactoryMethodPool.end();
2026 Factory != FactoryEnd; ++Factory) {
2027 // Check whether there is an instance method with the same
2028 // selector. If so, there is no work to do here.
2029 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
2030 = SemaRef.InstanceMethodPool.find(Factory->first);
2031
Douglas Gregor2d711832009-04-25 17:48:32 +00002032 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002033 Generator.insert(Factory->first,
2034 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00002035 ++NumSelectorsInMethodPool;
2036 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002037
2038 Empty = false;
2039 }
2040
Douglas Gregor2d711832009-04-25 17:48:32 +00002041 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002042 return;
2043
2044 // Create the on-disk hash table in a buffer.
2045 llvm::SmallVector<char, 4096> MethodPool;
2046 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00002047 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002048 {
2049 PCHMethodPoolTrait Trait(*this);
2050 llvm::raw_svector_ostream Out(MethodPool);
2051 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002052 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002053 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00002054
2055 // For every selector that we have seen but which was not
2056 // written into the hash table, write the selector itself and
2057 // record it's offset.
2058 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
2059 if (SelectorOffsets[I] == 0)
2060 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002061 }
2062
2063 // Create a blob abbreviation
2064 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2065 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2069 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2070
Douglas Gregor2d711832009-04-25 17:48:32 +00002071 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002072 RecordData Record;
2073 Record.push_back(pch::METHOD_POOL);
2074 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002075 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002076 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2077 &MethodPool.front(),
2078 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002079
2080 // Create a blob abbreviation for the selector table offsets.
2081 Abbrev = new BitCodeAbbrev();
2082 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2085 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2086
2087 // Write the selector offsets table.
2088 Record.clear();
2089 Record.push_back(pch::SELECTOR_OFFSETS);
2090 Record.push_back(SelectorOffsets.size());
2091 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2092 (const char *)&SelectorOffsets.front(),
2093 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002094 }
2095}
2096
2097namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002098class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2099 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002100 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002101
2102public:
2103 typedef const IdentifierInfo* key_type;
2104 typedef key_type key_type_ref;
2105
2106 typedef pch::IdentID data_type;
2107 typedef data_type data_type_ref;
2108
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002109 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2110 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002111
2112 static unsigned ComputeHash(const IdentifierInfo* II) {
2113 return clang::BernsteinHash(II->getName());
2114 }
2115
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002116 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002117 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2118 pch::IdentID ID) {
2119 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002120 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2121 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002122 if (II->hasMacroDefinition() &&
2123 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2124 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002125 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2126 DEnd = IdentifierResolver::end();
2127 D != DEnd; ++D)
2128 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002129 // We emit the key length after the data length so that the
2130 // "uninteresting" identifiers following the identifier hash table
2131 // structure will have the same (key length, key characters)
2132 // layout as the keys in the hash table. This also matches the
2133 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002134 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002135 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002136 return std::make_pair(KeyLen, DataLen);
2137 }
2138
2139 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2140 unsigned KeyLen) {
2141 // Record the location of the key data. This is used when generating
2142 // the mapping from persistent IDs to strings.
2143 Writer.SetIdentifierOffset(II, Out.tell());
2144 Out.write(II->getName(), KeyLen);
2145 }
2146
2147 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2148 pch::IdentID ID, unsigned) {
2149 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002150 bool hasMacroDefinition =
2151 II->hasMacroDefinition() &&
2152 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002153 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002154 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2155 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002156 Bits = (Bits << 1) | II->isExtensionToken();
2157 Bits = (Bits << 1) | II->isPoisoned();
2158 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2159 clang::io::Emit32(Out, Bits);
2160 clang::io::Emit32(Out, ID);
2161
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002162 if (hasMacroDefinition)
2163 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2164
Douglas Gregorc713da92009-04-21 22:25:48 +00002165 // Emit the declaration IDs in reverse order, because the
2166 // IdentifierResolver provides the declarations as they would be
2167 // visible (e.g., the function "stat" would come before the struct
2168 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2169 // adds declarations to the end of the list (so we need to see the
2170 // struct "status" before the function "status").
2171 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2172 IdentifierResolver::end());
2173 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2174 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002175 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002176 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002177 }
2178};
2179} // end anonymous namespace
2180
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002181/// \brief Write the identifier table into the PCH file.
2182///
2183/// The identifier table consists of a blob containing string data
2184/// (the actual identifiers themselves) and a separate "offsets" index
2185/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002186void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002187 using namespace llvm;
2188
2189 // Create and write out the blob that contains the identifier
2190 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002191 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002192 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002193 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2194
Douglas Gregor85c4a872009-04-25 21:04:17 +00002195 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2196
Douglas Gregorff9a6092009-04-20 20:36:09 +00002197 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002198 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2199 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2200 ID != IDEnd; ++ID) {
2201 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002202
2203 // Classify each identifier as either "interesting" or "not
2204 // interesting". Interesting identifiers are those that have
2205 // additional information that needs to be read from the PCH
2206 // file, e.g., a built-in ID, declaration chain, or macro
2207 // definition. These identifiers are placed into the hash table
2208 // so that they can be found when looked up in the user program.
2209 // All other identifiers are "uninteresting", which means that
2210 // the IdentifierInfo built by default has all of the
2211 // information we care about. Such identifiers are placed after
2212 // the hash table.
2213 const IdentifierInfo *II = ID->first;
2214 if (II->isPoisoned() ||
2215 II->isExtensionToken() ||
2216 II->hasMacroDefinition() ||
2217 II->getObjCOrBuiltinID() ||
2218 II->getFETokenInfo<void>())
2219 Generator.insert(ID->first, ID->second);
2220 else
2221 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002222 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002223
Douglas Gregorff9a6092009-04-20 20:36:09 +00002224 // Create the on-disk hash table in a buffer.
2225 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002226 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002227 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002228 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002229 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002230 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002231 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002232 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002233
2234 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2235 const IdentifierInfo *II = UninterestingIdentifiers[I];
2236 unsigned N = II->getLength() + 1;
2237 clang::io::Emit16(Out, N);
2238 SetIdentifierOffset(II, Out.tell());
2239 Out.write(II->getName(), N);
2240 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002241 }
2242
2243 // Create a blob abbreviation
2244 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2245 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002248 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002249
2250 // Write the identifier table
2251 RecordData Record;
2252 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002253 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002254 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2255 &IdentifierTable.front(),
2256 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002257 }
2258
2259 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002260 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2261 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2264 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2265
2266 RecordData Record;
2267 Record.push_back(pch::IDENTIFIER_OFFSET);
2268 Record.push_back(IdentifierOffsets.size());
2269 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2270 (const char *)&IdentifierOffsets.front(),
2271 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002272}
2273
Douglas Gregor1c507882009-04-15 21:30:51 +00002274/// \brief Write a record containing the given attributes.
2275void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2276 RecordData Record;
2277 for (; Attr; Attr = Attr->getNext()) {
2278 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2279 Record.push_back(Attr->isInherited());
2280 switch (Attr->getKind()) {
2281 case Attr::Alias:
2282 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2283 break;
2284
2285 case Attr::Aligned:
2286 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2287 break;
2288
2289 case Attr::AlwaysInline:
2290 break;
2291
2292 case Attr::AnalyzerNoReturn:
2293 break;
2294
2295 case Attr::Annotate:
2296 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2297 break;
2298
2299 case Attr::AsmLabel:
2300 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2301 break;
2302
2303 case Attr::Blocks:
2304 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2305 break;
2306
2307 case Attr::Cleanup:
2308 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2309 break;
2310
2311 case Attr::Const:
2312 break;
2313
2314 case Attr::Constructor:
2315 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2316 break;
2317
2318 case Attr::DLLExport:
2319 case Attr::DLLImport:
2320 case Attr::Deprecated:
2321 break;
2322
2323 case Attr::Destructor:
2324 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2325 break;
2326
2327 case Attr::FastCall:
2328 break;
2329
2330 case Attr::Format: {
2331 const FormatAttr *Format = cast<FormatAttr>(Attr);
2332 AddString(Format->getType(), Record);
2333 Record.push_back(Format->getFormatIdx());
2334 Record.push_back(Format->getFirstArg());
2335 break;
2336 }
2337
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002338 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002339 case Attr::IBOutletKind:
2340 case Attr::NoReturn:
2341 case Attr::NoThrow:
2342 case Attr::Nodebug:
2343 case Attr::Noinline:
2344 break;
2345
2346 case Attr::NonNull: {
2347 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2348 Record.push_back(NonNull->size());
2349 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2350 break;
2351 }
2352
2353 case Attr::ObjCException:
2354 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002355 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002356 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002357 case Attr::Overloadable:
2358 break;
2359
2360 case Attr::Packed:
2361 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2362 break;
2363
2364 case Attr::Pure:
2365 break;
2366
2367 case Attr::Regparm:
2368 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2369 break;
2370
2371 case Attr::Section:
2372 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2373 break;
2374
2375 case Attr::StdCall:
2376 case Attr::TransparentUnion:
2377 case Attr::Unavailable:
2378 case Attr::Unused:
2379 case Attr::Used:
2380 break;
2381
2382 case Attr::Visibility:
2383 // FIXME: stable encoding
2384 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2385 break;
2386
2387 case Attr::WarnUnusedResult:
2388 case Attr::Weak:
2389 case Attr::WeakImport:
2390 break;
2391 }
2392 }
2393
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002394 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002395}
2396
2397void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2398 Record.push_back(Str.size());
2399 Record.insert(Record.end(), Str.begin(), Str.end());
2400}
2401
Douglas Gregorff9a6092009-04-20 20:36:09 +00002402/// \brief Note that the identifier II occurs at the given offset
2403/// within the identifier table.
2404void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002405 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002406}
2407
Douglas Gregor2d711832009-04-25 17:48:32 +00002408/// \brief Note that the selector Sel occurs at the given offset
2409/// within the method pool/selector table.
2410void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2411 unsigned ID = SelectorIDs[Sel];
2412 assert(ID && "Unknown selector");
2413 SelectorOffsets[ID - 1] = Offset;
2414}
2415
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002416PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002417 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002418 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2419 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002420
Douglas Gregor87887da2009-04-20 15:53:59 +00002421void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002422 using namespace llvm;
2423
Douglas Gregor87887da2009-04-20 15:53:59 +00002424 ASTContext &Context = SemaRef.Context;
2425 Preprocessor &PP = SemaRef.PP;
2426
Douglas Gregorc34897d2009-04-09 22:27:44 +00002427 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002428 Stream.Emit((unsigned)'C', 8);
2429 Stream.Emit((unsigned)'P', 8);
2430 Stream.Emit((unsigned)'C', 8);
2431 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002432
2433 // The translation unit is the first declaration we'll emit.
2434 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2435 DeclsToEmit.push(Context.getTranslationUnitDecl());
2436
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002437 // Make sure that we emit IdentifierInfos (and any attached
2438 // declarations) for builtins.
2439 {
2440 IdentifierTable &Table = PP.getIdentifierTable();
2441 llvm::SmallVector<const char *, 32> BuiltinNames;
2442 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2443 Context.getLangOptions().NoBuiltin);
2444 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2445 getIdentifierRef(&Table.get(BuiltinNames[I]));
2446 }
2447
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002448 // Build a record containing all of the tentative definitions in
2449 // this header file. Generally, this record will be empty.
2450 RecordData TentativeDefinitions;
2451 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2452 TD = SemaRef.TentativeDefinitions.begin(),
2453 TDEnd = SemaRef.TentativeDefinitions.end();
2454 TD != TDEnd; ++TD)
2455 AddDeclRef(TD->second, TentativeDefinitions);
2456
Douglas Gregor062d9482009-04-22 22:18:58 +00002457 // Build a record containing all of the locally-scoped external
2458 // declarations in this header file. Generally, this record will be
2459 // empty.
2460 RecordData LocallyScopedExternalDecls;
2461 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2462 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2463 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2464 TD != TDEnd; ++TD)
2465 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2466
Douglas Gregorc34897d2009-04-09 22:27:44 +00002467 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002468 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002469 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002470 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002471 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002472 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002473 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002474
2475 // Keep writing types and declarations until all types and
2476 // declarations have been written.
2477 do {
2478 if (!DeclsToEmit.empty())
2479 WriteDeclsBlock(Context);
2480 if (!TypesToEmit.empty())
2481 WriteTypesBlock(Context);
2482 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2483
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002484 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002485 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002486
2487 // Write the type offsets array
2488 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2489 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2492 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2493 Record.clear();
2494 Record.push_back(pch::TYPE_OFFSET);
2495 Record.push_back(TypeOffsets.size());
2496 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2497 (const char *)&TypeOffsets.front(),
2498 TypeOffsets.size() * sizeof(uint64_t));
2499
2500 // Write the declaration offsets array
2501 Abbrev = new BitCodeAbbrev();
2502 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2505 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2506 Record.clear();
2507 Record.push_back(pch::DECL_OFFSET);
2508 Record.push_back(DeclOffsets.size());
2509 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2510 (const char *)&DeclOffsets.front(),
2511 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002512
2513 // Write the record of special types.
2514 Record.clear();
2515 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002516 AddTypeRef(Context.getObjCIdType(), Record);
2517 AddTypeRef(Context.getObjCSelType(), Record);
2518 AddTypeRef(Context.getObjCProtoType(), Record);
2519 AddTypeRef(Context.getObjCClassType(), Record);
2520 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2521 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002522 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2523
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002524 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002525 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002526 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002527
2528 // Write the record containing tentative definitions.
2529 if (!TentativeDefinitions.empty())
2530 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002531
2532 // Write the record containing locally-scoped external definitions.
2533 if (!LocallyScopedExternalDecls.empty())
2534 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2535 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002536
2537 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002538 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002539 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002540 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002541 Record.push_back(NumLexicalDeclContexts);
2542 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002543 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002544 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002545}
2546
2547void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2548 Record.push_back(Loc.getRawEncoding());
2549}
2550
2551void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2552 Record.push_back(Value.getBitWidth());
2553 unsigned N = Value.getNumWords();
2554 const uint64_t* Words = Value.getRawData();
2555 for (unsigned I = 0; I != N; ++I)
2556 Record.push_back(Words[I]);
2557}
2558
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002559void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2560 Record.push_back(Value.isUnsigned());
2561 AddAPInt(Value, Record);
2562}
2563
Douglas Gregore2f37202009-04-14 21:55:33 +00002564void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2565 AddAPInt(Value.bitcastToAPInt(), Record);
2566}
2567
Douglas Gregorc34897d2009-04-09 22:27:44 +00002568void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002569 Record.push_back(getIdentifierRef(II));
2570}
2571
2572pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2573 if (II == 0)
2574 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002575
2576 pch::IdentID &ID = IdentifierIDs[II];
2577 if (ID == 0)
2578 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002579 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002580}
2581
Steve Naroff9e84d782009-04-23 10:39:46 +00002582void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2583 if (SelRef.getAsOpaquePtr() == 0) {
2584 Record.push_back(0);
2585 return;
2586 }
2587
2588 pch::SelectorID &SID = SelectorIDs[SelRef];
2589 if (SID == 0) {
2590 SID = SelectorIDs.size();
2591 SelVector.push_back(SelRef);
2592 }
2593 Record.push_back(SID);
2594}
2595
Douglas Gregorc34897d2009-04-09 22:27:44 +00002596void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2597 if (T.isNull()) {
2598 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2599 return;
2600 }
2601
2602 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002603 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002604 switch (BT->getKind()) {
2605 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2606 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2607 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2608 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2609 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2610 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2611 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2612 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2613 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2614 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2615 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2616 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2617 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2618 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2619 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2620 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2621 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2622 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2623 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2624 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2625 }
2626
2627 Record.push_back((ID << 3) | T.getCVRQualifiers());
2628 return;
2629 }
2630
Douglas Gregorac8f2802009-04-10 17:25:41 +00002631 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002632 if (ID == 0) {
2633 // We haven't seen this type before. Assign it a new ID and put it
2634 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002635 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002636 TypesToEmit.push(T.getTypePtr());
2637 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002638
2639 // Encode the type qualifiers in the type reference.
2640 Record.push_back((ID << 3) | T.getCVRQualifiers());
2641}
2642
2643void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2644 if (D == 0) {
2645 Record.push_back(0);
2646 return;
2647 }
2648
Douglas Gregorac8f2802009-04-10 17:25:41 +00002649 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002650 if (ID == 0) {
2651 // We haven't seen this declaration before. Give it a new ID and
2652 // enqueue it in the list of declarations to emit.
2653 ID = DeclIDs.size();
2654 DeclsToEmit.push(const_cast<Decl *>(D));
2655 }
2656
2657 Record.push_back(ID);
2658}
2659
Douglas Gregorff9a6092009-04-20 20:36:09 +00002660pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2661 if (D == 0)
2662 return 0;
2663
2664 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2665 return DeclIDs[D];
2666}
2667
Douglas Gregorc34897d2009-04-09 22:27:44 +00002668void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2669 Record.push_back(Name.getNameKind());
2670 switch (Name.getNameKind()) {
2671 case DeclarationName::Identifier:
2672 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2673 break;
2674
2675 case DeclarationName::ObjCZeroArgSelector:
2676 case DeclarationName::ObjCOneArgSelector:
2677 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002678 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002679 break;
2680
2681 case DeclarationName::CXXConstructorName:
2682 case DeclarationName::CXXDestructorName:
2683 case DeclarationName::CXXConversionFunctionName:
2684 AddTypeRef(Name.getCXXNameType(), Record);
2685 break;
2686
2687 case DeclarationName::CXXOperatorName:
2688 Record.push_back(Name.getCXXOverloadedOperator());
2689 break;
2690
2691 case DeclarationName::CXXUsingDirective:
2692 // No extra data to emit
2693 break;
2694 }
2695}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002696
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002697/// \brief Write the given substatement or subexpression to the
2698/// bitstream.
2699void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002700 RecordData Record;
2701 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002702 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002703
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002704 if (!S) {
2705 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002706 return;
2707 }
2708
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002709 Writer.Code = pch::STMT_NULL_PTR;
2710 Writer.Visit(S);
2711 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002712 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002713 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002714}
2715
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002716/// \brief Flush all of the statements that have been added to the
2717/// queue via AddStmt().
2718void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002719 RecordData Record;
2720 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002721
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002722 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002723 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002724 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002725
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002726 if (!S) {
2727 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002728 continue;
2729 }
2730
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002731 Writer.Code = pch::STMT_NULL_PTR;
2732 Writer.Visit(S);
2733 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002734 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002735 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002736
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002737 assert(N == StmtsToEmit.size() &&
2738 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002739
2740 // Note that we are at the end of a full expression. Any
2741 // expression records that follow this one are part of a different
2742 // expression.
2743 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002744 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002745 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002746
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002747 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002748 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002749}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002750
2751unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2752 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2753 "SwitchCase recorded twice");
2754 unsigned NextID = SwitchCaseIDs.size();
2755 SwitchCaseIDs[S] = NextID;
2756 return NextID;
2757}
2758
2759unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2760 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2761 "SwitchCase hasn't been seen yet");
2762 return SwitchCaseIDs[S];
2763}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002764
2765/// \brief Retrieve the ID for the given label statement, which may
2766/// or may not have been emitted yet.
2767unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2768 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2769 if (Pos != LabelIDs.end())
2770 return Pos->second;
2771
2772 unsigned NextID = LabelIDs.size();
2773 LabelIDs[S] = NextID;
2774 return NextID;
2775}