blob: a43e59961a5a74a1e3dde370fa25f1414763a4dd [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);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000675 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
676 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
677 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000678 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000679 void VisitObjCSuperExpr(ObjCSuperExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000680 };
681}
682
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000683void PCHStmtWriter::VisitStmt(Stmt *S) {
684}
685
686void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
687 VisitStmt(S);
688 Writer.AddSourceLocation(S->getSemiLoc(), Record);
689 Code = pch::STMT_NULL;
690}
691
692void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
693 VisitStmt(S);
694 Record.push_back(S->size());
695 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
696 CS != CSEnd; ++CS)
697 Writer.WriteSubStmt(*CS);
698 Writer.AddSourceLocation(S->getLBracLoc(), Record);
699 Writer.AddSourceLocation(S->getRBracLoc(), Record);
700 Code = pch::STMT_COMPOUND;
701}
702
703void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
704 VisitStmt(S);
705 Record.push_back(Writer.RecordSwitchCaseID(S));
706}
707
708void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
709 VisitSwitchCase(S);
710 Writer.WriteSubStmt(S->getLHS());
711 Writer.WriteSubStmt(S->getRHS());
712 Writer.WriteSubStmt(S->getSubStmt());
713 Writer.AddSourceLocation(S->getCaseLoc(), Record);
714 Code = pch::STMT_CASE;
715}
716
717void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
718 VisitSwitchCase(S);
719 Writer.WriteSubStmt(S->getSubStmt());
720 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
721 Code = pch::STMT_DEFAULT;
722}
723
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000724void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
725 VisitStmt(S);
726 Writer.AddIdentifierRef(S->getID(), Record);
727 Writer.WriteSubStmt(S->getSubStmt());
728 Writer.AddSourceLocation(S->getIdentLoc(), Record);
729 Record.push_back(Writer.GetLabelID(S));
730 Code = pch::STMT_LABEL;
731}
732
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000733void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
734 VisitStmt(S);
735 Writer.WriteSubStmt(S->getCond());
736 Writer.WriteSubStmt(S->getThen());
737 Writer.WriteSubStmt(S->getElse());
738 Writer.AddSourceLocation(S->getIfLoc(), Record);
739 Code = pch::STMT_IF;
740}
741
742void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
743 VisitStmt(S);
744 Writer.WriteSubStmt(S->getCond());
745 Writer.WriteSubStmt(S->getBody());
746 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
747 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
748 SC = SC->getNextSwitchCase())
749 Record.push_back(Writer.getSwitchCaseID(SC));
750 Code = pch::STMT_SWITCH;
751}
752
Douglas Gregora6b503f2009-04-17 00:16:09 +0000753void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
754 VisitStmt(S);
755 Writer.WriteSubStmt(S->getCond());
756 Writer.WriteSubStmt(S->getBody());
757 Writer.AddSourceLocation(S->getWhileLoc(), Record);
758 Code = pch::STMT_WHILE;
759}
760
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000761void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
762 VisitStmt(S);
763 Writer.WriteSubStmt(S->getCond());
764 Writer.WriteSubStmt(S->getBody());
765 Writer.AddSourceLocation(S->getDoLoc(), Record);
766 Code = pch::STMT_DO;
767}
768
769void PCHStmtWriter::VisitForStmt(ForStmt *S) {
770 VisitStmt(S);
771 Writer.WriteSubStmt(S->getInit());
772 Writer.WriteSubStmt(S->getCond());
773 Writer.WriteSubStmt(S->getInc());
774 Writer.WriteSubStmt(S->getBody());
775 Writer.AddSourceLocation(S->getForLoc(), Record);
776 Code = pch::STMT_FOR;
777}
778
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000779void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
780 VisitStmt(S);
781 Record.push_back(Writer.GetLabelID(S->getLabel()));
782 Writer.AddSourceLocation(S->getGotoLoc(), Record);
783 Writer.AddSourceLocation(S->getLabelLoc(), Record);
784 Code = pch::STMT_GOTO;
785}
786
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000787void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
788 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000789 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000790 Writer.WriteSubStmt(S->getTarget());
791 Code = pch::STMT_INDIRECT_GOTO;
792}
793
Douglas Gregora6b503f2009-04-17 00:16:09 +0000794void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
795 VisitStmt(S);
796 Writer.AddSourceLocation(S->getContinueLoc(), Record);
797 Code = pch::STMT_CONTINUE;
798}
799
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000800void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
801 VisitStmt(S);
802 Writer.AddSourceLocation(S->getBreakLoc(), Record);
803 Code = pch::STMT_BREAK;
804}
805
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000806void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
807 VisitStmt(S);
808 Writer.WriteSubStmt(S->getRetValue());
809 Writer.AddSourceLocation(S->getReturnLoc(), Record);
810 Code = pch::STMT_RETURN;
811}
812
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000813void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
814 VisitStmt(S);
815 Writer.AddSourceLocation(S->getStartLoc(), Record);
816 Writer.AddSourceLocation(S->getEndLoc(), Record);
817 DeclGroupRef DG = S->getDeclGroup();
818 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
819 Writer.AddDeclRef(*D, Record);
820 Code = pch::STMT_DECL;
821}
822
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000823void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
824 VisitStmt(S);
825 Record.push_back(S->getNumOutputs());
826 Record.push_back(S->getNumInputs());
827 Record.push_back(S->getNumClobbers());
828 Writer.AddSourceLocation(S->getAsmLoc(), Record);
829 Writer.AddSourceLocation(S->getRParenLoc(), Record);
830 Record.push_back(S->isVolatile());
831 Record.push_back(S->isSimple());
832 Writer.WriteSubStmt(S->getAsmString());
833
834 // Outputs
835 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
836 Writer.AddString(S->getOutputName(I), Record);
837 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
838 Writer.WriteSubStmt(S->getOutputExpr(I));
839 }
840
841 // Inputs
842 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
843 Writer.AddString(S->getInputName(I), Record);
844 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
845 Writer.WriteSubStmt(S->getInputExpr(I));
846 }
847
848 // Clobbers
849 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
850 Writer.WriteSubStmt(S->getClobber(I));
851
852 Code = pch::STMT_ASM;
853}
854
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000855void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000856 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000857 Writer.AddTypeRef(E->getType(), Record);
858 Record.push_back(E->isTypeDependent());
859 Record.push_back(E->isValueDependent());
860}
861
Douglas Gregore2f37202009-04-14 21:55:33 +0000862void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
863 VisitExpr(E);
864 Writer.AddSourceLocation(E->getLocation(), Record);
865 Record.push_back(E->getIdentType()); // FIXME: stable encoding
866 Code = pch::EXPR_PREDEFINED;
867}
868
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000869void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
870 VisitExpr(E);
871 Writer.AddDeclRef(E->getDecl(), Record);
872 Writer.AddSourceLocation(E->getLocation(), Record);
873 Code = pch::EXPR_DECL_REF;
874}
875
876void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
877 VisitExpr(E);
878 Writer.AddSourceLocation(E->getLocation(), Record);
879 Writer.AddAPInt(E->getValue(), Record);
880 Code = pch::EXPR_INTEGER_LITERAL;
881}
882
Douglas Gregore2f37202009-04-14 21:55:33 +0000883void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
884 VisitExpr(E);
885 Writer.AddAPFloat(E->getValue(), Record);
886 Record.push_back(E->isExact());
887 Writer.AddSourceLocation(E->getLocation(), Record);
888 Code = pch::EXPR_FLOATING_LITERAL;
889}
890
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000891void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
892 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000893 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000894 Code = pch::EXPR_IMAGINARY_LITERAL;
895}
896
Douglas Gregor596e0932009-04-15 16:35:07 +0000897void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
898 VisitExpr(E);
899 Record.push_back(E->getByteLength());
900 Record.push_back(E->getNumConcatenated());
901 Record.push_back(E->isWide());
902 // FIXME: String data should be stored as a blob at the end of the
903 // StringLiteral. However, we can't do so now because we have no
904 // provision for coping with abbreviations when we're jumping around
905 // the PCH file during deserialization.
906 Record.insert(Record.end(),
907 E->getStrData(), E->getStrData() + E->getByteLength());
908 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
909 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
910 Code = pch::EXPR_STRING_LITERAL;
911}
912
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000913void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
914 VisitExpr(E);
915 Record.push_back(E->getValue());
916 Writer.AddSourceLocation(E->getLoc(), Record);
917 Record.push_back(E->isWide());
918 Code = pch::EXPR_CHARACTER_LITERAL;
919}
920
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000921void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
922 VisitExpr(E);
923 Writer.AddSourceLocation(E->getLParen(), Record);
924 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000925 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000926 Code = pch::EXPR_PAREN;
927}
928
Douglas Gregor12d74052009-04-15 15:58:59 +0000929void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
930 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000931 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000932 Record.push_back(E->getOpcode()); // FIXME: stable encoding
933 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
934 Code = pch::EXPR_UNARY_OPERATOR;
935}
936
937void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
938 VisitExpr(E);
939 Record.push_back(E->isSizeOf());
940 if (E->isArgumentType())
941 Writer.AddTypeRef(E->getArgumentType(), Record);
942 else {
943 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000944 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000945 }
946 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
947 Writer.AddSourceLocation(E->getRParenLoc(), Record);
948 Code = pch::EXPR_SIZEOF_ALIGN_OF;
949}
950
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000951void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
952 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000953 Writer.WriteSubStmt(E->getLHS());
954 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000955 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
956 Code = pch::EXPR_ARRAY_SUBSCRIPT;
957}
958
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000959void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
960 VisitExpr(E);
961 Record.push_back(E->getNumArgs());
962 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000963 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000964 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
965 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000966 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000967 Code = pch::EXPR_CALL;
968}
969
970void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
971 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000972 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000973 Writer.AddDeclRef(E->getMemberDecl(), Record);
974 Writer.AddSourceLocation(E->getMemberLoc(), Record);
975 Record.push_back(E->isArrow());
976 Code = pch::EXPR_MEMBER;
977}
978
Douglas Gregora151ba42009-04-14 23:32:43 +0000979void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
980 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000981 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000982}
983
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000984void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
985 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000986 Writer.WriteSubStmt(E->getLHS());
987 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000988 Record.push_back(E->getOpcode()); // FIXME: stable encoding
989 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
990 Code = pch::EXPR_BINARY_OPERATOR;
991}
992
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000993void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
994 VisitBinaryOperator(E);
995 Writer.AddTypeRef(E->getComputationLHSType(), Record);
996 Writer.AddTypeRef(E->getComputationResultType(), Record);
997 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
998}
999
1000void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
1001 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001002 Writer.WriteSubStmt(E->getCond());
1003 Writer.WriteSubStmt(E->getLHS());
1004 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001005 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1006}
1007
Douglas Gregora151ba42009-04-14 23:32:43 +00001008void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1009 VisitCastExpr(E);
1010 Record.push_back(E->isLvalueCast());
1011 Code = pch::EXPR_IMPLICIT_CAST;
1012}
1013
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001014void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1015 VisitCastExpr(E);
1016 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1017}
1018
1019void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1020 VisitExplicitCastExpr(E);
1021 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1022 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1023 Code = pch::EXPR_CSTYLE_CAST;
1024}
1025
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001026void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1027 VisitExpr(E);
1028 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001029 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001030 Record.push_back(E->isFileScope());
1031 Code = pch::EXPR_COMPOUND_LITERAL;
1032}
1033
Douglas Gregorec0b8292009-04-15 23:02:49 +00001034void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1035 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001036 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001037 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1038 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1039 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1040}
1041
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001042void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1043 VisitExpr(E);
1044 Record.push_back(E->getNumInits());
1045 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001046 Writer.WriteSubStmt(E->getInit(I));
1047 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001048 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1049 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1050 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1051 Record.push_back(E->hadArrayRangeDesignator());
1052 Code = pch::EXPR_INIT_LIST;
1053}
1054
1055void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1056 VisitExpr(E);
1057 Record.push_back(E->getNumSubExprs());
1058 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001059 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001060 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1061 Record.push_back(E->usesGNUSyntax());
1062 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1063 DEnd = E->designators_end();
1064 D != DEnd; ++D) {
1065 if (D->isFieldDesignator()) {
1066 if (FieldDecl *Field = D->getField()) {
1067 Record.push_back(pch::DESIG_FIELD_DECL);
1068 Writer.AddDeclRef(Field, Record);
1069 } else {
1070 Record.push_back(pch::DESIG_FIELD_NAME);
1071 Writer.AddIdentifierRef(D->getFieldName(), Record);
1072 }
1073 Writer.AddSourceLocation(D->getDotLoc(), Record);
1074 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1075 } else if (D->isArrayDesignator()) {
1076 Record.push_back(pch::DESIG_ARRAY);
1077 Record.push_back(D->getFirstExprIndex());
1078 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1079 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1080 } else {
1081 assert(D->isArrayRangeDesignator() && "Unknown designator");
1082 Record.push_back(pch::DESIG_ARRAY_RANGE);
1083 Record.push_back(D->getFirstExprIndex());
1084 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1085 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1086 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1087 }
1088 }
1089 Code = pch::EXPR_DESIGNATED_INIT;
1090}
1091
1092void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1093 VisitExpr(E);
1094 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1095}
1096
Douglas Gregorec0b8292009-04-15 23:02:49 +00001097void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1098 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001099 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001100 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1101 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1102 Code = pch::EXPR_VA_ARG;
1103}
1104
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001105void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1106 VisitExpr(E);
1107 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1108 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1109 Record.push_back(Writer.GetLabelID(E->getLabel()));
1110 Code = pch::EXPR_ADDR_LABEL;
1111}
1112
Douglas Gregoreca12f62009-04-17 19:05:30 +00001113void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1114 VisitExpr(E);
1115 Writer.WriteSubStmt(E->getSubStmt());
1116 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1117 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1118 Code = pch::EXPR_STMT;
1119}
1120
Douglas Gregor209d4622009-04-15 23:33:31 +00001121void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1122 VisitExpr(E);
1123 Writer.AddTypeRef(E->getArgType1(), Record);
1124 Writer.AddTypeRef(E->getArgType2(), Record);
1125 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1126 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1127 Code = pch::EXPR_TYPES_COMPATIBLE;
1128}
1129
1130void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1131 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001132 Writer.WriteSubStmt(E->getCond());
1133 Writer.WriteSubStmt(E->getLHS());
1134 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001135 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1136 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1137 Code = pch::EXPR_CHOOSE;
1138}
1139
1140void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1141 VisitExpr(E);
1142 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1143 Code = pch::EXPR_GNU_NULL;
1144}
1145
Douglas Gregor725e94b2009-04-16 00:01:45 +00001146void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1147 VisitExpr(E);
1148 Record.push_back(E->getNumSubExprs());
1149 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001150 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001151 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1152 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1153 Code = pch::EXPR_SHUFFLE_VECTOR;
1154}
1155
Douglas Gregore246b742009-04-17 19:21:43 +00001156void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1157 VisitExpr(E);
1158 Writer.AddDeclRef(E->getBlockDecl(), Record);
1159 Record.push_back(E->hasBlockDeclRefExprs());
1160 Code = pch::EXPR_BLOCK;
1161}
1162
Douglas Gregor725e94b2009-04-16 00:01:45 +00001163void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1164 VisitExpr(E);
1165 Writer.AddDeclRef(E->getDecl(), Record);
1166 Writer.AddSourceLocation(E->getLocation(), Record);
1167 Record.push_back(E->isByRef());
1168 Code = pch::EXPR_BLOCK_DECL_REF;
1169}
1170
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001171//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001172// Objective-C Expressions and Statements.
1173//===----------------------------------------------------------------------===//
1174
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001175void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1176 VisitExpr(E);
1177 Writer.WriteSubStmt(E->getString());
1178 Writer.AddSourceLocation(E->getAtLoc(), Record);
1179 Code = pch::EXPR_OBJC_STRING_LITERAL;
1180}
1181
Chris Lattner80f83c62009-04-22 05:57:30 +00001182void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1183 VisitExpr(E);
1184 Writer.AddTypeRef(E->getEncodedType(), Record);
1185 Writer.AddSourceLocation(E->getAtLoc(), Record);
1186 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1187 Code = pch::EXPR_OBJC_ENCODE;
1188}
1189
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001190void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1191 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001192 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001193 Writer.AddSourceLocation(E->getAtLoc(), Record);
1194 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1195 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1196}
1197
1198void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1199 VisitExpr(E);
1200 Writer.AddDeclRef(E->getProtocol(), Record);
1201 Writer.AddSourceLocation(E->getAtLoc(), Record);
1202 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1203 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1204}
1205
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001206void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1207 VisitExpr(E);
1208 Writer.AddDeclRef(E->getDecl(), Record);
1209 Writer.AddSourceLocation(E->getLocation(), Record);
1210 Writer.WriteSubStmt(E->getBase());
1211 Record.push_back(E->isArrow());
1212 Record.push_back(E->isFreeIvar());
1213}
1214
1215void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1216 VisitExpr(E);
1217 Writer.AddDeclRef(E->getProperty(), Record);
1218 Writer.AddSourceLocation(E->getLocation(), Record);
1219 Writer.WriteSubStmt(E->getBase());
1220}
1221
1222void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1223 VisitExpr(E);
1224 Writer.AddDeclRef(E->getGetterMethod(), Record);
1225 Writer.AddDeclRef(E->getSetterMethod(), Record);
1226
1227 // NOTE: ClassProp and Base are mutually exclusive.
1228 Writer.AddDeclRef(E->getClassProp(), Record);
1229 Writer.WriteSubStmt(E->getBase());
1230 Writer.AddSourceLocation(E->getLocation(), Record);
1231 Writer.AddSourceLocation(E->getClassLoc(), Record);
1232}
1233
Steve Narofffb3e4022009-04-25 14:04:28 +00001234void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1235 VisitExpr(E);
1236 Record.push_back(E->getNumArgs());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001237 Writer.AddSourceLocation(E->getLeftLoc(), Record);
1238 Writer.AddSourceLocation(E->getRightLoc(), Record);
Steve Narofffb3e4022009-04-25 14:04:28 +00001239 Writer.AddSelectorRef(E->getSelector(), Record);
1240 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001241
1242 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
Steve Narofffb3e4022009-04-25 14:04:28 +00001243 Writer.WriteSubStmt(E->getReceiver());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001244 Writer.AddDeclRef(CI.first, Record);
1245 Writer.AddIdentifierRef(CI.second, Record);
1246
Steve Narofffb3e4022009-04-25 14:04:28 +00001247 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1248 Arg != ArgEnd; ++Arg)
1249 Writer.WriteSubStmt(*Arg);
1250 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1251}
1252
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001253void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1254 VisitExpr(E);
1255 Writer.AddSourceLocation(E->getLoc(), Record);
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001256}
1257
Chris Lattner80f83c62009-04-22 05:57:30 +00001258
1259//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001260// PCHWriter Implementation
1261//===----------------------------------------------------------------------===//
1262
Douglas Gregorb5887f32009-04-10 21:16:55 +00001263/// \brief Write the target triple (e.g., i686-apple-darwin9).
1264void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1265 using namespace llvm;
1266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1267 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001269 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001270
1271 RecordData Record;
1272 Record.push_back(pch::TARGET_TRIPLE);
1273 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001274 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001275}
1276
1277/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001278void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1279 RecordData Record;
1280 Record.push_back(LangOpts.Trigraphs);
1281 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1282 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1283 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1284 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1285 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1286 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1287 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1288 Record.push_back(LangOpts.C99); // C99 Support
1289 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1290 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1291 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1292 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1293 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1294
1295 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1296 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1297 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1298
1299 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1300 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1301 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1302 Record.push_back(LangOpts.LaxVectorConversions);
1303 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1304
1305 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1306 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1307 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1308
1309 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1310 // by locks.
1311 Record.push_back(LangOpts.Blocks); // block extension to C
1312 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1313 // they are unused.
1314 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1315 // (modulo the platform support).
1316
1317 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1318 // signed integer arithmetic overflows.
1319
1320 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1321 // may be ripped out at any time.
1322
1323 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1324 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1325 // defined.
1326 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1327 // opposed to __DYNAMIC__).
1328 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1329
1330 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1331 // used (instead of C99 semantics).
1332 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1333 Record.push_back(LangOpts.getGCMode());
1334 Record.push_back(LangOpts.getVisibilityMode());
1335 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001336 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001337}
1338
Douglas Gregorab1cef72009-04-10 03:52:48 +00001339//===----------------------------------------------------------------------===//
1340// Source Manager Serialization
1341//===----------------------------------------------------------------------===//
1342
1343/// \brief Create an abbreviation for the SLocEntry that refers to a
1344/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001345static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001346 using namespace llvm;
1347 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1348 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1349 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001354 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001355}
1356
1357/// \brief Create an abbreviation for the SLocEntry that refers to a
1358/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001359static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001360 using namespace llvm;
1361 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1362 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001368 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001369}
1370
1371/// \brief Create an abbreviation for the SLocEntry that refers to a
1372/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001373static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001374 using namespace llvm;
1375 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1376 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001378 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001379}
1380
1381/// \brief Create an abbreviation for the SLocEntry that refers to an
1382/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001383static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001384 using namespace llvm;
1385 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1386 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001392 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001393}
1394
1395/// \brief Writes the block containing the serialized form of the
1396/// source manager.
1397///
1398/// TODO: We should probably use an on-disk hash table (stored in a
1399/// blob), indexed based on the file name, so that we only create
1400/// entries for files that we actually need. In the common case (no
1401/// errors), we probably won't have to create file entries for any of
1402/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001403void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1404 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001405 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001406 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001407
1408 // Abbreviations for the various kinds of source-location entries.
1409 int SLocFileAbbrv = -1;
1410 int SLocBufferAbbrv = -1;
1411 int SLocBufferBlobAbbrv = -1;
1412 int SLocInstantiationAbbrv = -1;
1413
1414 // Write out the source location entry table. We skip the first
1415 // entry, which is always the same dummy entry.
1416 RecordData Record;
1417 for (SourceManager::sloc_entry_iterator
1418 SLoc = SourceMgr.sloc_entry_begin() + 1,
1419 SLocEnd = SourceMgr.sloc_entry_end();
1420 SLoc != SLocEnd; ++SLoc) {
1421 // Figure out which record code to use.
1422 unsigned Code;
1423 if (SLoc->isFile()) {
1424 if (SLoc->getFile().getContentCache()->Entry)
1425 Code = pch::SM_SLOC_FILE_ENTRY;
1426 else
1427 Code = pch::SM_SLOC_BUFFER_ENTRY;
1428 } else
1429 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1430 Record.push_back(Code);
1431
1432 Record.push_back(SLoc->getOffset());
1433 if (SLoc->isFile()) {
1434 const SrcMgr::FileInfo &File = SLoc->getFile();
1435 Record.push_back(File.getIncludeLoc().getRawEncoding());
1436 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001437 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001438
1439 const SrcMgr::ContentCache *Content = File.getContentCache();
1440 if (Content->Entry) {
1441 // The source location entry is a file. The blob associated
1442 // with this entry is the file name.
1443 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001444 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1445 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001446 Content->Entry->getName(),
1447 strlen(Content->Entry->getName()));
1448 } else {
1449 // The source location entry is a buffer. The blob associated
1450 // with this entry contains the contents of the buffer.
1451 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001452 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1453 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001454 }
1455
1456 // We add one to the size so that we capture the trailing NULL
1457 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1458 // the reader side).
1459 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1460 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001461 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001462 Record.clear();
1463 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001464 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001465 Buffer->getBufferStart(),
1466 Buffer->getBufferSize() + 1);
1467 }
1468 } else {
1469 // The source location entry is an instantiation.
1470 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1471 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1472 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1473 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1474
Douglas Gregor364e5802009-04-15 18:05:10 +00001475 // Compute the token length for this macro expansion.
1476 unsigned NextOffset = SourceMgr.getNextOffset();
1477 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1478 if (++NextSLoc != SLocEnd)
1479 NextOffset = NextSLoc->getOffset();
1480 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1481
Douglas Gregorab1cef72009-04-10 03:52:48 +00001482 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001483 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1484 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001485 }
1486
1487 Record.clear();
1488 }
1489
Douglas Gregor635f97f2009-04-13 16:31:14 +00001490 // Write the line table.
1491 if (SourceMgr.hasLineTable()) {
1492 LineTableInfo &LineTable = SourceMgr.getLineTable();
1493
1494 // Emit the file names
1495 Record.push_back(LineTable.getNumFilenames());
1496 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1497 // Emit the file name
1498 const char *Filename = LineTable.getFilename(I);
1499 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1500 Record.push_back(FilenameLen);
1501 if (FilenameLen)
1502 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1503 }
1504
1505 // Emit the line entries
1506 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1507 L != LEnd; ++L) {
1508 // Emit the file ID
1509 Record.push_back(L->first);
1510
1511 // Emit the line entries
1512 Record.push_back(L->second.size());
1513 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1514 LEEnd = L->second.end();
1515 LE != LEEnd; ++LE) {
1516 Record.push_back(LE->FileOffset);
1517 Record.push_back(LE->LineNo);
1518 Record.push_back(LE->FilenameID);
1519 Record.push_back((unsigned)LE->FileKind);
1520 Record.push_back(LE->IncludeOffset);
1521 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001522 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001523 }
1524 }
1525
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001526 // Loop over all the header files.
1527 HeaderSearch &HS = PP.getHeaderSearchInfo();
1528 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1529 E = HS.header_file_end();
1530 I != E; ++I) {
1531 Record.push_back(I->isImport);
1532 Record.push_back(I->DirInfo);
1533 Record.push_back(I->NumIncludes);
1534 if (I->ControllingMacro)
1535 AddIdentifierRef(I->ControllingMacro, Record);
1536 else
1537 Record.push_back(0);
1538 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1539 Record.clear();
1540 }
1541
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001542 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001543}
1544
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001545/// \brief Writes the block containing the serialized form of the
1546/// preprocessor.
1547///
Chris Lattner850eabd2009-04-10 18:08:30 +00001548void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001549 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001550
Chris Lattner4b21c202009-04-13 01:29:17 +00001551 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1552 if (PP.getCounterValue() != 0) {
1553 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001554 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001555 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001556 }
1557
1558 // Enter the preprocessor block.
1559 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001560
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001561 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1562 // FIXME: use diagnostics subsystem for localization etc.
1563 if (PP.SawDateOrTime())
1564 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1565
Chris Lattner1b094952009-04-10 18:00:12 +00001566 // Loop over all the macro definitions that are live at the end of the file,
1567 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001568 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1569 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001570 // FIXME: This emits macros in hash table order, we should do it in a stable
1571 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001572 MacroInfo *MI = I->second;
1573
1574 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1575 // been redefined by the header (in which case they are not isBuiltinMacro).
1576 if (MI->isBuiltinMacro())
1577 continue;
1578
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001579 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001580 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001581 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001582 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1583 Record.push_back(MI->isUsed());
1584
1585 unsigned Code;
1586 if (MI->isObjectLike()) {
1587 Code = pch::PP_MACRO_OBJECT_LIKE;
1588 } else {
1589 Code = pch::PP_MACRO_FUNCTION_LIKE;
1590
1591 Record.push_back(MI->isC99Varargs());
1592 Record.push_back(MI->isGNUVarargs());
1593 Record.push_back(MI->getNumArgs());
1594 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1595 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001596 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001597 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001598 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001599 Record.clear();
1600
Chris Lattner850eabd2009-04-10 18:08:30 +00001601 // Emit the tokens array.
1602 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1603 // Note that we know that the preprocessor does not have any annotation
1604 // tokens in it because they are created by the parser, and thus can't be
1605 // in a macro definition.
1606 const Token &Tok = MI->getReplacementToken(TokNo);
1607
1608 Record.push_back(Tok.getLocation().getRawEncoding());
1609 Record.push_back(Tok.getLength());
1610
Chris Lattner850eabd2009-04-10 18:08:30 +00001611 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1612 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001613 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001614
1615 // FIXME: Should translate token kind to a stable encoding.
1616 Record.push_back(Tok.getKind());
1617 // FIXME: Should translate token flags to a stable encoding.
1618 Record.push_back(Tok.getFlags());
1619
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001620 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001621 Record.clear();
1622 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001623 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001624 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001625 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001626}
1627
1628
Douglas Gregorc34897d2009-04-09 22:27:44 +00001629/// \brief Write the representation of a type to the PCH stream.
1630void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001631 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001632 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001633 ID = NextTypeID++;
1634
1635 // Record the offset for this type.
1636 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001637 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001638 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1639 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001640 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001641 }
1642
1643 RecordData Record;
1644
1645 // Emit the type's representation.
1646 PCHTypeWriter W(*this, Record);
1647 switch (T->getTypeClass()) {
1648 // For all of the concrete, non-dependent types, call the
1649 // appropriate visitor function.
1650#define TYPE(Class, Base) \
1651 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1652#define ABSTRACT_TYPE(Class, Base)
1653#define DEPENDENT_TYPE(Class, Base)
1654#include "clang/AST/TypeNodes.def"
1655
1656 // For all of the dependent type nodes (which only occur in C++
1657 // templates), produce an error.
1658#define TYPE(Class, Base)
1659#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1660#include "clang/AST/TypeNodes.def"
1661 assert(false && "Cannot serialize dependent type nodes");
1662 break;
1663 }
1664
1665 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001666 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001667
1668 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001669 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001670}
1671
1672/// \brief Write a block containing all of the types.
1673void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001674 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001675 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001676
Douglas Gregore43f0972009-04-26 03:49:13 +00001677 // Emit all of the types that need to be emitted (so far).
1678 while (!TypesToEmit.empty()) {
1679 const Type *T = TypesToEmit.front();
1680 TypesToEmit.pop();
1681 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1682 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001683 }
1684
1685 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001686 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687}
1688
1689/// \brief Write the block containing all of the declaration IDs
1690/// lexically declared within the given DeclContext.
1691///
1692/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1693/// bistream, or 0 if no block was written.
1694uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1695 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001696 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001697 return 0;
1698
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001699 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001700 RecordData Record;
1701 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1702 DEnd = DC->decls_end(Context);
1703 D != DEnd; ++D)
1704 AddDeclRef(*D, Record);
1705
Douglas Gregoraf136d92009-04-22 22:34:57 +00001706 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001707 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001708 return Offset;
1709}
1710
1711/// \brief Write the block containing all of the declaration IDs
1712/// visible from the given DeclContext.
1713///
1714/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1715/// bistream, or 0 if no block was written.
1716uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1717 DeclContext *DC) {
1718 if (DC->getPrimaryContext() != DC)
1719 return 0;
1720
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001721 // Since there is no name lookup into functions or methods, and we
1722 // perform name lookup for the translation unit via the
1723 // IdentifierInfo chains, don't bother to build a
1724 // visible-declarations table for these entities.
1725 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001726 return 0;
1727
Douglas Gregorc34897d2009-04-09 22:27:44 +00001728 // Force the DeclContext to build a its name-lookup table.
1729 DC->lookup(Context, DeclarationName());
1730
1731 // Serialize the contents of the mapping used for lookup. Note that,
1732 // although we have two very different code paths, the serialized
1733 // representation is the same for both cases: a declaration name,
1734 // followed by a size, followed by references to the visible
1735 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001736 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001737 RecordData Record;
1738 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001739 if (!Map)
1740 return 0;
1741
Douglas Gregorc34897d2009-04-09 22:27:44 +00001742 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1743 D != DEnd; ++D) {
1744 AddDeclarationName(D->first, Record);
1745 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1746 Record.push_back(Result.second - Result.first);
1747 for(; Result.first != Result.second; ++Result.first)
1748 AddDeclRef(*Result.first, Record);
1749 }
1750
1751 if (Record.size() == 0)
1752 return 0;
1753
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001754 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001755 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001756 return Offset;
1757}
1758
1759/// \brief Write a block containing all of the declarations.
1760void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001761 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001762 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001763
1764 // Emit all of the declarations.
1765 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001766 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001767 while (!DeclsToEmit.empty()) {
1768 // Pull the next declaration off the queue
1769 Decl *D = DeclsToEmit.front();
1770 DeclsToEmit.pop();
1771
1772 // If this declaration is also a DeclContext, write blocks for the
1773 // declarations that lexically stored inside its context and those
1774 // declarations that are visible from its context. These blocks
1775 // are written before the declaration itself so that we can put
1776 // their offsets into the record for the declaration.
1777 uint64_t LexicalOffset = 0;
1778 uint64_t VisibleOffset = 0;
1779 DeclContext *DC = dyn_cast<DeclContext>(D);
1780 if (DC) {
1781 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1782 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1783 }
1784
1785 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001786 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001787 if (ID == 0)
1788 ID = DeclIDs.size();
1789
1790 unsigned Index = ID - 1;
1791
1792 // Record the offset for this declaration
1793 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001794 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001795 else if (DeclOffsets.size() < Index) {
1796 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001797 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001798 }
1799
1800 // Build and emit a record for this declaration
1801 Record.clear();
1802 W.Code = (pch::DeclCode)0;
1803 W.Visit(D);
1804 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001805
1806 if (!W.Code) {
1807 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1808 D->getDeclKindName());
1809 assert(false && "Unhandled declaration kind while generating PCH");
1810 exit(-1);
1811 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001812 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001813
Douglas Gregor1c507882009-04-15 21:30:51 +00001814 // If the declaration had any attributes, write them now.
1815 if (D->hasAttrs())
1816 WriteAttributeRecord(D->getAttrs());
1817
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001818 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001819 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001820
Douglas Gregor631f6c62009-04-14 00:24:19 +00001821 // Note external declarations so that we can add them to a record
1822 // in the PCH file later.
1823 if (isa<FileScopeAsmDecl>(D))
1824 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001825 }
1826
1827 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001828 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001829}
1830
Douglas Gregorff9a6092009-04-20 20:36:09 +00001831namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001832// Trait used for the on-disk hash table used in the method pool.
1833class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1834 PCHWriter &Writer;
1835
1836public:
1837 typedef Selector key_type;
1838 typedef key_type key_type_ref;
1839
1840 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1841 typedef const data_type& data_type_ref;
1842
1843 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1844
1845 static unsigned ComputeHash(Selector Sel) {
1846 unsigned N = Sel.getNumArgs();
1847 if (N == 0)
1848 ++N;
1849 unsigned R = 5381;
1850 for (unsigned I = 0; I != N; ++I)
1851 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1852 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1853 return R;
1854 }
1855
1856 std::pair<unsigned,unsigned>
1857 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1858 data_type_ref Methods) {
1859 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1860 clang::io::Emit16(Out, KeyLen);
1861 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1862 for (const ObjCMethodList *Method = &Methods.first; Method;
1863 Method = Method->Next)
1864 if (Method->Method)
1865 DataLen += 4;
1866 for (const ObjCMethodList *Method = &Methods.second; Method;
1867 Method = Method->Next)
1868 if (Method->Method)
1869 DataLen += 4;
1870 clang::io::Emit16(Out, DataLen);
1871 return std::make_pair(KeyLen, DataLen);
1872 }
1873
Douglas Gregor2d711832009-04-25 17:48:32 +00001874 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1875 uint64_t Start = Out.tell();
1876 assert((Start >> 32) == 0 && "Selector key offset too large");
1877 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001878 unsigned N = Sel.getNumArgs();
1879 clang::io::Emit16(Out, N);
1880 if (N == 0)
1881 N = 1;
1882 for (unsigned I = 0; I != N; ++I)
1883 clang::io::Emit32(Out,
1884 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1885 }
1886
1887 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001888 data_type_ref Methods, unsigned DataLen) {
1889 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001890 unsigned NumInstanceMethods = 0;
1891 for (const ObjCMethodList *Method = &Methods.first; Method;
1892 Method = Method->Next)
1893 if (Method->Method)
1894 ++NumInstanceMethods;
1895
1896 unsigned NumFactoryMethods = 0;
1897 for (const ObjCMethodList *Method = &Methods.second; Method;
1898 Method = Method->Next)
1899 if (Method->Method)
1900 ++NumFactoryMethods;
1901
1902 clang::io::Emit16(Out, NumInstanceMethods);
1903 clang::io::Emit16(Out, NumFactoryMethods);
1904 for (const ObjCMethodList *Method = &Methods.first; Method;
1905 Method = Method->Next)
1906 if (Method->Method)
1907 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001908 for (const ObjCMethodList *Method = &Methods.second; Method;
1909 Method = Method->Next)
1910 if (Method->Method)
1911 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001912
1913 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001914 }
1915};
1916} // end anonymous namespace
1917
1918/// \brief Write the method pool into the PCH file.
1919///
1920/// The method pool contains both instance and factory methods, stored
1921/// in an on-disk hash table indexed by the selector.
1922void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1923 using namespace llvm;
1924
1925 // Create and write out the blob that contains the instance and
1926 // factor method pools.
1927 bool Empty = true;
1928 {
1929 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1930
1931 // Create the on-disk hash table representation. Start by
1932 // iterating through the instance method pool.
1933 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001934 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001935 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1936 Instance = SemaRef.InstanceMethodPool.begin(),
1937 InstanceEnd = SemaRef.InstanceMethodPool.end();
1938 Instance != InstanceEnd; ++Instance) {
1939 // Check whether there is a factory method with the same
1940 // selector.
1941 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1942 = SemaRef.FactoryMethodPool.find(Instance->first);
1943
1944 if (Factory == SemaRef.FactoryMethodPool.end())
1945 Generator.insert(Instance->first,
1946 std::make_pair(Instance->second,
1947 ObjCMethodList()));
1948 else
1949 Generator.insert(Instance->first,
1950 std::make_pair(Instance->second, Factory->second));
1951
Douglas Gregor2d711832009-04-25 17:48:32 +00001952 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001953 Empty = false;
1954 }
1955
1956 // Now iterate through the factory method pool, to pick up any
1957 // selectors that weren't already in the instance method pool.
1958 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1959 Factory = SemaRef.FactoryMethodPool.begin(),
1960 FactoryEnd = SemaRef.FactoryMethodPool.end();
1961 Factory != FactoryEnd; ++Factory) {
1962 // Check whether there is an instance method with the same
1963 // selector. If so, there is no work to do here.
1964 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1965 = SemaRef.InstanceMethodPool.find(Factory->first);
1966
Douglas Gregor2d711832009-04-25 17:48:32 +00001967 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001968 Generator.insert(Factory->first,
1969 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001970 ++NumSelectorsInMethodPool;
1971 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001972
1973 Empty = false;
1974 }
1975
Douglas Gregor2d711832009-04-25 17:48:32 +00001976 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001977 return;
1978
1979 // Create the on-disk hash table in a buffer.
1980 llvm::SmallVector<char, 4096> MethodPool;
1981 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001982 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001983 {
1984 PCHMethodPoolTrait Trait(*this);
1985 llvm::raw_svector_ostream Out(MethodPool);
1986 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001987 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001988 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001989
1990 // For every selector that we have seen but which was not
1991 // written into the hash table, write the selector itself and
1992 // record it's offset.
1993 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1994 if (SelectorOffsets[I] == 0)
1995 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001996 }
1997
1998 // Create a blob abbreviation
1999 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2000 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2004 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2005
Douglas Gregor2d711832009-04-25 17:48:32 +00002006 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002007 RecordData Record;
2008 Record.push_back(pch::METHOD_POOL);
2009 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002010 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002011 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2012 &MethodPool.front(),
2013 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002014
2015 // Create a blob abbreviation for the selector table offsets.
2016 Abbrev = new BitCodeAbbrev();
2017 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2020 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2021
2022 // Write the selector offsets table.
2023 Record.clear();
2024 Record.push_back(pch::SELECTOR_OFFSETS);
2025 Record.push_back(SelectorOffsets.size());
2026 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2027 (const char *)&SelectorOffsets.front(),
2028 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002029 }
2030}
2031
2032namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002033class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2034 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002035 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002036
2037public:
2038 typedef const IdentifierInfo* key_type;
2039 typedef key_type key_type_ref;
2040
2041 typedef pch::IdentID data_type;
2042 typedef data_type data_type_ref;
2043
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002044 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2045 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002046
2047 static unsigned ComputeHash(const IdentifierInfo* II) {
2048 return clang::BernsteinHash(II->getName());
2049 }
2050
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002051 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002052 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2053 pch::IdentID ID) {
2054 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002055 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2056 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002057 if (II->hasMacroDefinition() &&
2058 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2059 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002060 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2061 DEnd = IdentifierResolver::end();
2062 D != DEnd; ++D)
2063 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002064 // We emit the key length after the data length so that the
2065 // "uninteresting" identifiers following the identifier hash table
2066 // structure will have the same (key length, key characters)
2067 // layout as the keys in the hash table. This also matches the
2068 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002069 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002070 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002071 return std::make_pair(KeyLen, DataLen);
2072 }
2073
2074 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2075 unsigned KeyLen) {
2076 // Record the location of the key data. This is used when generating
2077 // the mapping from persistent IDs to strings.
2078 Writer.SetIdentifierOffset(II, Out.tell());
2079 Out.write(II->getName(), KeyLen);
2080 }
2081
2082 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2083 pch::IdentID ID, unsigned) {
2084 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002085 bool hasMacroDefinition =
2086 II->hasMacroDefinition() &&
2087 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002088 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002089 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2090 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002091 Bits = (Bits << 1) | II->isExtensionToken();
2092 Bits = (Bits << 1) | II->isPoisoned();
2093 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2094 clang::io::Emit32(Out, Bits);
2095 clang::io::Emit32(Out, ID);
2096
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002097 if (hasMacroDefinition)
2098 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2099
Douglas Gregorc713da92009-04-21 22:25:48 +00002100 // Emit the declaration IDs in reverse order, because the
2101 // IdentifierResolver provides the declarations as they would be
2102 // visible (e.g., the function "stat" would come before the struct
2103 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2104 // adds declarations to the end of the list (so we need to see the
2105 // struct "status" before the function "status").
2106 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2107 IdentifierResolver::end());
2108 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2109 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002110 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002111 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002112 }
2113};
2114} // end anonymous namespace
2115
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002116/// \brief Write the identifier table into the PCH file.
2117///
2118/// The identifier table consists of a blob containing string data
2119/// (the actual identifiers themselves) and a separate "offsets" index
2120/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002121void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002122 using namespace llvm;
2123
2124 // Create and write out the blob that contains the identifier
2125 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002126 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002127 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002128 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2129
Douglas Gregor85c4a872009-04-25 21:04:17 +00002130 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2131
Douglas Gregorff9a6092009-04-20 20:36:09 +00002132 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002133 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2134 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2135 ID != IDEnd; ++ID) {
2136 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002137
2138 // Classify each identifier as either "interesting" or "not
2139 // interesting". Interesting identifiers are those that have
2140 // additional information that needs to be read from the PCH
2141 // file, e.g., a built-in ID, declaration chain, or macro
2142 // definition. These identifiers are placed into the hash table
2143 // so that they can be found when looked up in the user program.
2144 // All other identifiers are "uninteresting", which means that
2145 // the IdentifierInfo built by default has all of the
2146 // information we care about. Such identifiers are placed after
2147 // the hash table.
2148 const IdentifierInfo *II = ID->first;
2149 if (II->isPoisoned() ||
2150 II->isExtensionToken() ||
2151 II->hasMacroDefinition() ||
2152 II->getObjCOrBuiltinID() ||
2153 II->getFETokenInfo<void>())
2154 Generator.insert(ID->first, ID->second);
2155 else
2156 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002157 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002158
Douglas Gregorff9a6092009-04-20 20:36:09 +00002159 // Create the on-disk hash table in a buffer.
2160 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002161 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002162 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002163 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002164 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002165 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002166 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002167 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002168
2169 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2170 const IdentifierInfo *II = UninterestingIdentifiers[I];
2171 unsigned N = II->getLength() + 1;
2172 clang::io::Emit16(Out, N);
2173 SetIdentifierOffset(II, Out.tell());
2174 Out.write(II->getName(), N);
2175 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002176 }
2177
2178 // Create a blob abbreviation
2179 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2180 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002183 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002184
2185 // Write the identifier table
2186 RecordData Record;
2187 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002188 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002189 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2190 &IdentifierTable.front(),
2191 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002192 }
2193
2194 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2196 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2199 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2200
2201 RecordData Record;
2202 Record.push_back(pch::IDENTIFIER_OFFSET);
2203 Record.push_back(IdentifierOffsets.size());
2204 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2205 (const char *)&IdentifierOffsets.front(),
2206 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002207}
2208
Douglas Gregor1c507882009-04-15 21:30:51 +00002209/// \brief Write a record containing the given attributes.
2210void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2211 RecordData Record;
2212 for (; Attr; Attr = Attr->getNext()) {
2213 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2214 Record.push_back(Attr->isInherited());
2215 switch (Attr->getKind()) {
2216 case Attr::Alias:
2217 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2218 break;
2219
2220 case Attr::Aligned:
2221 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2222 break;
2223
2224 case Attr::AlwaysInline:
2225 break;
2226
2227 case Attr::AnalyzerNoReturn:
2228 break;
2229
2230 case Attr::Annotate:
2231 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2232 break;
2233
2234 case Attr::AsmLabel:
2235 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2236 break;
2237
2238 case Attr::Blocks:
2239 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2240 break;
2241
2242 case Attr::Cleanup:
2243 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2244 break;
2245
2246 case Attr::Const:
2247 break;
2248
2249 case Attr::Constructor:
2250 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2251 break;
2252
2253 case Attr::DLLExport:
2254 case Attr::DLLImport:
2255 case Attr::Deprecated:
2256 break;
2257
2258 case Attr::Destructor:
2259 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2260 break;
2261
2262 case Attr::FastCall:
2263 break;
2264
2265 case Attr::Format: {
2266 const FormatAttr *Format = cast<FormatAttr>(Attr);
2267 AddString(Format->getType(), Record);
2268 Record.push_back(Format->getFormatIdx());
2269 Record.push_back(Format->getFirstArg());
2270 break;
2271 }
2272
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002273 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002274 case Attr::IBOutletKind:
2275 case Attr::NoReturn:
2276 case Attr::NoThrow:
2277 case Attr::Nodebug:
2278 case Attr::Noinline:
2279 break;
2280
2281 case Attr::NonNull: {
2282 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2283 Record.push_back(NonNull->size());
2284 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2285 break;
2286 }
2287
2288 case Attr::ObjCException:
2289 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002290 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002291 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002292 case Attr::Overloadable:
2293 break;
2294
2295 case Attr::Packed:
2296 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2297 break;
2298
2299 case Attr::Pure:
2300 break;
2301
2302 case Attr::Regparm:
2303 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2304 break;
2305
2306 case Attr::Section:
2307 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2308 break;
2309
2310 case Attr::StdCall:
2311 case Attr::TransparentUnion:
2312 case Attr::Unavailable:
2313 case Attr::Unused:
2314 case Attr::Used:
2315 break;
2316
2317 case Attr::Visibility:
2318 // FIXME: stable encoding
2319 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2320 break;
2321
2322 case Attr::WarnUnusedResult:
2323 case Attr::Weak:
2324 case Attr::WeakImport:
2325 break;
2326 }
2327 }
2328
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002329 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002330}
2331
2332void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2333 Record.push_back(Str.size());
2334 Record.insert(Record.end(), Str.begin(), Str.end());
2335}
2336
Douglas Gregorff9a6092009-04-20 20:36:09 +00002337/// \brief Note that the identifier II occurs at the given offset
2338/// within the identifier table.
2339void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002340 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002341}
2342
Douglas Gregor2d711832009-04-25 17:48:32 +00002343/// \brief Note that the selector Sel occurs at the given offset
2344/// within the method pool/selector table.
2345void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2346 unsigned ID = SelectorIDs[Sel];
2347 assert(ID && "Unknown selector");
2348 SelectorOffsets[ID - 1] = Offset;
2349}
2350
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002351PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002352 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002353 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2354 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002355
Douglas Gregor87887da2009-04-20 15:53:59 +00002356void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002357 using namespace llvm;
2358
Douglas Gregor87887da2009-04-20 15:53:59 +00002359 ASTContext &Context = SemaRef.Context;
2360 Preprocessor &PP = SemaRef.PP;
2361
Douglas Gregorc34897d2009-04-09 22:27:44 +00002362 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002363 Stream.Emit((unsigned)'C', 8);
2364 Stream.Emit((unsigned)'P', 8);
2365 Stream.Emit((unsigned)'C', 8);
2366 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002367
2368 // The translation unit is the first declaration we'll emit.
2369 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2370 DeclsToEmit.push(Context.getTranslationUnitDecl());
2371
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002372 // Make sure that we emit IdentifierInfos (and any attached
2373 // declarations) for builtins.
2374 {
2375 IdentifierTable &Table = PP.getIdentifierTable();
2376 llvm::SmallVector<const char *, 32> BuiltinNames;
2377 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2378 Context.getLangOptions().NoBuiltin);
2379 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2380 getIdentifierRef(&Table.get(BuiltinNames[I]));
2381 }
2382
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002383 // Build a record containing all of the tentative definitions in
2384 // this header file. Generally, this record will be empty.
2385 RecordData TentativeDefinitions;
2386 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2387 TD = SemaRef.TentativeDefinitions.begin(),
2388 TDEnd = SemaRef.TentativeDefinitions.end();
2389 TD != TDEnd; ++TD)
2390 AddDeclRef(TD->second, TentativeDefinitions);
2391
Douglas Gregor062d9482009-04-22 22:18:58 +00002392 // Build a record containing all of the locally-scoped external
2393 // declarations in this header file. Generally, this record will be
2394 // empty.
2395 RecordData LocallyScopedExternalDecls;
2396 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2397 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2398 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2399 TD != TDEnd; ++TD)
2400 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2401
Douglas Gregorc34897d2009-04-09 22:27:44 +00002402 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002403 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002404 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002405 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002406 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002407 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002408 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002409
2410 // Keep writing types and declarations until all types and
2411 // declarations have been written.
2412 do {
2413 if (!DeclsToEmit.empty())
2414 WriteDeclsBlock(Context);
2415 if (!TypesToEmit.empty())
2416 WriteTypesBlock(Context);
2417 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2418
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002419 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002420 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002421
2422 // Write the type offsets array
2423 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2424 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2427 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2428 Record.clear();
2429 Record.push_back(pch::TYPE_OFFSET);
2430 Record.push_back(TypeOffsets.size());
2431 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2432 (const char *)&TypeOffsets.front(),
2433 TypeOffsets.size() * sizeof(uint64_t));
2434
2435 // Write the declaration offsets array
2436 Abbrev = new BitCodeAbbrev();
2437 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2440 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2441 Record.clear();
2442 Record.push_back(pch::DECL_OFFSET);
2443 Record.push_back(DeclOffsets.size());
2444 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2445 (const char *)&DeclOffsets.front(),
2446 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002447
2448 // Write the record of special types.
2449 Record.clear();
2450 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002451 AddTypeRef(Context.getObjCIdType(), Record);
2452 AddTypeRef(Context.getObjCSelType(), Record);
2453 AddTypeRef(Context.getObjCProtoType(), Record);
2454 AddTypeRef(Context.getObjCClassType(), Record);
2455 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2456 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002457 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2458
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002459 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002460 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002461 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002462
2463 // Write the record containing tentative definitions.
2464 if (!TentativeDefinitions.empty())
2465 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002466
2467 // Write the record containing locally-scoped external definitions.
2468 if (!LocallyScopedExternalDecls.empty())
2469 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2470 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002471
2472 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002473 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002474 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002475 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002476 Record.push_back(NumLexicalDeclContexts);
2477 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002478 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002479 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002480}
2481
2482void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2483 Record.push_back(Loc.getRawEncoding());
2484}
2485
2486void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2487 Record.push_back(Value.getBitWidth());
2488 unsigned N = Value.getNumWords();
2489 const uint64_t* Words = Value.getRawData();
2490 for (unsigned I = 0; I != N; ++I)
2491 Record.push_back(Words[I]);
2492}
2493
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002494void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2495 Record.push_back(Value.isUnsigned());
2496 AddAPInt(Value, Record);
2497}
2498
Douglas Gregore2f37202009-04-14 21:55:33 +00002499void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2500 AddAPInt(Value.bitcastToAPInt(), Record);
2501}
2502
Douglas Gregorc34897d2009-04-09 22:27:44 +00002503void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002504 Record.push_back(getIdentifierRef(II));
2505}
2506
2507pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2508 if (II == 0)
2509 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002510
2511 pch::IdentID &ID = IdentifierIDs[II];
2512 if (ID == 0)
2513 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002514 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002515}
2516
Steve Naroff9e84d782009-04-23 10:39:46 +00002517void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2518 if (SelRef.getAsOpaquePtr() == 0) {
2519 Record.push_back(0);
2520 return;
2521 }
2522
2523 pch::SelectorID &SID = SelectorIDs[SelRef];
2524 if (SID == 0) {
2525 SID = SelectorIDs.size();
2526 SelVector.push_back(SelRef);
2527 }
2528 Record.push_back(SID);
2529}
2530
Douglas Gregorc34897d2009-04-09 22:27:44 +00002531void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2532 if (T.isNull()) {
2533 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2534 return;
2535 }
2536
2537 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002538 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002539 switch (BT->getKind()) {
2540 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2541 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2542 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2543 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2544 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2545 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2546 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2547 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2548 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2549 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2550 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2551 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2552 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2553 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2554 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2555 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2556 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2557 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2558 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2559 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2560 }
2561
2562 Record.push_back((ID << 3) | T.getCVRQualifiers());
2563 return;
2564 }
2565
Douglas Gregorac8f2802009-04-10 17:25:41 +00002566 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002567 if (ID == 0) {
2568 // We haven't seen this type before. Assign it a new ID and put it
2569 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002570 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002571 TypesToEmit.push(T.getTypePtr());
2572 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002573
2574 // Encode the type qualifiers in the type reference.
2575 Record.push_back((ID << 3) | T.getCVRQualifiers());
2576}
2577
2578void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2579 if (D == 0) {
2580 Record.push_back(0);
2581 return;
2582 }
2583
Douglas Gregorac8f2802009-04-10 17:25:41 +00002584 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002585 if (ID == 0) {
2586 // We haven't seen this declaration before. Give it a new ID and
2587 // enqueue it in the list of declarations to emit.
2588 ID = DeclIDs.size();
2589 DeclsToEmit.push(const_cast<Decl *>(D));
2590 }
2591
2592 Record.push_back(ID);
2593}
2594
Douglas Gregorff9a6092009-04-20 20:36:09 +00002595pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2596 if (D == 0)
2597 return 0;
2598
2599 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2600 return DeclIDs[D];
2601}
2602
Douglas Gregorc34897d2009-04-09 22:27:44 +00002603void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2604 Record.push_back(Name.getNameKind());
2605 switch (Name.getNameKind()) {
2606 case DeclarationName::Identifier:
2607 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2608 break;
2609
2610 case DeclarationName::ObjCZeroArgSelector:
2611 case DeclarationName::ObjCOneArgSelector:
2612 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002613 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002614 break;
2615
2616 case DeclarationName::CXXConstructorName:
2617 case DeclarationName::CXXDestructorName:
2618 case DeclarationName::CXXConversionFunctionName:
2619 AddTypeRef(Name.getCXXNameType(), Record);
2620 break;
2621
2622 case DeclarationName::CXXOperatorName:
2623 Record.push_back(Name.getCXXOverloadedOperator());
2624 break;
2625
2626 case DeclarationName::CXXUsingDirective:
2627 // No extra data to emit
2628 break;
2629 }
2630}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002631
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002632/// \brief Write the given substatement or subexpression to the
2633/// bitstream.
2634void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002635 RecordData Record;
2636 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002637 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002638
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002639 if (!S) {
2640 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002641 return;
2642 }
2643
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002644 Writer.Code = pch::STMT_NULL_PTR;
2645 Writer.Visit(S);
2646 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002647 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002648 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002649}
2650
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002651/// \brief Flush all of the statements that have been added to the
2652/// queue via AddStmt().
2653void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002654 RecordData Record;
2655 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002656
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002657 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002658 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002659 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002660
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002661 if (!S) {
2662 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002663 continue;
2664 }
2665
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002666 Writer.Code = pch::STMT_NULL_PTR;
2667 Writer.Visit(S);
2668 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002669 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002670 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002671
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002672 assert(N == StmtsToEmit.size() &&
2673 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002674
2675 // Note that we are at the end of a full expression. Any
2676 // expression records that follow this one are part of a different
2677 // expression.
2678 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002679 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002680 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002681
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002682 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002683 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002684}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002685
2686unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2687 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2688 "SwitchCase recorded twice");
2689 unsigned NextID = SwitchCaseIDs.size();
2690 SwitchCaseIDs[S] = NextID;
2691 return NextID;
2692}
2693
2694unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2695 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2696 "SwitchCase hasn't been seen yet");
2697 return SwitchCaseIDs[S];
2698}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002699
2700/// \brief Retrieve the ID for the given label statement, which may
2701/// or may not have been emitted yet.
2702unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2703 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2704 if (Pos != LabelIDs.end())
2705 return Pos->second;
2706
2707 unsigned NextID = LabelIDs.size();
2708 LabelIDs[S] = NextID;
2709 return NextID;
2710}