blob: 408fa60d70c9cc60eee06f17fdcf3a2d1257129c [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
20#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000032#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000034#include "llvm/Bitcode/BitstreamWriter.h"
35#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000036#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000037#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000038using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// Type serialization
42//===----------------------------------------------------------------------===//
43namespace {
44 class VISIBILITY_HIDDEN PCHTypeWriter {
45 PCHWriter &Writer;
46 PCHWriter::RecordData &Record;
47
48 public:
49 /// \brief Type code that corresponds to the record generated.
50 pch::TypeCode Code;
51
52 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
53 : Writer(Writer), Record(Record) { }
54
55 void VisitArrayType(const ArrayType *T);
56 void VisitFunctionType(const FunctionType *T);
57 void VisitTagType(const TagType *T);
58
59#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
60#define ABSTRACT_TYPE(Class, Base)
61#define DEPENDENT_TYPE(Class, Base)
62#include "clang/AST/TypeNodes.def"
63 };
64}
65
66void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
67 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
68 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
69 Record.push_back(T->getAddressSpace());
70 Code = pch::TYPE_EXT_QUAL;
71}
72
73void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
74 assert(false && "Built-in types are never serialized");
75}
76
77void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
78 Record.push_back(T->getWidth());
79 Record.push_back(T->isSigned());
80 Code = pch::TYPE_FIXED_WIDTH_INT;
81}
82
83void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
84 Writer.AddTypeRef(T->getElementType(), Record);
85 Code = pch::TYPE_COMPLEX;
86}
87
88void PCHTypeWriter::VisitPointerType(const PointerType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_POINTER;
91}
92
93void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_BLOCK_POINTER;
96}
97
98void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_LVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Code = pch::TYPE_RVALUE_REFERENCE;
106}
107
108void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
109 Writer.AddTypeRef(T->getPointeeType(), Record);
110 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
111 Code = pch::TYPE_MEMBER_POINTER;
112}
113
114void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
115 Writer.AddTypeRef(T->getElementType(), Record);
116 Record.push_back(T->getSizeModifier()); // FIXME: stable values
117 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
118}
119
120void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
121 VisitArrayType(T);
122 Writer.AddAPInt(T->getSize(), Record);
123 Code = pch::TYPE_CONSTANT_ARRAY;
124}
125
126void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
127 VisitArrayType(T);
128 Code = pch::TYPE_INCOMPLETE_ARRAY;
129}
130
131void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
132 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000133 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000134 Code = pch::TYPE_VARIABLE_ARRAY;
135}
136
137void PCHTypeWriter::VisitVectorType(const VectorType *T) {
138 Writer.AddTypeRef(T->getElementType(), Record);
139 Record.push_back(T->getNumElements());
140 Code = pch::TYPE_VECTOR;
141}
142
143void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
144 VisitVectorType(T);
145 Code = pch::TYPE_EXT_VECTOR;
146}
147
148void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
149 Writer.AddTypeRef(T->getResultType(), Record);
150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
164 Code = pch::TYPE_FUNCTION_PROTO;
165}
166
167void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
168 Writer.AddDeclRef(T->getDecl(), Record);
169 Code = pch::TYPE_TYPEDEF;
170}
171
172void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000173 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000174 Code = pch::TYPE_TYPEOF_EXPR;
175}
176
177void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
178 Writer.AddTypeRef(T->getUnderlyingType(), Record);
179 Code = pch::TYPE_TYPEOF;
180}
181
182void PCHTypeWriter::VisitTagType(const TagType *T) {
183 Writer.AddDeclRef(T->getDecl(), Record);
184 assert(!T->isBeingDefined() &&
185 "Cannot serialize in the middle of a type definition");
186}
187
188void PCHTypeWriter::VisitRecordType(const RecordType *T) {
189 VisitTagType(T);
190 Code = pch::TYPE_RECORD;
191}
192
193void PCHTypeWriter::VisitEnumType(const EnumType *T) {
194 VisitTagType(T);
195 Code = pch::TYPE_ENUM;
196}
197
198void
199PCHTypeWriter::VisitTemplateSpecializationType(
200 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000201 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000202 assert(false && "Cannot serialize template specialization types");
203}
204
205void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000206 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000207 assert(false && "Cannot serialize qualified name types");
208}
209
210void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
211 Writer.AddDeclRef(T->getDecl(), Record);
212 Code = pch::TYPE_OBJC_INTERFACE;
213}
214
215void
216PCHTypeWriter::VisitObjCQualifiedInterfaceType(
217 const ObjCQualifiedInterfaceType *T) {
218 VisitObjCInterfaceType(T);
219 Record.push_back(T->getNumProtocols());
220 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
221 Writer.AddDeclRef(T->getProtocol(I), Record);
222 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
223}
224
225void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
226 Record.push_back(T->getNumProtocols());
227 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
228 Writer.AddDeclRef(T->getProtocols(I), Record);
229 Code = pch::TYPE_OBJC_QUALIFIED_ID;
230}
231
Douglas Gregorc34897d2009-04-09 22:27:44 +0000232//===----------------------------------------------------------------------===//
233// Declaration serialization
234//===----------------------------------------------------------------------===//
235namespace {
236 class VISIBILITY_HIDDEN PCHDeclWriter
237 : public DeclVisitor<PCHDeclWriter, void> {
238
239 PCHWriter &Writer;
Douglas Gregore3241e92009-04-18 00:02:19 +0000240 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000241 PCHWriter::RecordData &Record;
242
243 public:
244 pch::DeclCode Code;
245
Douglas Gregore3241e92009-04-18 00:02:19 +0000246 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
247 PCHWriter::RecordData &Record)
248 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000249
250 void VisitDecl(Decl *D);
251 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
252 void VisitNamedDecl(NamedDecl *D);
253 void VisitTypeDecl(TypeDecl *D);
254 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000255 void VisitTagDecl(TagDecl *D);
256 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000257 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000258 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000259 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000260 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000261 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000262 void VisitVarDecl(VarDecl *D);
Douglas Gregorce066712009-04-26 22:20:50 +0000263 void VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000264 void VisitParmVarDecl(ParmVarDecl *D);
265 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000266 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
267 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000268 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
269 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000270 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000271 void VisitObjCContainerDecl(ObjCContainerDecl *D);
272 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
273 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000274 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
275 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
276 void VisitObjCClassDecl(ObjCClassDecl *D);
277 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
278 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
279 void VisitObjCImplDecl(ObjCImplDecl *D);
280 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
281 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
282 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
283 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
284 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000285 };
286}
287
288void PCHDeclWriter::VisitDecl(Decl *D) {
289 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
290 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
291 Writer.AddSourceLocation(D->getLocation(), Record);
292 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000293 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000294 Record.push_back(D->isImplicit());
295 Record.push_back(D->getAccess());
296}
297
298void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
299 VisitDecl(D);
300 Code = pch::DECL_TRANSLATION_UNIT;
301}
302
303void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
304 VisitDecl(D);
305 Writer.AddDeclarationName(D->getDeclName(), Record);
306}
307
308void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
309 VisitNamedDecl(D);
310 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
311}
312
313void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
314 VisitTypeDecl(D);
315 Writer.AddTypeRef(D->getUnderlyingType(), Record);
316 Code = pch::DECL_TYPEDEF;
317}
318
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000319void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
320 VisitTypeDecl(D);
321 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
322 Record.push_back(D->isDefinition());
323 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
324}
325
326void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
327 VisitTagDecl(D);
328 Writer.AddTypeRef(D->getIntegerType(), Record);
329 Code = pch::DECL_ENUM;
330}
331
Douglas Gregor982365e2009-04-13 21:20:57 +0000332void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
333 VisitTagDecl(D);
334 Record.push_back(D->hasFlexibleArrayMember());
335 Record.push_back(D->isAnonymousStructOrUnion());
336 Code = pch::DECL_RECORD;
337}
338
Douglas Gregorc34897d2009-04-09 22:27:44 +0000339void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
340 VisitNamedDecl(D);
341 Writer.AddTypeRef(D->getType(), Record);
342}
343
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000344void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
345 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000346 Record.push_back(D->getInitExpr()? 1 : 0);
347 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000348 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000349 Writer.AddAPSInt(D->getInitVal(), Record);
350 Code = pch::DECL_ENUM_CONSTANT;
351}
352
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000353void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
354 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000355 Record.push_back(D->isThisDeclarationADefinition());
356 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000357 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000358 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
359 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
360 Record.push_back(D->isInline());
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000361 Record.push_back(D->isC99InlineDefinition());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000362 Record.push_back(D->isVirtual());
363 Record.push_back(D->isPure());
364 Record.push_back(D->inheritedPrototype());
365 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
366 Record.push_back(D->isDeleted());
367 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
368 Record.push_back(D->param_size());
369 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
370 P != PEnd; ++P)
371 Writer.AddDeclRef(*P, Record);
372 Code = pch::DECL_FUNCTION;
373}
374
Steve Naroff79ea0e02009-04-20 15:06:07 +0000375void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
376 VisitNamedDecl(D);
377 // FIXME: convert to LazyStmtPtr?
378 // Unlike C/C++, method bodies will never be in header files.
379 Record.push_back(D->getBody() != 0);
380 if (D->getBody() != 0) {
381 Writer.AddStmt(D->getBody(Context));
382 Writer.AddDeclRef(D->getSelfDecl(), Record);
383 Writer.AddDeclRef(D->getCmdDecl(), Record);
384 }
385 Record.push_back(D->isInstanceMethod());
386 Record.push_back(D->isVariadic());
387 Record.push_back(D->isSynthesized());
388 // FIXME: stable encoding for @required/@optional
389 Record.push_back(D->getImplementationControl());
390 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
391 Record.push_back(D->getObjCDeclQualifier());
392 Writer.AddTypeRef(D->getResultType(), Record);
393 Writer.AddSourceLocation(D->getLocEnd(), Record);
394 Record.push_back(D->param_size());
395 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
396 PEnd = D->param_end(); P != PEnd; ++P)
397 Writer.AddDeclRef(*P, Record);
398 Code = pch::DECL_OBJC_METHOD;
399}
400
Steve Naroff7333b492009-04-20 20:09:33 +0000401void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
402 VisitNamedDecl(D);
403 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
404 // Abstract class (no need to define a stable pch::DECL code).
405}
406
407void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
408 VisitObjCContainerDecl(D);
409 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
410 Writer.AddDeclRef(D->getSuperClass(), Record);
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000411 Record.push_back(D->protocol_size());
412 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
413 PEnd = D->protocol_end();
414 P != PEnd; ++P)
415 Writer.AddDeclRef(*P, Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000416 Record.push_back(D->ivar_size());
417 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
418 IEnd = D->ivar_end(); I != IEnd; ++I)
419 Writer.AddDeclRef(*I, Record);
Douglas Gregorae660c72009-04-23 22:34:55 +0000420 Writer.AddDeclRef(D->getCategoryList(), Record);
Steve Naroff7333b492009-04-20 20:09:33 +0000421 Record.push_back(D->isForwardDecl());
422 Record.push_back(D->isImplicitInterfaceDecl());
423 Writer.AddSourceLocation(D->getClassLoc(), Record);
424 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
425 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000426 Code = pch::DECL_OBJC_INTERFACE;
Steve Naroff7333b492009-04-20 20:09:33 +0000427}
428
429void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
430 VisitFieldDecl(D);
431 // FIXME: stable encoding for @public/@private/@protected/@package
432 Record.push_back(D->getAccessControl());
Steve Naroff97b53bd2009-04-21 15:12:33 +0000433 Code = pch::DECL_OBJC_IVAR;
434}
435
436void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
437 VisitObjCContainerDecl(D);
438 Record.push_back(D->isForwardDecl());
439 Writer.AddSourceLocation(D->getLocEnd(), Record);
440 Record.push_back(D->protocol_size());
441 for (ObjCProtocolDecl::protocol_iterator
442 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
443 Writer.AddDeclRef(*I, Record);
444 Code = pch::DECL_OBJC_PROTOCOL;
445}
446
447void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
448 VisitFieldDecl(D);
449 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
450}
451
452void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
453 VisitDecl(D);
454 Record.push_back(D->size());
455 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
456 Writer.AddDeclRef(*I, Record);
457 Code = pch::DECL_OBJC_CLASS;
458}
459
460void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
461 VisitDecl(D);
462 Record.push_back(D->protocol_size());
463 for (ObjCProtocolDecl::protocol_iterator
464 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
465 Writer.AddDeclRef(*I, Record);
466 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
467}
468
469void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
470 VisitObjCContainerDecl(D);
471 Writer.AddDeclRef(D->getClassInterface(), Record);
472 Record.push_back(D->protocol_size());
473 for (ObjCProtocolDecl::protocol_iterator
474 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
475 Writer.AddDeclRef(*I, Record);
476 Writer.AddDeclRef(D->getNextClassCategory(), Record);
477 Writer.AddSourceLocation(D->getLocEnd(), Record);
478 Code = pch::DECL_OBJC_CATEGORY;
479}
480
481void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
482 VisitNamedDecl(D);
483 Writer.AddDeclRef(D->getClassInterface(), Record);
484 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
485}
486
487void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
488 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000489 Writer.AddTypeRef(D->getType(), Record);
490 // FIXME: stable encoding
491 Record.push_back((unsigned)D->getPropertyAttributes());
492 // FIXME: stable encoding
493 Record.push_back((unsigned)D->getPropertyImplementation());
494 Writer.AddDeclarationName(D->getGetterName(), Record);
495 Writer.AddDeclarationName(D->getSetterName(), Record);
496 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
497 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
498 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000499 Code = pch::DECL_OBJC_PROPERTY;
500}
501
502void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000503 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000504 Writer.AddDeclRef(D->getClassInterface(), Record);
505 Writer.AddSourceLocation(D->getLocEnd(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000506 // Abstract class (no need to define a stable pch::DECL code).
507}
508
509void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
510 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000511 Writer.AddIdentifierRef(D->getIdentifier(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000512 Code = pch::DECL_OBJC_CATEGORY_IMPL;
513}
514
515void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
516 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000517 Writer.AddDeclRef(D->getSuperClass(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000518 Code = pch::DECL_OBJC_IMPLEMENTATION;
519}
520
521void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
522 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000523 Writer.AddSourceLocation(D->getLocStart(), Record);
524 Writer.AddDeclRef(D->getPropertyDecl(), Record);
525 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000526 Code = pch::DECL_OBJC_PROPERTY_IMPL;
Steve Naroff7333b492009-04-20 20:09:33 +0000527}
528
Douglas Gregor982365e2009-04-13 21:20:57 +0000529void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
530 VisitValueDecl(D);
531 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000532 Record.push_back(D->getBitWidth()? 1 : 0);
533 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000534 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000535 Code = pch::DECL_FIELD;
536}
537
Douglas Gregorc34897d2009-04-09 22:27:44 +0000538void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
539 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000540 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000541 Record.push_back(D->isThreadSpecified());
542 Record.push_back(D->hasCXXDirectInitializer());
543 Record.push_back(D->isDeclaredInCondition());
544 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
545 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000546 Record.push_back(D->getInit()? 1 : 0);
547 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000548 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000549 Code = pch::DECL_VAR;
550}
551
Douglas Gregorce066712009-04-26 22:20:50 +0000552void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
553 VisitVarDecl(D);
554 Code = pch::DECL_IMPLICIT_PARAM;
555}
556
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000557void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
558 VisitVarDecl(D);
559 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000560 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000561 // FIXME: why isn't the "default argument" just stored as the initializer
562 // in VarDecl?
563 Code = pch::DECL_PARM_VAR;
564}
565
566void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
567 VisitParmVarDecl(D);
568 Writer.AddTypeRef(D->getOriginalType(), Record);
569 Code = pch::DECL_ORIGINAL_PARM_VAR;
570}
571
Douglas Gregor2a491792009-04-13 22:49:25 +0000572void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
573 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000574 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000575 Code = pch::DECL_FILE_SCOPE_ASM;
576}
577
578void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
579 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000580 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000581 Record.push_back(D->param_size());
582 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
583 P != PEnd; ++P)
584 Writer.AddDeclRef(*P, Record);
585 Code = pch::DECL_BLOCK;
586}
587
Douglas Gregorc34897d2009-04-09 22:27:44 +0000588/// \brief Emit the DeclContext part of a declaration context decl.
589///
590/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
591/// block for this declaration context is stored. May be 0 to indicate
592/// that there are no declarations stored within this context.
593///
594/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
595/// block for this declaration context is stored. May be 0 to indicate
596/// that there are no declarations visible from this context. Note
597/// that this value will not be emitted for non-primary declaration
598/// contexts.
599void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
600 uint64_t VisibleOffset) {
601 Record.push_back(LexicalOffset);
Douglas Gregor405b6432009-04-22 19:09:20 +0000602 Record.push_back(VisibleOffset);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000603}
604
605//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000606// Statement/expression serialization
607//===----------------------------------------------------------------------===//
608namespace {
609 class VISIBILITY_HIDDEN PCHStmtWriter
610 : public StmtVisitor<PCHStmtWriter, void> {
611
612 PCHWriter &Writer;
613 PCHWriter::RecordData &Record;
614
615 public:
616 pch::StmtCode Code;
617
618 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
619 : Writer(Writer), Record(Record) { }
620
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000621 void VisitStmt(Stmt *S);
622 void VisitNullStmt(NullStmt *S);
623 void VisitCompoundStmt(CompoundStmt *S);
624 void VisitSwitchCase(SwitchCase *S);
625 void VisitCaseStmt(CaseStmt *S);
626 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000627 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000628 void VisitIfStmt(IfStmt *S);
629 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000630 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000631 void VisitDoStmt(DoStmt *S);
632 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000633 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000634 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000635 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000636 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000637 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000638 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000639 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000640 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000641 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000642 void VisitDeclRefExpr(DeclRefExpr *E);
643 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000644 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000645 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000646 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000647 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000648 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000649 void VisitUnaryOperator(UnaryOperator *E);
650 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000651 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000652 void VisitCallExpr(CallExpr *E);
653 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000654 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000655 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000656 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
657 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000658 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000659 void VisitExplicitCastExpr(ExplicitCastExpr *E);
660 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000661 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000662 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000663 void VisitInitListExpr(InitListExpr *E);
664 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
665 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000666 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000667 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000668 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000669 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
670 void VisitChooseExpr(ChooseExpr *E);
671 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000672 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000673 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000674 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000675
Steve Naroff79762bd2009-04-26 18:52:16 +0000676 // Objective-C Expressions
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000677 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000678 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000679 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
680 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000681 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
682 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
683 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000684 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000685 void VisitObjCSuperExpr(ObjCSuperExpr *E);
Steve Naroff79762bd2009-04-26 18:52:16 +0000686
687 // Objective-C Statements
688 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
Douglas Gregorce066712009-04-26 22:20:50 +0000689 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *);
690 void VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *);
Steve Naroff79762bd2009-04-26 18:52:16 +0000691 void VisitObjCAtTryStmt(ObjCAtTryStmt *);
692 void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
693 void VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000694 };
695}
696
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000697void PCHStmtWriter::VisitStmt(Stmt *S) {
698}
699
700void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
701 VisitStmt(S);
702 Writer.AddSourceLocation(S->getSemiLoc(), Record);
703 Code = pch::STMT_NULL;
704}
705
706void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
707 VisitStmt(S);
708 Record.push_back(S->size());
709 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
710 CS != CSEnd; ++CS)
711 Writer.WriteSubStmt(*CS);
712 Writer.AddSourceLocation(S->getLBracLoc(), Record);
713 Writer.AddSourceLocation(S->getRBracLoc(), Record);
714 Code = pch::STMT_COMPOUND;
715}
716
717void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
718 VisitStmt(S);
719 Record.push_back(Writer.RecordSwitchCaseID(S));
720}
721
722void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
723 VisitSwitchCase(S);
724 Writer.WriteSubStmt(S->getLHS());
725 Writer.WriteSubStmt(S->getRHS());
726 Writer.WriteSubStmt(S->getSubStmt());
727 Writer.AddSourceLocation(S->getCaseLoc(), Record);
728 Code = pch::STMT_CASE;
729}
730
731void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
732 VisitSwitchCase(S);
733 Writer.WriteSubStmt(S->getSubStmt());
734 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
735 Code = pch::STMT_DEFAULT;
736}
737
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000738void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
739 VisitStmt(S);
740 Writer.AddIdentifierRef(S->getID(), Record);
741 Writer.WriteSubStmt(S->getSubStmt());
742 Writer.AddSourceLocation(S->getIdentLoc(), Record);
743 Record.push_back(Writer.GetLabelID(S));
744 Code = pch::STMT_LABEL;
745}
746
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000747void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
748 VisitStmt(S);
749 Writer.WriteSubStmt(S->getCond());
750 Writer.WriteSubStmt(S->getThen());
751 Writer.WriteSubStmt(S->getElse());
752 Writer.AddSourceLocation(S->getIfLoc(), Record);
753 Code = pch::STMT_IF;
754}
755
756void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
757 VisitStmt(S);
758 Writer.WriteSubStmt(S->getCond());
759 Writer.WriteSubStmt(S->getBody());
760 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
761 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
762 SC = SC->getNextSwitchCase())
763 Record.push_back(Writer.getSwitchCaseID(SC));
764 Code = pch::STMT_SWITCH;
765}
766
Douglas Gregora6b503f2009-04-17 00:16:09 +0000767void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
768 VisitStmt(S);
769 Writer.WriteSubStmt(S->getCond());
770 Writer.WriteSubStmt(S->getBody());
771 Writer.AddSourceLocation(S->getWhileLoc(), Record);
772 Code = pch::STMT_WHILE;
773}
774
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000775void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
776 VisitStmt(S);
777 Writer.WriteSubStmt(S->getCond());
778 Writer.WriteSubStmt(S->getBody());
779 Writer.AddSourceLocation(S->getDoLoc(), Record);
780 Code = pch::STMT_DO;
781}
782
783void PCHStmtWriter::VisitForStmt(ForStmt *S) {
784 VisitStmt(S);
785 Writer.WriteSubStmt(S->getInit());
786 Writer.WriteSubStmt(S->getCond());
787 Writer.WriteSubStmt(S->getInc());
788 Writer.WriteSubStmt(S->getBody());
789 Writer.AddSourceLocation(S->getForLoc(), Record);
790 Code = pch::STMT_FOR;
791}
792
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000793void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
794 VisitStmt(S);
795 Record.push_back(Writer.GetLabelID(S->getLabel()));
796 Writer.AddSourceLocation(S->getGotoLoc(), Record);
797 Writer.AddSourceLocation(S->getLabelLoc(), Record);
798 Code = pch::STMT_GOTO;
799}
800
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000801void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
802 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000803 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000804 Writer.WriteSubStmt(S->getTarget());
805 Code = pch::STMT_INDIRECT_GOTO;
806}
807
Douglas Gregora6b503f2009-04-17 00:16:09 +0000808void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
809 VisitStmt(S);
810 Writer.AddSourceLocation(S->getContinueLoc(), Record);
811 Code = pch::STMT_CONTINUE;
812}
813
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000814void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
815 VisitStmt(S);
816 Writer.AddSourceLocation(S->getBreakLoc(), Record);
817 Code = pch::STMT_BREAK;
818}
819
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000820void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
821 VisitStmt(S);
822 Writer.WriteSubStmt(S->getRetValue());
823 Writer.AddSourceLocation(S->getReturnLoc(), Record);
824 Code = pch::STMT_RETURN;
825}
826
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000827void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
828 VisitStmt(S);
829 Writer.AddSourceLocation(S->getStartLoc(), Record);
830 Writer.AddSourceLocation(S->getEndLoc(), Record);
831 DeclGroupRef DG = S->getDeclGroup();
832 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
833 Writer.AddDeclRef(*D, Record);
834 Code = pch::STMT_DECL;
835}
836
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000837void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
838 VisitStmt(S);
839 Record.push_back(S->getNumOutputs());
840 Record.push_back(S->getNumInputs());
841 Record.push_back(S->getNumClobbers());
842 Writer.AddSourceLocation(S->getAsmLoc(), Record);
843 Writer.AddSourceLocation(S->getRParenLoc(), Record);
844 Record.push_back(S->isVolatile());
845 Record.push_back(S->isSimple());
846 Writer.WriteSubStmt(S->getAsmString());
847
848 // Outputs
849 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
850 Writer.AddString(S->getOutputName(I), Record);
851 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
852 Writer.WriteSubStmt(S->getOutputExpr(I));
853 }
854
855 // Inputs
856 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
857 Writer.AddString(S->getInputName(I), Record);
858 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
859 Writer.WriteSubStmt(S->getInputExpr(I));
860 }
861
862 // Clobbers
863 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
864 Writer.WriteSubStmt(S->getClobber(I));
865
866 Code = pch::STMT_ASM;
867}
868
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000869void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000870 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000871 Writer.AddTypeRef(E->getType(), Record);
872 Record.push_back(E->isTypeDependent());
873 Record.push_back(E->isValueDependent());
874}
875
Douglas Gregore2f37202009-04-14 21:55:33 +0000876void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
877 VisitExpr(E);
878 Writer.AddSourceLocation(E->getLocation(), Record);
879 Record.push_back(E->getIdentType()); // FIXME: stable encoding
880 Code = pch::EXPR_PREDEFINED;
881}
882
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000883void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
884 VisitExpr(E);
885 Writer.AddDeclRef(E->getDecl(), Record);
886 Writer.AddSourceLocation(E->getLocation(), Record);
887 Code = pch::EXPR_DECL_REF;
888}
889
890void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
891 VisitExpr(E);
892 Writer.AddSourceLocation(E->getLocation(), Record);
893 Writer.AddAPInt(E->getValue(), Record);
894 Code = pch::EXPR_INTEGER_LITERAL;
895}
896
Douglas Gregore2f37202009-04-14 21:55:33 +0000897void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
898 VisitExpr(E);
899 Writer.AddAPFloat(E->getValue(), Record);
900 Record.push_back(E->isExact());
901 Writer.AddSourceLocation(E->getLocation(), Record);
902 Code = pch::EXPR_FLOATING_LITERAL;
903}
904
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000905void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
906 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000907 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000908 Code = pch::EXPR_IMAGINARY_LITERAL;
909}
910
Douglas Gregor596e0932009-04-15 16:35:07 +0000911void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
912 VisitExpr(E);
913 Record.push_back(E->getByteLength());
914 Record.push_back(E->getNumConcatenated());
915 Record.push_back(E->isWide());
916 // FIXME: String data should be stored as a blob at the end of the
917 // StringLiteral. However, we can't do so now because we have no
918 // provision for coping with abbreviations when we're jumping around
919 // the PCH file during deserialization.
920 Record.insert(Record.end(),
921 E->getStrData(), E->getStrData() + E->getByteLength());
922 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
923 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
924 Code = pch::EXPR_STRING_LITERAL;
925}
926
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000927void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
928 VisitExpr(E);
929 Record.push_back(E->getValue());
930 Writer.AddSourceLocation(E->getLoc(), Record);
931 Record.push_back(E->isWide());
932 Code = pch::EXPR_CHARACTER_LITERAL;
933}
934
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000935void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
936 VisitExpr(E);
937 Writer.AddSourceLocation(E->getLParen(), Record);
938 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000939 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000940 Code = pch::EXPR_PAREN;
941}
942
Douglas Gregor12d74052009-04-15 15:58:59 +0000943void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
944 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000945 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000946 Record.push_back(E->getOpcode()); // FIXME: stable encoding
947 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
948 Code = pch::EXPR_UNARY_OPERATOR;
949}
950
951void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
952 VisitExpr(E);
953 Record.push_back(E->isSizeOf());
954 if (E->isArgumentType())
955 Writer.AddTypeRef(E->getArgumentType(), Record);
956 else {
957 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000958 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000959 }
960 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
961 Writer.AddSourceLocation(E->getRParenLoc(), Record);
962 Code = pch::EXPR_SIZEOF_ALIGN_OF;
963}
964
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000965void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
966 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000967 Writer.WriteSubStmt(E->getLHS());
968 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000969 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
970 Code = pch::EXPR_ARRAY_SUBSCRIPT;
971}
972
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000973void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
974 VisitExpr(E);
975 Record.push_back(E->getNumArgs());
976 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000977 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000978 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
979 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000980 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000981 Code = pch::EXPR_CALL;
982}
983
984void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
985 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000986 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000987 Writer.AddDeclRef(E->getMemberDecl(), Record);
988 Writer.AddSourceLocation(E->getMemberLoc(), Record);
989 Record.push_back(E->isArrow());
990 Code = pch::EXPR_MEMBER;
991}
992
Douglas Gregora151ba42009-04-14 23:32:43 +0000993void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
994 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000995 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000996}
997
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000998void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
999 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001000 Writer.WriteSubStmt(E->getLHS());
1001 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001002 Record.push_back(E->getOpcode()); // FIXME: stable encoding
1003 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1004 Code = pch::EXPR_BINARY_OPERATOR;
1005}
1006
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001007void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
1008 VisitBinaryOperator(E);
1009 Writer.AddTypeRef(E->getComputationLHSType(), Record);
1010 Writer.AddTypeRef(E->getComputationResultType(), Record);
1011 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
1012}
1013
1014void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
1015 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001016 Writer.WriteSubStmt(E->getCond());
1017 Writer.WriteSubStmt(E->getLHS());
1018 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001019 Code = pch::EXPR_CONDITIONAL_OPERATOR;
1020}
1021
Douglas Gregora151ba42009-04-14 23:32:43 +00001022void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1023 VisitCastExpr(E);
1024 Record.push_back(E->isLvalueCast());
1025 Code = pch::EXPR_IMPLICIT_CAST;
1026}
1027
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001028void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
1029 VisitCastExpr(E);
1030 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
1031}
1032
1033void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
1034 VisitExplicitCastExpr(E);
1035 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1036 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1037 Code = pch::EXPR_CSTYLE_CAST;
1038}
1039
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001040void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
1041 VisitExpr(E);
1042 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001043 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +00001044 Record.push_back(E->isFileScope());
1045 Code = pch::EXPR_COMPOUND_LITERAL;
1046}
1047
Douglas Gregorec0b8292009-04-15 23:02:49 +00001048void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
1049 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001050 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001051 Writer.AddIdentifierRef(&E->getAccessor(), Record);
1052 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
1053 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
1054}
1055
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001056void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
1057 VisitExpr(E);
1058 Record.push_back(E->getNumInits());
1059 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001060 Writer.WriteSubStmt(E->getInit(I));
1061 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001062 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
1063 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
1064 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
1065 Record.push_back(E->hadArrayRangeDesignator());
1066 Code = pch::EXPR_INIT_LIST;
1067}
1068
1069void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
1070 VisitExpr(E);
1071 Record.push_back(E->getNumSubExprs());
1072 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001073 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001074 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
1075 Record.push_back(E->usesGNUSyntax());
1076 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
1077 DEnd = E->designators_end();
1078 D != DEnd; ++D) {
1079 if (D->isFieldDesignator()) {
1080 if (FieldDecl *Field = D->getField()) {
1081 Record.push_back(pch::DESIG_FIELD_DECL);
1082 Writer.AddDeclRef(Field, Record);
1083 } else {
1084 Record.push_back(pch::DESIG_FIELD_NAME);
1085 Writer.AddIdentifierRef(D->getFieldName(), Record);
1086 }
1087 Writer.AddSourceLocation(D->getDotLoc(), Record);
1088 Writer.AddSourceLocation(D->getFieldLoc(), Record);
1089 } else if (D->isArrayDesignator()) {
1090 Record.push_back(pch::DESIG_ARRAY);
1091 Record.push_back(D->getFirstExprIndex());
1092 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1093 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1094 } else {
1095 assert(D->isArrayRangeDesignator() && "Unknown designator");
1096 Record.push_back(pch::DESIG_ARRAY_RANGE);
1097 Record.push_back(D->getFirstExprIndex());
1098 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
1099 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
1100 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
1101 }
1102 }
1103 Code = pch::EXPR_DESIGNATED_INIT;
1104}
1105
1106void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1107 VisitExpr(E);
1108 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
1109}
1110
Douglas Gregorec0b8292009-04-15 23:02:49 +00001111void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
1112 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001113 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +00001114 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1115 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1116 Code = pch::EXPR_VA_ARG;
1117}
1118
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001119void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
1120 VisitExpr(E);
1121 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
1122 Writer.AddSourceLocation(E->getLabelLoc(), Record);
1123 Record.push_back(Writer.GetLabelID(E->getLabel()));
1124 Code = pch::EXPR_ADDR_LABEL;
1125}
1126
Douglas Gregoreca12f62009-04-17 19:05:30 +00001127void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
1128 VisitExpr(E);
1129 Writer.WriteSubStmt(E->getSubStmt());
1130 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1131 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1132 Code = pch::EXPR_STMT;
1133}
1134
Douglas Gregor209d4622009-04-15 23:33:31 +00001135void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1136 VisitExpr(E);
1137 Writer.AddTypeRef(E->getArgType1(), Record);
1138 Writer.AddTypeRef(E->getArgType2(), Record);
1139 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1140 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1141 Code = pch::EXPR_TYPES_COMPATIBLE;
1142}
1143
1144void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1145 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001146 Writer.WriteSubStmt(E->getCond());
1147 Writer.WriteSubStmt(E->getLHS());
1148 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001149 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1150 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1151 Code = pch::EXPR_CHOOSE;
1152}
1153
1154void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1155 VisitExpr(E);
1156 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1157 Code = pch::EXPR_GNU_NULL;
1158}
1159
Douglas Gregor725e94b2009-04-16 00:01:45 +00001160void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1161 VisitExpr(E);
1162 Record.push_back(E->getNumSubExprs());
1163 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001164 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001165 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1166 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1167 Code = pch::EXPR_SHUFFLE_VECTOR;
1168}
1169
Douglas Gregore246b742009-04-17 19:21:43 +00001170void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1171 VisitExpr(E);
1172 Writer.AddDeclRef(E->getBlockDecl(), Record);
1173 Record.push_back(E->hasBlockDeclRefExprs());
1174 Code = pch::EXPR_BLOCK;
1175}
1176
Douglas Gregor725e94b2009-04-16 00:01:45 +00001177void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1178 VisitExpr(E);
1179 Writer.AddDeclRef(E->getDecl(), Record);
1180 Writer.AddSourceLocation(E->getLocation(), Record);
1181 Record.push_back(E->isByRef());
1182 Code = pch::EXPR_BLOCK_DECL_REF;
1183}
1184
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001185//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +00001186// Objective-C Expressions and Statements.
1187//===----------------------------------------------------------------------===//
1188
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001189void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1190 VisitExpr(E);
1191 Writer.WriteSubStmt(E->getString());
1192 Writer.AddSourceLocation(E->getAtLoc(), Record);
1193 Code = pch::EXPR_OBJC_STRING_LITERAL;
1194}
1195
Chris Lattner80f83c62009-04-22 05:57:30 +00001196void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1197 VisitExpr(E);
1198 Writer.AddTypeRef(E->getEncodedType(), Record);
1199 Writer.AddSourceLocation(E->getAtLoc(), Record);
1200 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1201 Code = pch::EXPR_OBJC_ENCODE;
1202}
1203
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001204void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1205 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001206 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001207 Writer.AddSourceLocation(E->getAtLoc(), Record);
1208 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1209 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
1210}
1211
1212void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1213 VisitExpr(E);
1214 Writer.AddDeclRef(E->getProtocol(), Record);
1215 Writer.AddSourceLocation(E->getAtLoc(), Record);
1216 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1217 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
1218}
1219
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001220void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1221 VisitExpr(E);
1222 Writer.AddDeclRef(E->getDecl(), Record);
1223 Writer.AddSourceLocation(E->getLocation(), Record);
1224 Writer.WriteSubStmt(E->getBase());
1225 Record.push_back(E->isArrow());
1226 Record.push_back(E->isFreeIvar());
Steve Naroffa323e972009-04-26 14:11:39 +00001227 Code = pch::EXPR_OBJC_IVAR_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001228}
1229
1230void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1231 VisitExpr(E);
1232 Writer.AddDeclRef(E->getProperty(), Record);
1233 Writer.AddSourceLocation(E->getLocation(), Record);
1234 Writer.WriteSubStmt(E->getBase());
Steve Naroffa323e972009-04-26 14:11:39 +00001235 Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001236}
1237
1238void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1239 VisitExpr(E);
1240 Writer.AddDeclRef(E->getGetterMethod(), Record);
1241 Writer.AddDeclRef(E->getSetterMethod(), Record);
1242
1243 // NOTE: ClassProp and Base are mutually exclusive.
1244 Writer.AddDeclRef(E->getClassProp(), Record);
1245 Writer.WriteSubStmt(E->getBase());
1246 Writer.AddSourceLocation(E->getLocation(), Record);
1247 Writer.AddSourceLocation(E->getClassLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001248 Code = pch::EXPR_OBJC_KVC_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001249}
1250
Steve Narofffb3e4022009-04-25 14:04:28 +00001251void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1252 VisitExpr(E);
1253 Record.push_back(E->getNumArgs());
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001254 Writer.AddSourceLocation(E->getLeftLoc(), Record);
1255 Writer.AddSourceLocation(E->getRightLoc(), Record);
Steve Narofffb3e4022009-04-25 14:04:28 +00001256 Writer.AddSelectorRef(E->getSelector(), Record);
1257 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
Steve Narofffb3e4022009-04-25 14:04:28 +00001258 Writer.WriteSubStmt(E->getReceiver());
Douglas Gregorce066712009-04-26 22:20:50 +00001259
1260 if (!E->getReceiver()) {
1261 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
1262 Writer.AddDeclRef(CI.first, Record);
1263 Writer.AddIdentifierRef(CI.second, Record);
1264 }
1265
Steve Narofffb3e4022009-04-25 14:04:28 +00001266 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1267 Arg != ArgEnd; ++Arg)
1268 Writer.WriteSubStmt(*Arg);
1269 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
1270}
1271
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001272void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1273 VisitExpr(E);
1274 Writer.AddSourceLocation(E->getLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +00001275 Code = pch::EXPR_OBJC_SUPER_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001276}
1277
Steve Naroff79762bd2009-04-26 18:52:16 +00001278void PCHStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1279 VisitStmt(S);
1280 Writer.WriteSubStmt(S->getElement());
1281 Writer.WriteSubStmt(S->getCollection());
1282 Writer.WriteSubStmt(S->getBody());
1283 Writer.AddSourceLocation(S->getForLoc(), Record);
1284 Writer.AddSourceLocation(S->getRParenLoc(), Record);
1285 Code = pch::STMT_OBJC_FOR_COLLECTION;
1286}
1287
Douglas Gregorce066712009-04-26 22:20:50 +00001288void PCHStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +00001289 Writer.WriteSubStmt(S->getCatchBody());
1290 Writer.WriteSubStmt(S->getNextCatchStmt());
1291 Writer.AddDeclRef(S->getCatchParamDecl(), Record);
1292 Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
1293 Writer.AddSourceLocation(S->getRParenLoc(), Record);
1294 Code = pch::STMT_OBJC_CATCH;
1295}
1296
Douglas Gregorce066712009-04-26 22:20:50 +00001297void PCHStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +00001298 Writer.WriteSubStmt(S->getFinallyBody());
1299 Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
1300 Code = pch::STMT_OBJC_FINALLY;
1301}
1302
1303void PCHStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1304 Writer.WriteSubStmt(S->getTryBody());
1305 Writer.WriteSubStmt(S->getCatchStmts());
1306 Writer.WriteSubStmt(S->getFinallyStmt());
1307 Writer.AddSourceLocation(S->getAtTryLoc(), Record);
1308 Code = pch::STMT_OBJC_AT_TRY;
1309}
1310
1311void PCHStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1312 Writer.WriteSubStmt(S->getSynchExpr());
1313 Writer.WriteSubStmt(S->getSynchBody());
1314 Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
1315 Code = pch::STMT_OBJC_AT_SYNCHRONIZED;
1316}
1317
1318void PCHStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1319 Writer.WriteSubStmt(S->getThrowExpr());
1320 Writer.AddSourceLocation(S->getThrowLoc(), Record);
1321 Code = pch::STMT_OBJC_AT_THROW;
1322}
Chris Lattner80f83c62009-04-22 05:57:30 +00001323
1324//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001325// PCHWriter Implementation
1326//===----------------------------------------------------------------------===//
1327
Chris Lattner920673a2009-04-26 22:26:21 +00001328static void EmitBlockID(unsigned ID, const char *Name,
1329 llvm::BitstreamWriter &Stream,
1330 PCHWriter::RecordData &Record) {
1331 Record.clear();
1332 Record.push_back(ID);
1333 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
1334
1335 // Emit the block name if present.
1336 if (Name == 0 || Name[0] == 0) return;
1337 Record.clear();
1338 while (*Name)
1339 Record.push_back(*Name++);
1340 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
1341}
1342
1343static void EmitRecordID(unsigned ID, const char *Name,
1344 llvm::BitstreamWriter &Stream,
1345 PCHWriter::RecordData &Record) {
1346 Record.clear();
1347 Record.push_back(ID);
1348 while (*Name)
1349 Record.push_back(*Name++);
1350 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerd16afaa2009-04-27 00:49:53 +00001351}
1352
1353static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
1354 PCHWriter::RecordData &Record) {
1355#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
1356 RECORD(STMT_STOP);
1357 RECORD(STMT_NULL_PTR);
1358 RECORD(STMT_NULL);
1359 RECORD(STMT_COMPOUND);
1360 RECORD(STMT_CASE);
1361 RECORD(STMT_DEFAULT);
1362 RECORD(STMT_LABEL);
1363 RECORD(STMT_IF);
1364 RECORD(STMT_SWITCH);
1365 RECORD(STMT_WHILE);
1366 RECORD(STMT_DO);
1367 RECORD(STMT_FOR);
1368 RECORD(STMT_GOTO);
1369 RECORD(STMT_INDIRECT_GOTO);
1370 RECORD(STMT_CONTINUE);
1371 RECORD(STMT_BREAK);
1372 RECORD(STMT_RETURN);
1373 RECORD(STMT_DECL);
1374 RECORD(STMT_ASM);
1375 RECORD(EXPR_PREDEFINED);
1376 RECORD(EXPR_DECL_REF);
1377 RECORD(EXPR_INTEGER_LITERAL);
1378 RECORD(EXPR_FLOATING_LITERAL);
1379 RECORD(EXPR_IMAGINARY_LITERAL);
1380 RECORD(EXPR_STRING_LITERAL);
1381 RECORD(EXPR_CHARACTER_LITERAL);
1382 RECORD(EXPR_PAREN);
1383 RECORD(EXPR_UNARY_OPERATOR);
1384 RECORD(EXPR_SIZEOF_ALIGN_OF);
1385 RECORD(EXPR_ARRAY_SUBSCRIPT);
1386 RECORD(EXPR_CALL);
1387 RECORD(EXPR_MEMBER);
1388 RECORD(EXPR_BINARY_OPERATOR);
1389 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
1390 RECORD(EXPR_CONDITIONAL_OPERATOR);
1391 RECORD(EXPR_IMPLICIT_CAST);
1392 RECORD(EXPR_CSTYLE_CAST);
1393 RECORD(EXPR_COMPOUND_LITERAL);
1394 RECORD(EXPR_EXT_VECTOR_ELEMENT);
1395 RECORD(EXPR_INIT_LIST);
1396 RECORD(EXPR_DESIGNATED_INIT);
1397 RECORD(EXPR_IMPLICIT_VALUE_INIT);
1398 RECORD(EXPR_VA_ARG);
1399 RECORD(EXPR_ADDR_LABEL);
1400 RECORD(EXPR_STMT);
1401 RECORD(EXPR_TYPES_COMPATIBLE);
1402 RECORD(EXPR_CHOOSE);
1403 RECORD(EXPR_GNU_NULL);
1404 RECORD(EXPR_SHUFFLE_VECTOR);
1405 RECORD(EXPR_BLOCK);
1406 RECORD(EXPR_BLOCK_DECL_REF);
1407 RECORD(EXPR_OBJC_STRING_LITERAL);
1408 RECORD(EXPR_OBJC_ENCODE);
1409 RECORD(EXPR_OBJC_SELECTOR_EXPR);
1410 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
1411 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
1412 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
1413 RECORD(EXPR_OBJC_KVC_REF_EXPR);
1414 RECORD(EXPR_OBJC_MESSAGE_EXPR);
1415 RECORD(EXPR_OBJC_SUPER_EXPR);
1416 RECORD(STMT_OBJC_FOR_COLLECTION);
1417 RECORD(STMT_OBJC_CATCH);
1418 RECORD(STMT_OBJC_FINALLY);
1419 RECORD(STMT_OBJC_AT_TRY);
1420 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
1421 RECORD(STMT_OBJC_AT_THROW);
1422#undef RECORD
Chris Lattner920673a2009-04-26 22:26:21 +00001423}
1424
1425void PCHWriter::WriteBlockInfoBlock() {
1426 RecordData Record;
1427 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
1428
Chris Lattner880f3f72009-04-27 00:40:25 +00001429#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner920673a2009-04-26 22:26:21 +00001430#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
1431
1432 // PCH Top-Level Block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001433 BLOCK(PCH_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001434 RECORD(TYPE_OFFSET);
1435 RECORD(DECL_OFFSET);
1436 RECORD(LANGUAGE_OPTIONS);
1437 RECORD(TARGET_TRIPLE);
1438 RECORD(IDENTIFIER_OFFSET);
1439 RECORD(IDENTIFIER_TABLE);
1440 RECORD(EXTERNAL_DEFINITIONS);
1441 RECORD(SPECIAL_TYPES);
1442 RECORD(STATISTICS);
1443 RECORD(TENTATIVE_DEFINITIONS);
1444 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
1445 RECORD(SELECTOR_OFFSETS);
1446 RECORD(METHOD_POOL);
1447 RECORD(PP_COUNTER_VALUE);
1448
1449 // SourceManager Block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001450 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001451 RECORD(SM_SLOC_FILE_ENTRY);
1452 RECORD(SM_SLOC_BUFFER_ENTRY);
1453 RECORD(SM_SLOC_BUFFER_BLOB);
1454 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
1455 RECORD(SM_LINE_TABLE);
1456 RECORD(SM_HEADER_FILE_INFO);
1457
1458 // Preprocessor Block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001459 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001460 RECORD(PP_MACRO_OBJECT_LIKE);
1461 RECORD(PP_MACRO_FUNCTION_LIKE);
1462 RECORD(PP_TOKEN);
1463
1464 // Types block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001465 BLOCK(TYPES_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001466 RECORD(TYPE_EXT_QUAL);
1467 RECORD(TYPE_FIXED_WIDTH_INT);
1468 RECORD(TYPE_COMPLEX);
1469 RECORD(TYPE_POINTER);
1470 RECORD(TYPE_BLOCK_POINTER);
1471 RECORD(TYPE_LVALUE_REFERENCE);
1472 RECORD(TYPE_RVALUE_REFERENCE);
1473 RECORD(TYPE_MEMBER_POINTER);
1474 RECORD(TYPE_CONSTANT_ARRAY);
1475 RECORD(TYPE_INCOMPLETE_ARRAY);
1476 RECORD(TYPE_VARIABLE_ARRAY);
1477 RECORD(TYPE_VECTOR);
1478 RECORD(TYPE_EXT_VECTOR);
1479 RECORD(TYPE_FUNCTION_PROTO);
1480 RECORD(TYPE_FUNCTION_NO_PROTO);
1481 RECORD(TYPE_TYPEDEF);
1482 RECORD(TYPE_TYPEOF_EXPR);
1483 RECORD(TYPE_TYPEOF);
1484 RECORD(TYPE_RECORD);
1485 RECORD(TYPE_ENUM);
1486 RECORD(TYPE_OBJC_INTERFACE);
1487 RECORD(TYPE_OBJC_QUALIFIED_INTERFACE);
1488 RECORD(TYPE_OBJC_QUALIFIED_ID);
Chris Lattnerd16afaa2009-04-27 00:49:53 +00001489 // Statements and Exprs can occur in the Types block.
1490 AddStmtsExprs(Stream, Record);
1491
Chris Lattner920673a2009-04-26 22:26:21 +00001492 // Decls block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001493 BLOCK(DECLS_BLOCK);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001494 RECORD(DECL_ATTR);
1495 RECORD(DECL_TRANSLATION_UNIT);
1496 RECORD(DECL_TYPEDEF);
1497 RECORD(DECL_ENUM);
1498 RECORD(DECL_RECORD);
1499 RECORD(DECL_ENUM_CONSTANT);
1500 RECORD(DECL_FUNCTION);
1501 RECORD(DECL_OBJC_METHOD);
1502 RECORD(DECL_OBJC_INTERFACE);
1503 RECORD(DECL_OBJC_PROTOCOL);
1504 RECORD(DECL_OBJC_IVAR);
1505 RECORD(DECL_OBJC_AT_DEFS_FIELD);
1506 RECORD(DECL_OBJC_CLASS);
1507 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
1508 RECORD(DECL_OBJC_CATEGORY);
1509 RECORD(DECL_OBJC_CATEGORY_IMPL);
1510 RECORD(DECL_OBJC_IMPLEMENTATION);
1511 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1512 RECORD(DECL_OBJC_PROPERTY);
1513 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +00001514 RECORD(DECL_FIELD);
1515 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001516 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +00001517 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001518 RECORD(DECL_ORIGINAL_PARM_VAR);
1519 RECORD(DECL_FILE_SCOPE_ASM);
1520 RECORD(DECL_BLOCK);
1521 RECORD(DECL_CONTEXT_LEXICAL);
1522 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerd16afaa2009-04-27 00:49:53 +00001523 // Statements and Exprs can occur in the Decls block.
1524 AddStmtsExprs(Stream, Record);
Chris Lattner920673a2009-04-26 22:26:21 +00001525#undef RECORD
1526#undef BLOCK
1527 Stream.ExitBlock();
1528}
1529
1530
Douglas Gregorb5887f32009-04-10 21:16:55 +00001531/// \brief Write the target triple (e.g., i686-apple-darwin9).
1532void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1533 using namespace llvm;
1534 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1535 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001537 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001538
1539 RecordData Record;
1540 Record.push_back(pch::TARGET_TRIPLE);
1541 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001542 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001543}
1544
1545/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001546void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1547 RecordData Record;
1548 Record.push_back(LangOpts.Trigraphs);
1549 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1550 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1551 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1552 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1553 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1554 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1555 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1556 Record.push_back(LangOpts.C99); // C99 Support
1557 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1558 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1559 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1560 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1561 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1562
1563 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1564 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1565 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1566
1567 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1568 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1569 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1570 Record.push_back(LangOpts.LaxVectorConversions);
1571 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1572
1573 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1574 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1575 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1576
1577 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1578 // by locks.
1579 Record.push_back(LangOpts.Blocks); // block extension to C
1580 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1581 // they are unused.
1582 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1583 // (modulo the platform support).
1584
1585 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1586 // signed integer arithmetic overflows.
1587
1588 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1589 // may be ripped out at any time.
1590
1591 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1592 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1593 // defined.
1594 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1595 // opposed to __DYNAMIC__).
1596 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1597
1598 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1599 // used (instead of C99 semantics).
1600 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1601 Record.push_back(LangOpts.getGCMode());
1602 Record.push_back(LangOpts.getVisibilityMode());
1603 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001604 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001605}
1606
Douglas Gregorab1cef72009-04-10 03:52:48 +00001607//===----------------------------------------------------------------------===//
1608// Source Manager Serialization
1609//===----------------------------------------------------------------------===//
1610
1611/// \brief Create an abbreviation for the SLocEntry that refers to a
1612/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001613static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001614 using namespace llvm;
1615 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1616 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001622 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001623}
1624
1625/// \brief Create an abbreviation for the SLocEntry that refers to a
1626/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001627static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001628 using namespace llvm;
1629 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1630 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001636 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001637}
1638
1639/// \brief Create an abbreviation for the SLocEntry that refers to a
1640/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001641static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001642 using namespace llvm;
1643 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1644 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001646 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001647}
1648
1649/// \brief Create an abbreviation for the SLocEntry that refers to an
1650/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001651static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001652 using namespace llvm;
1653 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1654 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1657 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001660 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001661}
1662
1663/// \brief Writes the block containing the serialized form of the
1664/// source manager.
1665///
1666/// TODO: We should probably use an on-disk hash table (stored in a
1667/// blob), indexed based on the file name, so that we only create
1668/// entries for files that we actually need. In the common case (no
1669/// errors), we probably won't have to create file entries for any of
1670/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001671void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1672 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001673 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001674 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001675
1676 // Abbreviations for the various kinds of source-location entries.
1677 int SLocFileAbbrv = -1;
1678 int SLocBufferAbbrv = -1;
1679 int SLocBufferBlobAbbrv = -1;
1680 int SLocInstantiationAbbrv = -1;
1681
1682 // Write out the source location entry table. We skip the first
1683 // entry, which is always the same dummy entry.
1684 RecordData Record;
1685 for (SourceManager::sloc_entry_iterator
1686 SLoc = SourceMgr.sloc_entry_begin() + 1,
1687 SLocEnd = SourceMgr.sloc_entry_end();
1688 SLoc != SLocEnd; ++SLoc) {
1689 // Figure out which record code to use.
1690 unsigned Code;
1691 if (SLoc->isFile()) {
1692 if (SLoc->getFile().getContentCache()->Entry)
1693 Code = pch::SM_SLOC_FILE_ENTRY;
1694 else
1695 Code = pch::SM_SLOC_BUFFER_ENTRY;
1696 } else
1697 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1698 Record.push_back(Code);
1699
1700 Record.push_back(SLoc->getOffset());
1701 if (SLoc->isFile()) {
1702 const SrcMgr::FileInfo &File = SLoc->getFile();
1703 Record.push_back(File.getIncludeLoc().getRawEncoding());
1704 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001705 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001706
1707 const SrcMgr::ContentCache *Content = File.getContentCache();
1708 if (Content->Entry) {
1709 // The source location entry is a file. The blob associated
1710 // with this entry is the file name.
1711 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001712 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1713 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001714 Content->Entry->getName(),
1715 strlen(Content->Entry->getName()));
1716 } else {
1717 // The source location entry is a buffer. The blob associated
1718 // with this entry contains the contents of the buffer.
1719 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001720 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1721 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001722 }
1723
1724 // We add one to the size so that we capture the trailing NULL
1725 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1726 // the reader side).
1727 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1728 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001729 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001730 Record.clear();
1731 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001732 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001733 Buffer->getBufferStart(),
1734 Buffer->getBufferSize() + 1);
1735 }
1736 } else {
1737 // The source location entry is an instantiation.
1738 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1739 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1740 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1741 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1742
Douglas Gregor364e5802009-04-15 18:05:10 +00001743 // Compute the token length for this macro expansion.
1744 unsigned NextOffset = SourceMgr.getNextOffset();
1745 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1746 if (++NextSLoc != SLocEnd)
1747 NextOffset = NextSLoc->getOffset();
1748 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1749
Douglas Gregorab1cef72009-04-10 03:52:48 +00001750 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001751 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1752 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001753 }
1754
1755 Record.clear();
1756 }
1757
Douglas Gregor635f97f2009-04-13 16:31:14 +00001758 // Write the line table.
1759 if (SourceMgr.hasLineTable()) {
1760 LineTableInfo &LineTable = SourceMgr.getLineTable();
1761
1762 // Emit the file names
1763 Record.push_back(LineTable.getNumFilenames());
1764 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1765 // Emit the file name
1766 const char *Filename = LineTable.getFilename(I);
1767 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1768 Record.push_back(FilenameLen);
1769 if (FilenameLen)
1770 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1771 }
1772
1773 // Emit the line entries
1774 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1775 L != LEnd; ++L) {
1776 // Emit the file ID
1777 Record.push_back(L->first);
1778
1779 // Emit the line entries
1780 Record.push_back(L->second.size());
1781 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1782 LEEnd = L->second.end();
1783 LE != LEEnd; ++LE) {
1784 Record.push_back(LE->FileOffset);
1785 Record.push_back(LE->LineNo);
1786 Record.push_back(LE->FilenameID);
1787 Record.push_back((unsigned)LE->FileKind);
1788 Record.push_back(LE->IncludeOffset);
1789 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001790 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001791 }
1792 }
1793
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001794 // Loop over all the header files.
1795 HeaderSearch &HS = PP.getHeaderSearchInfo();
1796 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1797 E = HS.header_file_end();
1798 I != E; ++I) {
1799 Record.push_back(I->isImport);
1800 Record.push_back(I->DirInfo);
1801 Record.push_back(I->NumIncludes);
1802 if (I->ControllingMacro)
1803 AddIdentifierRef(I->ControllingMacro, Record);
1804 else
1805 Record.push_back(0);
1806 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1807 Record.clear();
1808 }
1809
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001810 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001811}
1812
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001813/// \brief Writes the block containing the serialized form of the
1814/// preprocessor.
1815///
Chris Lattner850eabd2009-04-10 18:08:30 +00001816void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001817 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001818
Chris Lattner4b21c202009-04-13 01:29:17 +00001819 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1820 if (PP.getCounterValue() != 0) {
1821 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001822 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001823 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001824 }
1825
1826 // Enter the preprocessor block.
1827 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001828
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001829 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1830 // FIXME: use diagnostics subsystem for localization etc.
1831 if (PP.SawDateOrTime())
1832 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1833
Chris Lattner1b094952009-04-10 18:00:12 +00001834 // Loop over all the macro definitions that are live at the end of the file,
1835 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001836 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1837 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001838 // FIXME: This emits macros in hash table order, we should do it in a stable
1839 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001840 MacroInfo *MI = I->second;
1841
1842 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1843 // been redefined by the header (in which case they are not isBuiltinMacro).
1844 if (MI->isBuiltinMacro())
1845 continue;
1846
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001847 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001848 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001849 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001850 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1851 Record.push_back(MI->isUsed());
1852
1853 unsigned Code;
1854 if (MI->isObjectLike()) {
1855 Code = pch::PP_MACRO_OBJECT_LIKE;
1856 } else {
1857 Code = pch::PP_MACRO_FUNCTION_LIKE;
1858
1859 Record.push_back(MI->isC99Varargs());
1860 Record.push_back(MI->isGNUVarargs());
1861 Record.push_back(MI->getNumArgs());
1862 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1863 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001864 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001865 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001866 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001867 Record.clear();
1868
Chris Lattner850eabd2009-04-10 18:08:30 +00001869 // Emit the tokens array.
1870 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1871 // Note that we know that the preprocessor does not have any annotation
1872 // tokens in it because they are created by the parser, and thus can't be
1873 // in a macro definition.
1874 const Token &Tok = MI->getReplacementToken(TokNo);
1875
1876 Record.push_back(Tok.getLocation().getRawEncoding());
1877 Record.push_back(Tok.getLength());
1878
Chris Lattner850eabd2009-04-10 18:08:30 +00001879 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1880 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001881 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001882
1883 // FIXME: Should translate token kind to a stable encoding.
1884 Record.push_back(Tok.getKind());
1885 // FIXME: Should translate token flags to a stable encoding.
1886 Record.push_back(Tok.getFlags());
1887
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001888 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001889 Record.clear();
1890 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001891 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001892 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001893 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001894}
1895
1896
Douglas Gregorc34897d2009-04-09 22:27:44 +00001897/// \brief Write the representation of a type to the PCH stream.
1898void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001899 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001900 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001901 ID = NextTypeID++;
1902
1903 // Record the offset for this type.
1904 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001905 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001906 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1907 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001908 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001909 }
1910
1911 RecordData Record;
1912
1913 // Emit the type's representation.
1914 PCHTypeWriter W(*this, Record);
1915 switch (T->getTypeClass()) {
1916 // For all of the concrete, non-dependent types, call the
1917 // appropriate visitor function.
1918#define TYPE(Class, Base) \
1919 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1920#define ABSTRACT_TYPE(Class, Base)
1921#define DEPENDENT_TYPE(Class, Base)
1922#include "clang/AST/TypeNodes.def"
1923
1924 // For all of the dependent type nodes (which only occur in C++
1925 // templates), produce an error.
1926#define TYPE(Class, Base)
1927#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1928#include "clang/AST/TypeNodes.def"
1929 assert(false && "Cannot serialize dependent type nodes");
1930 break;
1931 }
1932
1933 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001934 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001935
1936 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001937 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001938}
1939
1940/// \brief Write a block containing all of the types.
1941void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001942 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001943 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001944
Douglas Gregore43f0972009-04-26 03:49:13 +00001945 // Emit all of the types that need to be emitted (so far).
1946 while (!TypesToEmit.empty()) {
1947 const Type *T = TypesToEmit.front();
1948 TypesToEmit.pop();
1949 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1950 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001951 }
1952
1953 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001954 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001955}
1956
1957/// \brief Write the block containing all of the declaration IDs
1958/// lexically declared within the given DeclContext.
1959///
1960/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1961/// bistream, or 0 if no block was written.
1962uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1963 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001964 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001965 return 0;
1966
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001967 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001968 RecordData Record;
1969 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1970 DEnd = DC->decls_end(Context);
1971 D != DEnd; ++D)
1972 AddDeclRef(*D, Record);
1973
Douglas Gregoraf136d92009-04-22 22:34:57 +00001974 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001975 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001976 return Offset;
1977}
1978
1979/// \brief Write the block containing all of the declaration IDs
1980/// visible from the given DeclContext.
1981///
1982/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1983/// bistream, or 0 if no block was written.
1984uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1985 DeclContext *DC) {
1986 if (DC->getPrimaryContext() != DC)
1987 return 0;
1988
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001989 // Since there is no name lookup into functions or methods, and we
1990 // perform name lookup for the translation unit via the
1991 // IdentifierInfo chains, don't bother to build a
1992 // visible-declarations table for these entities.
1993 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001994 return 0;
1995
Douglas Gregorc34897d2009-04-09 22:27:44 +00001996 // Force the DeclContext to build a its name-lookup table.
1997 DC->lookup(Context, DeclarationName());
1998
1999 // Serialize the contents of the mapping used for lookup. Note that,
2000 // although we have two very different code paths, the serialized
2001 // representation is the same for both cases: a declaration name,
2002 // followed by a size, followed by references to the visible
2003 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002004 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002005 RecordData Record;
2006 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00002007 if (!Map)
2008 return 0;
2009
Douglas Gregorc34897d2009-04-09 22:27:44 +00002010 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2011 D != DEnd; ++D) {
2012 AddDeclarationName(D->first, Record);
2013 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
2014 Record.push_back(Result.second - Result.first);
2015 for(; Result.first != Result.second; ++Result.first)
2016 AddDeclRef(*Result.first, Record);
2017 }
2018
2019 if (Record.size() == 0)
2020 return 0;
2021
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002022 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002023 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002024 return Offset;
2025}
2026
2027/// \brief Write a block containing all of the declarations.
2028void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00002029 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002030 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002031
2032 // Emit all of the declarations.
2033 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00002034 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002035 while (!DeclsToEmit.empty()) {
2036 // Pull the next declaration off the queue
2037 Decl *D = DeclsToEmit.front();
2038 DeclsToEmit.pop();
2039
2040 // If this declaration is also a DeclContext, write blocks for the
2041 // declarations that lexically stored inside its context and those
2042 // declarations that are visible from its context. These blocks
2043 // are written before the declaration itself so that we can put
2044 // their offsets into the record for the declaration.
2045 uint64_t LexicalOffset = 0;
2046 uint64_t VisibleOffset = 0;
2047 DeclContext *DC = dyn_cast<DeclContext>(D);
2048 if (DC) {
2049 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
2050 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
2051 }
2052
2053 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00002054 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002055 if (ID == 0)
2056 ID = DeclIDs.size();
2057
2058 unsigned Index = ID - 1;
2059
2060 // Record the offset for this declaration
2061 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002062 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002063 else if (DeclOffsets.size() < Index) {
2064 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002065 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002066 }
2067
2068 // Build and emit a record for this declaration
2069 Record.clear();
2070 W.Code = (pch::DeclCode)0;
2071 W.Visit(D);
2072 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002073
2074 if (!W.Code) {
2075 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
2076 D->getDeclKindName());
2077 assert(false && "Unhandled declaration kind while generating PCH");
2078 exit(-1);
2079 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002080 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00002081
Douglas Gregor1c507882009-04-15 21:30:51 +00002082 // If the declaration had any attributes, write them now.
2083 if (D->hasAttrs())
2084 WriteAttributeRecord(D->getAttrs());
2085
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002086 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002087 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002088
Douglas Gregor631f6c62009-04-14 00:24:19 +00002089 // Note external declarations so that we can add them to a record
2090 // in the PCH file later.
2091 if (isa<FileScopeAsmDecl>(D))
2092 ExternalDefinitions.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002093 }
2094
2095 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002096 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002097}
2098
Douglas Gregorff9a6092009-04-20 20:36:09 +00002099namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002100// Trait used for the on-disk hash table used in the method pool.
2101class VISIBILITY_HIDDEN PCHMethodPoolTrait {
2102 PCHWriter &Writer;
2103
2104public:
2105 typedef Selector key_type;
2106 typedef key_type key_type_ref;
2107
2108 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
2109 typedef const data_type& data_type_ref;
2110
2111 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
2112
2113 static unsigned ComputeHash(Selector Sel) {
2114 unsigned N = Sel.getNumArgs();
2115 if (N == 0)
2116 ++N;
2117 unsigned R = 5381;
2118 for (unsigned I = 0; I != N; ++I)
2119 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
2120 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
2121 return R;
2122 }
2123
2124 std::pair<unsigned,unsigned>
2125 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2126 data_type_ref Methods) {
2127 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2128 clang::io::Emit16(Out, KeyLen);
2129 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
2130 for (const ObjCMethodList *Method = &Methods.first; Method;
2131 Method = Method->Next)
2132 if (Method->Method)
2133 DataLen += 4;
2134 for (const ObjCMethodList *Method = &Methods.second; Method;
2135 Method = Method->Next)
2136 if (Method->Method)
2137 DataLen += 4;
2138 clang::io::Emit16(Out, DataLen);
2139 return std::make_pair(KeyLen, DataLen);
2140 }
2141
Douglas Gregor2d711832009-04-25 17:48:32 +00002142 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
2143 uint64_t Start = Out.tell();
2144 assert((Start >> 32) == 0 && "Selector key offset too large");
2145 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002146 unsigned N = Sel.getNumArgs();
2147 clang::io::Emit16(Out, N);
2148 if (N == 0)
2149 N = 1;
2150 for (unsigned I = 0; I != N; ++I)
2151 clang::io::Emit32(Out,
2152 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2153 }
2154
2155 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00002156 data_type_ref Methods, unsigned DataLen) {
2157 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002158 unsigned NumInstanceMethods = 0;
2159 for (const ObjCMethodList *Method = &Methods.first; Method;
2160 Method = Method->Next)
2161 if (Method->Method)
2162 ++NumInstanceMethods;
2163
2164 unsigned NumFactoryMethods = 0;
2165 for (const ObjCMethodList *Method = &Methods.second; Method;
2166 Method = Method->Next)
2167 if (Method->Method)
2168 ++NumFactoryMethods;
2169
2170 clang::io::Emit16(Out, NumInstanceMethods);
2171 clang::io::Emit16(Out, NumFactoryMethods);
2172 for (const ObjCMethodList *Method = &Methods.first; Method;
2173 Method = Method->Next)
2174 if (Method->Method)
2175 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002176 for (const ObjCMethodList *Method = &Methods.second; Method;
2177 Method = Method->Next)
2178 if (Method->Method)
2179 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00002180
2181 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002182 }
2183};
2184} // end anonymous namespace
2185
2186/// \brief Write the method pool into the PCH file.
2187///
2188/// The method pool contains both instance and factory methods, stored
2189/// in an on-disk hash table indexed by the selector.
2190void PCHWriter::WriteMethodPool(Sema &SemaRef) {
2191 using namespace llvm;
2192
2193 // Create and write out the blob that contains the instance and
2194 // factor method pools.
2195 bool Empty = true;
2196 {
2197 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
2198
2199 // Create the on-disk hash table representation. Start by
2200 // iterating through the instance method pool.
2201 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00002202 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002203 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2204 Instance = SemaRef.InstanceMethodPool.begin(),
2205 InstanceEnd = SemaRef.InstanceMethodPool.end();
2206 Instance != InstanceEnd; ++Instance) {
2207 // Check whether there is a factory method with the same
2208 // selector.
2209 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
2210 = SemaRef.FactoryMethodPool.find(Instance->first);
2211
2212 if (Factory == SemaRef.FactoryMethodPool.end())
2213 Generator.insert(Instance->first,
2214 std::make_pair(Instance->second,
2215 ObjCMethodList()));
2216 else
2217 Generator.insert(Instance->first,
2218 std::make_pair(Instance->second, Factory->second));
2219
Douglas Gregor2d711832009-04-25 17:48:32 +00002220 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002221 Empty = false;
2222 }
2223
2224 // Now iterate through the factory method pool, to pick up any
2225 // selectors that weren't already in the instance method pool.
2226 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
2227 Factory = SemaRef.FactoryMethodPool.begin(),
2228 FactoryEnd = SemaRef.FactoryMethodPool.end();
2229 Factory != FactoryEnd; ++Factory) {
2230 // Check whether there is an instance method with the same
2231 // selector. If so, there is no work to do here.
2232 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
2233 = SemaRef.InstanceMethodPool.find(Factory->first);
2234
Douglas Gregor2d711832009-04-25 17:48:32 +00002235 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002236 Generator.insert(Factory->first,
2237 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00002238 ++NumSelectorsInMethodPool;
2239 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002240
2241 Empty = false;
2242 }
2243
Douglas Gregor2d711832009-04-25 17:48:32 +00002244 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002245 return;
2246
2247 // Create the on-disk hash table in a buffer.
2248 llvm::SmallVector<char, 4096> MethodPool;
2249 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00002250 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002251 {
2252 PCHMethodPoolTrait Trait(*this);
2253 llvm::raw_svector_ostream Out(MethodPool);
2254 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002255 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002256 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00002257
2258 // For every selector that we have seen but which was not
2259 // written into the hash table, write the selector itself and
2260 // record it's offset.
2261 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
2262 if (SelectorOffsets[I] == 0)
2263 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002264 }
2265
2266 // Create a blob abbreviation
2267 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2268 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00002270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2272 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2273
Douglas Gregor2d711832009-04-25 17:48:32 +00002274 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002275 RecordData Record;
2276 Record.push_back(pch::METHOD_POOL);
2277 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00002278 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002279 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
2280 &MethodPool.front(),
2281 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00002282
2283 // Create a blob abbreviation for the selector table offsets.
2284 Abbrev = new BitCodeAbbrev();
2285 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
2286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
2287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2288 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2289
2290 // Write the selector offsets table.
2291 Record.clear();
2292 Record.push_back(pch::SELECTOR_OFFSETS);
2293 Record.push_back(SelectorOffsets.size());
2294 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
2295 (const char *)&SelectorOffsets.front(),
2296 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002297 }
2298}
2299
2300namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002301class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
2302 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002303 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002304
2305public:
2306 typedef const IdentifierInfo* key_type;
2307 typedef key_type key_type_ref;
2308
2309 typedef pch::IdentID data_type;
2310 typedef data_type data_type_ref;
2311
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002312 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
2313 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00002314
2315 static unsigned ComputeHash(const IdentifierInfo* II) {
2316 return clang::BernsteinHash(II->getName());
2317 }
2318
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002319 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00002320 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
2321 pch::IdentID ID) {
2322 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00002323 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
2324 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002325 if (II->hasMacroDefinition() &&
2326 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
2327 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002328 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2329 DEnd = IdentifierResolver::end();
2330 D != DEnd; ++D)
2331 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002332 // We emit the key length after the data length so that the
2333 // "uninteresting" identifiers following the identifier hash table
2334 // structure will have the same (key length, key characters)
2335 // layout as the keys in the hash table. This also matches the
2336 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00002337 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002338 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002339 return std::make_pair(KeyLen, DataLen);
2340 }
2341
2342 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
2343 unsigned KeyLen) {
2344 // Record the location of the key data. This is used when generating
2345 // the mapping from persistent IDs to strings.
2346 Writer.SetIdentifierOffset(II, Out.tell());
2347 Out.write(II->getName(), KeyLen);
2348 }
2349
2350 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
2351 pch::IdentID ID, unsigned) {
2352 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002353 bool hasMacroDefinition =
2354 II->hasMacroDefinition() &&
2355 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002356 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002357 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
2358 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002359 Bits = (Bits << 1) | II->isExtensionToken();
2360 Bits = (Bits << 1) | II->isPoisoned();
2361 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
2362 clang::io::Emit32(Out, Bits);
2363 clang::io::Emit32(Out, ID);
2364
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002365 if (hasMacroDefinition)
2366 clang::io::Emit64(Out, Writer.getMacroOffset(II));
2367
Douglas Gregorc713da92009-04-21 22:25:48 +00002368 // Emit the declaration IDs in reverse order, because the
2369 // IdentifierResolver provides the declarations as they would be
2370 // visible (e.g., the function "stat" would come before the struct
2371 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2372 // adds declarations to the end of the list (so we need to see the
2373 // struct "status" before the function "status").
2374 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
2375 IdentifierResolver::end());
2376 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2377 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00002378 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00002379 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002380 }
2381};
2382} // end anonymous namespace
2383
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002384/// \brief Write the identifier table into the PCH file.
2385///
2386/// The identifier table consists of a blob containing string data
2387/// (the actual identifiers themselves) and a separate "offsets" index
2388/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002389void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002390 using namespace llvm;
2391
2392 // Create and write out the blob that contains the identifier
2393 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00002394 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002395 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002396 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
2397
Douglas Gregor85c4a872009-04-25 21:04:17 +00002398 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
2399
Douglas Gregorff9a6092009-04-20 20:36:09 +00002400 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002401 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
2402 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2403 ID != IDEnd; ++ID) {
2404 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00002405
2406 // Classify each identifier as either "interesting" or "not
2407 // interesting". Interesting identifiers are those that have
2408 // additional information that needs to be read from the PCH
2409 // file, e.g., a built-in ID, declaration chain, or macro
2410 // definition. These identifiers are placed into the hash table
2411 // so that they can be found when looked up in the user program.
2412 // All other identifiers are "uninteresting", which means that
2413 // the IdentifierInfo built by default has all of the
2414 // information we care about. Such identifiers are placed after
2415 // the hash table.
2416 const IdentifierInfo *II = ID->first;
2417 if (II->isPoisoned() ||
2418 II->isExtensionToken() ||
2419 II->hasMacroDefinition() ||
2420 II->getObjCOrBuiltinID() ||
2421 II->getFETokenInfo<void>())
2422 Generator.insert(ID->first, ID->second);
2423 else
2424 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002425 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002426
Douglas Gregorff9a6092009-04-20 20:36:09 +00002427 // Create the on-disk hash table in a buffer.
2428 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00002429 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002430 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002431 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002432 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002433 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00002434 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00002435 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002436
2437 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
2438 const IdentifierInfo *II = UninterestingIdentifiers[I];
2439 unsigned N = II->getLength() + 1;
2440 clang::io::Emit16(Out, N);
2441 SetIdentifierOffset(II, Out.tell());
2442 Out.write(II->getName(), N);
2443 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002444 }
2445
2446 // Create a blob abbreviation
2447 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2448 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002450 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002451 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002452
2453 // Write the identifier table
2454 RecordData Record;
2455 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002456 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002457 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2458 &IdentifierTable.front(),
2459 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002460 }
2461
2462 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002463 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2464 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2466 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2467 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2468
2469 RecordData Record;
2470 Record.push_back(pch::IDENTIFIER_OFFSET);
2471 Record.push_back(IdentifierOffsets.size());
2472 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2473 (const char *)&IdentifierOffsets.front(),
2474 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002475}
2476
Douglas Gregor1c507882009-04-15 21:30:51 +00002477/// \brief Write a record containing the given attributes.
2478void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2479 RecordData Record;
2480 for (; Attr; Attr = Attr->getNext()) {
2481 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2482 Record.push_back(Attr->isInherited());
2483 switch (Attr->getKind()) {
2484 case Attr::Alias:
2485 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2486 break;
2487
2488 case Attr::Aligned:
2489 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2490 break;
2491
2492 case Attr::AlwaysInline:
2493 break;
2494
2495 case Attr::AnalyzerNoReturn:
2496 break;
2497
2498 case Attr::Annotate:
2499 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2500 break;
2501
2502 case Attr::AsmLabel:
2503 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2504 break;
2505
2506 case Attr::Blocks:
2507 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2508 break;
2509
2510 case Attr::Cleanup:
2511 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2512 break;
2513
2514 case Attr::Const:
2515 break;
2516
2517 case Attr::Constructor:
2518 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2519 break;
2520
2521 case Attr::DLLExport:
2522 case Attr::DLLImport:
2523 case Attr::Deprecated:
2524 break;
2525
2526 case Attr::Destructor:
2527 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2528 break;
2529
2530 case Attr::FastCall:
2531 break;
2532
2533 case Attr::Format: {
2534 const FormatAttr *Format = cast<FormatAttr>(Attr);
2535 AddString(Format->getType(), Record);
2536 Record.push_back(Format->getFormatIdx());
2537 Record.push_back(Format->getFirstArg());
2538 break;
2539 }
2540
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002541 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002542 case Attr::IBOutletKind:
2543 case Attr::NoReturn:
2544 case Attr::NoThrow:
2545 case Attr::Nodebug:
2546 case Attr::Noinline:
2547 break;
2548
2549 case Attr::NonNull: {
2550 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2551 Record.push_back(NonNull->size());
2552 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2553 break;
2554 }
2555
2556 case Attr::ObjCException:
2557 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002558 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002559 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002560 case Attr::Overloadable:
2561 break;
2562
2563 case Attr::Packed:
2564 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2565 break;
2566
2567 case Attr::Pure:
2568 break;
2569
2570 case Attr::Regparm:
2571 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2572 break;
2573
2574 case Attr::Section:
2575 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2576 break;
2577
2578 case Attr::StdCall:
2579 case Attr::TransparentUnion:
2580 case Attr::Unavailable:
2581 case Attr::Unused:
2582 case Attr::Used:
2583 break;
2584
2585 case Attr::Visibility:
2586 // FIXME: stable encoding
2587 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2588 break;
2589
2590 case Attr::WarnUnusedResult:
2591 case Attr::Weak:
2592 case Attr::WeakImport:
2593 break;
2594 }
2595 }
2596
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002597 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002598}
2599
2600void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2601 Record.push_back(Str.size());
2602 Record.insert(Record.end(), Str.begin(), Str.end());
2603}
2604
Douglas Gregorff9a6092009-04-20 20:36:09 +00002605/// \brief Note that the identifier II occurs at the given offset
2606/// within the identifier table.
2607void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002608 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002609}
2610
Douglas Gregor2d711832009-04-25 17:48:32 +00002611/// \brief Note that the selector Sel occurs at the given offset
2612/// within the method pool/selector table.
2613void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2614 unsigned ID = SelectorIDs[Sel];
2615 assert(ID && "Unknown selector");
2616 SelectorOffsets[ID - 1] = Offset;
2617}
2618
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002619PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002620 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002621 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2622 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002623
Douglas Gregor87887da2009-04-20 15:53:59 +00002624void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002625 using namespace llvm;
2626
Douglas Gregor87887da2009-04-20 15:53:59 +00002627 ASTContext &Context = SemaRef.Context;
2628 Preprocessor &PP = SemaRef.PP;
2629
Douglas Gregorc34897d2009-04-09 22:27:44 +00002630 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002631 Stream.Emit((unsigned)'C', 8);
2632 Stream.Emit((unsigned)'P', 8);
2633 Stream.Emit((unsigned)'C', 8);
2634 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00002635
2636 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002637
2638 // The translation unit is the first declaration we'll emit.
2639 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2640 DeclsToEmit.push(Context.getTranslationUnitDecl());
2641
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002642 // Make sure that we emit IdentifierInfos (and any attached
2643 // declarations) for builtins.
2644 {
2645 IdentifierTable &Table = PP.getIdentifierTable();
2646 llvm::SmallVector<const char *, 32> BuiltinNames;
2647 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2648 Context.getLangOptions().NoBuiltin);
2649 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2650 getIdentifierRef(&Table.get(BuiltinNames[I]));
2651 }
2652
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002653 // Build a record containing all of the tentative definitions in
2654 // this header file. Generally, this record will be empty.
2655 RecordData TentativeDefinitions;
2656 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2657 TD = SemaRef.TentativeDefinitions.begin(),
2658 TDEnd = SemaRef.TentativeDefinitions.end();
2659 TD != TDEnd; ++TD)
2660 AddDeclRef(TD->second, TentativeDefinitions);
2661
Douglas Gregor062d9482009-04-22 22:18:58 +00002662 // Build a record containing all of the locally-scoped external
2663 // declarations in this header file. Generally, this record will be
2664 // empty.
2665 RecordData LocallyScopedExternalDecls;
2666 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2667 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2668 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2669 TD != TDEnd; ++TD)
2670 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2671
Douglas Gregorc34897d2009-04-09 22:27:44 +00002672 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002673 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002674 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002675 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002676 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002677 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002678 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002679
2680 // Keep writing types and declarations until all types and
2681 // declarations have been written.
2682 do {
2683 if (!DeclsToEmit.empty())
2684 WriteDeclsBlock(Context);
2685 if (!TypesToEmit.empty())
2686 WriteTypesBlock(Context);
2687 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2688
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002689 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002690 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002691
2692 // Write the type offsets array
2693 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2694 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2696 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2697 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2698 Record.clear();
2699 Record.push_back(pch::TYPE_OFFSET);
2700 Record.push_back(TypeOffsets.size());
2701 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2702 (const char *)&TypeOffsets.front(),
2703 TypeOffsets.size() * sizeof(uint64_t));
2704
2705 // Write the declaration offsets array
2706 Abbrev = new BitCodeAbbrev();
2707 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2710 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2711 Record.clear();
2712 Record.push_back(pch::DECL_OFFSET);
2713 Record.push_back(DeclOffsets.size());
2714 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2715 (const char *)&DeclOffsets.front(),
2716 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002717
2718 // Write the record of special types.
2719 Record.clear();
2720 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002721 AddTypeRef(Context.getObjCIdType(), Record);
2722 AddTypeRef(Context.getObjCSelType(), Record);
2723 AddTypeRef(Context.getObjCProtoType(), Record);
2724 AddTypeRef(Context.getObjCClassType(), Record);
2725 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2726 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002727 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2728
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002729 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002730 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002731 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002732
2733 // Write the record containing tentative definitions.
2734 if (!TentativeDefinitions.empty())
2735 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002736
2737 // Write the record containing locally-scoped external definitions.
2738 if (!LocallyScopedExternalDecls.empty())
2739 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2740 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002741
2742 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002743 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002744 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002745 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002746 Record.push_back(NumLexicalDeclContexts);
2747 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002748 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002749 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002750}
2751
2752void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2753 Record.push_back(Loc.getRawEncoding());
2754}
2755
2756void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2757 Record.push_back(Value.getBitWidth());
2758 unsigned N = Value.getNumWords();
2759 const uint64_t* Words = Value.getRawData();
2760 for (unsigned I = 0; I != N; ++I)
2761 Record.push_back(Words[I]);
2762}
2763
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002764void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2765 Record.push_back(Value.isUnsigned());
2766 AddAPInt(Value, Record);
2767}
2768
Douglas Gregore2f37202009-04-14 21:55:33 +00002769void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2770 AddAPInt(Value.bitcastToAPInt(), Record);
2771}
2772
Douglas Gregorc34897d2009-04-09 22:27:44 +00002773void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002774 Record.push_back(getIdentifierRef(II));
2775}
2776
2777pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2778 if (II == 0)
2779 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002780
2781 pch::IdentID &ID = IdentifierIDs[II];
2782 if (ID == 0)
2783 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002784 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002785}
2786
Steve Naroff9e84d782009-04-23 10:39:46 +00002787void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2788 if (SelRef.getAsOpaquePtr() == 0) {
2789 Record.push_back(0);
2790 return;
2791 }
2792
2793 pch::SelectorID &SID = SelectorIDs[SelRef];
2794 if (SID == 0) {
2795 SID = SelectorIDs.size();
2796 SelVector.push_back(SelRef);
2797 }
2798 Record.push_back(SID);
2799}
2800
Douglas Gregorc34897d2009-04-09 22:27:44 +00002801void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2802 if (T.isNull()) {
2803 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2804 return;
2805 }
2806
2807 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002808 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002809 switch (BT->getKind()) {
2810 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2811 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2812 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2813 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2814 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2815 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2816 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2817 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2818 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2819 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2820 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2821 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2822 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2823 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2824 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2825 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2826 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2827 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2828 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2829 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2830 }
2831
2832 Record.push_back((ID << 3) | T.getCVRQualifiers());
2833 return;
2834 }
2835
Douglas Gregorac8f2802009-04-10 17:25:41 +00002836 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002837 if (ID == 0) {
2838 // We haven't seen this type before. Assign it a new ID and put it
2839 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002840 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002841 TypesToEmit.push(T.getTypePtr());
2842 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002843
2844 // Encode the type qualifiers in the type reference.
2845 Record.push_back((ID << 3) | T.getCVRQualifiers());
2846}
2847
2848void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2849 if (D == 0) {
2850 Record.push_back(0);
2851 return;
2852 }
2853
Douglas Gregorac8f2802009-04-10 17:25:41 +00002854 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002855 if (ID == 0) {
2856 // We haven't seen this declaration before. Give it a new ID and
2857 // enqueue it in the list of declarations to emit.
2858 ID = DeclIDs.size();
2859 DeclsToEmit.push(const_cast<Decl *>(D));
2860 }
2861
2862 Record.push_back(ID);
2863}
2864
Douglas Gregorff9a6092009-04-20 20:36:09 +00002865pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2866 if (D == 0)
2867 return 0;
2868
2869 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2870 return DeclIDs[D];
2871}
2872
Douglas Gregorc34897d2009-04-09 22:27:44 +00002873void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2874 Record.push_back(Name.getNameKind());
2875 switch (Name.getNameKind()) {
2876 case DeclarationName::Identifier:
2877 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2878 break;
2879
2880 case DeclarationName::ObjCZeroArgSelector:
2881 case DeclarationName::ObjCOneArgSelector:
2882 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002883 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002884 break;
2885
2886 case DeclarationName::CXXConstructorName:
2887 case DeclarationName::CXXDestructorName:
2888 case DeclarationName::CXXConversionFunctionName:
2889 AddTypeRef(Name.getCXXNameType(), Record);
2890 break;
2891
2892 case DeclarationName::CXXOperatorName:
2893 Record.push_back(Name.getCXXOverloadedOperator());
2894 break;
2895
2896 case DeclarationName::CXXUsingDirective:
2897 // No extra data to emit
2898 break;
2899 }
2900}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002901
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002902/// \brief Write the given substatement or subexpression to the
2903/// bitstream.
2904void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002905 RecordData Record;
2906 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002907 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002908
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002909 if (!S) {
2910 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002911 return;
2912 }
2913
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002914 Writer.Code = pch::STMT_NULL_PTR;
2915 Writer.Visit(S);
2916 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002917 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002918 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002919}
2920
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002921/// \brief Flush all of the statements that have been added to the
2922/// queue via AddStmt().
2923void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002924 RecordData Record;
2925 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002926
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002927 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002928 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002929 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002930
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002931 if (!S) {
2932 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002933 continue;
2934 }
2935
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002936 Writer.Code = pch::STMT_NULL_PTR;
2937 Writer.Visit(S);
2938 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002939 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002940 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002941
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002942 assert(N == StmtsToEmit.size() &&
2943 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002944
2945 // Note that we are at the end of a full expression. Any
2946 // expression records that follow this one are part of a different
2947 // expression.
2948 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002949 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002950 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002951
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002952 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002953 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002954}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002955
2956unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2957 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2958 "SwitchCase recorded twice");
2959 unsigned NextID = SwitchCaseIDs.size();
2960 SwitchCaseIDs[S] = NextID;
2961 return NextID;
2962}
2963
2964unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2965 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2966 "SwitchCase hasn't been seen yet");
2967 return SwitchCaseIDs[S];
2968}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002969
2970/// \brief Retrieve the ID for the given label statement, which may
2971/// or may not have been emitted yet.
2972unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2973 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2974 if (Pos != LabelIDs.end())
2975 return Pos->second;
2976
2977 unsigned NextID = LabelIDs.size();
2978 LabelIDs[S] = NextID;
2979 return NextID;
2980}