blob: d40b1065392bdef8db1bb146dbd33fcaa0347a0f [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());
Steve Naroffa323e972009-04-26 14:11:39 +00001213 Code = pch::EXPR_OBJC_IVAR_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001214}
1215
1216void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1217 VisitExpr(E);
1218 Writer.AddDeclRef(E->getProperty(), Record);
1219 Writer.AddSourceLocation(E->getLocation(), Record);
1220 Writer.WriteSubStmt(E->getBase());
Steve Naroffa323e972009-04-26 14:11:39 +00001221 Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001222}
1223
1224void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1225 VisitExpr(E);
1226 Writer.AddDeclRef(E->getGetterMethod(), Record);
1227 Writer.AddDeclRef(E->getSetterMethod(), Record);
1228
1229 // NOTE: ClassProp and Base are mutually exclusive.
1230 Writer.AddDeclRef(E->getClassProp(), Record);
1231 Writer.WriteSubStmt(E->getBase());
1232 Writer.AddSourceLocation(E->getLocation(), Record);
1233 Writer.AddSourceLocation(E->getClassLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001234 Code = pch::EXPR_OBJC_KVC_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001235}
1236
Steve Narofffb3e4022009-04-25 14:04:28 +00001237void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1238 VisitExpr(E);
1239 Record.push_back(E->getNumArgs());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001240 Writer.AddSourceLocation(E->getLeftLoc(), Record);
1241 Writer.AddSourceLocation(E->getRightLoc(), Record);
Steve Narofffb3e4022009-04-25 14:04:28 +00001242 Writer.AddSelectorRef(E->getSelector(), Record);
1243 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001244
1245 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
Steve Narofffb3e4022009-04-25 14:04:28 +00001246 Writer.WriteSubStmt(E->getReceiver());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001247 Writer.AddDeclRef(CI.first, Record);
1248 Writer.AddIdentifierRef(CI.second, Record);
1249
Steve Narofffb3e4022009-04-25 14:04:28 +00001250 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1251 Arg != ArgEnd; ++Arg)
1252 Writer.WriteSubStmt(*Arg);
1253 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1254}
1255
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001256void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1257 VisitExpr(E);
1258 Writer.AddSourceLocation(E->getLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001259 Code = pch::EXPR_OBJC_SUPER_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001260}
1261
Chris Lattner80f83c62009-04-22 05:57:30 +00001262
1263//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001264// PCHWriter Implementation
1265//===----------------------------------------------------------------------===//
1266
Douglas Gregorb5887f32009-04-10 21:16:55 +00001267/// \brief Write the target triple (e.g., i686-apple-darwin9).
1268void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1269 using namespace llvm;
1270 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1271 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001273 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001274
1275 RecordData Record;
1276 Record.push_back(pch::TARGET_TRIPLE);
1277 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001278 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001279}
1280
1281/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001282void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1283 RecordData Record;
1284 Record.push_back(LangOpts.Trigraphs);
1285 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1286 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1287 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1288 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1289 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1290 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1291 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1292 Record.push_back(LangOpts.C99); // C99 Support
1293 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1294 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1295 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1296 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1297 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1298
1299 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1300 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1301 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1302
1303 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1304 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1305 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1306 Record.push_back(LangOpts.LaxVectorConversions);
1307 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1308
1309 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1310 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1311 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1312
1313 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1314 // by locks.
1315 Record.push_back(LangOpts.Blocks); // block extension to C
1316 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1317 // they are unused.
1318 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1319 // (modulo the platform support).
1320
1321 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1322 // signed integer arithmetic overflows.
1323
1324 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1325 // may be ripped out at any time.
1326
1327 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1328 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1329 // defined.
1330 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1331 // opposed to __DYNAMIC__).
1332 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1333
1334 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1335 // used (instead of C99 semantics).
1336 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1337 Record.push_back(LangOpts.getGCMode());
1338 Record.push_back(LangOpts.getVisibilityMode());
1339 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001340 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001341}
1342
Douglas Gregorab1cef72009-04-10 03:52:48 +00001343//===----------------------------------------------------------------------===//
1344// Source Manager Serialization
1345//===----------------------------------------------------------------------===//
1346
1347/// \brief Create an abbreviation for the SLocEntry that refers to a
1348/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001349static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001350 using namespace llvm;
1351 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1352 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001358 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001359}
1360
1361/// \brief Create an abbreviation for the SLocEntry that refers to a
1362/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001363static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001364 using namespace llvm;
1365 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1366 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001372 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001373}
1374
1375/// \brief Create an abbreviation for the SLocEntry that refers to a
1376/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001377static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001378 using namespace llvm;
1379 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1380 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001382 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001383}
1384
1385/// \brief Create an abbreviation for the SLocEntry that refers to an
1386/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001387static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001388 using namespace llvm;
1389 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1390 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001396 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001397}
1398
1399/// \brief Writes the block containing the serialized form of the
1400/// source manager.
1401///
1402/// TODO: We should probably use an on-disk hash table (stored in a
1403/// blob), indexed based on the file name, so that we only create
1404/// entries for files that we actually need. In the common case (no
1405/// errors), we probably won't have to create file entries for any of
1406/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001407void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1408 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001409 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001410 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001411
1412 // Abbreviations for the various kinds of source-location entries.
1413 int SLocFileAbbrv = -1;
1414 int SLocBufferAbbrv = -1;
1415 int SLocBufferBlobAbbrv = -1;
1416 int SLocInstantiationAbbrv = -1;
1417
1418 // Write out the source location entry table. We skip the first
1419 // entry, which is always the same dummy entry.
1420 RecordData Record;
1421 for (SourceManager::sloc_entry_iterator
1422 SLoc = SourceMgr.sloc_entry_begin() + 1,
1423 SLocEnd = SourceMgr.sloc_entry_end();
1424 SLoc != SLocEnd; ++SLoc) {
1425 // Figure out which record code to use.
1426 unsigned Code;
1427 if (SLoc->isFile()) {
1428 if (SLoc->getFile().getContentCache()->Entry)
1429 Code = pch::SM_SLOC_FILE_ENTRY;
1430 else
1431 Code = pch::SM_SLOC_BUFFER_ENTRY;
1432 } else
1433 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1434 Record.push_back(Code);
1435
1436 Record.push_back(SLoc->getOffset());
1437 if (SLoc->isFile()) {
1438 const SrcMgr::FileInfo &File = SLoc->getFile();
1439 Record.push_back(File.getIncludeLoc().getRawEncoding());
1440 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001441 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001442
1443 const SrcMgr::ContentCache *Content = File.getContentCache();
1444 if (Content->Entry) {
1445 // The source location entry is a file. The blob associated
1446 // with this entry is the file name.
1447 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001448 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1449 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001450 Content->Entry->getName(),
1451 strlen(Content->Entry->getName()));
1452 } else {
1453 // The source location entry is a buffer. The blob associated
1454 // with this entry contains the contents of the buffer.
1455 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001456 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1457 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001458 }
1459
1460 // We add one to the size so that we capture the trailing NULL
1461 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1462 // the reader side).
1463 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1464 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001465 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001466 Record.clear();
1467 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001468 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001469 Buffer->getBufferStart(),
1470 Buffer->getBufferSize() + 1);
1471 }
1472 } else {
1473 // The source location entry is an instantiation.
1474 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1475 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1476 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1477 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1478
Douglas Gregor364e5802009-04-15 18:05:10 +00001479 // Compute the token length for this macro expansion.
1480 unsigned NextOffset = SourceMgr.getNextOffset();
1481 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1482 if (++NextSLoc != SLocEnd)
1483 NextOffset = NextSLoc->getOffset();
1484 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1485
Douglas Gregorab1cef72009-04-10 03:52:48 +00001486 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001487 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1488 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001489 }
1490
1491 Record.clear();
1492 }
1493
Douglas Gregor635f97f2009-04-13 16:31:14 +00001494 // Write the line table.
1495 if (SourceMgr.hasLineTable()) {
1496 LineTableInfo &LineTable = SourceMgr.getLineTable();
1497
1498 // Emit the file names
1499 Record.push_back(LineTable.getNumFilenames());
1500 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1501 // Emit the file name
1502 const char *Filename = LineTable.getFilename(I);
1503 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1504 Record.push_back(FilenameLen);
1505 if (FilenameLen)
1506 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1507 }
1508
1509 // Emit the line entries
1510 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1511 L != LEnd; ++L) {
1512 // Emit the file ID
1513 Record.push_back(L->first);
1514
1515 // Emit the line entries
1516 Record.push_back(L->second.size());
1517 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1518 LEEnd = L->second.end();
1519 LE != LEEnd; ++LE) {
1520 Record.push_back(LE->FileOffset);
1521 Record.push_back(LE->LineNo);
1522 Record.push_back(LE->FilenameID);
1523 Record.push_back((unsigned)LE->FileKind);
1524 Record.push_back(LE->IncludeOffset);
1525 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001526 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001527 }
1528 }
1529
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001530 // Loop over all the header files.
1531 HeaderSearch &HS = PP.getHeaderSearchInfo();
1532 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1533 E = HS.header_file_end();
1534 I != E; ++I) {
1535 Record.push_back(I->isImport);
1536 Record.push_back(I->DirInfo);
1537 Record.push_back(I->NumIncludes);
1538 if (I->ControllingMacro)
1539 AddIdentifierRef(I->ControllingMacro, Record);
1540 else
1541 Record.push_back(0);
1542 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1543 Record.clear();
1544 }
1545
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001546 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001547}
1548
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001549/// \brief Writes the block containing the serialized form of the
1550/// preprocessor.
1551///
Chris Lattner850eabd2009-04-10 18:08:30 +00001552void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001553 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001554
Chris Lattner4b21c202009-04-13 01:29:17 +00001555 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1556 if (PP.getCounterValue() != 0) {
1557 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001558 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001559 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001560 }
1561
1562 // Enter the preprocessor block.
1563 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001564
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001565 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1566 // FIXME: use diagnostics subsystem for localization etc.
1567 if (PP.SawDateOrTime())
1568 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1569
Chris Lattner1b094952009-04-10 18:00:12 +00001570 // Loop over all the macro definitions that are live at the end of the file,
1571 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001572 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1573 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001574 // FIXME: This emits macros in hash table order, we should do it in a stable
1575 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001576 MacroInfo *MI = I->second;
1577
1578 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1579 // been redefined by the header (in which case they are not isBuiltinMacro).
1580 if (MI->isBuiltinMacro())
1581 continue;
1582
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001583 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001584 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001585 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001586 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1587 Record.push_back(MI->isUsed());
1588
1589 unsigned Code;
1590 if (MI->isObjectLike()) {
1591 Code = pch::PP_MACRO_OBJECT_LIKE;
1592 } else {
1593 Code = pch::PP_MACRO_FUNCTION_LIKE;
1594
1595 Record.push_back(MI->isC99Varargs());
1596 Record.push_back(MI->isGNUVarargs());
1597 Record.push_back(MI->getNumArgs());
1598 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1599 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001600 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001601 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001602 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001603 Record.clear();
1604
Chris Lattner850eabd2009-04-10 18:08:30 +00001605 // Emit the tokens array.
1606 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1607 // Note that we know that the preprocessor does not have any annotation
1608 // tokens in it because they are created by the parser, and thus can't be
1609 // in a macro definition.
1610 const Token &Tok = MI->getReplacementToken(TokNo);
1611
1612 Record.push_back(Tok.getLocation().getRawEncoding());
1613 Record.push_back(Tok.getLength());
1614
Chris Lattner850eabd2009-04-10 18:08:30 +00001615 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1616 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001617 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001618
1619 // FIXME: Should translate token kind to a stable encoding.
1620 Record.push_back(Tok.getKind());
1621 // FIXME: Should translate token flags to a stable encoding.
1622 Record.push_back(Tok.getFlags());
1623
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001624 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001625 Record.clear();
1626 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001627 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001628 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001629 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001630}
1631
1632
Douglas Gregorc34897d2009-04-09 22:27:44 +00001633/// \brief Write the representation of a type to the PCH stream.
1634void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001635 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001636 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001637 ID = NextTypeID++;
1638
1639 // Record the offset for this type.
1640 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001641 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001642 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1643 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001644 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001645 }
1646
1647 RecordData Record;
1648
1649 // Emit the type's representation.
1650 PCHTypeWriter W(*this, Record);
1651 switch (T->getTypeClass()) {
1652 // For all of the concrete, non-dependent types, call the
1653 // appropriate visitor function.
1654#define TYPE(Class, Base) \
1655 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1656#define ABSTRACT_TYPE(Class, Base)
1657#define DEPENDENT_TYPE(Class, Base)
1658#include "clang/AST/TypeNodes.def"
1659
1660 // For all of the dependent type nodes (which only occur in C++
1661 // templates), produce an error.
1662#define TYPE(Class, Base)
1663#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1664#include "clang/AST/TypeNodes.def"
1665 assert(false && "Cannot serialize dependent type nodes");
1666 break;
1667 }
1668
1669 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001670 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001671
1672 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001673 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001674}
1675
1676/// \brief Write a block containing all of the types.
1677void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001678 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001679 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001680
Douglas Gregore43f0972009-04-26 03:49:13 +00001681 // Emit all of the types that need to be emitted (so far).
1682 while (!TypesToEmit.empty()) {
1683 const Type *T = TypesToEmit.front();
1684 TypesToEmit.pop();
1685 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1686 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687 }
1688
1689 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001690 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001691}
1692
1693/// \brief Write the block containing all of the declaration IDs
1694/// lexically declared within the given DeclContext.
1695///
1696/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1697/// bistream, or 0 if no block was written.
1698uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1699 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001700 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001701 return 0;
1702
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001703 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001704 RecordData Record;
1705 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1706 DEnd = DC->decls_end(Context);
1707 D != DEnd; ++D)
1708 AddDeclRef(*D, Record);
1709
Douglas Gregoraf136d92009-04-22 22:34:57 +00001710 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001711 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001712 return Offset;
1713}
1714
1715/// \brief Write the block containing all of the declaration IDs
1716/// visible from the given DeclContext.
1717///
1718/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1719/// bistream, or 0 if no block was written.
1720uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1721 DeclContext *DC) {
1722 if (DC->getPrimaryContext() != DC)
1723 return 0;
1724
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001725 // Since there is no name lookup into functions or methods, and we
1726 // perform name lookup for the translation unit via the
1727 // IdentifierInfo chains, don't bother to build a
1728 // visible-declarations table for these entities.
1729 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001730 return 0;
1731
Douglas Gregorc34897d2009-04-09 22:27:44 +00001732 // Force the DeclContext to build a its name-lookup table.
1733 DC->lookup(Context, DeclarationName());
1734
1735 // Serialize the contents of the mapping used for lookup. Note that,
1736 // although we have two very different code paths, the serialized
1737 // representation is the same for both cases: a declaration name,
1738 // followed by a size, followed by references to the visible
1739 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001740 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001741 RecordData Record;
1742 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001743 if (!Map)
1744 return 0;
1745
Douglas Gregorc34897d2009-04-09 22:27:44 +00001746 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1747 D != DEnd; ++D) {
1748 AddDeclarationName(D->first, Record);
1749 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1750 Record.push_back(Result.second - Result.first);
1751 for(; Result.first != Result.second; ++Result.first)
1752 AddDeclRef(*Result.first, Record);
1753 }
1754
1755 if (Record.size() == 0)
1756 return 0;
1757
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001758 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001759 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001760 return Offset;
1761}
1762
1763/// \brief Write a block containing all of the declarations.
1764void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001765 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001766 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001767
1768 // Emit all of the declarations.
1769 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001770 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001771 while (!DeclsToEmit.empty()) {
1772 // Pull the next declaration off the queue
1773 Decl *D = DeclsToEmit.front();
1774 DeclsToEmit.pop();
1775
1776 // If this declaration is also a DeclContext, write blocks for the
1777 // declarations that lexically stored inside its context and those
1778 // declarations that are visible from its context. These blocks
1779 // are written before the declaration itself so that we can put
1780 // their offsets into the record for the declaration.
1781 uint64_t LexicalOffset = 0;
1782 uint64_t VisibleOffset = 0;
1783 DeclContext *DC = dyn_cast<DeclContext>(D);
1784 if (DC) {
1785 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1786 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1787 }
1788
1789 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001790 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001791 if (ID == 0)
1792 ID = DeclIDs.size();
1793
1794 unsigned Index = ID - 1;
1795
1796 // Record the offset for this declaration
1797 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001798 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001799 else if (DeclOffsets.size() < Index) {
1800 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001801 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001802 }
1803
1804 // Build and emit a record for this declaration
1805 Record.clear();
1806 W.Code = (pch::DeclCode)0;
1807 W.Visit(D);
1808 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001809
1810 if (!W.Code) {
1811 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1812 D->getDeclKindName());
1813 assert(false && "Unhandled declaration kind while generating PCH");
1814 exit(-1);
1815 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001816 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001817
Douglas Gregor1c507882009-04-15 21:30:51 +00001818 // If the declaration had any attributes, write them now.
1819 if (D->hasAttrs())
1820 WriteAttributeRecord(D->getAttrs());
1821
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001822 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001823 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001824
Douglas Gregor631f6c62009-04-14 00:24:19 +00001825 // Note external declarations so that we can add them to a record
1826 // in the PCH file later.
1827 if (isa<FileScopeAsmDecl>(D))
1828 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001829 }
1830
1831 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001832 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001833}
1834
Douglas Gregorff9a6092009-04-20 20:36:09 +00001835namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001836// Trait used for the on-disk hash table used in the method pool.
1837class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1838 PCHWriter &Writer;
1839
1840public:
1841 typedef Selector key_type;
1842 typedef key_type key_type_ref;
1843
1844 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1845 typedef const data_type& data_type_ref;
1846
1847 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1848
1849 static unsigned ComputeHash(Selector Sel) {
1850 unsigned N = Sel.getNumArgs();
1851 if (N == 0)
1852 ++N;
1853 unsigned R = 5381;
1854 for (unsigned I = 0; I != N; ++I)
1855 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1856 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1857 return R;
1858 }
1859
1860 std::pair<unsigned,unsigned>
1861 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1862 data_type_ref Methods) {
1863 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1864 clang::io::Emit16(Out, KeyLen);
1865 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1866 for (const ObjCMethodList *Method = &Methods.first; Method;
1867 Method = Method->Next)
1868 if (Method->Method)
1869 DataLen += 4;
1870 for (const ObjCMethodList *Method = &Methods.second; Method;
1871 Method = Method->Next)
1872 if (Method->Method)
1873 DataLen += 4;
1874 clang::io::Emit16(Out, DataLen);
1875 return std::make_pair(KeyLen, DataLen);
1876 }
1877
Douglas Gregor2d711832009-04-25 17:48:32 +00001878 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1879 uint64_t Start = Out.tell();
1880 assert((Start >> 32) == 0 && "Selector key offset too large");
1881 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001882 unsigned N = Sel.getNumArgs();
1883 clang::io::Emit16(Out, N);
1884 if (N == 0)
1885 N = 1;
1886 for (unsigned I = 0; I != N; ++I)
1887 clang::io::Emit32(Out,
1888 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1889 }
1890
1891 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001892 data_type_ref Methods, unsigned DataLen) {
1893 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001894 unsigned NumInstanceMethods = 0;
1895 for (const ObjCMethodList *Method = &Methods.first; Method;
1896 Method = Method->Next)
1897 if (Method->Method)
1898 ++NumInstanceMethods;
1899
1900 unsigned NumFactoryMethods = 0;
1901 for (const ObjCMethodList *Method = &Methods.second; Method;
1902 Method = Method->Next)
1903 if (Method->Method)
1904 ++NumFactoryMethods;
1905
1906 clang::io::Emit16(Out, NumInstanceMethods);
1907 clang::io::Emit16(Out, NumFactoryMethods);
1908 for (const ObjCMethodList *Method = &Methods.first; Method;
1909 Method = Method->Next)
1910 if (Method->Method)
1911 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001912 for (const ObjCMethodList *Method = &Methods.second; Method;
1913 Method = Method->Next)
1914 if (Method->Method)
1915 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001916
1917 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001918 }
1919};
1920} // end anonymous namespace
1921
1922/// \brief Write the method pool into the PCH file.
1923///
1924/// The method pool contains both instance and factory methods, stored
1925/// in an on-disk hash table indexed by the selector.
1926void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1927 using namespace llvm;
1928
1929 // Create and write out the blob that contains the instance and
1930 // factor method pools.
1931 bool Empty = true;
1932 {
1933 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1934
1935 // Create the on-disk hash table representation. Start by
1936 // iterating through the instance method pool.
1937 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001938 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001939 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1940 Instance = SemaRef.InstanceMethodPool.begin(),
1941 InstanceEnd = SemaRef.InstanceMethodPool.end();
1942 Instance != InstanceEnd; ++Instance) {
1943 // Check whether there is a factory method with the same
1944 // selector.
1945 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1946 = SemaRef.FactoryMethodPool.find(Instance->first);
1947
1948 if (Factory == SemaRef.FactoryMethodPool.end())
1949 Generator.insert(Instance->first,
1950 std::make_pair(Instance->second,
1951 ObjCMethodList()));
1952 else
1953 Generator.insert(Instance->first,
1954 std::make_pair(Instance->second, Factory->second));
1955
Douglas Gregor2d711832009-04-25 17:48:32 +00001956 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001957 Empty = false;
1958 }
1959
1960 // Now iterate through the factory method pool, to pick up any
1961 // selectors that weren't already in the instance method pool.
1962 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1963 Factory = SemaRef.FactoryMethodPool.begin(),
1964 FactoryEnd = SemaRef.FactoryMethodPool.end();
1965 Factory != FactoryEnd; ++Factory) {
1966 // Check whether there is an instance method with the same
1967 // selector. If so, there is no work to do here.
1968 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1969 = SemaRef.InstanceMethodPool.find(Factory->first);
1970
Douglas Gregor2d711832009-04-25 17:48:32 +00001971 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001972 Generator.insert(Factory->first,
1973 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001974 ++NumSelectorsInMethodPool;
1975 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001976
1977 Empty = false;
1978 }
1979
Douglas Gregor2d711832009-04-25 17:48:32 +00001980 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001981 return;
1982
1983 // Create the on-disk hash table in a buffer.
1984 llvm::SmallVector<char, 4096> MethodPool;
1985 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001986 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001987 {
1988 PCHMethodPoolTrait Trait(*this);
1989 llvm::raw_svector_ostream Out(MethodPool);
1990 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001991 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001992 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001993
1994 // For every selector that we have seen but which was not
1995 // written into the hash table, write the selector itself and
1996 // record it's offset.
1997 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1998 if (SelectorOffsets[I] == 0)
1999 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002000 }
2001
2002 // Create a blob abbreviation
2003 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2004 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2008 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2009
Douglas Gregor2d711832009-04-25 17:48:32 +00002010 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002011 RecordData Record;
2012 Record.push_back(pch::METHOD_POOL);
2013 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002014 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002015 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2016 &MethodPool.front(),
2017 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002018
2019 // Create a blob abbreviation for the selector table offsets.
2020 Abbrev = new BitCodeAbbrev();
2021 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2024 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2025
2026 // Write the selector offsets table.
2027 Record.clear();
2028 Record.push_back(pch::SELECTOR_OFFSETS);
2029 Record.push_back(SelectorOffsets.size());
2030 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2031 (const char *)&SelectorOffsets.front(),
2032 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002033 }
2034}
2035
2036namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002037class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2038 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002039 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002040
2041public:
2042 typedef const IdentifierInfo* key_type;
2043 typedef key_type key_type_ref;
2044
2045 typedef pch::IdentID data_type;
2046 typedef data_type data_type_ref;
2047
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002048 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2049 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002050
2051 static unsigned ComputeHash(const IdentifierInfo* II) {
2052 return clang::BernsteinHash(II->getName());
2053 }
2054
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002055 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002056 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2057 pch::IdentID ID) {
2058 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002059 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2060 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002061 if (II->hasMacroDefinition() &&
2062 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2063 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002064 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2065 DEnd = IdentifierResolver::end();
2066 D != DEnd; ++D)
2067 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002068 // We emit the key length after the data length so that the
2069 // "uninteresting" identifiers following the identifier hash table
2070 // structure will have the same (key length, key characters)
2071 // layout as the keys in the hash table. This also matches the
2072 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002073 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002074 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002075 return std::make_pair(KeyLen, DataLen);
2076 }
2077
2078 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2079 unsigned KeyLen) {
2080 // Record the location of the key data. This is used when generating
2081 // the mapping from persistent IDs to strings.
2082 Writer.SetIdentifierOffset(II, Out.tell());
2083 Out.write(II->getName(), KeyLen);
2084 }
2085
2086 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2087 pch::IdentID ID, unsigned) {
2088 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002089 bool hasMacroDefinition =
2090 II->hasMacroDefinition() &&
2091 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002092 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002093 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2094 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002095 Bits = (Bits << 1) | II->isExtensionToken();
2096 Bits = (Bits << 1) | II->isPoisoned();
2097 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2098 clang::io::Emit32(Out, Bits);
2099 clang::io::Emit32(Out, ID);
2100
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002101 if (hasMacroDefinition)
2102 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2103
Douglas Gregorc713da92009-04-21 22:25:48 +00002104 // Emit the declaration IDs in reverse order, because the
2105 // IdentifierResolver provides the declarations as they would be
2106 // visible (e.g., the function "stat" would come before the struct
2107 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2108 // adds declarations to the end of the list (so we need to see the
2109 // struct "status" before the function "status").
2110 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2111 IdentifierResolver::end());
2112 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2113 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002114 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002115 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002116 }
2117};
2118} // end anonymous namespace
2119
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002120/// \brief Write the identifier table into the PCH file.
2121///
2122/// The identifier table consists of a blob containing string data
2123/// (the actual identifiers themselves) and a separate "offsets" index
2124/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002125void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002126 using namespace llvm;
2127
2128 // Create and write out the blob that contains the identifier
2129 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002130 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002131 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002132 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2133
Douglas Gregor85c4a872009-04-25 21:04:17 +00002134 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2135
Douglas Gregorff9a6092009-04-20 20:36:09 +00002136 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002137 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2138 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2139 ID != IDEnd; ++ID) {
2140 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002141
2142 // Classify each identifier as either "interesting" or "not
2143 // interesting". Interesting identifiers are those that have
2144 // additional information that needs to be read from the PCH
2145 // file, e.g., a built-in ID, declaration chain, or macro
2146 // definition. These identifiers are placed into the hash table
2147 // so that they can be found when looked up in the user program.
2148 // All other identifiers are "uninteresting", which means that
2149 // the IdentifierInfo built by default has all of the
2150 // information we care about. Such identifiers are placed after
2151 // the hash table.
2152 const IdentifierInfo *II = ID->first;
2153 if (II->isPoisoned() ||
2154 II->isExtensionToken() ||
2155 II->hasMacroDefinition() ||
2156 II->getObjCOrBuiltinID() ||
2157 II->getFETokenInfo<void>())
2158 Generator.insert(ID->first, ID->second);
2159 else
2160 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002161 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002162
Douglas Gregorff9a6092009-04-20 20:36:09 +00002163 // Create the on-disk hash table in a buffer.
2164 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002165 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002166 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002167 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002168 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002169 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002170 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002171 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002172
2173 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2174 const IdentifierInfo *II = UninterestingIdentifiers[I];
2175 unsigned N = II->getLength() + 1;
2176 clang::io::Emit16(Out, N);
2177 SetIdentifierOffset(II, Out.tell());
2178 Out.write(II->getName(), N);
2179 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002180 }
2181
2182 // Create a blob abbreviation
2183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2184 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002187 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002188
2189 // Write the identifier table
2190 RecordData Record;
2191 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002192 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002193 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2194 &IdentifierTable.front(),
2195 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002196 }
2197
2198 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002199 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2200 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2203 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2204
2205 RecordData Record;
2206 Record.push_back(pch::IDENTIFIER_OFFSET);
2207 Record.push_back(IdentifierOffsets.size());
2208 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2209 (const char *)&IdentifierOffsets.front(),
2210 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002211}
2212
Douglas Gregor1c507882009-04-15 21:30:51 +00002213/// \brief Write a record containing the given attributes.
2214void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2215 RecordData Record;
2216 for (; Attr; Attr = Attr->getNext()) {
2217 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2218 Record.push_back(Attr->isInherited());
2219 switch (Attr->getKind()) {
2220 case Attr::Alias:
2221 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2222 break;
2223
2224 case Attr::Aligned:
2225 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2226 break;
2227
2228 case Attr::AlwaysInline:
2229 break;
2230
2231 case Attr::AnalyzerNoReturn:
2232 break;
2233
2234 case Attr::Annotate:
2235 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2236 break;
2237
2238 case Attr::AsmLabel:
2239 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2240 break;
2241
2242 case Attr::Blocks:
2243 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2244 break;
2245
2246 case Attr::Cleanup:
2247 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2248 break;
2249
2250 case Attr::Const:
2251 break;
2252
2253 case Attr::Constructor:
2254 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2255 break;
2256
2257 case Attr::DLLExport:
2258 case Attr::DLLImport:
2259 case Attr::Deprecated:
2260 break;
2261
2262 case Attr::Destructor:
2263 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2264 break;
2265
2266 case Attr::FastCall:
2267 break;
2268
2269 case Attr::Format: {
2270 const FormatAttr *Format = cast<FormatAttr>(Attr);
2271 AddString(Format->getType(), Record);
2272 Record.push_back(Format->getFormatIdx());
2273 Record.push_back(Format->getFirstArg());
2274 break;
2275 }
2276
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002277 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002278 case Attr::IBOutletKind:
2279 case Attr::NoReturn:
2280 case Attr::NoThrow:
2281 case Attr::Nodebug:
2282 case Attr::Noinline:
2283 break;
2284
2285 case Attr::NonNull: {
2286 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2287 Record.push_back(NonNull->size());
2288 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2289 break;
2290 }
2291
2292 case Attr::ObjCException:
2293 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002294 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002295 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002296 case Attr::Overloadable:
2297 break;
2298
2299 case Attr::Packed:
2300 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2301 break;
2302
2303 case Attr::Pure:
2304 break;
2305
2306 case Attr::Regparm:
2307 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2308 break;
2309
2310 case Attr::Section:
2311 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2312 break;
2313
2314 case Attr::StdCall:
2315 case Attr::TransparentUnion:
2316 case Attr::Unavailable:
2317 case Attr::Unused:
2318 case Attr::Used:
2319 break;
2320
2321 case Attr::Visibility:
2322 // FIXME: stable encoding
2323 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2324 break;
2325
2326 case Attr::WarnUnusedResult:
2327 case Attr::Weak:
2328 case Attr::WeakImport:
2329 break;
2330 }
2331 }
2332
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002333 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002334}
2335
2336void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2337 Record.push_back(Str.size());
2338 Record.insert(Record.end(), Str.begin(), Str.end());
2339}
2340
Douglas Gregorff9a6092009-04-20 20:36:09 +00002341/// \brief Note that the identifier II occurs at the given offset
2342/// within the identifier table.
2343void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002344 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002345}
2346
Douglas Gregor2d711832009-04-25 17:48:32 +00002347/// \brief Note that the selector Sel occurs at the given offset
2348/// within the method pool/selector table.
2349void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2350 unsigned ID = SelectorIDs[Sel];
2351 assert(ID && "Unknown selector");
2352 SelectorOffsets[ID - 1] = Offset;
2353}
2354
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002355PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002356 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002357 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2358 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002359
Douglas Gregor87887da2009-04-20 15:53:59 +00002360void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002361 using namespace llvm;
2362
Douglas Gregor87887da2009-04-20 15:53:59 +00002363 ASTContext &Context = SemaRef.Context;
2364 Preprocessor &PP = SemaRef.PP;
2365
Douglas Gregorc34897d2009-04-09 22:27:44 +00002366 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002367 Stream.Emit((unsigned)'C', 8);
2368 Stream.Emit((unsigned)'P', 8);
2369 Stream.Emit((unsigned)'C', 8);
2370 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002371
2372 // The translation unit is the first declaration we'll emit.
2373 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2374 DeclsToEmit.push(Context.getTranslationUnitDecl());
2375
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002376 // Make sure that we emit IdentifierInfos (and any attached
2377 // declarations) for builtins.
2378 {
2379 IdentifierTable &Table = PP.getIdentifierTable();
2380 llvm::SmallVector<const char *, 32> BuiltinNames;
2381 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2382 Context.getLangOptions().NoBuiltin);
2383 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2384 getIdentifierRef(&Table.get(BuiltinNames[I]));
2385 }
2386
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002387 // Build a record containing all of the tentative definitions in
2388 // this header file. Generally, this record will be empty.
2389 RecordData TentativeDefinitions;
2390 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2391 TD = SemaRef.TentativeDefinitions.begin(),
2392 TDEnd = SemaRef.TentativeDefinitions.end();
2393 TD != TDEnd; ++TD)
2394 AddDeclRef(TD->second, TentativeDefinitions);
2395
Douglas Gregor062d9482009-04-22 22:18:58 +00002396 // Build a record containing all of the locally-scoped external
2397 // declarations in this header file. Generally, this record will be
2398 // empty.
2399 RecordData LocallyScopedExternalDecls;
2400 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2401 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2402 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2403 TD != TDEnd; ++TD)
2404 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2405
Douglas Gregorc34897d2009-04-09 22:27:44 +00002406 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002407 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002408 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002409 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002410 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002411 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002412 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002413
2414 // Keep writing types and declarations until all types and
2415 // declarations have been written.
2416 do {
2417 if (!DeclsToEmit.empty())
2418 WriteDeclsBlock(Context);
2419 if (!TypesToEmit.empty())
2420 WriteTypesBlock(Context);
2421 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2422
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002423 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002424 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002425
2426 // Write the type offsets array
2427 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2428 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2431 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2432 Record.clear();
2433 Record.push_back(pch::TYPE_OFFSET);
2434 Record.push_back(TypeOffsets.size());
2435 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2436 (const char *)&TypeOffsets.front(),
2437 TypeOffsets.size() * sizeof(uint64_t));
2438
2439 // Write the declaration offsets array
2440 Abbrev = new BitCodeAbbrev();
2441 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2444 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2445 Record.clear();
2446 Record.push_back(pch::DECL_OFFSET);
2447 Record.push_back(DeclOffsets.size());
2448 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2449 (const char *)&DeclOffsets.front(),
2450 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002451
2452 // Write the record of special types.
2453 Record.clear();
2454 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002455 AddTypeRef(Context.getObjCIdType(), Record);
2456 AddTypeRef(Context.getObjCSelType(), Record);
2457 AddTypeRef(Context.getObjCProtoType(), Record);
2458 AddTypeRef(Context.getObjCClassType(), Record);
2459 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2460 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002461 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2462
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002463 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002464 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002465 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002466
2467 // Write the record containing tentative definitions.
2468 if (!TentativeDefinitions.empty())
2469 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002470
2471 // Write the record containing locally-scoped external definitions.
2472 if (!LocallyScopedExternalDecls.empty())
2473 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2474 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002475
2476 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002477 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002478 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002479 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002480 Record.push_back(NumLexicalDeclContexts);
2481 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002482 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002483 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002484}
2485
2486void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2487 Record.push_back(Loc.getRawEncoding());
2488}
2489
2490void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2491 Record.push_back(Value.getBitWidth());
2492 unsigned N = Value.getNumWords();
2493 const uint64_t* Words = Value.getRawData();
2494 for (unsigned I = 0; I != N; ++I)
2495 Record.push_back(Words[I]);
2496}
2497
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002498void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2499 Record.push_back(Value.isUnsigned());
2500 AddAPInt(Value, Record);
2501}
2502
Douglas Gregore2f37202009-04-14 21:55:33 +00002503void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2504 AddAPInt(Value.bitcastToAPInt(), Record);
2505}
2506
Douglas Gregorc34897d2009-04-09 22:27:44 +00002507void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002508 Record.push_back(getIdentifierRef(II));
2509}
2510
2511pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2512 if (II == 0)
2513 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002514
2515 pch::IdentID &ID = IdentifierIDs[II];
2516 if (ID == 0)
2517 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002518 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002519}
2520
Steve Naroff9e84d782009-04-23 10:39:46 +00002521void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2522 if (SelRef.getAsOpaquePtr() == 0) {
2523 Record.push_back(0);
2524 return;
2525 }
2526
2527 pch::SelectorID &SID = SelectorIDs[SelRef];
2528 if (SID == 0) {
2529 SID = SelectorIDs.size();
2530 SelVector.push_back(SelRef);
2531 }
2532 Record.push_back(SID);
2533}
2534
Douglas Gregorc34897d2009-04-09 22:27:44 +00002535void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2536 if (T.isNull()) {
2537 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2538 return;
2539 }
2540
2541 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002542 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002543 switch (BT->getKind()) {
2544 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2545 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2546 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2547 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2548 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2549 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2550 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2551 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2552 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2553 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2554 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2555 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2556 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2557 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2558 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2559 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2560 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2561 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2562 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2563 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2564 }
2565
2566 Record.push_back((ID << 3) | T.getCVRQualifiers());
2567 return;
2568 }
2569
Douglas Gregorac8f2802009-04-10 17:25:41 +00002570 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002571 if (ID == 0) {
2572 // We haven't seen this type before. Assign it a new ID and put it
2573 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002574 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002575 TypesToEmit.push(T.getTypePtr());
2576 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002577
2578 // Encode the type qualifiers in the type reference.
2579 Record.push_back((ID << 3) | T.getCVRQualifiers());
2580}
2581
2582void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2583 if (D == 0) {
2584 Record.push_back(0);
2585 return;
2586 }
2587
Douglas Gregorac8f2802009-04-10 17:25:41 +00002588 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002589 if (ID == 0) {
2590 // We haven't seen this declaration before. Give it a new ID and
2591 // enqueue it in the list of declarations to emit.
2592 ID = DeclIDs.size();
2593 DeclsToEmit.push(const_cast<Decl *>(D));
2594 }
2595
2596 Record.push_back(ID);
2597}
2598
Douglas Gregorff9a6092009-04-20 20:36:09 +00002599pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2600 if (D == 0)
2601 return 0;
2602
2603 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2604 return DeclIDs[D];
2605}
2606
Douglas Gregorc34897d2009-04-09 22:27:44 +00002607void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2608 Record.push_back(Name.getNameKind());
2609 switch (Name.getNameKind()) {
2610 case DeclarationName::Identifier:
2611 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2612 break;
2613
2614 case DeclarationName::ObjCZeroArgSelector:
2615 case DeclarationName::ObjCOneArgSelector:
2616 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002617 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002618 break;
2619
2620 case DeclarationName::CXXConstructorName:
2621 case DeclarationName::CXXDestructorName:
2622 case DeclarationName::CXXConversionFunctionName:
2623 AddTypeRef(Name.getCXXNameType(), Record);
2624 break;
2625
2626 case DeclarationName::CXXOperatorName:
2627 Record.push_back(Name.getCXXOverloadedOperator());
2628 break;
2629
2630 case DeclarationName::CXXUsingDirective:
2631 // No extra data to emit
2632 break;
2633 }
2634}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002635
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002636/// \brief Write the given substatement or subexpression to the
2637/// bitstream.
2638void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002639 RecordData Record;
2640 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002641 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002642
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002643 if (!S) {
2644 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002645 return;
2646 }
2647
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002648 Writer.Code = pch::STMT_NULL_PTR;
2649 Writer.Visit(S);
2650 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002651 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002652 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002653}
2654
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002655/// \brief Flush all of the statements that have been added to the
2656/// queue via AddStmt().
2657void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002658 RecordData Record;
2659 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002660
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002661 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002662 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002663 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002664
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002665 if (!S) {
2666 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002667 continue;
2668 }
2669
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002670 Writer.Code = pch::STMT_NULL_PTR;
2671 Writer.Visit(S);
2672 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002673 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002674 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002675
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002676 assert(N == StmtsToEmit.size() &&
2677 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002678
2679 // Note that we are at the end of a full expression. Any
2680 // expression records that follow this one are part of a different
2681 // expression.
2682 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002683 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002684 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002685
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002686 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002687 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002688}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002689
2690unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2691 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2692 "SwitchCase recorded twice");
2693 unsigned NextID = SwitchCaseIDs.size();
2694 SwitchCaseIDs[S] = NextID;
2695 return NextID;
2696}
2697
2698unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2699 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2700 "SwitchCase hasn't been seen yet");
2701 return SwitchCaseIDs[S];
2702}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002703
2704/// \brief Retrieve the ID for the given label statement, which may
2705/// or may not have been emitted yet.
2706unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2707 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2708 if (Pos != LabelIDs.end())
2709 return Pos->second;
2710
2711 unsigned NextID = LabelIDs.size();
2712 LabelIDs[S] = NextID;
2713 return NextID;
2714}