blob: 5037a091931a3402b592ac591d2e69b1a4f0d5ca [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
Steve Naroff79762bd2009-04-26 18:52:16 +0000670 // Objective-C Expressions
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);
Steve Naroff79762bd2009-04-26 18:52:16 +0000680
681 // Objective-C Statements
682 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
683 void VisitObjCCatchStmt(ObjCAtCatchStmt *);
684 void VisitObjCFinallyStmt(ObjCAtFinallyStmt *);
685 void VisitObjCAtTryStmt(ObjCAtTryStmt *);
686 void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
687 void VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000688 };
689}
690
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000691void PCHStmtWriter::VisitStmt(Stmt *S) {
692}
693
694void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
695 VisitStmt(S);
696 Writer.AddSourceLocation(S->getSemiLoc(), Record);
697 Code = pch::STMT_NULL;
698}
699
700void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
701 VisitStmt(S);
702 Record.push_back(S->size());
703 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
704 CS != CSEnd; ++CS)
705 Writer.WriteSubStmt(*CS);
706 Writer.AddSourceLocation(S->getLBracLoc(), Record);
707 Writer.AddSourceLocation(S->getRBracLoc(), Record);
708 Code = pch::STMT_COMPOUND;
709}
710
711void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
712 VisitStmt(S);
713 Record.push_back(Writer.RecordSwitchCaseID(S));
714}
715
716void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
717 VisitSwitchCase(S);
718 Writer.WriteSubStmt(S->getLHS());
719 Writer.WriteSubStmt(S->getRHS());
720 Writer.WriteSubStmt(S->getSubStmt());
721 Writer.AddSourceLocation(S->getCaseLoc(), Record);
722 Code = pch::STMT_CASE;
723}
724
725void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
726 VisitSwitchCase(S);
727 Writer.WriteSubStmt(S->getSubStmt());
728 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
729 Code = pch::STMT_DEFAULT;
730}
731
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000732void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
733 VisitStmt(S);
734 Writer.AddIdentifierRef(S->getID(), Record);
735 Writer.WriteSubStmt(S->getSubStmt());
736 Writer.AddSourceLocation(S->getIdentLoc(), Record);
737 Record.push_back(Writer.GetLabelID(S));
738 Code = pch::STMT_LABEL;
739}
740
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000741void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
742 VisitStmt(S);
743 Writer.WriteSubStmt(S->getCond());
744 Writer.WriteSubStmt(S->getThen());
745 Writer.WriteSubStmt(S->getElse());
746 Writer.AddSourceLocation(S->getIfLoc(), Record);
747 Code = pch::STMT_IF;
748}
749
750void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
751 VisitStmt(S);
752 Writer.WriteSubStmt(S->getCond());
753 Writer.WriteSubStmt(S->getBody());
754 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
755 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
756 SC = SC->getNextSwitchCase())
757 Record.push_back(Writer.getSwitchCaseID(SC));
758 Code = pch::STMT_SWITCH;
759}
760
Douglas Gregora6b503f2009-04-17 00:16:09 +0000761void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
762 VisitStmt(S);
763 Writer.WriteSubStmt(S->getCond());
764 Writer.WriteSubStmt(S->getBody());
765 Writer.AddSourceLocation(S->getWhileLoc(), Record);
766 Code = pch::STMT_WHILE;
767}
768
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000769void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
770 VisitStmt(S);
771 Writer.WriteSubStmt(S->getCond());
772 Writer.WriteSubStmt(S->getBody());
773 Writer.AddSourceLocation(S->getDoLoc(), Record);
774 Code = pch::STMT_DO;
775}
776
777void PCHStmtWriter::VisitForStmt(ForStmt *S) {
778 VisitStmt(S);
779 Writer.WriteSubStmt(S->getInit());
780 Writer.WriteSubStmt(S->getCond());
781 Writer.WriteSubStmt(S->getInc());
782 Writer.WriteSubStmt(S->getBody());
783 Writer.AddSourceLocation(S->getForLoc(), Record);
784 Code = pch::STMT_FOR;
785}
786
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000787void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
788 VisitStmt(S);
789 Record.push_back(Writer.GetLabelID(S->getLabel()));
790 Writer.AddSourceLocation(S->getGotoLoc(), Record);
791 Writer.AddSourceLocation(S->getLabelLoc(), Record);
792 Code = pch::STMT_GOTO;
793}
794
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000795void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
796 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000797 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000798 Writer.WriteSubStmt(S->getTarget());
799 Code = pch::STMT_INDIRECT_GOTO;
800}
801
Douglas Gregora6b503f2009-04-17 00:16:09 +0000802void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
803 VisitStmt(S);
804 Writer.AddSourceLocation(S->getContinueLoc(), Record);
805 Code = pch::STMT_CONTINUE;
806}
807
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000808void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
809 VisitStmt(S);
810 Writer.AddSourceLocation(S->getBreakLoc(), Record);
811 Code = pch::STMT_BREAK;
812}
813
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000814void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
815 VisitStmt(S);
816 Writer.WriteSubStmt(S->getRetValue());
817 Writer.AddSourceLocation(S->getReturnLoc(), Record);
818 Code = pch::STMT_RETURN;
819}
820
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000821void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
822 VisitStmt(S);
823 Writer.AddSourceLocation(S->getStartLoc(), Record);
824 Writer.AddSourceLocation(S->getEndLoc(), Record);
825 DeclGroupRef DG = S->getDeclGroup();
826 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
827 Writer.AddDeclRef(*D, Record);
828 Code = pch::STMT_DECL;
829}
830
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000831void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
832 VisitStmt(S);
833 Record.push_back(S->getNumOutputs());
834 Record.push_back(S->getNumInputs());
835 Record.push_back(S->getNumClobbers());
836 Writer.AddSourceLocation(S->getAsmLoc(), Record);
837 Writer.AddSourceLocation(S->getRParenLoc(), Record);
838 Record.push_back(S->isVolatile());
839 Record.push_back(S->isSimple());
840 Writer.WriteSubStmt(S->getAsmString());
841
842 // Outputs
843 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
844 Writer.AddString(S->getOutputName(I), Record);
845 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
846 Writer.WriteSubStmt(S->getOutputExpr(I));
847 }
848
849 // Inputs
850 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
851 Writer.AddString(S->getInputName(I), Record);
852 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
853 Writer.WriteSubStmt(S->getInputExpr(I));
854 }
855
856 // Clobbers
857 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
858 Writer.WriteSubStmt(S->getClobber(I));
859
860 Code = pch::STMT_ASM;
861}
862
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000863void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000864 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000865 Writer.AddTypeRef(E->getType(), Record);
866 Record.push_back(E->isTypeDependent());
867 Record.push_back(E->isValueDependent());
868}
869
Douglas Gregore2f37202009-04-14 21:55:33 +0000870void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
871 VisitExpr(E);
872 Writer.AddSourceLocation(E->getLocation(), Record);
873 Record.push_back(E->getIdentType()); // FIXME: stable encoding
874 Code = pch::EXPR_PREDEFINED;
875}
876
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000877void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
878 VisitExpr(E);
879 Writer.AddDeclRef(E->getDecl(), Record);
880 Writer.AddSourceLocation(E->getLocation(), Record);
881 Code = pch::EXPR_DECL_REF;
882}
883
884void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
885 VisitExpr(E);
886 Writer.AddSourceLocation(E->getLocation(), Record);
887 Writer.AddAPInt(E->getValue(), Record);
888 Code = pch::EXPR_INTEGER_LITERAL;
889}
890
Douglas Gregore2f37202009-04-14 21:55:33 +0000891void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
892 VisitExpr(E);
893 Writer.AddAPFloat(E->getValue(), Record);
894 Record.push_back(E->isExact());
895 Writer.AddSourceLocation(E->getLocation(), Record);
896 Code = pch::EXPR_FLOATING_LITERAL;
897}
898
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000899void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
900 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000901 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000902 Code = pch::EXPR_IMAGINARY_LITERAL;
903}
904
Douglas Gregor596e0932009-04-15 16:35:07 +0000905void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
906 VisitExpr(E);
907 Record.push_back(E->getByteLength());
908 Record.push_back(E->getNumConcatenated());
909 Record.push_back(E->isWide());
910 // FIXME: String data should be stored as a blob at the end of the
911 // StringLiteral. However, we can't do so now because we have no
912 // provision for coping with abbreviations when we're jumping around
913 // the PCH file during deserialization.
914 Record.insert(Record.end(),
915 E->getStrData(), E->getStrData() + E->getByteLength());
916 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
917 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
918 Code = pch::EXPR_STRING_LITERAL;
919}
920
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000921void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
922 VisitExpr(E);
923 Record.push_back(E->getValue());
924 Writer.AddSourceLocation(E->getLoc(), Record);
925 Record.push_back(E->isWide());
926 Code = pch::EXPR_CHARACTER_LITERAL;
927}
928
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000929void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
930 VisitExpr(E);
931 Writer.AddSourceLocation(E->getLParen(), Record);
932 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000933 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000934 Code = pch::EXPR_PAREN;
935}
936
Douglas Gregor12d74052009-04-15 15:58:59 +0000937void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
938 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000939 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000940 Record.push_back(E->getOpcode()); // FIXME: stable encoding
941 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
942 Code = pch::EXPR_UNARY_OPERATOR;
943}
944
945void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
946 VisitExpr(E);
947 Record.push_back(E->isSizeOf());
948 if (E->isArgumentType())
949 Writer.AddTypeRef(E->getArgumentType(), Record);
950 else {
951 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000952 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000953 }
954 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
955 Writer.AddSourceLocation(E->getRParenLoc(), Record);
956 Code = pch::EXPR_SIZEOF_ALIGN_OF;
957}
958
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000959void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
960 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000961 Writer.WriteSubStmt(E->getLHS());
962 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000963 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
964 Code = pch::EXPR_ARRAY_SUBSCRIPT;
965}
966
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000967void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
968 VisitExpr(E);
969 Record.push_back(E->getNumArgs());
970 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000971 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000972 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
973 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000974 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000975 Code = pch::EXPR_CALL;
976}
977
978void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
979 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000980 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000981 Writer.AddDeclRef(E->getMemberDecl(), Record);
982 Writer.AddSourceLocation(E->getMemberLoc(), Record);
983 Record.push_back(E->isArrow());
984 Code = pch::EXPR_MEMBER;
985}
986
Douglas Gregora151ba42009-04-14 23:32:43 +0000987void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
988 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000989 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000990}
991
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000992void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
993 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000994 Writer.WriteSubStmt(E->getLHS());
995 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000996 Record.push_back(E->getOpcode()); // FIXME: stable encoding
997 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
998 Code = pch::EXPR_BINARY_OPERATOR;
999}
1000
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001001void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
1002 VisitBinaryOperator(E);
1003 Writer.AddTypeRef(E->getComputationLHSType(), Record);
1004 Writer.AddTypeRef(E->getComputationResultType(), Record);
1005 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
1006}
1007
1008void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
1009 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001010 Writer.WriteSubStmt(E->getCond());
1011 Writer.WriteSubStmt(E->getLHS());
1012 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001013 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1014}
1015
Douglas Gregora151ba42009-04-14 23:32:43 +00001016void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1017 VisitCastExpr(E);
1018 Record.push_back(E->isLvalueCast());
1019 Code = pch::EXPR_IMPLICIT_CAST;
1020}
1021
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001022void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1023 VisitCastExpr(E);
1024 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1025}
1026
1027void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1028 VisitExplicitCastExpr(E);
1029 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1030 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1031 Code = pch::EXPR_CSTYLE_CAST;
1032}
1033
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001034void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1035 VisitExpr(E);
1036 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001037 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001038 Record.push_back(E->isFileScope());
1039 Code = pch::EXPR_COMPOUND_LITERAL;
1040}
1041
Douglas Gregorec0b8292009-04-15 23:02:49 +00001042void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1043 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001044 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001045 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1046 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1047 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1048}
1049
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001050void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1051 VisitExpr(E);
1052 Record.push_back(E->getNumInits());
1053 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001054 Writer.WriteSubStmt(E->getInit(I));
1055 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001056 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1057 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1058 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1059 Record.push_back(E->hadArrayRangeDesignator());
1060 Code = pch::EXPR_INIT_LIST;
1061}
1062
1063void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1064 VisitExpr(E);
1065 Record.push_back(E->getNumSubExprs());
1066 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001067 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001068 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1069 Record.push_back(E->usesGNUSyntax());
1070 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1071 DEnd = E->designators_end();
1072 D != DEnd; ++D) {
1073 if (D->isFieldDesignator()) {
1074 if (FieldDecl *Field = D->getField()) {
1075 Record.push_back(pch::DESIG_FIELD_DECL);
1076 Writer.AddDeclRef(Field, Record);
1077 } else {
1078 Record.push_back(pch::DESIG_FIELD_NAME);
1079 Writer.AddIdentifierRef(D->getFieldName(), Record);
1080 }
1081 Writer.AddSourceLocation(D->getDotLoc(), Record);
1082 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1083 } else if (D->isArrayDesignator()) {
1084 Record.push_back(pch::DESIG_ARRAY);
1085 Record.push_back(D->getFirstExprIndex());
1086 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1087 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1088 } else {
1089 assert(D->isArrayRangeDesignator() && "Unknown designator");
1090 Record.push_back(pch::DESIG_ARRAY_RANGE);
1091 Record.push_back(D->getFirstExprIndex());
1092 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1093 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1094 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1095 }
1096 }
1097 Code = pch::EXPR_DESIGNATED_INIT;
1098}
1099
1100void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1101 VisitExpr(E);
1102 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1103}
1104
Douglas Gregorec0b8292009-04-15 23:02:49 +00001105void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1106 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001107 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001108 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1109 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1110 Code = pch::EXPR_VA_ARG;
1111}
1112
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001113void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1114 VisitExpr(E);
1115 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1116 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1117 Record.push_back(Writer.GetLabelID(E->getLabel()));
1118 Code = pch::EXPR_ADDR_LABEL;
1119}
1120
Douglas Gregoreca12f62009-04-17 19:05:30 +00001121void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1122 VisitExpr(E);
1123 Writer.WriteSubStmt(E->getSubStmt());
1124 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1125 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1126 Code = pch::EXPR_STMT;
1127}
1128
Douglas Gregor209d4622009-04-15 23:33:31 +00001129void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1130 VisitExpr(E);
1131 Writer.AddTypeRef(E->getArgType1(), Record);
1132 Writer.AddTypeRef(E->getArgType2(), Record);
1133 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1134 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1135 Code = pch::EXPR_TYPES_COMPATIBLE;
1136}
1137
1138void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1139 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001140 Writer.WriteSubStmt(E->getCond());
1141 Writer.WriteSubStmt(E->getLHS());
1142 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001143 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1144 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1145 Code = pch::EXPR_CHOOSE;
1146}
1147
1148void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1149 VisitExpr(E);
1150 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1151 Code = pch::EXPR_GNU_NULL;
1152}
1153
Douglas Gregor725e94b2009-04-16 00:01:45 +00001154void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1155 VisitExpr(E);
1156 Record.push_back(E->getNumSubExprs());
1157 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001158 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001159 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1160 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1161 Code = pch::EXPR_SHUFFLE_VECTOR;
1162}
1163
Douglas Gregore246b742009-04-17 19:21:43 +00001164void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1165 VisitExpr(E);
1166 Writer.AddDeclRef(E->getBlockDecl(), Record);
1167 Record.push_back(E->hasBlockDeclRefExprs());
1168 Code = pch::EXPR_BLOCK;
1169}
1170
Douglas Gregor725e94b2009-04-16 00:01:45 +00001171void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1172 VisitExpr(E);
1173 Writer.AddDeclRef(E->getDecl(), Record);
1174 Writer.AddSourceLocation(E->getLocation(), Record);
1175 Record.push_back(E->isByRef());
1176 Code = pch::EXPR_BLOCK_DECL_REF;
1177}
1178
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001179//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001180// Objective-C Expressions and Statements.
1181//===----------------------------------------------------------------------===//
1182
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001183void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1184 VisitExpr(E);
1185 Writer.WriteSubStmt(E->getString());
1186 Writer.AddSourceLocation(E->getAtLoc(), Record);
1187 Code = pch::EXPR_OBJC_STRING_LITERAL;
1188}
1189
Chris Lattner80f83c62009-04-22 05:57:30 +00001190void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1191 VisitExpr(E);
1192 Writer.AddTypeRef(E->getEncodedType(), Record);
1193 Writer.AddSourceLocation(E->getAtLoc(), Record);
1194 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1195 Code = pch::EXPR_OBJC_ENCODE;
1196}
1197
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001198void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1199 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001200 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001201 Writer.AddSourceLocation(E->getAtLoc(), Record);
1202 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1203 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1204}
1205
1206void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1207 VisitExpr(E);
1208 Writer.AddDeclRef(E->getProtocol(), Record);
1209 Writer.AddSourceLocation(E->getAtLoc(), Record);
1210 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1211 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1212}
1213
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001214void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1215 VisitExpr(E);
1216 Writer.AddDeclRef(E->getDecl(), Record);
1217 Writer.AddSourceLocation(E->getLocation(), Record);
1218 Writer.WriteSubStmt(E->getBase());
1219 Record.push_back(E->isArrow());
1220 Record.push_back(E->isFreeIvar());
Steve Naroffa323e972009-04-26 14:11:39 +00001221 Code = pch::EXPR_OBJC_IVAR_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001222}
1223
1224void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1225 VisitExpr(E);
1226 Writer.AddDeclRef(E->getProperty(), Record);
1227 Writer.AddSourceLocation(E->getLocation(), Record);
1228 Writer.WriteSubStmt(E->getBase());
Steve Naroffa323e972009-04-26 14:11:39 +00001229 Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001230}
1231
1232void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1233 VisitExpr(E);
1234 Writer.AddDeclRef(E->getGetterMethod(), Record);
1235 Writer.AddDeclRef(E->getSetterMethod(), Record);
1236
1237 // NOTE: ClassProp and Base are mutually exclusive.
1238 Writer.AddDeclRef(E->getClassProp(), Record);
1239 Writer.WriteSubStmt(E->getBase());
1240 Writer.AddSourceLocation(E->getLocation(), Record);
1241 Writer.AddSourceLocation(E->getClassLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001242 Code = pch::EXPR_OBJC_KVC_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001243}
1244
Steve Narofffb3e4022009-04-25 14:04:28 +00001245void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1246 VisitExpr(E);
1247 Record.push_back(E->getNumArgs());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001248 Writer.AddSourceLocation(E->getLeftLoc(), Record);
1249 Writer.AddSourceLocation(E->getRightLoc(), Record);
Steve Narofffb3e4022009-04-25 14:04:28 +00001250 Writer.AddSelectorRef(E->getSelector(), Record);
1251 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001252
1253 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
Steve Narofffb3e4022009-04-25 14:04:28 +00001254 Writer.WriteSubStmt(E->getReceiver());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001255 Writer.AddDeclRef(CI.first, Record);
1256 Writer.AddIdentifierRef(CI.second, Record);
1257
Steve Narofffb3e4022009-04-25 14:04:28 +00001258 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1259 Arg != ArgEnd; ++Arg)
1260 Writer.WriteSubStmt(*Arg);
1261 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1262}
1263
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001264void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1265 VisitExpr(E);
1266 Writer.AddSourceLocation(E->getLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001267 Code = pch::EXPR_OBJC_SUPER_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001268}
1269
Steve Naroff79762bd2009-04-26 18:52:16 +00001270void PCHStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1271 VisitStmt(S);
1272 Writer.WriteSubStmt(S->getElement());
1273 Writer.WriteSubStmt(S->getCollection());
1274 Writer.WriteSubStmt(S->getBody());
1275 Writer.AddSourceLocation(S->getForLoc(), Record);
1276 Writer.AddSourceLocation(S->getRParenLoc(), Record);
1277 Code = pch::STMT_OBJC_FOR_COLLECTION;
1278}
1279
1280void PCHStmtWriter::VisitObjCCatchStmt(ObjCAtCatchStmt *S) {
1281 Writer.WriteSubStmt(S->getCatchBody());
1282 Writer.WriteSubStmt(S->getNextCatchStmt());
1283 Writer.AddDeclRef(S->getCatchParamDecl(), Record);
1284 Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
1285 Writer.AddSourceLocation(S->getRParenLoc(), Record);
1286 Code = pch::STMT_OBJC_CATCH;
1287}
1288
1289void PCHStmtWriter::VisitObjCFinallyStmt(ObjCAtFinallyStmt *S) {
1290 Writer.WriteSubStmt(S->getFinallyBody());
1291 Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
1292 Code = pch::STMT_OBJC_FINALLY;
1293}
1294
1295void PCHStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1296 Writer.WriteSubStmt(S->getTryBody());
1297 Writer.WriteSubStmt(S->getCatchStmts());
1298 Writer.WriteSubStmt(S->getFinallyStmt());
1299 Writer.AddSourceLocation(S->getAtTryLoc(), Record);
1300 Code = pch::STMT_OBJC_AT_TRY;
1301}
1302
1303void PCHStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1304 Writer.WriteSubStmt(S->getSynchExpr());
1305 Writer.WriteSubStmt(S->getSynchBody());
1306 Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
1307 Code = pch::STMT_OBJC_AT_SYNCHRONIZED;
1308}
1309
1310void PCHStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1311 Writer.WriteSubStmt(S->getThrowExpr());
1312 Writer.AddSourceLocation(S->getThrowLoc(), Record);
1313 Code = pch::STMT_OBJC_AT_THROW;
1314}
Chris Lattner80f83c62009-04-22 05:57:30 +00001315
1316//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001317// PCHWriter Implementation
1318//===----------------------------------------------------------------------===//
1319
Douglas Gregorb5887f32009-04-10 21:16:55 +00001320/// \brief Write the target triple (e.g., i686-apple-darwin9).
1321void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1322 using namespace llvm;
1323 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1324 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001326 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001327
1328 RecordData Record;
1329 Record.push_back(pch::TARGET_TRIPLE);
1330 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001331 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001332}
1333
1334/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001335void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1336 RecordData Record;
1337 Record.push_back(LangOpts.Trigraphs);
1338 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1339 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1340 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1341 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1342 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1343 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1344 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1345 Record.push_back(LangOpts.C99); // C99 Support
1346 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1347 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1348 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1349 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1350 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1351
1352 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1353 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1354 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1355
1356 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1357 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1358 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1359 Record.push_back(LangOpts.LaxVectorConversions);
1360 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1361
1362 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1363 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1364 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1365
1366 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1367 // by locks.
1368 Record.push_back(LangOpts.Blocks); // block extension to C
1369 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1370 // they are unused.
1371 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1372 // (modulo the platform support).
1373
1374 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1375 // signed integer arithmetic overflows.
1376
1377 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1378 // may be ripped out at any time.
1379
1380 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1381 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1382 // defined.
1383 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1384 // opposed to __DYNAMIC__).
1385 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1386
1387 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1388 // used (instead of C99 semantics).
1389 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1390 Record.push_back(LangOpts.getGCMode());
1391 Record.push_back(LangOpts.getVisibilityMode());
1392 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001393 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001394}
1395
Douglas Gregorab1cef72009-04-10 03:52:48 +00001396//===----------------------------------------------------------------------===//
1397// Source Manager Serialization
1398//===----------------------------------------------------------------------===//
1399
1400/// \brief Create an abbreviation for the SLocEntry that refers to a
1401/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001402static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001403 using namespace llvm;
1404 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1405 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001411 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001412}
1413
1414/// \brief Create an abbreviation for the SLocEntry that refers to a
1415/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001416static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001417 using namespace llvm;
1418 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1419 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001425 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001426}
1427
1428/// \brief Create an abbreviation for the SLocEntry that refers to a
1429/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001430static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001431 using namespace llvm;
1432 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1433 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001435 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001436}
1437
1438/// \brief Create an abbreviation for the SLocEntry that refers to an
1439/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001440static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001441 using namespace llvm;
1442 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1443 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001449 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001450}
1451
1452/// \brief Writes the block containing the serialized form of the
1453/// source manager.
1454///
1455/// TODO: We should probably use an on-disk hash table (stored in a
1456/// blob), indexed based on the file name, so that we only create
1457/// entries for files that we actually need. In the common case (no
1458/// errors), we probably won't have to create file entries for any of
1459/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001460void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1461 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001462 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001463 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001464
1465 // Abbreviations for the various kinds of source-location entries.
1466 int SLocFileAbbrv = -1;
1467 int SLocBufferAbbrv = -1;
1468 int SLocBufferBlobAbbrv = -1;
1469 int SLocInstantiationAbbrv = -1;
1470
1471 // Write out the source location entry table. We skip the first
1472 // entry, which is always the same dummy entry.
1473 RecordData Record;
1474 for (SourceManager::sloc_entry_iterator
1475 SLoc = SourceMgr.sloc_entry_begin() + 1,
1476 SLocEnd = SourceMgr.sloc_entry_end();
1477 SLoc != SLocEnd; ++SLoc) {
1478 // Figure out which record code to use.
1479 unsigned Code;
1480 if (SLoc->isFile()) {
1481 if (SLoc->getFile().getContentCache()->Entry)
1482 Code = pch::SM_SLOC_FILE_ENTRY;
1483 else
1484 Code = pch::SM_SLOC_BUFFER_ENTRY;
1485 } else
1486 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1487 Record.push_back(Code);
1488
1489 Record.push_back(SLoc->getOffset());
1490 if (SLoc->isFile()) {
1491 const SrcMgr::FileInfo &File = SLoc->getFile();
1492 Record.push_back(File.getIncludeLoc().getRawEncoding());
1493 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001494 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001495
1496 const SrcMgr::ContentCache *Content = File.getContentCache();
1497 if (Content->Entry) {
1498 // The source location entry is a file. The blob associated
1499 // with this entry is the file name.
1500 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001501 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1502 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001503 Content->Entry->getName(),
1504 strlen(Content->Entry->getName()));
1505 } else {
1506 // The source location entry is a buffer. The blob associated
1507 // with this entry contains the contents of the buffer.
1508 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001509 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1510 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001511 }
1512
1513 // We add one to the size so that we capture the trailing NULL
1514 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1515 // the reader side).
1516 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1517 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001518 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001519 Record.clear();
1520 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001521 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001522 Buffer->getBufferStart(),
1523 Buffer->getBufferSize() + 1);
1524 }
1525 } else {
1526 // The source location entry is an instantiation.
1527 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1528 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1529 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1530 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1531
Douglas Gregor364e5802009-04-15 18:05:10 +00001532 // Compute the token length for this macro expansion.
1533 unsigned NextOffset = SourceMgr.getNextOffset();
1534 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1535 if (++NextSLoc != SLocEnd)
1536 NextOffset = NextSLoc->getOffset();
1537 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1538
Douglas Gregorab1cef72009-04-10 03:52:48 +00001539 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001540 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1541 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001542 }
1543
1544 Record.clear();
1545 }
1546
Douglas Gregor635f97f2009-04-13 16:31:14 +00001547 // Write the line table.
1548 if (SourceMgr.hasLineTable()) {
1549 LineTableInfo &LineTable = SourceMgr.getLineTable();
1550
1551 // Emit the file names
1552 Record.push_back(LineTable.getNumFilenames());
1553 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1554 // Emit the file name
1555 const char *Filename = LineTable.getFilename(I);
1556 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1557 Record.push_back(FilenameLen);
1558 if (FilenameLen)
1559 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1560 }
1561
1562 // Emit the line entries
1563 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1564 L != LEnd; ++L) {
1565 // Emit the file ID
1566 Record.push_back(L->first);
1567
1568 // Emit the line entries
1569 Record.push_back(L->second.size());
1570 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1571 LEEnd = L->second.end();
1572 LE != LEEnd; ++LE) {
1573 Record.push_back(LE->FileOffset);
1574 Record.push_back(LE->LineNo);
1575 Record.push_back(LE->FilenameID);
1576 Record.push_back((unsigned)LE->FileKind);
1577 Record.push_back(LE->IncludeOffset);
1578 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001579 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001580 }
1581 }
1582
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001583 // Loop over all the header files.
1584 HeaderSearch &HS = PP.getHeaderSearchInfo();
1585 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1586 E = HS.header_file_end();
1587 I != E; ++I) {
1588 Record.push_back(I->isImport);
1589 Record.push_back(I->DirInfo);
1590 Record.push_back(I->NumIncludes);
1591 if (I->ControllingMacro)
1592 AddIdentifierRef(I->ControllingMacro, Record);
1593 else
1594 Record.push_back(0);
1595 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1596 Record.clear();
1597 }
1598
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001599 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001600}
1601
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001602/// \brief Writes the block containing the serialized form of the
1603/// preprocessor.
1604///
Chris Lattner850eabd2009-04-10 18:08:30 +00001605void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001606 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001607
Chris Lattner4b21c202009-04-13 01:29:17 +00001608 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1609 if (PP.getCounterValue() != 0) {
1610 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001611 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001612 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001613 }
1614
1615 // Enter the preprocessor block.
1616 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001617
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001618 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1619 // FIXME: use diagnostics subsystem for localization etc.
1620 if (PP.SawDateOrTime())
1621 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1622
Chris Lattner1b094952009-04-10 18:00:12 +00001623 // Loop over all the macro definitions that are live at the end of the file,
1624 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001625 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1626 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001627 // FIXME: This emits macros in hash table order, we should do it in a stable
1628 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001629 MacroInfo *MI = I->second;
1630
1631 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1632 // been redefined by the header (in which case they are not isBuiltinMacro).
1633 if (MI->isBuiltinMacro())
1634 continue;
1635
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001636 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001637 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001638 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001639 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1640 Record.push_back(MI->isUsed());
1641
1642 unsigned Code;
1643 if (MI->isObjectLike()) {
1644 Code = pch::PP_MACRO_OBJECT_LIKE;
1645 } else {
1646 Code = pch::PP_MACRO_FUNCTION_LIKE;
1647
1648 Record.push_back(MI->isC99Varargs());
1649 Record.push_back(MI->isGNUVarargs());
1650 Record.push_back(MI->getNumArgs());
1651 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1652 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001653 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001654 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001655 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001656 Record.clear();
1657
Chris Lattner850eabd2009-04-10 18:08:30 +00001658 // Emit the tokens array.
1659 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1660 // Note that we know that the preprocessor does not have any annotation
1661 // tokens in it because they are created by the parser, and thus can't be
1662 // in a macro definition.
1663 const Token &Tok = MI->getReplacementToken(TokNo);
1664
1665 Record.push_back(Tok.getLocation().getRawEncoding());
1666 Record.push_back(Tok.getLength());
1667
Chris Lattner850eabd2009-04-10 18:08:30 +00001668 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1669 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001670 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001671
1672 // FIXME: Should translate token kind to a stable encoding.
1673 Record.push_back(Tok.getKind());
1674 // FIXME: Should translate token flags to a stable encoding.
1675 Record.push_back(Tok.getFlags());
1676
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001677 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001678 Record.clear();
1679 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001680 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001681 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001682 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001683}
1684
1685
Douglas Gregorc34897d2009-04-09 22:27:44 +00001686/// \brief Write the representation of a type to the PCH stream.
1687void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001688 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001689 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001690 ID = NextTypeID++;
1691
1692 // Record the offset for this type.
1693 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001694 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001695 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1696 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001697 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001698 }
1699
1700 RecordData Record;
1701
1702 // Emit the type's representation.
1703 PCHTypeWriter W(*this, Record);
1704 switch (T->getTypeClass()) {
1705 // For all of the concrete, non-dependent types, call the
1706 // appropriate visitor function.
1707#define TYPE(Class, Base) \
1708 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1709#define ABSTRACT_TYPE(Class, Base)
1710#define DEPENDENT_TYPE(Class, Base)
1711#include "clang/AST/TypeNodes.def"
1712
1713 // For all of the dependent type nodes (which only occur in C++
1714 // templates), produce an error.
1715#define TYPE(Class, Base)
1716#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1717#include "clang/AST/TypeNodes.def"
1718 assert(false && "Cannot serialize dependent type nodes");
1719 break;
1720 }
1721
1722 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001723 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001724
1725 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001726 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001727}
1728
1729/// \brief Write a block containing all of the types.
1730void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001731 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001732 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001733
Douglas Gregore43f0972009-04-26 03:49:13 +00001734 // Emit all of the types that need to be emitted (so far).
1735 while (!TypesToEmit.empty()) {
1736 const Type *T = TypesToEmit.front();
1737 TypesToEmit.pop();
1738 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1739 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001740 }
1741
1742 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001743 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001744}
1745
1746/// \brief Write the block containing all of the declaration IDs
1747/// lexically declared within the given DeclContext.
1748///
1749/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1750/// bistream, or 0 if no block was written.
1751uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1752 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001753 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001754 return 0;
1755
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001756 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001757 RecordData Record;
1758 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1759 DEnd = DC->decls_end(Context);
1760 D != DEnd; ++D)
1761 AddDeclRef(*D, Record);
1762
Douglas Gregoraf136d92009-04-22 22:34:57 +00001763 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001764 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001765 return Offset;
1766}
1767
1768/// \brief Write the block containing all of the declaration IDs
1769/// visible from the given DeclContext.
1770///
1771/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1772/// bistream, or 0 if no block was written.
1773uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1774 DeclContext *DC) {
1775 if (DC->getPrimaryContext() != DC)
1776 return 0;
1777
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001778 // Since there is no name lookup into functions or methods, and we
1779 // perform name lookup for the translation unit via the
1780 // IdentifierInfo chains, don't bother to build a
1781 // visible-declarations table for these entities.
1782 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001783 return 0;
1784
Douglas Gregorc34897d2009-04-09 22:27:44 +00001785 // Force the DeclContext to build a its name-lookup table.
1786 DC->lookup(Context, DeclarationName());
1787
1788 // Serialize the contents of the mapping used for lookup. Note that,
1789 // although we have two very different code paths, the serialized
1790 // representation is the same for both cases: a declaration name,
1791 // followed by a size, followed by references to the visible
1792 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001793 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001794 RecordData Record;
1795 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001796 if (!Map)
1797 return 0;
1798
Douglas Gregorc34897d2009-04-09 22:27:44 +00001799 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1800 D != DEnd; ++D) {
1801 AddDeclarationName(D->first, Record);
1802 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1803 Record.push_back(Result.second - Result.first);
1804 for(; Result.first != Result.second; ++Result.first)
1805 AddDeclRef(*Result.first, Record);
1806 }
1807
1808 if (Record.size() == 0)
1809 return 0;
1810
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001811 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001812 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001813 return Offset;
1814}
1815
1816/// \brief Write a block containing all of the declarations.
1817void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001818 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001819 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001820
1821 // Emit all of the declarations.
1822 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001823 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001824 while (!DeclsToEmit.empty()) {
1825 // Pull the next declaration off the queue
1826 Decl *D = DeclsToEmit.front();
1827 DeclsToEmit.pop();
1828
1829 // If this declaration is also a DeclContext, write blocks for the
1830 // declarations that lexically stored inside its context and those
1831 // declarations that are visible from its context. These blocks
1832 // are written before the declaration itself so that we can put
1833 // their offsets into the record for the declaration.
1834 uint64_t LexicalOffset = 0;
1835 uint64_t VisibleOffset = 0;
1836 DeclContext *DC = dyn_cast<DeclContext>(D);
1837 if (DC) {
1838 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1839 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1840 }
1841
1842 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001843 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001844 if (ID == 0)
1845 ID = DeclIDs.size();
1846
1847 unsigned Index = ID - 1;
1848
1849 // Record the offset for this declaration
1850 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001851 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001852 else if (DeclOffsets.size() < Index) {
1853 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001854 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001855 }
1856
1857 // Build and emit a record for this declaration
1858 Record.clear();
1859 W.Code = (pch::DeclCode)0;
1860 W.Visit(D);
1861 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00001862
1863 if (!W.Code) {
1864 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
1865 D->getDeclKindName());
1866 assert(false && "Unhandled declaration kind while generating PCH");
1867 exit(-1);
1868 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001869 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001870
Douglas Gregor1c507882009-04-15 21:30:51 +00001871 // If the declaration had any attributes, write them now.
1872 if (D->hasAttrs())
1873 WriteAttributeRecord(D->getAttrs());
1874
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001875 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001876 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001877
Douglas Gregor631f6c62009-04-14 00:24:19 +00001878 // Note external declarations so that we can add them to a record
1879 // in the PCH file later.
1880 if (isa<FileScopeAsmDecl>(D))
1881 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001882 }
1883
1884 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001885 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001886}
1887
Douglas Gregorff9a6092009-04-20 20:36:09 +00001888namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001889// Trait used for the on-disk hash table used in the method pool.
1890class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1891 PCHWriter &Writer;
1892
1893public:
1894 typedef Selector key_type;
1895 typedef key_type key_type_ref;
1896
1897 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1898 typedef const data_type& data_type_ref;
1899
1900 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1901
1902 static unsigned ComputeHash(Selector Sel) {
1903 unsigned N = Sel.getNumArgs();
1904 if (N == 0)
1905 ++N;
1906 unsigned R = 5381;
1907 for (unsigned I = 0; I != N; ++I)
1908 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1909 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1910 return R;
1911 }
1912
1913 std::pair<unsigned,unsigned>
1914 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1915 data_type_ref Methods) {
1916 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1917 clang::io::Emit16(Out, KeyLen);
1918 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1919 for (const ObjCMethodList *Method = &Methods.first; Method;
1920 Method = Method->Next)
1921 if (Method->Method)
1922 DataLen += 4;
1923 for (const ObjCMethodList *Method = &Methods.second; Method;
1924 Method = Method->Next)
1925 if (Method->Method)
1926 DataLen += 4;
1927 clang::io::Emit16(Out, DataLen);
1928 return std::make_pair(KeyLen, DataLen);
1929 }
1930
Douglas Gregor2d711832009-04-25 17:48:32 +00001931 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1932 uint64_t Start = Out.tell();
1933 assert((Start >> 32) == 0 && "Selector key offset too large");
1934 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001935 unsigned N = Sel.getNumArgs();
1936 clang::io::Emit16(Out, N);
1937 if (N == 0)
1938 N = 1;
1939 for (unsigned I = 0; I != N; ++I)
1940 clang::io::Emit32(Out,
1941 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1942 }
1943
1944 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001945 data_type_ref Methods, unsigned DataLen) {
1946 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001947 unsigned NumInstanceMethods = 0;
1948 for (const ObjCMethodList *Method = &Methods.first; Method;
1949 Method = Method->Next)
1950 if (Method->Method)
1951 ++NumInstanceMethods;
1952
1953 unsigned NumFactoryMethods = 0;
1954 for (const ObjCMethodList *Method = &Methods.second; Method;
1955 Method = Method->Next)
1956 if (Method->Method)
1957 ++NumFactoryMethods;
1958
1959 clang::io::Emit16(Out, NumInstanceMethods);
1960 clang::io::Emit16(Out, NumFactoryMethods);
1961 for (const ObjCMethodList *Method = &Methods.first; Method;
1962 Method = Method->Next)
1963 if (Method->Method)
1964 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001965 for (const ObjCMethodList *Method = &Methods.second; Method;
1966 Method = Method->Next)
1967 if (Method->Method)
1968 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001969
1970 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001971 }
1972};
1973} // end anonymous namespace
1974
1975/// \brief Write the method pool into the PCH file.
1976///
1977/// The method pool contains both instance and factory methods, stored
1978/// in an on-disk hash table indexed by the selector.
1979void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1980 using namespace llvm;
1981
1982 // Create and write out the blob that contains the instance and
1983 // factor method pools.
1984 bool Empty = true;
1985 {
1986 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1987
1988 // Create the on-disk hash table representation. Start by
1989 // iterating through the instance method pool.
1990 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001991 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001992 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1993 Instance = SemaRef.InstanceMethodPool.begin(),
1994 InstanceEnd = SemaRef.InstanceMethodPool.end();
1995 Instance != InstanceEnd; ++Instance) {
1996 // Check whether there is a factory method with the same
1997 // selector.
1998 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1999 = SemaRef.FactoryMethodPool.find(Instance->first);
2000
2001 if (Factory == SemaRef.FactoryMethodPool.end())
2002 Generator.insert(Instance->first,
2003 std::make_pair(Instance->second,
2004 ObjCMethodList()));
2005 else
2006 Generator.insert(Instance->first,
2007 std::make_pair(Instance->second, Factory->second));
2008
Douglas Gregor2d711832009-04-25 17:48:32 +00002009 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002010 Empty = false;
2011 }
2012
2013 // Now iterate through the factory method pool, to pick up any
2014 // selectors that weren't already in the instance method pool.
2015 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2016 Factory = SemaRef.FactoryMethodPool.begin(),
2017 FactoryEnd = SemaRef.FactoryMethodPool.end();
2018 Factory != FactoryEnd; ++Factory) {
2019 // Check whether there is an instance method with the same
2020 // selector. If so, there is no work to do here.
2021 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
2022 = SemaRef.InstanceMethodPool.find(Factory->first);
2023
Douglas Gregor2d711832009-04-25 17:48:32 +00002024 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002025 Generator.insert(Factory->first,
2026 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00002027 ++NumSelectorsInMethodPool;
2028 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002029
2030 Empty = false;
2031 }
2032
Douglas Gregor2d711832009-04-25 17:48:32 +00002033 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002034 return;
2035
2036 // Create the on-disk hash table in a buffer.
2037 llvm::SmallVector<char, 4096> MethodPool;
2038 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00002039 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002040 {
2041 PCHMethodPoolTrait Trait(*this);
2042 llvm::raw_svector_ostream Out(MethodPool);
2043 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002044 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002045 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00002046
2047 // For every selector that we have seen but which was not
2048 // written into the hash table, write the selector itself and
2049 // record it's offset.
2050 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
2051 if (SelectorOffsets[I] == 0)
2052 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002053 }
2054
2055 // Create a blob abbreviation
2056 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2057 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2061 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2062
Douglas Gregor2d711832009-04-25 17:48:32 +00002063 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002064 RecordData Record;
2065 Record.push_back(pch::METHOD_POOL);
2066 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002067 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002068 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2069 &MethodPool.front(),
2070 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002071
2072 // Create a blob abbreviation for the selector table offsets.
2073 Abbrev = new BitCodeAbbrev();
2074 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2077 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2078
2079 // Write the selector offsets table.
2080 Record.clear();
2081 Record.push_back(pch::SELECTOR_OFFSETS);
2082 Record.push_back(SelectorOffsets.size());
2083 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2084 (const char *)&SelectorOffsets.front(),
2085 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002086 }
2087}
2088
2089namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002090class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2091 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002092 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002093
2094public:
2095 typedef const IdentifierInfo* key_type;
2096 typedef key_type key_type_ref;
2097
2098 typedef pch::IdentID data_type;
2099 typedef data_type data_type_ref;
2100
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002101 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2102 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002103
2104 static unsigned ComputeHash(const IdentifierInfo* II) {
2105 return clang::BernsteinHash(II->getName());
2106 }
2107
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002108 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002109 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2110 pch::IdentID ID) {
2111 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002112 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2113 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002114 if (II->hasMacroDefinition() &&
2115 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2116 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002117 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2118 DEnd = IdentifierResolver::end();
2119 D != DEnd; ++D)
2120 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002121 // We emit the key length after the data length so that the
2122 // "uninteresting" identifiers following the identifier hash table
2123 // structure will have the same (key length, key characters)
2124 // layout as the keys in the hash table. This also matches the
2125 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002126 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002127 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002128 return std::make_pair(KeyLen, DataLen);
2129 }
2130
2131 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2132 unsigned KeyLen) {
2133 // Record the location of the key data. This is used when generating
2134 // the mapping from persistent IDs to strings.
2135 Writer.SetIdentifierOffset(II, Out.tell());
2136 Out.write(II->getName(), KeyLen);
2137 }
2138
2139 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2140 pch::IdentID ID, unsigned) {
2141 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002142 bool hasMacroDefinition =
2143 II->hasMacroDefinition() &&
2144 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002145 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002146 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2147 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002148 Bits = (Bits << 1) | II->isExtensionToken();
2149 Bits = (Bits << 1) | II->isPoisoned();
2150 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2151 clang::io::Emit32(Out, Bits);
2152 clang::io::Emit32(Out, ID);
2153
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002154 if (hasMacroDefinition)
2155 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2156
Douglas Gregorc713da92009-04-21 22:25:48 +00002157 // Emit the declaration IDs in reverse order, because the
2158 // IdentifierResolver provides the declarations as they would be
2159 // visible (e.g., the function "stat" would come before the struct
2160 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2161 // adds declarations to the end of the list (so we need to see the
2162 // struct "status" before the function "status").
2163 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2164 IdentifierResolver::end());
2165 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2166 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002167 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002168 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002169 }
2170};
2171} // end anonymous namespace
2172
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002173/// \brief Write the identifier table into the PCH file.
2174///
2175/// The identifier table consists of a blob containing string data
2176/// (the actual identifiers themselves) and a separate "offsets" index
2177/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002178void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002179 using namespace llvm;
2180
2181 // Create and write out the blob that contains the identifier
2182 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002183 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002184 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002185 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2186
Douglas Gregor85c4a872009-04-25 21:04:17 +00002187 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2188
Douglas Gregorff9a6092009-04-20 20:36:09 +00002189 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002190 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2191 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2192 ID != IDEnd; ++ID) {
2193 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002194
2195 // Classify each identifier as either "interesting" or "not
2196 // interesting". Interesting identifiers are those that have
2197 // additional information that needs to be read from the PCH
2198 // file, e.g., a built-in ID, declaration chain, or macro
2199 // definition. These identifiers are placed into the hash table
2200 // so that they can be found when looked up in the user program.
2201 // All other identifiers are "uninteresting", which means that
2202 // the IdentifierInfo built by default has all of the
2203 // information we care about. Such identifiers are placed after
2204 // the hash table.
2205 const IdentifierInfo *II = ID->first;
2206 if (II->isPoisoned() ||
2207 II->isExtensionToken() ||
2208 II->hasMacroDefinition() ||
2209 II->getObjCOrBuiltinID() ||
2210 II->getFETokenInfo<void>())
2211 Generator.insert(ID->first, ID->second);
2212 else
2213 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002214 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002215
Douglas Gregorff9a6092009-04-20 20:36:09 +00002216 // Create the on-disk hash table in a buffer.
2217 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002218 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002219 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002220 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002221 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002222 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002223 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002224 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002225
2226 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2227 const IdentifierInfo *II = UninterestingIdentifiers[I];
2228 unsigned N = II->getLength() + 1;
2229 clang::io::Emit16(Out, N);
2230 SetIdentifierOffset(II, Out.tell());
2231 Out.write(II->getName(), N);
2232 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002233 }
2234
2235 // Create a blob abbreviation
2236 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2237 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002240 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002241
2242 // Write the identifier table
2243 RecordData Record;
2244 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002245 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002246 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2247 &IdentifierTable.front(),
2248 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002249 }
2250
2251 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2253 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2256 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2257
2258 RecordData Record;
2259 Record.push_back(pch::IDENTIFIER_OFFSET);
2260 Record.push_back(IdentifierOffsets.size());
2261 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2262 (const char *)&IdentifierOffsets.front(),
2263 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002264}
2265
Douglas Gregor1c507882009-04-15 21:30:51 +00002266/// \brief Write a record containing the given attributes.
2267void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2268 RecordData Record;
2269 for (; Attr; Attr = Attr->getNext()) {
2270 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2271 Record.push_back(Attr->isInherited());
2272 switch (Attr->getKind()) {
2273 case Attr::Alias:
2274 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2275 break;
2276
2277 case Attr::Aligned:
2278 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2279 break;
2280
2281 case Attr::AlwaysInline:
2282 break;
2283
2284 case Attr::AnalyzerNoReturn:
2285 break;
2286
2287 case Attr::Annotate:
2288 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2289 break;
2290
2291 case Attr::AsmLabel:
2292 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2293 break;
2294
2295 case Attr::Blocks:
2296 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2297 break;
2298
2299 case Attr::Cleanup:
2300 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2301 break;
2302
2303 case Attr::Const:
2304 break;
2305
2306 case Attr::Constructor:
2307 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2308 break;
2309
2310 case Attr::DLLExport:
2311 case Attr::DLLImport:
2312 case Attr::Deprecated:
2313 break;
2314
2315 case Attr::Destructor:
2316 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2317 break;
2318
2319 case Attr::FastCall:
2320 break;
2321
2322 case Attr::Format: {
2323 const FormatAttr *Format = cast<FormatAttr>(Attr);
2324 AddString(Format->getType(), Record);
2325 Record.push_back(Format->getFormatIdx());
2326 Record.push_back(Format->getFirstArg());
2327 break;
2328 }
2329
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002330 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002331 case Attr::IBOutletKind:
2332 case Attr::NoReturn:
2333 case Attr::NoThrow:
2334 case Attr::Nodebug:
2335 case Attr::Noinline:
2336 break;
2337
2338 case Attr::NonNull: {
2339 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2340 Record.push_back(NonNull->size());
2341 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2342 break;
2343 }
2344
2345 case Attr::ObjCException:
2346 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002347 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002348 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002349 case Attr::Overloadable:
2350 break;
2351
2352 case Attr::Packed:
2353 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2354 break;
2355
2356 case Attr::Pure:
2357 break;
2358
2359 case Attr::Regparm:
2360 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2361 break;
2362
2363 case Attr::Section:
2364 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2365 break;
2366
2367 case Attr::StdCall:
2368 case Attr::TransparentUnion:
2369 case Attr::Unavailable:
2370 case Attr::Unused:
2371 case Attr::Used:
2372 break;
2373
2374 case Attr::Visibility:
2375 // FIXME: stable encoding
2376 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2377 break;
2378
2379 case Attr::WarnUnusedResult:
2380 case Attr::Weak:
2381 case Attr::WeakImport:
2382 break;
2383 }
2384 }
2385
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002386 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002387}
2388
2389void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2390 Record.push_back(Str.size());
2391 Record.insert(Record.end(), Str.begin(), Str.end());
2392}
2393
Douglas Gregorff9a6092009-04-20 20:36:09 +00002394/// \brief Note that the identifier II occurs at the given offset
2395/// within the identifier table.
2396void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002397 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002398}
2399
Douglas Gregor2d711832009-04-25 17:48:32 +00002400/// \brief Note that the selector Sel occurs at the given offset
2401/// within the method pool/selector table.
2402void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2403 unsigned ID = SelectorIDs[Sel];
2404 assert(ID && "Unknown selector");
2405 SelectorOffsets[ID - 1] = Offset;
2406}
2407
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002408PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002409 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002410 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2411 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002412
Douglas Gregor87887da2009-04-20 15:53:59 +00002413void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002414 using namespace llvm;
2415
Douglas Gregor87887da2009-04-20 15:53:59 +00002416 ASTContext &Context = SemaRef.Context;
2417 Preprocessor &PP = SemaRef.PP;
2418
Douglas Gregorc34897d2009-04-09 22:27:44 +00002419 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002420 Stream.Emit((unsigned)'C', 8);
2421 Stream.Emit((unsigned)'P', 8);
2422 Stream.Emit((unsigned)'C', 8);
2423 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002424
2425 // The translation unit is the first declaration we'll emit.
2426 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2427 DeclsToEmit.push(Context.getTranslationUnitDecl());
2428
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002429 // Make sure that we emit IdentifierInfos (and any attached
2430 // declarations) for builtins.
2431 {
2432 IdentifierTable &Table = PP.getIdentifierTable();
2433 llvm::SmallVector<const char *, 32> BuiltinNames;
2434 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2435 Context.getLangOptions().NoBuiltin);
2436 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2437 getIdentifierRef(&Table.get(BuiltinNames[I]));
2438 }
2439
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002440 // Build a record containing all of the tentative definitions in
2441 // this header file. Generally, this record will be empty.
2442 RecordData TentativeDefinitions;
2443 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2444 TD = SemaRef.TentativeDefinitions.begin(),
2445 TDEnd = SemaRef.TentativeDefinitions.end();
2446 TD != TDEnd; ++TD)
2447 AddDeclRef(TD->second, TentativeDefinitions);
2448
Douglas Gregor062d9482009-04-22 22:18:58 +00002449 // Build a record containing all of the locally-scoped external
2450 // declarations in this header file. Generally, this record will be
2451 // empty.
2452 RecordData LocallyScopedExternalDecls;
2453 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2454 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2455 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2456 TD != TDEnd; ++TD)
2457 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2458
Douglas Gregorc34897d2009-04-09 22:27:44 +00002459 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002460 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002461 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002462 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002463 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002464 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002465 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002466
2467 // Keep writing types and declarations until all types and
2468 // declarations have been written.
2469 do {
2470 if (!DeclsToEmit.empty())
2471 WriteDeclsBlock(Context);
2472 if (!TypesToEmit.empty())
2473 WriteTypesBlock(Context);
2474 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2475
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002476 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002477 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002478
2479 // Write the type offsets array
2480 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2481 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2482 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2484 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2485 Record.clear();
2486 Record.push_back(pch::TYPE_OFFSET);
2487 Record.push_back(TypeOffsets.size());
2488 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2489 (const char *)&TypeOffsets.front(),
2490 TypeOffsets.size() * sizeof(uint64_t));
2491
2492 // Write the declaration offsets array
2493 Abbrev = new BitCodeAbbrev();
2494 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2497 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2498 Record.clear();
2499 Record.push_back(pch::DECL_OFFSET);
2500 Record.push_back(DeclOffsets.size());
2501 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2502 (const char *)&DeclOffsets.front(),
2503 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002504
2505 // Write the record of special types.
2506 Record.clear();
2507 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002508 AddTypeRef(Context.getObjCIdType(), Record);
2509 AddTypeRef(Context.getObjCSelType(), Record);
2510 AddTypeRef(Context.getObjCProtoType(), Record);
2511 AddTypeRef(Context.getObjCClassType(), Record);
2512 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2513 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002514 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2515
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002516 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002517 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002518 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002519
2520 // Write the record containing tentative definitions.
2521 if (!TentativeDefinitions.empty())
2522 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002523
2524 // Write the record containing locally-scoped external definitions.
2525 if (!LocallyScopedExternalDecls.empty())
2526 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2527 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002528
2529 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002530 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002531 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002532 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002533 Record.push_back(NumLexicalDeclContexts);
2534 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002535 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002536 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002537}
2538
2539void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2540 Record.push_back(Loc.getRawEncoding());
2541}
2542
2543void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2544 Record.push_back(Value.getBitWidth());
2545 unsigned N = Value.getNumWords();
2546 const uint64_t* Words = Value.getRawData();
2547 for (unsigned I = 0; I != N; ++I)
2548 Record.push_back(Words[I]);
2549}
2550
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002551void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2552 Record.push_back(Value.isUnsigned());
2553 AddAPInt(Value, Record);
2554}
2555
Douglas Gregore2f37202009-04-14 21:55:33 +00002556void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2557 AddAPInt(Value.bitcastToAPInt(), Record);
2558}
2559
Douglas Gregorc34897d2009-04-09 22:27:44 +00002560void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002561 Record.push_back(getIdentifierRef(II));
2562}
2563
2564pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2565 if (II == 0)
2566 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002567
2568 pch::IdentID &ID = IdentifierIDs[II];
2569 if (ID == 0)
2570 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002571 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002572}
2573
Steve Naroff9e84d782009-04-23 10:39:46 +00002574void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2575 if (SelRef.getAsOpaquePtr() == 0) {
2576 Record.push_back(0);
2577 return;
2578 }
2579
2580 pch::SelectorID &SID = SelectorIDs[SelRef];
2581 if (SID == 0) {
2582 SID = SelectorIDs.size();
2583 SelVector.push_back(SelRef);
2584 }
2585 Record.push_back(SID);
2586}
2587
Douglas Gregorc34897d2009-04-09 22:27:44 +00002588void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2589 if (T.isNull()) {
2590 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2591 return;
2592 }
2593
2594 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002595 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002596 switch (BT->getKind()) {
2597 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2598 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2599 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2600 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2601 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2602 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2603 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2604 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2605 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2606 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2607 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2608 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2609 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2610 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2611 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2612 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2613 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2614 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2615 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2616 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2617 }
2618
2619 Record.push_back((ID << 3) | T.getCVRQualifiers());
2620 return;
2621 }
2622
Douglas Gregorac8f2802009-04-10 17:25:41 +00002623 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002624 if (ID == 0) {
2625 // We haven't seen this type before. Assign it a new ID and put it
2626 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002627 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002628 TypesToEmit.push(T.getTypePtr());
2629 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002630
2631 // Encode the type qualifiers in the type reference.
2632 Record.push_back((ID << 3) | T.getCVRQualifiers());
2633}
2634
2635void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2636 if (D == 0) {
2637 Record.push_back(0);
2638 return;
2639 }
2640
Douglas Gregorac8f2802009-04-10 17:25:41 +00002641 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002642 if (ID == 0) {
2643 // We haven't seen this declaration before. Give it a new ID and
2644 // enqueue it in the list of declarations to emit.
2645 ID = DeclIDs.size();
2646 DeclsToEmit.push(const_cast<Decl *>(D));
2647 }
2648
2649 Record.push_back(ID);
2650}
2651
Douglas Gregorff9a6092009-04-20 20:36:09 +00002652pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2653 if (D == 0)
2654 return 0;
2655
2656 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2657 return DeclIDs[D];
2658}
2659
Douglas Gregorc34897d2009-04-09 22:27:44 +00002660void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2661 Record.push_back(Name.getNameKind());
2662 switch (Name.getNameKind()) {
2663 case DeclarationName::Identifier:
2664 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2665 break;
2666
2667 case DeclarationName::ObjCZeroArgSelector:
2668 case DeclarationName::ObjCOneArgSelector:
2669 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002670 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002671 break;
2672
2673 case DeclarationName::CXXConstructorName:
2674 case DeclarationName::CXXDestructorName:
2675 case DeclarationName::CXXConversionFunctionName:
2676 AddTypeRef(Name.getCXXNameType(), Record);
2677 break;
2678
2679 case DeclarationName::CXXOperatorName:
2680 Record.push_back(Name.getCXXOverloadedOperator());
2681 break;
2682
2683 case DeclarationName::CXXUsingDirective:
2684 // No extra data to emit
2685 break;
2686 }
2687}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002688
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002689/// \brief Write the given substatement or subexpression to the
2690/// bitstream.
2691void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002692 RecordData Record;
2693 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002694 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002695
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002696 if (!S) {
2697 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002698 return;
2699 }
2700
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002701 Writer.Code = pch::STMT_NULL_PTR;
2702 Writer.Visit(S);
2703 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002704 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002705 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002706}
2707
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002708/// \brief Flush all of the statements that have been added to the
2709/// queue via AddStmt().
2710void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002711 RecordData Record;
2712 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002713
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002714 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002715 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002716 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002717
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002718 if (!S) {
2719 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002720 continue;
2721 }
2722
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002723 Writer.Code = pch::STMT_NULL_PTR;
2724 Writer.Visit(S);
2725 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002726 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002727 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002728
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002729 assert(N == StmtsToEmit.size() &&
2730 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002731
2732 // Note that we are at the end of a full expression. Any
2733 // expression records that follow this one are part of a different
2734 // expression.
2735 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002736 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002737 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002738
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002739 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002740 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002741}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002742
2743unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2744 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2745 "SwitchCase recorded twice");
2746 unsigned NextID = SwitchCaseIDs.size();
2747 SwitchCaseIDs[S] = NextID;
2748 return NextID;
2749}
2750
2751unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2752 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2753 "SwitchCase hasn't been seen yet");
2754 return SwitchCaseIDs[S];
2755}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002756
2757/// \brief Retrieve the ID for the given label statement, which may
2758/// or may not have been emitted yet.
2759unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2760 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2761 if (Pos != LabelIDs.end())
2762 return Pos->second;
2763
2764 unsigned NextID = LabelIDs.size();
2765 LabelIDs[S] = NextID;
2766 return NextID;
2767}