blob: bce941a8497b3e9bbd4cdec0cca767f7f38f1798 [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 Gregor23ce3a52009-04-13 22:18:37 +0000263 void VisitParmVarDecl(ParmVarDecl *D);
264 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000265 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
266 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000267 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
268 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000269 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000270 void VisitObjCContainerDecl(ObjCContainerDecl *D);
271 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
272 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000273 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
274 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
275 void VisitObjCClassDecl(ObjCClassDecl *D);
276 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
277 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
278 void VisitObjCImplDecl(ObjCImplDecl *D);
279 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
280 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
281 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
282 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
283 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000284 };
285}
286
287void PCHDeclWriter::VisitDecl(Decl *D) {
288 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
289 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
290 Writer.AddSourceLocation(D->getLocation(), Record);
291 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000292 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000293 Record.push_back(D->isImplicit());
294 Record.push_back(D->getAccess());
295}
296
297void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
298 VisitDecl(D);
299 Code = pch::DECL_TRANSLATION_UNIT;
300}
301
302void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
303 VisitDecl(D);
304 Writer.AddDeclarationName(D->getDeclName(), Record);
305}
306
307void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
308 VisitNamedDecl(D);
309 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
310}
311
312void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
313 VisitTypeDecl(D);
314 Writer.AddTypeRef(D->getUnderlyingType(), Record);
315 Code = pch::DECL_TYPEDEF;
316}
317
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000318void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
319 VisitTypeDecl(D);
320 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
321 Record.push_back(D->isDefinition());
322 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
323}
324
325void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
326 VisitTagDecl(D);
327 Writer.AddTypeRef(D->getIntegerType(), Record);
328 Code = pch::DECL_ENUM;
329}
330
Douglas Gregor982365e2009-04-13 21:20:57 +0000331void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
332 VisitTagDecl(D);
333 Record.push_back(D->hasFlexibleArrayMember());
334 Record.push_back(D->isAnonymousStructOrUnion());
335 Code = pch::DECL_RECORD;
336}
337
Douglas Gregorc34897d2009-04-09 22:27:44 +0000338void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
339 VisitNamedDecl(D);
340 Writer.AddTypeRef(D->getType(), Record);
341}
342
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000343void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
344 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000345 Record.push_back(D->getInitExpr()? 1 : 0);
346 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000347 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000348 Writer.AddAPSInt(D->getInitVal(), Record);
349 Code = pch::DECL_ENUM_CONSTANT;
350}
351
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000352void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
353 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000354 Record.push_back(D->isThisDeclarationADefinition());
355 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000356 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000357 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
358 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
359 Record.push_back(D->isInline());
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000360 Record.push_back(D->isC99InlineDefinition());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000361 Record.push_back(D->isVirtual());
362 Record.push_back(D->isPure());
363 Record.push_back(D->inheritedPrototype());
364 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
365 Record.push_back(D->isDeleted());
366 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
367 Record.push_back(D->param_size());
368 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
369 P != PEnd; ++P)
370 Writer.AddDeclRef(*P, Record);
371 Code = pch::DECL_FUNCTION;
372}
373
Steve Naroff79ea0e02009-04-20 15:06:07 +0000374void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
375 VisitNamedDecl(D);
376 // FIXME: convert to LazyStmtPtr?
377 // Unlike C/C++, method bodies will never be in header files.
378 Record.push_back(D->getBody() != 0);
379 if (D->getBody() != 0) {
380 Writer.AddStmt(D->getBody(Context));
381 Writer.AddDeclRef(D->getSelfDecl(), Record);
382 Writer.AddDeclRef(D->getCmdDecl(), Record);
383 }
384 Record.push_back(D->isInstanceMethod());
385 Record.push_back(D->isVariadic());
386 Record.push_back(D->isSynthesized());
387 // FIXME: stable encoding for @required/@optional
388 Record.push_back(D->getImplementationControl());
389 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
390 Record.push_back(D->getObjCDeclQualifier());
391 Writer.AddTypeRef(D->getResultType(), Record);
392 Writer.AddSourceLocation(D->getLocEnd(), Record);
393 Record.push_back(D->param_size());
394 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
395 PEnd = D->param_end(); P != PEnd; ++P)
396 Writer.AddDeclRef(*P, Record);
397 Code = pch::DECL_OBJC_METHOD;
398}
399
Steve Naroff7333b492009-04-20 20:09:33 +0000400void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
401 VisitNamedDecl(D);
402 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
403 // Abstract class (no need to define a stable pch::DECL code).
404}
405
406void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
407 VisitObjCContainerDecl(D);
408 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
409 Writer.AddDeclRef(D->getSuperClass(), Record);
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000410 Record.push_back(D->protocol_size());
411 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
412 PEnd = D->protocol_end();
413 P != PEnd; ++P)
414 Writer.AddDeclRef(*P, Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000415 Record.push_back(D->ivar_size());
416 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
417 IEnd = D->ivar_end(); I != IEnd; ++I)
418 Writer.AddDeclRef(*I, Record);
Douglas Gregorae660c72009-04-23 22:34:55 +0000419 Writer.AddDeclRef(D->getCategoryList(), Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000420 Record.push_back(D->isForwardDecl());
421 Record.push_back(D->isImplicitInterfaceDecl());
422 Writer.AddSourceLocation(D->getClassLoc(), Record);
423 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
424 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000425 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000426}
427
428void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
429 VisitFieldDecl(D);
430 // FIXME: stable encoding for @public/@private/@protected/@package
431 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000432 Code = pch::DECL_OBJC_IVAR;
433}
434
435void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
436 VisitObjCContainerDecl(D);
437 Record.push_back(D->isForwardDecl());
438 Writer.AddSourceLocation(D->getLocEnd(), Record);
439 Record.push_back(D->protocol_size());
440 for (ObjCProtocolDecl::protocol_iterator
441 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
442 Writer.AddDeclRef(*I, Record);
443 Code = pch::DECL_OBJC_PROTOCOL;
444}
445
446void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
447 VisitFieldDecl(D);
448 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
449}
450
451void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
452 VisitDecl(D);
453 Record.push_back(D->size());
454 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
455 Writer.AddDeclRef(*I, Record);
456 Code = pch::DECL_OBJC_CLASS;
457}
458
459void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
460 VisitDecl(D);
461 Record.push_back(D->protocol_size());
462 for (ObjCProtocolDecl::protocol_iterator
463 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
464 Writer.AddDeclRef(*I, Record);
465 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
466}
467
468void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
469 VisitObjCContainerDecl(D);
470 Writer.AddDeclRef(D->getClassInterface(), Record);
471 Record.push_back(D->protocol_size());
472 for (ObjCProtocolDecl::protocol_iterator
473 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
474 Writer.AddDeclRef(*I, Record);
475 Writer.AddDeclRef(D->getNextClassCategory(), Record);
476 Writer.AddSourceLocation(D->getLocEnd(), Record);
477 Code = pch::DECL_OBJC_CATEGORY;
478}
479
480void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
481 VisitNamedDecl(D);
482 Writer.AddDeclRef(D->getClassInterface(), Record);
483 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
484}
485
486void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
487 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000488 Writer.AddTypeRef(D->getType(), Record);
489 // FIXME: stable encoding
490 Record.push_back((unsigned)D->getPropertyAttributes());
491 // FIXME: stable encoding
492 Record.push_back((unsigned)D->getPropertyImplementation());
493 Writer.AddDeclarationName(D->getGetterName(), Record);
494 Writer.AddDeclarationName(D->getSetterName(), Record);
495 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
496 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
497 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000498 Code = pch::DECL_OBJC_PROPERTY;
499}
500
501void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000502 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000503 Writer.AddDeclRef(D->getClassInterface(), Record);
504 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000505 // Abstract class (no need to define a stable pch::DECL code).
506}
507
508void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
509 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000510 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000511 Code = pch::DECL_OBJC_CATEGORY_IMPL;
512}
513
514void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
515 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000516 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000517 Code = pch::DECL_OBJC_IMPLEMENTATION;
518}
519
520void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
521 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000522 Writer.AddSourceLocation(D->getLocStart(), Record);
523 Writer.AddDeclRef(D->getPropertyDecl(), Record);
524 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000525 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000526}
527
Douglas Gregor982365e2009-04-13 21:20:57 +0000528void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
529 VisitValueDecl(D);
530 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000531 Record.push_back(D->getBitWidth()? 1 : 0);
532 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000533 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000534 Code = pch::DECL_FIELD;
535}
536
Douglas Gregorc34897d2009-04-09 22:27:44 +0000537void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
538 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000539 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000540 Record.push_back(D->isThreadSpecified());
541 Record.push_back(D->hasCXXDirectInitializer());
542 Record.push_back(D->isDeclaredInCondition());
543 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
544 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000545 Record.push_back(D->getInit()? 1 : 0);
546 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000547 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000548 Code = pch::DECL_VAR;
549}
550
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000551void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
552 VisitVarDecl(D);
553 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000554 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000555 // FIXME: why isn't the "default argument" just stored as the initializer
556 // in VarDecl?
557 Code = pch::DECL_PARM_VAR;
558}
559
560void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
561 VisitParmVarDecl(D);
562 Writer.AddTypeRef(D->getOriginalType(), Record);
563 Code = pch::DECL_ORIGINAL_PARM_VAR;
564}
565
Douglas Gregor2a491792009-04-13 22:49:25 +0000566void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
567 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000568 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000569 Code = pch::DECL_FILE_SCOPE_ASM;
570}
571
572void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
573 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000574 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000575 Record.push_back(D->param_size());
576 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
577 P != PEnd; ++P)
578 Writer.AddDeclRef(*P, Record);
579 Code = pch::DECL_BLOCK;
580}
581
Douglas Gregorc34897d2009-04-09 22:27:44 +0000582/// \brief Emit the DeclContext part of a declaration context decl.
583///
584/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
585/// block for this declaration context is stored. May be 0 to indicate
586/// that there are no declarations stored within this context.
587///
588/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
589/// block for this declaration context is stored. May be 0 to indicate
590/// that there are no declarations visible from this context. Note
591/// that this value will not be emitted for non-primary declaration
592/// contexts.
593void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
594 uint64_t VisibleOffset) {
595 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000596 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000597}
598
599//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000600// Statement/expression serialization
601//===----------------------------------------------------------------------===//
602namespace {
603 class VISIBILITY_HIDDEN PCHStmtWriter
604 : public StmtVisitor<PCHStmtWriter, void> {
605
606 PCHWriter &Writer;
607 PCHWriter::RecordData &Record;
608
609 public:
610 pch::StmtCode Code;
611
612 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
613 : Writer(Writer), Record(Record) { }
614
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000615 void VisitStmt(Stmt *S);
616 void VisitNullStmt(NullStmt *S);
617 void VisitCompoundStmt(CompoundStmt *S);
618 void VisitSwitchCase(SwitchCase *S);
619 void VisitCaseStmt(CaseStmt *S);
620 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000621 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000622 void VisitIfStmt(IfStmt *S);
623 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000624 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000625 void VisitDoStmt(DoStmt *S);
626 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000627 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000628 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000629 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000630 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000631 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000632 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000633 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000634 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000635 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000636 void VisitDeclRefExpr(DeclRefExpr *E);
637 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000638 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000639 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000640 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000641 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000642 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000643 void VisitUnaryOperator(UnaryOperator *E);
644 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000645 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000646 void VisitCallExpr(CallExpr *E);
647 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000648 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000649 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000650 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
651 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000652 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000653 void VisitExplicitCastExpr(ExplicitCastExpr *E);
654 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000655 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000656 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000657 void VisitInitListExpr(InitListExpr *E);
658 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
659 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000660 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000661 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000662 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000663 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
664 void VisitChooseExpr(ChooseExpr *E);
665 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000666 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000667 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000668 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000669
670 // Objective-C
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000671 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000672 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000673 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
674 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000675 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000676 };
677}
678
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000679void PCHStmtWriter::VisitStmt(Stmt *S) {
680}
681
682void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
683 VisitStmt(S);
684 Writer.AddSourceLocation(S->getSemiLoc(), Record);
685 Code = pch::STMT_NULL;
686}
687
688void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
689 VisitStmt(S);
690 Record.push_back(S->size());
691 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
692 CS != CSEnd; ++CS)
693 Writer.WriteSubStmt(*CS);
694 Writer.AddSourceLocation(S->getLBracLoc(), Record);
695 Writer.AddSourceLocation(S->getRBracLoc(), Record);
696 Code = pch::STMT_COMPOUND;
697}
698
699void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
700 VisitStmt(S);
701 Record.push_back(Writer.RecordSwitchCaseID(S));
702}
703
704void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
705 VisitSwitchCase(S);
706 Writer.WriteSubStmt(S->getLHS());
707 Writer.WriteSubStmt(S->getRHS());
708 Writer.WriteSubStmt(S->getSubStmt());
709 Writer.AddSourceLocation(S->getCaseLoc(), Record);
710 Code = pch::STMT_CASE;
711}
712
713void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
714 VisitSwitchCase(S);
715 Writer.WriteSubStmt(S->getSubStmt());
716 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
717 Code = pch::STMT_DEFAULT;
718}
719
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000720void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
721 VisitStmt(S);
722 Writer.AddIdentifierRef(S->getID(), Record);
723 Writer.WriteSubStmt(S->getSubStmt());
724 Writer.AddSourceLocation(S->getIdentLoc(), Record);
725 Record.push_back(Writer.GetLabelID(S));
726 Code = pch::STMT_LABEL;
727}
728
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000729void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
730 VisitStmt(S);
731 Writer.WriteSubStmt(S->getCond());
732 Writer.WriteSubStmt(S->getThen());
733 Writer.WriteSubStmt(S->getElse());
734 Writer.AddSourceLocation(S->getIfLoc(), Record);
735 Code = pch::STMT_IF;
736}
737
738void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
739 VisitStmt(S);
740 Writer.WriteSubStmt(S->getCond());
741 Writer.WriteSubStmt(S->getBody());
742 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
743 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
744 SC = SC->getNextSwitchCase())
745 Record.push_back(Writer.getSwitchCaseID(SC));
746 Code = pch::STMT_SWITCH;
747}
748
Douglas Gregora6b503f2009-04-17 00:16:09 +0000749void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
750 VisitStmt(S);
751 Writer.WriteSubStmt(S->getCond());
752 Writer.WriteSubStmt(S->getBody());
753 Writer.AddSourceLocation(S->getWhileLoc(), Record);
754 Code = pch::STMT_WHILE;
755}
756
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000757void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
758 VisitStmt(S);
759 Writer.WriteSubStmt(S->getCond());
760 Writer.WriteSubStmt(S->getBody());
761 Writer.AddSourceLocation(S->getDoLoc(), Record);
762 Code = pch::STMT_DO;
763}
764
765void PCHStmtWriter::VisitForStmt(ForStmt *S) {
766 VisitStmt(S);
767 Writer.WriteSubStmt(S->getInit());
768 Writer.WriteSubStmt(S->getCond());
769 Writer.WriteSubStmt(S->getInc());
770 Writer.WriteSubStmt(S->getBody());
771 Writer.AddSourceLocation(S->getForLoc(), Record);
772 Code = pch::STMT_FOR;
773}
774
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000775void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
776 VisitStmt(S);
777 Record.push_back(Writer.GetLabelID(S->getLabel()));
778 Writer.AddSourceLocation(S->getGotoLoc(), Record);
779 Writer.AddSourceLocation(S->getLabelLoc(), Record);
780 Code = pch::STMT_GOTO;
781}
782
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000783void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
784 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000785 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000786 Writer.WriteSubStmt(S->getTarget());
787 Code = pch::STMT_INDIRECT_GOTO;
788}
789
Douglas Gregora6b503f2009-04-17 00:16:09 +0000790void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
791 VisitStmt(S);
792 Writer.AddSourceLocation(S->getContinueLoc(), Record);
793 Code = pch::STMT_CONTINUE;
794}
795
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000796void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
797 VisitStmt(S);
798 Writer.AddSourceLocation(S->getBreakLoc(), Record);
799 Code = pch::STMT_BREAK;
800}
801
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000802void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
803 VisitStmt(S);
804 Writer.WriteSubStmt(S->getRetValue());
805 Writer.AddSourceLocation(S->getReturnLoc(), Record);
806 Code = pch::STMT_RETURN;
807}
808
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000809void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
810 VisitStmt(S);
811 Writer.AddSourceLocation(S->getStartLoc(), Record);
812 Writer.AddSourceLocation(S->getEndLoc(), Record);
813 DeclGroupRef DG = S->getDeclGroup();
814 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
815 Writer.AddDeclRef(*D, Record);
816 Code = pch::STMT_DECL;
817}
818
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000819void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
820 VisitStmt(S);
821 Record.push_back(S->getNumOutputs());
822 Record.push_back(S->getNumInputs());
823 Record.push_back(S->getNumClobbers());
824 Writer.AddSourceLocation(S->getAsmLoc(), Record);
825 Writer.AddSourceLocation(S->getRParenLoc(), Record);
826 Record.push_back(S->isVolatile());
827 Record.push_back(S->isSimple());
828 Writer.WriteSubStmt(S->getAsmString());
829
830 // Outputs
831 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
832 Writer.AddString(S->getOutputName(I), Record);
833 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
834 Writer.WriteSubStmt(S->getOutputExpr(I));
835 }
836
837 // Inputs
838 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
839 Writer.AddString(S->getInputName(I), Record);
840 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
841 Writer.WriteSubStmt(S->getInputExpr(I));
842 }
843
844 // Clobbers
845 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
846 Writer.WriteSubStmt(S->getClobber(I));
847
848 Code = pch::STMT_ASM;
849}
850
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000851void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000852 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000853 Writer.AddTypeRef(E->getType(), Record);
854 Record.push_back(E->isTypeDependent());
855 Record.push_back(E->isValueDependent());
856}
857
Douglas Gregore2f37202009-04-14 21:55:33 +0000858void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
859 VisitExpr(E);
860 Writer.AddSourceLocation(E->getLocation(), Record);
861 Record.push_back(E->getIdentType()); // FIXME: stable encoding
862 Code = pch::EXPR_PREDEFINED;
863}
864
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000865void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
866 VisitExpr(E);
867 Writer.AddDeclRef(E->getDecl(), Record);
868 Writer.AddSourceLocation(E->getLocation(), Record);
869 Code = pch::EXPR_DECL_REF;
870}
871
872void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
873 VisitExpr(E);
874 Writer.AddSourceLocation(E->getLocation(), Record);
875 Writer.AddAPInt(E->getValue(), Record);
876 Code = pch::EXPR_INTEGER_LITERAL;
877}
878
Douglas Gregore2f37202009-04-14 21:55:33 +0000879void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
880 VisitExpr(E);
881 Writer.AddAPFloat(E->getValue(), Record);
882 Record.push_back(E->isExact());
883 Writer.AddSourceLocation(E->getLocation(), Record);
884 Code = pch::EXPR_FLOATING_LITERAL;
885}
886
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000887void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
888 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000889 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000890 Code = pch::EXPR_IMAGINARY_LITERAL;
891}
892
Douglas Gregor596e0932009-04-15 16:35:07 +0000893void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
894 VisitExpr(E);
895 Record.push_back(E->getByteLength());
896 Record.push_back(E->getNumConcatenated());
897 Record.push_back(E->isWide());
898 // FIXME: String data should be stored as a blob at the end of the
899 // StringLiteral. However, we can't do so now because we have no
900 // provision for coping with abbreviations when we're jumping around
901 // the PCH file during deserialization.
902 Record.insert(Record.end(),
903 E->getStrData(), E->getStrData() + E->getByteLength());
904 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
905 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
906 Code = pch::EXPR_STRING_LITERAL;
907}
908
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000909void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
910 VisitExpr(E);
911 Record.push_back(E->getValue());
912 Writer.AddSourceLocation(E->getLoc(), Record);
913 Record.push_back(E->isWide());
914 Code = pch::EXPR_CHARACTER_LITERAL;
915}
916
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000917void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
918 VisitExpr(E);
919 Writer.AddSourceLocation(E->getLParen(), Record);
920 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000921 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000922 Code = pch::EXPR_PAREN;
923}
924
Douglas Gregor12d74052009-04-15 15:58:59 +0000925void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
926 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000927 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000928 Record.push_back(E->getOpcode()); // FIXME: stable encoding
929 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
930 Code = pch::EXPR_UNARY_OPERATOR;
931}
932
933void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
934 VisitExpr(E);
935 Record.push_back(E->isSizeOf());
936 if (E->isArgumentType())
937 Writer.AddTypeRef(E->getArgumentType(), Record);
938 else {
939 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000940 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000941 }
942 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
943 Writer.AddSourceLocation(E->getRParenLoc(), Record);
944 Code = pch::EXPR_SIZEOF_ALIGN_OF;
945}
946
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000947void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
948 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000949 Writer.WriteSubStmt(E->getLHS());
950 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000951 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
952 Code = pch::EXPR_ARRAY_SUBSCRIPT;
953}
954
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000955void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
956 VisitExpr(E);
957 Record.push_back(E->getNumArgs());
958 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000959 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000960 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
961 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000962 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000963 Code = pch::EXPR_CALL;
964}
965
966void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
967 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000968 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000969 Writer.AddDeclRef(E->getMemberDecl(), Record);
970 Writer.AddSourceLocation(E->getMemberLoc(), Record);
971 Record.push_back(E->isArrow());
972 Code = pch::EXPR_MEMBER;
973}
974
Douglas Gregora151ba42009-04-14 23:32:43 +0000975void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
976 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000977 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000978}
979
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000980void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
981 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000982 Writer.WriteSubStmt(E->getLHS());
983 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000984 Record.push_back(E->getOpcode()); // FIXME: stable encoding
985 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
986 Code = pch::EXPR_BINARY_OPERATOR;
987}
988
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000989void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
990 VisitBinaryOperator(E);
991 Writer.AddTypeRef(E->getComputationLHSType(), Record);
992 Writer.AddTypeRef(E->getComputationResultType(), Record);
993 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
994}
995
996void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
997 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000998 Writer.WriteSubStmt(E->getCond());
999 Writer.WriteSubStmt(E->getLHS());
1000 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001001 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1002}
1003
Douglas Gregora151ba42009-04-14 23:32:43 +00001004void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1005 VisitCastExpr(E);
1006 Record.push_back(E->isLvalueCast());
1007 Code = pch::EXPR_IMPLICIT_CAST;
1008}
1009
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001010void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1011 VisitCastExpr(E);
1012 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1013}
1014
1015void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1016 VisitExplicitCastExpr(E);
1017 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1018 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1019 Code = pch::EXPR_CSTYLE_CAST;
1020}
1021
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001022void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1023 VisitExpr(E);
1024 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001025 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001026 Record.push_back(E->isFileScope());
1027 Code = pch::EXPR_COMPOUND_LITERAL;
1028}
1029
Douglas Gregorec0b8292009-04-15 23:02:49 +00001030void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1031 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001032 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001033 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1034 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1035 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1036}
1037
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001038void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1039 VisitExpr(E);
1040 Record.push_back(E->getNumInits());
1041 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001042 Writer.WriteSubStmt(E->getInit(I));
1043 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001044 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1045 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1046 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1047 Record.push_back(E->hadArrayRangeDesignator());
1048 Code = pch::EXPR_INIT_LIST;
1049}
1050
1051void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1052 VisitExpr(E);
1053 Record.push_back(E->getNumSubExprs());
1054 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001055 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001056 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1057 Record.push_back(E->usesGNUSyntax());
1058 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1059 DEnd = E->designators_end();
1060 D != DEnd; ++D) {
1061 if (D->isFieldDesignator()) {
1062 if (FieldDecl *Field = D->getField()) {
1063 Record.push_back(pch::DESIG_FIELD_DECL);
1064 Writer.AddDeclRef(Field, Record);
1065 } else {
1066 Record.push_back(pch::DESIG_FIELD_NAME);
1067 Writer.AddIdentifierRef(D->getFieldName(), Record);
1068 }
1069 Writer.AddSourceLocation(D->getDotLoc(), Record);
1070 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1071 } else if (D->isArrayDesignator()) {
1072 Record.push_back(pch::DESIG_ARRAY);
1073 Record.push_back(D->getFirstExprIndex());
1074 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1075 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1076 } else {
1077 assert(D->isArrayRangeDesignator() && "Unknown designator");
1078 Record.push_back(pch::DESIG_ARRAY_RANGE);
1079 Record.push_back(D->getFirstExprIndex());
1080 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1081 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1082 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1083 }
1084 }
1085 Code = pch::EXPR_DESIGNATED_INIT;
1086}
1087
1088void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1089 VisitExpr(E);
1090 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1091}
1092
Douglas Gregorec0b8292009-04-15 23:02:49 +00001093void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1094 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001095 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001096 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1097 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1098 Code = pch::EXPR_VA_ARG;
1099}
1100
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001101void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1102 VisitExpr(E);
1103 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1104 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1105 Record.push_back(Writer.GetLabelID(E->getLabel()));
1106 Code = pch::EXPR_ADDR_LABEL;
1107}
1108
Douglas Gregoreca12f62009-04-17 19:05:30 +00001109void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1110 VisitExpr(E);
1111 Writer.WriteSubStmt(E->getSubStmt());
1112 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1113 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1114 Code = pch::EXPR_STMT;
1115}
1116
Douglas Gregor209d4622009-04-15 23:33:31 +00001117void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1118 VisitExpr(E);
1119 Writer.AddTypeRef(E->getArgType1(), Record);
1120 Writer.AddTypeRef(E->getArgType2(), Record);
1121 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1122 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1123 Code = pch::EXPR_TYPES_COMPATIBLE;
1124}
1125
1126void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1127 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001128 Writer.WriteSubStmt(E->getCond());
1129 Writer.WriteSubStmt(E->getLHS());
1130 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001131 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1132 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1133 Code = pch::EXPR_CHOOSE;
1134}
1135
1136void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1137 VisitExpr(E);
1138 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1139 Code = pch::EXPR_GNU_NULL;
1140}
1141
Douglas Gregor725e94b2009-04-16 00:01:45 +00001142void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1143 VisitExpr(E);
1144 Record.push_back(E->getNumSubExprs());
1145 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001146 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001147 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1148 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1149 Code = pch::EXPR_SHUFFLE_VECTOR;
1150}
1151
Douglas Gregore246b742009-04-17 19:21:43 +00001152void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1153 VisitExpr(E);
1154 Writer.AddDeclRef(E->getBlockDecl(), Record);
1155 Record.push_back(E->hasBlockDeclRefExprs());
1156 Code = pch::EXPR_BLOCK;
1157}
1158
Douglas Gregor725e94b2009-04-16 00:01:45 +00001159void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1160 VisitExpr(E);
1161 Writer.AddDeclRef(E->getDecl(), Record);
1162 Writer.AddSourceLocation(E->getLocation(), Record);
1163 Record.push_back(E->isByRef());
1164 Code = pch::EXPR_BLOCK_DECL_REF;
1165}
1166
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001167//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001168// Objective-C Expressions and Statements.
1169//===----------------------------------------------------------------------===//
1170
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001171void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1172 VisitExpr(E);
1173 Writer.WriteSubStmt(E->getString());
1174 Writer.AddSourceLocation(E->getAtLoc(), Record);
1175 Code = pch::EXPR_OBJC_STRING_LITERAL;
1176}
1177
Chris Lattner80f83c62009-04-22 05:57:30 +00001178void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1179 VisitExpr(E);
1180 Writer.AddTypeRef(E->getEncodedType(), Record);
1181 Writer.AddSourceLocation(E->getAtLoc(), Record);
1182 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1183 Code = pch::EXPR_OBJC_ENCODE;
1184}
1185
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001186void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1187 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001188 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001189 Writer.AddSourceLocation(E->getAtLoc(), Record);
1190 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1191 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1192}
1193
1194void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1195 VisitExpr(E);
1196 Writer.AddDeclRef(E->getProtocol(), Record);
1197 Writer.AddSourceLocation(E->getAtLoc(), Record);
1198 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1199 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1200}
1201
Steve Narofffb3e4022009-04-25 14:04:28 +00001202void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1203 VisitExpr(E);
1204 Record.push_back(E->getNumArgs());
1205 Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record);
1206 Writer.AddSourceLocation(E->getSourceRange().getEnd(), Record);
1207 Writer.AddSelectorRef(E->getSelector(), Record);
1208 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
1209 // FIXME: deal with class messages.
1210 Writer.WriteSubStmt(E->getReceiver());
1211 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1212 Arg != ArgEnd; ++Arg)
1213 Writer.WriteSubStmt(*Arg);
1214 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1215}
1216
Chris Lattner80f83c62009-04-22 05:57:30 +00001217
1218//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001219// PCHWriter Implementation
1220//===----------------------------------------------------------------------===//
1221
Douglas Gregorb5887f32009-04-10 21:16:55 +00001222/// \brief Write the target triple (e.g., i686-apple-darwin9).
1223void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1224 using namespace llvm;
1225 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1226 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001228 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001229
1230 RecordData Record;
1231 Record.push_back(pch::TARGET_TRIPLE);
1232 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001233 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001234}
1235
1236/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001237void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1238 RecordData Record;
1239 Record.push_back(LangOpts.Trigraphs);
1240 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1241 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1242 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1243 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1244 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1245 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1246 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1247 Record.push_back(LangOpts.C99); // C99 Support
1248 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1249 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1250 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1251 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1252 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1253
1254 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1255 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1256 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1257
1258 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1259 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1260 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1261 Record.push_back(LangOpts.LaxVectorConversions);
1262 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1263
1264 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1265 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1266 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1267
1268 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1269 // by locks.
1270 Record.push_back(LangOpts.Blocks); // block extension to C
1271 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1272 // they are unused.
1273 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1274 // (modulo the platform support).
1275
1276 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1277 // signed integer arithmetic overflows.
1278
1279 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1280 // may be ripped out at any time.
1281
1282 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1283 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1284 // defined.
1285 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1286 // opposed to __DYNAMIC__).
1287 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1288
1289 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1290 // used (instead of C99 semantics).
1291 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1292 Record.push_back(LangOpts.getGCMode());
1293 Record.push_back(LangOpts.getVisibilityMode());
1294 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001295 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001296}
1297
Douglas Gregorab1cef72009-04-10 03:52:48 +00001298//===----------------------------------------------------------------------===//
1299// Source Manager Serialization
1300//===----------------------------------------------------------------------===//
1301
1302/// \brief Create an abbreviation for the SLocEntry that refers to a
1303/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001304static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001305 using namespace llvm;
1306 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1307 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001313 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001314}
1315
1316/// \brief Create an abbreviation for the SLocEntry that refers to a
1317/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001318static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001319 using namespace llvm;
1320 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1321 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001327 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001328}
1329
1330/// \brief Create an abbreviation for the SLocEntry that refers to a
1331/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001332static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001333 using namespace llvm;
1334 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1335 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001337 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001338}
1339
1340/// \brief Create an abbreviation for the SLocEntry that refers to an
1341/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001342static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001343 using namespace llvm;
1344 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1345 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1349 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001351 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001352}
1353
1354/// \brief Writes the block containing the serialized form of the
1355/// source manager.
1356///
1357/// TODO: We should probably use an on-disk hash table (stored in a
1358/// blob), indexed based on the file name, so that we only create
1359/// entries for files that we actually need. In the common case (no
1360/// errors), we probably won't have to create file entries for any of
1361/// the files in the AST.
1362void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001363 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001364 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001365
1366 // Abbreviations for the various kinds of source-location entries.
1367 int SLocFileAbbrv = -1;
1368 int SLocBufferAbbrv = -1;
1369 int SLocBufferBlobAbbrv = -1;
1370 int SLocInstantiationAbbrv = -1;
1371
1372 // Write out the source location entry table. We skip the first
1373 // entry, which is always the same dummy entry.
1374 RecordData Record;
1375 for (SourceManager::sloc_entry_iterator
1376 SLoc = SourceMgr.sloc_entry_begin() + 1,
1377 SLocEnd = SourceMgr.sloc_entry_end();
1378 SLoc != SLocEnd; ++SLoc) {
1379 // Figure out which record code to use.
1380 unsigned Code;
1381 if (SLoc->isFile()) {
1382 if (SLoc->getFile().getContentCache()->Entry)
1383 Code = pch::SM_SLOC_FILE_ENTRY;
1384 else
1385 Code = pch::SM_SLOC_BUFFER_ENTRY;
1386 } else
1387 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1388 Record.push_back(Code);
1389
1390 Record.push_back(SLoc->getOffset());
1391 if (SLoc->isFile()) {
1392 const SrcMgr::FileInfo &File = SLoc->getFile();
1393 Record.push_back(File.getIncludeLoc().getRawEncoding());
1394 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001395 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001396
1397 const SrcMgr::ContentCache *Content = File.getContentCache();
1398 if (Content->Entry) {
1399 // The source location entry is a file. The blob associated
1400 // with this entry is the file name.
1401 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001402 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1403 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001404 Content->Entry->getName(),
1405 strlen(Content->Entry->getName()));
1406 } else {
1407 // The source location entry is a buffer. The blob associated
1408 // with this entry contains the contents of the buffer.
1409 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001410 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1411 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001412 }
1413
1414 // We add one to the size so that we capture the trailing NULL
1415 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1416 // the reader side).
1417 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1418 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001419 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001420 Record.clear();
1421 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001422 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001423 Buffer->getBufferStart(),
1424 Buffer->getBufferSize() + 1);
1425 }
1426 } else {
1427 // The source location entry is an instantiation.
1428 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1429 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1430 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1431 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1432
Douglas Gregor364e5802009-04-15 18:05:10 +00001433 // Compute the token length for this macro expansion.
1434 unsigned NextOffset = SourceMgr.getNextOffset();
1435 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1436 if (++NextSLoc != SLocEnd)
1437 NextOffset = NextSLoc->getOffset();
1438 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1439
Douglas Gregorab1cef72009-04-10 03:52:48 +00001440 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001441 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1442 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001443 }
1444
1445 Record.clear();
1446 }
1447
Douglas Gregor635f97f2009-04-13 16:31:14 +00001448 // Write the line table.
1449 if (SourceMgr.hasLineTable()) {
1450 LineTableInfo &LineTable = SourceMgr.getLineTable();
1451
1452 // Emit the file names
1453 Record.push_back(LineTable.getNumFilenames());
1454 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1455 // Emit the file name
1456 const char *Filename = LineTable.getFilename(I);
1457 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1458 Record.push_back(FilenameLen);
1459 if (FilenameLen)
1460 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1461 }
1462
1463 // Emit the line entries
1464 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1465 L != LEnd; ++L) {
1466 // Emit the file ID
1467 Record.push_back(L->first);
1468
1469 // Emit the line entries
1470 Record.push_back(L->second.size());
1471 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1472 LEEnd = L->second.end();
1473 LE != LEEnd; ++LE) {
1474 Record.push_back(LE->FileOffset);
1475 Record.push_back(LE->LineNo);
1476 Record.push_back(LE->FilenameID);
1477 Record.push_back((unsigned)LE->FileKind);
1478 Record.push_back(LE->IncludeOffset);
1479 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001480 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001481 }
1482 }
1483
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001484 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001485}
1486
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001487/// \brief Writes the block containing the serialized form of the
1488/// preprocessor.
1489///
Chris Lattner850eabd2009-04-10 18:08:30 +00001490void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001491 // Enter the preprocessor block.
Douglas Gregorc713da92009-04-21 22:25:48 +00001492 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner84b04f12009-04-10 17:16:57 +00001493
Chris Lattner1b094952009-04-10 18:00:12 +00001494 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1495 // FIXME: use diagnostics subsystem for localization etc.
1496 if (PP.SawDateOrTime())
1497 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001498
Chris Lattner1b094952009-04-10 18:00:12 +00001499 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001500
Chris Lattner4b21c202009-04-13 01:29:17 +00001501 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1502 if (PP.getCounterValue() != 0) {
1503 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001504 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001505 Record.clear();
1506 }
1507
Chris Lattner1b094952009-04-10 18:00:12 +00001508 // Loop over all the macro definitions that are live at the end of the file,
1509 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001510 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1511 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001512 // FIXME: This emits macros in hash table order, we should do it in a stable
1513 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001514 MacroInfo *MI = I->second;
1515
1516 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1517 // been redefined by the header (in which case they are not isBuiltinMacro).
1518 if (MI->isBuiltinMacro())
1519 continue;
1520
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001521 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001522 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001523 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001524 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1525 Record.push_back(MI->isUsed());
1526
1527 unsigned Code;
1528 if (MI->isObjectLike()) {
1529 Code = pch::PP_MACRO_OBJECT_LIKE;
1530 } else {
1531 Code = pch::PP_MACRO_FUNCTION_LIKE;
1532
1533 Record.push_back(MI->isC99Varargs());
1534 Record.push_back(MI->isGNUVarargs());
1535 Record.push_back(MI->getNumArgs());
1536 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1537 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001538 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001539 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001540 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001541 Record.clear();
1542
Chris Lattner850eabd2009-04-10 18:08:30 +00001543 // Emit the tokens array.
1544 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1545 // Note that we know that the preprocessor does not have any annotation
1546 // tokens in it because they are created by the parser, and thus can't be
1547 // in a macro definition.
1548 const Token &Tok = MI->getReplacementToken(TokNo);
1549
1550 Record.push_back(Tok.getLocation().getRawEncoding());
1551 Record.push_back(Tok.getLength());
1552
Chris Lattner850eabd2009-04-10 18:08:30 +00001553 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1554 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001555 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001556
1557 // FIXME: Should translate token kind to a stable encoding.
1558 Record.push_back(Tok.getKind());
1559 // FIXME: Should translate token flags to a stable encoding.
1560 Record.push_back(Tok.getFlags());
1561
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001562 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001563 Record.clear();
1564 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001565 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001566 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001567
1568 // Loop over all the header files.
1569 HeaderSearch &HS = PP.getHeaderSearchInfo();
1570 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1571 E = HS.header_file_end();
1572 I != E; ++I) {
Steve Naroffc1166732009-04-25 12:07:12 +00001573 Record.push_back(I->isImport);
1574 Record.push_back(I->DirInfo);
1575 Record.push_back(I->NumIncludes);
1576 if (I->ControllingMacro)
1577 AddIdentifierRef(I->ControllingMacro, Record);
Steve Naroffcda68f22009-04-24 20:03:17 +00001578 else
1579 Record.push_back(0);
1580 Stream.EmitRecord(pch::PP_HEADER_FILE_INFO, Record);
1581 Record.clear();
1582 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001583 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001584}
1585
1586
Douglas Gregorc34897d2009-04-09 22:27:44 +00001587/// \brief Write the representation of a type to the PCH stream.
1588void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001589 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001590 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001591 ID = NextTypeID++;
1592
1593 // Record the offset for this type.
1594 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001595 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001596 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1597 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001598 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001599 }
1600
1601 RecordData Record;
1602
1603 // Emit the type's representation.
1604 PCHTypeWriter W(*this, Record);
1605 switch (T->getTypeClass()) {
1606 // For all of the concrete, non-dependent types, call the
1607 // appropriate visitor function.
1608#define TYPE(Class, Base) \
1609 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1610#define ABSTRACT_TYPE(Class, Base)
1611#define DEPENDENT_TYPE(Class, Base)
1612#include "clang/AST/TypeNodes.def"
1613
1614 // For all of the dependent type nodes (which only occur in C++
1615 // templates), produce an error.
1616#define TYPE(Class, Base)
1617#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1618#include "clang/AST/TypeNodes.def"
1619 assert(false && "Cannot serialize dependent type nodes");
1620 break;
1621 }
1622
1623 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001624 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001625
1626 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001627 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001628}
1629
1630/// \brief Write a block containing all of the types.
1631void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001632 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001633 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001634
1635 // Emit all of the types in the ASTContext
1636 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1637 TEnd = Context.getTypes().end();
1638 T != TEnd; ++T) {
1639 // Builtin types are never serialized.
1640 if (isa<BuiltinType>(*T))
1641 continue;
1642
1643 WriteType(*T);
1644 }
1645
1646 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001647 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001648}
1649
1650/// \brief Write the block containing all of the declaration IDs
1651/// lexically declared within the given DeclContext.
1652///
1653/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1654/// bistream, or 0 if no block was written.
1655uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1656 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001657 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001658 return 0;
1659
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001660 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001661 RecordData Record;
1662 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1663 DEnd = DC->decls_end(Context);
1664 D != DEnd; ++D)
1665 AddDeclRef(*D, Record);
1666
Douglas Gregoraf136d92009-04-22 22:34:57 +00001667 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001668 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001669 return Offset;
1670}
1671
1672/// \brief Write the block containing all of the declaration IDs
1673/// visible from the given DeclContext.
1674///
1675/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1676/// bistream, or 0 if no block was written.
1677uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1678 DeclContext *DC) {
1679 if (DC->getPrimaryContext() != DC)
1680 return 0;
1681
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001682 // Since there is no name lookup into functions or methods, and we
1683 // perform name lookup for the translation unit via the
1684 // IdentifierInfo chains, don't bother to build a
1685 // visible-declarations table for these entities.
1686 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001687 return 0;
1688
Douglas Gregorc34897d2009-04-09 22:27:44 +00001689 // Force the DeclContext to build a its name-lookup table.
1690 DC->lookup(Context, DeclarationName());
1691
1692 // Serialize the contents of the mapping used for lookup. Note that,
1693 // although we have two very different code paths, the serialized
1694 // representation is the same for both cases: a declaration name,
1695 // followed by a size, followed by references to the visible
1696 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001697 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001698 RecordData Record;
1699 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001700 if (!Map)
1701 return 0;
1702
Douglas Gregorc34897d2009-04-09 22:27:44 +00001703 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1704 D != DEnd; ++D) {
1705 AddDeclarationName(D->first, Record);
1706 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1707 Record.push_back(Result.second - Result.first);
1708 for(; Result.first != Result.second; ++Result.first)
1709 AddDeclRef(*Result.first, Record);
1710 }
1711
1712 if (Record.size() == 0)
1713 return 0;
1714
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001715 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001716 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001717 return Offset;
1718}
1719
1720/// \brief Write a block containing all of the declarations.
1721void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001722 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001723 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001724
1725 // Emit all of the declarations.
1726 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001727 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001728 while (!DeclsToEmit.empty()) {
1729 // Pull the next declaration off the queue
1730 Decl *D = DeclsToEmit.front();
1731 DeclsToEmit.pop();
1732
1733 // If this declaration is also a DeclContext, write blocks for the
1734 // declarations that lexically stored inside its context and those
1735 // declarations that are visible from its context. These blocks
1736 // are written before the declaration itself so that we can put
1737 // their offsets into the record for the declaration.
1738 uint64_t LexicalOffset = 0;
1739 uint64_t VisibleOffset = 0;
1740 DeclContext *DC = dyn_cast<DeclContext>(D);
1741 if (DC) {
1742 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1743 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1744 }
1745
1746 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001747 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001748 if (ID == 0)
1749 ID = DeclIDs.size();
1750
1751 unsigned Index = ID - 1;
1752
1753 // Record the offset for this declaration
1754 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001755 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001756 else if (DeclOffsets.size() < Index) {
1757 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001758 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001759 }
1760
1761 // Build and emit a record for this declaration
1762 Record.clear();
1763 W.Code = (pch::DeclCode)0;
1764 W.Visit(D);
1765 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001766
1767 if (!W.Code) {
1768 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1769 D->getDeclKindName());
1770 assert(false && "Unhandled declaration kind while generating PCH");
1771 exit(-1);
1772 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001773 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001774
Douglas Gregor1c507882009-04-15 21:30:51 +00001775 // If the declaration had any attributes, write them now.
1776 if (D->hasAttrs())
1777 WriteAttributeRecord(D->getAttrs());
1778
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001779 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001780 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001781
Douglas Gregor631f6c62009-04-14 00:24:19 +00001782 // Note external declarations so that we can add them to a record
1783 // in the PCH file later.
1784 if (isa<FileScopeAsmDecl>(D))
1785 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001786 }
1787
1788 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001789 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001790}
1791
Douglas Gregorff9a6092009-04-20 20:36:09 +00001792namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001793// Trait used for the on-disk hash table used in the method pool.
1794class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1795 PCHWriter &Writer;
1796
1797public:
1798 typedef Selector key_type;
1799 typedef key_type key_type_ref;
1800
1801 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1802 typedef const data_type& data_type_ref;
1803
1804 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1805
1806 static unsigned ComputeHash(Selector Sel) {
1807 unsigned N = Sel.getNumArgs();
1808 if (N == 0)
1809 ++N;
1810 unsigned R = 5381;
1811 for (unsigned I = 0; I != N; ++I)
1812 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1813 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1814 return R;
1815 }
1816
1817 std::pair<unsigned,unsigned>
1818 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1819 data_type_ref Methods) {
1820 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1821 clang::io::Emit16(Out, KeyLen);
1822 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1823 for (const ObjCMethodList *Method = &Methods.first; Method;
1824 Method = Method->Next)
1825 if (Method->Method)
1826 DataLen += 4;
1827 for (const ObjCMethodList *Method = &Methods.second; Method;
1828 Method = Method->Next)
1829 if (Method->Method)
1830 DataLen += 4;
1831 clang::io::Emit16(Out, DataLen);
1832 return std::make_pair(KeyLen, DataLen);
1833 }
1834
Douglas Gregor2d711832009-04-25 17:48:32 +00001835 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1836 uint64_t Start = Out.tell();
1837 assert((Start >> 32) == 0 && "Selector key offset too large");
1838 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001839 unsigned N = Sel.getNumArgs();
1840 clang::io::Emit16(Out, N);
1841 if (N == 0)
1842 N = 1;
1843 for (unsigned I = 0; I != N; ++I)
1844 clang::io::Emit32(Out,
1845 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1846 }
1847
1848 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001849 data_type_ref Methods, unsigned DataLen) {
1850 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001851 unsigned NumInstanceMethods = 0;
1852 for (const ObjCMethodList *Method = &Methods.first; Method;
1853 Method = Method->Next)
1854 if (Method->Method)
1855 ++NumInstanceMethods;
1856
1857 unsigned NumFactoryMethods = 0;
1858 for (const ObjCMethodList *Method = &Methods.second; Method;
1859 Method = Method->Next)
1860 if (Method->Method)
1861 ++NumFactoryMethods;
1862
1863 clang::io::Emit16(Out, NumInstanceMethods);
1864 clang::io::Emit16(Out, NumFactoryMethods);
1865 for (const ObjCMethodList *Method = &Methods.first; Method;
1866 Method = Method->Next)
1867 if (Method->Method)
1868 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001869 for (const ObjCMethodList *Method = &Methods.second; Method;
1870 Method = Method->Next)
1871 if (Method->Method)
1872 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001873
1874 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001875 }
1876};
1877} // end anonymous namespace
1878
1879/// \brief Write the method pool into the PCH file.
1880///
1881/// The method pool contains both instance and factory methods, stored
1882/// in an on-disk hash table indexed by the selector.
1883void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1884 using namespace llvm;
1885
1886 // Create and write out the blob that contains the instance and
1887 // factor method pools.
1888 bool Empty = true;
1889 {
1890 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1891
1892 // Create the on-disk hash table representation. Start by
1893 // iterating through the instance method pool.
1894 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001895 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001896 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1897 Instance = SemaRef.InstanceMethodPool.begin(),
1898 InstanceEnd = SemaRef.InstanceMethodPool.end();
1899 Instance != InstanceEnd; ++Instance) {
1900 // Check whether there is a factory method with the same
1901 // selector.
1902 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1903 = SemaRef.FactoryMethodPool.find(Instance->first);
1904
1905 if (Factory == SemaRef.FactoryMethodPool.end())
1906 Generator.insert(Instance->first,
1907 std::make_pair(Instance->second,
1908 ObjCMethodList()));
1909 else
1910 Generator.insert(Instance->first,
1911 std::make_pair(Instance->second, Factory->second));
1912
Douglas Gregor2d711832009-04-25 17:48:32 +00001913 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001914 Empty = false;
1915 }
1916
1917 // Now iterate through the factory method pool, to pick up any
1918 // selectors that weren't already in the instance method pool.
1919 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1920 Factory = SemaRef.FactoryMethodPool.begin(),
1921 FactoryEnd = SemaRef.FactoryMethodPool.end();
1922 Factory != FactoryEnd; ++Factory) {
1923 // Check whether there is an instance method with the same
1924 // selector. If so, there is no work to do here.
1925 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1926 = SemaRef.InstanceMethodPool.find(Factory->first);
1927
Douglas Gregor2d711832009-04-25 17:48:32 +00001928 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001929 Generator.insert(Factory->first,
1930 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001931 ++NumSelectorsInMethodPool;
1932 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001933
1934 Empty = false;
1935 }
1936
Douglas Gregor2d711832009-04-25 17:48:32 +00001937 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001938 return;
1939
1940 // Create the on-disk hash table in a buffer.
1941 llvm::SmallVector<char, 4096> MethodPool;
1942 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001943 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001944 {
1945 PCHMethodPoolTrait Trait(*this);
1946 llvm::raw_svector_ostream Out(MethodPool);
1947 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001948 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001949 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001950
1951 // For every selector that we have seen but which was not
1952 // written into the hash table, write the selector itself and
1953 // record it's offset.
1954 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1955 if (SelectorOffsets[I] == 0)
1956 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001957 }
1958
1959 // Create a blob abbreviation
1960 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1961 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1965 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1966
Douglas Gregor2d711832009-04-25 17:48:32 +00001967 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001968 RecordData Record;
1969 Record.push_back(pch::METHOD_POOL);
1970 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001971 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001972 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1973 &MethodPool.front(),
1974 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00001975
1976 // Create a blob abbreviation for the selector table offsets.
1977 Abbrev = new BitCodeAbbrev();
1978 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1981 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1982
1983 // Write the selector offsets table.
1984 Record.clear();
1985 Record.push_back(pch::SELECTOR_OFFSETS);
1986 Record.push_back(SelectorOffsets.size());
1987 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1988 (const char *)&SelectorOffsets.front(),
1989 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001990 }
1991}
1992
1993namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001994class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1995 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001996 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001997
1998public:
1999 typedef const IdentifierInfo* key_type;
2000 typedef key_type key_type_ref;
2001
2002 typedef pch::IdentID data_type;
2003 typedef data_type data_type_ref;
2004
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002005 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2006 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002007
2008 static unsigned ComputeHash(const IdentifierInfo* II) {
2009 return clang::BernsteinHash(II->getName());
2010 }
2011
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002012 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002013 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2014 pch::IdentID ID) {
2015 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002016 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2017 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002018 if (II->hasMacroDefinition() &&
2019 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2020 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002021 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2022 DEnd = IdentifierResolver::end();
2023 D != DEnd; ++D)
2024 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002025 // We emit the key length after the data length so that the
2026 // "uninteresting" identifiers following the identifier hash table
2027 // structure will have the same (key length, key characters)
2028 // layout as the keys in the hash table. This also matches the
2029 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002030 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002031 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002032 return std::make_pair(KeyLen, DataLen);
2033 }
2034
2035 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2036 unsigned KeyLen) {
2037 // Record the location of the key data. This is used when generating
2038 // the mapping from persistent IDs to strings.
2039 Writer.SetIdentifierOffset(II, Out.tell());
2040 Out.write(II->getName(), KeyLen);
2041 }
2042
2043 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2044 pch::IdentID ID, unsigned) {
2045 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002046 bool hasMacroDefinition =
2047 II->hasMacroDefinition() &&
2048 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002049 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002050 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2051 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002052 Bits = (Bits << 1) | II->isExtensionToken();
2053 Bits = (Bits << 1) | II->isPoisoned();
2054 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2055 clang::io::Emit32(Out, Bits);
2056 clang::io::Emit32(Out, ID);
2057
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002058 if (hasMacroDefinition)
2059 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2060
Douglas Gregorc713da92009-04-21 22:25:48 +00002061 // Emit the declaration IDs in reverse order, because the
2062 // IdentifierResolver provides the declarations as they would be
2063 // visible (e.g., the function "stat" would come before the struct
2064 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2065 // adds declarations to the end of the list (so we need to see the
2066 // struct "status" before the function "status").
2067 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2068 IdentifierResolver::end());
2069 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2070 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002071 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002072 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002073 }
2074};
2075} // end anonymous namespace
2076
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002077/// \brief Write the identifier table into the PCH file.
2078///
2079/// The identifier table consists of a blob containing string data
2080/// (the actual identifiers themselves) and a separate "offsets" index
2081/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002082void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002083 using namespace llvm;
2084
2085 // Create and write out the blob that contains the identifier
2086 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002087 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002088 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002089 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2090
Douglas Gregor85c4a872009-04-25 21:04:17 +00002091 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2092
Douglas Gregorff9a6092009-04-20 20:36:09 +00002093 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002094 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2095 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2096 ID != IDEnd; ++ID) {
2097 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002098
2099 // Classify each identifier as either "interesting" or "not
2100 // interesting". Interesting identifiers are those that have
2101 // additional information that needs to be read from the PCH
2102 // file, e.g., a built-in ID, declaration chain, or macro
2103 // definition. These identifiers are placed into the hash table
2104 // so that they can be found when looked up in the user program.
2105 // All other identifiers are "uninteresting", which means that
2106 // the IdentifierInfo built by default has all of the
2107 // information we care about. Such identifiers are placed after
2108 // the hash table.
2109 const IdentifierInfo *II = ID->first;
2110 if (II->isPoisoned() ||
2111 II->isExtensionToken() ||
2112 II->hasMacroDefinition() ||
2113 II->getObjCOrBuiltinID() ||
2114 II->getFETokenInfo<void>())
2115 Generator.insert(ID->first, ID->second);
2116 else
2117 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002118 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002119
Douglas Gregorff9a6092009-04-20 20:36:09 +00002120 // Create the on-disk hash table in a buffer.
2121 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002122 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002123 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002124 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002125 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002126 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002127 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002128 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002129
2130 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2131 const IdentifierInfo *II = UninterestingIdentifiers[I];
2132 unsigned N = II->getLength() + 1;
2133 clang::io::Emit16(Out, N);
2134 SetIdentifierOffset(II, Out.tell());
2135 Out.write(II->getName(), N);
2136 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002137 }
2138
2139 // Create a blob abbreviation
2140 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2141 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002144 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002145
2146 // Write the identifier table
2147 RecordData Record;
2148 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002149 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002150 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2151 &IdentifierTable.front(),
2152 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002153 }
2154
2155 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002156 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2157 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2160 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2161
2162 RecordData Record;
2163 Record.push_back(pch::IDENTIFIER_OFFSET);
2164 Record.push_back(IdentifierOffsets.size());
2165 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2166 (const char *)&IdentifierOffsets.front(),
2167 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002168}
2169
Douglas Gregor1c507882009-04-15 21:30:51 +00002170/// \brief Write a record containing the given attributes.
2171void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2172 RecordData Record;
2173 for (; Attr; Attr = Attr->getNext()) {
2174 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2175 Record.push_back(Attr->isInherited());
2176 switch (Attr->getKind()) {
2177 case Attr::Alias:
2178 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2179 break;
2180
2181 case Attr::Aligned:
2182 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2183 break;
2184
2185 case Attr::AlwaysInline:
2186 break;
2187
2188 case Attr::AnalyzerNoReturn:
2189 break;
2190
2191 case Attr::Annotate:
2192 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2193 break;
2194
2195 case Attr::AsmLabel:
2196 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2197 break;
2198
2199 case Attr::Blocks:
2200 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2201 break;
2202
2203 case Attr::Cleanup:
2204 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2205 break;
2206
2207 case Attr::Const:
2208 break;
2209
2210 case Attr::Constructor:
2211 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2212 break;
2213
2214 case Attr::DLLExport:
2215 case Attr::DLLImport:
2216 case Attr::Deprecated:
2217 break;
2218
2219 case Attr::Destructor:
2220 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2221 break;
2222
2223 case Attr::FastCall:
2224 break;
2225
2226 case Attr::Format: {
2227 const FormatAttr *Format = cast<FormatAttr>(Attr);
2228 AddString(Format->getType(), Record);
2229 Record.push_back(Format->getFormatIdx());
2230 Record.push_back(Format->getFirstArg());
2231 break;
2232 }
2233
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002234 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002235 case Attr::IBOutletKind:
2236 case Attr::NoReturn:
2237 case Attr::NoThrow:
2238 case Attr::Nodebug:
2239 case Attr::Noinline:
2240 break;
2241
2242 case Attr::NonNull: {
2243 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2244 Record.push_back(NonNull->size());
2245 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2246 break;
2247 }
2248
2249 case Attr::ObjCException:
2250 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002251 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002252 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002253 case Attr::Overloadable:
2254 break;
2255
2256 case Attr::Packed:
2257 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2258 break;
2259
2260 case Attr::Pure:
2261 break;
2262
2263 case Attr::Regparm:
2264 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2265 break;
2266
2267 case Attr::Section:
2268 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2269 break;
2270
2271 case Attr::StdCall:
2272 case Attr::TransparentUnion:
2273 case Attr::Unavailable:
2274 case Attr::Unused:
2275 case Attr::Used:
2276 break;
2277
2278 case Attr::Visibility:
2279 // FIXME: stable encoding
2280 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2281 break;
2282
2283 case Attr::WarnUnusedResult:
2284 case Attr::Weak:
2285 case Attr::WeakImport:
2286 break;
2287 }
2288 }
2289
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002290 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002291}
2292
2293void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2294 Record.push_back(Str.size());
2295 Record.insert(Record.end(), Str.begin(), Str.end());
2296}
2297
Douglas Gregorff9a6092009-04-20 20:36:09 +00002298/// \brief Note that the identifier II occurs at the given offset
2299/// within the identifier table.
2300void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002301 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002302}
2303
Douglas Gregor2d711832009-04-25 17:48:32 +00002304/// \brief Note that the selector Sel occurs at the given offset
2305/// within the method pool/selector table.
2306void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2307 unsigned ID = SelectorIDs[Sel];
2308 assert(ID && "Unknown selector");
2309 SelectorOffsets[ID - 1] = Offset;
2310}
2311
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002312PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002313 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002314 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2315 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002316
Douglas Gregor87887da2009-04-20 15:53:59 +00002317void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002318 using namespace llvm;
2319
Douglas Gregor87887da2009-04-20 15:53:59 +00002320 ASTContext &Context = SemaRef.Context;
2321 Preprocessor &PP = SemaRef.PP;
2322
Douglas Gregorc34897d2009-04-09 22:27:44 +00002323 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002324 Stream.Emit((unsigned)'C', 8);
2325 Stream.Emit((unsigned)'P', 8);
2326 Stream.Emit((unsigned)'C', 8);
2327 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002328
2329 // The translation unit is the first declaration we'll emit.
2330 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2331 DeclsToEmit.push(Context.getTranslationUnitDecl());
2332
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002333 // Make sure that we emit IdentifierInfos (and any attached
2334 // declarations) for builtins.
2335 {
2336 IdentifierTable &Table = PP.getIdentifierTable();
2337 llvm::SmallVector<const char *, 32> BuiltinNames;
2338 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2339 Context.getLangOptions().NoBuiltin);
2340 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2341 getIdentifierRef(&Table.get(BuiltinNames[I]));
2342 }
2343
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002344 // Build a record containing all of the tentative definitions in
2345 // this header file. Generally, this record will be empty.
2346 RecordData TentativeDefinitions;
2347 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2348 TD = SemaRef.TentativeDefinitions.begin(),
2349 TDEnd = SemaRef.TentativeDefinitions.end();
2350 TD != TDEnd; ++TD)
2351 AddDeclRef(TD->second, TentativeDefinitions);
2352
Douglas Gregor062d9482009-04-22 22:18:58 +00002353 // Build a record containing all of the locally-scoped external
2354 // declarations in this header file. Generally, this record will be
2355 // empty.
2356 RecordData LocallyScopedExternalDecls;
2357 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2358 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2359 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2360 TD != TDEnd; ++TD)
2361 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2362
Douglas Gregorc34897d2009-04-09 22:27:44 +00002363 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002364 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002365 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002366 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002367 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00002368 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002369 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002370 WriteTypesBlock(Context);
2371 WriteDeclsBlock(Context);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002372 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002373 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002374
2375 // Write the type offsets array
2376 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2377 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2380 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2381 Record.clear();
2382 Record.push_back(pch::TYPE_OFFSET);
2383 Record.push_back(TypeOffsets.size());
2384 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2385 (const char *)&TypeOffsets.front(),
2386 TypeOffsets.size() * sizeof(uint64_t));
2387
2388 // Write the declaration offsets array
2389 Abbrev = new BitCodeAbbrev();
2390 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2393 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2394 Record.clear();
2395 Record.push_back(pch::DECL_OFFSET);
2396 Record.push_back(DeclOffsets.size());
2397 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2398 (const char *)&DeclOffsets.front(),
2399 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002400
2401 // Write the record of special types.
2402 Record.clear();
2403 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002404 AddTypeRef(Context.getObjCIdType(), Record);
2405 AddTypeRef(Context.getObjCSelType(), Record);
2406 AddTypeRef(Context.getObjCProtoType(), Record);
2407 AddTypeRef(Context.getObjCClassType(), Record);
2408 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2409 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002410 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2411
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002412 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002413 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002414 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002415
2416 // Write the record containing tentative definitions.
2417 if (!TentativeDefinitions.empty())
2418 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002419
2420 // Write the record containing locally-scoped external definitions.
2421 if (!LocallyScopedExternalDecls.empty())
2422 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2423 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002424
2425 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002426 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002427 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002428 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002429 Record.push_back(NumLexicalDeclContexts);
2430 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002431 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002432 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002433}
2434
2435void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2436 Record.push_back(Loc.getRawEncoding());
2437}
2438
2439void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2440 Record.push_back(Value.getBitWidth());
2441 unsigned N = Value.getNumWords();
2442 const uint64_t* Words = Value.getRawData();
2443 for (unsigned I = 0; I != N; ++I)
2444 Record.push_back(Words[I]);
2445}
2446
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002447void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2448 Record.push_back(Value.isUnsigned());
2449 AddAPInt(Value, Record);
2450}
2451
Douglas Gregore2f37202009-04-14 21:55:33 +00002452void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2453 AddAPInt(Value.bitcastToAPInt(), Record);
2454}
2455
Douglas Gregorc34897d2009-04-09 22:27:44 +00002456void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002457 Record.push_back(getIdentifierRef(II));
2458}
2459
2460pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2461 if (II == 0)
2462 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002463
2464 pch::IdentID &ID = IdentifierIDs[II];
2465 if (ID == 0)
2466 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002467 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002468}
2469
Steve Naroff9e84d782009-04-23 10:39:46 +00002470void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2471 if (SelRef.getAsOpaquePtr() == 0) {
2472 Record.push_back(0);
2473 return;
2474 }
2475
2476 pch::SelectorID &SID = SelectorIDs[SelRef];
2477 if (SID == 0) {
2478 SID = SelectorIDs.size();
2479 SelVector.push_back(SelRef);
2480 }
2481 Record.push_back(SID);
2482}
2483
Douglas Gregorc34897d2009-04-09 22:27:44 +00002484void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2485 if (T.isNull()) {
2486 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2487 return;
2488 }
2489
2490 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002491 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002492 switch (BT->getKind()) {
2493 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2494 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2495 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2496 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2497 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2498 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2499 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2500 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2501 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2502 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2503 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2504 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2505 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2506 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2507 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2508 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2509 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2510 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2511 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2512 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2513 }
2514
2515 Record.push_back((ID << 3) | T.getCVRQualifiers());
2516 return;
2517 }
2518
Douglas Gregorac8f2802009-04-10 17:25:41 +00002519 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002520 if (ID == 0) // we haven't seen this type before
2521 ID = NextTypeID++;
2522
2523 // Encode the type qualifiers in the type reference.
2524 Record.push_back((ID << 3) | T.getCVRQualifiers());
2525}
2526
2527void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2528 if (D == 0) {
2529 Record.push_back(0);
2530 return;
2531 }
2532
Douglas Gregorac8f2802009-04-10 17:25:41 +00002533 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002534 if (ID == 0) {
2535 // We haven't seen this declaration before. Give it a new ID and
2536 // enqueue it in the list of declarations to emit.
2537 ID = DeclIDs.size();
2538 DeclsToEmit.push(const_cast<Decl *>(D));
2539 }
2540
2541 Record.push_back(ID);
2542}
2543
Douglas Gregorff9a6092009-04-20 20:36:09 +00002544pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2545 if (D == 0)
2546 return 0;
2547
2548 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2549 return DeclIDs[D];
2550}
2551
Douglas Gregorc34897d2009-04-09 22:27:44 +00002552void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2553 Record.push_back(Name.getNameKind());
2554 switch (Name.getNameKind()) {
2555 case DeclarationName::Identifier:
2556 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2557 break;
2558
2559 case DeclarationName::ObjCZeroArgSelector:
2560 case DeclarationName::ObjCOneArgSelector:
2561 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002562 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002563 break;
2564
2565 case DeclarationName::CXXConstructorName:
2566 case DeclarationName::CXXDestructorName:
2567 case DeclarationName::CXXConversionFunctionName:
2568 AddTypeRef(Name.getCXXNameType(), Record);
2569 break;
2570
2571 case DeclarationName::CXXOperatorName:
2572 Record.push_back(Name.getCXXOverloadedOperator());
2573 break;
2574
2575 case DeclarationName::CXXUsingDirective:
2576 // No extra data to emit
2577 break;
2578 }
2579}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002580
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002581/// \brief Write the given substatement or subexpression to the
2582/// bitstream.
2583void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002584 RecordData Record;
2585 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002586 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002587
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002588 if (!S) {
2589 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002590 return;
2591 }
2592
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002593 Writer.Code = pch::STMT_NULL_PTR;
2594 Writer.Visit(S);
2595 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002596 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002597 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002598}
2599
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002600/// \brief Flush all of the statements that have been added to the
2601/// queue via AddStmt().
2602void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002603 RecordData Record;
2604 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002605
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002606 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002607 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002608 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002609
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002610 if (!S) {
2611 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002612 continue;
2613 }
2614
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002615 Writer.Code = pch::STMT_NULL_PTR;
2616 Writer.Visit(S);
2617 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002618 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002619 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002620
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002621 assert(N == StmtsToEmit.size() &&
2622 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002623
2624 // Note that we are at the end of a full expression. Any
2625 // expression records that follow this one are part of a different
2626 // expression.
2627 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002628 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002629 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002630
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002631 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002632 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002633}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002634
2635unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2636 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2637 "SwitchCase recorded twice");
2638 unsigned NextID = SwitchCaseIDs.size();
2639 SwitchCaseIDs[S] = NextID;
2640 return NextID;
2641}
2642
2643unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2644 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2645 "SwitchCase hasn't been seen yet");
2646 return SwitchCaseIDs[S];
2647}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002648
2649/// \brief Retrieve the ID for the given label statement, which may
2650/// or may not have been emitted yet.
2651unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2652 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2653 if (Pos != LabelIDs.end())
2654 return Pos->second;
2655
2656 unsigned NextID = LabelIDs.size();
2657 LabelIDs[S] = NextID;
2658 return NextID;
2659}